-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloader64.c
347 lines (248 loc) · 9.29 KB
/
loader64.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
/* loader64.c
libftdi Everdrive 64 USB-tool
by saturnu
*/
#include <stdio.h>
#include <libftdi1/ftdi.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "gopt.h"
#include "loader64.h"
int main( int argc, const char **argv )
{
//usb buffer
char send_buff[512];
char recv_buff[512];
int arg_fail=1;
//rom filename
const char *filename;
//verbosity level 0-2
int verbosity;
int i,print_help;
void *options= gopt_sort( & argc, argv, gopt_start(
gopt_option( 'h', 0, gopt_shorts( 'h' ), gopt_longs( "help" )),
gopt_option( 'z', 0, gopt_shorts( 'z' ), gopt_longs( "version" )),
gopt_option( 'v', GOPT_REPEAT, gopt_shorts( 'v' ), gopt_longs( "verbose" )),
gopt_option( 'r', 0, gopt_shorts( 'r' ), gopt_longs( "read" )),
gopt_option( 'w', 0, gopt_shorts( 'w' ), gopt_longs( "write" )),
gopt_option( 'p', 0, gopt_shorts( 'p' ), gopt_longs( "pifboot" )),
gopt_option( 'f', GOPT_ARG, gopt_shorts( 'f' ), gopt_longs( "file" ))));
if( gopt( options, 'h' ) ){
fprintf( stdout, "Syntax: sudo ./loader64 [options] ...\n\n");
fprintf( stdout, "loader64 - Everdrive64 USB-tool\n" );
fprintf( stdout, "by saturnu <tt@anpa.nl>\n\n" );
fprintf( stdout, " -h, --help\t\tdisplay this help and exit\n" );
fprintf( stdout, " -v, --verbose\t\tverbose\n" );
fprintf( stdout, " -f, --file=rom.z64\trom in z64 format\n" );
//TODO:
//fprintf( stdout, " -r, --read\t\tread from sdram\n" );
fprintf( stdout, " -w, --write\t\twrite to sdram\n" );
fprintf( stdout, " -p, --pifboot\t\tsimulate pifboot CIC-6102\n" );
fprintf( stdout, " -z, --version\t\tversion\n" );
exit( EXIT_SUCCESS );
}
//show info without options
print_help=1;
if( gopt( options, 'p' ) || gopt( options, 'w' ) || gopt( options, 'r') || gopt( options, 'z') || gopt( options, 'h') || gopt( options, 'v') ){
print_help = 0;
if( gopt( options, 'p' ) || gopt( options, 'w' ) )
arg_fail = 0;
}
if( gopt( options, 'w' ) && gopt( options, 'r' )){
fprintf( stdout, "error: could not read and write at the same time\n" );
exit( EXIT_SUCCESS );
}
if( gopt( options, 'w' ) && gopt( options, 'p' )){
fprintf( stdout, "error: use pifboot separately\n" );
exit( EXIT_SUCCESS );
}
if( gopt( options, 'z' ) ){
fprintf( stdout, "loader64 version v%d.%d\n", MAJOR_VERSION, MINOR_VERSION );
exit( EXIT_SUCCESS );
}
verbosity = gopt( options, 'v' );
if( verbosity > 1 )
fprintf( stderr, "being really verbose\n" );
else if( verbosity )
fprintf( stderr, "being verbose\n" );
//options are ok - init ftdi
if(!arg_fail){
int ret;
struct ftdi_context *ftdi;
if ((ftdi = ftdi_new()) == 0)
{
fprintf(stderr, "ftdi_new failed\n");
return EXIT_FAILURE;
}
if ((ret = ftdi_usb_open(ftdi, USB_VENDOR, USB_DEVICE)) < 0)
{
fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
ftdi_free(ftdi);
return EXIT_FAILURE;
}else{
//read/write timeout e.g. 500ms
ftdi->usb_read_timeout = USB_READ_TIMEOUT;
ftdi->usb_write_timeout = USB_WRITE_TIMEOUT;
}
if (ftdi->type == TYPE_R)
{
unsigned int chipid;
if(verbosity >= 1){
printf("ftdi_read_chipid: %d\n", ftdi_read_chipid(ftdi, &chipid));
printf("FTDI chipid: %X\n", chipid);
}
}
//init usb transfer buffer
memset(send_buff, 0, 512);
memset(recv_buff, 0, 512);
send_buff[0]='C';
send_buff[1]='M';
send_buff[2]='D';
send_buff[3]='T'; //test
int ret_s = ftdi_write_data(ftdi, send_buff, 512);
if(verbosity >= 1)
printf("send: %i bytes\n",ret_s);
int ret_r = ftdi_read_data(ftdi, recv_buff, 512);
if(verbosity >= 1)
printf("recv: %i bytes\n",ret_r);
if(recv_buff[3]=='k'){
printf("init test: ok\n");
}
else{
printf("init test: faild - ED64 not running?\n");
ftdi_free(ftdi);
exit( EXIT_SUCCESS );
}
memset(send_buff, 0, 512);
memset(recv_buff, 0, 512);
ret_s = 0;
ret_r = 0;
//pifboot
if( gopt( options, 'p' ) ){
send_buff[0]='C';
send_buff[1]='M';
send_buff[2]='D';
send_buff[3]='S'; //pif boot
ret_s = ftdi_write_data(ftdi, send_buff, 512);
if(verbosity >= 1)
printf("send: %i bytes\n",ret_s);
printf("pif simulation instructed...\n");
}else //write
if( gopt( options, 'w' ) ){
if( gopt_arg( options, 'f', & filename ) && strcmp( filename, "-" ) ){
FILE *fp;
fp=fopen(filename, "rb");
int fsize;
struct stat st;
stat(filename, &st);
fsize = st.st_size;
if(verbosity >= 1)
printf("test_size: %d\n",fsize);
int length = (int) fsize;
if(verbosity >= 1)
printf("length: %d\n",length);
if (((length / 0x10000) * 0x10000) != fsize) {
length = (int) (((fsize / 0x10000) * 0x10000) + 0x10000);
}
if(verbosity >= 2)
printf("length_buffer: %d\n",length);
//FILE_CHUNK default 0x8000
char buffer[FILE_CHUNK];
memset(buffer, 0, FILE_CHUNK);
if(verbosity >= 2)
printf("buffer created\n");
//tiny rom fill-mode
if (length < 0x200000){
if(verbosity >= 1)
printf("needs filling\n");
send_buff[0]='C';
send_buff[1]='M';
send_buff[2]='D';
send_buff[3]='F'; //fill
ret_s = ftdi_write_data(ftdi, send_buff, 512);
if(verbosity >= 1)
printf("send: %i bytes\n",ret_s);
sleep(1);
int ret_r = ftdi_read_data(ftdi, recv_buff, 512);
if(verbosity >= 1)
printf("recv: %i bytes\n",ret_r);
if(recv_buff[3]=='k'){
printf("fill test: ok\n");
memset(send_buff, 0, 512);
memset(recv_buff, 0, 512);
ret_s = 0;
ret_r = 0;
}else{
printf("fill test: error\n");
ftdi_free(ftdi);
exit( EXIT_SUCCESS );
}
}
send_buff[0]='C';
send_buff[1]='M';
send_buff[2]='D';
send_buff[3]='W'; //write
send_buff[4]=0; //offset
send_buff[5]=0; //offset
send_buff[6] = (char) ((( length) / 0x200) >> 8); //length
send_buff[7] = (char) (length / 0x200); //length
ret_s = ftdi_write_data(ftdi, send_buff, 512);
if(verbosity >= 1)
printf("send write cmd: %i bytes\n",ret_s);
//now write in 0x8000 chunks -> 32768
printf("sending...\n");
int s;
for(s = 0;s < length; s += 0x8000){
if (s == 0x2000000) //step 1024
{
memset(send_buff, 0, 512);
memset(recv_buff, 0, 512);
ret_s = 0;
ret_r = 0;
send_buff[0]='C';
send_buff[1]='M';
send_buff[2]='D';
send_buff[3]='W'; //write
send_buff[4]=0x40; //32mb offset
send_buff[5]=0; //offset
send_buff[6] = (char) (( ( length - 0x2000000) / 0x200) >> 8); //length
send_buff[7] = (char) (( length - 0x2000000) / 0x200); //length
ret_s = ftdi_write_data(ftdi, send_buff, 512);
if(verbosity >= 1)
printf("send offset cmd: %i bytes\n",ret_s);
}
//read parts to memory
int fret = fread(buffer, sizeof(buffer[0]), sizeof(buffer)/sizeof(buffer[0]), fp);
ret_s = ftdi_write_data(ftdi, buffer, 0x8000);
if ((s % 0x80000) == 0)
{
if(verbosity >= 1 && s!=0)
printf("part sent: %i\n",s);
}
}
printf("upload done...\n", argv[0]);
}
}else //TODO: read
if( gopt( options, 'r' ) ){
send_buff[0]='C';
send_buff[1]='M';
send_buff[2]='D';
send_buff[3]='R'; //read
}
if ((ret = ftdi_usb_close(ftdi)) < 0)
{
fprintf(stderr, "unable to close ftdi device: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
ftdi_free(ftdi);
return EXIT_FAILURE;
}
ftdi_free(ftdi);
}
if(print_help){
printf("%s: missing operand\n", argv[0]);
printf("Try '%s --help' for more information.\n", argv[0]);
}
gopt_free( options );
return EXIT_SUCCESS;
}