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
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note main.om: hwman unit 4, commands and the target lifecycle |
| 3 | note discover -> claim -> prepare -> submit -> complete -> quiesce -> release |
| 4 | note Usage (MAN is the manifest; every command checks it first and refuses a malformed one, exit 3): |
| 5 | note hwman check MAN schema and rule check; prints CHECK rows families admitted |
| 6 | note hwman doctor MAN one TARGET line per row |
| 7 | note hwman admit MAN OP [TARGET] admission: TARGET (or the first admitted row offering |
| 8 | note OP) must exist, offer OP, be admitted and have its |
| 9 | note device node present |
| 10 | note hwman run MAN TARGET OP ARG GEN STATE [EXPECT] |
| 11 | note hwman fault MAN TARGET KIND GEN STATE send a malformed frame (tpm: badsize, badcc); passes only |
| 12 | note on the one expected refusal: badsize kernel errno -22, |
| 13 | note badcc rc 0x000b0143 (TPM2_RC_COMMAND_CODE from the |
| 14 | note kernel resource manager layer, see tpm.om) |
| 15 | note check binds every hwman- runner to the closed table in core.om (TRUN: family, route, operations), |
| 16 | note so check, admit and run all use that one table. |
| 17 | note STATE is the handle file "<generation> <target>\n" (created empty = generation 0); claim takes an |
| 18 | note exclusive non-blocking flock on it; GEN must equal the stored generation. Before submission byte 0 |
| 19 | note becomes 'x' (synced); release then writes and syncs bytes 1.., then byte 0, then syncs again. |
| 20 | note Any failed or short write, or failed sync, refuses with 74 instead of PASS; the handle is then |
| 21 | note unreadable (exit 7), or after only the last sync failed holds the advanced record: never an |
| 22 | note earlier generation. A replayed GEN is stale. timeout_ms is overrun detection, not cancellation: |
| 23 | note CPU compares the elapsed time after the operation returns; TPM bounds the wait for the response |
| 24 | note (waitio) and refuses, but the command already sent is not cancelled in the chip. |
| 25 | note EXPECT is the independent oracle value (decimal). |
| 26 | note Exit: 0 PASS, 2 usage, 3 manifest refused, 4 unknown target, 5 unsupported op, 6 not admitted, |
| 27 | note 7 stale generation or handle, 8 wrong device, 9 busy, 10 offline, 11 timeout, |
| 28 | note 12 oracle mismatch, 13 device error, 14 invalid argument, 74 I/O. Traps exit 100 + code. |
| 29 | |
| 30 | text MUSE "usage: hwman check MAN | doctor MAN | admit MAN OP [TARGET] | run MAN TARGET OP ARG GEN STATE [EXPECT] | fault MAN TARGET KIND GEN STATE\n" |
| 31 | text MCHECK "check" |
| 32 | text MDOCTOR "doctor" |
| 33 | text MADMIT "admit" |
| 34 | text MRUN "run" |
| 35 | text MFAULT "fault" |
| 36 | text MRCPU "hwman-cpu" |
| 37 | text MRTPM "hwman-tpm" |
| 38 | text ORC " rc=" |
| 39 | text OSTWR "state_write_failed state=" |
| 40 | text OFRES "unexpected_fault_result kind=" |
| 41 | text MYES "YES" |
| 42 | text OADM " admit=" |
| 43 | text OCHECK "CHECK rows=" |
| 44 | text OFAMS " families=21 admitted=" |
| 45 | text OTARGET "TARGET " |
| 46 | text OADMIT "ADMIT target=" |
| 47 | text ORUNNER " runner=" |
| 48 | text OROUTE " route=" |
| 49 | text OOP " op=" |
| 50 | text OSTATUS " status=" |
| 51 | text OBLOCK " blocker=" |
| 52 | text OFAM " family=" |
| 53 | text OUNKNOWN "unknown_target target=" |
| 54 | text OUNSUP "unsupported_op target=" |
| 55 | text ONOADM "not_admitted target=" |
| 56 | text ONOTGT "no_admitted_target op=" |
| 57 | text OSTWRONG "wrong_handle target=" |
| 58 | text OOFFLINE "offline target=" |
| 59 | text OERR " errno=" |
| 60 | text OINVAL "invalid_argument arg=" |
| 61 | text OBUSY "busy state=" |
| 62 | text OSTALE "stale_generation have=" |
| 63 | text OSTCORR "state_unreadable state=" |
| 64 | text OSTATEQ " state=" |
| 65 | text OWANT " want=" |
| 66 | text OUNPIN "wrong_device unpinned cpus_allowed_list" |
| 67 | text OMOVED "wrong_device processor=" |
| 68 | text OPIN " pinned=" |
| 69 | text OMIDR "wrong_device midr=" |
| 70 | text OEXPECT " manifest=" |
| 71 | text OMANUF "wrong_device tpm_manufacturer=" |
| 72 | text OTIME "timeout elapsed_ns=" |
| 73 | text OBUDGET " budget_ns=" |
| 74 | text OMISM "oracle_mismatch result=" |
| 75 | text ODEV "device_error rc=" |
| 76 | text OSTEP "STEP " |
| 77 | text SDISC "discover" |
| 78 | text SCLAIM "claim" |
| 79 | text SPREP "prepare" |
| 80 | text SSUB "submit" |
| 81 | text SCOMP "complete" |
| 82 | text SQUI "quiesce" |
| 83 | text SREL "release" |
| 84 | text OCPU " cpu=" |
| 85 | text OMIDREQ " midr_el1=" |
| 86 | text OMANEQ " tpm_manufacturer=" |
| 87 | text OFW " firmware=" |
| 88 | text OREV " revision=" |
| 89 | text OGEN " generation=" |
| 90 | text ORES " result=" |
| 91 | text OELAP " elapsed_ns=" |
| 92 | text ORECEIPT "RECEIPT target=" |
| 93 | text OARROW "->" |
| 94 | text OARG " arg=" |
| 95 | text OOUT " outcome=" |
| 96 | text OPASS "PASS" |
| 97 | text OFAULT "FAULT target=" |
| 98 | text OKIND " kind=" |
| 99 | text OLAYK " layer=kernel errno=" |
| 100 | text OLAYT " layer=tpmrm rc=" |
| 101 | text OBADK "unsupported_fault kind=" |
| 102 | text ODASH "-" |
| 103 | |
| 104 | global gstate uchar 256 |
| 105 | global gtmp uchar 32 |
| 106 | |
| 107 | fn refuse code int why view uchar * to int |
| 108 | ot TREF |
| 109 | ot why |
| 110 | ret code |
| 111 | end |
| 112 | |
| 113 | fn rend code int to int |
| 114 | oc 10 |
| 115 | oflush |
| 116 | ret code |
| 117 | end |
| 118 | |
| 119 | note the STATE argument s could not be made durable: refuse with I/O, no PASS |
| 120 | fn stfail s ulong to int |
| 121 | c = refuse XIO OSTWR |
| 122 | oarg s |
| 123 | ret rend c |
| 124 | end |
| 125 | |
| 126 | note the row's device node exists (faccess F_OK); 0 or -errno |
| 127 | fn nodeok to long |
| 128 | pclear |
| 129 | pfield CNODE |
| 130 | ret faccess gpath 0 get gst 5 0 |
| 131 | end |
| 132 | |
| 133 | note gate target argument t for op argument o (o = 0: no op check); leaves the row split |
| 134 | fn gate t ulong o ulong to int |
| 135 | r = findrow t |
| 136 | if lt r 0 |
| 137 | c = refuse XUNKNOWN OUNKNOWN |
| 138 | oarg t |
| 139 | ret rend c |
| 140 | end |
| 141 | if gt o 0 |
| 142 | if not flisthas COPS o |
| 143 | c = refuse XUNSUP OUNSUP |
| 144 | ofield CID |
| 145 | ot OOP |
| 146 | oarg o |
| 147 | ret rend c |
| 148 | end |
| 149 | end |
| 150 | if not feq CADMIT MYES |
| 151 | c = refuse XAUTH ONOADM |
| 152 | ofield CID |
| 153 | ot OSTATUS |
| 154 | ofield CSTAT |
| 155 | ot OBLOCK |
| 156 | ofield CBLOCK |
| 157 | ret rend c |
| 158 | end |
| 159 | note a runner outside the closed table is a manifest refusal in check (exit 3) |
| 160 | e = nodeok |
| 161 | if lt e 0 |
| 162 | c = refuse XOFFLINE OOFFLINE |
| 163 | ofield CID |
| 164 | ot OERR |
| 165 | osdec e |
| 166 | ret rend c |
| 167 | end |
| 168 | ret XOK |
| 169 | end |
| 170 | |
| 171 | fn cmdcheck to int |
| 172 | c = check |
| 173 | if ne c XOK |
| 174 | ret c |
| 175 | end |
| 176 | ot OCHECK |
| 177 | odec get gst 3 |
| 178 | ot OFAMS |
| 179 | odec get gst 8 |
| 180 | ret rend XOK |
| 181 | end |
| 182 | |
| 183 | fn cmddoctor to int |
| 184 | c = check |
| 185 | if ne c XOK |
| 186 | ret c |
| 187 | end |
| 188 | for r ulong get gst 3 |
| 189 | q = rowat get grow r |
| 190 | ot OTARGET |
| 191 | ofield CID |
| 192 | ot OFAM |
| 193 | ofield CFAM |
| 194 | ot OROUTE |
| 195 | ofield CROUTE |
| 196 | ot OSTATUS |
| 197 | ofield CSTAT |
| 198 | ot OADM |
| 199 | ofield CADMIT |
| 200 | ot ORUNNER |
| 201 | ofield CRUNNER |
| 202 | ot OOP |
| 203 | ofield COPS |
| 204 | oc 10 |
| 205 | end |
| 206 | ot OCHECK |
| 207 | odec get gst 3 |
| 208 | ot OFAMS |
| 209 | odec get gst 8 |
| 210 | ret rend XOK |
| 211 | end |
| 212 | |
| 213 | fn oadmit to void |
| 214 | ot OADMIT |
| 215 | ofield CID |
| 216 | ot OFAM |
| 217 | ofield CFAM |
| 218 | ot OROUTE |
| 219 | ofield CROUTE |
| 220 | ot ORUNNER |
| 221 | ofield CRUNNER |
| 222 | end |
| 223 | |
| 224 | fn cmdadmit to int |
| 225 | c = check |
| 226 | if ne c XOK |
| 227 | ret c |
| 228 | end |
| 229 | if eq argc 5 |
| 230 | g = gate 4 3 |
| 231 | if ne g XOK |
| 232 | ret g |
| 233 | end |
| 234 | oadmit |
| 235 | ot OOP |
| 236 | oarg 3 |
| 237 | ret rend XOK |
| 238 | end |
| 239 | for r ulong get gst 3 |
| 240 | q = rowat get grow r |
| 241 | if and flisthas COPS 3 feq CADMIT MYES |
| 242 | if ge nodeok 0 |
| 243 | oadmit |
| 244 | ot OOP |
| 245 | oarg 3 |
| 246 | ret rend XOK |
| 247 | end |
| 248 | end |
| 249 | end |
| 250 | c = refuse XUNSUP ONOTGT |
| 251 | oarg 3 |
| 252 | ret rend c |
| 253 | end |
| 254 | |
| 255 | note decimal text of v in gtmp; returns its length |
| 256 | fn tmpdec v ulong to ulong |
| 257 | d = new uchar 20 0 |
| 258 | k = ulong 0 |
| 259 | x = v |
| 260 | while true bound 21 |
| 261 | put d k uchar add rem x 10 48 |
| 262 | k = add k 1 |
| 263 | x = div x 10 |
| 264 | if eq x 0 |
| 265 | break |
| 266 | end |
| 267 | end |
| 268 | for i ulong k |
| 269 | put gtmp i get d sub sub k i 1 |
| 270 | end |
| 271 | ret k |
| 272 | end |
| 273 | |
| 274 | note parse the n handle-file bytes in gstate, "<generation> <target>\n": the generation, -1 when |
| 275 | note malformed, -2 when the stored target is not the split row's id |
| 276 | fn stparse n ulong to long |
| 277 | i = ulong 0 |
| 278 | have = ulong 0 |
| 279 | while true bound 20 |
| 280 | if ge i n |
| 281 | break |
| 282 | end |
| 283 | ch = get gstate i |
| 284 | if or lt ch 48 gt ch 57 |
| 285 | break |
| 286 | end |
| 287 | have = add mul have 10 ulong sub ch 48 |
| 288 | i = add i 1 |
| 289 | end |
| 290 | if or eq i 0 ge i n |
| 291 | ret -1 |
| 292 | end |
| 293 | if ne get gstate i 32 |
| 294 | ret -1 |
| 295 | end |
| 296 | o = add i 1 |
| 297 | idl = get gfl CID |
| 298 | if ne add add o idl 1 n |
| 299 | ret -2 |
| 300 | end |
| 301 | if ne get gstate add o idl 10 |
| 302 | ret -2 |
| 303 | end |
| 304 | fo = get gfo CID |
| 305 | for j ulong idl |
| 306 | if ne get gstate add o j get gman add fo j |
| 307 | ret -2 |
| 308 | end |
| 309 | end |
| 310 | ret long have |
| 311 | end |
| 312 | |
| 313 | fn unclaim fd int to void |
| 314 | u = flock fd 8 |
| 315 | c = fclose fd |
| 316 | end |
| 317 | |
| 318 | note claim: open and exclusively lock STATE (argument s); check generation argument g against it. |
| 319 | note Returns the descriptor (>= 0) or minus an exit class. |
| 320 | fn claim s ulong g ulong to long |
| 321 | want = argdec g |
| 322 | if lt want 0 |
| 323 | c = refuse XINVAL OINVAL |
| 324 | oarg g |
| 325 | ret long sub 0 rend c |
| 326 | end |
| 327 | pclear |
| 328 | parg s |
| 329 | fd = fopen gpath 0 get gst 5 66 384 |
| 330 | if lt fd 0 |
| 331 | c = refuse XIO OSTCORR |
| 332 | oarg s |
| 333 | ret long sub 0 rend c |
| 334 | end |
| 335 | k = flock int fd 6 |
| 336 | if lt k 0 |
| 337 | c = refuse XBUSY OBUSY |
| 338 | oarg s |
| 339 | cl = fclose int fd |
| 340 | ret long sub 0 rend c |
| 341 | end |
| 342 | n = pread int fd gstate 0 len gstate 0 |
| 343 | have = long 0 |
| 344 | if gt n 0 |
| 345 | have = stparse ulong n |
| 346 | end |
| 347 | if lt n 0 |
| 348 | have = long -1 |
| 349 | end |
| 350 | if eq have -2 |
| 351 | unclaim int fd |
| 352 | c = refuse XSTALE OSTWRONG |
| 353 | ofield CID |
| 354 | ot OSTATEQ |
| 355 | oarg s |
| 356 | ret long sub 0 rend c |
| 357 | end |
| 358 | if lt have 0 |
| 359 | unclaim int fd |
| 360 | c = refuse XSTALE OSTCORR |
| 361 | oarg s |
| 362 | ret long sub 0 rend c |
| 363 | end |
| 364 | if ne have want |
| 365 | unclaim int fd |
| 366 | c = refuse XSTALE OSTALE |
| 367 | osdec have |
| 368 | ot OWANT |
| 369 | osdec want |
| 370 | ret long sub 0 rend c |
| 371 | end |
| 372 | ot OSTEP |
| 373 | ot SCLAIM |
| 374 | ot OGEN |
| 375 | osdec have |
| 376 | oc 10 |
| 377 | put gst 15 ulong have |
| 378 | ret fd |
| 379 | end |
| 380 | |
| 381 | note inflight: before submission byte 0 of the handle becomes 'x' and is synced, so the handle no |
| 382 | note longer parses (state_unreadable, exit 7) until release completes; true when both succeeded |
| 383 | fn inflight fd int to bool |
| 384 | put gtmp 0 120 |
| 385 | w = pwrite fd gtmp 0 1 0 |
| 386 | if ne w 1 |
| 387 | ret false |
| 388 | end |
| 389 | s = fsync fd |
| 390 | ret eq s 0 |
| 391 | end |
| 392 | |
| 393 | note release: when bump, write "<generation+1> <id>\n" over the in-flight handle, bytes 1.. first and |
| 394 | note synced, then byte 0 and synced, so a failed, short or unsynced write leaves byte 0 'x' and the |
| 395 | note handle refused. The generation only grows, so the record never shrinks and needs no truncation. |
| 396 | note Unlocks and closes; returns XOK, or XIO when a write was short or failed or a sync failed. |
| 397 | fn release fd int bump bool to int |
| 398 | code = XOK |
| 399 | if bump |
| 400 | k = tmpdec add get gst 15 1 |
| 401 | for i ulong k |
| 402 | put gstate i get gtmp i |
| 403 | end |
| 404 | put gstate k 32 |
| 405 | o = get gfo CID |
| 406 | l = get gfl CID |
| 407 | for j ulong l |
| 408 | put gstate add add k 1 j get gman add o j |
| 409 | end |
| 410 | put gstate add add k 1 l 10 |
| 411 | m = add add k 1 l |
| 412 | w = pwrite fd gstate 1 m 1 |
| 413 | s = fsync fd |
| 414 | ok = and eq w long m eq s 0 |
| 415 | if ok |
| 416 | w = pwrite fd gstate 0 1 0 |
| 417 | s = fsync fd |
| 418 | ok = and eq w 1 eq s 0 |
| 419 | end |
| 420 | if not ok |
| 421 | code = XIO |
| 422 | end |
| 423 | end |
| 424 | u = flock fd 8 |
| 425 | c = fclose fd |
| 426 | if ne code XOK |
| 427 | ret code |
| 428 | end |
| 429 | ot OSTEP |
| 430 | ot SREL |
| 431 | ot OGEN |
| 432 | if bump |
| 433 | odec add get gst 15 1 |
| 434 | else |
| 435 | odec get gst 15 |
| 436 | end |
| 437 | oc 10 |
| 438 | ret XOK |
| 439 | end |
| 440 | |
| 441 | fn oreceipt a ulong o ulong to void |
| 442 | ot ORECEIPT |
| 443 | ofield CID |
| 444 | ot OFAM |
| 445 | ofield CFAM |
| 446 | ot OROUTE |
| 447 | ofield CROUTE |
| 448 | ot ORUNNER |
| 449 | ofield CRUNNER |
| 450 | ot OOP |
| 451 | oarg o |
| 452 | ot OARG |
| 453 | oarg a |
| 454 | ot OGEN |
| 455 | odec get gst 15 |
| 456 | ot OARROW |
| 457 | odec add get gst 15 1 |
| 458 | end |
| 459 | |
| 460 | note decimal text of v equals argument a |
| 461 | fn expok a ulong v ulong to bool |
| 462 | k = tmpdec v |
| 463 | if ne k get gal a |
| 464 | ret false |
| 465 | end |
| 466 | o = get gao a |
| 467 | for i ulong k |
| 468 | if ne get gtmp i get gav add o i |
| 469 | ret false |
| 470 | end |
| 471 | end |
| 472 | ret true |
| 473 | end |
| 474 | |
| 475 | note CPU discovery: pinned processor, current processor and MIDR_EL1 against the manifest identity. |
| 476 | note Returns the processor (MIDR in gst 10) or minus an exit class. |
| 477 | fn cpudisc to long |
| 478 | pin = cpupin |
| 479 | if lt pin 0 |
| 480 | c = refuse XWRDEV OUNPIN |
| 481 | ret long sub 0 rend c |
| 482 | end |
| 483 | cur = cpucur |
| 484 | if ne cur pin |
| 485 | c = refuse XWRDEV OMOVED |
| 486 | osdec cur |
| 487 | ot OPIN |
| 488 | osdec pin |
| 489 | ret long sub 0 rend c |
| 490 | end |
| 491 | m = cpumidr ulong pin |
| 492 | if lt m 0 |
| 493 | c = refuse XOFFLINE OOFFLINE |
| 494 | ofield CID |
| 495 | ot OERR |
| 496 | osdec m |
| 497 | ret long sub 0 rend c |
| 498 | end |
| 499 | if ne m fidhex |
| 500 | c = refuse XWRDEV OMIDR |
| 501 | ohx ulong m 8 |
| 502 | ot OEXPECT |
| 503 | ofield CIDENT |
| 504 | ot OCPU |
| 505 | osdec pin |
| 506 | ret long sub 0 rend c |
| 507 | end |
| 508 | ot OSTEP |
| 509 | ot SDISC |
| 510 | ot OCPU |
| 511 | osdec pin |
| 512 | ot OMIDREQ |
| 513 | ohx ulong m 8 |
| 514 | oc 10 |
| 515 | put gst 10 ulong m |
| 516 | ret pin |
| 517 | end |
| 518 | |
| 519 | note CPU lifecycle for argv: 2 MAN 3 TARGET 4 OP 5 ARG 6 GEN 7 STATE [8 EXPECT] |
| 520 | fn runcpu to int |
| 521 | pin = cpudisc |
| 522 | if lt pin 0 |
| 523 | ret int sub 0 pin |
| 524 | end |
| 525 | m = get gst 10 |
| 526 | fd = claim 7 6 |
| 527 | if lt fd 0 |
| 528 | ret int sub 0 fd |
| 529 | end |
| 530 | n = argdec 5 |
| 531 | if or lt n 1 gt n long CPUMAXN |
| 532 | x = release int fd false |
| 533 | c = refuse XINVAL OINVAL |
| 534 | oarg 5 |
| 535 | ret rend c |
| 536 | end |
| 537 | ot OSTEP |
| 538 | ot SPREP |
| 539 | ot OARG |
| 540 | odec ulong n |
| 541 | oc 10 |
| 542 | if not inflight int fd |
| 543 | x = release int fd false |
| 544 | ret stfail 7 |
| 545 | end |
| 546 | tmo = mul ulong fdec CTMO 1000000 |
| 547 | t0 = monons |
| 548 | v = cpuop 4 ulong n |
| 549 | t1 = monons |
| 550 | el = ulong sub t1 t0 |
| 551 | if eq get gst 9 1 |
| 552 | x = release int fd false |
| 553 | c = refuse XUNSUP OUNSUP |
| 554 | ofield CID |
| 555 | ot OOP |
| 556 | oarg 4 |
| 557 | ret rend c |
| 558 | end |
| 559 | ot OSTEP |
| 560 | ot SSUB |
| 561 | ot OELAP |
| 562 | odec el |
| 563 | oc 10 |
| 564 | code = XOK |
| 565 | if eq argc 9 |
| 566 | if not expok 8 v |
| 567 | code = XMISM |
| 568 | end |
| 569 | end |
| 570 | ot OSTEP |
| 571 | ot SCOMP |
| 572 | ot ORES |
| 573 | odec v |
| 574 | oc 10 |
| 575 | cur2 = cpucur |
| 576 | if ne cur2 pin |
| 577 | code = XWRDEV |
| 578 | end |
| 579 | if gt el tmo |
| 580 | code = XTIMEOUT |
| 581 | end |
| 582 | ot OSTEP |
| 583 | ot SQUI |
| 584 | ot OCPU |
| 585 | osdec cur2 |
| 586 | ot OELAP |
| 587 | odec el |
| 588 | ot OBUDGET |
| 589 | odec tmo |
| 590 | oc 10 |
| 591 | rl = release int fd true |
| 592 | if ne rl XOK |
| 593 | ret stfail 7 |
| 594 | end |
| 595 | oreceipt 5 4 |
| 596 | ot OCPU |
| 597 | odec ulong pin |
| 598 | ot OMIDREQ |
| 599 | ohx ulong m 8 |
| 600 | ot ORES |
| 601 | odec v |
| 602 | ot OELAP |
| 603 | odec el |
| 604 | if eq code XOK |
| 605 | ot OOUT |
| 606 | ot OPASS |
| 607 | ret rend XOK |
| 608 | end |
| 609 | oc 10 |
| 610 | if eq code XMISM |
| 611 | c = refuse code OMISM |
| 612 | odec v |
| 613 | ot OWANT |
| 614 | oarg 8 |
| 615 | ret rend c |
| 616 | end |
| 617 | if eq code XTIMEOUT |
| 618 | c = refuse code OTIME |
| 619 | odec el |
| 620 | ot OBUDGET |
| 621 | odec tmo |
| 622 | ret rend c |
| 623 | end |
| 624 | c = refuse code OMOVED |
| 625 | osdec cur2 |
| 626 | ot OPIN |
| 627 | osdec pin |
| 628 | ret rend c |
| 629 | end |
| 630 | |
| 631 | note TPM discovery: open the node, read the fixed properties, compare the manufacturer. |
| 632 | note Returns the descriptor or minus an exit class. |
| 633 | fn tpmdisc to long |
| 634 | fd = tpmopen |
| 635 | if lt fd 0 |
| 636 | code = XOFFLINE |
| 637 | if or eq fd -13 eq fd -1 |
| 638 | code = XAUTH |
| 639 | end |
| 640 | c = refuse code OOFFLINE |
| 641 | ofield CID |
| 642 | ot OERR |
| 643 | osdec fd |
| 644 | ret long sub 0 rend c |
| 645 | end |
| 646 | tmo = long mul ulong fdec CTMO 1000000 |
| 647 | r = tpmprops int fd tmo |
| 648 | if ne r 0 |
| 649 | cl = fclose int fd |
| 650 | code = XDEVERR |
| 651 | if eq r TPMETIME |
| 652 | code = XTIMEOUT |
| 653 | end |
| 654 | c = refuse code ODEV |
| 655 | osdec r |
| 656 | ret long sub 0 rend c |
| 657 | end |
| 658 | want = fidhex |
| 659 | if ne long get gst 10 want |
| 660 | cl = fclose int fd |
| 661 | c = refuse XWRDEV OMANUF |
| 662 | ohx get gst 10 8 |
| 663 | ot OEXPECT |
| 664 | ofield CIDENT |
| 665 | ret long sub 0 rend c |
| 666 | end |
| 667 | ot OSTEP |
| 668 | ot SDISC |
| 669 | ot OMANEQ |
| 670 | ohx get gst 10 8 |
| 671 | ot OFW |
| 672 | ohx get gst 11 8 |
| 673 | oc 46 |
| 674 | ohx get gst 12 8 |
| 675 | ot OREV |
| 676 | odec get gst 13 |
| 677 | oc 10 |
| 678 | ret fd |
| 679 | end |
| 680 | note TPM result: the fixed properties, or the n random bytes in hex |
| 681 | fn tpmout isprops bool n ulong to void |
| 682 | if isprops |
| 683 | ot OMANEQ |
| 684 | ohx get gst 10 8 |
| 685 | ot OFW |
| 686 | ohx get gst 11 8 |
| 687 | oc 46 |
| 688 | ohx get gst 12 8 |
| 689 | ret |
| 690 | end |
| 691 | ot ORES |
| 692 | for i ulong n |
| 693 | b = ulong get gresp add 12 i |
| 694 | oc hexc shr b 4 |
| 695 | oc hexc and b 15 |
| 696 | end |
| 697 | end |
| 698 | |
| 699 | note TPM prepare: the operand of argument 5 (props: 0; getrandom: 1..TPMMAXR), or -1 |
| 700 | fn tpmarg isprops bool to long |
| 701 | n = argdec 5 |
| 702 | if lt n 0 |
| 703 | ret -1 |
| 704 | end |
| 705 | if isprops |
| 706 | if ne n 0 |
| 707 | ret -1 |
| 708 | end |
| 709 | ret n |
| 710 | end |
| 711 | if or lt n 1 gt n long TPMMAXR |
| 712 | ret -1 |
| 713 | end |
| 714 | ret n |
| 715 | end |
| 716 | |
| 717 | fn closefd fd int to void |
| 718 | c = fclose fd |
| 719 | end |
| 720 | |
| 721 | fn runtpm to int |
| 722 | fd = tpmdisc |
| 723 | if lt fd 0 |
| 724 | ret int sub 0 fd |
| 725 | end |
| 726 | sf = claim 7 6 |
| 727 | if lt sf 0 |
| 728 | closefd int fd |
| 729 | ret int sub 0 sf |
| 730 | end |
| 731 | isprops = argeq 4 TPMPROPS |
| 732 | n = tpmarg isprops |
| 733 | if lt n 0 |
| 734 | x = release int sf false |
| 735 | closefd int fd |
| 736 | c = refuse XINVAL OINVAL |
| 737 | oarg 5 |
| 738 | ret rend c |
| 739 | end |
| 740 | ot OSTEP |
| 741 | ot SPREP |
| 742 | ot OARG |
| 743 | odec ulong n |
| 744 | oc 10 |
| 745 | if not inflight int sf |
| 746 | closefd int fd |
| 747 | x = release int sf false |
| 748 | ret stfail 7 |
| 749 | end |
| 750 | tmo = long mul ulong fdec CTMO 1000000 |
| 751 | t0 = monons |
| 752 | r = long 0 |
| 753 | if isprops |
| 754 | r = tpmprops int fd tmo |
| 755 | else |
| 756 | r = tpmrand int fd ulong n tmo |
| 757 | end |
| 758 | el = ulong sub monons t0 |
| 759 | ot OSTEP |
| 760 | ot SSUB |
| 761 | ot OELAP |
| 762 | odec el |
| 763 | oc 10 |
| 764 | ot OSTEP |
| 765 | ot SCOMP |
| 766 | ot ORC |
| 767 | osdec r |
| 768 | oc 10 |
| 769 | closefd int fd |
| 770 | ot OSTEP |
| 771 | ot SQUI |
| 772 | ot OELAP |
| 773 | odec el |
| 774 | ot OBUDGET |
| 775 | odec ulong tmo |
| 776 | oc 10 |
| 777 | rl = release int sf true |
| 778 | if ne rl XOK |
| 779 | ret stfail 7 |
| 780 | end |
| 781 | oreceipt 5 4 |
| 782 | if and eq r 0 le el ulong tmo |
| 783 | tpmout isprops ulong n |
| 784 | ot OELAP |
| 785 | odec el |
| 786 | ot OOUT |
| 787 | ot OPASS |
| 788 | ret rend XOK |
| 789 | end |
| 790 | oc 10 |
| 791 | if or eq r TPMETIME gt el ulong tmo |
| 792 | c = refuse XTIMEOUT OTIME |
| 793 | odec el |
| 794 | ot OBUDGET |
| 795 | odec ulong tmo |
| 796 | ret rend c |
| 797 | end |
| 798 | c = refuse XDEVERR ODEV |
| 799 | osdec r |
| 800 | ret rend c |
| 801 | end |
| 802 | |
| 803 | fn cmdfault to int |
| 804 | c = check |
| 805 | if ne c XOK |
| 806 | ret c |
| 807 | end |
| 808 | g = gate 3 0 |
| 809 | if ne g XOK |
| 810 | ret g |
| 811 | end |
| 812 | okk = or argeq 4 TPMBSZ argeq 4 TPMBADCC |
| 813 | if not and okk feq CRUNNER MRTPM |
| 814 | c = refuse XUNSUP OBADK |
| 815 | oarg 4 |
| 816 | ret rend c |
| 817 | end |
| 818 | fd = tpmdisc |
| 819 | if lt fd 0 |
| 820 | ret int sub 0 fd |
| 821 | end |
| 822 | sf = claim 6 5 |
| 823 | if lt sf 0 |
| 824 | cl = fclose int fd |
| 825 | ret int sub 0 sf |
| 826 | end |
| 827 | if not inflight int sf |
| 828 | cl = fclose int fd |
| 829 | x = release int sf false |
| 830 | ret stfail 5 |
| 831 | end |
| 832 | tmo = long mul ulong fdec CTMO 1000000 |
| 833 | t0 = monons |
| 834 | r = tpmfault int fd 4 tmo |
| 835 | el = ulong sub monons t0 |
| 836 | cl = fclose int fd |
| 837 | rl = release int sf true |
| 838 | if ne rl XOK |
| 839 | ret stfail 5 |
| 840 | end |
| 841 | if eq r TPMETIME |
| 842 | c = refuse XTIMEOUT OTIME |
| 843 | odec el |
| 844 | ot OBUDGET |
| 845 | odec ulong tmo |
| 846 | ret rend c |
| 847 | end |
| 848 | want = long 0xb0143 |
| 849 | if argeq 4 TPMBSZ |
| 850 | want = long -22 |
| 851 | end |
| 852 | if ne r want |
| 853 | c = refuse XDEVERR OFRES |
| 854 | oarg 4 |
| 855 | ot ORES |
| 856 | osdec r |
| 857 | ot OWANT |
| 858 | osdec want |
| 859 | ret rend c |
| 860 | end |
| 861 | ot OFAULT |
| 862 | ofield CID |
| 863 | ot OKIND |
| 864 | oarg 4 |
| 865 | if lt r 0 |
| 866 | ot OLAYK |
| 867 | osdec r |
| 868 | else |
| 869 | ot OLAYT |
| 870 | ohx ulong r 8 |
| 871 | end |
| 872 | ot OOUT |
| 873 | ot OPASS |
| 874 | ret rend XOK |
| 875 | end |
| 876 | |
| 877 | fn cmdrun to int |
| 878 | c = check |
| 879 | if ne c XOK |
| 880 | ret c |
| 881 | end |
| 882 | g = gate 3 4 |
| 883 | if ne g XOK |
| 884 | ret g |
| 885 | end |
| 886 | okop = argin 4 TOPTPM |
| 887 | if feq CRUNNER MRCPU |
| 888 | okop = argin 4 TOPCPU |
| 889 | end |
| 890 | if not okop |
| 891 | c = refuse XUNSUP OUNSUP |
| 892 | ofield CID |
| 893 | ot OOP |
| 894 | oarg 4 |
| 895 | ret rend c |
| 896 | end |
| 897 | if feq CRUNNER MRCPU |
| 898 | ret runcpu |
| 899 | end |
| 900 | ret runtpm |
| 901 | end |
| 902 | |
| 903 | fn main to int |
| 904 | loadargs |
| 905 | n = argc |
| 906 | if lt n 3 |
| 907 | fail XUSAGE MUSE |
| 908 | end |
| 909 | readman 2 |
| 910 | if and argeq 1 MCHECK eq n 3 |
| 911 | ret cmdcheck |
| 912 | end |
| 913 | if and argeq 1 MDOCTOR eq n 3 |
| 914 | ret cmddoctor |
| 915 | end |
| 916 | if and argeq 1 MADMIT or eq n 4 eq n 5 |
| 917 | ret cmdadmit |
| 918 | end |
| 919 | if and argeq 1 MRUN or eq n 8 eq n 9 |
| 920 | ret cmdrun |
| 921 | end |
| 922 | if and argeq 1 MFAULT eq n 7 |
| 923 | ret cmdfault |
| 924 | end |
| 925 | fail XUSAGE MUSE |
| 926 | ret XUSAGE |
| 927 | end |