refactor(espcoredump): keep checksum context in coredump stack

This commit is contained in:
Erhan Kurubas 2024-02-23 16:55:51 +01:00
parent 9e5c30baff
commit 0077c1234f
8 changed files with 65 additions and 63 deletions

@ -1 +1 @@
Subproject commit f5459aae80fe0ee7ea2b8387e8b6f9d6b556ca2c
Subproject commit c89851043b079a691d7eec788de36c1bba780956

@ -1 +1 @@
Subproject commit 1334b6d87456cd999e713e645e5dcdc10c527efb
Subproject commit 46342b66b8cd053a0de1ff3f0b8c853c92cbbfb3

View File

@ -55,31 +55,31 @@ uint32_t esp_core_dump_elf_version(void);
/**
* @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.
*
* @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.
* This is usually the new data to write (or already written) on
* 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.
*
* @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
* NULL, in this case, it will be ignored but the correct size
* of the checksum will be returned.
*
* @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.

View File

@ -91,6 +91,39 @@ extern "C" {
#error "Coredump cache size must be a multiple of 16"
#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.
*/
@ -101,7 +134,7 @@ typedef struct _core_dump_write_data_t
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_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;
/**

View File

@ -13,16 +13,7 @@
const static char TAG[] __attribute__((unused)) = "esp_core_dump_crc";
typedef uint32_t core_dump_crc_t;
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_init(core_dump_checksum_ctx cks_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")));
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")));
@ -49,12 +40,12 @@ static uint32_t core_dump_crc_version(void)
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) {
s_core_dump_crc_ctx.crc = 0;
s_core_dump_crc_ctx.total_bytes_checksum = 0;
*out_ctx = &s_core_dump_crc_ctx;
if (cks_ctx) {
core_dump_crc_ctx_t *crc_ctx = cks_ctx;
crc_ctx->crc = 0;
crc_ctx->total_bytes_checksum = 0;
}
}

View File

@ -29,10 +29,7 @@ typedef struct _core_dump_partition_t
bool encrypted;
} 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_t partition;
/* CRC of core dump partition config. */
@ -126,7 +123,7 @@ static esp_err_t esp_core_dump_flash_write_data(core_dump_write_data_t* priv, ui
wr_data->off += COREDUMP_CACHE_SIZE;
/* 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. */
wr_data->cached_bytes = 0;
@ -164,7 +161,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 */
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;
written += wr_sz;
data_size -= wr_sz;
@ -262,14 +259,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. */
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->cached_bytes = 0;
}
/* All data have been written to the flash, the cache is now empty, we can
* 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.
* (e.g. its size is not a multiple of 32) */
@ -302,8 +299,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)
{
static core_dump_write_config_t wr_cfg = { 0 };
static core_dump_write_data_t wr_data = { 0 };
core_dump_write_config_t wr_cfg = { 0 };
core_dump_write_data_t wr_data = { 0 };
/* Check core dump partition configuration. */
core_dump_crc_t crc = esp_core_dump_calc_flash_config_crc();
@ -392,7 +389,7 @@ esp_err_t esp_core_dump_image_check(void)
}
/* 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. */
offset += toread;
@ -400,7 +397,7 @@ esp_err_t esp_core_dump_image_check(void)
}
/* 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
* calculated. */

View File

@ -10,29 +10,10 @@
#include <string.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";
#define COREDUMP_SHA256_LEN 32
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_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")));
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")));
@ -107,12 +88,12 @@ static uint32_t core_dump_sha_version(void)
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) {
core_dump_sha256_start(&s_core_dump_sha_ctx);
s_core_dump_sha_ctx.total_bytes_checksum = 0;
*out_ctx = &s_core_dump_sha_ctx;
if (cks_ctx) {
core_dump_sha_ctx_t *sha_ctx = cks_ctx;
core_dump_sha256_start(sha_ctx);
sha_ctx->total_bytes_checksum = 0;
}
}

View File

@ -71,7 +71,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_write_data_t *wr_data = (core_dump_write_data_t *)priv;
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;
esp_core_dump_b64_encode((const uint8_t *)cs_addr, cs_len, (uint8_t*)&buf[0]);
ESP_COREDUMP_PRINT("%s\r\n", buf);
@ -108,7 +108,7 @@ static esp_err_t esp_core_dump_uart_write_data(core_dump_write_data_t *priv, voi
if (wr_data) {
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;
}