Skip to content

backup, upload, report: read partitions through /dev/mtdN (#283) - #284

Merged
openipc-ai merged 2 commits into
masterfrom
backup-mtdchar-283
Nov 14, 2026
Merged

openipc-ai merged 2 commits into
masterfrom
backup-mtdchar-283

Conversation

@openipc-ai

@openipc-ai openipc-ai commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Closes #283.

What was wrong

A NAND page that ECC cannot correct makes mtd_read() return -EBADMSG, with the data still read. /dev/mtdN's read() returns that data (which is why dd if=/dev/mtd3 worked in the issue). /dev/mtdblockN turns it into Buffer I/O error. ipctool mapped mtdblock, so:

before
ipctool backup the partition is cut at the bad page, every later partition is shifted in the file, exit 0
ipctool upload --backup Could not reach openipc.ru (error 5), though the network is fine
ipctool (report) SIGBUS with no output when the bad page is in a partition of 16 MB or less (hashed through a mapping)

Change

  • open_mtd_read() opens /dev/mtdN (enumeration already requires that node, so mtdblock needs no fallback).
  • Backup partitions are streamed spans, as UBI volumes already were. A read that still fails stops the backup by name instead of corrupting it.
  • examine_part() reads instead of mapping: whole for partitions 0/1 (env / XM crypto search), piece by piece into the SHA1 for the rest.
  • save_file() fails on a short write.
  • A partition the report cannot read gets read_error instead of silently losing its sha1.

Verified on the qemu-bench digital twin

A T31 running the 3.10 jz_sfc_nand driver, with the reporter's GD5F1GQ5UE (C8 51) and uncorrectable pages injected so that the kernel logs exactly the issue's mtdblock3, sector 1496 / logical block 187 and mtdblock5, sector 3320 / logical block 415. The model change is OpenIPC/qemu-bench (spinand ecc-fail=).

case master this PR
backup, bad pages in mtd3 and mtd5 corrupt from mtd3 on, exit 0 all 7 partitions byte-identical to the flash
upload --backup to a bench receiver Could not reach … (error 5) 134 MB received, backup byte-identical
report, bad page in mtd1 SIGBUS, rc 138 full report, SHA1s match the host's
report, no faults — identical to master

Native unit tests pass and the mips32 build is clean; arm32/arm64 are left to CI. Not yet tried on a real camera with a bad NAND page.

A NAND page that ECC cannot correct makes mtd_read() return -EBADMSG,
with the data still read. /dev/mtdN's read() hands that data over;
/dev/mtdblockN turns it into "Buffer I/O error". ipctool mapped
mtdblock, so on a camera with such a page:

- backup: fwrite() from the mapping stopped short at the page. The
  partition was cut, every later one shifted in the file, and the exit
  status was 0 (the 69 MB file of 128 MB in #283).
- upload --backup: send() from the mapping failed, reported as "Could
  not reach openipc.ru (error 5)" -- not a network problem.
- the report: examine_part() hashes every partition up to 16 MB through
  a mapping, and a bad page there is SIGBUS with no output at all.

open_mtd_read() opens /dev/mtdN, falling back to /dev/mtdblockN for a
/dev without the character nodes. Backup partitions are now streamed
spans, as UBI volumes already were, so a read error on the fallback
stops the backup by name instead of corrupting it. examine_part() reads
the first two partitions whole (the U-Boot environment and XM crypto
searches need them) and hashes the rest a piece at a time. save_file()
fails on a short write.

Verified on the qemu-bench T31 twin with the reporter's GD5F1GQ5UE and
injected uncorrectable pages at the sectors the issue's log names
(mtdblock3 sector 1496, mtdblock5 sector 3320): master's backup is
corrupt from mtd3 on and its upload fails with "error 5"; with this
change the backup and the upload are byte-identical to the flash, and
the report, with a bad page in mtd1, gives the right SHA1s where master
dies with SIGBUS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Read MTD partitions through character devices for backup and reporting

🐞 Bug fix 🕐 20-40 Minutes

Grey Divider

AI Description

• Prefer /dev/mtdN reads to preserve NAND data that mtdblock treats as an I/O error.
• Stream backup partitions and report hashes instead of mapping them, avoiding truncated backups and
 report crashes.
• Fail file backups on short reads or writes rather than silently producing incomplete output.
Diagram

graph TD
  A["MTD partitions"] --> B["Read selector"] --> C["Character device"] --> E["Streamed spans"] --> F["Backup or upload"]
  B --> D["Block fallback"] --> E
  C --> G["Report hashing"]
  D --> G
Loading
High-Level Assessment

Prefer the PR’s approach: it reuses the existing path-backed span streaming used for UBI volumes and preserves the required whole-partition inspection for early boot partitions. Continuing to map mtdblock cannot address the ECC failure, while separate backup and upload read implementations would duplicate the fix.

Files changed (3) +96 / -23

Bug fix (3) +96 / -23
backup.cStream MTD backup partitions and detect incomplete file output +30/-16

Stream MTD backup partitions and detect incomplete file output

• Partition backup spans now record the selected device path instead of a mapped mtdblock address, allowing backup and upload to stream their contents. File backups report premature source termination and fail on short payload writes or a failed final close.

src/backup.c

mtd.cPrefer MTD character reads and hash report partitions without mappings +63/-7

Prefer MTD character reads and hash report partitions without mappings

• Adds a device opener that prefers /dev/mtdN and falls back to /dev/mtdblockN, plus a helper that reads an exact byte count. Reporting reads partitions 0 and 1 whole for environment and crypto inspection, while hashing later eligible partitions in chunks to avoid mmap-related SIGBUS.

src/mtd.c

mtd.hExpose the shared MTD read-device selector +3/-0

Expose the shared MTD read-device selector

• Declares open_mtd_read and documents its preferred device, fallback path, and failure behavior for backup and reporting callers.

src/mtd.h

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Flash read failures vanish from reports ✓ Resolved

Resolving commit: 7ea3801

Resolution reason
examine_part() now takes a why buffer, filled with the failure reason on open, malloc, and read
failures; cb_mtd_info() adds a "read_error" parameter to the partition's report entry when
examination failed, while intentional skips (oversized partitions) leave it empty and get no marker.
Description
examine_part() returns false when read_full() encounters an error or early EOF, but its caller
omits the hash without recording why. When a partition read fails, the generated report still lists
that partition with no indication that its contents could not be examined.

Grey Divider

Tip of the day
💡 Did you know, you can ask Qodo to dismiss a finding you disagree with, with your reason on record

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

Qodo's review: when examine_part() could not read a partition, the
report left out its sha1 with no word, the same as for a partition left
out on purpose (over 16 MB, or mounted rw). It now adds read_error: the
device, the error and the 64K piece it came from. Both reads, whole and
streamed, go through one loop, so the offset is right for either.

The /dev/mtdblockN fallback in open_mtd_read() could never run:
enum_mtd_info() lists only partitions whose /dev/mtdN opens and answers
MEMGETINFO, so a partition without the node never reaches the backup
or the report. Seen on the twin with /dev/mtd1 removed.

Re-verified on the qemu-bench T31 twin with uncorrectable pages in mtd1,
mtd3 and mtd5: the report has every sha1, and the backup is
byte-identical to the flash.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@openipc-ai

openipc-ai commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Addressed Qodo's finding in 7ea3801: a partition that can't be read now gets read_error in the report (device, error, and the 64K piece it failed in), so it looks different from one skipped on purpose (over 16 MB, or mounted rw).

Also removed the /dev/mtdblockN fallback I'd added. It could never run: enum_mtd_info() only lists partitions whose /dev/mtdN opens and answers MEMGETINFO. I confirmed that on the twin by removing /dev/mtd1.

Re-verified on the twin with uncorrectable pages in mtd1, mtd3 and mtd5: every sha1 is in the report and the backup is byte-identical to the flash.

openipc-ai added a commit to OpenIPC/qemu-bench that referenced this pull request Nov 14, 2026
…ges (#312)

* ingenic-spinand: gd5f1gq5ue, and ecc-fail= to report uncorrectable pages

On-die ECC never reported an error, so nothing could test what a host
does with a bad NAND page. ecc-fail="p[-q],..." lists absolute pages
whose page read sets the status ECC bits 5:4 to 10b (uncorrectable)
while ECC is enabled; the data still reaches the cache, as on a part.
The 3.10 jz_sfc_nand returns -EBADMSG from mtd_read for it, which
mtdblock turns into "Buffer I/O error" and /dev/mtdN's read() ignores.

gd5f1gq5ue (C8 51, 2048 + 128, 1024 blocks) is the part in
OpenIPC/ipctool#283. It is in the 3.10 kernel's table, not the SPL's or
U-Boot's, so it boots with -kernel, and only by flash-type=: its
136 MiB image is gd5f1gq4ub's size.

With -global gd5f1gq5ue.ecc-fail=4470,16190 the twin logs the issue's
exact "mtdblock3, sector 1496 / logical block 187" and "mtdblock5,
sector 3320 / logical block 415", and reproduced its corrupt backup and
"Could not reach (error 5)" upload, fixed in OpenIPC/ipctool#284.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ingenic-spinand: ecc-fail= pages are decimal or 0x, never octal or empty

Qodo's review: strtoul(..., 0) read ecc-fail=010 as octal page 8, and
an empty range end ("0-") passed as page 0. A page is now decimal, or
hex after 0x, and must start with a digit, so an empty end, a sign or a
space is refused rather than taken for page 0. Checked: 010, 08, 0x10,
0-5, 65535 accepted; 0-, -5, " 5", 0x, 65536, 5-3 refused; on the T31
twin ecc-fail=0612 fails page 612.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@openipc-ai
openipc-ai merged commit a15ee91 into master Nov 14, 2026
5 checks passed
@openipc-ai
openipc-ai deleted the backup-mtdchar-283 branch November 14, 2026 19:43
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.

Upload report with flash backup if I/O errors present

1 participant