-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
454 lines (356 loc) · 12.3 KB
/
main.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
// Copyright 2017 Jannik Vogel
// Licensed under GPLv3 or any later version.
// Refer to the LICENSE.txt file included.
#include "zlib.h"
#include <assert.h>
#include <inttypes.h>
#include <arpa/inet.h>
#include <stdint.h>
#include <stddef.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "crc32.h"
#include "platform.h"
#include "md5.h"
#include "sha1.h"
#include "ss.h"
#include "scsi.h"
#define MIN(a,b) (((a)<(b))?(a):(b))
FILE* iso = NULL;
CRC32_CTX crc32context;
MD5_CTX md5context;
SHA1_CTX sha1context;
bool printprogress(uint32_t totalsize, uint32_t sectorsdone) {
uint64_t currenttime = millisecondstime();
static uint64_t starttime = 0;
static uint64_t lastupdate = 0;
if (starttime == 0 || lastupdate == 0) {
starttime = currenttime;
lastupdate = currenttime;
}
if (currenttime > (lastupdate + 10)) {
uint64_t ms_passed = currenttime - starttime;
lastupdate = currenttime;
printf("%" PRIu32 " sectors (%u%%", sectorsdone, ((sectorsdone * 100) / totalsize));
if (ms_passed > 0) {
unsigned int rate = ((sectorsdone / 512 * 100000) / ms_passed);
printf(", %u.%02u MiB/sec", rate / 100, rate % 100);
}
printf(") \r");
fflush(stdout);
return true;
}
return false;
}
bool processblock(const uint8_t* buffer, size_t buffer_size, bool compress) {
buffer_size *= SectorSize; // We work in bytes now!
static uint64_t saved = 0;
static uint64_t shitty = 0;
#if 1
//FIXME: In a thread
CRC32_Update(&crc32context, buffer, buffer_size);
MD5_Update(&md5context, buffer, buffer_size);
SHA1_Update(&sha1context, buffer, buffer_size);
//FIXME: In a thread
//FIXME: Better error checking
uint8_t* output_buffer;
size_t output_size;
if (compress) {
size_t output_buffer_size = buffer_size + 512;
output_buffer = malloc(output_buffer_size); //FIXME: How much overhead does zlib need in the worst case?!
z_stream defstream;
defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL;
defstream.opaque = Z_NULL;
// setup "a" as the input and "b" as the compressed output
defstream.avail_in = (uInt)buffer_size; // size of input, string + terminator
defstream.next_in = (Bytef*)buffer; // input char array
defstream.avail_out = (uInt)output_buffer_size; // size of output
defstream.next_out = (Bytef*)output_buffer; // output char array
deflateInit(&defstream, Z_BEST_COMPRESSION);
deflate(&defstream, Z_FINISH);
assert(defstream.avail_in == 0);
output_size = output_buffer_size - defstream.avail_out;
// printf("From %d to %d\n", buffer_size, output_size);
deflateEnd(&defstream);
saved += buffer_size - output_size;
if (output_size == 84) {
shitty += 1;
}
} else {
output_buffer = (uint8_t*)buffer;
output_size = buffer_size;
}
#if 0
if(fwrite(output_buffer, 1, output_size, iso) != output_size) {
if (compress) {
free(output_buffer);
}
printf("Error writing..\n");
return false;
}
#endif
if (compress) {
free(output_buffer);
}
printf("Saved: %" PRIu64 " %" PRIu64 " are shitty\n", saved, shitty);
#endif
return true;
}
bool dump_sectors(uint32_t start, uint32_t end, bool read_data, const SSRange* ss_ranges, unsigned int* sectorsdone, unsigned int totalsize, bool compress) {
uint32_t buffer_size = 32;
uint8_t* buffer = (uint8_t*)malloc(buffer_size * SectorSize);
if (!read_data) {
memset(buffer, 0x00, buffer_size * SectorSize);
}
uint32_t offset = start;
while (offset < end) {
uint32_t data_chunk_size = MIN(end - offset, buffer_size);
uint32_t skip_size = 0;
if (ss_ranges != NULL) {
const SSRange* ss_range = find_next_ss(ss_ranges, offset);
if (ss_range != NULL) {
if ((offset + data_chunk_size) >= ss_range->start) {
data_chunk_size = ss_range->start - offset;
skip_size = ss_range->end - ss_range->start + 1;
}
}
}
if (read_data) {
if (!readblock(buffer, offset, data_chunk_size)) {
break;
}
}
if (!processblock(buffer, data_chunk_size, compress)) {
break;
}
uint32_t total_chunk_size = data_chunk_size + skip_size;
// Write padding / zero data
if (read_data && (skip_size > 0)) {
memset(buffer, 0x00, buffer_size * SectorSize);
}
while(skip_size > 0) {
uint32_t skip_chunk_size = MIN(skip_size, buffer_size);
processblock(buffer, skip_chunk_size, compress);
skip_size -= skip_chunk_size;
}
*sectorsdone += total_chunk_size;
offset += total_chunk_size;
printprogress(totalsize, *sectorsdone);
}
free(buffer);
return offset == end;
}
int dump_data(SSRange* ss_ranges) {
size_t l0_video, l1_video, middlezone, gamedata, totalsize;
LayerSizes layers;
bool compress = false;
if (setlockingmode(MODE_LOCKED)) return 1;
if (getlayersizes(&layers)) return 1;
l0_video = layers.l0;
l1_video = layers.l1;
if (setlockingmode(MODE_XTREME)) return 1;
if (getlayersizes(&layers)) return 1;
gamedata = layers.l0 + layers.l1;
if (setlockingmode(MODE_WXRIPPER)) return 1;
if (getlayersizes(&layers)) return 1;
if (layers.l1 < gamedata) {
fprintf(stderr, "Error: Middle Zone has a negative value!\n");
return 1;
}
middlezone = layers.l1 - gamedata;
totalsize = l0_video + l1_video + (2 * middlezone) + gamedata;
printf("\n");
printf("L0 Video Size: %zu sectors\n", l0_video);
printf("L1 Video Size: %zu sectors\n", l1_video);
printf("Middle Zone Size: %zu sectors\n", middlezone);
printf("Game Data Size: %zu sectors\n", gamedata);
printf("Total Size: %zu sectors (%" PRIu64 " bytes = %.1f GiB)\n", totalsize, totalsize * (uint64_t)SectorSize, totalsize * (float)SectorSize / 1024.0f / 1024.0f / 1024.0f);
printf("\n");
printf("Real Layer Break: %zu sectors\n", l0_video + middlezone + (gamedata / 2));
printf("\n");
unsigned int sectorsdone = 0;
if (setstreaming()) {
return 1;
}
// L0 Video
dump_sectors(0, l0_video, true, NULL, §orsdone, totalsize, compress);
// Middle Zone A
dump_sectors(l0_video, l0_video + middlezone, false, NULL, §orsdone, totalsize, compress);
// Game Data
dump_sectors(l0_video + middlezone, l0_video + middlezone + gamedata, true, ss_ranges, §orsdone, totalsize, compress);
// Middle Zone D
dump_sectors(0, middlezone, false, NULL, §orsdone, totalsize, compress);
// L1 Video
if (setlockingmode(MODE_LOCKED)) {
return 1;
}
dump_sectors(l0_video, l0_video + l1_video, true, NULL, §orsdone, totalsize, compress);
//FIXME: Why do this? Comes from freecell..
if (setlockingmode(MODE_WXRIPPER)) {
return 1;
}
return 0;
}
void digesttostr(char *hash, const uint8_t* digest, size_t length) {
const char* hex_digits = "0123456789ABCDEF";
for (unsigned int i = 0; i < length; i++) {
hash[(i * 2) + 0] = hex_digits[digest[i] >> 4];
hash[(i * 2) + 1] = hex_digits[digest[i] & 0xF];
}
hash[length * 2] = 0;
}
uint32_t calculate_crc(uint8_t* buffer, size_t buffer_size) {
CRC32_CTX ctx;
unsigned int crc;
CRC32_Init(&ctx);
CRC32_Update(&ctx, buffer, buffer_size);
CRC32_Final(&crc, &ctx);
return crc;
}
void dump_file(const char* path, const uint8_t* buffer, size_t buffer_size) {
FILE* f = fopen(path, "wb");
fwrite(buffer, buffer_size, 1, f);
fclose(f);
}
const char* get_kreon_feature_description(uint16_t feature) {
switch(feature) {
case 0x0100: return "Xbox 360 unlock 1 state (xtreme)";
case 0x0101: return "Xbox 360 unlock 2 state (wxripper)";
case 0x0120: return "Xbox 360 SS reading and decryption";
case 0x0121: return "Xbox 360 full challenge response functionality";
case 0x0200: return "Xbox unlock 1 state (xtreme)";
case 0x0201: return "Xbox unlock 2 state (wxripper)";
case 0x0220: return "Xbox SS reading and decryption";
case 0x0221: return "Xbox 360 full challenge response functionality";
case 0xF000: return "Lock command (cancel unlock)";
case 0xF001: return "Error skipping";
default: break;
}
return NULL;
}
const char* executable = NULL;
int main(int argc, char *argv[]) {
executable = argv[0];
int ret = 0;
int optind = 1;
if (getdrive(argv[optind])) {
fprintf(stderr, "%s: %s does not look like a valid drive\n", executable, argv[optind]);
return 1;
}
optind++;
if (opendrive()) {
return 1;
}
uint8_t buffer[4096];
//FIXME: Get info about drive
inquiry(buffer, 255);
//dump_file("inquiry.bin", buffer, 255);
char vendor_identification[8+1] = {0};
memcpy(vendor_identification, &buffer[8], sizeof(vendor_identification) - 1);
printf("Vendor Identification: '%s'\n", vendor_identification);
char product_identification[16+1] = {0};
memcpy(product_identification, &buffer[16], sizeof(product_identification) - 1);
printf("Product Identification: '%s'\n", product_identification);
char product_revision_level[4+1] = {0};
memcpy(product_revision_level, &buffer[32], sizeof(product_revision_level) - 1);
printf("Product Revision Level: '%s'\n", product_revision_level);
char vendor_specific[20+1] = {0};
memcpy(vendor_specific, &buffer[36], sizeof(vendor_specific) - 1);
printf("Vendor Specific: '%s'\n", vendor_specific);
printf("\n");
if (!strcmp(vendor_specific, "KREON V1.00")) {
printf("Kreon 1.00 detected!\n");
printf("Kreon features:\n");
uint8_t buffer[4096];
getfeaturelist(buffer, sizeof(buffer));
const uint16_t* feature = (uint16_t*)buffer;
//FIXME: Watch out for overflow!
assert(*feature++ == htons(0xA55A));
assert(*feature++ == htons(0x5AA5));
while(*feature != 0x0000) {
printf("- %s\n", get_kreon_feature_description(htons(*feature)));
feature++;
}
} else {
printf("Unknown drive!\n");
//FIXME: Inform user which firmware might be applicable for their drive
}
printf("\n");
//FIXME: Get layer sizes and test if locking works properly
setlockingmode(MODE_XTREME);
// memset(buffer, 0xAA, sizeof(buffer));
// getfeaturelist(buffer, sizeof(buffer));
// Dump the typical piracy related files
if (1) {
memset(buffer, 0xAA, sizeof(buffer));
getdmi(buffer, sizeof(buffer));
dump_file("DMI.bin", &buffer[4], 2048);
printf("DMI: %08X\n", calculate_crc(&buffer[4], 2048));
}
if (1) {
memset(buffer, 0xAA, sizeof(buffer));
getpfi(0, buffer, sizeof(buffer));
dump_file("PFI.bin", &buffer[4], 2048);
printf("PFI: %08X\n", calculate_crc(&buffer[4], 2048));
}
uint8_t ss[2048];
if (1) {
//FIXME: I have no idea how to do this correctly?!
memset(buffer, 0xAA, sizeof(ss));
getss(0x01, ss, sizeof(ss)); // Expected checksum
//getss(0x03, buffer, sizeof(buffer)); // Expected checksum
//getss(0x05, buffer, sizeof(buffer)); // Expected checksum
//getss(0x07, buffer, sizeof(buffer)); // Expected checksum
dump_file("SS.bin", ss, sizeof(ss));
printf("SS: %08X\n", calculate_crc(ss, sizeof(ss)));
}
printf("\n");
SSType ss_type = get_ss_type(ss);
if (ss_type == Xbox360 || ss_type == Xbox360_XGD3) {
fprintf(stderr, "This seems to be an Xbox 360 disc. This tool is only capable of dumping original Xbox discs!\n");
return 1;
} else if (ss_type != Xbox) {
fprintf(stderr, "This does not seem to be an original Xbox disc!\n");
return 1;
}
SSRange ss_ranges[16];
get_ss_ranges(ss, ss_ranges);
{
FILE* f = fopen("sectors.txt", "wb");
for(unsigned int i = 0; i < 16; i++) {
printf("%" PRIu32 "-%" PRIu32 "\r\n", ss_ranges[i].start, ss_ranges[i].end);
fprintf(f, "%" PRIu32 "-%" PRIu32 "\r\n", ss_ranges[i].start, ss_ranges[i].end);
assert(((i == 0) ? 0 : ss_ranges[i - 1].end) <= ss_ranges[i].start);
}
fclose(f);
}
printf("\n");
{
iso = fopen("data.iso", "wb");
CRC32_Init(&crc32context);
MD5_Init(&md5context);
SHA1_Init(&sha1context);
dump_data(ss_ranges);
unsigned int crc;
CRC32_Final(&crc, &crc32context);
unsigned char md5digest[MD5_DIGEST_LENGTH];
char md5hash[(MD5_DIGEST_LENGTH * 2) + 1];
MD5_Final(md5digest, &md5context);
digesttostr(md5hash, md5digest, MD5_DIGEST_LENGTH);
unsigned char sha1digest[SHA1_DIGEST_LENGTH];
char sha1hash[(SHA1_DIGEST_LENGTH * 2) + 1];
SHA1_Final(sha1digest, &sha1context);
digesttostr(sha1hash, sha1digest, SHA1_DIGEST_LENGTH);
printf("CRC32: %08x\n", (unsigned int)crc);
printf("MD5: %s\n", md5hash);
printf("SHA1: %s\n", sha1hash);
fclose(iso);
}
closedrive();
return ret;
}