ESP_Sprite wrote: ↑Tue Jun 16, 2020 2:10 pmFYI, the partition table would be *a* reason why this could not work, but given that you now corrected it and you still have the error, it's not that. Perhaps you can show the code you're using? Maybe that helps us.
Please find the attached snippet
Code: Select all
void ota_task(void *pvParameter)
{
esp_err_t err;
/* update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */
esp_ota_handle_t update_handle = 0;
const esp_partition_t *update_partition = NULL;
ESP_LOGI(TAG, "Starting OTA example");
const esp_partition_t *configured = esp_ota_get_boot_partition();
const esp_partition_t *running = esp_ota_get_running_partition();
if (configured != running)
{
ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08x, but running from offset 0x%08x",
configured->address, running->address);
ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)");
}
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)",
running->type, running->subtype, running->address);
update_partition = esp_ota_get_next_update_partition(NULL);
ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x",
update_partition->subtype, update_partition->address);
assert(update_partition != NULL);
int binary_file_length = 0;
/*deal with all receive packet*/
bool image_header_was_checked = false;
int file_size = 0;
FILE *fd = fopen("/spiffs/firmware.bin", "rb+");
if (fd == NULL)
{
ESP_LOGE(TAG, "Failed to open file for reading");
return;
}
else
{
ESP_LOGI(TAG, "opened a file for reading");
}
struct stat st;
stat("/spiffs/firmware.bin", &st);
file_size = st.st_size;
ESP_LOGI(TAG, "file size %d", file_size);
size_t byteswritten = 0;
char *buf = NULL;
buf = (char *)malloc(1024);
size_t Nbytes;
while (1)
{
while (Nbytes = fread(ota_write_data, 1024, 1, fd), Nbytes > 0)
{
if (Nbytes)
{
byteswritten += 1024;
}
if (byteswritten < 0)
{
ESP_LOGE(TAG, "Error: data read error");
task_fatal_error();
}
else if (Nbytes > 0)
{
if (image_header_was_checked == false)
{
esp_app_desc_t new_app_info;
if (byteswritten > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t))
{
// check current version with downloading
memcpy(&new_app_info, &ota_write_data[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
ESP_LOGI(TAG, "New firmware version: %s", new_app_info.version);
esp_app_desc_t running_app_info;
if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK)
{
ESP_LOGI(TAG, "Running firmware version: %s", running_app_info.version);
}
const esp_partition_t *last_invalid_app = esp_ota_get_last_invalid_partition();
esp_app_desc_t invalid_app_info;
if (esp_ota_get_partition_description(last_invalid_app, &invalid_app_info) == ESP_OK)
{
ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version);
}
// check current version with last invalid partition
if (last_invalid_app != NULL)
{
if (memcmp(invalid_app_info.version, new_app_info.version, sizeof(new_app_info.version)) == 0)
{
ESP_LOGW(TAG, "New version is the same as invalid version.");
ESP_LOGW(TAG, "Previously, there was an attempt to launch the firmware with %s version, but it failed.", invalid_app_info.version);
ESP_LOGW(TAG, "The firmware has been rolled back to the previous version.");
}
}
if (memcmp(new_app_info.version, running_app_info.version, sizeof(new_app_info.version)) == 0)
{
ESP_LOGW(TAG, "Current running version is the same as a new. We will not continue the update.");
// return;
vTaskDelete(NULL);
}
image_header_was_checked = true;
err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
task_fatal_error();
}
ESP_LOGI(TAG, "esp_ota_begin succeeded");
}
else
{
ESP_LOGE(TAG, "received package is not fit len");
task_fatal_error();
}
}
err = esp_ota_write(update_handle, (const void *)ota_write_data, strlen(ota_write_data));
if (err != ESP_OK)
{
task_fatal_error();
}
binary_file_length += byteswritten;
ESP_LOGD(TAG, "Written image length %d", binary_file_length);
}
Nbytes = 0;
}
ESP_LOGI(TAG, "completed");
break;
}
ESP_LOGI(TAG, "Total Write binary data length : %d", binary_file_length);
if (esp_ota_end(update_handle) != ESP_OK)
{
ESP_LOGE(TAG, "esp_ota_end failed%s", esp_err_to_name(esp_ota_end(update_handle)));
task_fatal_error();
}
err = esp_ota_set_boot_partition(update_partition);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
task_fatal_error();
}
ESP_LOGI(TAG, "Prepare to restart system!");
esp_restart();
return;
}