Files
BRNSystemsandClaude Opus 5.5 c778b728c0 Tuya-free image with a writable DATA partition
Strips the Tuya stack from APP and repartitions via mtdparts (the DTB has
no partition table), adding a jffs2 DATA partition for settings and the
overlay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 22:01:43 +01:00

122 lines
4.3 KiB
Bash
Executable File

#!/bin/sh
# Build the Tuya-free, repartitioned image:
# - strips the Tuya app layer from APP (anyka_ipc/msgserver/cmdserver/...)
# - shrinks APP, adds a writable jffs2 DATA partition (~2944K)
# - wifi station + telnet + serial root shell + big writable overlay on DATA
# NOTE: needs the one-time U-Boot mtdparts change in layout-local.conf.
set -e
HERE="$(cd "$(dirname "$0")/.." && pwd)"
[ "${WIFI_SSID+set}" = set ] || { echo "set WIFI_SSID=... WIFI_PSK=..."; exit 1; }
WIFI_PSK=${WIFI_PSK-}
SET="$HERE/parts/local"
LAYOUT="$HERE/layout-local.conf"
WORK=$(mktemp -d); trap 'rm -rf "$WORK"' EXIT
mkdir -p "$SET"
# raw + unchanged fs parts from stock
for f in uboot env envbk dtb kernel rootfs config; do cp -a "$HERE/parts/stock/$f.bin" "$SET/"; done
# ---- stripped, patched APP tree ----
"$HERE/extract-src.sh"
cp -a "$HERE/src/app" "$WORK/app"
cd "$WORK/app"
find . -depth -name '*.extracted' -type d -exec rm -rf {} + 2>/dev/null || true # binwalk junk
rm -f bin/anyka_ipc bin/msgserver bin/cmdserver bin/system_upgrade # Tuya userland
rm -f local/model_0x0A000000_V1.1.0.bin # NPU AI model
rm -f share/*.mp3 # Tuya voice/siren
# local bootstrap: DATA overlay -> wifi -> dhcp -> telnet + serial shell
cat > sbin/local_bootstrap.sh <<'EOS'
#!/bin/sh
PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin
exec >/tmp/local_bootstrap.log 2>&1
echo "[local] start $(date)"
# --- writable store: prefer the big jffs2 DATA partition (mtd8), else CONFIG ---
OVROOT=/etc/config/overlay
if [ -e /dev/mtdblock8 ]; then
mkdir -p /data
if mount -t jffs2 /dev/mtdblock8 /data 2>/dev/null; then
OVROOT=/data/overlay
echo "[local] DATA mounted rw at /data"
else
echo "[local] WARN: DATA mount failed; using CONFIG overlay"
fi
fi
echo "$OVROOT" > /tmp/ovroot
mkdir -p "$OVROOT"
# bind-mount every file under $OVROOT over its real path (edit/replace any file)
find "$OVROOT" -type f 2>/dev/null | while read f; do
tgt=${f#$OVROOT}
[ -e "$tgt" ] && mount -o bind "$f" "$tgt" && echo "[ovl] bind $tgt"
done
# persistent serial root shell
(setsid /bin/sh </dev/ttySAK0 >/dev/ttySAK0 2>&1) &
# wifi station
insmod /usr/modules/cfg80211.ko 2>/dev/null
/usr/sbin/wifi_driver.sh sta
sleep 2
for c in /data/wpa_supplicant.conf /etc/config/wpa_supplicant.conf; do
[ -f "$c" ] && CONF="$c" && break
done
if [ -z "$CONF" ]; then
CONF=/tmp/wpa_supplicant.conf
cat > "$CONF" <<WPA
ctrl_interface=/var/run/wpa_supplicant
update_config=1
network={
scan_ssid=1
ssid="__WIFI_SSID__"
psk="__WIFI_PSK__"
}
WPA
fi
wpa_supplicant -B -iwlan0 -Dnl80211 -c "$CONF"
i=0; while [ $i -lt 30 ]; do
[ "$(wpa_cli -iwlan0 status 2>/dev/null | sed -n 's/^wpa_state=//p')" = "COMPLETED" ] && break
sleep 1; i=$((i+1)); done
echo "[local] wpa_state=$(wpa_cli -iwlan0 status 2>/dev/null|sed -n 's/^wpa_state=//p') after ${i}s"
udhcpc -i wlan0 -b
ifconfig wlan0
telnetd -l /bin/sh
echo "[local] ready: telnet :23 (no password), overlay=$OVROOT"
EOS
chmod 0755 sbin/local_bootstrap.sh
sed -i "s/__WIFI_SSID__/$WIFI_SSID/; s/__WIFI_PSK__/$WIFI_PSK/" sbin/local_bootstrap.sh
# on-device helper: make any file writable + persistent
cat > sbin/rw <<'EOS'
#!/bin/sh
f=$1; OV=$(cat /tmp/ovroot 2>/dev/null); OV=${OV:-/etc/config/overlay}
[ -z "$f" ] && { echo "usage: rw /abs/path/file (overlay: $OV)"; exit 1; }
mkdir -p "$OV$(dirname "$f")"
[ -e "$OV$f" ] || cp -a "$f" "$OV$f"
mount -o bind "$OV$f" "$f" 2>/dev/null
echo "editable+persistent: $f (backing: $OV$f)"
EOS
chmod 0755 sbin/rw
# run our bootstrap instead of the Tuya service stack
sed -i 's#^service.sh start &#/usr/sbin/local_bootstrap.sh \&#' sbin/main.sh
cd "$HERE"
# repack stripped APP (slot 0x220000)
"$HERE/repack-fs.sh" "$WORK/app" "$SET/app.bin" $((0x220000)) squashfs
# empty jffs2 DATA for 4KiB-erase NOR = one cleanmarker per 4K block + 0xFF
# (byte-for-byte what `flash_erase -j /dev/mtd8` would produce on this chip)
python3 -Ic "
import sys
SIZE=0x2e0000; EB=0x1000
cm=bytes.fromhex('851903200c000000b1b01ee4')
blk=cm+b'\xff'*(EB-len(cm))
open('$SET/data.bin','wb').write(blk*(SIZE//EB))
"
echo "DATA jffs2 (empty, 4K cleanmarkers): $(stat -c %s "$SET/data.bin") bytes"
# assemble with the repartitioned layout
LAYOUT="$LAYOUT" "$HERE/build-image.sh" "$SET" "$HERE/out/camera_flash_local.bin"