Skip to content
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

multiboot2-header crate - initial release #95

Merged
merged 3 commits into from Oct 8, 2021
Merged

Conversation

phip1611
Copy link
Collaborator

@phip1611 phip1611 commented Oct 2, 2021

After I prepared everything, the multiboot2-header crate is ready for the initial release. I've put a lot of work into it and I am confident that the code base is solid. There are some tests and I also used external tools to compare the results, for example how bootinfo parses the mb2 header.

Any additional comments @IsaacWoods ?

Small example/demo:

use multiboot2_header::builder::Multiboot2HeaderBuilder;
use multiboot2_header::{
    Multiboot2Header, HeaderTagFlag, HeaderTagISA, InformationRequestHeaderTagBuilder, MbiTagType,
    RelocatableHeaderTag, RelocatableHeaderTagPreference,
};

/// Small example that creates a Multiboot2 header and parses it afterwards.
fn main() {
    // We create a Multiboot2 header during runtime here. A practical example is that your
    // program gets the header from a file and parses it afterwards.
    let mb2_hdr_bytes = Multiboot2HeaderBuilder::new(HeaderTagISA::I386)
        .relocatable_tag(RelocatableHeaderTag::new(
            HeaderTagFlag::Required,
            0x1337,
            0xdeadbeef,
            4096,
            RelocatableHeaderTagPreference::None,
        ))
        .information_request_tag(
            InformationRequestHeaderTagBuilder::new(HeaderTagFlag::Required)
                .add_irs(&[MbiTagType::Cmdline, MbiTagType::BootLoaderName]),
        )
        .build();

    // Cast bytes in vector to Multiboot2 information structure
    let mb2_hdr = unsafe { Multiboot2Header::from_addr(mb2_hdr_bytes.as_ptr() as usize) };
    println!("{:#?}", mb2_hdr);
}

produces

Multiboot2Header {
    header_magic: 3897708758,
    arch: I386,
    length: 64,
    checksum: 397258474,
    tags: [
        InformationRequestHeaderTag {
            type: InformationRequest,
            flags: Required,
            size: 16,
            requests: [
                Cmdline,
                BootLoaderName,
            ],
        },
        RelocatableHeaderTag {
            type: Relocatable,
            flags: Required,
            size: 24,
            min_addr: 0x0000000000001337,
            max_addr: 0x00000000deadbeef,
            align: 4096,
            preference: None,
        },
        EndHeaderTag {
            typ: End,
            flags: Required,
            size: 8,
        },
    ],
}

The generated header can also be validated by crates such as bootinfo.

Copy link
Member

@IsaacWoods IsaacWoods left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid I won't have time to do a review of this crate at the moment, as I'm very busy what with uni starting up again in the UK atm. I've had a brief flick through at the examples and code, however, and this seems very reasonable. Feel free to merge when you're happy with it!

@phip1611 phip1611 merged commit 50ee5db into main Oct 8, 2021
@phip1611 phip1611 deleted the multiboot2-header-crate branch October 8, 2021 07:14
@@ -389,7 +389,7 @@ impl Reader {

pub(crate) fn read_u8(&mut self) -> u8 {
self.off += 1;
unsafe { core::ptr::read(self.ptr.add(self.off - 1)) }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, this was committed by accident. But this shouldn't change everything, because dereferencing a u8 or reading it, result both in a copy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants