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 / handoff.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/boot/handoff.om: Pure-Omega CHANDOF3 Boot Handoff Contract & Validator. |
| 3 | note Converted from upstream aienux native/boot/handoff.h and native/kernel/core/handoff_check.c. |
| 4 | note Compose with: si/directives/om2.om aienux/boot/handoff.om aienux/boot/model_map.om aienux/boot/slot.om aienux/boot/netboot.om <test.om> om/tls/sha2.om |
| 5 | |
| 6 | note Shared boot subsystem structs (declared before first fn per OM-2 rule) |
| 7 | struct SlotEval valid uint generation ulong policy_gen ulong err int end |
| 8 | |
| 9 | struct SlotDecision boot_slot int reason uint loader_write uint end |
| 10 | |
| 11 | struct NbMsg mtype uchar status uchar seconds ushort count ushort mlen ushort total uint offset uint end |
| 12 | |
| 13 | note CHANDOF3 magic constants (little-endian "CHANDOF3", "CHANDOF1", "CHANDOF2", stem "CHANDOF") |
| 14 | const HO_MAGIC_V3 ulong 0x33464f444e414843 |
| 15 | const HO_MAGIC_V2 ulong 0x32464f444e414843 |
| 16 | const HO_MAGIC_V1 ulong 0x31464f444e414843 |
| 17 | const HO_MAGIC_STEM ulong 0x00464f444e414843 |
| 18 | const HO_STEM_MASK ulong 0x00ffffffffffffff |
| 19 | const HO_BYTES ulong 232 |
| 20 | |
| 21 | note Handoff field offsets (pinned to CHANDOF3 LP64 ABI) |
| 22 | const HO_OFF_MAGIC ulong 0 |
| 23 | const HO_OFF_FW_EL ulong 8 |
| 24 | const HO_OFF_DESC_VER ulong 12 |
| 25 | const HO_OFF_FW_TTBR0 ulong 16 |
| 26 | const HO_OFF_FW_SCTLR ulong 24 |
| 27 | const HO_OFF_RSDP ulong 32 |
| 28 | const HO_OFF_MMAP ulong 40 |
| 29 | const HO_OFF_MAP_SIZE ulong 48 |
| 30 | const HO_OFF_DESC_SIZE ulong 56 |
| 31 | const HO_OFF_IMG_BASE ulong 64 |
| 32 | const HO_OFF_IMG_END ulong 72 |
| 33 | const HO_OFF_CNT_FREQ ulong 80 |
| 34 | const HO_OFF_ENTRY_TICKS ulong 88 |
| 35 | const HO_OFF_EXIT_ATTEMPTS ulong 96 |
| 36 | const HO_OFF_RESERVED0 ulong 100 |
| 37 | const HO_OFF_COMMIT ulong 104 |
| 38 | const HO_OFF_MODEL_BASE ulong 112 |
| 39 | const HO_OFF_MODEL_LEN ulong 120 |
| 40 | const HO_OFF_MODEL_SHA256 ulong 128 |
| 41 | const HO_OFF_MODEL_FLAGS ulong 160 |
| 42 | const HO_OFF_MODEL_EXTENTS ulong 164 |
| 43 | const HO_OFF_MODEL_READ_US ulong 168 |
| 44 | const HO_OFF_MODEL_LAST_BLK ulong 176 |
| 45 | const HO_OFF_MODEL_BLK_SZ ulong 184 |
| 46 | const HO_OFF_RESERVED1 ulong 188 |
| 47 | const HO_OFF_FB_BASE ulong 192 |
| 48 | const HO_OFF_FB_SIZE ulong 200 |
| 49 | const HO_OFF_FB_WIDTH ulong 208 |
| 50 | const HO_OFF_FB_HEIGHT ulong 212 |
| 51 | const HO_OFF_FB_PITCH ulong 216 |
| 52 | const HO_OFF_FB_FORMAT ulong 220 |
| 53 | const HO_OFF_FB_STATUS ulong 224 |
| 54 | const HO_OFF_FB_GOP_HANDLES ulong 228 |
| 55 | |
| 56 | note Model flags |
| 57 | const HO_MODEL_PRESENT uint 1 |
| 58 | const HO_MODEL_BLOCKIO uint 2 |
| 59 | |
| 60 | note Framebuffer status and format constants |
| 61 | const HO_FB_NONE uint 0 |
| 62 | const HO_FB_PRESENT uint 1 |
| 63 | const HO_FB_NO_LINEAR uint 2 |
| 64 | const HO_FB_RGBX uint 0 |
| 65 | const HO_FB_BGRX uint 1 |
| 66 | const HO_FB_MAX_DIM uint 16384 |
| 67 | |
| 68 | note Memory map limits |
| 69 | const HO_MAP_MAX ulong 65536 |
| 70 | const HO_DESC_MIN ulong 40 |
| 71 | const HO_DESC_VERSION uint 1 |
| 72 | |
| 73 | note Handoff check return codes |
| 74 | const HO_OK int 0 |
| 75 | const HO_ERR_SHORT int -1 |
| 76 | const HO_ERR_BAD_MAGIC int -2 |
| 77 | const HO_ERR_UNSUP_VER int -3 |
| 78 | const HO_ERR_RESERVED0 int -4 |
| 79 | const HO_ERR_RESERVED1 int -5 |
| 80 | const HO_ERR_MODEL_FLAGS int -6 |
| 81 | const HO_ERR_MODEL_RANGE int -7 |
| 82 | const HO_ERR_MODEL_ALIGN int -8 |
| 83 | const HO_ERR_MODEL_OVERFLOW int -9 |
| 84 | const HO_ERR_MODEL_BLKSZ int -10 |
| 85 | const HO_ERR_MODEL_EXTENTS int -11 |
| 86 | const HO_ERR_MODEL_SPURIOUS int -12 |
| 87 | const HO_ERR_FB_SPURIOUS int -13 |
| 88 | const HO_ERR_FB_STATUS int -14 |
| 89 | const HO_ERR_FB_FORMAT int -15 |
| 90 | const HO_ERR_FB_DIM int -16 |
| 91 | const HO_ERR_FB_BASE int -17 |
| 92 | const HO_ERR_FB_SIZE int -18 |
| 93 | const HO_ERR_FB_OVERFLOW int -19 |
| 94 | const HO_ERR_FB_GOP int -20 |
| 95 | const HO_ERR_DESC_SIZE int -21 |
| 96 | const HO_ERR_DESC_VER int -22 |
| 97 | const HO_ERR_MAP_SIZE int -23 |
| 98 | |
| 99 | note Little-endian byte helpers shared by boot modules |
| 100 | fn boot_rd16_le buf view uchar * off ulong to ushort |
| 101 | b0 = ushort get buf off |
| 102 | b1 = ushort get buf add off 1 |
| 103 | ret or b0 shl b1 8 |
| 104 | end |
| 105 | |
| 106 | fn boot_rd32_le buf view uchar * off ulong to uint |
| 107 | b0 = uint get buf off |
| 108 | b1 = uint get buf add off 1 |
| 109 | b2 = uint get buf add off 2 |
| 110 | b3 = uint get buf add off 3 |
| 111 | ret or or b0 shl b1 8 or shl b2 16 shl b3 24 |
| 112 | end |
| 113 | |
| 114 | fn boot_rd64_le buf view uchar * off ulong to ulong |
| 115 | lo = ulong boot_rd32_le buf off |
| 116 | hi = ulong boot_rd32_le buf add off 4 |
| 117 | ret or lo shl hi 32 |
| 118 | end |
| 119 | |
| 120 | fn boot_wr16_le buf edit uchar * off ulong v ushort to void |
| 121 | put buf off uchar and v 0xff |
| 122 | put buf add off 1 uchar and shr v 8 0xff |
| 123 | end |
| 124 | |
| 125 | fn boot_wr32_le buf edit uchar * off ulong v uint to void |
| 126 | put buf off uchar and v 0xff |
| 127 | put buf add off 1 uchar and shr v 8 0xff |
| 128 | put buf add off 2 uchar and shr v 16 0xff |
| 129 | put buf add off 3 uchar and shr v 24 0xff |
| 130 | end |
| 131 | |
| 132 | fn boot_wr64_le buf edit uchar * off ulong v ulong to void |
| 133 | boot_wr32_le buf off uint and v 0xffffffff |
| 134 | boot_wr32_le buf add off 4 uint and shr v 32 0xffffffff |
| 135 | end |
| 136 | |
| 137 | fn boot_all_zero buf view uchar * off ulong n ulong to bool |
| 138 | acc = uchar 0 |
| 139 | i = ulong 0 |
| 140 | while lt i n bound 4096 |
| 141 | acc = or acc get buf add off i |
| 142 | i = add i 1 |
| 143 | end |
| 144 | ret eq acc 0 |
| 145 | end |
| 146 | |
| 147 | fn boot_eq_bytes a view uchar * b view uchar * to bool |
| 148 | na = len a |
| 149 | if ne na len b |
| 150 | ret false |
| 151 | end |
| 152 | diff = uchar 0 |
| 153 | i = ulong 0 |
| 154 | while lt i na bound 4096 |
| 155 | diff = or diff xor get a i get b i |
| 156 | i = add i 1 |
| 157 | end |
| 158 | ret eq diff 0 |
| 159 | end |
| 160 | |
| 161 | fn boot_eq_within buf view uchar * o1 ulong o2 ulong n ulong to bool |
| 162 | diff = uchar 0 |
| 163 | i = ulong 0 |
| 164 | while lt i n bound 4096 |
| 165 | diff = or diff xor get buf add o1 i get buf add o2 i |
| 166 | i = add i 1 |
| 167 | end |
| 168 | ret eq diff 0 |
| 169 | end |
| 170 | |
| 171 | fn handoff_check_model buf view uchar * to int |
| 172 | mflags = boot_rd32_le buf HO_OFF_MODEL_FLAGS |
| 173 | allowed = or HO_MODEL_PRESENT HO_MODEL_BLOCKIO |
| 174 | if ne and mflags xor uint 0xffffffff allowed 0 |
| 175 | ret HO_ERR_MODEL_FLAGS |
| 176 | end |
| 177 | if ne and mflags HO_MODEL_PRESENT 0 |
| 178 | mbase = boot_rd64_le buf HO_OFF_MODEL_BASE |
| 179 | mlen = boot_rd64_le buf HO_OFF_MODEL_LEN |
| 180 | if or eq mbase 0 eq mlen 0 |
| 181 | ret HO_ERR_MODEL_RANGE |
| 182 | end |
| 183 | if ne and mbase 0xfff 0 |
| 184 | ret HO_ERR_MODEL_ALIGN |
| 185 | end |
| 186 | if lt add mbase mlen mbase |
| 187 | ret HO_ERR_MODEL_OVERFLOW |
| 188 | end |
| 189 | blksz = boot_rd32_le buf HO_OFF_MODEL_BLK_SZ |
| 190 | if and ne blksz 512 ne blksz 4096 |
| 191 | ret HO_ERR_MODEL_BLKSZ |
| 192 | end |
| 193 | if eq boot_rd32_le buf HO_OFF_MODEL_EXTENTS 0 |
| 194 | ret HO_ERR_MODEL_EXTENTS |
| 195 | end |
| 196 | ret HO_OK |
| 197 | end |
| 198 | if ne boot_rd64_le buf HO_OFF_MODEL_BASE 0 |
| 199 | ret HO_ERR_MODEL_SPURIOUS |
| 200 | end |
| 201 | if ne boot_rd64_le buf HO_OFF_MODEL_LEN 0 |
| 202 | ret HO_ERR_MODEL_SPURIOUS |
| 203 | end |
| 204 | if ne mflags 0 |
| 205 | ret HO_ERR_MODEL_SPURIOUS |
| 206 | end |
| 207 | if ne boot_rd32_le buf HO_OFF_MODEL_EXTENTS 0 |
| 208 | ret HO_ERR_MODEL_SPURIOUS |
| 209 | end |
| 210 | if ne boot_rd64_le buf HO_OFF_MODEL_READ_US 0 |
| 211 | ret HO_ERR_MODEL_SPURIOUS |
| 212 | end |
| 213 | if ne boot_rd64_le buf HO_OFF_MODEL_LAST_BLK 0 |
| 214 | ret HO_ERR_MODEL_SPURIOUS |
| 215 | end |
| 216 | if ne boot_rd32_le buf HO_OFF_MODEL_BLK_SZ 0 |
| 217 | ret HO_ERR_MODEL_SPURIOUS |
| 218 | end |
| 219 | if not boot_all_zero buf HO_OFF_MODEL_SHA256 32 |
| 220 | ret HO_ERR_MODEL_SPURIOUS |
| 221 | end |
| 222 | ret HO_OK |
| 223 | end |
| 224 | |
| 225 | fn handoff_check_fb buf view uchar * to int |
| 226 | fst = boot_rd32_le buf HO_OFF_FB_STATUS |
| 227 | fbase = boot_rd64_le buf HO_OFF_FB_BASE |
| 228 | fsize = boot_rd64_le buf HO_OFF_FB_SIZE |
| 229 | fw = boot_rd32_le buf HO_OFF_FB_WIDTH |
| 230 | fh = boot_rd32_le buf HO_OFF_FB_HEIGHT |
| 231 | fp = boot_rd32_le buf HO_OFF_FB_PITCH |
| 232 | ffmt = boot_rd32_le buf HO_OFF_FB_FORMAT |
| 233 | fgop = boot_rd32_le buf HO_OFF_FB_GOP_HANDLES |
| 234 | if eq fst HO_FB_NONE |
| 235 | if or ne fbase 0 ne fsize 0 |
| 236 | ret HO_ERR_FB_SPURIOUS |
| 237 | end |
| 238 | if or ne fw 0 or ne fh 0 ne fp 0 |
| 239 | ret HO_ERR_FB_SPURIOUS |
| 240 | end |
| 241 | if or ne ffmt 0 ne fgop 0 |
| 242 | ret HO_ERR_FB_SPURIOUS |
| 243 | end |
| 244 | ret HO_OK |
| 245 | end |
| 246 | if eq fst HO_FB_NO_LINEAR |
| 247 | if or ne fbase 0 ne fsize 0 |
| 248 | ret HO_ERR_FB_SIZE |
| 249 | end |
| 250 | if eq fgop 0 |
| 251 | ret HO_ERR_FB_GOP |
| 252 | end |
| 253 | ret HO_OK |
| 254 | end |
| 255 | if ne fst HO_FB_PRESENT |
| 256 | ret HO_ERR_FB_STATUS |
| 257 | end |
| 258 | if and ne ffmt HO_FB_RGBX ne ffmt HO_FB_BGRX |
| 259 | ret HO_ERR_FB_FORMAT |
| 260 | end |
| 261 | if or eq fw 0 gt fw HO_FB_MAX_DIM |
| 262 | ret HO_ERR_FB_DIM |
| 263 | end |
| 264 | if or eq fh 0 gt fh HO_FB_MAX_DIM |
| 265 | ret HO_ERR_FB_DIM |
| 266 | end |
| 267 | if or lt fp fw gt fp HO_FB_MAX_DIM |
| 268 | ret HO_ERR_FB_DIM |
| 269 | end |
| 270 | if or eq fbase 0 ne and fbase 3 0 |
| 271 | ret HO_ERR_FB_BASE |
| 272 | end |
| 273 | need = mul mul ulong fp ulong fh 4 |
| 274 | if lt fsize need |
| 275 | ret HO_ERR_FB_SIZE |
| 276 | end |
| 277 | if lt add fbase fsize fbase |
| 278 | ret HO_ERR_FB_OVERFLOW |
| 279 | end |
| 280 | if eq fgop 0 |
| 281 | ret HO_ERR_FB_GOP |
| 282 | end |
| 283 | ret HO_OK |
| 284 | end |
| 285 | |
| 286 | fn handoff_check buf view uchar * to int |
| 287 | if lt len buf HO_BYTES |
| 288 | ret HO_ERR_SHORT |
| 289 | end |
| 290 | magic = boot_rd64_le buf HO_OFF_MAGIC |
| 291 | if ne magic HO_MAGIC_V3 |
| 292 | ver_ch = shr magic 56 |
| 293 | if eq and magic HO_STEM_MASK HO_MAGIC_STEM |
| 294 | if and ge ver_ch 48 le ver_ch 57 |
| 295 | ret HO_ERR_UNSUP_VER |
| 296 | end |
| 297 | end |
| 298 | ret HO_ERR_BAD_MAGIC |
| 299 | end |
| 300 | if ne boot_rd32_le buf HO_OFF_RESERVED0 0 |
| 301 | ret HO_ERR_RESERVED0 |
| 302 | end |
| 303 | if ne boot_rd32_le buf HO_OFF_RESERVED1 0 |
| 304 | ret HO_ERR_RESERVED1 |
| 305 | end |
| 306 | mrc = handoff_check_model buf |
| 307 | if ne mrc HO_OK |
| 308 | ret mrc |
| 309 | end |
| 310 | frc = handoff_check_fb buf |
| 311 | if ne frc HO_OK |
| 312 | ret frc |
| 313 | end |
| 314 | dsz = boot_rd64_le buf HO_OFF_DESC_SIZE |
| 315 | if or lt dsz HO_DESC_MIN ne and dsz 7 0 |
| 316 | ret HO_ERR_DESC_SIZE |
| 317 | end |
| 318 | if ne boot_rd32_le buf HO_OFF_DESC_VER HO_DESC_VERSION |
| 319 | ret HO_ERR_DESC_VER |
| 320 | end |
| 321 | msz = boot_rd64_le buf HO_OFF_MAP_SIZE |
| 322 | if or eq msz 0 or gt msz HO_MAP_MAX ne rem msz dsz 0 |
| 323 | ret HO_ERR_MAP_SIZE |
| 324 | end |
| 325 | ret HO_OK |
| 326 | end |