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/blob/main/crates/aienos-kernel/src/uefi.rs # 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 branch `main` Branches Tags History `aienux / crates / aienos-kernel / src / uefi.rs` 126 lines ยท 4808 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 //! UEFI memory attributes table (MAT) parser. 17 use crate::mem::map_plan::{AddressRange, RuntimeAttributes, EFI_MEMORY_RO, EFI_MEMORY_XP}; 18 use alloc::vec::Vec; 19 20 /// EFI_MEMORY_ATTRIBUTES_TABLE configuration table GUID in UEFI byte order. 21 pub const EFI_MEMORY_ATTRIBUTES_TABLE_GUID: [u8; 16] = [ 22 0x1d, 0x91, 0xfa, 0xdc, 0xeb, 0x26, 0x9f, 0x46, 0xa2, 0x20, 0x38, 0xb7, 0xdc, 0x46, 0x12, 0x20, 23 ]; 24 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 25 pub enum MatError { 26 Truncated, 27 InvalidDescriptorSize, 28 Overflow, 29 } 30 fn u32at(b: &[u8], n: usize) -> Result { 31 let s = b 32 .get(n..n.checked_add(4).ok_or(MatError::Overflow)?) 33 .ok_or(MatError::Truncated)?; 34 Ok(u32::from_le_bytes([s[0], s[1], s[2], s[3]])) 35 } 36 fn u64at(b: &[u8], n: usize) -> Result { 37 let s = b 38 .get(n..n.checked_add(8).ok_or(MatError::Overflow)?) 39 .ok_or(MatError::Truncated)?; 40 Ok(u64::from_le_bytes([ 41 s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], 42 ])) 43 } 44 45 /// Parse the MAT table body. Descriptor extensions are skipped according to 46 /// DescriptorSize; each result describes one physical descriptor range. 47 pub fn parse_memory_attributes_table(table: &[u8]) -> Result, MatError> { 48 if table.len() < 16 { 49 return Err(MatError::Truncated); 50 } 51 let count = usize::try_from(u32at(table, 4)?).map_err(|_| MatError::Overflow)?; 52 let stride = usize::try_from(u32at(table, 8)?).map_err(|_| MatError::Overflow)?; 53 if stride < 40 { 54 return Err(MatError::InvalidDescriptorSize); 55 } 56 let bytes = count.checked_mul(stride).ok_or(MatError::Overflow)?; 57 let end = 16usize.checked_add(bytes).ok_or(MatError::Overflow)?; 58 table.get(16..end).ok_or(MatError::Truncated)?; 59 let mut out = Vec::with_capacity(count); 60 for i in 0..count { 61 let d = 16 + i * stride; 62 let start = u64at(table, d + 8)?; 63 let pages = u64at(table, d + 24)?; 64 let attribute = u64at(table, d + 32)?; 65 let length = pages.checked_mul(4096).ok_or(MatError::Overflow)?; 66 start.checked_add(length).ok_or(MatError::Overflow)?; 67 out.push(RuntimeAttributes { 68 range: AddressRange { start, length }, 69 read_only: attribute & EFI_MEMORY_RO != 0, 70 execute_protect: attribute & EFI_MEMORY_XP != 0, 71 }); 72 } 73 Ok(out) 74 } 75 76 #[cfg(test)] 77 mod tests { 78 use super::*; 79 fn table(stride: usize) -> Vec { 80 let mut b = vec![0; 16 + stride * 2]; 81 b[0..4].copy_from_slice(&1u32.to_le_bytes()); 82 b[4..8].copy_from_slice(&2u32.to_le_bytes()); 83 b[8..12].copy_from_slice(&(stride as u32).to_le_bytes()); 84 for i in 0..2 { 85 let d = 16 + i * stride; 86 b[d + 8..d + 16].copy_from_slice(&(0x8000u64 + i as u64 * 0x2000).to_le_bytes()); 87 b[d + 24..d + 32].copy_from_slice(&2u64.to_le_bytes()); 88 b[d + 32..d + 40].copy_from_slice( 89 &(if i == 0 { EFI_MEMORY_RO } else { EFI_MEMORY_XP }).to_le_bytes(), 90 ); 91 } 92 b 93 } 94 #[test] 95 fn honors_48_and_56_byte_descriptor_strides() { 96 for stride in [48, 56] { 97 let a = parse_memory_attributes_table(&table(stride)).unwrap(); 98 assert_eq!(a.len(), 2); 99 assert_eq!( 100 a[0].range, 101 AddressRange { 102 start: 0x8000, 103 length: 8192 104 } 105 ); 106 assert!(a[0].read_only && !a[0].execute_protect); 107 assert!(!a[1].read_only && a[1].execute_protect); 108 } 109 } 110 #[test] 111 fn rejects_truncated_and_short_descriptors() { 112 assert_eq!( 113 parse_memory_attributes_table(&[0; 15]), 114 Err(MatError::Truncated) 115 ); 116 let mut b = table(48); 117 b.truncate(b.len() - 1); 118 assert_eq!(parse_memory_attributes_table(&b), Err(MatError::Truncated)); 119 let mut b = table(48); 120 b[8..12].copy_from_slice(&39u32.to_le_bytes()); 121 assert_eq!( 122 parse_memory_attributes_table(&b), 123 Err(MatError::InvalidDescriptorSize) 124 ); 125 } 126 }