aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / tools / ck_gsp_fw_inspect.c
| 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_fw_inspect -- host tool: inventory and refusal checks for the NVIDIA |
| 18 | * GSP-RM firmware container that the pinned Linux driver (nvidia-open |
| 19 | * 580.173.02) loads for the GB10 (aienos#286 cut C, host work: "inventory of |
| 20 | * firmware and protocol sources, parsers and refusal tests for firmware |
| 21 | * metadata. No loading"). docs/GB10_FIRMWARE_INVENTORY.md is the report this |
| 22 | * tool backs. |
| 23 | * |
| 24 | * The container is an ELF64 relocatable object for RISC-V whose sections carry |
| 25 | * the payload: .fwimage (the GSP-RM image), .fwversion (NUL-terminated driver |
| 26 | * version string), .note.gnu.build-id, and one .fwsignature_<family> section |
| 27 | * per chip family. The driver's own checks that this tool mirrors (file:line |
| 28 | * in open-gpu-kernel-modules tag 580.173.02): |
| 29 | * - section lookup with bounds checks on the section header table, the |
| 30 | * section-name table and each section's offset+size |
| 31 | * (src/nvidia/src/kernel/gpu/gsp/kernel_gsp.c:5091-5170); |
| 32 | * - .fwversion must be NUL-terminated, shorter than 64 bytes and equal to |
| 33 | * NV_VERSION_STRING (kernel_gsp.c:4731-4790); |
| 34 | * - the signature section is ".fwsignature_" + chip family name |
| 35 | * (kernel_gsp.c:4805-4840), and the GB10 (GPU_IMPLEMENTATION_GB10B) maps |
| 36 | * to family "gb10y" (src/nvidia/arch/nvalloc/common/inc/ |
| 37 | * nv-firmware-chip-family-select.h:58-60). |
| 38 | * This tool only reads and hashes bytes. It does not verify NVIDIA's signature |
| 39 | * (the key and format belong to the chip's boot ROM and ACR) and it never |
| 40 | * loads anything onto a device. |
| 41 | * |
| 42 | * ck_gsp_fw_inspect inspect FILE [--family NAME] [--expect-version V] |
| 43 | * Prints one "key value" line per fact (ELF identity, every section with |
| 44 | * offset and size, version string, build id, SHA-256 of the whole file, |
| 45 | * of .fwimage and of the selected signature section) and ends with |
| 46 | * "RESULT OK" (exit 0) or "RESULT REFUSED <reason>" (exit 2). Default |
| 47 | * family is gb10y; default expected version is 580.173.02. |
| 48 | * ck_gsp_fw_inspect selftest |
| 49 | * Builds a small valid container in memory, then mutates it one field at |
| 50 | * a time and checks each mutation is refused for the named reason. |
| 51 | * Exit 0 only when every case holds. |
| 52 | * Hosted C, no outside libraries (SHA-256 is the kernel's own, native/argus). |
| 53 | * Never part of an image. No Python. */ |
| 54 | #include <errno.h> |
| 55 | #include <stdint.h> |
| 56 | #include <stdio.h> |
| 57 | #include <stdlib.h> |
| 58 | #include <string.h> |
| 59 | |
| 60 | #include "sha256.h" |
| 61 | |
| 62 | #define DEFAULT_FAMILY "gb10y" |
| 63 | #define DEFAULT_VERSION "580.173.02" |
| 64 | #define EM_RISCV 243u |
| 65 | #define ET_REL 1u |
| 66 | #define SHT_NOBITS 8u |
| 67 | #define SHDR_SIZE 64u |
| 68 | #define EHDR_SIZE 64u |
| 69 | #define MAX_SECTIONS 256u |
| 70 | #define MAX_REASON 160 |
| 71 | |
| 72 | typedef struct { |
| 73 | const char *name; |
| 74 | uint32_t type; |
| 75 | uint64_t offset; |
| 76 | uint64_t size; |
| 77 | } section; |
| 78 | |
| 79 | typedef struct { |
| 80 | char reason[MAX_REASON]; |
| 81 | size_t nsec; |
| 82 | section sec[MAX_SECTIONS]; |
| 83 | const section *fwimage, *fwversion, *buildid, *fwsig; |
| 84 | char version[64]; |
| 85 | char buildid_hex[129]; |
| 86 | char sig_name[64]; |
| 87 | uint8_t sha_file[32], sha_image[32], sha_sig[32]; |
| 88 | } report; |
| 89 | |
| 90 | static uint16_t rd16(const uint8_t *p) { return (uint16_t)(p[0] | ((uint16_t)p[1] << 8)); } |
| 91 | static uint32_t rd32(const uint8_t *p) { return (uint32_t)rd16(p) | ((uint32_t)rd16(p + 2) << 16); } |
| 92 | static uint64_t rd64(const uint8_t *p) { return (uint64_t)rd32(p) | ((uint64_t)rd32(p + 4) << 32); } |
| 93 | static void wr16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); } |
| 94 | static void wr32(uint8_t *p, uint32_t v) { wr16(p, (uint16_t)v); wr16(p + 2, (uint16_t)(v >> 16)); } |
| 95 | static void wr64(uint8_t *p, uint64_t v) { wr32(p, (uint32_t)v); wr32(p + 4, (uint32_t)(v >> 32)); } |
| 96 | |
| 97 | /* offset + size <= len without overflow (the driver uses portSafeAddU64). */ |
| 98 | static int in_bounds(uint64_t off, uint64_t size, uint64_t len) |
| 99 | { |
| 100 | if (off > len) return 0; |
| 101 | return size <= len - off; |
| 102 | } |
| 103 | |
| 104 | static int refuse(report *r, const char *reason) |
| 105 | { |
| 106 | snprintf(r->reason, sizeof r->reason, "%s", reason); |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | static void hex(const uint8_t *b, size_t n, char *out) |
| 111 | { |
| 112 | static const char d[] = "0123456789abcdef"; |
| 113 | for (size_t i = 0; i < n; i++) { out[2 * i] = d[b[i] >> 4]; out[2 * i + 1] = d[b[i] & 15]; } |
| 114 | out[2 * n] = 0; |
| 115 | } |
| 116 | |
| 117 | /* Parse and check the container. 0 = every check passed, -1 = refused |
| 118 | * (r->reason names the first failed check). Pure: no I/O. */ |
| 119 | static int inspect_buf(const uint8_t *buf, uint64_t len, const char *family, |
| 120 | const char *expect_version, report *r) |
| 121 | { |
| 122 | memset(r, 0, sizeof *r); |
| 123 | if (len < EHDR_SIZE) return refuse(r, "short header: file smaller than an ELF64 header"); |
| 124 | if (!(buf[0] == 0x7f && buf[1] == 'E' && buf[2] == 'L' && buf[3] == 'F')) |
| 125 | return refuse(r, "ELF magic missing"); |
| 126 | if (buf[4] != 2) return refuse(r, "not ELF64 (EI_CLASS)"); |
| 127 | if (buf[5] != 1) return refuse(r, "not little-endian (EI_DATA)"); |
| 128 | if (buf[6] != 1) return refuse(r, "EI_VERSION is not 1"); |
| 129 | if (rd16(buf + 16) != ET_REL) return refuse(r, "e_type is not ET_REL (the pinned container is a relocatable object)"); |
| 130 | if (rd16(buf + 18) != EM_RISCV) return refuse(r, "e_machine is not RISC-V (243)"); |
| 131 | uint64_t shoff = rd64(buf + 40); |
| 132 | uint16_t shentsize = rd16(buf + 58), shnum = rd16(buf + 60), shstrndx = rd16(buf + 62); |
| 133 | if (shentsize != SHDR_SIZE) return refuse(r, "e_shentsize is not 64"); |
| 134 | if (shnum == 0) return refuse(r, "e_shnum is 0: no section header table"); |
| 135 | if (shnum > MAX_SECTIONS) return refuse(r, "e_shnum above this tool's limit (256)"); |
| 136 | if (shstrndx >= shnum) return refuse(r, "e_shstrndx out of range"); |
| 137 | if (!in_bounds(shoff, (uint64_t)shnum * SHDR_SIZE, len)) |
| 138 | return refuse(r, "section header table outside the file"); |
| 139 | const uint8_t *strh = buf + shoff + (uint64_t)shstrndx * SHDR_SIZE; |
| 140 | uint64_t stroff = rd64(strh + 24), strsize = rd64(strh + 32); |
| 141 | if (strsize == 0 || !in_bounds(stroff, strsize, len)) |
| 142 | return refuse(r, "section string table outside the file or empty"); |
| 143 | if (buf[stroff + strsize - 1] != 0) return refuse(r, "section string table not NUL-terminated"); |
| 144 | const char *strtab = (const char *)buf + stroff; |
| 145 | for (uint16_t i = 0; i < shnum; i++) { |
| 146 | const uint8_t *sh = buf + shoff + (uint64_t)i * SHDR_SIZE; |
| 147 | uint32_t name = rd32(sh), type = rd32(sh + 4); |
| 148 | uint64_t off = rd64(sh + 24), size = rd64(sh + 32); |
| 149 | if (name >= strsize) return refuse(r, "section name index outside the string table"); |
| 150 | if (type != SHT_NOBITS && size > 0 && !in_bounds(off, size, len)) |
| 151 | return refuse(r, "section bounds: offset+size outside the file"); |
| 152 | section *s = &r->sec[r->nsec++]; |
| 153 | s->name = strtab + name; s->type = type; s->offset = off; s->size = size; |
| 154 | } |
| 155 | snprintf(r->sig_name, sizeof r->sig_name, ".fwsignature_%s", family); |
| 156 | for (size_t i = 0; i < r->nsec; i++) { |
| 157 | const section *s = &r->sec[i]; |
| 158 | if (!strcmp(s->name, ".fwimage")) r->fwimage = s; |
| 159 | else if (!strcmp(s->name, ".fwversion")) r->fwversion = s; |
| 160 | else if (!strcmp(s->name, ".note.gnu.build-id")) r->buildid = s; |
| 161 | else if (!strcmp(s->name, r->sig_name)) r->fwsig = s; |
| 162 | } |
| 163 | if (!r->fwimage) return refuse(r, "missing section .fwimage"); |
| 164 | if (r->fwimage->type == SHT_NOBITS || r->fwimage->size == 0) return refuse(r, ".fwimage empty"); |
| 165 | if (!r->fwversion) return refuse(r, "missing section .fwversion"); |
| 166 | if (!r->buildid) return refuse(r, "missing section .note.gnu.build-id"); |
| 167 | if (!r->fwsig) return refuse(r, "missing section: no signature section for this chip family"); |
| 168 | if (r->fwsig->type == SHT_NOBITS || r->fwsig->size == 0) return refuse(r, "signature section empty"); |
| 169 | /* .fwversion: the driver's three conditions, then equality with the pin. */ |
| 170 | const section *v = r->fwversion; |
| 171 | if (v->type == SHT_NOBITS || v->size == 0 || v->size >= 64) return refuse(r, "version section size not in 1..63"); |
| 172 | if (buf[v->offset + v->size - 1] != 0) return refuse(r, "version string not NUL-terminated"); |
| 173 | memcpy(r->version, buf + v->offset, (size_t)v->size); |
| 174 | if (expect_version && (v->size != strlen(expect_version) + 1 || strcmp(r->version, expect_version) != 0)) |
| 175 | return refuse(r, "version mismatch against the pinned driver version"); |
| 176 | /* build-id note: namesz, descsz, type, "GNU\0", desc. */ |
| 177 | const section *n = r->buildid; |
| 178 | if (n->type == SHT_NOBITS || n->size < 16) return refuse(r, "build-id note too short"); |
| 179 | const uint8_t *note = buf + n->offset; |
| 180 | uint32_t namesz = rd32(note), descsz = rd32(note + 4), ntype = rd32(note + 8); |
| 181 | if (namesz != 4 || memcmp(note + 12, "GNU", 4) != 0 || ntype != 3) |
| 182 | return refuse(r, "build-id note is not an NT_GNU_BUILD_ID note"); |
| 183 | if (descsz == 0 || descsz > 64 || 16 + (uint64_t)descsz > n->size) |
| 184 | return refuse(r, "build-id note descriptor size out of range"); |
| 185 | hex(note + 16, descsz, r->buildid_hex); |
| 186 | sha256_hash(buf, (size_t)len, r->sha_file); |
| 187 | sha256_hash(buf + r->fwimage->offset, (size_t)r->fwimage->size, r->sha_image); |
| 188 | sha256_hash(buf + r->fwsig->offset, (size_t)r->fwsig->size, r->sha_sig); |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static void print_report(const report *r, int ok) |
| 193 | { |
| 194 | char h[65]; |
| 195 | for (size_t i = 0; i < r->nsec; i++) |
| 196 | printf("section %zu %s type=%u offset=%llu size=%llu\n", i, r->sec[i].name[0] ? r->sec[i].name : "(null)", |
| 197 | (unsigned)r->sec[i].type, (unsigned long long)r->sec[i].offset, (unsigned long long)r->sec[i].size); |
| 198 | if (ok) { |
| 199 | printf("fwversion %s\n", r->version); |
| 200 | printf("build_id %s\n", r->buildid_hex); |
| 201 | printf("signature_section %s size=%llu\n", r->sig_name, (unsigned long long)r->fwsig->size); |
| 202 | hex(r->sha_file, 32, h); printf("sha256_file %s\n", h); |
| 203 | hex(r->sha_image, 32, h); printf("sha256_fwimage %s size=%llu\n", h, (unsigned long long)r->fwimage->size); |
| 204 | hex(r->sha_sig, 32, h); printf("sha256_signature %s\n", h); |
| 205 | printf("RESULT OK\n"); |
| 206 | } else { |
| 207 | printf("RESULT REFUSED %s\n", r->reason); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | static int cmd_inspect(int argc, char **argv) |
| 212 | { |
| 213 | const char *path = NULL, *family = DEFAULT_FAMILY, *expect = DEFAULT_VERSION; |
| 214 | for (int i = 0; i < argc; i++) { |
| 215 | if (!strcmp(argv[i], "--family") && i + 1 < argc) family = argv[++i]; |
| 216 | else if (!strcmp(argv[i], "--expect-version") && i + 1 < argc) expect = argv[++i]; |
| 217 | else if (!path) path = argv[i]; |
| 218 | else { fprintf(stderr, "unexpected argument %s\n", argv[i]); return 1; } |
| 219 | } |
| 220 | if (!path) { fprintf(stderr, "usage: ck_gsp_fw_inspect inspect FILE [--family NAME] [--expect-version V]\n"); return 1; } |
| 221 | FILE *f = fopen(path, "rb"); |
| 222 | if (!f) { fprintf(stderr, "open %s: %s\n", path, strerror(errno)); return 1; } |
| 223 | if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return 1; } |
| 224 | long sz = ftell(f); |
| 225 | if (sz < 0) { fclose(f); return 1; } |
| 226 | rewind(f); |
| 227 | uint8_t *buf = malloc(sz ? (size_t)sz : 1); |
| 228 | if (!buf) { fclose(f); return 1; } |
| 229 | if (fread(buf, 1, (size_t)sz, f) != (size_t)sz) { fprintf(stderr, "short read\n"); free(buf); fclose(f); return 1; } |
| 230 | fclose(f); |
| 231 | printf("file %s bytes=%ld family=%s expect_version=%s\n", path, sz, family, expect); |
| 232 | report *r = calloc(1, sizeof *r); |
| 233 | if (!r) { free(buf); return 1; } |
| 234 | int rc = inspect_buf(buf, (uint64_t)sz, family, expect, r); |
| 235 | print_report(r, rc == 0); |
| 236 | free(r); free(buf); |
| 237 | return rc == 0 ? 0 : 2; |
| 238 | } |
| 239 | |
| 240 | /* ---- self-test fixture: a minimal valid container ---------------------- */ |
| 241 | enum { FX_NULL, FX_IMAGE, FX_NOTE, FX_VERSION, FX_SIG, FX_STRTAB, FX_NSEC }; |
| 242 | #define FX_IMAGE_OFF 64u |
| 243 | #define FX_IMAGE_LEN 16u |
| 244 | #define FX_NOTE_OFF 80u |
| 245 | #define FX_NOTE_LEN 36u /* 12 + "GNU\0" + 20-byte id */ |
| 246 | #define FX_VER_OFF 116u |
| 247 | #define FX_VER_LEN 11u /* "580.173.02\0" */ |
| 248 | #define FX_SIG_OFF 128u |
| 249 | #define FX_SIG_LEN 32u |
| 250 | #define FX_STR_OFF 160u |
| 251 | #define FX_SHOFF 256u |
| 252 | #define FX_LEN (FX_SHOFF + FX_NSEC * SHDR_SIZE) |
| 253 | |
| 254 | static const char fx_strtab[] = "\0.fwimage\0.note.gnu.build-id\0.fwversion\0.fwsignature_gb10y\0.shstrtab\0"; |
| 255 | #define FX_STR_LEN (sizeof fx_strtab) |
| 256 | |
| 257 | static void fx_shdr(uint8_t *b, unsigned idx, uint32_t name, uint32_t type, uint64_t off, uint64_t size) |
| 258 | { |
| 259 | uint8_t *sh = b + FX_SHOFF + idx * SHDR_SIZE; |
| 260 | memset(sh, 0, SHDR_SIZE); |
| 261 | wr32(sh, name); wr32(sh + 4, type); wr64(sh + 24, off); wr64(sh + 32, size); |
| 262 | } |
| 263 | |
| 264 | static void fx_build(uint8_t *b) |
| 265 | { |
| 266 | memset(b, 0, FX_LEN); |
| 267 | b[0] = 0x7f; b[1] = 'E'; b[2] = 'L'; b[3] = 'F'; b[4] = 2; b[5] = 1; b[6] = 1; |
| 268 | wr16(b + 16, ET_REL); wr16(b + 18, EM_RISCV); wr32(b + 20, 1); |
| 269 | wr64(b + 40, FX_SHOFF); wr16(b + 52, EHDR_SIZE); wr16(b + 58, SHDR_SIZE); |
| 270 | wr16(b + 60, FX_NSEC); wr16(b + 62, FX_STRTAB); |
| 271 | for (unsigned i = 0; i < FX_IMAGE_LEN; i++) b[FX_IMAGE_OFF + i] = (uint8_t)(0xa0 + i); |
| 272 | wr32(b + FX_NOTE_OFF, 4); wr32(b + FX_NOTE_OFF + 4, 20); wr32(b + FX_NOTE_OFF + 8, 3); |
| 273 | memcpy(b + FX_NOTE_OFF + 12, "GNU", 4); |
| 274 | for (unsigned i = 0; i < 20; i++) b[FX_NOTE_OFF + 16 + i] = (uint8_t)i; |
| 275 | memcpy(b + FX_VER_OFF, DEFAULT_VERSION, FX_VER_LEN); |
| 276 | for (unsigned i = 0; i < FX_SIG_LEN; i++) b[FX_SIG_OFF + i] = (uint8_t)(0x50 + i); |
| 277 | memcpy(b + FX_STR_OFF, fx_strtab, FX_STR_LEN); |
| 278 | fx_shdr(b, FX_NULL, 0, 0, 0, 0); |
| 279 | fx_shdr(b, FX_IMAGE, 1, 1, FX_IMAGE_OFF, FX_IMAGE_LEN); |
| 280 | fx_shdr(b, FX_NOTE, 10, 7, FX_NOTE_OFF, FX_NOTE_LEN); |
| 281 | fx_shdr(b, FX_VERSION, 29, 1, FX_VER_OFF, FX_VER_LEN); |
| 282 | fx_shdr(b, FX_SIG, 40, 1, FX_SIG_OFF, FX_SIG_LEN); |
| 283 | fx_shdr(b, FX_STRTAB, 59, 3, FX_STR_OFF, FX_STR_LEN); |
| 284 | } |
| 285 | |
| 286 | typedef void (*mutate_fn)(uint8_t *b); |
| 287 | static void m_magic(uint8_t *b) { b[1] = 'X'; } |
| 288 | static void m_class32(uint8_t *b) { b[4] = 1; } |
| 289 | static void m_bigendian(uint8_t *b) { b[5] = 2; } |
| 290 | static void m_type(uint8_t *b) { wr16(b + 16, 2); } |
| 291 | static void m_machine(uint8_t *b) { wr16(b + 18, 62); } |
| 292 | static void m_shentsize(uint8_t *b) { wr16(b + 58, 40); } |
| 293 | static void m_shnum0(uint8_t *b) { wr16(b + 60, 0); } |
| 294 | static void m_shstrndx(uint8_t *b) { wr16(b + 62, FX_NSEC); } |
| 295 | static void m_shoff(uint8_t *b) { wr64(b + 40, FX_LEN - 8); } |
| 296 | static void m_strtab_nul(uint8_t *b) { b[FX_STR_OFF + FX_STR_LEN - 1] = 'x'; } |
| 297 | static void m_name_idx(uint8_t *b) { wr32(b + FX_SHOFF + FX_SIG * SHDR_SIZE, 100000); } |
| 298 | static void m_image_bounds(uint8_t *b) { wr64(b + FX_SHOFF + FX_IMAGE * SHDR_SIZE + 32, FX_LEN); } |
| 299 | static void m_overflow(uint8_t *b) { wr64(b + FX_SHOFF + FX_IMAGE * SHDR_SIZE + 24, UINT64_MAX - 1); } |
| 300 | static void m_image_empty(uint8_t *b) { wr64(b + FX_SHOFF + FX_IMAGE * SHDR_SIZE + 32, 0); } |
| 301 | static void m_version_nul(uint8_t *b) { wr64(b + FX_SHOFF + FX_VERSION * SHDR_SIZE + 32, FX_VER_LEN - 1); } |
| 302 | static void m_version_text(uint8_t *b) { b[FX_VER_OFF + FX_VER_LEN - 2] = '3'; } |
| 303 | static void m_version_long(uint8_t *b) { wr64(b + FX_SHOFF + FX_VERSION * SHDR_SIZE + 32, 64); } |
| 304 | static void m_sig_empty(uint8_t *b) { wr64(b + FX_SHOFF + FX_SIG * SHDR_SIZE + 32, 0); } |
| 305 | static void m_sig_renamed(uint8_t *b) { b[FX_STR_OFF + 40 + 17] = 'x'; } /* .fwsignature_gb10x */ |
| 306 | static void m_note_type(uint8_t *b) { wr32(b + FX_NOTE_OFF + 8, 1); } |
| 307 | static void m_note_desc(uint8_t *b) { wr32(b + FX_NOTE_OFF + 4, 40); } |
| 308 | static void m_note_short(uint8_t *b) { wr64(b + FX_SHOFF + FX_NOTE * SHDR_SIZE + 32, 8); } |
| 309 | |
| 310 | static const struct { const char *what; mutate_fn fn; const char *reason; } cases[] = { |
| 311 | { "bad ELF magic", m_magic, "ELF magic" }, |
| 312 | { "32-bit class", m_class32, "not ELF64" }, |
| 313 | { "big-endian", m_bigendian, "little-endian" }, |
| 314 | { "e_type EXEC", m_type, "e_type" }, |
| 315 | { "e_machine x86-64", m_machine, "e_machine" }, |
| 316 | { "e_shentsize 40", m_shentsize, "e_shentsize" }, |
| 317 | { "e_shnum 0", m_shnum0, "e_shnum" }, |
| 318 | { "e_shstrndx == shnum", m_shstrndx, "e_shstrndx" }, |
| 319 | { "section header table past end", m_shoff, "section header table" }, |
| 320 | { "string table not NUL-terminated", m_strtab_nul, "string table not NUL" }, |
| 321 | { "section name index past string table", m_name_idx, "section name index" }, |
| 322 | { ".fwimage size past end of file", m_image_bounds, "section bounds" }, |
| 323 | { ".fwimage offset near UINT64_MAX (overflow)", m_overflow, "section bounds" }, |
| 324 | { ".fwimage empty", m_image_empty, ".fwimage empty" }, |
| 325 | { ".fwversion without NUL", m_version_nul, "NUL-terminated" }, |
| 326 | { ".fwversion text differs from pin", m_version_text, "version mismatch" }, |
| 327 | { ".fwversion 64 bytes", m_version_long, "version section size" }, |
| 328 | { "signature section empty", m_sig_empty, "signature section empty" }, |
| 329 | { "signature section for another family only", m_sig_renamed, "no signature section" }, |
| 330 | { "build-id note wrong type", m_note_type, "NT_GNU_BUILD_ID" }, |
| 331 | { "build-id descriptor past section", m_note_desc, "descriptor size" }, |
| 332 | { "build-id note truncated", m_note_short, "too short" }, |
| 333 | }; |
| 334 | |
| 335 | static int cmd_selftest(void) |
| 336 | { |
| 337 | uint8_t *b = malloc(FX_LEN); |
| 338 | report *r = calloc(1, sizeof *r); |
| 339 | if (!b || !r) return 1; |
| 340 | int fail = 0; |
| 341 | fx_build(b); |
| 342 | if (inspect_buf(b, FX_LEN, "gb10y", DEFAULT_VERSION, r) != 0) { |
| 343 | printf("FAIL valid fixture refused: %s\n", r->reason); fail = 1; |
| 344 | } else if (strcmp(r->version, DEFAULT_VERSION) != 0 || strncmp(r->buildid_hex, "000102030405", 12) != 0) { |
| 345 | printf("FAIL valid fixture parsed wrong (version %s build id %s)\n", r->version, r->buildid_hex); fail = 1; |
| 346 | } else { |
| 347 | printf("PASS valid fixture accepted (version %s, build id %s)\n", r->version, r->buildid_hex); |
| 348 | } |
| 349 | /* Other family missing on the valid fixture: refused, not silently accepted. */ |
| 350 | if (inspect_buf(b, FX_LEN, "gb10x", DEFAULT_VERSION, r) == 0 || !strstr(r->reason, "no signature section")) { |
| 351 | printf("FAIL family gb10x should be refused on a gb10y-only fixture\n"); fail = 1; |
| 352 | } else printf("PASS family gb10x refused: %s\n", r->reason); |
| 353 | /* Truncated file: every length below FX_LEN must be refused, never crash. */ |
| 354 | int trunc_ok = 1; |
| 355 | for (uint64_t l = 0; l < FX_LEN; l++) |
| 356 | if (inspect_buf(b, l, "gb10y", DEFAULT_VERSION, r) == 0) { trunc_ok = 0; break; } |
| 357 | printf("%s every truncation 0..%u refused\n", trunc_ok ? "PASS" : "FAIL", (unsigned)(FX_LEN - 1)); |
| 358 | if (!trunc_ok) fail = 1; |
| 359 | size_t n = sizeof cases / sizeof cases[0]; |
| 360 | for (size_t i = 0; i < n; i++) { |
| 361 | fx_build(b); |
| 362 | cases[i].fn(b); |
| 363 | int rc = inspect_buf(b, FX_LEN, "gb10y", DEFAULT_VERSION, r); |
| 364 | if (rc == 0) { printf("FAIL %s: accepted\n", cases[i].what); fail = 1; } |
| 365 | else if (!strstr(r->reason, cases[i].reason)) { |
| 366 | printf("FAIL %s: refused for the wrong reason: %s\n", cases[i].what, r->reason); fail = 1; |
| 367 | } else printf("PASS %s: %s\n", cases[i].what, r->reason); |
| 368 | } |
| 369 | printf("CK_GSP_FW_INSPECT_SELFTEST: %s (%zu mutations)\n", fail ? "FAIL" : "PASS", n); |
| 370 | free(b); free(r); |
| 371 | return fail; |
| 372 | } |
| 373 | |
| 374 | int main(int argc, char **argv) |
| 375 | { |
| 376 | if (argc >= 2 && !strcmp(argv[1], "inspect")) return cmd_inspect(argc - 2, argv + 2); |
| 377 | if (argc == 2 && !strcmp(argv[1], "selftest")) return cmd_selftest(); |
| 378 | fprintf(stderr, "usage: ck_gsp_fw_inspect inspect FILE [--family NAME] [--expect-version V]\n" |
| 379 | " ck_gsp_fw_inspect selftest\n"); |
| 380 | return 1; |
| 381 | } |