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