/
ards_game_ls.c
559 lines (454 loc) · 13.2 KB
/
ards_game_ls.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
/*
* ARDS Game Listing utility
*
* Description:
* Given an Action Replay DS ROM dump, list all addresses of possible games
* to dump. These addresses can be fed into "ards_game_to_xml" to export an
* XML codelist.
*
* This program will skip all "game lists" and look directly in memory for
* magic numbers in hopes it finding a game.
*
* Author:
* Clara Nguyen (@iDestyKK)
*/
// C Includes
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
// ARDS Utils
#include "../lib/ards_util/io.h"
// CNDS (Clara Nguyen's Data Structures)
#include "../lib/CN_Vec/cn_vec.h"
#include "../lib/CN_Map/cn_cmp.h"
#include "../lib/CN_Map/cn_map.h"
// ----------------------------------------------------------------------------
// CNDS Configuration Functions {{{1
// ----------------------------------------------------------------------------
/*
* Map destructor for freeing a C-String key in a CN_Map.
*/
void destruct_key(CNM_NODE *node) {
char *v = *(char **) node->key;
if (v != NULL)
free(v);
}
// ----------------------------------------------------------------------------
// Basic Argument Parser {{{1
// ----------------------------------------------------------------------------
typedef struct ARGS_T {
uint8_t flag_allow_dup,
flag_error,
flag_skip_name,
flag_rescue,
flag_warning;
} args_t;
void print_help(int argc, char **argv) {
printf("usage: %s [-dehnrw] IN_ARDS.nds\n", argv[0]);
printf("Listing utility for game addresses in an Action Replay DS ROM "
"dump.\n\n");
printf("Optional arguments are:\n\n");
printf("\t-d\tAllow duplicates. Won't skip reading the same game even if "
"it's present\n\t\tin multiple locations in the same ROM. By "
"default, duplicates are\n\t\tskipped, and it's determined by the "
"Game ID (XXXX-YYYYYYYY).\n\n");
printf("\t-e\tPrints errors. By default, this will only print out games "
"where a code\n\t\tsection check looks correct. With this flag, it"
" will additionally print\n\t\terrors to stderr.\n\n");
printf("\t-h\tPrints this help prompt in the terminal and then "
"terminates.\n\n");
printf("\t-n\tSkips name reading. By default, this will read a game "
"header, validate\n\t\tthe code section, and then run 2n C-Style "
"string reads. This argument\n\t\ttells it to skip the final step "
"and try to read the next game through\n\t\tthose string "
"sections.\n\n");
printf("\t-r\tRescue mode. Skips the game list and tries to search for "
"games via a\n\t\tdeep search. Brute force. Searches all bytes after "
"0x00054000. Will be\n\t\tmuch slower.\n\n");
printf("\t-w\tShows warnings while reading. Prints to stderr.\n");
exit(0);
}
void parse_flags(int argc, char **argv, args_t *obj) {
int i, j, len;
// Defaults
obj->flag_allow_dup = 0;
obj->flag_error = 0;
obj->flag_skip_name = 0;
obj->flag_rescue = 0;
obj->flag_warning = 0;
// Go through every argument and read characters
for (i = 1; i < argc; i++) {
// Skip if not a valid flag header
if (argv[i][0] != '-')
continue;
len = strlen(argv[i]);
for (j = 1; j < len; j++) {
switch (argv[i][j]) {
case 'd':
// Allow duplicate games
obj->flag_allow_dup = 1;
break;
case 'e':
// Show errors
obj->flag_error = 1;
break;
case 'h':
// Shows help prompt and terminate
print_help(argc, argv);
break;
case 'n':
// Skips name reading
obj->flag_skip_name = 1;
break;
case 'r':
// Enter Rescue Mode
obj->flag_rescue = 1;
break;
case 'w':
// Show warnings
obj->flag_warning = 1;
break;
default:
// Invalid Flag
fprintf(
stderr,
"WARN: Invalid flag \"%c\" was given. Ignoring...\n",
argv[i][j]
);
break;
}
}
}
}
// ----------------------------------------------------------------------------
// Verification Functions {{{1
// ----------------------------------------------------------------------------
int verify_code_segment(
unsigned char *buf,
size_t len,
uint16_t num_codes,
args_t *args,
size_t *err_pos,
uint8_t *err_val
) {
size_t pos, i, j, c_found;
ar_flag_t flag, in_flag;
uint16_t num , in_num ;
// For every code...
for (i = pos = c_found = 0; i < num_codes; i++) {
if (pos > len)
// Exceeded buffer size
return 3;
// Code/Folder header via reinterpret casting
flag = (ar_flag_t) (*(uint8_t *) &buf[pos ]);
num = *(uint16_t *) &buf[pos + 2] ;
pos += 4;
// Act based on flag
switch (flag & 0x03) {
case AR_FLAG_CODE:
// All codes are 8 bytes. So n * 8.
pos += 8 * num;
c_found++;
break;
case AR_FLAG_FOLDER:
// ARDS doesn't allow nested folders. Cheat and avoid recursion
for (j = 0; j < num; j++) {
if (pos > len)
// Exceeded buffer size
return 3;
// They better be codes or else...
in_flag = (ar_flag_t) (*(uint8_t *) &buf[pos ]);
in_num = *(uint16_t *) &buf[pos + 2] ;
if ((in_flag & 0x03) != AR_FLAG_CODE) {
// Flag inside folder was not a code
*err_pos = pos;
return 2;
}
pos += 4 + (8 * in_num);
c_found++;
if (c_found == num_codes && j < num - 1) {
// More codes than mentioned in header
*err_pos = pos;
return 4;
}
}
break;
case AR_FLAG_TERMINATE:
if (c_found == num_codes)
// Looks good to me
return 0;
else {
// More codes than mentioned in header
*err_pos = pos;
return 4;
}
break;
default:
// Invalid flag was found
*err_pos = pos - 4;
*err_val = flag;
return 1;
}
}
// Nothing is wrong
return 0;
}
// ----------------------------------------------------------------------------
// Regular Mode {{{1
// ----------------------------------------------------------------------------
/*
* Use game list at 0x00044000 to get location of each game. Then jump to each
* spot to get game information.
*/
int data_iterate(int argc, char **argv, args_t *args) {
FILE *fp;
ar_game_list_node header_list, *it;
ar_game_info_t header_game;
CN_VEC game_list; // vector<ar_game_list_node>
char *title;
size_t i;
title = NULL;
// Prepare CN_Vec for insertion of "ar_game_list_node"s
game_list = cn_vec_init(ar_game_list_node);
// Setup file for traversal
// The first argument without a "-" is the filename.
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
break;
}
fp = fopen(argv[i], "rb");
// The code list is at 0x00044000
fseek(fp, 0x44000, SEEK_SET);
// Read in "ar_game_list_node"s until "FF FF FF FF"
while (1) {
// Read 32 bytes
fread(&header_list, sizeof(struct AR_GAME_LIST_NODE), 1, fp);
// Check header
if (header_list.magic == 0xFFFFFFFFU) {
// FF FF FF FF = End of list
break;
}
else
if (header_list.magic != 0x00000000U) {
// Anything other than "00 00 00 00" = not a game
continue;
}
// Guaranteed to be "00 00 00 00" at this point. Insert into CN_VEC
cn_vec_push_back(game_list, &header_list);
}
// Now go through each game and print out information
cn_vec_traverse(game_list, it) {
// Jump to spot in memory
fseek(fp, 0x40000 + (it->location << 8), SEEK_SET);
// Read in the game header (32 bytes)
fread(&header_game, sizeof(struct AR_GAME_INFO_T), 1, fp);
// Jump to the title and read the name of the game
fseek(fp, header_game.offset_text - 32 + 1, SEEK_CUR);
title = file_read_string(fp);
// Print info
printf(
"0x%08x - %s - %s\n",
0x40000 + (it->location << 8),
it->ID.raw,
title
);
// Clean up title, since we are done with it
free(title);
}
// Clean up
fclose(fp);
cn_vec_free(game_list);
}
// ----------------------------------------------------------------------------
// Rescue Mode {{{1
// ----------------------------------------------------------------------------
/*
* Search bytes from 0x00054000 onwards in each chunk for information on codes.
* This bypasses the code list at 0x00044000 entirely and tries to make sense
* out of bytes and magic numbers it finds.
*/
int data_rescue(int argc, char **argv, args_t *args) {
FILE *fp;
ar_game_info_t header;
size_t pos, i, fsize, err_at;
uint8_t err_val;
char *name;
char *shit;
unsigned char *buf;
int status, printable;
CN_MAP game_ids;
CNM_ITERATOR game_id_it;
char *game_id_key, *key_tmp, dummy;
// Defaults
name = NULL;
shit = NULL;
game_id_key = (char *) calloc(14, sizeof(char));
key_tmp = NULL;
// Setup CNDS for keeping track of Game IDs
game_ids = cn_map_init(char *, char, cn_cmp_cstr);
cn_map_set_func_destructor(game_ids, destruct_key);
// Setup file for traversal
// The first argument without a "-" is the filename.
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
break;
}
fp = fopen(argv[i], "rb");
// Get size of file
fseek(fp, 0, SEEK_END);
fsize = ftell(fp);
// First game should be at 0x00054000
fseek(fp, 0x54000, SEEK_SET);
// For every "game"...
while (1) {
// Fix address. At 0x00XYYYYY, the "YYYYY" should be 0x54000 or above.
pos = ftell(fp);
if (pos & 0x000FFFFF < 0x54000) {
pos = (pos & 0xFFF00000) | 0x54000;
fseek(fp, pos, SEEK_SET);
}
// If we are past the end of the file, kill it
if (ftell(fp) >= fsize)
break;
// Read header
file_read_type(fp, ar_game_info_t, header);
// Check Magic Number
if (header.magic != 0x001C0001 || header.nx20 != 0x0020) {
fseek(fp, pos + 1, SEEK_SET);
continue;
}
// Check if duplicate, and only if the flag "-d" isn't specified
if (!args->flag_allow_dup) {
// Read in the Game ID (XXXX-YYYYYYYY)
sprintf(game_id_key, "%.4s-%08X", header.ID, header.N_CRC32);
// Search for duplicates
cn_map_find(game_ids, &game_id_it, &game_id_key);
if (cn_map_at_end(game_ids, &game_id_it)) {
// No duplicates found. Insert into the CN_Map
key_tmp = strdup(game_id_key);
cn_map_insert(game_ids, &key_tmp, &dummy);
printable = 1;
}
else {
// Duplicate was found. Still process, but don't print.
printable = 0;
if (args->flag_warning) {
fprintf(
stderr,
"Warning 0x%08x: Duplicate Game ID \"%.4s-%08X\"\n",
pos,
header.ID,
header.N_CRC32
);
}
}
}
// Read bytes segment and check if it's correct
buf = (unsigned char *)
malloc(sizeof(unsigned char) * header.offset_strlen - 32);
fread(&buf[0], sizeof(unsigned char), header.offset_strlen - 32, fp);
status = verify_code_segment(
buf,
header.offset_strlen - 32,
header.num_codes,
args,
&err_at,
&err_val
);
free(buf);
if (status != 0) {
fseek(fp, pos + 1, SEEK_SET);
if (args->flag_error == 1) {
switch (status) {
case 1:
fprintf(
stderr,
"Error 0x%08x + 0x%08x: %s (%d)\n",
pos,
err_at + 32,
"Invalid flag was found",
err_val
);
break;
case 2:
fprintf(
stderr,
"Error 0x%08x + 0x%08x: %s\n",
pos,
err_at + 32,
"Flag inside folder was not a code"
);
break;
case 3:
fprintf(
stderr,
"Error 0x%08x + 0x%08x: %s\n",
pos,
err_at + 32,
"Exceeded buffer size"
);
break;
case 4:
fprintf(
stderr,
"Error 0x%08x + 0x%08x: %s\n",
pos,
err_at + 32,
"More codes than mentioned in header"
);
break;
default:
fprintf(
stderr,
"Error 0x%08x + 0x%08x: %s\n",
pos,
err_at + 32,
"Undocumented error"
);
break;
}
}
continue;
}
// Read name, if possible
fseek(fp, pos + header.offset_text + 1, SEEK_SET);
if (name != NULL) free(name); name = file_read_string(fp);
if (shit != NULL) free(shit); shit = file_read_string(fp);
// Print
if (printable)
printf("0x%08x - %s - %s\n", pos, game_id_key, name);
// Skip all codes afterwards
if (args->flag_skip_name != 1) {
for (i = 0; i < header.num_codes; i++) {
if (shit != NULL) free(shit); shit = file_read_string(fp);
if (shit != NULL) free(shit); shit = file_read_string(fp);
}
}
}
// We're done here. Clean up
fclose(fp);
if (name != NULL) free(name );
if (shit != NULL) free(shit );
if (game_id_key != NULL) free(game_id_key);
cn_map_free(game_ids);
return 0;
}
// ----------------------------------------------------------------------------
// Main Function {{{1
// ----------------------------------------------------------------------------
int main(int argc, char **argv) {
// Argument check
if (argc < 2) {
fprintf(stderr, "usage: %s [-dehnrw] IN_ARDS.nds\n", argv[0]);
return 1;
}
// Parse arguments
args_t args;
parse_flags(argc, argv, &args);
if (args.flag_rescue)
return data_rescue(argc, argv, &args);
else
return data_iterate(argc, argv, &args);
// Have a nice day, except this shouldn't ever happen
return 0;
}