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