Skip to content

Commit 9fb77ff

Browse files
committed
Add some validation of flash size
If flash_do_cmd returns a bad size for the flash or fails, use a default flash size of 16MB
1 parent 2af166a commit 9fb77ff

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

flash/nuke/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ target_link_libraries(flash_nuke
77
hardware_flash
88
)
99

10+
# We use flash command 0x9f to dynamically get the flash size
11+
# If that fails, assume 16Mb
12+
math(EXPR FLASH_SIZE_OVERRIDE "16 * 1024 * 1024" OUTPUT_FORMAT DECIMAL)
13+
target_compile_definitions(flash_nuke PRIVATE
14+
PICO_INCLUDE_FLASH_UTILS=1
15+
PICO_FLASH_SIZE_BYTES=${FLASH_SIZE_OVERRIDE}
16+
)
17+
1018
# It doesn't make sense to run this program from flash. Always build a
1119
# RAM-only binary.
1220
pico_set_binary_type(flash_nuke no_flash)

flash/nuke/nuke.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@
2525
#include "hardware/flash.h"
2626
#include "pico/bootrom.h"
2727

28+
#define MIN_FLASH_SIZE (1024 * 1024 * 2)
29+
2830
int main() {
29-
uint flash_size_bytes;
31+
uint flash_size_bytes = 0;
3032
uint8_t txbuf[4];
3133
uint8_t rxbuf[4];
3234
txbuf[0] = 0x9f;
3335
flash_do_cmd(txbuf, rxbuf, 4);
3436
flash_size_bytes = 1u << rxbuf[3];
37+
if (flash_size_bytes < MIN_FLASH_SIZE || flash_size_bytes > PICO_FLASH_SIZE_BYTES) {
38+
flash_size_bytes = PICO_FLASH_SIZE_BYTES;
39+
}
3540
flash_range_erase(0, flash_size_bytes);
3641
// Leave an eyecatcher pattern in the first page of flash so picotool can
3742
// more easily check the size:

0 commit comments

Comments
 (0)