-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFilesDbParser.h
272 lines (208 loc) · 7.37 KB
/
FilesDbParser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#pragma once
//very basics of the format can be found here
//http://www.vitadevwiki.com/index.php?title=Files.db
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdint>
#include <algorithm>
#include <map>
#include <iomanip>
#include <memory>
#include "Utils.h"
#include "FlagOperations.h"
#include "IF00DKeyEncryptor.h"
#include "ICryptoOperations.h"
#pragma pack(push, 1)
#define MAGIC_WORD "SCENGPFS"
#define MAX_FILES_IN_BLOCK 9
#define EXPECTED_BLOCK_SIZE 0x400
#define FILES_EXPECTED_VERSION_3 3
#define FILES_EXPECTED_VERSION_4 4 //looks like files.db salt appeared in this version. before that it was 0
#define FILES_EXPECTED_VERSION_5 5
struct sce_ng_pfs_header_t
{
std::uint8_t magic[8];
std::uint32_t version;
std::uint16_t image_spec; // allows to distinguish unicv.db and icv.db - check is_unicv_to_img_type
std::uint16_t key_id;
std::uint32_t pageSize;
std::uint32_t bt_order; // order value of the binary tree - derived from btree_order
std::uint32_t root_icv_page_number; // derived from off2pgn or btree_top
std::uint32_t files_salt; // first salt value used for key derrivation
std::uint64_t unk6; // is 0xFFFFFFFFFFFFFFFF or rarely may have other unknown value
std::uint64_t tailSize; // size of data after this header
std::uint64_t total_sz; // is 0
std::uint8_t root_icv[0x14]; // 0x38 hmac-sha1 of (pageSize - 4) of page (pointed by root_icv_page_number) with secret derived from klicensee
std::uint8_t header_icv[0x14]; // 0x4C hmac-sha1 of 0x16 bytes of header with secret derived from klicensee
std::uint8_t rsa_sig0[0x100];
std::uint8_t rsa_sig1[0x100];
std::uint8_t padding[0x1A0];
};
//still have to figure out. not quite clear
enum sce_ng_pfs_block_types : std::uint32_t
{
child = 0,
root = 1 // if page number is -1 then this should be root. otherwise - unknown
};
struct sce_ng_pfs_block_header_t
{
std::uint32_t parent_page_number;
sce_ng_pfs_block_types type;
std::uint32_t nFiles;
std::uint32_t padding; // probably padding ? always 0
};
//there can be 9 files at max in one block
struct sce_ng_pfs_file_header_t
{
std::uint32_t index; //parent index
std::uint8_t fileName[68];
};
enum sce_ng_pfs_file_types : std::uint16_t
{
unexisting = ATTR_RW_OR_NONE, //(0x0000)
normal_file = ATTR_RO, //(0x0001)
normal_directory = ATTR_DIR, //(0x8000)
sys_directory = ATTR_DIR | ATTR_SYS1 | ATTR_SYS2, //(0x8006)
unencrypted_system_file_rw = ATTR_NENC | ATTR_SYS1 | ATTR_SYS2, //(0x4006)
encrypted_system_file_rw = ATTR_SYS1 | ATTR_SYS2, //(0x0006)
unencrypted_system_file_ro = ATTR_NENC | ATTR_SYS1 | ATTR_SYS2 | ATTR_RO, //(0x4007)
encrypted_system_file_ro = ATTR_SYS1 | ATTR_SYS2 | ATTR_RO, //(0x0007)
acid_directory = ATTR_DIR | ATTR_AC | ATTR_SYS2, //(0x9004) encountered in ADDCONT
};
#define INVALID_FILE_INDEX 0xFFFFFFFF
struct sce_ng_pfs_file_info_t
{
std::uint32_t idx; // this file index. can be INVALID_FILE_INDEX
sce_ng_pfs_file_types type;
std::uint16_t padding0; //probably padding ? always 0
std::uint32_t size;
std::uint32_t padding1; //probably padding ? always 0
};
struct sce_ng_pfs_file_info_proxy_t
{
sce_ng_pfs_file_info_t header;
sce_ng_pfs_file_types original_type;
bool hasFixedType;
sce_ng_pfs_file_info_proxy_t()
: hasFixedType(false)
{
}
sce_ng_pfs_file_types get_original_type() const
{
if(hasFixedType)
return original_type;
else
return header.type;
}
};
struct sce_ng_pfs_hash_t
{
std::uint8_t data[20];
};
struct sce_ng_pfs_block_t
{
sce_ng_pfs_block_header_t header; //size = 16
std::vector<sce_ng_pfs_file_header_t> files; // size = 72 * 9 = 648
//infos may contain non INVALID_FILE_INDEX as last element
//still dont know the purpose of this
std::vector<sce_ng_pfs_file_info_proxy_t> m_infos; // size = 16 * 10 = 160
std::vector<sce_ng_pfs_hash_t> hashes; // size = 20 * 10 = 200
std::uint32_t page;
};
struct sce_ng_pfs_flat_block_t
{
sce_ng_pfs_block_header_t header;
sce_ng_pfs_file_header_t file;
sce_ng_pfs_file_info_proxy_t m_info;
sce_ng_pfs_hash_t hash;
};
struct sce_ng_pfs_file_t
{
private:
sce_junction m_path;
public:
sce_ng_pfs_flat_block_t file;
std::vector<sce_ng_pfs_flat_block_t> dirs;
sce_ng_pfs_file_t(const sce_junction& p)
: m_path(p)
{
}
public:
const sce_junction& path() const
{
return m_path;
}
};
struct sce_ng_pfs_dir_t
{
private:
sce_junction m_path;
public:
sce_ng_pfs_flat_block_t dir;
std::vector<sce_ng_pfs_flat_block_t> dirs;
sce_ng_pfs_dir_t(const sce_junction& p)
: m_path(p)
{
}
public:
const sce_junction& path() const
{
return m_path;
}
};
#pragma pack(pop)
bool is_directory(sce_ng_pfs_file_types type);
bool is_valid_file_type(sce_ng_pfs_file_types type);
bool is_encrypted(sce_ng_pfs_file_types type);
bool is_unencrypted(sce_ng_pfs_file_types type);
bool is_unexisting(sce_ng_pfs_file_types type);
class FilesDbParser
{
private:
std::shared_ptr<ICryptoOperations> m_cryptops;
std::shared_ptr<IF00DKeyEncryptor> m_iF00D;
std::ostream& m_output;
unsigned char m_klicensee[0x10];
psvpfs::path m_titleIdPath;
private:
sce_ng_pfs_header_t m_header;
std::vector<sce_ng_pfs_file_t> m_files;
std::vector<sce_ng_pfs_dir_t> m_dirs;
public:
FilesDbParser(std::shared_ptr<ICryptoOperations> cryptops, std::shared_ptr<IF00DKeyEncryptor> iF00D, std::ostream& output,
const unsigned char* klicensee, psvpfs::path titleIdPath);
private:
bool verify_header_icv(std::ifstream& inputStream, const unsigned char* secret);
bool get_isUnicv(bool& isUnicv);
bool validate_header(uint32_t dataSize);
bool parseFilesDb(std::ifstream& inputStream, std::vector<sce_ng_pfs_block_t>& blocks);
private:
bool constructDirmatrix(const std::vector<sce_ng_pfs_block_t>& blocks, std::map<std::uint32_t, std::uint32_t>& dirMatrix);
bool constructFileMatrix(std::vector<sce_ng_pfs_block_t>& blocks, std::map<std::uint32_t, std::uint32_t>& fileMatrix);
bool flattenBlocks(const std::vector<sce_ng_pfs_block_t>& blocks, std::vector<sce_ng_pfs_flat_block_t>& flatBlocks);
const std::vector<sce_ng_pfs_flat_block_t>::const_iterator findFlatBlockDir(const std::vector<sce_ng_pfs_flat_block_t>& flatBlocks, std::uint32_t index);
const std::vector<sce_ng_pfs_flat_block_t>::const_iterator findFlatBlockFile(const std::vector<sce_ng_pfs_flat_block_t>& flatBlocks, std::uint32_t index);
bool constructDirPaths(const std::map<std::uint32_t, std::uint32_t>& dirMatrix, const std::vector<sce_ng_pfs_flat_block_t>& flatBlocks);
bool constructFilePaths(const std::map<std::uint32_t, std::uint32_t>& dirMatrix, const std::map<std::uint32_t, std::uint32_t>& fileMatrix, const std::vector<sce_ng_pfs_flat_block_t>& flatBlocks);
private:
bool linkDirpaths(const std::set<psvpfs::path>& real_directories);
bool linkFilepaths(const std::set<psvpfs::path>& real_files, std::uint32_t fileSectorSize);
int matchFileLists(const std::set<psvpfs::path>& files);
public:
int parse();
public:
const sce_ng_pfs_header_t& get_header() const
{
return m_header;
}
const std::vector<sce_ng_pfs_file_t>& get_files() const
{
return m_files;
}
const std::vector<sce_ng_pfs_dir_t>& get_dirs() const
{
return m_dirs;
}
};