omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / gpu / nvidia / native / gsp / fwimage.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note fwimage.om: read-only parser for the NVIDIA GSP firmware container (gsp_ga10x.bin, ELF64 RISC-V). |
| 3 | note Format and driver behaviour: native/gsp/README.md. This file reads one firmware file and writes |
| 4 | note only stdout and stderr. It never opens a device, never maps memory and never writes a file. |
| 5 | note |
| 6 | note Build: this unit is OM-2 and calls sha256_init/update/final from omega-mv om/tls/sha2.om. |
| 7 | note The compiled unit is this file followed by sha2.om, fed to the pinned seeded omc: |
| 8 | note cat fwimage.om omega-mv/om/tls/sha2.om | omc > fwimage |
| 9 | note Usage: fwimage FILE [gb20y|gb10y] |
| 10 | note |
| 11 | note Report (stdout, only after every check passed; numbers lowercase hex with 0x, no padding): |
| 12 | note SECTION <name> off=<hex> size=<hex> every section header except index 0, in table order |
| 13 | note FWVERSION <text> .fwversion bytes before its terminating zero byte |
| 14 | note SHA256 <name> <64 hex> .fwimage and every .fwsignature_* section, table order |
| 15 | note SELECT family=<f> image_off=<hex> image_size=<hex> sig_section=.fwsignature_<f> sig_sha256=<hex> |
| 16 | note only with a family argument |
| 17 | note |
| 18 | note Exit codes (a refusal writes one line "FWIMAGE REFUSE <reason>" to stderr and nothing to stdout): |
| 19 | note 0 report written |
| 20 | note 2 usage: wrong argument count or unreadable path argument |
| 21 | note 3 I/O: open, seek, short read or stdout write failed |
| 22 | note 10 not ELF: the first four bytes are not 7f 45 4c 46 |
| 23 | note 11 not an ELF64 little-endian RISC-V (e_machine 243) version-1 file |
| 24 | note 12 truncated: file shorter than the 64-byte ELF header, or section header table past end of file |
| 25 | note 13 malformed header: e_ehsize or e_shentsize not 64, e_shnum 0, e_shstrndx 0 or not below |
| 26 | note e_shnum, or the name table section is not SHT_STRTAB or is empty |
| 27 | note 14 out of bounds: a section's offset + size passes end of file (overflow-checked), a name offset |
| 28 | note lies outside the name table, or the name table does not end with a zero byte |
| 29 | note 15 missing .fwimage |
| 30 | note 16 missing .fwversion |
| 31 | note 17 unknown family argument (only gb20y and gb10y are accepted) |
| 32 | note 18 duplicate section names |
| 33 | note 19 capacity: more than 1024 sections or a name table above 65536 bytes |
| 34 | note 20 the selected .fwsignature_<family> section is absent |
| 35 | note 21 .fwimage or a .fwsignature_* section is SHT_NOBITS or empty |
| 36 | note 22 .fwversion malformed: size 0 or above 63, last byte not zero, or a byte before it not 32..126 |
| 37 | note Runtime traps exit 100 + trap code (Omegese 9.1); none is expected on any input. |
| 38 | om2 |
| 39 | |
| 40 | text tpre ".fwsignature_" |
| 41 | text timg ".fwimage" |
| 42 | text tver ".fwversion" |
| 43 | text tf20 "gb20y" |
| 44 | text tf10 "gb10y" |
| 45 | text tsec "SECTION " |
| 46 | text toff " off=" |
| 47 | text tsize " size=" |
| 48 | text tfv "FWVERSION " |
| 49 | text tsha "SHA256 " |
| 50 | text tsel "SELECT family=" |
| 51 | text timo " image_off=" |
| 52 | text tims " image_size=" |
| 53 | text tsgs " sig_section=" |
| 54 | text tsgh " sig_sha256=" |
| 55 | text tsp " " |
| 56 | text tnl "\n" |
| 57 | text trf "FWIMAGE REFUSE " |
| 58 | text tuse "usage: fwimage FILE [gb20y|gb10y]" |
| 59 | text tio "I/O error" |
| 60 | text tnelf "not an ELF file" |
| 61 | text tkind "not ELF64 little-endian RISC-V version 1" |
| 62 | text ttrunc "truncated header or section header table" |
| 63 | text tshdr "malformed ELF header or name table section" |
| 64 | text tbnd "section out of bounds" |
| 65 | text tname "section name out of bounds" |
| 66 | text tnoimg "missing .fwimage" |
| 67 | text tnover "missing .fwversion" |
| 68 | text tfam "unknown family" |
| 69 | text tdup "duplicate section names" |
| 70 | text tcap "capacity exceeded" |
| 71 | text tnosig "missing selected signature section" |
| 72 | text tempty "image or signature section has no file bytes" |
| 73 | text tbadv "malformed .fwversion" |
| 74 | |
| 75 | global gap uchar 4096 |
| 76 | global gfam uchar 16 |
| 77 | global ghdr uchar 64 |
| 78 | global gsh uchar 65536 |
| 79 | global gss uchar 65536 |
| 80 | global gfv uchar 64 |
| 81 | global gob uchar 4096 |
| 82 | global gdg uchar 32 |
| 83 | global gfd int 1 |
| 84 | note gst: 0 file size, 1 e_shnum, 2 e_shstrndx, 3 output fill, 4 family length (0 none), |
| 85 | note 5 name table size, 6 .fwimage index, 7 .fwversion index, 8 selected signature index, 9 e_shoff |
| 86 | global gst ulong 16 |
| 87 | |
| 88 | fn refuse code int msg view uchar * to void |
| 89 | n = write 2 trf 0 len trf |
| 90 | n = write 2 msg 0 len msg |
| 91 | n = write 2 tnl 0 len tnl |
| 92 | exit code |
| 93 | end |
| 94 | |
| 95 | fn oflush to void |
| 96 | n = get gst 3 |
| 97 | if gt n 0 |
| 98 | w = write 1 gob 0 n |
| 99 | if ne w long n |
| 100 | refuse 3 tio |
| 101 | end |
| 102 | put gst 3 0 |
| 103 | end |
| 104 | end |
| 105 | |
| 106 | fn oc b uchar to void |
| 107 | if ge get gst 3 len gob |
| 108 | oflush |
| 109 | end |
| 110 | put gob get gst 3 b |
| 111 | put gst 3 add get gst 3 1 |
| 112 | end |
| 113 | |
| 114 | fn ot t view uchar * to void |
| 115 | for i ulong len t |
| 116 | oc get t i |
| 117 | end |
| 118 | end |
| 119 | |
| 120 | fn ohd n ulong to uchar |
| 121 | if lt n 10 |
| 122 | ret uchar add n 48 |
| 123 | end |
| 124 | ret uchar add n 87 |
| 125 | end |
| 126 | |
| 127 | fn ohx v ulong to void |
| 128 | oc 48 |
| 129 | oc 120 |
| 130 | on = ulong 0 |
| 131 | for i ulong 16 |
| 132 | nb = and shr v sub 60 mul i 4 15 |
| 133 | if or ne nb 0 or ne on 0 eq i 15 |
| 134 | oc ohd nb |
| 135 | on = 1 |
| 136 | end |
| 137 | end |
| 138 | end |
| 139 | |
| 140 | fn odg to void |
| 141 | for i ulong 32 |
| 142 | b = ulong get gdg i |
| 143 | oc ohd shr b 4 |
| 144 | oc ohd and b 15 |
| 145 | end |
| 146 | end |
| 147 | |
| 148 | fn hle o ulong w ulong to ulong |
| 149 | v = ulong 0 |
| 150 | for i ulong w |
| 151 | v = or v shl ulong get ghdr add o i mul i 8 |
| 152 | end |
| 153 | ret v |
| 154 | end |
| 155 | |
| 156 | fn sle o ulong w ulong to ulong |
| 157 | v = ulong 0 |
| 158 | for i ulong w |
| 159 | v = or v shl ulong get gsh add o i mul i 8 |
| 160 | end |
| 161 | ret v |
| 162 | end |
| 163 | |
| 164 | fn secnm i ulong to ulong |
| 165 | ret sle mul i 64 4 |
| 166 | end |
| 167 | |
| 168 | fn sectyp i ulong to ulong |
| 169 | ret sle add mul i 64 4 4 |
| 170 | end |
| 171 | |
| 172 | fn secoff i ulong to ulong |
| 173 | ret sle add mul i 64 24 8 |
| 174 | end |
| 175 | |
| 176 | fn secsz i ulong to ulong |
| 177 | ret sle add mul i 64 32 8 |
| 178 | end |
| 179 | |
| 180 | note Name of section i, which ldnames proved ends with a zero byte inside the name table. |
| 181 | fn oname i ulong to void |
| 182 | o = secnm i |
| 183 | for k ulong get gst 5 |
| 184 | c = get gss add o k |
| 185 | if eq c 0 |
| 186 | ret |
| 187 | end |
| 188 | oc c |
| 189 | end |
| 190 | end |
| 191 | |
| 192 | note Section i's name starts with t and has a zero byte after t inside the table. |
| 193 | fn nmpre i ulong t view uchar * to bool |
| 194 | o = secnm i |
| 195 | if gt len t sub sub get gst 5 o 1 |
| 196 | ret false |
| 197 | end |
| 198 | for k ulong len t |
| 199 | if ne get gss add o k get t k |
| 200 | ret false |
| 201 | end |
| 202 | end |
| 203 | ret true |
| 204 | end |
| 205 | |
| 206 | fn nmis i ulong t view uchar * to bool |
| 207 | if not nmpre i t |
| 208 | ret false |
| 209 | end |
| 210 | ret eq get gss add secnm i len t 0 |
| 211 | end |
| 212 | |
| 213 | note Section i is .fwsignature_<family argument>. |
| 214 | fn nmsel i ulong to bool |
| 215 | if not nmpre i tpre |
| 216 | ret false |
| 217 | end |
| 218 | o = add secnm i len tpre |
| 219 | fl = get gst 4 |
| 220 | if gt fl sub sub get gst 5 o 1 |
| 221 | ret false |
| 222 | end |
| 223 | for k ulong fl |
| 224 | if ne get gss add o k get gfam k |
| 225 | ret false |
| 226 | end |
| 227 | end |
| 228 | ret eq get gss add o fl 0 |
| 229 | end |
| 230 | |
| 231 | fn streq a ulong b ulong to bool |
| 232 | for k ulong get gst 5 |
| 233 | x = get gss add a k |
| 234 | if ne x get gss add b k |
| 235 | ret false |
| 236 | end |
| 237 | if eq x 0 |
| 238 | ret true |
| 239 | end |
| 240 | end |
| 241 | ret false |
| 242 | end |
| 243 | |
| 244 | fn fameq t view uchar * to bool |
| 245 | if ne get gst 4 len t |
| 246 | ret false |
| 247 | end |
| 248 | for k ulong len t |
| 249 | if ne get gfam k get t k |
| 250 | ret false |
| 251 | end |
| 252 | end |
| 253 | ret true |
| 254 | end |
| 255 | |
| 256 | fn find t view uchar * to ulong |
| 257 | for i ulong sub get gst 1 1 |
| 258 | if nmis add i 1 t |
| 259 | ret add i 1 |
| 260 | end |
| 261 | end |
| 262 | ret 0 |
| 263 | end |
| 264 | |
| 265 | fn chkhdr to void |
| 266 | fs = get gst 0 |
| 267 | if lt fs 64 |
| 268 | refuse 12 ttrunc |
| 269 | end |
| 270 | r = pread get gfd 0 ghdr 0 64 0 |
| 271 | if ne r 64 |
| 272 | refuse 3 tio |
| 273 | end |
| 274 | if ne hle 0 4 0x464c457f |
| 275 | refuse 10 tnelf |
| 276 | end |
| 277 | if ne hle 4 3 0x010102 |
| 278 | refuse 11 tkind |
| 279 | end |
| 280 | if or ne hle 18 2 243 ne hle 20 4 1 |
| 281 | refuse 11 tkind |
| 282 | end |
| 283 | if or ne hle 52 2 64 ne hle 58 2 64 |
| 284 | refuse 13 tshdr |
| 285 | end |
| 286 | nsec = hle 60 2 |
| 287 | idx = hle 62 2 |
| 288 | if eq nsec 0 |
| 289 | refuse 13 tshdr |
| 290 | end |
| 291 | if gt nsec 1024 |
| 292 | refuse 19 tcap |
| 293 | end |
| 294 | if or eq idx 0 ge idx nsec |
| 295 | refuse 13 tshdr |
| 296 | end |
| 297 | off = hle 40 8 |
| 298 | tl = mul nsec 64 |
| 299 | if or gt off fs gt tl sub fs off |
| 300 | refuse 12 ttrunc |
| 301 | end |
| 302 | put gst 1 nsec |
| 303 | put gst 2 idx |
| 304 | put gst 9 off |
| 305 | end |
| 306 | |
| 307 | fn ldsh to void |
| 308 | tl = mul get gst 1 64 |
| 309 | r = pread get gfd 0 gsh 0 tl long get gst 9 |
| 310 | if ne r long tl |
| 311 | refuse 3 tio |
| 312 | end |
| 313 | fs = get gst 0 |
| 314 | for i ulong get gst 1 |
| 315 | if ne sectyp i 8 |
| 316 | o = secoff i |
| 317 | z = secsz i |
| 318 | if or gt o fs gt z sub fs o |
| 319 | refuse 14 tbnd |
| 320 | end |
| 321 | end |
| 322 | end |
| 323 | end |
| 324 | |
| 325 | fn ldnames to void |
| 326 | k = get gst 2 |
| 327 | if ne sectyp k 3 |
| 328 | refuse 13 tshdr |
| 329 | end |
| 330 | z = secsz k |
| 331 | if eq z 0 |
| 332 | refuse 13 tshdr |
| 333 | end |
| 334 | if gt z len gss |
| 335 | refuse 19 tcap |
| 336 | end |
| 337 | r = pread get gfd 0 gss 0 z long secoff k |
| 338 | if ne r long z |
| 339 | refuse 3 tio |
| 340 | end |
| 341 | put gst 5 z |
| 342 | if ne get gss sub z 1 0 |
| 343 | refuse 14 tname |
| 344 | end |
| 345 | for i ulong get gst 1 |
| 346 | if ge secnm i z |
| 347 | refuse 14 tname |
| 348 | end |
| 349 | end |
| 350 | end |
| 351 | |
| 352 | fn chkdup to void |
| 353 | n = get gst 1 |
| 354 | for i ulong n |
| 355 | for j ulong sub n add i 1 |
| 356 | a = secnm i |
| 357 | b = secnm add add i 1 j |
| 358 | if ne get gss a 0 |
| 359 | if streq a b |
| 360 | refuse 18 tdup |
| 361 | end |
| 362 | end |
| 363 | end |
| 364 | end |
| 365 | end |
| 366 | |
| 367 | fn hashable i ulong to void |
| 368 | if or eq sectyp i 8 eq secsz i 0 |
| 369 | refuse 21 tempty |
| 370 | end |
| 371 | end |
| 372 | |
| 373 | fn findreq to void |
| 374 | img = find timg |
| 375 | if eq img 0 |
| 376 | refuse 15 tnoimg |
| 377 | end |
| 378 | hashable img |
| 379 | ver = find tver |
| 380 | if eq ver 0 |
| 381 | refuse 16 tnover |
| 382 | end |
| 383 | for i ulong get gst 1 |
| 384 | if nmpre i tpre |
| 385 | hashable i |
| 386 | end |
| 387 | end |
| 388 | z = secsz ver |
| 389 | if or or eq z 0 gt z 63 eq sectyp ver 8 |
| 390 | refuse 22 tbadv |
| 391 | end |
| 392 | r = pread get gfd 0 gfv 0 z long secoff ver |
| 393 | if ne r long z |
| 394 | refuse 3 tio |
| 395 | end |
| 396 | if ne get gfv sub z 1 0 |
| 397 | refuse 22 tbadv |
| 398 | end |
| 399 | for i ulong sub z 1 |
| 400 | c = get gfv i |
| 401 | if or lt c 32 gt c 126 |
| 402 | refuse 22 tbadv |
| 403 | end |
| 404 | end |
| 405 | put gst 6 img |
| 406 | put gst 7 ver |
| 407 | if gt get gst 4 0 |
| 408 | sel = ulong 0 |
| 409 | for i ulong get gst 1 |
| 410 | if nmsel i |
| 411 | sel = i |
| 412 | end |
| 413 | end |
| 414 | if eq sel 0 |
| 415 | refuse 20 tnosig |
| 416 | end |
| 417 | hashable sel |
| 418 | put gst 8 sel |
| 419 | end |
| 420 | end |
| 421 | |
| 422 | note SHA-256 of section i's file bytes into dg, read in 1 MiB slices (om/tls/sha2.om, FIPS 180-4). |
| 423 | fn hashsec i ulong to void |
| 424 | buf = new uchar 1048576 0 |
| 425 | c = new ulong 40 0 |
| 426 | d = new uchar 32 0 |
| 427 | sha256_init c |
| 428 | pos = secoff i |
| 429 | left = secsz i |
| 430 | fd = get gfd 0 |
| 431 | while gt left 0 bound 999999 |
| 432 | k = left |
| 433 | if gt k len buf |
| 434 | k = len buf |
| 435 | end |
| 436 | r = pread fd buf 0 k long pos |
| 437 | if le r 0 |
| 438 | refuse 3 tio |
| 439 | end |
| 440 | sha256_update c part buf 0 ulong r |
| 441 | pos = add pos ulong r |
| 442 | left = sub left ulong r |
| 443 | end |
| 444 | sha256_final c d |
| 445 | for j ulong 32 |
| 446 | put gdg j get d j |
| 447 | end |
| 448 | end |
| 449 | |
| 450 | fn report to void |
| 451 | n = get gst 1 |
| 452 | for i ulong sub n 1 |
| 453 | s = add i 1 |
| 454 | ot tsec |
| 455 | oname s |
| 456 | ot toff |
| 457 | ohx secoff s |
| 458 | ot tsize |
| 459 | ohx secsz s |
| 460 | ot tnl |
| 461 | end |
| 462 | ot tfv |
| 463 | for i ulong sub secsz get gst 7 1 |
| 464 | oc get gfv i |
| 465 | end |
| 466 | ot tnl |
| 467 | img = get gst 6 |
| 468 | for i ulong sub n 1 |
| 469 | s = add i 1 |
| 470 | if or eq s img nmpre s tpre |
| 471 | hashsec s |
| 472 | ot tsha |
| 473 | oname s |
| 474 | ot tsp |
| 475 | odg |
| 476 | ot tnl |
| 477 | end |
| 478 | end |
| 479 | if gt get gst 4 0 |
| 480 | sel = get gst 8 |
| 481 | hashsec sel |
| 482 | ot tsel |
| 483 | for i ulong get gst 4 |
| 484 | oc get gfam i |
| 485 | end |
| 486 | ot timo |
| 487 | ohx secoff img |
| 488 | ot tims |
| 489 | ohx secsz img |
| 490 | ot tsgs |
| 491 | oname sel |
| 492 | ot tsgh |
| 493 | odg |
| 494 | ot tnl |
| 495 | end |
| 496 | oflush |
| 497 | end |
| 498 | |
| 499 | fn main to int |
| 500 | ac = argcnt |
| 501 | if or lt ac 2 gt ac 3 |
| 502 | refuse 2 tuse |
| 503 | end |
| 504 | n = argread 1 gap 0 len gap |
| 505 | if le n 0 |
| 506 | refuse 2 tuse |
| 507 | end |
| 508 | fd = fopen gap 0 ulong n 0 0 |
| 509 | if lt fd 0 |
| 510 | refuse 3 tio |
| 511 | end |
| 512 | put gfd 0 int fd |
| 513 | sz = fseek int fd 0 2 |
| 514 | if lt sz 0 |
| 515 | refuse 3 tio |
| 516 | end |
| 517 | put gst 0 ulong sz |
| 518 | if eq ac 3 |
| 519 | f = argread 2 gfam 0 len gfam |
| 520 | if le f 0 |
| 521 | refuse 17 tfam |
| 522 | end |
| 523 | put gst 4 ulong f |
| 524 | if not or fameq tf20 fameq tf10 |
| 525 | refuse 17 tfam |
| 526 | end |
| 527 | end |
| 528 | chkhdr |
| 529 | ldsh |
| 530 | ldnames |
| 531 | chkdup |
| 532 | findreq |
| 533 | report |
| 534 | r = fclose int fd |
| 535 | ret 0 |
| 536 | end |