diff options
Diffstat (limited to 'src/core/loader')
-rw-r--r-- | src/core/loader/3dsx.cpp | 16 | ||||
-rw-r--r-- | src/core/loader/3dsx.h | 8 | ||||
-rw-r--r-- | src/core/loader/elf.cpp | 17 | ||||
-rw-r--r-- | src/core/loader/elf.h | 8 | ||||
-rw-r--r-- | src/core/loader/loader.cpp | 50 | ||||
-rw-r--r-- | src/core/loader/loader.h | 11 | ||||
-rw-r--r-- | src/core/loader/ncch.cpp | 62 | ||||
-rw-r--r-- | src/core/loader/ncch.h | 58 |
8 files changed, 146 insertions, 84 deletions
diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp index 958dd03e8..84b13ee52 100644 --- a/src/core/loader/3dsx.cpp +++ b/src/core/loader/3dsx.cpp @@ -5,11 +5,14 @@ #include <algorithm> #include <vector> +#include "common/logging/log.h" + #include "core/file_sys/archive_romfs.h" +#include "core/hle/kernel/process.h" +#include "core/hle/service/fs/archive.h" #include "core/loader/elf.h" #include "core/loader/ncch.h" -#include "core/hle/service/fs/archive.h" -#include "core/mem_map.h" +#include "core/memory.h" #include "3dsx.h" @@ -227,8 +230,13 @@ ResultStatus AppLoader_THREEDSX::Load() { if (!file->IsOpen()) return ResultStatus::Error; - Load3DSXFile(*file, 0x00100000); - Kernel::LoadExec(0x00100000); + Kernel::g_current_process = Kernel::Process::Create(filename, 0); + Kernel::g_current_process->svc_access_mask.set(); + Kernel::g_current_process->address_mappings = default_address_mappings; + + Load3DSXFile(*file, Memory::PROCESS_IMAGE_VADDR); + + Kernel::g_current_process->Run(Memory::PROCESS_IMAGE_VADDR, 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 a11667400..096b3ec20 100644 --- a/src/core/loader/3dsx.h +++ b/src/core/loader/3dsx.h @@ -4,6 +4,8 @@ #pragma once +#include <string> + #include "common/common_types.h" #include "core/loader/loader.h" @@ -15,7 +17,8 @@ namespace Loader { /// Loads an 3DSX file class AppLoader_THREEDSX final : public AppLoader { public: - AppLoader_THREEDSX(std::unique_ptr<FileUtil::IOFile>&& file) : AppLoader(std::move(file)) { } + AppLoader_THREEDSX(std::unique_ptr<FileUtil::IOFile>&& file, std::string filename) + : AppLoader(std::move(file)), filename(std::move(filename)) {} /** * Returns the type of the file @@ -29,6 +32,9 @@ public: * @return ResultStatus result of function */ ResultStatus Load() override; + +private: + std::string filename; }; } // namespace Loader diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index 773eaf771..a951bc80f 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp @@ -5,13 +5,14 @@ #include <string> #include <memory> -#include "common/common.h" +#include "common/common_types.h" #include "common/file_util.h" +#include "common/logging/log.h" #include "common/symbols.h" -#include "core/mem_map.h" -#include "core/loader/elf.h" #include "core/hle/kernel/kernel.h" +#include "core/loader/elf.h" +#include "core/memory.h" //////////////////////////////////////////////////////////////////////////////////////////////////// // ELF Header Constants @@ -349,9 +350,15 @@ ResultStatus AppLoader_ELF::Load() { if (file->ReadBytes(&buffer[0], size) != size) return ResultStatus::Error; + Kernel::g_current_process = Kernel::Process::Create(filename, 0); + Kernel::g_current_process->svc_access_mask.set(); + Kernel::g_current_process->address_mappings = default_address_mappings; + ElfReader elf_reader(&buffer[0]); - elf_reader.LoadInto(0x00100000); - Kernel::LoadExec(elf_reader.GetEntryPoint()); + elf_reader.LoadInto(Memory::PROCESS_IMAGE_VADDR); + // TODO: Fill application title + + Kernel::g_current_process->Run(elf_reader.GetEntryPoint(), 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 b6e6651f5..32841606a 100644 --- a/src/core/loader/elf.h +++ b/src/core/loader/elf.h @@ -4,6 +4,8 @@ #pragma once +#include <string> + #include "common/common_types.h" #include "core/loader/loader.h" @@ -15,7 +17,8 @@ namespace Loader { /// Loads an ELF/AXF file class AppLoader_ELF final : public AppLoader { public: - AppLoader_ELF(std::unique_ptr<FileUtil::IOFile>&& file) : AppLoader(std::move(file)) { } + AppLoader_ELF(std::unique_ptr<FileUtil::IOFile>&& file, std::string filename) + : AppLoader(std::move(file)), filename(std::move(filename)) { } /** * Returns the type of the file @@ -29,6 +32,9 @@ public: * @return ResultStatus result of function */ ResultStatus Load() override; + +private: + std::string filename; }; } // namespace Loader diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index aca09b374..8b14edf00 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp @@ -4,19 +4,26 @@ #include <string> +#include "common/logging/log.h" #include "common/make_unique.h" #include "core/file_sys/archive_romfs.h" +#include "core/hle/kernel/process.h" +#include "core/hle/service/fs/archive.h" #include "core/loader/3dsx.h" #include "core/loader/elf.h" #include "core/loader/ncch.h" -#include "core/hle/service/fs/archive.h" -#include "core/mem_map.h" //////////////////////////////////////////////////////////////////////////////////////////////////// namespace Loader { +const std::initializer_list<Kernel::AddressMapping> default_address_mappings = { + { 0x1FF50000, 0x8000, true }, // part of DSP RAM + { 0x1FF70000, 0x8000, true }, // part of DSP RAM + { 0x1F000000, 0x600000, false }, // entire VRAM +}; + /** * Identifies the type of a bootable file * @param file open file @@ -41,19 +48,11 @@ static FileType IdentifyFile(FileUtil::IOFile& file) { /** * Guess the type of a bootable file from its extension - * @param filename String filename of bootable file + * @param extension String extension of bootable file * @return FileType of file */ -static FileType GuessFromFilename(const std::string& filename) { - if (filename.size() == 0) { - LOG_ERROR(Loader, "invalid filename %s", filename.c_str()); - return FileType::Error; - } - - size_t extension_loc = filename.find_last_of('.'); - if (extension_loc == std::string::npos) - return FileType::Unknown; - std::string extension = Common::ToLower(filename.substr(extension_loc)); +static FileType GuessFromExtension(const std::string& extension_) { + std::string extension = Common::ToLower(extension_); if (extension == ".elf") return FileType::ELF; @@ -63,8 +62,6 @@ static FileType GuessFromFilename(const std::string& filename) { return FileType::CXI; else if (extension == ".cci") return FileType::CCI; - else if (extension == ".bin") - return FileType::BIN; else if (extension == ".3ds") return FileType::CCI; else if (extension == ".3dsx") @@ -82,8 +79,6 @@ static const char* GetFileTypeString(FileType type) { return "ELF"; case FileType::THREEDSX: return "3DSX"; - case FileType::BIN: - return "raw"; case FileType::Error: case FileType::Unknown: break; @@ -99,8 +94,11 @@ ResultStatus LoadFile(const std::string& filename) { return ResultStatus::Error; } + std::string filename_filename, filename_extension; + Common::SplitPath(filename, nullptr, &filename_filename, &filename_extension); + FileType type = IdentifyFile(*file); - FileType filename_type = GuessFromFilename(filename); + FileType filename_type = GuessFromExtension(filename_extension); if (type != filename_type) { LOG_WARNING(Loader, "File %s has a different type than its extension.", filename.c_str()); @@ -114,11 +112,11 @@ ResultStatus LoadFile(const std::string& filename) { //3DSX file format... case FileType::THREEDSX: - return AppLoader_THREEDSX(std::move(file)).Load(); + return AppLoader_THREEDSX(std::move(file), filename_filename).Load(); // Standard ELF file format... case FileType::ELF: - return AppLoader_ELF(std::move(file)).Load(); + return AppLoader_ELF(std::move(file), filename_filename).Load(); // NCCH/NCSD container formats... case FileType::CXI: @@ -128,24 +126,12 @@ ResultStatus LoadFile(const std::string& filename) { // Load application and RomFS if (ResultStatus::Success == app_loader.Load()) { - Kernel::g_program_id = app_loader.GetProgramId(); Service::FS::RegisterArchiveType(Common::make_unique<FileSys::ArchiveFactory_RomFS>(app_loader), Service::FS::ArchiveIdCode::RomFS); return ResultStatus::Success; } break; } - // Raw BIN file format... - case FileType::BIN: - { - size_t size = (size_t)file->GetSize(); - if (file->ReadBytes(Memory::GetPointer(Memory::EXEFS_CODE_VADDR), size) != size) - return ResultStatus::Error; - - Kernel::LoadExec(Memory::EXEFS_CODE_VADDR); - return ResultStatus::Success; - } - // Error occurred durring IdentifyFile... case FileType::Error: diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index 3510c6b28..87e16fb98 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h @@ -6,9 +6,11 @@ #include <vector> -#include "common/common.h" +#include "common/common_types.h" #include "common/file_util.h" +#include "core/hle/kernel/process.h" + //////////////////////////////////////////////////////////////////////////////////////////////////// // Loader namespace @@ -22,7 +24,6 @@ enum class FileType { CXI, CIA, ELF, - BIN, THREEDSX, //3DSX }; @@ -105,6 +106,12 @@ protected: }; /** + * Common address mappings found in most games, used for binary formats that don't have this + * information. + */ +extern const std::initializer_list<Kernel::AddressMapping> default_address_mappings; + +/** * Identifies and loads a bootable file * @param filename String filename of bootable file * @return ResultStatus result of function diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index aaaa4d650..36e341fd4 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -2,11 +2,17 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include <algorithm> #include <memory> -#include "core/loader/ncch.h" +#include "common/logging/log.h" +#include "common/make_unique.h" +#include "common/string_util.h" +#include "common/swap.h" + #include "core/hle/kernel/kernel.h" -#include "core/mem_map.h" +#include "core/loader/ncch.h" +#include "core/memory.h" //////////////////////////////////////////////////////////////////////////////////////////////////// // Loader namespace @@ -115,8 +121,21 @@ ResultStatus AppLoader_NCCH::LoadExec() const { std::vector<u8> code; if (ResultStatus::Success == ReadCode(code)) { + 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); + + // Copy data while converting endianess + std::array<u32, ARRAY_SIZE(exheader_header.arm11_kernel_caps.descriptors)> kernel_caps; + 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()); - Kernel::LoadExec(entry_point); + + 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); return ResultStatus::Success; } return ResultStatus::Error; @@ -198,20 +217,33 @@ ResultStatus AppLoader_NCCH::Load() { 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; - - LOG_INFO(Loader, "Name: %s", exheader_header.codeset_info.name); - LOG_DEBUG(Loader, "Code compressed: %s", is_compressed ? "yes" : "no"); - LOG_DEBUG(Loader, "Entry point: 0x%08X", entry_point); + 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_INFO(Loader, "Name: %s" , exheader_header.codeset_info.name); + 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 descriptor: 0x%08X", exheader_header.arm11_system_local_caps.resource_limit_descriptor); + LOG_DEBUG(Loader, "Resource limit category: %d" , resource_limit_category); // 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); + 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)) @@ -247,8 +279,8 @@ ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const { 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); + LOG_DEBUG(Loader, "RomFS offset: 0x%08X", romfs_offset); + LOG_DEBUG(Loader, "RomFS size: 0x%08X", romfs_size); buffer.resize(romfs_size); @@ -262,8 +294,4 @@ ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const { return ResultStatus::ErrorNotUsed; } -u64 AppLoader_NCCH::GetProgramId() const { - return *reinterpret_cast<u64 const*>(&ncch_header.program_id[0]); -} - } // namespace Loader diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h index f6f670060..29e39d2c0 100644 --- a/src/core/loader/ncch.h +++ b/src/core/loader/ncch.h @@ -4,7 +4,11 @@ #pragma once -#include "common/common.h" +#include <memory> + +#include "common/bit_field.h" +#include "common/common_types.h" +#include "common/swap.h" #include "core/loader/loader.h" @@ -43,6 +47,8 @@ struct NCCH_Header { u8 romfs_super_block_hash[0x20]; }; +static_assert(sizeof(NCCH_Header) == 0x200, "NCCH header structure size is wrong"); + //////////////////////////////////////////////////////////////////////////////////////////////////// // ExeFS (executable file system) headers @@ -61,13 +67,13 @@ struct ExeFs_Header { //////////////////////////////////////////////////////////////////////////////////////////////////// // ExHeader (executable file system header) headers -struct ExHeader_SystemInfoFlags{ +struct ExHeader_SystemInfoFlags { u8 reserved[5]; u8 flag; u8 remaster_version[2]; }; -struct ExHeader_CodeSegmentInfo{ +struct ExHeader_CodeSegmentInfo { u32 address; u32 num_max_pages; u32 code_size; @@ -77,24 +83,24 @@ struct ExHeader_CodeSetInfo { u8 name[8]; ExHeader_SystemInfoFlags flags; ExHeader_CodeSegmentInfo text; - u8 stacksize[4]; + u32 stack_size; ExHeader_CodeSegmentInfo ro; u8 reserved[4]; ExHeader_CodeSegmentInfo data; - u8 bsssize[4]; + u32 bss_size; }; -struct ExHeader_DependencyList{ +struct ExHeader_DependencyList { u8 program_id[0x30][8]; }; -struct ExHeader_SystemInfo{ +struct ExHeader_SystemInfo { u64 save_data_size; u8 jump_id[8]; u8 reserved_2[0x30]; }; -struct ExHeader_StorageInfo{ +struct ExHeader_StorageInfo { u8 ext_save_data_id[8]; u8 system_save_data_id[8]; u8 reserved[8]; @@ -102,30 +108,36 @@ struct ExHeader_StorageInfo{ u8 other_attributes; }; -struct ExHeader_ARM11_SystemLocalCaps{ +struct ExHeader_ARM11_SystemLocalCaps { u8 program_id[8]; u32 core_version; - u8 flags[3]; + 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[0x16][2]; + u8 resource_limit_descriptor[0x10][2]; ExHeader_StorageInfo storage_info; - u8 service_access_control[0x32][8]; + u8 service_access_control[0x20][8]; u8 ex_service_access_control[0x2][8]; u8 reserved[0xf]; u8 resource_limit_category; }; -struct ExHeader_ARM11_KernelCaps{ - u8 descriptors[28][4]; +struct ExHeader_ARM11_KernelCaps { + u32_le descriptors[28]; u8 reserved[0x10]; }; -struct ExHeader_ARM9_AccessControl{ +struct ExHeader_ARM9_AccessControl { u8 descriptors[15]; u8 descversion; }; -struct ExHeader_Header{ +struct ExHeader_Header { ExHeader_CodeSetInfo codeset_info; ExHeader_DependencyList dependency_list; ExHeader_SystemInfo system_info; @@ -141,6 +153,8 @@ struct ExHeader_Header{ } access_desc; }; +static_assert(sizeof(ExHeader_Header) == 0x800, "ExHeader structure size is wrong"); + //////////////////////////////////////////////////////////////////////////////////////////////////// // Loader namespace @@ -199,12 +213,6 @@ public: */ ResultStatus ReadRomFS(std::vector<u8>& buffer) const override; - /* - * Gets the program id from the NCCH header - * @return u64 Program id - */ - u64 GetProgramId() const; - private: /** @@ -224,6 +232,12 @@ private: 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; |