| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Copyright 2013 Dolphin Emulator Project
 | 
					
						
							|  |  |  | // Licensed under GPLv2
 | 
					
						
							|  |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-21 01:47:53 +01:00
										 |  |  | #include <cstddef>
 | 
					
						
							|  |  |  | #include <cstdio>
 | 
					
						
							|  |  |  | #include <cstring>
 | 
					
						
							|  |  |  | #include <functional>
 | 
					
						
							|  |  |  | #include <map>
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <utility>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2013-10-27 18:27:07 +00:00
										 |  |  | #include <polarssl/aes.h>
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-21 01:47:53 +01:00
										 |  |  | #include "Common/Common.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Common/FileUtil.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-19 02:02:01 +01:00
										 |  |  | #include "Common/MathUtil.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-21 01:47:53 +01:00
										 |  |  | #include "Common/NandPaths.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Common/StringUtil.h"
 | 
					
						
							| 
									
										
										
										
											2014-06-05 19:29:54 -04:00
										 |  |  | #include "Common/Logging/Log.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "DiscIO/NANDContentLoader.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-21 01:47:53 +01:00
										 |  |  | #include "DiscIO/Volume.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "DiscIO/WiiWad.h"
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace DiscIO | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-06-05 07:42:40 +00:00
										 |  |  | CSharedContent CSharedContent::m_Instance; | 
					
						
							|  |  |  | cUIDsys cUIDsys::m_Instance; | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-05 06:30:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-05 07:42:40 +00:00
										 |  |  | CSharedContent::CSharedContent() | 
					
						
							| 
									
										
										
										
											2011-06-05 06:30:01 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-06-05 07:42:40 +00:00
										 |  |  | 	UpdateLocation(); | 
					
						
							| 
									
										
										
										
											2011-06-05 06:30:01 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-03-09 17:09:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-05 07:42:40 +00:00
										 |  |  | void CSharedContent::UpdateLocation() | 
					
						
							| 
									
										
										
										
											2009-03-09 17:09:27 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-06-05 07:42:40 +00:00
										 |  |  | 	m_Elements.clear(); | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	m_lastID = 0; | 
					
						
							|  |  |  | 	m_contentMap = StringFromFormat("%sshared1/content.map", File::GetUserPath(D_WIIUSER_IDX).c_str()); | 
					
						
							| 
									
										
										
										
											2010-05-28 05:30:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	File::IOFile pFile(m_contentMap, "rb"); | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 	SElement Element; | 
					
						
							|  |  |  | 	while (pFile.ReadArray(&Element, 1)) | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 		m_Elements.push_back(Element); | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 		m_lastID++; | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-03-09 17:09:27 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CSharedContent::~CSharedContent() | 
					
						
							| 
									
										
										
										
											2011-06-05 07:42:40 +00:00
										 |  |  | {} | 
					
						
							| 
									
										
										
										
											2009-03-09 17:09:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 18:25:12 -04:00
										 |  |  | std::string CSharedContent::GetFilenameFromSHA1(const u8* _pHash) | 
					
						
							| 
									
										
										
										
											2009-03-09 17:09:27 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 	for (auto& Element : m_Elements) | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 		if (memcmp(_pHash, Element.SHA1Hash, 20) == 0) | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2014-06-03 01:08:54 -04:00
										 |  |  | 			return StringFromFormat("%sshared1/%c%c%c%c%c%c%c%c.app", File::GetUserPath(D_WIIUSER_IDX).c_str(), | 
					
						
							|  |  |  | 			    Element.FileName[0], Element.FileName[1], Element.FileName[2], Element.FileName[3], | 
					
						
							|  |  |  | 			    Element.FileName[4], Element.FileName[5], Element.FileName[6], Element.FileName[7]); | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return "unk"; | 
					
						
							| 
									
										
										
										
											2009-03-09 17:09:27 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 18:25:12 -04:00
										 |  |  | std::string CSharedContent::AddSharedContent(const u8* _pHash) | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-06-03 01:08:54 -04:00
										 |  |  | 	std::string filename = GetFilenameFromSHA1(_pHash); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (strcasecmp(filename.c_str(), "unk") == 0) | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 		std::string id = StringFromFormat("%08x", m_lastID); | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | 		SElement Element; | 
					
						
							| 
									
										
										
										
											2014-06-03 01:08:54 -04:00
										 |  |  | 		memcpy(Element.FileName, id.c_str(), 8); | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | 		memcpy(Element.SHA1Hash, _pHash, 20); | 
					
						
							|  |  |  | 		m_Elements.push_back(Element); | 
					
						
							| 
									
										
										
										
											2010-12-15 14:47:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 		File::CreateFullPath(m_contentMap); | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 		File::IOFile pFile(m_contentMap, "ab"); | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 		pFile.WriteArray(&Element, 1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-03 01:08:54 -04:00
										 |  |  | 		filename = StringFromFormat("%sshared1/%s.app", File::GetUserPath(D_WIIUSER_IDX).c_str(), id.c_str()); | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 		m_lastID++; | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-06-03 01:08:54 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return filename; | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-03-09 17:09:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | // this classes must be created by the CNANDContentManager
 | 
					
						
							|  |  |  | class CNANDContentLoader : public INANDContentLoader | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	CNANDContentLoader(const std::string& _rName); | 
					
						
							|  |  |  | 	virtual ~CNANDContentLoader(); | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-16 23:51:41 -05:00
										 |  |  | 	bool IsValid() const override { return m_Valid; } | 
					
						
							| 
									
										
										
										
											2014-08-30 12:41:21 -04:00
										 |  |  | 	void RemoveTitle() const override; | 
					
						
							| 
									
										
										
										
											2013-10-29 01:34:26 -04:00
										 |  |  | 	u64 GetTitleID() const override  { return m_TitleID; } | 
					
						
							|  |  |  | 	u16 GetIosVersion() const override { return m_IosVersion; } | 
					
						
							|  |  |  | 	u32 GetBootIndex() const override  { return m_BootIndex; } | 
					
						
							|  |  |  | 	size_t GetContentSize() const override { return m_Content.size(); } | 
					
						
							|  |  |  | 	const SNANDContent* GetContentByIndex(int _Index) const override; | 
					
						
							|  |  |  | 	const u8* GetTMDView() const override { return m_TMDView; } | 
					
						
							|  |  |  | 	const u8* GetTMDHeader() const override { return m_TMDHeader; } | 
					
						
							|  |  |  | 	u32 GetTIKSize() const override { return m_TIKSize; } | 
					
						
							|  |  |  | 	const u8* GetTIK() const override { return m_TIK; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const std::vector<SNANDContent>& GetContent() const override { return m_Content; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	u16 GetTitleVersion() const override {return m_TitleVersion;} | 
					
						
							|  |  |  | 	u16 GetNumEntries() const override {return m_numEntries;} | 
					
						
							|  |  |  | 	DiscIO::IVolume::ECountry GetCountry() const override; | 
					
						
							|  |  |  | 	u8 GetCountryChar() const override {return m_Country; } | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	bool m_Valid; | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	bool m_isWAD; | 
					
						
							|  |  |  | 	std::string m_Path; | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	u64 m_TitleID; | 
					
						
							| 
									
										
										
										
											2009-06-08 18:07:45 +00:00
										 |  |  | 	u16 m_IosVersion; | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	u32 m_BootIndex; | 
					
						
							|  |  |  | 	u16 m_numEntries; | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00: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 07:36:26 +00:00
										 |  |  | 	u8 m_Country; | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	std::vector<SNANDContent> m_Content; | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-15 22:27:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	bool Initialize(const std::string& _rName); | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	void AESDecode(u8* _pKey, u8* _IV, u8* _pSrc, u32 _Size, u8* _pDest); | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	void GetKeyFromTicket(u8* pTicket, u8* pTicketKey); | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | CNANDContentLoader::CNANDContentLoader(const std::string& _rName) | 
					
						
							| 
									
										
										
										
											2009-09-01 18:34:26 +00:00
										 |  |  | 	: m_Valid(false) | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	, m_isWAD(false) | 
					
						
							| 
									
										
										
										
											2009-09-01 18:34:26 +00:00
										 |  |  | 	, m_TitleID(-1) | 
					
						
							| 
									
										
										
										
											2009-06-08 18:07:45 +00:00
										 |  |  | 	, m_IosVersion(0x09) | 
					
						
							| 
									
										
										
										
											2009-09-01 18:34:26 +00:00
										 |  |  | 	, m_BootIndex(-1) | 
					
						
							| 
									
										
										
										
											2011-05-16 22:46:35 +00:00
										 |  |  | 	, m_TIKSize(0) | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	, m_TIK(nullptr) | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	m_Valid = Initialize(_rName); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CNANDContentLoader::~CNANDContentLoader() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 	for (auto& content : m_Content) | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 		delete [] content.m_pData; | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	m_Content.clear(); | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	if (m_TIK) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		delete []m_TIK; | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 		m_TIK = nullptr; | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | const SNANDContent* CNANDContentLoader::GetContentByIndex(int _Index) const | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 	for (auto& Content : m_Content) | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 		if (Content.m_Index == _Index) | 
					
						
							| 
									
										
										
										
											2013-08-29 18:25:12 -04:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 			return &Content; | 
					
						
							| 
									
										
										
										
											2013-08-29 18:25:12 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	return nullptr; | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | bool CNANDContentLoader::Initialize(const std::string& _rName) | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-08-21 21:09:18 -07:00
										 |  |  | 	if (_rName.empty()) | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	m_Path = _rName; | 
					
						
							| 
									
										
										
										
											2009-06-17 02:50:54 +00:00
										 |  |  | 	WiiWAD Wad(_rName); | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	u8* pDataApp = nullptr; | 
					
						
							|  |  |  | 	u8* pTMD = nullptr; | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	u8 DecryptTitleKey[16]; | 
					
						
							|  |  |  | 	u8 IV[16]; | 
					
						
							|  |  |  | 	if (Wad.IsValid()) | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00: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 08:35:01 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		std::string TMDFileName(m_Path); | 
					
						
							| 
									
										
										
										
											2011-07-18 01:47:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-17 01:57:25 -06:00
										 |  |  | 		if ('/' == *TMDFileName.rbegin()) | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 			TMDFileName += "title.tmd"; | 
					
						
							|  |  |  | 		else | 
					
						
							| 
									
										
										
										
											2011-06-02 07:04:26 +00:00
										 |  |  | 			m_Path = TMDFileName.substr(0, TMDFileName.find("title.tmd")); | 
					
						
							| 
									
										
										
										
											2011-07-18 01:47:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 		File::IOFile pTMDFile(TMDFileName, "rb"); | 
					
						
							|  |  |  | 		if (!pTMDFile) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2014-06-03 01:08:54 -04:00
										 |  |  | 			WARN_LOG(DISCIO, "CreateFromDirectory: error opening %s", TMDFileName.c_str()); | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		u32 pTMDSize = (u32)File::GetSize(TMDFileName); | 
					
						
							|  |  |  | 		pTMD = new u8[pTMDSize]; | 
					
						
							|  |  |  | 		pTMDFile.ReadBytes(pTMD, (size_t)pTMDSize); | 
					
						
							|  |  |  | 		pTMDFile.Close(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-08-31 23:39:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	memcpy(m_TMDView, pTMD + 0x180, TMD_VIEW_SIZE); | 
					
						
							|  |  |  | 	memcpy(m_TMDHeader, pTMD, TMD_HEADER_SIZE); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-09 23:21:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	m_TitleVersion = Common::swap16(pTMD + 0x01dc); | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00: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 07:36:26 +00:00
										 |  |  | 	m_Country = *(u8*)&m_TitleID; | 
					
						
							|  |  |  | 	if (m_Country == 2) // SYSMENU
 | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 		m_Country = GetSysMenuRegion(m_TitleVersion); | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	m_Content.resize(m_numEntries); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 	for (u32 i=0; i<m_numEntries; i++) | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		SNANDContent& rContent = m_Content[i]; | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00: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-09 05:47:29 +00:00
										 |  |  | 		rContent.m_Size= (u32)Common::swap64(pTMD + 0x01ec + 0x24*i); | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 		memcpy(rContent.m_SHA1Hash, pTMD + 0x01f4 + 0x24*i, 20); | 
					
						
							|  |  |  | 		memcpy(rContent.m_Header, pTMD + 0x01e4 + 0x24*i, 36); | 
					
						
							| 
									
										
										
										
											2013-04-16 23:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 		if (m_isWAD) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2013-04-16 23:14:36 -04:00
										 |  |  | 			u32 RoundedSize = ROUND_UP(rContent.m_Size, 0x40); | 
					
						
							|  |  |  | 			rContent.m_pData = new u8[RoundedSize]; | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-16 23:14:36 -04:00
										 |  |  | 			memset(IV, 0, sizeof IV); | 
					
						
							|  |  |  | 			memcpy(IV, pTMD + 0x01e8 + 0x24*i, 2); | 
					
						
							|  |  |  | 			AESDecode(DecryptTitleKey, IV, pDataApp, RoundedSize, rContent.m_pData); | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-16 23:14:36 -04:00
										 |  |  | 			pDataApp += RoundedSize; | 
					
						
							|  |  |  | 			continue; | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 		rContent.m_pData = nullptr; | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (rContent.m_Type & 0x8000)  // shared app
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2013-08-29 18:25:12 -04:00
										 |  |  | 			rContent.m_Filename = CSharedContent::AccessInstance().GetFilenameFromSHA1(rContent.m_SHA1Hash); | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							| 
									
										
										
										
											2013-04-16 23:14:36 -04:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2013-08-29 18:25:12 -04:00
										 |  |  | 			rContent.m_Filename = StringFromFormat("%s/%08x.app", m_Path.c_str(), rContent.m_ContentID); | 
					
						
							| 
									
										
										
										
											2013-04-16 23:14:36 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 18:25:12 -04:00
										 |  |  | 		// Be graceful about incorrect tmds.
 | 
					
						
							| 
									
										
										
										
											2013-08-31 23:39:26 -04:00
										 |  |  | 		if (File::Exists(rContent.m_Filename)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			rContent.m_Size = (u32) File::GetSize(rContent.m_Filename); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 06:59:37 +00:00
										 |  |  | 	delete [] pTMD; | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | void CNANDContentLoader::AESDecode(u8* _pKey, u8* _IV, u8* _pSrc, u32 _Size, u8* _pDest) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-27 18:27:07 +00:00
										 |  |  | 	aes_context AES_ctx; | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-27 18:27:07 +00:00
										 |  |  | 	aes_setkey_dec(&AES_ctx, _pKey, 128); | 
					
						
							|  |  |  | 	aes_crypt_cbc(&AES_ctx, AES_DECRYPT, _Size, _IV, _pSrc, _pDest); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CNANDContentLoader::GetKeyFromTicket(u8* pTicket, u8* pTicketKey) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 	u8 CommonKey[16] = {0xeb,0xe4,0x2a,0x22,0x5e,0x85,0x93,0xe4,0x48,0xd9,0xc5,0x45,0x73,0x81,0xaa,0xf7}; | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	u8 IV[16]; | 
					
						
							|  |  |  | 	memset(IV, 0, sizeof IV); | 
					
						
							|  |  |  | 	memcpy(IV, pTicket + 0x01dc, 8); | 
					
						
							|  |  |  | 	AESDecode(CommonKey, IV, pTicket + 0x01bf, 16, pTicketKey); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-01 16:14:35 +00:00
										 |  |  | DiscIO::IVolume::ECountry CNANDContentLoader::GetCountry() const | 
					
						
							| 
									
										
										
										
											2009-03-14 19:13:21 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	if (!IsValid()) | 
					
						
							|  |  |  | 		return DiscIO::IVolume::COUNTRY_UNKNOWN; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-16 07:36:26 +00:00
										 |  |  | 	return CountrySwitch(m_Country); | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | CNANDContentManager CNANDContentManager::m_Instance; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CNANDContentManager::~CNANDContentManager() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-03-03 06:25:15 +01:00
										 |  |  | 	for (auto& entry : m_Map) | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-03-03 06:25:15 +01:00
										 |  |  | 		delete entry.second; | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	m_Map.clear(); | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 05:07:44 +00:00
										 |  |  | const INANDContentLoader& CNANDContentManager::GetNANDLoader(const std::string& _rName, bool forceReload) | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-25 02:03:45 +00:00
										 |  |  | 	CNANDContentMap::iterator lb = m_Map.lower_bound(_rName); | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-11 00:30:55 +13:00
										 |  |  | 	if (lb == m_Map.end() || (m_Map.key_comp()(_rName, lb->first))) | 
					
						
							| 
									
										
										
										
											2011-03-05 05:07:44 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-10-25 02:03:45 +00:00
										 |  |  | 		m_Map.insert(lb, CNANDContentMap::value_type(_rName, new CNANDContentLoader(_rName))); | 
					
						
							| 
									
										
										
										
											2011-03-05 05:07:44 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (!lb->second->IsValid() || forceReload) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			delete lb->second; | 
					
						
							|  |  |  | 			lb->second = new CNANDContentLoader(_rName); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-10-25 02:03:45 +00:00
										 |  |  | 	return *m_Map[_rName]; | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 05:07:44 +00:00
										 |  |  | const INANDContentLoader& CNANDContentManager::GetNANDLoader(u64 _titleId, bool forceReload) | 
					
						
							| 
									
										
										
										
											2010-09-06 04:36:58 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	std::string _rName = Common::GetTitleContentPath(_titleId); | 
					
						
							| 
									
										
										
										
											2011-03-05 05:07:44 +00:00
										 |  |  | 	return GetNANDLoader(_rName, forceReload); | 
					
						
							| 
									
										
										
										
											2010-09-06 04:36:58 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-03-05 05:18:21 +00: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); | 
					
						
							| 
									
										
										
										
											2014-03-11 00:30:55 +13:00
										 |  |  | 	if (IsValid()) | 
					
						
							| 
									
										
										
										
											2011-03-05 05:18:21 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		// remove tmd?
 | 
					
						
							|  |  |  | 		for (u32 i = 0; i < m_numEntries; i++) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (!(m_Content[i].m_Type & 0x8000)) // skip shared apps
 | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2014-06-03 01:08:54 -04:00
										 |  |  | 				std::string filename = StringFromFormat("%s%08x.app", Common::GetTitleContentPath(m_TitleID).c_str(), m_Content[i].m_ContentID); | 
					
						
							|  |  |  | 				INFO_LOG(DISCIO, "Delete %s", filename.c_str()); | 
					
						
							|  |  |  | 				File::Delete(filename); | 
					
						
							| 
									
										
										
										
											2011-03-05 05:18:21 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-09-06 04:36:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-05 07:42:40 +00:00
										 |  |  | cUIDsys::cUIDsys() | 
					
						
							| 
									
										
										
										
											2011-06-05 06:30:01 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-06-05 07:42:40 +00:00
										 |  |  | 	UpdateLocation(); | 
					
						
							| 
									
										
										
										
											2011-06-05 06:30:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void cUIDsys::UpdateLocation() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-06-05 07:42:40 +00:00
										 |  |  | 	m_Elements.clear(); | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	m_lastUID = 0x00001000; | 
					
						
							|  |  |  | 	m_uidSys = StringFromFormat("%ssys/uid.sys", File::GetUserPath(D_WIIUSER_IDX).c_str()); | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	File::IOFile pFile(m_uidSys, "rb"); | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 	SElement Element; | 
					
						
							|  |  |  | 	while (pFile.ReadArray(&Element, 1)) | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 		*(u32*)&(Element.UID) = Common::swap32(m_lastUID++); | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 		m_Elements.push_back(Element); | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 	pFile.Close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (m_Elements.empty()) | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2010-09-06 04:36:58 +00:00
										 |  |  | 		*(u64*)&(Element.titleID) = Common::swap64(TITLEID_SYSMENU); | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 		*(u32*)&(Element.UID) = Common::swap32(m_lastUID++); | 
					
						
							| 
									
										
										
										
											2010-05-28 05:30:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 		File::CreateFullPath(m_uidSys); | 
					
						
							|  |  |  | 		pFile.Open(m_uidSys, "wb"); | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 		if (!pFile.WriteArray(&Element, 1)) | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 			ERROR_LOG(DISCIO, "Failed to write to %s", m_uidSys.c_str()); | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | cUIDsys::~cUIDsys() | 
					
						
							| 
									
										
										
										
											2011-06-05 07:42:40 +00:00
										 |  |  | {} | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | u32 cUIDsys::GetUIDFromTitle(u64 _Title) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 	for (auto& Element : m_Elements) | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 		if (Common::swap64(_Title) == *(u64*)&(Element.titleID)) | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 			return Common::swap32(Element.UID); | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | void cUIDsys::AddTitle(u64 _TitleID) | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (GetUIDFromTitle(_TitleID)) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 		INFO_LOG(DISCIO, "Title %08x%08x, already exists in uid.sys", (u32)(_TitleID >> 32), (u32)_TitleID); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-12-15 14:47:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	SElement Element; | 
					
						
							|  |  |  | 	*(u64*)&(Element.titleID) = Common::swap64(_TitleID); | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	*(u32*)&(Element.UID) = Common::swap32(m_lastUID++); | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	m_Elements.push_back(Element); | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	File::CreateFullPath(m_uidSys); | 
					
						
							|  |  |  | 	File::IOFile pFile(m_uidSys, "ab"); | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-31 16:07:11 +13:00
										 |  |  | 	if (!pFile.WriteArray(&Element, 1)) | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 		ERROR_LOG(DISCIO, "fwrite failed"); | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-07 06:06:08 +00:00
										 |  |  | void cUIDsys::GetTitleIDs(std::vector<u64>& _TitleIDs, bool _owned) | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 	for (auto& Element : m_Elements) | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 		if ((_owned && Common::CheckTitleTIK(Common::swap64(Element.titleID)))  || | 
					
						
							|  |  |  | 			(!_owned && Common::CheckTitleTMD(Common::swap64(Element.titleID)))) | 
					
						
							|  |  |  | 			_TitleIDs.push_back(Common::swap64(Element.titleID)); | 
					
						
							| 
									
										
										
										
											2010-05-28 05:30:03 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-05-13 04:50:18 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00: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); | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	for (u32 i = 0; i < ContentLoader.GetContentSize(); i++) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-08-29 18:25:12 -04:00
										 |  |  | 		const SNANDContent& Content = ContentLoader.GetContent()[i]; | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		pTMDFile.WriteBytes(Content.m_Header, INANDContentLoader::CONTENT_HEADER_SIZE); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-03 01:08:54 -04:00
										 |  |  | 		std::string APPFileName; | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 		if (Content.m_Type & 0x8000) //shared
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2014-06-03 01:08:54 -04:00
										 |  |  | 			APPFileName = CSharedContent::AccessInstance().AddSharedContent(Content.m_SHA1Hash); | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2014-06-03 01:08:54 -04:00
										 |  |  | 			APPFileName = StringFromFormat("%s%08x.app", ContentPath.c_str(), Content.m_ContentID); | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!File::Exists(APPFileName)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			File::CreateFullPath(APPFileName); | 
					
						
							|  |  |  | 			File::IOFile pAPPFile(APPFileName, "wb"); | 
					
						
							|  |  |  | 			if (!pAPPFile) | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2014-06-03 01:08:54 -04:00
										 |  |  | 				PanicAlertT("WAD installation failed: error creating %s", APPFileName.c_str()); | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 				return 0; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 			pAPPFile.WriteBytes(Content.m_pData, Content.m_Size); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2014-06-03 01:08:54 -04:00
										 |  |  | 			INFO_LOG(DISCIO, "Content %s already exists.", APPFileName.c_str()); | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-21 18:20:22 -07:00
										 |  |  | 	//Extract and copy WAD's ticket to ticket directory
 | 
					
						
							|  |  |  | 	if (!Add_Ticket(TitleID, ContentLoader.GetTIK(), ContentLoader.GetTIKSize())) | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-08-21 18:20:22 -07:00
										 |  |  | 		PanicAlertT("WAD installation failed: error creating ticket"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cUIDsys::AccessInstance().AddTitle(TitleID); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return TitleID; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-21 18:20:22 -07: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-09 05:47:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | } // namespace end
 | 
					
						
							|  |  |  | 
 |