fix(uf2): count comment blocks without a flash address range (#12350)

Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
This commit is contained in:
ShuaixinHuangandShuaixinHuang authored and GitHub committed 2026-11-19 08:28:11 -05:00
1 parent 48a5090bbb
commit 55719cafb6
2 files changed
+47 -3

No files matched your search

+40
View File
@@ -382,6 +382,46 @@ it("excludes not-main-flash and file-container blocks from regions", () => {
);
});
it("counts a nonflash comment whose address cannot describe a flash range", () => {
const flash = uf2Block({
address: 0x1000,
payload: [1, 2],
blockNo: 0,
numBlocks: 2,
});
const decode = (flags: number) =>
image(
uf2File([
flash,
uf2Block({
address: 0xffffffff,
payload: [3, 4],
blockNo: 1,
numBlocks: 2,
flags,
}),
]),
);
const comment = decode(UF2_FLAGS.notMainFlash);
expect(comment.complete).toBe(true);
expect(comment.metadata).toMatchObject({
invalid_block_count: 0,
missing_block_count: 0,
not_main_flash_block_count: 1,
flash_block_count: 1,
region_count: 1,
});
expect(comment.regions.map(({ bytes }) => [...bytes])).toEqual([[1, 2]]);
for (const flags of [0, UF2_FLAGS.notMainFlash | UF2_FLAGS.fileContainer]) {
expect(decode(flags).metadata).toMatchObject({
invalid_block_count: 1,
missing_block_count: 1,
not_main_flash_block_count: 0,
});
}
});
it("excludes invalid blocks and a truncated final block, leaving coverage unknown", () => {
const blocks = uf2Image(RP2040_FAMILY, 0x1000_0000, patternBytes(768));
const badEnd = uf2Block({
+7 -3
View File
@@ -229,6 +229,10 @@ class StreamingUf2Decoder implements RegionDecoder<Uf2Image> {
const index = this.#blockCount;
this.#blockCount += 1;
const flags = readUint32(block, 8);
const fileContainer = (flags & UF2_FLAG_FILE_CONTAINER) !== 0;
// File-container blocks use the address field as a file offset. Pure
// nonflash blocks do not establish an address range.
const nonflash = !fileContainer && (flags & UF2_FLAG_NOT_MAIN_FLASH) !== 0;
const address = readUint32(block, 12);
const size = readUint32(block, 16);
const fault =
@@ -242,7 +246,7 @@ class StreamingUf2Decoder implements RegionDecoder<Uf2Image> {
: (flags & UF2_FLAG_MD5) !== 0 &&
size > UF2_DATA_BYTES - UF2_MD5_BYTES
? "payload overlaps the reserved MD5 checksum metadata"
: address + size > ADDRESS_SPACE
: !nonflash && address + size > ADDRESS_SPACE
? `payload at ${hex32(address)} with ${String(size)} bytes wraps past 0xffffffff`
: undefined;
if (fault !== undefined) {
@@ -261,11 +265,11 @@ class StreamingUf2Decoder implements RegionDecoder<Uf2Image> {
address,
);
// The file-container flag overrides not-main-flash: such blocks hold files.
if ((flags & UF2_FLAG_FILE_CONTAINER) !== 0) {
if (fileContainer) {
this.#fileContainer += 1;
return ok(undefined);
}
if ((flags & UF2_FLAG_NOT_MAIN_FLASH) !== 0) {
if (nonflash) {
this.#notMainFlash += 1;
this.#notMainFlashBytes += size;
return ok(undefined);