Skip to content

Commit a07a373

Browse files
committed
Change the reboot-max value semantics and code style and other fixes
1 parent 254062f commit a07a373

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

cmsis/TARGET_CORTEX_M/mbed_fault_handler.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ void print_context_info(void);
3232

3333
#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
3434
//Global for populating the context in exception handler
35-
mbed_fault_context_t *mbed_fault_context=(mbed_fault_context_t *)((uint32_t)FAULT_CONTEXT_LOCATION);
35+
mbed_fault_context_t *const mbed_fault_context=(mbed_fault_context_t *)(FAULT_CONTEXT_LOCATION);
3636
#else
3737
mbed_fault_context_t fault_context;
38-
mbed_fault_context_t *mbed_fault_context=(mbed_fault_context_t *)&fault_context;
38+
mbed_fault_context_t *const mbed_fault_context=(mbed_fault_context_t *)&fault_context;
3939
#endif
4040

4141
//This is a handler function called from Fault handler to print the error information out.

platform/mbed_error.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static mbed_error_ctx first_error_ctx = {0};
5050

5151
#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
5252
//Global for populating the context in exception handler
53-
static mbed_error_ctx *report_error_ctx=(mbed_error_ctx *)((uint32_t)ERROR_CONTEXT_LOCATION);
53+
static mbed_error_ctx *const report_error_ctx=(mbed_error_ctx *)(ERROR_CONTEXT_LOCATION);
5454
static bool is_reboot_error_valid = false;
5555
#endif
5656

