Skip to content

Commit 08df6c2

Browse files
committed
multiboot2: Implement setting boot loader name
1 parent 41428fb commit 08df6c2

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

multiboot2/src/boot_loader_name.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
use crate::TagTrait;
2-
use crate::{Tag, TagTypeId};
1+
use crate::{Tag, TagTrait, TagType, TagTypeId};
32
use core::fmt::{Debug, Formatter};
3+
use core::mem::size_of;
44
use core::str::Utf8Error;
55

6+
#[cfg(feature = "builder")]
7+
use {crate::builder::boxed_dst_tag, alloc::boxed::Box, alloc::vec::Vec};
8+
9+
const METADATA_SIZE: usize = size_of::<TagTypeId>() + size_of::<u32>();
10+
611
/// The bootloader name tag.
712
#[derive(ptr_meta::Pointee)]
813
#[repr(C, packed)] // only repr(C) would add unwanted padding before first_section
@@ -14,6 +19,13 @@ pub struct BootLoaderNameTag {
1419
}
1520

1621
impl BootLoaderNameTag {
22+
#[cfg(feature = "builder")]
23+
pub fn new(name: &str) -> Box<Self> {
24+
let mut bytes: Vec<_> = name.bytes().collect();
25+
bytes.push(0);
26+
boxed_dst_tag(TagType::BootLoaderName, &bytes)
27+
}
28+
1729
/// Reads the name of the bootloader that is booting the kernel as Rust
1830
/// string slice without the null-byte.
1931
///
@@ -46,10 +58,8 @@ impl Debug for BootLoaderNameTag {
4658

4759
impl TagTrait for BootLoaderNameTag {
4860
fn dst_size(base_tag: &Tag) -> usize {
49-
// The size of the sized portion of the bootloader name tag.
50-
let tag_base_size = 8;
51-
assert!(base_tag.size >= 8);
52-
base_tag.size as usize - tag_base_size
61+
assert!(base_tag.size as usize >= METADATA_SIZE);
62+
base_tag.size as usize - METADATA_SIZE
5363
}
5464
}
5565

multiboot2/src/builder/information.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Exports item [`Multiboot2InformationBuilder`].
22
use crate::builder::traits::StructAsBytes;
3-
use crate::{CommandLineTag, ElfSectionsTag, ModuleTag};
3+
use crate::{BootLoaderNameTag, CommandLineTag, ElfSectionsTag, ModuleTag};
44

55
use alloc::boxed::Box;
66
use alloc::vec::Vec;
@@ -10,6 +10,7 @@ use alloc::vec::Vec;
1010
/// except for the END tag.
1111
#[derive(Debug)]
1212
pub struct Multiboot2InformationBuilder {
13+
boot_loader_name_tag: Option<Box<BootLoaderNameTag>>,
1314
command_line_tag: Option<Box<CommandLineTag>>,
1415
elf_sections_tag: Option<Box<ElfSectionsTag>>,
1516
module_tags: Vec<Box<ModuleTag>>,
@@ -18,12 +19,17 @@ pub struct Multiboot2InformationBuilder {
1819
impl Multiboot2InformationBuilder {
1920
pub const fn new() -> Self {
2021
Self {
22+
boot_loader_name_tag: None,
2123
command_line_tag: None,
2224
elf_sections_tag: None,
2325
module_tags: Vec::new(),
2426
}
2527
}
2628

29+
pub fn bootloader_name_tag(&mut self, boot_loader_name_tag: Box<BootLoaderNameTag>) {
30+
self.boot_loader_name_tag = Some(boot_loader_name_tag);
31+
}
32+
2733
pub fn command_line_tag(&mut self, command_line_tag: Box<CommandLineTag>) {
2834
self.command_line_tag = Some(command_line_tag);
2935
}

0 commit comments

Comments
 (0)