2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
#include "DiscIO/DirectoryBlob.h"
|
|
|
|
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <algorithm>
|
2017-06-07 12:32:09 -06:00
|
|
|
#include <array>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <cstring>
|
2017-01-24 10:18:26 -07:00
|
|
|
#include <locale>
|
2017-06-10 09:51:22 -06:00
|
|
|
#include <map>
|
2014-08-25 21:40:05 -06:00
|
|
|
#include <memory>
|
2017-06-08 08:07:01 -06:00
|
|
|
#include <set>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <string>
|
2017-06-07 12:32:09 -06:00
|
|
|
#include <utility>
|
2017-06-08 08:37:51 -06:00
|
|
|
#include <variant>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <vector>
|
|
|
|
|
2016-11-27 03:56:22 -07:00
|
|
|
#include "Common/Align.h"
|
2015-09-26 14:39:47 -06:00
|
|
|
#include "Common/Assert.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/CommonPaths.h"
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "Common/FileUtil.h"
|
2020-09-15 04:29:41 -06:00
|
|
|
#include "Common/IOFile.h"
|
2015-09-26 14:39:47 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2017-05-01 06:08:47 -06:00
|
|
|
#include "Common/StringUtil.h"
|
2017-06-07 12:32:09 -06:00
|
|
|
#include "Common/Swap.h"
|
|
|
|
#include "Core/Boot/DolReader.h"
|
2020-01-23 08:47:49 -07:00
|
|
|
#include "Core/IOS/ES/Formats.h"
|
2015-09-27 06:01:12 -06:00
|
|
|
#include "DiscIO/Blob.h"
|
2021-03-09 13:06:57 -07:00
|
|
|
#include "DiscIO/DiscUtils.h"
|
2021-09-21 20:18:05 -06:00
|
|
|
#include "DiscIO/VolumeDisc.h"
|
2017-06-10 11:07:12 -06:00
|
|
|
#include "DiscIO/VolumeWii.h"
|
2020-01-23 08:47:49 -07:00
|
|
|
#include "DiscIO/WiiEncryptionCache.h"
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2008-09-22 18:05:08 -06:00
|
|
|
namespace DiscIO
|
|
|
|
{
|
2017-06-10 00:46:21 -06:00
|
|
|
// Reads as many bytes as the vector fits (or less, if the file is smaller).
|
|
|
|
// Returns the number of bytes read.
|
|
|
|
static size_t ReadFileToVector(const std::string& path, std::vector<u8>* vector);
|
|
|
|
|
2017-06-10 06:42:41 -06:00
|
|
|
static void PadToAddress(u64 start_address, u64* address, u64* length, u8** buffer);
|
|
|
|
static void Write32(u32 data, u32 offset, std::vector<u8>* buffer);
|
|
|
|
|
2017-06-11 05:22:13 -06:00
|
|
|
enum class PartitionType : u32
|
|
|
|
{
|
|
|
|
Game = 0,
|
|
|
|
Update = 1,
|
|
|
|
Channel = 2,
|
|
|
|
// There are more types used by Super Smash Bros. Brawl, but they don't have special names
|
|
|
|
};
|
|
|
|
|
2017-07-18 07:53:04 -06:00
|
|
|
// 0xFF is an arbitrarily picked value. Note that we can't use 0x00, because that means NTSC-J
|
|
|
|
constexpr u32 INVALID_REGION = 0xFF;
|
|
|
|
|
2018-05-22 14:39:52 -06:00
|
|
|
constexpr u32 PARTITION_DATA_OFFSET = 0x20000;
|
|
|
|
|
2017-06-07 13:13:24 -06:00
|
|
|
constexpr u8 ENTRY_SIZE = 0x0c;
|
|
|
|
constexpr u8 FILE_ENTRY = 0;
|
|
|
|
constexpr u8 DIRECTORY_ENTRY = 1;
|
2015-11-17 02:09:54 -07:00
|
|
|
|
2021-09-20 23:58:41 -06:00
|
|
|
DiscContent::DiscContent(u64 offset, u64 size, ContentSource source)
|
|
|
|
: m_offset(offset), m_size(size), m_content_source(std::move(source))
|
2020-01-23 08:47:49 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-08 08:07:01 -06:00
|
|
|
DiscContent::DiscContent(u64 offset) : m_offset(offset)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 DiscContent::GetOffset() const
|
|
|
|
{
|
|
|
|
return m_offset;
|
|
|
|
}
|
|
|
|
|
2017-08-01 11:04:33 -06:00
|
|
|
u64 DiscContent::GetEndOffset() const
|
|
|
|
{
|
|
|
|
return m_offset + m_size;
|
|
|
|
}
|
|
|
|
|
2017-06-08 08:07:01 -06:00
|
|
|
u64 DiscContent::GetSize() const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DiscContent::Read(u64* offset, u64* length, u8** buffer) const
|
|
|
|
{
|
|
|
|
if (m_size == 0)
|
|
|
|
return true;
|
|
|
|
|
2018-03-16 10:57:36 -06:00
|
|
|
DEBUG_ASSERT(*offset >= m_offset);
|
2017-06-08 08:07:01 -06:00
|
|
|
const u64 offset_in_content = *offset - m_offset;
|
|
|
|
|
|
|
|
if (offset_in_content < m_size)
|
|
|
|
{
|
|
|
|
const u64 bytes_to_read = std::min(m_size - offset_in_content, *length);
|
|
|
|
|
2021-09-21 17:37:45 -06:00
|
|
|
if (std::holds_alternative<ContentFile>(m_content_source))
|
2017-06-08 08:37:51 -06:00
|
|
|
{
|
2021-09-21 17:37:45 -06:00
|
|
|
const auto& content = std::get<ContentFile>(m_content_source);
|
|
|
|
File::IOFile file(content.m_filename, "rb");
|
|
|
|
if (!file.Seek(content.m_offset + offset_in_content, SEEK_SET) ||
|
|
|
|
!file.ReadBytes(*buffer, bytes_to_read))
|
|
|
|
{
|
2017-06-08 08:37:51 -06:00
|
|
|
return false;
|
2021-09-21 17:37:45 -06:00
|
|
|
}
|
2017-06-08 08:37:51 -06:00
|
|
|
}
|
2020-01-23 08:47:49 -07:00
|
|
|
else if (std::holds_alternative<const u8*>(m_content_source))
|
2017-06-08 08:37:51 -06:00
|
|
|
{
|
|
|
|
const u8* const content_pointer = std::get<const u8*>(m_content_source) + offset_in_content;
|
|
|
|
std::copy(content_pointer, content_pointer + bytes_to_read, *buffer);
|
|
|
|
}
|
2021-09-21 18:51:10 -06:00
|
|
|
else if (std::holds_alternative<ContentPartition>(m_content_source))
|
2020-01-23 08:47:49 -07:00
|
|
|
{
|
2021-09-21 18:51:10 -06:00
|
|
|
const auto& content = std::get<ContentPartition>(m_content_source);
|
|
|
|
DirectoryBlobReader* blob = content.m_reader;
|
2020-01-23 08:47:49 -07:00
|
|
|
const u64 decrypted_size = m_size * VolumeWii::BLOCK_DATA_SIZE / VolumeWii::BLOCK_TOTAL_SIZE;
|
2021-09-21 18:51:10 -06:00
|
|
|
if (!blob->EncryptPartitionData(content.m_offset + offset_in_content, bytes_to_read, *buffer,
|
|
|
|
content.m_partition_data_offset, decrypted_size))
|
2020-01-23 08:47:49 -07:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-09-21 18:57:31 -06:00
|
|
|
else if (std::holds_alternative<ContentVolume>(m_content_source))
|
|
|
|
{
|
|
|
|
const auto& source = std::get<ContentVolume>(m_content_source);
|
|
|
|
if (!source.m_volume->Read(source.m_offset + offset_in_content, bytes_to_read, *buffer,
|
|
|
|
source.m_partition))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-09-21 19:02:39 -06:00
|
|
|
else if (std::holds_alternative<ContentFixedByte>(m_content_source))
|
|
|
|
{
|
|
|
|
const ContentFixedByte& source = std::get<ContentFixedByte>(m_content_source);
|
|
|
|
std::fill_n(*buffer, bytes_to_read, source.m_byte);
|
|
|
|
}
|
2021-09-21 18:51:10 -06:00
|
|
|
else
|
|
|
|
{
|
|
|
|
PanicAlertFmt("DirectoryBlob: Invalid content source in DiscContent.");
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-08 08:07:01 -06:00
|
|
|
|
|
|
|
*length -= bytes_to_read;
|
|
|
|
*buffer += bytes_to_read;
|
|
|
|
*offset += bytes_to_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-20 23:58:41 -06:00
|
|
|
void DiscContentContainer::Add(u64 offset, u64 size, ContentSource source)
|
2020-01-23 08:47:49 -07:00
|
|
|
{
|
|
|
|
if (size != 0)
|
2021-09-20 23:58:41 -06:00
|
|
|
m_contents.emplace(offset, size, std::move(source));
|
2020-01-23 08:47:49 -07:00
|
|
|
}
|
|
|
|
|
2017-08-01 11:13:19 -06:00
|
|
|
u64 DiscContentContainer::CheckSizeAndAdd(u64 offset, const std::string& path)
|
2017-08-01 09:45:46 -06:00
|
|
|
{
|
2017-08-01 11:13:19 -06:00
|
|
|
const u64 size = File::GetSize(path);
|
2021-09-21 17:37:45 -06:00
|
|
|
Add(offset, size, ContentFile{path, 0});
|
2017-08-01 11:13:19 -06:00
|
|
|
return size;
|
2017-08-01 09:45:46 -06:00
|
|
|
}
|
|
|
|
|
2017-08-01 11:13:19 -06:00
|
|
|
u64 DiscContentContainer::CheckSizeAndAdd(u64 offset, u64 max_size, const std::string& path)
|
2017-08-01 09:45:46 -06:00
|
|
|
{
|
2017-08-01 11:13:19 -06:00
|
|
|
const u64 size = std::min(File::GetSize(path), max_size);
|
2021-09-21 17:37:45 -06:00
|
|
|
Add(offset, size, ContentFile{path, 0});
|
2017-08-01 11:13:19 -06:00
|
|
|
return size;
|
2017-08-01 09:45:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DiscContentContainer::Read(u64 offset, u64 length, u8* buffer) const
|
|
|
|
{
|
|
|
|
// Determine which DiscContent the offset refers to
|
2017-08-01 11:04:33 -06:00
|
|
|
std::set<DiscContent>::const_iterator it = m_contents.upper_bound(DiscContent(offset));
|
2017-08-01 09:45:46 -06:00
|
|
|
|
|
|
|
while (it != m_contents.end() && length > 0)
|
|
|
|
{
|
2017-08-01 11:33:49 -06:00
|
|
|
// Zero fill to start of DiscContent data
|
|
|
|
PadToAddress(it->GetOffset(), &offset, &length, &buffer);
|
|
|
|
|
2021-09-13 23:09:55 -06:00
|
|
|
if (length == 0)
|
|
|
|
return true;
|
|
|
|
|
2017-08-01 09:45:46 -06:00
|
|
|
if (!it->Read(&offset, &length, &buffer))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
++it;
|
2018-03-16 10:57:36 -06:00
|
|
|
DEBUG_ASSERT(it == m_contents.end() || it->GetOffset() >= offset);
|
2017-08-01 09:45:46 -06:00
|
|
|
}
|
|
|
|
|
2017-08-01 11:33:49 -06:00
|
|
|
// Zero fill if we went beyond the last DiscContent
|
|
|
|
std::fill_n(buffer, static_cast<size_t>(length), 0);
|
|
|
|
|
2017-08-01 09:45:46 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-11 05:22:13 -06:00
|
|
|
static std::optional<PartitionType> ParsePartitionDirectoryName(const std::string& name)
|
|
|
|
{
|
|
|
|
if (name.size() < 2)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (!strcasecmp(name.c_str(), "DATA"))
|
|
|
|
return PartitionType::Game;
|
|
|
|
if (!strcasecmp(name.c_str(), "UPDATE"))
|
|
|
|
return PartitionType::Update;
|
|
|
|
if (!strcasecmp(name.c_str(), "CHANNEL"))
|
|
|
|
return PartitionType::Channel;
|
|
|
|
|
|
|
|
if (name[0] == 'P' || name[0] == 'p')
|
|
|
|
{
|
|
|
|
// e.g. "P-HA8E" (normally only used for Super Smash Bros. Brawl's VC partitions)
|
|
|
|
if (name[1] == '-' && name.size() == 6)
|
|
|
|
{
|
|
|
|
const u32 result = Common::swap32(reinterpret_cast<const u8*>(name.data() + 2));
|
|
|
|
return static_cast<PartitionType>(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// e.g. "P0"
|
|
|
|
if (std::all_of(name.cbegin() + 1, name.cend(), [](char c) { return c >= '0' && c <= '9'; }))
|
|
|
|
{
|
|
|
|
u32 result;
|
|
|
|
if (TryParse(name.substr(1), &result))
|
|
|
|
return static_cast<PartitionType>(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2017-06-11 06:45:42 -06:00
|
|
|
static bool IsDirectorySeparator(char c)
|
2017-06-07 12:32:09 -06:00
|
|
|
{
|
2017-06-11 06:45:42 -06:00
|
|
|
return c == '/'
|
2017-06-07 12:32:09 -06:00
|
|
|
#ifdef _WIN32
|
2017-06-11 06:45:42 -06:00
|
|
|
|| c == '\\'
|
2017-06-07 12:32:09 -06:00
|
|
|
#endif
|
|
|
|
;
|
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-11 06:45:42 -06:00
|
|
|
static bool PathCharactersEqual(char a, char b)
|
|
|
|
{
|
|
|
|
return a == b || (IsDirectorySeparator(a) && IsDirectorySeparator(b));
|
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
static bool PathEndsWith(const std::string& path, const std::string& suffix)
|
|
|
|
{
|
|
|
|
if (suffix.size() > path.size())
|
|
|
|
return false;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
std::string::const_iterator path_iterator = path.cend() - suffix.size();
|
|
|
|
std::string::const_iterator suffix_iterator = suffix.cbegin();
|
|
|
|
while (path_iterator != path.cend())
|
|
|
|
{
|
|
|
|
if (!PathCharactersEqual(*path_iterator, *suffix_iterator))
|
|
|
|
return false;
|
|
|
|
path_iterator++;
|
|
|
|
suffix_iterator++;
|
|
|
|
}
|
2009-12-10 02:16:10 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
return true;
|
2008-09-22 18:05:08 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-11 05:22:13 -06:00
|
|
|
static bool IsValidDirectoryBlob(const std::string& dol_path, std::string* partition_root,
|
2017-06-11 06:45:42 -06:00
|
|
|
std::string* true_root = nullptr)
|
2017-06-09 04:08:17 -06:00
|
|
|
{
|
|
|
|
if (!PathEndsWith(dol_path, "/sys/main.dol"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const size_t chars_to_remove = std::string("sys/main.dol").size();
|
2017-06-11 05:22:13 -06:00
|
|
|
*partition_root = dol_path.substr(0, dol_path.size() - chars_to_remove);
|
2017-06-09 04:08:17 -06:00
|
|
|
|
2017-06-11 05:22:13 -06:00
|
|
|
if (File::GetSize(*partition_root + "sys/boot.bin") < 0x20)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
constexpr const char* dir_separator = "/\\";
|
|
|
|
#else
|
|
|
|
constexpr char dir_separator = '/';
|
|
|
|
#endif
|
2017-06-11 06:45:42 -06:00
|
|
|
if (true_root)
|
|
|
|
{
|
|
|
|
*true_root =
|
|
|
|
dol_path.substr(0, dol_path.find_last_of(dir_separator, partition_root->size() - 2) + 1);
|
|
|
|
}
|
2017-06-11 05:22:13 -06:00
|
|
|
|
|
|
|
return true;
|
2017-06-09 04:08:17 -06:00
|
|
|
}
|
|
|
|
|
2017-06-11 06:45:42 -06:00
|
|
|
static bool ExistsAndIsValidDirectoryBlob(const std::string& dol_path)
|
|
|
|
{
|
|
|
|
std::string partition_root;
|
|
|
|
return File::Exists(dol_path) && IsValidDirectoryBlob(dol_path, &partition_root);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsInFilesDirectory(const std::string& path)
|
|
|
|
{
|
|
|
|
size_t files_pos = std::string::npos;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
files_pos = path.rfind("files", files_pos);
|
|
|
|
if (files_pos == std::string::npos)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const size_t slash_before_pos = files_pos - 1;
|
|
|
|
const size_t slash_after_pos = files_pos + 5;
|
|
|
|
if ((files_pos == 0 || IsDirectorySeparator(path[slash_before_pos])) &&
|
|
|
|
(slash_after_pos == path.size() || (IsDirectorySeparator(path[slash_after_pos]))) &&
|
|
|
|
ExistsAndIsValidDirectoryBlob(path.substr(0, files_pos) + "sys/main.dol"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
--files_pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsMainDolForNonGamePartition(const std::string& path)
|
|
|
|
{
|
|
|
|
std::string partition_root, true_root;
|
|
|
|
if (!IsValidDirectoryBlob(path, &partition_root, &true_root))
|
|
|
|
return false; // This is not a /sys/main.dol
|
|
|
|
|
|
|
|
std::string partition_directory_name = partition_root.substr(true_root.size());
|
|
|
|
partition_directory_name.pop_back(); // Remove trailing slash
|
|
|
|
const std::optional<PartitionType> partition_type =
|
|
|
|
ParsePartitionDirectoryName(partition_directory_name);
|
|
|
|
if (!partition_type || *partition_type == PartitionType::Game)
|
|
|
|
return false; // volume_path is the game partition's /sys/main.dol
|
|
|
|
|
|
|
|
const File::FSTEntry true_root_entry = File::ScanDirectoryTree(true_root, false);
|
|
|
|
for (const File::FSTEntry& entry : true_root_entry.children)
|
|
|
|
{
|
|
|
|
if (entry.isDirectory &&
|
|
|
|
ParsePartitionDirectoryName(entry.virtualName) == PartitionType::Game &&
|
|
|
|
ExistsAndIsValidDirectoryBlob(entry.physicalName + "/sys/main.dol"))
|
|
|
|
{
|
|
|
|
return true; // volume_path is the /sys/main.dol for a non-game partition
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false; // volume_path is the game partition's /sys/main.dol
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShouldHideFromGameList(const std::string& volume_path)
|
|
|
|
{
|
|
|
|
return IsInFilesDirectory(volume_path) || IsMainDolForNonGamePartition(volume_path);
|
|
|
|
}
|
|
|
|
|
2017-06-09 04:45:34 -06:00
|
|
|
std::unique_ptr<DirectoryBlobReader> DirectoryBlobReader::Create(const std::string& dol_path)
|
2008-09-22 18:05:08 -06:00
|
|
|
{
|
2017-06-11 05:22:13 -06:00
|
|
|
std::string partition_root, true_root;
|
|
|
|
if (!IsValidDirectoryBlob(dol_path, &partition_root, &true_root))
|
2017-06-07 12:32:09 -06:00
|
|
|
return nullptr;
|
|
|
|
|
2017-06-11 05:22:13 -06:00
|
|
|
return std::unique_ptr<DirectoryBlobReader>(new DirectoryBlobReader(partition_root, true_root));
|
2008-09-23 01:49:32 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2021-09-21 20:51:16 -06:00
|
|
|
std::unique_ptr<DirectoryBlobReader> DirectoryBlobReader::Create(
|
|
|
|
std::unique_ptr<DiscIO::VolumeDisc> volume,
|
|
|
|
const std::function<void(std::vector<FSTBuilderNode>* fst_nodes, FSTBuilderNode* dol_node)>&
|
|
|
|
fst_callback)
|
2021-09-21 20:35:27 -06:00
|
|
|
{
|
|
|
|
if (!volume)
|
|
|
|
return nullptr;
|
|
|
|
|
2021-09-21 20:51:16 -06:00
|
|
|
return std::unique_ptr<DirectoryBlobReader>(
|
|
|
|
new DirectoryBlobReader(std::move(volume), fst_callback));
|
2021-09-21 20:35:27 -06:00
|
|
|
}
|
|
|
|
|
2017-06-11 05:22:13 -06:00
|
|
|
DirectoryBlobReader::DirectoryBlobReader(const std::string& game_partition_root,
|
|
|
|
const std::string& true_root)
|
2020-01-23 08:47:49 -07:00
|
|
|
: m_encryption_cache(this)
|
2009-02-22 06:59:06 -07:00
|
|
|
{
|
2017-06-10 12:11:58 -06:00
|
|
|
DirectoryBlobPartition game_partition(game_partition_root, {});
|
2017-06-10 09:51:22 -06:00
|
|
|
m_is_wii = game_partition.IsWii();
|
2017-06-08 10:30:05 -06:00
|
|
|
|
2017-06-10 09:51:22 -06:00
|
|
|
if (!m_is_wii)
|
2017-06-08 10:43:52 -06:00
|
|
|
{
|
2017-06-10 09:51:22 -06:00
|
|
|
m_gamecube_pseudopartition = std::move(game_partition);
|
2017-06-10 11:07:12 -06:00
|
|
|
m_data_size = m_gamecube_pseudopartition.GetDataSize();
|
2020-01-23 08:47:49 -07:00
|
|
|
m_encrypted = false;
|
2017-06-10 09:51:22 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-21 20:35:27 -06:00
|
|
|
SetNonpartitionDiscHeaderFromFile(game_partition.GetHeader(), game_partition_root);
|
|
|
|
SetWiiRegionDataFromFile(game_partition_root);
|
2017-06-10 09:51:22 -06:00
|
|
|
|
2017-06-11 05:22:13 -06:00
|
|
|
std::vector<PartitionWithType> partitions;
|
|
|
|
partitions.emplace_back(std::move(game_partition), PartitionType::Game);
|
2017-06-10 11:07:12 -06:00
|
|
|
|
2017-06-11 05:22:13 -06:00
|
|
|
std::string game_partition_directory_name = game_partition_root.substr(true_root.size());
|
|
|
|
game_partition_directory_name.pop_back(); // Remove trailing slash
|
|
|
|
if (ParsePartitionDirectoryName(game_partition_directory_name) == PartitionType::Game)
|
|
|
|
{
|
|
|
|
const File::FSTEntry true_root_entry = File::ScanDirectoryTree(true_root, false);
|
|
|
|
for (const File::FSTEntry& entry : true_root_entry.children)
|
|
|
|
{
|
|
|
|
if (entry.isDirectory)
|
|
|
|
{
|
|
|
|
const std::optional<PartitionType> type = ParsePartitionDirectoryName(entry.virtualName);
|
|
|
|
if (type && *type != PartitionType::Game)
|
|
|
|
{
|
|
|
|
partitions.emplace_back(DirectoryBlobPartition(entry.physicalName + "/", m_is_wii),
|
|
|
|
*type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-10 09:51:22 -06:00
|
|
|
|
2017-06-11 05:22:13 -06:00
|
|
|
SetPartitions(std::move(partitions));
|
2017-06-08 10:43:52 -06:00
|
|
|
}
|
2017-06-07 12:32:09 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2021-09-21 20:51:16 -06:00
|
|
|
DirectoryBlobReader::DirectoryBlobReader(
|
|
|
|
std::unique_ptr<DiscIO::VolumeDisc> volume,
|
|
|
|
const std::function<void(std::vector<FSTBuilderNode>* fst_nodes, FSTBuilderNode* dol_node)>&
|
|
|
|
fst_callback)
|
2021-09-21 20:35:27 -06:00
|
|
|
: m_encryption_cache(this), m_wrapped_volume(std::move(volume))
|
|
|
|
{
|
2021-09-21 20:51:16 -06:00
|
|
|
DirectoryBlobPartition game_partition(
|
|
|
|
m_wrapped_volume.get(), m_wrapped_volume->GetGamePartition(), std::nullopt, fst_callback);
|
2021-09-21 20:35:27 -06:00
|
|
|
m_is_wii = game_partition.IsWii();
|
|
|
|
|
|
|
|
if (!m_is_wii)
|
|
|
|
{
|
|
|
|
m_gamecube_pseudopartition = std::move(game_partition);
|
|
|
|
m_data_size = m_gamecube_pseudopartition.GetDataSize();
|
|
|
|
m_encrypted = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector<u8> header_bin(WII_NONPARTITION_DISCHEADER_SIZE);
|
|
|
|
if (!m_wrapped_volume->Read(WII_NONPARTITION_DISCHEADER_ADDRESS,
|
|
|
|
WII_NONPARTITION_DISCHEADER_SIZE, header_bin.data(),
|
|
|
|
PARTITION_NONE))
|
|
|
|
{
|
|
|
|
header_bin.clear();
|
|
|
|
}
|
|
|
|
SetNonpartitionDiscHeader(game_partition.GetHeader(), std::move(header_bin));
|
|
|
|
|
|
|
|
std::vector<u8> wii_region_data(WII_REGION_DATA_SIZE);
|
|
|
|
if (!m_wrapped_volume->Read(WII_REGION_DATA_ADDRESS, WII_REGION_DATA_SIZE,
|
|
|
|
wii_region_data.data(), PARTITION_NONE))
|
|
|
|
{
|
|
|
|
wii_region_data.clear();
|
|
|
|
}
|
|
|
|
SetWiiRegionData(wii_region_data, "volume");
|
|
|
|
|
|
|
|
std::vector<PartitionWithType> partitions;
|
|
|
|
partitions.emplace_back(std::move(game_partition), PartitionType::Game);
|
|
|
|
|
|
|
|
for (Partition partition : m_wrapped_volume->GetPartitions())
|
|
|
|
{
|
|
|
|
if (partition == m_wrapped_volume->GetGamePartition())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto type = m_wrapped_volume->GetPartitionType(partition);
|
|
|
|
if (type)
|
|
|
|
{
|
2021-09-21 20:51:16 -06:00
|
|
|
partitions.emplace_back(
|
|
|
|
DirectoryBlobPartition(m_wrapped_volume.get(), partition, m_is_wii, nullptr),
|
|
|
|
static_cast<PartitionType>(*type));
|
2021-09-21 20:35:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetPartitions(std::move(partitions));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
bool DirectoryBlobReader::Read(u64 offset, u64 length, u8* buffer)
|
2008-09-22 18:05:08 -06:00
|
|
|
{
|
2020-01-13 14:08:44 -07:00
|
|
|
if (offset + length > m_data_size)
|
|
|
|
return false;
|
|
|
|
|
2017-08-01 09:45:46 -06:00
|
|
|
return (m_is_wii ? m_nonpartition_contents : m_gamecube_pseudopartition.GetContents())
|
|
|
|
.Read(offset, length, buffer);
|
2008-09-22 18:05:08 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2020-08-29 07:12:02 -06:00
|
|
|
const DirectoryBlobPartition* DirectoryBlobReader::GetPartition(u64 offset, u64 size,
|
|
|
|
u64 partition_data_offset) const
|
2008-10-08 23:33:24 -06:00
|
|
|
{
|
2020-08-29 07:12:02 -06:00
|
|
|
const auto it = m_partitions.find(partition_data_offset);
|
|
|
|
if (it == m_partitions.end())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (offset + size > it->second.GetDataSize())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return &it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DirectoryBlobReader::SupportsReadWiiDecrypted(u64 offset, u64 size,
|
|
|
|
u64 partition_data_offset) const
|
|
|
|
{
|
|
|
|
return static_cast<bool>(GetPartition(offset, size, partition_data_offset));
|
2008-10-08 23:33:24 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2019-12-27 14:04:51 -07:00
|
|
|
bool DirectoryBlobReader::ReadWiiDecrypted(u64 offset, u64 size, u8* buffer,
|
|
|
|
u64 partition_data_offset)
|
2015-04-10 14:10:49 -06:00
|
|
|
{
|
2020-08-29 07:12:02 -06:00
|
|
|
const DirectoryBlobPartition* partition = GetPartition(offset, size, partition_data_offset);
|
|
|
|
if (!partition)
|
2020-01-13 14:08:44 -07:00
|
|
|
return false;
|
|
|
|
|
2020-08-29 07:12:02 -06:00
|
|
|
return partition->GetContents().Read(offset, size, buffer);
|
2008-10-08 23:33:24 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2020-01-23 08:47:49 -07:00
|
|
|
bool DirectoryBlobReader::EncryptPartitionData(u64 offset, u64 size, u8* buffer,
|
|
|
|
u64 partition_data_offset,
|
|
|
|
u64 partition_data_decrypted_size)
|
|
|
|
{
|
|
|
|
auto it = m_partitions.find(partition_data_offset);
|
|
|
|
if (it == m_partitions.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!m_encrypted)
|
|
|
|
return it->second.GetContents().Read(offset, size, buffer);
|
|
|
|
|
|
|
|
return m_encryption_cache.EncryptGroups(offset, size, buffer, partition_data_offset,
|
|
|
|
partition_data_decrypted_size, it->second.GetKey());
|
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
BlobType DirectoryBlobReader::GetBlobType() const
|
2015-09-26 07:24:29 -06:00
|
|
|
{
|
2015-09-27 06:01:12 -06:00
|
|
|
return BlobType::DIRECTORY;
|
2015-09-26 07:24:29 -06:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
u64 DirectoryBlobReader::GetRawSize() const
|
2008-09-22 18:05:08 -06:00
|
|
|
{
|
2015-12-03 09:29:59 -07:00
|
|
|
// Not implemented
|
2008-09-22 18:05:08 -06:00
|
|
|
return 0;
|
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
u64 DirectoryBlobReader::GetDataSize() const
|
2013-04-09 11:58:56 -06:00
|
|
|
{
|
2017-06-10 11:07:12 -06:00
|
|
|
return m_data_size;
|
2013-04-09 11:58:56 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2021-09-21 20:35:27 -06:00
|
|
|
void DirectoryBlobReader::SetNonpartitionDiscHeaderFromFile(const std::vector<u8>& partition_header,
|
|
|
|
const std::string& game_partition_root)
|
|
|
|
{
|
|
|
|
std::vector<u8> header_bin(WII_NONPARTITION_DISCHEADER_SIZE);
|
|
|
|
const size_t header_bin_bytes_read =
|
|
|
|
ReadFileToVector(game_partition_root + "disc/header.bin", &header_bin);
|
|
|
|
header_bin.resize(header_bin_bytes_read);
|
|
|
|
SetNonpartitionDiscHeader(partition_header, std::move(header_bin));
|
|
|
|
}
|
|
|
|
|
2017-06-10 12:11:58 -06:00
|
|
|
void DirectoryBlobReader::SetNonpartitionDiscHeader(const std::vector<u8>& partition_header,
|
2021-09-21 20:35:27 -06:00
|
|
|
std::vector<u8> header_bin)
|
2017-06-10 07:54:26 -06:00
|
|
|
{
|
2021-09-21 20:35:27 -06:00
|
|
|
const size_t header_bin_size = header_bin.size();
|
|
|
|
m_disc_header_nonpartition = std::move(header_bin);
|
2021-09-19 14:00:13 -06:00
|
|
|
m_disc_header_nonpartition.resize(WII_NONPARTITION_DISCHEADER_SIZE);
|
2017-06-10 07:54:26 -06:00
|
|
|
|
|
|
|
// If header.bin is missing or smaller than expected, use the content of sys/boot.bin instead
|
2021-09-21 20:35:27 -06:00
|
|
|
if (header_bin_size < m_disc_header_nonpartition.size())
|
|
|
|
{
|
|
|
|
std::copy(partition_header.data() + header_bin_size,
|
|
|
|
partition_header.data() + m_disc_header_nonpartition.size(),
|
|
|
|
m_disc_header_nonpartition.data() + header_bin_size);
|
|
|
|
}
|
2017-06-10 07:54:26 -06:00
|
|
|
|
|
|
|
// 0x60 and 0x61 are the only differences between the partition and non-partition headers
|
2021-09-21 20:35:27 -06:00
|
|
|
if (header_bin_size < 0x60)
|
2017-07-18 13:06:35 -06:00
|
|
|
m_disc_header_nonpartition[0x60] = 0;
|
2021-09-21 20:35:27 -06:00
|
|
|
if (header_bin_size < 0x61)
|
2017-07-18 13:06:35 -06:00
|
|
|
m_disc_header_nonpartition[0x61] = 0;
|
2017-06-10 07:54:26 -06:00
|
|
|
|
2020-01-23 08:47:49 -07:00
|
|
|
m_encrypted = std::all_of(m_disc_header_nonpartition.data() + 0x60,
|
|
|
|
m_disc_header_nonpartition.data() + 0x64, [](u8 x) { return x == 0; });
|
|
|
|
|
2021-09-20 23:40:59 -06:00
|
|
|
m_nonpartition_contents.AddReference(WII_NONPARTITION_DISCHEADER_ADDRESS,
|
|
|
|
m_disc_header_nonpartition);
|
2008-09-22 18:05:08 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2021-09-21 20:35:27 -06:00
|
|
|
void DirectoryBlobReader::SetWiiRegionDataFromFile(const std::string& game_partition_root)
|
|
|
|
{
|
|
|
|
std::vector<u8> wii_region_data(WII_REGION_DATA_SIZE);
|
|
|
|
const std::string region_bin_path = game_partition_root + "disc/region.bin";
|
|
|
|
const size_t bytes_read = ReadFileToVector(region_bin_path, &wii_region_data);
|
|
|
|
wii_region_data.resize(bytes_read);
|
|
|
|
SetWiiRegionData(wii_region_data, region_bin_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectoryBlobReader::SetWiiRegionData(const std::vector<u8>& wii_region_data,
|
|
|
|
const std::string& log_path)
|
2017-06-10 01:53:36 -06:00
|
|
|
{
|
|
|
|
m_wii_region_data.resize(0x10, 0x00);
|
2021-09-21 20:35:27 -06:00
|
|
|
m_wii_region_data.resize(WII_REGION_DATA_SIZE, 0x80);
|
2017-06-10 01:53:36 -06:00
|
|
|
Write32(INVALID_REGION, 0, &m_wii_region_data);
|
|
|
|
|
2021-09-21 20:35:27 -06:00
|
|
|
std::copy_n(wii_region_data.begin(),
|
|
|
|
std::min<size_t>(wii_region_data.size(), WII_REGION_DATA_SIZE),
|
|
|
|
m_wii_region_data.begin());
|
|
|
|
|
|
|
|
if (wii_region_data.size() < 0x4)
|
|
|
|
ERROR_LOG_FMT(DISCIO, "Couldn't read region from {}", log_path);
|
|
|
|
else if (wii_region_data.size() < 0x20)
|
|
|
|
ERROR_LOG_FMT(DISCIO, "Couldn't read age ratings from {}", log_path);
|
2017-06-10 02:37:06 -06:00
|
|
|
|
2021-09-20 23:40:59 -06:00
|
|
|
m_nonpartition_contents.AddReference(WII_REGION_DATA_ADDRESS, m_wii_region_data);
|
2017-06-10 01:53:36 -06:00
|
|
|
}
|
|
|
|
|
2017-06-11 05:22:13 -06:00
|
|
|
void DirectoryBlobReader::SetPartitions(std::vector<PartitionWithType>&& partitions)
|
|
|
|
{
|
|
|
|
std::sort(partitions.begin(), partitions.end(),
|
|
|
|
[](const PartitionWithType& lhs, const PartitionWithType& rhs) {
|
|
|
|
if (lhs.type == rhs.type)
|
|
|
|
return lhs.partition.GetRootDirectory() < rhs.partition.GetRootDirectory();
|
|
|
|
|
|
|
|
// Ascending sort by partition type, except Update (1) comes before before Game (0)
|
|
|
|
return (lhs.type > PartitionType::Update || rhs.type > PartitionType::Update) ?
|
|
|
|
lhs.type < rhs.type :
|
|
|
|
lhs.type > rhs.type;
|
|
|
|
});
|
|
|
|
|
|
|
|
u32 subtable_1_size = 0;
|
|
|
|
while (subtable_1_size < partitions.size() && subtable_1_size < 3 &&
|
|
|
|
partitions[subtable_1_size].type <= PartitionType::Channel)
|
|
|
|
{
|
|
|
|
++subtable_1_size;
|
|
|
|
}
|
|
|
|
const u32 subtable_2_size = static_cast<u32>(partitions.size() - subtable_1_size);
|
|
|
|
|
|
|
|
constexpr u32 PARTITION_TABLE_ADDRESS = 0x40000;
|
|
|
|
constexpr u32 PARTITION_SUBTABLE1_OFFSET = 0x20;
|
|
|
|
constexpr u32 PARTITION_SUBTABLE2_OFFSET = 0x40;
|
|
|
|
m_partition_table.resize(PARTITION_SUBTABLE2_OFFSET + subtable_2_size * 8);
|
|
|
|
|
|
|
|
Write32(subtable_1_size, 0x0, &m_partition_table);
|
|
|
|
Write32((PARTITION_TABLE_ADDRESS + PARTITION_SUBTABLE1_OFFSET) >> 2, 0x4, &m_partition_table);
|
|
|
|
if (subtable_2_size != 0)
|
|
|
|
{
|
|
|
|
Write32(subtable_2_size, 0x8, &m_partition_table);
|
|
|
|
Write32((PARTITION_TABLE_ADDRESS + PARTITION_SUBTABLE2_OFFSET) >> 2, 0xC, &m_partition_table);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr u64 STANDARD_UPDATE_PARTITION_ADDRESS = 0x50000;
|
|
|
|
constexpr u64 STANDARD_GAME_PARTITION_ADDRESS = 0xF800000;
|
|
|
|
u64 partition_address = STANDARD_UPDATE_PARTITION_ADDRESS;
|
|
|
|
u64 offset_in_table = PARTITION_SUBTABLE1_OFFSET;
|
|
|
|
for (size_t i = 0; i < partitions.size(); ++i)
|
|
|
|
{
|
|
|
|
if (i == subtable_1_size)
|
|
|
|
offset_in_table = PARTITION_SUBTABLE2_OFFSET;
|
|
|
|
|
|
|
|
if (partitions[i].type == PartitionType::Game)
|
|
|
|
partition_address = std::max(partition_address, STANDARD_GAME_PARTITION_ADDRESS);
|
|
|
|
|
|
|
|
Write32(static_cast<u32>(partition_address >> 2), offset_in_table, &m_partition_table);
|
|
|
|
offset_in_table += 4;
|
|
|
|
Write32(static_cast<u32>(partitions[i].type), offset_in_table, &m_partition_table);
|
|
|
|
offset_in_table += 4;
|
|
|
|
|
2020-01-23 08:47:49 -07:00
|
|
|
SetPartitionHeader(&partitions[i].partition, partition_address);
|
2017-06-11 05:22:13 -06:00
|
|
|
|
2020-01-23 08:47:49 -07:00
|
|
|
const u64 data_size = partitions[i].partition.GetDataSize();
|
2021-09-21 18:51:10 -06:00
|
|
|
const u64 partition_data_offset = partition_address + PARTITION_DATA_OFFSET;
|
|
|
|
m_partitions.emplace(partition_data_offset, std::move(partitions[i].partition));
|
|
|
|
m_nonpartition_contents.Add(partition_data_offset, data_size,
|
|
|
|
ContentPartition{this, 0, partition_data_offset});
|
2018-05-22 14:39:52 -06:00
|
|
|
const u64 unaligned_next_partition_address = VolumeWii::EncryptedPartitionOffsetToRawOffset(
|
2020-01-23 08:47:49 -07:00
|
|
|
data_size, Partition(partition_address), PARTITION_DATA_OFFSET);
|
2017-06-11 05:22:13 -06:00
|
|
|
partition_address = Common::AlignUp(unaligned_next_partition_address, 0x10000ull);
|
|
|
|
}
|
|
|
|
m_data_size = partition_address;
|
|
|
|
|
2021-09-20 23:40:59 -06:00
|
|
|
m_nonpartition_contents.AddReference(PARTITION_TABLE_ADDRESS, m_partition_table);
|
2017-06-11 05:22:13 -06:00
|
|
|
}
|
|
|
|
|
2017-06-13 07:52:02 -06:00
|
|
|
// This function sets the header that's shortly before the start of the encrypted
|
|
|
|
// area, not the header that's right at the beginning of the encrypted area
|
2020-01-23 08:47:49 -07:00
|
|
|
void DirectoryBlobReader::SetPartitionHeader(DirectoryBlobPartition* partition,
|
2017-06-13 07:52:02 -06:00
|
|
|
u64 partition_address)
|
2017-06-10 02:38:59 -06:00
|
|
|
{
|
|
|
|
constexpr u32 TMD_OFFSET = 0x2c0;
|
2017-06-13 07:52:02 -06:00
|
|
|
constexpr u32 H3_OFFSET = 0x4000;
|
|
|
|
|
2021-09-21 20:35:27 -06:00
|
|
|
const std::optional<DiscIO::Partition>& wrapped_partition = partition->GetWrappedPartition();
|
2020-01-23 08:47:49 -07:00
|
|
|
const std::string& partition_root = partition->GetRootDirectory();
|
2017-06-13 07:52:02 -06:00
|
|
|
|
2021-09-21 20:35:27 -06:00
|
|
|
u64 ticket_size;
|
|
|
|
if (wrapped_partition)
|
|
|
|
{
|
|
|
|
const auto& ticket = m_wrapped_volume->GetTicket(*wrapped_partition).GetBytes();
|
|
|
|
auto& new_ticket = m_extra_data.emplace_back(ticket);
|
|
|
|
if (new_ticket.size() > WII_PARTITION_TICKET_SIZE)
|
|
|
|
new_ticket.resize(WII_PARTITION_TICKET_SIZE);
|
|
|
|
ticket_size = new_ticket.size();
|
|
|
|
m_nonpartition_contents.AddReference(partition_address + WII_PARTITION_TICKET_ADDRESS,
|
|
|
|
new_ticket);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ticket_size = m_nonpartition_contents.CheckSizeAndAdd(
|
|
|
|
partition_address + WII_PARTITION_TICKET_ADDRESS, WII_PARTITION_TICKET_SIZE,
|
|
|
|
partition_root + "ticket.bin");
|
|
|
|
}
|
2017-06-13 07:52:02 -06:00
|
|
|
|
2021-09-21 20:35:27 -06:00
|
|
|
u64 tmd_size;
|
|
|
|
if (wrapped_partition)
|
|
|
|
{
|
|
|
|
const auto& tmd = m_wrapped_volume->GetTMD(*wrapped_partition).GetBytes();
|
|
|
|
auto& new_tmd = m_extra_data.emplace_back(tmd);
|
|
|
|
if (new_tmd.size() > IOS::ES::MAX_TMD_SIZE)
|
|
|
|
new_tmd.resize(IOS::ES::MAX_TMD_SIZE);
|
|
|
|
tmd_size = new_tmd.size();
|
|
|
|
m_nonpartition_contents.AddReference(partition_address + TMD_OFFSET, new_tmd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmd_size = m_nonpartition_contents.CheckSizeAndAdd(
|
|
|
|
partition_address + TMD_OFFSET, IOS::ES::MAX_TMD_SIZE, partition_root + "tmd.bin");
|
|
|
|
}
|
2017-06-11 05:22:13 -06:00
|
|
|
|
2017-08-01 11:13:19 -06:00
|
|
|
const u64 cert_offset = Common::AlignUp(TMD_OFFSET + tmd_size, 0x20ull);
|
2017-06-13 07:52:02 -06:00
|
|
|
const u64 max_cert_size = H3_OFFSET - cert_offset;
|
|
|
|
|
2021-09-21 20:35:27 -06:00
|
|
|
u64 cert_size;
|
|
|
|
if (wrapped_partition)
|
|
|
|
{
|
|
|
|
const auto& cert = m_wrapped_volume->GetCertificateChain(*wrapped_partition);
|
|
|
|
auto& new_cert = m_extra_data.emplace_back(cert);
|
|
|
|
if (new_cert.size() > max_cert_size)
|
|
|
|
new_cert.resize(max_cert_size);
|
|
|
|
cert_size = new_cert.size();
|
|
|
|
m_nonpartition_contents.AddReference(partition_address + cert_offset, new_cert);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cert_size = m_nonpartition_contents.CheckSizeAndAdd(partition_address + cert_offset,
|
|
|
|
max_cert_size, partition_root + "cert.bin");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wrapped_partition)
|
|
|
|
{
|
|
|
|
if (m_wrapped_volume->IsEncryptedAndHashed())
|
|
|
|
{
|
|
|
|
const std::optional<u64> offset = m_wrapped_volume->ReadSwappedAndShifted(
|
|
|
|
wrapped_partition->offset + WII_PARTITION_H3_OFFSET_ADDRESS, PARTITION_NONE);
|
|
|
|
if (offset)
|
|
|
|
{
|
|
|
|
auto& new_h3 = m_extra_data.emplace_back(WII_PARTITION_H3_SIZE);
|
|
|
|
if (m_wrapped_volume->Read(wrapped_partition->offset + *offset, new_h3.size(),
|
|
|
|
new_h3.data(), PARTITION_NONE))
|
|
|
|
{
|
|
|
|
m_nonpartition_contents.AddReference(partition_address + H3_OFFSET, new_h3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_nonpartition_contents.CheckSizeAndAdd(partition_address + H3_OFFSET, WII_PARTITION_H3_SIZE,
|
|
|
|
partition_root + "h3.bin");
|
|
|
|
}
|
2017-06-13 07:52:02 -06:00
|
|
|
|
|
|
|
constexpr u32 PARTITION_HEADER_SIZE = 0x1c;
|
2020-01-23 08:47:49 -07:00
|
|
|
const u64 data_size = Common::AlignUp(partition->GetDataSize(), 0x7c00) / 0x7c00 * 0x8000;
|
2021-09-21 20:35:27 -06:00
|
|
|
std::vector<u8>& partition_header = m_extra_data.emplace_back(PARTITION_HEADER_SIZE);
|
2017-08-01 11:13:19 -06:00
|
|
|
Write32(static_cast<u32>(tmd_size), 0x0, &partition_header);
|
2017-06-13 07:52:02 -06:00
|
|
|
Write32(TMD_OFFSET >> 2, 0x4, &partition_header);
|
2017-08-01 11:13:19 -06:00
|
|
|
Write32(static_cast<u32>(cert_size), 0x8, &partition_header);
|
2017-06-13 07:52:02 -06:00
|
|
|
Write32(static_cast<u32>(cert_offset >> 2), 0x0C, &partition_header);
|
|
|
|
Write32(H3_OFFSET >> 2, 0x10, &partition_header);
|
2018-05-22 14:39:52 -06:00
|
|
|
Write32(PARTITION_DATA_OFFSET >> 2, 0x14, &partition_header);
|
2017-06-13 07:52:02 -06:00
|
|
|
Write32(static_cast<u32>(data_size >> 2), 0x18, &partition_header);
|
|
|
|
|
2021-09-20 23:40:59 -06:00
|
|
|
m_nonpartition_contents.AddReference(partition_address + WII_PARTITION_TICKET_SIZE,
|
|
|
|
partition_header);
|
2020-01-23 08:47:49 -07:00
|
|
|
|
|
|
|
std::vector<u8> ticket_buffer(ticket_size);
|
2021-09-19 14:00:13 -06:00
|
|
|
m_nonpartition_contents.Read(partition_address + WII_PARTITION_TICKET_ADDRESS, ticket_size,
|
2020-01-23 08:47:49 -07:00
|
|
|
ticket_buffer.data());
|
|
|
|
IOS::ES::TicketReader ticket(std::move(ticket_buffer));
|
|
|
|
if (ticket.IsValid())
|
|
|
|
partition->SetKey(ticket.GetTitleKey());
|
2017-06-10 02:38:59 -06:00
|
|
|
}
|
|
|
|
|
2021-09-21 20:18:05 -06:00
|
|
|
static void GenerateBuilderNodesFromFileSystem(const DiscIO::VolumeDisc& volume,
|
|
|
|
const DiscIO::Partition& partition,
|
|
|
|
std::vector<FSTBuilderNode>* nodes,
|
|
|
|
const FileInfo& parent_info)
|
|
|
|
{
|
|
|
|
for (const FileInfo& file_info : parent_info)
|
|
|
|
{
|
|
|
|
if (file_info.IsDirectory())
|
|
|
|
{
|
|
|
|
std::vector<FSTBuilderNode> child_nodes;
|
|
|
|
GenerateBuilderNodesFromFileSystem(volume, partition, &child_nodes, file_info);
|
|
|
|
nodes->emplace_back(FSTBuilderNode{file_info.GetName(), file_info.GetTotalChildren(),
|
|
|
|
std::move(child_nodes)});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector<BuilderContentSource> source;
|
|
|
|
source.emplace_back(BuilderContentSource{
|
|
|
|
0, file_info.GetSize(), ContentVolume{file_info.GetOffset(), &volume, partition}});
|
|
|
|
nodes->emplace_back(
|
|
|
|
FSTBuilderNode{file_info.GetName(), file_info.GetSize(), std::move(source)});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-10 09:31:03 -06:00
|
|
|
DirectoryBlobPartition::DirectoryBlobPartition(const std::string& root_directory,
|
|
|
|
std::optional<bool> is_wii)
|
2017-06-10 08:14:32 -06:00
|
|
|
: m_root_directory(root_directory)
|
|
|
|
{
|
2021-09-21 20:18:05 -06:00
|
|
|
SetDiscHeaderFromFile(m_root_directory + "sys/boot.bin");
|
|
|
|
SetDiscType(is_wii);
|
|
|
|
SetBI2FromFile(m_root_directory + "sys/bi2.bin");
|
|
|
|
const u64 dol_address = SetApploaderFromFile(m_root_directory + "sys/apploader.img");
|
|
|
|
const u64 fst_address = SetDOLFromFile(m_root_directory + "sys/main.dol", dol_address);
|
|
|
|
BuildFSTFromFolder(m_root_directory + "files/", fst_address);
|
2017-06-10 08:14:32 -06:00
|
|
|
}
|
|
|
|
|
2021-09-21 20:51:16 -06:00
|
|
|
DirectoryBlobPartition::DirectoryBlobPartition(
|
|
|
|
DiscIO::VolumeDisc* volume, const DiscIO::Partition& partition, std::optional<bool> is_wii,
|
|
|
|
const std::function<void(std::vector<FSTBuilderNode>* fst_nodes, FSTBuilderNode* dol_node)>&
|
|
|
|
fst_callback)
|
2021-09-21 20:18:05 -06:00
|
|
|
: m_wrapped_partition(partition)
|
|
|
|
{
|
|
|
|
std::vector<u8> disc_header(DISCHEADER_SIZE);
|
|
|
|
if (!volume->Read(DISCHEADER_ADDRESS, DISCHEADER_SIZE, disc_header.data(), partition))
|
|
|
|
disc_header.clear();
|
|
|
|
SetDiscHeader(std::move(disc_header));
|
|
|
|
SetDiscType(is_wii);
|
|
|
|
|
|
|
|
std::vector<u8> bi2(BI2_SIZE);
|
|
|
|
if (!volume->Read(BI2_ADDRESS, BI2_SIZE, bi2.data(), partition))
|
|
|
|
bi2.clear();
|
|
|
|
SetBI2(std::move(bi2));
|
|
|
|
|
|
|
|
std::vector<u8> apploader;
|
|
|
|
const auto apploader_size = GetApploaderSize(*volume, partition);
|
|
|
|
if (apploader_size)
|
|
|
|
{
|
|
|
|
apploader.resize(*apploader_size);
|
|
|
|
if (!volume->Read(APPLOADER_ADDRESS, *apploader_size, apploader.data(), partition))
|
|
|
|
apploader.clear();
|
|
|
|
}
|
|
|
|
const u64 new_dol_address = SetApploader(apploader, "apploader");
|
|
|
|
|
|
|
|
FSTBuilderNode dol_node{"main.dol", 0, {}};
|
|
|
|
const auto dol_offset = GetBootDOLOffset(*volume, partition);
|
|
|
|
if (dol_offset)
|
|
|
|
{
|
|
|
|
const auto dol_size = GetBootDOLSize(*volume, partition, *dol_offset);
|
|
|
|
if (dol_size)
|
|
|
|
{
|
|
|
|
std::vector<BuilderContentSource> dol_contents;
|
|
|
|
dol_contents.emplace_back(
|
|
|
|
BuilderContentSource{0, *dol_size, ContentVolume{*dol_offset, volume, partition}});
|
|
|
|
dol_node.m_size = *dol_size;
|
|
|
|
dol_node.m_content = std::move(dol_contents);
|
|
|
|
}
|
|
|
|
}
|
2021-09-21 20:51:16 -06:00
|
|
|
|
|
|
|
std::vector<FSTBuilderNode> nodes;
|
2021-09-21 20:18:05 -06:00
|
|
|
|
|
|
|
const FileSystem* fs = volume->GetFileSystem(partition);
|
2021-09-21 20:51:16 -06:00
|
|
|
if (fs && fs->IsValid())
|
|
|
|
GenerateBuilderNodesFromFileSystem(*volume, partition, &nodes, fs->GetRoot());
|
2021-09-21 20:18:05 -06:00
|
|
|
|
2021-09-21 20:51:16 -06:00
|
|
|
if (fst_callback)
|
|
|
|
fst_callback(&nodes, &dol_node);
|
|
|
|
|
|
|
|
const u64 new_fst_address = SetDOL(std::move(dol_node), new_dol_address);
|
2021-09-21 20:18:05 -06:00
|
|
|
BuildFST(std::move(nodes), new_fst_address);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectoryBlobPartition::SetDiscHeaderFromFile(const std::string& boot_bin_path)
|
2017-06-10 08:14:32 -06:00
|
|
|
{
|
2017-07-18 13:06:35 -06:00
|
|
|
m_disc_header.resize(DISCHEADER_SIZE);
|
|
|
|
if (ReadFileToVector(boot_bin_path, &m_disc_header) < 0x20)
|
2020-10-21 12:58:08 -06:00
|
|
|
ERROR_LOG_FMT(DISCIO, "{} doesn't exist or is too small", boot_bin_path);
|
2017-06-10 08:14:32 -06:00
|
|
|
|
2021-09-20 23:40:59 -06:00
|
|
|
m_contents.AddReference(DISCHEADER_ADDRESS, m_disc_header);
|
2021-09-21 20:18:05 -06:00
|
|
|
}
|
2017-06-10 08:14:32 -06:00
|
|
|
|
2021-09-21 20:18:05 -06:00
|
|
|
void DirectoryBlobPartition::SetDiscHeader(std::vector<u8> boot_bin)
|
|
|
|
{
|
|
|
|
m_disc_header = std::move(boot_bin);
|
|
|
|
m_disc_header.resize(DISCHEADER_SIZE);
|
|
|
|
m_contents.AddReference(DISCHEADER_ADDRESS, m_disc_header);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectoryBlobPartition::SetDiscType(std::optional<bool> is_wii)
|
|
|
|
{
|
2017-06-10 09:31:03 -06:00
|
|
|
if (is_wii.has_value())
|
|
|
|
{
|
|
|
|
m_is_wii = *is_wii;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-09 13:06:57 -07:00
|
|
|
m_is_wii = Common::swap32(&m_disc_header[0x18]) == WII_DISC_MAGIC;
|
|
|
|
const bool is_gc = Common::swap32(&m_disc_header[0x1c]) == GAMECUBE_DISC_MAGIC;
|
2017-06-10 09:31:03 -06:00
|
|
|
if (m_is_wii == is_gc)
|
2021-09-21 20:18:05 -06:00
|
|
|
{
|
|
|
|
ERROR_LOG_FMT(DISCIO, "Couldn't detect disc type based on disc header; assuming {}",
|
|
|
|
m_is_wii ? "Wii" : "GameCube");
|
|
|
|
}
|
2017-06-10 09:31:03 -06:00
|
|
|
}
|
2017-06-10 08:14:32 -06:00
|
|
|
|
|
|
|
m_address_shift = m_is_wii ? 2 : 0;
|
|
|
|
}
|
|
|
|
|
2021-09-21 20:18:05 -06:00
|
|
|
void DirectoryBlobPartition::SetBI2FromFile(const std::string& bi2_path)
|
2017-06-10 08:14:32 -06:00
|
|
|
{
|
2017-07-18 07:53:04 -06:00
|
|
|
m_bi2.resize(BI2_SIZE);
|
|
|
|
|
|
|
|
if (!m_is_wii)
|
|
|
|
Write32(INVALID_REGION, 0x18, &m_bi2);
|
|
|
|
|
|
|
|
const size_t bytes_read = ReadFileToVector(bi2_path, &m_bi2);
|
|
|
|
if (!m_is_wii && bytes_read < 0x1C)
|
2020-10-21 12:58:08 -06:00
|
|
|
ERROR_LOG_FMT(DISCIO, "Couldn't read region from {}", bi2_path);
|
2017-07-18 07:53:04 -06:00
|
|
|
|
2021-09-20 23:40:59 -06:00
|
|
|
m_contents.AddReference(BI2_ADDRESS, m_bi2);
|
2017-06-10 08:14:32 -06:00
|
|
|
}
|
|
|
|
|
2021-09-21 20:18:05 -06:00
|
|
|
void DirectoryBlobPartition::SetBI2(std::vector<u8> bi2)
|
2009-12-10 02:16:10 -07:00
|
|
|
{
|
2021-09-21 20:18:05 -06:00
|
|
|
const size_t bi2_size = bi2.size();
|
|
|
|
m_bi2 = std::move(bi2);
|
|
|
|
m_bi2.resize(BI2_SIZE);
|
2017-06-10 03:54:41 -06:00
|
|
|
|
2021-09-21 20:18:05 -06:00
|
|
|
if (!m_is_wii && bi2_size < 0x1C)
|
|
|
|
Write32(INVALID_REGION, 0x18, &m_bi2);
|
|
|
|
|
|
|
|
m_contents.AddReference(BI2_ADDRESS, m_bi2);
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 DirectoryBlobPartition::SetApploaderFromFile(const std::string& path)
|
|
|
|
{
|
2017-06-10 03:54:41 -06:00
|
|
|
File::IOFile file(path, "rb");
|
2021-09-21 20:18:05 -06:00
|
|
|
std::vector<u8> apploader(file.GetSize());
|
|
|
|
file.ReadBytes(apploader.data(), apploader.size());
|
|
|
|
return SetApploader(std::move(apploader), path);
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 DirectoryBlobPartition::SetApploader(std::vector<u8> apploader, const std::string& log_path)
|
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
m_apploader = std::move(apploader);
|
|
|
|
if (m_apploader.size() < 0x20)
|
2017-06-10 02:27:46 -06:00
|
|
|
{
|
2021-09-21 20:18:05 -06:00
|
|
|
ERROR_LOG_FMT(DISCIO, "{} couldn't be accessed or is too small", log_path);
|
2017-06-10 02:27:46 -06:00
|
|
|
}
|
|
|
|
else
|
2009-12-10 02:16:10 -07:00
|
|
|
{
|
2017-06-10 03:54:41 -06:00
|
|
|
const size_t apploader_size = 0x20 + Common::swap32(*(u32*)&m_apploader[0x14]) +
|
|
|
|
Common::swap32(*(u32*)&m_apploader[0x18]);
|
|
|
|
if (apploader_size != m_apploader.size())
|
2021-09-21 20:18:05 -06:00
|
|
|
ERROR_LOG_FMT(DISCIO, "{} is the wrong size... Is it really an apploader?", log_path);
|
2017-06-10 02:27:46 -06:00
|
|
|
else
|
2017-06-10 03:54:41 -06:00
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
m_apploader.resize(0x20);
|
|
|
|
// Make sure BS2 HLE doesn't try to run the apploader
|
|
|
|
Write32(static_cast<u32>(-1), 0x10, &m_apploader);
|
2009-12-10 02:16:10 -07:00
|
|
|
}
|
2017-06-10 02:27:46 -06:00
|
|
|
|
2021-09-20 23:40:59 -06:00
|
|
|
m_contents.AddReference(APPLOADER_ADDRESS, m_apploader);
|
2017-06-10 03:24:11 -06:00
|
|
|
|
|
|
|
// Return DOL address, 32 byte aligned (plus 32 byte padding)
|
|
|
|
return Common::AlignUp(APPLOADER_ADDRESS + m_apploader.size() + 0x20, 0x20ull);
|
2009-12-10 02:16:10 -07:00
|
|
|
}
|
|
|
|
|
2021-09-21 20:18:05 -06:00
|
|
|
u64 DirectoryBlobPartition::SetDOLFromFile(const std::string& path, u64 dol_address)
|
2009-12-10 02:16:10 -07:00
|
|
|
{
|
2021-09-21 20:18:05 -06:00
|
|
|
const u64 dol_size = m_contents.CheckSizeAndAdd(dol_address, path);
|
2009-12-10 02:16:10 -07:00
|
|
|
|
2017-07-18 13:06:35 -06:00
|
|
|
Write32(static_cast<u32>(dol_address >> m_address_shift), 0x0420, &m_disc_header);
|
2017-06-07 12:32:09 -06:00
|
|
|
|
2017-06-10 03:24:11 -06:00
|
|
|
// Return FST address, 32 byte aligned (plus 32 byte padding)
|
2017-08-01 11:13:19 -06:00
|
|
|
return Common::AlignUp(dol_address + dol_size + 0x20, 0x20ull);
|
2009-12-10 02:16:10 -07:00
|
|
|
}
|
|
|
|
|
2021-09-21 20:18:05 -06:00
|
|
|
u64 DirectoryBlobPartition::SetDOL(FSTBuilderNode dol_node, u64 dol_address)
|
|
|
|
{
|
|
|
|
for (auto& content : dol_node.GetFileContent())
|
|
|
|
m_contents.Add(dol_address + content.m_offset, content.m_size, std::move(content.m_source));
|
|
|
|
|
|
|
|
Write32(static_cast<u32>(dol_address >> m_address_shift), 0x0420, &m_disc_header);
|
|
|
|
|
|
|
|
// Return FST address, 32 byte aligned (plus 32 byte padding)
|
|
|
|
return Common::AlignUp(dol_address + dol_node.m_size + 0x20, 0x20ull);
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::vector<FSTBuilderNode> ConvertFSTEntriesToBuilderNodes(const File::FSTEntry& parent)
|
|
|
|
{
|
|
|
|
std::vector<FSTBuilderNode> nodes;
|
|
|
|
nodes.reserve(parent.children.size());
|
|
|
|
for (const File::FSTEntry& entry : parent.children)
|
|
|
|
{
|
|
|
|
std::variant<std::vector<BuilderContentSource>, std::vector<FSTBuilderNode>> content;
|
|
|
|
if (entry.isDirectory)
|
|
|
|
{
|
|
|
|
content = ConvertFSTEntriesToBuilderNodes(entry);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
content =
|
|
|
|
std::vector<BuilderContentSource>{{0, entry.size, ContentFile{entry.physicalName, 0}}};
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes.emplace_back(FSTBuilderNode{entry.virtualName, entry.size, std::move(content)});
|
|
|
|
}
|
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectoryBlobPartition::BuildFSTFromFolder(const std::string& fst_root_path, u64 fst_address)
|
|
|
|
{
|
|
|
|
auto nodes = ConvertFSTEntriesToBuilderNodes(File::ScanDirectoryTree(fst_root_path, true));
|
|
|
|
BuildFST(std::move(nodes), fst_address);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ConvertUTF8NamesToSHIFTJIS(std::vector<FSTBuilderNode>* fst)
|
|
|
|
{
|
|
|
|
for (FSTBuilderNode& entry : *fst)
|
|
|
|
{
|
|
|
|
if (entry.IsFolder())
|
|
|
|
ConvertUTF8NamesToSHIFTJIS(&entry.GetFolderContent());
|
|
|
|
entry.m_filename = UTF8ToSHIFTJIS(entry.m_filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 ComputeNameSize(const std::vector<FSTBuilderNode>& files)
|
|
|
|
{
|
|
|
|
u32 name_size = 0;
|
|
|
|
for (const FSTBuilderNode& entry : files)
|
|
|
|
{
|
|
|
|
if (entry.IsFolder())
|
|
|
|
name_size += ComputeNameSize(entry.GetFolderContent());
|
|
|
|
name_size += static_cast<u32>(entry.m_filename.length() + 1);
|
|
|
|
}
|
|
|
|
return name_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t RecalculateFolderSizes(std::vector<FSTBuilderNode>* fst)
|
|
|
|
{
|
|
|
|
size_t size = 0;
|
|
|
|
for (FSTBuilderNode& entry : *fst)
|
|
|
|
{
|
|
|
|
++size;
|
|
|
|
if (entry.IsFile())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
entry.m_size = RecalculateFolderSizes(&entry.GetFolderContent());
|
|
|
|
size += entry.m_size;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectoryBlobPartition::BuildFST(std::vector<FSTBuilderNode> root_nodes, u64 fst_address)
|
2008-09-22 18:05:08 -06:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
m_fst_data.clear();
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2021-09-21 20:18:05 -06:00
|
|
|
ConvertUTF8NamesToSHIFTJIS(&root_nodes);
|
2017-05-01 06:08:47 -06:00
|
|
|
|
2021-09-21 20:18:05 -06:00
|
|
|
u32 name_table_size = Common::AlignUp(ComputeNameSize(root_nodes), 1ull << m_address_shift);
|
2017-05-01 06:08:47 -06:00
|
|
|
|
2021-09-21 20:18:05 -06:00
|
|
|
// 1 extra for the root entry
|
|
|
|
u64 total_entries = RecalculateFolderSizes(&root_nodes) + 1;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-10 05:02:16 -06:00
|
|
|
const u64 name_table_offset = total_entries * ENTRY_SIZE;
|
|
|
|
m_fst_data.resize(name_table_offset + name_table_size);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-07-18 13:06:35 -06:00
|
|
|
// 32 KiB aligned start of data on disc
|
2017-06-10 03:24:11 -06:00
|
|
|
u64 current_data_address = Common::AlignUp(fst_address + m_fst_data.size(), 0x8000ull);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
u32 fst_offset = 0; // Offset within FST data
|
|
|
|
u32 name_offset = 0; // Offset within name table
|
|
|
|
u32 root_offset = 0; // Offset of root of FST
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2008-09-22 18:05:08 -06:00
|
|
|
// write root entry
|
2017-03-27 18:53:42 -06:00
|
|
|
WriteEntryData(&fst_offset, DIRECTORY_ENTRY, 0, 0, total_entries, m_address_shift);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2021-09-21 20:18:05 -06:00
|
|
|
WriteDirectory(&root_nodes, &fst_offset, &name_offset, ¤t_data_address, root_offset,
|
2017-06-10 05:02:16 -06:00
|
|
|
name_table_offset);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-03-27 18:52:40 -06:00
|
|
|
// overflow check, compare the aligned name offset with the aligned name table size
|
2018-03-14 18:34:35 -06:00
|
|
|
ASSERT(Common::AlignUp(name_offset, 1ull << m_address_shift) == name_table_size);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2008-09-22 18:05:08 -06:00
|
|
|
// write FST size and location
|
2017-07-18 13:06:35 -06:00
|
|
|
Write32((u32)(fst_address >> m_address_shift), 0x0424, &m_disc_header);
|
|
|
|
Write32((u32)(m_fst_data.size() >> m_address_shift), 0x0428, &m_disc_header);
|
|
|
|
Write32((u32)(m_fst_data.size() >> m_address_shift), 0x042c, &m_disc_header);
|
2017-06-10 02:37:06 -06:00
|
|
|
|
2021-09-20 23:40:59 -06:00
|
|
|
m_contents.AddReference(fst_address, m_fst_data);
|
2017-06-10 11:07:12 -06:00
|
|
|
|
|
|
|
m_data_size = current_data_address;
|
2008-09-22 18:05:08 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-10 07:54:26 -06:00
|
|
|
void DirectoryBlobPartition::WriteEntryData(u32* entry_offset, u8 type, u32 name_offset,
|
|
|
|
u64 data_offset, u64 length, u32 address_shift)
|
2008-09-22 18:05:08 -06:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
m_fst_data[(*entry_offset)++] = type;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
m_fst_data[(*entry_offset)++] = (name_offset >> 16) & 0xff;
|
|
|
|
m_fst_data[(*entry_offset)++] = (name_offset >> 8) & 0xff;
|
|
|
|
m_fst_data[(*entry_offset)++] = (name_offset)&0xff;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-03-27 18:53:42 -06:00
|
|
|
Write32((u32)(data_offset >> address_shift), *entry_offset, &m_fst_data);
|
2016-12-26 03:45:22 -07:00
|
|
|
*entry_offset += 4;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
Write32((u32)length, *entry_offset, &m_fst_data);
|
|
|
|
*entry_offset += 4;
|
2008-09-22 18:05:08 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-10 07:54:26 -06:00
|
|
|
void DirectoryBlobPartition::WriteEntryName(u32* name_offset, const std::string& name,
|
|
|
|
u64 name_table_offset)
|
2008-09-22 18:05:08 -06:00
|
|
|
{
|
2017-06-10 05:02:16 -06:00
|
|
|
strncpy((char*)&m_fst_data[*name_offset + name_table_offset], name.c_str(), name.length() + 1);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
*name_offset += (u32)(name.length() + 1);
|
2008-09-22 18:05:08 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2021-09-21 20:18:05 -06:00
|
|
|
void DirectoryBlobPartition::WriteDirectory(std::vector<FSTBuilderNode>* parent_entries,
|
|
|
|
u32* fst_offset, u32* name_offset, u64* data_offset,
|
2017-06-10 07:54:26 -06:00
|
|
|
u32 parent_entry_index, u64 name_table_offset)
|
2008-09-22 18:05:08 -06:00
|
|
|
{
|
2021-09-21 20:18:05 -06:00
|
|
|
std::vector<FSTBuilderNode>& sorted_entries = *parent_entries;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-01-24 10:18:26 -07:00
|
|
|
// Sort for determinism
|
|
|
|
std::sort(sorted_entries.begin(), sorted_entries.end(),
|
2021-09-21 20:18:05 -06:00
|
|
|
[](const FSTBuilderNode& one, const FSTBuilderNode& two) {
|
2022-01-16 17:38:11 -07:00
|
|
|
std::string one_upper = one.m_filename;
|
|
|
|
std::string two_upper = two.m_filename;
|
|
|
|
Common::ToUpper(&one_upper);
|
|
|
|
Common::ToUpper(&two_upper);
|
2021-09-21 20:18:05 -06:00
|
|
|
return one_upper == two_upper ? one.m_filename < two.m_filename :
|
2017-04-15 11:53:53 -06:00
|
|
|
one_upper < two_upper;
|
2017-01-24 10:18:26 -07:00
|
|
|
});
|
2016-12-25 14:35:38 -07:00
|
|
|
|
2021-09-21 20:18:05 -06:00
|
|
|
for (FSTBuilderNode& entry : sorted_entries)
|
2016-12-25 14:35:38 -07:00
|
|
|
{
|
2021-09-21 20:18:05 -06:00
|
|
|
if (entry.IsFolder())
|
2008-09-22 18:05:08 -06:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
u32 entry_index = *fst_offset / ENTRY_SIZE;
|
2017-03-27 18:53:42 -06:00
|
|
|
WriteEntryData(fst_offset, DIRECTORY_ENTRY, *name_offset, parent_entry_index,
|
2021-09-21 20:18:05 -06:00
|
|
|
entry_index + entry.m_size + 1, 0);
|
|
|
|
WriteEntryName(name_offset, entry.m_filename, name_table_offset);
|
2016-12-25 14:35:38 -07:00
|
|
|
|
2021-09-21 20:18:05 -06:00
|
|
|
auto& child_nodes = entry.GetFolderContent();
|
|
|
|
WriteDirectory(&child_nodes, fst_offset, name_offset, data_offset, entry_index,
|
|
|
|
name_table_offset);
|
2008-09-22 18:05:08 -06:00
|
|
|
}
|
2016-12-25 14:35:38 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// put entry in FST
|
2021-09-21 20:18:05 -06:00
|
|
|
WriteEntryData(fst_offset, FILE_ENTRY, *name_offset, *data_offset, entry.m_size,
|
2017-03-27 18:53:42 -06:00
|
|
|
m_address_shift);
|
2021-09-21 20:18:05 -06:00
|
|
|
WriteEntryName(name_offset, entry.m_filename, name_table_offset);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-08 08:07:01 -06:00
|
|
|
// write entry to virtual disc
|
2021-09-21 20:18:05 -06:00
|
|
|
auto& contents = entry.GetFileContent();
|
|
|
|
for (BuilderContentSource& content : contents)
|
|
|
|
{
|
|
|
|
m_contents.Add(*data_offset + content.m_offset, content.m_size,
|
|
|
|
std::move(content.m_source));
|
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-09 10:00:25 -06:00
|
|
|
// 32 KiB aligned - many games are fine with less alignment, but not all
|
2021-09-21 20:18:05 -06:00
|
|
|
*data_offset = Common::AlignUp(*data_offset + entry.m_size, 0x8000ull);
|
2016-12-25 14:35:38 -07:00
|
|
|
}
|
2008-09-22 18:05:08 -06:00
|
|
|
}
|
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-10 00:46:21 -06:00
|
|
|
static size_t ReadFileToVector(const std::string& path, std::vector<u8>* vector)
|
|
|
|
{
|
|
|
|
File::IOFile file(path, "rb");
|
|
|
|
size_t bytes_read;
|
|
|
|
file.ReadArray<u8>(vector->data(), std::min<u64>(file.GetSize(), vector->size()), &bytes_read);
|
|
|
|
return bytes_read;
|
|
|
|
}
|
|
|
|
|
2017-06-10 06:42:41 -06:00
|
|
|
static void PadToAddress(u64 start_address, u64* address, u64* length, u8** buffer)
|
|
|
|
{
|
|
|
|
if (start_address > *address && *length > 0)
|
|
|
|
{
|
|
|
|
u64 padBytes = std::min(start_address - *address, *length);
|
|
|
|
memset(*buffer, 0, (size_t)padBytes);
|
|
|
|
*length -= padBytes;
|
|
|
|
*buffer += padBytes;
|
|
|
|
*address += padBytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Write32(u32 data, u32 offset, std::vector<u8>* buffer)
|
|
|
|
{
|
|
|
|
(*buffer)[offset++] = (data >> 24);
|
|
|
|
(*buffer)[offset++] = (data >> 16) & 0xff;
|
|
|
|
(*buffer)[offset++] = (data >> 8) & 0xff;
|
|
|
|
(*buffer)[offset] = data & 0xff;
|
|
|
|
}
|
2019-05-05 17:48:12 -06:00
|
|
|
} // namespace DiscIO
|