Browse Source

Decouple handler and Acpi type

Isaac Woods 6 years ago
parent
commit
ebada32de9
5 changed files with 41 additions and 55 deletions
  1. 5 5
      src/aml/mod.rs
  2. 6 5
      src/aml/parser.rs
  3. 8 10
      src/fadt.rs
  4. 9 16
      src/lib.rs
  5. 13 19
      src/sdt.rs

+ 5 - 5
src/aml/mod.rs

@@ -42,18 +42,18 @@ impl AmlTable {
     }
 }
 
-pub(crate) fn parse_aml_table<'a, 'h, H>(
-    acpi: &'a mut Acpi<'h, H>,
+pub(crate) fn parse_aml_table<H>(
+    acpi: &mut Acpi,
+    handler: &mut H,
     mapping: &PhysicalMapping<AmlTable>,
     signature: &[u8; 4],
 ) -> Result<(), AcpiError>
 where
-    'h: 'a,
-    H: AcpiHandler + 'a,
+    H: AcpiHandler,
 {
     (*mapping).header.validate(signature)?;
 
-    match AmlParser::parse(acpi, "\\", (*mapping).stream()) {
+    match AmlParser::parse(acpi, handler, "\\", (*mapping).stream()) {
         Ok(_) => Ok(()),
         Err(error) => Err(AcpiError::InvalidAmlTable(*signature, error)),
     }

+ 6 - 5
src/aml/parser.rs

@@ -24,10 +24,10 @@ struct PkgLength {
 
 pub(crate) struct AmlParser<'s, 'a, 'h, H>
 where
-    'h: 'a,
     H: AcpiHandler + 'h,
 {
-    acpi: &'a mut Acpi<'h, H>,
+    acpi: &'a mut Acpi,
+    handler: &'h mut H,
     scope: String,
     stream: AmlStream<'s>,
 }
@@ -48,16 +48,17 @@ macro_rules! try_parse {
 
 impl<'s, 'a, 'h, H> AmlParser<'s, 'a, 'h, H>
 where
-    'h: 'a,
-    H: AcpiHandler + 'h,
+    H: AcpiHandler,
 {
     pub(crate) fn parse(
-        acpi: &'a mut Acpi<'h, H>,
+        acpi: &'a mut Acpi,
+        handler: &'h mut H,
         scope: &str,
         stream: AmlStream<'s>,
     ) -> Result<(), AmlError> {
         let mut parser = AmlParser {
             acpi,
+            handler,
             scope: String::from(scope),
             stream,
         };

+ 8 - 10
src/fadt.rs

@@ -74,13 +74,13 @@ pub struct Fadt {
     hypervisor_vendor_id: u64,
 }
 
-pub(crate) fn parse_fadt<'a, 'h, H>(
-    acpi: &'a mut Acpi<'h, H>,
+pub(crate) fn parse_fadt<H>(
+    acpi: &mut Acpi,
+    handler: &mut H,
     mapping: &PhysicalMapping<Fadt>,
 ) -> Result<(), AcpiError>
 where
-    'h: 'a,
-    H: AcpiHandler + 'a,
+    H: AcpiHandler,
 {
     (*mapping).header.validate(b"FACP")?;
 
@@ -91,14 +91,12 @@ where
     };
 
     // Parse the DSDT
-    let dsdt_header = sdt::peek_at_sdt_header(acpi.handler, dsdt_physical_address);
-    let dsdt_mapping = acpi
-        .handler
-        .map_physical_region::<AmlTable>(dsdt_physical_address, dsdt_header.length() as usize);
-    if let Err(error) = parse_aml_table(acpi, &dsdt_mapping, b"DSDT") {
+    let dsdt_header = sdt::peek_at_sdt_header(handler, dsdt_physical_address);
+    let dsdt_mapping = handler.map_physical_region::<AmlTable>(dsdt_physical_address, dsdt_header.length() as usize);
+    if let Err(error) = parse_aml_table(acpi, handler, &dsdt_mapping, b"DSDT") {
         error!("Failed to parse DSDT: {:?}. At this stage, this is expected, but should be fatal in the future", error);
     }
-    acpi.handler.unmap_physical_region(dsdt_mapping);
+    handler.unmap_physical_region(dsdt_mapping);
 
     Ok(())
 }

+ 9 - 16
src/lib.rs

@@ -94,12 +94,8 @@ pub trait AcpiHandler {
     fn unmap_physical_region<T>(&mut self, region: PhysicalMapping<T>);
 }
 
-/// This struct manages the internal state of `acpi`. It is not visible to the user of the library.
-pub(crate) struct Acpi<'a, H>
-where
-    H: AcpiHandler + 'a,
+pub struct Acpi
 {
-    handler: &'a mut H,
     acpi_revision: u8,
     namespace: BTreeMap<String, AmlValue>,
 }
@@ -107,7 +103,7 @@ where
 /// This is the entry point of `acpi` if you have the **physical** address of the RSDP. It maps
 /// the RSDP, works out what version of ACPI the hardware supports, and passes the physical
 /// address of the RSDT/XSDT to `parse_rsdt`.
-pub fn parse_rsdp<H>(handler: &mut H, rsdp_address: usize) -> Result<(), AcpiError>
+pub fn parse_rsdp<H>(handler: &mut H, rsdp_address: usize) -> Result<Acpi, AcpiError>
 where
     H: AcpiHandler,
 {
@@ -154,20 +150,17 @@ pub fn parse_rsdt<H>(
     handler: &mut H,
     revision: u8,
     physical_address: usize,
-) -> Result<(), AcpiError>
+) -> Result<Acpi, AcpiError>
 where
     H: AcpiHandler,
 {
     let mut acpi = Acpi {
-        handler,
         acpi_revision: revision,
         namespace: BTreeMap::new(),
     };
 
-    let header = sdt::peek_at_sdt_header(acpi.handler, physical_address);
-    let mapping = acpi
-        .handler
-        .map_physical_region::<SdtHeader>(physical_address, header.length() as usize);
+    let header = sdt::peek_at_sdt_header(handler, physical_address);
+    let mapping = handler.map_physical_region::<SdtHeader>(physical_address, header.length() as usize);
 
     if revision == 0 {
         /*
@@ -181,7 +174,7 @@ where
             ((mapping.virtual_start.as_ptr() as usize) + mem::size_of::<SdtHeader>()) as *const u32;
 
         for i in 0..num_tables {
-            sdt::dispatch_sdt(&mut acpi, unsafe { *tables_base.offset(i as isize) }
+            sdt::dispatch_sdt(&mut acpi, handler, unsafe { *tables_base.offset(i as isize) }
                 as usize)?;
         }
     } else {
@@ -196,15 +189,15 @@ where
             ((mapping.virtual_start.as_ptr() as usize) + mem::size_of::<SdtHeader>()) as *const u64;
 
         for i in 0..num_tables {
-            sdt::dispatch_sdt(&mut acpi, unsafe { *tables_base.offset(i as isize) }
+            sdt::dispatch_sdt(&mut acpi, handler, unsafe { *tables_base.offset(i as isize) }
                 as usize)?;
         }
     }
 
     info!("Parsed namespace: {:#?}", acpi.namespace);
 
-    acpi.handler.unmap_physical_region(mapping);
-    Ok(())
+    handler.unmap_physical_region(mapping);
+    Ok(acpi)
 }
 
 #[cfg(test)]

+ 13 - 19
src/sdt.rs

@@ -156,15 +156,15 @@ where
 
 /// This takes the physical address of an SDT, maps it correctly and dispatches it to whatever
 /// function parses that table.
-pub(crate) fn dispatch_sdt<'a, 'h, H>(
-    acpi: &'a mut Acpi<'h, H>,
+pub(crate) fn dispatch_sdt<H>(
+    acpi: &mut Acpi,
+    handler: &mut H,
     physical_address: usize,
 ) -> Result<(), AcpiError>
 where
-    'h: 'a,
-    H: AcpiHandler + 'a,
+    H: AcpiHandler,
 {
-    let header = peek_at_sdt_header(acpi.handler, physical_address);
+    let header = peek_at_sdt_header(handler, physical_address);
     info!(
         "Dispatching SDT with signature {:?} and length {:?}",
         header.signature(),
@@ -177,27 +177,21 @@ where
      */
     match header.signature() {
         "FACP" => {
-            let fadt_mapping = acpi
-                .handler
-                .map_physical_region::<Fadt>(physical_address, mem::size_of::<Fadt>());
-            ::fadt::parse_fadt(acpi, &fadt_mapping)?;
-            acpi.handler.unmap_physical_region(fadt_mapping);
+            let fadt_mapping = handler.map_physical_region::<Fadt>(physical_address, mem::size_of::<Fadt>());
+            ::fadt::parse_fadt(acpi, handler, &fadt_mapping)?;
+            handler.unmap_physical_region(fadt_mapping);
         }
 
         "HPET" => {
-            let hpet_mapping = acpi
-                .handler
-                .map_physical_region::<Hpet>(physical_address, mem::size_of::<Hpet>());
+            let hpet_mapping = handler.map_physical_region::<Hpet>(physical_address, mem::size_of::<Hpet>());
             ::hpet::parse_hpet(&hpet_mapping)?;
-            acpi.handler.unmap_physical_region(hpet_mapping);
+            handler.unmap_physical_region(hpet_mapping);
         }
 
         "APIC" => {
-            let madt_mapping = acpi
-                .handler
-                .map_physical_region::<Madt>(physical_address, header.length() as usize);
-            ::madt::parse_madt(acpi, &madt_mapping)?;
-            acpi.handler.unmap_physical_region(madt_mapping);
+            let madt_mapping = handler.map_physical_region::<Madt>(physical_address, header.length() as usize);
+            ::madt::parse_madt(acpi, handler, &madt_mapping)?;
+            handler.unmap_physical_region(madt_mapping);
         }
 
         signature => {