From 55719cafb698be63cd6f2cb3e0f97cce6facda66 Mon Sep 17 00:00:00 2001 From: ShuaixinHuang <1836656514@qq.com> Date: Thu, 19 Nov 2026 21:28:11 +0800 Subject: [PATCH] fix(uf2): count comment blocks without a flash address range (#12350) Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com> --- src/domain/firmware/uf2.test.ts | 40 +++++++++++++++++++++++++++++++++ src/domain/firmware/uf2.ts | 10 ++++++--- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/domain/firmware/uf2.test.ts b/src/domain/firmware/uf2.test.ts index 63b83be2e7..26c949e6b5 100644 --- a/src/domain/firmware/uf2.test.ts +++ b/src/domain/firmware/uf2.test.ts @@ -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({ diff --git a/src/domain/firmware/uf2.ts b/src/domain/firmware/uf2.ts index ed4ef6e996..430ccab174 100644 --- a/src/domain/firmware/uf2.ts +++ b/src/domain/firmware/uf2.ts @@ -229,6 +229,10 @@ class StreamingUf2Decoder implements RegionDecoder { 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 { : (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 { 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);