From f2ec2c891dc1aaf4a067dd44945ec0e3e3be0ff9 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Wed, 22 Jan 2025 18:09:41 +0000 Subject: [PATCH] Fix s2 and s3 Cache_Count_Flash_Pages rom function wrapper The rom function on the s2 and s3 only counts one page for any pages which are mapped to page 0 of flash as the Cache_Flash_To_SPIRAM_Copy function attempts to map all flash page 0 mapped pages to one PSRAM page. As this function can be called for multiple regions, it needs to track if a page mapped to page 0 has previously been accounted for by a previous call. It does this using the page0_mapped in-out parameter. This logic contains an error: ``` if (*page0_mapped == 0) { // BUG: If page0_count is 0, 1 is still added count = valid_flash_count + 1 - page0_count; } else { count = valid_flash_count - page0_count; } *page0_mapped += page0_count; return count; ``` The current Cache_Count_Flash_Pages wrapper in the idf attempts to compensate for this bug by checking if the page0_mapped parameter was changed by a call to the function and reducing the count if it has not. This, however, will incorrectly over-compensate in situations where the initial value of page0_mapped was not zero as the code above only miscounts when it was zero. This patch addresses the issue in this wrapper function by correctly compensating for the bug only in cases where the final page0_mapped value is 0. --- components/esp_rom/patches/esp_rom_cache_esp32s2_esp32s3.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/components/esp_rom/patches/esp_rom_cache_esp32s2_esp32s3.c b/components/esp_rom/patches/esp_rom_cache_esp32s2_esp32s3.c index 9866d652a82a..34e4bd11dde6 100644 --- a/components/esp_rom/patches/esp_rom_cache_esp32s2_esp32s3.c +++ b/components/esp_rom/patches/esp_rom_cache_esp32s2_esp32s3.c @@ -21,14 +21,13 @@ extern uint32_t rom_Cache_Count_Flash_Pages(uint32_t bus, uint32_t * page0_mapped); uint32_t Cache_Count_Flash_Pages(uint32_t bus, uint32_t * page0_mapped) { - uint32_t page0_before_count = *page0_mapped; uint32_t flash_pages = 0; flash_pages = rom_Cache_Count_Flash_Pages(bus, page0_mapped); -/* No page mapped to page0, in this condition, the rom api will return +/* No page mapped to page0 yet, in this condition, the rom api will return * unexpected value + 1. */ - if (page0_before_count == *page0_mapped) { + if (*page0_mapped == 0) { flash_pages--; } return flash_pages;