-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserxfer.c
358 lines (270 loc) · 7.46 KB
/
serxfer.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
/* Code to transfer an IWRAM image to the GBA via
a plain serial connection, communicating with 2ndloader
monitor code in the GBA. (The image is not NECESSARILY
a multiboot image, since the loader will download up to 256K into
IWRAM and then simply branch to the start of it. This will work
for MB images, but you can leave out the header if you want to
save 50 bytes or something. ;-D
Part of Matt'sSerialMultibootCable, (c) Matt Evans, 15th May 2002
Must be distributed with the complete tarball. This code is released
under the GPL.
*/
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <termios.h>
#include <fcntl.h>
#define XFERBLOCK 512
int ser_fd;
FILE *ser_fp;
extern unsigned char Header [];
extern int opendelay;
extern char *device;
void serial_error(char *s)
{
printf(s);
exit(0);
}
void xfer_init(void)
{
struct termios serialsettings;
struct termios serial_originalportsettings;
struct sigaction cleanup;
char linebuff[255];
int nr, l;
unsigned char ch;
char version[255];
int inver;
memset ((void *) &serialsettings, 0, sizeof (struct termios));
/*cleanup.sa_handler = (void (*)()) exit;
sigemptyset (&cleanup.sa_mask);
cleanup.sa_flags = 0;
sigaction (SIGINT, &cleanup, NULL);
sigaction (SIGHUP, &cleanup, NULL);
sigaction (SIGTERM, &cleanup, NULL);
sigaction (SIGSEGV, &cleanup, NULL);
sigaction (SIGBUS, &cleanup, NULL);
*/
ser_fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
printf("[Opening serial..");
if ( (ser_fd = open(device, O_SYNC | O_RDWR)) == -1 )
{
printf("Can't open %s!\n", device);
exit(0);
}
/*fcntl (ser_fd, F_SETFL, 0);*/
if (tcgetattr (ser_fd, &serial_originalportsettings) == -1)
serial_error ("failed to get serial port attributes");
if (cfsetispeed (&serialsettings, B115200)) /* used to be B9600 */
serial_error ("failed to set input baud rate");
if (cfsetospeed (&serialsettings, B115200))
serial_error ("failed to set output baud rate");
serialsettings.c_oflag &= ~OPOST;
serialsettings.c_iflag = IGNPAR | IGNBRK;
serialsettings.c_cflag = (serialsettings.c_cflag & ~CSIZE) | CS8 | CREAD | CLOCAL;
serialsettings.c_cflag &= ~CRTSCTS;
serialsettings.c_cc[VMIN] = 1;
serialsettings.c_cc[VTIME] = 5; /* <-wait time*/
if (tcsetattr (ser_fd, TCSANOW, &serialsettings))
serial_error ("failed to update serial port attributes");
printf(".done, fd = %d.]\n", ser_fd);
//Wait for arduino clones to reset
sleep(opendelay);
/* Check for cable. Sending 255,255,255 will ensure it's in
command mode. '1' will return version string, and '2' puts
it into multilink mode.
*/
ch = 255;
if (write(ser_fd, &ch, 1) != 1)
serial_error("Couldn't write probe byte 1!\n");
if (write(ser_fd, &ch, 1) != 1)
serial_error("Couldn't write probe byte 2!\n");
if (write(ser_fd, &ch, 1) != 1)
serial_error("Couldn't write probe byte 3!\n");
/* Should be in command mode now: ask for version */
ch = 1;
if (write(ser_fd, &ch, 1) != 1)
serial_error("Couldn't write version check byte!\n");
printf("[Checking cable version: \'");
fflush(stdout);
inver = 0;
while (1)
{
if (read(ser_fd, &ch, 1) != 1)
serial_error("Can't read version byte. :(\n");
if (ch == '[')
{
inver = 1;
continue;
}
if ( (inver) && (ch == ']') )
break; /* read whole version string! */
printf("%c", ch);
}
printf("\']\n");
/* Ok.. maybe we've found the cable...... :) */
/* Put it into multiboot mode: */
ch = 2;
if (write(ser_fd, &ch, 1) != 1)
serial_error("Couldn't write MB mode byte!\n");
/* That's it... */
}
void xfer_passthru(void)
{
char ch;
printf("-> Setting cable to passthru at 115k2..");
ch = 255;
if (write(ser_fd, &ch, 1) != 1)
serial_error("Couldn't write escape for passthru!\n");
/* Should be in command mode now: ask for passthru */
ch = 3;
if (write(ser_fd, &ch, 1) != 1)
serial_error("Couldn't write passthru command!\n");
printf(".done!\n\n");
}
void xfer_file(char *fname)
{
FILE *fp;
unsigned char ch;
int len, tlen;
char code[256*1024];
int codelen;
char inbuff[256];
char sumbuff[16];
int sum;
int offset;
int i;
fp = fopen(fname,"rb");
if (!fp)
{
printf("error - no such file: %s\n",fname);
exit(0);
}
codelen = fread(code, 1, 256*1024, fp); //load up to 256k to buffer
printf("-> Loaded %d bytes from %s.\n", codelen, fname);
fclose(fp);
printf("Talking to monitor\n");
ch = '\n';
if (write(ser_fd, &ch, 1) != 1)
serial_error("Couldn't write LF for monitor test!\n");
/* OK, aDDDDDDDD = set adddress, s<XFERBLOCK bytes> = send block etc.
and j = execute stuff */
/* address is 0 by default */
for (offset = 0; offset <= codelen; offset += XFERBLOCK)
{
len = read(ser_fd, &ch, 1);
if (len != 1)
serial_error("couldn't read monitor prompt\n");
if (ch != '>')
{
printf("Monitor prompt was %c instead of >!\n", ch);
exit(0);
}
sum = 0;
ch = 's';
if (write(ser_fd, &ch, 1) != 1)
serial_error("Couldn't 's' for block!\n");
for (i = 0; i < XFERBLOCK; i++)
{
ch = code[offset + i];
sum += ch;
if (write(ser_fd, &ch, 1) != 1)
serial_error("Couldn't write data byte!\n");
}
/* Wait for reply: %08x saying sum of block: */
len = 0;
do
{
tlen = read(ser_fd, &(inbuff[len]), 1);
len += tlen;
} while (len < 8);
if (len != 8)
{
printf("couldn't read block checksum (len = %d)\n", len);
exit(1);
}
inbuff[8] = 0;
/* Check that buff == my sum */
sprintf(sumbuff, "%08x", sum);
if (strcmp(sumbuff, inbuff) != 0) // Have a > to start with
{
printf("My checksum (%s) differs from GBA's (%s) for block %x!\n", sumbuff, inbuff, offset);
exit(1);
}
else
{
/* Else sent block ok! */
//printf("offset %x checksum %08x\n", offset, sum);
printf("Offset %08x sum %08x [", offset, sum);
for (i = 0; i < 40; i++)
{
if ( (float) i < (((float)offset / (float)codelen) * 40.0) )
printf("#");
else
printf(" ");
}
printf("]\r");
}
}
printf("\n\nRunning code..");
ch = 'j';
if (write(ser_fd, &ch, 1) != 1)
serial_error("Couldn't write 'jump' command!\n");
printf(".done!\n\n");
}
unsigned int xfer(unsigned int in)
{
unsigned int out;
int len, want;
unsigned char low, hi, tmp;
low = in & 0xff;
hi = (in >> 8) & 0xff;
if (low == 255)
{
len = write(ser_fd, &low, 1);
tmp = 0;
len += write(ser_fd, &tmp, 1);
want = 2;
}
else
{
len = write(ser_fd, &low, 1);
want = 1;
}
#ifdef DEBUG
printf("Wrote [%02x] ", low);
#endif
if (hi == 255)
{
len += write(ser_fd, &hi, 1);
tmp = 0;
len += write(ser_fd, &tmp, 1);
want += 2;
}
else
{
len += write(ser_fd, &hi, 1);
want += 1;
}
#ifdef DEBUG
printf("[%02x] (%d)\n", hi, len);
#endif
if ( len != want )
{
printf("Couldn't write all of %x to ser., (len = %d, errno = %d)\n", in, len, errno);
exit(0);
}
/* then get two bytes back: */
len = read(ser_fd, &low, 1);
len += read(ser_fd, &hi, 1);
#ifdef DEBUG
printf ("read %d bytes, [%02x][%02x]\n", len, low, hi);
#endif
out = low | (hi << 8);
return out;
}