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 / boot / handoff.om

326 lines · 8344 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/boot/handoff.om: Pure-Omega CHANDOF3 Boot Handoff Contract & Validator.
3note Converted from upstream aienux native/boot/handoff.h and native/kernel/core/handoff_check.c.
4note Compose with: si/directives/om2.om aienux/boot/handoff.om aienux/boot/model_map.om aienux/boot/slot.om aienux/boot/netboot.om <test.om> om/tls/sha2.om
5
6note Shared boot subsystem structs (declared before first fn per OM-2 rule)
7struct SlotEval valid uint generation ulong policy_gen ulong err int end
8
9struct SlotDecision boot_slot int reason uint loader_write uint end
10
11struct NbMsg mtype uchar status uchar seconds ushort count ushort mlen ushort total uint offset uint end
12
13note CHANDOF3 magic constants (little-endian "CHANDOF3", "CHANDOF1", "CHANDOF2", stem "CHANDOF")
14const HO_MAGIC_V3 ulong 0x33464f444e414843
15const HO_MAGIC_V2 ulong 0x32464f444e414843
16const HO_MAGIC_V1 ulong 0x31464f444e414843
17const HO_MAGIC_STEM ulong 0x00464f444e414843
18const HO_STEM_MASK ulong 0x00ffffffffffffff
19const HO_BYTES ulong 232
20
21note Handoff field offsets (pinned to CHANDOF3 LP64 ABI)
22const HO_OFF_MAGIC ulong 0
23const HO_OFF_FW_EL ulong 8
24const HO_OFF_DESC_VER ulong 12
25const HO_OFF_FW_TTBR0 ulong 16
26const HO_OFF_FW_SCTLR ulong 24
27const HO_OFF_RSDP ulong 32
28const HO_OFF_MMAP ulong 40
29const HO_OFF_MAP_SIZE ulong 48
30const HO_OFF_DESC_SIZE ulong 56
31const HO_OFF_IMG_BASE ulong 64
32const HO_OFF_IMG_END ulong 72
33const HO_OFF_CNT_FREQ ulong 80
34const HO_OFF_ENTRY_TICKS ulong 88
35const HO_OFF_EXIT_ATTEMPTS ulong 96
36const HO_OFF_RESERVED0 ulong 100
37const HO_OFF_COMMIT ulong 104
38const HO_OFF_MODEL_BASE ulong 112
39const HO_OFF_MODEL_LEN ulong 120
40const HO_OFF_MODEL_SHA256 ulong 128
41const HO_OFF_MODEL_FLAGS ulong 160
42const HO_OFF_MODEL_EXTENTS ulong 164
43const HO_OFF_MODEL_READ_US ulong 168
44const HO_OFF_MODEL_LAST_BLK ulong 176
45const HO_OFF_MODEL_BLK_SZ ulong 184
46const HO_OFF_RESERVED1 ulong 188
47const HO_OFF_FB_BASE ulong 192
48const HO_OFF_FB_SIZE ulong 200
49const HO_OFF_FB_WIDTH ulong 208
50const HO_OFF_FB_HEIGHT ulong 212
51const HO_OFF_FB_PITCH ulong 216
52const HO_OFF_FB_FORMAT ulong 220
53const HO_OFF_FB_STATUS ulong 224
54const HO_OFF_FB_GOP_HANDLES ulong 228
55
56note Model flags
57const HO_MODEL_PRESENT uint 1
58const HO_MODEL_BLOCKIO uint 2
59
60note Framebuffer status and format constants
61const HO_FB_NONE uint 0
62const HO_FB_PRESENT uint 1
63const HO_FB_NO_LINEAR uint 2
64const HO_FB_RGBX uint 0
65const HO_FB_BGRX uint 1
66const HO_FB_MAX_DIM uint 16384
67
68note Memory map limits
69const HO_MAP_MAX ulong 65536
70const HO_DESC_MIN ulong 40
71const HO_DESC_VERSION uint 1
72
73note Handoff check return codes
74const HO_OK int 0
75const HO_ERR_SHORT int -1
76const HO_ERR_BAD_MAGIC int -2
77const HO_ERR_UNSUP_VER int -3
78const HO_ERR_RESERVED0 int -4
79const HO_ERR_RESERVED1 int -5
80const HO_ERR_MODEL_FLAGS int -6
81const HO_ERR_MODEL_RANGE int -7
82const HO_ERR_MODEL_ALIGN int -8
83const HO_ERR_MODEL_OVERFLOW int -9
84const HO_ERR_MODEL_BLKSZ int -10
85const HO_ERR_MODEL_EXTENTS int -11
86const HO_ERR_MODEL_SPURIOUS int -12
87const HO_ERR_FB_SPURIOUS int -13
88const HO_ERR_FB_STATUS int -14
89const HO_ERR_FB_FORMAT int -15
90const HO_ERR_FB_DIM int -16
91const HO_ERR_FB_BASE int -17
92const HO_ERR_FB_SIZE int -18
93const HO_ERR_FB_OVERFLOW int -19
94const HO_ERR_FB_GOP int -20
95const HO_ERR_DESC_SIZE int -21
96const HO_ERR_DESC_VER int -22
97const HO_ERR_MAP_SIZE int -23
98
99note Little-endian byte helpers shared by boot modules
100fn boot_rd16_le buf view uchar * off ulong to ushort
101b0 = ushort get buf off
102b1 = ushort get buf add off 1
103ret or b0 shl b1 8
104end
105
106fn boot_rd32_le buf view uchar * off ulong to uint
107b0 = uint get buf off
108b1 = uint get buf add off 1
109b2 = uint get buf add off 2
110b3 = uint get buf add off 3
111ret or or b0 shl b1 8 or shl b2 16 shl b3 24
112end
113
114fn boot_rd64_le buf view uchar * off ulong to ulong
115lo = ulong boot_rd32_le buf off
116hi = ulong boot_rd32_le buf add off 4
117ret or lo shl hi 32
118end
119
120fn boot_wr16_le buf edit uchar * off ulong v ushort to void
121put buf off uchar and v 0xff
122put buf add off 1 uchar and shr v 8 0xff
123end
124
125fn boot_wr32_le buf edit uchar * off ulong v uint to void
126put buf off uchar and v 0xff
127put buf add off 1 uchar and shr v 8 0xff
128put buf add off 2 uchar and shr v 16 0xff
129put buf add off 3 uchar and shr v 24 0xff
130end
131
132fn boot_wr64_le buf edit uchar * off ulong v ulong to void
133boot_wr32_le buf off uint and v 0xffffffff
134boot_wr32_le buf add off 4 uint and shr v 32 0xffffffff
135end
136
137fn boot_all_zero buf view uchar * off ulong n ulong to bool
138acc = uchar 0
139i = ulong 0
140while lt i n bound 4096
141acc = or acc get buf add off i
142i = add i 1
143end
144ret eq acc 0
145end
146
147fn boot_eq_bytes a view uchar * b view uchar * to bool
148na = len a
149if ne na len b
150ret false
151end
152diff = uchar 0
153i = ulong 0
154while lt i na bound 4096
155diff = or diff xor get a i get b i
156i = add i 1
157end
158ret eq diff 0
159end
160
161fn boot_eq_within buf view uchar * o1 ulong o2 ulong n ulong to bool
162diff = uchar 0
163i = ulong 0
164while lt i n bound 4096
165diff = or diff xor get buf add o1 i get buf add o2 i
166i = add i 1
167end
168ret eq diff 0
169end
170
171fn handoff_check_model buf view uchar * to int
172mflags = boot_rd32_le buf HO_OFF_MODEL_FLAGS
173allowed = or HO_MODEL_PRESENT HO_MODEL_BLOCKIO
174if ne and mflags xor uint 0xffffffff allowed 0
175ret HO_ERR_MODEL_FLAGS
176end
177if ne and mflags HO_MODEL_PRESENT 0
178mbase = boot_rd64_le buf HO_OFF_MODEL_BASE
179mlen = boot_rd64_le buf HO_OFF_MODEL_LEN
180if or eq mbase 0 eq mlen 0
181ret HO_ERR_MODEL_RANGE
182end
183if ne and mbase 0xfff 0
184ret HO_ERR_MODEL_ALIGN
185end
186if lt add mbase mlen mbase
187ret HO_ERR_MODEL_OVERFLOW
188end
189blksz = boot_rd32_le buf HO_OFF_MODEL_BLK_SZ
190if and ne blksz 512 ne blksz 4096
191ret HO_ERR_MODEL_BLKSZ
192end
193if eq boot_rd32_le buf HO_OFF_MODEL_EXTENTS 0
194ret HO_ERR_MODEL_EXTENTS
195end
196ret HO_OK
197end
198if ne boot_rd64_le buf HO_OFF_MODEL_BASE 0
199ret HO_ERR_MODEL_SPURIOUS
200end
201if ne boot_rd64_le buf HO_OFF_MODEL_LEN 0
202ret HO_ERR_MODEL_SPURIOUS
203end
204if ne mflags 0
205ret HO_ERR_MODEL_SPURIOUS
206end
207if ne boot_rd32_le buf HO_OFF_MODEL_EXTENTS 0
208ret HO_ERR_MODEL_SPURIOUS
209end
210if ne boot_rd64_le buf HO_OFF_MODEL_READ_US 0
211ret HO_ERR_MODEL_SPURIOUS
212end
213if ne boot_rd64_le buf HO_OFF_MODEL_LAST_BLK 0
214ret HO_ERR_MODEL_SPURIOUS
215end
216if ne boot_rd32_le buf HO_OFF_MODEL_BLK_SZ 0
217ret HO_ERR_MODEL_SPURIOUS
218end
219if not boot_all_zero buf HO_OFF_MODEL_SHA256 32
220ret HO_ERR_MODEL_SPURIOUS
221end
222ret HO_OK
223end
224
225fn handoff_check_fb buf view uchar * to int
226fst = boot_rd32_le buf HO_OFF_FB_STATUS
227fbase = boot_rd64_le buf HO_OFF_FB_BASE
228fsize = boot_rd64_le buf HO_OFF_FB_SIZE
229fw = boot_rd32_le buf HO_OFF_FB_WIDTH
230fh = boot_rd32_le buf HO_OFF_FB_HEIGHT
231fp = boot_rd32_le buf HO_OFF_FB_PITCH
232ffmt = boot_rd32_le buf HO_OFF_FB_FORMAT
233fgop = boot_rd32_le buf HO_OFF_FB_GOP_HANDLES
234if eq fst HO_FB_NONE
235if or ne fbase 0 ne fsize 0
236ret HO_ERR_FB_SPURIOUS
237end
238if or ne fw 0 or ne fh 0 ne fp 0
239ret HO_ERR_FB_SPURIOUS
240end
241if or ne ffmt 0 ne fgop 0
242ret HO_ERR_FB_SPURIOUS
243end
244ret HO_OK
245end
246if eq fst HO_FB_NO_LINEAR
247if or ne fbase 0 ne fsize 0
248ret HO_ERR_FB_SIZE
249end
250if eq fgop 0
251ret HO_ERR_FB_GOP
252end
253ret HO_OK
254end
255if ne fst HO_FB_PRESENT
256ret HO_ERR_FB_STATUS
257end
258if and ne ffmt HO_FB_RGBX ne ffmt HO_FB_BGRX
259ret HO_ERR_FB_FORMAT
260end
261if or eq fw 0 gt fw HO_FB_MAX_DIM
262ret HO_ERR_FB_DIM
263end
264if or eq fh 0 gt fh HO_FB_MAX_DIM
265ret HO_ERR_FB_DIM
266end
267if or lt fp fw gt fp HO_FB_MAX_DIM
268ret HO_ERR_FB_DIM
269end
270if or eq fbase 0 ne and fbase 3 0
271ret HO_ERR_FB_BASE
272end
273need = mul mul ulong fp ulong fh 4
274if lt fsize need
275ret HO_ERR_FB_SIZE
276end
277if lt add fbase fsize fbase
278ret HO_ERR_FB_OVERFLOW
279end
280if eq fgop 0
281ret HO_ERR_FB_GOP
282end
283ret HO_OK
284end
285
286fn handoff_check buf view uchar * to int
287if lt len buf HO_BYTES
288ret HO_ERR_SHORT
289end
290magic = boot_rd64_le buf HO_OFF_MAGIC
291if ne magic HO_MAGIC_V3
292ver_ch = shr magic 56
293if eq and magic HO_STEM_MASK HO_MAGIC_STEM
294if and ge ver_ch 48 le ver_ch 57
295ret HO_ERR_UNSUP_VER
296end
297end
298ret HO_ERR_BAD_MAGIC
299end
300if ne boot_rd32_le buf HO_OFF_RESERVED0 0
301ret HO_ERR_RESERVED0
302end
303if ne boot_rd32_le buf HO_OFF_RESERVED1 0
304ret HO_ERR_RESERVED1
305end
306mrc = handoff_check_model buf
307if ne mrc HO_OK
308ret mrc
309end
310frc = handoff_check_fb buf
311if ne frc HO_OK
312ret frc
313end
314dsz = boot_rd64_le buf HO_OFF_DESC_SIZE
315if or lt dsz HO_DESC_MIN ne and dsz 7 0
316ret HO_ERR_DESC_SIZE
317end
318if ne boot_rd32_le buf HO_OFF_DESC_VER HO_DESC_VERSION
319ret HO_ERR_DESC_VER
320end
321msz = boot_rd64_le buf HO_OFF_MAP_SIZE
322if or eq msz 0 or gt msz HO_MAP_MAX ne rem msz dsz 0
323ret HO_ERR_MAP_SIZE
324end
325ret HO_OK
326end