//! Resource templates (ACPI 6.5, 6.4): what `_CRS` answers, a device's //! ports, memory, interrupts and connections, decoded. use alloc::string::String; use alloc::vec::Vec; /// One descriptor of a template. #[derive(Clone, Debug, PartialEq, Eq)] pub enum Resource { /// An ISA interrupt (`IRQ`, `IRQNoFlags`). Irq { irqs: Vec, edge: bool, active_low: bool, shared: bool, }, Io { min: u16, max: u16, align: u8, len: u8, }, FixedIo { base: u16, len: u8, }, Memory { base: u64, len: u64, writable: bool, }, /// An address range (`DWordMemory`, `QWordIO`, `WordBusNumber` …): /// kind 0 memory, 1 I/O, 2 bus numbers. Address { kind: u8, min: u64, len: u64, translation: u64, }, Interrupt { gsis: Vec, edge: bool, active_low: bool, shared: bool, wake: bool, }, /// A GPIO pin used as an interrupt (`GpioInt`) or as a line driven or /// read (`GpioIo`), on the controller `source` names. Gpio { interrupt: bool, pins: Vec, source: String, edge: bool, active_low: bool, both: bool, wake: bool, pull: u8, debounce: u16, }, /// A device on an I²C bus (`I2cSerialBusV2`): its address, speed in /// Hz, and the controller `source` names. I2c { address: u16, speed: u32, ten_bit: bool, source: String, }, /// Another serial bus (SPI, UART): its type and controller. SerialBus { kind: u8, source: String, }, /// A descriptor not decoded, by its tag. Other(u8), } fn u16_at(b: &[u8], at: usize) -> u16 { b.get(at..at + 2).map_or(0, |x| u16::from_le_bytes([x[0], x[1]])) } fn u32_at(b: &[u8], at: usize) -> u32 { b.get(at..at + 4) .map_or(0, |x| u32::from_le_bytes([x[0], x[1], x[2], x[3]])) } fn u64_at(b: &[u8], at: usize) -> u64 { u64::from(u32_at(b, at)) | u64::from(u32_at(b, at + 4)) << 32 } /// A null-terminated name from `at` in a descriptor. fn name_at(d: &[u8], at: usize) -> String { let tail = d.get(at..).unwrap_or(&[]); let end = tail.iter().position(|&b| b == 0).unwrap_or(tail.len()); String::from_utf8_lossy(&tail[..end]).into_owned() } /// Every descriptor in `template`, up to its end tag. pub fn parse(template: &[u8]) -> Vec { let mut out = Vec::new(); let mut at = 0; while at < template.len() { let tag = template[at]; if tag & 0x80 == 0 { let kind = (tag >> 3) & 0x0f; let len = usize::from(tag & 7); let Some(d) = template.get(at + 1..at + 1 + len) else { break; }; at += 1 + len; out.push(match kind { 0x04 => { let mask = u16_at(d, 0); let flags = d.get(2).copied().unwrap_or(1); Resource::Irq { irqs: (0..16).filter(|i| mask >> i & 1 != 0).collect(), edge: flags & 1 != 0, active_low: flags & 8 != 0, shared: flags & 0x10 != 0, } } 0x08 => Resource::Io { min: u16_at(d, 1), max: u16_at(d, 3), align: d.get(5).copied().unwrap_or(0), len: d.get(6).copied().unwrap_or(0), }, 0x09 => Resource::FixedIo { base: u16_at(d, 0) & 0x3ff, len: d.get(2).copied().unwrap_or(0), }, 0x0f => break, k => Resource::Other(k), }); } else { let len = usize::from(u16_at(template, at + 1)); let Some(d) = template.get(at..at + 3 + len) else { break; }; at += 3 + len; out.push(large(tag & 0x7f, d)); } } out } /// A large descriptor, `d` from its tag on. fn large(kind: u8, d: &[u8]) -> Resource { match kind { 0x05 => Resource::Memory { base: u64::from(u32_at(d, 4)), len: u64::from(u32_at(d, 16)), writable: d.get(3).is_some_and(|f| f & 1 != 0), }, 0x06 => Resource::Memory { base: u64::from(u32_at(d, 4)), len: u64::from(u32_at(d, 8)), writable: d.get(3).is_some_and(|f| f & 1 != 0), }, 0x07 | 0x08 | 0x0a => { let size = match kind { 0x07 => 4, 0x08 => 2, _ => 8, }; // Type, general flags, type flags, then granularity, minimum, // maximum, translation, length, each `size` bytes. let field = |i: usize| { let at = 6 + i * size; match size { 2 => u64::from(u16_at(d, at)), 4 => u64::from(u32_at(d, at)), _ => u64_at(d, at), } }; Resource::Address { kind: d.get(3).copied().unwrap_or(0), min: field(1), translation: field(3), len: field(4), } } 0x09 => { let flags = d.get(3).copied().unwrap_or(0); let count = usize::from(d.get(4).copied().unwrap_or(0)); Resource::Interrupt { gsis: (0..count).map(|i| u32_at(d, 5 + 4 * i)).collect(), edge: flags & 2 != 0, active_low: flags & 4 != 0, shared: flags & 8 != 0, wake: flags & 0x10 != 0, } } 0x0c => { let interrupt = d.get(4) == Some(&0); let flags = u16_at(d, 7); let pins_at = usize::from(u16_at(d, 14)); let source_at = usize::from(u16_at(d, 17)); let pins = (pins_at..source_at.max(pins_at)) .step_by(2) .map(|at| u16_at(d, at)) .collect(); let polarity = (flags >> 1) & 3; Resource::Gpio { interrupt, pins, source: name_at(d, source_at), edge: interrupt && flags & 1 != 0, active_low: polarity == 1, both: polarity == 2, wake: flags & 0x10 != 0, pull: d.get(9).copied().unwrap_or(0), debounce: u16_at(d, 12), } } 0x0e => { let bus = d.get(5).copied().unwrap_or(0); let flags = u16_at(d, 7); let data_len = usize::from(u16_at(d, 10)); let source = name_at(d, 12 + data_len); match bus { 1 => Resource::I2c { speed: u32_at(d, 12), address: u16_at(d, 16), ten_bit: flags & 1 != 0, source, }, kind => Resource::SerialBus { kind, source }, } } k => Resource::Other(0x80 | k), } }