diff --git a/.crumb b/.crumb index 9d5ba03c..cd84d576 100644 --- a/.crumb +++ b/.crumb @@ -33,27 +33,27 @@ "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "8303b3b9cfae4adb60d22cb79949b0152c95efd960b344371789b3ccf9ad018c", - "last_commit": "1994c4f", + "digest": "352a48dff4b4321e98d02b4f76fccf571ae86d86560d6850cdd042ec766cd49e", + "last_commit": "93ae032", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { - "digest": "80f03d413b7d49f38058ecbb8c712c6889dab80c227cf9f22999eaa885183abb", - "last_commit": "1994c4f", + "digest": "d2fbf98209136e9f0f16f6f5a8a0938a9b711f46798e0d74fb0344f13dedb210", + "last_commit": "93ae032", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:d9b73eb821200bc7d967a967e0c3cc6702a15909a37d5e8ae02563569e6e6841", - "compiled_at": "2026-10-13T05:44:11Z", + "children_root": "sha256:9872d818d301951efbb3310a64db9bfcefb116f94b14619e10033034bfe606ec", + "compiled_at": "2026-10-13T06:35:07Z", "compiler": "crumb-compile/1", - "digest": "sha256:06efc6bb5bbab7c5628162b2eb2daea545c4a3eb86d54b286597f4dea67d8197", + "digest": "sha256:a331ea8e8a5e5679e0f4d0226e99baa441c9c83f354050c20b78127802b2bc76", "evidence_files": 156, "evidence_root": "sha256:cd53ab119be9de6ce28c4f5949b106c9622f1fe105611de3de21c3bce7932694", - "files": 3124, - "generated_at_commit": "1994c4f8c4e8521d7febed45306515aa5085f207", - "source_tree": "sha256:1c9bf521ea3d699d57e9f1633a2adbeb59497b6adb6ee86b86f4b24ba25ee10b" + "files": 3137, + "generated_at_commit": "93ae0323091b1f40c93fbc3a2bdfaaa48745c61a", + "source_tree": "sha256:ed701cf9671da05d77fbadb88364beb56553bbcc9e7c22099400cfa3bd1345e8" }, "owner_lane": "", "provenance": { diff --git a/.github/workflows/ck-kernel.yml b/.github/workflows/ck-kernel.yml index 0f61eb29..8396a055 100644 --- a/.github/workflows/ck-kernel.yml +++ b/.github/workflows/ck-kernel.yml @@ -35,6 +35,7 @@ on: - 'native/**' - 'scripts/ck_gates.sh' - 'scripts/qemu_ck_*.sh' + - 'scripts/ck_pi5_build_check.sh' - 'crates/aienos-fpu-probe/**' - 'crates/aienos-infer-kernel/**' - 'scripts/lib_ck_m1_checks.sh' @@ -47,6 +48,7 @@ on: - 'native/**' - 'scripts/ck_gates.sh' - 'scripts/qemu_ck_*.sh' + - 'scripts/ck_pi5_build_check.sh' - 'crates/aienos-fpu-probe/**' - 'crates/aienos-infer-kernel/**' - 'scripts/lib_ck_m1_checks.sh' @@ -122,6 +124,9 @@ jobs: AIENOS_QEMU_GIC: "2" run: bash scripts/qemu_ck_boot_test.sh + - name: Raspberry Pi 5 kernel image builds, and the default image carries none of its board glue (AIENOS-PI5 step 1, build only) + run: bash scripts/ck_pi5_build_check.sh + - name: Check the host tests left the tree clean (the gate runner refuses a dirty tree) run: | git status --porcelain --untracked-files=normal diff --git a/native/.crumb b/native/.crumb index 3921b65a..75d3db7a 100644 --- a/native/.crumb +++ b/native/.crumb @@ -55,8 +55,8 @@ "purpose": "Directory native/argus. Purpose not yet described by a human." }, { - "digest": "75239007e38138a416cfa36166d3c95837b7f0f4c1daab6182cd1d9c034dab14", - "last_commit": "9980344", + "digest": "aa53401411b940e8361373c4e4325edf91b649c86ca6047dbe1c2dbb10f5c2de", + "last_commit": "93ae032", "name": "boot", "purpose": "Directory native/boot. Purpose not yet described by a human." }, @@ -74,13 +74,13 @@ }, { "digest": "3508a201e84c9e6a53160c5c46395f17c376b0c460b3b81049803505876d4f07", - "last_commit": "1994c4f", + "last_commit": "2563059", "name": "disk", "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "d42b02fe5930b1f65af466ecded139a8c4562a28ffcad4dc3e7481407894e13f", - "last_commit": "5135477", + "digest": "65c8ebf17ec712136e00e44cc326b1f2acad36e50b15af71e13c692e5bfa5add", + "last_commit": "93ae032", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, @@ -109,15 +109,15 @@ "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:311c1318e0867234ac0abe5ffc9455512707db230661932847feaa6255047129", - "compiled_at": "2026-10-13T05:44:11Z", + "children_root": "sha256:02036a0be67ea05b04ac7ad5a176ca56b01d0d84f80e6eddca834ba7ad9c9a60", + "compiled_at": "2026-10-13T06:35:07Z", "compiler": "crumb-compile/1", - "digest": "sha256:8303b3b9cfae4adb60d22cb79949b0152c95efd960b344371789b3ccf9ad018c", + "digest": "sha256:352a48dff4b4321e98d02b4f76fccf571ae86d86560d6850cdd042ec766cd49e", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", - "files": 493, - "generated_at_commit": "1994c4f8c4e8521d7febed45306515aa5085f207", - "source_tree": "sha256:ef584641d7c2b4c17ddf556295cb7d602eedc0bc1c45009f0566fe26dfd3352c" + "files": 505, + "generated_at_commit": "93ae0323091b1f40c93fbc3a2bdfaaa48745c61a", + "source_tree": "sha256:29539d34a7aac0f425e45e8480a7800d347610a67521497817c1b2abd6e311ef" }, "owner_lane": "", "provenance": { diff --git a/native/boot/.crumb b/native/boot/.crumb index 2da3f47e..d17671e4 100644 --- a/native/boot/.crumb +++ b/native/boot/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-13T03:39:20Z", + "compiled_at": "2026-10-13T06:35:07Z", "compiler": "crumb-compile/1", - "digest": "sha256:75239007e38138a416cfa36166d3c95837b7f0f4c1daab6182cd1d9c034dab14", + "digest": "sha256:aa53401411b940e8361373c4e4325edf91b649c86ca6047dbe1c2dbb10f5c2de", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 17, - "generated_at_commit": "1adbd4b7707bd586c253296ab4ca9f1e57015a9d", - "source_tree": "sha256:fc305978f6fd520ff798166b6f39f01b59b9cf79e55a5f36b0be7c63e967e7a0" + "files": 18, + "generated_at_commit": "93ae0323091b1f40c93fbc3a2bdfaaa48745c61a", + "source_tree": "sha256:63118f8e3a815f73d94b9980d177542e0d8a82d6f958e8af3127c82acf987fb2" }, "owner_lane": "", "provenance": { diff --git a/native/boot/efi_klog.c b/native/boot/efi_klog.c new file mode 100644 index 00000000..e06307d5 --- /dev/null +++ b/native/boot/efi_klog.c @@ -0,0 +1,107 @@ +/* efi_klog.c -- read \EFI\AIENOS\KLOG.MAP from the boot volume before + * ExitBootServices and find the card that carries the matching log slot, + * through the firmware's own Block I/O (Raspberry Pi 5 build, CK_KLOG). + * The kernel later writes its console log into that slot with its own SD + * driver (native/kernel/core/klog_flush.c), after checking the slot header + * again itself. Here nothing is written: this file only reads. + * + * Result in ck_klog_boot (shared with the kernel: the stub and the kernel + * are one image). state CK_KLOG_BOOT_ABSENT (no map: not an error), + * _REFUSED (printed why), _FOUND (map checked and the slot header read back + * from exactly one whole disk). */ +#include "efi.h" +#include "../kernel/core/klog_slot.h" +#include "../kernel/core/klog_boot.h" +#include "../kernel/arch/arch.h" +#include "../kernel/core/ck_internal.h" + +struct ck_klog_boot ck_klog_boot; +static struct ck_klog_hdr sector __attribute__((aligned(4096))); + +static int read_map(EFI_HANDLE image, EFI_BOOT_SERVICES *bs, struct ck_klog_map *m) +{ + static const EFI_GUID li_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID_INIT; + static const EFI_GUID fs_guid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID_INIT; + static CHAR16 path[] = {'\\', 'E', 'F', 'I', '\\', 'A', 'I', 'E', 'N', 'O', 'S', '\\', + 'K', 'L', 'O', 'G', '.', 'M', 'A', 'P', 0}; + EFI_LOADED_IMAGE_PROTOCOL *li = 0; + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fs = 0; + EFI_FILE_PROTOCOL *root = 0, *f = 0; + if (bs->HandleProtocol(image, &li_guid, (void **)&li) != EFI_SUCCESS || !li || !li->DeviceHandle) + return 1; + if (bs->HandleProtocol(li->DeviceHandle, &fs_guid, (void **)&fs) != EFI_SUCCESS || !fs) + return 1; + if (fs->OpenVolume(fs, &root) != EFI_SUCCESS || !root) + return 1; + if (root->Open(root, &f, path, EFI_FILE_MODE_READ, 0) != EFI_SUCCESS || !f) { + root->Close(root); + return 1; + } + uint64_t n = sizeof *m; + memset(m, 0, sizeof *m); + EFI_STATUS s = f->Read(f, &n, m); + f->Close(f); + root->Close(root); + if (s != EFI_SUCCESS || n != sizeof *m) { + ck_printf("klog_map: refused (read status=0x%llx bytes=%llu, want %u)\n", (unsigned long long)s, + (unsigned long long)n, (unsigned)sizeof *m); + return -1; + } + return 0; +} + +void ck_boot_klog_find(EFI_HANDLE image, EFI_SYSTEM_TABLE *st) +{ + static const EFI_GUID bio_guid = EFI_BLOCK_IO_PROTOCOL_GUID_INIT; + EFI_BOOT_SERVICES *bs = st->BootServices; + struct ck_klog_boot *kb = &ck_klog_boot; + const char *why = 0; + memset(kb, 0, sizeof *kb); + int rc = read_map(image, bs, &kb->map); + if (rc > 0) { + kb->state = CK_KLOG_BOOT_ABSENT; + ck_puts("klog_map: absent (no " CK_KLOG_MAP_PATH "); the log stays on the console only\n"); + return; + } + kb->state = CK_KLOG_BOOT_REFUSED; + if (rc < 0) + return; + if (ck_klog_map_check(&kb->map, 0, &why)) { + ck_printf("klog_map: refused (%s)\n", why); + return; + } + EFI_HANDLE *handles = 0; + uint64_t n = 0; + if (bs->LocateHandleBuffer(ByProtocol, &bio_guid, 0, &n, &handles) != EFI_SUCCESS || !handles) { + ck_puts("klog_map: refused (no Block I/O handles)\n"); + return; + } + unsigned matches = 0; + uint64_t last = 0; + for (uint64_t i = 0; i < n; i++) { + EFI_BLOCK_IO_PROTOCOL *bio = 0; + if (bs->HandleProtocol(handles[i], &bio_guid, (void **)&bio) != EFI_SUCCESS || !bio || !bio->Media) + continue; + const EFI_BLOCK_IO_MEDIA *md = bio->Media; + if (!md->MediaPresent || md->LogicalPartition || md->BlockSize != 512u || + md->LastBlock < kb->map.lba + kb->map.count - 1u) + continue; + if (bio->ReadBlocks(bio, md->MediaId, kb->map.lba, 512u, §or) != EFI_SUCCESS) + continue; + if (ck_klog_hdr_match(§or, &kb->map, &why) == 0) { + matches++; + last = md->LastBlock; + } + } + bs->FreePool(handles); + if (matches != 1) { + ck_printf("klog_map: refused (%u disks carry the slot header, want exactly 1; %llu Block I/O handles)\n", + matches, (unsigned long long)n); + return; + } + kb->state = CK_KLOG_BOOT_FOUND; + kb->disk_last_block = last; + ck_printf("klog_map: slot lba=%llu sectors=%llu on a disk of %llu sectors (firmware read back the header)\n", + (unsigned long long)kb->map.lba, (unsigned long long)kb->map.count, + (unsigned long long)(last + 1u)); +} diff --git a/native/boot/efi_main.c b/native/boot/efi_main.c index 88d86d46..00b46437 100644 --- a/native/boot/efi_main.c +++ b/native/boot/efi_main.c @@ -38,6 +38,9 @@ static struct ck_handoff handoff; static EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout; int ck_boot_model_load(EFI_HANDLE image, EFI_SYSTEM_TABLE *st, struct ck_handoff *h); +#ifdef CK_KLOG +void ck_boot_klog_find(EFI_HANDLE image, EFI_SYSTEM_TABLE *st); +#endif void ck_boot_gop_find(EFI_SYSTEM_TABLE *st, struct ck_handoff *h); static void efi_write(const char *s, size_t n) @@ -127,6 +130,10 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st) /* The yardstick model from the boot disk, while the firmware drivers are * still up (efi_model.c). Absent map: boot continues without a model. */ ck_boot_model_load(image, st, h); +#ifdef CK_KLOG + /* Pi 5 build: find the card's log slot while Block I/O is still up (efi_klog.c). */ + ck_boot_klog_find(image, st); +#endif ck_puts("kernel_impl: c (native/kernel, Lane 18)\n"); vendor_print(st->FirmwareVendor); ck_printf("firmware_revision: 0x%x\n", st->FirmwareRevision); diff --git a/native/kernel/.crumb b/native/kernel/.crumb index c74facef..87b7e81a 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -46,7 +46,7 @@ "children": [ { "digest": "b12ce043af831c40220c7546c603e941604e5875d0bd5b591df1d650a5053ef0", - "last_commit": "391fcd5", + "last_commit": "5135477", "name": "arch", "purpose": "Directory native/kernel/arch. Purpose not yet described by a human." }, @@ -57,20 +57,20 @@ "purpose": "Directory native/kernel/artifact. Purpose not yet described by a human." }, { - "digest": "3113e99820041c9ba4481d6da5d6dcebb3eaaf25867e4278945544260466d5f3", - "last_commit": "391fcd5", + "digest": "ba9f55f3590752efdda812ebdd12f1bca77776fbd684c944babe07da32e24e1f", + "last_commit": "93ae032", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, { - "digest": "d7b81a8f888e7b4ca5337a5c9279671c33e1f4a70a7318ed20e1c5367c8a289b", - "last_commit": "2cdd37f", + "digest": "7f3dc4d9ecbeba802bd8a3c59514ff336ea615e8aca587c1c998e660abde23c3", + "last_commit": "93ae032", "name": "dev", "purpose": "Directory native/kernel/dev. Purpose not yet described by a human." }, { - "digest": "283a167327166bcfd270869cc04a1a03ea21366248471464db7ef1cf37058ada", - "last_commit": "3647654", + "digest": "d3e1b6aa9a623c95208e730cea917c2824682caa6518dd4433882908b5facef3", + "last_commit": "93ae032", "name": "include", "purpose": "Directory native/kernel/include. Purpose not yet described by a human." }, @@ -87,8 +87,8 @@ "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, { - "digest": "da6470d80184ef3a173623e87a9712d4643f67d73dbab6be80f7e41902fc81e4", - "last_commit": "391fcd5", + "digest": "46def7a41aefac1c42a8bda757406960c6f03147fc147b860c94e8166febde16", + "last_commit": "93ae032", "name": "tests", "purpose": "Directory native/kernel/tests. Purpose not yet described by a human." }, @@ -99,15 +99,15 @@ "purpose": "Hosted (Linux-built) checkers for the native kernel, run by `make -C native/kernel test`; nothin" } ], - "children_root": "sha256:c4368bc26c5db8c0657821ed5865ceaf328cb8c0519af1b83d4da60f73503ef6", - "compiled_at": "2026-10-13T04:45:02Z", + "children_root": "sha256:565586a153ff32956bee505d1da9e43cb94da550bfa9dd7b354a16012ca49586", + "compiled_at": "2026-10-13T06:35:07Z", "compiler": "crumb-compile/1", - "digest": "sha256:d42b02fe5930b1f65af466ecded139a8c4562a28ffcad4dc3e7481407894e13f", + "digest": "sha256:65c8ebf17ec712136e00e44cc326b1f2acad36e50b15af71e13c692e5bfa5add", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 317, - "generated_at_commit": "391fcd54707b2ba006e0652c71259a17ef78ec6a", - "source_tree": "sha256:34a46f83a39f76993699af90e537d258752aefffff99710bce173113079fce2d" + "files": 328, + "generated_at_commit": "93ae0323091b1f40c93fbc3a2bdfaaa48745c61a", + "source_tree": "sha256:3876081051a14ec59246a47ffcd8b5f393acf291e95fd1d94d1a4b04807f6873" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 96c24375..2a4b1c55 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -132,6 +132,24 @@ KNM := $(CROSS)nm KREADELF := $(CROSS)readelf HOSTCC ?= cc +# Raspberry Pi 5 board image (CK_PI5=1, "make pi5"): the core image plus +# core/pi5.c (PM watchdog, microSD bring-up, console log written to the +# card), core/klog*.c, dev/pi5_wdt.c, the PIO SD driver (native/disk) and the +# UEFI stub's KLOG.MAP reader (native/boot/efi_klog.c). Own OUT, so it never +# replaces a default image. Not for the Spark or QEMU: pi5_wdt.c and pi5.c +# touch BCM2712-only addresses and refuse to build without CK_PI5. +CK_PI5 ?= +ifneq ($(strip $(CK_PI5)),) +ifneq ($(strip $(CK_PI5)),1) +$(error CK_PI5 must be empty or 1) +endif +ifneq ($(strip $(CK_HARDWARE_STAGING)),) +$(error CK_PI5 cannot be combined with CK_HARDWARE_STAGING) +endif +OUT ?= $(REPO)/target/native-kernel-pi5 +CK_EXTRA_CFLAGS += -DCK_PI5=1 -DCK_KLOG=1 +endif + # TEST-ONLY artifact source: CK_TEST_FWCFG_ARTIFACTS=1 compiles the QEMU fw_cfg # side channel (opt/aienos/artifacts) into the loader instead of the boot disk # Store. Every image built without it is checked to contain no fw_cfg artifact @@ -634,6 +652,10 @@ CORE_C := $(BOOT)/efi_main.c $(BOOT)/efi_model.c $(BOOT)/efi_gop.c \ $(KDIR)/core/smmu.c $(KDIR)/core/smmu_svc.c $(KDIR)/core/sched.c $(KDIR)/core/ipc.c \ $(KDIR)/core/fpu.c $(KDIR)/core/infer.c $(KDIR)/core/m3.c $(KDIR)/core/entropy.c $(KDIR)/arch/rndr.c $(KDIR)/core/smp.c \ $(KDIR)/mm/frames.c $(KDIR)/mm/heap.c $(KDIR)/mm/mmu.c $(KDIR)/mm/pt.c +ifeq ($(strip $(CK_PI5)),1) +CORE_C += $(BOOT)/efi_klog.c $(KDIR)/core/pi5.c $(KDIR)/core/klog.c $(KDIR)/core/klog_slot.c \ + $(KDIR)/dev/pi5_wdt.c $(NATIVE)/disk/disk.c $(NATIVE)/disk/disk_sdhci.c +endif # P2 sealed-artifact loader: format/admission/receipt (artifact/), SHA-256 # (argus), Ed25519/SHA-512 (sig). Built into the core so every image carries # the loader; the TEST-anchor qualification build is CK_SEED0B_TEST_ANCHOR=1. @@ -780,6 +802,9 @@ ART_HDRS := $(wildcard $(NATIVE)/sig/*.h $(NATIVE)/argus/sha256.h $(KDIR)/svc/co $(ART_OBJS): KCFLAGS += $(ART_INC) $(ART_OBJS): $(ART_HDRS) +# core/pi5.c drives the SD card through native/disk. +$(KOBJ)/kernel/core/pi5.o: KCFLAGS += -I$(NATIVE)/disk + # core/infer.c uses the in-core SHA-256 (argus) to prove the ingested bytes. $(KOBJ)/kernel/core/infer.o: KCFLAGS += -I$(NATIVE)/argus @@ -1028,10 +1053,10 @@ $(FULL_EFI): $(FULL_ELF) HOST_SRCS := $(KDIR)/core/fmt.c $(KDIR)/core/acpi.c $(KDIR)/core/smmu.c $(KDIR)/mm/frames.c \ $(KDIR)/mm/pt.c $(KDIR)/mm/heap.c $(KDIR)/core/sched.c $(KDIR)/core/ipc.c $(KDIR)/core/entropy.c \ $(KDIR)/core/handoff_check.c $(KDIR)/core/fbcon.c $(KDIR)/core/acpi_dev.c $(KDIR)/core/lifecycle_fsm.c \ - $(KDIR)/core/line_rx.c + $(KDIR)/core/line_rx.c $(KDIR)/core/klog.c $(KDIR)/core/klog_slot.c TESTS := test_fmt test_frames test_pt test_acpi test_acpi_dev test_heap test_smmu test_sched test_ipc test_artifact test_osc_unit test_osc_launch test_osc_admit test_entropy test_handoff test_fbcon test_osh_op \ test_continuity_codec test_continuity_resolve test_continuity_commit test_continuity_recovery test_continuity_geometry \ - test_continuity_subject test_continuity_subject_provision test_lifecycle test_console_rx + test_continuity_subject test_continuity_subject_provision test_lifecycle test_console_rx test_klog test_klog_slot test_pi5 HCFLAGS := -std=gnu11 -O1 -g -Wall -Wextra -Werror -I$(KDIR)/core -I$(KDIR)/mm -I$(KDIR)/tests -I$(BOOT) SANFLAGS := -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer @@ -1759,6 +1784,10 @@ $(PI5DIAG_EFI): $(PI5DIAG_ELF) $(call pe_image,$<,$@) pi5-diag: $(PI5DIAG_EFI) +# Raspberry Pi 5 board image (see CK_PI5 above): target/native-kernel-pi5/BOOTAA64.EFI +pi5: + $(MAKE) CK_PI5=1 all + # ---- host checker for the AIENOS_CK_SCREEN gate (scripts/qemu_ck_screen_test.sh) ---- FB_CHECK_TOOL := $(OUT)/host/ck_fb_check $(FB_CHECK_TOOL): $(KDIR)/tools/ck_fb_check.c $(KDIR)/core/fbcon.c $(KDIR)/core/fbcon.h $(KDIR)/core/font8x8.h @@ -1796,4 +1825,4 @@ clean: FORCE: .PHONY: uefi-vars slot-loader slot-loader-check pi5-diag -.PHONY: all full test sanitize handoff-mutant acpi-dev-mutant iort-mutants smmu-l2-mutants acpi-scan clean print-kcflags udp-echo tool store-image gpt-image cont-tool owner-gen fb-check continuity-mutants continuity-resolve-mutants FORCE +.PHONY: all full pi5 test sanitize handoff-mutant acpi-dev-mutant iort-mutants smmu-l2-mutants acpi-scan clean print-kcflags udp-echo tool store-image gpt-image cont-tool owner-gen fb-check continuity-mutants continuity-resolve-mutants FORCE diff --git a/native/kernel/README.md b/native/kernel/README.md index 865dbfd7..36ac9b5f 100644 --- a/native/kernel/README.md +++ b/native/kernel/README.md @@ -140,6 +140,28 @@ of them: host tools) are still present as data in every image, including hardware staging; the ordinary loader trusts no anchor. +## Raspberry Pi 5 image (AIENOS-PI5 step 1) + + make -C native/kernel pi5 # target/native-kernel-pi5/BOOTAA64.EFI (-DCK_PI5=1 -DCK_KLOG=1) + scripts/ck_pi5_build_check.sh # build only, ends CK_PI5_BUILD: PASS + +The Pi image is the default core image plus board glue (`core/pi5.c`). The +default and QEMU images link none of it: the hooks in `include/ck.h` are weak +and absent, and `scripts/ck_pi5_build_check.sh` checks both images. + +| Piece | What | +| --- | --- | +| `dev/pi5_wdt.c` | BCM2712 PM watchdog (Linux `bcm2835_wdt.c` registers), armed for `CK_PI5_WDT_S` (15 s) at start, petted at every `ck_set_stage`. A hang resets the board; on the hub the tryboot one-shot flag is already cleared, so it comes back on normal Linux. | +| `core/pi5.c` | Brings up the microSD host with `native/disk/disk_sdhci.c` at a fixed address (the DSDT computes `\_SB.SDC0` `_CRS` in a method the static ACPI scanner does not run; same range as Linux `bcm2712.dtsi` sdio1), clock 200 MHz (`clk_emmc2`), PIO only, so the missing SMMU does not matter. Reads sector 0 and checks the MBR signature. | +| `core/klog.c` | RAM copy of everything printed: the first 192 KiB plus the last 64 KiB. | +| `core/klog_slot.h`, `../boot/efi_klog.c` | The card log slot. Before ExitBootServices the UEFI stub reads `\EFI\AIENOS\KLOG.MAP` (absolute first sector, sector count, nonce) and keeps it only if it checks and exactly one whole disk holds a matching empty slot header there. | +| `ck_pi5_before_reset` | Runs inside `ck_reset` (the normal end and every panic). It re-reads the slot header with the kernel's own SD driver, refuses on any mismatch or if the card size differs from what the firmware saw, writes the log text after the header, flushes, then writes the WRITTEN header last and reads it back. One try only. | + +The image cannot boot in QEMU (the SD host and PM addresses are Pi 5 only). It +runs on the hub Pi with the one-shot tryboot runner, which prepares the slot +(one contiguous 2 MiB file, its raw card sector read back and compared before +the map is written) and pulls the log back after the hub returns to Linux. + ## Known hardware risk: disk footprint (CK gate DISK_LAYOUT: NOT_RUN) **Do not boot the C kernel on a disk holding data until CK gate `DISK_LAYOUT` diff --git a/native/kernel/core/.crumb b/native/kernel/core/.crumb index 8496eb05..60893290 100644 --- a/native/kernel/core/.crumb +++ b/native/kernel/core/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-13T04:45:02Z", + "compiled_at": "2026-10-13T06:35:07Z", "compiler": "crumb-compile/1", - "digest": "sha256:3113e99820041c9ba4481d6da5d6dcebb3eaaf25867e4278945544260466d5f3", + "digest": "sha256:ba9f55f3590752efdda812ebdd12f1bca77776fbd684c944babe07da32e24e1f", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 45, - "generated_at_commit": "391fcd54707b2ba006e0652c71259a17ef78ec6a", - "source_tree": "sha256:8cdf83971a1cde5fc3427130f19faa502874e0c594be76cc884311dbfa21ce30" + "files": 51, + "generated_at_commit": "93ae0323091b1f40c93fbc3a2bdfaaa48745c61a", + "source_tree": "sha256:36fbfaae2071682892468f4ae84c83ec1a508fbdc02acb407e6f1aa513bd4dbc" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/core/console.c b/native/kernel/core/console.c index a8933f20..c92b3ee3 100644 --- a/native/kernel/core/console.c +++ b/native/kernel/core/console.c @@ -9,6 +9,9 @@ #include "fbcon.h" #include "arch.h" #include "uart_rx.h" +#ifdef CK_KLOG +#include "klog.h" +#endif #ifndef CK_FB_HOLD_S #define CK_FB_HOLD_S 0 @@ -74,6 +77,9 @@ static void uart_putc(char c) static void write_n(const char *s, size_t n) { +#ifdef CK_KLOG + ck_klog_put(s, n); /* RAM copy for the card (Pi 5 build: core/klog_slot.c) */ +#endif if (efi_write) { efi_write(s, n); return; diff --git a/native/kernel/core/klog.c b/native/kernel/core/klog.c new file mode 100644 index 00000000..942827a2 --- /dev/null +++ b/native/kernel/core/klog.c @@ -0,0 +1,47 @@ +/* klog.c -- see klog.h. */ +#include "klog.h" +#include "fmt.h" + +static char head[CK_KLOG_HEAD]; +static char tail[CK_KLOG_TAIL]; +static uint32_t head_len; +static uint64_t tail_put; /* bytes put into the ring, ever */ +static uint64_t total; + +void ck_klog_put(const char *s, size_t n) +{ + total += n; + for (size_t i = 0; i < n; i++) { + if (head_len < CK_KLOG_HEAD) + head[head_len++] = s[i]; + else + tail[tail_put++ % CK_KLOG_TAIL] = s[i]; + } +} + +size_t ck_klog_read(char *out, size_t cap) +{ + size_t w = 0; + for (uint32_t i = 0; i < head_len && w < cap; i++) + out[w++] = head[i]; + uint64_t kept = tail_put < CK_KLOG_TAIL ? tail_put : CK_KLOG_TAIL; + if (tail_put > CK_KLOG_TAIL) { + char line[64]; + int n = ck_snprintf(line, sizeof line, "\n[klog: %llu bytes dropped]\n", + (unsigned long long)(tail_put - CK_KLOG_TAIL)); + for (int i = 0; i < n && w < cap; i++) + out[w++] = line[i]; + } + for (uint64_t i = tail_put - kept; i < tail_put && w < cap; i++) + out[w++] = tail[i % CK_KLOG_TAIL]; + return w; +} + +uint64_t ck_klog_total(void) { return total; } + +void ck_klog_clear(void) +{ + head_len = 0; + tail_put = 0; + total = 0; +} diff --git a/native/kernel/core/klog.h b/native/kernel/core/klog.h new file mode 100644 index 00000000..6086f3e2 --- /dev/null +++ b/native/kernel/core/klog.h @@ -0,0 +1,26 @@ +/* klog.h -- RAM copy of the console output, so a board with no serial line + * attached (the hub Raspberry Pi 5) can write it to its card before the + * reset (core/klog_slot.c). The first CK_KLOG_HEAD bytes are kept in order; + * after that the newest CK_KLOG_TAIL bytes are kept in a ring, so both the + * start of the boot and the lines right before a failure survive. Pure: + * no device access, no allocation. Host test: tests/test_klog.c. */ +#ifndef AIENOS_CK_KLOG_H +#define AIENOS_CK_KLOG_H +#include +#include + +#define CK_KLOG_HEAD (192u * 1024u) +#define CK_KLOG_TAIL (64u * 1024u) +/* Largest ck_klog_read output: head + the dropped-bytes line + tail. */ +#define CK_KLOG_MAX (CK_KLOG_HEAD + 64u + CK_KLOG_TAIL) + +void ck_klog_put(const char *s, size_t n); +/* Copies the log into out: the head, then "\n[klog: N bytes dropped]\n" when + * the ring overwrote anything, then the ring oldest first. Stops at cap. + * Returns the bytes written. */ +size_t ck_klog_read(char *out, size_t cap); +/* Every byte ever put, kept or not. */ +uint64_t ck_klog_total(void); +/* Empties the log (tests). */ +void ck_klog_clear(void); +#endif diff --git a/native/kernel/core/klog_boot.h b/native/kernel/core/klog_boot.h new file mode 100644 index 00000000..f8c742e2 --- /dev/null +++ b/native/kernel/core/klog_boot.h @@ -0,0 +1,17 @@ +/* klog_boot.h -- what the UEFI stub found about the log slot + * (native/boot/efi_klog.c), read by the kernel after ExitBootServices + * (core/klog_flush.c). Raspberry Pi 5 build only (CK_KLOG). */ +#ifndef AIENOS_CK_KLOG_BOOT_H +#define AIENOS_CK_KLOG_BOOT_H +#include +#include "klog_slot.h" + +enum { CK_KLOG_BOOT_NONE = 0, CK_KLOG_BOOT_ABSENT = 1, CK_KLOG_BOOT_REFUSED = 2, CK_KLOG_BOOT_FOUND = 3 }; + +struct ck_klog_boot { + int state; /* CK_KLOG_BOOT_* */ + struct ck_klog_map map; /* checked map (state FOUND) */ + uint64_t disk_last_block; /* firmware LastBlock of the disk that carries the slot */ +}; +extern struct ck_klog_boot ck_klog_boot; +#endif diff --git a/native/kernel/core/klog_slot.c b/native/kernel/core/klog_slot.c new file mode 100644 index 00000000..6bad21d4 --- /dev/null +++ b/native/kernel/core/klog_slot.c @@ -0,0 +1,93 @@ +/* klog_slot.c -- see klog_slot.h. Pure. */ +#include "klog_slot.h" + +static int same(const uint8_t *a, const char *b, unsigned n) +{ + for (unsigned i = 0; i < n; i++) + if (a[i] != (uint8_t)b[i]) + return 0; + return 1; +} + +int ck_klog_map_check(const struct ck_klog_map *m, uint64_t last_block, const char **why) +{ + if (!same(m->magic, CK_KLOG_MAP_MAGIC, 8)) { + *why = "bad map magic"; + return -1; + } + if (m->version != CK_KLOG_SLOT_VERSION) { + *why = "unsupported map version"; + return -1; + } + if (m->sector_size != 512u) { + *why = "sector size not 512"; + return -1; + } + if (m->count < CK_KLOG_MIN_SECTORS || m->count > CK_KLOG_MAX_SECTORS) { + *why = "slot length out of range"; + return -1; + } + if (m->lba == 0) { /* sector 0 is the partition table */ + *why = "slot at sector 0"; + return -1; + } + if (m->lba > UINT64_MAX - m->count) { + *why = "slot end overflows"; + return -1; + } + if (last_block && m->lba + m->count - 1u > last_block) { + *why = "slot past the end of the card"; + return -1; + } + uint8_t any = 0; + for (unsigned i = 0; i < 16; i++) + any |= m->nonce[i]; + if (!any) { + *why = "zero nonce"; + return -1; + } + return 0; +} + +int ck_klog_hdr_match(const struct ck_klog_hdr *h, const struct ck_klog_map *m, const char **why) +{ + if (!same(h->magic, CK_KLOG_HDR_MAGIC, 8)) { + *why = "no slot header at the mapped sector"; + return -1; + } + if (h->version != CK_KLOG_SLOT_VERSION) { + *why = "unsupported slot version"; + return -1; + } + for (unsigned i = 0; i < 16; i++) + if (h->nonce[i] != m->nonce[i]) { + *why = "slot nonce differs from the map"; + return -1; + } + if (h->lba != m->lba || h->count != m->count) { + *why = "slot position differs from the map"; + return -1; + } + return 0; +} + +void ck_klog_hdr_written(struct ck_klog_hdr *h, const struct ck_klog_map *m, uint64_t log_len, + uint64_t log_total, uint64_t commit, const char *stage) +{ + uint8_t *p = (uint8_t *)h; + for (unsigned i = 0; i < sizeof *h; i++) + p[i] = 0; + for (unsigned i = 0; i < 8; i++) + h->magic[i] = (uint8_t)CK_KLOG_HDR_MAGIC[i]; + h->version = CK_KLOG_SLOT_VERSION; + h->state = CK_KLOG_STATE_WRITTEN; + for (unsigned i = 0; i < 16; i++) + h->nonce[i] = m->nonce[i]; + h->lba = m->lba; + h->count = m->count; + h->log_len = log_len; + h->log_total = log_total; + h->commit = commit; + for (unsigned i = 0; stage && stage[i] && i < sizeof h->stage - 1; i++) + h->stage[i] = stage[i]; +} diff --git a/native/kernel/core/klog_slot.h b/native/kernel/core/klog_slot.h new file mode 100644 index 00000000..e52f56a8 --- /dev/null +++ b/native/kernel/core/klog_slot.h @@ -0,0 +1,69 @@ +/* klog_slot.h -- where the kernel may write its log on the boot card, and + * the checks that keep it from writing anywhere else (Raspberry Pi 5 build). + * + * The host side (the run tool) creates a contiguous file on the card's FAT + * volume, writes a slot header into its first sector with a fresh random + * nonce, and writes \EFI\AIENOS\KLOG.MAP naming the file's first absolute + * sector on the whole card, its length and the same nonce. The UEFI stub + * reads the map before ExitBootServices (native/boot/efi_klog.c). The + * kernel writes only after it has read the slot's first sector through its + * own SD driver and found the header with this map's nonce, sector and + * length (ck_klog_hdr_match): a stale map, a moved file, another card or a + * wrong sector number all refuse, and nothing is written. + * + * Slot layout: sector 0 header, sectors 1..count-1 log text. The kernel + * writes the text first and the header (state WRITTEN, lengths) last. + * Both structures are 512 bytes, little endian. */ +#ifndef AIENOS_CK_KLOG_SLOT_H +#define AIENOS_CK_KLOG_SLOT_H +#include +#include + +#define CK_KLOG_MAP_MAGIC "AIENKLM1" +#define CK_KLOG_HDR_MAGIC "AIENKLG1" +#define CK_KLOG_SLOT_VERSION 1u +#define CK_KLOG_MAP_PATH "\\EFI\\AIENOS\\KLOG.MAP" +#define CK_KLOG_MIN_SECTORS 2u +#define CK_KLOG_MAX_SECTORS 8192u /* 4 MiB */ + +enum { CK_KLOG_STATE_EMPTY = 0, CK_KLOG_STATE_WRITTEN = 1 }; + +struct ck_klog_map { + uint8_t magic[8]; /* "AIENKLM1" */ + uint32_t version; /* 1 */ + uint32_t sector_size; /* 512 */ + uint64_t lba; /* slot sector 0, absolute on the whole card */ + uint64_t count; /* slot sectors, CK_KLOG_MIN_SECTORS..CK_KLOG_MAX_SECTORS */ + uint8_t nonce[16]; + uint8_t pad[512 - 48]; +}; + +struct ck_klog_hdr { + uint8_t magic[8]; /* "AIENKLG1" */ + uint32_t version; /* 1 */ + uint32_t state; /* CK_KLOG_STATE_* */ + uint8_t nonce[16]; /* equals the map's */ + uint64_t lba; /* equals the map's: a copy of this sector elsewhere never matches */ + uint64_t count; /* equals the map's */ + uint64_t log_len; /* text bytes in sectors 1.. (state WRITTEN) */ + uint64_t log_total; /* bytes the console produced, kept or not */ + uint64_t commit; /* build commit (ck_commit) */ + char stage[32]; /* boot stage at the write, NUL terminated */ + uint8_t pad[512 - 104]; +}; + +_Static_assert(sizeof(struct ck_klog_map) == 512, "ck_klog_map size"); +_Static_assert(sizeof(struct ck_klog_hdr) == 512, "ck_klog_hdr size"); + +/* Structural map check. last_block 0 skips the disk-size comparison. + * 0 or -1 with *why. */ +int ck_klog_map_check(const struct ck_klog_map *m, uint64_t last_block, const char **why); +/* The slot's sector 0 as read from the card matches the map (magic, version, + * nonce, lba, count). Any state. 0 or -1 with *why. */ +int ck_klog_hdr_match(const struct ck_klog_hdr *h, const struct ck_klog_map *m, const char **why); +/* Builds the WRITTEN header for a log of log_len bytes. */ +void ck_klog_hdr_written(struct ck_klog_hdr *h, const struct ck_klog_map *m, uint64_t log_len, + uint64_t log_total, uint64_t commit, const char *stage); +/* Text capacity of the slot in bytes. */ +static inline uint64_t ck_klog_slot_bytes(const struct ck_klog_map *m) { return (m->count - 1u) * 512u; } +#endif diff --git a/native/kernel/core/kmain.c b/native/kernel/core/kmain.c index 84831d77..e580cf53 100644 --- a/native/kernel/core/kmain.c +++ b/native/kernel/core/kmain.c @@ -122,6 +122,8 @@ static __attribute__((noreturn)) void ck_el1_main(void *arg) ck_mm_el1_ready(); const struct ck_mm_report *mm = ck_mm_report(); screen_start(h, mm); + if (ck_pi5_start) + ck_pi5_start(); ck_puts("\n"); ck_report_header("kernel"); diff --git a/native/kernel/core/pi5.c b/native/kernel/core/pi5.c new file mode 100644 index 00000000..e669ac50 --- /dev/null +++ b/native/kernel/core/pi5.c @@ -0,0 +1,153 @@ +/* pi5.c -- Raspberry Pi 5 board glue (CK_PI5 build only). + * + * ck_pi5_start arm the PM watchdog (dev/pi5_wdt.c), bring up the + * microSD controller with native/disk/disk_sdhci.c + * ck_pi5_stage pet the watchdog at every boot stage + * ck_pi5_before_reset write the RAM console log (core/klog.c) into the + * card's log slot (core/klog_slot.h), then reset + * + * The SD host address is fixed for this board: the firmware's ACPI DSDT + * (\_SB.SDC0, dumped by AIENOS-PI5-ACPI-0, 2026-10-13) computes its _CRS in a + * method the kernel's static scanner does not run, and gives the same range + * as Linux bcm2712.dtsi sdio1 (mmc@fff000, 0x260 bytes, soc window + * 0x0 -> 0x10_0000_0000). Its clock is clk_emmc2, a fixed 200 MHz + * (bcm2712.dtsi); the CAPABILITIES base clock is printed beside it. The + * driver is PIO only: no DMA, so the missing SMMU does not matter here. */ +#ifndef CK_PI5 +#error "pi5.c is Raspberry Pi 5 only (CK_PI5)" +#endif +#include "ck_internal.h" +#include "klog.h" +#include "klog_boot.h" +#include "klog_slot.h" +#include "../dev/pi5_wdt.h" +#include "disk.h" +#include "disk_sdhci.h" + +#define PI5_SD_PHYS 0x1000fff000ull +#define PI5_SD_CLOCK_HZ 200000000u + +static volatile uint8_t *sd_regs; +static sdhci_card sd; +static int sd_up; + +static uint32_t sd_r32(void *c, uint32_t o) { (void)c; return *(volatile uint32_t *)(sd_regs + o); } +static void sd_w32(void *c, uint32_t o, uint32_t v) { (void)c; *(volatile uint32_t *)(sd_regs + o) = v; } +static void sd_delay(void *c, uint32_t us) { (void)c; ck_udelay(us); } +static const sdhci_ops sd_ops = {0, sd_r32, sd_w32, sd_delay}; + +void ck_pi5_start(void) +{ + if (ck_pi5_wdt_arm() == 0) + ck_printf("pi5_watchdog: armed %u s (PM block 0x%llx), petted at every stage\n", (unsigned)CK_PI5_WDT_S, + (unsigned long long)CK_PI5_PM_PHYS); + else + ck_printf("pi5_watchdog: FAIL map 0x%llx; a hang now needs a power cycle\n", + (unsigned long long)CK_PI5_PM_PHYS); + sd_regs = ck_mmio_map(PI5_SD_PHYS, 0x1000); + if (!sd_regs) { + ck_printf("pi5_sd: FAIL map 0x%llx\n", (unsigned long long)PI5_SD_PHYS); + return; + } + uint32_t caps = sd_r32(0, 0x40), ver = sd_r32(0, 0xfc) >> 16; + ck_printf("pi5_sd: host 0x%llx version=0x%x caps=0x%08x caps_base_mhz=%u using %u MHz (clk_emmc2)\n", + (unsigned long long)PI5_SD_PHYS, ver, caps, (caps >> 8) & 0xff, PI5_SD_CLOCK_HZ / 1000000u); + sdhci_cfg cfg = {PI5_SD_CLOCK_HZ, 0, 1, 0}; + int rc = sdhci_init(&sd, &sd_ops, &cfg); + if (rc) { + ck_printf("pi5_sd: init FAIL rc=%d last_cmd=%d last_int=0x%08x\n", rc, sd.last_cmd, sd.last_int); + return; + } + sd_up = 1; + ck_printf("pi5_sd: card ready spec=%u card_hz=%u high_capacity=%d blocks=%llu (%llu MiB) rca=0x%x bus=4bit\n", + sd.spec, sd.card_hz, sd.high_capacity, (unsigned long long)sd.blocks, + (unsigned long long)(sd.blocks / 2048u), sd.rca); + /* Read-only check: sector 0 of the card is an MBR (0x55 0xaa). */ + static uint8_t s0[512] __attribute__((aligned(64))); + rc = disk_read(&sd.dev, 0, 1, s0); + ck_printf("pi5_sd: read sector 0 rc=%d signature=%02x%02x (%s)\n", rc, s0[510], s0[511], + rc == 0 && s0[510] == 0x55 && s0[511] == 0xaa ? "MBR ok" : "no MBR signature"); +} + +void ck_pi5_stage(const char *name) +{ + (void)name; + ck_pi5_wdt_pet(); +} + +static uint64_t commit_prefix(void) +{ + const char *c = ck_commit(); + uint64_t v = 0; + for (int i = 0; i < 16 && c[i]; i++) { + char x = c[i]; + unsigned d = x >= '0' && x <= '9' ? (unsigned)(x - '0') : x >= 'a' && x <= 'f' ? (unsigned)(x - 'a' + 10) : 16; + if (d > 15) + break; + v = v << 4 | d; + } + return v; +} + +static char text[CK_KLOG_MAX + 512] __attribute__((aligned(64))); +static struct ck_klog_hdr hdr __attribute__((aligned(64))); + +/* 0 written and read back; 1 nothing to do (no slot / no card); -1 refused or failed. */ +static int klog_write(void) +{ + const struct ck_klog_boot *kb = &ck_klog_boot; + const struct ck_klog_map *m = &kb->map; + const char *why = 0; + if (kb->state != CK_KLOG_BOOT_FOUND) { + ck_printf("klog_card: skipped (stub state %d: no checked KLOG.MAP)\n", kb->state); + return 1; + } + if (!sd_up) { + ck_puts("klog_card: skipped (SD card not ready)\n"); + return 1; + } + if (m->lba + m->count > sd.blocks || kb->disk_last_block + 1u != sd.blocks) { + ck_printf("klog_card: refused (card has %llu sectors, firmware saw %llu, slot ends at %llu)\n", + (unsigned long long)sd.blocks, (unsigned long long)(kb->disk_last_block + 1u), + (unsigned long long)(m->lba + m->count)); + return -1; + } + if (disk_read(&sd.dev, m->lba, 1, (uint8_t *)&hdr) != DISK_OK || ck_klog_hdr_match(&hdr, m, &why)) { + ck_printf("klog_card: refused (slot header check: %s)\n", why ? why : "read failed"); + return -1; + } + ck_printf("klog_card: slot verified by the kernel's own SD driver (lba=%llu sectors=%llu); writing\n", + (unsigned long long)m->lba, (unsigned long long)m->count); + uint64_t cap = ck_klog_slot_bytes(m); + size_t n = ck_klog_read(text, cap < CK_KLOG_MAX ? (size_t)cap : CK_KLOG_MAX); + uint32_t secs = (uint32_t)((n + 511u) / 512u); + for (size_t i = n; i < (size_t)secs * 512u; i++) + text[i] = 0; + if (secs && disk_write(&sd.dev, m->lba + 1u, secs, (const uint8_t *)text) != DISK_OK) + return -1; + if (disk_flush(&sd.dev) != DISK_OK) + return -1; + ck_klog_hdr_written(&hdr, m, n, ck_klog_total(), commit_prefix(), ck_stage_name()); + if (disk_write(&sd.dev, m->lba, 1, (const uint8_t *)&hdr) != DISK_OK || disk_flush(&sd.dev) != DISK_OK) + return -1; + static struct ck_klog_hdr back __attribute__((aligned(64))); + if (disk_read(&sd.dev, m->lba, 1, (uint8_t *)&back) != DISK_OK) + return -1; + const uint8_t *a = (const uint8_t *)&hdr, *b = (const uint8_t *)&back; + for (unsigned i = 0; i < sizeof hdr; i++) + if (a[i] != b[i]) + return -1; + return 0; +} + +void ck_pi5_before_reset(void) +{ + static int once; + if (once++) + return; /* a fault inside the write resets without a second try */ + ck_pi5_wdt_pet(); + int rc = klog_write(); + /* This line reaches the screen and UART only: the card copy ends above. */ + ck_printf("klog_card: %s\n", rc == 0 ? "written and read back" : rc > 0 ? "not written" : "FAILED"); + ck_pi5_wdt_pet(); +} diff --git a/native/kernel/core/report.c b/native/kernel/core/report.c index d38371b5..a10c4e80 100644 --- a/native/kernel/core/report.c +++ b/native/kernel/core/report.c @@ -14,7 +14,12 @@ static int in_fatal; const char *ck_commit(void) { return AIENOS_COMMIT; } uint32_t ck_boot_count_hint(void) { return 0; } -void ck_set_stage(const char *name) { stage = name; } +void ck_set_stage(const char *name) +{ + stage = name; + if (ck_pi5_stage) + ck_pi5_stage(name); +} const char *ck_stage_name(void) { return stage; } void ck_report_header(const char *kind) @@ -66,6 +71,8 @@ void ck_reset(void) quiescing = 1; /* a panic inside the hook resets without it */ ck_stage_quiesce(); } + if (ck_pi5_before_reset) + ck_pi5_before_reset(); ck_console_fb_hold(); ck_mb(); psci_call(0x84000009u); /* PSCI SYSTEM_RESET */ diff --git a/native/kernel/dev/.crumb b/native/kernel/dev/.crumb index 2b928683..1c59867e 100644 --- a/native/kernel/dev/.crumb +++ b/native/kernel/dev/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-12T20:56:51Z", + "compiled_at": "2026-10-13T06:35:07Z", "compiler": "crumb-compile/1", - "digest": "sha256:d7b81a8f888e7b4ca5337a5c9279671c33e1f4a70a7318ed20e1c5367c8a289b", + "digest": "sha256:7f3dc4d9ecbeba802bd8a3c59514ff336ea615e8aca587c1c998e660abde23c3", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 27, - "generated_at_commit": "04e39e7223149deef6c14e4175d40e80c63eee0a", - "source_tree": "sha256:0805a014ffb340ed6b5f1cba3e6368c77f7d280bc1a962302156f95a6852f936" + "files": 29, + "generated_at_commit": "93ae0323091b1f40c93fbc3a2bdfaaa48745c61a", + "source_tree": "sha256:29c8c27228d50dd45d39070ffd23961fa57821564f2e3b43b1e706b8418c5ba3" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/dev/pi5_wdt.c b/native/kernel/dev/pi5_wdt.c new file mode 100644 index 00000000..7faba7ae --- /dev/null +++ b/native/kernel/dev/pi5_wdt.c @@ -0,0 +1,33 @@ +/* pi5_wdt.c -- see pi5_wdt.h. Pi 5 build only. */ +#ifndef CK_PI5 +#error "pi5_wdt.c is Raspberry Pi 5 only (CK_PI5); never build it for another board" +#endif +#include "pi5_wdt.h" +#include "ck.h" + +static volatile uint8_t *pm; + +static uint32_t rd(uint32_t off) { return *(volatile uint32_t *)(pm + off); } +static void wr(uint32_t off, uint32_t v) { *(volatile uint32_t *)(pm + off) = v; } + +int ck_pi5_wdt_arm(void) +{ + pm = ck_mmio_map(CK_PI5_PM_PHYS, 0x1000); + if (!pm) + return -1; + ck_pi5_wdt_pet(); + wr(CK_PI5_PM_RSTC, ck_pi5_rstc_arm(rd(CK_PI5_PM_RSTC))); + return 0; +} + +void ck_pi5_wdt_pet(void) +{ + if (pm) + wr(CK_PI5_PM_WDOG, ck_pi5_wdog_value(CK_PI5_WDT_S)); +} + +void ck_pi5_wdt_stop(void) +{ + if (pm) + wr(CK_PI5_PM_RSTC, CK_PI5_PM_PASSWORD | CK_PI5_PM_RSTC_RESET); +} diff --git a/native/kernel/dev/pi5_wdt.h b/native/kernel/dev/pi5_wdt.h new file mode 100644 index 00000000..c9f65e2a --- /dev/null +++ b/native/kernel/dev/pi5_wdt.h @@ -0,0 +1,42 @@ +/* pi5_wdt.h -- Raspberry Pi 5 (BCM2712) PM watchdog, Pi 5 build only + * (CK_PI5). Armed at kernel start and petted at every boot stage + * (ck_set_stage), so a kernel that stops making progress resets the board + * within CK_PI5_WDT_S seconds; the Pi then starts its normal system (the + * one-shot tryboot flag is already cleared). Never built for the Spark or + * QEMU: the address below is BCM2712-only. + * Registers and sequence: Linux drivers/watchdog/bcm2835_wdt.c + * (bcm2835_wdt_start / _stop); address: bcm2712.dtsi watchdog@7d200000 in + * the soc bus window 0x0 -> 0x10_0000_0000. */ +#ifndef AIENOS_CK_PI5_WDT_H +#define AIENOS_CK_PI5_WDT_H +#include + +#define CK_PI5_PM_PHYS 0x107d200000ull +#define CK_PI5_PM_RSTC 0x1cu +#define CK_PI5_PM_WDOG 0x24u +#define CK_PI5_PM_PASSWORD 0x5a000000u +#define CK_PI5_PM_WDOG_TIME_SET 0x000fffffu +#define CK_PI5_PM_RSTC_WRCFG_CLR 0xffffffcfu +#define CK_PI5_PM_RSTC_WRCFG_FULL_RESET 0x00000020u +#define CK_PI5_PM_RSTC_RESET 0x00000102u +#ifndef CK_PI5_WDT_S +#define CK_PI5_WDT_S 15u /* the counter holds at most 0xfffff ticks of 1/65536 s: just under 16 s */ +#endif + +/* Register values (pure, host-tested). */ +static inline uint32_t ck_pi5_wdog_value(uint32_t secs) +{ + return CK_PI5_PM_PASSWORD | ((secs << 16) & CK_PI5_PM_WDOG_TIME_SET); +} +static inline uint32_t ck_pi5_rstc_arm(uint32_t cur) +{ + return CK_PI5_PM_PASSWORD | (cur & CK_PI5_PM_RSTC_WRCFG_CLR) | CK_PI5_PM_RSTC_WRCFG_FULL_RESET; +} + +/* Maps the PM block and arms the watchdog. 0, or -1 if the map failed. */ +int ck_pi5_wdt_arm(void); +/* Restarts the countdown (no-op before arm). */ +void ck_pi5_wdt_pet(void); +/* Stops the watchdog (before an orderly PSCI reset or a long operator hold). */ +void ck_pi5_wdt_stop(void); +#endif diff --git a/native/kernel/include/.crumb b/native/kernel/include/.crumb index 4328a95e..e5121632 100644 --- a/native/kernel/include/.crumb +++ b/native/kernel/include/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-12T19:04:55Z", + "compiled_at": "2026-10-13T06:35:07Z", "compiler": "crumb-compile/1", - "digest": "sha256:283a167327166bcfd270869cc04a1a03ea21366248471464db7ef1cf37058ada", + "digest": "sha256:d3e1b6aa9a623c95208e730cea917c2824682caa6518dd4433882908b5facef3", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 1, - "generated_at_commit": "44dc7ee624d1b72eb3930cd35f55629c2c12a008", - "source_tree": "sha256:84ac1b9b8674343ec32004b9f305a96323108208013ad5a67d0ddc32aa17da8b" + "generated_at_commit": "93ae0323091b1f40c93fbc3a2bdfaaa48745c61a", + "source_tree": "sha256:4f93ee5ff5b711698b9dcd9e42926cee86d3288605c711dbd86dd4c7be77cc32" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/include/ck.h b/native/kernel/include/ck.h index 9c91fb91..968668c4 100644 --- a/native/kernel/include/ck.h +++ b/native/kernel/include/ck.h @@ -212,6 +212,12 @@ __attribute__((weak)) void ck_slot_boot_capture(const struct ck_handoff *h); * then returns 0 so kmain prints the final report and resets. Absent (weak) in * every diagnostic image. */ __attribute__((weak)) int ck_lifecycle_run(void); +/* Raspberry Pi 5 board glue (core/pi5.c, CK_PI5 build only): watchdog arm and + * SD bring-up, the watchdog pet at every stage, and the console log written + * to the card before the reset. Absent (weak) in every other image. */ +__attribute__((weak)) void ck_pi5_start(void); +__attribute__((weak)) void ck_pi5_stage(const char *name); +__attribute__((weak)) void ck_pi5_before_reset(void); /* Console serial receive (core/console.c + core/uart_rx.h), POLLED, no * interrupts. It uses the UART the transmit side chose from the ACPI SPCR: diff --git a/native/kernel/tests/.crumb b/native/kernel/tests/.crumb index 19b0d843..0424aac7 100644 --- a/native/kernel/tests/.crumb +++ b/native/kernel/tests/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-13T04:45:02Z", + "compiled_at": "2026-10-13T06:35:07Z", "compiler": "crumb-compile/1", - "digest": "sha256:da6470d80184ef3a173623e87a9712d4643f67d73dbab6be80f7e41902fc81e4", + "digest": "sha256:46def7a41aefac1c42a8bda757406960c6f03147fc147b860c94e8166febde16", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 151, - "generated_at_commit": "391fcd54707b2ba006e0652c71259a17ef78ec6a", - "source_tree": "sha256:63ed02b105fb0934e800ed8c6f3656af5fe7fb9c1ff1c101f8aa227fc146fd4f" + "files": 154, + "generated_at_commit": "93ae0323091b1f40c93fbc3a2bdfaaa48745c61a", + "source_tree": "sha256:e865db6add5a3a31d86e15171163edc371fcab8a67f4a1bf5521a3d4b4942858" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tests/test_klog.c b/native/kernel/tests/test_klog.c new file mode 100644 index 00000000..ad45dbf8 --- /dev/null +++ b/native/kernel/tests/test_klog.c @@ -0,0 +1,54 @@ +/* test_klog.c -- core/klog.c: head kept in order, ring keeps the newest + * bytes, the dropped-bytes line, the cap. */ +#include +#include +#include "ck_test.h" +#include "klog.h" + +static char out[CK_KLOG_MAX + 16]; + +int main(void) +{ + /* Small log: read back exactly. */ + ck_klog_put("hello\n", 6); + ck_klog_put("world\n", 6); + size_t n = ck_klog_read(out, sizeof out); + CHECK(n == 12 && memcmp(out, "hello\nworld\n", 12) == 0); + CHECK(ck_klog_total() == 12); + /* Cap is respected. */ + CHECK(ck_klog_read(out, 5) == 5 && memcmp(out, "hello", 5) == 0); + CHECK(ck_klog_read(out, 0) == 0); + + /* Fill the head exactly, then less than one ring: nothing dropped. */ + ck_klog_clear(); + char *big = malloc(CK_KLOG_HEAD + 3 * CK_KLOG_TAIL); + for (size_t i = 0; i < CK_KLOG_HEAD + 3 * CK_KLOG_TAIL; i++) + big[i] = (char)('a' + i % 23); + ck_klog_put(big, CK_KLOG_HEAD + 100); + n = ck_klog_read(out, sizeof out); + CHECK(n == CK_KLOG_HEAD + 100 && memcmp(out, big, n) == 0); + + /* Overflow the ring: head intact, dropped line, newest CK_KLOG_TAIL bytes. */ + ck_klog_clear(); + size_t put = CK_KLOG_HEAD + 2 * CK_KLOG_TAIL + 777; + for (size_t i = 0; i < put; i += 1000) /* uneven pieces */ + ck_klog_put(big + i, put - i < 1000 ? put - i : 1000); + CHECK(ck_klog_total() == put); + n = ck_klog_read(out, sizeof out); + CHECK(memcmp(out, big, CK_KLOG_HEAD) == 0); + char line[64]; + snprintf(line, sizeof line, "\n[klog: %zu bytes dropped]\n", put - CK_KLOG_HEAD - CK_KLOG_TAIL); + size_t ll = strlen(line); + CHECK(memcmp(out + CK_KLOG_HEAD, line, ll) == 0); + CHECK(n == CK_KLOG_HEAD + ll + CK_KLOG_TAIL); + CHECK(memcmp(out + CK_KLOG_HEAD + ll, big + put - CK_KLOG_TAIL, CK_KLOG_TAIL) == 0); + CHECK(n <= CK_KLOG_MAX); + + /* Exactly one full ring: nothing dropped, no line. */ + ck_klog_clear(); + ck_klog_put(big, CK_KLOG_HEAD + CK_KLOG_TAIL); + n = ck_klog_read(out, sizeof out); + CHECK(n == CK_KLOG_HEAD + CK_KLOG_TAIL && memcmp(out, big, n) == 0); + free(big); + return ck_t_verdict("test_klog"); +} diff --git a/native/kernel/tests/test_klog_slot.c b/native/kernel/tests/test_klog_slot.c new file mode 100644 index 00000000..d5844bd7 --- /dev/null +++ b/native/kernel/tests/test_klog_slot.c @@ -0,0 +1,83 @@ +/* test_klog_slot.c -- core/klog_slot.c: the map check and the header match + * refuse every way a write could land somewhere it should not. */ +#include +#include "ck_test.h" +#include "klog_slot.h" + +static struct ck_klog_map good_map(void) +{ + struct ck_klog_map m; + memset(&m, 0, sizeof m); + memcpy(m.magic, CK_KLOG_MAP_MAGIC, 8); + m.version = 1; + m.sector_size = 512; + m.lba = 123456; + m.count = 4096; + for (int i = 0; i < 16; i++) + m.nonce[i] = (uint8_t)(0xa0 + i); + return m; +} + +static struct ck_klog_hdr empty_hdr(const struct ck_klog_map *m) +{ + struct ck_klog_hdr h; + memset(&h, 0, sizeof h); + memcpy(h.magic, CK_KLOG_HDR_MAGIC, 8); + h.version = 1; + memcpy(h.nonce, m->nonce, 16); + h.lba = m->lba; + h.count = m->count; + return h; +} + +int main(void) +{ + const char *why = 0; + struct ck_klog_map m = good_map(), b; + CHECK(ck_klog_map_check(&m, 0, &why) == 0); + CHECK(ck_klog_map_check(&m, 123456 + 4095, &why) == 0); /* last sector is the card's last */ + CHECK(ck_klog_map_check(&m, 123456 + 4094, &why) == -1 && strstr(why, "past the end")); + b = m; b.magic[0] = 'X'; + CHECK(ck_klog_map_check(&b, 0, &why) == -1 && strstr(why, "magic")); + b = m; b.version = 2; + CHECK(ck_klog_map_check(&b, 0, &why) == -1 && strstr(why, "version")); + b = m; b.sector_size = 4096; + CHECK(ck_klog_map_check(&b, 0, &why) == -1 && strstr(why, "sector size")); + b = m; b.count = 1; + CHECK(ck_klog_map_check(&b, 0, &why) == -1 && strstr(why, "length")); + b = m; b.count = CK_KLOG_MAX_SECTORS + 1; + CHECK(ck_klog_map_check(&b, 0, &why) == -1 && strstr(why, "length")); + b = m; b.lba = 0; + CHECK(ck_klog_map_check(&b, 0, &why) == -1 && strstr(why, "sector 0")); + b = m; b.lba = UINT64_MAX - 10; + CHECK(ck_klog_map_check(&b, 0, &why) == -1 && strstr(why, "overflows")); + b = m; memset(b.nonce, 0, 16); + CHECK(ck_klog_map_check(&b, 0, &why) == -1 && strstr(why, "nonce")); + + struct ck_klog_hdr h = empty_hdr(&m), c; + CHECK(ck_klog_hdr_match(&h, &m, &why) == 0); + c = h; c.magic[7] = '2'; + CHECK(ck_klog_hdr_match(&c, &m, &why) == -1 && strstr(why, "no slot header")); + c = h; c.version = 9; + CHECK(ck_klog_hdr_match(&c, &m, &why) == -1 && strstr(why, "version")); + c = h; c.nonce[15] ^= 1; /* another run's slot, or another card */ + CHECK(ck_klog_hdr_match(&c, &m, &why) == -1 && strstr(why, "nonce")); + c = h; c.lba++; /* this header was copied somewhere else */ + CHECK(ck_klog_hdr_match(&c, &m, &why) == -1 && strstr(why, "position")); + c = h; c.count--; + CHECK(ck_klog_hdr_match(&c, &m, &why) == -1 && strstr(why, "position")); + struct ck_klog_hdr z; + memset(&z, 0, sizeof z); /* blank sector: a wrong LBA on a fresh card */ + CHECK(ck_klog_hdr_match(&z, &m, &why) == -1); + + /* The WRITTEN header still matches the map (a rerun can find it). */ + struct ck_klog_hdr w; + memset(&w, 0x5a, sizeof w); + ck_klog_hdr_written(&w, &m, 1000, 2000, 0x1234, "final_stage_name_that_is_far_too_long_to_fit"); + CHECK(ck_klog_hdr_match(&w, &m, &why) == 0); + CHECK(w.state == CK_KLOG_STATE_WRITTEN && w.log_len == 1000 && w.log_total == 2000 && w.commit == 0x1234); + CHECK(w.stage[sizeof w.stage - 1] == 0 && strncmp(w.stage, "final_stage", 11) == 0); + CHECK(w.pad[0] == 0 && w.pad[sizeof w.pad - 1] == 0); + CHECK(ck_klog_slot_bytes(&m) == 4095u * 512u); + return ck_t_verdict("test_klog_slot"); +} diff --git a/native/kernel/tests/test_pi5.c b/native/kernel/tests/test_pi5.c new file mode 100644 index 00000000..5179d4a1 --- /dev/null +++ b/native/kernel/tests/test_pi5.c @@ -0,0 +1,18 @@ +/* test_pi5.c -- Pi 5 build register values (dev/pi5_wdt.h) against the + * Linux bcm2835_wdt.c sequence: PM_WDOG = password | (secs << 16 & 0xfffff), + * PM_RSTC = password | (cur & ~0x30) | 0x20. */ +#include "ck_test.h" +#include "../dev/pi5_wdt.h" + +int main(void) +{ + CHECK(ck_pi5_wdog_value(15) == (0x5a000000u | 0x000f0000u)); + CHECK(ck_pi5_wdog_value(1) == 0x5a010000u); + CHECK(ck_pi5_wdog_value(16) == 0x5a000000u); /* 16 s does not fit: shows why the default is 15 */ + CHECK(CK_PI5_WDT_S <= 15u && CK_PI5_WDT_S >= 1u); + CHECK(ck_pi5_rstc_arm(0x00000000u) == 0x5a000020u); + CHECK(ck_pi5_rstc_arm(0xffffffffu) == (0x5a000000u | 0xffffffcfu | 0x20u)); + CHECK(ck_pi5_rstc_arm(0x00000030u) == 0x5a000020u); /* old WRCFG bits cleared first */ + CHECK(CK_PI5_PM_PHYS == 0x107d200000ull); + return ck_t_verdict("test_pi5"); +} diff --git a/scripts/.crumb b/scripts/.crumb index 1c034ecc..82ede34c 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-13T05:44:11Z", + "compiled_at": "2026-10-13T06:35:07Z", "compiler": "crumb-compile/1", - "digest": "sha256:80f03d413b7d49f38058ecbb8c712c6889dab80c227cf9f22999eaa885183abb", + "digest": "sha256:d2fbf98209136e9f0f16f6f5a8a0938a9b711f46798e0d74fb0344f13dedb210", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 91, - "generated_at_commit": "1994c4f8c4e8521d7febed45306515aa5085f207", - "source_tree": "sha256:c2a6ab2e0eb878412e4f28aebc36f452d1dc07c12f60ca6d874b00223701dac6" + "files": 92, + "generated_at_commit": "93ae0323091b1f40c93fbc3a2bdfaaa48745c61a", + "source_tree": "sha256:4e4695ef692ccba30cd343e0b816e72d4fb4b81b1540d1862240b9f7b9c872b4" }, "owner_lane": "", "provenance": { diff --git a/scripts/ck_pi5_build_check.sh b/scripts/ck_pi5_build_check.sh new file mode 100755 index 00000000..5ea5220f --- /dev/null +++ b/scripts/ck_pi5_build_check.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# ck_pi5_build_check.sh -- AIENOS-PI5 step 1, build only. +# +# Builds the Raspberry Pi 5 kernel image (make -C native/kernel pi5) and checks: +# 1. the Pi image defines the board glue: watchdog, SD host, card log slot; +# 2. the default image (make -C native/kernel all) defines none of it, so the +# Spark and QEMU builds carry no Pi code (the hooks are weak and absent). +# The Pi image cannot boot in QEMU (its SD host and PM watchdog addresses are +# Pi 5 only); it is run on the hub Pi with the one-shot tryboot runner. +set -euo pipefail +root="$(cd "$(dirname "$0")/.." && pwd)" +make -C "${root}/native/kernel" all >/dev/null +make -C "${root}/native/kernel" pi5 >/dev/null +def="${root}/target/native-kernel/aienos-ck.elf" +pi5="${root}/target/native-kernel-pi5/aienos-ck.elf" +syms="ck_pi5_start ck_pi5_stage ck_pi5_before_reset ck_pi5_wdt_arm ck_pi5_wdt_pet ck_boot_klog_find sdhci_init ck_klog_put ck_klog_hdr_match" +fail=0 +defined() { nm "$1" | awk -v s="$2" '$3==s && $2 ~ /^[TtDdBbRrW]$/ {f=1} END {exit !f}'; } +for s in ${syms}; do + if defined "${pi5}" "${s}"; then echo "PASS pi5 image defines ${s}"; else echo "FAIL pi5 image lacks ${s}"; fail=1; fi + if defined "${def}" "${s}"; then echo "FAIL default image defines ${s}"; fail=1; else echo "PASS default image has no ${s}"; fi +done +test -s "${root}/target/native-kernel-pi5/BOOTAA64.EFI" || { echo "FAIL no Pi BOOTAA64.EFI"; fail=1; } +if [[ ${fail} == 0 ]]; then echo "CK_PI5_BUILD: PASS"; else echo "CK_PI5_BUILD: FAIL"; exit 1; fi