interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / aienux / allen / efi-main.om

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