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 / crates / aienos-boot / boot_crate.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/crates/aienos-boot/boot_crate.om: Pure-Omega port of the aienos-boot crate. |
| 3 | note Converted from aienux crates/aienos-boot/src/{lib,main,handoff,nvme_read,rollback_mock,store_qual,usb_keyboard,wcslen}.rs. |
| 4 | note Library unit: no om2 header, no fn main, no globals. |
| 5 | note Compose with: si/directives/om2.om om/tls/sha2.om aienux/crates/aienos-boot/boot_crate.om ... |
| 6 | |
| 7 | note === Constants: lib.rs & main.rs === |
| 8 | const BC_CREATES_AGENT_IDENTITY ulong 0 |
| 9 | |
| 10 | text bc_firmware_banner "AIENOS firmware entry: aarch64" |
| 11 | text bc_firmware_services_line "firmware services: active" |
| 12 | text bc_kernel_handoff_line "kernel handoff: not implemented" |
| 13 | |
| 14 | note === Constants: rollback_mock.rs === |
| 15 | const BC_ROLE_STAGER ulong 0 |
| 16 | const BC_ROLE_CANDIDATE ulong 1 |
| 17 | const BC_ROLE_DEFAULT_OS ulong 2 |
| 18 | |
| 19 | const BC_MODE_NORMAL ulong 0 |
| 20 | const BC_MODE_FAULT ulong 1 |
| 21 | const BC_MODE_TIMEOUT ulong 2 |
| 22 | const BC_MODE_ABSENT ulong 3 |
| 23 | const BC_MODE_MALFORMED ulong 4 |
| 24 | const BC_MODE_REAL_AIENOS ulong 5 |
| 25 | |
| 26 | const BC_DP_TYPE_MEDIA uchar 0x04 |
| 27 | const BC_DP_SUBTYPE_FILE_PATH uchar 0x04 |
| 28 | const BC_DP_TYPE_END uchar 0x7f |
| 29 | const BC_DP_SUBTYPE_END_ENTIRE uchar 0xff |
| 30 | const BC_LOAD_OPTION_ACTIVE uint 1 |
| 31 | |
| 32 | const BC_BOOT_CANDIDATE ulong 0x0000 |
| 33 | const BC_BOOT_DEFAULT ulong 0x0001 |
| 34 | const BC_BOOT_INVALID ulong 0xffff |
| 35 | |
| 36 | const BC_RB_BOOT_ORDER_0 ulong 0 |
| 37 | const BC_RB_BOOT_ORDER_VALID ulong 1 |
| 38 | const BC_RB_BOOT_NEXT ulong 2 |
| 39 | const BC_RB_BOOT_NEXT_PRESENT ulong 3 |
| 40 | const BC_RB_BOOT_CURRENT ulong 4 |
| 41 | const BC_RB_CYCLE ulong 5 |
| 42 | const BC_RB_BOOT0000_SET ulong 6 |
| 43 | const BC_RB_BOOT0001_SET ulong 7 |
| 44 | const BC_RB_INVARIANT_MASK ulong 8 |
| 45 | |
| 46 | const BC_RB_ACTION_COLD_RESET_OK ulong 1 |
| 47 | const BC_RB_ACTION_COLD_RESET_ABORT ulong 2 |
| 48 | const BC_RB_ACTION_HANG ulong 3 |
| 49 | const BC_RB_ACTION_COLD_RESET_REPEAT ulong 4 |
| 50 | const BC_RB_ACTION_SHUTDOWN_PASS ulong 5 |
| 51 | const BC_RB_ACTION_UNSUPPORTED ulong 6 |
| 52 | const BC_RB_ACTION_FAIL ulong 7 |
| 53 | |
| 54 | text bc_rb_str_default "default" |
| 55 | text bc_rb_str_candidate "candidate" |
| 56 | text bc_rb_str_fault "fault" |
| 57 | text bc_rb_str_timeout "timeout" |
| 58 | text bc_rb_str_absent "absent" |
| 59 | text bc_rb_str_malformed "malformed" |
| 60 | text bc_rb_str_real_aienos "real_aienos" |
| 61 | text bc_rb_path_default "\\EFI\\DEFAULT\\default.efi" |
| 62 | text bc_rb_path_candidate "\\EFI\\AIENOS\\candidate.efi" |
| 63 | text bc_rb_path_absent "\\EFI\\AIENOS\\missing_candidate.efi" |
| 64 | text bc_rb_path_real_aienos "\\EFI\\AIENOS\\aienos-handoff.efi" |
| 65 | text bc_rb_desc_default "Default Linux OS" |
| 66 | text bc_rb_desc_candidate "AIENOS Candidate" |
| 67 | |
| 68 | note === Constants: store_qual.rs === |
| 69 | const BC_STORE_BASE_LBA ulong 64 |
| 70 | const BC_STORE_REGION_BLOCKS ulong 256 |
| 71 | const BC_CONFIG_LBA ulong 32 |
| 72 | const BC_MODE_VERIFY uchar 2 |
| 73 | |
| 74 | const BC_MODE_RUN_512B uchar 3 |
| 75 | const BC_MODE_VERIFY_512B uchar 4 |
| 76 | const BC_STORE_BASE_LBA_512 ulong 512 |
| 77 | const BC_STORE_REGION_BLOCKS_512 ulong 2048 |
| 78 | const BC_CONFIG_LBA_512 ulong 256 |
| 79 | |
| 80 | const BC_MODE_CONT_PROVISION uchar 5 |
| 81 | const BC_MODE_CONT_RESUME uchar 6 |
| 82 | const BC_MODE_CONT_REMEMBER uchar 7 |
| 83 | const BC_MODE_CONT_CRASH uchar 8 |
| 84 | |
| 85 | const BC_MODE_RECOVERY_INSPECT uchar 9 |
| 86 | const BC_MODE_RECOVERY_REPAIR uchar 10 |
| 87 | const BC_MODE_RECOVERY_PROVISION uchar 11 |
| 88 | |
| 89 | const BC_STORE_UNIT_BYTES ulong 4096 |
| 90 | const BC_TEST_ONLY_OPERATOR_KEY_BYTE uchar 0x0f |
| 91 | const BC_QUAL_STORE_UUID_BYTE uchar 0x5a |
| 92 | |
| 93 | const BC_BLK_OK int 0 |
| 94 | const BC_BLK_ERR_INVALID_INPUT int -1 |
| 95 | const BC_BLK_ERR_OUT_OF_RANGE int -2 |
| 96 | |
| 97 | const BC_QUAL_OK int 0 |
| 98 | const BC_QUAL_ERR_BLOCK_SIZE int -1 |
| 99 | const BC_QUAL_ERR_512_NEEDS_512 int -2 |
| 100 | const BC_QUAL_ERR_4096_REQUIRED int -3 |
| 101 | const BC_QUAL_ERR_NOT_ATOMIC int -4 |
| 102 | const BC_QUAL_ERR_REGION_OOB int -5 |
| 103 | |
| 104 | note === Constants: nvme_read.rs === |
| 105 | const BC_NVME_SENTINEL_LBA ulong 2048 |
| 106 | const BC_NVME_RW_LBA ulong 4096 |
| 107 | const BC_NVME_ARENA_BYTES ulong 524288 |
| 108 | const BC_NVME_CLASS_CODE uint 0x0108 |
| 109 | |
| 110 | const BC_PCI_BME_BIT ushort 0x0004 |
| 111 | const BC_DMA_GRANT_CONFINED int 1 |
| 112 | const BC_DMA_GRANT_UNSAFE_BYPASS int 2 |
| 113 | const BC_DMA_DENIED_SMMU_NOT_READY int -1 |
| 114 | const BC_DMA_DENIED_NO_SMMU int -2 |
| 115 | |
| 116 | text bc_nvme_sentinel_magic "AIENOS-NVME-SENT" |
| 117 | text bc_nvme_rw_magic "AIENOS-NVME-RW01" |
| 118 | |
| 119 | note === Constants: usb_keyboard.rs === |
| 120 | const BC_XHCI_CLASS_PROGIF uint 0x0c0330 |
| 121 | const BC_KEYBOARD_LISTEN_SECS ulong 30 |
| 122 | const BC_KB_LINE_CAP ulong 128 |
| 123 | |
| 124 | const BC_KEY_NONE uchar 0 |
| 125 | const BC_KEY_CHAR uchar 1 |
| 126 | const BC_KEY_ENTER uchar 2 |
| 127 | const BC_KEY_BACKSPACE uchar 3 |
| 128 | |
| 129 | const BC_KB_DONE_EXIT ulong 1 |
| 130 | const BC_KB_DONE_HALTED ulong 2 |
| 131 | const BC_KB_DONE_TIMEOUT ulong 3 |
| 132 | |
| 133 | text bc_kb_cmd_exit "exit" |
| 134 | |
| 135 | note === Constants: handoff.rs === |
| 136 | const BC_PT_POOL_PAGES ulong 256 |
| 137 | const BC_POST_EXIT_HEAP_PAGES ulong 1024 |
| 138 | const BC_SMMU_STREAM_ENTRIES uint 4096 |
| 139 | const BC_SMMU_QUEUE_ENTRIES uint 16 |
| 140 | const BC_SMMU_TABLES_ALIGN ulong 262144 |
| 141 | const BC_SMMU_OFF_STREAM ulong 0 |
| 142 | const BC_SMMU_OFF_EVENT ulong 262144 |
| 143 | const BC_SMMU_OFF_CMD ulong 262656 |
| 144 | const BC_SMMU_OFF_CTX ulong 262912 |
| 145 | const BC_SMMU_TOTAL_BYTES ulong 262976 |
| 146 | |
| 147 | const BC_TTBR_BADDR_MASK ulong 0x0000fffffffffffe |
| 148 | const BC_GICD_PIDR2 ulong 0xffe8 |
| 149 | const BC_REPORT_VERSION uint 1 |
| 150 | const BC_MAX_VAR_WRITES ulong 3 |
| 151 | const BC_MAX_VAR_BYTES ulong 3072 |
| 152 | const BC_MAX_ROOT_BRIDGES ulong 4 |
| 153 | const BC_MAX_ARTIFACT_CANDIDATES ulong 48 |
| 154 | const BC_MAX_ARTIFACT_FILE_BYTES ulong 655360 |
| 155 | const BC_ARTIFACT_NAME_BYTES ulong 32 |
| 156 | const BC_ARTIFACT_STRIDE ulong 48 |
| 157 | const BC_GB10_VENDOR_DEVICE uint 0x2e1210de |
| 158 | |
| 159 | const BC_STAGE_FIRMWARE_ENTRY ulong 0 |
| 160 | const BC_STAGE_GB10_DISCOVERY ulong 1 |
| 161 | const BC_STAGE_ACPI_TOPOLOGY ulong 2 |
| 162 | const BC_STAGE_FRAMEBUFFER_DISCOVERY ulong 3 |
| 163 | const BC_STAGE_PRE_EXIT_SAVED ulong 4 |
| 164 | const BC_STAGE_EXIT_BOOT_SERVICES ulong 5 |
| 165 | const BC_STAGE_VECTORS_INSTALLED ulong 6 |
| 166 | const BC_STAGE_KERNEL_ENTERED ulong 7 |
| 167 | const BC_STAGE_SCREEN_DRAWN ulong 8 |
| 168 | const BC_STAGE_FINAL_SAVED ulong 9 |
| 169 | const BC_STAGE_UART_SENT ulong 10 |
| 170 | const BC_STAGE_COUNTDOWN ulong 11 |
| 171 | const BC_STAGE_COUNT ulong 12 |
| 172 | |
| 173 | const BC_VAR_ERR_TOO_LARGE int -1 |
| 174 | const BC_VAR_ERR_BUDGET_SPENT int -2 |
| 175 | |
| 176 | text bc_rsdp_sig "RSD PTR " |
| 177 | text bc_artifact_suffix ".aien" |
| 178 | |
| 179 | note ============================================================================ |
| 180 | note Section 1: Feature policy (lib.rs) & Little-endian / Byte helpers |
| 181 | note ============================================================================ |
| 182 | |
| 183 | fn bc_feature_config_valid hw_staging bool unsafe_dma bool unsafe_nvme_dma bool to bool |
| 184 | if hw_staging |
| 185 | if unsafe_dma |
| 186 | ret false |
| 187 | end |
| 188 | if unsafe_nvme_dma |
| 189 | ret false |
| 190 | end |
| 191 | end |
| 192 | ret true |
| 193 | end |
| 194 | |
| 195 | fn bc_get_u16_le buf view uchar * off ulong to ushort |
| 196 | b0 = ushort get buf off |
| 197 | b1 = ushort get buf add off 1 |
| 198 | ret or b0 shl b1 8 |
| 199 | end |
| 200 | |
| 201 | fn bc_put_u16_le buf edit uchar * off ulong val ushort to void |
| 202 | put buf off uchar and val 255 |
| 203 | put buf add off 1 uchar and shr val 8 255 |
| 204 | end |
| 205 | |
| 206 | fn bc_get_u32_le buf view uchar * off ulong to uint |
| 207 | b0 = uint get buf off |
| 208 | b1 = uint get buf add off 1 |
| 209 | b2 = uint get buf add off 2 |
| 210 | b3 = uint get buf add off 3 |
| 211 | t0 = or b0 shl b1 8 |
| 212 | t1 = or shl b2 16 shl b3 24 |
| 213 | ret or t0 t1 |
| 214 | end |
| 215 | |
| 216 | fn bc_put_u32_le buf edit uchar * off ulong val uint to void |
| 217 | put buf off uchar and val 255 |
| 218 | put buf add off 1 uchar and shr val 8 255 |
| 219 | put buf add off 2 uchar and shr val 16 255 |
| 220 | put buf add off 3 uchar and shr val 24 255 |
| 221 | end |
| 222 | |
| 223 | fn bc_get_u64_le buf view uchar * off ulong to ulong |
| 224 | lo = ulong bc_get_u32_le buf off |
| 225 | hi = ulong bc_get_u32_le buf add off 4 |
| 226 | ret or lo shl hi 32 |
| 227 | end |
| 228 | |
| 229 | fn bc_put_u64_le buf edit uchar * off ulong val ulong to void |
| 230 | bc_put_u32_le buf off uint and val 0xffffffff |
| 231 | bc_put_u32_le buf add off 4 uint and shr val 32 0xffffffff |
| 232 | end |
| 233 | |
| 234 | fn bc_ascii_lower ch uchar to uchar |
| 235 | if ge ch 65 |
| 236 | if le ch 90 |
| 237 | ret add ch 32 |
| 238 | end |
| 239 | end |
| 240 | ret ch |
| 241 | end |
| 242 | |
| 243 | fn bc_bytes_eq a view uchar * b view uchar * to bool |
| 244 | na = len a |
| 245 | if ne na len b |
| 246 | ret false |
| 247 | end |
| 248 | for i ulong na bound 65536 |
| 249 | if ne get a i get b i |
| 250 | ret false |
| 251 | end |
| 252 | end |
| 253 | ret true |
| 254 | end |
| 255 | |
| 256 | fn bc_bytes_contains hay view uchar * needle view uchar * to bool |
| 257 | hn = len hay |
| 258 | nn = len needle |
| 259 | if eq nn 0 |
| 260 | ret true |
| 261 | end |
| 262 | if gt nn hn |
| 263 | ret false |
| 264 | end |
| 265 | limit = add sub hn nn 1 |
| 266 | for i ulong limit bound 65536 |
| 267 | matched = true |
| 268 | for j ulong nn bound 4096 |
| 269 | if ne get hay add i j get needle j |
| 270 | matched = false |
| 271 | break |
| 272 | end |
| 273 | end |
| 274 | if matched |
| 275 | ret true |
| 276 | end |
| 277 | end |
| 278 | ret false |
| 279 | end |
| 280 | |
| 281 | fn bc_hex_nibble n uchar to uchar |
| 282 | v = and n 15 |
| 283 | if lt v 10 |
| 284 | ret add v 48 |
| 285 | end |
| 286 | ret add v 87 |
| 287 | end |
| 288 | |
| 289 | fn bc_bytes_to_hex src view uchar * dst edit uchar * to ulong |
| 290 | n = len src |
| 291 | need = mul n 2 |
| 292 | if lt len dst need |
| 293 | ret 0 |
| 294 | end |
| 295 | for i ulong n bound 4096 |
| 296 | b = get src i |
| 297 | put dst mul i 2 bc_hex_nibble shr b 4 |
| 298 | put dst add mul i 2 1 bc_hex_nibble and b 15 |
| 299 | end |
| 300 | ret need |
| 301 | end |
| 302 | |
| 303 | note ============================================================================ |
| 304 | note Section 2: Bounded wcslen (wcslen.rs) |
| 305 | note ============================================================================ |
| 306 | |
| 307 | fn bc_wide_len s view ushort * to ulong |
| 308 | n = len s |
| 309 | for i ulong n bound 65536 |
| 310 | if eq get s i 0 |
| 311 | ret i |
| 312 | end |
| 313 | end |
| 314 | ret n |
| 315 | end |
| 316 | |
| 317 | note ============================================================================ |
| 318 | note Section 3: Rollback Mock (rollback_mock.rs) |
| 319 | note ============================================================================ |
| 320 | |
| 321 | fn bc_dp_node_match_role dp view uchar * off ulong node_len ulong to ulong |
| 322 | note Extracts low ASCII bytes of UTF-16LE file path node, lowercases, checks default/candidate |
| 323 | if le node_len 4 |
| 324 | ret BC_ROLE_STAGER |
| 325 | end |
| 326 | data_bytes = sub node_len 4 |
| 327 | chars = div data_bytes 2 |
| 328 | if gt chars 128 |
| 329 | chars = ulong 128 |
| 330 | end |
| 331 | tmp = new uchar 128 0 |
| 332 | for i ulong chars bound 128 |
| 333 | pos = add add off 4 mul i 2 |
| 334 | ch = get dp pos |
| 335 | put tmp i bc_ascii_lower ch |
| 336 | end |
| 337 | if bc_bytes_contains part tmp 0 chars bc_rb_str_default |
| 338 | ret BC_ROLE_DEFAULT_OS |
| 339 | end |
| 340 | if bc_bytes_contains part tmp 0 chars bc_rb_str_candidate |
| 341 | ret BC_ROLE_CANDIDATE |
| 342 | end |
| 343 | ret BC_ROLE_STAGER |
| 344 | end |
| 345 | |
| 346 | fn bc_dp_detect_role dp view uchar * to ulong |
| 347 | total = len dp |
| 348 | off = ulong 0 |
| 349 | while lt off total bound 1024 |
| 350 | if lt sub total off 4 |
| 351 | ret BC_ROLE_STAGER |
| 352 | end |
| 353 | dtype = get dp off |
| 354 | stype = get dp add off 1 |
| 355 | nlen = ulong bc_get_u16_le dp add off 2 |
| 356 | if lt nlen 4 |
| 357 | ret BC_ROLE_STAGER |
| 358 | end |
| 359 | if gt nlen sub total off |
| 360 | ret BC_ROLE_STAGER |
| 361 | end |
| 362 | if eq dtype BC_DP_TYPE_END |
| 363 | if eq stype BC_DP_SUBTYPE_END_ENTIRE |
| 364 | ret BC_ROLE_STAGER |
| 365 | end |
| 366 | end |
| 367 | if eq dtype BC_DP_TYPE_MEDIA |
| 368 | if eq stype BC_DP_SUBTYPE_FILE_PATH |
| 369 | r = bc_dp_node_match_role dp off nlen |
| 370 | if ne r BC_ROLE_STAGER |
| 371 | ret r |
| 372 | end |
| 373 | end |
| 374 | end |
| 375 | off = add off nlen |
| 376 | end |
| 377 | ret BC_ROLE_STAGER |
| 378 | end |
| 379 | |
| 380 | fn bc_dp_prefix_len dp view uchar * to ulong |
| 381 | total = len dp |
| 382 | off = ulong 0 |
| 383 | while lt off total bound 1024 |
| 384 | if lt sub total off 4 |
| 385 | ret 0 |
| 386 | end |
| 387 | dtype = get dp off |
| 388 | stype = get dp add off 1 |
| 389 | nlen = ulong bc_get_u16_le dp add off 2 |
| 390 | if lt nlen 4 |
| 391 | ret 0 |
| 392 | end |
| 393 | if gt nlen sub total off |
| 394 | ret 0 |
| 395 | end |
| 396 | if eq dtype BC_DP_TYPE_MEDIA |
| 397 | if eq stype BC_DP_SUBTYPE_FILE_PATH |
| 398 | ret off |
| 399 | end |
| 400 | end |
| 401 | if eq dtype BC_DP_TYPE_END |
| 402 | if eq stype BC_DP_SUBTYPE_END_ENTIRE |
| 403 | ret off |
| 404 | end |
| 405 | end |
| 406 | off = add off nlen |
| 407 | end |
| 408 | ret off |
| 409 | end |
| 410 | |
| 411 | fn bc_rollback_parse_mode buf view uchar * to ulong |
| 412 | if bc_bytes_contains buf bc_rb_str_fault |
| 413 | ret BC_MODE_FAULT |
| 414 | end |
| 415 | if bc_bytes_contains buf bc_rb_str_timeout |
| 416 | ret BC_MODE_TIMEOUT |
| 417 | end |
| 418 | if bc_bytes_contains buf bc_rb_str_absent |
| 419 | ret BC_MODE_ABSENT |
| 420 | end |
| 421 | if bc_bytes_contains buf bc_rb_str_malformed |
| 422 | ret BC_MODE_MALFORMED |
| 423 | end |
| 424 | if bc_bytes_contains buf bc_rb_str_real_aienos |
| 425 | ret BC_MODE_REAL_AIENOS |
| 426 | end |
| 427 | ret BC_MODE_NORMAL |
| 428 | end |
| 429 | |
| 430 | fn bc_build_file_dp prefix view uchar * path view uchar * dst edit uchar * to ulong |
| 431 | plen = len prefix |
| 432 | path_len = len path |
| 433 | node_len = add 4 mul 2 add path_len 1 |
| 434 | total = add add plen node_len 4 |
| 435 | if gt total len dst |
| 436 | ret 0 |
| 437 | end |
| 438 | for i ulong plen bound 4096 |
| 439 | put dst i get prefix i |
| 440 | end |
| 441 | pos = plen |
| 442 | put dst pos BC_DP_TYPE_MEDIA |
| 443 | put dst add pos 1 BC_DP_SUBTYPE_FILE_PATH |
| 444 | bc_put_u16_le dst add pos 2 ushort and node_len 65535 |
| 445 | pos = add pos 4 |
| 446 | for i ulong path_len bound 4096 |
| 447 | put dst pos get path i |
| 448 | put dst add pos 1 0 |
| 449 | pos = add pos 2 |
| 450 | end |
| 451 | put dst pos 0 |
| 452 | put dst add pos 1 0 |
| 453 | pos = add pos 2 |
| 454 | put dst pos BC_DP_TYPE_END |
| 455 | put dst add pos 1 BC_DP_SUBTYPE_END_ENTIRE |
| 456 | bc_put_u16_le dst add pos 2 4 |
| 457 | ret add pos 4 |
| 458 | end |
| 459 | |
| 460 | fn bc_build_load_option desc view uchar * full_dp view uchar * dst edit uchar * to ulong |
| 461 | dlen = len desc |
| 462 | fplen = len full_dp |
| 463 | desc_bytes = mul 2 add dlen 1 |
| 464 | total = add add 6 desc_bytes fplen |
| 465 | if gt total len dst |
| 466 | ret 0 |
| 467 | end |
| 468 | bc_put_u32_le dst 0 BC_LOAD_OPTION_ACTIVE |
| 469 | bc_put_u16_le dst 4 ushort and fplen 65535 |
| 470 | pos = ulong 6 |
| 471 | for i ulong dlen bound 4096 |
| 472 | put dst pos get desc i |
| 473 | put dst add pos 1 0 |
| 474 | pos = add pos 2 |
| 475 | end |
| 476 | put dst pos 0 |
| 477 | put dst add pos 1 0 |
| 478 | pos = add pos 2 |
| 479 | for i ulong fplen bound 4096 |
| 480 | put dst pos get full_dp i |
| 481 | pos = add pos 1 |
| 482 | end |
| 483 | ret pos |
| 484 | end |
| 485 | |
| 486 | fn bc_rollback_init st edit ulong 9 to void |
| 487 | for i ulong 9 |
| 488 | put st i 0 |
| 489 | end |
| 490 | put st BC_RB_BOOT_CURRENT BC_BOOT_INVALID |
| 491 | end |
| 492 | |
| 493 | fn bc_rollback_stager st edit ulong 9 mode ulong prefix_len ulong to ulong |
| 494 | note mode is recorded by caller when selecting candidate path; stager stages Boot0001/BootOrder/Boot0000/BootNext |
| 495 | if eq prefix_len 0 |
| 496 | ret BC_RB_ACTION_UNSUPPORTED |
| 497 | end |
| 498 | if gt mode BC_MODE_REAL_AIENOS |
| 499 | ret BC_RB_ACTION_UNSUPPORTED |
| 500 | end |
| 501 | put st BC_RB_BOOT0001_SET 1 |
| 502 | put st BC_RB_BOOT_ORDER_0 BC_BOOT_DEFAULT |
| 503 | put st BC_RB_BOOT_ORDER_VALID 1 |
| 504 | put st BC_RB_BOOT0000_SET 1 |
| 505 | put st BC_RB_BOOT_NEXT BC_BOOT_CANDIDATE |
| 506 | put st BC_RB_BOOT_NEXT_PRESENT 1 |
| 507 | put st BC_RB_CYCLE 0 |
| 508 | ret BC_RB_ACTION_COLD_RESET_OK |
| 509 | end |
| 510 | |
| 511 | fn bc_rollback_firmware_boot st edit ulong 9 to ulong |
| 512 | note Simulates UEFI BDS: BootNext is consumed once, otherwise BootOrder[0] boots |
| 513 | if ne get st BC_RB_BOOT_NEXT_PRESENT 0 |
| 514 | target = get st BC_RB_BOOT_NEXT |
| 515 | put st BC_RB_BOOT_NEXT_PRESENT 0 |
| 516 | put st BC_RB_BOOT_CURRENT target |
| 517 | ret target |
| 518 | end |
| 519 | if ne get st BC_RB_BOOT_ORDER_VALID 0 |
| 520 | target = get st BC_RB_BOOT_ORDER_0 |
| 521 | put st BC_RB_BOOT_CURRENT target |
| 522 | ret target |
| 523 | end |
| 524 | put st BC_RB_BOOT_CURRENT BC_BOOT_INVALID |
| 525 | ret BC_BOOT_INVALID |
| 526 | end |
| 527 | |
| 528 | fn bc_rollback_candidate st edit ulong 9 mode ulong to ulong |
| 529 | note Candidate checks mode and returns reset/hang action |
| 530 | if eq mode BC_MODE_FAULT |
| 531 | ret BC_RB_ACTION_COLD_RESET_ABORT |
| 532 | end |
| 533 | if eq mode BC_MODE_TIMEOUT |
| 534 | ret BC_RB_ACTION_HANG |
| 535 | end |
| 536 | ret BC_RB_ACTION_COLD_RESET_OK |
| 537 | end |
| 538 | |
| 539 | fn bc_rollback_default st edit ulong 9 to ulong |
| 540 | next_consumed = eq get st BC_RB_BOOT_NEXT_PRESENT 0 |
| 541 | order_ok = false |
| 542 | if ne get st BC_RB_BOOT_ORDER_VALID 0 |
| 543 | if eq get st BC_RB_BOOT_ORDER_0 BC_BOOT_DEFAULT |
| 544 | order_ok = true |
| 545 | end |
| 546 | end |
| 547 | mask = ulong 0 |
| 548 | if next_consumed |
| 549 | mask = or mask 1 |
| 550 | end |
| 551 | if order_ok |
| 552 | mask = or mask 2 |
| 553 | end |
| 554 | put st BC_RB_INVARIANT_MASK mask |
| 555 | cycle = get st BC_RB_CYCLE |
| 556 | if eq cycle 0 |
| 557 | put st BC_RB_CYCLE 1 |
| 558 | ret BC_RB_ACTION_COLD_RESET_REPEAT |
| 559 | end |
| 560 | if eq mask 3 |
| 561 | ret BC_RB_ACTION_SHUTDOWN_PASS |
| 562 | end |
| 563 | ret BC_RB_ACTION_FAIL |
| 564 | end |
| 565 | |
| 566 | note ============================================================================ |
| 567 | note Section 4: Store Qualification & Bounded NVMe (store_qual.rs) |
| 568 | note ============================================================================ |
| 569 | |
| 570 | fn bc_is_recovery_mode mode uchar to bool |
| 571 | if eq mode BC_MODE_RECOVERY_INSPECT |
| 572 | ret true |
| 573 | end |
| 574 | if eq mode BC_MODE_RECOVERY_REPAIR |
| 575 | ret true |
| 576 | end |
| 577 | if eq mode BC_MODE_RECOVERY_PROVISION |
| 578 | ret true |
| 579 | end |
| 580 | ret false |
| 581 | end |
| 582 | |
| 583 | fn bc_is_continuity_mode mode uchar to bool |
| 584 | if eq mode BC_MODE_CONT_PROVISION |
| 585 | ret true |
| 586 | end |
| 587 | if eq mode BC_MODE_CONT_RESUME |
| 588 | ret true |
| 589 | end |
| 590 | if eq mode BC_MODE_CONT_REMEMBER |
| 591 | ret true |
| 592 | end |
| 593 | if eq mode BC_MODE_CONT_CRASH |
| 594 | ret true |
| 595 | end |
| 596 | ret false |
| 597 | end |
| 598 | |
| 599 | fn bc_is_512_qual_mode mode uchar to bool |
| 600 | if eq mode BC_MODE_RUN_512B |
| 601 | ret true |
| 602 | end |
| 603 | if eq mode BC_MODE_VERIFY_512B |
| 604 | ret true |
| 605 | end |
| 606 | ret false |
| 607 | end |
| 608 | |
| 609 | fn bc_store_config_lba block_size uint to ulong |
| 610 | if eq block_size 512 |
| 611 | ret BC_CONFIG_LBA_512 |
| 612 | end |
| 613 | if eq block_size 4096 |
| 614 | ret BC_CONFIG_LBA |
| 615 | end |
| 616 | ret 0 |
| 617 | end |
| 618 | |
| 619 | fn bc_store_region_base is_512 bool to ulong |
| 620 | if is_512 |
| 621 | ret BC_STORE_BASE_LBA_512 |
| 622 | end |
| 623 | ret BC_STORE_BASE_LBA |
| 624 | end |
| 625 | |
| 626 | fn bc_store_region_blocks is_512 bool to ulong |
| 627 | if is_512 |
| 628 | ret BC_STORE_REGION_BLOCKS_512 |
| 629 | end |
| 630 | ret BC_STORE_REGION_BLOCKS |
| 631 | end |
| 632 | |
| 633 | fn bc_bounded_nvme_init dev edit ulong 3 block_size uint base_lba ulong blocks ulong to void |
| 634 | put dev 0 ulong block_size |
| 635 | put dev 1 base_lba |
| 636 | put dev 2 blocks |
| 637 | end |
| 638 | |
| 639 | fn bc_bounded_nvme_translate dev view ulong 3 lba ulong buf_len ulong dst_lba edit ulong 1 to int |
| 640 | bs = get dev 0 |
| 641 | base_lba = get dev 1 |
| 642 | blocks = get dev 2 |
| 643 | if eq bs 0 |
| 644 | ret BC_BLK_ERR_INVALID_INPUT |
| 645 | end |
| 646 | if ne rem buf_len bs 0 |
| 647 | ret BC_BLK_ERR_INVALID_INPUT |
| 648 | end |
| 649 | cnt = div buf_len bs |
| 650 | if gt lba blocks |
| 651 | ret BC_BLK_ERR_OUT_OF_RANGE |
| 652 | end |
| 653 | if gt cnt sub blocks lba |
| 654 | ret BC_BLK_ERR_OUT_OF_RANGE |
| 655 | end |
| 656 | put dst_lba 0 add base_lba lba |
| 657 | ret BC_BLK_OK |
| 658 | end |
| 659 | |
| 660 | fn bc_store_qual_check_geometry block_size uint mode uchar block_count ulong atomic_ok bool to int |
| 661 | if ne block_size 512 |
| 662 | if ne block_size 4096 |
| 663 | ret BC_QUAL_ERR_BLOCK_SIZE |
| 664 | end |
| 665 | end |
| 666 | is_512 = bc_is_512_qual_mode mode |
| 667 | if is_512 |
| 668 | if ne block_size 512 |
| 669 | ret BC_QUAL_ERR_512_NEEDS_512 |
| 670 | end |
| 671 | else |
| 672 | if ne block_size 4096 |
| 673 | ret BC_QUAL_ERR_4096_REQUIRED |
| 674 | end |
| 675 | if not atomic_ok |
| 676 | ret BC_QUAL_ERR_NOT_ATOMIC |
| 677 | end |
| 678 | end |
| 679 | base_lba = bc_store_region_base is_512 |
| 680 | region_blocks = bc_store_region_blocks is_512 |
| 681 | if gt base_lba block_count |
| 682 | ret BC_QUAL_ERR_REGION_OOB |
| 683 | end |
| 684 | if gt region_blocks sub block_count base_lba |
| 685 | ret BC_QUAL_ERR_REGION_OOB |
| 686 | end |
| 687 | ret BC_QUAL_OK |
| 688 | end |
| 689 | |
| 690 | fn bc_store_reopen_gen_valid generation ulong settle uchar to bool |
| 691 | upper = add ulong settle 2 |
| 692 | if lt generation 1 |
| 693 | ret false |
| 694 | end |
| 695 | if gt generation upper |
| 696 | ret false |
| 697 | end |
| 698 | ret true |
| 699 | end |
| 700 | |
| 701 | fn bc_store_slot_reuse_proven generation ulong settle uchar to bool |
| 702 | if ge settle 3 |
| 703 | if ge generation 4 |
| 704 | ret true |
| 705 | end |
| 706 | end |
| 707 | ret false |
| 708 | end |
| 709 | |
| 710 | note ============================================================================ |
| 711 | note Section 5: NVMe Read, DMA Gate & Arena (nvme_read.rs) |
| 712 | note ============================================================================ |
| 713 | |
| 714 | fn bc_pci_requester_id bus ulong dev ulong fun ulong to uint |
| 715 | b = shl and uint bus 255 8 |
| 716 | d = shl and uint dev 31 3 |
| 717 | f = and uint fun 7 |
| 718 | ret or or b d f |
| 719 | end |
| 720 | |
| 721 | fn bc_pci_bme_enabled cmd ushort to bool |
| 722 | ret ne and cmd BC_PCI_BME_BIT 0 |
| 723 | end |
| 724 | |
| 725 | fn bc_pci_without_bme cmd ushort to ushort |
| 726 | ret and cmd 0xfffb |
| 727 | end |
| 728 | |
| 729 | fn bc_pci_with_bme cmd ushort to ushort |
| 730 | ret or cmd BC_PCI_BME_BIT |
| 731 | end |
| 732 | |
| 733 | fn bc_dma_grant smmu_ready bool has_smmu bool unsafe_bypass bool to int |
| 734 | if smmu_ready |
| 735 | if has_smmu |
| 736 | ret BC_DMA_GRANT_CONFINED |
| 737 | end |
| 738 | end |
| 739 | if has_smmu |
| 740 | ret BC_DMA_DENIED_SMMU_NOT_READY |
| 741 | end |
| 742 | if unsafe_bypass |
| 743 | ret BC_DMA_GRANT_UNSAFE_BYPASS |
| 744 | end |
| 745 | ret BC_DMA_DENIED_NO_SMMU |
| 746 | end |
| 747 | |
| 748 | fn bc_nvme_decode_bar class_reg uint bar_low uint bar_high uint to ulong |
| 749 | cc = shr class_reg 16 |
| 750 | if ne cc BC_NVME_CLASS_CODE |
| 751 | ret 0 |
| 752 | end |
| 753 | if ne and bar_low 1 0 |
| 754 | ret 0 |
| 755 | end |
| 756 | lo = ulong and bar_low 0xfffffff0 |
| 757 | if eq and bar_low 7 4 |
| 758 | ret or shl ulong bar_high 32 lo |
| 759 | end |
| 760 | ret lo |
| 761 | end |
| 762 | |
| 763 | fn bc_dma_arena_init arena edit ulong 3 base ulong size ulong to void |
| 764 | put arena 0 base |
| 765 | put arena 1 base |
| 766 | put arena 2 add base size |
| 767 | end |
| 768 | |
| 769 | fn bc_dma_arena_alloc arena edit ulong 3 size ulong align ulong dst_phys edit ulong 1 to int |
| 770 | if eq align 0 |
| 771 | ret -1 |
| 772 | end |
| 773 | mask = sub align 1 |
| 774 | if ne and align mask 0 |
| 775 | ret -1 |
| 776 | end |
| 777 | cur = get arena 1 |
| 778 | end_addr = get arena 2 |
| 779 | if gt cur sub 0xffffffffffffffff mask |
| 780 | ret -1 |
| 781 | end |
| 782 | aligned = and add cur mask xor mask 0xffffffffffffffff |
| 783 | if gt aligned end_addr |
| 784 | ret -1 |
| 785 | end |
| 786 | if gt size sub end_addr aligned |
| 787 | ret -1 |
| 788 | end |
| 789 | put arena 1 add aligned size |
| 790 | put dst_phys 0 aligned |
| 791 | ret 0 |
| 792 | end |
| 793 | |
| 794 | fn bc_dma_arena_offset arena view ulong 3 phys ulong req_len ulong dst_off edit ulong 1 to int |
| 795 | base = get arena 0 |
| 796 | end_addr = get arena 2 |
| 797 | if lt phys base |
| 798 | ret -1 |
| 799 | end |
| 800 | if ge phys end_addr |
| 801 | ret -1 |
| 802 | end |
| 803 | off = sub phys base |
| 804 | avail = sub end_addr phys |
| 805 | if gt req_len avail |
| 806 | ret -1 |
| 807 | end |
| 808 | put dst_off 0 off |
| 809 | ret 0 |
| 810 | end |
| 811 | |
| 812 | fn bc_nvme_fill_sector_pattern buf edit uchar * magic view uchar * to bool |
| 813 | if ne len buf 512 |
| 814 | ret false |
| 815 | end |
| 816 | if ne len magic 16 |
| 817 | ret false |
| 818 | end |
| 819 | for c ulong 32 |
| 820 | base = mul c 16 |
| 821 | for i ulong 16 |
| 822 | put buf add base i get magic i |
| 823 | end |
| 824 | end |
| 825 | ret true |
| 826 | end |
| 827 | |
| 828 | fn bc_nvme_verify_sector_pattern buf view uchar * magic view uchar * to bool |
| 829 | if ne len buf 512 |
| 830 | ret false |
| 831 | end |
| 832 | if ne len magic 16 |
| 833 | ret false |
| 834 | end |
| 835 | for c ulong 32 |
| 836 | base = mul c 16 |
| 837 | for i ulong 16 |
| 838 | if ne get buf add base i get magic i |
| 839 | ret false |
| 840 | end |
| 841 | end |
| 842 | end |
| 843 | ret true |
| 844 | end |
| 845 | |
| 846 | fn bc_nvme_sector_sha256_hex buf view uchar * hex_dst edit uchar * to ulong |
| 847 | dig = new uchar 32 0 |
| 848 | dlen = sha2_digest 256 buf dig |
| 849 | if ne dlen 32 |
| 850 | ret 0 |
| 851 | end |
| 852 | ret bc_bytes_to_hex dig hex_dst |
| 853 | end |
| 854 | |
| 855 | fn bc_continuity_hash_statement ctx edit ulong * stmt view uchar * to void |
| 856 | lbuf = new uchar 4 0 |
| 857 | bc_put_u32_le lbuf 0 uint and len stmt 0xffffffff |
| 858 | sha256_update ctx lbuf |
| 859 | sha256_update ctx stmt |
| 860 | end |
| 861 | |
| 862 | note ============================================================================ |
| 863 | note Section 6: USB Keyboard & xHCI (usb_keyboard.rs) |
| 864 | note ============================================================================ |
| 865 | |
| 866 | fn bc_xhci_mmio_base class_reg uint bar_low uint bar_high uint to ulong |
| 867 | if ne shr class_reg 8 BC_XHCI_CLASS_PROGIF |
| 868 | ret 0 |
| 869 | end |
| 870 | if ne and bar_low 1 0 |
| 871 | ret 0 |
| 872 | end |
| 873 | lo = ulong and bar_low 0xfffffff0 |
| 874 | if eq and bar_low 6 4 |
| 875 | ret or shl ulong bar_high 32 lo |
| 876 | end |
| 877 | ret lo |
| 878 | end |
| 879 | |
| 880 | fn bc_hid_digit_ascii code uchar shifted bool to uchar |
| 881 | note HID usages 0x1e..0x27 -> '1'..'9','0' or '!','@','#','$','%','^','&','*','(',')' |
| 882 | if not shifted |
| 883 | if eq code 0x27 |
| 884 | ret 48 |
| 885 | end |
| 886 | ret add 49 sub code 0x1e |
| 887 | end |
| 888 | if eq code 0x1e |
| 889 | ret 33 |
| 890 | end |
| 891 | if eq code 0x1f |
| 892 | ret 64 |
| 893 | end |
| 894 | if eq code 0x20 |
| 895 | ret 35 |
| 896 | end |
| 897 | if eq code 0x21 |
| 898 | ret 36 |
| 899 | end |
| 900 | if eq code 0x22 |
| 901 | ret 37 |
| 902 | end |
| 903 | if eq code 0x23 |
| 904 | ret 94 |
| 905 | end |
| 906 | if eq code 0x24 |
| 907 | ret 38 |
| 908 | end |
| 909 | if eq code 0x25 |
| 910 | ret 42 |
| 911 | end |
| 912 | if eq code 0x26 |
| 913 | ret 40 |
| 914 | end |
| 915 | ret 41 |
| 916 | end |
| 917 | |
| 918 | fn bc_hid_punct_ascii code uchar shifted bool to uchar |
| 919 | note HID usages 0x2d..0x38 |
| 920 | if eq code 0x2d |
| 921 | if shifted |
| 922 | ret 95 |
| 923 | end |
| 924 | ret 45 |
| 925 | end |
| 926 | if eq code 0x2e |
| 927 | if shifted |
| 928 | ret 43 |
| 929 | end |
| 930 | ret 61 |
| 931 | end |
| 932 | if eq code 0x2f |
| 933 | if shifted |
| 934 | ret 123 |
| 935 | end |
| 936 | ret 91 |
| 937 | end |
| 938 | if eq code 0x30 |
| 939 | if shifted |
| 940 | ret 125 |
| 941 | end |
| 942 | ret 93 |
| 943 | end |
| 944 | if eq code 0x31 |
| 945 | if shifted |
| 946 | ret 124 |
| 947 | end |
| 948 | ret 92 |
| 949 | end |
| 950 | if eq code 0x33 |
| 951 | if shifted |
| 952 | ret 58 |
| 953 | end |
| 954 | ret 59 |
| 955 | end |
| 956 | if eq code 0x34 |
| 957 | if shifted |
| 958 | ret 34 |
| 959 | end |
| 960 | ret 39 |
| 961 | end |
| 962 | if eq code 0x35 |
| 963 | if shifted |
| 964 | ret 126 |
| 965 | end |
| 966 | ret 96 |
| 967 | end |
| 968 | if eq code 0x36 |
| 969 | if shifted |
| 970 | ret 60 |
| 971 | end |
| 972 | ret 44 |
| 973 | end |
| 974 | if eq code 0x37 |
| 975 | if shifted |
| 976 | ret 62 |
| 977 | end |
| 978 | ret 46 |
| 979 | end |
| 980 | if eq code 0x38 |
| 981 | if shifted |
| 982 | ret 63 |
| 983 | end |
| 984 | ret 47 |
| 985 | end |
| 986 | ret 0 |
| 987 | end |
| 988 | |
| 989 | fn bc_hid_keycode_to_ascii code uchar shifted bool to uchar |
| 990 | if ge code 0x04 |
| 991 | if le code 0x1d |
| 992 | if shifted |
| 993 | ret add 65 sub code 0x04 |
| 994 | end |
| 995 | ret add 97 sub code 0x04 |
| 996 | end |
| 997 | end |
| 998 | if ge code 0x1e |
| 999 | if le code 0x27 |
| 1000 | ret bc_hid_digit_ascii code shifted |
| 1001 | end |
| 1002 | end |
| 1003 | if eq code 0x2c |
| 1004 | ret 32 |
| 1005 | end |
| 1006 | if ge code 0x2d |
| 1007 | if le code 0x38 |
| 1008 | ret bc_hid_punct_ascii code shifted |
| 1009 | end |
| 1010 | end |
| 1011 | ret 0 |
| 1012 | end |
| 1013 | |
| 1014 | fn bc_hid_prev_contains prev view uchar 6 code uchar to bool |
| 1015 | for i ulong 6 |
| 1016 | if eq get prev i code |
| 1017 | ret true |
| 1018 | end |
| 1019 | end |
| 1020 | ret false |
| 1021 | end |
| 1022 | |
| 1023 | fn bc_hid_decode_report prev edit uchar 6 report view uchar 8 ev_kinds edit uchar 6 ev_chars edit uchar 6 to ulong |
| 1024 | note Ignore ErrorRollOver (0x01) reports without modifying prev |
| 1025 | for i ulong 6 |
| 1026 | if eq get report add i 2 0x01 |
| 1027 | ret 0 |
| 1028 | end |
| 1029 | end |
| 1030 | mod_byte = get report 0 |
| 1031 | shifted = ne and mod_byte 0x22 0 |
| 1032 | ev_cnt = ulong 0 |
| 1033 | for i ulong 6 |
| 1034 | code = get report add i 2 |
| 1035 | if ne code 0 |
| 1036 | if not bc_hid_prev_contains prev code |
| 1037 | if eq code 0x28 |
| 1038 | put ev_kinds ev_cnt BC_KEY_ENTER |
| 1039 | put ev_chars ev_cnt 0 |
| 1040 | ev_cnt = add ev_cnt 1 |
| 1041 | else |
| 1042 | if eq code 0x2a |
| 1043 | put ev_kinds ev_cnt BC_KEY_BACKSPACE |
| 1044 | put ev_chars ev_cnt 0 |
| 1045 | ev_cnt = add ev_cnt 1 |
| 1046 | else |
| 1047 | ch = bc_hid_keycode_to_ascii code shifted |
| 1048 | if ne ch 0 |
| 1049 | put ev_kinds ev_cnt BC_KEY_CHAR |
| 1050 | put ev_chars ev_cnt ch |
| 1051 | ev_cnt = add ev_cnt 1 |
| 1052 | end |
| 1053 | end |
| 1054 | end |
| 1055 | end |
| 1056 | end |
| 1057 | end |
| 1058 | for i ulong 6 |
| 1059 | put prev i get report add i 2 |
| 1060 | end |
| 1061 | ret ev_cnt |
| 1062 | end |
| 1063 | |
| 1064 | fn bc_kb_line_apply line edit uchar * line_len edit ulong 1 ev_kind uchar ev_char uchar to ulong |
| 1065 | cur = get line_len 0 |
| 1066 | if eq ev_kind BC_KEY_ENTER |
| 1067 | ret 1 |
| 1068 | end |
| 1069 | if eq ev_kind BC_KEY_BACKSPACE |
| 1070 | if gt cur 0 |
| 1071 | put line_len 0 sub cur 1 |
| 1072 | end |
| 1073 | ret 0 |
| 1074 | end |
| 1075 | if eq ev_kind BC_KEY_CHAR |
| 1076 | if ge ev_char 0x20 |
| 1077 | if le ev_char 0x7e |
| 1078 | if lt cur len line |
| 1079 | put line cur ev_char |
| 1080 | put line_len 0 add cur 1 |
| 1081 | end |
| 1082 | end |
| 1083 | end |
| 1084 | end |
| 1085 | ret 0 |
| 1086 | end |
| 1087 | |
| 1088 | fn bc_kb_stop_reason exited bool halted bool to ulong |
| 1089 | if exited |
| 1090 | ret BC_KB_DONE_EXIT |
| 1091 | end |
| 1092 | if halted |
| 1093 | ret BC_KB_DONE_HALTED |
| 1094 | end |
| 1095 | ret BC_KB_DONE_TIMEOUT |
| 1096 | end |
| 1097 | |
| 1098 | note ============================================================================ |
| 1099 | note Section 7: Handoff Logic (handoff.rs) |
| 1100 | note ============================================================================ |
| 1101 | |
| 1102 | fn bc_heap_init heap edit ulong 3 base ulong cap ulong to void |
| 1103 | put heap 0 base |
| 1104 | put heap 1 0 |
| 1105 | put heap 2 cap |
| 1106 | end |
| 1107 | |
| 1108 | fn bc_heap_alloc heap edit ulong 3 size ulong align ulong dst_addr edit ulong 1 to int |
| 1109 | base = get heap 0 |
| 1110 | used = get heap 1 |
| 1111 | cap = get heap 2 |
| 1112 | if eq base 0 |
| 1113 | ret -1 |
| 1114 | end |
| 1115 | if eq align 0 |
| 1116 | ret -1 |
| 1117 | end |
| 1118 | mask = sub align 1 |
| 1119 | if ne and align mask 0 |
| 1120 | ret -1 |
| 1121 | end |
| 1122 | if gt used sub 0xffffffffffffffff base |
| 1123 | ret -1 |
| 1124 | end |
| 1125 | cur = add base used |
| 1126 | if gt cur sub 0xffffffffffffffff mask |
| 1127 | ret -1 |
| 1128 | end |
| 1129 | aligned = and add cur mask xor mask 0xffffffffffffffff |
| 1130 | if gt size sub 0xffffffffffffffff aligned |
| 1131 | ret -1 |
| 1132 | end |
| 1133 | nxt = sub add aligned size base |
| 1134 | if gt nxt cap |
| 1135 | ret -1 |
| 1136 | end |
| 1137 | put heap 1 nxt |
| 1138 | put dst_addr 0 aligned |
| 1139 | ret 0 |
| 1140 | end |
| 1141 | |
| 1142 | fn bc_smmu_validate tables_addr ulong stream_id uint to bool |
| 1143 | if eq tables_addr 0 |
| 1144 | ret false |
| 1145 | end |
| 1146 | if ne rem tables_addr BC_SMMU_TABLES_ALIGN 0 |
| 1147 | ret false |
| 1148 | end |
| 1149 | if ge stream_id BC_SMMU_STREAM_ENTRIES |
| 1150 | ret false |
| 1151 | end |
| 1152 | ret true |
| 1153 | end |
| 1154 | |
| 1155 | fn bc_mat_header_length hdr view uchar * to ulong |
| 1156 | if lt len hdr 16 |
| 1157 | ret 0 |
| 1158 | end |
| 1159 | cnt = ulong bc_get_u32_le hdr 4 |
| 1160 | stride = ulong bc_get_u32_le hdr 8 |
| 1161 | if gt cnt 1048576 |
| 1162 | ret 0 |
| 1163 | end |
| 1164 | if gt stride 1048576 |
| 1165 | ret 0 |
| 1166 | end |
| 1167 | prod = mul cnt stride |
| 1168 | if gt prod 1048560 |
| 1169 | ret 0 |
| 1170 | end |
| 1171 | ret add 16 prod |
| 1172 | end |
| 1173 | |
| 1174 | fn bc_page_align_range base ulong size ulong dst edit ulong 2 to bool |
| 1175 | page_off = and base 4095 |
| 1176 | start = sub base page_off |
| 1177 | if gt size sub 0xffffffffffffffff page_off |
| 1178 | ret false |
| 1179 | end |
| 1180 | span = add size page_off |
| 1181 | if gt span 0xfffffffffffff000 |
| 1182 | ret false |
| 1183 | end |
| 1184 | aligned_len = and add span 4095 xor ulong 4095 0xffffffffffffffff |
| 1185 | put dst 0 start |
| 1186 | put dst 1 aligned_len |
| 1187 | ret true |
| 1188 | end |
| 1189 | |
| 1190 | fn bc_mmu_switched el ulong mmu_on bool ttbr0_el1 ulong fw_ttbr0 ulong kroot ulong to bool |
| 1191 | if ne el 1 |
| 1192 | ret false |
| 1193 | end |
| 1194 | if not mmu_on |
| 1195 | ret false |
| 1196 | end |
| 1197 | el1_base = and ttbr0_el1 BC_TTBR_BADDR_MASK |
| 1198 | fw_base = and fw_ttbr0 BC_TTBR_BADDR_MASK |
| 1199 | if ne el1_base kroot |
| 1200 | ret false |
| 1201 | end |
| 1202 | if eq el1_base fw_base |
| 1203 | ret false |
| 1204 | end |
| 1205 | ret true |
| 1206 | end |
| 1207 | |
| 1208 | fn bc_timer_ticks_to_us counter ulong freq_hz ulong to ulong |
| 1209 | if eq freq_hz 0 |
| 1210 | ret 0 |
| 1211 | end |
| 1212 | q = div counter freq_hz |
| 1213 | r = rem counter freq_hz |
| 1214 | note Compute (q * 1000000) + (r * 1000000) / freq_hz without u64 overflow for normal rates |
| 1215 | ret add mul q 1000000 div mul r 1000000 freq_hz |
| 1216 | end |
| 1217 | |
| 1218 | fn bc_var_budget_init b edit ulong 3 max_writes ulong max_bytes ulong to void |
| 1219 | put b 0 0 |
| 1220 | put b 1 max_writes |
| 1221 | put b 2 max_bytes |
| 1222 | end |
| 1223 | |
| 1224 | fn bc_var_budget_try_write b edit ulong 3 msg_len ulong to int |
| 1225 | if gt msg_len get b 2 |
| 1226 | ret BC_VAR_ERR_TOO_LARGE |
| 1227 | end |
| 1228 | n = get b 0 |
| 1229 | put b 0 add n 1 |
| 1230 | if ge n get b 1 |
| 1231 | ret BC_VAR_ERR_BUDGET_SPENT |
| 1232 | end |
| 1233 | ret int add n 1 |
| 1234 | end |
| 1235 | |
| 1236 | fn bc_artifact_name_valid wide view ushort * dst edit uchar * to ulong |
| 1237 | wlen = bc_wide_len wide |
| 1238 | if le wlen 5 |
| 1239 | ret 0 |
| 1240 | end |
| 1241 | if gt wlen BC_ARTIFACT_NAME_BYTES |
| 1242 | ret 0 |
| 1243 | end |
| 1244 | if lt len dst wlen |
| 1245 | ret 0 |
| 1246 | end |
| 1247 | for i ulong wlen bound 32 |
| 1248 | u = get wide i |
| 1249 | if lt u 0x21 |
| 1250 | ret 0 |
| 1251 | end |
| 1252 | if gt u 0x7e |
| 1253 | ret 0 |
| 1254 | end |
| 1255 | put dst i uchar and u 255 |
| 1256 | end |
| 1257 | tail = sub wlen 5 |
| 1258 | for i ulong 5 |
| 1259 | c1 = bc_ascii_lower get dst add tail i |
| 1260 | c2 = get bc_artifact_suffix i |
| 1261 | if ne c1 c2 |
| 1262 | ret 0 |
| 1263 | end |
| 1264 | end |
| 1265 | ret wlen |
| 1266 | end |
| 1267 | |
| 1268 | fn bc_artifact_oversize size ulong to bool |
| 1269 | ret gt size BC_MAX_ARTIFACT_FILE_BYTES |
| 1270 | end |
| 1271 | |
| 1272 | fn bc_artifact_name_lt a view uchar * entries view uchar * idx ulong to bool |
| 1273 | a_len = len a |
| 1274 | off = mul idx BC_ARTIFACT_STRIDE |
| 1275 | b_len = bc_get_u64_le entries add off 32 |
| 1276 | n = a_len |
| 1277 | if lt b_len n |
| 1278 | n = b_len |
| 1279 | end |
| 1280 | for i ulong n bound 32 |
| 1281 | ca = get a i |
| 1282 | cb = get entries add off i |
| 1283 | if lt ca cb |
| 1284 | ret true |
| 1285 | end |
| 1286 | if gt ca cb |
| 1287 | ret false |
| 1288 | end |
| 1289 | end |
| 1290 | ret lt a_len b_len |
| 1291 | end |
| 1292 | |
| 1293 | fn bc_artifact_copy_entry entries edit uchar * dst_idx ulong src_idx ulong to void |
| 1294 | doff = mul dst_idx BC_ARTIFACT_STRIDE |
| 1295 | soff = mul src_idx BC_ARTIFACT_STRIDE |
| 1296 | for i ulong BC_ARTIFACT_STRIDE |
| 1297 | put entries add doff i get entries add soff i |
| 1298 | end |
| 1299 | end |
| 1300 | |
| 1301 | fn bc_artifact_write_entry entries edit uchar * idx ulong name view uchar * size ulong to void |
| 1302 | off = mul idx BC_ARTIFACT_STRIDE |
| 1303 | nlen = len name |
| 1304 | for i ulong 32 |
| 1305 | if lt i nlen |
| 1306 | put entries add off i get name i |
| 1307 | else |
| 1308 | put entries add off i 0 |
| 1309 | end |
| 1310 | end |
| 1311 | bc_put_u64_le entries add off 32 nlen |
| 1312 | bc_put_u64_le entries add off 40 size |
| 1313 | end |
| 1314 | |
| 1315 | fn bc_artifact_insert_sorted entries edit uchar * meta edit ulong 2 new_name view uchar * new_size ulong to ulong |
| 1316 | note entries holds up to 48 records of 48 bytes (2304 bytes total); meta[0]=count, meta[1]=ignored |
| 1317 | cnt = get meta 0 |
| 1318 | ign = get meta 1 |
| 1319 | ins_pos = cnt |
| 1320 | for i ulong cnt bound 48 |
| 1321 | if bc_artifact_name_lt new_name entries i |
| 1322 | ins_pos = i |
| 1323 | break |
| 1324 | end |
| 1325 | end |
| 1326 | if eq ins_pos BC_MAX_ARTIFACT_CANDIDATES |
| 1327 | put meta 1 add ign 1 |
| 1328 | ret cnt |
| 1329 | end |
| 1330 | if eq cnt BC_MAX_ARTIFACT_CANDIDATES |
| 1331 | put meta 1 add ign 1 |
| 1332 | else |
| 1333 | cnt = add cnt 1 |
| 1334 | put meta 0 cnt |
| 1335 | end |
| 1336 | if gt cnt add ins_pos 1 |
| 1337 | steps = sub sub cnt 1 ins_pos |
| 1338 | for s ulong steps bound 48 |
| 1339 | dst_idx = sub sub cnt 1 s |
| 1340 | bc_artifact_copy_entry entries dst_idx sub dst_idx 1 |
| 1341 | end |
| 1342 | end |
| 1343 | bc_artifact_write_entry entries ins_pos new_name new_size |
| 1344 | ret cnt |
| 1345 | end |
| 1346 | |
| 1347 | fn bc_rsdp_xsdt_addr rsdp view uchar * to ulong |
| 1348 | if lt len rsdp 36 |
| 1349 | ret 0 |
| 1350 | end |
| 1351 | for i ulong 8 |
| 1352 | if ne get rsdp i get bc_rsdp_sig i |
| 1353 | ret 0 |
| 1354 | end |
| 1355 | end |
| 1356 | ret bc_get_u64_le rsdp 24 |
| 1357 | end |
| 1358 | |
| 1359 | fn bc_stage_valid s ulong to bool |
| 1360 | ret lt s BC_STAGE_COUNT |
| 1361 | end |
| 1362 | |
| 1363 | fn bc_el0_report_ok write_ok bool forged_denied bool fault_contained bool exit_code int to bool |
| 1364 | if not write_ok |
| 1365 | ret false |
| 1366 | end |
| 1367 | if not forged_denied |
| 1368 | ret false |
| 1369 | end |
| 1370 | if not fault_contained |
| 1371 | ret false |
| 1372 | end |
| 1373 | ret eq exit_code 0 |
| 1374 | end |
| 1375 | |
| 1376 | fn bc_preempt_ok a ulong b ulong switches ulong to bool |
| 1377 | if eq a 0 |
| 1378 | ret false |
| 1379 | end |
| 1380 | if eq b 0 |
| 1381 | ret false |
| 1382 | end |
| 1383 | ret ge switches 4 |
| 1384 | end |