Browse Source

Merge pull request #90 from rust-osdev/stricter-typing

Stricter typing + better split of modules + bugfix
Isaac Woods 3 years ago
parent
commit
661b478619
9 changed files with 103 additions and 87 deletions
  1. 3 1
      src/boot_loader_name.rs
  2. 3 1
      src/command_line.rs
  3. 53 0
      src/efi.rs
  4. 11 0
      src/image_load_addr.rs
  5. 5 1
      src/lib.rs
  6. 19 19
      src/memory_map.rs
  7. 1 1
      src/module.rs
  8. 6 63
      src/rsdp.rs
  9. 2 1
      src/vbe_info.rs

+ 3 - 1
src/boot_loader_name.rs

@@ -1,3 +1,5 @@
+use crate::TagType;
+
 /// This Tag contains the name of the bootloader that is booting the kernel.
 ///
 /// The name is a normal C-style UTF-8 zero-terminated string that can be
@@ -5,7 +7,7 @@
 #[derive(Clone, Copy, Debug)]
 #[repr(C, packed)] // only repr(C) would add unwanted padding before first_section
 pub struct BootLoaderNameTag {
-    typ: u32,
+    typ: TagType,
     size: u32,
     string: u8,
 }

+ 3 - 1
src/command_line.rs

@@ -1,3 +1,5 @@
+use crate::TagType;
+
 /// This Tag contains the command line string.
 ///
 /// The string is a normal C-style UTF-8 zero-terminated string that can be
@@ -5,7 +7,7 @@
 #[derive(Clone, Copy, Debug)]
 #[repr(C, packed)] // only repr(C) would add unwanted padding before first_section
 pub struct CommandLineTag {
-    typ: u32,
+    typ: TagType,
     size: u32,
     string: u8,
 }

+ 53 - 0
src/efi.rs

@@ -0,0 +1,53 @@
+//! All MBI tags related to (U)EFI.
+
+use crate::TagType;
+
+/// EFI system table in 32 bit mode
+#[derive(Clone, Copy, Debug)]
+#[repr(C, packed)] // only repr(C) would add unwanted padding before first_section
+pub struct EFISdt32 {
+    typ: TagType,
+    size: u32,
+    pointer: u32,
+}
+
+impl EFISdt32 {
+    /// The Physical address of a i386 EFI system table.
+    pub fn sdt_address(&self) -> usize {
+        self.pointer as usize
+    }
+}
+
+/// EFI system table in 64 bit mode
+#[derive(Clone, Copy, Debug)]
+#[repr(C)]
+pub struct EFISdt64 {
+    typ: TagType,
+    size: u32,
+    pointer: u64,
+}
+
+impl EFISdt64 {
+    /// The Physical address of a x86_64 EFI system table.
+    pub fn sdt_address(&self) -> usize {
+        self.pointer as usize
+    }
+}
+
+/// Contains pointer to boot loader image handle.
+#[derive(Debug)]
+#[repr(C)]
+pub struct EFIImageHandle32 {
+    typ: TagType,
+    size: u32,
+    pointer: u32,
+}
+
+/// Contains pointer to boot loader image handle.
+#[derive(Debug)]
+#[repr(C)]
+pub struct EFIImageHandle64 {
+    typ: TagType,
+    size: u32,
+    pointer: u64,
+}

+ 11 - 0
src/image_load_addr.rs

@@ -0,0 +1,11 @@
+use crate::TagType;
+
+/// If the image has relocatable header tag, this tag contains the image's
+/// base physical address.
+#[derive(Debug)]
+#[repr(C)]
+pub struct ImageLoadPhysAddr {
+    typ: TagType,
+    size: u32,
+    load_base_addr: u32,
+}

+ 5 - 1
src/lib.rs

@@ -39,8 +39,10 @@ pub use memory_map::{
 };
 pub use module::{ModuleIter, ModuleTag};
 pub use rsdp::{
-    EFIImageHandle32, EFIImageHandle64, EFISdt32, EFISdt64, ImageLoadPhysAddr, RsdpV1Tag, RsdpV2Tag,
+    RsdpV1Tag, RsdpV2Tag,
 };
