interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

GitHub aien-dev/aienux#319 (code only)

closed derived 365740955dc4f171 #319

Imported from GitHub aien-dev/aienux#319 (code only)

GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed).

Base fcb3113c3e91d709fab10d408a3bdf8fb8e7d8ee · Head 22f8f8a5a34c7a6baf2b18849fd91e772737cc2c

Apply it

git checkout -b patch-365740955dc4f171 fcb3113c3e91d709fab10d408a3bdf8fb8e7d8ee
curl -fsSL https://interplane.io/drakestapleton/aienux/patches/365740955dc4f171.patch | git am

Raw patch (mbox) · Diff

Provenance

This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record:

github-import 1
aien-dev/aienux
319
22f8f8a5a34c7a6baf2b18849fd91e772737cc2c
{"kind":"github-import","owner":"aien-dev","repo":"aienux","number":319,"url":"https://github.com/aien-dev/aienux/pull/319","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:12Z","metadata":"unavailable","state_derived":true}

Changes

diff --git a/.crumb b/.crumb
index 5fc298a6..1e13cc87 100644
--- a/.crumb
+++ b/.crumb
@@ -27,33 +27,33 @@
           "purpose": "Directory crates. Purpose not yet described by a human."
         },
         {
-          "digest": "7e7934c11eb156bf4249648ef757d6135570877703a1ccab214c07cb5a57e235",
-          "last_commit": "f47f6c7",
+          "digest": "67c7dafea9dd11fce8a30e2758fc9c0553d5e75c442bc6c2c22f134a090a5008",
+          "last_commit": "e09825a",
           "name": "docs",
           "purpose": "Directory docs. Purpose not yet described by a human."
         },
         {
-          "digest": "362c8c616872caca2069f3e748db0d8fb7c807bff2771679c4973481a15901f2",
-          "last_commit": "f47f6c7",
+          "digest": "00b3df5ff408cf07d222d4ebd836026a58a12532ff116b765594246a09a901bc",
+          "last_commit": "e09825a",
           "name": "native",
           "purpose": "Directory native. Purpose not yet described by a human."
         },
         {
-          "digest": "8f2ba6515d449096eccaa96e1d710d5556e36425d1b82eb8ef096d85200059f9",
-          "last_commit": "22eb360",
+          "digest": "348804e26984e873db9914d2a3ebfcf6c9ea02619247d78809999011b354e048",
+          "last_commit": "e09825a",
           "name": "scripts",
           "purpose": "Directory scripts. Purpose not yet described by a human."
         }
       ],
-      "children_root": "sha256:49ee7a6b7ab0a6a96d0329f00ef1839ff64e47aba7d5717ea27f8bbc865ace9f",
-      "compiled_at": "2026-10-12T11:44:52Z",
+      "children_root": "sha256:5e6b18b2f6fa0f85281593d5f46a2a039850a7a3bca50f3f06ad88dc557626f9",
+      "compiled_at": "2026-10-12T12:21:27Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:1ee2a15770e45746c15c5cdea47206165cf26eda84c4a39d1746c1096d89e9cc",
+      "digest": "sha256:815433c9c8cef1effe798105427e5d1f18af2014f0d2265838d91ac870214cf6",
       "evidence_files": 155,
       "evidence_root": "sha256:d59c0cef4e7dd5172923754a95b411ca54b01f35baca4587f792f1b259f1eaf7",
-      "files": 3087,
-      "generated_at_commit": "f47f6c76286d7c85b9d038afc730619c54266a9f",
-      "source_tree": "sha256:8e951d5463b4dde2502e3e2c7324bae7586f14fb04ef2355bc493b66ec6120e1"
+      "files": 3091,
+      "generated_at_commit": "e09825afddde294659ca4a06354990e7ff530b62",
+      "source_tree": "sha256:6ffa7e6c4e272b46b100fa2f8b698c7d25588870c03b2f320d513ae2297e926a"
     },
     "owner_lane": "",
     "provenance": {
diff --git a/.github/workflows/workflow-files.yml b/.github/workflows/workflow-files.yml
new file mode 100644
index 00000000..00cf3702
--- /dev/null
+++ b/.github/workflows/workflow-files.yml
@@ -0,0 +1,27 @@
+# Always-on guard: a workflow file GitHub cannot parse yields a run with zero
+# jobs that never shows in the PR checks, so the job it carries silently stops
+# running (2026-10-12: ck-kernel.yml, four hours). This job has no path filter
+# and is a required check on main, so that silence becomes a red check.
+name: Workflow files accepted
+
+on:
+  pull_request:
+  push:
+    branches: [main]
+
+permissions:
+  actions: read
+  contents: read
+
+jobs:
+  accepted:
+    name: workflow files accepted
+    runs-on: ubuntu-latest
+    timeout-minutes: 5
+    steps:
+      - uses: actions/checkout@v4
+      - name: Every workflow run on this commit has jobs
+        env:
+          GH_TOKEN: ${{ github.token }}
+          SHA: ${{ github.event.pull_request.head.sha || github.sha }}
+        run: sh scripts/workflow_files_check.sh "$SHA"
diff --git a/docs/.crumb b/docs/.crumb
index f1139326..9a9abe12 100644
--- a/docs/.crumb
+++ b/docs/.crumb
@@ -30,14 +30,14 @@
         }
       ],
       "children_root": "sha256:baea130f2c52779b57d90139beb66ac01b21ab499d113471a334be5500f3e369",
-      "compiled_at": "2026-10-12T11:39:56Z",
+      "compiled_at": "2026-10-12T12:21:27Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:7e7934c11eb156bf4249648ef757d6135570877703a1ccab214c07cb5a57e235",
+      "digest": "sha256:67c7dafea9dd11fce8a30e2758fc9c0553d5e75c442bc6c2c22f134a090a5008",
       "evidence_files": 0,
       "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
       "files": 70,
-      "generated_at_commit": "22eb3603074e5df77df9914dbd301d67122564b5",
-      "source_tree": "sha256:5641e25791a12e563582b9a529277f203c5ac192ffce39e8720ed47adc0edc19"
+      "generated_at_commit": "e09825afddde294659ca4a06354990e7ff530b62",
+      "source_tree": "sha256:479955d7d585b02e7323f4dbdbab0ac2ad395207ce4475eb4138b985ba7cb9ad"
     },
     "owner_lane": "",
     "provenance": {
diff --git a/docs/GB10_FIRMWARE_INVENTORY.md b/docs/GB10_FIRMWARE_INVENTORY.md
index ac12670a..370d15fe 100644
--- a/docs/GB10_FIRMWARE_INVENTORY.md
+++ b/docs/GB10_FIRMWARE_INVENTORY.md
@@ -35,6 +35,12 @@ the driver on the box; fetched read-only into a scratch directory, not vendored)
   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.
+- Two more hosted tools, same rule (nothing is ever sent to the chip):
+  `ck_gsp_cot_encode` builds and checks the exact message the driver sends to
+  SEC2 (section 10), and `ck_gsp_bindata_extract` reads the eight GB10B
+  firmware arrays out of the public driver source with our own inflater and
+  hash code, cross-checks every size, record and digest, and writes them as
+  files (section 11). `make -C native/kernel test` runs all three self-tests.
 - 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
@@ -93,15 +99,35 @@ The driver embeds these as C byte arrays ("bindata") with an MIT licence header
 `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 |
+| Archive label | Original file name (NVIDIA internal) | Bytes | Stored as | sha256 of the bytes [OBSERVED] |
+|---|---|---|---|---|
+| GspRmBoot_GB10B UCODE_IMAGE_PROD | kernel/inc/gspcc/bin/g_gsp_gb10b_dyn_prd_image.bin | 196,608 | raw | 427a691d8453f02548d395f40ac900eb0d5b8ea864e8f14f958eef890ba400a2 |
+| GspRmBoot_GB10B UCODE_DESC_PROD | kernel/inc/gspcc/bin/g_gsp_gb10b_dyn_prd_desc.bin | 84 (RM_RISCV_UCODE_DESC) | deflate, 28 bytes | 8371a29f1c68da89b73bdc87af477c37f0b4e05939c58379d0e3d29efc5575b5 |
+| GspRmBoot_GB10B UCODE_IMAGE_DBG | kernel/inc/gspcc/bin/g_gsp_gb10b_dyn_dbg_image.bin | 196,608 | raw | 709b88a88fc7ae9bd56b010bcc0a8269d4d40b3c0454aa5b366e8d562d83a404 |
+| GspRmBoot_GB10B UCODE_DESC_DBG | kernel/inc/gspcc/bin/g_gsp_gb10b_dyn_dbg_desc.bin | 84 | deflate, 28 bytes | 8371a29f... (byte-identical to DESC_PROD) |
+| GspRmFmcGfwProdSigned_GB10B UCODE_IMAGE | kernel/inc/gspcc/bin/g_gsp_gb10b_dyn_prd_gfw_image.bin | 195,912 | raw | b13ead4b9a6b8a0d268f3094cc4f4521d6be8a9a525edbc9a8f3f397c86f1f2e |
+| GspRmFmcGfwProdSigned_GB10B UCODE_HASH | g_gsp_gb10b_dyn_prd_gfw_hash.bin | 48 (SHA-384) | raw | f0389f3a9ce3bb6093e80bfc924bbe2a4ece0199b080a37f64bc1fef63f7500c |
+| GspRmFmcGfwProdSigned_GB10B UCODE_SIG | g_gsp_gb10b_dyn_prd_gfw_sig.bin | 384 | raw | 9db429257a18e7c5c1a3df185001c671e21bec4e94c719a574d2ce72e2e07272 |
+| GspRmFmcGfwProdSigned_GB10B UCODE_PKEY | g_gsp_gb10b_dyn_prd_gfw_pub_key.bin | 384 | raw | e9bbc669c27ad8827b5bbc2b2f52fdb47718659a651088869a5f3b11d2df7816 |
+
+Digests measured 2026-10-12 by `ck_gsp_bindata_extract` over the two files
+fetched from the 580.173.02 tag (raw.githubusercontent.com, path
+`src/nvidia/generated/<file>`); the 580.173.02 Makefile pin
+(`make -C native/kernel gsp-bindata-test`) checks the two image digests and
+the decoded descriptor on every run. `UCODE_HASH` equals SHA-384 of
+`UCODE_IMAGE` [OBSERVED, checked by the tool].
+
+Array storage format [SOURCE]: each array is a `BINDATA_STORAGE_PVT` record
+(uncompressed size, compressed size, data pointer, bCompressed,
+bFileOverride, bReferencedDuringLoad) and an archive table
+(`BINDATA_ARCHIVE` with `entryNum` and label/storage pairs) [bin_data_pvt.h,
+bin_data.h]. A compressed array is a gzip member without the 10-byte gzip
+header: raw DEFLATE, then CRC-32 and ISIZE, little endian. The driver inflates
+it with its own zlib-style decoder (`utilGzAllocate`/`utilGzGetData`,
+bin_data.c:36-45,96); the nouveau extractor prepends exactly that header
+before `gzip.decompress` [nouveau/extract-firmware-nouveau.py:107-108].
+The 28-byte descriptor is 20 bytes of fixed-Huffman DEFLATE plus the two
+trailers [OBSERVED]; our decoder verifies both.
 
 Source files: `src/nvidia/generated/g_bindata_kgspGetBinArchiveGspRmBoot_GB10B.c`
 (2,466,261 bytes of C) and
@@ -111,9 +137,27 @@ 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)]
+gb10b [nouveau/extract-firmware-nouveau.py:655-662,775-781]. DONE
+2026-10-12: `ck_gsp_bindata_extract` (section 11) is that step, written
+against the generated-file grammar and the storage records, not against the
+nouveau script.
+
+Descriptor decode [SOURCE rmRiscvUcode.h:36-87, 21 u32 fields; OBSERVED
+values]: version 5; bootloader/riscvElf/swbrom offsets and sizes 0;
+manifest at 0x0 size 0xa00; monitorData at 0xa00 size 0xa600; monitorCode at
+0xb000 size 0x24d00 (ends at 0x2fd00, inside the 196,608-byte image);
+bIsMonitorEnabled 1; appVersion 0; fbReservedSize 0; bSignedAsCode 0. The
+driver shifts these offsets by `RISCV_BR_ADDR_ALIGNMENT` = 8 bits when it
+programs them [kernel_gsp_gh100.c:47, kernel_gsp_ga102.c:44], so the tool
+refuses any region that is not 256-byte aligned.
+
+Which archive the driver picks for GB10B [SOURCE]: the HAL dispatch for
+`kgspGetGspRmBootUcodeStorage` lands on the GB100 variant
+[kernel_gsp_gb100.c:115-149], which outside confidential computing calls the
+GA102 variant [kernel_gsp_ga102.c:101-120]: the `GspRmBoot` archive's
+`UCODE_IMAGE_PROD` and `UCODE_DESC_PROD` (the DBG pair only on a debug-fused
+part). The `GspRmFmcGfwProdSigned` quartet goes into the SEC2 chain-of-trust
+payload (section 10).
 
 ### 3.4 Pieces that are NOT on the host side at all
 
@@ -247,13 +291,13 @@ code into AIENOS would require carrying the MIT notice (none was copied here).
 
 - 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.
+  not need the machine: (a) DONE 2026-10-12: `ck_gsp_bindata_extract`
+  (section 11) writes the eight arrays as files with digests, own DEFLATE and
+  SHA-384, `make -C native/kernel gsp-bindata-test`; (b) DONE 2026-10-12: the encoder, decoder
+  and refusal tests for `NVDM_PAYLOAD_COT`, the MCTP/NVDM headers, the EMEM
+  packet and `GSP_FMC_BOOT_PARAMS` are `ck_gsp_cot_encode` (section 10);
+  (c) remaining: an encoder for `GspFwWprMeta` (256 bytes, layout known, the
+  GH100 populate values not yet traced) and the libos init-argument block.
 - 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
@@ -261,11 +305,17 @@ code into AIENOS would require carrying the MIT notice (none was copied here).
 
 ## 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.
+1. RESOLVED 2026-10-12 [SOURCE]: the `NVDM_PAYLOAD_COT` layout is 860 packed
+   bytes, GB10B uses version 1, signature and key sizes 384, hash 48; the
+   whole wire format is in section 10 and checked by `ck_gsp_cot_encode`.
+2. [PARTLY RESOLVED 2026-10-12] `GSP_FMC_BOOT_PARAMS` (88 bytes, section 10)
+   and the `GspFwWprMeta` struct (256 bytes, `gsp/gsp_fw_wpr_meta.h`, magic
+   `0xdc3aae21371a60b3`, revision 1; GB10B zeroes `vgaWorkspaceSize`,
+   `pmuReservedSize` and `nonWprHeapSize`, kernel_gsp_gb10b.c:32-55) are
+   known. [UNKNOWN] the values `kgspPopulateWprMeta_GH100` computes (radix3
+   ELF placement, heap sizes, WPR carveout) and the libos init-argument block
+   (`gsp_init_args.h` gives `GSP_ARGUMENTS_CACHED`; the libos block itself not
+   traced).
 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.
@@ -276,11 +326,19 @@ code into AIENOS would require carrying the MIT notice (none was copied here).
    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.
+6. [RESOLVED on the host side 2026-10-12, SOURCE; consumer side INFERRED
+   (medium)] `GspRmFmcGfwProdSigned_GB10B` (image, hash, signature, public
+   key) is what SEC2 receives in the chain-of-trust payload (section 10).
+   `GspRmBoot_GB10B` (image plus RM_RISCV_UCODE_DESC) is what
+   `kgspPopulateWprMeta_GH100` describes to the FMC as the "GSP-FMC ucode"
+   bootloader: `sizeOfBootloader`, `sysmemAddrOfBootloader`,
+   `bootloaderCodeOffset`/`DataOffset`/`ManifestOffset` come from that image
+   and the descriptor's monitorCode/monitorData/manifest offsets
+   [kernel_gsp_gh100.c:295-335]; the direct boot-ROM path programs the same
+   three regions into `NV_PGSP_FALCON_BCR_DMAADDR_*` [kernel_gsp_gh100.c:
+   `_kgspBootstrapGspFmc_GH100`], which GB10B does not take (section 11).
+   Who reads the WprMeta bootloader fields on the SEC2 path (FMC or ACR) runs
+   inside NVIDIA code and is not readable.
 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.
@@ -293,9 +351,166 @@ 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
+(metadata comments only), README.md, COPYING, the firmware copyright file;
+for section 10 also kern_fsp_cot_payload.h, fsp_nvdm_format.h,
+fsp_mctp_format.h, fsp_emem_channels.h, kernel_sec2.c (`ksec2SendMessage`,
+`ksec2PollForCanSend`), g_kernel_sec2_nvoc.c/.h (version, sizes, SEID),
+gspifpub.h, gsp_init_args.h, gsp_fw_wpr_meta.h, kernel_gsp_gh100.c
+(`kgspSetupGspFmcArgs`), and published/blackwell/gb10b dev_sec_pri.h,
+dev_gsp.h, dev_falcon_v4.h; for sections 3.3 and 11 also rmRiscvUcode.h,
+bin_data.c, bin_data.h, bin_data_pvt.h, kernel_gsp_ga102.c,
+nouveau/extract-firmware-nouveau.py (lines 82-114 only), kernel_gsp_gh100.c
+(`kgspBootstrap_GH100`, `_kgspLockdownReleasedOrFmcError`,
+`kgspPopulateWprMeta_GH100`, `_kgspBootstrapGspFmc_GH100`), g_kern_fsp_nvoc.c
+(the `PDB_PROP_KFSP_IS_MISSING` chip list), and the full bindata record
+tables of both GB10B files. Not read: the GSP RPC layer, the libos
+init-argument block, 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).
+
+## 10. The SEC2 chain-of-trust message, byte for byte [SOURCE]
+
+Everything in this section is read from open-gpu-kernel-modules tag
+580.173.02 and mirrored by `native/kernel/tools/ck_gsp_cot_encode.c`
+(`make -C native/kernel gsp-cot-test`, part of `make test`). Nothing here was
+sent to the device.
+
+Transport. NVDM (NVIDIA Data Model) over MCTP over SEC2 EMEM channel 0,
+1024 bytes (`fsp/fsp_emem_channels.h:9-11`; `kernel_sec2_gb10b.c:36-41`,
+`:322-333`). A packet is two header dwords then the payload
+(`kernel_sec2.c:50-52`, `:305-325`):
+
+| dword | field | value for the boot message | source |
+|---|---|---|---|
+| 0 | MCTP transport header: SOM bit 31, EOM bit 30, SEQ 29:28, TO 27, TAG 26:24, SEID 23:16, DEID 15:8, RSVD 7:4, VERSION 3:0 | `0xC0000000` (SOM=1, EOM=1, SEQ=0, SEID=0; the driver sets only those four) | `fsp_mctp_format.h:8-16`; `kernel_sec2.c:319-321`; SEID 0 for GB10B `g_kernel_sec2_nvoc.c:879-888`, `g_kernel_sec2_nvoc.h:907-909` |
+| 1 | NVDM header: TYPE 6:0 = 0x7e (vendor PCI), IC 7 = 0, VENDOR_ID 23:8 = 0x10de, NVDM_TYPE 31:24 | `0x1410DE7E` (NVDM_TYPE_COT = 0x14) | `fsp_mctp_format.h:17-22`; `fsp_nvdm_format.h:14`; `kernel_sec2_gb10b.c:391-402` |
+
+A message longer than 1016 bytes would continue in packets that carry only
+the MCTP dword with SOM=0 and SEQ = (n mod 4) (`kernel_sec2.c:330-357`); the
+boot message is 860 bytes, so it is always one packet, and the driver sends
+only this one command on the channel (`kernel_sec2_gb10b.c:719-722`).
+
+Payload `NVDM_PAYLOAD_COT`, `#pragma pack(1)`, 860 bytes
+(`src/nvidia/inc/kernel/gpu/fsp/kern_fsp_cot_payload.h:12-29`):
+
+| offset | size | field | GB10B value | source |
+|---|---|---|---|---|
+| 0 | 2 | version | 1 | `g_kernel_sec2_nvoc.c:478-482` |
+| 2 | 2 | size | 860 (`sizeof`) | `kernel_sec2_gb10b.c:580-581` |
+| 4 | 8 | gspFmcSysmemOffset | physical address of the GSP-FMC image in system memory | `:507` |
+| 12 | 8 | frtsSysmemOffset | 0 (never set) | `:578` zero-fill |
+| 20 | 4 | frtsSysmemSize | the GSP-FMC image size (195,912), sic | `:508` |
+| 24 | 8 | frtsVidmemOffset | 0 | `:578` |
+| 32 | 4 | frtsVidmemSize | 0 | `:578` |
+| 36 | 48 | hash384 | `UCODE_HASH` array | `:514` |
+| 84 | 384 | publicKey | `UCODE_PKEY` array | `:526-527`; size `g_kernel_sec2_nvoc.c:475-476` |
+| 468 | 384 | signature | `UCODE_SIG` array | `:519-520`; size `g_kernel_sec2_nvoc.c:472-473` |
+| 852 | 8 | gspBootArgsSysmemOffset | physical address of `GSP_FMC_BOOT_PARAMS` | `:532`, `:241-258` |
+
+The packet is therefore 868 bytes. It is written at EMEM offset 0 with the
+write auto-increment bit, one dword at a time, then QUEUE_HEAD(0) = 0 and
+QUEUE_TAIL(0) = 864 (padded size minus one dword) (`kernel_sec2_gb10b.c:
+700-732`, `:108-152`, `:160-210`). The channel may be written only while
+HEAD == TAIL (`:344-356`, polled `kernel_sec2.c:605-640`); the command counts
+as handled once FALCON_MAILBOX0 no longer reads `0xc001cafe` (`:285-297`);
+before the boot the driver also waits until the GSP falcon's HWCFG2 register
+reads non-zero and not the `0xBADF41xx` "target locked" pattern (`:262-281`).
+Register addresses for GB10B: `published/blackwell/gb10b/dev_sec_pri.h`
+(MAILBOX0/1, QUEUE_HEAD/TAIL(i), EMEMC(i), EMEMD(i)), `dev_gsp.h` (NV_PGSP
+base), `dev_falcon_v4.h` (HWCFG2); the tool does not use them.
+
+Boot arguments the payload points at: `GSP_FMC_BOOT_PARAMS`
+(`src/nvidia/arch/nvalloc/common/inc/gsp/gspifpub.h`), natural C layout, 88
+bytes on the LP64 ABI the driver and this host share (the self-test checks the
+offsets against the compiler); filled by `kgspSetupGspFmcArgs_GH100`
+(`kernel_gsp_gh100.c:415-470`), allocated 4 KiB aligned (`:164`):
+
+| offset | size | field | fresh-boot value |
+|---|---|---|---|
+| 0 | 4 | initParams.regkeys | 0 unless CC or NVLink proxy keys |
+| 8 | 4 | bootGspRmParams.target | DMA target of the descriptor (0 FB, 1 coherent sysmem, 2 non-coherent sysmem) |
+| 12 | 4 | bootGspRmParams.gspRmDescSize | 256 (`sizeof GspFwWprMeta`) |
+| 16 | 8 | bootGspRmParams.gspRmDescOffset | physical address of the `GspFwWprMeta` |
+| 24 | 8 | bootGspRmParams.wprCarveoutOffset | not set by the driver (0) |
+| 32 | 4 | bootGspRmParams.wprCarveoutSize | not set (0) |
+| 36 | 1 | bootGspRmParams.bIsGspRmBoot | 1 |
+| 40 | 4 | gspRmParams.target | DMA target of the libos init arguments |
+| 48 | 8 | gspRmParams.bootArgsOffset | physical address of the libos init arguments |
+| 56 | 24 | gspSpdmParams | only with SPDM (assumed off) |
+| 80 | 8 | gspRmMemParams.flushSysmemAddrVal{Lo,Hi} | only with CC (0) |
+
+INFERRED, enforced by the tool but not a driver check: the image address is
+4 KiB aligned (the driver maps `NV_ALIGN_UP(size, 0x1000)` bytes of
+page-granular system memory, `kernel_sec2_gb10b.c:489-495`) and every
+system address fits the 41-bit DMA window `/proc/driver/nvidia/gpus` reports
+for this GPU (section 2; `--dma-bits` overrides).
+
+Evidence 2026-10-12 on the box: `CK_GSP_COT_ENCODE_SELFTEST: PASS` (80 checks:
+both layouts against the compiler, golden header words, encode/decode round
+trips, 46 field mutations and every header truncation refused by name), same
+under ASan/UBSan.
+
+## 11. Extracted arrays and the GB10B boot decision [SOURCE]
+
+Tool: `native/kernel/tools/ck_gsp_bindata_extract.c` (hosted C, no
+dependencies beyond our own `native/argus/sha256.c`; SHA-384, CRC-32 and the
+raw-DEFLATE decoder, stored/fixed/dynamic blocks, are in the tool).
+`make -C native/kernel gsp-bindata-extract` builds it;
+`make -C native/kernel gsp-bindata-test` runs the self-test and, when the two
+driver files are present under `GSP_BINDATA_DIR` (default
+`~/.cache/aienos/open-gpu-kernel-modules-580.173.02`), the pin check; it is
+part of `make test`. Without the files the pin line reads `NOT_RUN`, never
+PASS.
+
+What it checks before it writes a byte: the generated-file grammar
+(`FUNCTION`, `FILE NAME`, `FILE TYPE BINARY`, `COMPRESSION`, `COMPLEX_STRUCT
+NO`, `DATA SIZE`, `COMPRESSED SIZE`), the array identifier
+`kgspBinArchive<stem>_BINDATA_LABEL_<label>_data`, the storage record
+(both sizes, the data pointer, `bCompressed`), the archive table (`entryNum`,
+order, labels), duplicate labels, more than 16 arrays or 8 MiB per array;
+for compressed arrays the DEFLATE stream, the CRC-32 and the ISIZE trailer;
+then the meaning: `UCODE_DESC_*` is 84 bytes, version 5, monitor enabled,
+with manifest/monitor regions inside the matching `UCODE_IMAGE_*` and
+256-byte aligned; `UCODE_HASH` is 48 bytes and equals SHA-384 of
+`UCODE_IMAGE`; `UCODE_SIG` and `UCODE_PKEY` are 384 bytes; any other label is
+refused. Any failure prints `REFUSED: <reason>` and exits 2.
+
+Usage: `ck_gsp_bindata_extract extract FILE [--out DIR]` prints one
+`array <function> <label> file=... stored=N compressed=yes|no size=N
+sha256=...` line per array, the decoded descriptors, `hash384 ... equals
+sha384(UCODE_IMAGE)` and `RESULT OK arrays=N`; `selftest` prints
+`CK_GSP_BINDATA_EXTRACT_SELFTEST: PASS|FAIL (N checks, M failures)`.
+
+Evidence 2026-10-12 on the box: `CK_GSP_BINDATA_EXTRACT_SELFTEST: PASS (124
+checks, 0 failures)`, same under ASan/UBSan; both GB10B files extract with
+`RESULT OK arrays=4` and the digests in section 3.3;
+`CK_GSP_BINDATA_PIN: PASS`; a foreign C file is refused ("no arrays found").
+
+The boot decision the driver makes for GB10B, as the native kernel would have
+to mirror it [SOURCE kernel_gsp_gh100.c:833-995 `kgspBootstrap_GH100`,
+reached for GB10B through the dispatch else-branch, g_kernel_gsp_nvoc.c:719]:
+
+1. If a KernelFsp object exists and `PDB_PROP_KFSP_DISABLE_GSPFMC` is not
+   set: "boot GSP via FSP" (`kfspSendBootCommands`). GB10B is not in the
+   FSP-present chip list ("GH100 | GB100 | GB102 | GB110 | GB112 | GB202 |
+   GB203 | GB205 | GB206 | GB207"), so `PDB_PROP_KFSP_IS_MISSING` is true
+   [g_kern_fsp_nvoc.c:227-236]: not this branch.
+2. Else if a KernelSec2 object has `PDB_PROP_KSEC2_BOOT_GSPFMC`: "boot GSP
+   via SEC2" (`ksec2SendBootCommands`, section 10). True for "GB10B | GB20B |
+   GB20C" [g_kernel_sec2_nvoc.c:446-450]: **this is the GB10B path.**
+3. Else the direct path `_kgspBootstrapGspFmc_GH100`: program
+   `NV_PGSP_FALCON_BCR_DMAADDR_FMCCODE/FMCDATA/PKCPARAM` from the GspRmBoot
+   image and the descriptor's monitorCode/monitorData/manifest offsets, then
+   `NV_PGSP_FALCON_CPUCTL_STARTCPU`. Not taken on GB10B.
+
+After the SEC2 send, in order: `ksec2WaitForGspTargetMaskReleased`
+(conditional on `PDB_PROP_KSEC2_GSP_MODE_GSPRM`, kernel_sec2_gb10b.c:368 and
+:224/:250/:469); `gpuTimeoutCondWait(_kgspLockdownReleasedOrFmcError)`
+(kernel_gsp_gh100.c:492 onward: `NV_PFALCON_FALCON_MAILBOX0` carries ACR/FMC
+error codes, treated as fatal; the boot-argument address may still sit in the
+mailboxes until the FMC clears them); then any non-zero MAILBOX0 is "GSP-FMC
+reported an error"; `kflcnIsRiscvActive`; then "Waiting for GSP fw RM to be
+ready" (the RPC init handshake, section 8 item 4). Each of these is a
+measurable checkpoint for the attended window, with its own timeout and its
+own refusal line. Nothing in this section has been run on the chip.
+
diff --git a/native/.crumb b/native/.crumb
index 34313b0e..4ca9b66d 100644
--- a/native/.crumb
+++ b/native/.crumb
@@ -79,8 +79,8 @@
           "purpose": "Directory native/disk. Purpose not yet described by a human."
         },
         {
-          "digest": "363106d1f490a2b01ec6e574586fba7608c4d21daa15f8193a6eb6c09436b771",
-          "last_commit": "22eb360",
+          "digest": "5b7f4a413bc71bbd06f2fa0b4dce7beca1671e4d4ae8a27734937820cb85d3b2",
+          "last_commit": "e09825a",
           "name": "kernel",
           "purpose": "Directory native/kernel. Purpose not yet described by a human."
         },
