@@ -16,11 +16,10 @@ pub const MAGIC: u32 = 0xe85250d6;
16
16
/// by all tags (see [`crate::tags::HeaderTagType`]).
17
17
/// Use this if you get a pointer to the header and just want
18
18
/// 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 ) ]
20
21
#[ repr( transparent) ]
21
- pub struct Multiboot2Header < ' a > {
22
- inner : & ' a Multiboot2BasicHeader ,
23
- }
22
+ pub struct Multiboot2Header < ' a > ( & ' a Multiboot2BasicHeader ) ;
24
23
25
24
impl < ' a > Multiboot2Header < ' a > {
26
25
/// Public constructor for this type with various validations.
@@ -35,21 +34,23 @@ impl<'a> Multiboot2Header<'a> {
35
34
/// # Safety
36
35
/// This function may produce undefined behaviour, if the provided `addr` is not a valid
37
36
/// 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 {
42
40
return Err ( LoadError :: InvalidAddress ) ;
43
41
}
44
- let ptr = addr as * const Multiboot2BasicHeader ;
42
+
45
43
let reference = & * ptr;
44
+
46
45
if reference. header_magic ( ) != MAGIC {
47
46
return Err ( LoadError :: MagicNotFound ) ;
48
47
}
48
+
49
49
if !reference. verify_checksum ( ) {
50
50
return Err ( LoadError :: ChecksumMismatch ) ;
51
51
}
52
- Ok ( Self { inner : reference } )
52
+
53
+ Ok ( Self ( reference) )
53
54
}
54
55
55
56
/// Find the header in a given slice.
@@ -61,7 +62,7 @@ impl<'a> Multiboot2Header<'a> {
61
62
/// If there is no header, it returns `None`.
62
63
pub fn find_header ( buffer : & [ u8 ] ) -> Result < Option < ( & [ u8 ] , u32 ) > , LoadError > {
63
64
if buffer. as_ptr ( ) . align_offset ( 4 ) != 0 {
64
- return Err ( LoadError :: InvalidAddress )
65
+ return Err ( LoadError :: InvalidAddress ) ;
65
66
}
66
67
67
68
let mut windows = buffer[ 0 ..8192 ] . windows ( 4 ) ;
@@ -104,27 +105,27 @@ impl<'a> Multiboot2Header<'a> {
104
105
105
106
/// Wrapper around [`Multiboot2BasicHeader::verify_checksum`].
106
107
pub const fn verify_checksum ( & self ) -> bool {
107
- self . inner . verify_checksum ( )
108
+ self . 0 . verify_checksum ( )
108
109
}
109
110
/// Wrapper around [`Multiboot2BasicHeader::header_magic`].
110
111
pub const fn header_magic ( & self ) -> u32 {
111
- self . inner . header_magic ( )
112
+ self . 0 . header_magic ( )
112
113
}
113
114
/// Wrapper around [`Multiboot2BasicHeader::arch`].
114
115
pub const fn arch ( & self ) -> HeaderTagISA {
115
- self . inner . arch ( )
116
+ self . 0 . arch ( )
116
117
}
117
118
/// Wrapper around [`Multiboot2BasicHeader::length`].
118
119
pub const fn length ( & self ) -> u32 {
119
- self . inner . length ( )
120
+ self . 0 . length ( )
120
121
}
121
122
/// Wrapper around [`Multiboot2BasicHeader::checksum`].
122
123
pub const fn checksum ( & self ) -> u32 {
123
- self . inner . checksum ( )
124
+ self . 0 . checksum ( )
124
125
}
125
126
/// Wrapper around [`Multiboot2BasicHeader::tag_iter`].
126
127
pub fn iter ( & self ) -> Multiboot2HeaderTagIter {
127
- self . inner . tag_iter ( )
128
+ self . 0 . tag_iter ( )
128
129
}
129
130
/// Wrapper around [`Multiboot2BasicHeader::calc_checksum`].
130
131
pub const fn calc_checksum ( magic : u32 , arch : HeaderTagISA , length : u32 ) -> u32 {
@@ -192,14 +193,6 @@ impl<'a> Multiboot2Header<'a> {
192
193
}
193
194
}
194
195
195
- impl < ' a > Debug for Multiboot2Header < ' a > {
196
- fn fmt ( & self , f : & mut Formatter < ' _ > ) -> core:: fmt:: Result {
197
- // For debug fmt we only output the inner field
198
- let reference = unsafe { & * ( self . inner as * const Multiboot2BasicHeader ) } ;
199
- Debug :: fmt ( reference, f)
200
- }
201
- }
202
-
203
196
/// Errors that can occur when parsing a header from a slice.
204
197
/// See [`Multiboot2Header::find_header`].
205
198
#[ derive( Copy , Clone , Debug , derive_more:: Display , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
@@ -217,9 +210,6 @@ pub enum LoadError {
217
210
#[ cfg( feature = "unstable" ) ]
218
211
impl core:: error:: Error for LoadError { }
219
212
220
- /// **Use this only if you know what you do. You probably want to use
221
- /// [`Multiboot2Header`] instead.**
222
- ///
223
213
/// The "basic" Multiboot2 header. This means only the properties, that are known during
224
214
/// compile time. All other information are derived during runtime from the size property.
225
215
#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
@@ -230,8 +220,8 @@ pub struct Multiboot2BasicHeader {
230
220
arch : HeaderTagISA ,
231
221
length : u32 ,
232
222
checksum : u32 ,
233
- // additional tags. .
234
- // at minimum the end tag
223
+ // Followed by dynamic amount of dynamically sized header tags.
224
+ // At minimum, the end tag.
235
225
}
236
226
237
227
impl Multiboot2BasicHeader {
@@ -259,7 +249,6 @@ impl Multiboot2BasicHeader {
259
249
( 0x100000000 - magic as u64 - arch as u64 - length as u64 ) as u32
260
250
}
261
251
262
- /// Returns
263
252
pub const fn header_magic ( & self ) -> u32 {
264
253
self . header_magic
265
254
}
0 commit comments