-
Notifications
You must be signed in to change notification settings - Fork 7
/
PfsFilesystem.cpp
167 lines (140 loc) · 4.97 KB
/
PfsFilesystem.cpp
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
#include "PfsFilesystem.h"
#include "PfsFile.h"
#include <cstring>
PfsFilesystem::PfsFilesystem(std::shared_ptr<ICryptoOperations> cryptops, std::shared_ptr<IF00DKeyEncryptor> iF00D, std::ostream& output,
const unsigned char* klicensee, const psvpfs::path& titleIdPath)
: m_cryptops(cryptops), m_iF00D(iF00D), m_output(output), m_titleIdPath(titleIdPath)
{
memcpy(m_klicensee, klicensee, 0x10);
m_filesDbParser = std::unique_ptr<FilesDbParser>(new FilesDbParser(cryptops, iF00D, output, klicensee, titleIdPath));
m_unicvDbParser = std::unique_ptr<UnicvDbParser>(new UnicvDbParser(titleIdPath, output));
m_pageMapper = std::unique_ptr<PfsPageMapper>(new PfsPageMapper(cryptops, iF00D, output, klicensee, titleIdPath));
}
int PfsFilesystem::mount()
{
if(m_filesDbParser->parse() < 0)
return -1;
if(m_unicvDbParser->parse() < 0)
return -1;
if(m_pageMapper->bruteforce_map(m_filesDbParser, m_unicvDbParser) < 0)
return -1;
return 0;
}
static void to_uppercase(std::string &str) {
std::transform(str.begin(), str.end(), str.begin(), static_cast<int (*)(int)>(std::toupper));
}
int PfsFilesystem::decrypt_files(const psvpfs::path& destTitleIdPath) const
{
const sce_ng_pfs_header_t& ngpfs = m_filesDbParser->get_header();
const std::vector<sce_ng_pfs_file_t>& files = m_filesDbParser->get_files();
const std::vector<sce_ng_pfs_dir_t>& dirs = m_filesDbParser->get_dirs();
const std::unique_ptr<sce_idb_base_t>& unicv = m_unicvDbParser->get_idatabase();
const std::map<std::uint32_t, sce_junction>& pageMap = m_pageMapper->get_pageMap();
const std::set<sce_junction>& emptyFiles = m_pageMapper->get_emptyFiles();
std::map<std::string, const sce_ng_pfs_file_t *> file_map;
for (const auto &file : files) {
std::string path = file.path().get_value().string();
to_uppercase(path);
file_map[path] = &file;
}
m_output << "Creating directories..." << std::endl;
for(auto& d : dirs)
{
if(!d.path().create_empty_directory(m_titleIdPath, destTitleIdPath))
{
m_output << "Failed to create: " << d.path() << std::endl;
return -1;
}
else
{
m_output << "Created: " << d.path() << std::endl;
}
}
m_output << "Creating empty files..." << std::endl;
for(auto& f : emptyFiles)
{
std::string path = f.get_value().string();
to_uppercase(path);
auto file = file_map.find(path);
if (file == file_map.end())
{
m_output << "Ignored: " << f << std::endl;
}
else
{
if(!f.create_empty_file(m_titleIdPath, destTitleIdPath))
{
m_output << "Failed to create: " << f << std::endl;
return -1;
}
else
{
m_output << "Created: " << f << std::endl;
}
}
}
m_output << "Decrypting files..." << std::endl;
for(auto& t : unicv->m_tables)
{
//skip empty files and directories
if(t->get_header()->get_numSectors() == 0)
continue;
//find filepath by salt (filename for icv.db or page for unicv.db)
auto map_entry = pageMap.find(t->get_icv_salt());
if(map_entry == pageMap.end())
{
m_output << "failed to find page " << t->get_icv_salt() << " in map" << std::endl;
return -1;
}
//find file in files.db by filepath
sce_junction filepath = map_entry->second;
std::string path = filepath.get_value().string();
to_uppercase(path);
auto file_ptr = file_map.find(path);
if (file_ptr == file_map.end())
{
m_output << "failed to find file " << filepath << " in flat file list" << std::endl;
return -1;
}
auto &file = file_ptr->second;
//directory and unexisting file are unexpected
if(is_directory(file->file.m_info.header.type) || is_unexisting(file->file.m_info.header.type))
{
m_output << "Unexpected file type" << std::endl;
return -1;
}
//copy unencrypted files
else if(is_unencrypted(file->file.m_info.header.type))
{
if(!filepath.copy_existing_file(m_titleIdPath, destTitleIdPath, file->file.m_info.header.size))
{
m_output << "Failed to copy: " << filepath << std::endl;
return -1;
}
else
{
m_output << "Copied: " << filepath << std::endl;
}
}
//decrypt encrypted files
else if(is_encrypted(file->file.m_info.header.type))
{
PfsFile pfsFile(m_cryptops, m_iF00D, m_output, m_klicensee, m_titleIdPath, *file, filepath, ngpfs, t);
if(pfsFile.decrypt_file(destTitleIdPath) < 0)
{
m_output << "Failed to decrypt: " << filepath << std::endl;
return -1;
}
else
{
m_output << "Decrypted: " << filepath << std::endl;
}
}
else
{
m_output << "Unexpected file type" << std::endl;
return -1;
}
}
return 0;
}