mirror of
https://github.com/ok-home/ota_ws_update.git
synced 2025-11-13 22:03:27 +03:00
preencrypted fixed
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
@@ -16,7 +16,6 @@ openssl genrsa -out rsa_key/private.pem 3072
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#include "esp_ota_ops.h"
|
||||
#include "esp_flash_partitions.h"
|
||||
#include "esp_partition.h"
|
||||
@@ -29,30 +28,30 @@ static const char *TAG = "ota_ws_esp";
|
||||
|
||||
static const esp_partition_t *update_partition = NULL;
|
||||
static bool image_header_was_checked = false;
|
||||
static esp_ota_handle_t update_handle = NULL;
|
||||
static esp_ota_handle_t update_handle = 0;
|
||||
// pre-encrypted handle
|
||||
static esp_decrypt_handle_t enc_handle = NULL; // handle
|
||||
static esp_decrypt_cfg_t enc_cfg = {0}; // cfg
|
||||
static pre_enc_decrypt_arg_t enc_arg = {0}; // arg
|
||||
static esp_decrypt_cfg_t enc_cfg = {0}; // cfg
|
||||
static pre_enc_decrypt_arg_t enc_arg = {0}; // arg
|
||||
|
||||
static int tst_c=0;
|
||||
// static int tst_c=0;
|
||||
|
||||
extern const char rsa_private_pem_start[] asm("_binary_private_pem_start");
|
||||
extern const char rsa_private_pem_end[] asm("_binary_private_pem_end");
|
||||
extern const char rsa_private_pem_end[] asm("_binary_private_pem_end");
|
||||
|
||||
esp_err_t start_ota_ws(void)
|
||||
{
|
||||
//return ESP_OK; // debug return
|
||||
tst_c=0;
|
||||
// return ESP_OK; // debug return
|
||||
// tst_c=0;
|
||||
|
||||
esp_err_t err;
|
||||
ESP_LOGI(TAG, "Starting OTA");
|
||||
|
||||
const esp_partition_t *configured = esp_ota_get_boot_partition();
|
||||
const esp_partition_t *running = esp_ota_get_running_partition();
|
||||
if(configured==NULL || running == NULL)
|
||||
if (configured == NULL || running == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG,"OTA data not found");
|
||||
ESP_LOGE(TAG, "OTA data not found");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
@@ -79,34 +78,34 @@ esp_err_t start_ota_ws(void)
|
||||
image_header_was_checked = false;
|
||||
|
||||
enc_cfg.rsa_priv_key = rsa_private_pem_start;
|
||||
enc_cfg.rsa_priv_key_len = rsa_private_pem_end-rsa_private_pem_start;
|
||||
enc_cfg.rsa_priv_key_len = rsa_private_pem_end - rsa_private_pem_start;
|
||||
|
||||
enc_handle = esp_encrypted_img_decrypt_start(&enc_cfg);
|
||||
if(enc_handle == NULL)
|
||||
if (enc_handle == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "esp_encrypted_img_decrypt_start failed ");
|
||||
abort_ota_ws();
|
||||
return ESP_FAIL;
|
||||
}
|
||||
memset(&enc_arg,0,sizeof(pre_enc_decrypt_arg_t)); //??
|
||||
memset(&enc_arg, 0, sizeof(pre_enc_decrypt_arg_t)); //??
|
||||
ESP_LOGI(TAG, "esp_ota_begin succeeded");
|
||||
return ESP_OK;
|
||||
}
|
||||
esp_err_t write_ota_ws(int enc_data_read, uint8_t *enc_ota_write_data)
|
||||
{
|
||||
//return ESP_OK; // debug return
|
||||
enc_arg.data_in = (char*)enc_ota_write_data;
|
||||
// return ESP_OK; // debug return
|
||||
enc_arg.data_in = (char *)enc_ota_write_data;
|
||||
enc_arg.data_in_len = enc_data_read;
|
||||
esp_err_t ret = esp_encrypted_img_decrypt_data(enc_handle, &enc_arg);
|
||||
ESP_LOGI("OTA ENC ","ret=%x len=%d",ret,enc_arg.data_out_len);
|
||||
if(ret == ESP_FAIL || ret == ESP_ERR_INVALID_ARG)
|
||||
// ESP_LOGI("OTA ENC ","ret=%x len=%d",ret,enc_arg.data_out_len);
|
||||
if (ret == ESP_FAIL || ret == ESP_ERR_INVALID_ARG)
|
||||
{
|
||||
ESP_LOGE(TAG, "data decrypt err %x",ret);
|
||||
abort_ota_ws();
|
||||
return ret;
|
||||
ESP_LOGE(TAG, "data decrypt err %x", ret);
|
||||
abort_ota_ws();
|
||||
return ret;
|
||||
}
|
||||
int data_read = enc_arg.data_out_len;
|
||||
uint8_t *ota_write_data = (uint8_t*)enc_arg.data_out;
|
||||
uint8_t *ota_write_data = (uint8_t *)enc_arg.data_out;
|
||||
|
||||
if (image_header_was_checked == false) // first segment
|
||||
{
|
||||
@@ -126,29 +125,31 @@ esp_err_t write_ota_ws(int enc_data_read, uint8_t *enc_ota_write_data)
|
||||
}
|
||||
}
|
||||
ret = esp_ota_write(update_handle, (const void *)ota_write_data, data_read);
|
||||
tst_c += data_read;
|
||||
ESP_LOGI("OTA WRITE","ret=%x len=%d tst_c=%d",ret,data_read,tst_c);
|
||||
// tst_c += data_read;
|
||||
// ESP_LOGI("OTA WRITE","ret=%x len=%d tst_c=%d",ret,data_read,tst_c);
|
||||
if (ret != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "esp_ota_write err");
|
||||
abort_ota_ws();
|
||||
return ret;
|
||||
}
|
||||
return ESP_OK;
|
||||
return ESP_OK;
|
||||
}
|
||||
esp_err_t end_ota_ws(void)
|
||||
{
|
||||
//return ESP_OK; // debug return
|
||||
// return ESP_OK; // debug return
|
||||
esp_err_t ret = esp_encrypted_img_decrypt_end(enc_handle);
|
||||
if(ret)
|
||||
if (ret)
|
||||
{
|
||||
ESP_LOGE(TAG, "esp_encrypted_img_decrypt_end (%s)!", esp_err_to_name(ret));
|
||||
abort_ota_ws();
|
||||
return ret;
|
||||
}
|
||||
ret = esp_ota_end(update_handle);
|
||||
if (ret != ESP_OK) {
|
||||
if (ret == ESP_ERR_OTA_VALIDATE_FAILED) {
|
||||
if (ret != ESP_OK)
|
||||
{
|
||||
if (ret == ESP_ERR_OTA_VALIDATE_FAILED)
|
||||
{
|
||||
ESP_LOGE(TAG, "Image validation failed, image is corrupted");
|
||||
abort_ota_ws();
|
||||
return ret;
|
||||
@@ -158,25 +159,36 @@ esp_err_t end_ota_ws(void)
|
||||
return ret;
|
||||
}
|
||||
ret = esp_ota_set_boot_partition(update_partition);
|
||||
if (ret != ESP_OK) {
|
||||
if (ret != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(ret));
|
||||
abort_ota_ws();
|
||||
return ret;
|
||||
}
|
||||
if(enc_arg.data_out)
|
||||
{ free(enc_arg.data_out);}
|
||||
return ESP_OK;
|
||||
if (enc_arg.data_out)
|
||||
{
|
||||
free(enc_arg.data_out);
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
esp_err_t abort_ota_ws(void)
|
||||
{
|
||||
if(enc_handle)
|
||||
{esp_encrypted_img_decrypt_abort(enc_handle);}
|
||||
if(update_handle)
|
||||
{esp_ota_abort(update_handle);}
|
||||
if (enc_handle)
|
||||
{
|
||||
ESP_LOGI("abort", "abort cmd");
|
||||
esp_encrypted_img_decrypt_abort(enc_handle);
|
||||
}
|
||||
if (update_handle)
|
||||
{
|
||||
esp_ota_abort(update_handle);
|
||||
}
|
||||
if (enc_arg.data_out)
|
||||
{
|
||||
free(enc_arg.data_out);
|
||||
}
|
||||
enc_handle = NULL;
|
||||
update_handle = NULL;
|
||||
if(enc_arg.data_out)
|
||||
{ free(enc_arg.data_out);}
|
||||
update_handle = 0;
|
||||
memset(&enc_arg, 0, sizeof(pre_enc_decrypt_arg_t));
|
||||
return ESP_OK;
|
||||
}
|
||||
// false - rollback disable
|
||||
@@ -186,21 +198,23 @@ bool check_ota_ws_rollback_enable(void)
|
||||
#ifdef CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE
|
||||
esp_ota_img_states_t ota_state_running_part;
|
||||
const esp_partition_t *running = esp_ota_get_running_partition();
|
||||
if (esp_ota_get_state_partition(running, &ota_state_running_part) == ESP_OK) {
|
||||
if (ota_state_running_part == ESP_OTA_IMG_PENDING_VERIFY) {
|
||||
if (esp_ota_get_state_partition(running, &ota_state_running_part) == ESP_OK)
|
||||
{
|
||||
if (ota_state_running_part == ESP_OTA_IMG_PENDING_VERIFY)
|
||||
{
|
||||
ESP_LOGI(TAG, "Running app has ESP_OTA_IMG_PENDING_VERIFY state");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
// rollback == true - rollback
|
||||
// rollback == false - app valid? confirm update -> no rollback
|
||||
esp_err_t rollback_ota_ws(bool rollback)
|
||||
{
|
||||
#ifdef CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE
|
||||
if(rollback == false)
|
||||
if (rollback == false)
|
||||
{
|
||||
return esp_ota_mark_app_valid_cancel_rollback(); // app valid
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user