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 / kernel / hal / nvme_store_gate.om

530 lines · 12213 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/kernel/hal/nvme_store_gate.om: Pure-Omega GPT/AIENOS partition discovery,
3note single-layer NVMe write confinement, Store key admissibility, and single-transaction
4note continuity subject provisioning gate.
5note Converted from aienux native/kernel/dev/{disk_part,nvme_bind}.{h,c} and
6note native/kernel/svc/{store_boot,continuity_subject_provision}.{h,c}.
7note Compose with: si/directives/om2.om aienux/kernel/hal/iort.om aienux/kernel/hal/nvme_store_gate.om ...
8
9const HAL_GPT_OK int 0
10const HAL_GPT_EGEOM int -402
11const HAL_GPT_ENOMBR int -403
12const HAL_GPT_ESIG int -404
13const HAL_GPT_EHDR_CRC int -405
14const HAL_GPT_EHDR int -406
15const HAL_GPT_EENT_CRC int -407
16const HAL_GPT_EENT int -408
17const HAL_GPT_EBACKUP int -409
18const HAL_GPT_EOUTSIDE int -410
19const HAL_GPT_EOVERLAP int -411
20const HAL_GPT_ENOPART int -412
21const HAL_GPT_EMULTI int -413
22const HAL_GPT_ETOOMANY int -414
23
24const HAL_DISK_OK int 0
25const HAL_DISK_ESTATE int -201
26const HAL_DISK_EARG int -202
27const HAL_DISK_ERANGE int -203
28
29const HAL_NVME_OK int 0
30const HAL_NVME_ESTATE int -301
31const HAL_NVME_ESTUCK int -302
32const HAL_NVME_EUNCONFINED int -303
33
34const HAL_SB_OK int 0
35const HAL_SB_E_TEST_KEYS int -2001
36const HAL_SB_E_BLOCKED_OPERATOR int -2002
37const HAL_SB_E_KEYSLOT_BAD int -2006
38
39const HAL_CR_RESOLVED int 0
40const HAL_CR_E_ARG int -501
41const HAL_CR_CORRUPT int -502
42const HAL_CR_ALREADY_PROVISIONED int -503
43const HAL_CR_LIMIT int -504
44const HAL_CR_SPLIT_TXN int -505
45
46const HAL_KEY_TEST ulong 0
47const HAL_KEY_PROD ulong 1
48const HAL_ORIGIN_OPERATOR uchar 1
49const HAL_ORIGIN_PROMOTED uchar 2
50
51note Partition state: 0=valid, 1=first_lba, 2=blocks, 3=parent_blocks, 4=used, 5=part_index,
52note 6=first_usable, 7=last_usable
53global hal_part ulong 8
54global hal_used_lo ulong 128
55global hal_used_hi ulong 128
56
57note NVMe bind state: 0=claimed, 1=stuck, 2=confined, 3=stream_id, 4=seg, 5=rid
58global hal_nvme ulong 6
59
60note Provisioning state: 0=txn_count, 1=committed_objs, 2=subject_seq, 3=provisioned
61global hal_prov ulong 4
62
63note Test-only mutant hooks (default 0): 0=bypass_xlate, 1=split_txn
64global hal_mut ulong 2
65
66fn hal_set_mutants bypass_xlate ulong split_txn ulong to void
67put hal_mut 0 bypass_xlate
68put hal_mut 1 split_txn
69end
70
71fn hal_rd32 buf view uchar * off ulong to uint
72b0 = uint get buf off
73b1 = uint get buf add off 1
74b2 = uint get buf add off 2
75b3 = uint get buf add off 3
76lo = or b0 shl b1 8
77hi = or shl b2 16 shl b3 24
78ret or lo hi
79end
80
81fn hal_rd64 buf view uchar * off ulong to ulong
82lo = ulong hal_rd32 buf off
83hi = ulong hal_rd32 buf add off 4
84ret or lo shl hi 32
85end
86
87note IEEE 802.3 CRC32 (reflected 0xedb88320, init/xorout 0xffffffff).
88fn hal_crc32_slice buf view uchar * off ulong n ulong to uint
89crc = uint 0xffffffff
90i = ulong 0
91note Covers GPT headers (92..512 B) and entry arrays (up to 16384 B).
92while lt i n bound 65536
93b = uint get buf add off i
94crc = xor crc b
95for bit ulong 8
96if ne and crc 1 0
97crc = xor shr crc 1 0xedb88320
98else
99crc = shr crc 1
100end
101end
102i = add i 1
103end
104ret xor crc 0xffffffff
105end
106
107note CRC32 of a GPT header with the 4-byte HeaderCRC32 field at +16 treated as zero.
108fn hal_gpt_hdr_crc buf view uchar * off ulong hsize ulong to uint
109crc = uint 0xffffffff
110i = ulong 0
111while lt i hsize bound 4096
112b = uint 0
113if or lt i 16 ge i 20
114b = uint get buf add off i
115end
116crc = xor crc b
117for bit ulong 8
118if ne and crc 1 0
119crc = xor shr crc 1 0xedb88320
120else
121crc = shr crc 1
122end
123end
124i = add i 1
125end
126ret xor crc 0xffffffff
127end
128
129fn hal_part_reset to void
130for i ulong 8
131put hal_part i 0
132end
133end
134
135fn hal_is_gpt_sig buf view uchar * off ulong to bool
136note "EFI PART" = 69 70 73 32 80 65 82 84
137if or ne get buf off 69 ne get buf add off 1 70
138ret false
139end
140if or ne get buf add off 2 73 ne get buf add off 3 32
141ret false
142end
143if or ne get buf add off 4 80 ne get buf add off 5 65
144ret false
145end
146if or ne get buf add off 6 82 ne get buf add off 7 84
147ret false
148end
149ret true
150end
151
152fn hal_guid_is_zero buf view uchar * off ulong to bool
153i = ulong 0
154while lt i 16 bound 16
155if ne get buf add off i 0
156ret false
157end
158i = add i 1
159end
160ret true
161end
162
163fn hal_guid_eq_same buf view uchar * aoff ulong boff ulong to bool
164i = ulong 0
165while lt i 16 bound 16
166if ne get buf add aoff i get buf add boff i
167ret false
168end
169i = add i 1
170end
171ret true
172end
173
174fn hal_guid_eq_buf a view uchar * aoff ulong b view uchar * boff ulong to bool
175i = ulong 0
176while lt i 16 bound 16
177if ne get a add aoff i get b add boff i
178ret false
179end
180i = add i 1
181end
182ret true
183end
184
185fn hal_check_pmbr disk view uchar * to bool
186if or ne get disk 510 0x55 ne get disk 511 0xaa
187ret false
188end
189found_ee = false
190for p ulong 4
191ptype = get disk add add 446 mul p 16 4
192if eq ptype 0xee
193found_ee = true
194end
195end
196ret found_ee
197end
198
199fn hal_used_overlaps flba ulong llba ulong used ulong to bool
200j = ulong 0
201while lt j used bound 128
202ulo = get hal_used_lo j
203uhi = get hal_used_hi j
204if and le flba uhi le ulo llba
205ret true
206end
207j = add j 1
208end
209ret false
210end
211
212fn hal_scan_one_entry disk view uchar * cur ulong idx ulong want view uchar 16 to int
213if hal_guid_is_zero disk cur
214ret 0
215end
216used = get hal_part 4
217if ge used 128
218ret HAL_GPT_ETOOMANY
219end
220flba = hal_rd64 disk add cur 32
221llba = hal_rd64 disk add cur 40
222if or gt flba llba lt flba get hal_part 6
223ret HAL_GPT_EOUTSIDE
224end
225if gt llba get hal_part 7
226ret HAL_GPT_EOUTSIDE
227end
228if hal_used_overlaps flba llba used
229ret HAL_GPT_EOVERLAP
230end
231put hal_used_lo used flba
232put hal_used_hi used llba
233put hal_part 4 add used 1
234if hal_guid_eq_buf disk cur want 0
235put hal_part 1 flba
236put hal_part 2 add sub llba flba 1
237put hal_part 5 idx
238ret 1
239end
240ret 0
241end
242
243fn hal_scan_gpt_entries disk view uchar * eoff ulong entries ulong esize ulong want view uchar 16 to int
244matches = ulong 0
245i = ulong 0
246while lt i entries bound 1024
247cur = add eoff mul i esize
248rc = hal_scan_one_entry disk cur i want
249if lt rc 0
250ret rc
251end
252if eq rc 1
253matches = add matches 1
254end
255i = add i 1
256end
257if eq matches 0
258ret HAL_GPT_ENOPART
259end
260if gt matches 1
261ret HAL_GPT_EMULTI
262end
263put hal_part 0 1
264ret HAL_GPT_OK
265end
266
267fn hal_gpt_check_primary_ent disk view uchar * bsz ulong to int
268entries = ulong hal_rd32 disk add bsz 80
269esize = ulong hal_rd32 disk add bsz 84
270if or eq entries 0 gt entries 1024
271ret HAL_GPT_EENT
272end
273if or lt esize 128 gt esize 512
274ret HAL_GPT_EENT
275end
276pe_lba = hal_rd64 disk add bsz 72
277ebytes = mul entries esize
278if lt pe_lba 2
279ret HAL_GPT_EENT
280end
281if gt add mul pe_lba bsz ebytes len disk
282ret HAL_GPT_EENT
283end
284if ne hal_crc32_slice disk mul pe_lba bsz ebytes hal_rd32 disk add bsz 88
285ret HAL_GPT_EENT_CRC
286end
287ret HAL_GPT_OK
288end
289
290fn hal_gpt_check_primary disk view uchar * bsz ulong last_lba ulong to int
291if not hal_is_gpt_sig disk bsz
292ret HAL_GPT_ESIG
293end
294rev = hal_rd32 disk add bsz 8
295hsz = ulong hal_rd32 disk add bsz 12
296if ne rev 0x00010000
297ret HAL_GPT_ESIG
298end
299if or lt hsz 92 gt hsz bsz
300ret HAL_GPT_ESIG
301end
302if ne hal_gpt_hdr_crc disk bsz hsz hal_rd32 disk add bsz 16
303ret HAL_GPT_EHDR_CRC
304end
305if or ne hal_rd64 disk add bsz 24 1 ne hal_rd64 disk add bsz 32 last_lba
306ret HAL_GPT_EHDR
307end
308first_u = hal_rd64 disk add bsz 40
309last_u = hal_rd64 disk add bsz 48
310if or gt first_u last_u ge last_u last_lba
311ret HAL_GPT_EHDR
312end
313put hal_part 6 first_u
314put hal_part 7 last_u
315ret hal_gpt_check_primary_ent disk bsz
316end
317
318fn hal_gpt_check_backup_ent disk view uchar * bsz ulong b1 ulong to int
319entries = ulong hal_rd32 disk add bsz 80
320esize = ulong hal_rd32 disk add bsz 84
321ecrc = hal_rd32 disk add bsz 88
322if or ne ulong hal_rd32 disk add b1 80 entries ne ulong hal_rd32 disk add b1 84 esize
323ret HAL_GPT_EBACKUP
324end
325if ne hal_rd32 disk add b1 88 ecrc
326ret HAL_GPT_EBACKUP
327end
328be_lba = hal_rd64 disk add b1 72
329ebytes = mul entries esize
330if le be_lba get hal_part 7
331ret HAL_GPT_EBACKUP
332end
333if gt add mul be_lba bsz ebytes b1
334ret HAL_GPT_EBACKUP
335end
336if ne hal_crc32_slice disk mul be_lba bsz ebytes ecrc
337ret HAL_GPT_EBACKUP
338end
339ret HAL_GPT_OK
340end
341
342fn hal_gpt_check_backup disk view uchar * bsz ulong last_lba ulong to int
343b1 = mul last_lba bsz
344if not hal_is_gpt_sig disk b1
345ret HAL_GPT_EBACKUP
346end
347hsz = ulong hal_rd32 disk add bsz 12
348if or ne hal_rd32 disk add b1 8 0x00010000 ne ulong hal_rd32 disk add b1 12 hsz
349ret HAL_GPT_EBACKUP
350end
351if ne hal_gpt_hdr_crc disk b1 hsz hal_rd32 disk add b1 16
352ret HAL_GPT_EBACKUP
353end
354if or ne hal_rd64 disk add b1 24 last_lba ne hal_rd64 disk add b1 32 1
355ret HAL_GPT_EBACKUP
356end
357if not hal_guid_eq_same disk add bsz 56 add b1 56
358ret HAL_GPT_EBACKUP
359end
360ret hal_gpt_check_backup_ent disk bsz b1
361end
362
363note Parse and strictly validate protective MBR + primary/backup GPT + single target partition.
364fn hal_gpt_find_type disk view uchar * bsz ulong bcnt ulong want view uchar 16 to int
365hal_part_reset
366if or lt bsz 512 or gt bsz 4096 lt bcnt 6
367ret HAL_GPT_EGEOM
368end
369if gt mul bcnt bsz len disk
370ret HAL_GPT_EGEOM
371end
372if not hal_check_pmbr disk
373ret HAL_GPT_ENOMBR
374end
375last_lba = sub bcnt 1
376rc1 = hal_gpt_check_primary disk bsz last_lba
377if ne rc1 HAL_GPT_OK
378ret rc1
379end
380rc2 = hal_gpt_check_backup disk bsz last_lba
381if ne rc2 HAL_GPT_OK
382ret rc2
383end
384put hal_part 3 bcnt
385pe_lba = hal_rd64 disk add bsz 72
386entries = ulong hal_rd32 disk add bsz 80
387esize = ulong hal_rd32 disk add bsz 84
388ret hal_scan_gpt_entries disk mul pe_lba bsz entries esize want
389end
390
391note Single-layer partition translation (ck_part_xlate): partition-relative -> absolute LBA.
392fn hal_part_xlate lba ulong count uint abs_out edit ulong 1 to int
393if ne get hal_mut 0 0
394note Mutant: bypass partition translation and bounds check
395put abs_out 0 lba
396ret HAL_DISK_OK
397end
398if eq get hal_part 0 0
399ret HAL_DISK_ESTATE
400end
401if eq count 0
402ret HAL_DISK_EARG
403end
404pblocks = get hal_part 2
405if ge lba pblocks
406ret HAL_DISK_ERANGE
407end
408cnt64 = ulong count
409if gt cnt64 sub pblocks lba
410ret HAL_DISK_ERANGE
411end
412flba = get hal_part 1
413abs = add flba lba
414if lt abs flba
415ret HAL_DISK_ERANGE
416end
417parent_b = get hal_part 3
418if gt add abs cnt64 parent_b
419ret HAL_DISK_ERANGE
420end
421put abs_out 0 abs
422ret HAL_DISK_OK
423end
424
425note ---- NVMe ownership & DMA confinement gate ----
426
427fn hal_nvme_reset to void
428for i ulong 6
429put hal_nvme i 0
430end
431end
432
433fn hal_nvme_claim seg uint rid uint smmu_confined ulong stream_id uint bm_sticky ulong to int
434if ne get hal_nvme 0 0
435ret HAL_NVME_ESTATE
436end
437put hal_nvme 0 1
438put hal_nvme 4 ulong seg
439put hal_nvme 5 ulong rid
440if ne bm_sticky 0
441note Fail closed: device stays owned/stuck so no subsequent bind can proceed
442put hal_nvme 1 1
443ret HAL_NVME_ESTUCK
444end
445put hal_nvme 2 smmu_confined
446put hal_nvme 3 ulong stream_id
447ret HAL_NVME_OK
448end
449
450fn hal_nvme_release to int
451if ne get hal_nvme 1 0
452note Stuck device stays owned through release (fail closed)
453ret HAL_NVME_ESTUCK
454end
455hal_nvme_reset
456ret HAL_NVME_OK
457end
458
459fn hal_nvme_authorize_write lba ulong count uint abs_out edit ulong 1 to int
460if or eq get hal_nvme 0 0 ne get hal_nvme 1 0
461ret HAL_NVME_ESTATE
462end
463if eq get hal_nvme 2 0
464ret HAL_NVME_EUNCONFINED
465end
466ret hal_part_xlate lba count abs_out
467end
468
469note ---- Store key admissibility & single-transaction subject provisioning gate ----
470
471fn hal_store_key_admissible key_class ulong hw_staging ulong to int
472if eq key_class HAL_KEY_PROD
473ret HAL_SB_OK
474end
475if eq key_class HAL_KEY_TEST
476if ne hw_staging 0
477ret HAL_SB_E_TEST_KEYS
478end
479ret HAL_SB_OK
480end
481ret HAL_SB_E_KEYSLOT_BAD
482end
483
484fn hal_all_zero32 buf view uchar 32 to bool
485for i ulong 32
486if ne get buf i 0
487ret false
488end
489end
490ret true
491end
492
493fn hal_prov_reset to void
494for i ulong 4
495put hal_prov i 0
496end
497end
498
499note Single-transaction identity + ALLEN genesis subject provisioning gate.
500fn hal_store_provision_gate prov view uchar 32 cortex view uchar 32 origin uchar existing_roots uint existing_subjs uint base_objs uint to int
501hal_prov_reset
502if or hal_all_zero32 prov hal_all_zero32 cortex
503ret HAL_CR_E_ARG
504end
505if and ne origin HAL_ORIGIN_OPERATOR ne origin HAL_ORIGIN_PROMOTED
506ret HAL_CR_E_ARG
507end
508if and gt existing_subjs 0 eq existing_roots 0
509ret HAL_CR_CORRUPT
510end
511if gt existing_roots 0
512ret HAL_CR_ALREADY_PROVISIONED
513end
514if or eq base_objs 0 gt add base_objs 1 16
515ret HAL_CR_LIMIT
516end
517if ne get hal_mut 1 0
518note Mutant: split root and subject across two transactions
519put hal_prov 0 2
520put hal_prov 1 ulong add base_objs 1
521put hal_prov 2 1
522put hal_prov 3 1
523ret HAL_CR_SPLIT_TXN
524end
525put hal_prov 0 1
526put hal_prov 1 ulong add base_objs 1
527put hal_prov 2 1
528put hal_prov 3 1
529ret HAL_CR_RESOLVED
530end