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

590 lines · 15133 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/boot/boot_test.om: Pure-Omega behavioral self-test suite for Aienux Boot
3note (CHANDOF3 Handoff, MODEL.MAP, A/B Native Image Slots, and ANB1 Netboot Protocol).
4note Compose with: si/directives/om2.om aienux/boot/handoff.om aienux/boot/model_map.om aienux/boot/slot.om aienux/boot/netboot.om aienux/boot/boot_test.om om/tls/sha2.om
5
6text b_pass "PASS "
7text b_fail "FAIL "
8text b_nl "\n"
9text b_t01 "handoff_valid_and_version_refusals"
10text b_t02 "handoff_model_and_fb_validation"
11text b_t03 "handoff_mmap_and_reserved_refusals"
12text b_t04 "model_map_valid_and_negative_controls"
13text b_t05 "slot_manifest_and_control_checks"
14text b_t06 "slot_select_candidate_try_and_exhaust"
15text b_t07 "slot_select_fallback_floor_and_recovery"
16text b_t08 "slot_transitions_stage_promote_abandon"
17text b_t09 "slot_boot_record_seal_verify_and_kernel_action"
18text b_t10 "slot_verify_image_sha512_roundtrip"
19text b_t11 "netboot_build_parse_all_types"
20text b_t12 "netboot_negative_controls_and_chunks"
21text b_sum "BOOT_TEST_PASS total=12 failed=0\n"
22text b_fsum "BOOT_TEST_FAIL\n"
23text b_commit "0123456789abcdef0123456789abcdef01234567"
24text b_prov "aienux-om2-test"
25text b_nb_done "verified ok"
26
27fn bwrite_all fd int buf view uchar * to bool
28total = len buf
29off = ulong 0
30while lt off total bound 4096
31n = write fd buf off sub total off
32if le n 0
33ret false
34end
35off = add off ulong n
36end
37ret true
38end
39
40fn breport name view uchar * ok bool to uint
41if ok
42w1 = bwrite_all 1 b_pass
43w2 = bwrite_all 1 name
44w3 = bwrite_all 1 b_nl
45if and w1 and w2 w3
46ret 0
47end
48ret 1
49end
50w1 = bwrite_all 1 b_fail
51w2 = bwrite_all 1 name
52w3 = bwrite_all 1 b_nl
53if and w1 and w2 w3
54ret 1
55end
56ret 1
57end
58
59fn mk_valid_handoff dst edit uchar * to void
60slot_zero_bytes dst HO_BYTES
61boot_wr64_le dst HO_OFF_MAGIC HO_MAGIC_V3
62boot_wr32_le dst HO_OFF_FW_EL 2
63boot_wr32_le dst HO_OFF_DESC_VER HO_DESC_VERSION
64boot_wr64_le dst HO_OFF_MAP_SIZE 480
65boot_wr64_le dst HO_OFF_DESC_SIZE 48
66end
67
68fn test_01_handoff_magic to bool
69h = new uchar 232 0
70mk_valid_handoff h
71if ne handoff_check h HO_OK
72ret false
73end
74note CHANDOF1 and CHANDOF2 must fail with HO_ERR_UNSUP_VER
75boot_wr64_le h HO_OFF_MAGIC HO_MAGIC_V1
76if ne handoff_check h HO_ERR_UNSUP_VER
77ret false
78end
79boot_wr64_le h HO_OFF_MAGIC HO_MAGIC_V2
80if ne handoff_check h HO_ERR_UNSUP_VER
81ret false
82end
83note Arbitrary garbage magic fails with HO_ERR_BAD_MAGIC
84boot_wr64_le h HO_OFF_MAGIC 0xdeadbeefcafebabe
85if ne handoff_check h HO_ERR_BAD_MAGIC
86ret false
87end
88note Short buffer refused
89short_h = new uchar 128 0
90if ne handoff_check short_h HO_ERR_SHORT
91ret false
92end
93ret true
94end
95
96fn test_02_handoff_model_fb to bool
97h = new uchar 232 0
98mk_valid_handoff h
99note Valid model present
100boot_wr32_le h HO_OFF_MODEL_FLAGS or HO_MODEL_PRESENT HO_MODEL_BLOCKIO
101boot_wr64_le h HO_OFF_MODEL_BASE 0x40000000
102boot_wr64_le h HO_OFF_MODEL_LEN 8192
103boot_wr32_le h HO_OFF_MODEL_BLK_SZ 4096
104boot_wr32_le h HO_OFF_MODEL_EXTENTS 2
105if ne handoff_check h HO_OK
106ret false
107end
108note Unaligned model base refused
109boot_wr64_le h HO_OFF_MODEL_BASE 0x40000100
110if ne handoff_check h HO_ERR_MODEL_ALIGN
111ret false
112end
113note Spurious model field without PRESENT refused
114mk_valid_handoff h
115put h HO_OFF_MODEL_SHA256 1
116if ne handoff_check h HO_ERR_MODEL_SPURIOUS
117ret false
118end
119note Valid linear GOP framebuffer
120mk_valid_handoff h
121boot_wr32_le h HO_OFF_FB_STATUS HO_FB_PRESENT
122boot_wr32_le h HO_OFF_FB_FORMAT HO_FB_BGRX
123boot_wr32_le h HO_OFF_FB_WIDTH 1920
124boot_wr32_le h HO_OFF_FB_HEIGHT 1080
125boot_wr32_le h HO_OFF_FB_PITCH 1920
126boot_wr64_le h HO_OFF_FB_BASE 0x80000000
127boot_wr64_le h HO_OFF_FB_SIZE mul mul ulong 1920 1080 4
128boot_wr32_le h HO_OFF_FB_GOP_HANDLES 1
129if ne handoff_check h HO_OK
130ret false
131end
132note FB size too small refused
133boot_wr64_le h HO_OFF_FB_SIZE 4096
134if ne handoff_check h HO_ERR_FB_SIZE
135ret false
136end
137note FB pitch < width refused
138boot_wr64_le h HO_OFF_FB_SIZE mul mul ulong 1920 1080 4
139boot_wr32_le h HO_OFF_FB_PITCH 1000
140if ne handoff_check h HO_ERR_FB_DIM
141ret false
142end
143ret true
144end
145
146fn test_03_handoff_mmap_rsvd to bool
147h = new uchar 232 0
148mk_valid_handoff h
149boot_wr32_le h HO_OFF_RESERVED0 1
150if ne handoff_check h HO_ERR_RESERVED0
151ret false
152end
153mk_valid_handoff h
154boot_wr32_le h HO_OFF_RESERVED1 1
155if ne handoff_check h HO_ERR_RESERVED1
156ret false
157end
158mk_valid_handoff h
159boot_wr64_le h HO_OFF_DESC_SIZE 32
160if ne handoff_check h HO_ERR_DESC_SIZE
161ret false
162end
163mk_valid_handoff h
164boot_wr32_le h HO_OFF_DESC_VER 2
165if ne handoff_check h HO_ERR_DESC_VER
166ret false
167end
168mk_valid_handoff h
169boot_wr64_le h HO_OFF_MAP_SIZE 500
170if ne handoff_check h HO_ERR_MAP_SIZE
171ret false
172end
173ret true
174end
175
176fn test_04_model_map to bool
177m = new uchar 4096 0
178for i ulong 8
179put m i get mmap_magic i
180end
181boot_wr32_le m MMAP_OFF_VERSION MMAP_VERSION
182boot_wr32_le m MMAP_OFF_SECTOR_SZ 4096
183boot_wr64_le m MMAP_OFF_MODEL_LEN 10000
184boot_wr32_le m MMAP_OFF_N_EXTENTS 2
185note Need ceil(10000/4096) = 3 sectors -> extent 0: lba=10, cnt=2; extent 1: lba=50, cnt=1
186boot_wr64_le m MMAP_OFF_EXTENTS 10
187boot_wr64_le m add MMAP_OFF_EXTENTS 8 2
188boot_wr64_le m add MMAP_OFF_EXTENTS 16 50
189boot_wr64_le m add MMAP_OFF_EXTENTS 24 1
190if ne model_map_check m 4096 100 MMAP_OK
191ret false
192end
193note Disk block size mismatch refused
194if ne model_map_check m 512 100 MMAP_ERR_BLK_MISMATCH
195ret false
196end
197note Extent past last_block refused
198if ne model_map_check m 4096 49 MMAP_ERR_EXT_PAST_DISK
199ret false
200end
201note Wrong sector coverage refused
202boot_wr64_le m add MMAP_OFF_EXTENTS 24 2
203if ne model_map_check m 4096 100 MMAP_ERR_COVERAGE
204ret false
205end
206note Extent inside GPT header (lba < 2) refused
207boot_wr64_le m add MMAP_OFF_EXTENTS 24 1
208boot_wr64_le m MMAP_OFF_EXTENTS 1
209if ne model_map_check m 4096 100 MMAP_ERR_EXT_BAD
210ret false
211end
212ret true
213end
214
215fn mk_valid_manifest dst edit uchar * gen ulong pgen ulong imglen ulong to void
216slot_zero_bytes dst SLOT_UNIT
217for i ulong 8
218put dst i get slot_m_magic i
219end
220boot_wr32_le dst SLOT_M_OFF_VER SLOT_M_VERSION
221boot_wr32_le dst SLOT_M_OFF_PAYLOAD SLOT_M_PAYLOAD_BOOTAA64
222boot_wr32_le dst SLOT_M_OFF_LABI SLOT_M_LOADER_ABI
223boot_wr32_le dst SLOT_M_OFF_KABI SLOT_M_KERNEL_ABI
224boot_wr64_le dst SLOT_M_OFF_GEN gen
225boot_wr64_le dst SLOT_M_OFF_PGEN pgen
226boot_wr64_le dst SLOT_M_OFF_MINRB pgen
227boot_wr64_le dst SLOT_M_OFF_IMGLEN imglen
228for i ulong 64
229put dst add SLOT_M_OFF_IMGSHA i uchar add 0x10 and i 0x3f
230put dst add SLOT_M_OFF_KSHA i uchar add 0x10 and i 0x3f
231end
232for i ulong 40
233put dst add SLOT_M_OFF_COMMIT i get b_commit i
234end
235for i ulong 15
236put dst add SLOT_M_OFF_PROV i get b_prov i
237end
238boot_wr64_le dst SLOT_M_OFF_TS 1700000000
239end
240
241fn test_05_slot_struct_checks to bool
242if ne slot_cap_units 104 50
243ret false
244end
245if ne slot_manifest_unit 1 50 54
246ret false
247end
248m = new uchar 4096 0
249mk_valid_manifest m 5 3 4096
250if ne slot_manifest_check m SLOT_OK
251ret false
252end
253note policy_generation > generation refused
254boot_wr64_le m SLOT_M_OFF_PGEN 6
255if ne slot_manifest_check m SLOT_ERR_GEN
256ret false
257end
258note kernel_sha512 != image_sha512 refused
259mk_valid_manifest m 5 3 4096
260put m SLOT_M_OFF_KSHA 0xff
261if ne slot_manifest_check m SLOT_ERR_SHA_MISMATCH
262ret false
263end
264note Non-zero pad refused
265mk_valid_manifest m 5 3 4096
266put m 4095 1
267if ne slot_manifest_check m SLOT_ERR_RSVD
268ret false
269end
270note Control check positive and negative
271c = new uchar 4096 0
272slot_control_init c 0
273if ne slot_control_check c SLOT_OK
274ret false
275end
276put c SLOT_C_OFF_CAND 0
277if ne slot_control_check c SLOT_ERR_STATE
278ret false
279end
280ret true
281end
282
283fn test_06_slot_select_cand to bool
284ctl = new uchar 4096 0
285ctl_out = new uchar 4096 0
286slot_control_init ctl 0
287if ne slot_stage_candidate ctl 1 2 SLOT_OK
288ret false
289end
290ev0 = new SlotEval 1 1 1 0
291ev1 = new SlotEval 1 2 1 0
292dec = new SlotDecision -1 0 0
293note First try boots candidate slot 1 and decrements tries 3 -> 2
294slot_select true ctl ev0 ev1 dec ctl_out
295if or ne get dec boot_slot 1 or ne get dec reason SLOT_R_CANDIDATE_TRY ne get dec loader_write 1
296ret false
297end
298if ne get ctl_out SLOT_C_OFF_TRIES 2
299ret false
300end
301note Exhausted candidate (tries=0) falls back to active slot 0 with no loader write
302put ctl SLOT_C_OFF_TRIES 0
303slot_select true ctl ev0 ev1 dec ctl_out
304if or ne get dec boot_slot 0 or ne get dec reason SLOT_R_CANDIDATE_EXHAUSTED ne get dec loader_write 0
305ret false
306end
307ret true
308end
309
310fn test_07_slot_select_fallback to bool
311ctl = new uchar 4096 0
312ctl_out = new uchar 4096 0
313slot_control_init ctl 0
314boot_wr64_le ctl SLOT_C_OFF_FLOOR 5
315ev0 = new SlotEval 1 4 4 0
316ev1 = new SlotEval 1 6 5 0
317dec = new SlotDecision -1 0 0
318note Active slot 0 is below floor 5 -> fallback to slot 1
319slot_select true ctl ev0 ev1 dec ctl_out
320if or ne get dec boot_slot 1 ne get dec reason SLOT_R_ACTIVE_INVALID_FALLBACK
321ret false
322end
323note Missing control record ALWAYS requires recovery
324slot_select false ctl ev0 ev1 dec ctl_out
325if or ne get dec boot_slot -1 ne get dec reason SLOT_R_RECOVERY_REQUIRED
326ret false
327end
328note Both slots below floor -> recovery required
329ev1_lo = new SlotEval 1 3 3 0
330slot_select true ctl ev0 ev1_lo dec ctl_out
331if or ne get dec boot_slot -1 ne get dec reason SLOT_R_RECOVERY_REQUIRED
332ret false
333end
334ret true
335end
336
337fn test_08_slot_transitions to bool
338ctl = new uchar 4096 0
339slot_control_init ctl 0
340note Stage candidate slot 1 at gen 10
341if ne slot_stage_candidate ctl 1 10 SLOT_OK
342ret false
343end
344note Cannot stage active slot 0
345if ne slot_stage_candidate ctl 0 11 SLOT_ERR_STATE
346ret false
347end
348note Promote slot 1 with policy_gen 8 -> active=1, floor=8, last_good=10
349if ne slot_promote ctl 1 10 8 SLOT_OK
350ret false
351end
352if or ne get ctl SLOT_C_OFF_ACTIVE 1 ne get ctl SLOT_C_OFF_CAND SLOT_NONE
353ret false
354end
355if or ne boot_rd64_le ctl SLOT_C_OFF_FLOOR 8 ne boot_rd64_le ctl SLOT_C_OFF_LASTGOOD 10
356ret false
357end
358note Cannot stage downgrade gen 9 <= last_good 10
359if ne slot_stage_candidate ctl 0 9 SLOT_ERR_FLOOR
360ret false
361end
362note Stage slot 0 at gen 12, then abandon -> CANDIDATE_FAILED flag set
363if ne slot_stage_candidate ctl 0 12 SLOT_OK
364ret false
365end
366if ne slot_abandon_candidate ctl SLOT_OK
367ret false
368end
369if or ne get ctl SLOT_C_OFF_CAND SLOT_NONE ne get ctl SLOT_C_OFF_FLAGS SLOT_C_FLAG_CAND_FAILED
370ret false
371end
372ret true
373end
374
375fn test_09_boot_rec_and_action to bool
376ctl = new uchar 4096 0
377ctl_out = new uchar 4096 0
378man = new uchar 4096 0
379brec = new uchar 256 0
380slot_control_init ctl 0
381if ne slot_stage_candidate ctl 1 7 SLOT_OK
382ret false
383end
384mk_valid_manifest man 7 5 4096
385for i ulong 8
386put brec i get slot_b_magic i
387end
388boot_wr32_le brec SLOT_B_OFF_VER SLOT_B_VERSION
389put brec SLOT_B_OFF_SLOT 1
390put brec SLOT_B_OFF_REASON uchar SLOT_R_CANDIDATE_TRY
391put brec SLOT_B_OFF_TRIES 2
392put brec SLOT_B_OFF_CTL_PRES 1
393boot_wr64_le brec SLOT_B_OFF_CTL_SEQ 1
394boot_wr64_le brec SLOT_B_OFF_GEN 7
395boot_wr64_le brec SLOT_B_OFF_PGEN 5
396for i ulong 64
397put brec add SLOT_B_OFF_IMGSHA i get man add SLOT_M_OFF_IMGSHA i
398end
399slot_boot_seal brec
400if ne slot_boot_check brec SLOT_OK
401ret false
402end
403note Unhealthy candidate try -> WAIT
404if ne slot_kernel_action_inner brec ctl man false ctl_out SLOT_A_WAIT
405ret false
406end
407note Healthy candidate try -> PROMOTE
408if ne slot_kernel_action_inner brec ctl man true ctl_out SLOT_A_PROMOTE
409ret false
410end
411if or ne get ctl_out SLOT_C_OFF_ACTIVE 1 ne boot_rd64_le ctl_out SLOT_C_OFF_FLOOR 5
412ret false
413end
414note Tampered boot record byte breaks SHA-256 -> REFUSE
415put brec SLOT_B_OFF_TRIES 1
416if ne slot_boot_check brec SLOT_ERR_DIGEST
417ret false
418end
419if ne slot_kernel_action_inner brec ctl man true ctl_out SLOT_A_REFUSE
420ret false
421end
422ret true
423end
424
425fn test_10_slot_sha512 to bool
426img = new uchar 64 0
427for i ulong 64
428put img i uchar add 0x41 and i 0x1f
429end
430dig = new uchar 64 0
431dlen = sha2_digest 512 img dig
432if ne dlen 64
433ret false
434end
435man = new uchar 4096 0
436mk_valid_manifest man 3 2 64
437for i ulong 64
438put man add SLOT_M_OFF_IMGSHA i get dig i
439put man add SLOT_M_OFF_KSHA i get dig i
440end
441ev = new SlotEval 0 0 0 0
442if not slot_verify_image_sha512 man img 4096 ev
443ret false
444end
445if or ne get ev valid 1 ne get ev generation 3
446ret false
447end
448note Corrupt one image byte -> verify fails
449put img 10 xor get img 10 1
450if slot_verify_image_sha512 man img 4096 ev
451ret false
452end
453if ne get ev err SLOT_ERR_SHA_MISMATCH
454ret false
455end
456ret true
457end
458
459fn nb_rt_offer_done nonce view uchar 16 mac view uchar 6 to bool
460wire = new uchar 1056 0
461m2 = new NbMsg 0 0 0 0 0 0 0
462rn = new uchar 16 0
463extra = new uchar 1024 0
464m3 = new NbMsg NB_OFFER 0 0 0 0 8192 0
465n3 = nb_build m3 nonce mac wire
466if ne n3 NB_MIN
467ret false
468end
469if ne nb_parse part wire 0 n3 m2 rn extra NB_OK
470ret false
471end
472if or ne get m2 mtype NB_OFFER ne get m2 total 8192
473ret false
474end
475m4 = new NbMsg NB_DONE 0 0 0 11 0 0
476n4 = nb_build m4 nonce b_nb_done wire
477if ne n4 add NB_MIN 11
478ret false
479end
480if ne nb_parse part wire 0 n4 m2 rn extra NB_OK
481ret false
482end
483if ne get m2 mtype NB_DONE
484ret false
485end
486if ne get m2 mlen 11
487ret false
488end
489ret boot_eq_bytes part extra 0 11 b_nb_done
490end
491
492fn test_11_netboot_roundtrip to bool
493nonce = new uchar 16 0
494for i ulong 16
495put nonce i uchar add 0xa0 i
496end
497wire = new uchar 1056 0
498mac = new uchar 6 0
499put mac 0 0x02
500put mac 5 0x77
501m1 = new NbMsg NB_HELLO 0 0 0 0 0 0
502n1 = nb_build m1 nonce mac wire
503if ne n1 NB_MIN
504ret false
505end
506m2 = new NbMsg 0 0 0 0 0 0 0
507rn = new uchar 16 0
508extra = new uchar 1024 0
509if ne nb_parse part wire 0 n1 m2 rn extra NB_OK
510ret false
511end
512if ne get m2 mtype NB_HELLO
513ret false
514end
515if or ne get extra 0 0x02 ne get extra 5 0x77
516ret false
517end
518ret nb_rt_offer_done nonce mac
519end
520
521fn test_12_netboot_negatives to bool
522if ne nb_chunks 4097 5
523ret false
524end
525if ne nb_chunks 4096 4
526ret false
527end
528nonce = new uchar 16 1
529wire = new uchar 1056 0
530buf = new uchar 16 0x41
531note OFFER below NB_OBJECT_MIN refused
532mbad = new NbMsg NB_OFFER 0 0 0 0 4096 0
533if ne nb_build mbad nonce buf wire 0
534ret false
535end
536note GET with unaligned offset refused
537mget = new NbMsg NB_GET 0 0 4 0 0 512
538if ne nb_build mget nonce buf wire 0
539ret false
540end
541note DONE with non-printable byte refused
542bad_txt = new uchar 4 0x41
543put bad_txt 2 0x01
544mdone = new NbMsg NB_DONE 0 0 0 4 0 0
545if ne nb_build mdone nonce bad_txt wire 0
546ret false
547end
548note Valid GET then corrupt reserved byte -> parse refused
549mget_ok = new NbMsg NB_GET 0 0 4 0 0 1024
550n = nb_build mget_ok nonce buf wire
551if ne n NB_MIN
552ret false
553end
554put wire 30 1
555m2 = new NbMsg 0 0 0 0 0 0 0
556rn = new uchar 16 0
557extra = new uchar 1024 0
558if ne nb_parse part wire 0 n m2 rn extra NB_ERR
559ret false
560end
561ret true
562end
563
564fn main to int
565failed = uint 0
566failed = add failed breport b_t01 test_01_handoff_magic
567failed = add failed breport b_t02 test_02_handoff_model_fb
568failed = add failed breport b_t03 test_03_handoff_mmap_rsvd
569failed = add failed breport b_t04 test_04_model_map
570failed = add failed breport b_t05 test_05_slot_struct_checks
571failed = add failed breport b_t06 test_06_slot_select_cand
572failed = add failed breport b_t07 test_07_slot_select_fallback
573failed = add failed breport b_t08 test_08_slot_transitions
574failed = add failed breport b_t09 test_09_boot_rec_and_action
575failed = add failed breport b_t10 test_10_slot_sha512
576failed = add failed breport b_t11 test_11_netboot_roundtrip
577failed = add failed breport b_t12 test_12_netboot_negatives
578if eq failed 0
579w = bwrite_all 1 b_sum
580if w
581ret 0
582end
583ret 1
584end
585w = bwrite_all 1 b_fsum
586if w
587ret 1
588end
589ret 1
590end