Skip to content

Commit 648184d

Browse files
committed
multiboot2-header: code improvements
1 parent 8ea06c7 commit 648184d

File tree

18 files changed

+238
-216
lines changed

18 files changed

+238
-216
lines changed

multiboot2-header/README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ 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-
## Example
18+
## Example 1: Builder + Parse
1919
```rust
2020
use multiboot2_header::builder::Multiboot2HeaderBuilder;
21-
use multiboot2_header::{ConsoleHeaderTag, HeaderTagFlag, HeaderTagISA, InformationRequestHeaderTagBuilder, MbiTagType, Multiboot2Header, RelocatableHeaderTag, RelocatableHeaderTagPreference, load_mb2_header};
21+
use multiboot2_header::{HeaderTagFlag, HeaderTagISA, InformationRequestHeaderTagBuilder, MbiTagType, RelocatableHeaderTag, RelocatableHeaderTagPreference, Multiboot2Header};
2222

2323
/// Small example that creates a Multiboot2 header and parses it afterwards.
2424
fn main() {
@@ -39,11 +39,21 @@ fn main() {
3939
.build();
4040

4141
// Cast bytes in vector to Multiboot2 information structure
42-
let mb2_hdr = unsafe { load_mb2_header(mb2_hdr_bytes.as_ptr() as usize) };
42+
let mb2_hdr = unsafe { Multiboot2Header::from_addr(mb2_hdr_bytes.as_ptr() as usize) };
4343
println!("{:#?}", mb2_hdr);
4444
}
45+
```
4546

47+
## Example 2: Multiboot2 header as static data in Rust file
48+
You can use the builder, construct a Multiboot2 header, write it to a file and include it like this:
49+
```
50+
#[used]
51+
#[no_mangle]
52+
#[link_section = ".text.multiboot2_header"]
53+
static MULTIBOOT2_HDR: &[u8; 64] = include_bytes!("mb2_hdr_dump.bin");
4654
```
55+
You may need a special linker script to place this in a LOAD segment with a file offset with less than 32768 bytes.
56+
See specification.
4757

4858
## License & Contribution
4959

multiboot2-header/examples/minimal.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use multiboot2_header::builder::Multiboot2HeaderBuilder;
2-
use multiboot2_header::{ConsoleHeaderTag, HeaderTagFlag, HeaderTagISA, InformationRequestHeaderTagBuilder, MbiTagType, Multiboot2Header, RelocatableHeaderTag, RelocatableHeaderTagPreference, load_mb2_header};
2+
use multiboot2_header::{
3+
HeaderTagFlag, HeaderTagISA, InformationRequestHeaderTagBuilder, MbiTagType, Multiboot2Header,
4+
RelocatableHeaderTag, RelocatableHeaderTagPreference,
5+
};
36

