From 352dc6cf83e8d9ad8d6e175c95119856787eb8e1 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Tue, 27 Apr 2021 15:15:35 +0200 Subject: [PATCH 1/2] Fixed esp_core_dump_image_erase() for flash encryption with 16byte long write buffer --- components/espcoredump/src/core_dump_flash.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/components/espcoredump/src/core_dump_flash.c b/components/espcoredump/src/core_dump_flash.c index 55b8383c67..ff9fb85313 100644 --- a/components/espcoredump/src/core_dump_flash.c +++ b/components/espcoredump/src/core_dump_flash.c @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. #include +#include +#include "esp_core_dump.h" #include "esp_partition.h" #include "esp_log.h" #include "esp_core_dump_types.h" @@ -456,9 +458,21 @@ esp_err_t esp_core_dump_image_erase(void) return err; } + // helper to create (multiple of) 16 byte long write buffers + struct __attribute__((__packed__)) { + uint32_t size; + char buf[16-sizeof(uint32_t)]; + } helper; + + _Static_assert(sizeof(helper) % 16 == 0, "esp_partition_write() needs multiple of 16 byte long buffers"); + // 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)); + helper.size = BLANK_COREDUMP_SIZE; + + // fill the remaining bytes + memset(&helper.buf, '\0', sizeof(helper.buf)); + + err = esp_partition_write(core_part, 0, &helper, sizeof(helper)); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to write core dump partition size (%d)!", err); } From f810d51327673be5bd610b5e55fcea520af310f5 Mon Sep 17 00:00:00 2001 From: Omar Chebib Date: Mon, 21 Jun 2021 11:14:12 +0800 Subject: [PATCH 2/2] coredump: simplify the implementation of `esp_core_dump_image_erase` function Closes https://github.com/espressif/esp-idf/pull/6949 --- components/espcoredump/src/core_dump_flash.c | 23 +++++--------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/components/espcoredump/src/core_dump_flash.c b/components/espcoredump/src/core_dump_flash.c index ff9fb85313..6401e96f38 100644 --- a/components/espcoredump/src/core_dump_flash.c +++ b/components/espcoredump/src/core_dump_flash.c @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. #include -#include -#include "esp_core_dump.h" #include "esp_partition.h" #include "esp_log.h" #include "esp_core_dump_types.h" @@ -438,6 +436,11 @@ esp_err_t esp_core_dump_image_check(void) esp_err_t esp_core_dump_image_erase(void) { + /* If flash is encrypted, we can only write blocks of 16 bytes, let's always + * write a 16-byte buffer. */ + uint32_t helper[4] = { BLANK_COREDUMP_SIZE }; + _Static_assert(sizeof(helper) % 16 == 0, "esp_partition_write() needs multiple of 16 bytes long buffers"); + /* 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, @@ -458,21 +461,7 @@ esp_err_t esp_core_dump_image_erase(void) return err; } - // helper to create (multiple of) 16 byte long write buffers - struct __attribute__((__packed__)) { - uint32_t size; - char buf[16-sizeof(uint32_t)]; - } helper; - - _Static_assert(sizeof(helper) % 16 == 0, "esp_partition_write() needs multiple of 16 byte long buffers"); - - // Mark core dump as deleted by setting field size - helper.size = BLANK_COREDUMP_SIZE; - - // fill the remaining bytes - memset(&helper.buf, '\0', sizeof(helper.buf)); - - err = esp_partition_write(core_part, 0, &helper, sizeof(helper)); + err = esp_partition_write(core_part, 0, helper, sizeof(helper)); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to write core dump partition size (%d)!", err); }