OTA update : 2 partitions one with OTA program and second with application program

shirogeek
Posts: 15
Joined: Sun Nov 08, 2020 8:07 pm

OTA update : 2 partitions one with OTA program and second with application program

Postby shirogeek » Wed Sep 22, 2021 9:14 am

Hello,

I wanted to inquire if it was possible to setup a 2 OTA partitions scheme where one contains the simple ota example for instance and its role would be to update then boot on the second partition (my actual application). Is it possible to OTA update just this second partition and for instance reboot the esp32 (from my main application) to the first partition where I can then wait for OTA ?

The way i have used it so far is that my first and second partition are actually the same in size and content (both contain OTA code AND my application code). If it's possible to separate both then I can have the smallest first OTA parition just for update and a second partition with just my application code (and a lot bigger in size). It seems to me that we have three partitions in the case of simple_ota_example : factory, ota_0 and ota_1. When I compile my code and flash it with usb, I assume it goes to factory and later when OTA happens it either goes to ota_0 or ota_1. Why then do we have 3 app partitions and not just let's say factory and ota_0 ?

Does what i'm saying make sense ? I have tried to dissect the example of simple_ota_example but cannot seem to see where the choice of partition to update happens. Also, I would also need to know how to reboot the ESP32 on a specific partition.

Thanks in advance guys

boarchuz
Posts: 566
Joined: Tue Aug 21, 2018 5:28 am

Re: OTA update : 2 partitions one with OTA program and second with application program

Postby boarchuz » Wed Sep 22, 2021 10:07 am

Yes you could have a single OTA app partition and a small factory app partition with the sole purpose of updating and booting the OTA partition.
Why then do we have 3 app partitions and not just let's say factory and ota_0 ?
Typically, the factory app would be a fallback option, while the OTA app partitions alternate as they update and activate each other. You can't have a single OTA app partition if you intend to update while running from that partition because, aside from itself, the factory partition is the only other app partition and that should never be altered.
how to reboot the ESP32 on a specific partition
I don't think IDF has any existing way to one-time boot a different partition from within a running app. Couple ideas:
1. From the OTA app, erase the OTA data partition and reset in order to force the bootloader to boot the factory partition. In the factory app (or bootloader), check if the OTA app is valid and restore the OTA data.
2. Customise the bootloader. For example, from the OTA app, use rtc_retain_mem_t to set a flag indicating that a factory boot is desired; within the bootloader, check/clear the flag and select the boot partition accordingly.

shirogeek
Posts: 15
Joined: Sun Nov 08, 2020 8:07 pm

Re: OTA update : 2 partitions one with OTA program and second with application program

Postby shirogeek » Wed Sep 22, 2021 1:56 pm

Thanks for the reply boarchuz !
I kept looking at this in the meanwhile and understood what you mentioned exactly. I got the example working and i can switch between the factory and OTA app through reboot. The following code snippet (found here : https://www.esp32.com/viewtopic.php?t=4210) allows to boot to factory

Code: Select all

void backtofactory()
{
    esp_partition_iterator_t  pi ;                                  // Iterator for find
    const esp_partition_t*    factory ;                             // Factory partition
    esp_err_t                 err ;
    pi = esp_partition_find ( ESP_PARTITION_TYPE_APP,               // Get partition iterator for
                              ESP_PARTITION_SUBTYPE_APP_FACTORY,    // factory partition
                              "factory" ) ;
    if ( pi == NULL )                                               // Check result
    {
        ESP_LOGE ( "FACTORY", "Failed to find factory partition" ) ;
    }
    else
    {
        factory = esp_partition_get ( pi ) ;                        // Get partition struct
        esp_partition_iterator_release ( pi ) ;                     // Release the iterator
        err = esp_ota_set_boot_partition ( factory ) ;              // Set partition for boot
        if ( err != ESP_OK )                                        // Check error
	{
            ESP_LOGE ( "FACTORY", "Failed to set boot partition" ) ;
	}
	else
	{
            esp_restart() ;                                         // Restart ESP
        }
    }
}
I'm running a 1M factory and 6M OTA APP this way.
Thanks again for all the clarification

chegewara
Posts: 2230
Joined: Wed Jun 14, 2017 9:00 pm

Re: OTA update : 2 partitions one with OTA program and second with application program

Postby chegewara » Wed Sep 22, 2021 3:03 pm

shirogeek wrote: I'm running a 1M factory and 6M OTA APP this way.
Thanks again for all the clarification
You probably will learn this hard way, but as far as i know esp32 wont handle that big application. IIRC limit is around 3MB.

Who is online

Users browsing this forum: Baidu [Spider] and 89 guests