1 /* Remote debugging interface for boot monitors, for GDB.
2 Copyright 1990, 1991, 1992, 1993 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.
39 #include <sys/types.h>
43 #include "remote-utils.h"
46 # define TERMINAL struct termios
48 # define TERMINAL struct sgttyb
51 extern void make_xmodem_packet();
52 extern void print_xmodem_packet();
54 struct monitor_ops *current_monitor;
55 extern struct cmd_list_element *setlist;
56 extern struct cmd_list_element *unsetlist;
57 struct cmd_list_element *showlist;
59 extern char *host_name;
60 extern char *target_name;
62 static int hashmark; /* flag set by "set hash" */
64 #define LOG_FILE "monitor.log"
65 #if defined (LOG_FILE)
69 static int timeout = 24;
72 * Descriptor for I/O to remote machine. Initialize it to NULL so that
73 * monitor_open knows that we don't have a file open when the program starts.
75 static serial_t monitor_desc = NULL;
77 /* sets the download protocol, choices are srec, generic, boot */
79 static char *loadtype_str;
80 static char *loadproto_str;
81 static void set_loadtype_command();
82 static void set_loadproto_command();
83 static void monitor_load_srec();
84 static int monitor_write_srec();
87 * these definitions are for xmodem protocol
94 #define GETACK getacknak(ACK)
95 #define GETNAK getacknak(NAK)
96 #define XMODEM_DATASIZE 128 /* the data size is ALWAYS 128 */
97 #define XMODEM_PACKETSIZE 131 /* the packet size is ALWAYS 132 (zero based) */
101 * set_loadtype_command -- set the type for downloading. Check to make
102 * sure you have a support protocol for this target.
105 set_loadtype_command (ignore, from_tty, c)
108 struct cmd_list_element *c;
113 if (current_monitor == 0x0)
116 if (STREQ (LOADTYPES, "")) {
117 error ("No loadtype set");
121 tmp = savestring (LOADTYPES, strlen(LOADTYPES));
122 type = strtok(tmp, ",");
123 if (STREQ (type, (*(char **) c->var))) {
124 loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
128 while ((type = strtok (NULL, ",")) != (char *)NULL) {
129 if (STREQ (type, (*(char **) c->var)))
130 loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
134 error ("Loadtype \"%s\" does not exist.", (*(char **) c->var));
137 * set_loadproto_command -- set the protocol for downloading. Check to make
138 * sure you have a supported protocol for this target.
141 set_loadproto_command (ignore, from_tty, c)
144 struct cmd_list_element *c;
149 if (current_monitor == 0x0)
152 if (STREQ (LOADPROTOS, "")) {
153 error ("No load protocols set");
157 tmp = savestring (LOADPROTOS, strlen(LOADPROTOS));
158 type = strtok(tmp, ",");
159 if (STREQ (type, (*(char **) c->var))) {
160 loadproto_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
164 while ((type = strtok (NULL, ",")) != (char *)NULL) {
165 if (STREQ (type, (*(char **) c->var)))
166 loadproto_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
170 error ("Load protocol \"%s\" does not exist.", (*(char **) c->var));
174 * printf_monitor -- send data to monitor. Works just like printf.
177 printf_monitor(va_alist)
187 pattern = va_arg(args, char *);
189 vsprintf(buf, pattern, args);
191 debuglogs (1, "printf_monitor(), Sending: \"%s\".", buf);
193 if (SERIAL_WRITE(monitor_desc, buf, strlen(buf)))
194 fprintf(stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno));
197 * write_monitor -- send raw data to monitor.
200 write_monitor(data, len)
204 if (SERIAL_WRITE(monitor_desc, data, len))
205 fprintf(stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno));
207 *(data + len+1) = '\0';
208 debuglogs (1, "write_monitor(), Sending: \"%s\".", data);
213 * debuglogs -- deal with debugging info to multiple sources. This takes
214 * two real args, the first one is the level to be compared against
215 * the sr_get_debug() value, the second arg is a printf buffer and args
216 * to be formatted and printed. A CR is added after each string is printed.
230 level = va_arg(args, int); /* get the debug level */
231 if ((level <0) || (level > 100)) {
232 error ("Bad argument passed to debuglogs(), needs debug level");
236 pattern = va_arg(args, char *); /* get the printf style pattern */
238 vsprintf(buf, pattern, args); /* format the string */
240 /* convert some characters so it'll look right in the log */
242 for (i=0 ; buf[i] != '\0'; i++) {
244 case '\n': /* newlines */
248 case '\r': /* carriage returns */
252 case '\033': /* escape */
260 case '\b': /* backspace */
264 default: /* no change */
268 if (buf[i] < 26) { /* modify control characters */
274 *p = '\0'; /* terminate the string */
276 if (sr_get_debug() > level)
279 #ifdef LOG_FILE /* write to the monitor log */
280 if (log_file != 0x0) {
281 fputs (newbuf, log_file);
282 fputc ('\n', log_file);
288 /* readchar -- read a character from the remote system, doing all the fancy
297 c = SERIAL_READCHAR(monitor_desc, timeout);
299 if (sr_get_debug() > 5)
304 putc(c & 0x7f, log_file);
310 if (c == SERIAL_TIMEOUT) {
312 return c; /* Polls shouldn't generate timeout errors */
313 error("Timeout reading from remote system.");
315 fputs ("ERROR: Timeout reading from remote system", log_file);
318 perror_with_name("remote-monitor");
322 * expect -- scan input from the remote system, until STRING is found.
323 * If DISCARD is non-zero, then discard non-matching input, else print
324 * it out. Let the user break out immediately.
327 expect (string, discard)
335 debuglogs (1, "Expecting \"%s\".", string);
339 c = readchar(timeout);
345 debuglogs (4, "Matched");
350 fwrite(string, 1, (p - 1) - string, stdout);
359 /* Keep discarding input until we see the MONITOR prompt.
361 The convention for dealing with the prompt is that you
363 o *then* wait for the prompt.
365 Thus the last thing that a procedure does with the serial line
366 will be an expect_prompt(). Exception: monitor_resume does not
367 wait for the prompt, because the terminal is being handed over
368 to the inferior. However, the next thing which happens after that
369 is a monitor_wait which does wait for the prompt.
370 Note that this includes abnormal exit, e.g. error(). This is
371 necessary to prevent getting into states from which we can't
374 expect_prompt(discard)
377 expect (PROMPT, discard);
381 * junk -- ignore junk characters. Returns a 1 if junk, 0 otherwise
394 if (sr_get_debug() > 5)
395 debuglogs (5, "Ignoring \'%c\'.", ch);
398 if (sr_get_debug() > 5)
399 debuglogs (5, "Accepting \'%c\'.", ch);
405 * get_hex_digit -- Get a hex digit from the remote system & return its value.
406 * If ignore is nonzero, ignore spaces, newline & tabs.
409 get_hex_digit(ignore)
414 ch = readchar(timeout);
417 if (sr_get_debug() > 4)
418 debuglogs (4, "get_hex_digit() got a 0x%x(%c)", ch, ch);
420 if (ch >= '0' && ch <= '9')
422 else if (ch >= 'A' && ch <= 'F')
423 return ch - 'A' + 10;
424 else if (ch >= 'a' && ch <= 'f')
425 return ch - 'a' + 10;
426 else if (ch == ' ' && ignore)
430 error("Invalid hex digit from remote system. (0x%x)", ch);
435 /* get_hex_byte -- Get a byte from monitor and put it in *BYT.
436 * Accept any number leading spaces.
444 val = get_hex_digit (1) << 4;
445 debuglogs (4, "get_hex_digit() -- Read first nibble 0x%x", val);
447 val |= get_hex_digit (0);
448 debuglogs (4, "get_hex_digit() -- Read second nibble 0x%x", val);
451 debuglogs (4, "get_hex_digit() -- Read a 0x%x", val);
455 * get_hex_word -- Get N 32-bit words from remote, each preceded by a space,
456 * and put them in registers starting at REGNO.
465 for (i = 0; i < 8; i++)
466 val = (val << 4) + get_hex_digit (i == 0);
468 debuglogs (4, "get_hex_word() got a 0x%x.", val);
473 /* This is called not only when we first attach, but also when the
474 user types "run" after having attached. */
476 monitor_create_inferior (execfile, args, env)
484 error("Can't pass arguments to remote MONITOR process");
486 if (execfile == 0 || exec_bfd == 0)
487 error("No exec file specified");
489 entry_pt = (int) bfd_get_start_address (exec_bfd);
491 debuglogs (1, "create_inferior(exexfile=%s, args=%s, env=%s)", execfile, args, env);
493 /* The "process" (board) is already stopped awaiting our commands, and
494 the program is already downloaded. We just set its PC and go. */
496 clear_proceed_status ();
498 /* Tell wait_for_inferior that we've started a new process. */
499 init_wait_for_inferior ();
501 /* Set up the "saved terminal modes" of the inferior
502 based on what modes we are starting it with. */
503 target_terminal_init ();
505 /* Install inferior's terminal modes. */
506 target_terminal_inferior ();
508 /* insert_step_breakpoint (); FIXME, do we need this? */
511 proceed ((CORE_ADDR)entry_pt, TARGET_SIGNAL_DEFAULT, 0);
515 * monitor_open -- open a connection to a remote debugger.
516 * NAME is the filename used for communication.
518 static int baudrate = 9600;
519 static char dev_name[100];
522 monitor_open(args, name, from_tty)
529 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
530 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name);
535 strcpy(dev_name, args);
536 monitor_desc = SERIAL_OPEN(dev_name);
538 if (monitor_desc == NULL)
539 perror_with_name(dev_name);
541 if (baud_rate != -1) {
542 if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate)) {
543 SERIAL_CLOSE (monitor_desc);
544 perror_with_name (name);
548 SERIAL_RAW(monitor_desc);
550 #if defined (LOG_FILE)
551 log_file = fopen (LOG_FILE, "w");
552 if (log_file == NULL)
553 perror_with_name (LOG_FILE);
554 fprintf_filtered (log_file, "GDB %s (%s", version, host_name);
555 fprintf_filtered (log_file, " --target %s)\n", target_name);
556 fprintf_filtered (log_file, "Remote target %s connected to %s\n\n", TARGET_NAME, dev_name);
559 /* wake up the monitor and see if it's alive */
560 printf_monitor(INIT_CMD);
561 expect_prompt(1); /* See if we get a prompt */
563 /* try again to be sure */
564 printf_monitor(INIT_CMD);
565 expect_prompt(1); /* See if we get a prompt */
568 printf("Remote target %s connected to %s\n", TARGET_NAME, dev_name);
572 * monitor_close -- Close out all files and local state before this
573 * target loses control.
577 monitor_close (quitting)
580 SERIAL_CLOSE(monitor_desc);
583 debuglogs (1, "monitor_close (quitting=%d)", quitting);
585 #if defined (LOG_FILE)
587 if (ferror(log_file))
588 fprintf(stderr, "Error writing log file.\n");
589 if (fclose(log_file) != 0)
590 fprintf(stderr, "Error closing log file.\n");
596 * monitor_detach -- terminate the open connection to the remote
597 * debugger. Use this when you want to detach and do something
598 * else with your gdb.
601 monitor_detach (from_tty)
605 debuglogs (1, "monitor_detach ()");
607 pop_target(); /* calls monitor_close to do the real work */
609 printf ("Ending remote %s debugging\n", target_shortname);
613 * monitor_attach -- attach GDB to the target.
616 monitor_attach (args, from_tty)
621 printf ("Starting remote %s debugging\n", target_shortname);
623 debuglogs (1, "monitor_attach (args=%s)", args);
625 printf_monitor (GO_CMD);
626 /* swallow the echo. */
631 * monitor_resume -- Tell the remote machine to resume.
634 monitor_resume (pid, step, sig)
636 enum target_signal sig;
638 debuglogs (1, "monitor_resume (step=%d, sig=%d)", step, sig);
641 printf_monitor (STEP_CMD);
643 printf_monitor (CONT_CMD);
648 * monitor_wait -- Wait until the remote machine stops, then return,
649 * storing status in status just as `wait' would.
652 monitor_wait (pid, status)
654 struct target_waitstatus *status;
656 int old_timeout = timeout;
658 debuglogs(1, "monitor_wait (), printing extraneous text.");
660 status->kind = TARGET_WAITKIND_EXITED;
661 status->value.integer = 0;
663 timeout = 0; /* Don't time out -- user program is running. */
665 expect_prompt(0); /* Wait for prompt, outputting extraneous text */
666 debuglogs (4, "monitor_wait(), got the prompt.");
668 status->kind = TARGET_WAITKIND_STOPPED;
669 status->value.sig = TARGET_SIGNAL_TRAP;
671 timeout = old_timeout;
676 /* Return the name of register number regno in the form input and output by
677 monitor. Currently, register_names just happens to contain exactly what
678 monitor wants. Lets take advantage of that just as long as possible! */
693 for (p = REGNAMES(regno); *p; p++)
698 debuglogs (5, "Got name \"%s\" from regno #%d.", buf, regno);
704 * monitor_fetch_registers -- read the remote registers into the
708 monitor_fetch_registers ()
712 /* yeah yeah, i know this is horribly inefficient. but it isn't done
713 very often... i'll clean it up later. */
715 for (regno = 0; regno <= PC_REGNUM; regno++)
716 monitor_fetch_register(regno);
720 * monitor_fetch_register -- fetch register REGNO, or all registers if REGNO
721 * is -1. Returns errno value.
724 monitor_fetch_register (regno)
729 debuglogs (1, "monitor_fetch_register (reg=%s)", get_reg_name (regno));
732 monitor_fetch_registers ();
734 char *name = get_reg_name (regno);
737 printf_monitor (ROMCMD(GET_REG), name); /* send the command */
738 expect (name, 1); /* then strip the leading garbage */
739 if (*ROMDELIM(GET_REG) != 0) { /* if there's a delimiter */
740 expect (ROMDELIM(GET_REG), 1);
743 val = get_hex_word(); /* get the value, ignore junk */
744 supply_register (regno, (char *) &val);
746 if (*ROMDELIM(GET_REG) != 0) {
747 /*** expect (ROMRES(GET_REG)); ***/
748 printf_monitor (CMD_END);
755 /* Store the remote registers from the contents of the block REGS. */
758 monitor_store_registers ()
762 debuglogs (1, "monitor_store_registers()");
764 for (regno = 0; regno <= PC_REGNUM; regno++)
765 monitor_store_register(regno);
767 registers_changed ();
771 * monitor_store_register -- store register REGNO, or all if REGNO == 0.
772 * return errno value.
775 monitor_store_register (regno)
781 i = read_register(regno);
783 debuglogs (1, "monitor_store_register (regno=%d)", regno);
786 monitor_store_registers ();
788 debuglogs (3, "Setting register %s to 0x%x", get_reg_name (regno), read_register (regno));
790 name = get_reg_name (regno);
793 printf_monitor (ROMCMD(SET_REG), name, read_register(regno));
794 expect (name, 1); /* strip the leading garbage */
795 if (*ROMDELIM(SET_REG) != 0) { /* if there's a delimiter */
796 expect (ROMDELIM(SET_REG), 1);
798 printf_monitor ("%d%s\n", i, CMD_END);
805 printf_monitor (SET_REG, get_reg_name (regno),
806 read_register (regno));
812 /* Get ready to modify the registers array. On machines which store
813 individual registers, this doesn't need to do anything. On machines
814 which store all the registers in one fell swoop, this makes sure
815 that registers contains all the registers from the program being
819 monitor_prepare_to_store ()
821 /* Do nothing, since we can store individual regs */
825 monitor_files_info ()
827 printf ("\tAttached to %s at %d baud.\n",
832 * monitor_write_inferior_memory -- Copy LEN bytes of data from debugger
833 * memory at MYADDR to inferior's memory at MEMADDR. Returns length moved.
836 monitor_write_inferior_memory (memaddr, myaddr, len)
838 unsigned char *myaddr;
844 debuglogs (1, "monitor_write_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len);
846 for (i = 0; i < len; i++) {
847 printf_monitor (ROMCMD(SET_MEM), memaddr + i, myaddr[i] );
848 if (*ROMDELIM(SET_MEM) != 0) { /* if there's a delimiter */
849 expect (ROMDELIM(SET_MEM), 1);
851 printf_monitor ("%x", myaddr[i]);
853 /*** printf_monitor ("%x", myaddr[i]); ***/
854 if (sr_get_debug() > 1)
855 printf ("\nSet 0x%x to 0x%x\n", memaddr + i, myaddr[i]);
856 if (*ROMDELIM(SET_MEM) != 0) {
858 printf_monitor (CMD_END);
866 * monitor_read_inferior_memory -- read LEN bytes from inferior memory
867 * at MEMADDR. Put the result at debugger address MYADDR. Returns
871 monitor_read_inferior_memory(memaddr, myaddr, len)
879 /* Number of bytes read so far. */
882 /* Starting address of this pass. */
883 unsigned long startaddr;
885 /* Number of bytes to read in this pass. */
888 debuglogs (1, "monitor_read_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len);
890 /* Note that this code works correctly if startaddr is just less
891 than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
892 thing). That is, something like
893 monitor_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
894 works--it never adds len To memaddr and gets 0. */
895 /* However, something like
896 monitor_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
897 doesn't need to work. Detect it and give up if there's an attempt
899 if (((memaddr - 1) + len) < memaddr) {
906 while (count < len) {
908 if ((startaddr % 16) != 0)
909 len_this_pass -= startaddr % 16;
910 if (len_this_pass > (len - count))
911 len_this_pass = (len - count);
913 debuglogs (3, "Display %d bytes at %x", len_this_pass, startaddr);
915 for (i = 0; i < len_this_pass; i++) {
916 printf_monitor (ROMCMD(GET_MEM), startaddr, startaddr);
917 sprintf (buf, ROMCMD(GET_MEM), startaddr, startaddr);
918 if (*ROMDELIM(GET_MEM) != 0) { /* if there's a delimiter */
919 expect (ROMDELIM(GET_MEM), 1);
921 sprintf (buf, ROMCMD(GET_MEM), startaddr, startaddr);
922 expect (buf,1); /* get the command echo */
923 get_hex_word(1); /* strip away the address */
925 get_hex_byte (&myaddr[count++]); /* get the value at this address */
927 if (*ROMDELIM(GET_MEM) != 0) {
928 printf_monitor (CMD_END);
937 /* FIXME-someday! merge these two. */
939 monitor_xfer_inferior_memory (memaddr, myaddr, len, write, target)
944 struct target_ops *target; /* ignored */
947 return monitor_write_inferior_memory (memaddr, myaddr, len);
949 return monitor_read_inferior_memory (memaddr, myaddr, len);
953 monitor_kill (args, from_tty)
957 return; /* ignore attempts to kill target system */
960 /* Clean up when a program exits.
961 The program actually lives on in the remote processor's RAM, and may be
962 run again without a download. Don't leave it full of breakpoint
966 monitor_mourn_inferior ()
968 remove_breakpoints ();
969 generic_mourn_inferior (); /* Do all the proper things now */
972 #define MAX_MONITOR_BREAKPOINTS 16
974 extern int memory_breakpoint_size;
975 static CORE_ADDR breakaddr[MAX_MONITOR_BREAKPOINTS] = {0};
978 * monitor_insert_breakpoint -- add a breakpoint
981 monitor_insert_breakpoint (addr, shadow)
987 debuglogs (1, "monitor_insert_breakpoint() addr = 0x%x", addr);
989 for (i = 0; i <= MAX_MONITOR_BREAKPOINTS; i++) {
990 if (breakaddr[i] == 0) {
992 if (sr_get_debug() > 4)
993 printf ("Breakpoint at %x\n", addr);
994 monitor_read_inferior_memory(addr, shadow, memory_breakpoint_size);
995 printf_monitor(SET_BREAK_CMD, addr);
1001 fprintf(stderr, "Too many breakpoints (> 16) for monitor\n");
1006 * _remove_breakpoint -- Tell the monitor to remove a breakpoint
1009 monitor_remove_breakpoint (addr, shadow)
1015 debuglogs (1, "monitor_remove_breakpoint() addr = 0x%x", addr);
1017 for (i = 0; i < MAX_MONITOR_BREAKPOINTS; i++) {
1018 if (breakaddr[i] == addr) {
1020 /* some monitors remove breakpoints based on the address */
1022 printf_monitor(CLR_BREAK_CMD, addr);
1024 printf_monitor(CLR_BREAK_CMD, i);
1029 fprintf(stderr, "Can't find breakpoint associated with 0x%x\n", addr);
1033 /* monitor_load -- load a file. This file determines which of the
1034 * supported formats to use. The current types are:
1035 * FIXME: not all types supported yet.
1036 * default - reads any file using bfd and writes it to memory. This
1038 * srec - reads binary file using bfd and writes it as an
1040 * xmodem-bin - reads a binary file using bfd, and downloads it
1041 * using xmodem protocol.
1042 * xmodem-srec - reads a binary file using bfd, and after converting
1043 * it downloads it as an srecord using xmodem protocol.
1044 * ascii-srec - reads a ascii srecord file and downloads it
1046 * ascii-xmodem - reads a ascii file and downloads using xmodem
1050 monitor_load (file, fromtty)
1057 debuglogs (1, "Loading %s to monitor", file);
1059 if (STREQ (loadtype_str, "default")) { /* default, load a binary */
1060 gr_load_image (file, fromtty); /* by writing it into memory */
1064 /* load an srecord by converting */
1065 if ((STREQ (loadtype_str, "srec")) && STREQ (loadproto_str, "xmodem")) {
1066 monitor_load_srec(file, XMODEM);
1070 if (STREQ (loadtype_str, "srec")) { /* load an srecord by converting */
1071 monitor_load_srec(file, 0); /* if from a binary */
1075 if (STREQ (loadtype_str, "none")) { /* load an srecord by converting */
1076 error ("Unimplemented");
1080 if (STREQ (loadproto_str, "none")) { /* load an srecord file */
1081 monitor_load_ascii_srec(file, fromtty); /* if from a binary */
1085 if (STREQ (loadproto_str, "xmodem")) { /* load an srecord using the */
1086 monitor_load_srec(file, XMODEM);
1092 * monitor_load_ascii_srec -- download an ASCII srecord file.
1094 #define DOWNLOAD_LINE_SIZE 100
1096 monitor_load_ascii_srec (file, fromtty)
1101 char buf[DOWNLOAD_LINE_SIZE];
1104 debuglogs (1, "Loading an ASCII srecord file, %s.", file);
1106 download = fopen (file, "r");
1107 if (download == NULL) {
1108 error ("%s Does not exist", file);
1112 printf_monitor (LOAD_CMD);
1114 while (!feof (download)) {
1115 bytes_read = fread (buf, sizeof (char), DOWNLOAD_LINE_SIZE, download);
1120 if (SERIAL_WRITE(monitor_desc, buf, bytes_read)) {
1121 fprintf(stderr, "SERIAL_WRITE failed: (while downloading) %s\n", safe_strerror(errno));
1125 while (i++ <=200) {} ; /* Ugly HACK, probably needs flow control */
1126 if (bytes_read < DOWNLOAD_LINE_SIZE) {
1127 if (!feof (download))
1128 error ("Only read %d bytes\n", bytes_read);
1136 if (!feof (download))
1137 error ("Never got EOF while downloading");
1143 * monitor_command -- put a command string, in args, out to MONITOR.
1144 * Output from MONITOR is placed on the users terminal until the
1145 * prompt is seen. FIXME: We read the charcters ourseleves here
1146 * cause of a nasty echo.
1149 monitor_command (args, fromtty)
1158 debuglogs (1, "monitor_command (args=%s)", args);
1160 if (monitor_desc == NULL)
1161 error("monitor target not open.");
1164 error("Missing command.");
1166 printf_monitor ("%s\n", args);
1172 * monitor_load_srec -- download a binary file by converting it to srecords. This
1173 * will also use xmodem to download the resulting file.
1175 * A download goes like this when using xmodem:
1178 * <-------- (packet) [SOH|1|1|data|SUM]
1180 * <-------- (packet) [SOH|2|2|data|SUM]
1191 monitor_load_srec (args, protocol)
1199 char packet[XMODEM_PACKETSIZE];
1202 int type = 0; /* default to a type 0, header record */
1203 int srec_frame = 57; /* FIXME: this must be 57 There is 12 bytes
1204 of header, and 2 bytes of checksum at the end.
1205 The problem is an xmodem packet holds exactly
1208 abfd = bfd_openr (args, 0);
1210 printf_filtered ("Unable to open file %s\n", args);
1214 if (bfd_check_format (abfd, bfd_object) == 0) {
1215 printf_filtered ("File is not an object file\n");
1219 printf_monitor (LOAD_CMD); /* tell the monitor to load */
1220 if (protocol == XMODEM) { /* get the NAK from the target */
1222 debuglogs (3, "Got the NAK to start loading");
1224 printf_monitor ("%c", EOT);
1225 debuglogs (3, "Never got the NAK to start loading");
1226 error ("Never got the NAK to start loading");
1231 while (s != (asection *) NULL) {
1232 if (s->flags & SEC_LOAD) {
1233 char *buffer = xmalloc (srec_frame);
1234 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma, s->vma + s->_raw_size);
1236 for (i = 0; i < s->_raw_size; i += srec_frame) {
1237 if (srec_frame > s->_raw_size - i)
1238 srec_frame = s->_raw_size - i;
1240 bfd_get_section_contents (abfd, s, buffer, i, srec_frame);
1241 monitor_make_srec (srec, type, s->vma + i, buffer, srec_frame);
1242 if (protocol == XMODEM) { /* send a packet using xmodem */
1243 make_xmodem_packet (packet, srec, XMODEM_DATASIZE);
1244 write_monitor (packet, XMODEM_PACKETSIZE+1);
1246 while (retries++ <= 3) {
1247 if (GETNAK) { /* Resend packet */
1248 debuglogs (3, "Got a NAK, resending packet");
1250 write_monitor (packet, XMODEM_PACKETSIZE+1); /* send it again */
1251 if (GETACK) /* ACKnowledged, get next data chunk */
1253 } else { /* assume we got an ACK */
1255 printf_filtered ("#");
1256 debuglogs (3, "Got an ACK, sending next packet");
1260 if (retries >= 4) { /* too many tries, must be hosed */
1261 printf_monitor ("%c", EOT);
1262 error ("Never got a ACK after sending an xmodem packet");
1264 } else { /* no protocols at all */
1265 printf_monitor ("%s\n", srec);
1268 printf_filtered ("#");
1269 type = 3; /* switch to a 4 byte address record */
1272 printf_filtered ("\n");
1275 debuglogs (3, "%s doesn't need to be loaded", s->name);
1281 write a type 7 terminator record. no data for a type 7,
1282 and there is no data, so len is 0.
1284 if (protocol == XMODEM) { /* send a packet using xmodem */
1285 monitor_make_srec (srec, 7, abfd->start_address, "", 0);
1286 make_xmodem_packet (packet, srec, XMODEM_DATASIZE);
1287 write_monitor (packet, XMODEM_PACKETSIZE+1);
1289 monitor_make_srec (srec, 7, abfd->start_address, "", 0);
1290 printf_monitor ("%s\n", srec);
1292 if (protocol == XMODEM) {
1293 printf_monitor ("%c", EOT);
1295 error ("Never got ACK after sending EOT");
1305 * getacknak -- get an ACK or a NAK from the target.
1306 * returns 1 (true) or 0 (false) This is
1307 * for xmodem. ANy string starting with "***"
1308 * is an error message from the target.
1309 * Here's a few from the WinBond w89k "Cougar" PA board.
1310 * *** Too many errors found.
1312 * *** Command syntax error
1323 character = (char)readchar (0);
1324 if ((character == 0xfffffffe) || (character == 0x7f)) { /* empty uart */
1325 if (sr_get_debug() > 3)
1331 if (character == CANCEL) { /* target aborted load */
1333 error ("Got a CANCEL from the target.");
1335 if (character == '*') { /* look for missed error message */
1337 error ("Got an error message from the target");
1339 debuglogs (3, "Got a %s (0x%x or \'%c\'), expecting a %s.\n",
1340 (character == ACK) ? "ACK" : (character == NAK) ? "NAK" : "BOGUS",
1341 character, character, (byte == ACK) ? "ACK" : "NAK");
1342 if (character == byte) /* got what we wanted */
1344 if (character == ((byte == ACK) ? NAK : ACK)) { /* got the opposite */
1345 debuglogs (3, "Got the opposite, wanted 0x%x, got a 0x%x", byte, character);
1354 * monitor_make_srec -- make an srecord. This writes each line, one at a
1355 * time, each with it's own header and trailer line.
1356 * An srecord looks like this:
1358 * byte count-+ address
1359 * start ---+ | | data +- checksum
1361 * S01000006F6B692D746573742E73726563E4
1362 * S315000448600000000000000000FC00005900000000E9
1363 * S31A0004000023C1400037DE00F023604000377B009020825000348D
1364 * S30B0004485A0000000000004E
1367 * S<type><length><address><data><checksum>
1371 * is the number of bytes following upto the checksum. Note that
1372 * this is not the number of chars following, since it takes two
1373 * chars to represent a byte.
1377 * 1) two byte address data record
1378 * 2) three byte address data record
1379 * 3) four byte address data record
1380 * 7) four byte address termination record
1381 * 8) three byte address termination record
1382 * 9) two byte address termination record
1385 * is the start address of the data following, or in the case of
1386 * a termination record, the start address of the image
1390 * is the sum of all the raw byte data in the record, from the length
1391 * upwards, modulo 256 and subtracted from 255.
1394 monitor_make_srec (buffer, type, memaddr, myaddr, len)
1398 unsigned char *myaddr;
1406 debuglogs (4, "monitor_make_srec (buffer=0x%x, type=%d, memaddr=0x%x, len=%d",
1407 buffer, type, memaddr, len);
1411 create the header for the srec. 4 is the number of bytes in the address,
1412 and 1 is the number of bytes in the count.
1414 if (type == 0) /* FIXME: type 0 is optional */
1415 type = 3; /* so use data as it works */
1416 sprintf (buf, "S%d%02X%08X", type, len + 4 + 1, memaddr);
1419 checksum += (len + 4 + 1 /* calculate the checksum */
1421 + ((memaddr >> 8) & 0xff)
1422 + ((memaddr >> 16) & 0xff)
1423 + ((memaddr >> 24) & 0xff));
1425 for (i = 0; i < len; i++) { /* build the srecord */
1426 sprintf (buf, "%02X", myaddr[i]);
1427 checksum += myaddr[i];
1431 sprintf(buf, "%02X", ~checksum & 0xff); /* add the checksum */
1432 debuglogs (3, "srec is \"%s\"", buffer);
1438 * make_xmodem_packet -- this takes a 128 bytes of data and makes a packet
1441 * Each packet looks like this:
1442 * +-----+-------+-------+------+-----+
1443 * | SOH | Seq1. | Seq2. | data | SUM |
1444 * +-----+-------+-------+------+-----+
1446 * Seq1 = The sequence number.
1447 * Seq2 = The complement of the sequence number.
1448 * Data = A 128 bytes of data.
1449 * SUM = Add the contents of the 128 bytes and use the low-order
1450 * 8 bits of the result.
1453 make_xmodem_packet (packet, data, len)
1454 unsigned char packet[];
1455 unsigned char *data;
1458 static int sequence = 1;
1463 /* build the packet header */
1465 packet[1] = sequence;
1466 packet[2] = 255 - sequence;
1469 packet[2] = ~sequence++; /* the complement is the sequence checksum */
1472 sum = 0; /* calculate the data checksum */
1473 for (i = 3; i <= len + 2; i++) {
1479 for (i = len+1 ; i <= XMODEM_DATASIZE ; i++) { /* add padding for the rest of the packet */
1483 packet[XMODEM_PACKETSIZE] = sum & 0xff; /* add the checksum */
1485 if (sr_get_debug() > 4) {
1486 debuglogs (4, "The xmodem checksum is %d (0x%x)\n", sum & 0xff, sum & 0xff);
1487 print_xmodem_packet (packet);
1492 * print_xmodem_packet -- print the packet as a debug check
1495 print_xmodem_packet(packet)
1502 /* take apart the packet header the packet header */
1503 if (packet[0] == SOH) {
1506 error ("xmodem: SOH is wrong");
1509 /* check the sequence */
1510 if (packet[1] != 0) {
1511 lastseq = packet[1];
1512 if (packet[2] != ~lastseq)
1513 error ("xmodem: Sequence checksum is wrong");
1515 printf_filtered (" %d %d", lastseq, ~lastseq);
1518 /* check the data checksum */
1520 for (i = 3; i <= XMODEM_DATASIZE; i++) {
1524 /* ignore the data */
1526 printf (" [128 bytes of data] %d\n", sum & 0xff);
1528 printf_filtered (" [%s] %d\n", packet, sum & 0xff);
1530 if ((packet[XMODEM_PACKETSIZE] & 0xff) != (sum & 0xff)) {
1531 debuglogs (4, "xmodem: data checksum wrong, got a %d", packet[XMODEM_PACKETSIZE] & 0xff);
1537 * _initialize_remote_monitors -- setup a few addtitional commands that
1538 * are usually only used by monitors.
1541 _initialize_remote_monitors ()
1543 struct cmd_list_element *c;
1545 /* this sets the type of download protocol */
1546 c = add_set_cmd ("remoteloadprotocol", no_class, var_string, (char *)&loadproto_str,
1547 "Set the type of the remote load protocol.\n", &setlist);
1548 c->function.sfunc = set_loadproto_command;
1549 add_show_from_set (c, &showlist);
1550 loadproto_str = savestring ("none", 5);
1552 /* this sets the conversion type when loading */
1553 c = add_set_cmd ("remoteloadtype", no_class, var_string, (char *)&loadtype_str,
1554 "Set the type of the remote load protocol.\n", &setlist);
1555 c->function.sfunc = set_loadtype_command;
1556 add_show_from_set (c, &showlist);
1557 loadtype_str = savestring ("srec", 5);
1559 add_show_from_set (add_set_cmd ("hash", no_class, var_boolean,
1561 "Set display of activity while downloading a file.\n\
1562 When enabled, a period \'.\' is displayed.",
1566 /* generic monitor command */
1567 add_com ("monitor", class_obscure, monitor_command,
1568 "Send a command to the debug monitor.");