re-organize and clean-up

This commit is contained in:
microDev 2020-12-10 12:34:56 +05:30
parent 602243748b
commit 20c3184c87
No known key found for this signature in database
GPG Key ID: 2C0867BE60967730
1 changed files with 15 additions and 27 deletions

View File

@ -40,31 +40,23 @@ static void __attribute__((noreturn)) task_fatal_error(void) {
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;
update_partition = esp_ota_get_next_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();
const esp_partition_t *last_invalid = esp_ota_get_last_invalid_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);
@ -74,25 +66,23 @@ void common_hal_ota_flash(const void *buf, const size_t len) {
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) {
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 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();
}
// 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();
}
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();
// 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);
@ -100,7 +90,6 @@ void common_hal_ota_flash(const void *buf, const size_t len) {
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();
@ -111,7 +100,6 @@ void common_hal_ota_flash(const void *buf, const size_t len) {
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) {