Why OTA update needs to delete the entire flash partition befre update?

A_S_S_G
Posts: 5
Joined: Sun Jun 02, 2024 12:33 pm

Why OTA update needs to delete the entire flash partition befre update?

Postby A_S_S_G » Sun Jun 02, 2024 12:59 pm

Why does esp_ota_begin is erasing the partition before update? why not just overwrite (and only delete the remaining size of the partition when calling esp_ota_end();

Code: Select all

esp_ota_begin(update_partition, firmwareSize, &update_handle);
the documentation here is not very clear about the behavior,
Size of new OTA app image. Partition will be erased in order to receive this size of image. If 0 or OTA_SIZE_UNKNOWN, the entire partition is erased.

ESP_Sprite
Posts: 9724
Joined: Thu Nov 26, 2015 4:08 am

Re: Why OTA update needs to delete the entire flash partition befre update?

Postby ESP_Sprite » Mon Jun 03, 2024 1:15 am

Because of the way flash memory works. A flash write can only ever change bits from an 1 to a 0, never the other way around, so if you would write an OTA image to a partition that already has an OTA image on it, you would get the bitwise logical AND of both images, which normally would be garbage. That is why you erase the OTA partition beforehand: that does a 'bulk' erase which sets all 0 bits to 1 again, so the write afterwards has the correct effect.

A_S_S_G
Posts: 5
Joined: Sun Jun 02, 2024 12:33 pm

Re: Why OTA update needs to delete the entire flash partition befre update?

Postby A_S_S_G » Mon Jun 03, 2024 7:54 am

It doesn't explain why it needs to set the whole partition.
Assuming the packets size are 4096*x bytes. Why not deleting the region of the current packet received?
I can use the OTA_WITH_SEQUENTIAL_WRITES flag but that means I'm assuming all packets received in a incremental order. and not sure that will be the case...

ESP_Sprite
Posts: 9724
Joined: Thu Nov 26, 2015 4:08 am

Re: Why OTA update needs to delete the entire flash partition befre update?

Postby ESP_Sprite » Tue Jun 04, 2024 12:53 am

That packet size of 4096 is an assumption we don't want to make. OTA stuff generally comes from e.g. a webserver or a phone, and there's no guarantee at all what size of packets that sends.

If your situation is different, you can always write your own equivalent of the OTA partition write routines. You'd roughly:
- use esp_ota_get_next_update_partition to find the partition to write to
- receive the data and use esp_partition_erase_range/esp_partition_write to write it to the OTA partition
- optionally use esp_image_verify to check if the image is valid
- use esp_ota_set_boot_partition to set the partition as the one used for the next boot
- reboot the ESP using esp_restart.

Who is online

Users browsing this forum: No registered users and 248 guests