mtdflash, OTA updates and the U-Boot SD updater
mtdflash: static, verified MTD writer (per-block skip/erase/write/read-back, validates all images before writing, refuses U-Boot/env without -f). fwupdate + ota.sh flash a running camera over SSH; make-sd-update.sh builds a camupdate.env that the new bootcmd applies from the SD card. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
No files matched your search
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/bin/sh
|
||||
# fwupdate FILE... : flash images from Linux (no serial needed), then reboot.
|
||||
# Partition is picked from the file name: app*.bin->APP dtb*->DTB kernel*->KERNEL
|
||||
# rootfs*->ROOTFS config*->CONFIG data*->DATA, or say it explicitly: APP=/tmp/x.bin
|
||||
# -n = don't reboot afterwards. Every block is read-back verified.
|
||||
RB=-r; [ "$1" = -n ] && { RB=; shift; }
|
||||
[ $# -gt 0 ] || { sed -n '2,6p' "$0"; exit 2; }
|
||||
ARGS=
|
||||
for a; do
|
||||
case "$a" in
|
||||
*=*) p=${a%%=*}; f=${a#*=} ;;
|
||||
*) f=$a; case "$(basename "$f")" in
|
||||
app*) p=APP ;; dtb*) p=DTB ;; kernel*|uImage*) p=KERNEL ;;
|
||||
rootfs*) p=ROOTFS ;; config*) p=CONFIG ;; data*) p=DATA ;;
|
||||
*) echo "can't tell partition for $f (use PART=$f)"; exit 1 ;; esac ;;
|
||||
esac
|
||||
[ -f "$f" ] || { echo "no such file: $f"; exit 1; }
|
||||
ARGS="$ARGS $p $f"
|
||||
done
|
||||
# run the writer from RAM: APP (where we live) is about to be overwritten
|
||||
cp /usr/sbin/mtdflash /tmp/mtdflash && chmod 755 /tmp/mtdflash || exit 1
|
||||
touch /tmp/rtsp.stop; killall ak_rtsp_server_sample onvif_simple_server wsd_simple_server 2>/dev/null
|
||||
echo 3 > /proc/sys/vm/drop_caches
|
||||
exec /tmp/mtdflash $RB $ARGS
|
||||
Executable
+25
@@ -0,0 +1,25 @@
|
||||
#!/bin/sh
|
||||
# Build an SD-card recovery/update set that U-Boot flashes at power-on (no Linux,
|
||||
# no serial needed once the one-time bootcmd from README is set).
|
||||
# ./make-sd-update.sh /path/to/sdcard parts/rtsp/app.bin parts/rtsp/dtb.bin ...
|
||||
# Copies the images + writes camupdate.env. Linux renames it to camupdate.done after a
|
||||
# good boot so it's only applied once. Offsets = layout-rtsp.conf (LAYOUT= overrides).
|
||||
set -e
|
||||
[ $# -ge 2 ] || { sed -n '2,6p' "$0"; exit 2; }
|
||||
HERE="$(cd "$(dirname "$0")" && pwd)"; LAYOUT=${LAYOUT:-$HERE/layout-rtsp.conf}
|
||||
DST=$1; shift; mkdir -p "$DST"
|
||||
S="sf probe 0"
|
||||
for f in "$@"; do
|
||||
n=$(basename "$f")
|
||||
case "$n" in app*) p=APP;; dtb*) p=DTB;; kernel*) p=KERNEL;; rootfs*) p=ROOTFS;;
|
||||
config*) p=CONFIG;; data*) p=DATA;; *) echo "unknown partition for $n"; exit 1;; esac
|
||||
set -- $(awk -v p=$p '$1==p {print $2, $3}' "$LAYOUT"); off=$1; size=$2
|
||||
[ -n "$off" ] || { echo "$p not in $LAYOUT"; exit 1; }
|
||||
[ $(wc -c < "$f") -le $((size)) ] || { echo "$n bigger than $p"; exit 1; }
|
||||
cp "$f" "$DST/$n"
|
||||
S="$S; setenv filesize 0; fatload mmc 0 \${loadaddr} $n; sf update \${loadaddr} $off \${filesize}"
|
||||
echo " $n -> $p @ $off"
|
||||
done
|
||||
printf 'sdscript=%s\n' "$S" > "$DST/camupdate.env"
|
||||
rm -f "$DST/camupdate.done"
|
||||
echo "wrote $DST/camupdate.env"
|
||||
Executable
+18
@@ -0,0 +1,18 @@
|
||||
#!/bin/sh
|
||||
# Flash a running camera over SSH: ./ota.sh <camera-ip> parts/rtsp/app.bin [parts/rtsp/dtb.bin ...]
|
||||
# Uploads to /tmp (RAM), then fwupdate verifies + flashes + reboots. Keep file names
|
||||
# starting with app/dtb/kernel/rootfs/config/data so fwupdate knows the partition.
|
||||
set -e
|
||||
[ $# -ge 2 ] || { sed -n '2,4p' "$0"; exit 2; }
|
||||
IP=$1; shift
|
||||
REMOTE=
|
||||
for f in "$@"; do
|
||||
n=$(basename "$f")
|
||||
echo "upload $n ($(wc -c < "$f") bytes)"
|
||||
ssh root@"$IP" "cat > /tmp/$n" < "$f"
|
||||
l=$(md5sum < "$f" | cut -d' ' -f1); r=$(ssh root@"$IP" "md5sum /tmp/$n" | cut -d' ' -f1)
|
||||
[ "$l" = "$r" ] || { echo "md5 mismatch for $n"; exit 1; }
|
||||
REMOTE="$REMOTE /tmp/$n"
|
||||
done
|
||||
ssh root@"$IP" "fwupdate $REMOTE" || true
|
||||
echo "camera is rebooting (if all verified); wait ~40s then ssh back in"
|
||||
@@ -0,0 +1,91 @@
|
||||
/* mtdflash: safely write an image to a NOR MTD partition from Linux.
|
||||
* Per erase block: skip if identical, else erase + write + read-back verify (3 tries).
|
||||
* Image is padded with 0xFF to a block boundary; must fit the partition.
|
||||
* usage: mtdflash [-r] [-f] PART IMAGE [PART IMAGE ...]
|
||||
* PART = partition name from /proc/mtd (APP, DTB, KERNEL...) or /dev/mtdN
|
||||
* -r reboot when everything verified OK -f allow UBOOT/ENV/ENVBK
|
||||
* All images are size-checked before anything is written. Ignores SIGHUP/SIGPIPE,
|
||||
* so a dropped SSH session can't interrupt it. Static, no deps -> run it from /tmp
|
||||
* while the flash being written is mounted. */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/reboot.h>
|
||||
#include <signal.h>
|
||||
#include <mtd/mtd-user.h>
|
||||
|
||||
static int find_mtd(const char *name, char *dev, size_t n) {
|
||||
FILE *f = fopen("/proc/mtd", "r"); char line[256]; int idx; char nm[64];
|
||||
if (!f) return -1;
|
||||
while (fgets(line, sizeof line, f))
|
||||
if (sscanf(line, "mtd%d: %*x %*x \"%63[^\"]\"", &idx, nm) == 2 && !strcmp(nm, name)) {
|
||||
snprintf(dev, n, "/dev/mtd%d", idx); fclose(f); return 0; }
|
||||
fclose(f); return -1;
|
||||
}
|
||||
|
||||
static int resolve(const char *p, char *dev, size_t n) {
|
||||
if (p[0] == '/') { snprintf(dev, n, "%s", p); return 0; }
|
||||
if (find_mtd(p, dev, n)) { fprintf(stderr, "no mtd partition named %s\n", p); return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int flash_one(const char *dev, const char *img) {
|
||||
struct mtd_info_user mi; struct stat st;
|
||||
int fd = open(dev, O_RDWR); if (fd < 0) { perror(dev); return 1; }
|
||||
if (ioctl(fd, MEMGETINFO, &mi)) { perror("MEMGETINFO"); return 1; }
|
||||
int in = open(img, O_RDONLY); if (in < 0 || fstat(in, &st)) { perror(img); return 1; }
|
||||
unsigned bs = mi.erasesize, nblk = (st.st_size + bs - 1) / bs, skipped = 0, written = 0;
|
||||
unsigned char *buf = malloc(bs), *cur = malloc(bs);
|
||||
printf("%s -> %s (%ld bytes, %u x %uK blocks)\n", img, dev, (long)st.st_size, nblk, bs / 1024);
|
||||
for (unsigned b = 0; b < nblk; b++) {
|
||||
off_t off = (off_t)b * bs; memset(buf, 0xff, bs);
|
||||
if (pread(in, buf, bs, off) < 0) { perror("read image"); return 1; }
|
||||
if (pread(fd, cur, bs, off) == (ssize_t)bs && !memcmp(cur, buf, bs)) { skipped++; continue; }
|
||||
int ok = 0;
|
||||
for (int t = 0; t < 3 && !ok; t++) {
|
||||
struct erase_info_user ei = { (unsigned)off, bs };
|
||||
if (ioctl(fd, MEMERASE, &ei)) { perror("MEMERASE"); continue; }
|
||||
if (pwrite(fd, buf, bs, off) != (ssize_t)bs) { perror("write"); continue; }
|
||||
ok = pread(fd, cur, bs, off) == (ssize_t)bs && !memcmp(cur, buf, bs);
|
||||
}
|
||||
if (!ok) { fprintf(stderr, "FAILED at block %u (0x%lx) - do NOT reboot, retry!\n", b, (long)off); return 1; }
|
||||
if (++written % 64 == 0) { printf(" %u/%u\n", b + 1, nblk); fflush(stdout); }
|
||||
}
|
||||
printf(" ok: %u blocks written, %u unchanged\n", written, skipped); fflush(stdout);
|
||||
close(fd); close(in); free(buf); free(cur); return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int rb = 0, force = 0, i = 1; char dev[16][64];
|
||||
signal(SIGHUP, SIG_IGN); signal(SIGPIPE, SIG_IGN);
|
||||
for (; i < argc && argv[i][0] == '-' && argv[i][1]; i++) {
|
||||
if (!strcmp(argv[i], "-r")) rb = 1; else if (!strcmp(argv[i], "-f")) force = 1;
|
||||
else { fprintf(stderr, "bad option %s\n", argv[i]); return 2; }
|
||||
}
|
||||
int n = (argc - i) / 2;
|
||||
if (n < 1 || n > 16 || (argc - i) % 2) {
|
||||
fprintf(stderr, "usage: %s [-r] [-f] PART IMAGE [PART IMAGE ...]\n", argv[0]); return 2; }
|
||||
for (int k = 0; k < n; k++) { /* validate everything first */
|
||||
const char *p = argv[i + 2 * k], *img = argv[i + 2 * k + 1];
|
||||
struct mtd_info_user mi; struct stat st;
|
||||
if (!force && (!strcmp(p, "UBOOT") || !strcmp(p, "ENV") || !strcmp(p, "ENVBK"))) {
|
||||
fprintf(stderr, "refusing to write %s without -f\n", p); return 1; }
|
||||
if (resolve(p, dev[k], sizeof dev[k])) return 1;
|
||||
int fd = open(dev[k], O_RDONLY);
|
||||
if (fd < 0 || ioctl(fd, MEMGETINFO, &mi)) { perror(dev[k]); return 1; }
|
||||
close(fd);
|
||||
if (stat(img, &st)) { perror(img); return 1; }
|
||||
if (st.st_size == 0 || (unsigned long)st.st_size > mi.size) {
|
||||
fprintf(stderr, "%s: %ld bytes doesn't fit %s (%s, %u bytes)\n", img, (long)st.st_size, p, dev[k], mi.size);
|
||||
return 1; }
|
||||
}
|
||||
for (int k = 0; k < n; k++)
|
||||
if (flash_one(dev[k], argv[i + 2 * k + 1])) return 1;
|
||||
sync();
|
||||
if (rb) { printf("all verified, rebooting\n"); fflush(stdout); sleep(1); reboot(RB_AUTOBOOT); }
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user