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