Browse Source

multiboot2: Implement setting boot loader name

Niklas Sombert 2 years ago
parent
commit
08df6c2a47
2 changed files with 23 additions and 7 deletions
  1. 16 6
      multiboot2/src/boot_loader_name.rs
  2. 7 1
      multiboot2/src/builder/information.rs

+ 16 - 6
multiboot2/src/boot_loader_name.rs

@@ -1,8 +1,13 @@
-use crate::TagTrait;
-use crate::{Tag, TagTypeId};
+use crate::{Tag, TagTrait, TagType, TagTypeId};
 use core::fmt::{Debug, Formatter};
+use core::mem::size_of;
 use core::str::Utf8Error;
 
+#[cfg(feature = "builder")]
+use {crate::builder::boxed_dst_tag, alloc::boxed::Box, alloc::vec::Vec};
+
+const METADATA_SIZE: usize = size_of::<TagTypeId>() + size_of::<u32>();
+
 /// The bootloader name tag.
 #[derive(ptr_meta::Pointee)]
 #[repr(C, packed)] // only repr(C) would add unwanted padding before first_section
@@ -14,6 +19,13 @@ pub struct BootLoaderNameTag {
 }
 
 impl BootLoaderNameTag {
+    #[cfg(feature = "builder")]
+    pub fn new(name: &str) -> Box<Self> {
+        let mut bytes: Vec<_> = name.bytes().collect();
+        bytes.push(0);
+        boxed_dst_tag(TagType::BootLoaderName, &bytes)
+    }
+
     /// Reads the name of the bootloader that is booting the kernel as Rust
     /// string slice without the null-byte.
     ///
@@ -46,10 +58,8 @@ impl Debug for BootLoaderNameTag {
 
 impl TagTrait for BootLoaderNameTag {
     fn dst_size(base_tag: &Tag) -> usize {
-        // The size of the sized portion of the bootloader name tag.
-        let tag_base_size = 8;
-        assert!(base_tag.size >= 8);
-        base_tag.size as usize - tag_base_size
+        assert!(base_tag.size as usize >= METADATA_SIZE);
+        base_tag.size as usize - METADATA_SIZE
     }
 }
 

+ 7 - 1
multiboot2/src/builder/information.rs

@@ -1,6 +1,6 @@
 //! Exports item [`Multiboot2InformationBuilder`].
 use crate::builder::traits::StructAsBytes;
-use crate::{CommandLineTag, ElfSectionsTag, ModuleTag};
+use crate::{BootLoaderNameTag, CommandLineTag, ElfSectionsTag, ModuleTag};
 
 use alloc::boxed::Box;
 use alloc::vec::Vec;
@@ -10,6 +10,7 @@ use alloc::vec::Vec;
 /// except for the END tag.
 #[derive(Debug)]
 pub struct Multiboot2InformationBuilder {
+    boot_loader_name_tag: Option<Box<BootLoaderNameTag>>,
     command_line_tag: Option<Box<CommandLineTag>>,
     elf_sections_tag: Option<Box<ElfSectionsTag>>,
     module_tags: Vec<Box<ModuleTag>>,
@@ -18,12 +19,17 @@ pub struct Multiboot2InformationBuilder {
 impl Multiboot2InformationBuilder {
     pub const fn new() -> Self {
         Self {
+            boot_loader_name_tag: None,
             command_line_tag: None,
             elf_sections_tag: None,
             module_tags: Vec::new(),
         }
     }
 
+    pub fn bootloader_name_tag(&mut self, boot_loader_name_tag: Box<BootLoaderNameTag>) {
+        self.boot_loader_name_tag = Some(boot_loader_name_tag);
+    }
+
     pub fn command_line_tag(&mut self, command_line_tag: Box<CommandLineTag>) {
         self.command_line_tag = Some(command_line_tag);
     }