1 /* S-record download support for GDB, the GNU debugger.
2 Copyright (C) 1995-1997, 1999-2001, 2003-2004, 2007-2012 Free
3 Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>. */
24 #include "gdb_assert.h"
25 #include "gdb_string.h"
28 extern void report_transfer_performance (unsigned long, time_t, time_t);
30 extern int remote_debug;
32 static int make_srec (char *srec, CORE_ADDR targ_addr, bfd * abfd,
33 asection * sect, int sectoff, int *maxrecsize,
36 /* Download an executable by converting it to S records. DESC is a
37 `struct serial *' to send the data to. FILE is the name of the
38 file to be loaded. LOAD_OFFSET is the offset into memory to load
39 data into. It is usually specified by the user and is useful with
40 the a.out file format. MAXRECSIZE is the length in chars of the
41 largest S-record the host can accomodate. This is measured from
42 the starting `S' to the last char of the checksum. FLAGS is
43 various random flags, and HASHMARK is non-zero to cause a `#' to be
44 printed out for each record loaded. WAITACK, if non-NULL, is a
45 function that waits for an acknowledgement after each S-record, and
46 returns non-zero if the ack is read correctly. */
49 load_srec (struct serial *desc, const char *file, bfd_vma load_offset,
51 int flags, int hashmark, int (*waitack) (void))
58 time_t start_time, end_time;
59 unsigned long data_count = 0;
60 struct cleanup *cleanup;
62 srec = (char *) alloca (maxrecsize + 1);
64 abfd = gdb_bfd_open (file, NULL, -1);
67 printf_filtered (_("Unable to open file %s\n"), file);
71 cleanup = make_cleanup_bfd_unref (abfd);
72 if (bfd_check_format (abfd, bfd_object) == 0)
74 printf_filtered (_("File is not an object file\n"));
75 do_cleanups (cleanup);
79 start_time = time (NULL);
81 /* Write a type 0 header record. no data for a type 0, and there
82 is no data, so len is 0. */
85 make_srec (srec, 0, NULL, (asection *) 1, 0, &reclen, flags);
89 puts_debug ("sent -->", srec, "<--");
91 serial_write (desc, srec, reclen);
93 for (s = abfd->sections; s; s = s->next)
94 if (s->flags & SEC_LOAD)
98 bfd_vma addr = bfd_get_section_vma (abfd, s) + load_offset;
99 bfd_size_type size = bfd_get_section_size (s);
100 char *section_name = (char *) bfd_get_section_name (abfd, s);
101 /* Both GDB and BFD have mechanisms for printing addresses.
102 In the below, GDB's is used so that the address is
103 consistent with the rest of GDB. BFD's printf_vma() could
104 have also been used. cagney 1999-09-01 */
105 printf_filtered ("%s\t: %s .. %s ",
107 paddress (target_gdbarch, addr),
108 paddress (target_gdbarch, addr + size));
109 gdb_flush (gdb_stdout);
113 for (i = 0; i < size; i += numbytes)
116 numbytes = make_srec (srec, (CORE_ADDR) (addr + i), abfd, s,
122 puts_debug ("sent -->", srec, "<--");
125 /* Repeatedly send the S-record until a good
126 acknowledgement is sent back. */
129 serial_write (desc, srec, reclen);
130 if (deprecated_ui_load_progress_hook)
131 if (deprecated_ui_load_progress_hook (section_name,
133 error (_("Canceled the download"));
135 while (waitack != NULL && !waitack ());
139 putchar_unfiltered ('#');
140 gdb_flush (gdb_stdout);
142 } /* Per-packet (or S-record) loop. */
144 if (deprecated_ui_load_progress_hook)
145 if (deprecated_ui_load_progress_hook (section_name,
147 error (_("Canceled the download"));
148 putchar_unfiltered ('\n');
152 putchar_unfiltered ('\n');
154 end_time = time (NULL);
156 /* Write a terminator record. */
159 make_srec (srec, abfd->start_address, NULL, NULL, 0, &reclen, flags);
164 puts_debug ("sent -->", srec, "<--");
167 serial_write (desc, srec, reclen);
169 /* Some monitors need these to wake up properly. (Which ones? -sts) */
170 serial_write (desc, "\r\r", 2);
172 puts_debug ("sent -->", "\r\r", "<---");
174 serial_flush_input (desc);
176 report_transfer_performance (data_count, start_time, end_time);
177 do_cleanups (cleanup);
181 * make_srec -- make an srecord. This writes each line, one at a
182 * time, each with it's own header and trailer line.
183 * An srecord looks like this:
185 * byte count-+ address
186 * start ---+ | | data +- checksum
188 * S01000006F6B692D746573742E73726563E4
189 * S315000448600000000000000000FC00005900000000E9
190 * S31A0004000023C1400037DE00F023604000377B009020825000348D
191 * S30B0004485A0000000000004E
194 * S<type><length><address><data><checksum>
198 * is the number of bytes following upto the checksum. Note
199 * that this is not the number of chars following, since it
200 * takes two chars to represent a byte.
204 * 1) two byte address data record
205 * 2) three byte address data record
206 * 3) four byte address data record
207 * 7) four byte address termination record
208 * 8) three byte address termination record
209 * 9) two byte address termination record
212 * is the start address of the data following, or in the case of
213 * a termination record, the start address of the image
217 * is the sum of all the raw byte data in the record, from the length
218 * upwards, modulo 256 and subtracted from 255.
220 * This routine returns the length of the S-record.
225 make_srec (char *srec, CORE_ADDR targ_addr, bfd *abfd, asection *sect,
226 int sectoff, int *maxrecsize, int flags)
228 unsigned char checksum;
230 const static char hextab[] = "0123456789ABCDEF";
231 const static char data_code_table[] = "123";
232 const static char term_code_table[] = "987";
233 const static char header_code_table[] = "000";
234 char const *code_table;
242 tmp = flags; /* Data or header record */
243 code_table = abfd ? data_code_table : header_code_table;
244 binbuf = alloca (*maxrecsize / 2);
248 tmp = flags >> SREC_TERM_SHIFT; /* Term record */
249 code_table = term_code_table;
253 if ((tmp & SREC_2_BYTE_ADDR) && (targ_addr <= 0xffff))
255 else if ((tmp & SREC_3_BYTE_ADDR) && (targ_addr <= 0xffffff))
257 else if (tmp & SREC_4_BYTE_ADDR)
260 internal_error (__FILE__, __LINE__,
261 _("make_srec: Bad address (%s), or bad flags (0x%x)."),
262 paddress (target_gdbarch, targ_addr), flags);
264 /* Now that we know the address size, we can figure out how much
265 data this record can hold. */
269 payload_size = (*maxrecsize - (1 + 1 + 2 + addr_size * 2 + 2)) / 2;
270 payload_size = min (payload_size, bfd_get_section_size (sect) - sectoff);
272 bfd_get_section_contents (abfd, sect, binbuf, sectoff, payload_size);
275 payload_size = 0; /* Term or header packets have no payload. */
277 /* Output the header. */
278 snprintf (srec, (*maxrecsize) + 1, "S%c%02X%0*X",
279 code_table[addr_size - 2],
280 addr_size + payload_size + 1,
281 addr_size * 2, (int) targ_addr);
283 /* Note that the checksum is calculated on the raw data, not the
284 hexified data. It includes the length, address and the data
285 portions of the packet. */
289 checksum += (payload_size + addr_size + 1 /* Packet length */
290 + (targ_addr & 0xff) /* Address... */
291 + ((targ_addr >> 8) & 0xff)
292 + ((targ_addr >> 16) & 0xff)
293 + ((targ_addr >> 24) & 0xff));
295 /* NOTE: cagney/2003-08-10: The equation is old. Check that the
296 recent snprintf changes match that equation. */
297 gdb_assert (strlen (srec) == 1 + 1 + 2 + addr_size * 2);
298 p = srec + 1 + 1 + 2 + addr_size * 2;
300 /* Build the Srecord. */
301 for (tmp = 0; tmp < payload_size; tmp++)
306 *p++ = hextab[k >> 4];
307 *p++ = hextab[k & 0xf];
311 checksum = ~checksum;
313 *p++ = hextab[checksum >> 4];
314 *p++ = hextab[checksum & 0xf];
317 *maxrecsize = p - srec;