ncch.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <cinttypes>
  6. #include <codecvt>
  7. #include <cstring>
  8. #include <locale>
  9. #include <memory>
  10. #include "common/logging/log.h"
  11. #include "common/string_util.h"
  12. #include "common/swap.h"
  13. #include "core/core.h"
  14. #include "core/file_sys/archive_selfncch.h"
  15. #include "core/file_sys/ncch_container.h"
  16. #include "core/hle/kernel/process.h"
  17. #include "core/hle/kernel/resource_limit.h"
  18. #include "core/hle/service/cfg/cfg.h"
  19. #include "core/hle/service/fs/archive.h"
  20. #include "core/loader/ncch.h"
  21. #include "core/loader/smdh.h"
  22. #include "core/memory.h"
  23. #include "network/network.h"
  24. ////////////////////////////////////////////////////////////////////////////////////////////////////
  25. // Loader namespace
  26. namespace Loader {
  27. static const u64 UPDATE_MASK = 0x0000000e00000000;
  28. FileType AppLoader_NCCH::IdentifyType(FileUtil::IOFile& file) {
  29. u32 magic;
  30. file.Seek(0x100, SEEK_SET);
  31. if (1 != file.ReadArray<u32>(&magic, 1))
  32. return FileType::Error;
  33. if (MakeMagic('N', 'C', 'S', 'D') == magic)
  34. return FileType::CCI;
  35. if (MakeMagic('N', 'C', 'C', 'H') == magic)
  36. return FileType::CXI;
  37. return FileType::Error;
  38. }
  39. static std::string GetUpdateNCCHPath(u64_le program_id) {
  40. u32 high = static_cast<u32>((program_id | UPDATE_MASK) >> 32);
  41. u32 low = static_cast<u32>((program_id | UPDATE_MASK) & 0xFFFFFFFF);
  42. return Common::StringFromFormat("%sNintendo 3DS/%s/%s/title/%08x/%08x/content/00000000.app",
  43. FileUtil::GetUserPath(D_SDMC_IDX).c_str(), SYSTEM_ID, SDCARD_ID,
  44. high, low);
  45. }
  46. std::pair<boost::optional<u32>, ResultStatus> AppLoader_NCCH::LoadKernelSystemMode() {
  47. if (!is_loaded) {
  48. ResultStatus res = base_ncch.Load();
  49. if (res != ResultStatus::Success) {
  50. return std::make_pair(boost::none, res);
  51. }
  52. }
  53. // Set the system mode as the one from the exheader.
  54. return std::make_pair(overlay_ncch->exheader_header.arm11_system_local_caps.system_mode.Value(),
  55. ResultStatus::Success);
  56. }
  57. ResultStatus AppLoader_NCCH::LoadExec() {
  58. using Kernel::SharedPtr;
  59. using Kernel::CodeSet;
  60. if (!is_loaded)
  61. return ResultStatus::ErrorNotLoaded;
  62. std::vector<u8> code;
  63. u64_le program_id;
  64. if (ResultStatus::Success == ReadCode(code) &&
  65. ResultStatus::Success == ReadProgramId(program_id)) {
  66. std::string process_name = Common::StringFromFixedZeroTerminatedBuffer(
  67. (const char*)overlay_ncch->exheader_header.codeset_info.name, 8);
  68. SharedPtr<CodeSet> codeset = CodeSet::Create(process_name, program_id);
  69. codeset->code.offset = 0;
  70. codeset->code.addr = overlay_ncch->exheader_header.codeset_info.text.address;
  71. codeset->code.size =
  72. overlay_ncch->exheader_header.codeset_info.text.num_max_pages * Memory::PAGE_SIZE;
  73. codeset->rodata.offset = codeset->code.offset + codeset->code.size;
  74. codeset->rodata.addr = overlay_ncch->exheader_header.codeset_info.ro.address;
  75. codeset->rodata.size =
  76. overlay_ncch->exheader_header.codeset_info.ro.num_max_pages * Memory::PAGE_SIZE;
  77. // TODO(yuriks): Not sure if the bss size is added to the page-aligned .data size or just
  78. // to the regular size. Playing it safe for now.
  79. u32 bss_page_size = (overlay_ncch->exheader_header.codeset_info.bss_size + 0xFFF) & ~0xFFF;
  80. code.resize(code.size() + bss_page_size, 0);
  81. codeset->data.offset = codeset->rodata.offset + codeset->rodata.size;
  82. codeset->data.addr = overlay_ncch->exheader_header.codeset_info.data.address;
  83. codeset->data.size =
  84. overlay_ncch->exheader_header.codeset_info.data.num_max_pages * Memory::PAGE_SIZE +
  85. bss_page_size;
  86. codeset->entrypoint = codeset->code.addr;
  87. codeset->memory = std::make_shared<std::vector<u8>>(std::move(code));
  88. Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
  89. Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
  90. // Attach a resource limit to the process based on the resource limit category
  91. Kernel::g_current_process->resource_limit =
  92. Kernel::ResourceLimit::GetForCategory(static_cast<Kernel::ResourceLimitCategory>(
  93. overlay_ncch->exheader_header.arm11_system_local_caps.resource_limit_category));
  94. // Set the default CPU core for this process
  95. Kernel::g_current_process->ideal_processor =
  96. overlay_ncch->exheader_header.arm11_system_local_caps.ideal_processor;
  97. // Copy data while converting endianness
  98. std::array<u32, ARRAY_SIZE(overlay_ncch->exheader_header.arm11_kernel_caps.descriptors)>
  99. kernel_caps;
  100. std::copy_n(overlay_ncch->exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(),
  101. begin(kernel_caps));
  102. Kernel::g_current_process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size());
  103. s32 priority = overlay_ncch->exheader_header.arm11_system_local_caps.priority;
  104. u32 stack_size = overlay_ncch->exheader_header.codeset_info.stack_size;
  105. Kernel::g_current_process->Run(priority, stack_size);
  106. return ResultStatus::Success;
  107. }
  108. return ResultStatus::Error;
  109. }
  110. void AppLoader_NCCH::ParseRegionLockoutInfo() {
  111. std::vector<u8> smdh_buffer;
  112. if (ReadIcon(smdh_buffer) == ResultStatus::Success && smdh_buffer.size() >= sizeof(SMDH)) {
  113. SMDH smdh;
  114. memcpy(&smdh, smdh_buffer.data(), sizeof(SMDH));
  115. u32 region_lockout = smdh.region_lockout;
  116. constexpr u32 REGION_COUNT = 7;
  117. for (u32 region = 0; region < REGION_COUNT; ++region) {
  118. if (region_lockout & 1) {
  119. Service::CFG::SetPreferredRegionCode(region);
  120. break;
  121. }
  122. region_lockout >>= 1;
  123. }
  124. }
  125. }
  126. ResultStatus AppLoader_NCCH::Load() {
  127. u64_le ncch_program_id;
  128. if (is_loaded)
  129. return ResultStatus::ErrorAlreadyLoaded;
  130. ResultStatus result = base_ncch.Load();
  131. if (result != ResultStatus::Success)
  132. return result;
  133. ReadProgramId(ncch_program_id);
  134. std::string program_id{Common::StringFromFormat("%016" PRIX64, ncch_program_id)};
  135. LOG_INFO(Loader, "Program ID: %s", program_id.c_str());
  136. update_ncch.OpenFile(GetUpdateNCCHPath(ncch_program_id));
  137. result = update_ncch.Load();
  138. if (result == ResultStatus::Success) {
  139. overlay_ncch = &update_ncch;
  140. }
  141. Core::Telemetry().AddField(Telemetry::FieldType::Session, "ProgramId", program_id);
  142. if (auto room_member = Network::GetRoomMember().lock()) {
  143. Network::GameInfo game_info;
  144. ReadTitle(game_info.name);
  145. game_info.id = ncch_program_id;
  146. room_member->SendGameInfo(game_info);
  147. }
  148. is_loaded = true; // Set state to loaded
  149. result = LoadExec(); // Load the executable into memory for booting
  150. if (ResultStatus::Success != result)
  151. return result;
  152. Service::FS::RegisterSelfNCCH(*this);
  153. ParseRegionLockoutInfo();
  154. return ResultStatus::Success;
  155. }
  156. ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) {
  157. return overlay_ncch->LoadSectionExeFS(".code", buffer);
  158. }
  159. ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) {
  160. return overlay_ncch->LoadSectionExeFS("icon", buffer);
  161. }
  162. ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) {
  163. return overlay_ncch->LoadSectionExeFS("banner", buffer);
  164. }
  165. ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) {
  166. return overlay_ncch->LoadSectionExeFS("logo", buffer);
  167. }
  168. ResultStatus AppLoader_NCCH::ReadProgramId(u64& out_program_id) {
  169. ResultStatus result = base_ncch.ReadProgramId(out_program_id);
  170. if (result != ResultStatus::Success)
  171. return result;
  172. return ResultStatus::Success;
  173. }
  174. ResultStatus AppLoader_NCCH::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset,
  175. u64& size) {
  176. return base_ncch.ReadRomFS(romfs_file, offset, size);
  177. }
  178. ResultStatus AppLoader_NCCH::ReadUpdateRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file,
  179. u64& offset, u64& size) {
  180. ResultStatus result = update_ncch.ReadRomFS(romfs_file, offset, size);
  181. if (result != ResultStatus::Success)
  182. return base_ncch.ReadRomFS(romfs_file, offset, size);
  183. }
  184. ResultStatus AppLoader_NCCH::ReadTitle(std::string& title) {
  185. std::vector<u8> data;
  186. Loader::SMDH smdh;
  187. ReadIcon(data);
  188. if (!Loader::IsValidSMDH(data)) {
  189. return ResultStatus::ErrorInvalidFormat;
  190. }
  191. memcpy(&smdh, data.data(), sizeof(Loader::SMDH));
  192. const auto& short_title = smdh.GetShortTitle(SMDH::TitleLanguage::English);
  193. auto title_end = std::find(short_title.begin(), short_title.end(), u'\0');
  194. title = Common::UTF16ToUTF8(std::u16string{short_title.begin(), title_end});
  195. return ResultStatus::Success;
  196. }
  197. } // namespace Loader