Skip to content

Commit 267e0ee

Browse files
authored
Merge pull request #154 from rust-osdev/dev2
various cleanups
2 parents 7f3fc63 + fe48429 commit 267e0ee

File tree

16 files changed

+106
-120
lines changed

16 files changed

+106
-120
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/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ readme = "README.md"
2626
homepage = "https://github.com/rust-osdev/multiboot2-header"
2727
repository = "https://github.com/rust-osdev/multiboot2"
2828
documentation = "https://docs.rs/multiboot2-header"
29-
rust-version = "1.60"
29+
rust-version = "1.68"
3030

3131
[[example]]
3232
name = "minimal"

multiboot2-header/Changelog.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
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 `multiboot2@v0.16.0`
7+
- **BREAKING** renamed `MULTIBOOT2_HEADER_MAGIC` to `MAGIC`
8+
- **BREAKING** renamed `Multiboot2HeaderBuilder` to `HeaderBuilder`
9+
- **BREAKING** renamed `from_addr` to `load`. The function now consumes a ptr.
610
- added the optional `unstable` feature (requires nightly)
711
- implement `core::error::Error` for `LoadError`
8-
- depends on `multiboot2@v0.16.0`
9-
- **BREAKING** renamed `Multiboot2HeaderBuilder` to `HeaderBuilder`
1012

1113
## 0.2.0 (2022-05-03)
1214
- **BREAKING** renamed `EntryHeaderTag` to `EntryAddressHeaderTag`

