Skip to content
32 changes: 16 additions & 16 deletions src/aml/mod.rs
Comment thread
martin-hughes marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -677,13 +677,11 @@ where
}
}
Opcode::Notify => {
// TODO: may need special handling on the node to get path?
extract_args!(op => [Argument::Namestring(name), Argument::Object(value)]);
let value = value.as_integer()?;

info!("Notify {:?} with value {}", name, value);
// TODO: support
return Err(AmlError::LibUnimplemented);
// TODO: support notify handlers (ACPICA without one ignores it too)
extract_args!(op => [Argument::Object(object), Argument::Object(value)]);
let value = value.clone().unwrap_transparent_reference().as_integer()?;
info!("Ignoring Notify of {} with value {}", object, value);
context.retire_op(op);
Comment thread
martin-hughes marked this conversation as resolved.
Resolved
}
Opcode::FromBCD => self.do_from_bcd(&mut context, op)?,
Opcode::ToBCD => self.do_to_bcd(&mut context, op)?,
Expand Down Expand Up @@ -829,7 +827,7 @@ where
Argument::PkgLength(then_length),
Argument::Object(predicate),
]);
let predicate = predicate.as_integer()?;
let predicate = predicate.clone().unwrap_transparent_reference().as_integer()?;
let remaining_then_length = then_length - (context.current_block.pc - start_pc);

