diff options
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/loader/3dsx.cpp | 27 | ||||
| -rw-r--r-- | src/core/loader/3dsx.h | 9 | ||||
| -rw-r--r-- | src/core/loader/loader.cpp | 50 | ||||
| -rw-r--r-- | src/core/loader/loader.h | 57 | ||||
| -rw-r--r-- | src/core/loader/ncch.cpp | 22 | ||||
| -rw-r--r-- | src/core/loader/ncch.h | 7 |
6 files changed, 149 insertions, 23 deletions
diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp index 5fb3b9e2b..48a11ef81 100644 --- a/src/core/loader/3dsx.cpp +++ b/src/core/loader/3dsx.cpp | |||
| @@ -303,4 +303,31 @@ ResultStatus AppLoader_THREEDSX::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& ro | |||
| 303 | return ResultStatus::ErrorNotUsed; | 303 | return ResultStatus::ErrorNotUsed; |
| 304 | } | 304 | } |
| 305 | 305 | ||
| 306 | ResultStatus AppLoader_THREEDSX::ReadIcon(std::vector<u8>& buffer) { | ||
| 307 | if (!file.IsOpen()) | ||
| 308 | return ResultStatus::Error; | ||
| 309 | |||
| 310 | // Reset read pointer in case this file has been read before. | ||
| 311 | file.Seek(0, SEEK_SET); | ||
| 312 | |||
| 313 | THREEDSX_Header hdr; | ||
| 314 | if (file.ReadBytes(&hdr, sizeof(THREEDSX_Header)) != sizeof(THREEDSX_Header)) | ||
| 315 | return ResultStatus::Error; | ||
| 316 | |||
| 317 | if (hdr.header_size != sizeof(THREEDSX_Header)) | ||
| 318 | return ResultStatus::Error; | ||
| 319 | |||
| 320 | // Check if the 3DSX has a SMDH... | ||
| 321 | if (hdr.smdh_offset != 0) { | ||
| 322 | file.Seek(hdr.smdh_offset, SEEK_SET); | ||
| 323 | buffer.resize(hdr.smdh_size); | ||
| 324 | |||
| 325 | if (file.ReadBytes(&buffer[0], hdr.smdh_size) != hdr.smdh_size) | ||
| 326 | return ResultStatus::Error; | ||
| 327 | |||
| 328 | return ResultStatus::Success; | ||
| 329 | } | ||
| 330 | return ResultStatus::ErrorNotUsed; | ||
| 331 | } | ||
| 332 | |||
| 306 | } // namespace Loader | 333 | } // namespace Loader |
diff --git a/src/core/loader/3dsx.h b/src/core/loader/3dsx.h index 365ddb7a5..3ee686703 100644 --- a/src/core/loader/3dsx.h +++ b/src/core/loader/3dsx.h | |||
| @@ -17,7 +17,7 @@ namespace Loader { | |||
| 17 | /// Loads an 3DSX file | 17 | /// Loads an 3DSX file |
| 18 | class AppLoader_THREEDSX final : public AppLoader { | 18 | class AppLoader_THREEDSX final : public AppLoader { |
| 19 | public: | 19 | public: |
| 20 | AppLoader_THREEDSX(FileUtil::IOFile&& file, std::string filename, const std::string& filepath) | 20 | AppLoader_THREEDSX(FileUtil::IOFile&& file, const std::string& filename, const std::string& filepath) |
| 21 | : AppLoader(std::move(file)), filename(std::move(filename)), filepath(filepath) {} | 21 | : AppLoader(std::move(file)), filename(std::move(filename)), filepath(filepath) {} |
| 22 | 22 | ||
| 23 | /** | 23 | /** |
| @@ -34,6 +34,13 @@ public: | |||
| 34 | ResultStatus Load() override; | 34 | ResultStatus Load() override; |
| 35 | 35 | ||
| 36 | /** | 36 | /** |
| 37 | * Get the icon (typically icon section) of the application | ||
| 38 | * @param buffer Reference to buffer to store data | ||
| 39 | * @return ResultStatus result of function | ||
| 40 | */ | ||
| 41 | ResultStatus ReadIcon(std::vector<u8>& buffer) override; | ||
| 42 | |||
| 43 | /** | ||
| 37 | * Get the RomFS of the application | 44 | * Get the RomFS of the application |
| 38 | * @param romfs_file Reference to buffer to store data | 45 | * @param romfs_file Reference to buffer to store data |
| 39 | * @param offset Offset in the file to the RomFS | 46 | * @param offset Offset in the file to the RomFS |
diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index 886501c41..0d4c1d351 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp | |||
| @@ -90,6 +90,28 @@ const char* GetFileTypeString(FileType type) { | |||
| 90 | return "unknown"; | 90 | return "unknown"; |
| 91 | } | 91 | } |
| 92 | 92 | ||
| 93 | std::unique_ptr<AppLoader> GetLoader(FileUtil::IOFile&& file, FileType type, | ||
| 94 | const std::string& filename, const std::string& filepath) { | ||
| 95 | switch (type) { | ||
| 96 | |||
| 97 | // 3DSX file format. | ||
| 98 | case FileType::THREEDSX: | ||
| 99 | return std::make_unique<AppLoader_THREEDSX>(std::move(file), filename, filepath); | ||
| 100 | |||
| 101 | // Standard ELF file format. | ||
| 102 | case FileType::ELF: | ||
| 103 | return std::make_unique<AppLoader_ELF>(std::move(file), filename); | ||
| 104 | |||
| 105 | // NCCH/NCSD container formats. | ||
| 106 | case FileType::CXI: | ||
| 107 | case FileType::CCI: | ||
| 108 | return std::make_unique<AppLoader_NCCH>(std::move(file), filepath); | ||
| 109 | |||
| 110 | default: | ||
| 111 | return std::unique_ptr<AppLoader>(); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 93 | ResultStatus LoadFile(const std::string& filename) { | 115 | ResultStatus LoadFile(const std::string& filename) { |
| 94 | FileUtil::IOFile file(filename, "rb"); | 116 | FileUtil::IOFile file(filename, "rb"); |
| 95 | if (!file.IsOpen()) { | 117 | if (!file.IsOpen()) { |
| @@ -111,15 +133,19 @@ ResultStatus LoadFile(const std::string& filename) { | |||
| 111 | 133 | ||
| 112 | LOG_INFO(Loader, "Loading file %s as %s...", filename.c_str(), GetFileTypeString(type)); | 134 | LOG_INFO(Loader, "Loading file %s as %s...", filename.c_str(), GetFileTypeString(type)); |
| 113 | 135 | ||
| 136 | std::unique_ptr<AppLoader> app_loader = GetLoader(std::move(file), type, filename_filename, filename); | ||
| 137 | |||
| 114 | switch (type) { | 138 | switch (type) { |
| 115 | 139 | ||
| 116 | //3DSX file format... | 140 | // 3DSX file format... |
| 141 | // or NCCH/NCSD container formats... | ||
| 117 | case FileType::THREEDSX: | 142 | case FileType::THREEDSX: |
| 143 | case FileType::CXI: | ||
| 144 | case FileType::CCI: | ||
| 118 | { | 145 | { |
| 119 | AppLoader_THREEDSX app_loader(std::move(file), filename_filename, filename); | ||
| 120 | // Load application and RomFS | 146 | // Load application and RomFS |
| 121 | if (ResultStatus::Success == app_loader.Load()) { | 147 | if (ResultStatus::Success == app_loader->Load()) { |
| 122 | Service::FS::RegisterArchiveType(std::make_unique<FileSys::ArchiveFactory_RomFS>(app_loader), Service::FS::ArchiveIdCode::RomFS); | 148 | Service::FS::RegisterArchiveType(std::make_unique<FileSys::ArchiveFactory_RomFS>(*app_loader), Service::FS::ArchiveIdCode::RomFS); |
| 123 | return ResultStatus::Success; | 149 | return ResultStatus::Success; |
| 124 | } | 150 | } |
| 125 | break; | 151 | break; |
| @@ -127,21 +153,7 @@ ResultStatus LoadFile(const std::string& filename) { | |||
| 127 | 153 | ||
| 128 | // Standard ELF file format... | 154 | // Standard ELF file format... |
| 129 | case FileType::ELF: | 155 | case FileType::ELF: |
| 130 | return AppLoader_ELF(std::move(file), filename_filename).Load(); | 156 | return app_loader->Load(); |
| 131 | |||
| 132 | // NCCH/NCSD container formats... | ||
| 133 | case FileType::CXI: | ||
| 134 | case FileType::CCI: | ||
| 135 | { | ||
| 136 | AppLoader_NCCH app_loader(std::move(file), filename); | ||
| 137 | |||
| 138 | // Load application and RomFS | ||
| 139 | ResultStatus result = app_loader.Load(); | ||
| 140 | if (ResultStatus::Success == result) { | ||
| 141 | Service::FS::RegisterArchiveType(std::make_unique<FileSys::ArchiveFactory_RomFS>(app_loader), Service::FS::ArchiveIdCode::RomFS); | ||
| 142 | } | ||
| 143 | return result; | ||
| 144 | } | ||
| 145 | 157 | ||
| 146 | // CIA file format... | 158 | // CIA file format... |
| 147 | case FileType::CIA: | 159 | case FileType::CIA: |
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index 84a4ce5fc..9d3e9ed3b 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h | |||
| @@ -10,8 +10,10 @@ | |||
| 10 | #include <string> | 10 | #include <string> |
| 11 | #include <vector> | 11 | #include <vector> |
| 12 | 12 | ||
| 13 | #include "common/common_funcs.h" | ||
| 13 | #include "common/common_types.h" | 14 | #include "common/common_types.h" |
| 14 | #include "common/file_util.h" | 15 | #include "common/file_util.h" |
| 16 | #include "common/swap.h" | ||
| 15 | 17 | ||
| 16 | namespace Kernel { | 18 | namespace Kernel { |
| 17 | struct AddressMapping; | 19 | struct AddressMapping; |
| @@ -78,6 +80,51 @@ constexpr u32 MakeMagic(char a, char b, char c, char d) { | |||
| 78 | return a | b << 8 | c << 16 | d << 24; | 80 | return a | b << 8 | c << 16 | d << 24; |
| 79 | } | 81 | } |
| 80 | 82 | ||
| 83 | /// SMDH data structure that contains titles, icons etc. See https://www.3dbrew.org/wiki/SMDH | ||
| 84 | struct 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 | }; | ||
| 126 | static_assert(sizeof(SMDH) == 0x36C0, "SMDH structure size is wrong"); | ||
| 127 | |||
| 81 | /// Interface for loading an application | 128 | /// Interface for loading an application |
| 82 | class AppLoader : NonCopyable { | 129 | class AppLoader : NonCopyable { |
| 83 | public: | 130 | public: |
| @@ -150,6 +197,16 @@ protected: | |||
| 150 | extern const std::initializer_list<Kernel::AddressMapping> default_address_mappings; | 197 | extern const std::initializer_list<Kernel::AddressMapping> default_address_mappings; |
| 151 | 198 | ||
| 152 | /** | 199 | /** |
| 200 | * Get a loader for a file with a specific type | ||
| 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 | */ | ||
| 207 | std::unique_ptr<AppLoader> GetLoader(FileUtil::IOFile&& file, FileType type, const std::string& filename, const std::string& filepath); | ||
| 208 | |||
| 209 | /** | ||
| 153 | * Identifies and loads a bootable file | 210 | * Identifies and loads a bootable file |
| 154 | * @param filename String filename of bootable file | 211 | * @param filename String filename of bootable file |
| 155 | * @return ResultStatus result of function | 212 | * @return ResultStatus result of function |
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index 066e91a9e..d362a4419 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp | |||
| @@ -173,6 +173,10 @@ ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& | |||
| 173 | if (!file.IsOpen()) | 173 | if (!file.IsOpen()) |
| 174 | return ResultStatus::Error; | 174 | return ResultStatus::Error; |
| 175 | 175 | ||
| 176 | ResultStatus result = LoadExeFS(); | ||
| 177 | if (result != ResultStatus::Success) | ||
| 178 | return result; | ||
| 179 | |||
| 176 | LOG_DEBUG(Loader, "%d sections:", kMaxSections); | 180 | LOG_DEBUG(Loader, "%d sections:", kMaxSections); |
| 177 | // Iterate through the ExeFs archive until we find a section with the specified name... | 181 | // Iterate through the ExeFs archive until we find a section with the specified name... |
| 178 | for (unsigned section_number = 0; section_number < kMaxSections; section_number++) { | 182 | for (unsigned section_number = 0; section_number < kMaxSections; section_number++) { |
| @@ -215,9 +219,9 @@ ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& | |||
| 215 | return ResultStatus::ErrorNotUsed; | 219 | return ResultStatus::ErrorNotUsed; |
| 216 | } | 220 | } |
| 217 | 221 | ||
| 218 | ResultStatus AppLoader_NCCH::Load() { | 222 | ResultStatus AppLoader_NCCH::LoadExeFS() { |
| 219 | if (is_loaded) | 223 | if (is_exefs_loaded) |
| 220 | return ResultStatus::ErrorAlreadyLoaded; | 224 | return ResultStatus::Success; |
| 221 | 225 | ||
| 222 | if (!file.IsOpen()) | 226 | if (!file.IsOpen()) |
| 223 | return ResultStatus::Error; | 227 | return ResultStatus::Error; |
| @@ -282,6 +286,18 @@ ResultStatus AppLoader_NCCH::Load() { | |||
| 282 | if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header)) | 286 | if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header)) |
| 283 | return ResultStatus::Error; | 287 | return ResultStatus::Error; |
| 284 | 288 | ||
| 289 | is_exefs_loaded = true; | ||
| 290 | return ResultStatus::Success; | ||
| 291 | } | ||
| 292 | |||
| 293 | ResultStatus AppLoader_NCCH::Load() { | ||
| 294 | if (is_loaded) | ||
| 295 | return ResultStatus::ErrorAlreadyLoaded; | ||
| 296 | |||
| 297 | ResultStatus result = LoadExeFS(); | ||
| 298 | if (result != ResultStatus::Success) | ||
| 299 | return result; | ||
| 300 | |||
| 285 | is_loaded = true; // Set state to loaded | 301 | is_loaded = true; // Set state to loaded |
| 286 | 302 | ||
| 287 | return LoadExec(); // Load the executable into memory for booting | 303 | return LoadExec(); // Load the executable into memory for booting |
diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h index ca6772a78..fd852c3de 100644 --- a/src/core/loader/ncch.h +++ b/src/core/loader/ncch.h | |||
| @@ -232,6 +232,13 @@ private: | |||
| 232 | */ | 232 | */ |
| 233 | ResultStatus LoadExec(); | 233 | ResultStatus LoadExec(); |
| 234 | 234 | ||
| 235 | /** | ||
| 236 | * Ensure ExeFS is loaded and ready for reading sections | ||
| 237 | * @return ResultStatus result of function | ||
| 238 | */ | ||
| 239 | ResultStatus LoadExeFS(); | ||
| 240 | |||
| 241 | bool is_exefs_loaded = false; | ||
| 235 | bool is_compressed = false; | 242 | bool is_compressed = false; |
| 236 | 243 | ||
| 237 | u32 entry_point = 0; | 244 | u32 entry_point = 0; |