Skip to content

sysupgrade: add NAND flash support#2012

Open
widgetii wants to merge 2 commits intomasterfrom
sysupgrade-nand-support
Open

sysupgrade: add NAND flash support#2012
widgetii wants to merge 2 commits intomasterfrom
sysupgrade-nand-support

Conversation

@widgetii
Copy link
Copy Markdown
Member

@widgetii widgetii commented Apr 16, 2026

Summary

  • sysupgrade previously hardcoded NOR/squashfs firmware downloads and used flashcp for all flash types, which bricks NAND cameras (boot expects ubifs, not squashfs)
  • download_firmware() now selects the correct NAND package ($soc-nand-$osr) when ipcinfo -F reports nand
  • do_update_kernel() uses flash_eraseall + nandwrite -p for NAND instead of flashcp
  • New do_update_rootfs_nand() function: pivot_roots to tmpfs, detaches UBI, erases the MTD partition, writes the UBI image with nandwrite, and reboots
  • do_wipe_overlay() uses ubiupdatevol -t for NAND overlay wipe instead of flash_eraseall -j (JFFS2-specific)

Known limitation

NAND rootfs update erases the entire UBI partition, which includes both rootfs and rootfs_data (overlay). This means overlay data (settings, SSH keys, etc.) is lost during rootfs update. Preserving overlay would require ubiupdatevol with a bare UBIFS image, which is not currently included in release packages. Re-attaching UBI after nandwrite to restore overlay data is unsafe — it causes image_seq mismatch and kernel panic.

Test log (hi3516av200, 128MB SPI NAND)

sysupgrade -k -r -f -z output
OpenIPC System Updater v1.0.48

Vendor  hisilicon
SoC     hi3516av200
Kernel  23:23:49 2026-04-15
RootFS  master+1783691, 2026-04-15

Stop services, sync files, free up memory
Stopping crond: OK
Stopping ntpd: OK
Stopping klogd: OK
Stopping syslogd: OK

Firmware
Download from https://github.com/OpenIPC/firmware/releases/download/latest/openipc.hi3516av200-nand-ultimate.tgz
Received and unpacked

Kernel
Update kernel from /tmp/uImage.hi3516av200
Erasing kernel partition...
Erasing 128 Kibyte @ 300000 - 100% complete.
Writing kernel...
Writing at 0x001c0000
Kernel updated to 11:17:22 2026-04-16

RootFS
Update rootfs (NAND) from /tmp/rootfs.ubi.hi3516av200
UBI partition: /dev/mtd3 (mtd3)
Preparing tmpfs root for NAND upgrade...
Performing pivot_root to tmpfs and flashing NAND...
Unmounting old root filesystems...
Detaching UBI from mtd3...
Erasing /dev/mtd3...
Erasing 128 Kibyte @ 7c00000 - 100% complete.
Writing rootfs.ubi to /dev/mtd3...
Writing at 0x00c00000
NAND upgrade complete. Rebooting...
Post-upgrade verification
--- os-release ---
OPENIPC_VERSION=2.6.04.16
GITHUB_VERSION="master+22307c9, 2026-04-16"
BUILD_OPTION=ultimate

--- MTD partitions ---
mtd0: 00040000 00020000 "boot"
mtd1: 000c0000 00020000 "wtf"
mtd2: 00300000 00020000 "kernel"
mtd3: 07c00000 00020000 "ubi"
mtd4: 02017000 0001f000 "rootfs"
mtd5: 05521000 0001f000 "rootfs_data"

--- Mount points ---
ubi0:rootfs on /rom type ubifs (ro,relatime)
ubi0:rootfs_data on /overlay type ubifs (rw,relatime)
overlay on / type overlay (rw,relatime,...)

--- Disk usage ---
ubi0:rootfs    27.8M  9.2M  18.6M  33% /rom
ubi0:rootfs_data 76.8M 28.0K 72.8M  0% /overlay

--- Uptime ---
 11:27:24 up 1 min

Test plan

  • Tested on hi3516av200 with 128MB SPI NAND — upgrade from OpenIPC 2.2 (Nov 2022) to 2.6 (Apr 2026) succeeded
  • Tested second upgrade on same board — confirmed pivot_root works reliably
  • Verified overlay is erased (clean /overlay after upgrade)
  • Test on other NAND-based boards
  • Test NOR cameras are unaffected (no code path changes for NOR)
  • Test standalone overlay wipe (sysupgrade -n) on NAND

🤖 Generated with Claude Code

The sysupgrade script previously hardcoded NOR/squashfs firmware
downloads and used flashcp for all flash types. This bricks NAND
cameras which boot with rootfstype=ubifs, since flashing squashfs
to a UBI volume produces an unmountable rootfs.

Changes:
- download_firmware: select nand package when ipcinfo -F reports nand
- do_update_kernel: use flash_eraseall + nandwrite for NAND partitions
- do_update_rootfs_nand: new function that pivot_roots to tmpfs,
  detaches UBI, erases the MTD partition, and writes the UBI image
  with nandwrite before rebooting
- do_wipe_overlay: use ubiupdatevol -t for NAND overlay wipe instead
  of flash_eraseall -j which is JFFS2-specific

Tested on hi3516av200 with 128MB SPI NAND, upgrading from OpenIPC
2.2 (Nov 2022) to 2.6 (Apr 2026) successfully.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Re-attaching UBI after nandwrite to restore overlay data causes
image_seq mismatch — the rootfs_data autoresize writes PEBs with
a different sequence number than the original rootfs.ubi image,
resulting in kernel panic on boot (UBI attach fails).

Add a warning that NAND rootfs update erases overlay data.
Overlay preservation would require ubiupdatevol with a bare UBIFS
image, which is not currently included in release packages.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator

@ZigFisher ZigFisher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will break sigmastar nand devices, the old solution was utilizing gluebi as compatibility layer to overwrite the partitions without ubi tools.

@widgetii
Copy link
Copy Markdown
Member Author

Are you comfortable making these changes only for HiSilicon devices, which I can currently test?

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.

3 participants