From 30e766ee6b71d7b7113f7f9979e26012a399adf5 Mon Sep 17 00:00:00 2001 From: Alexey Gerenkov Date: Tue, 13 Mar 2018 17:07:42 +0300 Subject: [PATCH] esp32: Core dump API to retrieve current core data layout in flash --- components/esp32/core_dump.c | 55 +++++++++++++++++++++++- components/esp32/cpu_start.c | 5 +++ components/esp32/include/esp_core_dump.h | 21 +++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/components/esp32/core_dump.c b/components/esp32/core_dump.c index 6150893fac..5608653c06 100644 --- a/components/esp32/core_dump.c +++ b/components/esp32/core_dump.c @@ -24,6 +24,7 @@ #include "esp_panic.h" #include "esp_partition.h" #include "esp_clk.h" +#include "esp_core_dump.h" #include "esp_log.h" const static DRAM_ATTR char TAG[] __attribute__((unused)) = "esp_core_dump"; @@ -574,5 +575,57 @@ void esp_core_dump_init() #endif } -#endif +esp_err_t esp_core_dump_image_get(size_t* out_addr, size_t *out_size) +{ + esp_err_t err; + const void *core_data; + spi_flash_mmap_handle_t core_data_handle; + + + if (out_addr == NULL || out_size == NULL) { + return ESP_ERR_INVALID_ARG; + } + + 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_FAIL; + } + if (core_part->size < sizeof(uint32_t)) { + ESP_LOGE(TAG, "Too small core dump partition!"); + return ESP_FAIL; + } + + err = esp_partition_mmap(core_part, 0, sizeof(uint32_t), + SPI_FLASH_MMAP_DATA, &core_data, &core_data_handle); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to mmap core dump data (%d)!", err); + return err; + } + uint32_t *dw = (uint32_t *)core_data; + *out_size = *dw; + spi_flash_munmap(core_data_handle); + // remap full core dump with CRC + err = esp_partition_mmap(core_part, 0, *out_size, + SPI_FLASH_MMAP_DATA, &core_data, &core_data_handle); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to mmap core dump data (%d)!", err); + return err; + } + uint32_t *crc = (uint32_t *)(((uint8_t *)core_data) + *out_size); + crc--; // Point to CRC field + // Calc CRC over core dump data except for CRC field + core_dump_crc_t cur_crc = crc32_le(0, (uint8_t const *)core_data, *out_size - sizeof(core_dump_crc_t)); + if (*crc != cur_crc) { + ESP_LOGE(TAG, "Core dump data CRC check failed: 0x%x -> 0x%x!", *crc, cur_crc); + spi_flash_munmap(core_data_handle); + return ESP_FAIL; + } + + spi_flash_munmap(core_data_handle); + + *out_addr = core_part->address; + return ESP_OK; +} +#endif diff --git a/components/esp32/cpu_start.c b/components/esp32/cpu_start.c index 5a5dc7296c..8a7155f2e8 100644 --- a/components/esp32/cpu_start.c +++ b/components/esp32/cpu_start.c @@ -382,6 +382,11 @@ void start_cpu0_default(void) #if CONFIG_ESP32_ENABLE_COREDUMP esp_core_dump_init(); + size_t core_data_sz = 0; + size_t core_data_addr = 0; + if (esp_core_dump_image_get(&core_data_addr, &core_data_sz) == ESP_OK && core_data_sz > 0) { + ESP_LOGI(TAG, "Found core dump %d bytes in flash @ 0x%x", core_data_sz, core_data_addr); + } #endif portBASE_TYPE res = xTaskCreatePinnedToCore(&main_task, "main", diff --git a/components/esp32/include/esp_core_dump.h b/components/esp32/include/esp_core_dump.h index 89a1c2779c..4c798f59ed 100644 --- a/components/esp32/include/esp_core_dump.h +++ b/components/esp32/include/esp_core_dump.h @@ -14,6 +14,11 @@ #ifndef ESP_CORE_DUMP_H_ #define ESP_CORE_DUMP_H_ + +/**************************************************************************************/ +/******************************** EXCEPTION MODE API **********************************/ +/**************************************************************************************/ + /** * @brief Initializes core dump module internal data. * @@ -61,4 +66,20 @@ void esp_core_dump_to_flash(); */ void esp_core_dump_to_uart(); + +/**************************************************************************************/ +/*********************************** USER MODE API ************************************/ +/**************************************************************************************/ + +/** + * @brief Retrieves address and size of coredump data in flash. + * This function is always available, even when core dump is disabled in menuconfig. + * + * @param out_addr pointer to store image address in flash. + * @param out_size pointer to store image size in flash (including CRC). In bytes. + * + * @return ESP_OK on success, otherwise \see esp_err_t + */ +esp_err_t esp_core_dump_image_get(size_t* out_addr, size_t *out_size); + #endif -- 2.50.1