Compare commits

...

2 Commits

Author SHA1 Message Date
Devin AI
14b921b3d8 iplESMisc: DeleteUnauthorizedData check/return the call result (was uninit result + dead ret)
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-10-02 22:14:02 +00:00
Devin AI
ac32139715 Iterate iplESMisc DeleteUnauthorizedData + iplCardSequence loadCardFileIcons
- DeleteUnauthorizedData: s32 return, goto-dispatch blacklist, ret/result
  split, pTicketView local, u64 masked Zelda predicate (79.8 -> 89.6%)
- Remaining diffs documented as wall families (const remat, beq/b block
  layout, sched/web ties) in tools/decomp-assist/iplESMisc.attempts.md

Co-Authored-By: wch <usernamecole7@gmail.com>
2026-10-02 21:49:23 +00:00
4 changed files with 107 additions and 59 deletions

View File

@@ -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);

View File

@@ -850,14 +850,13 @@ iconSpeedDone:
} else if (totalImageSize > 0) {
result = CARDRead(&local.fileInfo, sThread->imageReadBuffer,
transferSize, iconAddressBase);
if (result < 0) {
goto closeFile;
if (result >= 0) {
memcpy(sThread->images[slot][fileNo],
(u8*)sThread->imageReadBuffer + iconAddressOffset,
totalImageSize);
DCStoreRange(sThread->images[slot][fileNo], transferSize);
result = 0;
}
memcpy(sThread->images[slot][fileNo],
(u8*)sThread->imageReadBuffer + iconAddressOffset,
totalImageSize);
DCStoreRange(sThread->images[slot][fileNo], transferSize);
result = 0;
}
}

View File

