#!/bin/sh # camerarev SD-card installer - runs from a temporary rootfs that stock U-Boot wrote # from the SD card (ht_recover_fw.conf, type 10 = rootfs region only). # At this point the camera's original APP (Tuya firmware), U-Boot, env, DTB, kernel and # CONFIG are all still untouched, so we: # 1. dump the COMPLETE 8 MiB flash to SD:/original-backup/ (rootfs region: see below) # 2. verify the install files, copy them to RAM # 3. write APP+DATA, DTB, the original rootfs, then the U-Boot env (last), and reboot # Every step is restartable: if power fails before the env is written, U-Boot simply # writes this installer again at the next power-on and it starts over (backup is kept). PATH=/bin:/sbin:/usr/bin:/usr/sbin exec >/dev/console 2>&1 say() { echo "[camerarev-install] $*"; [ -n "$LOG" ] && echo "$(date) $*" >> "$LOG"; } led() { [ -x /usr/sbin/red_led.sh ] && sh /usr/sbin/red_led.sh "$1" >/dev/null 2>&1; } fail() { say "FAILED: $* - camera left as it is; fix the SD card and power-cycle to retry"; led on; exit 1; } say "start" led on # SD card: the mmc drivers live in the (still original) APP partition for m in mmc_core mmc_block ak_mci; do insmod /usr/modules/$m.ko 2>/dev/null; done mkdir -p /mnt/sd; i=0 while [ $i -lt 10 ]; do mount -t vfat /dev/mmcblk0p1 /mnt/sd 2>/dev/null || mount -t vfat /dev/mmcblk0 /mnt/sd 2>/dev/null grep -q ' /mnt/sd ' /proc/mounts && break sleep 1; i=$((i+1)) done grep -q ' /mnt/sd ' /proc/mounts || fail "cannot mount the SD card (FAT32 needed)" D=/mnt/sd/camerarev; B=/mnt/sd/original-backup; LOG=/mnt/sd/camerarev-install.log R=/tmp/camerarev; mkdir -p $R say "SD mounted" # ---------------------------------------------------------------- 1. full backup # Stock layout: UBOOT ENV ENVBK DTB KERNEL ROOTFS CONFIG APP, contiguous over the 8 MiB. # The ROOTFS region now holds THIS installer, so it is replaced in full.bin by the stock # rootfs from the build's dump (camerarev/rootfs.bin). The rootfs is the vendor's generic # read-only image; UBOOT/KERNEL are compared with the same dump to show they match. if [ ! -f $B/full.bin ]; then say "backing up the whole flash (about 30 s)" mkdir -p $B.tmp; rm -f $B.tmp/* sed -n 's/^mtd\([0-9]*\): \([0-9a-f]*\) .*"\(.*\)"$/\1 \2 \3/p' /proc/mtd > $R/mtdlist while read n size name; do cat /dev/mtd$n > $B.tmp/$name.bin || fail "reading mtd$n ($name)" done < $R/mtdlist mv $B.tmp/ROOTFS.bin $B.tmp/ROOTFS.installer.bin cp $D/rootfs.bin $B.tmp/ROOTFS.bin : > $B.tmp/full.bin for name in UBOOT ENV ENVBK DTB KERNEL ROOTFS CONFIG APP; do [ -f $B.tmp/$name.bin ] || fail "partition $name missing in /proc/mtd" cat $B.tmp/$name.bin >> $B.tmp/full.bin done [ "$(wc -c < $B.tmp/full.bin)" -eq 8388608 ] || fail "full.bin is not 8 MiB" { echo "Original flash of this camera, saved by the camerarev SD installer on $(date)." echo "full.bin = whole 8 MiB chip (flashrom -w full.bin restores stock completely)." echo "ROOTFS region: the generic stock rootfs from the build's reference dump, because" echo "U-Boot had already replaced it with the installer when this ran." for p in UBOOT KERNEL; do ref=$(sed -n "s/ $p\$//p" $D/ref.md5); got=$(md5sum < $B.tmp/$p.bin | cut -d' ' -f1) [ "$ref" = "$got" ] && echo "$p: identical to the reference dump -> same firmware build, rootfs match expected" \ || echo "$p: DIFFERS from the reference dump -> the rootfs in full.bin may not be this camera's" done } > $B.tmp/README.txt ( cd $B.tmp && md5sum *.bin > MD5SUMS ) || fail "md5" sync; mv $B.tmp $B; sync say "backup done: SD:/original-backup/ ($(grep -c DIFFERS $B/README.txt) mismatches vs reference)" else say "backup already on the card - keeping it" fi # ---------------------------------------------------------------- 2. verify + stage in RAM ( cd $D && md5sum -c MD5SUMS ) >$R/md5.out 2>&1 || { cat $R/md5.out; fail "install files corrupt (MD5SUMS)"; } for f in usr.sqsh4 dtb.bin env.bin envbk.bin rootfs.bin mtdflash; do cp $D/$f $R/$f || fail "copy $f"; done ( cd $R && md5sum usr.sqsh4 dtb.bin env.bin envbk.bin rootfs.bin mtdflash ) > $R/ram.md5 grep -v ' MD5SUMS$' $D/MD5SUMS | while read sum f; do grep -q "^$sum $f\$" $R/ram.md5 || { echo "bad"; break; } done | grep -q bad && fail "RAM copy mismatch" chmod 755 $R/mtdflash say "install files verified" # ---------------------------------------------------------------- 3. write + reboot # ENV last: until it is written U-Boot keeps re-running this installer on power-up. say "writing APP+DATA, DTB, rootfs, U-Boot env - do not power off (about 2 min)" umount /mnt/sd 2>/dev/null; sync exec $R/mtdflash -f -r APP $R/usr.sqsh4 DTB $R/dtb.bin ROOTFS $R/rootfs.bin \ ENVBK $R/envbk.bin ENV $R/env.bin