aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / boot / model_map.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* model_map.h -- MODEL.MAP: where the yardstick model's bytes live on the |
| 18 | * boot disk, so the UEFI boot stub can read them through the firmware's own |
| 19 | * Block I/O driver before ExitBootServices (aienos#34 lane 6, hardware model |
| 20 | * ingest). Shared by the stub (native/boot/efi_model.c), the kernel's |
| 21 | * handoff checks and the hosted map tool (native/kernel/tools/ck_model_map.c). |
| 22 | * |
| 23 | * The file is exactly 4096 bytes, little endian, at \EFI\AIENOS\MODEL.MAP on |
| 24 | * the boot volume (the ESP the stub was loaded from). It names the whole disk |
| 25 | * by its GPT disk GUID (header LBA 1, bytes 56..71, raw on-disk order) and |
| 26 | * lists the model's extents as absolute LBAs of that disk, in FILE order |
| 27 | * (extent 0 holds byte 0 of the model). The model bytes themselves stay where |
| 28 | * they are (an ext4 file, or a raw range in a QEMU image); only this map is |
| 29 | * staged. Integrity: the declared SHA-256 of the model is carried to the kernel |
| 30 | * in the handoff and the kernel recomputes it over the bytes it received; any |
| 31 | * wrong LBA, short read or stale file shows up as a hash mismatch and the |
| 32 | * kernel refuses to decode (never a silent wrong model). |
| 33 | * |
| 34 | * The map has no checksum of its own: every field that could steer a read |
| 35 | * wrong is covered by the SHA-256 check, and a malformed map is refused by |
| 36 | * ck_model_map_check below (the same function in the stub and the tool). */ |
| 37 | #ifndef AIENOS_CK_MODEL_MAP_H |
| 38 | #define AIENOS_CK_MODEL_MAP_H |
| 39 | #include <stdint.h> |
| 40 | #include <stddef.h> |
| 41 | |
| 42 | #define CK_MODEL_MAP_MAGIC "AIENMDL1" |
| 43 | #define CK_MODEL_MAP_VERSION 1u |
| 44 | #define CK_MODEL_MAP_BYTES 4096u |
| 45 | #define CK_MODEL_MAP_MAX_EXTENTS 128u |
| 46 | #define CK_MODEL_MAP_PATH "\\EFI\\AIENOS\\MODEL.MAP" |
| 47 | |
| 48 | struct ck_model_extent { |
| 49 | uint64_t lba; /* first LBA of this extent on the whole disk */ |
| 50 | uint64_t count; /* sectors in this extent (> 0) */ |
| 51 | }; |
| 52 | |
| 53 | struct ck_model_map { |
| 54 | uint8_t magic[8]; /* "AIENMDL1" */ |
| 55 | uint32_t version; /* 1 */ |
| 56 | uint32_t sector_size; /* 512 or 4096; must equal the disk's BlockSize */ |
| 57 | uint8_t disk_guid[16]; /* GPT header bytes 56..71 of LBA 1, raw */ |
| 58 | uint64_t model_len; /* bytes of the model file */ |
| 59 | uint8_t sha256[32]; /* SHA-256 of the model file */ |
| 60 | uint32_t n_extents; /* 1..CK_MODEL_MAP_MAX_EXTENTS */ |
| 61 | uint32_t flags; /* 0 */ |
| 62 | struct ck_model_extent ext[CK_MODEL_MAP_MAX_EXTENTS]; |
| 63 | uint8_t pad[CK_MODEL_MAP_BYTES - 80u - 16u * CK_MODEL_MAP_MAX_EXTENTS]; |
| 64 | }; |
| 65 | _Static_assert(sizeof(struct ck_model_map) == CK_MODEL_MAP_BYTES, "ck_model_map size"); |
| 66 | _Static_assert(offsetof(struct ck_model_map, ext) == 80, "ck_model_map.ext offset"); |
| 67 | |
| 68 | /* Structural check, no I/O. block_size 0 / last_block 0 skip the disk |
| 69 | * comparisons (the host tool before it knows the disk). 0 = well formed; -1 |
| 70 | * with *why set otherwise. */ |
| 71 | static inline int ck_model_map_check(const struct ck_model_map *m, uint32_t block_size, |
| 72 | uint64_t last_block, const char **why) |
| 73 | { |
| 74 | static const uint8_t magic[8] = CK_MODEL_MAP_MAGIC; |
| 75 | for (unsigned i = 0; i < 8; i++) |
| 76 | if (m->magic[i] != magic[i]) { |
| 77 | *why = "bad magic"; |
| 78 | return -1; |
| 79 | } |
| 80 | if (m->version != CK_MODEL_MAP_VERSION) { |
| 81 | *why = "unsupported version"; |
| 82 | return -1; |
| 83 | } |
| 84 | if (m->sector_size != 512u && m->sector_size != 4096u) { |
| 85 | *why = "sector size not 512 or 4096"; |
| 86 | return -1; |
| 87 | } |
| 88 | if (block_size && m->sector_size != block_size) { |
| 89 | *why = "sector size differs from the disk block size"; |
| 90 | return -1; |
| 91 | } |
| 92 | if (m->flags != 0) { |
| 93 | *why = "unknown flags"; |
| 94 | return -1; |
| 95 | } |
| 96 | if (m->model_len == 0) { |
| 97 | *why = "zero model length"; |
| 98 | return -1; |
| 99 | } |
| 100 | if (m->n_extents == 0 || m->n_extents > CK_MODEL_MAP_MAX_EXTENTS) { |
| 101 | *why = "extent count out of range"; |
| 102 | return -1; |
| 103 | } |
| 104 | uint64_t sectors = 0; |
| 105 | for (uint32_t i = 0; i < m->n_extents; i++) { |
| 106 | uint64_t lba = m->ext[i].lba, n = m->ext[i].count; |
| 107 | if (n == 0 || lba < 2 /* MBR + GPT header */) { |
| 108 | *why = "extent empty or inside the GPT header"; |
| 109 | return -1; |
| 110 | } |
| 111 | if (lba + n < lba || (last_block && lba + n - 1 > last_block)) { |
| 112 | *why = "extent past the end of the disk"; |
| 113 | return -1; |
| 114 | } |
| 115 | if (sectors + n < sectors) { |
| 116 | *why = "extent total overflows"; |
| 117 | return -1; |
| 118 | } |
| 119 | sectors += n; |
| 120 | } |
| 121 | /* Exact coverage: the extents hold the whole model and less than one |
| 122 | * extra sector (the last sector may be partial). */ |
| 123 | uint64_t need = (m->model_len + m->sector_size - 1) / m->sector_size; |
| 124 | if (sectors != need) { |
| 125 | *why = "extents do not cover exactly the model length"; |
| 126 | return -1; |
| 127 | } |
| 128 | *why = 0; |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | #endif |