summaryrefslogtreecommitdiff
path: root/src/core/loader
diff options
context:
space:
mode:
authorGravatar bunnei2016-05-25 16:40:36 -0400
committerGravatar bunnei2016-05-25 16:40:36 -0400
commitf50a32bfce099eeff510e5e314b91dcf65d317ac (patch)
tree5bc0347199d722861ba09897e6c6e9015cf026bc /src/core/loader
parentNew3DS: Minor style cleanup to #1520. (diff)
parentLoader: Split SMDH into its own header and import helpers from QGameList (diff)
downloadyuzu-f50a32bfce099eeff510e5e314b91dcf65d317ac.tar.gz
yuzu-f50a32bfce099eeff510e5e314b91dcf65d317ac.tar.xz
yuzu-f50a32bfce099eeff510e5e314b91dcf65d317ac.zip
Merge pull request #1817 from linkmauve/smdh-stuff
Improve SMDH support in loaders and frontends
Diffstat (limited to 'src/core/loader')
-rw-r--r--src/core/loader/3dsx.cpp3
-rw-r--r--src/core/loader/3dsx.h8
-rw-r--r--src/core/loader/elf.h8
-rw-r--r--src/core/loader/loader.cpp60
-rw-r--r--src/core/loader/loader.h69
-rw-r--r--src/core/loader/ncch.cpp9
-rw-r--r--src/core/loader/ncch.h8
-rw-r--r--src/core/loader/smdh.cpp54
-rw-r--r--src/core/loader/smdh.h82
9 files changed, 196 insertions, 105 deletions
diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp
index 98e7ab48f..a16411e14 100644
--- a/src/core/loader/3dsx.cpp
+++ b/src/core/loader/3dsx.cpp
@@ -10,6 +10,7 @@
10#include "core/file_sys/archive_romfs.h" 10#include "core/file_sys/archive_romfs.h"
11#include "core/hle/kernel/process.h" 11#include "core/hle/kernel/process.h"
12#include "core/hle/kernel/resource_limit.h" 12#include "core/hle/kernel/resource_limit.h"
13#include "core/hle/service/fs/archive.h"
13#include "core/loader/3dsx.h" 14#include "core/loader/3dsx.h"
14#include "core/memory.h" 15#include "core/memory.h"
15 16
@@ -263,6 +264,8 @@ ResultStatus AppLoader_THREEDSX::Load() {
263 264
264 Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE); 265 Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE);
265 266
267 Service::FS::RegisterArchiveType(std::make_unique<FileSys::ArchiveFactory_RomFS>(*this), Service::FS::ArchiveIdCode::RomFS);
268
266 is_loaded = true; 269 is_loaded = true;
267 return ResultStatus::Success; 270 return ResultStatus::Success;
268} 271}
diff --git a/src/core/loader/3dsx.h b/src/core/loader/3dsx.h
index 3ee686703..90b20c61c 100644
--- a/src/core/loader/3dsx.h
+++ b/src/core/loader/3dsx.h
@@ -28,6 +28,14 @@ public:
28 static FileType IdentifyType(FileUtil::IOFile& file); 28 static FileType IdentifyType(FileUtil::IOFile& file);
29 29
30 /** 30 /**
31 * Returns the type of this file
32 * @return FileType corresponding to the loaded file
33 */
34 FileType GetFileType() override {
35 return IdentifyType(file);
36 }
37
38 /**
31 * Load the bootable file 39 * Load the bootable file
32 * @return ResultStatus result of function 40 * @return ResultStatus result of function
33 */ 41 */
diff --git a/src/core/loader/elf.h b/src/core/loader/elf.h
index c6a5ebe99..cb3724f9d 100644
--- a/src/core/loader/elf.h
+++ b/src/core/loader/elf.h
@@ -28,6 +28,14 @@ public:
28 static FileType IdentifyType(FileUtil::IOFile& file); 28 static FileType IdentifyType(FileUtil::IOFile& file);
29 29
30 /** 30 /**
31 * Returns the type of this file
32 * @return FileType corresponding to the loaded file
33 */
34 FileType GetFileType() override {
35 return IdentifyType(file);
36 }
37
38 /**
31 * Load the bootable file 39 * Load the bootable file
32 * @return ResultStatus result of function 40 * @return ResultStatus result of function
33 */ 41 */
diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp
index af3f62248..9719d30d5 100644
--- a/src/core/loader/loader.cpp
+++ b/src/core/loader/loader.cpp
@@ -8,9 +8,7 @@
8#include "common/logging/log.h" 8#include "common/logging/log.h"
9#include "common/string_util.h" 9#include "common/string_util.h"
10 10
11#include "core/file_sys/archive_romfs.h"
12#include "core/hle/kernel/process.h" 11#include "core/hle/kernel/process.h"
13#include "core/hle/service/fs/archive.h"
14#include "core/loader/3dsx.h" 12#include "core/loader/3dsx.h"
15#include "core/loader/elf.h" 13#include "core/loader/elf.h"
16#include "core/loader/ncch.h" 14#include "core/loader/ncch.h"
@@ -67,6 +65,9 @@ FileType GuessFromExtension(const std::string& extension_) {
67 if (extension == ".3dsx") 65 if (extension == ".3dsx")
68 return FileType::THREEDSX; 66 return FileType::THREEDSX;
69 67
68 if (extension == ".cia")
69 return FileType::CIA;
70
70 return FileType::Unknown; 71 return FileType::Unknown;
71} 72}
72 73
@@ -90,7 +91,15 @@ const char* GetFileTypeString(FileType type) {
90 return "unknown"; 91 return "unknown";
91} 92}
92 93
93std::unique_ptr<AppLoader> GetLoader(FileUtil::IOFile&& file, FileType type, 94/**
95 * Get a loader for a file with a specific type
96 * @param file The file to load
97 * @param type The type of the file
98 * @param filename the file name (without path)
99 * @param filepath the file full path (with name)
100 * @return std::unique_ptr<AppLoader> a pointer to a loader object; nullptr for unsupported type
101 */
102static std::unique_ptr<AppLoader> GetFileLoader(FileUtil::IOFile&& file, FileType type,
94 const std::string& filename, const std::string& filepath) { 103 const std::string& filename, const std::string& filepath) {
95 switch (type) { 104 switch (type) {
96 105
@@ -108,15 +117,15 @@ std::unique_ptr<AppLoader> GetLoader(FileUtil::IOFile&& file, FileType type,
108 return std::make_unique<AppLoader_NCCH>(std::move(file), filepath); 117 return std::make_unique<AppLoader_NCCH>(std::move(file), filepath);
109 118
110 default: 119 default:
111 return std::unique_ptr<AppLoader>(); 120 return nullptr;
112 } 121 }
113} 122}
114 123
115ResultStatus LoadFile(const std::string& filename) { 124std::unique_ptr<AppLoader> GetLoader(const std::string& filename) {
116 FileUtil::IOFile file(filename, "rb"); 125 FileUtil::IOFile file(filename, "rb");
117 if (!file.IsOpen()) { 126 if (!file.IsOpen()) {
118 LOG_ERROR(Loader, "Failed to load file %s", filename.c_str()); 127 LOG_ERROR(Loader, "Failed to load file %s", filename.c_str());
119 return ResultStatus::Error; 128 return nullptr;
120 } 129 }
121 130
122 std::string filename_filename, filename_extension; 131 std::string filename_filename, filename_extension;
@@ -133,44 +142,7 @@ ResultStatus LoadFile(const std::string& filename) {
133 142
134 LOG_INFO(Loader, "Loading file %s as %s...", filename.c_str(), GetFileTypeString(type)); 143 LOG_INFO(Loader, "Loading file %s as %s...", filename.c_str(), GetFileTypeString(type));
135 144
136 std::unique_ptr<AppLoader> app_loader = GetLoader(std::move(file), type, filename_filename, filename); 145 return GetFileLoader(std::move(file), type, filename_filename, filename);
137
138 switch (type) {
139
140 // 3DSX file format...
141 // or NCCH/NCSD container formats...
142 case FileType::THREEDSX:
143 case FileType::CXI:
144 case FileType::CCI:
145 {
146 // Load application and RomFS
147 ResultStatus result = app_loader->Load();
148 if (ResultStatus::Success == result) {
149 Service::FS::RegisterArchiveType(std::make_unique<FileSys::ArchiveFactory_RomFS>(*app_loader), Service::FS::ArchiveIdCode::RomFS);
150 return ResultStatus::Success;
151 }
152 return result;
153 }
154
155 // Standard ELF file format...
156 case FileType::ELF:
157 return app_loader->Load();
158
159 // CIA file format...
160 case FileType::CIA:
161 return ResultStatus::ErrorNotImplemented;
162
163 // Error occurred durring IdentifyFile...
164 case FileType::Error:
165
166 // IdentifyFile could know identify file type...
167 case FileType::Unknown:
168 {
169 LOG_CRITICAL(Loader, "File %s is of unknown type.", filename.c_str());
170 return ResultStatus::ErrorInvalidFormat;
171 }
172 }
173 return ResultStatus::Error;
174} 146}
175 147
176} // namespace Loader 148} // namespace Loader
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h
index 9d3e9ed3b..77d87afe1 100644
--- a/src/core/loader/loader.h
+++ b/src/core/loader/loader.h
@@ -10,10 +10,8 @@
10#include <string> 10#include <string>
11#include <vector> 11#include <vector>
12 12
13#include "common/common_funcs.h"
14#include "common/common_types.h" 13#include "common/common_types.h"
15#include "common/file_util.h" 14#include "common/file_util.h"
16#include "common/swap.h"
17 15
18namespace Kernel { 16namespace Kernel {
19struct AddressMapping; 17struct AddressMapping;
@@ -80,51 +78,6 @@ constexpr u32 MakeMagic(char a, char b, char c, char d) {
80 return a | b << 8 | c << 16 | d << 24; 78 return a | b << 8 | c << 16 | d << 24;
81} 79}
82 80
83/// SMDH data structure that contains titles, icons etc. See https://www.3dbrew.org/wiki/SMDH
84struct SMDH {
85 u32_le magic;
86 u16_le version;
87 INSERT_PADDING_BYTES(2);
88
89 struct Title {
90 std::array<u16, 0x40> short_title;
91 std::array<u16, 0x80> long_title;
92 std::array<u16, 0x40> publisher;
93 };
94 std::array<Title, 16> titles;
95
96 std::array<u8, 16> ratings;
97 u32_le region_lockout;
98 u32_le match_maker_id;
99 u64_le match_maker_bit_id;
100 u32_le flags;
101 u16_le eula_version;
102 INSERT_PADDING_BYTES(2);
103 float_le banner_animation_frame;
104 u32_le cec_id;
105 INSERT_PADDING_BYTES(8);
106
107 std::array<u8, 0x480> small_icon;
108 std::array<u8, 0x1200> large_icon;
109
110 /// indicates the language used for each title entry
111 enum class TitleLanguage {
112 Japanese = 0,
113 English = 1,
114 French = 2,
115 German = 3,
116 Italian = 4,
117 Spanish = 5,
118 SimplifiedChinese = 6,
119 Korean= 7,
120 Dutch = 8,
121 Portuguese = 9,
122 Russian = 10,
123 TraditionalChinese = 11
124 };
125};
126static_assert(sizeof(SMDH) == 0x36C0, "SMDH structure size is wrong");
127
128/// Interface for loading an application 81/// Interface for loading an application
129class AppLoader : NonCopyable { 82class AppLoader : NonCopyable {
130public: 83public:
@@ -132,6 +85,12 @@ public:
132 virtual ~AppLoader() { } 85 virtual ~AppLoader() { }
133 86
134 /** 87 /**
88 * Returns the type of this file
89 * @return FileType corresponding to the loaded file
90 */
91 virtual FileType GetFileType() = 0;
92
93 /**
135 * Load the application 94 * Load the application
136 * @return ResultStatus result of function 95 * @return ResultStatus result of function
137 */ 96 */
@@ -197,20 +156,10 @@ protected:
197extern const std::initializer_list<Kernel::AddressMapping> default_address_mappings; 156extern const std::initializer_list<Kernel::AddressMapping> default_address_mappings;
198 157
199/** 158/**
200 * Get a loader for a file with a specific type 159 * Identifies a bootable file and return a suitable loader
201 * @param file The file to load
202 * @param type The type of the file
203 * @param filename the file name (without path)
204 * @param filepath the file full path (with name)
205 * @return std::unique_ptr<AppLoader> a pointer to a loader object; nullptr for unsupported type
206 */
207std::unique_ptr<AppLoader> GetLoader(FileUtil::IOFile&& file, FileType type, const std::string& filename, const std::string& filepath);
208
209/**
210 * Identifies and loads a bootable file
211 * @param filename String filename of bootable file 160 * @param filename String filename of bootable file
212 * @return ResultStatus result of function 161 * @return best loader for this file
213 */ 162 */
214ResultStatus LoadFile(const std::string& filename); 163std::unique_ptr<AppLoader> GetLoader(const std::string& filename);
215 164
216} // namespace 165} // namespace
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp
index 7391bdb26..fca091ff9 100644
--- a/src/core/loader/ncch.cpp
+++ b/src/core/loader/ncch.cpp
@@ -10,8 +10,10 @@
10#include "common/string_util.h" 10#include "common/string_util.h"
11#include "common/swap.h" 11#include "common/swap.h"
12 12
13#include "core/file_sys/archive_romfs.h"
13#include "core/hle/kernel/process.h" 14#include "core/hle/kernel/process.h"
14#include "core/hle/kernel/resource_limit.h" 15#include "core/hle/kernel/resource_limit.h"
16#include "core/hle/service/fs/archive.h"
15#include "core/loader/ncch.h" 17#include "core/loader/ncch.h"
16#include "core/memory.h" 18#include "core/memory.h"
17 19
@@ -303,7 +305,12 @@ ResultStatus AppLoader_NCCH::Load() {
303 305
304 is_loaded = true; // Set state to loaded 306 is_loaded = true; // Set state to loaded
305 307
306 return LoadExec(); // Load the executable into memory for booting 308 result = LoadExec(); // Load the executable into memory for booting
309 if (ResultStatus::Success != result)
310 return result;
311
312 Service::FS::RegisterArchiveType(std::make_unique<FileSys::ArchiveFactory_RomFS>(*this), Service::FS::ArchiveIdCode::RomFS);
313 return ResultStatus::Success;
307} 314}
308 315
309ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) { 316ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) {
diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h
index fd852c3de..75609ee57 100644
--- a/src/core/loader/ncch.h
+++ b/src/core/loader/ncch.h
@@ -174,6 +174,14 @@ public:
174 static FileType IdentifyType(FileUtil::IOFile& file); 174 static FileType IdentifyType(FileUtil::IOFile& file);
175 175
176 /** 176 /**
177 * Returns the type of this file
178 * @return FileType corresponding to the loaded file
179 */
180 FileType GetFileType() override {
181 return IdentifyType(file);
182 }
183
184 /**
177 * Load the application 185 * Load the application
178 * @return ResultStatus result of function 186 * @return ResultStatus result of function
179 */ 187 */
diff --git a/src/core/loader/smdh.cpp b/src/core/loader/smdh.cpp
new file mode 100644
index 000000000..2d014054a
--- /dev/null
+++ b/src/core/loader/smdh.cpp
@@ -0,0 +1,54 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <cstring>
6#include <vector>
7
8#include "common/common_types.h"
9
10#include "core/loader/loader.h"
11#include "core/loader/smdh.h"
12
13#include "video_core/utils.h"
14
15namespace Loader {
16
17bool IsValidSMDH(const std::vector<u8>& smdh_data) {
18 if (smdh_data.size() < sizeof(Loader::SMDH))
19 return false;
20
21 u32 magic;
22 memcpy(&magic, smdh_data.data(), sizeof(u32));
23
24 return Loader::MakeMagic('S', 'M', 'D', 'H') == magic;
25}
26
27std::vector<u16> SMDH::GetIcon(bool large) const {
28 u32 size;
29 const u8* icon_data;
30
31 if (large) {
32 size = 48;
33 icon_data = large_icon.data();
34 } else {
35 size = 24;
36 icon_data = small_icon.data();
37 }
38
39 std::vector<u16> icon(size * size);
40 for (u32 x = 0; x < size; ++x) {
41 for (u32 y = 0; y < size; ++y) {
42 u32 coarse_y = y & ~7;
43 const u8* pixel = icon_data + VideoCore::GetMortonOffset(x, y, 2) + coarse_y * size * 2;
44 icon[x + size * y] = (pixel[1] << 8) + pixel[0];
45 }
46 }
47 return icon;
48}
49
50std::array<u16, 0x40> SMDH::GetShortTitle(Loader::SMDH::TitleLanguage language) const {
51 return titles[static_cast<int>(language)].short_title;
52}
53
54} // namespace
diff --git a/src/core/loader/smdh.h b/src/core/loader/smdh.h
new file mode 100644
index 000000000..2011abda2
--- /dev/null
+++ b/src/core/loader/smdh.h
@@ -0,0 +1,82 @@
1// Copyright 2016 Citra Emulator Project
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 <vector>
9
10#include "common/common_funcs.h"
11#include "common/common_types.h"
12#include "common/swap.h"
13
14namespace Loader {
15
16/**
17 * Tests if data is a valid SMDH by its length and magic number.
18 * @param smdh_data data buffer to test
19 * @return bool test result
20 */
21bool IsValidSMDH(const std::vector<u8>& smdh_data);
22
23/// SMDH data structure that contains titles, icons etc. See https://www.3dbrew.org/wiki/SMDH
24struct SMDH {
25 u32_le magic;
26 u16_le version;
27 INSERT_PADDING_BYTES(2);
28
29 struct Title {
30 std::array<u16, 0x40> short_title;
31 std::array<u16, 0x80> long_title;
32 std::array<u16, 0x40> publisher;
33 };
34 std::array<Title, 16> titles;
35
36 std::array<u8, 16> ratings;
37 u32_le region_lockout;
38 u32_le match_maker_id;
39 u64_le match_maker_bit_id;
40 u32_le flags;
41 u16_le eula_version;
42 INSERT_PADDING_BYTES(2);
43 float_le banner_animation_frame;
44 u32_le cec_id;
45 INSERT_PADDING_BYTES(8);
46
47 std::array<u8, 0x480> small_icon;
48 std::array<u8, 0x1200> large_icon;
49
50 /// indicates the language used for each title entry
51 enum class TitleLanguage {
52 Japanese = 0,
53 English = 1,
54 French = 2,
55 German = 3,
56 Italian = 4,
57 Spanish = 5,
58 SimplifiedChinese = 6,
59 Korean= 7,
60 Dutch = 8,
61 Portuguese = 9,
62 Russian = 10,
63 TraditionalChinese = 11
64 };
65
66 /**
67 * Gets game icon from SMDH
68 * @param large If true, returns large icon (48x48), otherwise returns small icon (24x24)
69 * @return vector of RGB565 data
70 */
71 std::vector<u16> GetIcon(bool large) const;
72
73 /**
74 * Gets the short game title from SMDH
75 * @param language title language
76 * @return UTF-16 array of the short title
77 */
78 std::array<u16, 0x40> GetShortTitle(Loader::SMDH::TitleLanguage language) const;
79};
80static_assert(sizeof(SMDH) == 0x36C0, "SMDH structure size is wrong");
81
82} // namespace