Compare commits

...

1 Commits

4 changed files with 443 additions and 254 deletions

View File

@@ -88,9 +88,7 @@ found. Historical logs absent from this branch have commit IDs and excerpts in
| `libs/RVL_SDK/src/base/PPCArch.c` | `PPCSetFpIEEEMode` | ORIGINAL | Special-register or synchronization operation: `mtfsb0 FPSCR_NI_BIT`. |
| `libs/RVL_SDK/src/base/PPCArch.c` | `PPCSetFpNonIEEEMode` | ORIGINAL | Special-register or synchronization operation: `mtfsb1 FPSCR_NI_BIT`. |
| `libs/RVL_SDK/src/base/PPCArch.c` | `PPCMthid4` | ORIGINAL | Both branches write HID4 with mtspr; the fallback preserves the Broadway H4A bit. |
| `libs/RVL_SDK/src/cntcache/cntcache.c` | `CNTCACHEClear` | PLACEHOLDER | NAND/token logic; best C 93.364235%, 147/151 instructions, four missing pool literals, [rx30][rx30]. |
| `libs/RVL_SDK/src/cntcache/cntcache.c` | `_CNTCACHEIsTitleRemovable` | PLACEHOLDER | NAND/TMD checks; best diagnostic C 93.70731%, 82/82 instructions, status-register interference, [rx18][rx18]. |
| `libs/RVL_SDK/src/cntcache/cntcache.c` | `_CNTCACHEDeleteContent` | PLACEHOLDER | Token parsing and ES deletion; best C 95.793106%, 143/145 instructions, missing reload/copy, [rx18][rx18]. |
| `libs/RVL_SDK/src/cntcache/cntcache.c` | `CNTCACHEClear` | PLACEHOLDER | NAND/token logic; best C 47/151 differing, 149/151 instructions; pool solved with stripped writer literals, two post-delete compares missing, [rx30][rx30], [opus-cntc](../tools/decomp-assist/opus-cntc.attempts.md). |
| `libs/RVL_SDK/src/db/db.c` | `__DBExceptionDestination` | ORIGINAL | mfmsr/mtmsr enable address translation before the exception-handler tail branch. |
| `libs/RVL_SDK/src/gx/GXLight.c` | `PushLight` | ORIGINAL | psq_l/psq_st transfers paired-single values for the GX command stream. |
| `libs/RVL_SDK/src/gx/GXTransform.c` | `WriteProjPS` | ORIGINAL | psq_l/psq_st transfers paired-single values for the GX command stream. |
@@ -198,7 +196,6 @@ found. Historical logs absent from this branch have commit IDs and excerpts in
| `src/system/rvl_dec.c` | `Rvl_decode_asr` | ORIGINAL | nofralloc hand-built 0x40 frame saves r21..31 with stmw; work address uses lis @h/ori @l. |
[rx17]: ../tools/decomp-assist/rx36.attempts.md#rx17
[rx18]: ../tools/decomp-assist/rx36.attempts.md#rx18
[rx24]: ../tools/decomp-assist/rx36.attempts.md#rx24
[rx30]: ../tools/decomp-assist/rx36.attempts.md#rx30
[rx36-osinit]: ../tools/decomp-assist/rx36.attempts.md#osinit

View File

@@ -31,8 +31,6 @@ char scCntCacheSpaceNl[3] = {0x20, 0x0a, 0x00};
extern void _savegpr_23();
extern void _restgpr_23();
extern void _savegpr_26();
extern void _restgpr_26();
OSMutex _CNTCACHEMutex;
long long _CNTCACHEUnused816997A8;
@@ -263,254 +261,102 @@ void _CNTCACHEDeleteTitle() {
}
}
asm int _CNTCACHEIsTitleRemovable(ESTitleId titleId) {
clrlwi r11, r1, 0x1b
mr r12, r1
subfic r11, r11, -0x2100
stwux r1, r1, r11
mflr r0
lis r5, scCntCacheTitleDataFmt@ha
stw r0, 4(r12)
li r0, 1
addi r5, r5, scCntCacheTitleDataFmt@l
stw r31, -4(r12)
stw r30, -8(r12)
mr r30, r3
addi r3, r1, 0x40
stw r29, -0xc(r12)
mr r29, r4
li r4, -1
stw r0, 0x20(r1)
and r6, r30, r4
and r7, r29, r4
li r4, 0x40
stw r0, 0x24(r1)
crclr 4*cr1+eq
bl snprintf
addi r3, r1, 0x40
addi r4, r1, 0x20
addi r5, r1, 0x24
bl NANDSecretGetUsage
cmpwi r3, -0xc
mr r4, r3
bne L_418
li r4, 2
b L_444
L_418:
cmpwi r3, 0
blt L_444
lwz r0, 0x24(r1)
cmplwi r0, 1
bne L_440
lwz r0, 0x20(r1)
cmpwi r0, 0
bne L_440
li r4, 1
b L_444
L_440:
li r4, 0
L_444:
cmpwi r4, 1
bne L_4bc
mr r4, r29
mr r3, r30
addi r31, r1, 0x80
addi r6, r1, 0x28
li r5, 0
bl ES_GetTmdView
cmpwi r3, 0
beq L_470
b L_4c0
L_470:
lwz r0, 0x28(r1)
cmplwi r0, 0x2060
ble L_484
li r3, -0x400
b L_4c0
L_484:
mr r4, r29
mr r3, r30
mr r5, r31
addi r6, r1, 0x28
bl ES_GetTmdView
cmpwi r3, 0
beq L_4a4
b L_4c0
L_4a4:
lhz r0, 0xd8(r1)
rlwinm. r0, r0, 0x18, 0x18, 0x1f
bne L_4b8
li r4, 1
b L_4bc
L_4b8:
li r4, 0
L_4bc:
mr r3, r4
L_4c0:
lwz r10, 0(r1)
lwz r0, 4(r10)
lwz r31, -4(r10)
lwz r30, -8(r10)
lwz r29, -0xc(r10)
mtlr r0
mr r1, r10
blr
static s32 GetSaveDataUsage(ESTitleId titleId) {
s32 ret = -1;
char nandPath[64] ALIGN32;
u32 usedBlocks = 1;
u32 usedINodes = 1;
snprintf(nandPath, (int)sizeof(nandPath), scCntCacheTitleDataFmt, NANDTitleIdHi(titleId), NANDTitleIdLo(titleId));
ret = NANDSecretGetUsage(nandPath, &usedBlocks, &usedINodes);
if (ret == NAND_RESULT_NOEXISTS) {
ret = 2;
} else if (ret >= NAND_RESULT_OK) {
if (usedINodes == 1 && usedBlocks == 0) {
ret = 1;
} else {
ret = 0;
}
}
return ret;
}
asm void _CNTCACHEDeleteContent() {
clrlwi r11, r1, 0x1b
mr r12, r1
subfic r11, r11, -0x2100
stwux r1, r1, r11
mflr r0
mr r11, r12
stw r0, 4(r12)
bl _savegpr_26
li r3, 0
la r4, scCntCacheSpaceNl
bl strtok
li r31, 0
li r4, 0
stw r31, errno(r0)
li r5, 0x10
bl strtoull
lwz r0, errno(r0)
mr r29, r4
mr r30, r3
cmpwi r0, 0
bne L_708
li r0, -1
and r5, r3, r0
addis r0, r5, -1
cmplwi r0, 5
bne L_708
addi r28, r1, 0x80
addi r6, r1, 0x28
li r5, 0
bl ES_GetTmdView
cmpwi r3, -0x6a
beq L_708
cmpwi r3, 0
bne L_708
mr r4, r29
mr r3, r30
mr r5, r28
addi r6, r1, 0x28
bl ES_GetTmdView
cmpwi r3, 0
bne L_708
lwz r3, 0x94(r1)
rlwinm. r0, r3, 0, 0x1c, 0x1c
beq L_6f0
rlwinm. r0, r3, 0, 0x1b, 0x1b
beq L_6f0
li r3, 0
la r4, scCntCacheSpaceNl
bl strtok
li r26, 1
lis r27, scCntCacheTitleDataFmt@ha
b L_6e8
L_5b0:
stw r31, errno(r0)
li r4, 0
li r5, 0xa
bl strtoul
lwz r0, errno(r0)
cmpwi r0, 0
bne L_6dc
cmplwi r3, 0x1fe
bgt L_6dc
lhz r0, 0xda(r1)
li r4, 0
mtctr r0
cmplwi r0, 0
ble L_6dc
L_5e8:
add r5, r28, r4
lhz r0, 0x60(r5)
cmplw r3, r0
bne L_6d4
lhz r0, 0x62(r5)
rlwinm. r0, r0, 0, 0x10, 0x10
bne L_6dc
lwz r5, 0x5c(r5)
mr r4, r29
mr r3, r30
bl ES_DeleteContent
cmpwi r3, 0
bne L_6dc
mr r4, r29
mr r3, r30
addi r6, r1, 0x2c
li r5, 0
bl ES_ListTitleContentsOnCard
cmpwi r3, 0
bne L_6dc
lwz r0, 0x2c(r1)
cmpwi r0, 0
bne L_6dc
li r0, -1
stw r26, 0x20(r1)
addi r3, r1, 0x40
addi r5, r27, scCntCacheTitleDataFmt@l
stw r26, 0x24(r1)
and r6, r30, r0
and r7, r29, r0
li r4, 0x40
crclr 4*cr1+eq
bl snprintf
addi r3, r1, 0x40
addi r4, r1, 0x20
addi r5, r1, 0x24
bl NANDSecretGetUsage
cmpwi r3, -0xc
mr r0, r3
bne L_690
li r0, 2
b L_6bc
L_690:
cmpwi r3, 0
blt L_6bc
lwz r0, 0x24(r1)
cmplwi r0, 1
bne L_6b8
lwz r0, 0x20(r1)
cmpwi r0, 0
bne L_6b8
li r0, 1
b L_6bc
L_6b8:
li r0, 0
L_6bc:
cmpwi r0, 1
bne L_6dc
mr r4, r29
mr r3, r30
bl ES_DeleteTitle
b L_6dc
L_6d4:
addi r4, r4, 0x10
bdnz L_5e8
L_6dc:
li r3, 0
la r4, scCntCacheSpaceNl
bl strtok
L_6e8:
cmpwi r3, 0
bne L_5b0
L_6f0:
lwz r5, 0x28(r1)
addi r3, r1, 0x80
li r4, 0
addi r0, r5, 0x1f
rlwinm r5, r0, 0, 0, 0x1a
bl memset
L_708:
lwz r10, 0(r1)
mr r11, r10
bl _restgpr_26
lwz r0, 4(r10)
mtlr r0
mr r1, r10
blr
BOOL _CNTCACHEIsTitleRemovable(ESTitleId titleId) {
u8 tmdBuf[OSRoundUp32B(sizeof(ESTmdView))] ALIGN32;
u32 tmdSize;
ESTmdView* tmd;
s32 ret;
ret = GetSaveDataUsage(titleId);
if (ret == 1) {
tmd = (ESTmdView*)tmdBuf;
ret = ES_GetTmdView(titleId, NULL, &tmdSize);
if (ret != ES_ERR_OK) {
return ret;
}
if (tmdSize > sizeof(tmdBuf)) {
return ES_ERR_MEMORY_ERROR;
}
ret = ES_GetTmdView(titleId, tmd, &tmdSize);
if (ret != ES_ERR_OK) {
return ret;
}
if (((tmd->head.titleVersion >> 8) & 0xFF) == 0) {
ret = 1;
} else {
ret = 0;
}
}
return ret;
}
void _CNTCACHEDeleteContent() {
u8 buf[OSRoundUp32B(sizeof(ESTmdView))] ALIGN32;
u32 numOnCard;
u32 size;
ESTitleId titleId;
ESTmdView* tmd;
ESError result;
char* token;
token = strtok(NULL, scCntCacheSpaceNl);
errno = 0;
titleId = strtoull(token, NULL, 16);
if (errno == 0 && NANDTitleIdHi(titleId) == 0x00010005) {
tmd = (ESTmdView*)buf;
result = ES_GetTmdView(titleId, NULL, &size);
if (result == ES_ERR_DONT_EXISTS) {
return;
}
if (result == ES_ERR_OK && ES_GetTmdView(titleId, tmd, &size) == ES_ERR_OK) {
if ((tmd->head.titleType & 8) && (tmd->head.titleType & 0x10)) {
token = strtok(NULL, scCntCacheSpaceNl);
while (token) {
u32 index;
u32 i;
errno = 0;
index = strtoul(token, NULL, 10);
if (errno == 0 && index <= 510) {
for (i = 0; i < tmd->head.numContents; i++) {
if (index == tmd->contents[i].index) {
if (!(tmd->contents[i].type & 0x8000) &&
ES_DeleteContent(titleId, tmd->contents[i].cid) == ES_ERR_OK &&
ES_ListTitleContentsOnCard(titleId, NULL, &numOnCard) == ES_ERR_OK &&
numOnCard == 0 && GetSaveDataUsage(titleId) == 1) {
ES_DeleteTitle(titleId);
}
break;
}
}
}
token = strtok(NULL, scCntCacheSpaceNl);
}
}
memset(buf, 0, OSRoundUp32B(size));
}
}
}

View File

@@ -0,0 +1,87 @@
# opus-cntc attempts (cntcache.c)
## _CNTCACHEDeleteContent (start: asm placeholder; prior best C 95.79%, 143/145)
1. Flat early-return C, typed `ESTmdView view` local: 80/145 differing, frame 0x2120.
2. `u8 buf[sizeof(ESTmdView)]` + `ESTmdView* tmd` pointer, size/count decl order swapped: 118 (frame fixed, pointer r28 appears).
3. Helper with multiple returns: 90 (worse branch shape in helper, `bge; b`).
4. Helper with `usage` var + if/else chain: 90.
5. `usage = result;` before chain: 118 (coalesced anyway).
6. Type check via `ES_TITLE_TYPE` (u32 cast and 64-bit): -1 still CSE'd into r31.
7. `(titleId & 0xFFFFFFFF00000000) != 0x0001000500000000`: 96, xor form.
8. Nested-if structure (single exit, memset inside): 90/145, 143/145 insns; only diffs are the
missing `li r0,-1` reload before snprintf (CSE/LICM keeps -1 in r31) and missing `mr r0,r3`
(helper result coalesced into r3); everything else is register renumbering from those.
9. Diagnostic: type check without mask -> li -1 hoisted to outer token-loop preheader (LICM).
10. 32-bit masks `(u32)(x>>32) & 0xFFFFFFFF`: masks fold away, 82 but wrong (no `and`).
11. Mask variants `(u32)(t>>32),(u32)(t&0xFFFFFFFF)` etc.: 89-90, -1 still hoisted.
12. Helper `static` (auto-inline) instead of `static inline`: 90, no change.
## _CNTCACHEIsTitleRemovable (start: asm placeholder; prior best C 93.71%)
Upstream koopthekoopa/wii-ipl has a non-matching C sketch (GetSavePath/GetTmdView helpers, `ret = -1`, goto out); read only.
1. Typed flat version with my usage helper: 69/82.
2. Upstream structure (GetSavePath + GetTmdView helpers, `s32 ret = -1`, goto out): 72.
3. GetTmdView takes `u32* tmdSize` from caller: frame offsets right, 72.
4. Usage code written directly in the caller (no GetSavePath): `mr r4,r3` copy appears in target position.
5. + `u8 tmdBuf[OSRoundUp32B(sizeof(ESTmdView))]` with `ESTmdView* tmd` pointer: 5/82.
6. `(u8)(titleVersion >> 8)`: no change (srawi.); `((v >> 8) & 0xFF)`: 4/82.
7. Back to GetSavePath helper with those fixes: 18 (copy coalesced on the wrong side).
8. `result`/`ret` split in helper: 18. `static inline`/`s32` helper: 18. Drop `= -1` init: 69.
## _CNTCACHEDeleteContent (cont.)
13. Usage code written directly (no helper): no copy, 90.
14. Function-wide `ret` reused for every status (typical SDK style): 94, no copy.
15. Shared `static s32 GetSaveDataUsage` helper written like the matched inline code
(`s32 ret = -1;`, separate `usedBlocks = 1; usedINodes = 1;` stores, if/else chain):
21/145. Swapping usedBlocks/usedINodes declaration order: 15. Declaring `titleId` before
`tmd`: **0/145 (exact)**. The dead `ret = -1` init is load-bearing (without it: 90).
16. IsTitleRemovable switched to the same helper with caller `s32 ret;` (no init): **0/82**.
(With caller `ret = -1` init: 16/82.)
## CNTCACHEClear (start: asm placeholder; prior best C 93.36%, 147/151)
1. Replaced the hex blob with real literals (SDKDefineVersion, path/command literals,
`" \n"`, `"/title/%08x/%08x/data"`). Clear in C with `FindLineEnd` static helper: 162/180;
`_CNTCACHEDeleteTitle` got auto-inlined into Clear (target calls it).
2. `(void)` prototypes, no prototypes (compile error), `-ipa function` / no ipa (no change, or
header static inlines emitted): DeleteTitle still inlined / unusable.
3. Diagnostic `#pragma inline_max_auto_size(k)`: DeleteTitle's auto-inline size is 15 and the
default limit is 15, so the original DeleteTitle must score >= 16.
while->for, nested ifs, result assignments: still 15. switch: breaks DeleteTitle.
4. DeleteTitle as a `for` loop with `if (errno != 0 || type == 1) continue;`: size 16, not
inlined, DeleteTitle still 0/50. Clear: 126/151, size 147/151.
5. Error exits: `goto close`, else-goto, switch, do/while(0)+break all give `bne close`; micro-tests
show only `return` gives the target `beq +8; b close`. Helper `ExecuteCacheFile(info, result)`
with returns: 151/151 (not inlined, auto size); `static inline`: 68/151, 149 insns, branch
layout matches.
6. `s32 readLength` + `> sizeof(buf)` clamp: cmpw/bgt/cmplwi as target, 67.
Swapping compare operands: no effect. `!lineEnd` / `if (command)`: immediate compares, 64.
7. Stripped writers `CNTCACHEAddDeleteTitle` / `CNTCACHEAddDeleteContent` (+ static Read/Write
helpers using /shared2/cntcache.txt, /tmp/cntcache.txt, NANDPrivateMove to /shared2):
.data and .sdata bytes identical to target, Clear string offsets match.
8. Brute-force local order: Clear's own decls have no effect; helper decls reversed
(lineLength, line, readLength, offset): 47/151 (helper r23..r26 match).
9. Lone `cmpwi r3,0` after each NANDPrivateDelete: empty if, `;`, `(void)(x == 0)`, empty inline
call, dead store, inline void/return helpers all remove it. Micro-test: kept only when
the body copies between two coalesced variables (`r = res; if (g()) { r = res; }`).
`opened = FALSE` in body: 49/155. `result = result;`: keeps only the second compare, 47/151 at 150 insns.
Not found; stopped here.
Result: CNTCACHEClear not exact (best 47/151, 149/151 insns). Best C plus required companion
changes in [opus-cntc.best.c](opus-cntc.best.c); the asm placeholder and original data arrays stay.
## Final state
- `_CNTCACHEIsTitleRemovable` 0/82, `_CNTCACHEDeleteContent` 0/145 (C, shared GetSaveDataUsage).
- `CNTCACHEInit` 0/29, `_CNTCACHEDeleteTitle` 0/50, `CNTCACHEClear` 0/151 (asm) unchanged.
- pool_diff: identical (9/9).
- Note: in DeleteContent, `result != ES_ERR_DONT_EXISTS && result == ES_ERR_OK` keeps the
target's separate -106 compare; this is redundant but not impossible.
## Round 2: _CNTCACHEDeleteContent without the redundant test
1. Replaced `result != ES_ERR_DONT_EXISTS && result == ES_ERR_OK && ...` with an early
`if (result == ES_ERR_DONT_EXISTS) { return; }` followed by `if (result == ES_ERR_OK && ...)`:
0/145, equal size. IsTitleRemovable still 0/82, pool identical (9/9).
check_asm_inventory: FAIL before and after (same 8 pre-existing placeholders incl. parked CNTCACHEClear).

View File

@@ -0,0 +1,259 @@
/*
* opus-cntc best C for CNTCACHEClear: 47/151 differing, 149/151 instructions
* (odiff libs/RVL_SDK/src/cntcache/cntcache CNTCACHEClear). Not exact; the retail
* assembly placeholder stays in cntcache.c.
*
* Needed alongside this text (all verified in the trial build):
* - Replace the scCntCacheStrs/scCntCacheTitleDataFmt/scCntCacheSpaceNl byte arrays with
* real literals: SDKDefineVersion(CNTCACHE, "Apr 20 2010", "14:15:28"), " \n",
* "/title/%08x/%08x/data". The writer functions below (stripped from the DOL) own
* "DeleteTitle ", "%016llx ", "/shared2" and "DeleteContent " and put them before
* Clear's literals; with them .data/.sdata bytes are identical to the target.
* - _CNTCACHEDeleteTitle must be the for/continue form below. The while form has auto-inline
* size 15 (= the -inline auto limit) and gets inlined into Clear; the target calls it.
* The for/continue form still matches DeleteTitle 0/50 and is not inlined.
* - ExecuteCacheFile must be `static inline` with `return`s: that is what produces the
* target's `beq +8; b close` exits (goto produces `bne close`).
* - Helper locals declared in reverse (lineLength, line, readLength, offset) give the
* target's r23..r26. Clear's own local order has no effect.
*
* Remaining: the target has a lone `cmpwi r3,0` after each NANDPrivateDelete. MWCC keeps
* such a compare when the if-body is a copy between two coalesced variables (micro-test:
* `r = res; if (g() != 0) { r = res; }`). An empty if-body or ignored return removes it, and
* `result = result;` keeps only the second one. The caller-side registers (base r30, opened
* r29, result r28, locked r27, zero r31) are still off, probably because of that missing copy.
*/
#define CNTCACHE_READ_SIZE 0x400
#define CNTCACHE_LINE_SIZE 0x40
#define CNTCACHE_RESULT_READ_ERROR (-5299)
static s32 ReadCacheFile(char* buf, u32 bufSize, u32* length) {
NANDFileInfo info;
s32 result;
*length = 0;
result = NANDPrivateOpen("/shared2/cntcache.txt", &info, NAND_ACCESS_READ);
if (result == NAND_RESULT_NOEXISTS) {
return NAND_RESULT_OK;
}
if (result != NAND_RESULT_OK) {
return result;
}
result = NANDGetLength(&info, length);
if (result == NAND_RESULT_OK && *length > bufSize) {
result = NAND_RESULT_MAXBLOCKS;
}
if (result == NAND_RESULT_OK && NANDRead(&info, buf, OSRoundUp32B(*length)) != *length) {
result = CNTCACHE_RESULT_READ_ERROR;
}
NANDClose(&info);
return result;
}
static s32 WriteCacheFile(const char* buf, u32 length);
s32 CNTCACHEAddDeleteTitle(ESTitleId titleId) {
char buf[CNTCACHE_READ_SIZE + CNTCACHE_LINE_SIZE] ALIGN32;
u32 length;
s32 result;
OSLockMutex(&_CNTCACHEMutex);
result = ReadCacheFile(buf, CNTCACHE_READ_SIZE, &length);
if (result == NAND_RESULT_OK) {
strcpy(buf + length, "DeleteTitle ");
length += strlen(buf + length);
length += sprintf(buf + length, "%016llx ", titleId);
buf[length++] = '\n';
result = WriteCacheFile(buf, length);
}
OSUnlockMutex(&_CNTCACHEMutex);
return result;
}
static s32 WriteCacheFile(const char* buf, u32 length) {
NANDFileInfo info;
s32 result;
NANDPrivateDelete("/tmp/cntcache.txt");
result = NANDPrivateCreate("/tmp/cntcache.txt", NAND_PERM_USER | NAND_PERM_GROUP, 0);
if (result != NAND_RESULT_OK) {
return result;
}
result = NANDPrivateOpen("/tmp/cntcache.txt", &info, NAND_ACCESS_WRITE);
if (result != NAND_RESULT_OK) {
return result;
}
if (NANDWrite(&info, buf, length) != length) {
result = CNTCACHE_RESULT_READ_ERROR;
}
NANDClose(&info);
if (result != NAND_RESULT_OK) {
return result;
}
return NANDPrivateMove("/tmp/cntcache.txt", "/shared2");
}
s32 CNTCACHEAddDeleteContent(ESTitleId titleId, const char* indices) {
char buf[CNTCACHE_READ_SIZE + CNTCACHE_LINE_SIZE] ALIGN32;
u32 length;
s32 result;
if (strlen(indices) > CNTCACHE_LINE_SIZE / 2) {
return NAND_RESULT_INVALID;
}
OSLockMutex(&_CNTCACHEMutex);
result = ReadCacheFile(buf, CNTCACHE_READ_SIZE, &length);
if (result == NAND_RESULT_OK) {
strcpy(buf + length, "DeleteContent ");
length += strlen(buf + length);
length += sprintf(buf + length, "%016llx ", titleId);
strcpy(buf + length, indices);
length += strlen(buf + length);
buf[length++] = '\n';
result = WriteCacheFile(buf, length);
}
OSUnlockMutex(&_CNTCACHEMutex);
return result;
}
static char* FindLineEnd(char* str, u32 length) {
u32 i;
for (i = 0; i < length; i++) {
if (*str == '\n') {
return str;
}
str++;
}
return NULL;
}
static inline s32 ExecuteCacheFile(NANDFileInfo* info, s32 result) {
u32 fileLength;
u8 buf[CNTCACHE_READ_SIZE] ALIGN32;
u32 lineLength;
char* line;
s32 readLength;
u32 offset;
char* lineEnd;
char* command;
if (result != NAND_RESULT_OK) {
return result;
}
result = NANDGetLength(info, &fileLength);
if (result != NAND_RESULT_OK) {
return result;
}
offset = 0;
while (fileLength != 0) {
readLength = fileLength;
if (readLength > sizeof(buf)) {
readLength = sizeof(buf);
}
line = (char*)buf;
if (NANDRead(info, buf, OSRoundUp32B(readLength)) != readLength) {
return CNTCACHE_RESULT_READ_ERROR;
}
result = NAND_RESULT_OK;
lineEnd = FindLineEnd(line, readLength);
if (!lineEnd) {
return NAND_RESULT_OK;
}
while (readLength > 0) {
*lineEnd = '\0';
lineLength = lineEnd - line;
command = strtok(line, " \n");
if (command) {
if (strcmp(command, "DeleteTitle") == 0) {
_CNTCACHEDeleteTitle();
} else if (strcmp(command, "DeleteContent") == 0) {
_CNTCACHEDeleteContent();
}
}
lineLength++;
line += lineLength;
fileLength -= lineLength;
readLength -= lineLength;
offset += lineLength;
lineEnd = FindLineEnd(line, readLength);
if (!lineEnd) {
break;
}
}
if (fileLength != 0) {
if (NANDSeek(info, offset, NAND_SEEK_BEG) != offset) {
return CNTCACHE_RESULT_READ_ERROR;
}
result = NAND_RESULT_OK;
}
}
return result;
}
s32 CNTCACHEClear(void) {
NANDFileInfo info;
BOOL opened = FALSE;
BOOL locked;
s32 result;
OSLockMutex(&_CNTCACHEMutex);
locked = TRUE;
result = NANDPrivateOpen("/shared2/cntcache.txt", &info, NAND_ACCESS_RW);
if (result == NAND_RESULT_NOEXISTS) {
result = NAND_RESULT_OK;
goto end;
}
result = ExecuteCacheFile(&info, result);
NANDClose(&info);
opened = FALSE;
if (NANDPrivateDelete("/shared2/cntcache.txt") != NAND_RESULT_OK) {
}
if (NANDPrivateDelete("/tmp/cntcache.txt") != NAND_RESULT_OK) {
}
end:
if (opened) {
NANDClose(&info);
}
if (locked) {
OSUnlockMutex(&_CNTCACHEMutex);
}
return result;
}
void _CNTCACHEDeleteTitle() {
char* token;
for (token = strtok(NULL, " \n"); token != NULL; token = strtok(NULL, " \n")) {
ESTitleId titleId;
int removable;
errno = 0;
titleId = strtoull(token, NULL, 16);
if (errno != 0 || (u32)ES_TITLE_TYPE(titleId) == 1) {
continue;
}
removable = _CNTCACHEIsTitleRemovable(titleId);
if (removable == 1) {
ES_DeleteTitle(titleId);
} else if (removable == 0) {
ES_DeleteTitleContent(titleId);
}
}
}