From 54bf1e708bda854f65a15514f504fcfe196f3342 Mon Sep 17 00:00:00 2001 From: "nilesh.kale" Date: Wed, 6 Sep 2023 12:35:47 +0530 Subject: [PATCH] fix: fix preencrypted ota failed with pytest server and partial http enabled --- .../esp_https_ota/include/esp_https_ota.h | 1 + components/esp_https_ota/src/esp_https_ota.c | 20 +++++++++++++++++-- .../main/pre_encrypted_ota.c | 2 ++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/components/esp_https_ota/include/esp_https_ota.h b/components/esp_https_ota/include/esp_https_ota.h index da7ad89a5b..af77b07971 100644 --- a/components/esp_https_ota/include/esp_https_ota.h +++ b/components/esp_https_ota/include/esp_https_ota.h @@ -62,6 +62,7 @@ typedef struct { #if CONFIG_ESP_HTTPS_OTA_DECRYPT_CB decrypt_cb_t decrypt_cb; /*!< Callback for external decryption layer */ void *decrypt_user_ctx; /*!< User context for external decryption layer */ + uint16_t enc_img_header_size; /*!< Header size of pre-encrypted ota image header */ #endif } esp_https_ota_config_t; diff --git a/components/esp_https_ota/src/esp_https_ota.c b/components/esp_https_ota/src/esp_https_ota.c index e153a086bf..bbb6626c03 100644 --- a/components/esp_https_ota/src/esp_https_ota.c +++ b/components/esp_https_ota/src/esp_https_ota.c @@ -48,6 +48,7 @@ struct esp_https_ota_handle { #if CONFIG_ESP_HTTPS_OTA_DECRYPT_CB decrypt_cb_t decrypt_cb; void *decrypt_user_ctx; + uint16_t enc_img_header_size; #endif }; @@ -306,6 +307,11 @@ esp_err_t esp_https_ota_begin(const esp_https_ota_config_t *ota_config, esp_http } https_ota_handle->image_length = esp_http_client_get_content_length(https_ota_handle->http_client); +#if CONFIG_ESP_HTTPS_OTA_DECRYPT_CB + /* In case of pre ecnrypted OTA, actual image size of OTA binary includes header size + which stored in variable enc_img_header_size*/ + https_ota_handle->image_length -= ota_config->enc_img_header_size; +#endif esp_http_client_close(https_ota_handle->http_client); if (https_ota_handle->image_length > https_ota_handle->max_http_request_size) { @@ -332,6 +338,11 @@ esp_err_t esp_https_ota_begin(const esp_https_ota_config_t *ota_config, esp_http if (!https_ota_handle->partial_http_download) { https_ota_handle->image_length = esp_http_client_get_content_length(https_ota_handle->http_client); +#if CONFIG_ESP_HTTPS_OTA_DECRYPT_CB + /* In case of pre ecnrypted OTA, actual image size of OTA binary includes header size + which stored in variable enc_img_header_size*/ + https_ota_handle->image_length -= ota_config->enc_img_header_size; +#endif } https_ota_handle->update_partition = NULL; @@ -359,6 +370,7 @@ esp_err_t esp_https_ota_begin(const esp_https_ota_config_t *ota_config, esp_http } https_ota_handle->decrypt_cb = ota_config->decrypt_cb; https_ota_handle->decrypt_user_ctx = ota_config->decrypt_user_ctx; + https_ota_handle->enc_img_header_size = ota_config->enc_img_header_size; #endif https_ota_handle->ota_upgrade_buf_size = alloc_size; https_ota_handle->bulk_flash_erase = ota_config->bulk_flash_erase; @@ -567,10 +579,14 @@ esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle) if (handle->state == ESP_HTTPS_OTA_IN_PROGRESS && handle->image_length > handle->binary_file_len) { esp_http_client_close(handle->http_client); char *header_val = NULL; + int header_size = 0; +#if CONFIG_ESP_HTTPS_OTA_DECRYPT_CB + header_size = handle->enc_img_header_size; +#endif if ((handle->image_length - handle->binary_file_len) > handle->max_http_request_size) { - asprintf(&header_val, "bytes=%d-%d", handle->binary_file_len, (handle->binary_file_len + handle->max_http_request_size - 1)); + asprintf(&header_val, "bytes=%d-%d", handle->binary_file_len + header_size, (handle->binary_file_len + header_size + handle->max_http_request_size - 1)); } else { - asprintf(&header_val, "bytes=%d-", handle->binary_file_len); + asprintf(&header_val, "bytes=%d-", handle->binary_file_len + header_size); } if (header_val == NULL) { ESP_LOGE(TAG, "Failed to allocate memory for HTTP header"); diff --git a/examples/system/ota/pre_encrypted_ota/main/pre_encrypted_ota.c b/examples/system/ota/pre_encrypted_ota/main/pre_encrypted_ota.c index 20f8f153aa..a0a4b75e78 100644 --- a/examples/system/ota/pre_encrypted_ota/main/pre_encrypted_ota.c +++ b/examples/system/ota/pre_encrypted_ota/main/pre_encrypted_ota.c @@ -74,6 +74,7 @@ static esp_err_t _decrypt_cb(decrypt_cb_arg_t *args, void *user_ctx) if (err != ESP_OK && err != ESP_ERR_NOT_FINISHED) { return err; } + static bool is_image_verified = false; if (pargs.data_out_len > 0) { args->data_out = pargs.data_out; @@ -143,6 +144,7 @@ void pre_encrypted_ota_task(void *pvParameter) #endif .decrypt_cb = _decrypt_cb, .decrypt_user_ctx = (void *)decrypt_handle, + .enc_img_header_size = esp_encrypted_img_get_header_size(), }; esp_https_ota_handle_t https_ota_handle = NULL;