2013-04-18 03:43:35 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
// Based off of tachtig/twintig http://git.infradead.org/?p=users/segher/wii.git
|
|
|
|
// Copyright 2007,2008 Segher Boessenkool <segher@kernel.crashing.org>
|
|
|
|
// Licensed under the terms of the GNU GPL, version 2
|
|
|
|
// http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
|
|
|
|
|
2013-10-26 09:55:41 +00:00
|
|
|
#include <cinttypes>
|
2014-02-22 22:36:30 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <polarssl/aes.h>
|
|
|
|
#include <polarssl/md5.h>
|
|
|
|
#include <polarssl/sha1.h>
|
2013-10-26 09:55:41 +00:00
|
|
|
|
2014-02-22 22:36:30 +00:00
|
|
|
#include "Common/Common.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/MathUtil.h"
|
|
|
|
#include "Common/NandPaths.h"
|
2014-02-22 22:36:30 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-28 11:41:10 +00:00
|
|
|
#include "Common/Crypto/ec.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "DolphinWX/MemoryCards/WiiSaveCrypted.h"
|
2010-12-22 00:48:59 +00:00
|
|
|
|
|
|
|
static Common::replace_v replacements;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-02-17 04:51:41 +00:00
|
|
|
const u8 SDKey[16] = {
|
|
|
|
0xAB, 0x01, 0xB9, 0xD8, 0xE1, 0x62, 0x2B, 0x08,
|
|
|
|
0xAF, 0xBA, 0xD8, 0x4D, 0xBF, 0xC2, 0xA5, 0x5D
|
|
|
|
};
|
|
|
|
const u8 MD5_BLANKER[0x10] = {
|
|
|
|
0x0E, 0x65, 0x37, 0x81, 0x99, 0xBE, 0x45, 0x17,
|
|
|
|
0xAB, 0x06, 0xEC, 0x22, 0x45, 0x1A, 0x57, 0x93
|
|
|
|
};
|
2011-06-27 20:48:15 +00:00
|
|
|
const u32 NG_id = 0x0403AC68;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-08-18 21:58:23 +00:00
|
|
|
bool CWiiSaveCrypted::ImportWiiSave(const char* FileName)
|
|
|
|
{
|
2014-02-17 04:51:41 +00:00
|
|
|
CWiiSaveCrypted saveFile(FileName);
|
2013-08-18 21:58:23 +00:00
|
|
|
return saveFile.b_valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CWiiSaveCrypted::ExportWiiSave(u64 TitleID)
|
|
|
|
{
|
|
|
|
CWiiSaveCrypted exportSave("", TitleID);
|
|
|
|
return exportSave.b_valid;
|
|
|
|
}
|
|
|
|
|
2013-08-18 21:59:05 +00:00
|
|
|
void CWiiSaveCrypted::ExportAllSaves()
|
|
|
|
{
|
|
|
|
std::string titleFolder = File::GetUserPath(D_WIIUSER_IDX) + "title";
|
|
|
|
std::vector<u64> titles;
|
|
|
|
u32 pathMask = 0x00010000;
|
|
|
|
for (int i = 0; i < 8; ++i)
|
2013-10-29 05:23:17 +00:00
|
|
|
{
|
2013-08-18 21:59:05 +00:00
|
|
|
File::FSTEntry FST_Temp;
|
|
|
|
std::string folder = StringFromFormat("%s/%08x/", titleFolder.c_str(), pathMask | i);
|
|
|
|
File::ScanDirectoryTree(folder, FST_Temp);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-02-12 15:00:34 +00:00
|
|
|
for (const File::FSTEntry& entry : FST_Temp.children)
|
2013-08-18 21:59:05 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
if (entry.isDirectory)
|
2013-08-18 21:59:05 +00:00
|
|
|
{
|
|
|
|
u32 gameid;
|
2014-03-12 19:33:41 +00:00
|
|
|
if (AsciiToHex(entry.virtualName, gameid))
|
2013-08-18 21:59:05 +00:00
|
|
|
{
|
|
|
|
std::string bannerPath = StringFromFormat("%s%08x/data/banner.bin", folder.c_str(), gameid);
|
|
|
|
if (File::Exists(bannerPath))
|
|
|
|
{
|
|
|
|
u64 titleID = (((u64)pathMask | i) << 32) | gameid;
|
|
|
|
titles.push_back(titleID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
}
|
2014-08-06 02:43:34 +00:00
|
|
|
SuccessAlertT("Found %d save files", (unsigned int) titles.size());
|
2014-02-12 15:00:34 +00:00
|
|
|
for (const u64& title : titles)
|
2013-08-18 21:59:05 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
CWiiSaveCrypted* exportSave = new CWiiSaveCrypted("", title);
|
2013-08-18 21:59:05 +00:00
|
|
|
delete exportSave;
|
|
|
|
}
|
|
|
|
}
|
2011-06-27 06:19:23 +00:00
|
|
|
CWiiSaveCrypted::CWiiSaveCrypted(const char* FileName, u64 TitleID)
|
|
|
|
: m_TitleID(TitleID)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2010-12-22 00:48:59 +00:00
|
|
|
Common::ReadReplacements(replacements);
|
2013-08-18 21:58:23 +00:00
|
|
|
encryptedSavePath = std::string(FileName);
|
2010-06-09 01:37:08 +00:00
|
|
|
memcpy(SD_IV, "\x21\x67\x12\xE6\xAA\x1F\x68\x9F\x95\xC5\xA2\x23\x24\xDC\x6A\x98", 0x10);
|
|
|
|
|
2011-06-27 06:19:23 +00:00
|
|
|
if (!TitleID) // Import
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-10-27 18:27:07 +00:00
|
|
|
aes_setkey_dec(&m_AES_ctx, SDKey, 128);
|
2010-06-09 01:37:08 +00:00
|
|
|
b_valid = true;
|
|
|
|
ReadHDR();
|
|
|
|
ReadBKHDR();
|
|
|
|
ImportWiiSaveFiles();
|
|
|
|
// TODO: check_sig()
|
|
|
|
if (b_valid)
|
|
|
|
{
|
2011-01-13 02:05:58 +00:00
|
|
|
SuccessAlertT("Successfully imported save files");
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
2013-04-08 05:16:50 +00:00
|
|
|
else
|
|
|
|
{
|
2013-08-18 21:58:23 +00:00
|
|
|
PanicAlertT("Import failed");
|
2013-04-08 05:16:50 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-27 18:27:07 +00:00
|
|
|
aes_setkey_enc(&m_AES_ctx, SDKey, 128);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
if (getPaths(true))
|
|
|
|
{
|
2013-08-18 21:58:23 +00:00
|
|
|
b_valid = true;
|
|
|
|
WriteHDR();
|
|
|
|
WriteBKHDR();
|
|
|
|
ExportWiiSaveFiles();
|
|
|
|
do_sig();
|
|
|
|
if (b_valid)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-08-18 21:58:23 +00:00
|
|
|
SuccessAlertT("Successfully exported file to %s", encryptedSavePath.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PanicAlertT("Export failed");
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CWiiSaveCrypted::ReadHDR()
|
|
|
|
{
|
2013-08-18 21:58:23 +00:00
|
|
|
File::IOFile fpData_bin(encryptedSavePath, "rb");
|
2010-06-09 01:37:08 +00:00
|
|
|
if (!fpData_bin)
|
|
|
|
{
|
2013-08-18 21:58:23 +00:00
|
|
|
PanicAlertT("Cannot open %s", encryptedSavePath.c_str());
|
2010-06-09 01:37:08 +00:00
|
|
|
b_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
2011-03-11 10:21:46 +00:00
|
|
|
if (!fpData_bin.ReadBytes(&_encryptedHeader, HEADER_SZ))
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-03-31 23:10:21 +00:00
|
|
|
PanicAlertT("Failed to read header");
|
2010-06-09 01:37:08 +00:00
|
|
|
b_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
2011-03-11 10:21:46 +00:00
|
|
|
fpData_bin.Close();
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-10-29 05:23:17 +00:00
|
|
|
aes_crypt_cbc(&m_AES_ctx, AES_DECRYPT, HEADER_SZ, SD_IV, (const u8*)&_encryptedHeader, (u8*)&_header);
|
2013-08-18 21:58:23 +00:00
|
|
|
u32 bannerSize = Common::swap32(_header.hdr.BannerSize);
|
|
|
|
if ((bannerSize < FULL_BNR_MIN) || (bannerSize > FULL_BNR_MAX) ||
|
|
|
|
(((bannerSize - BNR_SZ) % ICON_SZ) != 0))
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-08-18 21:58:23 +00:00
|
|
|
PanicAlertT("Not a Wii save or read failure for file header size %x", bannerSize);
|
2010-06-09 01:37:08 +00:00
|
|
|
b_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
2011-06-27 06:19:23 +00:00
|
|
|
m_TitleID = Common::swap64(_header.hdr.SaveGameTitle);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-02-17 04:51:41 +00:00
|
|
|
u8 md5_file[16];
|
|
|
|
u8 md5_calc[16];
|
2010-06-09 01:37:08 +00:00
|
|
|
memcpy(md5_file, _header.hdr.Md5, 0x10);
|
|
|
|
memcpy(_header.hdr.Md5, MD5_BLANKER, 0x10);
|
|
|
|
md5((u8*)&_header, HEADER_SZ, md5_calc);
|
|
|
|
if (memcmp(md5_file, md5_calc, 0x10))
|
|
|
|
{
|
2013-10-26 09:55:41 +00:00
|
|
|
PanicAlertT("MD5 mismatch\n %016" PRIx64 "%016" PRIx64 " != %016" PRIx64 "%016" PRIx64, Common::swap64(md5_file),Common::swap64(md5_file+8), Common::swap64(md5_calc), Common::swap64(md5_calc+8));
|
2010-06-09 01:37:08 +00:00
|
|
|
b_valid= false;
|
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
if (!getPaths())
|
|
|
|
{
|
|
|
|
b_valid = false;
|
2013-10-29 05:23:17 +00:00
|
|
|
return;
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
2013-08-18 21:58:23 +00:00
|
|
|
std::string BannerFilePath = WiiTitlePath + "banner.bin";
|
2011-06-27 06:19:23 +00:00
|
|
|
if (!File::Exists(BannerFilePath) || AskYesNoT("%s already exists, overwrite?", BannerFilePath.c_str()))
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-03-31 23:10:21 +00:00
|
|
|
INFO_LOG(CONSOLE, "Creating file %s", BannerFilePath.c_str());
|
2011-06-27 06:19:23 +00:00
|
|
|
File::IOFile fpBanner_bin(BannerFilePath, "wb");
|
2013-08-18 21:58:23 +00:00
|
|
|
fpBanner_bin.WriteBytes(_header.BNR, bannerSize);
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CWiiSaveCrypted::WriteHDR()
|
|
|
|
{
|
|
|
|
if (!b_valid) return;
|
|
|
|
memset(&_header, 0, HEADER_SZ);
|
|
|
|
|
2013-08-18 21:58:23 +00:00
|
|
|
std::string BannerFilePath = WiiTitlePath + "banner.bin";
|
2011-06-27 06:19:23 +00:00
|
|
|
u32 bannerSize = File::GetSize(BannerFilePath);
|
|
|
|
_header.hdr.BannerSize = Common::swap32(bannerSize);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2011-06-27 06:19:23 +00:00
|
|
|
_header.hdr.SaveGameTitle = Common::swap64(m_TitleID);
|
2010-06-09 01:37:08 +00:00
|
|
|
memcpy(_header.hdr.Md5, MD5_BLANKER, 0x10);
|
2013-08-18 21:58:23 +00:00
|
|
|
_header.hdr.Permissions = 0x3C;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2011-06-27 06:19:23 +00:00
|
|
|
File::IOFile fpBanner_bin(BannerFilePath, "rb");
|
|
|
|
if (!fpBanner_bin.ReadBytes(_header.BNR, bannerSize))
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2011-03-11 10:21:46 +00:00
|
|
|
PanicAlertT("Failed to read banner.bin");
|
|
|
|
b_valid = false;
|
|
|
|
return;
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
2013-08-18 21:58:23 +00:00
|
|
|
// remove nocopy flag
|
|
|
|
_header.BNR[7] &= ~1;
|
|
|
|
|
2014-02-17 04:51:41 +00:00
|
|
|
u8 md5_calc[16];
|
2010-06-09 01:37:08 +00:00
|
|
|
md5((u8*)&_header, HEADER_SZ, md5_calc);
|
|
|
|
memcpy(_header.hdr.Md5, md5_calc, 0x10);
|
|
|
|
|
2013-10-27 18:27:07 +00:00
|
|
|
aes_crypt_cbc(&m_AES_ctx, AES_ENCRYPT, HEADER_SZ, SD_IV, (const u8*)&_header, (u8*)&_encryptedHeader);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-08-18 21:58:23 +00:00
|
|
|
File::IOFile fpData_bin(encryptedSavePath, "wb");
|
2011-03-11 10:21:46 +00:00
|
|
|
if (!fpData_bin.WriteBytes(&_encryptedHeader, HEADER_SZ))
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-08-18 21:58:23 +00:00
|
|
|
PanicAlertT("Failed to write header for %s", encryptedSavePath.c_str());
|
2011-03-11 10:21:46 +00:00
|
|
|
b_valid = false;
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CWiiSaveCrypted::ReadBKHDR()
|
|
|
|
{
|
|
|
|
if (!b_valid) return;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-08-18 21:58:23 +00:00
|
|
|
File::IOFile fpData_bin(encryptedSavePath, "rb");
|
2010-06-09 01:37:08 +00:00
|
|
|
if (!fpData_bin)
|
|
|
|
{
|
2013-08-18 21:58:23 +00:00
|
|
|
PanicAlertT("Cannot open %s", encryptedSavePath.c_str());
|
2010-06-09 01:37:08 +00:00
|
|
|
b_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
2011-03-11 10:21:46 +00:00
|
|
|
fpData_bin.Seek(HEADER_SZ, SEEK_SET);
|
|
|
|
if (!fpData_bin.ReadBytes(&bkhdr, BK_SZ))
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-03-31 23:10:21 +00:00
|
|
|
PanicAlertT("Failed to read bk header");
|
2010-06-09 01:37:08 +00:00
|
|
|
b_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
2011-03-11 10:21:46 +00:00
|
|
|
fpData_bin.Close();
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
if (bkhdr.size != Common::swap32(BK_LISTED_SZ) ||
|
|
|
|
bkhdr.magic != Common::swap32(BK_HDR_MAGIC))
|
|
|
|
{
|
2011-01-13 02:05:58 +00:00
|
|
|
PanicAlertT("Invalid Size(%x) or Magic word (%x)", bkhdr.size, bkhdr.magic);
|
2010-06-09 01:37:08 +00:00
|
|
|
b_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
_numberOfFiles = Common::swap32(bkhdr.numberOfFiles);
|
|
|
|
_sizeOfFiles = Common::swap32(bkhdr.sizeOfFiles);
|
|
|
|
_totalSize = Common::swap32(bkhdr.totalSize);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
if (_sizeOfFiles + FULL_CERT_SZ != _totalSize)
|
|
|
|
WARN_LOG(CONSOLE, "Size(%x) + cert(%x) does not equal totalsize(%x)", _sizeOfFiles, FULL_CERT_SZ, _totalSize);
|
2011-06-27 06:19:23 +00:00
|
|
|
if (m_TitleID != Common::swap64(bkhdr.SaveGameTitle))
|
2013-10-26 09:55:41 +00:00
|
|
|
WARN_LOG(CONSOLE, "Encrypted title (%" PRIx64 ") does not match unencrypted title (%" PRIx64 ")", m_TitleID, Common::swap64(bkhdr.SaveGameTitle));
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CWiiSaveCrypted::WriteBKHDR()
|
|
|
|
{
|
|
|
|
if (!b_valid) return;
|
|
|
|
_numberOfFiles = 0;
|
|
|
|
_sizeOfFiles = 0;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2011-06-27 06:19:23 +00:00
|
|
|
ScanForFiles(WiiTitlePath, FilesList, &_numberOfFiles, &_sizeOfFiles);
|
2010-06-09 01:37:08 +00:00
|
|
|
memset(&bkhdr, 0, BK_SZ);
|
|
|
|
bkhdr.size = Common::swap32(BK_LISTED_SZ);
|
|
|
|
bkhdr.magic = Common::swap32(BK_HDR_MAGIC);
|
2011-06-27 20:48:15 +00:00
|
|
|
bkhdr.NGid = NG_id;
|
2010-06-09 01:37:08 +00:00
|
|
|
bkhdr.numberOfFiles = Common::swap32(_numberOfFiles);
|
|
|
|
bkhdr.sizeOfFiles = Common::swap32(_sizeOfFiles);
|
|
|
|
bkhdr.totalSize = Common::swap32(_sizeOfFiles + FULL_CERT_SZ);
|
2011-06-27 06:19:23 +00:00
|
|
|
bkhdr.SaveGameTitle = Common::swap64(m_TitleID);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-08-18 21:58:23 +00:00
|
|
|
File::IOFile fpData_bin(encryptedSavePath, "ab");
|
2011-03-11 10:21:46 +00:00
|
|
|
if (!fpData_bin.WriteBytes(&bkhdr, BK_SZ))
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2011-03-11 10:21:46 +00:00
|
|
|
PanicAlertT("Failed to write bkhdr");
|
|
|
|
b_valid = false;
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CWiiSaveCrypted::ImportWiiSaveFiles()
|
|
|
|
{
|
|
|
|
if (!b_valid) return;
|
|
|
|
|
2013-08-18 21:58:23 +00:00
|
|
|
File::IOFile fpData_bin(encryptedSavePath, "rb");
|
2010-06-09 01:37:08 +00:00
|
|
|
if (!fpData_bin)
|
|
|
|
{
|
2013-08-18 21:58:23 +00:00
|
|
|
PanicAlertT("Cannot open %s", encryptedSavePath.c_str());
|
2010-06-09 01:37:08 +00:00
|
|
|
b_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-08 02:56:09 +00:00
|
|
|
fpData_bin.Seek(HEADER_SZ + BK_SZ, SEEK_SET);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
FileHDR _tmpFileHDR;
|
|
|
|
|
2014-03-10 11:30:55 +00:00
|
|
|
for (u32 i = 0; i < _numberOfFiles; i++)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
|
|
|
memset(&_tmpFileHDR, 0, FILE_HDR_SZ);
|
|
|
|
memset(IV, 0, 0x10);
|
2013-08-18 21:58:23 +00:00
|
|
|
u32 _fileSize = 0;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-04-08 05:16:50 +00:00
|
|
|
if (!fpData_bin.ReadBytes(&_tmpFileHDR, FILE_HDR_SZ))
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-08-18 21:58:23 +00:00
|
|
|
PanicAlertT("Failed to read header for file %d", i);
|
2010-06-09 01:37:08 +00:00
|
|
|
b_valid = false;
|
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2011-03-11 10:21:46 +00:00
|
|
|
if (Common::swap32(_tmpFileHDR.magic) != FILE_HDR_MAGIC)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2011-01-13 02:05:58 +00:00
|
|
|
PanicAlertT("Bad File Header");
|
2010-06-09 01:37:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-22 00:48:59 +00:00
|
|
|
std::string fileName ((char*)_tmpFileHDR.name);
|
|
|
|
for (Common::replace_v::const_iterator iter = replacements.begin(); iter != replacements.end(); ++iter)
|
|
|
|
{
|
|
|
|
for (size_t j = 0; (j = fileName.find(iter->first, j)) != fileName.npos; ++j)
|
|
|
|
fileName.replace(j, 1, iter->second);
|
|
|
|
}
|
|
|
|
|
2011-06-27 06:19:23 +00:00
|
|
|
std::string fullFilePath = WiiTitlePath + fileName;
|
2013-10-29 05:23:17 +00:00
|
|
|
File::CreateFullPath(fullFilePath);
|
2010-06-09 01:37:08 +00:00
|
|
|
if (_tmpFileHDR.type == 1)
|
|
|
|
{
|
|
|
|
_fileSize = Common::swap32(_tmpFileHDR.size);
|
2011-05-08 00:37:44 +00:00
|
|
|
u32 RoundedFileSize = ROUND_UP(_fileSize, BLOCK_SZ);
|
2013-08-18 21:58:23 +00:00
|
|
|
std::vector<u8> _data,_encryptedData;
|
|
|
|
_data.reserve(RoundedFileSize);
|
|
|
|
_encryptedData.reserve(RoundedFileSize);
|
|
|
|
if (!fpData_bin.ReadBytes(&_encryptedData[0], RoundedFileSize))
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2011-01-13 02:05:58 +00:00
|
|
|
PanicAlertT("Failed to read data from file %d", i);
|
2010-06-09 01:37:08 +00:00
|
|
|
b_valid = false;
|
|
|
|
break;
|
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
|
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
memcpy(IV, _tmpFileHDR.IV, 0x10);
|
2013-10-29 05:23:17 +00:00
|
|
|
aes_crypt_cbc(&m_AES_ctx, AES_DECRYPT, RoundedFileSize, IV, (const u8*)&_encryptedData[0], &_data[0]);
|
|
|
|
|
2011-06-27 06:19:23 +00:00
|
|
|
if (!File::Exists(fullFilePath) || AskYesNoT("%s already exists, overwrite?", fullFilePath.c_str()))
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-03-31 23:10:21 +00:00
|
|
|
INFO_LOG(CONSOLE, "Creating file %s", fullFilePath.c_str());
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2011-06-27 06:19:23 +00:00
|
|
|
File::IOFile fpRawSaveFile(fullFilePath, "wb");
|
2013-08-18 21:58:23 +00:00
|
|
|
fpRawSaveFile.WriteBytes(&_data[0], _fileSize);
|
2013-04-08 05:16:50 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CWiiSaveCrypted::ExportWiiSaveFiles()
|
|
|
|
{
|
|
|
|
if (!b_valid) return;
|
|
|
|
|
2014-03-10 11:30:55 +00:00
|
|
|
for (u32 i = 0; i < _numberOfFiles; i++)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
|
|
|
FileHDR tmpFileHDR;
|
2014-02-23 22:03:39 +00:00
|
|
|
std::string __name;
|
2010-06-09 01:37:08 +00:00
|
|
|
memset(&tmpFileHDR, 0, FILE_HDR_SZ);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-08-18 21:58:23 +00:00
|
|
|
u32 _fileSize = 0;
|
|
|
|
if (File::IsDirectory(FilesList[i]))
|
|
|
|
{
|
2013-10-29 05:23:17 +00:00
|
|
|
tmpFileHDR.type = 2;
|
2013-08-18 21:58:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_fileSize = File::GetSize(FilesList[i]);
|
|
|
|
tmpFileHDR.type = 1;
|
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-08-18 21:58:23 +00:00
|
|
|
u32 _roundedfileSize = ROUND_UP(_fileSize, BLOCK_SZ);
|
2010-06-09 01:37:08 +00:00
|
|
|
tmpFileHDR.magic = Common::swap32(FILE_HDR_MAGIC);
|
|
|
|
tmpFileHDR.size = Common::swap32(_fileSize);
|
2013-08-18 21:58:23 +00:00
|
|
|
tmpFileHDR.Permissions = 0x3c;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-08-18 21:58:23 +00:00
|
|
|
__name = FilesList[i].substr(WiiTitlePath.length()+1);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-02-12 15:00:34 +00:00
|
|
|
for (const Common::replace_t& repl : replacements)
|
2010-12-22 00:48:59 +00:00
|
|
|
{
|
2014-02-12 15:00:34 +00:00
|
|
|
for (size_t j = 0; (j = __name.find(repl.second, j)) != __name.npos; ++j)
|
2010-12-22 00:48:59 +00:00
|
|
|
{
|
2014-02-12 15:00:34 +00:00
|
|
|
__name.replace(j, repl.second.length(), 1, repl.first);
|
2010-12-22 00:48:59 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
if (__name.length() > 0x44)
|
|
|
|
{
|
2014-02-12 15:00:34 +00:00
|
|
|
PanicAlertT("\"%s\" is too long for the filename, max length is 0x44 + \\0", __name.c_str());
|
2010-06-09 01:37:08 +00:00
|
|
|
b_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
2014-02-12 15:00:34 +00:00
|
|
|
strncpy((char *)tmpFileHDR.name, __name.c_str(), sizeof(tmpFileHDR.name));
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-08-18 21:58:23 +00:00
|
|
|
File::IOFile fpData_bin(encryptedSavePath, "ab");
|
2011-03-11 10:21:46 +00:00
|
|
|
fpData_bin.WriteBytes(&tmpFileHDR, FILE_HDR_SZ);
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tmpFileHDR.type == 1)
|
|
|
|
{
|
|
|
|
if (_fileSize == 0)
|
|
|
|
{
|
2011-03-01 03:06:14 +00:00
|
|
|
PanicAlertT("%s is a 0 byte file", FilesList[i].c_str());
|
2010-06-09 01:37:08 +00:00
|
|
|
b_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
2011-03-11 10:21:46 +00:00
|
|
|
File::IOFile fpRawSaveFile(FilesList[i], "rb");
|
2010-06-09 01:37:08 +00:00
|
|
|
if (!fpRawSaveFile)
|
|
|
|
{
|
2011-03-01 03:06:14 +00:00
|
|
|
PanicAlertT("%s failed to open", FilesList[i].c_str());
|
2010-06-09 01:37:08 +00:00
|
|
|
b_valid = false;
|
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-08-18 21:58:23 +00:00
|
|
|
std::vector<u8> _data,_encryptedData;
|
|
|
|
_data.reserve(_roundedfileSize);
|
|
|
|
_encryptedData.reserve(_roundedfileSize);
|
|
|
|
memset(&_data[0], 0, _roundedfileSize);
|
|
|
|
if (!fpRawSaveFile.ReadBytes(&_data[0], _fileSize))
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-03-31 23:10:21 +00:00
|
|
|
PanicAlertT("Failed to read data from file: %s", FilesList[i].c_str());
|
2010-06-09 01:37:08 +00:00
|
|
|
b_valid = false;
|
|
|
|
}
|
|
|
|
|
2013-10-27 18:27:07 +00:00
|
|
|
aes_crypt_cbc(&m_AES_ctx, AES_ENCRYPT, _roundedfileSize, tmpFileHDR.IV, (const u8*)&_data[0], &_encryptedData[0]);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-08-18 21:58:23 +00:00
|
|
|
File::IOFile fpData_bin(encryptedSavePath, "ab");
|
|
|
|
if (!fpData_bin.WriteBytes(&_encryptedData[0], _roundedfileSize))
|
|
|
|
PanicAlertT("Failed to write data to file: %s", encryptedSavePath.c_str());
|
2011-03-11 10:21:46 +00:00
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CWiiSaveCrypted::do_sig()
|
|
|
|
{
|
|
|
|
if (!b_valid) return;
|
|
|
|
u8 sig[0x40];
|
|
|
|
u8 ng_cert[0x180];
|
|
|
|
u8 ap_cert[0x180];
|
|
|
|
u8 hash[0x14];
|
|
|
|
u8 ap_priv[30];
|
|
|
|
u8 ap_sig[60];
|
|
|
|
char signer[64];
|
|
|
|
char name[64];
|
|
|
|
u8 *data;
|
|
|
|
u32 data_size;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2011-06-27 20:48:15 +00:00
|
|
|
u32 NG_key_id = 0x6AAB8C59;
|
|
|
|
|
2014-03-29 10:05:44 +00:00
|
|
|
u8 NG_priv[30] = {
|
2014-02-17 04:51:41 +00:00
|
|
|
0, 0xAB, 0xEE, 0xC1, 0xDD, 0xB4, 0xA6, 0x16, 0x6B, 0x70, 0xFD, 0x7E, 0x56, 0x67, 0x70,
|
|
|
|
0x57, 0x55, 0x27, 0x38, 0xA3, 0x26, 0xC5, 0x46, 0x16, 0xF7, 0x62, 0xC9, 0xED, 0x73, 0xF2
|
|
|
|
};
|
2011-06-27 20:48:15 +00:00
|
|
|
|
2014-02-17 04:51:41 +00:00
|
|
|
u8 NG_sig[0x3C] = {
|
|
|
|
0, 0xD8, 0x81, 0x63, 0xB2, 0x00, 0x6B, 0x0B, 0x54, 0x82, 0x88, 0x63, 0x81, 0x1C, 0x00,
|
|
|
|
0x71, 0x12, 0xED, 0xB7, 0xFD, 0x21, 0xAB, 0x0E, 0x50, 0x0E, 0x1F, 0xBF, 0x78, 0xAD, 0x37,
|
|
|
|
0x00, 0x71, 0x8D, 0x82, 0x41, 0xEE, 0x45, 0x11, 0xC7, 0x3B, 0xAC, 0x08, 0xB6, 0x83, 0xDC,
|
|
|
|
0x05, 0xB8, 0xA8, 0x90, 0x1F, 0xA8, 0x2A, 0x0E, 0x4E, 0x76, 0xEF, 0x44, 0x72, 0x99, 0xF8
|
|
|
|
};
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
sprintf(signer, "Root-CA00000001-MS00000002");
|
2011-06-27 20:48:15 +00:00
|
|
|
sprintf(name, "NG%08x", NG_id);
|
|
|
|
make_ec_cert(ng_cert, NG_sig, signer, name, NG_priv, NG_key_id);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
memset(ap_priv, 0, sizeof ap_priv);
|
|
|
|
ap_priv[10] = 1;
|
|
|
|
|
2014-02-17 04:51:41 +00:00
|
|
|
memset(ap_sig, 81, sizeof ap_sig); // temp
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2011-06-27 20:48:15 +00:00
|
|
|
sprintf(signer, "Root-CA00000001-MS00000002-NG%08x", NG_id);
|
2010-06-09 01:37:08 +00:00
|
|
|
sprintf(name, "AP%08x%08x", 1, 2);
|
|
|
|
make_ec_cert(ap_cert, ap_sig, signer, name, ap_priv, 0);
|
|
|
|
|
|
|
|
sha1(ap_cert + 0x80, 0x100, hash);
|
2011-06-27 20:48:15 +00:00
|
|
|
generate_ecdsa(ap_sig, ap_sig + 30, NG_priv, hash);
|
2010-06-09 01:37:08 +00:00
|
|
|
make_ec_cert(ap_cert, ap_sig, signer, name, ap_priv, 0);
|
|
|
|
|
|
|
|
data_size = Common::swap32(bkhdr.sizeOfFiles) + 0x80;
|
|
|
|
|
2013-08-18 21:58:23 +00:00
|
|
|
File::IOFile fpData_bin(encryptedSavePath, "rb");
|
2010-06-09 01:37:08 +00:00
|
|
|
if (!fpData_bin)
|
|
|
|
{
|
|
|
|
b_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data = new u8[data_size];
|
|
|
|
|
2011-03-11 10:21:46 +00:00
|
|
|
fpData_bin.Seek(0xf0c0, SEEK_SET);
|
|
|
|
if (!fpData_bin.ReadBytes(data, data_size))
|
2011-06-27 06:19:23 +00:00
|
|
|
{
|
|
|
|
b_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
2011-03-11 10:21:46 +00:00
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
sha1(data, data_size, hash);
|
|
|
|
sha1(hash, 20, hash);
|
|
|
|
delete []data;
|
2011-03-11 10:21:46 +00:00
|
|
|
|
2013-08-18 21:58:23 +00:00
|
|
|
fpData_bin.Open(encryptedSavePath, "ab");
|
2010-06-09 01:37:08 +00:00
|
|
|
if (!fpData_bin)
|
|
|
|
{
|
|
|
|
b_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
generate_ecdsa(sig, sig + 30, ap_priv, hash);
|
|
|
|
*(u32*)(sig + 60) = Common::swap32(0x2f536969);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2011-06-27 06:19:23 +00:00
|
|
|
fpData_bin.WriteArray(sig, sizeof(sig));
|
|
|
|
fpData_bin.WriteArray(ng_cert, sizeof(ng_cert));
|
|
|
|
fpData_bin.WriteArray(ap_cert, sizeof(ap_cert));
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2011-06-27 06:19:23 +00:00
|
|
|
b_valid = fpData_bin.IsGood();
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CWiiSaveCrypted::make_ec_cert(u8 *cert, u8 *sig, char *signer, char *name, u8 *priv, u32 key_id)
|
|
|
|
{
|
|
|
|
memset(cert, 0, 0x180);
|
|
|
|
*(u32*)cert = Common::swap32(0x10002);
|
2013-04-08 05:16:50 +00:00
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
memcpy(cert + 4, sig, 60);
|
|
|
|
strcpy((char*)cert + 0x80, signer);
|
|
|
|
*(u32*)(cert + 0xc0) = Common::swap32(2);
|
|
|
|
strcpy((char*)cert + 0xc4, name);
|
|
|
|
*(u32*)(cert + 0x104) = Common::swap32(key_id);
|
|
|
|
ec_priv_to_pub(priv, cert + 0x108);
|
|
|
|
}
|
|
|
|
|
2010-12-15 21:23:42 +00:00
|
|
|
bool CWiiSaveCrypted::getPaths(bool forExport)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2011-06-27 06:19:23 +00:00
|
|
|
if (m_TitleID)
|
2013-04-08 05:16:50 +00:00
|
|
|
{
|
2011-06-27 06:19:23 +00:00
|
|
|
WiiTitlePath = Common::GetTitleDataPath(m_TitleID);
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
2013-04-08 05:16:50 +00:00
|
|
|
|
2010-12-15 21:23:42 +00:00
|
|
|
if (forExport)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2011-06-27 06:19:23 +00:00
|
|
|
char GameID[5];
|
|
|
|
sprintf(GameID, "%c%c%c%c",
|
|
|
|
(u8)(m_TitleID >> 24) & 0xFF, (u8)(m_TitleID >> 16) & 0xFF,
|
|
|
|
(u8)(m_TitleID >> 8) & 0xFF, (u8)m_TitleID & 0xFF);
|
2013-04-08 05:16:50 +00:00
|
|
|
|
2014-03-10 11:30:55 +00:00
|
|
|
if (!File::IsDirectory(WiiTitlePath))
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
|
|
|
b_valid = false;
|
2011-06-27 06:19:23 +00:00
|
|
|
PanicAlertT("No save folder found for title %s", GameID);
|
2010-06-09 01:37:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-03-10 11:30:55 +00:00
|
|
|
if (!File::Exists(WiiTitlePath + "banner.bin"))
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
|
|
|
b_valid = false;
|
2011-06-27 06:19:23 +00:00
|
|
|
PanicAlertT("No banner file found for title %s", GameID);
|
2010-06-09 01:37:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
if (encryptedSavePath.length() == 0)
|
2014-05-12 05:23:21 +00:00
|
|
|
{
|
|
|
|
encryptedSavePath = File::GetUserPath(D_USER_IDX); // If no path was passed, use User folder
|
|
|
|
}
|
2013-08-18 21:58:23 +00:00
|
|
|
encryptedSavePath += StringFromFormat("/private/wii/title/%s/data.bin", GameID);
|
|
|
|
File::CreateFullPath(encryptedSavePath);
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-29 05:23:17 +00:00
|
|
|
File::CreateFullPath(WiiTitlePath);
|
2011-06-27 06:19:23 +00:00
|
|
|
if (!AskYesNoT("Warning! it is advised to backup all files in the folder:\n%s\nDo you wish to continue?", WiiTitlePath.c_str()))
|
2010-06-09 01:37:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CWiiSaveCrypted::ScanForFiles(std::string savDir, std::vector<std::string>& FileList, u32 *_numFiles, u32 *_sizeFiles)
|
|
|
|
{
|
|
|
|
std::vector<std::string> Directories;
|
|
|
|
*_numFiles = *_sizeFiles = 0;
|
|
|
|
|
|
|
|
Directories.push_back(savDir);
|
|
|
|
for (u32 i = 0; i < Directories.size(); i++)
|
|
|
|
{
|
2013-04-08 05:16:50 +00:00
|
|
|
if (i != 0)
|
|
|
|
{
|
|
|
|
FileList.push_back(Directories[i]);//add dir to fst
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
File::FSTEntry FST_Temp;
|
2011-03-01 03:06:14 +00:00
|
|
|
File::ScanDirectoryTree(Directories[i], FST_Temp);
|
2014-02-12 15:00:34 +00:00
|
|
|
for (const File::FSTEntry& elem : FST_Temp.children)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
if (strncmp(elem.virtualName.c_str(), "banner.bin", 10) != 0)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
|
|
|
(*_numFiles)++;
|
2013-08-18 21:58:23 +00:00
|
|
|
*_sizeFiles += FILE_HDR_SZ;
|
2013-10-29 05:09:01 +00:00
|
|
|
if (elem.isDirectory)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
if ((elem.virtualName == "nocopy") || elem.virtualName == "nomove")
|
2013-08-18 21:58:23 +00:00
|
|
|
{
|
2014-05-17 17:17:28 +00:00
|
|
|
PanicAlertT("This save will likely require homebrew tools to copy to a real Wii.");
|
2013-08-18 21:58:23 +00:00
|
|
|
}
|
|
|
|
|
2013-10-29 05:09:01 +00:00
|
|
|
Directories.push_back(elem.physicalName);
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
FileList.push_back(elem.physicalName);
|
|
|
|
*_sizeFiles += ROUND_UP(elem.size, BLOCK_SZ);
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CWiiSaveCrypted::~CWiiSaveCrypted()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|