Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/aienux/boot/slot.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / aienux / boot / slot.om` 569 lines ยท 13731 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aienux/boot/slot.om: Pure-Omega AIENOS A/B Native Image Slots (manifest, control record, 3 note selection state machine, transitions, boot record SHA-256 seal/check, and kernel slot action). 4 note Converted from upstream aienux native/boot/slot_format.h and native/boot/slot_select.c. 5 6 const SLOT_UNIT ulong 4096 7 const SLOT_CONTROL_UNITS ulong 4 8 const SLOT_FIRST_UNIT ulong 4 9 const SLOT_COUNT ulong 2 10 const SLOT_NONE uchar 0xff 11 const SLOT_MAX_TRIES uchar 3 12 const SLOT_MIN_CAP_UNITS ulong 2 13 14 note Manifest constants (4096 bytes) 15 const SLOT_M_VERSION uint 1 16 const SLOT_M_PAYLOAD_BOOTAA64 uint 1 17 const SLOT_M_LOADER_ABI uint 1 18 const SLOT_M_KERNEL_ABI uint 3 19 const SLOT_M_TBS_BYTES ulong 448 20 const SLOT_M_COMMIT_CHARS ulong 40 21 const SLOT_M_PROV_CHARS ulong 64 22 23 const SLOT_M_OFF_MAGIC ulong 0 24 const SLOT_M_OFF_VER ulong 8 25 const SLOT_M_OFF_PAYLOAD ulong 12 26 const SLOT_M_OFF_LABI ulong 16 27 const SLOT_M_OFF_KABI ulong 20 28 const SLOT_M_OFF_GEN ulong 24 29 const SLOT_M_OFF_PGEN ulong 32 30 const SLOT_M_OFF_MINRB ulong 40 31 const SLOT_M_OFF_IMGLEN ulong 48 32 const SLOT_M_OFF_IMGSHA ulong 56 33 const SLOT_M_OFF_KSHA ulong 120 34 const SLOT_M_OFF_SIGNER ulong 184 35 const SLOT_M_OFF_COMMIT ulong 216 36 const SLOT_M_OFF_PROV ulong 256 37 const SLOT_M_OFF_TS ulong 320 38 const SLOT_M_OFF_FLAGS ulong 328 39 const SLOT_M_OFF_RSVD0 ulong 332 40 const SLOT_M_OFF_RSVD1 ulong 336 41 const SLOT_M_OFF_SIG ulong 448 42 const SLOT_M_OFF_PAD ulong 512 43 44 note Control record constants (4096 bytes) 45 const SLOT_C_VERSION uint 1 46 const SLOT_C_FLAG_CAND_FAILED uchar 1 47 const SLOT_C_OFF_MAGIC ulong 0 48 const SLOT_C_OFF_VER ulong 8 49 const SLOT_C_OFF_ACTIVE ulong 12 50 const SLOT_C_OFF_CAND ulong 13 51 const SLOT_C_OFF_TRIES ulong 14 52 const SLOT_C_OFF_FLAGS ulong 15 53 const SLOT_C_OFF_FLOOR ulong 16 54 const SLOT_C_OFF_LASTGOOD ulong 24 55 const SLOT_C_OFF_RSVD ulong 32 56 57 note Boot record constants (256 bytes, SHA-256 over [0, 224)) 58 const SLOT_B_VERSION uint 1 59 const SLOT_B_BYTES ulong 256 60 const SLOT_B_SIGNED ulong 224 61 const SLOT_B_OFF_MAGIC ulong 0 62 const SLOT_B_OFF_VER ulong 8 63 const SLOT_B_OFF_SLOT ulong 12 64 const SLOT_B_OFF_REASON ulong 13 65 const SLOT_B_OFF_TRIES ulong 14 66 const SLOT_B_OFF_CTL_PRES ulong 15 67 const SLOT_B_OFF_CTL_SEQ ulong 16 68 const SLOT_B_OFF_GEN ulong 24 69 const SLOT_B_OFF_PGEN ulong 32 70 const SLOT_B_OFF_IMGSHA ulong 40 71 const SLOT_B_OFF_COMMIT ulong 104 72 const SLOT_B_OFF_RSVD ulong 144 73 const SLOT_B_OFF_DIGEST ulong 224 74 75 note Decision reason codes 76 const SLOT_R_NONE uint 0 77 const SLOT_R_CANDIDATE_TRY uint 1 78 const SLOT_R_ACTIVE uint 2 79 const SLOT_R_CANDIDATE_EXHAUSTED uint 3 80 const SLOT_R_CANDIDATE_INVALID uint 4 81 const SLOT_R_ACTIVE_INVALID_FALLBACK uint 5 82 const SLOT_R_NO_CONTROL uint 6 83 const SLOT_R_RECOVERY_REQUIRED uint 7 84 85 note Kernel slot action codes 86 const SLOT_A_NONE uint 0 87 const SLOT_A_PROMOTE uint 1 88 const SLOT_A_ABANDON uint 2 89 const SLOT_A_WAIT uint 3 90 const SLOT_A_REFUSE uint 4 91 92 note Error codes 93 const SLOT_OK int 0 94 const SLOT_ERR_SHORT int -1 95 const SLOT_ERR_MAGIC int -2 96 const SLOT_ERR_VER int -3 97 const SLOT_ERR_PAYLOAD int -4 98 const SLOT_ERR_ABI int -5 99 const SLOT_ERR_GEN int -6 100 const SLOT_ERR_IMGLEN int -7 101 const SLOT_ERR_SHA_MISMATCH int -8 102 const SLOT_ERR_TS int -9 103 const SLOT_ERR_FLAGS int -10 104 const SLOT_ERR_RSVD int -11 105 const SLOT_ERR_COMMIT int -12 106 const SLOT_ERR_PROV int -13 107 const SLOT_ERR_RANGE int -14 108 const SLOT_ERR_STATE int -15 109 const SLOT_ERR_FLOOR int -16 110 const SLOT_ERR_DIGEST int -17 111 112 text slot_m_magic "AIENSLT1" 113 text slot_c_magic "AIENSLC1" 114 text slot_b_magic "AIENSLB1" 115 116 fn slot_cap_units part_units ulong to ulong 117 if lt part_units SLOT_FIRST_UNIT 118 ret 0 119 end 120 ret div sub part_units SLOT_FIRST_UNIT SLOT_COUNT 121 end 122 123 fn slot_manifest_unit slot_idx ulong cap_units ulong to ulong 124 ret add SLOT_FIRST_UNIT mul slot_idx cap_units 125 end 126 127 fn slot_is_lower_hex ch uchar to bool 128 if and ge ch 48 le ch 57 129 ret true 130 end 131 if and ge ch 97 le ch 102 132 ret true 133 end 134 ret false 135 end 136 137 fn slot_copy_bytes dst edit uchar * src view uchar * n ulong to void 138 i = ulong 0 139 while lt i n bound 4096 140 put dst i get src i 141 i = add i 1 142 end 143 end 144 145 fn slot_zero_bytes dst edit uchar * n ulong to void 146 i = ulong 0 147 while lt i n bound 4096 148 put dst i 0 149 i = add i 1 150 end 151 end 152 153 fn slot_manifest_check buf view uchar * to int 154 if lt len buf SLOT_UNIT 155 ret SLOT_ERR_SHORT 156 end 157 if not boot_eq_bytes part buf SLOT_M_OFF_MAGIC 8 slot_m_magic 158 ret SLOT_ERR_MAGIC 159 end 160 if ne boot_rd32_le buf SLOT_M_OFF_VER SLOT_M_VERSION 161 ret SLOT_ERR_VER 162 end 163 if ne boot_rd32_le buf SLOT_M_OFF_PAYLOAD SLOT_M_PAYLOAD_BOOTAA64 164 ret SLOT_ERR_PAYLOAD 165 end 166 if ne boot_rd32_le buf SLOT_M_OFF_LABI SLOT_M_LOADER_ABI 167 ret SLOT_ERR_ABI 168 end 169 if ne boot_rd32_le buf SLOT_M_OFF_KABI SLOT_M_KERNEL_ABI 170 ret SLOT_ERR_ABI 171 end 172 gen = boot_rd64_le buf SLOT_M_OFF_GEN 173 pgen = boot_rd64_le buf SLOT_M_OFF_PGEN 174 minrb = boot_rd64_le buf SLOT_M_OFF_MINRB 175 if or eq gen 0 or gt pgen gen gt minrb gen 176 ret SLOT_ERR_GEN 177 end 178 if eq boot_rd64_le buf SLOT_M_OFF_IMGLEN 0 179 ret SLOT_ERR_IMGLEN 180 end 181 if not boot_eq_within buf SLOT_M_OFF_KSHA SLOT_M_OFF_IMGSHA 64 182 ret SLOT_ERR_SHA_MISMATCH 183 end 184 if eq boot_rd64_le buf SLOT_M_OFF_TS 0 185 ret SLOT_ERR_TS 186 end 187 if ne boot_rd32_le buf SLOT_M_OFF_FLAGS 0 188 ret SLOT_ERR_FLAGS 189 end 190 if ne boot_rd32_le buf SLOT_M_OFF_RSVD0 0 191 ret SLOT_ERR_RSVD 192 end 193 if not boot_all_zero buf SLOT_M_OFF_RSVD1 112 194 ret SLOT_ERR_RSVD 195 end 196 if not boot_all_zero buf SLOT_M_OFF_PAD 3584 197 ret SLOT_ERR_RSVD 198 end 199 i = ulong 0 200 while lt i SLOT_M_COMMIT_CHARS bound 40 201 if not slot_is_lower_hex get buf add SLOT_M_OFF_COMMIT i 202 ret SLOT_ERR_COMMIT 203 end 204 i = add i 1 205 end 206 ended = false 207 j = ulong 0 208 while lt j SLOT_M_PROV_CHARS bound 64 209 ch = get buf add SLOT_M_OFF_PROV j 210 if eq ch 0 211 ended = true 212 else 213 if or ended or lt ch 0x20 gt ch 0x7e 214 ret SLOT_ERR_PROV 215 end 216 end 217 j = add j 1 218 end 219 ret SLOT_OK 220 end 221 222 fn slot_control_check buf view uchar * to int 223 if lt len buf SLOT_UNIT 224 ret SLOT_ERR_SHORT 225 end 226 if not boot_eq_bytes part buf SLOT_C_OFF_MAGIC 8 slot_c_magic 227 ret SLOT_ERR_MAGIC 228 end 229 if ne boot_rd32_le buf SLOT_C_OFF_VER SLOT_C_VERSION 230 ret SLOT_ERR_VER 231 end 232 act = get buf SLOT_C_OFF_ACTIVE 233 cand = get buf SLOT_C_OFF_CAND 234 tries = get buf SLOT_C_OFF_TRIES 235 flags = get buf SLOT_C_OFF_FLAGS 236 if ge ulong act SLOT_COUNT 237 ret SLOT_ERR_RANGE 238 end 239 if and ne cand SLOT_NONE ge ulong cand SLOT_COUNT 240 ret SLOT_ERR_RANGE 241 end 242 if eq cand act 243 ret SLOT_ERR_STATE 244 end 245 if gt tries SLOT_MAX_TRIES 246 ret SLOT_ERR_RANGE 247 end 248 if and eq cand SLOT_NONE ne tries 0 249 ret SLOT_ERR_STATE 250 end 251 if ne and flags xor uchar 0xff SLOT_C_FLAG_CAND_FAILED 0 252 ret SLOT_ERR_FLAGS 253 end 254 floor = boot_rd64_le buf SLOT_C_OFF_FLOOR 255 lg = boot_rd64_le buf SLOT_C_OFF_LASTGOOD 256 if and ne lg 0 lt lg floor 257 ret SLOT_ERR_FLOOR 258 end 259 if not boot_all_zero buf SLOT_C_OFF_RSVD 4064 260 ret SLOT_ERR_RSVD 261 end 262 ret SLOT_OK 263 end 264 265 fn slot_control_init dst edit uchar * active_slot uchar to void 266 slot_zero_bytes dst SLOT_UNIT 267 for i ulong 8 268 put dst i get slot_c_magic i 269 end 270 boot_wr32_le dst SLOT_C_OFF_VER SLOT_C_VERSION 271 put dst SLOT_C_OFF_ACTIVE and active_slot 1 272 put dst SLOT_C_OFF_CAND SLOT_NONE 273 put dst SLOT_C_OFF_TRIES 0 274 end 275 276 fn slot_stage_candidate ctl edit uchar * target uchar gen ulong to int 277 rc = slot_control_check ctl 278 if ne rc SLOT_OK 279 ret rc 280 end 281 if ge ulong target SLOT_COUNT 282 ret SLOT_ERR_RANGE 283 end 284 if eq target get ctl SLOT_C_OFF_ACTIVE 285 ret SLOT_ERR_STATE 286 end 287 floor = boot_rd64_le ctl SLOT_C_OFF_FLOOR 288 if lt gen floor 289 ret SLOT_ERR_FLOOR 290 end 291 lg = boot_rd64_le ctl SLOT_C_OFF_LASTGOOD 292 if and ne lg 0 le gen lg 293 ret SLOT_ERR_FLOOR 294 end 295 put ctl SLOT_C_OFF_CAND target 296 put ctl SLOT_C_OFF_TRIES SLOT_MAX_TRIES 297 fl = get ctl SLOT_C_OFF_FLAGS 298 put ctl SLOT_C_OFF_FLAGS and fl xor uchar 0xff SLOT_C_FLAG_CAND_FAILED 299 ret SLOT_OK 300 end 301 302 fn slot_promote ctl edit uchar * booted uchar gen ulong pgen ulong to int 303 rc = slot_control_check ctl 304 if ne rc SLOT_OK 305 ret rc 306 end 307 if ge ulong booted SLOT_COUNT 308 ret SLOT_ERR_RANGE 309 end 310 cand = get ctl SLOT_C_OFF_CAND 311 if or eq cand SLOT_NONE ne booted cand 312 ret SLOT_ERR_STATE 313 end 314 floor = boot_rd64_le ctl SLOT_C_OFF_FLOOR 315 if lt gen floor 316 ret SLOT_ERR_FLOOR 317 end 318 if gt pgen gen 319 ret SLOT_ERR_GEN 320 end 321 put ctl SLOT_C_OFF_ACTIVE booted 322 put ctl SLOT_C_OFF_CAND SLOT_NONE 323 put ctl SLOT_C_OFF_TRIES 0 324 fl = get ctl SLOT_C_OFF_FLAGS 325 put ctl SLOT_C_OFF_FLAGS and fl xor uchar 0xff SLOT_C_FLAG_CAND_FAILED 326 boot_wr64_le ctl SLOT_C_OFF_LASTGOOD gen 327 if gt pgen floor 328 boot_wr64_le ctl SLOT_C_OFF_FLOOR pgen 329 end 330 ret SLOT_OK 331 end 332 333 fn slot_abandon_candidate ctl edit uchar * to int 334 rc = slot_control_check ctl 335 if ne rc SLOT_OK 336 ret rc 337 end 338 if eq get ctl SLOT_C_OFF_CAND SLOT_NONE 339 ret SLOT_ERR_STATE 340 end 341 put ctl SLOT_C_OFF_CAND SLOT_NONE 342 put ctl SLOT_C_OFF_TRIES 0 343 fl = get ctl SLOT_C_OFF_FLAGS 344 put ctl SLOT_C_OFF_FLAGS or fl SLOT_C_FLAG_CAND_FAILED 345 ret SLOT_OK 346 end 347 348 fn slot_eval_usable ev view SlotEval floor ulong to bool 349 if eq get ev valid 0 350 ret false 351 end 352 ret ge get ev generation floor 353 end 354 355 fn slot_select_apply_ctl ctl view uchar 4096 ev0 view SlotEval ev1 view SlotEval dec edit SlotDecision ctl_dst edit uchar 4096 to void 356 floor = boot_rd64_le ctl SLOT_C_OFF_FLOOR 357 act = get ctl SLOT_C_OFF_ACTIVE 358 cand = get ctl SLOT_C_OFF_CAND 359 tries = get ctl SLOT_C_OFF_TRIES 360 act_u = slot_eval_usable ev0 floor 361 oth_u = slot_eval_usable ev1 floor 362 if eq act 1 363 act_u = slot_eval_usable ev1 floor 364 oth_u = slot_eval_usable ev0 floor 365 end 366 if ne cand SLOT_NONE 367 cand_u = slot_eval_usable ev0 floor 368 if eq cand 1 369 cand_u = slot_eval_usable ev1 floor 370 end 371 if and gt tries 0 cand_u 372 put dec boot_slot int cand 373 put dec reason SLOT_R_CANDIDATE_TRY 374 put dec loader_write 1 375 slot_copy_bytes ctl_dst ctl SLOT_UNIT 376 put ctl_dst SLOT_C_OFF_TRIES sub tries 1 377 ret 378 end 379 if act_u 380 put dec boot_slot int act 381 if eq tries 0 382 put dec reason SLOT_R_CANDIDATE_EXHAUSTED 383 else 384 put dec reason SLOT_R_CANDIDATE_INVALID 385 end 386 ret 387 end 388 end 389 if act_u 390 put dec boot_slot int act 391 put dec reason SLOT_R_ACTIVE 392 ret 393 end 394 other = xor act 1 395 if and oth_u ne cand other 396 put dec boot_slot int other 397 put dec reason SLOT_R_ACTIVE_INVALID_FALLBACK 398 ret 399 end 400 end 401 402 fn slot_select has_ctl bool ctl view uchar 4096 ev0 view SlotEval ev1 view SlotEval dec edit SlotDecision ctl_dst edit uchar 4096 to void 403 put dec boot_slot -1 404 put dec reason SLOT_R_RECOVERY_REQUIRED 405 put dec loader_write 0 406 slot_zero_bytes ctl_dst SLOT_UNIT 407 if not has_ctl 408 ret 409 end 410 if ne slot_control_check ctl SLOT_OK 411 ret 412 end 413 slot_select_apply_ctl ctl ev0 ev1 dec ctl_dst 414 end 415 416 fn slot_boot_seal buf edit uchar * to void 417 dig = new uchar 32 0 418 msg = new uchar 224 0 419 for i ulong 224 420 put msg i get buf i 421 end 422 dlen = sha2_digest 256 msg dig 423 if eq dlen 32 424 for i ulong 32 425 put buf add SLOT_B_OFF_DIGEST i get dig i 426 end 427 end 428 end 429 430 fn slot_boot_check buf view uchar * to int 431 if lt len buf SLOT_B_BYTES 432 ret SLOT_ERR_SHORT 433 end 434 if not boot_eq_bytes part buf SLOT_B_OFF_MAGIC 8 slot_b_magic 435 ret SLOT_ERR_MAGIC 436 end 437 if ne boot_rd32_le buf SLOT_B_OFF_VER SLOT_B_VERSION 438 ret SLOT_ERR_VER 439 end 440 msg = new uchar 224 0 441 dig = new uchar 32 0 442 for i ulong 224 443 put msg i get buf i 444 end 445 dlen = sha2_digest 256 msg dig 446 if ne dlen 32 447 ret SLOT_ERR_DIGEST 448 end 449 if not boot_eq_bytes dig part buf SLOT_B_OFF_DIGEST 32 450 ret SLOT_ERR_DIGEST 451 end 452 bslot = get buf SLOT_B_OFF_SLOT 453 reason = uint get buf SLOT_B_OFF_REASON 454 tries = get buf SLOT_B_OFF_TRIES 455 cpres = get buf SLOT_B_OFF_CTL_PRES 456 cseq = boot_rd64_le buf SLOT_B_OFF_CTL_SEQ 457 gen = boot_rd64_le buf SLOT_B_OFF_GEN 458 pgen = boot_rd64_le buf SLOT_B_OFF_PGEN 459 if ge ulong bslot SLOT_COUNT 460 ret SLOT_ERR_RANGE 461 end 462 if or le reason SLOT_R_NONE ge reason SLOT_R_RECOVERY_REQUIRED 463 ret SLOT_ERR_RANGE 464 end 465 if gt cpres 1 466 ret SLOT_ERR_RANGE 467 end 468 if gt tries SLOT_MAX_TRIES 469 ret SLOT_ERR_RANGE 470 end 471 if or eq gen 0 gt pgen gen 472 ret SLOT_ERR_GEN 473 end 474 if not boot_all_zero buf SLOT_B_OFF_RSVD 80 475 ret SLOT_ERR_RSVD 476 end 477 if eq cpres 0 478 if or ne reason SLOT_R_NO_CONTROL ne cseq 0 479 ret SLOT_ERR_STATE 480 end 481 else 482 if eq reason SLOT_R_NO_CONTROL 483 ret SLOT_ERR_STATE 484 end 485 end 486 ret SLOT_OK 487 end 488 489 fn slot_brec_matches_man brec view uchar 256 man view uchar 4096 to bool 490 if ne boot_rd64_le man SLOT_M_OFF_GEN boot_rd64_le brec SLOT_B_OFF_GEN 491 ret false 492 end 493 if ne boot_rd64_le man SLOT_M_OFF_PGEN boot_rd64_le brec SLOT_B_OFF_PGEN 494 ret false 495 end 496 ret boot_eq_bytes part man SLOT_M_OFF_IMGSHA 64 part brec SLOT_B_OFF_IMGSHA 64 497 end 498 499 fn slot_kernel_action_inner brec view uchar 256 ctl view uchar 4096 man view uchar 4096 healthy bool ctl_dst edit uchar 4096 to uint 500 if ne slot_boot_check brec SLOT_OK 501 ret SLOT_A_REFUSE 502 end 503 if ne slot_control_check ctl SLOT_OK 504 ret SLOT_A_REFUSE 505 end 506 if ne slot_manifest_check man SLOT_OK 507 ret SLOT_A_REFUSE 508 end 509 if not slot_brec_matches_man brec man 510 ret SLOT_A_REFUSE 511 end 512 slot_copy_bytes ctl_dst ctl SLOT_UNIT 513 bslot = get brec SLOT_B_OFF_SLOT 514 reason = uint get brec SLOT_B_OFF_REASON 515 cand = get ctl SLOT_C_OFF_CAND 516 act = get ctl SLOT_C_OFF_ACTIVE 517 if eq reason SLOT_R_CANDIDATE_TRY 518 if ne cand bslot 519 ret SLOT_A_NONE 520 end 521 if not healthy 522 ret SLOT_A_WAIT 523 end 524 if ne slot_promote ctl_dst bslot boot_rd64_le man SLOT_M_OFF_GEN boot_rd64_le man SLOT_M_OFF_PGEN SLOT_OK 525 ret SLOT_A_REFUSE 526 end 527 ret SLOT_A_PROMOTE 528 end 529 if or eq reason SLOT_R_CANDIDATE_EXHAUSTED eq reason SLOT_R_CANDIDATE_INVALID 530 if and ne cand SLOT_NONE eq act bslot 531 if ne slot_abandon_candidate ctl_dst SLOT_OK 532 ret SLOT_A_REFUSE 533 end 534 ret SLOT_A_ABANDON 535 end 536 end 537 ret SLOT_A_NONE 538 end 539 540 fn slot_verify_image_sha512 man view uchar 4096 img view uchar * img_cap ulong ev edit SlotEval to bool 541 put ev valid 0 542 put ev generation 0 543 put ev policy_gen 0 544 rc = slot_manifest_check man 545 if ne rc SLOT_OK 546 put ev err rc 547 ret false 548 end 549 ilen = boot_rd64_le man SLOT_M_OFF_IMGLEN 550 if or gt ilen img_cap gt ilen len img 551 put ev err SLOT_ERR_IMGLEN 552 ret false 553 end 554 dig = new uchar 64 0 555 dlen = sha2_digest 512 part img 0 ilen dig 556 if ne dlen 64 557 put ev err SLOT_ERR_DIGEST 558 ret false 559 end 560 if not boot_eq_bytes dig part man SLOT_M_OFF_IMGSHA 64 561 put ev err SLOT_ERR_SHA_MISMATCH 562 ret false 563 end 564 put ev valid 1 565 put ev generation boot_rd64_le man SLOT_M_OFF_GEN 566 put ev policy_gen boot_rd64_le man SLOT_M_OFF_PGEN 567 put ev err SLOT_OK 568 ret true 569 end