Skip to content

Commit 152c985

Browse files
committed
multiboot2: Add basic information builder
1 parent 455f7bc commit 152c985

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

multiboot2/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ repository = "https://github.com/rust-osdev/multiboot2"
3232
documentation = "https://docs.rs/multiboot2"
3333

3434
[features]
35-
default = []
35+
# by default, builder is included
36+
default = ["builder"]
37+
std = []
38+
builder = ["std"]
3639
# Nightly-only features that will eventually be stabilized.
3740
unstable = []
3841

multiboot2/src/builder/information.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! Exports item [`Multiboot2InformationBuilder`].
2+
use crate::{builder::traits::StructAsBytes, CommandLineTag};
3+
4+
use alloc::boxed::Box;
5+
6+
/// Builder to construct a valid Multiboot2 information dynamically at runtime.
7+
/// The tags will appear in the order of their corresponding enumeration,
8+
/// except for the END tag.
9+
#[derive(Debug)]
10+
pub struct Multiboot2InformationBuilder {}
11+
12+
impl Multiboot2InformationBuilder {
13+
pub const fn new() -> Self {
14+
Self {}
15+
}
16+
}

multiboot2/src/builder/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//! Module for the builder-feature.
2+
3+
mod information;
4+
pub(self) mod traits;
5+
6+
pub use information::Multiboot2InformationBuilder;

multiboot2/src/builder/traits.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//! Module for the helper trait [`StructAsBytes`].
2+
3+
use core::mem::size_of;
4+
5+
/// Trait for all tags that helps to create a byte array from the tag.
6+
/// Useful in builders to construct a byte vector that
7+
/// represents the Multiboot2 information with all its tags.
8+
pub(crate) trait StructAsBytes: Sized {
9+
/// Returns the size in bytes of the struct, as known during compile
10+
/// time. This doesn't use read the "size" field of tags.
11+
fn byte_size(&self) -> usize {
12+
size_of::<Self>()
13+
}
14+
15+
/// Returns a byte pointer to the begin of the struct.
16+
fn as_ptr(&self) -> *const u8 {
17+
self as *const Self as *const u8
18+
}
19+
20+
/// Returns the structure as a vector of its bytes.
21+
/// The length is determined by [`Self::byte_size`].
22+
fn struct_as_bytes(&self) -> alloc::vec::Vec<u8> {
23+
let ptr = self.as_ptr();
24+
let mut vec = alloc::vec::Vec::with_capacity(self.byte_size());
25+
for i in 0..self.byte_size() {
26+
vec.push(unsafe { *ptr.add(i) })
27+
}
28+
vec
29+
}
30+
}

multiboot2/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
//! ## MSRV
3333
//! The MSRV is 1.56.1 stable.
3434
35+
#[cfg(feature = "builder")]
36+
extern crate alloc;
37+
3538
// this crate can use std in tests only
3639
#[cfg_attr(test, macro_use)]
3740
#[cfg(test)]
@@ -81,6 +84,9 @@ mod smbios;
8184
mod tag_type;
8285
mod vbe_info;
8386

87+
#[cfg(feature = "builder")]
88+
pub mod builder;
89+
8490
/// Magic number that a multiboot2-compliant boot loader will store in `eax` register
8591
/// right before handoff to the payload (the kernel). This value can be used to check,
8692
/// that the kernel was indeed booted via multiboot2.

0 commit comments

Comments
 (0)