Skip to content

Commit 8b7c130

Browse files
committed
feat: erase_apps
Signed-off-by: addrian-77 <lunguadrian30@gmail.com>
1 parent 65e43ff commit 8b7c130

6 files changed

Lines changed: 65 additions & 2 deletions

File tree

tockloader-cli/src/cli.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ fn get_subcommands() -> Vec<Command> {
5353
.args(get_app_args())
5454
.args(get_channel_args())
5555
.arg_required_else_help(false),
56+
Command::new("erase-apps")
57+
.about("Erase apps")
58+
.args(get_app_args())
59+
.args(get_channel_args())
60+
.arg_required_else_help(false),
5661
]
5762
}
5863

tockloader-cli/src/main.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use tockloader_lib::connection::{
1818
use tockloader_lib::known_boards::KnownBoard;
1919
use tockloader_lib::tabs::tab::Tab;
2020
use tockloader_lib::{
21-
list_debug_probes, list_serial_ports, CommandInfo, CommandInstall, CommandList,
21+
list_debug_probes, list_serial_ports, CommandEraseApps, CommandInfo, CommandInstall,
22+
CommandList,
2223
};
2324

2425
fn get_serial_target_info(user_options: &ArgMatches) -> SerialTargetInfo {
@@ -228,6 +229,15 @@ async fn main() -> Result<()> {
228229
.await
229230
.context("Failed to install app.")?;
230231
}
232+
Some(("erase-apps", sub_matches)) => {
233+
cli::validate(&mut cmd, sub_matches);
234+
let mut conn = open_connection(sub_matches).await?;
235+
let settings = get_board_settings(sub_matches);
236+
237+
conn.erase_apps(&settings)
238+
.await
239+
.context("Failed to erase apps.")?;
240+
}
231241
_ => {
232242
println!("Could not run the provided subcommand.");
233243
_ = make_cli().print_help();

tockloader-lib/src/command_impl/generalized.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::board_settings::BoardSettings;
66
use crate::connection::TockloaderConnection;
77
use crate::errors::TockloaderError;
88
use crate::tabs::tab::Tab;
9-
use crate::{CommandInfo, CommandInstall, CommandList};
9+
use crate::{CommandEraseApps, CommandInfo, CommandInstall, CommandList};
1010

1111
#[async_trait]
1212
impl CommandList for TockloaderConnection {
@@ -47,3 +47,13 @@ impl CommandInstall for TockloaderConnection {
4747
}
4848
}
4949
}
50+
51+
#[async_trait]
52+
impl CommandEraseApps for TockloaderConnection {
53+
async fn erase_apps(&mut self, settings: &BoardSettings) -> Result<(), TockloaderError> {
54+
match self {
55+
TockloaderConnection::ProbeRS(conn) => conn.erase_apps(settings).await,
56+
TockloaderConnection::Serial(_conn) => todo!(),
57+
}
58+
}
59+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use async_trait::async_trait;
2+
use probe_rs::flashing::DownloadOptions;
3+
4+
use crate::board_settings::BoardSettings;
5+
use crate::connection::{Connection, ProbeRSConnection};
6+
use crate::errors::{InternalError, TockloaderError};
7+
use crate::CommandEraseApps;
8+
9+
#[async_trait]
10+
impl CommandEraseApps for ProbeRSConnection {
11+
async fn erase_apps(&mut self, settings: &BoardSettings) -> Result<(), TockloaderError> {
12+
if !self.is_open() {
13+
return Err(InternalError::ConnectionNotOpen.into());
14+
}
15+
let session = self.session.as_mut().expect("Board must be open");
16+
17+
let mut loader = session.target().flash_loader();
18+
19+
let address = settings.start_address;
20+
// A single 0x0 byte is enough to invalidate the tbf header and make it all programs
21+
// unreadable to tockloader. This does mean app information will still exist on the board,
22+
// but they will be overwritten when the space is needed.
23+
loader.add_data((address as u32).into(), &[0x0])?;
24+
25+
let mut options = DownloadOptions::default();
26+
options.keep_unwritten_bytes = true;
27+
28+
// Finally, the data can be programmed
29+
loader.commit(session, options)?;
30+
Ok(())
31+
}
32+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod erase_apps;
12
pub mod info;
23
pub mod install;
34
pub mod list;

tockloader-lib/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,8 @@ pub trait CommandInstall {
5959
tab_file: Tab,
6060
) -> Result<(), TockloaderError>;
6161
}
62+
63+
#[async_trait]
64+
pub trait CommandEraseApps {
65+
async fn erase_apps(&mut self, settings: &BoardSettings) -> Result<(), TockloaderError>;
66+
}

0 commit comments

Comments
 (0)