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 / disk / ext4_ro.om

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