| 1 | // Copyright 2020 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DiscIO/VolumeDisc.h" |
| 5 | |
| 6 | #include <memory> |
| 7 | #include <optional> |
| 8 | #include <string> |
| 9 | |
| 10 | #include "Common/CommonTypes.h" |
| 11 | #include "Common/Crypto/SHA1.h" |
| 12 | #include "DiscIO/DiscUtils.h" |
| 13 | #include "DiscIO/Enums.h" |
| 14 | #include "DiscIO/Filesystem.h" |
| 15 | #include "DiscIO/VolumeGC.h" |
| 16 | |
| 17 | namespace DiscIO |
| 18 | { |
| 19 | std::string VolumeDisc::GetGameID(const Partition& partition) const |
| 20 | { |
| 21 | char id[6]{}; |
| 22 | |
| 23 | if (GetVolumeType() == Platform::Triforce) |
| 24 | { |
| 25 | // Triforce games have their Game ID stored in the boot.id file |
| 26 | const BootID* boot_id = static_cast<const VolumeGC*>(this)->GetTriforceBootID(); |
| 27 | |
| 28 | // Construct game ID from the BTID |
| 29 | id[0] = 'G'; |
| 30 | |
| 31 | memcpy(id + 1, boot_id->game_id.data() + 2, 2); |
| 32 | |
| 33 | switch (GetCountry()) |
| 34 | { |
| 35 | default: |
| 36 | case Country::Japan: |
| 37 | id[3] = 'J'; |
| 38 | break; |
| 39 | case Country::Taiwan: |
| 40 | id[3] = 'W'; |
| 41 | break; |
| 42 | case Country::USA: |
| 43 | id[3] = 'E'; |
| 44 | break; |
| 45 | case Country::Europe: |
| 46 | id[3] = 'P'; |
| 47 | break; |
| 48 | } |
| 49 | |
| 50 | const std::string maker_id{GetMakerID()}; |
| 51 | memcpy(id + 4, maker_id.c_str(), std::min<std::size_t>(maker_id.size(), 2)); |
| 52 | |
| 53 | return FilterGameID(id); |
| 54 | } |
| 55 | |
| 56 | if (!Read(0, sizeof(id), reinterpret_cast<u8*>(id), partition)) |
| 57 | return std::string(); |
| 58 | |
| 59 | return FilterGameID(id); |
| 60 | } |
| 61 | |
| 62 | Country VolumeDisc::GetCountry(const Partition& partition) const |
| 63 | { |
| 64 | u8 country_byte = 0; |
| 65 | |
| 66 | if (GetVolumeType() == Platform::Triforce) |
| 67 | { |
| 68 | const BootID* boot_id = static_cast<const VolumeGC*>(this)->GetTriforceBootID(); |
| 69 | |
| 70 | switch (boot_id->region_flags & 0x000000FF) |
| 71 | { |
| 72 | default: |
| 73 | case 0x02: // JAPAN |
| 74 | country_byte = 'J'; |
| 75 | break; |
| 76 | case 0x08: // ASIA |
| 77 | country_byte = 'W'; |
| 78 | break; |
| 79 | case 0x0E: // USA |
| 80 | country_byte = 'E'; |
| 81 | break; |
| 82 | case 0x0C: // EXPORT |
| 83 | country_byte = 'P'; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | // The 0 that we use as a default value is mapped to Country::Unknown and Region::Unknown |
| 90 | country_byte = ReadSwapped<u8>(3, partition).value_or(0); |
| 91 | } |
| 92 | |
| 93 | const Region region = GetRegion(); |
| 94 | const std::optional<u16> revision = GetRevision(); |
| 95 | |
| 96 | if (CountryCodeToRegion(country_byte, GetVolumeType(), region, revision) != region) |
| 97 | return TypicalCountryForRegion(region); |
| 98 | |
| 99 | return CountryCodeToCountry(country_byte, GetVolumeType(), region, revision); |
| 100 | } |
| 101 | |
| 102 | Region VolumeDisc::RegionCodeToRegion(std::optional<u32> region_code) const |
| 103 | { |
| 104 | if (!region_code) |
| 105 | return Region::Unknown; |
| 106 | |
| 107 | const Region region = static_cast<Region>(*region_code); |
| 108 | return region <= Region::NTSC_K ? region : Region::Unknown; |
| 109 | } |
| 110 | |
| 111 | std::string VolumeDisc::GetMakerID(const Partition& partition) const |
| 112 | { |
| 113 | char maker_id[2]; |
| 114 | |
| 115 | // Triforce games have their maker stored in the boot.id file as an actual string |
| 116 | if (GetVolumeType() == Platform::Triforce) |
| 117 | { |
| 118 | const BootID* boot_id = static_cast<const VolumeGC*>(this)->GetTriforceBootID(); |
| 119 | |
| 120 | // There only seem to be three different makers here, |
| 121 | // so we can just check the first char to difference between them. |
| 122 | // |
| 123 | // NAMCO CORPORATION, SEGA CORPORATION and Hitmaker co,ltd. |
| 124 | |
| 125 | switch (boot_id->manufacturer[0]) |
| 126 | { |
| 127 | case 'S': // SEGA CORPORATION |
| 128 | case 'H': // Hitmaker co,ltd |
| 129 | return "6E"; |
| 130 | case 'N': // NAMCO CORPORATION |
| 131 | return "82"; |
| 132 | default: |
| 133 | break; |
| 134 | } |
| 135 | // Fall back to normal maker from header |
| 136 | } |
| 137 | |
| 138 | if (!Read(0x4, sizeof(maker_id), reinterpret_cast<u8*>(&maker_id), partition)) |
| 139 | return std::string(); |
| 140 | |
| 141 | return FilterGameID(maker_id); |
| 142 | } |
| 143 | |
| 144 | std::optional<u16> VolumeDisc::GetRevision(const Partition& partition) const |
| 145 | { |
| 146 | std::optional<u8> revision = ReadSwapped<u8>(7, partition); |
| 147 | return revision ? *revision : std::optional<u16>(); |
| 148 | } |
| 149 | |
| 150 | std::string VolumeDisc::GetInternalName(const Partition& partition) const |
| 151 | { |
| 152 | char name[0x60]; |
| 153 | |
| 154 | // Triforce games have their Title stored in the boot.id file |
| 155 | if (GetVolumeType() == Platform::Triforce) |
| 156 | { |
| 157 | const BootID* boot_id = static_cast<const VolumeGC*>(this)->GetTriforceBootID(); |
| 158 | return DecodeString(boot_id->game_name); |
| 159 | } |
| 160 | |
| 161 | if (!Read(0x20, sizeof(name), reinterpret_cast<u8*>(&name), partition)) |
| 162 | return std::string(); |
| 163 | |
| 164 | return DecodeString(name); |
| 165 | } |
| 166 | |
| 167 | std::string VolumeDisc::GetApploaderDate(const Partition& partition) const |
| 168 | { |
| 169 | char date[16]; |
| 170 | |
| 171 | if (!Read(0x2440, sizeof(date), reinterpret_cast<u8*>(&date), partition)) |
| 172 | return std::string(); |
| 173 | |
| 174 | return DecodeString(date); |
| 175 | } |
| 176 | |
| 177 | std::optional<u8> VolumeDisc::GetDiscNumber(const Partition& partition) const |
| 178 | { |
| 179 | return ReadSwapped<u8>(6, partition); |
| 180 | } |
| 181 | |
| 182 | bool VolumeDisc::IsNKit() const |
| 183 | { |
| 184 | constexpr u32 NKIT_MAGIC = 0x4E4B4954; // "NKIT" |
| 185 | return ReadSwapped<u32>(0x200, PARTITION_NONE) == NKIT_MAGIC; |
| 186 | } |
| 187 | |
| 188 | void VolumeDisc::AddGamePartitionToSyncHash(Common::SHA1::Context* context) const |
| 189 | { |
| 190 | const Partition partition = GetGamePartition(); |
| 191 | |
| 192 | // All headers at the beginning of the partition, plus the apploader |
| 193 | ReadAndAddToSyncHash(context, 0, 0x2440 + GetApploaderSize(*this, partition).value_or(0), |
| 194 | partition); |
| 195 | |
| 196 | // Boot DOL (may be missing if this is a Datel disc) |
| 197 | const std::optional<u64> dol_offset = GetBootDOLOffset(*this, partition); |
| 198 | if (dol_offset) |
| 199 | { |
| 200 | ReadAndAddToSyncHash(context, *dol_offset, |
| 201 | GetBootDOLSize(*this, partition, *dol_offset).value_or(0), partition); |
| 202 | } |
| 203 | |
| 204 | // File system |
| 205 | const std::optional<u64> fst_offset = GetFSTOffset(*this, partition); |
| 206 | if (fst_offset) |
| 207 | ReadAndAddToSyncHash(context, *fst_offset, GetFSTSize(*this, partition).value_or(0), partition); |
| 208 | |
| 209 | // opening.bnr (name and banner) |
| 210 | const FileSystem* file_system = GetFileSystem(partition); |
| 211 | if (file_system) |
| 212 | { |
| 213 | std::unique_ptr<FileInfo> file_info = file_system->FindFileInfo("opening.bnr"); |
| 214 | if (file_info && !file_info->IsDirectory()) |
| 215 | ReadAndAddToSyncHash(context, file_info->GetOffset(), file_info->GetSize(), partition); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | } // namespace DiscIO |