nso.cpp 7.0 KB

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