File tree Expand file tree Collapse file tree 2 files changed +47
-2
lines changed Expand file tree Collapse file tree 2 files changed +47
-2
lines changed Original file line number Diff line number Diff line change 1
1
use core:: mem;
2
2
3
- use crate :: common;
3
+ use crate :: {
4
+ common,
5
+ fat:: { Error , Read } ,
6
+ mem:: MemoryRegion ,
7
+ } ;
4
8
5
9
// Common data needed for all boot paths
6
10
pub trait Info {
@@ -140,6 +144,26 @@ pub struct Header {
140
144
pub handover_offset : u32 ,
141
145
}
142
146
147
+ impl Header {
148
+ // Read a kernel header from the first two sectors of a file
149
+ pub fn from_file ( f : & mut dyn Read ) -> Result < Self , Error > {
150
+ let mut data: [ u8 ; 1024 ] = [ 0 ; 1024 ] ;
151
+ let mut region = MemoryRegion :: from_bytes ( & mut data) ;
152
+
153
+ f. seek ( 0 ) ?;
154
+ f. load_file ( & mut region) ?;
155
+
156
+ #[ repr( C ) ]
157
+ struct HeaderData {
158
+ before : [ u8 ; HEADER_START ] ,
159
+ hdr : Header ,
160
+ after : [ u8 ; 1024 - HEADER_END ] ,
161
+ }
162
+ // SAFETY: Struct consists entirely of primitive integral types.
163
+ Ok ( unsafe { mem:: transmute :: < _ , HeaderData > ( data) } . hdr )
164
+ }
165
+ }
166
+
143
167
// Right now the stucts below are unused, so we only need them to be the correct
144
168
// size. Update test_size_and_offset if a struct's real definition is added.
145
169
#[ derive( Clone , Copy ) ]
Original file line number Diff line number Diff line change 12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- use crate :: block:: SectorRead ;
15
+ use crate :: { block:: SectorRead , mem :: MemoryRegion } ;
16
16
17
17
#[ repr( packed) ]
18
18
struct Header {
@@ -244,6 +244,27 @@ pub trait Read {
244
244
fn read ( & mut self , data : & mut [ u8 ] ) -> Result < u32 , Error > ;
245
245
fn seek ( & mut self , offset : u32 ) -> Result < ( ) , Error > ;
246
246
fn get_size ( & self ) -> u32 ;
247
+
248
+ // Loads the remainer of the file into the specified memory region
249
+ fn load_file ( & mut self , mem : & mut MemoryRegion ) -> Result < ( ) , Error > {
250
+ let mut chunks = mem. as_bytes ( ) . chunks_exact_mut ( 512 ) ;
251
+ for chunk in chunks. by_ref ( ) {
252
+ self . read ( chunk) ?;
253
+ }
254
+ let last = chunks. into_remainder ( ) ;
255
+ if last. is_empty ( ) {
256
+ return Ok ( ( ) ) ;
257
+ }
258
+ // Use tmp buffer for last, partial sector
259
+ let mut dst: [ u8 ; 512 ] = [ 0 ; 512 ] ;
260
+ if let Some ( err) = self . read ( & mut dst) . err ( ) {
261
+ if err != Error :: EndOfFile {
262
+ return Err ( err) ;
263
+ }
264
+ }
265
+ last. copy_from_slice ( & dst[ ..last. len ( ) ] ) ;
266
+ Ok ( ( ) )
267
+ }
247
268
}
248
269
249
270
impl < ' a > Read for File < ' a > {
You can’t perform that action at this time.
0 commit comments