diff --git a/src/domain/apple/image4.ts b/src/domain/apple/image4.ts index fe26980e0cb..f563effd5e1 100644 --- a/src/domain/apple/image4.ts +++ b/src/domain/apple/image4.ts @@ -1,3 +1,4 @@ +import type { Image4Metadata } from "../formatMetadata/firmwareMetadata.js"; import { ASN1_MAX_TAG_NUMBER_OCTETS, Asn1BerFailure, @@ -56,7 +57,7 @@ export const IMAGE4_STRING_BYTES_LIMIT = "image4_string_bytes"; const FORMAT = "apple-img4"; -export type Image4Magic = "IMG4" | "IM4P" | "IM4M"; +export type Image4Magic = Image4Metadata["magic"]; /** * LZFSE block magics. `bvx2` is v2, `bvx1` is v1, `bvxn` is LZVN, and `bvx-` * is a raw block. Each one is compressed storage, not plain payload bytes. @@ -77,78 +78,36 @@ export const IMAGE4_PAYLOAD_MAGICS = [ export type Image4Compression = (typeof IMAGE4_COMPRESSIONS)[number]; export type Image4PayloadMagic = (typeof IMAGE4_PAYLOAD_MAGICS)[number]; -export interface Image4PropertyValueInteger { - readonly type: "integer"; - readonly decimal: string; -} -export interface Image4PropertyValueBoolean { - readonly type: "boolean"; - readonly value: boolean; -} -export interface Image4PropertyValueOctet { - readonly type: "octet"; - readonly hex: string; -} -export interface Image4PropertyValueOmitted { - readonly type: "omitted"; - readonly byte_length: number; -} -export type Image4PropertyValue = - | Image4PropertyValueInteger - | Image4PropertyValueBoolean - | Image4PropertyValueOctet - | Image4PropertyValueOmitted; +type Image4Fields = { + [Field in keyof Facts]: Field extends keyof Overrides + ? Overrides[Field] + : Facts[Field]; +}; +type MetadataManifest = NonNullable; +type MetadataProperty = MetadataManifest["properties"][number]; -export interface Image4Property { - name: string; - value: Image4PropertyValue; -} - -export interface Image4ObjectFacts { - name: string; - digest_hex: string | null; - properties: Image4Property[]; -} - -export interface Image4PayloadFacts { - type: string; - /** Null when the field is absent or its text could not be decoded. */ - description: string | null; - offset: number; - length: number; - compression: Image4Compression; - uncompressed_size: number | null; - payload_magic: Image4PayloadMagic | null; - encrypted: boolean; - /** Keybag entries; null when a keybag is present but was not decoded. */ - keybag_count: number | null; - /** Types that fit in 32 bits; null when the keybag was not decoded. */ - keybag_types: number[] | null; -} - -export interface Image4ManifestFacts { - /** Unsigned decimal, or null when the version element was not one. */ - version: string | null; - properties: Image4Property[]; - objects: Image4ObjectFacts[]; - signature_size: number | null; - certificate_chain_size: number | null; -} - -export interface Image4RestoreFacts { - properties_present: string[]; - values: "redacted"; -} - -export interface Image4Facts { - kind: "apple-img4"; - magic: Image4Magic; - payload: Image4PayloadFacts | null; - manifest: Image4ManifestFacts | null; - restore_info: Image4RestoreFacts | null; - /** Bytes after a container whose end was found, or null when it was not. */ - trailing_bytes: number | null; -} +export type Image4PropertyValue = Readonly; +export type Image4Property = Image4Fields< + MetadataProperty, + { value: Image4PropertyValue } +>; +export type Image4ObjectFacts = Image4Fields< + MetadataManifest["objects"][number], + { properties: Image4Property[] } +>; +export type Image4PayloadFacts = NonNullable; +export type Image4ManifestFacts = Image4Fields< + MetadataManifest, + { + properties: Image4Property[]; + objects: Image4ObjectFacts[]; + } +>; +export type Image4RestoreFacts = NonNullable; +export type Image4Facts = Image4Fields< + Image4Metadata, + { manifest: Image4ManifestFacts | null } +>; export interface Image4EntryFact { readonly role: "payload" | "manifest" | "restore-info"; diff --git a/src/domain/formatMetadata/firmwareMetadata.ts b/src/domain/formatMetadata/firmwareMetadata.ts index 8eb0d9c0fd8..f1be42bc0ed 100644 --- a/src/domain/formatMetadata/firmwareMetadata.ts +++ b/src/domain/formatMetadata/firmwareMetadata.ts @@ -797,6 +797,7 @@ const image4ObjectSchema = z.strictObject({ const image4PayloadSchema = z.strictObject({ type: z.string().min(1).max(IMAGE4_MAX_STRING_BYTES), + // Null when the field is absent or its text could not be decoded. description: z.string().max(IMAGE4_MAX_STRING_BYTES).nullable(), offset, length: offset, @@ -804,11 +805,14 @@ const image4PayloadSchema = z.strictObject({ uncompressed_size: offset.nullable(), payload_magic: z.enum(IMAGE4_PAYLOAD_MAGICS).nullable(), encrypted: z.boolean(), + // Keybag entries, or null when a present keybag could not be decoded. keybag_count: z.number().int().min(0).max(IMAGE4_MAX_ELEMENTS).nullable(), + // Types that fit in 32 bits, or null when the keybag was not decoded. keybag_types: z.array(uint32).max(IMAGE4_MAX_ELEMENTS).nullable(), }); const image4ManifestSchema = z.strictObject({ + // Unsigned decimal, or null when the version element was not one. version: image4UnsignedDecimal.nullable(), properties: z.array(image4PropertySchema).max(IMAGE4_MAX_PROPERTIES), objects: z.array(image4ObjectSchema).max(IMAGE4_MAX_ELEMENTS),