diff --git a/Makefile.arm9 b/Makefile.arm9 index 899eee9..cdcf5b5 100644 --- a/Makefile.arm9 +++ b/Makefile.arm9 @@ -35,7 +35,7 @@ -Wformat=2 -Wlogical-op -Wmissing-include-dirs -Wredundant-decls \ -Wshadow -Wsign-conversion -fstrict-overflow -Wstrict-overflow=5 \ -Wswitch-default -Wundef -fstrict-aliasing -Wstrict-aliasing \ - -std=c11 -O2 -flto -mword-relocations -fomit-frame-pointer \ + -std=c11 -Og -mword-relocations -fomit-frame-pointer \ -ffast-math -ffunction-sections $(ARCH) CFLAGS += $(INCLUDE) -DARM9 $(DEFINES) diff --git a/source/arm9/debug.c b/source/arm9/debug.c new file mode 100644 index 0000000..e50d211 --- /dev/null +++ b/source/arm9/debug.c @@ -0,0 +1,64 @@ +#include +#include +#include "types.h" +#include "mem_map.h" +#include "console.h" + +void NORETURN guruMeditation(u8 type, u32 *excStack, u32 *stackPointer) +{ + const char *typeStr[3] = {"Undefined instruction", "Prefetch abort", "Data abort"}; + u32 realPc, instSize = 4; + + + consoleInit(0, NULL); + + if(excStack[1] & 0x20) instSize = 2; // Processor was in Thumb mode? + if(type == 2) realPc = excStack[15] - (instSize * 2); // Data abort + else realPc = excStack[15] - instSize; // Other + + printf("\x1b[41m\x1b[0J\x1b[9CGuru Meditation Error!\n\n%s:\nCPSR: 0x%X\n", typeStr[type], excStack[1]); + printf("r0 = 0x%08X r8 = 0x%08X\n" + "r1 = 0x%08X r9 = 0x%08X\n" + "r2 = 0x%08X r10 = 0x%08X\n" + "r3 = 0x%08X r11 = 0x%08X\n" + "r4 = 0x%08X r12 = 0x%08X\n" + "r5 = 0x%08X sp = 0x%08X\n" + "r6 = 0x%08X lr = 0x%08X\n" + "r7 = 0x%08X pc = 0x%08X\n\n", + excStack[2], excStack[10], + excStack[3], excStack[11], + excStack[4], excStack[12], + excStack[5], excStack[13], + excStack[6], excStack[14], + excStack[7], (u32)stackPointer, + excStack[8], excStack[0], + excStack[9], realPc); + + // make sure we can actually dump the stack without triggering another fault. + if((stackPointer >= A9_RAM_BASE) && (stackPointer < A9_RAM_BASE + A9_RAM_SIZE)) + { + puts("Stack dump:"); + for(u32 i = 0; i < 15; i++) + { + printf("0x%08X: %08X %08X\n", (u32)stackPointer, stackPointer[0], stackPointer[1]); + stackPointer += 2; + } + } + + // avoid fs corruptions + unmount_fs(); + devs_close(); + + while(1); +} + +void NORETURN panic() +{ + printf("\x1b[41m\x1b[0J\x1b[9CGPANIC!!!\n"); + + unmount_fs(); + devs_close(); + + while(1); +} + diff --git a/source/arm9/dev.c b/source/arm9/dev.c index 6dcfd46..b4ded9b 100644 --- a/source/arm9/dev.c +++ b/source/arm9/dev.c @@ -126,6 +126,11 @@ { if(!dev_sd.initialized) return false; + + if((u32)buf & 3) panic(); + + if(size < 0x200) + panic(); return !sdmmc_sdcard_readsectors(offset >> 9, size >> 9, buf); } @@ -134,6 +139,11 @@ { if(!dev_sd.initialized) return false; + + if((u32)buf & 3) panic(); + + if(size < 0x200) + panic(); return !sdmmc_sdcard_writesectors(offset >> 9, size >> 9, buf); } @@ -170,6 +180,11 @@ { if(!dev_rnand.initialized) return false; + + if((u32)buf & 3) panic(); + + if(size < 0x200) + panic(); return !sdmmc_nand_readsectors(offset >> 9, size >> 9, buf); } @@ -178,6 +193,11 @@ { if(!dev_rnand.initialized) return false; + + if((u32)buf & 3) panic(); + + if(size < 0x200) + panic(); return !sdmmc_nand_writesectors(offset >> 9, size >> 9, buf); } @@ -300,6 +320,11 @@ { //printf("Decnand read: offset: 0x%X, size: 0x%X\n", (unsigned int)offset, (unsigned int)size); if(!dev_dnand.dev.initialized) return false; + + if((u32)buf & 3) panic(); + + if(size < 0x200) + panic(); AES_ctx *ctx; nand_partition_struct *partition = find_partition(offset, size); @@ -330,6 +355,11 @@ { if(!dev_dnand.dev.initialized) return false; + + if((u32)buf & 3) panic(); + + if(size < 0x200) + panic(); //return !sdmmc_nand_writesectors(offset >> 9, size >> 9, buf); printf("Decnand write not implemented!\n"); diff --git a/source/arm9/gurumeditation.c b/source/arm9/gurumeditation.c deleted file mode 100644 index 264839c..0000000 --- a/source/arm9/gurumeditation.c +++ /dev/null @@ -1,49 +0,0 @@ -#include -#include -#include "types.h" -#include "mem_map.h" -#include "console.h" - -void NORETURN guruMeditation(u8 type, u32 *excStack, u32 *stackPointer) -{ - const char *typeStr[3] = {"Undefined instruction", "Prefetch abort", "Data abort"}; - u32 realPc, instSize = 4; - - - consoleInit(0, NULL); - - if(excStack[1] & 0x20) instSize = 2; // Processor was in Thumb mode? - if(type == 2) realPc = excStack[15] - (instSize * 2); // Data abort - else realPc = excStack[15] - instSize; // Other - - printf("\x1b[41m\x1b[0J\x1b[9CGuru Meditation Error!\n\n%s:\nCPSR: 0x%X\n", typeStr[type], excStack[1]); - printf("r0 = 0x%08X r8 = 0x%08X\n" - "r1 = 0x%08X r9 = 0x%08X\n" - "r2 = 0x%08X r10 = 0x%08X\n" - "r3 = 0x%08X r11 = 0x%08X\n" - "r4 = 0x%08X r12 = 0x%08X\n" - "r5 = 0x%08X sp = 0x%08X\n" - "r6 = 0x%08X lr = 0x%08X\n" - "r7 = 0x%08X pc = 0x%08X\n\n", - excStack[2], excStack[10], - excStack[3], excStack[11], - excStack[4], excStack[12], - excStack[5], excStack[13], - excStack[6], excStack[14], - excStack[7], (u32)stackPointer, - excStack[8], excStack[0], - excStack[9], realPc); - - // make sure we can actually dump the stack without triggering another fault. - if((stackPointer >= A9_RAM_BASE) && (stackPointer < A9_RAM_BASE + A9_RAM_SIZE)) - { - puts("Stack dump:"); - for(u32 i = 0; i < 15; i++) - { - printf("0x%08X: %08X %08X\n", (u32)stackPointer, stackPointer[0], stackPointer[1]); - stackPointer += 2; - } - } - - while(1); -}