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/disk/ext4_ro.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 / disk / ext4_ro.om` 697 lines ยท 14419 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aienux/disk/ext4_ro.om: Strictly read-only ext4 filesystem reader & GspFwBundle TOC verifier. 3 note Safety invariants enforced (Slice D / Piece 4, #23/#27/#30): 4 note - Zero write paths (read-only sector callback ext4_read_blk only). 5 note - Handles 64-bit block group descriptors (s_desc_size = 32 or 64, bg_inode_table_hi). 6 note - Handles 48-bit extent physical blocks (ee_start_hi, ei_leaf_hi) and depth 0..5 trees. 7 note - Validates extent header magic 0xF30A on every node; fails closed on corruption. 8 note - Zero-fills unwritten extents (ee_len > 32768) and sparse holes. 9 note - Fails closed on inline-data (0x10000000), encrypted (0x800), and non-extent inodes. 10 note - Verifies SHA-256 via sha256_bare.om (compose with sha256_bare.om). 11 note 12 note Platform callbacks required from caller: 13 note ext4_read_lba(lba ulong) -> int (reads one 512-byte sector into e4_sec[0..512), 0 = OK) 14 note ext4_sink(off ulong, b uchar) -> void (receives each file byte at offset off) 15 16 const E4_OK int 0 17 const E4_E_IO int -501 18 const E4_E_MAGIC int -502 19 const E4_E_BLKSIZE int -503 20 const E4_E_FEATURE int -504 21 const E4_E_DESC int -505 22 const E4_E_INO int -506 23 const E4_E_INLINE int -510 24 const E4_E_ENCRYPT int -511 25 const E4_E_NOEXTENT int -512 26 const E4_E_EXT_MAGIC int -513 27 const E4_E_EXT_CORRUPT int -514 28 const E4_E_DIR int -515 29 const E4_E_NOENT int -516 30 const E4_E_RANGE int -517 31 const E4_E_SHA int -518 32 const E4_E_TOC int -519 33 34 const E4_FLAG_ENCRYPT ulong 2048 35 const E4_FLAG_EXTENTS ulong 524288 36 const E4_FLAG_INLINE ulong `268435456` 37 const E4_EXT_MAGIC ulong 62218 38 const E4_INCOMPAT_EXTENTS ulong 64 39 const E4_INCOMPAT_64BIT ulong 128 40 note Unsupported incompat features: COMPRESSION(0x1) | META_BG(0x10) | INLINE_DATA(0x8000) | ENCRYPT(0x10000) = 98321 41 const E4_INCOMPAT_UNSUP ulong 98321 42 43 global e4_sec uchar 512 44 global e4_blk uchar 4096 45 global e4_node uchar 4096 46 global e4_iblk uchar 60 47 global e4_path uchar 512 48 global e4_comp uchar 256 49 global e4_want_sha uchar 32 50 global e4_toc uchar 4096 51 52 note Superblock state e4_sb: 53 note 0=mounted, 1=part_lba, 2=part_secs, 3=blk_size, 4=secs_per_blk, 54 note 5=inodes_count, 6=blocks_count, 7=blocks_per_grp, 8=inodes_per_grp, 55 note 9=inode_size, 10=desc_size, 11=gdt_blk, 12=incompat 56 global e4_sb ulong 16 57 58 note Current inode state e4_ino: 59 note 0=ino_num, 1=mode, 2=size, 3=flags 60 global e4_ino ulong 4 61 62 note Extent lookup result e4_map: 63 note 0=phys_blk, 1=is_hole, 2=run_blks (contiguous blocks from lblock to end of extent) 64 global e4_map ulong 3 65 66 note GspFwBundle TOC parsed state (up to 8 entries): 67 note e4_bnd_meta: 0=valid, 1=app_version, 2=num_entries 68 global e4_bnd_meta ulong 4 69 global e4_bnd_kind ulong 8 70 global e4_bnd_off ulong 8 71 global e4_bnd_size ulong 8 72 global e4_bnd_sha uchar 256 73 74 fn e4_u16_blk off ulong to ulong 75 b0 = ulong get e4_blk off 76 b1 = ulong get e4_blk add off 1 77 ret or b0 shl b1 8 78 end 79 80 fn e4_u32_blk off ulong to ulong 81 b0 = ulong get e4_blk off 82 b1 = ulong get e4_blk add off 1 83 b2 = ulong get e4_blk add off 2 84 b3 = ulong get e4_blk add off 3 85 ret or or b0 shl b1 8 or shl b2 16 shl b3 24 86 end 87 88 fn e4_u16_node off ulong to ulong 89 b0 = ulong get e4_node off 90 b1 = ulong get e4_node add off 1 91 ret or b0 shl b1 8 92 end 93 94 fn e4_u32_node off ulong to ulong 95 b0 = ulong get e4_node off 96 b1 = ulong get e4_node add off 1 97 b2 = ulong get e4_node add off 2 98 b3 = ulong get e4_node add off 3 99 ret or or b0 shl b1 8 or shl b2 16 shl b3 24 100 end 101 102 fn e4_u32_toc off ulong to ulong 103 b0 = ulong get e4_toc off 104 b1 = ulong get e4_toc add off 1 105 b2 = ulong get e4_toc add off 2 106 b3 = ulong get e4_toc add off 3 107 ret or or b0 shl b1 8 or shl b2 16 shl b3 24 108 end 109 110 fn e4_u64_toc off ulong to ulong 111 lo = e4_u32_toc off 112 hi = e4_u32_toc add off 4 113 ret or lo shl hi 32 114 end 115 116 note Read one ext4 filesystem block (blk_size bytes) into e4_blk[0..blk_size). 117 fn e4_read_fs_block blk ulong to int 118 if ge blk get e4_sb 6 119 ret E4_E_RANGE 120 end 121 spb = get e4_sb 4 122 if ge blk div get e4_sb 2 spb 123 ret E4_E_RANGE 124 end 125 rel_lba = mul blk spb 126 base_lba = add get e4_sb 1 rel_lba 127 s = ulong 0 128 while lt s spb bound 8 129 r = ext4_read_lba add base_lba s 130 if ne r 0 131 ret E4_E_IO 132 end 133 dst = mul s 512 134 for b ulong 512 135 put e4_blk add dst b get e4_sec b 136 end 137 s = add s 1 138 end 139 ret E4_OK 140 end 141 142 note Mount a read-only ext4 partition starting at part_lba with part_secs 512B sectors. 143 fn ext4_ro_mount part_lba ulong part_secs ulong to int 144 for i ulong 16 145 put e4_sb i 0 146 end 147 if lt part_secs 8 148 ret E4_E_RANGE 149 end 150 r = ext4_read_lba add part_lba 2 151 if ne r 0 152 ret E4_E_IO 153 end 154 for b ulong 512 155 put e4_blk b get e4_sec b 156 end 157 r = ext4_read_lba add part_lba 3 158 if ne r 0 159 ret E4_E_IO 160 end 161 for b2 ulong 512 162 put e4_blk add 512 b2 get e4_sec b2 163 end 164 if ne e4_u16_blk 56 61267 165 ret E4_E_MAGIC 166 end 167 v = e4_u32_blk 24 168 if gt v 2 169 ret E4_E_BLKSIZE 170 end 171 bs = shl ulong 1024 v 172 put e4_sb 3 bs 173 put e4_sb 4 div bs 512 174 put e4_sb 5 e4_u32_blk 0 175 v = e4_u32_blk 32 176 if eq v 0 177 ret E4_E_MAGIC 178 end 179 put e4_sb 7 v 180 v = e4_u32_blk 40 181 if eq v 0 182 ret E4_E_MAGIC 183 end 184 put e4_sb 8 v 185 v = e4_u16_blk 88 186 if or lt v 128 gt v 1024 187 ret E4_E_MAGIC 188 end 189 put e4_sb 9 v 190 inc = e4_u32_blk 96 191 if eq and inc E4_INCOMPAT_EXTENTS 0 192 ret E4_E_FEATURE 193 end 194 if ne and inc E4_INCOMPAT_UNSUP 0 195 ret E4_E_FEATURE 196 end 197 put e4_sb 12 inc 198 put e4_sb 10 32 199 put e4_sb 6 e4_u32_blk 4 200 if ne and inc E4_INCOMPAT_64BIT 0 201 v = e4_u16_blk 254 202 if or lt v 64 gt v 1024 203 ret E4_E_DESC 204 end 205 put e4_sb 10 v 206 put e4_sb 6 or e4_u32_blk 4 shl e4_u32_blk 336 32 207 end 208 put e4_sb 11 1 209 if eq bs 1024 210 put e4_sb 11 2 211 end 212 put e4_sb 1 part_lba 213 put e4_sb 2 part_secs 214 put e4_sb 0 1 215 ret E4_OK 216 end 217 218 note Load and validate inode `ino` (1-based). Fails closed on inline/encrypted/non-extent inodes. 219 fn e4_read_itbl_blk grp ulong to long 220 desc_sz = get e4_sb 10 221 bs = get e4_sb 3 222 gbyte = mul grp desc_sz 223 gblk = add get e4_sb 11 div gbyte bs 224 goff = rem gbyte bs 225 if gt add goff desc_sz bs 226 ret long E4_E_DESC 227 end 228 r = e4_read_fs_block gblk 229 if ne r E4_OK 230 ret long r 231 end 232 lo = e4_u32_blk add goff 8 233 hi = ulong 0 234 if ge desc_sz 64 235 hi = e4_u32_blk add goff 40 236 end 237 ret long or lo shl hi 32 238 end 239 240 fn ext4_ro_load_inode ino ulong to int 241 if eq get e4_sb 0 0 242 ret E4_E_MAGIC 243 end 244 if or eq ino 0 gt ino get e4_sb 5 245 ret E4_E_INO 246 end 247 itbl = e4_read_itbl_blk div sub ino 1 get e4_sb 8 248 if lt itbl 0 249 ret int itbl 250 end 251 ibyte = mul rem sub ino 1 get e4_sb 8 get e4_sb 9 252 bs = get e4_sb 3 253 ioff = rem ibyte bs 254 if gt add ioff 128 bs 255 ret E4_E_INO 256 end 257 r = e4_read_fs_block add ulong itbl div ibyte bs 258 if ne r E4_OK 259 ret r 260 end 261 mode = e4_u16_blk ioff 262 flags = e4_u32_blk add ioff 32 263 if ne and flags E4_FLAG_INLINE 0 264 ret E4_E_INLINE 265 end 266 if ne and flags E4_FLAG_ENCRYPT 0 267 ret E4_E_ENCRYPT 268 end 269 if eq and flags E4_FLAG_EXTENTS 0 270 ret E4_E_NOEXTENT 271 end 272 fsize = e4_u32_blk add ioff 4 273 if eq and mode 61440 32768 274 fsize = or fsize shl e4_u32_blk add ioff 108 32 275 end 276 for k ulong 60 277 put e4_iblk k get e4_blk add add ioff 40 k 278 end 279 put e4_ino 0 ino 280 put e4_ino 1 mode 281 put e4_ino 2 fsize 282 put e4_ino 3 flags 283 ret E4_OK 284 end 285 286 fn e4_map_leaf lblock ulong entries ulong to int 287 i = ulong 0 288 while lt i entries bound 340 289 eoff = add 12 mul i 12 290 ee_blk = e4_u32_node eoff 291 ee_len = e4_u16_node add eoff 4 292 unw = ulong 0 293 if gt ee_len 32768 294 unw = ulong 1 295 ee_len = sub ee_len 32768 296 end 297 if gt ee_len 0 298 if ge lblock ee_blk 299 if lt lblock add ee_blk ee_len 300 rem_run = sub add ee_blk ee_len lblock 301 if ne unw 0 302 put e4_map 0 0 303 put e4_map 1 1 304 put e4_map 2 rem_run 305 ret E4_OK 306 end 307 pblk = add or e4_u32_node add eoff 8 shl e4_u16_node add eoff 6 32 sub lblock ee_blk 308 if ge pblk get e4_sb 6 309 ret E4_E_RANGE 310 end 311 if gt rem_run sub get e4_sb 6 pblk 312 ret E4_E_RANGE 313 end 314 put e4_map 0 pblk 315 put e4_map 1 0 316 put e4_map 2 rem_run 317 ret E4_OK 318 end 319 end 320 end 321 i = add i 1 322 end 323 put e4_map 0 0 324 put e4_map 1 1 325 put e4_map 2 1 326 ret E4_OK 327 end 328 329 fn e4_find_idx lblock ulong entries ulong to long 330 found = false 331 child = ulong 0 332 j = ulong 0 333 while lt j entries bound 340 334 ioff = add 12 mul j 12 335 if le e4_u32_node ioff lblock 336 child = or e4_u32_node add ioff 4 shl e4_u16_node add ioff 8 32 337 found = true 338 else 339 break 340 end 341 j = add j 1 342 end 343 if not found 344 ret -1 345 end 346 ret long child 347 end 348 349 fn e4_copy_iblk_to_node to void 350 for k ulong 60 351 put e4_node k get e4_iblk k 352 end 353 end 354 355 fn e4_copy_blk_to_node to void 356 bs = get e4_sb 3 357 b = ulong 0 358 while lt b bs bound 4096 359 put e4_node b get e4_blk b 360 b = add b 1 361 end 362 end 363 364 note Map logical file block `lblock` through the current inode's 48-bit extent tree (depth 0..5). 365 note Writes result to e4_map[0]=phys_blk, e4_map[1]=is_hole (1 if sparse or unwritten ee_len>32768). 366 fn ext4_ro_map_block lblock ulong to int 367 put e4_map 0 0 368 put e4_map 1 1 369 put e4_map 2 1 370 e4_copy_iblk_to_node 371 max_lim = ulong 4 372 prev_d = ulong 99 373 step = ulong 0 374 while lt step 6 bound 6 375 if ne e4_u16_node 0 E4_EXT_MAGIC 376 ret E4_E_EXT_MAGIC 377 end 378 entries = e4_u16_node 2 379 eh_max = e4_u16_node 4 380 depth = e4_u16_node 6 381 if or gt entries eh_max gt eh_max max_lim 382 ret E4_E_EXT_CORRUPT 383 end 384 if gt depth 5 385 ret E4_E_EXT_CORRUPT 386 end 387 if ne prev_d 99 388 if ne add depth 1 prev_d 389 ret E4_E_EXT_CORRUPT 390 end 391 end 392 if eq depth 0 393 ret e4_map_leaf lblock entries 394 end 395 ch = e4_find_idx lblock entries 396 if lt ch 0 397 put e4_map 0 0 398 put e4_map 1 1 399 put e4_map 2 1 400 ret E4_OK 401 end 402 r = e4_read_fs_block ulong ch 403 if ne r E4_OK 404 ret r 405 end 406 e4_copy_blk_to_node 407 max_lim = div sub get e4_sb 3 12 12 408 prev_d = depth 409 step = add step 1 410 end 411 ret E4_E_EXT_CORRUPT 412 end 413 414 fn e4_name_eq base ulong clen ulong to bool 415 c = ulong 0 416 while lt c clen bound 256 417 if ne get e4_blk add base c get e4_comp c 418 ret false 419 end 420 c = add c 1 421 end 422 ret true 423 end 424 425 fn e4_dir_scan_blk clen ulong to long 426 bs = get e4_sb 3 427 off = ulong 0 428 while lt off bs bound 512 429 if gt add off 8 bs 430 ret long E4_E_DIR 431 end 432 e_ino = e4_u32_blk off 433 rlen = e4_u16_blk add off 4 434 nlen = ulong get e4_blk add off 6 435 if or lt rlen 8 gt add off rlen bs 436 ret long E4_E_DIR 437 end 438 if ne e_ino 0 439 if eq nlen clen 440 if le add 8 nlen rlen 441 if e4_name_eq add off 8 clen 442 ret long e_ino 443 end 444 end 445 end 446 end 447 off = add off rlen 448 end 449 ret 0 450 end 451 452 note Lookup single component e4_comp[0..clen) inside currently loaded directory inode. 453 fn e4_dir_find clen ulong to long 454 if ne and get e4_ino 1 61440 16384 455 ret long E4_E_DIR 456 end 457 bs = get e4_sb 3 458 nblks = div add get e4_ino 2 sub bs 1 bs 459 lb = ulong 0 460 while lt lb nblks bound 4096 461 r = ext4_ro_map_block lb 462 if ne r E4_OK 463 ret long r 464 end 465 if eq get e4_map 1 0 466 r = e4_read_fs_block get e4_map 0 467 if ne r E4_OK 468 ret long r 469 end 470 hit = e4_dir_scan_blk clen 471 if ne hit 0 472 ret hit 473 end 474 end 475 lb = add lb 1 476 end 477 ret 0 478 end 479 480 note Resolve absolute path in e4_path[0..plen) starting from root inode 2. 481 note On success, leaves target inode loaded in e4_ino/e4_iblk and returns E4_OK. 482 fn ext4_ro_lookup plen ulong to int 483 r = ext4_ro_load_inode 2 484 if ne r E4_OK 485 ret r 486 end 487 p = ulong 0 488 while lt p plen bound 512 489 while lt p plen bound 512 490 if ne get e4_path p 47 491 break 492 end 493 p = add p 1 494 end 495 if ge p plen 496 break 497 end 498 clen = ulong 0 499 while lt p plen bound 256 500 ch = get e4_path p 501 if eq ch 47 502 break 503 end 504 if ge clen 255 505 ret E4_E_NOENT 506 end 507 put e4_comp clen ch 508 clen = add clen 1 509 p = add p 1 510 end 511 nxt = e4_dir_find clen 512 if lt nxt 0 513 ret int nxt 514 end 515 if eq nxt 0 516 ret E4_E_NOENT 517 end 518 r = ext4_ro_load_inode ulong nxt 519 if ne r E4_OK 520 ret r 521 end 522 end 523 ret E4_OK 524 end 525 526 fn e4_emit_zeros done ulong take ulong hash_on ulong to void 527 k = ulong 0 528 while lt k take bound 4096 529 ext4_sink add done k 0 530 if ne hash_on 0 531 s256_byte 0 532 end 533 k = add k 1 534 end 535 end 536 537 fn e4_emit_blk done ulong boff ulong take ulong hash_on ulong to void 538 k = ulong 0 539 while lt k take bound 4096 540 bv = get e4_blk add boff k 541 ext4_sink add done k bv 542 if ne hash_on 0 543 s256_byte bv 544 end 545 k = add k 1 546 end 547 end 548 549 note Stream [file_off .. file_off+nbytes) of current inode through ext4_sink and SHA-256. 550 note Fails closed if not mounted or current inode is not a regular file (S_IFREG = 0x8000). 551 fn ext4_ro_stream file_off ulong nbytes ulong hash_on ulong to int 552 if eq get e4_sb 0 0 553 ret E4_E_MAGIC 554 end 555 if ne and get e4_ino 1 61440 32768 556 ret E4_E_INO 557 end 558 fsize = get e4_ino 2 559 if or gt file_off fsize gt nbytes sub fsize file_off 560 ret E4_E_RANGE 561 end 562 if ne hash_on 0 563 s256_init 564 end 565 bs = get e4_sb 3 566 done = ulong 0 567 while lt done nbytes bound `9223372036854775807` 568 pos = add file_off done 569 boff = rem pos bs 570 take = sub bs boff 571 if gt take sub nbytes done 572 take = sub nbytes done 573 end 574 r = ext4_ro_map_block div pos bs 575 if ne r E4_OK 576 ret r 577 end 578 if ne get e4_map 1 0 579 e4_emit_zeros done take hash_on 580 else 581 r = e4_read_fs_block get e4_map 0 582 if ne r E4_OK 583 ret r 584 end 585 e4_emit_blk done boff take hash_on 586 end 587 done = add done take 588 end 589 if ne hash_on 0 590 s256_fin 591 end 592 ret E4_OK 593 end 594 595 fn ext4_ro_verify_sha to int 596 for i ulong 32 597 if ne get s2_out i get e4_want_sha i 598 ret E4_E_SHA 599 end 600 end 601 ret E4_OK 602 end 603 604 fn bundle_check_overlap i ulong off ulong sz ulong to int 605 j = ulong 0 606 while lt j i bound 8 607 po = get e4_bnd_off j 608 ps = get e4_bnd_size j 609 if lt off add po ps 610 if lt po add off sz 611 ret E4_E_TOC 612 end 613 end 614 j = add j 1 615 end 616 ret E4_OK 617 end 618 619 fn bundle_parse_entry i ulong bundle_len ulong to int 620 eo = add 32 mul i 64 621 off = e4_u64_toc add eo 8 622 sz = e4_u64_toc add eo 16 623 if or eq sz 0 or lt off 4096 ne and off 4095 0 624 ret E4_E_TOC 625 end 626 if or gt off bundle_len gt sz sub bundle_len off 627 ret E4_E_TOC 628 end 629 r = bundle_check_overlap i off sz 630 if ne r E4_OK 631 ret r 632 end 633 put e4_bnd_kind i e4_u32_toc eo 634 put e4_bnd_off i off 635 put e4_bnd_size i sz 636 for s ulong 32 637 put e4_bnd_sha add mul i 32 s get e4_toc add add eo 32 s 638 end 639 ret E4_OK 640 end 641 642 note Parse 4 KiB OMFWBND1 GspFwBundle TOC from e4_toc[0..4096). 643 note Header: "OMFWBND1" (8B), version=1 (u32), app_version (u32), num_entries (u32, 1..8), reserved (u32). 644 note Entries at +32 + i*64: kind(u32), flags(u32), offset(u64), size(u64), pad(u64), sha256(32B). 645 fn bundle_parse_toc bundle_len ulong to int 646 for z ulong 4 647 put e4_bnd_meta z 0 648 end 649 if lt bundle_len 4096 650 ret E4_E_TOC 651 end 652 if or ne get e4_toc 0 79 ne get e4_toc 1 77 653 ret E4_E_TOC 654 end 655 if or ne get e4_toc 2 70 ne get e4_toc 3 87 656 ret E4_E_TOC 657 end 658 if or ne get e4_toc 4 66 ne get e4_toc 5 78 659 ret E4_E_TOC 660 end 661 if or ne get e4_toc 6 68 ne get e4_toc 7 49 662 ret E4_E_TOC 663 end 664 if ne e4_u32_toc 8 1 665 ret E4_E_TOC 666 end 667 nent = e4_u32_toc 16 668 if or eq nent 0 gt nent 8 669 ret E4_E_TOC 670 end 671 i = ulong 0 672 while lt i nent bound 8 673 r = bundle_parse_entry i bundle_len 674 if ne r E4_OK 675 ret r 676 end 677 i = add i 1 678 end 679 put e4_bnd_meta 0 1 680 put e4_bnd_meta 1 e4_u32_toc 12 681 put e4_bnd_meta 2 nent 682 ret E4_OK 683 end 684 685 fn ext4_ro_file_size to ulong 686 ret get e4_ino 2 687 end 688 689 note Contiguous extent-run helper for Phase 3 128 KiB nvme_read_direct bursts: 690 note Maps lblock and exposes (e4_map[0]=phys_blk, e4_map[1]=is_hole, e4_map[2]=run_blks). 691 fn ext4_ro_map_extent lblock ulong to int 692 ret ext4_ro_map_block lblock 693 end 694 695 fn ext4_ro_run_blks to ulong 696 ret get e4_map 2 697 end