refactor(espcoredump): keep checksum context in coredump stack

This commit is contained in:
Erhan Kurubas 2024-02-23 16:55:51 +01:00 committed by BOT
parent dc86c17b38
commit 18fc19faf2
6 changed files with 62 additions and 59 deletions

View File

@ -55,31 +55,31 @@ uint32_t esp_core_dump_elf_version(void);
/** /**
* @brief Initialize checksum calculation for the given context. * @brief Initialize checksum calculation for the given context.
* *
* @param wr_data Core dump checksum context to fill. * @param ctx Core dump checksum context to fill.
*/ */
void esp_core_dump_checksum_init(void ** wr_data); void esp_core_dump_checksum_init(void *ctx);
/** /**
* @brief Update checksum calculation by integrating the given data in the context. * @brief Update checksum calculation by integrating the given data in the context.
* *
* @param wr_data Core dump checksum context. * @param ctx Core dump checksum context.
* @param data Pointer to the data to integrate in the checksum calculation. * @param data Pointer to the data to integrate in the checksum calculation.
* This is usually the new data to write (or already written) on * This is usually the new data to write (or already written) on
* the flash. * the flash.
*/ */
void esp_core_dump_checksum_update(void* wr_data, void* data, size_t data_len); void esp_core_dump_checksum_update(void *ctx, void *data, size_t data_len);
/** /**
* @brief Terminate and return checksum calculated for the given context. * @brief Terminate and return checksum calculated for the given context.
* *
* @param wr_data Core dump checksum context. * @param ctx Core dump checksum context.
* @param chs_ptr Pointer used to return the checksum calculated. It can be * @param chs_ptr Pointer used to return the checksum calculated. It can be
* NULL, in this case, it will be ignored but the correct size * NULL, in this case, it will be ignored but the correct size
* of the checksum will be returned. * of the checksum will be returned.
* *
* @return The size, in bytes, of the checksum. * @return The size, in bytes, of the checksum.
*/ */
uint32_t esp_core_dump_checksum_finish(void* wr_data, core_dump_checksum_bytes* chs_ptr); uint32_t esp_core_dump_checksum_finish(void *ctx, core_dump_checksum_bytes *chs_ptr);
/** /**
* @brief Return the size of the checksums. * @brief Return the size of the checksums.

View File

@ -91,6 +91,39 @@ extern "C" {
#error "Coredump cache size must be a multiple of 16" #error "Coredump cache size must be a multiple of 16"
#endif #endif
typedef uint32_t core_dump_crc_t;
#if CONFIG_ESP_COREDUMP_CHECKSUM_CRC32
typedef struct {
core_dump_crc_t crc;
uint32_t total_bytes_checksum; /* Number of bytes used to calculate the checksum */
} core_dump_crc_ctx_t;
typedef core_dump_crc_ctx_t checksum_ctx_t;
#else
#if CONFIG_IDF_TARGET_ESP32
#include "mbedtls/sha256.h" /* mbedtls_sha256_context */
typedef mbedtls_sha256_context sha256_ctx_t;
#else
#include "hal/sha_types.h" /* SHA_CTX */
typedef SHA_CTX sha256_ctx_t;
#endif
#define COREDUMP_SHA256_LEN 32
typedef struct {
sha256_ctx_t ctx;
uint8_t result[COREDUMP_SHA256_LEN];
uint32_t total_bytes_checksum; /* Number of bytes used to calculate the checksum */
} core_dump_sha_ctx_t;
typedef core_dump_sha_ctx_t checksum_ctx_t;
#endif
/** /**
* @brief Chip ID associated to this implementation. * @brief Chip ID associated to this implementation.
*/ */
@ -100,7 +133,7 @@ typedef struct _core_dump_write_data_t {
uint32_t off; /*!< Current offset of data being written */ uint32_t off; /*!< Current offset of data being written */
uint8_t cached_data[COREDUMP_CACHE_SIZE]; /*!< Cache used to write to flash */ uint8_t cached_data[COREDUMP_CACHE_SIZE]; /*!< Cache used to write to flash */
uint8_t cached_bytes; /*!< Number of bytes filled in the cached */ uint8_t cached_bytes; /*!< Number of bytes filled in the cached */
void *checksum_ctx; /*!< Checksum context */ checksum_ctx_t checksum_ctx; /*!< Checksum context */
} core_dump_write_data_t; } core_dump_write_data_t;
/** /**

View File

@ -13,16 +13,7 @@
const static char TAG[] __attribute__((unused)) = "esp_core_dump_crc"; const static char TAG[] __attribute__((unused)) = "esp_core_dump_crc";
typedef uint32_t core_dump_crc_t; void esp_core_dump_checksum_init(core_dump_checksum_ctx cks_ctx) __attribute__((alias("core_dump_crc_init")));
typedef struct {
core_dump_crc_t crc;
uint32_t total_bytes_checksum; /* Number of bytes used to calculate the checksum */
} core_dump_crc_ctx_t;
static core_dump_crc_ctx_t s_core_dump_crc_ctx = { 0 };
void esp_core_dump_checksum_init(core_dump_checksum_ctx *out_ctx) __attribute__((alias("core_dump_crc_init")));
void esp_core_dump_checksum_update(core_dump_checksum_ctx cks_ctx, void* data, size_t data_len) __attribute__((alias("core_dump_crc_update"))); void esp_core_dump_checksum_update(core_dump_checksum_ctx cks_ctx, void* data, size_t data_len) __attribute__((alias("core_dump_crc_update")));
uint32_t esp_core_dump_checksum_finish(core_dump_checksum_ctx cks_ctx, core_dump_checksum_bytes* chs_ptr) __attribute__((alias("core_dump_crc_finish"))); uint32_t esp_core_dump_checksum_finish(core_dump_checksum_ctx cks_ctx, core_dump_checksum_bytes* chs_ptr) __attribute__((alias("core_dump_crc_finish")));
void esp_core_dump_print_checksum(const char* msg, core_dump_checksum_bytes checksum) __attribute__((alias("core_dump_crc_print"))); void esp_core_dump_print_checksum(const char* msg, core_dump_checksum_bytes checksum) __attribute__((alias("core_dump_crc_print")));
@ -49,12 +40,12 @@ static uint32_t core_dump_crc_version(void)
return COREDUMP_VERSION_ELF_CRC32; return COREDUMP_VERSION_ELF_CRC32;
} }
static void core_dump_crc_init(core_dump_checksum_ctx *out_ctx) static void core_dump_crc_init(core_dump_checksum_ctx cks_ctx)
{ {
if (out_ctx) { if (cks_ctx) {
s_core_dump_crc_ctx.crc = 0; core_dump_crc_ctx_t *crc_ctx = cks_ctx;
s_core_dump_crc_ctx.total_bytes_checksum = 0; crc_ctx->crc = 0;
*out_ctx = &s_core_dump_crc_ctx; crc_ctx->total_bytes_checksum = 0;
} }
} }

View File

@ -32,8 +32,6 @@ typedef struct _core_dump_partition_t {
#endif #endif
} core_dump_partition_t; } core_dump_partition_t;
typedef uint32_t core_dump_crc_t;
typedef struct _core_dump_flash_config_t { typedef struct _core_dump_flash_config_t {
/* Core dump partition config. */ /* Core dump partition config. */
core_dump_partition_t partition; core_dump_partition_t partition;
@ -141,7 +139,7 @@ static esp_err_t esp_core_dump_flash_write_data(core_dump_write_data_t* priv, ui
wr_data->off += COREDUMP_CACHE_SIZE; wr_data->off += COREDUMP_CACHE_SIZE;
/* Update checksum with the newly written data on the flash. */ /* Update checksum with the newly written data on the flash. */
esp_core_dump_checksum_update(wr_data->checksum_ctx, &wr_data->cached_data, COREDUMP_CACHE_SIZE); esp_core_dump_checksum_update(&wr_data->checksum_ctx, &wr_data->cached_data, COREDUMP_CACHE_SIZE);
/* Reset cache from the next use. */ /* Reset cache from the next use. */
wr_data->cached_bytes = 0; wr_data->cached_bytes = 0;
@ -179,7 +177,7 @@ static esp_err_t esp_core_dump_flash_write_data(core_dump_write_data_t* priv, ui
} }
/* Update the checksum with the newly written bytes */ /* Update the checksum with the newly written bytes */
esp_core_dump_checksum_update(wr_data->checksum_ctx, data + written, wr_sz); esp_core_dump_checksum_update(&wr_data->checksum_ctx, data + written, wr_sz);
wr_data->off += wr_sz; wr_data->off += wr_sz;
written += wr_sz; written += wr_sz;
data_size -= wr_sz; data_size -= wr_sz;
@ -277,14 +275,14 @@ static esp_err_t esp_core_dump_flash_write_end(core_dump_write_data_t* priv)
} }
/* Update the checksum with the data written, including the padding. */ /* Update the checksum with the data written, including the padding. */
esp_core_dump_checksum_update(wr_data->checksum_ctx, wr_data->cached_data, COREDUMP_CACHE_SIZE); esp_core_dump_checksum_update(&wr_data->checksum_ctx, wr_data->cached_data, COREDUMP_CACHE_SIZE);
wr_data->off += COREDUMP_CACHE_SIZE; wr_data->off += COREDUMP_CACHE_SIZE;
wr_data->cached_bytes = 0; wr_data->cached_bytes = 0;
} }
/* All data have been written to the flash, the cache is now empty, we can /* All data have been written to the flash, the cache is now empty, we can
* terminate the checksum calculation. */ * terminate the checksum calculation. */
esp_core_dump_checksum_finish(wr_data->checksum_ctx, &checksum); esp_core_dump_checksum_finish(&wr_data->checksum_ctx, &checksum);
/* Use the cache to write the checksum if its size doesn't match the requirements. /* Use the cache to write the checksum if its size doesn't match the requirements.
* (e.g. its size is not a multiple of 32) */ * (e.g. its size is not a multiple of 32) */
@ -317,8 +315,8 @@ static esp_err_t esp_core_dump_flash_write_end(core_dump_write_data_t* priv)
void esp_core_dump_to_flash(panic_info_t *info) void esp_core_dump_to_flash(panic_info_t *info)
{ {
static core_dump_write_config_t wr_cfg = { 0 }; core_dump_write_config_t wr_cfg = { 0 };
static core_dump_write_data_t wr_data = { 0 }; core_dump_write_data_t wr_data = { 0 };
/* Check core dump partition configuration. */ /* Check core dump partition configuration. */
core_dump_crc_t crc = esp_core_dump_calc_flash_config_crc(); core_dump_crc_t crc = esp_core_dump_calc_flash_config_crc();
@ -414,7 +412,7 @@ esp_err_t esp_core_dump_image_check(void)
} }
/* Update the checksum according to what was just read. */ /* Update the checksum according to what was just read. */
esp_core_dump_checksum_update(wr_data.checksum_ctx, wr_data.cached_data, toread); esp_core_dump_checksum_update(&wr_data.checksum_ctx, wr_data.cached_data, toread);
/* Move the offset forward and decrease the remaining size. */ /* Move the offset forward and decrease the remaining size. */
offset += toread; offset += toread;
@ -422,7 +420,7 @@ esp_err_t esp_core_dump_image_check(void)
} }
/* The coredump has been totally read, finish the checksum calculation. */ /* The coredump has been totally read, finish the checksum calculation. */
esp_core_dump_checksum_finish(wr_data.checksum_ctx, &checksum_calc); esp_core_dump_checksum_finish(&wr_data.checksum_ctx, &checksum_calc);
/* Read the checksum from the flash and compare to the one just /* Read the checksum from the flash and compare to the one just
* calculated. */ * calculated. */

View File

@ -10,29 +10,10 @@
#include <string.h> #include <string.h>
#include "esp_core_dump_types.h" #include "esp_core_dump_types.h"
#if CONFIG_IDF_TARGET_ESP32
#include "mbedtls/sha256.h" /* mbedtls_sha256_context */
#else
#include "hal/sha_types.h" /* SHA_CTX */
#endif
const static char TAG[] __attribute__((unused)) = "esp_core_dump_sha"; const static char TAG[] __attribute__((unused)) = "esp_core_dump_sha";
#define COREDUMP_SHA256_LEN 32 void esp_core_dump_checksum_init(core_dump_checksum_ctx cks_ctx) __attribute__((alias("core_dump_sha_init")));
typedef struct {
#if CONFIG_IDF_TARGET_ESP32
mbedtls_sha256_context ctx;
#else
SHA_CTX ctx;
#endif
uint8_t result[COREDUMP_SHA256_LEN];
uint32_t total_bytes_checksum; /* Number of bytes used to calculate the checksum */
} core_dump_sha_ctx_t;
static core_dump_sha_ctx_t s_core_dump_sha_ctx = { 0 };
void esp_core_dump_checksum_init(core_dump_checksum_ctx *cks_ctx) __attribute__((alias("core_dump_sha_init")));
void esp_core_dump_checksum_update(core_dump_checksum_ctx cks_ctx, void* data, size_t data_len) __attribute__((alias("core_dump_sha_update"))); void esp_core_dump_checksum_update(core_dump_checksum_ctx cks_ctx, void* data, size_t data_len) __attribute__((alias("core_dump_sha_update")));
uint32_t esp_core_dump_checksum_finish(core_dump_checksum_ctx cks_ctx, core_dump_checksum_bytes* chs_ptr) __attribute__((alias("core_dump_sha_finish"))); uint32_t esp_core_dump_checksum_finish(core_dump_checksum_ctx cks_ctx, core_dump_checksum_bytes* chs_ptr) __attribute__((alias("core_dump_sha_finish")));
void esp_core_dump_print_checksum(const char* msg, core_dump_checksum_bytes checksum) __attribute__((alias("core_dump_sha256_print"))); void esp_core_dump_print_checksum(const char* msg, core_dump_checksum_bytes checksum) __attribute__((alias("core_dump_sha256_print")));
@ -107,12 +88,12 @@ static uint32_t core_dump_sha_version(void)
return COREDUMP_VERSION_ELF_SHA256; return COREDUMP_VERSION_ELF_SHA256;
} }
static void core_dump_sha_init(core_dump_checksum_ctx *out_ctx) static void core_dump_sha_init(core_dump_checksum_ctx cks_ctx)
{ {
if (out_ctx) { if (cks_ctx) {
core_dump_sha256_start(&s_core_dump_sha_ctx); core_dump_sha_ctx_t *sha_ctx = cks_ctx;
s_core_dump_sha_ctx.total_bytes_checksum = 0; core_dump_sha256_start(sha_ctx);
*out_ctx = &s_core_dump_sha_ctx; sha_ctx->total_bytes_checksum = 0;
} }
} }

