forked from John-K/pspdecrypt
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpspdecrypt.cpp
More file actions
400 lines (374 loc) · 14.9 KB
/
Copy pathpspdecrypt.cpp
File metadata and controls
400 lines (374 loc) · 14.9 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include <libgen.h>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <string.h>
#include <getopt.h>
#include "PsarDecrypter.h"
#include "PrxDecrypter.h"
#include "pspdecrypt_lib.h"
#include "common.h"
#include "Swap.h"
using namespace std;
/* Magic numbers in Big-Endian. */
static const u32 ELF_MAGIC = 0x7F454C46; /* \x7fELF */
static const u32 SCE_MAGIC = 0x7E534345; /* ~SCE */
static const u32 PSP_MAGIC = 0x7E505350; /* ~PSP */
static const u32 PSAR_MAGIC = 0x50534152; /* PSAR */
static const u32 PBP_MAGIC = 0x00504250; /* \0PBP */
static const u32 PUPK_MAGIC = 0x5055504B; /* PUPK */
static const u32 MAX_PREIPL_SIZE = 0x1000;
static void checkSkipSceHeader(u8 **ppInData, u32 size, u32 offset = 0);
static int decryptAndDecompressPrx(u8 *out, const u8 *in, u32 inSize, const u8 *secureId, bool verbose, bool decompPsp = true);
static inline u32 readMagic(const void *buf)
{
return (u32)(*(u32_be *)buf);
}
/* Get the appropriate output buffer size for a decrypted & decompressed ~PSP file. */
static size_t getPspOutputBufferCapacity(const u8 *psp)
{
/* The output buffer capacity must be at least the size of the input data
because the input data will be copied to the output buffer first,
then it will be decrypted. */
u32 size = max(pspGetElfSize(psp), pspGetPspSize(psp));
/* Align it to the AES block size ie. 16 bytes. */
return ALIGN(size, 16);
}
void help(const char* exeName) {
cout << "Usage: " << exeName << " [OPTION]... [FILE]" << endl;
cout << endl;
cout << "Decrypts encrypted PSP binaries (.PSP) or updaters (.PSAR)." << endl;
cout << "Can also take PBP files as an input, or IPL binaries if the option is given." << endl;
cout << "Skips SCE/PUPK header for ~PSP/PBP files, if any." << endl;
cout << endl;
cout << "General options:" << endl;
cout << " -h, --help display this help and exit" << endl;
cout << " -v, --verbose enable verbose mode (mostly for debugging)" << endl;
cout << " -i, --info display information about the input file and exit" << endl;
cout << " -c, --no-decomp do not decompress GZIP/KL4E/KL3E/2RLZ decrypted data" << endl;
cout << "PSP(/PBP)-only options:" << endl;
cout << " -o, --outfile=FILE output file for the decrypted binary (default: [FILE.PSP].dec)" << endl;
cout << " -s, --secureid=XSTR 16-bytes secure ID hex string (default: none)" << endl;
cout << " secure ID may only be used by PRX type 3/5/7/10" << endl;
cout << "PSAR(/PBP)-only options:" << endl;
cout << " -e, --extract-only do not decrypt files contained in the PSAR" << endl;
cout << "PBP-only options:" << endl;
cout << " -P, --psp-only only extract/decrypt the .PSP executable file of the PBP" << endl;
cout << " -A, --psar-only only extract/decrypt the .PSAR updater file of the PBP" << endl;
cout << "IPL decryption & PSAR(/PBP) options:" << endl;
cout << " -O, --outdir=DIR output path for the PSAR's or IPL's contents (default: [VER] if given [VER].PBP/PSAR)" << endl;
cout << " -i, --ipl-decrypt decrypt the IPL given as an argument" << endl;
cout << " -V, --version=VER the firmware version (eg 660) used for extracting the IPL stages" << endl;
cout << " -p, --preipl preipl image used for decrypting the later IPL stages" << endl;
cout << " -k, --keep-all also keep the intermediate .gz files of later stages" << endl;
}
int main(int argc, char *argv[]) {
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"verbose", no_argument, 0, 'v'},
{"outfile", required_argument, 0, 'o'},
{"secureid", required_argument, 0, 's'},
{"no-decomp", no_argument, 0, 'c'},
{"extract-only", no_argument, 0, 'e'},
{"outdir", required_argument, 0, 'O'},
{"ipl-decrypt", no_argument, 0, 'i'},
{"preipl", required_argument, 0, 'p'},
{"version", required_argument, 0, 'V'},
{"info", no_argument, 0, 'I'},
{"psar-only", no_argument, 0, 'A'},
{"psp-only", no_argument, 0, 'P'},
{"keep-all", no_argument, 0, 'k'},
{0, 0, 0, 0 }
};
int long_index;
string inFilename = "";
string outDir = "";
string outFile = "";
string preipl = "";
string xstr = "";
u8 secureId[16] = {0}; // optional for PRX type 3/5/7/10
bool preiplSet = false;
u8 preiplBuf[MAX_PREIPL_SIZE];
u32 preiplSize = 0;
bool verbose = false;
bool extractOnly = false;
bool decompPsp = true;
bool iplDecrypt = false;
bool infoOnly = false;
bool pspOnly = false;
bool psarOnly = false;
bool keepAll = false;
int version = -1;
int c = 0;
cout << showbase << internal << setfill('0');
cerr << showbase << internal << setfill('0');
while ((c = getopt_long(argc, argv, "hvco:s:eO:ip:V:IAP", long_options, &long_index)) != -1) {
switch (c) {
case 'h':
help(argv[0]);
return 0;
case 'v':
verbose = true;
break;
case 'c':
decompPsp = false;
break;
case 'o':
outFile = string(optarg);
break;
case 's':
xstr = string(optarg);
for (u32 i = 0, j = 0; i < xstr.length(); i += 2, j++) {
string byteStr = xstr.substr(i, 2);
secureId[j] = (u8)strtoul(byteStr.c_str(), NULL, 16);
}
break;
case 'e':
extractOnly = true;
break;
case 'O':
outDir = string(optarg);
break;
case 'i':
iplDecrypt = true;
break;
case 'p':
preipl = string(optarg);
preiplSet = true;
break;
case 'V':
version = atoi(optarg);
break;
case 'I':
infoOnly = true;
break;
case 'A':
psarOnly = true;
break;
case 'P':
pspOnly = true;
break;
case 'k':
keepAll = true;
break;
default:
help(argv[0]);
return 1;
}
}
if (optind >= argc) {
cerr << "No file specified!" << endl;
return 1;
}
if (optind + 1 < argc) {
cerr << "More than two input files specified!" << endl;
return 1;
}
inFilename = string(argv[optind]);
if (outFile == "") {
outFile = inFilename + ".dec";
}
if (outDir == "") {
size_t dotOff = inFilename.find_last_of('.');
if (dotOff == string::npos) {
outDir = inFilename + ".extr";
} else {
outDir = inFilename.substr(0, dotOff);
}
}
ifstream inFile (inFilename, ios::in|ios::binary|ios::ate);
if (!inFile.is_open()) {
cerr << "Could not open " << inFilename << "!" << endl;
return 1;
}
streampos size = inFile.tellg();
u8 *inData = new u8[size];
u8 *pInData = inData;
inFile.seekg(0, ios::beg);
inFile.read((char *)inData, size);
inFile.close();
if (size < 0x30) {
cerr << "Input file is too small!" << endl;
return 1;
}
if (preiplSet) {
ifstream preiplFile (preipl, ios::in|ios::binary|ios::ate);
if (!preiplFile.is_open()) {
cerr << "Could not open " << preipl << "!" << endl;
return 1;
}
preiplSize = preiplFile.tellg();
if (preiplSize > MAX_PREIPL_SIZE) {
cerr << "Preipl file too big!" << endl;
return 1;
}
preiplFile.seekg(0, ios::beg);
preiplFile.read((char*)preiplBuf, preiplSize);
preiplFile.close();
}
if (iplDecrypt) {
if (infoOnly) {
cerr << "No info to display for IPL..." << endl;
return 1;
}
if (version < 0) {
cerr << "You need to set --version to extract later stages of a standalone IPL." << endl;
return 1;
}
string logStr;
if (decryptIPL(inData, size, version, "ipl", outDir, preiplSet ? preiplBuf : nullptr, preiplSize, verbose, keepAll, logStr) < 0) {
cerr << "Decrypting standalone IPL" << logStr << endl;
return 1;
}
cout << "Decrypting standalone IPL" << logStr << endl;
}
else {
u32 pbpOff = 0;
ScePBPHeader *pbp = nullptr;
checkSkipSceHeader(&pInData, size);
switch (readMagic(pInData)) {
case PSP_MAGIC:
if (infoOnly) {
cout << "Input is an encrypted PSP executable encrypted with tag " << hex << setw(8) << pspGetTagVal(pInData) << endl;
}
else if (size < PSP_HEADER_SIZE) {
cerr << "Input file is too small!" << endl;
return 1;
}
else {
u8 *outData = new u8[getPspOutputBufferCapacity(pInData)];
int outSize = decryptAndDecompressPrx(outData, pInData, pspGetPspSize(pInData), secureId, true, decompPsp);
WriteFile(outFile.c_str(), outData, outSize);
delete[] outData;
}
break;
case PUPK_MAGIC:
/* Advance the input data pointer to the start of the PBP file data. */
pbpOff = (u32)(*(u32_le*)&inData[0xc]) + 0x18;
pInData += pbpOff;
size -= pbpOff;
[[fallthrough]];
case PBP_MAGIC:
{
pbp = (ScePBPHeader *)pInData;
u32 pspOff = pbp->off_data_psp;
u32 psarOff = pbp->off_data_psar;
if (infoOnly) {
cout << "Input is a PBP with:" << endl;
}
if (pspOff < size && !psarOnly) {
checkSkipSceHeader(&pInData, size, pspOff);
if (readMagic(&pInData[pspOff]) == ELF_MAGIC) {
if (infoOnly) {
cout << "- an unencrypted PSP (ELF) file" << endl;
} else {
cout << "Non-encrypted PSP file, writing to " << outFile << endl;
WriteFile(outFile.c_str(), &pInData[pspOff], psarOff - pspOff);
}
}
else if (readMagic(&pInData[pspOff]) == PSP_MAGIC) {
if (infoOnly) {
cout << "- an encrypted PSP executable encrypted with tag " << hex << setw(8) << pspGetTagVal(&pInData[pspOff]) << endl;
}
else if (psarOff - pspOff < PSP_HEADER_SIZE) {
cerr << "DATA.PSP file within the input PBP is too small!" << endl;
return 1;
}
else {
cout << "Decrypting PSP file to " << outFile << endl;
u8 *outData = new u8[getPspOutputBufferCapacity(&pInData[pspOff])];
int outSize = decryptAndDecompressPrx(outData, &pInData[pspOff], pspGetPspSize(&pInData[pspOff]), secureId, true, decompPsp);
WriteFile(outFile.c_str(), outData, outSize);
delete[] outData;
}
}
else if (infoOnly) {
cout << "- unknown DATA.PSP file data" << endl;
}
}
if (psarOff < size && !pspOnly) {
if (infoOnly) {
cout << "- a PSAR with the following characteristics:" << endl;
} else {
cout << "Extracting PSAR to " << outDir << endl;
}
pspDecryptPSAR(&pInData[psarOff], (u32)size - psarOff, outDir, extractOnly, preiplSet ? preiplBuf : nullptr, preiplSize, verbose, infoOnly, keepAll, decompPsp);
}
}
break;
case PSAR_MAGIC:
if (infoOnly) {
cout << "Input is a PSAR with the following characteristics:" << endl;
}
pspDecryptPSAR(pInData, size, outDir, extractOnly, preiplSet ? preiplBuf : nullptr, preiplSize, verbose, infoOnly, keepAll, decompPsp);
break;
case ELF_MAGIC:
if (infoOnly) {
cout << "Input is a non-encrypted PSP binary (ELF) file" << endl;
} else {
cout << "Non-encrypted file, copying to " << outFile << endl;
WriteFile(outFile.c_str(), pInData, size);
}
break;
default:
cout << "Unknown input file format!" << endl;
return 1;
}
}
delete[] inData;
return 0;
}
static void checkSkipSceHeader(u8 **ppInData, u32 size, u32 offset)
{
const SceHeader *hdr = (const SceHeader *)(&(*ppInData)[offset]);
/* Skip the useless SCE header, if any. */
if (hdr->magic == SCE_MAGIC) {
if (hdr->size < sizeof(SceHeader)) {
cerr << "Size in SCE header (" << hex << hdr->size << ") is lower than the header!" << endl;
exit(1);
}
if (hdr->size > size) {
cerr << "Size in SCE header (" << hex << hdr->size << ") points out of bounds!" << endl;
exit(1);
}
else if ((u32)size - hdr->size < 4) { /* we need at least 4 bytes for the magic */
cerr << "No input data after skipping SCE header" << endl;
exit(1);
}
*ppInData += hdr->size;
cout << "Skipped SCE header (" << hex << hdr->size << " bytes)" << endl;
}
}
static int decryptAndDecompressPrx(u8 *out, const u8 *in, u32 inSize, const u8 *secureId, bool verbose, bool decompPsp)
{
int elfSize, outSize;
elfSize = pspGetElfSize(in);
outSize = pspDecryptPRX(in, out, inSize, secureId, verbose);
if (outSize < 0) {
return outSize;
}
if (outSize >= 4 && pspIsCompressed(out)) {
if (decompPsp) {
std::string logStr;
u8 *temp = new u8[elfSize];
outSize = pspDecompress(out, outSize, temp, elfSize, logStr);
if (outSize == elfSize) {
memcpy(out, temp, elfSize);
if (verbose) {
printf("Decompression successful (%s)\n", logStr.substr(1).c_str());
}
}
else if (verbose) {
printf("Decompression failed (%s)\n", logStr.substr(1).c_str());
}
delete[] temp;
}
else if (verbose) {
printf("Skipped data decompression\n");
}
}
return outSize;
}