@@ -3,6 +3,7 @@ use crate::{
3
3
EntryAddressHeaderTag , EntryEfi32HeaderTag , EntryEfi64HeaderTag , FramebufferHeaderTag ,
4
4
HeaderTag , HeaderTagISA , HeaderTagType , InformationRequestHeaderTag , RelocatableHeaderTag ,
5
5
} ;
6
+ use core:: convert:: TryInto ;
6
7
use core:: fmt:: { Debug , Formatter } ;
7
8
use core:: mem:: size_of;
8
9
@@ -59,6 +60,28 @@ impl<'a> Multiboot2Header<'a> {
59
60
Self { inner : reference }
60
61
}
61
62
63
+ /// Find the header in a given slice.
64
+ pub fn find_header ( buffer : & [ u8 ] ) -> Option < ( & [ u8 ] , u32 ) > {
65
+ // the magic is 32 bit aligned and inside the first 8192 bytes
66
+ assert ! ( buffer. len( ) >= 8192 ) ;
67
+ let mut chunks = buffer[ 0 ..8192 ] . chunks_exact ( 4 ) ;
68
+ let magic_index = match chunks. position ( |vals| {
69
+ u32:: from_le_bytes ( vals. try_into ( ) . unwrap ( ) ) // yes, there's 4 bytes here
70
+ == MULTIBOOT2_HEADER_MAGIC
71
+ } ) {
72
+ Some ( idx) => idx * 4 ,
73
+ None => return None ,
74
+ } ;
75
+ chunks. next ( ) ; // arch
76
+ let header_length: usize = u32:: from_le_bytes ( chunks. next ( ) . unwrap ( ) . try_into ( ) . unwrap ( ) )
77
+ . try_into ( )
78
+ . unwrap ( ) ;
79
+ Some ( (
80
+ & buffer[ magic_index..magic_index + header_length] ,
81
+ magic_index as u32 ,
82
+ ) )
83
+ }
84
+
62
85
/// Wrapper around [`Multiboot2BasicHeader::verify_checksum`].
63
86
pub const fn verify_checksum ( & self ) -> bool {
64
87
self . inner . verify_checksum ( )
0 commit comments