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 / core / 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 | /* pi5_marker.c -- see pi5_marker.h. */ |
| 18 | #include "pi5_marker.h" |
| 19 | #include "../dev/disk_part.h" |
| 20 | #include "../dev/disk_layout.h" |
| 21 | |
| 22 | #define MAGIC "AIENPI5M" |
| 23 | #define HDR_CRC_BYTES 40u |
| 24 | |
| 25 | static void put64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); } |
| 26 | static void put32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); } |
| 27 | static uint64_t get64(const uint8_t *p) { uint64_t v = 0; for (int i = 7; i >= 0; i--) v = v << 8 | p[i]; return v; } |
| 28 | static uint32_t get32(const uint8_t *p) { uint32_t v = 0; for (int i = 3; i >= 0; i--) v = v << 8 | p[i]; return v; } |
| 29 | |
| 30 | /* splitmix64 of (seed, sector, word): the payload is regenerable. */ |
| 31 | static uint64_t word(uint64_t seed, uint32_t sec, uint32_t w) |
| 32 | { |
| 33 | uint64_t z = seed + 0x9e3779b97f4a7c15ull * ((uint64_t)sec << 8 | w) + 0x9e3779b97f4a7c15ull; |
| 34 | z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull; |
| 35 | z = (z ^ (z >> 27)) * 0x94d049bb133111ebull; |
| 36 | return z ^ (z >> 31); |
| 37 | } |
| 38 | |
| 39 | static void fill_payload(uint8_t *buf, uint64_t seed) |
| 40 | { |
| 41 | for (uint32_t s = 1; s < CK_PI5_MARKER_SECS; s++) |
| 42 | for (uint32_t w = 0; w < DISK_SECTOR_BYTES / 8; w++) |
| 43 | put64(buf + (size_t)s * DISK_SECTOR_BYTES + w * 8u, word(seed, s, w)); |
| 44 | } |
| 45 | |
| 46 | static int payload_ok(const uint8_t *buf, uint64_t seed) |
| 47 | { |
| 48 | for (uint32_t s = 1; s < CK_PI5_MARKER_SECS; s++) |
| 49 | for (uint32_t w = 0; w < DISK_SECTOR_BYTES / 8; w++) |
| 50 | if (get64(buf + (size_t)s * DISK_SECTOR_BYTES + w * 8u) != word(seed, s, w)) |
| 51 | return 0; |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | static void make_header(uint8_t *h, uint64_t gen, uint32_t payload_crc) |
| 56 | { |
| 57 | for (uint32_t i = 0; i < DISK_SECTOR_BYTES; i++) h[i] = 0; |
| 58 | for (int i = 0; i < 8; i++) h[i] = (uint8_t)MAGIC[i]; |
| 59 | put32(h + 8, 1); |
| 60 | put32(h + 12, CK_PI5_MARKER_SECS - 1u); |
| 61 | put64(h + 16, gen); |
| 62 | put64(h + 24, CK_PI5_MARKER_SEED); |
| 63 | put32(h + 32, payload_crc); |
| 64 | put32(h + 36, 0); |
| 65 | put32(h + HDR_CRC_BYTES, ck_crc32(0, h, HDR_CRC_BYTES)); |
| 66 | } |
| 67 | |
| 68 | static int header_ok(const uint8_t *h) |
| 69 | { |
| 70 | for (int i = 0; i < 8; i++) |
| 71 | if (h[i] != (uint8_t)MAGIC[i]) return 0; |
| 72 | return get32(h + 8) == 1 && get32(h + 12) == CK_PI5_MARKER_SECS - 1u && get64(h + 24) == CK_PI5_MARKER_SEED && |
| 73 | get32(h + HDR_CRC_BYTES) == ck_crc32(0, h, HDR_CRC_BYTES); |
| 74 | } |
| 75 | |
| 76 | static int all_zero(const uint8_t *p, size_t n) |
| 77 | { |
| 78 | for (size_t i = 0; i < n; i++) if (p[i]) return 0; |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | static uint32_t payload_crc(const uint8_t *buf) |
| 83 | { |
| 84 | return ck_crc32(0, buf + DISK_SECTOR_BYTES, (size_t)(CK_PI5_MARKER_SECS - 1u) * DISK_SECTOR_BYTES); |
| 85 | } |
| 86 | |
| 87 | /* Write the header sector, flush, read it back and compare. */ |
| 88 | static int write_header(const disk_dev *d, uint64_t lba, const uint8_t *hdr, uint8_t *copy) |
| 89 | { |
| 90 | if (disk_write(d, lba, 1, hdr) != DISK_OK || disk_flush(d) != DISK_OK) return CK_PI5_MARKER_EIO; |
| 91 | if (disk_read(d, lba, 1, copy) != DISK_OK) return CK_PI5_MARKER_EIO; |
| 92 | for (uint32_t i = 0; i < DISK_SECTOR_BYTES; i++) |
| 93 | if (copy[i] != hdr[i]) return CK_PI5_MARKER_EVERIFY; |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | int ck_pi5_marker_run(const disk_dev *d, uint8_t *buf, uint64_t *generation) |
| 98 | { |
| 99 | if (!d || !buf || d->block_size != DISK_SECTOR_BYTES || d->block_count < CK_PI5_MARKER_SECS) |
| 100 | return CK_PI5_MARKER_ESIZE; |
| 101 | uint64_t lba = d->block_count - CK_PI5_MARKER_SECS; |
| 102 | static uint8_t hdr[DISK_SECTOR_BYTES] __attribute__((aligned(64))); |
| 103 | static uint8_t copy[DISK_SECTOR_BYTES] __attribute__((aligned(64))); |
| 104 | if (disk_read(d, lba, CK_PI5_MARKER_SECS, buf) != DISK_OK) return CK_PI5_MARKER_EIO; |
| 105 | int rc; |
| 106 | if (header_ok(buf)) { |
| 107 | uint64_t seed = get64(buf + 24); |
| 108 | if (!payload_ok(buf, seed) || payload_crc(buf) != get32(buf + 32)) return CK_PI5_MARKER_ECORRUPT; |
| 109 | uint64_t gen = get64(buf + 16) + 1u; |
| 110 | make_header(hdr, gen, get32(buf + 32)); |
| 111 | if ((rc = write_header(d, lba, hdr, copy)) != 0) return rc; |
| 112 | *generation = gen; |
| 113 | return CK_PI5_MARKER_REOPENED; |
| 114 | } |
| 115 | if (!all_zero(buf, CK_PI5_MARKER_BUF_BYTES)) return CK_PI5_MARKER_EREFUSED; |
| 116 | fill_payload(buf, CK_PI5_MARKER_SEED); |
| 117 | uint32_t pc = payload_crc(buf); |
| 118 | /* payload first (63 sectors in one multi-sector write), flush, then the header, flush */ |
| 119 | if (disk_write(d, lba + 1u, CK_PI5_MARKER_SECS - 1u, buf + DISK_SECTOR_BYTES) != DISK_OK || disk_flush(d) != DISK_OK) |
| 120 | return CK_PI5_MARKER_EIO; |
| 121 | make_header(hdr, 1, pc); |
| 122 | if ((rc = write_header(d, lba, hdr, copy)) != 0) return rc; |
| 123 | /* whole region back, byte for byte against what was written */ |
| 124 | static uint8_t back[CK_PI5_MARKER_BUF_BYTES] __attribute__((aligned(64))); |
| 125 | if (disk_read(d, lba + 1u, CK_PI5_MARKER_SECS - 1u, back + DISK_SECTOR_BYTES) != DISK_OK) return CK_PI5_MARKER_EIO; |
| 126 | for (size_t i = DISK_SECTOR_BYTES; i < CK_PI5_MARKER_BUF_BYTES; i++) |
| 127 | if (back[i] != buf[i]) return CK_PI5_MARKER_EVERIFY; |
| 128 | *generation = 1; |
| 129 | return CK_PI5_MARKER_FORMATTED; |
| 130 | } |
| 131 | |
| 132 | const char *ck_pi5_marker_strerror(int rc) |
| 133 | { |
| 134 | switch (rc) { |
| 135 | case CK_PI5_MARKER_FORMATTED: return "formatted (blank region written, flushed, read back identical)"; |
| 136 | case CK_PI5_MARKER_REOPENED: return "reopened (payload identical to the pattern, generation bumped and read back)"; |
| 137 | case CK_PI5_MARKER_EIO: return "I/O error"; |
| 138 | case CK_PI5_MARKER_EREFUSED: return "region is neither blank nor a marker, nothing written"; |
| 139 | case CK_PI5_MARKER_ECORRUPT: return "marker header valid but payload differs"; |
| 140 | case CK_PI5_MARKER_EVERIFY: return "read back differs from what was written"; |
| 141 | case CK_PI5_MARKER_ESIZE: return "partition view too small or not 512-byte blocks"; |
| 142 | default: return "unknown"; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | int ck_pi5_store_view(const ck_part *full, ck_part *out) |
| 147 | { |
| 148 | const uint64_t bpu = CK_LAYOUT_UNIT / DISK_SECTOR_BYTES; |
| 149 | if (!full || !out || !full->valid || full->dev.block_size != DISK_SECTOR_BYTES || |
| 150 | full->blocks < CK_PI5_MARKER_SECS + bpu) |
| 151 | return CK_PI5_MARKER_ESIZE; |
| 152 | *out = *full; |
| 153 | out->blocks = (full->blocks - CK_PI5_MARKER_SECS) / bpu * bpu; |
| 154 | out->dev.ctx = out; |
| 155 | out->dev.block_count = out->blocks; |
| 156 | return 0; |
| 157 | } |