diff --git a/src/artifacts/AndroidBootImageFormat.ts b/src/artifacts/AndroidBootImageFormat.ts index a118818e9bd..4b245f7c151 100644 --- a/src/artifacts/AndroidBootImageFormat.ts +++ b/src/artifacts/AndroidBootImageFormat.ts @@ -415,13 +415,21 @@ const component = ( const place = ( components: AndroidBootComponentPlan[], - cursor: number, + start: number, page: number, - planned: AndroidBootComponentPlan | null, + ...planned: readonly [ + role: AndroidBootComponentPlan["role"], + size: number | null, + loadAddress: number | null, + ][] ): number => { - if (planned === null || planned.size === 0) return cursor; - components.push(planned); - return alignUp(endOf(cursor, planned.size), page); + let cursor = start; + for (const [role, size, loadAddress] of planned) { + if (size === null || size === 0) continue; + components.push(component(role, role, cursor, size, loadAddress)); + cursor = alignUp(endOf(cursor, size), page); + } + return cursor; }; const bootParse = ( @@ -572,12 +580,7 @@ const layoutLegacyTail = ( const dtbSize = u32(prefix, LEGACY_V1_SIZE); const dtbAddr = u64(prefix, LEGACY_V1_SIZE + 4, "dtb address"); return { - cursor: place( - components, - next, - page, - dtbSize === 0 ? null : component("dtb", "dtb", next, dtbSize, dtbAddr), - ), + cursor: place(components, next, page, ["dtb", dtbSize, dtbAddr]), unexposedLayoutBytes: unexposed, recoveryDtboSize, recoveryDtboOffset, @@ -622,29 +625,13 @@ const parseLegacyBoot = ( const ramdiskAddr = u32(prefix, 20); const secondSize = u32(prefix, 24); const secondAddr = u32(prefix, 28); - let cursor = place( + const cursor = place( components, pageSize, pageSize, - kernelSize === 0 - ? null - : component("kernel", "kernel", pageSize, kernelSize, kernelAddr), - ); - cursor = place( - components, - cursor, - pageSize, - ramdiskSize === 0 - ? null - : component("ramdisk", "ramdisk", cursor, ramdiskSize, ramdiskAddr), - ); - cursor = place( - components, - cursor, - pageSize, - secondSize === 0 - ? null - : component("second", "second", cursor, secondSize, secondAddr), + ["kernel", kernelSize, kernelAddr], + ["ramdisk", ramdiskSize, ramdiskAddr], + ["second", secondSize, secondAddr], ); // The tail's facts are named as the parse fields they become. const { cursor: imageEnd, ...tail } = layoutLegacyTail( @@ -714,29 +701,13 @@ const parseModernBoot = ( const signatureSize = headerVersion === 4 ? u32(prefix, MODERN_V3_SIZE) : null; const components: AndroidBootComponentPlan[] = []; - let cursor = place( + const cursor = place( components, BOOT_V3_PAGE_SIZE, BOOT_V3_PAGE_SIZE, - kernelSize === 0 - ? null - : component("kernel", "kernel", BOOT_V3_PAGE_SIZE, kernelSize, null), - ); - cursor = place( - components, - cursor, - BOOT_V3_PAGE_SIZE, - ramdiskSize === 0 - ? null - : component("ramdisk", "ramdisk", cursor, ramdiskSize, null), - ); - cursor = place( - components, - cursor, - BOOT_V3_PAGE_SIZE, - signatureSize === null || signatureSize === 0 - ? null - : component("signature", "signature", cursor, signatureSize, null), + ["kernel", kernelSize, null], + ["ramdisk", ramdiskSize, null], + ["signature", signatureSize, null], ); const dataEnd = requireFileCovers(fileSize, headerSize, components); return bootParse({ @@ -864,12 +835,7 @@ const parseVendorHeader = ( ramdiskAddr, ), ); - cursor = place( - components, - cursor, - pageSize, - dtbSize === 0 ? null : component("dtb", "dtb", cursor, dtbSize, dtbAddr), - ); + cursor = place(components, cursor, pageSize, ["dtb", dtbSize, dtbAddr]); const tableRange = table !== null && table.tableSize > 0 ? { offset: cursor, size: table.tableSize } @@ -877,14 +843,11 @@ const parseVendorHeader = ( if (tableRange !== null) cursor = alignUp(endOf(tableRange.offset, tableRange.size), pageSize); const bootconfigSize = table?.bootconfigSize ?? null; - cursor = place( - components, - cursor, - pageSize, - bootconfigSize === null || bootconfigSize === 0 - ? null - : component("bootconfig", "bootconfig", cursor, bootconfigSize, null), - ); + cursor = place(components, cursor, pageSize, [ + "bootconfig", + bootconfigSize, + null, + ]); const sectionUnexposed = table !== null && table.entryCount === 0 && vendorRamdiskSize > 0; if (sectionUnexposed)