esp32_shock/components/twai_ota/twai_ota.c

276 lines
9.5 KiB
C
Raw Normal View History

2024-04-29 17:35:50 +08:00
#include <string.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_ota_ops.h"
#include "esp_app_format.h"
#include "esp_flash_partitions.h"
#include "esp_partition.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "twai_ota.h"
static const char *TAG = "ota";
static void print_sha256 (const uint8_t *image_hash, const char *label)
{
char hash_print[HASH_LEN * 2 + 1];
hash_print[HASH_LEN * 2] = 0;
for (int i = 0; i < HASH_LEN; ++i) {
sprintf(&hash_print[i * 2], "%02x", image_hash[i]);
}
ESP_LOGI(TAG, "%s: %s", label, hash_print);
}
static bool diagnostic(void)
{
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pin_bit_mask = (1ULL << CONFIG_EXAMPLE_GPIO_DIAGNOSTIC);
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
gpio_config(&io_conf);
ESP_LOGI(TAG, "Diagnostics (5 sec)...");
vTaskDelay(5000 / portTICK_PERIOD_MS);
bool diagnostic_is_ok = gpio_get_level(CONFIG_EXAMPLE_GPIO_DIAGNOSTIC);
gpio_reset_pin(CONFIG_EXAMPLE_GPIO_DIAGNOSTIC);
return diagnostic_is_ok;
}
// 在app程序开始需调用用于检验镜像以及是否回滚
void ota_init(void)
{
ESP_LOGI(TAG, "ota_init");
uint8_t sha_256[HASH_LEN] = { 0 };
esp_partition_t partition;
const esp_partition_t *configured = esp_ota_get_boot_partition();
const esp_partition_t *running = esp_ota_get_running_partition();
// get sha256 digest for the partition table
partition.address = ESP_PARTITION_TABLE_OFFSET;
partition.size = ESP_PARTITION_TABLE_MAX_LEN;
partition.type = ESP_PARTITION_TYPE_DATA;
esp_partition_get_sha256(&partition, sha_256);
print_sha256(sha_256, "SHA-256 for the partition table: ");
// get sha256 digest for bootloader
partition.address = ESP_BOOTLOADER_OFFSET;
partition.size = ESP_PARTITION_TABLE_OFFSET;
partition.type = ESP_PARTITION_TYPE_APP;
esp_partition_get_sha256(&partition, sha_256);
print_sha256(sha_256, "SHA-256 for bootloader: ");
// get sha256 digest for running partition
esp_partition_get_sha256(esp_ota_get_running_partition(), sha_256);
print_sha256(sha_256, "SHA-256 for current firmware: ");
esp_ota_img_states_t ota_state;
if (esp_ota_get_state_partition(running, &ota_state) == ESP_OK) {
if (ota_state == ESP_OTA_IMG_PENDING_VERIFY) { // ota写入之后的第一次重启如果enable回滚状态为等待验证
bool diagnostic_is_ok = diagnostic();
if (diagnostic_is_ok) {
ESP_LOGI(TAG, "Diagnostics completed successfully! Continuing execution ...");
esp_ota_mark_app_valid_cancel_rollback();
} else {
ESP_LOGE(TAG, "Diagnostics failed! Start rollback to the previous version ...");
esp_ota_mark_app_invalid_rollback_and_reboot();
}
}
}
// 获取当前运行分区的信息
if (configured != running) {
ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08"PRIx32", but running from offset 0x%08"PRIx32,
configured->address, running->address);
ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)");
}
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08"PRIx32")",
running->type, running->subtype, running->address);
}
ota_ctrl_t ota_ctrl;
static void ota_ctrl_clean(void)
{
ota_ctrl.buf_len = 0;
ota_ctrl.binary_file_length = 0;
}
ota_err_t ota_enter_command(void)
{
memset(&ota_ctrl, 0, sizeof(ota_ctrl_t));
ota_ctrl.img_header_size = sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t);
ota_ctrl.img_header_app_desc_offset = sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t);
ota_ctrl.update_partition = esp_ota_get_next_update_partition(NULL);
if (ota_ctrl.update_partition == NULL)
{
ESP_LOGE(TAG, "Error: no partition found for OTA");
return OTA_GET_PARTITION_ERROR;
}
ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%"PRIx32,
ota_ctrl.update_partition->subtype, ota_ctrl.update_partition->address);
return OTA_OK;
}
ota_err_t ota_image_header_check_and_start(void)
{
esp_err_t err;
// 校验版本版本和当前运行版本或者上一个无效的运行版本相同的话不进行OTA
esp_app_desc_t new_app_info;
memcpy(&new_app_info, &ota_ctrl.buf[ota_ctrl.img_header_app_desc_offset], sizeof(esp_app_desc_t));
ESP_LOGI(TAG, "New firmware version: %s", new_app_info.version);
const esp_partition_t *running = esp_ota_get_running_partition();
esp_app_desc_t running_app_info;
if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) {
ESP_LOGI(TAG, "Running firmware version: %s", running_app_info.version);
}
const esp_partition_t* last_invalid_app = esp_ota_get_last_invalid_partition();
esp_app_desc_t invalid_app_info;
if (esp_ota_get_partition_description(last_invalid_app, &invalid_app_info) == ESP_OK) {
ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version);
}
if (last_invalid_app != NULL) {
if (memcmp(invalid_app_info.version, new_app_info.version, sizeof(new_app_info.version)) == 0) {
ESP_LOGW(TAG, "New version is the same as invalid version.");
ESP_LOGW(TAG, "Previously, there was an attempt to launch the firmware with %s version, but it failed.", invalid_app_info.version);
return OTA_HEADER_CHECK_ERROR;
}
}
if (memcmp(new_app_info.version, running_app_info.version, sizeof(new_app_info.version)) == 0) {
ESP_LOGW(TAG, "Current running version is the same as a new. We will not continue the update.");
return OTA_HEADER_CHECK_ERROR;
}
// 校验成功开启ota
ota_ctrl.image_header_was_checked = true;
err = esp_ota_begin(ota_ctrl.update_partition, OTA_WITH_SEQUENTIAL_WRITES, &ota_ctrl.update_handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
return OTA_BEGIN_FUN_ERROR;
}
ESP_LOGI(TAG, "esp_ota_begin succeeded");
return OTA_OK;
}
static ota_err_t __ota_download_data_in(void)
{
ota_err_t ret = 0;
esp_err_t err;
// 未校验则先校验
if (ota_ctrl.image_header_was_checked == false)
{
ret = ota_image_header_check_and_start();
if (ret != OTA_OK)
{
ESP_LOGE(TAG, "Error: image header check failed");
ota_ctrl_clean();
return ret;
}
}
// 写入分区
err = esp_ota_write(ota_ctrl.update_handle, (const void *)ota_ctrl.buf, ota_ctrl.buf_len);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Error: esp_ota_write failed (%s)!", esp_err_to_name(err));
esp_ota_abort(ota_ctrl.update_handle);
ota_ctrl_clean();
return OTA_WRITE_FUN_ERROR;
}
ota_ctrl.buf_len = 0;
ota_ctrl.binary_file_length += ota_ctrl.buf_len;
ESP_LOGD(TAG, "Written image length %lld", ota_ctrl.binary_file_length);
return OTA_OK;
}
// 内部有OTA_BUFFSIZE大小缓冲区缓冲区满或者结束才会触发ota写入
ota_err_t ota_download_data_in(uint8_t *data, int len)
{
int err = 0;
ota_err_t ret = 0;
int remain_len = 0;
uint8_t *remain_data = NULL;
if (len > OTA_BUFFSIZE)
{
ESP_LOGE(TAG, "Error: len too big, max is %d", OTA_BUFFSIZE);
ota_ctrl_clean();
return OTA_PARAM_ERROR; // 参数问题
}
if (ota_ctrl.buf_len + len >= OTA_BUFFSIZE)
{
remain_data = data + OTA_BUFFSIZE - ota_ctrl.buf_len;
remain_len = ota_ctrl.buf_len + len - OTA_BUFFSIZE;
memcpy(ota_ctrl.buf + ota_ctrl.buf_len, data, OTA_BUFFSIZE - ota_ctrl.buf_len);
ota_ctrl.buf_len = OTA_BUFFSIZE;
ret = __ota_download_data_in();
if (ret != OTA_OK) return ret;
ota_ctrl.buf_len = remain_len;
memcpy(ota_ctrl.buf, remain_data, ota_ctrl.buf_len);
}
return OTA_OK;
}
ota_err_t ota_download_finish_command(void)
{
int err = 0;
ota_err_t ret = 0;
// 写入剩下的数据
ret = __ota_download_data_in();
if (ret != OTA_OK) return ret;
ESP_LOGI(TAG, "Total Write binary data length: %lld", ota_ctrl.binary_file_length);
// 下载成功,进行收尾工作
err = esp_ota_end(ota_ctrl.update_handle);
if (err != ESP_OK) {
if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
ESP_LOGE(TAG, "Image validation failed, image is corrupted");
} else {
ESP_LOGE(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err));
}
ota_ctrl_clean();
return OTA_END_FUN_ERROR;
}
// 设置为启动分区
err = esp_ota_set_boot_partition(ota_ctrl.update_partition);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
ota_ctrl_clean();
return OTA_SET_BOOT_PARTITION_ERROR;
}
// 重启生效
ESP_LOGI(TAG, "Prepare to restart system!");
esp_restart();
return OTA_OK; // 重启则运行不到这里
}
char *ota_read_versions(void)
{
int err = 0;
ota_err_t ret = 0;
esp_app_desc_t *running_app_desc_p = esp_app_get_description();
char *running_version = running_app_desc_p->version;
ESP_LOGI(TAG, "Running binary version %s", running_version);
return running_version;
}