How to tell if encrypted flash has been written

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

How to tell if encrypted flash has been written

Postby jcolebaker » Sun Dec 04, 2022 6:34 pm

We have been using esp_partition_write/read functions to access a partition to store custom data, without flash encryption.

The code needs to detect whether a particular data element has been written yet. Previously, we read the contents, and if the bytes were 0xFF, we assumed the data has been erased and not written yet (we never write 0xFF to that location).

However, with flash encryption enabled, if the contents of the partition are erased, reading the data (with esp_partition_read or memory mapped access) returns garbled data because the device "decrypts" the 0xFF values.

We could use "esp_flash_read" to read the raw bytes, but then (maybe?) it is possible that real data has been written to the flash, which happened to encrypt to the value 0xFF.

Is there some other way to detect whether a flash sector is in an erased / unwritten state when flash encryption is turned on?

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: How to tell if encrypted flash has been written

Postby ESP_Sprite » Mon Dec 05, 2022 5:30 am

One approach is to add a checksum/CRC/hash to the data you write: if that matches with the calculated data checksum/crc/hash, the data is valid, if not it's either empty or corrupted. Another is to use esp_flash_read, according to the docs, that reads the flash directly so you could re-use the '0xff is empty' logic.

Who is online

Users browsing this forum: HighVoltage, mikecarlos and 100 guests