1+ use async_trait:: async_trait;
2+ use probe_rs:: flashing:: DownloadOptions ;
3+ use probe_rs:: { MemoryInterface } ;
4+ use tbf_parser:: parse:: { parse_tbf_header_lengths} ;
5+
6+ use crate :: board_settings:: BoardSettings ;
7+ use crate :: connection:: { Connection , ProbeRSConnection } ;
8+ use crate :: errors:: { InternalError , TockloaderError } ;
9+ use crate :: CommandEraseApps ;
10+
11+ #[ async_trait]
12+ impl CommandEraseApps for ProbeRSConnection {
13+ async fn erase_apps (
14+ & mut self ,
15+ settings : & BoardSettings ,
16+ ) -> Result < ( ) , TockloaderError > {
17+ if !self . is_open ( ) {
18+ return Err ( InternalError :: ConnectionNotOpen . into ( ) ) ;
19+ }
20+ let session = self . session . as_mut ( ) . expect ( "Board must be open" ) ;
21+
22+ let mut appaddr: u64 = settings. start_address ;
23+ // All applications are stored sequentially in memory, so we read until
24+ // we fail to parse.
25+ let mut total_size: u32 = 0 ;
26+ loop {
27+ let mut board_core = session. core ( self . target_info . core ) ?;
28+ let mut appdata = vec ! [ 0u8 ; 8 ] ;
29+
30+ board_core. read ( appaddr, & mut appdata) ?;
31+
32+ let app_size: u32 ;
33+
34+ // The first 8 bytes of the application data contain the TBF header
35+ // lengths and version.
36+ //
37+ // Note on expect: `read` always fills up the entire buffer, which
38+ // was previously declared as 8 bytes.
39+ log:: info!( "app data? {:?}" , appdata) ;
40+ match parse_tbf_header_lengths (
41+ & appdata
42+ . try_into ( )
43+ . expect ( "Buffer length must be at least 8 bytes long." ) ,
44+ ) {
45+ Ok ( data) => {
46+ app_size = data. 2 ;
47+ }
48+ _ => break ,
49+ } ;
50+
51+ appaddr += app_size as u64 ;
52+ total_size += app_size;
53+ }
54+
55+ let mut loader = session. target ( ) . flash_loader ( ) ;
56+
57+ let address = settings. start_address ;
58+ // (adi): fill the memory with total_size values of 0xFF
59+ let buffer = [ 0x0 ] . repeat ( total_size as usize ) ;
60+ loader. add_data (
61+ ( address as u32 ) . into ( ) ,
62+ & buffer
63+ ) ?;
64+
65+ let mut options = DownloadOptions :: default ( ) ;
66+ options. keep_unwritten_bytes = true ;
67+
68+ // Finally, the data can be programmed
69+ loader. commit ( session, options) ?;
70+ Ok ( ( ) )
71+ }
72+ }
0 commit comments