-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathromjudge.c
111 lines (102 loc) · 1.95 KB
/
romjudge.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
/* jrra 2023 */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <stdnoreturn.h>
#include <iso646.h>
#include "err.h"
#include "grader.h"
#include "mapfile.h"
#include "progname.h"
noreturn static void usage(void)
{
(void)fprintf(stderr,
"usage: %s [-c ipl] [-r region] -f romfile\n"
" -c will force an IPL and rewrite checksums.\n"
" -r will overwrite the region letter.\n"
"\n"
"Report bugs to:\n"
"https://github.com/jkbenaim/romjudge\n",
__progname
);
exit(EXIT_FAILURE);
}
enum ipl_e IPLdToEnum(unsigned u)
{
switch (u) {
case 6101:
return IPL_6101;
break;
case 6102:
case 7101:
return IPL_6102;
break;
case 6103:
case 7103:
return IPL_6103;
break;
case 6105:
case 7105:
return IPL_6105;
break;
case 6106:
case 7106:
return IPL_6106;
break;
case 8303:
return IPL_8303;
break;
default:
return IPL_NONE;
break;
};
}
int main(int argc, char *argv[])
{
int rc;
char *filename = NULL;
struct MappedFile_s m = {0};
struct romGrade rg = {0};
bool fix = false;
enum ipl_e force_ipl = IPL_NONE;
unsigned char force_region = '\0';
progname_init(argc, argv);
while ((rc = getopt(argc, argv, "f:c:r:")) != -1)
switch (rc) {
case 'f':
if (filename)
usage();
filename = optarg;
break;
case 'c':
if (force_ipl != IPL_NONE)
usage();
force_ipl = IPLdToEnum(strtoul(optarg, NULL, 10));
break;
case 'r':
if (force_region != '\0')
usage();
force_region = optarg[0];
break;
default:
usage();
}
argc -= optind;
argv += optind;
if (not filename)
usage();
if (*argv != NULL)
usage();
fix = (force_ipl != IPL_NONE) or force_region;
m = MappedFile_Open(filename, fix);
if (!m.data) err(1, "couldn't open '%s' for reading", filename);
rg.fix = fix;
grade(&rg, (uint8_t *)m.data, (size_t)m.size, force_ipl, force_region);
vis(&rg);
MappedFile_Close(m);
m.data = NULL;
return EXIT_SUCCESS;
}