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 / boot / diag_main.c

413 lines · 15114 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/* diag_main.c -- AIENOS-PI5-BOOT-0 / PI5-ACPI-0: a minimal AArch64 UEFI diagnostic.
18 *
19 * Prints what the firmware tells an application (vendor, revisions, EL, memory
20 * map summary, every ConfigurationTable GUID) to ConOut and to a buffer, copies
21 * every ACPI table the XSDT (and the FADT DSDT pointer) names, byte for byte, to
22 * \EFI\AIENOS\ACPI\<SIG>[n].BIN, writes the buffer to \EFI\AIENOS\PI5BOOT0.TXT
23 * on the volume the image was loaded from, waits ~5 s so a person can read the
24 * screen, and cold-resets the machine through ResetSystem (on the Pi this ends a
25 * one-shot tryboot run: the next boot is the normal OS). A 180 s firmware watchdog
26 * covers a hang before that. It links no kernel and never calls ExitBootServices.
27 * Built as PI5DIAG.EFI (make -C native/kernel pi5-diag); on the card it is
28 * EFI/BOOT/BOOTAA64.EFI. Test: scripts/qemu_ck_pi5_diag_test.sh.
29 *
30 * AIENOS-IMAC-BOOT-0: the same source built for x86_64 (make -C native/kernel
31 * imac-diag -> IMACDIAG.EFI, on the stick EFI/BOOT/BOOTX64.EFI) for the first
32 * boot on a 2017 iMac. diag_x86.c adds CPUID, SMBIOS identity, PCI devices,
33 * screen modes and disks; the report is \EFI\AIENOS\IMACBOOT.TXT; it holds the
34 * screen ~30 s and then powers the machine off (a Mac has no one-shot boot to
35 * fall back from). Test: scripts/qemu_ck_imac_diag_test.sh. */
36#include <stdarg.h>
37#include <string.h>
38#include "efi.h"
39#include "diag.h"
40#include "../kernel/core/fmt.h"
41
42#if defined(__x86_64__)
43#define DIAG_TITLE "AIENOS-IMAC-BOOT-0"
44#define DIAG_MILESTONE "AIENOS-IMAC-BOOT-0"
45#define DIAG_FILE "IMACBOOT.TXT"
46#define DIAG_HOLD_S 30ull
47#define DIAG_RESET EfiResetShutdown
48#define DIAG_RESET_NAME "shutdown"
49#define REPORT_BYTES 65536
50#else
51#define DIAG_TITLE "AIENOS-PI5-BOOT-0"
52#define DIAG_MILESTONE "AIENOS-PI5-ACPI-0"
53#define DIAG_FILE "PI5BOOT0.TXT"
54#define DIAG_HOLD_S 5ull
55#define DIAG_RESET EfiResetCold
56#define DIAG_RESET_NAME "cold"
57#define REPORT_BYTES 16384
58#endif
59#define MAP_BYTES (64u << 10)
60#define LINE_BYTES 256
61
62static EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout;
63static char report[REPORT_BYTES];
64static size_t report_len;
65static uint64_t map_buf[MAP_BYTES / 8];
66
67struct line {
68 char b[LINE_BYTES];
69 size_t n;
70};
71
72static void line_emit(void *ctx, char c)
73{
74 struct line *l = ctx;
75 if (l->n < sizeof l->b - 1)
76 l->b[l->n++] = c;
77}
78
79/* Format one line, print it on ConOut (CRLF) and append it (LF) to the report. */
80static void vsay(const char *fmt, va_list ap)
81{
82 struct line l;
83 CHAR16 w[2 * LINE_BYTES + 1];
84 size_t k = 0;
85 l.n = 0;
86 ck_vformat(line_emit, &l, fmt, ap);
87 for (size_t i = 0; i < l.n; i++) {
88 if (l.b[i] == '\n')
89 w[k++] = '\r';
90 w[k++] = (uint8_t)l.b[i];
91 if (report_len < sizeof report)
92 report[report_len++] = l.b[i];
93 }
94 w[k] = 0;
95 if (conout)
96 conout->OutputString(conout, w);
97}
98
99static void say(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
100static void say(const char *fmt, ...)
101{
102 va_list ap;
103 va_start(ap, fmt);
104 vsay(fmt, ap);
105 va_end(ap);
106}
107
108void diag_say(const char *fmt, ...)
109{
110 va_list ap;
111 va_start(ap, fmt);
112 vsay(fmt, ap);
113 va_end(ap);
114}
115
116
117/* Format into out (always NUL-terminated, truncated to cap - 1 bytes). */
118static void fmt_buf(char *out, size_t cap, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
119static void fmt_buf(char *out, size_t cap, const char *fmt, ...)
120{
121 struct line l;
122 va_list ap;
123 l.n = 0;
124 va_start(ap, fmt);
125 ck_vformat(line_emit, &l, fmt, ap);
126 va_end(ap);
127 size_t n = l.n < cap - 1 ? l.n : cap - 1;
128 memcpy(out, l.b, n);
129 out[n] = 0;
130}
131struct named_guid {
132 EFI_GUID g;
133 const char *name;
134};
135
136/* Values from edk2 MdePkg/Include/Guid/{Acpi.h,SmBios.h,Fdt.h}. */
137static const struct named_guid known[] = {
138 {{0xeb9d2d30, 0x2d88, 0x11d3, {0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}, "ACPI 1.0"},
139 {{0x8868e871, 0xe4f1, 0x11d3, {0xbc, 0x22, 0x00, 0x80, 0xc7, 0x3c, 0x88, 0x81}}, "ACPI 2.0"},
140 {{0xeb9d2d31, 0x2d88, 0x11d3, {0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}, "SMBIOS"},
141 {{0xf2fd1544, 0x9794, 0x4a2c, {0x99, 0x2e, 0xe5, 0xbb, 0xcf, 0x20, 0xe3, 0x94}}, "SMBIOS3"},
142 {{0xb1b621d5, 0xf19c, 0x41a5, {0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0}}, "Device Tree"},
143};
144
145static const char *guid_name(const EFI_GUID *g)
146{
147 for (size_t i = 0; i < sizeof known / sizeof known[0]; i++)
148 if (memcmp(g, &known[i].g, sizeof *g) == 0)
149 return known[i].name;
150 return "unknown";
151}
152
153static void narrow(char *out, size_t cap, const CHAR16 *v)
154{
155 size_t i = 0;
156 for (; v && v[i] && i < cap - 1; i++)
157 out[i] = v[i] < 0x80 ? (char)v[i] : '?';
158 out[i] = 0;
159}
160
161static CHAR16 *wide(CHAR16 *out, const char *s)
162{
163 size_t i = 0;
164 for (; s[i]; i++)
165 out[i] = (uint8_t)s[i];
166 out[i] = 0;
167 return out;
168}
169
170static void memory_summary(EFI_BOOT_SERVICES *bs)
171{
172 uint64_t size = sizeof map_buf, key = 0, dsize = 0, conv = 0;
173 uint32_t dver = 0;
174 EFI_STATUS s = bs->GetMemoryMap(&size, (EFI_MEMORY_DESCRIPTOR *)map_buf, &key, &dsize, &dver);
175 if (s != EFI_SUCCESS || dsize < sizeof(EFI_MEMORY_DESCRIPTOR)) {
176 say("memory_map: GetMemoryMap failed status=0x%llx\n", (unsigned long long)s);
177 return;
178 }
179 uint64_t n = size / dsize;
180 for (uint64_t i = 0; i < n; i++) {
181 const EFI_MEMORY_DESCRIPTOR *d = (const void *)((const uint8_t *)map_buf + i * dsize);
182 if (d->Type == EfiConventionalMemory)
183 conv += d->NumberOfPages;
184 }
185 say("memory_map: entries=%llu conventional_mib=%llu map_key=0x%llx\n", (unsigned long long)n,
186 (unsigned long long)(conv >> 8), (unsigned long long)key); /* 256 pages of 4 KiB per MiB */
187}
188
189/* Open (creating) a directory under the root of the volume the image came from. */
190static EFI_STATUS open_dir(EFI_HANDLE image, EFI_SYSTEM_TABLE *st, const char *path, EFI_FILE_PROTOCOL **dir)
191{
192 static const EFI_GUID li_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID_INIT;
193 static const EFI_GUID fs_guid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID_INIT;
194 EFI_BOOT_SERVICES *bs = st->BootServices;
195 EFI_LOADED_IMAGE_PROTOCOL *li = 0;
196 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fs = 0;
197 EFI_FILE_PROTOCOL *root = 0;
198 CHAR16 w[48];
199 EFI_STATUS s = bs->HandleProtocol(image, &li_guid, (void **)&li);
200 if (s != EFI_SUCCESS)
201 return s;
202 s = bs->HandleProtocol(li->DeviceHandle, &fs_guid, (void **)&fs);
203 if (s != EFI_SUCCESS)
204 return s;
205 s = fs->OpenVolume(fs, &root);
206 if (s != EFI_SUCCESS)
207 return s;
208 /* Opening an existing directory with CREATE returns it. */
209 s = root->Open(root, dir, wide(w, path), EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
210 EFI_FILE_DIRECTORY);
211 root->Close(root);
212 return s;
213}
214
215/* Replace dir\name with exactly len bytes of buf (a stale file is deleted first). */
216static EFI_STATUS write_file(EFI_FILE_PROTOCOL *dir, const char *name, const void *buf, uint64_t len)
217{
218 const uint64_t rw = EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE;
219 EFI_FILE_PROTOCOL *f = 0;
220 CHAR16 w[32];
221 if (dir->Open(dir, &f, wide(w, name), rw, 0) == EFI_SUCCESS)
222 f->Delete(f);
223 f = 0;
224 EFI_STATUS s = dir->Open(dir, &f, wide(w, name), rw | EFI_FILE_MODE_CREATE, 0);
225 if (s != EFI_SUCCESS)
226 return s;
227 uint64_t n = len;
228 s = f->Write(f, &n, (void *)buf);
229 if (s == EFI_SUCCESS && n != len)
230 s = EFI_DEVICE_ERROR;
231 if (s == EFI_SUCCESS)
232 s = f->Flush(f);
233 f->Close(f);
234 return s;
235}
236
237static EFI_STATUS write_report(EFI_HANDLE image, EFI_SYSTEM_TABLE *st)
238{
239 EFI_FILE_PROTOCOL *dir = 0;
240 EFI_STATUS s = open_dir(image, st, "\\EFI\\AIENOS", &dir);
241 if (s != EFI_SUCCESS)
242 return s;
243 s = write_file(dir, DIAG_FILE, report, report_len);
244 dir->Close(dir);
245 return s;
246}
247
248/* ---- ACPI table copy (ACPI 6.5: 5.2.5.3 RSDP, 5.2.6 header, 5.2.8 XSDT, 5.2.9 FADT) ---- */
249#define ACPI_MAX_TABLES 64
250#define ACPI_MAX_LEN (1u << 20)
251
252static uint32_t rd32(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; }
253static uint64_t rd64(const uint8_t *p) { return (uint64_t)rd32(p) | (uint64_t)rd32(p + 4) << 32; }
254
255static int acpi_sum_ok(const uint8_t *p, uint32_t len)
256{
257 uint8_t s = 0;
258 for (uint32_t i = 0; i < len; i++)
259 s += p[i];
260 return s == 0;
261}
262
263struct acpi_dump {
264 EFI_FILE_PROTOCOL *dir;
265 char seen[ACPI_MAX_TABLES][4];
266 unsigned nseen, copied, failed;
267};
268
269/* Copy one table to <SIG>.BIN, or <SIG><k>.BIN for the k-th repeat of a signature. */
270static void acpi_copy(struct acpi_dump *d, uint64_t addr, const char *via)
271{
272 const uint8_t *t = (const uint8_t *)(uintptr_t)addr;
273 char sig[5], name[16];
274 unsigned rep = 0;
275 if (!addr) {
276 say("acpi_table: NULL pointer via %s\n", via);
277 d->failed++;
278 return;
279 }
280 uint32_t len = rd32(t + 4);
281 for (int i = 0; i < 4; i++) {
282 char c = (char)t[i];
283 sig[i] = ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) ? c : '_';
284 }
285 sig[4] = 0;
286 if (len < 36 || len > ACPI_MAX_LEN) {
287 say("acpi_table: %s at 0x%llx bad length %u via %s\n", sig, (unsigned long long)addr, len, via);
288 d->failed++;
289 return;
290 }
291 for (unsigned i = 0; i < d->nseen; i++)
292 if (memcmp(d->seen[i], sig, 4) == 0)
293 rep++;
294 if (d->nseen < ACPI_MAX_TABLES)
295 memcpy(d->seen[d->nseen++], sig, 4);
296 if (rep)
297 fmt_buf(name, sizeof name, "%s%u.BIN", sig, rep);
298 else
299 fmt_buf(name, sizeof name, "%s.BIN", sig);
300 EFI_STATUS s = d->dir ? write_file(d->dir, name, t, len) : EFI_NOT_FOUND;
301 say("acpi_table: %s rev=%u len=%u at 0x%llx checksum=%s via %s file=%s %s\n", sig, t[8], len,
302 (unsigned long long)addr, acpi_sum_ok(t, len) ? "ok" : "BAD", via, name,
303 s == EFI_SUCCESS ? "written" : "WRITE_FAILED");
304 if (s == EFI_SUCCESS)
305 d->copied++;
306 else
307 d->failed++;
308}
309
310static void acpi_dump_all(EFI_HANDLE image, EFI_SYSTEM_TABLE *st)
311{
312 static const EFI_GUID acpi20 = EFI_ACPI_20_TABLE_GUID_INIT;
313 static struct acpi_dump d;
314 const uint8_t *rsdp = 0;
315 for (uint64_t i = 0; i < st->NumberOfTableEntries; i++)
316 if (memcmp(&st->ConfigurationTable[i].VendorGuid, &acpi20, sizeof acpi20) == 0)
317 rsdp = st->ConfigurationTable[i].VendorTable;
318 if (!rsdp || memcmp(rsdp, "RSD PTR ", 8) != 0 || rsdp[15] < 2) {
319 say("acpi: no ACPI 2.0 RSDP\n");
320 return;
321 }
322 uint64_t xsdt_addr = rd64(rsdp + 24);
323 say("acpi_rsdp: rev=%u oem=%c%c%c%c%c%c xsdt=0x%llx\n", rsdp[15], rsdp[9], rsdp[10], rsdp[11], rsdp[12],
324 rsdp[13], rsdp[14], (unsigned long long)xsdt_addr);
325 /* FAT creates one level per Open: make \EFI\AIENOS before \EFI\AIENOS\ACPI. */
326 EFI_FILE_PROTOCOL *parent = 0;
327 EFI_STATUS s = open_dir(image, st, "\\EFI\\AIENOS", &parent);
328 if (s == EFI_SUCCESS) {
329 parent->Close(parent);
330 s = open_dir(image, st, "\\EFI\\AIENOS\\ACPI", &d.dir);
331 }
332 if (s != EFI_SUCCESS) {
333 say("acpi: open \\EFI\\AIENOS\\ACPI FAILED status=0x%llx\n", (unsigned long long)s);
334 d.dir = 0;
335 }
336 acpi_copy(&d, xsdt_addr, "RSDP");
337 const uint8_t *x = (const uint8_t *)(uintptr_t)xsdt_addr;
338 uint32_t xlen = x ? rd32(x + 4) : 0;
339 if (!x || memcmp(x, "XSDT", 4) != 0 || xlen < 36 || xlen > ACPI_MAX_LEN) {
340 say("acpi: XSDT missing or malformed\n");
341 } else {
342 uint32_t n = (xlen - 36) / 8;
343 for (uint32_t i = 0; i < n && i < ACPI_MAX_TABLES; i++) {
344 uint64_t a = rd64(x + 36 + 8 * i);
345 acpi_copy(&d, a, "XSDT");
346 const uint8_t *t = (const uint8_t *)(uintptr_t)a;
347 if (a && memcmp(t, "FACP", 4) == 0) {
348 uint32_t flen = rd32(t + 4);
349 uint64_t dsdt = flen >= 148 ? rd64(t + 140) : 0; /* X_DSDT */
350 if (!dsdt && flen >= 44)
351 dsdt = rd32(t + 40); /* DSDT */
352 acpi_copy(&d, dsdt, "FADT");
353 }
354 }
355 }
356 say("acpi_dump: copied=%u failed=%u\n", d.copied, d.failed);
357 if (d.dir)
358 d.dir->Close(d.dir);
359}
360
361EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st)
362{
363 EFI_BOOT_SERVICES *bs = st->BootServices;
364 char vendor[80];
365 conout = st->ConOut;
366 /* 180 s: the firmware resets the machine if this app hangs (UEFI 2.x 7.5). */
367 if (bs->SetWatchdogTimer)
368 bs->SetWatchdogTimer(180, 0x10000, 0, 0);
369#if defined(__x86_64__)
370 diag_x86_console(st);
371#endif
372
373 say(DIAG_TITLE "\n");
374 say("milestone: " DIAG_MILESTONE "\n");
375 narrow(vendor, sizeof vendor, st->FirmwareVendor);
376 say("firmware_vendor: %s\n", vendor);
377 say("firmware_revision: 0x%x\n", st->FirmwareRevision);
378 say("uefi_spec_revision: %u.%u\n", st->Hdr.Revision >> 16, st->Hdr.Revision & 0xffff);
379#if defined(__x86_64__)
380 diag_x86_cpu();
381#else
382 uint64_t el;
383 __asm__ volatile("mrs %0, CurrentEL" : "=r"(el));
384 say("current_el: EL%u\n", (unsigned)((el >> 2) & 3));
385#endif
386 memory_summary(bs);
387 say("config_tables: %llu\n", (unsigned long long)st->NumberOfTableEntries);
388 for (uint64_t i = 0; i < st->NumberOfTableEntries; i++) {
389 const EFI_GUID *g = &st->ConfigurationTable[i].VendorGuid;
390 say("table %llu: %08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x %s at 0x%llx\n",
391 (unsigned long long)i, g->d1, g->d2, g->d3, g->d4[0], g->d4[1], g->d4[2], g->d4[3],
392 g->d4[4], g->d4[5], g->d4[6], g->d4[7], guid_name(g),
393 (unsigned long long)(uintptr_t)st->ConfigurationTable[i].VendorTable);
394 }
395 acpi_dump_all(image, st);
396#if defined(__x86_64__)
397 diag_x86_devices(st);
398#endif
399
400 say(DIAG_TITLE " DONE\n"); /* last line of the file; the write result below is console only */
401 EFI_STATUS s = write_report(image, st);
402 if (s == EFI_SUCCESS)
403 say("report_file: written \\EFI\\AIENOS\\" DIAG_FILE " (%llu bytes)\n", (unsigned long long)report_len);
404 else
405 say("report_file: write FAILED status=0x%llx\n", (unsigned long long)s);
406 bs->Stall(DIAG_HOLD_S * 1000 * 1000);
407 EFI_RUNTIME_SERVICES *rt = st->RuntimeServices;
408 say("reset: " DIAG_RESET_NAME "\n");
409 if (rt && rt->ResetSystem)
410 rt->ResetSystem(DIAG_RESET, EFI_SUCCESS, 0, 0);
411 say("reset: ResetSystem returned\n");
412 return EFI_SUCCESS;
413}