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 / tools / spark-image / spark-image.om

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