From 2af166aca7765cff0225b197bb84eb6b75b4a303 Mon Sep 17 00:00:00 2001 From: Tyeth Gundry Date: Wed, 4 Jun 2025 19:35:51 +0100 Subject: [PATCH 1/2] Update nuke.c to auto-detect flash size Fixes https://github.com/raspberrypi/pico-examples/issues/642 Thanks to @Gadgetoid for the work "Making A Thing" - Support [PiMoRoNi by buying stuff](https://shop.pimoroni.com/)! --- flash/nuke/nuke.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/flash/nuke/nuke.c b/flash/nuke/nuke.c index 521f5d74f..5a3156b16 100644 --- a/flash/nuke/nuke.c +++ b/flash/nuke/nuke.c @@ -27,12 +27,11 @@ int main() { uint flash_size_bytes; -#ifndef PICO_FLASH_SIZE_BYTES -#warning PICO_FLASH_SIZE_BYTES not set, assuming 16M - flash_size_bytes = 16 * 1024 * 1024; -#else - flash_size_bytes = PICO_FLASH_SIZE_BYTES; -#endif + uint8_t txbuf[4]; + uint8_t rxbuf[4]; + txbuf[0] = 0x9f; + flash_do_cmd(txbuf, rxbuf, 4); + flash_size_bytes = 1u << rxbuf[3]; flash_range_erase(0, flash_size_bytes); // Leave an eyecatcher pattern in the first page of flash so picotool can // more easily check the size: From 9fb77fff1bd6f39ebe3ec8915abd77a3c02fe7d8 Mon Sep 17 00:00:00 2001 From: Peter Harper Date: Fri, 12 Dec 2025 13:17:11 +0000 Subject: [PATCH 2/2] 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 --- flash/nuke/CMakeLists.txt | 8 ++++++++ flash/nuke/nuke.c | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/flash/nuke/CMakeLists.txt b/flash/nuke/CMakeLists.txt index c8ae47a94..d9a7a8c72 100644 --- a/flash/nuke/CMakeLists.txt +++ b/flash/nuke/CMakeLists.txt @@ -7,6 +7,14 @@ target_link_libraries(flash_nuke hardware_flash ) +# We use flash command 0x9f to dynamically get the flash size +# If that fails, assume 16Mb +math(EXPR FLASH_SIZE_OVERRIDE "16 * 1024 * 1024" OUTPUT_FORMAT DECIMAL) +target_compile_definitions(flash_nuke PRIVATE + PICO_INCLUDE_FLASH_UTILS=1 + PICO_FLASH_SIZE_BYTES=${FLASH_SIZE_OVERRIDE} + ) + # It doesn't make sense to run this program from flash. Always build a # RAM-only binary. pico_set_binary_type(flash_nuke no_flash) diff --git a/flash/nuke/nuke.c b/flash/nuke/nuke.c index 5a3156b16..e44efbfcf 100644 --- a/flash/nuke/nuke.c +++ b/flash/nuke/nuke.c @@ -25,13 +25,18 @@ #include "hardware/flash.h" #include "pico/bootrom.h" +#define MIN_FLASH_SIZE (1024 * 1024 * 2) + int main() { - uint flash_size_bytes; + uint flash_size_bytes = 0; uint8_t txbuf[4]; uint8_t rxbuf[4]; txbuf[0] = 0x9f; flash_do_cmd(txbuf, rxbuf, 4); flash_size_bytes = 1u << rxbuf[3]; + if (flash_size_bytes < MIN_FLASH_SIZE || flash_size_bytes > PICO_FLASH_SIZE_BYTES) { + flash_size_bytes = PICO_FLASH_SIZE_BYTES; + } flash_range_erase(0, flash_size_bytes); // Leave an eyecatcher pattern in the first page of flash so picotool can // more easily check the size: