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.
5 This file is part of GDB.
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.
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.
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. */
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.
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. */
38 #include <sys/types.h>
45 static void make_xmodem_packet ();
46 static void print_xmodem_packet ();
48 static void monitor_load_ascii_srec
PARAMS ((char *file
, int fromtty
));
50 static int monitor_make_srec
PARAMS ((char *buffer
, int type
,
52 unsigned char *myaddr
, int len
));
54 static void monitor_fetch_register
PARAMS ((int regno
));
55 static void monitor_store_register
PARAMS ((int regno
));
57 static int from_hex ();
58 static unsigned long get_hex_word
PARAMS ((void));
60 struct monitor_ops
*current_monitor
;
62 static char *loadtype_str
= "srec";
63 static char *loadproto_str
= "none";
65 static int hashmark
; /* flag set by "set hash" */
67 static int timeout
= 30;
69 static int expect
PARAMS ((char *string
, char *buf
, int buflen
));
70 static int expect_prompt
PARAMS ((char *buf
, int buflen
));
72 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
73 and i386-stub.c. Normally, no one would notice because it only matters
74 for writing large chunks of memory (e.g. in downloads). Also, this needs
75 to be more than 400 if required to hold the registers (see below, where
76 we round it up based on REGISTER_BYTES). */
80 /* Descriptor for I/O to remote machine. Initialize it to NULL so
81 that monitor_open knows that we don't have a file open when the
84 static serial_t monitor_desc
= NULL
;
86 static void monitor_load_srec ();
88 /* These definitions are for xmodem protocol. */
95 #define GETACK getacknak(ACK)
96 #define GETNAK getacknak(NAK)
97 #define XMODEM_DATASIZE 128 /* the data size is ALWAYS 128 */
98 #define XMODEM_PACKETSIZE 131 /* the packet size is ALWAYS 132 (zero based) */
101 static unsigned char output_buf
[0x200];
105 debug_save_output (buf
, len
)
110 for (; len
> 0; len
--)
111 output_buf
[obp
++ & 0x1ff] = *buf
++;
113 fputs_unfiltered (buf
, gdb_stdout
);
117 static unsigned char input_buf
[0x200];
121 debug_save_input_char (c
)
125 input_buf
[ibp
++ & 0x1ff] = c
;
127 fputc_unfiltered (c
, gdb_stdout
);
131 /* printf_monitor -- send data to monitor. Works just like printf. */
134 printf_monitor (va_alist
)
143 pattern
= va_arg (args
, char *);
145 vsprintf (buf
, pattern
, args
);
147 if (remote_debug
> 0)
148 debug_save_output (buf
, strlen (buf
));
150 if (strlen (buf
) > PBUFSIZ
)
151 error ("printf_monitor(): string too long");
152 if (SERIAL_WRITE(monitor_desc
, buf
, strlen (buf
)))
153 fprintf_unfiltered (stderr
, "SERIAL_WRITE failed: %s\n", safe_strerror (errno
));
156 /* Send raw data to monitor. */
159 write_monitor (data
, len
)
163 if (SERIAL_WRITE (monitor_desc
, data
, len
))
164 fprintf_unfiltered (stderr
, "SERIAL_WRITE failed: %s\n", safe_strerror(errno
));
166 *(data
+ len
+ 1) = '\0';
169 /* Read a character from the remote system, doing all the fancy
178 c
= SERIAL_READCHAR (monitor_desc
, timeout
);
180 if (remote_debug
> 0)
181 debug_save_input_char (c
& 0x7f);
186 if (c
== SERIAL_TIMEOUT
)
189 return c
; /* Polls shouldn't generate timeout errors */
190 error ("Timeout reading from remote system.");
192 perror_with_name ("remote-monitor");
195 /* Scan input from the remote system, until STRING is found. If BUF is non-
196 zero, then collect input until either STRING has been collected or BUFLEN
197 chars have been collected. If input overflows BUF because STRING can't be
198 found, return -1, else return number of chars in BUF (including STRING). */
201 expect (string
, buf
, buflen
)
207 int obuflen
= buflen
;
221 c
= readchar (timeout
);
226 c
= readchar (timeout
);
234 return obuflen
- buflen
;
246 /* Keep discarding input until we see the MONITOR prompt.
248 The convention for dealing with the prompt is that you
250 o *then* wait for the prompt.
252 Thus the last thing that a procedure does with the serial line
253 will be an expect_prompt(). Exception: monitor_resume does not
254 wait for the prompt, because the terminal is being handed over
255 to the inferior. However, the next thing which happens after that
256 is a monitor_wait which does wait for the prompt.
257 Note that this includes abnormal exit, e.g. error(). This is
258 necessary to prevent getting into states from which we can't
262 expect_prompt (buf
, buflen
)
266 return expect (PROMPT
, buf
, buflen
);
269 /* Ignore junk characters. Returns a 1 if junk, 0 otherwise. */
289 /* Get a hex digit from the remote system & return its value. If
290 ignore is nonzero, ignore spaces, newline & tabs. */
293 get_hex_digit (ignore
)
300 ch
= readchar (timeout
);
304 if (ch
>= '0' && ch
<= '9')
306 else if (ch
>= 'A' && ch
<= 'F')
307 return ch
- 'A' + 10;
308 else if (ch
>= 'a' && ch
<= 'f')
309 return ch
- 'a' + 10;
310 else if (ch
== ' ' && ignore
)
314 expect_prompt (NULL
, 0);
315 error ("Invalid hex digit from remote system. (0x%x)", ch
);
320 /* Get a byte from monitor and put it in *BYT. Accept any number of
329 val
= get_hex_digit (1) << 4;
331 val
|= get_hex_digit (0);
335 /* Get N 32-bit words from remote, each preceded by a space, and put
336 them in registers starting at REGNO. */
346 ch
= readchar (timeout
);
351 for (i
= 7; i
>= 1; i
--)
353 ch
= readchar (timeout
);
356 val
= (val
<< 4) | from_hex (ch
);
362 /* This is called not only when we first attach, but also when the
363 user types "run" after having attached. */
366 monitor_create_inferior (execfile
, args
, env
)
374 error ("Can't pass arguments to remote MONITOR process");
376 if (execfile
== 0 || exec_bfd
== 0)
377 error ("No exec file specified");
379 entry_pt
= (int) bfd_get_start_address (exec_bfd
);
381 /* The "process" (board) is already stopped awaiting our commands, and
382 the program is already downloaded. We just set its PC and go. */
384 clear_proceed_status ();
386 /* Tell wait_for_inferior that we've started a new process. */
387 init_wait_for_inferior ();
389 /* Set up the "saved terminal modes" of the inferior
390 based on what modes we are starting it with. */
391 target_terminal_init ();
393 /* Install inferior's terminal modes. */
394 target_terminal_inferior ();
396 /* insert_step_breakpoint (); FIXME, do we need this? */
399 proceed ((CORE_ADDR
)entry_pt
, TARGET_SIGNAL_DEFAULT
, 0);
402 /* Open a connection to a remote debugger. NAME is the filename used
403 for communication. */
405 static char *dev_name
;
408 monitor_open (args
, mon_ops
, from_tty
)
410 struct monitor_ops
*mon_ops
;
413 struct target_ops
*targ_ops
;
417 targ_ops
= mon_ops
->target
;
418 name
= targ_ops
->to_shortname
;
421 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
422 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name
, name
);
424 target_preopen (from_tty
);
426 unpush_target (targ_ops
);
430 dev_name
= strsave (args
);
432 monitor_desc
= SERIAL_OPEN (dev_name
);
435 perror_with_name (dev_name
);
439 if (SERIAL_SETBAUDRATE (monitor_desc
, baud_rate
))
441 SERIAL_CLOSE (monitor_desc
);
442 perror_with_name (dev_name
);
446 SERIAL_RAW (monitor_desc
);
448 SERIAL_FLUSH_INPUT (monitor_desc
);
450 /* some systems only work with 2 stop bits */
452 SERIAL_SETSTOPBITS (monitor_desc
, mon_ops
->stopbits
);
454 current_monitor
= mon_ops
;
456 /* see if the target is alive. For a ROM monitor, we can just try to
457 force the prompt to print a few times. */
459 /* wake up the monitor and see if it's alive */
460 printf_monitor (mon_ops
->init
);
461 expect_prompt (NULL
, 0); /* See if we get a prompt */
463 /* try again to be sure */
464 printf_monitor (mon_ops
->init
);
465 expect_prompt (NULL
, 0); /* See if we get a prompt */
467 /* Setup the suffixes for the `set remoteloadtype' command */
469 add_show_from_set (add_set_enum_cmd ("remoteloadtype", no_class
,
471 (char *)&loadtype_str
,
472 "Set the remote load type.",
476 /* Setup the suffixes for the `set remoteloadprotocol' command */
478 add_show_from_set (add_set_enum_cmd ("remoteloadprotocol", no_class
,
480 (char *)&loadproto_str
,
481 "Set the remote load protocol.",
486 printf_unfiltered ("Remote target %s connected to %s\n", name
, dev_name
);
488 push_target (targ_ops
);
490 inferior_pid
= 42000; /* Make run command think we are busy... */
492 printf_monitor ("\r");
497 /* Close out all files and local state before this target loses
501 monitor_close (quitting
)
505 SERIAL_CLOSE (monitor_desc
);
509 /* Terminate the open connection to the remote debugger. Use this
510 when you want to detach and do something else with your gdb. */
513 monitor_detach (args
, from_tty
)
517 pop_target (); /* calls monitor_close to do the real work */
519 printf_unfiltered ("Ending remote %s debugging\n", target_shortname
);
522 /* Tell the remote machine to resume. */
525 monitor_resume (pid
, step
, sig
)
527 enum target_signal sig
;
530 printf_monitor (STEP_CMD
);
532 printf_monitor (CONT_CMD
);
535 /* Wait until the remote machine stops, then return, storing status in
536 status just as `wait' would. */
539 monitor_wait (pid
, status
)
541 struct target_waitstatus
*status
;
543 int old_timeout
= timeout
;
545 status
->kind
= TARGET_WAITKIND_EXITED
;
546 status
->value
.integer
= 0;
548 timeout
= -1; /* Don't time out -- user program is running. */
550 expect_prompt (NULL
, 0); /* Wait for prompt, outputting extraneous text */
552 status
->kind
= TARGET_WAITKIND_STOPPED
;
553 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
555 timeout
= old_timeout
;
560 /* Return the name of register number regno in the form input and output by
561 monitor. Currently, register_names just happens to contain exactly what
562 monitor wants. Lets take advantage of that just as long as possible! */
568 if (regno
< 0 || regno
> NUM_REGS
)
571 return REGNAMES (regno
);
574 /* Fetch register REGNO, or all registers if REGNO is -1. Returns
578 monitor_fetch_register (regno
)
581 unsigned LONGEST val
;
582 unsigned char regbuf
[MAX_REGISTER_RAW_SIZE
];
588 name
= get_reg_name (regno
);
593 /* send the register examine command */
595 printf_monitor (current_monitor
->getreg
.cmd
, name
);
597 /* If TERM is present, we wait for that to show up. Also, (if TERM is
598 present), we will send TERM_CMD if that is present. In any case, we collect
599 all of the output into buf, and then wait for the normal prompt. */
601 if (current_monitor
->getreg
.term
)
603 resp_len
= expect (current_monitor
->getreg
.term
, buf
, sizeof buf
); /* get response */
606 error ("monitor_fetch_register (%d): excessive response from monitor: %.*s.", resp_len
, buf
);
608 if (current_monitor
->getreg
.term_cmd
)
610 SERIAL_WRITE (monitor_desc
, current_monitor
->getreg
.term_cmd
,
611 strlen (current_monitor
->getreg
.term_cmd
));
612 expect_prompt (NULL
, 0);
616 resp_len
= expect_prompt (buf
, sizeof buf
); /* get response */
619 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
620 the register value. Otherwise, we just start searching from the start of
623 if (current_monitor
->getreg
.resp_delim
)
625 p
= strstr (buf
, current_monitor
->getreg
.resp_delim
);
627 error ("monitor_fetch_register (%d): bad response from monitor: %.*s.", resp_len
, buf
);
628 p
+= strlen (current_monitor
->getreg
.resp_delim
);
633 val
= strtoul (p
, &p1
, 16);
635 if (val
== 0 && p
== p1
)
636 error ("monitor_fetch_register (%d): bad value from monitor: %.*s.",
639 /* supply register stores in target byte order, so swap here */
641 store_unsigned_integer (regbuf
, REGISTER_RAW_SIZE (regno
), val
);
643 supply_register (regno
, regbuf
);
646 /* Read the remote registers into the block regs. */
649 monitor_fetch_registers (regno
)
654 monitor_fetch_register (regno
);
658 for (regno
= 0; regno
< NUM_REGS
; regno
++)
659 monitor_fetch_register (regno
);
662 /* Store register REGNO, or all if REGNO == 0. Return errno value. */
665 monitor_store_register (regno
)
669 unsigned LONGEST val
;
671 name
= get_reg_name (regno
);
675 val
= read_register (regno
);
677 /* send the register deposit command */
679 printf_monitor (current_monitor
->setreg
.cmd
, name
, val
);
681 /* It's possible that there are actually some monitors out there that will
682 prompt you when you set a register. In that case, you may need to add some
683 code here to deal with TERM and TERM_CMD (see monitor_fetch_register to get
684 an idea of what's needed...) */
686 expect_prompt (NULL
, 0);
689 /* Store the remote registers. */
692 monitor_store_registers (regno
)
697 monitor_store_register (regno
);
701 for (regno
= 0; regno
< NUM_REGS
; regno
++)
702 monitor_store_register (regno
);
705 /* Get ready to modify the registers array. On machines which store
706 individual registers, this doesn't need to do anything. On machines
707 which store all the registers in one fell swoop, this makes sure
708 that registers contains all the registers from the program being
712 monitor_prepare_to_store ()
714 /* Do nothing, since we can store individual regs */
718 monitor_files_info ()
720 printf_unfiltered ("\tAttached to %s at %d baud.\n", dev_name
, baud_rate
);
724 monitor_write_memory (memaddr
, myaddr
, len
)
726 unsigned char *myaddr
;
729 /* send the memory deposit command */
731 printf_monitor (current_monitor
->setmem
.cmd
, memaddr
, *myaddr
);
733 /* It's possible that there are actually some monitors out there that will
734 prompt you when you deposit to memory. In that case, you may need to add
735 some code here to deal with TERM and TERM_CMD (see monitor_read_memory to
736 get an idea of what's needed...) */
738 expect_prompt (NULL
, 0);
743 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's memory
744 at MEMADDR. Returns length moved. Currently, we only do one byte at a
748 monitor_read_memory (memaddr
, myaddr
, len
)
753 unsigned LONGEST val
;
754 unsigned char regbuf
[MAX_REGISTER_RAW_SIZE
];
760 /* send the memory examine command */
762 printf_monitor (current_monitor
->getmem
.cmd
, memaddr
);
764 /* If TERM is present, we wait for that to show up. Also, (if TERM is
765 present), we will send TERM_CMD if that is present. In any case, we collect
766 all of the output into buf, and then wait for the normal prompt. */
768 if (current_monitor
->getmem
.term
)
770 resp_len
= expect (current_monitor
->getmem
.term
, buf
, sizeof buf
); /* get response */
773 error ("monitor_read_memory (%d): excessive response from monitor: %.*s.", resp_len
, buf
);
775 if (current_monitor
->getmem
.term_cmd
)
777 SERIAL_WRITE (monitor_desc
, current_monitor
->getmem
.term_cmd
,
778 strlen (current_monitor
->getmem
.term_cmd
));
779 expect_prompt (NULL
, 0);
783 resp_len
= expect_prompt (buf
, sizeof buf
); /* get response */
785 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
786 the register value. Otherwise, we just start searching from the start of
789 if (current_monitor
->getmem
.resp_delim
)
791 p
= strstr (buf
, current_monitor
->getmem
.resp_delim
);
793 error ("monitor_read_memory (%d): bad response from monitor: %.*s.", resp_len
, buf
);
794 p
+= strlen (current_monitor
->getmem
.resp_delim
);
799 val
= strtoul (p
, &p1
, 16);
801 if (val
== 0 && p
== p1
)
802 error ("monitor_read_memory (%d): bad value from monitor: %.*s.",
807 return 1; /* Got 1 byte */
810 /* FIXME-someday! merge these two. */
813 monitor_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
818 struct target_ops
*target
; /* ignored */
821 return monitor_write_memory (memaddr
, myaddr
, len
);
823 return monitor_read_memory (memaddr
, myaddr
, len
);
827 monitor_kill (args
, from_tty
)
831 return; /* ignore attempts to kill target system */
834 /* Clean up when a program exits.
835 The program actually lives on in the remote processor's RAM, and may be
836 run again without a download. Don't leave it full of breakpoint
840 monitor_mourn_inferior ()
842 remove_breakpoints ();
843 generic_mourn_inferior (); /* Do all the proper things now */
846 #define NUM_MONITOR_BREAKPOINTS 8
848 static CORE_ADDR breakaddr
[NUM_MONITOR_BREAKPOINTS
] = {0};
850 /* Tell the monitor to add a breakpoint. */
853 monitor_insert_breakpoint (addr
, shadow
)
858 static unsigned char break_insn
[] = BREAKPOINT
;
860 for (i
= 0; i
< NUM_MONITOR_BREAKPOINTS
; i
++)
862 if (breakaddr
[i
] == 0)
865 monitor_read_memory (addr
, shadow
, sizeof (break_insn
));
866 printf_monitor (SET_BREAK_CMD
, addr
);
867 expect_prompt (NULL
, 0);
872 error ("Too many breakpoints (> %d) for monitor.", NUM_MONITOR_BREAKPOINTS
);
875 /* Tell the monitor to remove a breakpoint. */
878 monitor_remove_breakpoint (addr
, shadow
)
884 for (i
= 0; i
< NUM_MONITOR_BREAKPOINTS
; i
++)
886 if (breakaddr
[i
] == addr
)
889 /* some monitors remove breakpoints based on the address */
891 printf_monitor(CLR_BREAK_CMD
, addr
);
893 printf_monitor(CLR_BREAK_CMD
, i
);
894 expect_prompt (NULL
, 0);
898 fprintf_unfiltered (stderr
, "Can't find breakpoint associated with 0x%x\n", addr
);
902 /* monitor_load -- load a file. This file determines which of the
903 * supported formats to use. The current types are:
904 * FIXME: not all types supported yet.
905 * default - reads any file using bfd and writes it to memory. This
907 * srec - reads binary file using bfd and writes it as an
909 * xmodem-bin - reads a binary file using bfd, and downloads it
910 * using xmodem protocol.
911 * xmodem-srec - reads a binary file using bfd, and after converting
912 * it downloads it as an srecord using xmodem protocol.
913 * ascii-srec - reads a ascii srecord file and downloads it
915 * ascii-xmodem - reads a ascii file and downloads using xmodem
920 monitor_load (file
, fromtty
)
924 /* default, load a binary */
925 if (STREQ (loadtype_str
, "default"))
927 error ("default load type not supported.");
930 /* load an srecord by converting */
931 if ((STREQ (loadtype_str
, "srec")) && STREQ (loadproto_str
, "xmodem"))
933 monitor_load_srec (file
, XMODEM
);
937 /* load an srecord by converting */
938 if (STREQ (loadtype_str
, "srec"))
940 monitor_load_srec (file
, 0); /* if from a binary */
944 /* load an srecord by converting */
945 if (STREQ (loadtype_str
, "none"))
947 error ("Unimplemented");
951 /* load an srecord file */
952 if (STREQ (loadproto_str
, "none"))
954 monitor_load_ascii_srec (file
, fromtty
); /* if from a binary */
958 if (STREQ (loadproto_str
, "xmodem"))
960 monitor_load_srec (file
, XMODEM
);
965 /* Download an ASCII srecord file. */
967 #define DOWNLOAD_LINE_SIZE 100
970 monitor_load_ascii_srec (file
, fromtty
)
975 char buf
[DOWNLOAD_LINE_SIZE
];
978 download
= fopen (file
, "r");
979 if (download
== NULL
)
981 error ("%s does not exist", file
);
985 printf_monitor (LOAD_CMD
);
987 while (!feof (download
))
989 bytes_read
= fread (buf
, sizeof (char), DOWNLOAD_LINE_SIZE
, download
);
992 putchar_unfiltered ('.');
993 gdb_flush (gdb_stdout
);
995 if (SERIAL_WRITE (monitor_desc
, buf
, bytes_read
))
997 fprintf_unfiltered (stderr
, "SERIAL_WRITE failed: (while downloading) %s\n",
998 safe_strerror (errno
));
1002 while (i
++ <=200) {} ; /* Ugly HACK, probably needs flow control */
1003 if (bytes_read
< DOWNLOAD_LINE_SIZE
)
1005 if (!feof (download
))
1006 error ("Only read %d bytes\n", bytes_read
);
1012 putchar_unfiltered ('\n');
1014 if (!feof (download
))
1015 error ("Never got EOF while downloading");
1016 expect_prompt (NULL
, 0);
1020 /* Put a command string, in args, out to MONITOR. Output from MONITOR
1021 is placed on the users terminal until the prompt is seen. FIXME: We
1022 read the characters ourseleves here cause of a nasty echo. */
1025 monitor_command (args
, fromtty
)
1033 if (monitor_desc
== NULL
)
1034 error ("monitor target not open.");
1036 /* Send the command. Note that if no args were supplied, then we're
1037 just sending the monitor a newline, which is sometimes useful. */
1039 printf_monitor ("%s\n", (args
? args
: ""));
1041 expect_prompt (NULL
, 0);
1044 /* Download a binary file by converting it to srecords. This
1045 will also use xmodem to download the resulting file.
1047 A download goes like this when using xmodem:
1050 <-------- (packet) [SOH|1|1|data|SUM]
1052 <-------- (packet) [SOH|2|2|data|SUM]
1063 monitor_load_srec (args
, protocol
)
1069 char *buffer
, srec
[1024];
1070 char packet
[XMODEM_PACKETSIZE
];
1073 int type
= 0; /* default to a type 0, header record */
1074 int srec_frame
= 57; /* FIXME: this must be 57 There is 12 bytes
1075 of header, and 2 bytes of checksum at the end.
1076 The problem is an xmodem packet holds exactly
1079 abfd
= bfd_openr (args
, 0);
1082 printf_filtered ("Unable to open file %s\n", args
);
1086 if (bfd_check_format (abfd
, bfd_object
) == 0)
1088 printf_filtered ("File is not an object file\n");
1092 printf_monitor (LOAD_CMD
); /* tell the monitor to load */
1094 /* get the NAK from the target */
1095 if (protocol
== XMODEM
)
1099 printf_monitor ("%c", EOT
);
1100 error ("Never got the NAK to start loading");
1105 while (s
!= (asection
*) NULL
)
1107 if (s
->flags
& SEC_LOAD
)
1109 buffer
= xmalloc (srec_frame
);
1111 printf_filtered ("%s\t: 0x%4x .. 0x%4x ",
1112 s
->name
, s
->vma
, s
->vma
+ s
->_raw_size
);
1113 gdb_flush (gdb_stdout
);
1114 for (i
= 0; i
< s
->_raw_size
; i
+= srec_frame
)
1116 if (srec_frame
> s
->_raw_size
- i
)
1117 srec_frame
= s
->_raw_size
- i
;
1119 bfd_get_section_contents (abfd
, s
, buffer
, i
, srec_frame
);
1120 monitor_make_srec (srec
, type
, s
->vma
+ i
, buffer
, srec_frame
);
1121 /* send a packet using xmodem */
1122 if (protocol
== XMODEM
)
1124 make_xmodem_packet (packet
, srec
, XMODEM_DATASIZE
);
1125 write_monitor (packet
, XMODEM_PACKETSIZE
+1);
1127 while (retries
++ <= 3)
1134 write_monitor (packet
, XMODEM_PACKETSIZE
+1);
1135 if (GETACK
) /* ACKnowledged, get next data chunk */
1139 { /* assume we got an ACK */
1142 putchar_unfiltered ('#');
1143 gdb_flush (gdb_stdout
);
1149 { /* too many tries, must be hosed */
1150 printf_monitor ("%c", EOT
);
1151 error ("Never got a ACK after sending an xmodem packet");
1155 { /* no protocols at all */
1156 printf_monitor ("%s\n", srec
);
1160 putchar_unfiltered ('#');
1161 gdb_flush (gdb_stdout
);
1163 type
= 3; /* switch to a 4 byte address record */
1164 gdb_flush (gdb_stdout
);
1170 putchar_unfiltered ('\n');
1172 /* Write a type 7 terminator record. no data for a type 7, and there
1173 is no data, so len is 0. */
1175 if (protocol
== XMODEM
)
1177 /* send a packet using xmodem */
1178 monitor_make_srec (srec
, 7, abfd
->start_address
, "", 0);
1179 make_xmodem_packet (packet
, srec
, XMODEM_DATASIZE
);
1180 write_monitor (packet
, XMODEM_PACKETSIZE
+1);
1184 monitor_make_srec (srec
, 7, abfd
->start_address
, "", 0);
1185 printf_monitor ("%s\n", srec
);
1187 if (protocol
== XMODEM
)
1189 printf_monitor ("%c", EOT
);
1191 error ("Never got ACK after sending EOT");
1195 putchar_unfiltered ('\n');
1197 expect_prompt (NULL
, 0);
1200 /* Get an ACK or a NAK from the target. returns 1 (true) or 0 (false)
1201 This is for xmodem. ANy string starting with "***" is an error
1202 message from the target. Here's a few from the WinBond w89k
1204 *** Too many errors found.
1206 *** Command syntax error
1219 character
= (char) readchar (0);
1220 if ((character
== 0xfffffffe) || (character
== 0x7f))
1225 if (character
== CANCEL
)
1226 { /* target aborted load */
1227 expect_prompt (NULL
, 0);
1228 error ("Got a CANCEL from the target.");
1230 if (character
== '*')
1231 { /* look for missed error message */
1232 expect_prompt (NULL
, 0);
1233 error ("Got an error message from the target");
1235 if (character
== byte
) /* got what we wanted */
1237 if (character
== ((byte
== ACK
) ? NAK
: ACK
))
1246 * monitor_make_srec -- make an srecord. This writes each line, one at a
1247 * time, each with it's own header and trailer line.
1248 * An srecord looks like this:
1250 * byte count-+ address
1251 * start ---+ | | data +- checksum
1253 * S01000006F6B692D746573742E73726563E4
1254 * S315000448600000000000000000FC00005900000000E9
1255 * S31A0004000023C1400037DE00F023604000377B009020825000348D
1256 * S30B0004485A0000000000004E
1259 * S<type><length><address><data><checksum>
1263 * is the number of bytes following upto the checksum. Note that
1264 * this is not the number of chars following, since it takes two
1265 * chars to represent a byte.
1269 * 1) two byte address data record
1270 * 2) three byte address data record
1271 * 3) four byte address data record
1272 * 7) four byte address termination record
1273 * 8) three byte address termination record
1274 * 9) two byte address termination record
1277 * is the start address of the data following, or in the case of
1278 * a termination record, the start address of the image
1282 * is the sum of all the raw byte data in the record, from the length
1283 * upwards, modulo 256 and subtracted from 255.
1287 monitor_make_srec (buffer
, type
, memaddr
, myaddr
, len
)
1291 unsigned char *myaddr
;
1302 /* Create the header for the srec. 4 is the number of bytes in the address,
1303 and 1 is the number of bytes in the count. */
1305 if (type
== 0) /* FIXME: type 0 is optional */
1306 type
= 3; /* so use data as it works */
1307 sprintf (buf
, "S%d%02X%08X", type
, len
+ 4 + 1, memaddr
);
1310 checksum
+= (len
+ 4 + 1 /* calculate the checksum */
1312 + ((memaddr
>> 8) & 0xff)
1313 + ((memaddr
>> 16) & 0xff)
1314 + ((memaddr
>> 24) & 0xff));
1316 /* build the srecord */
1317 for (i
= 0; i
< len
; i
++)
1319 sprintf (buf
, "%02X", myaddr
[i
]);
1320 checksum
+= myaddr
[i
];
1324 sprintf(buf
, "%02X", ~checksum
& 0xff); /* add the checksum */
1329 /* Take 128 bytes of data and make a packet out of it.
1331 * Each packet looks like this:
1332 * +-----+-------+-------+------+-----+
1333 * | SOH | Seq1. | Seq2. | data | SUM |
1334 * +-----+-------+-------+------+-----+
1336 * Seq1 = The sequence number.
1337 * Seq2 = The complement of the sequence number.
1338 * Data = A 128 bytes of data.
1339 * SUM = Add the contents of the 128 bytes and use the low-order
1340 * 8 bits of the result.
1344 make_xmodem_packet (packet
, data
, len
)
1345 unsigned char *packet
;
1346 unsigned char *data
;
1349 static int sequence
= 1;
1354 /* build the packet header */
1356 packet
[1] = sequence
;
1357 packet
[2] = 255 - sequence
;
1360 packet
[2] = ~sequence
++; /* the complement is the sequence checksum */
1363 sum
= 0; /* calculate the data checksum */
1364 for (i
= 3; i
<= len
+ 2; i
++) {
1370 /* add padding for the rest of the packet */
1371 for (i
= len
+1 ; i
<= XMODEM_DATASIZE
; i
++)
1374 packet
[XMODEM_PACKETSIZE
] = sum
& 0xff; /* add the checksum */
1377 /* Print the packet as a debug check. */
1380 print_xmodem_packet (packet
)
1387 /* take apart the packet header the packet header */
1388 if (packet
[0] == SOH
)
1389 printf_unfiltered ("SOH");
1391 error ("xmodem: SOH is wrong");
1393 /* check the sequence */
1396 lastseq
= packet
[1];
1397 if (packet
[2] != ~lastseq
)
1398 error ("xmodem: Sequence checksum is wrong");
1400 printf_filtered (" %d %d", lastseq
, ~lastseq
);
1403 /* check the data checksum */
1405 for (i
= 3; i
<= XMODEM_DATASIZE
; i
++)
1408 /* ignore the data */
1410 printf_unfiltered (" [128 bytes of data] %d\n", sum
& 0xff);
1412 printf_filtered (" [%s] %d\n", packet
, sum
& 0xff);
1414 if ((packet
[XMODEM_PACKETSIZE
] & 0xff) != (sum
& 0xff))
1415 printf_unfiltered ("xmodem: data checksum wrong, got a %d",
1416 packet
[XMODEM_PACKETSIZE
] & 0xff);
1418 putchar_unfiltered ('\n');
1421 /* Convert hex digit A to a number. */
1427 if (a
>= '0' && a
<= '9')
1429 if (a
>= 'a' && a
<= 'f')
1430 return a
- 'a' + 10;
1431 if (a
>= 'A' && a
<= 'F')
1432 return a
- 'A' + 10;
1434 error ("Reply contains invalid hex digit 0x%x", a
);
1437 /* Define additional commands that are usually only used by monitors. */
1440 _initialize_remote_monitors ()
1442 add_show_from_set (add_set_cmd ("hash", no_class
, var_boolean
,
1444 "Set display of activity while downloading a file.\n\
1445 When enabled, a hashmark \'#\' is displayed.",
1449 add_com ("monitor", class_obscure
, monitor_command
,
1450 "Send a command to the debug monitor.");