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/tools/imac-image/imac-image.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 / tools / imac-image / imac-image.om` 605 lines ยท 11452 bytes Raw Blame History 1 note SPDX-License-Identifier: MIT 2 note Builds the Allen iMac boot image with the seeded compiler alone: protective MBR, 3 note GPT with one FAT32 EFI System Partition, \EFI\BOOT\BOOTX64.EFI and STARTUP.NSH. 4 note No host partitioning or FAT tool is used; every byte is written by this program. 5 note Geometry is fixed so the same EFI bytes always give the same image digest: 6 note disk 196608 sectors, ESP 2048..196574 (194527 sectors), FAT32 512-byte sectors, 7 note 1 sector per cluster, 32 reserved sectors, 2 FATs of 1497 sectors, 191501 clusters. 8 9 const TOTS ulong 196608 10 const ESPST ulong 2048 11 const ESPOF ulong `1048576` 12 const ESPS ulong 194527 13 const RSV ulong 32 14 const FATSZ ulong 1497 15 const FATS ulong 1497 16 const CLUS ulong 191501 17 const BKLBA ulong 196607 18 const BKENT ulong 196575 19 const LASTU ulong 196574 20 const RDIR ulong 2 21 const EDIR ulong 3 22 const BDIR ulong 4 23 const FCLS ulong 5 24 const VOLID ulong `2065321822` 25 const DDATE ulong 23897 26 const ECOUNT ulong 128 27 const ESIZE ulong 128 28 const IMGLN ulong `100663296` 29 const EOC ulong `268435455` 30 31 global Nes ulong 1 32 global Err ulong 1 33 global Tv ulong 4 34 35 fn s16 S edit uchar 512 off ulong v ulong to void 36 put S off uchar and v 255 37 put S add off 1 uchar and shr v 8 255 38 end 39 40 fn s32 S edit uchar 512 off ulong v ulong to void 41 put S off uchar and v 255 42 put S add off 1 uchar and shr v 8 255 43 put S add off 2 uchar and shr v 16 255 44 put S add off 3 uchar and shr v 24 255 45 end 46 47 fn s64 S edit uchar 512 off ulong v ulong to void 48 s32 edit S off v 49 s32 edit S add off 4 shr v 32 50 end 51 52 fn g64 S edit uchar 16384 off ulong v ulong to void 53 put S off uchar and v 255 54 put S add off 1 uchar and shr v 8 255 55 put S add off 2 uchar and shr v 16 255 56 put S add off 3 uchar and shr v 24 255 57 put S add off 4 uchar and shr v 32 255 58 put S add off 5 uchar and shr v 40 255 59 put S add off 6 uchar and shr v 48 255 60 put S add off 7 uchar and shr v 56 255 61 end 62 63 fn scrc S view uchar 512 n ulong to ulong 64 note CRC32 of the first n bytes of the working sector. 65 c = ulong `4294967295` 66 i = ulong 0 67 while lt i n bound 512 68 c = xor c ulong get S i 69 k = ulong 0 70 while lt k 8 bound 8 71 if eq and c 1 1 72 c = xor shr c 1 `3988292384` 73 else 74 c = shr c 1 75 end 76 k = add k 1 77 end 78 i = add i 1 79 end 80 ret xor c `4294967295` 81 end 82 83 fn gcrc G view uchar 16384 to ulong 84 note CRC32 of the whole 16384-byte GPT entry array. 85 c = ulong `4294967295` 86 i = ulong 0 87 while lt i 16384 bound 20000 88 c = xor c ulong get G i 89 k = ulong 0 90 while lt k 8 bound 8 91 if eq and c 1 1 92 c = xor shr c 1 `3988292384` 93 else 94 c = shr c 1 95 end 96 k = add k 1 97 end 98 i = add i 1 99 end 100 ret xor c `4294967295` 101 end 102 103 fn zsec S edit uchar 512 to void 104 i = ulong 0 105 while lt i 512 bound 512 106 put S i uchar 0 107 i = add i 1 108 end 109 end 110 111 fn zsecs fd long S edit uchar 512 n ulong to void 112 i = ulong 0 113 while lt i n bound 2100 114 zsec edit S 115 if lt write int fd S 0 512 512 116 put Err 0 add get Err 0 1 117 end 118 i = add i 1 119 end 120 end 121 122 fn swr fd long S view uchar 512 to void 123 if lt write int fd S 0 512 512 124 put Err 0 add get Err 0 1 125 end 126 end 127 128 fn gwr fd long G view uchar 16384 to void 129 if lt write int fd G 0 16384 16384 130 put Err 0 add get Err 0 1 131 end 132 end 133 134 fn datap fi int fo long to void 135 note Copy the EFI payload one cluster at a time, zero padding a short final read. No size ceiling. 136 C = new uchar 512 0 137 i = ulong 0 138 while lt i get Nes 0 bound 200000 139 zsec edit C 140 r = read fi C 0 512 141 if lt r 0 142 put Err 0 add get Err 0 1 143 end 144 if lt write int fo C 0 512 512 145 put Err 0 add get Err 0 1 146 end 147 i = add i 1 148 end 149 end 150 151 fn seek fd long off ulong to void 152 r = fseek int fd long off 0 153 if lt r 0 154 put Err 0 add get Err 0 1 155 end 156 end 157 158 fn spcs S edit uchar 512 off ulong n ulong to void 159 i = ulong 0 160 while lt i n bound 12 161 put S add off i uchar 32 162 i = add i 1 163 end 164 end 165 166 fn dent S edit uchar 512 off ulong at ulong clus ulong sz ulong to void 167 note One 32-byte directory entry; the name bytes were written before this call. 168 put S add off 11 uchar at 169 put S add off 12 uchar 0 170 put S add off 13 uchar 0 171 s16 edit S add off 14 0 172 s16 edit S add off 16 DDATE 173 s16 edit S add off 18 DDATE 174 s16 edit S add off 20 shr clus 16 175 s16 edit S add off 22 0 176 s16 edit S add off 24 DDATE 177 s16 edit S add off 26 and clus 65535 178 s32 edit S add off 28 sz 179 end 180 181 fn fatvl c ulong to ulong 182 note FAT32 chain value for one cluster index; EOC marks the end of a chain. 183 last = add FCLS sub get Nes 0 1 184 nsh = add FCLS get Nes 0 185 if lt c RDIR 186 ret 0 187 end 188 if lt c FCLS 189 ret EOC 190 end 191 if lt c last 192 ret add c 1 193 end 194 if eq c last 195 ret EOC 196 end 197 if eq c nsh 198 ret EOC 199 end 200 ret 0 201 end 202 203 fn fspar to void 204 note FAT32 free-cluster count and next free cluster for the current EFI size. 205 n = get Nes 0 206 put Tv 0 sub sub CLUS 4 n 207 put Tv 1 add add RDIR 4 n 208 end 209 210 fn fats S edit uchar 512 idx ulong to void 211 note Build FAT sector idx from the chain function so both copies are identical. 212 zsec edit S 213 i = ulong 0 214 while lt i 128 bound 128 215 c = add mul idx 128 i 216 if lt c add CLUS 2 217 v = fatvl c 218 if eq c 0 219 v = `4294967288` 220 end 221 if eq c 1 222 v = EOC 223 end 224 s32 edit S mul i 4 v 225 end 226 i = add i 1 227 end 228 end 229 230 fn mbrmk S edit uchar 512 to void 231 zsec edit S 232 put S 446 uchar 0 233 put S 450 uchar 238 234 put S 454 uchar 1 235 put S 458 uchar and sub TOTS 1 255 236 put S 459 uchar and shr sub TOTS 1 8 255 237 put S 460 uchar and shr sub TOTS 1 16 255 238 put S 461 uchar and shr sub TOTS 1 24 255 239 put S 510 uchar 85 240 put S 511 uchar 170 241 end 242 243 fn gpthd S edit uchar 512 cur ulong ent ulong back ulong ecr ulong to void 244 note Build one GPT header sector and return the CRC32 of its 92 bytes. 245 zsec edit S 246 put S 0 uchar 69 247 put S 1 uchar 70 248 put S 2 uchar 73 249 put S 3 uchar 32 250 put S 4 uchar 80 251 put S 5 uchar 65 252 put S 6 uchar 82 253 put S 7 uchar 84 254 s32 edit S 8 65536 255 s32 edit S 12 92 256 s32 edit S 20 0 257 s64 edit S 24 cur 258 s64 edit S 32 back 259 s64 edit S 40 34 260 s64 edit S 48 LASTU 261 s32 edit S 56 `305419896` 262 s32 edit S 60 `2271560481` 263 s32 edit S 64 `1432778632` 264 s32 edit S 68 `3723482536` 265 s64 edit S 72 ent 266 s32 edit S 80 ECOUNT 267 s32 edit S 84 ESIZE 268 s32 edit S 88 ecr 269 s32 edit S 16 scrc view S 92 270 end 271 272 fn entar G edit uchar 16384 to void 273 note Build the 128-entry GPT array; entry 0 is the EFI System Partition. 274 i = ulong 0 275 while lt i 16384 bound 20000 276 put G i uchar 0 277 i = add i 1 278 end 279 put G 0 uchar 40 280 put G 1 uchar 115 281 put G 2 uchar 42 282 put G 3 uchar 193 283 put G 4 uchar 31 284 put G 5 uchar 248 285 put G 6 uchar 210 286 put G 7 uchar 17 287 put G 8 uchar 186 288 put G 9 uchar 75 289 put G 10 uchar 0 290 put G 11 uchar 160 291 put G 12 uchar 201 292 put G 13 uchar 62 293 put G 14 uchar 201 294 put G 15 uchar 59 295 put G 16 uchar 17 296 put G 17 uchar 34 297 put G 18 uchar 51 298 put G 19 uchar 68 299 put G 20 uchar 85 300 put G 21 uchar 102 301 put G 22 uchar 119 302 put G 23 uchar 136 303 put G 24 uchar 153 304 put G 25 uchar 170 305 put G 26 uchar 187 306 put G 27 uchar 204 307 put G 28 uchar 221 308 put G 29 uchar 238 309 put G 30 uchar 255 310 put G 31 uchar 0 311 g64 edit G 32 ESPST 312 g64 edit G 40 LASTU 313 g64 edit G 48 0 314 put G 56 uchar 65 315 put G 58 uchar 76 316 put G 60 uchar 76 317 put G 62 uchar 69 318 put G 64 uchar 78 319 end 320 321 fn bootsc S edit uchar 512 to void 322 note FAT32 BPB for the ESP. 323 zsec edit S 324 put S 0 uchar 235 325 put S 1 uchar 88 326 put S 2 uchar 144 327 put S 3 uchar 79 328 put S 4 uchar 77 329 put S 5 uchar 69 330 put S 6 uchar 71 331 put S 7 uchar 65 332 put S 8 uchar 32 333 put S 9 uchar 32 334 put S 10 uchar 32 335 s16 edit S 11 512 336 put S 13 uchar 1 337 s16 edit S 14 RSV 338 put S 16 uchar 2 339 s16 edit S 17 0 340 s16 edit S 19 0 341 put S 21 uchar 248 342 s16 edit S 22 0 343 s16 edit S 24 63 344 s16 edit S 26 255 345 s32 edit S 28 ESPST 346 s32 edit S 32 ESPS 347 s32 edit S 36 FATSZ 348 s16 edit S 40 0 349 s16 edit S 42 0 350 s32 edit S 44 RDIR 351 s16 edit S 48 1 352 s16 edit S 50 6 353 s32 edit S 52 0 354 s32 edit S 56 0 355 put S 64 uchar 128 356 put S 66 uchar 41 357 s32 edit S 67 VOLID 358 put S 71 uchar 65 359 put S 72 uchar 76 360 put S 73 uchar 76 361 put S 74 uchar 69 362 put S 75 uchar 78 363 spcs edit S 76 6 364 put S 82 uchar 70 365 put S 83 uchar 65 366 put S 84 uchar 84 367 put S 85 uchar 51 368 put S 86 uchar 50 369 spcs edit S 87 3 370 put S 510 uchar 85 371 put S 511 uchar 170 372 end 373 374 fn fsinf S edit uchar 512 frc ulong nxt ulong to void 375 zsec edit S 376 s32 edit S 0 `1096897106` 377 s32 edit S 484 `1631679090` 378 s32 edit S 488 frc 379 s32 edit S 492 nxt 380 s32 edit S 508 `2857697280` 381 end 382 383 fn rootd S edit uchar 512 to void 384 note Volume label, EFI directory and the STARTUP.NSH file in the root cluster. 385 zsec edit S 386 put S 0 uchar 65 387 put S 1 uchar 76 388 put S 2 uchar 76 389 put S 3 uchar 69 390 put S 4 uchar 78 391 spcs edit S 5 6 392 dent edit S 0 8 0 0 393 put S 32 uchar 69 394 put S 33 uchar 70 395 put S 34 uchar 73 396 spcs edit S 35 8 397 dent edit S 32 16 EDIR 0 398 put S 64 uchar 83 399 put S 65 uchar 84 400 put S 66 uchar 65 401 put S 67 uchar 82 402 put S 68 uchar 84 403 put S 69 uchar 85 404 put S 70 uchar 80 405 put S 71 uchar 32 406 put S 72 uchar 78 407 put S 73 uchar 83 408 put S 74 uchar 72 409 c = add FCLS get Nes 0 410 dent edit S 64 32 c 27 411 end 412 413 fn efidr S edit uchar 512 to void 414 zsec edit S 415 put S 0 uchar 46 416 spcs edit S 1 10 417 dent edit S 0 16 EDIR 0 418 put S 32 uchar 46 419 put S 33 uchar 46 420 spcs edit S 34 9 421 dent edit S 32 16 0 0 422 put S 64 uchar 66 423 put S 65 uchar 79 424 put S 66 uchar 79 425 put S 67 uchar 84 426 spcs edit S 68 7 427 dent edit S 64 16 BDIR 0 428 end 429 430 fn bootd S edit uchar 512 to void 431 zsec edit S 432 put S 0 uchar 46 433 spcs edit S 1 10 434 dent edit S 0 16 BDIR 0 435 put S 32 uchar 46 436 put S 33 uchar 46 437 spcs edit S 34 9 438 dent edit S 32 16 EDIR 0 439 put S 64 uchar 66 440 put S 65 uchar 79 441 put S 66 uchar 79 442 put S 67 uchar 84 443 put S 68 uchar 88 444 put S 69 uchar 54 445 put S 70 uchar 52 446 put S 71 uchar 32 447 put S 72 uchar 69 448 put S 73 uchar 70 449 put S 74 uchar 73 450 dent edit S 64 32 FCLS get Tv 2 451 end 452 453 fn startn S edit uchar 512 to void 454 zsec edit S 455 put S 0 uchar 102 456 put S 1 uchar 115 457 put S 2 uchar 48 458 put S 3 uchar 58 459 put S 4 uchar 92 460 put S 5 uchar 69 461 put S 6 uchar 70 462 put S 7 uchar 73 463 put S 8 uchar 92 464 put S 9 uchar 66 465 put S 10 uchar 79 466 put S 11 uchar 79 467 put S 12 uchar 84 468 put S 13 uchar 92 469 put S 14 uchar 66 470 put S 15 uchar 79 471 put S 16 uchar 79 472 put S 17 uchar 84 473 put S 18 uchar 88 474 put S 19 uchar 54 475 put S 20 uchar 52 476 put S 21 uchar 46 477 put S 22 uchar 69 478 put S 23 uchar 70 479 put S 24 uchar 73 480 put S 25 uchar 13 481 put S 26 uchar 10 482 end 483 484 fn main to void 485 Sct = new uchar 512 0 486 Gpt = new uchar 16384 0 487 Pth = new uchar 32 0 488 489 put Pth 0 uchar 66 490 put Pth 1 uchar 79 491 put Pth 2 uchar 79 492 put Pth 3 uchar 84 493 put Pth 4 uchar 88 494 put Pth 5 uchar 54 495 put Pth 6 uchar 52 496 put Pth 7 uchar 46 497 put Pth 8 uchar 69 498 put Pth 9 uchar 70 499 put Pth 10 uchar 73 500 501 fi = fopen Pth 0 11 0 0 502 if lt fi 0 503 exit 2 504 end 505 n = fseek int fi 0 2 506 if lt n 1 507 exit 3 508 end 509 q = fseek int fi 0 0 510 if lt q 0 511 exit 9 512 end 513 put Nes 0 ulong div add n 511 512 514 if gt get Nes 0 sub CLUS 4 515 exit 10 516 end 517 put Tv 2 ulong n 518 fspar 519 520 put Pth 0 uchar 105 521 put Pth 1 uchar 109 522 put Pth 2 uchar 97 523 put Pth 3 uchar 99 524 put Pth 4 uchar 45 525 put Pth 5 uchar 98 526 put Pth 6 uchar 111 527 put Pth 7 uchar 111 528 put Pth 8 uchar 116 529 put Pth 9 uchar 46 530 put Pth 10 uchar 105 531 put Pth 11 uchar 109 532 put Pth 12 uchar 103 533 534 fo = fopen Pth 0 13 577 420 535 if lt fo 0 536 exit 4 537 end 538 if lt ftrunc int fo long IMGLN 0 539 exit 5 540 end 541 542 mbrmk edit Sct 543 swr fo view Sct 544 entar edit Gpt 545 cc = gcrc view Gpt 546 gpthd edit Sct 1 2 BKLBA cc 547 swr fo view Sct 548 gwr fo view Gpt 549 550 seek fo ESPOF 551 bootsc edit Sct 552 swr fo view Sct 553 fsinf edit Sct get Tv 0 get Tv 1 554 swr fo view Sct 555 zsecs fo edit Sct 4 556 bootsc edit Sct 557 swr fo view Sct 558 fsinf edit Sct get Tv 0 get Tv 1 559 swr fo view Sct 560 zsecs fo edit Sct 24 561 562 i = ulong 0 563 while lt i FATS bound 1500 564 fats edit Sct i 565 swr fo view Sct 566 i = add i 1 567 end 568 i = ulong 0 569 while lt i FATS bound 1500 570 fats edit Sct i 571 swr fo view Sct 572 i = add i 1 573 end 574 575 rootd edit Sct 576 swr fo view Sct 577 efidr edit Sct 578 swr fo view Sct 579 bootd edit Sct 580 swr fo view Sct 581 582 datap int fi fo 583 startn edit Sct 584 swr fo view Sct 585 586 seek fo mul BKENT 512 587 gwr fo view Gpt 588 seek fo mul BKLBA 512 589 gpthd edit Sct BKLBA BKENT 1 cc 590 swr fo view Sct 591 592 if lt fsync int fo 0 593 exit 6 594 end 595 if lt fclose int fo 0 596 exit 7 597 end 598 if lt fclose int fi 0 599 exit 9 600 end 601 if ne get Err 0 0 602 exit 8 603 end 604 exit 0 605 end