Copy the image from partition to partition

aygh4266
Posts: 36
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:

Code: Untitled.c Select all

 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;
esp_err_t err;
while (offset < storage_partition->size) {
size_t bytes_to_copy = MIN(BUFFER_SIZE, storage_partition->size - offset);

err = esp_partition_read(storage_partition, offset, buffer, bytes_to_copy);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to read from storage partition at offset 0x%08x: %s", offset, esp_err_to_name(err));
free(buffer);
return;
}

err = esp_partition_write(ota_0_partition, offset, buffer, bytes_to_copy);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to write to OTA_0 partition at offset 0x%08x: %s", offset, esp_err_to_name(err));
free(buffer);
return;
}

ESP_LOGD(TAG, "Copied %d bytes from offset 0x%08x", bytes_to_copy, offset);
offset += bytes_to_copy;
}

ESP_LOGI(TAG, "Image copied successfully to OTA_0 partition");
free(buffer);
}

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: 2694
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: 36
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

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: 2694
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: 157
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], Qwantbot and 2 guests