From 89dd7c776dc61dfc89bf10b91dffd0168cea5130 Mon Sep 17 00:00:00 2001 From: dbalatoni13 Date: Sun, 19 Jan 2025 19:31:03 +0100 Subject: [PATCH] Match OSReboot.c and match some of TRK --- config/GMPE01_00/splits.txt | 2 +- config/GMPE01_00/symbols.txt | 2 +- config/GMPE01_01/splits.txt | 2 +- configure.py | 6 +- include/dolphin/dvd.h | 1 + include/dolphin/os/OSBootInfo.h | 2 +- include/dolphin/os/OSPriv.h | 3 + src/TRK_MINNOW_DOLPHIN/dolphin_trk.c | 5 +- src/TRK_MINNOW_DOLPHIN/targsupp.s | 25 ++++++ src/dolphin/os/OSReboot.c | 117 ++++++++++++++++++--------- 10 files changed, 119 insertions(+), 46 deletions(-) create mode 100644 src/TRK_MINNOW_DOLPHIN/targsupp.s diff --git a/config/GMPE01_00/splits.txt b/config/GMPE01_00/splits.txt index fc3ba2b8..d7d8237a 100644 --- a/config/GMPE01_00/splits.txt +++ b/config/GMPE01_00/splits.txt @@ -1221,7 +1221,7 @@ TRK_MINNOW_DOLPHIN/targimpl.c: .data start:0x8013EA58 end:0x8013EA88 .bss start:0x801A9138 end:0x801A96B8 -TRK_MINNOW_DOLPHIN/targsupp.c: +TRK_MINNOW_DOLPHIN/targsupp.s: .text start:0x800F1CF0 end:0x800F1D10 TRK_MINNOW_DOLPHIN/__exception.s: diff --git a/config/GMPE01_00/symbols.txt b/config/GMPE01_00/symbols.txt index 1417c27e..e2763245 100644 --- a/config/GMPE01_00/symbols.txt +++ b/config/GMPE01_00/symbols.txt @@ -5719,7 +5719,7 @@ __OSLastInterrupt = .sbss:0x801D4308; // type:object size:0x2 scope:global data: __OSLastInterruptTime = .sbss:0x801D4310; // type:object size:0x8 scope:global data:4byte SaveStart = .sbss:0x801D4318; // type:object size:0x4 scope:local data:4byte SaveEnd = .sbss:0x801D431C; // type:object size:0x4 scope:local data:4byte -Prepared = .sbss:0x801D4320; // type:object size:0x8 scope:local data:4byte +Prepared = .sbss:0x801D4320; // type:object size:0x4 scope:local data:4byte ResetFunctionQueue = .sbss:0x801D4328; // type:object size:0x8 scope:local data:4byte ResetCallback = .sbss:0x801D4330; // type:object size:0x4 data:4byte Down = .sbss:0x801D4334; // type:object size:0x4 data:4byte diff --git a/config/GMPE01_01/splits.txt b/config/GMPE01_01/splits.txt index fc3ba2b8..d7d8237a 100644 --- a/config/GMPE01_01/splits.txt +++ b/config/GMPE01_01/splits.txt @@ -1221,7 +1221,7 @@ TRK_MINNOW_DOLPHIN/targimpl.c: .data start:0x8013EA58 end:0x8013EA88 .bss start:0x801A9138 end:0x801A96B8 -TRK_MINNOW_DOLPHIN/targsupp.c: +TRK_MINNOW_DOLPHIN/targsupp.s: .text start:0x800F1CF0 end:0x800F1D10 TRK_MINNOW_DOLPHIN/__exception.s: diff --git a/configure.py b/configure.py index 6b56c797..201ba131 100644 --- a/configure.py +++ b/configure.py @@ -509,7 +509,7 @@ config.libs = [ Object(MatchingFor("GMPE01_00", "GMPE01_01"), "dolphin/os/OSMessage.c"), Object(MatchingFor("GMPE01_00", "GMPE01_01"), "dolphin/os/OSMemory.c"), Object(MatchingFor("GMPE01_00", "GMPE01_01"), "dolphin/os/OSMutex.c"), - Object(NonMatching, "dolphin/os/OSReboot.c"), + Object(MatchingFor("GMPE01_00", "GMPE01_01"), "dolphin/os/OSReboot.c"), Object(MatchingFor("GMPE01_00", "GMPE01_01"), "dolphin/os/OSReset.c"), Object(MatchingFor("GMPE01_00", "GMPE01_01"), "dolphin/os/OSResetSW.c"), Object(MatchingFor("GMPE01_00", "GMPE01_01"), "dolphin/os/OSRtc.c"), @@ -749,9 +749,9 @@ config.libs = [ Object(MatchingFor("GMPE01_00", "GMPE01_01"), "TRK_MINNOW_DOLPHIN/flush_cache.c"), Object(NonMatching, "TRK_MINNOW_DOLPHIN/mem_TRK.c"), Object(NonMatching, "TRK_MINNOW_DOLPHIN/targimpl.c"), - Object(NonMatching, "TRK_MINNOW_DOLPHIN/targsupp.c"), + Object(MatchingFor("GMPE01_00", "GMPE01_01"), "TRK_MINNOW_DOLPHIN/targsupp.s"), Object(MatchingFor("GMPE01_00", "GMPE01_01"), "TRK_MINNOW_DOLPHIN/__exception.s"), - Object(NonMatching, "TRK_MINNOW_DOLPHIN/dolphin_trk.c"), + Object(MatchingFor("GMPE01_00", "GMPE01_01"), "TRK_MINNOW_DOLPHIN/dolphin_trk.c"), Object(MatchingFor("GMPE01_00", "GMPE01_01"), "TRK_MINNOW_DOLPHIN/mpc_7xx_603e.c"), Object(MatchingFor("GMPE01_00", "GMPE01_01"), "TRK_MINNOW_DOLPHIN/main_TRK.c"), Object(NonMatching, "TRK_MINNOW_DOLPHIN/dolphin_trk_glue.c"), diff --git a/include/dolphin/dvd.h b/include/dolphin/dvd.h index cc6e1fa9..d8149fa2 100644 --- a/include/dolphin/dvd.h +++ b/include/dolphin/dvd.h @@ -89,6 +89,7 @@ void DVDInit(); BOOL DVDClose(DVDFileInfo* f); BOOL DVDSetAutoFatalMessaging(BOOL); void DVDReset(); +int DVDSetAutoInvalidation(int autoInval); s32 DVDCancel(DVDCommandBlock* block); BOOL DVDOpen(char* fileName, DVDFileInfo* fileInfo); BOOL DVDFastOpen(s32 entrynum, DVDFileInfo* fileInfo); diff --git a/include/dolphin/os/OSBootInfo.h b/include/dolphin/os/OSBootInfo.h index 19d4dfb5..2ed59d35 100644 --- a/include/dolphin/os/OSBootInfo.h +++ b/include/dolphin/os/OSBootInfo.h @@ -45,6 +45,6 @@ typedef struct BI2Debug { #define OS_DVD_MAGIC_NINTENDO 0xC2339F3D #define OS_THREAD_STACK_MAGIC 0xDEADBABE -#define OS_BOOTROM_ADDR 0x81300000 +#define OS_BOOTROM_ADDR ((void*)0x81300000) #endif // _DOLPHIN_OSBOOTINFO diff --git a/include/dolphin/os/OSPriv.h b/include/dolphin/os/OSPriv.h index 11eeec59..6d24df24 100644 --- a/include/dolphin/os/OSPriv.h +++ b/include/dolphin/os/OSPriv.h @@ -40,6 +40,9 @@ __OSExceptionHandler __OSGetExceptionHandler(__OSException exception); OSTime __OSGetSystemTime(); OSTime __OSTimeToSystemTime(OSTime); +// OSReset.c +void __OSDoHotReset(s32); + // OSReboot void __OSReboot(u32 resetCode, u32 bootDol); diff --git a/src/TRK_MINNOW_DOLPHIN/dolphin_trk.c b/src/TRK_MINNOW_DOLPHIN/dolphin_trk.c index 31153cd6..b8ffd29d 100644 --- a/src/TRK_MINNOW_DOLPHIN/dolphin_trk.c +++ b/src/TRK_MINNOW_DOLPHIN/dolphin_trk.c @@ -10,6 +10,7 @@ #define EXCEPTIONMASK_ADDR 0x80000044 static u32 lc_base; +extern u32 _db_stack_addr; static u32 TRK_ISR_OFFSETS[15] = { PPC_SystemReset, PPC_MachineCheck, @@ -64,8 +65,8 @@ asm void InitMetroTRK() mtspr 0x3f2, r0 mtspr 0x3f5, r0 //Restore stack pointer - lis r1, 0x80426008@h - ori r1, r1, 0x80426008@l + lis r1, _db_stack_addr@h + ori r1, r1, _db_stack_addr@l mr r3, r5 bl InitMetroTRKCommTable //Initialize comm table /* diff --git a/src/TRK_MINNOW_DOLPHIN/targsupp.s b/src/TRK_MINNOW_DOLPHIN/targsupp.s new file mode 100644 index 00000000..670f691f --- /dev/null +++ b/src/TRK_MINNOW_DOLPHIN/targsupp.s @@ -0,0 +1,25 @@ +.include "macros.inc" +.file "targsupp.s" + +.text +.balign 4 + +.fn TRKAccessFile, global + twui r0, 0x0 + blr +.endfn TRKAccessFile + +.fn TRKOpenFile, global + twui r0, 0x0 + blr +.endfn TRKOpenFile + +.fn TRKCloseFile, global + twui r0, 0x0 + blr +.endfn TRKCloseFile + +.fn TRKPositionFile, global + twui r0, 0x0 + blr +.endfn TRKPositionFile diff --git a/src/dolphin/os/OSReboot.c b/src/dolphin/os/OSReboot.c index 381d39c4..5331e90a 100644 --- a/src/dolphin/os/OSReboot.c +++ b/src/dolphin/os/OSReboot.c @@ -3,6 +3,7 @@ #include #include #include +#include typedef struct ApploaderHeader { // total size: 0x20 @@ -13,18 +14,7 @@ typedef struct ApploaderHeader { u32 reserved2; // offset 0x1C, size 0x4 } ApploaderHeader; -struct { - // total size: 0x1C - int valid; // offset 0x0, size 0x4 - unsigned long restartCode; // offset 0x4, size 0x4 - unsigned long bootDol; // offset 0x8, size 0x4 - void *regionStart; // offset 0xC, size 0x4 - void *regionEnd; // offset 0x10, size 0x4 - int argsUseDefault; // offset 0x14, size 0x4 - void *argsAddr; // offset 0x18, size 0x4 -} __OSRebootParams; // size: 0x1C, address: 0x0 - -static ApploaderHeader Header; +static ApploaderHeader Header ATTRIBUTE_ALIGN(32); extern void *__OSSavedRegionStart; extern void *__OSSavedRegionEnd; @@ -34,22 +24,25 @@ static void *SaveEnd = NULL; volatile u8 DAT_800030e2 : 0x800030e2; -extern u32 BOOT_REGION_START AT_ADDRESS(0x812FDFF0); //(*(u32 *)0x812fdff0) -extern u32 BOOT_REGION_END AT_ADDRESS(0x812FDFEC); //(*(u32 *)0x812fdfec) +extern void *BOOT_REGION_START AT_ADDRESS(0x812FDFF0); //(*(u32 *)0x812fdff0) +extern void *BOOT_REGION_END AT_ADDRESS(0x812FDFEC); //(*(u32 *)0x812fdfec) extern u32 UNK_HOT_RESET1 AT_ADDRESS(0x817ffff8); extern u32 UNK_HOT_RESET2 AT_ADDRESS(0x817ffffc); -// extern u32 OS_RESET_CODE AT_ADDRESS(0x800030F0); -// extern u8 OS_REBOOT_BOOL AT_ADDRESS(0x800030E2); // unknown function, set to true by __OSReboot +extern u32 OS_RESET_CODE AT_ADDRESS(0x800030F0); +extern u8 OS_REBOOT_BOOL AT_ADDRESS(0x800030E2); // unknown function, set to true by __OSReboot +extern u32 UNK_817FFFF8 AT_ADDRESS(0x817FFFF8); +extern u32 UNK_817FFFFC AT_ADDRESS(0x817FFFFC); -extern u32 __OSIsGcam; - -static BOOL Prepared = FALSE; +static volatile BOOL Prepared = FALSE; extern void __DVDPrepareResetAsync(DVDCBCallback callback); extern BOOL DVDCheckDisk(void); extern BOOL DVDReadAbsAsyncForBS(DVDCommandBlock *block, void *addr, s32 length, s32 offset, DVDCBCallback callback); -void Run(register u32 addr) +#pragma dont_inline on +// peephole bug with mwcc +static asm void myFunc() { } +static void Run(register void (*addr)()) { OSDisableInterrupts(); ICFlashInvalidate(); @@ -62,33 +55,83 @@ void Run(register u32 addr) } // clang-format on } +#pragma dont_inline reset static void Callback() { Prepared = TRUE; } -void ReadApploader(OSTime time1) { } +inline void ReadApploader(DVDCommandBlock *dvdCmd, void *addr, u32 offset, u32 numBytes) +{ + /* Not sure if this inline is correct - might need to call other inlines */ + while (Prepared == FALSE) { } + DVDReadAbsAsyncForBS(dvdCmd, addr, numBytes, offset + 0x2440, NULL); + + while (TRUE) { + switch (dvdCmd->state) { + case 0: + break; + case 1: + default: + continue; + case -1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + case 8: + case 9: + case 10: + case 11: + __OSDoHotReset(UNK_817FFFFC); + continue; + } + break; + } +} void __OSReboot(u32 resetCode, u32 bootDol) { - // TODO -} + OSContext exceptionContext; + DVDCommandBlock dvdCmd; + DVDCommandBlock dvdCmd2; + u32 numBytes; + u32 offset; -void OSSetSaveRegion(void *start, void *end) -{ - SaveStart = start; - SaveEnd = end; -} + OSDisableInterrupts(); -void OSGetSaveRegion(void **start, void **end) -{ - *start = SaveStart; - *end = SaveEnd; -} + UNK_817FFFFC = 0; + UNK_817FFFF8 = 0; + OS_REBOOT_BOOL = TRUE; + BOOT_REGION_START = SaveStart; + BOOT_REGION_END = SaveEnd; + OSClearContext(&exceptionContext); + OSSetCurrentContext(&exceptionContext); + DVDInit(); + DVDSetAutoInvalidation(TRUE); -void OSGetSavedRegion(void **start, void **end) -{ - *start = __OSSavedRegionStart; - *end = __OSSavedRegionEnd; + __DVDPrepareResetAsync(Callback); + + if (!DVDCheckDisk()) { + __OSDoHotReset(UNK_817FFFFC); + } + + __OSMaskInterrupts(0xffffffe0); + __OSUnmaskInterrupts(0x400); + + OSEnableInterrupts(); + + offset = 0; + numBytes = 32; + ReadApploader(&dvdCmd, (void *)&Header, offset, numBytes); + + offset = Header.size + 0x20; + numBytes = OSRoundUp32B(Header.rebootSize); + ReadApploader(&dvdCmd2, OS_BOOTROM_ADDR, offset, numBytes); + + ICInvalidateRange(OS_BOOTROM_ADDR, numBytes); + Run(OS_BOOTROM_ADDR); }