-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjrromchk.c
More file actions
409 lines (337 loc) · 8.21 KB
/
jrromchk.c
File metadata and controls
409 lines (337 loc) · 8.21 KB
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#define JRC_SIG "PCjr Cartridge image file\r\n"
#define JRC_CREATOR "Booterify " VERSION
struct jrc_header {
uint8_t signature[27+1];
char creator[30+1];
char comment[400+1];
uint8_t version[2];
uint16_t address;
uint16_t adrmask;
uint8_t reserved[46];
};
struct pcjr_rom {
uint8_t *data;
int offset;
int romsize;
int crc_ok;
uint16_t crc;
int has_jrc_header;
struct jrc_header jrc;
};
void loadJrcHeader(uint8_t *data, struct jrc_header *dst)
{
memset(dst, 0, sizeof(struct jrc_header));
memcpy(dst->signature, data, sizeof(dst->signature)-1);
memcpy(dst->creator, data+27, sizeof(dst->creator)-1);
memcpy(dst->comment, data+59, sizeof(dst->comment)-1);
dst->version[0] = data[460];
dst->version[1] = data[461];
dst->address = data[462] | (data[463] << 8);
dst->adrmask = data[464] | (data[465] << 8);
}
void writeJRCheader(FILE *fptr, const struct jrc_header *jrc)
{
uint8_t tmp[401];
// Signature
fprintf(fptr, JRC_SIG);
// Creator + CR
fprintf(fptr, "%-30s\r\n", JRC_CREATOR);
// Comment (terminated by 0x1A)
memset(tmp, 0x1A, sizeof(tmp));
memcpy(tmp, jrc->comment, strlen(jrc->comment));
fwrite(tmp, 400, 1, fptr);
// EOF
fprintf(fptr, "%c", 0x1A);
// Version
fwrite(jrc->version, 2, 1, fptr);
// Address
tmp[0] = jrc->address;
tmp[1] = jrc->address >> 8;
fwrite(tmp, 2, 1, fptr);
// Address mask
tmp[0] = jrc->adrmask;
tmp[1] = jrc->adrmask >> 8;
fwrite(tmp, 2, 1, fptr);
// Reserved
memset(tmp, 0x00, 46);
fwrite(tmp, 46, 1, fptr);
}
void buildJRCHeader(struct jrc_header *jrc, uint16_t address, uint16_t mask)
{
memset(jrc, 0, sizeof(struct jrc_header));
jrc->version[0] = 1;
jrc->version[1] = 0;
jrc->address = address;
jrc->adrmask = mask;
}
void printJrcHeader(const struct jrc_header *jrc)
{
printf("Creator.........: %s\n", jrc->creator);
printf("Comment.........: %s\n", jrc->comment);
printf("Image version...: %d.%d\n", jrc->version[0], jrc->version[1]);
printf("Segment address.: 0x%04x\n", jrc->address);
printf("Address mask....: 0x%04x\n", jrc->adrmask);
}
uint16_t crc_xmodem_update(uint16_t crc, uint8_t data)
{
int i;
crc = crc ^ ((uint16_t)data << 8);
for (i=0; i<8; i++)
{
if (crc & 0x8000)
crc = (crc << 1) ^ 0x1021;
else
crc <<= 1;
}
return crc;
}
uint16_t crc_compute(const unsigned char *data, int len)
{
uint16_t crc = 0xFFFF;
int i;
for (i=0; i<len; i++) {
crc = crc_xmodem_update(crc, *data);
data++;
}
return crc;
}
int crc_check(unsigned char *data, int checklen, int verbose)
{
uint16_t crc = 0xFFFF;
crc = crc_compute(data, checklen);
if (crc != 0) {
fprintf(stderr, "CRC ERROR 0x%04x\n", crc);
return -1;
}
if (verbose) {
printf("CRC OK\n");
}
return 0;
}
int writeROM(const char *out_filename, struct pcjr_rom *rom, int jrc_format)
{
FILE *fptr;
fptr = fopen(out_filename, "wb");
if (!fptr) {
perror(out_filename);
return -1;
}
if (jrc_format) {
if (!rom->has_jrc_header) {
buildJRCHeader(&rom->jrc, 0xE000, 0x0000);
}
writeJRCheader(fptr, &rom->jrc);
}
if (fwrite(rom->data + rom->offset, rom->romsize, 1, fptr) != 1) {
perror("Error writing ROM data\n");
fclose(fptr);
return -1;
}
fclose(fptr);
return 0;
}
/**
* \param from_data The source data (eg: read from a file) to load the ROM from.
* \param size The size of the source data
* \param dst The destination structure
* \param verbose Verbose output if non-zero
*/
int loadRom(unsigned char *from_data, int size,struct pcjr_rom *dst, int verbose, int fix_size)
{
uint8_t magic[2] = { 0x55, 0xAA };
memset(dst, 0, sizeof(struct pcjr_rom));
dst->data = from_data;
// Check for presence of a JRC file header
if (size >= 512) {
if (0 == memcmp(JRC_SIG, from_data, strlen(JRC_SIG))) {
loadJrcHeader(from_data, &dst->jrc);
if (verbose) {
printf("Detected a JRC header\n");
printJrcHeader(&dst->jrc);
}
dst->offset = 512; // skip the header
}
}
//
// [0-1] : 55AA
// [2] : Length (length / 512)
// code!
//
if (size < (dst->offset + 3)) {
fprintf(stderr, "ROM too small to be valid\n");
return -1;
}
if (memcmp(&from_data[dst->offset], magic, sizeof(magic))) {
fprintf(stderr, "ROM does not start with 55AA\n");
return -1;
}
if (fix_size) {
dst->romsize = (size-dst->offset);
from_data[dst->offset + 2] = dst->romsize / 512;
if (verbose) {
printf("Patched size byte to %d\n", from_data[dst->offset + 2]);
}
}
else {
dst->romsize = from_data[dst->offset + 2] * 512;
if (dst->romsize < 1) {
fprintf(stderr, "Error: A rom size of 0 does not make sense\n");
return -1;
}
}
if (verbose) {
printf("File size: %d\n", size);
printf("Offset: %d\n", dst->offset);
printf("ROM size: %d\n", dst->romsize);
}
// The file could be larger if, for instance, a 16K game was
// burned to a 32K eeprom. But the file may not be smaller.
if (dst->romsize > (size - dst->offset)) {
fprintf(stderr, "Error: ROM declares a size larger than the file\n");
return -1;
}
dst->crc = crc_compute(from_data + dst->offset, dst->romsize);
if (dst->crc == 0) {
dst->crc_ok = 1;
}
return 0;
}
void setRomCRC(struct pcjr_rom *rom, uint16_t crc)
{
rom->data[rom->offset + rom->romsize - 2] = crc >> 8;
rom->data[rom->offset + rom->romsize - 1] = crc;
}
void printHelp(void)
{
printf("jrromchk, part of Booterify version %s\n\n", VERSION);
printf("Usage: ./jrromchk [options] inputFile\n");
printf("\n");
printf(" -h Print usage information and exit\n");
printf(" -v Enable verbose output\n");
printf(" -p Compute CRC and patch the file\n");
printf(" -s Fix the size byte based on input file size\n");
printf(" -i Display information about the file (default)\n");
printf(" -o file Write (modified) ROM to file (default: Binary format)\n");
printf(" -j Enable JRC format output (use with -o)\n");
}
int main(int argc, char **argv)
{
FILE *fptr;
unsigned char *data;
long size;
int alloc_size;
int res;
int opt;
int patch_crc = 0;
int verbose = 0;
int fix_size = 0;
int jrc_output = 0;
struct pcjr_rom rom;
const char *outfilename = NULL;
while ((opt = getopt(argc, argv, "vipho:sj")) != -1) {
switch(opt)
{
case 'j':
jrc_output = 1;
break;
case 'o':
outfilename = optarg;
break;
case 's':
fix_size = 1;
break;
case 'v':
verbose =1;
break;
case 'h':
printHelp();
return 0;
case 'i':
break;
case 'p':
patch_crc = 1;
break;
default:
fprintf(stderr, "Unknown option. Try -h\n");
return 1;
}
}
if (optind >= argc) {
printf("No file specified\n");
return 1;
}
fptr = fopen(argv[optind], "rb");
if (!fptr) {
perror(argv[1]);
return -1;
}
fseek(fptr, 0, SEEK_END);
size = ftell(fptr);
alloc_size = size;
fseek(fptr, 0, SEEK_SET);
// Round up to 512 bytes
if (fix_size) {
alloc_size = (size + 511) / 512 * 512;
if (verbose) {
printf("Input file size is %d\n", (int)size);
printf("Rounded up to %d\n", (int)alloc_size);
}
}
data = malloc(alloc_size);
if (!data) {
perror("could not allocate memory for file");
fclose(fptr);
return -1;
}
memset(data, 0xff, alloc_size);
if (1 != fread(data, size, 1, fptr)) {
perror("could not read file");
free(data);
fclose(fptr);
return -1;
}
res = loadRom(data, alloc_size, &rom, verbose, fix_size);
if (res) {
printf("ROM INVALID\n");
fclose(fptr);
return -1;
}
if (rom.crc_ok) {
printf("ROM OK\n");
res = 0;
} else {
// CRC invalid. But do we correct it?
if (patch_crc) {
uint16_t crc = crc_compute(rom.data + rom.offset, rom.romsize-2);
setRomCRC(&rom, crc);
res = loadRom(data, alloc_size, &rom, verbose, 0);
if (res || !rom.crc_ok) {
printf("ROM INVALID - Failed to fix it? (bug?)\n");
res = -1;
} else {
printf("ROM OK (CRC PATCHED)\n");
res = 0;
}
} else {
printf("ROM INVALID (BAD CRC)\n");
res = -1;
}
}
// at this point, if res is 0, we have a perfectly valid ROM to
// write to the output file, if specified.
if ((res == 0) && outfilename) {
res = writeROM(outfilename, &rom, jrc_output);
if (res == 0) {
printf("Wrote %s\n", outfilename);
} else {
fprintf(stderr, "Could not write ROM\n");
}
}
fclose(fptr);
return res;
}