Back to OTA partition fails

meneldor
Posts: 75
Joined: Mon Dec 25, 2017 7:28 am

Back to OTA partition fails

Postby meneldor » Thu May 24, 2018 12:56 pm

Im using small factory+bigger OTA partitions.
I can switch to factory partition from OTA very easy (using the function posted somewhere here in the forum) but board resets when im trying the opposite. So to be able to boot to factory im using first:

Code: Select all

esptool.py --chip esp32 --port /dev/ttyUSB0 erase_region 0xd000 0x2000
which works as expected and i boot to factory.
This is my function it the factory which im using to set again OTA as next boot:

Code: Select all

void back_to_ota()
{
    const esp_partition_t *next_partition = esp_ota_get_next_update_partition(NULL);
    const esp_partition_t *configured = esp_ota_get_boot_partition();
    const esp_partition_t *running = esp_ota_get_running_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);
    
    if (next_partition == NULL) {
        ESP_LOGE(TAG, "next_partition is NULL!!");
        vTaskDelay(2000 / portTICK_PERIOD_MS);
        return;
    }
    ESP_LOGW(TAG, "esp_ota_set_boot_partition: %s...", next_partition->label);
    esp_err_t err = esp_ota_set_boot_partition(next_partition);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "esp_ota_set_boot_partition failed! err=0x%x", err);
        return;
    }
    ESP_LOGI(TAG, "Done!");
    esp_restart() ; 
}
I've added additional prints for debug. Here is the output:
I (13016) OTA: Running partition type 0 subtype 0 (offset 0x00010000)
W (13016) OTA: esp_ota_set_boot_partition: ota...
ets Jun 8 2016 00:22:57

