spi_flash: add config option to override flash size in bootloader header

Sometimes the flash size read from bootloader is not correct. This may
forbid SPI Flash driver from reading the the area larger than the size
in bootloader header.

When the new config option is enabled, the latest configured
ESPTOOLPY_FLAHSIZE in the app header will be used to override the value
read from bootloader header.
This commit is contained in:
Michael (XIAO Xufeng) 2020-08-24 11:09:33 +08:00
parent 7987f7ccae
commit e65ed8c959
6 changed files with 63 additions and 5 deletions

View File

@ -27,6 +27,14 @@ extern "C" {
*/ */
void bootloader_flash_update_id(); void bootloader_flash_update_id();
/**
* @brief Update the flash size in g_rom_flashchip (global esp_rom_spiflash_chip_t structure).
*
* @param size The size to store, in bytes.
* @return None
*/
void bootloader_flash_update_size(uint32_t size);
/** /**
* @brief Set the flash CS setup and hold time. * @brief Set the flash CS setup and hold time.
* *

View File

@ -123,6 +123,14 @@ esp_err_t esp_image_verify_bootloader(uint32_t *length);
*/ */
esp_err_t esp_image_verify_bootloader_data(esp_image_metadata_t *data); esp_err_t esp_image_verify_bootloader_data(esp_image_metadata_t *data);
/**
* @brief Get the flash size of the image
*
* @param app_flash_size The value configured in the image header
* @return Actual size, in bytes.
*/
int esp_image_get_flash_size(esp_image_flash_size_t app_flash_size);
typedef struct { typedef struct {
uint32_t drom_addr; uint32_t drom_addr;

View File

@ -32,6 +32,11 @@ void bootloader_flash_update_id()
g_rom_flashchip.device_id = bootloader_read_flash_id(); g_rom_flashchip.device_id = bootloader_read_flash_id();
} }
void bootloader_flash_update_size(uint32_t size)
{
g_rom_flashchip.chip_size = size;
}
void IRAM_ATTR bootloader_flash_cs_timing_config() void IRAM_ATTR bootloader_flash_cs_timing_config()
{ {
SET_PERI_REG_MASK(SPI_USER_REG(0), SPI_CS_HOLD_M | SPI_CS_SETUP_M); SET_PERI_REG_MASK(SPI_USER_REG(0), SPI_CS_HOLD_M | SPI_CS_SETUP_M);

View File

@ -655,3 +655,21 @@ static void debug_log_hash(const uint8_t *image_hash, const char *label)
ESP_LOGD(TAG, "%s: %s", label, hash_print); ESP_LOGD(TAG, "%s: %s", label, hash_print);
#endif #endif
} }
int esp_image_get_flash_size(esp_image_flash_size_t app_flash_size)
{
switch (app_flash_size) {
case ESP_IMAGE_FLASH_SIZE_1MB:
return 1 * 1024 * 1024;
case ESP_IMAGE_FLASH_SIZE_2MB:
return 2 * 1024 * 1024;
case ESP_IMAGE_FLASH_SIZE_4MB:
return 4 * 1024 * 1024;
case ESP_IMAGE_FLASH_SIZE_8MB:
return 8 * 1024 * 1024;
case ESP_IMAGE_FLASH_SIZE_16MB:
return 16 * 1024 * 1024;
default:
return 0;
}
}

View File

@ -407,6 +407,22 @@ void start_cpu0_default(void)
#ifndef CONFIG_FREERTOS_UNICORE #ifndef CONFIG_FREERTOS_UNICORE
esp_dport_access_int_init(); esp_dport_access_int_init();
#endif #endif
// Read the application binary image header. This will also decrypt the header if the image is encrypted.
esp_image_header_t fhdr = {0};
// This assumes that DROM is the first segment in the application binary, i.e. that we can read
// the binary header through cache by accessing SOC_DROM_LOW address.
memcpy(&fhdr, (void*) SOC_DROM_LOW, sizeof(fhdr));
#if CONFIG_SPI_FLASH_SIZE_OVERRIDE
int app_flash_size = esp_image_get_flash_size(fhdr.spi_size);
if (app_flash_size < 1 * 1024 * 1024) {
ESP_LOGE(TAG, "Invalid flash size in app image header.");
abort();
}
bootloader_flash_update_size(app_flash_size);
#endif //CONFIG_SPI_FLASH_SIZE_OVERRIDE
spi_flash_init(); spi_flash_init();
/* init default OS-aware flash access critical section */ /* init default OS-aware flash access critical section */
spi_flash_guard_set(&g_flash_guard_default_ops); spi_flash_guard_set(&g_flash_guard_default_ops);
@ -443,11 +459,6 @@ void start_cpu0_default(void)
bootloader_flash_update_id(); bootloader_flash_update_id();
#if !CONFIG_SPIRAM_BOOT_INIT #if !CONFIG_SPIRAM_BOOT_INIT
// Read the application binary image header. This will also decrypt the header if the image is encrypted.
esp_image_header_t fhdr = {0};
// This assumes that DROM is the first segment in the application binary, i.e. that we can read
// the binary header through cache by accessing SOC_DROM_LOW address.
memcpy(&fhdr, (void*) SOC_DROM_LOW, sizeof(fhdr));
// If psram is uninitialized, we need to improve some flash configuration. // If psram is uninitialized, we need to improve some flash configuration.
bootloader_flash_clock_config(&fhdr); bootloader_flash_clock_config(&fhdr);
bootloader_flash_gpio_config(&fhdr); bootloader_flash_gpio_config(&fhdr);

View File

@ -114,6 +114,14 @@ menu "SPI Flash driver"
help help
Defines how many ticks will be before returning to continue a erasing. Defines how many ticks will be before returning to continue a erasing.
config SPI_FLASH_SIZE_OVERRIDE
bool "Override flash size in bootloader header by ESPTOOLPY_FLASHSIZE"
default n
help
SPI Flash driver uses the flash size configured in bootloader header by default.
Enable this option to override flash size with latest ESPTOOLPY_FLASHSIZE value from
the app header if the size in the bootloader header is incorrect.
menu "Auto-detect flash chips" menu "Auto-detect flash chips"
config SPI_FLASH_SUPPORT_ISSI_CHIP config SPI_FLASH_SUPPORT_ISSI_CHIP