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