From a4b761b0beb6bdec1601a936d066cbec295a74c7 Mon Sep 17 00:00:00 2001 From: William Vinnicombe Date: Thu, 17 Jul 2025 11:55:21 +0100 Subject: [PATCH 1/2] Fix rounding bug for load/save/verify small files Want to round up size/100, as a non-zero size should always give a non-zero chunk size --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 7b87753a..e7408c5b 100644 --- a/main.cpp +++ b/main.cpp @@ -339,7 +339,7 @@ template struct range_map { // Calculate chunk size for load/save/verify // Returns size/100 rounded up to FLASH_SECTOR_ERASE_SIZE uint32_t calculate_chunk_size(uint32_t size) { - return ((size/100 + FLASH_SECTOR_ERASE_SIZE - 1) & ~(FLASH_SECTOR_ERASE_SIZE - 1)); + return (((size + (100 - 1))/100 + FLASH_SECTOR_ERASE_SIZE - 1) & ~(FLASH_SECTOR_ERASE_SIZE - 1)); } From e3288dddd9574cd7365500377e5ca6ec4c88e751 Mon Sep 17 00:00:00 2001 From: William Vinnicombe Date: Thu, 17 Jul 2025 12:05:01 +0100 Subject: [PATCH 2/2] Allow zero fill when checking checksum Otherwise fails as unmapped for small files --- main.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index e7408c5b..b5a67fc9 100644 --- a/main.cpp +++ b/main.cpp @@ -1811,24 +1811,24 @@ struct memory_access { virtual uint32_t get_binary_start() = 0; - uint32_t read_int(uint32_t addr) { + uint32_t read_int(uint32_t addr, bool zero_fill = false) { assert(!(addr & 3u)); uint32_t rc; - read(addr, (uint8_t *)&rc, 4); + read(addr, (uint8_t *)&rc, 4, zero_fill); return rc; } - uint32_t read_short(uint32_t addr) { + uint32_t read_short(uint32_t addr, bool zero_fill = false) { assert(!(addr & 1u)); uint16_t rc; - read(addr, (uint8_t *)&rc, 2); + read(addr, (uint8_t *)&rc, 2, zero_fill); return rc; } // read a vector of types that have a raw_type_mapping - template void read_raw(uint32_t addr, T &v) { + template void read_raw(uint32_t addr, T &v, bool zero_fill = false) { typename raw_type_mapping::access_type& check = v; // ugly check that we aren't trying to read into something we shouldn't - read(addr, (uint8_t *)&v, sizeof(typename raw_type_mapping::access_type)); + read(addr, (uint8_t *)&v, sizeof(typename raw_type_mapping::access_type), zero_fill); } // read a vector of types that have a raw_type_mapping @@ -4008,8 +4008,8 @@ uint32_t get_access_family_id(memory_access &file_access) { // No block, so RP2040 or absolute if (file_access.get_binary_start() == FLASH_START) { vector checksum_data = {}; - file_access.read_into_vector(FLASH_START, 252, checksum_data); - uint32_t checksum = file_access.read_int(FLASH_START + 252); + file_access.read_into_vector(FLASH_START, 252, checksum_data, true); + uint32_t checksum = file_access.read_int(FLASH_START + 252, true); if (checksum == calc_checksum(checksum_data)) { // Checksum is correct, so RP2040 DEBUG_LOG("Detected family ID %s due to boot2 checksum\n", family_name(RP2040_FAMILY_ID).c_str());