33#[ cfg( feature = "builder" ) ]
44use crate :: builder:: AsBytes ;
55use crate :: framebuffer:: UnknownFramebufferType ;
6- use crate :: tag:: TagIter ;
6+ use crate :: tag:: { TagHeader , TagIter } ;
77use crate :: {
88 module, BasicMemoryInfoTag , BootLoaderNameTag , CommandLineTag , EFIBootServicesNotExitedTag ,
99 EFIImageHandle32Tag , EFIImageHandle64Tag , EFIMemoryMapTag , EFISdt32Tag , EFISdt64Tag ,
1010 ElfSectionIter , ElfSectionsTag , EndTag , FramebufferTag , ImageLoadPhysAddrTag , MemoryMapTag ,
1111 ModuleIter , RsdpV1Tag , RsdpV2Tag , SmbiosTag , TagTrait , VBEInfoTag ,
1212} ;
1313use core:: fmt;
14+ use core:: mem;
15+ use core:: ptr;
1416use derive_more:: Display ;
1517
1618/// Error type that describes errors while loading/parsing a multiboot2 information structure
@@ -39,19 +41,25 @@ impl core::error::Error for MbiLoadError {}
3941#[ repr( C ) ]
4042pub struct BootInformationHeader {
4143 // size is multiple of 8
42- pub total_size : u32 ,
44+ total_size : u32 ,
4345 _reserved : u32 ,
4446 // Followed by the boot information tags.
4547}
4648
47- #[ cfg( feature = "builder" ) ]
4849impl BootInformationHeader {
50+ #[ cfg( feature = "builder" ) ]
4951 pub ( crate ) const fn new ( total_size : u32 ) -> Self {
5052 Self {
5153 total_size,
5254 _reserved : 0 ,
5355 }
5456 }
57+
58+ /// Returns the total size of the structure.
59+ #[ must_use]
60+ pub const fn total_size ( & self ) -> u32 {
61+ self . total_size
62+ }
5563}
5664
5765#[ cfg( feature = "builder" ) ]
@@ -70,18 +78,18 @@ impl BootInformationInner {
7078 /// Checks if the MBI has a valid end tag by checking the end of the mbi's
7179 /// bytes.
7280 fn has_valid_end_tag ( & self ) -> bool {
73- let end_tag_prototype = EndTag :: default ( ) ;
74-
75- let self_ptr = unsafe { self . tags . as_ptr ( ) . sub ( size_of :: < BootInformationHeader > ( ) ) } ;
81+ let self_ptr = ptr:: addr_of!( * self ) ;
7682
7783 let end_tag_ptr = unsafe {
7884 self_ptr
85+ . cast :: < u8 > ( )
7986 . add ( self . header . total_size as usize )
80- . sub ( size_of :: < EndTag > ( ) )
87+ . sub ( mem:: size_of :: < EndTag > ( ) )
88+ . cast :: < TagHeader > ( )
8189 } ;
82- let end_tag = unsafe { & * ( end_tag_ptr as * const EndTag ) } ;
90+ let end_tag = unsafe { & * end_tag_ptr } ;
8391
84- end_tag. typ == end_tag_prototype . typ && end_tag. size == end_tag_prototype . size
92+ end_tag. typ == EndTag :: ID && end_tag. size as usize == mem :: size_of :: < EndTag > ( )
8593 }
8694}
8795
@@ -127,7 +135,7 @@ impl<'a> BootInformation<'a> {
127135 return Err ( MbiLoadError :: IllegalTotalSize ( mbi. total_size ) ) ;
128136 }
129137
130- let slice_size = mbi. total_size as usize - size_of :: < BootInformationHeader > ( ) ;
138+ let slice_size = mbi. total_size as usize - mem :: size_of :: < BootInformationHeader > ( ) ;
131139 // mbi: reference to full mbi
132140 let mbi = ptr_meta:: from_raw_parts :: < BootInformationInner > ( ptr. cast ( ) , slice_size) ;
133141 let mbi = & * mbi;
0 commit comments