Page 1 of 1

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

Posted: Sun Jun 02, 2024 12:59 pm
by A_S_S_G
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.

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

Posted: Mon Jun 03, 2024 1:15 am
by ESP_Sprite
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.

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

Posted: Mon Jun 03, 2024 7:54 am
by A_S_S_G
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...

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

Posted: Tue Jun 04, 2024 12:53 am
by ESP_Sprite
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.