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 / core / pi5.c

271 lines · 12608 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/* pi5.c -- Raspberry Pi 5 board glue (CK_PI5 build only).
18 *
19 * ck_pi5_start arm the PM watchdog (dev/pi5_wdt.c), bring up the
20 * microSD controller with native/disk/disk_sdhci.c
21 * ck_pi5_stage pet the watchdog at every boot stage
22 * ck_pi5_before_reset write the RAM console log (core/klog.c) into the
23 * card's log slot (core/klog_slot.h), then reset
24 *
25 * Limit: the card log is written only on the orderly reset path. A watchdog
26 * expiry (a hang) resets the board without running ck_pi5_before_reset, so
27 * that boot's RAM log is lost and the slot keeps the previous run's header;
28 * pi5-run.sh's per-run nonce makes such a run read as "no report", never as
29 * a pass. Periodic flushes at stage boundaries are not implemented.
30 *
31 * The SD host address is fixed for this board: the firmware's ACPI DSDT
32 * (\_SB.SDC0, dumped by AIENOS-PI5-ACPI-0, 2026-10-13) computes its _CRS in a
33 * method the kernel's static scanner does not run, and gives the same range
34 * as Linux bcm2712.dtsi sdio1 (mmc@fff000, 0x260 bytes, soc window
35 * 0x0 -> 0x10_0000_0000). Its clock is clk_emmc2, a fixed 200 MHz
36 * (bcm2712.dtsi); the CAPABILITIES base clock is printed beside it. The
37 * driver is PIO only: no DMA, so the missing SMMU does not matter here. */
38#ifndef CK_PI5
39#error "pi5.c is Raspberry Pi 5 only (CK_PI5)"
40#endif
41#include "ck_internal.h"
42#include "klog.h"
43#include "klog_boot.h"
44#include "klog_slot.h"
45#include "../dev/pi5_wdt.h"
46#include "disk.h"
47#include "disk_sdhci.h"
48#include "pi5_marker.h"
49#ifdef CK_PI5_UNLOCK_CARD
50#include "unlock_req.h"
51#endif
52#include "../dev/disk_part.h"
53
54#define PI5_SD_PHYS 0x1000fff000ull
55#define PI5_SD_CLOCK_HZ 200000000u
56
57static volatile uint8_t *sd_regs;
58static sdhci_card sd;
59static int sd_up;
60static ck_part part; /* the AIENOS partition view, once found (the marker uses its reserved tail) */
61static ck_part store_part; /* the same partition minus the marker region: the sealed Store's view */
62
63static uint32_t sd_r32(void *c, uint32_t o) { (void)c; return *(volatile uint32_t *)(sd_regs + o); }
64static void sd_w32(void *c, uint32_t o, uint32_t v) { (void)c; *(volatile uint32_t *)(sd_regs + o) = v; }
65static void sd_delay(void *c, uint32_t us) { (void)c; ck_udelay(us); }
66static const sdhci_ops sd_ops = {0, sd_r32, sd_w32, sd_delay};
67
68void ck_pi5_start(void)
69{
70 if (ck_pi5_wdt_arm() == 0)
71 ck_printf("pi5_watchdog: armed %u s (PM block 0x%llx), petted at every stage\n", (unsigned)CK_PI5_WDT_S,
72 (unsigned long long)CK_PI5_PM_PHYS);
73 else
74 ck_printf("pi5_watchdog: FAIL map 0x%llx; a hang now needs a power cycle\n",
75 (unsigned long long)CK_PI5_PM_PHYS);
76 sd_regs = ck_mmio_map(PI5_SD_PHYS, 0x1000);
77 if (!sd_regs) {
78 ck_printf("pi5_sd: FAIL map 0x%llx\n", (unsigned long long)PI5_SD_PHYS);
79 return;
80 }
81 uint32_t caps = sd_r32(0, 0x40), ver = sd_r32(0, 0xfc) >> 16;
82 ck_printf("pi5_sd: host 0x%llx version=0x%x caps=0x%08x caps_base_mhz=%u using %u MHz (clk_emmc2)\n",
83 (unsigned long long)PI5_SD_PHYS, ver, caps, (caps >> 8) & 0xff, PI5_SD_CLOCK_HZ / 1000000u);
84 sdhci_cfg cfg = {PI5_SD_CLOCK_HZ, 0, 1, 0};
85 int rc = sdhci_init(&sd, &sd_ops, &cfg);
86 if (rc) {
87 ck_printf("pi5_sd: init FAIL rc=%d last_cmd=%d last_int=0x%08x\n", rc, sd.last_cmd, sd.last_int);
88 return;
89 }
90 sd_up = 1;
91 ck_printf("pi5_sd: card ready spec=%u card_hz=%u high_capacity=%d blocks=%llu (%llu MiB) rca=0x%x bus=4bit\n",
92 sd.spec, sd.card_hz, sd.high_capacity, (unsigned long long)sd.blocks,
93 (unsigned long long)(sd.blocks / 2048u), sd.rca);
94 /* Read-only check: sector 0 of the card is an MBR (0x55 0xaa). */
95 static uint8_t s0[512] __attribute__((aligned(64)));
96 rc = disk_read(&sd.dev, 0, 1, s0);
97 ck_printf("pi5_sd: read sector 0 rc=%d signature=%02x%02x (%s)\n", rc, s0[510], s0[511],
98 rc == 0 && s0[510] == 0x55 && s0[511] == 0xaa ? "MBR ok" : "no MBR signature");
99 /* Step 3: the AIENOS partition through the same GPT partition view the NVMe path uses (dev/disk_part.c),
100 * then the scratch marker at its end (core/pi5_marker.c): formatted on a blank card, reopened after a reboot. */
101 ck_gpt_info gi;
102 rc = ck_gpt_find_aienos(&sd.dev, &part, &gi);
103 if (rc) {
104 ck_printf("pi5_part: no AIENOS partition, refusing writes (%s, rc=%d)\n", ck_gpt_strerror(rc), rc);
105 return;
106 }
107 ck_printf("pi5_part: AIENOS partition first_lba=%llu blocks=%llu (%llu MiB) entries_used=%u index=%u via GPT (CRC32 primary+backup ok)\n",
108 (unsigned long long)part.first_lba, (unsigned long long)part.blocks,
109 (unsigned long long)(part.blocks / 2048u), gi.used, gi.part_index);
110 /* The Store's view ends before the marker region (core/pi5_marker.h ck_pi5_store_view), in every image,
111 * so the marker and the Store's units and probe unit never share a sector. */
112 rc = ck_pi5_store_view(&part, &store_part);
113 if (rc)
114 ck_printf("pi5_store_view: FAIL rc=%d (%s)\n", rc, ck_pi5_marker_strerror(rc));
115 else
116 ck_printf("pi5_store_view: lba 0..%llu (%llu units) for the Store; marker reserved at %llu..%llu\n",
117 (unsigned long long)(store_part.blocks - 1u), (unsigned long long)(store_part.blocks / 8u),
118 (unsigned long long)(part.blocks - CK_PI5_MARKER_SECS), (unsigned long long)(part.blocks - 1u));
119 static uint8_t mbuf[CK_PI5_MARKER_BUF_BYTES] __attribute__((aligned(64)));
120 uint64_t gen = 0;
121 rc = ck_pi5_marker_run(&part.dev, mbuf, &gen);
122 if (rc > 0)
123 ck_printf("pi5_store_layer: %s generation=%llu region=%u sectors at partition block %llu\n",
124 ck_pi5_marker_strerror(rc), (unsigned long long)gen, CK_PI5_MARKER_SECS,
125 (unsigned long long)(part.blocks - CK_PI5_MARKER_SECS));
126 else
127 ck_printf("pi5_store_layer: FAIL rc=%d (%s)\n", rc, ck_pi5_marker_strerror(rc));
128}
129
130void ck_pi5_stage(const char *name)
131{
132 (void)name;
133 ck_pi5_wdt_pet();
134}
135
136static uint64_t commit_prefix(void)
137{
138 const char *c = ck_commit();
139 uint64_t v = 0;
140 for (int i = 0; i < 16 && c[i]; i++) {
141 char x = c[i];
142 unsigned d = x >= '0' && x <= '9' ? (unsigned)(x - '0') : x >= 'a' && x <= 'f' ? (unsigned)(x - 'a' + 10) : 16;
143 if (d > 15)
144 break;
145 v = v << 4 | d;
146 }
147 return v;
148}
149
150static char text[CK_KLOG_MAX + 512] __attribute__((aligned(64)));
151static struct ck_klog_hdr hdr __attribute__((aligned(64)));
152
153/* 0 written and read back; 1 nothing to do (no slot / no card); -1 refused or failed. */
154static int klog_write(void)
155{
156 const struct ck_klog_boot *kb = &ck_klog_boot;
157 const struct ck_klog_map *m = &kb->map;
158 const char *why = 0;
159 if (kb->state != CK_KLOG_BOOT_FOUND) {
160 ck_printf("klog_card: skipped (stub state %d: no checked KLOG.MAP)\n", kb->state);
161 return 1;
162 }
163 if (!sd_up) {
164 ck_puts("klog_card: skipped (SD card not ready)\n");
165 return 1;
166 }
167 if (m->lba + m->count > sd.blocks || kb->disk_last_block + 1u != sd.blocks) {
168 ck_printf("klog_card: refused (card has %llu sectors, firmware saw %llu, slot ends at %llu)\n",
169 (unsigned long long)sd.blocks, (unsigned long long)(kb->disk_last_block + 1u),
170 (unsigned long long)(m->lba + m->count));
171 return -1;
172 }
173 if (disk_read(&sd.dev, m->lba, 1, (uint8_t *)&hdr) != DISK_OK || ck_klog_hdr_match(&hdr, m, &why)) {
174 ck_printf("klog_card: refused (slot header check: %s)\n", why ? why : "read failed");
175 return -1;
176 }
177 ck_printf("klog_card: slot verified by the kernel's own SD driver (lba=%llu sectors=%llu); writing\n",
178 (unsigned long long)m->lba, (unsigned long long)m->count);
179 uint64_t cap = ck_klog_slot_bytes(m);
180 size_t n = ck_klog_read(text, cap < CK_KLOG_MAX ? (size_t)cap : CK_KLOG_MAX);
181 uint32_t secs = (uint32_t)((n + 511u) / 512u);
182 for (size_t i = n; i < (size_t)secs * 512u; i++)
183 text[i] = 0;
184 if (secs && disk_write(&sd.dev, m->lba + 1u, secs, (const uint8_t *)text) != DISK_OK)
185 return -1;
186 if (disk_flush(&sd.dev) != DISK_OK)
187 return -1;
188 /* Payload read back and compared before the header that vouches for it is written. */
189 static uint8_t chk[8u * 512u] __attribute__((aligned(64)));
190 for (uint32_t s0 = 0; s0 < secs; s0 += 8u) {
191 uint32_t c = secs - s0 < 8u ? secs - s0 : 8u;
192 if (disk_read(&sd.dev, m->lba + 1u + s0, c, chk) != DISK_OK)
193 return -1;
194 for (uint32_t i = 0; i < c * 512u; i++)
195 if (chk[i] != (uint8_t)text[(size_t)s0 * 512u + i])
196 return -1;
197 }
198 ck_klog_hdr_written(&hdr, m, n, ck_klog_total(), commit_prefix(), ck_stage_name());
199 if (disk_write(&sd.dev, m->lba, 1, (const uint8_t *)&hdr) != DISK_OK || disk_flush(&sd.dev) != DISK_OK)
200 return -1;
201 static struct ck_klog_hdr back __attribute__((aligned(64)));
202 if (disk_read(&sd.dev, m->lba, 1, (uint8_t *)&back) != DISK_OK)
203 return -1;
204 const uint8_t *a = (const uint8_t *)&hdr, *b = (const uint8_t *)&back;
205 for (unsigned i = 0; i < sizeof hdr; i++)
206 if (a[i] != b[i])
207 return -1;
208 return 0;
209}
210
211void ck_pi5_before_reset(void)
212{
213 static int once;
214 if (once++)
215 return; /* a fault inside the write resets without a second try */
216 ck_pi5_wdt_pet();
217 int rc = klog_write();
218 /* This line reaches the screen and UART only: the card copy ends above. */
219 ck_printf("klog_card: %s\n", rc == 0 ? "written and read back" : rc > 0 ? "not written" : "FAILED");
220 ck_pi5_wdt_pet();
221}
222
223#if defined(CK_PI5_STORE) || defined(CK_PI5_UNLOCK)
224#if defined(CK_PI5_STORE) && defined(CK_PI5_UNLOCK)
225#error "CK_PI5_STORE (TEST keys) and CK_PI5_UNLOCK (production class keys) are separate images"
226#endif
227/* Hand the card's AIENOS partition view to the sealed Store stage (svc/store_boot.c ck_stage_store), the
228 * same stage the QEMU "full" image runs. Tier: SEED-0B-PI5, the Pi counterpart of SEED-0B-QEMU. The Store
229 * keys are the labelled TEST keys (ck_store_test_keys); the production key source stays BLOCKED_OPERATOR
230 * and is not touched here. CK_HARDWARE_STAGING and CK_PI5 cannot be combined (Makefile), so no hardware
231 * staging image can ever carry this. */
232const disk_dev *ck_dev_boot_disk(void)
233{
234 static int said;
235 if (!said++)
236#ifdef CK_PI5_UNLOCK
237 ck_puts("pi5_unlock_tier: PI5-4 recovery-secret unlock: Store keys derive from K_vol (ADR 0017 Slot 1), "
238 "PRODUCTION class; no TEST keys and no TEST uuid exist in this image\n");
239#else
240 ck_puts("pi5_store_tier: SEED-0B-PI5 qualification only: Store keys are the public TEST keys, "
241 "Store uuid is the TEST uuid, no production keys, not a production Store\n");
242#endif
243 return sd_up && store_part.valid ? &store_part.dev : 0;
244}
245
246/* The SD card has no bus master or SMMU stream to revoke: nothing to release. */
247void ck_dev_nvme_release(void) {}
248#endif
249
250#ifdef CK_PI5_UNLOCK
251/* The recovery secret source of the Pi unlock image (CK_SB_SECRET_LEN = 32, svc/store_boot.h). This
252 * image has no UART input and no TPM. Plain CK_PI5_UNLOCK compiles in no source: the Store stays
253 * closed (ck_store_pi5_unlock_run refuses before reading the disk). CK_PI5_UNLOCK_CARD (QUALIFICATION
254 * ONLY) adds the throwaway secret from the card request file the UEFI stub read (core/unlock_req.h);
255 * it is handed out once and wiped. The source string is printed loudly by the Store stage. */
256int ck_pi5_unlock_secret(uint8_t out[32], const char **source)
257{
258#ifdef CK_PI5_UNLOCK_CARD
259 if (ck_unlock_req_take(out) == 0) {
260 if (source)
261 *source = CK_UNLOCK_REQ_SOURCE;
262 return 0;
263 }
264#else
265 (void)source;
266#endif
267 for (unsigned i = 0; i < 32; i++)
268 out[i] = 0;
269 return -1;
270}
271#endif