Skip to content

Commit 888dd27

Browse files
committed
multiboot2: Allow setting the SMBIOS tag
1 parent d5e99f7 commit 888dd27

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

multiboot2/src/builder/information.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use crate::builder::traits::StructAsBytes;
33
use crate::{
44
BasicMemoryInfoTag, BootInformationInner, BootLoaderNameTag, CommandLineTag, EFISdt32,
5-
EFISdt64, ElfSectionsTag, EndTag, FramebufferTag, MemoryMapTag, ModuleTag,
5+
EFISdt64, ElfSectionsTag, EndTag, FramebufferTag, MemoryMapTag, ModuleTag, SmbiosTag,
66
};
77

88
use alloc::boxed::Box;
@@ -23,6 +23,7 @@ pub struct Multiboot2InformationBuilder {
2323
module_tags: Vec<Box<ModuleTag>>,
2424
efisdt32: Option<EFISdt32>,
2525
efisdt64: Option<EFISdt64>,
26+
smbios_tags: Vec<Box<SmbiosTag>>,
2627
}
2728

2829
impl Multiboot2InformationBuilder {
@@ -37,6 +38,7 @@ impl Multiboot2InformationBuilder {
3738
framebuffer_tag: None,
3839
memory_map_tag: None,
3940
module_tags: Vec::new(),
41+
smbios_tags: Vec::new(),
4042
}
4143
}
4244

@@ -88,6 +90,9 @@ impl Multiboot2InformationBuilder {
8890
for tag in &self.module_tags {
8991
len += Self::size_or_up_aligned(tag.byte_size())
9092
}
93+
for tag in &self.smbios_tags {
94+
len += Self::size_or_up_aligned(tag.byte_size())
95+
}
9196
// only here size_or_up_aligned is not important, because it is the last tag
9297
len += size_of::<EndTag>();
9398
len
@@ -145,6 +150,9 @@ impl Multiboot2InformationBuilder {
145150
for tag in self.module_tags {
146151
Self::build_add_bytes(&mut data, &tag.struct_as_bytes(), false)
147152
}
153+
for tag in self.smbios_tags {
154+
Self::build_add_bytes(&mut data, &tag.struct_as_bytes(), false)
155+
}
148156

149157
Self::build_add_bytes(&mut data, &EndTag::default().struct_as_bytes(), true);
150158

@@ -186,6 +194,10 @@ impl Multiboot2InformationBuilder {
186194
pub fn add_module_tag(&mut self, module_tag: Box<ModuleTag>) {
187195
self.module_tags.push(module_tag);
188196
}
197+
198+
pub fn add_smbios_tag(&mut self, smbios_tag: Box<SmbiosTag>) {
199+
self.smbios_tags.push(smbios_tag);
200+
}
189201
}
190202

191203
#[cfg(test)]

multiboot2/src/smbios.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
use crate::{Tag, TagTrait, TagTypeId};
1+
use crate::{Tag, TagTrait, TagType, TagTypeId};
22

3+
use core::convert::TryInto;
34
use core::fmt::Debug;
45

6+
#[cfg(feature = "builder")]
7+
use {crate::builder::boxed_dst_tag, crate::builder::traits::StructAsBytes, alloc::boxed::Box};
8+
59
const METADATA_SIZE: usize = core::mem::size_of::<TagTypeId>()
610
+ core::mem::size_of::<u32>()
711
+ core::mem::size_of::<u8>() * 8;
@@ -18,13 +22,29 @@ pub struct SmbiosTag {
1822
pub tables: [u8],
1923
}
2024

25+
impl SmbiosTag {
26+
#[cfg(feature = "builder")]
27+
pub fn new(major: u8, minor: u8, tables: &[u8]) -> Box<Self> {
28+
let mut bytes = [major, minor, 0, 0, 0, 0, 0, 0].to_vec();
29+
bytes.extend(tables);
30+
boxed_dst_tag(TagType::Smbios, &bytes)
31+
}
32+
}
33+
2134
impl TagTrait for SmbiosTag {
2235
fn dst_size(base_tag: &Tag) -> usize {
2336
assert!(base_tag.size as usize >= METADATA_SIZE);
2437
base_tag.size as usize - METADATA_SIZE
2538
}
2639
}
2740

41+
#[cfg(feature = "builder")]
42+
impl StructAsBytes for SmbiosTag {
43+
fn byte_size(&self) -> usize {
44+
self.size.try_into().unwrap()
45+
}
46+
}
47+
2848
impl Debug for SmbiosTag {
2949
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
3050
f.debug_struct("BootLoaderNameTag")
@@ -66,4 +86,15 @@ mod tests {
6686
assert_eq!(tag.minor, 0);
6787
assert_eq!(tag.tables, [0xabu8; 24]);
6888
}
89+
90+
/// Test to generate a tag.
91+
#[test]
92+
#[cfg(feature = "builder")]
93+
fn test_build() {
94+
use crate::builder::traits::StructAsBytes;
95+
96+
let tag = SmbiosTag::new(3, 0, &[0xabu8; 24]);
97+
let bytes = tag.struct_as_bytes();
98+
assert_eq!(bytes, get_bytes());
99+
}
69100
}

0 commit comments

Comments
 (0)