1 /* Remote debugging interface for boot monitors, for GDB.
2 Copyright 1990, 1991, 1992, 1993, 1995 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Rob Savoye for Cygnus.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This file was derived from various remote-* modules. It is a collection
22 of generic support functions so GDB can talk directly to a ROM based
23 monitor. This saves use from having to hack an exception based handler
24 into existance, and makes for quick porting.
26 This module talks to a debug monitor called 'MONITOR', which
27 We communicate with MONITOR via either a direct serial line, or a TCP
28 (or possibly TELNET) stream to a terminal multiplexor,
29 which in turn talks to the target board. */
38 #include <sys/types.h>
45 static void monitor_command PARAMS ((char *args, int fromtty));
46 static void monitor_load_srec PARAMS ((char *args, int protocol));
47 static int getacknak PARAMS ((int byte));
49 static void make_xmodem_packet PARAMS ((unsigned char *packet,
52 static void print_xmodem_packet PARAMS ((char *packet));
54 static void monitor_load_ascii_srec PARAMS ((char *file, int fromtty));
56 static int monitor_make_srec PARAMS ((char *buffer, int type,
58 unsigned char *myaddr, int len));
60 static void monitor_fetch_register PARAMS ((int regno));
61 static void monitor_store_register PARAMS ((int regno));
63 static int from_hex PARAMS ((int a));
64 static unsigned long get_hex_word PARAMS ((void));
66 static struct monitor_ops *current_monitor;
68 static char *loadtype_str = "srec";
69 static char *loadproto_str = "none";
71 static int hashmark; /* flag set by "set hash" */
73 static int timeout = 30;
75 static int expect PARAMS ((char *string, char *buf, int buflen));
76 static int expect_prompt PARAMS ((char *buf, int buflen));
78 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
79 and i386-stub.c. Normally, no one would notice because it only matters
80 for writing large chunks of memory (e.g. in downloads). Also, this needs
81 to be more than 400 if required to hold the registers (see below, where
82 we round it up based on REGISTER_BYTES). */
86 /* Descriptor for I/O to remote machine. Initialize it to NULL so
87 that monitor_open knows that we don't have a file open when the
90 static serial_t monitor_desc = NULL;
92 /* These definitions are for xmodem protocol. */
99 #define GETACK getacknak(ACK)
100 #define GETNAK getacknak(NAK)
101 #define XMODEM_DATASIZE 128 /* the data size is ALWAYS 128 */
102 #define XMODEM_PACKETSIZE 131 /* the packet size is ALWAYS 132 (zero based) */
105 static unsigned char output_buf[0x200];
109 debug_save_output (buf, len)
114 for (; len > 0; len--)
115 output_buf[obp++ & 0x1ff] = *buf++;
117 fputs_unfiltered (buf, gdb_stdout);
121 static unsigned char input_buf[0x200];
125 debug_save_input_char (c)
129 input_buf[ibp++ & 0x1ff] = c;
131 fputc_unfiltered (c, gdb_stdout);
135 /* printf_monitor -- send data to monitor. Works just like printf. */
138 printf_monitor (va_alist)
147 pattern = va_arg (args, char *);
149 vsprintf (buf, pattern, args);
151 if (remote_debug > 0)
152 debug_save_output (buf, strlen (buf));
154 if (strlen (buf) > PBUFSIZ)
155 error ("printf_monitor(): string too long");
156 if (SERIAL_WRITE(monitor_desc, buf, strlen (buf)))
157 fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
160 /* Send raw data to monitor. */
163 write_monitor (data, len)
167 if (SERIAL_WRITE (monitor_desc, data, len))
168 fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno));
170 *(data + len + 1) = '\0';
173 /* Read a character from the remote system, doing all the fancy
182 c = SERIAL_READCHAR (monitor_desc, timeout);
184 if (remote_debug > 0)
185 debug_save_input_char (c & 0x7f);
190 if (c == SERIAL_TIMEOUT)
193 return c; /* Polls shouldn't generate timeout errors */
194 error ("Timeout reading from remote system.");
196 perror_with_name ("remote-monitor");
199 /* Scan input from the remote system, until STRING is found. If BUF is non-
200 zero, then collect input until either STRING has been collected or BUFLEN
201 chars have been collected. If input overflows BUF because STRING can't be
202 found, return -1, else return number of chars in BUF (including STRING). */
205 expect (string, buf, buflen)
211 int obuflen = buflen;
225 c = readchar (timeout);
230 c = readchar (timeout);
238 return obuflen - buflen;
250 /* Keep discarding input until we see the MONITOR prompt.
252 The convention for dealing with the prompt is that you
254 o *then* wait for the prompt.
256 Thus the last thing that a procedure does with the serial line
257 will be an expect_prompt(). Exception: monitor_resume does not
258 wait for the prompt, because the terminal is being handed over
259 to the inferior. However, the next thing which happens after that
260 is a monitor_wait which does wait for the prompt.
261 Note that this includes abnormal exit, e.g. error(). This is
262 necessary to prevent getting into states from which we can't
266 expect_prompt (buf, buflen)
270 return expect (PROMPT, buf, buflen);
273 /* Get N 32-bit words from remote, each preceded by a space, and put
274 them in registers starting at REGNO. */
284 ch = readchar (timeout);
289 for (i = 7; i >= 1; i--)
291 ch = readchar (timeout);
294 val = (val << 4) | from_hex (ch);
300 /* Open a connection to a remote debugger. NAME is the filename used
301 for communication. */
303 static char *dev_name;
304 static struct target_ops *targ_ops;
307 monitor_open (args, mon_ops, from_tty)
309 struct monitor_ops *mon_ops;
315 targ_ops = mon_ops->target;
316 name = targ_ops->to_shortname;
319 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
320 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name);
322 target_preopen (from_tty);
324 unpush_target (targ_ops);
328 dev_name = strsave (args);
330 monitor_desc = SERIAL_OPEN (dev_name);
333 perror_with_name (dev_name);
337 if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate))
339 SERIAL_CLOSE (monitor_desc);
340 perror_with_name (dev_name);
344 SERIAL_RAW (monitor_desc);
346 SERIAL_FLUSH_INPUT (monitor_desc);
348 /* some systems only work with 2 stop bits */
350 SERIAL_SETSTOPBITS (monitor_desc, mon_ops->stopbits);
352 current_monitor = mon_ops;
354 /* see if the target is alive. For a ROM monitor, we can just try to
355 force the prompt to print a few times. */
357 /* wake up the monitor and see if it's alive */
358 printf_monitor (mon_ops->init);
359 expect_prompt (NULL, 0); /* See if we get a prompt */
361 /* try again to be sure */
362 printf_monitor (mon_ops->init);
363 expect_prompt (NULL, 0); /* See if we get a prompt */
365 /* Setup the suffixes for the `set remoteloadtype' command */
367 add_show_from_set (add_set_enum_cmd ("remoteloadtype", no_class,
369 (char *)&loadtype_str,
370 "Set the remote load type.",
374 /* Setup the suffixes for the `set remoteloadprotocol' command */
376 add_show_from_set (add_set_enum_cmd ("remoteloadprotocol", no_class,
378 (char *)&loadproto_str,
379 "Set the remote load protocol.",
384 printf_unfiltered ("Remote target %s connected to %s\n", name, dev_name);
386 push_target (targ_ops);
388 inferior_pid = 42000; /* Make run command think we are busy... */
390 printf_monitor ("\r");
395 /* Close out all files and local state before this target loses
399 monitor_close (quitting)
403 SERIAL_CLOSE (monitor_desc);
407 /* Terminate the open connection to the remote debugger. Use this
408 when you want to detach and do something else with your gdb. */
411 monitor_detach (args, from_tty)
415 pop_target (); /* calls monitor_close to do the real work */
417 printf_unfiltered ("Ending remote %s debugging\n", target_shortname);
420 /* Tell the remote machine to resume. */
423 monitor_resume (pid, step, sig)
425 enum target_signal sig;
428 printf_monitor (STEP_CMD);
430 printf_monitor (CONT_CMD);
433 /* Wait until the remote machine stops, then return, storing status in
434 status just as `wait' would. */
437 monitor_wait (pid, status)
439 struct target_waitstatus *status;
441 int old_timeout = timeout;
443 status->kind = TARGET_WAITKIND_EXITED;
444 status->value.integer = 0;
446 timeout = -1; /* Don't time out -- user program is running. */
448 expect_prompt (NULL, 0); /* Wait for prompt, outputting extraneous text */
450 status->kind = TARGET_WAITKIND_STOPPED;
451 status->value.sig = TARGET_SIGNAL_TRAP;
453 timeout = old_timeout;
458 /* Fetch register REGNO, or all registers if REGNO is -1. Returns
462 monitor_fetch_register (regno)
465 unsigned LONGEST val;
466 unsigned char regbuf[MAX_REGISTER_RAW_SIZE];
472 name = REGNAMES (regno);
477 /* send the register examine command */
479 printf_monitor (current_monitor->getreg.cmd, name);
481 /* If TERM is present, we wait for that to show up. Also, (if TERM is
482 present), we will send TERM_CMD if that is present. In any case, we collect
483 all of the output into buf, and then wait for the normal prompt. */
485 if (current_monitor->getreg.term)
487 resp_len = expect (current_monitor->getreg.term, buf, sizeof buf); /* get response */
490 error ("monitor_fetch_register (%d): excessive response from monitor: %.*s.",
491 regno, resp_len, buf);
493 if (current_monitor->getreg.term_cmd)
495 SERIAL_WRITE (monitor_desc, current_monitor->getreg.term_cmd,
496 strlen (current_monitor->getreg.term_cmd));
497 expect_prompt (NULL, 0);
501 resp_len = expect_prompt (buf, sizeof buf); /* get response */
504 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
505 the register value. Otherwise, we just start searching from the start of
508 if (current_monitor->getreg.resp_delim)
510 p = strstr (buf, current_monitor->getreg.resp_delim);
512 error ("monitor_fetch_register (%d): bad response from monitor: %.*s.",
513 regno, resp_len, buf);
514 p += strlen (current_monitor->getreg.resp_delim);
519 val = strtoul (p, &p1, 16);
521 if (val == 0 && p == p1)
522 error ("monitor_fetch_register (%d): bad value from monitor: %.*s.",
523 regno, resp_len, buf);
525 /* supply register stores in target byte order, so swap here */
527 store_unsigned_integer (regbuf, REGISTER_RAW_SIZE (regno), val);
529 supply_register (regno, regbuf);
532 /* Read the remote registers into the block regs. */
535 monitor_fetch_registers (regno)
540 monitor_fetch_register (regno);
544 for (regno = 0; regno < NUM_REGS; regno++)
545 monitor_fetch_register (regno);
548 /* Store register REGNO, or all if REGNO == 0. Return errno value. */
551 monitor_store_register (regno)
555 unsigned LONGEST val;
557 name = REGNAMES (regno);
561 val = read_register (regno);
563 /* send the register deposit command */
565 printf_monitor (current_monitor->setreg.cmd, name, val);
567 /* It's possible that there are actually some monitors out there that will
568 prompt you when you set a register. In that case, you may need to add some
569 code here to deal with TERM and TERM_CMD (see monitor_fetch_register to get
570 an idea of what's needed...) */
572 expect_prompt (NULL, 0);
575 /* Store the remote registers. */
578 monitor_store_registers (regno)
583 monitor_store_register (regno);
587 for (regno = 0; regno < NUM_REGS; regno++)
588 monitor_store_register (regno);
591 /* Get ready to modify the registers array. On machines which store
592 individual registers, this doesn't need to do anything. On machines
593 which store all the registers in one fell swoop, this makes sure
594 that registers contains all the registers from the program being
598 monitor_prepare_to_store ()
600 /* Do nothing, since we can store individual regs */
604 monitor_files_info ()
606 printf_unfiltered ("\tAttached to %s at %d baud.\n", dev_name, baud_rate);
610 monitor_write_memory (memaddr, myaddr, len)
612 unsigned char *myaddr;
615 /* send the memory deposit command */
617 printf_monitor (current_monitor->setmem.cmd, memaddr, *myaddr);
619 /* It's possible that there are actually some monitors out there that will
620 prompt you when you deposit to memory. In that case, you may need to add
621 some code here to deal with TERM and TERM_CMD (see monitor_read_memory to
622 get an idea of what's needed...) */
624 expect_prompt (NULL, 0);
629 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's memory
630 at MEMADDR. Returns length moved. Currently, we only do one byte at a
634 monitor_read_memory (memaddr, myaddr, len)
639 unsigned LONGEST val;
640 unsigned char regbuf[MAX_REGISTER_RAW_SIZE];
646 /* send the memory examine command */
648 printf_monitor (current_monitor->getmem.cmd, memaddr);
650 /* If TERM is present, we wait for that to show up. Also, (if TERM is
651 present), we will send TERM_CMD if that is present. In any case, we collect
652 all of the output into buf, and then wait for the normal prompt. */
654 if (current_monitor->getmem.term)
656 resp_len = expect (current_monitor->getmem.term, buf, sizeof buf); /* get response */
659 error ("monitor_read_memory (0x%x): excessive response from monitor: %.*s.",
660 memaddr, resp_len, buf);
662 if (current_monitor->getmem.term_cmd)
664 SERIAL_WRITE (monitor_desc, current_monitor->getmem.term_cmd,
665 strlen (current_monitor->getmem.term_cmd));
666 expect_prompt (NULL, 0);
670 resp_len = expect_prompt (buf, sizeof buf); /* get response */
672 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
673 the register value. Otherwise, we just start searching from the start of
676 if (current_monitor->getmem.resp_delim)
678 p = strstr (buf, current_monitor->getmem.resp_delim);
680 error ("monitor_read_memory (0x%x): bad response from monitor: %.*s.",
681 memaddr, resp_len, buf);
682 p += strlen (current_monitor->getmem.resp_delim);
687 val = strtoul (p, &p1, 16);
689 if (val == 0 && p == p1)
690 error ("monitor_read_memory (0x%x): bad value from monitor: %.*s.", memaddr,
695 return 1; /* Got 1 byte */
698 /* FIXME-someday! merge these two. */
701 monitor_xfer_memory (memaddr, myaddr, len, write, target)
706 struct target_ops *target; /* ignored */
709 return monitor_write_memory (memaddr, myaddr, len);
711 return monitor_read_memory (memaddr, myaddr, len);
715 monitor_kill (args, from_tty)
719 return; /* ignore attempts to kill target system */
722 /* Clean up when a program exits.
723 The program actually lives on in the remote processor's RAM, and may be
724 run again without a download. Don't leave it full of breakpoint
728 monitor_mourn_inferior ()
730 unpush_target (targ_ops);
731 generic_mourn_inferior (); /* Do all the proper things now */
734 #define NUM_MONITOR_BREAKPOINTS 8
736 static CORE_ADDR breakaddr[NUM_MONITOR_BREAKPOINTS] = {0};
738 /* Tell the monitor to add a breakpoint. */
741 monitor_insert_breakpoint (addr, shadow)
746 static unsigned char break_insn[] = BREAKPOINT;
748 for (i = 0; i < NUM_MONITOR_BREAKPOINTS; i++)
750 if (breakaddr[i] == 0)
753 monitor_read_memory (addr, shadow, sizeof (break_insn));
754 printf_monitor (SET_BREAK_CMD, addr);
755 expect_prompt (NULL, 0);
760 error ("Too many breakpoints (> %d) for monitor.", NUM_MONITOR_BREAKPOINTS);
763 /* Tell the monitor to remove a breakpoint. */
766 monitor_remove_breakpoint (addr, shadow)
772 for (i = 0; i < NUM_MONITOR_BREAKPOINTS; i++)
774 if (breakaddr[i] == addr)
777 /* some monitors remove breakpoints based on the address */
779 printf_monitor(CLR_BREAK_CMD, addr);
781 printf_monitor(CLR_BREAK_CMD, i);
782 expect_prompt (NULL, 0);
786 fprintf_unfiltered (stderr, "Can't find breakpoint associated with 0x%x\n", addr);
790 /* monitor_load -- load a file. This file determines which of the
791 * supported formats to use. The current types are:
792 * FIXME: not all types supported yet.
793 * default - reads any file using bfd and writes it to memory. This
795 * srec - reads binary file using bfd and writes it as an
797 * xmodem-bin - reads a binary file using bfd, and downloads it
798 * using xmodem protocol.
799 * xmodem-srec - reads a binary file using bfd, and after converting
800 * it downloads it as an srecord using xmodem protocol.
801 * ascii-srec - reads a ascii srecord file and downloads it
803 * ascii-xmodem - reads a ascii file and downloads using xmodem
808 monitor_load (file, fromtty)
812 /* default, load a binary */
813 if (STREQ (loadtype_str, "default"))
815 error ("default load type not supported.");
818 /* load an srecord by converting */
819 if ((STREQ (loadtype_str, "srec")) && STREQ (loadproto_str, "xmodem"))
821 monitor_load_srec (file, XMODEM);
825 /* load an srecord by converting */
826 if (STREQ (loadtype_str, "srec"))
828 monitor_load_srec (file, 0); /* if from a binary */
832 /* load an srecord by converting */
833 if (STREQ (loadtype_str, "none"))
835 error ("Unimplemented");
839 /* load an srecord file */
840 if (STREQ (loadproto_str, "none"))
842 monitor_load_ascii_srec (file, fromtty); /* if from a binary */
846 if (STREQ (loadproto_str, "xmodem"))
848 monitor_load_srec (file, XMODEM);
853 /* Download an ASCII srecord file. */
855 #define DOWNLOAD_LINE_SIZE 100
858 monitor_load_ascii_srec (file, fromtty)
863 char buf[DOWNLOAD_LINE_SIZE];
866 download = fopen (file, "r");
867 if (download == NULL)
869 error ("%s does not exist", file);
873 printf_monitor (LOAD_CMD);
875 while (!feof (download))
877 bytes_read = fread (buf, sizeof (char), DOWNLOAD_LINE_SIZE, download);
880 putchar_unfiltered ('.');
881 gdb_flush (gdb_stdout);
883 if (SERIAL_WRITE (monitor_desc, buf, bytes_read))
885 fprintf_unfiltered (stderr, "SERIAL_WRITE failed: (while downloading) %s\n",
886 safe_strerror (errno));
890 while (i++ <=200) {} ; /* Ugly HACK, probably needs flow control */
891 if (bytes_read < DOWNLOAD_LINE_SIZE)
893 if (!feof (download))
894 error ("Only read %d bytes\n", bytes_read);
900 putchar_unfiltered ('\n');
902 if (!feof (download))
903 error ("Never got EOF while downloading");
904 expect_prompt (NULL, 0);
908 /* Put a command string, in args, out to MONITOR. Output from MONITOR
909 is placed on the users terminal until the prompt is seen. FIXME: We
910 read the characters ourseleves here cause of a nasty echo. */
913 monitor_command (args, fromtty)
921 if (monitor_desc == NULL)
922 error ("monitor target not open.");
924 /* Send the command. Note that if no args were supplied, then we're
925 just sending the monitor a newline, which is sometimes useful. */
927 printf_monitor ("%s\n", (args ? args : ""));
929 expect_prompt (NULL, 0);
932 /* Download a binary file by converting it to srecords. This
933 will also use xmodem to download the resulting file.
935 A download goes like this when using xmodem:
938 <-------- (packet) [SOH|1|1|data|SUM]
940 <-------- (packet) [SOH|2|2|data|SUM]
951 monitor_load_srec (args, protocol)
957 char *buffer, srec[1024];
958 char packet[XMODEM_PACKETSIZE];
961 int type = 0; /* default to a type 0, header record */
962 int srec_frame = 57; /* FIXME: this must be 57 There is 12 bytes
963 of header, and 2 bytes of checksum at the end.
964 The problem is an xmodem packet holds exactly
967 abfd = bfd_openr (args, 0);
970 printf_filtered ("Unable to open file %s\n", args);
974 if (bfd_check_format (abfd, bfd_object) == 0)
976 printf_filtered ("File is not an object file\n");
980 printf_monitor (LOAD_CMD); /* tell the monitor to load */
982 /* get the NAK from the target */
983 if (protocol == XMODEM)
987 printf_monitor ("%c", EOT);
988 error ("Never got the NAK to start loading");
993 while (s != (asection *) NULL)
995 if (s->flags & SEC_LOAD)
997 buffer = xmalloc (srec_frame);
999 printf_filtered ("%s\t: 0x%4x .. 0x%4x ",
1000 s->name, s->vma, s->vma + s->_raw_size);
1001 gdb_flush (gdb_stdout);
1002 for (i = 0; i < s->_raw_size; i += srec_frame)
1004 if (srec_frame > s->_raw_size - i)
1005 srec_frame = s->_raw_size - i;
1007 bfd_get_section_contents (abfd, s, buffer, i, srec_frame);
1008 monitor_make_srec (srec, type, s->vma + i, buffer, srec_frame);
1009 /* send a packet using xmodem */
1010 if (protocol == XMODEM)
1012 make_xmodem_packet (packet, srec, XMODEM_DATASIZE);
1013 write_monitor (packet, XMODEM_PACKETSIZE+1);
1015 while (retries++ <= 3)
1022 write_monitor (packet, XMODEM_PACKETSIZE+1);
1023 if (GETACK) /* ACKnowledged, get next data chunk */
1027 { /* assume we got an ACK */
1030 putchar_unfiltered ('#');
1031 gdb_flush (gdb_stdout);
1037 { /* too many tries, must be hosed */
1038 printf_monitor ("%c", EOT);
1039 error ("Never got a ACK after sending an xmodem packet");
1043 { /* no protocols at all */
1044 printf_monitor ("%s\n", srec);
1048 putchar_unfiltered ('#');
1049 gdb_flush (gdb_stdout);
1051 type = 3; /* switch to a 4 byte address record */
1052 gdb_flush (gdb_stdout);
1058 putchar_unfiltered ('\n');
1060 /* Write a type 7 terminator record. no data for a type 7, and there
1061 is no data, so len is 0. */
1063 if (protocol == XMODEM)
1065 /* send a packet using xmodem */
1066 monitor_make_srec (srec, 7, abfd->start_address, "", 0);
1067 make_xmodem_packet (packet, srec, XMODEM_DATASIZE);
1068 write_monitor (packet, XMODEM_PACKETSIZE+1);
1072 monitor_make_srec (srec, 7, abfd->start_address, "", 0);
1073 printf_monitor ("%s\n", srec);
1075 if (protocol == XMODEM)
1077 printf_monitor ("%c", EOT);
1079 error ("Never got ACK after sending EOT");
1083 putchar_unfiltered ('\n');
1085 expect_prompt (NULL, 0);
1087 /* Finally, make the PC point at the start address */
1089 write_register (PC_REGNUM, bfd_get_start_address (abfd));
1092 /* Get an ACK or a NAK from the target. returns 1 (true) or 0 (false)
1093 This is for xmodem. ANy string starting with "***" is an error
1094 message from the target. Here's a few from the WinBond w89k
1096 *** Too many errors found.
1098 *** Command syntax error
1111 character = (char) readchar (0);
1112 if ((character == 0xfffffffe) || (character == 0x7f))
1117 if (character == CANCEL)
1118 { /* target aborted load */
1119 expect_prompt (NULL, 0);
1120 error ("Got a CANCEL from the target.");
1122 if (character == '*')
1123 { /* look for missed error message */
1124 expect_prompt (NULL, 0);
1125 error ("Got an error message from the target");
1127 if (character == byte) /* got what we wanted */
1129 if (character == ((byte == ACK) ? NAK : ACK))
1138 * monitor_make_srec -- make an srecord. This writes each line, one at a
1139 * time, each with it's own header and trailer line.
1140 * An srecord looks like this:
1142 * byte count-+ address
1143 * start ---+ | | data +- checksum
1145 * S01000006F6B692D746573742E73726563E4
1146 * S315000448600000000000000000FC00005900000000E9
1147 * S31A0004000023C1400037DE00F023604000377B009020825000348D
1148 * S30B0004485A0000000000004E
1151 * S<type><length><address><data><checksum>
1155 * is the number of bytes following upto the checksum. Note that
1156 * this is not the number of chars following, since it takes two
1157 * chars to represent a byte.
1161 * 1) two byte address data record
1162 * 2) three byte address data record
1163 * 3) four byte address data record
1164 * 7) four byte address termination record
1165 * 8) three byte address termination record
1166 * 9) two byte address termination record
1169 * is the start address of the data following, or in the case of
1170 * a termination record, the start address of the image
1174 * is the sum of all the raw byte data in the record, from the length
1175 * upwards, modulo 256 and subtracted from 255.
1179 monitor_make_srec (buffer, type, memaddr, myaddr, len)
1183 unsigned char *myaddr;
1194 /* Create the header for the srec. 4 is the number of bytes in the address,
1195 and 1 is the number of bytes in the count. */
1197 if (type == 0) /* FIXME: type 0 is optional */
1198 type = 3; /* so use data as it works */
1199 sprintf (buf, "S%d%02X%08X", type, len + 4 + 1, memaddr);
1202 checksum += (len + 4 + 1 /* calculate the checksum */
1204 + ((memaddr >> 8) & 0xff)
1205 + ((memaddr >> 16) & 0xff)
1206 + ((memaddr >> 24) & 0xff));
1208 /* build the srecord */
1209 for (i = 0; i < len; i++)
1211 sprintf (buf, "%02X", myaddr[i]);
1212 checksum += myaddr[i];
1216 sprintf(buf, "%02X", ~checksum & 0xff); /* add the checksum */
1221 /* Take 128 bytes of data and make a packet out of it.
1223 * Each packet looks like this:
1224 * +-----+-------+-------+------+-----+
1225 * | SOH | Seq1. | Seq2. | data | SUM |
1226 * +-----+-------+-------+------+-----+
1228 * Seq1 = The sequence number.
1229 * Seq2 = The complement of the sequence number.
1230 * Data = A 128 bytes of data.
1231 * SUM = Add the contents of the 128 bytes and use the low-order
1232 * 8 bits of the result.
1236 make_xmodem_packet (packet, data, len)
1237 unsigned char *packet;
1238 unsigned char *data;
1241 static int sequence = 1;
1246 /* build the packet header */
1248 packet[1] = sequence;
1249 packet[2] = 255 - sequence;
1252 packet[2] = ~sequence++; /* the complement is the sequence checksum */
1255 sum = 0; /* calculate the data checksum */
1256 for (i = 3; i <= len + 2; i++) {
1262 /* add padding for the rest of the packet */
1263 for (i = len+1 ; i <= XMODEM_DATASIZE ; i++)
1266 packet[XMODEM_PACKETSIZE] = sum & 0xff; /* add the checksum */
1269 /* Print the packet as a debug check. */
1272 print_xmodem_packet (packet)
1279 /* take apart the packet header the packet header */
1280 if (packet[0] == SOH)
1281 printf_unfiltered ("SOH");
1283 error ("xmodem: SOH is wrong");
1285 /* check the sequence */
1288 lastseq = packet[1];
1289 if (packet[2] != ~lastseq)
1290 error ("xmodem: Sequence checksum is wrong");
1292 printf_filtered (" %d %d", lastseq, ~lastseq);
1295 /* check the data checksum */
1297 for (i = 3; i <= XMODEM_DATASIZE; i++)
1300 /* ignore the data */
1302 printf_unfiltered (" [128 bytes of data] %d\n", sum & 0xff);
1304 printf_filtered (" [%s] %d\n", packet, sum & 0xff);
1306 if ((packet[XMODEM_PACKETSIZE] & 0xff) != (sum & 0xff))
1307 printf_unfiltered ("xmodem: data checksum wrong, got a %d",
1308 packet[XMODEM_PACKETSIZE] & 0xff);
1310 putchar_unfiltered ('\n');
1313 /* Convert hex digit A to a number. */
1319 if (a >= '0' && a <= '9')
1321 if (a >= 'a' && a <= 'f')
1322 return a - 'a' + 10;
1323 if (a >= 'A' && a <= 'F')
1324 return a - 'A' + 10;
1326 error ("Reply contains invalid hex digit 0x%x", a);
1329 /* Define additional commands that are usually only used by monitors. */
1332 _initialize_remote_monitors ()
1334 add_show_from_set (add_set_cmd ("hash", no_class, var_boolean,
1336 "Set display of activity while downloading a file.\n\
1337 When enabled, a hashmark \'#\' is displayed.",
1341 add_com ("monitor", class_obscure, monitor_command,
1342 "Send a command to the debug monitor.");