Skip to content

Commit ed5add3

Browse files
committed
add ability to flash in continuous chunks
1 parent dee86a0 commit ed5add3

File tree

2 files changed

+97
-49
lines changed

2 files changed

+97
-49
lines changed

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

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@
3131
#include "esp_log.h"
3232
#include "esp_ota_ops.h"
3333

34-
esp_ota_handle_t update_handle = 0 ;
35-
const esp_partition_t *update_partition = NULL;
34+
static const esp_partition_t *update_partition = NULL;
35+
static esp_ota_handle_t update_handle = 0;
3636

37+
static bool ota_inited = false;
3738
static const char *TAG = "OTA";
3839

40+
static void ota_reset(void) {
41+
update_handle = 0;
42+
update_partition = NULL;
43+
ota_inited = false;
44+
}
45+
3946
static void __attribute__((noreturn)) task_fatal_error(void) {
4047
ESP_LOGE(TAG, "Exiting task due to fatal error...");
4148
mp_raise_RuntimeError(translate("OTA Update Failed"));
@@ -44,56 +51,61 @@ static void __attribute__((noreturn)) task_fatal_error(void) {
4451
void common_hal_ota_flash(const void *buf, const size_t len) {
4552
esp_err_t err;
4653

47-
update_partition = esp_ota_get_next_update_partition(NULL);
48-
4954
const esp_partition_t *running = esp_ota_get_running_partition();
5055
const esp_partition_t *last_invalid = esp_ota_get_last_invalid_partition();
5156

52-
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)",
53-
running->type, running->subtype, running->address);
57+
if (update_partition == NULL) {
58+
update_partition = esp_ota_get_next_update_partition(NULL);
5459

55-
ESP_LOGI(TAG, "Writing partition type %d subtype %d (offset 0x%08x)\n",
56-
update_partition->type, update_partition->subtype, update_partition->address);
60+
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)",
61+
running->type, running->subtype, running->address);
5762

58-
assert(update_partition != NULL);
63+
ESP_LOGI(TAG, "Writing partition type %d subtype %d (offset 0x%08x)\n",
64+
update_partition->type, update_partition->subtype, update_partition->address);
5965

60-
if (len > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) {
61-
esp_app_desc_t new_app_info;
62-
memcpy(&new_app_info, &((char *)buf)[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
63-
ESP_LOGI(TAG, "New firmware version: %s", new_app_info.version);
66+
assert(update_partition != NULL);
67+
}
6468

65-
esp_app_desc_t running_app_info;
66-
if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) {
67-
ESP_LOGI(TAG, "Running firmware version: %s", running_app_info.version);
68-
}
69+
if (!ota_inited) {
70+
if (len > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) {
71+
esp_app_desc_t new_app_info;
72+
memcpy(&new_app_info, &((char *)buf)[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
73+
ESP_LOGI(TAG, "New firmware version: %s", new_app_info.version);
6974

70-
esp_app_desc_t invalid_app_info;
71-
if (esp_ota_get_partition_description(last_invalid, &invalid_app_info) == ESP_OK) {
72-
ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version);
73-
}
75+
esp_app_desc_t running_app_info;
76+
if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) {
77+
ESP_LOGI(TAG, "Running firmware version: %s", running_app_info.version);
78+
}
7479

75-
// check new version with running version
76-
if (memcmp(new_app_info.version, running_app_info.version, sizeof(new_app_info.version)) == 0) {
77-
ESP_LOGW(TAG, "New version is the same as running version.");
78-
task_fatal_error();
79-
}
80+
esp_app_desc_t invalid_app_info;
81+
if (esp_ota_get_partition_description(last_invalid, &invalid_app_info) == ESP_OK) {
82+
ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version);
83+
}
8084

81-
// check new version with last invalid partition
82-
if (last_invalid != NULL) {
83-
if (memcmp(new_app_info.version, invalid_app_info.version, sizeof(new_app_info.version)) == 0) {
84-
ESP_LOGW(TAG, "New version is the same as invalid version.");
85+
// check new version with running version
86+
if (memcmp(new_app_info.version, running_app_info.version, sizeof(new_app_info.version)) == 0) {
87+
ESP_LOGW(TAG, "New version is the same as running version.");
8588
task_fatal_error();
8689
}
87-
}
8890

89-
err = esp_ota_begin(update_partition, OTA_WITH_SEQUENTIAL_WRITES, &update_handle);
90-
if (err != ESP_OK) {
91-
ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
91+
// check new version with last invalid partition
92+
if (last_invalid != NULL) {
93+
if (memcmp(new_app_info.version, invalid_app_info.version, sizeof(new_app_info.version)) == 0) {
94+
ESP_LOGW(TAG, "New version is the same as invalid version.");
95+
task_fatal_error();
96+
}
97+
}
98+
99+
err = esp_ota_begin(update_partition, OTA_WITH_SEQUENTIAL_WRITES, &update_handle);
100+
if (err != ESP_OK) {
101+
ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
102+
task_fatal_error();
103+
}
104+
ota_inited = true;
105+
} else {
106+
ESP_LOGE(TAG, "received package is not fit len");
92107
task_fatal_error();
93108
}
94-
} else {
95-
ESP_LOGE(TAG, "received package is not fit len");
96-
task_fatal_error();
97109
}
98110

99111
err = esp_ota_write( update_handle, buf, len);
@@ -104,20 +116,24 @@ void common_hal_ota_flash(const void *buf, const size_t len) {
104116
}
105117

106118
void common_hal_ota_finish(void) {
107-
esp_err_t err;
119+
if (ota_inited) {
120+
esp_err_t err;
108121

109-
err = esp_ota_end(update_handle);
110-
if (err != ESP_OK) {
111-
if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
112-
ESP_LOGE(TAG, "Image validation failed, image is corrupted");
122+
err = esp_ota_end(update_handle);
123+
if (err != ESP_OK) {
124+
if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
125+
ESP_LOGE(TAG, "Image validation failed, image is corrupted");
126+
}
127+
ESP_LOGE(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err));
128+
task_fatal_error();
113129
}
114-
ESP_LOGE(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err));
115-
task_fatal_error();
116-
}
117130

118-
err = esp_ota_set_boot_partition(update_partition);
119-
if (err != ESP_OK) {
120-
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
121-
task_fatal_error();
131+
err = esp_ota_set_boot_partition(update_partition);
132+
if (err != ESP_OK) {
133+
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
134+
task_fatal_error();
135+
}
136+
137+
ota_reset();
122138
}
123139
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_OTA__INIT__H
28+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_OTA__INIT__H
29+
30+
extern void ota_reset(void);
31+
32+
#endif //MICROPY_INCLUDED_ESP32S2_COMMON_HAL_OTA__INIT__H

0 commit comments

Comments
 (0)