2014-06-17 02:57:09 +00:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 05:38:14 +00:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-06-17 02:57:09 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common/common.h"
|
2014-06-18 22:58:09 +00:00
|
|
|
|
|
|
|
#include "core/loader/loader.h"
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// NCCH header (Note: "NCCH" appears to be a publically unknown acronym)
|
|
|
|
|
|
|
|
struct NCCH_Header {
|
|
|
|
u8 signature[0x100];
|
2015-01-06 23:10:13 +00:00
|
|
|
u32 magic;
|
2014-06-18 22:58:09 +00:00
|
|
|
u32 content_size;
|
|
|
|
u8 partition_id[8];
|
|
|
|
u16 maker_code;
|
|
|
|
u16 version;
|
|
|
|
u8 reserved_0[4];
|
|
|
|
u8 program_id[8];
|
|
|
|
u8 temp_flag;
|
|
|
|
u8 reserved_1[0x2f];
|
|
|
|
u8 product_code[0x10];
|
|
|
|
u8 extended_header_hash[0x20];
|
|
|
|
u32 extended_header_size;
|
|
|
|
u8 reserved_2[4];
|
|
|
|
u8 flags[8];
|
|
|
|
u32 plain_region_offset;
|
|
|
|
u32 plain_region_size;
|
|
|
|
u8 reserved_3[8];
|
|
|
|
u32 exefs_offset;
|
|
|
|
u32 exefs_size;
|
|
|
|
u32 exefs_hash_region_size;
|
|
|
|
u8 reserved_4[4];
|
|
|
|
u32 romfs_offset;
|
|
|
|
u32 romfs_size;
|
|
|
|
u32 romfs_hash_region_size;
|
|
|
|
u8 reserved_5[4];
|
|
|
|
u8 exefs_super_block_hash[0x20];
|
|
|
|
u8 romfs_super_block_hash[0x20];
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// ExeFS (executable file system) headers
|
|
|
|
|
2014-09-06 17:37:59 +00:00
|
|
|
struct ExeFs_SectionHeader {
|
2014-06-18 22:58:09 +00:00
|
|
|
char name[8];
|
|
|
|
u32 offset;
|
|
|
|
u32 size;
|
2014-09-06 17:37:59 +00:00
|
|
|
};
|
2014-06-18 22:58:09 +00:00
|
|
|
|
2014-09-06 17:37:59 +00:00
|
|
|
struct ExeFs_Header {
|
2014-06-18 22:58:09 +00:00
|
|
|
ExeFs_SectionHeader section[8];
|
|
|
|
u8 reserved[0x80];
|
|
|
|
u8 hashes[8][0x20];
|
2014-09-06 17:37:59 +00:00
|
|
|
};
|
2014-06-18 22:58:09 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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;
|
|
|
|
u8 stacksize[4];
|
|
|
|
ExHeader_CodeSegmentInfo ro;
|
|
|
|
u8 reserved[4];
|
|
|
|
ExHeader_CodeSegmentInfo data;
|
|
|
|
u8 bsssize[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExHeader_DependencyList{
|
|
|
|
u8 program_id[0x30][8];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExHeader_SystemInfo{
|
|
|
|
u32 save_data_size;
|
|
|
|
u8 reserved[4];
|
|
|
|
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{
|
|
|
|
u8 program_id[8];
|
|
|
|
u8 flags[8];
|
|
|
|
u8 resource_limit_descriptor[0x10][2];
|
|
|
|
ExHeader_StorageInfo storage_info;
|
|
|
|
u8 service_access_control[0x20][8];
|
|
|
|
u8 reserved[0x1f];
|
|
|
|
u8 resource_limit_category;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExHeader_ARM11_KernelCaps{
|
|
|
|
u8 descriptors[28][4];
|
|
|
|
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;
|
|
|
|
};
|
2014-06-17 02:57:09 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2014-06-18 22:58:09 +00:00
|
|
|
// Loader namespace
|
2014-06-17 02:57:09 +00:00
|
|
|
|
|
|
|
namespace Loader {
|
|
|
|
|
2014-06-18 22:58:09 +00:00
|
|
|
/// Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI)
|
2014-07-04 17:20:40 +00:00
|
|
|
class AppLoader_NCCH final : public AppLoader {
|
2014-06-18 22:58:09 +00:00
|
|
|
public:
|
2015-01-06 21:30:40 +00:00
|
|
|
AppLoader_NCCH(std::unique_ptr<FileUtil::IOFile>&& file) : AppLoader(std::move(file)) { }
|
2014-06-18 22:58:09 +00:00
|
|
|
|
2015-01-06 23:10:13 +00: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);
|
|
|
|
|
2014-06-18 22:58:09 +00:00
|
|
|
/**
|
|
|
|
* Load the application
|
|
|
|
* @return ResultStatus result of function
|
|
|
|
*/
|
2014-07-04 17:25:30 +00:00
|
|
|
ResultStatus Load() override;
|
2014-06-18 22:58:09 +00:00
|
|
|
|
2014-06-22 19:40:21 +00:00
|
|
|
/**
|
|
|
|
* Get the code (typically .code section) of the application
|
2014-06-27 19:33:23 +00:00
|
|
|
* @param buffer Reference to buffer to store data
|
|
|
|
* @return ResultStatus result of function
|
2014-06-22 19:40:21 +00:00
|
|
|
*/
|
2014-07-04 17:25:30 +00:00
|
|
|
ResultStatus ReadCode(std::vector<u8>& buffer) const override;
|
2014-06-18 22:58:09 +00:00
|
|
|
|
|
|
|
/**
|
2014-06-22 19:40:21 +00:00
|
|
|
* Get the icon (typically icon section) of the application
|
2014-06-27 19:33:23 +00:00
|
|
|
* @param buffer Reference to buffer to store data
|
|
|
|
* @return ResultStatus result of function
|
2014-06-18 22:58:09 +00:00
|
|
|
*/
|
2014-07-04 17:25:30 +00:00
|
|
|
ResultStatus ReadIcon(std::vector<u8>& buffer) const override;
|
2014-06-18 22:58:09 +00:00
|
|
|
|
2014-06-19 04:11:45 +00:00
|
|
|
/**
|
2014-06-22 19:40:21 +00:00
|
|
|
* Get the banner (typically banner section) of the application
|
2014-06-27 19:33:23 +00:00
|
|
|
* @param buffer Reference to buffer to store data
|
|
|
|
* @return ResultStatus result of function
|
2014-06-22 19:40:21 +00:00
|
|
|
*/
|
2014-07-04 17:25:30 +00:00
|
|
|
ResultStatus ReadBanner(std::vector<u8>& buffer) const override;
|
2014-06-22 19:40:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the logo (typically logo section) of the application
|
2014-06-27 19:33:23 +00:00
|
|
|
* @param buffer Reference to buffer to store data
|
|
|
|
* @return ResultStatus result of function
|
2014-06-19 04:11:45 +00:00
|
|
|
*/
|
2014-07-04 17:25:30 +00:00
|
|
|
ResultStatus ReadLogo(std::vector<u8>& buffer) const override;
|
2014-06-22 19:40:21 +00:00
|
|
|
|
|
|
|
/**
|
2014-06-27 19:33:23 +00:00
|
|
|
* Get the RomFS of the application
|
|
|
|
* @param buffer Reference to buffer to store data
|
|
|
|
* @return ResultStatus result of function
|
2014-06-22 19:40:21 +00:00
|
|
|
*/
|
2014-07-04 17:25:30 +00:00
|
|
|
ResultStatus ReadRomFS(std::vector<u8>& buffer) const override;
|
2014-06-22 19:40:21 +00:00
|
|
|
|
2014-12-16 05:33:41 +00:00
|
|
|
/*
|
|
|
|
* Gets the program id from the NCCH header
|
|
|
|
* @return u64 Program id
|
|
|
|
*/
|
|
|
|
u64 GetProgramId() const;
|
|
|
|
|
2014-06-22 19:40:21 +00: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 19:33:23 +00:00
|
|
|
* @return ResultStatus result of function
|
2014-06-22 19:40:21 +00:00
|
|
|
*/
|
2014-07-04 17:11:09 +00:00
|
|
|
ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const;
|
2014-06-19 04:11:45 +00:00
|
|
|
|
2014-06-18 22:58:09 +00:00
|
|
|
/**
|
|
|
|
* Loads .code section into memory for booting
|
|
|
|
* @return ResultStatus result of function
|
|
|
|
*/
|
2014-07-04 17:11:09 +00:00
|
|
|
ResultStatus LoadExec() const;
|
2014-06-18 22:58:09 +00:00
|
|
|
|
2015-01-06 21:10:12 +00:00
|
|
|
bool is_compressed = false;
|
2014-06-18 22:58:09 +00:00
|
|
|
|
2015-01-06 21:10:12 +00:00
|
|
|
u32 entry_point = 0;
|
|
|
|
u32 ncch_offset = 0; // Offset to NCCH header, can be 0 or after NCSD header
|
|
|
|
u32 exefs_offset = 0;
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2014-06-19 04:11:45 +00:00
|
|
|
NCCH_Header ncch_header;
|
2014-06-18 22:58:09 +00:00
|
|
|
ExeFs_Header exefs_header;
|
|
|
|
ExHeader_Header exheader_header;
|
|
|
|
};
|
2014-06-17 02:57:09 +00:00
|
|
|
|
|
|
|
} // namespace Loader
|