diff --git a/cmsis/TARGET_CORTEX_M/mbed_fault_handler.c b/cmsis/TARGET_CORTEX_M/mbed_fault_handler.c index abd56116586..62fa90ff07c 100644 --- a/cmsis/TARGET_CORTEX_M/mbed_fault_handler.c +++ b/cmsis/TARGET_CORTEX_M/mbed_fault_handler.c @@ -32,10 +32,10 @@ void print_context_info(void); #if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED //Global for populating the context in exception handler - mbed_fault_context_t *mbed_fault_context=(mbed_fault_context_t *)((uint32_t)FAULT_CONTEXT_LOCATION); + mbed_fault_context_t *const mbed_fault_context=(mbed_fault_context_t *)(FAULT_CONTEXT_LOCATION); #else mbed_fault_context_t fault_context; - mbed_fault_context_t *mbed_fault_context=(mbed_fault_context_t *)&fault_context; + mbed_fault_context_t *const mbed_fault_context=(mbed_fault_context_t *)&fault_context; #endif //This is a handler function called from Fault handler to print the error information out. diff --git a/platform/mbed_error.c b/platform/mbed_error.c index 9723146e01d..4ef76827a7a 100644 --- a/platform/mbed_error.c +++ b/platform/mbed_error.c @@ -50,7 +50,7 @@ static mbed_error_ctx first_error_ctx = {0}; #if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED //Global for populating the context in exception handler - static mbed_error_ctx *report_error_ctx=(mbed_error_ctx *)((uint32_t)ERROR_CONTEXT_LOCATION); + static mbed_error_ctx *const report_error_ctx=(mbed_error_ctx *)(ERROR_CONTEXT_LOCATION); static bool is_reboot_error_valid = false; #endif @@ -66,13 +66,13 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign //we dont have many uses cases to create a C wrapper for MbedCRC and the data //we calculate CRC on in this context is very less we will use a local //implementation here. -static unsigned int compute_crc32(unsigned char *data, int datalen) +static unsigned int compute_crc32(void *data, int datalen) { const unsigned int polynomial = 0x04C11DB7; /* divisor is 32bit */ unsigned int crc = 0; /* CRC value is 32bit */ - for( ;datalen>=0; datalen-- ) { - unsigned char b = *data; + for ( ;datalen>=0; datalen-- ) { + unsigned char b = (*(unsigned char *)data); crc ^= (unsigned int )(b << 24); /* move byte into upper 8bit */ for (int i = 0; i < 8; i++) { /* is MSB 1 */ @@ -200,9 +200,9 @@ mbed_error_status_t mbed_error_initialize(void) { #if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED uint32_t crc_val = 0; - crc_val = compute_crc32( (unsigned char *)report_error_ctx, ((uint32_t)&(report_error_ctx->crc_error_ctx) - (uint32_t)report_error_ctx) ); - //Read report_error_ctx and check if CRC is correct for report_error_ctx - if((report_error_ctx->crc_error_ctx == crc_val) && (report_error_ctx->is_error_processed == 0)) { + crc_val = compute_crc32( report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx) ); + //Read report_error_ctx and check if CRC is correct, and with valid status code + if ((report_error_ctx->crc_error_ctx == crc_val) && (report_error_ctx->is_error_processed == 0) && (report_error_ctx->error_status < 0)) { is_reboot_error_valid = true; #if MBED_CONF_PLATFORM_REBOOT_CRASH_REPORT_ENABLED && !defined(NDEBUG) //Report the error info @@ -214,14 +214,14 @@ mbed_error_status_t mbed_error_initialize(void) //Enforce max-reboot only if auto reboot is enabled #if MBED_CONF_PLATFORM_FATAL_ERROR_AUTO_REBOOT_ENABLED - if( report_error_ctx->error_reboot_count > MBED_CONF_PLATFORM_ERROR_REBOOT_MAX ) { + if ( report_error_ctx->error_reboot_count >= MBED_CONF_PLATFORM_ERROR_REBOOT_MAX ) { //We have rebooted more than enough, hold the system here. mbed_error_printf("\n== Reboot count(=%ld) exceeded maximum, system halting ==\n", report_error_ctx->error_reboot_count); mbed_halt_system(); } #endif report_error_ctx->is_error_processed = 1;//Set the flag that we already processed this error - crc_val = compute_crc32( (unsigned char *)report_error_ctx, ((uint32_t)&(report_error_ctx->crc_error_ctx) - (uint32_t)report_error_ctx) ); + crc_val = compute_crc32( report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx) ); report_error_ctx->crc_error_ctx = crc_val; } #endif @@ -270,9 +270,9 @@ WEAK MBED_NORETURN mbed_error_status_t mbed_error(mbed_error_status_t error_stat #if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED uint32_t crc_val = 0; - crc_val = compute_crc32( (unsigned char *)report_error_ctx, ((uint32_t)&(report_error_ctx->crc_error_ctx) - (uint32_t)report_error_ctx) ); + crc_val = compute_crc32( report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx) ); //Read report_error_ctx and check if CRC is correct for report_error_ctx - if(report_error_ctx->crc_error_ctx == crc_val) { + if (report_error_ctx->crc_error_ctx == crc_val) { uint32_t current_reboot_count = report_error_ctx->error_reboot_count; last_error_ctx.error_reboot_count = current_reboot_count + 1; } else { @@ -280,18 +280,16 @@ WEAK MBED_NORETURN mbed_error_status_t mbed_error(mbed_error_status_t error_stat } last_error_ctx.is_error_processed = 0;//Set the flag that this is a new error //Update the struct with crc - 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) ); + last_error_ctx.crc_error_ctx = compute_crc32( &last_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx) ); //Protect report_error_ctx while we update it core_util_critical_section_enter(); memcpy(report_error_ctx, &last_error_ctx, sizeof(mbed_error_ctx)); core_util_critical_section_exit(); //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 -#if MBED_CONF_PLATFORM_FATAL_ERROR_AUTO_REBOOT_ENABLED +#if MBED_CONF_PLATFORM_FATAL_ERROR_AUTO_REBOOT_ENABLED && (MBED_CONF_PLATFORM_ERROR_REBOOT_MAX > 0) system_reset();//do a system reset to get the system rebooted - while(1); #endif #endif - mbed_halt_system(); return MBED_ERROR_FAILED_OPERATION; @@ -325,12 +323,12 @@ mbed_error_status_t mbed_reset_reboot_error_info() mbed_error_status_t mbed_reset_reboot_count() { #if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED - if(is_reboot_error_valid) { + if (is_reboot_error_valid) { uint32_t crc_val = 0; core_util_critical_section_enter(); report_error_ctx->error_reboot_count = 0;//Set reboot count to 0 //Update CRC - crc_val = compute_crc32( (unsigned char *)report_error_ctx, ((uint32_t)&(report_error_ctx->crc_error_ctx) - (uint32_t)report_error_ctx) ); + crc_val = compute_crc32( report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx) ); report_error_ctx->crc_error_ctx = crc_val; core_util_critical_section_exit(); return MBED_SUCCESS; @@ -345,7 +343,7 @@ mbed_error_status_t mbed_get_reboot_error_info(mbed_error_ctx *error_info) mbed_error_status_t status = MBED_ERROR_ITEM_NOT_FOUND; #if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED if (is_reboot_error_valid) { - if(error_info != NULL) { + if (error_info != NULL) { memcpy(error_info, report_error_ctx, sizeof(mbed_error_ctx)); status = MBED_SUCCESS; } else { @@ -519,7 +517,7 @@ static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg, #endif #if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT) - if(print_thread_info == true) { + if (print_thread_info) { mbed_error_printf("\nNext:"); print_thread(osRtxInfo.thread.run.next); diff --git a/platform/mbed_lib.json b/platform/mbed_lib.json index 9f654321a9a..3fd3b851797 100644 --- a/platform/mbed_lib.json +++ b/platform/mbed_lib.json @@ -120,7 +120,7 @@ }, "error-reboot-max": { "help": "Maximum number of auto reboots permitted when an error happens.", - "value": 0 + "value": 1 }, "fatal-error-auto-reboot-enabled": { "help": "Setting this to true enables auto-reboot on a fatal error.",