Skip to content

Fix restore: write boot partition last, use mtdparts offsets#58

Merged
widgetii merged 1 commit intomasterfrom
fix/restore-boot-last
Apr 22, 2026
Merged

Fix restore: write boot partition last, use mtdparts offsets#58
widgetii merged 1 commit intomasterfrom
fix/restore-boot-last

Conversation

@widgetii
Copy link
Copy Markdown
Member

Summary

Two bugs in defib restore for NAND:

  1. Boot partition corruption: setenv commands during restore (mtdparts, ipaddr, serverip) cause U-Boot to save env to NAND at offset 0x40000 within the boot area, corrupting mtd0. Fix: write boot partition last so the final nand write overwrites any env corruption.

  2. Wrong partition offsets: offsets were accumulated from image data sizes, which breaks when partitions are reordered or UBI images are smaller than their NAND partitions. Fix: pre-compute offsets from the original sequential partition order.

End-to-end test result

Full 7-partition NAND restore on hi3516av200 (128MB SPI NAND):

  mtd1: 4096KB → 0x100000   nand write    (4.5s)
  mtd2: 5084KB → 0x500000   ubi write     (11.0s)  ✓ rootfs
  mtd3: 12896KB → 0xD00000  ubi write     (11.1s)  ✓ data
  mtd4: 12896KB → 0x1D00000 ubi write     (11.1s)  ✓ upgradefs
  mtd5: 60512KB → 0x2D00000 ubi write     (34.1s)  ✓ appfs
  mtd6: 15872KB → 0x6D00000 ubi write     (11.2s)  ✓ exdata
  mtd0: 1024KB → 0x0        nand write    (3.5s)   ✓ boot (LAST)

Cold boot: all UBIFS volumes mount, vendor firmware runs (Sony IMX385 sensor, RTSP server, cloud login). Zero errors.

Test plan

  • 326 tests pass, lint + mypy clean
  • Full restore on hi3516av200 hardware — all 7 partitions
  • Cold boot — all UBIFS volumes mount, no corruption
  • Vendor firmware fully operational (sensor, network, RTSP)

🤖 Generated with Claude Code

During restore, setenv commands (mtdparts, ipaddr, serverip) cause
U-Boot to save env to NAND within the boot area at offset 0x40000,
corrupting the boot partition. Fix: write boot partition last so the
final nand write overwrites any env corruption.

Also fix partition offset calculation: use pre-computed offsets from
the original sequential partition order instead of accumulating from
data sizes (which breaks when partitions are reordered or UBI images
are smaller than their NAND partitions).

Tested end-to-end on hi3516av200: full 7-partition NAND restore
including 5 UBI volumes. Cold boot succeeds, all UBIFS volumes
mount cleanly, vendor firmware runs (Sony IMX385 sensor, RTSP, cloud).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@widgetii widgetii merged commit 68484da into master Apr 22, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant