1
1
use std:: {
2
2
fs:: { self , File } ,
3
3
io:: Read ,
4
- num:: ParseIntError ,
5
4
path:: PathBuf ,
6
5
} ;
7
6
8
7
use clap:: { Args , CommandFactory , Parser , Subcommand } ;
9
8
use espflash:: {
10
9
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 ,
15
15
} ,
16
+ error:: Error ,
16
17
image_format:: ImageFormatKind ,
17
18
logging:: initialize_logger,
18
19
targets:: Chip ,
19
20
update:: check_for_update,
20
21
} ;
21
- use log:: { debug, LevelFilter } ;
22
+ use log:: { debug, info , LevelFilter } ;
22
23
use miette:: { IntoDiagnostic , Result , WrapErr } ;
23
24
24
25
#[ derive( Debug , Parser ) ]
@@ -42,6 +43,12 @@ enum Commands {
42
43
/// depending on which shell is being used; consult your shell's
43
44
/// documentation to determine the appropriate path.
44
45
Completions ( CompletionsArgs ) ,
46
+ /// Erase Flash entirely
47
+ EraseFlash ( EraseFlashArgs ) ,
48
+ /// Erase specified partitions
49
+ EraseParts ( ErasePartsArgs ) ,
50
+ /// Erase specified region
51
+ EraseRegion ( EraseRegionArgs ) ,
45
52
/// Flash an application in ELF format to a connected target device
46
53
///
47
54
/// Given a path to an ELF file, first convert it into the appropriate
@@ -78,6 +85,22 @@ enum Commands {
78
85
WriteBin ( WriteBinArgs ) ,
79
86
}
80
87
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
+
81
104
#[ derive( Debug , Args ) ]
82
105
struct FlashArgs {
83
106
/// Connection configuration
@@ -121,11 +144,6 @@ struct WriteBinArgs {
121
144
connect_args : ConnectArgs ,
122
145
}
123
146
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
-
129
147
fn main ( ) -> Result < ( ) > {
130
148
miette:: set_panic_hook ( ) ;
131
149
initialize_logger ( LevelFilter :: Info ) ;
@@ -148,6 +166,9 @@ fn main() -> Result<()> {
148
166
match args {
149
167
Commands :: BoardInfo ( args) => board_info ( & args, & config) ,
150
168
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) ,
151
172
Commands :: Flash ( args) => flash ( args, & config) ,
152
173
Commands :: Monitor ( args) => serial_monitor ( args, & config) ,
153
174
Commands :: PartitionTable ( args) => partition_table ( args) ,
@@ -156,6 +177,24 @@ fn main() -> Result<()> {
156
177
}
157
178
}
158
179
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
+
159
198
fn flash ( args : FlashArgs , config : & Config ) -> Result < ( ) > {
160
199
let mut flasher = connect ( & args. connect_args , config) ?;
161
200
0 commit comments