refactor(uf2): share block magic checks (#16964)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-05 14:15:32 +08:00
1 parent e7237a1b2b
commit 56d044e9a0
1 file changed
+23 -29
+23 -29
View File
@@ -1,3 +1,4 @@
import { readUint32At } from "../binaryContainer.js";
import { boundedList, describeBoundedList } from "../boundedList.js";
import type { Uf2Metadata } from "../formatMetadata/firmwareMetadata.js";
import { err, ok, type Result } from "../result.js";
@@ -87,12 +88,13 @@ interface NumberingFacts {
readonly ambiguous: readonly string[];
}
const readUint32 = (bytes: Uint8Array, offset: number): number =>
((bytes[offset] ?? 0) |
((bytes[offset + 1] ?? 0) << 8) |
((bytes[offset + 2] ?? 0) << 16) |
((bytes[offset + 3] ?? 0) << 24)) >>>
0;
const magicFault = (block: Uint8Array): string | undefined =>
readUint32At(block, 0, true) !== UF2_MAGIC_START0 ||
readUint32At(block, 4, true) !== UF2_MAGIC_START1
? "start magic numbers do not match"
: readUint32At(block, UF2_BLOCK_BYTES - 4, true) !== UF2_MAGIC_END
? "end magic number does not match"
: undefined;
const hex32 = (value: number): string =>
`0x${value.toString(16).padStart(8, "0")}`;
@@ -111,10 +113,7 @@ const named = (items: readonly string[]): string =>
/** Recognize a file only by a complete first block with all three magic numbers. */
export const hasUf2Block = (prefix: Uint8Array): boolean =>
prefix.length >= UF2_BLOCK_BYTES &&
readUint32(prefix, 0) === UF2_MAGIC_START0 &&
readUint32(prefix, 4) === UF2_MAGIC_START1 &&
readUint32(prefix, UF2_BLOCK_BYTES - 4) === UF2_MAGIC_END;
prefix.length >= UF2_BLOCK_BYTES && magicFault(prefix) === undefined;
/** Start a block decoder whose arena is bounded by the admitted file size. */
export const createUf2Decoder = (
@@ -230,40 +229,35 @@ class StreamingUf2Decoder implements RegionDecoder<Uf2Image> {
#decodeBlock(block: Uint8Array): Result<void, RegionDecodeFailure> {
const index = this.#blockCount;
this.#blockCount += 1;
const flags = readUint32(block, 8);
const flags = readUint32At(block, 8, true);
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 = nonflash ? null : readUint32(block, 12);
const size = readUint32(block, 16);
const address = nonflash ? null : readUint32At(block, 12, true);
const size = readUint32At(block, 16, true);
const fault =
readUint32(block, 0) !== UF2_MAGIC_START0 ||
readUint32(block, 4) !== UF2_MAGIC_START1
? "start magic numbers do not match"
: readUint32(block, UF2_BLOCK_BYTES - 4) !== UF2_MAGIC_END
? "end magic number does not match"
: size > UF2_DATA_BYTES
? `payload size ${String(size)} exceeds the ${String(UF2_DATA_BYTES)}-byte data area`
: (flags & UF2_FLAG_MD5) !== 0 &&
size > UF2_DATA_BYTES - UF2_MD5_BYTES
? "payload overlaps the reserved MD5 checksum metadata"
: address !== null && address + size > ADDRESS_SPACE
? `payload at ${hex32(address)} with ${String(size)} bytes wraps past 0xffffffff`
: undefined;
magicFault(block) ??
(size > UF2_DATA_BYTES
? `payload size ${String(size)} exceeds the ${String(UF2_DATA_BYTES)}-byte data area`
: (flags & UF2_FLAG_MD5) !== 0 && size > UF2_DATA_BYTES - UF2_MD5_BYTES
? "payload overlaps the reserved MD5 checksum metadata"
: address !== null && address + size > ADDRESS_SPACE
? `payload at ${hex32(address)} with ${String(size)} bytes wraps past 0xffffffff`
: undefined);
if (fault !== undefined) {
this.#invalid += 1;
this.#firstInvalid ??= { block: index, reason: fault };
return ok(undefined);
}
const familyId =
(flags & UF2_FLAG_FAMILY_ID) !== 0 ? readUint32(block, 28) : null;
(flags & UF2_FLAG_FAMILY_ID) !== 0 ? readUint32At(block, 28, true) : null;
if ((flags & UF2_FLAG_MD5) !== 0) this.#md5 += 1;
if ((flags & UF2_FLAG_EXTENSION_TAGS) !== 0) this.#extensionTags += 1;
this.#number(
familyId,
readUint32(block, 24),
readUint32(block, 20),
readUint32At(block, 24, true),
readUint32At(block, 20, true),
address,
);
// The file-container flag overrides not-main-flash: such blocks hold files.