Skip to content

Commit

Permalink
flash_encryption: Quick fixed the issue that block when flash_encrypt…
Browse files Browse the repository at this point in the history
…ion_write, Related #6322, Related #6254
  • Loading branch information
mythbuster5 committed Feb 5, 2021
1 parent 2b5cbcf commit c1b41ec
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions components/esp_rom/include/esp32/rom/spi_flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ extern "C" {

//Extra dummy for flash read
#define ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_20M 0
#define ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_26M 0
#define ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_40M 1
#define ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_80M 2

Expand Down
61 changes: 61 additions & 0 deletions components/spi_flash/flash_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,70 @@ esp_err_t IRAM_ATTR spi_flash_write(size_t dst, const void *srcv, size_t size)
}
#endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL

#if !CONFIG_SPI_FLASH_USE_LEGACY_IMPL
extern void spi_common_set_dummy_output(esp_rom_spiflash_read_mode_t mode);
extern void spi_dummy_len_fix(uint8_t spi, uint8_t freqdiv);
static void IRAM_ATTR flash_rom_init(void)
{
uint32_t freqdiv = 0;
esp_rom_spiflash_read_mode_t read_mode;

#if CONFIG_IDF_TARGET_ESP32
uint32_t dummy_bit = 0;
#if CONFIG_ESPTOOLPY_FLASHFREQ_80M
dummy_bit = ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_80M;
#elif CONFIG_ESPTOOLPY_FLASHFREQ_40M
dummy_bit = ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_40M;
#elif CONFIG_ESPTOOLPY_FLASHFREQ_26M
dummy_bit = ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_26M;
#elif CONFIG_ESPTOOLPY_FLASHFREQ_20M
dummy_bit = ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_20M;
#endif
#endif//CONFIG_IDF_TARGET_ESP32

#if CONFIG_ESPTOOLPY_FLASHFREQ_80M
freqdiv = 1;
#elif CONFIG_ESPTOOLPY_FLASHFREQ_40M
freqdiv = 2;
#elif CONFIG_ESPTOOLPY_FLASHFREQ_26M
freqdiv = 3;
#elif CONFIG_ESPTOOLPY_FLASHFREQ_20M
freqdiv = 4;
#endif

#if CONFIG_ESPTOOLPY_FLASHMODE_QIO
read_mode = ESP_ROM_SPIFLASH_QIO_MODE;
#elif CONFIG_ESPTOOLPY_FLASHMODE_QOUT
read_mode = ESP_ROM_SPIFLASH_QOUT_MODE;
#elif CONFIG_ESPTOOLPY_FLASHMODE_DIO
read_mode = ESP_ROM_SPIFLASH_DIO_MODE;
#elif CONFIG_ESPTOOLPY_FLASHMODE_DOUT
read_mode = ESP_ROM_SPIFLASH_DOUT_MODE;
#endif

#if CONFIG_IDF_TARGET_ESP32
g_rom_spiflash_dummy_len_plus[1] = dummy_bit;
#else
spi_dummy_len_fix(1, freqdiv);
#endif //CONFIG_IDF_TARGET_ESP32

#if !CONFIG_IDF_TARGET_ESP32S2 && !CONFIG_IDF_TARGET_ESP32
spi_common_set_dummy_output(read_mode);
#endif //!CONFIG_IDF_TARGET_ESP32S2
esp_rom_spiflash_config_readmode(read_mode);
esp_rom_spiflash_config_clk(freqdiv, 1);
}
#else
static void IRAM_ATTR flash_rom_init(void)
{
return;
}
#endif // !CONFIG_SPI_FLASH_USE_LEGACY_IMPL

esp_err_t IRAM_ATTR spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t size)
{
esp_err_t err = ESP_OK;
flash_rom_init();
const spi_flash_guard_funcs_t *guard = spi_flash_guard_get();
CHECK_WRITE_ADDRESS(dest_addr, size);
if ((dest_addr % 16) != 0) {
Expand Down Expand Up @@ -757,6 +817,7 @@ esp_err_t IRAM_ATTR spi_flash_read(size_t src, void *dstv, size_t size)

esp_err_t IRAM_ATTR spi_flash_read_encrypted(size_t src, void *dstv, size_t size)
{
flash_rom_init();
if (src + size > g_rom_flashchip.chip_size) {
return ESP_ERR_INVALID_SIZE;
}
Expand Down
8 changes: 8 additions & 0 deletions tools/ci/config/target-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,14 @@ UT_020:
- Example_SPI_Multi_device
- psram

UT_021:
extends: .unit_test_32_template
parallel: 2
tags:
- ESP32_IDF
- psram
- UT_T1_FlashEncryption

UT_022:
extends: .unit_test_32_template
tags:
Expand Down
14 changes: 14 additions & 0 deletions tools/unit-test-app/configs/flash_encryption_psram
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This config is for ESP32 only (no ESP32-S2/S3 flash encryption support yet, ESP32-C3 has no psram)
CONFIG_IDF_TARGET="esp32"
TEST_COMPONENTS=spi_flash
TEST_GROUPS=flash_encryption
CONFIG_SECURE_FLASH_ENC_ENABLED=y
CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT=y
CONFIG_SECURE_BOOT_ALLOW_ROM_BASIC=y
CONFIG_SECURE_BOOT_ALLOW_JTAG=y
CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_ENC=y
CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_DEC=y
CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE=y
CONFIG_SECURE_FLASH_REQUIRE_ALREADY_ENABLED=y
CONFIG_ESP32_SPIRAM_SUPPORT=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y

0 comments on commit c1b41ec

Please sign in to comment.