diff options
| author | 2018-02-25 03:34:19 -0700 | |
|---|---|---|
| committer | 2018-02-25 07:02:39 -0700 | |
| commit | 2b28fd78094ce754d06b81ed50bf3fda5352feb1 (patch) | |
| tree | 35f5906679eab89aec6c8f3a52cfc8411b5807a4 | |
| parent | Merge pull request #212 from mailwl/stubs (diff) | |
| download | yuzu-2b28fd78094ce754d06b81ed50bf3fda5352feb1.tar.gz yuzu-2b28fd78094ce754d06b81ed50bf3fda5352feb1.tar.xz yuzu-2b28fd78094ce754d06b81ed50bf3fda5352feb1.zip | |
file_sys: Add support for parsing NPDM files
Diffstat (limited to '')
| -rw-r--r-- | src/core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/core/file_sys/program_metadata.cpp | 117 | ||||
| -rw-r--r-- | src/core/file_sys/program_metadata.h | 157 |
3 files changed, 276 insertions, 0 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index ec011787e..1bc536075 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt | |||
| @@ -12,6 +12,8 @@ add_library(core STATIC | |||
| 12 | file_sys/filesystem.h | 12 | file_sys/filesystem.h |
| 13 | file_sys/path_parser.cpp | 13 | file_sys/path_parser.cpp |
| 14 | file_sys/path_parser.h | 14 | file_sys/path_parser.h |
| 15 | file_sys/program_metadata.cpp | ||
| 16 | file_sys/program_metadata.h | ||
| 15 | file_sys/romfs_factory.cpp | 17 | file_sys/romfs_factory.cpp |
| 16 | file_sys/romfs_factory.h | 18 | file_sys/romfs_factory.h |
| 17 | file_sys/romfs_filesystem.cpp | 19 | file_sys/romfs_filesystem.cpp |
diff --git a/src/core/file_sys/program_metadata.cpp b/src/core/file_sys/program_metadata.cpp new file mode 100644 index 000000000..041617bb5 --- /dev/null +++ b/src/core/file_sys/program_metadata.cpp | |||
| @@ -0,0 +1,117 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <cinttypes> | ||
| 6 | #include "common/file_util.h" | ||
| 7 | #include "common/logging/log.h" | ||
| 8 | #include "core/file_sys/program_metadata.h" | ||
| 9 | #include "core/loader/loader.h" | ||
| 10 | |||
| 11 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 12 | // FileSys namespace | ||
| 13 | |||
| 14 | namespace FileSys { | ||
| 15 | |||
| 16 | Loader::ResultStatus ProgramMetadata::Load(const std::string& file_path) { | ||
| 17 | FileUtil::IOFile file(file_path, "rb"); | ||
| 18 | if (!file.IsOpen()) | ||
| 19 | return Loader::ResultStatus::Error; | ||
| 20 | |||
| 21 | std::vector<u8> file_data(file.GetSize()); | ||
| 22 | |||
| 23 | if (!file.ReadBytes(file_data.data(), file_data.size())) | ||
| 24 | return Loader::ResultStatus::Error; | ||
| 25 | |||
| 26 | Loader::ResultStatus result = Load(file_data); | ||
| 27 | if (result != Loader::ResultStatus::Success) | ||
| 28 | LOG_ERROR(Service_FS, "Failed to load NPDM from file %s!", file_path.c_str()); | ||
| 29 | |||
| 30 | return result; | ||
| 31 | } | ||
| 32 | |||
| 33 | Loader::ResultStatus ProgramMetadata::Load(const std::vector<u8> file_data, size_t offset) { | ||
| 34 | size_t total_size = static_cast<size_t>(file_data.size() - offset); | ||
| 35 | if (total_size < sizeof(Header)) | ||
| 36 | return Loader::ResultStatus::Error; | ||
| 37 | |||
| 38 | size_t header_offset = offset; | ||
| 39 | memcpy(&npdm_header, &file_data[offset], sizeof(Header)); | ||
| 40 | |||
| 41 | size_t aci_offset = header_offset + npdm_header.aci_offset; | ||
| 42 | size_t acid_offset = header_offset + npdm_header.acid_offset; | ||
| 43 | memcpy(&aci_header, &file_data[aci_offset], sizeof(AciHeader)); | ||
| 44 | memcpy(&acid_header, &file_data[acid_offset], sizeof(AcidHeader)); | ||
| 45 | |||
| 46 | size_t fac_offset = acid_offset + acid_header.fac_offset; | ||
| 47 | size_t fah_offset = aci_offset + aci_header.fah_offset; | ||
| 48 | memcpy(&acid_file_access, &file_data[fac_offset], sizeof(FileAccessControl)); | ||
| 49 | memcpy(&aci_file_access, &file_data[fah_offset], sizeof(FileAccessHeader)); | ||
| 50 | |||
| 51 | return Loader::ResultStatus::Success; | ||
| 52 | } | ||
| 53 | |||
| 54 | bool ProgramMetadata::Is64BitProgram() const { | ||
| 55 | return npdm_header.has_64_bit_instructions; | ||
| 56 | } | ||
| 57 | |||
| 58 | ProgramAddressSpaceType ProgramMetadata::GetAddressSpaceType() const { | ||
| 59 | return npdm_header.address_space_type; | ||
| 60 | } | ||
| 61 | |||
| 62 | u8 ProgramMetadata::GetMainThreadPriority() const { | ||
| 63 | return npdm_header.main_thread_priority; | ||
| 64 | } | ||
| 65 | |||
| 66 | u8 ProgramMetadata::GetMainThreadCore() const { | ||
| 67 | return npdm_header.main_thread_cpu; | ||
| 68 | } | ||
| 69 | |||
| 70 | u32 ProgramMetadata::GetMainThreadStackSize() const { | ||
| 71 | return npdm_header.main_stack_size; | ||
| 72 | } | ||
| 73 | |||
| 74 | u64 ProgramMetadata::GetTitleID() const { | ||
| 75 | return aci_header.title_id; | ||
| 76 | } | ||
| 77 | |||
| 78 | u64 ProgramMetadata::GetFilesystemPermissions() const { | ||
| 79 | return aci_file_access.permissions; | ||
| 80 | } | ||
| 81 | |||
| 82 | void ProgramMetadata::Print() const { | ||
| 83 | LOG_DEBUG(Service_FS, "Magic: %.4s", npdm_header.magic.data()); | ||
| 84 | LOG_DEBUG(Service_FS, "Main thread priority: 0x%02x", npdm_header.main_thread_priority); | ||
| 85 | LOG_DEBUG(Service_FS, "Main thread core: %u", npdm_header.main_thread_cpu); | ||
| 86 | LOG_DEBUG(Service_FS, "Main thread stack size: 0x%x bytes", npdm_header.main_stack_size); | ||
| 87 | LOG_DEBUG(Service_FS, "Process category: %u", npdm_header.process_category); | ||
| 88 | LOG_DEBUG(Service_FS, "Flags: %02x", npdm_header.flags); | ||
| 89 | LOG_DEBUG(Service_FS, " > 64-bit instructions: %s", | ||
| 90 | npdm_header.has_64_bit_instructions ? "YES" : "NO"); | ||
| 91 | |||
| 92 | auto address_space = "Unknown"; | ||
| 93 | switch (npdm_header.address_space_type) { | ||
| 94 | case ProgramAddressSpaceType::Is64Bit: | ||
| 95 | address_space = "64-bit"; | ||
| 96 | break; | ||
| 97 | case ProgramAddressSpaceType::Is32Bit: | ||
| 98 | address_space = "32-bit"; | ||
| 99 | break; | ||
| 100 | } | ||
| 101 | |||
| 102 | LOG_DEBUG(Service_FS, " > Address space: %s\n", address_space); | ||
| 103 | |||
| 104 | // Begin ACID printing (potential perms, signed) | ||
| 105 | LOG_DEBUG(Service_FS, "Magic: %.4s", acid_header.magic.data()); | ||
| 106 | LOG_DEBUG(Service_FS, "Flags: %02x", acid_header.flags); | ||
| 107 | LOG_DEBUG(Service_FS, " > Is Retail: %s", acid_header.is_retail ? "YES" : "NO"); | ||
| 108 | LOG_DEBUG(Service_FS, "Title ID Min: %016" PRIX64, acid_header.title_id_min); | ||
| 109 | LOG_DEBUG(Service_FS, "Title ID Max: %016" PRIX64, acid_header.title_id_max); | ||
| 110 | LOG_DEBUG(Service_FS, "Filesystem Access: %016" PRIX64 "\n", acid_file_access.permissions); | ||
| 111 | |||
| 112 | // Begin ACI0 printing (actual perms, unsigned) | ||
| 113 | LOG_DEBUG(Service_FS, "Magic: %.4s", aci_header.magic.data()); | ||
| 114 | LOG_DEBUG(Service_FS, "Title ID: %016" PRIX64, aci_header.title_id); | ||
| 115 | LOG_DEBUG(Service_FS, "Filesystem Access: %016" PRIX64 "\n", aci_file_access.permissions); | ||
| 116 | } | ||
| 117 | } // namespace FileSys | ||
diff --git a/src/core/file_sys/program_metadata.h b/src/core/file_sys/program_metadata.h new file mode 100644 index 000000000..4e04a8e68 --- /dev/null +++ b/src/core/file_sys/program_metadata.h | |||
| @@ -0,0 +1,157 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <string> | ||
| 9 | #include <vector> | ||
| 10 | #include "common/bit_field.h" | ||
| 11 | #include "common/common_types.h" | ||
| 12 | #include "common/swap.h" | ||
| 13 | |||
| 14 | namespace Loader { | ||
| 15 | enum class ResultStatus; | ||
| 16 | } | ||
| 17 | |||
| 18 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 19 | // FileSys namespace | ||
| 20 | |||
| 21 | namespace FileSys { | ||
| 22 | |||
| 23 | enum class ProgramAddressSpaceType : u8 { | ||
| 24 | Is64Bit = 1, | ||
| 25 | Is32Bit = 2, | ||
| 26 | }; | ||
| 27 | |||
| 28 | enum class ProgramFilePermission : u64 { | ||
| 29 | MountContent = 1ULL << 0, | ||
| 30 | SaveDataBackup = 1ULL << 5, | ||
| 31 | SdCard = 1ULL << 21, | ||
| 32 | Calibration = 1ULL << 34, | ||
| 33 | Bit62 = 1ULL << 62, | ||
| 34 | Everything = 1ULL << 63, | ||
| 35 | }; | ||
| 36 | |||
| 37 | /** | ||
| 38 | * Helper which implements an interface to parse Program Description Metadata (NPDM) | ||
| 39 | * Data can either be loaded from a file path or with data and an offset into it. | ||
| 40 | */ | ||
| 41 | class ProgramMetadata { | ||
| 42 | public: | ||
| 43 | Loader::ResultStatus Load(const std::string& file_path); | ||
| 44 | Loader::ResultStatus Load(const std::vector<u8> file_data, size_t offset = 0); | ||
| 45 | |||
| 46 | bool Is64BitProgram() const; | ||
| 47 | ProgramAddressSpaceType GetAddressSpaceType() const; | ||
| 48 | u8 GetMainThreadPriority() const; | ||
| 49 | u8 GetMainThreadCore() const; | ||
| 50 | u32 GetMainThreadStackSize() const; | ||
| 51 | u64 GetTitleID() const; | ||
| 52 | u64 GetFilesystemPermissions() const; | ||
| 53 | |||
| 54 | void Print() const; | ||
| 55 | |||
| 56 | private: | ||
| 57 | struct Header { | ||
| 58 | std::array<char, 4> magic; | ||
| 59 | std::array<u8, 8> reserved; | ||
| 60 | union { | ||
| 61 | u8 flags; | ||
| 62 | |||
| 63 | BitField<0, 1, u8> has_64_bit_instructions; | ||
| 64 | BitField<1, 3, ProgramAddressSpaceType> address_space_type; | ||
| 65 | BitField<4, 4, u8> reserved_2; | ||
| 66 | }; | ||
| 67 | u8 reserved_3; | ||
| 68 | u8 main_thread_priority; | ||
| 69 | u8 main_thread_cpu; | ||
| 70 | std::array<u8, 8> reserved_4; | ||
| 71 | u32_le process_category; | ||
| 72 | u32_le main_stack_size; | ||
| 73 | std::array<u8, 0x10> application_name; | ||
| 74 | std::array<u8, 0x40> reserved_5; | ||
| 75 | u32_le aci_offset; | ||
| 76 | u32_le aci_size; | ||
| 77 | u32_le acid_offset; | ||
| 78 | u32_le acid_size; | ||
| 79 | }; | ||
| 80 | |||
| 81 | static_assert(sizeof(Header) == 0x80, "NPDM header structure size is wrong"); | ||
| 82 | |||
| 83 | struct AcidHeader { | ||
| 84 | std::array<u8, 0x100> signature; | ||
| 85 | std::array<u8, 0x100> nca_modulus; | ||
| 86 | std::array<char, 4> magic; | ||
| 87 | u32_le nca_size; | ||
| 88 | std::array<u8, 0x4> reserved; | ||
| 89 | union { | ||
| 90 | u32 flags; | ||
| 91 | |||
| 92 | BitField<0, 1, u32> is_retail; | ||
| 93 | BitField<1, 31, u32> flags_unk; | ||
| 94 | }; | ||
| 95 | u64_le title_id_min; | ||
| 96 | u64_le title_id_max; | ||
| 97 | u32_le fac_offset; | ||
| 98 | u32_le fac_size; | ||
| 99 | u32_le sac_offset; | ||
| 100 | u32_le sac_size; | ||
| 101 | u32_le kac_offset; | ||
| 102 | u32_le kac_size; | ||
| 103 | std::array<u8, 0x8> padding; | ||
| 104 | }; | ||
| 105 | |||
| 106 | static_assert(sizeof(AcidHeader) == 0x240, "ACID header structure size is wrong"); | ||
| 107 | |||
| 108 | struct AciHeader { | ||
| 109 | std::array<char, 4> magic; | ||
| 110 | std::array<u8, 0xC> reserved; | ||
| 111 | u64_le title_id; | ||
| 112 | std::array<u8, 0x8> padding; | ||
| 113 | u32_le fah_offset; | ||
| 114 | u32_le fah_size; | ||
| 115 | u32_le sac_offset; | ||
| 116 | u32_le sac_size; | ||
| 117 | u32_le kac_offset; | ||
| 118 | u32_le kac_size; | ||
| 119 | std::array<u8, 0x8> padding_2; | ||
| 120 | }; | ||
| 121 | |||
| 122 | static_assert(sizeof(AciHeader) == 0x40, "ACI0 header structure size is wrong"); | ||
| 123 | |||
| 124 | #pragma pack(push, 1) | ||
| 125 | |||
| 126 | struct FileAccessControl { | ||
| 127 | u8 version; | ||
| 128 | std::array<u8, 3> padding; | ||
| 129 | u64_le permissions; | ||
| 130 | std::array<u8, 0x20> unknown; | ||
| 131 | }; | ||
| 132 | |||
| 133 | static_assert(sizeof(FileAccessControl) == 0x2C, "FS access control structure size is wrong"); | ||
| 134 | |||
| 135 | struct FileAccessHeader { | ||
| 136 | u8 version; | ||
| 137 | std::array<u8, 3> padding; | ||
| 138 | u64_le permissions; | ||
| 139 | u32_le unk_offset; | ||
| 140 | u32_le unk_size; | ||
| 141 | u32_le unk_offset_2; | ||
| 142 | u32_le unk_size_2; | ||
| 143 | }; | ||
| 144 | |||
| 145 | static_assert(sizeof(FileAccessHeader) == 0x1C, "FS access header structure size is wrong"); | ||
| 146 | |||
| 147 | #pragma pack(pop) | ||
| 148 | |||
| 149 | Header npdm_header; | ||
| 150 | AciHeader aci_header; | ||
| 151 | AcidHeader acid_header; | ||
| 152 | |||
| 153 | FileAccessControl acid_file_access; | ||
| 154 | FileAccessHeader aci_file_access; | ||
| 155 | }; | ||
| 156 | |||
| 157 | } // namespace FileSys | ||