* gdb.base/list.exp: Add expect patterns for output from
[deliverable/binutils-gdb.git] / gdb / monitor.c
1 /* Remote debugging interface for boot monitors, for GDB.
2 Copyright 1990, 1991, 1992, 1993, 1995 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Rob Savoye for Cygnus.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 /* This file was derived from various remote-* modules. It is a collection
22 of generic support functions so GDB can talk directly to a ROM based
23 monitor. This saves use from having to hack an exception based handler
24 into existance, and makes for quick porting.
25
26 This module talks to a debug monitor called 'MONITOR', which
27 We communicate with MONITOR via either a direct serial line, or a TCP
28 (or possibly TELNET) stream to a terminal multiplexor,
29 which in turn talks to the target board. */
30
31 #include "defs.h"
32 #include "gdbcore.h"
33 #include "target.h"
34 #include "wait.h"
35 #include <varargs.h>
36 #include <signal.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include "command.h"
40 #include "serial.h"
41 #include "monitor.h"
42 #include "gdbcmd.h"
43 #include "inferior.h"
44
45 static void monitor_command PARAMS ((char *args, int fromtty));
46 static void monitor_load_srec PARAMS ((char *args, int protocol));
47 static int getacknak PARAMS ((int byte));
48
49 static void make_xmodem_packet PARAMS ((unsigned char *packet,
50 unsigned char *data,
51 int len));
52 static void print_xmodem_packet PARAMS ((char *packet));
53
54 static void monitor_load_ascii_srec PARAMS ((char *file, int fromtty));
55
56 static int monitor_make_srec PARAMS ((char *buffer, int type,
57 CORE_ADDR memaddr,
58 unsigned char *myaddr, int len));
59
60 static void monitor_fetch_register PARAMS ((int regno));
61 static void monitor_store_register PARAMS ((int regno));
62
63 static int from_hex PARAMS ((int a));
64 static unsigned long get_hex_word PARAMS ((void));
65
66 static struct monitor_ops *current_monitor;
67
68 static char *loadtype_str = "srec";
69 static char *loadproto_str = "none";
70
71 static int hashmark; /* flag set by "set hash" */
72
73 static int timeout = 30;
74
75 static int expect PARAMS ((char *string, char *buf, int buflen));
76 static int expect_prompt PARAMS ((char *buf, int buflen));
77
78 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
79 and i386-stub.c. Normally, no one would notice because it only matters
80 for writing large chunks of memory (e.g. in downloads). Also, this needs
81 to be more than 400 if required to hold the registers (see below, where
82 we round it up based on REGISTER_BYTES). */
83
84 #define PBUFSIZ 400
85
86 /* Descriptor for I/O to remote machine. Initialize it to NULL so
87 that monitor_open knows that we don't have a file open when the
88 program starts. */
89
90 static serial_t monitor_desc = NULL;
91
92 /* These definitions are for xmodem protocol. */
93
94 #define SOH 0x01
95 #define ACK 0x06
96 #define NAK 0x15
97 #define EOT 0x04
98 #define CANCEL 0x18
99 #define GETACK getacknak(ACK)
100 #define GETNAK getacknak(NAK)
101 #define XMODEM_DATASIZE 128 /* the data size is ALWAYS 128 */
102 #define XMODEM_PACKETSIZE 131 /* the packet size is ALWAYS 132 (zero based) */
103 #define XMODEM 1
104
105 static unsigned char output_buf[0x200];
106 static int obp;
107
108 static void
109 debug_save_output (buf, len)
110 unsigned char *buf;
111 int len;
112 {
113 #if 0
114 for (; len > 0; len--)
115 output_buf[obp++ & 0x1ff] = *buf++;
116 #else
117 fputs_unfiltered (buf, gdb_stdout);
118 #endif
119 }
120
121 static unsigned char input_buf[0x200];
122 static int ibp;
123
124 static void
125 debug_save_input_char (c)
126 int c;
127 {
128 #if 0
129 input_buf[ibp++ & 0x1ff] = c;
130 #else
131 fputc_unfiltered (c, gdb_stdout);
132 #endif
133 }
134
135 /* printf_monitor -- send data to monitor. Works just like printf. */
136
137 static void
138 printf_monitor (va_alist)
139 va_dcl
140 {
141 va_list args;
142 char *pattern;
143 char buf[PBUFSIZ];
144
145 va_start (args);
146
147 pattern = va_arg (args, char *);
148
149 vsprintf (buf, pattern, args);
150
151 if (remote_debug > 0)
152 debug_save_output (buf, strlen (buf));
153
154 if (strlen (buf) > PBUFSIZ)
155 error ("printf_monitor(): string too long");
156 if (SERIAL_WRITE(monitor_desc, buf, strlen (buf)))
157 fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
158 }
159
160 /* Send raw data to monitor. */
161
162 static void
163 write_monitor (data, len)
164 char *data;
165 int len;
166 {
167 if (SERIAL_WRITE (monitor_desc, data, len))
168 fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno));
169
170 *(data + len + 1) = '\0';
171 }
172
173 /* Read a character from the remote system, doing all the fancy
174 timeout stuff. */
175
176 static int
177 readchar (timeout)
178 int timeout;
179 {
180 int c;
181
182 c = SERIAL_READCHAR (monitor_desc, timeout);
183
184 if (remote_debug > 0)
185 debug_save_input_char (c & 0x7f);
186
187 if (c >= 0)
188 return c & 0x7f;
189
190 if (c == SERIAL_TIMEOUT)
191 {
192 if (timeout == 0)
193 return c; /* Polls shouldn't generate timeout errors */
194 error ("Timeout reading from remote system.");
195 }
196 perror_with_name ("remote-monitor");
197 }
198
199 /* Scan input from the remote system, until STRING is found. If BUF is non-
200 zero, then collect input until either STRING has been collected or BUFLEN
201 chars have been collected. If input overflows BUF because STRING can't be
202 found, return -1, else return number of chars in BUF (including STRING). */
203
204 static int
205 expect (string, buf, buflen)
206 char *string;
207 char *buf;
208 int buflen;
209 {
210 char *p = string;
211 int obuflen = buflen;
212 int c;
213
214 immediate_quit = 1;
215 while (1)
216 {
217 if (buf)
218 {
219 if (buflen <= 0)
220 {
221 immediate_quit = 0;
222 return -1;
223 }
224
225 c = readchar (timeout);
226 *buf++ = c;
227 buflen--;
228 }
229 else
230 c = readchar (timeout);
231
232 if (c == *p++)
233 {
234 if (*p == '\0')
235 {
236 immediate_quit = 0;
237
238 return obuflen - buflen;
239 }
240 }
241 else
242 {
243 p = string;
244 if (c == *p)
245 p++;
246 }
247 }
248 }
249
250 /* Keep discarding input until we see the MONITOR prompt.
251
252 The convention for dealing with the prompt is that you
253 o give your command
254 o *then* wait for the prompt.
255
256 Thus the last thing that a procedure does with the serial line
257 will be an expect_prompt(). Exception: monitor_resume does not
258 wait for the prompt, because the terminal is being handed over
259 to the inferior. However, the next thing which happens after that
260 is a monitor_wait which does wait for the prompt.
261 Note that this includes abnormal exit, e.g. error(). This is
262 necessary to prevent getting into states from which we can't
263 recover. */
264
265 static int
266 expect_prompt (buf, buflen)
267 char *buf;
268 int buflen;
269 {
270 return expect (PROMPT, buf, buflen);
271 }
272
273 /* Get N 32-bit words from remote, each preceded by a space, and put
274 them in registers starting at REGNO. */
275
276 static unsigned long
277 get_hex_word ()
278 {
279 unsigned long val;
280 int i;
281 int ch;
282
283 do
284 ch = readchar (timeout);
285 while (isspace(ch));
286
287 val = from_hex (ch);
288
289 for (i = 7; i >= 1; i--)
290 {
291 ch = readchar (timeout);
292 if (!isxdigit (ch))
293 break;
294 val = (val << 4) | from_hex (ch);
295 }
296
297 return val;
298 }
299
300 /* Open a connection to a remote debugger. NAME is the filename used
301 for communication. */
302
303 static char *dev_name;
304 static struct target_ops *targ_ops;
305
306 void
307 monitor_open (args, mon_ops, from_tty)
308 char *args;
309 struct monitor_ops *mon_ops;
310 int from_tty;
311 {
312 char *name;
313 int i;
314
315 targ_ops = mon_ops->target;
316 name = targ_ops->to_shortname;
317
318 if (!args)
319 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
320 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name);
321
322 target_preopen (from_tty);
323
324 unpush_target (targ_ops);
325
326 if (dev_name)
327 free (dev_name);
328 dev_name = strsave (args);
329
330 monitor_desc = SERIAL_OPEN (dev_name);
331
332 if (!monitor_desc)
333 perror_with_name (dev_name);
334
335 if (baud_rate != -1)
336 {
337 if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate))
338 {
339 SERIAL_CLOSE (monitor_desc);
340 perror_with_name (dev_name);
341 }
342 }
343
344 SERIAL_RAW (monitor_desc);
345
346 SERIAL_FLUSH_INPUT (monitor_desc);
347
348 /* some systems only work with 2 stop bits */
349
350 SERIAL_SETSTOPBITS (monitor_desc, mon_ops->stopbits);
351
352 current_monitor = mon_ops;
353
354 /* see if the target is alive. For a ROM monitor, we can just try to
355 force the prompt to print a few times. */
356
357 /* wake up the monitor and see if it's alive */
358 printf_monitor (mon_ops->init);
359 expect_prompt (NULL, 0); /* See if we get a prompt */
360
361 /* try again to be sure */
362 printf_monitor (mon_ops->init);
363 expect_prompt (NULL, 0); /* See if we get a prompt */
364
365 /* Setup the suffixes for the `set remoteloadtype' command */
366
367 add_show_from_set (add_set_enum_cmd ("remoteloadtype", no_class,
368 mon_ops->loadtypes,
369 (char *)&loadtype_str,
370 "Set the remote load type.",
371 &setlist),
372 &showlist);
373
374 /* Setup the suffixes for the `set remoteloadprotocol' command */
375
376 add_show_from_set (add_set_enum_cmd ("remoteloadprotocol", no_class,
377 mon_ops->loadprotos,
378 (char *)&loadproto_str,
379 "Set the remote load protocol.",
380 &setlist),
381 &showlist);
382
383 if (from_tty)
384 printf_unfiltered ("Remote target %s connected to %s\n", name, dev_name);
385
386 push_target (targ_ops);
387
388 inferior_pid = 42000; /* Make run command think we are busy... */
389
390 printf_monitor ("\r");
391
392 start_remote ();
393 }
394
395 /* Close out all files and local state before this target loses
396 control. */
397
398 void
399 monitor_close (quitting)
400 int quitting;
401 {
402 if (monitor_desc)
403 SERIAL_CLOSE (monitor_desc);
404 monitor_desc = NULL;
405 }
406
407 /* Terminate the open connection to the remote debugger. Use this
408 when you want to detach and do something else with your gdb. */
409
410 void
411 monitor_detach (args, from_tty)
412 char *args;
413 int from_tty;
414 {
415 pop_target (); /* calls monitor_close to do the real work */
416 if (from_tty)
417 printf_unfiltered ("Ending remote %s debugging\n", target_shortname);
418 }
419
420 /* Tell the remote machine to resume. */
421
422 void
423 monitor_resume (pid, step, sig)
424 int pid, step;
425 enum target_signal sig;
426 {
427 if (step)
428 printf_monitor (STEP_CMD);
429 else
430 printf_monitor (CONT_CMD);
431 }
432
433 /* Wait until the remote machine stops, then return, storing status in
434 status just as `wait' would. */
435
436 int
437 monitor_wait (pid, status)
438 int pid;
439 struct target_waitstatus *status;
440 {
441 int old_timeout = timeout;
442
443 status->kind = TARGET_WAITKIND_EXITED;
444 status->value.integer = 0;
445
446 timeout = -1; /* Don't time out -- user program is running. */
447
448 expect_prompt (NULL, 0); /* Wait for prompt, outputting extraneous text */
449
450 status->kind = TARGET_WAITKIND_STOPPED;
451 status->value.sig = TARGET_SIGNAL_TRAP;
452
453 timeout = old_timeout;
454
455 return inferior_pid;
456 }
457
458 /* Fetch register REGNO, or all registers if REGNO is -1. Returns
459 errno value. */
460
461 static void
462 monitor_fetch_register (regno)
463 int regno;
464 {
465 unsigned LONGEST val;
466 unsigned char regbuf[MAX_REGISTER_RAW_SIZE];
467 char buf[200];
468 char *p, *p1;
469 char *name;
470 int resp_len;
471
472 name = REGNAMES (regno);
473
474 if (!name)
475 return;
476
477 /* send the register examine command */
478
479 printf_monitor (current_monitor->getreg.cmd, name);
480
481 /* If TERM is present, we wait for that to show up. Also, (if TERM is
482 present), we will send TERM_CMD if that is present. In any case, we collect
483 all of the output into buf, and then wait for the normal prompt. */
484
485 if (current_monitor->getreg.term)
486 {
487 resp_len = expect (current_monitor->getreg.term, buf, sizeof buf); /* get response */
488
489 if (resp_len <= 0)
490 error ("monitor_fetch_register (%d): excessive response from monitor: %.*s.",
491 regno, resp_len, buf);
492
493 if (current_monitor->getreg.term_cmd)
494 {
495 SERIAL_WRITE (monitor_desc, current_monitor->getreg.term_cmd,
496 strlen (current_monitor->getreg.term_cmd));
497 expect_prompt (NULL, 0);
498 }
499 }
500 else
501 resp_len = expect_prompt (buf, sizeof buf); /* get response */
502
503
504 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
505 the register value. Otherwise, we just start searching from the start of
506 the buf. */
507
508 if (current_monitor->getreg.resp_delim)
509 {
510 p = strstr (buf, current_monitor->getreg.resp_delim);
511 if (!p)
512 error ("monitor_fetch_register (%d): bad response from monitor: %.*s.",
513 regno, resp_len, buf);
514 p += strlen (current_monitor->getreg.resp_delim);
515 }
516 else
517 p = buf;
518
519 val = strtoul (p, &p1, 16);
520
521 if (val == 0 && p == p1)
522 error ("monitor_fetch_register (%d): bad value from monitor: %.*s.",
523 regno, resp_len, buf);
524
525 /* supply register stores in target byte order, so swap here */
526
527 store_unsigned_integer (regbuf, REGISTER_RAW_SIZE (regno), val);
528
529 supply_register (regno, regbuf);
530 }
531
532 /* Read the remote registers into the block regs. */
533
534 void
535 monitor_fetch_registers (regno)
536 int regno;
537 {
538 if (regno >= 0)
539 {
540 monitor_fetch_register (regno);
541 return;
542 }
543
544 for (regno = 0; regno < NUM_REGS; regno++)
545 monitor_fetch_register (regno);
546 }
547
548 /* Store register REGNO, or all if REGNO == 0. Return errno value. */
549
550 static void
551 monitor_store_register (regno)
552 int regno;
553 {
554 char *name;
555 unsigned LONGEST val;
556
557 name = REGNAMES (regno);
558 if (!name)
559 return;
560
561 val = read_register (regno);
562
563 /* send the register deposit command */
564
565 printf_monitor (current_monitor->setreg.cmd, name, val);
566
567 /* It's possible that there are actually some monitors out there that will
568 prompt you when you set a register. In that case, you may need to add some
569 code here to deal with TERM and TERM_CMD (see monitor_fetch_register to get
570 an idea of what's needed...) */
571
572 expect_prompt (NULL, 0);
573 }
574
575 /* Store the remote registers. */
576
577 void
578 monitor_store_registers (regno)
579 int regno;
580 {
581 if (regno >= 0)
582 {
583 monitor_store_register (regno);
584 return;
585 }
586
587 for (regno = 0; regno < NUM_REGS; regno++)
588 monitor_store_register (regno);
589 }
590
591 /* Get ready to modify the registers array. On machines which store
592 individual registers, this doesn't need to do anything. On machines
593 which store all the registers in one fell swoop, this makes sure
594 that registers contains all the registers from the program being
595 debugged. */
596
597 void
598 monitor_prepare_to_store ()
599 {
600 /* Do nothing, since we can store individual regs */
601 }
602
603 void
604 monitor_files_info ()
605 {
606 printf_unfiltered ("\tAttached to %s at %d baud.\n", dev_name, baud_rate);
607 }
608
609 static int
610 monitor_write_memory (memaddr, myaddr, len)
611 CORE_ADDR memaddr;
612 unsigned char *myaddr;
613 int len;
614 {
615 /* send the memory deposit command */
616
617 printf_monitor (current_monitor->setmem.cmd, memaddr, *myaddr);
618
619 /* It's possible that there are actually some monitors out there that will
620 prompt you when you deposit to memory. In that case, you may need to add
621 some code here to deal with TERM and TERM_CMD (see monitor_read_memory to
622 get an idea of what's needed...) */
623
624 expect_prompt (NULL, 0);
625
626 return 1;
627 }
628
629 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's memory
630 at MEMADDR. Returns length moved. Currently, we only do one byte at a
631 time. */
632
633 static int
634 monitor_read_memory (memaddr, myaddr, len)
635 CORE_ADDR memaddr;
636 char *myaddr;
637 int len;
638 {
639 unsigned LONGEST val;
640 unsigned char regbuf[MAX_REGISTER_RAW_SIZE];
641 char buf[200];
642 char *p, *p1;
643 char *name;
644 int resp_len;
645
646 /* send the memory examine command */
647
648 printf_monitor (current_monitor->getmem.cmd, memaddr);
649
650 /* If TERM is present, we wait for that to show up. Also, (if TERM is
651 present), we will send TERM_CMD if that is present. In any case, we collect
652 all of the output into buf, and then wait for the normal prompt. */
653
654 if (current_monitor->getmem.term)
655 {
656 resp_len = expect (current_monitor->getmem.term, buf, sizeof buf); /* get response */
657
658 if (resp_len <= 0)
659 error ("monitor_read_memory (0x%x): excessive response from monitor: %.*s.",
660 memaddr, resp_len, buf);
661
662 if (current_monitor->getmem.term_cmd)
663 {
664 SERIAL_WRITE (monitor_desc, current_monitor->getmem.term_cmd,
665 strlen (current_monitor->getmem.term_cmd));
666 expect_prompt (NULL, 0);
667 }
668 }
669 else
670 resp_len = expect_prompt (buf, sizeof buf); /* get response */
671
672 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
673 the register value. Otherwise, we just start searching from the start of
674 the buf. */
675
676 if (current_monitor->getmem.resp_delim)
677 {
678 p = strstr (buf, current_monitor->getmem.resp_delim);
679 if (!p)
680 error ("monitor_read_memory (0x%x): bad response from monitor: %.*s.",
681 memaddr, resp_len, buf);
682 p += strlen (current_monitor->getmem.resp_delim);
683 }
684 else
685 p = buf;
686
687 val = strtoul (p, &p1, 16);
688
689 if (val == 0 && p == p1)
690 error ("monitor_read_memory (0x%x): bad value from monitor: %.*s.", memaddr,
691 resp_len, buf);
692
693 *myaddr = val;
694
695 return 1; /* Got 1 byte */
696 }
697
698 /* FIXME-someday! merge these two. */
699
700 int
701 monitor_xfer_memory (memaddr, myaddr, len, write, target)
702 CORE_ADDR memaddr;
703 char *myaddr;
704 int len;
705 int write;
706 struct target_ops *target; /* ignored */
707 {
708 if (write)
709 return monitor_write_memory (memaddr, myaddr, len);
710 else
711 return monitor_read_memory (memaddr, myaddr, len);
712 }
713
714 void
715 monitor_kill (args, from_tty)
716 char *args;
717 int from_tty;
718 {
719 return; /* ignore attempts to kill target system */
720 }
721
722 /* Clean up when a program exits.
723 The program actually lives on in the remote processor's RAM, and may be
724 run again without a download. Don't leave it full of breakpoint
725 instructions. */
726
727 void
728 monitor_mourn_inferior ()
729 {
730 unpush_target (targ_ops);
731 generic_mourn_inferior (); /* Do all the proper things now */
732 }
733
734 #define NUM_MONITOR_BREAKPOINTS 8
735
736 static CORE_ADDR breakaddr[NUM_MONITOR_BREAKPOINTS] = {0};
737
738 /* Tell the monitor to add a breakpoint. */
739
740 int
741 monitor_insert_breakpoint (addr, shadow)
742 CORE_ADDR addr;
743 char *shadow;
744 {
745 int i;
746 static unsigned char break_insn[] = BREAKPOINT;
747
748 for (i = 0; i < NUM_MONITOR_BREAKPOINTS; i++)
749 {
750 if (breakaddr[i] == 0)
751 {
752 breakaddr[i] = addr;
753 monitor_read_memory (addr, shadow, sizeof (break_insn));
754 printf_monitor (SET_BREAK_CMD, addr);
755 expect_prompt (NULL, 0);
756 return 0;
757 }
758 }
759
760 error ("Too many breakpoints (> %d) for monitor.", NUM_MONITOR_BREAKPOINTS);
761 }
762
763 /* Tell the monitor to remove a breakpoint. */
764
765 int
766 monitor_remove_breakpoint (addr, shadow)
767 CORE_ADDR addr;
768 char *shadow;
769 {
770 int i;
771
772 for (i = 0; i < NUM_MONITOR_BREAKPOINTS; i++)
773 {
774 if (breakaddr[i] == addr)
775 {
776 breakaddr[i] = 0;
777 /* some monitors remove breakpoints based on the address */
778 if (CLR_BREAK_ADDR)
779 printf_monitor(CLR_BREAK_CMD, addr);
780 else
781 printf_monitor(CLR_BREAK_CMD, i);
782 expect_prompt (NULL, 0);
783 return 0;
784 }
785 }
786 fprintf_unfiltered (stderr, "Can't find breakpoint associated with 0x%x\n", addr);
787 return 1;
788 }
789
790 /* monitor_load -- load a file. This file determines which of the
791 * supported formats to use. The current types are:
792 * FIXME: not all types supported yet.
793 * default - reads any file using bfd and writes it to memory. This
794 * is really slow.
795 * srec - reads binary file using bfd and writes it as an
796 * ascii srecord.
797 * xmodem-bin - reads a binary file using bfd, and downloads it
798 * using xmodem protocol.
799 * xmodem-srec - reads a binary file using bfd, and after converting
800 * it downloads it as an srecord using xmodem protocol.
801 * ascii-srec - reads a ascii srecord file and downloads it
802 * without a change.
803 * ascii-xmodem - reads a ascii file and downloads using xmodem
804 * protocol.
805 */
806
807 void
808 monitor_load (file, fromtty)
809 char *file;
810 int fromtty;
811 {
812 /* default, load a binary */
813 if (STREQ (loadtype_str, "default"))
814 {
815 error ("default load type not supported.");
816 }
817
818 /* load an srecord by converting */
819 if ((STREQ (loadtype_str, "srec")) && STREQ (loadproto_str, "xmodem"))
820 {
821 monitor_load_srec (file, XMODEM);
822 return;
823 }
824
825 /* load an srecord by converting */
826 if (STREQ (loadtype_str, "srec"))
827 {
828 monitor_load_srec (file, 0); /* if from a binary */
829 return;
830 }
831
832 /* load an srecord by converting */
833 if (STREQ (loadtype_str, "none"))
834 {
835 error ("Unimplemented");
836 return;
837 }
838
839 /* load an srecord file */
840 if (STREQ (loadproto_str, "none"))
841 {
842 monitor_load_ascii_srec (file, fromtty); /* if from a binary */
843 return;
844 }
845
846 if (STREQ (loadproto_str, "xmodem"))
847 {
848 monitor_load_srec (file, XMODEM);
849 return;
850 }
851 }
852
853 /* Download an ASCII srecord file. */
854
855 #define DOWNLOAD_LINE_SIZE 100
856
857 static void
858 monitor_load_ascii_srec (file, fromtty)
859 char *file;
860 int fromtty;
861 {
862 FILE *download;
863 char buf[DOWNLOAD_LINE_SIZE];
864 int i, bytes_read;
865
866 download = fopen (file, "r");
867 if (download == NULL)
868 {
869 error ("%s does not exist", file);
870 return;
871 }
872
873 printf_monitor (LOAD_CMD);
874 sleep (1);
875 while (!feof (download))
876 {
877 bytes_read = fread (buf, sizeof (char), DOWNLOAD_LINE_SIZE, download);
878 if (hashmark)
879 {
880 putchar_unfiltered ('.');
881 gdb_flush (gdb_stdout);
882 }
883 if (SERIAL_WRITE (monitor_desc, buf, bytes_read))
884 {
885 fprintf_unfiltered (stderr, "SERIAL_WRITE failed: (while downloading) %s\n",
886 safe_strerror (errno));
887 break;
888 }
889 i = 0;
890 while (i++ <=200) {} ; /* Ugly HACK, probably needs flow control */
891 if (bytes_read < DOWNLOAD_LINE_SIZE)
892 {
893 if (!feof (download))
894 error ("Only read %d bytes\n", bytes_read);
895 break;
896 }
897 }
898
899 if (hashmark)
900 putchar_unfiltered ('\n');
901
902 if (!feof (download))
903 error ("Never got EOF while downloading");
904 expect_prompt (NULL, 0);
905 fclose (download);
906 }
907
908 /* Put a command string, in args, out to MONITOR. Output from MONITOR
909 is placed on the users terminal until the prompt is seen. FIXME: We
910 read the characters ourseleves here cause of a nasty echo. */
911
912 static void
913 monitor_command (args, fromtty)
914 char *args;
915 int fromtty;
916 {
917 char *p;
918
919 p = PROMPT;
920
921 if (monitor_desc == NULL)
922 error ("monitor target not open.");
923
924 /* Send the command. Note that if no args were supplied, then we're
925 just sending the monitor a newline, which is sometimes useful. */
926
927 printf_monitor ("%s\n", (args ? args : ""));
928
929 expect_prompt (NULL, 0);
930 }
931
932 /* Download a binary file by converting it to srecords. This
933 will also use xmodem to download the resulting file.
934
935 A download goes like this when using xmodem:
936 Receiver: Sender
937 NAK ---------->
938 <-------- (packet) [SOH|1|1|data|SUM]
939 ACK ---------->
940 <-------- (packet) [SOH|2|2|data|SUM]
941 ACK ---------->
942 <-------- EOT
943 ACK ---------->
944
945 ACK = 0x06
946 NAK = 0x15
947 EOT = 0x04
948 */
949
950 static void
951 monitor_load_srec (args, protocol)
952 char *args;
953 int protocol;
954 {
955 bfd *abfd;
956 asection *s;
957 char *buffer, srec[1024];
958 char packet[XMODEM_PACKETSIZE];
959 int i;
960 int retries;
961 int type = 0; /* default to a type 0, header record */
962 int srec_frame = 57; /* FIXME: this must be 57 There is 12 bytes
963 of header, and 2 bytes of checksum at the end.
964 The problem is an xmodem packet holds exactly
965 128 bytes. */
966
967 abfd = bfd_openr (args, 0);
968 if (!abfd)
969 {
970 printf_filtered ("Unable to open file %s\n", args);
971 return;
972 }
973
974 if (bfd_check_format (abfd, bfd_object) == 0)
975 {
976 printf_filtered ("File is not an object file\n");
977 return;
978 }
979
980 printf_monitor (LOAD_CMD); /* tell the monitor to load */
981 sleep (3);
982 /* get the NAK from the target */
983 if (protocol == XMODEM)
984 {
985 if (!GETNAK)
986 {
987 printf_monitor ("%c", EOT);
988 error ("Never got the NAK to start loading");
989 }
990 }
991
992 s = abfd->sections;
993 while (s != (asection *) NULL)
994 {
995 if (s->flags & SEC_LOAD)
996 {
997 buffer = xmalloc (srec_frame);
998
999 printf_filtered ("%s\t: 0x%4x .. 0x%4x ",
1000 s->name, s->vma, s->vma + s->_raw_size);
1001 gdb_flush (gdb_stdout);
1002 for (i = 0; i < s->_raw_size; i += srec_frame)
1003 {
1004 if (srec_frame > s->_raw_size - i)
1005 srec_frame = s->_raw_size - i;
1006
1007 bfd_get_section_contents (abfd, s, buffer, i, srec_frame);
1008 monitor_make_srec (srec, type, s->vma + i, buffer, srec_frame);
1009 /* send a packet using xmodem */
1010 if (protocol == XMODEM)
1011 {
1012 make_xmodem_packet (packet, srec, XMODEM_DATASIZE);
1013 write_monitor (packet, XMODEM_PACKETSIZE+1);
1014 retries = 0;
1015 while (retries++ <= 3)
1016 {
1017 /* Resend packet */
1018 if (GETNAK)
1019 {
1020 sleep (1);
1021 /* send it again */
1022 write_monitor (packet, XMODEM_PACKETSIZE+1);
1023 if (GETACK) /* ACKnowledged, get next data chunk */
1024 break;
1025 }
1026 else
1027 { /* assume we got an ACK */
1028 if (hashmark)
1029 {
1030 putchar_unfiltered ('#');
1031 gdb_flush (gdb_stdout);
1032 }
1033 break;
1034 }
1035 }
1036 if (retries >= 4)
1037 { /* too many tries, must be hosed */
1038 printf_monitor ("%c", EOT);
1039 error ("Never got a ACK after sending an xmodem packet");
1040 }
1041 }
1042 else
1043 { /* no protocols at all */
1044 printf_monitor ("%s\n", srec);
1045 }
1046 if (hashmark)
1047 {
1048 putchar_unfiltered ('#');
1049 gdb_flush (gdb_stdout);
1050 }
1051 type = 3; /* switch to a 4 byte address record */
1052 gdb_flush (gdb_stdout);
1053 }
1054 free (buffer);
1055 }
1056 s = s->next;
1057 }
1058 putchar_unfiltered ('\n');
1059
1060 /* Write a type 7 terminator record. no data for a type 7, and there
1061 is no data, so len is 0. */
1062
1063 if (protocol == XMODEM)
1064 {
1065 /* send a packet using xmodem */
1066 monitor_make_srec (srec, 7, abfd->start_address, "", 0);
1067 make_xmodem_packet (packet, srec, XMODEM_DATASIZE);
1068 write_monitor (packet, XMODEM_PACKETSIZE+1);
1069 }
1070 else
1071 {
1072 monitor_make_srec (srec, 7, abfd->start_address, "", 0);
1073 printf_monitor ("%s\n", srec);
1074 }
1075 if (protocol == XMODEM)
1076 {
1077 printf_monitor ("%c", EOT);
1078 if (!GETACK)
1079 error ("Never got ACK after sending EOT");
1080 }
1081
1082 if (hashmark)
1083 putchar_unfiltered ('\n');
1084
1085 expect_prompt (NULL, 0);
1086
1087 /* Finally, make the PC point at the start address */
1088
1089 write_register (PC_REGNUM, bfd_get_start_address (abfd));
1090 }
1091
1092 /* Get an ACK or a NAK from the target. returns 1 (true) or 0 (false)
1093 This is for xmodem. ANy string starting with "***" is an error
1094 message from the target. Here's a few from the WinBond w89k
1095 "Cougar" PA board:
1096 *** Too many errors found.
1097 *** Bad command
1098 *** Command syntax error
1099 */
1100
1101 static int
1102 getacknak (byte)
1103 int byte;
1104 {
1105 char character;
1106 int i;
1107
1108 i = 0;
1109 while (i++ < 60)
1110 {
1111 character = (char) readchar (0);
1112 if ((character == 0xfffffffe) || (character == 0x7f))
1113 { /* empty uart */
1114 sleep (1);
1115 continue;
1116 }
1117 if (character == CANCEL)
1118 { /* target aborted load */
1119 expect_prompt (NULL, 0);
1120 error ("Got a CANCEL from the target.");
1121 }
1122 if (character == '*')
1123 { /* look for missed error message */
1124 expect_prompt (NULL, 0);
1125 error ("Got an error message from the target");
1126 }
1127 if (character == byte) /* got what we wanted */
1128 return 1;
1129 if (character == ((byte == ACK) ? NAK : ACK))
1130 return 0;
1131 sleep (1);
1132 }
1133
1134 return 0;
1135 }
1136
1137 /*
1138 * monitor_make_srec -- make an srecord. This writes each line, one at a
1139 * time, each with it's own header and trailer line.
1140 * An srecord looks like this:
1141 *
1142 * byte count-+ address
1143 * start ---+ | | data +- checksum
1144 * | | | |
1145 * S01000006F6B692D746573742E73726563E4
1146 * S315000448600000000000000000FC00005900000000E9
1147 * S31A0004000023C1400037DE00F023604000377B009020825000348D
1148 * S30B0004485A0000000000004E
1149 * S70500040000F6
1150 *
1151 * S<type><length><address><data><checksum>
1152 *
1153 * Where
1154 * - length
1155 * is the number of bytes following upto the checksum. Note that
1156 * this is not the number of chars following, since it takes two
1157 * chars to represent a byte.
1158 * - type
1159 * is one of:
1160 * 0) header record
1161 * 1) two byte address data record
1162 * 2) three byte address data record
1163 * 3) four byte address data record
1164 * 7) four byte address termination record
1165 * 8) three byte address termination record
1166 * 9) two byte address termination record
1167 *
1168 * - address
1169 * is the start address of the data following, or in the case of
1170 * a termination record, the start address of the image
1171 * - data
1172 * is the data.
1173 * - checksum
1174 * is the sum of all the raw byte data in the record, from the length
1175 * upwards, modulo 256 and subtracted from 255.
1176 */
1177
1178 static int
1179 monitor_make_srec (buffer, type, memaddr, myaddr, len)
1180 char *buffer;
1181 int type;
1182 CORE_ADDR memaddr;
1183 unsigned char *myaddr;
1184 int len;
1185 {
1186 int checksum;
1187 int i;
1188 char *buf;
1189
1190 buf = buffer;
1191
1192 checksum = 0;
1193
1194 /* Create the header for the srec. 4 is the number of bytes in the address,
1195 and 1 is the number of bytes in the count. */
1196
1197 if (type == 0) /* FIXME: type 0 is optional */
1198 type = 3; /* so use data as it works */
1199 sprintf (buf, "S%d%02X%08X", type, len + 4 + 1, memaddr);
1200 buf += 12;
1201
1202 checksum += (len + 4 + 1 /* calculate the checksum */
1203 + (memaddr & 0xff)
1204 + ((memaddr >> 8) & 0xff)
1205 + ((memaddr >> 16) & 0xff)
1206 + ((memaddr >> 24) & 0xff));
1207
1208 /* build the srecord */
1209 for (i = 0; i < len; i++)
1210 {
1211 sprintf (buf, "%02X", myaddr[i]);
1212 checksum += myaddr[i];
1213 buf += 2;
1214 }
1215
1216 sprintf(buf, "%02X", ~checksum & 0xff); /* add the checksum */
1217
1218 return 0;
1219 }
1220
1221 /* Take 128 bytes of data and make a packet out of it.
1222 *
1223 * Each packet looks like this:
1224 * +-----+-------+-------+------+-----+
1225 * | SOH | Seq1. | Seq2. | data | SUM |
1226 * +-----+-------+-------+------+-----+
1227 * SOH = 0x01
1228 * Seq1 = The sequence number.
1229 * Seq2 = The complement of the sequence number.
1230 * Data = A 128 bytes of data.
1231 * SUM = Add the contents of the 128 bytes and use the low-order
1232 * 8 bits of the result.
1233 */
1234
1235 static void
1236 make_xmodem_packet (packet, data, len)
1237 unsigned char *packet;
1238 unsigned char *data;
1239 int len;
1240 {
1241 static int sequence = 1;
1242 int i, sum;
1243 unsigned char *buf;
1244
1245 buf = data;
1246 /* build the packet header */
1247 packet[0] = SOH;
1248 packet[1] = sequence;
1249 packet[2] = 255 - sequence;
1250 sequence++;
1251 #if 0
1252 packet[2] = ~sequence++; /* the complement is the sequence checksum */
1253 #endif
1254
1255 sum = 0; /* calculate the data checksum */
1256 for (i = 3; i <= len + 2; i++) {
1257 packet[i] = *buf;
1258 sum += *buf;
1259 buf++;
1260 }
1261
1262 /* add padding for the rest of the packet */
1263 for (i = len+1 ; i <= XMODEM_DATASIZE ; i++)
1264 packet[i] = '0';
1265
1266 packet[XMODEM_PACKETSIZE] = sum & 0xff; /* add the checksum */
1267 }
1268
1269 /* Print the packet as a debug check. */
1270
1271 static void
1272 print_xmodem_packet (packet)
1273 char *packet;
1274 {
1275 int i;
1276 static int lastseq;
1277 int sum;
1278
1279 /* take apart the packet header the packet header */
1280 if (packet[0] == SOH)
1281 printf_unfiltered ("SOH");
1282 else
1283 error ("xmodem: SOH is wrong");
1284
1285 /* check the sequence */
1286 if (packet[1] != 0)
1287 {
1288 lastseq = packet[1];
1289 if (packet[2] != ~lastseq)
1290 error ("xmodem: Sequence checksum is wrong");
1291 else
1292 printf_filtered (" %d %d", lastseq, ~lastseq);
1293 }
1294
1295 /* check the data checksum */
1296 sum = 0;
1297 for (i = 3; i <= XMODEM_DATASIZE; i++)
1298 sum += packet[i];
1299
1300 /* ignore the data */
1301 #if 0
1302 printf_unfiltered (" [128 bytes of data] %d\n", sum & 0xff);
1303 #endif
1304 printf_filtered (" [%s] %d\n", packet, sum & 0xff);
1305
1306 if ((packet[XMODEM_PACKETSIZE] & 0xff) != (sum & 0xff))
1307 printf_unfiltered ("xmodem: data checksum wrong, got a %d",
1308 packet[XMODEM_PACKETSIZE] & 0xff);
1309
1310 putchar_unfiltered ('\n');
1311 }
1312
1313 /* Convert hex digit A to a number. */
1314
1315 static int
1316 from_hex (a)
1317 int a;
1318 {
1319 if (a >= '0' && a <= '9')
1320 return a - '0';
1321 if (a >= 'a' && a <= 'f')
1322 return a - 'a' + 10;
1323 if (a >= 'A' && a <= 'F')
1324 return a - 'A' + 10;
1325
1326 error ("Reply contains invalid hex digit 0x%x", a);
1327 }
1328
1329 /* Define additional commands that are usually only used by monitors. */
1330
1331 void
1332 _initialize_remote_monitors ()
1333 {
1334 add_show_from_set (add_set_cmd ("hash", no_class, var_boolean,
1335 (char *)&hashmark,
1336 "Set display of activity while downloading a file.\n\
1337 When enabled, a hashmark \'#\' is displayed.",
1338 &setlist),
1339 &showlist);
1340
1341 add_com ("monitor", class_obscure, monitor_command,
1342 "Send a command to the debug monitor.");
1343 }
This page took 0.056583 seconds and 4 git commands to generate.