diff options
Diffstat (limited to 'src/core/loader/ncch.h')
| -rw-r--r-- | src/core/loader/ncch.h | 227 |
1 files changed, 227 insertions, 0 deletions
diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h new file mode 100644 index 000000000..bf65425a4 --- /dev/null +++ b/src/core/loader/ncch.h | |||
| @@ -0,0 +1,227 @@ | |||
| 1 | // Copyright 2014 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "common/common.h" | ||
| 8 | #include "common/file_util.h" | ||
| 9 | |||
| 10 | #include "core/loader/loader.h" | ||
| 11 | |||
| 12 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 13 | /// NCCH header (Note: "NCCH" appears to be a publically unknown acronym) | ||
| 14 | |||
| 15 | struct NCCH_Header { | ||
| 16 | u8 signature[0x100]; | ||
| 17 | char magic[4]; | ||
| 18 | u32 content_size; | ||
| 19 | u8 partition_id[8]; | ||
| 20 | u16 maker_code; | ||
| 21 | u16 version; | ||
| 22 | u8 reserved_0[4]; | ||
| 23 | u8 program_id[8]; | ||
| 24 | u8 temp_flag; | ||
| 25 | u8 reserved_1[0x2f]; | ||
| 26 | u8 product_code[0x10]; | ||
| 27 | u8 extended_header_hash[0x20]; | ||
| 28 | u32 extended_header_size; | ||
| 29 | u8 reserved_2[4]; | ||
| 30 | u8 flags[8]; | ||
| 31 | u32 plain_region_offset; | ||
| 32 | u32 plain_region_size; | ||
| 33 | u8 reserved_3[8]; | ||
| 34 | u32 exefs_offset; | ||
| 35 | u32 exefs_size; | ||
| 36 | u32 exefs_hash_region_size; | ||
| 37 | u8 reserved_4[4]; | ||
| 38 | u32 romfs_offset; | ||
| 39 | u32 romfs_size; | ||
| 40 | u32 romfs_hash_region_size; | ||
| 41 | u8 reserved_5[4]; | ||
| 42 | u8 exefs_super_block_hash[0x20]; | ||
| 43 | u8 romfs_super_block_hash[0x20]; | ||
| 44 | }; | ||
| 45 | |||
| 46 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 47 | // ExeFS (executable file system) headers | ||
| 48 | |||
| 49 | typedef struct { | ||
| 50 | char name[8]; | ||
| 51 | u32 offset; | ||
| 52 | u32 size; | ||
| 53 | } ExeFs_SectionHeader; | ||
| 54 | |||
| 55 | typedef struct { | ||
| 56 | ExeFs_SectionHeader section[8]; | ||
| 57 | u8 reserved[0x80]; | ||
| 58 | u8 hashes[8][0x20]; | ||
| 59 | } ExeFs_Header; | ||
| 60 | |||
| 61 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 62 | // ExHeader (executable file system header) headers | ||
| 63 | |||
| 64 | struct ExHeader_SystemInfoFlags{ | ||
| 65 | u8 reserved[5]; | ||
| 66 | u8 flag; | ||
| 67 | u8 remaster_version[2]; | ||
| 68 | }; | ||
| 69 | |||
| 70 | struct ExHeader_CodeSegmentInfo{ | ||
| 71 | u32 address; | ||
| 72 | u32 num_max_pages; | ||
| 73 | u32 code_size; | ||
| 74 | }; | ||
| 75 | |||
| 76 | struct ExHeader_CodeSetInfo { | ||
| 77 | u8 name[8]; | ||
| 78 | ExHeader_SystemInfoFlags flags; | ||
| 79 | ExHeader_CodeSegmentInfo text; | ||
| 80 | u8 stacksize[4]; | ||
| 81 | ExHeader_CodeSegmentInfo ro; | ||
| 82 | u8 reserved[4]; | ||
| 83 | ExHeader_CodeSegmentInfo data; | ||
| 84 | u8 bsssize[4]; | ||
| 85 | }; | ||
| 86 | |||
| 87 | struct ExHeader_DependencyList{ | ||
| 88 | u8 program_id[0x30][8]; | ||
| 89 | }; | ||
| 90 | |||
| 91 | struct ExHeader_SystemInfo{ | ||
| 92 | u32 save_data_size; | ||
| 93 | u8 reserved[4]; | ||
| 94 | u8 jump_id[8]; | ||
| 95 | u8 reserved_2[0x30]; | ||
| 96 | }; | ||
| 97 | |||
| 98 | struct ExHeader_StorageInfo{ | ||
| 99 | u8 ext_save_data_id[8]; | ||
| 100 | u8 system_save_data_id[8]; | ||
| 101 | u8 reserved[8]; | ||
| 102 | u8 access_info[7]; | ||
| 103 | u8 other_attributes; | ||
| 104 | }; | ||
| 105 | |||
| 106 | struct ExHeader_ARM11_SystemLocalCaps{ | ||
| 107 | u8 program_id[8]; | ||
| 108 | u8 flags[8]; | ||
| 109 | u8 resource_limit_descriptor[0x10][2]; | ||
| 110 | ExHeader_StorageInfo storage_info; | ||
| 111 | u8 service_access_control[0x20][8]; | ||
| 112 | u8 reserved[0x1f]; | ||
| 113 | u8 resource_limit_category; | ||
| 114 | }; | ||
| 115 | |||
| 116 | struct ExHeader_ARM11_KernelCaps{ | ||
| 117 | u8 descriptors[28][4]; | ||
| 118 | u8 reserved[0x10]; | ||
| 119 | }; | ||
| 120 | |||
| 121 | struct ExHeader_ARM9_AccessControl{ | ||
| 122 | u8 descriptors[15]; | ||
| 123 | u8 descversion; | ||
| 124 | }; | ||
| 125 | |||
| 126 | struct ExHeader_Header{ | ||
| 127 | ExHeader_CodeSetInfo codeset_info; | ||
| 128 | ExHeader_DependencyList dependency_list; | ||
| 129 | ExHeader_SystemInfo system_info; | ||
| 130 | ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps; | ||
| 131 | ExHeader_ARM11_KernelCaps arm11_kernel_caps; | ||
| 132 | ExHeader_ARM9_AccessControl arm9_access_control; | ||
| 133 | struct { | ||
| 134 | u8 signature[0x100]; | ||
| 135 | u8 ncch_public_key_modulus[0x100]; | ||
| 136 | ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps; | ||
| 137 | ExHeader_ARM11_KernelCaps arm11_kernel_caps; | ||
| 138 | ExHeader_ARM9_AccessControl arm9_access_control; | ||
| 139 | } access_desc; | ||
| 140 | }; | ||
| 141 | |||
| 142 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 143 | // Loader namespace | ||
| 144 | |||
| 145 | namespace Loader { | ||
| 146 | |||
| 147 | /// Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI) | ||
| 148 | class AppLoader_NCCH : public AppLoader { | ||
| 149 | public: | ||
| 150 | AppLoader_NCCH(const std::string& filename); | ||
| 151 | ~AppLoader_NCCH(); | ||
| 152 | |||
| 153 | /** | ||
| 154 | * Load the application | ||
| 155 | * @return ResultStatus result of function | ||
| 156 | */ | ||
| 157 | ResultStatus Load(); | ||
| 158 | |||
| 159 | /** | ||
| 160 | * Get the code (typically .code section) of the application | ||
| 161 | * @param error ResultStatus result of function | ||
| 162 | * @return Reference to code buffer | ||
| 163 | */ | ||
| 164 | const std::vector<u8>& ReadCode(ResultStatus& error); | ||
| 165 | |||
| 166 | /** | ||
| 167 | * Get the icon (typically icon section) of the application | ||
| 168 | * @param error ResultStatus result of function | ||
| 169 | * @return Reference to icon buffer | ||
| 170 | */ | ||
| 171 | const std::vector<u8>& ReadIcon(ResultStatus& error); | ||
| 172 | |||
| 173 | /** | ||
| 174 | * Get the banner (typically banner section) of the application | ||
| 175 | * @param error ResultStatus result of function | ||
| 176 | * @return Reference to banner buffer | ||
| 177 | */ | ||
| 178 | const std::vector<u8>& ReadBanner(ResultStatus& error); | ||
| 179 | |||
| 180 | /** | ||
| 181 | * Get the logo (typically logo section) of the application | ||
| 182 | * @param error ResultStatus result of function | ||
| 183 | * @return Reference to logo buffer | ||
| 184 | */ | ||
| 185 | const std::vector<u8>& ReadLogo(ResultStatus& error); | ||
| 186 | |||
| 187 | /** | ||
| 188 | * Get the RomFs archive of the application | ||
| 189 | * @param error ResultStatus result of function | ||
| 190 | * @return Reference to RomFs archive buffer | ||
| 191 | */ | ||
| 192 | const std::vector<u8>& ReadRomFS(ResultStatus& error); | ||
| 193 | |||
| 194 | private: | ||
| 195 | |||
| 196 | /** | ||
| 197 | * Reads an application ExeFS section of an NCCH file into AppLoader (e.g. .code, .logo, etc.) | ||
| 198 | * @param name Name of section to read out of NCCH file | ||
| 199 | * @param buffer Vector to read data into | ||
| 200 | * @param error ResultStatus result of function | ||
| 201 | * @return Reference to buffer of data that was read | ||
| 202 | */ | ||
| 203 | const std::vector<u8>& LoadSectionExeFS(const char* name, std::vector<u8>& buffer, | ||
| 204 | ResultStatus& error); | ||
| 205 | |||
| 206 | /** | ||
| 207 | * Loads .code section into memory for booting | ||
| 208 | * @return ResultStatus result of function | ||
| 209 | */ | ||
| 210 | ResultStatus LoadExec(); | ||
| 211 | |||
| 212 | File::IOFile file; | ||
| 213 | std::string filename; | ||
| 214 | |||
| 215 | bool is_loaded; | ||
| 216 | bool is_compressed; | ||
| 217 | |||
| 218 | u32 entry_point; | ||
| 219 | u32 ncch_offset; // Offset to NCCH header, can be 0 or after NCSD header | ||
| 220 | u32 exefs_offset; | ||
| 221 | |||
| 222 | NCCH_Header ncch_header; | ||
| 223 | ExeFs_Header exefs_header; | ||
| 224 | ExHeader_Header exheader_header; | ||
| 225 | }; | ||
| 226 | |||
| 227 | } // namespace Loader | ||