diff --git a/trunk/ConsoleDataMigration/sources/ConsoleBackup/ConsoleBackup.cpp b/trunk/ConsoleDataMigration/sources/ConsoleBackup/ConsoleBackup.cpp index a7a0ef9..0a11eb1 100644 --- a/trunk/ConsoleDataMigration/sources/ConsoleBackup/ConsoleBackup.cpp +++ b/trunk/ConsoleDataMigration/sources/ConsoleBackup/ConsoleBackup.cpp @@ -47,7 +47,6 @@ #include "CommonLogger.h" #include "SDMountManager.h" #include "HeapManager.h" -#include "PlayHistoryManager.h" #include "common_Types.h" #include "VersionDetect.h" #include "Util.h" @@ -111,10 +110,11 @@ extern "C" void nnMain(void) nn::am::InitializeForSystemMenu(); // ヒープの確保 - common::HeapManager::GetHeap()->Initialize(nn::os::GetDeviceMemoryAddress(), nn::os::GetDeviceMemorySize(), nn::os::ALLOCATE_OPTION_LINEAR); + common::InitializeHeap(); // RenderSystem の準備 - uptr heapForGx = reinterpret_cast(common::HeapManager::GetHeap()->Allocate(s_GxHeapSize)); + common::HeapManager gxHeapManager(s_GxHeapSize); + uptr heapForGx = reinterpret_cast(gxHeapManager.GetAddr()); demo::RenderSystemDrawing renderSystem; renderSystem.Initialize(heapForGx, s_GxHeapSize); diff --git a/trunk/ConsoleDataMigration/sources/ConsoleBackup/Controller.cpp b/trunk/ConsoleDataMigration/sources/ConsoleBackup/Controller.cpp index c612f5e..2e3952b 100644 --- a/trunk/ConsoleDataMigration/sources/ConsoleBackup/Controller.cpp +++ b/trunk/ConsoleDataMigration/sources/ConsoleBackup/Controller.cpp @@ -171,7 +171,15 @@ void ControlState(common::HardwareStateManager& manager, ::std::vector 99) + if (IsExportSucceeded()) { - s_BackupState = DELETE_NIM; + COMMON_LOGGER("Export NAND Data Finished.\n"); + + if (GetProgress() > 99) + { + s_BackupState = DELETE_NIM; + } + else + { + s_BackupState = FAIL; + } } else { s_BackupState = FAIL; } - } } break; @@ -253,8 +282,15 @@ void ControlState(common::HardwareStateManager& manager, ::std::vector s_ExportThreadStack; +bool s_IsExportSucceeded; wchar_t s_RootName[256]; @@ -156,12 +156,13 @@ void DeleteTrash(std::wstring currentDirectory) common::SdMountManager::Unmount(); } -void WriteTwlTitleList(std::vector& programIdList) +nn::Result WriteTwlTitleList(std::vector& programIdList) { COMMON_LOGGER("Export TwlTitle List.\n"); - size_t heapSize = common::HeapManager::GetHeap()->GetAllocatableSize(); - char* titleListBuf = reinterpret_cast (common::HeapManager::GetHeap()->Allocate(heapSize)); + size_t heapSize = common::GetAllocatableSize(); + common::HeapManager manager(heapSize); + char* titleListBuf = reinterpret_cast (manager.GetAddr()); size_t writeSize = 0; if (titleListBuf != NULL) @@ -175,8 +176,7 @@ void WriteTwlTitleList(std::vector& programIdList) } nn::Result result = s_SdWriter.WriteBufWithCmac(common::TWL_TITLELIST_PATHNAME, titleListBuf, writeSize); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - common::HeapManager::GetHeap()->Free(titleListBuf); + return result; } @@ -264,7 +264,8 @@ void WriteIvs(common::HardwareStateManager& manager) void* enc; nn::Result result; - enc = common::HeapManager::GetHeap()->Allocate(size); + common::HeapManager ivsHeap(size); + enc = ivsHeap.GetAddr(); if(enc != NULL) { // AES暗号化する @@ -276,8 +277,6 @@ void WriteIvs(common::HardwareStateManager& manager) swAesCtrContest.Encrypt(enc, ivs, size); s_SdWriter.WriteBufWithCmac(common::IVS_PATHNAME, enc, size); - - common::HeapManager::GetHeap()->Free(enc); } } @@ -458,27 +457,32 @@ void WriteTwlData(enum common::TWL_PATH_INDEX path) NN_ASSERT(path < common::TWL_PATHNAME_MAX); nn::Result result; + s_IsExportSucceeded = true; result = nn::fs::MountSpecialArchive(common::TWL_ARCHIVE_NAME_TABLE[path], common::TWL_FS_ARCHIVE_KIND[path]); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsExportSucceeded); result = common::SdMountManager::Mount(); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsExportSucceeded); - size_t bufSize = common::HeapManager::GetHeap()->GetAllocatableSize(); + size_t bufSize = common::GetAllocatableSize(); NN_LOG("AllocatableSize = %d\n", bufSize); u32 fileNum = 0; s64 fileSize = 0; - common::CalculateFileNum(::std::wstring(common::NAND_TWL_ROOT_PATHNAME_WITH_SLASH_TABLE[path]), fileNum, fileSize); + result = common::CalculateFileNum(::std::wstring(common::NAND_TWL_ROOT_PATHNAME_WITH_SLASH_TABLE[path]), fileNum, fileSize); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsExportSucceeded); nn::fs::Unmount(common::NAND_ARCHIVE_NAME); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsExportSucceeded); NN_LOG("File Number = %d\n", fileNum); NN_LOG("File Size = %d\n", fileSize); // 進捗表示用 common::InitializeTransferProgress(fileSize); - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize, AES_BLOCK_SIZE); + common::HeapManager writeHeap(bufSize, AES_BLOCK_SIZE); + void* buf = writeHeap.GetAddr(); if (buf != NULL) { nn::fs::FileOutputStream list; @@ -497,31 +501,38 @@ void WriteTwlData(enum common::TWL_PATH_INDEX path) ::std::mbstowcs(archiveName, common::TWL_ARCHIVE_NAME_TABLE[path], std::strlen(common::TWL_ARCHIVE_NAME_TABLE[path]) + 1); std::wstring archiveString(archiveName); - common::CopyDirectory( + if(!common::CopyDirectory( NULL, (archiveString + ::std::wstring(L"/")).c_str(), (common::SDMC_ROOT_DIRECTORY_PATH + ::std::wstring(common::SD_TWL_ROOTNAME_TABLE[path])).c_str(), - buf, bufSize, true, &list, &s_FileListContext); + buf, bufSize, true, &list, &s_FileListContext)) + { + s_IsExportSucceeded = false; + return; + } } list.TryFlush(); list.Finalize(); } } - common::HeapManager::GetHeap()->Free(buf); + else + { + s_IsExportSucceeded = false; + } } common::SdMountManager::Unmount(); nn::fs::Unmount(common::TWL_ARCHIVE_NAME_TABLE[path]); } -void CalculateTwlSaveData(std::vector* fileList, s64* fileSize) +nn::Result CalculateTwlSaveData(std::vector* fileList, s64* fileSize) { NN_NULL_ASSERT(fileList); NN_NULL_ASSERT(fileSize); *fileSize = 0; - nn::Result result; + nn::Result result = nn::ResultSuccess(); for (std::vector::iterator it = fileList->begin(); it != fileList->end(); it++) { @@ -541,28 +552,33 @@ void CalculateTwlSaveData(std::vector* fileList, s64* file } } + return result; + } void WriteTwlSaveData() { nn::Result result; + s_IsExportSucceeded = true; std::vector fileList; std::vector programIdList; COMMON_LOGGER("Export Twl Save Data.\n"); // ディレクトリ作成 - s_SdWriter.CreateDirectory((::std::wstring(common::SDMC_ROOT_DIRECTORY_PATH) + + result = s_SdWriter.CreateDirectory((::std::wstring(common::SDMC_ROOT_DIRECTORY_PATH) + std::wstring(common::SD_SAVEDATA_TWL_ROOT_NAME)).c_str()); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsExportSucceeded); // セーブデータを含むディレクトリ一覧を生成 result = nn::fs::MountSpecialArchive(common::NAND_TWL_ARCHIVE_NAME, nn::fs::CTR::ARCHIVE_TYPE_TWL_NAND); - COMMON_LOGGER_RESULT_IF_FAILED(result); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsExportSucceeded); ListTwlSaveDataDirectory(std::wstring(common::NAND_TWL_DATA_ROOT_PATHNAME_WITHOUT_SLASH), 0, &fileList, & programIdList); NN_LOG("No Twl Savedata\n"); if(fileList.size() == 0) { + s_IsExportSucceeded = true; return; } @@ -572,73 +588,75 @@ void WriteTwlSaveData() NN_LOG("%ls\n", it->name.c_str()); } - WriteTwlTitleList(programIdList); + result = WriteTwlTitleList(programIdList); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsExportSucceeded); // 合計サイズ取得 s64 fileSize; - CalculateTwlSaveData(&fileList, &fileSize); + result = CalculateTwlSaveData(&fileList, &fileSize); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsExportSucceeded); common::InitializeTransferProgress(fileSize); NN_LOG("\n"); // SDに書き出し result = common::SdMountManager::Mount(); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsExportSucceeded); - size_t bufSize = common::HeapManager::GetHeap()->GetAllocatableSize(); + size_t bufSize = common::GetAllocatableSize(); NN_LOG("AllocatableSize = %d\n", bufSize); - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize, AES_BLOCK_SIZE); + common::HeapManager writeHeap(bufSize, AES_BLOCK_SIZE); + void* buf = writeHeap.GetAddr(); if (buf != NULL) { nn::fs::FileOutputStream list; result = list.TryInitialize(common::FILE_LIST_PATHNAME, true); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - if (result.IsSuccess()) + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsExportSucceeded); + + result = list.TryGetSize(&fileSize); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsExportSucceeded); + + // 末尾に移動 + result = list.TrySetPosition(fileSize); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsExportSucceeded); + + wchar_t archiveName[nn::fs::MAX_FILE_PATH_LENGTH]; + ::std::mbstowcs(archiveName, common::NAND_TWL_ARCHIVE_NAME, std::strlen(common::NAND_TWL_ARCHIVE_NAME) + 1); + std::wstring archiveString(archiveName); + + for (std::vector::reverse_iterator it = fileList.rbegin(); it != fileList.rend(); it++) { - result = list.TryGetSize(&fileSize); - if (result.IsSuccess()) + // twln:/title/をsdmc:/CTR_Console_Repair/TWLBackup/に置換 + std::wstring toPath(it->name.c_str()); + toPath.replace(0, std::wcslen(common::NAND_TWL_DATA_ROOT_PATHNAME_WITHOUT_SLASH) + 1, + std::wstring(common::SDMC_ROOT_DIRECTORY_PATH) + std::wstring(common::SD_SAVEDATA_TWL_ROOT_NAME)); + + if (it->isDirectory) { - // 末尾に移動 - result = list.TrySetPosition(fileSize); - if (result.IsSuccess()) + if(!common::ExportTwlSaveDirectory(toPath.c_str(), &list, &s_FileListContext)) { - wchar_t archiveName[nn::fs::MAX_FILE_PATH_LENGTH]; - ::std::mbstowcs(archiveName, common::NAND_TWL_ARCHIVE_NAME, - std::strlen(common::NAND_TWL_ARCHIVE_NAME) + 1); - std::wstring archiveString(archiveName); - - for (std::vector::reverse_iterator it = fileList.rbegin(); it - != fileList.rend(); it++) - { - // twln:/title/をsdmc:/CTR_Console_Repair/TWLBackup/に置換 - std::wstring toPath(it->name.c_str()); - toPath.replace( - 0, - std::wcslen(common::NAND_TWL_DATA_ROOT_PATHNAME_WITHOUT_SLASH) + 1, - std::wstring(common::SDMC_ROOT_DIRECTORY_PATH) + std::wstring( - common::SD_SAVEDATA_TWL_ROOT_NAME)); - - if(it->isDirectory) - { - common::ExportTwlSaveDirectory(toPath.c_str(), &list, &s_FileListContext); - } - else - { - common::ExportTwlSaveFile(it->name.c_str(), toPath.c_str(), buf, bufSize, &list, - &s_FileListContext); - } - } + s_IsExportSucceeded = false; + return; + } + } + else + { + if(!common::ExportTwlSaveFile(it->name.c_str(), toPath.c_str(), buf, bufSize, &list, &s_FileListContext)) + { + s_IsExportSucceeded = false; + return; } - - list.TryFlush(); - list.Finalize(); } } - common::HeapManager::GetHeap()->Free(buf); + result = list.TryFlush(); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsExportSucceeded); + list.Finalize(); } - common::SdMountManager::Unmount(); - - nn::fs::Unmount( common::NAND_TWL_ARCHIVE_NAME); + result = common::SdMountManager::Unmount(); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsExportSucceeded); + result = nn::fs::Unmount(common::NAND_TWL_ARCHIVE_NAME); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsExportSucceeded); } void WriteTwlPhotoData() @@ -717,16 +735,18 @@ void WriteMcuRtcData(common::HardwareStateManager& manager) void ExportThreadFunc() { nn::Result result; + s_IsExportSucceeded = true; result = nn::fs::MountSpecialArchive(common::NAND_ARCHIVE_NAME, nn::fs::CTR::ARCHIVE_TYPE_CTR_NAND); COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); result = common::SdMountManager::Mount(); - size_t bufSize = common::HeapManager::GetHeap()->GetAllocatableSize(); + size_t bufSize = common::GetAllocatableSize(); NN_LOG("AllocatableSize = %d\n", bufSize); - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize, AES_BLOCK_SIZE); + common::HeapManager writeHeap(bufSize, AES_BLOCK_SIZE); + void* buf = writeHeap.GetAddr(); if (buf != NULL) { nn::fs::FileOutputStream list; @@ -742,12 +762,16 @@ void ExportThreadFunc() result = list.TrySetPosition(fileSize); if (result.IsSuccess()) { - common::CopyDirectory( + if(!common::CopyDirectory( NULL, ::std::wstring(common::NAND_DATA_ROOT_PATHNAME_WITH_SLASH).c_str(), (std::wstring(common::SDMC_ROOT_DIRECTORY_PATH) + ::std::wstring( common::SD_SAVEDATA_ROOT_NAME)).c_str(), buf, bufSize, true, &list, - &s_FileListContext); + &s_FileListContext)) + { + s_IsExportSucceeded = false; + return; + } AddCmac(&list, &s_FileListContext); list.TryFlush(); @@ -755,7 +779,6 @@ void ExportThreadFunc() } } } - common::HeapManager::GetHeap()->Free(buf); } common::SdMountManager::Unmount(); @@ -816,15 +839,7 @@ void WriteVersionData(common::HardwareStateManager& manager) s_SdWriter.WriteBufWithCmac(common::VERSION_DATA_PATHNAME, &versionData, sizeof(common::VerDef)); } -void WritePlayHistory() -{ - common::PlayHistoryManager historyManager; - - COMMON_LOGGER("Export PlayHistory\n"); - historyManager.Export(); -} - -void DeleteNimSaveData() +bool DeleteNimSaveData() { nn::Result result; ::std::wstring nimSaveDataPath = @@ -834,13 +849,21 @@ void DeleteNimSaveData() ::std::wstring(L"/") + std::wstring(common::NIM_SAVEDATA_DIRECTORY_NAME); - common::SdMountManager::Mount(); + result = common::SdMountManager::Mount(); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); NN_LOG("%ls\n", nimSaveDataPath.c_str()); result = nn::fs::TryDeleteDirectoryRecursively(nimSaveDataPath.c_str()); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + if (result.IsFailure() && !nn::fs::ResultNotFound::Includes(result)) + { + COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + return false; + } - common::SdMountManager::Unmount(); + result = common::SdMountManager::Unmount(); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + return true; } void AddShutDownPtmEvent() @@ -880,9 +903,6 @@ void ExportData(common::HardwareStateManager& manager) // IVSからセーブデータディレクトリ名を計算 common::Util::GetSaveDataDirectoryRoot(systemSaveRoot, ivs, size); - // プレイ履歴をSDに書き出す - //WritePlayHistory(); - // 電源断の履歴をptmに追加する AddShutDownPtmEvent(); @@ -909,6 +929,11 @@ bool IsExportThreadFinished() return s_ExportThread.IsValid() && !s_ExportThread.IsAlive(); } +bool IsExportSucceeded() +{ + return s_IsExportSucceeded; +} + //!@ brief ファイルにSHA256から計算したAES-CMACを付加します //!@ param[in] file CMACを付加したいInitialize済みのファイル //!@ param[in] context CMAC計算元のSHA256コンテキスト diff --git a/trunk/ConsoleDataMigration/sources/ConsoleBackup/Exporter.h b/trunk/ConsoleDataMigration/sources/ConsoleBackup/Exporter.h index 83d39d3..e4b21ca 100644 --- a/trunk/ConsoleDataMigration/sources/ConsoleBackup/Exporter.h +++ b/trunk/ConsoleDataMigration/sources/ConsoleBackup/Exporter.h @@ -40,7 +40,7 @@ void ExportTwlSoundData(); void ExportData(common::HardwareStateManager& manager); // NIMのセーブデータをSDカードから削除する -void DeleteNimSaveData(); +bool DeleteNimSaveData(); // 出力スレッドの進捗を返す u32 GetProgress(); @@ -51,6 +51,8 @@ bool IsExportThreadFinished(); // 出力スレッドを終了する void FinalizeExportThread(); +// 出力が成功したかどうか +bool IsExportSucceeded(); } diff --git a/trunk/ConsoleDataMigration/sources/ConsoleBackup/OMakefile b/trunk/ConsoleDataMigration/sources/ConsoleBackup/OMakefile index 7bd1982..88cdf18 100644 --- a/trunk/ConsoleDataMigration/sources/ConsoleBackup/OMakefile +++ b/trunk/ConsoleDataMigration/sources/ConsoleBackup/OMakefile @@ -38,7 +38,6 @@ SOURCES[] = ../common/LogConsole.cpp ../common/CommonLogger.cpp ../common/SdMountManager.cpp - ../common/PlayHistoryManager.cpp ../common/VersionDetect.cpp ../common/ResFont.cpp ../common/HardwareStateManager.cpp diff --git a/trunk/ConsoleDataMigration/sources/ConsoleRestore/ConsoleRestore.cpp b/trunk/ConsoleDataMigration/sources/ConsoleRestore/ConsoleRestore.cpp index 13ff5a3..786eb73 100644 --- a/trunk/ConsoleDataMigration/sources/ConsoleRestore/ConsoleRestore.cpp +++ b/trunk/ConsoleDataMigration/sources/ConsoleRestore/ConsoleRestore.cpp @@ -131,10 +131,11 @@ extern "C" void nnMain(void) nn::am::InitializeForSystemMenu(); // ヒープの確保 - common::HeapManager::GetHeap()->Initialize(nn::os::GetDeviceMemoryAddress(), nn::os::GetDeviceMemorySize(), nn::os::ALLOCATE_OPTION_LINEAR); + common::InitializeHeap(); // RenderSystem の準備 - uptr heapForGx = reinterpret_cast(common::HeapManager::GetHeap()->Allocate(s_GxHeapSize)); + common::HeapManager gxHeap(s_GxHeapSize); + uptr heapForGx = reinterpret_cast(gxHeap.GetAddr()); s_RenderSystem.Initialize(heapForGx, s_GxHeapSize); // ResFontの初期化 diff --git a/trunk/ConsoleDataMigration/sources/ConsoleRestore/Controller.cpp b/trunk/ConsoleDataMigration/sources/ConsoleRestore/Controller.cpp index 19d2a95..c0443ac 100644 --- a/trunk/ConsoleDataMigration/sources/ConsoleRestore/Controller.cpp +++ b/trunk/ConsoleDataMigration/sources/ConsoleRestore/Controller.cpp @@ -1166,7 +1166,15 @@ void ControlState(common::HardwareStateManager& manager, ::std::vector 99) + if (IsImportSucceeded()) { - s_RestoreState = POST_RESTORE; + + COMMON_LOGGER("Import NAND Data Finished.\n"); + + if (GetProgress() > 99) + { + s_RestoreState = POST_RESTORE; + } + else + { + s_RestoreState = FAIL; + } } else { s_RestoreState = FAIL; } - } } break; diff --git a/trunk/ConsoleDataMigration/sources/ConsoleRestore/Importer.cpp b/trunk/ConsoleDataMigration/sources/ConsoleRestore/Importer.cpp index 581511f..c2bbe3d 100644 --- a/trunk/ConsoleDataMigration/sources/ConsoleRestore/Importer.cpp +++ b/trunk/ConsoleDataMigration/sources/ConsoleRestore/Importer.cpp @@ -45,7 +45,6 @@ #include "common_Types.h" #include "Aes_define.h" #include "configLoader.h" -#include "PlayHistoryManager.h" #include "FileChecker.h" #include "VersionDetect.h" @@ -59,6 +58,7 @@ namespace const size_t IMPORT_THREAD_STACK_SIZE = 0x4000; nn::os::Thread s_ImportThread; nn::os::StackBuffer s_ImportThreadStack; +bool s_IsImportSucceeded; const size_t TIME_ZONE_LENGTH = 9; // "+23:45" char s_TimeZoneStr[TIME_ZONE_LENGTH]; @@ -230,7 +230,8 @@ nn::Result ReadSerialNumber(u8* serial) size_t readSize; common::SdReaderWriter sdReader; size_t bufSize = 1024; - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize); + common::HeapManager readHeap(bufSize); + void* buf = readHeap.GetAddr(); if(buf != NULL) { result = sdReader.ReadBufWithCmac(common::SERIAL_PATHNAME, buf, bufSize, &readSize); @@ -240,7 +241,6 @@ nn::Result ReadSerialNumber(u8* serial) std::memcpy(serial, s_SerialNo, sizeof(s_SerialNo)); s_ReadSerialNumber = true; } - common::HeapManager::GetHeap()->Free(buf); } else { @@ -265,7 +265,8 @@ nn::Result EqualsDeviceIdFileandDeviceId(common::HardwareStateManager& manager) common::SdReaderWriter sdReader; size_t totalSize; size_t bufSize = 1024; - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize); + common::HeapManager heap(bufSize); + void* buf = heap.GetAddr(); if (buf != NULL) { result = sdReader.ReadBufWithCmac(common::DEVICE_ID_PATHNAME, buf, bufSize, &totalSize); @@ -284,7 +285,6 @@ nn::Result EqualsDeviceIdFileandDeviceId(common::HardwareStateManager& manager) nn::Result::MODULE_COMMON, nn::Result::DESCRIPTION_INVALID_RESULT_VALUE); } } - common::HeapManager::GetHeap()->Free(buf); } else { @@ -313,7 +313,8 @@ nn::Result EqualsRegionDataandRegion() common::SdReaderWriter sdReader; size_t readSize; size_t bufSize = 1024; - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize); + common::HeapManager heap(bufSize); + void* buf = heap.GetAddr(); if (buf != NULL) { result = sdReader.ReadBufWithCmac(common::REGION_DATA_PATHNAME, buf, bufSize, &readSize); @@ -331,7 +332,6 @@ nn::Result EqualsRegionDataandRegion() nn::Result::MODULE_COMMON, nn::Result::DESCRIPTION_INVALID_RESULT_VALUE); } } - common::HeapManager::GetHeap()->Free(buf); } else { @@ -387,7 +387,8 @@ nn::Result ImportCountryLanguageData() if (common::ExistsCountryLanguageFile()) { size_t bufSize = 1024; - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize); + common::HeapManager heap(bufSize); + void* buf = heap.GetAddr(); if (buf != NULL) { common::SdReaderWriter sdReader; @@ -401,7 +402,6 @@ nn::Result ImportCountryLanguageData() SetLanguage(reinterpret_cast (buf)->language); } - common::HeapManager::GetHeap()->Free(buf); NN_UTIL_RETURN_IF_FAILED(result); } @@ -483,7 +483,8 @@ nn::Result ImportMcuRtc(common::HardwareStateManager& manager) size_t bufSize = 1024; NN_LOG("AllocatableSize = %d\n", bufSize); - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize); + common::HeapManager heap(bufSize); + void* buf = heap.GetAddr(); if (buf != NULL) { common::SdReaderWriter sdReader; @@ -523,7 +524,6 @@ nn::Result ImportMcuRtc(common::HardwareStateManager& manager) } } - common::HeapManager::GetHeap()->Free(buf); NN_UTIL_RETURN_IF_FAILED(result); } else @@ -628,7 +628,8 @@ nn::Result ImportIvs() nn::fs::FileOutputStream fos; size_t bufSize = 1024; - void* enc = common::HeapManager::GetHeap()->Allocate(bufSize); + common::HeapManager encHeap(bufSize); + void* enc = encHeap.GetAddr(); if (enc != NULL) { common::SdReaderWriter sdReader; @@ -641,7 +642,8 @@ nn::Result ImportIvs() result = nn::fs::MountSpecialArchive(common::NAND_ARCHIVE_NAME, nn::fs::CTR::ARCHIVE_TYPE_CTR_NAND); if (result.IsSuccess()) { - void *dec = common::HeapManager::GetHeap()->Allocate(readSize); + common::HeapManager decHeap(bufSize); + void *dec = decHeap.GetAddr(); if (dec != NULL) { // AES復号化する @@ -665,7 +667,6 @@ nn::Result ImportIvs() } } } - common::HeapManager::GetHeap()->Free(dec); } else { @@ -677,7 +678,6 @@ nn::Result ImportIvs() } nn::fs::Unmount(common::NAND_ARCHIVE_NAME); } - common::HeapManager::GetHeap()->Free(enc); NN_UTIL_RETURN_IF_FAILED(result); } else @@ -698,31 +698,36 @@ bool ImportIvsData() void ImportThreadFunc() { nn::Result result; + s_IsImportSucceeded = true; result = nn::fs::MountSpecialArchive(common::NAND_ARCHIVE_NAME, nn::fs::CTR::ARCHIVE_TYPE_CTR_NAND); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsImportSucceeded); result = common::SdMountManager::Mount(); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsImportSucceeded); - size_t bufSize = common::HeapManager::GetHeap()->GetAllocatableSize(); + size_t bufSize = common::GetAllocatableSize(); NN_LOG("AllocatableSize = %d\n", bufSize); - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize, AES_BLOCK_SIZE); + common::HeapManager heap(bufSize); + void* buf = heap.GetAddr(); if (buf != NULL) { - - common::CopyDirectory( + if(!common::CopyDirectory( &s_FileLists, (::std::wstring(common::SDMC_ROOT_DIRECTORY_PATH) + ::std::wstring(common::SD_SAVEDATA_ROOT_NAME)).c_str(), common::NAND_DATA_ROOT_PATHNAME_WITH_SLASH, - buf, bufSize, false, NULL, NULL); - - common::HeapManager::GetHeap()->Free(buf); + buf, bufSize, false, NULL, NULL)) + { + s_IsImportSucceeded = false; + return; + } } common::SdMountManager::Unmount(); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsImportSucceeded); nn::fs::Unmount(common::NAND_ARCHIVE_NAME); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsImportSucceeded); NN_LOG("Import Thread Finalize\n"); } @@ -752,10 +757,11 @@ nn::Result ImportNorData() nn::cfg::nor::CTR::Initialize(); - size_t bufSize = common::HeapManager::GetHeap()->GetAllocatableSize() / 2; + size_t bufSize = common::GetAllocatableSize() / 2; NN_LOG("AllocatableSize = %d\n", bufSize); - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize); + common::HeapManager heap(bufSize); + void* buf = heap.GetAddr(); if (buf != NULL) { common::SdReaderWriter sdReader; @@ -813,8 +819,6 @@ nn::Result ImportNorData() } } - common::HeapManager::GetHeap()->Free(buf); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); NN_UTIL_RETURN_IF_FAILED(result); } @@ -836,10 +840,11 @@ nn::Result ReadVersionData() nn::Result result = nn::ResultSuccess(); std::memset(&s_SDVersionData, 0, sizeof(common::VerDef)); - size_t bufSize = common::HeapManager::GetHeap()->GetAllocatableSize() / 2; + size_t bufSize = common::GetAllocatableSize() / 2; NN_LOG("AllocatableSize = %d\n", bufSize); - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize); + common::HeapManager heap(bufSize); + void* buf = heap.GetAddr(); if (buf != NULL) { common::SdReaderWriter sdReader; @@ -854,11 +859,8 @@ nn::Result ReadVersionData() s_SDVersionData.cup.minorVersion, s_SDVersionData.cup.microVersion, s_SDVersionData.nup.majorVersion); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - } - common::HeapManager::GetHeap()->Free(buf); COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); NN_UTIL_RETURN_IF_FAILED(result); } @@ -1075,7 +1077,8 @@ bool ReadSetting(bool* nupOnly, bool* getIvs, bool* checkSd, bool* skipNup) common::SdMountManager::Mount(); size_t size = 10240; - void* heapAddr = common::HeapManager::GetHeap()->Allocate(size); + common::HeapManager heap(size); + void* heapAddr = heap.GetAddr(); if(heapAddr != NULL) { @@ -1439,7 +1442,6 @@ bool ReadSetting(bool* nupOnly, bool* getIvs, bool* checkSd, bool* skipNup) COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); retval = false; } - common::HeapManager::GetHeap()->Free(heapAddr); } else { @@ -1472,67 +1474,80 @@ void ImportTwlData(enum common::TWL_PATH_INDEX path, s64 fileSize) NN_ASSERT(path < common::TWL_PATHNAME_MAX); nn::Result result; + s_IsImportSucceeded = true; result = nn::fs::MountSpecialArchive(common::TWL_ARCHIVE_NAME_TABLE[path], common::TWL_FS_ARCHIVE_KIND[path]); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsImportSucceeded); result = common::SdMountManager::Mount(); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsImportSucceeded); - size_t bufSize = common::HeapManager::GetHeap()->GetAllocatableSize(); + size_t bufSize = common::GetAllocatableSize(); NN_LOG("AllocatableSize = %d\n", bufSize); // ファイルサイズ設定 common::InitializeTransferProgress(fileSize); - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize, AES_BLOCK_SIZE); + common::HeapManager heap(bufSize, AES_BLOCK_SIZE); + void* buf = heap.GetAddr(); if (buf != NULL) { wchar_t archiveName[nn::fs::MAX_FILE_PATH_LENGTH]; ::std::mbstowcs(archiveName, common::TWL_ARCHIVE_NAME_TABLE[path], std::strlen(common::TWL_ARCHIVE_NAME_TABLE[path]) + 1); std::wstring archiveString(archiveName); - common::CopyDirectory( + if(!common::CopyDirectory( &s_FileLists, (::std::wstring(common::SDMC_ROOT_DIRECTORY_PATH) + ::std::wstring(common::SD_TWL_ROOTNAME_TABLE[path])).c_str(), (archiveString + ::std::wstring(L"/")).c_str(), - buf, bufSize, false, NULL, NULL); - - common::HeapManager::GetHeap()->Free(buf); + buf, bufSize, false, NULL, NULL)) + { + s_IsImportSucceeded = false; + return; + } } common::SdMountManager::Unmount(); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsImportSucceeded); nn::fs::Unmount(common::TWL_ARCHIVE_NAME_TABLE[path]); - + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsImportSucceeded); } void ImportTwlTitleSaveData(s64 fileSize) { nn::Result result; + s_IsImportSucceeded = true; result = nn::fs::MountSpecialArchive(common::NAND_TWL_ARCHIVE_NAME, nn::fs::CTR::ARCHIVE_TYPE_TWL_NAND); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsImportSucceeded); result = common::SdMountManager::Mount(); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsImportSucceeded); - size_t bufSize = common::HeapManager::GetHeap()->GetAllocatableSize(); + size_t bufSize = common::GetAllocatableSize(); NN_LOG("AllocatableSize = %d\n", bufSize); // ファイルサイズ設定 common::InitializeTransferProgress(fileSize); - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize, AES_BLOCK_SIZE); + common::HeapManager heap(bufSize, AES_BLOCK_SIZE); + void* buf = heap.GetAddr(); if (buf != NULL) { - common::CopyDirectory( + if(!common::CopyDirectory( &s_FileLists, (::std::wstring(common::SDMC_ROOT_DIRECTORY_PATH) + ::std::wstring(common::SD_SAVEDATA_TWL_ROOT_NAME)).c_str(), (std::wstring(common::NAND_TWL_DATA_ROOT_PATHNAME_WITHOUT_SLASH) + ::std::wstring(L"/")).c_str(), - buf, bufSize, false, NULL, NULL); - - common::HeapManager::GetHeap()->Free(buf); + buf, bufSize, false, NULL, NULL)) + { + s_IsImportSucceeded = false; + return; + } } common::SdMountManager::Unmount(); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsImportSucceeded); nn::fs::Unmount(common::NAND_TWL_ARCHIVE_NAME); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsImportSucceeded); } void ImportTwlPhotoDataFunc() @@ -1576,19 +1591,6 @@ void ClearFileReadResult() s_ReadSerialNumber = false; } -void ImportPlayHistoryThreadFunc() -{ - common::PlayHistoryManager historyManager; - - COMMON_LOGGER("Import PlayHistory\n"); - historyManager.Import(); -} - -void ImportPlayHistory() -{ - s_ImportThread.Start(ImportPlayHistoryThreadFunc, s_ImportThreadStack); -} - void ExportCalData() { using namespace nn::cfg::CTR::detail; @@ -1648,8 +1650,9 @@ nn::Result ImportCalData(common::CfgCalData *data) common::SdMountManager::Mount(); - size_t bufSize = common::HeapManager::GetHeap()->GetAllocatableSize() / 2; - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize); + size_t bufSize = common::GetAllocatableSize() / 2; + common::HeapManager heap(bufSize); + void* buf = heap.GetAddr(); if (buf != NULL) { common::SdReaderWriter sdReader; @@ -1661,7 +1664,6 @@ nn::Result ImportCalData(common::CfgCalData *data) // SDから読み出し成功 std::memcpy(data, buf, readSize); } - common::HeapManager::GetHeap()->Free(buf); COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); NN_UTIL_RETURN_IF_FAILED(result); } @@ -1765,7 +1767,8 @@ nn::Result ReadFileList(SdFileSize* sdFiles, common::ImportDataList* fileList) file.Finalize(); } - void* buf = common::HeapManager::GetHeap()->Allocate(fileSize); + common::HeapManager heap(fileSize); + void* buf = heap.GetAddr(); if(buf != NULL) { result = sdReader.ReadBufWithCmac(common::FILE_LIST_PATHNAME, buf, fileSize, &readSize); @@ -1835,7 +1838,6 @@ nn::Result ReadFileList(SdFileSize* sdFiles, common::ImportDataList* fileList) } } - common::HeapManager::GetHeap()->Free(buf); } else { @@ -1846,6 +1848,11 @@ nn::Result ReadFileList(SdFileSize* sdFiles, common::ImportDataList* fileList) } +bool IsImportSucceeded() +{ + return s_IsImportSucceeded; +} + nn::Result ImportData() { static nn::Result result = nn::ResultSuccess(); diff --git a/trunk/ConsoleDataMigration/sources/ConsoleRestore/Importer.h b/trunk/ConsoleDataMigration/sources/ConsoleRestore/Importer.h index b55c9d4..5c3e05f 100644 --- a/trunk/ConsoleDataMigration/sources/ConsoleRestore/Importer.h +++ b/trunk/ConsoleDataMigration/sources/ConsoleRestore/Importer.h @@ -157,6 +157,9 @@ bool ExistsIvsDirectory(std::string& ivsRoot); // SDカードのIVSデータを書き込む bool ImportIvsData(); +// 書き込みが成功したかどうか +bool IsImportSucceeded(); + } #endif /* IMPORTER_H_ */ diff --git a/trunk/ConsoleDataMigration/sources/ConsoleRestore/OMakefile b/trunk/ConsoleDataMigration/sources/ConsoleRestore/OMakefile index 4420d61..a09ecd0 100644 --- a/trunk/ConsoleDataMigration/sources/ConsoleRestore/OMakefile +++ b/trunk/ConsoleDataMigration/sources/ConsoleRestore/OMakefile @@ -43,7 +43,6 @@ SOURCES[] = ../common/CommonLogger.cpp ../common/SdMountManager.cpp ../common/configLoader.cpp - ../common/PlayHistoryManager.cpp ../common/VersionDetect.cpp ../common/ResFont.cpp ../common/HardwareStateManager.cpp diff --git a/trunk/ConsoleDataMigration/sources/ConsoleRestore/TitleDownloader.cpp b/trunk/ConsoleDataMigration/sources/ConsoleRestore/TitleDownloader.cpp index 2019e70..469fae3 100644 --- a/trunk/ConsoleDataMigration/sources/ConsoleRestore/TitleDownloader.cpp +++ b/trunk/ConsoleDataMigration/sources/ConsoleRestore/TitleDownloader.cpp @@ -219,8 +219,9 @@ nn::Result TitleDownloader::ListUp() { COMMON_LOGGER("Read TwlTitle List.\n"); - size_t heapSize = common::HeapManager::GetHeap()->GetAllocatableSize(); - char* titleListBuf = reinterpret_cast (common::HeapManager::GetHeap()->Allocate(heapSize)); + size_t heapSize = common::GetAllocatableSize(); + common::HeapManager heap(heapSize); + char* titleListBuf = reinterpret_cast (heap.GetAddr()); size_t readSize = 0; if (titleListBuf != NULL) @@ -247,7 +248,6 @@ nn::Result TitleDownloader::ListUp() } } COMMON_LOGGER("%d Title(s) found.\n", m_TwlTiteNum); - common::HeapManager::GetHeap()->Free(titleListBuf); } else { diff --git a/trunk/ConsoleDataMigration/sources/common/CommonLogger.h b/trunk/ConsoleDataMigration/sources/common/CommonLogger.h index e16ac9a..6cabc1c 100644 --- a/trunk/ConsoleDataMigration/sources/common/CommonLogger.h +++ b/trunk/ConsoleDataMigration/sources/common/CommonLogger.h @@ -64,6 +64,35 @@ COMMON_LOGGER_RESULT_WITH_LINE(result, __LINE__, __func__); \ } \ +#define COMMON_LOGGER_RETURN_RESULT_IF_FAILED(result) \ + if(result.IsFailure()) \ + { \ + COMMON_LOGGER_RESULT_WITH_LINE(result, __LINE__, __func__); \ + return result; \ + } \ + +#define COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result) \ + if(result.IsFailure()) \ + { \ + COMMON_LOGGER_RESULT_WITH_LINE(result, __LINE__, __func__); \ + return false; \ + } \ + +#define COMMON_LOGGER_RETURN_VOID_IF_FAILED(result) \ + if(result.IsFailure()) \ + { \ + COMMON_LOGGER_RESULT_WITH_LINE(result, __LINE__, __func__); \ + return; \ + } \ + +#define COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, setbool) \ + if(result.IsFailure()) \ + { \ + COMMON_LOGGER_RESULT_WITH_LINE(result, __LINE__, __func__); \ + setbool = false; \ + return; \ + } \ + namespace common { diff --git a/trunk/ConsoleDataMigration/sources/common/FileTransfer.cpp b/trunk/ConsoleDataMigration/sources/common/FileTransfer.cpp index 5607fd2..43262c8 100644 --- a/trunk/ConsoleDataMigration/sources/common/FileTransfer.cpp +++ b/trunk/ConsoleDataMigration/sources/common/FileTransfer.cpp @@ -44,7 +44,7 @@ bool VerifyMac(nn::fs::FileInputStream* sdFile, nn::fs::FileStream* nandFile, s6 bool ConfirmFile(nn::fs::FileInputStream* from_file, nn::fs::FileStream* to_file, s64 sdFileSize, s64 nandFileSize, void* buf, size_t bufSize, const wchar_t* sdPath, const wchar_t* tmpPath, const wchar_t* truePath); void AddPkcsPadding(u8* paddingSize, void* buf, size_t bufSize, s32* readSize); -void AddPathNameAndUpdateContext(nn::fs::FileOutputStream* file, const wchar_t *str, s64 fileSize, +bool AddPathNameAndUpdateContext(nn::fs::FileOutputStream* file, const wchar_t *str, s64 fileSize, nn::crypto::Sha256Context* context); const char* GetCharStr(const wchar_t* path) @@ -65,48 +65,36 @@ nn::Result CalculateFileNum(std::wstring currentDirectory, u32& fileNum, s64& fi std::vector::iterator entryIndex; result = dir.TryInitialize(currentDirectory.c_str()); + COMMON_LOGGER_RETURN_RESULT_IF_FAILED(result); - if(result.IsSuccess()) + nn::fs::DirectoryEntry entry; + s32 numEntry; + for (;;) { - nn::fs::DirectoryEntry entry; - s32 numEntry; - for(;;) + result = dir.TryRead(&numEntry, &entry, 1); + COMMON_LOGGER_RETURN_RESULT_IF_FAILED(result); + if (numEntry == 0) { - result = dir.TryRead(&numEntry, &entry, 1); - if(result.IsFailure()) - { - dir.Finalize(); - return result; - } - if(numEntry == 0) - { - // カレントディレクトリを閉じる - dir.Finalize(); + // カレントディレクトリを閉じる + dir.Finalize(); - // カレントディレクトリの子を開く - for(entryIndex = entryList.begin(); entryIndex != entryList.end(); entryIndex++) + // カレントディレクトリの子を開く + for (entryIndex = entryList.begin(); entryIndex != entryList.end(); entryIndex++) + { + if (entryIndex->attributes.isDirectory) { - if(entryIndex->attributes.isDirectory) - { - CalculateFileNum(currentDirectory + std::wstring(entryIndex->entryName) + std::wstring(L"/"), fileNum, fileSize); - } + CalculateFileNum(currentDirectory + std::wstring(entryIndex->entryName) + std::wstring(L"/"), + fileNum, fileSize); } - - return nn::ResultSuccess(); } - entryList.push_back(entry); - fileNum++; - fileSize+= entry.entrySize; + return nn::ResultSuccess(); } - } - else - { - NN_LOG("failed initialize directory\n"); - dir.Finalize(); - return result; - } + entryList.push_back(entry); + fileNum++; + fileSize += entry.entrySize; + } } bool ExistsInList(ImportDataList* fileList, const wchar_t* path, bool isDirectory) @@ -134,15 +122,15 @@ bool ExistsInList(ImportDataList* fileList, const wchar_t* path, bool isDirector return returnValue; } -void ExportTwlSaveDirectory(const wchar_t* dirPath, nn::fs::FileOutputStream* list, +bool ExportTwlSaveDirectory(const wchar_t* dirPath, nn::fs::FileOutputStream* list, nn::crypto::Sha256Context* listContext) { NN_LOG("Create Directory %ls\n", dirPath); nn::Result result = nn::fs::TryCreateDirectory(dirPath); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); - AddPathNameAndUpdateContext(list, dirPath, -1, listContext); + return AddPathNameAndUpdateContext(list, dirPath, -1, listContext); } bool ExportTwlSaveFile(const wchar_t* from_path, const wchar_t* to_path, void* buf, const size_t bufSize, @@ -164,138 +152,100 @@ bool ExportTwlSaveFile(const wchar_t* from_path, const wchar_t* to_path, void* b // 読み込み対象ファイル開く result = from_file.TryInitialize(from_path); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); - if (result.IsFailure()) + // 読み込み対象ファイルのサイズ取得 + result = from_file.TryGetSize(&filesize); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + if (!AddPathNameAndUpdateContext(list, to_path, filesize, listContext)) { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); return false; } - else + + nn::crypto::SwAesCtrContext swAesCtrContext; + swAesCtrContext.Initialize(iv, common::key, sizeof(key)); + + size_t totalReadSize = 0; + nn::crypto::Sha256Context context; + context.Initialize(); + + // ファイルサイズをヘッダに書いておく + // 書き込み対象ファイル作成 + result = nn::fs::TryCreateFile(to_path, filesize); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + result = to_file.TryInitialize(to_path, + nn::fs::OPEN_MODE_READ | nn::fs::OPEN_MODE_WRITE | nn::fs::OPEN_MODE_CREATE); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + // フルパスをハッシュに含める + context.Update(from_path, std::wcslen(from_path) * sizeof(wchar_t)); + + BackupDataHeader header; + BackupDataHeader enc; + std::memset(&header, 0, sizeof(header)); + std::memset(&enc, 0, sizeof(enc)); + header.size = filesize; + result = swAesCtrContext.Encrypt(&enc, &header, sizeof(header)); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + context.Update(&enc, sizeof(enc)); + s32 writeSize; + result = to_file.TryWrite(&writeSize, &enc, sizeof(enc), false); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + while (1) { - // 読み込み対象ファイルのサイズ取得 - result = from_file.TryGetSize(&filesize); - if (result.IsFailure()) + // バッファの後半半分を暗号・復号用に使う + result = from_file.TryRead(&readsize, buf, bufSize / 2); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + totalReadSize += readsize; + + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + if (readsize == 0) { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; + NN_LOG("Add CMAC %ls\n", from_path); + // SHA256を計算してCMACを付加する + bit8 sha256Hash[nn::crypto::Sha256Context::HASH_SIZE]; + context.GetHash(sha256Hash); + + bit8 cmac[nn::crypto::AES_CMAC_MAC_SIZE]; + result = nn::crypto::CalculateAesCmacSw(cmac, sha256Hash, nn::crypto::Sha256Context::HASH_SIZE, + common::cmacKey); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + result = to_file.TryWrite(&writesize, cmac, sizeof(cmac)); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + result = to_file.TryFlush(); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + break; } else { - AddPathNameAndUpdateContext(list, to_path, filesize, listContext); + NN_LOG("EncryptSize = %d\n", readsize); - nn::crypto::SwAesCtrContext swAesCtrContext; - swAesCtrContext.Initialize(iv, common::key, sizeof(key)); + u8 paddingSize = 0; + AddPkcsPadding(&paddingSize, reinterpret_cast(buf), bufSize / 2, &readsize); - size_t totalReadSize = 0; - nn::crypto::Sha256Context context; - context.Initialize(); + // 暗号化後SHA256を計算しつつ書き込み + result = swAesCtrContext.Encrypt(reinterpret_cast(buf) + bufSize / 2, buf, readsize); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + context.Update(reinterpret_cast(buf) + bufSize / 2, readsize); - // ファイルサイズをヘッダに書いておく - // 書き込み対象ファイル作成 - result = nn::fs::TryCreateFile(to_path, filesize); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - result = to_file.TryInitialize(to_path, - nn::fs::OPEN_MODE_READ | nn::fs::OPEN_MODE_WRITE | nn::fs::OPEN_MODE_CREATE); - if (result.IsFailure()) - { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; - } + result = to_file.TryWrite(&writesize, reinterpret_cast(buf) + bufSize / 2, readsize, false); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); - // フルパスをハッシュに含める - context.Update(from_path, std::wcslen(from_path) * sizeof(wchar_t)); + // 事前計算したファイルサイズに一致させるためパディング分減算 + readsize -= paddingSize; - BackupDataHeader header; - BackupDataHeader enc; - std::memset(&header, 0, sizeof(header)); - std::memset(&enc, 0, sizeof(enc)); - header.size = filesize; - result = swAesCtrContext.Encrypt(&enc, &header, sizeof(header)); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - context.Update(&enc, sizeof(enc)); - s32 writeSize; - result = to_file.TryWrite(&writeSize, &enc, sizeof(enc), false); - if (result.IsFailure()) - { - return false; - } - - while (1) - { - // バッファの後半半分を暗号・復号用に使う - result = from_file.TryRead(&readsize, buf, bufSize / 2); - totalReadSize += readsize; - - if (result.IsFailure()) - { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; - } - else - { - if (readsize == 0) - { - NN_LOG("Add CMAC %ls\n", from_path); - // SHA256を計算してCMACを付加する - bit8 sha256Hash[nn::crypto::Sha256Context::HASH_SIZE]; - context.GetHash(sha256Hash); - - bit8 cmac[nn::crypto::AES_CMAC_MAC_SIZE]; - result = nn::crypto::CalculateAesCmacSw(cmac, sha256Hash, nn::crypto::Sha256Context::HASH_SIZE, - common::cmacKey); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - - result = to_file.TryWrite(&writesize, cmac, sizeof(cmac)); - if (result.IsFailure()) - { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; - } - - result = to_file.TryFlush(); - - if (result.IsFailure()) - { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; - } - - break; - } - else - { - NN_LOG("EncryptSize = %d\n", readsize); - - u8 paddingSize = 0; - AddPkcsPadding(&paddingSize, reinterpret_cast (buf), bufSize / 2, &readsize); - - // 暗号化後SHA256を計算しつつ書き込み - result = swAesCtrContext.Encrypt(reinterpret_cast (buf) + bufSize / 2, buf, readsize); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - context.Update(reinterpret_cast (buf) + bufSize / 2, readsize); - - result = to_file.TryWrite(&writesize, reinterpret_cast (buf) + bufSize / 2, readsize, - false); - - // 事前計算したファイルサイズに一致させるためパディング分減算 - readsize -= paddingSize; - - s_FinishedFileSize += readsize; - s_Progress = s_FinishedFileSize * 100 / s_TotalFileSize; - NN_LOG("finish = %lld, total = %lld, progress = %lld\n", s_FinishedFileSize, s_TotalFileSize, s_Progress); - - if (result.IsFailure()) - { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; - } - - } - } - } - to_file.Finalize(); + s_FinishedFileSize += readsize; + s_Progress = s_FinishedFileSize * 100 / s_TotalFileSize; + NN_LOG( "finish = %lld, total = %lld, progress = %lld\n", s_FinishedFileSize, s_TotalFileSize, s_Progress); } } + to_file.Finalize(); + from_file.Finalize(); return true; @@ -312,12 +262,7 @@ bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wc std::wostringstream target_to; nn::Result result = from_dir.TryInitialize(from_path); - - if (result.IsFailure()) - { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; - } + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); while (1) { @@ -362,7 +307,10 @@ bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wc target_to << L"/"; if(encode) { - AddPathNameAndUpdateContext(list, target_to.str().c_str(), -1, listContext); + if(!AddPathNameAndUpdateContext(list, target_to.str().c_str(), -1, listContext)) + { + return false; + } } // 再帰処理 @@ -373,8 +321,7 @@ bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wc } else { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); } } // ファイルの場合 @@ -405,238 +352,195 @@ bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wc // 読み込み対象ファイル開く result = from_file.TryInitialize(target_from.str().c_str()); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); - if (result.IsFailure()) + // 読み込み対象ファイルのサイズ取得 + result = from_file.TryGetSize(&filesize); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + if (encode) { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; + if (!AddPathNameAndUpdateContext(list, target_to.str().c_str(), filesize, listContext)) + { + return false; + } + } + + nn::crypto::SwAesCtrContext swAesCtrContext; + swAesCtrContext.Initialize(iv, common::key, sizeof(key)); + + size_t totalReadSize = 0; + nn::crypto::Sha256Context context; + context.Initialize(); + + // ファイルサイズをヘッダに書いておく + if (encode) + { + // 書き込み対象ファイル作成 + result = nn::fs::TryCreateFile(target_tmp.str().c_str(), filesize); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + result = to_file.TryInitialize(target_tmp.str().c_str(), + nn::fs::OPEN_MODE_READ | nn::fs::OPEN_MODE_WRITE | nn::fs::OPEN_MODE_CREATE); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + // フルパスをハッシュに含める + context.Update(target_from.str().c_str(), target_from.str().size() * sizeof(wchar_t)); + + BackupDataHeader header; + BackupDataHeader enc; + std::memset(&header, 0, sizeof(header)); + std::memset(&enc, 0, sizeof(enc)); + header.size = filesize; + result = swAesCtrContext.Encrypt(&enc, &header, sizeof(header)); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + context.Update(&enc, sizeof(enc)); + s32 writeSize; + result = to_file.TryWrite(&writeSize, &enc, sizeof(enc), false); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); } else { - // 読み込み対象ファイルのサイズ取得 - result = from_file.TryGetSize(&filesize); - if (result.IsFailure()) + // ヘッダを読む + // ハッシュの計算は終わっているので復号化のみ + BackupDataHeader header; + BackupDataHeader dec; + std::memset(&header, 0, sizeof(header)); + std::memset(&dec, 0, sizeof(dec)); + s32 readSize; + result = from_file.TryRead(&readSize, &header, sizeof(header)); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + swAesCtrContext.Decrypt(&dec, &header, sizeof(header)); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + fileSizeWithoutHeaderAndFooter = dec.size; + + // 書き込み対象ファイル作成 + result = nn::fs::TryCreateFile(target_tmp.str().c_str(), fileSizeWithoutHeaderAndFooter); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + result = to_file.TryInitialize(target_tmp.str().c_str(), + nn::fs::OPEN_MODE_READ | nn::fs::OPEN_MODE_WRITE | nn::fs::OPEN_MODE_CREATE); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + } + + while (1) + { + // バッファの後半半分を暗号・復号用に使う + result = from_file.TryRead(&readsize, buf, bufSize / 2); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + totalReadSize += readsize; + + if (readsize == 0) { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; + if (encode) + { + NN_LOG("Add CMAC %ls\n", target_from.str().c_str()); + // SHA256を計算してCMACを付加する + bit8 sha256Hash[nn::crypto::Sha256Context::HASH_SIZE]; + context.GetHash(sha256Hash); + + bit8 cmac[nn::crypto::AES_CMAC_MAC_SIZE]; + result = nn::crypto::CalculateAesCmacSw(cmac, sha256Hash, nn::crypto::Sha256Context::HASH_SIZE, + common::cmacKey); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + result = to_file.TryWrite(&writesize, cmac, sizeof(cmac)); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + } + + result = to_file.TryFlush(); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + // 復号済みなら検証する + if (!encode) + { + if (!ConfirmFile(&from_file, &to_file, filesize, fileSizeWithoutHeaderAndFooter, buf, bufSize, + target_from.str().c_str(), target_tmp.str().c_str(), target_to.str().c_str())) + { + return false; + } + } + + break; } else { - if(encode) - { - AddPathNameAndUpdateContext(list, target_to.str().c_str(), filesize, listContext); - } - - nn::crypto::SwAesCtrContext swAesCtrContext; - swAesCtrContext.Initialize(iv, common::key, sizeof(key)); - - size_t totalReadSize = 0; - nn::crypto::Sha256Context context; - context.Initialize(); - - // ファイルサイズをヘッダに書いておく if (encode) { - // 書き込み対象ファイル作成 - result = nn::fs::TryCreateFile(target_tmp.str().c_str(), filesize); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - result = to_file.TryInitialize(target_tmp.str().c_str(), - nn::fs::OPEN_MODE_READ | nn::fs::OPEN_MODE_WRITE | nn::fs::OPEN_MODE_CREATE); - if (result.IsFailure()) - { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; - } + NN_LOG("EncryptSize = %d\n", readsize); - // フルパスをハッシュに含める - context.Update(target_from.str().c_str(), target_from.str().size() * sizeof(wchar_t)); + u8 paddingSize = 0; + AddPkcsPadding(&paddingSize, reinterpret_cast(buf), bufSize / 2, &readsize); - BackupDataHeader header; - BackupDataHeader enc; - std::memset(&header, 0, sizeof(header)); - std::memset(&enc, 0, sizeof(enc)); - header.size = filesize; - result = swAesCtrContext.Encrypt(&enc, &header, sizeof(header)); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - context.Update(&enc, sizeof(enc)); - s32 writeSize; - result = to_file.TryWrite(&writeSize, &enc, sizeof(enc), false); - if (result.IsFailure()) - { - return false; - } + // 暗号化後SHA256を計算しつつ書き込み + result = swAesCtrContext.Encrypt(reinterpret_cast(buf) + bufSize / 2, buf, readsize); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + context.Update(reinterpret_cast(buf) + bufSize / 2, readsize); + + result = to_file.TryWrite(&writesize, reinterpret_cast(buf) + bufSize / 2, readsize, + false); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + // 事前計算したファイルサイズに一致させるためパディング分減算 + readsize -= paddingSize; + + s_FinishedFileSize += readsize; + s_Progress = s_FinishedFileSize * 100 / s_TotalFileSize; + NN_LOG( + "finish = %lld, total = %lld, progress = %lld\n", s_FinishedFileSize, s_TotalFileSize, s_Progress); } else { - // ヘッダを読む - // ハッシュの計算は終わっているので復号化のみ - BackupDataHeader header; - BackupDataHeader dec; - std::memset(&header, 0, sizeof(header)); - std::memset(&dec, 0, sizeof(dec)); - s32 readSize; - result = from_file.TryRead(&readSize, &header, sizeof(header)); - if (result.IsFailure()) - { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; - } - swAesCtrContext.Decrypt(&dec, &header, sizeof(header)); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - fileSizeWithoutHeaderAndFooter = dec.size; + // ハッシュ検証は通っているので復号化しつつ書き込み + // パディング以降は書き込まないよう書き込みサイズを変更する - // 書き込み対象ファイル作成 - result = nn::fs::TryCreateFile(target_tmp.str().c_str(), fileSizeWithoutHeaderAndFooter); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - result = to_file.TryInitialize(target_tmp.str().c_str(), - nn::fs::OPEN_MODE_READ | nn::fs::OPEN_MODE_WRITE | nn::fs::OPEN_MODE_CREATE); - if (result.IsFailure()) + NN_LOG("DecryptSize = %d\n", readsize); + result = swAesCtrContext.Decrypt(reinterpret_cast(buf) + bufSize / 2, buf, readsize); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + // パディングまで読んだかどうか + bool readDone = false; + // パディングまで読んでいたら書き込みサイズを減らす + if (fileSizeWithoutHeaderAndFooter < totalReadSize) { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; + readsize -= totalReadSize - fileSizeWithoutHeaderAndFooter; + readDone = true; + } + + result = to_file.TryWrite(&writesize, reinterpret_cast(buf) + bufSize / 2, readsize, + false); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + s_FinishedFileSize += readsize; + s_Progress = s_FinishedFileSize * 100 / s_TotalFileSize; + NN_LOG( + "finish = %lld, total = %lld, progress = %lld\n", s_FinishedFileSize, s_TotalFileSize, s_Progress); + + // 読みきったので次のファイルへ + if (readDone) + { + result = to_file.TryFlush(); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + if (!ConfirmFile(&from_file, &to_file, filesize, fileSizeWithoutHeaderAndFooter, buf, + bufSize, target_from.str().c_str(), target_tmp.str().c_str(), + target_to.str().c_str())) + { + return false; + } + break; } } - while (1) + if (result.IsFailure()) { - // バッファの後半半分を暗号・復号用に使う - result = from_file.TryRead(&readsize, buf, bufSize / 2); - totalReadSize += readsize; - - if (result.IsFailure()) - { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; - } - else - { - if (readsize == 0) - { - if (encode) - { - NN_LOG("Add CMAC %ls\n", target_from.str().c_str()); - // SHA256を計算してCMACを付加する - bit8 sha256Hash[nn::crypto::Sha256Context::HASH_SIZE]; - context.GetHash(sha256Hash); - - bit8 cmac[nn::crypto::AES_CMAC_MAC_SIZE]; - result = nn::crypto::CalculateAesCmacSw(cmac, sha256Hash, - nn::crypto::Sha256Context::HASH_SIZE, common::cmacKey); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - - result = to_file.TryWrite(&writesize, cmac, sizeof(cmac)); - if (result.IsFailure()) - { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; - - } - } - - result = to_file.TryFlush(); - - if (result.IsFailure()) - { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; - } - - // 復号済みなら検証する - if (!encode) - { - if (!ConfirmFile(&from_file, &to_file, filesize, fileSizeWithoutHeaderAndFooter, - buf, bufSize, target_from.str().c_str(), target_tmp.str().c_str(), - target_to.str().c_str())) - { - return false; - } - } - - break; - } - else - { - if (encode) - { - NN_LOG("EncryptSize = %d\n", readsize); - - u8 paddingSize = 0; - AddPkcsPadding(&paddingSize, reinterpret_cast(buf), bufSize / 2, &readsize); - - // 暗号化後SHA256を計算しつつ書き込み - result = swAesCtrContext.Encrypt(reinterpret_cast (buf) + bufSize / 2, buf, - readsize); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - context.Update(reinterpret_cast (buf) + bufSize / 2, readsize); - - result = to_file.TryWrite(&writesize, reinterpret_cast (buf) + bufSize / 2, - readsize, false); - - // 事前計算したファイルサイズに一致させるためパディング分減算 - readsize -= paddingSize; - - s_FinishedFileSize += readsize; - s_Progress = s_FinishedFileSize * 100 / s_TotalFileSize; - NN_LOG("finish = %lld, total = %lld, progress = %lld\n", s_FinishedFileSize, s_TotalFileSize, s_Progress); - } - else - { - // ハッシュ検証は通っているので復号化しつつ書き込み - // パディング以降は書き込まないよう書き込みサイズを変更する - - NN_LOG("DecryptSize = %d\n", readsize); - result = swAesCtrContext.Decrypt(reinterpret_cast (buf) + bufSize / 2, buf, - readsize); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - - // パディングまで読んだかどうか - bool readDone = false; - // パディングまで読んでいたら書き込みサイズを減らす - if (fileSizeWithoutHeaderAndFooter < totalReadSize) - { - readsize -= totalReadSize - fileSizeWithoutHeaderAndFooter; - readDone = true; - } - - result = to_file.TryWrite(&writesize, reinterpret_cast (buf) + bufSize / 2, - readsize, false); - s_FinishedFileSize += readsize; - s_Progress = s_FinishedFileSize * 100 / s_TotalFileSize; - NN_LOG("finish = %lld, total = %lld, progress = %lld\n", s_FinishedFileSize, s_TotalFileSize, s_Progress); - - // 読みきったので次のファイルへ - if (readDone) - { - result = to_file.TryFlush(); - if (result.IsFailure()) - { - return false; - } - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - - if (!ConfirmFile(&from_file, &to_file, filesize, - fileSizeWithoutHeaderAndFooter, buf, bufSize, - target_from.str().c_str(), target_tmp.str().c_str(), - target_to.str().c_str())) - { - return false; - } - break; - } - } - - if (result.IsFailure()) - { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; - } - - } - } + COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + return false; } - to_file.Finalize(); + } } + to_file.Finalize(); + from_file.Finalize(); } } @@ -666,11 +570,7 @@ bool CalculateAndCompareCmac(nn::crypto::Sha256Context* context, bit8* sdCmac) context->Finalize(); result = nn::crypto::CalculateAesCmacSw(cmac, sha256Hash, sizeof(sha256Hash), common::cmacKey); - if (result.IsFailure()) - { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; - } + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); return std::memcmp(cmac, sdCmac, sizeof(cmac)) == 0; } @@ -727,7 +627,7 @@ bool VerifyMac(nn::fs::FileInputStream* sdFile, nn::fs::FileStream* nandFile, s6 std::memset(&enc, 0, sizeof(enc)); header.size = nandFileSize; result = swAesCtrContext.Encrypt(&enc, &header, sizeof(header)); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); context.Update(&enc, sizeof(enc)); bool retValue = false; @@ -736,8 +636,6 @@ bool VerifyMac(nn::fs::FileInputStream* sdFile, nn::fs::FileStream* nandFile, s6 while (1) { result = nandFile->TryRead(&readSize, buf, bufSize / 2); - totalReadSize += readSize; - if (result.IsFailure()) { COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); @@ -746,6 +644,8 @@ bool VerifyMac(nn::fs::FileInputStream* sdFile, nn::fs::FileStream* nandFile, s6 } else { + totalReadSize += readSize; + if (readSize == 0) { retValue = CalculateAndCompareCmac(&context, sdCmac); @@ -756,7 +656,7 @@ bool VerifyMac(nn::fs::FileInputStream* sdFile, nn::fs::FileStream* nandFile, s6 u8 paddingSize = 0; AddPkcsPadding(&paddingSize, reinterpret_cast(buf), bufSize / 2, &readSize); result = swAesCtrContext.Encrypt(reinterpret_cast(buf) + bufSize / 2, buf, readSize); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); context.Update(reinterpret_cast(buf) + bufSize / 2 , readSize); if (result.IsFailure()) @@ -786,8 +686,7 @@ bool ConfirmFile(nn::fs::FileInputStream* from_file, nn::fs::FileStream* to_file s_FinishedFileSize -= nandFileSize; COMMON_LOGGER("**********Verification Failed %s, Delete**********\n", GetCharStr(sdPath)); result = nn::fs::TryDeleteFile(tmpPath); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); } else { @@ -797,8 +696,7 @@ bool ConfirmFile(nn::fs::FileInputStream* from_file, nn::fs::FileStream* to_file if (result.IsFailure() && !nn::fs::ResultNotFound::Includes(result)) { COMMON_LOGGER("**********Verification Failed %s, Delete**********\n", GetCharStr(sdPath)); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return false; + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); } // 正しいファイル名にリネームする @@ -846,7 +744,7 @@ bool ContainsNimSaveDataDir(const wchar_t* str) //! @param[in] str 入力文字列 //! @param[in] fileSize サイズ //! @param[in] context SHA-256計算用コンテキスト -void AddPathNameAndUpdateContext(nn::fs::FileOutputStream* file, const wchar_t *str, s64 fileSize, +bool AddPathNameAndUpdateContext(nn::fs::FileOutputStream* file, const wchar_t *str, s64 fileSize, nn::crypto::Sha256Context* context) { nn::Result result; @@ -854,30 +752,32 @@ void AddPathNameAndUpdateContext(nn::fs::FileOutputStream* file, const wchar_t * if(ContainsNimSaveDataDir(str)) { - return; + return true; } std::string output(GetCharStr(str)); result = file->TryWrite(&writeSize, output.c_str(), output.size(), true); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); context->Update(output.c_str(), output.size()); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); char comma = ','; result = file->TryWrite(&writeSize, &comma, sizeof(comma), true); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); context->Update(&comma, sizeof(comma)); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); char sizeStr[10]; std::memset(sizeStr, 0, sizeof(sizeStr)); s32 sizeStrSize = std::snprintf(sizeStr, sizeof(sizeStr), "%lld", fileSize); result = file->TryWrite(&writeSize, sizeStr, sizeStrSize, true); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); context->Update(sizeStr, sizeStrSize); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); char newLine = '\n'; result = file->TryWrite(&writeSize, &newLine, sizeof(newLine), true); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); context->Update(&newLine, sizeof(newLine)); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + + return true; } } diff --git a/trunk/ConsoleDataMigration/sources/common/FileTransfer.h b/trunk/ConsoleDataMigration/sources/common/FileTransfer.h index 625b65a..e55f3bf 100644 --- a/trunk/ConsoleDataMigration/sources/common/FileTransfer.h +++ b/trunk/ConsoleDataMigration/sources/common/FileTransfer.h @@ -29,7 +29,7 @@ nn::Result CalculateFileNum(std::wstring currentDirectory, u32& fileNum, s64& fi // 単一のディレクトリを作成する // アーカイブはマウント済みにしておく -void ExportTwlSaveDirectory(const wchar_t* dirPath, nn::fs::FileOutputStream* list, +bool ExportTwlSaveDirectory(const wchar_t* dirPath, nn::fs::FileOutputStream* list, nn::crypto::Sha256Context* listContext); // 単一のファイルをコピーする diff --git a/trunk/ConsoleDataMigration/sources/common/HeapManager.cpp b/trunk/ConsoleDataMigration/sources/common/HeapManager.cpp index 22b8e87..1930fc2 100644 --- a/trunk/ConsoleDataMigration/sources/common/HeapManager.cpp +++ b/trunk/ConsoleDataMigration/sources/common/HeapManager.cpp @@ -18,21 +18,47 @@ namespace common { -nn::fnd::ThreadSafeExpHeap HeapManager::s_AppHeap; +nn::fnd::ThreadSafeExpHeap s_AppHeap; -HeapManager::HeapManager() + +HeapManager::HeapManager(size_t byteSize, s32 alignment, bit8 groupId, nn::fnd::ExpHeapBase::AllocationMode mode, bool reuse) { - + m_Ptr = s_AppHeap.Allocate(byteSize, alignment, groupId, mode, reuse); } HeapManager::~HeapManager() { - + if(m_Ptr != NULL) + { + s_AppHeap.Free(m_Ptr); + } } - -nn::fnd::ThreadSafeExpHeap* HeapManager::GetHeap() +void* HeapManager::GetAddr() { - return &s_AppHeap; + return m_Ptr; +} + +void InitializeHeap() +{ + s_AppHeap.Initialize(nn::os::GetDeviceMemoryAddress(), nn::os::GetDeviceMemorySize(), nn::os::ALLOCATE_OPTION_LINEAR); +} + +size_t GetAllocatableSize(s32 alignment) +{ + return s_AppHeap.GetAllocatableSize(alignment); +} + +void* ForceAllocate(size_t byteSize, s32 alignment, bit8 groupId, nn::fnd::ExpHeapBase::AllocationMode mode, bool reuse) +{ + return s_AppHeap.Allocate(byteSize, alignment, groupId, mode, reuse); +} + +void ForceFree(void* ptr) +{ + if(ptr != NULL) + { + s_AppHeap.Free(ptr); + } } } diff --git a/trunk/ConsoleDataMigration/sources/common/HeapManager.h b/trunk/ConsoleDataMigration/sources/common/HeapManager.h index b9fc469..eb8da76 100644 --- a/trunk/ConsoleDataMigration/sources/common/HeapManager.h +++ b/trunk/ConsoleDataMigration/sources/common/HeapManager.h @@ -17,6 +17,7 @@ #define HEAPMANAGER_H_ #include +#include namespace common { @@ -24,17 +25,27 @@ namespace common class HeapManager { public: - HeapManager(); + explicit HeapManager(size_t byteSize, s32 alignment = nn::fnd::ExpHeapBase::DEFAULT_ALIGNMENT, bit8 groupId = 0, + nn::fnd::ExpHeapBase::AllocationMode mode = nn::fnd::ExpHeapBase::ALLOCATION_MODE_FIRST_FIT, bool reuse = false); virtual ~HeapManager(); - static nn::fnd::ThreadSafeExpHeap* GetHeap(); + void* GetAddr(); private: - // デバイスメモリ管理用拡張ヒープ - static nn::fnd::ThreadSafeExpHeap s_AppHeap; + void* m_Ptr; }; +void InitializeHeap(); +size_t GetAllocatableSize(s32 alignment = nn::fnd::ExpHeapBase::DEFAULT_ALIGNMENT); + +// HeapManagerを使わず確保する場合のみ +void* ForceAllocate(size_t byteSize, s32 alignment = nn::fnd::ExpHeapBase::DEFAULT_ALIGNMENT, bit8 groupId = 0, + nn::fnd::ExpHeapBase::AllocationMode mode = nn::fnd::ExpHeapBase::ALLOCATION_MODE_FIRST_FIT, bool reuse = false); + +// HeapManagerを使わず解放する場合のみ +void ForceFree(void* ptr); + } // namespace common #endif /* HEAPMANAGER_H_ */ diff --git a/trunk/ConsoleDataMigration/sources/common/PlayHistoryManager.cpp b/trunk/ConsoleDataMigration/sources/common/PlayHistoryManager.cpp deleted file mode 100644 index c846745..0000000 --- a/trunk/ConsoleDataMigration/sources/common/PlayHistoryManager.cpp +++ /dev/null @@ -1,224 +0,0 @@ -/*---------------------------------------------------------------------------* - Project: Horizon - File: PlayHistoryManager.cpp - - Copyright 2009 Nintendo. All rights reserved. - - These coded instructions, statements, and computer programs contain - proprietary information of Nintendo of America Inc. and/or Nintendo - Company Ltd., and are protected by Federal copyright law. They may - not be disclosed to third parties or copied or duplicated in any form, - in whole or in part, without the prior written consent of Nintendo. - - $Rev$ - *---------------------------------------------------------------------------*/ - -#include -#include - -#include "PlayHistoryManager.h" -#include "HeapManager.h" -#include "SdMountManager.h" -#include "SdReaderWriter.h" -#include "CommonLogger.h" -#include "FileName.h" -#include "ProgramId.h" - -namespace common -{ - -PlayHistoryManager::PlayHistoryManager() -{ - - -} - -PlayHistoryManager::~PlayHistoryManager() -{ - -} - -void PlayHistoryManager::Export() -{ - nn::Result result; - SdMountManager::Mount(); - SdReaderWriter sd; - - size_t historyNum = nn::pl::CTR::GetPlayHistoryLength(); - if ( historyNum == 0) - { - NN_LOG("No Play Event\n"); - return; - } - - // SDに書き込む - result = sd.WriteBufWithCmac(common::PLAYHISTORY_COUNT_PATHNAME, reinterpret_cast(&historyNum), - sizeof(historyNum)); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - - nn::pl::CTR::PlayEvent* pEvent; - pEvent = reinterpret_cast (HeapManager::GetHeap()->Allocate( - sizeof(nn::pl::CTR::PlayEvent) * historyNum)); - if ( pEvent == NULL ) - { - NN_LOG("Failed to Allocate Heap\n"); - return; - } - - // 一番古いデータから素直に全部読み込む - nn::pl::CTR::GetPlayHistory(pEvent, 0, historyNum); - - // SDに書き込む - result = sd.WriteBufWithCmac(common::PLAYHISTORY_PATHNAME, reinterpret_cast(pEvent), - sizeof(nn::pl::CTR::PlayEvent) * historyNum); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - - HeapManager::GetHeap()->Free(pEvent); - - SdMountManager::Unmount(); -} - -nn::Result PlayHistoryManager::GetPlayHistoryNums(size_t* nums) -{ - nn::Result result = nn::ResultSuccess(); - - size_t bufSize = common::HeapManager::GetHeap()->GetAllocatableSize(); - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize); - if (buf != NULL) - { - common::SdReaderWriter sdReader; - - size_t readSize; - result = sdReader.ReadBufWithCmac(common::PLAYHISTORY_COUNT_PATHNAME, buf, bufSize, &readSize); - if (result.IsSuccess()) - { - *nums = *reinterpret_cast (buf); - } - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - - HeapManager::GetHeap()->Free(buf); - } - return result; -} - -nn::Result PlayHistoryManager::Import() -{ - nn::Result result = nn::ResultSuccess(); - SdMountManager::Mount(); - SdReaderWriter sd; - - SdMountManager::Mount(); - - // プレイ履歴の件数を取得する - size_t historyNum = 0; - - GetPlayHistoryNums(&historyNum); - - if(historyNum == 0) - { - NN_LOG("No PlayHistory found\n"); - return nn::Result(nn::Result::LEVEL_PERMANENT, nn::Result::SUMMARY_NOT_FOUND, nn::Result::MODULE_COMMON, - nn::Result::DESCRIPTION_NOT_FOUND); - } - - size_t bufSize = common::HeapManager::GetHeap()->GetAllocatableSize(); - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize); - if (buf != NULL) - { - common::SdReaderWriter sdReader; - - size_t readSize; - result = sdReader.ReadBufWithCmac(common::PLAYHISTORY_PATHNAME, buf, bufSize, &readSize); - if(result.IsSuccess()) - { - nn::pl::CTR::PlayEvent* pEvent = reinterpret_cast(buf); - NN_LOG("history num = %d\n", historyNum); - - // プレイ履歴を無効化する - nn::pl::CTR::ClearPlayHistory(); - - // データ移行ツールを無視して書き込む - for(u32 i = 0; i < historyNum; i++) - { - COMMON_LOGGER("Importing PlayHistory %d/%d", i, historyNum); - if ((pEvent[i].GetProgramId() & WITHOUT_VALIATION_MASK) != CONSOLE_BACKUP_PROGRAM_ID - && (pEvent[i].GetProgramId() & WITHOUT_VALIATION_MASK) != CONSOLE_RESTORE_PROGRAM_ID) - { - NN_LOG("%05d 0x%16llx : %X : %d\n", i, pEvent[i].GetProgramId(), pEvent[i].GetEventType(), - pEvent[i].minutes); - - nn::pl::CTR::NotifyPlayEvent(pEvent[i].GetEventType(), pEvent[i].GetProgramId(), - pEvent[i].GetEventTime()); - } - else - { - NN_LOG("Skipped\n"); - } - } - - } - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - - common::HeapManager::GetHeap()->Free(buf); - } - else - { - NN_LOG("Failed Allocate Heap!! %s, %d", __FILE__, __LINE__); - return result; - } - - SdMountManager::Unmount(); - - return result; -} - -void PlayHistoryManager::Dump() -{ - nn::Result result; - - s32 playEventLength = nn::pl::CTR::GetPlayHistoryLength(); - if ( playEventLength == 0 ) - { - NN_LOG("No Play Event\n"); - return; - } - - nn::pl::CTR::PlayEvent* playEvent = reinterpret_cast (HeapManager::GetHeap()->Allocate( - sizeof(nn::pl::CTR::PlayEvent) * playEventLength)); - if ( playEvent == NULL ) - { - NN_LOG("Failed to Allocate Heap\n"); - return; - } - - // 一番古いデータから素直に全部読み込む - (void)nn::pl::CTR::GetPlayHistory(playEvent, 0, playEventLength); - - // 取得したデータを順番にすべて表示していく - s32 restPlayEventLength = playEventLength; - s32 displayCount = 0; - - nn::pl::CTR::PlayEvent* pEvent; - while ( restPlayEventLength > 0 ) - { - // 一気に表示できるところまで表示する - int line; - for ( line = 0; line < 24; line++ ) - { - pEvent = &playEvent[displayCount]; - NN_LOG("0x%16llx : %X : %d\n", pEvent->GetProgramId(), pEvent->eventType, pEvent->minutes); - restPlayEventLength--; - displayCount++; - if(restPlayEventLength <= 0) - { - break; - } - } - } - NN_UNUSED_VAR(pEvent); - - HeapManager::GetHeap()->Free(playEvent); - return; -} - -} diff --git a/trunk/ConsoleDataMigration/sources/common/PlayHistoryManager.h b/trunk/ConsoleDataMigration/sources/common/PlayHistoryManager.h deleted file mode 100644 index 73afb52..0000000 --- a/trunk/ConsoleDataMigration/sources/common/PlayHistoryManager.h +++ /dev/null @@ -1,45 +0,0 @@ -/*---------------------------------------------------------------------------* - Project: Horizon - File: PlayHistoryManager.h - - Copyright 2009 Nintendo. All rights reserved. - - These coded instructions, statements, and computer programs contain - proprietary information of Nintendo of America Inc. and/or Nintendo - Company Ltd., and are protected by Federal copyright law. They may - not be disclosed to third parties or copied or duplicated in any form, - in whole or in part, without the prior written consent of Nintendo. - - $Rev$ - *---------------------------------------------------------------------------*/ - -#ifndef PLAYHISTORYMANAGER_H_ -#define PLAYHISTORYMANAGER_H_ - -namespace common -{ - -//! @brief データ移行ツールの履歴を消しながらプレイ履歴を移行するためのクラスです。 -class PlayHistoryManager -{ -public: - PlayHistoryManager(); - virtual ~PlayHistoryManager(); - - //! @brief SDカードに出力します。 - void Export(); - - //! @brief SDカードからプレイ履歴に書き込みます - nn::Result Import(); - - //! @brief デバッグ用。プレイ履歴をデバッグ出力します。 - void Dump(); - -private: - nn::Result GetPlayHistoryNums(size_t* nums); - -}; - -} - -#endif /* PLAYHISTORYMANAGER_H_ */ diff --git a/trunk/ConsoleDataMigration/sources/common/ResFont.cpp b/trunk/ConsoleDataMigration/sources/common/ResFont.cpp index b0183df..a9a8a79 100644 --- a/trunk/ConsoleDataMigration/sources/common/ResFont.cpp +++ b/trunk/ConsoleDataMigration/sources/common/ResFont.cpp @@ -63,7 +63,7 @@ InitShaders(nn::font::RectDrawer* pDrawer) const u32 fileSize = (u32)shaderReader.GetSize(); - void* shaderBinary = HeapManager::GetHeap()->Allocate(fileSize); + void* shaderBinary = ForceAllocate(fileSize); NN_NULL_ASSERT(shaderBinary); #ifndef NN_BUILD_RELEASE @@ -74,12 +74,10 @@ InitShaders(nn::font::RectDrawer* pDrawer) const u32 vtxBufCmdBufSize = nn::font::RectDrawer::GetVertexBufferCommandBufferSize(shaderBinary, fileSize); - void *const vtxBufCmdBuf = HeapManager::GetHeap()->Allocate(vtxBufCmdBufSize); + void *const vtxBufCmdBuf = ForceAllocate(vtxBufCmdBufSize); NN_NULL_ASSERT(vtxBufCmdBuf); pDrawer->Initialize(vtxBufCmdBuf, shaderBinary, fileSize); - HeapManager::GetHeap()->Free(shaderBinary); - return vtxBufCmdBuf; } @@ -180,7 +178,7 @@ InitFont( return false; } - void* buffer = HeapManager::GetHeap()->Allocate(fileSize, nn::font::GlyphDataAlignment); + void* buffer = common::ForceAllocate(fileSize, nn::font::GlyphDataAlignment); if (buffer == NULL) { return false; @@ -189,7 +187,7 @@ InitFont( s32 readSize = fontReader.Read(buffer, fileSize); if (readSize != fileSize) { - HeapManager::GetHeap()->Free(buffer); + common::ForceFree(buffer); return false; } @@ -200,12 +198,12 @@ InitFont( //--- 既にリソースをセット済みであるか,ロード済みであるか、リソースが不正な場合に失敗します。 if (! bSuccess) { - HeapManager::GetHeap()->Free(buffer); + common::ForceFree(buffer); } // 描画用バッファを設定します。 const u32 drawBufferSize = nn::font::ResFont::GetDrawBufferSize(buffer); - void* drawBuffer = HeapManager::GetHeap()->Allocate(drawBufferSize, 4); + void* drawBuffer = common::ForceAllocate(drawBufferSize, 4); NN_NULL_ASSERT(drawBuffer); pFont->SetDrawBuffer(drawBuffer); @@ -226,7 +224,7 @@ CleanupFont(nn::font::ResFont* pFont) void *const drawBuffer = pFont->SetDrawBuffer(NULL); if (drawBuffer != NULL) { - HeapManager::GetHeap()->Free(drawBuffer); + ForceFree(drawBuffer); } // フォントがセットされているなら SetResource 時に渡したリソースへの @@ -234,7 +232,7 @@ CleanupFont(nn::font::ResFont* pFont) void *const resource = pFont->RemoveResource(); if (resource != NULL) { - HeapManager::GetHeap()->Free(resource); + ForceFree(resource); } // RemoveResource 後は再度 SetResource するまでフォントとして使用できません。 @@ -251,7 +249,7 @@ nn::font::DispStringBuffer* AllocDispStringBuffer(s32 charMax) { const u32 DrawBufferSize = nn::font::CharWriter::GetDispStringBufferSize(charMax); - void *const bufMem = HeapManager::GetHeap()->Allocate(DrawBufferSize); + void *const bufMem = ForceAllocate(DrawBufferSize); NN_NULL_ASSERT(bufMem); return nn::font::CharWriter::InitDispStringBuffer(bufMem, charMax); @@ -400,13 +398,13 @@ void FinalizeResFont() s_Drawer.Finalize(); // 描画リソースの破棄 - HeapManager::GetHeap()->Free(s_DrawerBuf); + ForceFree(s_DrawerBuf); // フォントの破棄 CleanupFont(&s_Font); // 描画文字列用バッファの解放 - HeapManager::GetHeap()->Free(s_pDrawStringBuf0); + ForceFree(s_pDrawStringBuf0); } nn::font::TextWriter* GetTextWriter() diff --git a/trunk/ConsoleDataMigration/sources/common/SdReaderWriter.cpp b/trunk/ConsoleDataMigration/sources/common/SdReaderWriter.cpp index 3a72428..efd790f 100644 --- a/trunk/ConsoleDataMigration/sources/common/SdReaderWriter.cpp +++ b/trunk/ConsoleDataMigration/sources/common/SdReaderWriter.cpp @@ -123,19 +123,19 @@ nn::Result SdReaderWriter::WriteBufWithCmac(const wchar_t* path, void* buf, size bit8 cmac[nn::crypto::AES_CMAC_MAC_SIZE]; result = nn::crypto::CalculateAesCmacSw(cmac, sha256Hash, nn::crypto::Sha256Context::HASH_SIZE, common::cmacKey); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + COMMON_LOGGER_RETURN_RESULT_IF_FAILED(result); s32 writeSize; result = file.TryWrite(&writeSize, cmac, sizeof(cmac), false); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + COMMON_LOGGER_RETURN_RESULT_IF_FAILED(result); result = file.TryFlush(); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + COMMON_LOGGER_RETURN_RESULT_IF_FAILED(result); file.Finalize(); result = Finalize(); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + COMMON_LOGGER_RETURN_RESULT_IF_FAILED(result); return result; @@ -231,7 +231,7 @@ nn::Result SdReaderWriter::ReadBufWithCmac(const wchar_t* path, void* buf, size_ return result; } -void SdReaderWriter::CreateDirectory(const wchar_t* path) +nn::Result SdReaderWriter::CreateDirectory(const wchar_t* path) { nn::Result result; @@ -242,11 +242,12 @@ void SdReaderWriter::CreateDirectory(const wchar_t* path) NN_LOG("Create Directory %ls\n", path); result = nn::fs::TryCreateDirectory(path); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + COMMON_LOGGER_RETURN_RESULT_IF_FAILED(result); result = Finalize(); - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + COMMON_LOGGER_RETURN_RESULT_IF_FAILED(result); + return nn::ResultSuccess(); } } diff --git a/trunk/ConsoleDataMigration/sources/common/SdReaderWriter.h b/trunk/ConsoleDataMigration/sources/common/SdReaderWriter.h index 81a10d7..da65776 100644 --- a/trunk/ConsoleDataMigration/sources/common/SdReaderWriter.h +++ b/trunk/ConsoleDataMigration/sources/common/SdReaderWriter.h @@ -42,7 +42,7 @@ public : nn::Result ReadBufWithCmac(const wchar_t* path, void* buf, size_t size, size_t* totalSize); //! @brief 渡されたディレクトリ名のディレクトリを作成します - void CreateDirectory(const wchar_t* path); + nn::Result CreateDirectory(const wchar_t* path); private: //! @brief 渡されたバッファからサイズ分指定されたパス名で書きこみます diff --git a/trunk/ConsoleDataMigration/sources/common/SimplePlayer.cpp b/trunk/ConsoleDataMigration/sources/common/SimplePlayer.cpp index b04e2ad..921eff9 100644 --- a/trunk/ConsoleDataMigration/sources/common/SimplePlayer.cpp +++ b/trunk/ConsoleDataMigration/sources/common/SimplePlayer.cpp @@ -76,7 +76,7 @@ namespace if (::std::strcmp(::std::strrchr(apFileNames[i], '.'), ".wav") == false) { // 連続メモリ領域の取得、確認 - apMemory[i] = reinterpret_cast(HeapManager::GetHeap()->Allocate(GetWaveLength(fileReader), 32)); + apMemory[i] = reinterpret_cast(ForceAllocate(GetWaveLength(fileReader), 32)); if (apMemory[i] == NULL) { NN_LOG("Failed to allocate continuous memory\n"); diff --git a/trunk/ConsoleDataMigration/sources/common/Util.cpp b/trunk/ConsoleDataMigration/sources/common/Util.cpp index 45082ec..ff4918c 100644 --- a/trunk/ConsoleDataMigration/sources/common/Util.cpp +++ b/trunk/ConsoleDataMigration/sources/common/Util.cpp @@ -70,7 +70,7 @@ void Util::Initialize() s64 fileSize = fis.GetSize(); s32 ret; void* addr = NULL; - addr = common::HeapManager::GetHeap()->Allocate(fileSize); + addr = ForceAllocate(fileSize); if (addr != NULL) { mp_Ivs = addr; diff --git a/trunk/ConsoleDataMigration/sources/common/VersionDetect.cpp b/trunk/ConsoleDataMigration/sources/common/VersionDetect.cpp index f028569..09977cd 100644 --- a/trunk/ConsoleDataMigration/sources/common/VersionDetect.cpp +++ b/trunk/ConsoleDataMigration/sources/common/VersionDetect.cpp @@ -45,7 +45,8 @@ void GetCupVersion(nn::pl::CTR::CardUpdateVersion* cup, nn::cfg::CTR::CfgRegionC NN_LOG("version.bin size = %lld\n", fileSize); s32 ret; void* addr = NULL; - addr = common::HeapManager::GetHeap()->Allocate(fileSize); + common::HeapManager heap(fileSize); + addr = heap.GetAddr(); if (addr != NULL) { result = fis.TryRead(&ret, addr, fileSize); @@ -56,7 +57,6 @@ void GetCupVersion(nn::pl::CTR::CardUpdateVersion* cup, nn::cfg::CTR::CfgRegionC std::memcpy(cup, ver_buf, sizeof(nn::pl::CTR::CardUpdateVersion)); } - common::HeapManager::GetHeap()->Free(addr); } } fis.Finalize(); @@ -88,7 +88,8 @@ void GetNupVersion(nn::pl::CTR::NetworkUpdateVersion* nup, nn::cfg::CTR::CfgRegi NN_LOG("version.bin size = %lld\n", fileSize); s32 ret; void* addr = NULL; - addr = common::HeapManager::GetHeap()->Allocate(fileSize); + common::HeapManager heap(fileSize); + addr = heap.GetAddr(); if (addr != NULL) { result = fis.TryRead(&ret, addr, fileSize); @@ -100,7 +101,6 @@ void GetNupVersion(nn::pl::CTR::NetworkUpdateVersion* nup, nn::cfg::CTR::CfgRegi std::memcpy(nup, ver_buf, sizeof(nn::pl::CTR::NetworkUpdateVersion)); } - common::HeapManager::GetHeap()->Free(addr); } } diff --git a/trunk/ConsoleDataMigration/sources/common/common_Types.h b/trunk/ConsoleDataMigration/sources/common/common_Types.h index e36889c..fd76dad 100644 --- a/trunk/ConsoleDataMigration/sources/common/common_Types.h +++ b/trunk/ConsoleDataMigration/sources/common/common_Types.h @@ -81,6 +81,10 @@ const nn::ProgramId cCupVerId[] = nn::pl::CTR::SHAREDDATA_TITLEID_CUP_VERSION_JP, nn::pl::CTR::SHAREDDATA_TITLEID_CUP_VERSION_US, nn::pl::CTR::SHAREDDATA_TITLEID_CUP_VERSION_EU, + nn::pl::CTR::SHAREDDATA_TITLEID_CUP_VERSION_EU, + nn::pl::CTR::SHAREDDATA_TITLEID_CUP_VERSION_CN, + nn::pl::CTR::SHAREDDATA_TITLEID_CUP_VERSION_KR, + nn::pl::CTR::SHAREDDATA_TITLEID_CUP_VERSION_TW, }; const nn::ProgramId cNupVerId[] = @@ -88,6 +92,10 @@ const nn::ProgramId cNupVerId[] = nn::pl::CTR::SHAREDDATA_TITLEID_NUP_VERSION_JP, nn::pl::CTR::SHAREDDATA_TITLEID_NUP_VERSION_US, nn::pl::CTR::SHAREDDATA_TITLEID_NUP_VERSION_EU, + nn::pl::CTR::SHAREDDATA_TITLEID_NUP_VERSION_EU, + nn::pl::CTR::SHAREDDATA_TITLEID_NUP_VERSION_CN, + nn::pl::CTR::SHAREDDATA_TITLEID_NUP_VERSION_KR, + nn::pl::CTR::SHAREDDATA_TITLEID_NUP_VERSION_TW, }; // SDに出力するデータのヘッダ diff --git a/trunk/ConsoleDataMigration/sources/tools/ExportedDataVerifier/OMakefile b/trunk/ConsoleDataMigration/sources/tools/ExportedDataVerifier/OMakefile index 29beca3..3eec50d 100644 --- a/trunk/ConsoleDataMigration/sources/tools/ExportedDataVerifier/OMakefile +++ b/trunk/ConsoleDataMigration/sources/tools/ExportedDataVerifier/OMakefile @@ -37,7 +37,6 @@ SOURCES[] = ../../common/CommonLogger.cpp ../../common/SdMountManager.cpp ../../common/configLoader.cpp - ../../common/PlayHistoryManager.cpp ../../common/VersionDetect.cpp CTR_BANNER_SPEC = $(TARGET_PROGRAM).bsf diff --git a/trunk/ConsoleDataMigration/sources/tools/ExportedDataVerifier/main.cpp b/trunk/ConsoleDataMigration/sources/tools/ExportedDataVerifier/main.cpp index 616e004..e1b2d19 100644 --- a/trunk/ConsoleDataMigration/sources/tools/ExportedDataVerifier/main.cpp +++ b/trunk/ConsoleDataMigration/sources/tools/ExportedDataVerifier/main.cpp @@ -55,7 +55,6 @@ #include "CommonLogger.h" #include "SDMountManager.h" #include "HeapManager.h" -#include "PlayHistoryManager.h" #include "common_Types.h" #include "VersionDetect.h" #include "Util.h" @@ -363,9 +362,10 @@ void VerifyThreadFunc() result = common::SdMountManager::Mount(); - size_t bufSize = common::HeapManager::GetHeap()->GetAllocatableSize(); + size_t bufSize = common::GetAllocatableSize(); - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize, AES_BLOCK_SIZE); + common::HeapManager heap(bufSize, AES_BLOCK_SIZE); + void* buf = heap.GetAddr(); if (buf != NULL) { u32 i = 0; @@ -398,8 +398,6 @@ void VerifyThreadFunc() buf, bufSize); } nn::fs::Unmount(common::NAND_ARCHIVE_NAME); - - common::HeapManager::GetHeap()->Free(buf); } common::SdMountManager::Unmount(); @@ -428,10 +426,11 @@ extern "C" void nnMain(void) NN_UTIL_PANIC_IF_FAILED(result); // ヒープの確保 - common::HeapManager::GetHeap()->Initialize(nn::os::GetDeviceMemoryAddress(), nn::os::GetDeviceMemorySize(), nn::os::ALLOCATE_OPTION_LINEAR); + common::InitializeHeap(); // RenderSystem の準備 - uptr heapForGx = reinterpret_cast(common::HeapManager::GetHeap()->Allocate(s_GxHeapSize)); + common::HeapManager gxHeap(s_GxHeapSize); + uptr heapForGx = reinterpret_cast(gxHeap.GetAddr()); demo::RenderSystemDrawing s_RenderSystem; s_RenderSystem.Initialize(heapForGx, s_GxHeapSize);