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 / seed / a64efi / efirt.om

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