interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / tools / ck_model_map.c

316 lines · 13265 bytes Raw Blame History
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/* ck_model_map -- host tool for MODEL.MAP (native/boot/model_map.h): where
18 * the yardstick model's bytes live on the boot disk, so the UEFI boot stub
19 * can read them through the firmware's Block I/O driver (aienos#34 lane 6).
20 * Hosted C, Linux only (FIEMAP), test and staging tooling; never part of an
21 * image. No Python.
22 *
23 * ck_model_map make OUT.MAP --file GGUF --disk DEV|IMG [--sector 512]
24 * (--fiemap [--part-start LBA|auto] | --extent LBA:COUNT ...)
25 * [--corrupt-sha]
26 * Reads GGUF (length + SHA-256), the disk's GPT disk GUID (LBA 1 bytes
27 * 56..71 of DEV or IMG), and the extents: --extent lists them in file
28 * order (QEMU images, from ck_gpt_image esp-model); --fiemap asks the
29 * filesystem where the file's blocks are (FS_IOC_FIEMAP) and converts
30 * partition-relative byte offsets to whole-disk LBAs with --part-start
31 * (the partition's first LBA; "auto" reads /sys/dev/block/MAJ:MIN/start
32 * of the file's own device). Every FIEMAP extent must be plain mapped
33 * data (no delalloc, unwritten, inline, encoded or unknown extents) and
34 * the extents must cover the file exactly, else refused. --corrupt-sha
35 * flips the last SHA-256 byte (the gate's negative control: the stub
36 * reads fine, the kernel must refuse to decode).
37 * ck_model_map show MAP
38 * ck_model_map check MAP --disk DEV|IMG
39 * Structural check, GUID match against the disk, then reads every extent
40 * by LBA from DEV|IMG and recomputes the SHA-256: exit 0 only when it
41 * equals the declared hash. The same bytes the firmware will read, read
42 * the same way from Linux: the pre-flight for the attended native boot. */
43#define _GNU_SOURCE
44#include <errno.h>
45#include <fcntl.h>
46#include <stdint.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#include <sys/ioctl.h>
51#include <sys/stat.h>
52#include <sys/sysmacros.h>
53#include <unistd.h>
54#include <linux/fs.h>
55#include <linux/fiemap.h>
56#include "model_map.h"
57#include "sha256.h"
58
59static int usage(void)
60{
61 fprintf(stderr,
62 "usage: ck_model_map make OUT.MAP --file GGUF --disk DEV|IMG [--sector 512]\n"
63 " (--fiemap [--part-start LBA|auto] | --extent LBA:COUNT ...) [--corrupt-sha]\n"
64 " ck_model_map show MAP\n"
65 " ck_model_map check MAP --disk DEV|IMG\n");
66 return 2;
67}
68
69static void hex(char *out, const uint8_t *b, unsigned n)
70{
71 static const char d[] = "0123456789abcdef";
72 for (unsigned i = 0; i < n; i++) {
73 out[2 * i] = d[b[i] >> 4];
74 out[2 * i + 1] = d[b[i] & 15];
75 }
76 out[2 * n] = 0;
77}
78
79static int sha_file(FILE *f, uint64_t *len, uint8_t out[32])
80{
81 static uint8_t buf[1u << 20];
82 sha256_ctx c;
83 sha256_init(&c);
84 *len = 0;
85 size_t n;
86 while ((n = fread(buf, 1, sizeof buf, f)) > 0) {
87 sha256_update(&c, buf, n);
88 *len += n;
89 }
90 if (ferror(f)) return -1;
91 sha256_final(&c, out);
92 return 0;
93}
94
95/* GPT disk GUID from LBA 1 of DEV|IMG (raw 16 bytes). */
96static int disk_guid(const char *disk, uint32_t sector, uint8_t out[16])
97{
98 int fd = open(disk, O_RDONLY);
99 if (fd < 0) { fprintf(stderr, "cannot open %s: %s\n", disk, strerror(errno)); return -1; }
100 uint8_t h[4096];
101 ssize_t r = pread(fd, h, sector, (off_t)sector);
102 close(fd);
103 if (r != (ssize_t)sector) { fprintf(stderr, "cannot read LBA 1 of %s\n", disk); return -1; }
104 if (memcmp(h, "EFI PART", 8)) { fprintf(stderr, "%s: no GPT header at LBA 1 (sector %u)\n", disk, sector); return -1; }
105 memcpy(out, h + 56, 16);
106 return 0;
107}
108
109static int parse_extent(const char *s, struct ck_model_extent *e)
110{
111 char *end = 0;
112 e->lba = strtoull(s, &end, 10);
113 if (!end || *end != ':') return -1;
114 e->count = strtoull(end + 1, &end, 10);
115 return (end && *end == 0 && e->count) ? 0 : -1;
116}
117
118/* --part-start auto: the file's device's first 512-byte sector from sysfs. */
119static int part_start_auto(const char *file, uint64_t *start512)
120{
121 struct stat st;
122 if (stat(file, &st)) return -1;
123 char p[128];
124 snprintf(p, sizeof p, "/sys/dev/block/%u:%u/start", major(st.st_dev), minor(st.st_dev));
125 FILE *f = fopen(p, "r");
126 if (!f) { fprintf(stderr, "cannot read %s (is the file on a partition?)\n", p); return -1; }
127 int ok = fscanf(f, "%llu", (unsigned long long *)start512) == 1;
128 fclose(f);
129 return ok ? 0 : -1;
130}
131
132static int fiemap_extents(const char *file, uint64_t len, uint32_t sector, uint64_t part_start_lba,
133 struct ck_model_map *m)
134{
135 int fd = open(file, O_RDONLY);
136 if (fd < 0) { fprintf(stderr, "cannot open %s\n", file); return -1; }
137 const unsigned cap = 512;
138 struct fiemap *fm = calloc(1, sizeof *fm + cap * sizeof(struct fiemap_extent));
139 if (!fm) { close(fd); return -1; }
140 fm->fm_start = 0;
141 fm->fm_length = ~0ull;
142 fm->fm_flags = FIEMAP_FLAG_SYNC;
143 fm->fm_extent_count = cap;
144 if (ioctl(fd, FS_IOC_FIEMAP, fm)) {
145 fprintf(stderr, "FS_IOC_FIEMAP: %s\n", strerror(errno));
146 free(fm);
147 close(fd);
148 return -1;
149 }
150 close(fd);
151 const uint32_t bad = FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_ENCODED |
152 FIEMAP_EXTENT_DATA_ENCRYPTED | FIEMAP_EXTENT_NOT_ALIGNED |
153 FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_DATA_TAIL | FIEMAP_EXTENT_UNWRITTEN;
154 uint64_t logical = 0;
155 int last = 0, rc = 0;
156 for (uint32_t i = 0; i < fm->fm_mapped_extents && !rc; i++) {
157 const struct fiemap_extent *e = &fm->fm_extents[i];
158 if (e->fe_flags & bad) { fprintf(stderr, "extent %u: flags 0x%x not plain mapped data\n", i, e->fe_flags); rc = -1; break; }
159 if (e->fe_logical != logical) { fprintf(stderr, "extent %u: hole or out of order at %llu\n", i, (unsigned long long)e->fe_logical); rc = -1; break; }
160 if (e->fe_physical % sector) { fprintf(stderr, "extent %u: physical offset not sector aligned\n", i); rc = -1; break; }
161 if (m->n_extents >= CK_MODEL_MAP_MAX_EXTENTS) { fprintf(stderr, "more than %u extents\n", CK_MODEL_MAP_MAX_EXTENTS); rc = -1; break; }
162 uint64_t take = e->fe_length;
163 if (logical + take > len) take = len - logical; /* the last extent may be block padded */
164 m->ext[m->n_extents].lba = part_start_lba + e->fe_physical / sector;
165 m->ext[m->n_extents].count = (take + sector - 1) / sector;
166 m->n_extents++;
167 logical += take;
168 if (e->fe_flags & FIEMAP_EXTENT_LAST) last = 1;
169 }
170 free(fm);
171 if (rc) return rc;
172 if (!last || logical != len) {
173 fprintf(stderr, "FIEMAP did not cover the file (%llu of %llu bytes, last=%d)\n",
174 (unsigned long long)logical, (unsigned long long)len, last);
175 return -1;
176 }
177 return 0;
178}
179
180static int cmd_make(int argc, char **argv)
181{
182 const char *out = argv[2], *file = 0, *disk = 0;
183 uint32_t sector = 512;
184 int fiemap = 0, corrupt = 0;
185 uint64_t part_start = 0;
186 int part_auto = 0;
187 static struct ck_model_map m;
188 memset(&m, 0, sizeof m);
189 for (int i = 3; i < argc; i++) {
190 if (!strcmp(argv[i], "--file") && i + 1 < argc) file = argv[++i];
191 else if (!strcmp(argv[i], "--disk") && i + 1 < argc) disk = argv[++i];
192 else if (!strcmp(argv[i], "--sector") && i + 1 < argc) sector = (uint32_t)strtoul(argv[++i], NULL, 10);
193 else if (!strcmp(argv[i], "--fiemap")) fiemap = 1;
194 else if (!strcmp(argv[i], "--part-start") && i + 1 < argc) {
195 if (!strcmp(argv[i + 1], "auto")) part_auto = 1;
196 else part_start = strtoull(argv[i + 1], NULL, 10);
197 i++;
198 } else if (!strcmp(argv[i], "--extent") && i + 1 < argc) {
199 if (m.n_extents >= CK_MODEL_MAP_MAX_EXTENTS || parse_extent(argv[++i], &m.ext[m.n_extents])) {
200 fprintf(stderr, "bad --extent %s\n", argv[i]);
201 return 2;
202 }
203 m.n_extents++;
204 } else if (!strcmp(argv[i], "--corrupt-sha")) corrupt = 1;
205 else return usage();
206 }
207 if (!file || !disk || (sector != 512 && sector != 4096) || (fiemap == (m.n_extents > 0))) return usage();
208 FILE *f = fopen(file, "rb");
209 if (!f) { fprintf(stderr, "cannot open %s\n", file); return 1; }
210 uint64_t len = 0;
211 if (sha_file(f, &len, m.sha256)) { fclose(f); fprintf(stderr, "read error on %s\n", file); return 1; }
212 fclose(f);
213 memcpy(m.magic, CK_MODEL_MAP_MAGIC, 8);
214 m.version = CK_MODEL_MAP_VERSION;
215 m.sector_size = sector;
216 m.model_len = len;
217 if (disk_guid(disk, sector, m.disk_guid)) return 1;
218 if (fiemap) {
219 if (part_auto) {
220 uint64_t s512 = 0;
221 if (part_start_auto(file, &s512)) return 1;
222 part_start = s512 * 512 / sector;
223 }
224 if (fiemap_extents(file, len, sector, part_start, &m)) return 1;
225 }
226 const char *why = 0;
227 if (ck_model_map_check(&m, sector, 0, &why)) { fprintf(stderr, "map refused: %s\n", why); return 1; }
228 if (corrupt) m.sha256[31] ^= 0xff;
229 FILE *o = fopen(out, "wb");
230 if (!o || fwrite(&m, 1, sizeof m, o) != sizeof m || fclose(o)) { fprintf(stderr, "cannot write %s\n", out); return 1; }
231 char sh[65], gh[33];
232 hex(sh, m.sha256, 32);
233 hex(gh, m.disk_guid, 16);
234 printf("model_map written=%s len=%llu extents=%u sector=%u disk=%s sha256=%s%s\n", out,
235 (unsigned long long)len, m.n_extents, sector, gh, sh, corrupt ? " (CORRUPTED on purpose)" : "");
236 return 0;
237}
238
239static int load_map(const char *path, struct ck_model_map *m)
240{
241 FILE *f = fopen(path, "rb");
242 if (!f) { fprintf(stderr, "cannot open %s\n", path); return -1; }
243 size_t n = fread(m, 1, sizeof *m, f);
244 int extra = fgetc(f) != EOF;
245 fclose(f);
246 if (n != sizeof *m || extra) { fprintf(stderr, "%s: not exactly %u bytes\n", path, CK_MODEL_MAP_BYTES); return -1; }
247 const char *why = 0;
248 if (ck_model_map_check(m, 0, 0, &why)) { fprintf(stderr, "%s: refused: %s\n", path, why); return -1; }
249 return 0;
250}
251
252static int cmd_show(int argc, char **argv)
253{
254 if (argc != 3) return usage();
255 static struct ck_model_map m;
256 if (load_map(argv[2], &m)) return 1;
257 char sh[65], gh[33];
258 hex(sh, m.sha256, 32);
259 hex(gh, m.disk_guid, 16);
260 printf("magic=%.8s version=%u sector=%u disk=%s len=%llu sha256=%s extents=%u\n", m.magic, m.version,
261 m.sector_size, gh, (unsigned long long)m.model_len, sh, m.n_extents);
262 for (uint32_t i = 0; i < m.n_extents; i++)
263 printf(" extent %u lba=%llu count=%llu\n", i, (unsigned long long)m.ext[i].lba,
264 (unsigned long long)m.ext[i].count);
265 return 0;
266}
267
268static int cmd_check(int argc, char **argv)
269{
270 if (argc != 5 || strcmp(argv[3], "--disk")) return usage();
271 static struct ck_model_map m;
272 if (load_map(argv[2], &m)) return 1;
273 uint8_t g[16];
274 if (disk_guid(argv[4], m.sector_size, g)) return 1;
275 if (memcmp(g, m.disk_guid, 16)) { fprintf(stderr, "disk GUID differs from the map\n"); return 1; }
276 int fd = open(argv[4], O_RDONLY);
277 if (fd < 0) { fprintf(stderr, "cannot open %s\n", argv[4]); return 1; }
278 static uint8_t buf[1u << 20];
279 sha256_ctx c;
280 sha256_init(&c);
281 uint64_t left = m.model_len;
282 for (uint32_t i = 0; i < m.n_extents && left; i++) {
283 uint64_t off = m.ext[i].lba * m.sector_size, bytes = m.ext[i].count * m.sector_size;
284 if (bytes > left) bytes = left;
285 while (bytes) {
286 size_t want = bytes < sizeof buf ? (size_t)bytes : sizeof buf;
287 if (pread(fd, buf, want, (off_t)off) != (ssize_t)want) { close(fd); fprintf(stderr, "read failed at byte %llu\n", (unsigned long long)off); return 1; }
288 sha256_update(&c, buf, want);
289 off += want;
290 bytes -= want;
291 left -= want;
292 }
293 }
294 close(fd);
295 uint8_t dg[32];
296 sha256_final(&c, dg);
297 char sh[65];
298 hex(sh, dg, 32);
299 if (left || memcmp(dg, m.sha256, 32)) {
300 printf("model_map check: FAIL (sha256 of the mapped bytes %s, declared differs, unread=%llu)\n", sh,
301 (unsigned long long)left);
302 return 1;
303 }
304 printf("model_map check: PASS (sha256 of the mapped bytes = declared %s, %u extents, %llu bytes)\n", sh,
305 m.n_extents, (unsigned long long)m.model_len);
306 return 0;
307}
308
309int main(int argc, char **argv)
310{
311 if (argc < 3) return usage();
312 if (!strcmp(argv[1], "make")) return cmd_make(argc, argv);
313 if (!strcmp(argv[1], "show")) return cmd_show(argc, argv);
314 if (!strcmp(argv[1], "check")) return cmd_check(argc, argv);
315 return usage();
316}