@@ -3,6 +3,7 @@ use crate::{
33 EntryAddressHeaderTag , EntryEfi32HeaderTag , EntryEfi64HeaderTag , FramebufferHeaderTag ,
44 HeaderTag , HeaderTagISA , HeaderTagType , InformationRequestHeaderTag , RelocatableHeaderTag ,
55} ;
6+ use core:: convert:: TryInto ;
67use core:: fmt:: { Debug , Formatter } ;
78use core:: mem:: size_of;
89
@@ -59,6 +60,28 @@ impl<'a> Multiboot2Header<'a> {
5960 Self { inner : reference }
6061 }
6162
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+
6285 /// Wrapper around [`Multiboot2BasicHeader::verify_checksum`].
6386 pub const fn verify_checksum ( & self ) -> bool {
6487 self . inner . verify_checksum ( )
0 commit comments