mirror of
https://github.com/morluto/rea.git
synced 2026-12-06 14:59:05 +00:00
refactor(uimage): construct format failures in one place (#16976)
This commit is contained in:
1 parent
cf1d2c0f93
commit
003b9e29f2
1 file changed
+15
-31
+15
-31
@@ -191,10 +191,8 @@ const named = (
|
||||
export type UImageParseError =
|
||||
| { readonly _tag: "UImageFormatFailure"; readonly message: string }
|
||||
| { readonly _tag: "UImageCapacityFailure"; readonly message: string };
|
||||
const malformed = (message: string): UImageParseError => ({
|
||||
_tag: "UImageFormatFailure",
|
||||
message,
|
||||
});
|
||||
const malformed = (message: string) =>
|
||||
err<UImageParseError>({ _tag: "UImageFormatFailure", message });
|
||||
const UTF8 = strictUtf8Decoder("preserve");
|
||||
|
||||
/** True when the first four bytes are the U-Boot legacy magic. */
|
||||
@@ -213,10 +211,8 @@ export const parseUImageHeaderFields = (
|
||||
header: Buffer,
|
||||
): Result<UImageHeaderFields, UImageParseError> => {
|
||||
if (header.length < UIMAGE_HEADER_SIZE)
|
||||
return err(
|
||||
malformed(
|
||||
`uImage header is truncated: ${header.length} bytes, expected ${UIMAGE_HEADER_SIZE}`,
|
||||
),
|
||||
return malformed(
|
||||
`uImage header is truncated: ${header.length} bytes, expected ${UIMAGE_HEADER_SIZE}`,
|
||||
);
|
||||
const reader = new BinaryReader(
|
||||
header.subarray(0, UIMAGE_HEADER_SIZE),
|
||||
@@ -225,20 +221,16 @@ export const parseUImageHeaderFields = (
|
||||
);
|
||||
const magic = reader.u32(0);
|
||||
if (magic !== UIMAGE_MAGIC)
|
||||
return err(
|
||||
malformed(
|
||||
`uImage magic mismatch: expected 0x${UIMAGE_MAGIC.toString(16)}, found 0x${magic.toString(16)}`,
|
||||
),
|
||||
return malformed(
|
||||
`uImage magic mismatch: expected 0x${UIMAGE_MAGIC.toString(16)}, found 0x${magic.toString(16)}`,
|
||||
);
|
||||
const declaredHeaderCrc = reader.u32(4);
|
||||
const zeroed = Buffer.from(reader.bytes);
|
||||
zeroed.writeUInt32BE(0, 4);
|
||||
const computedHeaderCrc = crc32(zeroed);
|
||||
if (computedHeaderCrc !== declaredHeaderCrc)
|
||||
return err(
|
||||
malformed(
|
||||
`uImage header CRC32 mismatch: declared 0x${declaredHeaderCrc.toString(16)}, computed 0x${computedHeaderCrc.toString(16)}`,
|
||||
),
|
||||
return malformed(
|
||||
`uImage header CRC32 mismatch: declared 0x${declaredHeaderCrc.toString(16)}, computed 0x${computedHeaderCrc.toString(16)}`,
|
||||
);
|
||||
const timestamp = reader.u32(8);
|
||||
const nameBytes = reader.range(32, UIMAGE_NAME_LENGTH, "uImage image name");
|
||||
@@ -292,9 +284,7 @@ export const parseMultiImageTable = (
|
||||
payloadSize: number,
|
||||
): Result<readonly UImageSubImage[], UImageParseError> => {
|
||||
if (!Number.isSafeInteger(payloadSize) || payloadSize < 0)
|
||||
return err(
|
||||
malformed("uImage payload size is not an exact nonnegative extent"),
|
||||
);
|
||||
return malformed("uImage payload size is not an exact nonnegative extent");
|
||||
const table = new BinaryReader(
|
||||
prefix.subarray(0, Math.min(prefix.length, payloadSize)),
|
||||
false,
|
||||
@@ -304,10 +294,8 @@ export const parseMultiImageTable = (
|
||||
let cursor = 0;
|
||||
for (;;) {
|
||||
if (cursor + UIMAGE_SIZE_ENTRY_BYTES > table.bytes.length)
|
||||
return err(
|
||||
malformed(
|
||||
"uImage multi-image size table has no zero terminator inside the payload",
|
||||
),
|
||||
return malformed(
|
||||
"uImage multi-image size table has no zero terminator inside the payload",
|
||||
);
|
||||
const size = table.u32(cursor);
|
||||
cursor += UIMAGE_SIZE_ENTRY_BYTES;
|
||||
@@ -323,10 +311,8 @@ export const parseMultiImageTable = (
|
||||
0,
|
||||
);
|
||||
if (cursor + UIMAGE_SIZE_ENTRY_BYTES + minimumData > payloadSize)
|
||||
return err(
|
||||
malformed(
|
||||
"uImage multi-image table and declared sub-images cannot fit in the payload",
|
||||
),
|
||||
return malformed(
|
||||
"uImage multi-image table and declared sub-images cannot fit in the payload",
|
||||
);
|
||||
return err({
|
||||
_tag: "UImageCapacityFailure",
|
||||
@@ -338,10 +324,8 @@ export const parseMultiImageTable = (
|
||||
let offset = UIMAGE_HEADER_SIZE + cursor;
|
||||
for (const [index, size] of sizes.entries()) {
|
||||
if (offset + size > UIMAGE_HEADER_SIZE + payloadSize)
|
||||
return err(
|
||||
malformed(
|
||||
`uImage multi-image sub-image ${index} of ${size} bytes overruns the ${payloadSize}-byte payload`,
|
||||
),
|
||||
return malformed(
|
||||
`uImage multi-image sub-image ${index} of ${size} bytes overruns the ${payloadSize}-byte payload`,
|
||||
);
|
||||
subImages.push({ index, byteOffset: offset, declaredSize: size });
|
||||
offset += index + 1 < sizes.length ? Math.ceil(size / 4) * 4 : size;
|
||||
|
||||
Reference in new issue
Block a user