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