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
;
112 if (STREQ (LOADTYPES
, "")) {
113 error ("No loadtype set");
117 tmp
= savestring (LOADTYPES
, strlen(LOADTYPES
));
118 type
= strtok(tmp
, ",");
119 if (STREQ (type
, (*(char **) c
->var
))) {
120 loadtype_str
= savestring (*(char **) c
->var
, strlen (*(char **) c
->var
));
124 while ((type
= strtok (NULL
, ",")) != (char *)NULL
) {
125 if (STREQ (type
, (*(char **) c
->var
)))
126 loadtype_str
= savestring (*(char **) c
->var
, strlen (*(char **) c
->var
));
130 error ("Loadtype \"%s\" does not exist.", (*(char **) c
->var
));
133 * set_loadproto_command -- set the protocol for downloading. Check to make
134 * sure you have a supported protocol for this target.
137 set_loadproto_command (ignore
, from_tty
, c
)
140 struct cmd_list_element
*c
;
144 if (STREQ (LOADPROTOS
, "")) {
145 error ("No load protocols set");
149 tmp
= savestring (LOADPROTOS
, strlen(LOADPROTOS
));
150 type
= strtok(tmp
, ",");
151 if (STREQ (type
, (*(char **) c
->var
))) {
152 loadproto_str
= savestring (*(char **) c
->var
, strlen (*(char **) c
->var
));
156 while ((type
= strtok (NULL
, ",")) != (char *)NULL
) {
157 if (STREQ (type
, (*(char **) c
->var
)))
158 loadproto_str
= savestring (*(char **) c
->var
, strlen (*(char **) c
->var
));
162 error ("Load protocol \"%s\" does not exist.", (*(char **) c
->var
));
166 * printf_monitor -- send data to monitor. Works just like printf.
169 printf_monitor(va_alist
)
179 pattern
= va_arg(args
, char *);
181 vsprintf(buf
, pattern
, args
);
183 debuglogs (1, "printf_monitor(), Sending: \"%s\".", buf
);
185 if (SERIAL_WRITE(monitor_desc
, buf
, strlen(buf
)))
186 fprintf(stderr
, "SERIAL_WRITE failed: %s\n", safe_strerror(errno
));
189 * write_monitor -- send raw data to monitor.
192 write_monitor(data
, len
)
196 if (SERIAL_WRITE(monitor_desc
, data
, len
))
197 fprintf(stderr
, "SERIAL_WRITE failed: %s\n", safe_strerror(errno
));
199 *(data
+ len
+1) = '\0';
200 debuglogs (1, "write_monitor(), Sending: \"%s\".", data
);
205 * debuglogs -- deal with debugging info to multiple sources. This takes
206 * two real args, the first one is the level to be compared against
207 * the sr_get_debug() value, the second arg is a printf buffer and args
208 * to be formatted and printed. A CR is added after each string is printed.
222 level
= va_arg(args
, int); /* get the debug level */
223 if ((level
<0) || (level
> 100)) {
224 error ("Bad argument passed to debuglogs(), needs debug level");
228 pattern
= va_arg(args
, char *); /* get the printf style pattern */
230 vsprintf(buf
, pattern
, args
); /* format the string */
232 /* convert some characters so it'll look right in the log */
234 for (i
=0 ; buf
[i
] != '\0'; i
++) {
236 case '\n': /* newlines */
240 case '\r': /* carriage returns */
244 case '\033': /* escape */
252 case '\b': /* backspace */
256 default: /* no change */
260 if (buf
[i
] < 26) { /* modify control characters */
266 *p
= '\0'; /* terminate the string */
268 if (sr_get_debug() > level
)
271 #ifdef LOG_FILE /* write to the monitor log */
272 if (log_file
!= 0x0) {
273 fputs (newbuf
, log_file
);
274 fputc ('\n', log_file
);
280 /* readchar -- read a character from the remote system, doing all the fancy
289 c
= SERIAL_READCHAR(monitor_desc
, timeout
);
291 if (sr_get_debug() > 5)
296 putc(c
& 0x7f, log_file
);
302 if (c
== SERIAL_TIMEOUT
) {
304 return c
; /* Polls shouldn't generate timeout errors */
305 error("Timeout reading from remote system.");
307 fputs ("ERROR: Timeout reading from remote system", log_file
);
310 perror_with_name("remote-monitor");
314 * expect -- scan input from the remote system, until STRING is found.
315 * If DISCARD is non-zero, then discard non-matching input, else print
316 * it out. Let the user break out immediately.
319 expect (string
, discard
)
327 debuglogs (1, "Expecting \"%s\".", string
);
331 c
= readchar(timeout
);
337 debuglogs (4, "Matched");
342 fwrite(string
, 1, (p
- 1) - string
, stdout
);
351 /* Keep discarding input until we see the MONITOR prompt.
353 The convention for dealing with the prompt is that you
355 o *then* wait for the prompt.
357 Thus the last thing that a procedure does with the serial line
358 will be an expect_prompt(). Exception: monitor_resume does not
359 wait for the prompt, because the terminal is being handed over
360 to the inferior. However, the next thing which happens after that
361 is a monitor_wait which does wait for the prompt.
362 Note that this includes abnormal exit, e.g. error(). This is
363 necessary to prevent getting into states from which we can't
366 expect_prompt(discard
)
369 expect (PROMPT
, discard
);
373 * junk -- ignore junk characters. Returns a 1 if junk, 0 otherwise
386 if (sr_get_debug() > 5)
387 debuglogs (5, "Ignoring \'%c\'.", ch
);
390 if (sr_get_debug() > 5)
391 debuglogs (5, "Accepting \'%c\'.", ch
);
397 * get_hex_digit -- Get a hex digit from the remote system & return its value.
398 * If ignore is nonzero, ignore spaces, newline & tabs.
401 get_hex_digit(ignore
)
406 ch
= readchar(timeout
);
409 if (sr_get_debug() > 4)
410 debuglogs (4, "get_hex_digit() got a 0x%x(%c)", ch
, ch
);
412 if (ch
>= '0' && ch
<= '9')
414 else if (ch
>= 'A' && ch
<= 'F')
415 return ch
- 'A' + 10;
416 else if (ch
>= 'a' && ch
<= 'f')
417 return ch
- 'a' + 10;
418 else if (ch
== ' ' && ignore
)
422 error("Invalid hex digit from remote system. (0x%x)", ch
);
427 /* get_hex_byte -- Get a byte from monitor and put it in *BYT.
428 * Accept any number leading spaces.
436 val
= get_hex_digit (1) << 4;
437 debuglogs (4, "get_hex_digit() -- Read first nibble 0x%x", val
);
439 val
|= get_hex_digit (0);
440 debuglogs (4, "get_hex_digit() -- Read second nibble 0x%x", val
);
443 debuglogs (4, "get_hex_digit() -- Read a 0x%x", val
);
447 * get_hex_word -- Get N 32-bit words from remote, each preceded by a space,
448 * and put them in registers starting at REGNO.
457 for (i
= 0; i
< 8; i
++)
458 val
= (val
<< 4) + get_hex_digit (i
== 0);
460 debuglogs (4, "get_hex_word() got a 0x%x.", val
);
465 /* This is called not only when we first attach, but also when the
466 user types "run" after having attached. */
468 monitor_create_inferior (execfile
, args
, env
)
476 error("Can't pass arguments to remote MONITOR process");
478 if (execfile
== 0 || exec_bfd
== 0)
479 error("No exec file specified");
481 entry_pt
= (int) bfd_get_start_address (exec_bfd
);
483 debuglogs (1, "create_inferior(exexfile=%s, args=%s, env=%s)", execfile
, args
, env
);
485 /* The "process" (board) is already stopped awaiting our commands, and
486 the program is already downloaded. We just set its PC and go. */
488 clear_proceed_status ();
490 /* Tell wait_for_inferior that we've started a new process. */
491 init_wait_for_inferior ();
493 /* Set up the "saved terminal modes" of the inferior
494 based on what modes we are starting it with. */
495 target_terminal_init ();
497 /* Install inferior's terminal modes. */
498 target_terminal_inferior ();
500 /* insert_step_breakpoint (); FIXME, do we need this? */
503 proceed ((CORE_ADDR
)entry_pt
, TARGET_SIGNAL_DEFAULT
, 0);
507 * monitor_open -- open a connection to a remote debugger.
508 * NAME is the filename used for communication.
510 static int baudrate
= 9600;
511 static char dev_name
[100];
514 monitor_open(args
, name
, from_tty
)
521 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
522 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name
, name
);
527 strcpy(dev_name
, args
);
528 monitor_desc
= SERIAL_OPEN(dev_name
);
530 if (monitor_desc
== NULL
)
531 perror_with_name(dev_name
);
533 if (baud_rate
!= -1) {
534 if (SERIAL_SETBAUDRATE (monitor_desc
, baud_rate
)) {
535 SERIAL_CLOSE (monitor_desc
);
536 perror_with_name (name
);
540 SERIAL_RAW(monitor_desc
);
542 #if defined (LOG_FILE)
543 log_file
= fopen (LOG_FILE
, "w");
544 if (log_file
== NULL
)
545 perror_with_name (LOG_FILE
);
546 fprintf_filtered (log_file
, "GDB %s (%s", version
, host_name
);
547 fprintf_filtered (log_file
, " --target %s)\n", target_name
);
548 fprintf_filtered (log_file
, "Remote target %s connected to %s\n\n", TARGET_NAME
, dev_name
);
551 /* wake up the monitor and see if it's alive */
552 printf_monitor(INIT_CMD
);
553 expect_prompt(1); /* See if we get a prompt */
555 /* try again to be sure */
556 printf_monitor(INIT_CMD
);
557 expect_prompt(1); /* See if we get a prompt */
560 printf("Remote target %s connected to %s\n", TARGET_NAME
, dev_name
);
564 * monitor_close -- Close out all files and local state before this
565 * target loses control.
569 monitor_close (quitting
)
572 SERIAL_CLOSE(monitor_desc
);
575 debuglogs (1, "monitor_close (quitting=%d)", quitting
);
577 #if defined (LOG_FILE)
579 if (ferror(log_file
))
580 fprintf(stderr
, "Error writing log file.\n");
581 if (fclose(log_file
) != 0)
582 fprintf(stderr
, "Error closing log file.\n");
588 * monitor_detach -- terminate the open connection to the remote
589 * debugger. Use this when you want to detach and do something
590 * else with your gdb.
593 monitor_detach (from_tty
)
597 debuglogs (1, "monitor_detach ()");
599 pop_target(); /* calls monitor_close to do the real work */
601 printf ("Ending remote %s debugging\n", target_shortname
);
605 * monitor_attach -- attach GDB to the target.
608 monitor_attach (args
, from_tty
)
613 printf ("Starting remote %s debugging\n", target_shortname
);
615 debuglogs (1, "monitor_attach (args=%s)", args
);
617 printf_monitor (GO_CMD
);
618 /* swallow the echo. */
623 * monitor_resume -- Tell the remote machine to resume.
626 monitor_resume (pid
, step
, sig
)
628 enum target_signal sig
;
630 debuglogs (1, "monitor_resume (step=%d, sig=%d)", step
, sig
);
633 printf_monitor (STEP_CMD
);
635 printf_monitor (CONT_CMD
);
640 * monitor_wait -- Wait until the remote machine stops, then return,
641 * storing status in status just as `wait' would.
644 monitor_wait (pid
, status
)
646 struct target_waitstatus
*status
;
648 int old_timeout
= timeout
;
650 debuglogs(1, "monitor_wait (), printing extraneous text.");
652 status
->kind
= TARGET_WAITKIND_EXITED
;
653 status
->value
.integer
= 0;
655 timeout
= 0; /* Don't time out -- user program is running. */
657 expect_prompt(0); /* Wait for prompt, outputting extraneous text */
658 debuglogs (4, "monitor_wait(), got the prompt.");
660 status
->kind
= TARGET_WAITKIND_STOPPED
;
661 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
663 timeout
= old_timeout
;
668 /* Return the name of register number regno in the form input and output by
669 monitor. Currently, register_names just happens to contain exactly what
670 monitor wants. Lets take advantage of that just as long as possible! */
685 for (p
= REGNAMES(regno
); *p
; p
++)
690 debuglogs (5, "Got name \"%s\" from regno #%d.", buf
, regno
);
696 * monitor_fetch_registers -- read the remote registers into the
700 monitor_fetch_registers ()
704 /* yeah yeah, i know this is horribly inefficient. but it isn't done
705 very often... i'll clean it up later. */
707 for (regno
= 0; regno
<= PC_REGNUM
; regno
++)
708 monitor_fetch_register(regno
);
712 * monitor_fetch_register -- fetch register REGNO, or all registers if REGNO
713 * is -1. Returns errno value.
716 monitor_fetch_register (regno
)
721 debuglogs (1, "monitor_fetch_register (reg=%s)", get_reg_name (regno
));
724 monitor_fetch_registers ();
726 char *name
= get_reg_name (regno
);
729 printf_monitor (ROMCMD(GET_REG
), name
); /* send the command */
730 expect (name
, 1); /* then strip the leading garbage */
731 if (*ROMDELIM(GET_REG
) != 0) { /* if there's a delimiter */
732 expect (ROMDELIM(GET_REG
), 1);
735 val
= get_hex_word(); /* get the value, ignore junk */
736 supply_register (regno
, (char *) &val
);
738 if (*ROMDELIM(GET_REG
) != 0) {
739 /*** expect (ROMRES(GET_REG)); ***/
740 printf_monitor (CMD_END
);
747 /* Store the remote registers from the contents of the block REGS. */
750 monitor_store_registers ()
754 debuglogs (1, "monitor_store_registers()");
756 for (regno
= 0; regno
<= PC_REGNUM
; regno
++)
757 monitor_store_register(regno
);
759 registers_changed ();
763 * monitor_store_register -- store register REGNO, or all if REGNO == 0.
764 * return errno value.
767 monitor_store_register (regno
)
773 i
= read_register(regno
);
775 debuglogs (1, "monitor_store_register (regno=%d)", regno
);
778 monitor_store_registers ();
780 debuglogs (3, "Setting register %s to 0x%x", get_reg_name (regno
), read_register (regno
));
782 name
= get_reg_name (regno
);
785 printf_monitor (ROMCMD(SET_REG
), name
, read_register(regno
));
786 expect (name
, 1); /* strip the leading garbage */
787 if (*ROMDELIM(SET_REG
) != 0) { /* if there's a delimiter */
788 expect (ROMDELIM(SET_REG
), 1);
790 printf_monitor ("%d%s\n", i
, CMD_END
);
797 printf_monitor (SET_REG
, get_reg_name (regno
),
798 read_register (regno
));
804 /* Get ready to modify the registers array. On machines which store
805 individual registers, this doesn't need to do anything. On machines
806 which store all the registers in one fell swoop, this makes sure
807 that registers contains all the registers from the program being
811 monitor_prepare_to_store ()
813 /* Do nothing, since we can store individual regs */
817 monitor_files_info ()
819 printf ("\tAttached to %s at %d baud.\n",
824 * monitor_write_inferior_memory -- Copy LEN bytes of data from debugger
825 * memory at MYADDR to inferior's memory at MEMADDR. Returns length moved.
828 monitor_write_inferior_memory (memaddr
, myaddr
, len
)
830 unsigned char *myaddr
;
836 debuglogs (1, "monitor_write_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr
, myaddr
, len
);
838 for (i
= 0; i
< len
; i
++) {
839 printf_monitor (ROMCMD(SET_MEM
), memaddr
+ i
, myaddr
[i
] );
840 if (*ROMDELIM(SET_MEM
) != 0) { /* if there's a delimiter */
841 expect (ROMDELIM(SET_MEM
), 1);
843 printf_monitor ("%x", myaddr
[i
]);
845 /*** printf_monitor ("%x", myaddr[i]); ***/
846 if (sr_get_debug() > 1)
847 printf ("\nSet 0x%x to 0x%x\n", memaddr
+ i
, myaddr
[i
]);
848 if (*ROMDELIM(SET_MEM
) != 0) {
850 printf_monitor (CMD_END
);
858 * monitor_read_inferior_memory -- read LEN bytes from inferior memory
859 * at MEMADDR. Put the result at debugger address MYADDR. Returns
863 monitor_read_inferior_memory(memaddr
, myaddr
, len
)
871 /* Number of bytes read so far. */
874 /* Starting address of this pass. */
875 unsigned long startaddr
;
877 /* Number of bytes to read in this pass. */
880 debuglogs (1, "monitor_read_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr
, myaddr
, len
);
882 /* Note that this code works correctly if startaddr is just less
883 than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
884 thing). That is, something like
885 monitor_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
886 works--it never adds len To memaddr and gets 0. */
887 /* However, something like
888 monitor_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
889 doesn't need to work. Detect it and give up if there's an attempt
891 if (((memaddr
- 1) + len
) < memaddr
) {
898 while (count
< len
) {
900 if ((startaddr
% 16) != 0)
901 len_this_pass
-= startaddr
% 16;
902 if (len_this_pass
> (len
- count
))
903 len_this_pass
= (len
- count
);
905 debuglogs (3, "Display %d bytes at %x", len_this_pass
, startaddr
);
907 for (i
= 0; i
< len_this_pass
; i
++) {
908 printf_monitor (ROMCMD(GET_MEM
), startaddr
, startaddr
);
909 sprintf (buf
, ROMCMD(GET_MEM
), startaddr
, startaddr
);
910 if (*ROMDELIM(GET_MEM
) != 0) { /* if there's a delimiter */
911 expect (ROMDELIM(GET_MEM
), 1);
913 sprintf (buf
, ROMCMD(GET_MEM
), startaddr
, startaddr
);
914 expect (buf
,1); /* get the command echo */
915 get_hex_word(1); /* strip away the address */
917 get_hex_byte (&myaddr
[count
++]); /* get the value at this address */
919 if (*ROMDELIM(GET_MEM
) != 0) {
920 printf_monitor (CMD_END
);
929 /* FIXME-someday! merge these two. */
931 monitor_xfer_inferior_memory (memaddr
, myaddr
, len
, write
, target
)
936 struct target_ops
*target
; /* ignored */
939 return monitor_write_inferior_memory (memaddr
, myaddr
, len
);
941 return monitor_read_inferior_memory (memaddr
, myaddr
, len
);
945 monitor_kill (args
, from_tty
)
949 return; /* ignore attempts to kill target system */
952 /* Clean up when a program exits.
953 The program actually lives on in the remote processor's RAM, and may be
954 run again without a download. Don't leave it full of breakpoint
958 monitor_mourn_inferior ()
960 remove_breakpoints ();
961 generic_mourn_inferior (); /* Do all the proper things now */
964 #define MAX_MONITOR_BREAKPOINTS 16
966 extern int memory_breakpoint_size
;
967 static CORE_ADDR breakaddr
[MAX_MONITOR_BREAKPOINTS
] = {0};
970 * monitor_insert_breakpoint -- add a breakpoint
973 monitor_insert_breakpoint (addr
, shadow
)
979 debuglogs (1, "monitor_insert_breakpoint() addr = 0x%x", addr
);
981 for (i
= 0; i
<= MAX_MONITOR_BREAKPOINTS
; i
++) {
982 if (breakaddr
[i
] == 0) {
984 if (sr_get_debug() > 4)
985 printf ("Breakpoint at %x\n", addr
);
986 monitor_read_inferior_memory(addr
, shadow
, memory_breakpoint_size
);
987 printf_monitor(SET_BREAK_CMD
, addr
);
993 fprintf(stderr
, "Too many breakpoints (> 16) for monitor\n");
998 * _remove_breakpoint -- Tell the monitor to remove a breakpoint
1001 monitor_remove_breakpoint (addr
, shadow
)
1007 debuglogs (1, "monitor_remove_breakpoint() addr = 0x%x", addr
);
1009 for (i
= 0; i
< MAX_MONITOR_BREAKPOINTS
; i
++) {
1010 if (breakaddr
[i
] == addr
) {
1012 /* some monitors remove breakpoints based on the address */
1014 printf_monitor(CLR_BREAK_CMD
, addr
);
1016 printf_monitor(CLR_BREAK_CMD
, i
);
1021 fprintf(stderr
, "Can't find breakpoint associated with 0x%x\n", addr
);
1025 /* monitor_load -- load a file. This file determines which of the
1026 * supported formats to use. The current types are:
1027 * FIXME: not all types supported yet.
1028 * default - reads any file using bfd and writes it to memory. This
1030 * srec - reads binary file using bfd and writes it as an
1032 * xmodem-bin - reads a binary file using bfd, and downloads it
1033 * using xmodem protocol.
1034 * xmodem-srec - reads a binary file using bfd, and after converting
1035 * it downloads it as an srecord using xmodem protocol.
1036 * ascii-srec - reads a ascii srecord file and downloads it
1038 * ascii-xmodem - reads a ascii file and downloads using xmodem
1042 monitor_load (file
, fromtty
)
1049 debuglogs (1, "Loading %s to monitor", file
);
1051 if (STREQ (loadtype_str
, "default")) { /* default, load a binary */
1052 gr_load_image (file
, fromtty
); /* by writing it into memory */
1055 if (STREQ (loadtype_str
, "srec")) { /* load an srecord by converting */
1056 monitor_load_srec(file
, 0); /* if from a binary */
1059 if (STREQ (loadtype_str
, "ascii-srec")) { /* load an srecord file */
1060 monitor_load_ascii_srec(file
, fromtty
); /* if from a binary */
1063 if (STREQ (loadtype_str
, "xmodem-srec")) { /* load an srecord using the */
1064 monitor_load_srec(file
, XMODEM
);
1069 * monitor_load_ascii_srec -- download an ASCII srecord file.
1071 #define DOWNLOAD_LINE_SIZE 100
1073 monitor_load_ascii_srec (file
, fromtty
)
1078 char buf
[DOWNLOAD_LINE_SIZE
];
1081 debuglogs (1, "Loading an ASCII srecord file, %s.", file
);
1083 download
= fopen (file
, "r");
1084 if (download
== NULL
) {
1085 error ("%s Does not exist", file
);
1089 printf_monitor (LOAD_CMD
);
1091 while (!feof (download
)) {
1092 bytes_read
= fread (buf
, sizeof (char), DOWNLOAD_LINE_SIZE
, download
);
1097 if (SERIAL_WRITE(monitor_desc
, buf
, bytes_read
)) {
1098 fprintf(stderr
, "SERIAL_WRITE failed: (while downloading) %s\n", safe_strerror(errno
));
1102 while (i
++ <=200) {} ; /* Ugly HACK, probably needs flow control */
1103 if (bytes_read
< DOWNLOAD_LINE_SIZE
) {
1104 if (!feof (download
))
1105 error ("Only read %d bytes\n", bytes_read
);
1113 if (!feof (download
))
1114 error ("Never got EOF while downloading");
1120 * monitor_command -- put a command string, in args, out to MONITOR.
1121 * Output from MONITOR is placed on the users terminal until the
1122 * prompt is seen. FIXME: We read the charcters ourseleves here
1123 * cause of a nasty echo.
1126 monitor_command (args
, fromtty
)
1135 debuglogs (1, "monitor_command (args=%s)", args
);
1137 if (monitor_desc
== NULL
)
1138 error("monitor target not open.");
1141 error("Missing command.");
1143 printf_monitor ("%s\n", args
);
1149 * monitor_load_srec -- download a binary file by converting it to srecords. This
1150 * will also use xmodem to download the resulting file.
1152 * A download goes like this when using xmodem:
1155 * <-------- (packet) [SOH|1|1|data|SUM]
1157 * <-------- (packet) [SOH|2|2|data|SUM]
1168 monitor_load_srec (args
, protocol
)
1176 char packet
[XMODEM_PACKETSIZE
];
1179 int type
= 0; /* default to a type 0, header record */
1180 int srec_frame
= 57; /* FIXME: this must be 57 There is 12 bytes
1181 of header, and 2 bytes of checksum at the end.
1182 The problem is an xmodem packet holds exactly
1185 abfd
= bfd_openr (args
, 0);
1187 printf_filtered ("Unable to open file %s\n", args
);
1191 if (bfd_check_format (abfd
, bfd_object
) == 0) {
1192 printf_filtered ("File is not an object file\n");
1196 printf_monitor (LOAD_CMD
); /* tell the monitor to load */
1197 if (protocol
== XMODEM
) { /* get the NAK from the target */
1199 debuglogs (3, "Got the NAK to start loading");
1201 printf_monitor ("%c", EOT
);
1202 debuglogs (3, "Never got the NAK to start loading");
1203 error ("Never got the NAK to start loading");
1208 while (s
!= (asection
*) NULL
) {
1209 if (s
->flags
& SEC_LOAD
) {
1210 char *buffer
= xmalloc (srec_frame
);
1211 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s
->name
, s
->vma
, s
->vma
+ s
->_raw_size
);
1213 for (i
= 0; i
< s
->_raw_size
; i
+= srec_frame
) {
1214 if (srec_frame
> s
->_raw_size
- i
)
1215 srec_frame
= s
->_raw_size
- i
;
1217 bfd_get_section_contents (abfd
, s
, buffer
, i
, srec_frame
);
1218 monitor_make_srec (srec
, type
, s
->vma
+ i
, buffer
, srec_frame
);
1219 if (protocol
== XMODEM
) { /* send a packet using xmodem */
1220 make_xmodem_packet (packet
, srec
, XMODEM_DATASIZE
);
1221 write_monitor (packet
, XMODEM_PACKETSIZE
+1);
1223 while (retries
++ <= 3) {
1224 if (GETNAK
) { /* Resend packet */
1225 debuglogs (3, "Got a NAK, resending packet");
1227 write_monitor (packet
, XMODEM_PACKETSIZE
+1); /* send it again */
1228 if (GETACK
) /* ACKnowledged, get next data chunk */
1232 if (retries
>= 4) { /* too many tries, must be hosed */
1233 printf_monitor ("%c", EOT
);
1234 error ("Never got a ACK after sending an xmodem packet");
1236 } else { /* no protocols at all */
1237 printf_monitor ("%s\n", srec
);
1240 printf_filtered ("#");
1241 type
= 3; /* switch to a 4 byte address record */
1244 printf_filtered ("\n");
1247 debuglogs (3, "%s doesn't need to be loaded", s
->name
);
1253 write a type 7 terminator record. no data for a type 7,
1254 and there is no data, so len is 0.
1256 monitor_make_srec (srec
, 7, abfd
->start_address
, "", 0);
1257 printf_monitor ("%s\n", srec
);
1258 if (protocol
== XMODEM
) {
1259 printf_monitor ("%c", EOT
);
1261 error ("Never got ACK after sending EOT");
1271 * getacknak -- get an ACK or a NAK from the target.
1272 * returns 1 (true) or 0 (false) This is
1273 * for xmodem. ANy string starting with "***"
1274 * is an error message from the target.
1275 * Here's a few from the WinBond w89k "Cougar" PA board.
1276 * *** Too many errors found.
1278 * *** Command syntax error
1289 character
= (char)readchar (0);
1290 if ((character
== 0xfffffffe) || (character
== 0x7f)) { /* empty uart */
1291 if (sr_get_debug() > 3)
1297 if (character
== CANCEL
) { /* target aborted load */
1299 error ("Got a CANCEL from the target.");
1301 if (character
== '*') { /* look for missed error message */
1303 error ("Got an error message from the target");
1305 debuglogs (3, "Got a %s (0x%x or \'%c\'), expecting a %s.\n",
1306 (character
== ACK
) ? "ACK" : (character
== NAK
) ? "NAK" : "BOGUS",
1307 character
, character
, (byte
== ACK
) ? "ACK" : "NAK");
1308 if (character
== byte
) /* got what we wanted */
1310 if (character
== ((byte
== ACK
) ? NAK
: ACK
)) { /* got the opposite */
1311 debuglogs (3, "Got the opposite, wanted 0x%x, got a 0x%x", byte
, character
);
1320 * monitor_make_srec -- make an srecord. This writes each line, one at a
1321 * time, each with it's own header and trailer line.
1322 * An srecord looks like this:
1324 * byte count-+ address
1325 * start ---+ | | data +- checksum
1327 * S01000006F6B692D746573742E73726563E4
1328 * S315000448600000000000000000FC00005900000000E9
1329 * S31A0004000023C1400037DE00F023604000377B009020825000348D
1330 * S30B0004485A0000000000004E
1333 * S<type><length><address><data><checksum>
1337 * is the number of bytes following upto the checksum. Note that
1338 * this is not the number of chars following, since it takes two
1339 * chars to represent a byte.
1343 * 1) two byte address data record
1344 * 2) three byte address data record
1345 * 3) four byte address data record
1346 * 7) four byte address termination record
1347 * 8) three byte address termination record
1348 * 9) two byte address termination record
1351 * is the start address of the data following, or in the case of
1352 * a termination record, the start address of the image
1356 * is the sum of all the raw byte data in the record, from the length
1357 * upwards, modulo 256 and subtracted from 255.
1360 monitor_make_srec (buffer
, type
, memaddr
, myaddr
, len
)
1364 unsigned char *myaddr
;
1372 debuglogs (4, "monitor_make_srec (buffer=0x%x, type=%d, memaddr=0x%x, len=%d",
1373 buffer
, type
, memaddr
, len
);
1377 create the header for the srec. 4 is the number of bytes in the address,
1378 and 1 is the number of bytes in the count.
1380 if (type
== 0) /* FIXME: type 0 is optional */
1381 type
= 3; /* so use data as it works */
1382 sprintf (buf
, "S%d%02X%08X", type
, len
+ 4 + 1, memaddr
);
1385 checksum
+= (len
+ 4 + 1 /* calculate the checksum */
1387 + ((memaddr
>> 8) & 0xff)
1388 + ((memaddr
>> 16) & 0xff)
1389 + ((memaddr
>> 24) & 0xff));
1391 for (i
= 0; i
< len
; i
++) { /* build the srecord */
1392 sprintf (buf
, "%02X", myaddr
[i
]);
1393 checksum
+= myaddr
[i
];
1397 sprintf(buf
, "%02X", ~checksum
& 0xff); /* add the checksum */
1398 debuglogs (3, "srec is \"%s\"", buffer
);
1404 * make_xmodem_packet -- this takes a 128 bytes of data and makes a packet
1407 * Each packet looks like this:
1408 * +-----+-------+-------+------+-----+
1409 * | SOH | Seq1. | Seq2. | data | SUM |
1410 * +-----+-------+-------+------+-----+
1412 * Seq1 = The sequence number.
1413 * Seq2 = The complement of the sequence number.
1414 * Data = A 128 bytes of data.
1415 * SUM = Add the contents of the 128 bytes and use the low-order
1416 * 8 bits of the result.
1419 make_xmodem_packet (packet
, data
, len
)
1420 unsigned char packet
[];
1421 unsigned char *data
;
1424 static int sequence
= 1;
1429 /* build the packet header */
1431 packet
[1] = sequence
;
1432 packet
[2] = 255 - sequence
;
1435 packet
[2] = ~sequence
++; /* the complement is the sequence checksum */
1438 sum
= 0; /* calculate the data checksum */
1439 for (i
= 3; i
<= len
+ 2; i
++) {
1445 for (i
= len
+1 ; i
<= XMODEM_DATASIZE
; i
++) { /* add padding for the rest of the packet */
1449 packet
[XMODEM_PACKETSIZE
] = sum
& 0xff; /* add the checksum */
1451 if (sr_get_debug() > 4)
1452 debuglogs (4, "The xmodem checksum is %d (0x%x)\n", sum
& 0xff, sum
& 0xff);
1453 print_xmodem_packet (packet
);
1457 * print_xmodem_packet -- print the packet as a debug check
1460 print_xmodem_packet(packet
)
1467 /* take apart the packet header the packet header */
1468 if (packet
[0] == SOH
) {
1471 error ("xmodem: SOH is wrong");
1474 /* check the sequence */
1475 if (packet
[1] != 0) {
1476 lastseq
= packet
[1];
1477 if (packet
[2] != ~lastseq
)
1478 error ("xmodem: Sequence checksum is wrong");
1480 printf_filtered (" %d %d", lastseq
, ~lastseq
);
1483 /* check the data checksum */
1485 for (i
= 3; i
<= XMODEM_DATASIZE
; i
++) {
1489 /* ignore the data */
1491 printf (" [128 bytes of data] %d\n", sum
& 0xff);
1493 printf_filtered (" [%s] %d\n", packet
, sum
& 0xff);
1495 if ((packet
[XMODEM_PACKETSIZE
] & 0xff) != (sum
& 0xff)) {
1496 debuglogs (4, "xmodem: data checksum wrong, got a %d", packet
[XMODEM_PACKETSIZE
] & 0xff);
1502 * _initialize_remote_monitors -- setup a few addtitional commands that
1503 * are usually only used by monitors.
1506 _initialize_remote_monitors ()
1508 struct cmd_list_element
*c
;
1510 /* this sets the type of download protocol */
1511 c
= add_set_cmd ("remoteloadprotocol", no_class
, var_string
, (char *)&loadproto_str
,
1512 "Set the type of the remote load protocol.\n", &setlist
);
1513 c
->function
.sfunc
= set_loadproto_command
;
1514 add_show_from_set (c
, &showlist
);
1515 loadproto_str
= savestring ("none", 5);
1517 /* this sets the conversion type when loading */
1518 c
= add_set_cmd ("remoteloadtype", no_class
, var_string
, (char *)&loadtype_str
,
1519 "Set the type of the remote load protocol.\n", &setlist
);
1520 c
->function
.sfunc
= set_loadtype_command
;
1521 add_show_from_set (c
, &showlist
);
1522 loadtype_str
= savestring ("srec", 5);
1524 add_show_from_set (add_set_cmd ("hash", no_class
, var_boolean
,
1526 "Set display of activity while downloading a file.\n\
1527 When enabled, a period \'.\' is displayed.",
1531 /* generic monitor command */
1532 add_com ("monitor", class_obscure
, monitor_command
,
1533 "Send a command to the debug monitor.");
This page took 0.099741 seconds and 5 git commands to generate.