Skip to content

Commit 4a73d8d

Browse files
committed
multiboot2: code cleanup
1 parent da3f224 commit 4a73d8d

File tree

10 files changed

+60
-61
lines changed

10 files changed

+60
-61
lines changed

.github/workflows/qa.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ jobs:
99
steps:
1010
- uses: actions/checkout@v3
1111
# Executes "typos ."
12-
- uses: crate-ci/typos@v1.13.20
12+
- uses: crate-ci/typos@v1.15.2

multiboot2-header/Changelog.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# CHANGELOG for crate `multiboot2-header`
22

33
## 0.3.0 (xxxx-xx-xx)
4-
- MSRV is 1.68.0
5-
- renamed the `std` feature to `alloc`
4+
- **BREAKING** MSRV is 1.68.0
5+
- **BREAKING** renamed the `std` feature to `alloc`
6+
- **BREAKING** bumped dependency to `[email protected]`
7+
- **BREAKING** renamed `MULTIBOOT2_HEADER_MAGIC` to `MAGIC`
8+
- **BREAKING** renamed `Multiboot2HeaderBuilder` to `HeaderBuilder`
69
- added the optional `unstable` feature (requires nightly)
710
- implement `core::error::Error` for `LoadError`
8-
- depends on `[email protected]`
9-
- **BREAKING** renamed `Multiboot2HeaderBuilder` to `HeaderBuilder`
1011

1112
## 0.2.0 (2022-05-03)
1213
- **BREAKING** renamed `EntryHeaderTag` to `EntryAddressHeaderTag`

