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