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