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