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 / disk_part.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 | /* disk_part.h -- the AIENOS C kernel's partition-aware disk footprint. |
| 18 | * |
| 19 | * The kernel never writes a whole NVMe namespace. At bind time it parses the |
| 20 | * disk's GUID Partition Table (UEFI 2.10, section 5.3) READ-ONLY and selects |
| 21 | * exactly one AIENOS partition by its fixed partition type GUID: |
| 22 | * |
| 23 | * AIENOS partition type GUID 38DAAC89-5EAD-4B40-8B1E-3687A7418061 |
| 24 | * (generated once from /dev/urandom on 2026-10-01, version 4 / variant 1; |
| 25 | * on-disk bytes, mixed endian: 89 AC DA 38 AD 5E 40 4B 8B 1E 36 87 A7 41 80 61) |
| 26 | * |
| 27 | * Every kernel disk access after that (rw probe, torn_slot anchor, Store) |
| 28 | * goes through ONE bounds-checked translation layer, ck_part_xlate, behind a |
| 29 | * disk_dev "partition view" (ck_part.dev): partition-relative LBA in, |
| 30 | * absolute LBA out, refused unless the whole transfer lies inside the |
| 31 | * partition. Nothing else in the kernel holds a writable whole-disk handle. |
| 32 | * |
| 33 | * Validation is strict and fails closed (no fallback to the whole disk): |
| 34 | * protective MBR (0x55AA, one 0xEE record), primary header (signature, |
| 35 | * revision 1.0, size, CRC32, MyLBA 1, AlternateLBA = last LBA, usable range), |
| 36 | * primary entry array CRC32, backup header (CRC32, MyLBA = last, AlternateLBA |
| 37 | * 1, same disk GUID / entry geometry / entry CRC) and the backup entry array |
| 38 | * CRC32; every used entry inside the usable range, no two used entries |
| 39 | * overlapping, exactly one AIENOS entry. Any failure: the caller prints |
| 40 | * "disk: no AIENOS partition, refusing writes" and never writes the disk. |
| 41 | * |
| 42 | * Freestanding: no libc, no heap; reads through disk_read only. Shared with |
| 43 | * the host tools (tools/ck_gpt_image.c, tools/ck_store_image.c) and the host |
| 44 | * test dev/tests/disk_part_test.c. */ |
| 45 | #ifndef AIENOS_CK_DISK_PART_H |
| 46 | #define AIENOS_CK_DISK_PART_H |
| 47 | #include <stddef.h> |
| 48 | #include <stdint.h> |
| 49 | #include "disk.h" |
| 50 | |
| 51 | /* TEST-ONLY mutation (scripts/qemu_ck_disk_layout_test.sh): the translation |
| 52 | * layer adds no partition offset and checks only the parent disk's bounds. |
| 53 | * It exists so the DISK_LAYOUT gate can prove it notices a bypass. Never |
| 54 | * with a hardware staging build. */ |
| 55 | #if defined(CK_TEST_DISK_XLATE_BYPASS) && defined(CK_HARDWARE_STAGING) |
| 56 | #error "CK_TEST_DISK_XLATE_BYPASS (TEST-ONLY translation bypass mutation) cannot be combined with CK_HARDWARE_STAGING" |
| 57 | #endif |
| 58 | |
| 59 | extern const uint8_t ck_aienos_part_type[16]; /* on-disk byte order */ |
| 60 | |
| 61 | #define CK_GPT_MAX_ENTRIES 1024u /* NumberOfPartitionEntries upper bound */ |
| 62 | #define CK_GPT_MAX_USED 128u /* used (non-zero type) entries upper bound */ |
| 63 | #define CK_GPT_HDR_MIN 92u /* HeaderSize lower bound (UEFI) */ |
| 64 | |
| 65 | enum { |
| 66 | CK_GPT_OK = 0, |
| 67 | CK_GPT_EIO = -401, /* a read failed */ |
| 68 | CK_GPT_EGEOM = -402, /* block size / disk too small for a GPT */ |
| 69 | CK_GPT_ENOMBR = -403, /* no protective MBR (no 0x55AA or no 0xEE record) */ |
| 70 | CK_GPT_ESIG = -404, /* primary header signature / revision / size */ |
| 71 | CK_GPT_EHDR_CRC = -405, /* primary header CRC32 mismatch */ |
| 72 | CK_GPT_EHDR = -406, /* primary header fields inconsistent */ |
| 73 | CK_GPT_EENT_CRC = -407, /* primary entry array CRC32 mismatch */ |
| 74 | CK_GPT_EENT = -408, /* entry geometry (count/size/overflow) refused */ |
| 75 | CK_GPT_EBACKUP = -409, /* backup header or backup entry array invalid */ |
| 76 | CK_GPT_EOUTSIDE = -410, /* a used entry lies outside the usable range */ |
| 77 | CK_GPT_EOVERLAP = -411, /* two used entries overlap */ |
| 78 | CK_GPT_ENOPART = -412, /* no AIENOS partition */ |
| 79 | CK_GPT_EMULTI = -413, /* more than one AIENOS partition */ |
| 80 | CK_GPT_ETOOMANY = -414, /* more than CK_GPT_MAX_USED used entries */ |
| 81 | }; |
| 82 | |
| 83 | typedef struct { |
| 84 | uint64_t last_lba; /* disk's last LBA */ |
| 85 | uint64_t first_usable, last_usable; |
| 86 | uint32_t entries, entry_size; |
| 87 | uint32_t used; /* used entries */ |
| 88 | uint32_t part_index; /* index of the AIENOS entry */ |
| 89 | /* first_usable .. entry_size are filled whenever both GPT copies are |
| 90 | * valid, also when the answer is CK_GPT_ENOPART / EMULTI / EOVERLAP. */ |
| 91 | } ck_gpt_info; |
| 92 | |
| 93 | typedef struct { |
| 94 | const disk_dev *parent; /* whole namespace: read for the GPT, written only through ck_part_xlate */ |
| 95 | uint64_t first_lba; /* absolute first LBA of the AIENOS partition */ |
| 96 | uint64_t blocks; /* partition length in blocks */ |
| 97 | int valid; /* 1 only after ck_gpt_find_aienos succeeded */ |
| 98 | disk_dev dev; /* the partition view; dev.ctx == this */ |
| 99 | } ck_part; |
| 100 | |
| 101 | /* CRC32 (IEEE 802.3, reflected 0xEDB88320, init/xorout 0xFFFFFFFF), the |
| 102 | * GPT checksum. Chain: crc = ck_crc32(crc, p, n) starting from 0. */ |
| 103 | uint32_t ck_crc32(uint32_t crc, const uint8_t *p, size_t n); |
| 104 | |
| 105 | /* Parse and validate the GPT on d (reads only). On success fills *p (a valid |
| 106 | * partition view over d) and *info; on failure *p is invalid (every I/O |
| 107 | * through p->dev refused) and the CK_GPT_* code is returned. */ |
| 108 | int ck_gpt_find_aienos(const disk_dev *d, ck_part *p, ck_gpt_info *info); |
| 109 | |
| 110 | /* The same strict validation for exactly one partition of an arbitrary type |
| 111 | * (on-disk byte order): CK_GPT_ENOPART / CK_GPT_EMULTI then mean "none" / |
| 112 | * "more than one" of THAT type. Used for the AIENOS image slots partition |
| 113 | * (native/boot/slot_format.h, svc/slot_service.c): a second bounded view |
| 114 | * over the same parent, never a whole-disk handle. */ |
| 115 | int ck_gpt_find_type(const disk_dev *d, const uint8_t type[16], ck_part *p, ck_gpt_info *info); |
| 116 | |
| 117 | /* The single translation layer: partition-relative lba/count -> absolute |
| 118 | * LBA. DISK_ESTATE when p is not a valid partition, DISK_EARG for count 0, |
| 119 | * DISK_ERANGE when any block of the transfer would fall outside the |
| 120 | * partition (or the parent disk), including on overflow. */ |
| 121 | int ck_part_xlate(const ck_part *p, uint64_t lba, uint32_t count, uint64_t *abs); |
| 122 | |
| 123 | const char *ck_gpt_strerror(int rc); |
| 124 | #endif |