From 602243748bdf238ff16ffc8977e6cc18b4357dcc Mon Sep 17 00:00:00 2001 From: microDev <70126934+microDev1@users.noreply.github.com> Date: Tue, 8 Dec 2020 11:45:00 +0530 Subject: [PATCH] add ota support for esp32s2 --- locale/circuitpython.pot | 13 ++- ports/esp32s2/Makefile | 2 + ports/esp32s2/common-hal/ota/__init__.c | 101 +++++++++++++++++++++++- 3 files changed, 111 insertions(+), 5 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index b136e9dd26..5bf95043a6 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-11-27 23:57-0500\n" +"POT-Creation-Date: 2020-12-08 10:30+0530\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -498,8 +498,8 @@ msgstr "" msgid "Buffer must be a multiple of 512 bytes" msgstr "" -#: shared-bindings/bitbangio/I2C.c shared-bindings/busdevice/I2CDevice.c -#: shared-bindings/busio/I2C.c +#: shared-bindings/adafruit_bus_device/I2CDevice.c +#: shared-bindings/bitbangio/I2C.c shared-bindings/busio/I2C.c msgid "Buffer must be at least length 1" msgstr "" @@ -1296,7 +1296,7 @@ msgstr "" msgid "No DMA channel found" msgstr "" -#: shared-module/busdevice/I2CDevice.c +#: shared-module/adafruit_bus_device/I2CDevice.c #, c-format msgid "No I2C device at address: %x" msgstr "" @@ -1420,6 +1420,10 @@ msgstr "" msgid "Not settable" msgstr "" +#: ports/esp32s2/common-hal/ota/__init__.c +msgid "OTA Update Failed" +msgstr "" + #: shared-bindings/util.c msgid "" "Object has been deinitialized and can no longer be used. Create a new object." @@ -1503,6 +1507,7 @@ msgstr "" msgid "Pin does not have ADC capabilities" msgstr "" +#: shared-bindings/adafruit_bus_device/SPIDevice.c #: shared-bindings/digitalio/DigitalInOut.c msgid "Pin is input only" msgstr "" diff --git a/ports/esp32s2/Makefile b/ports/esp32s2/Makefile index 4486cb598c..ce0a4825d3 100644 --- a/ports/esp32s2/Makefile +++ b/ports/esp32s2/Makefile @@ -106,6 +106,8 @@ INC += -isystem esp-idf/components/heap/include INC += -isystem esp-idf/components/esp_system/include INC += -isystem esp-idf/components/spi_flash/include INC += -isystem esp-idf/components/nvs_flash/include +INC += -isystem esp-idf/components/app_update/include +INC += -isystem esp-idf/components/bootloader_support/include INC += -I$(BUILD)/esp-idf/config CFLAGS += -DHAVE_CONFIG_H \ diff --git a/ports/esp32s2/common-hal/ota/__init__.c b/ports/esp32s2/common-hal/ota/__init__.c index 87daffa568..727b6b107b 100644 --- a/ports/esp32s2/common-hal/ota/__init__.c +++ b/ports/esp32s2/common-hal/ota/__init__.c @@ -26,6 +26,105 @@ #include "shared-bindings/ota/__init__.h" -void common_hal_ota_flash(const void *buf, const size_t len) { +#include +#include "esp_log.h" +#include "esp_ota_ops.h" + +static const char *TAG = "OTA"; + +static void __attribute__((noreturn)) task_fatal_error(void) { + ESP_LOGE(TAG, "Exiting task due to fatal error..."); + mp_raise_RuntimeError(translate("OTA Update Failed")); +} + +void common_hal_ota_flash(const void *buf, const size_t len) { + esp_err_t err; + /* update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */ + esp_ota_handle_t update_handle = 0 ; + const esp_partition_t *update_partition = NULL; + + ESP_LOGI(TAG, "Starting update"); + + const esp_partition_t *configured = esp_ota_get_boot_partition(); + const esp_partition_t *running = esp_ota_get_running_partition(); + + if (configured != running) { + ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08x, but running from offset 0x%08x", + configured->address, running->address); + ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)"); + } + ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)", + running->type, running->subtype, running->address); + + update_partition = esp_ota_get_next_update_partition(NULL); + ESP_LOGI(TAG, "Writing partition type %d subtype %d (offset 0x%08x)\n", + update_partition->type, update_partition->subtype, update_partition->address); + assert(update_partition != NULL); + + + if (len > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) { + // check current version with downloading + esp_app_desc_t new_app_info; + memcpy(&new_app_info, &((char *)buf)[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t)); + ESP_LOGI(TAG, "New firmware version: %s", new_app_info.version); + + esp_app_desc_t running_app_info; + if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) { + ESP_LOGI(TAG, "Running firmware version: %s", running_app_info.version); + } + + const esp_partition_t* last_invalid_app = esp_ota_get_last_invalid_partition(); + esp_app_desc_t invalid_app_info; + if (esp_ota_get_partition_description(last_invalid_app, &invalid_app_info) == ESP_OK) { + ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version); + } + + // check current version with last invalid partition + if (last_invalid_app != NULL) { + if (memcmp(invalid_app_info.version, new_app_info.version, sizeof(new_app_info.version)) == 0) { + ESP_LOGW(TAG, "New version is the same as invalid version."); + ESP_LOGW(TAG, "Previously, there was an attempt to launch the firmware with %s version, but it failed.", invalid_app_info.version); + ESP_LOGW(TAG, "The firmware has been rolled back to the previous version."); + task_fatal_error(); + } + } + + if (memcmp(new_app_info.version, running_app_info.version, sizeof(new_app_info.version)) == 0) { + ESP_LOGW(TAG, "Current running version is the same as a new. We will not continue the update."); + task_fatal_error(); + } + + err = esp_ota_begin(update_partition, OTA_WITH_SEQUENTIAL_WRITES, &update_handle); + if (err != ESP_OK) { + ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err)); + task_fatal_error(); + } + ESP_LOGI(TAG, "esp_ota_begin succeeded"); + } else { + ESP_LOGE(TAG, "received package is not fit len"); + task_fatal_error(); + } + + err = esp_ota_write( update_handle, buf, len); + if (err != ESP_OK) { + ESP_LOGE(TAG, "esp_ota_write failed (%s)", esp_err_to_name(err)); + task_fatal_error(); + } + ESP_LOGI(TAG, "Total Write binary data length: %d", len); + + err = esp_ota_end(update_handle); + if (err != ESP_OK) { + if (err == ESP_ERR_OTA_VALIDATE_FAILED) { + ESP_LOGE(TAG, "Image validation failed, image is corrupted"); + } + ESP_LOGE(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err)); + task_fatal_error(); + } + + err = esp_ota_set_boot_partition(update_partition); + if (err != ESP_OK) { + ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err)); + task_fatal_error(); + } }