Skip to content

Commit 37ea973

Browse files
committed
multiboot2: cleanup
1 parent 865b5bb commit 37ea973

File tree

4 files changed

+76
-73
lines changed

4 files changed

+76
-73
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ jobs:
7575
# check that devs can also use this on Windows.
7676
build_nostd_stable_windows:
7777
name: build no_std (stable) [Windows]
78+
needs: build_stable
7879
uses: ./.github/workflows/_build-rust.yml
7980
with:
8081
runs-on: windows-latest

multiboot2/src/efi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ mod tests {
162162

163163
#[test]
164164
fn test_build_eftih64() {
165-
let tag = EFIImageHandle32::new(ADDR.try_into().unwrap());
165+
let tag = EFIImageHandle64::new(ADDR.try_into().unwrap());
166166
assert_eq!(tag.image_handle(), ADDR);
167167
}
168168
}

multiboot2/src/framebuffer.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Reader, Tag, TagTrait, TagTypeId};
1+
use crate::{Tag, TagTrait, TagTypeId};
22

33
use core::fmt::Debug;
44
use core::mem::size_of;
@@ -11,6 +11,39 @@ use {
1111
alloc::boxed::Box, alloc::vec::Vec,
1212
};
1313

14+
/// Helper struct to read bytes from a raw pointer and increase the pointer
15+
/// automatically.
16+
pub struct Reader {
17+
ptr: *const u8,
18+
off: usize,
19+
}
20+
21+
impl Reader {
22+
pub(crate) fn new<T>(ptr: *const T) -> Reader {
23+
Reader {
24+
ptr: ptr as *const u8,
25+
off: 0,
26+
}
27+
}
28+
29+
pub(crate) fn read_u8(&mut self) -> u8 {
30+
self.off += 1;
31+
unsafe { *self.ptr.add(self.off - 1) }
32+
}
33+
34+
pub(crate) fn read_u16(&mut self) -> u16 {
35+
self.read_u8() as u16 | (self.read_u8() as u16) << 8
36+
}
37+
38+
pub(crate) fn read_u32(&mut self) -> u32 {
39+
self.read_u16() as u32 | (self.read_u16() as u32) << 16
40+
}
41+
42+
pub(crate) fn current_address(&self) -> usize {
43+
unsafe { self.ptr.add(self.off) as usize }
44+
}
45+
}
46+
1447
const METADATA_SIZE: usize = size_of::<TagTypeId>()
1548
+ 4 * size_of::<u32>()
1649
+ size_of::<u64>()

multiboot2/src/lib.rs

Lines changed: 40 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,16 @@ pub mod builder;
9898
/// that the Rust compiler output changes `eax` before you can access it.
9999
pub const MAGIC: u32 = 0x36d76289;
100100

101+
/// # Safety
102+
/// Deprecated. Please use BootInformation::load() instead.
101103
#[deprecated = "Please use BootInformation::load() instead."]
102104
pub unsafe fn load<'a>(address: usize) -> Result<BootInformation<'a>, MbiLoadError> {
103105
let ptr = address as *const u8;
104106
BootInformation::load(ptr)
105107
}
106108

109+
/// # Safety
110+
/// Deprecated. Please use BootInformation::load() instead.
107111
#[deprecated = "Please use BootInformation::load() instead."]
108112
pub unsafe fn load_with_offset<'a>(
109113
address: usize,
@@ -135,6 +139,42 @@ pub enum MbiLoadError {
135139
#[cfg(feature = "unstable")]
136140
impl core::error::Error for MbiLoadError {}
137141

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+
138178
/// A Multiboot 2 Boot Information (MBI) accessor.
139179
#[repr(transparent)]
140180
pub struct BootInformation<'a>(&'a BootInformationInner);
@@ -185,35 +225,7 @@ impl BootInformation<'_> {
185225

186226
Ok(Self(mbi))
187227
}
188-
}
189-
190-
#[repr(C, align(8))]
191-
struct BootInformationInner {
192-
total_size: u32,
193-
_reserved: u32,
194-
// followed by various, dynamically sized multiboot2 tags
195-
tags: [Tag; 0],
196-
}
197-
198-
impl BootInformationInner {
199-
#[cfg(feature = "builder")]
200-
fn new(total_size: u32) -> Self {
201-
Self {
202-
total_size,
203-
_reserved: 0,
204-
tags: [],
205-
}
206-
}
207-
}
208228

209-
#[cfg(feature = "builder")]
210-
impl StructAsBytes for BootInformationInner {
211-
fn byte_size(&self) -> usize {
212-
core::mem::size_of::<Self>()
213-
}
214-
}
215-
216-
impl BootInformation<'_> {
217229
/// Get the start address of the boot info.
218230
pub fn start_address(&self) -> usize {
219231
core::ptr::addr_of!(*self.0) as usize
@@ -427,18 +439,6 @@ impl BootInformation<'_> {
427439
}
428440
}
429441

430-
impl BootInformationInner {
431-
fn has_valid_end_tag(&self) -> bool {
432-
let end_tag_prototype = EndTag::default();
433-
434-
let self_ptr = self as *const _;
435-
let end_tag_addr = self_ptr as usize + (self.total_size - end_tag_prototype.size) as usize;
436-
let end_tag = unsafe { &*(end_tag_addr as *const Tag) };
437-
438-
end_tag.typ == end_tag_prototype.typ && end_tag.size == end_tag_prototype.size
439-
}
440-
}
441-
442442
// SAFETY: BootInformation contains a const ptr to memory that is never mutated.
443443
// Sending this pointer to other threads is sound.
444444
unsafe impl Send for BootInformation<'_> {}
@@ -496,37 +496,6 @@ impl fmt::Debug for BootInformation<'_> {
496496
}
497497
}
498498

499-
pub(crate) struct Reader {
500-
pub(crate) ptr: *const u8,
501-
pub(crate) off: usize,
502-
}
503-
504-
impl Reader {
505-
pub(crate) fn new<T>(ptr: *const T) -> Reader {
506-
Reader {
507-
ptr: ptr as *const u8,
508-
off: 0,
509-
}
510-
}
511-
512-
pub(crate) fn read_u8(&mut self) -> u8 {
513-
self.off += 1;
514-
unsafe { *self.ptr.add(self.off - 1) }
515-
}
516-
517-
pub(crate) fn read_u16(&mut self) -> u16 {
518-
self.read_u8() as u16 | (self.read_u8() as u16) << 8
519-
}
520-
521-
pub(crate) fn read_u32(&mut self) -> u32 {
522-
self.read_u16() as u32 | (self.read_u16() as u32) << 16
523-
}
524-
525-
pub(crate) fn current_address(&self) -> usize {
526-
unsafe { self.ptr.add(self.off) as usize }
527-
}
528-
}
529-
530499
/// A trait to abstract over all sized and unsized tags (DSTs). For sized tags,
531500
/// this trait does not much. For DSTs, a `TagTrait::dst_size` implementation
532501
/// must me provided, which returns the right size hint for the dynamically

0 commit comments

Comments
 (0)