11use std:: {
22 fs:: { self , File } ,
33 io:: Read ,
4- num:: ParseIntError ,
54 path:: PathBuf ,
65} ;
76
87use clap:: { Args , CommandFactory , Parser , Subcommand } ;
98use espflash:: {
109 cli:: {
11- self , board_info, completions, config:: Config , connect, erase_partitions, flash_elf_image,
12- monitor:: monitor, parse_partition_table, partition_table, print_board_info,
13- save_elf_as_image, serial_monitor, CompletionsArgs , ConnectArgs , EspflashProgress ,
14- FlashConfigArgs , MonitorArgs , PartitionTableArgs ,
10+ self , board_info, completions, config:: Config , connect, erase_flash, erase_partitions,
11+ erase_region, flash_elf_image, monitor:: monitor, parse_partition_table, parse_uint32,
12+ partition_table, print_board_info, save_elf_as_image, serial_monitor, CompletionsArgs ,
13+ ConnectArgs , EraseFlashArgs , EraseRegionArgs , EspflashProgress , FlashConfigArgs ,
14+ MonitorArgs , PartitionTableArgs ,
1515 } ,
16+ error:: Error ,
1617 image_format:: ImageFormatKind ,
1718 logging:: initialize_logger,
1819 targets:: Chip ,
1920 update:: check_for_update,
2021} ;
21- use log:: { debug, LevelFilter } ;
22+ use log:: { debug, info , LevelFilter } ;
2223use miette:: { IntoDiagnostic , Result , WrapErr } ;
2324
2425#[ derive( Debug , Parser ) ]
@@ -42,6 +43,12 @@ enum Commands {
4243 /// depending on which shell is being used; consult your shell's
4344 /// documentation to determine the appropriate path.
4445 Completions ( CompletionsArgs ) ,
46+ /// Erase Flash entirely
47+ EraseFlash ( EraseFlashArgs ) ,
48+ /// Erase specified partitions
49+ EraseParts ( ErasePartsArgs ) ,
50+ /// Erase specified region
51+ EraseRegion ( EraseRegionArgs ) ,
4552 /// Flash an application in ELF format to a connected target device
4653 ///
4754 /// Given a path to an ELF file, first convert it into the appropriate
@@ -78,6 +85,22 @@ enum Commands {
7885 WriteBin ( WriteBinArgs ) ,
7986}
8087
88+ /// Erase named partitions based on provided partition table
89+ #[ derive( Debug , Args ) ]
90+ pub struct ErasePartsArgs {
91+ /// Connection configuration
92+ #[ clap( flatten) ]
93+ pub connect_args : ConnectArgs ,
94+
95+ /// Labels of the partitions to be erased
96+ #[ arg( value_name = "LABELS" , value_delimiter = ',' ) ]
97+ pub erase_parts : Vec < String > ,
98+
99+ /// Input partition table
100+ #[ arg( long, value_name = "FILE" ) ]
101+ pub partition_table : Option < PathBuf > ,
102+ }
103+
81104#[ derive( Debug , Args ) ]
82105struct FlashArgs {
83106 /// Connection configuration
@@ -121,11 +144,6 @@ struct WriteBinArgs {
121144 connect_args : ConnectArgs ,
122145}
123146
124- /// Parses a string as a 32-bit unsigned integer.
125- fn parse_uint32 ( input : & str ) -> Result < u32 , ParseIntError > {
126- parse_int:: parse ( input)
127- }
128-
129147fn main ( ) -> Result < ( ) > {
130148 miette:: set_panic_hook ( ) ;
131149 initialize_logger ( LevelFilter :: Info ) ;
@@ -148,6 +166,9 @@ fn main() -> Result<()> {
148166 match args {
149167 Commands :: BoardInfo ( args) => board_info ( & args, & config) ,
150168 Commands :: Completions ( args) => completions ( & args, & mut Cli :: command ( ) , "espflash" ) ,
169+ Commands :: EraseFlash ( args) => erase_flash ( args, & config) ,
170+ Commands :: EraseParts ( args) => erase_parts ( args, & config) ,
171+ Commands :: EraseRegion ( args) => erase_region ( args, & config) ,
151172 Commands :: Flash ( args) => flash ( args, & config) ,
152173 Commands :: Monitor ( args) => serial_monitor ( args, & config) ,
153174 Commands :: PartitionTable ( args) => partition_table ( args) ,
@@ -156,6 +177,24 @@ fn main() -> Result<()> {
156177 }
157178}
158179
180+ pub fn erase_parts ( args : ErasePartsArgs , config : & Config ) -> Result < ( ) > {
181+ if args. connect_args . no_stub {
182+ return Err ( Error :: StubRequiredToEraseFlash ) . into_diagnostic ( ) ;
183+ }
184+
185+ let mut flash = connect ( & args. connect_args , config) ?;
186+ let partition_table = match args. partition_table {
187+ Some ( path) => Some ( parse_partition_table ( & path) ?) ,
188+ None => None ,
189+ } ;
190+
191+ info ! ( "Erasing the following partitions: {:?}" , args. erase_parts) ;
192+ erase_partitions ( & mut flash, partition_table, Some ( args. erase_parts ) , None ) ?;
193+ flash. connection ( ) . reset ( ) ?;
194+
195+ Ok ( ( ) )
196+ }
197+
159198fn flash ( args : FlashArgs , config : & Config ) -> Result < ( ) > {
160199 let mut flasher = connect ( & args. connect_args , config) ?;
161200
0 commit comments