Skip to content

Commit 2fe9c76

Browse files
committed
multiboot2: remove weird offset thingy
1 parent 2d7c4ad commit 2fe9c76

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

multiboot2/src/elf_sections.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl ElfSectionsTag {
4141
}
4242

4343
/// Get an iterator of loaded ELF sections.
44-
pub(crate) fn sections(&self, offset: usize) -> ElfSectionIter {
44+
pub(crate) fn sections(&self) -> ElfSectionIter {
4545
let string_section_offset = (self.shndx * self.entry_size) as isize;
4646
let string_section_ptr =
4747
unsafe { self.first_section().offset(string_section_offset) as *const _ };
@@ -50,7 +50,6 @@ impl ElfSectionsTag {
5050
remaining_sections: self.number_of_sections,
5151
entry_size: self.entry_size,
5252
string_section: string_section_ptr,
53-
offset,
5453
}
5554
}
5655

@@ -93,7 +92,6 @@ pub struct ElfSectionIter {
9392
remaining_sections: u32,
9493
entry_size: u32,
9594
string_section: *const u8,
96-
offset: usize,
9795
}
9896

9997
impl Iterator for ElfSectionIter {
@@ -105,7 +103,6 @@ impl Iterator for ElfSectionIter {
105103
inner: self.current_section,
106104
string_section: self.string_section,
107105
entry_size: self.entry_size,
108-
offset: self.offset,
109106
};
110107

111108
self.current_section = unsafe { self.current_section.offset(self.entry_size as isize) };
@@ -136,7 +133,6 @@ impl Default for ElfSectionIter {
136133
remaining_sections: 0,
137134
entry_size: 0,
138135
string_section: core::ptr::null(),
139-
offset: 0,
140136
}
141137
}
142138
}
@@ -147,7 +143,6 @@ pub struct ElfSection {
147143
inner: *const u8,
148144
string_section: *const u8,
149145
entry_size: u32,
150-
offset: usize,
151146
}
152147

153148
#[derive(Clone, Copy, Debug)]
@@ -282,7 +277,7 @@ impl ElfSection {
282277
64 => (*(self.string_section as *const ElfSectionInner64)).addr as usize,
283278
s => panic!("Unexpected entry size: {}", s),
284279
};
285-
(addr + self.offset) as *const _
280+
addr as *const _
286281
}
287282
}
288283

multiboot2/src/lib.rs

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ pub unsafe fn load(address: usize) -> Result<BootInformation, MbiLoadError> {
144144
/// memory is not (identity) mapped (UEFI does only identity mapping).
145145
/// * The memory at `address` must not be modified after calling `load` or the
146146
/// program may observe unsynchronized mutation.
147+
#[deprecated("Please use load() instead.")]
147148
pub unsafe fn load_with_offset(
148149
address: usize,
149150
offset: usize,
@@ -193,16 +194,40 @@ pub enum MbiLoadError {
193194
impl core::error::Error for MbiLoadError {}
194195

195196
/// A Multiboot 2 Boot Information struct.
196-
pub struct BootInformation {
197-
inner: *const BootInformationInner,
198-
offset: usize,
197+
#[derive(Debug)]
198+
#[repr(transparent)]
199+
pub struct BootInformation<'a>(&'a BootInformationInner<'a>);
200+
201+
impl<'a> BootInformation {
202+
/// Loads the [`BootInformation`] from a pointer..
203+
pub unsafe fn load(ptr: *const ()) -> Result<Self, MbiLoadError> {
204+
if ptr.is_null() {
205+
return Err(MbiLoadError::IllegalAddress);
206+
}
207+
208+
// not aligned
209+
if ptr.align_offset(8) != 0 {
210+
return Err(MbiLoadError::IllegalAddress);
211+
}
212+
213+
let mbi = &*ptr.cast::<BootInformationInner>();
214+
215+
// Check if total size is a multiple of 8.
216+
// See MbiLoadError::IllegalTotalSize for comments
217+
if mbi.total_size & 0b111 != 0 {
218+
return Err(MbiLoadError::IllegalTotalSize(multiboot.total_size));
219+
}
220+
221+
Ok(Self(mbi))
222+
}
199223
}
200224

201225
#[derive(Clone, Copy)]
202-
#[repr(C)]
203-
struct BootInformationInner {
226+
#[repr(C), align(8)]
227+
struct BootInformationInner<'a> {
204228
total_size: u32,
205229
_reserved: u32,
230+
// followed by multiboot2 tags ...
206231
}
207232

208233
impl BootInformationInner {
@@ -565,7 +590,7 @@ pub trait TagTrait: Pointee {
565590
// All sized tags automatically have a Pointee implementation where
566591
// Pointee::Metadata is (). Hence, the TagTrait is implemented automatically for
567592
// all tags that are sized.
568-
impl<T: Pointee<Metadata = ()>> TagTrait for T {
593+
impl<T: Pointee<Metadata=()>> TagTrait for T {
569594
#[allow(clippy::unused_unit)]
570595
fn dst_size(_: &Tag) -> Self::Metadata {
571596
()
@@ -737,16 +762,16 @@ mod tests {
737762
FramebufferType::RGB {
738763
red: FramebufferField {
739764
position: 16,
740-
size: 8
765+
size: 8,
741766
},
742767
green: FramebufferField {
743768
position: 8,
744-
size: 8
769+
size: 8,
745770
},
746771
blue: FramebufferField {
747772
position: 0,
748-
size: 8
749-
}
773+
size: 8,
774+
},
750775
}
751776
);
752777
}
@@ -798,22 +823,22 @@ mod tests {
798823
FramebufferColor {
799824
red: 255,
800825
green: 0,
801-
blue: 0
826+
blue: 0,
802827
},
803828
FramebufferColor {
804829
red: 0,
805830
green: 255,
806-
blue: 0
831+
blue: 0,
807832
},
808833
FramebufferColor {
809834
red: 0,
810835
green: 0,
811-
blue: 255
836+
blue: 255,
812837
},
813838
FramebufferColor {
814839
red: 0,
815840
green: 0,
816-
blue: 0
841+
blue: 0,
817842
}
818843
]
819844
),
@@ -946,28 +971,28 @@ mod tests {
946971
vbe.mode_info.red_field,
947972
VBEField {
948973
position: 16,
949-
size: 8
974+
size: 8,
950975
}
951976
);
952977
assert_eq!(
953978
vbe.mode_info.green_field,
954979
VBEField {
955980
position: 8,
956-
size: 8
981+
size: 8,
957982
}
958983
);
959984
assert_eq!(
960985
vbe.mode_info.blue_field,
961986
VBEField {
962987
position: 0,
963-
size: 8
988+
size: 8,
964989
}
965990
);
966991
assert_eq!(
967992
vbe.mode_info.reserved_field,
968993
VBEField {
969994
position: 24,
970-
size: 8
995+
size: 8,
971996
}
972997
);
973998
assert_eq!(

0 commit comments

Comments
 (0)