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 / omega / allen / efi-main.om
| 1 | note SPDX-License-Identifier: MIT |
| 2 | note Firmware-hosted native Allen: UEFI Boot Services stay active. Display via GOP Blt, |
| 3 | note keys via Simple Text Input, packets via Simple Network, time via GetTime, entropy via |
| 4 | note EFI_RNG or CPU RDRAND, elapsed time via calibrated invariant TSC. Compile with the |
| 5 | note leading line `target x86efi`. All application, network, TLS and UI logic is Omega. |
| 6 | global Eb uchar 64 |
| 7 | global Ek uchar 8 |
| 8 | global Ep uchar 32 |
| 9 | global Et uchar 16 |
| 10 | global Eclk uchar 8 |
| 11 | global Erx uchar 1608 |
| 12 | global Etx uchar 2048 |
| 13 | note Es (screen and network state) is declared in platform.om. |
| 14 | fn efok st ulong to bool |
| 15 | ret eq st 0 |
| 16 | end |
| 17 | fn efms to ulong |
| 18 | st = efi 10 0 Eclk 0 8 |
| 19 | if not efok st |
| 20 | put Es 12 10 |
| 21 | ret 18446744073709551615 |
| 22 | end |
| 23 | v = ulong 0 |
| 24 | for i ulong 8 |
| 25 | v = or v shl ulong get Eclk i mul i 8 |
| 26 | end |
| 27 | ret v |
| 28 | end |
| 29 | fn efdays y ulong m ulong d ulong to ulong |
| 30 | note Days since 1970-01-01, proleptic Gregorian (civil_from_days inverse). |
| 31 | yy = y |
| 32 | if le m 2 |
| 33 | yy = sub yy 1 |
| 34 | end |
| 35 | era = div yy 400 |
| 36 | yoe = sub yy mul era 400 |
| 37 | mp = add m 9 |
| 38 | if gt m 2 |
| 39 | mp = sub m 3 |
| 40 | end |
| 41 | doy = mul 153 mp |
| 42 | doy = div add doy 2 5 |
| 43 | doy = add doy sub d 1 |
| 44 | doe = mul yoe 365 |
| 45 | doe = add doe div yoe 4 |
| 46 | doe = sub doe div yoe 100 |
| 47 | doe = add doe doy |
| 48 | days = mul era 146097 |
| 49 | days = add days doe |
| 50 | ret sub days 719468 |
| 51 | end |
| 52 | fn efepoch to ulong |
| 53 | st = efi 4 0 Et 0 16 |
| 54 | if not efok st |
| 55 | ret 0 |
| 56 | end |
| 57 | y = or ulong get Et 0 shl ulong get Et 1 8 |
| 58 | mo = ulong get Et 2 |
| 59 | d = ulong get Et 3 |
| 60 | h = ulong get Et 4 |
| 61 | mi = ulong get Et 5 |
| 62 | s = ulong get Et 6 |
| 63 | if or or lt y 2024 gt y 2099 or lt mo 1 gt mo 12 |
| 64 | ret 0 |
| 65 | end |
| 66 | if or or lt d 1 gt d 31 or gt h 23 gt mi 59 |
| 67 | ret 0 |
| 68 | end |
| 69 | if gt s 59 |
| 70 | ret 0 |
| 71 | end |
| 72 | v = mul efdays y mo d 86400 |
| 73 | v = add v add mul h 3600 add mul mi 60 s |
| 74 | tz = or ulong get Et 12 shl ulong get Et 13 8 |
| 75 | if ne tz 2047 |
| 76 | note UEFI 2.10 8.3: Localtime = UTC - TimeZone (signed 16-bit minutes), so UTC = local + TimeZone. |
| 77 | note EFI_UNSPECIFIED_TIMEZONE (2047): firmware time is taken as UTC; shown on screen for the operator. |
| 78 | if ge tz 32768 |
| 79 | v = sub v mul sub 65536 tz 60 |
| 80 | else |
| 81 | v = add v mul tz 60 |
| 82 | end |
| 83 | end |
| 84 | ret v |
| 85 | end |
| 86 | fn efentropy to bool |
| 87 | st = efi 5 0 Gsrand 0 96 |
| 88 | if not efok st |
| 89 | put Es 12 5 |
| 90 | note No firmware random source (neither EFI_RNG nor RDRAND): name it on the repository |
| 91 | note screen as stage 15 with the firmware status byte, not as a bare transport error. |
| 92 | put Gdiag 0 15 |
| 93 | put Gdiag 1 and st 255 |
| 94 | ret false |
| 95 | end |
| 96 | put Gsrandn 0 96 |
| 97 | ret true |
| 98 | end |
| 99 | fn efrows y0 ulong rows ulong to bool |
| 100 | note Rows of Efa (as zeros) to the screen: used before the show owns the frame. |
| 101 | w = get Es 0 |
| 102 | n = mul mul w rows 4 |
| 103 | note Computed-count loops trap past 1,000,000 passes: clear row by row. |
| 104 | for r ulong rows |
| 105 | base = mul mul r w 4 |
| 106 | for x ulong mul w 4 |
| 107 | put Efa add base x 0 |
| 108 | end |
| 109 | end |
| 110 | arg = or or w shl rows 32 shl y0 48 |
| 111 | st = efi 3 arg Efa 0 n |
| 112 | ret efok st |
| 113 | end |
| 114 | fn efscreen to bool |
| 115 | st = efi 0 0 Eb 0 64 |
| 116 | if not efok st |
| 117 | ret false |
| 118 | end |
| 119 | w = or ulong get Eb 8 shl ulong get Eb 9 8 |
| 120 | w = or w shl ulong get Eb 10 16 |
| 121 | h = or ulong get Eb 12 shl ulong get Eb 13 8 |
| 122 | h = or h shl ulong get Eb 14 16 |
| 123 | if not shfits w h |
| 124 | ret false |
| 125 | end |
| 126 | put Es 0 w |
| 127 | put Es 1 h |
| 128 | put Es 2 1 |
| 129 | put Es 3 0 |
| 130 | put Es 4 0 |
| 131 | band = div len Efa mul w 4 |
| 132 | y = ulong 0 |
| 133 | while lt y h bound 64 |
| 134 | n = band |
| 135 | if gt add y n h |
| 136 | n = sub h y |
| 137 | end |
| 138 | if not efrows y n |
| 139 | ret false |
| 140 | end |
| 141 | y = add y n |
| 142 | end |
| 143 | put Lg 0 long w |
| 144 | put Lg 1 long h |
| 145 | ret cvinit 768 384 |
| 146 | end |
| 147 | |
| 148 | note Little-endian u64 from Eb at..at+n-1 (n at most 8). |
| 149 | fn efle at ulong n ulong to ulong |
| 150 | v = ulong 0 |
| 151 | for i ulong n |
| 152 | v = or v shl ulong get Eb add at i mul i 8 |
| 153 | end |
| 154 | ret v |
| 155 | end |
| 156 | fn efnetup now ulong to void |
| 157 | note Es 14 and 15 keep efi 8's diagnostics, read through efi 0 after every attempt (handles | |
| 158 | note locate status << 8 | scans << 16, and one probe step per handle); `stats` shows them. |
| 159 | note An adapter refused only for its link stays initialized, so the next attempt sees the link. |
| 160 | st = efi 8 0 Eb 0 0 |
| 161 | ist = efi 0 0 Eb 0 64 |
| 162 | if efok ist |
| 163 | put Es 14 efle 52 4 |
| 164 | put Es 15 efle 56 8 |
| 165 | end |
| 166 | if not efok st |
| 167 | put Es 12 and st 255 |
| 168 | ret |
| 169 | end |
| 170 | if or not efok ist eq and ulong get Eb 4 2 0 |
| 171 | put Es 12 and ist 255 |
| 172 | ret |
| 173 | end |
| 174 | for i ulong 6 |
| 175 | put Nmac i get Eb add 24 i |
| 176 | end |
| 177 | netinit now |
| 178 | put Es 6 1 |
| 179 | end |
| 180 | fn efpackets now ulong to void |
| 181 | if eq get Es 6 0 |
| 182 | ret |
| 183 | end |
| 184 | for burst ulong 32 |
| 185 | st = efi 6 0 Erx 0 len Erx |
| 186 | if not efok st |
| 187 | ret |
| 188 | end |
| 189 | n = ulong 0 |
| 190 | for i ulong 8 |
| 191 | n = or n shl ulong get Erx i mul i 8 |
| 192 | end |
| 193 | if and ge n 14 le n 1514 |
| 194 | for i ulong n |
| 195 | put Nrx i get Erx add 8 i |
| 196 | end |
| 197 | netinput n now |
| 198 | end |
| 199 | end |
| 200 | end |
| 201 | fn eftransmit now ulong to void |
| 202 | if or eq get Es 6 0 eq get Ntxlen 0 0 |
| 203 | ret |
| 204 | end |
| 205 | n = get Ntxlen 0 |
| 206 | for i ulong n |
| 207 | put Etx i get Ntx i |
| 208 | end |
| 209 | st = efi 7 0 Etx 0 n |
| 210 | note EFI_NOT_READY: the previous private copy is still owned by firmware; retry later. |
| 211 | if eq st 9223372036854775814 |
| 212 | ret |
| 213 | end |
| 214 | netsent efok st now |
| 215 | end |
| 216 | fn efkeymap scan ulong ch ulong to ulong |
| 217 | if ne ch 0 |
| 218 | ret ch |
| 219 | end |
| 220 | if eq scan 1 |
| 221 | ret 256 |
| 222 | end |
| 223 | if eq scan 2 |
| 224 | ret 257 |
| 225 | end |
| 226 | if eq scan 3 |
| 227 | ret 261 |
| 228 | end |
| 229 | if eq scan 4 |
| 230 | ret 260 |
| 231 | end |
| 232 | if eq scan 9 |
| 233 | ret 258 |
| 234 | end |
| 235 | if eq scan 10 |
| 236 | ret 259 |
| 237 | end |
| 238 | if eq scan 23 |
| 239 | ret 27 |
| 240 | end |
| 241 | ret 0 |
| 242 | end |
| 243 | fn efkeys to bool |
| 244 | changed = false |
| 245 | for burst ulong 16 |
| 246 | st = efi 1 0 Ek 0 4 |
| 247 | if not efok st |
| 248 | ret changed |
| 249 | end |
| 250 | scan = or ulong get Ek 0 shl ulong get Ek 1 8 |
| 251 | ch = or ulong get Ek 2 shl ulong get Ek 3 8 |
| 252 | key = efkeymap scan ch |
| 253 | if ne key 0 |
| 254 | ok = shkey key |
| 255 | changed = true |
| 256 | end |
| 257 | end |
| 258 | ret changed |
| 259 | end |
| 260 | fn efpointer to bool |
| 261 | st = efi 9 0 Ep 0 32 |
| 262 | if not efok st |
| 263 | ret false |
| 264 | end |
| 265 | z = ulong 0 |
| 266 | y = ulong 0 |
| 267 | for i ulong 8 |
| 268 | z = or z shl ulong get Ep add 24 i mul i 8 |
| 269 | y = or y shl ulong get Ep add 16 i mul i 8 |
| 270 | end |
| 271 | if ne get Dmo 0 1 |
| 272 | ret false |
| 273 | end |
| 274 | note Wheel/scroll: signed rows. Drag with button held: accumulate vertical swipe. |
| 275 | if ne z 0 |
| 276 | if ge z 9223372036854775808 |
| 277 | ghscroll -3 |
| 278 | else |
| 279 | ghscroll 3 |
| 280 | end |
| 281 | ret true |
| 282 | end |
| 283 | if and ne and ulong get Ep 4 1 0 ne y 0 |
| 284 | if ge y 9223372036854775808 |
| 285 | ghswipe -1 |
| 286 | else |
| 287 | ghswipe 1 |
| 288 | end |
| 289 | ret true |
| 290 | end |
| 291 | ret false |
| 292 | end |
| 293 | fn efnet now ulong to void |
| 294 | if eq get Es 6 0 |
| 295 | if ne get Ghreq 0 0 |
| 296 | ok = ghreply get Ghreq 0 0 0 get Ghreq 2 |
| 297 | end |
| 298 | ret |
| 299 | end |
| 300 | if and eq get Gs 0 0 ne get Ghreq 0 0 |
| 301 | if not efentropy |
| 302 | ok = ghreply get Ghreq 0 0 0 get Ghreq 2 |
| 303 | ret |
| 304 | end |
| 305 | epoch = efepoch |
| 306 | if eq epoch 0 |
| 307 | put Es 12 4 |
| 308 | note No usable firmware clock for certificate validity: stage 16 on the repository screen. |
| 309 | put Gdiag 0 16 |
| 310 | ok = ghreply get Ghreq 0 0 0 get Ghreq 2 |
| 311 | ret |
| 312 | end |
| 313 | gsnext now epoch |
| 314 | ret |
| 315 | end |
| 316 | if ge get Gs 0 4 |
| 317 | put Gs 0 0 |
| 318 | ret |
| 319 | end |
| 320 | gspoll now |
| 321 | end |
| 322 | fn efsig to ulong |
| 323 | v = get Gh 0 |
| 324 | v = add v shl get Gh 1 8 |
| 325 | v = add v shl get Gs 0 40 |
| 326 | v = add v shl get Ghreq 0 48 |
| 327 | v = add v shl get Dmo 0 56 |
| 328 | ret v |
| 329 | end |
| 330 | fn efstep to int |
| 331 | now = efms |
| 332 | if eq now 18446744073709551615 |
| 333 | ret 4 |
| 334 | end |
| 335 | if eq get Es 6 0 |
| 336 | note Keep trying to bring the firmware network up; the link may settle after boot. |
| 337 | if gt now add get Es 13 2000 |
| 338 | put Es 13 now |
| 339 | efnetup now |
| 340 | end |
| 341 | end |
| 342 | efpackets now |
| 343 | changed = efkeys |
| 344 | if efpointer |
| 345 | put Sh 5 1 |
| 346 | end |
| 347 | before = efsig |
| 348 | efnet now |
| 349 | if ne before efsig |
| 350 | put Sh 5 1 |
| 351 | end |
| 352 | chstep now ne get Es 6 0 |
| 353 | shanswer |
| 354 | ststep ne get Es 6 0 |
| 355 | eftransmit now |
| 356 | if not shstep now |
| 357 | ret 6 |
| 358 | end |
| 359 | st = efi 2 1000 Eb 0 0 |
| 360 | ret 0 |
| 361 | end |
| 362 | fn efdrain to void |
| 363 | note Discard keys left in the firmware buffer by the shell before the image started. |
| 364 | note Four consecutive empty reads prove the queue is quiet; 4096 bounds the loop. |
| 365 | empty = ulong 0 |
| 366 | for i ulong 4096 |
| 367 | st = efi 1 0 Ek 0 4 |
| 368 | if not efok st |
| 369 | empty = add empty 1 |
| 370 | if ge empty 4 |
| 371 | break |
| 372 | end |
| 373 | else |
| 374 | empty = 0 |
| 375 | end |
| 376 | end |
| 377 | end |
| 378 | fn efloop to int |
| 379 | for oloop ulong 1000 |
| 380 | for tloop ulong 60000 |
| 381 | rc = efstep |
| 382 | if ne rc 0 |
| 383 | ret rc |
| 384 | end |
| 385 | end |
| 386 | end |
| 387 | ret 0 |
| 388 | end |
| 389 | fn efbpaint rh ulong gh ulong bh ulong w ulong n ulong to bool |
| 390 | for r ulong n |
| 391 | base = mul mul r w 4 |
| 392 | for x ulong w |
| 393 | at = add base mul x 4 |
| 394 | put Efa at uchar bh |
| 395 | put Efa add at 1 uchar gh |
| 396 | put Efa add at 2 uchar rh |
| 397 | put Efa add at 3 0 |
| 398 | end |
| 399 | end |
| 400 | arg = or w shl n 32 |
| 401 | st = efi 3 arg Efa 0 mul mul w n 4 |
| 402 | ret efok st |
| 403 | end |
| 404 | fn efbanner code ulong to int |
| 405 | note Paint a distinct colour for a failing stage, then hold it so a photograph is possible. |
| 406 | note No font or canvas dependency: this must work even when efscreen failed. |
| 407 | w = ulong 640 |
| 408 | h = ulong 480 |
| 409 | st = efi 0 0 Eb 0 64 |
| 410 | if efok st |
| 411 | tw = or ulong get Eb 8 shl ulong get Eb 9 8 |
| 412 | th = or ulong get Eb 12 shl ulong get Eb 13 8 |
| 413 | if and gt tw 0 le tw 1280 |
| 414 | w = tw |
| 415 | end |
| 416 | if and gt th 0 le th 800 |
| 417 | h = th |
| 418 | end |
| 419 | end |
| 420 | note Colour encodes the failing stage: red/green/blue derived from code. |
| 421 | rr = and mul code 37 255 |
| 422 | gg = and mul code 91 255 |
| 423 | bb = and mul code 149 255 |
| 424 | ok = efbpaint rr gg bb w h |
| 425 | for k ulong 12 |
| 426 | st = efi 2 500000 Eb 0 0 |
| 427 | end |
| 428 | ret int code |
| 429 | end |
| 430 | fn efpaint code ulong to void |
| 431 | note Paint the whole screen a colour derived from code and blit it. Used as a visible |
| 432 | note progress meter during startup, so a failure is identifiable from a photograph. |
| 433 | w = get Es 0 |
| 434 | h = get Es 1 |
| 435 | if or eq w 0 eq h 0 |
| 436 | w = ulong 640 |
| 437 | h = ulong 480 |
| 438 | end |
| 439 | if gt mul mul w h 4 len Efa |
| 440 | h = div len Efa mul w 4 |
| 441 | end |
| 442 | rr = and mul code 37 255 |
| 443 | gg = and mul code 91 255 |
| 444 | bb = and mul code 149 255 |
| 445 | ok = efbpaint rr gg bb w h |
| 446 | end |
| 447 | fn main to int |
| 448 | constants |
| 449 | if not efscreen |
| 450 | ret efbanner 1 |
| 451 | end |
| 452 | efpaint 1 |
| 453 | dminit |
| 454 | efdrain |
| 455 | efpaint 2 |
| 456 | now = efms |
| 457 | if eq now 18446744073709551615 |
| 458 | ret efbanner 2 |
| 459 | end |
| 460 | efpaint 3 |
| 461 | efnetup now |
| 462 | efpaint 4 |
| 463 | if not shinit now |
| 464 | ret efbanner 7 |
| 465 | end |
| 466 | if not efentropy |
| 467 | put Es 12 5 |
| 468 | end |
| 469 | run = or ulong get Gsrand 0 shl now 8 |
| 470 | if eq run 0 |
| 471 | run = 1 |
| 472 | end |
| 473 | ok = chinit now run |
| 474 | put Es 7 1 |
| 475 | rc = efloop |
| 476 | if ne rc 0 |
| 477 | bad = add ulong 10 ulong rc |
| 478 | ret efbanner bad |
| 479 | end |
| 480 | ret 0 |
| 481 | end |