diff --git a/include/utility/iplESMisc.h b/include/utility/iplESMisc.h index da532537..96ae5d19 100644 --- a/include/utility/iplESMisc.h +++ b/include/utility/iplESMisc.h @@ -50,7 +50,7 @@ namespace ipl { static BOOL IsLastTicketExpired(ESTitleId* titleId); static void DeleteExpiredFlagFile(); - static void DeleteUnauthorizedData(EGG::Heap* heap); + static s32 DeleteUnauthorizedData(EGG::Heap* heap); static u32 CheckTmdCountryCode(ESTmdView* tmdView); static u32 CheckTmdParentalControl(ESTmdView* tmdView); diff --git a/src/utility/iplESMisc.cpp b/src/utility/iplESMisc.cpp index 1a75a20a..0011ab12 100644 --- a/src/utility/iplESMisc.cpp +++ b/src/utility/iplESMisc.cpp @@ -884,172 +884,180 @@ namespace ipl { return ret; } - void ESMisc::DeleteUnauthorizedData(EGG::Heap* heap) { - u32 titleCount = 0; - ESTitleId* titleIds = NULL; - char path[88]; + static inline void verifySavedataZD(EGG::Heap* heap, ESTitleId titleId, NANDFileInfo* fileInfo) { + char path[NAND_MAX_PATH]; + s32 ret; + u8* saveData = NULL; + BOOL fileOpen = FALSE; + BOOL deleteSaveData = FALSE; + + sprintf(path, "/title/%08x/%08x/data/%s", (u32)((titleId & 0x00FFFFFFFFFFFFFFULL) >> 32), (u32)(titleId & 0x00FFFFFFFFFFFFFFULL), "zeldaTp.dat"); + if (!ESMisc::ChangeUid(titleId)) { + OSReport("%s::%s: ChangeUid failed\n", __FILE__, __FUNCTION__); + ESMisc::DeleteTitle(heap, titleId); + } else { + ret = NANDPrivateOpen(path, fileInfo, NAND_ACCESS_READ); + if (ret == NAND_RESULT_NOEXISTS) { + OSReport("%s::%s: Does not exist %s: %d\n", __FILE__, __FUNCTION__, path, ret); + } else if (ret != NAND_RESULT_OK) { + OSReport("%s::%s: Open save data file failed: %d\n", __FILE__, __FUNCTION__, ret); + } else { + fileOpen = TRUE; + saveData = (u8*)heap->alloc(0x4000, -DEFAULT_ALIGN); + if (saveData == NULL) { + OSReport("%s::%s: Alloc failed: %d\n", __FILE__, __FUNCTION__, -2); + } else { + memset(saveData, 0, 0x4000); + ret = NANDRead(fileInfo, saveData, 0x4000); + if (ret < 0) { + OSReport("%s::%s: Read file failed: %d\n", __FILE__, __FUNCTION__, ret); + goto cleanup; + } else if (ret != 0x4000) { + OSReport("%s::%s: File size is not correct: %d\n", __FILE__, __FUNCTION__, ret); + deleteSaveData = TRUE; + } else { + u32 offset = 8; + u32 j = 0; + BOOL valid = FALSE; + while (j < 3) { + u8* block = saveData + offset; + if (!checkForNullTermination((char*)block + 0x4e, 8) || + !checkForNullTermination((char*)block + 0x58, 8) || + !checkForNullTermination((char*)block + 0x72, 8) || + !checkForNullTermination((char*)block + 0x8e, 8) || + !checkForNullTermination((char*)block + 0x1b4, 0x11) || + !checkForNullTermination((char*)block + 0x1c5, 0x11)) { + goto verify_failed; + } + j++; + offset += 0xa94; + } + + offset = 0x2008; + j = 0; + while (j < 3) { + u8* block = saveData + offset; + if (!checkForNullTermination((char*)block + 0x4e, 8) || + !checkForNullTermination((char*)block + 0x58, 8) || + !checkForNullTermination((char*)block + 0x72, 8) || + !checkForNullTermination((char*)block + 0x8e, 8) || + !checkForNullTermination((char*)block + 0x1b4, 0x11) || + !checkForNullTermination((char*)block + 0x1c5, 0x11)) { + goto verify_failed; + } + j++; + offset += 0xa94; + } + valid = TRUE; + + verify_failed: + + if (!valid) { + OSReport("%s::%s: Verify failed for %016llx\n", __FILE__, __FUNCTION__, + titleId); + deleteSaveData = TRUE; + } + } + + NANDClose(fileInfo); + fileOpen = FALSE; + if (deleteSaveData) { + ESMisc::DeleteSavedata(titleId, heap); + } + } + } + } + + cleanup: + if (saveData != NULL) { + heap->free(saveData); + } + if (fileOpen) { + NANDClose(fileInfo); + } + ESMisc::ChangeUid(SYSMENU_TITLE_ID); + } + + static inline void DeleteTicketsForce(EGG::Heap* heap, ESTitleId titleId, u8* ticketViews, u32* ticketViewCount) { + ESTicketView* ticketViewList = NULL; + s32 ret; + memset(ticketViews, 0, OSRoundUp32B(sizeof(ESTicketView))); + ret = ES_GetTicketViews(titleId, NULL, ticketViewCount); + if (ret != ES_ERR_OK) { + OSReport("%s::%s: ES_GetTicketViews failed: %d for %016llx\n", __FILE__, __FUNCTION__, ret, titleId); + } else if (*ticketViewCount != 0) { + ticketViewList = (ESTicketView*)heap->alloc(*ticketViewCount * OSRoundUp32B(sizeof(ESTicketView)), -DEFAULT_ALIGN); + ret = ES_GetTicketViews(titleId, ticketViewList, ticketViewCount); + if (ret != ES_ERR_OK) { + OSReport("%s::%s: ES_GetTicketViews failed: %d for %016llx\n", __FILE__, __FUNCTION__, ret, titleId); + } else { + for (u32 j = 0; j < *ticketViewCount; j++) { + memcpy(ticketViews, (u8*)ticketViewList + j * sizeof(ESTicketView), sizeof(ESTicketView)); + ret = ES_DeleteTicket((ESTicketView*)ticketViews); + if (ret != ES_ERR_OK) { + OSReport("%s::%s: ES_DeleteTicket failed: %d for %016llx\n", __FILE__, __FUNCTION__, ret, + ((ESTicketView*)ticketViews)->ticketId); + } + } + } + } + + if (ticketViewList != NULL) { + heap->free(ticketViewList); + } + } + + static inline s32 InitSavedata(EGG::Heap* heap) { + u8 ticketViews[OSRoundUp32B(sizeof(ESTicketView))] ALIGN32; NANDFileInfo fileInfo ALIGN32; - u8 ticketViews[0xe0] ALIGN32; u32 ticketViewCount; - ESTicketView* ticketViewList; + ESTitleId* titleIds = NULL; + u32 titleCount = 0; s32 ret = ES_ListTitlesOnCard(NULL, &titleCount); - if (ret == ES_ERR_OK) { - titleIds = (ESTitleId*)heap->alloc(OSRoundUp32B(titleCount * sizeof(ESTitleId)), -DEFAULT_ALIGN); + if (ret != ES_ERR_OK) { + OSReport("%s::%s: Failed to ES_ListTitlesOnCard1: %d\n", __FILE__, __FUNCTION__, ret); + goto cleanup; + } + + titleIds = (ESTitleId*)heap->alloc(OSRoundUp32B(titleCount * sizeof(ESTitleId)), -DEFAULT_ALIGN); if (titleIds == NULL) { - OSReport("%s::%s: Unable to allocate\n", __FILE__, "InitSavedata"); - } else { - ret = ES_ListTitlesOnCard(titleIds, &titleCount); - if (ret == ES_ERR_OK) { + OSReport("%s::%s: Unable to allocate\n", __FILE__, __FUNCTION__); + goto cleanup; + } + + ret = ES_ListTitlesOnCard(titleIds, &titleCount); + if (ret != ES_ERR_OK) { + OSReport("%s::%s: Failed to ES_ListTitlesOnCard2: %d\n", __FILE__, __FUNCTION__, ret); + goto cleanup; + } - u32 titleIdOffset = 0; for (u32 i = 0; i < titleCount; i++) { - u32 titleIdHi = *(u32*)((u8*)titleIds + titleIdOffset); - u32 titleIdLo = *(u32*)((u8*)titleIds + titleIdOffset + 4); - - if ((titleIdLo & 0xffffff00) == 0x525a4400 && titleIdHi == 0x10000) { - u8* saveData = NULL; - BOOL fileOpen = FALSE; - BOOL deleteSaveData = FALSE; - - sprintf(path - 8, "/title/%08x/%08x/data/%s", titleIdHi & 0xffffff, titleIdLo, "zeldaTp.dat"); - if (!ChangeUid(((ESTitleId)titleIdHi << 32) | titleIdLo)) { - OSReport("%s::%s: ChangeUid failed\n", __FILE__, "verifySavedataZD"); - DeleteTitle(heap, ((ESTitleId)titleIdHi << 32) | titleIdLo); - } else { - ret = NANDPrivateOpen(path - 8, (NANDFileInfo*)((u8*)&fileInfo - 0x20), NAND_ACCESS_READ); - if (ret == NAND_RESULT_NOEXISTS) { - OSReport("%s::%s: Does not exist %s: %d\n", __FILE__, "verifySavedataZD", path - 8, ret); - } else if (ret != NAND_RESULT_OK) { - OSReport("%s::%s: Open save data file failed: %d\n", __FILE__, "verifySavedataZD", ret); - } else { - fileOpen = TRUE; - saveData = (u8*)heap->alloc(0x4000, -DEFAULT_ALIGN); - if (saveData == NULL) { - OSReport("%s::%s: Alloc failed: %d\n", __FILE__, "verifySavedataZD", -2); - } else { - memset(saveData, 0, 0x4000); - ret = NANDRead((NANDFileInfo*)((u8*)&fileInfo - 0x20), saveData, 0x4000); - if (ret < 0) { - OSReport("%s::%s: Read file failed: %d\n", __FILE__, "verifySavedataZD", ret); - } else if (ret != 0x4000) { - OSReport("%s::%s: File size is not correct: %d\n", __FILE__, "verifySavedataZD", ret); - deleteSaveData = TRUE; - } else { - u32 offset = 8; - u32 j = 0; - BOOL valid = FALSE; - while (j < 3) { - u8* block = saveData + offset; - if (!checkForNullTermination((char*)block + 0x4e, 8) || - !checkForNullTermination((char*)block + 0x58, 8) || - !checkForNullTermination((char*)block + 0x72, 8) || - !checkForNullTermination((char*)block + 0x8e, 8) || - !checkForNullTermination((char*)block + 0x1b4, 0x11) || - !checkForNullTermination((char*)block + 0x1c5, 0x11)) { - goto verify_failed; - } - j++; - offset += 0xa94; - } - - offset = 0x2008; - j = 0; - while (j < 3) { - u8* block = saveData + offset; - if (!checkForNullTermination((char*)block + 0x4e, 8) || - !checkForNullTermination((char*)block + 0x58, 8) || - !checkForNullTermination((char*)block + 0x72, 8) || - !checkForNullTermination((char*)block + 0x8e, 8) || - !checkForNullTermination((char*)block + 0x1b4, 0x11) || - !checkForNullTermination((char*)block + 0x1c5, 0x11)) { - goto verify_failed; - } - j++; - offset += 0xa94; - } - valid = TRUE; - - verify_failed: - - if (!valid) { - OSReport("%s::%s: Verify failed for %016llx\n", __FILE__, "verifySavedataZD", - ((ESTitleId)titleIdHi << 32) | titleIdLo); - deleteSaveData = TRUE; - } - } - - NANDClose((NANDFileInfo*)((u8*)&fileInfo - 0x20)); - fileOpen = FALSE; - if (deleteSaveData) { - DeleteSavedata(((ESTitleId)titleIdHi << 32) | titleIdLo, heap); - } - } - } - } - - if (saveData != NULL) { - heap->free(saveData); - } - if (fileOpen) { - NANDClose((NANDFileInfo*)((u8*)&fileInfo - 0x20)); - } - ChangeUid(SYSMENU_TITLE_ID); + if ((titleIds[i] & 0xFFFFFFFFFFFFFF00ULL) == 0x00010000525A4400ULL) { + verifySavedataZD(heap, titleIds[i], &fileInfo); continue; } - if ((((ESTitleId)titleIdHi << 32) | titleIdLo) == 0x0001000844495343ULL || - ((((ESTitleId)titleIdHi << 32) | titleIdLo) < 0x0001000844495343ULL && - ((((ESTitleId)titleIdHi << 32) | titleIdLo) == 0x000100014a4f4449ULL || - ((((ESTitleId)titleIdHi << 32) | titleIdLo) > 0x000100014a4f4449ULL && - (((ESTitleId)titleIdHi << 32) | titleIdLo) == 0x0001000148415858ULL))) || - ((((ESTitleId)titleIdHi << 32) | titleIdLo) > 0x0001000844495343ULL && - ((((ESTitleId)titleIdHi << 32) | titleIdLo) == 0x0001000844564458ULL || - ((((ESTitleId)titleIdHi << 32) | titleIdLo) > 0x0001000844564458ULL && - (((ESTitleId)titleIdHi << 32) | titleIdLo) == 0x000100084449534bULL)))) { - ESTitleId titleId = *(ESTitleId*)((u8*)titleIds + titleIdOffset); - ES_DeleteTitle(titleId); - - ticketViewList = NULL; - memset(ticketViews, 0, sizeof(ticketViews)); - ret = ES_GetTicketViews(titleId, NULL, &ticketViewCount); - if (ret != ES_ERR_OK) { - OSReport("%s::%s: ES_GetTicketViews failed: %d for %016llx\n", __FILE__, "DeleteTicketsForce", ret, titleId); - } else if (ticketViewCount != 0) { - ticketViewList = (ESTicketView*)heap->alloc(ticketViewCount * 0xe0, -DEFAULT_ALIGN); - ret = ES_GetTicketViews(titleId, ticketViewList, &ticketViewCount); - if (ret != ES_ERR_OK) { - OSReport("%s::%s: ES_GetTicketViews failed: %d for %016llx\n", __FILE__, "DeleteTicketsForce", ret, titleId); - } else { - for (u32 j = 0; j < ticketViewCount; j++) { - memcpy(ticketViews, (u8*)ticketViewList + j * sizeof(ESTicketView), 0xd8); - ret = ES_DeleteTicket((ESTicketView*)ticketViews); - if (ret != ES_ERR_OK) { - OSReport("%s::%s: ES_DeleteTicket failed: %d for %016llx\n", __FILE__, "DeleteTicketsForce", ret, - ((ESTicketView*)ticketViews)->ticketId); - } - } - } - } - - if (ticketViewList != NULL) { - heap->free(ticketViewList); - } - } - titleIdOffset += 8; - } - - } else { - OSReport("%s::%s: Failed to ES_ListTitlesOnCard2: %d\n", __FILE__, "InitSavedata", ret); + switch (titleIds[i]) { + case 0x0001000844495343ULL: + case 0x000100014A4F4449ULL: + case 0x0001000148415858ULL: + case 0x0001000844564458ULL: + case 0x000100084449534BULL: + ES_DeleteTitle(titleIds[i]); + DeleteTicketsForce(heap, titleIds[i], ticketViews, &ticketViewCount); + break; } } - } else { - OSReport("%s::%s: Failed to ES_ListTitlesOnCard1: %d\n", __FILE__, "InitSavedata", ret); - } + cleanup: if (titleIds != NULL) { heap->free(titleIds); } - return; + return ret; + } + + s32 ESMisc::DeleteUnauthorizedData(EGG::Heap* heap) { + return InitSavedata(heap); } BOOL checkForNullTermination(char* str, u32 len) { diff --git a/tools/decomp-assist/allhands-system-decompile.attempts.md b/tools/decomp-assist/allhands-system-decompile.attempts.md new file mode 100644 index 00000000..3c2cc88c --- /dev/null +++ b/tools/decomp-assist/allhands-system-decompile.attempts.md @@ -0,0 +1,129 @@ +# System remainder decompilation, 2026-10-02 + +Baseline: `449529d92405f8f7fda610660c7794914a81dfe6`. +Branch: `agent/bittle/allhands-system-decompile`. +Owned units: `src/utility/iplESMisc.cpp`, `src/scene/setting/AOSS.c`, +`src/scene/setting/ATERM.c`. Parent additionally approved the single ESMisc +return-type declaration change in `include/utility/iplESMisc.h`. + +The current user priority is complete source reconstruction first, exact code +matching second, linking last. This candidate is decompilation progress, not an +exact match or a linking change. No configuration, symbol, split, or link flag +was changed. AOSS and ATERM are unchanged. + +## Initial audit + +- Read AGENTS.md, CONTRIBUTING.md, local skill inventory, AOSS/ATERM attempt + histories, and ESMisc rounds 9/11. The sole local skill is unrelated TypeSafe + integration guidance. +- Configured and built only 43U with the approved Wibo wrapper. +- Before source work, pool_diff found ATERM 0/0 strings and AOSS 1/1 identical. + ESMisc first diverged at string 83; both contained 114 strings. +- ESMisc's 30 exact functions were preserved in earlier rounds, but a meaningful + helper reconstruction had been discarded solely because it was not exact. + Reconstructed it from the current C++ and target object, without importing an + old experimental source or altering linker behavior. + +## ESMisc reconstruction + +Restored three ordinary inline boundaries named by the target's diagnostic +literals: `verifySavedataZD`, `DeleteTicketsForce`, and `InitSavedata`. +`ESMisc::DeleteUnauthorizedData` returns the initializer status. + +Concrete correctness corrections: + +1. Use a real NAND_MAX_PATH buffer and a real caller-owned, 32-byte-aligned + NANDFileInfo. Removed the out-of-object `path - 8` and `fileInfo - 0x20` + accesses. The caller also owns the aligned, rounded ticket-view scratch. +2. Traverse `ESTitleId` values by typed array index, with one unconditional loop + increment for every title. The old verifier branch continued without + advancing its separate byte offset and could repeat the same title. +3. Replace the handwritten nested 64-bit switch-lowering expression with the + five actual title cases and the independently masked save-title predicate. +4. Preserve the two ES_ListTitlesOnCard results independently of inlined file + and ticket operation status. The public declaration now returns s32. +5. A negative NANDRead result jumps to cleanup, which frees the save buffer + before closing the still-open file. The old source took the success-path + close before freeing it. Successful or invalid-size reads retain their + close-before-delete/free path. +6. Use sizeof(ESTicketView) for the 0xD8-byte copy and OSRoundUp32B for the 0xE0 + scratch/allocation extent, preserving the target's distinct sizes. + +### ABI and control-flow evidence + +Target function: address 0x813673E0, size 0x704 (449 instructions). +At original object offsets 0x1D20 and 0x1D9C, the two enumeration return values +are saved in r27. Calls through both verifier/ticket paths leave that saved +value independent. At 0x23C0 it is copied to return register r3 after the +optional title-list free. The prior void declaration discarded this ABI. + +The only declaration is in include/utility/iplESMisc.h. The only call is +src/system/iplSystem.cpp:680; it intentionally ignores the result. The symbol +name is unchanged because the return type is not encoded in this member name. +All 1027 compiled source objects were compared after a forced full rebuild; +iplSystem and every unit except iplESMisc were byte/relocation unchanged. + +The target's verifier branch joins the common increment at 0x238C (index +1, +byte offset +8). Its negative-read path joins cleanup at 0x2104, whereas its +successful close begins at 0x20E0. The reconstruction restores these distinct +paths instead of compensating with stack offsets or register tricks. + +### Measurements + +| Unit / measure | Before | After | +| --- | ---: | ---: | +| ESMisc exact functions | 30/31 | 30/31 | +| ESMisc exact code | 9404/11200 | 9404/11200 | +| ESMisc fuzzy | 96.76036% | 99.33143% | +| DeleteUnauthorizedData fuzzy | 79.797325% | 95.830734% | +| Function instructions | 434/449 | 446/449 | +| ESMisc matched data | 4416/4416 | 4416/4416 | +| ATERM exact / fuzzy / data | 17/26; 96.624664%; 18584/18864 | unchanged | +| AOSS exact / fuzzy / data | 16/21; 96.777916%; 3928/3928 | unchanged | +| Whole-report fuzzy | 99.588615% | 99.598236% | + +pool_diff now reports all 114 strings identical. The source .data is 4378 +bytes and exactly equals the original section's first 4378 bytes. The target +section is 4384 bytes and ends in six zero alignment bytes. No synthetic +padding was added. The objdiff data score remains 100%, and the literal audit +checks 204 arguments in all 30 exact functions with zero candidates or errors. + +The candidate remains non-exact. ctxdiff shows 446/449 instructions and +remaining register allocation, -1 mask materialization, and scratch-pointer +lifetime differences. No brute-force declaration/register search was run. + +## Validation + +Commands: + +- python3 configure.py --version 43U --wrapper ../toolchain/wibo-build/wibo +- Delete generated native `build/43U/src/**/*.o`, retaining a private baseline + object snapshot for comparison. +- WIBO_SJIS_MISSING_IMPORTS=1 ../.venv/bin/ninja all_source build/43U/report.json build/43U/ok +- PYTHONPATH=../local-tools ../.venv/bin/python tools/decomp-assist/pool_diff.py +- PYTHONPATH=../local-tools ../.venv/bin/python tools/decomp-assist/ctxdiff.py src/utility/iplESMisc DeleteUnauthorizedData__Q33ipl7utility6ESMiscFPQ23EGG4Heap +- PYTHONPATH=../local-tools ../.venv/bin/python tools/decomp-assist/literal_reference_diff.py src/utility/iplESMisc +- Compare every unit in the fresh report and all 1027 source objects' allocated + bytes and normalized relocations against the baseline. +- git diff --check + +Full rebuild passed (1031 Ninja steps). DOL SHA1: +`26116613f624061ba99c8d1a299aaa6efa85670d`. + +Only the ESMisc report changed: its fuzzy and one function's fuzzy improved. +No report metric regressed. Only ESMisc's allocated .text and .data changed: +.text 11360 -> 11408 bytes (390 relocations on both sides), .data 4378 -> 4378 +bytes (zero relocations). Every other allocated section/object is unchanged. + +A temporary host harness extracted the actual reconstructed helper bodies, +substituted fake Wii/heap calls, and exercised 12 control-flow scenarios: +first and second enumeration errors, title-list allocation failure, mixed +Zelda/blocked/ordinary title traversal, negative read, short read, second-save +verification failure, absent file, save-buffer allocation failure, ChangeUid +failure, ticket enumeration failure, and zero-ticket handling. Tests ran with +AddressSanitizer and UndefinedBehaviorSanitizer. LeakSanitizer is unavailable +under the execution environment's ptrace, so only that component was disabled. +The harness is supplemental behavioral validation, not a retail runtime test. + +GATE: focused decompilation improvement; full 43U/DOL and no-regression checks +pass; exact-matching gate remains incomplete at 30/31. No linking claimed.