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 / slot.om

569 lines · 13731 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/boot/slot.om: Pure-Omega AIENOS A/B Native Image Slots (manifest, control record,
3note selection state machine, transitions, boot record SHA-256 seal/check, and kernel slot action).
4note Converted from upstream aienux native/boot/slot_format.h and native/boot/slot_select.c.
5
6const SLOT_UNIT ulong 4096
7const SLOT_CONTROL_UNITS ulong 4
8const SLOT_FIRST_UNIT ulong 4
9const SLOT_COUNT ulong 2
10const SLOT_NONE uchar 0xff
11const SLOT_MAX_TRIES uchar 3
12const SLOT_MIN_CAP_UNITS ulong 2
13
14note Manifest constants (4096 bytes)
15const SLOT_M_VERSION uint 1
16const SLOT_M_PAYLOAD_BOOTAA64 uint 1
17const SLOT_M_LOADER_ABI uint 1
18const SLOT_M_KERNEL_ABI uint 3
19const SLOT_M_TBS_BYTES ulong 448
20const SLOT_M_COMMIT_CHARS ulong 40
21const SLOT_M_PROV_CHARS ulong 64
22
23const SLOT_M_OFF_MAGIC ulong 0
24const SLOT_M_OFF_VER ulong 8
25const SLOT_M_OFF_PAYLOAD ulong 12
26const SLOT_M_OFF_LABI ulong 16
27const SLOT_M_OFF_KABI ulong 20
28const SLOT_M_OFF_GEN ulong 24
29const SLOT_M_OFF_PGEN ulong 32
30const SLOT_M_OFF_MINRB ulong 40
31const SLOT_M_OFF_IMGLEN ulong 48
32const SLOT_M_OFF_IMGSHA ulong 56
33const SLOT_M_OFF_KSHA ulong 120
34const SLOT_M_OFF_SIGNER ulong 184
35const SLOT_M_OFF_COMMIT ulong 216
36const SLOT_M_OFF_PROV ulong 256
37const SLOT_M_OFF_TS ulong 320
38const SLOT_M_OFF_FLAGS ulong 328
39const SLOT_M_OFF_RSVD0 ulong 332
40const SLOT_M_OFF_RSVD1 ulong 336
41const SLOT_M_OFF_SIG ulong 448
42const SLOT_M_OFF_PAD ulong 512
43
44note Control record constants (4096 bytes)
45const SLOT_C_VERSION uint 1
46const SLOT_C_FLAG_CAND_FAILED uchar 1
47const SLOT_C_OFF_MAGIC ulong 0
48const SLOT_C_OFF_VER ulong 8
49const SLOT_C_OFF_ACTIVE ulong 12
50const SLOT_C_OFF_CAND ulong 13
51const SLOT_C_OFF_TRIES ulong 14
52const SLOT_C_OFF_FLAGS ulong 15
53const SLOT_C_OFF_FLOOR ulong 16
54const SLOT_C_OFF_LASTGOOD ulong 24
55const SLOT_C_OFF_RSVD ulong 32
56
57note Boot record constants (256 bytes, SHA-256 over [0, 224))
58const SLOT_B_VERSION uint 1
59const SLOT_B_BYTES ulong 256
60const SLOT_B_SIGNED ulong 224
61const SLOT_B_OFF_MAGIC ulong 0
62const SLOT_B_OFF_VER ulong 8
63const SLOT_B_OFF_SLOT ulong 12
64const SLOT_B_OFF_REASON ulong 13
65const SLOT_B_OFF_TRIES ulong 14
66const SLOT_B_OFF_CTL_PRES ulong 15
67const SLOT_B_OFF_CTL_SEQ ulong 16
68const SLOT_B_OFF_GEN ulong 24
69const SLOT_B_OFF_PGEN ulong 32
70const SLOT_B_OFF_IMGSHA ulong 40
71const SLOT_B_OFF_COMMIT ulong 104
72const SLOT_B_OFF_RSVD ulong 144
73const SLOT_B_OFF_DIGEST ulong 224
74
75note Decision reason codes
76const SLOT_R_NONE uint 0
77const SLOT_R_CANDIDATE_TRY uint 1
78const SLOT_R_ACTIVE uint 2
79const SLOT_R_CANDIDATE_EXHAUSTED uint 3
80const SLOT_R_CANDIDATE_INVALID uint 4
81const SLOT_R_ACTIVE_INVALID_FALLBACK uint 5
82const SLOT_R_NO_CONTROL uint 6
83const SLOT_R_RECOVERY_REQUIRED uint 7
84
85note Kernel slot action codes
86const SLOT_A_NONE uint 0
87const SLOT_A_PROMOTE uint 1
88const SLOT_A_ABANDON uint 2
89const SLOT_A_WAIT uint 3
90const SLOT_A_REFUSE uint 4
91
92note Error codes
93const SLOT_OK int 0
94const SLOT_ERR_SHORT int -1
95const SLOT_ERR_MAGIC int -2
96const SLOT_ERR_VER int -3
97const SLOT_ERR_PAYLOAD int -4
98const SLOT_ERR_ABI int -5
99const SLOT_ERR_GEN int -6
100const SLOT_ERR_IMGLEN int -7
101const SLOT_ERR_SHA_MISMATCH int -8
102const SLOT_ERR_TS int -9
103const SLOT_ERR_FLAGS int -10
104const SLOT_ERR_RSVD int -11
105const SLOT_ERR_COMMIT int -12
106const SLOT_ERR_PROV int -13
107const SLOT_ERR_RANGE int -14
108const SLOT_ERR_STATE int -15
109const SLOT_ERR_FLOOR int -16
110const SLOT_ERR_DIGEST int -17
111
112text slot_m_magic "AIENSLT1"
113text slot_c_magic "AIENSLC1"
114text slot_b_magic "AIENSLB1"
115
116fn slot_cap_units part_units ulong to ulong
117if lt part_units SLOT_FIRST_UNIT
118ret 0
119end
120ret div sub part_units SLOT_FIRST_UNIT SLOT_COUNT
121end
122
123fn slot_manifest_unit slot_idx ulong cap_units ulong to ulong
124ret add SLOT_FIRST_UNIT mul slot_idx cap_units
125end
126
127fn slot_is_lower_hex ch uchar to bool
128if and ge ch 48 le ch 57
129ret true
130end
131if and ge ch 97 le ch 102
132ret true
133end
134ret false
135end
136
137fn slot_copy_bytes dst edit uchar * src view uchar * n ulong to void
138i = ulong 0
139while lt i n bound 4096
140put dst i get src i
141i = add i 1
142end
143end
144
145fn slot_zero_bytes dst edit uchar * n ulong to void
146i = ulong 0
147while lt i n bound 4096
148put dst i 0
149i = add i 1
150end
151end
152
153fn slot_manifest_check buf view uchar * to int
154if lt len buf SLOT_UNIT
155ret SLOT_ERR_SHORT
156end
157if not boot_eq_bytes part buf SLOT_M_OFF_MAGIC 8 slot_m_magic
158ret SLOT_ERR_MAGIC
159end
160if ne boot_rd32_le buf SLOT_M_OFF_VER SLOT_M_VERSION
161ret SLOT_ERR_VER
162end
163if ne boot_rd32_le buf SLOT_M_OFF_PAYLOAD SLOT_M_PAYLOAD_BOOTAA64
164ret SLOT_ERR_PAYLOAD
165end
166if ne boot_rd32_le buf SLOT_M_OFF_LABI SLOT_M_LOADER_ABI
167ret SLOT_ERR_ABI
168end
169if ne boot_rd32_le buf SLOT_M_OFF_KABI SLOT_M_KERNEL_ABI
170ret SLOT_ERR_ABI
171end
172gen = boot_rd64_le buf SLOT_M_OFF_GEN
173pgen = boot_rd64_le buf SLOT_M_OFF_PGEN
174minrb = boot_rd64_le buf SLOT_M_OFF_MINRB
175if or eq gen 0 or gt pgen gen gt minrb gen
176ret SLOT_ERR_GEN
177end
178if eq boot_rd64_le buf SLOT_M_OFF_IMGLEN 0
179ret SLOT_ERR_IMGLEN
180end
181if not boot_eq_within buf SLOT_M_OFF_KSHA SLOT_M_OFF_IMGSHA 64
182ret SLOT_ERR_SHA_MISMATCH
183end
184if eq boot_rd64_le buf SLOT_M_OFF_TS 0
185ret SLOT_ERR_TS
186end
187if ne boot_rd32_le buf SLOT_M_OFF_FLAGS 0
188ret SLOT_ERR_FLAGS
189end
190if ne boot_rd32_le buf SLOT_M_OFF_RSVD0 0
191ret SLOT_ERR_RSVD
192end
193if not boot_all_zero buf SLOT_M_OFF_RSVD1 112
194ret SLOT_ERR_RSVD
195end
196if not boot_all_zero buf SLOT_M_OFF_PAD 3584
197ret SLOT_ERR_RSVD
198end
199i = ulong 0
200while lt i SLOT_M_COMMIT_CHARS bound 40
201if not slot_is_lower_hex get buf add SLOT_M_OFF_COMMIT i
202ret SLOT_ERR_COMMIT
203end
204i = add i 1
205end
206ended = false
207j = ulong 0
208while lt j SLOT_M_PROV_CHARS bound 64
209ch = get buf add SLOT_M_OFF_PROV j
210if eq ch 0
211ended = true
212else
213if or ended or lt ch 0x20 gt ch 0x7e
214ret SLOT_ERR_PROV
215end
216end
217j = add j 1
218end
219ret SLOT_OK
220end
221
222fn slot_control_check buf view uchar * to int
223if lt len buf SLOT_UNIT
224ret SLOT_ERR_SHORT
225end
226if not boot_eq_bytes part buf SLOT_C_OFF_MAGIC 8 slot_c_magic
227ret SLOT_ERR_MAGIC
228end
229if ne boot_rd32_le buf SLOT_C_OFF_VER SLOT_C_VERSION
230ret SLOT_ERR_VER
231end
232act = get buf SLOT_C_OFF_ACTIVE
233cand = get buf SLOT_C_OFF_CAND
234tries = get buf SLOT_C_OFF_TRIES
235flags = get buf SLOT_C_OFF_FLAGS
236if ge ulong act SLOT_COUNT
237ret SLOT_ERR_RANGE
238end
239if and ne cand SLOT_NONE ge ulong cand SLOT_COUNT
240ret SLOT_ERR_RANGE
241end
242if eq cand act
243ret SLOT_ERR_STATE
244end
245if gt tries SLOT_MAX_TRIES
246ret SLOT_ERR_RANGE
247end
248if and eq cand SLOT_NONE ne tries 0
249ret SLOT_ERR_STATE
250end
251if ne and flags xor uchar 0xff SLOT_C_FLAG_CAND_FAILED 0
252ret SLOT_ERR_FLAGS
253end
254floor = boot_rd64_le buf SLOT_C_OFF_FLOOR
255lg = boot_rd64_le buf SLOT_C_OFF_LASTGOOD
256if and ne lg 0 lt lg floor
257ret SLOT_ERR_FLOOR
258end
259if not boot_all_zero buf SLOT_C_OFF_RSVD 4064
260ret SLOT_ERR_RSVD
261end
262ret SLOT_OK
263end
264
265fn slot_control_init dst edit uchar * active_slot uchar to void
266slot_zero_bytes dst SLOT_UNIT
267for i ulong 8
268put dst i get slot_c_magic i
269end
270boot_wr32_le dst SLOT_C_OFF_VER SLOT_C_VERSION
271put dst SLOT_C_OFF_ACTIVE and active_slot 1
272put dst SLOT_C_OFF_CAND SLOT_NONE
273put dst SLOT_C_OFF_TRIES 0
274end
275
276fn slot_stage_candidate ctl edit uchar * target uchar gen ulong to int
277rc = slot_control_check ctl
278if ne rc SLOT_OK
279ret rc
280end
281if ge ulong target SLOT_COUNT
282ret SLOT_ERR_RANGE
283end
284if eq target get ctl SLOT_C_OFF_ACTIVE
285ret SLOT_ERR_STATE
286end
287floor = boot_rd64_le ctl SLOT_C_OFF_FLOOR
288if lt gen floor
289ret SLOT_ERR_FLOOR
290end
291lg = boot_rd64_le ctl SLOT_C_OFF_LASTGOOD
292if and ne lg 0 le gen lg
293ret SLOT_ERR_FLOOR
294end
295put ctl SLOT_C_OFF_CAND target
296put ctl SLOT_C_OFF_TRIES SLOT_MAX_TRIES
297fl = get ctl SLOT_C_OFF_FLAGS
298put ctl SLOT_C_OFF_FLAGS and fl xor uchar 0xff SLOT_C_FLAG_CAND_FAILED
299ret SLOT_OK
300end
301
302fn slot_promote ctl edit uchar * booted uchar gen ulong pgen ulong to int
303rc = slot_control_check ctl
304if ne rc SLOT_OK
305ret rc
306end
307if ge ulong booted SLOT_COUNT
308ret SLOT_ERR_RANGE
309end
310cand = get ctl SLOT_C_OFF_CAND
311if or eq cand SLOT_NONE ne booted cand
312ret SLOT_ERR_STATE
313end
314floor = boot_rd64_le ctl SLOT_C_OFF_FLOOR
315if lt gen floor
316ret SLOT_ERR_FLOOR
317end
318if gt pgen gen
319ret SLOT_ERR_GEN
320end
321put ctl SLOT_C_OFF_ACTIVE booted
322put ctl SLOT_C_OFF_CAND SLOT_NONE
323put ctl SLOT_C_OFF_TRIES 0
324fl = get ctl SLOT_C_OFF_FLAGS
325put ctl SLOT_C_OFF_FLAGS and fl xor uchar 0xff SLOT_C_FLAG_CAND_FAILED
326boot_wr64_le ctl SLOT_C_OFF_LASTGOOD gen
327if gt pgen floor
328boot_wr64_le ctl SLOT_C_OFF_FLOOR pgen
329end
330ret SLOT_OK
331end
332
333fn slot_abandon_candidate ctl edit uchar * to int
334rc = slot_control_check ctl
335if ne rc SLOT_OK
336ret rc
337end
338if eq get ctl SLOT_C_OFF_CAND SLOT_NONE
339ret SLOT_ERR_STATE
340end
341put ctl SLOT_C_OFF_CAND SLOT_NONE
342put ctl SLOT_C_OFF_TRIES 0
343fl = get ctl SLOT_C_OFF_FLAGS
344put ctl SLOT_C_OFF_FLAGS or fl SLOT_C_FLAG_CAND_FAILED
345ret SLOT_OK
346end
347
348fn slot_eval_usable ev view SlotEval floor ulong to bool
349if eq get ev valid 0
350ret false
351end
352ret ge get ev generation floor
353end
354
355fn slot_select_apply_ctl ctl view uchar 4096 ev0 view SlotEval ev1 view SlotEval dec edit SlotDecision ctl_dst edit uchar 4096 to void
356floor = boot_rd64_le ctl SLOT_C_OFF_FLOOR
357act = get ctl SLOT_C_OFF_ACTIVE
358cand = get ctl SLOT_C_OFF_CAND
359tries = get ctl SLOT_C_OFF_TRIES
360act_u = slot_eval_usable ev0 floor
361oth_u = slot_eval_usable ev1 floor
362if eq act 1
363act_u = slot_eval_usable ev1 floor
364oth_u = slot_eval_usable ev0 floor
365end
366if ne cand SLOT_NONE
367cand_u = slot_eval_usable ev0 floor
368if eq cand 1
369cand_u = slot_eval_usable ev1 floor
370end
371if and gt tries 0 cand_u
372put dec boot_slot int cand
373put dec reason SLOT_R_CANDIDATE_TRY
374put dec loader_write 1
375slot_copy_bytes ctl_dst ctl SLOT_UNIT
376put ctl_dst SLOT_C_OFF_TRIES sub tries 1
377ret
378end
379if act_u
380put dec boot_slot int act
381if eq tries 0
382put dec reason SLOT_R_CANDIDATE_EXHAUSTED
383else
384put dec reason SLOT_R_CANDIDATE_INVALID
385end
386ret
387end
388end
389if act_u
390put dec boot_slot int act
391put dec reason SLOT_R_ACTIVE
392ret
393end
394other = xor act 1
395if and oth_u ne cand other
396put dec boot_slot int other
397put dec reason SLOT_R_ACTIVE_INVALID_FALLBACK
398ret
399end
400end
401
402fn slot_select has_ctl bool ctl view uchar 4096 ev0 view SlotEval ev1 view SlotEval dec edit SlotDecision ctl_dst edit uchar 4096 to void
403put dec boot_slot -1
404put dec reason SLOT_R_RECOVERY_REQUIRED
405put dec loader_write 0
406slot_zero_bytes ctl_dst SLOT_UNIT
407if not has_ctl
408ret
409end
410if ne slot_control_check ctl SLOT_OK
411ret
412end
413slot_select_apply_ctl ctl ev0 ev1 dec ctl_dst
414end
415
416fn slot_boot_seal buf edit uchar * to void
417dig = new uchar 32 0
418msg = new uchar 224 0
419for i ulong 224
420put msg i get buf i
421end
422dlen = sha2_digest 256 msg dig
423if eq dlen 32
424for i ulong 32
425put buf add SLOT_B_OFF_DIGEST i get dig i
426end
427end
428end
429
430fn slot_boot_check buf view uchar * to int
431if lt len buf SLOT_B_BYTES
432ret SLOT_ERR_SHORT
433end
434if not boot_eq_bytes part buf SLOT_B_OFF_MAGIC 8 slot_b_magic
435ret SLOT_ERR_MAGIC
436end
437if ne boot_rd32_le buf SLOT_B_OFF_VER SLOT_B_VERSION
438ret SLOT_ERR_VER
439end
440msg = new uchar 224 0
441dig = new uchar 32 0
442for i ulong 224
443put msg i get buf i
444end
445dlen = sha2_digest 256 msg dig
446if ne dlen 32
447ret SLOT_ERR_DIGEST
448end
449if not boot_eq_bytes dig part buf SLOT_B_OFF_DIGEST 32
450ret SLOT_ERR_DIGEST
451end
452bslot = get buf SLOT_B_OFF_SLOT
453reason = uint get buf SLOT_B_OFF_REASON
454tries = get buf SLOT_B_OFF_TRIES
455cpres = get buf SLOT_B_OFF_CTL_PRES
456cseq = boot_rd64_le buf SLOT_B_OFF_CTL_SEQ
457gen = boot_rd64_le buf SLOT_B_OFF_GEN
458pgen = boot_rd64_le buf SLOT_B_OFF_PGEN
459if ge ulong bslot SLOT_COUNT
460ret SLOT_ERR_RANGE
461end
462if or le reason SLOT_R_NONE ge reason SLOT_R_RECOVERY_REQUIRED
463ret SLOT_ERR_RANGE
464end
465if gt cpres 1
466ret SLOT_ERR_RANGE
467end
468if gt tries SLOT_MAX_TRIES
469ret SLOT_ERR_RANGE
470end
471if or eq gen 0 gt pgen gen
472ret SLOT_ERR_GEN
473end
474if not boot_all_zero buf SLOT_B_OFF_RSVD 80
475ret SLOT_ERR_RSVD
476end
477if eq cpres 0
478if or ne reason SLOT_R_NO_CONTROL ne cseq 0
479ret SLOT_ERR_STATE
480end
481else
482if eq reason SLOT_R_NO_CONTROL
483ret SLOT_ERR_STATE
484end
485end
486ret SLOT_OK
487end
488
489fn slot_brec_matches_man brec view uchar 256 man view uchar 4096 to bool
490if ne boot_rd64_le man SLOT_M_OFF_GEN boot_rd64_le brec SLOT_B_OFF_GEN
491ret false
492end
493if ne boot_rd64_le man SLOT_M_OFF_PGEN boot_rd64_le brec SLOT_B_OFF_PGEN
494ret false
495end
496ret boot_eq_bytes part man SLOT_M_OFF_IMGSHA 64 part brec SLOT_B_OFF_IMGSHA 64
497end
498
499fn slot_kernel_action_inner brec view uchar 256 ctl view uchar 4096 man view uchar 4096 healthy bool ctl_dst edit uchar 4096 to uint
500if ne slot_boot_check brec SLOT_OK
501ret SLOT_A_REFUSE
502end
503if ne slot_control_check ctl SLOT_OK
504ret SLOT_A_REFUSE
505end
506if ne slot_manifest_check man SLOT_OK
507ret SLOT_A_REFUSE
508end
509if not slot_brec_matches_man brec man
510ret SLOT_A_REFUSE
511end
512slot_copy_bytes ctl_dst ctl SLOT_UNIT
513bslot = get brec SLOT_B_OFF_SLOT
514reason = uint get brec SLOT_B_OFF_REASON
515cand = get ctl SLOT_C_OFF_CAND
516act = get ctl SLOT_C_OFF_ACTIVE
517if eq reason SLOT_R_CANDIDATE_TRY
518if ne cand bslot
519ret SLOT_A_NONE
520end
521if not healthy
522ret SLOT_A_WAIT
523end
524if ne slot_promote ctl_dst bslot boot_rd64_le man SLOT_M_OFF_GEN boot_rd64_le man SLOT_M_OFF_PGEN SLOT_OK
525ret SLOT_A_REFUSE
526end
527ret SLOT_A_PROMOTE
528end
529if or eq reason SLOT_R_CANDIDATE_EXHAUSTED eq reason SLOT_R_CANDIDATE_INVALID
530if and ne cand SLOT_NONE eq act bslot
531if ne slot_abandon_candidate ctl_dst SLOT_OK
532ret SLOT_A_REFUSE
533end
534ret SLOT_A_ABANDON
535end
536end
537ret SLOT_A_NONE
538end
539
540fn slot_verify_image_sha512 man view uchar 4096 img view uchar * img_cap ulong ev edit SlotEval to bool
541put ev valid 0
542put ev generation 0
543put ev policy_gen 0
544rc = slot_manifest_check man
545if ne rc SLOT_OK
546put ev err rc
547ret false
548end
549ilen = boot_rd64_le man SLOT_M_OFF_IMGLEN
550if or gt ilen img_cap gt ilen len img
551put ev err SLOT_ERR_IMGLEN
552ret false
553end
554dig = new uchar 64 0
555dlen = sha2_digest 512 part img 0 ilen dig
556if ne dlen 64
557put ev err SLOT_ERR_DIGEST
558ret false
559end
560if not boot_eq_bytes dig part man SLOT_M_OFF_IMGSHA 64
561put ev err SLOT_ERR_SHA_MISMATCH
562ret false
563end
564put ev valid 1
565put ev generation boot_rd64_le man SLOT_M_OFF_GEN
566put ev policy_gen boot_rd64_le man SLOT_M_OFF_PGEN
567put ev err SLOT_OK
568ret true
569end