Skip to content

Commit 20c3184

Browse files
committed
re-organize and clean-up
1 parent 6022437 commit 20c3184

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

ports/esp32s2/common-hal/ota/__init__.c

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,23 @@ static void __attribute__((noreturn)) task_fatal_error(void) {
4040

4141
void common_hal_ota_flash(const void *buf, const size_t len) {
4242
esp_err_t err;
43-
/* update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */
43+
4444
esp_ota_handle_t update_handle = 0 ;
4545
const esp_partition_t *update_partition = NULL;
46+
update_partition = esp_ota_get_next_update_partition(NULL);
4647

47-
ESP_LOGI(TAG, "Starting update");
48-
49-
const esp_partition_t *configured = esp_ota_get_boot_partition();
5048
const esp_partition_t *running = esp_ota_get_running_partition();
49+
const esp_partition_t *last_invalid = esp_ota_get_last_invalid_partition();
5150

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-
}
5751
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)",
5852
running->type, running->subtype, running->address);
5953

60-
update_partition = esp_ota_get_next_update_partition(NULL);
6154
ESP_LOGI(TAG, "Writing partition type %d subtype %d (offset 0x%08x)\n",
6255
update_partition->type, update_partition->subtype, update_partition->address);
63-
assert(update_partition != NULL);
6456

57+
assert(update_partition != NULL);
6558

6659
if (len > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) {
67-
// check current version with downloading
6860
esp_app_desc_t new_app_info;
6961
memcpy(&new_app_info, &((char *)buf)[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
7062
ESP_LOGI(TAG, "New firmware version: %s", new_app_info.version);
@@ -74,33 +66,30 @@ void common_hal_ota_flash(const void *buf, const size_t len) {
7466
ESP_LOGI(TAG, "Running firmware version: %s", running_app_info.version);
7567
}
7668

77-
const esp_partition_t* last_invalid_app = esp_ota_get_last_invalid_partition();
7869
esp_app_desc_t invalid_app_info;
79-
if (esp_ota_get_partition_description(last_invalid_app, &invalid_app_info) == ESP_OK) {
70+
if (esp_ota_get_partition_description(last_invalid, &invalid_app_info) == ESP_OK) {
8071
ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version);
8172
}
8273

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) {
74+
// check new version with running version
75+
if (memcmp(new_app_info.version, running_app_info.version, sizeof(new_app_info.version)) == 0) {
76+
ESP_LOGW(TAG, "New version is the same as running version.");
77+
task_fatal_error();
78+
}
79+
80+
// check new version with last invalid partition
81+
if (last_invalid != NULL) {
82+
if (memcmp(new_app_info.version, invalid_app_info.version, sizeof(new_app_info.version)) == 0) {
8683
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.");
8984
task_fatal_error();
9085
}
9186
}
9287

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-
9888
err = esp_ota_begin(update_partition, OTA_WITH_SEQUENTIAL_WRITES, &update_handle);
9989
if (err != ESP_OK) {
10090
ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
10191
task_fatal_error();
10292
}
103-
ESP_LOGI(TAG, "esp_ota_begin succeeded");
10493
} else {
10594
ESP_LOGE(TAG, "received package is not fit len");
10695
task_fatal_error();
@@ -111,7 +100,6 @@ void common_hal_ota_flash(const void *buf, const size_t len) {
111100
ESP_LOGE(TAG, "esp_ota_write failed (%s)", esp_err_to_name(err));
112101
task_fatal_error();
113102
}
114-
ESP_LOGI(TAG, "Total Write binary data length: %d", len);
115103

116104
err = esp_ota_end(update_handle);
117105
if (err != ESP_OK) {

0 commit comments

Comments
 (0)