Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 0184258

Browse files
author
Islam Wahdan
authored
Merge pull request #23 from pycom/PYFW-256
Fix for Failing OTA after switching to Factory partition
2 parents 8dc29d1 + ffe4c7f commit 0184258

File tree

3 files changed

+74
-28
lines changed

3 files changed

+74
-28
lines changed

esp32/bootloader/bootloader.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,18 +382,19 @@ static bool find_active_image(bootloader_state_t *bs, esp_partition_pos_t *parti
382382
// if the MD5 fails, then we roll back to the previous image
383383

384384
// do we have a new image that needs to be verified?
385-
if ((boot_info->ActiveImg != IMG_ACT_FACTORY) && (boot_info->Status == IMG_STATUS_CHECK)) {
385+
if (boot_info->Status == IMG_STATUS_CHECK) {
386386
if (boot_info->ActiveImg == IMG_ACT_UPDATE2) {
387387
boot_info->ActiveImg = IMG_ACT_FACTORY; // we only have space for 1 OTA image
388388
}
389389

390390
// verify the active image (ota partition)
391391
esp_image_metadata_t data;
392392
if (ESP_OK != esp_image_load(ESP_IMAGE_VERIFY, &bs->image[boot_info->ActiveImg], &data)) {
393-
ESP_LOGD(TAG, "Switch to the previous image");
394-
// switch to the previous image
393+
ets_printf("Cannot load Firmware img in the active partition! .. Defaulting back to previous partition\n");
394+
// switch to the previous image
395+
uint32_t tempimg = boot_info->ActiveImg;
395396
boot_info->ActiveImg = boot_info->PrevImg;
396-
boot_info->PrevImg = IMG_ACT_FACTORY;
397+
boot_info->PrevImg = tempimg;
397398
}
398399

399400
// in any case, change the status to "READY"

esp32/ftp/updater.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,28 @@ int updater_ota_next_slot_address() {
246246
// check which one should be the next active image
247247
if (updater_read_boot_info (&boot_info, &boot_info_offset)) {
248248
// if we still have an image pending for verification, keep overwriting it
249-
if ((boot_info.Status == IMG_STATUS_CHECK && boot_info.ActiveImg == IMG_ACT_UPDATE2) ||
250-
(boot_info.ActiveImg == IMG_ACT_UPDATE1 && boot_info.Status != IMG_STATUS_CHECK)) {
251-
ota_offset = IMG_UPDATE2_OFFSET;
249+
if (boot_info.Status == IMG_STATUS_CHECK) {
250+
if(boot_info.ActiveImg == IMG_ACT_FACTORY)
251+
252+
{
253+
ota_offset = IMG_FACTORY_OFFSET;
254+
}
255+
else
256+
{
257+
ota_offset = IMG_UPDATE1_OFFSET;
258+
}
259+
}
260+
else
261+
{
262+
if(boot_info.ActiveImg == IMG_ACT_FACTORY)
263+
264+
{
265+
ota_offset = IMG_UPDATE1_OFFSET;
266+
}
267+
else
268+
{
269+
ota_offset = IMG_FACTORY_OFFSET;
270+
}
252271
}
253272
}
254273

esp32/mods/modpycom.c

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static nvs_handle pycom_nvs_handle;
4949
boot_info_t boot_info;
5050
uint32_t boot_info_offset;
5151

52-
static void modpycom_bootmgr(uint8_t boot_partition, uint8_t fs_type, uint8_t safeboot);
52+
static void modpycom_bootmgr(uint8_t boot_partition, uint8_t fs_type, uint8_t safeboot, bool reset);
5353

5454
void modpycom_init0(void) {
5555
if (nvs_open(NVS_NAMESPACE, NVS_READWRITE, &pycom_nvs_handle) != ESP_OK) {
@@ -61,38 +61,60 @@ void modpycom_init0(void) {
6161
}
6262
}
6363

64-
static void modpycom_bootmgr(uint8_t boot_partition, uint8_t fs_type, uint8_t safeboot) {
65-
bool update_boot = false;
64+
static void modpycom_bootmgr(uint8_t boot_partition, uint8_t fs_type, uint8_t safeboot, bool reset) {
65+
bool update_part = false;
66+
bool update_fstype = false;
67+
bool update_safeboot = false;
6668

6769
if (boot_partition < 255) {
68-
if (boot_partition <= 1) {
70+
if ((boot_partition <= IMG_ACT_UPDATE1) && (boot_info.ActiveImg != boot_partition)) {
71+
boot_info.PrevImg = boot_info.ActiveImg;
6972
boot_info.ActiveImg = (uint32_t)boot_partition;
70-
boot_info.Status = 0;
71-
update_boot = true;
73+
boot_info.Status = IMG_STATUS_CHECK;
74+
update_part = true;
7275
} else {
73-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Error invalid boot partition!"));
76+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Error invalid boot partition! or partition already active!"));
7477
}
7578
}
7679
if (safeboot < 255) {
77-
if (safeboot <= 1) {
78-
boot_info.safeboot = (uint32_t)safeboot;
79-
update_boot = true;
80+
if ((safeboot <= 1) && (safeboot != boot_info.safeboot)) {
81+
if(safeboot)
82+
{
83+
boot_info.safeboot = (uint32_t)SAFE_BOOT_SW;
84+
}
85+
else
86+
{
87+
boot_info.safeboot = (uint32_t)0x00;
88+
}
89+
update_safeboot = true;
8090
} else {
8191
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Error safeboot must be True or False!"));
8292
}
8393
}
8494
if (fs_type < 255) {
8595
if (fs_type <= 1) {
86-
config_set_boot_fs_type(fs_type);
96+
if(config_get_boot_fs_type() != fs_type)
97+
{
98+
config_set_boot_fs_type(fs_type);
99+
update_fstype = true;
100+
}
87101
}
88102
else {
89103
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Error invalid filesystem type!"));
90104
}
91105
}
92-
if (update_boot) {
106+
if (update_part || update_safeboot) {
93107
if (updater_write_boot_info (&boot_info, boot_info_offset) == false) {
94108
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Error writing bootloader information!"));
95109
}
110+
if(update_part)
111+
{
112+
machine_reset();
113+
}
114+
}
115+
if((update_fstype || update_safeboot) && reset)
116+
{
117+
machine_reset();
96118
}
97119
}
98120

@@ -338,7 +360,7 @@ STATIC mp_obj_t mod_pycom_lte_modem_on_boot (mp_uint_t n_args, const mp_obj_t *a
338360
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_pycom_lte_modem_on_boot_obj, 0, 1, mod_pycom_lte_modem_on_boot);
339361

340362
STATIC mp_obj_t mod_pycom_bootmgr (size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
341-
enum { ARG_boot_partition, ARG_fs_type, ARG_safeboot, ARG_reset };
363+
enum { ARG_boot_partition, ARG_fs_type, ARG_safeboot, ARG_status };
342364
STATIC const mp_arg_t allowed_args[] = {
343365
{ MP_QSTR_boot_partition, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 255} },
344366
{ MP_QSTR_fs_type, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 255} },
@@ -350,7 +372,7 @@ STATIC mp_obj_t mod_pycom_bootmgr (size_t n_args, const mp_obj_t *pos_args, mp_m
350372
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
351373

352374
if (args[ARG_boot_partition].u_int == 255 && args[ARG_fs_type].u_int == 255 && args[ARG_safeboot].u_int == 255) {
353-
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
375+
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(4, NULL));
354376

355377
if(boot_info.ActiveImg == 0x00)
356378
{
@@ -372,21 +394,25 @@ STATIC mp_obj_t mod_pycom_bootmgr (size_t n_args, const mp_obj_t *pos_args, mp_m
372394

373395
if(boot_info.safeboot == 0x00)
374396
{
375-
t->items[ARG_safeboot] = mp_obj_new_str("False", strlen("False"));
397+
t->items[ARG_safeboot] = mp_obj_new_str("SafeBoot: False", strlen("SafeBoot: False"));
376398
}
377399
else
378400
{
379-
t->items[ARG_safeboot] = mp_obj_new_str("True", strlen("True"));
401+
t->items[ARG_safeboot] = mp_obj_new_str("SafeBoot: True", strlen("SafeBoot: True"));
402+
}
403+
if(boot_info.Status == 0x00)
404+
{
405+
t->items[ARG_status] = mp_obj_new_str("Status: Check", strlen("Status: Check"));
406+
}
407+
else
408+
{
409+
t->items[ARG_status] = mp_obj_new_str("Status: Ready", strlen("Status: Ready"));
380410
}
381411

382412
return MP_OBJ_FROM_PTR(t);
383413

384414
} else {
385-
modpycom_bootmgr(args[ARG_boot_partition].u_int, args[ARG_fs_type].u_int, args[ARG_safeboot].u_int);
386-
387-
if (args[ARG_reset].u_bool == true) {
388-
machine_reset();
389-
}
415+
modpycom_bootmgr(args[ARG_boot_partition].u_int, args[ARG_fs_type].u_int, args[ARG_safeboot].u_int, args[3].u_bool);
390416
}
391417

392418
return mp_const_none;

0 commit comments

Comments
 (0)