if predicate > 0 {
Expand Down Expand Up @@ -862,7 +860,7 @@ where
| opcode @ Opcode::CreateQWordField => {
extract_args!(op => [Argument::Object(buffer), Argument::Object(index)]);
let name = context.namestring()?;
let index = index.as_integer()?;
let index = index.clone().unwrap_transparent_reference().as_integer()?;
let (offset, length) = match opcode {
Opcode::CreateBitField => (index, 1),
Opcode::CreateByteField => (index * 8, 8),
Expand All @@ -880,8 +878,8 @@ where
Opcode::CreateField => {
extract_args!(op => [Argument::Object(buffer), Argument::Object(bit_index), Argument::Object(num_bits)]);
let name = context.namestring()?;
let bit_index = bit_index.as_integer()?;
let num_bits = num_bits.as_integer()?;
let bit_index = bit_index.clone().unwrap_transparent_reference().as_integer()?;
let num_bits = num_bits.clone().unwrap_transparent_reference().as_integer()?;

self.namespace.lock().insert(
name.resolve(&context.current_scope)?,
Expand Down Expand Up @@ -2144,7 +2142,7 @@ where
let result = if source.is_empty() {
Object::String(String::new())
} else {
let mut buffer = source.split_inclusive(|b| *b == b'\0').next().unwrap();
let mut buffer = source.split(|b| *b == b'\0').next().unwrap();
if length < usize::MAX {
buffer = &buffer[0..usize::min(length, buffer.len())];
}
Expand Down Expand Up @@ -2388,7 +2386,7 @@ where
}
Object::Package(ref package) => {
let Some(element) = package.get(index_value as usize) else { Err(AmlError::IndexOutOfBounds)? };
Object::Reference { kind: ReferenceKind::RefOf, inner: element.clone() }
Object::Reference { kind: ReferenceKind::Index, inner: element.clone() }
}
_ => Err(AmlError::IndexOutOfBounds)?,
}
Expand Down Expand Up @@ -2451,11 +2449,13 @@ where
}

fn do_deref_of(&self, object: WrappedObject, current_scope: &AmlName) -> Result<WrappedObject, AmlError> {
let object = object.unwrap_transparent_reference();
let through_reference = matches!(*object, Object::Reference { .. });
let object = self.resolve_name_path(object)?;
match &*object {
Object::BufferField { .. } => Ok(object.read_buffer_field(self.integer_size)?.wrap()),
Object::FieldUnit(field) => self.do_field_read(field),
Object::String(path) => {
Object::String(path) if !through_reference => {
let path = AmlName::from_str(path)?;
let (_, object) = self.namespace.lock().search(&path, current_scope)?;
Ok(object.clone())
Expand All @@ -2467,7 +2467,7 @@ where

/// Perform a store of `object` into `target`, matching the expected behaviour of `DefStore`,
/// which depends on the target:
/// - Locals and Index references are overwritten, unless the Local contains a reference
/// - Locals and Index references are overwritten, unless the Local or element contains a reference
/// (e.g. obtained via `RefOf`), in which case a store is performed to the referenced
/// object with implicit casting.
/// - Args are overwritten, unless they contain a reference, in which case the referenced
Expand Down Expand Up @@ -2563,7 +2563,7 @@ where
inner.clone().unwrap_transparent_reference()
}
}
ReferenceKind::Index => todo!(),
ReferenceKind::Index => inner.clone(),
ReferenceKind::RefOf | ReferenceKind::Unresolved => return Err(AmlError::StoreToInvalidReferenceType),
};

Expand Down
4 changes: 3 additions & 1 deletion src/aml/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ impl Namespace {
} else {
// If search rules don't apply, simply resolve it against the starting scope
let name = path.resolve(starting_scope)?;
let (level, last_seg) = self.get_level_for_path(&path.resolve(starting_scope)?)?;
let Ok((level, last_seg)) = self.get_level_for_path(&name) else {
return Err(AmlError::ObjectDoesNotExist(path.clone()));
};

if let Some((_, object)) = level.values.get(&last_seg) {
Ok((name, object.clone()))
Expand Down
8 changes: 6 additions & 2 deletions src/aml/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ impl WrappedObject {
// for the strange Windows NT behaviour handled in the `match arg_to_local` statement below.
let mut arg_to_local: Option<WrappedObject> = None;

let mut target = self;
// A package element is replaced, unless it holds a reference
let mut target = match *self {
Object::Reference { kind: ReferenceKind::Index, ref inner } => inner.clone(),
_ => self.clone(),
};

loop {
let Object::Reference { kind, ref inner } = *target else {
Expand Down Expand Up @@ -338,7 +342,7 @@ impl Object {
});
}
};
if *length <= integer_size as usize {
if *length <= integer_size as usize * 8 {
let mut dst = [0u8; 8];
copy_bits(buffer, *offset, &mut dst, 0, *length);
Ok(Object::Integer(u64::from_le_bytes(dst)))
Expand Down
38 changes: 38 additions & 0 deletions tests/buffer_fields.asl
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,42 @@ DefinitionBlock("buffer_fields.aml", "DSDT", 1, "RSACPI", "BUFFLD", 1) {
// `Y` should end up as `Integer(0x07060504)` (`Integer(117835012)` in decimal)
Name(Y, 4)
Y = DWRD

Name(FCNT, 0)

Method (CHEK, 2) {
If (Arg0 != Arg1) {
FCNT++
}
}

Method (T1) {
// Fields up to 64 bits wide read as integers
CHEK(WORD, 0x0302)
CHEK(DWRD, 0x07060504)
}

Method (GETD, 1) {
CreateDWordField(X, Arg0, FLD)
Return (FLD)
}

Method (GETF, 2) {
Local0 = Arg0
Local1 = Arg1
CreateField(X, Local0, Local1, FLD)
Return (FLD)
}

Method (T2) {
// The index and size of a field from an argument or a local
CHEK(GETD(4), 0x07060504)
CHEK(GETF(32, 16), 0x0504)
}

Method (MAIN) {
T1()
T2()
Return (FCNT)
}
}
34 changes: 34 additions & 0 deletions tests/de_ref_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,37 @@ DefinitionBlock ("", "SSDT", 2, "RSACPI", "DerefOf", 0x00000002) {
let handler = NullHandler {};
test_infra::run_aml_test(AML, handler);
}

#[test]
Comment thread
martin-hughes marked this conversation as resolved.
Resolved
fn test_deref_of_package_string_element() {
const AML: &str = r#"
DefinitionBlock ("", "SSDT", 2, "RSACPI", "DerefOf", 0x00000002) {
Name (TGT, 0xaa)
Name (PKG, Package () { "\\TGT" })

Method (MAIN, 0, NotSerialized) {
Return (ObjectType (DerefOf (PKG[0])) - 2)
}
}
"#;

let handler = NullHandler {};
test_infra::run_aml_test(AML, handler);
}

#[test]
fn test_deref_of_ref_to_string() {
const AML: &str = r#"
DefinitionBlock ("", "SSDT", 2, "RSACPI", "DerefOf", 0x00000002) {
Name (STR, "TestString")

Method(MAIN, 0, NotSerialized) {
Local0 = RefOf(STR)
Return (DerefOf(Local0) != "TestString")
}
}
"#;

let handler = NullHandler {};
test_infra::run_aml_test(AML, handler);
}
16 changes: 16 additions & 0 deletions tests/name_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,19 @@ DefinitionBlock("", "DSDT", 1, "RSACPI", "NAMING", 1) {
let handler = NullHandler {};
test_infra::run_aml_test(AML, handler);
}

#[test]
fn test_cond_ref_of_missing_parent() {
const AML: &str = r#"
DefinitionBlock("", "DSDT", 1, "RSACPI", "NAMING", 1) {
External (\_SB.NOPE.XXXX, IntObj)

Method (MAIN, 0, NotSerialized) {
Return (CondRefOf (\_SB.NOPE.XXXX))
}
}
"#;

let handler = NullHandler {};
test_infra::run_aml_test(AML, handler);
}
23 changes: 23 additions & 0 deletions tests/notify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use aml_test_tools::handlers::null_handler::NullHandler;

mod test_infra;

#[test]
fn test_notify() {
const AML: &str = r#"
DefinitionBlock ("", "SSDT", 2, "RSACPI", "NOTIFY", 0x00000002) {
Device (LID0) {
Name (_HID, EisaId ("PNP0C0D"))
}
Method (MAIN, 0, NotSerialized) {
Notify (LID0, 0x80)
Local0 = 0x80
Notify (\LID0, Local0)
Return (0)
}
}
"#;

let handler = NullHandler {};
test_infra::run_aml_test(AML, handler);
}
16 changes: 16 additions & 0 deletions tests/store.asl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ DefinitionBlock ("", "DSDT", 2, "RSACPI", "TESTTABL", 0xF0F0F0F0)
CHEK (Local1, 2)
}

// A store to a package element replaces it, but through a Local it's cast
Method (T8) {
Name(PKG, Package () { 0, 1 })
PKG[0] = "ABC"
CHEK(ObjectType(PKG[0]), 2)
}

Method (T9) {
Name(PKG, Package () { 0 })
Local1 = Index(PKG, 0)
Local1 = "ABC"
CHEK(ObjectType(PKG[0]), 1)
}

// uACPI equivalent: references-0.asl
Method (U1) {
Local0 = "MyString"
Expand Down Expand Up @@ -171,6 +185,8 @@ DefinitionBlock ("", "DSDT", 2, "RSACPI", "TESTTABL", 0xF0F0F0F0)
T5()
T6()
T7(0)
T8()
T9()

// uACPI equivalents given in the comments:
U1() // references-0
Expand Down
5 changes: 5 additions & 0 deletions tests/to_x.asl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ DefinitionBlock ("", "SSDT", 2, "uTEST", "TESTTABL", 0xF0F0F0F0)
Local1 = "0xFFFFFFFFFFFFFFFF"
CHEK(Local0, Local1)

// String
Local0 = ToString(Buffer { 0x41, 0x42, 0x00, 0x43 }, Ones)
Local1 = "AB"
CHEK(Local0, Local1)

// Buffer
Local0 = ToBuffer(Buffer { 1, 2, 3 })
Local1 = Buffer { 1, 2, 3 }
Expand Down
11 changes: 11 additions & 0 deletions tests/while.asl
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,23 @@ DefinitionBlock("while.aml", "DSDT", 1, "RSACPI", "WHILE", 1) {
CHEK(Local1, 0)
}

Method(T6) {
// Test `If` with a local in the predicate, as `While` in T5
Local0 = 1
Local1 = 0
If (Local0) {
Local1 = 1
}
CHEK(Local1, 1)
}

Method(MAIN) {
T1()
T2()
T3()
T4()
T5()
T6()

Return(FCNT)
}
Expand Down
Loading