mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 05:32:44 +00:00
fix(uefi): identify AMD guided zlib sections correctly (#12608)
* fix(uefi): identify AMD guided zlib sections correctly * refactor(uefi): align the AMD zlib decoder with section evidence --------- Co-authored-by: ShuaixinHuang <ShuaixinHuang@users.noreply.github.com> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
6 files changed
+20
-24
No files matched your search
@@ -108,7 +108,7 @@ export const uefiCompressionSchema = z.union([
|
||||
kind: z.enum([
|
||||
"lzma-custom",
|
||||
"lzmaf86",
|
||||
"ami-zlib",
|
||||
"amd-zlib",
|
||||
"tiano",
|
||||
"efi-standard",
|
||||
"unknown-compression",
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
import { imageRegion } from "./UefiBytes.js";
|
||||
import {
|
||||
AMD_ZLIB_HEADER_LENGTH,
|
||||
peelAmiZlibPayload,
|
||||
peelAmdZlibPayload,
|
||||
walkSections,
|
||||
} from "./FfsSections.js";
|
||||
|
||||
@@ -47,7 +47,7 @@ it("classifies a guided LZMA section through its definition GUID", () => {
|
||||
expect(draft?.payload).toEqual({ start: 24, end: 24 + compressed.length });
|
||||
});
|
||||
|
||||
it("classifies the AMI-zlib guided section and peels its AMD header", () => {
|
||||
it("classifies the AMD-zlib guided section and peels its AMD header", () => {
|
||||
const stream = Buffer.from([0x78, 0xda, 0x01, 0x02, 0x03]);
|
||||
const amdHeader = Buffer.alloc(AMD_ZLIB_HEADER_LENGTH, 0);
|
||||
amdHeader.writeUInt32LE(stream.length, 0x14);
|
||||
@@ -60,7 +60,7 @@ it("classifies the AMI-zlib guided section and peels its AMD header", () => {
|
||||
expect(walked.sections).toHaveLength(1);
|
||||
const draft = walked.sections[0];
|
||||
expect(draft?.compression).toMatchObject({
|
||||
kind: "ami-zlib",
|
||||
kind: "amd-zlib",
|
||||
guid: "ce3233f5-2cd6-4d87-9152-4a238bb6d1c4",
|
||||
dataOffset: 24,
|
||||
attributes: 0,
|
||||
@@ -69,9 +69,9 @@ it("classifies the AMI-zlib guided section and peels its AMD header", () => {
|
||||
start: 24,
|
||||
end: 24 + AMD_ZLIB_HEADER_LENGTH + stream.length,
|
||||
});
|
||||
const peeled = peelAmiZlibPayload(Buffer.concat([amdHeader, stream]));
|
||||
const peeled = peelAmdZlibPayload(Buffer.concat([amdHeader, stream]));
|
||||
expect(peeled).toEqual({ stream, declaredCompressedSize: stream.length });
|
||||
expect(peelAmiZlibPayload(Buffer.alloc(0x40, 0))).toBeNull();
|
||||
expect(peelAmdZlibPayload(Buffer.alloc(0x40, 0))).toBeNull();
|
||||
});
|
||||
|
||||
it("marks unknown definition GUIDs instead of guessing", () => {
|
||||
|
||||
+8
-12
@@ -1,4 +1,5 @@
|
||||
import { BinaryReader } from "../domain/binaryContainer.js";
|
||||
import type { UefiSectionRecord } from "../domain/uefi/uefiFirmwareAnalysis.js";
|
||||
import { Buffer } from "node:buffer";
|
||||
import {
|
||||
absoluteOffset,
|
||||
@@ -48,15 +49,10 @@ export interface SectionDraft {
|
||||
readonly compression:
|
||||
| { readonly kind: "none" }
|
||||
| {
|
||||
readonly kind:
|
||||
| "lzma-custom"
|
||||
| "lzmaf86"
|
||||
| "ami-zlib"
|
||||
| "tiano"
|
||||
| "unknown-compression"
|
||||
| "efi-standard"
|
||||
| "crc32-guided"
|
||||
| "unknown-guid";
|
||||
readonly kind: Exclude<
|
||||
UefiSectionRecord["compression"]["kind"],
|
||||
"none"
|
||||
>;
|
||||
readonly guid: string | null;
|
||||
readonly dataOffset: number;
|
||||
readonly attributes: number | null;
|
||||
@@ -125,7 +121,7 @@ function guidedSectionContent(
|
||||
|
||||
if (kind === "lzma-custom") return { kind: "decode", format: "lzma" };
|
||||
if (kind === "lzmaf86") return { kind: "decode", format: "lzmaf86" };
|
||||
if (kind === "ami-zlib") return { kind: "decode", format: "zlib" };
|
||||
if (kind === "amd-zlib") return { kind: "decode", format: "zlib" };
|
||||
|
||||
if (
|
||||
(attributes & GUIDED_PROCESSING_REQUIRED) === 0 &&
|
||||
@@ -304,8 +300,8 @@ export function walkSections(
|
||||
*/
|
||||
export const AMD_ZLIB_HEADER_LENGTH = 0x100;
|
||||
|
||||
/** Peel the 0x100-byte AMD header off one ami-zlib payload, or null when short. */
|
||||
export function peelAmiZlibPayload(
|
||||
/** Peel the 0x100-byte AMD header off one amd-zlib payload, or null when short. */
|
||||
export function peelAmdZlibPayload(
|
||||
payload: Buffer,
|
||||
): { stream: Buffer; declaredCompressedSize: number } | null {
|
||||
if (payload.length < AMD_ZLIB_HEADER_LENGTH) {
|
||||
|
||||
@@ -57,12 +57,12 @@ export const SECTION_TYPE_NAME_BY_CODE: Readonly<Record<number, string>> =
|
||||
export const COMPRESSION_GUID_BY_DISPLAY: Readonly<
|
||||
Record<
|
||||
string,
|
||||
"lzma-custom" | "lzmaf86" | "ami-zlib" | "tiano" | "crc32-guided"
|
||||
"lzma-custom" | "lzmaf86" | "amd-zlib" | "tiano" | "crc32-guided"
|
||||
>
|
||||
> = {
|
||||
"ee4e5898-3914-4259-9d6e-dc7bd79403cf": "lzma-custom",
|
||||
"d42ae6bd-1352-4bfb-909a-ca72a6eae889": "lzmaf86",
|
||||
"ce3233f5-2cd6-4d87-9152-4a238bb6d1c4": "ami-zlib",
|
||||
"ce3233f5-2cd6-4d87-9152-4a238bb6d1c4": "amd-zlib",
|
||||
"a31280ad-481e-41b6-95e8-127f4c984779": "tiano",
|
||||
"fc1bcdb0-7d31-49aa-936a-a4600d9dd083": "crc32-guided",
|
||||
};
|
||||
|
||||
@@ -332,7 +332,7 @@ it("preserves a decoded sibling after an on-disk retrieval wildcard", async () =
|
||||
);
|
||||
});
|
||||
|
||||
it("decodes AMI-zlib guided sections after peeling the AMD header", async () => {
|
||||
it("decodes AMD-zlib guided sections after peeling the AMD header", async () => {
|
||||
const nestedInner = buildFfsFile({
|
||||
guid: "2b111111-2222-3333-4444-555555555555",
|
||||
typeCode: 0x07,
|
||||
@@ -364,7 +364,7 @@ it("decodes AMI-zlib guided sections after peeling the AMD header", async () =>
|
||||
expect(nested?.logical_path).toContain("/zlib/FV@");
|
||||
const zlibSection = summary.volumes[0]?.ffs_files[0]?.sections[0];
|
||||
expect(zlibSection?.compression).toMatchObject({
|
||||
kind: "ami-zlib",
|
||||
kind: "amd-zlib",
|
||||
data_offset: 24,
|
||||
});
|
||||
expect(zlibSection?.sub_volumes).toEqual([summary.volumes[1]?.logical_path]);
|
||||
|
||||
@@ -30,7 +30,7 @@ import {
|
||||
} from "./FfsFile.js";
|
||||
import {
|
||||
AMD_ZLIB_HEADER_LENGTH,
|
||||
peelAmiZlibPayload,
|
||||
peelAmdZlibPayload,
|
||||
startsWithSectionFrame,
|
||||
walkSections,
|
||||
type UefiDecodeFormat,
|
||||
@@ -982,7 +982,7 @@ export const scanUefiImage = async (
|
||||
);
|
||||
let carrier = payload;
|
||||
if (format === "zlib") {
|
||||
const peeled = peelAmiZlibPayload(payload.bytes);
|
||||
const peeled = peelAmdZlibPayload(payload.bytes);
|
||||
if (
|
||||
peeled === null ||
|
||||
peeled.declaredCompressedSize !== peeled.stream.length
|
||||
|
||||
Reference in new issue
Block a user