Files
reverse-engineering-anything/tests/fixtures/androidSuperImage.ts
a5aee4f9e9 feat(artifacts): inventory Android super image metadata (#14928)
* feat(artifacts): inventory Android super image metadata

Refs #14391

* fix(artifacts): preserve partial Android super metadata evidence

* fix(artifacts): recognize Android super PE overlays

Refs #14928

* fix(artifacts): reject an Android super device with no usable sector

A first usable sector at the device size was accepted, so extent checks
reported a different field. Keep stored copy failures to malformed,
truncated, and unsupported now that format failures include unrecognized.

* fix(artifacts): preserve Android super partial evidence on budget exhaustion

* fix(mcp): preserve existing provider partial observation contracts

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2026-11-30 06:15:43 -05:00

162 lines
7.9 KiB
TypeScript

import { createHash } from "node:crypto";
// Unmodified non-padding bytes from AOSP android-14.0.0_r75 lpmake.
// Common options for both images:
// --metadata-size 4096 --metadata-slots 2 --device-size 65536 --alignment 8192
// --group main:32768 --partition system_a:readonly:8192:main --partition vendor_a:none:0:main
// Raw (super.img): --force-full-image --output super.img
// SHA-256 433f6af04a9ab7e019cf8c9ae1cce202b6345e881552a90e6c913c5041c71f61
// Template (super_empty.img): --virtual-ab --output super_empty.img
// SHA-256 b59245568627b107b97340745eeb4f84d909e6dc26046ce21e2da5314891165f
// Store producer bytes as text and reinstate the zero padding at the producer's offsets.
const geometry = Buffer.from(
"67446c61340000003610cac545cdf7b5d686fa64477219de218c44aef29c45bbe98bcd464244fa220010000002000000" +
"00100000",
"hex",
);
const rawMetadata = Buffer.from(
"30504c410a0000008000000058b4d5bc587218ddfa0e441190d0bb580f7677ec6dbf6f1641b8533a7e27303020010000" +
"7336f623a4aa51b732622eb0389f741fdd0c8ae7e5f1946e5f63be7a9f2e357e00000000020000003400000068000000" +
"0100000018000000800000000200000030000000e0000000010000004000000073797374656d5f610000000000000000" +
"00000000000000000000000000000000000000000100000000000000010000000100000076656e646f725f6100000000" +
"000000000000000000000000000000000000000000000000000000000100000000000000010000001000000000000000" +
"0000000040000000000000000000000064656661756c7400000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000006d61696e00000000000000000000000000000000000000000000000000000000" +
"000000000000000000800000000000004000000000000000002000000000000000000100000000007375706572000000" +
"0000000000000000000000000000000000000000000000000000000000000000",
"hex",
);
const templateMetadata = Buffer.from(
"30504c410a000200000100003d16e96e114a0effc2faab85878ce7b6e1852c49d81aa111e84086464806d53020010000" +
"7336f623a4aa51b732622eb0389f741fdd0c8ae7e5f1946e5f63be7a9f2e357e00000000020000003400000068000000" +
"0100000018000000800000000200000030000000e0000000010000004000000001000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"0000000000000000000000000000000073797374656d5f61000000000000000000000000000000000000000000000000" +
"000000000100000000000000010000000100000076656e646f725f610000000000000000000000000000000000000000" +
"000000000000000000000000010000000000000001000000100000000000000000000000400000000000000000000000" +
"64656661756c740000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"6d61696e0000000000000000000000000000000000000000000000000000000000000000000000000080000000000000" +
"400000000000000000200000000000000000010000000000737570657200000000000000000000000000000000000000" +
"00000000000000000000000000000000",
"hex",
);
// Raw metadata-bearing device from lpmake with these options:
// --metadata-size 4096 --metadata-slots 2 --device super:40960:8192:0
// --device companion:65536:8192:0 --group main:65536
// --partition system_a:readonly:16384:main --force-full-image --output split
// split/super_super.img SHA-256 3038a6bae51fb9e609e0780d8adf3e051063b1585ed49d9d41778f3d61d0c7d2
const splitMetadata = Buffer.from(
"30504c410a000000800000008f3b6613e14addd0a290fc5346047837c1304344c43bdab4846a06f35bd4011d44010000" +
"8586bf66947e3b64df2b6d20e4d727f6738af885691e209c83d4f6575951ad4400000000010000003400000034000000" +
"0200000018000000640000000200000030000000c4000000020000004000000073797374656d5f610000000000000000" +
"000000000000000000000000000000000000000001000000000000000200000001000000100000000000000000000000" +
"40000000000000000000000010000000000000000000000010000000000000000100000064656661756c740000000000" +
"0000000000000000000000000000000000000000000000000000000000000000000000006d61696e0000000000000000" +
"000000000000000000000000000000000000000000000000000000000000010000000000400000000000000000200000" +
"0000000000a0000000000000737570657200000000000000000000000000000000000000000000000000000000000000" +
"00000000100000000000000000200000000000000000010000000000636f6d70616e696f6e0000000000000000000000" +
"0000000000000000000000000000000000000000",
"hex",
);
export const androidSuperFixture = (
layout: "raw" | "template" | "split",
): Buffer => {
if (layout === "template") {
const bytes = Buffer.alloc(4640);
geometry.copy(bytes);
templateMetadata.copy(bytes, 4096);
return bytes;
}
const bytes = Buffer.alloc(layout === "split" ? 40960 : 65536);
const metadata = layout === "split" ? splitMetadata : rawMetadata;
for (const offset of [4096, 8192]) geometry.copy(bytes, offset);
for (const offset of [12288, 16384, 20480, 24576])
metadata.copy(bytes, offset);
return bytes;
};
const writeSha256 = (bytes: Buffer, offset: number, input: Buffer) =>
createHash("sha256").update(input).digest().copy(bytes, offset);
// Negative cases can alter table facts without failing the checksum gate first.
export const rehashSuperGeometry = (bytes: Buffer, offset: number): void => {
bytes.fill(0, offset + 8, offset + 40);
writeSha256(bytes, offset + 8, bytes.subarray(offset, offset + 52));
};
export const rehashSuperMetadata = (bytes: Buffer, offset: number): void => {
const headerSize = bytes.readUInt32LE(offset + 8);
const tablesStart = offset + headerSize;
const tablesEnd = tablesStart + bytes.readUInt32LE(offset + 44);
writeSha256(bytes, offset + 48, bytes.subarray(tablesStart, tablesEnd));
bytes.fill(0, offset + 12, offset + 44);
writeSha256(bytes, offset + 12, bytes.subarray(offset, tablesStart));
};
/** Synthesized checksummed image whose admitted slot exceeds a default MCP frame. */
export const largeAndroidSuperFixture = (): Buffer => {
const partitionCounts = [14_000, 100] as const;
const metadataBytes =
Math.ceil((128 + 76 * partitionCounts[0] + 112) / 512) * 512;
const metadataEnd = 12288 + 4 * metadataBytes;
const bytes = Buffer.alloc(metadataEnd + 4096);
geometry.copy(bytes, 4096);
bytes.writeUInt32LE(metadataBytes, 4096 + 40);
rehashSuperGeometry(bytes, 4096);
bytes.copy(bytes, 8192, 4096, 4096 + geometry.length);
for (const [slot, count] of partitionCounts.entries()) {
const offset = 12288 + slot * metadataBytes;
const header = bytes.subarray(offset, offset + 128);
const tables = bytes.subarray(
offset + 128,
offset + 128 + 76 * count + 112,
);
rawMetadata.copy(header, 0, 0, 128);
header.writeUInt32LE(tables.length, 44);
let tableOffset = 0;
for (const [index, [records, stride]] of (
[
[count, 52],
[count, 24],
[1, 48],
[1, 64],
] as const
).entries()) {
const descriptor = 80 + index * 12;
header.writeUInt32LE(tableOffset, descriptor);
header.writeUInt32LE(records, descriptor + 4);
header.writeUInt32LE(stride, descriptor + 8);
tableOffset += records * stride;
}
for (let index = 0; index < count; index++) {
tables.write(
`partition_${String(index).padStart(6, "0")}_for_delivery`,
index * 52,
);
tables.writeUInt32LE(index, index * 52 + 40);
tables.writeUInt32LE(1, index * 52 + 44);
// A zero-filled logical block, without any carrier payload.
tables.writeBigUInt64LE(8n, 52 * count + index * 24);
tables.writeUInt32LE(1, 52 * count + index * 24 + 8);
}
tables.write("default", tableOffset - 112);
tables.writeBigUInt64LE(BigInt(metadataEnd / 512), tableOffset - 64);
tables.writeBigUInt64LE(BigInt(bytes.length), tableOffset - 48);
tables.write("super", tableOffset - 40);
rehashSuperMetadata(bytes, offset);
bytes.copy(
bytes,
offset + 2 * metadataBytes,
offset,
offset + 128 + tables.length,
);
}
return bytes;
};