1 /* S-record download support for GDB, the GNU debugger.
2 Copyright (C) 1995-2013 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "gdb_assert.h"
25 #include "gdb_string.h"
28 extern int remote_debug
;
30 static int make_srec (char *srec
, CORE_ADDR targ_addr
, bfd
* abfd
,
31 asection
* sect
, int sectoff
, int *maxrecsize
,
34 /* Download an executable by converting it to S records. DESC is a
35 `struct serial *' to send the data to. FILE is the name of the
36 file to be loaded. LOAD_OFFSET is the offset into memory to load
37 data into. It is usually specified by the user and is useful with
38 the a.out file format. MAXRECSIZE is the length in chars of the
39 largest S-record the host can accomodate. This is measured from
40 the starting `S' to the last char of the checksum. FLAGS is
41 various random flags, and HASHMARK is non-zero to cause a `#' to be
42 printed out for each record loaded. WAITACK, if non-NULL, is a
43 function that waits for an acknowledgement after each S-record, and
44 returns non-zero if the ack is read correctly. */
47 load_srec (struct serial
*desc
, const char *file
, bfd_vma load_offset
,
49 int flags
, int hashmark
, int (*waitack
) (void))
56 struct timeval start_time
, end_time
;
57 unsigned long data_count
= 0;
58 struct cleanup
*cleanup
;
60 srec
= (char *) alloca (maxrecsize
+ 1);
62 abfd
= gdb_bfd_open (file
, NULL
, -1);
65 printf_filtered (_("Unable to open file %s\n"), file
);
69 cleanup
= make_cleanup_bfd_unref (abfd
);
70 if (bfd_check_format (abfd
, bfd_object
) == 0)
72 printf_filtered (_("File is not an object file\n"));
73 do_cleanups (cleanup
);
77 gettimeofday (&start_time
, NULL
);
79 /* Write a type 0 header record. no data for a type 0, and there
80 is no data, so len is 0. */
83 make_srec (srec
, 0, NULL
, (asection
*) 1, 0, &reclen
, flags
);
87 puts_debug ("sent -->", srec
, "<--");
89 serial_write (desc
, srec
, reclen
);
91 for (s
= abfd
->sections
; s
; s
= s
->next
)
92 if (s
->flags
& SEC_LOAD
)
96 bfd_vma addr
= bfd_get_section_vma (abfd
, s
) + load_offset
;
97 bfd_size_type size
= bfd_get_section_size (s
);
98 char *section_name
= (char *) bfd_get_section_name (abfd
, s
);
99 /* Both GDB and BFD have mechanisms for printing addresses.
100 In the below, GDB's is used so that the address is
101 consistent with the rest of GDB. BFD's printf_vma() could
102 have also been used. cagney 1999-09-01 */
103 printf_filtered ("%s\t: %s .. %s ",
105 paddress (target_gdbarch (), addr
),
106 paddress (target_gdbarch (), addr
+ size
));
107 gdb_flush (gdb_stdout
);
111 for (i
= 0; i
< size
; i
+= numbytes
)
114 numbytes
= make_srec (srec
, (CORE_ADDR
) (addr
+ i
), abfd
, s
,
120 puts_debug ("sent -->", srec
, "<--");
123 /* Repeatedly send the S-record until a good
124 acknowledgement is sent back. */
127 serial_write (desc
, srec
, reclen
);
128 if (deprecated_ui_load_progress_hook
)
129 if (deprecated_ui_load_progress_hook (section_name
,
131 error (_("Canceled the download"));
133 while (waitack
!= NULL
&& !waitack ());
137 putchar_unfiltered ('#');
138 gdb_flush (gdb_stdout
);
140 } /* Per-packet (or S-record) loop. */
142 if (deprecated_ui_load_progress_hook
)
143 if (deprecated_ui_load_progress_hook (section_name
,
145 error (_("Canceled the download"));
146 putchar_unfiltered ('\n');
150 putchar_unfiltered ('\n');
152 gettimeofday (&end_time
, NULL
);
154 /* Write a terminator record. */
157 make_srec (srec
, abfd
->start_address
, NULL
, NULL
, 0, &reclen
, flags
);
162 puts_debug ("sent -->", srec
, "<--");
165 serial_write (desc
, srec
, reclen
);
167 /* Some monitors need these to wake up properly. (Which ones? -sts) */
168 serial_write (desc
, "\r\r", 2);
170 puts_debug ("sent -->", "\r\r", "<---");
172 serial_flush_input (desc
);
174 print_transfer_performance (gdb_stdout
, data_count
, 0,
175 &start_time
, &end_time
);
176 do_cleanups (cleanup
);
180 * make_srec -- make an srecord. This writes each line, one at a
181 * time, each with it's own header and trailer line.
182 * An srecord looks like this:
184 * byte count-+ address
185 * start ---+ | | data +- checksum
187 * S01000006F6B692D746573742E73726563E4
188 * S315000448600000000000000000FC00005900000000E9
189 * S31A0004000023C1400037DE00F023604000377B009020825000348D
190 * S30B0004485A0000000000004E
193 * S<type><length><address><data><checksum>
197 * is the number of bytes following upto the checksum. Note
198 * that this is not the number of chars following, since it
199 * takes two chars to represent a byte.
203 * 1) two byte address data record
204 * 2) three byte address data record
205 * 3) four byte address data record
206 * 7) four byte address termination record
207 * 8) three byte address termination record
208 * 9) two byte address termination record
211 * is the start address of the data following, or in the case of
212 * a termination record, the start address of the image
216 * is the sum of all the raw byte data in the record, from the length
217 * upwards, modulo 256 and subtracted from 255.
219 * This routine returns the length of the S-record.
224 make_srec (char *srec
, CORE_ADDR targ_addr
, bfd
*abfd
, asection
*sect
,
225 int sectoff
, int *maxrecsize
, int flags
)
227 unsigned char checksum
;
229 const static char hextab
[] = "0123456789ABCDEF";
230 const static char data_code_table
[] = "123";
231 const static char term_code_table
[] = "987";
232 const static char header_code_table
[] = "000";
233 char const *code_table
;
241 tmp
= flags
; /* Data or header record */
242 code_table
= abfd
? data_code_table
: header_code_table
;
243 binbuf
= alloca (*maxrecsize
/ 2);
247 tmp
= flags
>> SREC_TERM_SHIFT
; /* Term record */
248 code_table
= term_code_table
;
252 if ((tmp
& SREC_2_BYTE_ADDR
) && (targ_addr
<= 0xffff))
254 else if ((tmp
& SREC_3_BYTE_ADDR
) && (targ_addr
<= 0xffffff))
256 else if (tmp
& SREC_4_BYTE_ADDR
)
259 internal_error (__FILE__
, __LINE__
,
260 _("make_srec: Bad address (%s), or bad flags (0x%x)."),
261 paddress (target_gdbarch (), targ_addr
), flags
);
263 /* Now that we know the address size, we can figure out how much
264 data this record can hold. */
268 payload_size
= (*maxrecsize
- (1 + 1 + 2 + addr_size
* 2 + 2)) / 2;
269 payload_size
= min (payload_size
, bfd_get_section_size (sect
) - sectoff
);
271 bfd_get_section_contents (abfd
, sect
, binbuf
, sectoff
, payload_size
);
274 payload_size
= 0; /* Term or header packets have no payload. */
276 /* Output the header. */
277 snprintf (srec
, (*maxrecsize
) + 1, "S%c%02X%0*X",
278 code_table
[addr_size
- 2],
279 addr_size
+ payload_size
+ 1,
280 addr_size
* 2, (int) targ_addr
);
282 /* Note that the checksum is calculated on the raw data, not the
283 hexified data. It includes the length, address and the data
284 portions of the packet. */
288 checksum
+= (payload_size
+ addr_size
+ 1 /* Packet length */
289 + (targ_addr
& 0xff) /* Address... */
290 + ((targ_addr
>> 8) & 0xff)
291 + ((targ_addr
>> 16) & 0xff)
292 + ((targ_addr
>> 24) & 0xff));
294 /* NOTE: cagney/2003-08-10: The equation is old. Check that the
295 recent snprintf changes match that equation. */
296 gdb_assert (strlen (srec
) == 1 + 1 + 2 + addr_size
* 2);
297 p
= srec
+ 1 + 1 + 2 + addr_size
* 2;
299 /* Build the Srecord. */
300 for (tmp
= 0; tmp
< payload_size
; tmp
++)
305 *p
++ = hextab
[k
>> 4];
306 *p
++ = hextab
[k
& 0xf];
310 checksum
= ~checksum
;
312 *p
++ = hextab
[checksum
>> 4];
313 *p
++ = hextab
[checksum
& 0xf];
316 *maxrecsize
= p
- srec
;