47
/// Small example that creates a Multiboot2 header and parses it afterwards.
58
fn main() {
@@ -20,6 +23,6 @@ fn main() {
2023
.build();
2124

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

multiboot2-header/src/address.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,25 @@ impl AddressHeaderTag {
4242
}
4343
}
4444

45-
pub fn typ(&self) -> HeaderTagType {
45+
pub const fn typ(&self) -> HeaderTagType {
4646
self.typ
4747
}
48-
pub fn flags(&self) -> HeaderTagFlag {
48+
pub const fn flags(&self) -> HeaderTagFlag {
4949
self.flags
5050
}
51-
pub fn size(&self) -> u32 {
51+
pub const fn size(&self) -> u32 {
5252
self.size
5353
}
54-
pub fn header_addr(&self) -> u32 {
54+
pub const fn header_addr(&self) -> u32 {
5555
self.header_addr
5656
}
57-
pub fn load_addr(&self) -> u32 {
57+
pub const fn load_addr(&self) -> u32 {
5858
self.load_addr
5959
}
60-
pub fn load_end_addr(&self) -> u32 {
60+
pub const fn load_end_addr(&self) -> u32 {
6161
self.load_end_addr
6262
}
63-
pub fn bss_end_addr(&self) -> u32 {
63+
pub const fn bss_end_addr(&self) -> u32 {
6464
self.bss_end_addr
6565
}
6666
}

multiboot2-header/src/console.rs

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

4+
/// Possible flags for [`ConsoleHeaderTag`].
45
#[repr(u32)]
56
#[derive(Copy, Clone, Debug)]
67
pub enum ConsoleHeaderTagFlags {
@@ -31,16 +32,16 @@ impl ConsoleHeaderTag {
3132
}
3233
}
3334

34-
pub fn typ(&self) -> HeaderTagType {
35+
pub const fn typ(&self) -> HeaderTagType {
3536
self.typ
3637
}
37-
pub fn flags(&self) -> HeaderTagFlag {
38+
pub const fn flags(&self) -> HeaderTagFlag {
3839
self.flags
3940
}
40-
pub fn size(&self) -> u32 {
41+
pub const fn size(&self) -> u32 {
4142
self.size
4243
}
43-
pub fn console_flags(&self) -> ConsoleHeaderTagFlags {
44+
pub const fn console_flags(&self) -> ConsoleHeaderTagFlags {
4445
self.console_flags
4546
}
4647
}

multiboot2-header/src/end.rs

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

44
/// Terminates a list of optional tags
5-
/// in a multiboot2 header.
5+
/// in a Multiboot2 header.
66
#[derive(Copy, Clone, Debug)]
77
#[repr(C, packed(8))]
88
pub struct EndHeaderTag {
@@ -22,13 +22,13 @@ impl EndHeaderTag {
2222
}
2323
}
2424

25-
pub fn typ(&self) -> HeaderTagType {
25+
pub const fn typ(&self) -> HeaderTagType {
2626
self.typ
2727
}
28-
pub fn flags(&self) -> HeaderTagFlag {
28+
pub const fn flags(&self) -> HeaderTagFlag {
2929
self.flags
3030
}
31-
pub fn size(&self) -> u32 {
31+
pub const fn size(&self) -> u32 {
3232
self.size
3333
}
3434
}

multiboot2-header/src/entry_efi_32.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ impl EntryEfi32HeaderTag {
2525
}
2626
}
2727

28-
pub fn typ(&self) -> HeaderTagType {
28+
pub const fn typ(&self) -> HeaderTagType {
2929
self.typ
3030
}
31-
pub fn flags(&self) -> HeaderTagFlag {
31+
pub const fn flags(&self) -> HeaderTagFlag {
3232
self.flags
3333
}
34-
pub fn size(&self) -> u32 {
34+
pub const fn size(&self) -> u32 {
3535
self.size
3636
}
37-
pub fn entry_addr(&self) -> u32 {
37+
pub const fn entry_addr(&self) -> u32 {
3838
self.entry_addr
3939
}
4040
}

multiboot2-header/src/entry_efi_64.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ impl EntryEfi64HeaderTag {
2525
}
2626
}
2727

28-
pub fn typ(&self) -> HeaderTagType {
28+
pub const fn typ(&self) -> HeaderTagType {
2929
self.typ
3030
}
31-
pub fn flags(&self) -> HeaderTagFlag {
31+
pub const fn flags(&self) -> HeaderTagFlag {
3232
self.flags
3333
}
34-
pub fn size(&self) -> u32 {
34+
pub const fn size(&self) -> u32 {
3535
self.size
3636
}
37-
pub fn entry_addr(&self) -> u32 {
37+
pub const fn entry_addr(&self) -> u32 {
3838
self.entry_addr
3939
}
4040
}

multiboot2-header/src/entry_header.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ impl EntryHeaderTag {
2525
}
2626
}
2727

28-
pub fn typ(&self) -> HeaderTagType {
28+
pub const fn typ(&self) -> HeaderTagType {
2929
self.typ
3030
}
31-
pub fn flags(&self) -> HeaderTagFlag {
31+
pub const fn flags(&self) -> HeaderTagFlag {
3232
self.flags
3333
}
34-
pub fn size(&self) -> u32 {
34+
pub const fn size(&self) -> u32 {
3535
self.size
3636
}
37-
pub fn entry_addr(&self) -> u32 {
37+
pub const fn entry_addr(&self) -> u32 {
3838
self.entry_addr
3939
}
4040
}

multiboot2-header/src/framebuffer.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@ impl FramebufferHeaderTag {
2828
}
2929
}
3030

31-
pub fn typ(&self) -> HeaderTagType {
31+
pub const fn typ(&self) -> HeaderTagType {
3232
self.typ
3333
}
34-
pub fn flags(&self) -> HeaderTagFlag {
34+
pub const fn flags(&self) -> HeaderTagFlag {
3535
self.flags
3636
}
37-
pub fn size(&self) -> u32 {
37+
pub const fn size(&self) -> u32 {
3838
self.size
3939
}
40-
pub fn width(&self) -> u32 {
40+
pub const fn width(&self) -> u32 {
4141
self.width
4242
}
43-
pub fn height(&self) -> u32 {
43+
pub const fn height(&self) -> u32 {
4444
self.height
4545
}
46-
pub fn depth(&self) -> u32 {
46+
pub const fn depth(&self) -> u32 {
4747
self.depth
4848
}
4949
}

multiboot2-header/src/header/builder.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
//! Exports item [`Multiboot2HeaderBuilder`].
2+
13
use crate::HeaderTagISA;
24
use crate::{
35
AddressHeaderTag, ConsoleHeaderTag, EfiBootServiceHeaderTag, EndHeaderTag, EntryEfi32HeaderTag,
46
EntryEfi64HeaderTag, EntryHeaderTag, FramebufferHeaderTag, InformationRequestHeaderTagBuilder,
5-
ModuleAlignHeaderTag, Multiboot2HeaderInner, RelocatableHeaderTag, StructAsBytes,
7+
ModuleAlignHeaderTag, Multiboot2BasicHeader, RelocatableHeaderTag, StructAsBytes,
68
};
79
use core::mem::size_of;
810

@@ -64,10 +66,10 @@ impl Multiboot2HeaderBuilder {
6466
}
6567
}
6668

67-
/// Returns the expected length of the multiboot2 header,
68-
/// when the [`build`]-method gets called.
69+
/// Returns the expected length of the Multiboot2 header,
70+
/// when the `build()`-method gets called.
6971
pub fn expected_len(&self) -> usize {
70-
let base_len = size_of::<Multiboot2HeaderInner>();
72+
let base_len = size_of::<Multiboot2BasicHeader>();
7173
// size_or_up_aligned not required, because basic header length is 16 and the
7274
// begin is 8 byte aligned => first tag automatically 8 byte aligned
7375
let mut len = Self::size_or_up_aligned(base_len);
@@ -109,9 +111,9 @@ impl Multiboot2HeaderBuilder {
109111
len
110112
}
111113

112-
/// Adds the bytes of a tag to the final multiboot2 header byte vector.
114+
/// Adds the bytes of a tag to the final Multiboot2 header byte vector.
113115
/// Align should be true for all tags except the end tag.
114-
fn build_add_bytes(dest: &mut Vec<u8>, source: &Vec<u8>, is_end_tag: bool) {
116+
fn build_add_bytes(dest: &mut Vec<u8>, source: &[u8], is_end_tag: bool) {
115117
dest.extend(source);
116118
if !is_end_tag {
117119
let size = source.len();
@@ -130,7 +132,7 @@ impl Multiboot2HeaderBuilder {
130132
Self::build_add_bytes(
131133
&mut data,
132134
// important that we write the correct expected length into the header!
133-
&Multiboot2HeaderInner::new(self.arch, self.expected_len() as u32).struct_as_bytes(),
135+
&Multiboot2BasicHeader::new(self.arch, self.expected_len() as u32).struct_as_bytes(),
134136
false,
135137
);
136138

@@ -174,46 +176,48 @@ impl Multiboot2HeaderBuilder {
174176
data
175177
}
176178

179+
// clippy thinks this can be a const fn but the compiler denies it
180+
#[allow(clippy::missing_const_for_fn)]
177181
pub fn information_request_tag(
178182
mut self,
179183
information_request_tag: InformationRequestHeaderTagBuilder,
180184
) -> Self {
181185
self.information_request_tag = Some(information_request_tag);
182186
self
183187
}
184-
pub fn address_tag(mut self, address_tag: AddressHeaderTag) -> Self {
188+
pub const fn address_tag(mut self, address_tag: AddressHeaderTag) -> Self {
185189
self.address_tag = Some(address_tag);
186190
self
187191
}
188-
pub fn entry_tag(mut self, entry_tag: EntryHeaderTag) -> Self {
192+
pub const fn entry_tag(mut self, entry_tag: EntryHeaderTag) -> Self {
189193
self.entry_tag = Some(entry_tag);
190194
self
191195
}
192-
pub fn console_tag(mut self, console_tag: ConsoleHeaderTag) -> Self {
196+
pub const fn console_tag(mut self, console_tag: ConsoleHeaderTag) -> Self {
193197
self.console_tag = Some(console_tag);
194198
self
195199
}
196-
pub fn framebuffer_tag(mut self, framebuffer_tag: FramebufferHeaderTag) -> Self {
200+
pub const fn framebuffer_tag(mut self, framebuffer_tag: FramebufferHeaderTag) -> Self {
197201
self.framebuffer_tag = Some(framebuffer_tag);
198202
self
199203
}
200-
pub fn module_align_tag(mut self, module_align_tag: ModuleAlignHeaderTag) -> Self {
204+
pub const fn module_align_tag(mut self, module_align_tag: ModuleAlignHeaderTag) -> Self {
201205
self.module_align_tag = Some(module_align_tag);
202206
self
203207
}
204-
pub fn efi_bs_tag(mut self, efi_bs_tag: EfiBootServiceHeaderTag) -> Self {
208+
pub const fn efi_bs_tag(mut self, efi_bs_tag: EfiBootServiceHeaderTag) -> Self {
205209
self.efi_bs_tag = Some(efi_bs_tag);
206210
self
207211
}
208-
pub fn efi_32_tag(mut self, efi_32_tag: EntryEfi32HeaderTag) -> Self {
212+
pub const fn efi_32_tag(mut self, efi_32_tag: EntryEfi32HeaderTag) -> Self {
209213
self.efi_32_tag = Some(efi_32_tag);
210214
self
211215
}
212-
pub fn efi_64_tag(mut self, efi_64_tag: EntryEfi64HeaderTag) -> Self {
216+
pub const fn efi_64_tag(mut self, efi_64_tag: EntryEfi64HeaderTag) -> Self {
213217
self.efi_64_tag = Some(efi_64_tag);
214218
self
215219
}
216-
pub fn relocatable_tag(mut self, relocatable_tag: RelocatableHeaderTag) -> Self {
220+
pub const fn relocatable_tag(mut self, relocatable_tag: RelocatableHeaderTag) -> Self {
217221
self.relocatable_tag = Some(relocatable_tag);
218222
self
219223
}
@@ -222,8 +226,8 @@ impl Multiboot2HeaderBuilder {
222226
#[cfg(test)]
223227
mod tests {
224228
use crate::{
225-
load_mb2_header, HeaderTagFlag, HeaderTagISA, InformationRequestHeaderTagBuilder,
226-
MbiTagType, Multiboot2HeaderBuilder, RelocatableHeaderTag,
229+
HeaderTagFlag, HeaderTagISA, InformationRequestHeaderTagBuilder, MbiTagType,
230+
Multiboot2Header, Multiboot2HeaderBuilder, RelocatableHeaderTag,
227231
RelocatableHeaderTagPreference,
228232
};
229233

@@ -238,7 +242,7 @@ mod tests {
238242
#[test]
239243
fn test_size_builder() {
240244
let builder = Multiboot2HeaderBuilder::new(HeaderTagISA::I386);
241-
// multiboot2 basic header + end tag
245+
// Multiboot2 basic header + end tag
242246
let mut expected_len = 16 + 8;
243247
assert_eq!(builder.expected_len(), expected_len);
244248

@@ -272,7 +276,7 @@ mod tests {
272276

273277
let mb2_hdr_data = builder.build();
274278
let mb2_hdr = mb2_hdr_data.as_ptr() as usize;
275-
let mb2_hdr = unsafe { load_mb2_header(mb2_hdr) };
279+
let mb2_hdr = unsafe { Multiboot2Header::from_addr(mb2_hdr) };
276280
println!("{:#?}", mb2_hdr);
277281

278282
/* you can write the binary to a file and a tool such as crate "bootinfo"

0 commit comments

Comments
 (0)