From 2d011dee6f0cd7b8dbfb98adf4f4bda207a8b400 Mon Sep 17 00:00:00 2001 From: N2614 Date: Wed, 2 Nov 2011 06:00:40 +0000 Subject: [PATCH] =?UTF-8?q?CARDBOARD=E7=94=A8API=E3=81=A7=E3=82=B7?= =?UTF-8?q?=E3=82=B9=E3=83=86=E3=83=A0=E3=82=BB=E3=83=BC=E3=83=96=E3=83=87?= =?UTF-8?q?=E3=83=BC=E3=82=BF=E3=82=92=E5=87=BA=E5=8A=9B=E3=81=99=E3=82=8B?= =?UTF-8?q?=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@474 385bec56-5757-e545-9c3a-d8741f4650f1 --- .../sources/ConsoleBackup/Exporter.cpp | 64 +----- .../sources/ConsoleBackup/OMakefile | 1 + .../sources/ConsoleRestore/Importer.cpp | 1 - .../sources/common/FileName.h | 4 + .../sources/common/FileTransfer.cpp | 2 - .../sources/common/SaveDataMover.cpp | 204 ++++++++++++++++++ .../sources/common/SaveDataMover.h | 61 ++++++ .../sources/common/SdReaderWriter.cpp | 3 +- 8 files changed, 281 insertions(+), 59 deletions(-) create mode 100644 trunk/ConsoleDataMigration/sources/common/SaveDataMover.cpp create mode 100644 trunk/ConsoleDataMigration/sources/common/SaveDataMover.h diff --git a/trunk/ConsoleDataMigration/sources/ConsoleBackup/Exporter.cpp b/trunk/ConsoleDataMigration/sources/ConsoleBackup/Exporter.cpp index 763c99a..2d19499 100644 --- a/trunk/ConsoleDataMigration/sources/ConsoleBackup/Exporter.cpp +++ b/trunk/ConsoleDataMigration/sources/ConsoleBackup/Exporter.cpp @@ -47,6 +47,7 @@ #include "Aes_define.h" #include "VersionDetect.h" #include "Util.h" +#include "SaveDataMover.h" namespace ConsoleBackup { @@ -66,6 +67,8 @@ wchar_t s_RootName[256]; nn::crypto::Sha256Context s_FileListContext; +u64 s_ExportProgress = 0; + } void AddCmac(nn::fs::FileOutputStream* file, nn::crypto::Sha256Context* context); @@ -281,8 +284,6 @@ nn::Result WriteIvs(common::HardwareStateManager& manager) if(enc != NULL) { // AES暗号化する - nn::crypto::Initialize(); - nn::crypto::SwAesCtrContext swAesCtrContest; swAesCtrContest.Initialize(common::iv, common::key, sizeof(common::key)); @@ -694,7 +695,6 @@ void WriteTwlSoundData() void InitializeFileListContext() { - nn::crypto::Initialize(); s_FileListContext.Initialize(); } @@ -763,9 +763,6 @@ 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::GetAllocatableSize(AES_BLOCK_SIZE * 2); @@ -779,40 +776,11 @@ void ExportThreadFunc() 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()) - { - s64 fileSize; - result = list.TryGetSize(&fileSize); - if (result.IsSuccess()) - { - // 末尾に移動 - result = list.TrySetPosition(fileSize); - if (result.IsSuccess()) - { - 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_IsExportSucceeded = false; - return; - } - - AddCmac(&list, &s_FileListContext); - list.TryFlush(); - list.Finalize(); - } - } - } + common::SaveDataMover saveDataMover; + saveDataMover.StartExport(buf, bufSize, &s_ExportProgress); } common::SdMountManager::Unmount(); - nn::fs::Unmount(common::NAND_ARCHIVE_NAME); NN_LOG("Export Thread Finalize\n"); } @@ -836,25 +804,13 @@ nn::Result WriteExportContext() nn::Result WriteSaveData() { + // 不要なデータを削除する + COMMON_LOGGER_RETURN_RESULT_IF_FAILED( + DeleteTrash((std::wstring(common::LOG_ROOT_DIRECTORY_PATH) + std::wstring(L"/")).c_str())); + // NANDからSDカードに書き出し nn::Result result; - result = nn::fs::MountSpecialArchive(common::NAND_ARCHIVE_NAME, nn::fs::CTR::ARCHIVE_TYPE_CTR_NAND); - COMMON_LOGGER_RETURN_RESULT_IF_FAILED(result); - - u32 fileNum = 0; - s64 fileSize = 0; - result = common::CalculateFileNum(::std::wstring(common::NAND_DATA_ROOT_PATHNAME_WITH_SLASH), fileNum, fileSize); - COMMON_LOGGER_RETURN_RESULT_IF_FAILED(result); - - result = nn::fs::Unmount(common::NAND_ARCHIVE_NAME); - COMMON_LOGGER_RETURN_RESULT_IF_FAILED(result); - - NN_LOG("File Number = %d\n", fileNum); - NN_LOG("File Size = %lld\n", fileSize); - // 進捗表示用 - common::InitializeTransferProgress(fileSize); - // セーブデータディレクトリ以下のデータをSDカードにコピー // コピー用ディレクトリ作成 common::SdReaderWriter sdWriter; @@ -975,7 +931,7 @@ bool ExportData(common::HardwareStateManager& manager) u32 GetExportProgress() { - return common::GetProgress(); + return s_ExportProgress; } bool IsExportThreadFinished() diff --git a/trunk/ConsoleDataMigration/sources/ConsoleBackup/OMakefile b/trunk/ConsoleDataMigration/sources/ConsoleBackup/OMakefile index 35b786d..5a9f4a5 100644 --- a/trunk/ConsoleDataMigration/sources/ConsoleBackup/OMakefile +++ b/trunk/ConsoleDataMigration/sources/ConsoleBackup/OMakefile @@ -43,6 +43,7 @@ SOURCES[] = ../common/VersionDetect.cpp ../common/ResFont.cpp ../common/HardwareStateManager.cpp + ../common/SaveDataMover.cpp CTR_BANNER_SPEC = $(TARGET_PROGRAM).bsf diff --git a/trunk/ConsoleDataMigration/sources/ConsoleRestore/Importer.cpp b/trunk/ConsoleDataMigration/sources/ConsoleRestore/Importer.cpp index 05ce82f..032289f 100644 --- a/trunk/ConsoleDataMigration/sources/ConsoleRestore/Importer.cpp +++ b/trunk/ConsoleDataMigration/sources/ConsoleRestore/Importer.cpp @@ -648,7 +648,6 @@ nn::Result ImportIvs() if (dec != NULL) { // AES復号化する - nn::crypto::Initialize(); nn::crypto::SwAesCtrContext swAesCtrContest; swAesCtrContest.Initialize(common::iv, common::key, sizeof(common::key)); diff --git a/trunk/ConsoleDataMigration/sources/common/FileName.h b/trunk/ConsoleDataMigration/sources/common/FileName.h index 8a47206..310e351 100644 --- a/trunk/ConsoleDataMigration/sources/common/FileName.h +++ b/trunk/ConsoleDataMigration/sources/common/FileName.h @@ -28,6 +28,8 @@ const char* const NAND_TWL_SOUND_ARCHIVE_NAME = "twls:"; const char* const NAND_TWL_ARCHIVE_NAME = "twln:"; const char* const BASHOTORYA_FILE_NAME = "bashotorya.dat"; +const char* const SD_SAVEDATA_SYS_ROOT_PATH = "sdmc:/CTR_Console_Repair/CTRBackup/sysdata"; +const char* const SD_SAVEDATA_EXT_ROOT_PATH = "sdmc:/CTR_Console_Repair/CTRBackup/extdata"; const wchar_t* const LOG_ROOT_DIRECTORY_PATH = L"sdmc:/CTR_Console_Repair"; const wchar_t* const SD_SAVEDATA_ROOT_NAME = L"CTR_Console_Repair/CTRBackup/"; @@ -68,6 +70,8 @@ const wchar_t* const TWL_TITLELIST_PATHNAME = L"sdmc:/CTR_Console_Repair/TwlTitl const wchar_t* const MOVE_CONTEXT_PATHNAME = L"sdmc:/CTR_Console_Repair/MoveContext.bin"; const wchar_t* const SD_NINTENDO_3DS_ROOT_PATH = L"sdmc:/Nintendo 3DS/"; +const wchar_t* const SD_SAEVDATA_SYS_NAME = L"sysdata"; + enum TWL_PATH_INDEX { TWL_PHOTO = 0, diff --git a/trunk/ConsoleDataMigration/sources/common/FileTransfer.cpp b/trunk/ConsoleDataMigration/sources/common/FileTransfer.cpp index cc76f83..04b6823 100644 --- a/trunk/ConsoleDataMigration/sources/common/FileTransfer.cpp +++ b/trunk/ConsoleDataMigration/sources/common/FileTransfer.cpp @@ -587,7 +587,6 @@ bool VerifyMac(nn::fs::FileInputStream* sdFile, nn::fs::FileStream* nandFile, s6 s32 readSize; // ハッシュを取得する - nn::crypto::Initialize(); result = sdFile->TrySetPosition(sdFileSize - nn::crypto::AES_CMAC_MAC_SIZE); COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); @@ -655,7 +654,6 @@ bool VerifyMac(nn::fs::FileInputStream* sdFile, nn::fs::FileStream* nandFile, s6 } } } - nn::crypto::Finalize(); nandFile->Finalize(); return retValue; diff --git a/trunk/ConsoleDataMigration/sources/common/SaveDataMover.cpp b/trunk/ConsoleDataMigration/sources/common/SaveDataMover.cpp new file mode 100644 index 0000000..69a1fb0 --- /dev/null +++ b/trunk/ConsoleDataMigration/sources/common/SaveDataMover.cpp @@ -0,0 +1,204 @@ +/*---------------------------------------------------------------------------* + Project: Horizon + File: SaveDataMover.cpp + + Copyright 2009-2011 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 + +#include + +#include "SaveDataMover.h" +#include "CommonLogger.h" +#include "FileName.h" +#include "SdReaderWriter.h" + + +namespace common +{ + +namespace +{ + const s32 MAX_SYSTEM_SAVE_DATA_ID_NUM = 256; + nn::fs::SystemSaveDataId s_SystemSaveDataIdList[MAX_SYSTEM_SAVE_DATA_ID_NUM * sizeof(bit32)]; + + + bool IsNotMovedId(bit32 id) + { + return id == 0x0001002c; // nim + } + +} + + +SaveDataMover::SaveDataMover() : + m_Progress(0), m_FinishedSize(0), m_TotalSize(0), m_Result(nn::ResultSuccess()) +{ + // TODO 自動生成されたコンストラクター・スタブ +} + +SaveDataMover::~SaveDataMover() +{ + // TODO Auto-generated destructor stub +} + +void SaveDataMover::StartExport(void* buf, size_t bufSize, u64* progress) +{ + SetupExport(); + CalculateFileSize(); + if(GetLastResult().IsFailure()) + { + return; + } + ExportSystemSaveData(buf, bufSize, progress); +} + +nn::Result SaveDataMover::GetLastResult() +{ + return m_Result; +} + +void SaveDataMover::SetupExport() +{ + // 1. デーモンの停止 + m_Result = nn::ndm::Initialize(); + COMMON_LOGGER_RETURN_VOID_IF_FAILED(m_Result); + m_Result = nn::ndm::SuspendScheduler(); + COMMON_LOGGER_RETURN_VOID_IF_FAILED(m_Result); + + // 2. StartDeviceMoveAsSource + nn::fs::DeviceMoveContext moveContext; + m_Result = nn::fs::StartDeviceMoveAsSource(&moveContext); + COMMON_LOGGER_RETURN_VOID_IF_FAILED(m_Result); + + // 3. 出力用ディレクトリの作成 + common::SdReaderWriter sdWriter; + m_Result = sdWriter.CreateDirectory((::std::wstring(common::SDMC_ROOT_DIRECTORY_PATH) + ::std::wstring( + common::SD_SAVEDATA_ROOT_NAME) + std::wstring(common::SD_SAEVDATA_SYS_NAME)).c_str()); + COMMON_LOGGER_RETURN_VOID_IF_FAILED(m_Result); +} + +void SaveDataMover::CalculateFileSize() +{ + s32 systemSaveDataIdNum; + m_Result = nn::fs::EnumerateSystemSaveData(&systemSaveDataIdNum, s_SystemSaveDataIdList, MAX_SYSTEM_SAVE_DATA_ID_NUM); + COMMON_LOGGER_RETURN_VOID_IF_FAILED(m_Result); + + for(int i = 0; i < systemSaveDataIdNum; ++i) + { + nn::fs::FileInputStream input; + + bit32 id = s_SystemSaveDataIdList[i]; + if(IsNotMovedId(id)) + { + continue; + } + + m_Result = nn::fs::OpenSystemSaveDataRawStorageFile(&input, id); + if(m_Result.IsFailure()) + { + COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(m_Result); + continue; + } + + m_TotalSize += input.GetSize(); + } +} + +void SaveDataMover::ExportSystemSaveData(void* buf, size_t bufSize, u64* progress) +{ + s64 totalFileSize = 0; + + // 0. ID の列挙を行う。 + s32 systemSaveDataIdNum; + m_Result = nn::fs::EnumerateSystemSaveData(&systemSaveDataIdNum, s_SystemSaveDataIdList, MAX_SYSTEM_SAVE_DATA_ID_NUM); + COMMON_LOGGER_RETURN_VOID_IF_FAILED(m_Result); + + for(int i = 0; i < systemSaveDataIdNum; ++i) + { + nn::fs::FileInputStream input; + + bit32 id = s_SystemSaveDataIdList[i]; + if(IsNotMovedId(id)) + { + continue; + } + + NN_LOG("id: %08x\n", id); + + // 1. fs::OpenSystemSaveDataRawStorageFile を呼び + // ID に対するセーブデータの FileInputStream を得る。 + m_Result = nn::fs::OpenSystemSaveDataRawStorageFile(&input, id); + if(m_Result.IsFailure()) + { + COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(m_Result); + continue; + } + NN_LOG("fileSize = %lld\n", input.GetSize()); + totalFileSize += input.GetSize(); + + // SD へコピー + { + char outPath[64]; + nn::nstd::TSNPrintf(outPath, 64, "%s/%08x", SD_SAVEDATA_SYS_ROOT_PATH, id); + NN_LOG("outPath = %s\n", outPath); + nn::fs::FileOutputStream output; + m_Result = output.TryInitialize(outPath, true); + if(m_Result.IsFailure()) + { + COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(m_Result); + continue; + } + + // 2. 1 で得たストリームからデータを読み込む。 + // 本来はここで読み込んだデータを引っ越し先に転送するが + // ここでは SD カードにエクスポートする。 + m_Result = CopyFile(input, output, buf, bufSize, progress); + if(m_Result.IsFailure()) + { + COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(m_Result); + continue; + } + } + } + NN_LOG("\n"); + + NN_LOG("totalFileSize = %lld\n", totalFileSize); +} + +// ファイルの読み書き +nn::Result SaveDataMover::CopyFile(nn::fs::FileInputStream& is, nn::fs::FileOutputStream& os, void* pBuffer, + const s32 bufferSize, u64* progress) +{ + s64 restSize; + NN_UTIL_RETURN_IF_FAILED(is.TryGetSize(&restSize)); + + while (restSize > 0) + { + s32 readSize = restSize > bufferSize ? bufferSize : restSize; + s32 read = 0; + s32 write = 0; + NN_UTIL_RETURN_IF_FAILED(is.TryRead(&read, pBuffer, readSize)); + NN_UTIL_RETURN_IF_FAILED(os.TryWrite(&write, pBuffer, read)); + + restSize -= read; + m_FinishedSize += read; + *progress = m_FinishedSize * 100 / m_TotalSize; + NN_LOG("progress = %lld\n", *progress); + } + return nn::ResultSuccess(); +} + +} /* namespace common */ diff --git a/trunk/ConsoleDataMigration/sources/common/SaveDataMover.h b/trunk/ConsoleDataMigration/sources/common/SaveDataMover.h new file mode 100644 index 0000000..cc66b21 --- /dev/null +++ b/trunk/ConsoleDataMigration/sources/common/SaveDataMover.h @@ -0,0 +1,61 @@ +/*---------------------------------------------------------------------------* + Project: Horizon + File: SaveDataMover.h + + Copyright (C)2011 Nintendo Co., Ltd. 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 SAVEDATAMOVER_H_ +#define SAVEDATAMOVER_H_ + +#include + +namespace common +{ + +class SaveDataMover +{ +public: + SaveDataMover(); + virtual ~SaveDataMover(); + + //! @brief SDカードへの出力を開始します + void StartExport(void* buf, size_t bufSize, u64* progress); + + //! @brief SDカードからの入力を開始します + void StartImport(void* buf, size_t bufSize, u64* progress); + + //! @brief 処理の結果を取得します + nn::Result GetLastResult(); + +private: + //! @brief 出力を準備します + void SetupExport(); + + //! @brief 出力するファイルサイズを計算します + void CalculateFileSize(); + + //! @brief システムセーブデータを出力します + void ExportSystemSaveData(void* buf, size_t bufSize, u64* progress); + + nn::Result CopyFile(nn::fs::FileInputStream& is, nn::fs::FileOutputStream& os, void* pBuffer, const s32 bufferSize, + u64* progress); + + NN_PADDING4; + u64 m_Progress; + u64 m_FinishedSize; + u64 m_TotalSize; + + nn::Result m_Result; +}; + +} /* namespace common */ +#endif /* SAVEDATAMOVER_H_ */ diff --git a/trunk/ConsoleDataMigration/sources/common/SdReaderWriter.cpp b/trunk/ConsoleDataMigration/sources/common/SdReaderWriter.cpp index 9ce75c6..01fe5fe 100644 --- a/trunk/ConsoleDataMigration/sources/common/SdReaderWriter.cpp +++ b/trunk/ConsoleDataMigration/sources/common/SdReaderWriter.cpp @@ -118,7 +118,6 @@ nn::Result SdReaderWriter::WriteBufWithCmac(const wchar_t* path, void* buf, size result = WriteBufCore(file, path, buf, size); NN_UTIL_RETURN_IF_FAILED(result); - nn::crypto::Initialize(); bit8 sha256Hash[nn::crypto::Sha256Context::HASH_SIZE]; nn::crypto::CalculateSha256(sha256Hash, buf, size); @@ -199,7 +198,7 @@ nn::Result SdReaderWriter::ReadBufWithCmac(const wchar_t* path, void* buf, size_ *totalSize -= nn::crypto::AES_CMAC_MAC_SIZE; // CMACの検証を行う - nn::crypto::Initialize(); + bit8 sha256Hash[nn::crypto::Sha256Context::HASH_SIZE]; nn::crypto::CalculateSha256(sha256Hash, buf, *totalSize);