diff --git a/src/domain/artifactFormatRecognition.test.ts b/src/domain/artifactFormatRecognition.test.ts index 03a8b1a9f1..50ca223366 100644 --- a/src/domain/artifactFormatRecognition.test.ts +++ b/src/domain/artifactFormatRecognition.test.ts @@ -10,6 +10,7 @@ import { fullAndroidOtaPayload, v1AndroidOtaPayload, } from "./firmware/androidOtaPayload.fixture.js"; +import { devicetreeFixture } from "./firmware/devicetree.fixture.js"; import { dfuseFile } from "./firmware/dfuse.fixture.js"; import { DFUSE_SUFFIX_BYTES } from "./firmware/dfuse.js"; import { ESP8266_REFUSAL } from "./firmware/espImage.js"; @@ -111,3 +112,34 @@ it("recognizes a CrAU v2 payload after the uImage magic check", () => { format: "android-ota-payload", }); }); + +it.each([16, 17])( + "recognizes FIT from an FDT v%s structure in a bounded prefix", + (version) => { + const bytes = devicetreeFixture( + { name: "", children: [{ name: "images" }] }, + version, + ); + const structureOffset = bytes.readUInt32BE(8); + const prefix = bytes.subarray(0, structureOffset + 20); + expect( + recognizeArtifactFormat({ + prefix, + fileSize: bytes.length, + espFlashLayout: false, + }), + ).toMatchObject({ state: "recognized", format: "fit" }); + }, +); + +it.each([8, 12, 19])( + "does not recognize FIT when the declared structure ends after %s bytes", + (structureSize) => { + const bytes = devicetreeFixture({ + name: "", + children: [{ name: "images" }], + }); + bytes.writeUInt32BE(structureSize, 36); + expect(recognize(bytes)).toBeUndefined(); + }, +); diff --git a/src/domain/firmware/fitImage.ts b/src/domain/firmware/fitImage.ts index 8ff2649268..fb8954ddbf 100644 --- a/src/domain/firmware/fitImage.ts +++ b/src/domain/firmware/fitImage.ts @@ -11,25 +11,33 @@ export const hasFitImagesNode = (prefix: Uint8Array): boolean => { const u32 = (offset: number): number => view.getUint32(offset); const version = u32(20); if (u32(0) !== 0xd00dfeed || (version !== 16 && version !== 17)) return false; - const end = Math.min(bytes.length, u32(4)); - let cursor = u32(8); + const start = u32(8); + const totalSize = u32(4); + // Version 17 declares a structure size. Bytes beyond it belong to other + // blocks or padding, even when they are available within the same prefix. + const structureEnd = version === 17 ? start + u32(36) : totalSize; + const end = Math.min(bytes.length, totalSize, structureEnd); + const structurePrefix = bytes.subarray(0, end); + let cursor = start; let depth = 0; while (cursor <= end - 4) { const token = u32(cursor); if (token === 1) { - const nul = bytes.indexOf(0, cursor + 4); - if (nul < 0 || nul >= end) return false; + const nul = structurePrefix.indexOf(0, cursor + 4); + if (nul < 0) return false; + const next = Math.ceil((nul + 1) / 4) * 4; + if (next > end) return false; const name = String.fromCharCode(...bytes.subarray(cursor + 4, nul)); if (depth === 0 && name !== "") return false; if (depth === 1 && name === "images") return true; depth += 1; - cursor = Math.ceil((nul + 1) / 4) * 4; + cursor = next; } else if (token === 2) { depth -= 1; if (depth <= 0) return false; cursor += 4; } else if (token === 3) { - if (cursor > end - 8) return false; + if (cursor > end - 12) return false; cursor += Math.ceil((12 + u32(cursor + 4)) / 4) * 4; } else if (token === 4) cursor += 4; else return false;