-
Notifications
You must be signed in to change notification settings - Fork 0
/
grader.c
591 lines (536 loc) · 12.7 KB
/
grader.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
/* jrra 2023 */
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stdint.h>
//#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include "endian.h"
#include "err.h"
#include "grader.h"
#include "ipltxt.h"
#include "sha1.h"
struct {
char *name;
int32_t epOffset;
uint32_t seed;
const uint8_t *txt;
uint8_t signature[20];
bool end;
} cics[] = {
[IPL_NONE] = {
.name = "unrecognized",
.signature = {0},
},
[IPL_6101] = {
.name = "6101",
.epOffset = 0,
.seed = 0x3f * 0x5d588b65u + 1,
.txt = ipl_6101,
.signature = {0xea, 0xad, 0xcb, 0x8c, 0xca, 0x9c, 0x6b, 0xa1,
0x44, 0x5f, 0x98, 0xf1, 0x72, 0x7b, 0xf4, 0xad,
0xba, 0xbb, 0x88, 0xb2},
},
[IPL_6102] = {
.name = "6102 or 7101",
.epOffset = 0,
.seed = 0x3f * 0x5d588b65u + 1,
.txt = ipl_6102,
.signature = {0xb2, 0xaf, 0xae, 0x24, 0x6e, 0x1d, 0xab, 0x74,
0x6b, 0xfb, 0x28, 0xcb, 0x34, 0x6e, 0x29, 0x11,
0x96, 0x5e, 0xef, 0xa1},
},
[IPL_6103] = {
.name = "6103 or 7103",
.epOffset = -0x100000,
.seed = 0x78 * 0x6c078965u + 1,
.txt = ipl_6103,
.signature = {0x3f, 0x73, 0x47, 0xaa, 0x04, 0x26, 0xee, 0x97,
0xd9, 0x67, 0x21, 0xb0, 0x9b, 0x91, 0x8d, 0x4c,
0x9c, 0xdd, 0xb6, 0x9b},
},
[IPL_6105] = {
.name = "6105 or 7105",
.epOffset = 0,
.seed = 0x91 * 0x5d588b65u + 1,
.txt = ipl_6105,
.signature = {0x41, 0x59, 0x26, 0x90, 0x55, 0xe8, 0xa5, 0xbe,
0x2e, 0x5c, 0x8e, 0x3e, 0x0f, 0x5e, 0x0d, 0x55,
0x2f, 0x1e, 0x85, 0xad},
},
[IPL_6106] = {
.name = "6106 or 7106",
.epOffset = -0x200000,
.seed = 0x85 * 0x003d6e72u + 0x40,
.txt = ipl_6106,
.signature = {0x63, 0x46, 0x8e, 0x3a, 0xb5, 0x54, 0x25, 0x3d,
0xa3, 0x4d, 0xe3, 0x59, 0x5c, 0xd0, 0x9b, 0x0e,
0xce, 0xa1, 0xce, 0x00},
},
[IPL_7102] = {
.name = "7102",
.epOffset = 0,
.seed = 0x3f * 0x5d588b65u + 1,
.txt = ipl_7102,
.signature = {0x79, 0xfe, 0x35, 0x1c, 0x50, 0xcd, 0xf7, 0x7c,
0x74, 0xe5, 0x03, 0xd7, 0x51, 0xa7, 0x15, 0x60,
0x5f, 0x87, 0xb8, 0x09},
},
[IPL_8303] = {
.name = "8303",
.epOffset = 0,
.txt = ipl_8303,
.signature = {0x5e, 0xa7, 0xfc, 0x32, 0x74, 0xbe, 0x01, 0x12,
0x1c, 0xfb, 0x20, 0xad, 0x6c, 0x9e, 0x60, 0x85,
0xd6, 0x32, 0x79, 0xbe},
},
[IPL_HW1] = {
.name = "HW1",
.epOffset = 0,
.signature = {0xd6, 0x8c, 0x83, 0xda, 0xb8, 0xc2, 0xc2, 0xb0,
0x8e, 0x36, 0x70, 0xbd, 0xf2, 0x8d, 0x93, 0xb8,
0x94, 0x61, 0x20, 0xc6},
},
[IPL_IQUE] = {
.name = "iQue",
.epOffset = 0,
.signature = {0x17, 0x6d, 0x20, 0xcd, 0x8b, 0x0f, 0x7e, 0x24,
0x5b, 0x3d, 0x4e, 0x02, 0x7e, 0x78, 0x95, 0x52,
0x01, 0xe7, 0xee, 0x5c},
},
{
.end = true,
},
};
char *gradeErrors[] = {
"ok",
"couldn't stat file",
"couldn't open file",
"file too small",
};
struct perm_iterator {
int *order;
signed char *direction;
int num_elements;
int swapped_idx;
};
int perm_iterator_init(struct perm_iterator *p, int n)
{
if (n < 1)
return -1;
p->num_elements = n;
p->order = calloc(n, sizeof(int));
p->direction = calloc(n, sizeof(char));
if (!p->order || !p->direction)
return -1;
for (int i = 0; i < n; i++) {
p->order[i] = i;
}
for (int i = 1; i < n; i++) {
p->direction[i] = (signed char)(-1);
}
p->direction[0] = 0;
return 0;
}
void perm_iterator_destroy(struct perm_iterator *p)
{
free(p->order);
free(p->direction);
}
int perm_iterator_has_next(struct perm_iterator *p)
{
int i;
for (i = 0; i < p->num_elements; ++i)
if (p->direction[i] != 0)
return 1;
return 0;
}
/*
* Johnson-Trotter algorithm with Even's speedup.
* Transcribed from prose on Wikipedia:
* https://en.wikipedia.org/w/index.php?oldid=795078780#Even.27s_speedup
*/
int perm_iterator_iterate(struct perm_iterator *p)
{
if (!perm_iterator_has_next(p))
return -1;
int n = p->num_elements;
int *a = p->order;
signed char *b = p->direction;
int largest = 0;
int lIdx = 0;
int i, j, t;
for (i = 0; i < n; i++) {
if (a[i] > largest && b[i] != 0) {
largest = a[i];
lIdx = i;
}
}
j = lIdx + b[lIdx];
t = a[lIdx];
a[lIdx] = a[j];
a[j] = t;
t = b[lIdx];
b[lIdx] = b[j];
b[j] = t;
if ((j == (n - 1)) || (j == 0) || (a[j + b[j]] > a[j])) {
b[j] = 0;
}
for (i = 0; i < n; i++) {
if (a[i] > a[j]) {
b[i] = (i < j) ? 1 : -1;
}
}
t = lIdx > j ? j : lIdx;
return p->swapped_idx = t;
}
int identify_ipl3(uint8_t * ipl3)
{
uint8_t sha1sum[SHA1_DIGEST_SIZE];
SHA1_CTX sha1ctx;
SHA1_Init(&sha1ctx);
SHA1_Update(&sha1ctx, ipl3, 0xFC0);
SHA1_Final(&sha1ctx, sha1sum);
int cicIndex = 0;
while (!cics[cicIndex].end) {
if (!memcmp(cics[cicIndex].signature, sha1sum,
SHA1_DIGEST_SIZE)) {
return cicIndex;
break;
}
cicIndex++;
}
return IPL_NONE;
}
void swap4(uint8_t * buf, size_t len, int order)
{
size_t i;
int j;
uint8_t tmp[4];
int lut[4];
lut[0] = order / 1000 % 10 - 1;
lut[1] = order / 100 % 10 - 1;
lut[2] = order / 10 % 10 - 1;
lut[3] = order / 1 % 10 - 1;
for (i = 0; i < len; i += 4) {
for (j = 0; j < 4; j++)
tmp[j] = buf[i + lut[j]];
for (j = 0; j < 4; j++)
buf[i + j] = tmp[j];
}
}
void vis(struct romGrade *rg)
{
#ifndef _WIN32
char **g = (char *[]){
[GRADE_OK] = "[\e[32mGOOD\e[0m]",
[GRADE_WARN] = "[\e[33mWARN\e[0m]",
[GRADE_ERROR] = "[\e[31mFAIL\e[0m]",
[GRADE_NOT_GRADED] = "[\e[35m NA \e[0m]",
[GRADE_FIXED] = "[\e[32mFIXD\e[0m]",
};
#else
char **g = (char *[]){
[GRADE_OK] = "[GOOD]",
[GRADE_WARN] = "[WARN]",
[GRADE_ERROR] = "[FAIL]",
[GRADE_NOT_GRADED] = "[ NA ]",
[GRADE_FIXED] = "[FIXD]",
};
#endif
printf("Game name:\t\t%s\n",
strlen(rg->name)?rg->name:"(empty)");
printf("Product code:\t\t%s\n",
strlen(rg->productCode)?rg->productCode:"(empty)");
printf("Entry point:\t\t%s %08x\n", g[rg->entrypointGrade],
rg->entrypoint);
printf("Byte order grade:\t%s %d\n", g[rg->byteOrderGrade],
rg->byteOrder);
printf("PI timings grade:\t%s %08x\n", g[rg->piTimingsGrade],
rg->piTimings);
printf("File size grade:\t%s 0x%x bytes\n", g[rg->fileSizeGrade],
rg->fileSize);
printf("CRC grade:\t\t%s %08x %08x\n", g[rg->crcGrade], rg->crc1_inrom,
rg->crc2_inrom);
printf("IPL3 grade:\t\t%s %s\n", g[rg->ipl3Grade],
rg->ipl3Grade != GRADE_ERROR ? cics[rg->ipl3].name : "");
if (rg->weirdIqueHeader) {
printf("WARNING: weird 32-byte iQue header present.\n");
}
}
void grade_size(struct romGrade *rg, uint8_t * rom, size_t len)
{
(void)rom;
rg->fileSize = len;
if (rg->fileSize < 0x101000) {
rg->fileSizeGrade = GRADE_ERROR;
return;
}
switch (rg->fileSize) {
case 4 * 1024 * 1024:
case 8 * 1024 * 1024:
case 12 * 1024 * 1024:
case 16 * 1024 * 1024:
case 20 * 1024 * 1024: // Disney's Donald Duck - Going Quackers
case 24 * 1024 * 1024: // Mario Golf
case 28 * 1024 * 1024: // Hydro Thunder, WWF WrestleMania 2000
case 32 * 1024 * 1024:
case 40 * 1024 * 1024: // Ogre Battle, Paper Mario
case 64 * 1024 * 1024: // Conker's BFD, Pokemon Stadium 2, RE2
// Normal rom size.
rg->fileSizeGrade = GRADE_OK;
break;
default:
rg->fileSizeGrade = GRADE_WARN;
break;
}
}
void grade_byte_order(struct romGrade *rg, uint8_t * rom, size_t len)
{
// Swap IPL3 around until it matches.
rg->ipl3 = IPL_NONE;
rg->ipl3Grade = GRADE_ERROR;
if (len < 0x1000) return;
struct perm_iterator p;
if (perm_iterator_init(&p, 4) != 0) {
fprintf(stderr, "perm_iterator_init\n");
exit(1);
}
do {
int i;
uint8_t a[0x1000], b[4];
for (i = 0x40; i < 0x1000; i += 4) {
b[0] = rom[i + p.order[0]];
b[1] = rom[i + p.order[1]];
b[2] = rom[i + p.order[2]];
b[3] = rom[i + p.order[3]];
a[i + 0] = b[0];
a[i + 1] = b[1];
a[i + 2] = b[2];
a[i + 3] = b[3];
}
rg->ipl3 = identify_ipl3(a + 0x40);
if (rg->ipl3 != IPL_NONE)
break;
} while (perm_iterator_iterate(&p) >= 0);
if (rg->ipl3 != IPL_NONE) {
rg->ipl3Grade = GRADE_OK;
rg->byteOrder = p.order[0] * 1000
+ p.order[1] * 100
+ p.order[2] * 10
+ p.order[3] * 1;
rg->byteOrder += 1111;
}
switch (rg->byteOrder) {
case 1234:
rg->byteOrderGrade = GRADE_OK;
break;
default:
if (rg->fix)
rg->byteOrderGrade = GRADE_FIXED;
else
rg->byteOrderGrade = GRADE_ERROR;
break;
}
perm_iterator_destroy(&p);
}
void grade_pi_timings(struct romGrade *rg, uint8_t *rom, size_t len)
{
// Check PI timings.
if (len < 64) return;
rg->piTimings = ntohl(((uint32_t *) rom)[0]);
switch (rg->piTimings) {
case 0x80371240:
rg->piTimingsGrade = GRADE_OK;
break;
case 0x80270740:
if (rg->ipl3 == IPL_8303) {
rg->piTimingsGrade = GRADE_OK;
} else {
rg->piTimingsGrade = GRADE_WARN;
}
break;
default:
rg->piTimingsGrade = GRADE_WARN;
break;
}
}
void grade_crcs(struct romGrade *rg, uint8_t *rom, size_t len)
{
uint32_t *rom32 = (uint32_t *)rom;
if (len < 24) return;
rg->crc1_inrom = ntohl(rom32[4]);
rg->crc2_inrom = ntohl(rom32[5]);
if (rg->ipl3Grade == GRADE_ERROR)
return;
if (len < 0x101000) return;
uint32_t v1, t0, v0, a3, t2, t3, s0, a2, t4, t7, t5, t8, t6, a0, a1,
t9, ra, t1;
a0 = /* start of 1mb seg */ 0x1000;
ra = 0x100000;
v1 = 0;
t0 = 0;
t1 = a0;
t5 = 0x20;
v0 = cics[rg->ipl3].seed;
a3 = v0;
t2 = v0;
t3 = v0;
s0 = v0;
a2 = v0;
t4 = v0;
// 80000130
do {
v0 = ntohl(rom32[t1 / 4]);
v1 = a3 + v0;
a1 = v1;
if (v1 < a3) {
t2 = t2 + 1;
}
// 80000148
v1 = v0 & 0x1f;
t7 = t5 - v1;
if (t7 == 32)
t8 = 0;
else
t8 = v0 >> t7;
t6 = v0 << v1;
a0 = t6 | t8;
a3 = a1;
t3 ^= v0;
s0 += a0;
if (a2 < v0) {
t9 = a3 ^ v0;
a2 = t9 ^ a2;
} else {
a2 ^= a0;
}
// 80000180
if (rg->ipl3 == IPL_6105) {
uint32_t addr = 0x750 + (t0 & 0xff);
t4 += v0 ^ ntohl(rom32[addr / 4]);
} else {
t4 += v0 ^ s0;
}
t0 += 4;
t1 += 4;
} while (t0 != ra);
switch (rg->ipl3) {
case IPL_6101:
case IPL_6102:
case IPL_7102:
case IPL_6105:
rg->crc1_calculated = (a3 ^ t2) ^ t3;
rg->crc2_calculated = (s0 ^ a2) ^ t4;
break;
case IPL_6103:
rg->crc1_calculated = (a3 ^ t2) + t3;
rg->crc2_calculated = (s0 ^ a2) + t4;
break;
case IPL_6106:
rg->crc1_calculated = (a3 * t2) + t3;
rg->crc2_calculated = (s0 * a2) + t4;
break;
case IPL_8303:
default:
rg->crc1_calculated = 0;
rg->crc2_calculated = 0;
break;
}
rom32[4] = htonl(rg->crc1_calculated);
rom32[5] = htonl(rg->crc2_calculated);
if ((rg->crc1_inrom == rg->crc1_calculated)
&& (rg->crc2_inrom == rg->crc2_calculated)) {
rg->crcGrade = GRADE_OK;
} else {
if (rg->fix) {
rg->crc1_inrom = rg->crc1_calculated;
rg->crc2_inrom = rg->crc2_calculated;
rg->crcGrade = GRADE_FIXED;
} else {
rg->crcGrade = GRADE_ERROR;
}
}
}
void grade_name(struct romGrade *rg, uint8_t * rom, size_t len)
{
if (len < 64) return;
memcpy(&rg->name, rom + 32, 20);
rg->name[20] = '\0';
for (int i=0; i<20; i++) {
char *c = &rg->name[i];
if ((*c < ' ') || (*c > '~')) {
*c = '\0';
break;
}
}
}
void grade(struct romGrade *rg, uint8_t * rom, size_t len, enum ipl_e force_ipl, uint8_t force_region)
{
if (len < 64) return;
// Detect presence of optional iQue header and skip it if present.
if (0x80371240 == ntohl(((uint32_t *) rom)[8])) {
rom += 32;
len -= 32;
rg->weirdIqueHeader = true;
}
// Grade rom size.
grade_size(rg, rom, len);
// Grade byte order and IPL3.
if (len < 0x1000) return;
grade_byte_order(rg, rom, len); // also grades ipl3
// Byteswap the ROM if necessary.
if (rg->ipl3 != IPL_NONE && rg->byteOrder != 1234) {
swap4(rom, rg->fileSize, rg->byteOrder);
}
// Grade entrypoint.
rg->entrypoint = ntohl(((uint32_t *) rom)[2]);
if (rg->ipl3 > 0)
rg->entrypoint += cics[rg->ipl3].epOffset;
if ( ((rg->entrypoint & 0xf) == 0) &&
(rg->entrypoint >= 0x80000000) &&
(rg->entrypoint <= 0x80700000) ) {
rg->entrypointGrade = GRADE_OK;
} else {
rg->entrypointGrade = GRADE_ERROR;
}
// Force IPL3?
if ((force_ipl != IPL_NONE) && (force_ipl != rg->ipl3)) {
uint32_t ep = ntohl(((uint32_t *)rom)[2]);
if (cics[force_ipl].txt == NULL)
errx(1, "can't force IPL '%s'", cics[force_ipl].name);
memcpy(rom + 0x40, cics[force_ipl].txt, 0xfc0);
if (rg->ipl3 != IPL_NONE) {
ep += cics[rg->ipl3].epOffset;
}
ep -= cics[force_ipl].epOffset;
((uint32_t *)rom)[2] = htonl(ep);
rg->ipl3 = force_ipl;
rg->ipl3Grade = GRADE_FIXED;
}
// Force region?
if (force_region != '\0') {
((uint8_t *)rom)[0x3e] = force_region;
}
// Copy product code into rg structure.
memcpy(rg->productCode, rom + 0x3b, 4);
switch (rom[0x3f]) {
case 0 ... 9:
rg->productCode[4] = rom[0x3f] + '0';
rg->productCode[5] = '\0';
break;
default:
rg->productCode[4] = '\\';
rg->productCode[5] = 'x';
rg->productCode[6] = "0123456789abcdef"[rom[0x3f] >> 4];
rg->productCode[7] = "0123456789abcdef"[rom[0x3f] & 0x0f];
rg->productCode[8] = '\0';
break;
}
// More gradings.
grade_pi_timings(rg, rom, len);
grade_name(rg, rom, len);
grade_crcs(rg, rom, len);
}