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 / kernel / tools / ck_store_image.c
| 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_store_image -- host tool: put signed P2 artifacts into the sealed C Store |
| 18 | * inside the AIENOS GPT partition of a boot disk image (made by |
| 19 | * tools/ck_gpt_image.c), at image build time, so the C kernel's loader |
| 20 | * reads them from NVMe (svc/artifact_store.h). It reuses the kernel's own |
| 21 | * Store code (store_boot.c formats and commits the boot record exactly as a |
| 22 | * first boot would; artifact_store.c writes and reads the artifact objects), |
| 23 | * so there is no second disk format. TEST identity and TEST keys only. |
| 24 | * |
| 25 | * ck_store_image build IMG BLOCK_SIZE ENTRY... IMG must hold a GPT whose AIENOS partition is blank |
| 26 | * ENTRY = path to a file (Store name = its basename, 1..32 bytes) |
| 27 | * | missing:NAME:LEN (index entry, no chunks: hostile case) |
| 28 | * ck_store_image list IMG BLOCK_SIZE read back with the kernel reader |
| 29 | * ck_store_image locate IMG BLOCK_SIZE NAME "offset=B length=L" (B: byte offset in the |
| 30 | * whole image file) of the sealed envelope holding chunk 0 of NAME (for the corrupt-disk case) |
| 31 | * list and locate only read the image. Hosted C; test tooling, never in an |
| 32 | * image. No Python, no outside libraries. */ |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include "artifact_store.h" |
| 37 | #include "disk_file.h" |
| 38 | #include "disk_layout.h" |
| 39 | #include "disk_part.h" |
| 40 | #include "store_boot.h" |
| 41 | |
| 42 | static int usage(void) |
| 43 | { |
| 44 | fprintf(stderr, "usage: ck_store_image build IMG BS ENTRY... | list IMG BS | locate IMG BS NAME\n"); |
| 45 | return 2; |
| 46 | } |
| 47 | |
| 48 | static uint8_t *slurp(const char *path, size_t *len) |
| 49 | { |
| 50 | FILE *f = fopen(path, "rb"); |
| 51 | if (!f) return NULL; |
| 52 | size_t cap = CK_ART_MAX_BYTES + 1, n = 0; |
| 53 | uint8_t *b = malloc(cap); |
| 54 | if (b) n = fread(b, 1, cap, f); |
| 55 | fclose(f); |
| 56 | if (!b || n == 0 || n > CK_ART_MAX_BYTES) { |
| 57 | free(b); |
| 58 | return NULL; |
| 59 | } |
| 60 | *len = n; |
| 61 | return b; |
| 62 | } |
| 63 | |
| 64 | typedef struct { |
| 65 | disk_file f; |
| 66 | disk_dev raw; /* the whole image: read for the GPT only */ |
| 67 | ck_part part; /* the AIENOS partition view (dev/disk_part.h) */ |
| 68 | const disk_dev *d; /* == &part.dev: every Store access goes through ck_part_xlate */ |
| 69 | st_disk sd; |
| 70 | st_dev sdev; |
| 71 | ts_device tdev; |
| 72 | uint64_t base_lba, units; |
| 73 | } image; |
| 74 | |
| 75 | static int img_open(image *im, const char *path, const char *bs_s) |
| 76 | { |
| 77 | uint32_t bs = (uint32_t)strtoul(bs_s, NULL, 10); |
| 78 | if (bs != 512 && bs != 4096) { |
| 79 | fprintf(stderr, "block size must be 512 or 4096\n"); |
| 80 | return -1; |
| 81 | } |
| 82 | memset(im, 0, sizeof *im); |
| 83 | if (disk_file_open(&im->f, &im->raw, path, bs, 0, 0)) { |
| 84 | fprintf(stderr, "cannot open %s\n", path); |
| 85 | return -1; |
| 86 | } |
| 87 | /* Same rule as the kernel: the Store lives only inside the AIENOS GPT |
| 88 | * partition; no valid GPT or no AIENOS partition is refused. Images come |
| 89 | * from tools/ck_gpt_image.c. */ |
| 90 | ck_gpt_info gi; |
| 91 | int grc = ck_gpt_find_aienos(&im->raw, &im->part, &gi); |
| 92 | if (grc) { |
| 93 | fprintf(stderr, "%s: no AIENOS partition, refusing writes (gpt: %s, rc=%d)\n", path, ck_gpt_strerror(grc), grc); |
| 94 | disk_file_close(&im->f); |
| 95 | return -1; |
| 96 | } |
| 97 | im->d = &im->part.dev; |
| 98 | uint32_t bpu = CK_LAYOUT_UNIT / bs; |
| 99 | uint64_t units = im->d->block_count / bpu; |
| 100 | if (units < CK_LAYOUT_ANCHOR_UNITS + CK_LAYOUT_MIN_STORE_UNITS + CK_LAYOUT_PROBE_UNITS) { |
| 101 | fprintf(stderr, "image too small\n"); |
| 102 | disk_file_close(&im->f); |
| 103 | return -1; |
| 104 | } |
| 105 | im->base_lba = (uint64_t)CK_LAYOUT_ANCHOR_UNITS * bpu; |
| 106 | im->units = units - CK_LAYOUT_ANCHOR_UNITS - CK_LAYOUT_PROBE_UNITS; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | /* Read-only keyed mount, the same proofs as the kernel's store stage. */ |
| 111 | static int img_mount(image *im, ss_store *s, ss_workspace *ws) |
| 112 | { |
| 113 | ss_keys keys; |
| 114 | ck_store_test_keys(&keys); |
| 115 | int rc = st_disk_bind(&im->sd, im->d, im->base_lba, im->units, &im->sdev); |
| 116 | if (!rc) { |
| 117 | ss_ts_device(im->d, &im->tdev); |
| 118 | memset(s, 0, sizeof *s); |
| 119 | rc = ss_open(s, &im->sdev, &im->tdev, 0, &keys, ws); |
| 120 | } |
| 121 | memset(&keys, 0, sizeof keys); |
| 122 | if (rc) fprintf(stderr, "Store refused: rc=%d\n", rc); |
| 123 | return rc; |
| 124 | } |
| 125 | |
| 126 | static void hex(const uint8_t *b, size_t n) |
| 127 | { |
| 128 | for (size_t i = 0; i < n; i++) printf("%02x", b[i]); |
| 129 | } |
| 130 | |
| 131 | static int cmd_build(int argc, char **argv) |
| 132 | { |
| 133 | if (argc < 5) return usage(); |
| 134 | size_t n = (size_t)argc - 4; |
| 135 | if (n > CK_ART_MAX_ENTRIES) { |
| 136 | fprintf(stderr, "at most %u artifacts\n", CK_ART_MAX_ENTRIES); |
| 137 | return 1; |
| 138 | } |
| 139 | ck_art_input *in = calloc(n, sizeof *in); |
| 140 | if (!in) return 1; |
| 141 | for (size_t i = 0; i < n; i++) { |
| 142 | const char *a = argv[4 + i]; |
| 143 | if (!strncmp(a, "missing:", 8)) { |
| 144 | char *name = strdup(a + 8), *c = name ? strrchr(name, ':') : NULL; |
| 145 | if (!c) return usage(); |
| 146 | *c = 0; |
| 147 | in[i].name = name; |
| 148 | in[i].len = strtoul(c + 1, NULL, 10); |
| 149 | in[i].missing = 1; |
| 150 | } else { |
| 151 | const char *base = strrchr(a, '/'); |
| 152 | in[i].name = base ? base + 1 : a; |
| 153 | in[i].bytes = slurp(a, &in[i].len); |
| 154 | if (!in[i].bytes) { |
| 155 | fprintf(stderr, "%s: unreadable, empty or over %u bytes\n", a, CK_ART_MAX_BYTES); |
| 156 | return 1; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | image im; |
| 161 | if (img_open(&im, argv[2], argv[3])) return 1; |
| 162 | ss_keys keys; |
| 163 | ck_store_report r; |
| 164 | ck_store_test_keys(&keys); |
| 165 | int rc = store_boot_run(im.d, &keys, ck_store_test_uuid, "ck_store_image", &r); |
| 166 | memset(&keys, 0, sizeof keys); |
| 167 | if (rc || !r.formatted) { |
| 168 | fprintf(stderr, "%s: %s (the image must be blank: all zero)\n", argv[2], |
| 169 | rc ? "Store format/commit failed" : "not blank"); |
| 170 | disk_file_close(&im.f); |
| 171 | return 1; |
| 172 | } |
| 173 | rc = ck_art_write(store_boot_store(), in, n); |
| 174 | if (!rc) rc = disk_flush(im.d); |
| 175 | uint64_t gen = ss_generation(store_boot_store()); |
| 176 | disk_file_close(&im.f); |
| 177 | if (rc) { |
| 178 | fprintf(stderr, "artifact write failed: rc=%d\n", rc); |
| 179 | return 1; |
| 180 | } |
| 181 | printf("ck_store_image: %s artifacts=%zu store_generation=%llu (TEST identity, TEST keys)\n", argv[2], n, |
| 182 | (unsigned long long)gen); |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static int cmd_list(int argc, char **argv) |
| 187 | { |
| 188 | if (argc != 4) return usage(); |
| 189 | image im; |
| 190 | if (img_open(&im, argv[2], argv[3])) return 1; |
| 191 | ss_workspace *ws = calloc(1, sizeof *ws); |
| 192 | ss_store *s = calloc(1, sizeof *s); |
| 193 | ck_art_set *set = calloc(1, sizeof *set); |
| 194 | int rc = !ws || !s || !set ? -1 : img_mount(&im, s, ws); |
| 195 | const char *why = ""; |
| 196 | if (!rc) rc = ck_art_collect(s, set, &why); |
| 197 | if (!rc && !set->found) printf("no artifact index\n"); |
| 198 | if (!rc && set->found) { |
| 199 | printf("index generation=%llu entries=%u sha256=", (unsigned long long)set->index_generation, set->count); |
| 200 | hex(set->index_sha256, 32); |
| 201 | printf("\n"); |
| 202 | for (uint32_t i = 0; i < set->count; i++) { |
| 203 | const ck_art_entry *a = &set->e[i]; |
| 204 | printf("%s bytes=%u ", a->name, a->len); |
| 205 | if (a->state == CK_ART_OK) { |
| 206 | printf("sha256="); |
| 207 | hex(a->sha256, 32); |
| 208 | printf("\n"); |
| 209 | } else { |
| 210 | printf("%s\n", a->state == CK_ART_TOO_LARGE ? "too-large" : a->state == CK_ART_MISMATCH ? "mismatch" : "missing"); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | if (rc) fprintf(stderr, "list failed: rc=%d %s\n", rc, why); |
| 215 | if (set) ck_art_set_free(set); |
| 216 | free(set); |
| 217 | free(s); |
| 218 | free(ws); |
| 219 | disk_file_close(&im.f); |
| 220 | return rc ? 1 : 0; |
| 221 | } |
| 222 | |
| 223 | static int cmd_locate(int argc, char **argv) |
| 224 | { |
| 225 | if (argc != 5) return usage(); |
| 226 | image im; |
| 227 | if (img_open(&im, argv[2], argv[3])) return 1; |
| 228 | ss_workspace *ws = calloc(1, sizeof *ws); |
| 229 | ss_store *s = calloc(1, sizeof *s); |
| 230 | uint8_t *buf = malloc(SS_MAX_PLAINTEXT); |
| 231 | int rc = !ws || !s || !buf ? -1 : img_mount(&im, s, ws), found = 0; |
| 232 | size_t nl = strlen(argv[4]); |
| 233 | if (nl == 0 || nl > CK_ART_NAME_MAX) { |
| 234 | fprintf(stderr, "name must be 1..%u bytes\n", CK_ART_NAME_MAX); |
| 235 | free(buf); |
| 236 | free(s); |
| 237 | free(ws); |
| 238 | disk_file_close(&im.f); |
| 239 | return 1; |
| 240 | } |
| 241 | for (uint32_t k = 0; !rc && !found && k < s->nclaims; k++) { |
| 242 | if (ws->claims[k].c.obj.object_kind != CK_ART_CHUNK_KIND) continue; |
| 243 | uint8_t sid[32]; |
| 244 | size_t len = 0; |
| 245 | uint16_t kind = 0; |
| 246 | memcpy(sid, ws->claims[k].sid, 32); |
| 247 | if (ss_read(s, sid, buf, SS_MAX_PLAINTEXT, &len, &kind) || len < CK_ART_CHUNK_HDR) continue; |
| 248 | if (buf[16] || buf[17] || buf[20] != nl || memcmp(buf + 24, argv[4], nl)) continue; /* chunk 0 of NAME */ |
| 249 | const st_store *st = &s->st; |
| 250 | for (uint32_t i = 0; i < st->root.n; i++) { |
| 251 | const sv1_entry *e = &st->ws->cat[st->root.cat_index][i]; |
| 252 | if (memcmp(e->object_id, sid, 32)) continue; |
| 253 | printf("offset=%llu length=%llu\n", |
| 254 | (unsigned long long)((im.part.first_lba + im.base_lba) * im.d->block_size + e->first_unit * CK_LAYOUT_UNIT), |
| 255 | (unsigned long long)e->byte_length); |
| 256 | found = 1; |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | if (!rc && !found) fprintf(stderr, "%s: no chunk 0 on disk\n", argv[4]); |
| 261 | free(buf); |
| 262 | free(s); |
| 263 | free(ws); |
| 264 | disk_file_close(&im.f); |
| 265 | return !rc && found ? 0 : 1; |
| 266 | } |
| 267 | |
| 268 | int main(int argc, char **argv) |
| 269 | { |
| 270 | if (argc < 2) return usage(); |
| 271 | if (!strcmp(argv[1], "build")) return cmd_build(argc, argv); |
| 272 | if (!strcmp(argv[1], "list")) return cmd_list(argc, argv); |
| 273 | if (!strcmp(argv[1], "locate")) return cmd_locate(argc, argv); |
| 274 | return usage(); |
| 275 | } |