summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar shinyquagsire232017-10-01 10:32:43 -0600
committerGravatar shinyquagsire232017-10-01 10:53:45 -0600
commit8e10c9bb2e8690055ba07003ebd53a5215f82f8f (patch)
tree46148de876e830563d95940374bb34bb511e5f79 /src
parentfile_sys/ncch_container: add RomFS, ExeFS override to allow for backward comp... (diff)
downloadyuzu-8e10c9bb2e8690055ba07003ebd53a5215f82f8f.tar.gz
yuzu-8e10c9bb2e8690055ba07003ebd53a5215f82f8f.tar.xz
yuzu-8e10c9bb2e8690055ba07003ebd53a5215f82f8f.zip
file_sys: add class for Title Metadata (TMD)
Diffstat (limited to 'src')
-rw-r--r--src/core/CMakeLists.txt1
-rw-r--r--src/core/file_sys/title_metadata.cpp212
-rw-r--r--src/core/file_sys/title_metadata.h125
3 files changed, 338 insertions, 0 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 3ed619991..2618da18c 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -29,6 +29,7 @@ set(SRCS
29 file_sys/ncch_container.cpp 29 file_sys/ncch_container.cpp
30 file_sys/path_parser.cpp 30 file_sys/path_parser.cpp
31 file_sys/savedata_archive.cpp 31 file_sys/savedata_archive.cpp
32 file_sys/title_metadata.cpp
32 frontend/camera/blank_camera.cpp 33 frontend/camera/blank_camera.cpp
33 frontend/camera/factory.cpp 34 frontend/camera/factory.cpp
34 frontend/camera/interface.cpp 35 frontend/camera/interface.cpp
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
16namespace FileSys {
17
18static 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
34Loader::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
71Loader::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
124u64 TitleMetadata::GetTitleID() const {
125 return tmd_body.title_id;
126}
127
128u32 TitleMetadata::GetTitleType() const {
129 return tmd_body.title_type;
130}
131
132u16 TitleMetadata::GetTitleVersion() const {
133 return tmd_body.title_version;
134}
135
136u64 TitleMetadata::GetSystemVersion() const {
137 return tmd_body.system_version;
138}
139
140size_t TitleMetadata::GetContentCount() const {
141 return tmd_chunks.size();
142}
143
144u32 TitleMetadata::GetBootContentID() const {
145 return tmd_chunks[TMDContentIndex::Main].id;
146}
147
148u32 TitleMetadata::GetManualContentID() const {
149 return tmd_chunks[TMDContentIndex::Manual].id;
150}
151
152u32 TitleMetadata::GetDLPContentID() const {
153 return tmd_chunks[TMDContentIndex::DLP].id;
154}
155
156void TitleMetadata::SetTitleID(u64 title_id) {
157 tmd_body.title_id = title_id;
158}
159
160void TitleMetadata::SetTitleType(u32 type) {
161 tmd_body.title_type = type;
162}
163
164void TitleMetadata::SetTitleVersion(u16 version) {
165 tmd_body.title_version = version;
166}
167
168void TitleMetadata::SetSystemVersion(u64 version) {
169 tmd_body.system_version = version;
170}
171
172void TitleMetadata::AddContentChunk(const ContentChunk& chunk) {
173 tmd_chunks.push_back(chunk);
174}
175
176void 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
12namespace Loader {
13enum class ResultStatus;
14}
15
16////////////////////////////////////////////////////////////////////////////////////////////////////
17// FileSys namespace
18
19namespace FileSys {
20
21enum TMDSignatureType : u32 {
22 Rsa4096Sha1 = 0x10000,
23 Rsa2048Sha1 = 0x10001,
24 EllipticSha1 = 0x10002,
25 Rsa4096Sha256 = 0x10003,
26 Rsa2048Sha256 = 0x10004,
27 EcdsaSha256 = 0x10005
28};
29
30enum 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 */
43class TitleMetadata {
44public:
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
114private:
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