@@ -109,15 +109,15 @@
           "purpose": "Directory native/store. Purpose not yet described by a human."
         }
       ],
-      "children_root": "sha256:b131138811cf35bc55eaf03588d4811aedb64faed4f43d833f5744c5b59cf78d",
-      "compiled_at": "2026-10-12T11:39:56Z",
+      "children_root": "sha256:00ef727fd94a39dffbb0833b33f9a3256b05bc0f01deea8b860ddf0ad08515dd",
+      "compiled_at": "2026-10-12T12:21:27Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:362c8c616872caca2069f3e748db0d8fb7c807bff2771679c4973481a15901f2",
+      "digest": "sha256:00b3df5ff408cf07d222d4ebd836026a58a12532ff116b765594246a09a901bc",
       "evidence_files": 4,
       "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335",
-      "files": 470,
-      "generated_at_commit": "22eb3603074e5df77df9914dbd301d67122564b5",
-      "source_tree": "sha256:f04c1759895b503bd9e16472e7390e4548af3a69bafe559cd4bd4ab865111550"
+      "files": 472,
+      "generated_at_commit": "e09825afddde294659ca4a06354990e7ff530b62",
+      "source_tree": "sha256:8807b5a7fe5e78aadc6acd37daa24c037b5018f7ae617985ca6677616a8fe59d"
     },
     "owner_lane": "",
     "provenance": {
diff --git a/native/kernel/.crumb b/native/kernel/.crumb
index 632d4982..e71cde8b 100644
--- a/native/kernel/.crumb
+++ b/native/kernel/.crumb
@@ -93,21 +93,21 @@
           "purpose": "Directory native/kernel/tests. Purpose not yet described by a human."
         },
         {
-          "digest": "17933def2bc3028812efbff34692c71e95e21d7c423e64646a3f2ad9dc5777ea",
-          "last_commit": "e90226b",
+          "digest": "62f6ec248db3b6415da5d71b60b24fcb0cb8f4c8ec8478b36874e291c4f1ba24",
+          "last_commit": "e09825a",
           "name": "tools",
-          "purpose": "Directory native/kernel/tools. Purpose not yet described by a human."
+          "purpose": "Hosted (Linux-built) checkers for the native kernel, run by `make -C native/kernel test`; nothin"
         }
       ],
-      "children_root": "sha256:c02bf658c88780c2019300a9cc12cb47465a58d3c8a7f9536ebdb3ca37f28d31",
-      "compiled_at": "2026-10-12T11:39:56Z",
+      "children_root": "sha256:81e2222e47ea38f16bb90bb7334df06f66d821b956acfdaa1f5df9a43def57d9",
+      "compiled_at": "2026-10-12T12:21:27Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:363106d1f490a2b01ec6e574586fba7608c4d21daa15f8193a6eb6c09436b771",
+      "digest": "sha256:5b7f4a413bc71bbd06f2fa0b4dce7beca1671e4d4ae8a27734937820cb85d3b2",
       "evidence_files": 0,
       "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
-      "files": 306,
-      "generated_at_commit": "22eb3603074e5df77df9914dbd301d67122564b5",
-      "source_tree": "sha256:e6ca170ff7a8f29ca37248259e42598bbb44d61d116f5f357f301e91eb2ffea1"
+      "files": 308,
+      "generated_at_commit": "e09825afddde294659ca4a06354990e7ff530b62",
+      "source_tree": "sha256:d83504ba35f0cda9f929b0271c9bf52573f8b0543e64e7f2d04925db5e8ccc4a"
     },
     "owner_lane": "",
     "provenance": {
diff --git a/native/kernel/Makefile b/native/kernel/Makefile
index 9c0d1971..8e975ff7 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 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; \
+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 gsp-cot-test gsp-bindata-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 gsp-cot-test gsp-bindata-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))
@@ -1468,6 +1468,49 @@ gsp-fw-inspect-test: $(GSP_FW_TOOL)
 	  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 encoder/decoder for the SEC2 chain-of-trust boot message (aienos#286 cut C/D host work) ----
+# Byte-exact NVDM-over-MCTP packet and GSP_FMC_BOOT_PARAMS layouts from the pinned
+# driver source; self-test = layouts, golden headers, round trips, every mutation refused.
+# Never loads anything. docs/GB10_FIRMWARE_INVENTORY.md section 10.
+GSP_COT_TOOL := $(OUT)/host/ck_gsp_cot_encode
+$(GSP_COT_TOOL): $(KDIR)/tools/ck_gsp_cot_encode.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_cot_encode.c $(NATIVE)/argus/sha256.c
+gsp-cot-encode: $(GSP_COT_TOOL)
+gsp-cot-test: $(GSP_COT_TOOL)
+	$(GSP_COT_TOOL) selftest | grep -E '^CK_GSP_COT_ENCODE_SELFTEST: (PASS|FAIL)' | grep -q PASS || { $(GSP_COT_TOOL) selftest | grep -E 'FAIL|SELFTEST'; exit 1; }
+	@$(GSP_COT_TOOL) selftest | tail -1
+
+# ---- host extractor for the GB10B firmware arrays compiled into the pinned driver (aienos#286 cut C host work) ----
+# Parses the two public generated files g_bindata_kgspGetBinArchive{GspRmBoot,GspRmFmcGfwProdSigned}_GB10B.c
+# (MIT), cross-checks every size/flag/record, inflates the compressed descriptor with our own
+# DEFLATE decoder (CRC-32 + ISIZE trailer verified), checks UCODE_HASH == SHA-384(UCODE_IMAGE)
+# and the RM_RISCV_UCODE_DESC offsets, and writes the arrays as files. Never loads anything.
+# The driver files are optional (fetch them from the 580.173.02 tag into GSP_BINDATA_DIR);
+# when absent the pin step is NOT_RUN, the self-test still runs. docs/GB10_FIRMWARE_INVENTORY.md 3.3 and 11.
+GSP_BINDATA_TOOL := $(OUT)/host/ck_gsp_bindata_extract
+GSP_BINDATA_DIR ?= $(HOME)/.cache/aienos/open-gpu-kernel-modules-580.173.02
+GSP_BINDATA_FMC_IMAGE_SHA256 := b13ead4b9a6b8a0d268f3094cc4f4521d6be8a9a525edbc9a8f3f397c86f1f2e
+GSP_BINDATA_BOOT_PROD_SHA256 := 427a691d8453f02548d395f40ac900eb0d5b8ea864e8f14f958eef890ba400a2
+$(GSP_BINDATA_TOOL): $(KDIR)/tools/ck_gsp_bindata_extract.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_bindata_extract.c $(NATIVE)/argus/sha256.c
+gsp-bindata-extract: $(GSP_BINDATA_TOOL)
+gsp-bindata-test: $(GSP_BINDATA_TOOL)
+	$(GSP_BINDATA_TOOL) selftest | grep -E '^CK_GSP_BINDATA_EXTRACT_SELFTEST: (PASS|FAIL)' | grep -q PASS || { $(GSP_BINDATA_TOOL) selftest | grep -E 'FAIL|SELFTEST'; exit 1; }
+	@$(GSP_BINDATA_TOOL) selftest | tail -1
+	@fmc="$(GSP_BINDATA_DIR)/g_bindata_kgspGetBinArchiveGspRmFmcGfwProdSigned_GB10B.c"; boot="$(GSP_BINDATA_DIR)/g_bindata_kgspGetBinArchiveGspRmBoot_GB10B.c"; \
+	if [ -r "$$fmc" ] && [ -r "$$boot" ]; then \
+	  out=$$($(GSP_BINDATA_TOOL) extract "$$fmc") || { echo "$$out"; echo "CK_GSP_BINDATA_PIN: FAIL (FMC file refused)"; exit 1; }; \
+	  echo "$$out" | grep -E '^(hash384|array .* UCODE_IMAGE |RESULT)'; \
+	  echo "$$out" | grep -q '^array .* UCODE_IMAGE .* size=195912 sha256=$(GSP_BINDATA_FMC_IMAGE_SHA256)$$' || { echo "CK_GSP_BINDATA_PIN: FAIL (FMC image digest differs from the pin)"; exit 1; }; \
+	  out=$$($(GSP_BINDATA_TOOL) extract "$$boot") || { echo "$$out"; echo "CK_GSP_BINDATA_PIN: FAIL (boot file refused)"; exit 1; }; \
+	  echo "$$out" | grep -E '^(desc UCODE_DESC_PROD|array .* UCODE_IMAGE_PROD |RESULT)'; \
+	  echo "$$out" | grep -q '^array .* UCODE_IMAGE_PROD .* size=196608 sha256=$(GSP_BINDATA_BOOT_PROD_SHA256)$$' || { echo "CK_GSP_BINDATA_PIN: FAIL (boot PROD image digest differs from the pin)"; exit 1; }; \
+	  echo "$$out" | grep -q '^desc UCODE_DESC_PROD version=5 manifest=0x0+0xa00 monitor_data=0xa00+0xa600 monitor_code=0xb000+0x24d00 monitor_enabled=1 ' || { echo "CK_GSP_BINDATA_PIN: FAIL (descriptor differs from the pin)"; exit 1; }; \
+	  echo "CK_GSP_BINDATA_PIN: PASS"; \
+	else echo "CK_GSP_BINDATA_PIN: NOT_RUN ($(GSP_BINDATA_DIR) lacks the two GB10B bindata files)"; 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 ed60363b..80c40ec6 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-12T11:39:56Z",
+      "compiled_at": "2026-10-12T12:21:27Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:17933def2bc3028812efbff34692c71e95e21d7c423e64646a3f2ad9dc5777ea",
+      "digest": "sha256:62f6ec248db3b6415da5d71b60b24fcb0cb8f4c8ec8478b36874e291c4f1ba24",
       "evidence_files": 0,
       "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
-      "files": 13,
-      "generated_at_commit": "22eb3603074e5df77df9914dbd301d67122564b5",
-      "source_tree": "sha256:7881dde2c31a3a3cef6c69fbb636f2ca6d6c4e9c12ad199c1a92c870e68764d6"
+      "files": 15,
+      "generated_at_commit": "e09825afddde294659ca4a06354990e7ff530b62",
+      "source_tree": "sha256:34d0d607f3d5f92784e0433c1755ddc6feeafb2beb0892402a20482dd6a356f8"
     },
     "owner_lane": "",
     "provenance": {
@@ -82,6 +82,6 @@
   },
   "layer": "native/kernel/tools",
   "name": "native/kernel/tools",
-  "purpose": "Directory native/kernel/tools. Purpose not yet described by a human.",
+  "purpose": "Hosted (Linux-built) checkers for the native kernel, run by `make -C native/kernel test`; nothing here runs on the chip or sends anything to a device. ck_gsp_fw_inspect: GSP-RM firmware container parser + pin check. ck_gsp_cot_encode: byte-exact SEC2 chain-of-trust message encoder/decoder (docs/GB10_FIRMWARE_INVENTORY.md section 10). ck_gsp_bindata_extract: extracts and verifies the eight GB10B firmware arrays compiled into the pinned driver source, own DEFLATE/CRC-32/SHA-384 (section 11). Each tool has a selftest that refuses malformed input by name; NOT_RUN is never PASS.",
   "schema_version": "1.0.0"
 }
