-
Notifications
You must be signed in to change notification settings - Fork 56
Add a builder to multiboot2 #133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
152c985
multiboot2: Add basic information builder
YtvwlD 544e1ca
multiboot2: Implement setting the command line
YtvwlD 535c9e7
multiboot2: Implement adding module tags
YtvwlD 41428fb
multiboot2: Implement setting ELF section tag
YtvwlD 08df6c2
multiboot2: Implement setting boot loader name
YtvwlD bb21998
multiboot2: Implement setting the framebuffer tag
YtvwlD 0320148
multiboot2: Implement setting the BasicMemoryInfoTag
YtvwlD d198dce
multiboot2: Implement setting memory map tag
YtvwlD 122fc9c
multiboot2: Implement building the multiboot2 information
YtvwlD 6379b42
multiboot2: Implement Debug and PartialEq for packed structs
YtvwlD d5e99f7
multiboot2: Support passing the EFI System Table
YtvwlD 888dd27
multiboot2: Allow setting the SMBIOS tag
YtvwlD 79646bb
multiboot2: Allow setting the RSDP tags
YtvwlD 690c84a
multiboot2: Support setting the EFI memory map tag
YtvwlD 12f4642
multiboot2: Support setting Boot Services not exited tag
YtvwlD ed316cb
multiboot2: Support setting the image handle pointer
YtvwlD cbc47ab
multiboot2: Support setting the image load address
YtvwlD 2f2a058
multiboot2: Improve builder test
YtvwlD 09de523
multiboot2-header: Improve builder test
YtvwlD 69630b3
multiboot2: Don't require an offset for the ELF sections
YtvwlD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//! Exports item [`Multiboot2InformationBuilder`]. | ||
use crate::builder::traits::StructAsBytes; | ||
use crate::{CommandLineTag, ElfSectionsTag, ModuleTag}; | ||
|
||
use alloc::boxed::Box; | ||
use alloc::vec::Vec; | ||
|
||
/// Builder to construct a valid Multiboot2 information dynamically at runtime. | ||
/// The tags will appear in the order of their corresponding enumeration, | ||
/// except for the END tag. | ||
#[derive(Debug)] | ||
pub struct Multiboot2InformationBuilder { | ||
command_line_tag: Option<Box<CommandLineTag>>, | ||
elf_sections_tag: Option<Box<ElfSectionsTag>>, | ||
module_tags: Vec<Box<ModuleTag>>, | ||
} | ||
|
||
impl Multiboot2InformationBuilder { | ||
pub const fn new() -> Self { | ||
Self { | ||
command_line_tag: None, | ||
elf_sections_tag: None, | ||
module_tags: Vec::new(), | ||
} | ||
} | ||
|
||
pub fn command_line_tag(&mut self, command_line_tag: Box<CommandLineTag>) { | ||
self.command_line_tag = Some(command_line_tag); | ||
} | ||
|
||
pub fn elf_sections_tag(&mut self, elf_sections_tag: Box<ElfSectionsTag>) { | ||
self.elf_sections_tag = Some(elf_sections_tag); | ||
} | ||
|
||
pub fn add_module_tag(&mut self, module_tag: Box<ModuleTag>) { | ||
self.module_tags.push(module_tag); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//! Module for the builder-feature. | ||
|
||
mod information; | ||
pub(self) mod traits; | ||
|
||
pub use information::Multiboot2InformationBuilder; | ||
|
||
use alloc::alloc::alloc; | ||
use alloc::boxed::Box; | ||
use core::alloc::Layout; | ||
use core::mem::size_of; | ||
|
||
use crate::{TagTrait, TagTypeId}; | ||
|
||
/// Create a boxed tag with the given content. | ||
pub(super) fn boxed_dst_tag<T: TagTrait<Metadata = usize> + ?Sized>( | ||
typ: impl Into<TagTypeId>, | ||
content: &[u8], | ||
) -> Box<T> { | ||
// based on https://stackoverflow.com/a/64121094/2192464 | ||
let (layout, size_offset) = Layout::new::<TagTypeId>() | ||
.extend(Layout::new::<u32>()) | ||
.unwrap(); | ||
let (layout, inner_offset) = layout | ||
.extend(Layout::array::<usize>(content.len()).unwrap()) | ||
.unwrap(); | ||
let ptr = unsafe { alloc(layout) }; | ||
assert!(!ptr.is_null()); | ||
unsafe { | ||
// initialize the content as good as we can | ||
ptr.cast::<TagTypeId>().write(typ.into()); | ||
ptr.add(size_offset).cast::<u32>().write( | ||
(content.len() + size_of::<TagTypeId>() + size_of::<u32>()) | ||
.try_into() | ||
.unwrap(), | ||
); | ||
// initialize body | ||
let content_ptr = ptr.add(inner_offset); | ||
for (idx, val) in content.iter().enumerate() { | ||
content_ptr.add(idx).write(*val); | ||
} | ||
Box::from_raw(ptr_meta::from_raw_parts_mut(ptr as *mut (), content.len())) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//! Module for the helper trait [`StructAsBytes`]. | ||
|
||
use core::mem::size_of; | ||
|
||
/// Trait for all tags that helps to create a byte array from the tag. | ||
/// Useful in builders to construct a byte vector that | ||
/// represents the Multiboot2 information with all its tags. | ||
pub(crate) trait StructAsBytes: Sized { | ||
/// Returns the size in bytes of the struct, as known during compile | ||
/// time. This doesn't use read the "size" field of tags. | ||
fn byte_size(&self) -> usize { | ||
size_of::<Self>() | ||
} | ||
|
||
/// Returns a byte pointer to the begin of the struct. | ||
fn as_ptr(&self) -> *const u8 { | ||
self as *const Self as *const u8 | ||
} | ||
|
||
/// Returns the structure as a vector of its bytes. | ||
/// The length is determined by [`Self::byte_size`]. | ||
fn struct_as_bytes(&self) -> alloc::vec::Vec<u8> { | ||
let ptr = self.as_ptr(); | ||
let mut vec = alloc::vec::Vec::with_capacity(self.byte_size()); | ||
for i in 0..self.byte_size() { | ||
vec.push(unsafe { *ptr.add(i) }) | ||
} | ||
vec | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.