Browse Source

Add proper error types and getters for SDTS

Isaac Woods 7 years ago
parent
commit
58911a0760
2 changed files with 48 additions and 3 deletions
  1. 5 0
      src/lib.rs
  2. 43 3
      src/sdt.rs

+ 5 - 0
src/lib.rs

@@ -16,6 +16,11 @@ pub enum AcpiError
     RsdpIncorrectSignature,
     RsdpInvalidOemId,
     RsdpInvalidChecksum,
+
+    SdtInvalidSignature,
+    SdtInvalidOemId,
+    SdtInvalidTableId,
+    SdtInvalidChecksum,
 }
 
 /// Describes a physical mapping created by `AcpiHandler::map_physical_region` and unmapped by

+ 43 - 3
src/sdt.rs

@@ -23,12 +23,24 @@ impl SdtHeader
     ///     b) The checksum of the SDT.
     ///
     /// This assumes that the whole SDT is mapped.
-    fn validate(&self) -> Result<(), &str>
+    fn validate(&self) -> Result<(), AcpiError>
     {
         // Check the signature
         if str::from_utf8(&self.signature).is_err()
         {
-            return Err("SDT signature is not valid UTF8");
+            return Err(AcpiError::SdtInvalidSignature);
+        }
+
+        // Check the OEM id
+        if str::from_utf8(&self.oem_id).is_err()
+        {
+            return Err(AcpiError::SdtInvalidOemId);
+        }
+
+        // Check the OEM table id
+        if str::from_utf8(&self.oem_table_id).is_err()
+        {
+            return Err(AcpiError::SdtInvalidTableId);
         }
 
         // Sum all bytes in the SDT (not just the header)
@@ -41,9 +53,37 @@ impl SdtHeader
         // Check that the lowest byte is 0
         if sum & 0b1111_1111 != 0
         {
-            return Err("SDT has incorrect checksum");
+            return Err(AcpiError::SdtInvalidChecksum);
         }
 
         Ok(())
     }
+
+    pub fn signature<'a>(&'a self) -> &'a str
+    {
+        // Safe to unwrap because we check signature is valid UTF8 in `validate`
+        str::from_utf8(&self.signature).unwrap()
+    }
+
+    pub fn length(&self) -> u32
+    {
+        self.length
+    }
+
+    pub fn revision(&self) -> u8
+    {
+        self.revision
+    }
+
+    pub fn oem_id<'a>(&'a self) -> &'a str
+    {
+        // Safe to unwrap because checked in `validate`
+        str::from_utf8(&self.oem_id).unwrap()
+    }
+
+    pub fn oem_table_id<'a>(&'a self) -> &'a str
+    {
+        // Safe to unwrap because checked in `validate`
+        str::from_utf8(&self.oem_table_id).unwrap()
+    }
 }