Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/patches/25614207d3fa50f4 # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/aienux#346 (code only) closed derived `25614207d3fa50f4` #346 Imported from GitHub aien-dev/aienux#346 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `502d1682153b03146c421e5eba4555a9b1ba1c4b` · Head `78be729f58b73103e97539c9f86a1c70a249a2cc` ### Apply it ``` git checkout -b patch-25614207d3fa50f4 502d1682153b03146c421e5eba4555a9b1ba1c4b curl -fsSL https://interplane.io/drakestapleton/aienux/patches/25614207d3fa50f4.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/aienux 346 78be729f58b73103e97539c9f86a1c70a249a2cc ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"aienux","number":346,"url":"https://github.com/aien-dev/aienux/pull/346","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:14Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.crumb b/.crumb index cd84d576..12a7a54d 100644 --- a/.crumb +++ b/.crumb @@ -33,27 +33,27 @@ "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "352a48dff4b4321e98d02b4f76fccf571ae86d86560d6850cdd042ec766cd49e", - "last_commit": "93ae032", + "digest": "f6dacf501f99cc29eabd70f9baa5f6bfe7ae76ee224a5e7f5094bf95f1668371", + "last_commit": "16cf1b2", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { - "digest": "d2fbf98209136e9f0f16f6f5a8a0938a9b711f46798e0d74fb0344f13dedb210", - "last_commit": "93ae032", + "digest": "197bb0910cb0164678c40a278c02240cd8f147d407d2322be38ce7a66289316e", + "last_commit": "27f24e8", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:9872d818d301951efbb3310a64db9bfcefb116f94b14619e10033034bfe606ec", - "compiled_at": "2026-10-13T06:35:07Z", + "children_root": "sha256:3ef11bc2637d50d288ed541ccd6f00c461d1480632f1af0497def463c28982db", + "compiled_at": "2026-10-13T07:56:49Z", "compiler": "crumb-compile/1", - "digest": "sha256:a331ea8e8a5e5679e0f4d0226e99baa441c9c83f354050c20b78127802b2bc76", + "digest": "sha256:9bb4345a08dabb6f996dea01e74e79ab55b8b050ecee977ed148e57b08066a1e", "evidence_files": 156, "evidence_root": "sha256:cd53ab119be9de6ce28c4f5949b106c9622f1fe105611de3de21c3bce7932694", - "files": 3137, - "generated_at_commit": "93ae0323091b1f40c93fbc3a2bdfaaa48745c61a", - "source_tree": "sha256:ed701cf9671da05d77fbadb88364beb56553bbcc9e7c22099400cfa3bd1345e8" + "files": 3140, + "generated_at_commit": "16cf1b2353974f0f22f25fd2fc992d594271a2a7", + "source_tree": "sha256:77aefea2773ae6c4091a81cca2a50cff3793f11e31ced38d1dd1b14a076d9d00" }, "owner_lane": "", "provenance": { diff --git a/native/.crumb b/native/.crumb index 75d3db7a..58d2e027 100644 --- a/native/.crumb +++ b/native/.crumb @@ -56,7 +56,7 @@ }, { "digest": "aa53401411b940e8361373c4e4325edf91b649c86ca6047dbe1c2dbb10f5c2de", - "last_commit": "93ae032", + "last_commit": "502d168", "name": "boot", "purpose": "Directory native/boot. Purpose not yet described by a human." }, @@ -79,8 +79,8 @@ "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "65c8ebf17ec712136e00e44cc326b1f2acad36e50b15af71e13c692e5bfa5add", - "last_commit": "93ae032", + "digest": "26315dfb3c6aea258c8e786146c9768fcdfcc48fc93b270f4d74add688327909", + "last_commit": "16cf1b2", "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:02036a0be67ea05b04ac7ad5a176ca56b01d0d84f80e6eddca834ba7ad9c9a60", - "compiled_at": "2026-10-13T06:35:07Z", + "children_root": "sha256:7b6572d0cdc9e1fdcd29bfb3f6e8bead19e3e087eb06aabfb3269b2e1d2bed2e", + "compiled_at": "2026-10-13T07:56:49Z", "compiler": "crumb-compile/1", - "digest": "sha256:352a48dff4b4321e98d02b4f76fccf571ae86d86560d6850cdd042ec766cd49e", + "digest": "sha256:f6dacf501f99cc29eabd70f9baa5f6bfe7ae76ee224a5e7f5094bf95f1668371", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", - "files": 505, - "generated_at_commit": "93ae0323091b1f40c93fbc3a2bdfaaa48745c61a", - "source_tree": "sha256:29539d34a7aac0f425e45e8480a7800d347610a67521497817c1b2abd6e311ef" + "files": 508, + "generated_at_commit": "16cf1b2353974f0f22f25fd2fc992d594271a2a7", + "source_tree": "sha256:ef0853bbc050a53e6f4b776b44290826c336832a9a27b0ba60647a472787c6ff" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index 87b7e81a..5a315759 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -57,20 +57,20 @@ "purpose": "Directory native/kernel/artifact. Purpose not yet described by a human." }, { - "digest": "ba9f55f3590752efdda812ebdd12f1bca77776fbd684c944babe07da32e24e1f", - "last_commit": "93ae032", + "digest": "6d0ed43b6dacbc54818db5b34531768fa2d0acf992e386b488ed66c61be7e3f7", + "last_commit": "27f24e8", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, { "digest": "7f3dc4d9ecbeba802bd8a3c59514ff336ea615e8aca587c1c998e660abde23c3", - "last_commit": "93ae032", + "last_commit": "502d168", "name": "dev", "purpose": "Directory native/kernel/dev. Purpose not yet described by a human." }, { "digest": "d3e1b6aa9a623c95208e730cea917c2824682caa6518dd4433882908b5facef3", - "last_commit": "93ae032", + "last_commit": "502d168", "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": "46def7a41aefac1c42a8bda757406960c6f03147fc147b860c94e8166febde16", - "last_commit": "93ae032", + "digest": "90c571fe56a5ba0d0fc8b68ded88e8006bb1be273725222d6a49c9c4c03ae350", + "last_commit": "27f24e8", "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:565586a153ff32956bee505d1da9e43cb94da550bfa9dd7b354a16012ca49586", - "compiled_at": "2026-10-13T06:35:07Z", + "children_root": "sha256:f270e5c733f1f2230a1fbcd92d52ca923abcdf121dcdcebd3cadd00afae97c02", + "compiled_at": "2026-10-13T07:56:49Z", "compiler": "crumb-compile/1", - "digest": "sha256:65c8ebf17ec712136e00e44cc326b1f2acad36e50b15af71e13c692e5bfa5add", + "digest": "sha256:26315dfb3c6aea258c8e786146c9768fcdfcc48fc93b270f4d74add688327909", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 328, - "generated_at_commit": "93ae0323091b1f40c93fbc3a2bdfaaa48745c61a", - "source_tree": "sha256:3876081051a14ec59246a47ffcd8b5f393acf291e95fd1d94d1a4b04807f6873" + "files": 331, + "generated_at_commit": "16cf1b2353974f0f22f25fd2fc992d594271a2a7", + "source_tree": "sha256:75486b99242892b6c2dd39f783caa7598fb73f1e1505f1ef5c942af5c4c99e91" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 2a4b1c55..6b4604be 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -653,7 +653,7 @@ CORE_C := $(BOOT)/efi_main.c $(BOOT)/efi_model.c $(BOOT)/efi_gop.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 \ +CORE_C += $(BOOT)/efi_klog.c $(KDIR)/core/pi5.c $(KDIR)/core/pi5_marker.c $(KDIR)/dev/disk_part.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 @@ -804,6 +804,8 @@ $(ART_OBJS): $(ART_HDRS) # core/pi5.c drives the SD card through native/disk. $(KOBJ)/kernel/core/pi5.o: KCFLAGS += -I$(NATIVE)/disk +$(KOBJ)/kernel/core/pi5_marker.o: KCFLAGS += -I$(NATIVE)/disk +$(KOBJ)/kernel/dev/disk_part.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 @@ -1056,7 +1058,7 @@ HOST_SRCS := $(KDIR)/core/fmt.c $(KDIR)/core/acpi.c $(KDIR)/core/smmu.c $(KDIR)/ $(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_klog test_klog_slot test_pi5 + test_continuity_subject test_continuity_subject_provision test_lifecycle test_console_rx test_klog test_klog_slot test_pi5 test_pi5_marker 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 @@ -1826,3 +1828,15 @@ clean: FORCE: .PHONY: uefi-vars slot-loader slot-loader-check pi5-diag .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 + +# AIENOS-PI5 step 3 marker + Pi card GPT test (hosted; a file stands in for the card, QEMU cannot model the Pi SD host). +PI5_MARKER_SRCS := $(KDIR)/core/pi5_marker.c $(KDIR)/dev/disk_part.c $(NATIVE)/disk/disk.c $(NATIVE)/disk/disk_file.c \ + $(KDIR)/tools/gpt_write.c +$(OUT)/host/test_pi5_marker: $(KDIR)/tests/test_pi5_marker.c $(PI5_MARKER_SRCS) $(KDIR)/tests/ck_test.h $(KDIR)/core/pi5_marker.h + @mkdir -p $(dir $@) + $(HOSTCC) $(HCFLAGS) -I$(KDIR)/dev -I$(KDIR)/tools -I$(NATIVE)/disk -o $@ $< $(PI5_MARKER_SRCS) +test-pi5-marker: $(OUT)/host/test_pi5_marker + $< +$(OUT)/host-san/test_pi5_marker: $(KDIR)/tests/test_pi5_marker.c $(PI5_MARKER_SRCS) $(KDIR)/tests/ck_test.h $(KDIR)/core/pi5_marker.h + @mkdir -p $(dir $@) + $(HOSTCC) $(HCFLAGS) $(SANFLAGS) -I$(KDIR)/dev -I$(KDIR)/tools -I$(NATIVE)/disk -o $@ $< $(PI5_MARKER_SRCS) diff --git a/native/kernel/core/.crumb b/native/kernel/core/.crumb index 60893290..b2bd6bdb 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-13T06:35:07Z", + "compiled_at": "2026-10-13T07:51:09Z", "compiler": "crumb-compile/1", - "digest": "sha256:ba9f55f3590752efdda812ebdd12f1bca77776fbd684c944babe07da32e24e1f", + "digest": "sha256:6d0ed43b6dacbc54818db5b34531768fa2d0acf992e386b488ed66c61be7e3f7", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 51, - "generated_at_commit": "93ae0323091b1f40c93fbc3a2bdfaaa48745c61a", - "source_tree": "sha256:36fbfaae2071682892468f4ae84c83ec1a508fbdc02acb407e6f1aa513bd4dbc" + "files": 53, + "generated_at_commit": "27f24e8c3b542611bc086755ba3b5693c2125f30", + "source_tree": "sha256:7132acc95cffde6d0b2e2839a5ca2b9272fcbd366c20174003029a65ee116696" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/core/pi5.c b/native/kernel/core/pi5.c index e669ac50..d8b5f4fa 100644 --- a/native/kernel/core/pi5.c +++ b/native/kernel/core/pi5.c @@ -23,6 +23,8 @@ #include "../dev/pi5_wdt.h" #include "disk.h" #include "disk_sdhci.h" +#include "pi5_marker.h" +#include "../dev/disk_part.h" #define PI5_SD_PHYS 0x1000fff000ull #define PI5_SD_CLOCK_HZ 200000000u @@ -67,6 +69,27 @@ void ck_pi5_start(void) 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"); + /* Step 3: the AIENOS partition through the same GPT partition view the NVMe path uses (dev/disk_part.c), + * then the scratch marker at its end (core/pi5_marker.c): formatted on a blank card, reopened after a reboot. */ + static ck_part part; + ck_gpt_info gi; + rc = ck_gpt_find_aienos(&sd.dev, &part, &gi); + if (rc) { + ck_printf("pi5_part: no AIENOS partition, refusing writes (%s, rc=%d)\n", ck_gpt_strerror(rc), rc); + return; + } + 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", + (unsigned long long)part.first_lba, (unsigned long long)part.blocks, + (unsigned long long)(part.blocks / 2048u), gi.used, gi.part_index); + static uint8_t mbuf[CK_PI5_MARKER_BUF_BYTES] __attribute__((aligned(64))); + uint64_t gen = 0; + rc = ck_pi5_marker_run(&part.dev, mbuf, &gen); + if (rc > 0) + ck_printf("pi5_store_layer: %s generation=%llu region=%u sectors at partition block %llu\n", + ck_pi5_marker_strerror(rc), (unsigned long long)gen, CK_PI5_MARKER_SECS, + (unsigned long long)(part.blocks - CK_PI5_MARKER_SECS)); + else + ck_printf("pi5_store_layer: FAIL rc=%d (%s)\n", rc, ck_pi5_marker_strerror(rc)); } void ck_pi5_stage(const char *name) diff --git a/native/kernel/core/pi5_marker.c b/native/kernel/core/pi5_marker.c new file mode 100644 index 00000000..b99b9ed4 --- /dev/null +++ b/native/kernel/core/pi5_marker.c @@ -0,0 +1,127 @@ +/* pi5_marker.c -- see pi5_marker.h. */ +#include "pi5_marker.h" +#include "../dev/disk_part.h" + +#define MAGIC "AIENPI5M" +#define HDR_CRC_BYTES 40u + +static void put64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); } +static void put32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); } +static uint64_t get64(const uint8_t *p) { uint64_t v = 0; for (int i = 7; i >= 0; i--) v = v << 8 | p[i]; return v; } +static uint32_t get32(const uint8_t *p) { uint32_t v = 0; for (int i = 3; i >= 0; i--) v = v << 8 | p[i]; return v; } + +/* splitmix64 of (seed, sector, word): the payload is regenerable. */ +static uint64_t word(uint64_t seed, uint32_t sec, uint32_t w) +{ + uint64_t z = seed + 0x9e3779b97f4a7c15ull * ((uint64_t)sec << 8 | w) + 0x9e3779b97f4a7c15ull; + z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull; + z = (z ^ (z >> 27)) * 0x94d049bb133111ebull; + return z ^ (z >> 31); +} + +static void fill_payload(uint8_t *buf, uint64_t seed) +{ + for (uint32_t s = 1; s < CK_PI5_MARKER_SECS; s++) + for (uint32_t w = 0; w < DISK_SECTOR_BYTES / 8; w++) + put64(buf + (size_t)s * DISK_SECTOR_BYTES + w * 8u, word(seed, s, w)); +} + +static int payload_ok(const uint8_t *buf, uint64_t seed) +{ + for (uint32_t s = 1; s < CK_PI5_MARKER_SECS; s++) + for (uint32_t w = 0; w < DISK_SECTOR_BYTES / 8; w++) + if (get64(buf + (size_t)s * DISK_SECTOR_BYTES + w * 8u) != word(seed, s, w)) + return 0; + return 1; +} + +static void make_header(uint8_t *h, uint64_t gen, uint32_t payload_crc) +{ + for (uint32_t i = 0; i < DISK_SECTOR_BYTES; i++) h[i] = 0; + for (int i = 0; i < 8; i++) h[i] = (uint8_t)MAGIC[i]; + put32(h + 8, 1); + put32(h + 12, CK_PI5_MARKER_SECS - 1u); + put64(h + 16, gen); + put64(h + 24, CK_PI5_MARKER_SEED); + put32(h + 32, payload_crc); + put32(h + 36, 0); + put32(h + HDR_CRC_BYTES, ck_crc32(0, h, HDR_CRC_BYTES)); +} + +static int header_ok(const uint8_t *h) +{ + for (int i = 0; i < 8; i++) + if (h[i] != (uint8_t)MAGIC[i]) return 0; + return get32(h + 8) == 1 && get32(h + 12) == CK_PI5_MARKER_SECS - 1u && get64(h + 24) == CK_PI5_MARKER_SEED && + get32(h + HDR_CRC_BYTES) == ck_crc32(0, h, HDR_CRC_BYTES); +} + +static int all_zero(const uint8_t *p, size_t n) +{ + for (size_t i = 0; i < n; i++) if (p[i]) return 0; + return 1; +} + +static uint32_t payload_crc(const uint8_t *buf) +{ + return ck_crc32(0, buf + DISK_SECTOR_BYTES, (size_t)(CK_PI5_MARKER_SECS - 1u) * DISK_SECTOR_BYTES); +} + +/* Write the header sector, flush, read it back and compare. */ +static int write_header(const disk_dev *d, uint64_t lba, const uint8_t *hdr, uint8_t *copy) +{ + if (disk_write(d, lba, 1, hdr) != DISK_OK || disk_flush(d) != DISK_OK) return CK_PI5_MARKER_EIO; + if (disk_read(d, lba, 1, copy) != DISK_OK) return CK_PI5_MARKER_EIO; + for (uint32_t i = 0; i < DISK_SECTOR_BYTES; i++) + if (copy[i] != hdr[i]) return CK_PI5_MARKER_EVERIFY; + return 0; +} + +int ck_pi5_marker_run(const disk_dev *d, uint8_t *buf, uint64_t *generation) +{ + if (!d || !buf || d->block_size != DISK_SECTOR_BYTES || d->block_count < CK_PI5_MARKER_SECS) + return CK_PI5_MARKER_ESIZE; + uint64_t lba = d->block_count - CK_PI5_MARKER_SECS; + static uint8_t hdr[DISK_SECTOR_BYTES] __attribute__((aligned(64))); + static uint8_t copy[DISK_SECTOR_BYTES] __attribute__((aligned(64))); + if (disk_read(d, lba, CK_PI5_MARKER_SECS, buf) != DISK_OK) return CK_PI5_MARKER_EIO; + int rc; + if (header_ok(buf)) { + uint64_t seed = get64(buf + 24); + if (!payload_ok(buf, seed) || payload_crc(buf) != get32(buf + 32)) return CK_PI5_MARKER_ECORRUPT; + uint64_t gen = get64(buf + 16) + 1u; + make_header(hdr, gen, get32(buf + 32)); + if ((rc = write_header(d, lba, hdr, copy)) != 0) return rc; + *generation = gen; + return CK_PI5_MARKER_REOPENED; + } + if (!all_zero(buf, CK_PI5_MARKER_BUF_BYTES)) return CK_PI5_MARKER_EREFUSED; + fill_payload(buf, CK_PI5_MARKER_SEED); + uint32_t pc = payload_crc(buf); + /* payload first (63 sectors in one multi-sector write), flush, then the header, flush */ + if (disk_write(d, lba + 1u, CK_PI5_MARKER_SECS - 1u, buf + DISK_SECTOR_BYTES) != DISK_OK || disk_flush(d) != DISK_OK) + return CK_PI5_MARKER_EIO; + make_header(hdr, 1, pc); + if ((rc = write_header(d, lba, hdr, copy)) != 0) return rc; + /* whole region back, byte for byte against what was written */ + static uint8_t back[CK_PI5_MARKER_BUF_BYTES] __attribute__((aligned(64))); + if (disk_read(d, lba + 1u, CK_PI5_MARKER_SECS - 1u, back + DISK_SECTOR_BYTES) != DISK_OK) return CK_PI5_MARKER_EIO; + for (size_t i = DISK_SECTOR_BYTES; i < CK_PI5_MARKER_BUF_BYTES; i++) + if (back[i] != buf[i]) return CK_PI5_MARKER_EVERIFY; + *generation = 1; + return CK_PI5_MARKER_FORMATTED; +} + +const char *ck_pi5_marker_strerror(int rc) +{ + switch (rc) { + case CK_PI5_MARKER_FORMATTED: return "formatted (blank region written, flushed, read back identical)"; + case CK_PI5_MARKER_REOPENED: return "reopened (payload identical to the pattern, generation bumped and read back)"; + case CK_PI5_MARKER_EIO: return "I/O error"; + case CK_PI5_MARKER_EREFUSED: return "region is neither blank nor a marker, nothing written"; + case CK_PI5_MARKER_ECORRUPT: return "marker header valid but payload differs"; + case CK_PI5_MARKER_EVERIFY: return "read back differs from what was written"; + case CK_PI5_MARKER_ESIZE: return "partition view too small or not 512-byte blocks"; + default: return "unknown"; + } +} diff --git a/native/kernel/core/pi5_marker.h b/native/kernel/core/pi5_marker.h new file mode 100644 index 00000000..b3d0fd74 --- /dev/null +++ b/native/kernel/core/pi5_marker.h @@ -0,0 +1,44 @@ +/* pi5_marker.h -- the layer below the sealed Store, proved on the Pi 5 card + * (AIENOS-PI5 step 3). The Store itself needs production keys, which are + * BLOCKED_OPERATOR (svc/store_boot.c), so the Pi image does not open it. What + * the Store needs from the card is: GPT partition view in, multi-sector + * writes with flush, bytes back identical after a reboot. This module + * proves exactly that on a bounded scratch region at the END of the AIENOS + * partition view, written only through that view (dev/disk_part.h). + * + * Region: CK_PI5_MARKER_SECS sectors. Sector 0 is the header, the rest a + * payload that is a pure function of (seed, sector, offset), so a later boot + * can verify it byte for byte without storing a copy: + * header: magic "AIENPI5M", version, generation, payload sectors, seed, + * CRC32 of the payload, CRC32 of the header's first 40 bytes. + * First boot, region all zero: write payload, flush, write header, flush, + * read back: CK_PI5_MARKER_FORMATTED. Later boot, valid header: payload + * read (multi-sector) and compared with the regenerated pattern, then + * generation + 1 is written and read back: CK_PI5_MARKER_REOPENED. Anything + * else (neither zero nor a valid marker) is refused and nothing is written. + * + * Freestanding (no libc): the caller supplies the buffer. */ +#ifndef AIENOS_CK_PI5_MARKER_H +#define AIENOS_CK_PI5_MARKER_H +#include +#include "disk.h" + +#define CK_PI5_MARKER_SECS 64u /* 32 KiB: one header sector + 63 payload sectors */ +#define CK_PI5_MARKER_BUF_BYTES (CK_PI5_MARKER_SECS * DISK_SECTOR_BYTES) +#define CK_PI5_MARKER_SEED 0x41494e5049354d31ull + +enum { + CK_PI5_MARKER_FORMATTED = 1, /* blank region: written, flushed, read back identical */ + CK_PI5_MARKER_REOPENED = 2, /* valid marker: payload identical, generation bumped and read back */ + CK_PI5_MARKER_EIO = -501, /* a read, write or flush failed */ + CK_PI5_MARKER_EREFUSED = -502, /* region is neither blank nor a valid marker: nothing written */ + CK_PI5_MARKER_ECORRUPT = -503, /* valid header, payload does not match: nothing written */ + CK_PI5_MARKER_EVERIFY = -504, /* read back after a write differs */ + CK_PI5_MARKER_ESIZE = -505, /* view smaller than the region, or not 512-byte blocks */ +}; + +/* d is the AIENOS partition view. buf is CK_PI5_MARKER_BUF_BYTES, 64-aligned. + * *generation is set on success. */ +int ck_pi5_marker_run(const disk_dev *d, uint8_t *buf, uint64_t *generation); +const char *ck_pi5_marker_strerror(int rc); +#endif diff --git a/native/kernel/tests/.crumb b/native/kernel/tests/.crumb index 0424aac7..10eed784 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-13T06:35:07Z", + "compiled_at": "2026-10-13T07:51:09Z", "compiler": "crumb-compile/1", - "digest": "sha256:46def7a41aefac1c42a8bda757406960c6f03147fc147b860c94e8166febde16", + "digest": "sha256:90c571fe56a5ba0d0fc8b68ded88e8006bb1be273725222d6a49c9c4c03ae350", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 154, - "generated_at_commit": "93ae0323091b1f40c93fbc3a2bdfaaa48745c61a", - "source_tree": "sha256:e865db6add5a3a31d86e15171163edc371fcab8a67f4a1bf5521a3d4b4942858" + "files": 155, + "generated_at_commit": "27f24e8c3b542611bc086755ba3b5693c2125f30", + "source_tree": "sha256:e4f71671a4e6fc1b4fe600c94be3adcddeb84da9cc6ad9b6288c4bfbc734edde" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tests/test_pi5_marker.c b/native/kernel/tests/test_pi5_marker.c new file mode 100644 index 00000000..29b84c56 --- /dev/null +++ b/native/kernel/tests/test_pi5_marker.c @@ -0,0 +1,166 @@ +/* test_pi5_marker.c -- AIENOS-PI5 step 3: the Pi 5 card layout (ESP + AIENOS + * partition under a protective MBR and a two-copy GPT) parsed by the kernel's + * own dev/disk_part.c, and the scratch marker (core/pi5_marker.c) written + * through the partition view with flush, "rebooted" (file closed and reopened, + * nothing kept in memory) and verified. Hosted: a file stands in for the + * card. This is NOT the SD controller, which QEMU cannot model; the controller + * is proved on the hub Pi (pi5-run.sh) and its write path by native/disk/tests/sdhci_test.c. */ +#include +#include +#include +#include +#include "ck_test.h" +#include "disk_file.h" +#include "disk_part.h" +#include "gpt_write.h" +#include "pi5_marker.h" + +#define BLOCKS 262144ull /* 128 MiB stand-in for the 119 GiB card */ + +static const uint8_t esp_type_ok = 1; +static const char *img = "/tmp/ck_pi5_marker_test.img"; +static uint8_t buf[CK_PI5_MARKER_BUF_BYTES] __attribute__((aligned(64))); + +static void make_card(const uint8_t *aienos_type, unsigned flags) +{ + gw_part p[2] = {{gw_esp_type, 2048, 4095, "AIENOSPI5-ESP", 0}, {aienos_type, 8192, 16383, "AIENOS", 0}}; + (void)esp_type_ok; + CHECK(gw_write(img, 512, BLOCKS, p, 2, flags) == 0); +} + +static int open_view(disk_file *f, disk_dev *d, ck_part *part, ck_gpt_info *gi) +{ + if (disk_file_open(f, d, img, 512, BLOCKS, 0)) return -1; + return ck_gpt_find_aienos(d, part, gi); +} + +static int run(uint64_t *gen) +{ + disk_file f; + disk_dev d; + ck_part part; + ck_gpt_info gi; + int rc = open_view(&f, &d, &part, &gi); + CHECK(rc == 0); + if (rc) { disk_file_close(&f); return -999; } + rc = ck_pi5_marker_run(&part.dev, buf, gen); + disk_file_close(&f); + return rc; +} + +static uint8_t *slurp(size_t *n) +{ + FILE *fp = fopen(img, "rb"); + fseek(fp, 0, SEEK_END); + *n = (size_t)ftell(fp); + rewind(fp); + uint8_t *m = malloc(*n); + CHECK(fread(m, 1, *n, fp) == *n); + fclose(fp); + return m; +} + +int main(void) +{ + const uint64_t first = 8192, last = 16383, region = last + 1 - CK_PI5_MARKER_SECS; /* absolute LBA of the marker */ + uint64_t gen = 0; + size_t n1, n2; + + /* --- GPT as the Pi card has it --- */ + make_card(ck_aienos_part_type, 0); + { + disk_file f; disk_dev d; ck_part part; ck_gpt_info gi; + CHECK(open_view(&f, &d, &part, &gi) == 0); + CHECK(part.valid && part.first_lba == first && part.blocks == last - first + 1 && gi.used == 2); + CHECK(part.dev.block_count == last - first + 1); + uint64_t abs = 0; + CHECK(ck_part_xlate(&part, 0, 1, &abs) == 0 && abs == first); + CHECK(ck_part_xlate(&part, part.blocks - 1, 1, &abs) == 0 && abs == last); + CHECK(ck_part_xlate(&part, part.blocks, 1, &abs) == DISK_ERANGE); /* one past the partition */ + CHECK(disk_write(&part.dev, part.blocks, 1, buf) == DISK_ERANGE); + disk_file_close(&f); + } + /* hostile cards: nothing is presented and the marker refuses */ + { + static const uint8_t other[16] = {1, 2, 3}; + const struct { const uint8_t *t; unsigned fl; } bad[] = { + {other, 0}, {ck_aienos_part_type, GW_NO_PMBR}, {ck_aienos_part_type, GW_BAD_PRIMARY_CRC}, + {ck_aienos_part_type, GW_BAD_BACKUP_CRC}, {ck_aienos_part_type, GW_BAD_PRIMARY_ENT}, + {ck_aienos_part_type, GW_BAD_BACKUP_ENT}, {ck_aienos_part_type, GW_NO_GPT}, + }; + for (unsigned i = 0; i < sizeof bad / sizeof bad[0]; i++) { + make_card(bad[i].t, bad[i].fl); + disk_file f; disk_dev d; ck_part part; ck_gpt_info gi; + CHECK(open_view(&f, &d, &part, &gi) != 0); + CHECK(!part.valid); + CHECK(ck_pi5_marker_run(&part.dev, buf, &gen) == CK_PI5_MARKER_ESIZE); + disk_file_close(&f); + } + } + + /* --- first boot: blank region is formatted --- */ + make_card(ck_aienos_part_type, 0); + uint8_t *before = slurp(&n1); + CHECK(run(&gen) == CK_PI5_MARKER_FORMATTED && gen == 1); + uint8_t *after = slurp(&n2); + CHECK(n1 == n2); + size_t changed_outside = 0, changed_inside = 0; + for (size_t i = 0; i < n1; i++) + if (before[i] != after[i]) { + if (i / 512 >= region && i / 512 <= last) changed_inside++; else changed_outside++; + } + CHECK(changed_inside > 0 && changed_outside == 0); /* every written byte is inside the marker region */ + /* --- reboot: reopen, same contents, generation moves --- */ + CHECK(run(&gen) == CK_PI5_MARKER_REOPENED && gen == 2); + uint8_t *again = slurp(&n2); + size_t diff_sectors = 0; + for (size_t s = 0; s < n1 / 512; s++) + if (memcmp(after + s * 512, again + s * 512, 512)) diff_sectors++; + CHECK(diff_sectors == 1); /* only the header sector; the 63 payload sectors are identical */ + CHECK(run(&gen) == CK_PI5_MARKER_REOPENED && gen == 3); + free(again); + + /* --- a flipped payload byte is caught and nothing is written --- */ + { + FILE *fp = fopen(img, "r+b"); + fseek(fp, (long)((region + 7) * 512 + 100), SEEK_SET); + int c = fgetc(fp); + fseek(fp, -1, SEEK_CUR); + fputc(c ^ 0x01, fp); + fclose(fp); + uint8_t *pre = slurp(&n1); + CHECK(run(&gen) == CK_PI5_MARKER_ECORRUPT); + uint8_t *post = slurp(&n2); + CHECK(n1 == n2 && memcmp(pre, post, n1) == 0); + free(pre); free(post); + } + + /* --- foreign data in the region: refused, untouched --- */ + make_card(ck_aienos_part_type, 0); + { + FILE *fp = fopen(img, "r+b"); + fseek(fp, (long)((region + 3) * 512), SEEK_SET); + fputs("someone else's data", fp); + fclose(fp); + uint8_t *pre = slurp(&n1); + CHECK(run(&gen) == CK_PI5_MARKER_EREFUSED); + uint8_t *post = slurp(&n2); + CHECK(memcmp(pre, post, n1) == 0); + free(pre); free(post); + } + + /* --- power cut during the first format: header is written last, so a torn run is refused, never "reopened" --- */ + make_card(ck_aienos_part_type, 0); + { + disk_file f; disk_dev d; ck_part part; ck_gpt_info gi; + CHECK(open_view(&f, &d, &part, &gi) == 0); + disk_file_arm_cut(&f, 20); /* 20 of the 63 payload sectors reach the card */ + CHECK(ck_pi5_marker_run(&part.dev, buf, &gen) < 0); + disk_file_close(&f); + int rc = run(&gen); + CHECK(rc == CK_PI5_MARKER_EREFUSED || rc == CK_PI5_MARKER_ECORRUPT); + } + free(before); free(after); + unlink(img); + return ck_t_verdict("test_pi5_marker"); +} diff --git a/scripts/.crumb b/scripts/.crumb index 82ede34c..6408916f 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-13T06:35:07Z", + "compiled_at": "2026-10-13T07:51:09Z", "compiler": "crumb-compile/1", - "digest": "sha256:d2fbf98209136e9f0f16f6f5a8a0938a9b711f46798e0d74fb0344f13dedb210", + "digest": "sha256:197bb0910cb0164678c40a278c02240cd8f147d407d2322be38ce7a66289316e", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 92, - "generated_at_commit": "93ae0323091b1f40c93fbc3a2bdfaaa48745c61a", - "source_tree": "sha256:4e4695ef692ccba30cd343e0b816e72d4fb4b81b1540d1862240b9f7b9c872b4" + "generated_at_commit": "27f24e8c3b542611bc086755ba3b5693c2125f30", + "source_tree": "sha256:4d2e5f840e9b0fb5373a8a70bc13fd3a6ef06eeeddaa8331d7af02c66228af1c" }, "owner_lane": "", "provenance": { diff --git a/scripts/ck_pi5_build_check.sh b/scripts/ck_pi5_build_check.sh index 5ea5220f..dd38f54d 100755 --- a/scripts/ck_pi5_build_check.sh +++ b/scripts/ck_pi5_build_check.sh @@ -13,7 +13,7 @@ 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" +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 ck_pi5_marker_run" fail=0 defined() { nm "$1" | awk -v s="$2" '$3==s && $2 ~ /^[TtDdBbRrW]$/ {f=1} END {exit !f}'; } for s in ${syms}; do ```