@@ -0,0 +1,202 @@
// Copyright 2021 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include "DiscIO/DiscUtils.h"

#include <algorithm>
#include <locale>
#include <optional>
#include <string>
#include <vector>

#include <fmt/format.h>

#include "Common/CommonTypes.h"
#include "DiscIO/Filesystem.h"
#include "DiscIO/Volume.h"

namespace DiscIO
{
std::string NameForPartitionType(u32 partition_type, bool include_prefix)
{
switch (partition_type)
{
case PARTITION_DATA:
return "DATA";
case PARTITION_UPDATE:
return "UPDATE";
case PARTITION_CHANNEL:
return "CHANNEL";
case PARTITION_INSTALL:
// wit doesn't recognize the name "INSTALL", so we can't use it when naming partition folders
if (!include_prefix)
return "INSTALL";
[[fallthrough]];
default:
const std::string type_as_game_id{static_cast<char>((partition_type >> 24) & 0xFF),
static_cast<char>((partition_type >> 16) & 0xFF),
static_cast<char>((partition_type >> 8) & 0xFF),
static_cast<char>(partition_type & 0xFF)};
if (std::all_of(type_as_game_id.cbegin(), type_as_game_id.cend(),
[](char c) { return std::isalnum(c, std::locale::classic()); }))
{
return include_prefix ? "P-" + type_as_game_id : type_as_game_id;
}

return fmt::format(include_prefix ? "P{}" : "{}", partition_type);
}
}

std::optional<u64> GetApploaderSize(const Volume& volume, const Partition& partition)
{
constexpr u64 header_size = 0x20;
const std::optional<u32> apploader_size = volume.ReadSwapped<u32>(0x2440 + 0x14, partition);
const std::optional<u32> trailer_size = volume.ReadSwapped<u32>(0x2440 + 0x18, partition);
if (!apploader_size || !trailer_size)
return std::nullopt;

return header_size + *apploader_size + *trailer_size;
}

std::optional<u64> GetBootDOLOffset(const Volume& volume, const Partition& partition)
{
const Platform volume_type = volume.GetVolumeType();
if (!IsDisc(volume_type))
return std::nullopt;

std::optional<u64> dol_offset = volume.ReadSwappedAndShifted(0x420, partition);

// Datel AR disc has 0x00000000 as the offset (invalid) and doesn't use it in the AppLoader.
if (dol_offset && *dol_offset == 0)
dol_offset.reset();

return dol_offset;
}

std::optional<u32> GetBootDOLSize(const Volume& volume, const Partition& partition, u64 dol_offset)
{
if (!IsDisc(volume.GetVolumeType()))
return std::nullopt;

u32 dol_size = 0;

// Iterate through the 7 code segments
for (size_t i = 0; i < 7; i++)
{
const std::optional<u32> offset = volume.ReadSwapped<u32>(dol_offset + 0x00 + i * 4, partition);
const std::optional<u32> size = volume.ReadSwapped<u32>(dol_offset + 0x90 + i * 4, partition);
if (!offset || !size)
return {};
dol_size = std::max(*offset + *size, dol_size);
}

// Iterate through the 11 data segments
for (size_t i = 0; i < 11; i++)
{
const std::optional<u32> offset = volume.ReadSwapped<u32>(dol_offset + 0x1c + i * 4, partition);
const std::optional<u32> size = volume.ReadSwapped<u32>(dol_offset + 0xac + i * 4, partition);
if (!offset || !size)
return {};
dol_size = std::max(*offset + *size, dol_size);
}

return dol_size;
}

std::optional<u64> GetFSTOffset(const Volume& volume, const Partition& partition)
{
const Platform volume_type = volume.GetVolumeType();
if (!IsDisc(volume_type))
return std::nullopt;

return volume.ReadSwappedAndShifted(0x424, partition);
}

std::optional<u64> GetFSTSize(const Volume& volume, const Partition& partition)
{
const Platform volume_type = volume.GetVolumeType();
if (!IsDisc(volume_type))
return std::nullopt;

return volume.ReadSwappedAndShifted(0x428, partition);
}

u64 GetBiggestReferencedOffset(const Volume& volume)
{
std::vector<Partition> partitions = volume.GetPartitions();

// If a partition doesn't seem to contain any valid data, skip it.
// This can happen when certain programs that create WBFS files scrub the entirety of
// the Masterpiece partitions in Super Smash Bros. Brawl without removing them from
// the partition table. https://bugs.dolphin-emu.org/issues/8733
const auto it =
std::remove_if(partitions.begin(), partitions.end(), [&](const Partition& partition) {
return volume.ReadSwapped<u32>(0x18, partition) != WII_DISC_MAGIC;
});
partitions.erase(it, partitions.end());

if (partitions.empty())
partitions.push_back(PARTITION_NONE);

return GetBiggestReferencedOffset(volume, partitions);
}

static u64 GetBiggestReferencedOffset(const Volume& volume, const FileInfo& file_info)
{
if (file_info.IsDirectory())
{
u64 biggest_offset = 0;
for (const FileInfo& f : file_info)
biggest_offset = std::max(biggest_offset, GetBiggestReferencedOffset(volume, f));
return biggest_offset;
}
else
{
return file_info.GetOffset() + file_info.GetSize();
}
}

u64 GetBiggestReferencedOffset(const Volume& volume, const std::vector<Partition>& partitions)
{
const u64 disc_header_size = volume.GetVolumeType() == Platform::GameCubeDisc ? 0x460 : 0x50000;
u64 biggest_offset = disc_header_size;
for (const Partition& partition : partitions)
{
if (partition != PARTITION_NONE)
{
const u64 offset = volume.PartitionOffsetToRawOffset(0x440, partition);
biggest_offset = std::max(biggest_offset, offset);
}

const std::optional<u64> dol_offset = GetBootDOLOffset(volume, partition);
if (dol_offset)
{
const std::optional<u64> dol_size = GetBootDOLSize(volume, partition, *dol_offset);
if (dol_size)
{
const u64 offset = volume.PartitionOffsetToRawOffset(*dol_offset + *dol_size, partition);
biggest_offset = std::max(biggest_offset, offset);
}
}

const std::optional<u64> fst_offset = GetFSTOffset(volume, partition);
const std::optional<u64> fst_size = GetFSTSize(volume, partition);
if (fst_offset && fst_size)
{
const u64 offset = volume.PartitionOffsetToRawOffset(*fst_offset + *fst_size, partition);
biggest_offset = std::max(biggest_offset, offset);
}

const FileSystem* fs = volume.GetFileSystem(partition);
if (fs)
{
const u64 offset_in_partition = GetBiggestReferencedOffset(volume, fs->GetRoot());
const u64 offset = volume.PartitionOffsetToRawOffset(offset_in_partition, partition);
biggest_offset = std::max(biggest_offset, offset);
}
}
return biggest_offset;
}

} // namespace DiscIO
@@ -0,0 +1,43 @@
// Copyright 2021 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

