2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-18 01:08:10 +02:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 05:09:55 +02:00
|
|
|
// Refer to the license.txt file included.
|
2009-05-21 21:19:15 +02:00
|
|
|
|
2017-06-17 12:37:30 +02:00
|
|
|
#include "DiscIO/DiscScrubber.h"
|
|
|
|
|
2014-02-21 01:47:53 +01:00
|
|
|
#include <algorithm>
|
2013-10-26 11:55:41 +02:00
|
|
|
#include <cinttypes>
|
2014-02-21 01:47:53 +01:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdio>
|
2014-08-31 15:34:58 +02:00
|
|
|
#include <memory>
|
2017-06-04 10:33:14 +02:00
|
|
|
#include <optional>
|
2014-02-21 01:47:53 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2013-10-26 11:55:41 +02:00
|
|
|
|
2014-09-08 03:06:58 +02:00
|
|
|
#include "Common/CommonTypes.h"
|
2017-01-15 21:46:32 +01:00
|
|
|
#include "Common/File.h"
|
2015-09-26 23:13:07 +02:00
|
|
|
#include "Common/Logging/Log.h"
|
2017-06-17 12:37:30 +02:00
|
|
|
|
|
|
|
#include "DiscIO/DiscExtractor.h"
|
2014-02-17 11:18:15 +01:00
|
|
|
#include "DiscIO/Filesystem.h"
|
2014-02-21 01:47:53 +01:00
|
|
|
#include "DiscIO/Volume.h"
|
2014-02-17 11:18:15 +01:00
|
|
|
|
2009-05-21 21:19:15 +02:00
|
|
|
namespace DiscIO
|
|
|
|
{
|
2017-01-05 01:53:31 +01:00
|
|
|
constexpr size_t CLUSTER_SIZE = 0x8000;
|
2009-05-21 21:19:15 +02:00
|
|
|
|
2017-01-04 20:31:38 +01:00
|
|
|
DiscScrubber::DiscScrubber() = default;
|
|
|
|
DiscScrubber::~DiscScrubber() = default;
|
2009-05-21 21:19:15 +02:00
|
|
|
|
2019-03-30 16:20:45 +01:00
|
|
|
bool DiscScrubber::SetupScrub(const Volume* disc, int block_size)
|
2009-05-21 21:19:15 +02:00
|
|
|
{
|
2019-03-30 16:20:45 +01:00
|
|
|
if (!disc)
|
|
|
|
return false;
|
|
|
|
m_disc = disc;
|
2017-01-04 21:39:27 +01:00
|
|
|
m_block_size = block_size;
|
2010-02-08 23:46:04 +01:00
|
|
|
|
2017-01-04 21:39:27 +01:00
|
|
|
if (CLUSTER_SIZE % m_block_size != 0)
|
2010-02-08 23:46:04 +01:00
|
|
|
{
|
2017-01-05 01:56:39 +01:00
|
|
|
ERROR_LOG(DISCIO, "Block size %u is not a factor of 0x8000, scrubbing not possible",
|
2017-01-04 21:39:27 +01:00
|
|
|
m_block_size);
|
2010-02-08 23:46:04 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:39:27 +01:00
|
|
|
m_file_size = m_disc->GetSize();
|
2009-05-21 21:19:15 +02:00
|
|
|
|
2019-08-07 10:34:52 +02:00
|
|
|
// Round up when diving by CLUSTER_SIZE, otherwise MarkAsUsed might write out of bounds
|
|
|
|
const size_t num_clusters = static_cast<size_t>((m_file_size + CLUSTER_SIZE - 1) / CLUSTER_SIZE);
|
2009-05-21 21:19:15 +02:00
|
|
|
|
|
|
|
// Warn if not DVD5 or DVD9 size
|
2017-01-05 01:53:31 +01:00
|
|
|
if (num_clusters != 0x23048 && num_clusters != 0x46090)
|
2019-03-30 16:20:45 +01:00
|
|
|
WARN_LOG(DISCIO, "Not a standard sized Wii disc! (%zx blocks)", num_clusters);
|
2009-05-21 21:19:15 +02:00
|
|
|
|
|
|
|
// Table of free blocks
|
2017-01-05 01:53:31 +01:00
|
|
|
m_free_table.resize(num_clusters, 1);
|
2009-05-21 21:19:15 +02:00
|
|
|
|
|
|
|
// Fill out table of free blocks
|
2017-01-04 21:39:27 +01:00
|
|
|
const bool success = ParseDisc();
|
2015-12-07 05:15:51 +01:00
|
|
|
|
2017-01-04 21:39:27 +01:00
|
|
|
m_block_count = 0;
|
2009-05-21 21:19:15 +02:00
|
|
|
|
2017-01-04 21:39:27 +01:00
|
|
|
m_is_scrubbing = success;
|
2010-02-08 23:46:04 +01:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2017-01-04 20:31:38 +01:00
|
|
|
size_t DiscScrubber::GetNextBlock(File::IOFile& in, u8* buffer)
|
2010-02-08 23:46:04 +01:00
|
|
|
{
|
2017-01-04 21:39:27 +01:00
|
|
|
const u64 current_offset = m_block_count * m_block_size;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2017-01-04 21:39:27 +01:00
|
|
|
size_t read_bytes = 0;
|
2019-03-30 16:20:45 +01:00
|
|
|
if (CanBlockBeScrubbed(current_offset))
|
2009-05-21 21:19:15 +02:00
|
|
|
{
|
2017-01-04 21:39:27 +01: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;
|
2010-02-08 23:46:04 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-04 21:39:27 +01:00
|
|
|
DEBUG_LOG(DISCIO, "Used 0x%016" PRIx64, current_offset);
|
|
|
|
in.ReadArray(buffer, m_block_size, &read_bytes);
|
2009-05-21 21:19:15 +02:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2017-01-04 21:39:27 +01:00
|
|
|
m_block_count++;
|
|
|
|
return read_bytes;
|
2010-02-08 23:46:04 +01:00
|
|
|
}
|
2009-05-22 09:14:34 +02:00
|
|
|
|
2019-03-30 16:20:45 +01:00
|
|
|
bool DiscScrubber::CanBlockBeScrubbed(u64 offset) const
|
|
|
|
{
|
|
|
|
return m_is_scrubbing && m_free_table[offset / CLUSTER_SIZE];
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:39:27 +01:00
|
|
|
void DiscScrubber::MarkAsUsed(u64 offset, u64 size)
|
2009-05-21 21:19:15 +02:00
|
|
|
{
|
2017-01-04 21:39:27 +01:00
|
|
|
u64 current_offset = offset;
|
|
|
|
const u64 end_offset = current_offset + size;
|
2009-05-21 21:19:15 +02:00
|
|
|
|
2017-01-04 21:39:27 +01:00
|
|
|
DEBUG_LOG(DISCIO, "Marking 0x%016" PRIx64 " - 0x%016" PRIx64 " as used", offset, end_offset);
|
2009-05-21 21:19:15 +02:00
|
|
|
|
2017-01-04 21:39:27 +01:00
|
|
|
while (current_offset < end_offset && current_offset < m_file_size)
|
2009-05-21 21:19:15 +02:00
|
|
|
{
|
2017-01-04 21:39:27 +01:00
|
|
|
m_free_table[current_offset / CLUSTER_SIZE] = 0;
|
|
|
|
current_offset += CLUSTER_SIZE;
|
2009-05-21 21:19:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 20:31:38 +01:00
|
|
|
void DiscScrubber::MarkAsUsedE(u64 partition_data_offset, u64 offset, u64 size)
|
2016-07-07 16:08:35 +02:00
|
|
|
{
|
2018-09-20 19:32:52 +02:00
|
|
|
u64 first_cluster_start = ToClusterOffset(offset) + partition_data_offset;
|
2009-05-21 21:19:15 +02:00
|
|
|
|
2016-07-07 16:08:35 +02: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
|
|
|
|
{
|
2018-09-20 19:32:52 +02:00
|
|
|
last_cluster_end = ToClusterOffset(offset + size - 1) + CLUSTER_SIZE + partition_data_offset;
|
2016-07-07 16:08:35 +02:00
|
|
|
}
|
2009-05-21 21:19:15 +02:00
|
|
|
|
2016-07-07 16:08:35 +02:00
|
|
|
MarkAsUsed(first_cluster_start, last_cluster_end - first_cluster_start);
|
2009-05-21 21:19:15 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 19:32:52 +02:00
|
|
|
// Compensate for 0x400 (SHA-1) per 0x8000 (cluster), and round to whole clusters
|
|
|
|
u64 DiscScrubber::ToClusterOffset(u64 offset) const
|
|
|
|
{
|
2018-09-20 19:35:29 +02:00
|
|
|
if (m_disc->IsEncryptedAndHashed())
|
|
|
|
return offset / 0x7c00 * CLUSTER_SIZE;
|
|
|
|
else
|
|
|
|
return offset % CLUSTER_SIZE;
|
2018-09-20 19:32:52 +02:00
|
|
|
}
|
|
|
|
|
2009-05-21 21:19:15 +02:00
|
|
|
// Helper functions for reading the BE volume
|
2015-06-13 12:51:24 +02:00
|
|
|
bool DiscScrubber::ReadFromVolume(u64 offset, u32& buffer, const Partition& partition)
|
2009-05-21 21:19:15 +02:00
|
|
|
{
|
2017-06-04 10:33:14 +02:00
|
|
|
std::optional<u32> value = m_disc->ReadSwapped<u32>(offset, partition);
|
|
|
|
if (value)
|
|
|
|
buffer = *value;
|
|
|
|
return value.has_value();
|
2009-05-21 21:19:15 +02:00
|
|
|
}
|
2015-10-22 21:43:22 +02:00
|
|
|
|
2015-06-13 12:51:24 +02:00
|
|
|
bool DiscScrubber::ReadFromVolume(u64 offset, u64& buffer, const Partition& partition)
|
2009-05-21 21:19:15 +02:00
|
|
|
{
|
2017-06-07 11:49:34 +02:00
|
|
|
std::optional<u64> value = m_disc->ReadSwappedAndShifted(offset, partition);
|
2017-06-04 10:33:14 +02:00
|
|
|
if (value)
|
2017-06-07 11:49:34 +02:00
|
|
|
buffer = *value;
|
2017-06-04 10:33:14 +02:00
|
|
|
return value.has_value();
|
2009-05-21 21:19:15 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 20:31:38 +01:00
|
|
|
bool DiscScrubber::ParseDisc()
|
2009-05-21 21:19:15 +02:00
|
|
|
{
|
|
|
|
// Mark the header as used - it's mostly 0s anyways
|
|
|
|
MarkAsUsed(0, 0x50000);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-06-13 12:51:24 +02:00
|
|
|
for (const DiscIO::Partition& partition : m_disc->GetPartitions())
|
2009-05-21 21:19:15 +02:00
|
|
|
{
|
2015-06-13 12:51:24 +02:00
|
|
|
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))
|
2017-01-04 21:39:27 +01:00
|
|
|
{
|
2015-10-22 21:43:22 +02:00
|
|
|
return false;
|
2017-01-04 21:39:27 +01:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-06-13 12:51:24 +02:00
|
|
|
MarkAsUsed(partition.offset, 0x2c0);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-06-13 12:51:24 +02: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);
|
|
|
|
// 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);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-06-13 12:51:24 +02:00
|
|
|
// Parse Data! This is where the big gain is
|
|
|
|
if (!ParsePartitionData(partition, &header))
|
|
|
|
return false;
|
2009-05-21 21:19:15 +02:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2009-05-22 09:14:34 +02:00
|
|
|
return true;
|
2009-05-21 21:19:15 +02:00
|
|
|
}
|
|
|
|
|
2015-06-13 12:51:24 +02:00
|
|
|
// Operations dealing with encrypted space are done here
|
|
|
|
bool DiscScrubber::ParsePartitionData(const Partition& partition, PartitionHeader* header)
|
2009-05-21 21:19:15 +02:00
|
|
|
{
|
2017-08-02 18:16:56 +02:00
|
|
|
const FileSystem* filesystem = m_disc->GetFileSystem(partition);
|
2015-06-13 12:51:24 +02:00
|
|
|
if (!filesystem)
|
|
|
|
{
|
|
|
|
ERROR_LOG(DISCIO, "Failed to read file system for the partition at 0x%" PRIx64,
|
|
|
|
partition.offset);
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-06-13 12:51:24 +02:00
|
|
|
const u64 partition_data_offset = partition.offset + header->data_offset;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-06-13 12:51:24 +02:00
|
|
|
// 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))
|
2015-07-02 08:43:18 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-13 12:51:24 +02:00
|
|
|
MarkAsUsedE(partition_data_offset, 0,
|
|
|
|
0x2440 + header->apploader_size + header->apploader_trailer_size);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-06-13 12:51:24 +02:00
|
|
|
// DOL
|
2017-06-17 12:37:30 +02:00
|
|
|
const std::optional<u64> dol_offset = GetBootDOLOffset(*m_disc, partition);
|
2017-06-04 10:33:14 +02:00
|
|
|
if (!dol_offset)
|
|
|
|
return false;
|
2017-06-17 12:37:30 +02:00
|
|
|
const std::optional<u64> dol_size = GetBootDOLSize(*m_disc, partition, *dol_offset);
|
2017-06-04 10:33:14 +02:00
|
|
|
if (!dol_size)
|
2015-06-13 12:51:24 +02:00
|
|
|
return false;
|
2017-06-04 10:33:14 +02:00
|
|
|
header->dol_offset = *dol_offset;
|
|
|
|
header->dol_size = *dol_size;
|
2015-06-13 12:51:24 +02:00
|
|
|
MarkAsUsedE(partition_data_offset, header->dol_offset, header->dol_size);
|
|
|
|
|
|
|
|
// FST
|
|
|
|
if (!ReadFromVolume(0x424, header->fst_offset, partition) ||
|
|
|
|
!ReadFromVolume(0x428, header->fst_size, partition))
|
2009-05-22 09:14:34 +02:00
|
|
|
{
|
2015-06-13 12:51:24 +02:00
|
|
|
return false;
|
2009-05-22 09:14:34 +02:00
|
|
|
}
|
2015-06-13 12:51:24 +02:00
|
|
|
MarkAsUsedE(partition_data_offset, header->fst_offset, header->fst_size);
|
|
|
|
|
|
|
|
// Go through the filesystem and mark entries as used
|
2015-08-08 19:59:33 +02:00
|
|
|
ParseFileSystemData(partition_data_offset, filesystem->GetRoot());
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-06-13 12:51:24 +02:00
|
|
|
return true;
|
2009-05-21 21:19:15 +02:00
|
|
|
}
|
|
|
|
|
2015-08-08 19:59:33 +02:00
|
|
|
void DiscScrubber::ParseFileSystemData(u64 partition_data_offset, const FileInfo& directory)
|
|
|
|
{
|
|
|
|
for (const DiscIO::FileInfo& file_info : directory)
|
|
|
|
{
|
|
|
|
DEBUG_LOG(DISCIO, "Scrubbing %s", file_info.GetPath().c_str());
|
|
|
|
if (file_info.IsDirectory())
|
|
|
|
ParseFileSystemData(partition_data_offset, file_info);
|
|
|
|
else
|
|
|
|
MarkAsUsedE(partition_data_offset, file_info.GetOffset(), file_info.GetSize());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-21 21:19:15 +02:00
|
|
|
} // namespace DiscIO
|