diff --git a/.crumb b/.crumb index aa3bb33f..5fc298a6 100644 --- a/.crumb +++ b/.crumb @@ -27,33 +27,33 @@ "purpose": "Directory crates. Purpose not yet described by a human." }, { - "digest": "9b91380635da227d4a2fb7667c961879f81445e00ec6e3784c1955e8a28dd1e6", - "last_commit": "56d125f", + "digest": "7e7934c11eb156bf4249648ef757d6135570877703a1ccab214c07cb5a57e235", + "last_commit": "f47f6c7", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "9b1a3dd490ab95dd51bd1b94a314eae35800c62fba5bb05b6e13189df7420738", - "last_commit": "56d125f", + "digest": "362c8c616872caca2069f3e748db0d8fb7c807bff2771679c4973481a15901f2", + "last_commit": "f47f6c7", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { "digest": "8f2ba6515d449096eccaa96e1d710d5556e36425d1b82eb8ef096d85200059f9", - "last_commit": "56d125f", + "last_commit": "22eb360", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:63a3bb1e28d23b5ae7225d2de0a34e9e3f75c14863c36a3d70c7c84ca848dc6f", - "compiled_at": "2026-10-12T10:28:17Z", + "children_root": "sha256:49ee7a6b7ab0a6a96d0329f00ef1839ff64e47aba7d5717ea27f8bbc865ace9f", + "compiled_at": "2026-10-12T11:44:52Z", "compiler": "crumb-compile/1", - "digest": "sha256:4962c66d02b68960e451bc7f64f82adc0548dff726b72ee9cd15cbde3312fc3a", + "digest": "sha256:1ee2a15770e45746c15c5cdea47206165cf26eda84c4a39d1746c1096d89e9cc", "evidence_files": 155, "evidence_root": "sha256:d59c0cef4e7dd5172923754a95b411ca54b01f35baca4587f792f1b259f1eaf7", - "files": 3085, - "generated_at_commit": "56d125f12abf2b8ba2886d6da4d86fda8dd244b3", - "source_tree": "sha256:f11fd859fa3c14ad139fc2dbced21d5c39d4b66ff17fbe09e97662c3a2e6ab22" + "files": 3087, + "generated_at_commit": "f47f6c76286d7c85b9d038afc730619c54266a9f", + "source_tree": "sha256:8e951d5463b4dde2502e3e2c7324bae7586f14fb04ef2355bc493b66ec6120e1" }, "owner_lane": "", "provenance": { diff --git a/.github/workflows/ck-kernel.yml b/.github/workflows/ck-kernel.yml index f9f05a87..33bc2abf 100644 --- a/.github/workflows/ck-kernel.yml +++ b/.github/workflows/ck-kernel.yml @@ -89,7 +89,7 @@ jobs: - name: Vendored Omega Cortex store matches omega.lock (P06 cut 3; shallow fetch of the pinned omega commit) run: bash scripts/omega_lock_check.sh - - name: Stage sources compile freestanding (nm -u: ck.h services only) + - name: "Stage sources compile freestanding (nm -u: ck.h services only)" run: make -C native/kernel stage-free - name: Gate runner self-test diff --git a/docs/.crumb b/docs/.crumb index 6599176c..f1139326 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -18,7 +18,7 @@ "children": [ { "digest": "a845beb92a810b2349a8fc52048276ca6949076e4343912bbb9b70859036bf8f", - "last_commit": "56d125f", + "last_commit": "22eb360", "name": "adr", "purpose": "Directory docs/adr. Purpose not yet described by a human." }, @@ -30,14 +30,14 @@ } ], "children_root": "sha256:baea130f2c52779b57d90139beb66ac01b21ab499d113471a334be5500f3e369", - "compiled_at": "2026-10-12T10:28:17Z", + "compiled_at": "2026-10-12T11:39:56Z", "compiler": "crumb-compile/1", - "digest": "sha256:9b91380635da227d4a2fb7667c961879f81445e00ec6e3784c1955e8a28dd1e6", + "digest": "sha256:7e7934c11eb156bf4249648ef757d6135570877703a1ccab214c07cb5a57e235", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 69, - "generated_at_commit": "56d125f12abf2b8ba2886d6da4d86fda8dd244b3", - "source_tree": "sha256:64d2069510df3b1bc132881c896185b6448ad71c489cca801a9cfd82065788c2" + "files": 70, + "generated_at_commit": "22eb3603074e5df77df9914dbd301d67122564b5", + "source_tree": "sha256:5641e25791a12e563582b9a529277f203c5ac192ffce39e8720ed47adc0edc19" }, "owner_lane": "", "provenance": { diff --git a/docs/GB10_FIRMWARE_INVENTORY.md b/docs/GB10_FIRMWARE_INVENTORY.md new file mode 100644 index 00000000..ac12670a --- /dev/null +++ b/docs/GB10_FIRMWARE_INVENTORY.md @@ -0,0 +1,301 @@ +# GB10 firmware inventory (aienos#286 cut C, host work only) + +Report plus one hosted checker. No firmware was loaded, no device was touched, +no QEMU run was made. This is the "inventory of firmware and protocol sources, +parsers and refusal tests for firmware metadata. No loading" item of +docs/GB10_NATIVE_DEPENDENCY_MAP.md section 7. Any firmware load or GSP handoff +on the real machine stays an OPERATOR STEP (same document, section 7). + +Tags: [OBSERVED] read on the Spark (spark-b87b, Linux 7.0.0-1019-nvidia, +nvidia-open 580.173.02) on 2026-10-12. [SOURCE file:line] read in the named +file of NVIDIA/open-gpu-kernel-modules at tag `580.173.02` (the same version as +the driver on the box; fetched read-only into a scratch directory, not vendored). +[DOCUMENTED] quoted from a licence or manual. [INFERRED] reasoned from sources. +[UNKNOWN] nothing read answers it. + +## 1. Plain-English summary + +- The GPU in the Spark is NVIDIA's "GB10B" chip (the integrated variant of the + GB10 family). To run it, three pieces of NVIDIA code have to be put in place + in a fixed order: a small signed "first stage" (GSP-FMC) that the SEC2 + security engine on the chip verifies and starts, which then unpacks and starts + the big GSP-RM firmware (the GPU's own resource manager, 75 MB on disk), after + which the host talks to the GPU through message queues the GSP-RM serves. +- Only the big piece is a file on disk. The first stage, its hash, its signature + and its public key are compiled into the Linux driver module as byte arrays + (open source, MIT licence, in the public driver repository). The GB10B has no + separate "FSP" security processor on the GSP boot path and runs no VBIOS + FWSEC step; SEC2 does the work. [SOURCE, section 4] +- Everything AIENOS would need to *hand* to the chip is public and + redistributable unmodified under NVIDIA's firmware licence and the driver's + MIT licence. What is not public is what the chip's boot ROM, SEC2 firmware, + ACR and GSP-RM *do* internally, and the signing keys. [DOCUMENTED, section 6] +- A hosted checker now exists: `make -C native/kernel gsp-fw-inspect-test` + (tool native/kernel/tools/ck_gsp_fw_inspect.c). It parses the firmware + container exactly as the driver does, refuses every malformed shape it was + given in its self-test (22 mutations plus every truncation), and proves the + file on this box matches the pinned digest. It never loads anything. +- The largest remaining unknowns for a native bring-up (section 8) are the + SEC2 command protocol details beyond what the driver source shows, the + exact GspFwWprMeta/boot-argument layout expected by this GSP-FMC build, and + the state the SoC firmware (UEFI) leaves SEC2 and the GPU in at AIENOS entry. + Do not attach a calendar to cut C/D until those are measured on the machine. + +## 2. Identity of the device and the driver on the box [OBSERVED] + +| Fact | Value | +|---|---| +| PCI address, id | `000f:01:00.0`, 10de:2e12 rev a1, subsystem 10de:21ec | +| Driver's supported-gpus entry | `{"devid":"0x2E12","subdevid":"0x21EC","subvendorid":"0x10DE","name":"NVIDIA GB10","features":[...,"kernelopen"]}` | +| nvidia-smi | Product NVIDIA GB10, Architecture Blackwell, VBIOS 9A.0B.2D.00.00, driver 580.173.02 | +| /proc/driver/nvidia/gpus/*/information | GPU Firmware 580.173.02, Video BIOS 9a.0b.2d.00.00, DMA 41 bits, IRQ 427 | +| Kernel module | /lib/modules/7.0.0-1019-nvidia/kernel/nvidia-580-open/nvidia.ko, 19,231,198 bytes, `license: Dual MIT/GPL`, declares `firmware: nvidia/580.173.02/gsp_ga10x.bin` and `firmware: nvidia/580.173.02/gsp_tu10x.bin` (modinfo) | +| Chip arch/impl in the driver | GB10B is `GPU_ARCHITECTURE_BLACKWELL_GB1XX` (0x01A0) implementation `GPU_IMPLEMENTATION_GB10B` (0x0B) [SOURCE src/common/inc/swref/published/nv_arch.h:51,111] | + +## 3. The firmware pieces, their identity and where each one lives + +### 3.1 GSP-RM container on disk (the only file-based piece) + +Package `nvidia-firmware-580-580.173.02`, directory `/lib/firmware/nvidia/580.173.02/`. [OBSERVED] + +| File | Bytes | SHA-256 | Used by GB10? | +|---|---|---|---| +| gsp_ga10x.bin | 75,012,080 | `32cb7cf3e77f97e92f6e92135939ccb3ba382748144569c0dfccf3338d43d963` | yes (section 3.2) | +| gsp_tu10x.bin | 30,471,256 | `6f3ccbd570c7ac2a7ea910d9d87fc3d23db9ae3dfe82020ea07b17a30954495e` | no (Turing/GA100 only) | + +Container layout of gsp_ga10x.bin, as printed by `ck_gsp_fw_inspect inspect` +[OBSERVED]: ELF64, little-endian, type REL, machine RISC-V, 17 sections. + +| Section | Offset | Size | Note | +|---|---|---|---| +| .fwimage | 64 | 74,969,088 | GSP-RM image; SHA-256 `931add19204a5f0240442272f58fb2bc5db6cec4dd6f6341c1d888754146aa07` | +| .note.gnu.build-id | 74,969,152 | 36 | NT_GNU_BUILD_ID `b96f53b91f7ff27613d8afe25762756a42a4dc1e` | +| .fwversion | 74,969,188 | 11 | `580.173.02` plus NUL | +| .fwsignature_gb20y, _cc_gb20x, _gb20x, _cc_gb10x, **_gb10y**, _gb10x, _cc_gh100, _gh100, _ad10x, _ga10x | 74,969,199 onward | 4,096 each | one per chip family; GB10B uses `.fwsignature_gb10y`, SHA-256 `08541c3a909f8fd21c3eaf55fbe6c6c30ff2b7729e885417f3fcb06a4d108f2d` | +| .symtab, .strtab, .shstrtab | 75,010,160 onward | 96, 466, 265 | ELF bookkeeping | + +### 3.2 How the driver picks that file and that signature section [SOURCE] + +- File name: `#define NV_FIRMWARE_FOR_NAME(name) "nvidia/" NV_VERSION_STRING "/" name ".bin"` [kernel-open/nvidia/nv.c:27]; loaded with `request_firmware(&fw, nv_firmware_for_chip_family(fw_type, fw_chip_family), nvl->dev)`, "path is relative to /lib/firmware" [nv.c:4058-4062]. +- Chip family: `case GPU_ARCHITECTURE_BLACKWELL_GB1XX: if (gpuImpl == GPU_IMPLEMENTATION_GB10B) return NV_FIRMWARE_CHIP_FAMILY_GB10Y; else return NV_FIRMWARE_CHIP_FAMILY_GB10X;` [src/nvidia/arch/nvalloc/common/inc/nv-firmware-chip-family-select.h:58-62]. Family string: `case NV_FIRMWARE_CHIP_FAMILY_GB10Y: return "gb10y";` [kernel-open/common/inc/nv-firmware.h:63]. +- File for the family: GB10X, GB10Y, GB20Y, GB20X, GH100, AD10X all `return NV_FIRMWARE_FOR_NAME("gsp_ga10x");` [nv-firmware.h:95-102]; GA100, TU11X, TU10X get `gsp_tu10x` [nv-firmware.h:104-107]. So the file is `nvidia/580.173.02/gsp_ga10x.bin`. +- Section names: `GSP_VERSION_SECTION_NAME ".fwversion"`, `GSP_IMAGE_SECTION_NAME ".fwimage"`, `GSP_SIGNATURE_SECTION_NAME_PREFIX ".fwsignature_"`, `GSP_BUILD_ID_SECTION_NAME ".note.gnu.build-id"`, `GSP_CC_SIGNATURE_SECTION_NAME_PREFIX ".fwsignature_cc_"` [src/nvidia/generated/g_kernel_gsp_nvoc.h:269-274]. +- Signature prefix for GB10B: the HAL table assigns `kgspGetSignatureSectionNamePrefix_GB100` to "GB100 | GB102 | GB10B | ..." [g_kernel_gsp_nvoc.c:1809-1813]; that function returns the `_cc_` prefix only when confidential computing is enabled, else `GSP_SIGNATURE_SECTION_NAME_PREFIX` [src/nvidia/src/kernel/gpu/gsp/arch/blackwell/kernel_gsp_gb100.c:94-108]. The section name is prefix + family [kernel_gsp.c:4805-4840]. Confidential computing is not enabled on this box [INFERRED (high): nvidia-smi reports no CC mode; not separately verified], so the section is `.fwsignature_gb10y`. +- Version check: `.fwversion` must be nonzero, shorter than 64 bytes, NUL-terminated and equal to `NV_VERSION_STRING`, else `NV_ERR_INVALID_DATA` [kernel_gsp.c:4762-4786]. +- Section lookup bounds checks: ELF identifier, section header table validity, name index inside the names table, `portSafeAddU64(offset, size - 1)` inside the buffer [kernel_gsp.c:5115-5166]. +- What the driver then does with the pieces (not reproduced here): copies the signature into its own memory descriptor (`_kgspCreateSignatureMemdesc`), builds a radix-3 page table over `.fwimage` for the GSP's DMA (`kgspCreateRadix3`), keeps the build id [kernel_gsp.c:4871-4905]. + +### 3.3 GSP-FMC first stage and the boot descriptor, compiled into the driver [SOURCE] + +The driver embeds these as C byte arrays ("bindata") with an MIT licence header +("THIS FILE IS AUTOMATICALLY GENERATED"). For GB10B the HAL table selects +`kgspGetBinArchiveGspRmBoot_GB10B` [g_kernel_gsp_nvoc.c:788-790] and +`kgspGetBinArchiveGspRmFmcGfwProdSigned_GB10B` [g_kernel_gsp_nvoc.c:924-926] +(debug-fused parts use the `...DebugSigned_GB10B` archive, [889-891]). + +| Archive label | Original file name (NVIDIA internal) | Bytes | +|---|---|---| +| GspRmBoot_GB10B UCODE_IMAGE_PROD | kernel/inc/gspcc/bin/g_gsp_gb10b_dyn_prd_image.bin | 196,608 | +| GspRmBoot_GB10B UCODE_DESC_PROD | kernel/inc/gspcc/bin/g_gsp_gb10b_dyn_prd_desc.bin | 84 (RM_RISCV_UCODE_DESC) | +| GspRmBoot_GB10B UCODE_IMAGE_DBG / DESC_DBG | ..._dbg_image.bin / ..._dbg_desc.bin | 196,608 / 84 | +| GspRmFmcGfwProdSigned_GB10B UCODE_IMAGE | kernel/inc/gspcc/bin/g_gsp_gb10b_dyn_prd_gfw_image.bin | 195,912 | +| GspRmFmcGfwProdSigned_GB10B UCODE_HASH | g_gsp_gb10b_dyn_prd_gfw_hash.bin | 48 (SHA-384) | +| GspRmFmcGfwProdSigned_GB10B UCODE_SIG | g_gsp_gb10b_dyn_prd_gfw_sig.bin | 384 | +| GspRmFmcGfwProdSigned_GB10B UCODE_PKEY | g_gsp_gb10b_dyn_prd_gfw_pub_key.bin | 384 | + +Source files: `src/nvidia/generated/g_bindata_kgspGetBinArchiveGspRmBoot_GB10B.c` +(2,466,261 bytes of C) and +`g_bindata_kgspGetBinArchiveGspRmFmcGfwProdSigned_GB10B.c` (1,238,060 bytes), +metadata comments at lines 31-40 and the storage records near the end of each +file. The README says the `nouveau/` script "is used to extract some of the +firmware binary images (and related data) encoded in the source code and store +them as distinct files. These files are used by the Nouveau device driver" +[README.md:170-173]; that script knows gh100, gb100 and gb202 FMC images, not +gb10b [nouveau/extract-firmware-nouveau.py:655-662,775-781]. Extracting the +GB10B arrays would be our own small step over the same public source. +[INFERRED (high)] + +### 3.4 Pieces that are NOT on the host side at all + +- SEC2's own firmware and the chip boot ROM: not loaded by the driver on GB10B. + The driver only sends SEC2 commands through its EMEM queue (section 4). Who + loads SEC2 firmware before Linux starts (SoC boot chain, UEFI) is [UNKNOWN] + from these sources. +- ACR (the access-control ucode that sets up the protected region WPR2) and + anything inside GSP-FMC and GSP-RM: NVIDIA binaries, run on the chip, not + readable by us (licence section 6). +- Booter Load/Unload ucodes (older dGPU path) and VBIOS FWSEC: not used on + GB10B (section 4). + +## 4. The loading path the pinned driver uses on GB10B [SOURCE] + +This is the order the native kernel would have to reproduce. Only the parts +visible in the open driver are listed; what the chip does in between is +inferred from the driver's comments. + +1. **No FSP on this chip.** `PDB_PROP_KFSP_IS_MISSING` is false only for + "GH100 | GB100 | GB102 | GB110 | GB112 | GB202 | GB203 | GB205 | GB206 | + GB207"; GB10B falls to the default, true [g_kern_fsp_nvoc.c:227-236]. +2. **SEC2 boots the GSP-FMC.** `PDB_PROP_KSEC2_BOOT_GSPFMC` is true exactly for + "GB10B | GB20B | GB20C" [g_kernel_sec2_nvoc.c:446-450]. The GSP code takes the + SEC2 branch when there is no FSP: `ksec2PrepareBootCommands_HAL` + [kernel_gsp_gh100.c:803-808] and "Starting to boot GSP via SEC2" then + `ksec2SendBootCommands_HAL` [kernel_gsp_gh100.c:868-872]. +3. **No VBIOS FWSEC step.** `kgspExtractVbiosFromRom` for GB10B is the stub + `_395e98` [g_kernel_gsp_nvoc.c:1300], which returns `NV_ERR_NOT_SUPPORTED` + [g_kernel_gsp_nvoc.h:1803-1805]; the init code treats that as "Extracting + VBIOS image from ROM is not supported" and continues [kernel_gsp.c:4053-4058]. + The VBIOS version string nvidia-smi shows therefore comes from elsewhere + (GSP-RM at runtime) [INFERRED (medium)]. +4. **No Booter ucodes.** `bPartitionedFmc` is true for "GH100 | GB100 | GB102 | + GB10B | ..." [g_kernel_gsp_nvoc.c:447-451]; with it set, "The secure boot + ucode is included in the partitioned FMC, no need for separate Booter + ucodes" [kernel_gsp.c:4076-4082]. +5. **Host-side preparation** (`kgspInitRm`): prepare the boot binary (copy the + GspRmBoot image into 4 KiB-aligned system memory flagged + `MEMDESC_FLAGS_ALLOC_IN_UNPROTECTED_MEMORY`, keep the 84-byte descriptor) + [kernel_gsp.c:4583-4640]; prepare the GSP-RM image (version check, `.fwimage`, + signature section, radix-3 page table, build id) [kernel_gsp.c:4848-4905]; + logging structures; wait for `GFW_BOOT` OK (`kgspWaitForGfwBootOk_HAL`); set + GPU time; libos init args and message-queue init args [kernel_gsp.c:4108-4165]. +6. **WPR meta for an integrated GPU.** `kgspPopulateWprMeta_GB10B` reuses the + GH100 layout, then sets `vgaWorkspaceSize = 0`, `pmuReservedSize = 0`, + `nonWprHeapSize = 0`, with the comment "For GB10Y, unlike on dGPU, this + structure is in sysmem" and the WPR2 layout diagram (WPR/LSB header, ACR + metadata, GSP FW heap, GSP FW ELF, boot bin) [kernel_gsp_gb10b.c:32-90]. + "The actual offsets are determined by secure ACR ucode and patched into the + GspFwWprMeta structure" [kernel_gsp_gb10b.c:33-35]. +7. **SEC2 command.** `ksec2SetupGspImages_GB10B` copies the FMC image into + system memory, writes its physical address into an `NVDM_PAYLOAD_COT` + (`gspFmcSysmemOffset`, `frtsSysmemSize` = image size), copies the 48-byte + hash, the 384-byte signature and the 384-byte public key into the payload, + and points `gspBootArgsSysmemOffset` at the cached GSP-FMC arguments + [kernel_sec2_gb10b.c:438-535]. `ksec2PrepareBootCommands_GB10B` allocates + the payload with `version = cotPayloadVersion` [561-583]; + `ksec2SendBootCommands_GB10B` sends it as `NVDM_TYPE_COT` through + `ksec2SendMessage` [612-623]. Transport: an MCTP/NVDM packet written a DWORD + at a time into SEC2 EMEM channel 0 (1 KiB) via `NV_PSEC_EMEMC`/`NV_PSEC_EMEMD`, + then queue head/tail writes (`NV_PSEC_QUEUE_TAIL` before `NV_PSEC_QUEUE_HEAD` + "because it will interrupt SEC2") [kernel_sec2_gb10b.c:36-210]. Register + definitions come from `published/blackwell/gb10b/dev_sec_pri.h`, + `dev_gsp.h`, `dev_falcon_v4.h` [kernel_sec2_gb10b.c:30-32]. +8. **Wait and hand over.** Wait for SEC2 to release the GSP target mask + (`ksec2WaitForGspTargetMaskReleased_HAL`), then for lockdown release or an + FMC error in `NV_PFALCON_FALCON_MAILBOX0`, program `NV_PFALCON_FALCON_OS` + with the descriptor's `appVersion`, confirm the RISC-V core is active, init + the status queue, wait for `kgspWaitForRmInitDone` [kernel_gsp_gh100.c:874-982]. + From then on the host talks to GSP-RM over the RPC message queues. +9. **Teardown and reset.** `kgspResetHw_GB100` (waits for reset access, then + resets the engine) [kernel_gsp_gb100.c:46-88]; `kgspTeardown_GH100` + [kernel_gsp_gh100.c:995]. Not read in detail here. + +## 5. Hosted checker: ck_gsp_fw_inspect + +`native/kernel/tools/ck_gsp_fw_inspect.c`; Makefile targets `gsp-fw-inspect` +and `gsp-fw-inspect-test` (the latter is part of `make -C native/kernel test`, +so CI runs the self-test on every PR; the real-file step prints NOT_RUN when +`/lib/firmware/nvidia/580.173.02/gsp_ga10x.bin` is absent, as it is in CI). + +- `inspect FILE [--family NAME] [--expect-version V]`: the driver's checks + from section 3.2 plus a bounds check on the build-id note, then SHA-256 of the + whole file, of `.fwimage` and of the selected signature section. Exit 0 and + `RESULT OK`, or exit 2 and `RESULT REFUSED `. +- `selftest`: a 640-byte valid container built in memory, then 22 single-field + mutations (bad magic, 32-bit, big-endian, wrong type, wrong machine, wrong + shentsize, zero sections, shstrndx out of range, table past end, string + table without NUL, name index past table, section past end, offset overflow + near 2^64, empty image, version without NUL, version text mismatch, 64-byte + version, empty signature, signature for another family only, wrong note + type, descriptor past section, truncated note) plus every truncation from 0 + to 639 bytes; each must be refused for the named reason. +- Result on the box, 2026-10-12 [OBSERVED]: `CK_GSP_FW_INSPECT_SELFTEST: PASS + (22 mutations)`; `CK_GSP_FW_INSPECT_PIN: PASS` with the file digest equal to + the pin in the Makefile (`GSP_FW_SHA256`). +- It does not verify NVIDIA's signature over `.fwimage` (that is the chip's + job; format and keys are not ours) and it never loads anything. + +## 6. Licence limits [DOCUMENTED] + +Firmware package: `/usr/share/doc/nvidia-firmware-580-580.173.02/copyright`, +"License: NVIDIA-graphics-drivers" (NVIDIA Driver License Agreement): + +- 1.1(d): may "Distribute the SOFTWARE ... for use with operating system + kernels distributed under the terms of an OSI-approved open source license + ... provided that (i) the binary files thereof are not modified in any way + ... and (ii) this Agreement is provided to each SOFTWARE recipient". AIENOS is + AGPL-3.0, an OSI-approved licence, so shipping the unmodified file plus the + agreement text is within this clause [INFERRED (high): plain reading, not + legal advice]. +- 2.1: firmware only on NVIDIA platforms; no translation to another + architecture or language. 2.2: no reverse engineering, decompiling or + disassembly of binaries. 2.3: no derivative works of binaries. 2.6: no + bypassing authentication or security mechanisms. 2.9: may not cause the + SOFTWARE to become subject to an open source licence. +- Consequence for this project: we may ship and load the bytes as they are, + parse their public container format (the driver does exactly that, in MIT + source), and must never patch, unpack `.fwimage`, or work around the + signature checks. A SEC2 refusal is final and is reported, never bypassed. + +Driver source: `COPYING` says "Except where noted otherwise, the individual +files within this package are licensed as MIT"; every file read here carries +`SPDX-License-Identifier: MIT` (including the bindata arrays and +`kernel_sec2_gb10b.c`). The kernel module reports `Dual MIT/GPL`. Reading and +reimplementing the host-side protocol from MIT source is permitted; copying +code into AIENOS would require carrying the MIT notice (none was copied here). + +## 7. What this means for cut C and cut D + +- Cut C "inventory" is done by this document; "parsers and refusal tests for + firmware metadata" by `ck_gsp_fw_inspect`. The next host-only steps that do + not need the machine: (a) a small extractor over the two public + `g_bindata_*_GB10B.c` files that writes the eight arrays as files with + digests (our own tool, no Python); (b) a C struct definition and encoder for + `NVDM_PAYLOAD_COT` and the MCTP/NVDM headers with fixture tests, from + `src/nvidia/inc/kernel/gpu/fsp/kern_fsp_cot_payload.h`, + `fsp/fsp_nvdm_format.h`, `fsp/fsp_mctp_format.h` (not yet read); (c) the + `GspFwWprMeta` and GSP-FMC argument layouts from the same source tree. +- The machine-side part of cut C (first SEC2 command, FMC boot, GSP-RM init + done) is an attended-window item under the P08 rules: documented cold power + state, no prior Linux GPU initialization, camera, abort rule, and the + B7b-style one-time boot entry. Nothing in this document authorizes it. + +## 8. Known unknowns + +1. [UNKNOWN] The exact `NVDM_PAYLOAD_COT` field layout, `cotPayloadVersion`, + `cotPayloadSignatureSize` and `cotPayloadPublicKeySize` values for GB10B + (set in the SEC2 object constructor; not read). +2. [UNKNOWN] The `GspFwWprMeta`, GSP-FMC argument block (`pGspFmcArgumentsCached`) + and libos init-argument layouts this FMC build expects. +3. [UNKNOWN] What SEC2 requires to be already true when the first command + arrives (its own firmware loaded by the SoC boot chain? clocks? power + state?), and whether UEFI leaves the GPU in that state at AIENOS entry. + On Linux the driver finds "GFW_BOOT" complete before it starts + (`kgspWaitForGfwBootOk_HAL`); who completes it without Linux is not shown. +4. [UNKNOWN] The message-queue (RPC) format and the init sequence after + `RmInitDone`; the native kernel needs only a subset (device init, memory + mapping, channel, submit) but which RPCs are mandatory is not established. +5. [UNKNOWN] Whether the confidential-computing (`_cc_`) sections or the SPDM + session are ever required on this platform. Assumed not [INFERRED (high)]. +6. [UNKNOWN] The exact meaning of the `GspRmBoot_GB10B` image versus the + `GspRmFmcGfwProdSigned_GB10B` image (both ~196 KiB): the SEC2 path sends the + FMC archive, the boot-binary preparation copies the GspRmBoot image and its + RM_RISCV_UCODE_DESC; which one the GSP's boot ROM consumes on GB10B, and how + the descriptor offsets are used, was not traced end to end. +7. [UNKNOWN] Reset and re-open behavior on GB10B after a failed FMC boot + (`kgspResetHw_GB100` waits for "reset access"); needed for the + resource-incarnation protection (#285) before any repeated attempt. + +## 9. Scope and limits of this document + +Read: nv-firmware.h, nv-firmware-chip-family-select.h, nv_arch.h, nv.c (the +firmware load function only), kernel_gsp.c (init, container parsing, boot +binary), kernel_gsp_gh100.c (prepare, bootstrap), kernel_gsp_gb100.c, +kernel_gsp_gb10b.c, kernel_gsp_booter.c and kernel_gsp_fwsec.c (grep only), +kernel_sec2_gb10b.c, g_kernel_gsp_nvoc.c/.h, g_kernel_sec2_nvoc.c, +g_kern_fsp_nvoc.c (property tables only), the two GB10B bindata files +(metadata comments only), README.md, COPYING, the firmware copyright file. Not +read: kern_fsp_cot_payload.h, the NVDM/MCTP format headers, kernel_sec2.c +(`ksec2SendMessage`), the GSP RPC layer, dev_sec_pri.h register values, ACR or +GSP internals (not available). No Context7 or web manual covers this layer; the +DGX Spark user guide is silent on GSP firmware loading (DOCS SILENT on native +GSP bring-up). diff --git a/docs/GB10_NATIVE_DEPENDENCY_MAP.md b/docs/GB10_NATIVE_DEPENDENCY_MAP.md index 3a62cf2a..98bbb14f 100644 --- a/docs/GB10_NATIVE_DEPENDENCY_MAP.md +++ b/docs/GB10_NATIVE_DEPENDENCY_MAP.md @@ -202,7 +202,7 @@ Two orders exist for native AIENOS. Order 1 (older): M5 trust, M6 network, M7 in - Cut A in full: this map, the platform-services contract, hiding `NvrmMem` behind typed handles, moving `nvrm_mthd` and `nvrm_gp_entry` into a shared encoding header, a freestanding link target for the codegen, QMD and engine files, backend contract tests over the existing fake. INFERRED (high): no kernel or hardware change. - B host fixtures: multi-segment MCFG parsing tests, fake config-space access that fails on any write, GPU identity fixture on a nonzero segment, malformed and overlapping entry tests. These are pure parsing and fixtures. The issue already authorizes "Host-only interface, parser, fixture, and refusal work". -- C host work: inventory of firmware and protocol sources, parsers and refusal tests for firmware metadata. No loading. +- C host work: inventory of firmware and protocol sources, parsers and refusal tests for firmware metadata. No loading. DONE 2026-10-12: docs/GB10_FIRMWARE_INVENTORY.md and native/kernel/tools/ck_gsp_fw_inspect.c (`make -C native/kernel gsp-fw-inspect-test`); the next host-only steps and the remaining unknowns are listed there (sections 7 and 8). - D host work: simulated-device tests for allocator, address-space bookkeeping and refusal paths, using no real GPU. ### Cannot start until #163's decision is made, or needs coordination first @@ -240,7 +240,7 @@ Where it may bite here. A GPU channel is the likely domain-1 kernel channel (INF ## 8. Known unknowns -1. UNKNOWN: GB10 GSP firmware transport and initialization protocol, exact firmware blobs and versions, and what is public. Our source contains none of it (RM does it, [nvrm.c:223](https://github.com/aien-dev/physics/blob/6d7cf0d4d8eb2cda7b512100ff6058e25dbb3ddf/nvrm/nvrm.c#L223)). Blocks cut C. +1. PARTLY RESOLVED 2026-10-12 (docs/GB10_FIRMWARE_INVENTORY.md): the blobs, versions, digests, the SEC2-boots-GSP-FMC path on GB10B and the licence limits are now inventoried from the public driver source at tag 580.173.02; what is public: everything the host hands to the chip. Still UNKNOWN: the COT payload and WPR-meta layouts, SEC2 preconditions at AIENOS entry, and the mandatory RPC subset (inventory section 8). Our source contains none of it (RM does it, [nvrm.c:223](https://github.com/aien-dev/physics/blob/6d7cf0d4d8eb2cda7b512100ff6058e25dbb3ddf/nvrm/nvrm.c#L223)). Still blocks the machine-side part of cut C. 2. UNKNOWN: GPU MMU page-table format, TLB invalidation and fault-reporting registers on GB10, as implemented by the pinned driver. Our code only requests mappings. 3. UNKNOWN: how the work-submit token is derived and the channel is made runnable without RM (`GET_WORK_SUBMIT_TOKEN`, `GPFIFO_SCHEDULE`). 4. UNKNOWN: which words of the captured constant bank 0 template and the 18-word setup prefix are context-independent versus values RM chose. Needed before a native context can reuse the Omega QMD and cbank builders. diff --git a/native/.crumb b/native/.crumb index bccf05c0..34313b0e 100644 --- a/native/.crumb +++ b/native/.crumb @@ -79,8 +79,8 @@ "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "58ca40969a50080dfe0c0add70eb0d38635b6abb39418f72a0872d4e84a2da67", - "last_commit": "56d125f", + "digest": "363106d1f490a2b01ec6e574586fba7608c4d21daa15f8193a6eb6c09436b771", + "last_commit": "22eb360", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, @@ -104,20 +104,20 @@ }, { "digest": "eeec17e754d15d5723d77d953711620c568c160f47e6c90a4c927c85ec5bb32d", - "last_commit": "56d125f", + "last_commit": "22eb360", "name": "store", "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:4073411743a525add34b064d8fc07e9882b7aecf69b3cde42f47b9dc84182b48", - "compiled_at": "2026-10-12T10:28:17Z", + "children_root": "sha256:b131138811cf35bc55eaf03588d4811aedb64faed4f43d833f5744c5b59cf78d", + "compiled_at": "2026-10-12T11:39:56Z", "compiler": "crumb-compile/1", - "digest": "sha256:9b1a3dd490ab95dd51bd1b94a314eae35800c62fba5bb05b6e13189df7420738", + "digest": "sha256:362c8c616872caca2069f3e748db0d8fb7c807bff2771679c4973481a15901f2", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", - "files": 469, - "generated_at_commit": "56d125f12abf2b8ba2886d6da4d86fda8dd244b3", - "source_tree": "sha256:3bc13822d57a405451ef88e000a5e109b64e815ef9747e64d65a431b390645a3" + "files": 470, + "generated_at_commit": "22eb3603074e5df77df9914dbd301d67122564b5", + "source_tree": "sha256:f04c1759895b503bd9e16472e7390e4548af3a69bafe559cd4bd4ab865111550" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index 9f9df203..632d4982 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -58,7 +58,7 @@ }, { "digest": "13c7a827e3ccb3c9395a86080f2a7c7540037fc7c4fb6039c80fbcc6b7413bdf", - "last_commit": "56d125f", + "last_commit": "22eb360", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, @@ -82,32 +82,32 @@ }, { "digest": "df0e798c909b72635d6b3aaba97462da29f468723857ee4fd055110c8eb07c1d", - "last_commit": "56d125f", + "last_commit": "22eb360", "name": "svc", "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, { "digest": "c65a92ef6ebb21a41f8148c443f9e87d142734e5f08b3d0402644060ff07c4bf", - "last_commit": "56d125f", + "last_commit": "22eb360", "name": "tests", "purpose": "Directory native/kernel/tests. Purpose not yet described by a human." }, { - "digest": "118855445b6fc55bc5abbcfc5d684135abcb0e8092d43edde4bca881ae5213f7", + "digest": "17933def2bc3028812efbff34692c71e95e21d7c423e64646a3f2ad9dc5777ea", "last_commit": "e90226b", "name": "tools", "purpose": "Directory native/kernel/tools. Purpose not yet described by a human." } ], - "children_root": "sha256:3b61ea92b741789db9a3fbade47fea1d4ee7819cbee5b2abfa809b879a4ad717", - "compiled_at": "2026-10-12T10:28:17Z", + "children_root": "sha256:c02bf658c88780c2019300a9cc12cb47465a58d3c8a7f9536ebdb3ca37f28d31", + "compiled_at": "2026-10-12T11:39:56Z", "compiler": "crumb-compile/1", - "digest": "sha256:58ca40969a50080dfe0c0add70eb0d38635b6abb39418f72a0872d4e84a2da67", + "digest": "sha256:363106d1f490a2b01ec6e574586fba7608c4d21daa15f8193a6eb6c09436b771", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 305, - "generated_at_commit": "56d125f12abf2b8ba2886d6da4d86fda8dd244b3", - "source_tree": "sha256:ddded0ac959d45d27f08036f51b4818ba4fb70705b1d83851e278b91360adc04" + "files": 306, + "generated_at_commit": "22eb3603074e5df77df9914dbd301d67122564b5", + "source_tree": "sha256:e6ca170ff7a8f29ca37248259e42598bbb44d61d116f5f357f301e91eb2ffea1" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 4905a435..9c0d1971 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -1066,8 +1066,8 @@ smmu-l2-mutants: $(OUT)/host/test_smmu else echo "mutant flag refused under CK_HARDWARE_STAGING"; fi; \ if [ $$fail = 0 ]; then echo "CK_SMMU_L2_MUTANTS: PASS"; else echo "CK_SMMU_L2_MUTANTS: FAIL"; exit 1; fi -test: $(addprefix $(OUT)/host/,$(TESTS)) handoff-mutant acpi-dev-mutant iort-mutants smmu-l2-mutants osh-op-mutant test-continuity-subject-restart - @fail=0; for t in $(filter-out handoff-mutant acpi-dev-mutant iort-mutants smmu-l2-mutants osh-op-mutant test-continuity-subject-restart,$^); do $$t || fail=1; done; \ +test: $(addprefix $(OUT)/host/,$(TESTS)) handoff-mutant acpi-dev-mutant iort-mutants smmu-l2-mutants osh-op-mutant test-continuity-subject-restart gsp-fw-inspect-test + @fail=0; for t in $(filter-out handoff-mutant acpi-dev-mutant iort-mutants smmu-l2-mutants osh-op-mutant test-continuity-subject-restart gsp-fw-inspect-test,$^); do $$t || fail=1; done; \ if [ $$fail = 0 ]; then echo "CK_CORE_HOST: PASS"; else echo "CK_CORE_HOST: FAIL"; exit 1; fi sanitize: $(addprefix $(OUT)/host-san/,$(TESTS)) @@ -1448,6 +1448,26 @@ $(MODEL_MAP_TOOL): $(KDIR)/tools/ck_model_map.c $(NATIVE)/argus/sha256.c $(BOOT) $(HOSTCC) -std=gnu11 -O2 -Wall -Wextra -Werror -I$(BOOT) -I$(NATIVE)/argus -o $@ $(KDIR)/tools/ck_model_map.c $(NATIVE)/argus/sha256.c model-map: $(MODEL_MAP_TOOL) +# GSP firmware container inspector (aienos#286 cut C, host work only: inventory +# and refusal checks for the NVIDIA GSP-RM ELF container the pinned Linux +# driver loads for the GB10; docs/GB10_FIRMWARE_INVENTORY.md). Never loads +# anything. SHA-256 is the kernel's own (argus). The real file is optional: +# when absent the inspect step is NOT_RUN, the self-test still runs. +GSP_FW_TOOL := $(OUT)/host/ck_gsp_fw_inspect +GSP_FW_FILE ?= /lib/firmware/nvidia/580.173.02/gsp_ga10x.bin +GSP_FW_SHA256 := 32cb7cf3e77f97e92f6e92135939ccb3ba382748144569c0dfccf3338d43d963 +$(GSP_FW_TOOL): $(KDIR)/tools/ck_gsp_fw_inspect.c $(NATIVE)/argus/sha256.c $(NATIVE)/argus/sha256.h + @mkdir -p $(dir $@) + $(HOSTCC) -std=gnu11 -O2 -Wall -Wextra -Werror -I$(NATIVE)/argus -o $@ $(KDIR)/tools/ck_gsp_fw_inspect.c $(NATIVE)/argus/sha256.c +gsp-fw-inspect: $(GSP_FW_TOOL) +gsp-fw-inspect-test: $(GSP_FW_TOOL) + $(GSP_FW_TOOL) selftest + @if [ -r "$(GSP_FW_FILE)" ]; then \ + out=$$($(GSP_FW_TOOL) inspect "$(GSP_FW_FILE)" --family gb10y --expect-version 580.173.02) || { echo "$$out"; echo "CK_GSP_FW_INSPECT_PIN: FAIL (refused)"; exit 1; }; \ + echo "$$out" | grep -E '^(fwversion|build_id|signature_section|sha256_file|RESULT)'; \ + echo "$$out" | grep -q '^sha256_file $(GSP_FW_SHA256)$$' && echo "CK_GSP_FW_INSPECT_PIN: PASS" || { echo "CK_GSP_FW_INSPECT_PIN: FAIL (digest differs from the pin)"; exit 1; }; \ + else echo "CK_GSP_FW_INSPECT_PIN: NOT_RUN ($(GSP_FW_FILE) absent)"; fi + # ---- host checker for the AIENOS_CK_SCREEN gate (scripts/qemu_ck_screen_test.sh) ---- FB_CHECK_TOOL := $(OUT)/host/ck_fb_check $(FB_CHECK_TOOL): $(KDIR)/tools/ck_fb_check.c $(KDIR)/core/fbcon.c $(KDIR)/core/fbcon.h $(KDIR)/core/font8x8.h diff --git a/native/kernel/tools/.crumb b/native/kernel/tools/.crumb index 67bb86e3..ed60363b 100644 --- a/native/kernel/tools/.crumb +++ b/native/kernel/tools/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-10T04:29:33Z", + "compiled_at": "2026-10-12T11:39:56Z", "compiler": "crumb-compile/1", - "digest": "sha256:118855445b6fc55bc5abbcfc5d684135abcb0e8092d43edde4bca881ae5213f7", + "digest": "sha256:17933def2bc3028812efbff34692c71e95e21d7c423e64646a3f2ad9dc5777ea", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 12, - "generated_at_commit": "ed308de52ede1d4c8a211d88e0e4a3ac5c92ec23", - "source_tree": "sha256:180f992d3310a0d3e83349cfb54abcab8a83a6094c3340de1a9a45c7191f7b8b" + "files": 13, + "generated_at_commit": "22eb3603074e5df77df9914dbd301d67122564b5", + "source_tree": "sha256:7881dde2c31a3a3cef6c69fbb636f2ca6d6c4e9c12ad199c1a92c870e68764d6" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tools/ck_gsp_fw_inspect.c b/native/kernel/tools/ck_gsp_fw_inspect.c new file mode 100644 index 00000000..c4204528 --- /dev/null +++ b/native/kernel/tools/ck_gsp_fw_inspect.c @@ -0,0 +1,365 @@ +/* ck_gsp_fw_inspect -- host tool: inventory and refusal checks for the NVIDIA + * GSP-RM firmware container that the pinned Linux driver (nvidia-open + * 580.173.02) loads for the GB10 (aienos#286 cut C, host work: "inventory of + * firmware and protocol sources, parsers and refusal tests for firmware + * metadata. No loading"). docs/GB10_FIRMWARE_INVENTORY.md is the report this + * tool backs. + * + * The container is an ELF64 relocatable object for RISC-V whose sections carry + * the payload: .fwimage (the GSP-RM image), .fwversion (NUL-terminated driver + * version string), .note.gnu.build-id, and one .fwsignature_ section + * per chip family. The driver's own checks that this tool mirrors (file:line + * in open-gpu-kernel-modules tag 580.173.02): + * - section lookup with bounds checks on the section header table, the + * section-name table and each section's offset+size + * (src/nvidia/src/kernel/gpu/gsp/kernel_gsp.c:5091-5170); + * - .fwversion must be NUL-terminated, shorter than 64 bytes and equal to + * NV_VERSION_STRING (kernel_gsp.c:4731-4790); + * - the signature section is ".fwsignature_" + chip family name + * (kernel_gsp.c:4805-4840), and the GB10 (GPU_IMPLEMENTATION_GB10B) maps + * to family "gb10y" (src/nvidia/arch/nvalloc/common/inc/ + * nv-firmware-chip-family-select.h:58-60). + * This tool only reads and hashes bytes. It does not verify NVIDIA's signature + * (the key and format belong to the chip's boot ROM and ACR) and it never + * loads anything onto a device. + * + * ck_gsp_fw_inspect inspect FILE [--family NAME] [--expect-version V] + * Prints one "key value" line per fact (ELF identity, every section with + * offset and size, version string, build id, SHA-256 of the whole file, + * of .fwimage and of the selected signature section) and ends with + * "RESULT OK" (exit 0) or "RESULT REFUSED " (exit 2). Default + * family is gb10y; default expected version is 580.173.02. + * ck_gsp_fw_inspect selftest + * Builds a small valid container in memory, then mutates it one field at + * a time and checks each mutation is refused for the named reason. + * Exit 0 only when every case holds. + * Hosted C, no outside libraries (SHA-256 is the kernel's own, native/argus). + * Never part of an image. No Python. */ +#include +#include +#include +#include +#include + +#include "sha256.h" + +#define DEFAULT_FAMILY "gb10y" +#define DEFAULT_VERSION "580.173.02" +#define EM_RISCV 243u +#define ET_REL 1u +#define SHT_NOBITS 8u +#define SHDR_SIZE 64u +#define EHDR_SIZE 64u +#define MAX_SECTIONS 256u +#define MAX_REASON 160 + +typedef struct { + const char *name; + uint32_t type; + uint64_t offset; + uint64_t size; +} section; + +typedef struct { + char reason[MAX_REASON]; + size_t nsec; + section sec[MAX_SECTIONS]; + const section *fwimage, *fwversion, *buildid, *fwsig; + char version[64]; + char buildid_hex[129]; + char sig_name[64]; + uint8_t sha_file[32], sha_image[32], sha_sig[32]; +} report; + +static uint16_t rd16(const uint8_t *p) { return (uint16_t)(p[0] | ((uint16_t)p[1] << 8)); } +static uint32_t rd32(const uint8_t *p) { return (uint32_t)rd16(p) | ((uint32_t)rd16(p + 2) << 16); } +static uint64_t rd64(const uint8_t *p) { return (uint64_t)rd32(p) | ((uint64_t)rd32(p + 4) << 32); } +static void wr16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); } +static void wr32(uint8_t *p, uint32_t v) { wr16(p, (uint16_t)v); wr16(p + 2, (uint16_t)(v >> 16)); } +static void wr64(uint8_t *p, uint64_t v) { wr32(p, (uint32_t)v); wr32(p + 4, (uint32_t)(v >> 32)); } + +/* offset + size <= len without overflow (the driver uses portSafeAddU64). */ +static int in_bounds(uint64_t off, uint64_t size, uint64_t len) +{ + if (off > len) return 0; + return size <= len - off; +} + +static int refuse(report *r, const char *reason) +{ + snprintf(r->reason, sizeof r->reason, "%s", reason); + return -1; +} + +static void hex(const uint8_t *b, size_t n, char *out) +{ + static const char d[] = "0123456789abcdef"; + for (size_t i = 0; i < n; i++) { out[2 * i] = d[b[i] >> 4]; out[2 * i + 1] = d[b[i] & 15]; } + out[2 * n] = 0; +} + +/* Parse and check the container. 0 = every check passed, -1 = refused + * (r->reason names the first failed check). Pure: no I/O. */ +static int inspect_buf(const uint8_t *buf, uint64_t len, const char *family, + const char *expect_version, report *r) +{ + memset(r, 0, sizeof *r); + if (len < EHDR_SIZE) return refuse(r, "short header: file smaller than an ELF64 header"); + if (!(buf[0] == 0x7f && buf[1] == 'E' && buf[2] == 'L' && buf[3] == 'F')) + return refuse(r, "ELF magic missing"); + if (buf[4] != 2) return refuse(r, "not ELF64 (EI_CLASS)"); + if (buf[5] != 1) return refuse(r, "not little-endian (EI_DATA)"); + if (buf[6] != 1) return refuse(r, "EI_VERSION is not 1"); + if (rd16(buf + 16) != ET_REL) return refuse(r, "e_type is not ET_REL (the pinned container is a relocatable object)"); + if (rd16(buf + 18) != EM_RISCV) return refuse(r, "e_machine is not RISC-V (243)"); + uint64_t shoff = rd64(buf + 40); + uint16_t shentsize = rd16(buf + 58), shnum = rd16(buf + 60), shstrndx = rd16(buf + 62); + if (shentsize != SHDR_SIZE) return refuse(r, "e_shentsize is not 64"); + if (shnum == 0) return refuse(r, "e_shnum is 0: no section header table"); + if (shnum > MAX_SECTIONS) return refuse(r, "e_shnum above this tool's limit (256)"); + if (shstrndx >= shnum) return refuse(r, "e_shstrndx out of range"); + if (!in_bounds(shoff, (uint64_t)shnum * SHDR_SIZE, len)) + return refuse(r, "section header table outside the file"); + const uint8_t *strh = buf + shoff + (uint64_t)shstrndx * SHDR_SIZE; + uint64_t stroff = rd64(strh + 24), strsize = rd64(strh + 32); + if (strsize == 0 || !in_bounds(stroff, strsize, len)) + return refuse(r, "section string table outside the file or empty"); + if (buf[stroff + strsize - 1] != 0) return refuse(r, "section string table not NUL-terminated"); + const char *strtab = (const char *)buf + stroff; + for (uint16_t i = 0; i < shnum; i++) { + const uint8_t *sh = buf + shoff + (uint64_t)i * SHDR_SIZE; + uint32_t name = rd32(sh), type = rd32(sh + 4); + uint64_t off = rd64(sh + 24), size = rd64(sh + 32); + if (name >= strsize) return refuse(r, "section name index outside the string table"); + if (type != SHT_NOBITS && size > 0 && !in_bounds(off, size, len)) + return refuse(r, "section bounds: offset+size outside the file"); + section *s = &r->sec[r->nsec++]; + s->name = strtab + name; s->type = type; s->offset = off; s->size = size; + } + snprintf(r->sig_name, sizeof r->sig_name, ".fwsignature_%s", family); + for (size_t i = 0; i < r->nsec; i++) { + const section *s = &r->sec[i]; + if (!strcmp(s->name, ".fwimage")) r->fwimage = s; + else if (!strcmp(s->name, ".fwversion")) r->fwversion = s; + else if (!strcmp(s->name, ".note.gnu.build-id")) r->buildid = s; + else if (!strcmp(s->name, r->sig_name)) r->fwsig = s; + } + if (!r->fwimage) return refuse(r, "missing section .fwimage"); + if (r->fwimage->type == SHT_NOBITS || r->fwimage->size == 0) return refuse(r, ".fwimage empty"); + if (!r->fwversion) return refuse(r, "missing section .fwversion"); + if (!r->buildid) return refuse(r, "missing section .note.gnu.build-id"); + if (!r->fwsig) return refuse(r, "missing section: no signature section for this chip family"); + if (r->fwsig->type == SHT_NOBITS || r->fwsig->size == 0) return refuse(r, "signature section empty"); + /* .fwversion: the driver's three conditions, then equality with the pin. */ + const section *v = r->fwversion; + if (v->type == SHT_NOBITS || v->size == 0 || v->size >= 64) return refuse(r, "version section size not in 1..63"); + if (buf[v->offset + v->size - 1] != 0) return refuse(r, "version string not NUL-terminated"); + memcpy(r->version, buf + v->offset, (size_t)v->size); + if (expect_version && (v->size != strlen(expect_version) + 1 || strcmp(r->version, expect_version) != 0)) + return refuse(r, "version mismatch against the pinned driver version"); + /* build-id note: namesz, descsz, type, "GNU\0", desc. */ + const section *n = r->buildid; + if (n->type == SHT_NOBITS || n->size < 16) return refuse(r, "build-id note too short"); + const uint8_t *note = buf + n->offset; + uint32_t namesz = rd32(note), descsz = rd32(note + 4), ntype = rd32(note + 8); + if (namesz != 4 || memcmp(note + 12, "GNU", 4) != 0 || ntype != 3) + return refuse(r, "build-id note is not an NT_GNU_BUILD_ID note"); + if (descsz == 0 || descsz > 64 || 16 + (uint64_t)descsz > n->size) + return refuse(r, "build-id note descriptor size out of range"); + hex(note + 16, descsz, r->buildid_hex); + sha256_hash(buf, (size_t)len, r->sha_file); + sha256_hash(buf + r->fwimage->offset, (size_t)r->fwimage->size, r->sha_image); + sha256_hash(buf + r->fwsig->offset, (size_t)r->fwsig->size, r->sha_sig); + return 0; +} + +static void print_report(const report *r, int ok) +{ + char h[65]; + for (size_t i = 0; i < r->nsec; i++) + printf("section %zu %s type=%u offset=%llu size=%llu\n", i, r->sec[i].name[0] ? r->sec[i].name : "(null)", + (unsigned)r->sec[i].type, (unsigned long long)r->sec[i].offset, (unsigned long long)r->sec[i].size); + if (ok) { + printf("fwversion %s\n", r->version); + printf("build_id %s\n", r->buildid_hex); + printf("signature_section %s size=%llu\n", r->sig_name, (unsigned long long)r->fwsig->size); + hex(r->sha_file, 32, h); printf("sha256_file %s\n", h); + hex(r->sha_image, 32, h); printf("sha256_fwimage %s size=%llu\n", h, (unsigned long long)r->fwimage->size); + hex(r->sha_sig, 32, h); printf("sha256_signature %s\n", h); + printf("RESULT OK\n"); + } else { + printf("RESULT REFUSED %s\n", r->reason); + } +} + +static int cmd_inspect(int argc, char **argv) +{ + const char *path = NULL, *family = DEFAULT_FAMILY, *expect = DEFAULT_VERSION; + for (int i = 0; i < argc; i++) { + if (!strcmp(argv[i], "--family") && i + 1 < argc) family = argv[++i]; + else if (!strcmp(argv[i], "--expect-version") && i + 1 < argc) expect = argv[++i]; + else if (!path) path = argv[i]; + else { fprintf(stderr, "unexpected argument %s\n", argv[i]); return 1; } + } + if (!path) { fprintf(stderr, "usage: ck_gsp_fw_inspect inspect FILE [--family NAME] [--expect-version V]\n"); return 1; } + FILE *f = fopen(path, "rb"); + if (!f) { fprintf(stderr, "open %s: %s\n", path, strerror(errno)); return 1; } + if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return 1; } + long sz = ftell(f); + if (sz < 0) { fclose(f); return 1; } + rewind(f); + uint8_t *buf = malloc(sz ? (size_t)sz : 1); + if (!buf) { fclose(f); return 1; } + if (fread(buf, 1, (size_t)sz, f) != (size_t)sz) { fprintf(stderr, "short read\n"); free(buf); fclose(f); return 1; } + fclose(f); + printf("file %s bytes=%ld family=%s expect_version=%s\n", path, sz, family, expect); + report *r = calloc(1, sizeof *r); + if (!r) { free(buf); return 1; } + int rc = inspect_buf(buf, (uint64_t)sz, family, expect, r); + print_report(r, rc == 0); + free(r); free(buf); + return rc == 0 ? 0 : 2; +} + +/* ---- self-test fixture: a minimal valid container ---------------------- */ +enum { FX_NULL, FX_IMAGE, FX_NOTE, FX_VERSION, FX_SIG, FX_STRTAB, FX_NSEC }; +#define FX_IMAGE_OFF 64u +#define FX_IMAGE_LEN 16u +#define FX_NOTE_OFF 80u +#define FX_NOTE_LEN 36u /* 12 + "GNU\0" + 20-byte id */ +#define FX_VER_OFF 116u +#define FX_VER_LEN 11u /* "580.173.02\0" */ +#define FX_SIG_OFF 128u +#define FX_SIG_LEN 32u +#define FX_STR_OFF 160u +#define FX_SHOFF 256u +#define FX_LEN (FX_SHOFF + FX_NSEC * SHDR_SIZE) + +static const char fx_strtab[] = "\0.fwimage\0.note.gnu.build-id\0.fwversion\0.fwsignature_gb10y\0.shstrtab\0"; +#define FX_STR_LEN (sizeof fx_strtab) + +static void fx_shdr(uint8_t *b, unsigned idx, uint32_t name, uint32_t type, uint64_t off, uint64_t size) +{ + uint8_t *sh = b + FX_SHOFF + idx * SHDR_SIZE; + memset(sh, 0, SHDR_SIZE); + wr32(sh, name); wr32(sh + 4, type); wr64(sh + 24, off); wr64(sh + 32, size); +} + +static void fx_build(uint8_t *b) +{ + memset(b, 0, FX_LEN); + b[0] = 0x7f; b[1] = 'E'; b[2] = 'L'; b[3] = 'F'; b[4] = 2; b[5] = 1; b[6] = 1; + wr16(b + 16, ET_REL); wr16(b + 18, EM_RISCV); wr32(b + 20, 1); + wr64(b + 40, FX_SHOFF); wr16(b + 52, EHDR_SIZE); wr16(b + 58, SHDR_SIZE); + wr16(b + 60, FX_NSEC); wr16(b + 62, FX_STRTAB); + for (unsigned i = 0; i < FX_IMAGE_LEN; i++) b[FX_IMAGE_OFF + i] = (uint8_t)(0xa0 + i); + wr32(b + FX_NOTE_OFF, 4); wr32(b + FX_NOTE_OFF + 4, 20); wr32(b + FX_NOTE_OFF + 8, 3); + memcpy(b + FX_NOTE_OFF + 12, "GNU", 4); + for (unsigned i = 0; i < 20; i++) b[FX_NOTE_OFF + 16 + i] = (uint8_t)i; + memcpy(b + FX_VER_OFF, DEFAULT_VERSION, FX_VER_LEN); + for (unsigned i = 0; i < FX_SIG_LEN; i++) b[FX_SIG_OFF + i] = (uint8_t)(0x50 + i); + memcpy(b + FX_STR_OFF, fx_strtab, FX_STR_LEN); + fx_shdr(b, FX_NULL, 0, 0, 0, 0); + fx_shdr(b, FX_IMAGE, 1, 1, FX_IMAGE_OFF, FX_IMAGE_LEN); + fx_shdr(b, FX_NOTE, 10, 7, FX_NOTE_OFF, FX_NOTE_LEN); + fx_shdr(b, FX_VERSION, 29, 1, FX_VER_OFF, FX_VER_LEN); + fx_shdr(b, FX_SIG, 40, 1, FX_SIG_OFF, FX_SIG_LEN); + fx_shdr(b, FX_STRTAB, 59, 3, FX_STR_OFF, FX_STR_LEN); +} + +typedef void (*mutate_fn)(uint8_t *b); +static void m_magic(uint8_t *b) { b[1] = 'X'; } +static void m_class32(uint8_t *b) { b[4] = 1; } +static void m_bigendian(uint8_t *b) { b[5] = 2; } +static void m_type(uint8_t *b) { wr16(b + 16, 2); } +static void m_machine(uint8_t *b) { wr16(b + 18, 62); } +static void m_shentsize(uint8_t *b) { wr16(b + 58, 40); } +static void m_shnum0(uint8_t *b) { wr16(b + 60, 0); } +static void m_shstrndx(uint8_t *b) { wr16(b + 62, FX_NSEC); } +static void m_shoff(uint8_t *b) { wr64(b + 40, FX_LEN - 8); } +static void m_strtab_nul(uint8_t *b) { b[FX_STR_OFF + FX_STR_LEN - 1] = 'x'; } +static void m_name_idx(uint8_t *b) { wr32(b + FX_SHOFF + FX_SIG * SHDR_SIZE, 100000); } +static void m_image_bounds(uint8_t *b) { wr64(b + FX_SHOFF + FX_IMAGE * SHDR_SIZE + 32, FX_LEN); } +static void m_overflow(uint8_t *b) { wr64(b + FX_SHOFF + FX_IMAGE * SHDR_SIZE + 24, UINT64_MAX - 1); } +static void m_image_empty(uint8_t *b) { wr64(b + FX_SHOFF + FX_IMAGE * SHDR_SIZE + 32, 0); } +static void m_version_nul(uint8_t *b) { wr64(b + FX_SHOFF + FX_VERSION * SHDR_SIZE + 32, FX_VER_LEN - 1); } +static void m_version_text(uint8_t *b) { b[FX_VER_OFF + FX_VER_LEN - 2] = '3'; } +static void m_version_long(uint8_t *b) { wr64(b + FX_SHOFF + FX_VERSION * SHDR_SIZE + 32, 64); } +static void m_sig_empty(uint8_t *b) { wr64(b + FX_SHOFF + FX_SIG * SHDR_SIZE + 32, 0); } +static void m_sig_renamed(uint8_t *b) { b[FX_STR_OFF + 40 + 17] = 'x'; } /* .fwsignature_gb10x */ +static void m_note_type(uint8_t *b) { wr32(b + FX_NOTE_OFF + 8, 1); } +static void m_note_desc(uint8_t *b) { wr32(b + FX_NOTE_OFF + 4, 40); } +static void m_note_short(uint8_t *b) { wr64(b + FX_SHOFF + FX_NOTE * SHDR_SIZE + 32, 8); } + +static const struct { const char *what; mutate_fn fn; const char *reason; } cases[] = { + { "bad ELF magic", m_magic, "ELF magic" }, + { "32-bit class", m_class32, "not ELF64" }, + { "big-endian", m_bigendian, "little-endian" }, + { "e_type EXEC", m_type, "e_type" }, + { "e_machine x86-64", m_machine, "e_machine" }, + { "e_shentsize 40", m_shentsize, "e_shentsize" }, + { "e_shnum 0", m_shnum0, "e_shnum" }, + { "e_shstrndx == shnum", m_shstrndx, "e_shstrndx" }, + { "section header table past end", m_shoff, "section header table" }, + { "string table not NUL-terminated", m_strtab_nul, "string table not NUL" }, + { "section name index past string table", m_name_idx, "section name index" }, + { ".fwimage size past end of file", m_image_bounds, "section bounds" }, + { ".fwimage offset near UINT64_MAX (overflow)", m_overflow, "section bounds" }, + { ".fwimage empty", m_image_empty, ".fwimage empty" }, + { ".fwversion without NUL", m_version_nul, "NUL-terminated" }, + { ".fwversion text differs from pin", m_version_text, "version mismatch" }, + { ".fwversion 64 bytes", m_version_long, "version section size" }, + { "signature section empty", m_sig_empty, "signature section empty" }, + { "signature section for another family only", m_sig_renamed, "no signature section" }, + { "build-id note wrong type", m_note_type, "NT_GNU_BUILD_ID" }, + { "build-id descriptor past section", m_note_desc, "descriptor size" }, + { "build-id note truncated", m_note_short, "too short" }, +}; + +static int cmd_selftest(void) +{ + uint8_t *b = malloc(FX_LEN); + report *r = calloc(1, sizeof *r); + if (!b || !r) return 1; + int fail = 0; + fx_build(b); + if (inspect_buf(b, FX_LEN, "gb10y", DEFAULT_VERSION, r) != 0) { + printf("FAIL valid fixture refused: %s\n", r->reason); fail = 1; + } else if (strcmp(r->version, DEFAULT_VERSION) != 0 || strncmp(r->buildid_hex, "000102030405", 12) != 0) { + printf("FAIL valid fixture parsed wrong (version %s build id %s)\n", r->version, r->buildid_hex); fail = 1; + } else { + printf("PASS valid fixture accepted (version %s, build id %s)\n", r->version, r->buildid_hex); + } + /* Other family missing on the valid fixture: refused, not silently accepted. */ + if (inspect_buf(b, FX_LEN, "gb10x", DEFAULT_VERSION, r) == 0 || !strstr(r->reason, "no signature section")) { + printf("FAIL family gb10x should be refused on a gb10y-only fixture\n"); fail = 1; + } else printf("PASS family gb10x refused: %s\n", r->reason); + /* Truncated file: every length below FX_LEN must be refused, never crash. */ + int trunc_ok = 1; + for (uint64_t l = 0; l < FX_LEN; l++) + if (inspect_buf(b, l, "gb10y", DEFAULT_VERSION, r) == 0) { trunc_ok = 0; break; } + printf("%s every truncation 0..%u refused\n", trunc_ok ? "PASS" : "FAIL", (unsigned)(FX_LEN - 1)); + if (!trunc_ok) fail = 1; + size_t n = sizeof cases / sizeof cases[0]; + for (size_t i = 0; i < n; i++) { + fx_build(b); + cases[i].fn(b); + int rc = inspect_buf(b, FX_LEN, "gb10y", DEFAULT_VERSION, r); + if (rc == 0) { printf("FAIL %s: accepted\n", cases[i].what); fail = 1; } + else if (!strstr(r->reason, cases[i].reason)) { + printf("FAIL %s: refused for the wrong reason: %s\n", cases[i].what, r->reason); fail = 1; + } else printf("PASS %s: %s\n", cases[i].what, r->reason); + } + printf("CK_GSP_FW_INSPECT_SELFTEST: %s (%zu mutations)\n", fail ? "FAIL" : "PASS", n); + free(b); free(r); + return fail; +} + +int main(int argc, char **argv) +{ + if (argc >= 2 && !strcmp(argv[1], "inspect")) return cmd_inspect(argc - 2, argv + 2); + if (argc == 2 && !strcmp(argv[1], "selftest")) return cmd_selftest(); + fprintf(stderr, "usage: ck_gsp_fw_inspect inspect FILE [--family NAME] [--expect-version V]\n" + " ck_gsp_fw_inspect selftest\n"); + return 1; +}