refactoring: explicit struct for error-related info

This commit is contained in:
Andrey 2024-02-08 22:03:35 -05:00
parent 01299ca9cd
commit 5a258a69c4
2 changed files with 46 additions and 41 deletions

View File

@ -31,22 +31,22 @@ void checkLastBootError() {
#if EFI_BACKUP_SRAM #if EFI_BACKUP_SRAM
auto sramState = getBackupSram(); auto sramState = getBackupSram();
switch (sramState->Cookie) { switch (sramState->Err.Cookie) {
case ErrorCookie::FirmwareError: case ErrorCookie::FirmwareError:
efiPrintf("Last boot had firmware error: %s", sramState->ErrorString); efiPrintf("Last boot had firmware error: %s", sramState->Err.ErrorString);
break; break;
case ErrorCookie::HardFault: { case ErrorCookie::HardFault: {
efiPrintf("Last boot had raw: %s", sramState->rawMsg); efiPrintf("Last boot had raw: %s", sramState->Err.rawMsg);
efiPrintf("Last boot had hardFile: %s", sramState->hardFile); efiPrintf("Last boot had hardFile: %s", sramState->Err.hardFile);
efiPrintf("Last boot had line: %d", sramState->hardLine); efiPrintf("Last boot had line: %d", sramState->Err.hardLine);
efiPrintf("Last boot had check: %d", sramState->check); efiPrintf("Last boot had check: %d", sramState->Err.check);
efiPrintf("Last boot had error: %s", sramState->ErrorString); efiPrintf("Last boot had error: %s", sramState->Err.ErrorString);
efiPrintf("Last boot had hard fault type: %x addr: %x CSFR: %x", sramState->FaultType, sramState->FaultAddress, sramState->Csfr); efiPrintf("Last boot had hard fault type: %x addr: %x CSFR: %x", sramState->Err.FaultType, sramState->Err.FaultAddress, sramState->Err.Csfr);
if (engineConfiguration->rethrowHardFault) { if (engineConfiguration->rethrowHardFault) {
criticalError("Last boot had hard fault type: %x addr: %x CSFR: %x", sramState->FaultType, sramState->FaultAddress, sramState->Csfr); criticalError("Last boot had hard fault type: %x addr: %x CSFR: %x", sramState->Err.FaultType, sramState->Err.FaultAddress, sramState->Err.Csfr);
} }
auto ctx = &sramState->FaultCtx; auto ctx = &sramState->Err.FaultCtx;
efiPrintf("r0 0x%x", ctx->r0); efiPrintf("r0 0x%x", ctx->r0);
efiPrintf("r1 0x%x", ctx->r1); efiPrintf("r1 0x%x", ctx->r1);
efiPrintf("r2 0x%x", ctx->r2); efiPrintf("r2 0x%x", ctx->r2);
@ -59,7 +59,7 @@ void checkLastBootError() {
/* FPU registers - not very usefull for debug */ /* FPU registers - not very usefull for debug */
if (0) { if (0) {
// Print rest the context as a sequence of uintptr // Print rest the context as a sequence of uintptr
uintptr_t* data = reinterpret_cast<uintptr_t*>(&sramState->FaultCtx); uintptr_t* data = reinterpret_cast<uintptr_t*>(&sramState->Err.FaultCtx);
for (size_t i = 8; i < sizeof(port_extctx) / sizeof(uintptr_t); i++) { for (size_t i = 8; i < sizeof(port_extctx) / sizeof(uintptr_t); i++) {
efiPrintf("Fault ctx %d: %x", i, data[i]); efiPrintf("Fault ctx %d: %x", i, data[i]);
} }
@ -73,15 +73,15 @@ void checkLastBootError() {
} }
// Reset cookie so we don't print it again. // Reset cookie so we don't print it again.
sramState->Cookie = ErrorCookie::None; sramState->Err.Cookie = ErrorCookie::None;
if (sramState->BootCountCookie != 0xdeadbeef) { if (sramState->Err.BootCountCookie != 0xdeadbeef) {
sramState->BootCountCookie = 0xdeadbeef; sramState->Err.BootCountCookie = 0xdeadbeef;
sramState->BootCount = 0; sramState->Err.BootCount = 0;
} }
efiPrintf("Power cycle count: %d", sramState->BootCount); efiPrintf("Power cycle count: %d", sramState->Err.BootCount);
sramState->BootCount++; sramState->Err.BootCount++;
#endif // EFI_BACKUP_SRAM #endif // EFI_BACKUP_SRAM
} }
@ -89,12 +89,12 @@ void logHardFault(uint32_t type, uintptr_t faultAddress, port_extctx* ctx, uint3
criticalShutdown(); criticalShutdown();
#if EFI_BACKUP_SRAM #if EFI_BACKUP_SRAM
auto sramState = getBackupSram(); auto sramState = getBackupSram();
sramState->Cookie = ErrorCookie::HardFault; sramState->Err.Cookie = ErrorCookie::HardFault;
sramState->check = 321; sramState->Err.check = 321;
sramState->FaultType = type; sramState->Err.FaultType = type;
sramState->FaultAddress = faultAddress; sramState->Err.FaultAddress = faultAddress;
sramState->Csfr = csfr; sramState->Err.Csfr = csfr;
memcpy(&sramState->FaultCtx, ctx, sizeof(port_extctx)); memcpy(&sramState->Err.FaultCtx, ctx, sizeof(port_extctx));
#endif // EFI_BACKUP_SRAM #endif // EFI_BACKUP_SRAM
} }
@ -110,10 +110,10 @@ void chDbgPanic3(const char *msg, const char * file, int line) {
#if EFI_BACKUP_SRAM #if EFI_BACKUP_SRAM
auto sramState = getBackupSram(); auto sramState = getBackupSram();
strncpy(sramState->hardFile, file, efi::size(sramState->hardFile) - 1); strncpy(sramState->Err.hardFile, file, efi::size(sramState->Err.hardFile) - 1);
sramState->hardLine = line; sramState->Err.hardLine = line;
sramState->check = 123; sramState->Err.check = 123;
strncpy(sramState->rawMsg, msg, efi::size(sramState->rawMsg) - 1); strncpy(sramState->Err.rawMsg, msg, efi::size(sramState->Err.rawMsg) - 1);
#endif // EFI_BACKUP_SRAM #endif // EFI_BACKUP_SRAM
if (hasOsPanicError()) if (hasOsPanicError())
@ -306,8 +306,8 @@ void firmwareError(ObdCode code, const char *fmt, ...) {
#if EFI_BACKUP_SRAM #if EFI_BACKUP_SRAM
auto sramState = getBackupSram(); auto sramState = getBackupSram();
strncpy(sramState->ErrorString, criticalErrorMessageBuffer, efi::size(sramState->ErrorString)); strncpy(sramState->Err.ErrorString, criticalErrorMessageBuffer, efi::size(sramState->Err.ErrorString));
sramState->Cookie = ErrorCookie::FirmwareError; sramState->Err.Cookie = ErrorCookie::FirmwareError;
#endif // EFI_BACKUP_SRAM #endif // EFI_BACKUP_SRAM
#else #else

View File

@ -49,20 +49,25 @@ enum class ErrorCookie : uint32_t {
#if EFI_PROD_CODE #if EFI_PROD_CODE
struct BackupSramData { struct BackupSramData {
ErrorCookie Cookie;
critical_msg_t ErrorString; // Error handling/recovery/reporting information
critical_msg_t hardFile; struct {
int hardLine; ErrorCookie Cookie;
int check;
critical_msg_t rawMsg; critical_msg_t ErrorString;
port_extctx FaultCtx; critical_msg_t hardFile;
uint32_t FaultType; int hardLine;
uint32_t FaultAddress; int check;
uint32_t Csfr; critical_msg_t rawMsg;
port_extctx FaultCtx;
uint32_t FaultType;
uint32_t FaultAddress;
uint32_t Csfr;
uint32_t BootCount;
uint32_t BootCountCookie;
} Err;
uint32_t BootCount;
uint32_t BootCountCookie;
}; };
BackupSramData* getBackupSram(); BackupSramData* getBackupSram();