diff --git a/native/kernel/tools/ck_gsp_bindata_extract.c b/native/kernel/tools/ck_gsp_bindata_extract.c
new file mode 100644
index 00000000..c73abd64
--- /dev/null
+++ b/native/kernel/tools/ck_gsp_bindata_extract.c
@@ -0,0 +1,1139 @@
+/* ck_gsp_bindata_extract.c -- hosted extractor for the GB10 GSP firmware
+ * arrays that the pinned NVIDIA open kernel driver (580.173.02) carries as
+ * generated C source (aienos#286 cut C, host work only). Never loads anything.
+ *
+ * Input: one generated file src/nvidia/generated/g_bindata_<func>.c, e.g.
+ *   g_bindata_kgspGetBinArchiveGspRmBoot_GB10B.c            (4 arrays)
+ *   g_bindata_kgspGetBinArchiveGspRmFmcGfwProdSigned_GB10B.c (4 arrays)
+ *
+ * The file format is the driver's own bindata layout: for every array a
+ * comment header (FUNCTION, FILE NAME, FILE TYPE, COMPRESSION, COMPLEX_STRUCT,
+ * DATA SIZE, COMPRESSED SIZE), the `static BINDATA_CONST NvU8 <ident>_data[]`
+ * hex array, a BINDATA_STORAGE_PVT record (uncompressed size, compressed
+ * size, pointer, bCompressed, ...) and at the end one BINDATA_ARCHIVE with
+ * entryNum and the { label, storage } list (SOURCE: the two files above;
+ * bin_data_pvt.h; bin_data.c:36-45 inflates compressed storage with the
+ * driver's zlib). Every one of those fields is cross-checked and any
+ * disagreement is a refusal: this tool never guesses sizes.
+ *
+ * Compressed arrays are gzip members without the 10-byte gzip header: a raw
+ * DEFLATE stream (RFC 1951) followed by the 8-byte gzip trailer CRC-32 and
+ * ISIZE, both little-endian (SOURCE: the driver's own
+ * extract-firmware-nouveau.py at tag 580.173.02, lines 107-108, prepends a
+ * synthetic gzip header and gzip-decompresses; OBSERVED 2026-10-12 on the
+ * 28-byte GB10B descriptor arrays: 20 bytes of fixed-Huffman deflate, CRC-32,
+ * then 0x54 = 84). The decoder and the CRC below are our own (no zlib, no
+ * outside dependency); the trailer is verified, never trusted.
+ *
+ * Semantic checks by label (SOURCE: rmRiscvUcode.h RM_RISCV_UCODE_DESC;
+ * kernel_gsp_gh100.c:303-333 kgspPopulateWprMeta_GH100 and :718-740 the
+ * RISC-V BootROM DMA programming, which shifts every offset by
+ * RISCV_BR_ADDR_ALIGNMENT = 8; kernel_gsp_gb10b.c:507-527 the chain-of-trust
+ * payload fields hash384 / publicKey / signature):
+ *   UCODE_DESC_*  84 bytes, version 5, monitor enabled, manifest + monitor
+ *                 data + monitor code inside the matching UCODE_IMAGE_* and
+ *                 256-byte aligned
+ *   UCODE_HASH    48 bytes and equal to SHA-384 of UCODE_IMAGE (OBSERVED
+ *                 2026-10-12 on the GB10B file; enforced here)
+ *   UCODE_SIG, UCODE_PKEY  384 bytes (RSA-3072)
+ *
+ * Usage:
+ *   ck_gsp_bindata_extract extract FILE [--out DIR]
+ *     Prints one `array` line per label (size, compression, SHA-256), one
+ *     `desc` line per descriptor and `RESULT OK`; with --out writes
+ *     DIR/<func>.<label>.bin (the uncompressed bytes). Exit 2 + `REFUSED:`.
+ *   ck_gsp_bindata_extract selftest
+ *     Known-answer tests for SHA-384 and the inflater (stored, fixed and
+ *     dynamic Huffman blocks, the real descriptor stream), a synthetic
+ *     bindata file that must extract, and every mutation that must be refused.
+ *
+ * Licence note: the driver files are MIT (SPDX in their header). This tool
+ * reads them at run time; nothing from them is vendored into AIENOS.
+ */
+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "sha256.h"
+
+#define MAX_BLOCKS 16
+#define MAX_ARRAY (8u * 1024u * 1024u)
+#define MAX_LINE 512
+#define DESC_BYTES 84u
+#define DESC_VERSION 5u
+#define RISCV_BR_ALIGN 256u
+#define HASH384_BYTES 48u
+#define RSA3072_BYTES 384u
+
+/* ------------------------------------------------------------------------ */
+/* SHA-384 (FIPS 180-4), our own                                            */
+/* ------------------------------------------------------------------------ */
+static const uint64_t K512[80] = {
+    0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
+    0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
+    0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
+    0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
+    0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
+    0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
+    0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
+    0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
+    0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
+    0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
+    0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
+    0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
+    0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
+    0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
+    0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
+    0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
+    0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
+    0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
+    0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
+    0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL,
+};
+
+typedef struct {
+    uint64_t h[8];
+    uint64_t total;
+    uint8_t buf[128];
+    size_t buflen;
+} sha384_ctx;
+
+static uint64_t ror64(uint64_t x, int n) { return (x >> n) | (x << (64 - n)); }
+
+static void sha384_block(sha384_ctx *c, const uint8_t *p) {
+    uint64_t w[80];
+    for (int i = 0; i < 16; i++) {
+        w[i] = 0;
+        for (int j = 0; j < 8; j++) w[i] = (w[i] << 8) | p[i * 8 + j];
+    }
+    for (int i = 16; i < 80; i++) {
+        uint64_t s0 = ror64(w[i - 15], 1) ^ ror64(w[i - 15], 8) ^ (w[i - 15] >> 7);
+        uint64_t s1 = ror64(w[i - 2], 19) ^ ror64(w[i - 2], 61) ^ (w[i - 2] >> 6);
+        w[i] = w[i - 16] + s0 + w[i - 7] + s1;
+    }
+    uint64_t a = c->h[0], b = c->h[1], cc = c->h[2], d = c->h[3], e = c->h[4], f = c->h[5], g = c->h[6], h = c->h[7];
+    for (int i = 0; i < 80; i++) {
+        uint64_t S1 = ror64(e, 14) ^ ror64(e, 18) ^ ror64(e, 41);
+        uint64_t ch = (e & f) ^ (~e & g);
+        uint64_t t1 = h + S1 + ch + K512[i] + w[i];
+        uint64_t S0 = ror64(a, 28) ^ ror64(a, 34) ^ ror64(a, 39);
+        uint64_t mj = (a & b) ^ (a & cc) ^ (b & cc);
+        uint64_t t2 = S0 + mj;
+        h = g; g = f; f = e; e = d + t1; d = cc; cc = b; b = a; a = t1 + t2;
+    }
+    c->h[0] += a; c->h[1] += b; c->h[2] += cc; c->h[3] += d; c->h[4] += e; c->h[5] += f; c->h[6] += g; c->h[7] += h;
+}
+
+static void sha384_init(sha384_ctx *c) {
+    static const uint64_t iv[8] = {
+        0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL, 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL,
+        0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL, 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL,
+    };
+    memcpy(c->h, iv, sizeof iv);
+    c->total = 0;
+    c->buflen = 0;
+}
+
+static void sha384_update(sha384_ctx *c, const uint8_t *p, size_t n) {
+    c->total += n;
+    while (n) {
+        size_t take = 128 - c->buflen;
+        if (take > n) take = n;
+        memcpy(c->buf + c->buflen, p, take);
+        c->buflen += take; p += take; n -= take;
+        if (c->buflen == 128) { sha384_block(c, c->buf); c->buflen = 0; }
+    }
+}
+
+static void sha384_final(sha384_ctx *c, uint8_t out[HASH384_BYTES]) {
+    uint64_t bits = c->total * 8;
+    uint8_t pad = 0x80;
+    sha384_update(c, &pad, 1);
+    c->total -= 1;
+    uint8_t z = 0;
+    while (c->buflen != 112) { sha384_update(c, &z, 1); c->total -= 1; }
+    uint8_t len[16] = {0};
+    for (int i = 0; i < 8; i++) len[15 - i] = (uint8_t)(bits >> (8 * i));
+    sha384_update(c, len, 16);
+    for (int i = 0; i < 6; i++)
+        for (int j = 0; j < 8; j++) out[i * 8 + j] = (uint8_t)(c->h[i] >> (56 - 8 * j));
+}
+
+static void sha384_hash(const uint8_t *p, size_t n, uint8_t out[HASH384_BYTES]) {
+    sha384_ctx c;
+    sha384_init(&c);
+    sha384_update(&c, p, n);
+    sha384_final(&c, out);
+}
+
+/* ------------------------------------------------------------------------ */
+/* Raw DEFLATE decoder (RFC 1951), our own                                  */
+/* ------------------------------------------------------------------------ */
+typedef struct {
+    const uint8_t *in; size_t inlen, inpos;
+    uint32_t bitbuf; int bitcnt;
+    uint8_t *out; size_t outlen, outpos;
+    const char *err;
+} inf_state;
+
+typedef struct { uint16_t count[16]; uint16_t symbol[288]; } huff;
+
+static int inf_bits(inf_state *s, int need, uint32_t *v) {
+    while (s->bitcnt < need) {
+        if (s->inpos >= s->inlen) { s->err = "inflate: input exhausted"; return -1; }
+        s->bitbuf |= (uint32_t)s->in[s->inpos++] << s->bitcnt;
+        s->bitcnt += 8;
+    }
+    *v = s->bitbuf & ((1u << need) - 1u);
+    s->bitbuf >>= need;
+    s->bitcnt -= need;
+    return 0;
+}
+
+/* Returns the number of unused codes (0 = complete), -1 = over-subscribed. */
+static int huff_construct(huff *h, const uint16_t *length, int n) {
+    int offs[16];
+    for (int i = 0; i < 16; i++) h->count[i] = 0;
+    for (int i = 0; i < n; i++) h->count[length[i]]++;
+    if (h->count[0] == n) return 0;
+    int left = 1;
+    for (int len = 1; len < 16; len++) {
+        left <<= 1;
+        left -= h->count[len];
+        if (left < 0) return -1;
+    }
+    offs[1] = 0;
+    for (int len = 1; len < 15; len++) offs[len + 1] = offs[len] + h->count[len];
+    for (int i = 0; i < n; i++)
+        if (length[i] != 0) h->symbol[offs[length[i]]++] = (uint16_t)i;
+    return left;
+}
+
+static int huff_decode(inf_state *s, const huff *h, int *sym) {
+    int code = 0, first = 0, index = 0;
+    for (int len = 1; len < 16; len++) {
+        uint32_t b;
+        if (inf_bits(s, 1, &b)) return -1;
+        code |= (int)b;
+        int count = h->count[len];
+        if (code - count < first) { *sym = h->symbol[index + (code - first)]; return 0; }
+        index += count;
+        first += count;
+        first <<= 1;
+        code <<= 1;
+    }
+    s->err = "inflate: invalid Huffman code";
+    return -1;
+}
+
+static int inf_codes(inf_state *s, const huff *lencode, const huff *distcode) {
+    static const uint16_t lbase[29] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
+    static const uint16_t lext[29] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
+    static const uint16_t dbase[30] = {1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
+    static const uint16_t dext[30] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
+    for (;;) {
+        int sym;
+        if (huff_decode(s, lencode, &sym)) return -1;
+        if (sym < 256) {
+            if (s->outpos >= s->outlen) { s->err = "inflate: output longer than declared size"; return -1; }
+            s->out[s->outpos++] = (uint8_t)sym;
+            continue;
+        }
+        if (sym == 256) return 0;
+        sym -= 257;
+        if (sym >= 29) { s->err = "inflate: invalid length symbol"; return -1; }
+        uint32_t e;
+        if (inf_bits(s, lext[sym], &e)) return -1;
+        size_t len = lbase[sym] + e;
+        if (huff_decode(s, distcode, &sym)) return -1;
+        if (sym >= 30) { s->err = "inflate: invalid distance symbol"; return -1; }
+        if (inf_bits(s, dext[sym], &e)) return -1;
+        size_t dist = dbase[sym] + e;
+        if (dist > s->outpos) { s->err = "inflate: distance before start of output"; return -1; }
+        if (s->outpos + len > s->outlen) { s->err = "inflate: output longer than declared size"; return -1; }
+        while (len--) { s->out[s->outpos] = s->out[s->outpos - dist]; s->outpos++; }
+    }
+}
+
+static int inf_stored(inf_state *s) {
+    s->bitbuf = 0; s->bitcnt = 0;
+    if (s->inpos + 4 > s->inlen) { s->err = "inflate: input exhausted"; return -1; }
+    unsigned len = s->in[s->inpos] | (s->in[s->inpos + 1] << 8);
+    unsigned nlen = s->in[s->inpos + 2] | (s->in[s->inpos + 3] << 8);
+    s->inpos += 4;
+    if ((len ^ 0xffffu) != nlen) { s->err = "inflate: stored block length check failed"; return -1; }
+    if (s->inpos + len > s->inlen) { s->err = "inflate: input exhausted"; return -1; }
+    if (s->outpos + len > s->outlen) { s->err = "inflate: output longer than declared size"; return -1; }
+    memcpy(s->out + s->outpos, s->in + s->inpos, len);
+    s->inpos += len; s->outpos += len;
+    return 0;
+}
+
+static int inf_fixed(inf_state *s) {
+    static huff lencode, distcode;
+    static int built = 0;
+    if (!built) {
+        uint16_t lengths[288];
+        int i = 0;
+        for (; i < 144; i++) lengths[i] = 8;
+        for (; i < 256; i++) lengths[i] = 9;
+        for (; i < 280; i++) lengths[i] = 7;
+        for (; i < 288; i++) lengths[i] = 8;
+        huff_construct(&lencode, lengths, 288);
+        for (i = 0; i < 30; i++) lengths[i] = 5;
+        huff_construct(&distcode, lengths, 30);
+        built = 1;
+    }
+    return inf_codes(s, &lencode, &distcode);
+}
+
+static int inf_dynamic(inf_state *s) {
+    static const uint16_t order[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
+    uint16_t lengths[320];
+    huff lencode, distcode;
+    uint32_t v;
+    if (inf_bits(s, 5, &v)) return -1;
+    int nlen = (int)v + 257;
+    if (inf_bits(s, 5, &v)) return -1;
+    int ndist = (int)v + 1;
+    if (inf_bits(s, 4, &v)) return -1;
+    int ncode = (int)v + 4;
+    if (nlen > 286 || ndist > 30) { s->err = "inflate: bad code counts"; return -1; }
+    int i;
+    for (i = 0; i < ncode; i++) { if (inf_bits(s, 3, &v)) return -1; lengths[order[i]] = (uint16_t)v; }
+    for (; i < 19; i++) lengths[order[i]] = 0;
+    if (huff_construct(&lencode, lengths, 19) != 0) { s->err = "inflate: bad code-length code"; return -1; }
+    i = 0;
+    while (i < nlen + ndist) {
+        int sym;
+        if (huff_decode(s, &lencode, &sym)) return -1;
+        if (sym < 16) { lengths[i++] = (uint16_t)sym; continue; }
+        uint16_t rep = 0;
+        int count;
+        if (sym == 16) {
+            if (i == 0) { s->err = "inflate: repeat with no previous length"; return -1; }
+            rep = lengths[i - 1];
+            if (inf_bits(s, 2, &v)) return -1;
+            count = 3 + (int)v;
+        } else if (sym == 17) {
+            if (inf_bits(s, 3, &v)) return -1;
+            count = 3 + (int)v;
+        } else {
+            if (inf_bits(s, 7, &v)) return -1;
+            count = 11 + (int)v;
+        }
+        if (i + count > nlen + ndist) { s->err = "inflate: too many lengths"; return -1; }
+        while (count--) lengths[i++] = rep;
+    }
+    if (lengths[256] == 0) { s->err = "inflate: no end-of-block code"; return -1; }
+    int err = huff_construct(&lencode, lengths, nlen);
+    if (err < 0 || (err > 0 && nlen - lencode.count[0] != 1)) { s->err = "inflate: bad literal/length code"; return -1; }
+    err = huff_construct(&distcode, lengths + nlen, ndist);
+    if (err < 0 || (err > 0 && ndist - distcode.count[0] != 1)) { s->err = "inflate: bad distance code"; return -1; }
+    return inf_codes(s, &lencode, &distcode);
+}
+
+/* Inflate exactly outlen bytes from the raw stream in[0..inlen); the stream
+ * must end with its final block in the last input byte. */
+static int inflate_raw(const uint8_t *in, size_t inlen, uint8_t *out, size_t outlen, const char **why) {
+    inf_state s = {in, inlen, 0, 0, 0, out, outlen, 0, "inflate: ok"};
+    uint32_t last, type;
+    do {
+        if (inf_bits(&s, 1, &last)) { *why = s.err; return -1; }
+        if (inf_bits(&s, 2, &type)) { *why = s.err; return -1; }
+        int rc = type == 0 ? inf_stored(&s) : type == 1 ? inf_fixed(&s) : type == 2 ? inf_dynamic(&s) : -2;
+        if (rc == -2) { *why = "inflate: invalid block type 3"; return -1; }
+        if (rc) { *why = s.err; return -1; }
+    } while (!last);
+    if (s.outpos != s.outlen) { *why = "inflate: output shorter than declared size"; return -1; }
+    if (s.inpos != s.inlen) { *why = "inflate: trailing bytes after the final block"; return -1; }
+    return 0;
+}
+
+static uint32_t crc32_bytes(const uint8_t *p, size_t n) {
+    uint32_t c = 0xffffffffu;
+    for (size_t i = 0; i < n; i++) {
+        c ^= p[i];
+        for (int k = 0; k < 8; k++) c = (c >> 1) ^ (0xedb88320u & (0u - (c & 1u)));
+    }
+    return ~c;
+}
+
+static uint32_t get32le(const uint8_t *p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); }
+
+/* A bindata compressed array: deflate body + CRC-32 + ISIZE. */
+static int unpack_gz_body(const uint8_t *in, size_t inlen, uint8_t *out, size_t outlen, const char **why) {
+    if (inlen < 9) { *why = "compressed array shorter than a deflate block plus its 8-byte trailer"; return -1; }
+    if (inflate_raw(in, inlen - 8, out, outlen, why)) return -1;
+    if (get32le(in + inlen - 4) != outlen) { *why = "compressed trailer: ISIZE differs from DATA SIZE"; return -1; }
+    if (get32le(in + inlen - 8) != crc32_bytes(out, outlen)) { *why = "compressed trailer: CRC-32 mismatch"; return -1; }
+    return 0;
+}
+
+/* ------------------------------------------------------------------------ */
+/* bindata file parser                                                      */
+/* ------------------------------------------------------------------------ */
+typedef struct {
+    char func[96];
+    char label[64];
+    char fname[160];
+    char ident[192];
+    int compressed;
+    uint32_t data_size, comp_size;
+    uint8_t *bytes;   /* as stored in the array */
+    size_t nbytes;
+    uint8_t *plain;   /* uncompressed (== bytes when not compressed) */
+    size_t plain_len;
+    int have_header, have_data, have_pvt;
+} bd_block;
+
+typedef struct {
+    bd_block b[MAX_BLOCKS];
+    int n;
+    char func[96];
+    int archive_seen;
+    int entry_num;
+    char reason[256];
+} bd_file;
+
+typedef struct { const char *p, *end; int lineno; } cursor;
+
+static void bd_free(bd_file *f) {
+    for (int i = 0; i < f->n; i++) {
+        free(f->b[i].bytes);
+        if (f->b[i].plain != f->b[i].bytes) free(f->b[i].plain);
+        f->b[i].bytes = f->b[i].plain = NULL;
+    }
+}
+
+static int refuse(bd_file *f, const cursor *c, const char *fmt, const char *a) {
+    int n = snprintf(f->reason, sizeof f->reason, "line %d: ", c ? c->lineno : 0);
+    if (n < 0) n = 0;
+    snprintf(f->reason + n, sizeof f->reason - (size_t)n, fmt, a ? a : "");
+    return -1;
+}
+
+/* Copy the next line (without its newline) into buf; 1 = got a line, 0 = EOF. */
+static int next_line(cursor *c, char *buf, size_t cap, bd_file *f) {
+    if (c->p >= c->end) return 0;
+    const char *nl = memchr(c->p, '\n', (size_t)(c->end - c->p));
+    size_t len = nl ? (size_t)(nl - c->p) : (size_t)(c->end - c->p);
+    c->lineno++;
+    if (len >= cap) { refuse(f, c, "line longer than %s characters", "511"); return -1; }
+    memcpy(buf, c->p, len);
+    buf[len] = 0;
+    if (len && buf[len - 1] == '\r') buf[len - 1] = 0;
+    c->p = nl ? nl + 1 : c->end;
+    return 1;
+}
+
+static const char *after(const char *line, const char *prefix) {
+    size_t n = strlen(prefix);
+    return strncmp(line, prefix, n) == 0 ? line + n : NULL;
+}
+
+static int parse_u32(const char *s, uint32_t *v) {
+    if (*s < '0' || *s > '9') return -1;
+    uint64_t acc = 0;
+    while (*s >= '0' && *s <= '9') { acc = acc * 10 + (uint64_t)(*s - '0'); if (acc > 0xffffffffULL) return -1; s++; }
+    while (*s == ' ' || *s == ',' || *s == '\t') s++;
+    if (*s && strncmp(s, "//", 2) != 0) return -1;
+    *v = (uint32_t)acc;
+    return 0;
+}
+
+static int copy_field(char *dst, size_t cap, const char *src, size_t len) {
+    if (len == 0 || len >= cap) return -1;
+    memcpy(dst, src, len);
+    dst[len] = 0;
+    return 0;
+}
+
+static bd_block *cur_block(bd_file *f) { return f->n ? &f->b[f->n - 1] : NULL; }
+
+static int parse_hex_array(cursor *c, bd_file *f, bd_block *b) {
+    char line[MAX_LINE];
+    size_t want = b->compressed ? b->comp_size : b->data_size;
+    if (want == 0 || want > MAX_ARRAY) return refuse(f, c, "array size out of range for %s", b->label);
+    if (next_line(c, line, sizeof line, f) != 1 || strcmp(line, "{") != 0) return refuse(f, c, "expected '{' after the array declaration of %s", b->label);
+    b->bytes = malloc(want);
+    if (!b->bytes) return refuse(f, c, "out of memory for %s", b->label);
+    b->nbytes = 0;
+    for (;;) {
+        int r = next_line(c, line, sizeof line, f);
+        if (r < 0) return -1;
+        if (r == 0) return refuse(f, c, "file ends inside the array of %s", b->label);
+        if (strcmp(line, "};") == 0) break;
+        const char *p = line;
+        while (*p) {
+            while (*p == ' ' || *p == '\t') p++;
+            if (!*p) break;
+            if (p[0] != '0' || p[1] != 'x') return refuse(f, c, "non-hex token in the array of %s", b->label);
+            unsigned v = 0;
+            for (int k = 2; k < 4; k++) {
+                char ch = p[k];
+                unsigned d = ch >= '0' && ch <= '9' ? (unsigned)(ch - '0') : ch >= 'a' && ch <= 'f' ? (unsigned)(ch - 'a' + 10) : ch >= 'A' && ch <= 'F' ? (unsigned)(ch - 'A' + 10) : 16u;
+                if (d == 16u) return refuse(f, c, "non-hex token in the array of %s", b->label);
+                v = v * 16 + d;
+            }
+            if (p[4] != ',') return refuse(f, c, "byte token without a trailing comma in %s", b->label);
+            if (b->nbytes >= want) return refuse(f, c, "more bytes than the declared size in %s", b->label);
+            b->bytes[b->nbytes++] = (uint8_t)v;
+            p += 5;
+        }
+    }
+    if (b->nbytes != want) return refuse(f, c, "fewer bytes than the declared size in %s", b->label);
+    b->have_data = 1;
+    return 0;
+}
+
+static int parse_pvt(cursor *c, bd_file *f, bd_block *b) {
+    char line[MAX_LINE];
+    uint32_t v;
+    if (next_line(c, line, sizeof line, f) != 1 || strcmp(line, "{") != 0) return refuse(f, c, "expected '{' in the storage record of %s", b->label);
+    if (next_line(c, line, sizeof line, f) != 1 || parse_u32(line + strspn(line, " "), &v)) return refuse(f, c, "storage record: bad uncompressed size for %s", b->label);
+    if (v != b->data_size) return refuse(f, c, "storage record: uncompressed size differs from DATA SIZE for %s", b->label);
+    if (next_line(c, line, sizeof line, f) != 1 || parse_u32(line + strspn(line, " "), &v)) return refuse(f, c, "storage record: bad compressed size for %s", b->label);
+    if (v != (b->compressed ? b->comp_size : b->data_size)) return refuse(f, c, "storage record: compressed size differs from the header for %s", b->label);
+    if (next_line(c, line, sizeof line, f) != 1) return refuse(f, c, "storage record: missing pointer line for %s", b->label);
+    const char *p = line + strspn(line, " ");
+    size_t n = strlen(b->ident);
+    if (strncmp(p, b->ident, n) != 0 || p[n] != ',') return refuse(f, c, "storage record: data pointer is not the array of %s", b->label);
+    if (next_line(c, line, sizeof line, f) != 1) return refuse(f, c, "storage record: missing compressed flag for %s", b->label);
+    p = line + strspn(line, " ");
+    int flag = after(p, "NV_TRUE,") ? 1 : after(p, "NV_FALSE,") ? 0 : -1;
+    if (flag < 0) return refuse(f, c, "storage record: bad compressed flag for %s", b->label);
+    if (flag != b->compressed) return refuse(f, c, "storage record: compressed flag disagrees with COMPRESSION for %s", b->label);
+    b->have_pvt = 1;
+    return 0;
+}
+
+static int parse_archive(cursor *c, bd_file *f, const char *decl) {
+    char line[MAX_LINE];
+    uint32_t v;
+    if (f->archive_seen) return refuse(f, c, "second archive table%s", "");
+    const char *name = after(decl, "static const BINDATA_ARCHIVE __");
+    size_t n = strlen(f->func);
+    if (!name || strncmp(name, f->func, n) != 0 || strcmp(name + n, " =") != 0) return refuse(f, c, "archive table name is not the file's function%s", "");
+    if (next_line(c, line, sizeof line, f) != 1 || strcmp(line, "{") != 0) return refuse(f, c, "expected '{' after the archive declaration%s", "");
+    if (next_line(c, line, sizeof line, f) != 1 || parse_u32(line + strspn(line, " "), &v)) return refuse(f, c, "archive: bad entryNum%s", "");
+    if ((int)v != f->n) return refuse(f, c, "archive: entryNum differs from the number of arrays%s", "");
+    f->entry_num = (int)v;
+    if (next_line(c, line, sizeof line, f) != 1 || strcmp(line + strspn(line, " "), "{") != 0) return refuse(f, c, "archive: expected entries block%s", "");
+    if (next_line(c, line, sizeof line, f) != 1 || !after(line + strspn(line, " "), "// entries[]")) return refuse(f, c, "archive: expected the entries comment%s", "");
+    for (int i = 0; i < f->n; i++) {
+        if (next_line(c, line, sizeof line, f) != 1) return refuse(f, c, "archive: missing entry %s", f->b[i].label);
+        const char *p = line + strspn(line, " ");
+        const char *lab = after(p, "{ BINDATA_LABEL_");
+        if (!lab) return refuse(f, c, "archive: malformed entry for %s", f->b[i].label);
+        size_t ll = strlen(f->b[i].label);
+        if (strncmp(lab, f->b[i].label, ll) != 0 || (lab[ll] != ' ' && lab[ll] != ',')) return refuse(f, c, "archive: entry order or label differs from the arrays at %s", f->b[i].label);
+        const char *idx = strstr(lab, "&g_bindata_pvt[__idx_");
+        if (!idx) return refuse(f, c, "archive: entry without a storage index for %s", f->b[i].label);
+        idx += strlen("&g_bindata_pvt[__idx_");
+        size_t il = strlen(f->b[i].ident) - strlen("_data");
+        if (strncmp(idx, f->b[i].ident, il) != 0 || idx[il] != ']') return refuse(f, c, "archive: storage index is not this array for %s", f->b[i].label);
+    }
+    if (next_line(c, line, sizeof line, f) != 1 || strcmp(line + strspn(line, " "), "}") != 0) return refuse(f, c, "archive: expected end of entries%s", "");
+    if (next_line(c, line, sizeof line, f) != 1 || strcmp(line, "};") != 0) return refuse(f, c, "archive: expected end of table%s", "");
+    f->archive_seen = 1;
+    return 0;
+}
+
+static int parse_file(bd_file *f, const char *text, size_t len) {
+    cursor c = {text, text + len, 0};
+    char line[MAX_LINE];
+    memset(f, 0, sizeof *f);
+    for (;;) {
+        int r = next_line(&c, line, sizeof line, f);
+        if (r < 0) return -1;
+        if (r == 0) break;
+        const char *v;
+        bd_block *b = cur_block(f);
+        if ((v = after(line, "// FUNCTION: "))) {
+            if (b && !(b->have_data && b->have_pvt)) return refuse(f, &c, "array %s has no data or no storage record", b->label);
+            if (f->n >= MAX_BLOCKS) return refuse(f, &c, "more than %s arrays", "16");
+            const char *lp = strchr(v, '(');
+            if (!lp || strncmp(lp, "(\"BINDATA_LABEL_", 16) != 0) return refuse(f, &c, "FUNCTION line without a BINDATA_LABEL%s", "");
+            const char *le = strstr(lp + 16, "\")");
+            if (!le || le[2] != 0) return refuse(f, &c, "FUNCTION line not terminated%s", "");
+            b = &f->b[f->n++];
+            if (copy_field(b->func, sizeof b->func, v, (size_t)(lp - v))) return refuse(f, &c, "bad function name%s", "");
+            if (copy_field(b->label, sizeof b->label, lp + 16, (size_t)(le - lp - 16))) return refuse(f, &c, "bad label%s", "");
+            if (f->n == 1) strcpy(f->func, b->func);
+            else if (strcmp(f->func, b->func) != 0) return refuse(f, &c, "array belongs to a different function: %s", b->func);
+            for (int i = 0; i < f->n - 1; i++)
+                if (strcmp(f->b[i].label, b->label) == 0) return refuse(f, &c, "duplicate label %s", b->label);
+            b->comp_size = 0xffffffffu; b->compressed = -1; b->data_size = 0;
+            const char *stem = after(b->func, "kgspGetBinArchive");
+            if (!stem) return refuse(f, &c, "function is not a kgspGetBinArchive function: %s", b->func);
+            char ident[sizeof b->ident];
+            if (snprintf(ident, sizeof ident, "kgspBinArchive%s_BINDATA_LABEL_%s_data", stem, b->label) >= (int)sizeof ident) return refuse(f, &c, "identifier too long for %s", b->label);
+            memcpy(b->ident, ident, sizeof ident);
+            continue;
+        }
+        if (!b) {
+            if (after(line, "static BINDATA_CONST") || after(line, "static const BINDATA_ARCHIVE")) return refuse(f, &c, "data before the first FUNCTION header%s", "");
+            continue;
+        }
+        if ((v = after(line, "// FILE NAME: "))) { if (copy_field(b->fname, sizeof b->fname, v, strlen(v))) return refuse(f, &c, "bad file name for %s", b->label); continue; }
+        if ((v = after(line, "// FILE TYPE: "))) { if (strcmp(v, "BINARY") != 0) return refuse(f, &c, "FILE TYPE is not BINARY for %s", b->label); continue; }
+        if ((v = after(line, "// COMPRESSION: "))) {
+            if (strcmp(v, "YES") == 0) b->compressed = 1;
+            else if (strcmp(v, "NO") == 0) b->compressed = 0;
+            else return refuse(f, &c, "COMPRESSION is neither YES nor NO for %s", b->label);
+            continue;
+        }
+        if ((v = after(line, "// COMPLEX_STRUCT: "))) { if (strcmp(v, "NO") != 0) return refuse(f, &c, "COMPLEX_STRUCT is not NO for %s", b->label); continue; }
+        if ((v = after(line, "// DATA SIZE (bytes): "))) { if (parse_u32(v, &b->data_size)) return refuse(f, &c, "bad DATA SIZE for %s", b->label); continue; }
+        if ((v = after(line, "// COMPRESSED SIZE (bytes): "))) {
+            if (strcmp(v, "N/A") == 0) b->comp_size = 0;
+            else if (parse_u32(v, &b->comp_size)) return refuse(f, &c, "bad COMPRESSED SIZE for %s", b->label);
+            continue;
+        }
+        if ((v = after(line, "static BINDATA_CONST NvU8 "))) {
+            if (b->have_data) return refuse(f, &c, "second array for %s", b->label);
+            if (b->compressed < 0 || b->data_size == 0 || b->comp_size == 0xffffffffu || !b->fname[0]) return refuse(f, &c, "array header incomplete for %s", b->label);
+            if (b->compressed == 1 && b->comp_size == 0) return refuse(f, &c, "COMPRESSED SIZE missing with COMPRESSION YES for %s", b->label);
+            if (b->compressed == 0 && b->comp_size != 0) return refuse(f, &c, "COMPRESSED SIZE given with COMPRESSION NO for %s", b->label);
+            size_t n = strlen(b->ident);
+            if (strncmp(v, b->ident, n) != 0 || strcmp(v + n, "[] = ") != 0) return refuse(f, &c, "array identifier is not the expected one for %s", b->label);
+            b->have_header = 1;
+            if (parse_hex_array(&c, f, b)) return -1;
+            continue;
+        }
+        if (strcmp(line, "#if defined(BINDATA_INCLUDE_STORAGE_PVT_DEFN)") == 0) {
+            if (!b->have_data) return refuse(f, &c, "storage record before the array of %s", b->label);
+            if (b->have_pvt) return refuse(f, &c, "second storage record for %s", b->label);
+            if (parse_pvt(&c, f, b)) return -1;
+            continue;
+        }
+        if (after(line, "static const BINDATA_ARCHIVE __")) {
+            if (!(b->have_data && b->have_pvt)) return refuse(f, &c, "array %s has no data or no storage record", b->label);
+            if (parse_archive(&c, f, line)) return -1;
+            continue;
+        }
+    }
+    if (f->n == 0) return refuse(f, &c, "no arrays found%s", "");
+    if (!f->archive_seen) return refuse(f, &c, "no archive table%s", "");
+    for (int i = 0; i < f->n; i++) {
+        bd_block *b = &f->b[i];
+        if (!(b->have_data && b->have_pvt)) return refuse(f, &c, "array %s has no data or no storage record", b->label);
+        if (b->compressed) {
+            b->plain = malloc(b->data_size);
+            if (!b->plain) return refuse(f, &c, "out of memory for %s", b->label);
+            const char *why;
+            if (unpack_gz_body(b->bytes, b->nbytes, b->plain, b->data_size, &why)) {
+                char msg[200];
+                snprintf(msg, sizeof msg, "%s (%s)", why, b->label);
+                return refuse(f, NULL, "%s", msg);
+            }
+            b->plain_len = b->data_size;
+        } else {
+            b->plain = b->bytes;
+            b->plain_len = b->nbytes;
+        }
+    }
+    return 0;
+}
+
+/* ------------------------------------------------------------------------ */
+/* Semantic checks                                                          */
+/* ------------------------------------------------------------------------ */
+static const bd_block *find_label(const bd_file *f, const char *label) {
+    for (int i = 0; i < f->n; i++)
+        if (strcmp(f->b[i].label, label) == 0) return &f->b[i];
+    return NULL;
+}
+
+typedef struct {
+    uint32_t version, bootloader_off, bootloader_size, bl_param_off, bl_param_size, elf_off, elf_size, app_version;
+    uint32_t manifest_off, manifest_size, mdata_off, mdata_size, mcode_off, mcode_size, monitor_enabled;
+    uint32_t swbrom_code_off, swbrom_code_size, swbrom_data_off, swbrom_data_size, fb_reserved, signed_as_code;
+} riscv_desc;
+
+static void decode_desc(const uint8_t *p, riscv_desc *d) {
+    uint32_t *w = (uint32_t *)d;
+    for (int i = 0; i < 21; i++) w[i] = get32le(p + 4 * i);
+}
+
+static int region_ok(uint32_t off, uint32_t size, size_t image) {
+    return size != 0 && (off % RISCV_BR_ALIGN) == 0 && (uint64_t)off + size <= image;
+}
+
+static int check_semantics(bd_file *f, FILE *out) {
+    for (int i = 0; i < f->n; i++) {
+        bd_block *b = &f->b[i];
+        const char *suffix = after(b->label, "UCODE_DESC");
+        if (suffix) {
+            if (b->plain_len != DESC_BYTES) return refuse(f, NULL, "descriptor is not 84 bytes: %s", b->label);
+            riscv_desc d;
+            decode_desc(b->plain, &d);
+            if (d.version != DESC_VERSION) return refuse(f, NULL, "descriptor version is not 5: %s", b->label);
+            if (d.monitor_enabled != 1) return refuse(f, NULL, "descriptor monitor not enabled: %s", b->label);
+            char img[64];
+            snprintf(img, sizeof img, "UCODE_IMAGE%s", suffix);
+            const bd_block *ib = find_label(f, img);
+            if (!ib) return refuse(f, NULL, "descriptor without a matching image: %s", b->label);
+            if (!region_ok(d.manifest_off, d.manifest_size, ib->plain_len)) return refuse(f, NULL, "descriptor manifest region outside the image or unaligned: %s", b->label);
+            if (!region_ok(d.mdata_off, d.mdata_size, ib->plain_len)) return refuse(f, NULL, "descriptor monitor-data region outside the image or unaligned: %s", b->label);
+            if (!region_ok(d.mcode_off, d.mcode_size, ib->plain_len)) return refuse(f, NULL, "descriptor monitor-code region outside the image or unaligned: %s", b->label);
+            if (out)
+                fprintf(out, "desc %s version=%u manifest=0x%x+0x%x monitor_data=0x%x+0x%x monitor_code=0x%x+0x%x monitor_enabled=%u fb_reserved=0x%x signed_as_code=%u app_version=%u image=%s/%zu\n",
+                        b->label, d.version, d.manifest_off, d.manifest_size, d.mdata_off, d.mdata_size, d.mcode_off, d.mcode_size,
+                        d.monitor_enabled, d.fb_reserved, d.signed_as_code, d.app_version, img, ib->plain_len);
+        } else if (strcmp(b->label, "UCODE_HASH") == 0) {
+            if (b->plain_len != HASH384_BYTES) return refuse(f, NULL, "hash array is not 48 bytes%s", "");
+            const bd_block *ib = find_label(f, "UCODE_IMAGE");
+            if (!ib) return refuse(f, NULL, "hash array without UCODE_IMAGE%s", "");
+            uint8_t h[HASH384_BYTES];
+            sha384_hash(ib->plain, ib->plain_len, h);
+            if (memcmp(h, b->plain, HASH384_BYTES) != 0) return refuse(f, NULL, "UCODE_HASH is not SHA-384 of UCODE_IMAGE%s", "");
+            if (out) fprintf(out, "hash384 UCODE_HASH equals sha384(UCODE_IMAGE)\n");
+        } else if (strcmp(b->label, "UCODE_SIG") == 0 || strcmp(b->label, "UCODE_PKEY") == 0) {
+            if (b->plain_len != RSA3072_BYTES) return refuse(f, NULL, "%s is not 384 bytes", b->label);
+        } else if (!after(b->label, "UCODE_IMAGE")) {
+            return refuse(f, NULL, "unknown label %s", b->label);
+        }
+    }
+    return 0;
+}
+
+static void hex(const uint8_t *p, size_t n, char *out) {
+    static const char *d = "0123456789abcdef";
+    for (size_t i = 0; i < n; i++) { out[2 * i] = d[p[i] >> 4]; out[2 * i + 1] = d[p[i] & 15]; }
+    out[2 * n] = 0;
+}
+
+static int report(const bd_file *f, FILE *out, const char *outdir) {
+    for (int i = 0; i < f->n; i++) {
+        const bd_block *b = &f->b[i];
+        uint8_t d[32];
+        char hx[65];
+        sha256_hash(b->plain, b->plain_len, d);
+        hex(d, 32, hx);
+        fprintf(out, "array %s %s file=%s stored=%zu compressed=%s size=%zu sha256=%s\n", b->func, b->label, b->fname, b->nbytes, b->compressed ? "yes" : "no", b->plain_len, hx);
+        if (outdir) {
+            char path[512];
+            if (snprintf(path, sizeof path, "%s/%s.%s.bin", outdir, b->func, b->label) >= (int)sizeof path) { fprintf(out, "REFUSED: output path too long\n"); return 2; }
+            FILE *w = fopen(path, "wb");
+            if (!w || fwrite(b->plain, 1, b->plain_len, w) != b->plain_len || fclose(w)) { fprintf(out, "REFUSED: cannot write %s: %s\n", path, strerror(errno)); return 2; }
+            fprintf(out, "wrote %s\n", path);
+        }
+    }
+    return 0;
+}
+
+/* ------------------------------------------------------------------------ */
+/* CLI                                                                      */
+/* ------------------------------------------------------------------------ */
+static char *read_file(const char *path, size_t *len) {
+    FILE *fp = fopen(path, "rb");
+    if (!fp) return NULL;
+    if (fseek(fp, 0, SEEK_END)) { fclose(fp); return NULL; }
+    long sz = ftell(fp);
+    if (sz < 0 || sz > 64L * 1024 * 1024) { fclose(fp); errno = EFBIG; return NULL; }
+    rewind(fp);
+    char *buf = malloc((size_t)sz + 1);
+    if (!buf) { fclose(fp); return NULL; }
+    if (fread(buf, 1, (size_t)sz, fp) != (size_t)sz) { free(buf); fclose(fp); errno = EIO; return NULL; }
+    fclose(fp);
+    buf[sz] = 0;
+    *len = (size_t)sz;
+    return buf;
+}
+
+static int cmd_extract(int argc, char **argv) {
+    const char *path = NULL, *outdir = NULL;
+    for (int i = 0; i < argc; i++) {
+        if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) outdir = argv[++i];
+        else if (!path) path = argv[i];
+        else { fprintf(stderr, "usage: ck_gsp_bindata_extract extract FILE [--out DIR]\n"); return 1; }
+    }
+    if (!path) { fprintf(stderr, "usage: ck_gsp_bindata_extract extract FILE [--out DIR]\n"); return 1; }
+    size_t len;
+    char *text = read_file(path, &len);
+    if (!text) { printf("REFUSED: cannot read %s: %s\n", path, strerror(errno)); return 2; }
+    bd_file *f = calloc(1, sizeof *f);
+    if (!f) { free(text); printf("REFUSED: out of memory\n"); return 2; }
+    int rc = parse_file(f, text, len);
+    free(text);
+    if (rc == 0) { printf("function %s arrays=%d\n", f->func, f->n); rc = check_semantics(f, stdout); }
+    if (rc) { printf("REFUSED: %s\n", f->reason); bd_free(f); free(f); return 2; }
+    rc = report(f, stdout, outdir);
+    if (rc == 0) printf("RESULT OK arrays=%d\n", f->n);
+    bd_free(f);
+    free(f);
+    return rc;
+}
+
+/* ------------------------------------------------------------------------ */
+/* Self-test                                                                */
+/* ------------------------------------------------------------------------ */
+static int failures, checks;
+static void check(int ok, const char *what) {
+    checks++;
+    if (!ok) { failures++; printf("FAIL: %s\n", what); }
+}
+
+static size_t unhex(const char *s, uint8_t *out, size_t cap) {
+    size_t n = 0;
+    for (; s[0] && s[1] && n < cap; s += 2, n++) {
+        unsigned v;
+        if (sscanf(s, "%2x", &v) != 1) return 0;
+        out[n] = (uint8_t)v;
+    }
+    return n;
+}
+
+/* Test vectors. Deflate streams were produced once with a reference
+ * compressor (raw, level 0 / 1 / 9) and are compared against the plaintext
+ * (stored, fixed) or its SHA-256 (dynamic, 760 bytes). */
+static const char STORED_STREAM[] = "011b00e4ff4149454e4f532073746f72656420626c6f636b2000ff7f20656e64";
+static const char STORED_PLAIN[] = "4149454e4f532073746f72656420626c6f636b2000ff7f20656e64";
+static const char FIXED_STREAM[] = "4b4c4a4e240e290200";
+static const char FIXED_PLAIN[] = "61626361626361626361626361626361626361626361626361626361626361626361626361626361626321";
+static const char DYNAMIC_STREAM[] =
+    "75525b12c2200cbc0a67f04614538711689db6eaf11df280a4d22f1ed9ddec068adfe31bdc06e1e6a6e4c3f30329b9c7b6ba3907977d4cd3f2c57d21e41de6e47740889421436e9caed277f51e415ad4b613dd06d58d642faba5b6f6a28c697c84b26c0a8ab73a91a831d2b0755d56ecc363384f67e416f1af030e1864661d2a0b834e5c625ba8829eda042f8c1b361d18a1873b7e026c50d46720fef5971049e6d8c5e6b127f1c0d6fa65ed70f26b42fccdae124c6634aeb352b5fd1116aab01f";
+static const char DYNAMIC_SHA256[] = "384833836853e0d0bbe4d054c4f167ce73bf9c120e749d76da574a55c1b0516a";
+#define DYNAMIC_LEN 760
+/* The GB10B RM_RISCV_UCODE_DESC arrays (DBG and PROD are byte-identical),
+ * 28 bytes of fixed-Huffman deflate, from the pinned driver file. */
+static const char GB10B_DESC_STREAM[] = "636520027041f13220de00c4be4c0c8c78f40100b72bc9c254000000";
+static const uint32_t GB10B_DESC_WORDS[21] = {5, 0, 0, 0, 0, 0, 0, 0, 0, 0xa00, 0xa00, 0xa600, 0xb000, 0x24d00, 1, 0, 0, 0, 0, 0, 0};
+
+/* Synthetic bindata file generator. */
+typedef struct { const char *label, *fname; int compressed; uint32_t data_size; const uint8_t *bytes; size_t n; } fx_array;
+
+static size_t fx_emit(char *buf, size_t cap, const char *func, const fx_array *a, int n) {
+    size_t o = 0;
+    const char *stem = func + strlen("kgspGetBinArchive");
+#define EMIT(...) do { int w_ = snprintf(buf + o, cap - o, __VA_ARGS__); if (w_ < 0 || (size_t)w_ >= cap - o) return 0; o += (size_t)w_; } while (0)
+    EMIT("/* synthetic */\n\n");
+    for (int i = 0; i < n; i++) {
+        EMIT("#if defined(BINDATA_INCLUDE_DATA)\n//\n// FUNCTION: %s(\"BINDATA_LABEL_%s\")\n// FILE NAME: %s\n// FILE TYPE: BINARY\n// VAR NAME: N/A\n// COMPRESSION: %s\n// COMPLEX_STRUCT: NO\n// DATA SIZE (bytes): %u\n// COMPRESSED SIZE (bytes): ",
+             func, a[i].label, a[i].fname, a[i].compressed ? "YES" : "NO", a[i].data_size);
+        if (a[i].compressed) EMIT("%zu\n", a[i].n); else EMIT("N/A\n");
+        EMIT("//\nstatic BINDATA_CONST NvU8 kgspBinArchive%s_BINDATA_LABEL_%s_data[] = \n{\n", stem, a[i].label);
+        for (size_t k = 0; k < a[i].n; k++) {
+            if (k % 16 == 0) EMIT("    ");
+            EMIT("0x%02x,%s", a[i].bytes[k], (k % 16 == 15 || k + 1 == a[i].n) ? "\n" : " ");
+        }
+        EMIT("};\n#endif // defined(BINDATA_INCLUDE_DATA)\n\n#if defined(BINDATA_INCLUDE_STORAGE_PVT_DECL)\nBINDATA_STORAGE_PVT kgspBinArchive%s_BINDATA_LABEL_%s_storage_pvt;\n#endif // defined(BINDATA_INCLUDE_STORAGE_PVT_DECL)\n\n", stem, a[i].label);
+        EMIT("#if defined(BINDATA_INCLUDE_STORAGE_PVT_DEFN)\n{\n    %u,  // uncompressed data size (bytes)\n    %zu,  // compressed data size (bytes)\n    kgspBinArchive%s_BINDATA_LABEL_%s_data,  // compressed data pointer\n    %s,  // is pData compressed?\n    NV_TRUE,  // contain information for file overriding?\n    NV_FALSE,  // is the data referenced during load?\n},\n#endif // defined(BINDATA_INCLUDE_STORAGE_PVT_DEFN)\n\n",
+             a[i].data_size, a[i].n, stem, a[i].label, a[i].compressed ? "NV_TRUE" : "NV_FALSE");
+    }
+    EMIT("#if defined(BINDATA_INCLUDE_ARCHIVE)\n//\n// Bindata Archive structure\n//\nstatic const BINDATA_ARCHIVE __%s =\n{\n    %d,  // entryNum\n    {\n        // entries[] : { label, pBinStorage }\n", func, n);
+    for (int i = 0; i < n; i++)
+        EMIT("        { BINDATA_LABEL_%s , (const PBINDATA_STORAGE) &g_bindata_pvt[__idx_kgspBinArchive%s_BINDATA_LABEL_%s] },\n", a[i].label, stem, a[i].label);
+    EMIT("    }\n};\n\n#endif // defined(BINDATA_INCLUDE_ARCHIVE)\n");
+#undef EMIT
+    return o;
+}
+
+static int fx_try(const char *text, size_t len, char *reason, size_t cap) {
+    bd_file *f = calloc(1, sizeof *f);
+    if (!f) return -1;
+    int rc = parse_file(f, text, len);
+    if (rc == 0) rc = check_semantics(f, NULL);
+    snprintf(reason, cap, "%s", f->reason);
+    bd_free(f);
+    free(f);
+    return rc;
+}
+
+static void expect_refused(const char *text, size_t len, const char *want, const char *what) {
+    char reason[256];
+    int rc = fx_try(text, len, reason, sizeof reason);
+    checks++;
+    if (rc == 0 || !strstr(reason, want)) {
+        failures++;
+        printf("FAIL: %s: rc=%d reason='%s' (want '%s')\n", what, rc, reason, want);
+    }
+}
+
+/* Replace the first occurrence of `from` in buf with `to` (same or shorter
+ * length is not required; the buffer is regenerated for each mutation). */
+static size_t mutate(char *buf, size_t len, size_t cap, const char *from, const char *to, int occurrence) {
+    char *p = buf;
+    for (int i = 0; i < occurrence; i++) {
+        p = strstr(p, from);
+        if (!p) return 0;
+        if (i + 1 < occurrence) p += strlen(from);
+    }
+    size_t fl = strlen(from), tl = strlen(to);
+    if (len - fl + tl >= cap) return 0;
+    memmove(p + tl, p + fl, len - (size_t)(p - buf) - fl + 1);
+    memcpy(p, to, tl);
+    return len - fl + tl;
+}
+
+/* gzip body (no header) holding one stored block: 5 + n + 8 bytes. */
+static void stored_block(const uint8_t *src, size_t n, uint8_t *dst) {
+    dst[0] = 1;
+    dst[1] = (uint8_t)n; dst[2] = (uint8_t)(n >> 8);
+    dst[3] = (uint8_t)~dst[1]; dst[4] = (uint8_t)~dst[2];
+    memcpy(dst + 5, src, n);
+    uint32_t crc = crc32_bytes(src, n);
+    for (int i = 0; i < 4; i++) { dst[5 + n + i] = (uint8_t)(crc >> (8 * i)); dst[9 + n + i] = (uint8_t)(n >> (8 * i)); }
+}
+
+static void put_desc(const uint32_t *w, uint8_t *desc) {
+    for (int i = 0; i < 21; i++) { desc[4 * i] = (uint8_t)w[i]; desc[4 * i + 1] = (uint8_t)(w[i] >> 8); desc[4 * i + 2] = (uint8_t)(w[i] >> 16); desc[4 * i + 3] = (uint8_t)(w[i] >> 24); }
+}
+
+static int cmd_selftest(void) {
+    uint8_t buf[1024], out[1024], h[48];
+    char hx[97];
+    const char *why;
+
+    /* SHA-384 known answers (FIPS 180-4 examples). */
+    sha384_hash((const uint8_t *)"", 0, h); hex(h, 48, hx);
+    check(strcmp(hx, "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b") == 0, "sha384 empty");
+    sha384_hash((const uint8_t *)"abc", 3, h); hex(h, 48, hx);
+    check(strcmp(hx, "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7") == 0, "sha384 abc");
+    {
+        uint8_t *big = malloc(1000000);
+        check(big != NULL, "alloc 1e6");
+        if (big) {
+            memset(big, 'a', 1000000);
+            sha384_ctx c; sha384_init(&c);
+            for (size_t o = 0; o < 1000000; o += 777) sha384_update(&c, big + o, o + 777 <= 1000000 ? 777 : 1000000 - o);
+            sha384_final(&c, h); hex(h, 48, hx);
+            check(strncmp(hx, "9d0e1809716474cb086e834e310a4a1c", 32) == 0, "sha384 a*1e6 streamed");
+            free(big);
+        }
+    }
+
+    /* Inflater known answers. */
+    size_t n = unhex(STORED_STREAM, buf, sizeof buf), pn = unhex(STORED_PLAIN, out + 512, 512);
+    check(inflate_raw(buf, n, out, pn, &why) == 0 && memcmp(out, out + 512, pn) == 0, "inflate stored block");
+    n = unhex(FIXED_STREAM, buf, sizeof buf); pn = unhex(FIXED_PLAIN, out + 512, 512);
+    check(inflate_raw(buf, n, out, pn, &why) == 0 && memcmp(out, out + 512, pn) == 0, "inflate fixed block");
+    n = unhex(DYNAMIC_STREAM, buf, sizeof buf);
+    {
+        uint8_t d[32]; char dh[65];
+        check(inflate_raw(buf, n, out, DYNAMIC_LEN, &why) == 0, "inflate dynamic block");
+        sha256_hash(out, DYNAMIC_LEN, d); hex(d, 32, dh);
+        check(strcmp(dh, DYNAMIC_SHA256) == 0, "inflate dynamic block content");
+        check(inflate_raw(buf, n - 1, out, DYNAMIC_LEN, &why) != 0 && strstr(why, "exhausted"), "inflate truncated dynamic refused");
+        check(inflate_raw(buf, n, out, DYNAMIC_LEN - 1, &why) != 0 && strstr(why, "longer than declared"), "inflate short declared size refused");
+        check(inflate_raw(buf, n, out, DYNAMIC_LEN + 1, &why) != 0 && strstr(why, "shorter than declared"), "inflate long declared size refused");
+        uint8_t t[sizeof buf + 1]; memcpy(t, buf, n); t[n] = 0;
+        check(inflate_raw(t, n + 1, out, DYNAMIC_LEN, &why) != 0 && strstr(why, "trailing"), "inflate trailing byte refused");
+    }
+    check(crc32_bytes((const uint8_t *)"123456789", 9) == 0xcbf43926u, "crc32 check value");
+    n = unhex(GB10B_DESC_STREAM, buf, sizeof buf);
+    check(n == 28 && unpack_gz_body(buf, n, out, DESC_BYTES, &why) == 0, "unpack GB10B descriptor array (deflate + CRC-32 + ISIZE)");
+    check(get32le(buf + 24) == 84 && get32le(buf + 20) == crc32_bytes(out, 84), "GB10B descriptor trailer fields");
+    { uint8_t t[28]; memcpy(t, buf, 28); t[21] ^= 1; check(unpack_gz_body(t, 28, out, DESC_BYTES, &why) != 0 && strstr(why, "CRC-32"), "GB10B descriptor CRC flip refused");
+      memcpy(t, buf, 28); t[24] = 83; check(unpack_gz_body(t, 28, out, DESC_BYTES, &why) != 0 && strstr(why, "ISIZE"), "GB10B descriptor ISIZE flip refused");
+      check(unpack_gz_body(t, 8, out, DESC_BYTES, &why) != 0 && strstr(why, "shorter than"), "short compressed array refused"); }
+    {
+        riscv_desc d; decode_desc(out, &d);
+        int same = 1;
+        for (int i = 0; i < 21; i++) if (get32le(out + 4 * i) != GB10B_DESC_WORDS[i]) same = 0;
+        check(same, "GB10B descriptor words");
+        check(d.version == 5 && d.manifest_off == 0 && d.manifest_size == 0xa00 && d.mdata_off == 0xa00 && d.mdata_size == 0xa600 && d.mcode_off == 0xb000 && d.mcode_size == 0x24d00 && d.monitor_enabled == 1, "GB10B descriptor fields");
+        check(d.mcode_off + d.mcode_size <= 196608u, "GB10B descriptor fits the 196608-byte image");
+    }
+    {
+        uint8_t bad[1] = {0x07}; /* BFINAL=1, BTYPE=3 */
+        check(inflate_raw(bad, 1, out, 1, &why) != 0 && strstr(why, "block type 3"), "inflate block type 3 refused");
+        uint8_t st[8] = {1, 2, 0, 0xfd, 0xfe, 'a', 'b', 0}; /* NLEN wrong */
+        check(inflate_raw(st, 7, out, 2, &why) != 0 && strstr(why, "length check"), "inflate stored NLEN refused");
+    }
+
+    /* Synthetic bindata files. File A mirrors the FMC archive (image, hash,
+     * sig, pkey); file B mirrors the boot archive (image + compressed desc). */
+    static uint8_t image[4096], sig[384], pkey[384], desc[84], desc_st[5 + 84 + 8], image2[0x3000];
+    for (size_t i = 0; i < sizeof image; i++) image[i] = (uint8_t)(i * 7 + 3);
+    for (size_t i = 0; i < 384; i++) { sig[i] = (uint8_t)(255 - i); pkey[i] = (uint8_t)(i ^ 0x5a); }
+    sha384_hash(image, sizeof image, h);
+    const uint32_t dw[21] = {5, 0, 0, 0, 0, 0, 0, 0, 0, 0x200, 0x200, 0x400, 0x600, 0x2400, 1, 0, 0, 0, 0, 0x8000, 0};
+    put_desc(dw, desc);
+    stored_block(desc, 84, desc_st);
+    for (size_t i = 0; i < sizeof image2; i++) image2[i] = (uint8_t)(i * 13 + 1);
+    const fx_array fa[4] = {
+        {"UCODE_IMAGE", "kernel/inc/gspcc/bin/test_gfw_image.bin", 0, sizeof image, image, sizeof image},
+        {"UCODE_HASH", "kernel/inc/gspcc/bin/test_gfw_hash.bin", 0, 48, h, 48},
+        {"UCODE_SIG", "kernel/inc/gspcc/bin/test_gfw_sig.bin", 0, 384, sig, 384},
+        {"UCODE_PKEY", "kernel/inc/gspcc/bin/test_gfw_pub_key.bin", 0, 384, pkey, 384},
+    };
+    const fx_array fb[4] = {
+        {"UCODE_IMAGE_DBG", "kernel/inc/gspcc/bin/test_dbg_image.bin", 0, sizeof image2, image2, sizeof image2},
+        {"UCODE_DESC_DBG", "kernel/inc/gspcc/bin/test_dbg_desc.bin", 1, 84, desc_st, sizeof desc_st},
+        {"UCODE_IMAGE_PROD", "kernel/inc/gspcc/bin/test_prd_image.bin", 0, sizeof image2, image2, sizeof image2},
+        {"UCODE_DESC_PROD", "kernel/inc/gspcc/bin/test_prd_desc.bin", 1, 84, desc_st, sizeof desc_st},
+    };
+    size_t cap = 256 * 1024;
+    char *ta = malloc(cap), *tb = malloc(cap), *m = malloc(cap);
+    check(ta && tb && m, "alloc fixtures");
+    if (!(ta && tb && m)) return 1;
+    size_t la = fx_emit(ta, cap, "kgspGetBinArchiveGspRmFmcGfwProdSigned_TEST", fa, 4);
+    size_t lb = fx_emit(tb, cap, "kgspGetBinArchiveGspRmBoot_TEST", fb, 4);
+    check(la > 0 && lb > 0, "emit fixtures");
+    {
+        char reason[256];
+        check(fx_try(ta, la, reason, sizeof reason) == 0, "fixture A extracts");
+        check(fx_try(tb, lb, reason, sizeof reason) == 0, "fixture B extracts");
+        bd_file *f = calloc(1, sizeof *f);
+        if (f && parse_file(f, tb, lb) == 0) {
+            check(f->n == 4 && f->b[1].compressed == 1 && f->b[1].plain_len == 84 && memcmp(f->b[1].plain, desc, 84) == 0, "fixture B descriptor inflated");
+            check(f->b[0].plain_len == sizeof image2 && memcmp(f->b[0].plain, image2, sizeof image2) == 0, "fixture B image bytes");
+        } else check(0, "fixture B reparse");
+        if (f) { bd_free(f); free(f); }
+    }
+
+    /* Mutations of file A (uncompressed arrays). */
+    struct { const char *from, *to; int occ; const char *want; const char *what; } ma[] = {
+        {"// DATA SIZE (bytes): 4096", "// DATA SIZE (bytes): 4095", 1, "more bytes than the declared size", "data size one short"},
+        {"// DATA SIZE (bytes): 4096", "// DATA SIZE (bytes): 4097", 1, "fewer bytes than the declared size", "data size one long"},
+        {"    4096,  // uncompressed", "    4095,  // uncompressed", 1, "uncompressed size differs from DATA SIZE", "pvt uncompressed size"},
+        {"    4096,  // compressed data size", "    4000,  // compressed data size", 1, "compressed size differs from the header", "pvt compressed size"},
+        {"    NV_FALSE,  // is pData compressed?", "    NV_TRUE,  // is pData compressed?", 1, "compressed flag disagrees", "pvt compressed flag"},
+        {"// COMPRESSION: NO", "// COMPRESSION: YES", 1, "COMPRESSED SIZE missing", "compression yes without size"},
+        {"// COMPRESSED SIZE (bytes): N/A", "// COMPRESSED SIZE (bytes): 100", 1, "COMPRESSED SIZE given with COMPRESSION NO", "compressed size with no"},
+        {"// FILE TYPE: BINARY", "// FILE TYPE: TEXT", 1, "FILE TYPE is not BINARY", "file type"},
+        {"// COMPLEX_STRUCT: NO", "// COMPLEX_STRUCT: YES", 1, "COMPLEX_STRUCT is not NO", "complex struct"},
+        {"0x03,", "0y03,", 1, "non-hex token", "non-hex token"},
+        {"0x03,", "0x03 ", 1, "without a trailing comma", "missing comma"},
+        {"};\n#endif // defined(BINDATA_INCLUDE_DATA)", "\n#endif // defined(BINDATA_INCLUDE_DATA)", 1, "non-hex token", "array end removed"},
+        {"(\"BINDATA_LABEL_UCODE_PKEY\")", "(\"BINDATA_LABEL_UCODE_SIG\")", 1, "duplicate label", "duplicate label"},
+        {"(\"BINDATA_LABEL_UCODE_SIG\")", "(\"BINDATA_LABEL_UCODE_FOO\")", 1, "array identifier is not the expected one", "label vs identifier"},
+        {"kgspBinArchiveGspRmFmcGfwProdSigned_TEST_BINDATA_LABEL_UCODE_SIG_data,  // compressed data pointer", "kgspBinArchiveGspRmFmcGfwProdSigned_TEST_BINDATA_LABEL_UCODE_PKEY_data,  // compressed data pointer", 1, "data pointer is not the array", "pvt pointer"},
+        {"    4,  // entryNum", "    3,  // entryNum", 1, "entryNum differs", "archive entry count"},
+        {"{ BINDATA_LABEL_UCODE_SIG ,", "{ BINDATA_LABEL_UCODE_PKEY ,", 1, "entry order or label differs", "archive label order"},
+        {"&g_bindata_pvt[__idx_kgspBinArchiveGspRmFmcGfwProdSigned_TEST_BINDATA_LABEL_UCODE_SIG]", "&g_bindata_pvt[__idx_kgspBinArchiveGspRmFmcGfwProdSigned_TEST_BINDATA_LABEL_UCODE_PKEY]", 1, "storage index is not this array", "archive storage index"},
+        {"static const BINDATA_ARCHIVE __kgspGetBinArchiveGspRmFmcGfwProdSigned_TEST =", "static const BINDATA_ARCHIVE __kgspGetBinArchiveOther_TEST =", 1, "archive table name is not the file's function", "archive name"},
+        {"// FUNCTION: kgspGetBinArchiveGspRmFmcGfwProdSigned_TEST(\"BINDATA_LABEL_UCODE_PKEY\")", "// FUNCTION: kgspGetBinArchiveGspRmBoot_TEST(\"BINDATA_LABEL_UCODE_PKEY\")", 1, "different function", "mixed functions"},
+        {"// DATA SIZE (bytes): 48", "// DATA SIZE (bytes): 47", 1, "more bytes than the declared size", "hash size via array"},
+        {"// COMPRESSION: NO", "// COMPRESSION: MAYBE", 1, "neither YES nor NO", "compression keyword"},
+    };
+    for (size_t i = 0; i < sizeof ma / sizeof ma[0]; i++) {
+        memcpy(m, ta, la + 1);
+        size_t lm = mutate(m, la, cap, ma[i].from, ma[i].to, ma[i].occ);
+        check(lm > 0, ma[i].what);
+        if (lm) expect_refused(m, lm, ma[i].want, ma[i].what);
+    }
+    /* Hash that does not match the image: flip one byte of the hash array. */
+    {
+        memcpy(m, ta, la + 1);
+        char from[8], to[8];
+        snprintf(from, sizeof from, "0x%02x,", h[0]);
+        snprintf(to, sizeof to, "0x%02x,", h[0] ^ 1);
+        /* the hash array is the second array; its first byte follows the IMAGE array */
+        char *p = strstr(m, "UCODE_HASH_data[] = \n{\n    ");
+        check(p != NULL, "locate hash array");
+        if (p) { p += strlen("UCODE_HASH_data[] = \n{\n    "); check(strncmp(p, from, 5) == 0, "hash first byte"); memcpy(p, to, 5); expect_refused(m, la, "not SHA-384 of UCODE_IMAGE", "hash mismatch"); }
+    }
+    /* Image byte flipped: also a hash mismatch. */
+    {
+        memcpy(m, ta, la + 1);
+        char *p = strstr(m, "UCODE_IMAGE_data[] = \n{\n    0x03,");
+        check(p != NULL, "locate image array");
+        if (p) { p += strlen("UCODE_IMAGE_data[] = \n{\n    "); memcpy(p, "0x04,", 5); expect_refused(m, la, "not SHA-384 of UCODE_IMAGE", "image byte flipped"); }
+    }
+    /* Truncated file (ends inside the archive). */
+    {
+        memcpy(m, ta, la + 1);
+        char *p = strstr(m, "        { BINDATA_LABEL_UCODE_PKEY");
+        check(p != NULL, "locate last archive entry");
+        if (p) { *p = 0; expect_refused(m, (size_t)(p - m), "archive: missing entry", "truncated archive"); }
+        memcpy(m, ta, la + 1);
+        p = strstr(m, "#if defined(BINDATA_INCLUDE_ARCHIVE)");
+        if (p) { *p = 0; expect_refused(m, (size_t)(p - m), "no archive table", "no archive table"); }
+    }
+    /* File ends inside an array. */
+    {
+        memcpy(m, ta, la + 1);
+        char *p = strstr(m, "};\n#endif // defined(BINDATA_INCLUDE_DATA)");
+        check(p != NULL, "locate first array end");
+        if (p) { *p = 0; expect_refused(m, (size_t)(p - m), "file ends inside the array", "file ends inside array"); }
+    }
+    /* Empty and foreign files. */
+    expect_refused("", 0, "no arrays found", "empty file");
+    expect_refused("int x;\nstatic BINDATA_CONST NvU8 foo[] = \n{\n};\n", 44, "data before the first FUNCTION", "array before header");
+
+    /* Mutations of file B: regenerate the fixture with a changed descriptor
+     * or a damaged compressed array, then expect the refusal. */
+    struct { uint32_t words[21]; int nlen_flip, crc_flip, isize_flip, cut; uint32_t data_size; const char *want, *what; } mbs[] = {
+        {{4, 0, 0, 0, 0, 0, 0, 0, 0, 0x200, 0x200, 0x400, 0x600, 0x2400, 1, 0, 0, 0, 0, 0x8000, 0}, 0, 0, 0, 0, 84, "descriptor version is not 5", "desc version 4"},
+        {{5, 0, 0, 0, 0, 0, 0, 0, 0, 0x200, 0x200, 0x400, 0x600, 0x2400, 0, 0, 0, 0, 0, 0x8000, 0}, 0, 0, 0, 0, 84, "monitor not enabled", "desc monitor disabled"},
+        {{5, 0, 0, 0, 0, 0, 0, 0, 0, 0x200, 0x200, 0x400, 0x600, 0x2a01, 1, 0, 0, 0, 0, 0x8000, 0}, 0, 0, 0, 0, 84, "monitor-code region outside the image", "desc code past image"},
+        {{5, 0, 0, 0, 0, 0, 0, 0, 0, 0x200, 0x200, 0x400, 0x610, 0x2400, 1, 0, 0, 0, 0, 0x8000, 0}, 0, 0, 0, 0, 84, "monitor-code region outside the image or unaligned", "desc code unaligned"},
+        {{5, 0, 0, 0, 0, 0, 0, 0, 0, 0x200, 0x200, 0x400, 0x600, 0, 1, 0, 0, 0, 0, 0x8000, 0}, 0, 0, 0, 0, 84, "monitor-code region", "desc code size zero"},
+        {{5, 0, 0, 0, 0, 0, 0, 0, 0, 0x200, 0x200, 0x3000, 0x600, 0x2400, 1, 0, 0, 0, 0, 0x8000, 0}, 0, 0, 0, 0, 84, "monitor-data region outside the image", "desc data past image"},
+        {{5, 0, 0, 0, 0, 0, 0, 0, 0x100, 0x2f80, 0x200, 0x400, 0x600, 0x2400, 1, 0, 0, 0, 0, 0x8000, 0}, 0, 0, 0, 0, 84, "manifest region outside the image", "desc manifest past image"},
+        {{5, 0, 0, 0, 0, 0, 0, 0, 0, 0x200, 0x200, 0x400, 0x600, 0x2400, 1, 0, 0, 0, 0, 0x8000, 0}, 1, 0, 0, 0, 84, "stored block length check failed", "desc stored NLEN"},
+        {{5, 0, 0, 0, 0, 0, 0, 0, 0, 0x200, 0x200, 0x400, 0x600, 0x2400, 1, 0, 0, 0, 0, 0x8000, 0}, 0, 1, 0, 0, 84, "CRC-32 mismatch", "desc CRC flipped"},
+        {{5, 0, 0, 0, 0, 0, 0, 0, 0, 0x200, 0x200, 0x400, 0x600, 0x2400, 1, 0, 0, 0, 0, 0x8000, 0}, 0, 0, 1, 0, 84, "ISIZE differs", "desc ISIZE flipped"},
+        {{5, 0, 0, 0, 0, 0, 0, 0, 0, 0x200, 0x200, 0x400, 0x600, 0x2400, 1, 0, 0, 0, 0, 0x8000, 0}, 0, 0, 0, 0, 83, "output longer than declared size", "desc declared 83"},
+        {{5, 0, 0, 0, 0, 0, 0, 0, 0, 0x200, 0x200, 0x400, 0x600, 0x2400, 1, 0, 0, 0, 0, 0x8000, 0}, 0, 0, 0, 0, 85, "output shorter than declared size", "desc declared 85"},
+        {{5, 0, 0, 0, 0, 0, 0, 0, 0, 0x200, 0x200, 0x400, 0x600, 0x2400, 1, 0, 0, 0, 0, 0x8000, 0}, 0, 0, 0, 5, 84, "inflate: input exhausted", "desc stream cut"},
+    };
+    for (size_t i = 0; i < sizeof mbs / sizeof mbs[0]; i++) {
+        uint8_t d2[84], st[5 + 84 + 8];
+        put_desc(mbs[i].words, d2);
+        stored_block(d2, 84, st);
+        if (mbs[i].nlen_flip) st[4] ^= 1;
+        if (mbs[i].crc_flip) st[5 + 84] ^= 1;
+        if (mbs[i].isize_flip) st[5 + 84 + 4] ^= 1;
+        fx_array fm[2] = {fb[0], fb[1]};
+        fm[1].bytes = st; fm[1].n = sizeof st - (size_t)mbs[i].cut; fm[1].data_size = mbs[i].data_size;
+        size_t lm = fx_emit(m, cap, "kgspGetBinArchiveGspRmBoot_TEST", fm, 2);
+        check(lm > 0, mbs[i].what);
+        if (lm) expect_refused(m, lm, mbs[i].want, mbs[i].what);
+    }
+    /* Text mutations of file B. */
+    struct { const char *from, *to; const char *want, *what; } mbt[] = {
+        {"(\"BINDATA_LABEL_UCODE_IMAGE_PROD\")", "(\"BINDATA_LABEL_UCODE_IMAGE_PRD\")", "array identifier is not the expected one", "renamed image (identifier catches)"},
+        {"(\"BINDATA_LABEL_UCODE_DESC_PROD\")", "(\"BINDATA_LABEL_UCODE_DESC_XYZ\")", "array identifier is not the expected one", "renamed desc (identifier catches)"},
+        {"    97,  // compressed data size", "    98,  // compressed data size", "compressed size differs from the header", "pvt compressed size (B)"},
+    };
+    for (size_t i = 0; i < sizeof mbt / sizeof mbt[0]; i++) {
+        memcpy(m, tb, lb + 1);
+        size_t lm = mutate(m, lb, cap, mbt[i].from, mbt[i].to, 1);
+        check(lm > 0, mbt[i].what);
+        if (lm) expect_refused(m, lm, mbt[i].want, mbt[i].what);
+    }
+    /* Semantic refusals that need a consistent archive: rebuild fixtures. */
+    {
+        fx_array fc[2] = {fb[0], fb[1]};
+        fc[1].label = "UCODE_DESC_PROD";
+        size_t lc = fx_emit(m, cap, "kgspGetBinArchiveGspRmBoot_TEST", fc, 2);
+        check(lc > 0, "emit fixture C");
+        if (lc) expect_refused(m, lc, "descriptor without a matching image", "desc without image");
+        fx_array fd[1] = {fb[0]};
+        fd[0].label = "UCODE_OTHER";
+        lc = fx_emit(m, cap, "kgspGetBinArchiveGspRmBoot_TEST", fd, 1);
+        if (lc) expect_refused(m, lc, "unknown label", "unknown label");
+        fx_array fe[2] = {fa[0], fa[2]};
+        fe[1].label = "UCODE_HASH";
+        lc = fx_emit(m, cap, "kgspGetBinArchiveGspRmFmcGfwProdSigned_TEST", fe, 2);
+        if (lc) expect_refused(m, lc, "hash array is not 48 bytes", "hash size");
+        fx_array ff[1] = {fa[2]};
+        ff[0].data_size = 383; ff[0].n = 383;
+        lc = fx_emit(m, cap, "kgspGetBinArchiveGspRmFmcGfwProdSigned_TEST", ff, 1);
+        if (lc) expect_refused(m, lc, "UCODE_SIG is not 384 bytes", "sig size");
+        fx_array fg[1] = {fa[1]};
+        lc = fx_emit(m, cap, "kgspGetBinArchiveGspRmFmcGfwProdSigned_TEST", fg, 1);
+        if (lc) expect_refused(m, lc, "hash array without UCODE_IMAGE", "hash without image");
+        fx_array fi[2] = {fb[0], fb[1]};
+        fi[1].compressed = 1; fi[1].n = 0;
+        lc = fx_emit(m, cap, "kgspGetBinArchiveGspRmBoot_TEST", fi, 2);
+        if (lc) expect_refused(m, lc, "COMPRESSED SIZE missing", "compressed size zero");
+    }
+    free(ta); free(tb); free(m);
+    printf("CK_GSP_BINDATA_EXTRACT_SELFTEST: %s (%d checks, %d failures)\n", failures ? "FAIL" : "PASS", checks, failures);
+    return failures ? 1 : 0;
+}
+
+int main(int argc, char **argv) {
+    if (argc >= 2 && strcmp(argv[1], "extract") == 0) return cmd_extract(argc - 2, argv + 2);
+    if (argc == 2 && strcmp(argv[1], "selftest") == 0) return cmd_selftest();
+    fprintf(stderr, "usage: ck_gsp_bindata_extract extract FILE [--out DIR]\n       ck_gsp_bindata_extract selftest\n");
+    return 1;
+}
diff --git a/native/kernel/tools/ck_gsp_cot_encode.c b/native/kernel/tools/ck_gsp_cot_encode.c
new file mode 100644
index 00000000..4b60eb5d
--- /dev/null
+++ b/native/kernel/tools/ck_gsp_cot_encode.c
@@ -0,0 +1,617 @@
+/* ck_gsp_cot_encode -- host tool: byte-exact encoder, decoder and refusal
+ * tests for the message the pinned Linux driver (nvidia-open 580.173.02)
+ * sends to SEC2 to boot the GSP first-stage image (GSP-FMC) on the GB10
+ * (aienos#286 cut C/D host work: "parsers and refusal tests for firmware
+ * metadata. No loading"). docs/GB10_FIRMWARE_INVENTORY.md section 10 is the
+ * report this tool backs. Nothing here touches a device.
+ *
+ * Wire format, every line from open-gpu-kernel-modules tag 580.173.02:
+ *   - Transport: NVDM (NVIDIA Data Model) over MCTP over the SEC2 EMEM
+ *     channel 0, 1024 bytes (src/nvidia/arch/nvalloc/common/inc/fsp/
+ *     fsp_emem_channels.h:9-11; kernel_sec2_gb10b.c:36-41, :322-333).
+ *   - A packet is two header dwords then the payload (kernel_sec2.c:50-52,
+ *     :305-325). Dword 0 is the MCTP transport header (fsp_mctp_format.h:
+ *     8-16: SOM bit 31, EOM bit 30, SEQ 29:28, TO 27, TAG 26:24, SEID 23:16,
+ *     DEID 15:8, RSVD 7:4, VERSION 3:0); the driver sets only SOM, EOM,
+ *     SEID and SEQ (kernel_sec2.c:319-321; kern_fsp_gh100.c:319-322).
+ *     Dword 1 is the NVDM message header (fsp_mctp_format.h:17-22:
+ *     TYPE 6:0 = 0x7e vendor-PCI, IC bit 7 = 0, VENDOR_ID 23:8 = 0x10de,
+ *     NVDM_TYPE 31:24; kernel_sec2_gb10b.c:391-402).
+ *   - The boot message is NVDM_TYPE_COT = 0x14 (fsp_nvdm_format.h:14) and
+ *     the GB10B SEID is 0 for every type (g_kernel_sec2_nvoc.c:879-888,
+ *     g_kernel_sec2_nvoc.h:907-909). The driver sends it as one packet
+ *     (payload <= 1016 bytes; kernel_sec2.c:305-308) and never sends a
+ *     second command on this channel (kernel_sec2_gb10b.c:719-722).
+ *   - Payload: NVDM_PAYLOAD_COT, packed, 860 bytes (src/nvidia/inc/kernel/
+ *     gpu/fsp/kern_fsp_cot_payload.h:12-29): version u16, size u16,
+ *     gspFmcSysmemOffset u64, frtsSysmemOffset u64, frtsSysmemSize u32,
+ *     frtsVidmemOffset u64, frtsVidmemSize u32, hash384 48 B, publicKey
+ *     384 B, signature 384 B, gspBootArgsSysmemOffset u64. GB10B uses
+ *     version 1 (g_kernel_sec2_nvoc.c:478-482), size = 860
+ *     (kernel_sec2_gb10b.c:580-581), signature and key sizes 384
+ *     (g_kernel_sec2_nvoc.c:472-476). The driver zero-fills the payload and
+ *     sets only gspFmcSysmemOffset, frtsSysmemSize (= the GSP-FMC image
+ *     size, sic), hash384, signature, publicKey and gspBootArgsSysmemOffset
+ *     (kernel_sec2_gb10b.c:507-532, :578); frtsSysmemOffset and the two
+ *     frtsVidmem fields stay 0.
+ *   - After the packet is written at EMEM offset 0 with auto-increment,
+ *     QUEUE_HEAD = 0 and QUEUE_TAIL = padded size - 4 (kernel_sec2_gb10b.c:
+ *     700-732); SEC2 sets HEAD = TAIL when it has taken the packet
+ *     (:344-356) and MAILBOX0 leaves 0xc001cafe when the command is handled
+ *     (:285-297).
+ *   - Boot arguments the payload points at: GSP_FMC_BOOT_PARAMS
+ *     (src/nvidia/arch/nvalloc/common/inc/gsp/gspifpub.h), natural C layout
+ *     (not packed), 88 bytes on the AArch64 LP64 ABI the driver and this
+ *     host share; filled by kgspSetupGspFmcArgs_GH100
+ *     (kernel_gsp_gh100.c:415-470): descriptor = GspFwWprMeta (256 bytes,
+ *     gsp_fw_wpr_meta.h) on a fresh boot, bIsGspRmBoot = true, bootArgs =
+ *     the libos init arguments, SPDM and flush fields only with CC.
+ *
+ * Refusals this tool enforces when encoding or decoding (reason strings are
+ * the test oracle): zero or unaligned image address, address beyond the DMA
+ * window, zero image size, hash/key/signature length not 48/384/384, zero
+ * boot-args address, payload version or size field wrong, non-zero
+ * reserved FRTS fields, payload that does not fit one packet, header dwords
+ * that are not the single-packet COT shape, packet length not a dword
+ * multiple, DMA target out of range, descriptor size not 256, boot flag
+ * not set.
+ *
+ * INFERRED (not a driver check): the image address must be 4 KiB aligned
+ * (the driver maps NV_ALIGN_UP(size, 0x1000) bytes of page-granular system
+ * memory, kernel_sec2_gb10b.c:489-495) and every system address must fit
+ * the 41-bit DMA window /proc/driver/nvidia/gpus reports for this GPU
+ * (--dma-bits overrides).
+ *
+ *   ck_gsp_cot_encode selftest     layout, golden headers, round trips,
+ *                                  every mutation and truncation refused
+ *   ck_gsp_cot_encode layout       print the two layouts as "field offset size"
+ *   ck_gsp_cot_encode encode --fmc-addr HEX --fmc-size N --hash FILE
+ *        --key FILE --sig FILE --bootargs-addr HEX [--dma-bits N] [--out FILE]
+ *                                  build the packet; prints header words,
+ *                                  sizes, tail offset and SHA-256
+ * Hosted C, no outside libraries (SHA-256 is the kernel's own, native/argus).
+ * Never part of an image. No Python. */
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "sha256.h"
+
+/* ---- constants from the driver (SOURCE lines in the header comment) ---- */
+#define EMEM_CHANNEL_BYTES 1024u
+#define PACKET_HEADER_BYTES 8u
+#define PACKET_PAYLOAD_CAP (EMEM_CHANNEL_BYTES - PACKET_HEADER_BYTES)
+#define NVDM_TYPE_COT 0x14u
+#define MCTP_MSG_TYPE_VENDOR_PCI 0x7eu
+#define MCTP_VENDOR_ID_NV 0x10deu
+#define COT_VERSION_GB10B 1u
+#define COT_HASH_BYTES 48u
+#define COT_KEY_BYTES 384u
+#define COT_SIG_BYTES 384u
+#define COT_PAYLOAD_BYTES 860u
+#define COT_PACKET_BYTES (PACKET_HEADER_BYTES + COT_PAYLOAD_BYTES) /* 868 */
+#define FMC_PARAMS_BYTES 88u
+#define WPR_META_BYTES 256u
+#define DMA_BITS_DEFAULT 41u
+#define GSP_DMA_TARGET_COUNT 3u
+
+/* NVDM_PAYLOAD_COT offsets (packed). */
+#define COT_OFF_VERSION 0u
+#define COT_OFF_SIZE 2u
+#define COT_OFF_FMC_ADDR 4u
+#define COT_OFF_FRTS_SYSMEM_OFF 12u
+#define COT_OFF_FRTS_SYSMEM_SIZE 20u
+#define COT_OFF_FRTS_VIDMEM_OFF 24u
+#define COT_OFF_FRTS_VIDMEM_SIZE 32u
+#define COT_OFF_HASH 36u
+#define COT_OFF_KEY 84u
+#define COT_OFF_SIG 468u
+#define COT_OFF_BOOTARGS_ADDR 852u
+
+/* GSP_FMC_BOOT_PARAMS offsets (natural layout, LP64). */
+#define FMC_OFF_REGKEYS 0u
+#define FMC_OFF_BOOT_TARGET 8u
+#define FMC_OFF_BOOT_DESC_SIZE 12u
+#define FMC_OFF_BOOT_DESC_OFFSET 16u
+#define FMC_OFF_BOOT_WPR_OFFSET 24u
+#define FMC_OFF_BOOT_WPR_SIZE 32u
+#define FMC_OFF_BOOT_IS_GSP_RM 36u
+#define FMC_OFF_RM_TARGET 40u
+#define FMC_OFF_RM_BOOTARGS 48u
+#define FMC_OFF_SPDM_TARGET 56u
+#define FMC_OFF_SPDM_PAYLOAD_OFF 64u
+#define FMC_OFF_SPDM_PAYLOAD_SIZE 72u
+#define FMC_OFF_FLUSH_LO 80u
+#define FMC_OFF_FLUSH_HI 84u
+
+/* Mirrors of the driver's declarations, used only to check the offsets
+ * above against the compiler's own layout at self-test time. */
+#pragma pack(1)
+typedef struct {
+    uint16_t version;
+    uint16_t size;
+    uint64_t gspFmcSysmemOffset;
+    uint64_t frtsSysmemOffset;
+    uint32_t frtsSysmemSize;
+    uint64_t frtsVidmemOffset;
+    uint32_t frtsVidmemSize;
+    uint32_t hash384[12];
+    uint32_t publicKey[96];
+    uint32_t signature[96];
+    uint64_t gspBootArgsSysmemOffset;
+} mirror_cot_payload;
+#pragma pack()
+typedef enum { M_TARGET_A, M_TARGET_B, M_TARGET_C, M_TARGET_COUNT } mirror_dma_target;
+typedef struct { uint32_t regkeys; } mirror_init_params;
+typedef struct {
+    mirror_dma_target target;
+    uint32_t gspRmDescSize;
+    uint64_t gspRmDescOffset;
+    uint64_t wprCarveoutOffset;
+    uint32_t wprCarveoutSize;
+    uint8_t bIsGspRmBoot;
+} mirror_acr_params;
+typedef struct { mirror_dma_target target; uint64_t bootArgsOffset; } mirror_rm_params;
+typedef struct { mirror_dma_target target; uint64_t payloadBufferOffset; uint32_t payloadBufferSize; } mirror_spdm_params;
+typedef struct { uint32_t flushSysmemAddrValLo; uint32_t flushSysmemAddrValHi; } mirror_mem_params;
+typedef struct {
+    mirror_init_params initParams;
+    mirror_acr_params bootGspRmParams;
+    mirror_rm_params gspRmParams;
+    mirror_spdm_params gspSpdmParams;
+    mirror_mem_params gspRmMemParams;
+} mirror_fmc_boot_params;
+
+/* ---- inputs and results ---- */
+typedef struct {
+    uint64_t fmc_sysmem_addr;
+    uint32_t fmc_size;
+    const uint8_t *hash; size_t hash_len;
+    const uint8_t *key; size_t key_len;
+    const uint8_t *sig; size_t sig_len;
+    uint64_t bootargs_sysmem_addr;
+    unsigned dma_bits; /* 0 = DMA_BITS_DEFAULT */
+} cot_inputs;
+
+typedef struct {
+    uint32_t regkeys;
+    uint32_t desc_target;
+    uint32_t desc_size;
+    uint64_t desc_offset;
+    uint64_t wpr_carveout_offset;
+    uint32_t wpr_carveout_size;
+    uint8_t is_gsp_rm_boot;
+    uint32_t bootargs_target;
+    uint64_t bootargs_offset;
+    uint32_t spdm_target;
+    uint64_t spdm_payload_offset;
+    uint32_t spdm_payload_size;
+    uint32_t flush_lo, flush_hi;
+    unsigned dma_bits;
+} fmc_inputs;
+
+static void put16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); }
+static void put32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); }
+static void put64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); }
+static uint16_t get16(const uint8_t *p) { return (uint16_t)(p[0] | (p[1] << 8)); }
+static uint32_t get32(const uint8_t *p) { uint32_t v = 0; for (int i = 3; i >= 0; i--) v = (v << 8) | p[i]; return v; }
+static uint64_t get64(const uint8_t *p) { uint64_t v = 0; for (int i = 7; i >= 0; i--) v = (v << 8) | p[i]; return v; }
+
+static int addr_ok(uint64_t a, unsigned dma_bits, int need_page_align, const char **why, const char *what) {
+    if (dma_bits == 0) dma_bits = DMA_BITS_DEFAULT;
+    if (a == 0) { *why = "zero address"; return 0; }
+    if (dma_bits < 64 && (a >> dma_bits) != 0) { *why = "address beyond the DMA window"; return 0; }
+    if (need_page_align && (a & 0xfffu) != 0) { *why = "image address not 4 KiB aligned"; return 0; }
+    (void)what;
+    return 1;
+}
+
+/* ---- headers ---- */
+int cot_mctp_header(unsigned som, unsigned eom, unsigned seid, unsigned seq, uint32_t *out, const char **why) {
+    if (som > 1 || eom > 1) { *why = "som/eom not a bit"; return -1; }
+    if (seid > 0xff) { *why = "seid out of range"; return -1; }
+    if (seq > 3) { *why = "seq out of range"; return -1; }
+    *out = ((uint32_t)som << 31) | ((uint32_t)eom << 30) | ((uint32_t)seq << 28) | ((uint32_t)seid << 16);
+    return 0;
+}
+int cot_nvdm_header(unsigned nvdm_type, uint32_t *out, const char **why) {
+    if (nvdm_type > 0xff) { *why = "nvdm type out of range"; return -1; }
+    *out = MCTP_MSG_TYPE_VENDOR_PCI | (MCTP_VENDOR_ID_NV << 8) | ((uint32_t)nvdm_type << 24);
+    return 0;
+}
+
+/* ---- NVDM_PAYLOAD_COT version 1 ---- */
+int cot_payload_encode(const cot_inputs *in, uint8_t out[COT_PAYLOAD_BYTES], const char **why) {
+    if (!in || !out) { *why = "null argument"; return -1; }
+    if (!addr_ok(in->fmc_sysmem_addr, in->dma_bits, 1, why, "fmc")) return -1;
+    if (in->fmc_size == 0) { *why = "zero image size"; return -1; }
+    if (!in->hash || in->hash_len != COT_HASH_BYTES) { *why = "hash length not 48"; return -1; }
+    if (!in->key || in->key_len != COT_KEY_BYTES) { *why = "public key length not 384"; return -1; }
+    if (!in->sig || in->sig_len != COT_SIG_BYTES) { *why = "signature length not 384"; return -1; }
+    if (!addr_ok(in->bootargs_sysmem_addr, in->dma_bits, 0, why, "bootargs")) return -1;
+    memset(out, 0, COT_PAYLOAD_BYTES);
+    put16(out + COT_OFF_VERSION, COT_VERSION_GB10B);
+    put16(out + COT_OFF_SIZE, COT_PAYLOAD_BYTES);
+    put64(out + COT_OFF_FMC_ADDR, in->fmc_sysmem_addr);
+    put32(out + COT_OFF_FRTS_SYSMEM_SIZE, in->fmc_size);
+    memcpy(out + COT_OFF_HASH, in->hash, COT_HASH_BYTES);
+    memcpy(out + COT_OFF_KEY, in->key, COT_KEY_BYTES);
+    memcpy(out + COT_OFF_SIG, in->sig, COT_SIG_BYTES);
+    put64(out + COT_OFF_BOOTARGS_ADDR, in->bootargs_sysmem_addr);
+    return 0;
+}
+/* Decode and re-check. On success the hash/key/sig pointers point into buf. */
+int cot_payload_decode(const uint8_t *buf, size_t len, cot_inputs *out, unsigned dma_bits, const char **why) {
+    if (!buf || !out) { *why = "null argument"; return -1; }
+    if (len != COT_PAYLOAD_BYTES) { *why = "payload length not 860"; return -1; }
+    if (get16(buf + COT_OFF_VERSION) != COT_VERSION_GB10B) { *why = "payload version not 1"; return -1; }
+    if (get16(buf + COT_OFF_SIZE) != COT_PAYLOAD_BYTES) { *why = "payload size field not 860"; return -1; }
+    if (get64(buf + COT_OFF_FRTS_SYSMEM_OFF) != 0) { *why = "frtsSysmemOffset not zero"; return -1; }
+    if (get64(buf + COT_OFF_FRTS_VIDMEM_OFF) != 0) { *why = "frtsVidmemOffset not zero"; return -1; }
+    if (get32(buf + COT_OFF_FRTS_VIDMEM_SIZE) != 0) { *why = "frtsVidmemSize not zero"; return -1; }
+    memset(out, 0, sizeof *out);
+    out->fmc_sysmem_addr = get64(buf + COT_OFF_FMC_ADDR);
+    out->fmc_size = get32(buf + COT_OFF_FRTS_SYSMEM_SIZE);
+    out->hash = buf + COT_OFF_HASH; out->hash_len = COT_HASH_BYTES;
+    out->key = buf + COT_OFF_KEY; out->key_len = COT_KEY_BYTES;
+    out->sig = buf + COT_OFF_SIG; out->sig_len = COT_SIG_BYTES;
+    out->bootargs_sysmem_addr = get64(buf + COT_OFF_BOOTARGS_ADDR);
+    out->dma_bits = dma_bits;
+    if (!addr_ok(out->fmc_sysmem_addr, dma_bits, 1, why, "fmc")) return -1;
+    if (out->fmc_size == 0) { *why = "zero image size"; return -1; }
+    if (!addr_ok(out->bootargs_sysmem_addr, dma_bits, 0, why, "bootargs")) return -1;
+    return 0;
+}
+
+/* ---- one EMEM packet ---- */
+int cot_packet_encode(const uint8_t *payload, size_t len, unsigned nvdm_type, uint8_t out[EMEM_CHANNEL_BYTES],
+                      size_t *out_len, uint32_t *tail_offset, const char **why) {
+    uint32_t h0, h1;
+    if (!payload || !out || !out_len || !tail_offset) { *why = "null argument"; return -1; }
+    if (len == 0) { *why = "empty payload"; return -1; }
+    if (len > PACKET_PAYLOAD_CAP) { *why = "payload does not fit one packet"; return -1; }
+    if (cot_mctp_header(1, 1, 0, 0, &h0, why) != 0) return -1;
+    if (cot_nvdm_header(nvdm_type, &h1, why) != 0) return -1;
+    memset(out, 0, EMEM_CHANNEL_BYTES);
+    put32(out, h0);
+    put32(out + 4, h1);
+    memcpy(out + PACKET_HEADER_BYTES, payload, len);
+    *out_len = (PACKET_HEADER_BYTES + len + 3u) & ~3u; /* NV_ALIGN_UP(size, 4) */
+    *tail_offset = (uint32_t)(*out_len - 4u);           /* TAIL = last dword written */
+    return 0;
+}
+/* Accepts exactly the single-packet COT shape. payload_out points into buf. */
+int cot_packet_decode(const uint8_t *buf, size_t len, const uint8_t **payload_out, size_t *payload_len, const char **why) {
+    uint32_t h0, h1, want;
+    if (!buf || !payload_out || !payload_len) { *why = "null argument"; return -1; }
+    if (len < PACKET_HEADER_BYTES + 1) { *why = "packet shorter than the headers"; return -1; }
+    if (len > EMEM_CHANNEL_BYTES) { *why = "packet larger than the channel"; return -1; }
+    if ((len & 3u) != 0) { *why = "packet length not a dword multiple"; return -1; }
+    h0 = get32(buf); h1 = get32(buf + 4);
+    if ((h0 >> 31) != 1) { *why = "SOM not set"; return -1; }
+    if (((h0 >> 30) & 1u) != 1) { *why = "EOM not set"; return -1; }
+    if (((h0 >> 28) & 3u) != 0) { *why = "seq not zero"; return -1; }
+    if (((h0 >> 16) & 0xffu) != 0) { *why = "seid not zero"; return -1; }
+    if ((h0 & 0x0fffffffu) != 0) { *why = "mctp reserved bits set"; return -1; }
+    cot_nvdm_header(NVDM_TYPE_COT, &want, why);
+    if (h1 != want) {
+        if ((h1 & 0x7fu) != MCTP_MSG_TYPE_VENDOR_PCI) { *why = "nvdm msg type not vendor-pci"; return -1; }
+        if (((h1 >> 8) & 0xffffu) != MCTP_VENDOR_ID_NV) { *why = "nvdm vendor id not nvidia"; return -1; }
+        if ((h1 >> 24) != NVDM_TYPE_COT) { *why = "nvdm type not COT"; return -1; }
+        *why = "nvdm IC bit set"; return -1;
+    }
+    *payload_out = buf + PACKET_HEADER_BYTES;
+    *payload_len = len - PACKET_HEADER_BYTES;
+    return 0;
+}
+
+/* ---- GSP_FMC_BOOT_PARAMS ---- */
+int fmc_params_encode(const fmc_inputs *in, uint8_t out[FMC_PARAMS_BYTES], const char **why) {
+    if (!in || !out) { *why = "null argument"; return -1; }
+    if (in->desc_target >= GSP_DMA_TARGET_COUNT) { *why = "descriptor DMA target out of range"; return -1; }
+    if (in->desc_size != WPR_META_BYTES) { *why = "descriptor size not 256"; return -1; }
+    if (!addr_ok(in->desc_offset, in->dma_bits, 0, why, "desc")) return -1;
+    if (in->is_gsp_rm_boot != 1) { *why = "bIsGspRmBoot not set"; return -1; }
+    if (in->bootargs_target >= GSP_DMA_TARGET_COUNT) { *why = "boot-args DMA target out of range"; return -1; }
+    if (!addr_ok(in->bootargs_offset, in->dma_bits, 0, why, "bootargs")) return -1;
+    if (in->spdm_target >= GSP_DMA_TARGET_COUNT) { *why = "SPDM DMA target out of range"; return -1; }
+    memset(out, 0, FMC_PARAMS_BYTES);
+    put32(out + FMC_OFF_REGKEYS, in->regkeys);
+    put32(out + FMC_OFF_BOOT_TARGET, in->desc_target);
+    put32(out + FMC_OFF_BOOT_DESC_SIZE, in->desc_size);
+    put64(out + FMC_OFF_BOOT_DESC_OFFSET, in->desc_offset);
+    put64(out + FMC_OFF_BOOT_WPR_OFFSET, in->wpr_carveout_offset);
+    put32(out + FMC_OFF_BOOT_WPR_SIZE, in->wpr_carveout_size);
+    out[FMC_OFF_BOOT_IS_GSP_RM] = in->is_gsp_rm_boot;
+    put32(out + FMC_OFF_RM_TARGET, in->bootargs_target);
+    put64(out + FMC_OFF_RM_BOOTARGS, in->bootargs_offset);
+    put32(out + FMC_OFF_SPDM_TARGET, in->spdm_target);
+    put64(out + FMC_OFF_SPDM_PAYLOAD_OFF, in->spdm_payload_offset);
+    put32(out + FMC_OFF_SPDM_PAYLOAD_SIZE, in->spdm_payload_size);
+    put32(out + FMC_OFF_FLUSH_LO, in->flush_lo);
+    put32(out + FMC_OFF_FLUSH_HI, in->flush_hi);
+    return 0;
+}
+int fmc_params_decode(const uint8_t *buf, size_t len, fmc_inputs *out, unsigned dma_bits, const char **why) {
+    if (!buf || !out) { *why = "null argument"; return -1; }
+    if (len != FMC_PARAMS_BYTES) { *why = "params length not 88"; return -1; }
+    memset(out, 0, sizeof *out);
+    out->regkeys = get32(buf + FMC_OFF_REGKEYS);
+    out->desc_target = get32(buf + FMC_OFF_BOOT_TARGET);
+    out->desc_size = get32(buf + FMC_OFF_BOOT_DESC_SIZE);
+    out->desc_offset = get64(buf + FMC_OFF_BOOT_DESC_OFFSET);
+    out->wpr_carveout_offset = get64(buf + FMC_OFF_BOOT_WPR_OFFSET);
+    out->wpr_carveout_size = get32(buf + FMC_OFF_BOOT_WPR_SIZE);
+    out->is_gsp_rm_boot = buf[FMC_OFF_BOOT_IS_GSP_RM];
+    out->bootargs_target = get32(buf + FMC_OFF_RM_TARGET);
+    out->bootargs_offset = get64(buf + FMC_OFF_RM_BOOTARGS);
+    out->spdm_target = get32(buf + FMC_OFF_SPDM_TARGET);
+    out->spdm_payload_offset = get64(buf + FMC_OFF_SPDM_PAYLOAD_OFF);
+    out->spdm_payload_size = get32(buf + FMC_OFF_SPDM_PAYLOAD_SIZE);
+    out->flush_lo = get32(buf + FMC_OFF_FLUSH_LO);
+    out->flush_hi = get32(buf + FMC_OFF_FLUSH_HI);
+    out->dma_bits = dma_bits;
+    /* padding bytes must be zero: 4..7, 37..39, 44..47, 60..63, 76..79 */
+    for (size_t i = 0; i < len; i++) {
+        int pad = (i >= 4 && i < 8) || (i >= 37 && i < 40) || (i >= 44 && i < 48) || (i >= 60 && i < 64) || (i >= 76 && i < 80);
+        if (pad && buf[i] != 0) { *why = "padding byte not zero"; return -1; }
+    }
+    uint8_t re[FMC_PARAMS_BYTES];
+    if (fmc_params_encode(out, re, why) != 0) return -1;
+    if (memcmp(re, buf, FMC_PARAMS_BYTES) != 0) { *why = "re-encode differs"; return -1; }
+    return 0;
+}
+
+/* ---- self-test ---- */
+static int fails;
+static void check(int ok, const char *what) {
+    printf("  %s %s\n", ok ? "ok  " : "FAIL", what);
+    if (!ok) fails++;
+}
+static void expect_refused(int rc, const char *why, const char *want, const char *what) {
+    int ok = rc != 0 && why && strcmp(why, want) == 0;
+    printf("  %s refused: %s", ok ? "ok  " : "FAIL", what);
+    if (!ok) printf(" (rc=%d why=%s want=%s)", rc, why ? why : "(null)", want);
+    printf("\n");
+    if (!ok) fails++;
+}
+
+static int selftest(void) {
+    const char *why = "";
+    printf("CK_GSP_COT_ENCODE_SELFTEST: begin\n");
+
+    /* 1. layouts against the compiler's own view of the mirrored structs */
+    check(sizeof(mirror_cot_payload) == COT_PAYLOAD_BYTES, "NVDM_PAYLOAD_COT is 860 bytes");
+    check(offsetof(mirror_cot_payload, version) == COT_OFF_VERSION &&
+          offsetof(mirror_cot_payload, size) == COT_OFF_SIZE &&
+          offsetof(mirror_cot_payload, gspFmcSysmemOffset) == COT_OFF_FMC_ADDR &&
+          offsetof(mirror_cot_payload, frtsSysmemOffset) == COT_OFF_FRTS_SYSMEM_OFF &&
+          offsetof(mirror_cot_payload, frtsSysmemSize) == COT_OFF_FRTS_SYSMEM_SIZE &&
+          offsetof(mirror_cot_payload, frtsVidmemOffset) == COT_OFF_FRTS_VIDMEM_OFF &&
+          offsetof(mirror_cot_payload, frtsVidmemSize) == COT_OFF_FRTS_VIDMEM_SIZE &&
+          offsetof(mirror_cot_payload, hash384) == COT_OFF_HASH &&
+          offsetof(mirror_cot_payload, publicKey) == COT_OFF_KEY &&
+          offsetof(mirror_cot_payload, signature) == COT_OFF_SIG &&
+          offsetof(mirror_cot_payload, gspBootArgsSysmemOffset) == COT_OFF_BOOTARGS_ADDR,
+          "NVDM_PAYLOAD_COT field offsets");
+    check(sizeof(mirror_fmc_boot_params) == FMC_PARAMS_BYTES && sizeof(mirror_dma_target) == 4, "GSP_FMC_BOOT_PARAMS is 88 bytes");
+    check(offsetof(mirror_fmc_boot_params, initParams) == FMC_OFF_REGKEYS &&
+          offsetof(mirror_fmc_boot_params, bootGspRmParams) == FMC_OFF_BOOT_TARGET &&
+          offsetof(mirror_fmc_boot_params, bootGspRmParams.gspRmDescSize) == FMC_OFF_BOOT_DESC_SIZE &&
+          offsetof(mirror_fmc_boot_params, bootGspRmParams.gspRmDescOffset) == FMC_OFF_BOOT_DESC_OFFSET &&
+          offsetof(mirror_fmc_boot_params, bootGspRmParams.wprCarveoutOffset) == FMC_OFF_BOOT_WPR_OFFSET &&
+          offsetof(mirror_fmc_boot_params, bootGspRmParams.wprCarveoutSize) == FMC_OFF_BOOT_WPR_SIZE &&
+          offsetof(mirror_fmc_boot_params, bootGspRmParams.bIsGspRmBoot) == FMC_OFF_BOOT_IS_GSP_RM &&
+          offsetof(mirror_fmc_boot_params, gspRmParams) == FMC_OFF_RM_TARGET &&
+          offsetof(mirror_fmc_boot_params, gspRmParams.bootArgsOffset) == FMC_OFF_RM_BOOTARGS &&
+          offsetof(mirror_fmc_boot_params, gspSpdmParams) == FMC_OFF_SPDM_TARGET &&
+          offsetof(mirror_fmc_boot_params, gspSpdmParams.payloadBufferOffset) == FMC_OFF_SPDM_PAYLOAD_OFF &&
+          offsetof(mirror_fmc_boot_params, gspSpdmParams.payloadBufferSize) == FMC_OFF_SPDM_PAYLOAD_SIZE &&
+          offsetof(mirror_fmc_boot_params, gspRmMemParams) == FMC_OFF_FLUSH_LO &&
+          offsetof(mirror_fmc_boot_params, gspRmMemParams.flushSysmemAddrValHi) == FMC_OFF_FLUSH_HI,
+          "GSP_FMC_BOOT_PARAMS field offsets");
+
+    /* 2. golden header words */
+    uint32_t h;
+    check(cot_mctp_header(1, 1, 0, 0, &h, &why) == 0 && h == 0xC0000000u, "MCTP first+last packet, seid 0, seq 0 = 0xC0000000");
+    check(cot_mctp_header(0, 1, 0, 3, &h, &why) == 0 && h == 0x70000000u, "MCTP last continuation seq 3 = 0x70000000");
+    check(cot_mctp_header(0, 0, 0, 1, &h, &why) == 0 && h == 0x10000000u, "MCTP middle packet seq 1 = 0x10000000");
+    check(cot_mctp_header(1, 1, 0x2a, 0, &h, &why) == 0 && h == 0xC02A0000u, "MCTP seid lands in bits 23:16");
+    check(cot_nvdm_header(NVDM_TYPE_COT, &h, &why) == 0 && h == 0x1410DE7Eu, "NVDM header for COT = 0x1410DE7E");
+    expect_refused(cot_mctp_header(1, 1, 0, 4, &h, &why), why, "seq out of range", "mctp seq 4");
+    expect_refused(cot_mctp_header(2, 1, 0, 0, &h, &why), why, "som/eom not a bit", "mctp som 2");
+    expect_refused(cot_mctp_header(1, 1, 256, 0, &h, &why), why, "seid out of range", "mctp seid 256");
+    expect_refused(cot_nvdm_header(256, &h, &why), why, "nvdm type out of range", "nvdm type 256");
+
+    /* 3. payload round trip */
+    static uint8_t hash[COT_HASH_BYTES], key[COT_KEY_BYTES], sig[COT_SIG_BYTES];
+    for (unsigned i = 0; i < COT_HASH_BYTES; i++) hash[i] = (uint8_t)(0x10 + i);
+    for (unsigned i = 0; i < COT_KEY_BYTES; i++) key[i] = (uint8_t)(0x20 + i);
+    for (unsigned i = 0; i < COT_SIG_BYTES; i++) sig[i] = (uint8_t)(0x30 + i);
+    cot_inputs in = { 0x0000012345670000ull, 195912u, hash, COT_HASH_BYTES, key, COT_KEY_BYTES, sig, COT_SIG_BYTES, 0x00000123456a0000ull, 0 };
+    uint8_t pl[COT_PAYLOAD_BYTES], pkt[EMEM_CHANNEL_BYTES];
+    size_t pkt_len = 0; uint32_t tail = 0;
+    check(cot_payload_encode(&in, pl, &why) == 0, "payload encodes");
+    check(get16(pl) == 1 && get16(pl + 2) == 860, "payload version 1 size 860");
+    check(get64(pl + COT_OFF_FMC_ADDR) == in.fmc_sysmem_addr && get32(pl + COT_OFF_FRTS_SYSMEM_SIZE) == in.fmc_size, "image address and size in place");
+    check(memcmp(pl + COT_OFF_HASH, hash, 48) == 0 && memcmp(pl + COT_OFF_KEY, key, 384) == 0 && memcmp(pl + COT_OFF_SIG, sig, 384) == 0, "hash, key, signature in place");
+    check(get64(pl + COT_OFF_BOOTARGS_ADDR) == in.bootargs_sysmem_addr, "boot-args address in place");
+    { int zero = 1; for (unsigned i = COT_OFF_FRTS_SYSMEM_OFF; i < COT_OFF_HASH; i++) if (i < COT_OFF_FRTS_SYSMEM_SIZE || i >= COT_OFF_FRTS_VIDMEM_OFF) zero &= pl[i] == 0; check(zero, "reserved FRTS fields zero"); }
+    cot_inputs back;
+    check(cot_payload_decode(pl, sizeof pl, &back, 0, &why) == 0 && back.fmc_sysmem_addr == in.fmc_sysmem_addr && back.fmc_size == in.fmc_size &&
+          back.bootargs_sysmem_addr == in.bootargs_sysmem_addr && memcmp(back.hash, hash, 48) == 0 && memcmp(back.key, key, 384) == 0 && memcmp(back.sig, sig, 384) == 0,
+          "payload decodes to the same fields");
+    check(cot_packet_encode(pl, sizeof pl, NVDM_TYPE_COT, pkt, &pkt_len, &tail, &why) == 0 && pkt_len == COT_PACKET_BYTES && tail == COT_PACKET_BYTES - 4u,
+          "packet is 868 bytes, tail dword offset 864");
+    check(get32(pkt) == 0xC0000000u && get32(pkt + 4) == 0x1410DE7Eu && memcmp(pkt + 8, pl, sizeof pl) == 0, "packet = headers + payload");
+    { const uint8_t *p; size_t n; check(cot_packet_decode(pkt, pkt_len, &p, &n, &why) == 0 && n == COT_PAYLOAD_BYTES && memcmp(p, pl, n) == 0, "packet decodes"); }
+    { uint8_t one[3] = {1, 2, 3}; size_t n; uint32_t t; check(cot_packet_encode(one, 3, NVDM_TYPE_COT, pkt, &n, &t, &why) == 0 && n == 12 && t == 8, "3-byte payload pads to 12, tail 8"); }
+
+    /* 4. payload encode refusals, one field at a time */
+    cot_inputs m;
+    m = in; m.fmc_sysmem_addr = 0; expect_refused(cot_payload_encode(&m, pl, &why), why, "zero address", "image address 0");
+    m = in; m.fmc_sysmem_addr = 0x12345670800ull; expect_refused(cot_payload_encode(&m, pl, &why), why, "image address not 4 KiB aligned", "image address unaligned");
+    m = in; m.fmc_sysmem_addr = 1ull << 41; expect_refused(cot_payload_encode(&m, pl, &why), why, "address beyond the DMA window", "image address at 2^41");
+    m = in; m.fmc_sysmem_addr = 1ull << 41; m.dma_bits = 42; check(cot_payload_encode(&m, pl, &why) == 0, "2^41 accepted with --dma-bits 42");
+    m = in; m.fmc_size = 0; expect_refused(cot_payload_encode(&m, pl, &why), why, "zero image size", "image size 0");
+    m = in; m.hash_len = 47; expect_refused(cot_payload_encode(&m, pl, &why), why, "hash length not 48", "hash 47 bytes");
+    m = in; m.hash = NULL; expect_refused(cot_payload_encode(&m, pl, &why), why, "hash length not 48", "hash null");
+    m = in; m.key_len = 385; expect_refused(cot_payload_encode(&m, pl, &why), why, "public key length not 384", "key 385 bytes");
+    m = in; m.sig_len = 0; expect_refused(cot_payload_encode(&m, pl, &why), why, "signature length not 384", "signature empty");
+    m = in; m.bootargs_sysmem_addr = 0; expect_refused(cot_payload_encode(&m, pl, &why), why, "zero address", "boot-args address 0");
+    m = in; m.bootargs_sysmem_addr = 1ull << 41; expect_refused(cot_payload_encode(&m, pl, &why), why, "address beyond the DMA window", "boot-args at 2^41");
+    expect_refused(cot_payload_encode(NULL, pl, &why), why, "null argument", "null inputs");
+
+    /* 5. payload decode refusals */
+    uint8_t d[COT_PAYLOAD_BYTES];
+    cot_payload_encode(&in, pl, &why);
+#define MUT(setup, want, what) do { memcpy(d, pl, sizeof d); setup; expect_refused(cot_payload_decode(d, sizeof d, &back, 0, &why), why, want, what); } while (0)
+    expect_refused(cot_payload_decode(pl, 859, &back, 0, &why), why, "payload length not 860", "payload 859 bytes");
+    expect_refused(cot_payload_decode(pl, 861, &back, 0, &why), why, "payload length not 860", "payload 861 bytes");
+    MUT(put16(d, 2), "payload version not 1", "version 2");
+    MUT(put16(d, 0), "payload version not 1", "version 0");
+    MUT(put16(d + 2, 859), "payload size field not 860", "size field 859");
+    MUT(put64(d + COT_OFF_FRTS_SYSMEM_OFF, 0x1000), "frtsSysmemOffset not zero", "frtsSysmemOffset set");
+    MUT(put64(d + COT_OFF_FRTS_VIDMEM_OFF, 1), "frtsVidmemOffset not zero", "frtsVidmemOffset set");
+    MUT(put32(d + COT_OFF_FRTS_VIDMEM_SIZE, 1), "frtsVidmemSize not zero", "frtsVidmemSize set");
+    MUT(put64(d + COT_OFF_FMC_ADDR, 0), "zero address", "decoded image address 0");
+    MUT(put64(d + COT_OFF_FMC_ADDR, 0x1010), "image address not 4 KiB aligned", "decoded image address unaligned");
+    MUT(put64(d + COT_OFF_FMC_ADDR, 1ull << 41), "address beyond the DMA window", "decoded image address at 2^41");
+    MUT(put32(d + COT_OFF_FRTS_SYSMEM_SIZE, 0), "zero image size", "decoded image size 0");
+    MUT(put64(d + COT_OFF_BOOTARGS_ADDR, 0), "zero address", "decoded boot-args 0");
+#undef MUT
+
+    /* 6. packet refusals */
+    cot_packet_encode(pl, sizeof pl, NVDM_TYPE_COT, pkt, &pkt_len, &tail, &why);
+    { uint8_t big[PACKET_PAYLOAD_CAP + 1]; size_t n; uint32_t t; memset(big, 0, sizeof big);
+      expect_refused(cot_packet_encode(big, sizeof big, NVDM_TYPE_COT, pkt, &n, &t, &why), why, "payload does not fit one packet", "payload 1017 bytes");
+      check(cot_packet_encode(big, PACKET_PAYLOAD_CAP, NVDM_TYPE_COT, pkt, &n, &t, &why) == 0 && n == 1024 && t == 1020, "payload 1016 bytes fills the channel exactly");
+      expect_refused(cot_packet_encode(big, 0, NVDM_TYPE_COT, pkt, &n, &t, &why), why, "empty payload", "payload 0 bytes");
+      cot_packet_encode(pl, sizeof pl, NVDM_TYPE_COT, pkt, &pkt_len, &tail, &why); }
+    uint8_t q[EMEM_CHANNEL_BYTES]; const uint8_t *p; size_t n;
+#define PMUT(setup, want, what) do { memcpy(q, pkt, sizeof q); setup; expect_refused(cot_packet_decode(q, pkt_len, &p, &n, &why), why, want, what); } while (0)
+    PMUT(put32(q, 0x40000000u), "SOM not set", "SOM cleared");
+    PMUT(put32(q, 0x80000000u), "EOM not set", "EOM cleared");
+    PMUT(put32(q, 0xD0000000u), "seq not zero", "seq 1");
+    PMUT(put32(q, 0xC0050000u), "seid not zero", "seid 5");
+    PMUT(put32(q, 0xC8000000u), "mctp reserved bits set", "TO bit set");
+    PMUT(put32(q, 0xC0000001u), "mctp reserved bits set", "version nibble set");
+    PMUT(put32(q + 4, 0x1510DE7Eu), "nvdm type not COT", "nvdm type 0x15 (FSP_RESPONSE)");
+    PMUT(put32(q + 4, 0x1410DF7Eu), "nvdm vendor id not nvidia", "vendor id off by one");
+    PMUT(put32(q + 4, 0x1410DE7Fu), "nvdm msg type not vendor-pci", "msg type 0x7f");
+    PMUT(put32(q + 4, 0x1410DEFEu), "nvdm IC bit set", "IC bit set");
+#undef PMUT
+    expect_refused(cot_packet_decode(pkt, pkt_len - 1, &p, &n, &why), why, "packet length not a dword multiple", "packet 867 bytes");
+    expect_refused(cot_packet_decode(pkt, 1028, &p, &n, &why), why, "packet larger than the channel", "packet 1028 bytes");
+    { int all = 1; for (size_t t = 0; t < PACKET_HEADER_BYTES + 1; t++) { if (cot_packet_decode(pkt, t, &p, &n, &why) == 0) all = 0; }
+      check(all, "every truncation 0..8 refused"); }
+    { const uint8_t *pp; size_t nn; cot_packet_decode(pkt, pkt_len, &pp, &nn, &why);
+      expect_refused(cot_payload_decode(pp, nn - 4, &back, 0, &why), why, "payload length not 860", "packet with a short payload"); }
+
+    /* 7. FMC boot params round trip and refusals */
+    fmc_inputs f = { 0, 1, WPR_META_BYTES, 0x0000012345680000ull, 0, 0, 1, 1, 0x00000123456c0000ull, 0, 0, 0, 0, 0, 0 };
+    uint8_t fb[FMC_PARAMS_BYTES]; fmc_inputs fback;
+    check(fmc_params_encode(&f, fb, &why) == 0, "FMC params encode");
+    check(get32(fb + FMC_OFF_BOOT_TARGET) == 1 && get32(fb + FMC_OFF_BOOT_DESC_SIZE) == 256 && get64(fb + FMC_OFF_BOOT_DESC_OFFSET) == f.desc_offset &&
+          fb[FMC_OFF_BOOT_IS_GSP_RM] == 1 && get64(fb + FMC_OFF_RM_BOOTARGS) == f.bootargs_offset, "FMC params fields in place");
+    check(fmc_params_decode(fb, sizeof fb, &fback, 0, &why) == 0 && fback.desc_offset == f.desc_offset && fback.bootargs_offset == f.bootargs_offset && fback.desc_target == 1, "FMC params decode");
+    fmc_inputs g;
+    g = f; g.desc_target = 3; expect_refused(fmc_params_encode(&g, fb, &why), why, "descriptor DMA target out of range", "desc target 3");
+    g = f; g.desc_size = 255; expect_refused(fmc_params_encode(&g, fb, &why), why, "descriptor size not 256", "desc size 255");
+    g = f; g.desc_offset = 0; expect_refused(fmc_params_encode(&g, fb, &why), why, "zero address", "desc offset 0");
+    g = f; g.is_gsp_rm_boot = 0; expect_refused(fmc_params_encode(&g, fb, &why), why, "bIsGspRmBoot not set", "boot flag clear");
+    g = f; g.bootargs_target = 3; expect_refused(fmc_params_encode(&g, fb, &why), why, "boot-args DMA target out of range", "boot-args target 3");
+    g = f; g.bootargs_offset = 1ull << 41; expect_refused(fmc_params_encode(&g, fb, &why), why, "address beyond the DMA window", "boot-args at 2^41");
+    g = f; g.spdm_target = 7; expect_refused(fmc_params_encode(&g, fb, &why), why, "SPDM DMA target out of range", "SPDM target 7");
+    fmc_params_encode(&f, fb, &why);
+    { uint8_t e[FMC_PARAMS_BYTES]; memcpy(e, fb, sizeof e); e[5] = 1; expect_refused(fmc_params_decode(e, sizeof e, &fback, 0, &why), why, "padding byte not zero", "padding byte 5 set");
+      memcpy(e, fb, sizeof e); e[FMC_OFF_BOOT_IS_GSP_RM] = 0; expect_refused(fmc_params_decode(e, sizeof e, &fback, 0, &why), why, "bIsGspRmBoot not set", "decoded boot flag clear");
+      memcpy(e, fb, sizeof e); put32(e + FMC_OFF_BOOT_DESC_SIZE, 512); expect_refused(fmc_params_decode(e, sizeof e, &fback, 0, &why), why, "descriptor size not 256", "decoded desc size 512");
+      expect_refused(fmc_params_decode(fb, 87, &fback, 0, &why), why, "params length not 88", "params 87 bytes"); }
+
+    printf("CK_GSP_COT_ENCODE_SELFTEST: %s (%d failures)\n", fails ? "FAIL" : "PASS", fails);
+    return fails ? 1 : 0;
+}
+
+static void layout(void) {
+    printf("NVDM_PAYLOAD_COT version 1, packed, %u bytes (kern_fsp_cot_payload.h:13-28)\n", COT_PAYLOAD_BYTES);
+    printf("version %u 2\nsize %u 2\ngspFmcSysmemOffset %u 8\nfrtsSysmemOffset %u 8\nfrtsSysmemSize %u 4\nfrtsVidmemOffset %u 8\nfrtsVidmemSize %u 4\nhash384 %u 48\npublicKey %u 384\nsignature %u 384\ngspBootArgsSysmemOffset %u 8\n",
+           COT_OFF_VERSION, COT_OFF_SIZE, COT_OFF_FMC_ADDR, COT_OFF_FRTS_SYSMEM_OFF, COT_OFF_FRTS_SYSMEM_SIZE, COT_OFF_FRTS_VIDMEM_OFF, COT_OFF_FRTS_VIDMEM_SIZE, COT_OFF_HASH, COT_OFF_KEY, COT_OFF_SIG, COT_OFF_BOOTARGS_ADDR);
+    printf("packet: mctp 0xC0000000 at 0, nvdm 0x1410DE7E at 4, payload at 8, length %u, QUEUE_TAIL %u, QUEUE_HEAD 0\n", COT_PACKET_BYTES, COT_PACKET_BYTES - 4u);
+    printf("GSP_FMC_BOOT_PARAMS natural layout, %u bytes (gspifpub.h)\n", FMC_PARAMS_BYTES);
+    printf("initParams.regkeys %u 4\nbootGspRmParams.target %u 4\nbootGspRmParams.gspRmDescSize %u 4\nbootGspRmParams.gspRmDescOffset %u 8\nbootGspRmParams.wprCarveoutOffset %u 8\nbootGspRmParams.wprCarveoutSize %u 4\nbootGspRmParams.bIsGspRmBoot %u 1\ngspRmParams.target %u 4\ngspRmParams.bootArgsOffset %u 8\ngspSpdmParams.target %u 4\ngspSpdmParams.payloadBufferOffset %u 8\ngspSpdmParams.payloadBufferSize %u 4\ngspRmMemParams.flushSysmemAddrValLo %u 4\ngspRmMemParams.flushSysmemAddrValHi %u 4\n",
+           FMC_OFF_REGKEYS, FMC_OFF_BOOT_TARGET, FMC_OFF_BOOT_DESC_SIZE, FMC_OFF_BOOT_DESC_OFFSET, FMC_OFF_BOOT_WPR_OFFSET, FMC_OFF_BOOT_WPR_SIZE, FMC_OFF_BOOT_IS_GSP_RM, FMC_OFF_RM_TARGET, FMC_OFF_RM_BOOTARGS, FMC_OFF_SPDM_TARGET, FMC_OFF_SPDM_PAYLOAD_OFF, FMC_OFF_SPDM_PAYLOAD_SIZE, FMC_OFF_FLUSH_LO, FMC_OFF_FLUSH_HI);
+}
+
+static uint8_t *read_file(const char *path, size_t *len) {
+    FILE *f = fopen(path, "rb");
+    if (!f) return NULL;
+    uint8_t *buf = malloc(4096);
+    size_t cap = 4096, n = 0;
+    if (!buf) { fclose(f); return NULL; }
+    for (;;) {
+        size_t r = fread(buf + n, 1, cap - n, f);
+        n += r;
+        if (n < cap) break;
+        uint8_t *nb = realloc(buf, cap * 2);
+        if (!nb) { free(buf); fclose(f); return NULL; }
+        buf = nb; cap *= 2;
+    }
+    fclose(f);
+    *len = n;
+    return buf;
+}
+
+static int encode_cli(int argc, char **argv) {
+    const char *hash_p = NULL, *key_p = NULL, *sig_p = NULL, *out_p = NULL, *why = "";
+    cot_inputs in; memset(&in, 0, sizeof in);
+    for (int i = 2; i < argc; i++) {
+        const char *a = argv[i], *v = (i + 1 < argc) ? argv[i + 1] : NULL;
+        if (!v) { fprintf(stderr, "missing value for %s\n", a); return 2; }
+        if (!strcmp(a, "--fmc-addr")) in.fmc_sysmem_addr = strtoull(v, NULL, 16);
+        else if (!strcmp(a, "--fmc-size")) in.fmc_size = (uint32_t)strtoul(v, NULL, 0);
+        else if (!strcmp(a, "--hash")) hash_p = v;
+        else if (!strcmp(a, "--key")) key_p = v;
+        else if (!strcmp(a, "--sig")) sig_p = v;
+        else if (!strcmp(a, "--bootargs-addr")) in.bootargs_sysmem_addr = strtoull(v, NULL, 16);
+        else if (!strcmp(a, "--dma-bits")) in.dma_bits = (unsigned)strtoul(v, NULL, 0);
+        else if (!strcmp(a, "--out")) out_p = v;
+        else { fprintf(stderr, "unknown option %s\n", a); return 2; }
+        i++;
+    }
+    if (!hash_p || !key_p || !sig_p) { fprintf(stderr, "need --hash --key --sig\n"); return 2; }
+    size_t hl = 0, kl = 0, sl = 0;
+    uint8_t *hb = read_file(hash_p, &hl), *kb = read_file(key_p, &kl), *sb = read_file(sig_p, &sl);
+    if (!hb || !kb || !sb) { fprintf(stderr, "cannot read an input file\n"); return 2; }
+    in.hash = hb; in.hash_len = hl; in.key = kb; in.key_len = kl; in.sig = sb; in.sig_len = sl;
+    uint8_t pl[COT_PAYLOAD_BYTES], pkt[EMEM_CHANNEL_BYTES];
+    size_t n = 0; uint32_t tail = 0;
+    if (cot_payload_encode(&in, pl, &why) != 0 || cot_packet_encode(pl, sizeof pl, NVDM_TYPE_COT, pkt, &n, &tail, &why) != 0) {
+        printf("RESULT REFUSED %s\n", why);
+        return 2;
+    }
+    uint8_t dg[32];
+    sha256_hash(pkt, n, dg);
+    printf("mctp_header 0x%08x\nnvdm_header 0x%08x\npayload_version %u\npayload_size %u\npacket_len %zu\nqueue_head 0\nqueue_tail %u\nfmc_addr 0x%llx\nfmc_size %u\nbootargs_addr 0x%llx\nsha256_packet ",
+           get32(pkt), get32(pkt + 4), get16(pl), get16(pl + 2), n, tail, (unsigned long long)in.fmc_sysmem_addr, in.fmc_size, (unsigned long long)in.bootargs_sysmem_addr);
+    for (int i = 0; i < 32; i++) printf("%02x", dg[i]);
+    printf("\n");
+    if (out_p) {
+        FILE *f = fopen(out_p, "wb");
+        if (!f || fwrite(pkt, 1, n, f) != n) { printf("RESULT REFUSED cannot write output\n"); if (f) fclose(f); return 2; }
+        fclose(f);
+    }
+    printf("RESULT OK\n");
+    free(hb); free(kb); free(sb);
+    return 0;
+}
+
+int main(int argc, char **argv) {
+    if (argc >= 2 && !strcmp(argv[1], "selftest")) return selftest();
+    if (argc >= 2 && !strcmp(argv[1], "layout")) { layout(); return 0; }
+    if (argc >= 2 && !strcmp(argv[1], "encode")) return encode_cli(argc, argv);
+    fprintf(stderr, "usage: ck_gsp_cot_encode selftest | layout | encode --fmc-addr HEX --fmc-size N --hash F --key F --sig F --bootargs-addr HEX [--dma-bits N] [--out F]\n");
+    return 2;
+}
diff --git a/scripts/.crumb b/scripts/.crumb
index dd38a699..6c7ab731 100644
--- a/scripts/.crumb
+++ b/scripts/.crumb
@@ -7,14 +7,14 @@
     "generated": {
       "children": [],
       "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
-      "compiled_at": "2026-10-12T10:28:17Z",
+      "compiled_at": "2026-10-12T11:57:55Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:8f2ba6515d449096eccaa96e1d710d5556e36425d1b82eb8ef096d85200059f9",
+      "digest": "sha256:348804e26984e873db9914d2a3ebfcf6c9ea02619247d78809999011b354e048",
       "evidence_files": 0,
       "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
-      "files": 83,
-      "generated_at_commit": "56d125f12abf2b8ba2886d6da4d86fda8dd244b3",
-      "source_tree": "sha256:7988f7a7c826487fa7709995435d980b07c43f7788773a03e3394fc1be0c3517"
+      "files": 84,
+      "generated_at_commit": "20847ab80738c427be90ca3a1ba0c6b69ee0a90c",
+      "source_tree": "sha256:8a24952f080c725f45f951d689db199366d345ccbc97a847ecdf4698bf8ff22f"
     },
     "owner_lane": "",
     "provenance": {
diff --git a/scripts/workflow_files_check.sh b/scripts/workflow_files_check.sh
new file mode 100755
index 00000000..dde7e1b1
--- /dev/null
+++ b/scripts/workflow_files_check.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+# Fail when GitHub refused any workflow file on a commit.
+#
+# A workflow file GitHub cannot parse (for example an unquoted ": " inside a
+# step name) produces a run that is recorded as "failure" with zero jobs, and
+# that run never appears in a pull request's checks list, so the job it was
+# supposed to run silently never runs. 2026-10-12: .github/workflows/
+# ck-kernel.yml was in that state for four hours (aienos#318). This script
+# runs from an always-on workflow and turns that silence into a red check.
+#
+#   scripts/workflow_files_check.sh <commit sha>
+# Needs: gh with a token that can read Actions (GITHUB_TOKEN in CI), and
+# GITHUB_REPOSITORY (owner/name; defaults to aien-dev/aienos). POSIX sh, no
+# Python. Exit 0 when every workflow run on the commit has at least one job,
+# 1 when one does not, 2 on usage or API error.
+set -eu
+sha="${1:-}"
+[ -n "$sha" ] || { echo "usage: $0 <commit sha>" >&2; exit 2; }
+repo="${GITHUB_REPOSITORY:-aien-dev/aienos}"
+settle="${WORKFLOW_FILES_SETTLE_SECONDS:-20}"
+[ "$settle" -gt 0 ] && sleep "$settle"   # let GitHub record the push-event runs
+runs=$(gh api "repos/$repo/actions/runs?head_sha=$sha&per_page=100" \
+       --jq '.workflow_runs[] | select(.conclusion == "failure") | "\(.id) \(.path)"') \
+  || { echo "WORKFLOW_FILES: ERROR cannot list runs for $sha" >&2; exit 2; }
+bad=0
+for id in $(printf '%s\n' "$runs" | awk 'NF {print $1}'); do
+  path=$(printf '%s\n' "$runs" | awk -v i="$id" '$1 == i {print $2}')
+  n=$(gh api "repos/$repo/actions/runs/$id/jobs" --jq '.total_count') \
+    || { echo "WORKFLOW_FILES: ERROR cannot read jobs of run $id" >&2; exit 2; }
+  if [ "$n" = 0 ]; then
+    echo "WORKFLOW_FILES: REFUSED $path (run $id failed with no jobs: GitHub did not accept the file)"
+    bad=1
+  fi
+done
+[ "$bad" = 0 ] && echo "WORKFLOW_FILES: OK (every workflow run on $sha has jobs)"
+exit "$bad"