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 / fwcfg_x86.c

103 lines · 3741 bytes Raw Blame History
1/* fwcfg_x86.c -- one named file from QEMU's firmware configuration device
2 * (fw_cfg), read-only: how a launcher (aienos-run, or a QEMU test) hands the
3 * kernel text about the computer around it (svc/setup_inv.h).
4 *
5 * Nothing is assumed: the device is used only when the ACPI DSDT names it
6 * (_HID "QEMU0002") with an I/O port descriptor, and its signature reads
7 * "QEMU". Registers per QEMU docs/specs/fw_cfg.rst: on x86 the selector is
8 * I/O port 0x510 (16 bit, little endian; a selector below 0x4000 only reads)
9 * and the data port is the next one (8 bit, offset advances by one per
10 * read); key 0x0000 is the signature, key 0x0019 the file directory: a
11 * big-endian count, then 64-byte entries {size BE32, select BE16, reserved,
12 * name[56]}. On the iMac there is no such device and nothing is touched. */
13#include "x86.h"
14#include "../core/acpi.h"
15
16static uint16_t sel_port;
17
18static 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; }
19static uint32_t be32(const uint8_t *b) { return (uint32_t)b[0] << 24 | (uint32_t)b[1] << 16 | (uint32_t)b[2] << 8 | b[3]; }
20
21/* The DSDT's QEMU0002 device and the base of its I/O port descriptor
22 * (small resource 0x47: info, min lo/hi, max lo/hi, align, length); 0 if none. */
23static uint16_t port_from_dsdt(uint64_t rsdp)
24{
25 const uint8_t *fadt = rsdp ? ck_acpi_lookup(rsdp, "FACP") : 0;
26 if (!fadt)
27 return 0;
28 uint32_t flen = rd32(fadt + 4);
29 uint64_t dsdt = 0;
30 if (flen >= 148)
31 dsdt = (uint64_t)rd32(fadt + 140) | (uint64_t)rd32(fadt + 144) << 32;
32 if (!dsdt && flen >= 44)
33 dsdt = rd32(fadt + 40);
34 if (!dsdt)
35 return 0;
36 const uint8_t *d = (const uint8_t *)(uintptr_t)dsdt;
37 if (d[0] != 'D' || d[1] != 'S' || d[2] != 'D' || d[3] != 'T')
38 return 0;
39 uint32_t len = rd32(d + 4);
40 static const char hid[] = "QEMU0002";
41 for (uint32_t i = 36; i + 8 <= len; i++) {
42 unsigned k = 0;
43 while (k < 8 && d[i + k] == (uint8_t)hid[k])
44 k++;
45 if (k < 8)
46 continue;
47 for (uint32_t j = i + 8; j + 8 <= len && j < i + 8 + 96; j++)
48 if (d[j] == 0x47 && d[j + 7] >= 2) {
49 uint16_t lo = (uint16_t)(d[j + 2] | d[j + 3] << 8), hi = (uint16_t)(d[j + 4] | d[j + 5] << 8);
50 if (lo && lo == hi)
51 return lo;
52 }
53 }
54 return 0;
55}
56
57static void fw_select(uint16_t key) { ck_outw(sel_port, key); }
58static void fw_read(uint8_t *out, uint32_t n)
59{
60 for (uint32_t i = 0; i < n; i++)
61 out[i] = ck_inb((uint16_t)(sel_port + 1));
62}
63
64int ck_x86_fwcfg_file(uint64_t rsdp, const char *name, char *buf, unsigned cap, unsigned *len)
65{
66 *len = 0;
67 if (!sel_port)
68 sel_port = port_from_dsdt(rsdp);
69 if (!sel_port)
70 return -1;
71 uint8_t b[64];
72 fw_select(0x0000);
73 fw_read(b, 4);
74 if (b[0] != 'Q' || b[1] != 'E' || b[2] != 'M' || b[3] != 'U') {
75 sel_port = 0;
76 return -1;
77 }
78 fw_select(0x0019);
79 fw_read(b, 4);
80 uint32_t n = be32(b);
81 unsigned nl = 0;
82 while (name[nl])
83 nl++;
84 for (uint32_t i = 0; i < n && i < 4096; i++) {
85 fw_read(b, 64);
86 b[63] = 0;
87 unsigned k = 0;
88 while (k < nl && b[8 + k] == (uint8_t)name[k])
89 k++;
90 if (k < nl || b[8 + nl])
91 continue;
92 uint32_t size = be32(b);
93 uint16_t key = (uint16_t)(b[4] << 8 | b[5]);
94 if (key >= 0x4000)
95 return -1; /* only ever a read */
96 fw_select(key);
97 uint32_t take = size < cap ? size : cap;
98 fw_read((uint8_t *)buf, take);
99 *len = take;
100 return size > cap ? 1 : 0;
101 }
102 return -1;
103}