Skip to content

Commit

Permalink
fix(rom): Fix s2 and s3 Cache_Count_Flash_Pages rom function wrapper
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EliteTK committed Jan 22, 2025
1 parent 0f0068f commit c29d8b9
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions components/esp_rom/patches/esp_rom_cache_esp32s2_esp32s3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c29d8b9

Please sign in to comment.