2017-06-05 02:02:04 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Luma3DS
|
2020-04-25 14:26:21 +02:00
|
|
|
* Copyright (C) 2016-2020 Aurora Wright, TuxSH
|
2017-06-05 02:02:04 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
|
|
|
|
* * Requiring preservation of specified reasonable legal notices or
|
|
|
|
* author attributions in that material or in the Appropriate Legal
|
|
|
|
* Notices displayed by works containing it.
|
|
|
|
* * Prohibiting misrepresentation of the origin of that material,
|
|
|
|
* or requiring that modified versions of such material be marked in
|
|
|
|
* reasonable ways as different from the original version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <3ds.h>
|
|
|
|
#include "errdisp.h"
|
|
|
|
#include "draw.h"
|
|
|
|
#include "menu.h"
|
|
|
|
#include "memory.h"
|
2017-07-19 00:55:13 +02:00
|
|
|
#include "fmt.h"
|
|
|
|
#include "ifile.h"
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2020-05-15 21:00:13 +02:00
|
|
|
extern Handle preTerminationEvent;
|
2020-04-16 01:16:25 +02:00
|
|
|
|
2017-06-05 02:02:04 +02:00
|
|
|
static inline void assertSuccess(Result res)
|
|
|
|
{
|
|
|
|
if(R_FAILED(res))
|
|
|
|
svcBreak(USERBREAK_PANIC);
|
|
|
|
}
|
|
|
|
|
2020-04-16 01:16:25 +02:00
|
|
|
static MyThread errDispThread;
|
|
|
|
static u8 ALIGN(8) errDispThreadStack[0xD00];
|
|
|
|
|
2017-06-05 02:02:04 +02:00
|
|
|
static char userString[0x100 + 1] = {0};
|
2020-04-16 01:16:25 +02:00
|
|
|
static char staticBuf[0x100 + 1] = {0};
|
|
|
|
|
|
|
|
MyThread *errDispCreateThread(void)
|
|
|
|
{
|
2020-04-26 20:36:59 +02:00
|
|
|
if(R_FAILED(MyThread_Create(&errDispThread, errDispThreadMain, errDispThreadStack, 0xD00, 55, CORE_SYSTEM)))
|
2020-04-16 01:16:25 +02:00
|
|
|
svcBreak(USERBREAK_PANIC);
|
|
|
|
return &errDispThread;
|
|
|
|
}
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
static inline u32 ERRF_DisplayRegisterValue(u32 posX, u32 posY, const char *name, u32 value)
|
|
|
|
{
|
2018-05-24 00:55:38 +02:00
|
|
|
return Draw_DrawFormattedString(posX, posY, COLOR_WHITE, "%-9s %08lx", name, value);
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
|
|
|
|
2017-07-19 00:55:13 +02:00
|
|
|
static inline int ERRF_FormatRegisterValue(char *out, const char *name, u32 value)
|
2017-06-05 02:02:04 +02:00
|
|
|
{
|
2018-05-24 00:55:38 +02:00
|
|
|
return sprintf(out, "%-9s %08lx", name, value);
|
2017-07-19 00:55:13 +02:00
|
|
|
}
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2019-10-23 01:03:00 +02:00
|
|
|
static inline void ERRF_GetErrInfo(ERRF_FatalErrInfo* info, u32* in, u32 size)
|
|
|
|
{
|
|
|
|
memcpy(info, in, size);
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:55:13 +02:00
|
|
|
static int ERRF_FormatError(char *out, ERRF_FatalErrInfo *info)
|
|
|
|
{
|
|
|
|
char *outStart = out;
|
2017-06-05 02:02:04 +02:00
|
|
|
static const char *types[] = {
|
|
|
|
"generic", "corrupted", "card removed", "exception", "result failure", "logged", "invalid"
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *exceptionTypes[] = {
|
|
|
|
"prefetch abort", "data abort", "undefined instruction", "VFP", "invalid"
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *type = (u32)info->type > (u32)ERRF_ERRTYPE_LOGGED ? types[6] : types[(u32)info->type];
|
|
|
|
|
|
|
|
if(info->type == ERRF_ERRTYPE_EXCEPTION)
|
|
|
|
{
|
|
|
|
const char *exceptionType = (u32)info->data.exception_data.excep.type > (u32)ERRF_EXCEPTION_VFP ?
|
|
|
|
exceptionTypes[4] : exceptionTypes[(u32)info->data.exception_data.excep.type];
|
|
|
|
|
2017-07-19 00:55:13 +02:00
|
|
|
out += sprintf(out, "Error type: exception (%s)\n", exceptionType);
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
|
|
|
else
|
2017-07-19 00:55:13 +02:00
|
|
|
out += sprintf(out, "Error type: %s\n", type);
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
if(info->type != ERRF_ERRTYPE_CARD_REMOVED)
|
|
|
|
{
|
|
|
|
Handle processHandle;
|
|
|
|
Result res;
|
|
|
|
|
2018-05-24 00:55:38 +02:00
|
|
|
out += sprintf(out, "\nProcess ID: %lu\n", info->procId);
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
res = svcOpenProcess(&processHandle, info->procId);
|
|
|
|
if(R_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
u64 titleId;
|
|
|
|
char name[9] = { 0 };
|
|
|
|
svcGetProcessInfo((s64 *)name, processHandle, 0x10000);
|
|
|
|
svcGetProcessInfo((s64 *)&titleId, processHandle, 0x10001);
|
|
|
|
svcCloseHandle(processHandle);
|
2017-07-19 00:55:13 +02:00
|
|
|
out += sprintf(out, "Process name: %s\n", name);
|
|
|
|
out += sprintf(out, "Process title ID: 0x%016llx\n", titleId);
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
|
|
|
|
2017-07-19 00:55:13 +02:00
|
|
|
out += sprintf(out, "\n");
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(info->type == ERRF_ERRTYPE_EXCEPTION)
|
|
|
|
{
|
|
|
|
static const char *registerNames[] = {
|
|
|
|
"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12",
|
|
|
|
"sp", "lr", "pc", "cpsr"
|
|
|
|
};
|
|
|
|
|
|
|
|
u32 *regs = (u32 *)(&info->data.exception_data.regs);
|
|
|
|
for(u32 i = 0; i < 17; i += 2)
|
|
|
|
{
|
2017-07-19 00:55:13 +02:00
|
|
|
out += ERRF_FormatRegisterValue(out, registerNames[i], regs[i]);
|
2017-06-05 02:02:04 +02:00
|
|
|
if(i != 16)
|
2017-07-19 00:55:13 +02:00
|
|
|
{
|
|
|
|
out += sprintf(out, " ");
|
|
|
|
out += ERRF_FormatRegisterValue(out, registerNames[i + 1], i == 16 ? regs[20] : regs[i + 1]);
|
|
|
|
out += sprintf(out, "\n");
|
|
|
|
}
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(info->data.exception_data.excep.type == ERRF_EXCEPTION_PREFETCH_ABORT
|
|
|
|
|| info->data.exception_data.excep.type == ERRF_EXCEPTION_DATA_ABORT)
|
|
|
|
{
|
2017-07-19 00:55:13 +02:00
|
|
|
out += sprintf(out, " ");
|
|
|
|
out += ERRF_FormatRegisterValue(out, "far", info->data.exception_data.excep.far);
|
|
|
|
out += sprintf(out, "\n");
|
|
|
|
out += ERRF_FormatRegisterValue(out, "fsr", info->data.exception_data.excep.fsr);
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if(info->data.exception_data.excep.type == ERRF_EXCEPTION_VFP)
|
|
|
|
{
|
2017-07-19 00:55:13 +02:00
|
|
|
out += sprintf(out, " ");
|
|
|
|
out += ERRF_FormatRegisterValue(out, "fpexc", info->data.exception_data.excep.fpexc);
|
|
|
|
out += sprintf(out, "\n");
|
|
|
|
out += ERRF_FormatRegisterValue(out, "fpinst", info->data.exception_data.excep.fpinst);
|
|
|
|
out += sprintf(out, " ");
|
|
|
|
out += ERRF_FormatRegisterValue(out, "fpinst2", info->data.exception_data.excep.fpinst2);
|
|
|
|
out += sprintf(out, "\n");
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
|
|
|
|
2017-07-19 00:55:13 +02:00
|
|
|
out += sprintf(out, "\n");
|
|
|
|
}
|
2017-06-05 02:02:04 +02:00
|
|
|
else if(info->type != ERRF_ERRTYPE_CARD_REMOVED)
|
|
|
|
{
|
|
|
|
if(info->type != ERRF_ERRTYPE_FAILURE)
|
2018-05-24 00:55:38 +02:00
|
|
|
out += sprintf(out, "Address: 0x%08lx\n", info->pcAddr);
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2018-05-24 00:55:38 +02:00
|
|
|
out += sprintf(out, "Error code: 0x%08lx\n", info->resCode);
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *desc;
|
|
|
|
switch(info->type)
|
|
|
|
{
|
|
|
|
case ERRF_ERRTYPE_CARD_REMOVED:
|
2017-06-13 02:00:41 +02:00
|
|
|
desc = "The card was removed.";
|
2017-06-05 02:02:04 +02:00
|
|
|
break;
|
|
|
|
case ERRF_ERRTYPE_MEM_CORRUPT:
|
|
|
|
desc = "The System Memory has been damaged.";
|
|
|
|
break;
|
|
|
|
case ERRF_ERRTYPE_FAILURE:
|
2019-10-23 01:03:00 +02:00
|
|
|
info->data.failure_mesg[0x5F] = 0; // make sure the last byte in the IPC buffer is NULL
|
2017-06-05 02:02:04 +02:00
|
|
|
desc = info->data.failure_mesg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
desc = "";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(desc[0] != 0)
|
2017-07-19 00:55:13 +02:00
|
|
|
out += sprintf(out, "\n%s\n", desc);
|
|
|
|
out += sprintf(out, "\n");
|
|
|
|
return out - outStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void ERRF_DisplayError(ERRF_FatalErrInfo *info)
|
|
|
|
{
|
|
|
|
Draw_Lock();
|
|
|
|
|
|
|
|
u32 posY = Draw_DrawString(10, 10, COLOR_RED, userString[0] == 0 ? "An error occurred (ErrDisp)" : userString);
|
|
|
|
char buf[0x400];
|
2018-06-14 18:13:57 +02:00
|
|
|
|
2017-07-19 00:55:13 +02:00
|
|
|
ERRF_FormatError(buf, info);
|
|
|
|
posY = posY < 30 ? 30 : posY;
|
|
|
|
|
|
|
|
posY = Draw_DrawString(10, posY, COLOR_WHITE, buf);
|
|
|
|
posY = Draw_DrawString(10, posY + SPACING_Y, COLOR_WHITE, "Press any button to reboot.");
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
Draw_FlushFramebuffer();
|
|
|
|
Draw_Unlock();
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:55:13 +02:00
|
|
|
static Result ERRF_SaveErrorToFile(ERRF_FatalErrInfo *info)
|
|
|
|
{
|
|
|
|
char buf[0x400];
|
|
|
|
|
|
|
|
FS_ArchiveID archiveId;
|
|
|
|
s64 out;
|
|
|
|
u64 size, total;
|
|
|
|
bool isSdMode;
|
|
|
|
int n = 0;
|
|
|
|
Result res = 0;
|
|
|
|
IFile file;
|
|
|
|
|
|
|
|
n = ERRF_FormatError(buf, info);
|
|
|
|
n += sprintf(buf + n, "-------------------------------------\n\n");
|
|
|
|
|
|
|
|
if(R_FAILED(svcGetSystemInfo(&out, 0x10000, 0x203))) svcBreak(USERBREAK_ASSERT);
|
|
|
|
isSdMode = (bool)out;
|
|
|
|
|
|
|
|
archiveId = isSdMode ? ARCHIVE_SDMC : ARCHIVE_NAND_RW;
|
|
|
|
res = IFile_Open(&file, archiveId, fsMakePath(PATH_EMPTY, ""), fsMakePath(PATH_ASCII, "/luma/errdisp.txt"), FS_OPEN_WRITE | FS_OPEN_CREATE);
|
2018-06-14 18:13:57 +02:00
|
|
|
|
2017-07-19 00:55:13 +02:00
|
|
|
if(R_FAILED(res))
|
|
|
|
return res;
|
2018-06-14 18:13:57 +02:00
|
|
|
|
2017-07-19 00:55:13 +02:00
|
|
|
res = IFile_GetSize(&file, &size);
|
|
|
|
if(R_FAILED(res))
|
|
|
|
{
|
|
|
|
IFile_Close(&file);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
file.pos = size;
|
|
|
|
|
|
|
|
res = IFile_Write(&file, &total, buf, (u32)n, 0);
|
|
|
|
IFile_Close(&file);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-04-16 01:16:25 +02:00
|
|
|
void ERRF_HandleCommands(void)
|
2017-06-05 02:02:04 +02:00
|
|
|
{
|
|
|
|
u32 *cmdbuf = getThreadCommandBuffer();
|
2019-10-23 01:03:00 +02:00
|
|
|
ERRF_FatalErrInfo info;
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
switch(cmdbuf[0] >> 16)
|
|
|
|
{
|
|
|
|
case 1: // Throw
|
|
|
|
{
|
2019-10-23 01:03:00 +02:00
|
|
|
ERRF_GetErrInfo(&info, (cmdbuf + 1), sizeof(ERRF_FatalErrInfo));
|
|
|
|
ERRF_SaveErrorToFile(&info);
|
2020-05-15 21:00:13 +02:00
|
|
|
if(!menuShouldExit && (info.type != ERRF_ERRTYPE_LOGGED || info.procId == 0))
|
2017-07-19 00:55:13 +02:00
|
|
|
{
|
|
|
|
menuEnter();
|
|
|
|
|
|
|
|
Draw_Lock();
|
|
|
|
Draw_ClearFramebuffer();
|
|
|
|
Draw_FlushFramebuffer();
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2019-10-23 01:03:00 +02:00
|
|
|
ERRF_DisplayError(&info);
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2017-07-19 00:55:13 +02:00
|
|
|
/*
|
|
|
|
If we ever wanted to return:
|
|
|
|
Draw_Unlock();
|
|
|
|
menuLeave();
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2017-07-19 00:55:13 +02:00
|
|
|
but we don't
|
|
|
|
*/
|
|
|
|
waitInput();
|
|
|
|
svcKernelSetState(7);
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2020-04-16 01:16:25 +02:00
|
|
|
cmdbuf[0] = IPC_MakeHeader(1, 1, 0);
|
2017-06-05 02:02:04 +02:00
|
|
|
cmdbuf[1] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 2: // SetUserString
|
|
|
|
{
|
2020-04-16 01:16:25 +02:00
|
|
|
if(cmdbuf[0] != IPC_MakeHeader(2, 1, 2) || (cmdbuf[2] & 0x3C0F) != 2)
|
2017-06-05 02:02:04 +02:00
|
|
|
{
|
2020-04-16 01:16:25 +02:00
|
|
|
cmdbuf[0] = IPC_MakeHeader(0, 1, 0);
|
2017-06-05 02:02:04 +02:00
|
|
|
cmdbuf[1] = 0xD9001830;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-08-13 09:11:45 +02:00
|
|
|
u32 sz = cmdbuf[1] <= 0x100 ? cmdbuf[1] : 0x100;
|
2017-06-05 02:02:04 +02:00
|
|
|
memcpy(userString, cmdbuf + 3, sz);
|
|
|
|
userString[sz] = 0;
|
2020-04-16 01:16:25 +02:00
|
|
|
|
|
|
|
cmdbuf[0] = IPC_MakeHeader(2, 1, 0);
|
|
|
|
cmdbuf[1] = 0;
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-16 01:16:25 +02:00
|
|
|
|
|
|
|
void errDispThreadMain(void)
|
|
|
|
{
|
|
|
|
Handle handles[3];
|
|
|
|
Handle serverHandle, clientHandle, sessionHandle = 0;
|
|
|
|
|
|
|
|
u32 replyTarget = 0;
|
|
|
|
s32 index;
|
|
|
|
|
|
|
|
Result res;
|
|
|
|
u32 *cmdbuf = getThreadCommandBuffer();
|
|
|
|
u32 *sbuf = getThreadStaticBuffers();
|
|
|
|
|
|
|
|
sbuf[0] = IPC_Desc_StaticBuffer(0x100, 0);
|
|
|
|
sbuf[1] = (u32)staticBuf;
|
|
|
|
|
|
|
|
assertSuccess(svcCreatePort(&serverHandle, &clientHandle, "err:f", 1));
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2020-05-15 21:00:13 +02:00
|
|
|
handles[0] = preTerminationEvent;
|
2020-04-16 01:16:25 +02:00
|
|
|
handles[1] = serverHandle;
|
|
|
|
handles[2] = sessionHandle;
|
|
|
|
|
|
|
|
if(replyTarget == 0) // k11
|
|
|
|
cmdbuf[0] = 0xFFFF0000;
|
|
|
|
res = svcReplyAndReceive(&index, handles, 1 + (sessionHandle == 0 ? 1 : 2), replyTarget);
|
|
|
|
|
|
|
|
if(R_FAILED(res))
|
|
|
|
{
|
|
|
|
if((u32)res == 0xC920181A) // session closed by remote
|
|
|
|
{
|
|
|
|
svcCloseHandle(sessionHandle);
|
|
|
|
sessionHandle = 0;
|
|
|
|
replyTarget = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
svcBreak(USERBREAK_PANIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (index == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(index == 1)
|
|
|
|
{
|
|
|
|
Handle session;
|
|
|
|
assertSuccess(svcAcceptSession(&session, serverHandle));
|
|
|
|
|
|
|
|
if(sessionHandle == 0)
|
|
|
|
sessionHandle = session;
|
|
|
|
else
|
|
|
|
svcCloseHandle(session);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERRF_HandleCommands();
|
|
|
|
replyTarget = sessionHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 21:00:13 +02:00
|
|
|
while(!preTerminationRequested);
|
2020-04-16 01:16:25 +02:00
|
|
|
|
|
|
|
svcCloseHandle(sessionHandle);
|
|
|
|
svcCloseHandle(clientHandle);
|
|
|
|
svcCloseHandle(serverHandle);
|
|
|
|
}
|