ncch.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #include <memory>
  5. #include "common/file_util.h"
  6. #include "core/loader/ncch.h"
  7. #include "core/hle/kernel/kernel.h"
  8. #include "core/mem_map.h"
  9. ////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // Loader namespace
  11. namespace Loader {
  12. static const int kMaxSections = 8; ///< Maximum number of sections (files) in an ExeFs
  13. static const int kBlockSize = 0x200; ///< Size of ExeFS blocks (in bytes)
  14. /**
  15. * Get the decompressed size of an LZSS compressed ExeFS file
  16. * @param buffer Buffer of compressed file
  17. * @param size Size of compressed buffer
  18. * @return Size of decompressed buffer
  19. */
  20. u32 LZSS_GetDecompressedSize(u8* buffer, u32 size) {
  21. u32 offset_size = *(u32*)(buffer + size - 4);
  22. return offset_size + size;
  23. }
  24. /**
  25. * Decompress ExeFS file (compressed with LZSS)
  26. * @param compressed Compressed buffer
  27. * @param compressed_size Size of compressed buffer
  28. * @param decompressed Decompressed buffer
  29. * @param decompressed_size Size of decompressed buffer
  30. * @return True on success, otherwise false
  31. */
  32. bool LZSS_Decompress(u8* compressed, u32 compressed_size, u8* decompressed, u32 decompressed_size) {
  33. u8* footer = compressed + compressed_size - 8;
  34. u32 buffer_top_and_bottom = *(u32*)footer;
  35. u32 i, j;
  36. u32 out = decompressed_size;
  37. u32 index = compressed_size - ((buffer_top_and_bottom >> 24) & 0xFF);
  38. u8 control;
  39. u32 stop_index = compressed_size - (buffer_top_and_bottom & 0xFFFFFF);
  40. memset(decompressed, 0, decompressed_size);
  41. memcpy(decompressed, compressed, compressed_size);
  42. while(index > stop_index) {
  43. control = compressed[--index];
  44. for(i = 0; i < 8; i++) {
  45. if(index <= stop_index)
  46. break;
  47. if(index <= 0)
  48. break;
  49. if(out <= 0)
  50. break;
  51. if(control & 0x80) {
  52. // Check if compression is out of bounds
  53. if(index < 2) {
  54. return false;
  55. }
  56. index -= 2;
  57. u32 segment_offset = compressed[index] | (compressed[index + 1] << 8);
  58. u32 segment_size = ((segment_offset >> 12) & 15) + 3;
  59. segment_offset &= 0x0FFF;
  60. segment_offset += 2;
  61. // Check if compression is out of bounds
  62. if(out < segment_size) {
  63. return false;
  64. }
  65. for(j = 0; j < segment_size; j++) {
  66. u8 data;
  67. // Check if compression is out of bounds
  68. if(out + segment_offset >= decompressed_size) {
  69. return false;
  70. }
  71. data = decompressed[out + segment_offset];
  72. decompressed[--out] = data;
  73. }
  74. } else {
  75. // Check if compression is out of bounds
  76. if(out < 1) {
  77. return false;
  78. }
  79. decompressed[--out] = compressed[--index];
  80. }
  81. control <<= 1;
  82. }
  83. }
  84. return true;
  85. }
  86. ////////////////////////////////////////////////////////////////////////////////////////////////////
  87. // AppLoader_NCCH class
  88. /// AppLoader_NCCH constructor
  89. AppLoader_NCCH::AppLoader_NCCH(const std::string& filename) {
  90. this->filename = filename;
  91. is_loaded = false;
  92. is_compressed = false;
  93. entry_point = 0;
  94. ncch_offset = 0;
  95. exefs_offset = 0;
  96. }
  97. /// AppLoader_NCCH destructor
  98. AppLoader_NCCH::~AppLoader_NCCH() {
  99. }
  100. /**
  101. * Loads .code section into memory for booting
  102. * @return ResultStatus result of function
  103. */
  104. ResultStatus AppLoader_NCCH::LoadExec() const {
  105. if (!is_loaded)
  106. return ResultStatus::ErrorNotLoaded;
  107. std::vector<u8> code;
  108. if (ResultStatus::Success == ReadCode(code)) {
  109. Memory::WriteBlock(entry_point, &code[0], code.size());
  110. Kernel::LoadExec(entry_point);
  111. return ResultStatus::Success;
  112. }
  113. return ResultStatus::Error;
  114. }
  115. /**
  116. * Reads an application ExeFS section of an NCCH file into AppLoader (e.g. .code, .logo, etc.)
  117. * @param name Name of section to read out of NCCH file
  118. * @param buffer Vector to read data into
  119. * @return ResultStatus result of function
  120. */
  121. ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const {
  122. // Iterate through the ExeFs archive until we find the .code file...
  123. File::IOFile file(filename, "rb");
  124. if (file.IsOpen()) {
  125. for (int i = 0; i < kMaxSections; i++) {
  126. // Load the specified section...
  127. if (strcmp((const char*)exefs_header.section[i].name, name) == 0) {
  128. INFO_LOG(LOADER, "ExeFS section %d:", i);
  129. INFO_LOG(LOADER, " name: %s", exefs_header.section[i].name);
  130. INFO_LOG(LOADER, " offset: 0x%08X", exefs_header.section[i].offset);
  131. INFO_LOG(LOADER, " size: 0x%08X", exefs_header.section[i].size);
  132. s64 section_offset = (exefs_header.section[i].offset + exefs_offset +
  133. sizeof(ExeFs_Header)+ncch_offset);
  134. file.Seek(section_offset, 0);
  135. // Section is compressed...
  136. if (i == 0 && is_compressed) {
  137. // Read compressed .code section...
  138. std::unique_ptr<u8[]> temp_buffer(new u8[exefs_header.section[i].size]);
  139. file.ReadBytes(&temp_buffer[0], exefs_header.section[i].size);
  140. // Decompress .code section...
  141. u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0],
  142. exefs_header.section[i].size);
  143. buffer.resize(decompressed_size);
  144. if (!LZSS_Decompress(&temp_buffer[0], exefs_header.section[i].size, &buffer[0],
  145. decompressed_size)) {
  146. return ResultStatus::ErrorInvalidFormat;
  147. }
  148. // Section is uncompressed...
  149. }
  150. else {
  151. buffer.resize(exefs_header.section[i].size);
  152. file.ReadBytes(&buffer[0], exefs_header.section[i].size);
  153. }
  154. return ResultStatus::Success;
  155. }
  156. }
  157. } else {
  158. ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str());
  159. return ResultStatus::Error;
  160. }
  161. return ResultStatus::ErrorNotUsed;
  162. }
  163. /**
  164. * Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI)
  165. * @param error_string Pointer to string to put error message if an error has occurred
  166. * @todo Move NCSD parsing out of here and create a separate function for loading these
  167. * @return True on success, otherwise false
  168. */
  169. ResultStatus AppLoader_NCCH::Load() {
  170. INFO_LOG(LOADER, "Loading NCCH file %s...", filename.c_str());
  171. if (is_loaded)
  172. return ResultStatus::ErrorAlreadyLoaded;
  173. File::IOFile file(filename, "rb");
  174. if (file.IsOpen()) {
  175. file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
  176. // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)...
  177. if (0 == memcmp(&ncch_header.magic, "NCSD", 4)) {
  178. WARN_LOG(LOADER, "Only loading the first (bootable) NCCH within the NCSD file!");
  179. ncch_offset = 0x4000;
  180. file.Seek(ncch_offset, 0);
  181. file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
  182. }
  183. // Verify we are loading the correct file type...
  184. if (0 != memcmp(&ncch_header.magic, "NCCH", 4))
  185. return ResultStatus::ErrorInvalidFormat;
  186. // Read ExHeader...
  187. file.ReadBytes(&exheader_header, sizeof(ExHeader_Header));
  188. is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1;
  189. entry_point = exheader_header.codeset_info.text.address;
  190. INFO_LOG(LOADER, "Name: %s", exheader_header.codeset_info.name);
  191. INFO_LOG(LOADER, "Code compressed: %s", is_compressed ? "yes" : "no");
  192. INFO_LOG(LOADER, "Entry point: 0x%08X", entry_point);
  193. // Read ExeFS...
  194. exefs_offset = ncch_header.exefs_offset * kBlockSize;
  195. u32 exefs_size = ncch_header.exefs_size * kBlockSize;
  196. INFO_LOG(LOADER, "ExeFS offset: 0x%08X", exefs_offset);
  197. INFO_LOG(LOADER, "ExeFS size: 0x%08X", exefs_size);
  198. file.Seek(exefs_offset + ncch_offset, 0);
  199. file.ReadBytes(&exefs_header, sizeof(ExeFs_Header));
  200. is_loaded = true; // Set state to loaded
  201. LoadExec(); // Load the executable into memory for booting
  202. return ResultStatus::Success;
  203. } else {
  204. ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str());
  205. }
  206. return ResultStatus::Error;
  207. }
  208. /**
  209. * Get the code (typically .code section) of the application
  210. * @param buffer Reference to buffer to store data
  211. * @return ResultStatus result of function
  212. */
  213. ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) const {
  214. return LoadSectionExeFS(".code", buffer);
  215. }
  216. /**
  217. * Get the icon (typically icon section) of the application
  218. * @param buffer Reference to buffer to store data
  219. * @return ResultStatus result of function
  220. */
  221. ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) const {
  222. return LoadSectionExeFS("icon", buffer);
  223. }
  224. /**
  225. * Get the banner (typically banner section) of the application
  226. * @param buffer Reference to buffer to store data
  227. * @return ResultStatus result of function
  228. */
  229. ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) const {
  230. return LoadSectionExeFS("banner", buffer);
  231. }
  232. /**
  233. * Get the logo (typically logo section) of the application
  234. * @param buffer Reference to buffer to store data
  235. * @return ResultStatus result of function
  236. */
  237. ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) const {
  238. return LoadSectionExeFS("logo", buffer);
  239. }
  240. /**
  241. * Get the RomFS of the application
  242. * @param buffer Reference to buffer to store data
  243. * @return ResultStatus result of function
  244. */
  245. ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const {
  246. File::IOFile file(filename, "rb");
  247. if (file.IsOpen()) {
  248. // Check if the NCCH has a RomFS...
  249. if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) {
  250. u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000;
  251. u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000;
  252. INFO_LOG(LOADER, "RomFS offset: 0x%08X", romfs_offset);
  253. INFO_LOG(LOADER, "RomFS size: 0x%08X", romfs_size);
  254. buffer.resize(romfs_size);
  255. file.Seek(romfs_offset, 0);
  256. file.ReadBytes(&buffer[0], romfs_size);
  257. return ResultStatus::Success;
  258. }
  259. NOTICE_LOG(LOADER, "RomFS unused");
  260. return ResultStatus::ErrorNotUsed;
  261. } else {
  262. ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str());
  263. }
  264. return ResultStatus::Error;
  265. }
  266. } // namespace Loader