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
|
|
|
|
2017-06-17 04:37:30 -06:00
|
|
|
#include "DiscIO/DiscScrubber.h"
|
|
|
|
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstddef>
|
2014-08-31 07:34:58 -06:00
|
|
|
#include <memory>
|
2017-06-04 02:33:14 -06:00
|
|
|
#include <optional>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2013-10-26 03:55:41 -06:00
|
|
|
|
2019-12-08 11:40:57 -07:00
|
|
|
#include "Common/Align.h"
|
2020-04-04 12:56:20 -06:00
|
|
|
#include "Common/Assert.h"
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2015-09-26 15:13:07 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2017-06-17 04:37:30 -06:00
|
|
|
|
2021-03-09 12:06:34 -07:00
|
|
|
#include "DiscIO/DiscUtils.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "DiscIO/Filesystem.h"
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "DiscIO/Volume.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
|
2009-05-21 13:19:15 -06:00
|
|
|
namespace DiscIO
|
|
|
|
{
|
2017-01-04 12:31:38 -07:00
|
|
|
DiscScrubber::DiscScrubber() = default;
|
|
|
|
DiscScrubber::~DiscScrubber() = default;
|
2009-05-21 13:19:15 -06:00
|
|
|
|
2020-04-04 12:56:20 -06:00
|
|
|
bool DiscScrubber::SetupScrub(const Volume* disc)
|
2009-05-21 13:19:15 -06:00
|
|
|
{
|
2019-03-30 09:20:45 -06:00
|
|
|
if (!disc)
|
|
|
|
return false;
|
|
|
|
m_disc = disc;
|
2010-02-08 15:46:04 -07:00
|
|
|
|
2017-01-04 13:39:27 -07:00
|
|
|
m_file_size = m_disc->GetSize();
|
2009-05-21 13:19:15 -06:00
|
|
|
|
2019-08-07 02:34:52 -06: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 13:19:15 -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
|
|
|
|
|
|
|
// 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
|
|
|
|
2017-01-04 13:39:27 -07:00
|
|
|
m_is_scrubbing = success;
|
2010-02-08 15:46:04 -07:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2019-03-30 09:20:45 -06:00
|
|
|
bool DiscScrubber::CanBlockBeScrubbed(u64 offset) const
|
|
|
|
{
|
|
|
|
return m_is_scrubbing && m_free_table[offset / CLUSTER_SIZE];
|
|
|
|
}
|
|
|
|
|
2017-01-04 13:39:27 -07:00
|
|
|
void DiscScrubber::MarkAsUsed(u64 offset, u64 size)
|
2009-05-21 13:19:15 -06:00
|
|
|
{
|
2020-04-10 08:21:51 -06:00
|
|
|
u64 current_offset = Common::AlignDown(offset, CLUSTER_SIZE);
|
|
|
|
const u64 end_offset = offset + size;
|
2009-05-21 13:19:15 -06:00
|
|
|
|
2020-10-21 12:58:08 -06:00
|
|
|
DEBUG_LOG_FMT(DISCIO, "Marking {:#018x} - {:#018x} 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)
|
2009-05-21 13:19:15 -06:00
|
|
|
{
|
2017-01-04 13:39:27 -07:00
|
|
|
m_free_table[current_offset / CLUSTER_SIZE] = 0;
|
|
|
|
current_offset += CLUSTER_SIZE;
|
2009-05-21 13:19:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2020-04-10 07:53:14 -06:00
|
|
|
if (partition_data_offset == 0)
|
2016-07-07 08:08:35 -06:00
|
|
|
{
|
2020-04-10 07:53:14 -06:00
|
|
|
MarkAsUsed(offset, size);
|
2016-07-07 08:08:35 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-10 07:53:14 -06:00
|
|
|
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;
|
|
|
|
}
|
2009-05-21 13:19:15 -06:00
|
|
|
|
2020-04-10 07:53:14 -06:00
|
|
|
MarkAsUsed(first_cluster_start, last_cluster_end - first_cluster_start);
|
|
|
|
}
|
2009-05-21 13:19:15 -06:00
|
|
|
}
|
|
|
|
|
2018-09-20 11:32:52 -06:00
|
|
|
// Compensate for 0x400 (SHA-1) per 0x8000 (cluster), and round to whole clusters
|
|
|
|
u64 DiscScrubber::ToClusterOffset(u64 offset) const
|
|
|
|
{
|
2018-09-20 11:35:29 -06:00
|
|
|
if (m_disc->IsEncryptedAndHashed())
|
|
|
|
return offset / 0x7c00 * CLUSTER_SIZE;
|
|
|
|
else
|
2019-12-08 11:40:57 -07:00
|
|
|
return Common::AlignDown(offset, CLUSTER_SIZE);
|
2018-09-20 11:32:52 -06:00
|
|
|
}
|
|
|
|
|
2009-05-21 13:19:15 -06:00
|
|
|
// Helper functions for reading the BE volume
|
2015-06-13 04:51:24 -06:00
|
|
|
bool DiscScrubber::ReadFromVolume(u64 offset, u32& buffer, const Partition& partition)
|
2009-05-21 13:19:15 -06:00
|
|
|
{
|
2017-06-04 02:33:14 -06:00
|
|
|
std::optional<u32> value = m_disc->ReadSwapped<u32>(offset, partition);
|
|
|
|
if (value)
|
|
|
|
buffer = *value;
|
|
|
|
return value.has_value();
|
2009-05-21 13:19:15 -06:00
|
|
|
}
|
2015-10-22 13:43:22 -06:00
|
|
|
|
2015-06-13 04:51:24 -06:00
|
|
|
bool DiscScrubber::ReadFromVolume(u64 offset, u64& buffer, const Partition& partition)
|
2009-05-21 13:19:15 -06:00
|
|
|
{
|
2017-06-07 03:49:34 -06:00
|
|
|
std::optional<u64> value = m_disc->ReadSwappedAndShifted(offset, partition);
|
2017-06-04 02:33:14 -06:00
|
|
|
if (value)
|
2017-06-07 03:49:34 -06:00
|
|
|
buffer = *value;
|
2017-06-04 02:33:14 -06:00
|
|
|
return value.has_value();
|
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
|
|
|
{
|
2020-04-10 07:53:14 -06:00
|
|
|
if (m_disc->GetPartitions().empty())
|
|
|
|
return ParsePartitionData(PARTITION_NONE);
|
|
|
|
|
2009-05-21 13:19:15 -06:00
|
|
|
// Mark the header as used - it's mostly 0s anyways
|
|
|
|
MarkAsUsed(0, 0x50000);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-06-13 04:51:24 -06:00
|
|
|
for (const DiscIO::Partition& partition : m_disc->GetPartitions())
|
2009-05-21 13:19:15 -06:00
|
|
|
{
|
2020-04-10 07:53:14 -06:00
|
|
|
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))
|
2017-01-04 13:39:27 -07:00
|
|
|
{
|
2015-10-22 13:43:22 -06:00
|
|
|
return false;
|
2017-01-04 13:39:27 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-06-13 04:51:24 -06:00
|
|
|
MarkAsUsed(partition.offset, 0x2c0);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2020-04-10 07:53:14 -06:00
|
|
|
MarkAsUsed(partition.offset + tmd_offset, tmd_size);
|
|
|
|
MarkAsUsed(partition.offset + cert_chain_offset, cert_chain_size);
|
|
|
|
MarkAsUsed(partition.offset + h3_offset, 0x18000);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-06-13 04:51:24 -06:00
|
|
|
// Parse Data! This is where the big gain is
|
2020-04-10 07:53:14 -06:00
|
|
|
if (!ParsePartitionData(partition))
|
2015-06-13 04:51:24 -06:00
|
|
|
return false;
|
2009-05-21 13:19:15 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2009-05-22 01:14:34 -06:00
|
|
|
return true;
|
2009-05-21 13:19:15 -06:00
|
|
|
}
|
|
|
|
|
2015-06-13 04:51:24 -06:00
|
|
|
// Operations dealing with encrypted space are done here
|
2020-04-10 07:53:14 -06:00
|
|
|
bool DiscScrubber::ParsePartitionData(const Partition& partition)
|
2009-05-21 13:19:15 -06:00
|
|
|
{
|
2017-08-02 10:16:56 -06:00
|
|
|
const FileSystem* filesystem = m_disc->GetFileSystem(partition);
|
2015-06-13 04:51:24 -06:00
|
|
|
if (!filesystem)
|
|
|
|
{
|
2020-10-21 12:58:08 -06:00
|
|
|
ERROR_LOG_FMT(DISCIO, "Failed to read file system for the partition at {:#x}",
|
|
|
|
partition.offset);
|
2015-06-13 04:51:24 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2020-04-10 07:53:14 -06:00
|
|
|
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;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-06-13 04:51:24 -06:00
|
|
|
// Mark things as used which are not in the filesystem
|
|
|
|
// Header, Header Information, Apploader
|
2020-04-10 07:53:14 -06:00
|
|
|
u32 apploader_size;
|
|
|
|
u32 apploader_trailer_size;
|
|
|
|
if (!ReadFromVolume(0x2440 + 0x14, apploader_size, partition) ||
|
|
|
|
!ReadFromVolume(0x2440 + 0x18, apploader_trailer_size, partition))
|
2015-07-02 00:43:18 -06:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-10 07:53:14 -06:00
|
|
|
MarkAsUsedE(partition_data_offset, 0, 0x2440 + apploader_size + apploader_trailer_size);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-06-13 04:51:24 -06:00
|
|
|
// DOL
|
2017-06-17 04:37:30 -06:00
|
|
|
const std::optional<u64> dol_offset = GetBootDOLOffset(*m_disc, partition);
|
2017-06-04 02:33:14 -06:00
|
|
|
if (!dol_offset)
|
|
|
|
return false;
|
2017-06-17 04:37:30 -06:00
|
|
|
const std::optional<u64> dol_size = GetBootDOLSize(*m_disc, partition, *dol_offset);
|
2017-06-04 02:33:14 -06:00
|
|
|
if (!dol_size)
|
2015-06-13 04:51:24 -06:00
|
|
|
return false;
|
2020-04-10 07:53:14 -06:00
|
|
|
MarkAsUsedE(partition_data_offset, *dol_offset, *dol_size);
|
2015-06-13 04:51:24 -06:00
|
|
|
|
|
|
|
// FST
|
2020-04-10 07:53:14 -06:00
|
|
|
const std::optional<u64> fst_offset = GetFSTOffset(*m_disc, partition);
|
|
|
|
const std::optional<u64> fst_size = GetFSTSize(*m_disc, partition);
|
|
|
|
if (!fst_offset || !fst_size)
|
2015-06-13 04:51:24 -06:00
|
|
|
return false;
|
2020-04-10 07:53:14 -06:00
|
|
|
MarkAsUsedE(partition_data_offset, *fst_offset, *fst_size);
|
2015-06-13 04:51:24 -06:00
|
|
|
|
|
|
|
// Go through the filesystem and mark entries as used
|
2015-08-08 11:59:33 -06:00
|
|
|
ParseFileSystemData(partition_data_offset, filesystem->GetRoot());
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-06-13 04:51:24 -06:00
|
|
|
return true;
|
2009-05-21 13:19:15 -06:00
|
|
|
}
|
|
|
|
|
2015-08-08 11:59:33 -06:00
|
|
|
void DiscScrubber::ParseFileSystemData(u64 partition_data_offset, const FileInfo& directory)
|
|
|
|
{
|
|
|
|
for (const DiscIO::FileInfo& file_info : directory)
|
|
|
|
{
|
2020-10-21 12:58:08 -06:00
|
|
|
DEBUG_LOG_FMT(DISCIO, "Scrubbing {}", file_info.GetPath());
|
2015-08-08 11:59:33 -06:00
|
|
|
if (file_info.IsDirectory())
|
|
|
|
ParseFileSystemData(partition_data_offset, file_info);
|
|
|
|
else
|
|
|
|
MarkAsUsedE(partition_data_offset, file_info.GetOffset(), file_info.GetSize());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-21 13:19:15 -06:00
|
|
|
} // namespace DiscIO
|