Skip to content

fix(flash): check install-if-different against the written bytes only - #472

Merged
otavio merged 1 commit into
masterfrom
fix/flash-install-if-different-region
Nov 18, 2026
Merged

otavio merged 1 commit into
masterfrom
fix/flash-install-if-different-region

Conversation

@otavio

@otavio otavio commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

What

With install-if-different set to content-diverges, an object installed by the flash handler is written on every update, even when the partition already holds it.

The failure

An i.MX8MM package installs flash.bin to the u-boot MTD partition with the flash handler. The check handed should_skip_install the whole partition, but flashcp and nandwrite -p write the object from the start of the partition and leave the rest erased. The checksum then covers the object plus the erased tail, so it never matches the object's sha256sum: the partition is erased and rewritten on every update.

The fix

Read exactly the size bytes the install writes, from the start of the partition. On NAND the read skips bad erase blocks (MEMGETBADBLOCK), as nandwrite does when writing, so it sees the data where it actually landed.

#449 left flash reading the whole partition on purpose, because a plain size bound would cut off a version banner pushed past it by a bad block. Skipping bad blocks removes that concern, so version-diverges is now bounded to the object as well. If the installed binary is larger than the new one and its banner lies past the new size, no version is found and the object is installed. That errs on the safe side, as for raw since #449.

imxkobs is unchanged, for the reasons given in #449.

Tests

  • read_written_stops_at_the_object_size, read_written_skips_bad_blocks and read_written_stops_at_the_end_of_the_device cover the bad-block-aware read without hardware.
  • install_if_different_checksum_ignores_the_erased_tail and install_if_different_checksum_writes_a_changed_object run the installer over mtdram and over nandsim. On NAND they use the partition nandsim registers itself, since it cannot be split further without CONFIG_MTD_PARTITIONED_MASTER. Like the other MTD tests they need root, so CI skips them.

Run as root on Linux 7.3.4, all 7 tests of installer::flash and utils::mtd pass. With the previous check restored, install_if_different_checksum_ignores_the_erased_tail fails (assertion failed: !checksum_install_writes(MtdKind::Nor, &object, &object)): the unchanged object is written again.

cargo test --lib (132 passed), cargo fmt --check and cargo clippy -- -D clippy::all are clean.

The flash handler handed `should_skip_install` the whole MTD partition.
`flashcp` and `nandwrite -p` write the object from the start of the
partition and leave the rest erased, so with `content-diverges` the
checksum covered the object plus the erased tail and never matched: the
object was written on every update, however unchanged. A bootloader
partition is erased and rewritten each time for nothing.

Read exactly the `size` bytes the install writes, from the start of the
partition. On NAND, skip the bad erase blocks as `nandwrite` does, so the
read sees the data where it was written. That also keeps the version
check of `version-diverges` inside the object, which #449 left reading
the whole partition because a plain `size` bound would cut off a banner
pushed past it by a bad block.

The bad-block-aware read is unit tested without hardware. The installer
tests over nandsim and mtdram need root, like the other MTD tests.
@otavio
otavio force-pushed the fix/flash-install-if-different-region branch from 1f8546b to b5865ed Compare November 18, 2026 12:40
@otavio
otavio merged commit c2ae53f into master Nov 18, 2026
4 checks passed
@otavio
otavio deleted the fix/flash-install-if-different-region branch November 18, 2026 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant