Skip to content

Commit

Permalink
Merge branch 'bugfix/preencrypted_ota_failed_with_partial_download_v5…
Browse files Browse the repository at this point in the history
….1' into 'release/v5.1'

fix(esp_https_ota): fix preencrypted ota failed with pytest server and partial http enabled (v5.1)

See merge request espressif/esp-idf!27354
  • Loading branch information
mahavirj committed Nov 29, 2023
2 parents 4788804 + 0e214b7 commit 3b1d428
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions components/esp_https_ota/include/esp_https_ota.h
Expand Up @@ -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;

Expand Down
20 changes: 18 additions & 2 deletions components/esp_https_ota/src/esp_https_ota.c
Expand Up @@ -53,6 +53,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
};

Expand Down Expand Up @@ -323,6 +324,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) {
Expand All @@ -349,6 +355,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;
Expand Down Expand Up @@ -376,6 +387,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;
Expand Down Expand Up @@ -584,10 +596,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");
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3b1d428

Please sign in to comment.