mirror of
https://github.com/espressif/esp-idf.git
synced 2025-09-25 01:37:22 +00:00
spi_flash: Fix over-allocation and OOM crash when reading from SPI flash to PSRAM buffers
Previously would try allocate buffer of minimum size 16KB not maximum size 16KB, causing out of memory errors for any large reads, or if less than 16KB contiguous free heap. Also, if using legacy API and internal allocation failed then implementation would abort() instead of returning the error to the caller. Added test for using large buffers in PSRAM. Closes https://github.com/espressif/esp-idf/issues/4769 Also reported on forum: https://esp32.com/viewtopic.php?f=13&t=14304&p=55972
This commit is contained in:
@@ -498,11 +498,13 @@ esp_err_t IRAM_ATTR esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t add
|
||||
uint8_t* temp_buffer = NULL;
|
||||
|
||||
if (!direct_read) {
|
||||
uint32_t length_to_allocate = MAX(MAX_READ_CHUNK, length);
|
||||
uint32_t length_to_allocate = MIN(MAX_READ_CHUNK, length);
|
||||
length_to_allocate = (length_to_allocate+3)&(~3);
|
||||
temp_buffer = heap_caps_malloc(length_to_allocate, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||
ESP_LOGV(TAG, "allocate temp buffer: %p", temp_buffer);
|
||||
if (temp_buffer == NULL) return ESP_ERR_NO_MEM;
|
||||
ESP_LOGV(TAG, "allocate temp buffer: %p (%d)", temp_buffer, length_to_allocate);
|
||||
if (temp_buffer == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t err = ESP_OK;
|
||||
@@ -682,27 +684,32 @@ esp_err_t esp_flash_app_disable_protect(bool disable)
|
||||
|
||||
#ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
|
||||
|
||||
/* Translate any ESP_ERR_FLASH_xxx error code (new API) to a generic ESP_ERR_xyz error code
|
||||
*/
|
||||
static IRAM_ATTR esp_err_t spi_flash_translate_rc(esp_err_t err)
|
||||
{
|
||||
switch (err) {
|
||||
case ESP_OK:
|
||||
return ESP_OK;
|
||||
case ESP_ERR_INVALID_ARG:
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
case ESP_ERR_NO_MEM:
|
||||
return err;
|
||||
|
||||
case ESP_ERR_FLASH_NOT_INITIALISED:
|
||||
case ESP_ERR_FLASH_PROTECTED:
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
|
||||
case ESP_ERR_NOT_FOUND:
|
||||
case ESP_ERR_FLASH_UNSUPPORTED_HOST:
|
||||
case ESP_ERR_FLASH_UNSUPPORTED_CHIP:
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
|
||||
case ESP_ERR_FLASH_NO_RESPONSE:
|
||||
return ESP_ERR_INVALID_RESPONSE;
|
||||
|
||||
default:
|
||||
ESP_EARLY_LOGE(TAG, "unexpected spi flash error code: %x", err);
|
||||
ESP_EARLY_LOGE(TAG, "unexpected spi flash error code: 0x%x", err);
|
||||
abort();
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t IRAM_ATTR spi_flash_erase_range(uint32_t start_addr, uint32_t size)
|
||||
|
Reference in New Issue
Block a user