2009-07-28 15:32:10 -06:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2009-03-07 01:35:01 -07:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
2013-04-15 14:28:55 -06:00
|
|
|
// Official Git repository and contact information can be found at
|
2009-03-07 01:35:01 -07:00
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
#include "NANDContentLoader.h"
|
|
|
|
|
2009-03-13 10:15:15 -06:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
2010-12-21 23:00:36 -07:00
|
|
|
#include "Crypto/aes.h"
|
2009-03-07 01:35:01 -07:00
|
|
|
#include "MathUtil.h"
|
|
|
|
#include "FileUtil.h"
|
|
|
|
#include "Log.h"
|
2009-06-16 20:50:54 -06:00
|
|
|
#include "WiiWad.h"
|
2011-05-08 23:47:29 -06:00
|
|
|
#include "StringUtil.h"
|
2009-03-07 01:35:01 -07:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2011-06-05 01:42:40 -06:00
|
|
|
CSharedContent CSharedContent::m_Instance;
|
|
|
|
cUIDsys cUIDsys::m_Instance;
|
2009-03-07 01:35:01 -07:00
|
|
|
|
2011-06-05 00:30:01 -06:00
|
|
|
|
2011-06-05 01:42:40 -06:00
|
|
|
CSharedContent::CSharedContent()
|
2011-06-05 00:30:01 -06:00
|
|
|
{
|
2011-06-05 01:42:40 -06:00
|
|
|
UpdateLocation();
|
2011-06-05 00:30:01 -06:00
|
|
|
}
|
2009-03-09 11:09:27 -06:00
|
|
|
|
2011-06-05 01:42:40 -06:00
|
|
|
void CSharedContent::UpdateLocation()
|
2009-03-09 11:09:27 -06:00
|
|
|
{
|
2011-06-05 01:42:40 -06:00
|
|
|
m_Elements.clear();
|
2010-05-12 22:50:18 -06:00
|
|
|
lastID = 0;
|
2011-02-28 13:40:15 -07:00
|
|
|
sprintf(contentMap, "%sshared1/content.map", File::GetUserPath(D_WIIUSER_IDX).c_str());
|
2010-05-27 23:30:03 -06:00
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
File::IOFile pFile(contentMap, "rb");
|
|
|
|
SElement Element;
|
|
|
|
while (pFile.ReadArray(&Element, 1))
|
2009-07-03 16:34:51 -06:00
|
|
|
{
|
2011-03-11 03:21:46 -07:00
|
|
|
m_Elements.push_back(Element);
|
|
|
|
lastID++;
|
2009-07-03 16:34:51 -06:00
|
|
|
}
|
2009-03-09 11:09:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
CSharedContent::~CSharedContent()
|
2011-06-05 01:42:40 -06:00
|
|
|
{}
|
2009-03-09 11:09:27 -06:00
|
|
|
|
|
|
|
std::string CSharedContent::GetFilenameFromSHA1(u8* _pHash)
|
|
|
|
{
|
2009-07-03 16:34:51 -06:00
|
|
|
for (size_t i=0; i<m_Elements.size(); i++)
|
|
|
|
{
|
|
|
|
if (memcmp(_pHash, m_Elements[i].SHA1Hash, 20) == 0)
|
|
|
|
{
|
|
|
|
char szFilename[1024];
|
2011-02-28 13:40:15 -07:00
|
|
|
sprintf(szFilename, "%sshared1/%c%c%c%c%c%c%c%c.app", File::GetUserPath(D_WIIUSER_IDX).c_str(),
|
2009-07-03 16:34:51 -06:00
|
|
|
m_Elements[i].FileName[0], m_Elements[i].FileName[1], m_Elements[i].FileName[2], m_Elements[i].FileName[3],
|
|
|
|
m_Elements[i].FileName[4], m_Elements[i].FileName[5], m_Elements[i].FileName[6], m_Elements[i].FileName[7]);
|
|
|
|
return szFilename;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "unk";
|
2009-03-09 11:09:27 -06:00
|
|
|
}
|
|
|
|
|
2010-05-12 22:50:18 -06:00
|
|
|
std::string CSharedContent::AddSharedContent(u8* _pHash)
|
|
|
|
{
|
|
|
|
std::string szFilename = GetFilenameFromSHA1(_pHash);
|
|
|
|
if (strcasecmp(szFilename.c_str(), "unk") == 0)
|
|
|
|
{
|
|
|
|
char tempFilename[1024], c_ID[9];
|
|
|
|
SElement Element;
|
|
|
|
sprintf(c_ID, "%08x", lastID);
|
|
|
|
memcpy(Element.FileName, c_ID, 8);
|
|
|
|
memcpy(Element.SHA1Hash, _pHash, 20);
|
|
|
|
m_Elements.push_back(Element);
|
2010-12-15 07:47:13 -07:00
|
|
|
|
|
|
|
File::CreateFullPath(contentMap);
|
2011-03-11 03:21:46 -07:00
|
|
|
|
|
|
|
File::IOFile pFile(contentMap, "ab");
|
|
|
|
pFile.WriteArray(&Element, 1);
|
|
|
|
|
2011-02-28 13:40:15 -07:00
|
|
|
sprintf(tempFilename, "%sshared1/%s.app", File::GetUserPath(D_WIIUSER_IDX).c_str(), c_ID);
|
2010-05-12 22:50:18 -06:00
|
|
|
szFilename = tempFilename;
|
|
|
|
lastID++;
|
|
|
|
}
|
|
|
|
return szFilename;
|
|
|
|
}
|
2009-03-09 11:09:27 -06:00
|
|
|
|
|
|
|
|
2009-03-13 10:15:15 -06:00
|
|
|
// this classes must be created by the CNANDContentManager
|
|
|
|
class CNANDContentLoader : public INANDContentLoader
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2009-07-03 16:34:51 -06:00
|
|
|
CNANDContentLoader(const std::string& _rName);
|
2009-03-13 10:15:15 -06:00
|
|
|
|
2009-07-03 16:34:51 -06:00
|
|
|
virtual ~CNANDContentLoader();
|
2009-03-13 10:15:15 -06:00
|
|
|
|
2009-07-03 16:34:51 -06:00
|
|
|
bool IsValid() const { return m_Valid; }
|
2011-03-04 22:18:21 -07:00
|
|
|
void RemoveTitle(void) const;
|
2009-07-03 16:34:51 -06:00
|
|
|
u64 GetTitleID() const { return m_TitleID; }
|
2009-06-08 12:07:45 -06:00
|
|
|
u16 GetIosVersion() const { return m_IosVersion; }
|
2009-07-03 16:34:51 -06:00
|
|
|
u32 GetBootIndex() const { return m_BootIndex; }
|
|
|
|
size_t GetContentSize() const { return m_Content.size(); }
|
|
|
|
const SNANDContent* GetContentByIndex(int _Index) const;
|
2011-05-08 23:47:29 -06:00
|
|
|
const u8* GetTMDView() const { return m_TMDView; }
|
|
|
|
const u8* GetTMDHeader() const { return m_TMDHeader; }
|
2011-05-09 01:43:02 -06:00
|
|
|
u32 GetTIKSize() const { return m_TIKSize; }
|
2011-05-08 23:47:29 -06:00
|
|
|
const u8* GetTIK() const { return m_TIK; }
|
2009-03-13 10:15:15 -06:00
|
|
|
|
2009-07-03 16:34:51 -06:00
|
|
|
const std::vector<SNANDContent>& GetContent() const { return m_Content; }
|
2009-03-13 10:15:15 -06:00
|
|
|
|
2011-05-08 23:47:29 -06:00
|
|
|
u16 GetTitleVersion() const {return m_TitleVersion;}
|
2010-08-01 10:14:35 -06:00
|
|
|
u16 GetNumEntries() const {return m_numEntries;}
|
|
|
|
DiscIO::IVolume::ECountry GetCountry() const;
|
2010-12-16 00:36:26 -07:00
|
|
|
u8 GetCountryChar() const {return m_Country; }
|
2009-03-13 10:15:15 -06:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2009-07-03 16:34:51 -06:00
|
|
|
bool m_Valid;
|
2011-05-08 23:47:29 -06:00
|
|
|
bool m_isWAD;
|
|
|
|
std::string m_Path;
|
2009-07-03 16:34:51 -06:00
|
|
|
u64 m_TitleID;
|
2009-06-08 12:07:45 -06:00
|
|
|
u16 m_IosVersion;
|
2009-07-03 16:34:51 -06:00
|
|
|
u32 m_BootIndex;
|
|
|
|
u16 m_numEntries;
|
2011-05-08 23:47:29 -06:00
|
|
|
u16 m_TitleVersion;
|
|
|
|
u8 m_TMDView[TMD_VIEW_SIZE];
|
|
|
|
u8 m_TMDHeader[TMD_HEADER_SIZE];
|
|
|
|
u32 m_TIKSize;
|
|
|
|
u8 *m_TIK;
|
2010-12-16 00:36:26 -07:00
|
|
|
u8 m_Country;
|
2009-03-13 10:15:15 -06:00
|
|
|
|
2009-07-03 16:34:51 -06:00
|
|
|
std::vector<SNANDContent> m_Content;
|
2009-03-13 10:15:15 -06:00
|
|
|
|
2009-06-15 16:27:46 -06:00
|
|
|
|
2011-05-08 23:47:29 -06:00
|
|
|
bool Initialize(const std::string& _rName);
|
2009-03-13 10:15:15 -06:00
|
|
|
|
2009-07-03 16:34:51 -06:00
|
|
|
void AESDecode(u8* _pKey, u8* _IV, u8* _pSrc, u32 _Size, u8* _pDest);
|
2009-03-13 10:15:15 -06:00
|
|
|
|
2009-07-03 16:34:51 -06:00
|
|
|
void GetKeyFromTicket(u8* pTicket, u8* pTicketKey);
|
2009-03-13 10:15:15 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-03-07 01:35:01 -07:00
|
|
|
|
|
|
|
|
|
|
|
CNANDContentLoader::CNANDContentLoader(const std::string& _rName)
|
2009-09-01 12:34:26 -06:00
|
|
|
: m_Valid(false)
|
2011-05-08 23:47:29 -06:00
|
|
|
, m_isWAD(false)
|
2009-09-01 12:34:26 -06:00
|
|
|
, m_TitleID(-1)
|
2009-06-08 12:07:45 -06:00
|
|
|
, m_IosVersion(0x09)
|
2009-09-01 12:34:26 -06:00
|
|
|
, m_BootIndex(-1)
|
2011-05-16 16:46:35 -06:00
|
|
|
, m_TIKSize(0)
|
2011-05-08 23:47:29 -06:00
|
|
|
, m_TIK(NULL)
|
2009-03-07 01:35:01 -07:00
|
|
|
{
|
2011-05-08 23:47:29 -06:00
|
|
|
m_Valid = Initialize(_rName);
|
2009-03-07 01:35:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
CNANDContentLoader::~CNANDContentLoader()
|
|
|
|
{
|
2009-07-03 16:34:51 -06:00
|
|
|
for (size_t i=0; i<m_Content.size(); i++)
|
|
|
|
{
|
|
|
|
delete [] m_Content[i].m_pData;
|
|
|
|
}
|
|
|
|
m_Content.clear();
|
2011-05-08 23:47:29 -06:00
|
|
|
if (m_TIK)
|
|
|
|
{
|
|
|
|
delete []m_TIK;
|
|
|
|
m_TIK = NULL;
|
|
|
|
}
|
2009-03-07 01:35:01 -07:00
|
|
|
}
|
|
|
|
|
2009-03-13 10:15:15 -06:00
|
|
|
const SNANDContent* CNANDContentLoader::GetContentByIndex(int _Index) const
|
2009-03-07 01:35:01 -07:00
|
|
|
{
|
2009-07-03 16:34:51 -06:00
|
|
|
for (size_t i=0; i<m_Content.size(); i++)
|
|
|
|
{
|
|
|
|
if (m_Content[i].m_Index == _Index)
|
|
|
|
return &m_Content[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
2009-03-07 01:35:01 -07:00
|
|
|
}
|
|
|
|
|
2011-05-08 23:47:29 -06:00
|
|
|
bool CNANDContentLoader::Initialize(const std::string& _rName)
|
2009-03-07 01:35:01 -07:00
|
|
|
{
|
2011-08-21 22:09:18 -06:00
|
|
|
if (_rName.empty())
|
|
|
|
return false;
|
2011-05-08 23:47:29 -06:00
|
|
|
m_Path = _rName;
|
2009-06-16 20:50:54 -06:00
|
|
|
WiiWAD Wad(_rName);
|
2011-05-08 23:47:29 -06:00
|
|
|
u8* pDataApp = NULL;
|
|
|
|
u8* pTMD = NULL;
|
|
|
|
u8 DecryptTitleKey[16];
|
|
|
|
u8 IV[16];
|
|
|
|
if (Wad.IsValid())
|
2011-03-11 03:21:46 -07:00
|
|
|
{
|
2011-05-08 23:47:29 -06:00
|
|
|
m_isWAD = true;
|
|
|
|
m_TIKSize = Wad.GetTicketSize();
|
|
|
|
m_TIK = new u8[m_TIKSize];
|
|
|
|
memcpy(m_TIK, Wad.GetTicket(), m_TIKSize);
|
|
|
|
GetKeyFromTicket(m_TIK, DecryptTitleKey);
|
|
|
|
u32 pTMDSize = Wad.GetTMDSize();
|
|
|
|
pTMD = new u8[pTMDSize];
|
|
|
|
memcpy(pTMD, Wad.GetTMD(), pTMDSize);
|
|
|
|
pDataApp = Wad.GetDataApp();
|
2009-03-07 01:35:01 -07:00
|
|
|
}
|
2011-05-08 23:47:29 -06:00
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string TMDFileName(m_Path);
|
2011-07-17 19:47:55 -06:00
|
|
|
|
2013-02-17 00:57:25 -07:00
|
|
|
if ('/' == *TMDFileName.rbegin())
|
2011-05-08 23:47:29 -06:00
|
|
|
TMDFileName += "title.tmd";
|
|
|
|
else
|
2011-06-02 01:04:26 -06:00
|
|
|
m_Path = TMDFileName.substr(0, TMDFileName.find("title.tmd"));
|
2011-07-17 19:47:55 -06:00
|
|
|
|
2011-05-08 23:47:29 -06:00
|
|
|
File::IOFile pTMDFile(TMDFileName, "rb");
|
|
|
|
if (!pTMDFile)
|
|
|
|
{
|
2011-08-21 22:09:18 -06:00
|
|
|
DEBUG_LOG(DISCIO, "CreateFromDirectory: error opening %s",
|
2011-05-08 23:47:29 -06:00
|
|
|
TMDFileName.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
u32 pTMDSize = (u32)File::GetSize(TMDFileName);
|
|
|
|
pTMD = new u8[pTMDSize];
|
|
|
|
pTMDFile.ReadBytes(pTMD, (size_t)pTMDSize);
|
|
|
|
pTMDFile.Close();
|
|
|
|
}
|
|
|
|
if (!pTMD)
|
|
|
|
return false;
|
|
|
|
memcpy(m_TMDView, pTMD + 0x180, TMD_VIEW_SIZE);
|
|
|
|
memcpy(m_TMDHeader, pTMD, TMD_HEADER_SIZE);
|
2009-03-07 01:35:01 -07:00
|
|
|
|
2009-03-09 17:21:05 -06:00
|
|
|
|
2011-05-08 23:47:29 -06:00
|
|
|
m_TitleVersion = Common::swap16(pTMD + 0x01dc);
|
2009-07-03 16:34:51 -06:00
|
|
|
m_numEntries = Common::swap16(pTMD + 0x01de);
|
|
|
|
m_BootIndex = Common::swap16(pTMD + 0x01e0);
|
|
|
|
m_TitleID = Common::swap64(pTMD + 0x018c);
|
|
|
|
m_IosVersion = Common::swap16(pTMD + 0x018a);
|
2010-12-16 00:36:26 -07:00
|
|
|
m_Country = *(u8*)&m_TitleID;
|
|
|
|
if (m_Country == 2) // SYSMENU
|
2011-05-08 23:47:29 -06:00
|
|
|
m_Country = GetSysMenuRegion(m_TitleVersion);
|
2009-07-03 16:34:51 -06:00
|
|
|
|
|
|
|
m_Content.resize(m_numEntries);
|
|
|
|
|
2011-05-08 23:47:29 -06:00
|
|
|
|
|
|
|
for (u32 i=0; i<m_numEntries; i++)
|
2009-07-03 16:34:51 -06:00
|
|
|
{
|
|
|
|
SNANDContent& rContent = m_Content[i];
|
2011-05-08 23:47:29 -06:00
|
|
|
|
2009-07-03 16:34:51 -06:00
|
|
|
rContent.m_ContentID = Common::swap32(pTMD + 0x01e4 + 0x24*i);
|
|
|
|
rContent.m_Index = Common::swap16(pTMD + 0x01e8 + 0x24*i);
|
|
|
|
rContent.m_Type = Common::swap16(pTMD + 0x01ea + 0x24*i);
|
2011-05-08 23:47:29 -06:00
|
|
|
rContent.m_Size= (u32)Common::swap64(pTMD + 0x01ec + 0x24*i);
|
2009-07-03 16:34:51 -06:00
|
|
|
memcpy(rContent.m_SHA1Hash, pTMD + 0x01f4 + 0x24*i, 20);
|
|
|
|
memcpy(rContent.m_Header, pTMD + 0x01e4 + 0x24*i, 36);
|
2013-04-16 21:14:36 -06:00
|
|
|
|
2011-05-08 23:47:29 -06:00
|
|
|
if (m_isWAD)
|
|
|
|
{
|
2013-04-16 21:14:36 -06:00
|
|
|
u32 RoundedSize = ROUND_UP(rContent.m_Size, 0x40);
|
|
|
|
rContent.m_pData = new u8[RoundedSize];
|
2011-05-08 23:47:29 -06:00
|
|
|
|
2013-04-16 21:14:36 -06:00
|
|
|
memset(IV, 0, sizeof IV);
|
|
|
|
memcpy(IV, pTMD + 0x01e8 + 0x24*i, 2);
|
|
|
|
AESDecode(DecryptTitleKey, IV, pDataApp, RoundedSize, rContent.m_pData);
|
2011-05-08 23:47:29 -06:00
|
|
|
|
2013-04-16 21:14:36 -06:00
|
|
|
pDataApp += RoundedSize;
|
|
|
|
continue;
|
2011-05-08 23:47:29 -06:00
|
|
|
}
|
2009-07-03 16:34:51 -06:00
|
|
|
|
2013-03-19 07:59:41 -06:00
|
|
|
rContent.m_pData = NULL;
|
2009-07-03 16:34:51 -06:00
|
|
|
char szFilename[1024];
|
|
|
|
|
|
|
|
if (rContent.m_Type & 0x8000) // shared app
|
|
|
|
{
|
|
|
|
std::string Filename = CSharedContent::AccessInstance().GetFilenameFromSHA1(rContent.m_SHA1Hash);
|
|
|
|
strcpy(szFilename, Filename.c_str());
|
|
|
|
}
|
|
|
|
else
|
2013-04-16 21:14:36 -06:00
|
|
|
{
|
2011-05-08 23:47:29 -06:00
|
|
|
sprintf(szFilename, "%s/%08x.app", m_Path.c_str(), rContent.m_ContentID);
|
2013-04-16 21:14:36 -06:00
|
|
|
}
|
2009-07-03 16:34:51 -06:00
|
|
|
|
|
|
|
INFO_LOG(DISCIO, "NANDContentLoader: load %s", szFilename);
|
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
File::IOFile pFile(szFilename, "rb");
|
|
|
|
if (pFile)
|
2009-07-03 16:34:51 -06:00
|
|
|
{
|
2011-03-11 03:21:46 -07:00
|
|
|
const u64 ContentSize = File::GetSize(szFilename);
|
2009-08-03 16:51:13 -06:00
|
|
|
rContent.m_pData = new u8[(u32)ContentSize];
|
2009-07-03 16:34:51 -06:00
|
|
|
|
2011-07-17 19:47:55 -06:00
|
|
|
_dbg_assert_msg_(BOOT, rContent.m_Size==ContentSize, "TMDLoader: Incorrect filesize (%s %i). Your NAND dump may be corrupt.", szFilename, i);
|
2009-07-03 16:34:51 -06:00
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
pFile.ReadBytes(rContent.m_pData, (size_t)ContentSize);
|
2009-07-03 16:34:51 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-08-31 16:42:10 -06:00
|
|
|
ERROR_LOG(DISCIO, "NANDContentLoader: error opening %s", szFilename);
|
2010-09-07 00:06:08 -06:00
|
|
|
delete [] pTMD;
|
2010-05-27 23:30:03 -06:00
|
|
|
return false;
|
2009-03-07 01:35:01 -07:00
|
|
|
}
|
2009-07-03 16:34:51 -06:00
|
|
|
}
|
2009-03-07 01:35:01 -07:00
|
|
|
|
2010-03-02 23:59:37 -07:00
|
|
|
delete [] pTMD;
|
2009-07-03 16:34:51 -06:00
|
|
|
return true;
|
2009-03-07 01:35:01 -07:00
|
|
|
}
|
|
|
|
void CNANDContentLoader::AESDecode(u8* _pKey, u8* _IV, u8* _pSrc, u32 _Size, u8* _pDest)
|
|
|
|
{
|
2009-07-03 16:34:51 -06:00
|
|
|
AES_KEY AESKey;
|
2009-03-07 01:35:01 -07:00
|
|
|
|
2009-07-03 16:34:51 -06:00
|
|
|
AES_set_decrypt_key(_pKey, 128, &AESKey);
|
|
|
|
AES_cbc_encrypt(_pSrc, _pDest, _Size, &AESKey, _IV, AES_DECRYPT);
|
2009-03-07 01:35:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void CNANDContentLoader::GetKeyFromTicket(u8* pTicket, u8* pTicketKey)
|
|
|
|
{
|
|
|
|
u8 CommonKey[16] = {0xeb,0xe4,0x2a,0x22,0x5e,0x85,0x93,0xe4,0x48,0xd9,0xc5,0x45,0x73,0x81,0xaa,0xf7};
|
2009-07-03 16:34:51 -06:00
|
|
|
u8 IV[16];
|
|
|
|
memset(IV, 0, sizeof IV);
|
|
|
|
memcpy(IV, pTicket + 0x01dc, 8);
|
|
|
|
AESDecode(CommonKey, IV, pTicket + 0x01bf, 16, pTicketKey);
|
2009-03-07 01:35:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-01 10:14:35 -06:00
|
|
|
DiscIO::IVolume::ECountry CNANDContentLoader::GetCountry() const
|
2009-03-14 13:13:21 -06:00
|
|
|
{
|
2009-07-03 16:34:51 -06:00
|
|
|
if (!IsValid())
|
|
|
|
return DiscIO::IVolume::COUNTRY_UNKNOWN;
|
|
|
|
|
2010-12-16 00:36:26 -07:00
|
|
|
return CountrySwitch(m_Country);
|
2009-07-03 16:34:51 -06:00
|
|
|
}
|
2009-03-07 01:35:01 -07:00
|
|
|
|
2009-03-13 10:15:15 -06:00
|
|
|
|
|
|
|
CNANDContentManager CNANDContentManager::m_Instance;
|
|
|
|
|
|
|
|
|
|
|
|
CNANDContentManager::~CNANDContentManager()
|
|
|
|
{
|
2009-07-03 16:34:51 -06:00
|
|
|
CNANDContentMap::iterator itr = m_Map.begin();
|
|
|
|
while (itr != m_Map.end())
|
|
|
|
{
|
|
|
|
delete itr->second;
|
2010-05-28 17:14:16 -06:00
|
|
|
++itr;
|
2009-07-03 16:34:51 -06:00
|
|
|
}
|
|
|
|
m_Map.clear();
|
2009-03-13 10:15:15 -06:00
|
|
|
}
|
|
|
|
|
2011-03-04 22:07:44 -07:00
|
|
|
const INANDContentLoader& CNANDContentManager::GetNANDLoader(const std::string& _rName, bool forceReload)
|
2009-03-13 10:15:15 -06:00
|
|
|
{
|
2009-10-24 20:03:45 -06:00
|
|
|
CNANDContentMap::iterator lb = m_Map.lower_bound(_rName);
|
2009-03-13 10:15:15 -06:00
|
|
|
|
2009-10-24 20:03:45 -06:00
|
|
|
if(lb == m_Map.end() || (m_Map.key_comp()(_rName, lb->first)))
|
2011-03-04 22:07:44 -07:00
|
|
|
{
|
2009-10-24 20:03:45 -06:00
|
|
|
m_Map.insert(lb, CNANDContentMap::value_type(_rName, new CNANDContentLoader(_rName)));
|
2011-03-04 22:07:44 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!lb->second->IsValid() || forceReload)
|
|
|
|
{
|
|
|
|
delete lb->second;
|
|
|
|
lb->second = new CNANDContentLoader(_rName);
|
|
|
|
}
|
|
|
|
}
|
2009-10-24 20:03:45 -06:00
|
|
|
return *m_Map[_rName];
|
2009-03-13 10:15:15 -06:00
|
|
|
}
|
|
|
|
|
2011-03-04 22:07:44 -07:00
|
|
|
const INANDContentLoader& CNANDContentManager::GetNANDLoader(u64 _titleId, bool forceReload)
|
2010-09-05 22:36:58 -06:00
|
|
|
{
|
2011-05-08 23:47:29 -06:00
|
|
|
std::string _rName = Common::GetTitleContentPath(_titleId);
|
2011-03-04 22:07:44 -07:00
|
|
|
return GetNANDLoader(_rName, forceReload);
|
2010-09-05 22:36:58 -06:00
|
|
|
}
|
2011-03-04 22:18:21 -07:00
|
|
|
bool CNANDContentManager::RemoveTitle(u64 _titleID)
|
|
|
|
{
|
|
|
|
if (!GetNANDLoader(_titleID).IsValid())
|
|
|
|
return false;
|
|
|
|
GetNANDLoader(_titleID).RemoveTitle();
|
|
|
|
return GetNANDLoader(_titleID, true).IsValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CNANDContentLoader::RemoveTitle() const
|
|
|
|
{
|
|
|
|
INFO_LOG(DISCIO, "RemoveTitle %08x/%08x", (u32)(m_TitleID >> 32), (u32)m_TitleID);
|
|
|
|
if(IsValid())
|
|
|
|
{
|
|
|
|
// remove tmd?
|
|
|
|
for (u32 i = 0; i < m_numEntries; i++)
|
|
|
|
{
|
|
|
|
char szFilename[1024];
|
|
|
|
if (!(m_Content[i].m_Type & 0x8000)) // skip shared apps
|
|
|
|
{
|
2011-05-08 23:47:29 -06:00
|
|
|
sprintf(szFilename, "%s%08x.app", Common::GetTitleContentPath(m_TitleID).c_str(), m_Content[i].m_ContentID);
|
2011-03-04 22:18:21 -07:00
|
|
|
INFO_LOG(DISCIO, "Delete %s", szFilename);
|
|
|
|
File::Delete(szFilename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-05 22:36:58 -06:00
|
|
|
|
2011-06-05 01:42:40 -06:00
|
|
|
cUIDsys::cUIDsys()
|
2011-06-05 00:30:01 -06:00
|
|
|
{
|
2011-06-05 01:42:40 -06:00
|
|
|
UpdateLocation();
|
2011-06-05 00:30:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void cUIDsys::UpdateLocation()
|
|
|
|
{
|
2011-06-05 01:42:40 -06:00
|
|
|
m_Elements.clear();
|
2010-05-12 22:50:18 -06:00
|
|
|
lastUID = 0x00001000;
|
2011-06-05 01:42:40 -06:00
|
|
|
sprintf(uidSys, "%ssys/uid.sys", File::GetUserPath(D_WIIUSER_IDX).c_str());
|
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
File::IOFile pFile(uidSys, "rb");
|
|
|
|
SElement Element;
|
|
|
|
while (pFile.ReadArray(&Element, 1))
|
2010-05-12 22:50:18 -06:00
|
|
|
{
|
2011-03-11 03:21:46 -07:00
|
|
|
*(u32*)&(Element.UID) = Common::swap32(lastUID++);
|
|
|
|
m_Elements.push_back(Element);
|
2010-05-12 22:50:18 -06:00
|
|
|
}
|
2011-03-11 03:21:46 -07:00
|
|
|
pFile.Close();
|
|
|
|
|
|
|
|
if (m_Elements.empty())
|
2010-05-12 22:50:18 -06:00
|
|
|
{
|
2010-09-05 22:36:58 -06:00
|
|
|
*(u64*)&(Element.titleID) = Common::swap64(TITLEID_SYSMENU);
|
2010-05-27 23:30:03 -06:00
|
|
|
*(u32*)&(Element.UID) = Common::swap32(lastUID++);
|
|
|
|
|
2010-12-15 07:47:13 -07:00
|
|
|
File::CreateFullPath(uidSys);
|
2011-03-11 03:21:46 -07:00
|
|
|
pFile.Open(uidSys, "wb");
|
|
|
|
if (!pFile.WriteArray(&Element, 1))
|
|
|
|
ERROR_LOG(DISCIO, "Failed to write to %s", uidSys);
|
2010-05-12 22:50:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cUIDsys::~cUIDsys()
|
2011-06-05 01:42:40 -06:00
|
|
|
{}
|
2010-05-12 22:50:18 -06:00
|
|
|
|
|
|
|
u32 cUIDsys::GetUIDFromTitle(u64 _Title)
|
|
|
|
{
|
|
|
|
for (size_t i=0; i<m_Elements.size(); i++)
|
|
|
|
{
|
2010-09-07 00:06:08 -06:00
|
|
|
if (Common::swap64(_Title) == *(u64*)&(m_Elements[i].titleID))
|
2010-05-12 22:50:18 -06:00
|
|
|
{
|
|
|
|
return Common::swap32(m_Elements[i].UID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-08 23:47:29 -06:00
|
|
|
void cUIDsys::AddTitle(u64 _TitleID)
|
2010-05-12 22:50:18 -06:00
|
|
|
{
|
|
|
|
if (GetUIDFromTitle(_TitleID))
|
|
|
|
{
|
2011-05-08 23:47:29 -06:00
|
|
|
INFO_LOG(DISCIO, "Title %08x%08x, already exists in uid.sys", (u32)(_TitleID >> 32), (u32)_TitleID);
|
|
|
|
return;
|
|
|
|
}
|
2010-12-15 07:47:13 -07:00
|
|
|
|
2011-05-08 23:47:29 -06:00
|
|
|
SElement Element;
|
|
|
|
*(u64*)&(Element.titleID) = Common::swap64(_TitleID);
|
|
|
|
*(u32*)&(Element.UID) = Common::swap32(lastUID++);
|
|
|
|
m_Elements.push_back(Element);
|
2011-03-11 03:21:46 -07:00
|
|
|
|
2011-05-08 23:47:29 -06:00
|
|
|
File::CreateFullPath(uidSys);
|
|
|
|
File::IOFile pFile(uidSys, "ab");
|
|
|
|
|
2012-03-30 21:07:11 -06:00
|
|
|
if (!pFile.WriteArray(&Element, 1))
|
2011-05-08 23:47:29 -06:00
|
|
|
ERROR_LOG(DISCIO, "fwrite failed");
|
2010-05-12 22:50:18 -06:00
|
|
|
}
|
|
|
|
|
2010-09-07 00:06:08 -06:00
|
|
|
void cUIDsys::GetTitleIDs(std::vector<u64>& _TitleIDs, bool _owned)
|
2010-05-12 22:50:18 -06:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_Elements.size(); i++)
|
|
|
|
{
|
2010-09-07 00:06:08 -06:00
|
|
|
if ((_owned && Common::CheckTitleTIK(Common::swap64(m_Elements[i].titleID))) ||
|
|
|
|
(!_owned && Common::CheckTitleTMD(Common::swap64(m_Elements[i].titleID))))
|
|
|
|
_TitleIDs.push_back(Common::swap64(m_Elements[i].titleID));
|
2010-05-27 23:30:03 -06:00
|
|
|
}
|
2010-05-12 22:50:18 -06:00
|
|
|
}
|
|
|
|
|
2011-05-08 23:47:29 -06:00
|
|
|
u64 CNANDContentManager::Install_WiiWAD(std::string &fileName)
|
|
|
|
{
|
|
|
|
if (fileName.find(".wad") == std::string::npos)
|
|
|
|
return 0;
|
|
|
|
const INANDContentLoader& ContentLoader = GetNANDLoader(fileName);
|
|
|
|
if (ContentLoader.IsValid() == false)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
u64 TitleID = ContentLoader.GetTitleID();
|
|
|
|
|
|
|
|
//copy WAD's tmd header and contents to content directory
|
|
|
|
|
|
|
|
std::string ContentPath(Common::GetTitleContentPath(TitleID));
|
|
|
|
std::string TMDFileName(Common::GetTMDFileName(TitleID));
|
|
|
|
File::CreateFullPath(TMDFileName);
|
|
|
|
|
|
|
|
File::IOFile pTMDFile(TMDFileName, "wb");
|
|
|
|
if (!pTMDFile)
|
|
|
|
{
|
|
|
|
PanicAlertT("WAD installation failed: error creating %s", TMDFileName.c_str());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pTMDFile.WriteBytes(ContentLoader.GetTMDHeader(), INANDContentLoader::TMD_HEADER_SIZE);
|
|
|
|
|
|
|
|
for (u32 i = 0; i < ContentLoader.GetContentSize(); i++)
|
|
|
|
{
|
|
|
|
SNANDContent Content = ContentLoader.GetContent()[i];
|
|
|
|
|
|
|
|
pTMDFile.WriteBytes(Content.m_Header, INANDContentLoader::CONTENT_HEADER_SIZE);
|
|
|
|
|
|
|
|
char APPFileName[1024];
|
|
|
|
if (Content.m_Type & 0x8000) //shared
|
|
|
|
{
|
|
|
|
sprintf(APPFileName, "%s",
|
|
|
|
CSharedContent::AccessInstance().AddSharedContent(Content.m_SHA1Hash).c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf(APPFileName, "%s%08x.app", ContentPath.c_str(), Content.m_ContentID);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!File::Exists(APPFileName))
|
|
|
|
{
|
|
|
|
File::CreateFullPath(APPFileName);
|
|
|
|
File::IOFile pAPPFile(APPFileName, "wb");
|
|
|
|
if (!pAPPFile)
|
|
|
|
{
|
|
|
|
PanicAlertT("WAD installation failed: error creating %s", APPFileName);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pAPPFile.WriteBytes(Content.m_pData, Content.m_Size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
INFO_LOG(DISCIO, "Content %s already exists.", APPFileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pTMDFile.Close();
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-21 19:20:22 -06:00
|
|
|
|
|
|
|
//Extract and copy WAD's ticket to ticket directory
|
|
|
|
if (!Add_Ticket(TitleID, ContentLoader.GetTIK(), ContentLoader.GetTIKSize()))
|
2011-05-08 23:47:29 -06:00
|
|
|
{
|
2011-08-21 19:20:22 -06:00
|
|
|
PanicAlertT("WAD installation failed: error creating ticket");
|
|
|
|
return 0;
|
2011-05-08 23:47:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
cUIDsys::AccessInstance().AddTitle(TitleID);
|
|
|
|
|
|
|
|
|
|
|
|
return TitleID;
|
|
|
|
}
|
|
|
|
|
2011-08-21 19:20:22 -06:00
|
|
|
bool Add_Ticket(u64 TitleID, const u8 *p_tik, u32 tikSize)
|
|
|
|
{
|
|
|
|
std::string TicketFileName = Common::GetTicketFileName(TitleID);
|
|
|
|
File::CreateFullPath(TicketFileName);
|
|
|
|
File::IOFile pTicketFile(TicketFileName, "wb");
|
|
|
|
if (!pTicketFile || !p_tik)
|
|
|
|
{
|
|
|
|
//PanicAlertT("WAD installation failed: error creating %s", TicketFileName.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return pTicketFile.WriteBytes(p_tik, tikSize);
|
|
|
|
}
|
2011-05-08 23:47:29 -06:00
|
|
|
|
2009-03-07 01:35:01 -07:00
|
|
|
} // namespace end
|
|
|
|
|