|
26 | 26 |
|
27 | 27 | #include "shared-bindings/ota/__init__.h"
|
28 | 28 |
|
| 29 | +#include <string.h> |
| 30 | + |
| 31 | +#include "esp_log.h" |
| 32 | +#include "esp_ota_ops.h" |
| 33 | + |
| 34 | +static const char *TAG = "OTA"; |
| 35 | + |
| 36 | +static void __attribute__((noreturn)) task_fatal_error(void) { |
| 37 | + ESP_LOGE(TAG, "Exiting task due to fatal error..."); |
| 38 | + mp_raise_RuntimeError(translate("OTA Update Failed")); |
| 39 | +} |
| 40 | + |
29 | 41 | void common_hal_ota_flash(const void *buf, const size_t len) {
|
| 42 | + esp_err_t err; |
| 43 | + /* update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */ |
| 44 | + esp_ota_handle_t update_handle = 0 ; |
| 45 | + const esp_partition_t *update_partition = NULL; |
| 46 | + |
| 47 | + ESP_LOGI(TAG, "Starting update"); |
| 48 | + |
| 49 | + const esp_partition_t *configured = esp_ota_get_boot_partition(); |
| 50 | + const esp_partition_t *running = esp_ota_get_running_partition(); |
| 51 | + |
| 52 | + if (configured != running) { |
| 53 | + ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08x, but running from offset 0x%08x", |
| 54 | + configured->address, running->address); |
| 55 | + ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)"); |
| 56 | + } |
| 57 | + ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)", |
| 58 | + running->type, running->subtype, running->address); |
| 59 | + |
| 60 | + update_partition = esp_ota_get_next_update_partition(NULL); |
| 61 | + ESP_LOGI(TAG, "Writing partition type %d subtype %d (offset 0x%08x)\n", |
| 62 | + update_partition->type, update_partition->subtype, update_partition->address); |
| 63 | + assert(update_partition != NULL); |
| 64 | + |
| 65 | + |
| 66 | + if (len > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) { |
| 67 | + // check current version with downloading |
| 68 | + esp_app_desc_t new_app_info; |
| 69 | + memcpy(&new_app_info, &((char *)buf)[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t)); |
| 70 | + ESP_LOGI(TAG, "New firmware version: %s", new_app_info.version); |
| 71 | + |
| 72 | + esp_app_desc_t running_app_info; |
| 73 | + if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) { |
| 74 | + ESP_LOGI(TAG, "Running firmware version: %s", running_app_info.version); |
| 75 | + } |
| 76 | + |
| 77 | + const esp_partition_t* last_invalid_app = esp_ota_get_last_invalid_partition(); |
| 78 | + esp_app_desc_t invalid_app_info; |
| 79 | + if (esp_ota_get_partition_description(last_invalid_app, &invalid_app_info) == ESP_OK) { |
| 80 | + ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version); |
| 81 | + } |
| 82 | + |
| 83 | + // check current version with last invalid partition |
| 84 | + if (last_invalid_app != NULL) { |
| 85 | + if (memcmp(invalid_app_info.version, new_app_info.version, sizeof(new_app_info.version)) == 0) { |
| 86 | + ESP_LOGW(TAG, "New version is the same as invalid version."); |
| 87 | + ESP_LOGW(TAG, "Previously, there was an attempt to launch the firmware with %s version, but it failed.", invalid_app_info.version); |
| 88 | + ESP_LOGW(TAG, "The firmware has been rolled back to the previous version."); |
| 89 | + task_fatal_error(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (memcmp(new_app_info.version, running_app_info.version, sizeof(new_app_info.version)) == 0) { |
| 94 | + ESP_LOGW(TAG, "Current running version is the same as a new. We will not continue the update."); |
| 95 | + task_fatal_error(); |
| 96 | + } |
| 97 | + |
| 98 | + err = esp_ota_begin(update_partition, OTA_WITH_SEQUENTIAL_WRITES, &update_handle); |
| 99 | + if (err != ESP_OK) { |
| 100 | + ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err)); |
| 101 | + task_fatal_error(); |
| 102 | + } |
| 103 | + ESP_LOGI(TAG, "esp_ota_begin succeeded"); |
| 104 | + } else { |
| 105 | + ESP_LOGE(TAG, "received package is not fit len"); |
| 106 | + task_fatal_error(); |
| 107 | + } |
| 108 | + |
| 109 | + err = esp_ota_write( update_handle, buf, len); |
| 110 | + if (err != ESP_OK) { |
| 111 | + ESP_LOGE(TAG, "esp_ota_write failed (%s)", esp_err_to_name(err)); |
| 112 | + task_fatal_error(); |
| 113 | + } |
| 114 | + ESP_LOGI(TAG, "Total Write binary data length: %d", len); |
| 115 | + |
| 116 | + err = esp_ota_end(update_handle); |
| 117 | + if (err != ESP_OK) { |
| 118 | + if (err == ESP_ERR_OTA_VALIDATE_FAILED) { |
| 119 | + ESP_LOGE(TAG, "Image validation failed, image is corrupted"); |
| 120 | + } |
| 121 | + ESP_LOGE(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err)); |
| 122 | + task_fatal_error(); |
| 123 | + } |
30 | 124 |
|
| 125 | + err = esp_ota_set_boot_partition(update_partition); |
| 126 | + if (err != ESP_OK) { |
| 127 | + ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err)); |
| 128 | + task_fatal_error(); |
| 129 | + } |
31 | 130 | }
|
0 commit comments