multiboot2-header/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn main() {
5252
.build();
5353

5454
// Cast bytes in vector to Multiboot2 information structure
55-
let mb2_hdr = unsafe { Multiboot2Header::from_addr(mb2_hdr_bytes.as_ptr() as usize) };
55+
let mb2_hdr = unsafe { Multiboot2Header::from_addr(mb2_hdr_bytes.as_ptr().cast()) };
5656
println!("{:#?}", mb2_hdr);
5757
}
5858
```
@@ -69,7 +69,7 @@ You may need a special linker script to place this in a LOAD segment with a file
6969
See specification.
7070

7171
## MSRV
72-
The MSRV is 1.56.1 stable.
72+
The MSRV is 1.68.0 stable.
7373

7474
## License & Contribution
7575

multiboot2-header/examples/minimal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ fn main() {
2323
.build();
2424

2525
// Cast bytes in vector to Multiboot2 information structure
26-
let mb2_hdr = unsafe { Multiboot2Header::from_addr(mb2_hdr_bytes.as_ptr() as usize) };
26+
let mb2_hdr = unsafe { Multiboot2Header::load(mb2_hdr_bytes.as_ptr().cast()) };
2727
println!("{:#?}", mb2_hdr);
2828
}

multiboot2-header/src/builder/header.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ mod tests {
281281
println!("expected_len: {} bytes", builder.expected_len());
282282

283283
let mb2_hdr_data = builder.build();
284-
let mb2_hdr = mb2_hdr_data.as_ptr() as usize;
285-
let mb2_hdr = unsafe { Multiboot2Header::from_addr(mb2_hdr) }
284+
let mb2_hdr = mb2_hdr_data.as_ptr().cast();
285+
let mb2_hdr = unsafe { Multiboot2Header::load(mb2_hdr) }
286286
.expect("the generated header to be loadable");
287287
println!("{:#?}", mb2_hdr);
288288
assert_eq!(

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: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@ 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
1616
/// by all tags (see [`crate::tags::HeaderTagType`]).
1717
/// Use this if you get a pointer to the header and just want
1818
/// to parse it. If you want to construct the type by yourself,
19-
/// please look at [`crate::builder::HeaderBuilder`].
19+
/// please look at [`crate::builder::HeaderBuilder`]..
20+
#[derive(Debug)]
2021
#[repr(transparent)]
21-
pub struct Multiboot2Header<'a> {
22-
inner: &'a Multiboot2BasicHeader,
23-
}
22+
pub struct Multiboot2Header<'a>(&'a Multiboot2BasicHeader);
2423

2524
impl<'a> Multiboot2Header<'a> {
2625
/// Public constructor for this type with various validations.
@@ -35,37 +34,41 @@ impl<'a> Multiboot2Header<'a> {
3534
/// # Safety
3635
/// This function may produce undefined behaviour, if the provided `addr` is not a valid
3736
/// Multiboot2 header pointer.
38-
// This function can be `const` on newer Rust versions.
39-
#[allow(clippy::missing_const_for_fn)]
40-
pub unsafe fn from_addr(addr: usize) -> Result<Self, LoadError> {
41-
if addr == 0 || addr % 8 != 0 {
37+
pub unsafe fn load(ptr: *const Multiboot2BasicHeader) -> Result<Self, LoadError> {
38+
// null or not aligned
39+
if ptr.is_null() || ptr.align_offset(8) != 0 {
4240
return Err(LoadError::InvalidAddress);
4341
}
44-
let ptr = addr as *const Multiboot2BasicHeader;
42+
4543
let reference = &*ptr;
46-
if reference.header_magic() != MULTIBOOT2_HEADER_MAGIC {
44+
45+
if reference.header_magic() != MAGIC {
4746
return Err(LoadError::MagicNotFound);
4847
}
48+
4949
if !reference.verify_checksum() {
5050
return Err(LoadError::ChecksumMismatch);
5151
}
52-
Ok(Self { inner: reference })
52+
53+
Ok(Self(reference))
5354
}
5455

5556
/// Find the header in a given slice.
5657
///
5758
/// If it succeeds, it returns a tuple consisting of the subslice containing
5859
/// 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
60+
/// If it fails (either because the header is not properly 64-bit aligned
6061
/// or because it is truncated), it returns a [`LoadError`].
6162
/// If there is no header, it returns `None`.
6263
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);
64+
if buffer.as_ptr().align_offset(4) != 0 {
65+
return Err(LoadError::InvalidAddress);
66+
}
67+
6568
let mut windows = buffer[0..8192].windows(4);
6669
let magic_index = match windows.position(|vals| {
6770
u32::from_le_bytes(vals.try_into().unwrap()) // yes, there's 4 bytes here
68-
== MULTIBOOT2_HEADER_MAGIC
71+
== MAGIC
6972
}) {
7073
Some(idx) => {
7174
if idx % 8 == 0 {
@@ -102,27 +105,27 @@ impl<'a> Multiboot2Header<'a> {
102105

103106
/// Wrapper around [`Multiboot2BasicHeader::verify_checksum`].
104107
pub const fn verify_checksum(&self) -> bool {
105-
self.inner.verify_checksum()
108+
self.0.verify_checksum()
106109
}
107110
/// Wrapper around [`Multiboot2BasicHeader::header_magic`].
108111
pub const fn header_magic(&self) -> u32 {
109-
self.inner.header_magic()
112+
self.0.header_magic()
110113
}
111114
/// Wrapper around [`Multiboot2BasicHeader::arch`].
112115
pub const fn arch(&self) -> HeaderTagISA {
113-
self.inner.arch()
116+
self.0.arch()
114117
}
115118
/// Wrapper around [`Multiboot2BasicHeader::length`].
116119
pub const fn length(&self) -> u32 {
117-
self.inner.length()
120+
self.0.length()
118121
}
119122
/// Wrapper around [`Multiboot2BasicHeader::checksum`].
120123
pub const fn checksum(&self) -> u32 {
121-
self.inner.checksum()
124+
self.0.checksum()
122125
}
123126
/// Wrapper around [`Multiboot2BasicHeader::tag_iter`].
124127
pub fn iter(&self) -> Multiboot2HeaderTagIter {
125-
self.inner.tag_iter()
128+
self.0.tag_iter()
126129
}
127130
/// Wrapper around [`Multiboot2BasicHeader::calc_checksum`].
128131
pub const fn calc_checksum(magic: u32, arch: HeaderTagISA, length: u32) -> u32 {
@@ -190,14 +193,6 @@ impl<'a> Multiboot2Header<'a> {
190193
}
191194
}
192195

193-
impl<'a> Debug for Multiboot2Header<'a> {
194-
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
195-
// For debug fmt we only output the inner field
196-
let reference = unsafe { &*(self.inner as *const Multiboot2BasicHeader) };
197-
Debug::fmt(reference, f)
198-
}
199-
}
200-
201196
/// Errors that can occur when parsing a header from a slice.
202197
/// See [`Multiboot2Header::find_header`].
203198
#[derive(Copy, Clone, Debug, derive_more::Display, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -215,28 +210,25 @@ pub enum LoadError {
215210
#[cfg(feature = "unstable")]
216211
impl core::error::Error for LoadError {}
217212

218-
/// **Use this only if you know what you do. You probably want to use
219-
/// [`Multiboot2Header`] instead.**
220-
///
221213
/// The "basic" Multiboot2 header. This means only the properties, that are known during
222214
/// compile time. All other information are derived during runtime from the size property.
223215
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
224216
#[repr(C)]
225217
pub struct Multiboot2BasicHeader {
226-
/// Must be the value of [`MULTIBOOT2_HEADER_MAGIC`].
218+
/// Must be the value of [`MAGIC`].
227219
header_magic: u32,
228220
arch: HeaderTagISA,
229221
length: u32,
230222
checksum: u32,
231-
// additional tags..
232-
// at minimum the end tag
223+
// Followed by dynamic amount of dynamically sized header tags.
224+
// At minimum, the end tag.
233225
}
234226

235227
impl Multiboot2BasicHeader {
236228
#[cfg(feature = "builder")]
237229
/// Constructor for the basic header.
238230
pub(crate) const fn new(arch: HeaderTagISA, length: u32) -> Self {
239-
let magic = MULTIBOOT2_HEADER_MAGIC;
231+
let magic = MAGIC;
240232
let checksum = Self::calc_checksum(magic, arch, length);
241233
Multiboot2BasicHeader {
242234
header_magic: magic,
@@ -257,7 +249,6 @@ impl Multiboot2BasicHeader {
257249
(0x100000000 - magic as u64 - arch as u64 - length as u64) as u32
258250
}
259251

260-
/// Returns
261252
pub const fn header_magic(&self) -> u32 {
262253
self.header_magic
263254
}

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-header/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
//! .build();
2626
//!
2727
//! // Cast bytes in vector to Multiboot2 information structure
28-
//! let mb2_hdr = unsafe { Multiboot2Header::from_addr(mb2_hdr_bytes.as_ptr() as usize) };
28+
//! let mb2_hdr = unsafe { Multiboot2Header::load(mb2_hdr_bytes.as_ptr().cast()) };
2929
//! println!("{:#?}", mb2_hdr);
3030
//!
3131
//! ```
3232
//!
3333
//! ## MSRV
34-
//! The MSRV is 1.56.1 stable.
34+
//! The MSRV is 1.68.0 stable.
3535
3636
#![no_std]
3737
#![cfg_attr(feature = "unstable", feature(error_in_core))]

