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 / tests / test_pi5_marker.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 | /* test_pi5_marker.c -- AIENOS-PI5 step 3: the Pi 5 card layout (ESP + AIENOS |
| 18 | * partition under a protective MBR and a two-copy GPT) parsed by the kernel's |
| 19 | * own dev/disk_part.c, and the scratch marker (core/pi5_marker.c) written |
| 20 | * through the partition view with flush, "rebooted" (file closed and reopened, |
| 21 | * nothing kept in memory) and verified. Hosted: a file stands in for the |
| 22 | * card. This is NOT the SD controller, which QEMU cannot model; the controller |
| 23 | * is proved on the hub Pi (pi5-run.sh) and its write path by native/disk/tests/sdhci_test.c. */ |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <unistd.h> |
| 28 | #include "ck_test.h" |
| 29 | #include "disk_file.h" |
| 30 | #include "disk_part.h" |
| 31 | #include "gpt_write.h" |
| 32 | #include "pi5_marker.h" |
| 33 | |
| 34 | #define BLOCKS 262144ull /* 128 MiB stand-in for the 119 GiB card */ |
| 35 | |
| 36 | static const uint8_t esp_type_ok = 1; |
| 37 | static const char *img = "/tmp/ck_pi5_marker_test.img"; |
| 38 | static uint8_t buf[CK_PI5_MARKER_BUF_BYTES] __attribute__((aligned(64))); |
| 39 | |
| 40 | static void make_card(const uint8_t *aienos_type, unsigned flags) |
| 41 | { |
| 42 | gw_part p[2] = {{gw_esp_type, 2048, 4095, "AIENOSPI5-ESP", 0}, {aienos_type, 8192, 16383, "AIENOS", 0}}; |
| 43 | (void)esp_type_ok; |
| 44 | CHECK(gw_write(img, 512, BLOCKS, p, 2, flags) == 0); |
| 45 | } |
| 46 | |
| 47 | static int open_view(disk_file *f, disk_dev *d, ck_part *part, ck_gpt_info *gi) |
| 48 | { |
| 49 | if (disk_file_open(f, d, img, 512, BLOCKS, 0)) return -1; |
| 50 | return ck_gpt_find_aienos(d, part, gi); |
| 51 | } |
| 52 | |
| 53 | static int run(uint64_t *gen) |
| 54 | { |
| 55 | disk_file f; |
| 56 | disk_dev d; |
| 57 | ck_part part; |
| 58 | ck_gpt_info gi; |
| 59 | int rc = open_view(&f, &d, &part, &gi); |
| 60 | CHECK(rc == 0); |
| 61 | if (rc) { disk_file_close(&f); return -999; } |
| 62 | rc = ck_pi5_marker_run(&part.dev, buf, gen); |
| 63 | disk_file_close(&f); |
| 64 | return rc; |
| 65 | } |
| 66 | |
| 67 | static uint8_t *slurp(size_t *n) |
| 68 | { |
| 69 | FILE *fp = fopen(img, "rb"); |
| 70 | fseek(fp, 0, SEEK_END); |
| 71 | *n = (size_t)ftell(fp); |
| 72 | rewind(fp); |
| 73 | uint8_t *m = malloc(*n); |
| 74 | CHECK(fread(m, 1, *n, fp) == *n); |
| 75 | fclose(fp); |
| 76 | return m; |
| 77 | } |
| 78 | |
| 79 | int main(void) |
| 80 | { |
| 81 | const uint64_t first = 8192, last = 16383, region = last + 1 - CK_PI5_MARKER_SECS; /* absolute LBA of the marker */ |
| 82 | uint64_t gen = 0; |
| 83 | size_t n1, n2; |
| 84 | |
| 85 | /* --- GPT as the Pi card has it --- */ |
| 86 | make_card(ck_aienos_part_type, 0); |
| 87 | { |
| 88 | disk_file f; disk_dev d; ck_part part; ck_gpt_info gi; |
| 89 | CHECK(open_view(&f, &d, &part, &gi) == 0); |
| 90 | CHECK(part.valid && part.first_lba == first && part.blocks == last - first + 1 && gi.used == 2); |
| 91 | CHECK(part.dev.block_count == last - first + 1); |
| 92 | uint64_t abs = 0; |
| 93 | CHECK(ck_part_xlate(&part, 0, 1, &abs) == 0 && abs == first); |
| 94 | CHECK(ck_part_xlate(&part, part.blocks - 1, 1, &abs) == 0 && abs == last); |
| 95 | CHECK(ck_part_xlate(&part, part.blocks, 1, &abs) == DISK_ERANGE); /* one past the partition */ |
| 96 | CHECK(disk_write(&part.dev, part.blocks, 1, buf) == DISK_ERANGE); |
| 97 | disk_file_close(&f); |
| 98 | } |
| 99 | /* hostile cards: nothing is presented and the marker refuses */ |
| 100 | { |
| 101 | static const uint8_t other[16] = {1, 2, 3}; |
| 102 | const struct { const uint8_t *t; unsigned fl; } bad[] = { |
| 103 | {other, 0}, {ck_aienos_part_type, GW_NO_PMBR}, {ck_aienos_part_type, GW_BAD_PRIMARY_CRC}, |
| 104 | {ck_aienos_part_type, GW_BAD_BACKUP_CRC}, {ck_aienos_part_type, GW_BAD_PRIMARY_ENT}, |
| 105 | {ck_aienos_part_type, GW_BAD_BACKUP_ENT}, {ck_aienos_part_type, GW_NO_GPT}, |
| 106 | }; |
| 107 | for (unsigned i = 0; i < sizeof bad / sizeof bad[0]; i++) { |
| 108 | make_card(bad[i].t, bad[i].fl); |
| 109 | disk_file f; disk_dev d; ck_part part; ck_gpt_info gi; |
| 110 | CHECK(open_view(&f, &d, &part, &gi) != 0); |
| 111 | CHECK(!part.valid); |
| 112 | CHECK(ck_pi5_marker_run(&part.dev, buf, &gen) == CK_PI5_MARKER_ESIZE); |
| 113 | disk_file_close(&f); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /* --- first boot: blank region is formatted --- */ |
| 118 | make_card(ck_aienos_part_type, 0); |
| 119 | uint8_t *before = slurp(&n1); |
| 120 | CHECK(run(&gen) == CK_PI5_MARKER_FORMATTED && gen == 1); |
| 121 | uint8_t *after = slurp(&n2); |
| 122 | CHECK(n1 == n2); |
| 123 | size_t changed_outside = 0, changed_inside = 0; |
| 124 | for (size_t i = 0; i < n1; i++) |
| 125 | if (before[i] != after[i]) { |
| 126 | if (i / 512 >= region && i / 512 <= last) changed_inside++; else changed_outside++; |
| 127 | } |
| 128 | CHECK(changed_inside > 0 && changed_outside == 0); /* every written byte is inside the marker region */ |
| 129 | /* --- reboot: reopen, same contents, generation moves --- */ |
| 130 | CHECK(run(&gen) == CK_PI5_MARKER_REOPENED && gen == 2); |
| 131 | uint8_t *again = slurp(&n2); |
| 132 | size_t diff_sectors = 0; |
| 133 | for (size_t s = 0; s < n1 / 512; s++) |
| 134 | if (memcmp(after + s * 512, again + s * 512, 512)) diff_sectors++; |
| 135 | CHECK(diff_sectors == 1); /* only the header sector; the 63 payload sectors are identical */ |
| 136 | CHECK(run(&gen) == CK_PI5_MARKER_REOPENED && gen == 3); |
| 137 | free(again); |
| 138 | |
| 139 | /* --- a flipped payload byte is caught and nothing is written --- */ |
| 140 | { |
| 141 | FILE *fp = fopen(img, "r+b"); |
| 142 | fseek(fp, (long)((region + 7) * 512 + 100), SEEK_SET); |
| 143 | int c = fgetc(fp); |
| 144 | fseek(fp, -1, SEEK_CUR); |
| 145 | fputc(c ^ 0x01, fp); |
| 146 | fclose(fp); |
| 147 | uint8_t *pre = slurp(&n1); |
| 148 | CHECK(run(&gen) == CK_PI5_MARKER_ECORRUPT); |
| 149 | uint8_t *post = slurp(&n2); |
| 150 | CHECK(n1 == n2 && memcmp(pre, post, n1) == 0); |
| 151 | free(pre); free(post); |
| 152 | } |
| 153 | |
| 154 | /* --- foreign data in the region: refused, untouched --- */ |
| 155 | make_card(ck_aienos_part_type, 0); |
| 156 | { |
| 157 | FILE *fp = fopen(img, "r+b"); |
| 158 | fseek(fp, (long)((region + 3) * 512), SEEK_SET); |
| 159 | fputs("someone else's data", fp); |
| 160 | fclose(fp); |
| 161 | uint8_t *pre = slurp(&n1); |
| 162 | CHECK(run(&gen) == CK_PI5_MARKER_EREFUSED); |
| 163 | uint8_t *post = slurp(&n2); |
| 164 | CHECK(memcmp(pre, post, n1) == 0); |
| 165 | free(pre); free(post); |
| 166 | } |
| 167 | |
| 168 | /* --- power cut during the first format: header is written last, so a torn run is refused, never "reopened" --- */ |
| 169 | make_card(ck_aienos_part_type, 0); |
| 170 | { |
| 171 | disk_file f; disk_dev d; ck_part part; ck_gpt_info gi; |
| 172 | CHECK(open_view(&f, &d, &part, &gi) == 0); |
| 173 | disk_file_arm_cut(&f, 20); /* 20 of the 63 payload sectors reach the card */ |
| 174 | CHECK(ck_pi5_marker_run(&part.dev, buf, &gen) < 0); |
| 175 | disk_file_close(&f); |
| 176 | int rc = run(&gen); |
| 177 | CHECK(rc == CK_PI5_MARKER_EREFUSED || rc == CK_PI5_MARKER_ECORRUPT); |
| 178 | } |
| 179 | free(before); free(after); |
| 180 | unlink(img); |
| 181 | return ck_t_verdict("test_pi5_marker"); |
| 182 | } |