2017-09-25 06:17:38 +00:00
|
|
|
// 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>
|
2018-07-28 15:30:54 +00:00
|
|
|
#include <cryptopp/aes.h>
|
|
|
|
#include <cryptopp/modes.h>
|
2018-10-01 15:42:10 +00:00
|
|
|
#include <cryptopp/sha.h>
|
2017-09-25 06:17:38 +00:00
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "core/core.h"
|
2020-02-07 05:45:10 +00:00
|
|
|
#include "core/file_sys/layered_fs.h"
|
2017-09-25 06:17:38 +00:00
|
|
|
#include "core/file_sys/ncch_container.h"
|
2019-11-24 19:32:10 +00:00
|
|
|
#include "core/file_sys/patch.h"
|
2018-10-01 15:42:10 +00:00
|
|
|
#include "core/file_sys/seed_db.h"
|
2018-07-28 15:30:54 +00:00
|
|
|
#include "core/hw/aes/key.h"
|
2017-09-25 06:17:38 +00:00
|
|
|
#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)
|
|
|
|
|
2020-02-11 06:03:07 +00:00
|
|
|
u64 GetModId(u64 program_id) {
|
|
|
|
constexpr u64 UPDATE_MASK = 0x0000000e'00000000;
|
|
|
|
if ((program_id & 0x000000ff'00000000) == UPDATE_MASK) { // Apply the mods to updates
|
|
|
|
return program_id & ~UPDATE_MASK;
|
|
|
|
}
|
|
|
|
return program_id;
|
|
|
|
}
|
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2018-03-28 23:55:24 +00:00
|
|
|
u32 offset_size;
|
|
|
|
std::memcpy(&offset_size, buffer + size - sizeof(u32), sizeof(u32));
|
2017-09-25 06:17:38 +00:00
|
|
|
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;
|
2018-03-28 23:55:24 +00:00
|
|
|
|
|
|
|
u32 buffer_top_and_bottom;
|
|
|
|
std::memcpy(&buffer_top_and_bottom, footer, sizeof(u32));
|
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-14 14:14:30 +00:00
|
|
|
NCCHContainer::NCCHContainer(const std::string& filepath, u32 ncch_offset, u32 partition)
|
|
|
|
: ncch_offset(ncch_offset), partition(partition), filepath(filepath) {
|
2017-09-25 06:17:38 +00:00
|
|
|
file = FileUtil::IOFile(filepath, "rb");
|
|
|
|
}
|
|
|
|
|
2020-07-14 14:14:30 +00:00
|
|
|
Loader::ResultStatus NCCHContainer::OpenFile(const std::string& filepath, u32 ncch_offset,
|
|
|
|
u32 partition) {
|
2017-09-25 06:17:38 +00:00
|
|
|
this->filepath = filepath;
|
2017-11-16 00:30:52 +00:00
|
|
|
this->ncch_offset = ncch_offset;
|
2020-07-14 14:14:30 +00:00
|
|
|
this->partition = partition;
|
2017-09-25 06:17:38 +00:00
|
|
|
file = FileUtil::IOFile(filepath, "rb");
|
|
|
|
|
|
|
|
if (!file.IsOpen()) {
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_WARNING(Service_FS, "Failed to open {}", filepath);
|
2017-09-25 06:17:38 +00:00
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
}
|
|
|
|
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_DEBUG(Service_FS, "Opened {}", filepath);
|
2017-09-25 06:17:38 +00:00
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2020-03-15 16:54:13 +00:00
|
|
|
Loader::ResultStatus NCCHContainer::LoadHeader() {
|
2020-03-15 18:00:26 +00:00
|
|
|
if (has_header) {
|
2020-03-15 16:54:13 +00:00
|
|
|
return Loader::ResultStatus::Success;
|
2020-03-15 18:00:26 +00:00
|
|
|
}
|
2020-03-15 16:54:13 +00:00
|
|
|
if (!file.IsOpen()) {
|
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset read pointer in case this file has been read before.
|
|
|
|
file.Seek(ncch_offset, SEEK_SET);
|
|
|
|
|
2020-03-15 18:00:26 +00:00
|
|
|
if (file.ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header)) {
|
2020-03-15 16:54:13 +00:00
|
|
|
return Loader::ResultStatus::Error;
|
2020-03-15 18:00:26 +00:00
|
|
|
}
|
2020-03-15 16:54:13 +00:00
|
|
|
|
|
|
|
// 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) {
|
2020-07-14 14:14:30 +00:00
|
|
|
NCSD_Header ncsd_header;
|
|
|
|
file.Seek(ncch_offset, SEEK_SET);
|
|
|
|
file.ReadBytes(&ncsd_header, sizeof(NCSD_Header));
|
|
|
|
ASSERT(Loader::MakeMagic('N', 'C', 'S', 'D') == ncsd_header.magic);
|
|
|
|
ASSERT(partition < 8);
|
|
|
|
ncch_offset = ncsd_header.partitions[partition].offset * kBlockSize;
|
|
|
|
LOG_ERROR(Service_FS, "{}", ncch_offset);
|
2020-03-15 16:54:13 +00:00
|
|
|
file.Seek(ncch_offset, SEEK_SET);
|
|
|
|
file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are loading the correct file type...
|
2020-03-15 18:00:26 +00:00
|
|
|
if (Loader::MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic) {
|
2020-03-15 16:54:13 +00:00
|
|
|
return Loader::ResultStatus::ErrorInvalidFormat;
|
2020-03-15 18:00:26 +00:00
|
|
|
}
|
2020-03-15 16:54:13 +00:00
|
|
|
|
|
|
|
has_header = true;
|
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
Loader::ResultStatus NCCHContainer::Load() {
|
|
|
|
if (is_loaded)
|
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
if (file.IsOpen()) {
|
|
|
|
// Reset read pointer in case this file has been read before.
|
2017-11-16 00:30:52 +00:00
|
|
|
file.Seek(ncch_offset, SEEK_SET);
|
2017-09-25 06:17:38 +00:00
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
if (file.ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header))
|
|
|
|
return Loader::ResultStatus::Error;
|
2017-09-25 06:17:38 +00:00
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
// 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) {
|
2020-07-14 14:14:30 +00:00
|
|
|
NCSD_Header ncsd_header;
|
|
|
|
file.Seek(ncch_offset, SEEK_SET);
|
|
|
|
file.ReadBytes(&ncsd_header, sizeof(NCSD_Header));
|
|
|
|
ASSERT(Loader::MakeMagic('N', 'C', 'S', 'D') == ncsd_header.magic);
|
|
|
|
ASSERT(partition < 8);
|
|
|
|
ncch_offset = ncsd_header.partitions[partition].offset * kBlockSize;
|
2017-10-01 16:30:47 +00:00
|
|
|
file.Seek(ncch_offset, SEEK_SET);
|
|
|
|
file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
|
|
|
|
}
|
2017-09-25 06:17:38 +00:00
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
// 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;
|
2018-07-28 15:30:54 +00:00
|
|
|
bool failed_to_decrypt = false;
|
|
|
|
if (!ncch_header.no_crypto) {
|
|
|
|
is_encrypted = true;
|
|
|
|
|
|
|
|
// Find primary and secondary keys
|
|
|
|
if (ncch_header.fixed_key) {
|
|
|
|
LOG_DEBUG(Service_FS, "Fixed-key crypto");
|
|
|
|
primary_key.fill(0);
|
|
|
|
secondary_key.fill(0);
|
|
|
|
} else {
|
|
|
|
using namespace HW::AES;
|
|
|
|
InitKeys();
|
|
|
|
std::array<u8, 16> key_y_primary, key_y_secondary;
|
|
|
|
|
|
|
|
std::copy(ncch_header.signature, ncch_header.signature + key_y_primary.size(),
|
|
|
|
key_y_primary.begin());
|
|
|
|
|
|
|
|
if (!ncch_header.seed_crypto) {
|
|
|
|
key_y_secondary = key_y_primary;
|
|
|
|
} else {
|
2018-10-01 15:42:10 +00:00
|
|
|
auto opt{FileSys::GetSeed(ncch_header.program_id)};
|
|
|
|
if (!opt.has_value()) {
|
|
|
|
LOG_ERROR(Service_FS, "Seed for program {:016X} not found",
|
|
|
|
ncch_header.program_id);
|
|
|
|
failed_to_decrypt = true;
|
|
|
|
} else {
|
|
|
|
auto seed{*opt};
|
|
|
|
std::array<u8, 32> input;
|
|
|
|
std::memcpy(input.data(), key_y_primary.data(), key_y_primary.size());
|
|
|
|
std::memcpy(input.data() + key_y_primary.size(), seed.data(), seed.size());
|
|
|
|
CryptoPP::SHA256 sha;
|
2018-11-11 03:57:29 +00:00
|
|
|
std::array<u8, CryptoPP::SHA256::DIGESTSIZE> hash;
|
|
|
|
sha.CalculateDigest(hash.data(), input.data(), input.size());
|
|
|
|
std::memcpy(key_y_secondary.data(), hash.data(), key_y_secondary.size());
|
2018-10-01 15:42:10 +00:00
|
|
|
}
|
2018-07-28 15:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SetKeyY(KeySlotID::NCCHSecure1, key_y_primary);
|
|
|
|
if (!IsNormalKeyAvailable(KeySlotID::NCCHSecure1)) {
|
|
|
|
LOG_ERROR(Service_FS, "Secure1 KeyX missing");
|
|
|
|
failed_to_decrypt = true;
|
|
|
|
}
|
|
|
|
primary_key = GetNormalKey(KeySlotID::NCCHSecure1);
|
|
|
|
|
|
|
|
switch (ncch_header.secondary_key_slot) {
|
|
|
|
case 0:
|
|
|
|
LOG_DEBUG(Service_FS, "Secure1 crypto");
|
|
|
|
secondary_key = primary_key;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
LOG_DEBUG(Service_FS, "Secure2 crypto");
|
|
|
|
SetKeyY(KeySlotID::NCCHSecure2, key_y_secondary);
|
|
|
|
if (!IsNormalKeyAvailable(KeySlotID::NCCHSecure2)) {
|
|
|
|
LOG_ERROR(Service_FS, "Secure2 KeyX missing");
|
|
|
|
failed_to_decrypt = true;
|
|
|
|
}
|
|
|
|
secondary_key = GetNormalKey(KeySlotID::NCCHSecure2);
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
LOG_DEBUG(Service_FS, "Secure3 crypto");
|
|
|
|
SetKeyY(KeySlotID::NCCHSecure3, key_y_secondary);
|
|
|
|
if (!IsNormalKeyAvailable(KeySlotID::NCCHSecure3)) {
|
|
|
|
LOG_ERROR(Service_FS, "Secure3 KeyX missing");
|
|
|
|
failed_to_decrypt = true;
|
|
|
|
}
|
|
|
|
secondary_key = GetNormalKey(KeySlotID::NCCHSecure3);
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
LOG_DEBUG(Service_FS, "Secure4 crypto");
|
|
|
|
SetKeyY(KeySlotID::NCCHSecure4, key_y_secondary);
|
|
|
|
if (!IsNormalKeyAvailable(KeySlotID::NCCHSecure4)) {
|
|
|
|
LOG_ERROR(Service_FS, "Secure4 KeyX missing");
|
|
|
|
failed_to_decrypt = true;
|
|
|
|
}
|
|
|
|
secondary_key = GetNormalKey(KeySlotID::NCCHSecure4);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find CTR for each section
|
|
|
|
// Written with reference to
|
|
|
|
// https://github.com/d0k3/GodMode9/blob/99af6a73be48fa7872649aaa7456136da0df7938/arm9/source/game/ncch.c#L34-L52
|
|
|
|
if (ncch_header.version == 0 || ncch_header.version == 2) {
|
|
|
|
LOG_DEBUG(Loader, "NCCH version 0/2");
|
|
|
|
// In this version, CTR for each section is a magic number prefixed by partition ID
|
|
|
|
// (reverse order)
|
|
|
|
std::reverse_copy(ncch_header.partition_id, ncch_header.partition_id + 8,
|
|
|
|
exheader_ctr.begin());
|
|
|
|
exefs_ctr = romfs_ctr = exheader_ctr;
|
|
|
|
exheader_ctr[8] = 1;
|
|
|
|
exefs_ctr[8] = 2;
|
|
|
|
romfs_ctr[8] = 3;
|
|
|
|
} else if (ncch_header.version == 1) {
|
|
|
|
LOG_DEBUG(Loader, "NCCH version 1");
|
|
|
|
// In this version, CTR for each section is the section offset prefixed by partition
|
|
|
|
// ID, as if the entire NCCH image is encrypted using a single CTR stream.
|
|
|
|
std::copy(ncch_header.partition_id, ncch_header.partition_id + 8,
|
|
|
|
exheader_ctr.begin());
|
|
|
|
exefs_ctr = romfs_ctr = exheader_ctr;
|
|
|
|
auto u32ToBEArray = [](u32 value) -> std::array<u8, 4> {
|
|
|
|
return std::array<u8, 4>{
|
|
|
|
static_cast<u8>(value >> 24),
|
|
|
|
static_cast<u8>((value >> 16) & 0xFF),
|
|
|
|
static_cast<u8>((value >> 8) & 0xFF),
|
|
|
|
static_cast<u8>(value & 0xFF),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
auto offset_exheader = u32ToBEArray(0x200); // exheader offset
|
|
|
|
auto offset_exefs = u32ToBEArray(ncch_header.exefs_offset * kBlockSize);
|
|
|
|
auto offset_romfs = u32ToBEArray(ncch_header.romfs_offset * kBlockSize);
|
|
|
|
std::copy(offset_exheader.begin(), offset_exheader.end(),
|
|
|
|
exheader_ctr.begin() + 12);
|
|
|
|
std::copy(offset_exefs.begin(), offset_exefs.end(), exefs_ctr.begin() + 12);
|
|
|
|
std::copy(offset_romfs.begin(), offset_romfs.end(), romfs_ctr.begin() + 12);
|
|
|
|
} else {
|
|
|
|
LOG_ERROR(Service_FS, "Unknown NCCH version {}", ncch_header.version);
|
|
|
|
failed_to_decrypt = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG_DEBUG(Service_FS, "No crypto");
|
|
|
|
is_encrypted = false;
|
|
|
|
}
|
2017-10-01 16:30:47 +00:00
|
|
|
|
|
|
|
// System archives and DLC don't have an extended header but have RomFS
|
|
|
|
if (ncch_header.extended_header_size) {
|
2019-06-29 16:08:52 +00:00
|
|
|
auto read_exheader = [this](FileUtil::IOFile& file) {
|
|
|
|
const std::size_t size = sizeof(exheader_header);
|
|
|
|
return file && file.ReadBytes(&exheader_header, size) == size;
|
|
|
|
};
|
|
|
|
|
2019-08-22 09:10:28 +00:00
|
|
|
if (!read_exheader(file)) {
|
2017-10-01 16:30:47 +00:00
|
|
|
return Loader::ResultStatus::Error;
|
2019-06-29 16:08:52 +00:00
|
|
|
}
|
2017-10-01 16:30:47 +00:00
|
|
|
|
2019-08-22 09:10:28 +00:00
|
|
|
if (is_encrypted) {
|
2018-07-28 15:30:54 +00:00
|
|
|
// This ID check is masked to low 32-bit as a toleration to ill-formed ROM created
|
|
|
|
// by merging games and its updates.
|
|
|
|
if ((exheader_header.system_info.jump_id & 0xFFFFFFFF) ==
|
|
|
|
(ncch_header.program_id & 0xFFFFFFFF)) {
|
|
|
|
LOG_WARNING(Service_FS, "NCCH is marked as encrypted but with decrypted "
|
|
|
|
"exheader. Force no crypto scheme.");
|
|
|
|
is_encrypted = false;
|
|
|
|
} else {
|
|
|
|
if (failed_to_decrypt) {
|
|
|
|
LOG_ERROR(Service_FS, "Failed to decrypt");
|
|
|
|
return Loader::ResultStatus::ErrorEncrypted;
|
|
|
|
}
|
|
|
|
CryptoPP::byte* data = reinterpret_cast<CryptoPP::byte*>(&exheader_header);
|
|
|
|
CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption(
|
|
|
|
primary_key.data(), primary_key.size(), exheader_ctr.data())
|
|
|
|
.ProcessData(data, data, sizeof(exheader_header));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 07:55:35 +00:00
|
|
|
const auto mods_path =
|
|
|
|
fmt::format("{}mods/{:016X}/", FileUtil::GetUserPath(FileUtil::UserPath::LoadDir),
|
2020-02-11 06:03:07 +00:00
|
|
|
GetModId(ncch_header.program_id));
|
2020-02-29 01:11:34 +00:00
|
|
|
const std::array<std::string, 2> exheader_override_paths{{
|
2020-02-07 07:55:35 +00:00
|
|
|
mods_path + "exheader.bin",
|
|
|
|
filepath + ".exheader",
|
|
|
|
}};
|
|
|
|
|
|
|
|
bool has_exheader_override = false;
|
|
|
|
for (const auto& path : exheader_override_paths) {
|
|
|
|
FileUtil::IOFile exheader_override_file{path, "rb"};
|
|
|
|
if (read_exheader(exheader_override_file)) {
|
|
|
|
has_exheader_override = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-08-22 09:10:28 +00:00
|
|
|
if (has_exheader_override) {
|
|
|
|
if (exheader_header.system_info.jump_id !=
|
|
|
|
exheader_header.arm11_system_local_caps.program_id) {
|
|
|
|
LOG_WARNING(Service_FS, "Jump ID and Program ID don't match. "
|
|
|
|
"The override exheader might not be decrypted.");
|
|
|
|
}
|
|
|
|
is_tainted = true;
|
|
|
|
}
|
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
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;
|
|
|
|
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_DEBUG(Service_FS, "Name: {}",
|
2018-06-29 13:56:12 +00:00
|
|
|
exheader_header.codeset_info.name);
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_DEBUG(Service_FS, "Program ID: {:016X}", ncch_header.program_id);
|
2018-06-29 13:56:12 +00:00
|
|
|
LOG_DEBUG(Service_FS, "Code compressed: {}", is_compressed ? "yes" : "no");
|
2018-06-29 11:18:07 +00:00
|
|
|
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: {}", core_version);
|
|
|
|
LOG_DEBUG(Service_FS, "Thread priority: 0x{:X}", priority);
|
|
|
|
LOG_DEBUG(Service_FS, "Resource limit category: {}", resource_limit_category);
|
|
|
|
LOG_DEBUG(Service_FS, "System Mode: {}",
|
2018-06-29 13:56:12 +00:00
|
|
|
static_cast<int>(exheader_header.arm11_system_local_caps.system_mode));
|
2017-10-01 16:30:47 +00:00
|
|
|
|
|
|
|
has_exheader = true;
|
|
|
|
}
|
2017-09-25 06:17:38 +00:00
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
// 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;
|
|
|
|
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_DEBUG(Service_FS, "ExeFS offset: 0x{:08X}", exefs_offset);
|
|
|
|
LOG_DEBUG(Service_FS, "ExeFS size: 0x{:08X}", exefs_size);
|
2017-10-01 16:30:47 +00:00
|
|
|
|
|
|
|
file.Seek(exefs_offset + ncch_offset, SEEK_SET);
|
|
|
|
if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header))
|
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
|
2018-07-28 15:30:54 +00:00
|
|
|
if (is_encrypted) {
|
|
|
|
CryptoPP::byte* data = reinterpret_cast<CryptoPP::byte*>(&exefs_header);
|
|
|
|
CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption(primary_key.data(),
|
|
|
|
primary_key.size(), exefs_ctr.data())
|
|
|
|
.ProcessData(data, data, sizeof(exefs_header));
|
|
|
|
}
|
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
exefs_file = FileUtil::IOFile(filepath, "rb");
|
|
|
|
has_exefs = true;
|
2017-09-25 06:17:38 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0)
|
|
|
|
has_romfs = true;
|
2017-09-25 06:17:38 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
LoadOverrides();
|
2017-09-25 06:17:38 +00:00
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
// We need at least one of these or overrides, practically
|
|
|
|
if (!(has_exefs || has_romfs || is_tainted))
|
|
|
|
return Loader::ResultStatus::Error;
|
2017-09-25 06:17:38 +00:00
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
is_loaded = true;
|
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
2017-09-25 06:17:38 +00:00
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
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;
|
2017-09-25 06:17:38 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
// 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)) {
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_DEBUG(Service_FS, "Loading ExeFS section from {}", exefs_override);
|
2017-10-01 16:30:47 +00:00
|
|
|
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)
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_WARNING(Service_FS,
|
2018-06-29 13:56:12 +00:00
|
|
|
"Loaded NCCH {} is tainted, application behavior may not be as expected!",
|
|
|
|
filepath);
|
2017-09-25 06:17:38 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
// Check if we have files that can drop-in and replace
|
|
|
|
result = LoadOverrideExeFSSection(name, buffer);
|
|
|
|
if (result == Loader::ResultStatus::Success || !has_exefs)
|
|
|
|
return result;
|
|
|
|
|
2017-12-09 16:21:52 +00:00
|
|
|
// As of firmware 5.0.0-11 the logo is stored between the access descriptor and the plain region
|
|
|
|
// instead of the ExeFS.
|
|
|
|
if (std::strcmp(name, "logo") == 0) {
|
|
|
|
if (ncch_header.logo_region_offset && ncch_header.logo_region_size) {
|
2018-09-06 20:03:28 +00:00
|
|
|
std::size_t logo_offset = ncch_header.logo_region_offset * kBlockSize;
|
|
|
|
std::size_t logo_size = ncch_header.logo_region_size * kBlockSize;
|
2017-12-09 16:21:52 +00:00
|
|
|
|
|
|
|
buffer.resize(logo_size);
|
|
|
|
file.Seek(ncch_offset + logo_offset, SEEK_SET);
|
|
|
|
|
|
|
|
if (file.ReadBytes(buffer.data(), logo_size) != logo_size) {
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_ERROR(Service_FS, "Could not read NCCH logo");
|
2017-12-09 16:21:52 +00:00
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
}
|
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
} else {
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_INFO(Service_FS, "Attempting to load logo from the ExeFS");
|
2017-12-09 16:21:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
// If we don't have any separate files, we'll need a full ExeFS
|
|
|
|
if (!exefs_file.IsOpen())
|
|
|
|
return Loader::ResultStatus::Error;
|
2017-09-25 06:17:38 +00:00
|
|
|
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_DEBUG(Service_FS, "{} sections:", kMaxSections);
|
2017-09-25 06:17:38 +00:00
|
|
|
// 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) {
|
2018-06-29 13:56:12 +00:00
|
|
|
LOG_DEBUG(Service_FS, "{} - offset: 0x{:08X}, size: 0x{:08X}, name: {}", section_number,
|
|
|
|
section.offset, section.size, section.name);
|
2017-09-25 06:17:38 +00:00
|
|
|
|
|
|
|
s64 section_offset =
|
|
|
|
(section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset);
|
2017-10-01 16:30:47 +00:00
|
|
|
exefs_file.Seek(section_offset, SEEK_SET);
|
2017-09-25 06:17:38 +00:00
|
|
|
|
2018-07-28 15:30:54 +00:00
|
|
|
std::array<u8, 16> key;
|
|
|
|
if (strcmp(section.name, "icon") == 0 || strcmp(section.name, "banner") == 0) {
|
|
|
|
key = primary_key;
|
|
|
|
} else {
|
|
|
|
key = secondary_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption dec(key.data(), key.size(),
|
|
|
|
exefs_ctr.data());
|
|
|
|
dec.Seek(section.offset + sizeof(ExeFs_Header));
|
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
if (exefs_file.ReadBytes(&temp_buffer[0], section.size) != section.size)
|
2017-09-25 06:17:38 +00:00
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
|
2018-07-28 15:30:54 +00:00
|
|
|
if (is_encrypted) {
|
|
|
|
dec.ProcessData(&temp_buffer[0], &temp_buffer[0], section.size);
|
|
|
|
}
|
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
// Decompress .code section...
|
|
|
|
u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], section.size);
|
|
|
|
buffer.resize(decompressed_size);
|
2021-07-14 09:01:14 +00:00
|
|
|
if (!LZSS_Decompress(&temp_buffer[0], section.size, buffer.data(),
|
|
|
|
decompressed_size))
|
2017-09-25 06:17:38 +00:00
|
|
|
return Loader::ResultStatus::ErrorInvalidFormat;
|
|
|
|
} else {
|
|
|
|
// Section is uncompressed...
|
|
|
|
buffer.resize(section.size);
|
2021-07-14 09:01:14 +00:00
|
|
|
if (exefs_file.ReadBytes(buffer.data(), section.size) != section.size)
|
2017-09-25 06:17:38 +00:00
|
|
|
return Loader::ResultStatus::Error;
|
2018-07-28 15:30:54 +00:00
|
|
|
if (is_encrypted) {
|
2021-07-14 09:01:14 +00:00
|
|
|
dec.ProcessData(buffer.data(), buffer.data(), section.size);
|
2018-07-28 15:30:54 +00:00
|
|
|
}
|
2017-09-25 06:17:38 +00:00
|
|
|
}
|
2018-10-20 18:09:05 +00:00
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Loader::ResultStatus::ErrorNotUsed;
|
|
|
|
}
|
|
|
|
|
2019-12-21 12:31:34 +00:00
|
|
|
Loader::ResultStatus NCCHContainer::ApplyCodePatch(std::vector<u8>& code) const {
|
2019-11-25 19:49:48 +00:00
|
|
|
struct PatchLocation {
|
|
|
|
std::string path;
|
|
|
|
bool (*patch_fn)(const std::vector<u8>& patch, std::vector<u8>& code);
|
|
|
|
};
|
2020-02-07 05:50:29 +00:00
|
|
|
|
|
|
|
const auto mods_path =
|
|
|
|
fmt::format("{}mods/{:016X}/", FileUtil::GetUserPath(FileUtil::UserPath::LoadDir),
|
2020-02-11 06:03:07 +00:00
|
|
|
GetModId(ncch_header.program_id));
|
2020-02-28 15:36:17 +00:00
|
|
|
const std::array<PatchLocation, 6> patch_paths{{
|
2020-02-07 05:50:29 +00:00
|
|
|
{mods_path + "exefs/code.ips", Patch::ApplyIpsPatch},
|
|
|
|
{mods_path + "exefs/code.bps", Patch::ApplyBpsPatch},
|
2020-02-28 15:36:17 +00:00
|
|
|
{mods_path + "code.ips", Patch::ApplyIpsPatch},
|
|
|
|
{mods_path + "code.bps", Patch::ApplyBpsPatch},
|
2019-11-25 19:49:48 +00:00
|
|
|
{filepath + ".exefsdir/code.ips", Patch::ApplyIpsPatch},
|
|
|
|
{filepath + ".exefsdir/code.bps", Patch::ApplyBpsPatch},
|
|
|
|
}};
|
|
|
|
|
|
|
|
for (const PatchLocation& info : patch_paths) {
|
|
|
|
FileUtil::IOFile file{info.path, "rb"};
|
|
|
|
if (!file)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::vector<u8> patch(file.GetSize());
|
|
|
|
if (file.ReadBytes(patch.data(), patch.size()) != patch.size())
|
|
|
|
return Loader::ResultStatus::Error;
|
2019-06-30 16:27:00 +00:00
|
|
|
|
2019-11-25 19:49:48 +00:00
|
|
|
LOG_INFO(Service_FS, "File {} patching code.bin", info.path);
|
|
|
|
if (!info.patch_fn(patch, code))
|
|
|
|
return Loader::ResultStatus::Error;
|
2019-12-21 12:31:34 +00:00
|
|
|
|
2019-11-25 19:49:48 +00:00
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
return Loader::ResultStatus::ErrorNotUsed;
|
2019-06-30 16:27:00 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
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"))
|
2019-06-30 09:48:28 +00:00
|
|
|
override_name = "icon.bin";
|
2017-10-01 16:30:47 +00:00
|
|
|
else if (!strcmp(name, "banner"))
|
|
|
|
override_name = "banner.bnr";
|
|
|
|
else if (!strcmp(name, "logo"))
|
|
|
|
override_name = "logo.bcma.lz";
|
|
|
|
else
|
2017-09-25 06:17:38 +00:00
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
|
2020-02-07 05:50:29 +00:00
|
|
|
const auto mods_path =
|
|
|
|
fmt::format("{}mods/{:016X}/", FileUtil::GetUserPath(FileUtil::UserPath::LoadDir),
|
2020-02-11 06:03:07 +00:00
|
|
|
GetModId(ncch_header.program_id));
|
2020-02-29 01:11:34 +00:00
|
|
|
const std::array<std::string, 3> override_paths{{
|
2020-02-07 05:50:29 +00:00
|
|
|
mods_path + "exefs/" + override_name,
|
2020-02-28 15:36:17 +00:00
|
|
|
mods_path + override_name,
|
2020-02-07 05:50:29 +00:00
|
|
|
filepath + ".exefsdir/" + override_name,
|
|
|
|
}};
|
2017-10-01 16:30:47 +00:00
|
|
|
|
2020-02-07 05:50:29 +00:00
|
|
|
for (const auto& path : override_paths) {
|
|
|
|
FileUtil::IOFile section_file(path, "rb");
|
2017-10-01 16:30:47 +00:00
|
|
|
|
2020-02-07 05:50:29 +00:00
|
|
|
if (section_file.IsOpen()) {
|
|
|
|
auto section_size = section_file.GetSize();
|
|
|
|
buffer.resize(section_size);
|
|
|
|
|
|
|
|
section_file.Seek(0, SEEK_SET);
|
2021-07-14 09:01:14 +00:00
|
|
|
if (section_file.ReadBytes(buffer.data(), section_size) == section_size) {
|
2020-02-07 05:50:29 +00:00
|
|
|
LOG_WARNING(Service_FS, "File {} overriding built-in ExeFS file", path);
|
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
2017-10-01 16:30:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Loader::ResultStatus::ErrorNotUsed;
|
|
|
|
}
|
|
|
|
|
2020-02-09 12:59:31 +00:00
|
|
|
Loader::ResultStatus NCCHContainer::ReadRomFS(std::shared_ptr<RomFSReader>& romfs_file,
|
|
|
|
bool use_layered_fs) {
|
2017-09-25 06:17:38 +00:00
|
|
|
Loader::ResultStatus result = Load();
|
|
|
|
if (result != Loader::ResultStatus::Success)
|
|
|
|
return result;
|
|
|
|
|
2018-07-28 15:30:54 +00:00
|
|
|
if (ReadOverrideRomFS(romfs_file) == Loader::ResultStatus::Success)
|
2017-10-01 16:30:47 +00:00
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
if (!has_romfs) {
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_DEBUG(Service_FS, "RomFS requested from NCCH which has no RomFS");
|
2017-09-25 06:17:38 +00:00
|
|
|
return Loader::ResultStatus::ErrorNotUsed;
|
|
|
|
}
|
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
if (!file.IsOpen())
|
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000;
|
|
|
|
u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000;
|
|
|
|
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_DEBUG(Service_FS, "RomFS offset: 0x{:08X}", romfs_offset);
|
|
|
|
LOG_DEBUG(Service_FS, "RomFS size: 0x{:08X}", romfs_size);
|
2017-09-25 06:17:38 +00:00
|
|
|
|
|
|
|
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
|
2018-07-28 15:30:54 +00:00
|
|
|
FileUtil::IOFile romfs_file_inner(filepath, "rb");
|
|
|
|
if (!romfs_file_inner.IsOpen())
|
2017-09-25 06:17:38 +00:00
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
|
2020-02-07 05:45:10 +00:00
|
|
|
std::shared_ptr<RomFSReader> direct_romfs;
|
2018-07-28 15:30:54 +00:00
|
|
|
if (is_encrypted) {
|
2020-02-07 05:45:10 +00:00
|
|
|
direct_romfs =
|
|
|
|
std::make_shared<DirectRomFSReader>(std::move(romfs_file_inner), romfs_offset,
|
|
|
|
romfs_size, secondary_key, romfs_ctr, 0x1000);
|
2018-07-28 15:30:54 +00:00
|
|
|
} else {
|
2020-02-07 05:45:10 +00:00
|
|
|
direct_romfs = std::make_shared<DirectRomFSReader>(std::move(romfs_file_inner),
|
|
|
|
romfs_offset, romfs_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto path =
|
|
|
|
fmt::format("{}mods/{:016X}/", FileUtil::GetUserPath(FileUtil::UserPath::LoadDir),
|
2020-02-11 06:03:07 +00:00
|
|
|
GetModId(ncch_header.program_id));
|
2020-02-09 12:59:31 +00:00
|
|
|
if (use_layered_fs &&
|
|
|
|
(FileUtil::Exists(path + "romfs/") || FileUtil::Exists(path + "romfs_ext/"))) {
|
|
|
|
|
2020-02-07 05:45:10 +00:00
|
|
|
romfs_file = std::make_shared<LayeredFS>(std::move(direct_romfs), path + "romfs/",
|
|
|
|
path + "romfs_ext/");
|
|
|
|
} else {
|
|
|
|
romfs_file = std::move(direct_romfs);
|
2018-07-28 15:30:54 +00:00
|
|
|
}
|
2017-09-25 06:17:38 +00:00
|
|
|
|
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2020-02-09 12:59:31 +00:00
|
|
|
Loader::ResultStatus NCCHContainer::DumpRomFS(const std::string& target_path) {
|
|
|
|
std::shared_ptr<RomFSReader> direct_romfs;
|
|
|
|
Loader::ResultStatus result = ReadRomFS(direct_romfs, false);
|
|
|
|
if (result != Loader::ResultStatus::Success)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
std::shared_ptr<LayeredFS> layered_fs =
|
|
|
|
std::make_shared<LayeredFS>(std::move(direct_romfs), "", "", false);
|
|
|
|
|
|
|
|
if (!layered_fs->DumpRomFS(target_path)) {
|
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
}
|
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2018-07-28 15:30:54 +00:00
|
|
|
Loader::ResultStatus NCCHContainer::ReadOverrideRomFS(std::shared_ptr<RomFSReader>& romfs_file) {
|
2017-10-01 16:30:47 +00:00
|
|
|
// Check for RomFS overrides
|
|
|
|
std::string split_filepath = filepath + ".romfs";
|
|
|
|
if (FileUtil::Exists(split_filepath)) {
|
2018-07-28 15:30:54 +00:00
|
|
|
FileUtil::IOFile romfs_file_inner(split_filepath, "rb");
|
|
|
|
if (romfs_file_inner.IsOpen()) {
|
2020-02-07 05:45:10 +00:00
|
|
|
LOG_WARNING(Service_FS, "File {} overriding built-in RomFS; LayeredFS not enabled",
|
|
|
|
split_filepath);
|
|
|
|
romfs_file = std::make_shared<DirectRomFSReader>(std::move(romfs_file_inner), 0,
|
|
|
|
romfs_file_inner.GetSize());
|
2017-10-01 16:30:47 +00:00
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Loader::ResultStatus::ErrorNotUsed;
|
|
|
|
}
|
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
Loader::ResultStatus NCCHContainer::ReadProgramId(u64_le& program_id) {
|
2020-03-15 16:54:13 +00:00
|
|
|
Loader::ResultStatus result = LoadHeader();
|
2017-09-25 06:17:38 +00:00
|
|
|
if (result != Loader::ResultStatus::Success)
|
|
|
|
return result;
|
|
|
|
|
2017-10-01 16:30:47 +00:00
|
|
|
if (!has_header)
|
|
|
|
return Loader::ResultStatus::ErrorNotUsed;
|
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
program_id = ncch_header.program_id;
|
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2018-09-16 04:48:39 +00:00
|
|
|
Loader::ResultStatus NCCHContainer::ReadExtdataId(u64& extdata_id) {
|
|
|
|
Loader::ResultStatus result = Load();
|
|
|
|
if (result != Loader::ResultStatus::Success)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
if (!has_exheader)
|
|
|
|
return Loader::ResultStatus::ErrorNotUsed;
|
|
|
|
|
|
|
|
if (exheader_header.arm11_system_local_caps.storage_info.other_attributes >> 1) {
|
2018-10-01 00:41:40 +00:00
|
|
|
// Using extended save data access
|
|
|
|
// There would be multiple possible extdata IDs in this case. The best we can do for now is
|
|
|
|
// guessing that the first one would be the main save.
|
|
|
|
const std::array<u64, 6> extdata_ids{{
|
|
|
|
exheader_header.arm11_system_local_caps.storage_info.extdata_id0.Value(),
|
|
|
|
exheader_header.arm11_system_local_caps.storage_info.extdata_id1.Value(),
|
|
|
|
exheader_header.arm11_system_local_caps.storage_info.extdata_id2.Value(),
|
|
|
|
exheader_header.arm11_system_local_caps.storage_info.extdata_id3.Value(),
|
|
|
|
exheader_header.arm11_system_local_caps.storage_info.extdata_id4.Value(),
|
|
|
|
exheader_header.arm11_system_local_caps.storage_info.extdata_id5.Value(),
|
|
|
|
}};
|
|
|
|
for (u64 id : extdata_ids) {
|
|
|
|
if (id) {
|
|
|
|
// Found a non-zero ID, use it
|
|
|
|
extdata_id = id;
|
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-16 04:48:39 +00:00
|
|
|
return Loader::ResultStatus::ErrorNotUsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
extdata_id = exheader_header.arm11_system_local_caps.storage_info.ext_save_data_id;
|
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
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
|