Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 83e2f7f5 authored by Daniel Campora's avatar Daniel Campora
Browse files

esp32: FTP OTA fixes.

parent 870ceaca
No related branches found
No related tags found
No related merge requests found
......@@ -29,7 +29,7 @@ static const char *TAG = "updater";
#define UPDATER_IMG_PATH "/flash/sys/appimg.bin"
/* if flash is encrypted, it requires the flash_write operation to be done in 16 Bytes chunks */
#define ENCRYP_FLASH_MIN_CHUNK 16
#define ENCRYP_FLASH_MIN_CHUNK 16
/******************************************************************************
DEFINE TYPES
......@@ -72,14 +72,14 @@ bool updater_read_boot_info (boot_info_t *boot_info, uint32_t *boot_info_offset)
ESP_LOGV(TAG, "Reading boot info\n");
if (ESP_OK != updater_spi_flash_read(ESP_PARTITION_TABLE_ADDR, (void *)partition_info, sizeof(partition_info), true)) {
ESP_LOGE(TAG, "err1\n");
return false;
ESP_LOGE(TAG, "err1\n");
return false;
}
// get the data from the boot info partition
ESP_LOGI(TAG, "read data from: 0x%X\n", partition_info[OTA_DATA_INDEX].pos.offset);
if (ESP_OK != updater_spi_flash_read(partition_info[OTA_DATA_INDEX].pos.offset, (void *)boot_info, sizeof(boot_info_t), true)) {
ESP_LOGE(TAG, "err2\n");
return false;
ESP_LOGE(TAG, "err2\n");
return false;
}
*boot_info_offset = partition_info[OTA_DATA_INDEX].pos.offset;
ESP_LOGD(TAG, "off: %d, status:%d, %d\n", *boot_info_offset, boot_info->Status, boot_info->ActiveImg);
......@@ -159,49 +159,9 @@ bool updater_finish (void) {
boot_info.ActiveImg = IMG_ACT_UPDATE1;
}
boot_info.Status = IMG_STATUS_CHECK;
boot_info.crc = crc32_le(UINT32_MAX, (uint8_t*)&boot_info.ActiveImg,
sizeof(boot_info) - sizeof(boot_info.crc));
ESP_LOGI(TAG, "Wr crc=0x%x\n", boot_info.crc);
if (ESP_OK != spi_flash_erase_sector(boot_info_offset / SPI_FLASH_SEC_SIZE)) {
ESP_LOGE(TAG, "Erasing boot info failed\n");
return false;
}
// saving boot info, encrypted
esp_err_t ret; // return code of the flash_write operation
if (esp_flash_encryption_enabled()) {
// sizeof(boot_info_t) is 40 bytes, and we have to write multiple of 16
// so read next 48-40 bytes from flash, and write back 48 B
uint32_t len_aligned_16 = ((sizeof(boot_info_t) + 15) / 16) * 16;
uint8_t *buff; // buffer used for filling boot_info data
buff = (uint8_t *)malloc(len_aligned_16);
if (!buff) {
ESP_LOGE(TAG, "Can't allocate %d\n", len_aligned_16);
return false;
}
// put the first sizeof(boot_info_t)
memcpy(buff, (void *)&boot_info, sizeof(boot_info_t));
// read the next bytes
spi_flash_read_encrypted(boot_info_offset + sizeof(boot_info_t),
(void *)(buff + sizeof(boot_info_t)),
len_aligned_16 - sizeof(boot_info_t) );
ret = spi_flash_write_encrypted(boot_info_offset, (void *)buff, len_aligned_16);
} else { // not-encrypted flash, just write directly boot_info
ret = spi_flash_write(boot_info_offset, (void *)&boot_info, sizeof(boot_info_t));
}
if (ESP_OK != ret) {
ESP_LOGE(TAG, "Saving boot info failed\n");
return false;
}
ESP_LOGI(TAG, "Boot info saved OK\n");
// save the actual boot_info structure to otadata partition
updater_write_boot_info(&boot_info, boot_info_offset);
}
// sl_LockObjUnlock (&wlan_LockObj);
updater_data.offset = 0;
......@@ -214,10 +174,10 @@ bool updater_verify (void) {
// bootloader verifies anyway the image, but the user can check himself
// so, the next code is adapted from bootloader/bootloader.c,
// the last image written stats at updater_data.offset_start_upd and
// the last image written stats at updater_data.offset_start_upd and
// has the lenght boot_info.size
esp_err_t ret;
esp_err_t ret;
esp_image_metadata_t data;
const esp_partition_pos_t part_pos = {
.offset = updater_data.offset_start_upd,
......@@ -233,37 +193,68 @@ bool updater_verify (void) {
bool updater_write_boot_info(boot_info_t *boot_info, uint32_t boot_info_offset) {
boot_info->crc = crc32_le(UINT32_MAX, (uint8_t *)boot_info, sizeof(boot_info_t) - sizeof(boot_info->crc));
ESP_LOGI(TAG, "Wr crc=0x%x\n", boot_info->crc);
if (ESP_OK != spi_flash_erase_sector(boot_info_offset / SPI_FLASH_SEC_SIZE)) {
printf("Erasing boot info failed\n");
return false;
}
if (ESP_OK != spi_flash_write(boot_info_offset, (void *)boot_info, sizeof(boot_info_t))) {
printf("Saving boot info failed\n");
return false;
// saving boot info, encrypted
esp_err_t ret; // return code of the flash_write operation
if (esp_flash_encryption_enabled()) {
// sizeof(boot_info_t) is 40 bytes, and we have to write multiple of 16
// so read next 48-40 bytes from flash, and write back 48 B
uint32_t len_aligned_16 = ((sizeof(boot_info_t) + 15) / 16) * 16;
uint8_t *buff; // buffer used for filling boot_info data
buff = (uint8_t *)malloc(len_aligned_16);
if (!buff) {
ESP_LOGE(TAG, "Can't allocate %d\n", len_aligned_16);
return false;
}
// put the first sizeof(boot_info_t)
memcpy(buff, (void *)boot_info, sizeof(boot_info_t));
// read the next bytes
spi_flash_read_encrypted(boot_info_offset + sizeof(boot_info_t),
(void *)(buff + sizeof(boot_info_t)),
len_aligned_16 - sizeof(boot_info_t) );
ret = spi_flash_write_encrypted(boot_info_offset, (void *)buff, len_aligned_16);
} else { // not-encrypted flash, just write directly boot_info
ret = spi_flash_write(boot_info_offset, (void *)boot_info, sizeof(boot_info_t));
}
printf("Boot info saved OK\n");
return true;
if (ESP_OK != ret) {
ESP_LOGE(TAG, "Saving boot info failed\n");
} else {
ESP_LOGI(TAG, "Boot info saved OK\n");
}
return (ESP_OK == ret);
}
int updater_ota_next_slot_address() {
int ota_offset = IMG_UPDATE1_OFFSET;
int ota_offset = IMG_UPDATE1_OFFSET;
// check which one should be the next active image
// check which one should be the next active image
if (updater_read_boot_info (&boot_info, &boot_info_offset)) {
// if we still have an image pending for verification, keep overwriting it
if ((boot_info.Status == IMG_STATUS_CHECK && boot_info.ActiveImg == IMG_ACT_UPDATE2) ||
(boot_info.ActiveImg == IMG_ACT_UPDATE1 && boot_info.Status != IMG_STATUS_CHECK)) {
ota_offset = IMG_UPDATE2_OFFSET;
ota_offset = IMG_UPDATE2_OFFSET;
}
}
ESP_LOGI(TAG, "Next slot address: 0x%6X\n", ota_offset);
return ota_offset;
return ota_offset;
}
/******************************************************************************
......@@ -284,7 +275,7 @@ static esp_err_t updater_spi_flash_read(size_t src, void *dest, size_t size, boo
* be multiples of 32 bytes.
*/
static esp_err_t updater_spi_flash_write(size_t dest_addr, void *src, size_t size,
bool write_encrypted)
bool write_encrypted)
{
if (write_encrypted && esp_flash_encryption_enabled()) {
return spi_flash_write_encrypted(dest_addr, src, size);
......
......@@ -35,11 +35,11 @@ extern bool updater_start(void);
* @brief OTA Write next chunk to Flash.
*
* @note The OTA process has to be previously initialized with updater_start().
* The buf is written as it is (not-encrypted) into Flash.
* If Flash Encryption is enabled, the buf must be already encrypted (by the OTA server).
* The buf is written as it is (not-encrypted) into Flash.
* If Flash Encryption is enabled, the buf must be already encrypted (by the OTA server).
*
* @param buf buffer with the data-chunk which needs to be written into Flash
* @param len length of the buf data.
* @param buf buffer with the data-chunk which needs to be written into Flash
* @param len length of the buf data.
*
* @return true if write into Flash succeeded.
*/
......@@ -56,7 +56,7 @@ extern bool updater_finish(void);
* @brief Verifies the newly written OTA image.
*
* @note If Secure Boot is enabled the signature is checked.
* Anyway the image integrity (SHA256) is checked.
* Anyway the image integrity (SHA256) is checked.
*
* @return true if boot info was saved successful; false otherwise.
*/
......@@ -65,8 +65,8 @@ extern bool updater_verify(void);
/**
* @brief Reads the boot information, what partition is going to be booted from.
*
* @param boot_info [in/out] filled with boot info
* @param boot_info_offset [in/out] filled with the address in Flash, of the boot_info (otadata partition)
* @param boot_info [in/out] filled with boot info
* @param boot_info_offset [in/out] filled with the address in Flash, of the boot_info (otadata partition)
*
* @return true if reading was done successful.
*/
......@@ -79,6 +79,14 @@ extern bool updater_read_boot_info(boot_info_t *boot_info, uint32_t *boot_info_o
*/
extern int updater_ota_next_slot_address();
/**
* @brief Writes the boot information into the otadata partition.
*
* @param boot_info [in] the boot info data structure
* @param boot_info_offset [in] the address in Flash, of the boot_info (otadata partition)
*
* @return true if reading was done successful.
*/
extern bool updater_write_boot_info(boot_info_t *boot_info, uint32_t boot_info_offset);
#endif /* UPDATER_H_ */
......@@ -89,6 +89,8 @@ void mperror_init0 (void) {
// configure the heartbeat led pin
pin_config(&pin_GPIO0, -1, -1, GPIO_MODE_OUTPUT, MACHPIN_PULL_NONE, 0);
led_init(&led_info);
led_info.color.value = 0;
led_set_color(&led_info, false);
mperror_heart_beat.on_time = 0;
mperror_heart_beat.off_time = 0;
mperror_heart_beat.beating = false;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment