From 3a72a894869e9be3292d2cd3f0fdd4cd125ed21a Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Fri, 9 Oct 2026 18:27:05 +0000 Subject: [PATCH] cleanup(nandsd,esmisc): structured control flow replaces decompiler gotos (worker 76b2d600) --- src/system/iplNandSDWorker.cpp | 1426 +++++++++---------- src/utility/iplESMisc.cpp | 550 ++++--- tools/decomp-assist/cleanup2-w2.attempts.md | 228 +++ 3 files changed, 1177 insertions(+), 1027 deletions(-) diff --git a/src/system/iplNandSDWorker.cpp b/src/system/iplNandSDWorker.cpp index d3af23c8..5438918e 100644 --- a/src/system/iplNandSDWorker.cpp +++ b/src/system/iplNandSDWorker.cpp @@ -1069,107 +1069,105 @@ namespace ipl { OSReport("NandSDWorker: nand save num = %d\n", myWork->nandSaveNum); } - if (myWork->saveCacheHeap == NULL) { - goto clean_up; - } - - if (nand_get_length(c_transferid_path) != (int)OSRoundUp32B(NCD_MAC_ADDRESS_LENGTH)) { // If transfer.id is invalid - // Delete old one - err = nand::wrapper::PrivateDelete(c_transferid_path); - if (err != NAND_RESULT_OK) { - OSReport("NandSDWorker: NANDPrivateDelete %s failed.[%d]\n", c_transferid_path, err); - if (err == NAND_RESULT_CORRUPT) { - myWork->asyncResult = RESULT_NAND_CORRUPT; - goto clean_up; - } - } - - u8 type; - err = nand::wrapper::PrivateGetType(c_succession_path, &type); - if (err != NAND_RESULT_OK || type != NAND_TYPE_DIR) { - // Is "succession" a file? - if (err == NAND_RESULT_OK && type != NAND_TYPE_DIR) { - // Delete file. It's supposed to be a directory. - err = nand::wrapper::PrivateDelete(c_succession_path); - if (err != NAND_RESULT_OK) { - OSReport("NandSDWorker: NANDPrivateDelete %s failed.[%d]\n", c_succession_path, err); - if (err == NAND_RESULT_CORRUPT) { - myWork->asyncResult = RESULT_NAND_CORRUPT; - goto clean_up; - } - } - } else { - if (check_nand_free_area(1, 0, 0, 0) != RESULT_OK) { - OSReport("NandSDWorker: cannot make %s because of system area shortage.\n", c_succession_path); + if (myWork->saveCacheHeap != NULL) { + if (nand_get_length(c_transferid_path) != (int)OSRoundUp32B(NCD_MAC_ADDRESS_LENGTH)) { // If transfer.id is invalid + // Delete old one + err = nand::wrapper::PrivateDelete(c_transferid_path); + if (err != NAND_RESULT_OK) { + OSReport("NandSDWorker: NANDPrivateDelete %s failed.[%d]\n", c_transferid_path, err); + if (err == NAND_RESULT_CORRUPT) { + myWork->asyncResult = RESULT_NAND_CORRUPT; goto clean_up; } } - // Recreate directory - err = nand::wrapper::PrivateCreateDir(c_succession_path, NAND_PERM_ALL_RW, 0); + u8 type; + err = nand::wrapper::PrivateGetType(c_succession_path, &type); + if (err != NAND_RESULT_OK || type != NAND_TYPE_DIR) { + // Is "succession" a file? + if (err == NAND_RESULT_OK && type != NAND_TYPE_DIR) { + // Delete file. It's supposed to be a directory. + err = nand::wrapper::PrivateDelete(c_succession_path); + if (err != NAND_RESULT_OK) { + OSReport("NandSDWorker: NANDPrivateDelete %s failed.[%d]\n", c_succession_path, err); + if (err == NAND_RESULT_CORRUPT) { + myWork->asyncResult = RESULT_NAND_CORRUPT; + goto clean_up; + } + } + } else { + if (check_nand_free_area(1, 0, 0, 0) != RESULT_OK) { + OSReport("NandSDWorker: cannot make %s because of system area shortage.\n", c_succession_path); + goto clean_up; + } + } + + // Recreate directory + err = nand::wrapper::PrivateCreateDir(c_succession_path, NAND_PERM_ALL_RW, 0); + if (err != NAND_RESULT_OK) { + OSReport("NandSDWorker: NANDPrivateCreateDir %s failed.[%d]\n", c_succession_path, err); + if (err == NAND_RESULT_CORRUPT) { + myWork->asyncResult = RESULT_NAND_CORRUPT; + } + goto clean_up; + } + } + + if (check_nand_free_area(1, 1, 0, 0) != RESULT_OK) { + OSReport("NandSDWorker: cannot make %s because of system area shortage.\n", c_transferid_path); + goto clean_up; + } + + // Recreate transfer.id + err = nand::wrapper::PrivateCreate(c_transferid_path, NAND_PERM_ALL_RW, 0); if (err != NAND_RESULT_OK) { - OSReport("NandSDWorker: NANDPrivateCreateDir %s failed.[%d]\n", c_succession_path, err); + OSReport("NandSDWorker: NANDPrivateCreate %s failed.[%d]\n", c_transferid_path, err); if (err == NAND_RESULT_CORRUPT) { myWork->asyncResult = RESULT_NAND_CORRUPT; } goto clean_up; } - } - if (check_nand_free_area(1, 1, 0, 0) != RESULT_OK) { - OSReport("NandSDWorker: cannot make %s because of system area shortage.\n", c_transferid_path); - goto clean_up; - } - - // Recreate transfer.id - err = nand::wrapper::PrivateCreate(c_transferid_path, NAND_PERM_ALL_RW, 0); - if (err != NAND_RESULT_OK) { - OSReport("NandSDWorker: NANDPrivateCreate %s failed.[%d]\n", c_transferid_path, err); - if (err == NAND_RESULT_CORRUPT) { - myWork->asyncResult = RESULT_NAND_CORRUPT; + // Let's write some stuff to the file... + NANDFileInfo fileInfo; + err = nand::wrapper::PrivateOpen(c_transferid_path, &fileInfo, NAND_ACCESS_WRITE); + if (err != NAND_RESULT_OK) { + OSReport("NandSDWorker: NANDPrivateOpen %s failed.[%d]\n", c_transferid_path, err); + goto clean_up; } - goto clean_up; - } - // Let's write some stuff to the file... - NANDFileInfo fileInfo; - err = nand::wrapper::PrivateOpen(c_transferid_path, &fileInfo, NAND_ACCESS_WRITE); - if (err != NAND_RESULT_OK) { - OSReport("NandSDWorker: NANDPrivateOpen %s failed.[%d]\n", c_transferid_path, err); - goto clean_up; - } + // Let's write the MAC address... + buf = (u8*)MEMAllocFromExpHeapEx(myWork->mainHeap, OSRoundUp32B(NCD_MAC_ADDRESS_LENGTH), 32); + memset(buf, 0, OSRoundUp32B(NCD_MAC_ADDRESS_LENGTH)); + NETGetWirelessMacAddress(buf); - // Let's write the MAC address... - buf = (u8*)MEMAllocFromExpHeapEx(myWork->mainHeap, OSRoundUp32B(NCD_MAC_ADDRESS_LENGTH), 32); - memset(buf, 0, OSRoundUp32B(NCD_MAC_ADDRESS_LENGTH)); - NETGetWirelessMacAddress(buf); + // and that's it. - // and that's it. - - // Write the buffer to the file! - err = nand::wrapper::Write(&fileInfo, buf, OSRoundUp32B(NCD_MAC_ADDRESS_LENGTH)); - if (err != (s32)OSRoundUp32B(NCD_MAC_ADDRESS_LENGTH)) { - OSReport("NandSDWorker: NANDWrite %s failed.[%d]\n", c_transferid_path, err); - if (err == NAND_RESULT_CORRUPT) { - myWork->asyncResult = RESULT_NAND_CORRUPT; + // Write the buffer to the file! + err = nand::wrapper::Write(&fileInfo, buf, OSRoundUp32B(NCD_MAC_ADDRESS_LENGTH)); + if (err != (s32)OSRoundUp32B(NCD_MAC_ADDRESS_LENGTH)) { + OSReport("NandSDWorker: NANDWrite %s failed.[%d]\n", c_transferid_path, err); + if (err == NAND_RESULT_CORRUPT) { + myWork->asyncResult = RESULT_NAND_CORRUPT; + } + nand::wrapper::Close(&fileInfo); + goto clean_up; } - nand::wrapper::Close(&fileInfo); - goto clean_up; - } - // We're done with the file now. Close... - err = nand::wrapper::Close(&fileInfo); - if (err != NAND_RESULT_OK) { - OSReport("NandSDWorker: NANDClose %s failed.[%d]\n", c_transferid_path, err); - if (err == NAND_RESULT_CORRUPT) { - myWork->asyncResult = RESULT_NAND_CORRUPT; + // We're done with the file now. Close... + err = nand::wrapper::Close(&fileInfo); + if (err != NAND_RESULT_OK) { + OSReport("NandSDWorker: NANDClose %s failed.[%d]\n", c_transferid_path, err); + if (err == NAND_RESULT_CORRUPT) { + myWork->asyncResult = RESULT_NAND_CORRUPT; + } + } else { + OSReport("NandSDWorker: %s created.[%02x%02x%02x%02x%02x%02x]\n", c_transferid_path, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); } - goto clean_up; + } else { + // transfer.id is valid. Do nothing. + OSReport("NandSDWorker: %s already exist.\n", c_transferid_path); } - OSReport("NandSDWorker: %s created.[%02x%02x%02x%02x%02x%02x]\n", c_transferid_path, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); - } else { - // transfer.id is valid. Do nothing. - OSReport("NandSDWorker: %s already exist.\n", c_transferid_path); } clean_up: @@ -1599,16 +1597,16 @@ namespace ipl { if (err != NAND_RESULT_OK) { OSReport("NandSDWorker: failed to close %s [%d]\n", path, err); ret = RESULT_FATAL_SD_ERROR; - goto clean_up; - } - fileOpened = false; - - // Verify signature in file - if (bannerFile->signature != WII_SAVE_BANNER_SIGNATURE) { - OSReport("NandSDWorker: banner file %s signature is not correct\n", path); - ret = RESULT_BAD_FILE; } else { - ret = RESULT_OK; + fileOpened = false; + + // Verify signature in file + if (bannerFile->signature != WII_SAVE_BANNER_SIGNATURE) { + OSReport("NandSDWorker: banner file %s signature is not correct\n", path); + ret = RESULT_BAD_FILE; + } else { + ret = RESULT_OK; + } } clean_up: @@ -1954,60 +1952,58 @@ namespace ipl { if (err == WAD_ERROR_INCORRECT_DEVICE) { OSReport("NandSDWorker: WADImportGetBlocks failed, not original Wii.[%d]\n", err); myWork->asyncResult = RESULT_NOT_TRANSFERRABLE; - goto clean_up; } else if (err != WAD_ERROR_OK) { OSReport("NandSDWorker: WADImportGetBlocks failed.[%d]\n", err); myWork->asyncResult = RESULT_BAD_FILE; - goto clean_up; - } + } else { + // Calculate required blocks + ((AppBlocksInfo*)myWork->paramA)->bytes = NAND_BLOCKS_TO_BYTES(wadBlocks.privateBlocks + wadBlocks.unkBlocks); + ((AppBlocksInfo*)myWork->paramA)->blocks = wadBlocks.privateInodes + wadBlocks.unkInodes; - // Calculate required blocks - ((AppBlocksInfo*)myWork->paramA)->bytes = NAND_BLOCKS_TO_BYTES(wadBlocks.privateBlocks + wadBlocks.unkBlocks); - ((AppBlocksInfo*)myWork->paramA)->blocks = wadBlocks.privateInodes + wadBlocks.unkInodes; + // Calculate free blocks + if (myWork->paramB != NULL) { + u32 sysNodes, sysBlocks; + s32 userNodes, userBlocks; + err = get_nand_free_area(&sysNodes, &sysBlocks, &userNodes, &userBlocks); + if (err != RESULT_OK) { + myWork->asyncResult = RESULT_FATAL_SD_ERROR; + goto clean_up; + } + ((AppBlocksInfo*)myWork->paramB)->bytes = NAND_BLOCKS_TO_BYTES(userBlocks); + ((AppBlocksInfo*)myWork->paramB)->blocks = userNodes; + } - // Calculate free blocks - if (myWork->paramB != NULL) { - u32 sysNodes, sysBlocks; - s32 userNodes, userBlocks; - err = get_nand_free_area(&sysNodes, &sysBlocks, &userNodes, &userBlocks); - if (err != RESULT_OK) { - myWork->asyncResult = RESULT_FATAL_SD_ERROR; + // MWCC needs the banner result to remain live through the free-area query. + if (result != RESULT_OK) { + myWork->asyncResult = result; goto clean_up; } - ((AppBlocksInfo*)myWork->paramB)->bytes = NAND_BLOCKS_TO_BYTES(userBlocks); - ((AppBlocksInfo*)myWork->paramB)->blocks = userNodes; + + // If the file already exists + if (nand_app_exist_ex(sdAppBanner->titleId) == EXISTENCE_COMPLETE) { + OSReport("NandSDWorker: titleid = 0x%016llx is already in nand.\n", sdAppBanner->titleId); + myWork->asyncResult = RESULT_ALREADY_EXISTS; + goto clean_up; + } + + err = get_nand_title_version(sdAppBanner->titleId, &nandTitleVersion); + if (err != RESULT_OK) { + myWork->asyncResult = err; + } else { + // Compare version + int nandTitleMajor, sdTitleMajor; + nandTitleMajor = (u16)(nandTitleVersion >> 8); + sdTitleMajor = (u16)(sdTitleVersion >> 8); + if (nandTitleMajor > sdTitleMajor) { + OSReport("NandSDWorker: titleid %016llx has higher version than that of sd (%d vs %d)\n", sdAppBanner->titleId, nandTitleVersion, + sdTitleVersion); + myWork->asyncResult = RESULT_VERSION_ERR; + } else { + myWork->asyncResult = RESULT_OK; + } + } } - // MWCC needs the banner result to remain live through the free-area query. - if (result != RESULT_OK) { - myWork->asyncResult = result; - goto clean_up; - } - - // If the file already exists - if (nand_app_exist_ex(sdAppBanner->titleId) == EXISTENCE_COMPLETE) { - OSReport("NandSDWorker: titleid = 0x%016llx is already in nand.\n", sdAppBanner->titleId); - myWork->asyncResult = RESULT_ALREADY_EXISTS; - goto clean_up; - } - - err = get_nand_title_version(sdAppBanner->titleId, &nandTitleVersion); - if (err != RESULT_OK) { - myWork->asyncResult = err; - goto clean_up; - } - - // Compare version - int nandTitleMajor, sdTitleMajor; - nandTitleMajor = (u16)(nandTitleVersion >> 8); - sdTitleMajor = (u16)(sdTitleVersion >> 8); - if (nandTitleMajor > sdTitleMajor) { - OSReport("NandSDWorker: titleid %016llx has higher version than that of sd (%d vs %d)\n", sdAppBanner->titleId, nandTitleVersion, - sdTitleVersion); - myWork->asyncResult = RESULT_VERSION_ERR; - } else { - myWork->asyncResult = RESULT_OK; - } clean_up: MEMFreeToAllocator(&myWork->scratchAllocator, sdAppBanner); } @@ -2023,95 +2019,94 @@ namespace ipl { s64 sdFreeArea = get_sd_free_area(); if (sdFreeArea < 0) { myWork->asyncResult = sdFreeArea; - goto clean_up; - } + } else { + u64 totalBackupSize; + totalBackupSize = 0; + for (int i = 0; i < newTitleCount; i++) { + if (s_sd_state != SD_STATE_READY) { + myWork->asyncResult = RESULT_FATAL_SD_ERROR; + goto clean_up; + } - u64 totalBackupSize; - totalBackupSize = 0; - for (int i = 0; i < newTitleCount; i++) { - if (s_sd_state != SD_STATE_READY) { - myWork->asyncResult = RESULT_FATAL_SD_ERROR; - goto clean_up; + // Get app banner + int result = get_app_banner_from_meta(newTitleIds[i], backupData); + if (result != RESULT_OK) { + myWork->asyncResult = result; + goto clean_up; + } + + u32 backupSize = 0; + u32 wadOffset = sizeof(SDAppBanner) + ROUNDUP(backupData->head.thumbSize, 64); + + s32 err = WADBackupEx(newTitleIds[i], 1, &myWork->scratchAllocator, NULL, &backupSize, WAD_LOCATION_SD_CARD, wadOffset, NULL); + if (err != WAD_ERROR_OK) { + OSReport("NandSDWorker: WADBackup failed.[%d]\n", err); + myWork->asyncResult = RESULT_FATAL_SD_ERROR; + goto clean_up; + } + totalBackupSize += wadOffset; + totalBackupSize += backupSize; } - // Get app banner - int result = get_app_banner_from_meta(newTitleIds[i], backupData); - if (result != RESULT_OK) { - myWork->asyncResult = result; - goto clean_up; + for (int i = 0; i < replacingTitleCount; i++) { + if (s_sd_state != SD_STATE_READY) { + myWork->asyncResult = RESULT_FATAL_SD_ERROR; + goto clean_up; + } + + int result = get_app_banner_from_meta(replacingTitleIds[i], backupData); + if (result != RESULT_OK) { + myWork->asyncResult = result; + goto clean_up; + } + + u32 backupSize = 0; + u32 wadOffset = sizeof(SDAppBanner) + ROUNDUP(backupData->head.thumbSize, 64); + + s32 err = WADBackupEx(replacingTitleIds[i], 1, &myWork->scratchAllocator, NULL, &backupSize, WAD_LOCATION_SD_CARD, wadOffset, NULL); + if (err != WAD_ERROR_OK) { + OSReport("NandSDWorker: WADBackup failed.[%d]\n", err); + myWork->asyncResult = RESULT_FATAL_SD_ERROR; + goto clean_up; + } + + totalBackupSize += wadOffset; + totalBackupSize += backupSize; } - u32 backupSize = 0; - u32 wadOffset = sizeof(SDAppBanner) + ROUNDUP(backupData->head.thumbSize, 64); + u64 totalDeletableSize; + totalDeletableSize = 0; + for (int i = 0; i < replacingTitleCount; i++) { + if (s_sd_state != SD_STATE_READY) { + myWork->asyncResult = RESULT_FATAL_SD_ERROR; + goto clean_up; + } - s32 err = WADBackupEx(newTitleIds[i], 1, &myWork->scratchAllocator, NULL, &backupSize, WAD_LOCATION_SD_CARD, wadOffset, NULL); - if (err != WAD_ERROR_OK) { - OSReport("NandSDWorker: WADBackup failed.[%d]\n", err); - myWork->asyncResult = RESULT_FATAL_SD_ERROR; - goto clean_up; - } - totalBackupSize += wadOffset; - totalBackupSize += backupSize; - } + char sdAppPath[NAND_MAX_PATH]; + get_sd_app_path(replacingTitleIds[i], sdAppPath); - for (int i = 0; i < replacingTitleCount; i++) { - if (s_sd_state != SD_STATE_READY) { - myWork->asyncResult = RESULT_FATAL_SD_ERROR; - goto clean_up; - } - - int result = get_app_banner_from_meta(replacingTitleIds[i], backupData); - if (result != RESULT_OK) { - myWork->asyncResult = result; - goto clean_up; - } - - u32 backupSize = 0; - u32 wadOffset = sizeof(SDAppBanner) + ROUNDUP(backupData->head.thumbSize, 64); - - s32 err = WADBackupEx(replacingTitleIds[i], 1, &myWork->scratchAllocator, NULL, &backupSize, WAD_LOCATION_SD_CARD, wadOffset, NULL); - if (err != WAD_ERROR_OK) { - OSReport("NandSDWorker: WADBackup failed.[%d]\n", err); - myWork->asyncResult = RESULT_FATAL_SD_ERROR; - goto clean_up; - } - - totalBackupSize += wadOffset; - totalBackupSize += backupSize; - } - - u64 totalDeletableSize; - totalDeletableSize = 0; - for (int i = 0; i < replacingTitleCount; i++) { - if (s_sd_state != SD_STATE_READY) { - myWork->asyncResult = RESULT_FATAL_SD_ERROR; - goto clean_up; - } - - char sdAppPath[NAND_MAX_PATH]; - get_sd_app_path(replacingTitleIds[i], sdAppPath); - - FAFileStat fileStat; - if (FAFstat(sdAppPath, &fileStat) == FA_ERR_SUCCESS) { - if (is_fa_file(fileStat.stat)) { - totalDeletableSize += fileStat.size; + FAFileStat fileStat; + if (FAFstat(sdAppPath, &fileStat) == FA_ERR_SUCCESS) { + if (is_fa_file(fileStat.stat)) { + totalDeletableSize += fileStat.size; + } else { + myWork->asyncResult = RESULT_FATAL_SD_ERROR; + goto clean_up; + } } else { myWork->asyncResult = RESULT_FATAL_SD_ERROR; goto clean_up; } - } else { - myWork->asyncResult = RESULT_FATAL_SD_ERROR; - goto clean_up; } - } - u64 netSize; - netSize = totalBackupSize - totalDeletableSize; - if (netSize + 0x19000 > sdFreeArea) { - OSReport("NandSDWorker: total backup size [%d] is too large for SD card. \n", netSize + 0x19000); - myWork->asyncResult = netSize + 0x19000; - } else { - myWork->asyncResult = RESULT_OK; + u64 netSize; + netSize = totalBackupSize - totalDeletableSize; + if (netSize + 0x19000 > sdFreeArea) { + OSReport("NandSDWorker: total backup size [%d] is too large for SD card. \n", netSize + 0x19000); + myWork->asyncResult = netSize + 0x19000; + } else { + myWork->asyncResult = RESULT_OK; + } } clean_up: @@ -2553,41 +2548,38 @@ namespace ipl { if (esErr != ES_ERR_OK) { OSReport("NandSDWorker: ES_ListOwnedTitles failed [%d]\n", esErr); ret = RESULT_FATAL_SD_ERROR; - goto clean_up; + } else { + // Get title ID list + titleIds = (ESTitleId*)MEMAllocFromExpHeapEx(myWork->mainHeap, titleIdCount * sizeof(ESTitleId), 32); + esErr = ES_ListOwnedTitles(titleIds, &titleIdCount); + if (esErr != ES_ERR_OK) { + OSReport("NandSDWorker: ES_ListOwnedTitles failed [%d]\n", esErr); + ret = RESULT_FATAL_SD_ERROR; + } else { + for (int i = 0; i < titleIdCount; i++) { + // Check if it's a disc title (title type 00010000 and only has "data" folder) + if (!is_disk_or_user_nand_app(titleIds[i]) && !is_data_only_title(titleIds[i])) { + continue; + } + + // Goodbye ticket! + int result = delete_ticket(titleIds[i]); + if (result != RESULT_OK && result != RESULT_ES_ERROR) { + OSReport("NandSDWorker: delete ticket 0x%016llx failed [%d]\n", titleIds[i], result); + } + + // Goodbye title! + esErr = utility::ESMisc::DeleteTitle(System::getMem2Sys(), titleIds[i]); + if (esErr != ES_ERR_OK && esErr != ES_ERR_DONT_EXISTS) { + OSReport("NandSDWorker: ESMisc::DeleteTitle 0x%016llx failed [%d]\n", titleIds[i], esErr); + } + + OSReport("NandSDWorker: delete title [0x%016llx]\n", titleIds[i]); + } + ret = RESULT_OK; + } } - // Get title ID list - titleIds = (ESTitleId*)MEMAllocFromExpHeapEx(myWork->mainHeap, titleIdCount * sizeof(ESTitleId), 32); - esErr = ES_ListOwnedTitles(titleIds, &titleIdCount); - if (esErr != ES_ERR_OK) { - OSReport("NandSDWorker: ES_ListOwnedTitles failed [%d]\n", esErr); - ret = RESULT_FATAL_SD_ERROR; - goto clean_up; - } - - for (int i = 0; i < titleIdCount; i++) { - // Check if it's a disc title (title type 00010000 and only has "data" folder) - if (!is_disk_or_user_nand_app(titleIds[i]) && !is_data_only_title(titleIds[i])) { - continue; - } - - // Goodbye ticket! - int result = delete_ticket(titleIds[i]); - if (result != RESULT_OK && result != RESULT_ES_ERROR) { - OSReport("NandSDWorker: delete ticket 0x%016llx failed [%d]\n", titleIds[i], result); - } - - // Goodbye title! - esErr = utility::ESMisc::DeleteTitle(System::getMem2Sys(), titleIds[i]); - if (esErr != ES_ERR_OK && esErr != ES_ERR_DONT_EXISTS) { - OSReport("NandSDWorker: ESMisc::DeleteTitle 0x%016llx failed [%d]\n", titleIds[i], esErr); - } - - OSReport("NandSDWorker: delete title [0x%016llx]\n", titleIds[i]); - } - ret = RESULT_OK; - - clean_up: if (titleIds != NULL) { MEMFreeToExpHeap(myWork->mainHeap, titleIds); } @@ -2697,59 +2689,58 @@ namespace ipl { if (esErr != ES_ERR_OK) { OSReport("NandSDWorker: ES_ListTitlesOnCard failed [%d]\n", esErr); ret = RESULT_FATAL_SD_ERROR; - goto clean_up; - } - - // Now let's delete all titles! Except the system titles of course - for (int i = 0; i < titleCount; i++) { - switch ((u32)ES_TITLE_TYPE_NOMASK(titleIds[i])) { - case ES_TITLE_TYPE(SYSMENU_TITLE_ID): { - // Delete Wii Menu save data. - if ((u32)ES_TITLE_CODE(titleIds[i]) == (u32)ES_TITLE_CODE(SYSMENU_TITLE_ID)) { - ret = recursion_nand_save(titleIds[i], RECURSIVE_DELETE, c_ignore_list_for_wiimenu, ARRAY_LENGTH(c_ignore_list_for_wiimenu)); - if (ret != RESULT_OK && ret != RESULT_FATAL_SD_ERROR) { - OSReport("NandSDWorker: failed to delete wii menu data.\n"); + } else { + // Now let's delete all titles! Except the system titles of course + for (int i = 0; i < titleCount; i++) { + switch ((u32)ES_TITLE_TYPE_NOMASK(titleIds[i])) { + case ES_TITLE_TYPE(SYSMENU_TITLE_ID): { + // Delete Wii Menu save data. + if ((u32)ES_TITLE_CODE(titleIds[i]) == (u32)ES_TITLE_CODE(SYSMENU_TITLE_ID)) { + ret = recursion_nand_save(titleIds[i], RECURSIVE_DELETE, c_ignore_list_for_wiimenu, ARRAY_LENGTH(c_ignore_list_for_wiimenu)); + if (ret != RESULT_OK && ret != RESULT_FATAL_SD_ERROR) { + OSReport("NandSDWorker: failed to delete wii menu data.\n"); + goto clean_up; + } + OSReport("NandSDWorker: successfully initialize wii menu data.\n"); + } + break; + } + case TITLE_TYPE_DISC: + case TITLE_TYPE_CHANNEL: + case TITLE_TYPE_DISC_CHANNEL: + case TITLE_TYPE_SHARED: + case TITLE_TYPE_UNK6: + case TITLE_TYPE_USER_APP7: { + // Delete user installed titles + ret = utility::ESMisc::DeleteTitle(System::getMem2Sys(), titleIds[i]); + if (ret != ES_ERR_OK && ret != ES_ERR_DONT_EXISTS) { + OSReport("NandSDWorker: ESMisc::DeleteTitle 0x%016llx failed [%d]\n", titleIds[i], ret); + ret = RESULT_FATAL_SD_ERROR; goto clean_up; } - OSReport("NandSDWorker: successfully initialize wii menu data.\n"); + OSReport("NandSDWorker: successfully delete app [0x%016llx].\n", titleIds[i]); + break; } - break; - } - case TITLE_TYPE_DISC: - case TITLE_TYPE_CHANNEL: - case TITLE_TYPE_DISC_CHANNEL: - case TITLE_TYPE_SHARED: - case TITLE_TYPE_UNK6: - case TITLE_TYPE_USER_APP7: { - // Delete user installed titles - ret = utility::ESMisc::DeleteTitle(System::getMem2Sys(), titleIds[i]); - if (ret != ES_ERR_OK && ret != ES_ERR_DONT_EXISTS) { - OSReport("NandSDWorker: ESMisc::DeleteTitle 0x%016llx failed [%d]\n", titleIds[i], ret); - ret = RESULT_FATAL_SD_ERROR; - goto clean_up; + case TITLE_TYPE_SYSTEM_CHANNEL: + case TITLE_TYPE_UNK3: + case TITLE_TYPE_HIDDEN_CHANNEL: { + // Delete save data for hidden and system channels. + ret = recursion_nand_save(titleIds[i], RECURSIVE_DELETE, NULL, 0); + if (ret != RESULT_OK && ret != RESULT_FATAL_SD_ERROR) { + OSReport("NandSDWorker: failed to delete 0x%016llx save data.\n", titleIds[i]); + goto clean_up; + } + break; } - OSReport("NandSDWorker: successfully delete app [0x%016llx].\n", titleIds[i]); - break; - } - case TITLE_TYPE_SYSTEM_CHANNEL: - case TITLE_TYPE_UNK3: - case TITLE_TYPE_HIDDEN_CHANNEL: { - // Delete save data for hidden and system channels. - ret = recursion_nand_save(titleIds[i], RECURSIVE_DELETE, NULL, 0); - if (ret != RESULT_OK && ret != RESULT_FATAL_SD_ERROR) { - OSReport("NandSDWorker: failed to delete 0x%016llx save data.\n", titleIds[i]); - goto clean_up; + default: { + // Unknown channel type. Don't bother to do anything with it. + OSReport("NandSDWorker: don't process for 0x%016llx\n", titleIds[i]); + break; } - break; - } - default: { - // Unknown channel type. Don't bother to do anything with it. - OSReport("NandSDWorker: don't process for 0x%016llx\n", titleIds[i]); - break; } } + ret = RESULT_OK; } - ret = RESULT_OK; clean_up: if (titleIds != NULL) { @@ -2935,88 +2926,85 @@ namespace ipl { if (wadErr == WAD_ERROR_NOCOPY) { OSReport("NandSDWorker: WADBackup 0x%016llx failed, this save data has nocopy only.[%d]\n", myWork->curTitleId, wadErr); myWork->asyncResult = RESULT_NOCOPY; - goto clean_up; } else if (wadErr != 0) { OSReport("NandSDWorker: WADBackup %s failed.[%d]\n", bnrPath, wadErr); myWork->asyncResult = RESULT_FATAL_SD_ERROR; - goto clean_up; - } - - if (backupWadSize + sizeof(SDSaveBanner) > sdFreeArea) { - OSReport("NandSDWorker: backup size [%d] is too large for SD card.\n", backupWadSize); - myWork->asyncResult = RESULT_OUT_OF_SPACE; - goto clean_up; - } - memcpy(sdSaveBnr->MD5Sum, c_md5_secret_key, NET_MD5_DIGEST_SIZE); - - NETMD5Sum MD5Sum; - NETCalcMD5(MD5Sum, sdSaveBnr, sizeof(SDSaveBanner)); - - memcpy(sdSaveBnr->MD5Sum, MD5Sum, NET_MD5_DIGEST_SIZE); - encSaveBnr = MEMAllocFromAllocator(&myWork->scratchAllocator, sizeof(SDSaveBanner)); - if (encrypt(sdSaveBnr, sizeof(SDSaveBanner), encSaveBnr) != RESULT_OK) { - myWork->asyncResult = RESULT_FATAL_SD_ERROR; - goto clean_up; - } - - backedUp = true; - if (isDataOnlyTitle) { - wadErr = WADBackupEx(myWork->curTitleId, 1, &myWork->scratchAllocator, savePath, &backupWadSize, WAD_LOCATION_SD_CARD, 0xf0c0, NULL); } else { - wadErr = WADBackupEx(myWork->curTitleId, 14, &myWork->scratchAllocator, savePath, &backupWadSize, WAD_LOCATION_SD_CARD, 0xf0c0, NULL); - } - if (wadErr == WAD_ERROR_NOCOPY) { - OSReport("NandSDWorker: WADBackup 0x%016llx failed, this save data has nocopy only.[%d]\n", myWork->curTitleId, wadErr); - myWork->asyncResult = RESULT_NOCOPY; - goto clean_up; - } else if (wadErr != 0) { - OSReport("NandSDWorker: WADBackup %s failed.[%d]\n", bnrPath, wadErr); - myWork->asyncResult = RESULT_FATAL_SD_ERROR; - goto clean_up; - } - OSReport("NandSDWorker: make wad backup, size = [%d].\n", backupWadSize); + if (backupWadSize + sizeof(SDSaveBanner) > sdFreeArea) { + OSReport("NandSDWorker: backup size [%d] is too large for SD card.\n", backupWadSize); + myWork->asyncResult = RESULT_OUT_OF_SPACE; + goto clean_up; + } + memcpy(sdSaveBnr->MD5Sum, c_md5_secret_key, NET_MD5_DIGEST_SIZE); - FAFILE* stream; - stream = FAFopen(savePath, "r+"); - if (stream == NULL) { - OSReport("NandSDWorker: FAFopen %s failed.\n", savePath); - handle_sd_error(FA_ERR_SYSTEM, &myWork->asyncResult); - goto clean_up; - } - OSReport("NandSDWorker: create %s in sd.\n", savePath); + NETMD5Sum MD5Sum; + NETCalcMD5(MD5Sum, sdSaveBnr, sizeof(SDSaveBanner)); - if (handle_sd_error(FAFseek(stream, 0, FA_SEEK_ORIGIN_BEGIN), &myWork->asyncResult) != RESULT_OK) { - OSReport("NandSDWorker: FAFseek %s failed.\n", savePath); - FAFclose(stream); - goto clean_up; - } + memcpy(sdSaveBnr->MD5Sum, MD5Sum, NET_MD5_DIGEST_SIZE); + encSaveBnr = MEMAllocFromAllocator(&myWork->scratchAllocator, sizeof(SDSaveBanner)); + if (encrypt(sdSaveBnr, sizeof(SDSaveBanner), encSaveBnr) != RESULT_OK) { + myWork->asyncResult = RESULT_FATAL_SD_ERROR; + goto clean_up; + } - size_t fwriteRes; - fwriteRes = FAFwrite(encSaveBnr, sizeof(SDSaveBanner), 1, stream); - if (fwriteRes != 1) { - OSReport("NandSDWorker: FAFwrite %s failed.[%d]\n", savePath, fwriteRes); - handle_sd_error(FA_ERR_SYSTEM, &myWork->asyncResult); - FAFclose(stream); - goto clean_up; - } + backedUp = true; + if (isDataOnlyTitle) { + wadErr = WADBackupEx(myWork->curTitleId, 1, &myWork->scratchAllocator, savePath, &backupWadSize, WAD_LOCATION_SD_CARD, 0xf0c0, NULL); + } else { + wadErr = WADBackupEx(myWork->curTitleId, 14, &myWork->scratchAllocator, savePath, &backupWadSize, WAD_LOCATION_SD_CARD, 0xf0c0, NULL); + } + if (wadErr == WAD_ERROR_NOCOPY) { + OSReport("NandSDWorker: WADBackup 0x%016llx failed, this save data has nocopy only.[%d]\n", myWork->curTitleId, wadErr); + myWork->asyncResult = RESULT_NOCOPY; + goto clean_up; + } else if (wadErr != 0) { + OSReport("NandSDWorker: WADBackup %s failed.[%d]\n", bnrPath, wadErr); + myWork->asyncResult = RESULT_FATAL_SD_ERROR; + goto clean_up; + } + OSReport("NandSDWorker: make wad backup, size = [%d].\n", backupWadSize); - if (handle_sd_error(FAFclose(stream), &myWork->asyncResult) != RESULT_OK) { - OSReport("NandSDWorker: FAFclose %s failed.\n", savePath); - goto clean_up; - } + FAFILE* stream; + stream = FAFopen(savePath, "r+"); + if (stream == NULL) { + OSReport("NandSDWorker: FAFopen %s failed.\n", savePath); + handle_sd_error(FA_ERR_SYSTEM, &myWork->asyncResult); + goto clean_up; + } + OSReport("NandSDWorker: create %s in sd.\n", savePath); - int nocopyExists, notransferExists; - nocopyExists = item_exist_nand_save_folder(myWork->curTitleId, c_nocopy_folder_name); - notransferExists = item_exist_nand_save_folder(myWork->curTitleId, c_notransfer_folder_name); + if (handle_sd_error(FAFseek(stream, 0, FA_SEEK_ORIGIN_BEGIN), &myWork->asyncResult) != RESULT_OK) { + OSReport("NandSDWorker: FAFseek %s failed.\n", savePath); + FAFclose(stream); + goto clean_up; + } - // MWCC needs this separate boolean conversion when packing the copy flags. - bool dataOnlyTitle; - dataOnlyTitle = false; - if (isDataOnlyTitle) { - dataOnlyTitle = true; + size_t fwriteRes; + fwriteRes = FAFwrite(encSaveBnr, sizeof(SDSaveBanner), 1, stream); + if (fwriteRes != 1) { + OSReport("NandSDWorker: FAFwrite %s failed.[%d]\n", savePath, fwriteRes); + handle_sd_error(FA_ERR_SYSTEM, &myWork->asyncResult); + FAFclose(stream); + goto clean_up; + } + + if (handle_sd_error(FAFclose(stream), &myWork->asyncResult) != RESULT_OK) { + OSReport("NandSDWorker: FAFclose %s failed.\n", savePath); + } else { + int nocopyExists, notransferExists; + nocopyExists = item_exist_nand_save_folder(myWork->curTitleId, c_nocopy_folder_name); + notransferExists = item_exist_nand_save_folder(myWork->curTitleId, c_notransfer_folder_name); + + // MWCC needs this separate boolean conversion when packing the copy flags. + bool dataOnlyTitle; + dataOnlyTitle = false; + if (isDataOnlyTitle) { + dataOnlyTitle = true; + } + myWork->asyncResult = nocopyExists | (notransferExists << 1) | (dataOnlyTitle << 2); + myWork->sdSaveNum++; + } } - myWork->asyncResult = nocopyExists | (notransferExists << 1) | (dataOnlyTitle << 2); - myWork->sdSaveNum++; clean_up: if (sdSaveBnr != NULL) { @@ -3253,124 +3241,122 @@ namespace ipl { if (wadErr == WAD_ERROR_INCORRECT_DEVICE) { OSReport("NandSDWorker: WADImportGetBlocks failed, not original Wii.[%d]\n", wadErr); myWork->asyncResult = RESULT_NOT_TRANSFERRABLE; - goto clean_up; } else if (wadErr != WAD_ERROR_OK) { OSReport("NandSDWorker: WADImportGetBlocks failed.[%d]\n", wadErr); myWork->asyncResult = RESULT_BAD_FILE; - goto clean_up; - } - - OSReport("NandSDWorker: shared content inodes/blocks = %d/%d\n", wadBlocks.sharedInodes, wadBlocks.sharedBlocks); - OSReport("NandSDWorker: private content inodes/blocks = %d/%d\n", wadBlocks.privateInodes, wadBlocks.privateBlocks); - OSReport("NandSDWorker: file inodes/blocks = %d/%d\n", wadBlocks.fileInodes, wadBlocks.fileBlocks); - int result; - result = - check_nand_free_area(wadBlocks.sharedInodes, wadBlocks.sharedBlocks, wadBlocks.privateInodes + wadBlocks.fileInodes + 1, - (saveBanner->headerSize + NAND_BLOCK_SIZE - 1) / NAND_BLOCK_SIZE + wadBlocks.fileBlocks + wadBlocks.privateBlocks); - if (result != RESULT_OK) { - myWork->asyncResult = result; - goto clean_up; - } - - if (!isDataOnlyTitle) { - WADBackupHeader wadBackupHeader; - result = get_sd_wad_header(myWork->curTitleId, &wadBackupHeader, 0); + } else { + OSReport("NandSDWorker: shared content inodes/blocks = %d/%d\n", wadBlocks.sharedInodes, wadBlocks.sharedBlocks); + OSReport("NandSDWorker: private content inodes/blocks = %d/%d\n", wadBlocks.privateInodes, wadBlocks.privateBlocks); + OSReport("NandSDWorker: file inodes/blocks = %d/%d\n", wadBlocks.fileInodes, wadBlocks.fileBlocks); + int result; + result = + check_nand_free_area(wadBlocks.sharedInodes, wadBlocks.sharedBlocks, wadBlocks.privateInodes + wadBlocks.fileInodes + 1, + (saveBanner->headerSize + NAND_BLOCK_SIZE - 1) / NAND_BLOCK_SIZE + wadBlocks.fileBlocks + wadBlocks.privateBlocks); if (result != RESULT_OK) { myWork->asyncResult = result; - goto clean_up; - } - - if (wadBlocks.fileInodes == 0) { - if (wadBackupHeader.numFiles != 0) { - OSReport("NandSDWorker: this save data includes only notransfer.\n"); - myWork->asyncResult = RESULT_NOT_TRANSFERRABLE; - goto clean_up; - } - } - if (wadBlocks.fileInodes < wadBackupHeader.numFiles - 1) { - OSReport("NandSDWorker: this save data includes notransfer, %d files/directries not copied.\n", - wadBackupHeader.numFiles - wadBlocks.fileInodes); - hasNotransfer = true; - } - } - - wadErr = WADImportEx(sdSavePath, &myWork->scratchAllocator, WAD_LOCATION_SD_CARD, sizeof(SDSaveBanner), 8, NULL); - if (wadErr != WAD_ERROR_OK) { - OSReport("NandSDWorker: WADRestoreSDEx failed.[%d]\n", wadErr); - if (isDataOnlyTitle) { - if (nand_app_exist_ex(saveBanner->curTitleId) != EXISTENCE_COMPLETE) { - int delNandAppContentRes = delete_nand_app_content(saveBanner->curTitleId); - if (delNandAppContentRes == RESULT_NAND_CORRUPT) { - myWork->asyncResult = RESULT_NAND_CORRUPT; + } else { + if (!isDataOnlyTitle) { + WADBackupHeader wadBackupHeader; + result = get_sd_wad_header(myWork->curTitleId, &wadBackupHeader, 0); + if (result != RESULT_OK) { + myWork->asyncResult = result; goto clean_up; } - if (delNandAppContentRes != RESULT_OK) { - OSReport("NandSDWorker: delete title 0x%016llx content failed.[%d]\n", saveBanner->curTitleId, delNandAppContentRes); + + if (wadBlocks.fileInodes == 0) { + if (wadBackupHeader.numFiles != 0) { + OSReport("NandSDWorker: this save data includes only notransfer.\n"); + myWork->asyncResult = RESULT_NOT_TRANSFERRABLE; + goto clean_up; + } + } + if (wadBlocks.fileInodes < wadBackupHeader.numFiles - 1) { + OSReport("NandSDWorker: this save data includes notransfer, %d files/directries not copied.\n", + wadBackupHeader.numFiles - wadBlocks.fileInodes); + hasNotransfer = true; } } + + wadErr = WADImportEx(sdSavePath, &myWork->scratchAllocator, WAD_LOCATION_SD_CARD, sizeof(SDSaveBanner), 8, NULL); + if (wadErr != WAD_ERROR_OK) { + OSReport("NandSDWorker: WADRestoreSDEx failed.[%d]\n", wadErr); + if (isDataOnlyTitle) { + if (nand_app_exist_ex(saveBanner->curTitleId) != EXISTENCE_COMPLETE) { + int delNandAppContentRes = delete_nand_app_content(saveBanner->curTitleId); + if (delNandAppContentRes == RESULT_NAND_CORRUPT) { + myWork->asyncResult = RESULT_NAND_CORRUPT; + goto clean_up; + } + if (delNandAppContentRes != RESULT_OK) { + OSReport("NandSDWorker: delete title 0x%016llx content failed.[%d]\n", saveBanner->curTitleId, delNandAppContentRes); + } + } + } + myWork->asyncResult = RESULT_BAD_FILE; + goto clean_up; + } + + if (!isDataOnlyTitle) { + strncpy(tmpPath, c_tmp_work_path, NAND_MAX_PATH); + strncat(tmpPath, "/", NAND_MAX_PATH); + strncat(tmpPath, c_banner_file_name, NAND_MAX_PATH); + + s32 nandErr = nand::wrapper::Create(tmpPath, saveBanner->perms, saveBanner->attr); + if (check_nand_corrupt(nandErr, &myWork->asyncResult)) { + goto clean_up; + } + if (nandErr != NAND_RESULT_OK) { + OSReport("NandSDWorker: NANDCreate %s failed [%d]\n", tmpPath, nandErr); + myWork->asyncResult = RESULT_FATAL_SD_ERROR; + goto clean_up; + } + + NANDFileInfo fileInfo; + s32 err = nand::wrapper::Open(tmpPath, &fileInfo, NAND_ACCESS_WRITE); + if (err != NAND_RESULT_OK) { + OSReport("NandSDWorker: NANDOpen %s failed [%d]\n", tmpPath, err); + myWork->asyncResult = RESULT_FATAL_SD_ERROR; + goto clean_up; + } + + nandErr = nand::wrapper::Write(&fileInfo, &saveBanner->banner, saveBanner->headerSize); + if (check_nand_corrupt(nandErr, &myWork->asyncResult)) { + nand::wrapper::Close(&fileInfo); + goto clean_up; + } + if (nandErr < saveBanner->headerSize) { + OSReport("NandSDWorker: NANDWrite %s failed [%d]\n", tmpPath, nandErr); + myWork->asyncResult = RESULT_FATAL_SD_ERROR; + nand::wrapper::Close(&fileInfo); + goto clean_up; + } + + nandErr = nand::wrapper::Close(&fileInfo); + if (check_nand_corrupt(nandErr, &myWork->asyncResult)) + goto clean_up; + if (nandErr != NAND_RESULT_OK) { + OSReport("NandSDWorker: NANDClose %s failed [%d]\n", tmpPath, nandErr); + myWork->asyncResult = RESULT_FATAL_SD_ERROR; + goto clean_up; + } + + result = recursion_nand(c_tmp_work_path, targetPath, RECURSIVE_MOVE, NULL, 0); + if (result != RESULT_OK) { + OSReport("NandSDWorker: failed to move files from %s to %s.\n", c_tmp_work_path, targetPath); + myWork->asyncResult = result; + goto clean_up; + } + OSReport("NandSDWorker: import save data titleid = 0x%016llx.\n", saveBanner->curTitleId); + if (hasNotransfer) { + myWork->asyncResult = 1; + } else { + myWork->asyncResult = 0; + } + } + myWork->nandSaveNum++; } - myWork->asyncResult = RESULT_BAD_FILE; - goto clean_up; } - if (!isDataOnlyTitle) { - strncpy(tmpPath, c_tmp_work_path, NAND_MAX_PATH); - strncat(tmpPath, "/", NAND_MAX_PATH); - strncat(tmpPath, c_banner_file_name, NAND_MAX_PATH); - - s32 nandErr = nand::wrapper::Create(tmpPath, saveBanner->perms, saveBanner->attr); - if (check_nand_corrupt(nandErr, &myWork->asyncResult)) { - goto clean_up; - } - if (nandErr != NAND_RESULT_OK) { - OSReport("NandSDWorker: NANDCreate %s failed [%d]\n", tmpPath, nandErr); - myWork->asyncResult = RESULT_FATAL_SD_ERROR; - goto clean_up; - } - - NANDFileInfo fileInfo; - s32 err = nand::wrapper::Open(tmpPath, &fileInfo, NAND_ACCESS_WRITE); - if (err != NAND_RESULT_OK) { - OSReport("NandSDWorker: NANDOpen %s failed [%d]\n", tmpPath, err); - myWork->asyncResult = RESULT_FATAL_SD_ERROR; - goto clean_up; - } - - nandErr = nand::wrapper::Write(&fileInfo, &saveBanner->banner, saveBanner->headerSize); - if (check_nand_corrupt(nandErr, &myWork->asyncResult)) { - nand::wrapper::Close(&fileInfo); - goto clean_up; - } - if (nandErr < saveBanner->headerSize) { - OSReport("NandSDWorker: NANDWrite %s failed [%d]\n", tmpPath, nandErr); - myWork->asyncResult = RESULT_FATAL_SD_ERROR; - nand::wrapper::Close(&fileInfo); - goto clean_up; - } - - nandErr = nand::wrapper::Close(&fileInfo); - if (check_nand_corrupt(nandErr, &myWork->asyncResult)) - goto clean_up; - if (nandErr != NAND_RESULT_OK) { - OSReport("NandSDWorker: NANDClose %s failed [%d]\n", tmpPath, nandErr); - myWork->asyncResult = RESULT_FATAL_SD_ERROR; - goto clean_up; - } - - result = recursion_nand(c_tmp_work_path, targetPath, RECURSIVE_MOVE, NULL, 0); - if (result != RESULT_OK) { - OSReport("NandSDWorker: failed to move files from %s to %s.\n", c_tmp_work_path, targetPath); - myWork->asyncResult = result; - goto clean_up; - } - OSReport("NandSDWorker: import save data titleid = 0x%016llx.\n", saveBanner->curTitleId); - if (hasNotransfer) { - myWork->asyncResult = 1; - } else { - myWork->asyncResult = 0; - } - } - myWork->nandSaveNum++; clean_up: if (!isDataOnlyTitle) { check_nand_corrupt(nand::wrapper::Delete(c_tmp_work_path), &myWork->asyncResult); @@ -3418,25 +3404,24 @@ namespace ipl { if (handle_sd_error_for_entry(FAFclose(fStream), &ret) != RESULT_OK) { OSReport("NandSDWorker: FAFclose failed.\n"); - goto clean_up; - } - - if (decrypt(encBanner, sizeof(SDSaveBanner), banner) != RESULT_OK) { - ret = RESULT_BAD_FILE; - goto clean_up; - } - NETMD5Sum MD5Sum; - memcpy(MD5Sum, banner->MD5Sum, NET_MD5_DIGEST_SIZE); - memcpy(banner->MD5Sum, &c_md5_secret_key, NET_MD5_DIGEST_SIZE); - - if (!check_md5(MD5Sum, (u8*)banner, sizeof(SDSaveBanner))) { - OSReport("NandSDWorker: banner file %s md5 digest is not correct.\n", sdSavePath); - ret = RESULT_BAD_FILE; - } else if (titleId == (ESTitleId32)ES_TITLE_CODE(banner->curTitleId) && banner->banner.signature == WII_SAVE_BANNER_SIGNATURE) { - ret = RESULT_OK; } else { - OSReport("NandSDWorker: banner file %s signature is not correct\n", sdSavePath); - ret = RESULT_BAD_FILE; + if (decrypt(encBanner, sizeof(SDSaveBanner), banner) != RESULT_OK) { + ret = RESULT_BAD_FILE; + } else { + NETMD5Sum MD5Sum; + memcpy(MD5Sum, banner->MD5Sum, NET_MD5_DIGEST_SIZE); + memcpy(banner->MD5Sum, &c_md5_secret_key, NET_MD5_DIGEST_SIZE); + + if (!check_md5(MD5Sum, (u8*)banner, sizeof(SDSaveBanner))) { + OSReport("NandSDWorker: banner file %s md5 digest is not correct.\n", sdSavePath); + ret = RESULT_BAD_FILE; + } else if (titleId == (ESTitleId32)ES_TITLE_CODE(banner->curTitleId) && banner->banner.signature == WII_SAVE_BANNER_SIGNATURE) { + ret = RESULT_OK; + } else { + OSReport("NandSDWorker: banner file %s signature is not correct\n", sdSavePath); + ret = RESULT_BAD_FILE; + } + } } clean_up: @@ -3461,31 +3446,27 @@ namespace ipl { if (fStream == NULL) { OSReport("NandSDWorker: FAFopen %s failed.\n", path); handle_sd_error_for_entry(FA_ERR_SYSTEM, &ret); - goto ret; - } - - if (handle_sd_error_for_entry(FAFseek(fStream, fileOffset, FA_SEEK_ORIGIN_BEGIN), &ret) != RESULT_OK) { - OSReport("NandSDWorker: FAFseek %s failed.\n", path); - FAFclose(fStream); - goto ret; - } - - if (FAFread(wadBackupHeader, sizeof(WADBackupHeader), 1, fStream) != 1) { - ret = handle_sd_error_for_entry(FA_ERR_SYSTEM, NULL); - if (ret == RESULT_OK) { - ret = RESULT_BAD_FILE; + } else { + if (handle_sd_error_for_entry(FAFseek(fStream, fileOffset, FA_SEEK_ORIGIN_BEGIN), &ret) != RESULT_OK) { + OSReport("NandSDWorker: FAFseek %s failed.\n", path); + FAFclose(fStream); + } else { + if (FAFread(wadBackupHeader, sizeof(WADBackupHeader), 1, fStream) != 1) { + ret = handle_sd_error_for_entry(FA_ERR_SYSTEM, NULL); + if (ret == RESULT_OK) { + ret = RESULT_BAD_FILE; + } + FAFclose(fStream); + } else { + if (handle_sd_error_for_entry(FAFclose(fStream), &ret) != RESULT_OK) { + OSReport("NandSDWorker: FAFclose failed.\n"); + } else { + ret = RESULT_OK; + } + } } - FAFclose(fStream); - goto ret; } - if (handle_sd_error_for_entry(FAFclose(fStream), &ret) != RESULT_OK) { - OSReport("NandSDWorker: FAFclose failed.\n"); - goto ret; - } - - ret = RESULT_OK; - ret: return ret; } @@ -3731,14 +3712,13 @@ namespace ipl { OSReport("NandSDWorker: FAFwrite %s failed.[%d]\n", sdAppPath, wadOffset); handle_sd_error(FA_ERR_SYSTEM, &myWork->asyncResult); FAFclose(fStream); - goto clean_up; - } - - if (handle_sd_error(FAFclose(fStream), &myWork->asyncResult) != RESULT_OK) { - OSReport("NandSDWorker: FAFclose %s failed.\n", sdAppPath); } else { - myWork->sdAppNum++; - myWork->asyncResult = RESULT_OK; + if (handle_sd_error(FAFclose(fStream), &myWork->asyncResult) != RESULT_OK) { + OSReport("NandSDWorker: FAFclose %s failed.\n", sdAppPath); + } else { + myWork->sdAppNum++; + myWork->asyncResult = RESULT_OK; + } } clean_up: @@ -3773,31 +3753,29 @@ namespace ipl { if (contentSize < WII_SAVE_BANNER_NORMAL_SIZE) { OSReport("NandSDWorker: banner file is too small [%d]\n", contentSize); ret = RESULT_BAD_FILE; - goto clean_up; } else if (contentSize > WII_SAVE_BANNER_FULL_SIZE) { OSReport("NandSDWorker: banner file is too large [%d]\n", contentSize); ret = RESULT_BAD_FILE; - goto clean_up; - } + } else { + if (sizeOut != NULL) { + *sizeOut = contentSize; + } - if (sizeOut != NULL) { - *sizeOut = contentSize; - } + ret = read_file(fd, FILEKIND_ES_CONTENT, 0x40, contentSize, fileOut); + if (ret != RESULT_OK) { + OSReport("NandSDWorker: failed to read from content.\n"); + ret = RESULT_FATAL_SD_ERROR; + goto clean_up; + } - ret = read_file(fd, FILEKIND_ES_CONTENT, 0x40, contentSize, fileOut); - if (ret != RESULT_OK) { - OSReport("NandSDWorker: failed to read from content.\n"); - ret = RESULT_FATAL_SD_ERROR; - goto clean_up; + if (fileOut->signature != WII_SAVE_BANNER_SIGNATURE) { + OSReport("NandSDWorker: banner file signature is not correct\n"); + ret = RESULT_BAD_FILE; + } else { + ret = RESULT_OK; + } } - if (fileOut->signature != WII_SAVE_BANNER_SIGNATURE) { - OSReport("NandSDWorker: banner file signature is not correct\n"); - ret = RESULT_BAD_FILE; - goto clean_up; - } - ret = RESULT_OK; - clean_up: ES_CloseContentFile((ESFd)fd); if (ticketView != NULL) { @@ -3914,17 +3892,17 @@ namespace ipl { if (OSRoundUp32B(thumbnailSize) > SD_APP_BACKUP_THUMBNAIL_LENGTH) { OSReport("NandSDWorker: illegal thumbnail size.[%d]\n", thumbnailSize); ret = RESULT_BAD_FILE; - goto clean_up; - } - appBnrOut->head.thumbSize = thumbnailSize; - OSReport("NandSDWorker: thumbnail size = %d\n", thumbnailSize); + } else { + appBnrOut->head.thumbSize = thumbnailSize; + OSReport("NandSDWorker: thumbnail size = %d\n", thumbnailSize); - u32 iconBinOffset; - iconBinOffset = hdrOffset + headerSize + ARCGetStartOffset(&arcFile); + u32 iconBinOffset; + iconBinOffset = hdrOffset + headerSize + ARCGetStartOffset(&arcFile); - ret = read_file(fd, fileKind, iconBinOffset, thumbnailSize, appBnrOut->thumbnail); - if (ret == RESULT_OK) { - ret = RESULT_OK; + ret = read_file(fd, fileKind, iconBinOffset, thumbnailSize, appBnrOut->thumbnail); + if (ret == RESULT_OK) { + ret = RESULT_OK; + } } clean_up: @@ -3971,35 +3949,30 @@ namespace ipl { if (ret != ES_ERR_OK) { OSReport("NandSDWorker: ES_GetTicketViews titleid 0x%016llx failed.\n", titleId); ret = RESULT_FATAL_SD_ERROR; - goto clean_up; - } - - if (ticketViewCount == 0) { + } else if (ticketViewCount == 0) { OSReport("NandSDWorker: titleid 0x%016llx has no ticket.\n", titleId); ret = RESULT_FATAL_SD_ERROR; - goto clean_up; - } - - ESTicketView* ticketViewList; - ticketViewList = (ESTicketView*)MEMAllocFromExpHeapEx(myWork->mainHeap, OSRoundUp32B(ticketViewCount * sizeof(ESTicketView)), 32); - *ticketViewOut = ticketViewList; - ret = ES_GetTicketViews(titleId, ticketViewList, &ticketViewCount); - if (ret != ES_ERR_OK) { - OSReport("NandSDWorker: ES_GetTicketViews titleid 0x%016llx failed.\n", titleId); - ret = RESULT_FATAL_SD_ERROR; - goto clean_up; - } - - s32 err; - err = ES_OpenTitleContentFile(titleId, *ticketViewOut, contentIdx); - if (err < ES_ERR_OK) { - OSReport("NandSDWorker: ES_OpenTitleContentFile titleid 0x%016llx cidx %d failed.[%d]\n", titleId, contentIdx, err); - ret = RESULT_FATAL_SD_ERROR; } else { - ret = err; + ESTicketView* ticketViewList; + ticketViewList = (ESTicketView*)MEMAllocFromExpHeapEx(myWork->mainHeap, OSRoundUp32B(ticketViewCount * sizeof(ESTicketView)), 32); + *ticketViewOut = ticketViewList; + ret = ES_GetTicketViews(titleId, ticketViewList, &ticketViewCount); + if (ret != ES_ERR_OK) { + OSReport("NandSDWorker: ES_GetTicketViews titleid 0x%016llx failed.\n", titleId); + ret = RESULT_FATAL_SD_ERROR; + } else { + s32 err; + err = ES_OpenTitleContentFile(titleId, *ticketViewOut, contentIdx); + if (err < ES_ERR_OK) { + OSReport("NandSDWorker: ES_OpenTitleContentFile titleid 0x%016llx cidx %d failed.[%d]\n", titleId, contentIdx, err); + ret = RESULT_FATAL_SD_ERROR; + } else { + ret = err; - *contentSizeOut = tmdView->contents[contentIdx].size; - OSReport("NandSDWorker: successfully open titleid 0x%016llx cidx %d. fd = %d, size = %d.\n", titleId, contentIdx, ret, *contentSizeOut); + *contentSizeOut = tmdView->contents[contentIdx].size; + OSReport("NandSDWorker: successfully open titleid 0x%016llx cidx %d. fd = %d, size = %d.\n", titleId, contentIdx, ret, *contentSizeOut); + } + } } clean_up: @@ -4197,50 +4170,47 @@ namespace ipl { if (wadErr == WAD_ERROR_INCORRECT_DEVICE) { OSReport("NandSDWorker: WADImportGetBlocks failed, not original Wii.[%d]\n", wadErr); myWork->asyncResult = RESULT_NOT_TRANSFERRABLE; - goto clean_up; } else if (wadErr != WAD_ERROR_OK) { OSReport("NandSDWorker: WADImportGetBlocks failed.[%d]\n", wadErr); myWork->asyncResult = RESULT_BAD_FILE; - goto clean_up; - } - - OSReport("NandSDWorker: shared content inodes/blocks = %d/%d\n", wadBlocks.sharedInodes, wadBlocks.sharedBlocks); - OSReport("NandSDWorker: private content inodes/blocks = %d/%d\n", wadBlocks.privateInodes, wadBlocks.privateBlocks); - OSReport("NandSDWorker: file inodes/blocks = %d/%d\n", wadBlocks.fileInodes, wadBlocks.fileBlocks); - - result = check_nand_free_area(wadBlocks.sharedInodes, wadBlocks.sharedBlocks, wadBlocks.privateInodes + wadBlocks.fileInodes, - wadBlocks.privateBlocks + wadBlocks.fileBlocks); - if (result != RESULT_OK) { - myWork->asyncResult = result; - goto clean_up; - } - - wadErr = WADImportEx(sdAppPath, &myWork->scratchAllocator, WAD_LOCATION_SD_CARD, ROUNDUP(sdAppBanner->thumbSize, 64) + sizeof(SDAppBanner), 8, - wad_backup_progress_callback); - if (wadErr == WAD_ERROR_INCORRECT_DEVICE) { - OSReport("NandSDWorker: WADRestoreSDEx failed, not original Wii.[%d]\n", wadErr); - myWork->asyncResult = RESULT_NOT_TRANSFERRABLE; - } else if (wadErr != WAD_ERROR_OK) { - OSReport("NandSDWorker: WADRestoreSDEx failed.[%d]\n", wadErr); - if (nand_app_exist_ex(sdAppBanner->titleId) == 1) { - result = delete_nand_app_content(sdAppBanner->titleId); - if (result == RESULT_NAND_CORRUPT) { - myWork->asyncResult = RESULT_NAND_CORRUPT; - goto clean_up; - } - if (result != RESULT_OK) { - OSReport("NandSDWorker: delete title 0x%016llx content failed.[%d]\n", sdAppBanner->titleId, result); - } - } - myWork->asyncResult = RESULT_BAD_FILE; } else { - OSReport("NandSDWorker: import app data titleid = 0x%016llx.\n", sdAppBanner->titleId); - myWork->nandAppNum++; - if (!changeAppCount) { - change_nand_app_count(1); - } + OSReport("NandSDWorker: shared content inodes/blocks = %d/%d\n", wadBlocks.sharedInodes, wadBlocks.sharedBlocks); + OSReport("NandSDWorker: private content inodes/blocks = %d/%d\n", wadBlocks.privateInodes, wadBlocks.privateBlocks); + OSReport("NandSDWorker: file inodes/blocks = %d/%d\n", wadBlocks.fileInodes, wadBlocks.fileBlocks); - myWork->asyncResult = RESULT_OK; + result = check_nand_free_area(wadBlocks.sharedInodes, wadBlocks.sharedBlocks, wadBlocks.privateInodes + wadBlocks.fileInodes, + wadBlocks.privateBlocks + wadBlocks.fileBlocks); + if (result != RESULT_OK) { + myWork->asyncResult = result; + } else { + wadErr = WADImportEx(sdAppPath, &myWork->scratchAllocator, WAD_LOCATION_SD_CARD, ROUNDUP(sdAppBanner->thumbSize, 64) + sizeof(SDAppBanner), 8, + wad_backup_progress_callback); + if (wadErr == WAD_ERROR_INCORRECT_DEVICE) { + OSReport("NandSDWorker: WADRestoreSDEx failed, not original Wii.[%d]\n", wadErr); + myWork->asyncResult = RESULT_NOT_TRANSFERRABLE; + } else if (wadErr != WAD_ERROR_OK) { + OSReport("NandSDWorker: WADRestoreSDEx failed.[%d]\n", wadErr); + if (nand_app_exist_ex(sdAppBanner->titleId) == 1) { + result = delete_nand_app_content(sdAppBanner->titleId); + if (result == RESULT_NAND_CORRUPT) { + myWork->asyncResult = RESULT_NAND_CORRUPT; + goto clean_up; + } + if (result != RESULT_OK) { + OSReport("NandSDWorker: delete title 0x%016llx content failed.[%d]\n", sdAppBanner->titleId, result); + } + } + myWork->asyncResult = RESULT_BAD_FILE; + } else { + OSReport("NandSDWorker: import app data titleid = 0x%016llx.\n", sdAppBanner->titleId); + myWork->nandAppNum++; + if (!changeAppCount) { + change_nand_app_count(1); + } + + myWork->asyncResult = RESULT_OK; + } + } } clean_up: @@ -4306,32 +4276,29 @@ namespace ipl { if (decrypt(encBuffer, sizeof(SDAppBanner), appBanner) != RESULT_OK) { ret = RESULT_BAD_FILE; - goto clean_up; - } - - NETMD5Sum MD5Sum; - memcpy(MD5Sum, appBanner->headerMD5, NET_MD5_DIGEST_SIZE); - memcpy(appBanner->headerMD5, &c_md5_secret_key, NET_MD5_DIGEST_SIZE); - if (!check_md5(MD5Sum, (u8*)appBanner, sizeof(SDAppBanner))) { - OSReport("NandSDWorker: banner file %s md5 digest is not correct.\n", appPath); - ret = RESULT_BAD_FILE; - goto clean_up; - } - - if (titleId == (u32)ES_TITLE_CODE(appBanner->titleId)) { - ret = RESULT_OK; } else { - ret = RESULT_BAD_FILE; - } + NETMD5Sum MD5Sum; + memcpy(MD5Sum, appBanner->headerMD5, NET_MD5_DIGEST_SIZE); + memcpy(appBanner->headerMD5, &c_md5_secret_key, NET_MD5_DIGEST_SIZE); + if (!check_md5(MD5Sum, (u8*)appBanner, sizeof(SDAppBanner))) { + OSReport("NandSDWorker: banner file %s md5 digest is not correct.\n", appPath); + ret = RESULT_BAD_FILE; + } else { + if (titleId == (u32)ES_TITLE_CODE(appBanner->titleId)) { + ret = RESULT_OK; + } else { + ret = RESULT_BAD_FILE; + } - FAFileStat fileStat; - memset(&fileStat, 0, sizeof(FAFileStat)); - if (handle_sd_error_for_entry(FAFstat(appPath, &fileStat), &ret) != RESULT_OK) { - // @bug this should say FAFstat, looks like it was copy-pasted from above - OSReport("NandSDWorker: FAFclose failed.\n"); - - } else if (fileStat.size <= ROUNDUP(appBanner->thumbSize, 64) + sizeof(SDAppBanner)) { - ret = RESULT_BAD_FILE; + FAFileStat fileStat; + memset(&fileStat, 0, sizeof(FAFileStat)); + if (handle_sd_error_for_entry(FAFstat(appPath, &fileStat), &ret) != RESULT_OK) { + // @bug this should say FAFstat, looks like it was copy-pasted from above + OSReport("NandSDWorker: FAFclose failed.\n"); + } else if (fileStat.size <= ROUNDUP(appBanner->thumbSize, 64) + sizeof(SDAppBanner)) { + ret = RESULT_BAD_FILE; + } + } } clean_up: @@ -4384,21 +4351,19 @@ namespace ipl { if (decrypt(encBanner, ROUNDUP(appBanner->thumbSize, 64), cmprBuf) != RESULT_OK) { ret = RESULT_BAD_FILE; - goto clean_up; - } - - if (!check_md5(appBanner->iconMD5, cmprBuf, ROUNDUP(appBanner->thumbSize, 64))) { - OSReport("NandSDWorker: banner file %s md5 digest is not correct.\n", appPath); - ret = RESULT_BAD_FILE; - goto clean_up; - } - - ret = uncompress_app_thumbnail(cmprBuf, appBanner->thumbSize, thumbnailBuf); - if (ret < RESULT_OK) { - OSReport("NandSDWorker: cannot uncompress thumbnail of %016llx [%d].\n", appBanner->titleId, ret); - ret = RESULT_BAD_FILE; } else { - OSReport("NandSDWorker: uncompress thumbnail size = %d.\n", ret); + if (!check_md5(appBanner->iconMD5, cmprBuf, ROUNDUP(appBanner->thumbSize, 64))) { + OSReport("NandSDWorker: banner file %s md5 digest is not correct.\n", appPath); + ret = RESULT_BAD_FILE; + } else { + ret = uncompress_app_thumbnail(cmprBuf, appBanner->thumbSize, thumbnailBuf); + if (ret < RESULT_OK) { + OSReport("NandSDWorker: cannot uncompress thumbnail of %016llx [%d].\n", appBanner->titleId, ret); + ret = RESULT_BAD_FILE; + } else { + OSReport("NandSDWorker: uncompress thumbnail size = %d.\n", ret); + } + } } clean_up: @@ -4465,51 +4430,50 @@ namespace ipl { if (esErr != ES_ERR_OK) { OSReport("NandSDWorker: ES_ListTitleContentsOnCard failed.[%d]\n", esErr); ret = EXISTENCE_NOEXIST; - goto clean_up; - } - if (numContents == 0) { - OSReport("NandSDWorker: no content for 0x%016llx.\n", titleId); - ret = EXISTENCE_NOEXIST; - goto clean_up; - } - - esErr = ES_ListTitleContentsOnCard(titleId, contentIds, &numContents); - if (esErr == ES_ERR_OK) { - bool foundNonOptionalMatch = false; - for (u32 i = 0; i < numContents && !foundNonOptionalMatch; i++) { - for (u32 j = 0; j < tmdView->head.numContents; j++) { - if (contentIds[i] == tmdView->contents[j].cid && (tmdView->contents[j].type & 0x8000) == 0) { - foundNonOptionalMatch = true; - break; - } - } - } - if (!foundNonOptionalMatch) { - OSReport("NandSDWorker: 0x%016llx is already deleted.\n", titleId); - ret = EXISTENCE_NOEXIST; - goto clean_up; - } - - ret = EXISTENCE_NOEXIST; - u32 count; - for (u32 i = 0; i < tmdView->head.numContents; i++) { - count = 0; - for (u32 j = 0; j < numContents; j++) { - if (contentIds[j] == tmdView->contents[i].cid) - break; - count++; - } - if (count == numContents && (tmdView->contents[i].type & 0x4000) == 0) { - OSReport("NandSDWorker: not complete: non-optional cidx %d missing for 0x%016llx.\n", tmdView->contents[i].index, titleId); - ret = EXISTENCE_INCOMPLETE; - goto clean_up; - } - } - OSReport("NandSDWorker: complete: only missing non-optional contents for 0x%016llx.\n", titleId); - ret = EXISTENCE_COMPLETE; } else { - OSReport("NandSDWorker: ES_ListTitleContentsOnCard failed.[%d]\n", esErr); - ret = EXISTENCE_NOEXIST; + if (numContents == 0) { + OSReport("NandSDWorker: no content for 0x%016llx.\n", titleId); + ret = EXISTENCE_NOEXIST; + } else { + esErr = ES_ListTitleContentsOnCard(titleId, contentIds, &numContents); + if (esErr == ES_ERR_OK) { + bool foundNonOptionalMatch = false; + for (u32 i = 0; i < numContents && !foundNonOptionalMatch; i++) { + for (u32 j = 0; j < tmdView->head.numContents; j++) { + if (contentIds[i] == tmdView->contents[j].cid && (tmdView->contents[j].type & 0x8000) == 0) { + foundNonOptionalMatch = true; + break; + } + } + } + if (!foundNonOptionalMatch) { + OSReport("NandSDWorker: 0x%016llx is already deleted.\n", titleId); + ret = EXISTENCE_NOEXIST; + goto clean_up; + } + + ret = EXISTENCE_NOEXIST; + u32 count; + for (u32 i = 0; i < tmdView->head.numContents; i++) { + count = 0; + for (u32 j = 0; j < numContents; j++) { + if (contentIds[j] == tmdView->contents[i].cid) + break; + count++; + } + if (count == numContents && (tmdView->contents[i].type & 0x4000) == 0) { + OSReport("NandSDWorker: not complete: non-optional cidx %d missing for 0x%016llx.\n", tmdView->contents[i].index, titleId); + ret = EXISTENCE_INCOMPLETE; + goto clean_up; + } + } + OSReport("NandSDWorker: complete: only missing non-optional contents for 0x%016llx.\n", titleId); + ret = EXISTENCE_COMPLETE; + } else { + OSReport("NandSDWorker: ES_ListTitleContentsOnCard failed.[%d]\n", esErr); + ret = EXISTENCE_NOEXIST; + } + } } clean_up: @@ -5363,51 +5327,34 @@ namespace ipl { fileOut = (WIISaveBannerFile*)MEMAllocFromExpHeapEx(this->myWork->mainHeap, 0xf0a0, 0x20); ret = get_nand_save_banner(titleId, fileOut, NULL); - if (ret != RESULT_OK) { - goto clean_up; - } - - if (is_data_only_title(titleId)) { - ret = 5; - goto clean_up; - } - - if ((fileOut->flags & WII_SAVE_BANNER_FLAG_NOCOPY) != 0) { - ret = 1; - goto clean_up; - } - - if (item_exist_nand_save_folder(titleId, c_nocopy_folder_name)) { - ret = get_num_nand_data_only_title_save_files(titleId, &numTitleFiles); - if (ret != RESULT_OK) { - goto clean_up; - } - - if (numTitleFiles == 2) { + if (ret == RESULT_OK) { + if (is_data_only_title(titleId)) { + ret = 5; + } else if ((fileOut->flags & WII_SAVE_BANNER_FLAG_NOCOPY) != 0) { ret = 1; + } else if (item_exist_nand_save_folder(titleId, c_nocopy_folder_name)) { + ret = get_num_nand_data_only_title_save_files(titleId, &numTitleFiles); + if (ret == RESULT_OK) { + if (numTitleFiles == 2) { + ret = 1; + } else { + ret = 2; + } + } + } else if (item_exist_nand_save_folder(titleId, c_notransfer_folder_name)) { + ret = get_num_nand_data_only_title_save_files(titleId, &numTitleFiles); + if (ret == RESULT_OK) { + if (numTitleFiles == 2) { + ret = 3; + } else { + ret = 4; + } + } } else { - ret = 2; + ret = 0; } - goto clean_up; } - if (item_exist_nand_save_folder(titleId, c_notransfer_folder_name)) { - ret = get_num_nand_data_only_title_save_files(titleId, &numTitleFiles); - if (ret != RESULT_OK) { - goto clean_up; - } - - if (numTitleFiles == 2) { - ret = 3; - } else { - ret = 4; - } - goto clean_up; - } - - ret = 0; - - clean_up: if (fileOut != NULL) { MEMFreeToExpHeap(myWork->mainHeap, fileOut); } @@ -5607,20 +5554,17 @@ namespace ipl { WADBackupHeader wadBakHdr; ESDeviceId deviceId; ret = get_sd_wad_header(titleId, &wadBakHdr, ROUNDUP(sdAppBanner->thumbSize, 64)); - if (ret != RESULT_OK) { - goto clean_up; - } - - ret = ES_GetDeviceId(&deviceId); - if (ret != ES_ERR_OK) { - OSReport("NandSDWorker: ES_GetDeviceId failed: %d\n", ret); - ret = RESULT_FATAL_SD_ERROR; - goto clean_up; - } - - if (wadBakHdr.deviceId != deviceId) { - OSReport("NandSDWorker: device id is different: %d vs %d\n", wadBakHdr.deviceId, deviceId); - ret = RESULT_NOT_TRANSFERRABLE; + if (ret == RESULT_OK) { + ret = ES_GetDeviceId(&deviceId); + if (ret != ES_ERR_OK) { + OSReport("NandSDWorker: ES_GetDeviceId failed: %d\n", ret); + ret = RESULT_FATAL_SD_ERROR; + } else { + if (wadBakHdr.deviceId != deviceId) { + OSReport("NandSDWorker: device id is different: %d vs %d\n", wadBakHdr.deviceId, deviceId); + ret = RESULT_NOT_TRANSFERRABLE; + } + } } clean_up: diff --git a/src/utility/iplESMisc.cpp b/src/utility/iplESMisc.cpp index 51989485..b6f58d61 100644 --- a/src/utility/iplESMisc.cpp +++ b/src/utility/iplESMisc.cpp @@ -38,21 +38,19 @@ namespace ipl { if (ret < ES_ERR_OK) { OSReport("ESMisc::GetTmdView: ES_GetTmdView1 err %d\n", ret); *outTmdView = NULL; - goto out; + } else { + tmdView = (ESTmdView*)heap->alloc(OSRoundUp32B(tmdViewSize), -DEFAULT_ALIGN); + *outTmdView = tmdView; + memset(tmdView, 0, OSRoundUp32B(tmdViewSize)); + + ret = ES_GetTmdView(titleId, *outTmdView, &tmdViewSize); + if (ret < ES_ERR_OK) { + OSReport("ESMisc::GetTmdView: ES_GetTmdView2 err %d\n", ret); + heap->free(*outTmdView); + *outTmdView = NULL; + } } - tmdView = (ESTmdView*)heap->alloc(OSRoundUp32B(tmdViewSize), -DEFAULT_ALIGN); - *outTmdView = tmdView; - memset(tmdView, 0, OSRoundUp32B(tmdViewSize)); - - ret = ES_GetTmdView(titleId, *outTmdView, &tmdViewSize); - if (ret < ES_ERR_OK) { - OSReport("ESMisc::GetTmdView: ES_GetTmdView2 err %d\n", ret); - heap->free(*outTmdView); - *outTmdView = NULL; - } - - out: return ret; } @@ -287,63 +285,59 @@ namespace ipl { ticketView = (ESTicketView*)heap->alloc(0xe0, -DEFAULT_ALIGN); - if (ESP_InitLib() < ES_ERR_OK) { - goto error; - } + if (ESP_InitLib() >= ES_ERR_OK) { + index = __OSGetValidTicketIndex(ticket, ticketLength); - index = __OSGetValidTicketIndex(ticket, ticketLength); + if (index >= -1) { + if (index == -1) { + index = 0; + } - if (index < -1) { - goto error; - } + memcpy(ticketView, &ticket[index], sizeof(ESTicketView)); - if (index == -1) { - index = 0; - } + { + ESFd fd = ES_OpenTitleContentFile(titleId, ticketView, 0); - memcpy(ticketView, &ticket[index], sizeof(ESTicketView)); - - { - ESFd fd = ES_OpenTitleContentFile(titleId, ticketView, 0); - - if (fd < 0) { - OSReport("ESMisc::GetValidTicketIndex: ES_OpenTitleContentFile fd %d\n", fd); - } else { - char* buf = (char*)heap->alloc(0x40, -DEFAULT_ALIGN); - u32 numRead = ES_ReadContentFile(fd, buf, 0x40); - - if (numRead != 0x40) { - OSReport("ESMisc::GetValidTicketIndex: ES_ReadContentFile err %d\n", numRead); - heap->free(buf); - - if (ES_CloseContentFile(fd) < ES_ERR_OK) { - goto error; - } - } else { - if (ES_CloseContentFile(fd) < ES_ERR_OK) { - heap->free(buf); - goto error; - } - - if (buf[0] == 0) { - OSReport("ESMisc::GetValidTicketIndex: No name\n"); - heap->free(buf); + if (fd < 0) { + OSReport("ESMisc::GetValidTicketIndex: ES_OpenTitleContentFile fd %d\n", fd); } else { - heap->free(buf); - found = true; + char* buf = (char*)heap->alloc(0x40, -DEFAULT_ALIGN); + u32 numRead = ES_ReadContentFile(fd, buf, 0x40); + + if (numRead != 0x40) { + OSReport("ESMisc::GetValidTicketIndex: ES_ReadContentFile err %d\n", numRead); + heap->free(buf); + + if (ES_CloseContentFile(fd) < ES_ERR_OK) { + goto error; + } + } else { + if (ES_CloseContentFile(fd) < ES_ERR_OK) { + heap->free(buf); + goto error; + } + + if (buf[0] == 0) { + OSReport("ESMisc::GetValidTicketIndex: No name\n"); + heap->free(buf); + } else { + heap->free(buf); + found = true; + } + } } } + + heap->free(ticketView); + + if (allocated && ticket != NULL) { + heap->free(ticket); + } + + return found ? index : -1; } } - heap->free(ticketView); - - if (allocated && ticket != NULL) { - heap->free(ticket); - } - - return found ? index : -1; - error: heap->free(ticketView); @@ -405,66 +399,65 @@ namespace ipl { ret = ES_ListTitlesOnCard(NULL, &numTitles); if (ret < ES_ERR_OK) { OSReport("DeleteSharedContent: ES_ListTitlesOnCard1 err %d\n", ret); - goto out; - } + } else { + if (numTitles != 0) { + titleIds = (ESTitleId*)heap->alloc(OSRoundUp32B(numTitles * sizeof(ESTitleId)), -DEFAULT_ALIGN); + if (titleIds == NULL) { + OSReport("DeleteSharedContent: MEMAllocate for Title List err\n"); + ret = ES_ERR_MEMORY_ERROR; + goto out; + } - if (numTitles != 0) { - titleIds = (ESTitleId*)heap->alloc(OSRoundUp32B(numTitles * sizeof(ESTitleId)), -DEFAULT_ALIGN); - if (titleIds == NULL) { - OSReport("DeleteSharedContent: MEMAllocate for Title List err\n"); - ret = ES_ERR_MEMORY_ERROR; - goto out; - } - - ret = ES_ListTitlesOnCard(titleIds, &numTitles); - if (ret < ES_ERR_OK) { - OSReport("DeleteSharedContent: ES_ListTitlesOnCard2 err %d\n", ret); - goto out; - } - - for (u32 i = 0; i < numTitles; i++) { - u32 tmdSize = 0; - - ret = ES_GetTmd(titleIds[i], NULL, &tmdSize); + ret = ES_ListTitlesOnCard(titleIds, &numTitles); if (ret < ES_ERR_OK) { - OSReport("DeleteSharedContent: ES_GetTmd1 err %d\n", ret); + OSReport("DeleteSharedContent: ES_ListTitlesOnCard2 err %d\n", ret); goto out; } - if (tmdSize == 0 || tmdSize > sizeof(ESTitleMeta)) { - ret = ES_ERR_INVALID; - goto out; - } + for (u32 i = 0; i < numTitles; i++) { + u32 tmdSize = 0; - ret = ES_GetTmd(titleIds[i], tmd, &tmdSize); - if (ret < ES_ERR_OK) { - OSReport("DeleteSharedContent: ES_GetTmd2 err %d\n", ret); - goto out; - } - - for (u16 c = 0; c < tmd->head.numContents; c++) { - if ((tmd->contents[c].type & 0x8000) == 0) { - continue; + ret = ES_GetTmd(titleIds[i], NULL, &tmdSize); + if (ret < ES_ERR_OK) { + OSReport("DeleteSharedContent: ES_GetTmd1 err %d\n", ret); + goto out; } - for (u32 h = 0; h < numShared; h++) { - if (memcmp(tmd->contents[c].hash, hashes[h], sizeof(ESHash)) == 0) { - used[h] = 1; - break; + if (tmdSize == 0 || tmdSize > sizeof(ESTitleMeta)) { + ret = ES_ERR_INVALID; + goto out; + } + + ret = ES_GetTmd(titleIds[i], tmd, &tmdSize); + if (ret < ES_ERR_OK) { + OSReport("DeleteSharedContent: ES_GetTmd2 err %d\n", ret); + goto out; + } + + for (u16 c = 0; c < tmd->head.numContents; c++) { + if ((tmd->contents[c].type & 0x8000) == 0) { + continue; + } + + for (u32 h = 0; h < numShared; h++) { + if (memcmp(tmd->contents[c].hash, hashes[h], sizeof(ESHash)) == 0) { + used[h] = 1; + break; + } } } } } - } - for (u32 h = 0; h < numShared; h++) { - if (used[h] != 0) { - continue; - } + for (u32 h = 0; h < numShared; h++) { + if (used[h] != 0) { + continue; + } - ret = ES_DeleteSharedContent(hashes[h]); - if (ret < ES_ERR_OK) { - OSReport("DeleteSharedContent: ES_DeleteSharedContent err %d\n", ret); + ret = ES_DeleteSharedContent(hashes[h]); + if (ret < ES_ERR_OK) { + OSReport("DeleteSharedContent: ES_DeleteSharedContent err %d\n", ret); + } } } @@ -584,48 +577,43 @@ namespace ipl { if (ret < ES_ERR_OK) { ES_ERR_REPORT("ES_GetTmdView1 failed: %d", ret); - goto out; - } - - ret = ES_ListTitleContentsOnCard(titleId, NULL, &numContents); - if (ret != ES_ERR_OK) { - ES_ERR_REPORT("ES_ListTitleContentsOnCard1 failed: %d for %016llx", ret, titleId); - goto out; - } - - if (numContents == 0) { - return ES_ERR_OK; - } - - tmdView = (ESTmdView*)heap->alloc(OSRoundUp32B(tmdViewSize), -DEFAULT_ALIGN); - contentIds = (ESContentId*)heap->alloc(OSRoundUp32B(numContents * sizeof(ESContentId)), -DEFAULT_ALIGN); - - ret = ES_ListTitleContentsOnCard(titleId, contentIds, &numContents); - if (ret != ES_ERR_OK) { - ES_ERR_REPORT("ES_ListTitleContentsOnCard2 failed: %d for %016llx", ret, titleId); - goto out; - } - - ret = ES_GetTmdView(titleId, tmdView, &tmdViewSize); - if (ret < ES_ERR_OK) { - ES_ERR_REPORT("ES_GetTmdView2 failed: %d", ret); - goto out; - } - - ret = 0; - u32 i = 0; - u32 j = 0; - ESCmdView* content = tmdView->contents; - for (; i < tmdView->head.numContents && j < numContents; i++, content++) { - if (content->cid == contentIds[j]) { - if (!(content->type & 0x8000)) { - ret++; + } else { + ret = ES_ListTitleContentsOnCard(titleId, NULL, &numContents); + if (ret != ES_ERR_OK) { + ES_ERR_REPORT("ES_ListTitleContentsOnCard1 failed: %d for %016llx", ret, titleId); + } else { + if (numContents == 0) { + return ES_ERR_OK; + } + + tmdView = (ESTmdView*)heap->alloc(OSRoundUp32B(tmdViewSize), -DEFAULT_ALIGN); + contentIds = (ESContentId*)heap->alloc(OSRoundUp32B(numContents * sizeof(ESContentId)), -DEFAULT_ALIGN); + + ret = ES_ListTitleContentsOnCard(titleId, contentIds, &numContents); + if (ret != ES_ERR_OK) { + ES_ERR_REPORT("ES_ListTitleContentsOnCard2 failed: %d for %016llx", ret, titleId); + } else { + ret = ES_GetTmdView(titleId, tmdView, &tmdViewSize); + if (ret < ES_ERR_OK) { + ES_ERR_REPORT("ES_GetTmdView2 failed: %d", ret); + } else { + ret = 0; + u32 i = 0; + u32 j = 0; + ESCmdView* content = tmdView->contents; + for (; i < tmdView->head.numContents && j < numContents; i++, content++) { + if (content->cid == contentIds[j]) { + if (!(content->type & 0x8000)) { + ret++; + } + j++; + } + } + } } - j++; } } - out: if (contentIds != NULL) { heap->free(contentIds); } @@ -946,35 +934,31 @@ namespace ipl { if (ret != ES_ERR_OK || entryCount == 0) { ES_ERR_REPORT("Could not read1 %s: %d", dirPath, ret); - goto cleanup; - } - - entries = (char*)heap->alloc(OSRoundUp32B(entryCount * (NAND_MAX_PATH + 1)), -DEFAULT_ALIGN); - if (entries == NULL) { - ret = -2; - ES_ERR_REPORT("Could not alloc: %d", -2); - goto cleanup; - } - - ret = NANDReadDir(dirPath, entries, &entryCount); - if (ret != ES_ERR_OK) { - ES_ERR_REPORT("Could not read2 %s: %d", dirPath, ret); - goto cleanup; - } - - entry = entries; - for (i = 0; i < entryCount;) { - snprintf(filePath, NAND_MAX_PATH, "%s%s", dirPath, entry); - filePath[NAND_MAX_PATH] = 0; - ret = NANDPrivateDelete(filePath); - if (ret != ES_ERR_OK) { - ES_ERR_REPORT("Failed to delete %s: %d", filePath, ret); + } else { + entries = (char*)heap->alloc(OSRoundUp32B(entryCount * (NAND_MAX_PATH + 1)), -DEFAULT_ALIGN); + if (entries == NULL) { + ret = -2; + ES_ERR_REPORT("Could not alloc: %d", -2); + } else { + ret = NANDReadDir(dirPath, entries, &entryCount); + if (ret != ES_ERR_OK) { + ES_ERR_REPORT("Could not read2 %s: %d", dirPath, ret); + } else { + entry = entries; + for (i = 0; i < entryCount;) { + snprintf(filePath, NAND_MAX_PATH, "%s%s", dirPath, entry); + filePath[NAND_MAX_PATH] = 0; + ret = NANDPrivateDelete(filePath); + if (ret != ES_ERR_OK) { + ES_ERR_REPORT("Failed to delete %s: %d", filePath, ret); + } + i++; + entry += strlen(entry) + 1; + } + } } - i++; - entry += strlen(entry) + 1; } - cleanup: if (entries != NULL) { heap->free(entries); } @@ -1020,65 +1004,65 @@ namespace ipl { 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 { - offset = 8; - valid = FALSE; - j = 0; - while (j < 3) { - firstBlock = saveData + offset; - if (!checkForNullTermination((char*)firstBlock + 0x4e, 8) || - !checkForNullTermination((char*)firstBlock + 0x58, 8) || - !checkForNullTermination((char*)firstBlock + 0x72, 8) || - !checkForNullTermination((char*)firstBlock + 0x8e, 8) || - !checkForNullTermination((char*)firstBlock + 0x1b4, 0x11) || - !checkForNullTermination((char*)firstBlock + 0x1c5, 0x11)) { - goto verify_failed; - } - j++; - offset += 0xa94; - } - - offset = 0x2008; - j = 0; - while (j < 3) { - secondBlock = saveData + offset; - if (!checkForNullTermination((char*)secondBlock + 0x4e, 8) || - !checkForNullTermination((char*)secondBlock + 0x58, 8) || - !checkForNullTermination((char*)secondBlock + 0x72, 8) || - !checkForNullTermination((char*)secondBlock + 0x8e, 8) || - !checkForNullTermination((char*)secondBlock + 0x1b4, 0x11) || - !checkForNullTermination((char*)secondBlock + 0x1c5, 0x11)) { - goto verify_failed; - } - j++; - offset += 0xa94; - } - valid = TRUE; - - // MWCC needs both verification loops to share this failure label. - verify_failed: - - if (!valid) { - OSReport("%s::%s: Verify failed for %016llx\n", __FILE__, __FUNCTION__, - titleId); + if (ret != 0x4000) { + OSReport("%s::%s: File size is not correct: %d\n", __FILE__, __FUNCTION__, ret); deleteSaveData = TRUE; - } - } + } else { + offset = 8; + valid = FALSE; + j = 0; + while (j < 3) { + firstBlock = saveData + offset; + if (!checkForNullTermination((char*)firstBlock + 0x4e, 8) || + !checkForNullTermination((char*)firstBlock + 0x58, 8) || + !checkForNullTermination((char*)firstBlock + 0x72, 8) || + !checkForNullTermination((char*)firstBlock + 0x8e, 8) || + !checkForNullTermination((char*)firstBlock + 0x1b4, 0x11) || + !checkForNullTermination((char*)firstBlock + 0x1c5, 0x11)) { + goto verify_failed; + } + j++; + offset += 0xa94; + } - NANDClose(fileInfo); - fileOpen = FALSE; - if (deleteSaveData) { - ESMisc::DeleteSavedata(titleId, heap); + offset = 0x2008; + j = 0; + while (j < 3) { + secondBlock = saveData + offset; + if (!checkForNullTermination((char*)secondBlock + 0x4e, 8) || + !checkForNullTermination((char*)secondBlock + 0x58, 8) || + !checkForNullTermination((char*)secondBlock + 0x72, 8) || + !checkForNullTermination((char*)secondBlock + 0x8e, 8) || + !checkForNullTermination((char*)secondBlock + 0x1b4, 0x11) || + !checkForNullTermination((char*)secondBlock + 0x1c5, 0x11)) { + goto verify_failed; + } + j++; + offset += 0xa94; + } + valid = TRUE; + + // MWCC needs direct failure branches; loop breaks add completion tests. + 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); } @@ -1098,26 +1082,26 @@ namespace ipl { 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); - goto cleanup; - } - if (*ticketViewCount == 0) { - return ret; - } - 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); - goto cleanup; - } - for (j = 0; j < *ticketViewCount; j++) { - memcpy(ticketViews, &ticketViewList[j], sizeof(ESTicketView)); - ret = ES_DeleteTicket((ESTicketView*)ticketViews); + } else { + if (*ticketViewCount == 0) { + return ret; + } + 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_DeleteTicket failed: %d for %016llx\n", __FILE__, __FUNCTION__, ret, - ((ESTicketView*)ticketViews)->ticketId); + OSReport("%s::%s: ES_GetTicketViews failed: %d for %016llx\n", __FILE__, __FUNCTION__, ret, titleId); + } else { + for (j = 0; j < *ticketViewCount; j++) { + memcpy(ticketViews, &ticketViewList[j], 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); + } + } } } - cleanup: + if (ticketViewList != NULL) { heap->free(ticketViewList); } @@ -1138,42 +1122,38 @@ namespace ipl { 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__, __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; - } - - { - ticketScratch = ticketViews; - for (i = 0; i < titleCount; i++) { - if ((titleIds[i] & 0xFFFFFFFFFFFFFF00ULL) == 0x00010000525A4400ULL) { - verifySavedataZD(heap, titleIds[i], &fileInfo); - continue; - } - switch (titleIds[i]) { - case 0x0001000844495343ULL: - case 0x000100014A4F4449ULL: - case 0x0001000148415858ULL: - case 0x0001000844564458ULL: - case 0x000100084449534BULL: - ES_DeleteTitle(titleIds[i]); - DeleteTicketsForce(heap, titleIds[i], ticketScratch, &ticketViewCount); - break; + } else { + titleIds = (ESTitleId*)heap->alloc(OSRoundUp32B(titleCount * sizeof(ESTitleId)), -DEFAULT_ALIGN); + if (titleIds == NULL) { + OSReport("%s::%s: Unable to allocate\n", __FILE__, __FUNCTION__); + } else { + ret = ES_ListTitlesOnCard(titleIds, &titleCount); + if (ret != ES_ERR_OK) { + OSReport("%s::%s: Failed to ES_ListTitlesOnCard2: %d\n", __FILE__, __FUNCTION__, ret); + } else { + { + ticketScratch = ticketViews; + for (i = 0; i < titleCount; i++) { + if ((titleIds[i] & 0xFFFFFFFFFFFFFF00ULL) == 0x00010000525A4400ULL) { + verifySavedataZD(heap, titleIds[i], &fileInfo); + continue; + } + switch (titleIds[i]) { + case 0x0001000844495343ULL: + case 0x000100014A4F4449ULL: + case 0x0001000148415858ULL: + case 0x0001000844564458ULL: + case 0x000100084449534BULL: + ES_DeleteTitle(titleIds[i]); + DeleteTicketsForce(heap, titleIds[i], ticketScratch, &ticketViewCount); + break; + } + } + } } } } - cleanup: if (titleIds != NULL) { heap->free(titleIds); } @@ -1342,12 +1322,11 @@ namespace ipl { ret = NANDWrite(&mFile, entry, entryLen); if (ret != entryLen) { ES_ERR_REPORT("NANDWrite err: %d!=%d", ret, entryLen); - goto out; + } else { + // We are done!!! + ret = NAND_RESULT_OK; + mFileLength += entryLen; } - - // We are done!!! - ret = NAND_RESULT_OK; - mFileLength += entryLen; } out: if (entry != NULL) { @@ -1396,18 +1375,17 @@ namespace ipl { ret = NANDRead(&mFile, tmdBuffer, fileLen); if (ret != fileLen) { ES_ERR_REPORT("NANDRead err: %d!=%d", ret, fileLen); - goto out; - } + } else { + ret = ES_ImportTitleInit(tmdBuffer, tmdSize, NULL, 0, NULL, 0, 2, 1); + if (ret != ES_ERR_OK) { + ES_ERR_REPORT("ES_ImportTitleInit err: %d", ret); + ES_ImportTitleCancel(); + } - ret = ES_ImportTitleInit(tmdBuffer, tmdSize, NULL, 0, NULL, 0, 2, 1); - if (ret != ES_ERR_OK) { - ES_ERR_REPORT("ES_ImportTitleInit err: %d", ret); - ES_ImportTitleCancel(); - } - - ret = ES_ImportTitleDone(); - if (ret != ES_ERR_OK) { - ES_ERR_REPORT("ES_ImportTitleDone err: %d", ret); + ret = ES_ImportTitleDone(); + if (ret != ES_ERR_OK) { + ES_ERR_REPORT("ES_ImportTitleDone err: %d", ret); + } } } out: diff --git a/tools/decomp-assist/cleanup2-w2.attempts.md b/tools/decomp-assist/cleanup2-w2.attempts.md index 27a0013a..cd731f2a 100644 --- a/tools/decomp-assist/cleanup2-w2.attempts.md +++ b/tools/decomp-assist/cleanup2-w2.attempts.md @@ -103,3 +103,231 @@ The retry-loop rewrite in ESMisc also renumbers compiler-generated `__FUNCTION__ - `483f492f`: NandSDWorker cleanup only. - `6daf7896`: ESMisc cleanup only. + +## Round b, MAX effort + +- Worktree `data-d3`, branch `agent/w1009/cleanup2-w2b`, baseline `8e47db4a`, containing round a as PR #1353. Re-read cleanup-common.md, this log, unslop, and writing-for-agents. +- Fresh 43U build and completion checker passed. DOL SHA1 `26116613f624061ba99c8d1a299aaa6efa85670d`. Section-aware baseline verification is 203/203 NandSDWorker and 31/31 ESMisc exact functions. +- Inventory: 197 NandSDWorker gotos in 25 functions; 43 ESMisc gotos in 11 functions. Every remaining NandSDWorker target is a common cleanup or return label, not a state-machine label. ESMisc also has the Zelda verification failure label. +- Do not repeat round-a pragma, weak-emitter, buffer, boolean-conversion, or inline-verification-helper experiments. Test structured error chains, existing loop exits, and return lifetime with assembly evidence per function. + +### Structured control-flow trials + +- `NandSDWorker::do_startup`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::get_nand_save_banner`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::do_check_for_sd_app_to_nand`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::check_backup_fits`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::delete_nand_disk_app_with_ticket`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::delete_nand_titles`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::do_copy_nand_save_to_sd`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::do_copy_sd_save_to_nand`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::get_sd_save_banner`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::get_sd_wad_header`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::do_copy_nand_app_to_sd`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::get_save_banner_for_data_only_title`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::get_app_banner_from_meta`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::open_nand_app_content`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::do_copy_sd_app_to_nand`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::get_sd_app_banner`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::get_sd_app_thumbnail`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::nand_app_exist_ex`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::get_nand_save_perms`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::check_sd_title_restorable`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `ESMisc::GetTmdView`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `ESMisc::GetValidTicketIndex`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `ESMisc::DeleteSharedContent`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `ESMisc::NumPrivateContents`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `ESMisc::DeleteSavedata`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `DeleteTicketsForce`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `InitSavedata`: replace the last error-exit jump with a success else. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::do_startup`: express the save-cache requirement as a positive guard. kept; all allocated sections, relocations, and named symbols unchanged. +- `ESMisc::GetValidTicketIndex`: express the valid ticket index as a positive guard. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::do_startup`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - do_startup__Q23ipl12NandSDWorkerFv: 323 -> 480 instructions; #32 `bne 0x98` -> `bne 0xc4`; #33 `lwz r3, 0(r28)` -> `lwz r4, 0(r28)`; #35 `addis r3, r3, 4` -> `mr r3, r28`. +- `NandSDWorker::do_check_for_sd_app_to_nand`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - do_check_for_sd_app_to_nand__Q23ipl12NandSDWorkerFv: 242 -> 292 instructions; #29 `beq 0x98` -> `beq 0xac`; #35 `addis r3, r3, 4` -> `mr r4, r28`; #36 `stw r30, -0x15dc(r3)` -> `addis r3, r3, 4`. +- `NandSDWorker::check_backup_fits`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - check_backup_fits__Q23ipl12NandSDWorkerFv: 246 -> 291 instructions; #36 `b 0x3ac` -> `b 0x460`; #41 `b 0x188` -> `b 0x1c4`; #44 `beq 0xc8` -> `beq 0xdc`. +- `NandSDWorker::do_list_nand_apps_usage`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - do_list_nand_apps_usage__Q23ipl12NandSDWorkerFv: 114 -> 119 instructions; #38 `b 0x180` -> `b 0x194`; #40 `bge 0x18c` -> `bge 0x1a0`; #46 `beq 0x178` -> `beq 0x18c`. +- `NandSDWorker::delete_nand_disk_app_with_ticket`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - delete_nand_disk_app_with_ticket__Q23ipl12NandSDWorkerFv: 108 -> 107 instructions; #5 `li r27, 0` -> `li r30, 0`; #7 `stw r27, 8(r1)` -> `stw r30, 8(r1)`; #19 `li r28, -2` -> `li r3, -2`. +- `NandSDWorker::delete_nand_titles`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - delete_nand_titles__Q23ipl12NandSDWorkerFv: 146 -> 168 instructions; #5 `li r22, 0` -> `li r25, 0`; #6 `lis r25, 0` -> `lis r24, 0`; #7 `stw r22, 8(r1)` -> `stw r25, 8(r1)`. +- `NandSDWorker::do_copy_nand_save_to_sd`: use early returns with the existing cleanup at each exit. reverted; changed ('.data', 0), ('.text', 0), rel('.rodata', 0), rel('.text', 0), symbols. + - change_nand_app_count__Q23ipl12NandSDWorkerFl: 47 -> 47 instructions; #16 `addi r3, r31, 0x26af` -> `addi r3, r31, 0x2684`; #21 `addi r3, r31, 0x26e4` -> `addi r3, r31, 0x26b9`; #32 `addi r3, r31, 0x2716` -> `addi r3, r31, 0x26eb`. + - check_sd_title_restorable__Q23ipl12NandSDWorkerFUl: 97 -> 97 instructions; #51 `addi r3, r31, 0x2ec5` -> `addi r3, r31, 0x2e9a`; #72 `addi r3, r31, 0x2f00` -> `addi r3, r31, 0x2ed5`; #81 `addi r3, r31, 0x2f29` -> `addi r3, r31, 0x2efe`. +- `NandSDWorker::do_copy_sd_save_to_nand`: use early returns with the existing cleanup at each exit. reverted; changed ('.data', 0), ('.text', 0), rel('.text', 0), symbols. + - change_nand_app_count__Q23ipl12NandSDWorkerFl: 47 -> 47 instructions; #16 `addi r3, r31, 0x26af` -> `addi r3, r31, 0x261f`; #21 `addi r3, r31, 0x26e4` -> `addi r3, r31, 0x2654`; #32 `addi r3, r31, 0x2716` -> `addi r3, r31, 0x2686`. + - check_sd_title_restorable__Q23ipl12NandSDWorkerFUl: 97 -> 97 instructions; #51 `addi r3, r31, 0x2ec5` -> `addi r3, r31, 0x2e35`; #72 `addi r3, r31, 0x2f00` -> `addi r3, r31, 0x2e70`; #81 `addi r3, r31, 0x2f29` -> `addi r3, r31, 0x2e99`. +- `NandSDWorker::get_sd_save_banner`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - get_sd_save_banner__Q23ipl12NandSDWorkerFUlPQ33ipl12NandSDWorker12SDSaveBanner: 142 -> 166 instructions; #6 `mr r31, r3` -> `mr r30, r3`; #7 `lis r29, 0` -> `lis r28, 0`; #10 `lis r30, 1` -> `lis r29, 1`. +- `NandSDWorker::delete_download_task`: use early returns with the existing cleanup at each exit. reverted; changed ('.data', 0), ('.text', 0), rel('.text', 0), symbols. + - delete_download_task__Q23ipl12NandSDWorkerFUl: 133 -> 143 instructions; #6 `mr r25, r3` -> `mr r26, r3`; #8 `mr r26, r4` -> `mr r27, r4`; #10 `li r27, 0` -> `li r4, 0x4000`. +- `NandSDWorker::do_copy_nand_app_to_sd`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - do_copy_nand_app_to_sd__Q23ipl12NandSDWorkerFv: 332 -> 481 instructions; #6 `mr r23, r3` -> `mr r25, r3`; #15 `lwz r4, 0(r23)` -> `lwz r4, 0(r25)`; #18 `li r26, 0` -> `addis r4, r4, 4`. +- `NandSDWorker::get_save_banner_for_data_only_title`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - get_save_banner_for_data_only_title__Q23ipl12NandSDWorkerFUxP17WIISaveBannerFilePUl: 88 -> 115 instructions; #26 `b 0x148` -> `b 0x1b4`; #32 `bge 0x98` -> `bge 0xbc`; #36 `li r31, -3` -> `mr r3, r30`. +- `NandSDWorker::get_app_banner_from_meta`: use early returns with the existing cleanup at each exit. reverted; changed ('.data', 0), ('.text', 0), rel('.text', 0), symbols. + - change_nand_app_count__Q23ipl12NandSDWorkerFl: 47 -> 47 instructions; #16 `addi r3, r31, 0x26af` -> `addi r3, r31, 0x2688`; #21 `addi r3, r31, 0x26e4` -> `addi r3, r31, 0x26bd`; #32 `addi r3, r31, 0x2716` -> `addi r3, r31, 0x26ef`. + - check_sd_title_restorable__Q23ipl12NandSDWorkerFUl: 97 -> 97 instructions; #51 `addi r3, r31, 0x2ec5` -> `addi r3, r31, 0x2e9e`; #72 `addi r3, r31, 0x2f00` -> `addi r3, r31, 0x2ed9`; #81 `addi r3, r31, 0x2f29` -> `addi r3, r31, 0x2f02`. +- `NandSDWorker::open_nand_app_content`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - open_nand_app_content__Q23ipl12NandSDWorkerFUxUsPUlPP12ESTicketView: 135 -> 203 instructions; #6 `lis r30, 0` -> `lis r29, 0`; #9 `mr r31, r3` -> `mr r30, r3`; #14 `mr r28, r9` -> `mr r31, r9`. +- `NandSDWorker::do_copy_sd_app_to_nand`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - do_copy_sd_app_to_nand__Q23ipl12NandSDWorkerFb: 300 -> 370 instructions; #19 `bne 0xa8` -> `bne 0xc4`; #23 `beq 0x9c` -> `beq 0xb8`; #30 `bne 0xa8` -> `bne 0xc4`. +- `NandSDWorker::get_sd_app_banner`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - get_sd_app_banner__Q23ipl12NandSDWorkerFUlPQ33ipl12NandSDWorker11SDAppBanner: 157 -> 187 instructions; #20 `bne 0x6c` -> `bne 0x84`; #25 `stw r0, 8(r1)` -> `mr r4, r31`; #26 `b 0x244` -> `stw r0, 8(r1)`. +- `NandSDWorker::get_sd_app_thumbnail`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - get_sd_app_thumbnail__Q23ipl12NandSDWorkerFPCQ33ipl12NandSDWorker11SDAppBannerPUc: 172 -> 236 instructions; #13 `li r30, 0` -> `bl 0x34`; #14 `li r29, 0` -> `lwz r5, 8(r27)`; #15 `bl 0x3c` -> `cmplwi r5, 0xc800`. +- `NandSDWorker::nand_app_exist_ex`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - nand_app_exist_ex__Q23ipl12NandSDWorkerFUx: 226 -> 274 instructions; #29 `beq 0x98` -> `beq 0xb4`; #31 `beq 0x98` -> `beq 0xb4`; #36 `li r26, 0` -> `lwz r4, 0x2c(r1)`. +- `NandSDWorker::recursion_nand`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - recursion_nand__Q23ipl12NandSDWorkerFPCcPCcQ33ipl12NandSDWorker16RecursiveProcessPPCcUl: 318 -> 479 instructions; #83 `b 0x484` -> `b 0x708`; #89 `beq 0x188` -> `beq 0x1d0`; #96 `stw r0, 0xc(r1)` -> `lwz r4, 0x1c(r1)`. +- `NandSDWorker::get_nand_save_perms`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - get_nand_save_perms__Q23ipl12NandSDWorkerFUx: 96 -> 144 instructions; #6 `mr r27, r3` -> `mr r30, r3`; #8 `mr r29, r5` -> `mr r28, r5`; #10 `mr r28, r6` -> `mr r27, r6`. +- `NandSDWorker::check_sd_title_restorable`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - check_sd_title_restorable__Q23ipl12NandSDWorkerFUl: 97 -> 130 instructions; #15 `mr r30, r3` -> `mr r29, r3`; #19 `mr r5, r30` -> `mr r5, r29`; #24 `bne 0x154` -> `beq 0x80`. +- `ESMisc::DeleteSharedContent`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - DeleteSharedContent__Q33ipl7utility6ESMiscFPQ23EGG4Heap: 261 -> 477 instructions; #5 `li r28, 0` -> `li r25, 0`; #7 `stw r28, 0x10(r1)` -> `stw r25, 0x10(r1)`; #10 `li r27, 0` -> `li r4, 0x4a00`. +- `ESMisc::NumPrivateContents`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - NumPrivateContents__Q33ipl7utility6ESMiscFPQ23EGG4HeapUx: 153 -> 170 instructions; #13 `li r28, 0` -> `li r5, 0`; #14 `li r27, 0` -> `bl 0x38`; #15 `li r5, 0` -> `cmpwi r3, -0x6a`. +- `DeleteDownloadTask`: use early returns with the existing cleanup at each exit. reverted; changed ('.data', 0), ('.text', 0), rel('.text', 0), symbols. + - DeleteDownloadTask__Q33ipl7utility6ESMiscFv: 154 -> 168 instructions; #23 `b 0xcc` -> `b 0x104`; #43 `ble 0xcc` -> `ble 0x104`; #47 `li r23, -0x1a` -> `crclr cr1eq`. +- `ESMisc::DeleteSavedata`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - DeleteSavedata__Q33ipl7utility6ESMiscFUxPQ23EGG4Heap: 123 -> 140 instructions; #31 `bne 0xa0` -> `bne 0xc4`; #39 `b 0x1ac` -> `cmpwi r29, 0`; #40 `mulli r4, r0, 0x41` -> `beq 0xbc`. +- `DeleteTicketsForce`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - DeleteUnauthorizedData__Q33ipl7utility6ESMiscFPQ23EGG4Heap: 449 -> 457 instructions; #25 `b 0x6c4` -> `b 0x6e4`; #44 `b 0x6c4` -> `b 0x6e4`; #56 `b 0x6c4` -> `b 0x6e4`. +- `InitSavedata`: use early returns with the existing cleanup at each exit. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - DeleteUnauthorizedData__Q33ipl7utility6ESMiscFPQ23EGG4Heap: 449 -> 465 instructions; #18 `beq 0x68` -> `beq 0x88`; #25 `b 0x6c4` -> `cmpwi r28, 0`; #26 `lwz r0, 0x24(r1)` -> `beq 0x724`. +- `NandSDWorker::get_nand_save_perms`: classify permissions with a single guarded if/else chain. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::clean_duplicated_nand_app`: return the known corruption result directly, preserving the final result assignment. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - clean_duplicated_nand_app__Q23ipl12NandSDWorkerFPUxUl: 130 -> 134 instructions; #16 `b 0x1e4` -> `b 0x1f4`; #24 `beq 0x1dc` -> `beq 0x1ec`; #30 `beq 0x1dc` -> `beq 0x1ec`. +- `NandSDWorker::clean_partial_nand_app`: return the known corruption result directly, preserving the final result assignment. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - clean_partial_nand_app__Q23ipl12NandSDWorkerFPUxUlPUl: 100 -> 102 instructions; #19 `b 0x16c` -> `b 0x174`; #24 `b 0x178` -> `b 0x180`; #30 `beq 0x164` -> `beq 0x16c`. +- `TMDFile::Backup`: use early returns with the existing backup-buffer cleanup. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - Backup__Q33ipl7utility7TMDFileFUx: 173 -> 203 instructions; #6 `lis r30, 0` -> `lis r28, 0`; #7 `mr r31, r3` -> `mr r29, r3`; #8 `mr r26, r5` -> `mr r31, r5`. +- `TMDFile::Restore`: use early returns with the existing backup-buffer cleanup. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - Restore__Q33ipl7utility7TMDFileFUx: 134 -> 145 instructions; #18 `b 0x200` -> `b 0x22c`; #26 `beq 0x1dc` -> `beq 0x208`; #28 `beq 0x90` -> `beq 0x94`. +- `NandSDWorker::delete_nand_disk_app_with_ticket`: structure the remaining outer error checks while sharing the final cleanup. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::get_sd_wad_header`: structure the remaining outer error checks while sharing the final cleanup. kept; all allocated sections, relocations, and named symbols unchanged. +- `ESMisc::DeleteSavedata`: structure the remaining outer error checks while sharing the final cleanup. kept; all allocated sections, relocations, and named symbols unchanged. +- `DeleteTicketsForce`: structure the remaining outer error checks while sharing the final cleanup. kept; all allocated sections, relocations, and named symbols unchanged. +- `InitSavedata`: structure the remaining outer error checks while sharing the final cleanup. kept; all allocated sections, relocations, and named symbols unchanged. +- `ESMisc::NumPrivateContents`: structure the remaining outer error checks while sharing the final cleanup. kept; all allocated sections, relocations, and named symbols unchanged. +- `ESMisc::GetValidTicketIndex`: structure the remaining outer error checks while sharing the final cleanup. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::do_startup`: structure the final error check inside the existing operation branch. kept; all allocated sections, relocations, and named symbols unchanged. +- `TMDFile::Backup`: structure the final error check inside the existing operation branch. kept; all allocated sections, relocations, and named symbols unchanged. +- `TMDFile::Restore`: structure the final error check inside the existing operation branch. kept; all allocated sections, relocations, and named symbols unchanged. +- `verifySavedataZD`: put verification and save deletion in the successful-read branch. kept; all allocated sections, relocations, and named symbols unchanged. +- `verifySavedataZD`: use loop breaks and validate completion before the second verification pass. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - DeleteUnauthorizedData__Q33ipl7utility6ESMiscFPQ23EGG4Heap: 449 -> 453 instructions; #8 `li r28, 0` -> `li r18, 0`; #9 `lis r21, 0` -> `lis r28, 0`; #10 `stw r28, 0x24(r1)` -> `stw r18, 0x24(r1)`. +- `NandSDWorker::delete_download_task`: break on timeout and use the existing result to guard the library operations. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - delete_download_task__Q23ipl12NandSDWorkerFUl: 133 -> 135 instructions; #47 `b 0x1cc` -> `b 0xd0`; #52 `li r0, 0` -> `cmpwi r27, 0`; #53 `addi r3, r1, 8` -> `bne 0x1d4`. +- `DeleteDownloadTask`: break on timeout and guard the library operations with the result. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - DeleteDownloadTask__Q33ipl7utility6ESMiscFv: 154 -> 156 instructions; #7 `mr r27, r4` -> `mr r26, r4`; #8 `mr r26, r3` -> `mr r25, r3`; #15 `mr r28, r3` -> `mr r27, r3`. +- `NandSDWorker::clean_partial_nand_app`: break on corruption and preserve the operation result at the shared return. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - clean_partial_nand_app__Q23ipl12NandSDWorkerFPUxUlPUl: 100 -> 105 instructions; #0 `stwu r1, -0x30(r1)` -> `stwu r1, -0x40(r1)`; #2 `stw r0, 0x34(r1)` -> `stw r0, 0x44(r1)`; #3 `addi r11, r1, 0x30` -> `addi r11, r1, 0x40`. +- `NandSDWorker::clean_duplicated_nand_app`: use the result to stop both title loops after corruption. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - clean_duplicated_nand_app__Q23ipl12NandSDWorkerFPUxUl: 130 -> 138 instructions; #6 `mr r19, r3` -> `mr r18, r3`; #8 `mr r20, r4` -> `mr r19, r4`; #9 `mr r21, r5` -> `mr r20, r5`. +- `NandSDWorker::do_list_nand_apps_usage`: break on usage failure and report success only after successful iteration. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - do_list_nand_apps_usage__Q23ipl12NandSDWorkerFv: 114 -> 118 instructions; #10 `mr r27, r3` -> `mr r26, r3`; #20 `lwz r3, 0(r27)` -> `lwz r3, 0(r26)`; #30 `lwz r3, 0(r27)` -> `lwz r3, 0(r26)`. +- `NandSDWorker::recursion_nand`: use a traversal failure flag and ordinary loop and switch breaks. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - recursion_nand__Q23ipl12NandSDWorkerFPCcPCcQ33ipl12NandSDWorker16RecursiveProcessPPCcUl: 318 -> 329 instructions; #5 `li r31, 0` -> `li r27, 0`; #6 `lis r30, 0` -> `lis r26, 0`; #7 `stw r31, 0x10(r1)` -> `stw r27, 0x10(r1)`. +- `NandSDWorker::get_save_banner_for_data_only_title`: use one if/else chain for adjacent error outcomes and the successful operation. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::do_check_for_sd_app_to_nand`: use one if/else chain for adjacent error outcomes and the successful operation. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::do_copy_nand_save_to_sd`: use one if/else chain for adjacent error outcomes and the successful operation. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::do_copy_sd_save_to_nand`: use one if/else chain for adjacent error outcomes and the successful operation. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::do_copy_sd_app_to_nand`: use one if/else chain for adjacent error outcomes and the successful operation. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::open_nand_app_content`: use one if/else chain for adjacent error outcomes and the successful operation. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - open_nand_app_content__Q23ipl12NandSDWorkerFUxUsPUlPP12ESTicketView: 135 -> 133 instructions; #20 `bne 0x1b8` -> `bne 0x1b0`; #33 `b 0x1b8` -> `b 0x1b0`; #40 `beq 0xc0` -> `beq 0xb8`. +- `NandSDWorker::open_nand_app_content`: structure ticket query failure, zero tickets, and content opening as one error chain. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::get_sd_save_banner`: structure the preceding validation before the successful final checks. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::get_sd_app_banner`: structure the preceding validation before the successful final checks. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::get_sd_app_thumbnail`: structure the preceding validation before the successful final checks. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::check_sd_title_restorable`: structure the preceding validation before the successful final checks. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::nand_app_exist_ex`: structure the preceding validation before the successful final checks. kept; all allocated sections, relocations, and named symbols unchanged. +- `ESMisc::GetValidTicketIndex`: write the library initialization success test directly. kept; all allocated sections, relocations, and named symbols unchanged. +- `ESMisc::GetValidTicketIndex`: return through the existing error cleanup after either content-close failure. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - GetValidTicketIndex__Q33ipl7utility6ESMiscFPQ23EGG4HeapUxP12ESTicketViewUl: 183 -> 235 instructions; #30 `b 0x2c4` -> `b 0x394`; #42 `blt 0x25c` -> `blt 0x32c`; #48 `blt 0x25c` -> `blt 0x32c`. +- `ESMisc::GetValidTicketIndex`: share one error jump after both content-close outcomes. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - GetValidTicketIndex__Q33ipl7utility6ESMiscFPQ23EGG4HeapUxP12ESTicketViewUl: 183 -> 185 instructions; #30 `b 0x2c4` -> `b 0x2cc`; #42 `blt 0x25c` -> `blt 0x264`; #48 `blt 0x25c` -> `blt 0x264`. +- `verifySavedataZD`: keep validation status in the existing flag and stop each verification loop with break. reverted; changed ('.text', 0), rel('.text', 0), symbols. + - DeleteUnauthorizedData__Q33ipl7utility6ESMiscFPQ23EGG4Heap: 449 -> 454 instructions; #25 `b 0x6c4` -> `b 0x6d8`; #44 `b 0x6c4` -> `b 0x6d8`; #56 `b 0x6c4` -> `b 0x6d8`. +- `NandSDWorker::check_sd_title_restorable`: guard device verification with the successful WAD-header result. kept; all allocated sections, relocations, and named symbols unchanged. +- `NandSDWorker::get_nand_save_banner`: return after closing the file and restoring the UID on each banner error. reverted; changed ('.data', 0), ('.text', 0), rel('.text', 0), symbols. + - get_app_banner_from_meta__Q23ipl12NandSDWorkerFUxPQ33ipl12NandSDWorker15SDAppBackupData: 288 -> 288 instructions; #253 `addi r3, r30, 0xa48` -> `addi r3, r30, 0x9c2`. + - get_nand_save_banner__Q23ipl12NandSDWorkerFUxP17WIISaveBannerFilePUl: 236 -> 383 instructions; #0 `stwu r1, -0x100(r1)` -> `stwu r1, -0x110(r1)`; #2 `stw r0, 0x104(r1)` -> `stw r0, 0x114(r1)`; #3 `addi r11, r1, 0x100` -> `addi r11, r1, 0x110`. + +### Round-b results by function + +Every function containing a goto was reviewed. The 197 NandSDWorker jumps all went to a single cleanup or return label per function; there was no remaining state-machine dispatch to replace with a switch. ESMisc had the same pattern plus two jumps out of Zelda verification loops. Keep the remaining ordinary resource/error exits after the structured changes below. + +#### iplNandSDWorker + +| Function | Gotos before -> after | Kept structure or measured reason for retention | +|---|---:|---| +| `NandSDWorker::do_startup` | 14 -> 12 | Early cleanup returns change block layout/allocation, 323 -> 480 instructions; shared error cleanup stays. | +| `NandSDWorker::clean_duplicated_nand_app` | 2 -> 2 | Constant early returns add 4 instructions. Result-controlled loops add 8 and change saved-register allocation. | +| `NandSDWorker::clean_partial_nand_app` | 1 -> 1 | Constant early return adds 2 instructions. Result-driven break adds 5 and grows the stack frame from 0x30 to 0x40. | +| `NandSDWorker::get_nand_save_banner` | 9 -> 8 | Early cleanup returns reorder error-report literals and change text/data; shared error cleanup stays. | +| `NandSDWorker::do_check_for_sd_app_to_nand` | 11 -> 8 | Early cleanup returns change block layout/allocation, 242 -> 292 instructions; shared error cleanup stays. | +| `NandSDWorker::check_backup_fits` | 10 -> 9 | Early cleanup returns change block layout/allocation, 246 -> 291 instructions; shared error cleanup stays. | +| `NandSDWorker::do_list_nand_apps_usage` | 1 -> 1 | Early cleanup return grows 114 -> 119 instructions. Break plus final status check grows 114 -> 118 and changes allocation. | +| `NandSDWorker::delete_nand_disk_app_with_ticket` | 2 -> 0 | Structured checks and one final cleanup/return preserve every byte. | +| `NandSDWorker::delete_nand_titles` | 5 -> 4 | Early cleanup returns change block layout/allocation, 146 -> 168 instructions; shared error cleanup stays. | +| `NandSDWorker::do_copy_nand_save_to_sd` | 19 -> 16 | Early cleanup returns reorder error-report literals and change text/data; shared error cleanup stays. | +| `NandSDWorker::do_copy_sd_save_to_nand` | 27 -> 24 | Early cleanup returns reorder error-report literals and change text/data; shared error cleanup stays. | +| `NandSDWorker::get_sd_save_banner` | 5 -> 3 | Early cleanup returns change block layout/allocation, 142 -> 166 instructions; shared error cleanup stays. | +| `NandSDWorker::get_sd_wad_header` | 4 -> 0 | Structured checks and one final cleanup/return preserve every byte. | +| `NandSDWorker::delete_download_task` | 1 -> 1 | Timeout break plus result guard adds one `cmpwi` and one `bne`, 133 -> 135 instructions. Direct returns duplicate cleanup and alter literal order. | +| `NandSDWorker::do_copy_nand_app_to_sd` | 11 -> 10 | Early cleanup returns change block layout/allocation, 332 -> 481 instructions; shared error cleanup stays. | +| `NandSDWorker::get_save_banner_for_data_only_title` | 4 -> 1 | Early cleanup returns change block layout/allocation, 88 -> 115 instructions; shared error cleanup stays. | +| `NandSDWorker::get_app_banner_from_meta` | 12 -> 11 | Early cleanup returns reorder error-report literals and change text/data; shared error cleanup stays. | +| `NandSDWorker::open_nand_app_content` | 5 -> 2 | Early cleanup returns change block layout/allocation, 135 -> 203 instructions; shared error cleanup stays. | +| `NandSDWorker::do_copy_sd_app_to_nand` | 11 -> 8 | Early cleanup returns change block layout/allocation, 300 -> 370 instructions; shared error cleanup stays. | +| `NandSDWorker::get_sd_app_banner` | 6 -> 4 | Early cleanup returns change block layout/allocation, 157 -> 187 instructions; shared error cleanup stays. | +| `NandSDWorker::get_sd_app_thumbnail` | 7 -> 5 | Early cleanup returns change block layout/allocation, 172 -> 236 instructions; shared error cleanup stays. | +| `NandSDWorker::nand_app_exist_ex` | 8 -> 6 | Early cleanup returns change block layout/allocation, 226 -> 274 instructions; shared error cleanup stays. | +| `NandSDWorker::recursion_nand` | 9 -> 9 | Early cleanup returns grow 318 -> 479 instructions. Failure-controlled loop/switch breaks grow 318 -> 329, with three extra comparisons and new failure branches. | +| `NandSDWorker::get_nand_save_perms` | 7 -> 0 | Structured checks and one final cleanup/return preserve every byte. | +| `NandSDWorker::check_sd_title_restorable` | 6 -> 4 | Early cleanup returns change block layout/allocation, 97 -> 130 instructions; shared error cleanup stays. | + +#### iplESMisc + +| Function | Gotos before -> after | Kept structure or measured reason for retention | +|---|---:|---| +| `ESMisc::GetTmdView` | 1 -> 0 | Structured checks and one final cleanup/return preserve every byte. | +| `ESMisc::GetValidTicketIndex` | 4 -> 2 | Library/index checks are structured. Direct error returns grow 183 -> 235; merging the two close-error jumps grows 183 -> 185 with two extra register moves. | +| `ESMisc::DeleteSharedContent` | 11 -> 10 | Early cleanup returns change block layout/allocation, 261 -> 477 instructions; shared error cleanup stays. | +| `ESMisc::NumPrivateContents` | 4 -> 0 | Structured checks and one final cleanup/return preserve every byte. | +| `DeleteDownloadTask` | 1 -> 1 | Timeout break plus result guard grows 154 -> 156 instructions and changes register allocation. Direct returns duplicate cleanup and alter literal order. | +| `ESMisc::DeleteSavedata` | 3 -> 0 | Structured checks and one final cleanup/return preserve every byte. | +| `verifySavedataZD` | 3 -> 2 | Successful-read branch removes the cleanup jump. Loop-index and validation-flag break forms add 4 and 5 instructions to `DeleteUnauthorizedData`, including completion tests and different failure branches. | +| `DeleteTicketsForce` | 2 -> 0 | Structured checks and one final cleanup/return preserve every byte. | +| `InitSavedata` | 3 -> 0 | Structured checks and one final cleanup/return preserve every byte. | +| `TMDFile::Backup` | 7 -> 6 | Early cleanup returns change block layout/allocation, 173 -> 203 instructions; shared error cleanup stays. | +| `TMDFile::Restore` | 4 -> 3 | Early cleanup returns change block layout/allocation, 134 -> 145 instructions; shared error cleanup stays. | + +### Scope and comparison notes + +- NandSDWorker: 197 -> 149 gotos, 48 removed. ESMisc: 43 -> 24 gotos, 19 removed. No new goto, carrier flag, helper, compiler pragma, inline assembly, or volatile access was kept. +- Rejected candidates include register allocation changes, extra exit tests/branches, duplicated cleanup blocks, and reordered error-report literals. The detailed trial entries above give function sizes and the first changed instructions. Zelda validation retains its one-line compiler explanation. +- The first ticket-query chain trial used two independent checks after deleting jumps. It changed the failure flow and was reverted. Discard that trial as compiler evidence; the corrected explicit else-if trial preserved every allocated byte and was kept. +- Formatting was limited to changed functions. Removed unreferenced labels and blank lines left by rewritten guards. Reviewed both focused source diffs; no unrelated edit was kept. +- After formatting, both rebuilt objects have identical allocated sections, relocation destinations, and named symbols versus the fresh round-b baseline. Only compiler-generated anonymous literal/`__FUNCTION__` symbol numbers are normalized, with offsets/sizes/bytes checked. +- Section-aware comparison against retail: NandSDWorker 203/203 and ESMisc 31/31 instruction-exact functions, zero raw-byte differences. Pools remain 280/280 and 114/114 identical strings. + +### Round-b final validation + +- Required gate ran once after all source changes: `python3 /mnt/drive2/projects/wii-ipl-workers/_restore0928-tools/gate.py src/system/iplNandSDWorker src/utility/iplESMisc --quick`. +- `GATE PASS`: full 43U build passed; 0 regressions; 0 forbidden-pattern additions; 0 readability warnings. Gate output: `/tmp/cleanup2-w2b-gate.log`. +- NandSDWorker: 203/203 functions; 51428/51428 code bytes; 12312/12312 data bytes. ESMisc: 31/31 functions; 11200/11200 code bytes; 4416/4416 data bytes. Both units, code/data/link metrics, and every reported section remain 100%. +- Regenerated live report, `ninja build/43U/ok`, and `check_decomp_complete.py` passed with `DECOMPLETE_OK`. DOL SHA1 remains `26116613f624061ba99c8d1a299aaa6efa85670d`. +- Rechecked all original functions with the section-aware decoder: 203/203 and 31/31, identical instruction counts, zero instruction differences, and zero raw-byte differences. This independently resolves the gate's known `.text` reader bug, which still prints 7/203 for the two-text-section NandSDWorker object. +- The additional strict assembly inventory check reports three existing placeholders: `TMCJPEGDEC_err_restart`, `CNTCACHEClear`, and `System::warning_run`. Their three source files are byte-identical to baseline `8e47db4a` and outside this assignment. This is a baseline inventory failure; this cleanup introduces no assembly changes. +- Final scope is the two assigned source files and this attempts log. No push, PR, merge, rebase, other-worktree edit, or subagent. + +### Round-b source commits + +- `ecfc73c9e`: NandSDWorker structured error paths only. +- `866b01d3e`: ESMisc structured error paths only. -- 2.49.1