multiboot2/Cargo.toml

Lines changed: 2 additions & 2 deletions
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.
@@ -31,7 +31,7 @@ readme = "README.md"
3131
homepage = "https://github.com/rust-osdev/multiboot2"
3232
repository = "https://github.com/rust-osdev/multiboot2"
3333
documentation = "https://docs.rs/multiboot2"
34-
rust-version = "1.60"
34+
rust-version = "1.68"
3535

3636
[features]
3737
default = ["builder"]

multiboot2/Changelog.md

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

33
## 0.16.0 (xxxx-xx-xx)
4-
- Add `TagTrait` trait which enables to use DSTs as multiboot2 tags. This is
5-
mostly relevant for the command line tag, the modules tag, and the bootloader
6-
name tag. However, this might also be relevant for users of custom multiboot2
7-
tags that use DSTs as types. See the example provided in the doc of the
8-
`get_tag` method.
9-
- renamed `MULTIBOOT2_BOOTLOADER_MAGIC` to `MAGIC`
10-
- added a `builder` feature and a `builder` module with a
11-
`builder::InformationBuilder` struct
12-
- `EFIMemoryDesc` was removed and is now an alias of
4+
- **BREAKING** renamed `MULTIBOOT2_BOOTLOADER_MAGIC` to `MAGIC`
5+
- **BREAKING** `EFIMemoryDesc` was removed and is now an alias of
136
`uefi_raw::table::boot::MemoryDescriptor`
14-
- `EFIMemoryAreaType` was removed and is now an alias of
7+
- **BREAKING** `EFIMemoryAreaType` was removed and is now an alias of
158
`uefi_raw::table::boot::MemoryType`
16-
- MSRV is 1.68.0
9+
- **BREAKING** MSRV is 1.68.0
1710
- **BREAKING** Removed `MemoryAreaIter` and `MemoryMapTag::available_memory_areas`
18-
- Added `MemoryMapTag::entry_size` and `MemoryMapTag::entry_version`
1911
- **BREAKING** Renamed `BootInformation::load_base_addr` to `BootInformation::load_base_addr_tag`
2012
- **BREAKING** Renamed `BootInformation::efi_32_ih` to `BootInformation::efi_32_ih_tag`
2113
- **BREAKING** Renamed `BootInformation::efi_32_ih` to `BootInformation::efi_32_ih_tag`
@@ -25,8 +17,17 @@
2517
- **BREAKING** Renamed `EFISdt32` to `EFISdt32Tag`
2618
- **BREAKING** Renamed `EFISdt64` to `EFISdt64Tag`
2719
- **BREAKING** Renamed `EFIBootServicesNotExited` to `EFIBootServicesNotExitedTag`
20+
- **\[Might be\] BREAKING** Added `TagTrait` trait which enables to use DSTs as multiboot2 tags. This is
21+
mostly relevant for the command line tag, the modules tag, and the bootloader
22+
name tag. However, this might also be relevant for users of custom multiboot2
23+
tags that use DSTs as types. See the example provided in the doc of the
24+
`get_tag` method.
25+
- added a `builder` feature and a `builder` module with a
26+
`builder::InformationBuilder` struct
27+
- added `BootInformation::efi_bs_not_exited_tag`
2828
- deprecated `load` and `load_with_offset`
2929
- added `BootInformation::load` as new default constructor
30+
- added `MemoryMapTag::entry_size` and `MemoryMapTag::entry_version`
3031

3132
## 0.15.1 (2023-03-18)
3233
- **BREAKING** `MemoryMapTag::all_memory_areas()` was renamed to `memory_areas`

multiboot2/README.md

Lines changed: 2 additions & 2 deletions
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.
@@ -37,7 +37,7 @@ other fields | variable
3737
All tags and the mbi itself are 8-byte aligned. The last tag must be the _end tag_, which is a tag of type `0` and size `8`.
3838

3939
## MSRV
40-
The MSRV is 1.56.1 stable.
40+
The MSRV is 1.68.0 stable.
4141

4242
## License & Contribution
4343

0 commit comments

Comments
 (0)