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.h
| 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.h -- where the kernel may write its log on the boot card, and |
| 18 | * the checks that keep it from writing anywhere else (Raspberry Pi 5 build). |
| 19 | * |
| 20 | * The host side (the run tool) creates a contiguous file on the card's FAT |
| 21 | * volume, writes a slot header into its first sector with a fresh random |
| 22 | * nonce, and writes \EFI\AIENOS\KLOG.MAP naming the file's first absolute |
| 23 | * sector on the whole card, its length and the same nonce. The UEFI stub |
| 24 | * reads the map before ExitBootServices (native/boot/efi_klog.c). The |
| 25 | * kernel writes only after it has read the slot's first sector through its |
| 26 | * own SD driver and found the header with this map's nonce, sector and |
| 27 | * length (ck_klog_hdr_match): a stale map, a moved file, another card or a |
| 28 | * wrong sector number all refuse, and nothing is written. |
| 29 | * |
| 30 | * Slot layout: sector 0 header, sectors 1..count-1 log text. The kernel |
| 31 | * writes the text first and the header (state WRITTEN, lengths) last. |
| 32 | * Both structures are 512 bytes, little endian. */ |
| 33 | #ifndef AIENOS_CK_KLOG_SLOT_H |
| 34 | #define AIENOS_CK_KLOG_SLOT_H |
| 35 | #include <stddef.h> |
| 36 | #include <stdint.h> |
| 37 | |
| 38 | #define CK_KLOG_MAP_MAGIC "AIENKLM1" |
| 39 | #define CK_KLOG_HDR_MAGIC "AIENKLG1" |
| 40 | #define CK_KLOG_SLOT_VERSION 1u |
| 41 | #define CK_KLOG_MAP_PATH "\\EFI\\AIENOS\\KLOG.MAP" |
| 42 | #define CK_KLOG_MIN_SECTORS 2u |
| 43 | #define CK_KLOG_MAX_SECTORS 8192u /* 4 MiB */ |
| 44 | |
| 45 | enum { CK_KLOG_STATE_EMPTY = 0, CK_KLOG_STATE_WRITTEN = 1 }; |
| 46 | |
| 47 | struct ck_klog_map { |
| 48 | uint8_t magic[8]; /* "AIENKLM1" */ |
| 49 | uint32_t version; /* 1 */ |
| 50 | uint32_t sector_size; /* 512 */ |
| 51 | uint64_t lba; /* slot sector 0, absolute on the whole card */ |
| 52 | uint64_t count; /* slot sectors, CK_KLOG_MIN_SECTORS..CK_KLOG_MAX_SECTORS */ |
| 53 | uint8_t nonce[16]; |
| 54 | uint8_t pad[512 - 48]; |
| 55 | }; |
| 56 | |
| 57 | struct ck_klog_hdr { |
| 58 | uint8_t magic[8]; /* "AIENKLG1" */ |
| 59 | uint32_t version; /* 1 */ |
| 60 | uint32_t state; /* CK_KLOG_STATE_* */ |
| 61 | uint8_t nonce[16]; /* equals the map's */ |
| 62 | uint64_t lba; /* equals the map's: a copy of this sector elsewhere never matches */ |
| 63 | uint64_t count; /* equals the map's */ |
| 64 | uint64_t log_len; /* text bytes in sectors 1.. (state WRITTEN) */ |
| 65 | uint64_t log_total; /* bytes the console produced, kept or not */ |
| 66 | uint64_t commit; /* build commit (ck_commit) */ |
| 67 | char stage[32]; /* boot stage at the write, NUL terminated */ |
| 68 | uint8_t pad[512 - 104]; |
| 69 | }; |
| 70 | |
| 71 | _Static_assert(sizeof(struct ck_klog_map) == 512, "ck_klog_map size"); |
| 72 | _Static_assert(sizeof(struct ck_klog_hdr) == 512, "ck_klog_hdr size"); |
| 73 | |
| 74 | /* Structural map check. last_block 0 skips the disk-size comparison. |
| 75 | * 0 or -1 with *why. */ |
| 76 | int ck_klog_map_check(const struct ck_klog_map *m, uint64_t last_block, const char **why); |
| 77 | /* The slot's sector 0 as read from the card matches the map (magic, version, |
| 78 | * nonce, lba, count). Any state. 0 or -1 with *why. */ |
| 79 | int ck_klog_hdr_match(const struct ck_klog_hdr *h, const struct ck_klog_map *m, const char **why); |
| 80 | /* Builds the WRITTEN header for a log of log_len bytes. */ |
| 81 | void ck_klog_hdr_written(struct ck_klog_hdr *h, const struct ck_klog_map *m, uint64_t log_len, |
| 82 | uint64_t log_total, uint64_t commit, const char *stage); |
| 83 | /* Text capacity of the slot in bytes. */ |
| 84 | static inline uint64_t ck_klog_slot_bytes(const struct ck_klog_map *m) { return (m->count - 1u) * 512u; } |
| 85 | #endif |