Copy the image from partition to partition

aygh4266
Posts: 23
Joined: Mon Mar 04, 2024 10:33 am

Copy the image from partition to partition

Postby aygh4266 » Wed May 29, 2024 11:35 am

Hello Everyone,

i am trying to copy a binary image located on a data fat partition to an app partition to set the boot from the app partition.
My Idea is to allocate a buffer and read and write the image in loop. Unfortunately is the copied image invalid and i keep getting these log messages:
E (3024) esp_image: image at 0x110000 has invalid magic byte (nothing flashed here?)
E (3034) example_main: Failed to set boot partition to OTA_0: ESP_ERR_OTA_VALIDATE_FAILED

Here is the code:
  1.  void copy_image_to_ota0() {
  2.     const esp_partition_t *storage_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, "storage");
  3.     if (!storage_partition) {
  4.         ESP_LOGE(TAG, "Storage partition not found");
  5.         return;
  6.     }
  7.  
  8.     const esp_partition_t *ota_0_partition = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_0, NULL);
  9.     if (!ota_0_partition) {
  10.         ESP_LOGE(TAG, "OTA_0 partition not found");
  11.         return;
  12.     }
  13.  
  14.     if (storage_partition->size > ota_0_partition->size) {
  15.         ESP_LOGE(TAG, "Storage partition is larger than OTA_0 partition");
  16.         return;
  17.     }
  18.  
  19.     ESP_LOGI(TAG, "Copying image from storage to OTA_0 partition");
  20.     const int BUFFER_SIZE = 4096;
  21.     void *buffer = malloc(BUFFER_SIZE);
  22.     if (!buffer) {
  23.         ESP_LOGE(TAG, "Failed to allocate memory for buffer");
  24.         return;
  25.     }
  26.  
  27.     size_t offset = 0;
  28.     esp_err_t err;
  29.     while (offset < storage_partition->size) {
  30.         size_t bytes_to_copy = MIN(BUFFER_SIZE, storage_partition->size - offset);
  31.        
  32.         err = esp_partition_read(storage_partition, offset, buffer, bytes_to_copy);
  33.         if (err != ESP_OK) {
  34.             ESP_LOGE(TAG, "Failed to read from storage partition at offset 0x%08x: %s", offset, esp_err_to_name(err));
  35.             free(buffer);
  36.             return;
  37.         }
  38.  
  39.         err = esp_partition_write(ota_0_partition, offset, buffer, bytes_to_copy);
  40.         if (err != ESP_OK) {
  41.             ESP_LOGE(TAG, "Failed to write to OTA_0 partition at offset 0x%08x: %s", offset, esp_err_to_name(err));
  42.             free(buffer);
  43.             return;
  44.         }
  45.  
  46.         ESP_LOGD(TAG, "Copied %d bytes from offset 0x%08x", bytes_to_copy, offset);
  47.         offset += bytes_to_copy;
  48.     }
  49.  
  50.     ESP_LOGI(TAG, "Image copied successfully to OTA_0 partition");
  51.     free(buffer);
  52. }

liaifat85
Posts: 200
Joined: Wed Dec 06, 2023 2:46 pm

Re: Copy the image from partition to partition

Postby liaifat85 » Wed May 29, 2024 3:19 pm

You can try this edited version of your code:

Code: Select all

#include "esp_partition.h"
#include "esp_log.h"
#include "esp_ota_ops.h"
#include <stdlib.h>

#define TAG "example_main"

void copy_image_to_ota0() {
    const esp_partition_t *storage_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, "storage");
    if (!storage_partition) {
        ESP_LOGE(TAG, "Storage partition not found");
        return;
    }

    const esp_partition_t *ota_0_partition = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_0, NULL);
    if (!ota_0_partition) {
        ESP_LOGE(TAG, "OTA_0 partition not found");
        return;
    }

    if (storage_partition->size > ota_0_partition->size) {
        ESP_LOGE(TAG, "Storage partition is larger than OTA_0 partition");
        return;
    }

    ESP_LOGI(TAG, "Copying image from storage to OTA_0 partition");
    const int BUFFER_SIZE = 4096;
    void *buffer = malloc(BUFFER_SIZE);
    if (!buffer) {
        ESP_LOGE(TAG, "Failed to allocate memory for buffer");
        return;
    }

    size_t offset = 0

MicroController
Posts: 1701
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Copy the image from partition to partition

Postby MicroController » Wed May 29, 2024 8:32 pm

a binary image located on a data fat partition
Is there a FAT filesystem on this partition?
How do you get the image into this storage partition?

aygh4266
Posts: 23
Joined: Mon Mar 04, 2024 10:33 am

Re: Copy the image from partition to partition

Postby aygh4266 » Thu Jun 20, 2024 8:58 am

MicroController wrote:
Wed May 29, 2024 8:32 pm
a binary image located on a data fat partition
Is there a FAT filesystem on this partition?
How do you get the image into this storage partition?
Yes, there is a FAT Parition in the partition table.csv
By Configuring the esp32s3 to act like Mass Storage device. So I can send the binary image direct from my PC or smartphone using USB OTG.

MicroController
Posts: 1701
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Copy the image from partition to partition

Postby MicroController » Thu Jun 20, 2024 11:17 am

You can't copy a FAT filesystem into an OTA partition and expect the FAT to be a valid firmware image.

User avatar
ok-home
Posts: 78
Joined: Sun May 02, 2021 7:23 pm
Location: Russia Novosibirsk
Contact:

Re: Copy the image from partition to partition

Postby ok-home » Thu Jun 20, 2024 1:58 pm

Hi,
why can't the OTA API
esp_ota_begin/esp_ota_write/esp_ota_end
be used with fread ?

Who is online

Users browsing this forum: Bing [Bot] and 119 guests