interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / dev / disk_part.c

291 lines · 11796 bytes Raw Blame History
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.c -- read-only GPT parser and the single bounds-checked LBA
18 * translation layer of the AIENOS C kernel (see disk_part.h). Freestanding:
19 * no libc, no heap; all disk reads go through disk_read on the parent. */
20#include "disk_part.h"
21
22/* 38DAAC89-5EAD-4B40-8B1E-3687A7418061 in GPT on-disk (mixed endian) order. */
23const uint8_t ck_aienos_part_type[16] = {0x89, 0xac, 0xda, 0x38, 0xad, 0x5e, 0x40, 0x4b,
24 0x8b, 0x1e, 0x36, 0x87, 0xa7, 0x41, 0x80, 0x61};
25
26#define GPT_BUF 4096u
27static uint8_t g_blk[GPT_BUF]; /* one block of the device being read */
28static uint8_t g_hdr[GPT_BUF]; /* primary header copy */
29static uint8_t g_bak[GPT_BUF]; /* backup header copy */
30static uint64_t g_used_first[CK_GPT_MAX_USED];
31static uint64_t g_used_last[CK_GPT_MAX_USED];
32
33static uint32_t get32(const uint8_t *p)
34{
35 return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24;
36}
37static uint64_t get64(const uint8_t *p)
38{
39 return (uint64_t)get32(p) | (uint64_t)get32(p + 4) << 32;
40}
41static int same(const uint8_t *a, const uint8_t *b, size_t n)
42{
43 uint8_t d = 0;
44 for (size_t i = 0; i < n; i++) d |= (uint8_t)(a[i] ^ b[i]);
45 return d == 0;
46}
47static int all_zero(const uint8_t *a, size_t n)
48{
49 uint8_t d = 0;
50 for (size_t i = 0; i < n; i++) d |= a[i];
51 return d == 0;
52}
53static void copy(uint8_t *d, const uint8_t *s, size_t n)
54{
55 for (size_t i = 0; i < n; i++) d[i] = s[i];
56}
57
58uint32_t ck_crc32(uint32_t crc, const uint8_t *p, size_t n)
59{
60 crc = ~crc;
61 for (size_t i = 0; i < n; i++) {
62 crc ^= p[i];
63 for (int k = 0; k < 8; k++) crc = (crc >> 1) ^ (0xedb88320u & (0u - (crc & 1u)));
64 }
65 return ~crc;
66}
67
68/* Header field offsets (UEFI 2.10 table 5-5). */
69enum {
70 H_SIG = 0, H_REV = 8, H_SIZE = 12, H_CRC = 16, H_MY = 24, H_ALT = 32, H_FIRST = 40, H_LAST = 48,
71 H_GUID = 56, H_ENT_LBA = 72, H_NUM = 80, H_ESIZE = 84, H_ECRC = 88
72};
73static const uint8_t gpt_sig[8] = {'E', 'F', 'I', ' ', 'P', 'A', 'R', 'T'};
74
75/* Read block lba of d into g_blk, then check signature, revision, size and
76 * CRC32; on success copy the header into out. */
77static int read_header(const disk_dev *d, uint64_t lba, uint8_t *out, int bad_crc_rc, int bad_rc)
78{
79 if (disk_read(d, lba, 1, g_blk) != DISK_OK) return CK_GPT_EIO;
80 uint32_t hsize = get32(g_blk + H_SIZE);
81 if (!same(g_blk + H_SIG, gpt_sig, 8) || get32(g_blk + H_REV) != 0x00010000u || hsize < CK_GPT_HDR_MIN ||
82 hsize > d->block_size)
83 return bad_rc;
84 uint32_t want = get32(g_blk + H_CRC);
85 static const uint8_t zero4[4] = {0, 0, 0, 0};
86 uint32_t crc = ck_crc32(0, g_blk, H_CRC);
87 crc = ck_crc32(crc, zero4, 4);
88 crc = ck_crc32(crc, g_blk + H_CRC + 4, hsize - H_CRC - 4u);
89 if (crc != want) return bad_crc_rc;
90 copy(out, g_blk, d->block_size);
91 return CK_GPT_OK;
92}
93
94typedef struct {
95 uint32_t num, esize, blocks, bytes;
96} ent_geom;
97
98/* Stream the entry array at lba: CRC32 over num*esize bytes; when scan is
99 * set, also check every used entry and find the one entry of `type`. */
100static int scan_entries(const disk_dev *d, const uint8_t *type, uint64_t lba, const ent_geom *g, int scan, uint32_t *crc_out,
101 uint64_t first_usable, uint64_t last_usable, int *sem_rc, ck_part *p, ck_gpt_info *info)
102{
103 uint32_t crc = 0, left = g->bytes, idx = 0, used = 0, aienos = 0;
104 for (uint32_t b = 0; b < g->blocks; b++) {
105 if (disk_read(d, lba + b, 1, g_blk) != DISK_OK) return CK_GPT_EIO;
106 uint32_t take = left < d->block_size ? left : d->block_size;
107 crc = ck_crc32(crc, g_blk, take);
108 left -= take;
109 for (uint32_t off = 0; scan && off + g->esize <= take; off += g->esize, idx++) {
110 const uint8_t *e = g_blk + off;
111 if (all_zero(e, 16)) continue; /* unused entry */
112 uint64_t f = get64(e + 32), l = get64(e + 40);
113 if (*sem_rc == CK_GPT_OK && (f > l || f < first_usable || l > last_usable)) *sem_rc = CK_GPT_EOUTSIDE;
114 if (used >= CK_GPT_MAX_USED) {
115 if (*sem_rc == CK_GPT_OK) *sem_rc = CK_GPT_ETOOMANY;
116 continue;
117 }
118 g_used_first[used] = f;
119 g_used_last[used] = l;
120 used++;
121 if (same(e, type, 16)) {
122 aienos++;
123 p->first_lba = f;
124 p->blocks = l - f + 1u; /* f <= l checked above before it is trusted */
125 info->part_index = idx;
126 }
127 }
128 }
129 *crc_out = crc;
130 if (scan) {
131 info->used = used;
132 for (uint32_t i = 0; *sem_rc == CK_GPT_OK && i < used; i++)
133 for (uint32_t j = i + 1; j < used; j++)
134 if (g_used_first[i] <= g_used_last[j] && g_used_first[j] <= g_used_last[i]) {
135 *sem_rc = CK_GPT_EOVERLAP;
136 break;
137 }
138 if (*sem_rc == CK_GPT_OK && aienos == 0) *sem_rc = CK_GPT_ENOPART;
139 if (*sem_rc == CK_GPT_OK && aienos > 1) *sem_rc = CK_GPT_EMULTI;
140 }
141 return CK_GPT_OK;
142}
143
144static int part_read(void *ctx, uint64_t lba, uint32_t count, uint8_t *buf)
145{
146 const ck_part *p = ctx;
147 uint64_t abs = 0;
148 int rc = ck_part_xlate(p, lba, count, &abs);
149 return rc ? rc : disk_read(p->parent, abs, count, buf);
150}
151static int part_write(void *ctx, uint64_t lba, uint32_t count, const uint8_t *buf)
152{
153 const ck_part *p = ctx;
154 uint64_t abs = 0;
155 int rc = ck_part_xlate(p, lba, count, &abs);
156 return rc ? rc : disk_write(p->parent, abs, count, buf);
157}
158static int part_flush(void *ctx)
159{
160 const ck_part *p = ctx;
161 if (!p || !p->valid || !p->parent) return DISK_ESTATE;
162 return disk_flush(p->parent);
163}
164
165static void part_invalid(ck_part *p, const disk_dev *d)
166{
167 p->parent = d;
168 p->first_lba = 0;
169 p->blocks = 0;
170 p->valid = 0;
171 p->dev.ctx = p;
172 p->dev.block_size = d ? d->block_size : 0;
173 p->dev.block_count = 0; /* disk_check refuses a zero-block device */
174 p->dev.read = part_read;
175 p->dev.write = part_write;
176 p->dev.flush = part_flush;
177 p->dev.max_blocks_per_io = d ? d->max_blocks_per_io : 0;
178}
179
180int ck_gpt_find_type(const disk_dev *d, const uint8_t type[16], ck_part *p, ck_gpt_info *info)
181{
182 if (!p || !info || !type) return CK_GPT_EGEOM;
183 part_invalid(p, d);
184 for (size_t i = 0; i < sizeof *info; i++) ((uint8_t *)info)[i] = 0;
185 if (disk_check(d) != DISK_OK || d->block_size > GPT_BUF || d->block_count < 8u) return CK_GPT_EGEOM;
186 uint64_t last = d->block_count - 1u;
187 info->last_lba = last;
188
189 /* Protective MBR: boot signature and at least one 0xEE record. */
190 if (disk_read(d, 0, 1, g_blk) != DISK_OK) return CK_GPT_EIO;
191 int pmbr = g_blk[510] == 0x55 && g_blk[511] == 0xaa;
192 int ee = 0;
193 for (int i = 0; i < 4; i++) ee |= g_blk[446 + 16 * i + 4] == 0xee;
194 if (!pmbr || !ee) return CK_GPT_ENOMBR;
195
196 int rc = read_header(d, 1, g_hdr, CK_GPT_EHDR_CRC, CK_GPT_ESIG);
197 if (rc) return rc;
198 ent_geom g;
199 g.num = get32(g_hdr + H_NUM);
200 g.esize = get32(g_hdr + H_ESIZE);
201 if (g.num == 0 || g.num > CK_GPT_MAX_ENTRIES || (g.esize != 128u && g.esize != 256u && g.esize != 512u))
202 return CK_GPT_EENT;
203 g.bytes = g.num * g.esize; /* <= 1024 * 512: no overflow */
204 g.blocks = (g.bytes + d->block_size - 1u) / d->block_size;
205 uint64_t first_u = get64(g_hdr + H_FIRST), last_u = get64(g_hdr + H_LAST), ent_lba = get64(g_hdr + H_ENT_LBA);
206 if (get64(g_hdr + H_MY) != 1u || get64(g_hdr + H_ALT) != last) return CK_GPT_EHDR;
207 /* Every comparison below is against values <= last, so no sum overflows. */
208 if (ent_lba < 2u || ent_lba > last || g.blocks > last - ent_lba) return CK_GPT_EHDR;
209 if (first_u < ent_lba + g.blocks || first_u > last_u || last_u >= last) return CK_GPT_EHDR;
210 /* The backup array must fit between the usable range and the backup header. */
211 if (last - 1u - last_u < g.blocks) return CK_GPT_EHDR;
212
213 int sem = CK_GPT_OK;
214 uint32_t crc = 0;
215 rc = scan_entries(d, type, ent_lba, &g, 1, &crc, first_u, last_u, &sem, p, info);
216 if (rc) return rc;
217 if (crc != get32(g_hdr + H_ECRC)) return CK_GPT_EENT_CRC;
218
219 /* Backup header and backup entry array: both copies must be valid and agree. */
220 rc = read_header(d, last, g_bak, CK_GPT_EBACKUP, CK_GPT_EBACKUP);
221 if (rc) return rc == CK_GPT_EIO ? rc : CK_GPT_EBACKUP;
222 uint64_t bent = get64(g_bak + H_ENT_LBA);
223 if (get64(g_bak + H_MY) != last || get64(g_bak + H_ALT) != 1u || get64(g_bak + H_FIRST) != first_u ||
224 get64(g_bak + H_LAST) != last_u || !same(g_bak + H_GUID, g_hdr + H_GUID, 16) ||
225 get32(g_bak + H_NUM) != g.num || get32(g_bak + H_ESIZE) != g.esize ||
226 get32(g_bak + H_ECRC) != get32(g_hdr + H_ECRC) || bent <= last_u || bent >= last || g.blocks > last - bent)
227 return CK_GPT_EBACKUP;
228 uint32_t bcrc = 0;
229 rc = scan_entries(d, type, bent, &g, 0, &bcrc, first_u, last_u, &sem, p, info);
230 if (rc) return rc;
231 if (bcrc != get32(g_hdr + H_ECRC)) return CK_GPT_EBACKUP;
232
233 info->first_usable = first_u;
234 info->last_usable = last_u;
235 info->entries = g.num;
236 info->entry_size = g.esize;
237 if (sem) {
238 p->first_lba = 0;
239 p->blocks = 0;
240 return sem;
241 }
242 p->valid = 1;
243 p->dev.block_count = p->blocks;
244 return CK_GPT_OK;
245}
246
247int ck_part_xlate(const ck_part *p, uint64_t lba, uint32_t count, uint64_t *abs)
248{
249 if (!p || !p->valid || !p->parent || !abs) return DISK_ESTATE;
250 if (count == 0) return DISK_EARG;
251 uint64_t pc = p->parent->block_count;
252#if defined(CK_TEST_DISK_XLATE_BYPASS)
253 /* TEST-ONLY mutation: no partition offset, parent bounds only. */
254 if (lba >= pc || (uint64_t)count > pc - lba) return DISK_ERANGE;
255 *abs = lba;
256 return DISK_OK;
257#else
258 if (lba >= p->blocks || (uint64_t)count > p->blocks - lba) return DISK_ERANGE; /* GUARD:part-end */
259 uint64_t a = p->first_lba + lba;
260 if (a < p->first_lba || a >= pc || (uint64_t)count > pc - a) return DISK_ERANGE; /* GUARD:part-parent */
261 *abs = a;
262 return DISK_OK;
263#endif
264}
265
266const char *ck_gpt_strerror(int rc)
267{
268 switch (rc) {
269 case CK_GPT_OK: return "ok";
270 case CK_GPT_EIO: return "read error";
271 case CK_GPT_EGEOM: return "disk geometry";
272 case CK_GPT_ENOMBR: return "no protective MBR";
273 case CK_GPT_ESIG: return "no GPT header";
274 case CK_GPT_EHDR_CRC: return "primary header CRC32 mismatch";
275 case CK_GPT_EHDR: return "primary header fields invalid";
276 case CK_GPT_EENT_CRC: return "entry array CRC32 mismatch";
277 case CK_GPT_EENT: return "entry geometry refused";
278 case CK_GPT_EBACKUP: return "backup GPT invalid";
279 case CK_GPT_EOUTSIDE: return "partition outside the usable range";
280 case CK_GPT_EOVERLAP: return "overlapping partitions";
281 case CK_GPT_ENOPART: return "no AIENOS partition type";
282 case CK_GPT_EMULTI: return "more than one AIENOS partition";
283 case CK_GPT_ETOOMANY: return "too many used entries";
284 default: return "error";
285 }
286}
287
288int ck_gpt_find_aienos(const disk_dev *d, ck_part *p, ck_gpt_info *info)
289{
290 return ck_gpt_find_type(d, ck_aienos_part_type, p, info);
291}