From 8a9597e32e98707d5bcdc1a41d8c45e3174d4047 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sat, 4 Apr 2020 20:56:20 +0200 Subject: DiscIO: Allow converting from formats other than ISO and GCZ The constant DESIRED_BUFFER_SIZE was determined by multiplying the old hardcoded value 32 with the default GCZ block size 16 KiB. Not sure if it actually is the best value, but it seems fine. --- Source/Core/DiscIO/DiscScrubber.cpp | 35 ++--------------------------------- 1 file changed, 2 insertions(+), 33 deletions(-) (limited to 'Source/Core/DiscIO/DiscScrubber.cpp') diff --git a/Source/Core/DiscIO/DiscScrubber.cpp b/Source/Core/DiscIO/DiscScrubber.cpp index 8a06d9266e..07d504d72a 100644 --- a/Source/Core/DiscIO/DiscScrubber.cpp +++ b/Source/Core/DiscIO/DiscScrubber.cpp @@ -14,6 +14,7 @@ #include #include "Common/Align.h" +#include "Common/Assert.h" #include "Common/CommonTypes.h" #include "Common/File.h" #include "Common/Logging/Log.h" @@ -29,19 +30,11 @@ constexpr size_t CLUSTER_SIZE = 0x8000; DiscScrubber::DiscScrubber() = default; DiscScrubber::~DiscScrubber() = default; -bool DiscScrubber::SetupScrub(const Volume* disc, int block_size) +bool DiscScrubber::SetupScrub(const Volume* disc) { if (!disc) return false; m_disc = disc; - m_block_size = block_size; - - if (CLUSTER_SIZE % m_block_size != 0) - { - ERROR_LOG(DISCIO, "Block size %u is not a factor of 0x8000, scrubbing not possible", - m_block_size); - return false; - } m_file_size = m_disc->GetSize(); @@ -54,34 +47,10 @@ bool DiscScrubber::SetupScrub(const Volume* disc, int block_size) // Fill out table of free blocks const bool success = ParseDisc(); - m_block_count = 0; - m_is_scrubbing = success; return success; } -size_t DiscScrubber::GetNextBlock(File::IOFile& in, u8* buffer) -{ - const u64 current_offset = m_block_count * m_block_size; - - size_t read_bytes = 0; - if (CanBlockBeScrubbed(current_offset)) - { - DEBUG_LOG(DISCIO, "Freeing 0x%016" PRIx64, current_offset); - std::fill(buffer, buffer + m_block_size, 0x00); - in.Seek(m_block_size, SEEK_CUR); - read_bytes = m_block_size; - } - else - { - DEBUG_LOG(DISCIO, "Used 0x%016" PRIx64, current_offset); - in.ReadArray(buffer, m_block_size, &read_bytes); - } - - m_block_count++; - return read_bytes; -} - bool DiscScrubber::CanBlockBeScrubbed(u64 offset) const { return m_is_scrubbing && m_free_table[offset / CLUSTER_SIZE]; -- cgit v1.2.3 From cefc2a7baa9a4dba878352ebc3a5c92b63ed295a Mon Sep 17 00:00:00 2001 From: JosJuice Date: Fri, 10 Apr 2020 16:21:51 +0200 Subject: DiscIO: Fix edge case where blocks could get scrubbed accidentally If we start 31 KiB into a 32 KiB block and want to mark 2 KiB of data as used, we need to mark 2 blocks as used, not just 1. This problem is avoided when calling MarkAsUsed from MarkAsUsedE, since MarkAsUsedE aligns to 32 KiB on its own. Most calls to MarkAsUsed are from MarkAsUsedE, which is why this hasn't been a noticeable problem in the past. --- Source/Core/DiscIO/DiscScrubber.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Source/Core/DiscIO/DiscScrubber.cpp') diff --git a/Source/Core/DiscIO/DiscScrubber.cpp b/Source/Core/DiscIO/DiscScrubber.cpp index 07d504d72a..706a2cd00d 100644 --- a/Source/Core/DiscIO/DiscScrubber.cpp +++ b/Source/Core/DiscIO/DiscScrubber.cpp @@ -58,8 +58,8 @@ bool DiscScrubber::CanBlockBeScrubbed(u64 offset) const void DiscScrubber::MarkAsUsed(u64 offset, u64 size) { - u64 current_offset = offset; - const u64 end_offset = current_offset + size; + u64 current_offset = Common::AlignDown(offset, CLUSTER_SIZE); + const u64 end_offset = offset + size; DEBUG_LOG(DISCIO, "Marking 0x%016" PRIx64 " - 0x%016" PRIx64 " as used", offset, end_offset); -- cgit v1.2.3 From 04c7892b93f4ae129609060be5347628fa29eced Mon Sep 17 00:00:00 2001 From: JosJuice Date: Fri, 10 Apr 2020 15:53:14 +0200 Subject: DiscIO: Add GameCube disc scrubbing support The code was actually already rather well adapted for this. We more or less just have to skip ParseDisc and run ParsePartitionData directly. This required the PartitionHeader struct to be removed (which wasn't that useful anyway). --- Source/Core/DiscIO/DiscScrubber.cpp | 98 ++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 39 deletions(-) (limited to 'Source/Core/DiscIO/DiscScrubber.cpp') diff --git a/Source/Core/DiscIO/DiscScrubber.cpp b/Source/Core/DiscIO/DiscScrubber.cpp index 706a2cd00d..06c444c84d 100644 --- a/Source/Core/DiscIO/DiscScrubber.cpp +++ b/Source/Core/DiscIO/DiscScrubber.cpp @@ -72,20 +72,27 @@ void DiscScrubber::MarkAsUsed(u64 offset, u64 size) void DiscScrubber::MarkAsUsedE(u64 partition_data_offset, u64 offset, u64 size) { - u64 first_cluster_start = ToClusterOffset(offset) + partition_data_offset; - - u64 last_cluster_end; - if (size == 0) + if (partition_data_offset == 0) { - // Without this special case, a size of 0 can be rounded to 1 cluster instead of 0 - last_cluster_end = first_cluster_start; + MarkAsUsed(offset, size); } else { - last_cluster_end = ToClusterOffset(offset + size - 1) + CLUSTER_SIZE + partition_data_offset; - } + u64 first_cluster_start = ToClusterOffset(offset) + partition_data_offset; + + u64 last_cluster_end; + if (size == 0) + { + // Without this special case, a size of 0 can be rounded to 1 cluster instead of 0 + last_cluster_end = first_cluster_start; + } + else + { + last_cluster_end = ToClusterOffset(offset + size - 1) + CLUSTER_SIZE + partition_data_offset; + } - MarkAsUsed(first_cluster_start, last_cluster_end - first_cluster_start); + MarkAsUsed(first_cluster_start, last_cluster_end - first_cluster_start); + } } // Compensate for 0x400 (SHA-1) per 0x8000 (cluster), and round to whole clusters @@ -116,35 +123,38 @@ bool DiscScrubber::ReadFromVolume(u64 offset, u64& buffer, const Partition& part bool DiscScrubber::ParseDisc() { + if (m_disc->GetPartitions().empty()) + return ParsePartitionData(PARTITION_NONE); + // Mark the header as used - it's mostly 0s anyways MarkAsUsed(0, 0x50000); for (const DiscIO::Partition& partition : m_disc->GetPartitions()) { - PartitionHeader header; - - if (!ReadFromVolume(partition.offset + 0x2a4, header.tmd_size, PARTITION_NONE) || - !ReadFromVolume(partition.offset + 0x2a8, header.tmd_offset, PARTITION_NONE) || - !ReadFromVolume(partition.offset + 0x2ac, header.cert_chain_size, PARTITION_NONE) || - !ReadFromVolume(partition.offset + 0x2b0, header.cert_chain_offset, PARTITION_NONE) || - !ReadFromVolume(partition.offset + 0x2b4, header.h3_offset, PARTITION_NONE) || - !ReadFromVolume(partition.offset + 0x2b8, header.data_offset, PARTITION_NONE) || - !ReadFromVolume(partition.offset + 0x2bc, header.data_size, PARTITION_NONE)) + u32 tmd_size; + u64 tmd_offset; + u32 cert_chain_size; + u64 cert_chain_offset; + u64 h3_offset; + // The H3 size is always 0x18000 + + if (!ReadFromVolume(partition.offset + 0x2a4, tmd_size, PARTITION_NONE) || + !ReadFromVolume(partition.offset + 0x2a8, tmd_offset, PARTITION_NONE) || + !ReadFromVolume(partition.offset + 0x2ac, cert_chain_size, PARTITION_NONE) || + !ReadFromVolume(partition.offset + 0x2b0, cert_chain_offset, PARTITION_NONE) || + !ReadFromVolume(partition.offset + 0x2b4, h3_offset, PARTITION_NONE)) { return false; } MarkAsUsed(partition.offset, 0x2c0); - MarkAsUsed(partition.offset + header.tmd_offset, header.tmd_size); - MarkAsUsed(partition.offset + header.cert_chain_offset, header.cert_chain_size); - MarkAsUsed(partition.offset + header.h3_offset, 0x18000); - // This would mark the whole (encrypted) data area - // we need to parse FST and other crap to find what's free within it! - // MarkAsUsed(partition.offset + header.data_offset, header.data_size); + MarkAsUsed(partition.offset + tmd_offset, tmd_size); + MarkAsUsed(partition.offset + cert_chain_offset, cert_chain_size); + MarkAsUsed(partition.offset + h3_offset, 0x18000); // Parse Data! This is where the big gain is - if (!ParsePartitionData(partition, &header)) + if (!ParsePartitionData(partition)) return false; } @@ -152,7 +162,7 @@ bool DiscScrubber::ParseDisc() } // Operations dealing with encrypted space are done here -bool DiscScrubber::ParsePartitionData(const Partition& partition, PartitionHeader* header) +bool DiscScrubber::ParsePartitionData(const Partition& partition) { const FileSystem* filesystem = m_disc->GetFileSystem(partition); if (!filesystem) @@ -162,17 +172,30 @@ bool DiscScrubber::ParsePartitionData(const Partition& partition, PartitionHeade return false; } - const u64 partition_data_offset = partition.offset + header->data_offset; + u64 partition_data_offset; + if (partition == PARTITION_NONE) + { + partition_data_offset = 0; + } + else + { + u64 data_offset; + if (!ReadFromVolume(partition.offset + 0x2b8, data_offset, PARTITION_NONE)) + return false; + + partition_data_offset = partition.offset + data_offset; + } // Mark things as used which are not in the filesystem // Header, Header Information, Apploader - if (!ReadFromVolume(0x2440 + 0x14, header->apploader_size, partition) || - !ReadFromVolume(0x2440 + 0x18, header->apploader_size, partition)) + u32 apploader_size; + u32 apploader_trailer_size; + if (!ReadFromVolume(0x2440 + 0x14, apploader_size, partition) || + !ReadFromVolume(0x2440 + 0x18, apploader_trailer_size, partition)) { return false; } - MarkAsUsedE(partition_data_offset, 0, - 0x2440 + header->apploader_size + header->apploader_trailer_size); + MarkAsUsedE(partition_data_offset, 0, 0x2440 + apploader_size + apploader_trailer_size); // DOL const std::optional dol_offset = GetBootDOLOffset(*m_disc, partition); @@ -181,17 +204,14 @@ bool DiscScrubber::ParsePartitionData(const Partition& partition, PartitionHeade const std::optional dol_size = GetBootDOLSize(*m_disc, partition, *dol_offset); if (!dol_size) return false; - header->dol_offset = *dol_offset; - header->dol_size = *dol_size; - MarkAsUsedE(partition_data_offset, header->dol_offset, header->dol_size); + MarkAsUsedE(partition_data_offset, *dol_offset, *dol_size); // FST - if (!ReadFromVolume(0x424, header->fst_offset, partition) || - !ReadFromVolume(0x428, header->fst_size, partition)) - { + const std::optional fst_offset = GetFSTOffset(*m_disc, partition); + const std::optional fst_size = GetFSTSize(*m_disc, partition); + if (!fst_offset || !fst_size) return false; - } - MarkAsUsedE(partition_data_offset, header->fst_offset, header->fst_size); + MarkAsUsedE(partition_data_offset, *fst_offset, *fst_size); // Go through the filesystem and mark entries as used ParseFileSystemData(partition_data_offset, filesystem->GetRoot()); -- cgit v1.2.3 From 6ffcbcee70c043a0d158d87458f99d20eddbb939 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Fri, 10 Apr 2020 17:40:07 +0200 Subject: DiscIO: Move scrubbing code out of ConvertToGCZ This way, scrubbing can also be performed when converting to other formats. --- Source/Core/DiscIO/DiscScrubber.cpp | 2 -- 1 file changed, 2 deletions(-) (limited to 'Source/Core/DiscIO/DiscScrubber.cpp') diff --git a/Source/Core/DiscIO/DiscScrubber.cpp b/Source/Core/DiscIO/DiscScrubber.cpp index 06c444c84d..e7b098b374 100644 --- a/Source/Core/DiscIO/DiscScrubber.cpp +++ b/Source/Core/DiscIO/DiscScrubber.cpp @@ -25,8 +25,6 @@ namespace DiscIO { -constexpr size_t CLUSTER_SIZE = 0x8000; - DiscScrubber::DiscScrubber() = default; DiscScrubber::~DiscScrubber() = default; -- cgit v1.2.3