diff options
| author | 2017-10-09 23:56:20 -0400 | |
|---|---|---|
| committer | 2017-10-09 23:56:20 -0400 | |
| commit | b1d5db1cf60344b6b081c9d03cb6ccc3264326cd (patch) | |
| tree | fde377c4ba3c0f92c032e6f5ec8627aae37270ef /src/core/file_sys | |
| parent | loader: Various improvements for NSO/NRO loaders. (diff) | |
| parent | Merge pull request #2996 from MerryMage/split-travis (diff) | |
| download | yuzu-b1d5db1cf60344b6b081c9d03cb6ccc3264326cd.tar.gz yuzu-b1d5db1cf60344b6b081c9d03cb6ccc3264326cd.tar.xz yuzu-b1d5db1cf60344b6b081c9d03cb6ccc3264326cd.zip | |
Merge remote-tracking branch 'upstream/master' into nx
# Conflicts:
# src/core/CMakeLists.txt
# src/core/arm/dynarmic/arm_dynarmic.cpp
# src/core/arm/dyncom/arm_dyncom.cpp
# src/core/hle/kernel/process.cpp
# src/core/hle/kernel/thread.cpp
# src/core/hle/kernel/thread.h
# src/core/hle/kernel/vm_manager.cpp
# src/core/loader/3dsx.cpp
# src/core/loader/elf.cpp
# src/core/loader/ncch.cpp
# src/core/memory.cpp
# src/core/memory.h
# src/core/memory_setup.h
Diffstat (limited to 'src/core/file_sys')
| -rw-r--r-- | src/core/file_sys/archive_backend.cpp | 2 | ||||
| -rw-r--r-- | src/core/file_sys/archive_ncch.cpp | 29 | ||||
| -rw-r--r-- | src/core/file_sys/archive_sdmc.cpp | 41 | ||||
| -rw-r--r-- | src/core/file_sys/archive_selfncch.cpp | 60 | ||||
| -rw-r--r-- | src/core/file_sys/archive_selfncch.h | 13 | ||||
| -rw-r--r-- | src/core/file_sys/ncch_container.cpp | 423 | ||||
| -rw-r--r-- | src/core/file_sys/ncch_container.h | 274 | ||||
| -rw-r--r-- | src/core/file_sys/savedata_archive.cpp | 41 | ||||
| -rw-r--r-- | src/core/file_sys/title_metadata.cpp | 212 | ||||
| -rw-r--r-- | src/core/file_sys/title_metadata.h | 125 |
10 files changed, 1198 insertions, 22 deletions
diff --git a/src/core/file_sys/archive_backend.cpp b/src/core/file_sys/archive_backend.cpp index 1fae0ede0..87a240d7a 100644 --- a/src/core/file_sys/archive_backend.cpp +++ b/src/core/file_sys/archive_backend.cpp | |||
| @@ -90,6 +90,8 @@ std::u16string Path::AsU16Str() const { | |||
| 90 | LOG_ERROR(Service_FS, "LowPathType cannot be converted to u16string!"); | 90 | LOG_ERROR(Service_FS, "LowPathType cannot be converted to u16string!"); |
| 91 | return {}; | 91 | return {}; |
| 92 | } | 92 | } |
| 93 | |||
| 94 | UNREACHABLE(); | ||
| 93 | } | 95 | } |
| 94 | 96 | ||
| 95 | std::vector<u8> Path::AsBinary() const { | 97 | std::vector<u8> Path::AsBinary() const { |
diff --git a/src/core/file_sys/archive_ncch.cpp b/src/core/file_sys/archive_ncch.cpp index 6d9007731..e8c5be983 100644 --- a/src/core/file_sys/archive_ncch.cpp +++ b/src/core/file_sys/archive_ncch.cpp | |||
| @@ -13,7 +13,10 @@ | |||
| 13 | #include "core/file_sys/archive_ncch.h" | 13 | #include "core/file_sys/archive_ncch.h" |
| 14 | #include "core/file_sys/errors.h" | 14 | #include "core/file_sys/errors.h" |
| 15 | #include "core/file_sys/ivfc_archive.h" | 15 | #include "core/file_sys/ivfc_archive.h" |
| 16 | #include "core/file_sys/ncch_container.h" | ||
| 17 | #include "core/file_sys/title_metadata.h" | ||
| 16 | #include "core/hle/service/fs/archive.h" | 18 | #include "core/hle/service/fs/archive.h" |
| 19 | #include "core/loader/loader.h" | ||
| 17 | 20 | ||
| 18 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 21 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 19 | // FileSys namespace | 22 | // FileSys namespace |
| @@ -25,8 +28,18 @@ static std::string GetNCCHContainerPath(const std::string& nand_directory) { | |||
| 25 | } | 28 | } |
| 26 | 29 | ||
| 27 | static std::string GetNCCHPath(const std::string& mount_point, u32 high, u32 low) { | 30 | static std::string GetNCCHPath(const std::string& mount_point, u32 high, u32 low) { |
| 28 | return Common::StringFromFormat("%s%08x/%08x/content/00000000.app.romfs", mount_point.c_str(), | 31 | u32 content_id = 0; |
| 29 | high, low); | 32 | |
| 33 | // TODO(shinyquagsire23): Title database should be doing this path lookup | ||
| 34 | std::string content_path = | ||
| 35 | Common::StringFromFormat("%s%08x/%08x/content/", mount_point.c_str(), high, low); | ||
| 36 | std::string tmd_path = content_path + "00000000.tmd"; | ||
| 37 | TitleMetadata tmd(tmd_path); | ||
| 38 | if (tmd.Load() == Loader::ResultStatus::Success) { | ||
| 39 | content_id = tmd.GetBootContentID(); | ||
| 40 | } | ||
| 41 | |||
| 42 | return Common::StringFromFormat("%s%08x.app", content_path.c_str(), content_id); | ||
| 30 | } | 43 | } |
| 31 | 44 | ||
| 32 | ArchiveFactory_NCCH::ArchiveFactory_NCCH(const std::string& nand_directory) | 45 | ArchiveFactory_NCCH::ArchiveFactory_NCCH(const std::string& nand_directory) |
| @@ -38,9 +51,14 @@ ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_NCCH::Open(const Path& | |||
| 38 | u32 high = data[1]; | 51 | u32 high = data[1]; |
| 39 | u32 low = data[0]; | 52 | u32 low = data[0]; |
| 40 | std::string file_path = GetNCCHPath(mount_point, high, low); | 53 | std::string file_path = GetNCCHPath(mount_point, high, low); |
| 41 | auto file = std::make_shared<FileUtil::IOFile>(file_path, "rb"); | ||
| 42 | 54 | ||
| 43 | if (!file->IsOpen()) { | 55 | std::shared_ptr<FileUtil::IOFile> romfs_file; |
| 56 | u64 romfs_offset = 0; | ||
| 57 | u64 romfs_size = 0; | ||
| 58 | auto ncch_container = NCCHContainer(file_path); | ||
| 59 | |||
| 60 | if (ncch_container.ReadRomFS(romfs_file, romfs_offset, romfs_size) != | ||
| 61 | Loader::ResultStatus::Success) { | ||
| 44 | // High Title ID of the archive: The category (https://3dbrew.org/wiki/Title_list). | 62 | // High Title ID of the archive: The category (https://3dbrew.org/wiki/Title_list). |
| 45 | constexpr u32 shared_data_archive = 0x0004009B; | 63 | constexpr u32 shared_data_archive = 0x0004009B; |
| 46 | constexpr u32 system_data_archive = 0x000400DB; | 64 | constexpr u32 system_data_archive = 0x000400DB; |
| @@ -74,9 +92,8 @@ ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_NCCH::Open(const Path& | |||
| 74 | } | 92 | } |
| 75 | return ERROR_NOT_FOUND; | 93 | return ERROR_NOT_FOUND; |
| 76 | } | 94 | } |
| 77 | auto size = file->GetSize(); | ||
| 78 | 95 | ||
| 79 | auto archive = std::make_unique<IVFCArchive>(file, 0, size); | 96 | auto archive = std::make_unique<IVFCArchive>(romfs_file, romfs_offset, romfs_size); |
| 80 | return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive)); | 97 | return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive)); |
| 81 | } | 98 | } |
| 82 | 99 | ||
diff --git a/src/core/file_sys/archive_sdmc.cpp b/src/core/file_sys/archive_sdmc.cpp index 679909d06..fe3dce5d4 100644 --- a/src/core/file_sys/archive_sdmc.cpp +++ b/src/core/file_sys/archive_sdmc.cpp | |||
| @@ -121,7 +121,25 @@ ResultCode SDMCArchive::DeleteFile(const Path& path) const { | |||
| 121 | } | 121 | } |
| 122 | 122 | ||
| 123 | ResultCode SDMCArchive::RenameFile(const Path& src_path, const Path& dest_path) const { | 123 | ResultCode SDMCArchive::RenameFile(const Path& src_path, const Path& dest_path) const { |
| 124 | if (FileUtil::Rename(mount_point + src_path.AsString(), mount_point + dest_path.AsString())) { | 124 | const PathParser path_parser_src(src_path); |
| 125 | |||
| 126 | // TODO: Verify these return codes with HW | ||
| 127 | if (!path_parser_src.IsValid()) { | ||
| 128 | LOG_ERROR(Service_FS, "Invalid src path %s", src_path.DebugStr().c_str()); | ||
| 129 | return ERROR_INVALID_PATH; | ||
| 130 | } | ||
| 131 | |||
| 132 | const PathParser path_parser_dest(dest_path); | ||
| 133 | |||
| 134 | if (!path_parser_dest.IsValid()) { | ||
| 135 | LOG_ERROR(Service_FS, "Invalid dest path %s", dest_path.DebugStr().c_str()); | ||
| 136 | return ERROR_INVALID_PATH; | ||
| 137 | } | ||
| 138 | |||
| 139 | const auto src_path_full = path_parser_src.BuildHostPath(mount_point); | ||
| 140 | const auto dest_path_full = path_parser_dest.BuildHostPath(mount_point); | ||
| 141 | |||
| 142 | if (FileUtil::Rename(src_path_full, dest_path_full)) { | ||
| 125 | return RESULT_SUCCESS; | 143 | return RESULT_SUCCESS; |
| 126 | } | 144 | } |
| 127 | 145 | ||
| @@ -260,8 +278,27 @@ ResultCode SDMCArchive::CreateDirectory(const Path& path) const { | |||
| 260 | } | 278 | } |
| 261 | 279 | ||
| 262 | ResultCode SDMCArchive::RenameDirectory(const Path& src_path, const Path& dest_path) const { | 280 | ResultCode SDMCArchive::RenameDirectory(const Path& src_path, const Path& dest_path) const { |
| 263 | if (FileUtil::Rename(mount_point + src_path.AsString(), mount_point + dest_path.AsString())) | 281 | const PathParser path_parser_src(src_path); |
| 282 | |||
| 283 | // TODO: Verify these return codes with HW | ||
| 284 | if (!path_parser_src.IsValid()) { | ||
| 285 | LOG_ERROR(Service_FS, "Invalid src path %s", src_path.DebugStr().c_str()); | ||
| 286 | return ERROR_INVALID_PATH; | ||
| 287 | } | ||
| 288 | |||
| 289 | const PathParser path_parser_dest(dest_path); | ||
| 290 | |||
| 291 | if (!path_parser_dest.IsValid()) { | ||
| 292 | LOG_ERROR(Service_FS, "Invalid dest path %s", dest_path.DebugStr().c_str()); | ||
| 293 | return ERROR_INVALID_PATH; | ||
| 294 | } | ||
| 295 | |||
| 296 | const auto src_path_full = path_parser_src.BuildHostPath(mount_point); | ||
| 297 | const auto dest_path_full = path_parser_dest.BuildHostPath(mount_point); | ||
| 298 | |||
| 299 | if (FileUtil::Rename(src_path_full, dest_path_full)) { | ||
| 264 | return RESULT_SUCCESS; | 300 | return RESULT_SUCCESS; |
| 301 | } | ||
| 265 | 302 | ||
| 266 | // TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't | 303 | // TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't |
| 267 | // exist or similar. Verify. | 304 | // exist or similar. Verify. |
diff --git a/src/core/file_sys/archive_selfncch.cpp b/src/core/file_sys/archive_selfncch.cpp index 298a37a44..3222000cf 100644 --- a/src/core/file_sys/archive_selfncch.cpp +++ b/src/core/file_sys/archive_selfncch.cpp | |||
| @@ -3,12 +3,14 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <array> | 5 | #include <array> |
| 6 | #include <cinttypes> | ||
| 6 | #include "common/common_types.h" | 7 | #include "common/common_types.h" |
| 7 | #include "common/logging/log.h" | 8 | #include "common/logging/log.h" |
| 8 | #include "common/swap.h" | 9 | #include "common/swap.h" |
| 9 | #include "core/file_sys/archive_selfncch.h" | 10 | #include "core/file_sys/archive_selfncch.h" |
| 10 | #include "core/file_sys/errors.h" | 11 | #include "core/file_sys/errors.h" |
| 11 | #include "core/file_sys/ivfc_archive.h" | 12 | #include "core/file_sys/ivfc_archive.h" |
| 13 | #include "core/hle/kernel/process.h" | ||
| 12 | 14 | ||
| 13 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 15 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 14 | // FileSys namespace | 16 | // FileSys namespace |
| @@ -102,8 +104,7 @@ public: | |||
| 102 | 104 | ||
| 103 | switch (static_cast<SelfNCCHFilePathType>(file_path.type)) { | 105 | switch (static_cast<SelfNCCHFilePathType>(file_path.type)) { |
| 104 | case SelfNCCHFilePathType::UpdateRomFS: | 106 | case SelfNCCHFilePathType::UpdateRomFS: |
| 105 | LOG_WARNING(Service_FS, "(STUBBED) open update RomFS"); | 107 | return OpenUpdateRomFS(); |
| 106 | return OpenRomFS(); | ||
| 107 | 108 | ||
| 108 | case SelfNCCHFilePathType::RomFS: | 109 | case SelfNCCHFilePathType::RomFS: |
| 109 | return OpenRomFS(); | 110 | return OpenRomFS(); |
| @@ -179,6 +180,17 @@ private: | |||
| 179 | } | 180 | } |
| 180 | } | 181 | } |
| 181 | 182 | ||
| 183 | ResultVal<std::unique_ptr<FileBackend>> OpenUpdateRomFS() const { | ||
| 184 | if (ncch_data.update_romfs_file) { | ||
| 185 | return MakeResult<std::unique_ptr<FileBackend>>(std::make_unique<IVFCFile>( | ||
| 186 | ncch_data.update_romfs_file, ncch_data.update_romfs_offset, | ||
| 187 | ncch_data.update_romfs_size)); | ||
| 188 | } else { | ||
| 189 | LOG_INFO(Service_FS, "Unable to read update RomFS"); | ||
| 190 | return ERROR_ROMFS_NOT_FOUND; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 182 | ResultVal<std::unique_ptr<FileBackend>> OpenExeFS(const std::string& filename) const { | 194 | ResultVal<std::unique_ptr<FileBackend>> OpenExeFS(const std::string& filename) const { |
| 183 | if (filename == "icon") { | 195 | if (filename == "icon") { |
| 184 | if (ncch_data.icon) { | 196 | if (ncch_data.icon) { |
| @@ -217,31 +229,59 @@ private: | |||
| 217 | NCCHData ncch_data; | 229 | NCCHData ncch_data; |
| 218 | }; | 230 | }; |
| 219 | 231 | ||
| 220 | ArchiveFactory_SelfNCCH::ArchiveFactory_SelfNCCH(Loader::AppLoader& app_loader) { | 232 | void ArchiveFactory_SelfNCCH::Register(Loader::AppLoader& app_loader) { |
| 233 | u64 program_id = 0; | ||
| 234 | if (app_loader.ReadProgramId(program_id) != Loader::ResultStatus::Success) { | ||
| 235 | LOG_WARNING( | ||
| 236 | Service_FS, | ||
| 237 | "Could not read program id when registering with SelfNCCH, this might be a 3dsx file"); | ||
| 238 | } | ||
| 239 | |||
| 240 | LOG_DEBUG(Service_FS, "Registering program %016" PRIX64 " with the SelfNCCH archive factory", | ||
| 241 | program_id); | ||
| 242 | |||
| 243 | if (ncch_data.find(program_id) != ncch_data.end()) { | ||
| 244 | LOG_WARNING(Service_FS, "Registering program %016" PRIX64 | ||
| 245 | " with SelfNCCH will override existing mapping", | ||
| 246 | program_id); | ||
| 247 | } | ||
| 248 | |||
| 249 | NCCHData& data = ncch_data[program_id]; | ||
| 250 | |||
| 221 | std::shared_ptr<FileUtil::IOFile> romfs_file_; | 251 | std::shared_ptr<FileUtil::IOFile> romfs_file_; |
| 222 | if (Loader::ResultStatus::Success == | 252 | if (Loader::ResultStatus::Success == |
| 223 | app_loader.ReadRomFS(romfs_file_, ncch_data.romfs_offset, ncch_data.romfs_size)) { | 253 | app_loader.ReadRomFS(romfs_file_, data.romfs_offset, data.romfs_size)) { |
| 254 | |||
| 255 | data.romfs_file = std::move(romfs_file_); | ||
| 256 | } | ||
| 257 | |||
| 258 | std::shared_ptr<FileUtil::IOFile> update_romfs_file; | ||
| 259 | if (Loader::ResultStatus::Success == | ||
| 260 | app_loader.ReadUpdateRomFS(update_romfs_file, data.update_romfs_offset, | ||
| 261 | data.update_romfs_size)) { | ||
| 224 | 262 | ||
| 225 | ncch_data.romfs_file = std::move(romfs_file_); | 263 | data.update_romfs_file = std::move(update_romfs_file); |
| 226 | } | 264 | } |
| 227 | 265 | ||
| 228 | std::vector<u8> buffer; | 266 | std::vector<u8> buffer; |
| 229 | 267 | ||
| 230 | if (Loader::ResultStatus::Success == app_loader.ReadIcon(buffer)) | 268 | if (Loader::ResultStatus::Success == app_loader.ReadIcon(buffer)) |
| 231 | ncch_data.icon = std::make_shared<std::vector<u8>>(std::move(buffer)); | 269 | data.icon = std::make_shared<std::vector<u8>>(std::move(buffer)); |
| 232 | 270 | ||
| 233 | buffer.clear(); | 271 | buffer.clear(); |
| 234 | if (Loader::ResultStatus::Success == app_loader.ReadLogo(buffer)) | 272 | if (Loader::ResultStatus::Success == app_loader.ReadLogo(buffer)) |
| 235 | ncch_data.logo = std::make_shared<std::vector<u8>>(std::move(buffer)); | 273 | data.logo = std::make_shared<std::vector<u8>>(std::move(buffer)); |
| 236 | 274 | ||
| 237 | buffer.clear(); | 275 | buffer.clear(); |
| 238 | if (Loader::ResultStatus::Success == app_loader.ReadBanner(buffer)) | 276 | if (Loader::ResultStatus::Success == app_loader.ReadBanner(buffer)) |
| 239 | ncch_data.banner = std::make_shared<std::vector<u8>>(std::move(buffer)); | 277 | data.banner = std::make_shared<std::vector<u8>>(std::move(buffer)); |
| 240 | } | 278 | } |
| 241 | 279 | ||
| 242 | ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SelfNCCH::Open(const Path& path) { | 280 | ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SelfNCCH::Open(const Path& path) { |
| 243 | auto archive = std::make_unique<SelfNCCHArchive>(ncch_data); | 281 | //auto archive = std::make_unique<SelfNCCHArchive>( |
| 244 | return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive)); | 282 | // ncch_data[Kernel::g_current_process->codeset->program_id]); |
| 283 | //return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive)); | ||
| 284 | return {}; | ||
| 245 | } | 285 | } |
| 246 | 286 | ||
| 247 | ResultCode ArchiveFactory_SelfNCCH::Format(const Path&, const FileSys::ArchiveFormatInfo&) { | 287 | ResultCode ArchiveFactory_SelfNCCH::Format(const Path&, const FileSys::ArchiveFormatInfo&) { |
diff --git a/src/core/file_sys/archive_selfncch.h b/src/core/file_sys/archive_selfncch.h index f1b971296..0d6d6766e 100644 --- a/src/core/file_sys/archive_selfncch.h +++ b/src/core/file_sys/archive_selfncch.h | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | 6 | ||
| 7 | #include <memory> | 7 | #include <memory> |
| 8 | #include <string> | 8 | #include <string> |
| 9 | #include <unordered_map> | ||
| 9 | #include <vector> | 10 | #include <vector> |
| 10 | #include "common/common_types.h" | 11 | #include "common/common_types.h" |
| 11 | #include "core/file_sys/archive_backend.h" | 12 | #include "core/file_sys/archive_backend.h" |
| @@ -24,12 +25,19 @@ struct NCCHData { | |||
| 24 | std::shared_ptr<FileUtil::IOFile> romfs_file; | 25 | std::shared_ptr<FileUtil::IOFile> romfs_file; |
| 25 | u64 romfs_offset = 0; | 26 | u64 romfs_offset = 0; |
| 26 | u64 romfs_size = 0; | 27 | u64 romfs_size = 0; |
| 28 | |||
| 29 | std::shared_ptr<FileUtil::IOFile> update_romfs_file; | ||
| 30 | u64 update_romfs_offset = 0; | ||
| 31 | u64 update_romfs_size = 0; | ||
| 27 | }; | 32 | }; |
| 28 | 33 | ||
| 29 | /// File system interface to the SelfNCCH archive | 34 | /// File system interface to the SelfNCCH archive |
| 30 | class ArchiveFactory_SelfNCCH final : public ArchiveFactory { | 35 | class ArchiveFactory_SelfNCCH final : public ArchiveFactory { |
| 31 | public: | 36 | public: |
| 32 | explicit ArchiveFactory_SelfNCCH(Loader::AppLoader& app_loader); | 37 | ArchiveFactory_SelfNCCH() = default; |
| 38 | |||
| 39 | /// Registers a loaded application so that we can open its SelfNCCH archive when requested. | ||
| 40 | void Register(Loader::AppLoader& app_loader); | ||
| 33 | 41 | ||
| 34 | std::string GetName() const override { | 42 | std::string GetName() const override { |
| 35 | return "SelfNCCH"; | 43 | return "SelfNCCH"; |
| @@ -39,7 +47,8 @@ public: | |||
| 39 | ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override; | 47 | ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override; |
| 40 | 48 | ||
| 41 | private: | 49 | private: |
| 42 | NCCHData ncch_data; | 50 | /// Mapping of ProgramId -> NCCHData |
| 51 | std::unordered_map<u64, NCCHData> ncch_data; | ||
| 43 | }; | 52 | }; |
| 44 | 53 | ||
| 45 | } // namespace FileSys | 54 | } // namespace FileSys |
diff --git a/src/core/file_sys/ncch_container.cpp b/src/core/file_sys/ncch_container.cpp new file mode 100644 index 000000000..b9fb940c7 --- /dev/null +++ b/src/core/file_sys/ncch_container.cpp | |||
| @@ -0,0 +1,423 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <cinttypes> | ||
| 6 | #include <cstring> | ||
| 7 | #include <memory> | ||
| 8 | #include "common/common_types.h" | ||
| 9 | #include "common/logging/log.h" | ||
| 10 | #include "core/core.h" | ||
| 11 | #include "core/file_sys/ncch_container.h" | ||
| 12 | #include "core/loader/loader.h" | ||
| 13 | |||
| 14 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 15 | // FileSys namespace | ||
| 16 | |||
| 17 | namespace FileSys { | ||
| 18 | |||
| 19 | static const int kMaxSections = 8; ///< Maximum number of sections (files) in an ExeFs | ||
| 20 | static const int kBlockSize = 0x200; ///< Size of ExeFS blocks (in bytes) | ||
| 21 | |||
| 22 | /** | ||
| 23 | * Get the decompressed size of an LZSS compressed ExeFS file | ||
| 24 | * @param buffer Buffer of compressed file | ||
| 25 | * @param size Size of compressed buffer | ||
| 26 | * @return Size of decompressed buffer | ||
| 27 | */ | ||
| 28 | static u32 LZSS_GetDecompressedSize(const u8* buffer, u32 size) { | ||
| 29 | u32 offset_size = *(u32*)(buffer + size - 4); | ||
| 30 | return offset_size + size; | ||
| 31 | } | ||
| 32 | |||
| 33 | /** | ||
| 34 | * Decompress ExeFS file (compressed with LZSS) | ||
| 35 | * @param compressed Compressed buffer | ||
| 36 | * @param compressed_size Size of compressed buffer | ||
| 37 | * @param decompressed Decompressed buffer | ||
| 38 | * @param decompressed_size Size of decompressed buffer | ||
| 39 | * @return True on success, otherwise false | ||
| 40 | */ | ||
| 41 | static bool LZSS_Decompress(const u8* compressed, u32 compressed_size, u8* decompressed, | ||
| 42 | u32 decompressed_size) { | ||
| 43 | const u8* footer = compressed + compressed_size - 8; | ||
| 44 | u32 buffer_top_and_bottom = *reinterpret_cast<const u32*>(footer); | ||
| 45 | u32 out = decompressed_size; | ||
| 46 | u32 index = compressed_size - ((buffer_top_and_bottom >> 24) & 0xFF); | ||
| 47 | u32 stop_index = compressed_size - (buffer_top_and_bottom & 0xFFFFFF); | ||
| 48 | |||
| 49 | memset(decompressed, 0, decompressed_size); | ||
| 50 | memcpy(decompressed, compressed, compressed_size); | ||
| 51 | |||
| 52 | while (index > stop_index) { | ||
| 53 | u8 control = compressed[--index]; | ||
| 54 | |||
| 55 | for (unsigned i = 0; i < 8; i++) { | ||
| 56 | if (index <= stop_index) | ||
| 57 | break; | ||
| 58 | if (index <= 0) | ||
| 59 | break; | ||
| 60 | if (out <= 0) | ||
| 61 | break; | ||
| 62 | |||
| 63 | if (control & 0x80) { | ||
| 64 | // Check if compression is out of bounds | ||
| 65 | if (index < 2) | ||
| 66 | return false; | ||
| 67 | index -= 2; | ||
| 68 | |||
| 69 | u32 segment_offset = compressed[index] | (compressed[index + 1] << 8); | ||
| 70 | u32 segment_size = ((segment_offset >> 12) & 15) + 3; | ||
| 71 | segment_offset &= 0x0FFF; | ||
| 72 | segment_offset += 2; | ||
| 73 | |||
| 74 | // Check if compression is out of bounds | ||
| 75 | if (out < segment_size) | ||
| 76 | return false; | ||
| 77 | |||
| 78 | for (unsigned j = 0; j < segment_size; j++) { | ||
| 79 | // Check if compression is out of bounds | ||
| 80 | if (out + segment_offset >= decompressed_size) | ||
| 81 | return false; | ||
| 82 | |||
| 83 | u8 data = decompressed[out + segment_offset]; | ||
| 84 | decompressed[--out] = data; | ||
| 85 | } | ||
| 86 | } else { | ||
| 87 | // Check if compression is out of bounds | ||
| 88 | if (out < 1) | ||
| 89 | return false; | ||
| 90 | decompressed[--out] = compressed[--index]; | ||
| 91 | } | ||
| 92 | control <<= 1; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | return true; | ||
| 96 | } | ||
| 97 | |||
| 98 | NCCHContainer::NCCHContainer(const std::string& filepath) : filepath(filepath) { | ||
| 99 | file = FileUtil::IOFile(filepath, "rb"); | ||
| 100 | } | ||
| 101 | |||
| 102 | Loader::ResultStatus NCCHContainer::OpenFile(const std::string& filepath) { | ||
| 103 | this->filepath = filepath; | ||
| 104 | file = FileUtil::IOFile(filepath, "rb"); | ||
| 105 | |||
| 106 | if (!file.IsOpen()) { | ||
| 107 | LOG_WARNING(Service_FS, "Failed to open %s", filepath.c_str()); | ||
| 108 | return Loader::ResultStatus::Error; | ||
| 109 | } | ||
| 110 | |||
| 111 | LOG_DEBUG(Service_FS, "Opened %s", filepath.c_str()); | ||
| 112 | return Loader::ResultStatus::Success; | ||
| 113 | } | ||
| 114 | |||
| 115 | Loader::ResultStatus NCCHContainer::Load() { | ||
| 116 | if (is_loaded) | ||
| 117 | return Loader::ResultStatus::Success; | ||
| 118 | |||
| 119 | if (file.IsOpen()) { | ||
| 120 | // Reset read pointer in case this file has been read before. | ||
| 121 | file.Seek(0, SEEK_SET); | ||
| 122 | |||
| 123 | if (file.ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header)) | ||
| 124 | return Loader::ResultStatus::Error; | ||
| 125 | |||
| 126 | // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)... | ||
| 127 | if (Loader::MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) { | ||
| 128 | LOG_DEBUG(Service_FS, "Only loading the first (bootable) NCCH within the NCSD file!"); | ||
| 129 | ncch_offset = 0x4000; | ||
| 130 | file.Seek(ncch_offset, SEEK_SET); | ||
| 131 | file.ReadBytes(&ncch_header, sizeof(NCCH_Header)); | ||
| 132 | } | ||
| 133 | |||
| 134 | // Verify we are loading the correct file type... | ||
| 135 | if (Loader::MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic) | ||
| 136 | return Loader::ResultStatus::ErrorInvalidFormat; | ||
| 137 | |||
| 138 | has_header = true; | ||
| 139 | |||
| 140 | // System archives and DLC don't have an extended header but have RomFS | ||
| 141 | if (ncch_header.extended_header_size) { | ||
| 142 | if (file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != | ||
| 143 | sizeof(ExHeader_Header)) | ||
| 144 | return Loader::ResultStatus::Error; | ||
| 145 | |||
| 146 | is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1; | ||
| 147 | u32 entry_point = exheader_header.codeset_info.text.address; | ||
| 148 | u32 code_size = exheader_header.codeset_info.text.code_size; | ||
| 149 | u32 stack_size = exheader_header.codeset_info.stack_size; | ||
| 150 | u32 bss_size = exheader_header.codeset_info.bss_size; | ||
| 151 | u32 core_version = exheader_header.arm11_system_local_caps.core_version; | ||
| 152 | u8 priority = exheader_header.arm11_system_local_caps.priority; | ||
| 153 | u8 resource_limit_category = | ||
| 154 | exheader_header.arm11_system_local_caps.resource_limit_category; | ||
| 155 | |||
| 156 | LOG_DEBUG(Service_FS, "Name: %s", | ||
| 157 | exheader_header.codeset_info.name); | ||
| 158 | LOG_DEBUG(Service_FS, "Program ID: %016" PRIX64, | ||
| 159 | ncch_header.program_id); | ||
| 160 | LOG_DEBUG(Service_FS, "Code compressed: %s", is_compressed ? "yes" : "no"); | ||
| 161 | LOG_DEBUG(Service_FS, "Entry point: 0x%08X", entry_point); | ||
| 162 | LOG_DEBUG(Service_FS, "Code size: 0x%08X", code_size); | ||
| 163 | LOG_DEBUG(Service_FS, "Stack size: 0x%08X", stack_size); | ||
| 164 | LOG_DEBUG(Service_FS, "Bss size: 0x%08X", bss_size); | ||
| 165 | LOG_DEBUG(Service_FS, "Core version: %d", core_version); | ||
| 166 | LOG_DEBUG(Service_FS, "Thread priority: 0x%X", priority); | ||
| 167 | LOG_DEBUG(Service_FS, "Resource limit category: %d", resource_limit_category); | ||
| 168 | LOG_DEBUG(Service_FS, "System Mode: %d", | ||
| 169 | static_cast<int>(exheader_header.arm11_system_local_caps.system_mode)); | ||
| 170 | |||
| 171 | if (exheader_header.system_info.jump_id != ncch_header.program_id) { | ||
| 172 | LOG_ERROR(Service_FS, | ||
| 173 | "ExHeader Program ID mismatch: the ROM is probably encrypted."); | ||
| 174 | return Loader::ResultStatus::ErrorEncrypted; | ||
| 175 | } | ||
| 176 | |||
| 177 | has_exheader = true; | ||
| 178 | } | ||
| 179 | |||
| 180 | // DLC can have an ExeFS and a RomFS but no extended header | ||
| 181 | if (ncch_header.exefs_size) { | ||
| 182 | exefs_offset = ncch_header.exefs_offset * kBlockSize; | ||
| 183 | u32 exefs_size = ncch_header.exefs_size * kBlockSize; | ||
| 184 | |||
| 185 | LOG_DEBUG(Service_FS, "ExeFS offset: 0x%08X", exefs_offset); | ||
| 186 | LOG_DEBUG(Service_FS, "ExeFS size: 0x%08X", exefs_size); | ||
| 187 | |||
| 188 | file.Seek(exefs_offset + ncch_offset, SEEK_SET); | ||
| 189 | if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header)) | ||
| 190 | return Loader::ResultStatus::Error; | ||
| 191 | |||
| 192 | exefs_file = FileUtil::IOFile(filepath, "rb"); | ||
| 193 | has_exefs = true; | ||
| 194 | } | ||
| 195 | |||
| 196 | if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) | ||
| 197 | has_romfs = true; | ||
| 198 | } | ||
| 199 | |||
| 200 | LoadOverrides(); | ||
| 201 | |||
| 202 | // We need at least one of these or overrides, practically | ||
| 203 | if (!(has_exefs || has_romfs || is_tainted)) | ||
| 204 | return Loader::ResultStatus::Error; | ||
| 205 | |||
| 206 | is_loaded = true; | ||
| 207 | return Loader::ResultStatus::Success; | ||
| 208 | } | ||
| 209 | |||
| 210 | Loader::ResultStatus NCCHContainer::LoadOverrides() { | ||
| 211 | // Check for split-off files, mark the archive as tainted if we will use them | ||
| 212 | std::string romfs_override = filepath + ".romfs"; | ||
| 213 | if (FileUtil::Exists(romfs_override)) { | ||
| 214 | is_tainted = true; | ||
| 215 | } | ||
| 216 | |||
| 217 | // If we have a split-off exefs file/folder, it takes priority | ||
| 218 | std::string exefs_override = filepath + ".exefs"; | ||
| 219 | std::string exefsdir_override = filepath + ".exefsdir/"; | ||
| 220 | if (FileUtil::Exists(exefs_override)) { | ||
| 221 | exefs_file = FileUtil::IOFile(exefs_override, "rb"); | ||
| 222 | |||
| 223 | if (exefs_file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) == sizeof(ExeFs_Header)) { | ||
| 224 | LOG_DEBUG(Service_FS, "Loading ExeFS section from %s", exefs_override.c_str()); | ||
| 225 | exefs_offset = 0; | ||
| 226 | is_tainted = true; | ||
| 227 | has_exefs = true; | ||
| 228 | } else { | ||
| 229 | exefs_file = FileUtil::IOFile(filepath, "rb"); | ||
| 230 | } | ||
| 231 | } else if (FileUtil::Exists(exefsdir_override) && FileUtil::IsDirectory(exefsdir_override)) { | ||
| 232 | is_tainted = true; | ||
| 233 | } | ||
| 234 | |||
| 235 | if (is_tainted) | ||
| 236 | LOG_WARNING(Service_FS, | ||
| 237 | "Loaded NCCH %s is tainted, application behavior may not be as expected!", | ||
| 238 | filepath.c_str()); | ||
| 239 | |||
| 240 | return Loader::ResultStatus::Success; | ||
| 241 | } | ||
| 242 | |||
| 243 | Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) { | ||
| 244 | Loader::ResultStatus result = Load(); | ||
| 245 | if (result != Loader::ResultStatus::Success) | ||
| 246 | return result; | ||
| 247 | |||
| 248 | // Check if we have files that can drop-in and replace | ||
| 249 | result = LoadOverrideExeFSSection(name, buffer); | ||
| 250 | if (result == Loader::ResultStatus::Success || !has_exefs) | ||
| 251 | return result; | ||
| 252 | |||
| 253 | // If we don't have any separate files, we'll need a full ExeFS | ||
| 254 | if (!exefs_file.IsOpen()) | ||
| 255 | return Loader::ResultStatus::Error; | ||
| 256 | |||
| 257 | LOG_DEBUG(Service_FS, "%d sections:", kMaxSections); | ||
| 258 | // Iterate through the ExeFs archive until we find a section with the specified name... | ||
| 259 | for (unsigned section_number = 0; section_number < kMaxSections; section_number++) { | ||
| 260 | const auto& section = exefs_header.section[section_number]; | ||
| 261 | |||
| 262 | // Load the specified section... | ||
| 263 | if (strcmp(section.name, name) == 0) { | ||
| 264 | LOG_DEBUG(Service_FS, "%d - offset: 0x%08X, size: 0x%08X, name: %s", section_number, | ||
| 265 | section.offset, section.size, section.name); | ||
| 266 | |||
| 267 | s64 section_offset = | ||
| 268 | (section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset); | ||
| 269 | exefs_file.Seek(section_offset, SEEK_SET); | ||
| 270 | |||
| 271 | if (strcmp(section.name, ".code") == 0 && is_compressed) { | ||
| 272 | // Section is compressed, read compressed .code section... | ||
| 273 | std::unique_ptr<u8[]> temp_buffer; | ||
| 274 | try { | ||
| 275 | temp_buffer.reset(new u8[section.size]); | ||
| 276 | } catch (std::bad_alloc&) { | ||
| 277 | return Loader::ResultStatus::ErrorMemoryAllocationFailed; | ||
| 278 | } | ||
| 279 | |||
| 280 | if (exefs_file.ReadBytes(&temp_buffer[0], section.size) != section.size) | ||
| 281 | return Loader::ResultStatus::Error; | ||
| 282 | |||
| 283 | // Decompress .code section... | ||
| 284 | u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], section.size); | ||
| 285 | buffer.resize(decompressed_size); | ||
| 286 | if (!LZSS_Decompress(&temp_buffer[0], section.size, &buffer[0], decompressed_size)) | ||
| 287 | return Loader::ResultStatus::ErrorInvalidFormat; | ||
| 288 | } else { | ||
| 289 | // Section is uncompressed... | ||
| 290 | buffer.resize(section.size); | ||
| 291 | if (exefs_file.ReadBytes(&buffer[0], section.size) != section.size) | ||
| 292 | return Loader::ResultStatus::Error; | ||
| 293 | } | ||
| 294 | return Loader::ResultStatus::Success; | ||
| 295 | } | ||
| 296 | } | ||
| 297 | return Loader::ResultStatus::ErrorNotUsed; | ||
| 298 | } | ||
| 299 | |||
| 300 | Loader::ResultStatus NCCHContainer::LoadOverrideExeFSSection(const char* name, | ||
| 301 | std::vector<u8>& buffer) { | ||
| 302 | std::string override_name; | ||
| 303 | |||
| 304 | // Map our section name to the extracted equivalent | ||
| 305 | if (!strcmp(name, ".code")) | ||
| 306 | override_name = "code.bin"; | ||
| 307 | else if (!strcmp(name, "icon")) | ||
| 308 | override_name = "code.bin"; | ||
| 309 | else if (!strcmp(name, "banner")) | ||
| 310 | override_name = "banner.bnr"; | ||
| 311 | else if (!strcmp(name, "logo")) | ||
| 312 | override_name = "logo.bcma.lz"; | ||
| 313 | else | ||
| 314 | return Loader::ResultStatus::Error; | ||
| 315 | |||
| 316 | std::string section_override = filepath + ".exefsdir/" + override_name; | ||
| 317 | FileUtil::IOFile section_file(section_override, "rb"); | ||
| 318 | |||
| 319 | if (section_file.IsOpen()) { | ||
| 320 | auto section_size = section_file.GetSize(); | ||
| 321 | buffer.resize(section_size); | ||
| 322 | |||
| 323 | section_file.Seek(0, SEEK_SET); | ||
| 324 | if (section_file.ReadBytes(&buffer[0], section_size) == section_size) { | ||
| 325 | LOG_WARNING(Service_FS, "File %s overriding built-in ExeFS file", | ||
| 326 | section_override.c_str()); | ||
| 327 | return Loader::ResultStatus::Success; | ||
| 328 | } | ||
| 329 | } | ||
| 330 | return Loader::ResultStatus::ErrorNotUsed; | ||
| 331 | } | ||
| 332 | |||
| 333 | Loader::ResultStatus NCCHContainer::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, | ||
| 334 | u64& offset, u64& size) { | ||
| 335 | Loader::ResultStatus result = Load(); | ||
| 336 | if (result != Loader::ResultStatus::Success) | ||
| 337 | return result; | ||
| 338 | |||
| 339 | if (ReadOverrideRomFS(romfs_file, offset, size) == Loader::ResultStatus::Success) | ||
| 340 | return Loader::ResultStatus::Success; | ||
| 341 | |||
| 342 | if (!has_romfs) { | ||
| 343 | LOG_DEBUG(Service_FS, "RomFS requested from NCCH which has no RomFS"); | ||
| 344 | return Loader::ResultStatus::ErrorNotUsed; | ||
| 345 | } | ||
| 346 | |||
| 347 | if (!file.IsOpen()) | ||
| 348 | return Loader::ResultStatus::Error; | ||
| 349 | |||
| 350 | u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000; | ||
| 351 | u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000; | ||
| 352 | |||
| 353 | LOG_DEBUG(Service_FS, "RomFS offset: 0x%08X", romfs_offset); | ||
| 354 | LOG_DEBUG(Service_FS, "RomFS size: 0x%08X", romfs_size); | ||
| 355 | |||
| 356 | if (file.GetSize() < romfs_offset + romfs_size) | ||
| 357 | return Loader::ResultStatus::Error; | ||
| 358 | |||
| 359 | // We reopen the file, to allow its position to be independent from file's | ||
| 360 | romfs_file = std::make_shared<FileUtil::IOFile>(filepath, "rb"); | ||
| 361 | if (!romfs_file->IsOpen()) | ||
| 362 | return Loader::ResultStatus::Error; | ||
| 363 | |||
| 364 | offset = romfs_offset; | ||
| 365 | size = romfs_size; | ||
| 366 | |||
| 367 | return Loader::ResultStatus::Success; | ||
| 368 | } | ||
| 369 | |||
| 370 | Loader::ResultStatus NCCHContainer::ReadOverrideRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, | ||
| 371 | u64& offset, u64& size) { | ||
| 372 | // Check for RomFS overrides | ||
| 373 | std::string split_filepath = filepath + ".romfs"; | ||
| 374 | if (FileUtil::Exists(split_filepath)) { | ||
| 375 | romfs_file = std::make_shared<FileUtil::IOFile>(split_filepath, "rb"); | ||
| 376 | if (romfs_file->IsOpen()) { | ||
| 377 | LOG_WARNING(Service_FS, "File %s overriding built-in RomFS", split_filepath.c_str()); | ||
| 378 | offset = 0; | ||
| 379 | size = romfs_file->GetSize(); | ||
| 380 | return Loader::ResultStatus::Success; | ||
| 381 | } | ||
| 382 | } | ||
| 383 | |||
| 384 | return Loader::ResultStatus::ErrorNotUsed; | ||
| 385 | } | ||
| 386 | |||
| 387 | Loader::ResultStatus NCCHContainer::ReadProgramId(u64_le& program_id) { | ||
| 388 | Loader::ResultStatus result = Load(); | ||
| 389 | if (result != Loader::ResultStatus::Success) | ||
| 390 | return result; | ||
| 391 | |||
| 392 | if (!has_header) | ||
| 393 | return Loader::ResultStatus::ErrorNotUsed; | ||
| 394 | |||
| 395 | program_id = ncch_header.program_id; | ||
| 396 | return Loader::ResultStatus::Success; | ||
| 397 | } | ||
| 398 | |||
| 399 | bool NCCHContainer::HasExeFS() { | ||
| 400 | Loader::ResultStatus result = Load(); | ||
| 401 | if (result != Loader::ResultStatus::Success) | ||
| 402 | return false; | ||
| 403 | |||
| 404 | return has_exefs; | ||
| 405 | } | ||
| 406 | |||
| 407 | bool NCCHContainer::HasRomFS() { | ||
| 408 | Loader::ResultStatus result = Load(); | ||
| 409 | if (result != Loader::ResultStatus::Success) | ||
| 410 | return false; | ||
| 411 | |||
| 412 | return has_romfs; | ||
| 413 | } | ||
| 414 | |||
| 415 | bool NCCHContainer::HasExHeader() { | ||
| 416 | Loader::ResultStatus result = Load(); | ||
| 417 | if (result != Loader::ResultStatus::Success) | ||
| 418 | return false; | ||
| 419 | |||
| 420 | return has_exheader; | ||
| 421 | } | ||
| 422 | |||
| 423 | } // namespace FileSys | ||
diff --git a/src/core/file_sys/ncch_container.h b/src/core/file_sys/ncch_container.h new file mode 100644 index 000000000..2cc9d13dc --- /dev/null +++ b/src/core/file_sys/ncch_container.h | |||
| @@ -0,0 +1,274 @@ | |||
| 1 | // Copyright 2017 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 <cstddef> | ||
| 8 | #include <memory> | ||
| 9 | #include <string> | ||
| 10 | #include <vector> | ||
| 11 | #include "common/bit_field.h" | ||
| 12 | #include "common/common_types.h" | ||
| 13 | #include "common/file_util.h" | ||
| 14 | #include "common/swap.h" | ||
| 15 | #include "core/core.h" | ||
| 16 | |||
| 17 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 18 | /// NCCH header (Note: "NCCH" appears to be a publicly unknown acronym) | ||
| 19 | |||
| 20 | struct NCCH_Header { | ||
| 21 | u8 signature[0x100]; | ||
| 22 | u32_le magic; | ||
| 23 | u32_le content_size; | ||
| 24 | u8 partition_id[8]; | ||
| 25 | u16_le maker_code; | ||
| 26 | u16_le version; | ||
| 27 | u8 reserved_0[4]; | ||
| 28 | u64_le program_id; | ||
| 29 | u8 reserved_1[0x10]; | ||
| 30 | u8 logo_region_hash[0x20]; | ||
| 31 | u8 product_code[0x10]; | ||
| 32 | u8 extended_header_hash[0x20]; | ||
| 33 | u32_le extended_header_size; | ||
| 34 | u8 reserved_2[4]; | ||
| 35 | u8 flags[8]; | ||
| 36 | u32_le plain_region_offset; | ||
| 37 | u32_le plain_region_size; | ||
| 38 | u32_le logo_region_offset; | ||
| 39 | u32_le logo_region_size; | ||
| 40 | u32_le exefs_offset; | ||
| 41 | u32_le exefs_size; | ||
| 42 | u32_le exefs_hash_region_size; | ||
| 43 | u8 reserved_3[4]; | ||
| 44 | u32_le romfs_offset; | ||
| 45 | u32_le romfs_size; | ||
| 46 | u32_le romfs_hash_region_size; | ||
| 47 | u8 reserved_4[4]; | ||
| 48 | u8 exefs_super_block_hash[0x20]; | ||
| 49 | u8 romfs_super_block_hash[0x20]; | ||
| 50 | }; | ||
| 51 | |||
| 52 | static_assert(sizeof(NCCH_Header) == 0x200, "NCCH header structure size is wrong"); | ||
| 53 | |||
| 54 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 55 | // ExeFS (executable file system) headers | ||
| 56 | |||
| 57 | struct ExeFs_SectionHeader { | ||
| 58 | char name[8]; | ||
| 59 | u32 offset; | ||
| 60 | u32 size; | ||
| 61 | }; | ||
| 62 | |||
| 63 | struct ExeFs_Header { | ||
| 64 | ExeFs_SectionHeader section[8]; | ||
| 65 | u8 reserved[0x80]; | ||
| 66 | u8 hashes[8][0x20]; | ||
| 67 | }; | ||
| 68 | |||
| 69 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 70 | // ExHeader (executable file system header) headers | ||
| 71 | |||
| 72 | struct ExHeader_SystemInfoFlags { | ||
| 73 | u8 reserved[5]; | ||
| 74 | u8 flag; | ||
| 75 | u8 remaster_version[2]; | ||
| 76 | }; | ||
| 77 | |||
| 78 | struct ExHeader_CodeSegmentInfo { | ||
| 79 | u32 address; | ||
| 80 | u32 num_max_pages; | ||
| 81 | u32 code_size; | ||
| 82 | }; | ||
| 83 | |||
| 84 | struct ExHeader_CodeSetInfo { | ||
| 85 | u8 name[8]; | ||
| 86 | ExHeader_SystemInfoFlags flags; | ||
| 87 | ExHeader_CodeSegmentInfo text; | ||
| 88 | u32 stack_size; | ||
| 89 | ExHeader_CodeSegmentInfo ro; | ||
| 90 | u8 reserved[4]; | ||
| 91 | ExHeader_CodeSegmentInfo data; | ||
| 92 | u32 bss_size; | ||
| 93 | }; | ||
| 94 | |||
| 95 | struct ExHeader_DependencyList { | ||
| 96 | u8 program_id[0x30][8]; | ||
| 97 | }; | ||
| 98 | |||
| 99 | struct ExHeader_SystemInfo { | ||
| 100 | u64 save_data_size; | ||
| 101 | u64_le jump_id; | ||
| 102 | u8 reserved_2[0x30]; | ||
| 103 | }; | ||
| 104 | |||
| 105 | struct ExHeader_StorageInfo { | ||
| 106 | u8 ext_save_data_id[8]; | ||
| 107 | u8 system_save_data_id[8]; | ||
| 108 | u8 reserved[8]; | ||
| 109 | u8 access_info[7]; | ||
| 110 | u8 other_attributes; | ||
| 111 | }; | ||
| 112 | |||
| 113 | struct ExHeader_ARM11_SystemLocalCaps { | ||
| 114 | u64_le program_id; | ||
| 115 | u32_le core_version; | ||
| 116 | u8 reserved_flags[2]; | ||
| 117 | union { | ||
| 118 | u8 flags0; | ||
| 119 | BitField<0, 2, u8> ideal_processor; | ||
| 120 | BitField<2, 2, u8> affinity_mask; | ||
| 121 | BitField<4, 4, u8> system_mode; | ||
| 122 | }; | ||
| 123 | u8 priority; | ||
| 124 | u8 resource_limit_descriptor[0x10][2]; | ||
| 125 | ExHeader_StorageInfo storage_info; | ||
| 126 | u8 service_access_control[0x20][8]; | ||
| 127 | u8 ex_service_access_control[0x2][8]; | ||
| 128 | u8 reserved[0xf]; | ||
| 129 | u8 resource_limit_category; | ||
| 130 | }; | ||
| 131 | |||
| 132 | struct ExHeader_ARM11_KernelCaps { | ||
| 133 | u32_le descriptors[28]; | ||
| 134 | u8 reserved[0x10]; | ||
| 135 | }; | ||
| 136 | |||
| 137 | struct ExHeader_ARM9_AccessControl { | ||
| 138 | u8 descriptors[15]; | ||
| 139 | u8 descversion; | ||
| 140 | }; | ||
| 141 | |||
| 142 | struct ExHeader_Header { | ||
| 143 | ExHeader_CodeSetInfo codeset_info; | ||
| 144 | ExHeader_DependencyList dependency_list; | ||
| 145 | ExHeader_SystemInfo system_info; | ||
| 146 | ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps; | ||
| 147 | ExHeader_ARM11_KernelCaps arm11_kernel_caps; | ||
| 148 | ExHeader_ARM9_AccessControl arm9_access_control; | ||
| 149 | struct { | ||
| 150 | u8 signature[0x100]; | ||
| 151 | u8 ncch_public_key_modulus[0x100]; | ||
| 152 | ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps; | ||
| 153 | ExHeader_ARM11_KernelCaps arm11_kernel_caps; | ||
| 154 | ExHeader_ARM9_AccessControl arm9_access_control; | ||
| 155 | } access_desc; | ||
| 156 | }; | ||
| 157 | |||
| 158 | static_assert(sizeof(ExHeader_Header) == 0x800, "ExHeader structure size is wrong"); | ||
| 159 | |||
| 160 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 161 | // FileSys namespace | ||
| 162 | |||
| 163 | namespace FileSys { | ||
| 164 | |||
| 165 | /** | ||
| 166 | * Helper which implements an interface to deal with NCCH containers which can | ||
| 167 | * contain ExeFS archives or RomFS archives for games or other applications. | ||
| 168 | */ | ||
| 169 | class NCCHContainer { | ||
| 170 | public: | ||
| 171 | NCCHContainer(const std::string& filepath); | ||
| 172 | NCCHContainer() {} | ||
| 173 | |||
| 174 | Loader::ResultStatus OpenFile(const std::string& filepath); | ||
| 175 | |||
| 176 | /** | ||
| 177 | * Ensure ExeFS and exheader is loaded and ready for reading sections | ||
| 178 | * @return ResultStatus result of function | ||
| 179 | */ | ||
| 180 | Loader::ResultStatus Load(); | ||
| 181 | |||
| 182 | /** | ||
| 183 | * Attempt to find overridden sections for the NCCH and mark the container as tainted | ||
| 184 | * if any are found. | ||
| 185 | * @return ResultStatus result of function | ||
| 186 | */ | ||
| 187 | Loader::ResultStatus LoadOverrides(); | ||
| 188 | |||
| 189 | /** | ||
| 190 | * Reads an application ExeFS section of an NCCH file (e.g. .code, .logo, etc.) | ||
| 191 | * @param name Name of section to read out of NCCH file | ||
| 192 | * @param buffer Vector to read data into | ||
| 193 | * @return ResultStatus result of function | ||
| 194 | */ | ||
| 195 | Loader::ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer); | ||
| 196 | |||
| 197 | /** | ||
| 198 | * Reads an application ExeFS section from external files instead of an NCCH file, | ||
| 199 | * (e.g. code.bin, logo.bcma.lz, icon.icn, banner.bnr) | ||
| 200 | * @param name Name of section to read from external files | ||
| 201 | * @param buffer Vector to read data into | ||
| 202 | * @return ResultStatus result of function | ||
| 203 | */ | ||
| 204 | Loader::ResultStatus LoadOverrideExeFSSection(const char* name, std::vector<u8>& buffer); | ||
| 205 | |||
| 206 | /** | ||
| 207 | * Get the RomFS of the NCCH container | ||
| 208 | * Since the RomFS can be huge, we return a file reference instead of copying to a buffer | ||
| 209 | * @param romfs_file The file containing the RomFS | ||
| 210 | * @param offset The offset the romfs begins on | ||
| 211 | * @param size The size of the romfs | ||
| 212 | * @return ResultStatus result of function | ||
| 213 | */ | ||
| 214 | Loader::ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, | ||
| 215 | u64& size); | ||
| 216 | |||
| 217 | /** | ||
| 218 | * Get the override RomFS of the NCCH container | ||
| 219 | * Since the RomFS can be huge, we return a file reference instead of copying to a buffer | ||
| 220 | * @param romfs_file The file containing the RomFS | ||
| 221 | * @param offset The offset the romfs begins on | ||
| 222 | * @param size The size of the romfs | ||
| 223 | * @return ResultStatus result of function | ||
| 224 | */ | ||
| 225 | Loader::ResultStatus ReadOverrideRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, | ||
| 226 | u64& offset, u64& size); | ||
| 227 | |||
| 228 | /** | ||
| 229 | * Get the Program ID of the NCCH container | ||
| 230 | * @return ResultStatus result of function | ||
| 231 | */ | ||
| 232 | Loader::ResultStatus ReadProgramId(u64_le& program_id); | ||
| 233 | |||
| 234 | /** | ||
| 235 | * Checks whether the NCCH container contains an ExeFS | ||
| 236 | * @return bool check result | ||
| 237 | */ | ||
| 238 | bool HasExeFS(); | ||
| 239 | |||
| 240 | /** | ||
| 241 | * Checks whether the NCCH container contains a RomFS | ||
| 242 | * @return bool check result | ||
| 243 | */ | ||
| 244 | bool HasRomFS(); | ||
| 245 | |||
| 246 | /** | ||
| 247 | * Checks whether the NCCH container contains an ExHeader | ||
| 248 | * @return bool check result | ||
| 249 | */ | ||
| 250 | bool HasExHeader(); | ||
| 251 | |||
| 252 | NCCH_Header ncch_header; | ||
| 253 | ExeFs_Header exefs_header; | ||
| 254 | ExHeader_Header exheader_header; | ||
| 255 | |||
| 256 | private: | ||
| 257 | bool has_header = false; | ||
| 258 | bool has_exheader = false; | ||
| 259 | bool has_exefs = false; | ||
| 260 | bool has_romfs = false; | ||
| 261 | |||
| 262 | bool is_tainted = false; // Are there parts of this container being overridden? | ||
| 263 | bool is_loaded = false; | ||
| 264 | bool is_compressed = false; | ||
| 265 | |||
| 266 | u32 ncch_offset = 0; // Offset to NCCH header, can be 0 or after NCSD header | ||
| 267 | u32 exefs_offset = 0; | ||
| 268 | |||
| 269 | std::string filepath; | ||
| 270 | FileUtil::IOFile file; | ||
| 271 | FileUtil::IOFile exefs_file; | ||
| 272 | }; | ||
| 273 | |||
| 274 | } // namespace FileSys | ||
diff --git a/src/core/file_sys/savedata_archive.cpp b/src/core/file_sys/savedata_archive.cpp index f540c4a93..f8f811ba0 100644 --- a/src/core/file_sys/savedata_archive.cpp +++ b/src/core/file_sys/savedata_archive.cpp | |||
| @@ -106,7 +106,25 @@ ResultCode SaveDataArchive::DeleteFile(const Path& path) const { | |||
| 106 | } | 106 | } |
| 107 | 107 | ||
| 108 | ResultCode SaveDataArchive::RenameFile(const Path& src_path, const Path& dest_path) const { | 108 | ResultCode SaveDataArchive::RenameFile(const Path& src_path, const Path& dest_path) const { |
| 109 | if (FileUtil::Rename(mount_point + src_path.AsString(), mount_point + dest_path.AsString())) { | 109 | const PathParser path_parser_src(src_path); |
| 110 | |||
| 111 | // TODO: Verify these return codes with HW | ||
| 112 | if (!path_parser_src.IsValid()) { | ||
| 113 | LOG_ERROR(Service_FS, "Invalid src path %s", src_path.DebugStr().c_str()); | ||
| 114 | return ERROR_INVALID_PATH; | ||
| 115 | } | ||
| 116 | |||
| 117 | const PathParser path_parser_dest(dest_path); | ||
| 118 | |||
| 119 | if (!path_parser_dest.IsValid()) { | ||
| 120 | LOG_ERROR(Service_FS, "Invalid dest path %s", dest_path.DebugStr().c_str()); | ||
| 121 | return ERROR_INVALID_PATH; | ||
| 122 | } | ||
| 123 | |||
| 124 | const auto src_path_full = path_parser_src.BuildHostPath(mount_point); | ||
| 125 | const auto dest_path_full = path_parser_dest.BuildHostPath(mount_point); | ||
| 126 | |||
| 127 | if (FileUtil::Rename(src_path_full, dest_path_full)) { | ||
| 110 | return RESULT_SUCCESS; | 128 | return RESULT_SUCCESS; |
| 111 | } | 129 | } |
| 112 | 130 | ||
| @@ -247,8 +265,27 @@ ResultCode SaveDataArchive::CreateDirectory(const Path& path) const { | |||
| 247 | } | 265 | } |
| 248 | 266 | ||
| 249 | ResultCode SaveDataArchive::RenameDirectory(const Path& src_path, const Path& dest_path) const { | 267 | ResultCode SaveDataArchive::RenameDirectory(const Path& src_path, const Path& dest_path) const { |
| 250 | if (FileUtil::Rename(mount_point + src_path.AsString(), mount_point + dest_path.AsString())) | 268 | const PathParser path_parser_src(src_path); |
| 269 | |||
| 270 | // TODO: Verify these return codes with HW | ||
| 271 | if (!path_parser_src.IsValid()) { | ||
| 272 | LOG_ERROR(Service_FS, "Invalid src path %s", src_path.DebugStr().c_str()); | ||
| 273 | return ERROR_INVALID_PATH; | ||
| 274 | } | ||
| 275 | |||
| 276 | const PathParser path_parser_dest(dest_path); | ||
| 277 | |||
| 278 | if (!path_parser_dest.IsValid()) { | ||
| 279 | LOG_ERROR(Service_FS, "Invalid dest path %s", dest_path.DebugStr().c_str()); | ||
| 280 | return ERROR_INVALID_PATH; | ||
| 281 | } | ||
| 282 | |||
| 283 | const auto src_path_full = path_parser_src.BuildHostPath(mount_point); | ||
| 284 | const auto dest_path_full = path_parser_dest.BuildHostPath(mount_point); | ||
| 285 | |||
| 286 | if (FileUtil::Rename(src_path_full, dest_path_full)) { | ||
| 251 | return RESULT_SUCCESS; | 287 | return RESULT_SUCCESS; |
| 288 | } | ||
| 252 | 289 | ||
| 253 | // TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't | 290 | // TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't |
| 254 | // exist or similar. Verify. | 291 | // exist or similar. Verify. |
diff --git a/src/core/file_sys/title_metadata.cpp b/src/core/file_sys/title_metadata.cpp new file mode 100644 index 000000000..1ef8840a0 --- /dev/null +++ b/src/core/file_sys/title_metadata.cpp | |||
| @@ -0,0 +1,212 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <cinttypes> | ||
| 6 | #include <cryptopp/sha.h> | ||
| 7 | #include "common/alignment.h" | ||
| 8 | #include "common/file_util.h" | ||
| 9 | #include "common/logging/log.h" | ||
| 10 | #include "core/file_sys/title_metadata.h" | ||
| 11 | #include "core/loader/loader.h" | ||
| 12 | |||
| 13 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 14 | // FileSys namespace | ||
| 15 | |||
| 16 | namespace FileSys { | ||
| 17 | |||
| 18 | static u32 GetSignatureSize(u32 signature_type) { | ||
| 19 | switch (signature_type) { | ||
| 20 | case Rsa4096Sha1: | ||
| 21 | case Rsa4096Sha256: | ||
| 22 | return 0x200; | ||
| 23 | |||
| 24 | case Rsa2048Sha1: | ||
| 25 | case Rsa2048Sha256: | ||
| 26 | return 0x100; | ||
| 27 | |||
| 28 | case EllipticSha1: | ||
| 29 | case EcdsaSha256: | ||
| 30 | return 0x3C; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | Loader::ResultStatus TitleMetadata::Load() { | ||
| 35 | FileUtil::IOFile file(filepath, "rb"); | ||
| 36 | if (!file.IsOpen()) | ||
| 37 | return Loader::ResultStatus::Error; | ||
| 38 | |||
| 39 | if (!file.ReadBytes(&signature_type, sizeof(u32_be))) | ||
| 40 | return Loader::ResultStatus::Error; | ||
| 41 | |||
| 42 | // Signature lengths are variable, and the body follows the signature | ||
| 43 | u32 signature_size = GetSignatureSize(signature_type); | ||
| 44 | |||
| 45 | tmd_signature.resize(signature_size); | ||
| 46 | if (!file.ReadBytes(&tmd_signature[0], signature_size)) | ||
| 47 | return Loader::ResultStatus::Error; | ||
| 48 | |||
| 49 | // The TMD body start position is rounded to the nearest 0x40 after the signature | ||
| 50 | size_t body_start = Common::AlignUp(signature_size + sizeof(u32), 0x40); | ||
| 51 | file.Seek(body_start, SEEK_SET); | ||
| 52 | |||
| 53 | // Read our TMD body, then load the amount of ContentChunks specified | ||
| 54 | if (file.ReadBytes(&tmd_body, sizeof(TitleMetadata::Body)) != sizeof(TitleMetadata::Body)) | ||
| 55 | return Loader::ResultStatus::Error; | ||
| 56 | |||
| 57 | for (u16 i = 0; i < tmd_body.content_count; i++) { | ||
| 58 | ContentChunk chunk; | ||
| 59 | if (file.ReadBytes(&chunk, sizeof(ContentChunk)) == sizeof(ContentChunk)) { | ||
| 60 | tmd_chunks.push_back(chunk); | ||
| 61 | } else { | ||
| 62 | LOG_ERROR(Service_FS, "Malformed TMD %s, failed to load content chunk index %u!", | ||
| 63 | filepath.c_str(), i); | ||
| 64 | return Loader::ResultStatus::ErrorInvalidFormat; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | return Loader::ResultStatus::Success; | ||
| 69 | } | ||
| 70 | |||
| 71 | Loader::ResultStatus TitleMetadata::Save() { | ||
| 72 | FileUtil::IOFile file(filepath, "wb"); | ||
| 73 | if (!file.IsOpen()) | ||
| 74 | return Loader::ResultStatus::Error; | ||
| 75 | |||
| 76 | if (!file.WriteBytes(&signature_type, sizeof(u32_be))) | ||
| 77 | return Loader::ResultStatus::Error; | ||
| 78 | |||
| 79 | // Signature lengths are variable, and the body follows the signature | ||
| 80 | u32 signature_size = GetSignatureSize(signature_type); | ||
| 81 | |||
| 82 | if (!file.WriteBytes(tmd_signature.data(), signature_size)) | ||
| 83 | return Loader::ResultStatus::Error; | ||
| 84 | |||
| 85 | // The TMD body start position is rounded to the nearest 0x40 after the signature | ||
| 86 | size_t body_start = Common::AlignUp(signature_size + sizeof(u32), 0x40); | ||
| 87 | file.Seek(body_start, SEEK_SET); | ||
| 88 | |||
| 89 | // Update our TMD body values and hashes | ||
| 90 | tmd_body.content_count = static_cast<u16>(tmd_chunks.size()); | ||
| 91 | |||
| 92 | // TODO(shinyquagsire23): Do TMDs with more than one contentinfo exist? | ||
| 93 | // For now we'll just adjust the first index to hold all content chunks | ||
| 94 | // and ensure that no further content info data exists. | ||
| 95 | tmd_body.contentinfo = {}; | ||
| 96 | tmd_body.contentinfo[0].index = 0; | ||
| 97 | tmd_body.contentinfo[0].command_count = static_cast<u16>(tmd_chunks.size()); | ||
| 98 | |||
| 99 | CryptoPP::SHA256 chunk_hash; | ||
| 100 | for (u16 i = 0; i < tmd_body.content_count; i++) { | ||
| 101 | chunk_hash.Update(reinterpret_cast<u8*>(&tmd_chunks[i]), sizeof(ContentChunk)); | ||
| 102 | } | ||
| 103 | chunk_hash.Final(tmd_body.contentinfo[0].hash.data()); | ||
| 104 | |||
| 105 | CryptoPP::SHA256 contentinfo_hash; | ||
| 106 | for (size_t i = 0; i < tmd_body.contentinfo.size(); i++) { | ||
| 107 | chunk_hash.Update(reinterpret_cast<u8*>(&tmd_body.contentinfo[i]), sizeof(ContentInfo)); | ||
| 108 | } | ||
| 109 | chunk_hash.Final(tmd_body.contentinfo_hash.data()); | ||
| 110 | |||
| 111 | // Write our TMD body, then write each of our ContentChunks | ||
| 112 | if (file.WriteBytes(&tmd_body, sizeof(TitleMetadata::Body)) != sizeof(TitleMetadata::Body)) | ||
| 113 | return Loader::ResultStatus::Error; | ||
| 114 | |||
| 115 | for (u16 i = 0; i < tmd_body.content_count; i++) { | ||
| 116 | ContentChunk chunk = tmd_chunks[i]; | ||
| 117 | if (file.WriteBytes(&chunk, sizeof(ContentChunk)) != sizeof(ContentChunk)) | ||
| 118 | return Loader::ResultStatus::Error; | ||
| 119 | } | ||
| 120 | |||
| 121 | return Loader::ResultStatus::Success; | ||
| 122 | } | ||
| 123 | |||
| 124 | u64 TitleMetadata::GetTitleID() const { | ||
| 125 | return tmd_body.title_id; | ||
| 126 | } | ||
| 127 | |||
| 128 | u32 TitleMetadata::GetTitleType() const { | ||
| 129 | return tmd_body.title_type; | ||
| 130 | } | ||
| 131 | |||
| 132 | u16 TitleMetadata::GetTitleVersion() const { | ||
| 133 | return tmd_body.title_version; | ||
| 134 | } | ||
| 135 | |||
| 136 | u64 TitleMetadata::GetSystemVersion() const { | ||
| 137 | return tmd_body.system_version; | ||
| 138 | } | ||
| 139 | |||
| 140 | size_t TitleMetadata::GetContentCount() const { | ||
| 141 | return tmd_chunks.size(); | ||
| 142 | } | ||
| 143 | |||
| 144 | u32 TitleMetadata::GetBootContentID() const { | ||
| 145 | return tmd_chunks[TMDContentIndex::Main].id; | ||
| 146 | } | ||
| 147 | |||
| 148 | u32 TitleMetadata::GetManualContentID() const { | ||
| 149 | return tmd_chunks[TMDContentIndex::Manual].id; | ||
| 150 | } | ||
| 151 | |||
| 152 | u32 TitleMetadata::GetDLPContentID() const { | ||
| 153 | return tmd_chunks[TMDContentIndex::DLP].id; | ||
| 154 | } | ||
| 155 | |||
| 156 | void TitleMetadata::SetTitleID(u64 title_id) { | ||
| 157 | tmd_body.title_id = title_id; | ||
| 158 | } | ||
| 159 | |||
| 160 | void TitleMetadata::SetTitleType(u32 type) { | ||
| 161 | tmd_body.title_type = type; | ||
| 162 | } | ||
| 163 | |||
| 164 | void TitleMetadata::SetTitleVersion(u16 version) { | ||
| 165 | tmd_body.title_version = version; | ||
| 166 | } | ||
| 167 | |||
| 168 | void TitleMetadata::SetSystemVersion(u64 version) { | ||
| 169 | tmd_body.system_version = version; | ||
| 170 | } | ||
| 171 | |||
| 172 | void TitleMetadata::AddContentChunk(const ContentChunk& chunk) { | ||
| 173 | tmd_chunks.push_back(chunk); | ||
| 174 | } | ||
| 175 | |||
| 176 | void TitleMetadata::Print() const { | ||
| 177 | LOG_DEBUG(Service_FS, "%s - %u chunks", filepath.c_str(), | ||
| 178 | static_cast<u32>(tmd_body.content_count)); | ||
| 179 | |||
| 180 | // Content info describes ranges of content chunks | ||
| 181 | LOG_DEBUG(Service_FS, "Content info:"); | ||
| 182 | for (size_t i = 0; i < tmd_body.contentinfo.size(); i++) { | ||
| 183 | if (tmd_body.contentinfo[i].command_count == 0) | ||
| 184 | break; | ||
| 185 | |||
| 186 | LOG_DEBUG(Service_FS, " Index %04X, Command Count %04X", | ||
| 187 | static_cast<u32>(tmd_body.contentinfo[i].index), | ||
| 188 | static_cast<u32>(tmd_body.contentinfo[i].command_count)); | ||
| 189 | } | ||
| 190 | |||
| 191 | // For each content info, print their content chunk range | ||
| 192 | for (size_t i = 0; i < tmd_body.contentinfo.size(); i++) { | ||
| 193 | u16 index = static_cast<u16>(tmd_body.contentinfo[i].index); | ||
| 194 | u16 count = static_cast<u16>(tmd_body.contentinfo[i].command_count); | ||
| 195 | |||
| 196 | if (count == 0) | ||
| 197 | continue; | ||
| 198 | |||
| 199 | LOG_DEBUG(Service_FS, "Content chunks for content info index %zu:", i); | ||
| 200 | for (u16 j = index; j < index + count; j++) { | ||
| 201 | // Don't attempt to print content we don't have | ||
| 202 | if (j > tmd_body.content_count) | ||
| 203 | break; | ||
| 204 | |||
| 205 | const ContentChunk& chunk = tmd_chunks[j]; | ||
| 206 | LOG_DEBUG(Service_FS, " ID %08X, Index %04X, Type %04x, Size %016" PRIX64, | ||
| 207 | static_cast<u32>(chunk.id), static_cast<u32>(chunk.index), | ||
| 208 | static_cast<u32>(chunk.type), static_cast<u64>(chunk.size)); | ||
| 209 | } | ||
| 210 | } | ||
| 211 | } | ||
| 212 | } // namespace FileSys | ||
diff --git a/src/core/file_sys/title_metadata.h b/src/core/file_sys/title_metadata.h new file mode 100644 index 000000000..1fc157bf3 --- /dev/null +++ b/src/core/file_sys/title_metadata.h | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | // Copyright 2017 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 <string> | ||
| 8 | #include <vector> | ||
| 9 | #include "common/common_types.h" | ||
| 10 | #include "common/swap.h" | ||
| 11 | |||
| 12 | namespace Loader { | ||
| 13 | enum class ResultStatus; | ||
| 14 | } | ||
| 15 | |||
| 16 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 17 | // FileSys namespace | ||
| 18 | |||
| 19 | namespace FileSys { | ||
| 20 | |||
| 21 | enum TMDSignatureType : u32 { | ||
| 22 | Rsa4096Sha1 = 0x10000, | ||
| 23 | Rsa2048Sha1 = 0x10001, | ||
| 24 | EllipticSha1 = 0x10002, | ||
| 25 | Rsa4096Sha256 = 0x10003, | ||
| 26 | Rsa2048Sha256 = 0x10004, | ||
| 27 | EcdsaSha256 = 0x10005 | ||
| 28 | }; | ||
| 29 | |||
| 30 | enum TMDContentTypeFlag : u16 { | ||
| 31 | Encrypted = 1 << 1, | ||
| 32 | Disc = 1 << 2, | ||
| 33 | CFM = 1 << 3, | ||
| 34 | Optional = 1 << 14, | ||
| 35 | Shared = 1 << 15 | ||
| 36 | }; | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Helper which implements an interface to read and write Title Metadata (TMD) files. | ||
| 40 | * If a file path is provided and the file exists, it can be parsed and used, otherwise | ||
| 41 | * it must be created. The TMD file can then be interpreted, modified and/or saved. | ||
| 42 | */ | ||
| 43 | class TitleMetadata { | ||
| 44 | public: | ||
| 45 | struct ContentChunk { | ||
| 46 | u32_be id; | ||
| 47 | u16_be index; | ||
| 48 | u16_be type; | ||
| 49 | u64_be size; | ||
| 50 | std::array<u8, 0x20> hash; | ||
| 51 | }; | ||
| 52 | |||
| 53 | static_assert(sizeof(ContentChunk) == 0x30, "TMD ContentChunk structure size is wrong"); | ||
| 54 | |||
| 55 | struct ContentInfo { | ||
| 56 | u16_be index; | ||
| 57 | u16_be command_count; | ||
| 58 | std::array<u8, 0x20> hash; | ||
| 59 | }; | ||
| 60 | |||
| 61 | static_assert(sizeof(ContentInfo) == 0x24, "TMD ContentInfo structure size is wrong"); | ||
| 62 | |||
| 63 | #pragma pack(push, 1) | ||
| 64 | |||
| 65 | struct Body { | ||
| 66 | std::array<u8, 0x40> issuer; | ||
| 67 | u8 version; | ||
| 68 | u8 ca_crl_version; | ||
| 69 | u8 signer_crl_version; | ||
| 70 | u8 reserved; | ||
| 71 | u64_be system_version; | ||
| 72 | u64_be title_id; | ||
| 73 | u32_be title_type; | ||
| 74 | u16_be group_id; | ||
| 75 | u32_be savedata_size; | ||
| 76 | u32_be srl_private_savedata_size; | ||
| 77 | std::array<u8, 4> reserved_2; | ||
| 78 | u8 srl_flag; | ||
| 79 | std::array<u8, 0x31> reserved_3; | ||
| 80 | u32_be access_rights; | ||
| 81 | u16_be title_version; | ||
| 82 | u16_be content_count; | ||
| 83 | u16_be boot_content; | ||
| 84 | std::array<u8, 2> reserved_4; | ||
| 85 | std::array<u8, 0x20> contentinfo_hash; | ||
| 86 | std::array<ContentInfo, 64> contentinfo; | ||
| 87 | }; | ||
| 88 | |||
| 89 | static_assert(sizeof(Body) == 0x9C4, "TMD body structure size is wrong"); | ||
| 90 | |||
| 91 | #pragma pack(pop) | ||
| 92 | |||
| 93 | explicit TitleMetadata(std::string& path) : filepath(std::move(path)) {} | ||
| 94 | Loader::ResultStatus Load(); | ||
| 95 | Loader::ResultStatus Save(); | ||
| 96 | |||
| 97 | u64 GetTitleID() const; | ||
| 98 | u32 GetTitleType() const; | ||
| 99 | u16 GetTitleVersion() const; | ||
| 100 | u64 GetSystemVersion() const; | ||
| 101 | size_t GetContentCount() const; | ||
| 102 | u32 GetBootContentID() const; | ||
| 103 | u32 GetManualContentID() const; | ||
| 104 | u32 GetDLPContentID() const; | ||
| 105 | |||
| 106 | void SetTitleID(u64 title_id); | ||
| 107 | void SetTitleType(u32 type); | ||
| 108 | void SetTitleVersion(u16 version); | ||
| 109 | void SetSystemVersion(u64 version); | ||
| 110 | void AddContentChunk(const ContentChunk& chunk); | ||
| 111 | |||
| 112 | void Print() const; | ||
| 113 | |||
| 114 | private: | ||
| 115 | enum TMDContentIndex { Main = 0, Manual = 1, DLP = 2 }; | ||
| 116 | |||
| 117 | Body tmd_body; | ||
| 118 | u32_be signature_type; | ||
| 119 | std::vector<u8> tmd_signature; | ||
| 120 | std::vector<ContentChunk> tmd_chunks; | ||
| 121 | |||
| 122 | std::string filepath; | ||
| 123 | }; | ||
| 124 | |||
| 125 | } // namespace FileSys | ||