diff --git a/components/spi_flash/flash_ops.c b/components/spi_flash/flash_ops.c index a74558a96..22070eb5f 100644 --- a/components/spi_flash/flash_ops.c +++ b/components/spi_flash/flash_ops.c @@ -381,6 +381,33 @@ out: return spi_flash_translate_rc(rc); } +#define FLASH_PAGE_SIZE 0x10000 + +esp_err_t IRAM_ATTR spi_flash_read_encrypted(size_t src, void *dstv, size_t size) +{ + if (src + size > g_rom_flashchip.chip_size) { + return ESP_ERR_INVALID_SIZE; + } + if (size == 0) { + return ESP_OK; + } + + esp_err_t err; + const uint8_t *map; + spi_flash_mmap_handle_t map_handle; + size_t map_src = src & ~(FLASH_PAGE_SIZE-1); + size_t map_size = size + (src - map_src); + + err = spi_flash_mmap(map_src, map_size, SPI_FLASH_MMAP_DATA, (const void **)&map, &map_handle); + if (err != ESP_OK) { + return err; + } + memcpy(dstv, map + (src - map_src), size); + spi_flash_munmap(map_handle); + return err; +} + + static esp_err_t spi_flash_translate_rc(SpiFlashOpResult rc) { switch (rc) { diff --git a/components/spi_flash/include/esp_spi_flash.h b/components/spi_flash/include/esp_spi_flash.h index bf897e899..d0b6cd20b 100644 --- a/components/spi_flash/include/esp_spi_flash.h +++ b/components/spi_flash/include/esp_spi_flash.h @@ -116,6 +116,23 @@ esp_err_t spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t si */ esp_err_t spi_flash_read(size_t src_addr, void *dest, size_t size); + +/** + * @brief Read data from Encrypted Flash. + * + * If flash encryption is enabled, this function will transparently decrypt data as it is read. + * If flash encryption is not enabled, this function behaves the same as spi_flash_read(). + * + * See @ref esp_flash_encryption_enabled() for a function to check if flash encryption is enabled. + * + * @param src source address of the data in Flash. + * @param dest pointer to the destination buffer + * @param size length of data + * + * @return esp_err_t + */ +esp_err_t spi_flash_read_encrypted(size_t src, void *dstv, size_t size); + /** * @brief Enumeration which specifies memory space requested in an mmap call */