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 / dev / tests / fat_boot_test.c
| 1 | /* fat_boot_test.c -- host tests for the AIENOS boot section: |
| 2 | * 1. the EFI System Partition added in unused space next to an existing |
| 3 | * partition full of sentinel bytes (dev/gpt_add.c ck_gpt_add), known as |
| 4 | * ours afterwards; a foreign one is not; |
| 5 | * 2. a volume that is not ours is refused unless formatting is allowed; |
| 6 | * installed: only blocks inside the partition change; the same image |
| 7 | * again writes nothing; a new image goes to the other slot; |
| 8 | * 3. a power cut after every possible number of written blocks (sampled, |
| 9 | * plus the exact switch point) leaves exactly the old or the new |
| 10 | * kernel, old before one point and new after it, and the next install |
| 11 | * completes; |
| 12 | * 4. FAT time stamps. |
| 13 | * Writes part1.img/img1.bin, part2.img/img2.bin and part3.img into the |
| 14 | * folder given, for scripts/ck_fat_boot_test.sh to check with fsck.fat and |
| 15 | * mtools (outside readers). Hosted test code only (libc). */ |
| 16 | #include <fcntl.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <unistd.h> |
| 21 | #include "disk_file.h" |
| 22 | #include "disk_part.h" |
| 23 | #include "fat_boot.h" |
| 24 | #include "gpt_add.h" |
| 25 | #include "gpt_write.h" |
| 26 | |
| 27 | static int checks, failures; |
| 28 | #define CHECK(c) \ |
| 29 | do { \ |
| 30 | checks++; \ |
| 31 | if (!(c)) { \ |
| 32 | failures++; \ |
| 33 | printf(" FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); \ |
| 34 | } \ |
| 35 | } while (0) |
| 36 | |
| 37 | #define BS 512u |
| 38 | #define MIB (1024u * 1024u / BS) |
| 39 | static const ck_gpt_new esp = {ck_esp_part_type, "AIENOS boot", "aienos.boot.v1"}; |
| 40 | static const uint32_t serial = 0x1234abcdu; |
| 41 | static char g_dir[400], g_path[512]; |
| 42 | |
| 43 | static uint8_t *slurp(const char *path, size_t *len) |
| 44 | { |
| 45 | FILE *fp = fopen(path, "rb"); |
| 46 | if (!fp) |
| 47 | return NULL; |
| 48 | fseek(fp, 0, SEEK_END); |
| 49 | long n = ftell(fp); |
| 50 | fseek(fp, 0, SEEK_SET); |
| 51 | uint8_t *b = malloc((size_t)n); |
| 52 | if (b && fread(b, 1, (size_t)n, fp) != (size_t)n) { |
| 53 | free(b); |
| 54 | b = NULL; |
| 55 | } |
| 56 | fclose(fp); |
| 57 | *len = (size_t)n; |
| 58 | return b; |
| 59 | } |
| 60 | |
| 61 | static int spill(const char *name, const uint8_t *b, size_t n) |
| 62 | { |
| 63 | char path[512]; |
| 64 | snprintf(path, sizeof path, "%s/%s", g_dir, name); |
| 65 | /* 0644, not fopen's 0666 & ~umask: the images must not be world-writable. */ |
| 66 | int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); |
| 67 | FILE *fp = fd < 0 ? NULL : fdopen(fd, "wb"); |
| 68 | if (fd >= 0 && !fp) |
| 69 | close(fd); |
| 70 | if (!fp) |
| 71 | return -1; |
| 72 | size_t w = fwrite(b, 1, n, fp); |
| 73 | return fclose(fp) == 0 && w == n ? 0 : -1; |
| 74 | } |
| 75 | |
| 76 | static void pattern(uint8_t *b, size_t n, uint32_t seed) |
| 77 | { |
| 78 | for (size_t i = 0; i < n; i++) { |
| 79 | seed = seed * 1103515245u + 12345u; |
| 80 | b[i] = (uint8_t)(seed >> 16); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /* the partition's bytes from the image file, written to name */ |
| 85 | static int export_part(const ck_part *p, const char *name) |
| 86 | { |
| 87 | size_t n = 0; |
| 88 | uint8_t *all = slurp(g_path, &n); |
| 89 | int rc = all ? spill(name, all + p->first_lba * BS, p->blocks * BS) : -1; |
| 90 | free(all); |
| 91 | return rc; |
| 92 | } |
| 93 | |
| 94 | /* xors byte at of the partition's sector lba with x, through the disk itself */ |
| 95 | static int poke(const ck_part *p, uint32_t lba, uint32_t at, uint8_t x) |
| 96 | { |
| 97 | uint8_t s[BS]; |
| 98 | if (disk_read(&p->dev, lba, 1, s) != DISK_OK) |
| 99 | return -1; |
| 100 | s[at] ^= x; |
| 101 | return disk_write(&p->dev, lba, 1, s) == DISK_OK ? 0 : -1; |
| 102 | } |
| 103 | |
| 104 | /* the changes FAT drivers make in normal use: BS_Reserved1, FAT[1]'s two |
| 105 | * flags in both FATs, DIR_LstAccDate in the root and in BOOT */ |
| 106 | static int driver_marks(const ck_part *p, uint32_t rsv, uint32_t fatsz) |
| 107 | { |
| 108 | uint32_t root = rsv + 2u * fatsz; |
| 109 | return poke(p, 0, 65, 0x01) || poke(p, rsv, 7, 0x0c) || poke(p, rsv + fatsz, 7, 0x08) || |
| 110 | poke(p, root, 32 + 18, 0x5a) || poke(p, root + 2, 19, 0x01); |
| 111 | } |
| 112 | |
| 113 | static int install(disk_file *f, const ck_part *p, int fmt, const uint8_t *man, const uint8_t *img, uint32_t len, |
| 114 | ck_fatb_result *r, uint64_t *blocks) |
| 115 | { |
| 116 | uint64_t b0 = f->blocks_written; |
| 117 | int rc = ck_fatb_install(&p->dev, serial, fmt, man, img, len, 23890, 12149, r); |
| 118 | if (blocks) |
| 119 | *blocks = f->blocks_written - b0; |
| 120 | return rc; |
| 121 | } |
| 122 | |
| 123 | int main(int argc, char **argv) |
| 124 | { |
| 125 | if (argc < 2) |
| 126 | return 2; |
| 127 | snprintf(g_dir, sizeof g_dir, "%s", argv[1]); |
| 128 | snprintf(g_path, sizeof g_path, "%s/disk.img", g_dir); |
| 129 | const uint64_t disk = 96 * MIB; |
| 130 | |
| 131 | /* 1. the partition */ |
| 132 | gw_part sentinel[1] = {{gw_linux_type, 1 * MIB, 9 * MIB - 1u, "old system", 0xa5}}; |
| 133 | CHECK(gw_write(g_path, BS, disk, sentinel, 1, 0) == 0); |
| 134 | disk_file f; |
| 135 | disk_dev d; |
| 136 | CHECK(disk_file_open(&f, &d, g_path, BS, 0, 0) == 0); |
| 137 | ck_gpt_added a; |
| 138 | CHECK(ck_gpt_add(&d, &esp, 64 * MIB, MIB, &a) == CK_GPT_OK); |
| 139 | CHECK(a.created == 1 && a.first == 9 * MIB && a.last == 73 * MIB - 1u && a.index == 1); |
| 140 | CHECK(ck_gpt_is_ours(&d, &esp, a.index) == 1); |
| 141 | CHECK(ck_gpt_add(&d, &esp, 64 * MIB, MIB, &a) == CK_GPT_OK && a.created == 0 && a.first == 9 * MIB); |
| 142 | ck_part p, q; |
| 143 | ck_gpt_info gi; |
| 144 | CHECK(ck_gpt_find_type(&d, ck_esp_part_type, &p, &gi) == CK_GPT_OK && p.first_lba == 9 * MIB && |
| 145 | p.blocks == 64 * MIB); |
| 146 | CHECK(ck_gpt_find_aienos(&d, &q, &gi) == CK_GPT_ENOPART); /* GPT still valid; no Allen section yet */ |
| 147 | /* Allen's section can still be added beside it */ |
| 148 | CHECK(ck_gpt_add_aienos(&d, 8 * MIB, MIB, &a) == CK_GPT_OK && a.created == 1 && a.first == 73 * MIB); |
| 149 | CHECK(ck_gpt_find_type(&d, ck_esp_part_type, &p, &gi) == CK_GPT_OK && ck_gpt_is_ours(&d, &esp, gi.part_index) == 1); |
| 150 | |
| 151 | /* 2. install */ |
| 152 | ck_fatb_state st; |
| 153 | CHECK(ck_fatb_state_read(&p.dev, serial, &st) == CK_FATB_OK && st.ours == 0 && st.slot == -1); |
| 154 | static uint8_t got_man[CK_FATB_MAN_BYTES]; |
| 155 | CHECK(ck_fatb_manifest_read(&p.dev, serial, got_man) == CK_FATB_ENOTOURS); /* not formatted yet */ |
| 156 | static uint8_t man1[CK_FATB_MAN_BYTES], man2[CK_FATB_MAN_BYTES]; |
| 157 | const uint32_t len1 = 300001, len2 = 123457; |
| 158 | uint8_t *img1 = malloc(len1), *img2 = malloc(len2); |
| 159 | if (!img1 || !img2) |
| 160 | return 2; |
| 161 | pattern(man1, sizeof man1, 1); |
| 162 | pattern(man2, sizeof man2, 2); |
| 163 | pattern(img1, len1, 3); |
| 164 | pattern(img2, len2, 4); |
| 165 | ck_fatb_result r; |
| 166 | uint64_t wrote = 0; |
| 167 | size_t n0, n1; |
| 168 | uint8_t *before = slurp(g_path, &n0), *after; |
| 169 | CHECK(install(&f, &p, 0, man1, img1, len1, &r, &wrote) == CK_FATB_ENOTOURS && wrote == 0); |
| 170 | CHECK(install(&f, &p, 1, man1, img1, len1, &r, &wrote) == CK_FATB_OK && r.formatted && r.written && r.slot == 0); |
| 171 | after = slurp(g_path, &n1); |
| 172 | CHECK(before && after && n0 == n1); |
| 173 | if (before && after && n0 == n1) { |
| 174 | CHECK(!memcmp(before, after, p.first_lba * BS)); /* MBR, GPT, the sentinel partition */ |
| 175 | size_t end = (p.first_lba + p.blocks) * BS; |
| 176 | CHECK(!memcmp(before + end, after + end, n0 - end)); /* Allen's section, the backup GPT */ |
| 177 | } |
| 178 | free(before); |
| 179 | free(after); |
| 180 | CHECK(ck_fatb_state_read(&p.dev, serial, &st) == CK_FATB_OK && st.ours && st.slot == 0 && st.image_len == len1); |
| 181 | CHECK(ck_fatb_state_read(&p.dev, serial + 1u, &st) == CK_FATB_OK && st.ours == 0); /* another serial: not ours */ |
| 182 | CHECK(ck_fatb_manifest_read(&p.dev, serial, got_man) == CK_FATB_OK && !memcmp(got_man, man1, sizeof man1)); |
| 183 | CHECK(ck_fatb_manifest_read(&p.dev, serial + 1u, got_man) == CK_FATB_ENOTOURS); |
| 184 | { |
| 185 | /* what FAT drivers change in normal use leaves it ours; any other change is named */ |
| 186 | uint8_t s0[BS]; |
| 187 | CHECK(disk_read(&p.dev, 0, 1, s0) == DISK_OK); |
| 188 | uint32_t rsv = (uint32_t)s0[14] | (uint32_t)s0[15] << 8, fatsz; |
| 189 | memcpy(&fatsz, s0 + 36, 4); |
| 190 | uint32_t efi = rsv + 2u * fatsz + 1u, boot_entry = 64u + 13u; /* EFI dir, BOOT's DIR_CrtTimeTenth */ |
| 191 | CHECK(driver_marks(&p, rsv, fatsz) == 0); |
| 192 | CHECK(ck_fatb_state_read(&p.dev, serial, &st) == CK_FATB_OK && st.ours && st.slot == 0 && st.image_len == len1); |
| 193 | CHECK(poke(&p, efi, boot_entry, 0x01) == 0); |
| 194 | CHECK(ck_fatb_state_read(&p.dev, serial, &st) == CK_FATB_OK && !st.ours && st.where && |
| 195 | !strcmp(st.where, "EFI") && st.at == boot_entry && st.got == 1 && st.want == 0); |
| 196 | CHECK(poke(&p, efi, boot_entry, 0x01) == 0 && driver_marks(&p, rsv, fatsz) == 0); /* back as written */ |
| 197 | CHECK(ck_fatb_state_read(&p.dev, serial, &st) == CK_FATB_OK && st.ours && st.slot == 0); |
| 198 | /* Apple sets ATTR_ARCHIVE on the root's EFI directory during boot. */ |
| 199 | CHECK(poke(&p, efi - 1u, 32u + 11u, 0x20) == 0); |
| 200 | CHECK(ck_fatb_state_read(&p.dev, serial, &st) == CK_FATB_OK && st.ours && st.slot == 0); |
| 201 | /* ... and stamps it written (real machine: root byte 54 = 0x82); a |
| 202 | * file entry's stamp is not taken over */ |
| 203 | CHECK(poke(&p, efi - 1u, 32u + 22u, 0x82) == 0 && poke(&p, efi - 1u, 32u + 25u, 0x5a) == 0); |
| 204 | CHECK(ck_fatb_state_read(&p.dev, serial, &st) == CK_FATB_OK && st.ours && st.slot == 0); |
| 205 | CHECK(poke(&p, efi - 1u, 32u + 22u, 0x82) == 0 && poke(&p, efi - 1u, 32u + 25u, 0x5a) == 0); |
| 206 | CHECK(poke(&p, efi - 1u, 22u, 0x01) == 0); /* the volume label entry: strict */ |
| 207 | CHECK(ck_fatb_state_read(&p.dev, serial, &st) == CK_FATB_OK && !st.ours && st.at == 22u); |
| 208 | CHECK(poke(&p, efi - 1u, 22u, 0x01) == 0); |
| 209 | /* The directory type is still structural, even with ARCHIVE set. */ |
| 210 | CHECK(poke(&p, efi - 1u, 32u + 11u, 0x10) == 0); |
| 211 | CHECK(ck_fatb_state_read(&p.dev, serial, &st) == CK_FATB_OK && !st.ours && st.where && |
| 212 | !strcmp(st.where, "root") && st.at == 43u); |
| 213 | CHECK(poke(&p, efi - 1u, 32u + 11u, 0x10) == 0); |
| 214 | /* Do not ignore archive bits in the volume label or unused entries. */ |
| 215 | CHECK(poke(&p, efi - 1u, 11u, 0x20) == 0); |
| 216 | CHECK(ck_fatb_state_read(&p.dev, serial, &st) == CK_FATB_OK && !st.ours); |
| 217 | CHECK(poke(&p, efi - 1u, 11u, 0x20) == 0); |
| 218 | CHECK(poke(&p, efi - 1u, 64u + 11u, 0x20) == 0); |
| 219 | CHECK(ck_fatb_state_read(&p.dev, serial, &st) == CK_FATB_OK && !st.ours); |
| 220 | CHECK(poke(&p, efi - 1u, 64u + 11u, 0x20) == 0); |
| 221 | CHECK(ck_fatb_state_read(&p.dev, serial, &st) == CK_FATB_OK && st.ours && st.slot == 0); |
| 222 | } |
| 223 | CHECK(export_part(&p, "part1.img") == 0 && spill("img1.bin", img1, len1) == 0); |
| 224 | CHECK(install(&f, &p, 0, man1, img1, len1, &r, &wrote) == CK_FATB_OK && !r.written && r.slot == 0 && wrote == 0); |
| 225 | static uint8_t big[1]; |
| 226 | CHECK(install(&f, &p, 0, man1, big, st.slot_max + 1u, &r, &wrote) == CK_FATB_EBIG && wrote == 0); |
| 227 | |
| 228 | /* 3. power cuts while img2 replaces img1 */ |
| 229 | uint8_t *base = malloc(p.blocks * BS); |
| 230 | if (!base) |
| 231 | return 2; |
| 232 | CHECK(pread(f.fd, base, p.blocks * BS, (off_t)(p.first_lba * BS)) == (ssize_t)(p.blocks * BS)); |
| 233 | uint64_t total = 0; |
| 234 | CHECK(install(&f, &p, 0, man2, img2, len2, &r, &total) == CK_FATB_OK && r.written && r.slot == 1); |
| 235 | CHECK(ck_fatb_manifest_read(&p.dev, serial, got_man) == CK_FATB_OK && !memcmp(got_man, man2, sizeof man2)); |
| 236 | int last = 0, bad = 0, switches = 0; |
| 237 | uint64_t first_new = 0; |
| 238 | for (uint64_t cut = 0; cut <= total; cut += cut < 16 || total - cut < 16 ? 1 : total / 97 + 1) { |
| 239 | if (pwrite(f.fd, base, p.blocks * BS, (off_t)(p.first_lba * BS)) != (ssize_t)(p.blocks * BS)) |
| 240 | bad++; |
| 241 | disk_file_arm_cut(&f, cut); |
| 242 | int rc = install(&f, &p, 0, man2, img2, len2, &r, 0); |
| 243 | disk_file_disarm(&f); |
| 244 | if (rc == CK_FATB_OK && cut < total) /* a cut is always reported (at the very end only the flush is lost) */ |
| 245 | bad++; |
| 246 | int now = -1; |
| 247 | if (ck_fatb_state_read(&p.dev, serial, &st) == CK_FATB_OK && st.ours) { |
| 248 | uint64_t w; |
| 249 | if (st.slot == 0 && install(&f, &p, 0, man1, img1, len1, &r, &w) == CK_FATB_OK && !r.written && !w) |
| 250 | now = 0; |
| 251 | else if (st.slot == 1 && install(&f, &p, 0, man2, img2, len2, &r, &w) == CK_FATB_OK && !r.written && !w) |
| 252 | now = 1; |
| 253 | } |
| 254 | if (now < 0 || now < last) { |
| 255 | printf(" cut after %llu of %llu blocks: %s\n", (unsigned long long)cut, (unsigned long long)total, |
| 256 | now < 0 ? "neither kernel whole" : "back to the old kernel"); |
| 257 | bad++; |
| 258 | } |
| 259 | if (now == 1 && last == 0) { |
| 260 | switches++; |
| 261 | first_new = cut; |
| 262 | } |
| 263 | last = now < 0 ? last : now; |
| 264 | if (install(&f, &p, 0, man2, img2, len2, &r, 0) != CK_FATB_OK) |
| 265 | bad++; /* the next start installs it whole */ |
| 266 | } |
| 267 | /* the exact switch: one block before it the old kernel, at it the new */ |
| 268 | uint64_t lo = 0, hi = first_new; |
| 269 | while (lo + 1 < hi) { |
| 270 | uint64_t mid = (lo + hi) / 2; |
| 271 | if (pwrite(f.fd, base, p.blocks * BS, (off_t)(p.first_lba * BS)) != (ssize_t)(p.blocks * BS)) |
| 272 | bad++; |
| 273 | disk_file_arm_cut(&f, mid); |
| 274 | install(&f, &p, 0, man2, img2, len2, &r, 0); |
| 275 | disk_file_disarm(&f); |
| 276 | if (ck_fatb_state_read(&p.dev, serial, &st) == CK_FATB_OK && st.slot == 1) |
| 277 | hi = mid; |
| 278 | else |
| 279 | lo = mid; |
| 280 | } |
| 281 | printf("note: replacing the kernel writes %llu blocks; the firmware's view switches after block %llu " |
| 282 | "(one directory sector); cut points tried with neither kernel whole: %d\n", |
| 283 | (unsigned long long)total, (unsigned long long)hi, bad); |
| 284 | CHECK(bad == 0 && switches == 1 && hi > 0); |
| 285 | CHECK(pwrite(f.fd, base, p.blocks * BS, (off_t)(p.first_lba * BS)) == (ssize_t)(p.blocks * BS)); |
| 286 | CHECK(install(&f, &p, 0, man2, img2, len2, &r, 0) == CK_FATB_OK && r.written && r.slot == 1); |
| 287 | CHECK(export_part(&p, "part2.img") == 0 && spill("img2.bin", img2, len2) == 0); |
| 288 | CHECK(install(&f, &p, 0, man1, img1, len1, &r, 0) == CK_FATB_OK && r.written && r.slot == 0); |
| 289 | CHECK(export_part(&p, "part3.img") == 0); |
| 290 | free(base); |
| 291 | disk_file_close(&f); |
| 292 | |
| 293 | /* someone else's EFI System Partition is not ours */ |
| 294 | gw_part foreign[1] = {{gw_esp_type, 1 * MIB, 65 * MIB - 1u, "AIENOS boot", 0}}; |
| 295 | CHECK(gw_write(g_path, BS, disk, foreign, 1, 0) == 0); |
| 296 | CHECK(disk_file_open(&f, &d, g_path, BS, 0, 0) == 0); |
| 297 | CHECK(ck_gpt_find_type(&d, ck_esp_part_type, &p, &gi) == CK_GPT_OK && ck_gpt_is_ours(&d, &esp, gi.part_index) == 0); |
| 298 | CHECK(ck_gpt_add(&d, &esp, 64 * MIB, MIB, &a) == CK_GPT_OK && a.created == 0); /* reported, never a second */ |
| 299 | disk_file_close(&f); |
| 300 | |
| 301 | /* 4. time stamps */ |
| 302 | uint16_t dd, tt; |
| 303 | ck_fatb_dos_time(1792303183u, &dd, &tt); /* 2026-10-18 05:59:43 UTC */ |
| 304 | CHECK(dd == 23890 && tt == 12149); |
| 305 | ck_fatb_dos_time(1709251199u, &dd, &tt); /* 2024-02-29 23:59:59 UTC */ |
| 306 | CHECK(dd == 22621 && tt == 49021); |
| 307 | ck_fatb_dos_time(0, &dd, &tt); |
| 308 | CHECK(dd == 0x21 && tt == 0); |
| 309 | |
| 310 | free(img1); |
| 311 | free(img2); |
| 312 | printf("fat boot: %d checks, %d failures\n", checks, failures); |
| 313 | return failures ? 1 : 0; |
| 314 | } |