3dsx.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <vector>
  6. #include "core/file_sys/archive_romfs.h"
  7. #include "core/loader/elf.h"
  8. #include "core/loader/ncch.h"
  9. #include "core/hle/service/fs/archive.h"
  10. #include "core/mem_map.h"
  11. #include "3dsx.h"
  12. namespace Loader {
  13. /**
  14. * File layout:
  15. * - File header
  16. * - Code, rodata and data relocation table headers
  17. * - Code segment
  18. * - Rodata segment
  19. * - Loadable (non-BSS) part of the data segment
  20. * - Code relocation table
  21. * - Rodata relocation table
  22. * - Data relocation table
  23. *
  24. * Memory layout before relocations are applied:
  25. * [0..codeSegSize) -> code segment
  26. * [codeSegSize..rodataSegSize) -> rodata segment
  27. * [rodataSegSize..dataSegSize) -> data segment
  28. *
  29. * Memory layout after relocations are applied: well, however the loader sets it up :)
  30. * The entrypoint is always the start of the code segment.
  31. * The BSS section must be cleared manually by the application.
  32. */
  33. enum THREEDSX_Error {
  34. ERROR_NONE = 0,
  35. ERROR_READ = 1,
  36. ERROR_FILE = 2,
  37. ERROR_ALLOC = 3
  38. };
  39. static const u32 RELOCBUFSIZE = 512;
  40. // File header
  41. static const u32 THREEDSX_MAGIC = 0x58534433; // '3DSX'
  42. #pragma pack(1)
  43. struct THREEDSX_Header
  44. {
  45. u32 magic;
  46. u16 header_size, reloc_hdr_size;
  47. u32 format_ver;
  48. u32 flags;
  49. // Sizes of the code, rodata and data segments +
  50. // size of the BSS section (uninitialized latter half of the data segment)
  51. u32 code_seg_size, rodata_seg_size, data_seg_size, bss_size;
  52. };
  53. // Relocation header: all fields (even extra unknown fields) are guaranteed to be relocation counts.
  54. struct THREEDSX_RelocHdr
  55. {
  56. // # of absolute relocations (that is, fix address to post-relocation memory layout)
  57. u32 cross_segment_absolute;
  58. // # of cross-segment relative relocations (that is, 32bit signed offsets that need to be patched)
  59. u32 cross_segment_relative;
  60. // more?
  61. // Relocations are written in this order:
  62. // - Absolute relocations
  63. // - Relative relocations
  64. };
  65. // Relocation entry: from the current pointer, skip X words and patch Y words
  66. struct THREEDSX_Reloc
  67. {
  68. u16 skip, patch;
  69. };
  70. #pragma pack()
  71. struct THREEloadinfo
  72. {
  73. u8* seg_ptrs[3]; // code, rodata & data
  74. u32 seg_addrs[3];
  75. u32 seg_sizes[3];
  76. };
  77. class THREEDSXReader {
  78. public:
  79. static int Load3DSXFile(const std::string& filename, u32 base_addr);
  80. };
  81. static u32 TranslateAddr(u32 addr, THREEloadinfo *loadinfo, u32* offsets)
  82. {
  83. if (addr < offsets[0])
  84. return loadinfo->seg_addrs[0] + addr;
  85. if (addr < offsets[1])
  86. return loadinfo->seg_addrs[1] + addr - offsets[0];
  87. return loadinfo->seg_addrs[2] + addr - offsets[1];
  88. }
  89. int THREEDSXReader::Load3DSXFile(const std::string& filename, u32 base_addr)
  90. {
  91. FileUtil::IOFile file(filename, "rb");
  92. if (!file.IsOpen()) {
  93. return ERROR_FILE;
  94. }
  95. THREEDSX_Header hdr;
  96. if (file.ReadBytes(&hdr, sizeof(hdr)) != sizeof(hdr))
  97. return ERROR_READ;
  98. THREEloadinfo loadinfo;
  99. //loadinfo segments must be a multiple of 0x1000
  100. loadinfo.seg_sizes[0] = (hdr.code_seg_size + 0xFFF) &~0xFFF;
  101. loadinfo.seg_sizes[1] = (hdr.rodata_seg_size + 0xFFF) &~0xFFF;
  102. loadinfo.seg_sizes[2] = (hdr.data_seg_size + 0xFFF) &~0xFFF;
  103. u32 offsets[2] = { loadinfo.seg_sizes[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] };
  104. u32 data_load_size = (hdr.data_seg_size - hdr.bss_size + 0xFFF) &~0xFFF;
  105. u32 bss_load_size = loadinfo.seg_sizes[2] - data_load_size;
  106. u32 n_reloc_tables = hdr.reloc_hdr_size / 4;
  107. std::vector<u8> all_mem(loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] + loadinfo.seg_sizes[2] + 3 * n_reloc_tables);
  108. loadinfo.seg_addrs[0] = base_addr;
  109. loadinfo.seg_addrs[1] = loadinfo.seg_addrs[0] + loadinfo.seg_sizes[0];
  110. loadinfo.seg_addrs[2] = loadinfo.seg_addrs[1] + loadinfo.seg_sizes[1];
  111. loadinfo.seg_ptrs[0] = &all_mem[0];
  112. loadinfo.seg_ptrs[1] = loadinfo.seg_ptrs[0] + loadinfo.seg_sizes[0];
  113. loadinfo.seg_ptrs[2] = loadinfo.seg_ptrs[1] + loadinfo.seg_sizes[1];
  114. // Skip header for future compatibility
  115. file.Seek(hdr.header_size, SEEK_SET);
  116. // Read the relocation headers
  117. u32* relocs = (u32*)(loadinfo.seg_ptrs[2] + hdr.data_seg_size);
  118. for (u32 current_segment = 0; current_segment < 3; current_segment++) {
  119. if (file.ReadBytes(&relocs[current_segment*n_reloc_tables], n_reloc_tables * 4) != n_reloc_tables * 4)
  120. return ERROR_READ;
  121. }
  122. // Read the segments
  123. if (file.ReadBytes(loadinfo.seg_ptrs[0], hdr.code_seg_size) != hdr.code_seg_size)
  124. return ERROR_READ;
  125. if (file.ReadBytes(loadinfo.seg_ptrs[1], hdr.rodata_seg_size) != hdr.rodata_seg_size)
  126. return ERROR_READ;
  127. if (file.ReadBytes(loadinfo.seg_ptrs[2], hdr.data_seg_size - hdr.bss_size) != hdr.data_seg_size - hdr.bss_size)
  128. return ERROR_READ;
  129. // BSS clear
  130. memset((char*)loadinfo.seg_ptrs[2] + hdr.data_seg_size - hdr.bss_size, 0, hdr.bss_size);
  131. // Relocate the segments
  132. for (u32 current_segment = 0; current_segment < 3; current_segment++) {
  133. for (u32 current_segment_reloc_table = 0; current_segment_reloc_table < n_reloc_tables; current_segment_reloc_table++) {
  134. u32 n_relocs = relocs[current_segment*n_reloc_tables + current_segment_reloc_table];
  135. if (current_segment_reloc_table >= 2) {
  136. // We are not using this table - ignore it because we don't know what it dose
  137. file.Seek(n_relocs*sizeof(THREEDSX_Reloc), SEEK_CUR);
  138. continue;
  139. }
  140. static THREEDSX_Reloc reloc_table[RELOCBUFSIZE];
  141. u32* pos = (u32*)loadinfo.seg_ptrs[current_segment];
  142. u32* end_pos = pos + (loadinfo.seg_sizes[current_segment] / 4);
  143. while (n_relocs) {
  144. u32 remaining = std::min(RELOCBUFSIZE, n_relocs);
  145. n_relocs -= remaining;
  146. if (file.ReadBytes(reloc_table, remaining*sizeof(THREEDSX_Reloc)) != remaining*sizeof(THREEDSX_Reloc))
  147. return ERROR_READ;
  148. for (u32 current_inprogress = 0; current_inprogress < remaining && pos < end_pos; current_inprogress++) {
  149. LOG_TRACE(Loader, "(t=%d,skip=%u,patch=%u)\n",
  150. current_segment_reloc_table, (u32)reloc_table[current_inprogress].skip, (u32)reloc_table[current_inprogress].patch);
  151. pos += reloc_table[current_inprogress].skip;
  152. s32 num_patches = reloc_table[current_inprogress].patch;
  153. while (0 < num_patches && pos < end_pos) {
  154. u32 in_addr = (char*)pos - (char*)&all_mem[0];
  155. u32 addr = TranslateAddr(*pos, &loadinfo, offsets);
  156. LOG_TRACE(Loader, "Patching %08X <-- rel(%08X,%d) (%08X)\n",
  157. base_addr + in_addr, addr, current_segment_reloc_table, *pos);
  158. switch (current_segment_reloc_table) {
  159. case 0: *pos = (addr); break;
  160. case 1: *pos = (addr - in_addr); break;
  161. default: break; //this should never happen
  162. }
  163. pos++;
  164. num_patches--;
  165. }
  166. }
  167. }
  168. }
  169. }
  170. // Write the data
  171. memcpy(Memory::GetPointer(base_addr), &all_mem[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] + loadinfo.seg_sizes[2]);
  172. LOG_DEBUG(Loader, "CODE: %u pages\n", loadinfo.seg_sizes[0] / 0x1000);
  173. LOG_DEBUG(Loader, "RODATA: %u pages\n", loadinfo.seg_sizes[1] / 0x1000);
  174. LOG_DEBUG(Loader, "DATA: %u pages\n", data_load_size / 0x1000);
  175. LOG_DEBUG(Loader, "BSS: %u pages\n", bss_load_size / 0x1000);
  176. return ERROR_NONE;
  177. }
  178. /// AppLoader_DSX constructor
  179. AppLoader_THREEDSX::AppLoader_THREEDSX(const std::string& filename) : filename(filename) {
  180. }
  181. /// AppLoader_DSX destructor
  182. AppLoader_THREEDSX::~AppLoader_THREEDSX() {
  183. }
  184. /**
  185. * Loads a 3DSX file
  186. * @return Success on success, otherwise Error
  187. */
  188. ResultStatus AppLoader_THREEDSX::Load() {
  189. LOG_INFO(Loader, "Loading 3DSX file %s...", filename.c_str());
  190. FileUtil::IOFile file(filename, "rb");
  191. if (file.IsOpen()) {
  192. THREEDSXReader::Load3DSXFile(filename, 0x00100000);
  193. Kernel::LoadExec(0x00100000);
  194. } else {
  195. return ResultStatus::Error;
  196. }
  197. return ResultStatus::Success;
  198. }
  199. } // namespace Loader