2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2009-05-21 19:19:15 +00:00
|
|
|
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <algorithm>
|
2013-10-26 09:55:41 +00:00
|
|
|
#include <cinttypes>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdio>
|
2014-08-31 13:34:58 +00:00
|
|
|
#include <memory>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2013-10-26 09:55:41 +00:00
|
|
|
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2015-09-26 21:13:07 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "DiscIO/DiscScrubber.h"
|
|
|
|
#include "DiscIO/Filesystem.h"
|
2014-02-21 00:47:53 +00:00
|
|
|
#include "DiscIO/Volume.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "DiscIO/VolumeCreator.h"
|
|
|
|
|
2009-05-21 19:19:15 +00:00
|
|
|
namespace DiscIO
|
|
|
|
{
|
2017-01-05 00:53:31 +00:00
|
|
|
constexpr size_t CLUSTER_SIZE = 0x8000;
|
2009-05-21 19:19:15 +00:00
|
|
|
|
2017-01-04 19:31:38 +00:00
|
|
|
DiscScrubber::DiscScrubber() = default;
|
|
|
|
DiscScrubber::~DiscScrubber() = default;
|
2009-05-21 19:19:15 +00:00
|
|
|
|
2017-01-04 19:31:38 +00:00
|
|
|
bool DiscScrubber::SetupScrub(const std::string& filename, int block_size)
|
2009-05-21 19:19:15 +00:00
|
|
|
{
|
2017-01-04 20:39:27 +00:00
|
|
|
m_filename = filename;
|
|
|
|
m_block_size = block_size;
|
2010-02-08 22:46:04 +00:00
|
|
|
|
2017-01-04 20:39:27 +00:00
|
|
|
if (CLUSTER_SIZE % m_block_size != 0)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-01-05 00:56:39 +00:00
|
|
|
ERROR_LOG(DISCIO, "Block size %u is not a factor of 0x8000, scrubbing not possible",
|
2017-01-04 20:39:27 +00:00
|
|
|
m_block_size);
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-02-08 22:46:04 +00:00
|
|
|
|
2017-01-04 19:31:38 +00:00
|
|
|
m_disc = CreateVolumeFromFilename(filename);
|
|
|
|
if (!m_disc)
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
2015-08-10 17:11:29 +00:00
|
|
|
|
2017-01-04 20:39:27 +00:00
|
|
|
m_file_size = m_disc->GetSize();
|
2009-05-21 19:19:15 +00:00
|
|
|
|
2017-01-05 00:53:31 +00:00
|
|
|
const size_t num_clusters = static_cast<size_t>(m_file_size / CLUSTER_SIZE);
|
2009-05-21 19:19:15 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Warn if not DVD5 or DVD9 size
|
2017-01-05 00:53:31 +00:00
|
|
|
if (num_clusters != 0x23048 && num_clusters != 0x46090)
|
2017-01-04 19:31:38 +00:00
|
|
|
{
|
2017-01-05 00:53:31 +00:00
|
|
|
WARN_LOG(DISCIO, "%s is not a standard sized Wii disc! (%zx blocks)", filename.c_str(),
|
|
|
|
num_clusters);
|
2017-01-04 19:31:38 +00:00
|
|
|
}
|
2009-05-21 19:19:15 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Table of free blocks
|
2017-01-05 00:53:31 +00:00
|
|
|
m_free_table.resize(num_clusters, 1);
|
2009-05-21 19:19:15 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Fill out table of free blocks
|
2017-01-04 20:39:27 +00:00
|
|
|
const bool success = ParseDisc();
|
2015-12-07 04:15:51 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Done with it; need it closed for the next part
|
2017-01-04 19:31:38 +00:00
|
|
|
m_disc.reset();
|
2017-01-04 20:39:27 +00:00
|
|
|
m_block_count = 0;
|
2009-05-21 19:19:15 +00:00
|
|
|
|
2017-01-04 20:39:27 +00:00
|
|
|
m_is_scrubbing = success;
|
2016-06-24 08:43:46 +00:00
|
|
|
return success;
|
2010-02-08 22:46:04 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 19:31:38 +00:00
|
|
|
size_t DiscScrubber::GetNextBlock(File::IOFile& in, u8* buffer)
|
2010-02-08 22:46:04 +00:00
|
|
|
{
|
2017-01-04 20:39:27 +00:00
|
|
|
const u64 current_offset = m_block_count * m_block_size;
|
|
|
|
const u64 i = current_offset / CLUSTER_SIZE;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-01-04 20:39:27 +00:00
|
|
|
size_t read_bytes = 0;
|
|
|
|
if (m_is_scrubbing && m_free_table[i])
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-01-04 20:39:27 +00:00
|
|
|
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;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-04 20:39:27 +00:00
|
|
|
DEBUG_LOG(DISCIO, "Used 0x%016" PRIx64, current_offset);
|
|
|
|
in.ReadArray(buffer, m_block_size, &read_bytes);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 20:39:27 +00:00
|
|
|
m_block_count++;
|
|
|
|
return read_bytes;
|
2010-02-08 22:46:04 +00:00
|
|
|
}
|
2009-05-22 07:14:34 +00:00
|
|
|
|
2017-01-04 20:39:27 +00:00
|
|
|
void DiscScrubber::MarkAsUsed(u64 offset, u64 size)
|
2009-05-21 19:19:15 +00:00
|
|
|
{
|
2017-01-04 20:39:27 +00:00
|
|
|
u64 current_offset = offset;
|
|
|
|
const u64 end_offset = current_offset + size;
|
2009-05-21 19:19:15 +00:00
|
|
|
|
2017-01-04 20:39:27 +00:00
|
|
|
DEBUG_LOG(DISCIO, "Marking 0x%016" PRIx64 " - 0x%016" PRIx64 " as used", offset, end_offset);
|
2009-05-21 19:19:15 +00:00
|
|
|
|
2017-01-04 20:39:27 +00:00
|
|
|
while (current_offset < end_offset && current_offset < m_file_size)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-01-04 20:39:27 +00:00
|
|
|
m_free_table[current_offset / CLUSTER_SIZE] = 0;
|
|
|
|
current_offset += CLUSTER_SIZE;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2009-05-21 19:19:15 +00:00
|
|
|
}
|
|
|
|
|
2016-07-07 14:08:35 +00:00
|
|
|
// Compensate for 0x400 (SHA-1) per 0x8000 (cluster), and round to whole clusters
|
2017-01-04 19:31:38 +00:00
|
|
|
void DiscScrubber::MarkAsUsedE(u64 partition_data_offset, u64 offset, u64 size)
|
2016-07-07 14:08:35 +00:00
|
|
|
{
|
|
|
|
u64 first_cluster_start = offset / 0x7c00 * CLUSTER_SIZE + partition_data_offset;
|
2009-05-21 19:19:15 +00:00
|
|
|
|
2016-07-07 14:08:35 +00:00
|
|
|
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 = ((offset + size - 1) / 0x7c00 + 1) * CLUSTER_SIZE + partition_data_offset;
|
|
|
|
}
|
2009-05-21 19:19:15 +00:00
|
|
|
|
2016-07-07 14:08:35 +00:00
|
|
|
MarkAsUsed(first_cluster_start, last_cluster_end - first_cluster_start);
|
2009-05-21 19:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper functions for reading the BE volume
|
2017-01-04 20:39:27 +00:00
|
|
|
bool DiscScrubber::ReadFromVolume(u64 offset, u32& buffer, bool decrypt)
|
2009-05-21 19:19:15 +00:00
|
|
|
{
|
2017-01-04 20:39:27 +00:00
|
|
|
return m_disc->ReadSwapped(offset, &buffer, decrypt);
|
2009-05-21 19:19:15 +00:00
|
|
|
}
|
2015-10-22 19:43:22 +00:00
|
|
|
|
2017-01-04 20:39:27 +00:00
|
|
|
bool DiscScrubber::ReadFromVolume(u64 offset, u64& buffer, bool decrypt)
|
2009-05-21 19:19:15 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
u32 temp_buffer;
|
2017-01-04 20:39:27 +00:00
|
|
|
if (!m_disc->ReadSwapped(offset, &temp_buffer, decrypt))
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
2017-01-04 20:39:27 +00:00
|
|
|
buffer = static_cast<u64>(temp_buffer) << 2;
|
2016-06-24 08:43:46 +00:00
|
|
|
return true;
|
2009-05-21 19:19:15 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 19:31:38 +00:00
|
|
|
bool DiscScrubber::ParseDisc()
|
2009-05-21 19:19:15 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// Mark the header as used - it's mostly 0s anyways
|
|
|
|
MarkAsUsed(0, 0x50000);
|
|
|
|
|
2017-01-05 01:03:09 +00:00
|
|
|
for (u32 x = 0; x < 4; x++)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-01-04 20:39:27 +00:00
|
|
|
if (!ReadFromVolume(0x40000 + (x * 8) + 0, m_partition_group[x].num_partitions, false) ||
|
|
|
|
!ReadFromVolume(0x40000 + (x * 8) + 4, m_partition_group[x].partitions_offset, false))
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
2017-01-04 20:39:27 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// Read all partitions
|
2017-01-04 20:39:27 +00:00
|
|
|
for (u32 i = 0; i < m_partition_group[x].num_partitions; i++)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-01-04 20:39:27 +00:00
|
|
|
Partition partition;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-01-04 20:39:27 +00:00
|
|
|
partition.group_number = x;
|
|
|
|
partition.number = i;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-01-04 20:39:27 +00:00
|
|
|
if (!ReadFromVolume(m_partition_group[x].partitions_offset + (i * 8) + 0, partition.offset,
|
2016-06-24 08:43:46 +00:00
|
|
|
false) ||
|
2017-01-04 20:39:27 +00:00
|
|
|
!ReadFromVolume(m_partition_group[x].partitions_offset + (i * 8) + 4, partition.type,
|
2016-06-24 08:43:46 +00:00
|
|
|
false) ||
|
2017-01-04 20:39:27 +00:00
|
|
|
!ReadFromVolume(partition.offset + 0x2a4, partition.header.tmd_size, false) ||
|
|
|
|
!ReadFromVolume(partition.offset + 0x2a8, partition.header.tmd_offset, false) ||
|
|
|
|
!ReadFromVolume(partition.offset + 0x2ac, partition.header.cert_chain_size, false) ||
|
|
|
|
!ReadFromVolume(partition.offset + 0x2b0, partition.header.cert_chain_offset, false) ||
|
|
|
|
!ReadFromVolume(partition.offset + 0x2b4, partition.header.h3_offset, false) ||
|
|
|
|
!ReadFromVolume(partition.offset + 0x2b8, partition.header.data_offset, false) ||
|
|
|
|
!ReadFromVolume(partition.offset + 0x2bc, partition.header.data_size, false))
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
2017-01-04 20:39:27 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-01-04 20:39:27 +00:00
|
|
|
m_partition_group[x].partitions.push_back(partition);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 20:39:27 +00:00
|
|
|
for (auto& partition : m_partition_group[x].partitions)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-01-04 20:39:27 +00:00
|
|
|
const PartitionHeader& header = partition.header;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-01-04 20:39:27 +00:00
|
|
|
MarkAsUsed(partition.offset, 0x2c0);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-01-04 20:39:27 +00:00
|
|
|
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);
|
2016-06-24 08:43:46 +00:00
|
|
|
// This would mark the whole (encrypted) data area
|
|
|
|
// we need to parse FST and other crap to find what's free within it!
|
2017-01-04 20:39:27 +00:00
|
|
|
// MarkAsUsed(partition.offset + header.data_offset, header.data_size);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// Parse Data! This is where the big gain is
|
2017-01-04 20:39:27 +00:00
|
|
|
if (!ParsePartitionData(partition))
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2009-05-21 19:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Operations dealing with encrypted space are done here - the volume is swapped to allow this
|
2017-01-04 20:39:27 +00:00
|
|
|
bool DiscScrubber::ParsePartitionData(Partition& partition)
|
2009-05-21 19:19:15 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
bool parsed_ok = true;
|
|
|
|
|
|
|
|
// Switch out the main volume temporarily
|
|
|
|
std::unique_ptr<IVolume> old_volume;
|
2017-01-04 19:31:38 +00:00
|
|
|
m_disc.swap(old_volume);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// Ready some stuff
|
2017-01-04 20:39:27 +00:00
|
|
|
m_disc = CreateVolumeFromFilename(m_filename, partition.group_number, partition.number);
|
2017-01-04 19:31:38 +00:00
|
|
|
if (m_disc == nullptr)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-01-04 20:39:27 +00:00
|
|
|
ERROR_LOG(DISCIO, "Failed to create volume from file %s", m_filename.c_str());
|
2017-01-04 19:31:38 +00:00
|
|
|
m_disc.swap(old_volume);
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-04 19:31:38 +00:00
|
|
|
std::unique_ptr<IFileSystem> filesystem(CreateFileSystem(m_disc.get()));
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!filesystem)
|
|
|
|
{
|
2017-01-05 00:56:39 +00:00
|
|
|
ERROR_LOG(DISCIO, "Failed to create filesystem for group %u partition %u",
|
2017-01-04 20:39:27 +00:00
|
|
|
partition.group_number, partition.number);
|
2016-06-24 08:43:46 +00:00
|
|
|
parsed_ok = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Mark things as used which are not in the filesystem
|
|
|
|
// Header, Header Information, Apploader
|
2017-01-04 20:39:27 +00:00
|
|
|
parsed_ok = parsed_ok && ReadFromVolume(0x2440 + 0x14, partition.header.apploader_size, true);
|
2016-06-24 08:43:46 +00:00
|
|
|
parsed_ok =
|
2017-01-04 20:39:27 +00:00
|
|
|
parsed_ok && ReadFromVolume(0x2440 + 0x18, partition.header.apploader_trailer_size, true);
|
|
|
|
MarkAsUsedE(partition.offset + partition.header.data_offset, 0,
|
|
|
|
0x2440 + partition.header.apploader_size + partition.header.apploader_trailer_size);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// DOL
|
2017-01-04 20:39:27 +00:00
|
|
|
partition.header.dol_offset = filesystem->GetBootDOLOffset();
|
|
|
|
partition.header.dol_size = filesystem->GetBootDOLSize(partition.header.dol_offset);
|
|
|
|
parsed_ok = parsed_ok && partition.header.dol_offset && partition.header.dol_size;
|
|
|
|
MarkAsUsedE(partition.offset + partition.header.data_offset, partition.header.dol_offset,
|
|
|
|
partition.header.dol_size);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// FST
|
2017-01-04 20:39:27 +00:00
|
|
|
parsed_ok = parsed_ok && ReadFromVolume(0x424, partition.header.fst_offset, true);
|
|
|
|
parsed_ok = parsed_ok && ReadFromVolume(0x428, partition.header.fst_size, true);
|
|
|
|
MarkAsUsedE(partition.offset + partition.header.data_offset, partition.header.fst_offset,
|
|
|
|
partition.header.fst_size);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// Go through the filesystem and mark entries as used
|
|
|
|
for (SFileInfo file : filesystem->GetFileList())
|
|
|
|
{
|
|
|
|
DEBUG_LOG(DISCIO, "%s", file.m_FullPath.empty() ? "/" : file.m_FullPath.c_str());
|
|
|
|
if ((file.m_NameOffset & 0x1000000) == 0)
|
2017-01-04 20:39:27 +00:00
|
|
|
{
|
|
|
|
MarkAsUsedE(partition.offset + partition.header.data_offset, file.m_Offset,
|
|
|
|
file.m_FileSize);
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap back
|
2017-01-04 19:31:38 +00:00
|
|
|
m_disc.swap(old_volume);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
return parsed_ok;
|
2009-05-21 19:19:15 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
} // namespace DiscIO
|