@@ -884,7 +884,7 @@ namespace ipl {
return ret;
}
void ESMisc::DeleteUnauthorizedData(EGG::Heap* heap) {
s32 ESMisc::DeleteUnauthorizedData(EGG::Heap* heap) {
u32 titleCount = 0;
ESTitleId* titleIds = NULL;
char path[88];
@@ -892,36 +892,40 @@ namespace ipl {
u8 ticketViews[0xe0] ALIGN32;
u32 ticketViewCount;
ESTicketView* ticketViewList;
s32 ret = ES_ListTitlesOnCard(NULL, &titleCount);
s32 result = ES_ListTitlesOnCard(NULL, &titleCount);
if (ret == ES_ERR_OK) {
if (result != ES_ERR_OK) {
OSReport("%s::%s: Failed to ES_ListTitlesOnCard1: %d\n", __FILE__, "InitSavedata", result);
} else {
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) {
result = ES_ListTitlesOnCard(titleIds, &titleCount);
if (result != ES_ERR_OK) {
OSReport("%s::%s: Failed to ES_ListTitlesOnCard2: %d\n", __FILE__, "InitSavedata", result);
} else {
u32 titleIdOffset = 0;
for (u32 i = 0; i < titleCount; i++) {
u32 titleIdHi = *(u32*)((u8*)titleIds + titleIdOffset);
u32 titleIdLo = *(u32*)((u8*)titleIds + titleIdOffset + 4);
ESTitleId titleId = titleIds[i];
u32 titleIdHi = (u32)(titleId >> 32);
u32 titleIdLo = (u32)titleId;
if ((titleIdLo & 0xffffff00) == 0x525a4400 && titleIdHi == 0x10000) {
if (((((ESTitleId)titleIdHi << 32) | titleIdLo) & ~0xFFULL) == 0x00010000525a4400ULL) {
u8* saveData = NULL;
BOOL fileOpen = FALSE;
BOOL deleteSaveData = FALSE;
sprintf(path - 8, "/title/%08x/%08x/data/%s", titleIdHi & 0xffffff, titleIdLo, "zeldaTp.dat");
sprintf(path, "/title/%08x/%08x/data/%s", (u32)((titleId >> 32) & 0xffffffULL), (u32)(titleId & 0xffffffffULL), "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);
result = NANDPrivateOpen(path, &fileInfo, NAND_ACCESS_READ);
if (result == NAND_RESULT_NOEXISTS) {
OSReport("%s::%s: Does not exist %s: %d\n", __FILE__, "verifySavedataZD", path, result);
} else if (result != NAND_RESULT_OK) {
OSReport("%s::%s: Open save data file failed: %d\n", __FILE__, "verifySavedataZD", result);
} else {
fileOpen = TRUE;
saveData = (u8*)heap->alloc(0x4000, -DEFAULT_ALIGN);
@@ -929,11 +933,11 @@ namespace ipl {
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);
result = NANDRead(&fileInfo, saveData, 0x4000);
if (result < 0) {
OSReport("%s::%s: Read file failed: %d\n", __FILE__, "verifySavedataZD", result);
} else if (result != 0x4000) {
OSReport("%s::%s: File size is not correct: %d\n", __FILE__, "verifySavedataZD", result);
deleteSaveData = TRUE;
} else {
u32 offset = 8;
@@ -979,7 +983,7 @@ namespace ipl {
}
}
NANDClose((NANDFileInfo*)((u8*)&fileInfo - 0x20));
NANDClose(&fileInfo);
fileOpen = FALSE;
if (deleteSaveData) {
DeleteSavedata(((ESTitleId)titleIdHi << 32) | titleIdLo, heap);
@@ -992,40 +996,54 @@ namespace ipl {
heap->free(saveData);
}
if (fileOpen) {
NANDClose((NANDFileInfo*)((u8*)&fileInfo - 0x20));
NANDClose(&fileInfo);
}
ChangeUid(SYSMENU_TITLE_ID);
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);
if (titleId == 0x0001000844495343ULL) {
goto delete_title;
}
if (titleId < 0x0001000844495343ULL) {
if (titleId == 0x000100014a4f4449ULL) {
goto delete_title;
}
if (titleId > 0x000100014a4f4449ULL && titleId == 0x0001000148415858ULL) {
goto delete_title;
}
goto next_title;
}
if (titleId == 0x0001000844564458ULL) {
goto delete_title;
}
if (titleId > 0x0001000844564458ULL && titleId == 0x000100084449534bULL) {
goto delete_title;
}
goto next_title;
delete_title:
{
ES_DeleteTitle(titleIds[i]);
ESTitleId titleId = titleIds[i];
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);
ESTicketView* pTicketView = (ESTicketView*)ticketViews;
memset(pTicketView, 0, sizeof(ticketViews));
result = ES_GetTicketViews(titleId, NULL, &ticketViewCount);
if (result != ES_ERR_OK) {
OSReport("%s::%s: ES_GetTicketViews failed: %d for %016llx\n", __FILE__, "DeleteTicketsForce", result, 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);
result = ES_GetTicketViews(titleId, ticketViewList, &ticketViewCount);
if (result != ES_ERR_OK) {
OSReport("%s::%s: ES_GetTicketViews failed: %d for %016llx\n", __FILE__, "DeleteTicketsForce", result, 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);
memcpy(pTicketView, (u8*)ticketViewList + j * sizeof(ESTicketView), 0xd8);
result = ES_DeleteTicket(pTicketView);
if (result != ES_ERR_OK) {
OSReport("%s::%s: ES_DeleteTicket failed: %d for %016llx\n", __FILE__, "DeleteTicketsForce", result,
pTicketView->ticketId);
}
}
}
@@ -1035,21 +1053,17 @@ namespace ipl {
heap->free(ticketViewList);
}
}
titleIdOffset += 8;
next_title:;
}
} else {
OSReport("%s::%s: Failed to ES_ListTitlesOnCard2: %d\n", __FILE__, "InitSavedata", ret);
}
}
} else {
OSReport("%s::%s: Failed to ES_ListTitlesOnCard1: %d\n", __FILE__, "InitSavedata", ret);
}
if (titleIds != NULL) {
heap->free(titleIds);
}
return;
return result;
}
BOOL checkForNullTermination(char* str, u32 len) {

View File

@@ -0,0 +1,35 @@
## w1008/card2 leaf — DeleteUnauthorizedData 79.8 → 89.6
Decodes that landed:
- Inverted head checks `if (ret != ES_ERR_OK) {report} else {work}` — orig inlines
error bodies with success branched over (was tail-merged in nested success-if form).
- `return ret` — fn returns s32 (orig mr r3,r27 before epilog); only the OUTER
ListTitlesOnCard results go to `ret`; inner call results use a separate local
(`result`) — kills the per-call `mr r24` homes.
- Zelda predicate is a u64 masked compare: `(titleId & ~0xFFULL) == 0x00010000525A4400ULL`
→ `and r4,r20,-1` (hi & 0xFFFFFFFF) + `and r0,r19,-0x100` + xor/xoris/or.
- sprintf args are u64-masked exprs `(u32)((titleId >> 32) & 0xffffff)` and
`(u32)(titleId & 0xffffffff)` → `and` with materialized masks, not clrlwi/mr.
- `ESTitleId titleId = titleIds[i]` u64 indexed load → lwzx+add+lwz4; ticket block
re-reads `titleIds[i]` after ES_DeleteTitle (forced reload across the call).
- `ESTicketView* pTicketView` named local for memset/memcpy/DeleteTicket → r29 web.
- Blacklist chain = goto-dispatch form (beq to shared delete block + goto next_title);
`||` chain folded wrong (bne-merged), BOOL flag form emits li/cmpwi — both wrong.
Remaining (documented walls): orig re-materializes each u64 constant per compare
(lis/addi at every == and ordering check); mine keeps them in volatile regs across
adjacent blocks — MWCC remat-vs-keep tie, ~10 insns. Plus beq+b vs bne tail dispatch
and mr r6,r3 placement scheduling.
## card2 wave (w1008) — DeleteUnauthorizedData residual decode
- `beq fwd; b` dispatch sites (DISK check `beq→del;b→next`, ticketViewCount `bne→alloc;b→next`):
decoded — orig's then-block is adjacent-forward but else is a real `b` — needs else-goto + non-fallthrough then.
Probes that all fold to single-branch: `if(c){goto}else{goto}` (bne→next), inverted polarity `if(!c)goto/else goto` (bne),
nested `if(>){if(==)goto}else goto`, `else if(c==0){goto}else{X}` — MWCC always prefers bne+fallthrough-del.
Confirmed A-vs-B block-placement tie (same wall as cardseq's `bge fwd;b` sites — MWCC places del-block
adjacent and folds else-goto). ~2 sites ≈ +2 insns orig.
- Per-compare u64 const remat: orig keeps r14=0x44490000 (DI-prefixed los) + r23=0x10000 (all his via `addi r0,r23,K`),
rematting `addi r0,r23,{1,8}` per compare (~15 extra insns vs mine keeping 0x10001/0x10008 in r4/r5 across blocks).
Keep-vs-remat wall — consts live in volatile regs stay live across non-call blocks; no source lever found.
- `mr r6,r27` vs `mr r6,r3` arg-reg timing shifts — sched tie.
- ret homing: `mr r27,r3` after EACH ListTitlesOnCard (both webs homed); mine keeps first web in r3.
Final residual: 1796B orig vs 1736B mine — all in documented wall families (remat, block-layout, sched, web-homing).