From 5c46810a368e0739c403f6bb0e8c8c260be2464a Mon Sep 17 00:00:00 2001 From: JosJuice Date: Tue, 21 Apr 2015 11:17:39 +0200 Subject: VolumeWiiCrypted: Store last decrypted offset instead of block number This makes it unnecessary to reset m_last_decrypted_offset when changing partition. --- Source/Core/DiscIO/VolumeWiiCrypted.cpp | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'Source/Core/DiscIO/VolumeWiiCrypted.cpp') diff --git a/Source/Core/DiscIO/VolumeWiiCrypted.cpp b/Source/Core/DiscIO/VolumeWiiCrypted.cpp index e1e0657114..b706c8e6f3 100644 --- a/Source/Core/DiscIO/VolumeWiiCrypted.cpp +++ b/Source/Core/DiscIO/VolumeWiiCrypted.cpp @@ -31,7 +31,7 @@ namespace DiscIO CVolumeWiiCrypted::CVolumeWiiCrypted(std::unique_ptr reader, u64 _VolumeOffset, const unsigned char* _pVolumeKey) : m_pReader(std::move(reader)), m_AES_ctx(std::make_unique()), - m_VolumeOffset(_VolumeOffset), m_dataOffset(0x20000), m_LastDecryptedBlockOffset(-1) + m_VolumeOffset(_VolumeOffset), m_dataOffset(0x20000), m_last_decrypted_block(-1) { _assert_(m_pReader); @@ -41,7 +41,6 @@ CVolumeWiiCrypted::CVolumeWiiCrypted(std::unique_ptr reader, u64 _V bool CVolumeWiiCrypted::ChangePartition(u64 offset) { m_VolumeOffset = offset; - m_LastDecryptedBlockOffset = -1; u8 volume_key[16]; DiscIO::VolumeKeyForPartition(*m_pReader, offset, volume_key); @@ -61,25 +60,25 @@ bool CVolumeWiiCrypted::Read(u64 _ReadOffset, u64 _Length, u8* _pBuffer, bool de std::vector read_buffer(BLOCK_TOTAL_SIZE); while (_Length > 0) { - // Calculate block offset - u64 Block = _ReadOffset / BLOCK_DATA_SIZE; - u64 Offset = _ReadOffset % BLOCK_DATA_SIZE; + // Calculate offsets + u64 block_offset_on_disc = + m_VolumeOffset + m_dataOffset + _ReadOffset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE; + u64 data_offset_in_block = _ReadOffset % BLOCK_DATA_SIZE; - if (m_LastDecryptedBlockOffset != Block) + if (m_last_decrypted_block != block_offset_on_disc) { // Read the current block - if (!m_pReader->Read(m_VolumeOffset + m_dataOffset + Block * BLOCK_TOTAL_SIZE, - BLOCK_TOTAL_SIZE, read_buffer.data())) + if (!m_pReader->Read(block_offset_on_disc, BLOCK_TOTAL_SIZE, read_buffer.data())) return false; // Decrypt the block's data. - // 0x3D0 - 0x3DF in m_pBuffer will be overwritten, + // 0x3D0 - 0x3DF in read_buffer will be overwritten, // but that won't affect anything, because we won't - // use the content of m_pBuffer anymore after this + // use the content of read_buffer anymore after this mbedtls_aes_crypt_cbc(m_AES_ctx.get(), MBEDTLS_AES_DECRYPT, BLOCK_DATA_SIZE, &read_buffer[0x3D0], &read_buffer[BLOCK_HEADER_SIZE], - m_LastDecryptedBlock); - m_LastDecryptedBlockOffset = Block; + m_last_decrypted_block_data); + m_last_decrypted_block = block_offset_on_disc; // The only thing we currently use from the 0x000 - 0x3FF part // of the block is the IV (at 0x3D0), but it also contains SHA-1 @@ -88,9 +87,9 @@ bool CVolumeWiiCrypted::Read(u64 _ReadOffset, u64 _Length, u8* _pBuffer, bool de } // Copy the decrypted data - u64 MaxSizeToCopy = BLOCK_DATA_SIZE - Offset; + u64 MaxSizeToCopy = BLOCK_DATA_SIZE - data_offset_in_block; u64 CopySize = (_Length > MaxSizeToCopy) ? MaxSizeToCopy : _Length; - memcpy(_pBuffer, &m_LastDecryptedBlock[Offset], (size_t)CopySize); + memcpy(_pBuffer, &m_last_decrypted_block_data[data_offset_in_block], (size_t)CopySize); // Update offsets _Length -= CopySize; -- cgit v1.2.3 From 405643b902b5b4ec84822551269dfede780b38c7 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Tue, 21 Apr 2015 11:24:13 +0200 Subject: VolumeWiiCrypted: Use std::min --- Source/Core/DiscIO/VolumeWiiCrypted.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'Source/Core/DiscIO/VolumeWiiCrypted.cpp') diff --git a/Source/Core/DiscIO/VolumeWiiCrypted.cpp b/Source/Core/DiscIO/VolumeWiiCrypted.cpp index b706c8e6f3..d37df8d9c7 100644 --- a/Source/Core/DiscIO/VolumeWiiCrypted.cpp +++ b/Source/Core/DiscIO/VolumeWiiCrypted.cpp @@ -4,6 +4,7 @@ #include "DiscIO/VolumeWiiCrypted.h" +#include #include #include #include @@ -87,14 +88,14 @@ bool CVolumeWiiCrypted::Read(u64 _ReadOffset, u64 _Length, u8* _pBuffer, bool de } // Copy the decrypted data - u64 MaxSizeToCopy = BLOCK_DATA_SIZE - data_offset_in_block; - u64 CopySize = (_Length > MaxSizeToCopy) ? MaxSizeToCopy : _Length; - memcpy(_pBuffer, &m_last_decrypted_block_data[data_offset_in_block], (size_t)CopySize); + u64 copy_size = std::min(_Length, BLOCK_DATA_SIZE - data_offset_in_block); + memcpy(_pBuffer, &m_last_decrypted_block_data[data_offset_in_block], + static_cast(copy_size)); // Update offsets - _Length -= CopySize; - _pBuffer += CopySize; - _ReadOffset += CopySize; + _Length -= copy_size; + _pBuffer += copy_size; + _ReadOffset += copy_size; } return true; -- cgit v1.2.3 From 74d84c5af2c82f5395ce2d67d4f901a4e1825185 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Tue, 21 Apr 2015 11:33:51 +0200 Subject: VolumeWiiCrypted: Turn m_dataOffset into a constant --- Source/Core/DiscIO/VolumeWiiCrypted.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'Source/Core/DiscIO/VolumeWiiCrypted.cpp') diff --git a/Source/Core/DiscIO/VolumeWiiCrypted.cpp b/Source/Core/DiscIO/VolumeWiiCrypted.cpp index d37df8d9c7..a78b3017d5 100644 --- a/Source/Core/DiscIO/VolumeWiiCrypted.cpp +++ b/Source/Core/DiscIO/VolumeWiiCrypted.cpp @@ -29,10 +29,12 @@ namespace DiscIO { +constexpr u64 PARTITION_DATA_OFFSET = 0x20000; + CVolumeWiiCrypted::CVolumeWiiCrypted(std::unique_ptr reader, u64 _VolumeOffset, const unsigned char* _pVolumeKey) : m_pReader(std::move(reader)), m_AES_ctx(std::make_unique()), - m_VolumeOffset(_VolumeOffset), m_dataOffset(0x20000), m_last_decrypted_block(-1) + m_VolumeOffset(_VolumeOffset), m_last_decrypted_block(-1) { _assert_(m_pReader); @@ -63,7 +65,7 @@ bool CVolumeWiiCrypted::Read(u64 _ReadOffset, u64 _Length, u8* _pBuffer, bool de { // Calculate offsets u64 block_offset_on_disc = - m_VolumeOffset + m_dataOffset + _ReadOffset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE; + _ReadOffset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE + m_VolumeOffset + PARTITION_DATA_OFFSET; u64 data_offset_in_block = _ReadOffset % BLOCK_DATA_SIZE; if (m_last_decrypted_block != block_offset_on_disc) @@ -143,7 +145,7 @@ IOS::ES::TMDReader CVolumeWiiCrypted::GetTMD() const u64 CVolumeWiiCrypted::PartitionOffsetToRawOffset(u64 offset) const { - return m_VolumeOffset + m_dataOffset + (offset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE) + + return m_VolumeOffset + PARTITION_DATA_OFFSET + (offset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE) + (offset % BLOCK_DATA_SIZE); } @@ -286,7 +288,7 @@ bool CVolumeWiiCrypted::CheckIntegrity() const u32 nClusters = (u32)(partDataSize / 0x8000); for (u32 clusterID = 0; clusterID < nClusters; ++clusterID) { - u64 clusterOff = m_VolumeOffset + m_dataOffset + (u64)clusterID * 0x8000; + u64 clusterOff = m_VolumeOffset + PARTITION_DATA_OFFSET + (u64)clusterID * 0x8000; // Read and decrypt the cluster metadata u8 clusterMDCrypted[0x400]; -- cgit v1.2.3 From 19b8f1c10adeefd9413687ab7a7c21b509fe777d Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sat, 13 Jun 2015 12:51:24 +0200 Subject: VolumeWiiCrypted: Replace ChangePartition with a partition parameter By removing mutable state in VolumeWiiCrypted, this change makes partition-related code simpler. It also gets rid of other ugly things, like ISOProperties's "over 9000" loop that creates a list of partitions by trying possible combinations, and DiscScrubber's volume swapping that recreates the entire volume when it needs to change partition. --- Source/Core/DiscIO/VolumeWiiCrypted.cpp | 201 +++++++++++++++++++++++--------- 1 file changed, 146 insertions(+), 55 deletions(-) (limited to 'Source/Core/DiscIO/VolumeWiiCrypted.cpp') diff --git a/Source/Core/DiscIO/VolumeWiiCrypted.cpp b/Source/Core/DiscIO/VolumeWiiCrypted.cpp index a78b3017d5..310d447c43 100644 --- a/Source/Core/DiscIO/VolumeWiiCrypted.cpp +++ b/Source/Core/DiscIO/VolumeWiiCrypted.cpp @@ -31,41 +31,112 @@ namespace DiscIO { constexpr u64 PARTITION_DATA_OFFSET = 0x20000; -CVolumeWiiCrypted::CVolumeWiiCrypted(std::unique_ptr reader, u64 _VolumeOffset, - const unsigned char* _pVolumeKey) - : m_pReader(std::move(reader)), m_AES_ctx(std::make_unique()), - m_VolumeOffset(_VolumeOffset), m_last_decrypted_block(-1) +CVolumeWiiCrypted::CVolumeWiiCrypted(std::unique_ptr reader) + : m_pReader(std::move(reader)), m_game_partition(PARTITION_NONE), m_last_decrypted_block(-1) { _assert_(m_pReader); - mbedtls_aes_setkey_dec(m_AES_ctx.get(), _pVolumeKey, 128); -} + // Get decryption keys for all partitions + CBlobBigEndianReader big_endian_reader(*m_pReader.get()); + for (u32 partition_group = 0; partition_group < 4; ++partition_group) + { + u32 number_of_partitions; + if (!big_endian_reader.ReadSwapped(0x40000 + (partition_group * 8), &number_of_partitions)) + continue; -bool CVolumeWiiCrypted::ChangePartition(u64 offset) -{ - m_VolumeOffset = offset; + u32 read_buffer; + if (!big_endian_reader.ReadSwapped(0x40000 + (partition_group * 8) + 4, &read_buffer)) + continue; + const u64 partition_table_offset = (u64)read_buffer << 2; - u8 volume_key[16]; - DiscIO::VolumeKeyForPartition(*m_pReader, offset, volume_key); - mbedtls_aes_setkey_dec(m_AES_ctx.get(), volume_key, 128); - return true; + for (u32 i = 0; i < number_of_partitions; i++) + { + if (!big_endian_reader.ReadSwapped(partition_table_offset + (i * 8), &read_buffer)) + continue; + const u64 partition_offset = (u64)read_buffer << 2; + + if (m_game_partition == PARTITION_NONE) + { + u32 partition_type; + if (!big_endian_reader.ReadSwapped(partition_table_offset + (i * 8) + 4, &partition_type)) + continue; + + if (partition_type == 0) + m_game_partition = Partition(partition_offset); + } + + u8 sub_key[16]; + if (!m_pReader->Read(partition_offset + 0x1bf, 16, sub_key)) + continue; + + u8 iv[16]; + memset(iv, 0, 16); + if (!m_pReader->Read(partition_offset + 0x44c, 8, iv)) + continue; + + static const u8 common_key_standard[16] = {0xeb, 0xe4, 0x2a, 0x22, 0x5e, 0x85, 0x93, 0xe4, + 0x48, 0xd9, 0xc5, 0x45, 0x73, 0x81, 0xaa, 0xf7}; + static const u8 common_key_korean[16] = {0x63, 0xb8, 0x2b, 0xb4, 0xf4, 0x61, 0x4e, 0x2e, + 0x13, 0xf2, 0xfe, 0xfb, 0xba, 0x4c, 0x9b, 0x7e}; + static const u8 common_key_rvt[16] = {0xa1, 0x60, 0x4a, 0x6a, 0x71, 0x23, 0xb5, 0x29, + 0xae, 0x8b, 0xec, 0x32, 0xc8, 0x16, 0xfc, 0xaa}; + static const char issuer_rvt[] = "Root-CA00000002-XS00000006"; + + const u8* common_key; + + u8 issuer[sizeof(issuer_rvt)]; + if (!m_pReader->Read(partition_offset + 0x140, sizeof(issuer), issuer)) + continue; + + if (!memcmp(issuer, issuer_rvt, sizeof(issuer_rvt))) + { + // RVT issuer. Use the RVT (debug) master key. + common_key = common_key_rvt; + } + else + { + u8 key_number = 0; + if (!big_endian_reader.ReadSwapped(partition_offset + 0x1f1, &key_number)) + continue; + common_key = (key_number == 1) ? common_key_korean : common_key_standard; + } + + mbedtls_aes_context aes_context; + mbedtls_aes_setkey_dec(&aes_context, common_key, 128); + + u8 volume_key[16]; + mbedtls_aes_crypt_cbc(&aes_context, MBEDTLS_AES_DECRYPT, 16, iv, sub_key, volume_key); + + std::unique_ptr partition_AES_context = + std::make_unique(); + mbedtls_aes_setkey_dec(partition_AES_context.get(), volume_key, 128); + m_partitions[Partition(partition_offset)] = std::move(partition_AES_context); + } + } } CVolumeWiiCrypted::~CVolumeWiiCrypted() { } -bool CVolumeWiiCrypted::Read(u64 _ReadOffset, u64 _Length, u8* _pBuffer, bool decrypt) const +bool CVolumeWiiCrypted::Read(u64 _ReadOffset, u64 _Length, u8* _pBuffer, + const Partition& partition) const { - if (!decrypt) + if (partition == PARTITION_NONE) return m_pReader->Read(_ReadOffset, _Length, _pBuffer); + // Get the decryption key for the partition + auto it = m_partitions.find(partition); + if (it == m_partitions.end()) + return false; + mbedtls_aes_context* aes_context = it->second.get(); + std::vector read_buffer(BLOCK_TOTAL_SIZE); while (_Length > 0) { // Calculate offsets u64 block_offset_on_disc = - _ReadOffset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE + m_VolumeOffset + PARTITION_DATA_OFFSET; + partition.offset + PARTITION_DATA_OFFSET + _ReadOffset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE; u64 data_offset_in_block = _ReadOffset % BLOCK_DATA_SIZE; if (m_last_decrypted_block != block_offset_on_disc) @@ -78,9 +149,8 @@ bool CVolumeWiiCrypted::Read(u64 _ReadOffset, u64 _Length, u8* _pBuffer, bool de // 0x3D0 - 0x3DF in read_buffer will be overwritten, // but that won't affect anything, because we won't // use the content of read_buffer anymore after this - mbedtls_aes_crypt_cbc(m_AES_ctx.get(), MBEDTLS_AES_DECRYPT, BLOCK_DATA_SIZE, - &read_buffer[0x3D0], &read_buffer[BLOCK_HEADER_SIZE], - m_last_decrypted_block_data); + mbedtls_aes_crypt_cbc(aes_context, MBEDTLS_AES_DECRYPT, BLOCK_DATA_SIZE, &read_buffer[0x3D0], + &read_buffer[BLOCK_HEADER_SIZE], m_last_decrypted_block_data); m_last_decrypted_block = block_offset_on_disc; // The only thing we currently use from the 0x000 - 0x3FF part @@ -103,26 +173,39 @@ bool CVolumeWiiCrypted::Read(u64 _ReadOffset, u64 _Length, u8* _pBuffer, bool de return true; } -bool CVolumeWiiCrypted::GetTitleID(u64* buffer) const +std::vector CVolumeWiiCrypted::GetPartitions() const { - return ReadSwapped(m_VolumeOffset + 0x1DC, buffer, false); + std::vector partitions; + for (const auto& pair : m_partitions) + partitions.push_back(pair.first); + return partitions; } -IOS::ES::TicketReader CVolumeWiiCrypted::GetTicket() const +Partition CVolumeWiiCrypted::GetGamePartition() const +{ + return m_game_partition; +} + +bool CVolumeWiiCrypted::GetTitleID(u64* buffer, const Partition& partition) const +{ + return ReadSwapped(partition.offset + 0x1DC, buffer, PARTITION_NONE); +} + +IOS::ES::TicketReader CVolumeWiiCrypted::GetTicket(const Partition& partition) const { std::vector buffer(0x2a4); - Read(m_VolumeOffset, buffer.size(), buffer.data(), false); + Read(partition.offset, buffer.size(), buffer.data(), PARTITION_NONE); return IOS::ES::TicketReader{std::move(buffer)}; } -IOS::ES::TMDReader CVolumeWiiCrypted::GetTMD() const +IOS::ES::TMDReader CVolumeWiiCrypted::GetTMD(const Partition& partition) const { u32 tmd_size = 0; u32 tmd_address = 0; - if (!ReadSwapped(m_VolumeOffset + 0x2a4, &tmd_size, false)) + if (!ReadSwapped(partition.offset + 0x2a4, &tmd_size, PARTITION_NONE)) return {}; - if (!ReadSwapped(m_VolumeOffset + 0x2a8, &tmd_address, false)) + if (!ReadSwapped(partition.offset + 0x2a8, &tmd_address, PARTITION_NONE)) return {}; tmd_address <<= 2; @@ -137,23 +220,26 @@ IOS::ES::TMDReader CVolumeWiiCrypted::GetTMD() const } std::vector buffer(tmd_size); - if (!Read(m_VolumeOffset + tmd_address, tmd_size, buffer.data(), false)) + if (!Read(partition.offset + tmd_address, tmd_size, buffer.data(), PARTITION_NONE)) return {}; return IOS::ES::TMDReader{std::move(buffer)}; } -u64 CVolumeWiiCrypted::PartitionOffsetToRawOffset(u64 offset) const +u64 CVolumeWiiCrypted::PartitionOffsetToRawOffset(u64 offset, const Partition& partition) { - return m_VolumeOffset + PARTITION_DATA_OFFSET + (offset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE) + + if (partition == PARTITION_NONE) + return offset; + + return partition.offset + PARTITION_DATA_OFFSET + (offset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE) + (offset % BLOCK_DATA_SIZE); } -std::string CVolumeWiiCrypted::GetGameID() const +std::string CVolumeWiiCrypted::GetGameID(const Partition& partition) const { char ID[6]; - if (!Read(0, 6, (u8*)ID, true)) + if (!Read(0, 6, (u8*)ID, partition)) return std::string(); return DecodeString(ID); @@ -162,16 +248,16 @@ std::string CVolumeWiiCrypted::GetGameID() const Region CVolumeWiiCrypted::GetRegion() const { u32 region_code; - if (!ReadSwapped(0x4E000, ®ion_code, false)) + if (!ReadSwapped(0x4E000, ®ion_code, PARTITION_NONE)) return Region::UNKNOWN_REGION; return static_cast(region_code); } -Country CVolumeWiiCrypted::GetCountry() const +Country CVolumeWiiCrypted::GetCountry(const Partition& partition) const { u8 country_byte; - if (!ReadSwapped(3, &country_byte, true)) + if (!ReadSwapped(3, &country_byte, partition)) return Country::COUNTRY_UNKNOWN; const Region region = GetRegion(); @@ -182,29 +268,29 @@ Country CVolumeWiiCrypted::GetCountry() const return CountrySwitch(country_byte); } -std::string CVolumeWiiCrypted::GetMakerID() const +std::string CVolumeWiiCrypted::GetMakerID(const Partition& partition) const { char makerID[2]; - if (!Read(0x4, 0x2, (u8*)&makerID, true)) + if (!Read(0x4, 0x2, (u8*)&makerID, partition)) return std::string(); return DecodeString(makerID); } -u16 CVolumeWiiCrypted::GetRevision() const +u16 CVolumeWiiCrypted::GetRevision(const Partition& partition) const { u8 revision; - if (!ReadSwapped(7, &revision, true)) + if (!ReadSwapped(7, &revision, partition)) return 0; return revision; } -std::string CVolumeWiiCrypted::GetInternalName() const +std::string CVolumeWiiCrypted::GetInternalName(const Partition& partition) const { char name_buffer[0x60]; - if (Read(0x20, 0x60, (u8*)&name_buffer, true)) + if (Read(0x20, 0x60, (u8*)&name_buffer, partition)) return DecodeString(name_buffer); return ""; @@ -212,7 +298,7 @@ std::string CVolumeWiiCrypted::GetInternalName() const std::map CVolumeWiiCrypted::GetLongNames() const { - std::unique_ptr file_system(CreateFileSystem(this)); + std::unique_ptr file_system(CreateFileSystem(this, GetGamePartition())); std::vector opening_bnr(NAMES_TOTAL_BYTES); size_t size = file_system->ReadFile("opening.bnr", opening_bnr.data(), opening_bnr.size(), 0x5C); opening_bnr.resize(size); @@ -225,27 +311,27 @@ std::vector CVolumeWiiCrypted::GetBanner(int* width, int* height) const *height = 0; u64 title_id; - if (!GetTitleID(&title_id)) + if (!GetTitleID(&title_id, GetGamePartition())) return std::vector(); return GetWiiBanner(width, height, title_id); } -u64 CVolumeWiiCrypted::GetFSTSize() const +u64 CVolumeWiiCrypted::GetFSTSize(const Partition& partition) const { u32 size; - if (!Read(0x428, 0x4, (u8*)&size, true)) + if (!Read(0x428, 0x4, (u8*)&size, partition)) return 0; return (u64)Common::swap32(size) << 2; } -std::string CVolumeWiiCrypted::GetApploaderDate() const +std::string CVolumeWiiCrypted::GetApploaderDate(const Partition& partition) const { char date[16]; - if (!Read(0x2440, 0x10, (u8*)&date, true)) + if (!Read(0x2440, 0x10, (u8*)&date, partition)) return std::string(); return DecodeString(date); @@ -256,10 +342,10 @@ Platform CVolumeWiiCrypted::GetVolumeType() const return Platform::WII_DISC; } -u8 CVolumeWiiCrypted::GetDiscNumber() const +u8 CVolumeWiiCrypted::GetDiscNumber(const Partition& partition) const { u8 disc_number = 0; - ReadSwapped(6, &disc_number, true); + ReadSwapped(6, &disc_number, partition); return disc_number; } @@ -278,29 +364,34 @@ u64 CVolumeWiiCrypted::GetRawSize() const return m_pReader->GetRawSize(); } -bool CVolumeWiiCrypted::CheckIntegrity() const +bool CVolumeWiiCrypted::CheckIntegrity(const Partition& partition) const { + // Get the decryption key for the partition + auto it = m_partitions.find(partition); + if (it == m_partitions.end()) + return false; + mbedtls_aes_context* aes_context = it->second.get(); + // Get partition data size u32 partSizeDiv4; - Read(m_VolumeOffset + 0x2BC, 4, (u8*)&partSizeDiv4, false); + Read(partition.offset + 0x2BC, 4, (u8*)&partSizeDiv4, PARTITION_NONE); u64 partDataSize = (u64)Common::swap32(partSizeDiv4) * 4; u32 nClusters = (u32)(partDataSize / 0x8000); for (u32 clusterID = 0; clusterID < nClusters; ++clusterID) { - u64 clusterOff = m_VolumeOffset + PARTITION_DATA_OFFSET + (u64)clusterID * 0x8000; + u64 clusterOff = partition.offset + PARTITION_DATA_OFFSET + (u64)clusterID * 0x8000; // Read and decrypt the cluster metadata u8 clusterMDCrypted[0x400]; u8 clusterMD[0x400]; u8 IV[16] = {0}; - if (!Read(clusterOff, 0x400, clusterMDCrypted, false)) + if (!Read(clusterOff, 0x400, clusterMDCrypted, PARTITION_NONE)) { WARN_LOG(DISCIO, "Integrity Check: fail at cluster %d: could not read metadata", clusterID); return false; } - mbedtls_aes_crypt_cbc(m_AES_ctx.get(), MBEDTLS_AES_DECRYPT, 0x400, IV, clusterMDCrypted, - clusterMD); + mbedtls_aes_crypt_cbc(aes_context, MBEDTLS_AES_DECRYPT, 0x400, IV, clusterMDCrypted, clusterMD); // Some clusters have invalid data and metadata because they aren't // meant to be read by the game (for example, holes between files). To @@ -319,7 +410,7 @@ bool CVolumeWiiCrypted::CheckIntegrity() const continue; u8 clusterData[0x7C00]; - if (!Read((u64)clusterID * 0x7C00, 0x7C00, clusterData, true)) + if (!Read((u64)clusterID * 0x7C00, 0x7C00, clusterData, partition)) { WARN_LOG(DISCIO, "Integrity Check: fail at cluster %d: could not read data", clusterID); return false; -- cgit v1.2.3 From 2bcad57225b9b0a4635d62eeabbbb138d71386bf Mon Sep 17 00:00:00 2001 From: JosJuice Date: Wed, 3 Jun 2015 13:19:28 +0200 Subject: Check file system validity before reading opening.bnr This happened to work without any problems because the only way for a file system to be invalid was to not have the right GC/Wii magic word in the unencrypted area, and a volume could not be created without having the right GC/Wii magic word there. Now that file systems read the magic word from a partition instead, a fix is needed. --- Source/Core/DiscIO/VolumeWiiCrypted.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'Source/Core/DiscIO/VolumeWiiCrypted.cpp') diff --git a/Source/Core/DiscIO/VolumeWiiCrypted.cpp b/Source/Core/DiscIO/VolumeWiiCrypted.cpp index 310d447c43..05d34aa126 100644 --- a/Source/Core/DiscIO/VolumeWiiCrypted.cpp +++ b/Source/Core/DiscIO/VolumeWiiCrypted.cpp @@ -299,6 +299,9 @@ std::string CVolumeWiiCrypted::GetInternalName(const Partition& partition) const std::map CVolumeWiiCrypted::GetLongNames() const { std::unique_ptr file_system(CreateFileSystem(this, GetGamePartition())); + if (!file_system) + return {{}}; + std::vector opening_bnr(NAMES_TOTAL_BYTES); size_t size = file_system->ReadFile("opening.bnr", opening_bnr.data(), opening_bnr.size(), 0x5C); opening_bnr.resize(size); -- cgit v1.2.3