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 / boot / netboot.om

288 lines · 5564 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/boot/netboot.om: Pure-Omega AIENOS Lab Netboot Wire Protocol (ANB1).
3note Converted from upstream aienux native/boot/netboot_proto.h and native/boot/netboot_proto.c.
4
5const NB_PORT_SERVER ushort 47032
6const NB_PORT_LOADER ushort 47033
7const NB_HDR ulong 24
8const NB_MIN ulong 32
9const NB_CHUNK uint 1024
10const NB_WINDOW ushort 16
11const NB_WAIT_MAX ushort 60
12const NB_TEXT_MAX ushort 96
13const NB_OBJECT_MIN uint 4097
14const NB_OBJECT_MAX uint 67108864
15const NB_MSG_MAX ulong 1056
16
17const NB_HELLO uchar 1
18const NB_OFFER uchar 2
19const NB_WAIT uchar 3
20const NB_GET uchar 4
21const NB_DATA uchar 5
22const NB_DONE uchar 6
23
24const NB_OK int 0
25const NB_ERR int -1
26
27text nb_magic "ANB1"
28
29fn nb_put16_be dst edit uchar * off ulong v ushort to void
30put dst off uchar and shr v 8 0xff
31put dst add off 1 uchar and v 0xff
32end
33
34fn nb_put32_be dst edit uchar * off ulong v uint to void
35put dst off uchar and shr v 24 0xff
36put dst add off 1 uchar and shr v 16 0xff
37put dst add off 2 uchar and shr v 8 0xff
38put dst add off 3 uchar and v 0xff
39end
40
41fn nb_get16_be src view uchar * off ulong to ushort
42b0 = ushort get src off
43b1 = ushort get src add off 1
44ret or shl b0 8 b1
45end
46
47fn nb_get32_be src view uchar * off ulong to uint
48b0 = uint get src off
49b1 = uint get src add off 1
50b2 = uint get src add off 2
51b3 = uint get src add off 3
52ret or or shl b0 24 shl b1 16 or shl b2 8 b3
53end
54
55fn nb_printable src view uchar * off ulong n ulong to bool
56i = ulong 0
57while lt i n bound 128
58ch = get src add off i
59if or lt ch 0x20 gt ch 0x7e
60ret false
61end
62i = add i 1
63end
64ret true
65end
66
67fn nb_chunks total uint to uint
68rem_c = rem total NB_CHUNK
69q = div total NB_CHUNK
70if ne rem_c 0
71ret add q 1
72end
73ret q
74end
75
76fn nb_fields_ok msg view NbMsg payload view uchar * to bool
77mt = get msg mtype
78if eq mt NB_HELLO
79ret ge len payload 6
80end
81if eq mt NB_OFFER
82tot = get msg total
83ret and ge tot NB_OBJECT_MIN le tot NB_OBJECT_MAX
84end
85if eq mt NB_WAIT
86sec = get msg seconds
87ret and ge sec 1 le sec NB_WAIT_MAX
88end
89if eq mt NB_GET
90cnt = get msg count
91off = get msg offset
92ret and ge cnt 1 and le cnt NB_WINDOW and eq rem off NB_CHUNK 0 lt off NB_OBJECT_MAX
93end
94if eq mt NB_DATA
95ml = get msg mlen
96off = get msg offset
97if or lt ml 1 gt ml ushort NB_CHUNK
98ret false
99end
100if or ne rem off NB_CHUNK 0 ge off NB_OBJECT_MAX
101ret false
102end
103ret ge len payload ulong ml
104end
105if eq mt NB_DONE
106st = get msg status
107ml = get msg mlen
108if or gt st 1 gt ml NB_TEXT_MAX
109ret false
110end
111if eq ml 0
112ret true
113end
114if lt len payload ulong ml
115ret false
116end
117ret nb_printable payload 0 ulong ml
118end
119ret false
120end
121
122fn nb_build msg view NbMsg nonce view uchar 16 payload view uchar * dst edit uchar * to ulong
123if not nb_fields_ok msg payload
124ret 0
125end
126mt = get msg mtype
127extra = ulong 0
128if or eq mt NB_DATA eq mt NB_DONE
129extra = ulong get msg mlen
130end
131n = add NB_MIN extra
132if lt len dst n
133ret 0
134end
135for i ulong 32
136put dst i 0
137end
138for i ulong 4
139put dst i get nb_magic i
140end
141put dst 4 mt
142for i ulong 16
143put dst add 8 i get nonce i
144end
145if eq mt NB_HELLO
146for i ulong 6
147put dst add 24 i get payload i
148end
149ret n
150end
151if eq mt NB_OFFER
152nb_put32_be dst 24 get msg total
153nb_put16_be dst 28 ushort NB_CHUNK
154ret n
155end
156if eq mt NB_WAIT
157nb_put16_be dst 24 get msg seconds
158ret n
159end
160if eq mt NB_GET
161nb_put32_be dst 24 get msg offset
162nb_put16_be dst 28 get msg count
163ret n
164end
165if eq mt NB_DATA
166nb_put32_be dst 24 get msg offset
167nb_put16_be dst 28 get msg mlen
168end
169if eq mt NB_DONE
170put dst 24 get msg status
171end
172i = ulong 0
173while lt i extra bound 1024
174put dst add NB_MIN i get payload i
175i = add i 1
176end
177ret n
178end
179
180fn nb_parse_body src view uchar * msg edit NbMsg extra_dst edit uchar * to int
181slen = len src
182mt = get msg mtype
183if eq mt NB_HELLO
184if or ne slen NB_MIN not boot_all_zero src 30 2
185ret NB_ERR
186end
187if lt len extra_dst 6
188ret NB_ERR
189end
190for i ulong 6
191put extra_dst i get src add 24 i
192end
193ret NB_OK
194end
195if eq mt NB_OFFER
196if or ne slen NB_MIN or ne nb_get16_be src 28 ushort NB_CHUNK not boot_all_zero src 30 2
197ret NB_ERR
198end
199put msg total nb_get32_be src 24
200ret NB_OK
201end
202if eq mt NB_WAIT
203if or ne slen NB_MIN not boot_all_zero src 26 6
204ret NB_ERR
205end
206put msg seconds nb_get16_be src 24
207ret NB_OK
208end
209if eq mt NB_GET
210if or ne slen NB_MIN not boot_all_zero src 30 2
211ret NB_ERR
212end
213put msg offset nb_get32_be src 24
214put msg count nb_get16_be src 28
215ret NB_OK
216end
217if eq mt NB_DATA
218if not boot_all_zero src 30 2
219ret NB_ERR
220end
221put msg offset nb_get32_be src 24
222ml = nb_get16_be src 28
223put msg mlen ml
224if ne slen add NB_MIN ulong ml
225ret NB_ERR
226end
227if lt len extra_dst ulong ml
228ret NB_ERR
229end
230i = ulong 0
231while lt i ulong ml bound 1024
232put extra_dst i get src add NB_MIN i
233i = add i 1
234end
235ret NB_OK
236end
237if eq mt NB_DONE
238if not boot_all_zero src 25 7
239ret NB_ERR
240end
241put msg status get src 24
242dlen = sub slen NB_MIN
243if gt dlen ulong NB_TEXT_MAX
244ret NB_ERR
245end
246put msg mlen ushort dlen
247if lt len extra_dst dlen
248ret NB_ERR
249end
250i = ulong 0
251while lt i dlen bound 128
252put extra_dst i get src add NB_MIN i
253i = add i 1
254end
255ret NB_OK
256end
257ret NB_ERR
258end
259
260fn nb_parse src view uchar * msg edit NbMsg nonce_dst edit uchar 16 extra_dst edit uchar * to int
261slen = len src
262if or lt slen NB_MIN gt slen NB_MSG_MAX
263ret NB_ERR
264end
265if not boot_eq_bytes part src 0 4 nb_magic
266ret NB_ERR
267end
268if not boot_all_zero src 5 3
269ret NB_ERR
270end
271put msg mtype get src 4
272put msg status 0
273put msg seconds 0
274put msg count 0
275put msg mlen 0
276put msg total 0
277put msg offset 0
278for i ulong 16
279put nonce_dst i get src add 8 i
280end
281if ne nb_parse_body src msg extra_dst NB_OK
282ret NB_ERR
283end
284if not nb_fields_ok msg extra_dst
285ret NB_ERR
286end
287ret NB_OK
288end