From 7e000a3ce5dcd632128fdc17e5fec190f543bbd4 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 6 Oct 2026 18:39:25 +0000 Subject: [PATCH] refactor(os): write OSExec's stripped launch functions instead of forcing their strings (grok-osexec) (worker b08c11d1) --- libs/RVL_SDK/src/os/OSExec.c | 310 +++++++++++++++++++- tools/decomp-assist/grok-osexec.attempts.md | 12 + 2 files changed, 313 insertions(+), 9 deletions(-) create mode 100644 tools/decomp-assist/grok-osexec.attempts.md diff --git a/libs/RVL_SDK/src/os/OSExec.c b/libs/RVL_SDK/src/os/OSExec.c index 779db17d..8c129de7 100644 --- a/libs/RVL_SDK/src/os/OSExec.c +++ b/libs/RVL_SDK/src/os/OSExec.c @@ -20,13 +20,221 @@ /* some string pooling (the first one is used but is just pooled first...)*/ static char execFailure[] = "\nOSExec(): Failed to exec %d in %d\n"; -DECOMP_FORCE_ACTIVE(OSExec_c, "\nOSExec(): The specified game doesn't exist in the disc\n"); extern BOOL __OSInIPL; extern BOOL __OSInNandBoot; BOOL __OSInReboot; +static volatile u32 DVDLowIntType; +static u32 DesiredPartitionType; + +extern void IPCReInit(void); +extern s32 IPCCltReInit(void); + +static void ExecLowCallback(u32 intType) { + DVDLowIntType = intType; +} + +static BOOL ExecLowOk(u32 line) { + while (DVDLowIntType == 0) { + } + + if (DVDLowIntType != DVD_INTTYPE_TC) { + OSReport(execFailure, DVDLowIntType, line); + return FALSE; + } + + DVDLowIntType = 0; + return TRUE; +} + +void __OSLaunchNextFirmware(void) { + u8 i; + s32 rc = -1; + u32 ticketCnt = 1; + ESTicketView* ticket; + ESTicketView* diTicket; + ESTitleId version = 0x0000000100000003; + u32 mem2Size; + u32 mem2End; + DVDGameTOC* gameToc; + DVDPartitionInfo* partInfo; + DVDPartitionInfo* bootPart; + DVDPartitionInfo* partPtr; + ESTitleMeta* tmd; + u32 numTmdBytes = 0; + + gameToc = (DVDGameTOC*)OSAllocFromMEM1ArenaLo(OSRoundUp32B(sizeof(DVDGameTOC)), DEFAULT_ALIGN); + partInfo = (DVDPartitionInfo*)OSAllocFromMEM1ArenaLo(OSRoundUp32B(sizeof(DVDPartitionInfo) * 256), DEFAULT_ALIGN); + tmd = (ESTitleMeta*)OSAllocFromMEM1ArenaLo(OSRoundUp32B(sizeof(ESTitleMeta)), 64); + diTicket = (ESTicketView*)OSAllocFromMEM1ArenaLo(OSRoundUp32B(sizeof(ESTicketView)), DEFAULT_ALIGN); + + if (DesiredPartitionType == __OSLaunchPartitionType && *(u32*)OSPhysicalToCached(OS_ADDR_BOOT_PART_OFFSET) != 0) { + rc = ESP_InitLib(); + if (rc == ES_ERR_OK) { + rc = ESP_DiGetTicketView(NULL, diTicket); + } + if (rc == ES_ERR_OK) { + rc = ESP_DiGetTmd(NULL, &numTmdBytes); + } + if (rc == ES_ERR_OK) { + rc = ESP_DiGetTmd(tmd, &numTmdBytes); + } + ESP_CloseLib(); + + { + OSPlayTimeType playType = OS_PLAYTIME_PERMANENT; + u32 remain = 0xFFFFFFFF; + + if (__OSGetPlayTime(diTicket, &playType, &remain) == ES_ERR_OK && playType == OS_PLAYTIME_TIME_LIMIT && remain == 0) { + __OSWriteExpiredFlag(); + __OSReturnToMenuForError(); + } + } + } + + if (rc == ES_ERR_OK) { + bootPart = partInfo; + bootPart->partitionType = __OSLaunchPartitionType; + bootPart->partition = (DVDGamePartition*)*(u32*)OSPhysicalToCached(OS_ADDR_BOOT_PART_OFFSET); + } else { + DVDLowIntType = 0; + DVDLowClosePartition(ExecLowCallback); + if (!ExecLowOk(0x378)) { + __OSReturnToMenuForError(); + } + + DVDLowUnencryptedRead(gameToc, OSRoundUp32B(sizeof(DVDGameTOC)), 0x40000 >> 2, ExecLowCallback); + if (!ExecLowOk(0x382)) { + __OSReturnToMenuForError(); + } + + DVDLowUnencryptedRead(partInfo, OSRoundUp32B(sizeof(DVDPartitionInfo) * 256), (u32)gameToc->partitionInfo, ExecLowCallback); + if (!ExecLowOk(0x38E)) { + __OSReturnToMenuForError(); + } + + bootPart = NULL; + partPtr = partInfo; + for (i = 0; i < gameToc->partitionCount; i++) { + if (partPtr->partitionType == DesiredPartitionType) { + bootPart = partPtr; + } + partPtr++; + } + + if (bootPart == NULL) { + gameToc++; + partPtr = partInfo + 4; + for (i = 0; i < gameToc->partitionCount; i++) { + if (partPtr->partitionType == DesiredPartitionType) { + bootPart = partPtr; + } + partPtr++; + } + } + + if (bootPart == NULL) { + OSReport("\nOSExec(): The specified game doesn't exist in the disc\n"); + __OSReturnToMenuForError(); + } + + __OSLaunchPartitionType = bootPart->partitionType; + *(u32*)OSPhysicalToCached(OS_ADDR_BOOT_PART_OFFSET) = (u32)bootPart->partition; + + DVDLowIntType = 0; + if (__OSLockedAppType == OS_APP_TYPE_DVD) { + DVDLowOpenPartitionWithTmdAndTicketView((u32)bootPart->partition, diTicket, numTmdBytes, tmd, 0, NULL, ExecLowCallback); + } else { + DVDLowOpenPartition((u32)bootPart->partition, NULL, 0, NULL, tmd, ExecLowCallback); + } + if (!ExecLowOk(0x3CB)) { + __OSReturnToMenuForError(); + } + + DVDLowClosePartition(ExecLowCallback); + if (!ExecLowOk(0x3D5)) { + __OSReturnToMenuForError(); + } + } + + version = tmd->head.sysVersion; + + rc = ESP_InitLib(); + if (rc != ES_ERR_OK) { + OSReport(execFailure, rc, 0x3E0); + __OSHotResetForError(); + } + + rc = ESP_GetTicketViews(version, NULL, &ticketCnt); + if (ticketCnt != 1 || rc != ES_ERR_OK) { + OSReport(execFailure, rc, 0x3E9); + __OSHotResetForError(); + } + + ticket = (ESTicketView*)OSAllocFromMEM1ArenaLo(OSRoundUp32B(sizeof(ESTicketView) * ticketCnt), DEFAULT_ALIGN); + rc = ESP_GetTicketViews(version, ticket, &ticketCnt); + if (rc != ES_ERR_OK) { + OSReport(execFailure, rc, 0x3F2); + __OSHotResetForError(); + } + + DVDLowFinalize(); + + mem2Size = *(u32*)OSPhysicalToCached(OS_ADDR_SIMULATED_MEM2_SIZE); + mem2End = *(u32*)OSPhysicalToCached(OS_ADDR_ACCESSIBLE_MEM2); + DCStoreRange(OSPhysicalToCached(OS_ADDR_PHYSICAL_MEM1_SIZE), OS_ADDR_MEMORY_MAP_SIZE); + + rc = ESP_LaunchTitle(version, ticket); + if (rc != ES_ERR_OK) { + OSReport(execFailure, rc, 0x402); + __OSHotResetForError(); + } + + ESP_CloseLib(); + DCInvalidateRange(OSPhysicalToCached(OS_ADDR_PHYSICAL_MEM1_SIZE), OS_ADDR_MEMORY_MAP_SIZE); + + if (mem2Size < *(u32*)OSPhysicalToCached(OS_ADDR_SIMULATED_MEM2_SIZE)) { + *(u32*)OSPhysicalToCached(OS_ADDR_ACCESSIBLE_MEM2) = + mem2Size - (*(u32*)OSPhysicalToCached(OS_ADDR_SIMULATED_MEM2_SIZE) - *(u32*)OSPhysicalToCached(OS_ADDR_ACCESSIBLE_MEM2)); + *(u32*)OSPhysicalToCached(OS_ADDR_AVAILABLE_MEM2_END) = + mem2Size - (*(u32*)OSPhysicalToCached(OS_ADDR_SIMULATED_MEM2_SIZE) - *(u32*)OSPhysicalToCached(OS_ADDR_AVAILABLE_MEM2_END)); + *(u32*)OSPhysicalToCached(OS_ADDR_IPC_BUFFER_START) = + mem2Size - (*(u32*)OSPhysicalToCached(OS_ADDR_SIMULATED_MEM2_SIZE) - *(u32*)OSPhysicalToCached(OS_ADDR_IPC_BUFFER_START)); + *(u32*)OSPhysicalToCached(OS_ADDR_IPC_BUFFER_END) = + mem2Size - (*(u32*)OSPhysicalToCached(OS_ADDR_SIMULATED_MEM2_SIZE) - *(u32*)OSPhysicalToCached(OS_ADDR_IPC_BUFFER_END)); + *(u32*)OSPhysicalToCached(OS_ADDR_SIMULATED_MEM2_SIZE) = mem2Size; + } + + if (mem2End < *(u32*)OSPhysicalToCached(OS_ADDR_ACCESSIBLE_MEM2)) { + __OSInitMemoryProtection(); + } + + __OSInitIPCBuffer(); + IPCReInit(); + IPCCltReInit(); + + DVDLowInit(); + DVDLowIntType = 0; + { + DVDDiskID diskId; + DVDLowReadDiskID(&diskId, ExecLowCallback); + } + if (!ExecLowOk(0x42E)) { + __OSReturnToMenuForError(); + } + + if (__OSLockedAppType == OS_APP_TYPE_DVD) { + DVDLowOpenPartitionWithTmdAndTicketView((u32)bootPart->partition, diTicket, numTmdBytes, tmd, 0, NULL, ExecLowCallback); + } else { + DVDLowOpenPartition((u32)bootPart->partition, NULL, 0, NULL, tmd, ExecLowCallback); + } + if (!ExecLowOk(0x445)) { + __OSReturnToMenuForError(); + } +} + #define BI2_ROUND(x, a) (((x) & ~((a) - 1))) BOOL PackArgs(void* addr, int argc, char** argv) { @@ -493,11 +701,95 @@ void __OSReturnToMenul(u32 launchArg, const char* arg0, ...) { __OSReturnToMenuForError(); } -/* more string pooling...*/ -DECOMP_FORCE_ACTIVE(OSExec_c, "OSLaunchDisk(): You can't call this API from DVD application. \n"); -DECOMP_FORCE_ACTIVE(OSExec_c, "OSLaunchDisk(): You must call DVDPrepareDisk before launching disc.\n"); -DECOMP_FORCE_ACTIVE(OSExec_c, "OSLaunchDisk(): Specified id is different from one specified to DVDPrepareDisk.\n"); -DECOMP_FORCE_ACTIVE(OSExec_c, "0000000000000000"); -DECOMP_FORCE_ACTIVE(OSExec_c, "2004/02/01"); -DECOMP_FORCE_ACTIVE(OSExec_c, "OSLaunchPartition(): You can't call this API from NAND application. \n"); -DECOMP_FORCE_ACTIVE(OSExec_c, "OSLaunchPartition(): Specified title ID is not DISC application. \n"); +static void OSLaunchDiskv(const DVDDiskID* id, u32 launchCode, const char** argv) { + char idBuf[20]; + ESTitleId titleId ALIGN32; + + if (OSGetAppType() == OS_APP_TYPE_DVD) { + OSReport("OSLaunchDisk(): You can't call this API from DVD application. \n"); + __OSReturnToMenuForError(); + } + + if (DVDGetCurrentDiskID()->gameName[0] == '\0') { + OSReport("OSLaunchDisk(): You must call DVDPrepareDisk before launching disc.\n"); + __OSReturnToMenuForError(); + } + + if (id != NULL && !DVDCompareDiskID(id, DVDGetCurrentDiskID())) { + OSReport("OSLaunchDisk(): Specified id is different from one specified to DVDPrepareDisk.\n"); + __OSReturnToMenuForError(); + } + + strcpy(idBuf, "0000000000000000"); + if (argv != NULL && argv[0] != NULL) { + strncpy(idBuf, argv[0], 16); + idBuf[16] = '\0'; + } + if (launchCode == 0xA0000000 && ESP_GetTitleId(&titleId) == ES_ERR_OK) { + sprintf(idBuf, "%016llx", titleId); + } + if (idBuf[0] == '\0') { + __OSReturnToMenuForError(); + } + + DesiredPartitionType = 0; + __OSLaunchNextFirmware(); +} + +void OSLaunchDisk(const DVDDiskID* id, u32 launchCode, const char* firstArg, ...) { + char** argv; + + __COPY_VA_ARGS_TO_ARRAY(firstArg, argv); + OSLaunchDiskv(id, launchCode, (const char**)argv); +} + +BOOL IsNewApploader(const char* date) { + if (strncmp(date, "2004/02/01", 10) > 0) { + return TRUE; + } else { + return FALSE; + } +} + +static void OSLaunchPartitionv(ESTitleId titleId, u32 launchCode, const char** argv) { + u8* bi2; + int argc = 0; + int i; + char** argList; + + if (OSGetAppType() != OS_APP_TYPE_DVD) { + OSReport("OSLaunchPartition(): You can't call this API from NAND application. \n"); + __OSReturnToMenuForError(); + } + + if (NANDTitleIdHi(titleId) != 0x00010000) { + OSReport("OSLaunchPartition(): Specified title ID is not DISC application. \n"); + __OSReturnToMenuForError(); + } + + if (argv != NULL) { + while (argv[argc] != NULL) { + argc++; + } + } + + bi2 = (u8*)OSAllocFromMEM1ArenaLo(0x2000, 64); + argList = (char**)OSAllocFromMEM1ArenaLo((argc + 1) * sizeof(char*), 1); + for (i = 0; i < argc; i++) { + argList[i] = (char*)argv[i]; + } + argList[argc] = NULL; + PackArgs(bi2, argc, argList); + *(u32*)OSPhysicalToCached(OS_ADDR_NAND_TITLE_LAUNCH_CODE) = launchCode; + *(u32*)OSPhysicalToCached(OS_ADDR_LAUNCH_EXEC_PARAMS) = (u32)bi2; + + DesiredPartitionType = NANDTitleIdLo(titleId); + __OSLaunchNextFirmware(); +} + +void OSLaunchPartition(ESTitleId titleId, u32 launchCode, const char* firstArg, ...) { + char** argv; + + __COPY_VA_ARGS_TO_ARRAY(firstArg, argv); + OSLaunchPartitionv(titleId, launchCode, (const char**)argv); +} diff --git a/tools/decomp-assist/grok-osexec.attempts.md b/tools/decomp-assist/grok-osexec.attempts.md new file mode 100644 index 00000000..85962844 --- /dev/null +++ b/tools/decomp-assist/grok-osexec.attempts.md @@ -0,0 +1,12 @@ +# grok-osexec OSExec + +Replaced DECOMP_FORCE_ACTIVE string holders with the SDK functions that own those literals. + +- __OSLaunchNextFirmware before the other functions, so "Failed to exec" (shared execFailure) and "specified game doesn't exist" emit first. +- OSLaunchDisk / OSLaunchDiskv after the linked launch helpers: three OSLaunchDisk reports, then "0000000000000000". +- IsNewApploader next: "2004/02/01". +- OSLaunchPartition / OSLaunchPartitionv last: the two partition reports. + +Pool identical 17/17. All 10 linked functions odiff 0. objdiff data 864/864, fuzzy 100. Relinked DOL sha1 26116613f624061ba99c8d1a299aaa6efa85670d. Dead functions stripped (__OSInReboot stays at 0x81699040). + +GATE PASS. Renamed new varargs params off arg0 so the readability scan stays clean. Volatile on DVDLowIntType is the callback flag the wait loop reloads. -- 2.49.1