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

branch main Branches Tags History

aienux / native / kernel / tools / ck_gsp_cot_encode.c

634 lines · 40149 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* ck_gsp_cot_encode -- host tool: byte-exact encoder, decoder and refusal
18 * tests for the message the pinned Linux driver (nvidia-open 580.173.02)
19 * sends to SEC2 to boot the GSP first-stage image (GSP-FMC) on the GB10
20 * (aienos#286 cut C/D host work: "parsers and refusal tests for firmware
21 * metadata. No loading"). docs/GB10_FIRMWARE_INVENTORY.md section 10 is the
22 * report this tool backs. Nothing here touches a device.
23 *
24 * Wire format, every line from open-gpu-kernel-modules tag 580.173.02:
25 * - Transport: NVDM (NVIDIA Data Model) over MCTP over the SEC2 EMEM
26 * channel 0, 1024 bytes (src/nvidia/arch/nvalloc/common/inc/fsp/
27 * fsp_emem_channels.h:9-11; kernel_sec2_gb10b.c:36-41, :322-333).
28 * - A packet is two header dwords then the payload (kernel_sec2.c:50-52,
29 * :305-325). Dword 0 is the MCTP transport header (fsp_mctp_format.h:
30 * 8-16: SOM bit 31, EOM bit 30, SEQ 29:28, TO 27, TAG 26:24, SEID 23:16,
31 * DEID 15:8, RSVD 7:4, VERSION 3:0); the driver sets only SOM, EOM,
32 * SEID and SEQ (kernel_sec2.c:319-321; kern_fsp_gh100.c:319-322).
33 * Dword 1 is the NVDM message header (fsp_mctp_format.h:17-22:
34 * TYPE 6:0 = 0x7e vendor-PCI, IC bit 7 = 0, VENDOR_ID 23:8 = 0x10de,
35 * NVDM_TYPE 31:24; kernel_sec2_gb10b.c:391-402).
36 * - The boot message is NVDM_TYPE_COT = 0x14 (fsp_nvdm_format.h:14) and
37 * the GB10B SEID is 0 for every type (g_kernel_sec2_nvoc.c:879-888,
38 * g_kernel_sec2_nvoc.h:907-909). The driver sends it as one packet
39 * (payload <= 1016 bytes; kernel_sec2.c:305-308) and never sends a
40 * second command on this channel (kernel_sec2_gb10b.c:719-722).
41 * - Payload: NVDM_PAYLOAD_COT, packed, 860 bytes (src/nvidia/inc/kernel/
42 * gpu/fsp/kern_fsp_cot_payload.h:12-29): version u16, size u16,
43 * gspFmcSysmemOffset u64, frtsSysmemOffset u64, frtsSysmemSize u32,
44 * frtsVidmemOffset u64, frtsVidmemSize u32, hash384 48 B, publicKey
45 * 384 B, signature 384 B, gspBootArgsSysmemOffset u64. GB10B uses
46 * version 1 (g_kernel_sec2_nvoc.c:478-482), size = 860
47 * (kernel_sec2_gb10b.c:580-581), signature and key sizes 384
48 * (g_kernel_sec2_nvoc.c:472-476). The driver zero-fills the payload and
49 * sets only gspFmcSysmemOffset, frtsSysmemSize (= the GSP-FMC image
50 * size, sic), hash384, signature, publicKey and gspBootArgsSysmemOffset
51 * (kernel_sec2_gb10b.c:507-532, :578); frtsSysmemOffset and the two
52 * frtsVidmem fields stay 0.
53 * - After the packet is written at EMEM offset 0 with auto-increment,
54 * QUEUE_HEAD = 0 and QUEUE_TAIL = padded size - 4 (kernel_sec2_gb10b.c:
55 * 700-732); SEC2 sets HEAD = TAIL when it has taken the packet
56 * (:344-356) and MAILBOX0 leaves 0xc001cafe when the command is handled
57 * (:285-297).
58 * - Boot arguments the payload points at: GSP_FMC_BOOT_PARAMS
59 * (src/nvidia/arch/nvalloc/common/inc/gsp/gspifpub.h), natural C layout
60 * (not packed), 88 bytes on the AArch64 LP64 ABI the driver and this
61 * host share; filled by kgspSetupGspFmcArgs_GH100
62 * (kernel_gsp_gh100.c:415-470): descriptor = GspFwWprMeta (256 bytes,
63 * gsp_fw_wpr_meta.h) on a fresh boot, bIsGspRmBoot = true, bootArgs =
64 * the libos init arguments, SPDM and flush fields only with CC.
65 *
66 * Refusals this tool enforces when encoding or decoding (reason strings are
67 * the test oracle): zero or unaligned image address, address beyond the DMA
68 * window, zero image size, hash/key/signature length not 48/384/384, zero
69 * boot-args address, payload version or size field wrong, non-zero
70 * reserved FRTS fields, payload that does not fit one packet, header dwords
71 * that are not the single-packet COT shape, packet length not a dword
72 * multiple, DMA target out of range, descriptor size not 256, boot flag
73 * not set.
74 *
75 * INFERRED (not a driver check): the image address must be 4 KiB aligned
76 * (the driver maps NV_ALIGN_UP(size, 0x1000) bytes of page-granular system
77 * memory, kernel_sec2_gb10b.c:489-495) and every system address must fit
78 * the 41-bit DMA window /proc/driver/nvidia/gpus reports for this GPU
79 * (--dma-bits overrides).
80 *
81 * ck_gsp_cot_encode selftest layout, golden headers, round trips,
82 * every mutation and truncation refused
83 * ck_gsp_cot_encode layout print the two layouts as "field offset size"
84 * ck_gsp_cot_encode encode --fmc-addr HEX --fmc-size N --hash FILE
85 * --key FILE --sig FILE --bootargs-addr HEX [--dma-bits N] [--out FILE]
86 * build the packet; prints header words,
87 * sizes, tail offset and SHA-256
88 * Hosted C, no outside libraries (SHA-256 is the kernel's own, native/argus).
89 * Never part of an image. No Python. */
90#include <stddef.h>
91#include <stdint.h>
92#include <stdio.h>
93#include <stdlib.h>
94#include <string.h>
95
96#include "sha256.h"
97
98/* ---- constants from the driver (SOURCE lines in the header comment) ---- */
99#define EMEM_CHANNEL_BYTES 1024u
100#define PACKET_HEADER_BYTES 8u
101#define PACKET_PAYLOAD_CAP (EMEM_CHANNEL_BYTES - PACKET_HEADER_BYTES)
102#define NVDM_TYPE_COT 0x14u
103#define MCTP_MSG_TYPE_VENDOR_PCI 0x7eu
104#define MCTP_VENDOR_ID_NV 0x10deu
105#define COT_VERSION_GB10B 1u
106#define COT_HASH_BYTES 48u
107#define COT_KEY_BYTES 384u
108#define COT_SIG_BYTES 384u
109#define COT_PAYLOAD_BYTES 860u
110#define COT_PACKET_BYTES (PACKET_HEADER_BYTES + COT_PAYLOAD_BYTES) /* 868 */
111#define FMC_PARAMS_BYTES 88u
112#define WPR_META_BYTES 256u
113#define DMA_BITS_DEFAULT 41u
114#define GSP_DMA_TARGET_COUNT 3u
115
116/* NVDM_PAYLOAD_COT offsets (packed). */
117#define COT_OFF_VERSION 0u
118#define COT_OFF_SIZE 2u
119#define COT_OFF_FMC_ADDR 4u
120#define COT_OFF_FRTS_SYSMEM_OFF 12u
121#define COT_OFF_FRTS_SYSMEM_SIZE 20u
122#define COT_OFF_FRTS_VIDMEM_OFF 24u
123#define COT_OFF_FRTS_VIDMEM_SIZE 32u
124#define COT_OFF_HASH 36u
125#define COT_OFF_KEY 84u
126#define COT_OFF_SIG 468u
127#define COT_OFF_BOOTARGS_ADDR 852u
128
129/* GSP_FMC_BOOT_PARAMS offsets (natural layout, LP64). */
130#define FMC_OFF_REGKEYS 0u
131#define FMC_OFF_BOOT_TARGET 8u
132#define FMC_OFF_BOOT_DESC_SIZE 12u
133#define FMC_OFF_BOOT_DESC_OFFSET 16u
134#define FMC_OFF_BOOT_WPR_OFFSET 24u
135#define FMC_OFF_BOOT_WPR_SIZE 32u
136#define FMC_OFF_BOOT_IS_GSP_RM 36u
137#define FMC_OFF_RM_TARGET 40u
138#define FMC_OFF_RM_BOOTARGS 48u
139#define FMC_OFF_SPDM_TARGET 56u
140#define FMC_OFF_SPDM_PAYLOAD_OFF 64u
141#define FMC_OFF_SPDM_PAYLOAD_SIZE 72u
142#define FMC_OFF_FLUSH_LO 80u
143#define FMC_OFF_FLUSH_HI 84u
144
145/* Mirrors of the driver's declarations, used only to check the offsets
146 * above against the compiler's own layout at self-test time. */
147#pragma pack(1)
148typedef struct {
149 uint16_t version;
150 uint16_t size;
151 uint64_t gspFmcSysmemOffset;
152 uint64_t frtsSysmemOffset;
153 uint32_t frtsSysmemSize;
154 uint64_t frtsVidmemOffset;
155 uint32_t frtsVidmemSize;
156 uint32_t hash384[12];
157 uint32_t publicKey[96];
158 uint32_t signature[96];
159 uint64_t gspBootArgsSysmemOffset;
160} mirror_cot_payload;
161#pragma pack()
162typedef enum { M_TARGET_A, M_TARGET_B, M_TARGET_C, M_TARGET_COUNT } mirror_dma_target;
163typedef struct { uint32_t regkeys; } mirror_init_params;
164typedef struct {
165 mirror_dma_target target;
166 uint32_t gspRmDescSize;
167 uint64_t gspRmDescOffset;
168 uint64_t wprCarveoutOffset;
169 uint32_t wprCarveoutSize;
170 uint8_t bIsGspRmBoot;
171} mirror_acr_params;
172typedef struct { mirror_dma_target target; uint64_t bootArgsOffset; } mirror_rm_params;
173typedef struct { mirror_dma_target target; uint64_t payloadBufferOffset; uint32_t payloadBufferSize; } mirror_spdm_params;
174typedef struct { uint32_t flushSysmemAddrValLo; uint32_t flushSysmemAddrValHi; } mirror_mem_params;
175typedef struct {
176 mirror_init_params initParams;
177 mirror_acr_params bootGspRmParams;
178 mirror_rm_params gspRmParams;
179 mirror_spdm_params gspSpdmParams;
180 mirror_mem_params gspRmMemParams;
181} mirror_fmc_boot_params;
182
183/* ---- inputs and results ---- */
184typedef struct {
185 uint64_t fmc_sysmem_addr;
186 uint32_t fmc_size;
187 const uint8_t *hash; size_t hash_len;
188 const uint8_t *key; size_t key_len;
189 const uint8_t *sig; size_t sig_len;
190 uint64_t bootargs_sysmem_addr;
191 unsigned dma_bits; /* 0 = DMA_BITS_DEFAULT */
192} cot_inputs;
193
194typedef struct {
195 uint32_t regkeys;
196 uint32_t desc_target;
197 uint32_t desc_size;
198 uint64_t desc_offset;
199 uint64_t wpr_carveout_offset;
200 uint32_t wpr_carveout_size;
201 uint8_t is_gsp_rm_boot;
202 uint32_t bootargs_target;
203 uint64_t bootargs_offset;
204 uint32_t spdm_target;
205 uint64_t spdm_payload_offset;
206 uint32_t spdm_payload_size;
207 uint32_t flush_lo, flush_hi;
208 unsigned dma_bits;
209} fmc_inputs;
210
211static void put16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); }
212static void put32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); }
213static void put64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); }
214static uint16_t get16(const uint8_t *p) { return (uint16_t)(p[0] | (p[1] << 8)); }
215static 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; }
216static 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; }
217
218static int addr_ok(uint64_t a, unsigned dma_bits, int need_page_align, const char **why, const char *what) {
219 if (dma_bits == 0) dma_bits = DMA_BITS_DEFAULT;
220 if (a == 0) { *why = "zero address"; return 0; }
221 if (dma_bits < 64 && (a >> dma_bits) != 0) { *why = "address beyond the DMA window"; return 0; }
222 if (need_page_align && (a & 0xfffu) != 0) { *why = "image address not 4 KiB aligned"; return 0; }
223 (void)what;
224 return 1;
225}
226
227/* ---- headers ---- */
228/* The MCTP dword the driver actually sends on GB10B: kernel_sec2.c:320 via ksec2CreateMctpHeader_HAL; on GB10B the HAL is ksec2CreateMctpHeader_539ab4 = `return 1;` (g_kernel_sec2_nvoc.c:786-788, g_kernel_sec2_nvoc.h:853-855); only GB20B/GB20C build SOM|EOM (kernel_sec2_gb20b.c:206-219).
229 * cot_mctp_header() below is the GB20B form, kept with its goldens for a future GB20B package. */
230#define GB10B_MCTP_DWORD 0x00000001u
231int cot_mctp_header(unsigned som, unsigned eom, unsigned seid, unsigned seq, uint32_t *out, const char **why) {
232 if (som > 1 || eom > 1) { *why = "som/eom not a bit"; return -1; }
233 if (seid > 0xff) { *why = "seid out of range"; return -1; }
234 if (seq > 3) { *why = "seq out of range"; return -1; }
235 *out = ((uint32_t)som << 31) | ((uint32_t)eom << 30) | ((uint32_t)seq << 28) | ((uint32_t)seid << 16);
236 return 0;
237}
238int cot_nvdm_header(unsigned nvdm_type, uint32_t *out, const char **why) {
239 if (nvdm_type > 0xff) { *why = "nvdm type out of range"; return -1; }
240 *out = MCTP_MSG_TYPE_VENDOR_PCI | (MCTP_VENDOR_ID_NV << 8) | ((uint32_t)nvdm_type << 24);
241 return 0;
242}
243
244/* ---- NVDM_PAYLOAD_COT version 1 ---- */
245int cot_payload_encode(const cot_inputs *in, uint8_t out[COT_PAYLOAD_BYTES], const char **why) {
246 if (!in || !out) { *why = "null argument"; return -1; }
247 if (!addr_ok(in->fmc_sysmem_addr, in->dma_bits, 1, why, "fmc")) return -1;
248 if (in->fmc_size == 0) { *why = "zero image size"; return -1; }
249 if (!in->hash || in->hash_len != COT_HASH_BYTES) { *why = "hash length not 48"; return -1; }
250 if (!in->key || in->key_len != COT_KEY_BYTES) { *why = "public key length not 384"; return -1; }
251 if (!in->sig || in->sig_len != COT_SIG_BYTES) { *why = "signature length not 384"; return -1; }
252 if (!addr_ok(in->bootargs_sysmem_addr, in->dma_bits, 0, why, "bootargs")) return -1;
253 memset(out, 0, COT_PAYLOAD_BYTES);
254 put16(out + COT_OFF_VERSION, COT_VERSION_GB10B);
255 put16(out + COT_OFF_SIZE, COT_PAYLOAD_BYTES);
256 put64(out + COT_OFF_FMC_ADDR, in->fmc_sysmem_addr);
257 put32(out + COT_OFF_FRTS_SYSMEM_SIZE, in->fmc_size);
258 memcpy(out + COT_OFF_HASH, in->hash, COT_HASH_BYTES);
259 memcpy(out + COT_OFF_KEY, in->key, COT_KEY_BYTES);
260 memcpy(out + COT_OFF_SIG, in->sig, COT_SIG_BYTES);
261 put64(out + COT_OFF_BOOTARGS_ADDR, in->bootargs_sysmem_addr);
262 return 0;
263}
264/* Decode and re-check. On success the hash/key/sig pointers point into buf. */
265int cot_payload_decode(const uint8_t *buf, size_t len, cot_inputs *out, unsigned dma_bits, const char **why) {
266 if (!buf || !out) { *why = "null argument"; return -1; }
267 if (len != COT_PAYLOAD_BYTES) { *why = "payload length not 860"; return -1; }
268 if (get16(buf + COT_OFF_VERSION) != COT_VERSION_GB10B) { *why = "payload version not 1"; return -1; }
269 if (get16(buf + COT_OFF_SIZE) != COT_PAYLOAD_BYTES) { *why = "payload size field not 860"; return -1; }
270 if (get64(buf + COT_OFF_FRTS_SYSMEM_OFF) != 0) { *why = "frtsSysmemOffset not zero"; return -1; }
271 if (get64(buf + COT_OFF_FRTS_VIDMEM_OFF) != 0) { *why = "frtsVidmemOffset not zero"; return -1; }
272 if (get32(buf + COT_OFF_FRTS_VIDMEM_SIZE) != 0) { *why = "frtsVidmemSize not zero"; return -1; }
273 memset(out, 0, sizeof *out);
274 out->fmc_sysmem_addr = get64(buf + COT_OFF_FMC_ADDR);
275 out->fmc_size = get32(buf + COT_OFF_FRTS_SYSMEM_SIZE);
276 out->hash = buf + COT_OFF_HASH; out->hash_len = COT_HASH_BYTES;
277 out->key = buf + COT_OFF_KEY; out->key_len = COT_KEY_BYTES;
278 out->sig = buf + COT_OFF_SIG; out->sig_len = COT_SIG_BYTES;
279 out->bootargs_sysmem_addr = get64(buf + COT_OFF_BOOTARGS_ADDR);
280 out->dma_bits = dma_bits;
281 if (!addr_ok(out->fmc_sysmem_addr, dma_bits, 1, why, "fmc")) return -1;
282 if (out->fmc_size == 0) { *why = "zero image size"; return -1; }
283 if (!addr_ok(out->bootargs_sysmem_addr, dma_bits, 0, why, "bootargs")) return -1;
284 return 0;
285}
286
287/* ---- one EMEM packet ---- */
288int cot_packet_encode(const uint8_t *payload, size_t len, unsigned nvdm_type, uint8_t out[EMEM_CHANNEL_BYTES],
289 size_t *out_len, uint32_t *tail_offset, const char **why) {
290 uint32_t h0, h1;
291 if (!payload || !out || !out_len || !tail_offset) { *why = "null argument"; return -1; }
292 if (len == 0) { *why = "empty payload"; return -1; }
293 if (len > PACKET_PAYLOAD_CAP) { *why = "payload does not fit one packet"; return -1; }
294 h0 = GB10B_MCTP_DWORD;
295 if (cot_nvdm_header(nvdm_type, &h1, why) != 0) return -1;
296 memset(out, 0, EMEM_CHANNEL_BYTES);
297 put32(out, h0);
298 put32(out + 4, h1);
299 memcpy(out + PACKET_HEADER_BYTES, payload, len);
300 *out_len = (PACKET_HEADER_BYTES + len + 3u) & ~3u; /* NV_ALIGN_UP(size, 4) */
301 *tail_offset = (uint32_t)(*out_len - 4u); /* TAIL = last dword written */
302 return 0;
303}
304/* Accepts exactly the single-packet COT shape. payload_out points into buf. */
305int cot_packet_decode(const uint8_t *buf, size_t len, const uint8_t **payload_out, size_t *payload_len, const char **why) {
306 uint32_t h0, h1, want;
307 if (!buf || !payload_out || !payload_len) { *why = "null argument"; return -1; }
308 if (len < PACKET_HEADER_BYTES + 1) { *why = "packet shorter than the headers"; return -1; }
309 if (len > EMEM_CHANNEL_BYTES) { *why = "packet larger than the channel"; return -1; }
310 if ((len & 3u) != 0) { *why = "packet length not a dword multiple"; return -1; }
311 h0 = get32(buf); h1 = get32(buf + 4);
312 if (h0 != GB10B_MCTP_DWORD) { *why = "mctp dword not the GB10B constant 1"; return -1; }
313 cot_nvdm_header(NVDM_TYPE_COT, &want, why);
314 if (h1 != want) {
315 if ((h1 & 0x7fu) != MCTP_MSG_TYPE_VENDOR_PCI) { *why = "nvdm msg type not vendor-pci"; return -1; }
316 if (((h1 >> 8) & 0xffffu) != MCTP_VENDOR_ID_NV) { *why = "nvdm vendor id not nvidia"; return -1; }
317 if ((h1 >> 24) != NVDM_TYPE_COT) { *why = "nvdm type not COT"; return -1; }
318 *why = "nvdm IC bit set"; return -1;
319 }
320 *payload_out = buf + PACKET_HEADER_BYTES;
321 *payload_len = len - PACKET_HEADER_BYTES;
322 return 0;
323}
324
325/* ---- GSP_FMC_BOOT_PARAMS ---- */
326int fmc_params_encode(const fmc_inputs *in, uint8_t out[FMC_PARAMS_BYTES], const char **why) {
327 if (!in || !out) { *why = "null argument"; return -1; }
328 if (in->desc_target >= GSP_DMA_TARGET_COUNT) { *why = "descriptor DMA target out of range"; return -1; }
329 if (in->desc_size != WPR_META_BYTES) { *why = "descriptor size not 256"; return -1; }
330 if (!addr_ok(in->desc_offset, in->dma_bits, 0, why, "desc")) return -1;
331 if (in->is_gsp_rm_boot != 1) { *why = "bIsGspRmBoot not set"; return -1; }
332 if (in->bootargs_target >= GSP_DMA_TARGET_COUNT) { *why = "boot-args DMA target out of range"; return -1; }
333 if (!addr_ok(in->bootargs_offset, in->dma_bits, 0, why, "bootargs")) return -1;
334 if (in->spdm_target >= GSP_DMA_TARGET_COUNT) { *why = "SPDM DMA target out of range"; return -1; }
335 memset(out, 0, FMC_PARAMS_BYTES);
336 put32(out + FMC_OFF_REGKEYS, in->regkeys);
337 put32(out + FMC_OFF_BOOT_TARGET, in->desc_target);
338 put32(out + FMC_OFF_BOOT_DESC_SIZE, in->desc_size);
339 put64(out + FMC_OFF_BOOT_DESC_OFFSET, in->desc_offset);
340 put64(out + FMC_OFF_BOOT_WPR_OFFSET, in->wpr_carveout_offset);
341 put32(out + FMC_OFF_BOOT_WPR_SIZE, in->wpr_carveout_size);
342 out[FMC_OFF_BOOT_IS_GSP_RM] = in->is_gsp_rm_boot;
343 put32(out + FMC_OFF_RM_TARGET, in->bootargs_target);
344 put64(out + FMC_OFF_RM_BOOTARGS, in->bootargs_offset);
345 put32(out + FMC_OFF_SPDM_TARGET, in->spdm_target);
346 put64(out + FMC_OFF_SPDM_PAYLOAD_OFF, in->spdm_payload_offset);
347 put32(out + FMC_OFF_SPDM_PAYLOAD_SIZE, in->spdm_payload_size);
348 put32(out + FMC_OFF_FLUSH_LO, in->flush_lo);
349 put32(out + FMC_OFF_FLUSH_HI, in->flush_hi);
350 return 0;
351}
352int fmc_params_decode(const uint8_t *buf, size_t len, fmc_inputs *out, unsigned dma_bits, const char **why) {
353 if (!buf || !out) { *why = "null argument"; return -1; }
354 if (len != FMC_PARAMS_BYTES) { *why = "params length not 88"; return -1; }
355 memset(out, 0, sizeof *out);
356 out->regkeys = get32(buf + FMC_OFF_REGKEYS);
357 out->desc_target = get32(buf + FMC_OFF_BOOT_TARGET);
358 out->desc_size = get32(buf + FMC_OFF_BOOT_DESC_SIZE);
359 out->desc_offset = get64(buf + FMC_OFF_BOOT_DESC_OFFSET);
360 out->wpr_carveout_offset = get64(buf + FMC_OFF_BOOT_WPR_OFFSET);
361 out->wpr_carveout_size = get32(buf + FMC_OFF_BOOT_WPR_SIZE);
362 out->is_gsp_rm_boot = buf[FMC_OFF_BOOT_IS_GSP_RM];
363 out->bootargs_target = get32(buf + FMC_OFF_RM_TARGET);
364 out->bootargs_offset = get64(buf + FMC_OFF_RM_BOOTARGS);
365 out->spdm_target = get32(buf + FMC_OFF_SPDM_TARGET);
366 out->spdm_payload_offset = get64(buf + FMC_OFF_SPDM_PAYLOAD_OFF);
367 out->spdm_payload_size = get32(buf + FMC_OFF_SPDM_PAYLOAD_SIZE);
368 out->flush_lo = get32(buf + FMC_OFF_FLUSH_LO);
369 out->flush_hi = get32(buf + FMC_OFF_FLUSH_HI);
370 out->dma_bits = dma_bits;
371 /* padding bytes must be zero: 4..7, 37..39, 44..47, 60..63, 76..79 */
372 for (size_t i = 0; i < len; i++) {
373 int pad = (i >= 4 && i < 8) || (i >= 37 && i < 40) || (i >= 44 && i < 48) || (i >= 60 && i < 64) || (i >= 76 && i < 80);
374 if (pad && buf[i] != 0) { *why = "padding byte not zero"; return -1; }
375 }
376 uint8_t re[FMC_PARAMS_BYTES];
377 if (fmc_params_encode(out, re, why) != 0) return -1;
378 if (memcmp(re, buf, FMC_PARAMS_BYTES) != 0) { *why = "re-encode differs"; return -1; }
379 return 0;
380}
381
382/* ---- self-test ---- */
383static int fails;
384static void check(int ok, const char *what) {
385 printf(" %s %s\n", ok ? "ok " : "FAIL", what);
386 if (!ok) fails++;
387}
388static void expect_refused_rc(int rc, const char *why, const char *want, const char *what) {
389 int ok = rc != 0 && why && strcmp(why, want) == 0;
390 printf(" %s refused: %s", ok ? "ok " : "FAIL", what);
391 if (!ok) printf(" (rc=%d why=%s want=%s)", rc, why ? why : "(null)", want);
392 printf("\n");
393 if (!ok) fails++;
394}
395/* The call that writes the reason through &why must finish before why is read. As two arguments
396 * of one function call their evaluation order is unspecified (C11 6.5.2.2p10): x86-64 gcc 13 read
397 * why first and every check saw the previous reason (#483). The statement boundary sequences them. */
398#define expect_refused(call, why, want, what) \
399 do { int expect_rc_ = (call); expect_refused_rc(expect_rc_, (why), (want), (what)); } while (0)
400
401static int selftest(void) {
402 const char *why = "";
403 printf("CK_GSP_COT_ENCODE_SELFTEST: begin\n");
404
405 /* 1. layouts against the compiler's own view of the mirrored structs */
406 check(sizeof(mirror_cot_payload) == COT_PAYLOAD_BYTES, "NVDM_PAYLOAD_COT is 860 bytes");
407 check(offsetof(mirror_cot_payload, version) == COT_OFF_VERSION &&
408 offsetof(mirror_cot_payload, size) == COT_OFF_SIZE &&
409 offsetof(mirror_cot_payload, gspFmcSysmemOffset) == COT_OFF_FMC_ADDR &&
410 offsetof(mirror_cot_payload, frtsSysmemOffset) == COT_OFF_FRTS_SYSMEM_OFF &&
411 offsetof(mirror_cot_payload, frtsSysmemSize) == COT_OFF_FRTS_SYSMEM_SIZE &&
412 offsetof(mirror_cot_payload, frtsVidmemOffset) == COT_OFF_FRTS_VIDMEM_OFF &&
413 offsetof(mirror_cot_payload, frtsVidmemSize) == COT_OFF_FRTS_VIDMEM_SIZE &&
414 offsetof(mirror_cot_payload, hash384) == COT_OFF_HASH &&
415 offsetof(mirror_cot_payload, publicKey) == COT_OFF_KEY &&
416 offsetof(mirror_cot_payload, signature) == COT_OFF_SIG &&
417 offsetof(mirror_cot_payload, gspBootArgsSysmemOffset) == COT_OFF_BOOTARGS_ADDR,
418 "NVDM_PAYLOAD_COT field offsets");
419 check(sizeof(mirror_fmc_boot_params) == FMC_PARAMS_BYTES && sizeof(mirror_dma_target) == 4, "GSP_FMC_BOOT_PARAMS is 88 bytes");
420 check(offsetof(mirror_fmc_boot_params, initParams) == FMC_OFF_REGKEYS &&
421 offsetof(mirror_fmc_boot_params, bootGspRmParams) == FMC_OFF_BOOT_TARGET &&
422 offsetof(mirror_fmc_boot_params, bootGspRmParams.gspRmDescSize) == FMC_OFF_BOOT_DESC_SIZE &&
423 offsetof(mirror_fmc_boot_params, bootGspRmParams.gspRmDescOffset) == FMC_OFF_BOOT_DESC_OFFSET &&
424 offsetof(mirror_fmc_boot_params, bootGspRmParams.wprCarveoutOffset) == FMC_OFF_BOOT_WPR_OFFSET &&
425 offsetof(mirror_fmc_boot_params, bootGspRmParams.wprCarveoutSize) == FMC_OFF_BOOT_WPR_SIZE &&
426 offsetof(mirror_fmc_boot_params, bootGspRmParams.bIsGspRmBoot) == FMC_OFF_BOOT_IS_GSP_RM &&
427 offsetof(mirror_fmc_boot_params, gspRmParams) == FMC_OFF_RM_TARGET &&
428 offsetof(mirror_fmc_boot_params, gspRmParams.bootArgsOffset) == FMC_OFF_RM_BOOTARGS &&
429 offsetof(mirror_fmc_boot_params, gspSpdmParams) == FMC_OFF_SPDM_TARGET &&
430 offsetof(mirror_fmc_boot_params, gspSpdmParams.payloadBufferOffset) == FMC_OFF_SPDM_PAYLOAD_OFF &&
431 offsetof(mirror_fmc_boot_params, gspSpdmParams.payloadBufferSize) == FMC_OFF_SPDM_PAYLOAD_SIZE &&
432 offsetof(mirror_fmc_boot_params, gspRmMemParams) == FMC_OFF_FLUSH_LO &&
433 offsetof(mirror_fmc_boot_params, gspRmMemParams.flushSysmemAddrValHi) == FMC_OFF_FLUSH_HI,
434 "GSP_FMC_BOOT_PARAMS field offsets");
435
436 /* 2. golden header words */
437 uint32_t h;
438 check(cot_mctp_header(1, 1, 0, 0, &h, &why) == 0 && h == 0xC0000000u, "MCTP first+last packet, seid 0, seq 0 = 0xC0000000");
439 check(cot_mctp_header(0, 1, 0, 3, &h, &why) == 0 && h == 0x70000000u, "MCTP last continuation seq 3 = 0x70000000");
440 check(cot_mctp_header(0, 0, 0, 1, &h, &why) == 0 && h == 0x10000000u, "MCTP middle packet seq 1 = 0x10000000");
441 check(cot_mctp_header(1, 1, 0x2a, 0, &h, &why) == 0 && h == 0xC02A0000u, "MCTP seid lands in bits 23:16");
442 check(cot_nvdm_header(NVDM_TYPE_COT, &h, &why) == 0 && h == 0x1410DE7Eu, "NVDM header for COT = 0x1410DE7E");
443 expect_refused(cot_mctp_header(1, 1, 0, 4, &h, &why), why, "seq out of range", "mctp seq 4");
444 expect_refused(cot_mctp_header(2, 1, 0, 0, &h, &why), why, "som/eom not a bit", "mctp som 2");
445 expect_refused(cot_mctp_header(1, 1, 256, 0, &h, &why), why, "seid out of range", "mctp seid 256");
446 expect_refused(cot_nvdm_header(256, &h, &why), why, "nvdm type out of range", "nvdm type 256");
447
448 /* 3. payload round trip */
449 static uint8_t hash[COT_HASH_BYTES], key[COT_KEY_BYTES], sig[COT_SIG_BYTES];
450 for (unsigned i = 0; i < COT_HASH_BYTES; i++) hash[i] = (uint8_t)(0x10 + i);
451 for (unsigned i = 0; i < COT_KEY_BYTES; i++) key[i] = (uint8_t)(0x20 + i);
452 for (unsigned i = 0; i < COT_SIG_BYTES; i++) sig[i] = (uint8_t)(0x30 + i);
453 cot_inputs in = { 0x0000012345670000ull, 195912u, hash, COT_HASH_BYTES, key, COT_KEY_BYTES, sig, COT_SIG_BYTES, 0x00000123456a0000ull, 0 };
454 uint8_t pl[COT_PAYLOAD_BYTES], pkt[EMEM_CHANNEL_BYTES];
455 size_t pkt_len = 0; uint32_t tail = 0;
456 check(cot_payload_encode(&in, pl, &why) == 0, "payload encodes");
457 check(get16(pl) == 1 && get16(pl + 2) == 860, "payload version 1 size 860");
458 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");
459 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");
460 check(get64(pl + COT_OFF_BOOTARGS_ADDR) == in.bootargs_sysmem_addr, "boot-args address in place");
461 { 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"); }
462 cot_inputs back;
463 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 &&
464 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,
465 "payload decodes to the same fields");
466 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,
467 "packet is 868 bytes, tail dword offset 864");
468 check(get32(pkt) == 0x00000001u && get32(pkt + 4) == 0x1410DE7Eu && memcmp(pkt + 8, pl, sizeof pl) == 0, "packet = headers + payload");
469 { 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"); }
470 { 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"); }
471
472 /* 4. payload encode refusals, one field at a time */
473 cot_inputs m;
474 m = in; m.fmc_sysmem_addr = 0; expect_refused(cot_payload_encode(&m, pl, &why), why, "zero address", "image address 0");
475 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");
476 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");
477 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");
478 m = in; m.fmc_size = 0; expect_refused(cot_payload_encode(&m, pl, &why), why, "zero image size", "image size 0");
479 m = in; m.hash_len = 47; expect_refused(cot_payload_encode(&m, pl, &why), why, "hash length not 48", "hash 47 bytes");
480 m = in; m.hash = NULL; expect_refused(cot_payload_encode(&m, pl, &why), why, "hash length not 48", "hash null");
481 m = in; m.key_len = 385; expect_refused(cot_payload_encode(&m, pl, &why), why, "public key length not 384", "key 385 bytes");
482 m = in; m.sig_len = 0; expect_refused(cot_payload_encode(&m, pl, &why), why, "signature length not 384", "signature empty");
483 m = in; m.bootargs_sysmem_addr = 0; expect_refused(cot_payload_encode(&m, pl, &why), why, "zero address", "boot-args address 0");
484 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");
485 expect_refused(cot_payload_encode(NULL, pl, &why), why, "null argument", "null inputs");
486
487 /* 5. payload decode refusals */
488 uint8_t d[COT_PAYLOAD_BYTES];
489 cot_payload_encode(&in, pl, &why);
490#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)
491 expect_refused(cot_payload_decode(pl, 859, &back, 0, &why), why, "payload length not 860", "payload 859 bytes");
492 expect_refused(cot_payload_decode(pl, 861, &back, 0, &why), why, "payload length not 860", "payload 861 bytes");
493 MUT(put16(d, 2), "payload version not 1", "version 2");
494 MUT(put16(d, 0), "payload version not 1", "version 0");
495 MUT(put16(d + 2, 859), "payload size field not 860", "size field 859");
496 MUT(put64(d + COT_OFF_FRTS_SYSMEM_OFF, 0x1000), "frtsSysmemOffset not zero", "frtsSysmemOffset set");
497 MUT(put64(d + COT_OFF_FRTS_VIDMEM_OFF, 1), "frtsVidmemOffset not zero", "frtsVidmemOffset set");
498 MUT(put32(d + COT_OFF_FRTS_VIDMEM_SIZE, 1), "frtsVidmemSize not zero", "frtsVidmemSize set");
499 MUT(put64(d + COT_OFF_FMC_ADDR, 0), "zero address", "decoded image address 0");
500 MUT(put64(d + COT_OFF_FMC_ADDR, 0x1010), "image address not 4 KiB aligned", "decoded image address unaligned");
501 MUT(put64(d + COT_OFF_FMC_ADDR, 1ull << 41), "address beyond the DMA window", "decoded image address at 2^41");
502 MUT(put32(d + COT_OFF_FRTS_SYSMEM_SIZE, 0), "zero image size", "decoded image size 0");
503 MUT(put64(d + COT_OFF_BOOTARGS_ADDR, 0), "zero address", "decoded boot-args 0");
504#undef MUT
505
506 /* 6. packet refusals */
507 cot_packet_encode(pl, sizeof pl, NVDM_TYPE_COT, pkt, &pkt_len, &tail, &why);
508 { uint8_t big[PACKET_PAYLOAD_CAP + 1]; size_t n; uint32_t t; memset(big, 0, sizeof big);
509 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");
510 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");
511 expect_refused(cot_packet_encode(big, 0, NVDM_TYPE_COT, pkt, &n, &t, &why), why, "empty payload", "payload 0 bytes");
512 cot_packet_encode(pl, sizeof pl, NVDM_TYPE_COT, pkt, &pkt_len, &tail, &why); }
513 uint8_t q[EMEM_CHANNEL_BYTES]; const uint8_t *p; size_t n;
514#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)
515 PMUT(put32(q, 0xC0000000u), "mctp dword not the GB10B constant 1", "GB20B-form SOM|EOM header");
516 PMUT(put32(q, 0x00000000u), "mctp dword not the GB10B constant 1", "zero header");
517 PMUT(put32(q, 0x00000003u), "mctp dword not the GB10B constant 1", "constant plus one bit");
518 PMUT(put32(q + 4, 0x1510DE7Eu), "nvdm type not COT", "nvdm type 0x15 (FSP_RESPONSE)");
519 PMUT(put32(q + 4, 0x1410DF7Eu), "nvdm vendor id not nvidia", "vendor id off by one");
520 PMUT(put32(q + 4, 0x1410DE7Fu), "nvdm msg type not vendor-pci", "msg type 0x7f");
521 PMUT(put32(q + 4, 0x1410DEFEu), "nvdm IC bit set", "IC bit set");
522#undef PMUT
523 expect_refused(cot_packet_decode(pkt, pkt_len - 1, &p, &n, &why), why, "packet length not a dword multiple", "packet 867 bytes");
524 expect_refused(cot_packet_decode(pkt, 1028, &p, &n, &why), why, "packet larger than the channel", "packet 1028 bytes");
525 { 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; }
526 check(all, "every truncation 0..8 refused"); }
527 { const uint8_t *pp; size_t nn; cot_packet_decode(pkt, pkt_len, &pp, &nn, &why);
528 expect_refused(cot_payload_decode(pp, nn - 4, &back, 0, &why), why, "payload length not 860", "packet with a short payload"); }
529
530 /* 7. FMC boot params round trip and refusals */
531 fmc_inputs f = { 0, 1, WPR_META_BYTES, 0x0000012345680000ull, 0, 0, 1, 1, 0x00000123456c0000ull, 0, 0, 0, 0, 0, 0 };
532 uint8_t fb[FMC_PARAMS_BYTES]; fmc_inputs fback;
533 check(fmc_params_encode(&f, fb, &why) == 0, "FMC params encode");
534 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 &&
535 fb[FMC_OFF_BOOT_IS_GSP_RM] == 1 && get64(fb + FMC_OFF_RM_BOOTARGS) == f.bootargs_offset, "FMC params fields in place");
536 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");
537 fmc_inputs g;
538 g = f; g.desc_target = 3; expect_refused(fmc_params_encode(&g, fb, &why), why, "descriptor DMA target out of range", "desc target 3");
539 g = f; g.desc_size = 255; expect_refused(fmc_params_encode(&g, fb, &why), why, "descriptor size not 256", "desc size 255");
540 g = f; g.desc_offset = 0; expect_refused(fmc_params_encode(&g, fb, &why), why, "zero address", "desc offset 0");
541 g = f; g.is_gsp_rm_boot = 0; expect_refused(fmc_params_encode(&g, fb, &why), why, "bIsGspRmBoot not set", "boot flag clear");
542 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");
543 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");
544 g = f; g.spdm_target = 7; expect_refused(fmc_params_encode(&g, fb, &why), why, "SPDM DMA target out of range", "SPDM target 7");
545 fmc_params_encode(&f, fb, &why);
546 { 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");
547 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");
548 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");
549 expect_refused(fmc_params_decode(fb, 87, &fback, 0, &why), why, "params length not 88", "params 87 bytes"); }
550
551 printf("CK_GSP_COT_ENCODE_SELFTEST: %s (%d failures)\n", fails ? "FAIL" : "PASS", fails);
552 return fails ? 1 : 0;
553}
554
555static void layout(void) {
556 printf("NVDM_PAYLOAD_COT version 1, packed, %u bytes (kern_fsp_cot_payload.h:13-28)\n", COT_PAYLOAD_BYTES);
557 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",
558 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);
559 printf("packet: mctp 0x00000001 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);
560 printf("GSP_FMC_BOOT_PARAMS natural layout, %u bytes (gspifpub.h)\n", FMC_PARAMS_BYTES);
561 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",
562 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);
563}
564
565static uint8_t *read_file(const char *path, size_t *len) {
566 FILE *f = fopen(path, "rb");
567 if (!f) return NULL;
568 uint8_t *buf = malloc(4096);
569 size_t cap = 4096, n = 0;
570 if (!buf) { fclose(f); return NULL; }
571 for (;;) {
572 size_t r = fread(buf + n, 1, cap - n, f);
573 n += r;
574 if (n < cap) break;
575 uint8_t *nb = realloc(buf, cap * 2);
576 if (!nb) { free(buf); fclose(f); return NULL; }
577 buf = nb; cap *= 2;
578 }
579 fclose(f);
580 *len = n;
581 return buf;
582}
583
584static int encode_cli(int argc, char **argv) {
585 const char *hash_p = NULL, *key_p = NULL, *sig_p = NULL, *out_p = NULL, *why = "";
586 cot_inputs in; memset(&in, 0, sizeof in);
587 for (int i = 2; i < argc; i++) {
588 const char *a = argv[i], *v = (i + 1 < argc) ? argv[i + 1] : NULL;
589 if (!v) { fprintf(stderr, "missing value for %s\n", a); return 2; }
590 if (!strcmp(a, "--fmc-addr")) in.fmc_sysmem_addr = strtoull(v, NULL, 16);
591 else if (!strcmp(a, "--fmc-size")) in.fmc_size = (uint32_t)strtoul(v, NULL, 0);
592 else if (!strcmp(a, "--hash")) hash_p = v;
593 else if (!strcmp(a, "--key")) key_p = v;
594 else if (!strcmp(a, "--sig")) sig_p = v;
595 else if (!strcmp(a, "--bootargs-addr")) in.bootargs_sysmem_addr = strtoull(v, NULL, 16);
596 else if (!strcmp(a, "--dma-bits")) in.dma_bits = (unsigned)strtoul(v, NULL, 0);
597 else if (!strcmp(a, "--out")) out_p = v;
598 else { fprintf(stderr, "unknown option %s\n", a); return 2; }
599 i++;
600 }
601 if (!hash_p || !key_p || !sig_p) { fprintf(stderr, "need --hash --key --sig\n"); return 2; }
602 size_t hl = 0, kl = 0, sl = 0;
603 uint8_t *hb = read_file(hash_p, &hl), *kb = read_file(key_p, &kl), *sb = read_file(sig_p, &sl);
604 if (!hb || !kb || !sb) { fprintf(stderr, "cannot read an input file\n"); return 2; }
605 in.hash = hb; in.hash_len = hl; in.key = kb; in.key_len = kl; in.sig = sb; in.sig_len = sl;
606 uint8_t pl[COT_PAYLOAD_BYTES], pkt[EMEM_CHANNEL_BYTES];
607 size_t n = 0; uint32_t tail = 0;
608 if (cot_payload_encode(&in, pl, &why) != 0 || cot_packet_encode(pl, sizeof pl, NVDM_TYPE_COT, pkt, &n, &tail, &why) != 0) {
609 printf("RESULT REFUSED %s\n", why);
610 return 2;
611 }
612 uint8_t dg[32];
613 sha256_hash(pkt, n, dg);
614 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 ",
615 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);
616 for (int i = 0; i < 32; i++) printf("%02x", dg[i]);
617 printf("\n");
618 if (out_p) {
619 FILE *f = fopen(out_p, "wb");
620 if (!f || fwrite(pkt, 1, n, f) != n) { printf("RESULT REFUSED cannot write output\n"); if (f) fclose(f); return 2; }
621 fclose(f);
622 }
623 printf("RESULT OK\n");
624 free(hb); free(kb); free(sb);
625 return 0;
626}
627
628int main(int argc, char **argv) {
629 if (argc >= 2 && !strcmp(argv[1], "selftest")) return selftest();
630 if (argc >= 2 && !strcmp(argv[1], "layout")) { layout(); return 0; }
631 if (argc >= 2 && !strcmp(argv[1], "encode")) return encode_cli(argc, argv);
632 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");
633 return 2;
634}