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"
45 #if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY)
52 # define TERMINAL struct termios
55 # define TERMINAL struct sgttyb
59 #define CSTOPB 0x00000040
62 #define SWAP_TARGET_AND_HOST(buffer,len) \
65 if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER) \
68 char *p = (char *)(buffer); \
69 char *q = ((char *)(buffer)) + len - 1; \
70 for (; p < q; p++, q--) \
81 extern void make_xmodem_packet();
82 extern void print_xmodem_packet();
84 struct monitor_ops *current_monitor;
85 extern struct cmd_list_element *setlist;
86 extern struct cmd_list_element *unsetlist;
87 struct cmd_list_element *showlist;
89 extern char *host_name;
90 extern char *target_name;
92 static int hashmark; /* flag set by "set hash" */
94 #define LOG_FILE "monitor.log"
95 #if defined (LOG_FILE)
99 static int timeout = 24;
102 * Descriptor for I/O to remote machine. Initialize it to NULL so that
103 * monitor_open knows that we don't have a file open when the program starts.
105 serial_t monitor_desc = NULL;
107 /* sets the download protocol, choices are srec, generic, boot */
109 static char *loadtype_str;
110 static char *loadproto_str;
111 static void set_loadtype_command();
112 static void set_loadproto_command();
113 static void monitor_load_srec();
114 static int monitor_write_srec();
117 * these definitions are for xmodem protocol
124 #define GETACK getacknak(ACK)
125 #define GETNAK getacknak(NAK)
126 #define XMODEM_DATASIZE 128 /* the data size is ALWAYS 128 */
127 #define XMODEM_PACKETSIZE 131 /* the packet size is ALWAYS 132 (zero based) */
131 * set_loadtype_command -- set the type for downloading. Check to make
132 * sure you have a support protocol for this target.
135 set_loadtype_command (ignore, from_tty, c)
138 struct cmd_list_element *c;
143 if (current_monitor == 0x0)
146 if (STREQ (LOADTYPES, "")) {
147 error ("No loadtype set");
151 tmp = savestring (LOADTYPES, strlen(LOADTYPES));
152 type = strtok(tmp, ",");
153 if (STREQ (type, (*(char **) c->var))) {
154 loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
158 while ((type = strtok (NULL, ",")) != (char *)NULL) {
159 if (STREQ (type, (*(char **) c->var)))
160 loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
164 error ("Loadtype \"%s\" does not exist.", (*(char **) c->var));
167 * set_loadproto_command -- set the protocol for downloading. Check to make
168 * sure you have a supported protocol for this target.
171 set_loadproto_command (ignore, from_tty, c)
174 struct cmd_list_element *c;
179 if (current_monitor == 0x0)
182 if (STREQ (LOADPROTOS, "")) {
183 error ("No load protocols set");
187 tmp = savestring (LOADPROTOS, strlen(LOADPROTOS));
188 type = strtok(tmp, ",");
189 if (STREQ (type, (*(char **) c->var))) {
190 loadproto_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
194 while ((type = strtok (NULL, ",")) != (char *)NULL) {
195 if (STREQ (type, (*(char **) c->var)))
196 loadproto_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
200 error ("Load protocol \"%s\" does not exist.", (*(char **) c->var));
204 * printf_monitor -- send data to monitor. Works just like printf.
207 printf_monitor(va_alist)
217 pattern = va_arg(args, char *);
219 vsprintf(buf, pattern, args);
221 debuglogs (1, "printf_monitor(), Sending: \"%s\".", buf);
223 if (SERIAL_WRITE(monitor_desc, buf, strlen(buf)))
224 fprintf(stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno));
227 * write_monitor -- send raw data to monitor.
230 write_monitor(data, len)
234 if (SERIAL_WRITE(monitor_desc, data, len))
235 fprintf(stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno));
237 *(data + len+1) = '\0';
238 debuglogs (1, "write_monitor(), Sending: \"%s\".", data);
243 * debuglogs -- deal with debugging info to multiple sources. This takes
244 * two real args, the first one is the level to be compared against
245 * the sr_get_debug() value, the second arg is a printf buffer and args
246 * to be formatted and printed. A CR is added after each string is printed.
260 level = va_arg(args, int); /* get the debug level */
261 if ((level <0) || (level > 100)) {
262 error ("Bad argument passed to debuglogs(), needs debug level");
266 pattern = va_arg(args, char *); /* get the printf style pattern */
268 vsprintf(buf, pattern, args); /* format the string */
270 /* convert some characters so it'll look right in the log */
272 for (i=0 ; buf[i] != '\0'; i++) {
274 case '\n': /* newlines */
278 case '\r': /* carriage returns */
282 case '\033': /* escape */
290 case '\b': /* backspace */
294 default: /* no change */
298 if (buf[i] < 26) { /* modify control characters */
304 *p = '\0'; /* terminate the string */
306 if (sr_get_debug() > level)
309 #ifdef LOG_FILE /* write to the monitor log */
310 if (log_file != 0x0) {
311 fputs (newbuf, log_file);
312 fputc ('\n', log_file);
318 /* readchar -- read a character from the remote system, doing all the fancy
327 c = SERIAL_READCHAR(monitor_desc, timeout);
329 if (sr_get_debug() > 5)
334 putc(c & 0x7f, log_file);
340 if (c == SERIAL_TIMEOUT) {
342 return c; /* Polls shouldn't generate timeout errors */
343 error("Timeout reading from remote system.");
345 fputs ("ERROR: Timeout reading from remote system", log_file);
348 perror_with_name("remote-monitor");
352 * expect -- scan input from the remote system, until STRING is found.
353 * If DISCARD is non-zero, then discard non-matching input, else print
354 * it out. Let the user break out immediately.
357 expect (string, discard)
365 debuglogs (1, "Expecting \"%s\".", string);
369 c = readchar(timeout);
375 debuglogs (4, "Matched");
380 fwrite(string, 1, (p - 1) - string, stdout);
389 /* Keep discarding input until we see the MONITOR prompt.
391 The convention for dealing with the prompt is that you
393 o *then* wait for the prompt.
395 Thus the last thing that a procedure does with the serial line
396 will be an expect_prompt(). Exception: monitor_resume does not
397 wait for the prompt, because the terminal is being handed over
398 to the inferior. However, the next thing which happens after that
399 is a monitor_wait which does wait for the prompt.
400 Note that this includes abnormal exit, e.g. error(). This is
401 necessary to prevent getting into states from which we can't
404 expect_prompt(discard)
407 expect (PROMPT, discard);
411 * junk -- ignore junk characters. Returns a 1 if junk, 0 otherwise
424 if (sr_get_debug() > 5)
425 debuglogs (5, "Ignoring \'%c\'.", ch);
428 if (sr_get_debug() > 5)
429 debuglogs (5, "Accepting \'%c\'.", ch);
435 * get_hex_digit -- Get a hex digit from the remote system & return its value.
436 * If ignore is nonzero, ignore spaces, newline & tabs.
439 get_hex_digit(ignore)
444 ch = readchar(timeout);
447 if (sr_get_debug() > 4) {
448 debuglogs (4, "get_hex_digit() got a 0x%x(%c)", ch, ch);
450 #ifdef LOG_FILE /* write to the monitor log */
451 if (log_file != 0x0) {
452 fputs ("get_hex_digit() got a 0x", log_file);
453 fputc (ch, log_file);
454 fputc ('\n', log_file);
460 if (ch >= '0' && ch <= '9')
462 else if (ch >= 'A' && ch <= 'F')
463 return ch - 'A' + 10;
464 else if (ch >= 'a' && ch <= 'f')
465 return ch - 'a' + 10;
466 else if (ch == ' ' && ignore)
470 debuglogs (4, "Invalid hex digit from remote system. (0x%x)", ch);
471 error("Invalid hex digit from remote system. (0x%x)", ch);
476 /* get_hex_byte -- Get a byte from monitor and put it in *BYT.
477 * Accept any number leading spaces.
485 val = get_hex_digit (1) << 4;
486 debuglogs (4, "get_hex_byte() -- Read first nibble 0x%x", val);
488 val |= get_hex_digit (0);
489 debuglogs (4, "get_hex_byte() -- Read second nibble 0x%x", val);
492 debuglogs (4, "get_hex_byte() -- Read a 0x%x", val);
496 * get_hex_word -- Get N 32-bit words from remote, each preceded by a space,
497 * and put them in registers starting at REGNO.
508 if (HOST_BYTE_ORDER == BIG_ENDIAN) {
510 for (i = 0; i < 8; i++)
511 val = (val << 4) + get_hex_digit (i == 0);
514 for (i = 7; i >= 0; i--)
515 val = (val << 4) + get_hex_digit (i == 0);
519 debuglogs (4, "get_hex_word() got a 0x%x for a %s host.", val, (HOST_BYTE_ORDER == BIG_ENDIAN) ? "big endian" : "little endian");
524 /* This is called not only when we first attach, but also when the
525 user types "run" after having attached. */
527 monitor_create_inferior (execfile, args, env)
535 error("Can't pass arguments to remote MONITOR process");
537 if (execfile == 0 || exec_bfd == 0)
538 error("No exec file specified");
540 entry_pt = (int) bfd_get_start_address (exec_bfd);
542 debuglogs (3, "create_inferior(exexfile=%s, args=%s, env=%s)", execfile, args, env);
544 /* The "process" (board) is already stopped awaiting our commands, and
545 the program is already downloaded. We just set its PC and go. */
547 clear_proceed_status ();
549 /* Tell wait_for_inferior that we've started a new process. */
550 init_wait_for_inferior ();
552 /* Set up the "saved terminal modes" of the inferior
553 based on what modes we are starting it with. */
554 target_terminal_init ();
556 /* Install inferior's terminal modes. */
557 target_terminal_inferior ();
559 /* insert_step_breakpoint (); FIXME, do we need this? */
562 proceed ((CORE_ADDR)entry_pt, TARGET_SIGNAL_DEFAULT, 0);
566 * monitor_open -- open a connection to a remote debugger.
567 * NAME is the filename used for communication.
569 static int baudrate = 9600;
570 static char dev_name[100];
573 monitor_open(args, name, from_tty)
578 TERMINAL *temptempio;
581 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
582 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name);
587 strcpy(dev_name, args);
588 monitor_desc = SERIAL_OPEN(dev_name);
590 if (monitor_desc == NULL)
591 perror_with_name(dev_name);
593 if (baud_rate != -1) {
594 if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate)) {
595 SERIAL_CLOSE (monitor_desc);
596 perror_with_name (name);
600 SERIAL_RAW(monitor_desc);
603 /* some systems only work with 2 stop bits */
605 temptempio = (TERMINAL *)SERIAL_GET_TTY_STATE(monitor_desc);
607 temptempio->sg_cflag |= baud_rate | CSTOPB;
609 temptempio->c_cflag |= baud_rate | CSTOPB;
611 SERIAL_SET_TTY_STATE(monitor_desc, temptempio);
612 debuglogs (4, "Set serial port to 2 stop bits");
614 #endif /* __GO32__ */
616 #if defined (LOG_FILE)
617 log_file = fopen (LOG_FILE, "w");
618 if (log_file == NULL)
619 perror_with_name (LOG_FILE);
620 fprintf_filtered (log_file, "GDB %s (%s", version, host_name);
621 fprintf_filtered (log_file, " --target %s)\n", target_name);
622 fprintf_filtered (log_file, "Remote target %s connected to %s\n\n", TARGET_NAME, dev_name);
625 /* wake up the monitor and see if it's alive */
626 printf_monitor(INIT_CMD);
627 expect_prompt(1); /* See if we get a prompt */
629 /* try again to be sure */
630 printf_monitor(INIT_CMD);
631 expect_prompt(1); /* See if we get a prompt */
634 printf("Remote target %s connected to %s\n", TARGET_NAME, dev_name);
638 * monitor_close -- Close out all files and local state before this
639 * target loses control.
643 monitor_close (quitting)
646 SERIAL_CLOSE(monitor_desc);
649 debuglogs (1, "monitor_close (quitting=%d)", quitting);
651 #if defined (LOG_FILE)
653 if (ferror(log_file))
654 fprintf(stderr, "Error writing log file.\n");
655 if (fclose(log_file) != 0)
656 fprintf(stderr, "Error closing log file.\n");
662 * monitor_detach -- terminate the open connection to the remote
663 * debugger. Use this when you want to detach and do something
664 * else with your gdb.
667 monitor_detach (from_tty)
671 debuglogs (1, "monitor_detach ()");
673 pop_target(); /* calls monitor_close to do the real work */
675 printf ("Ending remote %s debugging\n", target_shortname);
679 * monitor_attach -- attach GDB to the target.
682 monitor_attach (args, from_tty)
687 printf ("Starting remote %s debugging\n", target_shortname);
689 debuglogs (1, "monitor_attach (args=%s)", args);
691 printf_monitor (GO_CMD);
692 /* swallow the echo. */
697 * monitor_resume -- Tell the remote machine to resume.
700 monitor_resume (pid, step, sig)
702 enum target_signal sig;
704 debuglogs (1, "monitor_resume (step=%d, sig=%d)", step, sig);
707 printf_monitor (STEP_CMD);
709 printf_monitor (CONT_CMD);
714 * monitor_wait -- Wait until the remote machine stops, then return,
715 * storing status in status just as `wait' would.
718 monitor_wait (pid, status)
720 struct target_waitstatus *status;
722 int old_timeout = timeout;
724 debuglogs(1, "monitor_wait (), printing extraneous text.");
726 status->kind = TARGET_WAITKIND_EXITED;
727 status->value.integer = 0;
729 timeout = 0; /* Don't time out -- user program is running. */
731 expect_prompt(0); /* Wait for prompt, outputting extraneous text */
732 debuglogs (4, "monitor_wait(), got the prompt.");
734 status->kind = TARGET_WAITKIND_STOPPED;
735 status->value.sig = TARGET_SIGNAL_TRAP;
739 timeout = old_timeout;
744 /* Return the name of register number regno in the form input and output by
745 monitor. Currently, register_names just happens to contain exactly what
746 monitor wants. Lets take advantage of that just as long as possible! */
761 for (p = REGNAMES(regno); *p; p++)
766 debuglogs (5, "Got name \"%s\" from regno #%d.", buf, regno);
772 * monitor_fetch_registers -- read the remote registers into the
776 monitor_fetch_registers ()
780 /* yeah yeah, i know this is horribly inefficient. but it isn't done
781 very often... i'll clean it up later. */
783 for (regno = 0; regno <= PC_REGNUM; regno++)
784 monitor_fetch_register(regno);
788 * monitor_fetch_register -- fetch register REGNO, or all registers if REGNO
789 * is -1. Returns errno value.
792 monitor_fetch_register (regno)
797 debuglogs (1, "monitor_fetch_register (reg=%s)", get_reg_name (regno));
800 monitor_fetch_registers ();
802 char *name = get_reg_name (regno);
805 printf_monitor (ROMCMD(GET_REG), name); /* send the command */
806 expect (name, 1); /* then strip the leading garbage */
807 if (*ROMDELIM(GET_REG) != 0) { /* if there's a delimiter */
808 expect (ROMDELIM(GET_REG), 1);
811 val = get_hex_word(); /* get the value, ignore junk */
814 /* supply register stores in target byte order, so swap here */
815 SWAP_TARGET_AND_HOST (&val, 4);
816 supply_register (regno, (char *) &val);
818 if (*ROMDELIM(GET_REG) != 0) {
819 /*** expect (ROMRES(GET_REG)); ***/
820 printf_monitor (CMD_END);
827 /* Store the remote registers from the contents of the block REGS. */
830 monitor_store_registers ()
834 debuglogs (1, "monitor_store_registers()");
836 for (regno = 0; regno <= PC_REGNUM; regno++)
837 monitor_store_register(regno);
839 registers_changed ();
843 * monitor_store_register -- store register REGNO, or all if REGNO == 0.
844 * return errno value.
847 monitor_store_register (regno)
853 i = read_register(regno);
855 debuglogs (1, "monitor_store_register (regno=%d)", regno);
858 monitor_store_registers ();
860 debuglogs (3, "Setting register %s to 0x%x", get_reg_name (regno), read_register (regno));
862 name = get_reg_name (regno);
865 printf_monitor (ROMCMD(SET_REG), name, read_register(regno));
866 expect (name, 1); /* strip the leading garbage */
867 if (*ROMDELIM(SET_REG) != 0) { /* if there's a delimiter */
868 expect (ROMDELIM(SET_REG), 1);
870 printf_monitor ("%d%s\n", i, CMD_END);
877 printf_monitor (SET_REG, get_reg_name (regno),
878 read_register (regno));
884 /* Get ready to modify the registers array. On machines which store
885 individual registers, this doesn't need to do anything. On machines
886 which store all the registers in one fell swoop, this makes sure
887 that registers contains all the registers from the program being
891 monitor_prepare_to_store ()
893 /* Do nothing, since we can store individual regs */
897 monitor_files_info ()
899 printf ("\tAttached to %s at %d baud.\n",
904 * monitor_write_inferior_memory -- Copy LEN bytes of data from debugger
905 * memory at MYADDR to inferior's memory at MEMADDR. Returns length moved.
908 monitor_write_inferior_memory (memaddr, myaddr, len)
910 unsigned char *myaddr;
916 debuglogs (1, "monitor_write_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len);
918 for (i = 0; i < len; i++) {
919 printf_monitor (ROMCMD(SET_MEM), memaddr + i, myaddr[i] );
920 if (*ROMDELIM(SET_MEM) != 0) { /* if there's a delimiter */
921 expect (ROMDELIM(SET_MEM), 1);
923 printf_monitor ("%x", myaddr[i]);
925 /*** printf_monitor ("%x", myaddr[i]); ***/
926 if (sr_get_debug() > 1)
927 printf ("\nSet 0x%x to 0x%x\n", memaddr + i, myaddr[i]);
928 if (*ROMDELIM(SET_MEM) != 0) {
930 printf_monitor (CMD_END);
938 * monitor_read_inferior_memory -- read LEN bytes from inferior memory
939 * at MEMADDR. Put the result at debugger address MYADDR. Returns
943 monitor_read_inferior_memory(memaddr, myaddr, len)
951 /* Number of bytes read so far. */
954 /* Starting address of this pass. */
955 unsigned long startaddr;
957 /* Starting address of this pass. */
958 unsigned long endaddr;
960 /* Number of bytes to read in this pass. */
963 debuglogs (1, "monitor_read_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len);
965 /* Note that this code works correctly if startaddr is just less
966 than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
967 thing). That is, something like
968 monitor_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
969 works--it never adds len To memaddr and gets 0. */
970 /* However, something like
971 monitor_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
972 doesn't need to work. Detect it and give up if there's an attempt
974 if (((memaddr - 1) + len) < memaddr) {
981 while (count < len) {
983 if ((startaddr % 16) != 0)
984 len_this_pass -= startaddr % 16;
985 if (len_this_pass > (len - count))
986 len_this_pass = (len - count);
988 debuglogs (3, "Display %d bytes at %x for Big Endian host", len_this_pass, startaddr);
990 for (i = 0; i < len_this_pass; i++) {
991 printf_monitor (ROMCMD(GET_MEM), startaddr, startaddr);
992 sprintf (buf, ROMCMD(GET_MEM), startaddr, startaddr);
993 if (*ROMDELIM(GET_MEM) != 0) { /* if there's a delimiter */
994 expect (ROMDELIM(GET_MEM), 1);
996 sprintf (buf, ROMCMD(GET_MEM), startaddr, startaddr);
997 expect (buf,1); /* get the command echo */
998 get_hex_word(1); /* strip away the address */
1000 get_hex_byte (&myaddr[count++]); /* get the value at this address */
1002 if (*ROMDELIM(GET_MEM) != 0) {
1003 printf_monitor (CMD_END);
1012 /* FIXME-someday! merge these two. */
1014 monitor_xfer_inferior_memory (memaddr, myaddr, len, write, target)
1019 struct target_ops *target; /* ignored */
1022 return monitor_write_inferior_memory (memaddr, myaddr, len);
1024 return monitor_read_inferior_memory (memaddr, myaddr, len);
1028 monitor_kill (args, from_tty)
1032 return; /* ignore attempts to kill target system */
1035 /* Clean up when a program exits.
1036 The program actually lives on in the remote processor's RAM, and may be
1037 run again without a download. Don't leave it full of breakpoint
1041 monitor_mourn_inferior ()
1043 remove_breakpoints ();
1044 generic_mourn_inferior (); /* Do all the proper things now */
1047 #define MAX_MONITOR_BREAKPOINTS 16
1049 extern int memory_breakpoint_size;
1050 static CORE_ADDR breakaddr[MAX_MONITOR_BREAKPOINTS] = {0};
1053 * monitor_insert_breakpoint -- add a breakpoint
1056 monitor_insert_breakpoint (addr, shadow)
1062 debuglogs (1, "monitor_insert_breakpoint() addr = 0x%x", addr);
1064 for (i = 0; i <= MAX_MONITOR_BREAKPOINTS; i++) {
1065 if (breakaddr[i] == 0) {
1066 breakaddr[i] = addr;
1067 if (sr_get_debug() > 4)
1068 printf ("Breakpoint at %x\n", addr);
1069 monitor_read_inferior_memory(addr, shadow, memory_breakpoint_size);
1070 printf_monitor(SET_BREAK_CMD, addr);
1076 fprintf(stderr, "Too many breakpoints (> 16) for monitor\n");
1081 * _remove_breakpoint -- Tell the monitor to remove a breakpoint
1084 monitor_remove_breakpoint (addr, shadow)
1090 debuglogs (1, "monitor_remove_breakpoint() addr = 0x%x", addr);
1092 for (i = 0; i < MAX_MONITOR_BREAKPOINTS; i++) {
1093 if (breakaddr[i] == addr) {
1095 /* some monitors remove breakpoints based on the address */
1097 printf_monitor(CLR_BREAK_CMD, addr);
1099 printf_monitor(CLR_BREAK_CMD, i);
1104 fprintf(stderr, "Can't find breakpoint associated with 0x%x\n", addr);
1108 /* monitor_load -- load a file. This file determines which of the
1109 * supported formats to use. The current types are:
1110 * FIXME: not all types supported yet.
1111 * default - reads any file using bfd and writes it to memory. This
1113 * srec - reads binary file using bfd and writes it as an
1115 * xmodem-bin - reads a binary file using bfd, and downloads it
1116 * using xmodem protocol.
1117 * xmodem-srec - reads a binary file using bfd, and after converting
1118 * it downloads it as an srecord using xmodem protocol.
1119 * ascii-srec - reads a ascii srecord file and downloads it
1121 * ascii-xmodem - reads a ascii file and downloads using xmodem
1125 monitor_load (file, fromtty)
1132 debuglogs (1, "Loading %s to monitor", file);
1134 if (STREQ (loadtype_str, "default")) { /* default, load a binary */
1135 gr_load_image (file, fromtty); /* by writing it into memory */
1139 /* load an srecord by converting */
1140 if ((STREQ (loadtype_str, "srec")) && STREQ (loadproto_str, "xmodem")) {
1141 monitor_load_srec(file, XMODEM);
1145 if (STREQ (loadtype_str, "srec")) { /* load an srecord by converting */
1146 monitor_load_srec(file, 0); /* if from a binary */
1150 if (STREQ (loadtype_str, "none")) { /* load an srecord by converting */
1151 error ("Unimplemented");
1155 if (STREQ (loadproto_str, "none")) { /* load an srecord file */
1156 monitor_load_ascii_srec(file, fromtty); /* if from a binary */
1160 if (STREQ (loadproto_str, "xmodem")) { /* load an srecord using the */
1161 monitor_load_srec(file, XMODEM);
1167 * monitor_load_ascii_srec -- download an ASCII srecord file.
1169 #define DOWNLOAD_LINE_SIZE 100
1171 monitor_load_ascii_srec (file, fromtty)
1176 char buf[DOWNLOAD_LINE_SIZE];
1179 debuglogs (1, "Loading an ASCII srecord file, %s.", file);
1181 download = fopen (file, "r");
1182 if (download == NULL) {
1183 error ("%s Does not exist", file);
1187 printf_monitor (LOAD_CMD);
1189 while (!feof (download)) {
1190 bytes_read = fread (buf, sizeof (char), DOWNLOAD_LINE_SIZE, download);
1195 if (SERIAL_WRITE(monitor_desc, buf, bytes_read)) {
1196 fprintf(stderr, "SERIAL_WRITE failed: (while downloading) %s\n", safe_strerror(errno));
1200 while (i++ <=200) {} ; /* Ugly HACK, probably needs flow control */
1201 if (bytes_read < DOWNLOAD_LINE_SIZE) {
1202 if (!feof (download))
1203 error ("Only read %d bytes\n", bytes_read);
1211 if (!feof (download))
1212 error ("Never got EOF while downloading");
1218 * monitor_command -- put a command string, in args, out to MONITOR.
1219 * Output from MONITOR is placed on the users terminal until the
1220 * prompt is seen. FIXME: We read the charcters ourseleves here
1221 * cause of a nasty echo.
1224 monitor_command (args, fromtty)
1233 debuglogs (1, "monitor_command (args=%s)", args);
1235 if (monitor_desc == NULL)
1236 error("monitor target not open.");
1239 error("Missing command.");
1241 printf_monitor ("%s\n", args);
1247 * monitor_load_srec -- download a binary file by converting it to srecords. This
1248 * will also use xmodem to download the resulting file.
1250 * A download goes like this when using xmodem:
1253 * <-------- (packet) [SOH|1|1|data|SUM]
1255 * <-------- (packet) [SOH|2|2|data|SUM]
1266 monitor_load_srec (args, protocol)
1274 char packet[XMODEM_PACKETSIZE];
1277 int type = 0; /* default to a type 0, header record */
1278 int srec_frame = 57; /* FIXME: this must be 57 There is 12 bytes
1279 of header, and 2 bytes of checksum at the end.
1280 The problem is an xmodem packet holds exactly
1283 abfd = bfd_openr (args, 0);
1285 printf_filtered ("Unable to open file %s\n", args);
1289 if (bfd_check_format (abfd, bfd_object) == 0) {
1290 printf_filtered ("File is not an object file\n");
1294 printf_monitor (LOAD_CMD); /* tell the monitor to load */
1295 if (protocol == XMODEM) { /* get the NAK from the target */
1297 debuglogs (3, "Got the NAK to start loading");
1299 printf_monitor ("%c", EOT);
1300 debuglogs (3, "Never got the NAK to start loading");
1301 error ("Never got the NAK to start loading");
1306 while (s != (asection *) NULL) {
1307 if (s->flags & SEC_LOAD) {
1308 char *buffer = xmalloc (srec_frame);
1309 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma, s->vma + s->_raw_size);
1311 for (i = 0; i < s->_raw_size; i += srec_frame) {
1312 if (srec_frame > s->_raw_size - i)
1313 srec_frame = s->_raw_size - i;
1315 bfd_get_section_contents (abfd, s, buffer, i, srec_frame);
1316 monitor_make_srec (srec, type, s->vma + i, buffer, srec_frame);
1317 if (protocol == XMODEM) { /* send a packet using xmodem */
1318 make_xmodem_packet (packet, srec, XMODEM_DATASIZE);
1319 write_monitor (packet, XMODEM_PACKETSIZE+1);
1321 while (retries++ <= 3) {
1322 if (GETNAK) { /* Resend packet */
1323 debuglogs (3, "Got a NAK, resending packet");
1325 write_monitor (packet, XMODEM_PACKETSIZE+1); /* send it again */
1326 if (GETACK) /* ACKnowledged, get next data chunk */
1328 } else { /* assume we got an ACK */
1330 printf_filtered ("#");
1331 debuglogs (3, "Got an ACK, sending next packet");
1335 if (retries >= 4) { /* too many tries, must be hosed */
1336 printf_monitor ("%c", EOT);
1337 error ("Never got a ACK after sending an xmodem packet");
1339 } else { /* no protocols at all */
1340 printf_monitor ("%s\n", srec);
1343 printf_filtered ("#");
1344 type = 3; /* switch to a 4 byte address record */
1349 debuglogs (3, "%s doesn't need to be loaded", s->name);
1353 printf_filtered ("\n");
1356 write a type 7 terminator record. no data for a type 7,
1357 and there is no data, so len is 0.
1359 if (protocol == XMODEM) { /* send a packet using xmodem */
1360 monitor_make_srec (srec, 7, abfd->start_address, "", 0);
1361 make_xmodem_packet (packet, srec, XMODEM_DATASIZE);
1362 write_monitor (packet, XMODEM_PACKETSIZE+1);
1364 monitor_make_srec (srec, 7, abfd->start_address, "", 0);
1365 printf_monitor ("%s\n", srec);
1367 if (protocol == XMODEM) {
1368 printf_monitor ("%c", EOT);
1370 error ("Never got ACK after sending EOT");
1380 * getacknak -- get an ACK or a NAK from the target.
1381 * returns 1 (true) or 0 (false) This is
1382 * for xmodem. ANy string starting with "***"
1383 * is an error message from the target.
1384 * Here's a few from the WinBond w89k "Cougar" PA board.
1385 * *** Too many errors found.
1387 * *** Command syntax error
1398 character = (char)readchar (0);
1399 if ((character == 0xfffffffe) || (character == 0x7f)) { /* empty uart */
1400 if (sr_get_debug() > 3)
1406 if (character == CANCEL) { /* target aborted load */
1408 error ("Got a CANCEL from the target.");
1410 if (character == '*') { /* look for missed error message */
1412 error ("Got an error message from the target");
1414 debuglogs (3, "Got a %s (0x%x or \'%c\'), expecting a %s.\n",
1415 (character == ACK) ? "ACK" : (character == NAK) ? "NAK" : "BOGUS",
1416 character, character, (byte == ACK) ? "ACK" : "NAK");
1417 if (character == byte) /* got what we wanted */
1419 if (character == ((byte == ACK) ? NAK : ACK)) { /* got the opposite */
1420 debuglogs (3, "Got the opposite, wanted 0x%x, got a 0x%x", byte, character);
1429 * monitor_make_srec -- make an srecord. This writes each line, one at a
1430 * time, each with it's own header and trailer line.
1431 * An srecord looks like this:
1433 * byte count-+ address
1434 * start ---+ | | data +- checksum
1436 * S01000006F6B692D746573742E73726563E4
1437 * S315000448600000000000000000FC00005900000000E9
1438 * S31A0004000023C1400037DE00F023604000377B009020825000348D
1439 * S30B0004485A0000000000004E
1442 * S<type><length><address><data><checksum>
1446 * is the number of bytes following upto the checksum. Note that
1447 * this is not the number of chars following, since it takes two
1448 * chars to represent a byte.
1452 * 1) two byte address data record
1453 * 2) three byte address data record
1454 * 3) four byte address data record
1455 * 7) four byte address termination record
1456 * 8) three byte address termination record
1457 * 9) two byte address termination record
1460 * is the start address of the data following, or in the case of
1461 * a termination record, the start address of the image
1465 * is the sum of all the raw byte data in the record, from the length
1466 * upwards, modulo 256 and subtracted from 255.
1469 monitor_make_srec (buffer, type, memaddr, myaddr, len)
1473 unsigned char *myaddr;
1481 debuglogs (4, "monitor_make_srec (buffer=0x%x, type=%d, memaddr=0x%x, len=%d",
1482 buffer, type, memaddr, len);
1486 create the header for the srec. 4 is the number of bytes in the address,
1487 and 1 is the number of bytes in the count.
1489 if (type == 0) /* FIXME: type 0 is optional */
1490 type = 3; /* so use data as it works */
1491 sprintf (buf, "S%d%02X%08X", type, len + 4 + 1, memaddr);
1494 checksum += (len + 4 + 1 /* calculate the checksum */
1496 + ((memaddr >> 8) & 0xff)
1497 + ((memaddr >> 16) & 0xff)
1498 + ((memaddr >> 24) & 0xff));
1500 for (i = 0; i < len; i++) { /* build the srecord */
1501 sprintf (buf, "%02X", myaddr[i]);
1502 checksum += myaddr[i];
1506 sprintf(buf, "%02X", ~checksum & 0xff); /* add the checksum */
1507 debuglogs (3, "srec is \"%s\"", buffer);
1513 * make_xmodem_packet -- this takes a 128 bytes of data and makes a packet
1516 * Each packet looks like this:
1517 * +-----+-------+-------+------+-----+
1518 * | SOH | Seq1. | Seq2. | data | SUM |
1519 * +-----+-------+-------+------+-----+
1521 * Seq1 = The sequence number.
1522 * Seq2 = The complement of the sequence number.
1523 * Data = A 128 bytes of data.
1524 * SUM = Add the contents of the 128 bytes and use the low-order
1525 * 8 bits of the result.
1528 make_xmodem_packet (packet, data, len)
1529 unsigned char packet[];
1530 unsigned char *data;
1533 static int sequence = 1;
1538 /* build the packet header */
1540 packet[1] = sequence;
1541 packet[2] = 255 - sequence;
1544 packet[2] = ~sequence++; /* the complement is the sequence checksum */
1547 sum = 0; /* calculate the data checksum */
1548 for (i = 3; i <= len + 2; i++) {
1554 for (i = len+1 ; i <= XMODEM_DATASIZE ; i++) { /* add padding for the rest of the packet */
1558 packet[XMODEM_PACKETSIZE] = sum & 0xff; /* add the checksum */
1560 if (sr_get_debug() > 4) {
1561 debuglogs (4, "The xmodem checksum is %d (0x%x)\n", sum & 0xff, sum & 0xff);
1562 print_xmodem_packet (packet);
1567 * print_xmodem_packet -- print the packet as a debug check
1570 print_xmodem_packet(packet)
1577 /* take apart the packet header the packet header */
1578 if (packet[0] == SOH) {
1581 error ("xmodem: SOH is wrong");
1584 /* check the sequence */
1585 if (packet[1] != 0) {
1586 lastseq = packet[1];
1587 if (packet[2] != ~lastseq)
1588 error ("xmodem: Sequence checksum is wrong");
1590 printf_filtered (" %d %d", lastseq, ~lastseq);
1593 /* check the data checksum */
1595 for (i = 3; i <= XMODEM_DATASIZE; i++) {
1599 /* ignore the data */
1601 printf (" [128 bytes of data] %d\n", sum & 0xff);
1603 printf_filtered (" [%s] %d\n", packet, sum & 0xff);
1605 if ((packet[XMODEM_PACKETSIZE] & 0xff) != (sum & 0xff)) {
1606 debuglogs (4, "xmodem: data checksum wrong, got a %d", packet[XMODEM_PACKETSIZE] & 0xff);
1612 * _initialize_remote_monitors -- setup a few addtitional commands that
1613 * are usually only used by monitors.
1616 _initialize_remote_monitors ()
1618 struct cmd_list_element *c;
1620 /* this sets the type of download protocol */
1621 c = add_set_cmd ("remoteloadprotocol", no_class, var_string, (char *)&loadproto_str,
1622 "Set the type of the remote load protocol.\n", &setlist);
1623 c->function.sfunc = set_loadproto_command;
1624 add_show_from_set (c, &showlist);
1625 loadproto_str = savestring ("none", 5);
1627 /* this sets the conversion type when loading */
1628 c = add_set_cmd ("remoteloadtype", no_class, var_string, (char *)&loadtype_str,
1629 "Set the type of the remote load protocol.\n", &setlist);
1630 c->function.sfunc = set_loadtype_command;
1631 add_show_from_set (c, &showlist);
1632 loadtype_str = savestring ("srec", 5);
1634 add_show_from_set (add_set_cmd ("hash", no_class, var_boolean,
1636 "Set display of activity while downloading a file.\n\
1637 When enabled, a period \'.\' is displayed.",
1641 /* generic monitor command */
1642 add_com ("monitor", class_obscure, monitor_command,
1643 "Send a command to the debug monitor.");