+pub use image_load_addr::ImageLoadPhysAddr;
+pub use efi::{EFIImageHandle32, EFIImageHandle64, EFISdt32, EFISdt64};
 pub use vbe_info::{
     VBECapabilities, VBEControlInfo, VBEDirectColorAttributes, VBEField, VBEInfoTag,
     VBEMemoryModel, VBEModeAttributes, VBEModeInfo, VBEWindowAttributes,
@@ -58,6 +60,8 @@ mod memory_map;
 mod module;
 mod rsdp;
 mod vbe_info;
+mod efi;
+mod image_load_addr;
 
 /// Load the multiboot boot information struct from an address.
 ///

+ 19 - 19
src/memory_map.rs

@@ -1,4 +1,5 @@
 use core::marker::PhantomData;
+use crate::TagType;
 
 /// This Tag provides an initial host memory map.
 ///
@@ -13,7 +14,7 @@ use core::marker::PhantomData;
 #[derive(Debug)]
 #[repr(C)]
 pub struct MemoryMapTag {
-    typ: u32,
+    typ: TagType,
     size: u32,
     entry_size: u32,
     entry_version: u32,
@@ -23,7 +24,7 @@ pub struct MemoryMapTag {
 impl MemoryMapTag {
     /// Return an iterator over all AVAILABLE marked memory areas.
     pub fn memory_areas(&self) -> impl Iterator<Item = &MemoryArea> {
-        self.all_memory_areas().filter(|entry| entry.typ == 1)
+        self.all_memory_areas().filter(|entry| matches!(entry.typ, MemoryAreaType::Available))
     }
 
     /// Return an iterator over all marked memory areas.
@@ -45,7 +46,7 @@ impl MemoryMapTag {
 pub struct MemoryArea {
     base_addr: u64,
     length: u64,
-    typ: u32,
+    typ: MemoryAreaType,
     _reserved: u32,
 }
 
@@ -67,33 +68,32 @@ impl MemoryArea {
 
     /// The type of the memory region.
     pub fn typ(&self) -> MemoryAreaType {
-        match self.typ {
-            1 => MemoryAreaType::Available,
-            3 => MemoryAreaType::AcpiAvailable,
-            4 => MemoryAreaType::ReservedHibernate,
-            5 => MemoryAreaType::Defective,
-            _ => MemoryAreaType::Reserved,
-        }
+        self.typ
     }
 }
 
 /// An enum of possible reported region types.
-#[derive(Debug, PartialEq, Eq)]
+/// Inside the Multiboot2 spec this is kind of hidden
+/// inside the implementation of `struct multiboot_mmap_entry`.
+#[derive(Debug, PartialEq, Eq, Copy, Clone)]
+#[repr(u32)]
 pub enum MemoryAreaType {
-    /// A reserved area that must not be used.
-    Reserved,
-
     /// Available memory free to be used by the OS.
-    Available,
+    Available = 1,
+
+    /// A reserved area that must not be used.
+    Reserved = 2,
 
     /// Usable memory holding ACPI information.
-    AcpiAvailable,
+    AcpiAvailable = 3,
 
     /// Reserved memory which needs to be preserved on hibernation.
-    ReservedHibernate,
+    /// Also called NVS in spec, which stands for "Non-Volatile Sleep/Storage",
+    /// which is part of ACPI specification.
+    ReservedHibernate = 4,
 
     /// Memory which is occupied by defective RAM modules.
-    Defective,
+    Defective = 5,
 }
 
 /// An iterator over all memory areas
@@ -122,7 +122,7 @@ impl<'a> Iterator for MemoryAreaIter<'a> {
 #[derive(Debug)]
 #[repr(C)]
 pub struct EFIMemoryMapTag {
-    typ: u32,
+    typ: TagType,
     size: u32,
     desc_size: u32,
     desc_version: u32,

+ 1 - 1
src/module.rs

@@ -6,7 +6,7 @@ use core::fmt::{Debug, Formatter};
 #[derive(Clone, Copy)]
 #[repr(C, packed)] // only repr(C) would add unwanted padding near name_byte.
 pub struct ModuleTag {
-    typ: u32,
+    typ: TagType,
     size: u32,
     mod_start: u32,
     mod_end: u32,

+ 6 - 63
src/rsdp.rs

@@ -1,3 +1,6 @@
+//! Module for RSDP/ACPI. RSDP (Root System Description Pointer) is a data structure used in the
+//! ACPI programming interface.
+//!
 //! The tag that the bootloader passes will depend on the ACPI version the hardware supports.
 //! For ACPI Version 1.0, a `RsdpV1Tag` will be provided, which can be accessed from
 //! `BootInformation` using the `rsdp_v1_tag` function. For subsequent versions of ACPI, a
@@ -5,77 +8,17 @@
 //!
 //! Even though the bootloader should give the address of the real RSDP/XSDT, the checksum and
 //! signature should be manually verified.
-
 use core::slice;
 use core::str;
+use crate::TagType;
 
 const RSDPV1_LENGTH: usize = 20;
 
-/// EFI system table in 32 bit mode
-#[derive(Clone, Copy, Debug)]
-#[repr(C, packed)] // only repr(C) would add unwanted padding before first_section
-pub struct EFISdt32 {
-    typ: u32,
-    size: u32,
-    pointer: u32,
-}
-
-impl EFISdt32 {
-    /// The Physical address of a i386 EFI system table.
-    pub fn sdt_address(&self) -> usize {
-        self.pointer as usize
-    }
-}
-
-/// EFI system table in 64 bit mode
-#[derive(Clone, Copy, Debug)]
-#[repr(C)]
-pub struct EFISdt64 {
-    typ: u32,
-    size: u32,
-    pointer: u64,
-}
-
-impl EFISdt64 {
-    /// The Physical address of a x86_64 EFI system table.
-    pub fn sdt_address(&self) -> usize {
-        self.pointer as usize
-    }
-}
-
-/// Contains pointer to boot loader image handle.
-#[derive(Debug)]
-#[repr(C)]
-pub struct EFIImageHandle32 {
-    typ: u32,
-    size: u32,
-    pointer: u32,
-}
-
-/// Contains pointer to boot loader image handle.
-#[derive(Debug)]
-#[repr(C)]
-pub struct EFIImageHandle64 {
-    typ: u32,
-    size: u32,
-    pointer: u64,
-}
-
-/// If the image has relocatable header tag, this tag contains the image's
-/// base physical address.
-#[derive(Debug)]
-#[repr(C)]
-pub struct ImageLoadPhysAddr {
-    typ: u32,
-    size: u32,
-    load_base_addr: u32,
-}
-
 /// This tag contains a copy of RSDP as defined per ACPI 1.0 specification.
 #[derive(Clone, Copy, Debug)]
 #[repr(C, packed)]
 pub struct RsdpV1Tag {
-    typ: u32,
+    typ: TagType,
     size: u32,
     signature: [u8; 8],
     checksum: u8,
@@ -122,7 +65,7 @@ impl RsdpV1Tag {
 #[derive(Clone, Copy, Debug)]
 #[repr(C, packed)]
 pub struct RsdpV2Tag {
-    typ: u32,
+    typ: TagType,
     size: u32,
     signature: [u8; 8],
     checksum: u8,

+ 2 - 1
src/vbe_info.rs

@@ -1,11 +1,12 @@
 use core::fmt;
+use crate::TagType;
 
 /// This tag contains VBE metadata, VBE controller information returned by the
 /// VBE Function 00h and VBE mode information returned by the VBE Function 01h.
 #[derive(Debug, Copy, Clone)]
 #[repr(C, packed)]
 pub struct VBEInfoTag {
-    typ: u32,
+    typ: TagType,
     length: u32,
 
     /// Indicates current video mode in the format specified in VBE 3.0.