File tree Expand file tree Collapse file tree 2 files changed +44
-2
lines changed Expand file tree Collapse file tree 2 files changed +44
-2
lines changed Original file line number Diff line number Diff line change 11use core:: mem;
22
3- use crate :: common;
3+ use crate :: {
4+ common,
5+ fat:: { Error , Read } ,
6+ mem:: MemoryRegion ,
7+ } ;
48
59// Common data needed for all boot paths
610pub trait Info {
@@ -143,6 +147,26 @@ pub struct Header {
143147 pub handover_offset : u32 ,
144148}
145149
150+ impl Header {
151+ // Read a kernel header from the first two sectors of a file
152+ pub fn from_file ( f : & mut dyn Read ) -> Result < Self , Error > {
153+ let mut data: [ u8 ; 1024 ] = [ 0 ; 1024 ] ;
154+ let mut region = MemoryRegion :: from_bytes ( & mut data) ;
155+
156+ f. seek ( 0 ) ?;
157+ f. load_file ( & mut region) ?;
158+
159+ #[ repr( C ) ]
160+ struct HeaderData {
161+ before : [ u8 ; HEADER_START ] ,
162+ hdr : Header ,
163+ after : [ u8 ; 1024 - HEADER_END ] ,
164+ }
165+ // SAFETY: Struct consists entirely of primitive integral types.
166+ Ok ( unsafe { mem:: transmute :: < _ , HeaderData > ( data) } . hdr )
167+ }
168+ }
169+
146170// Right now the stucts below are unused, so we only need them to be the correct
147171// size. Update test_size_and_offset if a struct's real definition is added.
148172#[ derive( Clone , Copy ) ]
Original file line number Diff line number Diff line change 1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- use crate :: block:: SectorRead ;
15+ use crate :: { block:: SectorRead , mem :: MemoryRegion } ;
1616
1717#[ repr( packed) ]
1818struct Header {
@@ -244,6 +244,24 @@ pub trait Read {
244244 fn read ( & mut self , data : & mut [ u8 ] ) -> Result < u32 , Error > ;
245245 fn seek ( & mut self , offset : u32 ) -> Result < ( ) , Error > ;
246246 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 = [ 0 ; 512 ] ;
260+ let bytes = self . read ( & mut dst) ? as usize ;
261+ assert_eq ! ( bytes, last. len( ) ) ;
262+ last. copy_from_slice ( & dst[ ..bytes] ) ;
263+ Ok ( ( ) )
264+ }
247265}
248266
249267impl < ' a > Read for File < ' a > {
You can’t perform that action at this time.
0 commit comments