aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-boot / src / store_qual.rs
| 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 | //! Test-only Store-over-NVMe qualification plumbing. |
| 17 | //! |
| 18 | //! Bounded block device over the native NVMe controller plus canonical Store v1 |
| 19 | //! genesis bytes. No production Store, NVMe, or recovery semantics are changed. |
| 20 | |
| 21 | extern crate alloc; |
| 22 | |
| 23 | use aienos_kernel::block::{BlockDevice, BlockError}; |
| 24 | use aienos_kernel::nvme::driver::{Delay, DmaMemory, NvmeController}; |
| 25 | use aienos_kernel::nvme::Registers; |
| 26 | |
| 27 | /// Bounded Store region base LBA and size, in logical blocks. |
| 28 | pub const STORE_BASE_LBA: u64 = 64; |
| 29 | pub const STORE_REGION_BLOCKS: u64 = 256; |
| 30 | /// Control block LBA the host script writes to select the qualification mode. |
| 31 | pub const CONFIG_LBA: u64 = 32; |
| 32 | |
| 33 | pub const MODE_VERIFY: u8 = 2; |
| 34 | |
| 35 | /// 512-byte LBA qualification constants. |
| 36 | pub const MODE_RUN_512B: u8 = 3; |
| 37 | pub const MODE_VERIFY_512B: u8 = 4; |
| 38 | pub const STORE_BASE_LBA_512: u64 = 512; |
| 39 | pub const STORE_REGION_BLOCKS_512: u64 = 2048; |
| 40 | pub const CONFIG_LBA_512: u64 = 256; |
| 41 | |
| 42 | /// M4 continuity qualification modes (ADR 0016), 4096-byte LBA only. |
| 43 | /// Provision: format if blank, then create the one identity from RNDR. |
| 44 | #[cfg(feature = "continuity-qual")] |
| 45 | pub const MODE_CONT_PROVISION: u8 = 5; |
| 46 | /// Resume: locate and verify the identity, commit incarnation + 1. Never mints. |
| 47 | #[cfg(feature = "continuity-qual")] |
| 48 | pub const MODE_CONT_RESUME: u8 = 6; |
| 49 | /// Resume, then commit one Cortex fact and one branch fork. |
| 50 | #[cfg(feature = "continuity-qual")] |
| 51 | pub const MODE_CONT_REMEMBER: u8 = 7; |
| 52 | /// Resume, then commit one Cortex fact while emitting Store checkpoints. |
| 53 | #[cfg(feature = "continuity-qual")] |
| 54 | pub const MODE_CONT_CRASH: u8 = 8; |
| 55 | |
| 56 | /// Recovery Core qualification modes (ADR 0006). The operator response, when |
| 57 | /// needed, is control-block bytes 16..48. |
| 58 | #[cfg(feature = "recovery-qual")] |
| 59 | pub const MODE_RECOVERY_INSPECT: u8 = 9; |
| 60 | #[cfg(feature = "recovery-qual")] |
| 61 | pub const MODE_RECOVERY_REPAIR: u8 = 10; |
| 62 | #[cfg(feature = "recovery-qual")] |
| 63 | pub const MODE_RECOVERY_PROVISION: u8 = 11; |
| 64 | |
| 65 | #[cfg(feature = "recovery-qual")] |
| 66 | pub const fn is_recovery_mode(mode: u8) -> bool { |
| 67 | matches!( |
| 68 | mode, |
| 69 | MODE_RECOVERY_INSPECT | MODE_RECOVERY_REPAIR | MODE_RECOVERY_PROVISION |
| 70 | ) |
| 71 | } |
| 72 | |
| 73 | /// TEST ONLY operator key for the QEMU Recovery Core qualification. It is |
| 74 | /// public in this source tree and proves nothing about a real operator; the |
| 75 | /// real credential scheme (ADR 0006 / M5) is not decided. Every boot that uses |
| 76 | /// it prints `RECOVERY_OPERATOR_KEY: TEST-ONLY`. |
| 77 | #[cfg(feature = "recovery-qual")] |
| 78 | pub const TEST_ONLY_OPERATOR_KEY: [u8; 32] = [0x0f; 32]; |
| 79 | |
| 80 | #[cfg(feature = "continuity-qual")] |
| 81 | pub const fn is_continuity_mode(mode: u8) -> bool { |
| 82 | matches!( |
| 83 | mode, |
| 84 | MODE_CONT_PROVISION | MODE_CONT_RESUME | MODE_CONT_REMEMBER | MODE_CONT_CRASH |
| 85 | ) |
| 86 | } |
| 87 | |
| 88 | /// Adds a fixed LBA offset and rejects accesses past the bounded region. |
| 89 | pub struct BoundedNvme<R: Registers, D: DmaMemory, T: Delay> { |
| 90 | inner: NvmeController<R, D, T>, |
| 91 | base_lba: u64, |
| 92 | blocks: u64, |
| 93 | } |
| 94 | |
| 95 | impl<R: Registers, D: DmaMemory, T: Delay> BoundedNvme<R, D, T> { |
| 96 | pub fn new(inner: NvmeController<R, D, T>, base_lba: u64, blocks: u64) -> Self { |
| 97 | Self { |
| 98 | inner, |
| 99 | base_lba, |
| 100 | blocks, |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | impl<R: Registers, D: DmaMemory, T: Delay> BlockDevice for BoundedNvme<R, D, T> { |
| 106 | fn block_size(&self) -> u32 { |
| 107 | self.inner.block_size() |
| 108 | } |
| 109 | fn block_count(&self) -> u64 { |
| 110 | self.blocks |
| 111 | } |
| 112 | fn read_blocks(&mut self, lba: u64, buffer: &mut [u8]) -> Result<(), BlockError> { |
| 113 | let bs = self.inner.block_size() as usize; |
| 114 | if bs == 0 || !buffer.len().is_multiple_of(bs) { |
| 115 | return Err(BlockError::InvalidInput); |
| 116 | } |
| 117 | let count = (buffer.len() / bs) as u64; |
| 118 | if lba.checked_add(count).is_none_or(|end| end > self.blocks) { |
| 119 | return Err(BlockError::OutOfRange); |
| 120 | } |
| 121 | self.inner.read_blocks(self.base_lba + lba, buffer) |
| 122 | } |
| 123 | fn write_blocks(&mut self, lba: u64, buffer: &[u8]) -> Result<(), BlockError> { |
| 124 | let bs = self.inner.block_size() as usize; |
| 125 | if bs == 0 || !buffer.len().is_multiple_of(bs) { |
| 126 | return Err(BlockError::InvalidInput); |
| 127 | } |
| 128 | let count = (buffer.len() / bs) as u64; |
| 129 | if lba.checked_add(count).is_none_or(|end| end > self.blocks) { |
| 130 | return Err(BlockError::OutOfRange); |
| 131 | } |
| 132 | self.inner.write_blocks(self.base_lba + lba, buffer) |
| 133 | } |
| 134 | fn flush(&mut self) -> Result<(), BlockError> { |
| 135 | self.inner.flush() |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | pub const STORE_UNIT_BYTES: usize = 4096; |
| 140 | |
| 141 | /// Canonical Store v1 genesis: Superblock A only (slot B is left all zero so |
| 142 | /// the first transaction exercises the "inactive slot initially zero" case), |
| 143 | /// an empty catalog at unit 2, and the CommitRecord at unit 3. |
| 144 | pub fn genesis_units(region_units: u64) -> [[u8; STORE_UNIT_BYTES]; 4] { |
| 145 | aienos_kernel::store::genesis::genesis_units(QUAL_STORE_UUID, region_units) |
| 146 | } |
| 147 | |
| 148 | /// Fixed test-only Store UUID used by the qualification images. |
| 149 | pub const QUAL_STORE_UUID: [u8; 16] = [0x5A; 16]; |