Skip to content

Commit cf6c1c6

Browse files
authored
Merge pull request #102 from rust-osdev/mb2-hdr-no-std-fix
multiboot2-header: fix `no_std`-build + v0.1.1
2 parents 8adc3e0 + 72bf91d commit cf6c1c6

16 files changed

+79
-32
lines changed

multiboot2-header/Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description = """
44
Library with type definitions and parsing functions for Multiboot2 headers.
55
This library is `no_std` and can be used in bootloaders.
66
"""
7-
version = "0.1.0"
7+
version = "0.1.1"
88
authors = [
99
"Philipp Schuster <[email protected]>"
1010
]
@@ -27,6 +27,12 @@ homepage = "https://github.com/rust-osdev/multiboot2-header"
2727
repository = "https://github.com/rust-osdev/multiboot2"
2828
documentation = "https://docs.rs/multiboot2-header"
2929

30+
[features]
31+
# by default, builder is included
32+
default = ["builder"]
33+
std = []
34+
builder = ["std"]
35+
3036
[dependencies]
3137
# used for MBI tags
32-
multiboot2 = "0.12.2"
38+
multiboot2 = "0.13.2"

multiboot2-header/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ What this library is good for:
1515
What this library is not optimal for:
1616
- compiling a Multiboot2 header statically into an object file using only Rust code
1717

18+
## Features and Usage in `no_std`
19+
This library is always `no_std`. However, the `builder`-feature requires the `alloc`-crate
20+
to be available. You need the `builder` only if you want to construct new headers. For parsing,
21+
this is not relevant.
22+
23+
```toml
24+
# without `builder`-feature (and without `alloc`-crate)
25+
multiboot2-header = { version = "<latest>", default-features = false }
26+
# else (requires `alloc`-crate)
27+
multiboot2-header = "<latest>"
28+
```
29+
1830
## Example 1: Builder + Parse
1931
```rust
2032
use multiboot2_header::builder::Multiboot2HeaderBuilder;

multiboot2-header/src/address.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::mem::size_of;
33

44
/// This information does not need to be provided if the kernel image is in ELF
@@ -65,4 +65,5 @@ impl AddressHeaderTag {
6565
}
6666
}
6767

68-
impl StructAsBytes for AddressHeaderTag {}
68+
#[cfg(feature = "builder")]
69+
impl crate::StructAsBytes for AddressHeaderTag {}

multiboot2-header/src/console.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::mem::size_of;
33

44
/// Possible flags for [`ConsoleHeaderTag`].
@@ -46,7 +46,8 @@ impl ConsoleHeaderTag {
4646
}
4747
}
4848

49-
impl StructAsBytes for ConsoleHeaderTag {}
49+
#[cfg(feature = "builder")]
50+
impl crate::StructAsBytes for ConsoleHeaderTag {}
5051

5152
#[cfg(test)]
5253
mod tests {

multiboot2-header/src/end.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::mem::size_of;
33

44
/// Terminates a list of optional tags
@@ -33,4 +33,5 @@ impl EndHeaderTag {
3333
}
3434
}
3535

36-
impl StructAsBytes for EndHeaderTag {}
36+
#[cfg(feature = "builder")]
37+
impl crate::StructAsBytes for EndHeaderTag {}

multiboot2-header/src/entry_efi_32.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::fmt;
33
use core::fmt::{Debug, Formatter};
44
use core::mem::size_of;
@@ -50,4 +50,5 @@ impl Debug for EntryEfi32HeaderTag {
5050
}
5151
}
5252

53-
impl StructAsBytes for EntryEfi32HeaderTag {}
53+
#[cfg(feature = "builder")]
54+
impl crate::StructAsBytes for EntryEfi32HeaderTag {}

multiboot2-header/src/entry_efi_64.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::fmt;
33
use core::fmt::{Debug, Formatter};
44
use core::mem::size_of;
@@ -50,4 +50,5 @@ impl Debug for EntryEfi64HeaderTag {
5050
}
5151
}
5252

53-
impl StructAsBytes for EntryEfi64HeaderTag {}
53+
#[cfg(feature = "builder")]
54+
impl crate::StructAsBytes for EntryEfi64HeaderTag {}

multiboot2-header/src/entry_header.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::fmt;
33
use core::fmt::{Debug, Formatter};
44
use core::mem::size_of;
@@ -50,4 +50,5 @@ impl Debug for EntryHeaderTag {
5050
}
5151
}
5252

53-
impl StructAsBytes for EntryHeaderTag {}
53+
#[cfg(feature = "builder")]
54+
impl crate::StructAsBytes for EntryHeaderTag {}

multiboot2-header/src/framebuffer.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::mem::size_of;
33

44
/// Specifies the preferred graphics mode. If this tag
@@ -48,4 +48,5 @@ impl FramebufferHeaderTag {
4848
}
4949
}
5050

51-
impl StructAsBytes for FramebufferHeaderTag {}
51+
#[cfg(feature = "builder")]
52+
impl crate::StructAsBytes for FramebufferHeaderTag {}

multiboot2-header/src/header/builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{
66
EntryEfi64HeaderTag, EntryHeaderTag, FramebufferHeaderTag, InformationRequestHeaderTagBuilder,
77
ModuleAlignHeaderTag, Multiboot2BasicHeader, RelocatableHeaderTag, StructAsBytes,
88
};
9+
use alloc::vec::Vec;
910
use core::mem::size_of;
1011

1112
/// Builder to construct a valid Multiboot2 header dynamically at runtime.

multiboot2-header/src/header/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
pub mod builder;
55

66
pub use self::builder::*;
7-
use crate::{AddressHeaderTag, InformationRequestHeaderTag, RelocatableHeaderTag, StructAsBytes};
7+
use crate::{AddressHeaderTag, InformationRequestHeaderTag, RelocatableHeaderTag};
88
use crate::{ConsoleHeaderTag, EntryHeaderTag};
99
use crate::{EfiBootServiceHeaderTag, FramebufferHeaderTag};
1010
use crate::{EndHeaderTag, HeaderTagType};
@@ -194,7 +194,8 @@ impl Debug for Multiboot2BasicHeader {
194194
}
195195
}
196196

197-
impl StructAsBytes for Multiboot2BasicHeader {}
197+
#[cfg(feature = "builder")]
198+
impl crate::StructAsBytes for Multiboot2BasicHeader {}
198199

199200
/// Iterator over all tags of a Multiboot2 header. The number of items is derived
200201
/// by the size/length of the header.

multiboot2-header/src/information_request.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
use crate::HeaderTagType;
2+
#[cfg(feature = "builder")]
3+
use crate::StructAsBytes;
14
use crate::{HeaderTagFlag, MbiTagType};
2-
use crate::{HeaderTagType, StructAsBytes};
5+
#[cfg(feature = "builder")]
6+
use alloc::collections::BTreeSet;
7+
#[cfg(feature = "builder")]
8+
use alloc::vec::Vec;
39
use core::fmt;
410
use core::fmt::{Debug, Formatter};
511
use core::marker::PhantomData;
612
use core::mem::size_of;
7-
use std::collections::HashSet;
813

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

90-
impl<const N: usize> StructAsBytes for InformationRequestHeaderTag<N> {}
95+
#[cfg(feature = "builder")]
96+
impl<const N: usize> crate::StructAsBytes for InformationRequestHeaderTag<N> {}
9197

9298
/// Helper to build the dynamically sized [`InformationRequestHeaderTag`]
9399
/// at runtime.
94100
#[derive(Debug)]
101+
#[cfg(feature = "builder")]
95102
pub struct InformationRequestHeaderTagBuilder {
96103
flag: HeaderTagFlag,
97-
irs: HashSet<MbiTagType>,
104+
irs: BTreeSet<MbiTagType>,
98105
}
99106

107+
#[cfg(feature = "builder")]
100108
impl InformationRequestHeaderTagBuilder {
101109
/// New builder.
102110
pub fn new(flag: HeaderTagFlag) -> Self {
103111
Self {
104-
irs: HashSet::new(),
112+
irs: BTreeSet::new(),
105113
flag,
106114
}
107115
}

multiboot2-header/src/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,20 @@
3030
//!
3131
//! ```
3232
33+
#![no_std]
3334
#![deny(rustdoc::all)]
3435
#![allow(rustdoc::missing_doc_code_examples)]
3536
#![deny(clippy::all)]
3637
#![deny(clippy::missing_const_for_fn)]
3738
#![deny(missing_debug_implementations)]
3839

