Skip to content

Add UBI-aware NAND writes for install and restore#62

Merged
widgetii merged 2 commits intomasterfrom
fix/ubi-aware-nand-writes
Apr 22, 2026
Merged

Add UBI-aware NAND writes for install and restore#62
widgetii merged 2 commits intomasterfrom
fix/ubi-aware-nand-writes

Conversation

@widgetii
Copy link
Copy Markdown
Member

Summary

  • Raw UBI images (rootfs.ubi) written via nand write corrupt UBIFS because bad block skipping shifts data, breaking UBI's physical-to-logical block mapping
  • New defib.ubi module extracts UBIFS volume data from raw UBI images, then writes via ubi part / ubi create / ubi write which handles bad blocks correctly
  • defib install --nand now detects download command mode (XHEAD/XCMD) for chips with CONFIG_START_MAGIC (hi3516av200), page-aligns NAND writes, and uses UBI-aware rootfs writes
  • defib restore auto-converts raw UBI images to UBIFS and creates UBI volumes with full partition space (not just image size)

Test plan

  • 8 unit tests for UBI extraction (multi-LEB ordering, volume filtering, empty PEB handling)
  • 334 tests pass, lint clean
  • Verified on hi3516av200 hardware (128MB SPI NAND, MX35LF1GE4AB):
    • UBI attaches: 944 PEBs, 0 bad, 0 corrupted
    • UBIFS mounts: 111 MiB usable space (was 10 MiB with sized volume)
    • No "space left on device" errors
    • Linux boots fully, dropbear/syslog/crond start

Before (raw nand write):

UBIFS error: bad magic 0x6111831, expected 0x6101831
Kernel panic - not syncing: VFS: Unable to mount root fs

After (ubi write):

UBI: good PEBs: 944, bad PEBs: 0, corrupted PEBs: 0
UBIFS: mounted UBI device 0, volume 0, name "rootfs", R/O mode
UBIFS: FS size: 116817920 bytes (111 MiB, 920 LEBs)

🤖 Generated with Claude Code

widgetii and others added 2 commits April 22, 2026 23:31
Raw UBI images written via `nand write` corrupt UBIFS because bad block
skipping shifts data, breaking UBI's physical-to-logical block mapping.

This adds a UBI image parser (`defib.ubi`) that extracts UBIFS volume data
from raw UBI images (UBI# magic), then writes via `ubi part` / `ubi create`
/ `ubi write` which lets U-Boot's UBI subsystem handle bad block mapping.

Changes:
- New `src/defib/ubi.py`: parse UBI EC/VID headers, extract UBIFS by volume ID
- `defib install --nand`: detect raw UBI rootfs, extract UBIFS, write via
  `ubi write` instead of `nand write`; detect download command mode (XHEAD/XCMD)
  so install works on chips with CONFIG_START_MAGIC (e.g. hi3516av200);
  page-align NAND write sizes to 2KB
- `defib restore`: auto-detect raw UBI images in partition files and convert
  to UBIFS before UBI-aware write; create UBI volumes with full partition
  space (not just image size) so UBIFS has room for runtime writes
- 8 new tests for UBI extraction

Tested on hi3516av200 (128MB SPI NAND, MX35LF1GE4AB):
- UBI attaches cleanly: 944 PEBs, 0 bad, 0 corrupted
- UBIFS mounts without errors: 111 MiB usable (vs 10 MiB before)
- Linux boots fully, services start, no "space left" errors

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@widgetii widgetii merged commit 63467a0 into master Apr 22, 2026
13 checks passed
@widgetii widgetii deleted the fix/ubi-aware-nand-writes branch April 22, 2026 20:38
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