Skip to content

multiboot2-header: fix no_std-build + v0.1.1 #102

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 2 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions multiboot2-header/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = """
Library with type definitions and parsing functions for Multiboot2 headers.
This library is `no_std` and can be used in bootloaders.
"""
version = "0.1.0"
version = "0.1.1"
authors = [
"Philipp Schuster <[email protected]>"
]
Expand All @@ -27,6 +27,12 @@ homepage = "https://github.com/rust-osdev/multiboot2-header"
repository = "https://github.com/rust-osdev/multiboot2"
documentation = "https://docs.rs/multiboot2-header"

[features]
# by default, builder is included
default = ["builder"]
std = []
builder = ["std"]

[dependencies]
# used for MBI tags
multiboot2 = "0.12.2"
multiboot2 = "0.13.2"
12 changes: 12 additions & 0 deletions multiboot2-header/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ What this library is good for:
What this library is not optimal for:
- compiling a Multiboot2 header statically into an object file using only Rust code

## Features and Usage in `no_std`
This library is always `no_std`. However, the `builder`-feature requires the `alloc`-crate
to be available. You need the `builder` only if you want to construct new headers. For parsing,
this is not relevant.

```toml
# without `builder`-feature (and without `alloc`-crate)
multiboot2-header = { version = "<latest>", default-features = false }
# else (requires `alloc`-crate)
multiboot2-header = "<latest>"
```

