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 / seed / a64efi / efirt.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note efirt.om: the efi word of `target a64efi`, written in Omega (docs/A64-EFI.md). The compiler turns |
| 3 | note `efi OP ARG A OFF N` into a call of efirt OP ARG address N after its bounds check; everything the |
| 4 | note firmware does for it is here, through the fw words (fwcall, fwld, fwst, fwsys, fwadr). Place this |
| 5 | note unit before the program's own functions. The contract is x86efi's (docs/X86-EFI.md): results are |
| 6 | note ulong, an error is 2^63 | code. Operations here: 1 key, 2 stall, 10 milliseconds, 12 general file, |
| 7 | note 13 console, 14 identity. The others (0 info, 3 Blt, 4 time, 5 entropy, 6..9 network and pointer, |
| 8 | note 11 boot file) are 2^63 | 3 (EFI_UNSUPPORTED) on this target until someone needs them. |
| 9 | note UEFI 2.10 offsets: SystemTable 24 FirmwareVendor, 32 FirmwareRevision, 48 ConIn, 64 ConOut, |
| 10 | note 96 BootServices, 104 NumberOfTableEntries, 112 ConfigurationTable. BootServices 72 FreePool, |
| 11 | note 152 HandleProtocol, 248 Stall, 256 SetWatchdogTimer, 312 LocateHandleBuffer. File protocol 8 Open, |
| 12 | note 16 Close, 24 Delete, 32 Read, 40 Write, 56 SetPosition, 64 GetInfo, 80 Flush. |
| 13 | |
| 14 | note efh: 0 file handle, 8 volume root, 16 writable, 24 and 32 out-parameters, 40 started, 48 tick base |
| 15 | global efh uchar 64 |
| 16 | note efu: UCS-2 text for the firmware (a path of 255 characters, or a console chunk) |
| 17 | global efu uchar 1024 |
| 18 | global efg uchar 16 |
| 19 | global efinf uchar 512 |
| 20 | |
| 21 | fn eferr c ulong to ulong |
| 22 | ret or 9223372036854775808 c |
| 23 | end |
| 24 | |
| 25 | fn efbs to ulong |
| 26 | ret fwld add fwsys 1 96 8 |
| 27 | end |
| 28 | |
| 29 | note efhg / efhp: the 8-byte cell at byte k of efh |
| 30 | fn efhg k ulong to ulong |
| 31 | ret fwld fwadr efh k 8 8 |
| 32 | end |
| 33 | |
| 34 | fn efhp k ulong v ulong to void |
| 35 | x = fwst fwadr efh k 8 8 v |
| 36 | end |
| 37 | |
| 38 | note efguid: the GUID whose two little-endian halves are a and b, in efg; its address |
| 39 | fn efguid a ulong b ulong to ulong |
| 40 | p = fwadr efg 0 16 |
| 41 | x = fwst p 8 a |
| 42 | x = fwst add p 8 8 b |
| 43 | ret p |
| 44 | end |
| 45 | |
| 46 | note efstart: once, before the first operation: no watchdog reset during a long job (UEFI 7.5 |
| 47 | note SetWatchdogTimer 0), and the counter value that efi 10 counts from |
| 48 | fn efstart to void |
| 49 | if ne efhg 40 0 |
| 50 | ret |
| 51 | end |
| 52 | efhp 40 1 |
| 53 | efhp 48 fwsys 2 |
| 54 | x = fwcall fwld add efbs 256 8 0 0 0 0 0 0 |
| 55 | end |
| 56 | |
| 57 | note efi 13: A[0, n) to ConOut. Bytes 9, 10, 13 and 32..126 only (42), all checked first; LF becomes |
| 58 | note CR LF; chunks of at most 250 characters. No ConOut, or OutputString fails: 43. Result n. |
| 59 | fn efcon addr ulong n ulong to ulong |
| 60 | con = fwld add fwsys 1 64 8 |
| 61 | if eq con 0 |
| 62 | ret eferr 43 |
| 63 | end |
| 64 | i = ulong 0 |
| 65 | while lt i n |
| 66 | b = fwld add addr i 1 |
| 67 | ok = or or eq b 9 eq b 10 or eq b 13 and ge b 32 le b 126 |
| 68 | if not ok |
| 69 | ret eferr 42 |
| 70 | end |
| 71 | i = add i 1 |
| 72 | end |
| 73 | ostr = fwld add con 8 8 |
| 74 | u = fwadr efu 0 1024 |
| 75 | k = ulong 0 |
| 76 | i = 0 |
| 77 | while lt i n |
| 78 | b = fwld add addr i 1 |
| 79 | if eq b 10 |
| 80 | x = fwst add u mul k 2 2 13 |
| 81 | k = add k 1 |
| 82 | end |
| 83 | x = fwst add u mul k 2 2 b |
| 84 | k = add k 1 |
| 85 | i = add i 1 |
| 86 | if or ge k 250 eq i n |
| 87 | x = fwst add u mul k 2 2 0 |
| 88 | r = fwcall ostr con u 0 0 0 0 |
| 89 | if ne shr r 63 0 |
| 90 | ret eferr 43 |
| 91 | end |
| 92 | k = 0 |
| 93 | end |
| 94 | end |
| 95 | ret n |
| 96 | end |
| 97 | |
| 98 | note efclose: flush when writable, close the file and its root, empty the slot; 0 or 40 |
| 99 | fn efclose to ulong |
| 100 | f = efhg 0 |
| 101 | r = ulong 0 |
| 102 | if ne f 0 |
| 103 | if ne efhg 16 0 |
| 104 | s = fwcall fwld add f 80 8 f 0 0 0 0 0 |
| 105 | if ne shr s 63 0 |
| 106 | r = eferr 40 |
| 107 | end |
| 108 | end |
| 109 | x = fwcall fwld add f 16 8 f 0 0 0 0 0 |
| 110 | end |
| 111 | t = efhg 8 |
| 112 | if ne t 0 |
| 113 | x = fwcall fwld add t 16 8 t 0 0 0 0 0 |
| 114 | end |
| 115 | efhp 0 0 |
| 116 | efhp 8 0 |
| 117 | efhp 16 0 |
| 118 | ret r |
| 119 | end |
| 120 | |
| 121 | note efroot: open the root of volume v (255: the volume this image came from) into efh 8; 0 or an error |
| 122 | note 16 no handles, 18 no LoadedImage, 19 no such volume, 21 no file system on it, 22 OpenVolume failed |
| 123 | fn efroot v ulong to ulong |
| 124 | bs = efbs |
| 125 | o = fwadr efh 24 8 |
| 126 | dev = ulong 0 |
| 127 | if eq v 255 |
| 128 | g = efguid 1284253093421855137 4283602510276476814 |
| 129 | hp = fwld add bs 152 8 |
| 130 | ih = fwsys 0 |
| 131 | s = fwcall hp ih g o 0 0 0 |
| 132 | if ne s 0 |
| 133 | ret eferr 18 |
| 134 | end |
| 135 | dev = fwld add efhg 24 24 8 |
| 136 | else |
| 137 | g = efguid 1284199179690597154 4283602510276475278 |
| 138 | c = fwadr efh 32 8 |
| 139 | s = fwcall fwld add bs 312 8 2 g 0 c o 0 |
| 140 | if ne s 0 |
| 141 | ret eferr 16 |
| 142 | end |
| 143 | buf = efhg 24 |
| 144 | if ge v efhg 32 |
| 145 | x = fwcall fwld add bs 72 8 buf 0 0 0 0 0 |
| 146 | ret eferr 19 |
| 147 | end |
| 148 | dev = fwld add buf mul v 8 8 |
| 149 | x = fwcall fwld add bs 72 8 buf 0 0 0 0 0 |
| 150 | end |
| 151 | g = efguid 1284199179690597154 4283602510276475278 |
| 152 | s = fwcall fwld add bs 152 8 dev g o 0 0 0 |
| 153 | if ne s 0 |
| 154 | ret eferr 21 |
| 155 | end |
| 156 | fs = efhg 24 |
| 157 | ov = fwld add fs 8 8 |
| 158 | ro = fwadr efh 8 8 |
| 159 | s = fwcall ov fs ro 0 0 0 0 |
| 160 | if ne s 0 |
| 161 | efhp 8 0 |
| 162 | ret eferr 22 |
| 163 | end |
| 164 | ret 0 |
| 165 | end |
| 166 | |
| 167 | note efopen: Open(root, path in efu, mode, 0) into efh 0; the firmware status |
| 168 | fn efopen mode ulong to ulong |
| 169 | t = efhg 8 |
| 170 | op = fwld add t 8 8 |
| 171 | fh = fwadr efh 0 8 |
| 172 | nm = fwadr efu 0 1024 |
| 173 | ret fwcall op t fh nm mode 0 0 |
| 174 | end |
| 175 | |
| 176 | note efpath: the path A[0, n) checked (1..255 bytes, first '\', each 32..126) and copied to efu as UCS-2 |
| 177 | fn efpath addr ulong n ulong to ulong |
| 178 | if or eq n 0 gt n 255 |
| 179 | ret eferr 33 |
| 180 | end |
| 181 | if ne fwld addr 1 92 |
| 182 | ret eferr 33 |
| 183 | end |
| 184 | u = fwadr efu 0 1024 |
| 185 | i = ulong 0 |
| 186 | while lt i n |
| 187 | b = fwld add addr i 1 |
| 188 | if or lt b 32 gt b 126 |
| 189 | ret eferr 33 |
| 190 | end |
| 191 | x = fwst add u mul i 2 2 b |
| 192 | i = add i 1 |
| 193 | end |
| 194 | x = fwst add u mul n 2 2 0 |
| 195 | ret 0 |
| 196 | end |
| 197 | |
| 198 | note efsize: FileSize of the open file (GetInfo with the FileInfo GUID), or 41 after closing it |
| 199 | fn efsize to ulong |
| 200 | f = efhg 0 |
| 201 | g = efguid 1284208961261104786 4283602510276475278 |
| 202 | efhp 24 512 |
| 203 | sz = fwadr efh 24 8 |
| 204 | inf = fwadr efinf 0 512 |
| 205 | s = fwcall fwld add f 64 8 f g sz inf 0 0 |
| 206 | if ne s 0 |
| 207 | x = efclose |
| 208 | ret eferr 41 |
| 209 | end |
| 210 | ret fwld add inf 8 8 |
| 211 | end |
| 212 | |
| 213 | note efopn: subs 0, 1 and 5 on volume vol, the path already in efu |
| 214 | fn efopn sb ulong vol ulong to ulong |
| 215 | if ne efhg 0 0 |
| 216 | ret eferr 34 |
| 217 | end |
| 218 | e = efroot vol |
| 219 | if ne e 0 |
| 220 | ret e |
| 221 | end |
| 222 | if eq sb 1 |
| 223 | s = efopen 3 |
| 224 | if eq s 0 |
| 225 | f = efhg 0 |
| 226 | x = fwcall fwld add f 24 8 f 0 0 0 0 0 |
| 227 | efhp 0 0 |
| 228 | end |
| 229 | s = efopen 9223372036854775811 |
| 230 | if ne s 0 |
| 231 | efhp 0 0 |
| 232 | x = efclose |
| 233 | ret eferr 36 |
| 234 | end |
| 235 | efhp 16 1 |
| 236 | ret 0 |
| 237 | end |
| 238 | mode = ulong 1 |
| 239 | if eq sb 5 |
| 240 | mode = 3 |
| 241 | end |
| 242 | s = efopen mode |
| 243 | if ne s 0 |
| 244 | efhp 0 0 |
| 245 | x = efclose |
| 246 | ret eferr 36 |
| 247 | end |
| 248 | r = efsize |
| 249 | if and eq sb 5 eq shr r 63 0 |
| 250 | efhp 16 1 |
| 251 | end |
| 252 | ret r |
| 253 | end |
| 254 | |
| 255 | note efrw: sub 2 (read) or 3 (write) of n bytes at position pos |
| 256 | fn efrw sb ulong pos ulong addr ulong n ulong to ulong |
| 257 | f = efhg 0 |
| 258 | bad = ulong 37 |
| 259 | if eq sb 3 |
| 260 | bad = 38 |
| 261 | if eq efhg 16 0 |
| 262 | ret eferr 38 |
| 263 | end |
| 264 | end |
| 265 | s = fwcall fwld add f 56 8 f pos 0 0 0 0 |
| 266 | if ne s 0 |
| 267 | ret eferr bad |
| 268 | end |
| 269 | efhp 24 n |
| 270 | sz = fwadr efh 24 8 |
| 271 | m = ulong 32 |
| 272 | if eq sb 3 |
| 273 | m = 40 |
| 274 | end |
| 275 | s = fwcall fwld add f m 8 f sz addr 0 0 0 |
| 276 | if ne s 0 |
| 277 | ret eferr bad |
| 278 | end |
| 279 | if eq sb 2 |
| 280 | ret efhg 24 |
| 281 | end |
| 282 | if ne efhg 24 n |
| 283 | ret eferr 39 |
| 284 | end |
| 285 | ret n |
| 286 | end |
| 287 | |
| 288 | note efi 12, see docs/X86-EFI.md "efi 12 general file" for the contract kept here |
| 289 | fn effile arg ulong addr ulong n ulong to ulong |
| 290 | sb = and arg 255 |
| 291 | if gt sb 5 |
| 292 | ret eferr 32 |
| 293 | end |
| 294 | if or or eq sb 0 eq sb 1 eq sb 5 |
| 295 | e = efpath addr n |
| 296 | if ne e 0 |
| 297 | ret e |
| 298 | end |
| 299 | ret efopn sb and shr arg 8 255 |
| 300 | end |
| 301 | if eq efhg 0 0 |
| 302 | ret eferr 35 |
| 303 | end |
| 304 | if eq sb 4 |
| 305 | ret efclose |
| 306 | end |
| 307 | ret efrw sb shr arg 16 addr n |
| 308 | end |
| 309 | |
| 310 | note efcfg: the address stored for configuration table GUID (a, b), 0 when it is not there |
| 311 | fn efcfg a ulong b ulong to ulong |
| 312 | st = fwsys 1 |
| 313 | cnt = fwld add st 104 8 |
| 314 | tab = fwld add st 112 8 |
| 315 | i = ulong 0 |
| 316 | while lt i cnt |
| 317 | e = add tab mul i 24 |
| 318 | if and eq fwld e 8 a eq fwld add e 8 8 b |
| 319 | ret fwld add e 16 8 |
| 320 | end |
| 321 | i = add i 1 |
| 322 | end |
| 323 | ret 0 |
| 324 | end |
| 325 | |
| 326 | note efi 14 identity. ARG with bits 0..15 zero: this target has no CPUID; leaf 0 gives MIDR_EL1 in the |
| 327 | note first 8 of 16 bytes and zeros after it, every other leaf 16 zeros (N < 16 is 5, a leaf at or above |
| 328 | note 2^32 is 32; result 16). ARG 1 FirmwareVendor, ARG 2 FirmwareRevision, sub 3 the SMBIOS structure |
| 329 | note table from byte ARG >> 16, as on x86efi (44 when no table is found). Any other ARG is 32. |
| 330 | fn efident arg ulong addr ulong n ulong to ulong |
| 331 | st = fwsys 1 |
| 332 | sb = and arg 65535 |
| 333 | par = shr arg 16 |
| 334 | if eq sb 0 |
| 335 | if ne shr par 32 0 |
| 336 | ret eferr 32 |
| 337 | end |
| 338 | if lt n 16 |
| 339 | ret eferr 5 |
| 340 | end |
| 341 | v = ulong 0 |
| 342 | if eq par 0 |
| 343 | v = fwsys 4 |
| 344 | end |
| 345 | x = fwst addr 8 v |
| 346 | x = fwst add addr 8 8 0 |
| 347 | ret 16 |
| 348 | end |
| 349 | if eq arg 1 |
| 350 | p = fwld add st 24 8 |
| 351 | if eq p 0 |
| 352 | ret 0 |
| 353 | end |
| 354 | k = ulong 0 |
| 355 | go = true |
| 356 | while and go le add k 2 n |
| 357 | c = fwld add p k 2 |
| 358 | if eq c 0 |
| 359 | go = false |
| 360 | else |
| 361 | x = fwst add addr k 2 c |
| 362 | k = add k 2 |
| 363 | end |
| 364 | end |
| 365 | ret k |
| 366 | end |
| 367 | if eq arg 2 |
| 368 | ret fwld add st 32 4 |
| 369 | end |
| 370 | if eq sb 3 |
| 371 | base = ulong 0 |
| 372 | size = ulong 0 |
| 373 | ep = efcfg 5344813523769562436 10728454813909331609 |
| 374 | if ne ep 0 |
| 375 | if eq fwld ep 4 860705631 |
| 376 | size = fwld add ep 12 4 |
| 377 | base = fwld add ep 16 8 |
| 378 | end |
| 379 | else |
| 380 | ep = efcfg 1284420384822471985 5602828850577807002 |
| 381 | if ne ep 0 |
| 382 | if eq fwld ep 4 1598903135 |
| 383 | size = fwld add ep 22 2 |
| 384 | base = fwld add ep 24 4 |
| 385 | end |
| 386 | end |
| 387 | end |
| 388 | if eq base 0 |
| 389 | ret eferr 44 |
| 390 | end |
| 391 | if ge par size |
| 392 | ret 0 |
| 393 | end |
| 394 | m = sub size par |
| 395 | if gt m n |
| 396 | m = n |
| 397 | end |
| 398 | i = ulong 0 |
| 399 | while lt i m |
| 400 | x = fwst add addr i 1 fwld add add base par i 1 |
| 401 | i = add i 1 |
| 402 | end |
| 403 | ret m |
| 404 | end |
| 405 | ret eferr 32 |
| 406 | end |
| 407 | |
| 408 | fn efirt op ulong arg ulong addr ulong n ulong to ulong |
| 409 | efstart |
| 410 | if eq op 13 |
| 411 | if ne arg 0 |
| 412 | ret eferr 32 |
| 413 | end |
| 414 | ret efcon addr n |
| 415 | end |
| 416 | if eq op 12 |
| 417 | ret effile arg addr n |
| 418 | end |
| 419 | if eq op 10 |
| 420 | if lt n 8 |
| 421 | ret eferr 5 |
| 422 | end |
| 423 | f = fwsys 3 |
| 424 | if eq f 0 |
| 425 | ret eferr 3 |
| 426 | end |
| 427 | t = sub fwsys 2 efhg 48 |
| 428 | x = fwst addr 8 add mul div t f 1000 div mul rem t f 1000 f |
| 429 | ret 0 |
| 430 | end |
| 431 | if eq op 14 |
| 432 | ret efident arg addr n |
| 433 | end |
| 434 | if eq op 2 |
| 435 | ret fwcall fwld add efbs 248 8 arg 0 0 0 0 0 |
| 436 | end |
| 437 | if eq op 1 |
| 438 | if lt n 4 |
| 439 | ret eferr 5 |
| 440 | end |
| 441 | ci = fwld add fwsys 1 48 8 |
| 442 | if eq ci 0 |
| 443 | ret eferr 3 |
| 444 | end |
| 445 | ret fwcall fwld add ci 8 8 ci addr 0 0 0 0 |
| 446 | end |
| 447 | ret eferr 3 |
| 448 | end |
| 449 | |
| 450 | note efiexit: called by the shared return on every exit path (return, exit, trap): a file still open |
| 451 | note is flushed and closed, as a process exit closes descriptors. Then, when the boot volume holds a |
| 452 | note file \OLLM\REBOOT.TXT, the machine is restarted (Runtime Services ResetSystem, SystemTable 88, |
| 453 | note offset 104, EfiResetCold): a firmware that shows its menu after an application returns would |
| 454 | note otherwise leave an unattended machine there. Without that file the image returns to the firmware. |
| 455 | text efrbt "\\OLLM\\REBOOT.TXT" |
| 456 | fn efiexit to void |
| 457 | x = efclose |
| 458 | u = fwadr efu 0 1024 |
| 459 | n = len efrbt |
| 460 | i = ulong 0 |
| 461 | while lt i n |
| 462 | x = fwst add u mul i 2 2 ulong get efrbt i |
| 463 | i = add i 1 |
| 464 | end |
| 465 | x = fwst add u mul n 2 2 0 |
| 466 | if ne efroot 255 0 |
| 467 | ret |
| 468 | end |
| 469 | s = efopen 1 |
| 470 | x = efclose |
| 471 | if eq s 0 |
| 472 | rs = fwld add fwsys 1 88 8 |
| 473 | x = fwcall fwld add rs 104 8 0 0 0 0 0 0 |
| 474 | end |
| 475 | end |