aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/loader
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/loader')
-rw-r--r--src/core/loader/3dsx.cpp73
-rw-r--r--src/core/loader/3dsx.h2
-rw-r--r--src/core/loader/elf.cpp93
-rw-r--r--src/core/loader/elf.h2
-rw-r--r--src/core/loader/loader.cpp28
-rw-r--r--src/core/loader/loader.h29
-rw-r--r--src/core/loader/ncch.cpp87
-rw-r--r--src/core/loader/ncch.h19
8 files changed, 224 insertions, 109 deletions
diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp
index 14aeebeb..530837d0 100644
--- a/src/core/loader/3dsx.cpp
+++ b/src/core/loader/3dsx.cpp
@@ -19,7 +19,7 @@
namespace Loader {
-/**
+/*
* File layout:
* - File header
* - Code, rodata and data relocation table headers
@@ -39,13 +39,16 @@ namespace Loader {
* The entrypoint is always the start of the code segment.
* The BSS section must be cleared manually by the application.
*/
+
enum THREEDSX_Error {
ERROR_NONE = 0,
ERROR_READ = 1,
ERROR_FILE = 2,
ERROR_ALLOC = 3
};
+
static const u32 RELOCBUFSIZE = 512;
+static const unsigned int NUM_SEGMENTS = 3;
// File header
#pragma pack(1)
@@ -98,7 +101,10 @@ static u32 TranslateAddr(u32 addr, const THREEloadinfo *loadinfo, u32* offsets)
return loadinfo->seg_addrs[2] + addr - offsets[1];
}
-static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
+using Kernel::SharedPtr;
+using Kernel::CodeSet;
+
+static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr, SharedPtr<CodeSet>* out_codeset)
{
if (!file.IsOpen())
return ERROR_FILE;
@@ -116,15 +122,13 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
loadinfo.seg_sizes[1] = (hdr.rodata_seg_size + 0xFFF) &~0xFFF;
loadinfo.seg_sizes[2] = (hdr.data_seg_size + 0xFFF) &~0xFFF;
u32 offsets[2] = { loadinfo.seg_sizes[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] };
- u32 data_load_size = (hdr.data_seg_size - hdr.bss_size + 0xFFF) &~0xFFF;
- u32 bss_load_size = loadinfo.seg_sizes[2] - data_load_size;
- u32 n_reloc_tables = hdr.reloc_hdr_size / 4;
- std::vector<u8> all_mem(loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] + loadinfo.seg_sizes[2] + 3 * n_reloc_tables);
+ u32 n_reloc_tables = hdr.reloc_hdr_size / sizeof(u32);
+ std::vector<u8> program_image(loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] + loadinfo.seg_sizes[2]);
loadinfo.seg_addrs[0] = base_addr;
loadinfo.seg_addrs[1] = loadinfo.seg_addrs[0] + loadinfo.seg_sizes[0];
loadinfo.seg_addrs[2] = loadinfo.seg_addrs[1] + loadinfo.seg_sizes[1];
- loadinfo.seg_ptrs[0] = &all_mem[0];
+ loadinfo.seg_ptrs[0] = program_image.data();
loadinfo.seg_ptrs[1] = loadinfo.seg_ptrs[0] + loadinfo.seg_sizes[0];
loadinfo.seg_ptrs[2] = loadinfo.seg_ptrs[1] + loadinfo.seg_sizes[1];
@@ -132,10 +136,9 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
file.Seek(hdr.header_size, SEEK_SET);
// Read the relocation headers
- u32* relocs = (u32*)(loadinfo.seg_ptrs[2] + hdr.data_seg_size);
-
- for (unsigned current_segment : {0, 1, 2}) {
- size_t size = n_reloc_tables * 4;
+ std::vector<u32> relocs(n_reloc_tables * NUM_SEGMENTS);
+ for (unsigned int current_segment = 0; current_segment < NUM_SEGMENTS; ++current_segment) {
+ size_t size = n_reloc_tables * sizeof(u32);
if (file.ReadBytes(&relocs[current_segment * n_reloc_tables], size) != size)
return ERROR_READ;
}
@@ -152,7 +155,7 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
memset((char*)loadinfo.seg_ptrs[2] + hdr.data_seg_size - hdr.bss_size, 0, hdr.bss_size);
// Relocate the segments
- for (unsigned current_segment : {0, 1, 2}) {
+ for (unsigned int current_segment = 0; current_segment < NUM_SEGMENTS; ++current_segment) {
for (unsigned current_segment_reloc_table = 0; current_segment_reloc_table < n_reloc_tables; current_segment_reloc_table++) {
u32 n_relocs = relocs[current_segment * n_reloc_tables + current_segment_reloc_table];
if (current_segment_reloc_table >= 2) {
@@ -160,7 +163,7 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
file.Seek(n_relocs*sizeof(THREEDSX_Reloc), SEEK_CUR);
continue;
}
- static THREEDSX_Reloc reloc_table[RELOCBUFSIZE];
+ THREEDSX_Reloc reloc_table[RELOCBUFSIZE];
u32* pos = (u32*)loadinfo.seg_ptrs[current_segment];
const u32* end_pos = pos + (loadinfo.seg_sizes[current_segment] / 4);
@@ -179,7 +182,7 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
pos += table.skip;
s32 num_patches = table.patch;
while (0 < num_patches && pos < end_pos) {
- u32 in_addr = (char*)pos - (char*)&all_mem[0];
+ u32 in_addr = (u8*)pos - program_image.data();
u32 addr = TranslateAddr(*pos, &loadinfo, offsets);
LOG_TRACE(Loader, "Patching %08X <-- rel(%08X,%d) (%08X)\n",
base_addr + in_addr, addr, current_segment_reloc_table, *pos);
@@ -188,7 +191,7 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
*pos = (addr);
break;
case 1:
- *pos = (addr - in_addr);
+ *pos = static_cast<u32>(addr - in_addr);
break;
default:
break; //this should never happen
@@ -201,14 +204,29 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
}
}
- // Write the data
- memcpy(Memory::GetPointer(base_addr), &all_mem[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] + loadinfo.seg_sizes[2]);
+ // Create the CodeSet
+ SharedPtr<CodeSet> code_set = CodeSet::Create("", 0);
+
+ code_set->code.offset = loadinfo.seg_ptrs[0] - program_image.data();
+ code_set->code.addr = loadinfo.seg_addrs[0];
+ code_set->code.size = loadinfo.seg_sizes[0];
+
+ code_set->rodata.offset = loadinfo.seg_ptrs[1] - program_image.data();
+ code_set->rodata.addr = loadinfo.seg_addrs[1];
+ code_set->rodata.size = loadinfo.seg_sizes[1];
- LOG_DEBUG(Loader, "CODE: %u pages\n", loadinfo.seg_sizes[0] / 0x1000);
- LOG_DEBUG(Loader, "RODATA: %u pages\n", loadinfo.seg_sizes[1] / 0x1000);
- LOG_DEBUG(Loader, "DATA: %u pages\n", data_load_size / 0x1000);
- LOG_DEBUG(Loader, "BSS: %u pages\n", bss_load_size / 0x1000);
+ code_set->data.offset = loadinfo.seg_ptrs[2] - program_image.data();
+ code_set->data.addr = loadinfo.seg_addrs[2];
+ code_set->data.size = loadinfo.seg_sizes[2];
+ code_set->entrypoint = code_set->code.addr;
+ code_set->memory = std::make_shared<std::vector<u8>>(std::move(program_image));
+
+ LOG_DEBUG(Loader, "code size: 0x%X", loadinfo.seg_sizes[0]);
+ LOG_DEBUG(Loader, "rodata size: 0x%X", loadinfo.seg_sizes[1]);
+ LOG_DEBUG(Loader, "data size: 0x%X (including 0x%X of bss)", loadinfo.seg_sizes[2], hdr.bss_size);
+
+ *out_codeset = code_set;
return ERROR_NONE;
}
@@ -228,19 +246,22 @@ ResultStatus AppLoader_THREEDSX::Load() {
if (is_loaded)
return ResultStatus::ErrorAlreadyLoaded;
- if (!file->IsOpen())
+ if (!file.IsOpen())
+ return ResultStatus::Error;
+
+ SharedPtr<CodeSet> codeset;
+ if (Load3DSXFile(file, Memory::PROCESS_IMAGE_VADDR, &codeset) != ERROR_NONE)
return ResultStatus::Error;
+ codeset->name = filename;
- Kernel::g_current_process = Kernel::Process::Create(filename, 0);
+ 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;
// Attach the default resource limit (APPLICATION) to the process
Kernel::g_current_process->resource_limit = Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
- Load3DSXFile(*file, Memory::PROCESS_IMAGE_VADDR);
-
- Kernel::g_current_process->Run(Memory::PROCESS_IMAGE_VADDR, 48, Kernel::DEFAULT_STACK_SIZE);
+ Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE);
is_loaded = true;
return ResultStatus::Success;
diff --git a/src/core/loader/3dsx.h b/src/core/loader/3dsx.h
index 096b3ec2..a0aa0c53 100644
--- a/src/core/loader/3dsx.h
+++ b/src/core/loader/3dsx.h
@@ -17,7 +17,7 @@ namespace Loader {
/// Loads an 3DSX file
class AppLoader_THREEDSX final : public AppLoader {
public:
- AppLoader_THREEDSX(std::unique_ptr<FileUtil::IOFile>&& file, std::string filename)
+ AppLoader_THREEDSX(FileUtil::IOFile&& file, std::string filename)
: AppLoader(std::move(file)), filename(std::move(filename)) {}
/**
diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp
index f00753a7..5d7264f1 100644
--- a/src/core/loader/elf.cpp
+++ b/src/core/loader/elf.cpp
@@ -2,6 +2,7 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
+#include <cstring>
#include <string>
#include <memory>
@@ -10,11 +11,14 @@
#include "common/logging/log.h"
#include "common/symbols.h"
-#include "core/hle/kernel/kernel.h"
+#include "core/hle/kernel/process.h"
#include "core/hle/kernel/resource_limit.h"
#include "core/loader/elf.h"
#include "core/memory.h"
+using Kernel::SharedPtr;
+using Kernel::CodeSet;
+
////////////////////////////////////////////////////////////////////////////////////////////////////
// ELF Header Constants
@@ -96,6 +100,12 @@ enum ElfSectionFlags
#define PT_LOPROC 0x70000000
#define PT_HIPROC 0x7FFFFFFF
+// Segment flags
+#define PF_X 0x1
+#define PF_W 0x2
+#define PF_R 0x4
+#define PF_MASKPROC 0xF0000000
+
typedef unsigned int Elf32_Addr;
typedef unsigned short Elf32_Half;
typedef unsigned int Elf32_Off;
@@ -192,7 +202,7 @@ public:
ElfMachine GetMachine() const { return (ElfMachine)(header->e_machine); }
u32 GetEntryPoint() const { return entryPoint; }
u32 GetFlags() const { return (u32)(header->e_flags); }
- void LoadInto(u32 vaddr);
+ SharedPtr<CodeSet> LoadInto(u32 vaddr);
bool LoadSymbols();
int GetNumSegments() const { return (int)(header->e_phnum); }
@@ -248,7 +258,7 @@ const char *ElfReader::GetSectionName(int section) const {
return nullptr;
}
-void ElfReader::LoadInto(u32 vaddr) {
+SharedPtr<CodeSet> ElfReader::LoadInto(u32 vaddr) {
LOG_DEBUG(Loader, "String section: %i", header->e_shstrndx);
// Should we relocate?
@@ -263,22 +273,63 @@ void ElfReader::LoadInto(u32 vaddr) {
LOG_DEBUG(Loader, "%i segments:", header->e_phnum);
// First pass : Get the bits into RAM
- u32 segment_addr[32];
u32 base_addr = relocate ? vaddr : 0;
- for (unsigned i = 0; i < header->e_phnum; i++) {
- Elf32_Phdr* p = segments + i;
- LOG_DEBUG(Loader, "Type: %i Vaddr: %08x Filesz: %i Memsz: %i ", p->p_type, p->p_vaddr,
+ u32 total_image_size = 0;
+ for (unsigned int i = 0; i < header->e_phnum; ++i) {
+ Elf32_Phdr* p = &segments[i];
+ if (p->p_type == PT_LOAD) {
+ total_image_size += (p->p_memsz + 0xFFF) & ~0xFFF;
+ }
+ }
+
+ std::vector<u8> program_image(total_image_size);
+ size_t current_image_position = 0;
+
+ SharedPtr<CodeSet> codeset = CodeSet::Create("", 0);
+
+ for (unsigned int i = 0; i < header->e_phnum; ++i) {
+ Elf32_Phdr* p = &segments[i];
+ LOG_DEBUG(Loader, "Type: %i Vaddr: %08X Filesz: %8X Memsz: %8X ", p->p_type, p->p_vaddr,
p->p_filesz, p->p_memsz);
if (p->p_type == PT_LOAD) {
- segment_addr[i] = base_addr + p->p_vaddr;
- memcpy(Memory::GetPointer(segment_addr[i]), GetSegmentPtr(i), p->p_filesz);
- LOG_DEBUG(Loader, "Loadable Segment Copied to %08x, size %08x", segment_addr[i],
- p->p_memsz);
+ CodeSet::Segment* codeset_segment;
+ u32 permission_flags = p->p_flags & (PF_R | PF_W | PF_X);
+ if (permission_flags == (PF_R | PF_X)) {
+ codeset_segment = &codeset->code;
+ } else if (permission_flags == (PF_R)) {
+ codeset_segment = &codeset->rodata;
+ } else if (permission_flags == (PF_R | PF_W)) {
+ codeset_segment = &codeset->data;
+ } else {
+ LOG_ERROR(Loader, "Unexpected ELF PT_LOAD segment id %u with flags %X", i, p->p_flags);
+ continue;
+ }
+
+ if (codeset_segment->size != 0) {
+ LOG_ERROR(Loader, "ELF has more than one segment of the same type. Skipping extra segment (id %i)", i);
+ continue;
+ }
+
+ u32 segment_addr = base_addr + p->p_vaddr;
+ u32 aligned_size = (p->p_memsz + 0xFFF) & ~0xFFF;
+
+ codeset_segment->offset = current_image_position;
+ codeset_segment->addr = segment_addr;
+ codeset_segment->size = aligned_size;
+
+ memcpy(&program_image[current_image_position], GetSegmentPtr(i), p->p_filesz);
+ current_image_position += aligned_size;
}
}
+
+ codeset->entrypoint = base_addr + header->e_entry;
+ codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image));
+
LOG_DEBUG(Loader, "Done loading.");
+
+ return codeset;
}
SectionID ElfReader::GetSectionByName(const char *name, int firstSection) const {
@@ -340,29 +391,29 @@ ResultStatus AppLoader_ELF::Load() {
if (is_loaded)
return ResultStatus::ErrorAlreadyLoaded;
- if (!file->IsOpen())
+ if (!file.IsOpen())
return ResultStatus::Error;
// Reset read pointer in case this file has been read before.
- file->Seek(0, SEEK_SET);
+ file.Seek(0, SEEK_SET);
- u32 size = static_cast<u32>(file->GetSize());
+ size_t size = file.GetSize();
std::unique_ptr<u8[]> buffer(new u8[size]);
- if (file->ReadBytes(&buffer[0], size) != size)
+ if (file.ReadBytes(&buffer[0], size) != size)
return ResultStatus::Error;
- Kernel::g_current_process = Kernel::Process::Create(filename, 0);
+ ElfReader elf_reader(&buffer[0]);
+ SharedPtr<CodeSet> 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;
// Attach the default resource limit (APPLICATION) to the process
Kernel::g_current_process->resource_limit = Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
- ElfReader elf_reader(&buffer[0]);
- elf_reader.LoadInto(Memory::PROCESS_IMAGE_VADDR);
- // TODO: Fill application title
-
- Kernel::g_current_process->Run(elf_reader.GetEntryPoint(), 48, Kernel::DEFAULT_STACK_SIZE);
+ Kernel::g_current_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 32841606..c6a5ebe9 100644
--- a/src/core/loader/elf.h
+++ b/src/core/loader/elf.h
@@ -17,7 +17,7 @@ namespace Loader {
/// Loads an ELF/AXF file
class AppLoader_ELF final : public AppLoader {
public:
- AppLoader_ELF(std::unique_ptr<FileUtil::IOFile>&& file, std::string filename)
+ AppLoader_ELF(FileUtil::IOFile&& file, std::string filename)
: AppLoader(std::move(file)), filename(std::move(filename)) { }
/**
diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp
index 8b14edf0..f5b349a7 100644
--- a/src/core/loader/loader.cpp
+++ b/src/core/loader/loader.cpp
@@ -2,10 +2,12 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
+#include <memory>
#include <string>
#include "common/logging/log.h"
#include "common/make_unique.h"
+#include "common/string_util.h"
#include "core/file_sys/archive_romfs.h"
#include "core/hle/kernel/process.h"
@@ -54,18 +56,18 @@ static FileType IdentifyFile(FileUtil::IOFile& file) {
static FileType GuessFromExtension(const std::string& extension_) {
std::string extension = Common::ToLower(extension_);
- if (extension == ".elf")
+ if (extension == ".elf" || extension == ".axf")
return FileType::ELF;
- else if (extension == ".axf")
- return FileType::ELF;
- else if (extension == ".cxi")
- return FileType::CXI;
- else if (extension == ".cci")
- return FileType::CCI;
- else if (extension == ".3ds")
+
+ if (extension == ".cci" || extension == ".3ds")
return FileType::CCI;
- else if (extension == ".3dsx")
+
+ if (extension == ".cxi")
+ return FileType::CXI;
+
+ if (extension == ".3dsx")
return FileType::THREEDSX;
+
return FileType::Unknown;
}
@@ -88,8 +90,8 @@ static const char* GetFileTypeString(FileType type) {
}
ResultStatus LoadFile(const std::string& filename) {
- std::unique_ptr<FileUtil::IOFile> file(new FileUtil::IOFile(filename, "rb"));
- if (!file->IsOpen()) {
+ FileUtil::IOFile file(filename, "rb");
+ if (!file.IsOpen()) {
LOG_ERROR(Loader, "Failed to load file %s", filename.c_str());
return ResultStatus::Error;
}
@@ -97,7 +99,7 @@ ResultStatus LoadFile(const std::string& filename) {
std::string filename_filename, filename_extension;
Common::SplitPath(filename, nullptr, &filename_filename, &filename_extension);
- FileType type = IdentifyFile(*file);
+ FileType type = IdentifyFile(file);
FileType filename_type = GuessFromExtension(filename_extension);
if (type != filename_type) {
@@ -122,7 +124,7 @@ ResultStatus LoadFile(const std::string& filename) {
case FileType::CXI:
case FileType::CCI:
{
- AppLoader_NCCH app_loader(std::move(file));
+ AppLoader_NCCH app_loader(std::move(file), filename);
// Load application and RomFS
if (ResultStatus::Success == app_loader.Load()) {
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h
index 87e16fb9..a37d3348 100644
--- a/src/core/loader/loader.h
+++ b/src/core/loader/loader.h
@@ -4,12 +4,18 @@
#pragma once
+#include <algorithm>
+#include <initializer_list>
+#include <memory>
+#include <string>
#include <vector>
#include "common/common_types.h"
#include "common/file_util.h"
-#include "core/hle/kernel/process.h"
+namespace Kernel {
+struct AddressMapping;
+}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Loader namespace
@@ -46,7 +52,7 @@ static inline u32 MakeMagic(char a, char b, char c, char d) {
/// Interface for loading an application
class AppLoader : NonCopyable {
public:
- AppLoader(std::unique_ptr<FileUtil::IOFile>&& file) : file(std::move(file)) { }
+ AppLoader(FileUtil::IOFile&& file) : file(std::move(file)) { }
virtual ~AppLoader() { }
/**
@@ -60,7 +66,7 @@ public:
* @param buffer Reference to buffer to store data
* @return ResultStatus result of function
*/
- virtual ResultStatus ReadCode(std::vector<u8>& buffer) const {
+ virtual ResultStatus ReadCode(std::vector<u8>& buffer) {
return ResultStatus::ErrorNotImplemented;
}
@@ -69,7 +75,7 @@ public:
* @param buffer Reference to buffer to store data
* @return ResultStatus result of function
*/
- virtual ResultStatus ReadIcon(std::vector<u8>& buffer) const {
+ virtual ResultStatus ReadIcon(std::vector<u8>& buffer) {
return ResultStatus::ErrorNotImplemented;
}
@@ -78,7 +84,7 @@ public:
* @param buffer Reference to buffer to store data
* @return ResultStatus result of function
*/
- virtual ResultStatus ReadBanner(std::vector<u8>& buffer) const {
+ virtual ResultStatus ReadBanner(std::vector<u8>& buffer) {
return ResultStatus::ErrorNotImplemented;
}
@@ -87,22 +93,25 @@ public:
* @param buffer Reference to buffer to store data
* @return ResultStatus result of function
*/
- virtual ResultStatus ReadLogo(std::vector<u8>& buffer) const {
+ virtual ResultStatus ReadLogo(std::vector<u8>& buffer) {
return ResultStatus::ErrorNotImplemented;
}
/**
* Get the RomFS of the application
- * @param buffer Reference to buffer to store data
+ * 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 ReadRomFS(std::vector<u8>& buffer) const {
+ virtual ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, u64& size) {
return ResultStatus::ErrorNotImplemented;
}
protected:
- std::unique_ptr<FileUtil::IOFile> file;
- bool is_loaded = false;
+ FileUtil::IOFile file;
+ bool is_loaded = false;
};
/**
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp
index 08993c4f..094d7410 100644
--- a/src/core/loader/ncch.cpp
+++ b/src/core/loader/ncch.cpp
@@ -3,6 +3,7 @@
// Refer to the license.txt file included.
#include <algorithm>
+#include <cstring>
#include <memory>
#include "common/logging/log.h"
@@ -10,7 +11,7 @@
#include "common/string_util.h"
#include "common/swap.h"
-#include "core/hle/kernel/kernel.h"
+#include "core/hle/kernel/process.h"
#include "core/hle/kernel/resource_limit.h"
#include "core/loader/ncch.h"
#include "core/memory.h"
@@ -116,7 +117,10 @@ FileType AppLoader_NCCH::IdentifyType(FileUtil::IOFile& file) {
return FileType::Error;
}
-ResultStatus AppLoader_NCCH::LoadExec() const {
+ResultStatus AppLoader_NCCH::LoadExec() {
+ using Kernel::SharedPtr;
+ using Kernel::CodeSet;
+
if (!is_loaded)
return ResultStatus::ErrorNotLoaded;
@@ -125,7 +129,30 @@ ResultStatus AppLoader_NCCH::LoadExec() const {
std::string process_name = Common::StringFromFixedZeroTerminatedBuffer(
(const char*)exheader_header.codeset_info.name, 8);
u64 program_id = *reinterpret_cast<u64_le const*>(&ncch_header.program_id[0]);
- Kernel::g_current_process = Kernel::Process::Create(process_name, program_id);
+
+ SharedPtr<CodeSet> 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->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;
+
+ // 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;
+ 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.size = 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::vector<u8>>(std::move(code));
+
+ Kernel::g_current_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 = Kernel::ResourceLimit::GetForCategory(
@@ -136,18 +163,16 @@ ResultStatus AppLoader_NCCH::LoadExec() const {
std::copy_n(exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(), begin(kernel_caps));
Kernel::g_current_process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size());
- Memory::WriteBlock(entry_point, &code[0], code.size());
-
s32 priority = exheader_header.arm11_system_local_caps.priority;
u32 stack_size = exheader_header.codeset_info.stack_size;
- Kernel::g_current_process->Run(entry_point, priority, 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<u8>& buffer) const {
- if (!file->IsOpen())
+ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) {
+ if (!file.IsOpen())
return ResultStatus::Error;
LOG_DEBUG(Loader, "%d sections:", kMaxSections);
@@ -161,7 +186,7 @@ ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>&
section.offset, section.size, section.name);
s64 section_offset = (section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset);
- file->Seek(section_offset, SEEK_SET);
+ file.Seek(section_offset, SEEK_SET);
if (is_compressed) {
// Section is compressed, read compressed .code section...
@@ -172,7 +197,7 @@ ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>&
return ResultStatus::ErrorMemoryAllocationFailed;
}
- if (file->ReadBytes(&temp_buffer[0], section.size) != section.size)
+ if (file.ReadBytes(&temp_buffer[0], section.size) != section.size)
return ResultStatus::Error;
// Decompress .code section...
@@ -183,7 +208,7 @@ ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>&
} else {
// Section is uncompressed...
buffer.resize(section.size);
- if (file->ReadBytes(&buffer[0], section.size) != section.size)
+ if (file.ReadBytes(&buffer[0], section.size) != section.size)
return ResultStatus::Error;
}
return ResultStatus::Success;
@@ -196,21 +221,21 @@ ResultStatus AppLoader_NCCH::Load() {
if (is_loaded)
return ResultStatus::ErrorAlreadyLoaded;
- if (!file->IsOpen())
+ if (!file.IsOpen())
return ResultStatus::Error;
// Reset read pointer in case this file has been read before.
- file->Seek(0, SEEK_SET);
+ file.Seek(0, SEEK_SET);
- if (file->ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header))
+ 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_WARNING(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));
+ file.Seek(ncch_offset, SEEK_SET);
+ file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
}
// Verify we are loading the correct file type...
@@ -219,7 +244,7 @@ ResultStatus AppLoader_NCCH::Load() {
// Read ExHeader...
- if (file->ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != sizeof(ExHeader_Header))
+ if (file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != sizeof(ExHeader_Header))
return ResultStatus::Error;
is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1;
@@ -239,7 +264,6 @@ ResultStatus AppLoader_NCCH::Load() {
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 descriptor: 0x%08X", exheader_header.arm11_system_local_caps.resource_limit_descriptor);
LOG_DEBUG(Loader, "Resource limit category: %d" , resource_limit_category);
// Read ExeFS...
@@ -250,8 +274,8 @@ ResultStatus AppLoader_NCCH::Load() {
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))
+ file.Seek(exefs_offset + ncch_offset, SEEK_SET);
+ if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header))
return ResultStatus::Error;
is_loaded = true; // Set state to loaded
@@ -259,24 +283,24 @@ ResultStatus AppLoader_NCCH::Load() {
return LoadExec(); // Load the executable into memory for booting
}
-ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) const {
+ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) {
return LoadSectionExeFS(".code", buffer);
}
-ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) const {
+ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) {
return LoadSectionExeFS("icon", buffer);
}
-ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) const {
+ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) {
return LoadSectionExeFS("banner", buffer);
}
-ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) const {
+ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) {
return LoadSectionExeFS("logo", buffer);
}
-ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const {
- if (!file->IsOpen())
+ResultStatus AppLoader_NCCH::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, u64& size) {
+ if (!file.IsOpen())
return ResultStatus::Error;
// Check if the NCCH has a RomFS...
@@ -287,12 +311,17 @@ ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const {
LOG_DEBUG(Loader, "RomFS offset: 0x%08X", romfs_offset);
LOG_DEBUG(Loader, "RomFS size: 0x%08X", romfs_size);
- buffer.resize(romfs_size);
+ if (file.GetSize () < romfs_offset + romfs_size)
+ return ResultStatus::Error;
- file->Seek(romfs_offset, SEEK_SET);
- if (file->ReadBytes(&buffer[0], romfs_size) != romfs_size)
+ // 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 ResultStatus::Error;
+ offset = romfs_offset;
+ size = romfs_size;
+
return ResultStatus::Success;
}
LOG_DEBUG(Loader, "NCCH has no RomFS");
diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h
index 29e39d2c..b4374a47 100644
--- a/src/core/loader/ncch.h
+++ b/src/core/loader/ncch.h
@@ -163,7 +163,8 @@ namespace Loader {
/// Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI)
class AppLoader_NCCH final : public AppLoader {
public:
- AppLoader_NCCH(std::unique_ptr<FileUtil::IOFile>&& file) : AppLoader(std::move(file)) { }
+ AppLoader_NCCH(FileUtil::IOFile&& file, const std::string& filepath)
+ : AppLoader(std::move(file)), filepath(filepath) { }
/**
* Returns the type of the file
@@ -183,35 +184,35 @@ public:
* @param buffer Reference to buffer to store data
* @return ResultStatus result of function
*/
- ResultStatus ReadCode(std::vector<u8>& buffer) const override;
+ ResultStatus ReadCode(std::vector<u8>& buffer) override;
/**
* Get the icon (typically icon section) of the application
* @param buffer Reference to buffer to store data
* @return ResultStatus result of function
*/
- ResultStatus ReadIcon(std::vector<u8>& buffer) const override;
+ ResultStatus ReadIcon(std::vector<u8>& buffer) override;
/**
* Get the banner (typically banner section) of the application
* @param buffer Reference to buffer to store data
* @return ResultStatus result of function
*/
- ResultStatus ReadBanner(std::vector<u8>& buffer) const override;
+ ResultStatus ReadBanner(std::vector<u8>& buffer) override;
/**
* Get the logo (typically logo section) of the application
* @param buffer Reference to buffer to store data
* @return ResultStatus result of function
*/
- ResultStatus ReadLogo(std::vector<u8>& buffer) const override;
+ ResultStatus ReadLogo(std::vector<u8>& buffer) override;
/**
* Get the RomFS of the application
* @param buffer Reference to buffer to store data
* @return ResultStatus result of function
*/
- ResultStatus ReadRomFS(std::vector<u8>& buffer) const override;
+ ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, u64& size) override;
private:
@@ -221,13 +222,13 @@ private:
* @param buffer Vector to read data into
* @return ResultStatus result of function
*/
- ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const;
+ ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer);
/**
* Loads .code section into memory for booting
* @return ResultStatus result of function
*/
- ResultStatus LoadExec() const;
+ ResultStatus LoadExec();
bool is_compressed = false;
@@ -244,6 +245,8 @@ private:
NCCH_Header ncch_header;
ExeFs_Header exefs_header;
ExHeader_Header exheader_header;
+
+ std::string filepath;
};
} // namespace Loader