summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/CMakeLists.txt2
-rw-r--r--src/core/file_sys/program_metadata.cpp114
-rw-r--r--src/core/file_sys/program_metadata.h154
-rw-r--r--src/core/loader/deconstructed_rom_directory.cpp23
-rw-r--r--src/core/loader/deconstructed_rom_directory.h2
-rw-r--r--src/core/loader/nso.cpp6
-rw-r--r--src/core/loader/nso.h2
7 files changed, 294 insertions, 9 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index ec011787e..1bc536075 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -12,6 +12,8 @@ add_library(core STATIC
12 file_sys/filesystem.h 12 file_sys/filesystem.h
13 file_sys/path_parser.cpp 13 file_sys/path_parser.cpp
14 file_sys/path_parser.h 14 file_sys/path_parser.h
15 file_sys/program_metadata.cpp
16 file_sys/program_metadata.h
15 file_sys/romfs_factory.cpp 17 file_sys/romfs_factory.cpp
16 file_sys/romfs_factory.h 18 file_sys/romfs_factory.h
17 file_sys/romfs_filesystem.cpp 19 file_sys/romfs_filesystem.cpp
diff --git a/src/core/file_sys/program_metadata.cpp b/src/core/file_sys/program_metadata.cpp
new file mode 100644
index 000000000..a6dcebcc3
--- /dev/null
+++ b/src/core/file_sys/program_metadata.cpp
@@ -0,0 +1,114 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <cinttypes>
6#include "common/file_util.h"
7#include "common/logging/log.h"
8#include "core/file_sys/program_metadata.h"
9#include "core/loader/loader.h"
10
11namespace FileSys {
12
13Loader::ResultStatus ProgramMetadata::Load(const std::string& file_path) {
14 FileUtil::IOFile file(file_path, "rb");
15 if (!file.IsOpen())
16 return Loader::ResultStatus::Error;
17
18 std::vector<u8> file_data(file.GetSize());
19
20 if (!file.ReadBytes(file_data.data(), file_data.size()))
21 return Loader::ResultStatus::Error;
22
23 Loader::ResultStatus result = Load(file_data);
24 if (result != Loader::ResultStatus::Success)
25 LOG_ERROR(Service_FS, "Failed to load NPDM from file %s!", file_path.c_str());
26
27 return result;
28}
29
30Loader::ResultStatus ProgramMetadata::Load(const std::vector<u8> file_data, size_t offset) {
31 size_t total_size = static_cast<size_t>(file_data.size() - offset);
32 if (total_size < sizeof(Header))
33 return Loader::ResultStatus::Error;
34
35 size_t header_offset = offset;
36 memcpy(&npdm_header, &file_data[offset], sizeof(Header));
37
38 size_t aci_offset = header_offset + npdm_header.aci_offset;
39 size_t acid_offset = header_offset + npdm_header.acid_offset;
40 memcpy(&aci_header, &file_data[aci_offset], sizeof(AciHeader));
41 memcpy(&acid_header, &file_data[acid_offset], sizeof(AcidHeader));
42
43 size_t fac_offset = acid_offset + acid_header.fac_offset;
44 size_t fah_offset = aci_offset + aci_header.fah_offset;
45 memcpy(&acid_file_access, &file_data[fac_offset], sizeof(FileAccessControl));
46 memcpy(&aci_file_access, &file_data[fah_offset], sizeof(FileAccessHeader));
47
48 return Loader::ResultStatus::Success;
49}
50
51bool ProgramMetadata::Is64BitProgram() const {
52 return npdm_header.has_64_bit_instructions;
53}
54
55ProgramAddressSpaceType ProgramMetadata::GetAddressSpaceType() const {
56 return npdm_header.address_space_type;
57}
58
59u8 ProgramMetadata::GetMainThreadPriority() const {
60 return npdm_header.main_thread_priority;
61}
62
63u8 ProgramMetadata::GetMainThreadCore() const {
64 return npdm_header.main_thread_cpu;
65}
66
67u32 ProgramMetadata::GetMainThreadStackSize() const {
68 return npdm_header.main_stack_size;
69}
70
71u64 ProgramMetadata::GetTitleID() const {
72 return aci_header.title_id;
73}
74
75u64 ProgramMetadata::GetFilesystemPermissions() const {
76 return aci_file_access.permissions;
77}
78
79void ProgramMetadata::Print() const {
80 LOG_DEBUG(Service_FS, "Magic: %.4s", npdm_header.magic.data());
81 LOG_DEBUG(Service_FS, "Main thread priority: 0x%02x", npdm_header.main_thread_priority);
82 LOG_DEBUG(Service_FS, "Main thread core: %u", npdm_header.main_thread_cpu);
83 LOG_DEBUG(Service_FS, "Main thread stack size: 0x%x bytes", npdm_header.main_stack_size);
84 LOG_DEBUG(Service_FS, "Process category: %u", npdm_header.process_category);
85 LOG_DEBUG(Service_FS, "Flags: %02x", npdm_header.flags);
86 LOG_DEBUG(Service_FS, " > 64-bit instructions: %s",
87 npdm_header.has_64_bit_instructions ? "YES" : "NO");
88
89 auto address_space = "Unknown";
90 switch (npdm_header.address_space_type) {
91 case ProgramAddressSpaceType::Is64Bit:
92 address_space = "64-bit";
93 break;
94 case ProgramAddressSpaceType::Is32Bit:
95 address_space = "32-bit";
96 break;
97 }
98
99 LOG_DEBUG(Service_FS, " > Address space: %s\n", address_space);
100
101 // Begin ACID printing (potential perms, signed)
102 LOG_DEBUG(Service_FS, "Magic: %.4s", acid_header.magic.data());
103 LOG_DEBUG(Service_FS, "Flags: %02x", acid_header.flags);
104 LOG_DEBUG(Service_FS, " > Is Retail: %s", acid_header.is_retail ? "YES" : "NO");
105 LOG_DEBUG(Service_FS, "Title ID Min: %016" PRIX64, acid_header.title_id_min);
106 LOG_DEBUG(Service_FS, "Title ID Max: %016" PRIX64, acid_header.title_id_max);
107 LOG_DEBUG(Service_FS, "Filesystem Access: %016" PRIX64 "\n", acid_file_access.permissions);
108
109 // Begin ACI0 printing (actual perms, unsigned)
110 LOG_DEBUG(Service_FS, "Magic: %.4s", aci_header.magic.data());
111 LOG_DEBUG(Service_FS, "Title ID: %016" PRIX64, aci_header.title_id);
112 LOG_DEBUG(Service_FS, "Filesystem Access: %016" PRIX64 "\n", aci_file_access.permissions);
113}
114} // namespace FileSys
diff --git a/src/core/file_sys/program_metadata.h b/src/core/file_sys/program_metadata.h
new file mode 100644
index 000000000..b80a08485
--- /dev/null
+++ b/src/core/file_sys/program_metadata.h
@@ -0,0 +1,154 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <array>
8#include <string>
9#include <vector>
10#include "common/bit_field.h"
11#include "common/common_types.h"
12#include "common/swap.h"
13
14namespace Loader {
15enum class ResultStatus;
16}
17
18namespace FileSys {
19
20enum class ProgramAddressSpaceType : u8 {
21 Is64Bit = 1,
22 Is32Bit = 2,
23};
24
25enum class ProgramFilePermission : u64 {
26 MountContent = 1ULL << 0,
27 SaveDataBackup = 1ULL << 5,
28 SdCard = 1ULL << 21,
29 Calibration = 1ULL << 34,
30 Bit62 = 1ULL << 62,
31 Everything = 1ULL << 63,
32};
33
34/**
35 * Helper which implements an interface to parse Program Description Metadata (NPDM)
36 * Data can either be loaded from a file path or with data and an offset into it.
37 */
38class ProgramMetadata {
39public:
40 Loader::ResultStatus Load(const std::string& file_path);
41 Loader::ResultStatus Load(const std::vector<u8> file_data, size_t offset = 0);
42
43 bool Is64BitProgram() const;
44 ProgramAddressSpaceType GetAddressSpaceType() const;
45 u8 GetMainThreadPriority() const;
46 u8 GetMainThreadCore() const;
47 u32 GetMainThreadStackSize() const;
48 u64 GetTitleID() const;
49 u64 GetFilesystemPermissions() const;
50
51 void Print() const;
52
53private:
54 struct Header {
55 std::array<char, 4> magic;
56 std::array<u8, 8> reserved;
57 union {
58 u8 flags;
59
60 BitField<0, 1, u8> has_64_bit_instructions;
61 BitField<1, 3, ProgramAddressSpaceType> address_space_type;
62 BitField<4, 4, u8> reserved_2;
63 };
64 u8 reserved_3;
65 u8 main_thread_priority;
66 u8 main_thread_cpu;
67 std::array<u8, 8> reserved_4;
68 u32_le process_category;
69 u32_le main_stack_size;
70 std::array<u8, 0x10> application_name;
71 std::array<u8, 0x40> reserved_5;
72 u32_le aci_offset;
73 u32_le aci_size;
74 u32_le acid_offset;
75 u32_le acid_size;
76 };
77
78 static_assert(sizeof(Header) == 0x80, "NPDM header structure size is wrong");
79
80 struct AcidHeader {
81 std::array<u8, 0x100> signature;
82 std::array<u8, 0x100> nca_modulus;
83 std::array<char, 4> magic;
84 u32_le nca_size;
85 std::array<u8, 0x4> reserved;
86 union {
87 u32 flags;
88
89 BitField<0, 1, u32> is_retail;
90 BitField<1, 31, u32> flags_unk;
91 };
92 u64_le title_id_min;
93 u64_le title_id_max;
94 u32_le fac_offset;
95 u32_le fac_size;
96 u32_le sac_offset;
97 u32_le sac_size;
98 u32_le kac_offset;
99 u32_le kac_size;
100 INSERT_PADDING_BYTES(0x8);
101 };
102
103 static_assert(sizeof(AcidHeader) == 0x240, "ACID header structure size is wrong");
104
105 struct AciHeader {
106 std::array<char, 4> magic;
107 std::array<u8, 0xC> reserved;
108 u64_le title_id;
109 INSERT_PADDING_BYTES(0x8);
110 u32_le fah_offset;
111 u32_le fah_size;
112 u32_le sac_offset;
113 u32_le sac_size;
114 u32_le kac_offset;
115 u32_le kac_size;
116 INSERT_PADDING_BYTES(0x8);
117 };
118
119 static_assert(sizeof(AciHeader) == 0x40, "ACI0 header structure size is wrong");
120
121#pragma pack(push, 1)
122
123 struct FileAccessControl {
124 u8 version;
125 INSERT_PADDING_BYTES(3);
126 u64_le permissions;
127 std::array<u8, 0x20> unknown;
128 };
129
130 static_assert(sizeof(FileAccessControl) == 0x2C, "FS access control structure size is wrong");
131
132 struct FileAccessHeader {
133 u8 version;
134 INSERT_PADDING_BYTES(3);
135 u64_le permissions;
136 u32_le unk_offset;
137 u32_le unk_size;
138 u32_le unk_offset_2;
139 u32_le unk_size_2;
140 };
141
142 static_assert(sizeof(FileAccessHeader) == 0x1C, "FS access header structure size is wrong");
143
144#pragma pack(pop)
145
146 Header npdm_header;
147 AciHeader aci_header;
148 AcidHeader acid_header;
149
150 FileAccessControl acid_file_access;
151 FileAccessHeader aci_file_access;
152};
153
154} // namespace FileSys
diff --git a/src/core/loader/deconstructed_rom_directory.cpp b/src/core/loader/deconstructed_rom_directory.cpp
index 661803b5f..864cf25cd 100644
--- a/src/core/loader/deconstructed_rom_directory.cpp
+++ b/src/core/loader/deconstructed_rom_directory.cpp
@@ -53,6 +53,7 @@ AppLoader_DeconstructedRomDirectory::AppLoader_DeconstructedRomDirectory(FileUti
53FileType AppLoader_DeconstructedRomDirectory::IdentifyType(FileUtil::IOFile& file, 53FileType AppLoader_DeconstructedRomDirectory::IdentifyType(FileUtil::IOFile& file,
54 const std::string& filepath) { 54 const std::string& filepath) {
55 bool is_main_found{}; 55 bool is_main_found{};
56 bool is_npdm_found{};
56 bool is_rtld_found{}; 57 bool is_rtld_found{};
57 bool is_sdk_found{}; 58 bool is_sdk_found{};
58 59
@@ -67,6 +68,9 @@ FileType AppLoader_DeconstructedRomDirectory::IdentifyType(FileUtil::IOFile& fil
67 // Verify filename 68 // Verify filename
68 if (Common::ToLower(virtual_name) == "main") { 69 if (Common::ToLower(virtual_name) == "main") {
69 is_main_found = true; 70 is_main_found = true;
71 } else if (Common::ToLower(virtual_name) == "main.npdm") {
72 is_npdm_found = true;
73 return true;
70 } else if (Common::ToLower(virtual_name) == "rtld") { 74 } else if (Common::ToLower(virtual_name) == "rtld") {
71 is_rtld_found = true; 75 is_rtld_found = true;
72 } else if (Common::ToLower(virtual_name) == "sdk") { 76 } else if (Common::ToLower(virtual_name) == "sdk") {
@@ -83,14 +87,14 @@ FileType AppLoader_DeconstructedRomDirectory::IdentifyType(FileUtil::IOFile& fil
83 } 87 }
84 88
85 // We are done if we've found and verified all required NSOs 89 // We are done if we've found and verified all required NSOs
86 return !(is_main_found && is_rtld_found && is_sdk_found); 90 return !(is_main_found && is_npdm_found && is_rtld_found && is_sdk_found);
87 }; 91 };
88 92
89 // Search the directory recursively, looking for the required modules 93 // Search the directory recursively, looking for the required modules
90 const std::string directory = filepath.substr(0, filepath.find_last_of("/\\")) + DIR_SEP; 94 const std::string directory = filepath.substr(0, filepath.find_last_of("/\\")) + DIR_SEP;
91 FileUtil::ForeachDirectoryEntry(nullptr, directory, callback); 95 FileUtil::ForeachDirectoryEntry(nullptr, directory, callback);
92 96
93 if (is_main_found && is_rtld_found && is_sdk_found) { 97 if (is_main_found && is_npdm_found && is_rtld_found && is_sdk_found) {
94 return FileType::DeconstructedRomDirectory; 98 return FileType::DeconstructedRomDirectory;
95 } 99 }
96 100
@@ -108,14 +112,22 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load(
108 112
109 process = Kernel::Process::Create("main"); 113 process = Kernel::Process::Create("main");
110 114
115 const std::string directory = filepath.substr(0, filepath.find_last_of("/\\")) + DIR_SEP;
116 const std::string npdm_path = directory + DIR_SEP + "main.npdm";
117
118 ResultStatus result = metadata.Load(npdm_path);
119 if (result != ResultStatus::Success) {
120 return result;
121 }
122 metadata.Print();
123
111 // Load NSO modules 124 // Load NSO modules
112 VAddr next_load_addr{Memory::PROCESS_IMAGE_VADDR}; 125 VAddr next_load_addr{Memory::PROCESS_IMAGE_VADDR};
113 const std::string directory = filepath.substr(0, filepath.find_last_of("/\\")) + DIR_SEP;
114 for (const auto& module : {"rtld", "main", "subsdk0", "subsdk1", "subsdk2", "subsdk3", 126 for (const auto& module : {"rtld", "main", "subsdk0", "subsdk1", "subsdk2", "subsdk3",
115 "subsdk4", "subsdk5", "subsdk6", "subsdk7", "sdk"}) { 127 "subsdk4", "subsdk5", "subsdk6", "subsdk7", "sdk"}) {
116 const std::string path = directory + DIR_SEP + module; 128 const std::string path = directory + DIR_SEP + module;
117 const VAddr load_addr = next_load_addr; 129 const VAddr load_addr = next_load_addr;
118 next_load_addr = AppLoader_NSO::LoadModule(path, load_addr); 130 next_load_addr = AppLoader_NSO::LoadModule(path, load_addr, metadata.GetTitleID());
119 if (next_load_addr) { 131 if (next_load_addr) {
120 LOG_DEBUG(Loader, "loaded module %s @ 0x%" PRIx64, module, load_addr); 132 LOG_DEBUG(Loader, "loaded module %s @ 0x%" PRIx64, module, load_addr);
121 } else { 133 } else {
@@ -127,7 +139,8 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load(
127 process->address_mappings = default_address_mappings; 139 process->address_mappings = default_address_mappings;
128 process->resource_limit = 140 process->resource_limit =
129 Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); 141 Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
130 process->Run(Memory::PROCESS_IMAGE_VADDR, 48, Kernel::DEFAULT_STACK_SIZE); 142 process->Run(Memory::PROCESS_IMAGE_VADDR, metadata.GetMainThreadPriority(),
143 metadata.GetMainThreadStackSize());
131 144
132 // Find the RomFS by searching for a ".romfs" file in this directory 145 // Find the RomFS by searching for a ".romfs" file in this directory
133 filepath_romfs = FindRomFS(directory); 146 filepath_romfs = FindRomFS(directory);
diff --git a/src/core/loader/deconstructed_rom_directory.h b/src/core/loader/deconstructed_rom_directory.h
index 536a2dab7..23295d911 100644
--- a/src/core/loader/deconstructed_rom_directory.h
+++ b/src/core/loader/deconstructed_rom_directory.h
@@ -6,6 +6,7 @@
6 6
7#include <string> 7#include <string>
8#include "common/common_types.h" 8#include "common/common_types.h"
9#include "core/file_sys/program_metadata.h"
9#include "core/hle/kernel/kernel.h" 10#include "core/hle/kernel/kernel.h"
10#include "core/loader/loader.h" 11#include "core/loader/loader.h"
11 12
@@ -41,6 +42,7 @@ public:
41private: 42private:
42 std::string filepath_romfs; 43 std::string filepath_romfs;
43 std::string filepath; 44 std::string filepath;
45 FileSys::ProgramMetadata metadata;
44}; 46};
45 47
46} // namespace Loader 48} // namespace Loader
diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp
index 407025da0..7f8d24dd6 100644
--- a/src/core/loader/nso.cpp
+++ b/src/core/loader/nso.cpp
@@ -92,7 +92,7 @@ static constexpr u32 PageAlignSize(u32 size) {
92 return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; 92 return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK;
93} 93}
94 94
95VAddr AppLoader_NSO::LoadModule(const std::string& path, VAddr load_base) { 95VAddr AppLoader_NSO::LoadModule(const std::string& path, VAddr load_base, u64 tid) {
96 FileUtil::IOFile file(path, "rb"); 96 FileUtil::IOFile file(path, "rb");
97 if (!file.IsOpen()) { 97 if (!file.IsOpen()) {
98 return {}; 98 return {};
@@ -109,7 +109,7 @@ VAddr AppLoader_NSO::LoadModule(const std::string& path, VAddr load_base) {
109 } 109 }
110 110
111 // Build program image 111 // Build program image
112 Kernel::SharedPtr<Kernel::CodeSet> codeset = Kernel::CodeSet::Create("", 0); 112 Kernel::SharedPtr<Kernel::CodeSet> codeset = Kernel::CodeSet::Create("", tid);
113 std::vector<u8> program_image; 113 std::vector<u8> program_image;
114 for (int i = 0; i < nso_header.segments.size(); ++i) { 114 for (int i = 0; i < nso_header.segments.size(); ++i) {
115 std::vector<u8> data = 115 std::vector<u8> data =
@@ -158,7 +158,7 @@ ResultStatus AppLoader_NSO::Load(Kernel::SharedPtr<Kernel::Process>& process) {
158 process = Kernel::Process::Create("main"); 158 process = Kernel::Process::Create("main");
159 159
160 // Load module 160 // Load module
161 LoadModule(filepath, Memory::PROCESS_IMAGE_VADDR); 161 LoadModule(filepath, Memory::PROCESS_IMAGE_VADDR, 0);
162 LOG_DEBUG(Loader, "loaded module %s @ 0x%" PRIx64, filepath.c_str(), 162 LOG_DEBUG(Loader, "loaded module %s @ 0x%" PRIx64, filepath.c_str(),
163 Memory::PROCESS_IMAGE_VADDR); 163 Memory::PROCESS_IMAGE_VADDR);
164 164
diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h
index 1ae30a824..14eb1d87e 100644
--- a/src/core/loader/nso.h
+++ b/src/core/loader/nso.h
@@ -29,7 +29,7 @@ public:
29 return IdentifyType(file, filepath); 29 return IdentifyType(file, filepath);
30 } 30 }
31 31
32 static VAddr LoadModule(const std::string& path, VAddr load_base); 32 static VAddr LoadModule(const std::string& path, VAddr load_base, u64 tid);
33 33
34 ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; 34 ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override;
35 35