* defs.h maint.c monitor.c remote-mips.c remote.c: Add support
[binutils-gdb.git] / gdb / remote-nrom.c
1 /* Remote debugging with the XLNT Designs, Inc (XDI) NetROM.
2 Copyright 1990, 1991, 1992, 1995 Free Software Foundation, Inc.
3 Contributed by:
4 Roger Moyers
5 XLNT Designs, Inc.
6 15050 Avenue of Science, Suite 106
7 San Diego, CA 92128
8 (619)487-9320
9 roger@xlnt.com
10 Adapted from work done at Cygnus Support in remote-nindy.c,
11 later merged in by Stan Shebs at Cygnus.
12
13 This file is part of GDB.
14
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.
19
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.
24
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. */
28
29 #include "defs.h"
30 #include "gdbcmd.h"
31 #include "serial.h"
32
33 #include <string.h>
34 #include "inferior.h"
35 #include "wait.h"
36 #include "value.h"
37 #include <ctype.h>
38 #include <fcntl.h>
39 #include <signal.h>
40 #include <errno.h>
41 #include <stropts.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/ioctl.h>
45 #include <netinet/in.h>
46 #include <netdb.h>
47 #include <unistd.h>
48 #include "terminal.h"
49 #include "target.h"
50 #include "gdbcore.h"
51
52 /* Packet header found on every packet sent to/from GDB. */
53
54 typedef struct gpkthdr {
55 unsigned char csum; /* Check sum */
56 unsigned char seq_num; /* Sequence number */
57 unsigned short len; /* Number of bytes in packet */
58 unsigned char cmd; /* GDB command */
59 unsigned char pad[3];
60 unsigned long addr; /* Address if needed */
61 unsigned long datalen; /* # of bytes to read/write */
62 } GPKTHDR;
63
64 #define GDB_START_DELIMITER '['
65 #define GDB_END_DELIMITER ']'
66
67 /* GDB requests. */
68
69 #define GDB_QUERY_CMD 0x01
70 #define GDB_STATUS 0x02
71 #define GDB_READ_REGS 0x03
72 #define GDB_WRITE_REGS 0x04
73 #define GDB_READ_MEM 0x05
74 #define GDB_WRITE_MEM 0x06
75 #define GDB_CONTINUE 0x07
76 #define GDB_STEP 0x08
77
78 /* Responses. */
79
80 #define GDB_ACK 0x11
81 #define GDB_NACK 0x12
82 #define GDB_READ_REG_RESP 0x13
83 #define GDB_READ_MEM_RESP 0x14
84 #define GDB_WRITE_REG_RESP 0x15
85 #define GDB_WRITE_MEM_RESP 0x16
86
87 #define GDB_BP_TRAP "05"
88 #define GDB_BUSERR_TRAP "10"
89 #define GDB_ILLOP_TRAP "40"
90
91 #define GDB_ACK_VALUE "OK"
92
93 /* Default ports used to talk with the NetROM. */
94
95 #define DEFAULT_NETROM_TARGET_PORT 1235
96 #define DEFAULT_NETROM_LOAD_PORT 1236
97 #define DEFAULT_NETROM_CONTROL_PORT 1237
98
99 #define UC(b) (((long)b)&0xff)
100
101 #define NROM_BUF_SIZE 2048
102 #define MAX_SEND_ATTEMPTS 10
103 #define READ_BUF_SIZE 2048
104
105 /* Definitions for filetype. */
106
107 #define BINARY_FTYPE 0
108 #define MOTO_SREC 1
109 #define INTEL_HEX 2
110
111 #if 0 /* for debugging, if anyone cares */
112 static char *GCMDTYPE[] = {
113 "ZERO",
114 "GDB_QUERY",
115 "GDB_STATUS",
116 "GDB_READ_REGS",
117 "GDB_WRITE_REGS",
118 "GDB_READ_MEM",
119 "GDB_WRITE_MEM",
120 "GDB_CONTINUE",
121 "GDB_STEP",
122 "CMD9",
123 "CMDA",
124 "CMDB",
125 "CMDC",
126 "CMDD",
127 "CMDE",
128 "CMDF",
129 "RESP0",
130 "GDB_ACK",
131 "GDB_NACK",
132 "GDB_READ_REG_RESP",
133 "GDB_READ_MEM_RESP",
134 "GDB_WRITE_REG_RESP",
135 "GDB_WRITE_MEM_RESP"
136 };
137 #endif
138
139 static void nrom_attach PARAMS ((char *, int));
140
141 static int nrom_can_run PARAMS ((void));
142
143 static void nrom_close PARAMS ((int));
144
145 static void nrom_create_inferior PARAMS ((char *, char *, char **));
146
147 static void nrom_detach PARAMS ((char *, int));
148
149 static void nrom_fetch_registers PARAMS ((int));
150
151 static void nrom_files_info PARAMS ((struct target_ops *));
152
153 static void nrom_kill PARAMS ((void));
154
155 static void nrom_load PARAMS ((char *, int));
156
157 static void nrom_mourn PARAMS ((void));
158
159 static void nrom_open PARAMS ((char *,int));
160
161 static void nrom_resume PARAMS ((int, int, enum target_signal));
162
163 static void nrom_prepare_to_store PARAMS ((void));
164
165 static void nrom_store_registers PARAMS ((int));
166
167 static int nrom_wait PARAMS ((int pid, struct target_waitstatus *status));
168
169 static int nrom_xfer_inferior_memory PARAMS ((CORE_ADDR, char *, int, int,
170 struct target_ops *));
171
172 static int nrom_write_inferior_memory PARAMS ((CORE_ADDR, char *, int));
173
174 static int nrom_read_inferior_memory PARAMS ((CORE_ADDR, char *, int));
175
176 /* New commands. */
177
178 static void nrom_set_target_port PARAMS ((char *, int));
179
180 static void nrom_set_control_port PARAMS ((char *, int));
181
182 static void nrom_set_load_port PARAMS ((char *, int));
183
184 static void nrom_set_ipaddr PARAMS ((char *, int));
185
186 static void nrom_set_filetype PARAMS ((char *, int));
187
188 static void nrom_show_status PARAMS ((char *, int));
189
190 static void nrom_passthru PARAMS ((char *, int));
191
192 /* Packet functions. */
193
194 static void build_pkt PARAMS ((int, unsigned char *, long,
195 unsigned char *, unsigned long, unsigned long,
196 int));
197
198 static int compute_csum PARAMS ((unsigned char *, int));
199
200 #if 0
201 static void dump_pkt PARAMS ((GPKTHDR *, unsigned char *));
202 #endif
203
204 static int get_seq_number PARAMS ((void));
205
206 static char *hex2mem PARAMS ((char *, char *, int));
207
208 static char *mem2hex PARAMS ((char *, char *, int));
209
210 static void nrom_send PARAMS ((int, char *, int, long, long, char *));
211
212 static void send_query_cmd PARAMS ((void));
213
214 static int send_pkt PARAMS ((int, char *, int, long, int));
215
216 static int read_pkt PARAMS ((char *));
217
218 static void send_query_cmd PARAMS ((void));
219
220 static int tohex PARAMS ((int));
221
222 static int parse_pkt PARAMS ((unsigned char *, GPKTHDR *, char *));
223
224 static int writen PARAMS ((int, char *, int));
225
226 /* Private globals. */
227
228 /* We talk to the NetROM over these sockets. */
229
230 static int nrom_load_sock = -1;
231 static int nrom_targ_sock = -1;
232 static int nrom_ctrl_sock = -1;
233
234 static serial_t load_desc = NULL;
235 static serial_t targ_desc = NULL;
236 static serial_t ctrl_desc = NULL;
237
238 /* For binding to the socket we ned a sockaddr_in structure. */
239
240 static struct sockaddr_in nrom_sin;
241
242 /* The IP Address of the NetROM is needed so we know who to talk to. */
243
244 static unsigned long nrom_ipaddr = 0;
245
246 static int load_port = DEFAULT_NETROM_LOAD_PORT;
247 static int target_port = DEFAULT_NETROM_TARGET_PORT;
248 static int control_port = DEFAULT_NETROM_CONTROL_PORT;
249
250 static int nrom_filetype = BINARY_FTYPE;
251
252 static unsigned char host_seq_num = 0;
253
254 static char hexchars[] = "0123456789abcdef";
255
256 static char freadbuf[READ_BUF_SIZE];
257
258 static char readbuf[NROM_BUF_SIZE];
259 static int bufdata = 0;
260 static int bufindex = 0;
261
262 static char workbuf[NROM_BUF_SIZE];
263 static char sendbuf[NROM_BUF_SIZE];
264
265 static char nrom_hostname[100];
266
267 /* Forward data declaration. */
268
269 extern struct target_ops nrom_ops;
270
271 /* This routine builds a packet to send to gdb running on the host. */
272
273 static void
274 build_pkt (cmd, data, datalen, pkt, addr, len, seq)
275 int cmd;
276 unsigned char *data;
277 long datalen;
278 unsigned char *pkt;
279 unsigned long addr;
280 unsigned long len;
281 int seq;
282 {
283 GPKTHDR phdr;
284 char *dptr;
285 char *pktptr;
286 unsigned char csum;
287 int plen;
288
289 phdr.csum = 0;
290 phdr.len = sizeof(GPKTHDR) + datalen;
291 phdr.seq_num = seq;
292 phdr.cmd = cmd;
293 phdr.pad[0] = phdr.pad[1] = phdr.pad[2] = 0;
294 phdr.addr = addr;
295 phdr.datalen = len;
296 /* Convert packet header to ASCII. */
297 dptr = mem2hex ((char *) &phdr, pkt, sizeof(GPKTHDR));
298
299 /* Calculate pkt length now that it is converted. */
300 plen = (int) ((unsigned long)dptr - (unsigned long)pkt) + datalen;
301 /* Put data in packet. */
302 if (datalen > 0)
303 memcpy (dptr, data, datalen);
304
305 /* Compute checksum. For computing checksum we skip first two bytes
306 since this is where the checksum will go. */
307 pktptr = pkt + 2;
308 csum = compute_csum (pktptr, plen - 2);
309 *pkt++ = hexchars[csum >> 4];
310 *pkt++ = hexchars[csum % 16];
311 dptr += datalen;
312 *dptr = '\0';
313 }
314
315 static int
316 compute_csum (data, len)
317 unsigned char *data;
318 int len;
319 {
320 unsigned char csum;
321
322 csum = 0;
323 while (len > 0)
324 {
325 csum += *data++;
326 --len;
327 }
328 return csum;
329 }
330
331 #if 0 /* for debugging, if anyone cares */
332 static void
333 dump_pkt (hdr, data)
334 GPKTHDR *hdr;
335 unsigned char *data;
336 {
337 int i;
338
339 printf_filtered ("PACKET: csum = %x,seq = %d,len = %d\n",hdr->csum,hdr->seq_num,
340 hdr->len);
341
342 printf_filtered ("cmd = %s,addr = %x, datalen = %d\n", GCMDTYPE[hdr->cmd],
343 hdr->addr,hdr->datalen);
344 if (hdr->datalen)
345 {
346 for (i = 0; i < hdr->datalen * 2; i++)
347 printf_filtered ("%x",data[i]);
348 printf_filtered ("\n");
349 }
350 }
351 #endif
352
353 static int
354 get_seq_number()
355 {
356 return host_seq_num++;
357 }
358
359 int
360 hex (ch)
361 int ch;
362 {
363 if ((ch >= 'a') && (ch <= 'f'))
364 return (ch - 'a' + 10);
365 if((ch >= 'A') && (ch <= 'F'))
366 return ((ch - 'A') + 10);
367 if ((ch >= '0') && (ch <= '9'))
368 return (ch - '0');
369 return 0;
370 }
371
372 /* Convert the hex array pointed to by buf into binary to be placed in mem
373 return a pointer to the character AFTER the last byte written. */
374
375 static char*
376 hex2mem(buf, mem, count)
377 char* buf;
378 char* mem;
379 int count;
380 {
381 int i;
382 unsigned char ch;
383
384 for (i = 0; i < count; i++)
385 {
386 ch = hex(*buf++) << 4;
387 ch = ch + hex(*buf++);
388 *mem++ = ch;
389 }
390 return mem;
391 }
392
393 /* Convert the memory pointed to by mem into hex, placing result in buf
394 return a pointer to the last char put in buf (null) */
395
396 static char *
397 mem2hex (mem, buf, count)
398 char* mem;
399 char* buf;
400 int count;
401 {
402 int i;
403 unsigned char ch;
404
405 for (i = 0; i < count; i++)
406 {
407 ch = *mem++;
408 *buf++ = hexchars[ch >> 4];
409 *buf++ = hexchars[ch % 16];
410 }
411 *buf = 0;
412 return buf;
413 }
414
415 /* Scan input from the remote system, until STRING is found. If BUF is non-
416 zero, then collect input until we have collected either STRING or BUFLEN-1
417 chars. In either case we terminate BUF with a 0. If input overflows BUF
418 because STRING can't be found, return -1, else return number of chars in BUF
419 (minus the terminating NUL). Note that in the non-overflow case, STRING
420 will be at the end of BUF. */
421
422 static int
423 expect (string)
424 char *string;
425 {
426 char *p = string;
427 int c;
428
429 immediate_quit = 1;
430
431 while (1)
432 {
433 c = SERIAL_READCHAR (ctrl_desc, 5);
434
435 if (c == *p++)
436 {
437 if (*p == '\0')
438 {
439 immediate_quit = 0;
440
441 return 0;
442 }
443 }
444 else
445 {
446 fputc_unfiltered (c, gdb_stdout);
447 p = string;
448 if (c == *p)
449 p++;
450 }
451 }
452 }
453
454 static int
455 nrom_control_send (s, nbytes)
456 char *s;
457 int nbytes;
458 {
459 SERIAL_WRITE (ctrl_desc, s, nbytes);
460
461 return 0;
462 }
463
464 static void
465 nrom_kill ()
466 {
467 nrom_close (0);
468 }
469
470 static serial_t
471 open_socket (name, port)
472 char *name;
473 int port;
474 {
475 char sockname[100];
476 serial_t desc;
477
478 sprintf (sockname, "%s:%d", name, port);
479 desc = SERIAL_OPEN (sockname);
480 if (!desc)
481 perror_with_name (sockname);
482
483 return desc;
484 }
485
486 /* Download a file specified in ARGS to the netROM. */
487
488 static void
489 nrom_load (args, fromtty)
490 char *args;
491 int fromtty;
492 {
493 int fd, rd_amt, fsize;
494 struct sockaddr_in sin;
495 bfd *pbfd;
496 asection *section;
497 char *downloadstring = "download 0\n";
498
499 /* Tell the netrom to get ready to download. */
500 if (nrom_control_send (downloadstring, strlen (downloadstring)) < 0)
501 error ("nrom_load: control_send() of `%s' failed", downloadstring);
502
503 expect ("Waiting for a connection...\n");
504
505 load_desc = open_socket (nrom_hostname, load_port);
506
507 pbfd = bfd_openr (args, 0);
508
509 if (pbfd)
510 {
511 if (!bfd_check_format (pbfd, bfd_object))
512 error ("\"%s\": not in executable format: %s",
513 args, bfd_errmsg (bfd_get_error ()));
514
515 for (section = pbfd->sections; section; section = section->next)
516 {
517 if (bfd_get_section_flags (pbfd, section) & SEC_ALLOC)
518 {
519 bfd_vma section_address;
520 unsigned long section_size;
521 const char *section_name;
522
523 section_name = bfd_get_section_name (pbfd, section);
524 section_address = bfd_get_section_vma (pbfd, section);
525 section_size = bfd_section_size (pbfd, section);
526
527 if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
528 {
529 file_ptr fptr;
530
531 printf_filtered ("[Loading section %s at %x (%d bytes)]\n",
532 section_name, section_address,
533 section_size);
534
535 fptr = 0;
536
537 while (section_size > 0)
538 {
539 char buffer[1024];
540 int count;
541
542 count = min (section_size, 1024);
543
544 bfd_get_section_contents (pbfd, section, buffer, fptr,
545 count);
546
547 SERIAL_WRITE (load_desc, buffer, count);
548 section_address += count;
549 fptr += count;
550 section_size -= count;
551 }
552 }
553 else /* BSS and such */
554 {
555 printf_filtered ("[section %s: not loading]\n",
556 section_name);
557 }
558 }
559 }
560 }
561 else
562 error ("\"%s\": Could not open", args);
563
564 SERIAL_CLOSE (load_desc);
565 load_desc = NULL;
566 }
567
568 /* This is called not only when we first attach, but also when the
569 user types "run" after having attached. */
570
571 static void
572 nrom_create_inferior (execfile, args, env)
573 char *execfile;
574 char *args;
575 char **env;
576 {
577 }
578
579 /* Open a connection to the remote NetROM devices. */
580
581 static void
582 nrom_open (name, from_tty)
583 char *name;
584 int from_tty;
585 {
586 int errn;
587
588 if (!name || strchr (name, '/') || strchr (name, ':'))
589 error (
590 "To open a NetROM connection, you must specify the hostname\n\
591 or IP address of the NetROM device you wish to use.");
592
593 strcpy (nrom_hostname, name);
594
595 target_preopen (from_tty);
596
597 unpush_target (&nrom_ops);
598
599 targ_desc = open_socket (nrom_hostname, target_port);
600 ctrl_desc = open_socket (nrom_hostname, control_port);
601
602 push_target (&nrom_ops);
603
604 if (from_tty)
605 printf_filtered ("Connected to NetROM device \"%s\"\n", nrom_hostname);
606 }
607
608 static int
609 nrom_can_run ()
610 {
611 return 1;
612 }
613
614 /* Close out all files and local state before this target loses control. */
615
616 static void
617 nrom_close (quitting)
618 int quitting;
619 {
620 if (load_desc)
621 SERIAL_CLOSE (load_desc);
622 if (targ_desc)
623 SERIAL_CLOSE (targ_desc);
624 if (ctrl_desc)
625 SERIAL_CLOSE (ctrl_desc);
626
627 load_desc = NULL;
628 targ_desc = NULL;
629 ctrl_desc = NULL;
630 }
631
632 /* Attach to the target that is already loaded and possibly running */
633
634 static void
635 nrom_attach (args, from_tty)
636 char *args;
637 int from_tty;
638 {
639 int nwaiting;
640 char buf[10];
641
642 if (from_tty)
643 printf_filtered ("Attaching to NetROM\n");
644
645 /* clear any pending data on the socket */
646 printf_filtered ("Waiting for pending data to arrive... ");
647 fflush(stdout);
648 sleep(1);
649 printf_filtered ("that's long enough!\n");
650 while (1)
651 {
652 if (ioctl(nrom_targ_sock, FIONREAD, &nwaiting) != 0)
653 {
654 /* couldn't determine data left */
655 perror("nrom_attach: ioctl FIONREAD");
656 break;
657 }
658 else if (nwaiting > 0)
659 {
660 /* flush incoming data */
661 while (nwaiting != 0)
662 {
663 if (read (nrom_targ_sock, buf, 1) < 0)
664 {
665 perror("nrom_attach: read");
666 exit(1);
667 }
668 if (remote_debug > 2)
669 putc(buf[0], stdout);
670 nwaiting--;
671 }
672 }
673 else
674 {
675 /* no more data */
676 break;
677 }
678 }
679 printf_filtered ("Pending data removed\n");
680
681 /* We will get a task spawn event immediately. */
682 send_query_cmd ();
683 target_has_execution = 1;
684 /*
685 start_remote();
686 */
687 }
688
689 /* Terminate the open connection to the remote debugger. Use this
690 when you want to detach and do something else with your gdb. */
691
692 static void
693 nrom_detach (args, from_tty)
694 char *args;
695 int from_tty;
696 {
697 pop_target ();
698 if (from_tty)
699 printf_filtered ("Ending remote debugging\n");
700 }
701
702 /* Tell the remote machine to resume. */
703
704 static void
705 nrom_prepare_to_store()
706 {
707 }
708
709 static void
710 nrom_resume (pid, step, siggnal)
711 int pid, step;
712 enum target_signal siggnal;
713 {
714 if (step)
715 send_pkt (GDB_STEP, NULL, 0, 0, 0);
716 else
717 send_pkt (GDB_CONTINUE, NULL, 0, 0, 0);
718 }
719
720 /* Wait until the remote machine stops, then return,
721 storing status in STATUS just as `wait' would. */
722
723 static int
724 nrom_wait (pid, status)
725 int pid;
726 struct target_waitstatus *status;
727 {
728 static char pkt[NROM_BUF_SIZE], inbuf[NROM_BUF_SIZE];
729 GPKTHDR phdr;
730
731 status->kind = TARGET_WAITKIND_EXITED;
732 status->value.integer = 0;
733
734 while (1)
735 {
736 if (read_pkt (pkt) == -1)
737 continue;
738
739 if (parse_pkt (pkt, &phdr, inbuf) < 0)
740 {
741 if (remote_debug)
742 printf_filtered ("Bad packet in nrom_wait\n");
743 send_query_cmd ();
744 continue;
745 }
746
747 /* Got good packet. Verify command is right. */
748 if (phdr.cmd != GDB_STATUS)
749 {
750 /* Wrong response. Resend command. */
751 send_query_cmd ();
752 continue;
753 }
754 /* Packet is fine. Exit loop. */
755 return inferior_pid;
756 }
757 }
758
759 /* Read the remote registers. */
760
761 static void
762 nrom_fetch_registers (regno)
763 int regno;
764 {
765 char buf[NROM_BUF_SIZE];
766 char regs[REGISTER_BYTES];
767 int i;
768
769 #ifdef DEBUG
770 printf_filtered ("reg no is %d\n",regno);
771 #endif
772
773 nrom_send (GDB_READ_REGS, NULL, 0, -1, 0, buf);
774 memcpy (regs, buf, REGISTER_BYTES);
775 for (i = 0; i < NUM_REGS; i++)
776 supply_register (i, &regs[REGISTER_BYTE(i)]);
777 #ifdef NO_SINGLE_REG
778 nrom_send (GDB_READ_REGS, NULL, 0, regno, 0, buf);
779 if (regno != -1)
780 supply_register(regno,buf);
781 else
782 {
783 memcpy (regs, buf, REGISTER_BYTES);
784 for (i = 0; i < NUM_REGS; i++)
785 supply_register (i, &regs[REGISTER_BYTE(i)]);
786 }
787 #endif
788 }
789
790 static void
791 nrom_send (cmd, data, datalen, addr, len, resp)
792 int cmd;
793 char *data;
794 int datalen;
795 long addr;
796 long len;
797 char *resp;
798 {
799 GPKTHDR phdr;
800 char inbuf[NROM_BUF_SIZE];
801
802 while (1)
803 {
804 while (send_pkt (cmd, data, datalen, addr, len) < 0)
805 ;
806 if (read_pkt (inbuf) != -1)
807 {
808 if (parse_pkt (inbuf, &phdr, resp) < 0)
809 continue;
810 if (phdr.cmd != GDB_NACK)
811 return;
812 }
813 }
814 }
815
816 static void
817 nrom_set_filetype (args, fromtty)
818 char *args;
819 int fromtty;
820 {
821 if (args[0] == 'b')
822 nrom_filetype = BINARY_FTYPE;
823 else if (args[0] == 'm')
824 nrom_filetype = MOTO_SREC;
825 else if (args[0] == 'i')
826 nrom_filetype = INTEL_HEX;
827 else
828 printf_filtered ("Unknown file type\n");
829 }
830
831 static void
832 nrom_set_ipaddr (args, fromtty)
833 char *args;
834 int fromtty;
835 {
836 struct hostent *h;
837 char buf[10];
838 int i,j,val;
839 unsigned long newip = 0;
840
841 /* First do a check to see if they typed in a hostname. */
842 if (!(*args))
843 error ("Please enter a hostname or IP address");
844
845 h = gethostbyname (args);
846 if (h)
847 {
848 /* It is a hostname. We just need the ipaddress. */
849 memcpy (&nrom_ipaddr, h->h_addr, h->h_length);
850 }
851 else
852 {
853 /* Better be in decimal dot notation,ie. xx.xx.xx.xx */
854 if (isdigit (args[0]) && strchr (args, '.'))
855 {
856 j = 4;
857 while (j)
858 {
859 memset (buf, 0, 10);
860 i = 0;
861 while((*args) && (*args != '.'))
862 {
863 buf[i] = *args++;
864 i++;
865 }
866 if (i)
867 {
868 val = (int) strtol (buf, NULL, 10);
869
870 if (val > 255)
871 error ("Invalid IP address");
872
873 j--;
874 newip |= val << (8 * j);
875 args++;
876 }
877 }
878 }
879 else
880 {
881 error ("Invalid host name/address");
882 }
883 nrom_ipaddr = newip;
884 }
885 }
886
887 static void
888 nrom_set_load_port (args, fromtty)
889 char *args;
890 int fromtty;
891 {
892 load_port = (int) strtol (args, NULL, 10);
893 }
894
895 static void
896 nrom_set_target_port (args, from_tty)
897 char *args;
898 int from_tty;
899 {
900 target_port = (int) strtol (args, NULL, 10);
901 }
902
903 static void
904 nrom_set_control_port (args, fromtty)
905 char *args;
906 int fromtty;
907 {
908 control_port = (int) strtol (args, NULL, 10);
909 }
910
911 static void
912 nrom_show_status (args,from_tty)
913 char *args;
914 int from_tty;
915 {
916 unsigned char *i;
917
918 i = (unsigned char *)&nrom_ipaddr;
919
920 printf_filtered ("NetROM target port is %d\n", target_port);
921 printf_filtered ("NetROM download port is %d\n", load_port);
922 printf_filtered ("NetROM debug control port is %d\n", control_port);
923
924 printf_filtered ("NetROM IP Address is %d.%d.%d.%d\n",
925 UC(i[0]), UC(i[1]), UC(i[2]), UC(i[3]));
926 if (nrom_filetype == BINARY_FTYPE)
927 printf_filtered ("download filetype is binary\n");
928 else if (nrom_filetype == MOTO_SREC)
929 printf_filtered ("download filetype is moto-srec\n");
930 else if (nrom_filetype == INTEL_HEX)
931 printf_filtered ("download filetype is intel-hex\n");
932 }
933
934 /* Pass arguments directly to the NetROM. */
935
936 static void
937 nrom_passthru (args, fromtty)
938 char *args;
939 int fromtty;
940 {
941 char buf[1024];
942
943 sprintf(buf, "%s\n", args);
944 if (nrom_control_send (buf, strlen (buf)) < 0)
945 error ("nrom_reset: control_send() of `%s'failed", args);
946 }
947
948 static void
949 nrom_store_registers (regno)
950 int regno;
951 {
952 char buf[NROM_BUF_SIZE];
953 int i;
954 char *p;
955
956 p = buf;
957 for (i = 0; i < REGISTER_BYTES; i++)
958 {
959 *p++ = tohex ((registers[i] >> 4) & 0xf);
960 *p++ = tohex (registers[i] & 0xf);
961 }
962 *p = '\0';
963 nrom_send (GDB_WRITE_REGS, buf, REGISTER_BYTES * 2, 0, REGISTER_BYTES * 2,
964 buf);
965 }
966
967 static int
968 nrom_xfer_inferior_memory (memaddr, myaddr, len, write, target)
969 CORE_ADDR memaddr;
970 char *myaddr;
971 int len;
972 int write;
973 struct target_ops *target;
974 {
975 if (write)
976 return nrom_write_inferior_memory (memaddr, myaddr, len);
977 else
978 return nrom_read_inferior_memory (memaddr, myaddr, len);
979 }
980
981 /* Copy LEN bytes of data from debugger memory at MYADDR
982 to inferior's memory at MEMADDR. Returns errno value. */
983
984 static int
985 nrom_write_inferior_memory (memaddr, myaddr, len)
986 CORE_ADDR memaddr;
987 char *myaddr;
988 int len;
989 {
990 char buf[NROM_BUF_SIZE],obuf[NROM_BUF_SIZE];
991 int i;
992 char *p;
993
994 p = obuf;
995 for (i = 0; i < len; i++)
996 {
997 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
998 *p++ = tohex (myaddr[i] & 0xf);
999 }
1000 *p = '\0';
1001 nrom_send (GDB_WRITE_MEM, obuf, len * 2, memaddr, len, buf);
1002
1003 return len;
1004 }
1005
1006 /* Read LEN bytes from inferior memory at MEMADDR. Put the result
1007 at debugger address MYADDR. Returns errno value. */
1008
1009 static int
1010 nrom_read_inferior_memory (memaddr, myaddr, len)
1011 CORE_ADDR memaddr;
1012 char *myaddr;
1013 int len;
1014 {
1015 char buf[NROM_BUF_SIZE];
1016
1017 nrom_send (GDB_READ_MEM, NULL, 0, memaddr, len, buf);
1018 memcpy (myaddr, buf, len);
1019 return len;
1020 }
1021
1022 static void
1023 nrom_files_info (ignore)
1024 struct target_ops *ignore;
1025 {
1026 }
1027
1028 static void
1029 nrom_mourn()
1030 {
1031 unpush_target (&nrom_ops);
1032 generic_mourn_inferior ();
1033 }
1034
1035 /* Convert a packet into its parts and verify check sum. */
1036
1037 static int
1038 parse_pkt (pkt, hdr, data)
1039 unsigned char *pkt;
1040 GPKTHDR *hdr;
1041 char *data;
1042 {
1043 unsigned char xcsum;
1044 unsigned char *dptr;
1045
1046 /* Build packet header from received data. */
1047 hex2mem (pkt, (char *) hdr, sizeof(GPKTHDR));
1048 if (remote_debug > 1)
1049 {
1050 printf_filtered ("Packet received: csum = %x,seq number = %x,len = %d\n",
1051 hdr->csum,hdr->seq_num,hdr->len);
1052 printf_filtered ("cmd = %x,addr = %x,datalen = %d\n",
1053 hdr->cmd,hdr->addr,hdr->datalen);
1054 }
1055 /* Skip first two bytes of packet when computing checksum. */
1056 dptr = (sizeof(GPKTHDR) * 2) + pkt;
1057 pkt += 2;
1058 if (remote_debug > 1)
1059 {
1060 printf_filtered ("Computing checksum over pkt %s\n",pkt);
1061 printf_filtered ("Of length %d\n",strlen(pkt));
1062 }
1063 xcsum = compute_csum (pkt, strlen (pkt));
1064 if (xcsum != hdr->csum)
1065 {
1066 if (remote_debug)
1067 printf_filtered ("Checksum failure: computed %x, received %x\n",xcsum,
1068 hdr->csum);
1069 return (-1);
1070 }
1071
1072 /* Copy data portion to callers data buffer converting from ASCII
1073 to data as we go. */
1074 hex2mem (dptr, data, hdr->datalen);
1075 return 0;
1076 }
1077
1078 static int
1079 read_pkt (pkt)
1080 char *pkt;
1081 {
1082 int n, tries;
1083 struct sockaddr_in from;
1084 int from_len = sizeof(from);
1085 int gotstart;
1086 int total_len;
1087 char *p;
1088
1089 p = pkt;
1090 total_len = 0;
1091 gotstart = 0;
1092
1093 while (1)
1094 {
1095 /* Don't let us get wedged if the target is losing. */
1096 QUIT;
1097
1098 if (bufdata == 0)
1099 {
1100 bufindex = 0;
1101 n = NROM_BUF_SIZE;
1102 /* Perform read on socket. This will wait. */
1103 bufdata = recvfrom (nrom_targ_sock, readbuf, n, 0, &from, &from_len);
1104 if (bufdata < 0)
1105 {
1106 printf_filtered ("Error on socket read of %d\n",errno);
1107 return (-1);
1108 }
1109 if (remote_debug > 2)
1110 {
1111 readbuf[bufdata] = '\0';
1112 printf_filtered ("Received %d bytes. Data is %s\n",
1113 bufdata, readbuf);
1114 }
1115 }
1116
1117 /* skip stuff between packets */
1118 while (gotstart == 0 && bufdata != 0
1119 && readbuf[bufindex] != GDB_START_DELIMITER)
1120 {
1121 bufdata--;
1122 bufindex++;
1123 }
1124
1125 /* look for a start if we haven't seen one */
1126 if (gotstart == 0 && bufdata != 0
1127 && readbuf[bufindex] == GDB_START_DELIMITER)
1128 {
1129 gotstart = 1;
1130 bufindex++;
1131 bufdata--;
1132 }
1133
1134 /* copy packet data */
1135 if (gotstart != 0)
1136 {
1137 while (bufdata && readbuf[bufindex] != GDB_END_DELIMITER)
1138 {
1139 *p = readbuf[bufindex];
1140 p++;
1141 bufdata--;
1142 bufindex++;
1143 total_len++;
1144 if (total_len > NROM_BUF_SIZE)
1145 {
1146 error ("read_pkt: packet length exceeds %d\n",
1147 NROM_BUF_SIZE);
1148 return (-1);
1149 }
1150 }
1151 *p = '\0';
1152 if (remote_debug > 2)
1153 printf_filtered ("Packet is '%s'\n", pkt);
1154 }
1155
1156 /* Make sure this is the end of the packet. */
1157 if (gotstart != 0 && bufdata != 0
1158 && readbuf[bufindex] == GDB_END_DELIMITER)
1159 {
1160 gotstart = 0;
1161 bufindex++;
1162 bufdata--;
1163 /* Ensure that the packet is terminated. */
1164 *p = '\0';
1165 if (remote_debug > 2)
1166 printf_filtered ("Returning '%s'\n", pkt);
1167 return 0;
1168 }
1169 }
1170 }
1171
1172 static void
1173 send_query_cmd ()
1174 {
1175 while (send_pkt (GDB_QUERY_CMD, NULL, 0, 0, 0) < 0)
1176 ;
1177 }
1178
1179 static int
1180 send_pkt (cmd, data, datalen, addr, len)
1181 int cmd;
1182 char *data;
1183 int datalen;
1184 long addr;
1185 int len;
1186 {
1187 char c[2];
1188 unsigned char seq;
1189 struct sockaddr_in mysin;
1190 int send_cnt;
1191
1192 while (1)
1193 {
1194 /* Get a sequence number for this packet. */
1195 seq = get_seq_number ();
1196 /* Build the packet. */
1197 build_pkt (cmd, data, datalen, workbuf, addr, len, seq);
1198 /* Put delimiters around the pkt. */
1199 memset (sendbuf, 0, NROM_BUF_SIZE);
1200 sendbuf[0] = GDB_START_DELIMITER;
1201 strcat (sendbuf, workbuf);
1202 c[0] = GDB_END_DELIMITER;
1203 c[1] = '\0';
1204 strcat (sendbuf, c);
1205
1206 /* Send the packet out on our socket. */
1207 if (remote_debug > 1)
1208 printf_filtered ("Sending pkt: %s\n", sendbuf);
1209 mysin.sin_family = AF_INET;
1210 mysin.sin_port = target_port;
1211 mysin.sin_addr.S_un.S_addr = nrom_ipaddr;
1212
1213 send_cnt = 0;
1214 while (send_cnt < MAX_SEND_ATTEMPTS)
1215 {
1216 if (sendto (nrom_targ_sock, sendbuf, strlen(sendbuf), 0, &mysin,
1217 sizeof(struct sockaddr_in)) < 0)
1218 {
1219 printf_filtered ("sendto error of %d\n", errno);
1220 send_cnt++;
1221 }
1222 else
1223 break;
1224 }
1225 if (send_cnt >= MAX_SEND_ATTEMPTS)
1226 {
1227 printf_filtered ("Socket send failed after %d tries\n",
1228 MAX_SEND_ATTEMPTS);
1229 return (-1);
1230 }
1231 return 1;
1232 }
1233 }
1234
1235 /* Convert number NIB to a hex digit. */
1236
1237 static int
1238 tohex (nib)
1239 int nib;
1240 {
1241 if (nib < 10)
1242 return '0' + nib;
1243 else
1244 return 'a' + nib - 10;
1245 }
1246
1247 /* snatched from Stevens, pp279+ */
1248
1249 int
1250 writen (sock, ptr, nbytes)
1251 int sock;
1252 char *ptr;
1253 int nbytes;
1254 {
1255 int nleft, nwritten;
1256 char *buf = ptr;
1257
1258 nleft = nbytes;
1259 while (nleft > 0)
1260 {
1261 nwritten = write (sock, buf, nleft);
1262 if (nwritten <= 0)
1263 return nwritten;
1264 nleft -= nwritten;
1265 buf += nwritten;
1266 }
1267 return (nbytes - nleft);
1268 }
1269
1270 /* Define the target vector. */
1271
1272 struct target_ops nrom_ops = {
1273 "nrom", /* to_shortname */
1274 "Remote XDI `NetROM' target", /* to_longname */
1275 "Remote debug using a NetROM over Ethernet",
1276 nrom_open, /* to_open */
1277 nrom_close,
1278 nrom_attach,
1279 nrom_detach,
1280 nrom_resume,
1281 nrom_wait, /* to_wait */
1282 nrom_fetch_registers, /* to_fetch_registers */
1283 nrom_store_registers, /* to_store_registers */
1284 nrom_prepare_to_store, /* to_prepare_to_store */
1285 nrom_xfer_inferior_memory, /* to_xfer_memory */
1286 nrom_files_info, /* to_files_info */
1287 NULL, /* to_insert_breakpoint */
1288 NULL, /* to_remove_breakpoint */
1289 NULL,
1290 NULL,
1291 NULL,
1292 NULL,
1293 NULL,
1294 nrom_kill,
1295 nrom_load,
1296 NULL,
1297 nrom_create_inferior, /* to_create_inferior */
1298 nrom_mourn,
1299 nrom_can_run,
1300 0, /* to_notice_signals */
1301 0,
1302 download_stratum, /* to_stratum */
1303 NULL, /* to_next */
1304 1,
1305 1,
1306 1,
1307 1,
1308 0, /* to_has_execution */
1309 NULL, /* sections */
1310 NULL, /* sections_end */
1311 OPS_MAGIC /* to_magic */
1312 };
1313
1314 void
1315 _initialize_remote_nrom ()
1316 {
1317 add_target (&nrom_ops);
1318
1319 /* Add some commands for helpers. */
1320 add_cmd ("nrom_ipaddr", no_class, nrom_set_ipaddr,
1321 "Set the IP Address of the NetROM\n",
1322 &setlist);
1323 add_cmd ("target_port", no_class, nrom_set_target_port,
1324 "Set the Port to use for NetROM target communication\n",
1325 &setlist);
1326 add_cmd ("load_port", no_class, nrom_set_load_port,
1327 "Set the Port to use for NetROM downloads\n",
1328 &setlist);
1329 add_cmd ("control_port", no_class, nrom_set_control_port,
1330 "Set the Port to use for NetROM debugger services\n",
1331 &setlist);
1332 add_cmd ("nrom_filetype", no_class, nrom_set_filetype,
1333 "Set the filetype to use on NetROM downloads",
1334 &setlist);
1335
1336 add_cmd ("nrom", no_class, nrom_show_status,
1337 "Show the current NetROM status\n",
1338 &showlist);
1339
1340 add_cmd ("nrom", no_class, nrom_passthru,
1341 "Pass arguments as command to NetROM",
1342 &cmdlist);
1343 }