21
21
22
22
#if defined(CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH) && defined(CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF)
23
23
#include < esp_core_dump.h>
24
- #include < esp_flash_encrypt.h>
25
- // Its a bit hackish but we need this in order to pull in the sizeof(core_dump_header_t)
26
- // we can even use the static 20 but, what if that gets chagned?
27
- #include " ../include_core_dump/esp_core_dump_types.h"
28
24
#endif // defined(CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH) && defined(CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF)
29
25
30
26
using namespace chip ;
@@ -95,34 +91,16 @@ size_t LogProvider::GetCrashSize()
95
91
size_t outSize = 0 ;
96
92
97
93
#if defined(CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH) && defined(CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF)
98
- size_t unusedOutAddr;
99
- esp_err_t esp_err = esp_core_dump_image_get (&unusedOutAddr, &outSize);
100
- VerifyOrReturnValue (esp_err == ESP_OK, 0 , ChipLogError (DeviceLayer, " Failed to get core dump image, esp_err:%d" , esp_err));
94
+ // Verify that the crash is present and sane
95
+ esp_err_t esp_err = esp_core_dump_image_check ();
96
+ VerifyOrReturnValue (esp_err == ESP_OK, 0 , ChipLogError (DeviceLayer, " Core dump image check failed, esp_err:%d" , esp_err));
97
+
98
+ outSize = sizeof (esp_core_dump_summary_t );
101
99
#endif // defined(CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH) && defined(CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF)
102
100
103
101
return outSize;
104
102
}
105
103
106
- CHIP_ERROR LogProvider::MapCrashPartition (CrashLogContext * context)
107
- {
108
- #if defined(CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH) && defined(CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF)
109
- size_t outAddr, outSize;
110
- esp_err_t esp_err = esp_core_dump_image_get (&outAddr, &outSize);
111
- VerifyOrReturnError (esp_err == ESP_OK, CHIP_ERROR (ChipError::Range::kPlatform , esp_err),
112
- ChipLogError (DeviceLayer, " Failed to get core dump image, esp_err:%d" , esp_err));
113
-
114
- /* map the full core dump parition, including the checksum. */
115
- esp_err = spi_flash_mmap (outAddr, outSize, SPI_FLASH_MMAP_DATA, &context->mappedAddress , &context->mappedHandle );
116
- VerifyOrReturnError (esp_err == ESP_OK, CHIP_ERROR (ChipError::Range::kPlatform , esp_err),
117
- ChipLogError (DeviceLayer, " Failed to mmap the crash partition, esp_err:%d" , esp_err));
118
-
119
- context->crashSize = static_cast <uint32_t >(outSize);
120
- return CHIP_NO_ERROR;
121
- #else
122
- return CHIP_ERROR_NOT_FOUND;
123
- #endif // defined(CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH) && defined(CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF)
124
- }
125
-
126
104
CHIP_ERROR LogProvider::PrepareLogContextForIntent (LogContext * context, IntentEnum intent)
127
105
{
128
106
context->intent = intent;
@@ -146,11 +124,24 @@ CHIP_ERROR LogProvider::PrepareLogContextForIntent(LogContext * context, IntentE
146
124
sCrashLogContext .Reset ();
147
125
context->Crash .logContext = &sCrashLogContext ;
148
126
149
- CHIP_ERROR err = MapCrashPartition (context->Crash .logContext );
150
- VerifyOrReturnError (err == CHIP_NO_ERROR, err, context->Crash .logContext = nullptr );
127
+ size_t crashSize = GetCrashSize ();
128
+ VerifyOrReturnError (crashSize > 0 , CHIP_ERROR_NOT_FOUND);
129
+
130
+ esp_core_dump_summary_t * summary =
131
+ reinterpret_cast <esp_core_dump_summary_t *>(Platform::MemoryCalloc (1 , sizeof (esp_core_dump_summary_t )));
132
+ VerifyOrReturnError (summary != nullptr , CHIP_ERROR_NO_MEMORY);
133
+
134
+ esp_err_t esp_err = esp_core_dump_get_summary (summary);
135
+ if (esp_err != ESP_OK)
136
+ {
137
+ ChipLogError (DeviceLayer, " Failed to get core dump image, esp_err:%d" , esp_err);
138
+ Platform::MemoryFree (summary);
139
+ return CHIP_ERROR_NOT_FOUND;
140
+ }
151
141
152
- context->Crash .logContext ->readOffset = sizeof (core_dump_header_t );
153
- context->Crash .logContext ->isMapped = true ;
142
+ context->Crash .logContext ->crashSize = crashSize;
143
+ context->Crash .logContext ->readOffset = 0 ;
144
+ context->Crash .logContext ->summary = summary;
154
145
#else
155
146
return CHIP_ERROR_NOT_FOUND;
156
147
#endif // defined(CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH) && defined(CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF)
@@ -177,7 +168,7 @@ void LogProvider::CleanupLogContextForIntent(LogContext * context)
177
168
case IntentEnum::kCrashLogs : {
178
169
#if defined(CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH) && defined(CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF)
179
170
CrashLogContext * logContext = context->Crash .logContext ;
180
- spi_flash_munmap (logContext-> mappedHandle );
171
+ // Reset() frees the summary if allocated
181
172
logContext->Reset ();
182
173
#endif // defined(CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH) && defined(CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF)
183
174
}
@@ -227,12 +218,15 @@ CHIP_ERROR LogProvider::GetDataForIntent(LogContext * context, MutableByteSpan &
227
218
case IntentEnum::kCrashLogs : {
228
219
#if defined(CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH) && defined(CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF)
229
220
CrashLogContext * logContext = context->Crash .logContext ;
230
- size_t dataSize = logContext->crashSize - logContext->readOffset ;
231
- auto count = std::min (dataSize, outBuffer.size ());
221
+
222
+ VerifyOrReturnError (logContext->readOffset < logContext->crashSize , CHIP_ERROR_INCORRECT_STATE, outBuffer.reduce_size (0 ));
223
+
224
+ size_t dataSize = logContext->crashSize - logContext->readOffset ;
225
+ auto count = std::min (dataSize, outBuffer.size ());
232
226
233
227
VerifyOrReturnError (CanCastTo<off_t >(count), CHIP_ERROR_INVALID_ARGUMENT, outBuffer.reduce_size (0 ));
234
228
235
- const uint8_t * readAddr = reinterpret_cast <const uint8_t *>(logContext->mappedAddress ) + logContext->readOffset ;
229
+ const uint8_t * readAddr = reinterpret_cast <const uint8_t *>(logContext->summary ) + logContext->readOffset ;
236
230
memcpy (outBuffer.data (), readAddr, count);
237
231
outBuffer.reduce_size (count);
238
232
@@ -257,12 +251,14 @@ CHIP_ERROR LogProvider::StartLogCollection(IntentEnum intent, LogSessionHandle &
257
251
{
258
252
VerifyOrReturnValue (IsValidIntent (intent), CHIP_ERROR_INVALID_ARGUMENT);
259
253
254
+ #if defined(CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH) && defined(CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF)
260
255
// In case of crash logs we can only mmap at max once, so check before doing anything
261
256
if (intent == IntentEnum::kCrashLogs )
262
257
{
263
- VerifyOrReturnError (sCrashLogContext .isMapped == false , CHIP_ERROR_INCORRECT_STATE,
264
- ChipLogError (DeviceLayer, " Crash partition already mapped " ));
258
+ VerifyOrReturnError (sCrashLogContext .summary == nullptr , CHIP_ERROR_INCORRECT_STATE,
259
+ ChipLogError (DeviceLayer, " Crash summary already allocated " ));
265
260
}
261
+ #endif // defined(CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH) && defined(CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF)
266
262
267
263
LogContext * context = reinterpret_cast <LogContext *>(Platform::MemoryCalloc (1 , sizeof (LogContext)));
268
264
VerifyOrReturnValue (context != nullptr , CHIP_ERROR_NO_MEMORY);
0 commit comments