#![no_std]
#![feature(decl_macro, type_ascription, box_syntax)]
extern crate alloc;
#[cfg(test)]
extern crate std;
#[cfg(test)]
mod test_utils;
pub(crate) mod misc;
pub(crate) mod name_object;
pub(crate) mod namespace;
pub(crate) mod opcode;
pub(crate) mod parser;
pub mod pci_routing;
pub(crate) mod pkg_length;
pub mod resource;
pub(crate) mod term_object;
pub(crate) mod type1;
pub(crate) mod type2;
pub mod value;
pub use crate::{
namespace::{AmlHandle, AmlName, Namespace},
value::AmlValue,
};
use alloc::boxed::Box;
use core::mem;
use log::error;
use misc::{ArgNum, LocalNum};
use name_object::Target;
use namespace::LevelType;
use parser::Parser;
use pkg_length::PkgLength;
use term_object::term_list;
use value::{AmlType, Args};
pub const AML_INTERPRETER_REVISION: u64 = 0;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum DebugVerbosity {
None,
Scopes,
AllScopes,
All,
}
struct MethodContext {
local_0: Option<AmlValue>,
local_1: Option<AmlValue>,
local_2: Option<AmlValue>,
local_3: Option<AmlValue>,
local_4: Option<AmlValue>,
local_5: Option<AmlValue>,
local_6: Option<AmlValue>,
local_7: Option<AmlValue>,
args: Args,
}
impl MethodContext {
fn new(args: Args) -> MethodContext {
MethodContext {
local_0: None,
local_1: None,
local_2: None,
local_3: None,
local_4: None,
local_5: None,
local_6: None,
local_7: None,
args,
}
}
}
pub struct AmlContext {
handler: Box<dyn Handler>,
legacy_mode: bool,
pub namespace: Namespace,
method_context: Option<MethodContext>,
current_scope: AmlName,
scope_indent: usize,
debug_verbosity: DebugVerbosity,
}
impl AmlContext {
pub fn new(handler: Box<dyn Handler>, legacy_mode: bool, debug_verbosity: DebugVerbosity) -> AmlContext {
let mut context = AmlContext {
handler,
legacy_mode,
namespace: Namespace::new(),
method_context: None,
current_scope: AmlName::root(),
scope_indent: 0,
debug_verbosity,
};
context.namespace.add_level(AmlName::from_str("\\_GPE").unwrap(), LevelType::Scope).unwrap();
context.namespace.add_level(AmlName::from_str("\\_SB").unwrap(), LevelType::Scope).unwrap();
context.namespace.add_level(AmlName::from_str("\\_SI").unwrap(), LevelType::Scope).unwrap();
if legacy_mode {
context.namespace.add_level(AmlName::from_str("\\_PR").unwrap(), LevelType::Scope).unwrap();
context.namespace.add_level(AmlName::from_str("\\_TZ").unwrap(), LevelType::Scope).unwrap();
}
context
}
pub fn parse_table(&mut self, stream: &[u8]) -> Result<(), AmlError> {
if stream.len() == 0 {
return Err(AmlError::UnexpectedEndOfStream);
}
let table_length = PkgLength::from_raw_length(stream, stream.len() as u32) as PkgLength;
match term_object::term_list(table_length).parse(stream, self) {
Ok(_) => Ok(()),
Err((_, _, err)) => {
error!("Failed to parse AML stream. Err = {:?}", err);
Err(err)
}
}
}
pub fn invoke_method(&mut self, path: &AmlName, args: Args) -> Result<AmlValue, AmlError> {
match self.namespace.get_by_path(path)?.clone() {
AmlValue::Method { flags, code } => {
let old_context = mem::replace(&mut self.method_context, Some(MethodContext::new(args)));
let old_scope = mem::replace(&mut self.current_scope, path.clone());
self.namespace.add_level(path.clone(), LevelType::MethodLocals)?;
let return_value =
match term_list(PkgLength::from_raw_length(&code, code.len() as u32)).parse(&code, self) {
Ok(_) => Ok(AmlValue::Integer(0)),
Err((_, _, AmlError::Return(result))) => Ok(result),
Err((_, _, err)) => {
error!("Failed to execute control method: {:?}", err);
Err(err)
}
};
self.namespace.remove_level(path.clone())?;
self.method_context = old_context;
self.current_scope = old_scope;
return_value
}
value => Ok(value),
}
}
pub fn initialize_objects(&mut self) -> Result<(), AmlError> {
use name_object::NameSeg;
use namespace::NamespaceLevel;
use value::StatusObject;
match self.invoke_method(&AmlName::from_str("\\_SB._INI").unwrap(), Args::default()) {
Ok(_) => (),
Err(AmlError::ValueDoesNotExist(_)) => (),
Err(err) => return Err(err),
}
self.namespace.clone().traverse(|path, level: &NamespaceLevel| match level.typ {
LevelType::Device => {
let status = if level.values.contains_key(&NameSeg::from_str("_STA").unwrap()) {
self.invoke_method(&AmlName::from_str("_STA").unwrap().resolve(&path)?, Args::default())?
.as_status()?
} else {
StatusObject::default()
};
if status.present && level.values.contains_key(&NameSeg::from_str("_INI").unwrap()) {
log::info!("Invoking _INI at level: {}", path);
self.invoke_method(&AmlName::from_str("_INI").unwrap().resolve(&path)?, Args::default())?;
}
Ok(status.present || status.functional)
}
LevelType::Scope => Ok(true),
LevelType::Processor => Ok(false),
LevelType::MethodLocals => Ok(false),
})?;
Ok(())
}
pub(crate) fn current_arg(&self, arg: ArgNum) -> Result<&AmlValue, AmlError> {
self.method_context.as_ref().ok_or(AmlError::NotExecutingControlMethod)?.args.arg(arg)
}
pub(crate) fn local(&self, local: LocalNum) -> Result<&AmlValue, AmlError> {
if let None = self.method_context {
return Err(AmlError::NotExecutingControlMethod);
}
match local {
0 => self.method_context.as_ref().unwrap().local_0.as_ref().ok_or(AmlError::InvalidLocalAccess(local)),
1 => self.method_context.as_ref().unwrap().local_1.as_ref().ok_or(AmlError::InvalidLocalAccess(local)),
2 => self.method_context.as_ref().unwrap().local_2.as_ref().ok_or(AmlError::InvalidLocalAccess(local)),
3 => self.method_context.as_ref().unwrap().local_3.as_ref().ok_or(AmlError::InvalidLocalAccess(local)),
4 => self.method_context.as_ref().unwrap().local_4.as_ref().ok_or(AmlError::InvalidLocalAccess(local)),
5 => self.method_context.as_ref().unwrap().local_5.as_ref().ok_or(AmlError::InvalidLocalAccess(local)),
6 => self.method_context.as_ref().unwrap().local_6.as_ref().ok_or(AmlError::InvalidLocalAccess(local)),
7 => self.method_context.as_ref().unwrap().local_7.as_ref().ok_or(AmlError::InvalidLocalAccess(local)),
_ => Err(AmlError::InvalidLocalAccess(local)),
}
}
pub(crate) fn store(&mut self, target: Target, value: AmlValue) -> Result<AmlValue, AmlError> {
match target {
Target::Name(ref path) => {
let (_, handle) = self.namespace.search(path, &self.current_scope)?;
let converted_object = match self.namespace.get(handle).unwrap().type_of() {
AmlType::FieldUnit => {
let mut field = self.namespace.get(handle).unwrap().clone();
field.write_field(value, self)?;
field.read_field(self)?
}
typ => value.as_type(typ, self)?,
};
*self.namespace.get_mut(handle)? = converted_object;
Ok(self.namespace.get(handle)?.clone())
}
Target::Debug => {
unimplemented!()
}
Target::Arg(arg_num) => {
if let None = self.method_context {
return Err(AmlError::NotExecutingControlMethod);
}
match arg_num {
1 => self.method_context.as_mut().unwrap().args.arg_1 = Some(value.clone()),
2 => self.method_context.as_mut().unwrap().args.arg_2 = Some(value.clone()),
3 => self.method_context.as_mut().unwrap().args.arg_3 = Some(value.clone()),
4 => self.method_context.as_mut().unwrap().args.arg_4 = Some(value.clone()),
5 => self.method_context.as_mut().unwrap().args.arg_5 = Some(value.clone()),
6 => self.method_context.as_mut().unwrap().args.arg_6 = Some(value.clone()),
_ => return Err(AmlError::InvalidArgAccess(arg_num)),
}
Ok(value)
}
Target::Local(local_num) => {
if let None = self.method_context {
return Err(AmlError::NotExecutingControlMethod);
}
match local_num {
0 => self.method_context.as_mut().unwrap().local_0 = Some(value.clone()),
1 => self.method_context.as_mut().unwrap().local_1 = Some(value.clone()),
2 => self.method_context.as_mut().unwrap().local_2 = Some(value.clone()),
3 => self.method_context.as_mut().unwrap().local_3 = Some(value.clone()),
4 => self.method_context.as_mut().unwrap().local_4 = Some(value.clone()),
5 => self.method_context.as_mut().unwrap().local_5 = Some(value.clone()),
6 => self.method_context.as_mut().unwrap().local_6 = Some(value.clone()),
7 => self.method_context.as_mut().unwrap().local_7 = Some(value.clone()),
_ => return Err(AmlError::InvalidLocalAccess(local_num)),
}
Ok(value)
}
Target::Null => Ok(value),
}
}
pub(crate) fn read_region(&self, region_handle: AmlHandle, offset: u64, length: u64) -> Result<u64, AmlError> {
use bit_field::BitField;
use core::convert::TryInto;
use value::RegionSpace;
let (region_space, region_base, region_length, parent_device) = {
if let AmlValue::OpRegion { region, offset, length, parent_device } =
self.namespace.get(region_handle)?
{
(region, offset, length, parent_device)
} else {
return Err(AmlError::FieldRegionIsNotOpRegion);
}
};
match region_space {
RegionSpace::SystemMemory => {
let address = (region_base + offset).try_into().map_err(|_| AmlError::FieldInvalidAddress)?;
match length {
8 => Ok(self.handler.read_u8(address) as u64),
16 => Ok(self.handler.read_u16(address) as u64),
32 => Ok(self.handler.read_u32(address) as u64),
64 => Ok(self.handler.read_u64(address)),
_ => Err(AmlError::FieldInvalidAccessSize),
}
}
RegionSpace::SystemIo => {
let port = (region_base + offset).try_into().map_err(|_| AmlError::FieldInvalidAddress)?;
match length {
8 => Ok(self.handler.read_io_u8(port) as u64),
16 => Ok(self.handler.read_io_u16(port) as u64),
32 => Ok(self.handler.read_io_u32(port) as u64),
_ => Err(AmlError::FieldInvalidAccessSize),
}
}
RegionSpace::PciConfig => {
let parent_device = parent_device.as_ref().unwrap();
let seg = match self.namespace.search(&AmlName::from_str("_SEG").unwrap(), parent_device) {
Ok((_, handle)) => self
.namespace
.get(handle)?
.as_integer(self)?
.try_into()
.map_err(|_| AmlError::FieldInvalidAddress)?,
Err(AmlError::ValueDoesNotExist(_)) => 0,
Err(err) => return Err(err),
};
let bbn = match self.namespace.search(&AmlName::from_str("_BBN").unwrap(), parent_device) {
Ok((_, handle)) => self
.namespace
.get(handle)?
.as_integer(self)?
.try_into()
.map_err(|_| AmlError::FieldInvalidAddress)?,
Err(AmlError::ValueDoesNotExist(_)) => 0,
Err(err) => return Err(err),
};
let adr = {
let (_, handle) = self.namespace.search(&AmlName::from_str("_ADR").unwrap(), parent_device)?;
self.namespace.get(handle)?.as_integer(self)?
};
let device = adr.get_bits(16..24) as u8;
let function = adr.get_bits(0..8) as u8;
let offset = (region_base + offset).try_into().map_err(|_| AmlError::FieldInvalidAddress)?;
match length {
8 => Ok(self.handler.read_pci_u8(seg, bbn, device, function, offset) as u64),
16 => Ok(self.handler.read_pci_u16(seg, bbn, device, function, offset) as u64),
32 => Ok(self.handler.read_pci_u32(seg, bbn, device, function, offset) as u64),
_ => Err(AmlError::FieldInvalidAccessSize),
}
}
_ => unimplemented!(),
}
}
pub(crate) fn write_region(
&mut self,
region_handle: AmlHandle,
offset: u64,
length: u64,
value: u64,
) -> Result<(), AmlError> {
use bit_field::BitField;
use core::convert::TryInto;
use value::RegionSpace;
let (region_space, region_base, region_length, parent_device) = {
if let AmlValue::OpRegion { region, offset, length, parent_device } =
self.namespace.get(region_handle)?
{
(region, offset, length, parent_device)
} else {
return Err(AmlError::FieldRegionIsNotOpRegion);
}
};
match region_space {
RegionSpace::SystemMemory => {
let address = (region_base + offset).try_into().map_err(|_| AmlError::FieldInvalidAddress)?;
match length {
8 => Ok(self.handler.write_u8(address, value as u8)),
16 => Ok(self.handler.write_u16(address, value as u16)),
32 => Ok(self.handler.write_u32(address, value as u32)),
64 => Ok(self.handler.write_u64(address, value)),
_ => Err(AmlError::FieldInvalidAccessSize),
}
}
RegionSpace::SystemIo => {
let port = (region_base + offset).try_into().map_err(|_| AmlError::FieldInvalidAddress)?;
match length {
8 => Ok(self.handler.write_io_u8(port, value as u8)),
16 => Ok(self.handler.write_io_u16(port, value as u16)),
32 => Ok(self.handler.write_io_u32(port, value as u32)),
_ => Err(AmlError::FieldInvalidAccessSize),
}
}
RegionSpace::PciConfig => {
let parent_device = parent_device.as_ref().unwrap();
let seg = match self.namespace.search(&AmlName::from_str("_SEG").unwrap(), parent_device) {
Ok((_, handle)) => self
.namespace
.get(handle)?
.as_integer(self)?
.try_into()
.map_err(|_| AmlError::FieldInvalidAddress)?,
Err(AmlError::ValueDoesNotExist(_)) => 0,
Err(err) => return Err(err),
};
let bbn = match self.namespace.search(&AmlName::from_str("_BBN").unwrap(), parent_device) {
Ok((_, handle)) => self
.namespace
.get(handle)?
.as_integer(self)?
.try_into()
.map_err(|_| AmlError::FieldInvalidAddress)?,
Err(AmlError::ValueDoesNotExist(_)) => 0,
Err(err) => return Err(err),
};
let adr = {
let (_, handle) = self.namespace.search(&AmlName::from_str("_ADR").unwrap(), parent_device)?;
self.namespace.get(handle)?.as_integer(self)?
};
let device = adr.get_bits(16..24) as u8;
let function = adr.get_bits(0..8) as u8;
let offset = (region_base + offset).try_into().map_err(|_| AmlError::FieldInvalidAddress)?;
match length {
8 => Ok(self.handler.write_pci_u8(seg, bbn, device, function, offset, value as u8)),
16 => Ok(self.handler.write_pci_u16(seg, bbn, device, function, offset, value as u16)),
32 => Ok(self.handler.write_pci_u32(seg, bbn, device, function, offset, value as u32)),
_ => Err(AmlError::FieldInvalidAccessSize),
}
}
_ => unimplemented!(),
}
}
}
pub trait Handler {
fn read_u8(&self, address: usize) -> u8;
fn read_u16(&self, address: usize) -> u16;
fn read_u32(&self, address: usize) -> u32;
fn read_u64(&self, address: usize) -> u64;
fn write_u8(&mut self, address: usize, value: u8);
fn write_u16(&mut self, address: usize, value: u16);
fn write_u32(&mut self, address: usize, value: u32);
fn write_u64(&mut self, address: usize, value: u64);
fn read_io_u8(&self, port: u16) -> u8;
fn read_io_u16(&self, port: u16) -> u16;
fn read_io_u32(&self, port: u16) -> u32;
fn write_io_u8(&self, port: u16, value: u8);
fn write_io_u16(&self, port: u16, value: u16);
fn write_io_u32(&self, port: u16, value: u32);
fn read_pci_u8(&self, segment: u16, bus: u8, device: u8, function: u8, offset: u16) -> u8;
fn read_pci_u16(&self, segment: u16, bus: u8, device: u8, function: u8, offset: u16) -> u16;
fn read_pci_u32(&self, segment: u16, bus: u8, device: u8, function: u8, offset: u16) -> u32;
fn write_pci_u8(&self, segment: u16, bus: u8, device: u8, function: u8, offset: u16, value: u8);
fn write_pci_u16(&self, segment: u16, bus: u8, device: u8, function: u8, offset: u16, value: u16);
fn write_pci_u32(&self, segment: u16, bus: u8, device: u8, function: u8, offset: u16, value: u32);
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AmlError {
UnexpectedEndOfStream,
UnexpectedByte(u8),
InvalidNameSeg,
InvalidFieldFlags,
IncompatibleValueConversion,
UnterminatedStringConstant,
InvalidStringConstant,
InvalidRegionSpace(u8),
WrongParser,
EmptyNamesAreInvalid,
InvalidNormalizedName(AmlName),
RootHasNoParent,
LevelDoesNotExist(AmlName),
ValueDoesNotExist(AmlName),
NameCollision(AmlName),
TriedToRemoveRootNamespace,
NotExecutingControlMethod,
InvalidArgAccess(ArgNum),
InvalidLocalAccess(LocalNum),
Return(AmlValue),
PrtInvalidAddress,
PrtInvalidPin,
PrtInvalidSource,
PrtInvalidGsi,
PrtNoEntry,
ReservedResourceType,
ResourceDescriptorTooShort,
InvalidStatusObject,
InvalidShiftLeft,
InvalidShiftRight,
FieldRegionIsNotOpRegion,
FieldInvalidAddress,
FieldInvalidAccessSize,
TypeCannotBeCompared(AmlType),
}