@@ -66,13 +66,13 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
6666
//we dont have many uses cases to create a C wrapper for MbedCRC and the data
6767
//we calculate CRC on in this context is very less we will use a local
6868
//implementation here.
69-
static unsigned int compute_crc32(unsigned char *data, int datalen)
69+
static unsigned int compute_crc32(void *data, int datalen)
7070
{
7171
const unsigned int polynomial = 0x04C11DB7; /* divisor is 32bit */
7272
unsigned int crc = 0; /* CRC value is 32bit */
7373

74-
for( ;datalen>=0; datalen-- ) {
75-
unsigned char b = *data;
74+
for ( ;datalen>=0; datalen-- ) {
75+
unsigned char b = (*(unsigned char *)data);
7676
crc ^= (unsigned int )(b << 24); /* move byte into upper 8bit */
7777
for (int i = 0; i < 8; i++) {
7878
/* is MSB 1 */
@@ -200,9 +200,9 @@ mbed_error_status_t mbed_error_initialize(void)
200200
{
201201
#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
202202
uint32_t crc_val = 0;
203-
crc_val = compute_crc32( (unsigned char *)report_error_ctx, ((uint32_t)&(report_error_ctx->crc_error_ctx) - (uint32_t)report_error_ctx) );
204-
//Read report_error_ctx and check if CRC is correct for report_error_ctx
205-
if((report_error_ctx->crc_error_ctx == crc_val) && (report_error_ctx->is_error_processed == 0)) {
203+
crc_val = compute_crc32( report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx) );
204+
//Read report_error_ctx and check if CRC is correct, and with valid status code
205+
if ((report_error_ctx->crc_error_ctx == crc_val) && (report_error_ctx->is_error_processed == 0) && (report_error_ctx->error_status < 0)) {
206206
is_reboot_error_valid = true;
207207
#if MBED_CONF_PLATFORM_REBOOT_CRASH_REPORT_ENABLED && !defined(NDEBUG)
208208
//Report the error info
@@ -214,14 +214,14 @@ mbed_error_status_t mbed_error_initialize(void)
214214

215215
//Enforce max-reboot only if auto reboot is enabled
216216
#if MBED_CONF_PLATFORM_FATAL_ERROR_AUTO_REBOOT_ENABLED
217-
if( report_error_ctx->error_reboot_count > MBED_CONF_PLATFORM_ERROR_REBOOT_MAX ) {
217+
if ( report_error_ctx->error_reboot_count >= MBED_CONF_PLATFORM_ERROR_REBOOT_MAX ) {
218218
//We have rebooted more than enough, hold the system here.
219219
mbed_error_printf("\n== Reboot count(=%ld) exceeded maximum, system halting ==\n", report_error_ctx->error_reboot_count);
220220
mbed_halt_system();
221221
}
222222
#endif
223223
report_error_ctx->is_error_processed = 1;//Set the flag that we already processed this error
224-
crc_val = compute_crc32( (unsigned char *)report_error_ctx, ((uint32_t)&(report_error_ctx->crc_error_ctx) - (uint32_t)report_error_ctx) );
224+
crc_val = compute_crc32( report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx) );
225225
report_error_ctx->crc_error_ctx = crc_val;
226226
}
227227
#endif
@@ -270,28 +270,26 @@ WEAK MBED_NORETURN mbed_error_status_t mbed_error(mbed_error_status_t error_stat
270270

271271
#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
272272
uint32_t crc_val = 0;
273-
crc_val = compute_crc32( (unsigned char *)report_error_ctx, ((uint32_t)&(report_error_ctx->crc_error_ctx) - (uint32_t)report_error_ctx) );
273+
crc_val = compute_crc32( report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx) );
274274
//Read report_error_ctx and check if CRC is correct for report_error_ctx
275-
if(report_error_ctx->crc_error_ctx == crc_val) {
275+
if (report_error_ctx->crc_error_ctx == crc_val) {
276276
uint32_t current_reboot_count = report_error_ctx->error_reboot_count;
277277
last_error_ctx.error_reboot_count = current_reboot_count + 1;
278278
} else {
279279
last_error_ctx.error_reboot_count = 1;
280280
}
281281
last_error_ctx.is_error_processed = 0;//Set the flag that this is a new error
282282
//Update the struct with crc
283-
last_error_ctx.crc_error_ctx = compute_crc32( (unsigned char *)&last_error_ctx, ((uint32_t)&(last_error_ctx.crc_error_ctx) - (uint32_t)&last_error_ctx) );
283+
last_error_ctx.crc_error_ctx = compute_crc32( &last_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx) );
284284
//Protect report_error_ctx while we update it
285285
core_util_critical_section_enter();
286286
memcpy(report_error_ctx, &last_error_ctx, sizeof(mbed_error_ctx));
287287
core_util_critical_section_exit();
288288
//We need not call delete_mbed_crc(crc_obj) here as we are going to reset the system anyway, and calling delete while handling a fatal error may cause nested exception
289-
#if MBED_CONF_PLATFORM_FATAL_ERROR_AUTO_REBOOT_ENABLED
289+
#if MBED_CONF_PLATFORM_FATAL_ERROR_AUTO_REBOOT_ENABLED && (MBED_CONF_PLATFORM_ERROR_REBOOT_MAX > 0)
290290
system_reset();//do a system reset to get the system rebooted
291-
while(1);
292291
#endif
293292
#endif
294-
295293
mbed_halt_system();
296294

297295
return MBED_ERROR_FAILED_OPERATION;
@@ -325,12 +323,12 @@ mbed_error_status_t mbed_reset_reboot_error_info()
325323
mbed_error_status_t mbed_reset_reboot_count()
326324
{
327325
#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
328-
if(is_reboot_error_valid) {
326+
if (is_reboot_error_valid) {
329327
uint32_t crc_val = 0;
330328
core_util_critical_section_enter();
331329
report_error_ctx->error_reboot_count = 0;//Set reboot count to 0
332330
//Update CRC
333-
crc_val = compute_crc32( (unsigned char *)report_error_ctx, ((uint32_t)&(report_error_ctx->crc_error_ctx) - (uint32_t)report_error_ctx) );
331+
crc_val = compute_crc32( report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx) );
334332
report_error_ctx->crc_error_ctx = crc_val;
335333
core_util_critical_section_exit();
336334
return MBED_SUCCESS;
@@ -345,7 +343,7 @@ mbed_error_status_t mbed_get_reboot_error_info(mbed_error_ctx *error_info)
345343
mbed_error_status_t status = MBED_ERROR_ITEM_NOT_FOUND;
346344
#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
347345
if (is_reboot_error_valid) {
348-
if(error_info != NULL) {
346+
if (error_info != NULL) {
349347
memcpy(error_info, report_error_ctx, sizeof(mbed_error_ctx));
350348
status = MBED_SUCCESS;
351349
} else {
@@ -519,7 +517,7 @@ static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg,
519517
#endif
520518

521519
#if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT)
522-
if(print_thread_info == true) {
520+
if (print_thread_info) {
523521
mbed_error_printf("\nNext:");
524522
print_thread(osRtxInfo.thread.run.next);
525523

platform/mbed_lib.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
},
121121
"error-reboot-max": {
122122
"help": "Maximum number of auto reboots permitted when an error happens.",
123-
"value": 0
123+
"value": 1
124124
},
125125
"fatal-error-auto-reboot-enabled": {
126126
"help": "Setting this to true enables auto-reboot on a fatal error.",

0 commit comments

Comments
 (0)