Skip to content

multiboot2-header crate - initial release - #95

Merged
phip1611 merged 3 commits into
mainfrom
multiboot2-header-crate
Oct 8, 2021
Merged

multiboot2-header crate - initial release#95
phip1611 merged 3 commits into
mainfrom
multiboot2-header-crate

Conversation

@phip1611

@phip1611 phip1611 commented Oct 2, 2021

Copy link
Copy Markdown
Member

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.

@phip1611
phip1611 requested a review from IsaacWoods October 2, 2021 13:37
@phip1611 phip1611 self-assigned this Oct 2, 2021
@phip1611
phip1611 force-pushed the multiboot2-header-crate branch from f0ad633 to f89b1de Compare October 2, 2021 13:39

@IsaacWoods IsaacWoods left a comment

Copy link
Copy Markdown
Member

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 force-pushed the multiboot2-header-crate branch from 54c09cb to 78de7b3 Compare October 8, 2021 07:12
@phip1611
phip1611 merged commit 50ee5db into main Oct 8, 2021
@phip1611
phip1611 deleted the multiboot2-header-crate branch October 8, 2021 07:14
Comment thread multiboot2/src/lib.rs

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

Copy link
Copy Markdown
Member 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.

2 participants