]> granicus.if.org Git - esp-idf/commitdiff
spiflash: Add spi_flash_read_encrypted() function
authorAngus Gratton <angus@espressif.com>
Wed, 4 Jan 2017 06:37:15 +0000 (17:37 +1100)
committerIvan Grokhotkov <ivan@espressif.com>
Fri, 20 Jan 2017 11:48:46 +0000 (19:48 +0800)
components/spi_flash/flash_ops.c
components/spi_flash/include/esp_spi_flash.h

index a74558a96fa9ed66284ec952a262eed2f77813a6..22070eb5ffda7cd75733779117876a379046d06d 100644 (file)
@@ -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) {
index bf897e8995c74d1c5e93b6d07f2ad2d15a89c1dd..d0b6cd20b9a2420ef546d0742cd5136e6371ee37 100644 (file)
@@ -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
  */