Skip to content

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

Merged
merged 1 commit into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions cores/esp32/Esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,20 @@ bool EspClass::flashRead(uint32_t offset, uint32_t *data, size_t size)
return spi_flash_read(offset, (uint32_t*) data, size) == ESP_OK;
}

bool EspClass::partitionEraseRange(const esp_partition_t *partition, uint32_t offset, size_t size)
{
return esp_partition_erase_range(partition, offset, size) == ESP_OK;
}

bool EspClass::partitionWrite(const esp_partition_t *partition, uint32_t offset, uint32_t *data, size_t size)
{
return esp_partition_write(partition, offset, data, size) == ESP_OK;
}

bool EspClass::partitionRead(const esp_partition_t *partition, uint32_t offset, uint32_t *data, size_t size)
{
return esp_partition_read(partition, offset, data, size) == ESP_OK;
}

uint64_t EspClass::getEfuseMac(void)
{
Expand Down
5 changes: 5 additions & 0 deletions cores/esp32/Esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define ESP_H

#include <Arduino.h>
#include <esp_partition.h>

/**
* AVR macros for WDT managment
Expand Down Expand Up @@ -97,6 +98,10 @@ class EspClass
bool flashWrite(uint32_t offset, uint32_t *data, size_t size);
bool flashRead(uint32_t offset, uint32_t *data, size_t size);

bool partitionEraseRange(const esp_partition_t *partition, uint32_t offset, size_t size);
bool partitionWrite(const esp_partition_t *partition, uint32_t offset, uint32_t *data, size_t size);
bool partitionRead(const esp_partition_t *partition, uint32_t offset, uint32_t *data, size_t size);

uint64_t getEfuseMac();

};
Expand Down
4 changes: 4 additions & 0 deletions libraries/Update/src/Update.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#define U_SPIFFS 100
#define U_AUTH 200

#define ENCRYPTED_BLOCK_SIZE 16

class UpdateClass {
public:
typedef std::function<void(size_t, size_t)> THandlerFunction_Progress;
Expand Down Expand Up @@ -163,10 +165,12 @@ class UpdateClass {
bool _writeBuffer();
bool _verifyHeader(uint8_t data);
bool _verifyEnd();
bool _enablePartition(const esp_partition_t* partition);


uint8_t _error;
uint8_t *_buffer;
uint8_t *_skipBuffer;
size_t _bufferLen;
size_t _size;
THandlerFunction_Progress _progress_callback;
Expand Down
33 changes: 18 additions & 15 deletions libraries/Update/src/Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ static const char * _err2str(uint8_t _error){
}

static bool _partitionIsBootable(const esp_partition_t* partition){
uint8_t buf[4];
uint8_t buf[ENCRYPTED_BLOCK_SIZE];
if(!partition){
return false;
}
if(!ESP.flashRead(partition->address, (uint32_t*)buf, 4)) {
if(!ESP.partitionRead(partition, 0, (uint32_t*)buf, ENCRYPTED_BLOCK_SIZE)) {
return false;
}

Expand All @@ -50,17 +50,11 @@ static bool _partitionIsBootable(const esp_partition_t* partition){
return true;
}

static bool _enablePartition(const esp_partition_t* partition){
uint8_t buf[4];
bool UpdateClass::_enablePartition(const esp_partition_t* partition){
if(!partition){
return false;
}
if(!ESP.flashRead(partition->address, (uint32_t*)buf, 4)) {
return false;
}
buf[0] = ESP_IMAGE_HEADER_MAGIC;

return ESP.flashWrite(partition->address, (uint32_t*)buf, 4);
return ESP.partitionWrite(partition, 0, (uint32_t*) _skipBuffer, ENCRYPTED_BLOCK_SIZE);
}

UpdateClass::UpdateClass()
Expand Down Expand Up @@ -179,24 +173,33 @@ void UpdateClass::abort(){

bool UpdateClass::_writeBuffer(){
//first bytes of new firmware
uint8_t skip = 0;
if(!_progress && _command == U_FLASH){
//check magic
if(_buffer[0] != ESP_IMAGE_HEADER_MAGIC){
_abort(UPDATE_ERROR_MAGIC_BYTE);
return false;
}
//remove magic byte from the firmware now and write it upon success
//this ensures that partially written firmware will not be bootable
_buffer[0] = 0xFF;

//Stash the first 16 bytes of data and set the offset so they are
//not written at this point so that partially written firmware
//will not be bootable
skip = ENCRYPTED_BLOCK_SIZE;
_skipBuffer = (uint8_t*)malloc(skip);
if(!_skipBuffer){
log_e("malloc failed");
return false;
}
memcpy(_skipBuffer, _buffer, skip);
}
if (!_progress && _progress_callback) {
_progress_callback(0, _size);
}
if(!ESP.flashEraseSector((_partition->address + _progress)/SPI_FLASH_SEC_SIZE)){
if(!ESP.partitionEraseRange(_partition, _progress, SPI_FLASH_SEC_SIZE)){
_abort(UPDATE_ERROR_ERASE);
return false;
}
if (!ESP.flashWrite(_partition->address + _progress, (uint32_t*)_buffer, _bufferLen)) {
if (!ESP.partitionWrite(_partition, _progress + skip, (uint32_t*)_buffer + skip/sizeof(uint32_t), _bufferLen - skip)) {
_abort(UPDATE_ERROR_WRITE);
return false;
}
Expand Down