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 / x86 / memory_x86.c

363 lines · 17119 bytes Raw Blame History
1/* memory_x86.c -- Allen's memory and notes on a hard disk of whatever
2 * machine AIENOS boots on.
3 *
4 * Which disk, at boot, once:
5 * 1. the SATA disk that already holds Allen's section (a GPT partition of
6 * the AIENOS type): on any machine, the section is what makes a disk
7 * Allen's. Two disks with a section: neither is written;
8 * 2. with no section anywhere, one of ALLEN_PART_MIB is added only on a disk
9 * its owner approved (approved_disks, by IDENTIFY model; Drake approved
10 * his hard disk 2026-10-17), in unused space (dev/gpt_add.c): only then
11 * are the GPT blocks (LBA 1 up to the first usable LBA, and after the
12 * last usable LBA) opened for writing, and closed again right after.
13 * Existing partitions are never touched; no unused space, an invalid GPT
14 * or no GPT: nothing written. A new machine gets its section from the
15 * installer instead, on the disk its owner picks there;
16 * 3. every partition of the chosen disk is listed ("ahci_part:"), a record
17 * of the layout as found;
18 * 4. the boot section beside it (boot_section below): the network loader's
19 * verified kernel installed as \EFI\BOOT\BOOTX64.EFI so the disk starts
20 * AIENOS by itself;
21 * 5. the write range is set to the units of the section Allen uses (the two
22 * memory copies and the note units, svc/allen_memory.h), and the two
23 * units after them for the setup wizard's records (svc/setup_store.h)
24 * when the section has room for them, and nothing
25 * else; the newest valid copy is loaded, then the notes it counts.
26 * NVMe disks are never written (no write path exists). With no usable disk
27 * Allen remembers only until the power goes off. The conversation itself
28 * never goes on the console or the network report from here: only counts
29 * ("3 facts", "2 notes"). */
30#include "../core/ck_internal.h"
31#include "../svc/allen_memory.h"
32#include "../svc/setup_store.h"
33#include "disk_part.h"
34#include "gpt_add.h"
35#include "fat_boot.h"
36#include "aienos_sig.h"
37#include "../../boot/netboot_proto.h"
38#include "../../boot/slot_format.h"
39#include "x86.h"
40
41extern const uint8_t ck_slot_loader_keys[][32];
42extern const unsigned ck_slot_loader_nkeys;
43
44#define ALLEN_PART_MIB 1024u
45#define BOOT_PART_MIB 256u
46
47static const ck_gpt_new boot_spec = {ck_esp_part_type, "AIENOS boot", "aienos.boot.v1"};
48
49/* Disks whose owner approved adding Allen's section to them. */
50static const char *const approved_disks[] = {"APPLE HDD ST1000DM003", 0};
51
52static ck_part part;
53static ck_allen_store store;
54static unsigned disk_index;
55static int lasting;
56static int setup_room; /* the setup wizard's two units are inside the write range */
57static uint64_t hz;
58
59static int same_text(const char *a, const char *b)
60{
61 while (*a && *a == *b)
62 a++, b++;
63 return *a == *b;
64}
65
66static int approved(const char *model)
67{
68 for (const char *const *p = approved_disks; *p; p++)
69 if (same_text(model, *p))
70 return 1;
71 return 0;
72}
73
74static void show_entry(void *ctx, const ck_gpt_entry *e)
75{
76 const disk_dev *d = ctx;
77 const uint8_t *t = e->type;
78 ck_printf("ahci_part: disk=%u entry=%u type=%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x "
79 "first_lba=%llu last_lba=%llu mib=%llu name=\"%s\"%s\n",
80 disk_index, e->index, t[3], t[2], t[1], t[0], t[5], t[4], t[7], t[6], t[8], t[9], t[10], t[11], t[12],
81 t[13], t[14], t[15], (unsigned long long)e->first, (unsigned long long)e->last,
82 (unsigned long long)((e->last - e->first + 1u) * d->block_size >> 20), e->name,
83 memcmp(t, ck_aienos_part_type, 16) ? "" : " (AIENOS)");
84}
85
86static int no_memory(const char *why)
87{
88 ck_printf("allen_memory: %s; Allen remembers only until this computer turns off\n", why);
89 return 0;
90}
91
92/* Adds Allen's section on the approved disk d (no section anywhere yet). */
93static int add_section(const disk_dev *d, const ck_gpt_info *gi)
94{
95 uint64_t per_mib = (1024u * 1024u) / d->block_size;
96 if (ck_x86_ahci_allow(disk_index, 1, gi->first_usable - 1u) ||
97 ck_x86_ahci_allow(disk_index, gi->last_usable + 1u, gi->last_lba)) {
98 ck_x86_ahci_allow_none(disk_index);
99 return no_memory("could not open the GPT blocks for writing");
100 }
101 ck_gpt_added a;
102 int rc = ck_gpt_add_aienos(d, (uint64_t)ALLEN_PART_MIB * per_mib, per_mib, &a);
103 ck_x86_ahci_allow_none(disk_index);
104 if (rc) {
105 ck_printf("allen_memory: AIENOS partition not added: %s%s\n", ck_gpt_add_strerror(rc),
106 rc == CK_GPT_EWRITE || rc == CK_GPT_EVERIFY ? " (the GPT may need a look)" : "; nothing was written");
107 return no_memory("no AIENOS partition");
108 }
109 ck_printf("allen_memory: AIENOS partition added, entry %u, first_lba=%llu last_lba=%llu (%u MiB); the existing "
110 "partitions were not touched\n", a.index, (unsigned long long)a.first, (unsigned long long)a.last,
111 ALLEN_PART_MIB);
112 return 1;
113}
114
115/* The boot section beside Allen's (dev/fat_boot.h), on the same disk: when the
116 * network loader handed over a verified kernel, that kernel is installed as
117 * \EFI\BOOT\BOOTX64.EFI, so the next start needs no stick and no network
118 * (Drake approved a small boot section in free space on this disk,
119 * 2026-10-17). Its manifest signature and image SHA-512 are checked again
120 * here against the kernel's configured owner keys, before any boot-section
121 * write window is opened. An image built without owner keys cannot install.
122 * The network is unauthenticated, so an older owner-signed kernel can be
123 * replayed: a kernel whose generation is below the installed kernel's
124 * policy_generation (the floor that kernel set, slot_format.h) is refused, as
125 * is any install when the installed manifest cannot be read.
126 * A disk that already has another system's EFI System Partition is
127 * never touched. Without a handed-over kernel only what is there is reported.
128 * Writes happen only inside the windows opened here and closed right after:
129 * the GPT blocks while the partition is added, then the partition itself. */
130static uint32_t boot_serial(const ck_part *bp)
131{
132 return (uint32_t)(bp->first_lba ^ bp->first_lba >> 32) * 2654435761u ^ 0x41494e42u;
133}
134
135static void boot_section(const disk_dev *d)
136{
137 int dropped = 0;
138 const struct nb_pass *np = ck_x86_netldr_pass(&dropped);
139 const uint8_t *obj = np && np->object ? (const uint8_t *)(uintptr_t)np->object : 0;
140 ck_part bp;
141 ck_gpt_info gi;
142 ck_fatb_state st;
143 if (dropped)
144 ck_puts("boot_disk: the loader named a kernel outside its own memory; ignored\n");
145 int rc = ck_gpt_find_type(d, ck_esp_part_type, &bp, &gi);
146 if (rc == CK_GPT_OK && ck_gpt_is_ours(d, &boot_spec, gi.part_index) != 1) {
147 ck_puts("boot_disk: this disk already has another system's boot section; not touched\n");
148 return;
149 }
150 if (rc != CK_GPT_OK && rc != CK_GPT_ENOPART) {
151 ck_printf("boot_disk: %s; not touched\n", ck_gpt_add_strerror(rc));
152 return;
153 }
154 if (!obj) {
155 if (rc == CK_GPT_ENOPART)
156 ck_puts("boot_disk: no boot section yet (made when the network loader starts a kernel)\n");
157 else if ((rc = ck_fatb_state_read(&bp.dev, boot_serial(&bp), &st)) != CK_FATB_OK)
158 ck_printf("boot_disk: the boot section could not be read (%s)\n", ck_fatb_strerror(rc));
159 else if (!st.ours)
160 ck_printf("boot_disk: the boot section is not as AIENOS wrote it: %s byte %u is 0x%02x, not 0x%02x\n",
161 st.where, st.at, st.got, st.want);
162 else if (st.slot >= 0)
163 ck_printf("boot_disk: \\EFI\\BOOT\\BOOTX64.EFI on disk %u: %u bytes (slot %c)\n", disk_index,
164 st.image_len, 'A' + st.slot);
165 else
166 ck_puts("boot_disk: the boot section holds no complete kernel\n");
167 return;
168 }
169 const struct ck_slot_manifest *m = (const void *)obj;
170 if (np->object_len <= CK_SLOT_UNIT || memcmp(m->magic, CK_SLOT_MANIFEST_MAGIC, 8) ||
171 m->image_len != np->object_len - CK_SLOT_UNIT) {
172 ck_puts("boot_disk: the loader's object is not a manifest and its image; nothing installed\n");
173 return;
174 }
175 uint64_t t0 = ck_rdtsc();
176 struct ck_slot_eval verified;
177 if (!ck_slot_verify(m, obj + CK_SLOT_UNIT, np->object_len - CK_SLOT_UNIT,
178 ck_slot_loader_keys, ck_slot_loader_nkeys, &verified) ||
179 m->image_len > UINT32_MAX) {
180 ck_puts("boot_disk: owner signature or image verification failed; nothing installed\n");
181 return;
182 }
183 if (rc == CK_GPT_OK) {
184 static uint8_t installed[CK_FATB_MAN_BYTES];
185 const struct ck_slot_manifest *im = (const void *)installed;
186 int mr = ck_fatb_manifest_read(&bp.dev, boot_serial(&bp), installed);
187 if (mr != CK_FATB_OK && mr != CK_FATB_ENOTOURS) {
188 ck_printf("boot_disk: the installed kernel's manifest could not be read (%s); nothing installed\n",
189 ck_fatb_strerror(mr));
190 return;
191 }
192 if (mr == CK_FATB_OK && !memcmp(im->magic, CK_SLOT_MANIFEST_MAGIC, 8) &&
193 verified.generation < im->policy_generation) {
194 ck_printf("boot_disk: generation %llu is below the installed kernel's floor %llu (generation %llu); "
195 "an older kernel is not installed\n", (unsigned long long)verified.generation,
196 (unsigned long long)im->policy_generation, (unsigned long long)im->generation);
197 return;
198 }
199 }
200 if (rc == CK_GPT_ENOPART && d->block_size != 512u) {
201 ck_printf("boot_disk: %u-byte sectors: the boot section handles 512 only; nothing added\n", d->block_size);
202 return;
203 }
204 if (rc == CK_GPT_ENOPART) {
205 uint64_t per_mib = (1024u * 1024u) / d->block_size;
206 ck_gpt_added a;
207 if (ck_x86_ahci_allow(disk_index, 1, gi.first_usable - 1u) ||
208 ck_x86_ahci_allow(disk_index, gi.last_usable + 1u, gi.last_lba))
209 rc = CK_GPT_EWRITE;
210 else
211 rc = ck_gpt_add(d, &boot_spec, (uint64_t)BOOT_PART_MIB * per_mib, per_mib, &a);
212 ck_x86_ahci_allow_none(disk_index);
213 if (rc) {
214 ck_printf("boot_disk: boot section not added: %s%s\n", ck_gpt_add_strerror(rc),
215 rc == CK_GPT_EWRITE || rc == CK_GPT_EVERIFY ? " (the GPT may need a look)" : "; nothing was written");
216 return;
217 }
218 ck_printf("boot_disk: boot section added, entry %u, first_lba=%llu last_lba=%llu (%u MiB, EFI System "
219 "Partition); the existing partitions were not touched\n", a.index, (unsigned long long)a.first,
220 (unsigned long long)a.last, BOOT_PART_MIB);
221 if ((rc = ck_gpt_find_type(d, ck_esp_part_type, &bp, &gi))) {
222 ck_printf("boot_disk: %s after adding it\n", ck_gpt_add_strerror(rc));
223 return;
224 }
225 }
226 uint16_t dd, tt;
227 ck_fatb_dos_time(m->timestamp, &dd, &tt);
228 ck_fatb_result r;
229 if (ck_x86_ahci_allow(disk_index, bp.first_lba, bp.first_lba + bp.blocks - 1u))
230 rc = CK_FATB_EIO;
231 else
232 rc = ck_fatb_install(&bp.dev, boot_serial(&bp), 1, obj, obj + CK_SLOT_UNIT, (uint32_t)m->image_len, dd, tt,
233 &r);
234 ck_x86_ahci_allow_none(disk_index);
235 char commit[13];
236 memcpy(commit, m->commit, 12);
237 commit[12] = 0;
238 uint64_t ms = hz ? (ck_rdtsc() - t0) * 1000 / hz : 0;
239 if (rc)
240 ck_printf("boot_disk: install FAILED: %s; the computer still starts from the stick\n", ck_fatb_strerror(rc));
241 else if (!r.written)
242 ck_printf("boot_disk: this kernel (commit %s, generation %llu) is already \\EFI\\BOOT\\BOOTX64.EFI (slot %c); "
243 "nothing written\n", commit, (unsigned long long)m->generation, 'A' + r.slot);
244 else
245 ck_printf("boot_disk: installed this kernel (commit %s, generation %llu, %llu bytes) as \\EFI\\BOOT\\BOOTX64.EFI, "
246 "slot %c%s, %u blocks in %llu ms, read back exact; the computer can now start without the stick\n",
247 commit, (unsigned long long)m->generation, (unsigned long long)m->image_len, 'A' + r.slot,
248 r.formatted ? ", new FAT32 volume" : "", r.sectors_written, (unsigned long long)ms);
249}
250
251int ck_x86_memory_open(ck_allen_memory *mem, ck_allen_notes *notes, uint64_t tsc_hz)
252{
253 hz = tsc_hz;
254 const disk_dev *home = 0, *ok = 0;
255 unsigned homes = 0, oks = 0, home_i = 0, ok_i = 0;
256 ck_gpt_info gi, ok_gi;
257 for (unsigned i = 0;; i++) {
258 const char *model = 0;
259 const disk_dev *x = ck_x86_ahci_dev(i, &model);
260 if (!x)
261 break;
262 int rc = ck_gpt_find_aienos(x, &part, &gi);
263 if (rc == CK_GPT_OK) {
264 home = x;
265 home_i = i;
266 homes++;
267 } else if (rc == CK_GPT_ENOPART && approved(model)) {
268 ok = x;
269 ok_i = i;
270 ok_gi = gi;
271 oks++;
272 }
273 }
274 if (homes > 1)
275 return no_memory("two disks hold Allen's section, writing to neither");
276 const disk_dev *d = homes ? home : ok;
277 disk_index = homes ? home_i : ok_i;
278 if (!homes && oks != 1)
279 return no_memory(oks ? "two approved disks and no section yet, writing to neither"
280 : "no disk holds Allen's section and none is approved for one");
281 ck_printf("allen_memory: %s disk %u (%llu blocks of %u bytes)\n",
282 homes ? "Allen's section is on" : "no section yet; the approved disk is", disk_index,
283 (unsigned long long)d->block_count, d->block_size);
284 int n = ck_gpt_list(d, show_entry, (void *)d);
285 if (n < 0)
286 return no_memory(ck_gpt_add_strerror(n)); /* no GPT or an invalid one: never written */
287 ck_printf("allen_memory: the disk has %d partition%s\n", n, n == 1 ? "" : "s");
288 if (!homes && !add_section(d, &ok_gi))
289 return 0;
290 int rc = ck_gpt_find_aienos(d, &part, &gi);
291 if (rc)
292 return no_memory(ck_gpt_add_strerror(rc));
293 if (homes)
294 ck_printf("allen_memory: AIENOS partition found, entry %u, first_lba=%llu last_lba=%llu\n", gi.part_index,
295 (unsigned long long)part.first_lba, (unsigned long long)(part.first_lba + part.blocks - 1u));
296
297 boot_section(d);
298
299 uint64_t units = (uint64_t)CK_ALLEN_DISK_UNITS * CK_ALLEN_MEM_UNIT / d->block_size;
300 uint64_t with_setup = (uint64_t)(CK_SETUP_FIRST + CK_SETUP_UNITS) * CK_ALLEN_MEM_UNIT / d->block_size;
301 setup_room = part.blocks >= with_setup;
302 if (setup_room)
303 units = with_setup;
304 if (part.blocks < units || ck_x86_ahci_allow(disk_index, part.first_lba, part.first_lba + units - 1u))
305 return no_memory("could not open Allen's section for writing");
306 rc = ck_allen_store_load(&store, &part.dev, mem);
307 if (rc < 0) {
308 ck_x86_ahci_allow_none(disk_index);
309 return no_memory("reading the memory copies failed");
310 }
311 if (rc == CK_ALLEN_MEM_EMPTY)
312 ck_puts("allen_memory: empty (a first start); Allen starts fresh\n");
313 else
314 ck_printf("allen_memory: loaded generation %llu (%u of 2 copies valid): %u fact%s, name %s, %u earlier start%s\n",
315 (unsigned long long)store.gen, store.valid, mem->nfacts, mem->nfacts == 1 ? "" : "s",
316 mem->name[0] ? "known" : "not told", mem->starts, mem->starts == 1 ? "" : "s");
317 uint32_t damaged = 0;
318 if (ck_allen_notes_load(&part.dev, mem->nnotes, notes, &damaged) != CK_ALLEN_MEM_OK) {
319 ck_x86_ahci_allow_none(disk_index);
320 return no_memory("reading the notes failed");
321 }
322 ck_printf("allen_memory: %u note%s loaded (%u damaged)\n", mem->nnotes, mem->nnotes == 1 ? "" : "s", damaged);
323 lasting = 1;
324 return 1;
325}
326
327static int failed(const char *what, int rc)
328{
329 lasting = 0;
330 ck_x86_ahci_allow_none(disk_index);
331 ck_printf("allen_memory: %s FAILED (rc=%d); nothing more is written this start\n", what, rc);
332 return -1;
333}
334
335const disk_dev *ck_x86_memory_part(void) { return lasting && setup_room ? &part.dev : 0; }
336
337int ck_x86_memory_save(const ck_allen_memory *mem)
338{
339 if (!lasting)
340 return -1;
341 uint64_t t0 = ck_rdtsc();
342 int rc = ck_allen_store_save(&store, mem);
343 if (rc)
344 return failed("save", rc);
345 ck_printf("allen_memory: saved generation %llu to copy %c (%u fact%s, %u note%s) in %llu ms\n",
346 (unsigned long long)store.gen, store.newest ? 'B' : 'A', mem->nfacts, mem->nfacts == 1 ? "" : "s",
347 mem->nnotes, mem->nnotes == 1 ? "" : "s",
348 hz ? (unsigned long long)((ck_rdtsc() - t0) * 1000 / hz) : 0ull);
349 return 0;
350}
351
352int ck_x86_notes_write(const ck_allen_notes *notes, uint32_t index)
353{
354 if (!lasting)
355 return -1;
356 uint64_t t0 = ck_rdtsc();
357 int rc = ck_allen_note_write(&part.dev, notes, index);
358 if (rc)
359 return failed("note write", rc);
360 ck_printf("allen_memory: note %u written in %llu ms\n", index,
361 hz ? (unsigned long long)((ck_rdtsc() - t0) * 1000 / hz) : 0ull);
362 return 0;
363}