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

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