From a4834ca886a516fd1e715edb629bcd848eeaff22 Mon Sep 17 00:00:00 2001 From: N2614 Date: Fri, 8 Jul 2011 07:32:01 +0000 Subject: [PATCH] =?UTF-8?q?=E6=9B=B8=E3=81=8D=E8=BE=BC=E3=81=BF=E5=BE=8CCT?= =?UTF-8?q?R-NAND=E9=A0=98=E5=9F=9F=E3=82=92=E3=83=81=E3=82=A7=E3=83=83?= =?UTF-8?q?=E3=82=AF=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-05-23%20-%20ctr.7z%20+%20svn_v1.068.zip/ctr/svn/ctr_Repair@373 385bec56-5757-e545-9c3a-d8741f4650f1 --- .../sources/ConsoleRestore/Controller.cpp | 180 ++++++++++-------- .../sources/ConsoleRestore/Importer.cpp | 107 ++++++++++- .../sources/ConsoleRestore/Importer.h | 3 + .../sources/common/FileTransfer.cpp | 84 ++++++++ .../sources/common/FileTransfer.h | 3 + 5 files changed, 299 insertions(+), 78 deletions(-) diff --git a/trunk/ConsoleDataMigration/sources/ConsoleRestore/Controller.cpp b/trunk/ConsoleDataMigration/sources/ConsoleRestore/Controller.cpp index 62b0d87..b060d0e 100644 --- a/trunk/ConsoleDataMigration/sources/ConsoleRestore/Controller.cpp +++ b/trunk/ConsoleDataMigration/sources/ConsoleRestore/Controller.cpp @@ -135,6 +135,7 @@ typedef enum RestoreState RESTORE_IN_PROGRESS, // 書き込み中 POST_RESTORE, // 書き込み後の処理 RESTORE_DONE, // 書き込み完了 + RESTORE_TEST, // 書き込み後のチェック REBOOTING, // 再起動を行う ERASE, // 削除処理を行う RESTORE_CAL, // cfgの一部をcal値で上書きする @@ -777,15 +778,14 @@ void ControlState(common::HardwareStateManager& manager, ::std::vector (serial)); + ::std::string serialStr(reinterpret_cast(serial)); operationMessage.push_back(::std::string("Serial Number in SD : ") + serialStr); } else @@ -852,7 +852,7 @@ void ControlState(common::HardwareStateManager& manager, ::std::vector 99) { - s_RestoreState = POST_RESTORE; + s_RestoreState = RESTORE_TEST; } else { @@ -1260,8 +1260,8 @@ void ControlState(common::HardwareStateManager& manager, ::std::vector #include #include +#include #include #include // cfg:norの初期化に必要 #include @@ -622,6 +623,43 @@ bool InitializeFileSystem() return true; } +nn::Result GetNewIvs(void* oldBuf, size_t oldBufSize, void* newBuf, size_t newBufSize, size_t* newIvsSize) +{ + if (oldBufSize < nn::fs::CTR::SIZE_INTEGRITY_VERIFICATION_SEED + || newBufSize < nn::fs::CTR::SIZE_INTEGRITY_VERIFICATION_SEED + || oldBuf == NULL || newBuf == NULL + ) + { + return nn::Result(nn::Result::LEVEL_FATAL, nn::Result::SUMMARY_OUT_OF_RESOURCE, nn::Result::MODULE_COMMON, + nn::Result::DESCRIPTION_OUT_OF_MEMORY); + } + + nn::Result result; + result = common::SdMountManager::Mount(); + COMMON_LOGGER_RETURN_RESULT_IF_FAILED(result); + + common::SdReaderWriter sdReader; + size_t readSize; + result = sdReader.ReadBufWithCmac(common::IVS_PATHNAME, oldBuf, oldBufSize, &readSize); + COMMON_LOGGER_RETURN_RESULT_IF_FAILED(result); + *newIvsSize = readSize; + + // AES復号化する + nn::crypto::Initialize(); + nn::crypto::SwAesCtrContext swAesCtrContest; + + swAesCtrContest.Initialize(common::iv, common::key, sizeof(common::key)); + result = swAesCtrContest.Decrypt(newBuf, oldBuf, readSize); + COMMON_LOGGER_RETURN_RESULT_IF_FAILED(result); + + nn::crypto::Finalize(); + + result = common::SdMountManager::Unmount(); + COMMON_LOGGER_RETURN_RESULT_IF_FAILED(result); + + return nn::ResultSuccess(); +} + nn::Result ImportIvs() { nn::Result result = nn::ResultSuccess(); @@ -636,7 +674,7 @@ nn::Result ImportIvs() size_t readSize; result = sdReader.ReadBufWithCmac(common::IVS_PATHNAME, enc, bufSize, &readSize); - if(result.IsSuccess()) + if (result.IsSuccess()) { // SDから読み出し成功 result = nn::fs::MountSpecialArchive(common::NAND_ARCHIVE_NAME, nn::fs::CTR::ARCHIVE_TYPE_CTR_NAND); @@ -732,6 +770,7 @@ void ImportThreadFunc() NN_LOG("Import Thread Finalize\n"); } + namespace { @@ -1856,6 +1895,72 @@ bool IsImportSucceeded() return s_IsImportSucceeded; } + +nn::Result GetNewSaveDirRoot(std::wstring& sysSaveRoot) +{ + nn::Result result; + + const size_t BUF_SIZE = 1024; + common::HeapManager oldBuf(BUF_SIZE); + common::HeapManager newBuf(BUF_SIZE); + size_t ivsSize; + + result = GetNewIvs(oldBuf.GetAddr(), BUF_SIZE, newBuf.GetAddr(), BUF_SIZE, &ivsSize); + COMMON_LOGGER_RETURN_RESULT_IF_FAILED(result); + + std::string root; + common::Util::GetSaveDataDirectoryRoot(root, newBuf.GetAddr(), ivsSize); + + wchar_t rootName[256]; + std::mbstowcs(rootName, root.c_str(), root.size() + 1); + sysSaveRoot = rootName; + + return nn::ResultSuccess(); +} + +void TestCtrDataThreadFunc() +{ + nn::Result result; + s_IsImportSucceeded = true; + + // 新IVSディレクトリを取得 + std::wstring sysSaveRoot; + result = GetNewSaveDirRoot(sysSaveRoot); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsImportSucceeded); + + result = nn::fs::MountSpecialArchive(common::NAND_ARCHIVE_NAME, nn::fs::CTR::ARCHIVE_TYPE_CTR_NAND); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsImportSucceeded); + + u32 fileNum = 0; + s64 fileSize = 0; + result = common::CalculateFileNum( + ::std::wstring(common::NAND_DATA_ROOT_PATHNAME_WITH_SLASH) + sysSaveRoot + std::wstring(L"/"), fileNum, + fileSize); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsImportSucceeded); + + common::InitializeTransferProgress(fileSize); + + size_t bufSize = common::GetAllocatableSize(AES_BLOCK_SIZE); + common::HeapManager buf(bufSize, AES_BLOCK_SIZE); + + if(!common::TestReadDirectory((std::wstring(common::NAND_DATA_ROOT_PATHNAME_WITH_SLASH) + sysSaveRoot + std::wstring(L"/")).c_str(), buf.GetAddr(), bufSize + )) + { + s_IsImportSucceeded = false; + return; + } + + result = nn::fs::Unmount(common::NAND_ARCHIVE_NAME); + COMMON_LOGGER_RETURN_VOID_SET_BOOL_IF_FAILED(result, s_IsImportSucceeded); + +} + +void TestCtrData() +{ + COMMON_LOGGER("Test NAND Data Start...\n"); + s_ImportThread.Start(TestCtrDataThreadFunc, s_ImportThreadStack); +} + 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 e2c49b1..a875a7c 100644 --- a/trunk/ConsoleDataMigration/sources/ConsoleRestore/Importer.h +++ b/trunk/ConsoleDataMigration/sources/ConsoleRestore/Importer.h @@ -160,6 +160,9 @@ bool ImportIvsData(); // 書き込みが成功したかどうか bool IsImportSucceeded(); +// 新たにスレッドを立て、CTR-NAND領域をテストする +void TestCtrData(); + } #endif /* IMPORTER_H_ */ diff --git a/trunk/ConsoleDataMigration/sources/common/FileTransfer.cpp b/trunk/ConsoleDataMigration/sources/common/FileTransfer.cpp index 16bea31..ba419f3 100644 --- a/trunk/ConsoleDataMigration/sources/common/FileTransfer.cpp +++ b/trunk/ConsoleDataMigration/sources/common/FileTransfer.cpp @@ -767,4 +767,88 @@ bool AddPathNameAndUpdateContext(nn::fs::FileOutputStream* file, const wchar_t * return true; } + +bool TestReadDirectory(const wchar_t* path, void* buf, const size_t bufSize) +{ + nn::fs::Directory dir; + nn::fs::DirectoryEntry entry; + s32 numread = 0; + std::wostringstream targetStream; + + NN_LOG("Read Directory %ls\n", path); + nn::Result result = dir.TryInitialize(path); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + while (1) + { + result = dir.TryRead(&numread, &entry, 1); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + if(numread == 0) + { + break; + } + + if (std::wcscmp(entry.entryName, L".") == 0 || std::wcscmp(entry.entryName, L"..") == 0) + { + continue; + } + + targetStream.str(L""); + targetStream.clear(std::stringstream::goodbit); + targetStream << path << entry.entryName; + + // ディレクトリの場合 + if (entry.attributes.isDirectory) + { + targetStream << L"/"; + + // 再帰処理 + if (!TestReadDirectory(targetStream.str().c_str(), buf, bufSize)) + { + return false; + } + } + // ファイルの場合 + else + { + // ファイル作成 + nn::fs::FileInputStream file; + s64 filesize; + s32 readsize; + + NN_LOG("Read File %ls\n", targetStream.str().c_str()); + + // 読み込み対象ファイル開く + result = file.TryInitialize(targetStream.str().c_str()); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + // 読み込み対象ファイルのサイズ取得 + result = file.TryGetSize(&filesize); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + // ファイル全体を読みきるまでループ + while (1) + { + result = file.TryRead(&readsize, buf, bufSize); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + if (readsize == 0) + { + break; + } + else + { + 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); + } + } + } + } + + return true; +} + } diff --git a/trunk/ConsoleDataMigration/sources/common/FileTransfer.h b/trunk/ConsoleDataMigration/sources/common/FileTransfer.h index e55f3bf..7a6e4c0 100644 --- a/trunk/ConsoleDataMigration/sources/common/FileTransfer.h +++ b/trunk/ConsoleDataMigration/sources/common/FileTransfer.h @@ -57,6 +57,9 @@ void InitializeTransferProgress(u64 totalSize); // 内部のバッファを使用するためスレッドアンセーフ const char* GetCharStr(const wchar_t* path); +// pathで指定したディレクトリ以下を再帰的に読み込んでエラーが発生しないかテストする +bool TestReadDirectory(const wchar_t* path, void* buf, const size_t bufSize); + } #endif /* FILETRANSFER_H_ */