From bf0d0d9f9b76c9c3e8d8e5e47b2cf1697ce694ce Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Wed, 16 Oct 2019 17:42:31 +0100 Subject: [PATCH] acpi_tables: aml: Add support for named definitions Named definitions use op code 0x08 and are used to name some more complex object. Signed-off-by: Rob Bradford --- acpi_tables/src/aml.rs | 53 +++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/acpi_tables/src/aml.rs b/acpi_tables/src/aml.rs index 52eb72d96..073818974 100644 --- a/acpi_tables/src/aml.rs +++ b/acpi_tables/src/aml.rs @@ -62,9 +62,9 @@ impl From<&str> for Path { } } -pub type AmlByte = u8; +pub type Byte = u8; -impl Aml for AmlByte { +impl Aml for Byte { fn to_bytes(&self) -> Vec { let mut bytes = Vec::new(); bytes.push(0x0a); /* BytePrefix */ @@ -73,9 +73,9 @@ impl Aml for AmlByte { } } -pub type AmlWord = u16; +pub type Word = u16; -impl Aml for AmlWord { +impl Aml for Word { fn to_bytes(&self) -> Vec { let mut bytes = Vec::new(); bytes.push(0x0bu8); /* WordPrefix */ @@ -84,9 +84,9 @@ impl Aml for AmlWord { } } -pub type AmlDWord = u32; +pub type DWord = u32; -impl Aml for AmlDWord { +impl Aml for DWord { fn to_bytes(&self) -> Vec { let mut bytes = Vec::new(); bytes.push(0x0c); /* DWordPrefix */ @@ -95,9 +95,9 @@ impl Aml for AmlDWord { } } -pub type AmlQWord = u64; +pub type QWord = u64; -impl Aml for AmlQWord { +impl Aml for QWord { fn to_bytes(&self) -> Vec { let mut bytes = Vec::new(); bytes.push(0x0e); /* QWordPrefix */ @@ -106,6 +106,26 @@ impl Aml for AmlQWord { } } +pub struct Name { + bytes: Vec, +} + +impl Aml for Name { + fn to_bytes(&self) -> Vec { + self.bytes.clone() + } +} + +impl Name { + pub fn new(path: Path, inner: &dyn Aml) -> Self { + let mut bytes = Vec::new(); + bytes.push(0x08); /* NameOp */ + bytes.append(&mut path.to_bytes()); + bytes.append(&mut inner.to_bytes()); + Name { bytes } + } +} + #[cfg(test)] mod tests { use super::*; @@ -140,4 +160,21 @@ mod tests { [0x0e, 0xad, 0xfb, 0xca, 0xde, 0xad, 0xfb, 0xca, 0xde] ); } + + #[test] + fn test_name() { + assert_eq!( + Name::new("_SB_.PCI0._UID".into(), &0x1234u16).to_bytes(), + [ + 0x08, /* NameOp */ + 0x2F, /* MultiNamePrefix */ + 0x03, /* 3 name parts */ + 0x5F, 0x53, 0x42, 0x5F, /* _SB_ */ + 0x50, 0x43, 0x49, 0x30, /* PCI0 */ + 0x5F, 0x55, 0x49, 0x44, /* _UID */ + 0x0b, /* WordPrefix */ + 0x34, 0x12 + ] + ); + } }