Skip to content

Commit 403b93d

Browse files
authored
erase-region: Check if the address and size is multiple of 4096 and add HIL test (#771)
* erase-region: Check if the address and size is multiple of 4096 and add test * changelog * reviews * updated test
1 parent 7b6e5e1 commit 403b93d

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ Add list-ports command to list available serial ports.
1717
### Changed
1818
- Split the baudrate for connecting and monitorinig in `flash` subcommand (#737)
1919
- Normalized arguments of the CLI commands (#759)
20-
2120
- `board-info` now prints `Security information`. (#758)
2221

2322
### Fixed
2423

2524
- Update the app image SHA in the correct location for padded images (#715)
2625
- Fix `-s` argument collision (#731)
26+
- `address` and `size` in `erase-region` have to be multiples of 4096 (#771)
2727

2828
### Removed
2929

espflash/src/cli/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use crate::{
4545
FlashSize,
4646
Flasher,
4747
ProgressCallbacks,
48+
FLASH_SECTOR_SIZE,
4849
},
4950
targets::{Chip, XtalFrequency},
5051
};
@@ -716,6 +717,14 @@ pub fn erase_region(args: EraseRegionArgs, config: &Config) -> Result<()> {
716717
return Err(Error::StubRequired).into_diagnostic();
717718
}
718719

720+
if args.address % FLASH_SECTOR_SIZE as u32 != 0 || args.size % FLASH_SECTOR_SIZE as u32 != 0 {
721+
return Err(Error::InvalidEraseRegionArgument {
722+
address: args.address,
723+
size: args.size,
724+
})
725+
.into_diagnostic();
726+
}
727+
719728
let mut flasher = connect(&args.connect_args, config, true, true)?;
720729

721730
info!(

espflash/src/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ pub enum Error {
217217
#[error("Invalid response: {0}")]
218218
#[diagnostic(code(espflash::invalid_response))]
219219
InvalidResponse(String),
220+
221+
#[error("Invalid `address`({address}) and/or `size`({size}) argument(s)")]
222+
#[diagnostic(
223+
code(espflash::erase_region::invalid_argument),
224+
help("`address` and `size` must be multiples of 0x1000 (4096)")
225+
)]
226+
InvalidEraseRegionArgument { address: u32, size: u32 },
220227
}
221228

222229
#[cfg(feature = "serialport")]

espflash/tests/scripts/erase-region.sh

100644100755
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
#!/usr/bin/env bash
22

3+
# Function to check expected failure for unaligned erase arguments
4+
check_unaligned_erase() {
5+
local address=$1
6+
local size=$2
7+
result=$(espflash erase-region "$address" "$size" 2>&1)
8+
echo "$result"
9+
10+
if [[ $result =~ "Invalid `address`" ]]; then
11+
echo "Unaligned erase correctly rejected: address=$address, size=$size"
12+
else
13+
echo "Test failed: unaligned erase was not rejected!"
14+
exit 1
15+
fi
16+
}
17+
18+
# Unaligned address (not a multiple of 4096)
19+
check_unaligned_erase 0x1001 0x1000
20+
21+
# Unaligned size (not a multiple of 4096)
22+
check_unaligned_erase 0x1000 0x1001
23+
24+
# Both address and size unaligned
25+
check_unaligned_erase 0x1003 0x1005
26+
27+
# Valid erase - should succeed
328
result=$(espflash erase-region 0x1000 0x1000 2>&1)
429
echo "$result"
530
if [[ ! $result =~ "Erasing region at" ]]; then

0 commit comments

Comments
 (0)