gdb/dsrec.c - gdb

Functions defined

Source code

  1. /* S-record download support for GDB, the GNU debugger.
  2.    Copyright (C) 1995-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "serial.h"
  16. #include "srec.h"
  17. #include <sys/time.h>
  18. #include <time.h>
  19. #include "gdb_bfd.h"

  20. extern int remote_debug;

  21. static int make_srec (char *srec, CORE_ADDR targ_addr, bfd * abfd,
  22.                       asection * sect, int sectoff, int *maxrecsize,
  23.                       int flags);

  24. /* Download an executable by converting it to S records.  DESC is a
  25.    `struct serial *' to send the data to.  FILE is the name of the
  26.    file to be loaded.  LOAD_OFFSET is the offset into memory to load
  27.    data into.  It is usually specified by the user and is useful with
  28.    the a.out file format.  MAXRECSIZE is the length in chars of the
  29.    largest S-record the host can accomodate.  This is measured from
  30.    the starting `S' to the last char of the checksum.  FLAGS is
  31.    various random flags, and HASHMARK is non-zero to cause a `#' to be
  32.    printed out for each record loaded.  WAITACK, if non-NULL, is a
  33.    function that waits for an acknowledgement after each S-record, and
  34.    returns non-zero if the ack is read correctly.  */

  35. void
  36. load_srec (struct serial *desc, const char *file, bfd_vma load_offset,
  37.            int maxrecsize,
  38.            int flags, int hashmark, int (*waitack) (void))
  39. {
  40.   bfd *abfd;
  41.   asection *s;
  42.   char *srec;
  43.   int i;
  44.   int reclen;
  45.   struct timeval start_time, end_time;
  46.   unsigned long data_count = 0;
  47.   struct cleanup *cleanup;

  48.   srec = (char *) alloca (maxrecsize + 1);

  49.   abfd = gdb_bfd_open (file, NULL, -1);
  50.   if (!abfd)
  51.     {
  52.       printf_filtered (_("Unable to open file %s\n"), file);
  53.       return;
  54.     }

  55.   cleanup = make_cleanup_bfd_unref (abfd);
  56.   if (bfd_check_format (abfd, bfd_object) == 0)
  57.     {
  58.       printf_filtered (_("File is not an object file\n"));
  59.       do_cleanups (cleanup);
  60.       return;
  61.     }

  62.   gettimeofday (&start_time, NULL);

  63.   /* Write a type 0 header record. no data for a type 0, and there
  64.      is no data, so len is 0.  */

  65.   reclen = maxrecsize;
  66.   make_srec (srec, 0, NULL, (asection *) 1, 0, &reclen, flags);
  67.   if (remote_debug)
  68.     {
  69.       srec[reclen] = '\0';
  70.       puts_debug ("sent -->", srec, "<--");
  71.     }
  72.   serial_write (desc, srec, reclen);

  73.   for (s = abfd->sections; s; s = s->next)
  74.     if (s->flags & SEC_LOAD)
  75.       {
  76.         int numbytes;

  77.         bfd_vma addr = bfd_get_section_vma (abfd, s) + load_offset;
  78.         bfd_size_type size = bfd_get_section_size (s);
  79.         char *section_name = (char *) bfd_get_section_name (abfd, s);
  80.         /* Both GDB and BFD have mechanisms for printing addresses.
  81.            In the below, GDB's is used so that the address is
  82.            consistent with the rest of GDB.  BFD's printf_vma() could
  83.            have also been used.  cagney 1999-09-01 */
  84.         printf_filtered ("%s\t: %s .. %s  ",
  85.                          section_name,
  86.                          paddress (target_gdbarch (), addr),
  87.                          paddress (target_gdbarch (), addr + size));
  88.         gdb_flush (gdb_stdout);

  89.         data_count += size;

  90.         for (i = 0; i < size; i += numbytes)
  91.           {
  92.             reclen = maxrecsize;
  93.             numbytes = make_srec (srec, (CORE_ADDR) (addr + i), abfd, s,
  94.                                   i, &reclen, flags);

  95.             if (remote_debug)
  96.               {
  97.                 srec[reclen] = '\0';
  98.                 puts_debug ("sent -->", srec, "<--");
  99.               }

  100.             /* Repeatedly send the S-record until a good
  101.                acknowledgement is sent back.  */
  102.             do
  103.               {
  104.                 serial_write (desc, srec, reclen);
  105.                 if (deprecated_ui_load_progress_hook)
  106.                   if (deprecated_ui_load_progress_hook (section_name,
  107.                                                         (unsigned long) i))
  108.                     error (_("Canceled the download"));
  109.               }
  110.             while (waitack != NULL && !waitack ());

  111.             if (hashmark)
  112.               {
  113.                 putchar_unfiltered ('#');
  114.                 gdb_flush (gdb_stdout);
  115.               }
  116.           }                        /* Per-packet (or S-record) loop.  */

  117.         if (deprecated_ui_load_progress_hook)
  118.           if (deprecated_ui_load_progress_hook (section_name,
  119.                                                 (unsigned long) i))
  120.             error (_("Canceled the download"));
  121.         putchar_unfiltered ('\n');
  122.       }

  123.   if (hashmark)
  124.     putchar_unfiltered ('\n');

  125.   gettimeofday (&end_time, NULL);

  126.   /* Write a terminator record.  */

  127.   reclen = maxrecsize;
  128.   make_srec (srec, abfd->start_address, NULL, NULL, 0, &reclen, flags);

  129.   if (remote_debug)
  130.     {
  131.       srec[reclen] = '\0';
  132.       puts_debug ("sent -->", srec, "<--");
  133.     }

  134.   serial_write (desc, srec, reclen);

  135.   /* Some monitors need these to wake up properly.  (Which ones? -sts)  */
  136.   serial_write (desc, "\r\r", 2);
  137.   if (remote_debug)
  138.     puts_debug ("sent -->", "\r\r", "<---");

  139.   serial_flush_input (desc);

  140.   print_transfer_performance (gdb_stdout, data_count, 0,
  141.                               &start_time, &end_time);
  142.   do_cleanups (cleanup);
  143. }

  144. /*
  145. * make_srec -- make an srecord.  This writes each line, one at a
  146. *      time, each with it's own header and trailer line.
  147. *      An srecord looks like this:
  148. *
  149. * byte count-+     address
  150. * start ---+ |        |       data        +- checksum
  151. *          | |        |                   |
  152. *        S01000006F6B692D746573742E73726563E4
  153. *        S315000448600000000000000000FC00005900000000E9
  154. *        S31A0004000023C1400037DE00F023604000377B009020825000348D
  155. *        S30B0004485A0000000000004E
  156. *        S70500040000F6
  157. *
  158. *      S<type><length><address><data><checksum>
  159. *
  160. *      Where
  161. *      - length
  162. *        is the number of bytes following upto the checksum.  Note
  163. *        that this is not the number of chars following, since it
  164. *        takes two chars to represent a byte.
  165. *      - type
  166. *        is one of:
  167. *        0) header record
  168. *        1) two byte address data record
  169. *        2) three byte address data record
  170. *        3) four byte address data record
  171. *        7) four byte address termination record
  172. *        8) three byte address termination record
  173. *        9) two byte address termination record
  174. *
  175. *      - address
  176. *        is the start address of the data following, or in the case of
  177. *        a termination record, the start address of the image
  178. *      - data
  179. *        is the data.
  180. *      - checksum
  181. *        is the sum of all the raw byte data in the record, from the length
  182. *        upwards, modulo 256 and subtracted from 255.
  183. *
  184. * This routine returns the length of the S-record.
  185. *
  186. */

  187. static int
  188. make_srec (char *srec, CORE_ADDR targ_addr, bfd *abfd, asection *sect,
  189.            int sectoff, int *maxrecsize, int flags)
  190. {
  191.   unsigned char checksum;
  192.   int tmp;
  193.   static const char hextab[] = "0123456789ABCDEF";
  194.   static const char data_code_table[] = "123";
  195.   static const char term_code_table[] = "987";
  196.   static const char header_code_table[] = "000";
  197.   char const *code_table;
  198.   int addr_size;
  199.   int payload_size;
  200.   char *binbuf;
  201.   char *p;

  202.   if (sect)
  203.     {
  204.       tmp = flags;                /* Data or header record */
  205.       code_table = abfd ? data_code_table : header_code_table;
  206.       binbuf = alloca (*maxrecsize / 2);
  207.     }
  208.   else
  209.     {
  210.       tmp = flags >> SREC_TERM_SHIFT;        /* Term record */
  211.       code_table = term_code_table;
  212.       binbuf = NULL;
  213.     }

  214.   if ((tmp & SREC_2_BYTE_ADDR) && (targ_addr <= 0xffff))
  215.     addr_size = 2;
  216.   else if ((tmp & SREC_3_BYTE_ADDR) && (targ_addr <= 0xffffff))
  217.     addr_size = 3;
  218.   else if (tmp & SREC_4_BYTE_ADDR)
  219.     addr_size = 4;
  220.   else
  221.     internal_error (__FILE__, __LINE__,
  222.                     _("make_srec:  Bad address (%s), or bad flags (0x%x)."),
  223.                     paddress (target_gdbarch (), targ_addr), flags);

  224.   /* Now that we know the address size, we can figure out how much
  225.      data this record can hold.  */

  226.   if (sect && abfd)
  227.     {
  228.       payload_size = (*maxrecsize - (1 + 1 + 2 + addr_size * 2 + 2)) / 2;
  229.       payload_size = min (payload_size, bfd_get_section_size (sect) - sectoff);

  230.       bfd_get_section_contents (abfd, sect, binbuf, sectoff, payload_size);
  231.     }
  232.   else
  233.     payload_size = 0;                /* Term or header packets have no payload.  */

  234.   /* Output the header.  */
  235.   snprintf (srec, (*maxrecsize) + 1, "S%c%02X%0*X",
  236.             code_table[addr_size - 2],
  237.             addr_size + payload_size + 1,
  238.             addr_size * 2, (int) targ_addr);

  239.   /* Note that the checksum is calculated on the raw data, not the
  240.      hexified data.  It includes the length, address and the data
  241.      portions of the packet.  */

  242.   checksum = 0;

  243.   checksum += (payload_size + addr_size + 1        /* Packet length */
  244.                + (targ_addr & 0xff)                /* Address...  */
  245.                + ((targ_addr >> 8) & 0xff)
  246.                + ((targ_addr >> 16) & 0xff)
  247.                + ((targ_addr >> 24) & 0xff));

  248.   /* NOTE: cagney/2003-08-10: The equation is old.  Check that the
  249.      recent snprintf changes match that equation.  */
  250.   gdb_assert (strlen (srec) == 1 + 1 + 2 + addr_size * 2);
  251.   p = srec + 1 + 1 + 2 + addr_size * 2;

  252.   /* Build the Srecord.  */
  253.   for (tmp = 0; tmp < payload_size; tmp++)
  254.     {
  255.       unsigned char k;

  256.       k = binbuf[tmp];
  257.       *p++ = hextab[k >> 4];
  258.       *p++ = hextab[k & 0xf];
  259.       checksum += k;
  260.     }

  261.   checksum = ~checksum;

  262.   *p++ = hextab[checksum >> 4];
  263.   *p++ = hextab[checksum & 0xf];
  264.   *p++ = '\r';

  265.   *maxrecsize = p - srec;
  266.   return payload_size;
  267. }