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 / model_map.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/boot/model_map.om: Pure-Omega MODEL.MAP (AIENMDL1) 4096-byte Extent Map Validator. |
| 3 | note Converted from upstream aienux native/boot/model_map.h. |
| 4 | |
| 5 | const MMAP_BYTES ulong 4096 |
| 6 | const MMAP_VERSION uint 1 |
| 7 | const MMAP_MAX_EXTENTS uint 128 |
| 8 | |
| 9 | const MMAP_OFF_MAGIC ulong 0 |
| 10 | const MMAP_OFF_VERSION ulong 8 |
| 11 | const MMAP_OFF_SECTOR_SZ ulong 12 |
| 12 | const MMAP_OFF_DISK_GUID ulong 16 |
| 13 | const MMAP_OFF_MODEL_LEN ulong 32 |
| 14 | const MMAP_OFF_SHA256 ulong 40 |
| 15 | const MMAP_OFF_N_EXTENTS ulong 72 |
| 16 | const MMAP_OFF_FLAGS ulong 76 |
| 17 | const MMAP_OFF_EXTENTS ulong 80 |
| 18 | |
| 19 | const MMAP_OK int 0 |
| 20 | const MMAP_ERR_SHORT int -1 |
| 21 | const MMAP_ERR_MAGIC int -2 |
| 22 | const MMAP_ERR_VERSION int -3 |
| 23 | const MMAP_ERR_SECTOR_SZ int -4 |
| 24 | const MMAP_ERR_BLK_MISMATCH int -5 |
| 25 | const MMAP_ERR_FLAGS int -6 |
| 26 | const MMAP_ERR_ZERO_LEN int -7 |
| 27 | const MMAP_ERR_EXT_COUNT int -8 |
| 28 | const MMAP_ERR_EXT_BAD int -9 |
| 29 | const MMAP_ERR_EXT_PAST_DISK int -10 |
| 30 | const MMAP_ERR_EXT_OVERFLOW int -11 |
| 31 | const MMAP_ERR_COVERAGE int -12 |
| 32 | |
| 33 | text mmap_magic "AIENMDL1" |
| 34 | |
| 35 | fn model_map_sum_extents buf view uchar * nexts uint last_block ulong sec_out edit ulong 1 to int |
| 36 | sectors = ulong 0 |
| 37 | i = ulong 0 |
| 38 | n = ulong nexts |
| 39 | while lt i n bound 128 |
| 40 | eoff = add MMAP_OFF_EXTENTS mul i 16 |
| 41 | elba = boot_rd64_le buf eoff |
| 42 | ecnt = boot_rd64_le buf add eoff 8 |
| 43 | if eq ecnt 0 |
| 44 | ret MMAP_ERR_EXT_BAD |
| 45 | end |
| 46 | if lt elba 2 |
| 47 | ret MMAP_ERR_EXT_BAD |
| 48 | end |
| 49 | eend = add elba ecnt |
| 50 | if lt eend elba |
| 51 | ret MMAP_ERR_EXT_PAST_DISK |
| 52 | end |
| 53 | if ne last_block 0 |
| 54 | if gt sub eend 1 last_block |
| 55 | ret MMAP_ERR_EXT_PAST_DISK |
| 56 | end |
| 57 | end |
| 58 | if lt add sectors ecnt sectors |
| 59 | ret MMAP_ERR_EXT_OVERFLOW |
| 60 | end |
| 61 | sectors = add sectors ecnt |
| 62 | i = add i 1 |
| 63 | end |
| 64 | put sec_out 0 sectors |
| 65 | ret MMAP_OK |
| 66 | end |
| 67 | |
| 68 | fn model_map_check buf view uchar * block_size uint last_block ulong to int |
| 69 | if lt len buf MMAP_BYTES |
| 70 | ret MMAP_ERR_SHORT |
| 71 | end |
| 72 | if not boot_eq_bytes part buf MMAP_OFF_MAGIC 8 mmap_magic |
| 73 | ret MMAP_ERR_MAGIC |
| 74 | end |
| 75 | if ne boot_rd32_le buf MMAP_OFF_VERSION MMAP_VERSION |
| 76 | ret MMAP_ERR_VERSION |
| 77 | end |
| 78 | secsz = boot_rd32_le buf MMAP_OFF_SECTOR_SZ |
| 79 | if and ne secsz 512 ne secsz 4096 |
| 80 | ret MMAP_ERR_SECTOR_SZ |
| 81 | end |
| 82 | if and ne block_size 0 ne secsz block_size |
| 83 | ret MMAP_ERR_BLK_MISMATCH |
| 84 | end |
| 85 | if ne boot_rd32_le buf MMAP_OFF_FLAGS 0 |
| 86 | ret MMAP_ERR_FLAGS |
| 87 | end |
| 88 | mlen = boot_rd64_le buf MMAP_OFF_MODEL_LEN |
| 89 | if eq mlen 0 |
| 90 | ret MMAP_ERR_ZERO_LEN |
| 91 | end |
| 92 | nexts = boot_rd32_le buf MMAP_OFF_N_EXTENTS |
| 93 | if or eq nexts 0 gt nexts MMAP_MAX_EXTENTS |
| 94 | ret MMAP_ERR_EXT_COUNT |
| 95 | end |
| 96 | sbox = new ulong 1 0 |
| 97 | rc = model_map_sum_extents buf nexts last_block sbox |
| 98 | if ne rc MMAP_OK |
| 99 | ret rc |
| 100 | end |
| 101 | ssz = ulong secsz |
| 102 | rem_m = rem mlen ssz |
| 103 | need = div mlen ssz |
| 104 | if ne rem_m 0 |
| 105 | need = add need 1 |
| 106 | end |
| 107 | if ne get sbox 0 need |
| 108 | ret MMAP_ERR_COVERAGE |
| 109 | end |
| 110 | ret MMAP_OK |
| 111 | end |