core: Refactor MakeMagic usage and remove dead code.
This commit is contained in:
parent
72eeca1f03
commit
746c2a3ae7
@ -11,21 +11,6 @@
|
||||
#include <QString>
|
||||
#include "citra_qt/util/util.h"
|
||||
#include "common/string_util.h"
|
||||
#include "core/loader/smdh.h"
|
||||
|
||||
/**
|
||||
* Gets the game icon from SMDH data.
|
||||
* @param smdh SMDH data
|
||||
* @param large If true, returns large icon (48x48), otherwise returns small icon (24x24)
|
||||
* @return QPixmap game icon
|
||||
*/
|
||||
static QPixmap GetQPixmapFromSMDH(const Loader::SMDH& smdh, bool large) {
|
||||
std::vector<u16> icon_data = smdh.GetIcon(large);
|
||||
const uchar* data = reinterpret_cast<const uchar*>(icon_data.data());
|
||||
int size = large ? 48 : 24;
|
||||
QImage icon(data, size, size, QImage::Format::Format_RGB16);
|
||||
return QPixmap::fromImage(icon);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default icon (for games without valid SMDH)
|
||||
@ -39,17 +24,6 @@ static QPixmap GetDefaultIcon(bool large) {
|
||||
return icon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the short game title from SMDH data.
|
||||
* @param smdh SMDH data
|
||||
* @param language title language
|
||||
* @return QString short title
|
||||
*/
|
||||
static QString GetQStringShortTitleFromSMDH(const Loader::SMDH& smdh,
|
||||
Loader::SMDH::TitleLanguage language) {
|
||||
return QString::fromUtf16(smdh.GetShortTitle(language).data());
|
||||
}
|
||||
|
||||
class GameListItem : public QStandardItem {
|
||||
|
||||
public:
|
||||
@ -76,22 +50,6 @@ public:
|
||||
: GameListItem() {
|
||||
setData(game_path, FullPathRole);
|
||||
setData(qulonglong(program_id), ProgramIdRole);
|
||||
|
||||
if (!Loader::IsValidSMDH(smdh_data)) {
|
||||
// SMDH is not valid, set a default icon
|
||||
setData(GetDefaultIcon(true), Qt::DecorationRole);
|
||||
return;
|
||||
}
|
||||
|
||||
Loader::SMDH smdh;
|
||||
memcpy(&smdh, smdh_data.data(), sizeof(Loader::SMDH));
|
||||
|
||||
// Get icon from SMDH
|
||||
setData(GetQPixmapFromSMDH(smdh, true), Qt::DecorationRole);
|
||||
|
||||
// Get title form SMDH
|
||||
setData(GetQStringShortTitleFromSMDH(smdh, Loader::SMDH::TitleLanguage::English),
|
||||
TitleRole);
|
||||
}
|
||||
|
||||
QVariant data(int role) const override {
|
||||
|
@ -98,3 +98,11 @@ __declspec(dllimport) void __stdcall DebugBreak(void);
|
||||
// This function might change the error code.
|
||||
// Defined in Misc.cpp.
|
||||
const char* GetLastErrorMsg();
|
||||
|
||||
namespace Common {
|
||||
|
||||
constexpr u32 MakeMagic(char a, char b, char c, char d) {
|
||||
return a | b << 8 | c << 16 | d << 24;
|
||||
}
|
||||
|
||||
} // namespace Common
|
||||
|
@ -19,7 +19,6 @@ set(SRCS
|
||||
file_sys/archive_backend.cpp
|
||||
file_sys/disk_archive.cpp
|
||||
file_sys/ivfc_archive.cpp
|
||||
file_sys/ncch_container.cpp
|
||||
file_sys/path_parser.cpp
|
||||
file_sys/savedata_archive.cpp
|
||||
file_sys/title_metadata.cpp
|
||||
@ -71,7 +70,6 @@ set(SRCS
|
||||
loader/loader.cpp
|
||||
loader/nro.cpp
|
||||
loader/nso.cpp
|
||||
loader/smdh.cpp
|
||||
tracer/recorder.cpp
|
||||
memory.cpp
|
||||
perf_stats.cpp
|
||||
@ -163,7 +161,6 @@ set(HEADERS
|
||||
loader/loader.h
|
||||
loader/nro.h
|
||||
loader/nso.h
|
||||
loader/smdh.h
|
||||
tracer/recorder.h
|
||||
tracer/citrace.h
|
||||
memory.h
|
||||
|
@ -1,423 +0,0 @@
|
||||
// Copyright 2017 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <cinttypes>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include "common/common_types.h"
|
||||
#include "common/logging/log.h"
|
||||
#include "core/core.h"
|
||||
#include "core/file_sys/ncch_container.h"
|
||||
#include "core/loader/loader.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// FileSys namespace
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
static const int kMaxSections = 8; ///< Maximum number of sections (files) in an ExeFs
|
||||
static const int kBlockSize = 0x200; ///< Size of ExeFS blocks (in bytes)
|
||||
|
||||
/**
|
||||
* Get the decompressed size of an LZSS compressed ExeFS file
|
||||
* @param buffer Buffer of compressed file
|
||||
* @param size Size of compressed buffer
|
||||
* @return Size of decompressed buffer
|
||||
*/
|
||||
static u32 LZSS_GetDecompressedSize(const u8* buffer, u32 size) {
|
||||
u32 offset_size = *(u32*)(buffer + size - 4);
|
||||
return offset_size + size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decompress ExeFS file (compressed with LZSS)
|
||||
* @param compressed Compressed buffer
|
||||
* @param compressed_size Size of compressed buffer
|
||||
* @param decompressed Decompressed buffer
|
||||
* @param decompressed_size Size of decompressed buffer
|
||||
* @return True on success, otherwise false
|
||||
*/
|
||||
static bool LZSS_Decompress(const u8* compressed, u32 compressed_size, u8* decompressed,
|
||||
u32 decompressed_size) {
|
||||
const u8* footer = compressed + compressed_size - 8;
|
||||
u32 buffer_top_and_bottom = *reinterpret_cast<const u32*>(footer);
|
||||
u32 out = decompressed_size;
|
||||
u32 index = compressed_size - ((buffer_top_and_bottom >> 24) & 0xFF);
|
||||
u32 stop_index = compressed_size - (buffer_top_and_bottom & 0xFFFFFF);
|
||||
|
||||
memset(decompressed, 0, decompressed_size);
|
||||
memcpy(decompressed, compressed, compressed_size);
|
||||
|
||||
while (index > stop_index) {
|
||||
u8 control = compressed[--index];
|
||||
|
||||
for (unsigned i = 0; i < 8; i++) {
|
||||
if (index <= stop_index)
|
||||
break;
|
||||
if (index <= 0)
|
||||
break;
|
||||
if (out <= 0)
|
||||
break;
|
||||
|
||||
if (control & 0x80) {
|
||||
// Check if compression is out of bounds
|
||||
if (index < 2)
|
||||
return false;
|
||||
index -= 2;
|
||||
|
||||
u32 segment_offset = compressed[index] | (compressed[index + 1] << 8);
|
||||
u32 segment_size = ((segment_offset >> 12) & 15) + 3;
|
||||
segment_offset &= 0x0FFF;
|
||||
segment_offset += 2;
|
||||
|
||||
// Check if compression is out of bounds
|
||||
if (out < segment_size)
|
||||
return false;
|
||||
|
||||
for (unsigned j = 0; j < segment_size; j++) {
|
||||
// Check if compression is out of bounds
|
||||
if (out + segment_offset >= decompressed_size)
|
||||
return false;
|
||||
|
||||
u8 data = decompressed[out + segment_offset];
|
||||
decompressed[--out] = data;
|
||||
}
|
||||
} else {
|
||||
// Check if compression is out of bounds
|
||||
if (out < 1)
|
||||
return false;
|
||||
decompressed[--out] = compressed[--index];
|
||||
}
|
||||
control <<= 1;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
NCCHContainer::NCCHContainer(const std::string& filepath) : filepath(filepath) {
|
||||
file = FileUtil::IOFile(filepath, "rb");
|
||||
}
|
||||
|
||||
Loader::ResultStatus NCCHContainer::OpenFile(const std::string& filepath) {
|
||||
this->filepath = filepath;
|
||||
file = FileUtil::IOFile(filepath, "rb");
|
||||
|
||||
if (!file.IsOpen()) {
|
||||
LOG_WARNING(Service_FS, "Failed to open %s", filepath.c_str());
|
||||
return Loader::ResultStatus::Error;
|
||||
}
|
||||
|
||||
LOG_DEBUG(Service_FS, "Opened %s", filepath.c_str());
|
||||
return Loader::ResultStatus::Success;
|
||||
}
|
||||
|
||||
Loader::ResultStatus NCCHContainer::Load() {
|
||||
if (is_loaded)
|
||||
return Loader::ResultStatus::Success;
|
||||
|
||||
if (file.IsOpen()) {
|
||||
// Reset read pointer in case this file has been read before.
|
||||
file.Seek(0, SEEK_SET);
|
||||
|
||||
if (file.ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header))
|
||||
return Loader::ResultStatus::Error;
|
||||
|
||||
// Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)...
|
||||
if (Loader::MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) {
|
||||
LOG_DEBUG(Service_FS, "Only loading the first (bootable) NCCH within the NCSD file!");
|
||||
ncch_offset = 0x4000;
|
||||
file.Seek(ncch_offset, SEEK_SET);
|
||||
file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
|
||||
}
|
||||
|
||||
// Verify we are loading the correct file type...
|
||||
if (Loader::MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic)
|
||||
return Loader::ResultStatus::ErrorInvalidFormat;
|
||||
|
||||
has_header = true;
|
||||
|
||||
// System archives and DLC don't have an extended header but have RomFS
|
||||
if (ncch_header.extended_header_size) {
|
||||
if (file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)) !=
|
||||
sizeof(ExHeader_Header))
|
||||
return Loader::ResultStatus::Error;
|
||||
|
||||
is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1;
|
||||
u32 entry_point = exheader_header.codeset_info.text.address;
|
||||
u32 code_size = exheader_header.codeset_info.text.code_size;
|
||||
u32 stack_size = exheader_header.codeset_info.stack_size;
|
||||
u32 bss_size = exheader_header.codeset_info.bss_size;
|
||||
u32 core_version = exheader_header.arm11_system_local_caps.core_version;
|
||||
u8 priority = exheader_header.arm11_system_local_caps.priority;
|
||||
u8 resource_limit_category =
|
||||
exheader_header.arm11_system_local_caps.resource_limit_category;
|
||||
|
||||
LOG_DEBUG(Service_FS, "Name: %s",
|
||||
exheader_header.codeset_info.name);
|
||||
LOG_DEBUG(Service_FS, "Program ID: %016" PRIX64,
|
||||
ncch_header.program_id);
|
||||
LOG_DEBUG(Service_FS, "Code compressed: %s", is_compressed ? "yes" : "no");
|
||||
LOG_DEBUG(Service_FS, "Entry point: 0x%08X", entry_point);
|
||||
LOG_DEBUG(Service_FS, "Code size: 0x%08X", code_size);
|
||||
LOG_DEBUG(Service_FS, "Stack size: 0x%08X", stack_size);
|
||||
LOG_DEBUG(Service_FS, "Bss size: 0x%08X", bss_size);
|
||||
LOG_DEBUG(Service_FS, "Core version: %d", core_version);
|
||||
LOG_DEBUG(Service_FS, "Thread priority: 0x%X", priority);
|
||||
LOG_DEBUG(Service_FS, "Resource limit category: %d", resource_limit_category);
|
||||
LOG_DEBUG(Service_FS, "System Mode: %d",
|
||||
static_cast<int>(exheader_header.arm11_system_local_caps.system_mode));
|
||||
|
||||
if (exheader_header.system_info.jump_id != ncch_header.program_id) {
|
||||
LOG_ERROR(Service_FS,
|
||||
"ExHeader Program ID mismatch: the ROM is probably encrypted.");
|
||||
return Loader::ResultStatus::ErrorEncrypted;
|
||||
}
|
||||
|
||||
has_exheader = true;
|
||||
}
|
||||
|
||||
// DLC can have an ExeFS and a RomFS but no extended header
|
||||
if (ncch_header.exefs_size) {
|
||||
exefs_offset = ncch_header.exefs_offset * kBlockSize;
|
||||
u32 exefs_size = ncch_header.exefs_size * kBlockSize;
|
||||
|
||||
LOG_DEBUG(Service_FS, "ExeFS offset: 0x%08X", exefs_offset);
|
||||
LOG_DEBUG(Service_FS, "ExeFS size: 0x%08X", exefs_size);
|
||||
|
||||
file.Seek(exefs_offset + ncch_offset, SEEK_SET);
|
||||
if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header))
|
||||
return Loader::ResultStatus::Error;
|
||||
|
||||
exefs_file = FileUtil::IOFile(filepath, "rb");
|
||||
has_exefs = true;
|
||||
}
|
||||
|
||||
if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0)
|
||||
has_romfs = true;
|
||||
}
|
||||
|
||||
LoadOverrides();
|
||||
|
||||
// We need at least one of these or overrides, practically
|
||||
if (!(has_exefs || has_romfs || is_tainted))
|
||||
return Loader::ResultStatus::Error;
|
||||
|
||||
is_loaded = true;
|
||||
return Loader::ResultStatus::Success;
|
||||
}
|
||||
|
||||
Loader::ResultStatus NCCHContainer::LoadOverrides() {
|
||||
// Check for split-off files, mark the archive as tainted if we will use them
|
||||
std::string romfs_override = filepath + ".romfs";
|
||||
if (FileUtil::Exists(romfs_override)) {
|
||||
is_tainted = true;
|
||||
}
|
||||
|
||||
// If we have a split-off exefs file/folder, it takes priority
|
||||
std::string exefs_override = filepath + ".exefs";
|
||||
std::string exefsdir_override = filepath + ".exefsdir/";
|
||||
if (FileUtil::Exists(exefs_override)) {
|
||||
exefs_file = FileUtil::IOFile(exefs_override, "rb");
|
||||
|
||||
if (exefs_file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) == sizeof(ExeFs_Header)) {
|
||||
LOG_DEBUG(Service_FS, "Loading ExeFS section from %s", exefs_override.c_str());
|
||||
exefs_offset = 0;
|
||||
is_tainted = true;
|
||||
has_exefs = true;
|
||||
} else {
|
||||
exefs_file = FileUtil::IOFile(filepath, "rb");
|
||||
}
|
||||
} else if (FileUtil::Exists(exefsdir_override) && FileUtil::IsDirectory(exefsdir_override)) {
|
||||
is_tainted = true;
|
||||
}
|
||||
|
||||
if (is_tainted)
|
||||
LOG_WARNING(Service_FS,
|
||||
"Loaded NCCH %s is tainted, application behavior may not be as expected!",
|
||||
filepath.c_str());
|
||||
|
||||
return Loader::ResultStatus::Success;
|
||||
}
|
||||
|
||||
Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) {
|
||||
Loader::ResultStatus result = Load();
|
||||
if (result != Loader::ResultStatus::Success)
|
||||
return result;
|
||||
|
||||
// Check if we have files that can drop-in and replace
|
||||
result = LoadOverrideExeFSSection(name, buffer);
|
||||
if (result == Loader::ResultStatus::Success || !has_exefs)
|
||||
return result;
|
||||
|
||||
// If we don't have any separate files, we'll need a full ExeFS
|
||||
if (!exefs_file.IsOpen())
|
||||
return Loader::ResultStatus::Error;
|
||||
|
||||
LOG_DEBUG(Service_FS, "%d sections:", kMaxSections);
|
||||
// Iterate through the ExeFs archive until we find a section with the specified name...
|
||||
for (unsigned section_number = 0; section_number < kMaxSections; section_number++) {
|
||||
const auto& section = exefs_header.section[section_number];
|
||||
|
||||
// Load the specified section...
|
||||
if (strcmp(section.name, name) == 0) {
|
||||
LOG_DEBUG(Service_FS, "%d - offset: 0x%08X, size: 0x%08X, name: %s", section_number,
|
||||
section.offset, section.size, section.name);
|
||||
|
||||
s64 section_offset =
|
||||
(section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset);
|
||||
exefs_file.Seek(section_offset, SEEK_SET);
|
||||
|
||||
if (strcmp(section.name, ".code") == 0 && is_compressed) {
|
||||
// Section is compressed, read compressed .code section...
|
||||
std::unique_ptr<u8[]> temp_buffer;
|
||||
try {
|
||||
temp_buffer.reset(new u8[section.size]);
|
||||
} catch (std::bad_alloc&) {
|
||||
return Loader::ResultStatus::ErrorMemoryAllocationFailed;
|
||||
}
|
||||
|
||||
if (exefs_file.ReadBytes(&temp_buffer[0], section.size) != section.size)
|
||||
return Loader::ResultStatus::Error;
|
||||
|
||||
// Decompress .code section...
|
||||
u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], section.size);
|
||||
buffer.resize(decompressed_size);
|
||||
if (!LZSS_Decompress(&temp_buffer[0], section.size, &buffer[0], decompressed_size))
|
||||
return Loader::ResultStatus::ErrorInvalidFormat;
|
||||
} else {
|
||||
// Section is uncompressed...
|
||||
buffer.resize(section.size);
|
||||
if (exefs_file.ReadBytes(&buffer[0], section.size) != section.size)
|
||||
return Loader::ResultStatus::Error;
|
||||
}
|
||||
return Loader::ResultStatus::Success;
|
||||
}
|
||||
}
|
||||
return Loader::ResultStatus::ErrorNotUsed;
|
||||
}
|
||||
|
||||
Loader::ResultStatus NCCHContainer::LoadOverrideExeFSSection(const char* name,
|
||||
std::vector<u8>& buffer) {
|
||||
std::string override_name;
|
||||
|
||||
// Map our section name to the extracted equivalent
|
||||
if (!strcmp(name, ".code"))
|
||||
override_name = "code.bin";
|
||||
else if (!strcmp(name, "icon"))
|
||||
override_name = "code.bin";
|
||||
else if (!strcmp(name, "banner"))
|
||||
override_name = "banner.bnr";
|
||||
else if (!strcmp(name, "logo"))
|
||||
override_name = "logo.bcma.lz";
|
||||
else
|
||||
return Loader::ResultStatus::Error;
|
||||
|
||||
std::string section_override = filepath + ".exefsdir/" + override_name;
|
||||
FileUtil::IOFile section_file(section_override, "rb");
|
||||
|
||||
if (section_file.IsOpen()) {
|
||||
auto section_size = section_file.GetSize();
|
||||
buffer.resize(section_size);
|
||||
|
||||
section_file.Seek(0, SEEK_SET);
|
||||
if (section_file.ReadBytes(&buffer[0], section_size) == section_size) {
|
||||
LOG_WARNING(Service_FS, "File %s overriding built-in ExeFS file",
|
||||
section_override.c_str());
|
||||
return Loader::ResultStatus::Success;
|
||||
}
|
||||
}
|
||||
return Loader::ResultStatus::ErrorNotUsed;
|
||||
}
|
||||
|
||||
Loader::ResultStatus NCCHContainer::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file,
|
||||
u64& offset, u64& size) {
|
||||
Loader::ResultStatus result = Load();
|
||||
if (result != Loader::ResultStatus::Success)
|
||||
return result;
|
||||
|
||||
if (ReadOverrideRomFS(romfs_file, offset, size) == Loader::ResultStatus::Success)
|
||||
return Loader::ResultStatus::Success;
|
||||
|
||||
if (!has_romfs) {
|
||||
LOG_DEBUG(Service_FS, "RomFS requested from NCCH which has no RomFS");
|
||||
return Loader::ResultStatus::ErrorNotUsed;
|
||||
}
|
||||
|
||||
if (!file.IsOpen())
|
||||
return Loader::ResultStatus::Error;
|
||||
|
||||
u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000;
|
||||
u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000;
|
||||
|
||||
LOG_DEBUG(Service_FS, "RomFS offset: 0x%08X", romfs_offset);
|
||||
LOG_DEBUG(Service_FS, "RomFS size: 0x%08X", romfs_size);
|
||||
|
||||
if (file.GetSize() < romfs_offset + romfs_size)
|
||||
return Loader::ResultStatus::Error;
|
||||
|
||||
// We reopen the file, to allow its position to be independent from file's
|
||||
romfs_file = std::make_shared<FileUtil::IOFile>(filepath, "rb");
|
||||
if (!romfs_file->IsOpen())
|
||||
return Loader::ResultStatus::Error;
|
||||
|
||||
offset = romfs_offset;
|
||||
size = romfs_size;
|
||||
|
||||
return Loader::ResultStatus::Success;
|
||||
}
|
||||
|
||||
Loader::ResultStatus NCCHContainer::ReadOverrideRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file,
|
||||
u64& offset, u64& size) {
|
||||
// Check for RomFS overrides
|
||||
std::string split_filepath = filepath + ".romfs";
|
||||
if (FileUtil::Exists(split_filepath)) {
|
||||
romfs_file = std::make_shared<FileUtil::IOFile>(split_filepath, "rb");
|
||||
if (romfs_file->IsOpen()) {
|
||||
LOG_WARNING(Service_FS, "File %s overriding built-in RomFS", split_filepath.c_str());
|
||||
offset = 0;
|
||||
size = romfs_file->GetSize();
|
||||
return Loader::ResultStatus::Success;
|
||||
}
|
||||
}
|
||||
|
||||
return Loader::ResultStatus::ErrorNotUsed;
|
||||
}
|
||||
|
||||
Loader::ResultStatus NCCHContainer::ReadProgramId(u64_le& program_id) {
|
||||
Loader::ResultStatus result = Load();
|
||||
if (result != Loader::ResultStatus::Success)
|
||||
return result;
|
||||
|
||||
if (!has_header)
|
||||
return Loader::ResultStatus::ErrorNotUsed;
|
||||
|
||||
program_id = ncch_header.program_id;
|
||||
return Loader::ResultStatus::Success;
|
||||
}
|
||||
|
||||
bool NCCHContainer::HasExeFS() {
|
||||
Loader::ResultStatus result = Load();
|
||||
if (result != Loader::ResultStatus::Success)
|
||||
return false;
|
||||
|
||||
return has_exefs;
|
||||
}
|
||||
|
||||
bool NCCHContainer::HasRomFS() {
|
||||
Loader::ResultStatus result = Load();
|
||||
if (result != Loader::ResultStatus::Success)
|
||||
return false;
|
||||
|
||||
return has_romfs;
|
||||
}
|
||||
|
||||
bool NCCHContainer::HasExHeader() {
|
||||
Loader::ResultStatus result = Load();
|
||||
if (result != Loader::ResultStatus::Success)
|
||||
return false;
|
||||
|
||||
return has_exheader;
|
||||
}
|
||||
|
||||
} // namespace FileSys
|
@ -1,274 +0,0 @@
|
||||
// Copyright 2017 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "common/bit_field.h"
|
||||
#include "common/common_types.h"
|
||||
#include "common/file_util.h"
|
||||
#include "common/swap.h"
|
||||
#include "core/core.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// NCCH header (Note: "NCCH" appears to be a publicly unknown acronym)
|
||||
|
||||
struct NCCH_Header {
|
||||
u8 signature[0x100];
|
||||
u32_le magic;
|
||||
u32_le content_size;
|
||||
u8 partition_id[8];
|
||||
u16_le maker_code;
|
||||
u16_le version;
|
||||
u8 reserved_0[4];
|
||||
u64_le program_id;
|
||||
u8 reserved_1[0x10];
|
||||
u8 logo_region_hash[0x20];
|
||||
u8 product_code[0x10];
|
||||
u8 extended_header_hash[0x20];
|
||||
u32_le extended_header_size;
|
||||
u8 reserved_2[4];
|
||||
u8 flags[8];
|
||||
u32_le plain_region_offset;
|
||||
u32_le plain_region_size;
|
||||
u32_le logo_region_offset;
|
||||
u32_le logo_region_size;
|
||||
u32_le exefs_offset;
|
||||
u32_le exefs_size;
|
||||
u32_le exefs_hash_region_size;
|
||||
u8 reserved_3[4];
|
||||
u32_le romfs_offset;
|
||||
u32_le romfs_size;
|
||||
u32_le romfs_hash_region_size;
|
||||
u8 reserved_4[4];
|
||||
u8 exefs_super_block_hash[0x20];
|
||||
u8 romfs_super_block_hash[0x20];
|
||||
};
|
||||
|
||||
static_assert(sizeof(NCCH_Header) == 0x200, "NCCH header structure size is wrong");
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// ExeFS (executable file system) headers
|
||||
|
||||
struct ExeFs_SectionHeader {
|
||||
char name[8];
|
||||
u32 offset;
|
||||
u32 size;
|
||||
};
|
||||
|
||||
struct ExeFs_Header {
|
||||
ExeFs_SectionHeader section[8];
|
||||
u8 reserved[0x80];
|
||||
u8 hashes[8][0x20];
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// ExHeader (executable file system header) headers
|
||||
|
||||
struct ExHeader_SystemInfoFlags {
|
||||
u8 reserved[5];
|
||||
u8 flag;
|
||||
u8 remaster_version[2];
|
||||
};
|
||||
|
||||
struct ExHeader_CodeSegmentInfo {
|
||||
u32 address;
|
||||
u32 num_max_pages;
|
||||
u32 code_size;
|
||||
};
|
||||
|
||||
struct ExHeader_CodeSetInfo {
|
||||
u8 name[8];
|
||||
ExHeader_SystemInfoFlags flags;
|
||||
ExHeader_CodeSegmentInfo text;
|
||||
u32 stack_size;
|
||||
ExHeader_CodeSegmentInfo ro;
|
||||
u8 reserved[4];
|
||||
ExHeader_CodeSegmentInfo data;
|
||||
u32 bss_size;
|
||||
};
|
||||
|
||||
struct ExHeader_DependencyList {
|
||||
u8 program_id[0x30][8];
|
||||
};
|
||||
|
||||
struct ExHeader_SystemInfo {
|
||||
u64 save_data_size;
|
||||
u64_le jump_id;
|
||||
u8 reserved_2[0x30];
|
||||
};
|
||||
|
||||
struct ExHeader_StorageInfo {
|
||||
u8 ext_save_data_id[8];
|
||||
u8 system_save_data_id[8];
|
||||
u8 reserved[8];
|
||||
u8 access_info[7];
|
||||
u8 other_attributes;
|
||||
};
|
||||
|
||||
struct ExHeader_ARM11_SystemLocalCaps {
|
||||
u64_le program_id;
|
||||
u32_le core_version;
|
||||
u8 reserved_flags[2];
|
||||
union {
|
||||
u8 flags0;
|
||||
BitField<0, 2, u8> ideal_processor;
|
||||
BitField<2, 2, u8> affinity_mask;
|
||||
BitField<4, 4, u8> system_mode;
|
||||
};
|
||||
u8 priority;
|
||||
u8 resource_limit_descriptor[0x10][2];
|
||||
ExHeader_StorageInfo storage_info;
|
||||
u8 service_access_control[0x20][8];
|
||||
u8 ex_service_access_control[0x2][8];
|
||||
u8 reserved[0xf];
|
||||
u8 resource_limit_category;
|
||||
};
|
||||
|
||||
struct ExHeader_ARM11_KernelCaps {
|
||||
u32_le descriptors[28];
|
||||
u8 reserved[0x10];
|
||||
};
|
||||
|
||||
struct ExHeader_ARM9_AccessControl {
|
||||
u8 descriptors[15];
|
||||
u8 descversion;
|
||||
};
|
||||
|
||||
struct ExHeader_Header {
|
||||
ExHeader_CodeSetInfo codeset_info;
|
||||
ExHeader_DependencyList dependency_list;
|
||||
ExHeader_SystemInfo system_info;
|
||||
ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps;
|
||||
ExHeader_ARM11_KernelCaps arm11_kernel_caps;
|
||||
ExHeader_ARM9_AccessControl arm9_access_control;
|
||||
struct {
|
||||
u8 signature[0x100];
|
||||
u8 ncch_public_key_modulus[0x100];
|
||||
ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps;
|
||||
ExHeader_ARM11_KernelCaps arm11_kernel_caps;
|
||||
ExHeader_ARM9_AccessControl arm9_access_control;
|
||||
} access_desc;
|
||||
};
|
||||
|
||||
static_assert(sizeof(ExHeader_Header) == 0x800, "ExHeader structure size is wrong");
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// FileSys namespace
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
/**
|
||||
* Helper which implements an interface to deal with NCCH containers which can
|
||||
* contain ExeFS archives or RomFS archives for games or other applications.
|
||||
*/
|
||||
class NCCHContainer {
|
||||
public:
|
||||
NCCHContainer(const std::string& filepath);
|
||||
NCCHContainer() {}
|
||||
|
||||
Loader::ResultStatus OpenFile(const std::string& filepath);
|
||||
|
||||
/**
|
||||
* Ensure ExeFS and exheader is loaded and ready for reading sections
|
||||
* @return ResultStatus result of function
|
||||
*/
|
||||
Loader::ResultStatus Load();
|
||||
|
||||
/**
|
||||
* Attempt to find overridden sections for the NCCH and mark the container as tainted
|
||||
* if any are found.
|
||||
* @return ResultStatus result of function
|
||||
*/
|
||||
Loader::ResultStatus LoadOverrides();
|
||||
|
||||
/**
|
||||
* Reads an application ExeFS section of an NCCH file (e.g. .code, .logo, etc.)
|
||||
* @param name Name of section to read out of NCCH file
|
||||
* @param buffer Vector to read data into
|
||||
* @return ResultStatus result of function
|
||||
*/
|
||||
Loader::ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer);
|
||||
|
||||
/**
|
||||
* Reads an application ExeFS section from external files instead of an NCCH file,
|
||||
* (e.g. code.bin, logo.bcma.lz, icon.icn, banner.bnr)
|
||||
* @param name Name of section to read from external files
|
||||
* @param buffer Vector to read data into
|
||||
* @return ResultStatus result of function
|
||||
*/
|
||||
Loader::ResultStatus LoadOverrideExeFSSection(const char* name, std::vector<u8>& buffer);
|
||||
|
||||
/**
|
||||
* Get the RomFS of the NCCH container
|
||||
* Since the RomFS can be huge, we return a file reference instead of copying to a buffer
|
||||
* @param romfs_file The file containing the RomFS
|
||||
* @param offset The offset the romfs begins on
|
||||
* @param size The size of the romfs
|
||||
* @return ResultStatus result of function
|
||||
*/
|
||||
Loader::ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset,
|
||||
u64& size);
|
||||
|
||||
/**
|
||||
* Get the override RomFS of the NCCH container
|
||||
* Since the RomFS can be huge, we return a file reference instead of copying to a buffer
|
||||
* @param romfs_file The file containing the RomFS
|
||||
* @param offset The offset the romfs begins on
|
||||
* @param size The size of the romfs
|
||||
* @return ResultStatus result of function
|
||||
*/
|
||||
Loader::ResultStatus ReadOverrideRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file,
|
||||
u64& offset, u64& size);
|
||||
|
||||
/**
|
||||
* Get the Program ID of the NCCH container
|
||||
* @return ResultStatus result of function
|
||||
*/
|
||||
Loader::ResultStatus ReadProgramId(u64_le& program_id);
|
||||
|
||||
/**
|
||||
* Checks whether the NCCH container contains an ExeFS
|
||||
* @return bool check result
|
||||
*/
|
||||
bool HasExeFS();
|
||||
|
||||
/**
|
||||
* Checks whether the NCCH container contains a RomFS
|
||||
* @return bool check result
|
||||
*/
|
||||
bool HasRomFS();
|
||||
|
||||
/**
|
||||
* Checks whether the NCCH container contains an ExHeader
|
||||
* @return bool check result
|
||||
*/
|
||||
bool HasExHeader();
|
||||
|
||||
NCCH_Header ncch_header;
|
||||
ExeFs_Header exefs_header;
|
||||
ExHeader_Header exheader_header;
|
||||
|
||||
private:
|
||||
bool has_header = false;
|
||||
bool has_exheader = false;
|
||||
bool has_exefs = false;
|
||||
bool has_romfs = false;
|
||||
|
||||
bool is_tainted = false; // Are there parts of this container being overridden?
|
||||
bool is_loaded = false;
|
||||
bool is_compressed = false;
|
||||
|
||||
u32 ncch_offset = 0; // Offset to NCCH header, can be 0 or after NCSD header
|
||||
u32 exefs_offset = 0;
|
||||
|
||||
std::string filepath;
|
||||
FileUtil::IOFile file;
|
||||
FileUtil::IOFile exefs_file;
|
||||
};
|
||||
|
||||
} // namespace FileSys
|
@ -5,6 +5,7 @@
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "common/common_funcs.h"
|
||||
#include "common/common_types.h"
|
||||
#include "common/file_util.h"
|
||||
#include "common/logging/log.h"
|
||||
@ -376,7 +377,7 @@ FileType AppLoader_ELF::IdentifyType(FileUtil::IOFile& file) {
|
||||
if (1 != file.ReadArray<u16>(&machine, 1))
|
||||
return FileType::Error;
|
||||
|
||||
if (MakeMagic('\x7f', 'E', 'L', 'F') == magic && ELF_MACHINE_ARM == machine)
|
||||
if (Common::MakeMagic('\x7f', 'E', 'L', 'F') == magic && ELF_MACHINE_ARM == machine)
|
||||
return FileType::ELF;
|
||||
|
||||
return FileType::Error;
|
||||
|
@ -75,10 +75,6 @@ enum class ResultStatus {
|
||||
ErrorEncrypted,
|
||||
};
|
||||
|
||||
constexpr u32 MakeMagic(char a, char b, char c, char d) {
|
||||
return a | b << 8 | c << 16 | d << 24;
|
||||
}
|
||||
|
||||
/// Interface for loading an application
|
||||
class AppLoader : NonCopyable {
|
||||
public:
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "common/common_funcs.h"
|
||||
#include "common/logging/log.h"
|
||||
#include "common/swap.h"
|
||||
#include "core/hle/kernel/process.h"
|
||||
@ -51,7 +52,7 @@ FileType AppLoader_NRO::IdentifyType(FileUtil::IOFile& file) {
|
||||
if (sizeof(NroHeader) != file.ReadBytes(&nro_header, sizeof(NroHeader))) {
|
||||
return FileType::Error;
|
||||
}
|
||||
if (nro_header.magic == MakeMagic('N', 'R', 'O', '0')) {
|
||||
if (nro_header.magic == Common::MakeMagic('N', 'R', 'O', '0')) {
|
||||
return FileType::NRO;
|
||||
}
|
||||
return FileType::Error;
|
||||
@ -87,7 +88,7 @@ bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) {
|
||||
if (sizeof(NroHeader) != file.ReadBytes(&nro_header, sizeof(NroHeader))) {
|
||||
return {};
|
||||
}
|
||||
if (nro_header.magic != MakeMagic('N', 'R', 'O', '0')) {
|
||||
if (nro_header.magic != Common::MakeMagic('N', 'R', 'O', '0')) {
|
||||
return {};
|
||||
}
|
||||
|
||||
@ -109,7 +110,7 @@ bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) {
|
||||
u32 bss_size{Memory::PAGE_SIZE}; // Default .bss to page size if MOD0 section doesn't exist
|
||||
std::memcpy(&mod_header, program_image.data() + nro_header.module_header_offset,
|
||||
sizeof(ModHeader));
|
||||
const bool has_mod_header{mod_header.magic == MakeMagic('M', 'O', 'D', '0')};
|
||||
const bool has_mod_header{mod_header.magic == Common::MakeMagic('M', 'O', 'D', '0')};
|
||||
if (has_mod_header) {
|
||||
// Resize program image to include .bss section and page align each section
|
||||
bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset);
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <vector>
|
||||
#include <lz4.h>
|
||||
|
||||
#include "common/common_funcs.h"
|
||||
#include "common/logging/log.h"
|
||||
#include "common/swap.h"
|
||||
#include "core/hle/kernel/process.h"
|
||||
@ -50,7 +51,7 @@ FileType AppLoader_NSO::IdentifyType(FileUtil::IOFile& file) {
|
||||
return FileType::Error;
|
||||
}
|
||||
|
||||
if (MakeMagic('N', 'S', 'O', '0') == magic) {
|
||||
if (Common::MakeMagic('N', 'S', 'O', '0') == magic) {
|
||||
return FileType::NSO;
|
||||
}
|
||||
|
||||
@ -96,7 +97,7 @@ VAddr AppLoader_NSO::LoadNso(const std::string& path, VAddr load_base, bool relo
|
||||
if (sizeof(NsoHeader) != file.ReadBytes(&nso_header, sizeof(NsoHeader))) {
|
||||
return {};
|
||||
}
|
||||
if (nso_header.magic != MakeMagic('N', 'S', 'O', '0')) {
|
||||
if (nso_header.magic != Common::MakeMagic('N', 'S', 'O', '0')) {
|
||||
return {};
|
||||
}
|
||||
|
||||
@ -121,7 +122,7 @@ VAddr AppLoader_NSO::LoadNso(const std::string& path, VAddr load_base, bool relo
|
||||
ModHeader mod_header{};
|
||||
u32 bss_size{Memory::PAGE_SIZE}; // Default .bss to page size if MOD0 section doesn't exist
|
||||
std::memcpy(&mod_header, program_image.data() + module_offset, sizeof(ModHeader));
|
||||
const bool has_mod_header{mod_header.magic == MakeMagic('M', 'O', 'D', '0')};
|
||||
const bool has_mod_header{mod_header.magic == Common::MakeMagic('M', 'O', 'D', '0')};
|
||||
if (has_mod_header) {
|
||||
// Resize program image to include .bss section and page align each section
|
||||
bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset);
|
||||
|
@ -1,51 +0,0 @@
|
||||
// Copyright 2016 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include "common/common_types.h"
|
||||
#include "core/loader/loader.h"
|
||||
#include "core/loader/smdh.h"
|
||||
#include "video_core/utils.h"
|
||||
|
||||
namespace Loader {
|
||||
|
||||
bool IsValidSMDH(const std::vector<u8>& smdh_data) {
|
||||
if (smdh_data.size() < sizeof(Loader::SMDH))
|
||||
return false;
|
||||
|
||||
u32 magic;
|
||||
memcpy(&magic, smdh_data.data(), sizeof(u32));
|
||||
|
||||
return Loader::MakeMagic('S', 'M', 'D', 'H') == magic;
|
||||
}
|
||||
|
||||
std::vector<u16> SMDH::GetIcon(bool large) const {
|
||||
u32 size;
|
||||
const u8* icon_data;
|
||||
|
||||
if (large) {
|
||||
size = 48;
|
||||
icon_data = large_icon.data();
|
||||
} else {
|
||||
size = 24;
|
||||
icon_data = small_icon.data();
|
||||
}
|
||||
|
||||
std::vector<u16> icon(size * size);
|
||||
for (u32 x = 0; x < size; ++x) {
|
||||
for (u32 y = 0; y < size; ++y) {
|
||||
u32 coarse_y = y & ~7;
|
||||
const u8* pixel = icon_data + VideoCore::GetMortonOffset(x, y, 2) + coarse_y * size * 2;
|
||||
icon[x + size * y] = (pixel[1] << 8) + pixel[0];
|
||||
}
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
std::array<u16, 0x40> SMDH::GetShortTitle(Loader::SMDH::TitleLanguage language) const {
|
||||
return titles[static_cast<int>(language)].short_title;
|
||||
}
|
||||
|
||||
} // namespace
|
@ -1,81 +0,0 @@
|
||||
// Copyright 2016 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <vector>
|
||||
#include "common/common_funcs.h"
|
||||
#include "common/common_types.h"
|
||||
#include "common/swap.h"
|
||||
|
||||
namespace Loader {
|
||||
|
||||
/**
|
||||
* Tests if data is a valid SMDH by its length and magic number.
|
||||
* @param smdh_data data buffer to test
|
||||
* @return bool test result
|
||||
*/
|
||||
bool IsValidSMDH(const std::vector<u8>& smdh_data);
|
||||
|
||||
/// SMDH data structure that contains titles, icons etc. See https://www.3dbrew.org/wiki/SMDH
|
||||
struct SMDH {
|
||||
u32_le magic;
|
||||
u16_le version;
|
||||
INSERT_PADDING_BYTES(2);
|
||||
|
||||
struct Title {
|
||||
std::array<u16, 0x40> short_title;
|
||||
std::array<u16, 0x80> long_title;
|
||||
std::array<u16, 0x40> publisher;
|
||||
};
|
||||
std::array<Title, 16> titles;
|
||||
|
||||
std::array<u8, 16> ratings;
|
||||
u32_le region_lockout;
|
||||
u32_le match_maker_id;
|
||||
u64_le match_maker_bit_id;
|
||||
u32_le flags;
|
||||
u16_le eula_version;
|
||||
INSERT_PADDING_BYTES(2);
|
||||
float_le banner_animation_frame;
|
||||
u32_le cec_id;
|
||||
INSERT_PADDING_BYTES(8);
|
||||
|
||||
std::array<u8, 0x480> small_icon;
|
||||
std::array<u8, 0x1200> large_icon;
|
||||
|
||||
/// indicates the language used for each title entry
|
||||
enum class TitleLanguage {
|
||||
Japanese = 0,
|
||||
English = 1,
|
||||
French = 2,
|
||||
German = 3,
|
||||
Italian = 4,
|
||||
Spanish = 5,
|
||||
SimplifiedChinese = 6,
|
||||
Korean = 7,
|
||||
Dutch = 8,
|
||||
Portuguese = 9,
|
||||
Russian = 10,
|
||||
TraditionalChinese = 11
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets game icon from SMDH
|
||||
* @param large If true, returns large icon (48x48), otherwise returns small icon (24x24)
|
||||
* @return vector of RGB565 data
|
||||
*/
|
||||
std::vector<u16> GetIcon(bool large) const;
|
||||
|
||||
/**
|
||||
* Gets the short game title from SMDH
|
||||
* @param language title language
|
||||
* @return UTF-16 array of the short title
|
||||
*/
|
||||
std::array<u16, 0x40> GetShortTitle(Loader::SMDH::TitleLanguage language) const;
|
||||
};
|
||||
static_assert(sizeof(SMDH) == 0x36C0, "SMDH structure size is wrong");
|
||||
|
||||
} // namespace
|
Loading…
Reference in New Issue
Block a user