nso.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cinttypes>
  5. #include <cstring>
  6. #include <vector>
  7. #include "common/common_funcs.h"
  8. #include "common/file_util.h"
  9. #include "common/hex_util.h"
  10. #include "common/logging/log.h"
  11. #include "common/lz4_compression.h"
  12. #include "common/settings.h"
  13. #include "common/swap.h"
  14. #include "core/core.h"
  15. #include "core/file_sys/patch_manager.h"
  16. #include "core/hle/kernel/code_set.h"
  17. #include "core/hle/kernel/k_page_table.h"
  18. #include "core/hle/kernel/k_thread.h"
  19. #include "core/hle/kernel/process.h"
  20. #include "core/loader/nso.h"
  21. #include "core/memory.h"
  22. namespace Loader {
  23. namespace {
  24. struct MODHeader {
  25. u32_le magic;
  26. u32_le dynamic_offset;
  27. u32_le bss_start_offset;
  28. u32_le bss_end_offset;
  29. u32_le eh_frame_hdr_start_offset;
  30. u32_le eh_frame_hdr_end_offset;
  31. u32_le module_offset; // Offset to runtime-generated module object. typically equal to .bss base
  32. };
  33. static_assert(sizeof(MODHeader) == 0x1c, "MODHeader has incorrect size.");
  34. std::vector<u8> DecompressSegment(const std::vector<u8>& compressed_data,
  35. const NSOSegmentHeader& header) {
  36. std::vector<u8> uncompressed_data =
  37. Common::Compression::DecompressDataLZ4(compressed_data, header.size);
  38. ASSERT_MSG(uncompressed_data.size() == header.size, "{} != {}", header.size,
  39. uncompressed_data.size());
  40. return uncompressed_data;
  41. }
  42. constexpr u32 PageAlignSize(u32 size) {
  43. return static_cast<u32>((size + Core::Memory::PAGE_MASK) & ~Core::Memory::PAGE_MASK);
  44. }
  45. } // Anonymous namespace
  46. bool NSOHeader::IsSegmentCompressed(size_t segment_num) const {
  47. ASSERT_MSG(segment_num < 3, "Invalid segment {}", segment_num);
  48. return ((flags >> segment_num) & 1) != 0;
  49. }
  50. AppLoader_NSO::AppLoader_NSO(FileSys::VirtualFile file_) : AppLoader(std::move(file_)) {}
  51. FileType AppLoader_NSO::IdentifyType(const FileSys::VirtualFile& in_file) {
  52. u32 magic = 0;
  53. if (in_file->ReadObject(&magic) != sizeof(magic)) {
  54. return FileType::Error;
  55. }
  56. if (Common::MakeMagic('N', 'S', 'O', '0') != magic) {
  57. return FileType::Error;
  58. }
  59. return FileType::NSO;
  60. }
  61. std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process, Core::System& system,
  62. const FileSys::VfsFile& nso_file, VAddr load_base,
  63. bool should_pass_arguments, bool load_into_process,
  64. std::optional<FileSys::PatchManager> pm) {
  65. if (nso_file.GetSize() < sizeof(NSOHeader)) {
  66. return std::nullopt;
  67. }
  68. NSOHeader nso_header{};
  69. if (sizeof(NSOHeader) != nso_file.ReadObject(&nso_header)) {
  70. return std::nullopt;
  71. }
  72. if (nso_header.magic != Common::MakeMagic('N', 'S', 'O', '0')) {
  73. return std::nullopt;
  74. }
  75. // Build program image
  76. Kernel::CodeSet codeset;
  77. Kernel::PhysicalMemory program_image;
  78. for (std::size_t i = 0; i < nso_header.segments.size(); ++i) {
  79. std::vector<u8> data = nso_file.ReadBytes(nso_header.segments_compressed_size[i],
  80. nso_header.segments[i].offset);
  81. if (nso_header.IsSegmentCompressed(i)) {
  82. data = DecompressSegment(data, nso_header.segments[i]);
  83. }
  84. program_image.resize(nso_header.segments[i].location + static_cast<u32>(data.size()));
  85. std::memcpy(program_image.data() + nso_header.segments[i].location, data.data(),
  86. data.size());
  87. codeset.segments[i].addr = nso_header.segments[i].location;
  88. codeset.segments[i].offset = nso_header.segments[i].location;
  89. codeset.segments[i].size = nso_header.segments[i].size;
  90. }
  91. if (should_pass_arguments && !Settings::values.program_args.empty()) {
  92. const auto arg_data{Settings::values.program_args};
  93. codeset.DataSegment().size += NSO_ARGUMENT_DATA_ALLOCATION_SIZE;
  94. NSOArgumentHeader args_header{
  95. NSO_ARGUMENT_DATA_ALLOCATION_SIZE, static_cast<u32_le>(arg_data.size()), {}};
  96. const auto end_offset = program_image.size();
  97. program_image.resize(static_cast<u32>(program_image.size()) +
  98. NSO_ARGUMENT_DATA_ALLOCATION_SIZE);
  99. std::memcpy(program_image.data() + end_offset, &args_header, sizeof(NSOArgumentHeader));
  100. std::memcpy(program_image.data() + end_offset + sizeof(NSOArgumentHeader), arg_data.data(),
  101. arg_data.size());
  102. }
  103. codeset.DataSegment().size += nso_header.segments[2].bss_size;
  104. const u32 image_size{
  105. PageAlignSize(static_cast<u32>(program_image.size()) + nso_header.segments[2].bss_size)};
  106. program_image.resize(image_size);
  107. for (std::size_t i = 0; i < nso_header.segments.size(); ++i) {
  108. codeset.segments[i].size = PageAlignSize(codeset.segments[i].size);
  109. }
  110. // Apply patches if necessary
  111. if (pm && (pm->HasNSOPatch(nso_header.build_id) || Settings::values.dump_nso)) {
  112. std::vector<u8> pi_header;
  113. pi_header.insert(pi_header.begin(), reinterpret_cast<u8*>(&nso_header),
  114. reinterpret_cast<u8*>(&nso_header) + sizeof(NSOHeader));
  115. pi_header.insert(pi_header.begin() + sizeof(NSOHeader), program_image.data(),
  116. program_image.data() + program_image.size());
  117. pi_header = pm->PatchNSO(pi_header, nso_file.GetName());
  118. std::copy(pi_header.begin() + sizeof(NSOHeader), pi_header.end(), program_image.data());
  119. }
  120. // If we aren't actually loading (i.e. just computing the process code layout), we are done
  121. if (!load_into_process) {
  122. return load_base + image_size;
  123. }
  124. // Apply cheats if they exist and the program has a valid title ID
  125. if (pm) {
  126. system.SetCurrentProcessBuildID(nso_header.build_id);
  127. const auto cheats = pm->CreateCheatList(nso_header.build_id);
  128. if (!cheats.empty()) {
  129. system.RegisterCheatList(cheats, nso_header.build_id, load_base, image_size);
  130. }
  131. }
  132. // Load codeset for current process
  133. codeset.memory = std::move(program_image);
  134. process.LoadModule(std::move(codeset), load_base);
  135. return load_base + image_size;
  136. }
  137. AppLoader_NSO::LoadResult AppLoader_NSO::Load(Kernel::Process& process, Core::System& system) {
  138. if (is_loaded) {
  139. return {ResultStatus::ErrorAlreadyLoaded, {}};
  140. }
  141. modules.clear();
  142. // Load module
  143. const VAddr base_address = process.PageTable().GetCodeRegionStart();
  144. if (!LoadModule(process, system, *file, base_address, true, true)) {
  145. return {ResultStatus::ErrorLoadingNSO, {}};
  146. }
  147. modules.insert_or_assign(base_address, file->GetName());
  148. LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", file->GetName(), base_address);
  149. is_loaded = true;
  150. return {ResultStatus::Success, LoadParameters{Kernel::KThread::DefaultThreadPriority,
  151. Core::Memory::DEFAULT_STACK_SIZE}};
  152. }
  153. ResultStatus AppLoader_NSO::ReadNSOModules(Modules& out_modules) {
  154. out_modules = this->modules;
  155. return ResultStatus::Success;
  156. }
  157. } // namespace Loader