Skip to content

Support for OTA update #3812

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 14 commits into from
Dec 22, 2020
12 changes: 12 additions & 0 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,10 @@ msgstr ""
msgid "Filters too complex"
msgstr ""

#: ports/esp32s2/common-hal/dualbank/__init__.c
msgid "Firmware image is invalid"
msgstr ""

#: ports/cxd56/common-hal/camera/Camera.c
msgid "Format not supported"
msgstr ""
Expand Down Expand Up @@ -1983,6 +1987,10 @@ msgstr ""
msgid "Unsupported pull value."
msgstr ""

#: ports/esp32s2/common-hal/dualbank/__init__.c
msgid "Update Failed"
msgstr ""

#: ports/nrf/common-hal/_bleio/Characteristic.c
#: ports/nrf/common-hal/_bleio/Descriptor.c
msgid "Value length != required fixed length"
Expand Down Expand Up @@ -3211,6 +3219,10 @@ msgstr ""
msgid "offset is too large"
msgstr ""

#: shared-bindings/dualbank/__init__.c
msgid "offset must be >= 0"
msgstr ""

#: py/objstr.c py/objstrunicode.c
msgid "offset out of bounds"
msgstr ""
Expand Down
2 changes: 2 additions & 0 deletions ports/esp32s2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
139 changes: 139 additions & 0 deletions ports/esp32s2/common-hal/dualbank/__init__.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "common-hal/dualbank/__init__.h"
#include "shared-bindings/dualbank/__init__.h"

#include <string.h>

#include "esp_log.h"
#include "esp_ota_ops.h"

static const esp_partition_t *update_partition = NULL;
static esp_ota_handle_t update_handle = 0;

static const char *TAG = "dualbank";

void dualbank_reset(void) {
// should use `abort` instead of `end`
// but not in idf v4.2
// esp_ota_abort(update_handle);
if (esp_ota_end(update_handle) == ESP_OK) {
update_handle = 0;
update_partition = NULL;
}
}

static void __attribute__((noreturn)) task_fatal_error(void) {
ESP_LOGE(TAG, "Exiting task due to fatal error...");
mp_raise_RuntimeError(translate("Update Failed"));
}

void common_hal_dualbank_flash(const void *buf, const size_t len, const size_t offset) {
esp_err_t err;

const esp_partition_t *running = esp_ota_get_running_partition();
const esp_partition_t *last_invalid = esp_ota_get_last_invalid_partition();

if (update_partition == NULL) {
update_partition = esp_ota_get_next_update_partition(NULL);

ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)",
running->type, running->subtype, running->address);

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 (update_handle == 0) {
if (len > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) {
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);
}

esp_app_desc_t invalid_app_info;
if (esp_ota_get_partition_description(last_invalid, &invalid_app_info) == ESP_OK) {
ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version);
}

// check new version with running version
if (memcmp(new_app_info.version, running_app_info.version, sizeof(new_app_info.version)) == 0) {
ESP_LOGW(TAG, "New version is the same as running version.");
task_fatal_error();
}

// check new version with last invalid partition
if (last_invalid != NULL) {
if (memcmp(new_app_info.version, invalid_app_info.version, sizeof(new_app_info.version)) == 0) {
ESP_LOGW(TAG, "New version is the same as invalid version.");
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();
}
} else {
ESP_LOGE(TAG, "received package is not fit len");
task_fatal_error();
}
}

if (offset == 0) {
err = esp_ota_write(update_handle, buf, len);
} else {
err = esp_ota_write_with_offset(update_handle, buf, len, offset);
}
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_ota_write failed (%s)", esp_err_to_name(err));
task_fatal_error();
}
}

