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 / gpt_ro.om

376 lines · 7626 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/disk/gpt_ro.om: Standalone strictly read-only GPT partition discovery (Slice D / Piece 4).
3note ZERO write paths — never links hal_nvme_authorize_write or nvme_store_gate.om.
4note Expects a read-only sector reader callback:
5note gpt_read_lba(lba ulong) -> int (reads 512-byte sector into global gpt_sec[0..512), 0 = OK)
6
7const GPT_OK int 0
8const GPT_E_IO int -401
9const GPT_E_GEOM int -402
10const GPT_E_NOMBR int -403
11const GPT_E_SIG int -404
12const GPT_E_HDR_CRC int -405
13const GPT_E_HDR int -406
14const GPT_E_ENT_CRC int -407
15const GPT_E_ENT int -408
16const GPT_E_BACKUP int -409
17const GPT_E_OUTSIDE int -410
18const GPT_E_OVERLAP int -411
19const GPT_E_NOPART int -412
20
21global gpt_sec uchar 512
22global gpt_ents uchar 16384
23note gpt_st: 0=valid, 1=first_usable, 2=last_usable, 3=num_ents, 4=ent_size,
24note 5=found_first_lba, 6=found_sectors, 7=found_index
25global gpt_st ulong 8
26global gpt_lo ulong 128
27global gpt_hi ulong 128
28
29fn gpt_rd32_sec off ulong to uint
30b0 = uint get gpt_sec off
31b1 = uint get gpt_sec add off 1
32b2 = uint get gpt_sec add off 2
33b3 = uint get gpt_sec add off 3
34ret or or b0 shl b1 8 or shl b2 16 shl b3 24
35end
36
37fn gpt_rd64_sec off ulong to ulong
38lo = ulong gpt_rd32_sec off
39hi = ulong gpt_rd32_sec add off 4
40ret or lo shl hi 32
41end
42
43fn gpt_rd32_ent off ulong to uint
44b0 = uint get gpt_ents off
45b1 = uint get gpt_ents add off 1
46b2 = uint get gpt_ents add off 2
47b3 = uint get gpt_ents add off 3
48ret or or b0 shl b1 8 or shl b2 16 shl b3 24
49end
50
51fn gpt_rd64_ent off ulong to ulong
52lo = ulong gpt_rd32_ent off
53hi = ulong gpt_rd32_ent add off 4
54ret or lo shl hi 32
55end
56
57fn gpt_crc32_hdr hsize ulong to uint
58crc = uint 4294967295
59i = ulong 0
60while lt i hsize bound 512
61b = uint 0
62if or lt i 16 ge i 20
63b = uint get gpt_sec i
64end
65crc = xor crc b
66for bit ulong 8
67if ne and crc 1 0
68crc = xor shr crc 1 3988292384
69else
70crc = shr crc 1
71end
72end
73i = add i 1
74end
75ret xor crc 4294967295
76end
77
78fn gpt_crc32_ents n ulong to uint
79crc = uint 4294967295
80i = ulong 0
81while lt i n bound 16384
82b = uint get gpt_ents i
83crc = xor crc b
84for bit ulong 8
85if ne and crc 1 0
86crc = xor shr crc 1 3988292384
87else
88crc = shr crc 1
89end
90end
91i = add i 1
92end
93ret xor crc 4294967295
94end
95
96fn gpt_is_sig to bool
97if or ne get gpt_sec 0 69 ne get gpt_sec 1 70
98ret false
99end
100if or ne get gpt_sec 2 73 ne get gpt_sec 3 32
101ret false
102end
103if or ne get gpt_sec 4 80 ne get gpt_sec 5 65
104ret false
105end
106if or ne get gpt_sec 6 82 ne get gpt_sec 7 84
107ret false
108end
109ret true
110end
111
112fn gpt_ent_nonzero off ulong to bool
113for i ulong 16
114if ne get gpt_ents add off i 0
115ret true
116end
117end
118ret false
119end
120
121note Linux filesystem GUID: 0FC63DAF-8483-4772-8E79-3D69D8477DE4
122note Little-endian bytes: af 3d c6 0f 83 84 72 47 8e 79 3d 69 d8 47 7d e4
123fn gpt_is_linux_fs off ulong to bool
124if or ne get gpt_ents off 175 ne get gpt_ents add off 1 61
125ret false
126end
127if or ne get gpt_ents add off 2 198 ne get gpt_ents add off 3 15
128ret false
129end
130if or ne get gpt_ents add off 4 131 ne get gpt_ents add off 5 132
131ret false
132end
133if or ne get gpt_ents add off 6 114 ne get gpt_ents add off 7 71
134ret false
135end
136if or ne get gpt_ents add off 8 142 ne get gpt_ents add off 9 121
137ret false
138end
139if or ne get gpt_ents add off 10 61 ne get gpt_ents add off 11 105
140ret false
141end
142if or ne get gpt_ents add off 12 216 ne get gpt_ents add off 13 71
143ret false
144end
145if or ne get gpt_ents add off 14 125 ne get gpt_ents add off 15 228
146ret false
147end
148ret true
149end
150
151fn gpt_check_mbr to int
152r = gpt_read_lba 0
153if ne r 0
154ret GPT_E_IO
155end
156if or ne get gpt_sec 510 85 ne get gpt_sec 511 170
157ret GPT_E_NOMBR
158end
159has_ee = false
160for p ulong 4
161if eq get gpt_sec add 446 add mul p 16 4 238
162has_ee = true
163end
164end
165if not has_ee
166ret GPT_E_NOMBR
167end
168ret GPT_OK
169end
170
171fn gpt_load_ents ent_lba ulong ent_bytes ulong want_ecrc uint to int
172ent_secs = div add ent_bytes 511 512
173s = ulong 0
174while lt s ent_secs bound 32
175r = gpt_read_lba add ent_lba s
176if ne r 0
177ret GPT_E_IO
178end
179base = mul s 512
180for b ulong 512
181if lt add base b ent_bytes
182put gpt_ents add base b get gpt_sec b
183end
184end
185s = add s 1
186end
187if ne gpt_crc32_ents ent_bytes want_ecrc
188ret GPT_E_ENT_CRC
189end
190ret GPT_OK
191end
192
193fn gpt_check_backup bak_lba ulong want_ecrc uint to int
194r = gpt_read_lba bak_lba
195if ne r 0
196ret GPT_E_IO
197end
198if not gpt_is_sig
199ret GPT_E_BACKUP
200end
201bhsize = ulong gpt_rd32_sec 12
202if or lt bhsize 92 gt bhsize 512
203ret GPT_E_BACKUP
204end
205if ne gpt_crc32_hdr bhsize gpt_rd32_sec 16
206ret GPT_E_BACKUP
207end
208if or ne gpt_rd64_sec 24 bak_lba ne gpt_rd64_sec 32 1
209ret GPT_E_BACKUP
210end
211if ne gpt_rd32_sec 88 want_ecrc
212ret GPT_E_BACKUP
213end
214ret GPT_OK
215end
216
217fn gpt_check_overlap num_ents ulong first_u ulong last_u ulong to int
218used = ulong 0
219idx = ulong 0
220while lt idx num_ents bound 128
221off = mul idx 128
222if gpt_ent_nonzero off
223fl = gpt_rd64_ent add off 32
224ll = gpt_rd64_ent add off 40
225if or lt fl first_u gt ll last_u
226ret GPT_E_OUTSIDE
227end
228if gt fl ll
229ret GPT_E_OUTSIDE
230end
231j = ulong 0
232while lt j used bound 128
233if le fl get gpt_hi j
234if ge ll get gpt_lo j
235ret GPT_E_OVERLAP
236end
237end
238j = add j 1
239end
240put gpt_lo used fl
241put gpt_hi used ll
242used = add used 1
243end
244idx = add idx 1
245end
246ret GPT_OK
247end
248
249note Parse and verify Protective MBR + Primary GPT + Backup GPT + Partition Entry Array.
250note total_sectors: total 512-byte sectors on the NVMe namespace.
251fn gpt_ro_parse total_sectors ulong to int
252for i ulong 8
253put gpt_st i 0
254end
255if lt total_sectors 68
256ret GPT_E_GEOM
257end
258r = gpt_check_mbr
259if ne r GPT_OK
260ret r
261end
262r = gpt_read_lba 1
263if ne r 0
264ret GPT_E_IO
265end
266if not gpt_is_sig
267ret GPT_E_SIG
268end
269hsize = ulong gpt_rd32_sec 12
270if or lt hsize 92 gt hsize 512
271ret GPT_E_HDR
272end
273if ne gpt_crc32_hdr hsize gpt_rd32_sec 16
274ret GPT_E_HDR_CRC
275end
276bak_lba = gpt_rd64_sec 32
277if or ne gpt_rd64_sec 24 1 ne bak_lba sub total_sectors 1
278ret GPT_E_HDR
279end
280first_u = gpt_rd64_sec 40
281last_u = gpt_rd64_sec 48
282if or lt first_u 34 ge last_u bak_lba
283ret GPT_E_HDR
284end
285if gt first_u last_u
286ret GPT_E_HDR
287end
288num_ents = ulong gpt_rd32_sec 80
289if or eq num_ents 0 gt num_ents 128
290ret GPT_E_ENT
291end
292if ne ulong gpt_rd32_sec 84 128
293ret GPT_E_ENT
294end
295ent_lba = gpt_rd64_sec 72
296ent_secs = div add mul num_ents 128 511 512
297if or lt ent_lba 2 lt add ent_lba ent_secs ent_lba
298ret GPT_E_ENT
299end
300if gt add ent_lba ent_secs first_u
301ret GPT_E_ENT
302end
303want_ecrc = gpt_rd32_sec 88
304r = gpt_load_ents ent_lba mul num_ents 128 want_ecrc
305if ne r GPT_OK
306ret r
307end
308r = gpt_check_backup bak_lba want_ecrc
309if ne r GPT_OK
310ret r
311end
312r = gpt_check_overlap num_ents first_u last_u
313if ne r GPT_OK
314ret r
315end
316put gpt_st 0 1
317put gpt_st 1 first_u
318put gpt_st 2 last_u
319put gpt_st 3 num_ents
320put gpt_st 4 128
321ret GPT_OK
322end
323
324note Locate 1-based partition index (e.g., 2 for nvme0n1p2).
325fn gpt_ro_select_index part_idx ulong to int
326if eq get gpt_st 0 0
327ret GPT_E_HDR
328end
329if or eq part_idx 0 gt part_idx get gpt_st 3
330ret GPT_E_NOPART
331end
332off = mul sub part_idx 1 128
333if not gpt_ent_nonzero off
334ret GPT_E_NOPART
335end
336fl = gpt_rd64_ent add off 32
337ll = gpt_rd64_ent add off 40
338put gpt_st 5 fl
339put gpt_st 6 add sub ll fl 1
340put gpt_st 7 part_idx
341ret GPT_OK
342end
343
344note Locate first Linux Filesystem GUID partition (0FC63DAF-8483-4772-8E79-3D69D8477DE4).
345fn gpt_ro_select_linux_root to int
346if eq get gpt_st 0 0
347ret GPT_E_HDR
348end
349num_ents = get gpt_st 3
350idx = ulong 0
351while lt idx num_ents bound 128
352off = mul idx 128
353if gpt_is_linux_fs off
354fl = gpt_rd64_ent add off 32
355ll = gpt_rd64_ent add off 40
356put gpt_st 5 fl
357put gpt_st 6 add sub ll fl 1
358put gpt_st 7 add idx 1
359ret GPT_OK
360end
361idx = add idx 1
362end
363ret GPT_E_NOPART
364end
365
366fn gpt_ro_first_lba to ulong
367ret get gpt_st 5
368end
369
370fn gpt_ro_sectors to ulong
371ret get gpt_st 6
372end
373
374fn gpt_ro_part_index to ulong
375ret get gpt_st 7
376end