Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Target dependent code for the Fujitsu SPARClite for GDB, the GNU debugger. |
43e526b9 | 2 | Copyright 1994, 1995, 1996, 1999 Free Software Foundation, Inc. |
c906108c | 3 | |
c5aa993b | 4 | This file is part of GDB. |
c906108c | 5 | |
c5aa993b JM |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
c906108c | 10 | |
c5aa993b JM |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
c906108c | 15 | |
c5aa993b JM |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
20 | |
21 | #include "defs.h" | |
22 | #include "gdbcore.h" | |
23 | #include "breakpoint.h" | |
24 | #include "target.h" | |
25 | #include "serial.h" | |
26 | #include <sys/types.h> | |
27 | ||
28 | #if (!defined(__GO32__) && !defined(_WIN32)) || defined(__CYGWIN32__) | |
29 | #define HAVE_SOCKETS | |
30 | #include <sys/time.h> | |
c906108c SS |
31 | #include <sys/socket.h> |
32 | #include <netinet/in.h> | |
33 | #include <netdb.h> | |
34 | #endif | |
35 | ||
43e526b9 | 36 | static struct target_ops sparclite_ops; |
c906108c SS |
37 | |
38 | static char *remote_target_name = NULL; | |
39 | static serial_t remote_desc = NULL; | |
40 | static int serial_flag; | |
41 | #ifdef HAVE_SOCKETS | |
42 | static int udp_fd = -1; | |
43 | #endif | |
44 | ||
a14ed312 KB |
45 | static serial_t open_tty (char *name); |
46 | static int send_resp (serial_t desc, char c); | |
74b7792f | 47 | static void close_tty (void * ignore); |
c906108c | 48 | #ifdef HAVE_SOCKETS |
a14ed312 KB |
49 | static int recv_udp_buf (int fd, unsigned char *buf, int len, int timeout); |
50 | static int send_udp_buf (int fd, unsigned char *buf, int len); | |
c906108c | 51 | #endif |
a14ed312 KB |
52 | static void sparclite_open (char *name, int from_tty); |
53 | static void sparclite_close (int quitting); | |
54 | static void download (char *target_name, char *args, int from_tty, | |
55 | void (*write_routine) (bfd * from_bfd, | |
56 | asection * from_sec, | |
57 | file_ptr from_addr, | |
58 | bfd_vma to_addr, int len), | |
59 | void (*start_routine) (bfd_vma entry)); | |
60 | static void sparclite_serial_start (bfd_vma entry); | |
61 | static void sparclite_serial_write (bfd * from_bfd, asection * from_sec, | |
62 | file_ptr from_addr, | |
63 | bfd_vma to_addr, int len); | |
c906108c | 64 | #ifdef HAVE_SOCKETS |
a14ed312 KB |
65 | static unsigned short calc_checksum (unsigned char *buffer, int count); |
66 | static void sparclite_udp_start (bfd_vma entry); | |
67 | static void sparclite_udp_write (bfd * from_bfd, asection * from_sec, | |
68 | file_ptr from_addr, bfd_vma to_addr, | |
69 | int len); | |
c906108c | 70 | #endif |
a14ed312 | 71 | static void sparclite_download (char *filename, int from_tty); |
c906108c SS |
72 | |
73 | #define DDA2_SUP_ASI 0xb000000 | |
74 | #define DDA1_SUP_ASI 0xb0000 | |
75 | ||
76 | #define DDA2_ASI_MASK 0xff000000 | |
c5aa993b | 77 | #define DDA1_ASI_MASK 0xff0000 |
c906108c SS |
78 | #define DIA2_SUP_MODE 0x8000 |
79 | #define DIA1_SUP_MODE 0x4000 | |
80 | #define DDA2_ENABLE 0x100 | |
81 | #define DDA1_ENABLE 0x80 | |
82 | #define DIA2_ENABLE 0x40 | |
83 | #define DIA1_ENABLE 0x20 | |
c5aa993b | 84 | #define DSINGLE_STEP 0x10 /* not used */ |
c906108c SS |
85 | #define DDV_TYPE_MASK 0xc |
86 | #define DDV_TYPE_LOAD 0x0 | |
87 | #define DDV_TYPE_STORE 0x4 | |
88 | #define DDV_TYPE_ACCESS 0x8 | |
89 | #define DDV_TYPE_ALWAYS 0xc | |
90 | #define DDV_COND 0x2 | |
91 | #define DDV_MASK 0x1 | |
92 | ||
93 | int | |
fba45db2 | 94 | sparclite_insert_watchpoint (CORE_ADDR addr, int len, int type) |
c906108c SS |
95 | { |
96 | CORE_ADDR dcr; | |
97 | ||
98 | dcr = read_register (DCR_REGNUM); | |
99 | ||
100 | if (!(dcr & DDA1_ENABLE)) | |
101 | { | |
102 | write_register (DDA1_REGNUM, addr); | |
103 | dcr &= ~(DDA1_ASI_MASK | DDV_TYPE_MASK); | |
104 | dcr |= (DDA1_SUP_ASI | DDA1_ENABLE); | |
105 | if (type == 1) | |
106 | { | |
107 | write_register (DDV1_REGNUM, 0); | |
108 | write_register (DDV2_REGNUM, 0xffffffff); | |
109 | dcr |= (DDV_TYPE_LOAD & (~DDV_COND & ~DDV_MASK)); | |
c5aa993b | 110 | } |
c906108c SS |
111 | else if (type == 0) |
112 | { | |
113 | write_register (DDV1_REGNUM, 0); | |
114 | write_register (DDV2_REGNUM, 0xffffffff); | |
115 | dcr |= (DDV_TYPE_STORE & (~DDV_COND & ~DDV_MASK)); | |
116 | } | |
117 | else | |
118 | { | |
119 | write_register (DDV1_REGNUM, 0); | |
120 | write_register (DDV2_REGNUM, 0xffffffff); | |
121 | dcr |= (DDV_TYPE_ACCESS); | |
122 | } | |
123 | write_register (DCR_REGNUM, dcr); | |
124 | } | |
125 | else if (!(dcr & DDA2_ENABLE)) | |
126 | { | |
127 | write_register (DDA2_REGNUM, addr); | |
128 | dcr &= ~(DDA2_ASI_MASK & DDV_TYPE_MASK); | |
129 | dcr |= (DDA2_SUP_ASI | DDA2_ENABLE); | |
130 | if (type == 1) | |
131 | { | |
132 | write_register (DDV1_REGNUM, 0); | |
133 | write_register (DDV2_REGNUM, 0xffffffff); | |
134 | dcr |= (DDV_TYPE_LOAD & ~DDV_COND & ~DDV_MASK); | |
135 | } | |
136 | else if (type == 0) | |
137 | { | |
138 | write_register (DDV1_REGNUM, 0); | |
139 | write_register (DDV2_REGNUM, 0xffffffff); | |
140 | dcr |= (DDV_TYPE_STORE & ~DDV_COND & ~DDV_MASK); | |
141 | } | |
142 | else | |
143 | { | |
144 | write_register (DDV1_REGNUM, 0); | |
145 | write_register (DDV2_REGNUM, 0xffffffff); | |
146 | dcr |= (DDV_TYPE_ACCESS); | |
147 | } | |
148 | write_register (DCR_REGNUM, dcr); | |
149 | } | |
150 | else | |
151 | return -1; | |
152 | ||
153 | return 0; | |
c5aa993b | 154 | } |
c906108c SS |
155 | |
156 | int | |
fba45db2 | 157 | sparclite_remove_watchpoint (CORE_ADDR addr, int len, int type) |
c906108c SS |
158 | { |
159 | CORE_ADDR dcr, dda1, dda2; | |
160 | ||
161 | dcr = read_register (DCR_REGNUM); | |
162 | dda1 = read_register (DDA1_REGNUM); | |
163 | dda2 = read_register (DDA2_REGNUM); | |
164 | ||
165 | if ((dcr & DDA1_ENABLE) && addr == dda1) | |
166 | write_register (DCR_REGNUM, (dcr & ~DDA1_ENABLE)); | |
167 | else if ((dcr & DDA2_ENABLE) && addr == dda2) | |
168 | write_register (DCR_REGNUM, (dcr & ~DDA2_ENABLE)); | |
169 | else | |
170 | return -1; | |
171 | ||
172 | return 0; | |
173 | } | |
174 | ||
175 | int | |
fba45db2 | 176 | sparclite_insert_hw_breakpoint (CORE_ADDR addr, int len) |
c906108c SS |
177 | { |
178 | CORE_ADDR dcr; | |
179 | ||
180 | dcr = read_register (DCR_REGNUM); | |
c5aa993b | 181 | |
c906108c SS |
182 | if (!(dcr & DIA1_ENABLE)) |
183 | { | |
184 | write_register (DIA1_REGNUM, addr); | |
185 | write_register (DCR_REGNUM, (dcr | DIA1_ENABLE | DIA1_SUP_MODE)); | |
186 | } | |
187 | else if (!(dcr & DIA2_ENABLE)) | |
188 | { | |
189 | write_register (DIA2_REGNUM, addr); | |
190 | write_register (DCR_REGNUM, (dcr | DIA2_ENABLE | DIA2_SUP_MODE)); | |
191 | } | |
192 | else | |
193 | return -1; | |
194 | ||
195 | return 0; | |
196 | } | |
197 | ||
198 | int | |
fba45db2 | 199 | sparclite_remove_hw_breakpoint (CORE_ADDR addr, int shadow) |
c906108c SS |
200 | { |
201 | CORE_ADDR dcr, dia1, dia2; | |
202 | ||
203 | dcr = read_register (DCR_REGNUM); | |
204 | dia1 = read_register (DIA1_REGNUM); | |
205 | dia2 = read_register (DIA2_REGNUM); | |
c5aa993b | 206 | |
c906108c SS |
207 | if ((dcr & DIA1_ENABLE) && addr == dia1) |
208 | write_register (DCR_REGNUM, (dcr & ~DIA1_ENABLE)); | |
209 | else if ((dcr & DIA2_ENABLE) && addr == dia2) | |
210 | write_register (DCR_REGNUM, (dcr & ~DIA2_ENABLE)); | |
211 | else | |
212 | return -1; | |
213 | ||
214 | return 0; | |
215 | } | |
216 | ||
217 | int | |
fba45db2 | 218 | sparclite_check_watch_resources (int type, int cnt, int ot) |
c906108c SS |
219 | { |
220 | /* Watchpoints not supported on simulator. */ | |
221 | if (strcmp (target_shortname, "sim") == 0) | |
222 | return 0; | |
223 | ||
224 | if (type == bp_hardware_breakpoint) | |
225 | { | |
226 | if (TARGET_HW_BREAK_LIMIT == 0) | |
227 | return 0; | |
228 | else if (cnt <= TARGET_HW_BREAK_LIMIT) | |
229 | return 1; | |
230 | } | |
231 | else | |
232 | { | |
233 | if (TARGET_HW_WATCH_LIMIT == 0) | |
234 | return 0; | |
235 | else if (ot) | |
236 | return -1; | |
237 | else if (cnt <= TARGET_HW_WATCH_LIMIT) | |
238 | return 1; | |
239 | } | |
240 | return -1; | |
241 | } | |
242 | ||
243 | CORE_ADDR | |
fba45db2 | 244 | sparclite_stopped_data_address (void) |
c906108c SS |
245 | { |
246 | CORE_ADDR dsr, dda1, dda2; | |
247 | ||
248 | dsr = read_register (DSR_REGNUM); | |
249 | dda1 = read_register (DDA1_REGNUM); | |
250 | dda2 = read_register (DDA2_REGNUM); | |
251 | ||
252 | if (dsr & 0x10) | |
253 | return dda1; | |
254 | else if (dsr & 0x20) | |
255 | return dda2; | |
256 | else | |
257 | return 0; | |
258 | } | |
259 | \f | |
260 | static serial_t | |
fba45db2 | 261 | open_tty (char *name) |
c906108c SS |
262 | { |
263 | serial_t desc; | |
264 | ||
265 | desc = SERIAL_OPEN (name); | |
266 | if (!desc) | |
267 | perror_with_name (name); | |
268 | ||
269 | if (baud_rate != -1) | |
270 | { | |
271 | if (SERIAL_SETBAUDRATE (desc, baud_rate)) | |
272 | { | |
273 | SERIAL_CLOSE (desc); | |
274 | perror_with_name (name); | |
275 | } | |
276 | } | |
277 | ||
278 | SERIAL_RAW (desc); | |
279 | ||
280 | SERIAL_FLUSH_INPUT (desc); | |
281 | ||
282 | return desc; | |
283 | } | |
284 | ||
285 | /* Read a single character from the remote end, masking it down to 7 bits. */ | |
286 | ||
287 | static int | |
fba45db2 | 288 | readchar (serial_t desc, int timeout) |
c906108c SS |
289 | { |
290 | int ch; | |
291 | char s[10]; | |
292 | ||
293 | ch = SERIAL_READCHAR (desc, timeout); | |
294 | ||
295 | switch (ch) | |
296 | { | |
297 | case SERIAL_EOF: | |
298 | error ("SPARClite remote connection closed"); | |
299 | case SERIAL_ERROR: | |
300 | perror_with_name ("SPARClite communication error"); | |
301 | case SERIAL_TIMEOUT: | |
302 | error ("SPARClite remote timeout"); | |
303 | default: | |
304 | if (remote_debug > 0) | |
305 | { | |
306 | sprintf (s, "[%02x]", ch & 0xff); | |
307 | puts_debug ("read -->", s, "<--"); | |
308 | } | |
309 | return ch; | |
310 | } | |
311 | } | |
312 | ||
313 | static void | |
fba45db2 | 314 | debug_serial_write (serial_t desc, char *buf, int len) |
c906108c SS |
315 | { |
316 | char s[10]; | |
317 | ||
318 | SERIAL_WRITE (desc, buf, len); | |
319 | if (remote_debug > 0) | |
320 | { | |
321 | while (len-- > 0) | |
322 | { | |
323 | sprintf (s, "[%02x]", *buf & 0xff); | |
324 | puts_debug ("Sent -->", s, "<--"); | |
325 | buf++; | |
326 | } | |
327 | } | |
328 | } | |
329 | ||
330 | ||
331 | static int | |
fba45db2 | 332 | send_resp (serial_t desc, char c) |
c906108c SS |
333 | { |
334 | debug_serial_write (desc, &c, 1); | |
335 | return readchar (desc, remote_timeout); | |
336 | } | |
337 | ||
338 | static void | |
74b7792f | 339 | close_tty (void *ignore) |
c906108c SS |
340 | { |
341 | if (!remote_desc) | |
342 | return; | |
343 | ||
344 | SERIAL_CLOSE (remote_desc); | |
345 | ||
346 | remote_desc = NULL; | |
347 | } | |
348 | ||
349 | #ifdef HAVE_SOCKETS | |
350 | static int | |
fba45db2 | 351 | recv_udp_buf (int fd, unsigned char *buf, int len, int timeout) |
c906108c SS |
352 | { |
353 | int cc; | |
354 | fd_set readfds; | |
355 | ||
356 | FD_ZERO (&readfds); | |
357 | FD_SET (fd, &readfds); | |
358 | ||
359 | if (timeout >= 0) | |
360 | { | |
361 | struct timeval timebuf; | |
362 | ||
363 | timebuf.tv_sec = timeout; | |
364 | timebuf.tv_usec = 0; | |
365 | cc = select (fd + 1, &readfds, 0, 0, &timebuf); | |
366 | } | |
367 | else | |
368 | cc = select (fd + 1, &readfds, 0, 0, 0); | |
369 | ||
370 | if (cc == 0) | |
371 | return 0; | |
372 | ||
373 | if (cc != 1) | |
374 | perror_with_name ("recv_udp_buf: Bad return value from select:"); | |
375 | ||
376 | cc = recv (fd, buf, len, 0); | |
377 | ||
378 | if (cc < 0) | |
379 | perror_with_name ("Got an error from recv: "); | |
380 | } | |
381 | ||
382 | static int | |
fba45db2 | 383 | send_udp_buf (int fd, unsigned char *buf, int len) |
c906108c SS |
384 | { |
385 | int cc; | |
386 | ||
387 | cc = send (fd, buf, len, 0); | |
388 | ||
389 | if (cc == len) | |
390 | return; | |
391 | ||
392 | if (cc < 0) | |
393 | perror_with_name ("Got an error from send: "); | |
394 | ||
395 | error ("Short count in send: tried %d, sent %d\n", len, cc); | |
396 | } | |
397 | #endif /* HAVE_SOCKETS */ | |
398 | ||
399 | static void | |
fba45db2 | 400 | sparclite_open (char *name, int from_tty) |
c906108c SS |
401 | { |
402 | struct cleanup *old_chain; | |
403 | int c; | |
404 | char *p; | |
405 | ||
406 | if (!name) | |
407 | error ("You need to specify what device or hostname is associated with the SparcLite board."); | |
408 | ||
409 | target_preopen (from_tty); | |
410 | ||
411 | unpush_target (&sparclite_ops); | |
412 | ||
413 | if (remote_target_name) | |
414 | free (remote_target_name); | |
415 | ||
416 | remote_target_name = strsave (name); | |
417 | ||
418 | /* We need a 'serial' or 'udp' keyword to disambiguate host:port, which can | |
419 | mean either a serial port on a terminal server, or the IP address of a | |
420 | SPARClite demo board. If there's no colon, then it pretty much has to be | |
421 | a local device (except for DOS... grrmble) */ | |
422 | ||
423 | p = strchr (name, ' '); | |
424 | ||
425 | if (p) | |
426 | { | |
427 | *p++ = '\000'; | |
c5aa993b JM |
428 | while ((*p != '\000') && isspace (*p)) |
429 | p++; | |
c906108c SS |
430 | |
431 | if (strncmp (name, "serial", strlen (name)) == 0) | |
432 | serial_flag = 1; | |
433 | else if (strncmp (name, "udp", strlen (name)) == 0) | |
434 | serial_flag = 0; | |
435 | else | |
436 | error ("Must specify either `serial' or `udp'."); | |
437 | } | |
438 | else | |
439 | { | |
440 | p = name; | |
441 | ||
442 | if (!strchr (name, ':')) | |
443 | serial_flag = 1; /* No colon is unambiguous (local device) */ | |
444 | else | |
445 | error ("Usage: target sparclite serial /dev/ttyb\n\ | |
446 | or: target sparclite udp host"); | |
447 | } | |
448 | ||
449 | if (serial_flag) | |
450 | { | |
451 | remote_desc = open_tty (p); | |
452 | ||
74b7792f | 453 | old_chain = make_cleanup (close_tty, 0 /*ignore*/); |
c906108c SS |
454 | |
455 | c = send_resp (remote_desc, 0x00); | |
456 | ||
457 | if (c != 0xaa) | |
458 | error ("Unknown response (0x%x) from SparcLite. Try resetting the board.", | |
459 | c); | |
460 | ||
461 | c = send_resp (remote_desc, 0x55); | |
462 | ||
463 | if (c != 0x55) | |
464 | error ("Sparclite appears to be ill."); | |
465 | } | |
466 | else | |
467 | { | |
468 | #ifdef HAVE_SOCKETS | |
469 | struct hostent *he; | |
470 | struct sockaddr_in sockaddr; | |
471 | unsigned char buffer[100]; | |
472 | int cc; | |
473 | ||
474 | /* Setup the socket. Must be raw UDP. */ | |
475 | ||
476 | he = gethostbyname (p); | |
477 | ||
478 | if (!he) | |
479 | error ("No such host %s.", p); | |
480 | ||
481 | udp_fd = socket (PF_INET, SOCK_DGRAM, 0); | |
482 | ||
483 | old_chain = make_cleanup (close, udp_fd); | |
484 | ||
485 | sockaddr.sin_family = PF_INET; | |
c5aa993b | 486 | sockaddr.sin_port = htons (7000); |
c906108c SS |
487 | memcpy (&sockaddr.sin_addr.s_addr, he->h_addr, sizeof (struct in_addr)); |
488 | ||
c5aa993b | 489 | if (connect (udp_fd, &sockaddr, sizeof (sockaddr))) |
c906108c SS |
490 | perror_with_name ("Connect failed"); |
491 | ||
492 | buffer[0] = 0x5; | |
493 | buffer[1] = 0; | |
494 | ||
c5aa993b JM |
495 | send_udp_buf (udp_fd, buffer, 2); /* Request version */ |
496 | cc = recv_udp_buf (udp_fd, buffer, sizeof (buffer), 5); /* Get response */ | |
c906108c SS |
497 | if (cc == 0) |
498 | error ("SPARClite isn't responding."); | |
499 | ||
500 | if (cc < 3) | |
501 | error ("SPARClite appears to be ill."); | |
502 | #else | |
503 | error ("UDP downloading is not supported for DOS hosts."); | |
504 | #endif /* HAVE_SOCKETS */ | |
505 | } | |
506 | ||
507 | printf_unfiltered ("[SPARClite appears to be alive]\n"); | |
508 | ||
509 | push_target (&sparclite_ops); | |
510 | ||
511 | discard_cleanups (old_chain); | |
512 | ||
513 | return; | |
514 | } | |
515 | ||
516 | static void | |
fba45db2 | 517 | sparclite_close (int quitting) |
c906108c SS |
518 | { |
519 | if (serial_flag) | |
520 | close_tty (0); | |
521 | #ifdef HAVE_SOCKETS | |
c5aa993b JM |
522 | else if (udp_fd != -1) |
523 | close (udp_fd); | |
c906108c SS |
524 | #endif |
525 | } | |
526 | ||
527 | #define LOAD_ADDRESS 0x40000000 | |
528 | ||
529 | static void | |
530 | download (target_name, args, from_tty, write_routine, start_routine) | |
531 | char *target_name; | |
532 | char *args; | |
533 | int from_tty; | |
507f3c78 KB |
534 | void (*write_routine) (bfd * from_bfd, asection * from_sec, |
535 | file_ptr from_addr, bfd_vma to_addr, int len); | |
536 | void (*start_routine) (bfd_vma entry); | |
c906108c SS |
537 | { |
538 | struct cleanup *old_chain; | |
539 | asection *section; | |
540 | bfd *pbfd; | |
541 | bfd_vma entry; | |
542 | int i; | |
543 | #define WRITESIZE 1024 | |
544 | char *filename; | |
545 | int quiet; | |
546 | int nostart; | |
547 | ||
548 | quiet = 0; | |
549 | nostart = 0; | |
550 | filename = NULL; | |
551 | ||
552 | while (*args != '\000') | |
553 | { | |
554 | char *arg; | |
555 | ||
c5aa993b JM |
556 | while (isspace (*args)) |
557 | args++; | |
c906108c SS |
558 | |
559 | arg = args; | |
560 | ||
c5aa993b JM |
561 | while ((*args != '\000') && !isspace (*args)) |
562 | args++; | |
c906108c SS |
563 | |
564 | if (*args != '\000') | |
565 | *args++ = '\000'; | |
566 | ||
567 | if (*arg != '-') | |
568 | filename = arg; | |
569 | else if (strncmp (arg, "-quiet", strlen (arg)) == 0) | |
570 | quiet = 1; | |
571 | else if (strncmp (arg, "-nostart", strlen (arg)) == 0) | |
572 | nostart = 1; | |
573 | else | |
574 | error ("unknown option `%s'", arg); | |
575 | } | |
576 | ||
577 | if (!filename) | |
578 | filename = get_exec_file (1); | |
579 | ||
580 | pbfd = bfd_openr (filename, gnutarget); | |
581 | if (pbfd == NULL) | |
582 | { | |
583 | perror_with_name (filename); | |
584 | return; | |
585 | } | |
5c65bbb6 | 586 | old_chain = make_cleanup_bfd_close (pbfd); |
c906108c | 587 | |
c5aa993b | 588 | if (!bfd_check_format (pbfd, bfd_object)) |
c906108c SS |
589 | error ("\"%s\" is not an object file: %s", filename, |
590 | bfd_errmsg (bfd_get_error ())); | |
591 | ||
c5aa993b | 592 | for (section = pbfd->sections; section; section = section->next) |
c906108c SS |
593 | { |
594 | if (bfd_get_section_flags (pbfd, section) & SEC_LOAD) | |
595 | { | |
c5aa993b | 596 | bfd_vma section_address; |
c906108c | 597 | bfd_size_type section_size; |
c5aa993b JM |
598 | file_ptr fptr; |
599 | const char *section_name; | |
c906108c | 600 | |
c5aa993b | 601 | section_name = bfd_get_section_name (pbfd, section); |
c906108c SS |
602 | |
603 | section_address = bfd_get_section_vma (pbfd, section); | |
604 | ||
605 | /* Adjust sections from a.out files, since they don't | |
606 | carry their addresses with. */ | |
607 | if (bfd_get_flavour (pbfd) == bfd_target_aout_flavour) | |
608 | { | |
609 | if (strcmp (section_name, ".text") == 0) | |
610 | section_address = bfd_get_start_address (pbfd); | |
611 | else if (strcmp (section_name, ".data") == 0) | |
612 | { | |
613 | /* Read the first 8 bytes of the data section. | |
614 | There should be the string 'DaTa' followed by | |
615 | a word containing the actual section address. */ | |
616 | struct data_marker | |
c5aa993b JM |
617 | { |
618 | char signature[4]; /* 'DaTa' */ | |
619 | unsigned char sdata[4]; /* &sdata */ | |
620 | } | |
621 | marker; | |
c906108c SS |
622 | bfd_get_section_contents (pbfd, section, &marker, 0, |
623 | sizeof (marker)); | |
624 | if (strncmp (marker.signature, "DaTa", 4) == 0) | |
625 | { | |
626 | if (TARGET_BYTE_ORDER == BIG_ENDIAN) | |
627 | section_address = bfd_getb32 (marker.sdata); | |
628 | else | |
629 | section_address = bfd_getl32 (marker.sdata); | |
630 | } | |
631 | } | |
632 | } | |
633 | ||
634 | section_size = bfd_get_section_size_before_reloc (section); | |
635 | ||
636 | if (!quiet) | |
637 | printf_filtered ("[Loading section %s at 0x%x (%d bytes)]\n", | |
638 | bfd_get_section_name (pbfd, section), | |
639 | section_address, | |
640 | section_size); | |
641 | ||
642 | fptr = 0; | |
643 | while (section_size > 0) | |
644 | { | |
645 | int count; | |
646 | static char inds[] = "|/-\\"; | |
647 | static int k = 0; | |
648 | ||
649 | QUIT; | |
650 | ||
651 | count = min (section_size, WRITESIZE); | |
652 | ||
653 | write_routine (pbfd, section, fptr, section_address, count); | |
654 | ||
655 | if (!quiet) | |
656 | { | |
657 | printf_unfiltered ("\r%c", inds[k++ % 4]); | |
658 | gdb_flush (gdb_stdout); | |
659 | } | |
660 | ||
661 | section_address += count; | |
662 | fptr += count; | |
663 | section_size -= count; | |
664 | } | |
665 | } | |
666 | } | |
667 | ||
668 | if (!nostart) | |
669 | { | |
670 | entry = bfd_get_start_address (pbfd); | |
671 | ||
672 | if (!quiet) | |
673 | printf_unfiltered ("[Starting %s at 0x%x]\n", filename, entry); | |
674 | ||
675 | start_routine (entry); | |
676 | } | |
677 | ||
678 | do_cleanups (old_chain); | |
679 | } | |
680 | ||
681 | static void | |
fba45db2 | 682 | sparclite_serial_start (bfd_vma entry) |
c906108c SS |
683 | { |
684 | char buffer[5]; | |
685 | int i; | |
686 | ||
687 | buffer[0] = 0x03; | |
688 | store_unsigned_integer (buffer + 1, 4, entry); | |
689 | ||
690 | debug_serial_write (remote_desc, buffer, 1 + 4); | |
691 | i = readchar (remote_desc, remote_timeout); | |
692 | if (i != 0x55) | |
693 | error ("Can't start SparcLite. Error code %d\n", i); | |
694 | } | |
695 | ||
696 | static void | |
fba45db2 KB |
697 | sparclite_serial_write (bfd *from_bfd, asection *from_sec, file_ptr from_addr, |
698 | bfd_vma to_addr, int len) | |
c906108c | 699 | { |
c5aa993b | 700 | char buffer[4 + 4 + WRITESIZE]; /* addr + len + data */ |
c906108c SS |
701 | unsigned char checksum; |
702 | int i; | |
703 | ||
c5aa993b JM |
704 | store_unsigned_integer (buffer, 4, to_addr); /* Address */ |
705 | store_unsigned_integer (buffer + 4, 4, len); /* Length */ | |
c906108c SS |
706 | |
707 | bfd_get_section_contents (from_bfd, from_sec, buffer + 8, from_addr, len); | |
708 | ||
709 | checksum = 0; | |
710 | for (i = 0; i < len; i++) | |
711 | checksum += buffer[8 + i]; | |
712 | ||
713 | i = send_resp (remote_desc, 0x01); | |
714 | ||
715 | if (i != 0x5a) | |
716 | error ("Bad response from load command (0x%x)", i); | |
717 | ||
718 | debug_serial_write (remote_desc, buffer, 4 + 4 + len); | |
719 | i = readchar (remote_desc, remote_timeout); | |
720 | ||
721 | if (i != checksum) | |
722 | error ("Bad checksum from load command (0x%x)", i); | |
723 | } | |
724 | ||
725 | #ifdef HAVE_SOCKETS | |
726 | ||
727 | static unsigned short | |
fba45db2 | 728 | calc_checksum (unsigned char *buffer, int count) |
c906108c SS |
729 | { |
730 | unsigned short checksum; | |
731 | ||
732 | checksum = 0; | |
733 | for (; count > 0; count -= 2, buffer += 2) | |
734 | checksum += (*buffer << 8) | *(buffer + 1); | |
735 | ||
736 | if (count != 0) | |
737 | checksum += *buffer << 8; | |
738 | ||
739 | return checksum; | |
740 | } | |
741 | ||
742 | static void | |
fba45db2 | 743 | sparclite_udp_start (bfd_vma entry) |
c906108c SS |
744 | { |
745 | unsigned char buffer[6]; | |
746 | int i; | |
747 | ||
748 | buffer[0] = 0x3; | |
749 | buffer[1] = 0; | |
750 | buffer[2] = entry >> 24; | |
751 | buffer[3] = entry >> 16; | |
752 | buffer[4] = entry >> 8; | |
753 | buffer[5] = entry; | |
754 | ||
c5aa993b JM |
755 | send_udp_buf (udp_fd, buffer, 6); /* Send start addr */ |
756 | i = recv_udp_buf (udp_fd, buffer, sizeof (buffer), -1); /* Get response */ | |
c906108c SS |
757 | |
758 | if (i < 1 || buffer[0] != 0x55) | |
759 | error ("Failed to take start address."); | |
760 | } | |
761 | ||
762 | static void | |
fba45db2 KB |
763 | sparclite_udp_write (bfd *from_bfd, asection *from_sec, file_ptr from_addr, |
764 | bfd_vma to_addr, int len) | |
c906108c SS |
765 | { |
766 | unsigned char buffer[2000]; | |
767 | unsigned short checksum; | |
768 | static int pkt_num = 0; | |
769 | static unsigned long old_addr = -1; | |
770 | int i; | |
771 | ||
772 | while (1) | |
773 | { | |
774 | if (to_addr != old_addr) | |
775 | { | |
776 | buffer[0] = 0x1; /* Load command */ | |
777 | buffer[1] = 0x1; /* Loading address */ | |
778 | buffer[2] = to_addr >> 24; | |
779 | buffer[3] = to_addr >> 16; | |
780 | buffer[4] = to_addr >> 8; | |
781 | buffer[5] = to_addr; | |
782 | ||
783 | checksum = 0; | |
784 | for (i = 0; i < 6; i++) | |
785 | checksum += buffer[i]; | |
786 | checksum &= 0xff; | |
787 | ||
788 | send_udp_buf (udp_fd, buffer, 6); | |
789 | i = recv_udp_buf (udp_fd, buffer, sizeof buffer, -1); | |
790 | ||
791 | if (i < 1) | |
792 | error ("Got back short checksum for load addr."); | |
793 | ||
794 | if (checksum != buffer[0]) | |
795 | error ("Got back bad checksum for load addr."); | |
796 | ||
797 | pkt_num = 0; /* Load addr resets packet seq # */ | |
798 | old_addr = to_addr; | |
799 | } | |
800 | ||
801 | bfd_get_section_contents (from_bfd, from_sec, buffer + 6, from_addr, | |
802 | len); | |
803 | ||
804 | checksum = calc_checksum (buffer + 6, len); | |
805 | ||
806 | buffer[0] = 0x1; /* Load command */ | |
807 | buffer[1] = 0x2; /* Loading data */ | |
808 | buffer[2] = pkt_num >> 8; | |
809 | buffer[3] = pkt_num; | |
810 | buffer[4] = checksum >> 8; | |
811 | buffer[5] = checksum; | |
812 | ||
813 | send_udp_buf (udp_fd, buffer, len + 6); | |
814 | i = recv_udp_buf (udp_fd, buffer, sizeof buffer, 3); | |
815 | ||
816 | if (i == 0) | |
817 | { | |
818 | fprintf_unfiltered (gdb_stderr, "send_data: timeout sending %d bytes to address 0x%x retrying\n", len, to_addr); | |
819 | continue; | |
820 | } | |
821 | ||
822 | if (buffer[0] != 0xff) | |
823 | error ("Got back bad response for load data."); | |
824 | ||
825 | old_addr += len; | |
826 | pkt_num++; | |
827 | ||
828 | return; | |
829 | } | |
830 | } | |
831 | ||
832 | #endif /* HAVE_SOCKETS */ | |
833 | ||
834 | static void | |
fba45db2 | 835 | sparclite_download (char *filename, int from_tty) |
c906108c SS |
836 | { |
837 | if (!serial_flag) | |
838 | #ifdef HAVE_SOCKETS | |
839 | download (remote_target_name, filename, from_tty, sparclite_udp_write, | |
840 | sparclite_udp_start); | |
841 | #else | |
842 | abort (); /* sparclite_open should prevent this! */ | |
843 | #endif | |
844 | else | |
845 | download (remote_target_name, filename, from_tty, sparclite_serial_write, | |
846 | sparclite_serial_start); | |
847 | } | |
848 | \f | |
43e526b9 | 849 | /* Set up the sparclite target vector. */ |
c906108c | 850 | |
c5aa993b JM |
851 | static void |
852 | init_sparclite_ops (void) | |
c906108c | 853 | { |
c5aa993b | 854 | sparclite_ops.to_shortname = "sparclite"; |
43e526b9 JM |
855 | sparclite_ops.to_longname = "SPARClite download target"; |
856 | sparclite_ops.to_doc = "Download to a remote SPARClite target board via serial of UDP.\n\ | |
857 | Specify the device it is connected to (e.g. /dev/ttya)."; | |
c5aa993b JM |
858 | sparclite_ops.to_open = sparclite_open; |
859 | sparclite_ops.to_close = sparclite_close; | |
c5aa993b | 860 | sparclite_ops.to_load = sparclite_download; |
c5aa993b | 861 | sparclite_ops.to_stratum = download_stratum; |
c5aa993b | 862 | sparclite_ops.to_magic = OPS_MAGIC; |
43e526b9 | 863 | } |
c906108c SS |
864 | |
865 | void | |
fba45db2 | 866 | _initialize_sparcl_tdep (void) |
c906108c | 867 | { |
c5aa993b | 868 | init_sparclite_ops (); |
c906108c SS |
869 | add_target (&sparclite_ops); |
870 | } |