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 / atlas / main.om

471 lines · 8881 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/atlas/main.om: Atlas Stage-0 Boot Verification CLI.
3note Reads either a raw binary Atlas boot image (64-byte header + stage payload) or
4note an ATLAS_MANIFEST text manifest on stdin, verifies alignment, bounds, epoch
5note anti-rollback, memory-map disjointness, and constant-time SHA-256 digest.
6note Emits canonical ATLAS checkpoints and verified handoff descriptor, or halts
7note fail-closed with nonzero exit status on any violation.
8note Compose with: si/directives/om2.om aienux/atlas/lib.om aienux/atlas/main.om
9
10text m_awaken "ATLAS: AWAKEN\n"
11text m_verify "ATLAS: VERIFY\n"
12text m_handoff "ATLAS: HANDOFF\n"
13text m_refuse "ATLAS: REFUSE\n"
14text m_halt "ATLAS: HALT code="
15text m_reason " reason="
16text m_nl "\n"
17
18text m_r_short "short_header"
19text m_r_magic "bad_magic"
20text m_r_ver "bad_version"
21text m_r_slen "invalid_stage_len"
22text m_r_align "bad_alignment"
23text m_r_entry "entry_out_of_bounds"
24text m_r_trunc "truncated_payload"
25text m_r_roll "epoch_rollback"
26text m_r_mmap "memmap_violation"
27text m_r_hash "digest_mismatch"
28text m_r_parse "malformed_manifest"
29text m_r_io "stdin_read_error"
30
31text m_k_desc "desc_addr="
32text m_k_ram " ram_base="
33text m_k_rsz " ram_size="
34text m_k_uart "\nuart_base="
35text m_k_pbase " payload_base="
36text m_k_psize " payload_size="
37text m_k_entry "\nentry_addr="
38text m_k_epoch " epoch="
39text m_k_cookie " cookie="
40text m_k_sha "\nsha256="
41
42text m_hdr_man "ATLAS_MANIFEST"
43text m_arg_me "--min-epoch"
44
45text m_p_ver "version="
46text m_p_ep "epoch="
47text m_p_mep "min_epoch="
48text m_p_eoff "entry_off="
49text m_p_sha "sha256="
50text m_p_pay "payload_hex="
51
52fn atlas_eq_slice a view uchar * b view uchar * to bool
53if ne len a len b
54ret false
55end
56i = ulong 0
57n = len a
58while lt i n bound 65536
59if ne get a i get b i
60ret false
61end
62i = add i 1
63end
64ret true
65end
66
67fn atlas_starts_with a view uchar * pfx view uchar * to bool
68if lt len a len pfx
69ret false
70end
71ret atlas_eq_slice part a 0 len pfx pfx
72end
73
74fn atlas_parse_u64 s view uchar * dst edit ulong 1 to bool
75n = len s
76if eq n 0
77ret false
78end
79if gt n 20
80ret false
81end
82acc = ulong 0
83i = ulong 0
84while lt i n bound 24
85c = get s i
86if lt c 48
87ret false
88end
89if gt c 57
90ret false
91end
92d = ulong sub c 48
93if gt acc 1844674407370955161
94ret false
95end
96m = mul acc 10
97if atlas_add_overflow m d
98ret false
99end
100acc = add m d
101i = add i 1
102end
103put dst 0 acc
104ret true
105end
106
107fn atlas_print_reason fd int rc uint to void
108ok = false
109if eq rc ATLAS_ERR_SHORT_HDR
110ok = atlas_write_all fd m_r_short
111else
112if eq rc ATLAS_ERR_MAGIC
113ok = atlas_write_all fd m_r_magic
114else
115if eq rc ATLAS_ERR_VERSION
116ok = atlas_write_all fd m_r_ver
117else
118if eq rc ATLAS_ERR_STAGE_LEN
119ok = atlas_write_all fd m_r_slen
120else
121if eq rc ATLAS_ERR_ALIGN
122ok = atlas_write_all fd m_r_align
123else
124if eq rc ATLAS_ERR_ENTRY_OOB
125ok = atlas_write_all fd m_r_entry
126else
127if eq rc ATLAS_ERR_TRUNCATED
128ok = atlas_write_all fd m_r_trunc
129else
130if eq rc ATLAS_ERR_ROLLBACK
131ok = atlas_write_all fd m_r_roll
132else
133if eq rc ATLAS_ERR_MEMMAP
134ok = atlas_write_all fd m_r_mmap
135else
136if eq rc ATLAS_ERR_DIGEST
137ok = atlas_write_all fd m_r_hash
138else
139ok = atlas_write_all fd m_r_parse
140end
141end
142end
143end
144end
145end
146end
147end
148end
149end
150if not ok
151exit 2
152end
153end
154
155fn atlas_print_halt rc uint to void
156ok = atlas_write_all 1 m_refuse
157if not ok
158exit 2
159end
160ok = atlas_write_all 1 m_halt
161if not ok
162exit 2
163end
164ok = atlas_write_u64 1 ulong rc
165if not ok
166exit 2
167end
168ok = atlas_write_all 1 m_reason
169if not ok
170exit 2
171end
172atlas_print_reason 1 rc
173ok = atlas_write_all 1 m_nl
174if not ok
175exit 2
176end
177end
178
179fn atlas_print_handoff desc view AtlasBootDesc comp view uchar 32 to bool
180if not atlas_write_all 1 m_handoff
181ret false
182end
183if not atlas_write_all 1 m_k_desc
184ret false
185end
186if not atlas_write_hex64 1 get desc desc_addr
187ret false
188end
189if not atlas_write_all 1 m_k_ram
190ret false
191end
192if not atlas_write_hex64 1 get desc ram_base
193ret false
194end
195if not atlas_write_all 1 m_k_rsz
196ret false
197end
198if not atlas_write_hex64 1 get desc ram_size
199ret false
200end
201if not atlas_write_all 1 m_k_uart
202ret false
203end
204if not atlas_write_hex64 1 get desc uart_base
205ret false
206end
207if not atlas_write_all 1 m_k_pbase
208ret false
209end
210if not atlas_write_hex64 1 get desc payload_base
211ret false
212end
213if not atlas_write_all 1 m_k_psize
214ret false
215end
216if not atlas_write_u64 1 get desc payload_size
217ret false
218end
219if not atlas_write_all 1 m_k_entry
220ret false
221end
222if not atlas_write_hex64 1 get desc entry_addr
223ret false
224end
225if not atlas_write_all 1 m_k_epoch
226ret false
227end
228if not atlas_write_u64 1 get desc epoch
229ret false
230end
231if not atlas_write_all 1 m_k_cookie
232ret false
233end
234if not atlas_write_hex64 1 get desc cookie
235ret false
236end
237if not atlas_write_all 1 m_k_sha
238ret false
239end
240if not atlas_write_digest 1 comp
241ret false
242end
243ret atlas_write_all 1 m_nl
244end
245
246fn atlas_read_stdin dst edit uchar * got edit ulong 1 to bool
247cap = len dst
248tot = ulong 0
249while lt tot cap bound 65536
250k = read 0 dst tot sub cap tot
251if lt k 0
252ret false
253end
254if eq k 0
255break
256end
257tot = add tot ulong k
258end
259put got 0 tot
260ret true
261end
262
263fn atlas_parse_line_meta line view uchar * hdr edit AtlasHeader exp edit uchar 32 meta edit ulong 4 to int
264note meta[0]=total_len, meta[1]=min_epoch, meta[2]=has_sha, meta[3]=has_pay
265tmp = new ulong 1 0
266llen = len line
267if atlas_starts_with line m_p_ver
268if not atlas_parse_u64 part line 8 sub llen 8 tmp
269ret -1
270end
271if gt get tmp 0 0xffffffff
272ret -1
273end
274put hdr ver uint get tmp 0
275ret 1
276end
277if atlas_starts_with line m_p_ep
278if not atlas_parse_u64 part line 6 sub llen 6 tmp
279ret -1
280end
281put hdr epoch get tmp 0
282ret 1
283end
284if atlas_starts_with line m_p_mep
285if not atlas_parse_u64 part line 10 sub llen 10 tmp
286ret -1
287end
288put meta 1 get tmp 0
289ret 1
290end
291if atlas_starts_with line m_p_eoff
292if not atlas_parse_u64 part line 10 sub llen 10 tmp
293ret -1
294end
295put hdr entry_off get tmp 0
296ret 1
297end
298if atlas_starts_with line m_p_sha
299if ne sub llen 7 64
300ret -1
301end
302if not atlas_parse_hex_bytes part line 7 64 exp
303ret -1
304end
305put meta 2 1
306ret 1
307end
308ret 0
309end
310
311fn atlas_parse_line_pay line view uchar * img edit uchar * hdr edit AtlasHeader meta edit ulong 4 to int
312llen = len line
313if not atlas_starts_with line m_p_pay
314ret 0
315end
316hlen = sub llen 12
317if ne and hlen 1 0
318ret -1
319end
320plen = shr hlen 1
321if gt add ATLAS_HDR_SIZE plen len img
322ret -1
323end
324if not atlas_parse_hex_bytes part line 12 hlen part img ATLAS_HDR_SIZE plen
325ret -1
326end
327put hdr stage_len plen
328put meta 0 add ATLAS_HDR_SIZE plen
329put meta 3 1
330ret 1
331end
332
333fn atlas_find_nl src view uchar * start ulong to ulong
334n = len src
335p = start
336while lt p n bound 65536
337if eq get src p 10
338ret p
339end
340p = add p 1
341end
342ret n
343end
344
345fn atlas_parse_manifest src view uchar * img edit uchar * meta edit ulong 4 to bool
346exp = new uchar 32 0
347hdr = new AtlasHeader ATLAS_MAGIC_LE ATLAS_HDR_VER 0 0 0
348n = len src
349pos = atlas_find_nl src 0
350if lt pos n
351pos = add pos 1
352end
353while lt pos n bound 1024
354lend = atlas_find_nl src pos
355nextp = lend
356if lt nextp n
357nextp = add nextp 1
358end
359if gt lend pos
360if eq get src sub lend 1 13
361lend = sub lend 1
362end
363end
364llen = sub lend pos
365if gt llen 0
366r1 = atlas_parse_line_meta part src pos llen hdr exp meta
367if lt r1 0
368ret false
369end
370if eq r1 0
371r2 = atlas_parse_line_pay part src pos llen img hdr meta
372if lt r2 0
373ret false
374end
375end
376end
377pos = nextp
378end
379if ne get meta 2 1
380ret false
381end
382if ne get meta 3 1
383ret false
384end
385ret atlas_header_encode img hdr exp
386end
387
388fn atlas_read_min_epoch dst edit ulong 1 to bool
389abuf = new uchar 64 0
390vbuf = new uchar 64 0
391put dst 0 0
392ac = argcnt
393if le ac 1
394ret true
395end
396if ne ac 3
397ret false
398end
399k1 = argread 1 abuf 0 64
400if le k1 0
401ret false
402end
403if not atlas_eq_slice part abuf 0 ulong k1 m_arg_me
404ret false
405end
406k2 = argread 2 vbuf 0 64
407if le k2 0
408ret false
409end
410ret atlas_parse_u64 part vbuf 0 ulong k2 dst
411end
412
413fn atlas_exec_image img view uchar * min_ep ulong to int
414hdr = new AtlasHeader 0 0 0 0 0
415desc = new AtlasBootDesc 0 0 0 0 0 0 0 0 0 0 0 1
416comp = new uchar 32 0
417wire = new uchar 64 0
418rc = atlas_verify_image img min_ep hdr desc comp
419if ne rc ATLAS_OK
420atlas_print_halt rc
421ret int rc
422end
423if not atlas_emit_desc_bytes desc wire
424atlas_print_halt ATLAS_ERR_MEMMAP
425ret int ATLAS_ERR_MEMMAP
426end
427if not atlas_print_handoff desc comp
428ret 2
429end
430ret 0
431end
432
433fn atlas_exec_manifest raw view uchar * min_ep ulong to int
434img = new uchar 65536 0
435meta = new ulong 4 0
436put meta 1 min_ep
437if not atlas_parse_manifest raw img meta
438atlas_print_halt 11
439ret 11
440end
441ret atlas_exec_image part img 0 get meta 0 get meta 1
442end
443
444fn main to int
445raw = new uchar 65536 0
446got = new ulong 1 0
447mep = new ulong 1 0
448if not atlas_write_all 1 m_awaken
449ret 2
450end
451if not atlas_read_min_epoch mep
452atlas_print_halt 11
453ret 11
454end
455if not atlas_read_stdin raw got
456ok = atlas_write_all 2 m_r_io
457if ok
458ret 12
459end
460ret 12
461end
462if not atlas_write_all 1 m_verify
463ret 2
464end
465rlen = get got 0
466min_ep = get mep 0
467if atlas_starts_with part raw 0 rlen m_hdr_man
468ret atlas_exec_manifest part raw 0 rlen min_ep
469end
470ret atlas_exec_image part raw 0 rlen min_ep
471end