circuitpython/ports/esp32s2/common-hal/dualbank/__init__.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

140 lines
5.4 KiB
C
Raw Normal View History

2020-12-08 01:00:00 -05:00
/*
* 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.
*/
2020-12-17 14:04:56 -05:00
#include "common-hal/dualbank/__init__.h"
#include "shared-bindings/dualbank/__init__.h"
2020-12-08 01:00:00 -05:00
2020-12-08 01:15:00 -05:00
#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;
2020-12-10 03:34:24 -05:00
2020-12-17 14:04:56 -05:00
static const char *TAG = "dualbank";
2020-12-08 01:15:00 -05:00
2020-12-17 14:04:56 -05:00
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;
}
}
2020-12-08 01:15:00 -05:00
static void __attribute__((noreturn)) task_fatal_error(void) {
ESP_LOGE(TAG, "Exiting task due to fatal error...");
2020-12-17 14:04:56 -05:00
mp_raise_RuntimeError(translate("Update Failed"));
2020-12-08 01:15:00 -05:00
}
2020-12-17 14:04:56 -05:00
void common_hal_dualbank_flash(const void *buf, const size_t len, const size_t offset) {
2020-12-08 01:15:00 -05:00
esp_err_t err;
2020-12-10 02:04:56 -05:00
2020-12-08 01:15:00 -05:00
const esp_partition_t *running = esp_ota_get_running_partition();
2020-12-10 02:04:56 -05:00
const esp_partition_t *last_invalid = esp_ota_get_last_invalid_partition();
2020-12-08 01:15:00 -05:00
if (update_partition == NULL) {
update_partition = esp_ota_get_next_update_partition(NULL);
2020-12-08 01:15:00 -05:00
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)",
2021-03-15 09:57:36 -04:00
running->type, running->subtype, running->address);
2020-12-08 01:15:00 -05:00
ESP_LOGI(TAG, "Writing partition type %d subtype %d (offset 0x%08x)\n",
2021-03-15 09:57:36 -04:00
update_partition->type, update_partition->subtype, update_partition->address);
2020-12-08 01:15:00 -05:00
assert(update_partition != NULL);
}
2020-12-08 01:15:00 -05:00
2020-12-17 14:04:56 -05:00
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);
2020-12-08 01:15:00 -05:00
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);
}
2020-12-08 01:15:00 -05:00
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);
}
2020-12-10 02:04:56 -05:00
// 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.");
2020-12-08 01:15:00 -05:00
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();
}
}
2020-12-27 23:30:49 -05:00
err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &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");
2020-12-08 01:15:00 -05:00
task_fatal_error();
}
}
2020-12-17 14:04:56 -05:00
if (offset == 0) {
err = esp_ota_write(update_handle, buf, len);
} else {
err = esp_ota_write_with_offset(update_handle, buf, len, offset);
}
2020-12-08 01:15:00 -05:00
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_ota_write failed (%s)", esp_err_to_name(err));
task_fatal_error();
}
2020-12-10 03:34:24 -05:00
}
2020-12-17 14:04:56 -05:00
void common_hal_dualbank_switch(void) {
if (esp_ota_end(update_handle) == ESP_OK) {
update_handle = 0;
update_partition = NULL;
2020-12-08 01:15:00 -05:00
}
2020-12-17 14:04:56 -05:00
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();
2020-12-10 06:12:45 -05:00
}
}