@@ -17,10 +17,9 @@ pub const MAGIC: u32 = 0xe85250d6;
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
19
/// please look at [`crate::builder::Multiboot2HeaderBuilder`].
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( Debug , Clone , PartialEq , Eq ) ]
@@ -223,11 +216,11 @@ impl Display for LoadError {
223
216
#[ cfg( feature = "unstable" ) ]
224
217
impl core:: error:: Error for LoadError { }
225
218
226
- /// **Use this only if you know what you do. You probably want to use
227
- /// [`Multiboot2Header`] instead.**
228
- ///
229
219
/// The "basic" Multiboot2 header. This means only the properties, that are known during
230
220
/// compile time. All other information are derived during runtime from the size property.
221
+ ///
222
+ /// **Use this only if you know what you do. You probably want to use
223
+ /// [`Multiboot2Header`] instead.**
231
224
#[ derive( Copy , Clone ) ]
232
225
#[ repr( C ) ]
233
226
pub struct Multiboot2BasicHeader {
@@ -236,8 +229,8 @@ pub struct Multiboot2BasicHeader {
236
229
arch : HeaderTagISA ,
237
230
length : u32 ,
238
231
checksum : u32 ,
239
- // additional tags. .
240
- // at minimum the end tag
232
+ // Followed by dynamic amount of dynamically sized header tags.
233
+ // At minimum, the end tag.
241
234
}
242
235
243
236
impl Multiboot2BasicHeader {
@@ -265,7 +258,6 @@ impl Multiboot2BasicHeader {
265
258
( 0x100000000 - magic as u64 - arch as u64 - length as u64 ) as u32
266
259
}
267
260
268
- /// Returns
269
261
pub const fn header_magic ( & self ) -> u32 {
270
262
self . header_magic
271
263
}
0 commit comments