9#include "rusefi/efistringutil.h"
14#include "rusefi/critical_error.h"
15#include "rusefi/efistring.h"
24using namespace rusefi::stringutil;
29#define bkpt() __asm volatile("BKPT #0\n")
51 criticalError(message);
83 memset(&sramState->err, 0x00,
sizeof(sramState->err));
96 if (sramState->BootCountCookie != 0xdeadbeef) {
97 sramState->BootCountCookie = 0xdeadbeef;
98 sramState->BootCount = 0;
103 sramState->BootCount++;
108 criticalError(
"Last boot had hard fault type: %lx addr: %lx CSFR: %lx",
144 return "ChibiOS panic";
150#define printResetReason() \
151 PRINT("Reset Cause: %s", getMCUResetCause(getMCUResetCause()))
154#define printWdResetCounter() \
156 uint8_t wd_counter = 0; \
157 SharedParamsReadByIndex(1, &wd_counter); \
158 PRINT("WD resets: %u", (unsigned int)wd_counter); \
161#define printWdResetCounter() \
166#define printErrorState() \
168 PRINT("Power cycle count: %lu", bootCount); \
170 if (cookie == ErrorCookie::None) { \
174 PRINT("Last error type %s", errorCookieToName(err->Cookie)); \
177 case ErrorCookie::FirmwareError: \
179 PRINT("%s", err->msg); \
182 case ErrorCookie::HardFault: \
184 PRINT("type: 0x%08lx addr: 0x%08lx CSFR: 0x%08lx", \
185 err->FaultType, err->FaultAddress, err->Csfr); \
187 auto ctx = &err->FaultCtx; \
188 PRINT("r0 0x%08lx", ctx->r0); \
189 PRINT("r1 0x%08lx", ctx->r1); \
190 PRINT("r2 0x%08lx", ctx->r2); \
191 PRINT("r3 0x%08lx", ctx->r3); \
192 PRINT("r12 0x%08lx", ctx->r12); \
193 PRINT("lr (thread) 0x%08lx", ctx->lr_thd); \
194 PRINT("pc 0x%08lx", ctx->pc); \
195 PRINT("xpsr 0x%08lx", ctx->xpsr); \
200 uintptr_t* data = reinterpret_cast<uintptr_t*>(&err->FaultCtx); \
201 for (size_t i = 8; i < sizeof(port_extctx) / sizeof(uintptr_t); i++) { \
202 PRINT("Fault ctx %d: 0x%08x", i, data[i]); \
207 case ErrorCookie::ChibiOsPanic: \
209 PRINT("msg %s", err->msg); \
210 PRINT("file %s", err->file); \
211 PRINT("line %d", err->line); \
220#define printErrorStack() \
222 PRINT("SP 0x%08lx", err->sp); \
223 for (size_t i = 0; i < ERROR_STACK_DEPTH; i++) { \
224 uint32_t cur = err->stack[i]; \
226 PRINT(" 0x%08lx: 0x%08lx", err->sp - i * 4, cur); \
234 #define PRINT(...) efiPrintf(__VA_ARGS__)
237 printWdResetCounter();
252#define FAIL_REPORT_PREFIX "fail"
296 char fileName[_MAX_FILLER + 20];
297 memset(
fd, 0,
sizeof(FIL));
300 sprintf(fileName,
"%05ld_%s_%s.txt",
303 sprintf(fileName,
"last_%s_%s.txt",
307 FRESULT ret = f_open(
fd, fileName, FA_CREATE_ALWAYS | FA_WRITE);
310 #define PRINT(format, ...) f_printf(fd, format "\r\n", __VA_ARGS__)
312 printWdResetCounter();
336 fr = f_findfirst(&dj, &fno,
"", pattern);
337 exist = ((fr == FR_OK) && (fno.fname[0]));
362 fr = f_findfirst(&dj, &fno,
"", pattern);
365 if ((fr == FR_OK) && (fno.fname[0])) {
366 efiPrintf(
"deleting %s", fno.fname);
367 FRESULT ret = f_unlink(fno.fname);
369 efiPrintf(
"Faield to delete %s: %d", fno.fname, ret);
372 efiPrintf(
"%s removed", fno.fname);
375 }
while ((!failed) && (fr == FR_OK) && (fno.fname[0]));
392 err->
sp = (uint32_t)sp;
393 for (
size_t i = 0; i < ERROR_STACK_DEPTH; i++) {
396 err->
stack[i] = *(
volatile uint32_t *)sp;
402void logHardFault(uint32_t type, uintptr_t faultAddress,
void* sp, port_extctx* ctx, uint32_t csfr) {
408 auto err = &bkpram->err;
410 err->FaultType = type;
411 err->FaultAddress = faultAddress;
413 memcpy(&err->FaultCtx, ctx,
sizeof(port_extctx));
426#if EFI_SIMULATOR || EFI_PROD_CODE
433 uint32_t tmp = 0xfffffa11;
436 if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
444 auto err = &bkpram->err;
446 strlncpy(err->file, file, efi::size(err->file));
448 strlncpy(err->msg, msg, efi::size(err->msg));
456 if (hasOsPanicError())
460#if CH_DBG_SYSTEM_STATE_CHECK
461 ch0.dbg.panic_msg = msg;
465 printf(
"chDbgPanic3 %s %s%d", msg, file, line);
469 criticalError(
"assert fail %s %s:%d", msg, file, line);
472 if (chThdGetSelfX()->threadId == 0) {
474 chSysUnconditionalUnlock();
506#if EFI_SIMULATOR || EFI_PROD_CODE
519#if EFI_SIMULATOR || EFI_PROD_CODE
589#if EFI_SIMULATOR || EFI_UNIT_TEST
610 uint32_t tmp = 0xfaaaaa11;
617 if (indexOf(fmt,
'%') == -1) {
629 static char versionBuffer[32];
630 chsnprintf(versionBuffer,
sizeof(versionBuffer),
" %d@%s",
getRusEfiVersion(), FIRMWARE_ID);
639 auto err = &bkpram->err;
649#if EFI_ENGINE_CONTROL
660 static char errorBuffer[200];
662 vsnprintf(errorBuffer,
sizeof(errorBuffer), fmt, ap);
664 printf(
"\x1B[31m>>>>>>>>>> firmwareError [%s]\r\n\x1B[0m\r\n", errorBuffer);
666 throw std::logic_error(errorBuffer);
Reset_Cause_t getMCUResetCause()
Non-volatile backup-RAM registers support.
BackupSramData * getBackupSram()
OutputPin communicationLedPin
WarningCodeState warnings
void setValue(const char *msg, int logicValue, bool isForce=false)
void addWarningCode(ObdCode code, const char *text=nullptr)
bool isWarningNow() const
void addConsoleAction(const char *token, Void callback)
Register console action without parameters.
LimpManager * getLimpManager()
static EngineAccessor engine
static constexpr engine_configuration_s * engineConfiguration
bool warning(ObdCode code, const char *fmt,...)
static int errorHandlerIsReportExist(ErrorCookie cookie)
static critical_msg_t warningBuffer
void firmwareError(ObdCode code, const char *fmt,...)
bool warningTsReport(ObdCode code, const char *fmt,...)
static void firmwareErrorV(ObdCode code, const char *fmt, va_list ap)
void logHardFault(uint32_t type, uintptr_t faultAddress, void *sp, port_extctx *ctx, uint32_t csfr)
int errorHandlerCheckReportFiles()
bool warningVA(ObdCode code, bool reportToTs, const char *fmt, va_list args)
uint32_t maxLockedDuration
static bool hasConfigErrorFlag
const char * getCriticalErrorMessage()
bool errorHandlerIsStartFromError()
static uint32_t bootCount
void clearConfigErrorMessage()
void efiCriticalError(const char *message)
static void errorHandlerDeleteTypedReport(ErrorCookie cookie)
bool hasCriticalFirmwareErrorFlag
static critical_msg_t criticalErrorMessageBuffer
void errorHandlerDeleteReports()
const char * errorCookieToName(ErrorCookie cookie)
static const char * errorHandlerGetErrorName(ErrorCookie cookie)
const char * dbg_panic_file
static backupErrorState lastBootError
void configError(const char *fmt,...)
static void errorHandlerSaveStack(backupErrorState *err, uint32_t *sp)
PUBLIC_API_WEAK void onBoardWriteErrorFile(FIL *)
void criticalErrorC(const char *fmt,...)
bool hasErrorReportFile()
bool isInHardFaultHandler
void errorHandlerShowBootReasonAndErrors()
static bool hasReportFile
void chDbgPanic3(const char *msg, const char *file, int line)
void errorHandlerWriteReportFile(FIL *fd)
static critical_msg_t configErrorMessageBuffer
const char * getConfigErrorMessage()
char[CRITICAL_BUFFER_SIZE] critical_msg_t
uint32_t getTimeNowLowerNt()
@ RUNTIME_CRITICAL_TEST_ERROR
@ OBD_PCM_Processor_Fault
void perfEventInstantGlobal(PE event)
void onAssertionFailure()
uint32_t stack[ERROR_STACK_DEPTH]