mirror of
https://github.com/morluto/rea.git
synced 2026-12-07 00:34:10 +08:00
* feat(uefi): read TianoCore EDK2 NVRAM variable stores Add read-only parsing of gEfiVariableGuid / gEfiAuthenticatedVariableGuid VARIABLE_STORE_HEADER regions to inspect_uefi_nvram via additive edk2_stores, preserving the AMI NVAR stores contract and reporting added vs superseded variable states. * fix(uefi): harden EDK2 NVRAM scan coverage and accounting Occupy store ranges and charge decoded names before retention, keep truncated and damaged-name facts, examine unread tails, and compose empty-format diagnostics from scanner facts with a provider-level test. * fix(uefi): preserve enclosing EDK2 stores and scan erased ranges in blocks Walk store GUID candidates in ascending offset order, claim only verified spans so damaged oversized headers cannot hide later stores, and checkpoint erased suffixes per 64 KiB block. * fix(uefi): keep unusable EDK2 store signatures from reading as absence A gEfiVariableGuid or gEfiAuthenticatedVariableGuid match whose header lacks the formatted marker was skipped silently, so an image holding only such a header reported that no EDK2 store exists. It is now reported as a rejected signature, and the absence statement is qualified. A formatted store that is not HEALTHY is walked with EDK2's verdict as a caveat: EDK2 treats it as invalid, while UEFITool still parses it. A store at an unaligned image offset states the HEADER_ALIGN assumption. An in-use EDK2 fault-tolerant-write queue is noted, because REA does not replay FTW writes or rebuild a store from the spare area. AMI NVAR and EDK2 scans now share one structural record budget, as the limit documents. GUID signature encoding moves to UefiGuid beside its decoder, header field offsets are named after VariableFormat.h, the limitation-line cap is a named limit, and the store walk is split from header admission with unreachable branches removed. * fix(uefi): distinguish unchecked EDK2 stores from absence Carry an explicit completed-search absence fact from the EDK2 scanner. Partial or budget-stopped searches report that presence is unknown instead of claiming no store exists. --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
165 lines
5.3 KiB
TypeScript
165 lines
5.3 KiB
TypeScript
import type { Logger } from "pino";
|
|
import { z } from "zod";
|
|
|
|
import { createUefiAnalysisService } from "../application/ProviderOperationServices.js";
|
|
import { createUefiAnalysisProvider } from "../composition/uefi.js";
|
|
import { UEFI_TOOL_CONTRACTS } from "../contracts/uefi/uefiToolContracts.js";
|
|
import { AnalysisInputError } from "../domain/analysisErrorCore.js";
|
|
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
|
import type { UefiOperation } from "../domain/uefi/uefiFirmwareAnalysis.js";
|
|
import { UEFI_PROVIDER_IDENTITY } from "../uefi/UefiProviderIdentity.js";
|
|
import { contractNamed } from "./contractCliDescription.js";
|
|
import { registerContractCommand } from "./contractCommand.js";
|
|
import { inspectWithService } from "./contractCommandRunners.js";
|
|
import { cliDecimalField } from "./options.js";
|
|
import type { CliInstance } from "./types.js";
|
|
|
|
const FLASH_PATH = { description: "Local UEFI firmware image" };
|
|
const OFFSET_OPTIONS = {
|
|
volumeOffset: "volume",
|
|
fileOffset: "ffs-file",
|
|
sectionOffset: "section",
|
|
} as const;
|
|
const targetOffset = cliDecimalField(z.number().int().nonnegative().optional());
|
|
|
|
/** One-shot UEFI firmware CLI commands backed by the same workflows as MCP. */
|
|
export const registerUefiCommands = (
|
|
cli: CliInstance,
|
|
logger: Logger,
|
|
): void => {
|
|
const service = createUefiAnalysisService(createUefiAnalysisProvider());
|
|
const run = (operation: UefiOperation) =>
|
|
inspectWithService(
|
|
() => ({
|
|
inspect: (input, options) => service.execute(operation, input, options),
|
|
close: () => service.close(),
|
|
}),
|
|
UEFI_PROVIDER_IDENTITY.id,
|
|
);
|
|
|
|
registerContractCommand(
|
|
cli,
|
|
logger,
|
|
contractNamed(UEFI_TOOL_CONTRACTS, "inspect_uefi_firmware"),
|
|
{
|
|
description:
|
|
"Map UEFI firmware volumes, FFS files and sections of a flash image",
|
|
positionals: ["path"],
|
|
callerPaths: ["path"],
|
|
fields: { path: FLASH_PATH },
|
|
omit: {
|
|
detail:
|
|
"The one-shot command returns complete Evidence; summaries are retained by MCP sessions.",
|
|
view: "Views page Evidence retained by MCP sessions.",
|
|
},
|
|
},
|
|
run("inspect_uefi_firmware"),
|
|
);
|
|
registerContractCommand(
|
|
cli,
|
|
logger,
|
|
contractNamed(UEFI_TOOL_CONTRACTS, "inspect_uefi_nvram"),
|
|
{
|
|
description:
|
|
"Map AMI NVAR and EDK2 / OVMF variable stores of a flash image",
|
|
positionals: ["path"],
|
|
callerPaths: ["path"],
|
|
fields: { path: FLASH_PATH },
|
|
},
|
|
run("inspect_uefi_nvram"),
|
|
);
|
|
|
|
const extraction = contractNamed(
|
|
UEFI_TOOL_CONTRACTS,
|
|
"extract_uefi_firmware",
|
|
);
|
|
const extract = run(extraction.name);
|
|
registerContractCommand(
|
|
cli,
|
|
logger,
|
|
extraction,
|
|
{
|
|
description:
|
|
"Extract selected UEFI firmware bytes with provenance and decode gaps",
|
|
positionals: ["path", "output_directory"],
|
|
callerPaths: ["path", "output_directory"],
|
|
omit: {
|
|
detail:
|
|
"The one-shot command returns complete Evidence; summaries are retained by MCP sessions.",
|
|
view: "Views page Evidence retained by MCP sessions.",
|
|
},
|
|
fields: {
|
|
path: FLASH_PATH,
|
|
output_directory: {
|
|
description: "Absent output directory; parent must exist",
|
|
},
|
|
},
|
|
handlerFields: ["target"],
|
|
cliOptions: {
|
|
volumeOffset: targetOffset.describe(
|
|
"Extract one firmware volume at this byte offset",
|
|
),
|
|
fileOffset: targetOffset.describe(
|
|
"Extract one FFS file at this byte offset",
|
|
),
|
|
sectionOffset: targetOffset.describe(
|
|
"Extract one section at this byte offset",
|
|
),
|
|
sourceStream: z
|
|
.string()
|
|
.optional()
|
|
.describe(
|
|
"Carrier logical path for a decoded offset, or flash for the original image. Requires one target offset.",
|
|
),
|
|
},
|
|
},
|
|
async (input, context) => {
|
|
const options = context.cliOptions;
|
|
const selectors = (
|
|
Object.keys(OFFSET_OPTIONS) as (keyof typeof OFFSET_OPTIONS)[]
|
|
).flatMap((key) => {
|
|
const offset = options[key];
|
|
return offset === undefined
|
|
? []
|
|
: [{ kind: OFFSET_OPTIONS[key], offset }];
|
|
});
|
|
const selected = selectors[0];
|
|
const source = options.sourceStream;
|
|
const refuse = (key: string, message: string) =>
|
|
projectAnalysisError(
|
|
new AnalysisInputError(extraction.name, undefined, [
|
|
{ path: [key], reason: "invalid_value", message },
|
|
]),
|
|
);
|
|
|
|
if (selectors.length > 1)
|
|
return refuse(
|
|
"volumeOffset",
|
|
"--volume-offset, --file-offset and --section-offset are mutually exclusive.",
|
|
);
|
|
if (source !== undefined && selected === undefined)
|
|
return refuse(
|
|
"sourceStream",
|
|
"--source-stream requires one target offset.",
|
|
);
|
|
|
|
return extract(
|
|
{
|
|
...input,
|
|
target:
|
|
selected === undefined
|
|
? { kind: "whole-image" }
|
|
: {
|
|
kind: selected.kind,
|
|
offset: selected.offset,
|
|
...(source === undefined
|
|
? {}
|
|
: { source_stream: source === "flash" ? null : source }),
|
|
},
|
|
},
|
|
context,
|
|
);
|
|
},
|
|
);
|
|
};
|