@@ -23,12 +23,10 @@ pub struct Multiboot2Header<'a> {
2323}
2424
2525impl < ' a > Multiboot2Header < ' a > {
26- /// Public constructor for this type with various validations. It panics if the address is invalid.
27- /// It panics rather than returning a result, because if this fails, it is
28- /// a fatal, unrecoverable error anyways and a bug in your code.
26+ /// Public constructor for this type with various validations.
2927 ///
30- /// # Panics
31- /// Panics if one of the following conditions is true :
28+ /// If the header is invalid, it returns a [`LoadError`].
29+ /// This may be because :
3230 /// - `addr` is a null-pointer
3331 /// - `addr` isn't 8-byte aligned
3432 /// - the magic value of the header is not present
@@ -37,38 +35,29 @@ impl<'a> Multiboot2Header<'a> {
3735 /// # Safety
3836 /// This function may produce undefined behaviour, if the provided `addr` is not a valid
3937 /// Multiboot2 header pointer.
40- pub unsafe fn from_addr ( addr : usize ) -> Self {
41- assert_ne ! ( 0 , addr, "`addr` is null pointer" ) ;
42- assert_eq ! (
43- addr % 8 ,
44- 0 ,
45- "`addr` must be 8-byte aligned, see Multiboot2 spec"
46- ) ;
38+ pub const unsafe fn from_addr ( addr : usize ) -> Result < Self , LoadError > {
39+ if addr == 0 || addr % 8 != 0 {
40+ return Err ( LoadError :: InvalidAddress ) ;
41+ }
4742 let ptr = addr as * const Multiboot2BasicHeader ;
4843 let reference = & * ptr;
49- assert_eq ! (
50- reference. header_magic( ) ,
51- MULTIBOOT2_HEADER_MAGIC ,
52- "The Multiboot2 header must contain the MULTIBOOT2_HEADER_MAGIC={:x}" ,
53- MULTIBOOT2_HEADER_MAGIC
54- ) ;
55- assert ! (
56- reference. verify_checksum( ) ,
57- "checksum invalid! Is {:x}, expected {:x}" ,
58- reference. checksum( ) ,
59- Self :: calc_checksum( reference. header_magic, reference. arch, reference. length)
60- ) ;
61- Self { inner : reference }
44+ if reference. header_magic ( ) != MULTIBOOT2_HEADER_MAGIC {
45+ return Err ( LoadError :: MagicNotFound ) ;
46+ }
47+ if !reference. verify_checksum ( ) {
48+ return Err ( LoadError :: ChecksumMismatch ) ;
49+ }
50+ Ok ( Self { inner : reference } )
6251 }
6352
6453 /// Find the header in a given slice.
6554 ///
6655 /// If it succeeds, it returns a tuple consisting of the subslice containing
6756 /// just the header and the index of the header in the given slice.
6857 /// If it fails (either because the header is not properply 64-bit aligned
69- /// or because it is truncated), it returns a [`BufError `].
58+ /// or because it is truncated), it returns a [`LoadError `].
7059 /// If there is no header, it returns `None`.
71- pub fn find_header ( buffer : & [ u8 ] ) -> Result < Option < ( & [ u8 ] , u32 ) > , BufError > {
60+ pub fn find_header ( buffer : & [ u8 ] ) -> Result < Option < ( & [ u8 ] , u32 ) > , LoadError > {
7261 // the magic is 32 bit aligned and inside the first 8192 bytes
7362 assert ! ( buffer. len( ) >= 8192 ) ;
7463 let mut windows = buffer[ 0 ..8192 ] . windows ( 4 ) ;
@@ -80,7 +69,7 @@ impl<'a> Multiboot2Header<'a> {
8069 if idx % 8 == 0 {
8170 idx
8271 } else {
83- return Err ( BufError :: Unaligned ) ;
72+ return Err ( LoadError :: InvalidAddress ) ;
8473 }
8574 }
8675 None => return Ok ( None ) ,
@@ -97,7 +86,7 @@ impl<'a> Multiboot2Header<'a> {
9786 let header_length: usize = u32:: from_le_bytes (
9887 windows
9988 . next ( )
100- . ok_or ( BufError :: TooSmall ) ?
89+ . ok_or ( LoadError :: TooSmall ) ?
10190 . try_into ( )
10291 . unwrap ( ) , // 4 bytes are a u32
10392 )
@@ -210,11 +199,15 @@ impl<'a> Debug for Multiboot2Header<'a> {
210199/// Errors that can occur when parsing a header from a slice.
211200/// See [`Multiboot2Header::find_header`].
212201#[ derive( Debug ) ]
213- pub enum BufError {
214- /// The header in the given slice is truncated.
202+ pub enum LoadError {
203+ /// The checksum does not match the data.
204+ ChecksumMismatch ,
205+ /// The header is not properly 64-bit aligned (or a null pointer).
206+ InvalidAddress ,
207+ /// The header does not contain the correct magic number.
208+ MagicNotFound ,
209+ /// The header is truncated.
215210 TooSmall ,
216- /// The header in the given slice is not properly 64-bit aligned.
217- Unaligned ,
218211}
219212
220213/// **Use this only if you know what you do. You probably want to use
0 commit comments