[SOLVED] ESP_ERR_INVALID_ARG during OTA update

filo_gr
Posts: 110
Joined: Wed Jul 28, 2021 12:25 pm
Location: Italy

[SOLVED] ESP_ERR_INVALID_ARG during OTA update

Postby filo_gr » Sun Mar 05, 2023 3:00 pm

Hi community,

I'm trying to update my ESP32 through OTA by downloading a file from my Google Drive.
When I start the OTA update, the system starts to write in the partition but then it quits.
I'm sharing a link to google drive that works as direct download link (tested).

I use an ESP32-WROOM-32 with 4MB of RAM, 1 factory and 2 OTA partition table. ESP-IDF v4.4.

Here I attach my code:
  1. static esp_err_t
  2. client_event_handler (esp_http_client_event_t * p_evt)
  3. {
  4.     switch (p_evt->event_id)
  5.     {
  6.         case HTTP_EVENT_ERROR:
  7.             ESP_LOGI(TAG, "HTTP_EVENT_ERROR");
  8.         break;
  9.  
  10.         case HTTP_EVENT_ON_CONNECTED:
  11.             ESP_LOGI(TAG, "HTTP_EVENT_ON_CONNECTED");
  12.         break;
  13.  
  14.         case HTTP_EVENT_HEADERS_SENT:
  15.             ESP_LOGI(TAG, "HTTP_EVENT_HEADERS_SENT");
  16.         break;
  17.  
  18.         case HTTP_EVENT_ON_HEADER:
  19.             ESP_LOGI(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", p_evt->header_key, p_evt->header_value);
  20.         break;
  21.  
  22.         case HTTP_EVENT_ON_DATA:
  23.             ESP_LOGI(TAG, "HTTP_EVENT_ON_DATA, len=%d", p_evt->data_len);
  24.         break;
  25.  
  26.         case HTTP_EVENT_ON_FINISH:
  27.             ESP_LOGI(TAG, "HTTP_EVENT_ON_FINISH");
  28.         break;
  29.  
  30.         case HTTP_EVENT_DISCONNECTED:
  31.             ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
  32.         break;
  33.     }
  34.  
  35.     return ESP_OK;
  36. }
  37.  
  38. static void
  39. task_ota (void * p_param)
  40. {
  41.     for (;;)
  42.     {
  43.         xSemaphoreTake(gh_ota_semaphore, portMAX_DELAY);
  44.         ESP_LOGI(TAG, "Invoking OTA");
  45.  
  46.         esp_http_client_config_t client_config = {
  47.             .url = "https://drive.google.com/uc?authuser=0&id=1wnJTjI31OjusFE5X4pqWlO_-GpFpfh3c&export=download",
  48.             .event_handler = client_event_handler,
  49.             .cert_pem = (char *) g_server_cert_pem_start,
  50.         };
  51.  
  52.         esp_err_t ret = esp_https_ota(&client_config);
  53.  
  54.         if (ESP_OK == ret)
  55.         {
  56.             ESP_LOGI(TAG, "OTA flash successful for version %d", g_software_version);
  57.             printf("restarting in 5 seconds\n");
  58.             vTaskDelay(5000 / portTICK_PERIOD_MS);
  59.             esp_restart();
  60.         }
  61.         else
  62.         {
  63.             ESP_LOGE(TAG, "Failed to update the firmare, error %s", esp_err_to_name(ret));
  64.         }
  65.     }
  66. }
The error given by the function esp_https_ota() is ESP_ERR_INVALID_ARG. From the documentation I'm not really sure I understood what is, but looking at the code inside this funcion I discovered that this error is returned by esp_https_ota_finish().

Also, it starts writing to partition, but I don't see any HTTP_EVENT_ON_DATA in my client_event_handler(), so I think there is something strange when I try to receive the data.

Code: Select all

I (987) wifi: connected
W (997) wifi:<ba-add>idx:0 (ifx:0, 54:af:97:96:fd:14), tid:0, ssn:2, winSize:64
I (1067) wifi:AP's beacon interval = 102400 us, DTIM period = 1
I (2097) wifi: got ip
I (2097) esp_netif_handlers: sta ip: 192.168.1.101, mask: 255.255.255.0, gw: 192.168.1.1
I (28657) OTA: Invoking OTA
W (29537) wifi:<ba-add>idx:1 (ifx:0, 54:af:97:96:fd:14), tid:4, ssn:0, winSize:64
I (32967) OTA: HTTP_EVENT_ON_CONNECTED
I (32967) OTA: HTTP_EVENT_HEADERS_SENT
I (33637) OTA: HTTP_EVENT_ON_HEADER, key=Content-Type, value=application/binary
I (33637) OTA: HTTP_EVENT_ON_HEADER, key=Cache-Control, value=no-cache, no-store, max-age=0, must-revalidate
I (33647) OTA: HTTP_EVENT_ON_HEADER, key=Pragma, value=no-cache
I (33657) OTA: HTTP_EVENT_ON_HEADER, key=Expires, value=Mon, 01 Jan 1990 00:00:00 GMT
I (33667) OTA: HTTP_EVENT_ON_HEADER, key=Date, value=Sun, 05 Mar 2023 14:46:30 GMT
I (33667) OTA: HTTP_EVENT_ON_HEADER, key=Location, value=https://doc-14-20-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/6ir6a96hmgvcs9brg6n22srp2sbkm7u0/1678027575000/09062185099277447438/*/1wnJTjI31OjusFE5X4pqWlO_-GpFpfh3c?e=download&uuid=49b47a62-a14f-4620-96b2-8c6d7411b5d4
I (33697) OTA: HTTP_EVENT_ON_HEADER, key=Strict-Transport-Security, value=max-age=31536000
I (33707) OTA: HTTP_EVENT_ON_HEADER, key=Content-Security-Policy, value=require-trusted-types-for 'script';report-uri /_/DriveUntrustedContentHttp/cspreport
I (33727) OTA: HTTP_EVENT_ON_HEADER, key=Content-Security-Policy, value=script-src 'report-sample' 'nonce-XMIgXLmtSKVl__jItA-H2g' 'unsafe-inline';object-src 'none';base-uri 'self';report-uri /_/DriveUntrustedContentHttp/cspreport;worker-src 'self'
I (33747) OTA: HTTP_EVENT_ON_HEADER, key=Cross-Origin-Opener-Policy, value=same-origin
I (33757) OTA: HTTP_EVENT_ON_HEADER, key=Permissions-Policy, value=ch-ua-arch=*, ch-ua-bitness=*, ch-ua-full-version=*, ch-ua-full-version-list=*, ch-ua-model=*, ch-ua-wow64=*, ch-ua-platform=*, ch-ua-platform-version=*
I (33777) OTA: HTTP_EVENT_ON_HEADER, key=Accept-CH, value=Sec-CH-UA-Arch, Sec-CH-UA-Bitness, Sec-CH-UA-Full-Version, Sec-CH-UA-Full-Version-List, Sec-CH-UA-Model, Sec-CH-UA-WoW64, Sec-CH-UA-Platform, Sec-CH-UA-Platform-Version
I (33797) OTA: HTTP_EVENT_ON_HEADER, key=Server, value=ESF
I (33797) OTA: HTTP_EVENT_ON_HEADER, key=Content-Length, value=0
I (33807) OTA: HTTP_EVENT_ON_HEADER, key=X-XSS-Protection, value=0
I (33817) OTA: HTTP_EVENT_ON_HEADER, key=X-Frame-Options, value=SAMEORIGIN
I (33827) OTA: HTTP_EVENT_ON_HEADER, key=X-Content-Type-Options, value=nosniff
I (33827) OTA: HTTP_EVENT_ON_HEADER, key=Alt-Svc, value=h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
I (33837) esp_https_ota: Starting OTA...
I (33847) esp_https_ota: Writing to partition subtype 16 at offset 0x110000
I (33857) OTA: HTTP_EVENT_DISCONNECTED
I (33867) OTA: HTTP_EVENT_DISCONNECTED
E (33867) OTA: Failed to update the firmare, error ESP_ERR_INVALID_ARG
For an additional test, I used a certificate bundle instead of a single certificate, but the error persists. Maybe something related to the Google Drive platform?
Last edited by filo_gr on Mon Mar 27, 2023 12:11 pm, edited 1 time in total.
Filippo

filo_gr
Posts: 110
Joined: Wed Jul 28, 2021 12:25 pm
Location: Italy

Re: ESP_ERR_INVALID_ARG during OTA update

Postby filo_gr » Sun Mar 05, 2023 8:16 pm

I switched to OneDrive and here the problem is not present.

Has anyone encountered similar problems with Google Drive?
Filippo

ESP_Mahavir
Posts: 190
Joined: Wed Jan 24, 2018 6:51 am

Re: ESP_ERR_INVALID_ARG during OTA update

Postby ESP_Mahavir » Mon Mar 06, 2023 3:18 am

This could be specific to the download URL embedded in the example, please confirm that URL is direct download link. Please check this issue: https://github.com/espressif/esp-idf/issues/8581

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

Re: ESP_ERR_INVALID_ARG during OTA update

Postby ESP_Sprite » Mon Mar 06, 2023 4:02 am

Yep:

Code: Select all

I (33667) OTA: HTTP_EVENT_ON_HEADER, key=Location, value=https://doc-14-20-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/6ir6a96hmgvcs9brg6n22srp2sbkm7u0/1678027575000/09062185099277447438/*/1wnJTjI31OjusFE5X4pqWlO_-GpFpfh3c?e=download&uuid=49b47a62-a14f-4620-96b2-8c6d7411b5d4
Seems Google is redirecting you somewhere else.

filo_gr
Posts: 110
Joined: Wed Jul 28, 2021 12:25 pm
Location: Italy

Re: ESP_ERR_INVALID_ARG during OTA update

Postby filo_gr » Mon Mar 06, 2023 7:59 am

ESP_Mahavir wrote:
Mon Mar 06, 2023 3:18 am
This could be specific to the download URL embedded in the example, please confirm that URL is direct download link. Please check this issue: https://github.com/espressif/esp-idf/issues/8581
They talk about a patch for ESP-IDF v5.0. I think it is a similar problem but as ESP_Sprite stated:
ESP_Sprite wrote: Seems Google is redirecting you somewhere else.
I created a direct link URL. Unfortunately Google Drive (as well as One Drive) doesn't release a direct link URL so I followed the instructions in a website to change it.
The link I provide in client_config directly downloads the file (you can try) but I should analize if Google redirects me before the final download.
On the other hand, I though the (eventual) redirect would not a problem if it is performed automatically by the website.
Filippo

Who is online

Users browsing this forum: Baidu [Spider] and 225 guests