-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.c
315 lines (284 loc) · 9.39 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
#include <err.h>
#include <ctype.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ems.h"
#include "header.h"
#include "cmd.h"
// don't forget to bump this :P
#define VERSION "0.04"
const int limits[3] = {0, PAGESIZE, SRAMSIZE};
// operation mode
#define MODE_READ 1
#define MODE_WRITE 2
#define MODE_TITLE 3
#define MODE_DELETE 4
#define MODE_FORMAT 5
#define MODE_RESTORE 6
#define MODE_DUMP 7
/* options */
typedef struct _options_t {
int verbose;
int blocksize;
int mode;
char *file;
int bank;
int space;
int rem_argc;
char **rem_argv;
int force;
} options_t;
// defaults
options_t opts = {
.verbose = 0,
.blocksize = 0,
.mode = 0,
.file = NULL,
.bank = 0,
.space = 0,
.force = 0,
};
// default blocksizes
#define BLOCKSIZE_READ 4096
#define BLOCKSIZE_WRITE 32
/**
* Usage
*/
void usage(char *name) {
printf("Usage: %s [ OPTION... ] COMMAND [ ARG... ]\n", name);
printf("\n");
printf("Options:\n");
printf(" --force force writing of ROMs from different models "
"of Game Boy\n");
printf(" --verbose displays more information and a progress "
"bar\n");
printf(" --page PAGE select cart page (1 or 2).\n");
printf(" --save force restore/dump to/from SRAM\n");
printf(" --rom force restore/dump to/from Flash\n");
printf("\n");
printf("Commands:\n");
printf(" --read BANK:FILE... read ROMs with the specified banks to "
"files\n");
printf(" --write FILE... write ROM file(s) to cart\n");
printf(" --dump dump an entire page of Flash or SRAM to "
"a file\n");
printf(" --restore restore an entire page of Flash or SRAM "
"to a file\n");
printf(" --delete BANK... delete ROMs with the specified banks\n");
printf(" --format delete all ROMs of the specified page\n");
printf(" --title list page content\n");
printf(" --version print version number\n");
printf(" --help show this help\n");
printf("\n");
printf("You MUST supply exactly one command.\n");
printf("ROMs specified in --read and --delete are designated by the bank "
"number printed\nby the --title command.\n");
printf("Dumping or restoring with a file ending in .sav will read/write to "
"SRAM.\n");
printf("To select between ROM and SRAM, use ONE of the --save / --rom options.\n");
printf("\n");
printf("Written by Mike Ryan <[email protected]> and others\n");
printf("See web site for more info:\n");
printf(" http://lacklustre.net/gb/ems/\n");
exit(1);
}
/**
* Get the options to the binary. Options are stored in the global "opts".
*/
void get_options(int argc, char **argv) {
int c, optval;
while (1) {
int option_index = 0;
static struct option long_options[] = {
{"help", 0, 0, 'h'},
{"version", 0, 0, 'V'},
{"verbose", 0, 0, 'v'},
{"read", 0, 0, 'r'},
{"write", 0, 0, 'w'},
{"restore", 0, 0, 'e'},
{"dump", 0, 0, 'u'},
{"title", 0, 0, 't'},
{"delete", 0, 0, 'd'},
{"format", 0, 0, 'f'},
{"blocksize", 1, 0, 's'},
{"bank", 1, 0, 'b'},
{"page", 1, 0, 'b'},
{"save", 0, 0, 'S'},
{"rom", 0, 0, 'R'},
{"force", 0, 0, 'F'},
{0, 0, 0, 0}
};
c = getopt_long(
argc, argv, "hVvs:rwt",
long_options, &option_index
);
if (c == -1)
break;
switch (c) {
case 'h':
usage(argv[0]);
break;
case 'V':
printf("EMS-flasher %s\n", VERSION);
exit(0);
break;
case 'v':
opts.verbose = 1;
break;
case 'r':
if (opts.mode != 0) goto mode_error;
opts.mode = MODE_READ;
break;
case 'w':
if (opts.mode != 0) goto mode_error;
opts.mode = MODE_WRITE;
break;
case 'e':
if (opts.mode != 0) goto mode_error;
opts.mode = MODE_RESTORE;
break;
case 'u':
if (opts.mode != 0) goto mode_error;
opts.mode = MODE_DUMP;
break;
case 't':
if (opts.mode != 0) goto mode_error;
opts.mode = MODE_TITLE;
break;
case 'd':
if (opts.mode != 0) goto mode_error;
opts.mode = MODE_DELETE;
break;
case 'f':
if (opts.mode != 0) goto mode_error;
opts.mode = MODE_FORMAT;
break;
case 's':
optval = atoi(optarg);
if (optval <= 0) {
printf("Error: block size must be > 0\n");
usage(argv[0]);
}
// TODO make sure it divides evenly into bank size
opts.blocksize = optval;
break;
case 'b':
optval = atoi(optarg);
if (optval < 1 || optval > 2) {
printf("Error: cart only has two banks 1 and 2\n");
usage(argv[0]);
}
opts.bank = optval - 1;
break;
case 'S':
if (opts.space != 0) goto mode_error2;
opts.space = FROM_SRAM;
break;
case 'R':
if (opts.space != 0) goto mode_error2;
opts.space = FROM_ROM;
break;
case 'F':
opts.force = 1;
break;
default:
usage(argv[0]);
break;
}
}
// user didn't supply mode
if (opts.mode == 0)
goto mode_error;
opts.rem_argc = argc - optind;
if (optind < argc)
opts.rem_argv = &argv[optind];
if (opts.mode == MODE_FORMAT || opts.mode == MODE_TITLE) {
if (optind < argc) {
printf("Error: no argument expected\n");
usage(argv[0]);
}
} else if (opts.mode == MODE_DELETE) {
if (optind >= argc) {
printf("Error: you must provide bank numbers\n");
usage(argv[0]);
}
} else if (opts.mode == MODE_WRITE || opts.mode == MODE_READ ||
opts.mode == MODE_RESTORE || opts.mode == MODE_DUMP) {
// user didn't give a filename
if (optind >= argc) {
printf("Error: you must provide an %s filename\n", opts.mode == MODE_READ ? "output" : "input");
usage(argv[0]);
}
// extra argument: ROM file
opts.file = argv[optind];
// set a default blocksize if the user hasn't given one
if (opts.blocksize == 0)
opts.blocksize = (opts.mode == MODE_READ
|| opts.mode == MODE_DUMP)? BLOCKSIZE_READ : BLOCKSIZE_WRITE;
}
return;
mode_error:
printf("Error: must supply exactly one of --read, --write, --dump, "
"--restore, --delete, --format or --title\n");
usage(argv[0]);
mode_error2:
printf("Error: must supply zero or one of --sram, or --rom\n");
usage(argv[0]);
}
/**
* Main
*/
int main(int argc, char **argv) {
int r;
get_options(argc, argv);
if (opts.verbose)
printf("trying to find EMS cart\n");
r = ems_init();
if (r < 0)
return 1;
if (opts.verbose)
printf("claimed EMS cart\n");
// we'll need a buffer one way or another
uint32_t base = opts.bank * PAGESIZE;
if (opts.verbose)
printf("base address is 0x%X\n", base);
// determine what we're reading/writing from/to
int space = opts.space;
if (space == 0 && opts.file != NULL) {
//attempt to autodetect the file
//are the last four characters .sav ?
size_t namelen = strlen(opts.file);
if (opts.file[namelen - 4] == '.' &&
tolower(opts.file[namelen - 3]) == 's' &&
tolower(opts.file[namelen - 2]) == 'a' &&
tolower(opts.file[namelen - 1]) == 'v') {
space = FROM_SRAM;
} else {
space = FROM_ROM;
}
}
// read the ROM and save it into the file
if (opts.mode == MODE_READ) {
cmd_read(opts.bank, opts.verbose, opts.rem_argc, opts.rem_argv);
} else if (opts.mode == MODE_DUMP) {
cmd_dump(opts.bank, opts.verbose, opts.file, space);
} else if (opts.mode == MODE_RESTORE) {
cmd_restore(opts.bank, opts.verbose, opts.file, space);
} else if (opts.mode == MODE_WRITE) {
cmd_write(opts.bank, opts.verbose, opts.force, opts.rem_argc, opts.rem_argv);
} else if (opts.mode == MODE_DELETE) {
cmd_delete(opts.bank, opts.verbose, opts.rem_argc, opts.rem_argv);
} else if (opts.mode == MODE_FORMAT) {
cmd_format(opts.bank, opts.verbose);
}
// read the ROM header
else if (opts.mode == MODE_TITLE) {
cmd_title(opts.bank);
}
// should never reach here
else
errx(1, "Unknown mode %d, file a bug report", opts.mode);
return 0;
}