VolumeCreator.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2008 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <cstddef>
  5. #include <cstring>
  6. #include <string>
  7. #include <vector>
  8. #include <polarssl/aes.h>
  9. #include "Common/CommonTypes.h"
  10. #include "Common/StringUtil.h"
  11. #include "DiscIO/Blob.h"
  12. #include "DiscIO/Volume.h"
  13. #include "DiscIO/VolumeCreator.h"
  14. #include "DiscIO/VolumeDirectory.h"
  15. #include "DiscIO/VolumeGC.h"
  16. #include "DiscIO/VolumeWad.h"
  17. #include "DiscIO/VolumeWiiCrypted.h"
  18. namespace DiscIO
  19. {
  20. enum EDiscType
  21. {
  22. DISC_TYPE_UNK,
  23. DISC_TYPE_WII,
  24. DISC_TYPE_WII_CONTAINER,
  25. DISC_TYPE_GC,
  26. DISC_TYPE_WAD
  27. };
  28. class CBlobBigEndianReader
  29. {
  30. public:
  31. CBlobBigEndianReader(IBlobReader& _rReader) : m_rReader(_rReader) {}
  32. u32 Read32(u64 _Offset)
  33. {
  34. u32 Temp;
  35. m_rReader.Read(_Offset, 4, (u8*)&Temp);
  36. return Common::swap32(Temp);
  37. }
  38. u16 Read16(u64 _Offset)
  39. {
  40. u16 Temp;
  41. m_rReader.Read(_Offset, 2, (u8*)&Temp);
  42. return Common::swap16(Temp);
  43. }
  44. u8 Read8(u64 _Offset)
  45. {
  46. u8 Temp;
  47. m_rReader.Read(_Offset, 1, &Temp);
  48. return Temp;
  49. }
  50. private:
  51. IBlobReader& m_rReader;
  52. };
  53. static const unsigned char s_master_key[16] = {
  54. 0xeb,0xe4,0x2a,0x22,0x5e,0x85,0x93,0xe4,
  55. 0x48,0xd9,0xc5,0x45,0x73,0x81,0xaa,0xf7
  56. };
  57. static const unsigned char s_master_key_korean[16] = {
  58. 0x63,0xb8,0x2b,0xb4,0xf4,0x61,0x4e,0x2e,
  59. 0x13,0xf2,0xfe,0xfb,0xba,0x4c,0x9b,0x7e
  60. };
  61. static IVolume* CreateVolumeFromCryptedWiiImage(IBlobReader& _rReader, u32 _PartitionGroup, u32 _VolumeType, u32 _VolumeNum);
  62. EDiscType GetDiscType(IBlobReader& _rReader);
  63. IVolume* CreateVolumeFromFilename(const std::string& _rFilename, u32 _PartitionGroup, u32 _VolumeNum)
  64. {
  65. IBlobReader* pReader = CreateBlobReader(_rFilename);
  66. if (pReader == nullptr)
  67. return nullptr;
  68. switch (GetDiscType(*pReader))
  69. {
  70. case DISC_TYPE_WII:
  71. case DISC_TYPE_GC:
  72. return new CVolumeGC(pReader);
  73. case DISC_TYPE_WAD:
  74. return new CVolumeWAD(pReader);
  75. case DISC_TYPE_WII_CONTAINER:
  76. {
  77. IVolume* pVolume = CreateVolumeFromCryptedWiiImage(*pReader, _PartitionGroup, 0, _VolumeNum);
  78. if (pVolume == nullptr)
  79. {
  80. delete pReader;
  81. }
  82. return pVolume;
  83. }
  84. case DISC_TYPE_UNK:
  85. default:
  86. std::string Filename, ext;
  87. SplitPath(_rFilename, nullptr, &Filename, &ext);
  88. Filename += ext;
  89. NOTICE_LOG(DISCIO, "%s does not have the Magic word for a gcm, wiidisc or wad file\n"
  90. "Set Log Verbosity to Warning and attempt to load the game again to view the values", Filename.c_str());
  91. delete pReader;
  92. }
  93. return nullptr;
  94. }
  95. IVolume* CreateVolumeFromDirectory(const std::string& _rDirectory, bool _bIsWii, const std::string& _rApploader, const std::string& _rDOL)
  96. {
  97. if (CVolumeDirectory::IsValidDirectory(_rDirectory))
  98. return new CVolumeDirectory(_rDirectory, _bIsWii, _rApploader, _rDOL);
  99. return nullptr;
  100. }
  101. bool IsVolumeWadFile(const IVolume *_rVolume)
  102. {
  103. u32 MagicWord = 0;
  104. _rVolume->Read(0x02, 4, (u8*)&MagicWord, false);
  105. return (Common::swap32(MagicWord) == 0x00204973 || Common::swap32(MagicWord) == 0x00206962);
  106. }
  107. void VolumeKeyForParition(IBlobReader& _rReader, u64 offset, u8* VolumeKey)
  108. {
  109. CBlobBigEndianReader Reader(_rReader);
  110. u8 SubKey[16];
  111. _rReader.Read(offset + 0x1bf, 16, SubKey);
  112. u8 IV[16];
  113. memset(IV, 0, 16);
  114. _rReader.Read(offset + 0x44c, 8, IV);
  115. bool usingKoreanKey = false;
  116. // Issue: 6813
  117. // Magic value is at partition's offset + 0x1f1 (1byte)
  118. // If encrypted with the Korean key, the magic value would be 1
  119. // Otherwise it is zero
  120. if (Reader.Read8(0x3) == 'K' && Reader.Read8(offset + 0x1f1) == 1)
  121. usingKoreanKey = true;
  122. aes_context AES_ctx;
  123. aes_setkey_dec(&AES_ctx, (usingKoreanKey ? s_master_key_korean : s_master_key), 128);
  124. aes_crypt_cbc(&AES_ctx, AES_DECRYPT, 16, IV, SubKey, VolumeKey);
  125. }
  126. static IVolume* CreateVolumeFromCryptedWiiImage(IBlobReader& _rReader, u32 _PartitionGroup, u32 _VolumeType, u32 _VolumeNum)
  127. {
  128. CBlobBigEndianReader Reader(_rReader);
  129. u32 numPartitions = Reader.Read32(0x40000 + (_PartitionGroup * 8));
  130. u64 PartitionsOffset = (u64)Reader.Read32(0x40000 + (_PartitionGroup * 8) + 4) << 2;
  131. // Check if we're looking for a valid partition
  132. if ((int)_VolumeNum != -1 && _VolumeNum > numPartitions)
  133. return nullptr;
  134. struct SPartition
  135. {
  136. u64 Offset;
  137. u32 Type;
  138. };
  139. struct SPartitionGroup
  140. {
  141. u32 numPartitions;
  142. u64 PartitionsOffset;
  143. std::vector<SPartition> PartitionsVec;
  144. };
  145. SPartitionGroup PartitionGroup[4];
  146. // read all partitions
  147. for (SPartitionGroup& group : PartitionGroup)
  148. {
  149. for (u32 i = 0; i < numPartitions; i++)
  150. {
  151. SPartition Partition;
  152. Partition.Offset = ((u64)Reader.Read32(PartitionsOffset + (i * 8) + 0)) << 2;
  153. Partition.Type = Reader.Read32(PartitionsOffset + (i * 8) + 4);
  154. group.PartitionsVec.push_back(Partition);
  155. }
  156. }
  157. // return the partition type specified or number
  158. // types: 0 = game, 1 = firmware update, 2 = channel installer
  159. // some partitions on ssbb use the ascii title id of the demo VC game they hold...
  160. for (size_t i = 0; i < PartitionGroup[_PartitionGroup].PartitionsVec.size(); i++)
  161. {
  162. const SPartition& rPartition = PartitionGroup[_PartitionGroup].PartitionsVec.at(i);
  163. if ((rPartition.Type == _VolumeType && (int)_VolumeNum == -1) || i == _VolumeNum)
  164. {
  165. u8 VolumeKey[16];
  166. VolumeKeyForParition(_rReader, rPartition.Offset, VolumeKey);
  167. return new CVolumeWiiCrypted(&_rReader, rPartition.Offset, VolumeKey);
  168. }
  169. }
  170. return nullptr;
  171. }
  172. EDiscType GetDiscType(IBlobReader& _rReader)
  173. {
  174. CBlobBigEndianReader Reader(_rReader);
  175. u32 WiiMagic = Reader.Read32(0x18);
  176. u32 WiiContainerMagic = Reader.Read32(0x60);
  177. u32 WADMagic = Reader.Read32(0x02);
  178. u32 GCMagic = Reader.Read32(0x1C);
  179. // check for Wii
  180. if (WiiMagic == 0x5D1C9EA3 && WiiContainerMagic != 0)
  181. return DISC_TYPE_WII;
  182. if (WiiMagic == 0x5D1C9EA3 && WiiContainerMagic == 0)
  183. return DISC_TYPE_WII_CONTAINER;
  184. // check for WAD
  185. // 0x206962 for boot2 wads
  186. if (WADMagic == 0x00204973 || WADMagic == 0x00206962)
  187. return DISC_TYPE_WAD;
  188. // check for GC
  189. if (GCMagic == 0xC2339F3D)
  190. return DISC_TYPE_GC;
  191. WARN_LOG(DISCIO, "No known magic words found");
  192. WARN_LOG(DISCIO, "Wii offset: 0x18 value: 0x%08x", WiiMagic);
  193. WARN_LOG(DISCIO, "WiiC offset: 0x60 value: 0x%08x", WiiContainerMagic);
  194. WARN_LOG(DISCIO, "WAD offset: 0x02 value: 0x%08x", WADMagic);
  195. WARN_LOG(DISCIO, "GC offset: 0x1C value: 0x%08x", GCMagic);
  196. return DISC_TYPE_UNK;
  197. }
  198. } // namespace