summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/file_sys/patch_manager.cpp22
-rw-r--r--src/core/loader/nso.cpp68
-rw-r--r--src/core/loader/nso.h39
3 files changed, 65 insertions, 64 deletions
diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp
index efc572c72..fd21d3ad1 100644
--- a/src/core/file_sys/patch_manager.cpp
+++ b/src/core/file_sys/patch_manager.cpp
@@ -20,6 +20,7 @@
20#include "core/file_sys/vfs_vector.h" 20#include "core/file_sys/vfs_vector.h"
21#include "core/hle/service/filesystem/filesystem.h" 21#include "core/hle/service/filesystem/filesystem.h"
22#include "core/loader/loader.h" 22#include "core/loader/loader.h"
23#include "core/loader/nso.h"
23#include "core/settings.h" 24#include "core/settings.h"
24 25
25namespace FileSys { 26namespace FileSys {
@@ -32,14 +33,6 @@ constexpr std::array<const char*, 14> EXEFS_FILE_NAMES{
32 "subsdk3", "subsdk4", "subsdk5", "subsdk6", "subsdk7", "subsdk8", "subsdk9", 33 "subsdk3", "subsdk4", "subsdk5", "subsdk6", "subsdk7", "subsdk8", "subsdk9",
33}; 34};
34 35
35struct NSOBuildHeader {
36 u32_le magic;
37 INSERT_PADDING_BYTES(0x3C);
38 std::array<u8, 0x20> build_id;
39 INSERT_PADDING_BYTES(0xA0);
40};
41static_assert(sizeof(NSOBuildHeader) == 0x100, "NSOBuildHeader has incorrect size.");
42
43std::string FormatTitleVersion(u32 version, TitleVersionFormat format) { 36std::string FormatTitleVersion(u32 version, TitleVersionFormat format) {
44 std::array<u8, sizeof(u32)> bytes{}; 37 std::array<u8, sizeof(u32)> bytes{};
45 bytes[0] = version % SINGLE_BYTE_MODULUS; 38 bytes[0] = version % SINGLE_BYTE_MODULUS;
@@ -163,15 +156,16 @@ std::vector<VirtualFile> PatchManager::CollectPatches(const std::vector<VirtualD
163} 156}
164 157
165std::vector<u8> PatchManager::PatchNSO(const std::vector<u8>& nso) const { 158std::vector<u8> PatchManager::PatchNSO(const std::vector<u8>& nso) const {
166 if (nso.size() < sizeof(NSOBuildHeader)) { 159 if (nso.size() < sizeof(Loader::NSOHeader)) {
167 return nso; 160 return nso;
168 } 161 }
169 162
170 NSOBuildHeader header; 163 Loader::NSOHeader header;
171 std::memcpy(&header, nso.data(), sizeof(NSOBuildHeader)); 164 std::memcpy(&header, nso.data(), sizeof(header));
172 165
173 if (header.magic != Common::MakeMagic('N', 'S', 'O', '0')) 166 if (header.magic != Common::MakeMagic('N', 'S', 'O', '0')) {
174 return nso; 167 return nso;
168 }
175 169
176 const auto build_id_raw = Common::HexArrayToString(header.build_id); 170 const auto build_id_raw = Common::HexArrayToString(header.build_id);
177 const auto build_id = build_id_raw.substr(0, build_id_raw.find_last_not_of('0') + 1); 171 const auto build_id = build_id_raw.substr(0, build_id_raw.find_last_not_of('0') + 1);
@@ -214,11 +208,11 @@ std::vector<u8> PatchManager::PatchNSO(const std::vector<u8>& nso) const {
214 } 208 }
215 } 209 }
216 210
217 if (out.size() < sizeof(NSOBuildHeader)) { 211 if (out.size() < sizeof(Loader::NSOHeader)) {
218 return nso; 212 return nso;
219 } 213 }
220 214
221 std::memcpy(out.data(), &header, sizeof(NSOBuildHeader)); 215 std::memcpy(out.data(), &header, sizeof(header));
222 return out; 216 return out;
223} 217}
224 218
diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp
index a52104792..262eaeaee 100644
--- a/src/core/loader/nso.cpp
+++ b/src/core/loader/nso.cpp
@@ -22,47 +22,7 @@
22 22
23namespace Loader { 23namespace Loader {
24 24
25struct NsoSegmentHeader { 25struct MODHeader {
26 u32_le offset;
27 u32_le location;
28 u32_le size;
29 union {
30 u32_le alignment;
31 u32_le bss_size;
32 };
33};
34static_assert(sizeof(NsoSegmentHeader) == 0x10, "NsoSegmentHeader has incorrect size.");
35
36struct NsoHeader {
37 using SHA256Hash = std::array<u8, 0x20>;
38
39 struct RODataRelativeExtent {
40 u32 data_offset;
41 u32 size;
42 };
43
44 u32_le magic;
45 u32_le version;
46 u32 reserved;
47 u32_le flags;
48 std::array<NsoSegmentHeader, 3> segments; // Text, RoData, Data (in that order)
49 std::array<u8, 0x20> build_id;
50 std::array<u32_le, 3> segments_compressed_size;
51 std::array<u8, 0x1C> padding;
52 RODataRelativeExtent api_info_extent;
53 RODataRelativeExtent dynstr_extent;
54 RODataRelativeExtent dynsyn_extent;
55 std::array<SHA256Hash, 3> segment_hashes;
56
57 bool IsSegmentCompressed(size_t segment_num) const {
58 ASSERT_MSG(segment_num < 3, "Invalid segment {}", segment_num);
59 return ((flags >> segment_num) & 1);
60 }
61};
62static_assert(sizeof(NsoHeader) == 0x100, "NsoHeader has incorrect size.");
63static_assert(std::is_trivially_copyable_v<NsoHeader>, "NsoHeader isn't trivially copyable.");
64
65struct ModHeader {
66 u32_le magic; 26 u32_le magic;
67 u32_le dynamic_offset; 27 u32_le dynamic_offset;
68 u32_le bss_start_offset; 28 u32_le bss_start_offset;
@@ -71,7 +31,12 @@ struct ModHeader {
71 u32_le eh_frame_hdr_end_offset; 31 u32_le eh_frame_hdr_end_offset;
72 u32_le module_offset; // Offset to runtime-generated module object. typically equal to .bss base 32 u32_le module_offset; // Offset to runtime-generated module object. typically equal to .bss base
73}; 33};
74static_assert(sizeof(ModHeader) == 0x1c, "ModHeader has incorrect size."); 34static_assert(sizeof(MODHeader) == 0x1c, "MODHeader has incorrect size.");
35
36bool NSOHeader::IsSegmentCompressed(size_t segment_num) const {
37 ASSERT_MSG(segment_num < 3, "Invalid segment {}", segment_num);
38 return ((flags >> segment_num) & 1) != 0;
39}
75 40
76AppLoader_NSO::AppLoader_NSO(FileSys::VirtualFile file) : AppLoader(std::move(file)) {} 41AppLoader_NSO::AppLoader_NSO(FileSys::VirtualFile file) : AppLoader(std::move(file)) {}
77 42
@@ -89,7 +54,7 @@ FileType AppLoader_NSO::IdentifyType(const FileSys::VirtualFile& file) {
89} 54}
90 55
91static std::vector<u8> DecompressSegment(const std::vector<u8>& compressed_data, 56static std::vector<u8> DecompressSegment(const std::vector<u8>& compressed_data,
92 const NsoSegmentHeader& header) { 57 const NSOSegmentHeader& header) {
93 std::vector<u8> uncompressed_data(header.size); 58 std::vector<u8> uncompressed_data(header.size);
94 const int bytes_uncompressed = 59 const int bytes_uncompressed =
95 LZ4_decompress_safe(reinterpret_cast<const char*>(compressed_data.data()), 60 LZ4_decompress_safe(reinterpret_cast<const char*>(compressed_data.data()),
@@ -111,15 +76,18 @@ std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process,
111 const FileSys::VfsFile& file, VAddr load_base, 76 const FileSys::VfsFile& file, VAddr load_base,
112 bool should_pass_arguments, 77 bool should_pass_arguments,
113 std::optional<FileSys::PatchManager> pm) { 78 std::optional<FileSys::PatchManager> pm) {
114 if (file.GetSize() < sizeof(NsoHeader)) 79 if (file.GetSize() < sizeof(NSOHeader)) {
115 return {}; 80 return {};
81 }
116 82
117 NsoHeader nso_header{}; 83 NSOHeader nso_header{};
118 if (sizeof(NsoHeader) != file.ReadObject(&nso_header)) 84 if (sizeof(NSOHeader) != file.ReadObject(&nso_header)) {
119 return {}; 85 return {};
86 }
120 87
121 if (nso_header.magic != Common::MakeMagic('N', 'S', 'O', '0')) 88 if (nso_header.magic != Common::MakeMagic('N', 'S', 'O', '0')) {
122 return {}; 89 return {};
90 }
123 91
124 // Build program image 92 // Build program image
125 Kernel::CodeSet codeset; 93 Kernel::CodeSet codeset;
@@ -155,10 +123,10 @@ std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process,
155 std::memcpy(&module_offset, program_image.data() + 4, sizeof(u32)); 123 std::memcpy(&module_offset, program_image.data() + 4, sizeof(u32));
156 124
157 // Read MOD header 125 // Read MOD header
158 ModHeader mod_header{}; 126 MODHeader mod_header{};
159 // Default .bss to size in segment header if MOD0 section doesn't exist 127 // Default .bss to size in segment header if MOD0 section doesn't exist
160 u32 bss_size{PageAlignSize(nso_header.segments[2].bss_size)}; 128 u32 bss_size{PageAlignSize(nso_header.segments[2].bss_size)};
161 std::memcpy(&mod_header, program_image.data() + module_offset, sizeof(ModHeader)); 129 std::memcpy(&mod_header, program_image.data() + module_offset, sizeof(MODHeader));
162 const bool has_mod_header{mod_header.magic == Common::MakeMagic('M', 'O', 'D', '0')}; 130 const bool has_mod_header{mod_header.magic == Common::MakeMagic('M', 'O', 'D', '0')};
163 if (has_mod_header) { 131 if (has_mod_header) {
164 // Resize program image to include .bss section and page align each section 132 // Resize program image to include .bss section and page align each section
@@ -171,7 +139,7 @@ std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process,
171 // Apply patches if necessary 139 // Apply patches if necessary
172 if (pm && (pm->HasNSOPatch(nso_header.build_id) || Settings::values.dump_nso)) { 140 if (pm && (pm->HasNSOPatch(nso_header.build_id) || Settings::values.dump_nso)) {
173 std::vector<u8> pi_header(program_image.size() + 0x100); 141 std::vector<u8> pi_header(program_image.size() + 0x100);
174 std::memcpy(pi_header.data(), &nso_header, sizeof(NsoHeader)); 142 std::memcpy(pi_header.data(), &nso_header, sizeof(NSOHeader));
175 std::memcpy(pi_header.data() + 0x100, program_image.data(), program_image.size()); 143 std::memcpy(pi_header.data() + 0x100, program_image.data(), program_image.size());
176 144
177 pi_header = pm->PatchNSO(pi_header); 145 pi_header = pm->PatchNSO(pi_header);
diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h
index 167c8a694..4674c3724 100644
--- a/src/core/loader/nso.h
+++ b/src/core/loader/nso.h
@@ -4,7 +4,9 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <array>
7#include <optional> 8#include <optional>
9#include <type_traits>
8#include "common/common_types.h" 10#include "common/common_types.h"
9#include "common/swap.h" 11#include "common/swap.h"
10#include "core/file_sys/patch_manager.h" 12#include "core/file_sys/patch_manager.h"
@@ -16,6 +18,43 @@ class Process;
16 18
17namespace Loader { 19namespace Loader {
18 20
21struct NSOSegmentHeader {
22 u32_le offset;
23 u32_le location;
24 u32_le size;
25 union {
26 u32_le alignment;
27 u32_le bss_size;
28 };
29};
30static_assert(sizeof(NSOSegmentHeader) == 0x10, "NsoSegmentHeader has incorrect size.");
31
32struct NSOHeader {
33 using SHA256Hash = std::array<u8, 0x20>;
34
35 struct RODataRelativeExtent {
36 u32_le data_offset;
37 u32_le size;
38 };
39
40 u32_le magic;
41 u32_le version;
42 u32 reserved;
43 u32_le flags;
44 std::array<NSOSegmentHeader, 3> segments; // Text, RoData, Data (in that order)
45 std::array<u8, 0x20> build_id;
46 std::array<u32_le, 3> segments_compressed_size;
47 std::array<u8, 0x1C> padding;
48 RODataRelativeExtent api_info_extent;
49 RODataRelativeExtent dynstr_extent;
50 RODataRelativeExtent dynsyn_extent;
51 std::array<SHA256Hash, 3> segment_hashes;
52
53 bool IsSegmentCompressed(size_t segment_num) const;
54};
55static_assert(sizeof(NSOHeader) == 0x100, "NSOHeader has incorrect size.");
56static_assert(std::is_trivially_copyable_v<NSOHeader>, "NSOHeader must be trivially copyable.");
57
19constexpr u64 NSO_ARGUMENT_DATA_ALLOCATION_SIZE = 0x9000; 58constexpr u64 NSO_ARGUMENT_DATA_ALLOCATION_SIZE = 0x9000;
20 59
21struct NSOArgumentHeader { 60struct NSOArgumentHeader {