| 
									
										
										
										
											2009-07-28 21:32:10 +00:00
										 |  |  | // Copyright (C) 2003 Dolphin Project.
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00: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/
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Official SVN repository and contact information can be found at
 | 
					
						
							|  |  |  | // http://code.google.com/p/dolphin-emu/
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "stdafx.h"
 | 
					
						
							|  |  |  | #include "NANDContentLoader.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | #include <algorithm>
 | 
					
						
							|  |  |  | #include <cctype> 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | #include "AES/aes.h"
 | 
					
						
							|  |  |  | #include "MathUtil.h"
 | 
					
						
							|  |  |  | #include "FileUtil.h"
 | 
					
						
							|  |  |  | #include "Log.h"
 | 
					
						
							| 
									
										
										
										
											2009-06-17 02:50:54 +00:00
										 |  |  | #include "WiiWad.h"
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace DiscIO | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-09 17:09:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class CSharedContent | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-03-09 17:09:27 +00:00
										 |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	static CSharedContent& AccessInstance() { return m_Instance; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	std::string GetFilenameFromSHA1(u8* _pHash); | 
					
						
							| 
									
										
										
										
											2009-03-09 17:09:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	CSharedContent(); | 
					
						
							| 
									
										
										
										
											2009-03-09 17:09:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	virtual ~CSharedContent(); | 
					
						
							| 
									
										
										
										
											2009-03-09 17:09:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	struct SElement | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		u8 FileName[8]; | 
					
						
							|  |  |  | 		u8 SHA1Hash[20]; | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2009-03-09 17:09:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	std::vector<SElement> m_Elements; | 
					
						
							|  |  |  | 	static CSharedContent m_Instance; | 
					
						
							| 
									
										
										
										
											2009-03-09 17:09:27 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CSharedContent CSharedContent::m_Instance; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CSharedContent::CSharedContent() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	char szFilename[1024]; | 
					
						
							| 
									
										
										
										
											2010-02-02 21:56:29 +00:00
										 |  |  | 	sprintf(szFilename, "%sshared1/content.map", File::GetUserPath(D_WIIUSER_IDX)); | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	if (File::Exists(szFilename)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		FILE* pFile = fopen(szFilename, "rb"); | 
					
						
							|  |  |  | 		while(!feof(pFile)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			SElement Element; | 
					
						
							|  |  |  | 			if (fread(&Element, sizeof(SElement), 1, pFile) == 1) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				m_Elements.push_back(Element); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-08-01 17:51:17 +00:00
										 |  |  | 		fclose(pFile); | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-03-09 17:09:27 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CSharedContent::~CSharedContent() | 
					
						
							|  |  |  | {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string CSharedContent::GetFilenameFromSHA1(u8* _pHash) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	for (size_t i=0; i<m_Elements.size(); i++) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (memcmp(_pHash, m_Elements[i].SHA1Hash, 20) == 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			char szFilename[1024]; | 
					
						
							| 
									
										
										
										
											2010-02-02 21:56:29 +00:00
										 |  |  | 			sprintf(szFilename,  "%sshared1/%c%c%c%c%c%c%c%c.app", File::GetUserPath(D_WIIUSER_IDX), | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00: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 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); | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	virtual ~CNANDContentLoader(); | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	bool IsValid() const	{ return m_Valid; } | 
					
						
							|  |  |  | 	u64 GetTitleID() const  { return m_TitleID; } | 
					
						
							| 
									
										
										
										
											2009-06-08 18:07:45 +00:00
										 |  |  | 	u16 GetIosVersion() const { return m_IosVersion; } | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	u32 GetBootIndex() const  { return m_BootIndex; } | 
					
						
							|  |  |  | 	size_t GetContentSize() const { return m_Content.size(); } | 
					
						
							|  |  |  | 	const SNANDContent* GetContentByIndex(int _Index) const; | 
					
						
							|  |  |  | 	const u8* GetTicketView() const { return m_TicketView; } | 
					
						
							|  |  |  | 	const u8* GetTmdHeader() const { return m_TmdHeader; } | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	const std::vector<SNANDContent>& GetContent() const { return m_Content; } | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	const u16 GetTitleVersion() const {return m_TileVersion;} | 
					
						
							|  |  |  | 	const u16 GetNumEntries() const {return m_numEntries;} | 
					
						
							|  |  |  | 	const DiscIO::IVolume::ECountry GetCountry() const; | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	bool m_Valid; | 
					
						
							|  |  |  | 	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; | 
					
						
							|  |  |  | 	u16 m_TileVersion; | 
					
						
							|  |  |  | 	u8 m_TicketView[TICKET_VIEW_SIZE]; | 
					
						
							| 
									
										
										
										
											2009-06-15 22:27:46 +00:00
										 |  |  | 	u8 m_TmdHeader[TMD_HEADER_SIZE]; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	bool CreateFromDirectory(const std::string& _rPath); | 
					
						
							|  |  |  | 	bool CreateFromWAD(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-07-03 22:34:51 +00:00
										 |  |  | 	bool ParseTMD(u8* pDataApp, u32 pDataAppSize, u8* pTicket, u8* pTMD); | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | 	, 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) | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	if (File::IsDirectory(_rName.c_str())) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		m_Valid = CreateFromDirectory(_rName); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if (File::Exists(_rName.c_str())) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		m_Valid = CreateFromWAD(_rName); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-11-23 08:05:54 +00:00
										 |  |  | //		_dbg_assert_msg_(BOOT, 0, "CNANDContentLoader loads neither folder nor file");
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CNANDContentLoader::~CNANDContentLoader() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	for (size_t i=0; i<m_Content.size(); i++) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		delete [] m_Content[i].m_pData; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	m_Content.clear(); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00: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 08:35:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool CNANDContentLoader::CreateFromWAD(const std::string& _rName) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-06-17 02:50:54 +00:00
										 |  |  | 	WiiWAD Wad(_rName); | 
					
						
							|  |  |  | 	if (!Wad.IsValid()) | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 02:50:54 +00:00
										 |  |  | 	return ParseTMD(Wad.GetDataApp(), Wad.GetDataAppSize(), Wad.GetTicket(), Wad.GetTMD()); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool CNANDContentLoader::CreateFromDirectory(const std::string& _rPath) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	std::string TMDFileName(_rPath); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 	TMDFileName += "/title.tmd"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	FILE* pTMDFile = fopen(TMDFileName.c_str(), "rb"); | 
					
						
							|  |  |  | 	if (pTMDFile == NULL) { | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 		ERROR_LOG(DISCIO, "CreateFromDirectory: error opening %s",  | 
					
						
							|  |  |  | 				  TMDFileName.c_str()); | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	u64 Size = File::GetSize(TMDFileName.c_str()); | 
					
						
							|  |  |  | 	u8* pTMD = new u8[(u32)Size]; | 
					
						
							|  |  |  | 	fread(pTMD, (size_t)Size, 1, pTMDFile); | 
					
						
							|  |  |  | 	fclose(pTMDFile); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	memcpy(m_TicketView, pTMD + 0x180, TICKET_VIEW_SIZE); | 
					
						
							| 
									
										
										
										
											2009-06-15 22:27:46 +00:00
										 |  |  | 	memcpy(m_TmdHeader, pTMD, TMD_HEADER_SIZE); | 
					
						
							| 
									
										
										
										
											2009-03-09 23:21:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-02 21:00:45 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	m_TileVersion = Common::swap16(pTMD + 0x01dc); | 
					
						
							|  |  |  | 	m_numEntries = Common::swap16(pTMD + 0x01de); | 
					
						
							|  |  |  | 	m_BootIndex = Common::swap16(pTMD + 0x01e0); | 
					
						
							|  |  |  | 	m_TitleID = Common::swap64(pTMD + 0x018c); | 
					
						
							|  |  |  | 	m_IosVersion = Common::swap16(pTMD + 0x018a); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m_Content.resize(m_numEntries); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (u32 i = 0; i < m_numEntries; i++)  | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		SNANDContent& rContent = m_Content[i]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		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); | 
					
						
							|  |  |  | 		rContent.m_Size = (u32)Common::swap64(pTMD + 0x01ec + 0x24*i); | 
					
						
							|  |  |  | 		memcpy(rContent.m_SHA1Hash, pTMD + 0x01f4 + 0x24*i, 20); | 
					
						
							|  |  |  | 		memcpy(rContent.m_Header, pTMD + 0x01e4 + 0x24*i, 36); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		rContent.m_pData = NULL;		  | 
					
						
							|  |  |  | 		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 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			sprintf(szFilename, "%s/%08x.app", _rPath.c_str(), rContent.m_ContentID); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		INFO_LOG(DISCIO, "NANDContentLoader: load %s", szFilename); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		FILE* pFile = fopen(szFilename, "rb"); | 
					
						
							|  |  |  | 		if (pFile != NULL) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-08-03 22:51:13 +00:00
										 |  |  | 			u64 ContentSize = File::GetSize(szFilename); | 
					
						
							|  |  |  | 			rContent.m_pData = new u8[(u32)ContentSize]; | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-03 22:51:13 +00:00
										 |  |  | 			_dbg_assert_msg_(BOOT, rContent.m_Size==ContentSize, "TMDLoader: Filesize doesnt fit (%s %i)... prolly you have a bad dump", szFilename, i); | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-03 22:51:13 +00:00
										 |  |  | 			fread(rContent.m_pData, (size_t)ContentSize, 1, pFile); | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 			fclose(pFile); | 
					
						
							|  |  |  | 		}  | 
					
						
							|  |  |  | 		else  | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-08-31 22:42:10 +00:00
										 |  |  | 			ERROR_LOG(DISCIO, "NANDContentLoader: error opening %s", szFilename); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	AES_KEY AESKey; | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	AES_set_decrypt_key(_pKey, 128, &AESKey); | 
					
						
							|  |  |  | 	AES_cbc_encrypt(_pSrc, _pDest, _Size, &AESKey, _IV, AES_DECRYPT); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00: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 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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool CNANDContentLoader::ParseTMD(u8* pDataApp, u32 pDataAppSize, u8* pTicket, u8* pTMD) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	u8 DecryptTitleKey[16]; | 
					
						
							|  |  |  | 	u8 IV[16]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	GetKeyFromTicket(pTicket, DecryptTitleKey); | 
					
						
							| 
									
										
										
										
											2009-06-15 22:27:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	memcpy(m_TicketView, pTMD + 0x180, TICKET_VIEW_SIZE); | 
					
						
							| 
									
										
										
										
											2009-06-15 22:27:46 +00:00
										 |  |  | 	memcpy(m_TmdHeader, pTMD, TMD_HEADER_SIZE); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	m_TileVersion = Common::swap16(pTMD + 0x01dc); | 
					
						
							|  |  |  | 	m_numEntries = Common::swap16(pTMD + 0x01de); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 	m_BootIndex = Common::swap16(pTMD + 0x01e0); | 
					
						
							| 
									
										
										
										
											2009-06-08 18:07:45 +00:00
										 |  |  | 	m_TitleID = Common::swap64(pTMD + 0x018c); | 
					
						
							|  |  |  | 	m_IosVersion = Common::swap16(pTMD + 0x018a); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	u8* p = pDataApp; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-15 22:27:46 +00:00
										 |  |  | 	m_Content.resize(m_numEntries); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-15 22:27:46 +00:00
										 |  |  | 	for (u32 i=0; i<m_numEntries; i++)  | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		SNANDContent& rContent = m_Content[i]; | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 		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); | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 		rContent.m_Size= (u32)Common::swap64(pTMD + 0x01ec + 0x24*i); | 
					
						
							|  |  |  | 		memcpy(rContent.m_SHA1Hash, pTMD + 0x01f4 + 0x24*i, 20); | 
					
						
							|  |  |  | 		memcpy(rContent.m_Header, pTMD + 0x01e4 + 0x24*i, 36); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 		u32 RoundedSize = ROUND_UP(rContent.m_Size, 0x40); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 		rContent.m_pData = new u8[RoundedSize]; | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		memset(IV, 0, sizeof IV); | 
					
						
							|  |  |  | 		memcpy(IV, pTMD + 0x01e8 + 0x24*i, 2); | 
					
						
							|  |  |  | 		AESDecode(DecryptTitleKey, IV, p, RoundedSize, rContent.m_pData); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		p += RoundedSize; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-14 19:13:21 +00:00
										 |  |  | const DiscIO::IVolume::ECountry CNANDContentLoader::GetCountry() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	if (!IsValid()) | 
					
						
							|  |  |  | 		return DiscIO::IVolume::COUNTRY_UNKNOWN; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	u64 TitleID = GetTitleID(); | 
					
						
							|  |  |  | 	char* pTitleID = (char*)&TitleID; | 
					
						
							| 
									
										
										
										
											2009-03-14 19:13:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	return CountrySwitch((u8)pTitleID[0]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CNANDContentManager CNANDContentManager::m_Instance; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CNANDContentManager::~CNANDContentManager() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-03 22:34:51 +00:00
										 |  |  | 	CNANDContentMap::iterator itr = m_Map.begin(); | 
					
						
							|  |  |  | 	while (itr != m_Map.end()) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		delete itr->second; | 
					
						
							|  |  |  | 		itr++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	m_Map.clear(); | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const INANDContentLoader& CNANDContentManager::GetNANDLoader(const std::string& _rName) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-25 02:03:45 +00:00
										 |  |  | 	CNANDContentMap::iterator lb = m_Map.lower_bound(_rName); | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-25 02:03:45 +00:00
										 |  |  | 	if(lb == m_Map.end() || (m_Map.key_comp()(_rName, lb->first))) | 
					
						
							|  |  |  | 		m_Map.insert(lb, CNANDContentMap::value_type(_rName, new CNANDContentLoader(_rName))); | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-25 02:03:45 +00:00
										 |  |  | 	return *m_Map[_rName]; | 
					
						
							| 
									
										
										
										
											2009-03-13 16:15:15 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | } // namespace end
 | 
					
						
							|  |  |  | 
 |