@@ -11,22 +11,41 @@ use tokio_serial::SerialStream;
1111
1212use crate :: bootloader_serial:: { issue_command, Command , Response } ;
1313use crate :: errors:: { TockError , TockloaderError } ;
14+ use crate :: IOCommands ;
1415
1516/// This structure contains all relevant information about a tock application.
1617///
1718/// All data is stored either within [TbfHeader]s, or [TbfFooter]s.
1819///
1920/// See also <https://book.tockos.org/doc/tock_binary_format>
20- #[ derive( Debug ) ]
21+ #[ derive( Debug , Clone ) ]
2122pub struct AppAttributes {
23+ pub address : u64 ,
24+ pub size : u32 ,
25+ pub index : u8 ,
2226 pub tbf_header : TbfHeader ,
2327 pub tbf_footers : Vec < TbfFooter > ,
28+ pub installed : bool ,
29+ pub is_padding : bool ,
30+ }
31+
32+ impl std:: fmt:: Display for AppAttributes {
33+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
34+ write ! (
35+ f,
36+ "{}. {} - start: {:#x}, size: {}" ,
37+ self . index,
38+ self . tbf_header. get_package_name( ) . unwrap_or( "" ) ,
39+ self . address,
40+ self . size
41+ )
42+ }
2443}
2544
2645/// This structure represents a footer of a Tock application. Currently, footers
2746/// only contain credentials, which are used to verify the integrity of the
2847/// application.
29- #[ derive( Debug ) ]
48+ #[ derive( Debug , Clone ) ]
3049pub struct TbfFooter {
3150 pub credentials : TbfFooterV2Credentials ,
3251 pub size : u32 ,
@@ -41,10 +60,22 @@ impl TbfFooter {
4160// TODO(george-cosma): Could take advantages of the trait rework
4261
4362impl AppAttributes {
44- pub ( crate ) fn new ( header_data : TbfHeader , footers_data : Vec < TbfFooter > ) -> AppAttributes {
63+ pub ( crate ) fn new (
64+ address : u64 ,
65+ size : u32 ,
66+ index : u8 ,
67+ header_data : TbfHeader ,
68+ footers_data : Vec < TbfFooter > ,
69+ installed : bool ,
70+ ) -> AppAttributes {
4571 AppAttributes {
72+ address,
73+ size,
74+ index,
4675 tbf_header : header_data,
4776 tbf_footers : footers_data,
77+ installed,
78+ is_padding : false ,
4879 }
4980 }
5081
@@ -117,13 +148,10 @@ impl AppAttributes {
117148 // crash the process.
118149 let binary_end_offset = header. get_binary_end ( ) ;
119150
120- match & header {
121- TbfHeader :: TbfHeaderV2 ( _hd) => { }
122- _ => {
123- appaddr += total_size as u64 ;
124- continue ;
125- }
126- } ;
151+ if !header. is_app ( ) {
152+ appaddr += total_size as u64 ;
153+ continue ;
154+ }
127155
128156 let mut footers: Vec < TbfFooter > = vec ! [ ] ;
129157 let total_footers_size = total_size - binary_end_offset;
@@ -137,9 +165,9 @@ impl AppAttributes {
137165 // binary_end_offset`) , even if we overread.
138166 let mut appfooter =
139167 vec ! [ 0u8 ; ( total_footers_size - ( footer_offset - binary_end_offset) ) as usize ] ;
140-
168+ // log::info!("footer init {:?}", appfooter);
141169 board_core. read ( appaddr + footer_offset as u64 , & mut appfooter) ?;
142-
170+ // log::info!("footer read {:?}", appfooter);
143171 let footer_info =
144172 parse_tbf_footer ( & appfooter) . map_err ( TockError :: InvalidAppTbfHeader ) ?;
145173
@@ -150,9 +178,10 @@ impl AppAttributes {
150178 footer_offset += footer_info. 1 + 4 ;
151179 }
152180
153- let details: AppAttributes = AppAttributes :: new ( header, footers) ;
181+ let details: AppAttributes =
182+ AppAttributes :: new ( appaddr, total_size, apps_counter, header, footers, true ) ;
154183
155- apps_details. insert ( apps_counter, details) ;
184+ apps_details. insert ( apps_counter. into ( ) , details) ;
156185 apps_counter += 1 ;
157186 appaddr += total_size as u64 ;
158187 }
@@ -240,15 +269,13 @@ impl AppAttributes {
240269 log:: debug!( "App #{apps_counter}: Header data: {header_data:?}" ) ;
241270 let header = parse_tbf_header ( & header_data, tbf_version)
242271 . map_err ( TockError :: InvalidAppTbfHeader ) ?;
272+
243273 let binary_end_offset = header. get_binary_end ( ) ;
244274
245- match & header {
246- TbfHeader :: TbfHeaderV2 ( _hd) => { }
247- _ => {
248- appaddr += total_size as u64 ;
249- continue ;
250- }
251- } ;
275+ if !header. is_app ( ) {
276+ appaddr += total_size as u64 ;
277+ continue ;
278+ }
252279
253280 let mut footers: Vec < TbfFooter > = vec ! [ ] ;
254281 let total_footers_size = total_size - binary_end_offset;
@@ -289,12 +316,17 @@ impl AppAttributes {
289316 footer_offset += footer_info. 1 + 4 ;
290317 }
291318
292- let details: AppAttributes = AppAttributes :: new ( header, footers) ;
319+ let details: AppAttributes =
320+ AppAttributes :: new ( appaddr, total_size, apps_counter, header, footers, true ) ;
293321
294- apps_details. insert ( apps_counter, details) ;
322+ apps_details. insert ( apps_counter. into ( ) , details) ;
295323 apps_counter += 1 ;
296324 appaddr += total_size as u64 ;
297325 }
298326 Ok ( apps_details)
299327 }
328+
329+ pub async fn read ( & mut self , conn : & mut dyn IOCommands ) -> Result < Vec < u8 > , TockloaderError > {
330+ conn. read ( self . address , self . size as usize ) . await
331+ }
300332}
0 commit comments