forked from idc/ps4-pup_unpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
432 lines (387 loc) · 16.2 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using NDesk.Options;
namespace pup_unpack
{
internal class Program
{
private static string GetExecutableName()
{
return Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location);
}
public static void Main(string[] args)
{
bool showHelp = false;
bool saveTables = false;
var options = new OptionSet()
{
{ "save-tables", "save tables", v => saveTables = v != null },
{ "h|help", "show this message and exit", v => showHelp = v != null },
};
List<string> extras;
try
{
extras = options.Parse(args);
}
catch (OptionException e)
{
Console.Write("{0}: ", GetExecutableName());
Console.WriteLine(e.Message);
Console.WriteLine("Try `{0} --help' for more information.", GetExecutableName());
return;
}
if (extras.Count < 1 || extras.Count > 2 || showHelp == true)
{
Console.WriteLine("Usage: {0} [OPTIONS]+ input_dec [output_dir]", GetExecutableName());
Console.WriteLine();
Console.WriteLine("Options:");
options.WriteOptionDescriptions(Console.Out);
return;
}
string inputPath = extras[0];
string baseOutputPath = extras.Count > 1 ? extras[1] : Path.ChangeExtension(inputPath, null) + "_unpack";
var fileNames = new Dictionary<uint, string>()
{
{ 3, "wlan_firmware.bin" },
{ 5, "secure_modules.bin" },
{ 6, Path.Combine("fs", "system.img" ) },
{ 8, Path.Combine("fs", "eap.img") },
{ 9, Path.Combine("fs", "recovery.img") },
{ 11, Path.Combine("fs", "preinst.img") },
{ 12, Path.Combine("fs", "system_ex.img") },
{ 34, "torus2_firmware.bin" },
{ 257, "eula.xml" },
{ 512, "orbis_swu.elf" },
{ 514, "orbis_swu.self" },
{ 3337, "cp_firmware.bin" },
};
var deviceNames = new Dictionary<uint, string>()
{
{ 1, "/dev/sflash0s0x32b" },
{ 13, "/dev/sflash0s0x32b" },
{ 32, "/dev/sflash0s0x32b" },
{ 36, "/dev/sflash0s0x32b" },
{ 40, "/dev/sflash0s0x32b" },
{ 42, "/dev/sflash0s0x32b" },
{ 44, "/dev/sflash0s0x32b" },
{ 46, "/dev/sflash0s0x32b" },
{ 2, "/dev/sflash0s0x33" },
{ 14, "/dev/sflash0s0x33" },
{ 33, "/dev/sflash0s0x33" },
{ 37, "/dev/sflash0s0x33" },
{ 43, "/dev/sflash0s0x33" },
{ 3, "/dev/sflash0s0x38" },
{ 34, "/dev/sflash0s0x38" },
{ 48, "/dev/sflash0s0x38" },
{ 4, "/dev/sflash0s1.cryptx2b" },
{ 35, "/dev/sflash0s1.cryptx2b" },
{ 38, "/dev/sflash0s1.cryptx2b" },
{ 39, "/dev/sflash0s1.cryptx2b" },
{ 45, "/dev/sflash0s1.cryptx2b" },
{ 5, "/dev/sflash0s1.cryptx3b" },
{ 10, "/dev/sflash0s1.cryptx40" },
{ 9, "/dev/da0x0.crypt" },
{ 11, "/dev/da0x1.crypt" },
{ 7, "/dev/da0x2" },
{ 8, "/dev/da0x3.crypt" },
{ 6, "/dev/da0x4b.crypt" },
{ 12, "/dev/da0x5b.crypt" },
{ 3328, "/dev/sc_fw_update0" },
{ 3336, "/dev/sc_fw_update0" },
{ 3335, "/dev/sc_fw_update0" },
{ 3329, "cd0" },
{ 3330, "da0" },
{ 16, "/dev/sbram0" },
{ 17, "/dev/sbram0" },
{ 19, "/dev/sbram0" },
{ 20, "/dev/sbram0" },
{ 21, "/dev/sbram0" },
{ 22, "/dev/sbram0" },
{ 18, "/dev/sbram0" },
{ 3337, "cpfirm" },
{ 15, "test" },
{ 769, "/update" },
{ 770, "/update" },
{ 782, "test" },
{ 783, "test" },
};
using (var input = File.OpenRead(inputPath))
{
var reader = new BinaryReader(input);
var magic = reader.ReadUInt32();
var unknown04 = reader.ReadUInt32();
var unknown08 = reader.ReadUInt16();
var flags = reader.ReadByte();
var unknown0B = reader.ReadByte();
var headerSize = reader.ReadUInt16();
var hashSize = reader.ReadUInt16();
var fileSize = reader.ReadInt64();
var entryCount = reader.ReadUInt16();
var hashCount = reader.ReadUInt16();
var unknown1C = reader.ReadUInt32();
var entries = new Entry[entryCount];
for (int i = 0; i < entryCount; i++)
{
Entry entry;
entry.Flags = reader.ReadUInt32();
input.Seek(4, SeekOrigin.Current);
entry.Offset = reader.ReadInt64();
entry.CompressedSize = reader.ReadInt64();
entry.UncompressedSize = reader.ReadInt64();
entries[i] = entry;
}
var tableEntries = new int[entryCount];
for (int i = 0; i < entryCount; i++)
{
tableEntries[i] = -2;
}
for (int i = 0; i < entryCount; i++)
{
var entry = entries[i];
if (entry.IsBlocked == false)
{
tableEntries[i] = -2;
}
else
{
if (((entry.Id | 0x100) & 0xF00) == 0xF00)
{
throw new InvalidOperationException();
}
int tableIndex = -1;
for (int j = 0; j < entryCount; j++)
{
if ((entries[j].Flags & 1) != 0)
{
if (entries[j].Id == i)
{
tableIndex = j;
break;
}
}
}
if (tableIndex < 0)
{
throw new InvalidOperationException();
}
if (tableEntries[tableIndex] != -2)
{
throw new InvalidOperationException();
}
tableEntries[tableIndex] = i;
}
}
for (int i = 0; i < entries.Length; i++)
{
var entry = entries[i];
var special = entry.Flags & 0xF0000000;
if (special == 0xE0000000 || special == 0xF0000000)
{
continue;
}
string name;
if (tableEntries[i] < 0)
{
if (fileNames.TryGetValue(entry.Id, out name) == true)
{
}
else if (deviceNames.TryGetValue(entry.Id, out name) == true)
{
var index = name.LastIndexOf('/');
name = name.Substring(index + 1);
name = entry.Id + "__" + name + ".bin";
name = Path.Combine("devices", name);
}
else
{
name = entry.Id + ".bin";
name = Path.Combine("unknown", name);
}
}
else
{
if (saveTables == false)
{
continue;
}
name = string.Format("{0} for {1}.bin", entry.Id, entries[tableEntries[i]].Id);
name = Path.Combine("tables", name);
}
var outputPath = Path.Combine(baseOutputPath, name);
var outputParentPath = Path.GetDirectoryName(outputPath);
if (string.IsNullOrEmpty(outputParentPath) == false)
{
Directory.CreateDirectory(outputParentPath);
}
using (var output = File.Create(outputPath))
{
ExtractEntry(input, i, entry, entries, output);
}
}
}
}
private static void CopyStream(Stream input, Stream output, long size)
{
long left = size;
var data = new byte[1 * 1024 * 1024];
while (left > 0)
{
var block = (int)(Math.Min(left, data.Length));
var read = input.Read(data, 0, block);
if (read != block)
{
throw new EndOfStreamException();
}
output.Write(data, 0, block);
left -= block;
}
}
private static void ExtractEntry(Stream input, int index, Entry entry, Entry[] entries, Stream output)
{
if (entry.IsBlocked == false)
{
input.Position = entry.Offset;
if (entry.IsCompressed == false)
{
CopyStream(input, output, entry.CompressedSize);
}
else
{
var zlib = new InflaterInputStream(input);
CopyStream(zlib, output, entry.UncompressedSize);
}
}
else
{
if (((entry.Id | 0x100) & 0xF00) == 0xF00)
{
throw new InvalidOperationException();
}
int tableIndex = -1;
for (int j = 0; j < entries.Length; j++)
{
if ((entries[j].Flags & 1) != 0)
{
if (entries[j].Id == index)
{
tableIndex = j;
break;
}
}
}
if (tableIndex < 0)
{
throw new InvalidOperationException();
}
var tableEntry = entries[tableIndex];
var blockSize = 1u << (int)(((entry.Flags & 0xF000) >> 12) + 12);
var blockCount = (blockSize + entry.UncompressedSize - 1) / blockSize;
var tailSize = entry.UncompressedSize % blockSize;
if (tailSize == 0)
{
tailSize = blockSize;
}
BlockInfo[] blockInfos = null;
if (entry.IsCompressed == true)
{
blockInfos = new BlockInfo[blockCount];
using (var tableData = new MemoryStream())
{
input.Position = tableEntry.Offset;
if (tableEntry.IsCompressed == false)
{
CopyStream(input, tableData, tableEntry.CompressedSize);
}
else
{
var zlib = new InflaterInputStream(input);
CopyStream(zlib, tableData, tableEntry.UncompressedSize);
}
tableData.Position = 32 * blockCount;
var tableReader = new BinaryReader(tableData);
for (int j = 0; j < blockCount; j++)
{
BlockInfo blockInfo;
blockInfo.Offset = tableReader.ReadUInt32();
blockInfo.Size = tableReader.ReadUInt32();
blockInfos[j] = blockInfo;
}
}
}
var buffer = new byte[blockSize];
input.Position = entry.Offset;
var compressedRemainingSize = entry.CompressedSize;
var uncompressedRemainingSize = entry.UncompressedSize;
var lastIndex = blockCount - 1;
for (int i = 0; i < blockCount; i++)
{
long compressedReadSize, uncompressedReadSize;
bool blockIsCompressed = false;
if (entry.IsCompressed == true)
{
var blockInfo = blockInfos[i];
var unpaddedSize = (blockInfo.Size & ~0xFu) - (blockInfo.Size & 0xFu);
compressedReadSize = blockSize;
if (unpaddedSize != blockSize)
{
compressedReadSize = blockInfo.Size;
if (i != lastIndex || tailSize != blockInfo.Size)
{
compressedReadSize &= ~0xFu;
blockIsCompressed = true;
}
}
if (blockInfo.Offset != 0)
{
var blockOffset = entry.Offset + blockInfo.Offset;
input.Position = blockOffset;
output.Position = i * blockSize;
}
}
else
{
compressedReadSize = compressedRemainingSize;
if (blockSize < compressedReadSize)
{
compressedReadSize = blockSize;
}
}
if (blockIsCompressed == true)
{
using (var temp = new MemoryStream())
{
CopyStream(input, temp, compressedReadSize - (blockInfos[i].Size & 0xF));
temp.Position = 0;
uncompressedReadSize = uncompressedRemainingSize;
if (blockSize < uncompressedReadSize)
{
uncompressedReadSize = blockSize;
}
var zlib = new InflaterInputStream(temp);
var read = zlib.Read(buffer, 0, (int)uncompressedReadSize);
if (read != uncompressedReadSize)
{
throw new InvalidOperationException();
}
output.Write(buffer, 0, (int)(uncompressedReadSize));
}
}
else
{
uncompressedReadSize = compressedReadSize;
var read = input.Read(buffer, 0, (int)compressedReadSize);
if (read != compressedReadSize)
{
throw new InvalidOperationException();
}
output.Write(buffer, 0, (int)compressedReadSize);
}
compressedRemainingSize -= compressedReadSize;
uncompressedRemainingSize -= uncompressedReadSize;
}
}
}
}
}