From f5cf9960d9eb5ff5afb39c0356f42035e2dd1ccf Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 1 Aug 2017 19:51:44 -0400 Subject: loader: Expose program title. --- src/core/loader/loader.h | 9 +++++++++ src/core/loader/ncch.cpp | 20 ++++++++++++++++++++ src/core/loader/ncch.h | 14 ++------------ 3 files changed, 31 insertions(+), 12 deletions(-) (limited to 'src/core/loader') diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index 48bbf687d..e731888a2 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h @@ -166,6 +166,15 @@ public: return ResultStatus::ErrorNotImplemented; } + /** + * Get the title of the application + * @param title Reference to store the application title into + * @return ResultStatus result of function + */ + virtual ResultStatus ReadTitle(std::string& title) { + return ResultStatus::ErrorNotImplemented; + } + protected: FileUtil::IOFile file; bool is_loaded = false; diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index fc4d14a59..c007069a9 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -4,7 +4,9 @@ #include #include +#include #include +#include #include #include "common/logging/log.h" #include "common/string_util.h" @@ -420,4 +422,22 @@ ResultStatus AppLoader_NCCH::ReadRomFS(std::shared_ptr& romfs_ return ResultStatus::ErrorNotUsed; } +ResultStatus AppLoader_NCCH::ReadTitle(std::string& title) { + std::vector data; + Loader::SMDH smdh; + ReadIcon(data); + + if (!Loader::IsValidSMDH(data)) { + return ResultStatus::ErrorInvalidFormat; + } + + memcpy(&smdh, data.data(), sizeof(Loader::SMDH)); + + const auto& short_title = smdh.GetShortTitle(SMDH::TitleLanguage::English); + auto title_end = std::find(short_title.begin(), short_title.end(), u'\0'); + title = Common::UTF16ToUTF8(std::u16string{short_title.begin(), title_end}); + + return ResultStatus::Success; +} + } // namespace Loader diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h index 0ebd47fd5..e40cef764 100644 --- a/src/core/loader/ncch.h +++ b/src/core/loader/ncch.h @@ -191,23 +191,13 @@ public: ResultStatus ReadLogo(std::vector& buffer) override; - /** - * Get the program id of the application - * @param out_program_id Reference to store program id into - * @return ResultStatus result of function - */ ResultStatus ReadProgramId(u64& out_program_id) override; - /** - * Get the RomFS of the application - * @param romfs_file Reference to buffer to store data - * @param offset Offset in the file to the RomFS - * @param size Size of the RomFS in bytes - * @return ResultStatus result of function - */ ResultStatus ReadRomFS(std::shared_ptr& romfs_file, u64& offset, u64& size) override; + ResultStatus ReadTitle(std::string& title) override; + private: /** * Reads an application ExeFS section of an NCCH file into AppLoader (e.g. .code, .logo, etc.) -- cgit v1.2.3 From 5d0a1e7efddf234234d54fe97395f6975f8d1a28 Mon Sep 17 00:00:00 2001 From: B3n30 Date: Sat, 19 Aug 2017 19:14:33 +0200 Subject: Added missing parts in libnetwork (#2838) * Network: Set and send the game information over enet Added Callbacks for RoomMember and GetMemberList to Room in preparation for web_services. --- src/core/loader/ncch.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/core/loader') diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index c007069a9..7aff7f29b 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -20,6 +20,7 @@ #include "core/loader/ncch.h" #include "core/loader/smdh.h" #include "core/memory.h" +#include "network/network.h" //////////////////////////////////////////////////////////////////////////////////////////////////// // Loader namespace @@ -350,6 +351,13 @@ ResultStatus AppLoader_NCCH::Load() { Core::Telemetry().AddField(Telemetry::FieldType::Session, "ProgramId", program_id); + if (auto room_member = Network::GetRoomMember().lock()) { + Network::GameInfo game_info; + ReadTitle(game_info.name); + game_info.id = ncch_header.program_id; + room_member->SendGameInfo(game_info); + } + is_loaded = true; // Set state to loaded result = LoadExec(); // Load the executable into memory for booting -- cgit v1.2.3 From 6d2734a074f44a24129db850339677d8d7b436aa Mon Sep 17 00:00:00 2001 From: Subv Date: Fri, 21 Jul 2017 21:17:57 -0500 Subject: Kernel/Memory: Give each Process its own page table. The loader is in charge of setting the newly created process's page table as the main one during the loading process. --- src/core/loader/3dsx.cpp | 1 + src/core/loader/elf.cpp | 1 + src/core/loader/ncch.cpp | 1 + 3 files changed, 3 insertions(+) (limited to 'src/core/loader') diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp index 74e336487..69cdc0867 100644 --- a/src/core/loader/3dsx.cpp +++ b/src/core/loader/3dsx.cpp @@ -270,6 +270,7 @@ ResultStatus AppLoader_THREEDSX::Load() { Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); Kernel::g_current_process->svc_access_mask.set(); Kernel::g_current_process->address_mappings = default_address_mappings; + Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; // Attach the default resource limit (APPLICATION) to the process Kernel::g_current_process->resource_limit = diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index cfcde9167..2f27606a1 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp @@ -397,6 +397,7 @@ ResultStatus AppLoader_ELF::Load() { Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); Kernel::g_current_process->svc_access_mask.set(); Kernel::g_current_process->address_mappings = default_address_mappings; + Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; // Attach the default resource limit (APPLICATION) to the process Kernel::g_current_process->resource_limit = diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index 7aff7f29b..79ea50147 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -172,6 +172,7 @@ ResultStatus AppLoader_NCCH::LoadExec() { codeset->memory = std::make_shared>(std::move(code)); Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); + Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; // Attach a resource limit to the process based on the resource limit category Kernel::g_current_process->resource_limit = -- cgit v1.2.3 From c02bbb7030efd072511bd0051a44d9e503016f74 Mon Sep 17 00:00:00 2001 From: MerryMage Date: Sun, 24 Sep 2017 22:42:42 +0100 Subject: memory: Add GetCurrentPageTable/SetCurrentPageTable Don't expose Memory::current_page_table as a global. --- src/core/loader/3dsx.cpp | 2 +- src/core/loader/elf.cpp | 2 +- src/core/loader/ncch.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/core/loader') diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp index 69cdc0867..a03515e6e 100644 --- a/src/core/loader/3dsx.cpp +++ b/src/core/loader/3dsx.cpp @@ -270,7 +270,7 @@ ResultStatus AppLoader_THREEDSX::Load() { Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); Kernel::g_current_process->svc_access_mask.set(); Kernel::g_current_process->address_mappings = default_address_mappings; - Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; + Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table); // Attach the default resource limit (APPLICATION) to the process Kernel::g_current_process->resource_limit = diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index 2f27606a1..2de1f4e81 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp @@ -397,7 +397,7 @@ ResultStatus AppLoader_ELF::Load() { Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); Kernel::g_current_process->svc_access_mask.set(); Kernel::g_current_process->address_mappings = default_address_mappings; - Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; + Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table); // Attach the default resource limit (APPLICATION) to the process Kernel::g_current_process->resource_limit = diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index 79ea50147..ca282f935 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -172,7 +172,7 @@ ResultStatus AppLoader_NCCH::LoadExec() { codeset->memory = std::make_shared>(std::move(code)); Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); - Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; + Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table); // Attach a resource limit to the process based on the resource limit category Kernel::g_current_process->resource_limit = -- cgit v1.2.3 From c91ccbd0ba4118554d7377bbc3bd4c64f9bccf84 Mon Sep 17 00:00:00 2001 From: Max Thomas Date: Mon, 25 Sep 2017 00:17:38 -0600 Subject: Loader/NCCH: Add support for loading application updates (#2927) * loader/ncch: split NCCH parsing into its own file * loader/ncch: add support for loading update NCCHs from the SD card * loader/ncch: fix formatting * file_sys/ncch_container: Return a value for OpenFile * loader/ncch: cleanup, always instantiate overlay_ncch to base_ncch * file_sys/ncch_container: better encryption checks, allow non-app NCCHs to load properly and for the existence of NCCH structures to be checked * file_sys/ncch_container: pass filepath as a const reference --- src/core/loader/loader.h | 13 ++ src/core/loader/ncch.cpp | 319 +++++++++-------------------------------------- src/core/loader/ncch.h | 184 ++------------------------- 3 files changed, 82 insertions(+), 434 deletions(-) (limited to 'src/core/loader') diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index e731888a2..3160fd2fd 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h @@ -166,6 +166,19 @@ public: return ResultStatus::ErrorNotImplemented; } + /** + * Get the update RomFS of the application + * 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 + */ + virtual ResultStatus ReadUpdateRomFS(std::shared_ptr& romfs_file, u64& offset, + u64& size) { + return ResultStatus::ErrorNotImplemented; + } + /** * Get the title of the application * @param title Reference to store the application title into diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index 79ea50147..bef7fa567 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -13,6 +13,7 @@ #include "common/swap.h" #include "core/core.h" #include "core/file_sys/archive_selfncch.h" +#include "core/file_sys/ncch_container.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/resource_limit.h" #include "core/hle/service/cfg/cfg.h" @@ -27,87 +28,7 @@ namespace Loader { -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(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; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// AppLoader_NCCH class +static const u64 UPDATE_MASK = 0x0000000e00000000; FileType AppLoader_NCCH::IdentifyType(FileUtil::IOFile& file) { u32 magic; @@ -124,15 +45,25 @@ FileType AppLoader_NCCH::IdentifyType(FileUtil::IOFile& file) { return FileType::Error; } +static std::string GetUpdateNCCHPath(u64_le program_id) { + u32 high = static_cast((program_id | UPDATE_MASK) >> 32); + u32 low = static_cast((program_id | UPDATE_MASK) & 0xFFFFFFFF); + + return Common::StringFromFormat("%sNintendo 3DS/%s/%s/title/%08x/%08x/content/00000000.app", + FileUtil::GetUserPath(D_SDMC_IDX).c_str(), SYSTEM_ID, SDCARD_ID, + high, low); +} + std::pair, ResultStatus> AppLoader_NCCH::LoadKernelSystemMode() { if (!is_loaded) { - ResultStatus res = LoadExeFS(); + ResultStatus res = base_ncch.Load(); if (res != ResultStatus::Success) { return std::make_pair(boost::none, res); } } + // Set the system mode as the one from the exheader. - return std::make_pair(exheader_header.arm11_system_local_caps.system_mode.Value(), + return std::make_pair(overlay_ncch->exheader_header.arm11_system_local_caps.system_mode.Value(), ResultStatus::Success); } @@ -144,29 +75,34 @@ ResultStatus AppLoader_NCCH::LoadExec() { return ResultStatus::ErrorNotLoaded; std::vector code; - if (ResultStatus::Success == ReadCode(code)) { + u64_le program_id; + if (ResultStatus::Success == ReadCode(code) && + ResultStatus::Success == ReadProgramId(program_id)) { std::string process_name = Common::StringFromFixedZeroTerminatedBuffer( - (const char*)exheader_header.codeset_info.name, 8); + (const char*)overlay_ncch->exheader_header.codeset_info.name, 8); - SharedPtr codeset = CodeSet::Create(process_name, ncch_header.program_id); + SharedPtr codeset = CodeSet::Create(process_name, program_id); codeset->code.offset = 0; - codeset->code.addr = exheader_header.codeset_info.text.address; - codeset->code.size = exheader_header.codeset_info.text.num_max_pages * Memory::PAGE_SIZE; + codeset->code.addr = overlay_ncch->exheader_header.codeset_info.text.address; + codeset->code.size = + overlay_ncch->exheader_header.codeset_info.text.num_max_pages * Memory::PAGE_SIZE; codeset->rodata.offset = codeset->code.offset + codeset->code.size; - codeset->rodata.addr = exheader_header.codeset_info.ro.address; - codeset->rodata.size = exheader_header.codeset_info.ro.num_max_pages * Memory::PAGE_SIZE; + codeset->rodata.addr = overlay_ncch->exheader_header.codeset_info.ro.address; + codeset->rodata.size = + overlay_ncch->exheader_header.codeset_info.ro.num_max_pages * Memory::PAGE_SIZE; // TODO(yuriks): Not sure if the bss size is added to the page-aligned .data size or just // to the regular size. Playing it safe for now. - u32 bss_page_size = (exheader_header.codeset_info.bss_size + 0xFFF) & ~0xFFF; + u32 bss_page_size = (overlay_ncch->exheader_header.codeset_info.bss_size + 0xFFF) & ~0xFFF; code.resize(code.size() + bss_page_size, 0); codeset->data.offset = codeset->rodata.offset + codeset->rodata.size; - codeset->data.addr = exheader_header.codeset_info.data.address; + codeset->data.addr = overlay_ncch->exheader_header.codeset_info.data.address; codeset->data.size = - exheader_header.codeset_info.data.num_max_pages * Memory::PAGE_SIZE + bss_page_size; + overlay_ncch->exheader_header.codeset_info.data.num_max_pages * Memory::PAGE_SIZE + + bss_page_size; codeset->entrypoint = codeset->code.addr; codeset->memory = std::make_shared>(std::move(code)); @@ -177,150 +113,27 @@ ResultStatus AppLoader_NCCH::LoadExec() { // Attach a resource limit to the process based on the resource limit category Kernel::g_current_process->resource_limit = Kernel::ResourceLimit::GetForCategory(static_cast( - exheader_header.arm11_system_local_caps.resource_limit_category)); + overlay_ncch->exheader_header.arm11_system_local_caps.resource_limit_category)); // Set the default CPU core for this process Kernel::g_current_process->ideal_processor = - exheader_header.arm11_system_local_caps.ideal_processor; + overlay_ncch->exheader_header.arm11_system_local_caps.ideal_processor; // Copy data while converting endianness - std::array kernel_caps; - std::copy_n(exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(), + std::arrayexheader_header.arm11_kernel_caps.descriptors)> + kernel_caps; + std::copy_n(overlay_ncch->exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(), begin(kernel_caps)); Kernel::g_current_process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size()); - s32 priority = exheader_header.arm11_system_local_caps.priority; - u32 stack_size = exheader_header.codeset_info.stack_size; + s32 priority = overlay_ncch->exheader_header.arm11_system_local_caps.priority; + u32 stack_size = overlay_ncch->exheader_header.codeset_info.stack_size; Kernel::g_current_process->Run(priority, stack_size); return ResultStatus::Success; } return ResultStatus::Error; } -ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector& buffer) { - if (!file.IsOpen()) - return ResultStatus::Error; - - ResultStatus result = LoadExeFS(); - if (result != ResultStatus::Success) - return result; - - LOG_DEBUG(Loader, "%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(Loader, "%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); - file.Seek(section_offset, SEEK_SET); - - if (strcmp(section.name, ".code") == 0 && is_compressed) { - // Section is compressed, read compressed .code section... - std::unique_ptr temp_buffer; - try { - temp_buffer.reset(new u8[section.size]); - } catch (std::bad_alloc&) { - return ResultStatus::ErrorMemoryAllocationFailed; - } - - if (file.ReadBytes(&temp_buffer[0], section.size) != section.size) - return 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 ResultStatus::ErrorInvalidFormat; - } else { - // Section is uncompressed... - buffer.resize(section.size); - if (file.ReadBytes(&buffer[0], section.size) != section.size) - return ResultStatus::Error; - } - return ResultStatus::Success; - } - } - return ResultStatus::ErrorNotUsed; -} - -ResultStatus AppLoader_NCCH::LoadExeFS() { - if (is_exefs_loaded) - return ResultStatus::Success; - - if (!file.IsOpen()) - return ResultStatus::Error; - - // 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 ResultStatus::Error; - - // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)... - if (MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) { - LOG_DEBUG(Loader, "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 (MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic) - return ResultStatus::ErrorInvalidFormat; - - // Read ExHeader... - - if (file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != sizeof(ExHeader_Header)) - return ResultStatus::Error; - - is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1; - entry_point = exheader_header.codeset_info.text.address; - code_size = exheader_header.codeset_info.text.code_size; - stack_size = exheader_header.codeset_info.stack_size; - bss_size = exheader_header.codeset_info.bss_size; - core_version = exheader_header.arm11_system_local_caps.core_version; - priority = exheader_header.arm11_system_local_caps.priority; - resource_limit_category = exheader_header.arm11_system_local_caps.resource_limit_category; - - LOG_DEBUG(Loader, "Name: %s", exheader_header.codeset_info.name); - LOG_DEBUG(Loader, "Program ID: %016" PRIX64, ncch_header.program_id); - LOG_DEBUG(Loader, "Code compressed: %s", is_compressed ? "yes" : "no"); - LOG_DEBUG(Loader, "Entry point: 0x%08X", entry_point); - LOG_DEBUG(Loader, "Code size: 0x%08X", code_size); - LOG_DEBUG(Loader, "Stack size: 0x%08X", stack_size); - LOG_DEBUG(Loader, "Bss size: 0x%08X", bss_size); - LOG_DEBUG(Loader, "Core version: %d", core_version); - LOG_DEBUG(Loader, "Thread priority: 0x%X", priority); - LOG_DEBUG(Loader, "Resource limit category: %d", resource_limit_category); - LOG_DEBUG(Loader, "System Mode: %d", - static_cast(exheader_header.arm11_system_local_caps.system_mode)); - - if (exheader_header.arm11_system_local_caps.program_id != ncch_header.program_id) { - LOG_ERROR(Loader, "ExHeader Program ID mismatch: the ROM is probably encrypted."); - return ResultStatus::ErrorEncrypted; - } - - // Read ExeFS... - - exefs_offset = ncch_header.exefs_offset * kBlockSize; - u32 exefs_size = ncch_header.exefs_size * kBlockSize; - - LOG_DEBUG(Loader, "ExeFS offset: 0x%08X", exefs_offset); - LOG_DEBUG(Loader, "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 ResultStatus::Error; - - is_exefs_loaded = true; - return ResultStatus::Success; -} - void AppLoader_NCCH::ParseRegionLockoutInfo() { std::vector smdh_buffer; if (ReadIcon(smdh_buffer) == ResultStatus::Success && smdh_buffer.size() >= sizeof(SMDH)) { @@ -339,23 +152,32 @@ void AppLoader_NCCH::ParseRegionLockoutInfo() { } ResultStatus AppLoader_NCCH::Load() { + u64_le ncch_program_id; + if (is_loaded) return ResultStatus::ErrorAlreadyLoaded; - ResultStatus result = LoadExeFS(); + ResultStatus result = base_ncch.Load(); if (result != ResultStatus::Success) return result; - std::string program_id{Common::StringFromFormat("%016" PRIX64, ncch_header.program_id)}; + ReadProgramId(ncch_program_id); + std::string program_id{Common::StringFromFormat("%016" PRIX64, ncch_program_id)}; LOG_INFO(Loader, "Program ID: %s", program_id.c_str()); + update_ncch.OpenFile(GetUpdateNCCHPath(ncch_program_id)); + result = update_ncch.Load(); + if (result == ResultStatus::Success) { + overlay_ncch = &update_ncch; + } + Core::Telemetry().AddField(Telemetry::FieldType::Session, "ProgramId", program_id); if (auto room_member = Network::GetRoomMember().lock()) { Network::GameInfo game_info; ReadTitle(game_info.name); - game_info.id = ncch_header.program_id; + game_info.id = ncch_program_id; room_member->SendGameInfo(game_info); } @@ -374,61 +196,40 @@ ResultStatus AppLoader_NCCH::Load() { } ResultStatus AppLoader_NCCH::ReadCode(std::vector& buffer) { - return LoadSectionExeFS(".code", buffer); + return overlay_ncch->LoadSectionExeFS(".code", buffer); } ResultStatus AppLoader_NCCH::ReadIcon(std::vector& buffer) { - return LoadSectionExeFS("icon", buffer); + return overlay_ncch->LoadSectionExeFS("icon", buffer); } ResultStatus AppLoader_NCCH::ReadBanner(std::vector& buffer) { - return LoadSectionExeFS("banner", buffer); + return overlay_ncch->LoadSectionExeFS("banner", buffer); } ResultStatus AppLoader_NCCH::ReadLogo(std::vector& buffer) { - return LoadSectionExeFS("logo", buffer); + return overlay_ncch->LoadSectionExeFS("logo", buffer); } ResultStatus AppLoader_NCCH::ReadProgramId(u64& out_program_id) { - if (!file.IsOpen()) - return ResultStatus::Error; - - ResultStatus result = LoadExeFS(); + ResultStatus result = base_ncch.ReadProgramId(out_program_id); if (result != ResultStatus::Success) return result; - out_program_id = ncch_header.program_id; return ResultStatus::Success; } ResultStatus AppLoader_NCCH::ReadRomFS(std::shared_ptr& romfs_file, u64& offset, u64& size) { - if (!file.IsOpen()) - return ResultStatus::Error; - - // Check if the NCCH has a RomFS... - if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) { - u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000; - u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000; - - LOG_DEBUG(Loader, "RomFS offset: 0x%08X", romfs_offset); - LOG_DEBUG(Loader, "RomFS size: 0x%08X", romfs_size); - - if (file.GetSize() < romfs_offset + romfs_size) - return ResultStatus::Error; - - // We reopen the file, to allow its position to be independent from file's - romfs_file = std::make_shared(filepath, "rb"); - if (!romfs_file->IsOpen()) - return ResultStatus::Error; + return base_ncch.ReadRomFS(romfs_file, offset, size); +} - offset = romfs_offset; - size = romfs_size; +ResultStatus AppLoader_NCCH::ReadUpdateRomFS(std::shared_ptr& romfs_file, + u64& offset, u64& size) { + ResultStatus result = update_ncch.ReadRomFS(romfs_file, offset, size); - return ResultStatus::Success; - } - LOG_DEBUG(Loader, "NCCH has no RomFS"); - return ResultStatus::ErrorNotUsed; + if (result != ResultStatus::Success) + return base_ncch.ReadRomFS(romfs_file, offset, size); } ResultStatus AppLoader_NCCH::ReadTitle(std::string& title) { diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h index e40cef764..9b56465cb 100644 --- a/src/core/loader/ncch.h +++ b/src/core/loader/ncch.h @@ -5,154 +5,11 @@ #pragma once #include -#include "common/bit_field.h" #include "common/common_types.h" #include "common/swap.h" +#include "core/file_sys/ncch_container.h" #include "core/loader/loader.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; - u8 jump_id[8]; - 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"); - //////////////////////////////////////////////////////////////////////////////////////////////////// // Loader namespace @@ -162,7 +19,8 @@ namespace Loader { class AppLoader_NCCH final : public AppLoader { public: AppLoader_NCCH(FileUtil::IOFile&& file, const std::string& filepath) - : AppLoader(std::move(file)), filepath(filepath) {} + : AppLoader(std::move(file)), filepath(filepath), base_ncch(filepath), + overlay_ncch(&base_ncch) {} /** * Returns the type of the file @@ -196,48 +54,24 @@ public: ResultStatus ReadRomFS(std::shared_ptr& romfs_file, u64& offset, u64& size) override; + ResultStatus ReadUpdateRomFS(std::shared_ptr& romfs_file, u64& offset, + u64& size) override; + ResultStatus ReadTitle(std::string& title) override; private: - /** - * Reads an application ExeFS section of an NCCH file into AppLoader (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 - */ - ResultStatus LoadSectionExeFS(const char* name, std::vector& buffer); - /** * Loads .code section into memory for booting * @return ResultStatus result of function */ ResultStatus LoadExec(); - /** - * Ensure ExeFS is loaded and ready for reading sections - * @return ResultStatus result of function - */ - ResultStatus LoadExeFS(); - /// Reads the region lockout info in the SMDH and send it to CFG service void ParseRegionLockoutInfo(); - bool is_exefs_loaded = false; - bool is_compressed = false; - - u32 entry_point = 0; - u32 code_size = 0; - u32 stack_size = 0; - u32 bss_size = 0; - u32 core_version = 0; - u8 priority = 0; - u8 resource_limit_category = 0; - u32 ncch_offset = 0; // Offset to NCCH header, can be 0 or after NCSD header - u32 exefs_offset = 0; - - NCCH_Header ncch_header; - ExeFs_Header exefs_header; - ExHeader_Header exheader_header; + FileSys::NCCHContainer base_ncch; + FileSys::NCCHContainer update_ncch; + FileSys::NCCHContainer* overlay_ncch; std::string filepath; }; -- cgit v1.2.3 From 774e7deae8655a6f09530770c56ae2e75d55309b Mon Sep 17 00:00:00 2001 From: Subv Date: Sat, 23 Sep 2017 20:32:18 -0500 Subject: HLE/Archives: Allow multiple loaded applications to access their SelfNCCH archive independently. The loaders now register each loaded ROM with the SelfNCCH factory, which keeps the data around for the duration of the emulation session. When opening the SelfNCCH archive, the factory queries the current program's programid and uses that as a key to the map that contains the NCCHData structure (RomFS, Icon, Banner, etc). 3dsx files do not have a programid and will use a default of 0 for this value, thus, only 1 3dsx file with RomFS is loadable at the same time. --- src/core/loader/3dsx.cpp | 3 +-- src/core/loader/ncch.cpp | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) (limited to 'src/core/loader') diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp index a03515e6e..5ad5c5287 100644 --- a/src/core/loader/3dsx.cpp +++ b/src/core/loader/3dsx.cpp @@ -278,8 +278,7 @@ ResultStatus AppLoader_THREEDSX::Load() { Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE); - Service::FS::RegisterArchiveType(std::make_unique(*this), - Service::FS::ArchiveIdCode::SelfNCCH); + Service::FS::RegisterSelfNCCH(*this); is_loaded = true; return ResultStatus::Success; diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index c46d7cfc6..5107135f9 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -187,8 +187,7 @@ ResultStatus AppLoader_NCCH::Load() { if (ResultStatus::Success != result) return result; - Service::FS::RegisterArchiveType(std::make_unique(*this), - Service::FS::ArchiveIdCode::SelfNCCH); + Service::FS::RegisterSelfNCCH(*this); ParseRegionLockoutInfo(); -- cgit v1.2.3 From 7f48aa8d2580da6b3b83a389e31804e493aba69f Mon Sep 17 00:00:00 2001 From: Subv Date: Tue, 26 Sep 2017 18:17:47 -0500 Subject: Loaders: Don't automatically set the current process every time we load an application. The loaders will now just create a Kernel::Process, construct it and return it to the caller, which is responsible for setting it as the current process and configuring the global page table. --- src/core/loader/3dsx.cpp | 15 +++++++-------- src/core/loader/3dsx.h | 2 +- src/core/loader/elf.cpp | 15 +++++++-------- src/core/loader/elf.h | 2 +- src/core/loader/loader.h | 13 ++++++++----- src/core/loader/ncch.cpp | 19 +++++++++---------- src/core/loader/ncch.h | 5 +++-- 7 files changed, 36 insertions(+), 35 deletions(-) (limited to 'src/core/loader') diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp index 5ad5c5287..918038f1e 100644 --- a/src/core/loader/3dsx.cpp +++ b/src/core/loader/3dsx.cpp @@ -91,8 +91,8 @@ static u32 TranslateAddr(u32 addr, const THREEloadinfo* loadinfo, u32* offsets) return loadinfo->seg_addrs[2] + addr - offsets[1]; } -using Kernel::SharedPtr; using Kernel::CodeSet; +using Kernel::SharedPtr; static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr, SharedPtr* out_codeset) { @@ -255,7 +255,7 @@ FileType AppLoader_THREEDSX::IdentifyType(FileUtil::IOFile& file) { return FileType::Error; } -ResultStatus AppLoader_THREEDSX::Load() { +ResultStatus AppLoader_THREEDSX::Load(Kernel::SharedPtr& process) { if (is_loaded) return ResultStatus::ErrorAlreadyLoaded; @@ -267,16 +267,15 @@ ResultStatus AppLoader_THREEDSX::Load() { return ResultStatus::Error; codeset->name = filename; - Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); - Kernel::g_current_process->svc_access_mask.set(); - Kernel::g_current_process->address_mappings = default_address_mappings; - Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table); + process = Kernel::Process::Create(std::move(codeset)); + process->svc_access_mask.set(); + process->address_mappings = default_address_mappings; // Attach the default resource limit (APPLICATION) to the process - Kernel::g_current_process->resource_limit = + process->resource_limit = Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); - Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE); + process->Run(48, Kernel::DEFAULT_STACK_SIZE); Service::FS::RegisterSelfNCCH(*this); diff --git a/src/core/loader/3dsx.h b/src/core/loader/3dsx.h index 3f376778a..1e59bbb9d 100644 --- a/src/core/loader/3dsx.h +++ b/src/core/loader/3dsx.h @@ -31,7 +31,7 @@ public: return IdentifyType(file); } - ResultStatus Load() override; + ResultStatus Load(Kernel::SharedPtr& process) override; ResultStatus ReadIcon(std::vector& buffer) override; diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index 2de1f4e81..e36e42120 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp @@ -13,8 +13,8 @@ #include "core/loader/elf.h" #include "core/memory.h" -using Kernel::SharedPtr; using Kernel::CodeSet; +using Kernel::SharedPtr; //////////////////////////////////////////////////////////////////////////////////////////////////// // ELF Header Constants @@ -375,7 +375,7 @@ FileType AppLoader_ELF::IdentifyType(FileUtil::IOFile& file) { return FileType::Error; } -ResultStatus AppLoader_ELF::Load() { +ResultStatus AppLoader_ELF::Load(Kernel::SharedPtr& process) { if (is_loaded) return ResultStatus::ErrorAlreadyLoaded; @@ -394,16 +394,15 @@ ResultStatus AppLoader_ELF::Load() { SharedPtr codeset = elf_reader.LoadInto(Memory::PROCESS_IMAGE_VADDR); codeset->name = filename; - Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); - Kernel::g_current_process->svc_access_mask.set(); - Kernel::g_current_process->address_mappings = default_address_mappings; - Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table); + process = Kernel::Process::Create(std::move(codeset)); + process->svc_access_mask.set(); + process->address_mappings = default_address_mappings; // Attach the default resource limit (APPLICATION) to the process - Kernel::g_current_process->resource_limit = + process->resource_limit = Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); - Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE); + process->Run(48, Kernel::DEFAULT_STACK_SIZE); is_loaded = true; return ResultStatus::Success; diff --git a/src/core/loader/elf.h b/src/core/loader/elf.h index 862aa90d8..113da5917 100644 --- a/src/core/loader/elf.h +++ b/src/core/loader/elf.h @@ -30,7 +30,7 @@ public: return IdentifyType(file); } - ResultStatus Load() override; + ResultStatus Load(Kernel::SharedPtr& process) override; private: std::string filename; diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index 3160fd2fd..82b2be6a3 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h @@ -13,10 +13,12 @@ #include #include "common/common_types.h" #include "common/file_util.h" +#include "core/hle/kernel/kernel.h" namespace Kernel { struct AddressMapping; -} +class Process; +} // namespace Kernel //////////////////////////////////////////////////////////////////////////////////////////////////// // Loader namespace @@ -92,10 +94,11 @@ public: virtual FileType GetFileType() = 0; /** - * Load the application - * @return ResultStatus result of function + * Load the application and return the created Process instance + * @param process The newly created process. + * @return The status result of the operation. */ - virtual ResultStatus Load() = 0; + virtual ResultStatus Load(Kernel::SharedPtr& process) = 0; /** * Loads the system mode that this application needs. @@ -206,4 +209,4 @@ extern const std::initializer_list default_address_mappi */ std::unique_ptr GetLoader(const std::string& filename); -} // namespace +} // namespace Loader diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index 5107135f9..66bc5823d 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -67,9 +67,9 @@ std::pair, ResultStatus> AppLoader_NCCH::LoadKernelSystemMo ResultStatus::Success); } -ResultStatus AppLoader_NCCH::LoadExec() { - using Kernel::SharedPtr; +ResultStatus AppLoader_NCCH::LoadExec(Kernel::SharedPtr& process) { using Kernel::CodeSet; + using Kernel::SharedPtr; if (!is_loaded) return ResultStatus::ErrorNotLoaded; @@ -107,16 +107,15 @@ ResultStatus AppLoader_NCCH::LoadExec() { codeset->entrypoint = codeset->code.addr; codeset->memory = std::make_shared>(std::move(code)); - Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); - Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table); + process = Kernel::Process::Create(std::move(codeset)); // Attach a resource limit to the process based on the resource limit category - Kernel::g_current_process->resource_limit = + process->resource_limit = Kernel::ResourceLimit::GetForCategory(static_cast( overlay_ncch->exheader_header.arm11_system_local_caps.resource_limit_category)); // Set the default CPU core for this process - Kernel::g_current_process->ideal_processor = + process->ideal_processor = overlay_ncch->exheader_header.arm11_system_local_caps.ideal_processor; // Copy data while converting endianness @@ -124,11 +123,11 @@ ResultStatus AppLoader_NCCH::LoadExec() { kernel_caps; std::copy_n(overlay_ncch->exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(), begin(kernel_caps)); - Kernel::g_current_process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size()); + process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size()); s32 priority = overlay_ncch->exheader_header.arm11_system_local_caps.priority; u32 stack_size = overlay_ncch->exheader_header.codeset_info.stack_size; - Kernel::g_current_process->Run(priority, stack_size); + process->Run(priority, stack_size); return ResultStatus::Success; } return ResultStatus::Error; @@ -151,7 +150,7 @@ void AppLoader_NCCH::ParseRegionLockoutInfo() { } } -ResultStatus AppLoader_NCCH::Load() { +ResultStatus AppLoader_NCCH::Load(Kernel::SharedPtr& process) { u64_le ncch_program_id; if (is_loaded) @@ -183,7 +182,7 @@ ResultStatus AppLoader_NCCH::Load() { is_loaded = true; // Set state to loaded - result = LoadExec(); // Load the executable into memory for booting + result = LoadExec(process); // Load the executable into memory for booting if (ResultStatus::Success != result) return result; diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h index 9b56465cb..09230ae33 100644 --- a/src/core/loader/ncch.h +++ b/src/core/loader/ncch.h @@ -33,7 +33,7 @@ public: return IdentifyType(file); } - ResultStatus Load() override; + ResultStatus Load(Kernel::SharedPtr& process) override; /** * Loads the Exheader and returns the system mode for this application. @@ -62,9 +62,10 @@ public: private: /** * Loads .code section into memory for booting + * @param process The newly created process * @return ResultStatus result of function */ - ResultStatus LoadExec(); + ResultStatus LoadExec(Kernel::SharedPtr& process); /// Reads the region lockout info in the SMDH and send it to CFG service void ParseRegionLockoutInfo(); -- cgit v1.2.3 From 4887d1859102234c594c3140c31217ff64791f37 Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Sun, 1 Oct 2017 10:41:40 -0600 Subject: file_sys, loader: add support for reading TMDs to determine app paths --- src/core/loader/ncch.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/core/loader') diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index 66bc5823d..52686e364 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -14,6 +14,7 @@ #include "core/core.h" #include "core/file_sys/archive_selfncch.h" #include "core/file_sys/ncch_container.h" +#include "core/file_sys/title_metadata.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/resource_limit.h" #include "core/hle/service/cfg/cfg.h" @@ -49,9 +50,19 @@ static std::string GetUpdateNCCHPath(u64_le program_id) { u32 high = static_cast((program_id | UPDATE_MASK) >> 32); u32 low = static_cast((program_id | UPDATE_MASK) & 0xFFFFFFFF); - return Common::StringFromFormat("%sNintendo 3DS/%s/%s/title/%08x/%08x/content/00000000.app", - FileUtil::GetUserPath(D_SDMC_IDX).c_str(), SYSTEM_ID, SDCARD_ID, - high, low); + // TODO(shinyquagsire23): Title database should be doing this path lookup + std::string content_path = Common::StringFromFormat( + "%sNintendo 3DS/%s/%s/title/%08x/%08x/content/", FileUtil::GetUserPath(D_SDMC_IDX).c_str(), + SYSTEM_ID, SDCARD_ID, high, low); + std::string tmd_path = content_path + "00000000.tmd"; + + u32 content_id = 0; + FileSys::TitleMetadata tmd(tmd_path); + if (tmd.Load() == ResultStatus::Success) { + content_id = tmd.GetBootContentID(); + } + + return Common::StringFromFormat("%s%08x.app", content_path.c_str(), content_id); } std::pair, ResultStatus> AppLoader_NCCH::LoadKernelSystemMode() { -- cgit v1.2.3