Garbled read of MMap'ed data from UNENCRYPTED partition on ENCRYPTED flash

jcolebaker
Posts: 64
Joined: Thu Mar 18, 2021 12:23 am

Garbled read of MMap'ed data from UNENCRYPTED partition on ENCRYPTED flash

Postby jcolebaker » Thu Jun 15, 2023 8:57 pm

I'm having trouble accessing data from a partition via memory map.

We're using flash encryption, and some partitions are encrypted while others are not.

Here's my partition table (some rows omitted for clarity):

Code: Select all

# ESP-IDF Partition Table
# Name,Type,SubType,Offset,Size,Flags
nvs,data,nvs,0xF000,28K,
otadata,data,ota,0x16000,8K,
phy_init,data,phy,0x18000,4K,
nvs_keys,data,nvs_keys,0x31000,4K,encrypted
certificates,data,fat,0x32000,120K,encrypted
logs,data,fat,0x60000,1024K,
factory,app,factory,0x180000,2048K,encrypted
ota_0,app,ota_0,0x380000,2304K,encrypted
ota_1,app,ota_1,0x5C0000,2304K,encrypted
With the encrypted "certificates" partition:
* I can write to the partition with esp_partition_write
* I can memory map the partition with esp_partition_mmap
* I can read back the plain-text data via the pointer returned from esp_partition_mmap
* Working as expected!

With the unencrypted "logs" partition:
* I can write to the partition with esp_partition_write
* I can read the data via esp_partition_read - I get back the original data
* I can memory map the partition with esp_partition_mmap
* BUT when I read back the data via the pointer returned from esp_partition_mmap, I get garbage.

The only explanation I can think of at the moment is that the pointer read is (wrongly) going through the flash decryption process even though the partition is not flagged as "encrypted" and contains plain-text data.

I'm using basically the same code for both memory maps, e.g.:

Code: Select all

    void *p_logs_mmap_data;
    spi_flash_mmap_handle_t logs_mmap_handle;
    
    esp_err_t mmap_result = esp_partition_mmap(
        p_logs_partition,
        0,
        p_logs_partition->size,
        SPI_FLASH_MMAP_DATA,
        &p_logs_mmap_data,
        &logs_mmap_handle
    );
I thought the lack of the "encrypted" flag would mean the pointer (memory-mapped) access would not go through the decryption. Am I using this incorrectly?

MicroController
Posts: 1709
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Garbled read of MMap'ed data from UNENCRYPTED partition on ENCRYPTED flash

Postby MicroController » Fri Jun 16, 2023 7:31 am

the pointer read is (wrongly) going through the flash decryption process
Works as specified:
The MMU flash cache unconditionally decrypts all existing data. Data which is stored unencrypted in flash memory will also be “transparently decrypted” via the flash cache and will appear to software as random garbage.
So there is no way to mmap unencrypted parts of flash when flash encryption is enabled.

Who is online

Users browsing this forum: No registered users and 92 guests