Files
actionkamera/toolkit/src/mtdflash.c
T
BRNSystemsandClaude Opus 5.5 1005aad61f 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>
2026-11-23 22:01:43 +01:00

92 lines
4.6 KiB
C

/* 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;
}