Skip to content
Permalink
Browse files
Merge pull request #9178 from lioncash/disclog
DiscIO: Migrate logging over to fmt
  • Loading branch information
leoetlino committed Oct 22, 2020
2 parents 98b7814 + e93fbb7 commit 64f7a44
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 85 deletions.
@@ -206,7 +206,7 @@ static ConversionResult<OutputParameters> Compress(CompressThreadState* state,

if (retval != Z_OK)
{
ERROR_LOG(DISCIO, "Deflate failed");
ERROR_LOG_FMT(DISCIO, "Deflate failed");
return ConversionResultCode::InternalError;
}

@@ -502,9 +502,9 @@ void DirectoryBlobReader::SetWiiRegionData(const std::string& game_partition_roo
const std::string region_bin_path = game_partition_root + "disc/region.bin";
const size_t bytes_read = ReadFileToVector(region_bin_path, &m_wii_region_data);
if (bytes_read < 0x4)
ERROR_LOG(DISCIO, "Couldn't read region from %s", region_bin_path.c_str());
ERROR_LOG_FMT(DISCIO, "Couldn't read region from {}", region_bin_path);
else if (bytes_read < 0x20)
ERROR_LOG(DISCIO, "Couldn't read age ratings from %s", region_bin_path.c_str());
ERROR_LOG_FMT(DISCIO, "Couldn't read age ratings from {}", region_bin_path);

constexpr u64 WII_REGION_DATA_ADDRESS = 0x4E000;
[[maybe_unused]] constexpr u64 WII_REGION_DATA_SIZE = 0x20;
@@ -644,7 +644,7 @@ void DirectoryBlobPartition::SetDiscHeaderAndDiscType(std::optional<bool> is_wii
m_disc_header.resize(DISCHEADER_SIZE);
const std::string boot_bin_path = m_root_directory + "sys/boot.bin";
if (ReadFileToVector(boot_bin_path, &m_disc_header) < 0x20)
ERROR_LOG(DISCIO, "%s doesn't exist or is too small", boot_bin_path.c_str());
ERROR_LOG_FMT(DISCIO, "{} doesn't exist or is too small", boot_bin_path);

m_contents.Add(DISCHEADER_ADDRESS, m_disc_header);

@@ -657,7 +657,7 @@ void DirectoryBlobPartition::SetDiscHeaderAndDiscType(std::optional<bool> is_wii
m_is_wii = Common::swap32(&m_disc_header[0x18]) == 0x5d1c9ea3;
const bool is_gc = Common::swap32(&m_disc_header[0x1c]) == 0xc2339f3d;
if (m_is_wii == is_gc)
ERROR_LOG(DISCIO, "Couldn't detect disc type based on %s", boot_bin_path.c_str());
ERROR_LOG_FMT(DISCIO, "Couldn't detect disc type based on {}", boot_bin_path);
}

m_address_shift = m_is_wii ? 2 : 0;
@@ -675,7 +675,7 @@ void DirectoryBlobPartition::SetBI2()
const std::string bi2_path = m_root_directory + "sys/bi2.bin";
const size_t bytes_read = ReadFileToVector(bi2_path, &m_bi2);
if (!m_is_wii && bytes_read < 0x1C)
ERROR_LOG(DISCIO, "Couldn't read region from %s", bi2_path.c_str());
ERROR_LOG_FMT(DISCIO, "Couldn't read region from {}", bi2_path);

m_contents.Add(BI2_ADDRESS, m_bi2);
}
@@ -689,14 +689,14 @@ u64 DirectoryBlobPartition::SetApploader()
m_apploader.resize(file.GetSize());
if (m_apploader.size() < 0x20 || !file.ReadBytes(m_apploader.data(), m_apploader.size()))
{
ERROR_LOG(DISCIO, "%s couldn't be accessed or is too small", path.c_str());
ERROR_LOG_FMT(DISCIO, "{} couldn't be accessed or is too small", path);
}
else
{
const size_t apploader_size = 0x20 + Common::swap32(*(u32*)&m_apploader[0x14]) +
Common::swap32(*(u32*)&m_apploader[0x18]);
if (apploader_size != m_apploader.size())
ERROR_LOG(DISCIO, "%s is the wrong size... Is it really an apploader?", path.c_str());
ERROR_LOG_FMT(DISCIO, "{} is the wrong size... Is it really an apploader?", path);
else
success = true;
}
@@ -5,7 +5,6 @@
#include "DiscIO/DiscExtractor.h"

#include <algorithm>
#include <cinttypes>
#include <locale>
#include <optional>

@@ -58,11 +57,9 @@ u64 ReadFile(const Volume& volume, const Partition& partition, const FileInfo* f

const u64 read_length = std::min(max_buffer_size, file_info->GetSize() - offset_in_file);

DEBUG_LOG(DISCIO,
"Reading %" PRIx64 " bytes at %" PRIx64 " from file %s. Offset: %" PRIx64
" Size: %" PRIx32,
read_length, offset_in_file, file_info->GetPath().c_str(), file_info->GetOffset(),
file_info->GetSize());
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());

if (!volume.Read(file_info->GetOffset() + offset_in_file, read_length, buffer, partition))
return 0;
@@ -144,14 +141,14 @@ void ExportDirectory(const Volume& volume, const Partition& partition, const Fil
if (update_progress(path))
return;

DEBUG_LOG(DISCIO, "%s", export_path.c_str());
DEBUG_LOG_FMT(DISCIO, "{}", export_path);

if (!file_info.IsDirectory())
{
if (File::Exists(export_path))
NOTICE_LOG(DISCIO, "%s already exists", export_path.c_str());
NOTICE_LOG_FMT(DISCIO, "{} already exists", export_path);
else if (!ExportFile(volume, partition, &file_info, export_path))
ERROR_LOG(DISCIO, "Could not export %s", export_path.c_str());
ERROR_LOG_FMT(DISCIO, "Could not export {}", export_path);
}
else if (recursive)
{
@@ -5,9 +5,7 @@
#include "DiscIO/DiscScrubber.h"

#include <algorithm>
#include <cinttypes>
#include <cstddef>
#include <cstdio>
#include <memory>
#include <optional>
#include <string>
@@ -16,7 +14,6 @@
#include "Common/Align.h"
#include "Common/Assert.h"
#include "Common/CommonTypes.h"
#include "Common/File.h"
#include "Common/Logging/Log.h"

#include "DiscIO/DiscExtractor.h"
@@ -59,7 +56,7 @@ void DiscScrubber::MarkAsUsed(u64 offset, u64 size)
u64 current_offset = Common::AlignDown(offset, CLUSTER_SIZE);
const u64 end_offset = offset + size;

DEBUG_LOG(DISCIO, "Marking 0x%016" PRIx64 " - 0x%016" PRIx64 " as used", offset, end_offset);
DEBUG_LOG_FMT(DISCIO, "Marking {:#018x} - {:#018x} as used", offset, end_offset);

while (current_offset < end_offset && current_offset < m_file_size)
{
@@ -165,8 +162,8 @@ bool DiscScrubber::ParsePartitionData(const Partition& partition)
const FileSystem* filesystem = m_disc->GetFileSystem(partition);
if (!filesystem)
{
ERROR_LOG(DISCIO, "Failed to read file system for the partition at 0x%" PRIx64,
partition.offset);
ERROR_LOG_FMT(DISCIO, "Failed to read file system for the partition at {:#x}",
partition.offset);
return false;
}

@@ -221,7 +218,7 @@ void DiscScrubber::ParseFileSystemData(u64 partition_data_offset, const FileInfo
{
for (const DiscIO::FileInfo& file_info : directory)
{
DEBUG_LOG(DISCIO, "Scrubbing %s", file_info.GetPath().c_str());
DEBUG_LOG_FMT(DISCIO, "Scrubbing {}", file_info.GetPath());
if (file_info.IsDirectory())
ParseFileSystemData(partition_data_offset, file_info);
else
@@ -98,7 +98,7 @@ DriveReader::DriveReader(const std::string& drive)
}
else
{
NOTICE_LOG(DISCIO, "Load from DVD backup failed or no disc in drive %s", drive.c_str());
NOTICE_LOG_FMT(DISCIO, "Load from DVD backup failed or no disc in drive {}", drive);
}
}

@@ -324,7 +324,7 @@ Country CountryCodeToCountry(u8 country_code, Platform platform, Region region,

default:
if (country_code > 'A') // Silently ignore IOS wads
WARN_LOG(DISCIO, "Unknown Country Code! %c", country_code);
WARN_LOG_FMT(DISCIO, "Unknown Country Code! {}", static_cast<char>(country_code));
return Country::Unknown;
}
}
@@ -366,7 +366,7 @@ std::string GetSysMenuVersionString(u16 title_version)
region_letter = 'K';
break;
case Region::Unknown:
WARN_LOG(DISCIO, "Unknown region for Wii Menu version %u", title_version);
WARN_LOG_FMT(DISCIO, "Unknown region for Wii Menu version {}", title_version);
break;
}

@@ -187,29 +187,29 @@ bool FileInfoGCWii::IsValid(u64 fst_size, const FileInfoGCWii& parent_directory)
{
if (GetNameOffset() >= fst_size)
{
ERROR_LOG(DISCIO, "Impossibly large name offset in file system");
ERROR_LOG_FMT(DISCIO, "Impossibly large name offset in file system");
return false;
}

if (IsDirectory())
{
if (Get(EntryProperty::FILE_OFFSET) != parent_directory.m_index)
{
ERROR_LOG(DISCIO, "Incorrect parent offset in file system");
ERROR_LOG_FMT(DISCIO, "Incorrect parent offset in file system");
return false;
}

u32 size = Get(EntryProperty::FILE_SIZE);
const u32 size = Get(EntryProperty::FILE_SIZE);

if (size <= m_index)
{
ERROR_LOG(DISCIO, "Impossibly small directory size in file system");
ERROR_LOG_FMT(DISCIO, "Impossibly small directory size in file system");
return false;
}

if (size > parent_directory.Get(EntryProperty::FILE_SIZE))
{
ERROR_LOG(DISCIO, "Impossibly large directory size in file system");
ERROR_LOG_FMT(DISCIO, "Impossibly large directory size in file system");
return false;
}

@@ -241,7 +241,7 @@ FileSystemGCWii::FileSystemGCWii(const VolumeDisc* volume, const Partition& part
return;
if (*fst_size < FST_ENTRY_SIZE)
{
ERROR_LOG(DISCIO, "File system is too small");
ERROR_LOG_FMT(DISCIO, "File system is too small");
return;
}

@@ -253,36 +253,36 @@ FileSystemGCWii::FileSystemGCWii(const VolumeDisc* volume, const Partition& part
// Without this check, Dolphin can crash by trying to allocate too much
// memory when loading a disc image with an incorrect FST size.

ERROR_LOG(DISCIO, "File system is abnormally large! Aborting loading");
ERROR_LOG_FMT(DISCIO, "File system is abnormally large! Aborting loading");
return;
}

// Read the whole FST
m_file_system_table.resize(*fst_size);
if (!volume->Read(*fst_offset, *fst_size, m_file_system_table.data(), partition))
{
ERROR_LOG(DISCIO, "Couldn't read file system table");
ERROR_LOG_FMT(DISCIO, "Couldn't read file system table");
return;
}

// Create the root object
m_root = FileInfoGCWii(m_file_system_table.data(), offset_shift);
if (!m_root.IsDirectory())
{
ERROR_LOG(DISCIO, "File system root is not a directory");
ERROR_LOG_FMT(DISCIO, "File system root is not a directory");
return;
}

if (FST_ENTRY_SIZE * m_root.GetSize() > *fst_size)
{
ERROR_LOG(DISCIO, "File system has too many entries for its size");
ERROR_LOG_FMT(DISCIO, "File system has too many entries for its size");
return;
}

// If the FST's final byte isn't 0, FileInfoGCWii::GetName() can read past the end
if (m_file_system_table[*fst_size - 1] != 0)
{
ERROR_LOG(DISCIO, "File system does not end with a null byte");
ERROR_LOG_FMT(DISCIO, "File system does not end with a null byte");
return;
}

@@ -105,8 +105,8 @@ void NANDImporter::FindSuperblock()
{
if (!memcmp(m_nand.data() + pos, "SFFS", 4))
{
u32 version = Common::swap32(&m_nand[pos + 4]);
INFO_LOG(DISCIO, "Found superblock at 0x%zx with version 0x%x", pos, version);
const u32 version = Common::swap32(&m_nand[pos + 4]);
INFO_LOG_FMT(DISCIO, "Found superblock at {:#x} with version {:#x}", pos, version);
if (superblock == 0 || version > newest_version)
{
superblock = pos;
@@ -117,8 +117,9 @@ void NANDImporter::FindSuperblock()

m_nand_fat_offset = superblock + 0xC;
m_nand_fst_offset = m_nand_fat_offset + 0x10000;
INFO_LOG(DISCIO, "Using superblock version 0x%x at position 0x%zx. FAT/FST offset: 0x%zx/0x%zx",
newest_version, superblock, m_nand_fat_offset, m_nand_fst_offset);
INFO_LOG_FMT(DISCIO,
"Using superblock version {:#x} at position {:#x}. FAT/FST offset: {:#x}/{:#x}",
newest_version, superblock, m_nand_fat_offset, m_nand_fst_offset);
}

std::string NANDImporter::GetPath(const NANDFSTEntry& entry, const std::string& parent_path)
@@ -153,21 +154,21 @@ void NANDImporter::ProcessEntry(u16 entry_number, const std::string& parent_path
else if ((entry.mode & 3) == 2)
ProcessDirectory(entry, parent_path);
else
ERROR_LOG(DISCIO, "Unknown mode: %s", FormatDebugString(entry).c_str());
ERROR_LOG_FMT(DISCIO, "Unknown mode: {}", FormatDebugString(entry));
}

void NANDImporter::ProcessDirectory(const NANDFSTEntry& entry, const std::string& parent_path)
{
m_update_callback();
INFO_LOG(DISCIO, "Path: %s", FormatDebugString(entry).c_str());
INFO_LOG_FMT(DISCIO, "Path: {}", FormatDebugString(entry));

const std::string path = GetPath(entry, parent_path);
File::CreateDir(path);

if (entry.sub != 0xffff)
ProcessEntry(entry.sub, path);

INFO_LOG(DISCIO, "Path: %s", parent_path.c_str() + m_nand_root_length);
INFO_LOG_FMT(DISCIO, "Path: {}", parent_path.data() + m_nand_root_length);
}

void NANDImporter::ProcessFile(const NANDFSTEntry& entry, const std::string& parent_path)
@@ -176,7 +177,7 @@ void NANDImporter::ProcessFile(const NANDFSTEntry& entry, const std::string& par
constexpr size_t NAND_FAT_BLOCK_SIZE = 0x4000;

m_update_callback();
INFO_LOG(DISCIO, "File: %s", FormatDebugString(entry).c_str());
INFO_LOG_FMT(DISCIO, "File: {}", FormatDebugString(entry));

const std::string path = GetPath(entry, parent_path);
File::IOFile file(path, "wb");
@@ -206,33 +207,33 @@ bool NANDImporter::ExtractCertificates(const std::string& nand_root)
std::vector<u8> tmd_bytes(tmd_file.GetSize());
if (!tmd_file.ReadBytes(tmd_bytes.data(), tmd_bytes.size()))
{
ERROR_LOG(DISCIO, "ExtractCertificates: Could not read IOS13 TMD");
ERROR_LOG_FMT(DISCIO, "ExtractCertificates: Could not read IOS13 TMD");
return false;
}

IOS::ES::TMDReader tmd(std::move(tmd_bytes));
IOS::ES::Content content_metadata;
if (!tmd.GetContent(tmd.GetBootIndex(), &content_metadata))
{
ERROR_LOG(DISCIO, "ExtractCertificates: Could not get content ID from TMD");
ERROR_LOG_FMT(DISCIO, "ExtractCertificates: Could not get content ID from TMD");
return false;
}

File::IOFile content_file(content_dir + fmt::format("{:08x}.app", content_metadata.id), "rb");
std::vector<u8> content_bytes(content_file.GetSize());
if (!content_file.ReadBytes(content_bytes.data(), content_bytes.size()))
{
ERROR_LOG(DISCIO, "ExtractCertificates: Could not read IOS13 contents");
ERROR_LOG_FMT(DISCIO, "ExtractCertificates: Could not read IOS13 contents");
return false;
}

struct PEMCertificate
{
std::string filename;
std::string_view filename;
std::array<u8, 4> search_bytes;
};

std::array<PEMCertificate, 3> certificates = {{
static constexpr std::array<PEMCertificate, 3> certificates{{
{"/clientca.pem", {{0x30, 0x82, 0x03, 0xE9}}},
{"/clientcakey.pem", {{0x30, 0x82, 0x02, 0x5D}}},
{"/rootca.pem", {{0x30, 0x82, 0x03, 0x7D}}},
@@ -246,21 +247,21 @@ bool NANDImporter::ExtractCertificates(const std::string& nand_root)

if (search_result == content_bytes.end())
{
ERROR_LOG(DISCIO, "ExtractCertificates: Could not find offset for certficate '%s'",
certificate.filename.c_str());
ERROR_LOG_FMT(DISCIO, "ExtractCertificates: Could not find offset for certficate '{}'",
certificate.filename);
return false;
}

const std::string pem_file_path = nand_root + certificate.filename;
const std::string pem_file_path = nand_root + std::string(certificate.filename);
const ptrdiff_t certificate_offset = std::distance(content_bytes.begin(), search_result);
const u16 certificate_size = Common::swap16(&content_bytes[certificate_offset - 2]);
INFO_LOG(DISCIO, "ExtractCertificates: '%s' offset: 0x%tx size: 0x%x",
certificate.filename.c_str(), certificate_offset, certificate_size);
INFO_LOG_FMT(DISCIO, "ExtractCertificates: '{}' offset: {:#x} size: {:#x}",
certificate.filename, certificate_offset, certificate_size);

File::IOFile pem_file(pem_file_path, "wb");
if (!pem_file.WriteBytes(&content_bytes[certificate_offset], certificate_size))
{
ERROR_LOG(DISCIO, "ExtractCertificates: Unable to write to file %s", pem_file_path.c_str());
ERROR_LOG_FMT(DISCIO, "ExtractCertificates: Unable to write to file {}", pem_file_path);
return false;
}
}

0 comments on commit 64f7a44

Please sign in to comment.