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