#include <optional>
#include <string>
#include <vector>

#include "Common/CommonTypes.h"

namespace DiscIO
{
class FileInfo;
struct Partition;
class Volume;

constexpr u64 MINI_DVD_SIZE = 1459978240; // GameCube
constexpr u64 SL_DVD_SIZE = 4699979776; // Wii retail
constexpr u64 SL_DVD_R_SIZE = 4707319808; // Wii RVT-R
constexpr u64 DL_DVD_SIZE = 8511160320; // Wii retail
constexpr u64 DL_DVD_R_SIZE = 8543666176; // Wii RVT-R

constexpr u32 GAMECUBE_DISC_MAGIC = 0xC2339F3D;
constexpr u32 WII_DISC_MAGIC = 0x5D1C9EA3;

constexpr u32 PARTITION_DATA = 0;
constexpr u32 PARTITION_UPDATE = 1;
constexpr u32 PARTITION_CHANNEL = 2; // Mario Kart Wii, Wii Fit, Wii Fit Plus, Rabbids Go Home
constexpr u32 PARTITION_INSTALL = 3; // Dragon Quest X only

std::string NameForPartitionType(u32 partition_type, bool include_prefix);

std::optional<u64> GetApploaderSize(const Volume& volume, const Partition& partition);
std::optional<u64> GetBootDOLOffset(const Volume& volume, const Partition& partition);
std::optional<u32> GetBootDOLSize(const Volume& volume, const Partition& partition, u64 dol_offset);
std::optional<u64> GetFSTOffset(const Volume& volume, const Partition& partition);
std::optional<u64> GetFSTSize(const Volume& volume, const Partition& partition);

u64 GetBiggestReferencedOffset(const Volume& volume);
u64 GetBiggestReferencedOffset(const Volume& volume, const std::vector<Partition>& partitions);
} // namespace DiscIO
@@ -19,7 +19,7 @@
#include "Common/Logging/Log.h"
#include "Common/MsgHandler.h"
#include "Common/StringUtil.h"
#include "DiscIO/DiscExtractor.h"
#include "DiscIO/DiscUtils.h"
#include "DiscIO/FileSystemGCWii.h"
#include "DiscIO/Filesystem.h"
#include "DiscIO/VolumeDisc.h"
@@ -232,9 +232,9 @@ FileSystemGCWii::FileSystemGCWii(const VolumeDisc* volume, const Partition& part
{
u8 offset_shift;
// Check if this is a GameCube or Wii disc
if (volume->ReadSwapped<u32>(0x18, partition) == u32(0x5D1C9EA3))
if (volume->ReadSwapped<u32>(0x18, partition) == WII_DISC_MAGIC)
offset_shift = 2; // Wii file system
else if (volume->ReadSwapped<u32>(0x1c, partition) == u32(0xC2339F3D))
else if (volume->ReadSwapped<u32>(0x1c, partition) == GAMECUBE_DISC_MAGIC)
offset_shift = 0; // GameCube file system
else
return; // Invalid partition (maybe someone removed its data but not its partition table entry)
@@ -20,6 +20,7 @@

#include "Core/IOS/ES/Formats.h"
#include "DiscIO/Blob.h"
#include "DiscIO/DiscUtils.h"
#include "DiscIO/Enums.h"
#include "DiscIO/VolumeDisc.h"
#include "DiscIO/VolumeGC.h"
@@ -87,14 +88,10 @@ std::map<Language, std::string> Volume::ReadWiiNames(const std::vector<char16_t>

static std::unique_ptr<VolumeDisc> CreateDisc(std::unique_ptr<BlobReader>& reader)
{
// Check for Wii
const std::optional<u32> wii_magic = reader->ReadSwapped<u32>(0x18);
if (wii_magic == u32(0x5D1C9EA3))
if (reader->ReadSwapped<u32>(0x18) == WII_DISC_MAGIC)
return std::make_unique<VolumeWii>(std::move(reader));

// Check for GC
const std::optional<u32> gc_magic = reader->ReadSwapped<u32>(0x1C);
if (gc_magic == u32(0xC2339F3D))
if (reader->ReadSwapped<u32>(0x1C) == GAMECUBE_DISC_MAGIC)
return std::make_unique<VolumeGC>(std::move(reader));

// No known magic words found
@@ -12,7 +12,7 @@
#include <mbedtls/sha1.h>

#include "Common/CommonTypes.h"
#include "DiscIO/DiscExtractor.h"
#include "DiscIO/DiscUtils.h"
#include "DiscIO/Enums.h"
#include "DiscIO/Filesystem.h"

@@ -23,6 +23,7 @@

#include "DiscIO/Blob.h"
#include "DiscIO/DiscExtractor.h"
#include "DiscIO/DiscUtils.h"
#include "DiscIO/Enums.h"
#include "DiscIO/FileSystemGCWii.h"
#include "DiscIO/Filesystem.h"
@@ -41,8 +41,8 @@
#include "Core/IOS/IOS.h"
#include "Core/IOS/IOSC.h"
#include "DiscIO/Blob.h"
#include "DiscIO/DiscExtractor.h"
#include "DiscIO/DiscScrubber.h"
#include "DiscIO/DiscUtils.h"
#include "DiscIO/Enums.h"
#include "DiscIO/Filesystem.h"
#include "DiscIO/Volume.h"
@@ -358,12 +358,6 @@ RedumpVerifier::Result RedumpVerifier::Finish(const Hashes<std::vector<u8>>& has
return {Status::Unknown, Common::GetStringT("Unknown disc")};
}

constexpr u64 MINI_DVD_SIZE = 1459978240; // GameCube
constexpr u64 SL_DVD_SIZE = 4699979776; // Wii retail
constexpr u64 SL_DVD_R_SIZE = 4707319808; // Wii RVT-R
constexpr u64 DL_DVD_SIZE = 8511160320; // Wii retail
constexpr u64 DL_DVD_R_SIZE = 8543666176; // Wii RVT-R

constexpr u64 BLOCK_SIZE = 0x20000;

VolumeVerifier::VolumeVerifier(const Volume& volume, bool redump_verification,
@@ -397,7 +391,7 @@ void VolumeVerifier::Start()
const std::vector<Partition> partitions = CheckPartitions();

if (IsDisc(m_volume.GetVolumeType()))
m_biggest_referenced_offset = GetBiggestReferencedOffset(partitions);
m_biggest_referenced_offset = GetBiggestReferencedOffset(m_volume, partitions);

CheckMisc();

@@ -529,12 +523,11 @@ bool VolumeVerifier::CheckPartition(const Partition& partition)
bool invalid_header = false;
bool blank_contents = false;
std::vector<u8> disc_header(0x80);
constexpr u32 WII_MAGIC = 0x5D1C9EA3;
if (!m_volume.Read(0, disc_header.size(), disc_header.data(), partition))
{
invalid_header = true;
}
else if (Common::swap32(disc_header.data() + 0x18) != WII_MAGIC)
else if (Common::swap32(disc_header.data() + 0x18) != WII_DISC_MAGIC)
{
for (size_t i = 0; i < disc_header.size(); i += 4)
{
@@ -822,63 +815,6 @@ void VolumeVerifier::CheckVolumeSize()
}
}

u64 VolumeVerifier::GetBiggestReferencedOffset(const std::vector<Partition>& partitions) const
{
const u64 disc_header_size = m_volume.GetVolumeType() == Platform::GameCubeDisc ? 0x460 : 0x50000;
u64 biggest_offset = disc_header_size;
for (const Partition& partition : partitions)
{
if (partition != PARTITION_NONE)
{
const u64 offset = m_volume.PartitionOffsetToRawOffset(0x440, partition);
biggest_offset = std::max(biggest_offset, offset);
}

const std::optional<u64> dol_offset = GetBootDOLOffset(m_volume, partition);
if (dol_offset)
{
const std::optional<u64> dol_size = GetBootDOLSize(m_volume, partition, *dol_offset);
if (dol_size)
{
const u64 offset = m_volume.PartitionOffsetToRawOffset(*dol_offset + *dol_size, partition);
biggest_offset = std::max(biggest_offset, offset);
}
}

const std::optional<u64> fst_offset = GetFSTOffset(m_volume, partition);
const std::optional<u64> fst_size = GetFSTSize(m_volume, partition);
if (fst_offset && fst_size)
{
const u64 offset = m_volume.PartitionOffsetToRawOffset(*fst_offset + *fst_size, partition);
biggest_offset = std::max(biggest_offset, offset);
}

const FileSystem* fs = m_volume.GetFileSystem(partition);
if (fs)
{
const u64 offset =
m_volume.PartitionOffsetToRawOffset(GetBiggestReferencedOffset(fs->GetRoot()), partition);
biggest_offset = std::max(biggest_offset, offset);
}
}
return biggest_offset;
}

u64 VolumeVerifier::GetBiggestReferencedOffset(const FileInfo& file_info) const
{
if (file_info.IsDirectory())
{
u64 biggest_offset = 0;
for (const FileInfo& f : file_info)
biggest_offset = std::max(biggest_offset, GetBiggestReferencedOffset(f));
return biggest_offset;
}
else
{
return file_info.GetOffset() + file_info.GetSize();
}
}

void VolumeVerifier::CheckMisc()
{
const std::string game_id_unencrypted = m_volume.GetGameID(PARTITION_NONE);
@@ -34,8 +34,6 @@

namespace DiscIO
{
class FileInfo;

template <typename T>
struct Hashes
{
@@ -154,8 +152,6 @@ class VolumeVerifier final
bool ShouldHaveMasterpiecePartitions() const;
bool ShouldBeDualLayer() const;
void CheckVolumeSize();
u64 GetBiggestReferencedOffset(const std::vector<Partition>& partitions) const;
u64 GetBiggestReferencedOffset(const FileInfo& file_info) const;
void CheckMisc();
void CheckSuperPaperMario();
void SetUpHashing();
@@ -30,7 +30,7 @@
#include "Common/Swap.h"

#include "DiscIO/Blob.h"
#include "DiscIO/DiscExtractor.h"
#include "DiscIO/DiscUtils.h"
#include "DiscIO/Filesystem.h"
#include "DiscIO/LaggedFibonacciGenerator.h"
#include "DiscIO/MultithreadedCompressor.h"
@@ -417,6 +417,7 @@
<ClInclude Include="DiscIO\DirectoryBlob.h" />
<ClInclude Include="DiscIO\DiscExtractor.h" />
<ClInclude Include="DiscIO\DiscScrubber.h" />
<ClInclude Include="DiscIO\DiscUtils.h" />
<ClInclude Include="DiscIO\DriveBlob.h" />
<ClInclude Include="DiscIO\Enums.h" />
<ClInclude Include="DiscIO\FileBlob.h" />
@@ -991,6 +992,7 @@
<ClCompile Include="DiscIO\DirectoryBlob.cpp" />
<ClCompile Include="DiscIO\DiscExtractor.cpp" />
<ClCompile Include="DiscIO\DiscScrubber.cpp" />
<ClCompile Include="DiscIO\DiscUtils.cpp" />
<ClCompile Include="DiscIO\DriveBlob.cpp" />
<ClCompile Include="DiscIO\Enums.cpp" />
<ClCompile Include="DiscIO\FileBlob.cpp" />
@@ -18,6 +18,7 @@
#include <future>

#include "DiscIO/DiscExtractor.h"
#include "DiscIO/DiscUtils.h"
#include "DiscIO/Filesystem.h"
#include "DiscIO/Volume.h"