## Example 1: Builder + Parse
```rust
use multiboot2_header::builder::Multiboot2HeaderBuilder;
Expand Down
5 changes: 3 additions & 2 deletions multiboot2-header/src/address.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
use crate::{HeaderTagFlag, HeaderTagType};
use core::mem::size_of;

/// This information does not need to be provided if the kernel image is in ELF
Expand Down Expand Up @@ -65,4 +65,5 @@ impl AddressHeaderTag {
}
}

impl StructAsBytes for AddressHeaderTag {}
#[cfg(feature = "builder")]
impl crate::StructAsBytes for AddressHeaderTag {}
5 changes: 3 additions & 2 deletions multiboot2-header/src/console.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
use crate::{HeaderTagFlag, HeaderTagType};
use core::mem::size_of;

/// Possible flags for [`ConsoleHeaderTag`].
Expand Down Expand Up @@ -46,7 +46,8 @@ impl ConsoleHeaderTag {
}
}

impl StructAsBytes for ConsoleHeaderTag {}
#[cfg(feature = "builder")]
impl crate::StructAsBytes for ConsoleHeaderTag {}

#[cfg(test)]
mod tests {
Expand Down
5 changes: 3 additions & 2 deletions multiboot2-header/src/end.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
use crate::{HeaderTagFlag, HeaderTagType};
use core::mem::size_of;

/// Terminates a list of optional tags
Expand Down Expand Up @@ -33,4 +33,5 @@ impl EndHeaderTag {
}
}

impl StructAsBytes for EndHeaderTag {}
#[cfg(feature = "builder")]
impl crate::StructAsBytes for EndHeaderTag {}
5 changes: 3 additions & 2 deletions multiboot2-header/src/entry_efi_32.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
use crate::{HeaderTagFlag, HeaderTagType};
use core::fmt;
use core::fmt::{Debug, Formatter};
use core::mem::size_of;
Expand Down Expand Up @@ -50,4 +50,5 @@ impl Debug for EntryEfi32HeaderTag {
}
}

impl StructAsBytes for EntryEfi32HeaderTag {}
#[cfg(feature = "builder")]
impl crate::StructAsBytes for EntryEfi32HeaderTag {}
5 changes: 3 additions & 2 deletions multiboot2-header/src/entry_efi_64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
use crate::{HeaderTagFlag, HeaderTagType};
use core::fmt;
use core::fmt::{Debug, Formatter};
use core::mem::size_of;
Expand Down Expand Up @@ -50,4 +50,5 @@ impl Debug for EntryEfi64HeaderTag {
}
}

impl StructAsBytes for EntryEfi64HeaderTag {}
#[cfg(feature = "builder")]
impl crate::StructAsBytes for EntryEfi64HeaderTag {}
5 changes: 3 additions & 2 deletions multiboot2-header/src/entry_header.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
use crate::{HeaderTagFlag, HeaderTagType};
use core::fmt;
use core::fmt::{Debug, Formatter};
use core::mem::size_of;
Expand Down Expand Up @@ -50,4 +50,5 @@ impl Debug for EntryHeaderTag {
}
}

impl StructAsBytes for EntryHeaderTag {}
#[cfg(feature = "builder")]
impl crate::StructAsBytes for EntryHeaderTag {}
5 changes: 3 additions & 2 deletions multiboot2-header/src/framebuffer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
use crate::{HeaderTagFlag, HeaderTagType};
use core::mem::size_of;

/// Specifies the preferred graphics mode. If this tag
Expand Down Expand Up @@ -48,4 +48,5 @@ impl FramebufferHeaderTag {
}
}

impl StructAsBytes for FramebufferHeaderTag {}
#[cfg(feature = "builder")]
impl crate::StructAsBytes for FramebufferHeaderTag {}
1 change: 1 addition & 0 deletions multiboot2-header/src/header/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
EntryEfi64HeaderTag, EntryHeaderTag, FramebufferHeaderTag, InformationRequestHeaderTagBuilder,
ModuleAlignHeaderTag, Multiboot2BasicHeader, RelocatableHeaderTag, StructAsBytes,
};
use alloc::vec::Vec;
use core::mem::size_of;

/// Builder to construct a valid Multiboot2 header dynamically at runtime.
Expand Down
5 changes: 3 additions & 2 deletions multiboot2-header/src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pub mod builder;

pub use self::builder::*;
use crate::{AddressHeaderTag, InformationRequestHeaderTag, RelocatableHeaderTag, StructAsBytes};
use crate::{AddressHeaderTag, InformationRequestHeaderTag, RelocatableHeaderTag};
use crate::{ConsoleHeaderTag, EntryHeaderTag};
use crate::{EfiBootServiceHeaderTag, FramebufferHeaderTag};
use crate::{EndHeaderTag, HeaderTagType};
Expand Down Expand Up @@ -194,7 +194,8 @@ impl Debug for Multiboot2BasicHeader {
}
}

impl StructAsBytes for Multiboot2BasicHeader {}
#[cfg(feature = "builder")]
impl crate::StructAsBytes for Multiboot2BasicHeader {}

/// Iterator over all tags of a Multiboot2 header. The number of items is derived
/// by the size/length of the header.
Expand Down
18 changes: 13 additions & 5 deletions multiboot2-header/src/information_request.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use crate::HeaderTagType;
#[cfg(feature = "builder")]
use crate::StructAsBytes;
use crate::{HeaderTagFlag, MbiTagType};
use crate::{HeaderTagType, StructAsBytes};
#[cfg(feature = "builder")]
use alloc::collections::BTreeSet;
#[cfg(feature = "builder")]
use alloc::vec::Vec;
use core::fmt;
use core::fmt::{Debug, Formatter};
use core::marker::PhantomData;
use core::mem::size_of;
use std::collections::HashSet;

/// Specifies what specific tag types the bootloader should provide
/// inside the mbi.
Expand Down Expand Up @@ -87,21 +92,24 @@ impl<const N: usize> Debug for InformationRequestHeaderTag<N> {
}
}

impl<const N: usize> StructAsBytes for InformationRequestHeaderTag<N> {}
#[cfg(feature = "builder")]
impl<const N: usize> crate::StructAsBytes for InformationRequestHeaderTag<N> {}

/// Helper to build the dynamically sized [`InformationRequestHeaderTag`]
/// at runtime.
#[derive(Debug)]
#[cfg(feature = "builder")]
pub struct InformationRequestHeaderTagBuilder {
flag: HeaderTagFlag,
irs: HashSet<MbiTagType>,
irs: BTreeSet<MbiTagType>,
}

#[cfg(feature = "builder")]
impl InformationRequestHeaderTagBuilder {
/// New builder.
pub fn new(flag: HeaderTagFlag) -> Self {
Self {
irs: HashSet::new(),
irs: BTreeSet::new(),
flag,
}
}
Expand Down
15 changes: 12 additions & 3 deletions multiboot2-header/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@
//!
//! ```

