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 / driver / gsp / fw.om
| 1 | note gsp/fw.om: GSP firmware container inspection in seeded Omega (OM-1). Ports the inspect path of |
| 2 | note aienux PR #441 native/kernel/tools/ck_gsp_fw_inspect.c (in_bounds, inspect_buf, print_report, |
| 3 | note cmd_inspect) for firmware version 580.173.02 (default family gb10y). Library unit: no main. |
| 4 | note Prefix fw. Needs gsp/sha2.om concatenated first. The file is read with pread from an open fd, |
| 5 | note never mapped and never a device. Also holds the shared stdout writer (fwo*) that gsp/bindata.om |
| 6 | note and fwprep.om print through, and the strerror texts the C prints. |
| 7 | note Deviation (documented): the section string table is copied into a 16 MiB global; a larger one is |
| 8 | note refused with reason 29 after the C's bounds check and before its NUL check. The C keeps the whole |
| 9 | note file in memory; this port preads only the header, the section table, the string table, the |
| 10 | note version and note bytes, and streams the three SHA-256 digests (same results). |
| 11 | const FWOCAP ulong 65536 |
| 12 | const FWSTMX ulong 16777216 |
| 13 | const FWNONE ulong 999 |
| 14 | const FWFAM10 ulong 1 |
| 15 | const FWFAM20 ulong 2 |
| 16 | global fwob uchar 65536 |
| 17 | global fwon ulong 1 |
| 18 | global fwofd int 1 |
| 19 | global fwoerr ulong 1 |
| 20 | global fwsgb ulong 80 |
| 21 | global fwsgl ulong 80 |
| 22 | global fwok ulong 1 |
| 23 | global fwfam ulong 1 |
| 24 | global fwpath uchar 4096 |
| 25 | global fwpathn ulong 1 |
| 26 | global fwhd uchar 64 |
| 27 | global fwsh uchar 16384 |
| 28 | global fwst uchar 16777216 |
| 29 | global fwstn ulong 1 |
| 30 | global fwbuf uchar 128 |
| 31 | global fwshn ulong 1 |
| 32 | global fwnsec ulong 1 |
| 33 | global fwsn ulong 256 |
| 34 | global fwsty ulong 256 |
| 35 | global fwsof ulong 256 |
| 36 | global fwssz ulong 256 |
| 37 | global fwlen ulong 1 |
| 38 | global fwimx ulong 1 |
| 39 | global fwvx ulong 1 |
| 40 | global fwbx ulong 1 |
| 41 | global fwsx ulong 1 |
| 42 | global fwver uchar 64 |
| 43 | global fwvern ulong 1 |
| 44 | global fwbidh uchar 128 |
| 45 | global fwbidn ulong 1 |
| 46 | global fwhxf uchar 64 |
| 47 | global fwhxi uchar 64 |
| 48 | global fwhxs uchar 64 |
| 49 | note segment k of FWTXT is the text between the k-th and (k+1)-th '|' (segment 0 is empty). |
| 50 | note 1..28 are the C refusal reasons in check order, 29 the deviation reason, 30.. report pieces, |
| 51 | note 54.. strerror texts (glibc wording); 68..70 more strerror texts for fwprep's write paths; |
| 52 | note 71..72 gb20y signature section and family name; 73 gb20y signature size refusal reason. |
| 53 | text FWTXT "|short header: file smaller than an ELF64 header|ELF magic missing|not ELF64 (EI_CLASS)|not little-endian (EI_DATA)|EI_VERSION is not 1|e_type is not ET_REL (the pinned container is a relocatable object)|e_machine is not RISC-V (243)|e_shentsize is not 64|e_shnum is 0: no section header table|e_shnum above this tool's limit (256)|e_shstrndx out of range|section header table outside the file|section string table outside the file or empty|section string table not NUL-terminated|section name index outside the string table|section bounds: offset+size outside the file|missing section .fwimage|.fwimage empty|missing section .fwversion|missing section .note.gnu.build-id|missing section: no signature section for this chip family|signature section empty|version section size not in 1..63|version string not NUL-terminated|version mismatch against the pinned driver version|build-id note too short|build-id note is not an NT_GNU_BUILD_ID note|build-id note descriptor size out of range|section string table above this port's 16 MiB buffer|file | bytes=| family=| expect_version=|section | type=| offset=| size=|(null)|fwversion |build_id |signature_section |sha256_file |sha256_fwimage |sha256_signature |RESULT OK|RESULT REFUSED |.fwimage|.fwversion|.note.gnu.build-id|.fwsignature_gb10y|580.173.02|gb10y|GNU|No such file or directory|Permission denied|Is a directory|File too large|Input/output error|Not a directory|Too many levels of symbolic links|File name too long|Unknown error |open |: |short read|Too many open files|Cannot allocate memory|File exists|No space left on device|Read-only file system|.fwsignature_gb20y|gb20y|signature section size is not 4096|" |
| 54 | text FWHEXD "0123456789abcdef" |
| 55 | note build the segment table once |
| 56 | fn fwsetup to void |
| 57 | if eq get fwok 0 1 |
| 58 | ret |
| 59 | end |
| 60 | put fwok 0 1 |
| 61 | seg = ulong 0 |
| 62 | put fwsgb 0 0 |
| 63 | put fwsgl 0 0 |
| 64 | for i ulong len FWTXT |
| 65 | c = get FWTXT i |
| 66 | if eq c 124 |
| 67 | seg = add seg 1 |
| 68 | put fwsgb seg add i 1 |
| 69 | put fwsgl seg 0 |
| 70 | else |
| 71 | put fwsgl seg add get fwsgl seg 1 |
| 72 | end |
| 73 | end |
| 74 | put fwon 0 0 |
| 75 | put fwofd 0 1 |
| 76 | put fwoerr 0 0 |
| 77 | end |
| 78 | note explicit chip-family selector: FWFAM10 (gb10y, default) or FWFAM20 (gb20y); returns 0 or -1 |
| 79 | fn fwfamily fam ulong to long |
| 80 | if or eq fam FWFAM10 eq fam FWFAM20 |
| 81 | put fwfam 0 fam |
| 82 | ret 0 |
| 83 | end |
| 84 | ret -1 |
| 85 | end |
| 86 | fn fwsigseg to ulong |
| 87 | if eq get fwfam 0 FWFAM20 |
| 88 | ret 71 |
| 89 | end |
| 90 | ret 50 |
| 91 | end |
| 92 | fn fwfamseg to ulong |
| 93 | if eq get fwfam 0 FWFAM20 |
| 94 | ret 72 |
| 95 | end |
| 96 | ret 52 |
| 97 | end |
| 98 | note ---- buffered writer: fwob[0..fwon) goes to fd fwofd; a failed write sets fwoerr 1 ---- |
| 99 | fn fwoflush to long |
| 100 | n = get fwon 0 |
| 101 | p = ulong 0 |
| 102 | while lt p n bound 999999 |
| 103 | r = write get fwofd 0 fwob p sub n p |
| 104 | if le r 0 |
| 105 | put fwoerr 0 1 |
| 106 | put fwon 0 0 |
| 107 | ret -5 |
| 108 | end |
| 109 | p = add p ulong r |
| 110 | end |
| 111 | put fwon 0 0 |
| 112 | ret 0 |
| 113 | end |
| 114 | fn fwoc c uchar to void |
| 115 | if ge get fwon 0 FWOCAP |
| 116 | z = fwoflush |
| 117 | end |
| 118 | n = get fwon 0 |
| 119 | put fwob n c |
| 120 | put fwon 0 add n 1 |
| 121 | end |
| 122 | note print segment k of FWTXT |
| 123 | fn fwos k ulong to void |
| 124 | b = get fwsgb k |
| 125 | for i ulong get fwsgl k |
| 126 | fwoc get FWTXT add b i |
| 127 | end |
| 128 | end |
| 129 | note print v in decimal |
| 130 | fn fwodec v ulong to void |
| 131 | d = new uchar 24 0 |
| 132 | n = ulong 0 |
| 133 | x = v |
| 134 | while lt n 24 bound 24 |
| 135 | put d n add uchar rem x 10 48 |
| 136 | n = add n 1 |
| 137 | x = div x 10 |
| 138 | if eq x 0 |
| 139 | break |
| 140 | end |
| 141 | end |
| 142 | while gt n 0 bound 24 |
| 143 | n = sub n 1 |
| 144 | fwoc get d n |
| 145 | end |
| 146 | end |
| 147 | note print v in lowercase hex without leading zeros (C %x) |
| 148 | fn fwohex v ulong to void |
| 149 | d = new uchar 24 0 |
| 150 | n = ulong 0 |
| 151 | x = v |
| 152 | while lt n 24 bound 24 |
| 153 | put d n get FWHEXD rem x 16 |
| 154 | n = add n 1 |
| 155 | x = div x 16 |
| 156 | if eq x 0 |
| 157 | break |
| 158 | end |
| 159 | end |
| 160 | while gt n 0 bound 24 |
| 161 | n = sub n 1 |
| 162 | fwoc get d n |
| 163 | end |
| 164 | end |
| 165 | note print shhx[0..2n) (call shhex n first) |
| 166 | fn fwohx n ulong to void |
| 167 | for i ulong mul n 2 |
| 168 | fwoc get shhx i |
| 169 | end |
| 170 | end |
| 171 | fn fwopath to void |
| 172 | for i ulong get fwpathn 0 |
| 173 | fwoc get fwpath i |
| 174 | end |
| 175 | end |
| 176 | note print the strerror text of errno e (positive) |
| 177 | fn fwoerrno e ulong to void |
| 178 | k = ulong 0 |
| 179 | if eq e 2 |
| 180 | k = 54 |
| 181 | end |
| 182 | if eq e 13 |
| 183 | k = 55 |
| 184 | end |
| 185 | if eq e 21 |
| 186 | k = 56 |
| 187 | end |
| 188 | if eq e 27 |
| 189 | k = 57 |
| 190 | end |
| 191 | if eq e 5 |
| 192 | k = 58 |
| 193 | end |
| 194 | if eq e 20 |
| 195 | k = 59 |
| 196 | end |
| 197 | if eq e 40 |
| 198 | k = 60 |
| 199 | end |
| 200 | if eq e 36 |
| 201 | k = 61 |
| 202 | end |
| 203 | if eq e 24 |
| 204 | k = 66 |
| 205 | end |
| 206 | if eq e 12 |
| 207 | k = 67 |
| 208 | end |
| 209 | if eq e 17 |
| 210 | k = 68 |
| 211 | end |
| 212 | if eq e 28 |
| 213 | k = 69 |
| 214 | end |
| 215 | if eq e 30 |
| 216 | k = 70 |
| 217 | end |
| 218 | if eq k 0 |
| 219 | fwos 62 |
| 220 | fwodec e |
| 221 | ret |
| 222 | end |
| 223 | fwos k |
| 224 | end |
| 225 | note ---- pread loops, one per destination global: 0, -errno, or -5 when the file ends early ---- |
| 226 | fn fwrdhd fd int off ulong n ulong to long |
| 227 | q = ulong 0 |
| 228 | while lt q n bound 999999 |
| 229 | r = pread fd fwhd q sub n q long add off q |
| 230 | if lt r 0 |
| 231 | ret r |
| 232 | end |
| 233 | if eq r 0 |
| 234 | ret -5 |
| 235 | end |
| 236 | q = add q ulong r |
| 237 | end |
| 238 | ret 0 |
| 239 | end |
| 240 | fn fwrdsh fd int off ulong n ulong to long |
| 241 | q = ulong 0 |
| 242 | while lt q n bound 999999 |
| 243 | r = pread fd fwsh q sub n q long add off q |
| 244 | if lt r 0 |
| 245 | ret r |
| 246 | end |
| 247 | if eq r 0 |
| 248 | ret -5 |
| 249 | end |
| 250 | q = add q ulong r |
| 251 | end |
| 252 | ret 0 |
| 253 | end |
| 254 | fn fwrdst fd int off ulong n ulong to long |
| 255 | q = ulong 0 |
| 256 | while lt q n bound 999999 |
| 257 | r = pread fd fwst q sub n q long add off q |
| 258 | if lt r 0 |
| 259 | ret r |
| 260 | end |
| 261 | if eq r 0 |
| 262 | ret -5 |
| 263 | end |
| 264 | q = add q ulong r |
| 265 | end |
| 266 | ret 0 |
| 267 | end |
| 268 | fn fwrdbf fd int off ulong n ulong to long |
| 269 | q = ulong 0 |
| 270 | while lt q n bound 999999 |
| 271 | r = pread fd fwbuf q sub n q long add off q |
| 272 | if lt r 0 |
| 273 | ret r |
| 274 | end |
| 275 | if eq r 0 |
| 276 | ret -5 |
| 277 | end |
| 278 | q = add q ulong r |
| 279 | end |
| 280 | ret 0 |
| 281 | end |
| 282 | note ---- little-endian readers (C rd16/rd32/rd64) over fwhd, fwsh, fwbuf ---- |
| 283 | fn fwh16 p ulong to ulong |
| 284 | ret or ulong get fwhd p shl ulong get fwhd add p 1 8 |
| 285 | end |
| 286 | fn fwh64 p ulong to ulong |
| 287 | v = ulong 0 |
| 288 | for i ulong 8 |
| 289 | v = or v shl ulong get fwhd add p i mul i 8 |
| 290 | end |
| 291 | ret v |
| 292 | end |
| 293 | fn fws32 p ulong to ulong |
| 294 | v = ulong 0 |
| 295 | for i ulong 4 |
| 296 | v = or v shl ulong get fwsh add p i mul i 8 |
| 297 | end |
| 298 | ret v |
| 299 | end |
| 300 | fn fws64 p ulong to ulong |
| 301 | v = ulong 0 |
| 302 | for i ulong 8 |
| 303 | v = or v shl ulong get fwsh add p i mul i 8 |
| 304 | end |
| 305 | ret v |
| 306 | end |
| 307 | fn fwb32 p ulong to ulong |
| 308 | v = ulong 0 |
| 309 | for i ulong 4 |
| 310 | v = or v shl ulong get fwbuf add p i mul i 8 |
| 311 | end |
| 312 | ret v |
| 313 | end |
| 314 | note C in_bounds: off + size <= len without overflow |
| 315 | fn fwinb off ulong size ulong flen ulong to ulong |
| 316 | if gt off flen |
| 317 | ret 0 |
| 318 | end |
| 319 | if le size sub flen off |
| 320 | ret 1 |
| 321 | end |
| 322 | ret 0 |
| 323 | end |
| 324 | note does the NUL-terminated name at fwst[off] equal segment k? |
| 325 | fn fwnmeq off ulong k ulong to ulong |
| 326 | b = get fwsgb k |
| 327 | l = get fwsgl k |
| 328 | n = get fwstn 0 |
| 329 | for i ulong l |
| 330 | p = add off i |
| 331 | if ge p n |
| 332 | ret 0 |
| 333 | end |
| 334 | if ne get fwst p get FWTXT add b i |
| 335 | ret 0 |
| 336 | end |
| 337 | end |
| 338 | p2 = add off l |
| 339 | if ge p2 n |
| 340 | ret 0 |
| 341 | end |
| 342 | if ne get fwst p2 0 |
| 343 | ret 0 |
| 344 | end |
| 345 | ret 1 |
| 346 | end |
| 347 | note checks 1..12 (C inspect_buf lines 123-138): returns 0, a reason, or 1000 on an I/O error |
| 348 | fn fwchk1 fd int flen ulong to ulong |
| 349 | if lt flen 64 |
| 350 | ret 1 |
| 351 | end |
| 352 | r = fwrdhd fd 0 64 |
| 353 | if ne r 0 |
| 354 | ret 1000 |
| 355 | end |
| 356 | note magic 0x7f 'E' 'L' 'F' = 127 69 76 70 |
| 357 | m = ulong 0 |
| 358 | if ne get fwhd 0 127 |
| 359 | m = 1 |
| 360 | end |
| 361 | if ne get fwhd 1 69 |
| 362 | m = 1 |
| 363 | end |
| 364 | if ne get fwhd 2 76 |
| 365 | m = 1 |
| 366 | end |
| 367 | if ne get fwhd 3 70 |
| 368 | m = 1 |
| 369 | end |
| 370 | if eq m 1 |
| 371 | ret 2 |
| 372 | end |
| 373 | if ne get fwhd 4 2 |
| 374 | ret 3 |
| 375 | end |
| 376 | if ne get fwhd 5 1 |
| 377 | ret 4 |
| 378 | end |
| 379 | if ne get fwhd 6 1 |
| 380 | ret 5 |
| 381 | end |
| 382 | if ne fwh16 16 1 |
| 383 | ret 6 |
| 384 | end |
| 385 | if ne fwh16 18 243 |
| 386 | ret 7 |
| 387 | end |
| 388 | if ne fwh16 58 64 |
| 389 | ret 8 |
| 390 | end |
| 391 | n = fwh16 60 |
| 392 | if eq n 0 |
| 393 | ret 9 |
| 394 | end |
| 395 | if gt n 256 |
| 396 | ret 10 |
| 397 | end |
| 398 | if ge fwh16 62 n |
| 399 | ret 11 |
| 400 | end |
| 401 | if eq fwinb fwh64 40 mul n 64 flen 0 |
| 402 | ret 12 |
| 403 | end |
| 404 | put fwshn 0 n |
| 405 | r = fwrdsh fd fwh64 40 mul n 64 |
| 406 | if ne r 0 |
| 407 | ret 1000 |
| 408 | end |
| 409 | ret 0 |
| 410 | end |
| 411 | note checks 13..16 (C lines 139-154): string table, then every section header in order |
| 412 | fn fwchk2 fd int flen ulong to ulong |
| 413 | si = mul fwh16 62 64 |
| 414 | so = fws64 add si 24 |
| 415 | ss = fws64 add si 32 |
| 416 | if eq ss 0 |
| 417 | ret 13 |
| 418 | end |
| 419 | if eq fwinb so ss flen 0 |
| 420 | ret 13 |
| 421 | end |
| 422 | if gt ss FWSTMX |
| 423 | ret 29 |
| 424 | end |
| 425 | r = fwrdst fd so ss |
| 426 | if ne r 0 |
| 427 | ret 1000 |
| 428 | end |
| 429 | if ne get fwst sub ss 1 0 |
| 430 | ret 14 |
| 431 | end |
| 432 | put fwstn 0 ss |
| 433 | ret fwsecs flen |
| 434 | end |
| 435 | note C lines 145-154: every section header in order; records each before checking the next |
| 436 | fn fwsecs flen ulong to ulong |
| 437 | for i ulong get fwshn 0 |
| 438 | b = mul i 64 |
| 439 | nm = fws32 b |
| 440 | ty = fws32 add b 4 |
| 441 | of = fws64 add b 24 |
| 442 | sz = fws64 add b 32 |
| 443 | if ge nm get fwstn 0 |
| 444 | ret 15 |
| 445 | end |
| 446 | if ne ty 8 |
| 447 | if gt sz 0 |
| 448 | if eq fwinb of sz flen 0 |
| 449 | ret 16 |
| 450 | end |
| 451 | end |
| 452 | end |
| 453 | k = get fwnsec 0 |
| 454 | put fwsn k nm |
| 455 | put fwsty k ty |
| 456 | put fwsof k of |
| 457 | put fwssz k sz |
| 458 | put fwnsec 0 add k 1 |
| 459 | end |
| 460 | ret 0 |
| 461 | end |
| 462 | note checks 17..22 (C lines 155-168): the strcmp chain, last match wins |
| 463 | fn fwchk3 to ulong |
| 464 | put fwimx 0 FWNONE |
| 465 | put fwvx 0 FWNONE |
| 466 | put fwbx 0 FWNONE |
| 467 | put fwsx 0 FWNONE |
| 468 | for i ulong get fwnsec 0 |
| 469 | o = get fwsn i |
| 470 | if eq fwnmeq o 47 1 |
| 471 | put fwimx 0 i |
| 472 | else |
| 473 | if eq fwnmeq o 48 1 |
| 474 | put fwvx 0 i |
| 475 | else |
| 476 | if eq fwnmeq o 49 1 |
| 477 | put fwbx 0 i |
| 478 | else |
| 479 | if eq fwnmeq o fwsigseg 1 |
| 480 | put fwsx 0 i |
| 481 | end |
| 482 | end |
| 483 | end |
| 484 | end |
| 485 | end |
| 486 | x = get fwimx 0 |
| 487 | if eq x FWNONE |
| 488 | ret 17 |
| 489 | end |
| 490 | if eq get fwsty x 8 |
| 491 | ret 18 |
| 492 | end |
| 493 | if eq get fwssz x 0 |
| 494 | ret 18 |
| 495 | end |
| 496 | if eq get fwvx 0 FWNONE |
| 497 | ret 19 |
| 498 | end |
| 499 | if eq get fwbx 0 FWNONE |
| 500 | ret 20 |
| 501 | end |
| 502 | x = get fwsx 0 |
| 503 | if eq x FWNONE |
| 504 | ret 21 |
| 505 | end |
| 506 | if eq get fwsty x 8 |
| 507 | ret 22 |
| 508 | end |
| 509 | if eq get fwssz x 0 |
| 510 | ret 22 |
| 511 | end |
| 512 | if eq get fwfam 0 FWFAM20 |
| 513 | if ne get fwssz x 4096 |
| 514 | ret 73 |
| 515 | end |
| 516 | end |
| 517 | ret 0 |
| 518 | end |
| 519 | note checks 23..25 (C lines 170-175): .fwversion size, NUL, equality with the pin "580.173.02" |
| 520 | fn fwchk4 fd int to ulong |
| 521 | x = get fwvx 0 |
| 522 | sz = get fwssz x |
| 523 | if eq get fwsty x 8 |
| 524 | ret 23 |
| 525 | end |
| 526 | if eq sz 0 |
| 527 | ret 23 |
| 528 | end |
| 529 | if ge sz 64 |
| 530 | ret 23 |
| 531 | end |
| 532 | r = fwrdbf fd get fwsof x sz |
| 533 | if ne r 0 |
| 534 | ret 1000 |
| 535 | end |
| 536 | if ne get fwbuf sub sz 1 0 |
| 537 | ret 24 |
| 538 | end |
| 539 | n = ulong 0 |
| 540 | while lt n sz bound 64 |
| 541 | if eq get fwbuf n 0 |
| 542 | break |
| 543 | end |
| 544 | put fwver n get fwbuf n |
| 545 | n = add n 1 |
| 546 | end |
| 547 | put fwvern 0 n |
| 548 | if ne sz add get fwsgl 51 1 |
| 549 | ret 25 |
| 550 | end |
| 551 | b = get fwsgb 51 |
| 552 | for i ulong get fwsgl 51 |
| 553 | if ne get fwbuf i get FWTXT add b i |
| 554 | ret 25 |
| 555 | end |
| 556 | end |
| 557 | ret 0 |
| 558 | end |
| 559 | note checks 26..28 (C lines 177-185): the NT_GNU_BUILD_ID note, then its hex |
| 560 | fn fwchk5 fd int to ulong |
| 561 | x = get fwbx 0 |
| 562 | sz = get fwssz x |
| 563 | if eq get fwsty x 8 |
| 564 | ret 26 |
| 565 | end |
| 566 | if lt sz 16 |
| 567 | ret 26 |
| 568 | end |
| 569 | n = sz |
| 570 | if gt n 80 |
| 571 | n = 80 |
| 572 | end |
| 573 | r = fwrdbf fd get fwsof x n |
| 574 | if ne r 0 |
| 575 | ret 1000 |
| 576 | end |
| 577 | m = ulong 0 |
| 578 | if ne fwb32 0 4 |
| 579 | m = 1 |
| 580 | end |
| 581 | note "GNU\0" = 71 78 85 0 |
| 582 | if ne get fwbuf 12 71 |
| 583 | m = 1 |
| 584 | end |
| 585 | if ne get fwbuf 13 78 |
| 586 | m = 1 |
| 587 | end |
| 588 | if ne get fwbuf 14 85 |
| 589 | m = 1 |
| 590 | end |
| 591 | if ne get fwbuf 15 0 |
| 592 | m = 1 |
| 593 | end |
| 594 | if ne fwb32 8 3 |
| 595 | m = 1 |
| 596 | end |
| 597 | if eq m 1 |
| 598 | ret 27 |
| 599 | end |
| 600 | d = fwb32 4 |
| 601 | if eq d 0 |
| 602 | ret 28 |
| 603 | end |
| 604 | if gt d 64 |
| 605 | ret 28 |
| 606 | end |
| 607 | if gt add 16 d sz |
| 608 | ret 28 |
| 609 | end |
| 610 | fwbidhx d |
| 611 | ret 0 |
| 612 | end |
| 613 | note lowercase hex of the d note descriptor bytes fwbuf[16..16+d) into fwbidh (C hex) |
| 614 | fn fwbidhx d ulong to void |
| 615 | for i ulong d |
| 616 | c = get fwbuf add 16 i |
| 617 | put fwbidh mul i 2 get FWHEXD shr c 4 |
| 618 | put fwbidh add mul i 2 1 get FWHEXD and c 15 |
| 619 | end |
| 620 | put fwbidn 0 mul d 2 |
| 621 | end |
| 622 | note the three SHA-256 digests as lowercase hex in fwhxf / fwhxi / fwhxs; 0 or 1000 on an I/O error |
| 623 | fn fwdig fd int flen ulong to ulong |
| 624 | sh2init 0 |
| 625 | sh2init 1 |
| 626 | sh2init 2 |
| 627 | r = sh2file 0 fd 0 flen |
| 628 | if ne r 0 |
| 629 | ret 1000 |
| 630 | end |
| 631 | x = get fwimx 0 |
| 632 | r = sh2file 1 fd get fwsof x get fwssz x |
| 633 | if ne r 0 |
| 634 | ret 1000 |
| 635 | end |
| 636 | x = get fwsx 0 |
| 637 | r = sh2file 2 fd get fwsof x get fwssz x |
| 638 | if ne r 0 |
| 639 | ret 1000 |
| 640 | end |
| 641 | sh2fin 0 |
| 642 | shhex 32 |
| 643 | for i ulong 64 |
| 644 | put fwhxf i get shhx i |
| 645 | end |
| 646 | sh2fin 1 |
| 647 | shhex 32 |
| 648 | for i ulong 64 |
| 649 | put fwhxi i get shhx i |
| 650 | end |
| 651 | sh2fin 2 |
| 652 | shhex 32 |
| 653 | for i ulong 64 |
| 654 | put fwhxs i get shhx i |
| 655 | end |
| 656 | ret 0 |
| 657 | end |
| 658 | note all checks in the C order: 0 = every check passed, 1..29 = refusal reason, 1000 = I/O error |
| 659 | fn fwcheck fd int flen ulong to ulong |
| 660 | put fwnsec 0 0 |
| 661 | put fwlen 0 flen |
| 662 | e = fwchk1 fd flen |
| 663 | if ne e 0 |
| 664 | ret e |
| 665 | end |
| 666 | e = fwchk2 fd flen |
| 667 | if ne e 0 |
| 668 | ret e |
| 669 | end |
| 670 | e = fwchk3 |
| 671 | if ne e 0 |
| 672 | ret e |
| 673 | end |
| 674 | e = fwchk4 fd |
| 675 | if ne e 0 |
| 676 | ret e |
| 677 | end |
| 678 | e = fwchk5 fd |
| 679 | if ne e 0 |
| 680 | ret e |
| 681 | end |
| 682 | ret fwdig fd flen |
| 683 | end |
| 684 | note print the NUL-terminated name at fwst[off], or (null) when it is empty |
| 685 | fn fwoname off ulong to void |
| 686 | if eq get fwst off 0 |
| 687 | fwos 38 |
| 688 | ret |
| 689 | end |
| 690 | note the string table is NUL-terminated (check 14), so the name ends inside it; chunked for the |
| 691 | note 999999 bound (a name may run up to 16 MiB) |
| 692 | p = off |
| 693 | go = ulong 1 |
| 694 | while eq go 1 bound 20 |
| 695 | while ne get fwst p 0 bound 999999 |
| 696 | fwoc get fwst p |
| 697 | p = add p 1 |
| 698 | end |
| 699 | if eq get fwst p 0 |
| 700 | go = 0 |
| 701 | end |
| 702 | end |
| 703 | end |
| 704 | note C cmd_inspect + print_report for an open fd of flen bytes, path in fwpath[0..fwpathn). |
| 705 | note Returns 0 = RESULT OK, 2 = RESULT REFUSED, 1 = I/O error (the C prints "short read" on |
| 706 | note stderr and nothing on stdout; here the buffered file line is dropped and the caller reports). |
| 707 | fn fwinsp fd int flen ulong to ulong |
| 708 | fwsetup |
| 709 | mk = get fwon 0 |
| 710 | fwos 30 |
| 711 | fwopath |
| 712 | fwos 31 |
| 713 | fwodec flen |
| 714 | fwos 32 |
| 715 | fwos fwfamseg |
| 716 | fwos 33 |
| 717 | fwos 51 |
| 718 | fwoc 10 |
| 719 | e = fwcheck fd flen |
| 720 | if eq e 1000 |
| 721 | put fwon 0 mk |
| 722 | ret 1 |
| 723 | end |
| 724 | for i ulong get fwnsec 0 |
| 725 | fwos 34 |
| 726 | fwodec i |
| 727 | fwoc 32 |
| 728 | fwoname get fwsn i |
| 729 | fwos 35 |
| 730 | fwodec get fwsty i |
| 731 | fwos 36 |
| 732 | fwodec get fwsof i |
| 733 | fwos 37 |
| 734 | fwodec get fwssz i |
| 735 | fwoc 10 |
| 736 | end |
| 737 | if ne e 0 |
| 738 | fwos 46 |
| 739 | fwos e |
| 740 | fwoc 10 |
| 741 | ret 2 |
| 742 | end |
| 743 | fwos 39 |
| 744 | for i ulong get fwvern 0 |
| 745 | fwoc get fwver i |
| 746 | end |
| 747 | fwoc 10 |
| 748 | fwos 40 |
| 749 | for i ulong get fwbidn 0 |
| 750 | fwoc get fwbidh i |
| 751 | end |
| 752 | fwoc 10 |
| 753 | fwos 41 |
| 754 | fwos fwsigseg |
| 755 | fwos 37 |
| 756 | fwodec get fwssz get fwsx 0 |
| 757 | fwoc 10 |
| 758 | fwos 42 |
| 759 | for i ulong 64 |
| 760 | fwoc get fwhxf i |
| 761 | end |
| 762 | fwoc 10 |
| 763 | fwos 43 |
| 764 | for i ulong 64 |
| 765 | fwoc get fwhxi i |
| 766 | end |
| 767 | fwos 37 |
| 768 | fwodec get fwssz get fwimx 0 |
| 769 | fwoc 10 |
| 770 | fwos 44 |
| 771 | for i ulong 64 |
| 772 | fwoc get fwhxs i |
| 773 | end |
| 774 | fwoc 10 |
| 775 | fwos 45 |
| 776 | fwoc 10 |
| 777 | ret 0 |
| 778 | end |