View File

@ -72,7 +72,7 @@ static esp_err_t esp_core_dump_uart_write_end(core_dump_write_data_t *priv)
core_dump_checksum_bytes cs_addr = NULL; core_dump_checksum_bytes cs_addr = NULL;
core_dump_write_data_t *wr_data = (core_dump_write_data_t *)priv; core_dump_write_data_t *wr_data = (core_dump_write_data_t *)priv;
if (wr_data) { if (wr_data) {
size_t cs_len = esp_core_dump_checksum_finish(wr_data->checksum_ctx, &cs_addr); size_t cs_len = esp_core_dump_checksum_finish(&wr_data->checksum_ctx, &cs_addr);
wr_data->off += cs_len; wr_data->off += cs_len;
esp_core_dump_b64_encode((const uint8_t *)cs_addr, cs_len, (uint8_t*)&buf[0]); esp_core_dump_b64_encode((const uint8_t *)cs_addr, cs_len, (uint8_t*)&buf[0]);
ESP_COREDUMP_PRINT("%s\r\n", buf); ESP_COREDUMP_PRINT("%s\r\n", buf);
@ -111,7 +111,7 @@ static esp_err_t esp_core_dump_uart_write_data(core_dump_write_data_t *priv, voi
if (wr_data) { if (wr_data) {
wr_data->off += data_len; wr_data->off += data_len;
esp_core_dump_checksum_update(wr_data->checksum_ctx, data, data_len); esp_core_dump_checksum_update(&wr_data->checksum_ctx, data, data_len);
} }
return err; return err;
} }