40+
#[cfg(feature = "builder")]
41+
extern crate alloc;
42+
43+
#[cfg_attr(test, macro_use)]
44+
#[cfg(test)]
45+
extern crate std;
46+
3947
#[cfg_attr(test, macro_use)]
4048
#[cfg(test)]
4149
pub(crate) mod test_utils;
@@ -68,14 +76,15 @@ pub use self::relocatable::*;
6876
pub use self::tags::*;
6977
pub use self::uefi_bs::*;
7078

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

7684
/// Trait for all tags that creates a byte array from the tag.
7785
/// Useful in builders to construct a byte vector that
7886
/// represents the Multiboot2 header with all its tags.
87+
#[cfg(feature = "builder")]
7988
pub(crate) trait StructAsBytes: Sized {
8089
/// Returns the size in bytes of the struct, as known during compile
8190
/// time. This doesn't use read the "size" field of tags.
@@ -90,9 +99,9 @@ pub(crate) trait StructAsBytes: Sized {
9099

91100
/// Returns the structure as a vector of its bytes.
92101
/// The length is determined by [`size`].
93-
fn struct_as_bytes(&self) -> Vec<u8> {
102+
fn struct_as_bytes(&self) -> alloc::vec::Vec<u8> {
94103
let ptr = self.as_ptr();
95-
let mut vec = Vec::with_capacity(self.byte_size());
104+
let mut vec = alloc::vec::Vec::with_capacity(self.byte_size());
96105
for i in 0..self.byte_size() {
97106
vec.push(unsafe { *ptr.add(i) })
98107
}

multiboot2-header/src/module_alignment.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::mem::size_of;
33

44
/// If this tag is present, provided boot modules must be page aligned.
@@ -30,4 +30,5 @@ impl ModuleAlignHeaderTag {
3030
}
3131
}
3232

33-
impl StructAsBytes for ModuleAlignHeaderTag {}
33+
#[cfg(feature = "builder")]
34+
impl crate::StructAsBytes for ModuleAlignHeaderTag {}

multiboot2-header/src/relocatable.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::fmt;
33
use core::fmt::{Debug, Formatter};
44
use core::mem::size_of;
@@ -91,4 +91,5 @@ impl Debug for RelocatableHeaderTag {
9191
}
9292
}
9393

94-
impl StructAsBytes for RelocatableHeaderTag {}
94+
#[cfg(feature = "builder")]
95+
impl crate::StructAsBytes for RelocatableHeaderTag {}

multiboot2-header/src/uefi_bs.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::mem::size_of;
33

44
/// This tag indicates that payload supports starting without terminating UEFI boot services.
@@ -31,4 +31,5 @@ impl EfiBootServiceHeaderTag {
3131
}
3232
}
3333

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

0 commit comments

Comments
 (0)