1 /* Remote debugging with the XLNT Designs, Inc (XDI) NetROM.
2 Copyright 1990, 1991, 1992, 1995 Free Software Foundation, Inc.
6 15050 Avenue of Science, Suite 106
10 Adapted from work done at Cygnus Support in remote-nindy.c,
11 later merged in by Stan Shebs at Cygnus.
13 This file is part of GDB.
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/ioctl.h>
43 #include <netinet/in.h>
50 /* Packet header found on every packet sent to/from GDB. */
52 typedef struct gpkthdr
{
53 unsigned char csum
; /* Check sum */
54 unsigned char seq_num
; /* Sequence number */
55 unsigned short len
; /* Number of bytes in packet */
56 unsigned char cmd
; /* GDB command */
58 unsigned long addr
; /* Address if needed */
59 unsigned long datalen
; /* # of bytes to read/write */
62 #define GDB_START_DELIMITER '['
63 #define GDB_END_DELIMITER ']'
67 #define GDB_QUERY_CMD 0x01
68 #define GDB_STATUS 0x02
69 #define GDB_READ_REGS 0x03
70 #define GDB_WRITE_REGS 0x04
71 #define GDB_READ_MEM 0x05
72 #define GDB_WRITE_MEM 0x06
73 #define GDB_CONTINUE 0x07
80 #define GDB_READ_REG_RESP 0x13
81 #define GDB_READ_MEM_RESP 0x14
82 #define GDB_WRITE_REG_RESP 0x15
83 #define GDB_WRITE_MEM_RESP 0x16
85 #define GDB_BP_TRAP "05"
86 #define GDB_BUSERR_TRAP "10"
87 #define GDB_ILLOP_TRAP "40"
89 #define GDB_ACK_VALUE "OK"
91 /* Default ports used to talk with the NetROM. */
93 #define DEFAULT_NETROM_TARGET_PORT 1235
94 #define DEFAULT_NETROM_LOAD_PORT 1236
95 #define DEFAULT_NETROM_CONTROL_PORT 1237
97 #define UC(b) (((long)b)&0xff)
99 #define NROM_BUF_SIZE 2048
100 #define MAX_SEND_ATTEMPTS 10
101 #define READ_BUF_SIZE 2048
103 /* Definitions for filetype. */
105 #define BINARY_FTYPE 0
109 #if 0 /* for debugging, if anyone cares */
110 static char *GCMDTYPE
[] = {
132 "GDB_WRITE_REG_RESP",
137 static void nrom_attach
PARAMS ((char *, int));
139 static int nrom_can_run
PARAMS ((void));
141 static void nrom_close
PARAMS ((int));
143 static void nrom_create_inferior
PARAMS ((char *, char *, char **));
145 static void nrom_detach
PARAMS ((char *, int));
147 static void nrom_fetch_registers
PARAMS ((int));
149 static void nrom_files_info
PARAMS ((struct target_ops
*));
151 static void nrom_kill
PARAMS ((void));
153 static void nrom_load
PARAMS ((char *, int));
155 static void nrom_mourn
PARAMS ((void));
157 static void nrom_open
PARAMS ((char *,int));
159 static void nrom_resume
PARAMS ((int, int, enum target_signal
));
161 static void nrom_prepare_to_store
PARAMS ((void));
163 static void nrom_store_registers
PARAMS ((int));
165 static int nrom_wait
PARAMS ((int pid
, struct target_waitstatus
*status
));
167 static int nrom_xfer_inferior_memory
PARAMS ((CORE_ADDR
, char *, int, int,
168 struct target_ops
*));
170 static int nrom_write_inferior_memory
PARAMS ((CORE_ADDR
, char *, int));
172 static int nrom_read_inferior_memory
PARAMS ((CORE_ADDR
, char *, int));
176 static void nrom_set_target_port
PARAMS ((char *, int));
178 static void nrom_set_control_port
PARAMS ((char *, int));
180 static void nrom_set_load_port
PARAMS ((char *, int));
182 static void nrom_set_ipaddr
PARAMS ((char *, int));
184 static void nrom_set_filetype
PARAMS ((char *, int));
186 static void nrom_show_status
PARAMS ((char *, int));
188 static void nrom_passthru
PARAMS ((char *, int));
190 /* Packet functions. */
192 static void build_pkt
PARAMS ((int, unsigned char *, long,
193 unsigned char *, unsigned long, unsigned long,
196 static int compute_csum
PARAMS ((unsigned char *, int));
199 static void dump_pkt
PARAMS ((GPKTHDR
*, unsigned char *));
202 static int get_seq_number
PARAMS ((void));
204 static char *hex2mem
PARAMS ((char *, char *, int));
206 static char *mem2hex
PARAMS ((char *, char *, int));
208 static void nrom_send
PARAMS ((int, char *, int, long, long, char *));
210 static void send_query_cmd
PARAMS ((void));
212 static int send_pkt
PARAMS ((int, char *, int, long, int));
214 static int read_pkt
PARAMS ((char *));
216 static void send_query_cmd
PARAMS ((void));
218 static int tohex
PARAMS ((int));
220 static int parse_pkt
PARAMS ((unsigned char *, GPKTHDR
*, char *));
222 static int writen
PARAMS ((int, char *, int));
224 /* Private globals. */
226 /* We talk to the NetROM over these sockets. */
228 static int nrom_load_sock
= -1;
229 static int nrom_targ_sock
= -1;
230 static int nrom_ctrl_sock
= -1;
232 /* For binding to the socket we ned a sockaddr_in structure. */
234 static struct sockaddr_in nrom_sin
;
236 /* The IP Address of the NetROM is needed so we know who to talk to. */
238 static unsigned long nrom_ipaddr
= 0;
240 static int load_port
= DEFAULT_NETROM_LOAD_PORT
;
241 static int target_port
= DEFAULT_NETROM_TARGET_PORT
;
242 static int control_port
= DEFAULT_NETROM_CONTROL_PORT
;
244 static int nrom_filetype
= BINARY_FTYPE
;
246 static unsigned char host_seq_num
= 0;
248 static char hexchars
[] = "0123456789abcdef";
250 static char freadbuf
[READ_BUF_SIZE
];
252 static char readbuf
[NROM_BUF_SIZE
];
253 static int bufdata
= 0;
254 static int bufindex
= 0;
256 static char workbuf
[NROM_BUF_SIZE
];
257 static char sendbuf
[NROM_BUF_SIZE
];
259 /* Forward data declaration. */
261 extern struct target_ops nrom_ops
;
263 /* This routine builds a packet to send to gdb running on the host. */
266 build_pkt (cmd
, data
, datalen
, pkt
, addr
, len
, seq
)
282 phdr
.len
= sizeof(GPKTHDR
) + datalen
;
285 phdr
.pad
[0] = phdr
.pad
[1] = phdr
.pad
[2] = 0;
288 /* Convert packet header to ASCII. */
289 dptr
= mem2hex ((char *) &phdr
, pkt
, sizeof(GPKTHDR
));
291 /* Calculate pkt length now that it is converted. */
292 plen
= (int) ((unsigned long)dptr
- (unsigned long)pkt
) + datalen
;
293 /* Put data in packet. */
295 memcpy (dptr
, data
, datalen
);
297 /* Compute checksum. For computing checksum we skip first two bytes
298 since this is where the checksum will go. */
300 csum
= compute_csum (pktptr
, plen
- 2);
301 *pkt
++ = hexchars
[csum
>> 4];
302 *pkt
++ = hexchars
[csum
% 16];
308 compute_csum (data
, len
)
323 #if 0 /* for debugging, if anyone cares */
331 printf_filtered ("PACKET: csum = %x,seq = %d,len = %d\n",hdr
->csum
,hdr
->seq_num
,
334 printf_filtered ("cmd = %s,addr = %x, datalen = %d\n", GCMDTYPE
[hdr
->cmd
],
335 hdr
->addr
,hdr
->datalen
);
338 for (i
= 0; i
< hdr
->datalen
* 2; i
++)
339 printf_filtered ("%x",data
[i
]);
340 printf_filtered ("\n");
348 return host_seq_num
++;
355 if ((ch
>= 'a') && (ch
<= 'f'))
356 return (ch
- 'a' + 10);
357 if((ch
>= 'A') && (ch
<= 'F'))
358 return ((ch
- 'A') + 10);
359 if ((ch
>= '0') && (ch
<= '9'))
364 /* Convert the hex array pointed to by buf into binary to be placed in mem
365 return a pointer to the character AFTER the last byte written. */
368 hex2mem(buf
, mem
, count
)
376 for (i
= 0; i
< count
; i
++)
378 ch
= hex(*buf
++) << 4;
379 ch
= ch
+ hex(*buf
++);
385 /* Convert the memory pointed to by mem into hex, placing result in buf
386 return a pointer to the last char put in buf (null) */
389 mem2hex (mem
, buf
, count
)
397 for (i
= 0; i
< count
; i
++)
400 *buf
++ = hexchars
[ch
>> 4];
401 *buf
++ = hexchars
[ch
% 16];
408 nrom_control_send (s
, nbytes
)
415 /* clear leading characters */
416 /* FIXME: The ioctl uses here seem bogus to me. -sts */
420 if (ioctl (nrom_ctrl_sock
, FIONREAD
, &len
) < 0)
422 perror ("nrom_control_send ioctl");
427 if (read (nrom_ctrl_sock
, buf
, 1) < 0)
429 perror ("nrom_control_send read");
436 printf_filtered ("nrom_control_send: sending '%s' (%d bytes) to NetROM\n",
439 if (writen (nrom_ctrl_sock
, s
, nbytes
) < 0)
441 perror ("nrom_control_send");
445 /* clear trailing characters */
449 if (ioctl (nrom_ctrl_sock
, FIONREAD
, &len
) < 0)
451 perror ("nrom_control_send ioctl");
456 if (read (nrom_ctrl_sock
, buf
, 1) < 0)
458 perror ("nrom_control_send read");
471 /* Download a file specified in ARGS to the netROM. */
474 nrom_load (args
, fromtty
)
478 int fd
, rd_amt
, fsize
;
479 struct sockaddr_in sin
;
482 char *downloadstring
= "download 0\n";
484 /* Tell the netrom to get ready to download. */
485 if (nrom_control_send (downloadstring
, strlen (downloadstring
)) < 0)
486 error ("nrom_load: control_send() of `%s' failed", downloadstring
);
488 /* Wait for the download daemon to start up. */
491 nrom_load_sock
= socket (AF_INET
, SOCK_STREAM
, 0);
492 if (nrom_load_sock
== -1)
493 error ("Could not create download socket, error %d", errno
);
495 memset (&sin
, 0, sizeof(struct sockaddr_in
));
496 sin
.sin_family
= AF_INET
;
497 sin
.sin_port
= htons (load_port
);
498 sin
.sin_addr
.s_addr
= htonl (nrom_ipaddr
);
500 if (connect (nrom_load_sock
, &sin
, sizeof(sin
)) == -1)
501 error ("Connect failed, error %d", errno
);
503 pbfd
= bfd_openr (args
, 0);
507 if (!bfd_check_format (pbfd
, bfd_object
))
508 error ("\"%s\": not in executable format: %s",
509 args
, bfd_errmsg (bfd_get_error ()));
511 for (section
= pbfd
->sections
; section
; section
= section
->next
)
513 if (bfd_get_section_flags (pbfd
, section
) & SEC_ALLOC
)
515 bfd_vma section_address
;
516 unsigned long section_size
;
517 const char *section_name
;
519 section_name
= bfd_get_section_name (pbfd
, section
);
520 section_address
= bfd_get_section_vma (pbfd
, section
);
521 section_size
= bfd_section_size (pbfd
, section
);
523 if (bfd_get_section_flags (pbfd
, section
) & SEC_LOAD
)
527 printf_filtered ("[Loading section %s at %x (%d bytes)]\n",
528 section_name
, section_address
,
533 while (section_size
> 0)
538 count
= min (section_size
, 1024);
540 bfd_get_section_contents (pbfd
, section
, buffer
, fptr
,
543 writen (nrom_load_sock
, buffer
, count
);
544 section_address
+= count
;
546 section_size
-= count
;
549 else /* BSS and such */
551 printf_filtered ("[section %s: not loading]\n",
558 error ("\"%s\": Could not open", args
);
560 close (nrom_load_sock
);
563 /* This is called not only when we first attach, but also when the
564 user types "run" after having attached. */
567 nrom_create_inferior (execfile
, args
, env
)
574 /* Open a connection to the remote NetROM devices. */
577 nrom_open (name
, from_tty
)
584 nrom_set_ipaddr (name
, from_tty
);
585 else if (nrom_ipaddr
== 0)
587 "To open a NetROM connection, you must specify the hostname\n\
588 or IP address of the NetROM device you wish to use.");
590 push_target (&nrom_ops
);
592 /* Create the socket used for talking with the target. */
593 nrom_targ_sock
= socket (AF_INET
, SOCK_STREAM
, 0);
595 /* Bind the socket. */
596 nrom_sin
.sin_family
= AF_INET
;
597 nrom_sin
.sin_port
= htons (target_port
);
598 nrom_sin
.sin_addr
.S_un
.S_addr
= htonl (nrom_ipaddr
);
600 /* Connect to the remote host. */
601 if (connect (nrom_targ_sock
, &nrom_sin
, sizeof(nrom_sin
)) == -1)
602 error ("Connect failed, error %d", errno
);
604 /* Create the socket used for talking with the debugger services. */
605 nrom_ctrl_sock
= socket (AF_INET
, SOCK_STREAM
, 0);
607 /* Bind the socket. */
608 nrom_sin
.sin_family
= AF_INET
;
609 nrom_sin
.sin_port
= htons (control_port
);
610 nrom_sin
.sin_addr
.S_un
.S_addr
= htonl (nrom_ipaddr
);
612 /* Connect to the remote host. */
613 if (connect (nrom_ctrl_sock
, &nrom_sin
, sizeof(nrom_sin
)) == -1)
616 close (nrom_targ_sock
);
617 error ("Connect control_socket failed, error %d", errn
);
624 printf_filtered ("Connected to NetROM device \"%s\"", name
);
625 i
= (unsigned char *) &nrom_ipaddr
;
626 printf_filtered (" (%d.%d.%d.%d)\n",
627 UC(i
[0]), UC(i
[1]), UC(i
[2]), UC(i
[3]));
637 /* Close out all files and local state before this target loses control. */
640 nrom_close (quitting
)
645 /* Attach to the target that is already loaded and possibly running */
648 nrom_attach (args
, from_tty
)
656 printf_filtered ("Attaching to NetROM\n");
658 /* clear any pending data on the socket */
659 printf_filtered ("Waiting for pending data to arrive... ");
662 printf_filtered ("that's long enough!\n");
665 if (ioctl(nrom_targ_sock
, FIONREAD
, &nwaiting
) != 0)
667 /* couldn't determine data left */
668 perror("nrom_attach: ioctl FIONREAD");
671 else if (nwaiting
> 0)
673 /* flush incoming data */
674 while (nwaiting
!= 0)
676 if (read (nrom_targ_sock
, buf
, 1) < 0)
678 perror("nrom_attach: read");
681 if (remote_debug
> 2)
682 putc(buf
[0], stdout
);
692 printf_filtered ("Pending data removed\n");
694 /* We will get a task spawn event immediately. */
696 target_has_execution
= 1;
702 /* Terminate the open connection to the remote debugger. Use this
703 when you want to detach and do something else with your gdb. */
706 nrom_detach (args
, from_tty
)
712 printf_filtered ("Ending remote debugging\n");
715 /* Tell the remote machine to resume. */
718 nrom_prepare_to_store()
723 nrom_resume (pid
, step
, siggnal
)
725 enum target_signal siggnal
;
728 send_pkt (GDB_STEP
, NULL
, 0, 0, 0);
730 send_pkt (GDB_CONTINUE
, NULL
, 0, 0, 0);
733 /* Wait until the remote machine stops, then return,
734 storing status in STATUS just as `wait' would. */
737 nrom_wait (pid
, status
)
739 struct target_waitstatus
*status
;
741 static char pkt
[NROM_BUF_SIZE
], inbuf
[NROM_BUF_SIZE
];
744 status
->kind
= TARGET_WAITKIND_EXITED
;
745 status
->value
.integer
= 0;
749 if (read_pkt (pkt
) == -1)
752 if (parse_pkt (pkt
, &phdr
, inbuf
) < 0)
755 printf_filtered ("Bad packet in nrom_wait\n");
760 /* Got good packet. Verify command is right. */
761 if (phdr
.cmd
!= GDB_STATUS
)
763 /* Wrong response. Resend command. */
767 /* Packet is fine. Exit loop. */
772 /* Read the remote registers. */
775 nrom_fetch_registers (regno
)
778 char buf
[NROM_BUF_SIZE
];
779 char regs
[REGISTER_BYTES
];
783 printf_filtered ("reg no is %d\n",regno
);
786 nrom_send (GDB_READ_REGS
, NULL
, 0, -1, 0, buf
);
787 memcpy (regs
, buf
, REGISTER_BYTES
);
788 for (i
= 0; i
< NUM_REGS
; i
++)
789 supply_register (i
, ®s
[REGISTER_BYTE(i
)]);
791 nrom_send (GDB_READ_REGS
, NULL
, 0, regno
, 0, buf
);
793 supply_register(regno
,buf
);
796 memcpy (regs
, buf
, REGISTER_BYTES
);
797 for (i
= 0; i
< NUM_REGS
; i
++)
798 supply_register (i
, ®s
[REGISTER_BYTE(i
)]);
804 nrom_send (cmd
, data
, datalen
, addr
, len
, resp
)
813 char inbuf
[NROM_BUF_SIZE
];
817 while (send_pkt (cmd
, data
, datalen
, addr
, len
) < 0)
819 if (read_pkt (inbuf
) != -1)
821 if (parse_pkt (inbuf
, &phdr
, resp
) < 0)
823 if (phdr
.cmd
!= GDB_NACK
)
830 nrom_set_filetype (args
, fromtty
)
835 nrom_filetype
= BINARY_FTYPE
;
836 else if (args
[0] == 'm')
837 nrom_filetype
= MOTO_SREC
;
838 else if (args
[0] == 'i')
839 nrom_filetype
= INTEL_HEX
;
841 printf_filtered ("Unknown file type\n");
845 nrom_set_ipaddr (args
, fromtty
)
852 unsigned long newip
= 0;
854 /* First do a check to see if they typed in a hostname. */
856 error ("Please enter a hostname or IP address");
858 h
= gethostbyname (args
);
861 /* It is a hostname. We just need the ipaddress. */
862 memcpy (&nrom_ipaddr
, h
->h_addr
, h
->h_length
);
866 /* Better be in decimal dot notation,ie. xx.xx.xx.xx */
867 if (isdigit (args
[0]) && strchr (args
, '.'))
874 while((*args
) && (*args
!= '.'))
881 val
= (int) strtol (buf
, NULL
, 10);
884 error ("Invalid IP address");
887 newip
|= val
<< (8 * j
);
894 error ("Invalid host name/address");
901 nrom_set_load_port (args
, fromtty
)
905 load_port
= (int) strtol (args
, NULL
, 10);
909 nrom_set_target_port (args
, from_tty
)
913 target_port
= (int) strtol (args
, NULL
, 10);
917 nrom_set_control_port (args
, fromtty
)
921 control_port
= (int) strtol (args
, NULL
, 10);
925 nrom_show_status (args
,from_tty
)
931 i
= (unsigned char *)&nrom_ipaddr
;
933 printf_filtered ("NetROM target port is %d\n", target_port
);
934 printf_filtered ("NetROM download port is %d\n", load_port
);
935 printf_filtered ("NetROM debug control port is %d\n", control_port
);
937 printf_filtered ("NetROM IP Address is %d.%d.%d.%d\n",
938 UC(i
[0]), UC(i
[1]), UC(i
[2]), UC(i
[3]));
939 if (nrom_filetype
== BINARY_FTYPE
)
940 printf_filtered ("download filetype is binary\n");
941 else if (nrom_filetype
== MOTO_SREC
)
942 printf_filtered ("download filetype is moto-srec\n");
943 else if (nrom_filetype
== INTEL_HEX
)
944 printf_filtered ("download filetype is intel-hex\n");
947 /* Pass arguments directly to the NetROM. */
950 nrom_passthru (args
, fromtty
)
956 sprintf(buf
, "%s\n", args
);
957 if (nrom_control_send (buf
, strlen (buf
)) < 0)
958 error ("nrom_reset: control_send() of `%s'failed", args
);
962 nrom_store_registers (regno
)
965 char buf
[NROM_BUF_SIZE
];
970 for (i
= 0; i
< REGISTER_BYTES
; i
++)
972 *p
++ = tohex ((registers
[i
] >> 4) & 0xf);
973 *p
++ = tohex (registers
[i
] & 0xf);
976 nrom_send (GDB_WRITE_REGS
, buf
, REGISTER_BYTES
* 2, 0, REGISTER_BYTES
* 2,
981 nrom_xfer_inferior_memory (memaddr
, myaddr
, len
, write
, target
)
986 struct target_ops
*target
;
989 return nrom_write_inferior_memory (memaddr
, myaddr
, len
);
991 return nrom_read_inferior_memory (memaddr
, myaddr
, len
);
994 /* Copy LEN bytes of data from debugger memory at MYADDR
995 to inferior's memory at MEMADDR. Returns errno value. */
998 nrom_write_inferior_memory (memaddr
, myaddr
, len
)
1003 char buf
[NROM_BUF_SIZE
],obuf
[NROM_BUF_SIZE
];
1008 for (i
= 0; i
< len
; i
++)
1010 *p
++ = tohex ((myaddr
[i
] >> 4) & 0xf);
1011 *p
++ = tohex (myaddr
[i
] & 0xf);
1014 nrom_send (GDB_WRITE_MEM
, obuf
, len
* 2, memaddr
, len
, buf
);
1019 /* Read LEN bytes from inferior memory at MEMADDR. Put the result
1020 at debugger address MYADDR. Returns errno value. */
1023 nrom_read_inferior_memory (memaddr
, myaddr
, len
)
1028 char buf
[NROM_BUF_SIZE
];
1030 nrom_send (GDB_READ_MEM
, NULL
, 0, memaddr
, len
, buf
);
1031 memcpy (myaddr
, buf
, len
);
1036 nrom_files_info (ignore
)
1037 struct target_ops
*ignore
;
1044 unpush_target (&nrom_ops
);
1045 generic_mourn_inferior ();
1048 /* Convert a packet into its parts and verify check sum. */
1051 parse_pkt (pkt
, hdr
, data
)
1056 unsigned char xcsum
;
1057 unsigned char *dptr
;
1059 /* Build packet header from received data. */
1060 hex2mem (pkt
, (char *) hdr
, sizeof(GPKTHDR
));
1061 if (remote_debug
> 1)
1063 printf_filtered ("Packet received: csum = %x,seq number = %x,len = %d\n",
1064 hdr
->csum
,hdr
->seq_num
,hdr
->len
);
1065 printf_filtered ("cmd = %x,addr = %x,datalen = %d\n",
1066 hdr
->cmd
,hdr
->addr
,hdr
->datalen
);
1068 /* Skip first two bytes of packet when computing checksum. */
1069 dptr
= (sizeof(GPKTHDR
) * 2) + pkt
;
1071 if (remote_debug
> 1)
1073 printf_filtered ("Computing checksum over pkt %s\n",pkt
);
1074 printf_filtered ("Of length %d\n",strlen(pkt
));
1076 xcsum
= compute_csum (pkt
, strlen (pkt
));
1077 if (xcsum
!= hdr
->csum
)
1080 printf_filtered ("Checksum failure: computed %x, received %x\n",xcsum
,
1085 /* Copy data portion to callers data buffer converting from ASCII
1086 to data as we go. */
1087 hex2mem (dptr
, data
, hdr
->datalen
);
1096 struct sockaddr_in from
;
1097 int from_len
= sizeof(from
);
1108 /* Don't let us get wedged if the target is losing. */
1115 /* Perform read on socket. This will wait. */
1116 bufdata
= recvfrom (nrom_targ_sock
, readbuf
, n
, 0, &from
, &from_len
);
1119 printf_filtered ("Error on socket read of %d\n",errno
);
1122 if (remote_debug
> 2)
1124 readbuf
[bufdata
] = '\0';
1125 printf_filtered ("Received %d bytes. Data is %s\n",
1130 /* skip stuff between packets */
1131 while (gotstart
== 0 && bufdata
!= 0
1132 && readbuf
[bufindex
] != GDB_START_DELIMITER
)
1138 /* look for a start if we haven't seen one */
1139 if (gotstart
== 0 && bufdata
!= 0
1140 && readbuf
[bufindex
] == GDB_START_DELIMITER
)
1147 /* copy packet data */
1150 while (bufdata
&& readbuf
[bufindex
] != GDB_END_DELIMITER
)
1152 *p
= readbuf
[bufindex
];
1157 if (total_len
> NROM_BUF_SIZE
)
1159 error ("read_pkt: packet length exceeds %d\n",
1165 if (remote_debug
> 2)
1166 printf_filtered ("Packet is '%s'\n", pkt
);
1169 /* Make sure this is the end of the packet. */
1170 if (gotstart
!= 0 && bufdata
!= 0
1171 && readbuf
[bufindex
] == GDB_END_DELIMITER
)
1176 /* Ensure that the packet is terminated. */
1178 if (remote_debug
> 2)
1179 printf_filtered ("Returning '%s'\n", pkt
);
1188 while (send_pkt (GDB_QUERY_CMD
, NULL
, 0, 0, 0) < 0)
1193 send_pkt (cmd
, data
, datalen
, addr
, len
)
1202 struct sockaddr_in mysin
;
1207 /* Get a sequence number for this packet. */
1208 seq
= get_seq_number ();
1209 /* Build the packet. */
1210 build_pkt (cmd
, data
, datalen
, workbuf
, addr
, len
, seq
);
1211 /* Put delimiters around the pkt. */
1212 memset (sendbuf
, 0, NROM_BUF_SIZE
);
1213 sendbuf
[0] = GDB_START_DELIMITER
;
1214 strcat (sendbuf
, workbuf
);
1215 c
[0] = GDB_END_DELIMITER
;
1217 strcat (sendbuf
, c
);
1219 /* Send the packet out on our socket. */
1220 if (remote_debug
> 1)
1221 printf_filtered ("Sending pkt: %s\n", sendbuf
);
1222 mysin
.sin_family
= AF_INET
;
1223 mysin
.sin_port
= target_port
;
1224 mysin
.sin_addr
.S_un
.S_addr
= nrom_ipaddr
;
1227 while (send_cnt
< MAX_SEND_ATTEMPTS
)
1229 if (sendto (nrom_targ_sock
, sendbuf
, strlen(sendbuf
), 0, &mysin
,
1230 sizeof(struct sockaddr_in
)) < 0)
1232 printf_filtered ("sendto error of %d\n", errno
);
1238 if (send_cnt
>= MAX_SEND_ATTEMPTS
)
1240 printf_filtered ("Socket send failed after %d tries\n",
1248 /* Convert number NIB to a hex digit. */
1257 return 'a' + nib
- 10;
1260 /* snatched from Stevens, pp279+ */
1263 writen (sock
, ptr
, nbytes
)
1268 int nleft
, nwritten
;
1274 nwritten
= write (sock
, buf
, nleft
);
1280 return (nbytes
- nleft
);
1283 /* Define the target vector. */
1285 struct target_ops nrom_ops
= {
1286 "nrom", /* to_shortname */
1287 "Remote XDI `NetROM' target", /* to_longname */
1288 "Remote debug using a NetROM over Ethernet",
1289 nrom_open
, /* to_open */
1294 nrom_wait
, /* to_wait */
1295 nrom_fetch_registers
, /* to_fetch_registers */
1296 nrom_store_registers
, /* to_store_registers */
1297 nrom_prepare_to_store
, /* to_prepare_to_store */
1298 nrom_xfer_inferior_memory
, /* to_xfer_memory */
1299 nrom_files_info
, /* to_files_info */
1300 NULL
, /* to_insert_breakpoint */
1301 NULL
, /* to_remove_breakpoint */
1310 nrom_create_inferior
, /* to_create_inferior */
1313 0, /* to_notice_signals */
1315 process_stratum
, /* to_stratum */
1321 0, /* to_has_execution */
1322 NULL
, /* sections */
1323 NULL
, /* sections_end */
1324 OPS_MAGIC
/* to_magic */
1328 _initialize_remote_nrom ()
1330 add_target (&nrom_ops
);
1332 /* Add some commands for helpers. */
1333 add_cmd ("nrom_ipaddr", no_class
, nrom_set_ipaddr
,
1334 "Set the IP Address of the NetROM\n",
1336 add_cmd ("target_port", no_class
, nrom_set_target_port
,
1337 "Set the Port to use for NetROM target communication\n",
1339 add_cmd ("load_port", no_class
, nrom_set_load_port
,
1340 "Set the Port to use for NetROM downloads\n",
1342 add_cmd ("control_port", no_class
, nrom_set_control_port
,
1343 "Set the Port to use for NetROM debugger services\n",
1345 add_cmd ("nrom_filetype", no_class
, nrom_set_filetype
,
1346 "Set the filetype to use on NetROM downloads",
1349 add_cmd ("nrom", no_class
, nrom_show_status
,
1350 "Show the current NetROM status\n",
1353 add_cmd ("nrom", no_class
, nrom_passthru
,
1354 "Pass arguments as command to NetROM",