esp_https_ota: fix for checking chip id at start of OTA

This commit is contained in:
Harshit Malpani 2022-01-10 18:32:37 +05:30
parent 63f6fe34ac
commit 74845d89cb
2 changed files with 47 additions and 14 deletions

View File

@ -225,17 +225,8 @@ failure:
return err;
}
esp_err_t esp_https_ota_get_img_desc(esp_https_ota_handle_t https_ota_handle, esp_app_desc_t *new_app_info)
static esp_err_t read_header(esp_https_ota_t *handle)
{
esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
if (handle == NULL || new_app_info == NULL) {
ESP_LOGE(TAG, "esp_https_ota_read_img_desc: Invalid argument");
return ESP_ERR_INVALID_ARG;
}
if (handle->state < ESP_HTTPS_OTA_BEGIN) {
ESP_LOGE(TAG, "esp_https_ota_read_img_desc: Invalid state");
return ESP_FAIL;
}
/*
* `data_read_size` holds number of bytes needed to read complete header.
* `bytes_read` holds number of bytes read.
@ -246,7 +237,7 @@ esp_err_t esp_https_ota_get_img_desc(esp_https_ota_handle_t https_ota_handle, es
* while loop is added to download complete image headers, even if the headers
* are not sent in a single packet.
*/
while (data_read_size > 0 && !esp_https_ota_is_complete_data_received(https_ota_handle)) {
while (data_read_size > 0 && !esp_https_ota_is_complete_data_received((esp_https_ota_handle_t)handle)) {
data_read = esp_http_client_read(handle->http_client,
(handle->ota_upgrade_buf + bytes_read),
data_read_size);
@ -266,10 +257,37 @@ esp_err_t esp_https_ota_get_img_desc(esp_https_ota_handle_t https_ota_handle, es
return ESP_FAIL;
}
handle->binary_file_len = bytes_read;
return ESP_OK;
}
esp_err_t esp_https_ota_get_img_desc(esp_https_ota_handle_t https_ota_handle, esp_app_desc_t *new_app_info)
{
esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
if (handle == NULL || new_app_info == NULL) {
ESP_LOGE(TAG, "esp_https_ota_read_img_desc: Invalid argument");
return ESP_ERR_INVALID_ARG;
}
if (handle->state < ESP_HTTPS_OTA_BEGIN) {
ESP_LOGE(TAG, "esp_https_ota_read_img_desc: Invalid state");
return ESP_FAIL;
}
if (read_header(handle) != ESP_OK) {
return ESP_FAIL;
}
memcpy(new_app_info, &handle->ota_upgrade_buf[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
return ESP_OK;
}
static esp_err_t esp_ota_verify_chip_id(void *arg)
{
esp_image_header_t *data = (esp_image_header_t*)(arg);
if (data->chip_id != CONFIG_IDF_FIRMWARE_CHIP_ID) {
ESP_LOGE(TAG, "Mismatch chip id, expected %d, found %d", CONFIG_IDF_FIRMWARE_CHIP_ID, data->chip_id);
return ESP_ERR_INVALID_VERSION;
}
return ESP_OK;
}
esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle)
{
esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
@ -296,10 +314,25 @@ esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle)
/* In case `esp_https_ota_read_img_desc` was invoked first,
then the image data read there should be written to OTA partition
*/
int binary_file_len = 0;
if (handle->binary_file_len) {
return _ota_write(handle, (const void *)handle->ota_upgrade_buf, handle->binary_file_len);
/*
* Header length gets added to handle->binary_file_len in _ota_write
* Clear handle->binary_file_len to avoid additional 289 bytes in binary_file_len
*/
binary_file_len = handle->binary_file_len;
handle->binary_file_len = 0;
} else {
if (read_header(handle) != ESP_OK) {
return ESP_FAIL;
}
binary_file_len = IMAGE_HEADER_SIZE;
}
/* falls through */
err = esp_ota_verify_chip_id(handle->ota_upgrade_buf);
if (err != ESP_OK) {
return err;
}
return _ota_write(handle, (const void *)handle->ota_upgrade_buf, binary_file_len);
case ESP_HTTPS_OTA_IN_PROGRESS:
data_read = esp_http_client_read(handle->http_client,
handle->ota_upgrade_buf,

View File

@ -300,7 +300,7 @@ def test_examples_protocol_advanced_https_ota_example_random(env, extra_data):
print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + random_bin_name))
dut1.write('https://' + host_ip + ':' + str(server_port) + '/' + random_bin_name)
dut1.expect('esp_ota_ops: OTA image has invalid magic byte', timeout=10)
dut1.expect(re.compile(r'esp_https_ota: Mismatch chip id, expected 0, found \d'), timeout=10)
os.remove(binary_file)
thread1.terminate()