3
3
#[ cfg( feature = "builder" ) ]
4
4
use crate :: builder:: AsBytes ;
5
5
use crate :: framebuffer:: UnknownFramebufferType ;
6
- use crate :: tag:: TagIter ;
6
+ use crate :: tag:: { TagHeader , TagIter } ;
7
7
use crate :: {
8
8
module, BasicMemoryInfoTag , BootLoaderNameTag , CommandLineTag , EFIBootServicesNotExitedTag ,
9
9
EFIImageHandle32Tag , EFIImageHandle64Tag , EFIMemoryMapTag , EFISdt32Tag , EFISdt64Tag ,
10
10
ElfSectionIter , ElfSectionsTag , EndTag , FramebufferTag , ImageLoadPhysAddrTag , MemoryMapTag ,
11
11
ModuleIter , RsdpV1Tag , RsdpV2Tag , SmbiosTag , TagTrait , VBEInfoTag ,
12
12
} ;
13
13
use core:: fmt;
14
+ use core:: mem;
15
+ use core:: ptr;
14
16
use derive_more:: Display ;
15
17
16
18
/// Error type that describes errors while loading/parsing a multiboot2 information structure
@@ -39,19 +41,25 @@ impl core::error::Error for MbiLoadError {}
39
41
#[ repr( C ) ]
40
42
pub struct BootInformationHeader {
41
43
// size is multiple of 8
42
- pub total_size : u32 ,
44
+ total_size : u32 ,
43
45
_reserved : u32 ,
44
46
// Followed by the boot information tags.
45
47
}
46
48
47
- #[ cfg( feature = "builder" ) ]
48
49
impl BootInformationHeader {
50
+ #[ cfg( feature = "builder" ) ]
49
51
pub ( crate ) const fn new ( total_size : u32 ) -> Self {
50
52
Self {
51
53
total_size,
52
54
_reserved : 0 ,
53
55
}
54
56
}
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
+ }
55
63
}
56
64
57
65
#[ cfg( feature = "builder" ) ]
@@ -70,18 +78,18 @@ impl BootInformationInner {
70
78
/// Checks if the MBI has a valid end tag by checking the end of the mbi's
71
79
/// bytes.
72
80
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 ) ;
76
82
77
83
let end_tag_ptr = unsafe {
78
84
self_ptr
85
+ . cast :: < u8 > ( )
79
86
. add ( self . header . total_size as usize )
80
- . sub ( size_of :: < EndTag > ( ) )
87
+ . sub ( mem:: size_of :: < EndTag > ( ) )
88
+ . cast :: < TagHeader > ( )
81
89
} ;
82
- let end_tag = unsafe { & * ( end_tag_ptr as * const EndTag ) } ;
90
+ let end_tag = unsafe { & * end_tag_ptr } ;
83
91
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 > ( )
85
93
}
86
94
}
87
95
@@ -127,7 +135,7 @@ impl<'a> BootInformation<'a> {
127
135
return Err ( MbiLoadError :: IllegalTotalSize ( mbi. total_size ) ) ;
128
136
}
129
137
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 > ( ) ;
131
139
// mbi: reference to full mbi
132
140
let mbi = ptr_meta:: from_raw_parts :: < BootInformationInner > ( ptr. cast ( ) , slice_size) ;
133
141
let mbi = & * mbi;
0 commit comments