#![no_std]
#![deny(rustdoc::all)]
#![allow(rustdoc::missing_doc_code_examples)]
#![deny(clippy::all)]
#![deny(clippy::missing_const_for_fn)]
#![deny(missing_debug_implementations)]

#[cfg(feature = "builder")]
extern crate alloc;

#[cfg_attr(test, macro_use)]
#[cfg(test)]
extern crate std;

#[cfg_attr(test, macro_use)]
#[cfg(test)]
pub(crate) mod test_utils;
Expand Down Expand Up @@ -68,14 +76,15 @@ pub use self::relocatable::*;
pub use self::tags::*;
pub use self::uefi_bs::*;

use core::mem::size_of;
/// Re-export of [`multiboot2::TagType`] from `multiboot2`-crate as `MbiTagType`, i.e. tags that
/// describe the entries in the Multiboot2 Information Structure (MBI).
pub use multiboot2::TagType as MbiTagType;
use std::mem::size_of;

/// Trait for all tags that creates a byte array from the tag.
/// Useful in builders to construct a byte vector that
/// represents the Multiboot2 header with all its tags.
#[cfg(feature = "builder")]
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.
Expand All @@ -90,9 +99,9 @@ pub(crate) trait StructAsBytes: Sized {

/// Returns the structure as a vector of its bytes.
/// The length is determined by [`size`].
fn struct_as_bytes(&self) -> Vec<u8> {
fn struct_as_bytes(&self) -> alloc::vec::Vec<u8> {
let ptr = self.as_ptr();
let mut vec = Vec::with_capacity(self.byte_size());
let mut vec = alloc::vec::Vec::with_capacity(self.byte_size());
for i in 0..self.byte_size() {
vec.push(unsafe { *ptr.add(i) })
}
Expand Down
5 changes: 3 additions & 2 deletions multiboot2-header/src/module_alignment.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
use crate::{HeaderTagFlag, HeaderTagType};
use core::mem::size_of;

/// If this tag is present, provided boot modules must be page aligned.
Expand Down Expand Up @@ -30,4 +30,5 @@ impl ModuleAlignHeaderTag {
}
}

impl StructAsBytes for ModuleAlignHeaderTag {}
#[cfg(feature = "builder")]
impl crate::StructAsBytes for ModuleAlignHeaderTag {}
5 changes: 3 additions & 2 deletions multiboot2-header/src/relocatable.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
use crate::{HeaderTagFlag, HeaderTagType};
use core::fmt;
use core::fmt::{Debug, Formatter};
use core::mem::size_of;
Expand Down Expand Up @@ -91,4 +91,5 @@ impl Debug for RelocatableHeaderTag {
}
}

impl StructAsBytes for RelocatableHeaderTag {}
#[cfg(feature = "builder")]
impl crate::StructAsBytes for RelocatableHeaderTag {}
5 changes: 3 additions & 2 deletions multiboot2-header/src/uefi_bs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
use crate::{HeaderTagFlag, HeaderTagType};
use core::mem::size_of;

/// This tag indicates that payload supports starting without terminating UEFI boot services.
Expand Down Expand Up @@ -31,4 +31,5 @@ impl EfiBootServiceHeaderTag {
}
}

impl StructAsBytes for EfiBootServiceHeaderTag {}
#[cfg(feature = "builder")]
impl crate::StructAsBytes for EfiBootServiceHeaderTag {}