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 / svc / allen_memory.c
| 1 | /* allen_memory.c -- Allen's memory on disk in two copies; see allen_memory.h. */ |
| 2 | #include "allen_memory.h" |
| 3 | |
| 4 | #include "sha256.h" |
| 5 | |
| 6 | #define HDR 64u |
| 7 | #define FACT_BYTES (CK_ALLEN_KEY_MAX + CK_ALLEN_VAL_MAX + 1u) |
| 8 | #define PAYLOAD_FIXED (4u + 4u + CK_ALLEN_NAME_MAX + 4u + 4u) |
| 9 | #define VERSION 4u /* 4 adds sky mode and location after the self name; v2/v3 still decode (sky fields zeroed) */ |
| 10 | #define SKY_BYTES 12u /* sky_mode + sky_region + sky_have_loc + sky_pad + sky_lat_udeg + sky_lon_udeg */ |
| 11 | #define SKY_REGIONS 12u /* the somewhere_new regions (allen_reply.c, ui/allen_ui.c SKY_NREGIONS) */ |
| 12 | #define SELF_BYTES CK_ALLEN_NAME_MAX |
| 13 | |
| 14 | _Static_assert(HDR + PAYLOAD_FIXED + CK_ALLEN_FACTS_MAX * FACT_BYTES + SELF_BYTES + SKY_BYTES <= CK_ALLEN_MEM_UNIT, "memory fits one unit"); |
| 15 | |
| 16 | static const uint8_t magic[8] = {'A', 'I', 'E', 'N', 'A', 'L', 'M', '1'}; |
| 17 | static uint8_t g_unit[CK_ALLEN_MEM_UNIT] __attribute__((aligned(64))); |
| 18 | static uint8_t g_back[CK_ALLEN_MEM_UNIT] __attribute__((aligned(64))); |
| 19 | |
| 20 | static void put32(uint8_t *p, uint32_t v) |
| 21 | { |
| 22 | for (int i = 0; i < 4; i++) |
| 23 | p[i] = (uint8_t)(v >> (8 * i)); |
| 24 | } |
| 25 | static void put64(uint8_t *p, uint64_t v) |
| 26 | { |
| 27 | put32(p, (uint32_t)v); |
| 28 | put32(p + 4, (uint32_t)(v >> 32)); |
| 29 | } |
| 30 | static uint32_t get32(const uint8_t *p) |
| 31 | { |
| 32 | return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; |
| 33 | } |
| 34 | static uint64_t get64(const uint8_t *p) { return (uint64_t)get32(p) | (uint64_t)get32(p + 4) << 32; } |
| 35 | static void zero(void *d, uint32_t n) |
| 36 | { |
| 37 | for (uint32_t i = 0; i < n; i++) |
| 38 | ((uint8_t *)d)[i] = 0; |
| 39 | } |
| 40 | static void copy(void *d, const void *s, uint32_t n) |
| 41 | { |
| 42 | for (uint32_t i = 0; i < n; i++) |
| 43 | ((uint8_t *)d)[i] = ((const uint8_t *)s)[i]; |
| 44 | } |
| 45 | static int same(const uint8_t *a, const uint8_t *b, uint32_t n) |
| 46 | { |
| 47 | uint8_t d = 0; |
| 48 | for (uint32_t i = 0; i < n; i++) |
| 49 | d |= (uint8_t)(a[i] ^ b[i]); |
| 50 | return d == 0; |
| 51 | } |
| 52 | |
| 53 | static void seal(uint8_t *unit, uint32_t plen) |
| 54 | { |
| 55 | sha256_ctx h; |
| 56 | sha256_init(&h); |
| 57 | sha256_update(&h, unit, 32); |
| 58 | sha256_update(&h, unit + HDR, plen); |
| 59 | sha256_final(&h, unit + 32); |
| 60 | } |
| 61 | |
| 62 | /* Folded text in a field of cap bytes: NUL-terminated, a-z 0-9 and single |
| 63 | * inner spaces only, the rest of the field zero. letters: a name (A-Z first, |
| 64 | * then a-z, or empty). */ |
| 65 | static int text_ok(const uint8_t *t, uint32_t cap, int name) |
| 66 | { |
| 67 | uint32_t n = 0; |
| 68 | while (n < cap && t[n]) |
| 69 | n++; |
| 70 | if (n == cap) |
| 71 | return 0; |
| 72 | for (uint32_t i = n; i < cap; i++) |
| 73 | if (t[i]) |
| 74 | return 0; |
| 75 | for (uint32_t i = 0; i < n; i++) { |
| 76 | uint8_t c = t[i]; |
| 77 | int lower = c >= 'a' && c <= 'z', digit = c >= '0' && c <= '9'; |
| 78 | if (name) { |
| 79 | /* learn_name keeps one folded word, first letter capitalised */ |
| 80 | if (!(i == 0 ? (c >= 'A' && c <= 'Z') || digit : lower || digit)) |
| 81 | return 0; |
| 82 | } else if (c == ' ') { |
| 83 | if (i == 0 || i == n - 1 || t[i - 1] == ' ') |
| 84 | return 0; |
| 85 | } else if (!lower && !digit) { |
| 86 | return 0; |
| 87 | } |
| 88 | } |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | void ck_allen_mem_encode(const ck_allen_memory *mem, uint64_t gen, uint8_t unit[CK_ALLEN_MEM_UNIT]) |
| 93 | { |
| 94 | uint32_t n = mem->nfacts > CK_ALLEN_FACTS_MAX ? CK_ALLEN_FACTS_MAX : mem->nfacts; |
| 95 | uint32_t plen = PAYLOAD_FIXED + n * FACT_BYTES + SELF_BYTES + SKY_BYTES; |
| 96 | zero(unit, CK_ALLEN_MEM_UNIT); |
| 97 | copy(unit, magic, 8); |
| 98 | put32(unit + 8, VERSION); |
| 99 | put32(unit + 12, plen); |
| 100 | put64(unit + 16, gen); |
| 101 | uint8_t *p = unit + HDR; |
| 102 | put32(p, mem->starts); |
| 103 | put32(p + 4, mem->lines); |
| 104 | copy(p + 8, mem->name, CK_ALLEN_NAME_MAX); |
| 105 | p[8 + CK_ALLEN_NAME_MAX - 1] = 0; |
| 106 | put32(p + 8 + CK_ALLEN_NAME_MAX, n); |
| 107 | put32(p + 12 + CK_ALLEN_NAME_MAX, mem->nnotes); |
| 108 | p += PAYLOAD_FIXED; |
| 109 | for (uint32_t i = 0; i < n; i++, p += FACT_BYTES) { |
| 110 | copy(p, mem->fact[i].key, CK_ALLEN_KEY_MAX); |
| 111 | p[CK_ALLEN_KEY_MAX - 1] = 0; |
| 112 | copy(p + CK_ALLEN_KEY_MAX, mem->fact[i].val, CK_ALLEN_VAL_MAX); |
| 113 | p[CK_ALLEN_KEY_MAX + CK_ALLEN_VAL_MAX - 1] = 0; |
| 114 | p[CK_ALLEN_KEY_MAX + CK_ALLEN_VAL_MAX] = mem->fact[i].are ? 1 : 0; |
| 115 | } |
| 116 | copy(p, mem->self, SELF_BYTES); |
| 117 | p[SELF_BYTES - 1] = 0; |
| 118 | p += SELF_BYTES; |
| 119 | p[0] = mem->sky_mode; |
| 120 | p[1] = mem->sky_region; |
| 121 | p[2] = mem->sky_have_loc; |
| 122 | p[3] = 0; |
| 123 | put32(p + 4, (uint32_t)mem->sky_lat_udeg); |
| 124 | put32(p + 8, (uint32_t)mem->sky_lon_udeg); |
| 125 | seal(unit, plen); |
| 126 | } |
| 127 | |
| 128 | int ck_allen_mem_decode(const uint8_t unit[CK_ALLEN_MEM_UNIT], ck_allen_memory *mem, uint64_t *gen) |
| 129 | { |
| 130 | uint32_t version = get32(unit + 8); |
| 131 | if (!same(unit, magic, 8) || (version < 2 || version > VERSION)) |
| 132 | return -1; |
| 133 | const uint8_t *p = unit + HDR; |
| 134 | uint32_t plen = get32(unit + 12), n = get32(p + 8 + CK_ALLEN_NAME_MAX); |
| 135 | uint32_t self_bytes = version >= 3 ? SELF_BYTES : 0; |
| 136 | uint32_t sky_bytes = version >= 4 ? SKY_BYTES : 0; |
| 137 | if (n > CK_ALLEN_FACTS_MAX || get32(p + 12 + CK_ALLEN_NAME_MAX) > CK_ALLEN_NOTES_MAX || |
| 138 | plen != PAYLOAD_FIXED + n * FACT_BYTES + self_bytes + sky_bytes || get64(unit + 16) == 0) |
| 139 | return -1; |
| 140 | for (uint32_t i = 24; i < 32; i++) |
| 141 | if (unit[i]) |
| 142 | return -1; |
| 143 | for (uint32_t i = HDR + plen; i < CK_ALLEN_MEM_UNIT; i++) |
| 144 | if (unit[i]) |
| 145 | return -1; |
| 146 | uint8_t want[32]; |
| 147 | sha256_ctx h; |
| 148 | sha256_init(&h); |
| 149 | sha256_update(&h, unit, 32); |
| 150 | sha256_update(&h, unit + HDR, plen); |
| 151 | sha256_final(&h, want); |
| 152 | if (!same(want, unit + 32, 32)) |
| 153 | return -1; |
| 154 | if (!text_ok(p + 8, CK_ALLEN_NAME_MAX, 1)) |
| 155 | return -1; |
| 156 | const uint8_t *f = p + PAYLOAD_FIXED; |
| 157 | for (uint32_t i = 0; i < n; i++, f += FACT_BYTES) |
| 158 | if (!text_ok(f, CK_ALLEN_KEY_MAX, 0) || !f[0] || !text_ok(f + CK_ALLEN_KEY_MAX, CK_ALLEN_VAL_MAX, 0) || |
| 159 | !f[CK_ALLEN_KEY_MAX] || f[CK_ALLEN_KEY_MAX + CK_ALLEN_VAL_MAX] > 1) |
| 160 | return -1; |
| 161 | if (self_bytes && !text_ok(f, SELF_BYTES, 1)) |
| 162 | return -1; |
| 163 | if (sky_bytes) { /* sky fields as allen_reply.h defines them; the reserved byte is zero */ |
| 164 | const uint8_t *sp = f + self_bytes; |
| 165 | int32_t lat = (int32_t)get32(sp + 4), lon = (int32_t)get32(sp + 8); |
| 166 | if (sp[0] > 2 || sp[1] >= SKY_REGIONS || sp[2] > 1 || sp[3] || lat < -90000000 || lat > 90000000 || |
| 167 | lon < -180000000 || lon > 180000000) |
| 168 | return -1; |
| 169 | } |
| 170 | zero(mem, sizeof *mem); |
| 171 | mem->starts = get32(p); |
| 172 | mem->lines = get32(p + 4); |
| 173 | copy(mem->name, p + 8, CK_ALLEN_NAME_MAX); |
| 174 | mem->nfacts = n; |
| 175 | mem->nnotes = get32(p + 12 + CK_ALLEN_NAME_MAX); |
| 176 | f = p + PAYLOAD_FIXED; |
| 177 | for (uint32_t i = 0; i < n; i++, f += FACT_BYTES) { |
| 178 | copy(mem->fact[i].key, f, CK_ALLEN_KEY_MAX); |
| 179 | copy(mem->fact[i].val, f + CK_ALLEN_KEY_MAX, CK_ALLEN_VAL_MAX); |
| 180 | mem->fact[i].are = f[CK_ALLEN_KEY_MAX + CK_ALLEN_VAL_MAX]; |
| 181 | } |
| 182 | if (self_bytes) |
| 183 | copy(mem->self, f, SELF_BYTES); |
| 184 | else |
| 185 | zero(mem->self, SELF_BYTES); |
| 186 | /* sky fields (version 4+); zero for older versions */ |
| 187 | if (sky_bytes) { |
| 188 | const uint8_t *sp = f + self_bytes; |
| 189 | mem->sky_mode = sp[0]; |
| 190 | mem->sky_region = sp[1]; |
| 191 | mem->sky_have_loc = sp[2]; |
| 192 | mem->sky_pad = 0; |
| 193 | mem->sky_lat_udeg = (int32_t)get32(sp + 4); |
| 194 | mem->sky_lon_udeg = (int32_t)get32(sp + 8); |
| 195 | } else { |
| 196 | mem->sky_mode = mem->sky_region = mem->sky_have_loc = mem->sky_pad = 0; |
| 197 | mem->sky_lat_udeg = mem->sky_lon_udeg = 0; |
| 198 | } |
| 199 | *gen = get64(unit + 16); |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static int geom(const disk_dev *d, uint32_t *per) |
| 204 | { |
| 205 | if (disk_check(d) != DISK_OK || (d->block_size != 512 && d->block_size != 4096)) |
| 206 | return CK_ALLEN_MEM_EGEOM; |
| 207 | *per = CK_ALLEN_MEM_UNIT / d->block_size; |
| 208 | if (d->block_count < (uint64_t)*per * CK_ALLEN_MEM_UNITS) |
| 209 | return CK_ALLEN_MEM_EGEOM; |
| 210 | return CK_ALLEN_MEM_OK; |
| 211 | } |
| 212 | |
| 213 | int ck_allen_store_load(ck_allen_store *st, const disk_dev *part, ck_allen_memory *mem) |
| 214 | { |
| 215 | uint32_t per = 0; |
| 216 | zero(st, sizeof *st); |
| 217 | zero(mem, sizeof *mem); |
| 218 | st->dev = part; |
| 219 | int rc = geom(part, &per); |
| 220 | if (rc) |
| 221 | return rc; |
| 222 | for (unsigned c = 0; c < 2; c++) { |
| 223 | ck_allen_memory m; |
| 224 | uint64_t g = 0; |
| 225 | if (disk_read(part, (uint64_t)c * per, per, g_unit) != DISK_OK) |
| 226 | return CK_ALLEN_MEM_EIO; |
| 227 | if (ck_allen_mem_decode(g_unit, &m, &g)) |
| 228 | continue; |
| 229 | st->valid++; |
| 230 | if (g > st->gen) { |
| 231 | st->gen = g; |
| 232 | st->newest = c; |
| 233 | *mem = m; |
| 234 | } |
| 235 | } |
| 236 | return st->gen ? CK_ALLEN_MEM_OK : CK_ALLEN_MEM_EMPTY; |
| 237 | } |
| 238 | |
| 239 | int ck_allen_store_save(ck_allen_store *st, const ck_allen_memory *mem) |
| 240 | { |
| 241 | uint32_t per = 0; |
| 242 | int rc = geom(st->dev, &per); |
| 243 | if (rc) |
| 244 | return rc; |
| 245 | unsigned target = st->gen ? 1u - st->newest : 0u; /* the older copy (A on a first save) */ |
| 246 | uint64_t lba = (uint64_t)target * per; |
| 247 | ck_allen_mem_encode(mem, st->gen + 1, g_unit); |
| 248 | if (disk_write(st->dev, lba, per, g_unit) != DISK_OK || disk_flush(st->dev) != DISK_OK) |
| 249 | return CK_ALLEN_MEM_EIO; |
| 250 | if (disk_read(st->dev, lba, per, g_back) != DISK_OK) |
| 251 | return CK_ALLEN_MEM_EIO; |
| 252 | if (!same(g_unit, g_back, CK_ALLEN_MEM_UNIT)) |
| 253 | return CK_ALLEN_MEM_EVERIFY; |
| 254 | st->gen++; |
| 255 | st->newest = target; |
| 256 | if (st->valid < 2) |
| 257 | st->valid++; |
| 258 | return CK_ALLEN_MEM_OK; |
| 259 | } |
| 260 | |
| 261 | /* ---- notes: one unit each, from unit CK_ALLEN_NOTES_FIRST ---- */ |
| 262 | |
| 263 | static const uint8_t note_magic[8] = {'A', 'I', 'E', 'N', 'N', 'O', 'T', '1'}; |
| 264 | |
| 265 | void ck_allen_note_encode(const ck_allen_note *n, uint32_t index, uint8_t unit[CK_ALLEN_MEM_UNIT]) |
| 266 | { |
| 267 | uint32_t len = n->len < CK_ALLEN_NOTE_MAX ? n->len : CK_ALLEN_NOTE_MAX - 1u; |
| 268 | zero(unit, CK_ALLEN_MEM_UNIT); |
| 269 | copy(unit, note_magic, 8); |
| 270 | put32(unit + 8, 1); |
| 271 | put32(unit + 12, len); |
| 272 | put32(unit + 16, index); |
| 273 | put32(unit + 20, n->start); |
| 274 | put32(unit + 24, n->gone ? 1u : 0u); |
| 275 | copy(unit + HDR, n->text, len); |
| 276 | seal(unit, len); |
| 277 | } |
| 278 | |
| 279 | int ck_allen_note_decode(const uint8_t unit[CK_ALLEN_MEM_UNIT], uint32_t index, ck_allen_note *n) |
| 280 | { |
| 281 | uint32_t len = get32(unit + 12); |
| 282 | if (!same(unit, note_magic, 8) || get32(unit + 8) != 1 || len == 0 || len >= CK_ALLEN_NOTE_MAX || |
| 283 | get32(unit + 16) != index || get32(unit + 24) > 1 || get32(unit + 28)) |
| 284 | return -1; |
| 285 | for (uint32_t i = HDR + len; i < CK_ALLEN_MEM_UNIT; i++) |
| 286 | if (unit[i]) |
| 287 | return -1; |
| 288 | for (uint32_t i = 0; i < len; i++) |
| 289 | if (unit[HDR + i] < 0x20 || unit[HDR + i] > 0x7e) |
| 290 | return -1; |
| 291 | uint8_t want[32]; |
| 292 | sha256_ctx h; |
| 293 | sha256_init(&h); |
| 294 | sha256_update(&h, unit, 32); |
| 295 | sha256_update(&h, unit + HDR, len); |
| 296 | sha256_final(&h, want); |
| 297 | if (!same(want, unit + 32, 32)) |
| 298 | return -1; |
| 299 | zero(n, sizeof *n); |
| 300 | copy(n->text, unit + HDR, len); |
| 301 | n->len = (uint16_t)len; |
| 302 | n->start = get32(unit + 20); |
| 303 | n->gone = (uint8_t)get32(unit + 24); |
| 304 | ck_allen_note_index(n); |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static int notes_geom(const disk_dev *d, uint32_t count, uint32_t *per) |
| 309 | { |
| 310 | int rc = geom(d, per); |
| 311 | if (rc) |
| 312 | return rc; |
| 313 | if (count > CK_ALLEN_NOTES_MAX || d->block_count < (uint64_t)*per * (CK_ALLEN_NOTES_FIRST + count)) |
| 314 | return CK_ALLEN_MEM_EGEOM; |
| 315 | return CK_ALLEN_MEM_OK; |
| 316 | } |
| 317 | |
| 318 | int ck_allen_notes_load(const disk_dev *part, uint32_t count, ck_allen_notes *ns, uint32_t *damaged) |
| 319 | { |
| 320 | uint32_t per = 0; |
| 321 | zero(ns, sizeof *ns); |
| 322 | *damaged = 0; |
| 323 | int rc = notes_geom(part, count, &per); |
| 324 | if (rc) |
| 325 | return rc; |
| 326 | for (uint32_t i = 0; i < count; i++) { |
| 327 | if (disk_read(part, (uint64_t)(CK_ALLEN_NOTES_FIRST + i) * per, per, g_unit) != DISK_OK) |
| 328 | return CK_ALLEN_MEM_EIO; |
| 329 | if (ck_allen_note_decode(g_unit, i, &ns->note[i])) { |
| 330 | zero(&ns->note[i], sizeof ns->note[i]); |
| 331 | ns->note[i].gone = 1; |
| 332 | (*damaged)++; |
| 333 | } |
| 334 | } |
| 335 | ns->n = count; |
| 336 | return CK_ALLEN_MEM_OK; |
| 337 | } |
| 338 | |
| 339 | int ck_allen_note_write(const disk_dev *part, const ck_allen_notes *ns, uint32_t index) |
| 340 | { |
| 341 | uint32_t per = 0; |
| 342 | int rc = notes_geom(part, index + 1u, &per); |
| 343 | if (rc) |
| 344 | return rc; |
| 345 | uint64_t lba = (uint64_t)(CK_ALLEN_NOTES_FIRST + index) * per; |
| 346 | ck_allen_note_encode(&ns->note[index], index, g_unit); |
| 347 | if (disk_write(part, lba, per, g_unit) != DISK_OK || disk_flush(part) != DISK_OK) |
| 348 | return CK_ALLEN_MEM_EIO; |
| 349 | if (disk_read(part, lba, per, g_back) != DISK_OK) |
| 350 | return CK_ALLEN_MEM_EIO; |
| 351 | return same(g_unit, g_back, CK_ALLEN_MEM_UNIT) ? CK_ALLEN_MEM_OK : CK_ALLEN_MEM_EVERIFY; |
| 352 | } |