multiboot2-header/src/builder/information_request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct InformationRequestHeaderTagBuilder {
2222
#[cfg(feature = "builder")]
2323
impl InformationRequestHeaderTagBuilder {
2424
/// New builder.
25-
pub fn new(flag: HeaderTagFlag) -> Self {
25+
pub const fn new(flag: HeaderTagFlag) -> Self {
2626
Self {
2727
irs: BTreeSet::new(),
2828
flag,

multiboot2-header/src/builder/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ mod tests {
5858
c: u128,
5959
}
6060
impl StructAsBytes for Foobar {}
61-
#[allow(clippy::blacklisted_name)]
61+
#[allow(clippy::disallowed_names)]
6262
let foo = Foobar {
6363
a: 11,
6464
b: 22,

multiboot2-header/src/header.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use core::convert::TryInto;
88
use core::fmt::{Debug, Formatter};
99
use core::mem::size_of;
1010

11-
/// Magic value for a [`Multiboot2Header`], as defined in spec.
12-
pub const MULTIBOOT2_HEADER_MAGIC: u32 = 0xe85250d6;
11+
/// Magic value for a [`Multiboot2Header`], as defined by the spec.
12+
pub const MAGIC: u32 = 0xe85250d6;
1313

1414
/// Wrapper type around a pointer to the Multiboot2 header.
1515
/// The Multiboot2 header is the [`Multiboot2BasicHeader`] followed
@@ -43,7 +43,7 @@ impl<'a> Multiboot2Header<'a> {
4343
}
4444
let ptr = addr as *const Multiboot2BasicHeader;
4545
let reference = &*ptr;
46-
if reference.header_magic() != MULTIBOOT2_HEADER_MAGIC {
46+
if reference.header_magic() != MAGIC {
4747
return Err(LoadError::MagicNotFound);
4848
}
4949
if !reference.verify_checksum() {
@@ -56,16 +56,18 @@ impl<'a> Multiboot2Header<'a> {
5656
///
5757
/// If it succeeds, it returns a tuple consisting of the subslice containing
5858
/// just the header and the index of the header in the given slice.
59-
/// If it fails (either because the header is not properply 64-bit aligned
59+
/// If it fails (either because the header is not properly 64-bit aligned
6060
/// or because it is truncated), it returns a [`LoadError`].
6161
/// If there is no header, it returns `None`.
6262
pub fn find_header(buffer: &[u8]) -> Result<Option<(&[u8], u32)>, LoadError> {
63-
// the magic is 32 bit aligned and inside the first 8192 bytes
64-
assert!(buffer.len() >= 8192);
63+
if buffer.as_ptr().align_offset(4) != 0 {
64+
return Err(LoadError::InvalidAddress)
65+
}
66+
6567
let mut windows = buffer[0..8192].windows(4);
6668
let magic_index = match windows.position(|vals| {
6769
u32::from_le_bytes(vals.try_into().unwrap()) // yes, there's 4 bytes here
68-
== MULTIBOOT2_HEADER_MAGIC
70+
== MAGIC
6971
}) {
7072
Some(idx) => {
7173
if idx % 8 == 0 {
@@ -223,7 +225,7 @@ impl core::error::Error for LoadError {}
223225
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
224226
#[repr(C)]
225227
pub struct Multiboot2BasicHeader {
226-
/// Must be the value of [`MULTIBOOT2_HEADER_MAGIC`].
228+
/// Must be the value of [`MAGIC`].
227229
header_magic: u32,
228230
arch: HeaderTagISA,
229231
length: u32,
@@ -236,7 +238,7 @@ impl Multiboot2BasicHeader {
236238
#[cfg(feature = "builder")]
237239
/// Constructor for the basic header.
238240
pub(crate) const fn new(arch: HeaderTagISA, length: u32) -> Self {
239-
let magic = MULTIBOOT2_HEADER_MAGIC;
241+
let magic = MAGIC;
240242
let checksum = Self::calc_checksum(magic, arch, length);
241243
Multiboot2BasicHeader {
242244
header_magic: magic,

multiboot2-header/src/information_request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<const N: usize> InformationRequestHeaderTag<N> {
6565
}
6666

6767
/// Returns an [`InformationRequestHeaderTagIter`].
68-
pub fn req_iter(&self) -> InformationRequestHeaderTagIter {
68+
pub const fn req_iter(&self) -> InformationRequestHeaderTagIter {
6969
let base_struct_size = size_of::<InformationRequestHeaderTag<0>>();
7070
let count = self.dynamic_requests_size();
7171
let base_ptr = self as *const InformationRequestHeaderTag<N>;

multiboot2/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "multiboot2"
33
description = """
4-
Library that helps you to parse the multiboot information structure (mbi) from
4+
Library that assists parsing the Multiboot2 Information Structure (MBI) from
55
Multiboot2-compliant bootloaders, like GRUB. It supports all tags from the specification
66
including full support for the sections of ELF-64. This library is `no_std` and can be
77
used in a Multiboot2-kernel.

multiboot2/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![crates.io](https://img.shields.io/crates/v/multiboot2.svg)](https://crates.io/crates/multiboot2)
44
[![docs](https://docs.rs/multiboot2/badge.svg)](https://docs.rs/multiboot2/)
55

6-
Rust library that helps you to parse the multiboot information structure (mbi) from
6+
Rust library that assists parsing the Multiboot2 Information Structure (MBI) from
77
Multiboot2-compliant bootloaders, like GRUB. It supports all tags from the specification
88
including full support for the sections of ELF-64 files. This library is `no_std` and can be
99
used in a Multiboot2-kernel.

multiboot2/src/lib.rs

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#![allow(rustdoc::private_doc_tests)]
1010
// --- END STYLE CHECKS ---
1111

12-
//! Library that helps you to parse the multiboot information structure (mbi) from
12+
//! Library that assists parsing the Multiboot2 Information Structure (MBI) from
1313
//! Multiboot2-compliant bootloaders, like GRUB. It supports all tags from the specification
1414
//! including full support for the sections of ELF-64. This library is `no_std` and can be
1515
//! used in a Multiboot2-kernel.
@@ -139,6 +139,42 @@ pub enum MbiLoadError {
139139
#[cfg(feature = "unstable")]
140140
impl core::error::Error for MbiLoadError {}
141141

142+
#[repr(C, align(8))]
143+
struct BootInformationInner {
144+
total_size: u32,
145+
_reserved: u32,
146+
// followed by various, dynamically sized multiboot2 tags
147+
tags: [Tag; 0],
148+
}
149+
150+
impl BootInformationInner {
151+
#[cfg(feature = "builder")]
152+
fn new(total_size: u32) -> Self {
153+
Self {
154+
total_size,
155+
_reserved: 0,
156+
tags: [],
157+
}
158+
}
159+
160+
fn has_valid_end_tag(&self) -> bool {
161+
let end_tag_prototype = EndTag::default();
162+
163+
let self_ptr = self as *const _;
164+
let end_tag_addr = self_ptr as usize + (self.total_size - end_tag_prototype.size) as usize;
165+
let end_tag = unsafe { &*(end_tag_addr as *const Tag) };
166+
167+
end_tag.typ == end_tag_prototype.typ && end_tag.size == end_tag_prototype.size
168+
}
169+
}
170+
171+
#[cfg(feature = "builder")]
172+
impl StructAsBytes for BootInformationInner {
173+
fn byte_size(&self) -> usize {
174+
core::mem::size_of::<Self>()
175+
}
176+
}
177+
142178
/// A Multiboot 2 Boot Information (MBI) accessor.
143179
#[repr(transparent)]
144180
pub struct BootInformation<'a>(&'a BootInformationInner);
@@ -191,35 +227,7 @@ impl BootInformation<'_> {
191227

192228
Ok(Self(mbi))
193229
}
194-
}
195-
196-
#[repr(C, align(8))]
197-
struct BootInformationInner {
198-
total_size: u32,
199-
_reserved: u32,
200-
// followed by various, dynamically sized multiboot2 tags
201-
tags: [Tag; 0],
202-
}
203-
204-
impl BootInformationInner {
205-
#[cfg(feature = "builder")]
206-
fn new(total_size: u32) -> Self {
207-
Self {
208-
total_size,
209-
_reserved: 0,
210-
tags: [],
211-
}
212-
}
213-
}
214-
215-
#[cfg(feature = "builder")]
216-
impl StructAsBytes for BootInformationInner {
217-
fn byte_size(&self) -> usize {
218-
core::mem::size_of::<Self>()
219-
}
220-
}
221230

222-
impl BootInformation<'_> {
223231
/// Get the start address of the boot info.
224232
pub fn start_address(&self) -> usize {
225233
core::ptr::addr_of!(*self.0) as usize
@@ -438,18 +446,6 @@ impl BootInformation<'_> {
438446
}
439447
}
440448

441-
impl BootInformationInner {
442-
fn has_valid_end_tag(&self) -> bool {
443-
let end_tag_prototype = EndTag::default();
444-
445-
let self_ptr = self as *const _;
446-
let end_tag_addr = self_ptr as usize + (self.total_size - end_tag_prototype.size) as usize;
447-
let end_tag = unsafe { &*(end_tag_addr as *const Tag) };
448-
449-
end_tag.typ == end_tag_prototype.typ && end_tag.size == end_tag_prototype.size
450-
}
451-
}
452-
453449
// SAFETY: BootInformation contains a const ptr to memory that is never mutated.
454450
// Sending this pointer to other threads is sound.
455451
unsafe impl Send for BootInformation<'_> {}

multiboot2/src/tag_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ mod partial_eq_impls {
287287
}
288288

289289
/// Common base structure for all tags that can be passed via the Multiboot2
290-
/// information structure (MBI) to a Multiboot2 payload/program/kernel.
290+
/// Information Structure (MBI) to a Multiboot2 payload/program/kernel.
291291
///
292292
/// Can be transformed to any other tag (sized or unsized/DST) via
293293
/// [`Tag::cast_tag`].

0 commit comments

Comments
 (0)