瀏覽代碼

Add bootloader name tag (#5)

Thomas Winwood 8 年之前
父節點
當前提交
44b4b3f518
共有 2 個文件被更改,包括 20 次插入0 次删除
  1. 14 0
      src/boot_loader_name.rs
  2. 6 0
      src/lib.rs

+ 14 - 0
src/boot_loader_name.rs

@@ -0,0 +1,14 @@
+
+#[derive(Debug)]
+#[repr(packed)] // repr(C) would add unwanted padding before first_section
+pub struct BootLoaderNameTag {
+    typ: u32,
+    size: u32,
+    string: u8,
+}
+
+impl BootLoaderNameTag {
+    pub fn name(&self) -> &str {
+        unsafe { ::core::str::from_utf8_unchecked(::core::slice::from_raw_parts((&self.string) as *const u8, self.size as usize - 8)) }
+    }
+}

+ 6 - 0
src/lib.rs

@@ -1,5 +1,6 @@
 #![no_std]
 
+pub use boot_loader_name::BootLoaderNameTag;
 pub use elf_sections::{ElfSectionsTag, ElfSection, ElfSectionIter, ElfSectionType, ElfSectionFlags};
 pub use elf_sections::{ELF_SECTION_WRITABLE, ELF_SECTION_ALLOCATED, ELF_SECTION_EXECUTABLE};
 pub use memory_map::{MemoryMapTag, MemoryArea, MemoryAreaIter};
@@ -7,6 +8,7 @@ pub use memory_map::{MemoryMapTag, MemoryArea, MemoryAreaIter};
 #[macro_use]
 extern crate bitflags;
 
+mod boot_loader_name;
 mod elf_sections;
 mod memory_map;
 
@@ -40,6 +42,10 @@ impl BootInformation {
         self.get_tag(6).map(|tag| unsafe{&*(tag as *const Tag as *const MemoryMapTag)})
     }
 
+    pub fn boot_loader_name_tag(&self) -> Option<&'static BootLoaderNameTag> {
+        self.get_tag(2).map(|tag| unsafe{&*(tag as *const Tag as *const BootLoaderNameTag)})
+    }
+
     fn has_valid_end_tag(&self) -> bool {
         const END_TAG: Tag = Tag{typ:0, size:8};