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