-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathpup.c
637 lines (513 loc) · 15.2 KB
/
pup.c
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
* pup.c -- PS3 PUP update file extractor/creator
*
* Copyright (C) Youness Alaoui (KaKaRoTo)
*
* This software is distributed under the terms of the GNU General Public
* License ("GPL") version 3, as published by the Free Software Foundation.
*
*/
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#include <arpa/inet.h>
#include <string.h>
#include "sha1.h"
#define VERSION "0.2"
#define PUP_MAGIC (uint64_t) 0x5343455546000000 /* "SCEUF\0\0\0" */
static const uint8_t hmac_pup_key[] = {
0xf4, 0x91, 0xad, 0x94, 0xc6, 0x81, 0x10, 0x96,
0x91, 0x5f, 0xd5, 0xd2, 0x44, 0x81, 0xae, 0xdc,
0xed, 0xed, 0xbe, 0x6b, 0xe5, 0x13, 0x72, 0x4d,
0xd8, 0xf7, 0xb6, 0x91, 0xe8, 0x8a, 0x38, 0xf4,
0xb5, 0x16, 0x2b, 0xfb, 0xec, 0xbe, 0x3a, 0x62,
0x18, 0x5d, 0xd7, 0xc9, 0x4d, 0xa2, 0x22, 0x5a,
0xda, 0x3f, 0xbf, 0xce, 0x55, 0x5b, 0x9e, 0xa9,
0x64, 0x98, 0x29, 0xeb, 0x30, 0xce, 0x83, 0x66
};
typedef struct {
uint64_t magic;
uint64_t package_version;
uint64_t image_version;
uint64_t file_count;
uint64_t header_length;
uint64_t data_length;
} PUPHeader;
typedef struct {
uint64_t entry_id;
uint64_t data_offset;
uint64_t data_length;
uint8_t padding[8];
} PUPFileEntry;
typedef struct {
uint64_t entry_id;
uint8_t hash[20];
uint8_t padding[4];
} PUPHashEntry;
typedef struct
{
uint8_t hash[20];
uint8_t padding[12];
} PUPFooter;
#define ntohll(x) (((uint64_t) ntohl (x) << 32) | (uint64_t) ntohl (x >> 32) )
#define htonll(x) (((uint64_t) htonl (x) << 32) | (uint64_t) htonl (x >> 32) )
static void usage (const char *program)
{
fprintf (stderr, "Usage:\n\t%s <command> <options>\n\n"
"Commands/Options:\n"
"\ti <filename.pup>:\t\t\t\t\tInformation about the PUP file\n"
"\tx <filename.pup> <output directory>:\t\t\tExtract PUP file\n"
"\tc <input directory> <filename.pup> <build number>:\tCreate PUP file\n\n", program);
exit (-1);
}
typedef struct {
uint64_t id;
const char *filename;
} PUPEntryID;
static const PUPEntryID entries[] = {
{0x100, "version.txt"},
{0x101, "license.xml"},
{0x102, "promo_flags.txt"},
{0x103, "update_flags.txt"},
{0x104, "patch_build.txt"},
{0x200, "ps3swu.self"},
{0x201, "vsh.tar"},
{0x202, "dots.txt"},
{0x203, "patch_data.pkg"},
{0x300, "update_files.tar"},
{0, NULL}
};
static const char *id_to_filename (uint64_t entry_id)
{
const PUPEntryID *entry = entries;
while (entry->id) {
if (entry->id == entry_id)
return entry->filename;
entry++;
}
return NULL;
}
static void print_hash (const char *message, uint8_t hash[20])
{
int i;
printf ("%s : ", message);
for (i = 0; i < 20; i++) {
printf ("%.2X", hash[i]);
}
printf ("\n");
}
static void print_header_info (PUPHeader *header, PUPFooter *footer)
{
printf ("PUP file information\n"
"Package version: %llu\n"
"Image version: %llu\n"
"File count: %llu\n"
"Header length: %llu\n"
"Data length: %llu\n",
header->package_version, header->image_version,
header->file_count, header->header_length, header->data_length);
print_hash ("PUP file hash", footer->hash);
}
static void print_file_info ( PUPFileEntry *file, PUPHashEntry *hash)
{
const char *filename = NULL;
filename = id_to_filename (file->entry_id);
printf ("\tFile %d\n"
"\tEntry id: 0x%X\n"
"\tFilename : %s\n"
"\tData offset: 0x%X\n"
"\tData length: %llu\n",
(uint32_t) hash->entry_id, (uint32_t) file->entry_id,
filename ? filename : "Unknown entry id",
(uint32_t) file->data_offset, file->data_length);
print_hash ("File hash", hash->hash);
}
static int read_header (FILE *fd, PUPHeader *header,
PUPFileEntry **files, PUPHashEntry **hashes, PUPFooter *footer)
{
PUPHeader orig_header;
HMAC_CTX context;
uint8_t hash[SHA1_MAC_LEN];
int read;
unsigned int i;
*files = NULL;
*hashes = NULL;
read = fread (&orig_header, sizeof(PUPHeader), 1, fd);
if (read < 1) {
perror ("Couldn't read header");
goto error;
}
header->magic = ntohll(orig_header.magic);
header->package_version = ntohll(orig_header.package_version);
header->image_version = ntohll(orig_header.image_version);
header->file_count = ntohll(orig_header.file_count);
header->header_length = ntohll(orig_header.header_length);
header->data_length = ntohll(orig_header.data_length);
if (header->magic != PUP_MAGIC) {
fprintf (stderr, "Magic number is not the same 0x%X%X\n",
(uint32_t) (header->magic >> 32), (uint32_t) header->magic);
goto error;
}
*files = malloc (header->file_count * sizeof(PUPFileEntry));
*hashes = malloc (header->file_count * sizeof(PUPHashEntry));
read = fread (*files, sizeof(PUPFileEntry), header->file_count, fd);
if (read < 0 || (uint) read < header->file_count) {
perror ("Couldn't read file entries");
goto error;
}
read = fread (*hashes, sizeof(PUPHashEntry), header->file_count, fd);
if (read < 0 || (uint) read < header->file_count) {
perror ("Couldn't read hash entries");
goto error;
}
read = fread (footer, sizeof(PUPFooter), 1, fd);
if (read < 1) {
perror ("Couldn't read footer");
goto error;
}
HMACInit (&context, hmac_pup_key, sizeof(hmac_pup_key));
HMACUpdate (&context, &orig_header, sizeof(PUPHeader));
HMACUpdate (&context, *files, header->file_count * sizeof(PUPFileEntry));
HMACUpdate (&context, *hashes, header->file_count * sizeof(PUPHashEntry));
HMACFinal (hash, &context);
if (memcmp (hash, footer->hash, SHA1_MAC_LEN) != 0) {
fprintf (stderr, "PUP file is corrupted, wrong header hash\n\n");
print_hash ("Header hash", hash);
print_hash ("Expected hash", footer->hash);
goto error;
}
for (i = 0; i < header->file_count; i++) {
(*files)[i].entry_id = ntohll ((*files)[i].entry_id);
(*files)[i].data_offset = ntohll ((*files)[i].data_offset);
(*files)[i].data_length = ntohll ((*files)[i].data_length);
(*hashes)[i].entry_id = ntohll ((*hashes)[i].entry_id);
}
return 1;
error:
if (*files)
free (*files);
*files = NULL;
if (*hashes)
free (*hashes);
*hashes = NULL;
return 0;
}
static void info (const char *file)
{
FILE *fd = NULL;
int i;
PUPHeader header;
PUPFooter footer;
PUPFileEntry *files = NULL;
PUPHashEntry *hashes = NULL;
fd = fopen (file, "rb");
if (fd == NULL) {
perror ("Error opening input file");
exit (-2);
}
if (read_header (fd, &header, &files, &hashes, &footer) == 0)
goto error;
print_header_info (&header, &footer);
for (i = 0; (uint) i < header.file_count; i++)
print_file_info (&files[i], &hashes[i]);
fclose (fd);
free (files);
free (hashes);
return;
error:
fclose (fd);
exit (-2);
}
static void extract (const char *file, const char *dest)
{
FILE *fd = NULL;
FILE *out = NULL;
int read;
int written;
int i;
PUPHeader header;
PUPFooter footer;
PUPFileEntry *files = NULL;
PUPHashEntry *hashes = NULL;
char filename[PATH_MAX+1];
char buffer[1024];
HMAC_CTX context;
uint8_t hash[SHA1_MAC_LEN];
struct stat stat_buf;
if (stat (dest, &stat_buf) == 0) {
fprintf (stderr, "Destination directory must not exist\n");
goto error;
}
fd = fopen (file, "rb");
if (fd == NULL) {
perror ("Error opening input file");
goto error;
}
if (read_header (fd, &header, &files, &hashes, &footer) == 0)
goto error;
print_header_info (&header, &footer);
if (mkdir (dest, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
perror ("Couldn't create output directory");
goto error;
}
for (i = 0; (uint) i < header.file_count; i++) {
const char *file = NULL;
unsigned int len;
print_file_info (&files[i], &hashes[i]);
file = id_to_filename (files[i].entry_id);
if (file == NULL) {
printf ("*** Unknown entry id, file skipped ****\n\n");
continue;
}
sprintf (filename, "%s/%s", dest, file);
printf ("Writing file %s\n", filename);
out = fopen (filename, "wb");
if (out == NULL) {
perror ("Could not open output file");
goto error;
}
fseek (fd, files[i].data_offset, SEEK_SET);
HMACInit (&context, hmac_pup_key, sizeof(hmac_pup_key));
do {
len = files[i].data_length;
if (len > sizeof(buffer))
len = sizeof(buffer);
read = fread (buffer, 1, len, fd);
if (read < 0 || (uint) read < len) {
perror ("Couldn't read all the data");
goto error;
}
HMACUpdate (&context, buffer, len);
written = fwrite (buffer, 1, len, out);
if (written < 0 || (uint) written < len) {
perror ("Couldn't write all the data");
goto error;
}
files[i].data_length -= len;
} while (files[i].data_length > 0);
HMACFinal (hash, &context);
fclose (out);
out = NULL;
if (memcmp (hash, hashes[i].hash, SHA1_MAC_LEN) != 0) {
fprintf (stderr, "PUP file is corrupted, wrong file hash\n\n");
print_hash ("File hash", hash);
print_hash ("Expected hash", hashes[i].hash);
goto error;
}
}
if (out)
fclose (out);
fclose (fd);
free (files);
free (hashes);
return;
error:
if (fd)
fclose (fd);
if (out)
fclose (out);
if (files)
free (files);
if (hashes)
free (hashes);
exit (-2);
}
static void create (const char *directory, const char *dest, long build)
{
FILE *fd = NULL;
FILE *out = NULL;
int read;
int written;
unsigned int i;
PUPHeader orig_header;
PUPHeader header;
PUPFooter footer;
PUPFileEntry *files = NULL;
PUPHashEntry *hashes = NULL;
char filename[PATH_MAX+1];
char buffer[1024];
HMAC_CTX context;
struct stat stat_buf;
const PUPEntryID *entry = entries;
if (stat (dest, &stat_buf) == 0) {
fprintf (stderr, "Destination file must not exist\n");
goto error;
}
out = fopen (dest, "wb");
if (out == NULL) {
perror ("Could not open output file");
goto error;
}
memset (&header, 0, sizeof(PUPHeader));
memset (&footer, 0, sizeof(PUPHeader));
header.magic = PUP_MAGIC;
header.package_version = 1;
header.image_version = build;
header.header_length = sizeof(PUPHeader) + sizeof(PUPFooter);
while (entry->id) {
HMAC_CTX context;
PUPFileEntry *file = NULL;
PUPHashEntry *hash = NULL;
sprintf (filename, "%s/%s", directory, entry->filename);
fd = fopen (filename, "rb");
if (fd == NULL) {
entry++;
continue;
}
printf ("Found file %s\n", filename);
header.file_count++;
header.header_length += sizeof(PUPFileEntry) + sizeof(PUPHashEntry);
files = realloc (files, sizeof(PUPFileEntry) * header.file_count);
hashes = realloc (hashes, sizeof(PUPHashEntry) * header.file_count);
file = &files[header.file_count - 1];
memset (file, 0, sizeof(PUPFileEntry));
hash = &hashes[header.file_count - 1];
memset (hash, 0, sizeof(PUPHashEntry));
hash->entry_id = header.file_count - 1;
file->entry_id = entry->id;
entry++;
HMACInit (&context, hmac_pup_key, sizeof(hmac_pup_key));
do {
read = fread (buffer, 1, sizeof(buffer), fd);
if (read < 0)
break;
HMACUpdate (&context, buffer, read);
file->data_length += read;
} while (!feof (fd));
HMACFinal (hash->hash, &context);
header.data_length += file->data_length;
}
for (i = 0; i < header.file_count; i++) {
if (i == 0)
files[i].data_offset = header.header_length;
else
files[i].data_offset = files[i-1].data_offset + files[i-1].data_length;
}
orig_header.magic = htonll (header.magic);
orig_header.package_version = htonll (header.package_version);
orig_header.image_version = htonll (header.image_version);
orig_header.file_count = htonll (header.file_count);
orig_header.header_length = htonll (header.header_length);
orig_header.data_length = htonll (header.data_length);
for (i = 0; i < header.file_count; i++) {
files[i].entry_id = htonll (files[i].entry_id);
files[i].data_offset = htonll (files[i].data_offset);
files[i].data_length = htonll (files[i].data_length);
hashes[i].entry_id = htonll (hashes[i].entry_id);
}
HMACInit (&context, hmac_pup_key, sizeof(hmac_pup_key));
HMACUpdate (&context, &orig_header, sizeof(PUPHeader));
HMACUpdate (&context, files, header.file_count * sizeof(PUPFileEntry));
HMACUpdate (&context, hashes, header.file_count * sizeof(PUPHashEntry));
HMACFinal (footer.hash, &context);
print_header_info (&header, &footer);
written = fwrite (&orig_header, sizeof(PUPHeader), 1, out);
if (written < 0 || written < 1) {
perror ("Error writing header");
goto error;
}
written = fwrite (files, sizeof(PUPFileEntry), header.file_count, out);
if (written < 0 || (unsigned int) written < header.file_count) {
perror ("Error writing files header");
goto error;
}
written = fwrite (hashes, sizeof(PUPHashEntry), header.file_count, out);
if (written < 0 || (unsigned int) written < header.file_count) {
perror ("Error writing hashes header");
goto error;
}
written = fwrite (&footer, sizeof(PUPFooter), 1, out);
if (written < 0 || written < 1) {
perror ("Error writing footer");
goto error;
}
for (i = 0; i < header.file_count; i++) {
files[i].entry_id = ntohll (files[i].entry_id);
files[i].data_offset = ntohll (files[i].data_offset);
files[i].data_length = ntohll (files[i].data_length);
hashes[i].entry_id = ntohll (hashes[i].entry_id);
}
for (i = 0; i < header.file_count; i++) {
const char *file = NULL;
unsigned int len;
print_file_info (&files[i], &hashes[i]);
file = id_to_filename (files[i].entry_id);
if (file == NULL) {
printf ("*** Unknown entry id, file skipped ****\n\n");
continue;
}
sprintf (filename, "%s/%s", directory, file);
fd = fopen (filename, "rb");
if (fd == NULL) {
perror ("Could not open input file");
goto error;
}
fseek (out, files[i].data_offset, SEEK_SET);
do {
len = files[i].data_length;
if (len > sizeof(buffer))
len = sizeof(buffer);
read = fread (buffer, 1, len, fd);
if (read < 0 || (uint) read < len) {
perror ("Couldn't read all the data");
goto error;
}
written = fwrite (buffer, 1, len, out);
if (written < 0 || (uint) written < len) {
perror ("Couldn't write all the data");
goto error;
}
files[i].data_length -= len;
} while (files[i].data_length > 0);
fclose (fd);
fd = NULL;
}
if (fd)
fclose (fd);
fclose (out);
free (files);
free (hashes);
return;
error:
if (fd)
fclose (fd);
if (out)
fclose (out);
if (files)
free (files);
if (hashes)
free (hashes);
exit (-2);
}
int main (int argc, char *argv[])
{
fprintf (stderr, "PUP Creator/Extractor %s\nBy KaKaRoTo\n\n", VERSION);
if (argc < 2)
usage (argv[0]);
if (argv[1][1] != '\0')
usage (argv[0]);
switch (argv[1][0]) {
case 'i':
if (argc != 3)
usage (argv[0]);
info (argv[2]);
break;
case 'e':
case 'x':
if (argc != 4)
usage (argv[0]);
extract (argv[2], argv[3]);
break;
case 'c':
if (argc != 5)
usage (argv[0]);
create (argv[2], argv[3], atol (argv[4]));
break;
default:
usage (argv[0]);
}
return 0;
}