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 / crates / aienos-boot / boot_crate.om

1384 lines · 28594 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/crates/aienos-boot/boot_crate.om: Pure-Omega port of the aienos-boot crate.
3note Converted from aienux crates/aienos-boot/src/{lib,main,handoff,nvme_read,rollback_mock,store_qual,usb_keyboard,wcslen}.rs.
4note Library unit: no om2 header, no fn main, no globals.
5note Compose with: si/directives/om2.om om/tls/sha2.om aienux/crates/aienos-boot/boot_crate.om ...
6
7note === Constants: lib.rs & main.rs ===
8const BC_CREATES_AGENT_IDENTITY ulong 0
9
10text bc_firmware_banner "AIENOS firmware entry: aarch64"
11text bc_firmware_services_line "firmware services: active"
12text bc_kernel_handoff_line "kernel handoff: not implemented"
13
14note === Constants: rollback_mock.rs ===
15const BC_ROLE_STAGER ulong 0
16const BC_ROLE_CANDIDATE ulong 1
17const BC_ROLE_DEFAULT_OS ulong 2
18
19const BC_MODE_NORMAL ulong 0
20const BC_MODE_FAULT ulong 1
21const BC_MODE_TIMEOUT ulong 2
22const BC_MODE_ABSENT ulong 3
23const BC_MODE_MALFORMED ulong 4
24const BC_MODE_REAL_AIENOS ulong 5
25
26const BC_DP_TYPE_MEDIA uchar 0x04
27const BC_DP_SUBTYPE_FILE_PATH uchar 0x04
28const BC_DP_TYPE_END uchar 0x7f
29const BC_DP_SUBTYPE_END_ENTIRE uchar 0xff
30const BC_LOAD_OPTION_ACTIVE uint 1
31
32const BC_BOOT_CANDIDATE ulong 0x0000
33const BC_BOOT_DEFAULT ulong 0x0001
34const BC_BOOT_INVALID ulong 0xffff
35
36const BC_RB_BOOT_ORDER_0 ulong 0
37const BC_RB_BOOT_ORDER_VALID ulong 1
38const BC_RB_BOOT_NEXT ulong 2
39const BC_RB_BOOT_NEXT_PRESENT ulong 3
40const BC_RB_BOOT_CURRENT ulong 4
41const BC_RB_CYCLE ulong 5
42const BC_RB_BOOT0000_SET ulong 6
43const BC_RB_BOOT0001_SET ulong 7
44const BC_RB_INVARIANT_MASK ulong 8
45
46const BC_RB_ACTION_COLD_RESET_OK ulong 1
47const BC_RB_ACTION_COLD_RESET_ABORT ulong 2
48const BC_RB_ACTION_HANG ulong 3
49const BC_RB_ACTION_COLD_RESET_REPEAT ulong 4
50const BC_RB_ACTION_SHUTDOWN_PASS ulong 5
51const BC_RB_ACTION_UNSUPPORTED ulong 6
52const BC_RB_ACTION_FAIL ulong 7
53
54text bc_rb_str_default "default"
55text bc_rb_str_candidate "candidate"
56text bc_rb_str_fault "fault"
57text bc_rb_str_timeout "timeout"
58text bc_rb_str_absent "absent"
59text bc_rb_str_malformed "malformed"
60text bc_rb_str_real_aienos "real_aienos"
61text bc_rb_path_default "\\EFI\\DEFAULT\\default.efi"
62text bc_rb_path_candidate "\\EFI\\AIENOS\\candidate.efi"
63text bc_rb_path_absent "\\EFI\\AIENOS\\missing_candidate.efi"
64text bc_rb_path_real_aienos "\\EFI\\AIENOS\\aienos-handoff.efi"
65text bc_rb_desc_default "Default Linux OS"
66text bc_rb_desc_candidate "AIENOS Candidate"
67
68note === Constants: store_qual.rs ===
69const BC_STORE_BASE_LBA ulong 64
70const BC_STORE_REGION_BLOCKS ulong 256
71const BC_CONFIG_LBA ulong 32
72const BC_MODE_VERIFY uchar 2
73
74const BC_MODE_RUN_512B uchar 3
75const BC_MODE_VERIFY_512B uchar 4
76const BC_STORE_BASE_LBA_512 ulong 512
77const BC_STORE_REGION_BLOCKS_512 ulong 2048
78const BC_CONFIG_LBA_512 ulong 256
79
80const BC_MODE_CONT_PROVISION uchar 5
81const BC_MODE_CONT_RESUME uchar 6
82const BC_MODE_CONT_REMEMBER uchar 7
83const BC_MODE_CONT_CRASH uchar 8
84
85const BC_MODE_RECOVERY_INSPECT uchar 9
86const BC_MODE_RECOVERY_REPAIR uchar 10
87const BC_MODE_RECOVERY_PROVISION uchar 11
88
89const BC_STORE_UNIT_BYTES ulong 4096
90const BC_TEST_ONLY_OPERATOR_KEY_BYTE uchar 0x0f
91const BC_QUAL_STORE_UUID_BYTE uchar 0x5a
92
93const BC_BLK_OK int 0
94const BC_BLK_ERR_INVALID_INPUT int -1
95const BC_BLK_ERR_OUT_OF_RANGE int -2
96
97const BC_QUAL_OK int 0
98const BC_QUAL_ERR_BLOCK_SIZE int -1
99const BC_QUAL_ERR_512_NEEDS_512 int -2
100const BC_QUAL_ERR_4096_REQUIRED int -3
101const BC_QUAL_ERR_NOT_ATOMIC int -4
102const BC_QUAL_ERR_REGION_OOB int -5
103
104note === Constants: nvme_read.rs ===
105const BC_NVME_SENTINEL_LBA ulong 2048
106const BC_NVME_RW_LBA ulong 4096
107const BC_NVME_ARENA_BYTES ulong 524288
108const BC_NVME_CLASS_CODE uint 0x0108
109
110const BC_PCI_BME_BIT ushort 0x0004
111const BC_DMA_GRANT_CONFINED int 1
112const BC_DMA_GRANT_UNSAFE_BYPASS int 2
113const BC_DMA_DENIED_SMMU_NOT_READY int -1
114const BC_DMA_DENIED_NO_SMMU int -2
115
116text bc_nvme_sentinel_magic "AIENOS-NVME-SENT"
117text bc_nvme_rw_magic "AIENOS-NVME-RW01"
118
119note === Constants: usb_keyboard.rs ===
120const BC_XHCI_CLASS_PROGIF uint 0x0c0330
121const BC_KEYBOARD_LISTEN_SECS ulong 30
122const BC_KB_LINE_CAP ulong 128
123
124const BC_KEY_NONE uchar 0
125const BC_KEY_CHAR uchar 1
126const BC_KEY_ENTER uchar 2
127const BC_KEY_BACKSPACE uchar 3
128
129const BC_KB_DONE_EXIT ulong 1
130const BC_KB_DONE_HALTED ulong 2
131const BC_KB_DONE_TIMEOUT ulong 3
132
133text bc_kb_cmd_exit "exit"
134
135note === Constants: handoff.rs ===
136const BC_PT_POOL_PAGES ulong 256
137const BC_POST_EXIT_HEAP_PAGES ulong 1024
138const BC_SMMU_STREAM_ENTRIES uint 4096
139const BC_SMMU_QUEUE_ENTRIES uint 16
140const BC_SMMU_TABLES_ALIGN ulong 262144
141const BC_SMMU_OFF_STREAM ulong 0
142const BC_SMMU_OFF_EVENT ulong 262144
143const BC_SMMU_OFF_CMD ulong 262656
144const BC_SMMU_OFF_CTX ulong 262912
145const BC_SMMU_TOTAL_BYTES ulong 262976
146
147const BC_TTBR_BADDR_MASK ulong 0x0000fffffffffffe
148const BC_GICD_PIDR2 ulong 0xffe8
149const BC_REPORT_VERSION uint 1
150const BC_MAX_VAR_WRITES ulong 3
151const BC_MAX_VAR_BYTES ulong 3072
152const BC_MAX_ROOT_BRIDGES ulong 4
153const BC_MAX_ARTIFACT_CANDIDATES ulong 48
154const BC_MAX_ARTIFACT_FILE_BYTES ulong 655360
155const BC_ARTIFACT_NAME_BYTES ulong 32
156const BC_ARTIFACT_STRIDE ulong 48
157const BC_GB10_VENDOR_DEVICE uint 0x2e1210de
158
159const BC_STAGE_FIRMWARE_ENTRY ulong 0
160const BC_STAGE_GB10_DISCOVERY ulong 1
161const BC_STAGE_ACPI_TOPOLOGY ulong 2
162const BC_STAGE_FRAMEBUFFER_DISCOVERY ulong 3
163const BC_STAGE_PRE_EXIT_SAVED ulong 4
164const BC_STAGE_EXIT_BOOT_SERVICES ulong 5
165const BC_STAGE_VECTORS_INSTALLED ulong 6
166const BC_STAGE_KERNEL_ENTERED ulong 7
167const BC_STAGE_SCREEN_DRAWN ulong 8
168const BC_STAGE_FINAL_SAVED ulong 9
169const BC_STAGE_UART_SENT ulong 10
170const BC_STAGE_COUNTDOWN ulong 11
171const BC_STAGE_COUNT ulong 12
172
173const BC_VAR_ERR_TOO_LARGE int -1
174const BC_VAR_ERR_BUDGET_SPENT int -2
175
176text bc_rsdp_sig "RSD PTR "
177text bc_artifact_suffix ".aien"
178
179note ============================================================================
180note Section 1: Feature policy (lib.rs) & Little-endian / Byte helpers
181note ============================================================================
182
183fn bc_feature_config_valid hw_staging bool unsafe_dma bool unsafe_nvme_dma bool to bool
184if hw_staging
185if unsafe_dma
186ret false
187end
188if unsafe_nvme_dma
189ret false
190end
191end
192ret true
193end
194
195fn bc_get_u16_le buf view uchar * off ulong to ushort
196b0 = ushort get buf off
197b1 = ushort get buf add off 1
198ret or b0 shl b1 8
199end
200
201fn bc_put_u16_le buf edit uchar * off ulong val ushort to void
202put buf off uchar and val 255
203put buf add off 1 uchar and shr val 8 255
204end
205
206fn bc_get_u32_le buf view uchar * off ulong to uint
207b0 = uint get buf off
208b1 = uint get buf add off 1
209b2 = uint get buf add off 2
210b3 = uint get buf add off 3
211t0 = or b0 shl b1 8
212t1 = or shl b2 16 shl b3 24
213ret or t0 t1
214end
215
216fn bc_put_u32_le buf edit uchar * off ulong val uint to void
217put buf off uchar and val 255
218put buf add off 1 uchar and shr val 8 255
219put buf add off 2 uchar and shr val 16 255
220put buf add off 3 uchar and shr val 24 255
221end
222
223fn bc_get_u64_le buf view uchar * off ulong to ulong
224lo = ulong bc_get_u32_le buf off
225hi = ulong bc_get_u32_le buf add off 4
226ret or lo shl hi 32
227end
228
229fn bc_put_u64_le buf edit uchar * off ulong val ulong to void
230bc_put_u32_le buf off uint and val 0xffffffff
231bc_put_u32_le buf add off 4 uint and shr val 32 0xffffffff
232end
233
234fn bc_ascii_lower ch uchar to uchar
235if ge ch 65
236if le ch 90
237ret add ch 32
238end
239end
240ret ch
241end
242
243fn bc_bytes_eq a view uchar * b view uchar * to bool
244na = len a
245if ne na len b
246ret false
247end
248for i ulong na bound 65536
249if ne get a i get b i
250ret false
251end
252end
253ret true
254end
255
256fn bc_bytes_contains hay view uchar * needle view uchar * to bool
257hn = len hay
258nn = len needle
259if eq nn 0
260ret true
261end
262if gt nn hn
263ret false
264end
265limit = add sub hn nn 1
266for i ulong limit bound 65536
267matched = true
268for j ulong nn bound 4096
269if ne get hay add i j get needle j
270matched = false
271break
272end
273end
274if matched
275ret true
276end
277end
278ret false
279end
280
281fn bc_hex_nibble n uchar to uchar
282v = and n 15
283if lt v 10
284ret add v 48
285end
286ret add v 87
287end
288
289fn bc_bytes_to_hex src view uchar * dst edit uchar * to ulong
290n = len src
291need = mul n 2
292if lt len dst need
293ret 0
294end
295for i ulong n bound 4096
296b = get src i
297put dst mul i 2 bc_hex_nibble shr b 4
298put dst add mul i 2 1 bc_hex_nibble and b 15
299end
300ret need
301end
302
303note ============================================================================
304note Section 2: Bounded wcslen (wcslen.rs)
305note ============================================================================
306
307fn bc_wide_len s view ushort * to ulong
308n = len s
309for i ulong n bound 65536
310if eq get s i 0
311ret i
312end
313end
314ret n
315end
316
317note ============================================================================
318note Section 3: Rollback Mock (rollback_mock.rs)
319note ============================================================================
320
321fn bc_dp_node_match_role dp view uchar * off ulong node_len ulong to ulong
322note Extracts low ASCII bytes of UTF-16LE file path node, lowercases, checks default/candidate
323if le node_len 4
324ret BC_ROLE_STAGER
325end
326data_bytes = sub node_len 4
327chars = div data_bytes 2
328if gt chars 128
329chars = ulong 128
330end
331tmp = new uchar 128 0
332for i ulong chars bound 128
333pos = add add off 4 mul i 2
334ch = get dp pos
335put tmp i bc_ascii_lower ch
336end
337if bc_bytes_contains part tmp 0 chars bc_rb_str_default
338ret BC_ROLE_DEFAULT_OS
339end
340if bc_bytes_contains part tmp 0 chars bc_rb_str_candidate
341ret BC_ROLE_CANDIDATE
342end
343ret BC_ROLE_STAGER
344end
345
346fn bc_dp_detect_role dp view uchar * to ulong
347total = len dp
348off = ulong 0
349while lt off total bound 1024
350if lt sub total off 4
351ret BC_ROLE_STAGER
352end
353dtype = get dp off
354stype = get dp add off 1
355nlen = ulong bc_get_u16_le dp add off 2
356if lt nlen 4
357ret BC_ROLE_STAGER
358end
359if gt nlen sub total off
360ret BC_ROLE_STAGER
361end
362if eq dtype BC_DP_TYPE_END
363if eq stype BC_DP_SUBTYPE_END_ENTIRE
364ret BC_ROLE_STAGER
365end
366end
367if eq dtype BC_DP_TYPE_MEDIA
368if eq stype BC_DP_SUBTYPE_FILE_PATH
369r = bc_dp_node_match_role dp off nlen
370if ne r BC_ROLE_STAGER
371ret r
372end
373end
374end
375off = add off nlen
376end
377ret BC_ROLE_STAGER
378end
379
380fn bc_dp_prefix_len dp view uchar * to ulong
381total = len dp
382off = ulong 0
383while lt off total bound 1024
384if lt sub total off 4
385ret 0
386end
387dtype = get dp off
388stype = get dp add off 1
389nlen = ulong bc_get_u16_le dp add off 2
390if lt nlen 4
391ret 0
392end
393if gt nlen sub total off
394ret 0
395end
396if eq dtype BC_DP_TYPE_MEDIA
397if eq stype BC_DP_SUBTYPE_FILE_PATH
398ret off
399end
400end
401if eq dtype BC_DP_TYPE_END
402if eq stype BC_DP_SUBTYPE_END_ENTIRE
403ret off
404end
405end
406off = add off nlen
407end
408ret off
409end
410
411fn bc_rollback_parse_mode buf view uchar * to ulong
412if bc_bytes_contains buf bc_rb_str_fault
413ret BC_MODE_FAULT
414end
415if bc_bytes_contains buf bc_rb_str_timeout
416ret BC_MODE_TIMEOUT
417end
418if bc_bytes_contains buf bc_rb_str_absent
419ret BC_MODE_ABSENT
420end
421if bc_bytes_contains buf bc_rb_str_malformed
422ret BC_MODE_MALFORMED
423end
424if bc_bytes_contains buf bc_rb_str_real_aienos
425ret BC_MODE_REAL_AIENOS
426end
427ret BC_MODE_NORMAL
428end
429
430fn bc_build_file_dp prefix view uchar * path view uchar * dst edit uchar * to ulong
431plen = len prefix
432path_len = len path
433node_len = add 4 mul 2 add path_len 1
434total = add add plen node_len 4
435if gt total len dst
436ret 0
437end
438for i ulong plen bound 4096
439put dst i get prefix i
440end
441pos = plen
442put dst pos BC_DP_TYPE_MEDIA
443put dst add pos 1 BC_DP_SUBTYPE_FILE_PATH
444bc_put_u16_le dst add pos 2 ushort and node_len 65535
445pos = add pos 4
446for i ulong path_len bound 4096
447put dst pos get path i
448put dst add pos 1 0
449pos = add pos 2
450end
451put dst pos 0
452put dst add pos 1 0
453pos = add pos 2
454put dst pos BC_DP_TYPE_END
455put dst add pos 1 BC_DP_SUBTYPE_END_ENTIRE
456bc_put_u16_le dst add pos 2 4
457ret add pos 4
458end
459
460fn bc_build_load_option desc view uchar * full_dp view uchar * dst edit uchar * to ulong
461dlen = len desc
462fplen = len full_dp
463desc_bytes = mul 2 add dlen 1
464total = add add 6 desc_bytes fplen
465if gt total len dst
466ret 0
467end
468bc_put_u32_le dst 0 BC_LOAD_OPTION_ACTIVE
469bc_put_u16_le dst 4 ushort and fplen 65535
470pos = ulong 6
471for i ulong dlen bound 4096
472put dst pos get desc i
473put dst add pos 1 0
474pos = add pos 2
475end
476put dst pos 0
477put dst add pos 1 0
478pos = add pos 2
479for i ulong fplen bound 4096
480put dst pos get full_dp i
481pos = add pos 1
482end
483ret pos
484end
485
486fn bc_rollback_init st edit ulong 9 to void
487for i ulong 9
488put st i 0
489end
490put st BC_RB_BOOT_CURRENT BC_BOOT_INVALID
491end
492
493fn bc_rollback_stager st edit ulong 9 mode ulong prefix_len ulong to ulong
494note mode is recorded by caller when selecting candidate path; stager stages Boot0001/BootOrder/Boot0000/BootNext
495if eq prefix_len 0
496ret BC_RB_ACTION_UNSUPPORTED
497end
498if gt mode BC_MODE_REAL_AIENOS
499ret BC_RB_ACTION_UNSUPPORTED
500end
501put st BC_RB_BOOT0001_SET 1
502put st BC_RB_BOOT_ORDER_0 BC_BOOT_DEFAULT
503put st BC_RB_BOOT_ORDER_VALID 1
504put st BC_RB_BOOT0000_SET 1
505put st BC_RB_BOOT_NEXT BC_BOOT_CANDIDATE
506put st BC_RB_BOOT_NEXT_PRESENT 1
507put st BC_RB_CYCLE 0
508ret BC_RB_ACTION_COLD_RESET_OK
509end
510
511fn bc_rollback_firmware_boot st edit ulong 9 to ulong
512note Simulates UEFI BDS: BootNext is consumed once, otherwise BootOrder[0] boots
513if ne get st BC_RB_BOOT_NEXT_PRESENT 0
514target = get st BC_RB_BOOT_NEXT
515put st BC_RB_BOOT_NEXT_PRESENT 0
516put st BC_RB_BOOT_CURRENT target
517ret target
518end
519if ne get st BC_RB_BOOT_ORDER_VALID 0
520target = get st BC_RB_BOOT_ORDER_0
521put st BC_RB_BOOT_CURRENT target
522ret target
523end
524put st BC_RB_BOOT_CURRENT BC_BOOT_INVALID
525ret BC_BOOT_INVALID
526end
527
528fn bc_rollback_candidate st edit ulong 9 mode ulong to ulong
529note Candidate checks mode and returns reset/hang action
530if eq mode BC_MODE_FAULT
531ret BC_RB_ACTION_COLD_RESET_ABORT
532end
533if eq mode BC_MODE_TIMEOUT
534ret BC_RB_ACTION_HANG
535end
536ret BC_RB_ACTION_COLD_RESET_OK
537end
538
539fn bc_rollback_default st edit ulong 9 to ulong
540next_consumed = eq get st BC_RB_BOOT_NEXT_PRESENT 0
541order_ok = false
542if ne get st BC_RB_BOOT_ORDER_VALID 0
543if eq get st BC_RB_BOOT_ORDER_0 BC_BOOT_DEFAULT
544order_ok = true
545end
546end
547mask = ulong 0
548if next_consumed
549mask = or mask 1
550end
551if order_ok
552mask = or mask 2
553end
554put st BC_RB_INVARIANT_MASK mask
555cycle = get st BC_RB_CYCLE
556if eq cycle 0
557put st BC_RB_CYCLE 1
558ret BC_RB_ACTION_COLD_RESET_REPEAT
559end
560if eq mask 3
561ret BC_RB_ACTION_SHUTDOWN_PASS
562end
563ret BC_RB_ACTION_FAIL
564end
565
566note ============================================================================
567note Section 4: Store Qualification & Bounded NVMe (store_qual.rs)
568note ============================================================================
569
570fn bc_is_recovery_mode mode uchar to bool
571if eq mode BC_MODE_RECOVERY_INSPECT
572ret true
573end
574if eq mode BC_MODE_RECOVERY_REPAIR
575ret true
576end
577if eq mode BC_MODE_RECOVERY_PROVISION
578ret true
579end
580ret false
581end
582
583fn bc_is_continuity_mode mode uchar to bool
584if eq mode BC_MODE_CONT_PROVISION
585ret true
586end
587if eq mode BC_MODE_CONT_RESUME
588ret true
589end
590if eq mode BC_MODE_CONT_REMEMBER
591ret true
592end
593if eq mode BC_MODE_CONT_CRASH
594ret true
595end
596ret false
597end
598
599fn bc_is_512_qual_mode mode uchar to bool
600if eq mode BC_MODE_RUN_512B
601ret true
602end
603if eq mode BC_MODE_VERIFY_512B
604ret true
605end
606ret false
607end
608
609fn bc_store_config_lba block_size uint to ulong
610if eq block_size 512
611ret BC_CONFIG_LBA_512
612end
613if eq block_size 4096
614ret BC_CONFIG_LBA
615end
616ret 0
617end
618
619fn bc_store_region_base is_512 bool to ulong
620if is_512
621ret BC_STORE_BASE_LBA_512
622end
623ret BC_STORE_BASE_LBA
624end
625
626fn bc_store_region_blocks is_512 bool to ulong
627if is_512
628ret BC_STORE_REGION_BLOCKS_512
629end
630ret BC_STORE_REGION_BLOCKS
631end
632
633fn bc_bounded_nvme_init dev edit ulong 3 block_size uint base_lba ulong blocks ulong to void
634put dev 0 ulong block_size
635put dev 1 base_lba
636put dev 2 blocks
637end
638
639fn bc_bounded_nvme_translate dev view ulong 3 lba ulong buf_len ulong dst_lba edit ulong 1 to int
640bs = get dev 0
641base_lba = get dev 1
642blocks = get dev 2
643if eq bs 0
644ret BC_BLK_ERR_INVALID_INPUT
645end
646if ne rem buf_len bs 0
647ret BC_BLK_ERR_INVALID_INPUT
648end
649cnt = div buf_len bs
650if gt lba blocks
651ret BC_BLK_ERR_OUT_OF_RANGE
652end
653if gt cnt sub blocks lba
654ret BC_BLK_ERR_OUT_OF_RANGE
655end
656put dst_lba 0 add base_lba lba
657ret BC_BLK_OK
658end
659
660fn bc_store_qual_check_geometry block_size uint mode uchar block_count ulong atomic_ok bool to int
661if ne block_size 512
662if ne block_size 4096
663ret BC_QUAL_ERR_BLOCK_SIZE
664end
665end
666is_512 = bc_is_512_qual_mode mode
667if is_512
668if ne block_size 512
669ret BC_QUAL_ERR_512_NEEDS_512
670end
671else
672if ne block_size 4096
673ret BC_QUAL_ERR_4096_REQUIRED
674end
675if not atomic_ok
676ret BC_QUAL_ERR_NOT_ATOMIC
677end
678end
679base_lba = bc_store_region_base is_512
680region_blocks = bc_store_region_blocks is_512
681if gt base_lba block_count
682ret BC_QUAL_ERR_REGION_OOB
683end
684if gt region_blocks sub block_count base_lba
685ret BC_QUAL_ERR_REGION_OOB
686end
687ret BC_QUAL_OK
688end
689
690fn bc_store_reopen_gen_valid generation ulong settle uchar to bool
691upper = add ulong settle 2
692if lt generation 1
693ret false
694end
695if gt generation upper
696ret false
697end
698ret true
699end
700
701fn bc_store_slot_reuse_proven generation ulong settle uchar to bool
702if ge settle 3
703if ge generation 4
704ret true
705end
706end
707ret false
708end
709
710note ============================================================================
711note Section 5: NVMe Read, DMA Gate & Arena (nvme_read.rs)
712note ============================================================================
713
714fn bc_pci_requester_id bus ulong dev ulong fun ulong to uint
715b = shl and uint bus 255 8
716d = shl and uint dev 31 3
717f = and uint fun 7
718ret or or b d f
719end
720
721fn bc_pci_bme_enabled cmd ushort to bool
722ret ne and cmd BC_PCI_BME_BIT 0
723end
724
725fn bc_pci_without_bme cmd ushort to ushort
726ret and cmd 0xfffb
727end
728
729fn bc_pci_with_bme cmd ushort to ushort
730ret or cmd BC_PCI_BME_BIT
731end
732
733fn bc_dma_grant smmu_ready bool has_smmu bool unsafe_bypass bool to int
734if smmu_ready
735if has_smmu
736ret BC_DMA_GRANT_CONFINED
737end
738end
739if has_smmu
740ret BC_DMA_DENIED_SMMU_NOT_READY
741end
742if unsafe_bypass
743ret BC_DMA_GRANT_UNSAFE_BYPASS
744end
745ret BC_DMA_DENIED_NO_SMMU
746end
747
748fn bc_nvme_decode_bar class_reg uint bar_low uint bar_high uint to ulong
749cc = shr class_reg 16
750if ne cc BC_NVME_CLASS_CODE
751ret 0
752end
753if ne and bar_low 1 0
754ret 0
755end
756lo = ulong and bar_low 0xfffffff0
757if eq and bar_low 7 4
758ret or shl ulong bar_high 32 lo
759end
760ret lo
761end
762
763fn bc_dma_arena_init arena edit ulong 3 base ulong size ulong to void
764put arena 0 base
765put arena 1 base
766put arena 2 add base size
767end
768
769fn bc_dma_arena_alloc arena edit ulong 3 size ulong align ulong dst_phys edit ulong 1 to int
770if eq align 0
771ret -1
772end
773mask = sub align 1
774if ne and align mask 0
775ret -1
776end
777cur = get arena 1
778end_addr = get arena 2
779if gt cur sub 0xffffffffffffffff mask
780ret -1
781end
782aligned = and add cur mask xor mask 0xffffffffffffffff
783if gt aligned end_addr
784ret -1
785end
786if gt size sub end_addr aligned
787ret -1
788end
789put arena 1 add aligned size
790put dst_phys 0 aligned
791ret 0
792end
793
794fn bc_dma_arena_offset arena view ulong 3 phys ulong req_len ulong dst_off edit ulong 1 to int
795base = get arena 0
796end_addr = get arena 2
797if lt phys base
798ret -1
799end
800if ge phys end_addr
801ret -1
802end
803off = sub phys base
804avail = sub end_addr phys
805if gt req_len avail
806ret -1
807end
808put dst_off 0 off
809ret 0
810end
811
812fn bc_nvme_fill_sector_pattern buf edit uchar * magic view uchar * to bool
813if ne len buf 512
814ret false
815end
816if ne len magic 16
817ret false
818end
819for c ulong 32
820base = mul c 16
821for i ulong 16
822put buf add base i get magic i
823end
824end
825ret true
826end
827
828fn bc_nvme_verify_sector_pattern buf view uchar * magic view uchar * to bool
829if ne len buf 512
830ret false
831end
832if ne len magic 16
833ret false
834end
835for c ulong 32
836base = mul c 16
837for i ulong 16
838if ne get buf add base i get magic i
839ret false
840end
841end
842end
843ret true
844end
845
846fn bc_nvme_sector_sha256_hex buf view uchar * hex_dst edit uchar * to ulong
847dig = new uchar 32 0
848dlen = sha2_digest 256 buf dig
849if ne dlen 32
850ret 0
851end
852ret bc_bytes_to_hex dig hex_dst
853end
854
855fn bc_continuity_hash_statement ctx edit ulong * stmt view uchar * to void
856lbuf = new uchar 4 0
857bc_put_u32_le lbuf 0 uint and len stmt 0xffffffff
858sha256_update ctx lbuf
859sha256_update ctx stmt
860end
861
862note ============================================================================
863note Section 6: USB Keyboard & xHCI (usb_keyboard.rs)
864note ============================================================================
865
866fn bc_xhci_mmio_base class_reg uint bar_low uint bar_high uint to ulong
867if ne shr class_reg 8 BC_XHCI_CLASS_PROGIF
868ret 0
869end
870if ne and bar_low 1 0
871ret 0
872end
873lo = ulong and bar_low 0xfffffff0
874if eq and bar_low 6 4
875ret or shl ulong bar_high 32 lo
876end
877ret lo
878end
879
880fn bc_hid_digit_ascii code uchar shifted bool to uchar
881note HID usages 0x1e..0x27 -> '1'..'9','0' or '!','@','#','$','%','^','&','*','(',')'
882if not shifted
883if eq code 0x27
884ret 48
885end
886ret add 49 sub code 0x1e
887end
888if eq code 0x1e
889ret 33
890end
891if eq code 0x1f
892ret 64
893end
894if eq code 0x20
895ret 35
896end
897if eq code 0x21
898ret 36
899end
900if eq code 0x22
901ret 37
902end
903if eq code 0x23
904ret 94
905end
906if eq code 0x24
907ret 38
908end
909if eq code 0x25
910ret 42
911end
912if eq code 0x26
913ret 40
914end
915ret 41
916end
917
918fn bc_hid_punct_ascii code uchar shifted bool to uchar
919note HID usages 0x2d..0x38
920if eq code 0x2d
921if shifted
922ret 95
923end
924ret 45
925end
926if eq code 0x2e
927if shifted
928ret 43
929end
930ret 61
931end
932if eq code 0x2f
933if shifted
934ret 123
935end
936ret 91
937end
938if eq code 0x30
939if shifted
940ret 125
941end
942ret 93
943end
944if eq code 0x31
945if shifted
946ret 124
947end
948ret 92
949end
950if eq code 0x33
951if shifted
952ret 58
953end
954ret 59
955end
956if eq code 0x34
957if shifted
958ret 34
959end
960ret 39
961end
962if eq code 0x35
963if shifted
964ret 126
965end
966ret 96
967end
968if eq code 0x36
969if shifted
970ret 60
971end
972ret 44
973end
974if eq code 0x37
975if shifted
976ret 62
977end
978ret 46
979end
980if eq code 0x38
981if shifted
982ret 63
983end
984ret 47
985end
986ret 0
987end
988
989fn bc_hid_keycode_to_ascii code uchar shifted bool to uchar
990if ge code 0x04
991if le code 0x1d
992if shifted
993ret add 65 sub code 0x04
994end
995ret add 97 sub code 0x04
996end
997end
998if ge code 0x1e
999if le code 0x27
1000ret bc_hid_digit_ascii code shifted
1001end
1002end
1003if eq code 0x2c
1004ret 32
1005end
1006if ge code 0x2d
1007if le code 0x38
1008ret bc_hid_punct_ascii code shifted
1009end
1010end
1011ret 0
1012end
1013
1014fn bc_hid_prev_contains prev view uchar 6 code uchar to bool
1015for i ulong 6
1016if eq get prev i code
1017ret true
1018end
1019end
1020ret false
1021end
1022
1023fn bc_hid_decode_report prev edit uchar 6 report view uchar 8 ev_kinds edit uchar 6 ev_chars edit uchar 6 to ulong
1024note Ignore ErrorRollOver (0x01) reports without modifying prev
1025for i ulong 6
1026if eq get report add i 2 0x01
1027ret 0
1028end
1029end
1030mod_byte = get report 0
1031shifted = ne and mod_byte 0x22 0
1032ev_cnt = ulong 0
1033for i ulong 6
1034code = get report add i 2
1035if ne code 0
1036if not bc_hid_prev_contains prev code
1037if eq code 0x28
1038put ev_kinds ev_cnt BC_KEY_ENTER
1039put ev_chars ev_cnt 0
1040ev_cnt = add ev_cnt 1
1041else
1042if eq code 0x2a
1043put ev_kinds ev_cnt BC_KEY_BACKSPACE
1044put ev_chars ev_cnt 0
1045ev_cnt = add ev_cnt 1
1046else
1047ch = bc_hid_keycode_to_ascii code shifted
1048if ne ch 0
1049put ev_kinds ev_cnt BC_KEY_CHAR
1050put ev_chars ev_cnt ch
1051ev_cnt = add ev_cnt 1
1052end
1053end
1054end
1055end
1056end
1057end
1058for i ulong 6
1059put prev i get report add i 2
1060end
1061ret ev_cnt
1062end
1063
1064fn bc_kb_line_apply line edit uchar * line_len edit ulong 1 ev_kind uchar ev_char uchar to ulong
1065cur = get line_len 0
1066if eq ev_kind BC_KEY_ENTER
1067ret 1
1068end
1069if eq ev_kind BC_KEY_BACKSPACE
1070if gt cur 0
1071put line_len 0 sub cur 1
1072end
1073ret 0
1074end
1075if eq ev_kind BC_KEY_CHAR
1076if ge ev_char 0x20
1077if le ev_char 0x7e
1078if lt cur len line
1079put line cur ev_char
1080put line_len 0 add cur 1
1081end
1082end
1083end
1084end
1085ret 0
1086end
1087
1088fn bc_kb_stop_reason exited bool halted bool to ulong
1089if exited
1090ret BC_KB_DONE_EXIT
1091end
1092if halted
1093ret BC_KB_DONE_HALTED
1094end
1095ret BC_KB_DONE_TIMEOUT
1096end
1097
1098note ============================================================================
1099note Section 7: Handoff Logic (handoff.rs)
1100note ============================================================================
1101
1102fn bc_heap_init heap edit ulong 3 base ulong cap ulong to void
1103put heap 0 base
1104put heap 1 0
1105put heap 2 cap
1106end
1107
1108fn bc_heap_alloc heap edit ulong 3 size ulong align ulong dst_addr edit ulong 1 to int
1109base = get heap 0
1110used = get heap 1
1111cap = get heap 2
1112if eq base 0
1113ret -1
1114end
1115if eq align 0
1116ret -1
1117end
1118mask = sub align 1
1119if ne and align mask 0
1120ret -1
1121end
1122if gt used sub 0xffffffffffffffff base
1123ret -1
1124end
1125cur = add base used
1126if gt cur sub 0xffffffffffffffff mask
1127ret -1
1128end
1129aligned = and add cur mask xor mask 0xffffffffffffffff
1130if gt size sub 0xffffffffffffffff aligned
1131ret -1
1132end
1133nxt = sub add aligned size base
1134if gt nxt cap
1135ret -1
1136end
1137put heap 1 nxt
1138put dst_addr 0 aligned
1139ret 0
1140end
1141
1142fn bc_smmu_validate tables_addr ulong stream_id uint to bool
1143if eq tables_addr 0
1144ret false
1145end
1146if ne rem tables_addr BC_SMMU_TABLES_ALIGN 0
1147ret false
1148end
1149if ge stream_id BC_SMMU_STREAM_ENTRIES
1150ret false
1151end
1152ret true
1153end
1154
1155fn bc_mat_header_length hdr view uchar * to ulong
1156if lt len hdr 16
1157ret 0
1158end
1159cnt = ulong bc_get_u32_le hdr 4
1160stride = ulong bc_get_u32_le hdr 8
1161if gt cnt 1048576
1162ret 0
1163end
1164if gt stride 1048576
1165ret 0
1166end
1167prod = mul cnt stride
1168if gt prod 1048560
1169ret 0
1170end
1171ret add 16 prod
1172end
1173
1174fn bc_page_align_range base ulong size ulong dst edit ulong 2 to bool
1175page_off = and base 4095
1176start = sub base page_off
1177if gt size sub 0xffffffffffffffff page_off
1178ret false
1179end
1180span = add size page_off
1181if gt span 0xfffffffffffff000
1182ret false
1183end
1184aligned_len = and add span 4095 xor ulong 4095 0xffffffffffffffff
1185put dst 0 start
1186put dst 1 aligned_len
1187ret true
1188end
1189
1190fn bc_mmu_switched el ulong mmu_on bool ttbr0_el1 ulong fw_ttbr0 ulong kroot ulong to bool
1191if ne el 1
1192ret false
1193end
1194if not mmu_on
1195ret false
1196end
1197el1_base = and ttbr0_el1 BC_TTBR_BADDR_MASK
1198fw_base = and fw_ttbr0 BC_TTBR_BADDR_MASK
1199if ne el1_base kroot
1200ret false
1201end
1202if eq el1_base fw_base
1203ret false
1204end
1205ret true
1206end
1207
1208fn bc_timer_ticks_to_us counter ulong freq_hz ulong to ulong
1209if eq freq_hz 0
1210ret 0
1211end
1212q = div counter freq_hz
1213r = rem counter freq_hz
1214note Compute (q * 1000000) + (r * 1000000) / freq_hz without u64 overflow for normal rates
1215ret add mul q 1000000 div mul r 1000000 freq_hz
1216end
1217
1218fn bc_var_budget_init b edit ulong 3 max_writes ulong max_bytes ulong to void
1219put b 0 0
1220put b 1 max_writes
1221put b 2 max_bytes
1222end
1223
1224fn bc_var_budget_try_write b edit ulong 3 msg_len ulong to int
1225if gt msg_len get b 2
1226ret BC_VAR_ERR_TOO_LARGE
1227end
1228n = get b 0
1229put b 0 add n 1
1230if ge n get b 1
1231ret BC_VAR_ERR_BUDGET_SPENT
1232end
1233ret int add n 1
1234end
1235
1236fn bc_artifact_name_valid wide view ushort * dst edit uchar * to ulong
1237wlen = bc_wide_len wide
1238if le wlen 5
1239ret 0
1240end
1241if gt wlen BC_ARTIFACT_NAME_BYTES
1242ret 0
1243end
1244if lt len dst wlen
1245ret 0
1246end
1247for i ulong wlen bound 32
1248u = get wide i
1249if lt u 0x21
1250ret 0
1251end
1252if gt u 0x7e
1253ret 0
1254end
1255put dst i uchar and u 255
1256end
1257tail = sub wlen 5
1258for i ulong 5
1259c1 = bc_ascii_lower get dst add tail i
1260c2 = get bc_artifact_suffix i
1261if ne c1 c2
1262ret 0
1263end
1264end
1265ret wlen
1266end
1267
1268fn bc_artifact_oversize size ulong to bool
1269ret gt size BC_MAX_ARTIFACT_FILE_BYTES
1270end
1271
1272fn bc_artifact_name_lt a view uchar * entries view uchar * idx ulong to bool
1273a_len = len a
1274off = mul idx BC_ARTIFACT_STRIDE
1275b_len = bc_get_u64_le entries add off 32
1276n = a_len
1277if lt b_len n
1278n = b_len
1279end
1280for i ulong n bound 32
1281ca = get a i
1282cb = get entries add off i
1283if lt ca cb
1284ret true
1285end
1286if gt ca cb
1287ret false
1288end
1289end
1290ret lt a_len b_len
1291end
1292
1293fn bc_artifact_copy_entry entries edit uchar * dst_idx ulong src_idx ulong to void
1294doff = mul dst_idx BC_ARTIFACT_STRIDE
1295soff = mul src_idx BC_ARTIFACT_STRIDE
1296for i ulong BC_ARTIFACT_STRIDE
1297put entries add doff i get entries add soff i
1298end
1299end
1300
1301fn bc_artifact_write_entry entries edit uchar * idx ulong name view uchar * size ulong to void
1302off = mul idx BC_ARTIFACT_STRIDE
1303nlen = len name
1304for i ulong 32
1305if lt i nlen
1306put entries add off i get name i
1307else
1308put entries add off i 0
1309end
1310end
1311bc_put_u64_le entries add off 32 nlen
1312bc_put_u64_le entries add off 40 size
1313end
1314
1315fn bc_artifact_insert_sorted entries edit uchar * meta edit ulong 2 new_name view uchar * new_size ulong to ulong
1316note entries holds up to 48 records of 48 bytes (2304 bytes total); meta[0]=count, meta[1]=ignored
1317cnt = get meta 0
1318ign = get meta 1
1319ins_pos = cnt
1320for i ulong cnt bound 48
1321if bc_artifact_name_lt new_name entries i
1322ins_pos = i
1323break
1324end
1325end
1326if eq ins_pos BC_MAX_ARTIFACT_CANDIDATES
1327put meta 1 add ign 1
1328ret cnt
1329end
1330if eq cnt BC_MAX_ARTIFACT_CANDIDATES
1331put meta 1 add ign 1
1332else
1333cnt = add cnt 1
1334put meta 0 cnt
1335end
1336if gt cnt add ins_pos 1
1337steps = sub sub cnt 1 ins_pos
1338for s ulong steps bound 48
1339dst_idx = sub sub cnt 1 s
1340bc_artifact_copy_entry entries dst_idx sub dst_idx 1
1341end
1342end
1343bc_artifact_write_entry entries ins_pos new_name new_size
1344ret cnt
1345end
1346
1347fn bc_rsdp_xsdt_addr rsdp view uchar * to ulong
1348if lt len rsdp 36
1349ret 0
1350end
1351for i ulong 8
1352if ne get rsdp i get bc_rsdp_sig i
1353ret 0
1354end
1355end
1356ret bc_get_u64_le rsdp 24
1357end
1358
1359fn bc_stage_valid s ulong to bool
1360ret lt s BC_STAGE_COUNT
1361end
1362
1363fn bc_el0_report_ok write_ok bool forged_denied bool fault_contained bool exit_code int to bool
1364if not write_ok
1365ret false
1366end
1367if not forged_denied
1368ret false
1369end
1370if not fault_contained
1371ret false
1372end
1373ret eq exit_code 0
1374end
1375
1376fn bc_preempt_ok a ulong b ulong switches ulong to bool
1377if eq a 0
1378ret false
1379end
1380if eq b 0
1381ret false
1382end
1383ret ge switches 4
1384end