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 / klog_slot.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 | /* klog_slot.c -- see klog_slot.h. Pure. */ |
| 18 | #include "klog_slot.h" |
| 19 | |
| 20 | static int same(const uint8_t *a, const char *b, unsigned n) |
| 21 | { |
| 22 | for (unsigned i = 0; i < n; i++) |
| 23 | if (a[i] != (uint8_t)b[i]) |
| 24 | return 0; |
| 25 | return 1; |
| 26 | } |
| 27 | |
| 28 | int ck_klog_map_check(const struct ck_klog_map *m, uint64_t last_block, const char **why) |
| 29 | { |
| 30 | if (!same(m->magic, CK_KLOG_MAP_MAGIC, 8)) { |
| 31 | *why = "bad map magic"; |
| 32 | return -1; |
| 33 | } |
| 34 | if (m->version != CK_KLOG_SLOT_VERSION) { |
| 35 | *why = "unsupported map version"; |
| 36 | return -1; |
| 37 | } |
| 38 | if (m->sector_size != 512u) { |
| 39 | *why = "sector size not 512"; |
| 40 | return -1; |
| 41 | } |
| 42 | if (m->count < CK_KLOG_MIN_SECTORS || m->count > CK_KLOG_MAX_SECTORS) { |
| 43 | *why = "slot length out of range"; |
| 44 | return -1; |
| 45 | } |
| 46 | if (m->lba == 0) { /* sector 0 is the partition table */ |
| 47 | *why = "slot at sector 0"; |
| 48 | return -1; |
| 49 | } |
| 50 | if (m->lba > UINT64_MAX - m->count) { |
| 51 | *why = "slot end overflows"; |
| 52 | return -1; |
| 53 | } |
| 54 | if (last_block && m->lba + m->count - 1u > last_block) { |
| 55 | *why = "slot past the end of the card"; |
| 56 | return -1; |
| 57 | } |
| 58 | uint8_t any = 0; |
| 59 | for (unsigned i = 0; i < 16; i++) |
| 60 | any |= m->nonce[i]; |
| 61 | if (!any) { |
| 62 | *why = "zero nonce"; |
| 63 | return -1; |
| 64 | } |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | int ck_klog_hdr_match(const struct ck_klog_hdr *h, const struct ck_klog_map *m, const char **why) |
| 69 | { |
| 70 | if (!same(h->magic, CK_KLOG_HDR_MAGIC, 8)) { |
| 71 | *why = "no slot header at the mapped sector"; |
| 72 | return -1; |
| 73 | } |
| 74 | if (h->version != CK_KLOG_SLOT_VERSION) { |
| 75 | *why = "unsupported slot version"; |
| 76 | return -1; |
| 77 | } |
| 78 | for (unsigned i = 0; i < 16; i++) |
| 79 | if (h->nonce[i] != m->nonce[i]) { |
| 80 | *why = "slot nonce differs from the map"; |
| 81 | return -1; |
| 82 | } |
| 83 | if (h->lba != m->lba || h->count != m->count) { |
| 84 | *why = "slot position differs from the map"; |
| 85 | return -1; |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | void ck_klog_hdr_written(struct ck_klog_hdr *h, const struct ck_klog_map *m, uint64_t log_len, |
| 91 | uint64_t log_total, uint64_t commit, const char *stage) |
| 92 | { |
| 93 | uint8_t *p = (uint8_t *)h; |
| 94 | for (unsigned i = 0; i < sizeof *h; i++) |
| 95 | p[i] = 0; |
| 96 | for (unsigned i = 0; i < 8; i++) |
| 97 | h->magic[i] = (uint8_t)CK_KLOG_HDR_MAGIC[i]; |
| 98 | h->version = CK_KLOG_SLOT_VERSION; |
| 99 | h->state = CK_KLOG_STATE_WRITTEN; |
| 100 | for (unsigned i = 0; i < 16; i++) |
| 101 | h->nonce[i] = m->nonce[i]; |
| 102 | h->lba = m->lba; |
| 103 | h->count = m->count; |
| 104 | h->log_len = log_len; |
| 105 | h->log_total = log_total; |
| 106 | h->commit = commit; |
| 107 | for (unsigned i = 0; stage && stage[i] && i < sizeof h->stage - 1; i++) |
| 108 | h->stage[i] = stage[i]; |
| 109 | } |