From a72f2e22571e400cfe84b8f4c64f7fb048723a86 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sun, 28 Feb 2021 15:23:26 +0100 Subject: [PATCH 1/2] coredump: Implemented esp_core_dump_image_erase() and esp_core_dump_image_get() now returns ESP_ERR_NOT_FOUND when partition is blank Closes https://github.com/espressif/esp-idf/pull/6631 --- .../espcoredump/include/esp_core_dump.h | 9 +++++ components/espcoredump/src/core_dump_flash.c | 38 ++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/components/espcoredump/include/esp_core_dump.h b/components/espcoredump/include/esp_core_dump.h index fb0d44d2c8..0272754771 100644 --- a/components/espcoredump/include/esp_core_dump.h +++ b/components/espcoredump/include/esp_core_dump.h @@ -89,6 +89,15 @@ void esp_core_dump_to_uart(panic_info_t *info); */ esp_err_t esp_core_dump_image_get(size_t* out_addr, size_t *out_size); +/** + * @brief Erases coredump data in flash. esp_core_dump_image_get() will then return + * ESP_ERR_NOT_FOUND. Can be used after a coredump has been transmitted successfully. + * This function is always available, even when core dump is disabled in menuconfig. + * + * @return ESP_OK on success, otherwise \see esp_err_t + */ +esp_err_t esp_core_dump_image_erase(); + #ifdef __cplusplus } #endif diff --git a/components/espcoredump/src/core_dump_flash.c b/components/espcoredump/src/core_dump_flash.c index cf63d7539f..0a6c173d61 100644 --- a/components/espcoredump/src/core_dump_flash.c +++ b/components/espcoredump/src/core_dump_flash.c @@ -392,8 +392,8 @@ esp_err_t esp_core_dump_image_get(size_t* out_addr, size_t *out_size) /* Verify that the size read from the flash is not corrupted. */ if (size == 0xFFFFFFFF) { - ESP_LOGD(TAG, "Blank core dump partition!"); - err = ESP_ERR_INVALID_SIZE; + ESP_LOGD(TAG, "Coredump not found, blank core dump partition!"); + err = ESP_ERR_NOT_FOUND; } else if ((size < sizeof(uint32_t)) || (size > core_part->size)) { ESP_LOGE(TAG, "Incorrect size of core dump image: %d", size); err = ESP_ERR_INVALID_SIZE; @@ -459,3 +459,37 @@ esp_err_t esp_core_dump_image_get(size_t* out_addr, size_t *out_size) } #endif + +esp_err_t esp_core_dump_image_erase() +{ + /* Find the partition that could potentially contain a (previous) core dump. */ + const esp_partition_t *core_part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, + ESP_PARTITION_SUBTYPE_DATA_COREDUMP, + NULL); + if (!core_part) { + ESP_LOGE(TAG, "No core dump partition found!"); + return ESP_ERR_NOT_FOUND; + } + if (core_part->size < sizeof(uint32_t)) { + ESP_LOGE(TAG, "Too small core dump partition!"); + return ESP_ERR_INVALID_SIZE; + } + + esp_err_t err = ESP_OK; + err = esp_partition_erase_range(core_part, 0, core_part->size); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to erase core dump partition (%d)!", err); + return err; + } + + // on encrypted flash esp_partition_erase_range will leave encrypted + // garbage instead of 0xFFFFFFFF so overwriting again to safely signalize + // deleted coredumps + const uint32_t invalid_size = 0xFFFFFFFF; + err = esp_partition_write(core_part, 0, &invalid_size, sizeof(invalid_size)); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to write core dump partition size (%d)!", err); + } + + return err; +} From 45de6f9c59b0af050d925e8643b1dfe6196dbcf7 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Wed, 3 Mar 2021 13:36:56 +0100 Subject: [PATCH 2/2] espcoredump erase review comments --- components/espcoredump/include/esp_core_dump.h | 2 +- components/espcoredump/src/core_dump_flash.c | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/components/espcoredump/include/esp_core_dump.h b/components/espcoredump/include/esp_core_dump.h index 0272754771..818c0a3519 100644 --- a/components/espcoredump/include/esp_core_dump.h +++ b/components/espcoredump/include/esp_core_dump.h @@ -96,7 +96,7 @@ esp_err_t esp_core_dump_image_get(size_t* out_addr, size_t *out_size); * * @return ESP_OK on success, otherwise \see esp_err_t */ -esp_err_t esp_core_dump_image_erase(); +esp_err_t esp_core_dump_image_erase(void); #ifdef __cplusplus } diff --git a/components/espcoredump/src/core_dump_flash.c b/components/espcoredump/src/core_dump_flash.c index 0a6c173d61..33412d61fc 100644 --- a/components/espcoredump/src/core_dump_flash.c +++ b/components/espcoredump/src/core_dump_flash.c @@ -20,6 +20,8 @@ #include "esp_flash_encrypt.h" #include "esp_rom_crc.h" +#define BLANK_COREDUMP_SIZE 0xFFFFFFFF + const static DRAM_ATTR char TAG[] __attribute__((unused)) = "esp_core_dump_flash"; #if CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH @@ -391,7 +393,7 @@ esp_err_t esp_core_dump_image_get(size_t* out_addr, size_t *out_size) } /* Verify that the size read from the flash is not corrupted. */ - if (size == 0xFFFFFFFF) { + if (size == BLANK_COREDUMP_SIZE) { ESP_LOGD(TAG, "Coredump not found, blank core dump partition!"); err = ESP_ERR_NOT_FOUND; } else if ((size < sizeof(uint32_t)) || (size > core_part->size)) { @@ -460,7 +462,7 @@ esp_err_t esp_core_dump_image_get(size_t* out_addr, size_t *out_size) #endif -esp_err_t esp_core_dump_image_erase() +esp_err_t esp_core_dump_image_erase(void) { /* Find the partition that could potentially contain a (previous) core dump. */ const esp_partition_t *core_part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, @@ -482,11 +484,9 @@ esp_err_t esp_core_dump_image_erase() return err; } - // on encrypted flash esp_partition_erase_range will leave encrypted - // garbage instead of 0xFFFFFFFF so overwriting again to safely signalize - // deleted coredumps - const uint32_t invalid_size = 0xFFFFFFFF; - err = esp_partition_write(core_part, 0, &invalid_size, sizeof(invalid_size)); + // Mark core dump as deleted by setting field size + const uint32_t blank_size = BLANK_COREDUMP_SIZE; + err = esp_partition_write(core_part, 0, &blank_size, sizeof(blank_size)); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to write core dump partition size (%d)!", err); }