rst:0x8 (TG1WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5640
load:0x40078000,len:0
ho 12 tail 0 room 4
load:0x40078000,len:15356
entry 0x4007862c
W (30) boot: PRO CPU has been reset by WDT.
W (30) boot: WDT reset info: PRO CPU PC=0x400822ed
0x400822ed: esp_intr_noniram_enable at /home/ubuntu/esp/esp-idf/components/esp32/intr_alloc.c:773

W (31) boot: WDT reset info: APP CPU PC=0x4008e66f
0x4008e66f: multi_heap_assert at /home/ubuntu/esp/esp-idf/components/heap/multi_heap.c:377
(inlined by) merge_adjacent at /home/ubuntu/esp/esp-idf/components/heap/multi_heap.c:239

I (37) boot: ESP-IDF v3.1-dev-1130-g92c469b5 2nd stage bootloader
I (43) boot: compile time 23:51:16
I (48) boot: Enabling RNG early entropy source...
I (53) boot: SPI Speed : 40MHz
I (57) boot: SPI Mode : DIO
I (61) boot: SPI Flash Size : 4MB
I (65) boot: Partition Table:
I (69) boot: ## Label Usage Type ST Offset Length
I (76) boot: 0 nvs WiFi data 01 02 00009000 00004000
I (83) boot: 1 otadata OTA data 01 00 0000d000 00002000
I (91) boot: 2 phy_init RF data 01 01 0000f000 00001000
I (98) boot: 3 factory factory app 00 00 00010000 00100000
I (106) boot: 4 ota OTA app 00 10 00110000 00170000
I (113) boot: 5 storage Unknown data 01 82 00280000 00100000
I (121) boot: End of partition table
I (125) boot: Defaulting to factory image
I (130) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x2c18c (180620) map
I (202) esp_image: segment 1: paddr=0x0003c1b4 vaddr=0x3ffb0000 size=0x038ec ( 14572) load
I (208) esp_image: segment 2: paddr=0x0003faa8 vaddr=0x40080000 size=0x00400 ( 1024) load
0x40080000: _WindowOverflow4 at /home/ubuntu/esp/esp-idf/components/freertos/xtensa_vectors.S:1685

I (209) esp_image: segment 3: paddr=0x0003feb0 vaddr=0x40080400 size=0x00160 ( 352) load
I (218) esp_image: segment 4: paddr=0x00040018 vaddr=0x400d0018 size=0x8f68c (587404) map
0x400d0018: _flash_cache_start at ??:?

I (432) esp_image: segment 5: paddr=0x000cf6ac vaddr=0x40080560 size=0x0ffc4 ( 65476) load
I (460) esp_image: segment 6: paddr=0x000df678 vaddr=0x400c0000 size=0x00000 ( 0) load
I (460) esp_image: segment 7: paddr=0x000df680 vaddr=0x50000000 size=0x00008 ( 8) load
I (477) boot: Loaded app from partition at offset 0x10000
I (477) boot: Disabling RNG early entropy source...
I (478) cpu_start: Pro cpu up.
I (482) cpu_start: Starting app cpu, entry point is 0x40080ffc
0x40080ffc: start_cpu0_default at /home/ubuntu/esp/esp-idf/components/esp32/cpu_start.c:379 (discriminator 9)

I (0) cpu_start: App cpu up.
I (493) heap_init: Initializing. RAM available for dynamic allocation:
I (499) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (505) heap_init: At 3FFBC410 len 00023BF0 (142 KiB): DRAM
I (512) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (518) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (524) heap_init: At 40090524 len 0000FADC (62 KiB): IRAM
I (531) cpu_start: Pro cpu start user code
I (554) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
Start ESP32 OTA: /factory. NVS: espmain
UUID: 31:be:a4:44:4f:ff. Version: 20180524131056/factory
My partition table:

Code: Select all

nvs,data,nvs,0x9000,0x4000,
otadata,data,ota,0xd000,0x2000,
phy_init,data,phy,0xf000,0x1000,
factory,app,factory,0x10000,1M,
ota,app,ota_0,,0x170000,
storage,data,spiffs,,1M,
So it crashes on esp_ota_set_boot_partition(). My understanding is that if i erase otadata but keep ota i could set it as next boot again. Everything is working good except this error. Is there anything i am missing?

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Back to OTA partition fails

Postby ESP_Angus » Fri May 25, 2018 6:41 am

Hi meneldor,

TG1WDT_SYS_RESET in the log suggests that Timer Group 1 watchdog reset the system. By default this is the interrupt watchdog timer.

esp_ota_set_boot_partition() verifies the image before it allows it to be set for booting. Image verification currently uses a lot of SPI flash read operations which can disable interrupts for an extended period.

If you set the Interrupt Watchdog Timer timeout to some higher value (maybe 2000ms), does this code behave as expected?

(If this is the cause then it's a bug, as the watchdog timer shouldn't trigger in this case. But this helps us narrow down what the bug might be.)

meneldor
Posts: 75
Joined: Mon Dec 25, 2017 7:28 am

Re: Back to OTA partition fails

Postby meneldor » Fri May 25, 2018 6:57 am

Thanks for the suggestion, ESP_Angus.
However even 10000 doesnt help. WDT just waits 10 seconds and resets the board again.

meneldor
Posts: 75
Joined: Mon Dec 25, 2017 7:28 am

Re: Back to OTA partition fails

Postby meneldor » Fri May 25, 2018 1:51 pm

UPDATE: I am not sure how but it works now. The function was executed in the main task which saw on the edge of the free memory because i parsed console commands using sstream:

Code: Select all

.....
char data[128];
string arr[4];
int i = 0;
stringstream ssin(data);
while (ssin.good() && i < 4){
      ssin >> arr[i];
      ++i;
}
commands(arr, 4);
....
I replaced the entire c++ code with esp_console, linenoise and argtable3. Now back_to_ota() is working as expected.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Back to OTA partition fails

Postby ESP_Angus » Sun May 27, 2018 11:38 pm

meneldor wrote:UPDATE: I am not sure how but it works now. The function was executed in the main task which saw on the edge of the free memory because i parsed console commands using sstream:
Is it possible there was a stack overflow, as the task didn't have enough stack headroom? Usually even a stack overflow in a critical section (like when writing to flash) should trigger the stack overflow detection when the critical section exits, but there are possible sequences of events where this won't happen.

In any case, I'm glad you got it working.

meneldor
Posts: 75
Joined: Mon Dec 25, 2017 7:28 am

Re: Back to OTA partition fails

Postby meneldor » Mon May 28, 2018 5:00 am

Yep, stack overflow with "canary" message and reset but when i wrote a command in my previous version of console.
Thank again!

Who is online

Users browsing this forum: No registered users and 117 guests