void common_hal_dualbank_switch(void) {
if (esp_ota_end(update_handle) == ESP_OK) {
update_handle = 0;
update_partition = NULL;
}
esp_err_t err = esp_ota_set_boot_partition(esp_ota_get_next_update_partition(NULL));
if (err != ESP_OK) {
if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
ESP_LOGE(TAG, "Image validation failed, image is corrupted");
mp_raise_RuntimeError(translate("Firmware image is invalid"));
}
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
task_fatal_error();
}
}
32 changes: 32 additions & 0 deletions ports/esp32s2/common-hal/dualbank/__init__.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_DUALBANK___INIT___H
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_DUALBANK___INIT___H

extern void dualbank_reset(void);

#endif //MICROPY_INCLUDED_ESP32S2_COMMON_HAL_DUALBANK___INIT___H
1 change: 1 addition & 0 deletions ports/esp32s2/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_AUDIOIO = 0
CIRCUITPY_CANIO = 1
CIRCUITPY_COUNTIO = 1
CIRCUITPY_DUALBANK = 1
CIRCUITPY_FREQUENCYIO = 1
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_ROTARYIO = 1
Expand Down
5 changes: 5 additions & 0 deletions ports/esp32s2/supervisor/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "common-hal/busio/I2C.h"
#include "common-hal/busio/SPI.h"
#include "common-hal/busio/UART.h"
#include "common-hal/dualbank/__init__.h"
#include "common-hal/ps2io/Ps2.h"
#include "common-hal/pulseio/PulseIn.h"
#include "common-hal/pwmio/PWMOut.h"
Expand Down Expand Up @@ -123,6 +124,10 @@ void reset_port(void) {
analogout_reset();
#endif

#if CIRCUITPY_DUALBANK
dualbank_reset();
#endif

#if CIRCUITPY_PS2IO
ps2_reset();
#endif
Expand Down
4 changes: 4 additions & 0 deletions py/circuitpy_defns.mk
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ endif
ifeq ($(CIRCUITPY_OS),1)
SRC_PATTERNS += os/%
endif
ifeq ($(CIRCUITPY_DUALBANK),1)
SRC_PATTERNS += dualbank/%
endif
ifeq ($(CIRCUITPY_PIXELBUF),1)
SRC_PATTERNS += _pixelbuf/%
endif
Expand Down Expand Up @@ -348,6 +351,7 @@ SRC_COMMON_HAL_ALL = \
nvm/ByteArray.c \
nvm/__init__.c \
os/__init__.c \
dualbank/__init__.c \
ps2io/Ps2.c \
ps2io/__init__.c \
pulseio/PulseIn.c \
Expand Down
8 changes: 8 additions & 0 deletions py/circuitpy_mpconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,13 @@ extern const struct _mp_obj_module_t os_module;
#define OS_MODULE_ALT_NAME
#endif

#if CIRCUITPY_DUALBANK
extern const struct _mp_obj_module_t dualbank_module;
#define DUALBANK_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_dualbank), (mp_obj_t)&dualbank_module },
#else
#define DUALBANK_MODULE
#endif

#if CIRCUITPY_PEW
extern const struct _mp_obj_module_t pew_module;
#define PEW_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR__pew),(mp_obj_t)&pew_module },
Expand Down Expand Up @@ -827,6 +834,7 @@ extern const struct _mp_obj_module_t wifi_module;
NETWORK_MODULE \
SOCKET_MODULE \
WIZNET_MODULE \
DUALBANK_MODULE \
PEW_MODULE \
PIXELBUF_MODULE \
PS2IO_MODULE \
Expand Down
3 changes: 3 additions & 0 deletions py/circuitpy_mpconfig.mk
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ CFLAGS += -DCIRCUITPY_NVM=$(CIRCUITPY_NVM)
CIRCUITPY_OS ?= 1
CFLAGS += -DCIRCUITPY_OS=$(CIRCUITPY_OS)

CIRCUITPY_DUALBANK ?= 0
CFLAGS += -DCIRCUITPY_DUALBANK=$(CIRCUITPY_DUALBANK)

CIRCUITPY_PIXELBUF ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_PIXELBUF=$(CIRCUITPY_PIXELBUF)

Expand Down
Loading