2017-06-17 04:37:30 -06:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-06-17 04:37:30 -06:00
|
|
|
|
|
|
|
#include "DiscIO/DiscExtractor.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2021-03-09 12:06:34 -07:00
|
|
|
#include <functional>
|
2017-06-17 04:37:30 -06:00
|
|
|
#include <optional>
|
2021-03-09 12:06:34 -07:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2020-10-14 10:34:45 -06:00
|
|
|
|
2017-06-17 04:37:30 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2018-06-09 14:15:14 -06:00
|
|
|
#include "Common/FileUtil.h"
|
2020-09-15 04:29:41 -06:00
|
|
|
#include "Common/IOFile.h"
|
2021-03-09 12:06:34 -07:00
|
|
|
#include "DiscIO/DiscUtils.h"
|
2017-06-17 04:37:30 -06:00
|
|
|
#include "DiscIO/Enums.h"
|
|
|
|
#include "DiscIO/Filesystem.h"
|
|
|
|
#include "DiscIO/Volume.h"
|
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
|
|
|
u64 ReadFile(const Volume& volume, const Partition& partition, const FileInfo* file_info,
|
|
|
|
u8* buffer, u64 max_buffer_size, u64 offset_in_file)
|
|
|
|
{
|
2017-06-17 05:26:04 -06:00
|
|
|
if (!file_info || file_info->IsDirectory() || offset_in_file >= file_info->GetSize())
|
2017-06-17 04:37:30 -06:00
|
|
|
return 0;
|
|
|
|
|
2017-06-17 05:26:04 -06:00
|
|
|
const u64 read_length = std::min(max_buffer_size, file_info->GetSize() - offset_in_file);
|
2017-06-17 04:37:30 -06:00
|
|
|
|
2020-10-21 12:58:08 -06:00
|
|
|
DEBUG_LOG_FMT(DISCIO, "Reading {:x} bytes at {:x} from file {}. Offset: {:x} Size: {:x}",
|
|
|
|
read_length, offset_in_file, file_info->GetPath(), file_info->GetOffset(),
|
|
|
|
file_info->GetSize());
|
2017-06-17 04:37:30 -06:00
|
|
|
|
2017-06-17 05:28:14 -06:00
|
|
|
if (!volume.Read(file_info->GetOffset() + offset_in_file, read_length, buffer, partition))
|
|
|
|
return 0;
|
2017-06-17 04:37:30 -06:00
|
|
|
|
|
|
|
return read_length;
|
|
|
|
}
|
|
|
|
|
2019-05-29 00:16:12 -06:00
|
|
|
u64 ReadFile(const Volume& volume, const Partition& partition, std::string_view path, u8* buffer,
|
2017-08-02 10:16:56 -06:00
|
|
|
u64 max_buffer_size, u64 offset_in_file)
|
|
|
|
{
|
|
|
|
const FileSystem* file_system = volume.GetFileSystem(partition);
|
|
|
|
if (!file_system)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return ReadFile(volume, partition, file_system->FindFileInfo(path).get(), buffer, max_buffer_size,
|
|
|
|
offset_in_file);
|
|
|
|
}
|
|
|
|
|
2017-06-17 05:37:33 -06:00
|
|
|
bool ExportData(const Volume& volume, const Partition& partition, u64 offset, u64 size,
|
2017-06-17 04:37:30 -06:00
|
|
|
const std::string& export_filename)
|
|
|
|
{
|
|
|
|
File::IOFile f(export_filename, "wb");
|
|
|
|
if (!f)
|
|
|
|
return false;
|
|
|
|
|
2017-06-17 05:37:33 -06:00
|
|
|
while (size)
|
2017-06-17 04:37:30 -06:00
|
|
|
{
|
|
|
|
// Limit read size to 128 MB
|
2017-06-17 05:37:33 -06:00
|
|
|
const size_t read_size = static_cast<size_t>(std::min<u64>(size, 0x08000000));
|
2017-06-17 04:37:30 -06:00
|
|
|
|
|
|
|
std::vector<u8> buffer(read_size);
|
|
|
|
|
2017-06-17 05:37:33 -06:00
|
|
|
if (!volume.Read(offset, read_size, buffer.data(), partition))
|
2017-06-17 05:26:04 -06:00
|
|
|
return false;
|
2017-06-17 04:37:30 -06:00
|
|
|
|
2017-06-17 05:28:14 -06:00
|
|
|
if (!f.WriteBytes(buffer.data(), read_size))
|
|
|
|
return false;
|
2017-06-17 04:37:30 -06:00
|
|
|
|
2017-06-17 05:37:33 -06:00
|
|
|
size -= read_size;
|
|
|
|
offset += read_size;
|
2017-06-17 04:37:30 -06:00
|
|
|
}
|
|
|
|
|
2017-06-17 05:26:04 -06:00
|
|
|
return true;
|
2017-06-17 04:37:30 -06:00
|
|
|
}
|
|
|
|
|
2017-06-17 05:37:33 -06:00
|
|
|
bool ExportFile(const Volume& volume, const Partition& partition, const FileInfo* file_info,
|
|
|
|
const std::string& export_filename)
|
|
|
|
{
|
|
|
|
if (!file_info || file_info->IsDirectory())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ExportData(volume, partition, file_info->GetOffset(), file_info->GetSize(),
|
|
|
|
export_filename);
|
|
|
|
}
|
|
|
|
|
2019-05-29 00:16:12 -06:00
|
|
|
bool ExportFile(const Volume& volume, const Partition& partition, std::string_view path,
|
2017-08-02 10:16:56 -06:00
|
|
|
const std::string& export_filename)
|
|
|
|
{
|
|
|
|
const FileSystem* file_system = volume.GetFileSystem(partition);
|
|
|
|
if (!file_system)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ExportFile(volume, partition, file_system->FindFileInfo(path).get(), export_filename);
|
|
|
|
}
|
|
|
|
|
2018-03-27 19:49:39 -06:00
|
|
|
void ExportDirectory(const Volume& volume, const Partition& partition, const FileInfo& directory,
|
2017-06-18 05:20:54 -06:00
|
|
|
bool recursive, const std::string& filesystem_path,
|
|
|
|
const std::string& export_folder,
|
|
|
|
const std::function<bool(const std::string& path)>& update_progress)
|
|
|
|
{
|
2020-12-03 12:59:45 -07:00
|
|
|
std::string export_root = export_folder + '/';
|
|
|
|
if (directory.IsDirectory() && !directory.IsRoot())
|
|
|
|
export_root += directory.GetName() + '/';
|
|
|
|
|
2020-10-27 16:31:05 -06:00
|
|
|
File::CreateFullPath(export_root);
|
2017-06-18 06:17:33 -06:00
|
|
|
|
2017-06-18 05:20:54 -06:00
|
|
|
for (const FileInfo& file_info : directory)
|
|
|
|
{
|
2017-06-18 06:17:33 -06:00
|
|
|
const std::string name = file_info.GetName() + (file_info.IsDirectory() ? "/" : "");
|
|
|
|
const std::string path = filesystem_path + name;
|
2020-10-27 16:31:05 -06:00
|
|
|
const std::string export_path = export_root + name;
|
2017-06-18 05:20:54 -06:00
|
|
|
|
|
|
|
if (update_progress(path))
|
|
|
|
return;
|
|
|
|
|
2020-10-21 12:58:08 -06:00
|
|
|
DEBUG_LOG_FMT(DISCIO, "{}", export_path);
|
2017-06-18 05:20:54 -06:00
|
|
|
|
|
|
|
if (!file_info.IsDirectory())
|
|
|
|
{
|
|
|
|
if (File::Exists(export_path))
|
2020-10-21 12:58:08 -06:00
|
|
|
NOTICE_LOG_FMT(DISCIO, "{} already exists", export_path);
|
2017-06-18 05:20:54 -06:00
|
|
|
else if (!ExportFile(volume, partition, &file_info, export_path))
|
2020-10-21 12:58:08 -06:00
|
|
|
ERROR_LOG_FMT(DISCIO, "Could not export {}", export_path);
|
2017-06-18 05:20:54 -06:00
|
|
|
}
|
|
|
|
else if (recursive)
|
|
|
|
{
|
2020-12-03 12:59:45 -07:00
|
|
|
ExportDirectory(volume, partition, file_info, recursive, filesystem_path, export_root,
|
|
|
|
update_progress);
|
2017-06-18 05:20:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 09:31:50 -06:00
|
|
|
bool ExportWiiUnencryptedHeader(const Volume& volume, const std::string& export_filename)
|
|
|
|
{
|
2018-03-31 06:04:13 -06:00
|
|
|
if (volume.GetVolumeType() != Platform::WiiDisc)
|
2017-06-20 09:31:50 -06:00
|
|
|
return false;
|
|
|
|
|
2021-09-19 14:00:13 -06:00
|
|
|
return ExportData(volume, PARTITION_NONE, WII_NONPARTITION_DISCHEADER_ADDRESS,
|
|
|
|
WII_NONPARTITION_DISCHEADER_SIZE, export_filename);
|
2017-06-20 09:31:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ExportWiiRegionData(const Volume& volume, const std::string& export_filename)
|
|
|
|
{
|
2018-03-31 06:04:13 -06:00
|
|
|
if (volume.GetVolumeType() != Platform::WiiDisc)
|
2017-06-20 09:31:50 -06:00
|
|
|
return false;
|
|
|
|
|
2021-09-19 14:00:13 -06:00
|
|
|
return ExportData(volume, PARTITION_NONE, WII_REGION_DATA_ADDRESS, WII_REGION_DATA_SIZE,
|
|
|
|
export_filename);
|
2017-06-20 09:31:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ExportTicket(const Volume& volume, const Partition& partition,
|
|
|
|
const std::string& export_filename)
|
|
|
|
{
|
2018-03-31 06:04:13 -06:00
|
|
|
if (volume.GetVolumeType() != Platform::WiiDisc)
|
2017-06-20 09:31:50 -06:00
|
|
|
return false;
|
|
|
|
|
2021-09-19 14:00:13 -06:00
|
|
|
return ExportData(volume, PARTITION_NONE, partition.offset + WII_PARTITION_TICKET_ADDRESS,
|
|
|
|
WII_PARTITION_TICKET_SIZE, export_filename);
|
2017-06-20 09:31:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ExportTMD(const Volume& volume, const Partition& partition, const std::string& export_filename)
|
|
|
|
{
|
2018-03-31 06:04:13 -06:00
|
|
|
if (volume.GetVolumeType() != Platform::WiiDisc)
|
2017-06-20 09:31:50 -06:00
|
|
|
return false;
|
|
|
|
|
2021-09-19 14:00:13 -06:00
|
|
|
const std::optional<u32> size =
|
|
|
|
volume.ReadSwapped<u32>(partition.offset + WII_PARTITION_TMD_SIZE_ADDRESS, PARTITION_NONE);
|
|
|
|
const std::optional<u64> offset = volume.ReadSwappedAndShifted(
|
|
|
|
partition.offset + WII_PARTITION_TMD_OFFSET_ADDRESS, PARTITION_NONE);
|
2017-06-20 09:31:50 -06:00
|
|
|
if (!size || !offset)
|
|
|
|
return false;
|
|
|
|
|
2017-08-25 09:30:17 -06:00
|
|
|
return ExportData(volume, PARTITION_NONE, partition.offset + *offset, *size, export_filename);
|
2017-06-20 09:31:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ExportCertificateChain(const Volume& volume, const Partition& partition,
|
|
|
|
const std::string& export_filename)
|
|
|
|
{
|
2018-03-31 06:04:13 -06:00
|
|
|
if (volume.GetVolumeType() != Platform::WiiDisc)
|
2017-06-20 09:31:50 -06:00
|
|
|
return false;
|
|
|
|
|
2021-09-19 14:00:13 -06:00
|
|
|
const std::optional<u32> size = volume.ReadSwapped<u32>(
|
|
|
|
partition.offset + WII_PARTITION_CERT_CHAIN_SIZE_ADDRESS, PARTITION_NONE);
|
|
|
|
const std::optional<u64> offset = volume.ReadSwappedAndShifted(
|
|
|
|
partition.offset + WII_PARTITION_CERT_CHAIN_OFFSET_ADDRESS, PARTITION_NONE);
|
2017-06-20 09:31:50 -06:00
|
|
|
if (!size || !offset)
|
|
|
|
return false;
|
|
|
|
|
2019-03-21 16:07:24 -06:00
|
|
|
return ExportData(volume, PARTITION_NONE, partition.offset + *offset, *size, export_filename);
|
2017-06-20 09:31:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ExportH3Hashes(const Volume& volume, const Partition& partition,
|
|
|
|
const std::string& export_filename)
|
|
|
|
{
|
2018-03-31 06:04:13 -06:00
|
|
|
if (volume.GetVolumeType() != Platform::WiiDisc)
|
2017-06-20 09:31:50 -06:00
|
|
|
return false;
|
|
|
|
|
2021-09-19 14:00:13 -06:00
|
|
|
const std::optional<u64> offset = volume.ReadSwappedAndShifted(
|
|
|
|
partition.offset + WII_PARTITION_H3_OFFSET_ADDRESS, PARTITION_NONE);
|
2017-06-20 09:31:50 -06:00
|
|
|
if (!offset)
|
|
|
|
return false;
|
|
|
|
|
2021-09-19 14:00:13 -06:00
|
|
|
return ExportData(volume, PARTITION_NONE, partition.offset + *offset, WII_PARTITION_H3_SIZE,
|
|
|
|
export_filename);
|
2017-06-20 09:31:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ExportHeader(const Volume& volume, const Partition& partition,
|
|
|
|
const std::string& export_filename)
|
|
|
|
{
|
|
|
|
if (!IsDisc(volume.GetVolumeType()))
|
|
|
|
return false;
|
|
|
|
|
2021-09-19 14:00:13 -06:00
|
|
|
return ExportData(volume, partition, DISCHEADER_ADDRESS, DISCHEADER_SIZE, export_filename);
|
2017-06-20 09:31:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ExportBI2Data(const Volume& volume, const Partition& partition,
|
|
|
|
const std::string& export_filename)
|
|
|
|
{
|
|
|
|
if (!IsDisc(volume.GetVolumeType()))
|
|
|
|
return false;
|
|
|
|
|
2021-09-19 14:00:13 -06:00
|
|
|
return ExportData(volume, partition, BI2_ADDRESS, BI2_SIZE, export_filename);
|
2017-06-20 09:31:50 -06:00
|
|
|
}
|
|
|
|
|
2017-06-17 04:37:30 -06:00
|
|
|
bool ExportApploader(const Volume& volume, const Partition& partition,
|
2017-06-17 08:53:55 -06:00
|
|
|
const std::string& export_filename)
|
2017-06-17 04:37:30 -06:00
|
|
|
{
|
|
|
|
if (!IsDisc(volume.GetVolumeType()))
|
|
|
|
return false;
|
|
|
|
|
2020-06-07 14:58:03 -06:00
|
|
|
const std::optional<u64> apploader_size = GetApploaderSize(volume, partition);
|
|
|
|
if (!apploader_size)
|
2017-06-17 04:37:30 -06:00
|
|
|
return false;
|
|
|
|
|
2021-09-19 14:00:13 -06:00
|
|
|
return ExportData(volume, partition, APPLOADER_ADDRESS, *apploader_size, export_filename);
|
2017-06-17 04:37:30 -06:00
|
|
|
}
|
|
|
|
|
2017-06-17 08:53:55 -06:00
|
|
|
bool ExportDOL(const Volume& volume, const Partition& partition, const std::string& export_filename)
|
2017-06-17 04:37:30 -06:00
|
|
|
{
|
|
|
|
if (!IsDisc(volume.GetVolumeType()))
|
|
|
|
return false;
|
|
|
|
|
2017-06-17 05:26:04 -06:00
|
|
|
const std::optional<u64> dol_offset = GetBootDOLOffset(volume, partition);
|
2017-06-17 04:37:30 -06:00
|
|
|
if (!dol_offset)
|
|
|
|
return false;
|
2017-06-17 05:26:04 -06:00
|
|
|
const std::optional<u32> dol_size = GetBootDOLSize(volume, partition, *dol_offset);
|
2017-06-17 04:37:30 -06:00
|
|
|
if (!dol_size)
|
|
|
|
return false;
|
|
|
|
|
2017-06-17 08:53:55 -06:00
|
|
|
return ExportData(volume, partition, *dol_offset, *dol_size, export_filename);
|
2017-06-17 04:37:30 -06:00
|
|
|
}
|
|
|
|
|
2017-06-20 09:31:50 -06:00
|
|
|
bool ExportFST(const Volume& volume, const Partition& partition, const std::string& export_filename)
|
|
|
|
{
|
|
|
|
if (!IsDisc(volume.GetVolumeType()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const std::optional<u64> fst_offset = GetFSTOffset(volume, partition);
|
|
|
|
const std::optional<u64> fst_size = GetFSTSize(volume, partition);
|
|
|
|
if (!fst_offset || !fst_size)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ExportData(volume, partition, *fst_offset, *fst_size, export_filename);
|
|
|
|
}
|
|
|
|
|
2017-06-20 07:41:17 -06:00
|
|
|
bool ExportSystemData(const Volume& volume, const Partition& partition,
|
|
|
|
const std::string& export_folder)
|
|
|
|
{
|
|
|
|
bool success = true;
|
2017-06-20 09:31:50 -06:00
|
|
|
|
|
|
|
File::CreateFullPath(export_folder + "/sys/");
|
|
|
|
success &= ExportHeader(volume, partition, export_folder + "/sys/boot.bin");
|
|
|
|
success &= ExportBI2Data(volume, partition, export_folder + "/sys/bi2.bin");
|
|
|
|
success &= ExportApploader(volume, partition, export_folder + "/sys/apploader.img");
|
|
|
|
success &= ExportDOL(volume, partition, export_folder + "/sys/main.dol");
|
|
|
|
success &= ExportFST(volume, partition, export_folder + "/sys/fst.bin");
|
|
|
|
|
2018-03-31 06:04:13 -06:00
|
|
|
if (volume.GetVolumeType() == Platform::WiiDisc)
|
2017-06-20 09:31:50 -06:00
|
|
|
{
|
|
|
|
File::CreateFullPath(export_folder + "/disc/");
|
|
|
|
success &= ExportWiiUnencryptedHeader(volume, export_folder + "/disc/header.bin");
|
|
|
|
success &= ExportWiiRegionData(volume, export_folder + "/disc/region.bin");
|
|
|
|
|
|
|
|
success &= ExportTicket(volume, partition, export_folder + "/ticket.bin");
|
|
|
|
success &= ExportTMD(volume, partition, export_folder + "/tmd.bin");
|
|
|
|
success &= ExportCertificateChain(volume, partition, export_folder + "/cert.bin");
|
2018-05-22 14:39:52 -06:00
|
|
|
if (volume.IsEncryptedAndHashed())
|
|
|
|
success &= ExportH3Hashes(volume, partition, export_folder + "/h3.bin");
|
2017-06-20 09:31:50 -06:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:41:17 -06:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2017-06-17 04:37:30 -06:00
|
|
|
} // namespace DiscIO
|