diff --git a/toolkit/layout-local.conf b/toolkit/layout-local.conf new file mode 100644 index 0000000..e254890 --- /dev/null +++ b/toolkit/layout-local.conf @@ -0,0 +1,15 @@ +# Anyka AK3918AV130 — 8 MiB SPI-NOR, REPARTITIONED (Tuya-free + writable DATA) +# APP shrunk to hold the stripped (Tuya-free) squashfs; freed space becomes a +# writable jffs2 DATA partition. Requires a one-time U-Boot mtdparts change: +# setenv mtdparts 'mtdparts=spi-nor:200k@0x0(UBOOT),4k@0x32000(ENV),4k@0x33000(ENVBK),64k@0x34000(DTB),1536k@0x44000(KERNEL),1008k@0x1c4000(ROOTFS),256k@0x2c0000(CONFIG),2176k@0x300000(APP),2944k@0x520000(DATA)' +# saveenv +# name offset size kind file +UBOOT 0x000000 0x032000 raw uboot.bin +ENV 0x032000 0x001000 raw env.bin +ENVBK 0x033000 0x001000 raw envbk.bin +DTB 0x034000 0x010000 raw dtb.bin +KERNEL 0x044000 0x180000 raw kernel.bin +ROOTFS 0x1c4000 0x0fc000 fs rootfs.bin +CONFIG 0x2c0000 0x040000 fs config.bin +APP 0x300000 0x220000 fs app.bin +DATA 0x520000 0x2e0000 fs data.bin diff --git a/toolkit/variants/local.sh b/toolkit/variants/local.sh new file mode 100755 index 0000000..24770c1 --- /dev/null +++ b/toolkit/variants/local.sh @@ -0,0 +1,121 @@ +#!/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 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" </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"