Page 1 of 1

How to tell if encrypted flash has been written

Posted: Sun Dec 04, 2022 6:34 pm
by jcolebaker
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?

Re: How to tell if encrypted flash has been written

Posted: Mon Dec 05, 2022 5:30 am
by ESP_Sprite
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.