2014-06-17 04:57:09 +02:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 06:38:14 +01:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-06-17 04:57:09 +02:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2015-05-06 09:06:12 +02:00
|
|
|
#include <memory>
|
2015-05-04 05:01:16 +02:00
|
|
|
#include "common/bit_field.h"
|
2015-05-06 09:06:12 +02:00
|
|
|
#include "common/common_types.h"
|
2015-05-04 05:01:16 +02:00
|
|
|
#include "common/swap.h"
|
2014-06-19 00:58:09 +02:00
|
|
|
#include "core/loader/loader.h"
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2016-10-20 16:26:59 +02:00
|
|
|
/// NCCH header (Note: "NCCH" appears to be a publicly unknown acronym)
|
2014-06-19 00:58:09 +02:00
|
|
|
|
|
|
|
struct NCCH_Header {
|
|
|
|
u8 signature[0x100];
|
2015-10-11 03:51:06 +02:00
|
|
|
u32_le magic;
|
|
|
|
u32_le content_size;
|
2014-06-19 00:58:09 +02:00
|
|
|
u8 partition_id[8];
|
2015-10-11 03:51:06 +02:00
|
|
|
u16_le maker_code;
|
|
|
|
u16_le version;
|
2014-06-19 00:58:09 +02:00
|
|
|
u8 reserved_0[4];
|
2015-10-11 03:50:10 +02:00
|
|
|
u64_le program_id;
|
2015-03-15 22:17:19 +01:00
|
|
|
u8 reserved_1[0x10];
|
|
|
|
u8 logo_region_hash[0x20];
|
2014-06-19 00:58:09 +02:00
|
|
|
u8 product_code[0x10];
|
|
|
|
u8 extended_header_hash[0x20];
|
2015-10-11 03:51:06 +02:00
|
|
|
u32_le extended_header_size;
|
2014-06-19 00:58:09 +02:00
|
|
|
u8 reserved_2[4];
|
|
|
|
u8 flags[8];
|
2015-10-11 03:51:06 +02:00
|
|
|
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;
|
2015-03-15 22:17:19 +01:00
|
|
|
u8 reserved_3[4];
|
2015-10-11 03:51:06 +02:00
|
|
|
u32_le romfs_offset;
|
|
|
|
u32_le romfs_size;
|
|
|
|
u32_le romfs_hash_region_size;
|
2015-03-15 22:17:19 +01:00
|
|
|
u8 reserved_4[4];
|
2014-06-19 00:58:09 +02:00
|
|
|
u8 exefs_super_block_hash[0x20];
|
|
|
|
u8 romfs_super_block_hash[0x20];
|
|
|
|
};
|
|
|
|
|
2015-04-09 09:11:03 +02:00
|
|
|
static_assert(sizeof(NCCH_Header) == 0x200, "NCCH header structure size is wrong");
|
|
|
|
|
2014-06-19 00:58:09 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// ExeFS (executable file system) headers
|
|
|
|
|
2014-09-06 19:37:59 +02:00
|
|
|
struct ExeFs_SectionHeader {
|
2014-06-19 00:58:09 +02:00
|
|
|
char name[8];
|
|
|
|
u32 offset;
|
|
|
|
u32 size;
|
2014-09-06 19:37:59 +02:00
|
|
|
};
|
2014-06-19 00:58:09 +02:00
|
|
|
|
2014-09-06 19:37:59 +02:00
|
|
|
struct ExeFs_Header {
|
2014-06-19 00:58:09 +02:00
|
|
|
ExeFs_SectionHeader section[8];
|
|
|
|
u8 reserved[0x80];
|
|
|
|
u8 hashes[8][0x20];
|
2014-09-06 19:37:59 +02:00
|
|
|
};
|
2014-06-19 00:58:09 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// ExHeader (executable file system header) headers
|
|
|
|
|
2015-05-08 22:41:55 +02:00
|
|
|
struct ExHeader_SystemInfoFlags {
|
2014-06-19 00:58:09 +02:00
|
|
|
u8 reserved[5];
|
|
|
|
u8 flag;
|
|
|
|
u8 remaster_version[2];
|
|
|
|
};
|
|
|
|
|
2015-05-08 22:41:55 +02:00
|
|
|
struct ExHeader_CodeSegmentInfo {
|
2014-06-19 00:58:09 +02:00
|
|
|
u32 address;
|
|
|
|
u32 num_max_pages;
|
|
|
|
u32 code_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExHeader_CodeSetInfo {
|
|
|
|
u8 name[8];
|
|
|
|
ExHeader_SystemInfoFlags flags;
|
|
|
|
ExHeader_CodeSegmentInfo text;
|
2015-04-09 09:11:03 +02:00
|
|
|
u32 stack_size;
|
2014-06-19 00:58:09 +02:00
|
|
|
ExHeader_CodeSegmentInfo ro;
|
|
|
|
u8 reserved[4];
|
|
|
|
ExHeader_CodeSegmentInfo data;
|
2015-04-09 09:11:03 +02:00
|
|
|
u32 bss_size;
|
2014-06-19 00:58:09 +02:00
|
|
|
};
|
|
|
|
|
2015-05-08 22:41:55 +02:00
|
|
|
struct ExHeader_DependencyList {
|
2014-06-19 00:58:09 +02:00
|
|
|
u8 program_id[0x30][8];
|
|
|
|
};
|
|
|
|
|
2015-05-08 22:41:55 +02:00
|
|
|
struct ExHeader_SystemInfo {
|
2015-03-15 22:17:19 +01:00
|
|
|
u64 save_data_size;
|
2014-06-19 00:58:09 +02:00
|
|
|
u8 jump_id[8];
|
|
|
|
u8 reserved_2[0x30];
|
|
|
|
};
|
|
|
|
|
2015-05-08 22:41:55 +02:00
|
|
|
struct ExHeader_StorageInfo {
|
2014-06-19 00:58:09 +02:00
|
|
|
u8 ext_save_data_id[8];
|
|
|
|
u8 system_save_data_id[8];
|
|
|
|
u8 reserved[8];
|
|
|
|
u8 access_info[7];
|
|
|
|
u8 other_attributes;
|
|
|
|
};
|
|
|
|
|
2015-05-08 22:41:55 +02:00
|
|
|
struct ExHeader_ARM11_SystemLocalCaps {
|
2015-10-11 03:50:10 +02:00
|
|
|
u64_le program_id;
|
2015-10-11 03:51:06 +02:00
|
|
|
u32_le core_version;
|
2015-05-04 05:01:16 +02:00
|
|
|
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;
|
|
|
|
};
|
2015-03-15 22:17:19 +01:00
|
|
|
u8 priority;
|
2015-04-09 09:11:03 +02:00
|
|
|
u8 resource_limit_descriptor[0x10][2];
|
2014-06-19 00:58:09 +02:00
|
|
|
ExHeader_StorageInfo storage_info;
|
2015-04-09 09:11:03 +02:00
|
|
|
u8 service_access_control[0x20][8];
|
2015-03-15 22:17:19 +01:00
|
|
|
u8 ex_service_access_control[0x2][8];
|
|
|
|
u8 reserved[0xf];
|
2014-06-19 00:58:09 +02:00
|
|
|
u8 resource_limit_category;
|
|
|
|
};
|
|
|
|
|
2015-05-08 22:41:55 +02:00
|
|
|
struct ExHeader_ARM11_KernelCaps {
|
2015-05-04 05:01:16 +02:00
|
|
|
u32_le descriptors[28];
|
2014-06-19 00:58:09 +02:00
|
|
|
u8 reserved[0x10];
|
|
|
|
};
|
|
|
|
|
2015-05-08 22:41:55 +02:00
|
|
|
struct ExHeader_ARM9_AccessControl {
|
2014-06-19 00:58:09 +02:00
|
|
|
u8 descriptors[15];
|
|
|
|
u8 descversion;
|
|
|
|
};
|
|
|
|
|
2015-05-08 22:41:55 +02:00
|
|
|
struct ExHeader_Header {
|
2014-06-19 00:58:09 +02:00
|
|
|
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;
|
|
|
|
};
|
2014-06-17 04:57:09 +02:00
|
|
|
|
2015-04-09 09:11:03 +02:00
|
|
|
static_assert(sizeof(ExHeader_Header) == 0x800, "ExHeader structure size is wrong");
|
|
|
|
|
2014-06-17 04:57:09 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2014-06-19 00:58:09 +02:00
|
|
|
// Loader namespace
|
2014-06-17 04:57:09 +02:00
|
|
|
|
|
|
|
namespace Loader {
|
|
|
|
|
2014-06-19 00:58:09 +02:00
|
|
|
/// Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI)
|
2014-07-04 19:20:40 +02:00
|
|
|
class AppLoader_NCCH final : public AppLoader {
|
2014-06-19 00:58:09 +02:00
|
|
|
public:
|
2015-07-12 00:16:33 +02:00
|
|
|
AppLoader_NCCH(FileUtil::IOFile&& file, const std::string& filepath)
|
2016-09-19 03:01:46 +02:00
|
|
|
: AppLoader(std::move(file)), filepath(filepath) {}
|
2014-06-19 00:58:09 +02:00
|
|
|
|
2015-01-07 00:10:13 +01:00
|
|
|
/**
|
|
|
|
* Returns the type of the file
|
|
|
|
* @param file FileUtil::IOFile open file
|
|
|
|
* @return FileType found, or FileType::Error if this loader doesn't know it
|
|
|
|
*/
|
|
|
|
static FileType IdentifyType(FileUtil::IOFile& file);
|
|
|
|
|
2016-05-18 00:30:44 +02:00
|
|
|
/**
|
|
|
|
* Returns the type of this file
|
|
|
|
* @return FileType corresponding to the loaded file
|
|
|
|
*/
|
|
|
|
FileType GetFileType() override {
|
|
|
|
return IdentifyType(file);
|
|
|
|
}
|
|
|
|
|
2014-06-19 00:58:09 +02:00
|
|
|
/**
|
|
|
|
* Load the application
|
|
|
|
* @return ResultStatus result of function
|
|
|
|
*/
|
2014-07-04 19:25:30 +02:00
|
|
|
ResultStatus Load() override;
|
2014-06-19 00:58:09 +02:00
|
|
|
|
2016-11-20 02:40:04 +01:00
|
|
|
/**
|
|
|
|
* Loads the Exheader and returns the system mode for this application.
|
2016-11-27 05:13:40 +01:00
|
|
|
* @return Optional with the kernel system mode
|
2016-11-20 02:40:04 +01:00
|
|
|
*/
|
2016-11-27 05:13:40 +01:00
|
|
|
boost::optional<u32> LoadKernelSystemMode();
|
2016-11-20 02:40:04 +01:00
|
|
|
|
2014-06-22 21:40:21 +02:00
|
|
|
/**
|
|
|
|
* Get the code (typically .code section) of the application
|
2014-06-27 21:33:23 +02:00
|
|
|
* @param buffer Reference to buffer to store data
|
|
|
|
* @return ResultStatus result of function
|
2014-06-22 21:40:21 +02:00
|
|
|
*/
|
2015-07-12 00:16:33 +02:00
|
|
|
ResultStatus ReadCode(std::vector<u8>& buffer) override;
|
2014-06-19 00:58:09 +02:00
|
|
|
|
|
|
|
/**
|
2014-06-22 21:40:21 +02:00
|
|
|
* Get the icon (typically icon section) of the application
|
2014-06-27 21:33:23 +02:00
|
|
|
* @param buffer Reference to buffer to store data
|
|
|
|
* @return ResultStatus result of function
|
2014-06-19 00:58:09 +02:00
|
|
|
*/
|
2015-07-12 00:16:33 +02:00
|
|
|
ResultStatus ReadIcon(std::vector<u8>& buffer) override;
|
2014-06-19 00:58:09 +02:00
|
|
|
|
2014-06-19 06:11:45 +02:00
|
|
|
/**
|
2014-06-22 21:40:21 +02:00
|
|
|
* Get the banner (typically banner section) of the application
|
2014-06-27 21:33:23 +02:00
|
|
|
* @param buffer Reference to buffer to store data
|
|
|
|
* @return ResultStatus result of function
|
2014-06-22 21:40:21 +02:00
|
|
|
*/
|
2015-07-12 00:16:33 +02:00
|
|
|
ResultStatus ReadBanner(std::vector<u8>& buffer) override;
|
2014-06-22 21:40:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the logo (typically logo section) of the application
|
2014-06-27 21:33:23 +02:00
|
|
|
* @param buffer Reference to buffer to store data
|
|
|
|
* @return ResultStatus result of function
|
2014-06-19 06:11:45 +02:00
|
|
|
*/
|
2015-07-12 00:16:33 +02:00
|
|
|
ResultStatus ReadLogo(std::vector<u8>& buffer) override;
|
2014-06-22 21:40:21 +02:00
|
|
|
|
2016-12-15 10:54:25 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2014-06-22 21:40:21 +02:00
|
|
|
/**
|
2014-06-27 21:33:23 +02:00
|
|
|
* Get the RomFS of the application
|
2015-09-10 17:07:33 +02:00
|
|
|
* @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
|
2014-06-27 21:33:23 +02:00
|
|
|
* @return ResultStatus result of function
|
2014-06-22 21:40:21 +02:00
|
|
|
*/
|
2016-09-18 02:38:01 +02:00
|
|
|
ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset,
|
|
|
|
u64& size) override;
|
2014-06-22 21:40:21 +02:00
|
|
|
|
|
|
|
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
|
2014-06-27 21:33:23 +02:00
|
|
|
* @return ResultStatus result of function
|
2014-06-22 21:40:21 +02:00
|
|
|
*/
|
2015-07-12 00:16:33 +02:00
|
|
|
ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer);
|
2014-06-19 06:11:45 +02:00
|
|
|
|
2014-06-19 00:58:09 +02:00
|
|
|
/**
|
|
|
|
* Loads .code section into memory for booting
|
|
|
|
* @return ResultStatus result of function
|
|
|
|
*/
|
2015-07-12 00:16:33 +02:00
|
|
|
ResultStatus LoadExec();
|
2014-06-19 00:58:09 +02:00
|
|
|
|
2016-04-13 23:04:05 +02:00
|
|
|
/**
|
|
|
|
* Ensure ExeFS is loaded and ready for reading sections
|
|
|
|
* @return ResultStatus result of function
|
|
|
|
*/
|
|
|
|
ResultStatus LoadExeFS();
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
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;
|
2014-06-19 00:58:09 +02:00
|
|
|
ExHeader_Header exheader_header;
|
2015-07-09 23:55:23 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
std::string filepath;
|
2014-06-19 00:58:09 +02:00
|
|
|
};
|
2014-06-17 04:57:09 +02:00
|
|
|
|
|
|
|
} // namespace Loader
|