From bb0a95b17c45a6b648c30aece6f58b23a95949a5 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Thu, 16 Apr 2020 21:32:13 +1000 Subject: [PATCH] spi_flash s2: Fix encrypted writes when legacy implementation disabled ROM function didn't use correct Addr bitlen if legacy was disabled on ESP32-S2 --- .../spi_flash/esp32s2/flash_ops_esp32s2.c | 19 +++++++++++++------ .../main/flash_encrypt_main.c | 6 ++++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/components/spi_flash/esp32s2/flash_ops_esp32s2.c b/components/spi_flash/esp32s2/flash_ops_esp32s2.c index 604f30d86..6cbe9b4fa 100644 --- a/components/spi_flash/esp32s2/flash_ops_esp32s2.c +++ b/components/spi_flash/esp32s2/flash_ops_esp32s2.c @@ -22,6 +22,12 @@ #include "esp32s2/rom/cache.h" #include "hal/spi_flash_hal.h" #include "esp_flash.h" +#include "esp_log.h" + +static const char *TAG = "spiflash_s2"; + +#define SPICACHE SPIMEM0 +#define SPIFLASH SPIMEM1 esp_rom_spiflash_result_t IRAM_ATTR spi_flash_write_encrypted_chip(size_t dest_addr, const void *src, size_t size) { @@ -51,10 +57,13 @@ esp_rom_spiflash_result_t IRAM_ATTR spi_flash_write_encrypted_chip(size_t dest_a return ESP_ROM_SPIFLASH_RESULT_OK; } else { // Already in internal memory - rc = esp_rom_spiflash_unlock(); - if (rc != ESP_ROM_SPIFLASH_RESULT_OK) { - return rc; - } + ESP_LOGV(TAG, "calling SPI_Encrypt_Write addr 0x%x src %p size 0x%x", dest_addr, src, size); + +#ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL + /* The ROM function SPI_Encrypt_Write assumes ADDR_BITLEN is already set but new + implementation doesn't automatically set this to a usable value */ + SPIFLASH.user1.usr_addr_bitlen = 23; +#endif if (ops && ops->start) { ops->start(); @@ -67,8 +76,6 @@ esp_rom_spiflash_result_t IRAM_ATTR spi_flash_write_encrypted_chip(size_t dest_a } } -#define SPICACHE SPIMEM0 -#define SPIFLASH SPIMEM1 #define FLASH_WRAP_CMD 0x77 esp_err_t spi_flash_wrap_set(spi_flash_wrap_mode_t mode) { diff --git a/examples/security/flash_encryption/main/flash_encrypt_main.c b/examples/security/flash_encryption/main/flash_encrypt_main.c index b26c185c0..ffad2225e 100644 --- a/examples/security/flash_encryption/main/flash_encrypt_main.c +++ b/examples/security/flash_encryption/main/flash_encrypt_main.c @@ -24,9 +24,11 @@ static void example_read_write_flash(void); static const char* TAG = "example"; #if CONFIG_IDF_TARGET_ESP32 -#define TARGET_CRYPT_CNT_EFUSE ESP_EFUSE_FLASH_CRYPT_CNT +#define TARGET_CRYPT_CNT_EFUSE ESP_EFUSE_FLASH_CRYPT_CNT +#define TARGET_CRYPT_CNT_WIDTH 7 #elif CONFIG_IDF_TARGET_ESP32S2 #define TARGET_CRYPT_CNT_EFUSE ESP_EFUSE_SPI_BOOT_CRYPT_CNT +#define TARGET_CRYPT_CNT_WIDTH 3 #endif void app_main(void) @@ -59,7 +61,7 @@ static void example_print_chip_info(void) static void example_print_flash_encryption_status(void) { uint32_t flash_crypt_cnt = 0; - esp_efuse_read_field_blob(TARGET_CRYPT_CNT_EFUSE, &flash_crypt_cnt, 7); + esp_efuse_read_field_blob(TARGET_CRYPT_CNT_EFUSE, &flash_crypt_cnt, TARGET_CRYPT_CNT_WIDTH); printf("FLASH_CRYPT_CNT eFuse value is %d\n", flash_crypt_cnt); esp_flash_enc_mode_t mode = esp_get_flash_encryption_mode();