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