-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Updater.cpp support for encrypted flash #3898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This makes Updater compatible with encrypted partitions
@robertpoll which flash APIs are non-atomic in IDF v4? |
@atanisoft I haven't looked in detail, but the idf4 docs say:
There's a Config option |
I'm wondering if you might be able to shed some light on a question I've got. Do you know if this PR update will also work for pre-encrypted binaries? I'm looking to use the Arduino Update libraries to write a pre-encrypted binary to encrypted flash. The Espress-If docs say to use esp_flash_write_encrypted(...) for this purpose rather than esp_partition_write(...). When I dig deeper the former calls spi_flash_write_encrypted(...), the same as esp_partition_write(...), and so I can't see what the purpose of what is outlined in the documentation is? |
Short answer is no unfortunately. Pre-encrypted binaries are tied to a specific memory location, and with OTA you don't know which of the two partitions will be used. If you really need to do it it is possible to disable the address specific part of the encryption/decryption process using |
The docs recommend using the esp_partition_* calls as they 1) do some bounds checking and 2) check whether the partition is encrypted and pick either the encrypted or unencrypted esp_flash_* function.
|
Ah okay, I see. The ESP32 I'm working with will be receiving the binary via a BLE connection with a mobile app. I wanted to pre-encrypt it so the original binary isn't accessible to the user. I'll have to do a bit more research. Cheers for your help! |
So basically, we can do OTA update which should be NOT encrypted, and when it is placed in the partition, it will get encrypted by esp32 if FLASH_CRYPT_CNT is set to odd number? |
Yes, that's it. |
@me-no-dev Thanks! |
Background
The current implementation of
Update()
uses thespi_flash_*
api to write and read from flash. These functions ignore thepartition->encrypted
flag and always write raw data to flash even if the partition is marked as encrypted.Changes in this PR
Update()
now uses theesp_partition_*
api.esp_partition_*
added toESP.cpp
. This was done to maintain a consistent approach to the way thespi_flash_*
functions were used. I note though that not all of the esp-idf functions are used are wrapped, for exampleesp_ota_get_next_update_partition()
so it may be that these should not be added?0xFF
on write, and then when the firmware is completely written changes it back toESP_IMAGE_HEADER_MAGIC
. This works without erasing the sector because flash bits can be changed from 1->0 (but not 0->1). If the flash is encrypted then the actual data written to flash will not be all ones, so this approach will not work. In addition, encrypted flash must be written in 16 byte blocks. So, instead of changing the first byte the changed code stashes the first 16 bytes, and starts writing at the 17th byte, leaving the first 16 bytes as0xFF
. Then, in_enablePartition()
the stashed bytes can be successfully written.Benefits
esp_partition_*
api is recommended over theapi_flash_*
api.Questions