Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/dev/tests/pi5_store_test.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / dev / tests / pi5_store_test.c` 231 lines ยท 9770 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 /* pi5_store_test.c -- AIENOS-PI5 step 3b host test: the sealed Store stage 18 * (svc/store_boot.c, the same one the QEMU full image runs) over the Pi 5 19 * card layout (protective MBR, ESP, AIENOS partition, two-copy GPT), in the 20 * TEST-key qualification tier (SEED-0B-PI5). A file stands in for the card; 21 * the SD controller itself is proved on the hub Pi (pi5-run.sh). 22 * 23 * Checks: blank card formats and commits boot 1; after a "reboot" (file 24 * closed, GPT parsed again, nothing kept in memory) the Store reopens and 25 * commits boot 2 and 3 with the generation advancing; every byte outside the 26 * AIENOS partition (MBR, GPT copies, ESP) is unchanged; the production key 27 * source still refuses and hands out no key bytes; a hardware-staging policy 28 * refuses the TEST keys before touching the card. The Store runs on the 29 * partition view minus the marker region (core/pi5_marker.h 30 * ck_pi5_store_view): its units and probe unit never write a marker sector, 31 * stale non-zero data in the reserved tail does not stop a format, and the 32 * marker and the Store each reopen after the other has written. Hosted test 33 * code only. */ 34 #include 35 #include 36 #include 37 #include 38 #include "ck.h" 39 #include "ck_host.h" 40 #include "disk_file.h" 41 #include "disk_layout.h" 42 #include "disk_part.h" 43 #include "gpt_write.h" 44 #include "../../core/pi5_marker.h" 45 #include "store_boot.h" 46 47 static int checks, failures; 48 #define CHECK(c) \ 49 do { \ 50 checks++; \ 51 if (!(c)) { \ 52 failures++; \ 53 printf(" FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); \ 54 } \ 55 } while (0) 56 57 #define BLOCKS 262144ull /* 128 MiB stand-in for the card */ 58 static char g_path[256]; 59 60 static uint8_t *slurp(size_t *n) 61 { 62 FILE *fp = fopen(g_path, "rb"); 63 CHECK(fp != 0); 64 if (!fp) { *n = 0; return 0; } 65 fseek(fp, 0, SEEK_END); 66 *n = (size_t)ftell(fp); 67 rewind(fp); 68 uint8_t *m = malloc(*n); 69 CHECK(m && fread(m, 1, *n, fp) == *n); 70 fclose(fp); 71 return m; 72 } 73 74 /* One "boot": open the card file, parse the GPT, run the Store stage body. */ 75 static int boot(int hardware_staging, const ss_keys *k, ck_store_report *r) 76 { 77 disk_file f; 78 disk_dev d; 79 ck_part p; 80 ck_gpt_info gi; 81 if (disk_file_open(&f, &d, g_path, 512, 0, 0)) return -9999; 82 ck_part sp; 83 int rc = ck_gpt_find_aienos(&d, &p, &gi); 84 if (rc || ck_pi5_store_view(&p, &sp)) { disk_file_close(&f); return -9998; } 85 ck_host_quiet = 1; 86 rc = store_boot_run_policy(&sp.dev, k, ck_store_test_uuid, "pi5-store-test", hardware_staging, r); 87 ck_host_quiet = 0; 88 disk_file_close(&f); 89 return rc; 90 } 91 92 /* One marker "boot" on the full partition view (what core/pi5.c runs in every image). */ 93 static int marker_boot(uint64_t *gen) 94 { 95 disk_file f; 96 disk_dev d; 97 ck_part p; 98 ck_gpt_info gi; 99 static uint8_t mbuf[CK_PI5_MARKER_BUF_BYTES] __attribute__((aligned(64))); 100 if (disk_file_open(&f, &d, g_path, 512, 0, 0)) return -9999; 101 int rc = ck_gpt_find_aienos(&d, &p, &gi); 102 if (!rc) rc = ck_pi5_marker_run(&p.dev, mbuf, gen); 103 disk_file_close(&f); 104 return rc; 105 } 106 107 #define PART_FIRST 8192u 108 #define PART_SECS 8192u 109 #define MARKER_AT (PART_FIRST + PART_SECS - CK_PI5_MARKER_SECS) /* absolute sector of the marker header */ 110 111 int main(void) 112 { 113 snprintf(g_path, sizeof g_path, "/tmp/aienos-pi5-store-test-%d.img", (int)getpid()); 114 gw_part gp[2] = {{gw_esp_type, 2048, 4095, "AIENOSPI5-ESP", 0x5a}, {ck_aienos_part_type, 8192, 8192 + 8192 - 1, "AIENOS", 0}}; 115 CHECK(gw_write(g_path, 512, BLOCKS, gp, 2, 0) == 0); 116 size_t n0 = 0, n1 = 0; 117 /* Stale non-zero bytes in the reserved tail (an old marker, or junk) before the first format. */ 118 { 119 FILE *fp = fopen(g_path, "r+b"); 120 CHECK(fp != 0); 121 if (fp) { 122 uint8_t junk[CK_PI5_MARKER_SECS * 512u]; 123 memset(junk, 0xA5, sizeof junk); 124 fseek(fp, (long)MARKER_AT * 512L, SEEK_SET); 125 CHECK(fwrite(junk, 1, sizeof junk, fp) == sizeof junk); 126 fclose(fp); 127 } 128 } 129 uint8_t *before = slurp(&n0); 130 131 ss_keys k; 132 ck_store_report r; 133 ck_store_test_keys(&k); 134 CHECK(k.identity_class == M5_ID_TEST); /* the tier is TEST: labelled, public, protects nothing */ 135 136 int rc = boot(0, &k, &r); 137 CHECK(rc == 0 && r.verdict == CK_SB_COMMITTED && r.formatted == 1 && r.boot_count_new == 1); 138 uint64_t g1 = r.gen_commit; 139 rc = boot(0, &k, &r); 140 CHECK(rc == 0 && r.formatted == 0 && r.boot_count_prev == 1 && r.boot_count_new == 2 && r.gen_commit > g1); 141 uint64_t g2 = r.gen_commit; 142 rc = boot(0, &k, &r); 143 CHECK(rc == 0 && r.formatted == 0 && r.boot_count_new == 3 && r.gen_commit > g2); 144 CHECK(strcmp(r.prev_commit, "pi5-store-test") == 0); 145 /* AIENOS partition is 8192 sectors = 1024 units; the Store view drops the 64-sector (8-unit) marker 146 * region: 1016 units, anchor 4, probe 1. */ 147 CHECK(r.store_units == (8192u - CK_PI5_MARKER_SECS) / 8u - CK_LAYOUT_ANCHOR_UNITS - CK_LAYOUT_PROBE_UNITS); 148 149 uint8_t *after = slurp(&n1); 150 CHECK(before && after && n0 == n1); 151 if (before && after && n0 == n1) { 152 CHECK(memcmp(before, after, 8192u * 512u) == 0); /* MBR, GPT, ESP */ 153 CHECK(memcmp(before + 16384u * 512u, after + 16384u * 512u, n0 - 16384u * 512u) == 0); /* after the partition, backup GPT */ 154 CHECK(memcmp(before + 8192u * 512u, after + 8192u * 512u, 8192u * 512u) != 0); /* the Store wrote inside */ 155 CHECK(memcmp(before + (size_t)MARKER_AT * 512u, after + (size_t)MARKER_AT * 512u, 156 CK_PI5_MARKER_SECS * 512u) == 0); /* three Store boots, probe included: reserved tail untouched */ 157 } 158 free(before); 159 free(after); 160 161 /* The stale junk is neither blank nor a marker: the marker refuses and writes nothing. Clear it as a 162 * fresh card would be, then marker and Store alternate: each reopens after the other wrote. */ 163 uint64_t mg = 0; 164 CHECK(marker_boot(&mg) == CK_PI5_MARKER_EREFUSED); 165 { 166 FILE *fp = fopen(g_path, "r+b"); 167 CHECK(fp != 0); 168 if (fp) { 169 uint8_t z[CK_PI5_MARKER_SECS * 512u]; 170 memset(z, 0, sizeof z); 171 fseek(fp, (long)MARKER_AT * 512L, SEEK_SET); 172 CHECK(fwrite(z, 1, sizeof z, fp) == sizeof z); 173 fclose(fp); 174 } 175 } 176 CHECK(marker_boot(&mg) == CK_PI5_MARKER_FORMATTED && mg == 1); 177 rc = boot(0, &k, &r); 178 CHECK(rc == 0 && r.formatted == 0 && r.boot_count_new == 4); 179 CHECK(marker_boot(&mg) == CK_PI5_MARKER_REOPENED && mg == 2); 180 rc = boot(0, &k, &r); 181 CHECK(rc == 0 && r.formatted == 0 && r.boot_count_new == 5); 182 CHECK(marker_boot(&mg) == CK_PI5_MARKER_REOPENED && mg == 3); 183 184 /* Geometry: the Store view ends at or before the marker's first sector, in whole units. */ 185 { 186 disk_file f; 187 disk_dev d; 188 ck_part p, sp; 189 ck_gpt_info gi; 190 CHECK(disk_file_open(&f, &d, g_path, 512, 0, 0) == 0); 191 CHECK(ck_gpt_find_aienos(&d, &p, &gi) == 0); 192 CHECK(ck_pi5_store_view(&p, &sp) == 0); 193 CHECK(sp.blocks % 8u == 0 && sp.blocks <= p.blocks - CK_PI5_MARKER_SECS && sp.dev.block_count == sp.blocks); 194 CHECK(sp.dev.ctx == &sp && sp.first_lba == p.first_lba); 195 uint8_t one[512]; 196 CHECK(disk_read(&sp.dev, sp.blocks, 1, one) != DISK_OK); /* the first marker sector is outside the view */ 197 /* An odd-sized partition still leaves the whole marker outside the Store's last unit. */ 198 ck_part odd = p, so; 199 odd.blocks = p.blocks - 3u; 200 odd.dev.ctx = &odd; 201 odd.dev.block_count = odd.blocks; 202 CHECK(ck_pi5_store_view(&odd, &so) == 0 && so.blocks % 8u == 0 && so.blocks <= odd.blocks - CK_PI5_MARKER_SECS); 203 ck_part tiny = p, st; 204 tiny.blocks = CK_PI5_MARKER_SECS + 7u; 205 CHECK(ck_pi5_store_view(&tiny, &st) == CK_PI5_MARKER_ESIZE); 206 disk_file_close(&f); 207 } 208 209 /* Production key source: still BLOCKED_OPERATOR, no key bytes handed out, no fallback. */ 210 ss_keys pk; 211 memset(&pk, 0xAA, sizeof pk); 212 CHECK(ck_store_production_keys(&pk) == CK_SB_E_BLOCKED_OPERATOR); 213 int nz = 0; 214 for (size_t i = 0; i < sizeof pk; i++) nz |= ((uint8_t *)&pk)[i]; 215 CHECK(nz == 0); 216 217 /* A hardware-staging policy refuses the TEST keys before any disk access. */ 218 uint8_t *b2 = slurp(&n0); 219 rc = boot(1, &k, &r); 220 CHECK(rc == CK_SB_E_TEST_KEYS); 221 uint8_t *a2 = slurp(&n1); 222 CHECK(b2 && a2 && n0 == n1 && memcmp(b2, a2, n0) == 0); 223 free(b2); 224 free(a2); 225 226 memset(&k, 0, sizeof k); 227 unlink(g_path); 228 printf("pi5_store_test: %d checks, %d failures\n", checks, failures); 229 printf("CK_PI5_STORE_HOST: %s\n", failures ? "FAIL" : "PASS"); 230 return failures ? 1 : 0; 231 }