-
Notifications
You must be signed in to change notification settings - Fork 5
/
cartio.c
259 lines (216 loc) · 6.2 KB
/
cartio.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
/*
* Based on f2a by Ulrich Hecht <uli@emulinks.de>
* if2a by D. Gauchard <deyv@free.fr>
* F2A Ultra support by Vincent Rubiolo <vincent.rubiolo@free.fr>
* Licensed under the terms of the GNU Public License version 2
*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "libf2a.h"
#include "cartio.h"
#include "cartrom.h"
#include "cartutils.h"
#include "drivers/cart-f2a/f2aio.h" // DEFAULT_ROMBLOCKSIZE_LOG2
// defined by binware.c
extern binware_s binware_f2a_loader_pro [];
extern binware_s binware_f2a_loader_ultra [];
extern binware_s binware_f2a_usb_firmware [];
extern binware_s binware_f2a_multiboot [];
extern binware_s binware_f2a_splash [];
binware_s loader = { NULL, 0, NULL };
binware_s firmware = { NULL, 0, NULL };
binware_s multiboot = { NULL, 0, NULL };
binware_s splash = { NULL, 0, NULL };
cartio_s cartio = { .setup = 0, };
const char* cart_type_str (cart_type_e cart_type)
{
switch (cart_type)
{
case CART_TYPE_F2A_PRO: return "F2A-Pro";
case CART_TYPE_F2A_ULTRA: return "F2A-Ultra";
case CART_TYPE_TEMPLATE: return ">template<";
default: return "(Not detected)";
}
}
void cart_init (void)
{
cart_trim_always = 0;
cart_trim_allowed = 1;
cart_correct_header_allowed = 1;
cart_burn_without_comparison = 0;
cart_io_sim = 0;
cart_verbose = 0;
cart_size_mbits = -1; /* autodetect, see cart_get_type() */
cart_write_block_size_log2 = DEFAULT_WRITEBLOCKSIZE_LOG2;
cart_rom_block_size_log2 = DEFAULT_ROMBLOCKSIZE_LOG2;
cart_thorough_compare = 0;
}
void cart_reinit (void)
{
// Re/Initialize all global library settings.
check_endianness();
if (!cartio.setup)
{
printerr("Internal error, cart input/output functions not initialized.\n");
exit(1);
}
cartio.linker_reinit();
}
int cart_select_firmware (const char* binware_file)
{
return cartio.select_firmware(binware_file);
}
int cart_select_linker_multiboot (const char* binware_file)
{
return cartio.select_linker_multiboot(binware_file);
}
int cart_select_splash (const char* binware_file)
{
return cartio.select_splash(binware_file);
}
int cart_select_loader (cart_type_e cart_type, const char* binware_file)
{
return cartio.select_loader(cart_type, binware_file);
}
void cart_exit (int status)
{
cartio.linker_release();
exit(status);
}
int cart_connect (void)
{
if (cart_io_sim > 1)
return 0;
return cartio.linker_connect();
}
int cart_check_or_init_linker (void)
{
return cartio.linker_multiboot();
}
cart_type_e cart_autodetect (int* size_mbits, int* write_block_size_log2, int* rom_block_size_log2)
{
return cartio.autodetect(size_mbits, write_block_size_log2, rom_block_size_log2);
}
int cart_user_multiboot (const char* file)
{
return cartio.user_multiboot(file);
}
int cart_read_mem (unsigned char* data, int address, int size)
{
return cartio.read(data, address, size);
}
int cart_direct_write (const unsigned char* data, int base, int offset, int size, int blocksize, int first_offset, int overall_size)
{
return cartio.direct_write(data, base, offset, size, blocksize, first_offset, overall_size);
}
int cart_read_mem_to_file (const char* file, int address, int size, enum read_type_e read_type)
{
FILE* f;
unsigned char data[size];
if (cart_read_mem(data, address, size) < 0)
return -1;
switch (read_type)
{
case READ_MANY: // to read several memory areas into file
if ((f = fopen(file, "ab")) == NULL)
{
printerrno(file);
return -1;
}
break;
case READ_ONCE:
if ((f = fopen(file, "wb")) == NULL)
{
printerrno(file);
return -1;
}
break;
default:
printerr("This is bad. Should have reached either WRITE_MANY or WRITE_ONCE\n");
return -1;
}
if (fwrite(data, size, 1, f) != 1)
{
printerrno(file);
fclose(f);
return -1;
}
fclose(f);
return 0;
}
int cart_burn (int cart_base, int cart_offset, const unsigned char* rom, int rom_offset, int rom_size)
{
int initial_rom_offset = cart_offset + rom_offset;
// data flash addresses for cartio.direct_write() process must not cross MAXBURNCHUNK multiples
int size_to_burn = rom_size;
while (size_to_burn > 0)
{
// chunksize is the maximum size such that offset/MAXBURNCHUNK
// and (offset+chunksize-1)/MAXBURNCHUNK are equals:
int chunksize = (((cart_offset + rom_offset) + MAXBURNCHUNK) / MAXBURNCHUNK) * MAXBURNCHUNK - (cart_offset + rom_offset);
if (chunksize > size_to_burn)
chunksize = size_to_burn;
assert(chunksize > 0);
int offset_burn = cart_offset + rom_offset;
int size_burn = chunksize;
int offset_save = offset_burn;
int size_save = size_burn;
if (cart_verbose)
print("Burning from 0x%x to 0x%x", cart_base + offset_burn, cart_base + offset_burn + size_burn);
adjust_burn_addresses(&offset_burn, &size_burn);
if (cart_verbose)
{
if ((offset_save != offset_burn) || (size_save != size_burn))
print(" (adjusted: from 0x%x to 0x%x)\n", cart_base + offset_burn, cart_base + offset_burn + size_burn);
else
print("\n");
}
if (cartio.direct_write(&rom[rom_offset], cart_base, offset_burn, size_burn, CART_WRITE_BLOCK_SIZE, initial_rom_offset, rom_size) < 0)
return -1;
rom_offset += chunksize;
size_to_burn -= chunksize;
if (cart_verbose)
print("\n");
}
return 0;
}
void print_array (const unsigned char* array, int size)
{
// prints contents of an array
int idx;
for (idx = size - 1 ; idx >= 0; idx--)
print("%c ", (unsigned char)array[idx]);
print("\n");
}
void print_array_dual (const unsigned char* array, int size)
{
// prints contents of an array
int idx;
int block;
#define NBLOCKS 4
size /= sizeof(u_int32_t);
for (block = 0; block < size; block += NBLOCKS)
{
char ascii4[6];
char ascii[128] = { 0, };
for (idx = block; idx < block + NBLOCKS; idx++)
{
#define H(v) ((unsigned char)(v))
// reads 32 bits word numbered idx and converts it to host endian format if necessary
// (assuming gba/arm's endianness is little-endian like network's convention)
u_int32_t v;
if (idx < size)
v = ntoh32(((u_int32_t*)array)[idx]);
else
v = 0xffffffff;
// print big endian first
print("%02x %02x %02x %02x - ", H(v), H(v>>8), H(v>>16), H(v>>24));
sprintf(ascii4, "%c%c%c%c ", ASCII(v), ASCII(v>>8), ASCII(v>>16), ASCII(v>>24));
strcat(ascii, ascii4);
}
print("%s\n", ascii);
}
}