2016-04-26 22:06:19 +02:00
|
|
|
/*
|
|
|
|
* exceptions.c
|
|
|
|
* by TuxSH
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "exceptions.h"
|
|
|
|
#include "fs.h"
|
|
|
|
#include "memory.h"
|
2016-05-03 17:05:19 +02:00
|
|
|
#include "screeninit.h"
|
|
|
|
#include "draw.h"
|
|
|
|
#include "i2c.h"
|
2016-04-26 22:06:19 +02:00
|
|
|
#include "utils.h"
|
|
|
|
#include "../build/arm9_exceptions.h"
|
2016-06-02 22:33:44 +02:00
|
|
|
#include "../build/arm11_exceptions.h"
|
|
|
|
|
2016-04-26 22:06:19 +02:00
|
|
|
void installArm9Handlers(void)
|
|
|
|
{
|
2016-06-03 21:38:35 +02:00
|
|
|
void *payloadAddress = (void *)0x01FF8000;
|
2016-05-28 22:05:07 +02:00
|
|
|
const u32 offsets[] = {0x08, 0x18, 0x20, 0x28};
|
|
|
|
|
2016-06-03 21:38:35 +02:00
|
|
|
memcpy(payloadAddress, arm9_exceptions + 32, arm9_exceptions_size - 32);
|
|
|
|
|
2016-05-28 22:05:07 +02:00
|
|
|
//IRQHandler is at 0x08000000, but we won't handle it for some reasons
|
|
|
|
//svcHandler is at 0x08000010, but we won't handle svc either
|
|
|
|
|
|
|
|
for(u32 i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
*(vu32 *)(0x08000000 + offsets[i]) = 0xE51FF004;
|
2016-06-03 21:38:35 +02:00
|
|
|
*(vu32 *)(0x08000000 + offsets[i] + 4) = *((const u32 *)arm9_exceptions + 1 + i);
|
2016-05-28 22:05:07 +02:00
|
|
|
}
|
2016-04-26 22:06:19 +02:00
|
|
|
}
|
|
|
|
|
2016-06-02 22:33:44 +02:00
|
|
|
#define MAKE_BRANCH(src,dst) (0xEA000000 | ((u32)((((u8 *)(dst) - (u8 *)(src)) >> 2) - 2) & 0xFFFFFF))
|
|
|
|
#define MAKE_BRANCH_LINK(src,dst) (0xEB000000 | ((u32)((((u8 *)(dst) - (u8 *)(src)) >> 2) - 2) & 0xFFFFFF))
|
|
|
|
|
|
|
|
void installArm11Handlers(u32 *exceptionsPage, u32 stackAddr)
|
|
|
|
{
|
|
|
|
u32 *initFPU;
|
|
|
|
for(initFPU = exceptionsPage; initFPU < (exceptionsPage + 0x400) && (initFPU[0] != 0xE59F0008 || initFPU[1] != 0xE5900000); initFPU += 1);
|
|
|
|
|
|
|
|
u32 *mcuReboot;
|
|
|
|
for(mcuReboot = exceptionsPage; mcuReboot < (exceptionsPage + 0x400) && (mcuReboot[0] != 0xE59F4104 || mcuReboot[1] != 0xE3A0A0C2); mcuReboot += 1);
|
2016-06-03 21:38:35 +02:00
|
|
|
mcuReboot--;
|
2016-06-02 22:33:44 +02:00
|
|
|
|
|
|
|
u32 *freeSpace;
|
|
|
|
for(freeSpace = initFPU; freeSpace < (exceptionsPage + 0x400) && (freeSpace[0] != 0xFFFFFFFF || freeSpace[1] != 0xFFFFFFFF); freeSpace += 1);
|
2016-06-03 21:38:35 +02:00
|
|
|
memcpy(freeSpace, arm11_exceptions + 32, arm11_exceptions_size - 32);
|
2016-06-02 22:33:44 +02:00
|
|
|
|
2016-06-03 21:38:35 +02:00
|
|
|
exceptionsPage[1] = MAKE_BRANCH(exceptionsPage + 1, (u8 *)freeSpace + *(u32 *)(arm11_exceptions + 8) - 32); //Undefined Instruction
|
|
|
|
exceptionsPage[3] = MAKE_BRANCH(exceptionsPage + 3, (u8 *)freeSpace + *(u32 *)(arm11_exceptions + 12) - 32); //Prefetch Abort
|
|
|
|
exceptionsPage[4] = MAKE_BRANCH(exceptionsPage + 4, (u8 *)freeSpace + *(u32 *)(arm11_exceptions + 16) - 32); //Data Abort
|
|
|
|
exceptionsPage[7] = MAKE_BRANCH(exceptionsPage + 7, (u8 *)freeSpace + *(u32 *)(arm11_exceptions + 4) - 32); //FIQ
|
2016-06-02 22:33:44 +02:00
|
|
|
|
2016-06-03 21:38:35 +02:00
|
|
|
for(u32 *pos = freeSpace; pos < (u32 *)((u8 *)freeSpace + arm11_exceptions_size - 32); pos++)
|
2016-06-02 22:33:44 +02:00
|
|
|
{
|
2016-06-03 21:38:35 +02:00
|
|
|
switch(*pos) //Perform relocations
|
2016-06-02 22:33:44 +02:00
|
|
|
{
|
|
|
|
case 0xFFFF3000: *pos = stackAddr; break;
|
|
|
|
case 0xEBFFFFFE: *pos = MAKE_BRANCH_LINK(pos, initFPU); break;
|
|
|
|
case 0xEAFFFFFE: *pos = MAKE_BRANCH(pos, mcuReboot); break;
|
2016-06-03 21:38:35 +02:00
|
|
|
case 0xE12FFF1C: pos[1] = 0xFFFF0000 + 4 * (u32)(freeSpace - exceptionsPage) + pos[1] - 32; break; // bx r12 (mainHandler)
|
2016-06-02 22:33:44 +02:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 15:15:25 +02:00
|
|
|
static void hexItoa(u32 n, char *out)
|
2016-05-06 22:50:01 +02:00
|
|
|
{
|
2016-05-27 15:15:25 +02:00
|
|
|
const char hexDigits[] = "0123456789ABCDEF";
|
|
|
|
u32 i = 0;
|
|
|
|
|
2016-05-06 22:50:01 +02:00
|
|
|
while(n > 0)
|
|
|
|
{
|
2016-05-27 15:15:25 +02:00
|
|
|
out[7 - i++] = hexDigits[n & 0xF];
|
2016-05-06 22:50:01 +02:00
|
|
|
n >>= 4;
|
|
|
|
}
|
2016-05-27 15:15:25 +02:00
|
|
|
|
|
|
|
for(; i < 8; i++) out[7 - i] = '0';
|
2016-05-06 22:50:01 +02:00
|
|
|
}
|
|
|
|
|
2016-04-26 22:06:19 +02:00
|
|
|
void detectAndProcessExceptionDumps(void)
|
|
|
|
{
|
2016-05-27 15:15:25 +02:00
|
|
|
const char *handledExceptionNames[] = {
|
2016-05-06 22:50:01 +02:00
|
|
|
"FIQ", "undefined instruction", "prefetch abort", "data abort"
|
|
|
|
};
|
|
|
|
|
2016-05-27 15:15:25 +02:00
|
|
|
const char *registerNames[] = {
|
2016-05-06 22:50:01 +02:00
|
|
|
"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12",
|
2016-06-02 22:33:44 +02:00
|
|
|
"SP", "LR", "PC", "CPSR", "FPEXC"
|
2016-05-06 22:50:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
char hexstring[] = "00000000";
|
|
|
|
|
2016-06-02 22:33:44 +02:00
|
|
|
vu32 *dump = (vu32 *)0x25000000;
|
2016-04-26 22:06:19 +02:00
|
|
|
|
2016-06-02 22:33:44 +02:00
|
|
|
if(dump[0] == 0xDEADC0DE && dump[1] == 0xDEADCAFE && (dump[3] == 9 || (dump[3] & 0xFFFF) == 11))
|
2016-04-26 22:06:19 +02:00
|
|
|
{
|
2016-06-02 22:33:44 +02:00
|
|
|
char path9[41] = "/luma/dumps/arm9";
|
|
|
|
char path11[42] = "/luma/dumps/arm11";
|
2016-04-26 22:06:19 +02:00
|
|
|
char fileName[] = "crash_dump_00000000.dmp";
|
2016-06-04 21:11:15 +02:00
|
|
|
u32 size = dump[5];
|
2016-04-26 22:06:19 +02:00
|
|
|
|
2016-06-02 22:33:44 +02:00
|
|
|
if(dump[3] == 9)
|
|
|
|
{
|
|
|
|
findDumpFile(path9, fileName);
|
|
|
|
path9[16] = '/';
|
|
|
|
memcpy(&path9[17], fileName, sizeof(fileName));
|
2016-06-04 21:11:15 +02:00
|
|
|
fileWrite((void *)dump, path9, size);
|
2016-06-02 22:33:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
findDumpFile(path11, fileName);
|
|
|
|
path11[17] = '/';
|
|
|
|
memcpy(&path11[18], fileName, sizeof(fileName));
|
|
|
|
fileWrite((void *)dump, path11, dump[5]);
|
|
|
|
}
|
|
|
|
|
2016-04-26 22:06:19 +02:00
|
|
|
|
2016-06-02 22:33:44 +02:00
|
|
|
char arm11Str[] = "Processor: ARM11 (core X)";
|
|
|
|
if((dump[3] & 0xFFFF) == 11) arm11Str[28] = '0' + (char)(dump[3] >> 16);
|
|
|
|
|
2016-05-03 17:05:19 +02:00
|
|
|
initScreens();
|
|
|
|
|
2016-05-06 22:50:01 +02:00
|
|
|
drawString("An exception occurred", 10, 10, COLOR_RED);
|
2016-06-02 22:33:44 +02:00
|
|
|
int posY = drawString(((dump[3] & 0xFFFF) == 11) ? arm11Str : "Processor: ARM9", 10, 30, COLOR_WHITE) + SPACING_Y;
|
2016-05-06 22:50:01 +02:00
|
|
|
posY = drawString("Exception type: ", 10, posY, COLOR_WHITE);
|
|
|
|
posY = drawString(handledExceptionNames[dump[4]], 10 + 16 * SPACING_X, posY, COLOR_WHITE);
|
2016-06-04 21:11:15 +02:00
|
|
|
if(dump[4] == 2 && dump[7] >= 4 && (dump[10 + 16] & 0x20) == 0 && *(vu32 *)((vu8 *)dump + 40 + dump[6] + dump[7] - 4) == 0xE12FFF7F)
|
|
|
|
posY = drawString("(svcBreak)", 10 + 31 * SPACING_X, posY, COLOR_WHITE);
|
|
|
|
|
2016-05-06 22:50:01 +02:00
|
|
|
posY += 3 * SPACING_Y;
|
|
|
|
for(u32 i = 0; i < 17; i += 2)
|
|
|
|
{
|
|
|
|
posY = drawString(registerNames[i], 10, posY, COLOR_WHITE);
|
|
|
|
hexItoa(dump[10 + i], hexstring);
|
|
|
|
posY = drawString(hexstring, 10 + 7 * SPACING_X, posY, COLOR_WHITE);
|
2016-05-27 15:15:25 +02:00
|
|
|
|
2016-06-02 22:33:44 +02:00
|
|
|
if(dump[3] != 9 || i != 16)
|
2016-05-06 22:50:01 +02:00
|
|
|
{
|
|
|
|
posY = drawString(registerNames[i + 1], 10 + 22 * SPACING_X, posY, COLOR_WHITE);
|
|
|
|
hexItoa(dump[10 + i + 1], hexstring);
|
|
|
|
posY = drawString(hexstring, 10 + 29 * SPACING_X, posY, COLOR_WHITE);
|
|
|
|
}
|
2016-05-27 15:15:25 +02:00
|
|
|
|
2016-05-06 22:50:01 +02:00
|
|
|
posY += SPACING_Y;
|
|
|
|
}
|
2016-05-27 15:15:25 +02:00
|
|
|
|
2016-05-06 22:50:01 +02:00
|
|
|
posY += 2 * SPACING_Y;
|
2016-06-03 21:38:35 +02:00
|
|
|
|
2016-06-04 21:11:15 +02:00
|
|
|
u32 mode = dump[10 + 16] & 0xF;
|
2016-06-03 21:38:35 +02:00
|
|
|
if(dump[4] == 3 && (mode == 7 || mode == 11))
|
|
|
|
posY = drawString("Incorrect dump: failed to dump code and/or stack", 10, posY, 0x00FFFF) + 2 * SPACING_Y; //in yellow
|
|
|
|
|
2016-05-06 22:50:01 +02:00
|
|
|
posY = drawString("You can find a dump in the following file:", 10, posY, COLOR_WHITE) + SPACING_Y;
|
2016-06-02 22:33:44 +02:00
|
|
|
posY = drawString((dump[3] == 9) ? path9 : path11, 10, posY, COLOR_WHITE) + 2 * SPACING_Y;
|
2016-05-06 22:50:01 +02:00
|
|
|
drawString("Press any button to shutdown", 10, posY, COLOR_WHITE);
|
2016-05-27 15:15:25 +02:00
|
|
|
|
2016-05-03 17:05:19 +02:00
|
|
|
waitInput();
|
2016-06-03 21:38:35 +02:00
|
|
|
|
2016-06-04 21:11:15 +02:00
|
|
|
for(u32 i = 0; i < size / 4; i++) dump[i] = 0;
|
2016-06-03 21:38:35 +02:00
|
|
|
|
|
|
|
clearScreens();
|
|
|
|
|
|
|
|
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1); //Shutdown
|
2016-05-03 17:05:19 +02:00
|
|
|
while(1);
|
2016-04-26 22:06:19 +02:00
|
|
|
}
|
|
|
|
}
|