* bfd-target.c (target_bfd_reopen): Update.
[binutils-gdb.git] / gdb / dsrec.c
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.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21 #include "serial.h"
22 #include "srec.h"
23 #include <time.h>
24 #include "gdb_assert.h"
25 #include "gdb_string.h"
26 #include "gdb_bfd.h"
27
28 extern void report_transfer_performance (unsigned long, time_t, time_t);
29
30 extern int remote_debug;
31
32 static int make_srec (char *srec, CORE_ADDR targ_addr, bfd * abfd,
33 asection * sect, int sectoff, int *maxrecsize,
34 int flags);
35
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. */
47
48 void
49 load_srec (struct serial *desc, const char *file, bfd_vma load_offset,
50 int maxrecsize,
51 int flags, int hashmark, int (*waitack) (void))
52 {
53 bfd *abfd;
54 asection *s;
55 char *srec;
56 int i;
57 int reclen;
58 time_t start_time, end_time;
59 unsigned long data_count = 0;
60 struct cleanup *cleanup;
61
62 srec = (char *) alloca (maxrecsize + 1);
63
64 abfd = bfd_openr (file, 0);
65 gdb_bfd_ref (abfd);
66 if (!abfd)
67 {
68 printf_filtered (_("Unable to open file %s\n"), file);
69 return;
70 }
71
72 cleanup = make_cleanup_bfd_unref (abfd);
73 if (bfd_check_format (abfd, bfd_object) == 0)
74 {
75 printf_filtered (_("File is not an object file\n"));
76 do_cleanups (cleanup);
77 return;
78 }
79
80 start_time = time (NULL);
81
82 /* Write a type 0 header record. no data for a type 0, and there
83 is no data, so len is 0. */
84
85 reclen = maxrecsize;
86 make_srec (srec, 0, NULL, (asection *) 1, 0, &reclen, flags);
87 if (remote_debug)
88 {
89 srec[reclen] = '\0';
90 puts_debug ("sent -->", srec, "<--");
91 }
92 serial_write (desc, srec, reclen);
93
94 for (s = abfd->sections; s; s = s->next)
95 if (s->flags & SEC_LOAD)
96 {
97 int numbytes;
98
99 bfd_vma addr = bfd_get_section_vma (abfd, s) + load_offset;
100 bfd_size_type size = bfd_get_section_size (s);
101 char *section_name = (char *) bfd_get_section_name (abfd, s);
102 /* Both GDB and BFD have mechanisms for printing addresses.
103 In the below, GDB's is used so that the address is
104 consistent with the rest of GDB. BFD's printf_vma() could
105 have also been used. cagney 1999-09-01 */
106 printf_filtered ("%s\t: %s .. %s ",
107 section_name,
108 paddress (target_gdbarch, addr),
109 paddress (target_gdbarch, addr + size));
110 gdb_flush (gdb_stdout);
111
112 data_count += size;
113
114 for (i = 0; i < size; i += numbytes)
115 {
116 reclen = maxrecsize;
117 numbytes = make_srec (srec, (CORE_ADDR) (addr + i), abfd, s,
118 i, &reclen, flags);
119
120 if (remote_debug)
121 {
122 srec[reclen] = '\0';
123 puts_debug ("sent -->", srec, "<--");
124 }
125
126 /* Repeatedly send the S-record until a good
127 acknowledgement is sent back. */
128 do
129 {
130 serial_write (desc, srec, reclen);
131 if (deprecated_ui_load_progress_hook)
132 if (deprecated_ui_load_progress_hook (section_name,
133 (unsigned long) i))
134 error (_("Canceled the download"));
135 }
136 while (waitack != NULL && !waitack ());
137
138 if (hashmark)
139 {
140 putchar_unfiltered ('#');
141 gdb_flush (gdb_stdout);
142 }
143 } /* Per-packet (or S-record) loop. */
144
145 if (deprecated_ui_load_progress_hook)
146 if (deprecated_ui_load_progress_hook (section_name,
147 (unsigned long) i))
148 error (_("Canceled the download"));
149 putchar_unfiltered ('\n');
150 }
151
152 if (hashmark)
153 putchar_unfiltered ('\n');
154
155 end_time = time (NULL);
156
157 /* Write a terminator record. */
158
159 reclen = maxrecsize;
160 make_srec (srec, abfd->start_address, NULL, NULL, 0, &reclen, flags);
161
162 if (remote_debug)
163 {
164 srec[reclen] = '\0';
165 puts_debug ("sent -->", srec, "<--");
166 }
167
168 serial_write (desc, srec, reclen);
169
170 /* Some monitors need these to wake up properly. (Which ones? -sts) */
171 serial_write (desc, "\r\r", 2);
172 if (remote_debug)
173 puts_debug ("sent -->", "\r\r", "<---");
174
175 serial_flush_input (desc);
176
177 report_transfer_performance (data_count, start_time, end_time);
178 do_cleanups (cleanup);
179 }
180
181 /*
182 * make_srec -- make an srecord. This writes each line, one at a
183 * time, each with it's own header and trailer line.
184 * An srecord looks like this:
185 *
186 * byte count-+ address
187 * start ---+ | | data +- checksum
188 * | | | |
189 * S01000006F6B692D746573742E73726563E4
190 * S315000448600000000000000000FC00005900000000E9
191 * S31A0004000023C1400037DE00F023604000377B009020825000348D
192 * S30B0004485A0000000000004E
193 * S70500040000F6
194 *
195 * S<type><length><address><data><checksum>
196 *
197 * Where
198 * - length
199 * is the number of bytes following upto the checksum. Note
200 * that this is not the number of chars following, since it
201 * takes two chars to represent a byte.
202 * - type
203 * is one of:
204 * 0) header record
205 * 1) two byte address data record
206 * 2) three byte address data record
207 * 3) four byte address data record
208 * 7) four byte address termination record
209 * 8) three byte address termination record
210 * 9) two byte address termination record
211 *
212 * - address
213 * is the start address of the data following, or in the case of
214 * a termination record, the start address of the image
215 * - data
216 * is the data.
217 * - checksum
218 * is the sum of all the raw byte data in the record, from the length
219 * upwards, modulo 256 and subtracted from 255.
220 *
221 * This routine returns the length of the S-record.
222 *
223 */
224
225 static int
226 make_srec (char *srec, CORE_ADDR targ_addr, bfd *abfd, asection *sect,
227 int sectoff, int *maxrecsize, int flags)
228 {
229 unsigned char checksum;
230 int tmp;
231 const static char hextab[] = "0123456789ABCDEF";
232 const static char data_code_table[] = "123";
233 const static char term_code_table[] = "987";
234 const static char header_code_table[] = "000";
235 char const *code_table;
236 int addr_size;
237 int payload_size;
238 char *binbuf;
239 char *p;
240
241 if (sect)
242 {
243 tmp = flags; /* Data or header record */
244 code_table = abfd ? data_code_table : header_code_table;
245 binbuf = alloca (*maxrecsize / 2);
246 }
247 else
248 {
249 tmp = flags >> SREC_TERM_SHIFT; /* Term record */
250 code_table = term_code_table;
251 binbuf = NULL;
252 }
253
254 if ((tmp & SREC_2_BYTE_ADDR) && (targ_addr <= 0xffff))
255 addr_size = 2;
256 else if ((tmp & SREC_3_BYTE_ADDR) && (targ_addr <= 0xffffff))
257 addr_size = 3;
258 else if (tmp & SREC_4_BYTE_ADDR)
259 addr_size = 4;
260 else
261 internal_error (__FILE__, __LINE__,
262 _("make_srec: Bad address (%s), or bad flags (0x%x)."),
263 paddress (target_gdbarch, targ_addr), flags);
264
265 /* Now that we know the address size, we can figure out how much
266 data this record can hold. */
267
268 if (sect && abfd)
269 {
270 payload_size = (*maxrecsize - (1 + 1 + 2 + addr_size * 2 + 2)) / 2;
271 payload_size = min (payload_size, bfd_get_section_size (sect) - sectoff);
272
273 bfd_get_section_contents (abfd, sect, binbuf, sectoff, payload_size);
274 }
275 else
276 payload_size = 0; /* Term or header packets have no payload. */
277
278 /* Output the header. */
279 snprintf (srec, (*maxrecsize) + 1, "S%c%02X%0*X",
280 code_table[addr_size - 2],
281 addr_size + payload_size + 1,
282 addr_size * 2, (int) targ_addr);
283
284 /* Note that the checksum is calculated on the raw data, not the
285 hexified data. It includes the length, address and the data
286 portions of the packet. */
287
288 checksum = 0;
289
290 checksum += (payload_size + addr_size + 1 /* Packet length */
291 + (targ_addr & 0xff) /* Address... */
292 + ((targ_addr >> 8) & 0xff)
293 + ((targ_addr >> 16) & 0xff)
294 + ((targ_addr >> 24) & 0xff));
295
296 /* NOTE: cagney/2003-08-10: The equation is old. Check that the
297 recent snprintf changes match that equation. */
298 gdb_assert (strlen (srec) == 1 + 1 + 2 + addr_size * 2);
299 p = srec + 1 + 1 + 2 + addr_size * 2;
300
301 /* Build the Srecord. */
302 for (tmp = 0; tmp < payload_size; tmp++)
303 {
304 unsigned char k;
305
306 k = binbuf[tmp];
307 *p++ = hextab[k >> 4];
308 *p++ = hextab[k & 0xf];
309 checksum += k;
310 }
311
312 checksum = ~checksum;
313
314 *p++ = hextab[checksum >> 4];
315 *p++ = hextab[checksum & 0xf];
316 *p++ = '\r';
317
318 *maxrecsize = p - srec;
319 return payload_size;
320 }