-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
372 lines (300 loc) · 10.6 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
/* Copyright 2008-2017 Carsten Elton Sorensen
This file is part of ASMotor.
ASMotor is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ASMotor is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ASMotor. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include "asmotor.h"
#include "file.h"
#define XGBFIX_VERSION "1.0.0"
#define POS_NINTENDO_LOGO 0x0104L
#define POS_CARTRIDGE_TITLE 0x0134L
#define POS_CARTRIDGE_TYPE 0x0147L
#define POS_ROM_SIZE 0x0148L
#define POS_COMP_CHECKSUM 0x014DL
#define POS_CHECKSUM 0x014EL
/* Option handling
*/
#define OPTF_DEBUG 0x01L
#define OPTF_PAD 0x02L
#define OPTF_VALIDATE 0x04L
#define OPTF_TITLE 0x08L
uint8_t g_optionFlags;
static bool
isOptionSet(uint8_t optionFlag) {
return (g_optionFlags & optionFlag) != 0;
}
/* Misc. variables */
static uint8_t g_nintendoChar[48] = {
0xCE, 0xED, 0x66, 0x66, 0xCC, 0x0D, 0x00, 0x0B, 0x03, 0x73, 0x00, 0x83, 0x00, 0x0C, 0x00, 0x0D, 0x00, 0x08,
0x11, 0x1F, 0x88, 0x89, 0x00, 0x0E, 0xDC, 0xCC, 0x6E, 0xE6, 0xDD, 0xDD, 0xD9, 0x99, 0xBB, 0xBB, 0x67, 0x63,
0x6E, 0x0E, 0xEC, 0xCC, 0xDD, 0xDC, 0x99, 0x9F, 0xBB, 0xB9, 0x33, 0x3E
};
/* Diagnostic functions */
static void
printUsage(void) {
printf("xgbfix v" XGBFIX_VERSION " (part of ASMotor " ASMOTOR_VERSION ")\n\n"
"Usage: xgbfix [options] image[.gb]\n"
"Options:\n"
"\t-h\t\tThis text\n"
"\t-d\t\tDebug: Don't change image\n"
"\t-p\t\tPad image to valid size\n\t\t\tPads to 32/64/128/256/512kB as appropriate\n"
"\t-t<name>\tChange cartridge title field (16 characters)\n"
"\t-v\t\tValidate header\n"
"\t\t\tCorrects - Nintendo Character Area (0x0104)\n"
"\t\t\t\t - ROM type (0x0147)\n"
"\t\t\t\t - ROM size (0x0148)\n"
"\t\t\t\t - Checksums (0x014D-0x014F)\n");
exit(EXIT_SUCCESS);
}
static void
fatalError(const char* error) {
printf("ERROR: %s\n\n", error);
exit(EXIT_FAILURE);
}
/* ROM image validation functions */
static void
validateNintendoCharacterArea(FILE* fileHandle) {
uint32_t bytesChangedCount = 0;
fflush(fileHandle);
fseek(fileHandle, POS_NINTENDO_LOGO, SEEK_SET);
for (int i = 0; i < 48; ++i) {
int ch = fgetc(fileHandle);
if (ch == EOF)
ch = 0x00;
if (ch != g_nintendoChar[i]) {
++bytesChangedCount;
if (!isOptionSet(OPTF_DEBUG)) {
fflush(fileHandle);
fseek(fileHandle, POS_NINTENDO_LOGO + i, SEEK_SET);
fwrite(&g_nintendoChar[i], 1, 1, fileHandle);
fflush(fileHandle);
}
}
}
if (isOptionSet(OPTF_DEBUG)) {
if (bytesChangedCount != 0)
printf("\tChanged %d bytes in the Nintendo Character Area\n", bytesChangedCount);
else
printf("\tNintendo Character Area is OK\n");
}
}
static void
validateRomSize(FILE* fileHandle) {
fflush(fileHandle);
fseek(fileHandle, POS_ROM_SIZE, SEEK_SET);
int cartRomSize = fgetc(fileHandle);
if (cartRomSize == EOF)
cartRomSize = 0x00;
uint8_t calculatedRomSize = 0;
size_t romSize = fsize(fileHandle);
while (romSize > (0x8000UL << calculatedRomSize))
++calculatedRomSize;
if (calculatedRomSize == cartRomSize) {
if (isOptionSet(OPTF_DEBUG))
printf("\tROM size byte is OK\n");
return;
}
if (isOptionSet(OPTF_DEBUG)) {
printf("\tChanged ROM size byte from 0x%02X (%ldkB) to 0x%02X (%ldkB)\n", cartRomSize,
(0x8000UL << (uint8_t) cartRomSize) / 1024, calculatedRomSize, (0x8000UL << calculatedRomSize) / 1024);
} else {
fseek(fileHandle, POS_ROM_SIZE, SEEK_SET);
fwrite(&calculatedRomSize, 1, 1, fileHandle);
fflush(fileHandle);
}
}
static void
validateCartridgeType(FILE* fileHandle) {
fflush(fileHandle);
fseek(fileHandle, POS_CARTRIDGE_TYPE, SEEK_SET);
int cartType = fgetc(fileHandle);
if (cartType == EOF)
cartType = 0x00;
if (fsize(fileHandle) <= 0x8000UL || cartType != 0x00) {
/* cart type byte can be anything? */
if (isOptionSet(OPTF_DEBUG))
printf("\tCartridge type byte is OK\n");
return;
}
if (isOptionSet(OPTF_DEBUG)) {
printf("\tCartridge type byte changed to 0x01\n");
return;
}
cartType = 0x01;
fseek(fileHandle, POS_CARTRIDGE_TYPE, SEEK_SET);
fwrite(&cartType, 1, 1, fileHandle);
fflush(fileHandle);
}
static void
validateChecksum(FILE* fileHandle) {
size_t romSize = fsize(fileHandle);
uint16_t cartChecksum = 0;
uint16_t calculatedChecksum = 0;
uint8_t cartCompChecksum = 0;
uint8_t calculatedCompChecksum = 0;
fflush(fileHandle);
fseek(fileHandle, 0, SEEK_SET);
for (size_t i = 0; i < romSize; ++i) {
int ch = fgetc(fileHandle);
if (ch == EOF)
ch = 0;
if (i < 0x0134L) {
calculatedChecksum += (uint16_t) ch;
} else if (i < 0x014DL) {
calculatedCompChecksum += (uint8_t) ch;
calculatedChecksum += (uint16_t) ch;
} else if (i == 0x014DL) {
cartCompChecksum = (uint8_t) ch;
} else if (i == 0x014EL) {
cartChecksum = (uint16_t) ch << 8U;
} else if (i == 0x014FL) {
cartChecksum |= (uint16_t) ch;
} else {
calculatedChecksum += (uint16_t) ch;
}
}
calculatedCompChecksum = (uint8_t) (0xE7U - calculatedCompChecksum);
calculatedChecksum += calculatedCompChecksum;
if (cartChecksum == calculatedChecksum) {
if (isOptionSet(OPTF_DEBUG))
printf("\tChecksum is OK\n");
} else {
if (!isOptionSet(OPTF_DEBUG)) {
fflush(fileHandle);
fseek(fileHandle, POS_CHECKSUM, SEEK_SET);
fputc(calculatedChecksum >> 8U, fileHandle);
fputc(calculatedChecksum & 0xFFU, fileHandle);
fflush(fileHandle);
} else
printf("\tChecksum changed from 0x%04lX to 0x%04lX\n", (long) cartChecksum, (long) calculatedChecksum);
}
if (cartCompChecksum == calculatedCompChecksum) {
if (isOptionSet(OPTF_DEBUG))
printf("\tCompChecksum is OK\n");
} else if (!isOptionSet(OPTF_DEBUG)) {
fflush(fileHandle);
fseek(fileHandle, POS_COMP_CHECKSUM, SEEK_SET);
fwrite(&calculatedCompChecksum, 1, 1, fileHandle);
fflush(fileHandle);
} else
printf("\tCompChecksum changed from 0x%02lX to 0x%02lX\n", (long) cartCompChecksum,
(long) calculatedCompChecksum);
}
static void
padRomImage(FILE* fileHandle) {
size_t romSize = fsize(fileHandle);
size_t padToSize;
padToSize = 0x8000UL;
while (romSize > padToSize)
padToSize *= 2;
if (isOptionSet(OPTF_DEBUG))
printf("Padding to %ldkB:\n", (long) (padToSize / 1024));
if (romSize == padToSize) {
printf("\tNo padding needed\n");
return;
}
if (isOptionSet(OPTF_DEBUG)) {
printf("\tAdded %ld bytes\n", (long) (padToSize - romSize));
return;
}
fseek(fileHandle, 0, SEEK_END);
while (romSize < padToSize) {
++romSize;
fputc(0, fileHandle);
}
fflush(fileHandle);
}
static void
setCartridgeTitle(FILE* fileHandle, char* newTitle) {
char cartTitle[16];
if (isOptionSet(OPTF_DEBUG)) {
printf("Setting cartridge title:\n");
printf("\tTitle set to %s\n", newTitle);
return;
}
memset(cartTitle, 0, 16);
strncpy(cartTitle, newTitle, 16);
fflush(fileHandle);
fseek(fileHandle, POS_CARTRIDGE_TITLE, SEEK_SET);
fwrite(cartTitle, 16, 1, fileHandle);
fflush(fileHandle);
}
int
main(int argc, char* argv[]) {
if (--argc == 0)
printUsage();
g_optionFlags = 0;
char newCartTitle[32];
int argn = 1;
while (*argv[argn] == '-') {
--argc;
switch (argv[argn++][1]) {
default:
printf("Unknown option \"%s\"\n", argv[argn - 1]);
exit(EXIT_FAILURE);
case '?':
case 'h':
printUsage();
break;
case 'd':
g_optionFlags |= OPTF_DEBUG;
break;
case 'p':
g_optionFlags |= OPTF_PAD;
break;
case 'v':
g_optionFlags |= OPTF_VALIDATE;
break;
case 't':
strncpy(newCartTitle, argv[argn - 1] + 2, 16);
g_optionFlags |= OPTF_TITLE;
break;
}
}
char filename[512];
strcpy(filename, argv[argn++]);
FILE* fileHandle;
if (!fexists(filename))
strcat(filename, ".gb");
if ((fileHandle = fopen(filename, "rb+")) != NULL) {
/* -d (Debug) option */
if (isOptionSet(OPTF_DEBUG))
printf("-d (Debug) option enabled...\n");
/* -p (Pad) option */
if (isOptionSet(OPTF_PAD))
padRomImage(fileHandle);
/* -t (Set cart title) option */
if (isOptionSet(OPTF_TITLE))
setCartridgeTitle(fileHandle, newCartTitle);
/* -v (Validate header) option */
if (isOptionSet(OPTF_VALIDATE)) {
if (isOptionSet(OPTF_DEBUG))
printf("Validating header:\n");
/* Nintendo Character Area */
validateNintendoCharacterArea(fileHandle);
/* ROM size */
validateRomSize(fileHandle);
/* Cartridge type */
validateCartridgeType(fileHandle);
/* Checksum */
validateChecksum(fileHandle);
}
fclose(fileHandle);
} else {
fatalError("Unable to open file");
}
return EXIT_SUCCESS;
}