2011-01-07 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / dsrec.c
CommitLineData
c906108c 1/* S-record download support for GDB, the GNU debugger.
0fb0cc75 2 Copyright (C) 1995, 1996, 1997, 1999, 2000, 2001, 2003, 2004, 2007, 2008,
7b6bb8da 3 2009, 2010, 2011 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
21#include "serial.h"
22#include "srec.h"
23#include <time.h>
44160db3 24#include "gdb_assert.h"
cdcd5552 25#include "gdb_string.h"
c906108c 26
a14ed312 27extern void report_transfer_performance (unsigned long, time_t, time_t);
c906108c
SS
28
29extern int remote_debug;
30
a14ed312
KB
31static int make_srec (char *srec, CORE_ADDR targ_addr, bfd * abfd,
32 asection * sect, int sectoff, int *maxrecsize,
33 int flags);
c906108c
SS
34
35/* Download an executable by converting it to S records. DESC is a
819cc324
AC
36 `struct serial *' to send the data to. FILE is the name of the
37 file to be loaded. LOAD_OFFSET is the offset into memory to load
38 data into. It is usually specified by the user and is useful with
39 the a.out file format. MAXRECSIZE is the length in chars of the
40 largest S-record the host can accomodate. This is measured from
41 the starting `S' to the last char of the checksum. FLAGS is
42 various random flags, and HASHMARK is non-zero to cause a `#' to be
c5aa993b 43 printed out for each record loaded. WAITACK, if non-NULL, is a
819cc324
AC
44 function that waits for an acknowledgement after each S-record, and
45 returns non-zero if the ack is read correctly. */
c906108c
SS
46
47void
819cc324
AC
48load_srec (struct serial *desc, const char *file, bfd_vma load_offset,
49 int maxrecsize,
9df3df99 50 int flags, int hashmark, int (*waitack) (void))
c906108c
SS
51{
52 bfd *abfd;
53 asection *s;
54 char *srec;
55 int i;
56 int reclen;
57 time_t start_time, end_time;
58 unsigned long data_count = 0;
59
60 srec = (char *) alloca (maxrecsize + 1);
61
62 abfd = bfd_openr (file, 0);
63 if (!abfd)
64 {
a3f17187 65 printf_filtered (_("Unable to open file %s\n"), file);
c906108c
SS
66 return;
67 }
68
69 if (bfd_check_format (abfd, bfd_object) == 0)
70 {
a3f17187 71 printf_filtered (_("File is not an object file\n"));
c906108c
SS
72 return;
73 }
74
75 start_time = time (NULL);
76
77 /* Write a type 0 header record. no data for a type 0, and there
78 is no data, so len is 0. */
79
80 reclen = maxrecsize;
c5aa993b 81 make_srec (srec, 0, NULL, (asection *) 1, 0, &reclen, flags);
c906108c
SS
82 if (remote_debug)
83 {
84 srec[reclen] = '\0';
85 puts_debug ("sent -->", srec, "<--");
86 }
2cd58942 87 serial_write (desc, srec, reclen);
c906108c
SS
88
89 for (s = abfd->sections; s; s = s->next)
90 if (s->flags & SEC_LOAD)
91 {
92 int numbytes;
9a619af0 93
c906108c 94 bfd_vma addr = bfd_get_section_vma (abfd, s) + load_offset;
2c500098 95 bfd_size_type size = bfd_get_section_size (s);
c5aa993b 96 char *section_name = (char *) bfd_get_section_name (abfd, s);
d4f3574e
SS
97 /* Both GDB and BFD have mechanisms for printing addresses.
98 In the below, GDB's is used so that the address is
99 consistent with the rest of GDB. BFD's printf_vma() could
0963b4bd 100 have also been used. cagney 1999-09-01 */
5af949e3 101 printf_filtered ("%s\t: %s .. %s ",
d4f3574e 102 section_name,
5af949e3
UW
103 paddress (target_gdbarch, addr),
104 paddress (target_gdbarch, addr + size));
c906108c
SS
105 gdb_flush (gdb_stdout);
106
107 data_count += size;
108
109 for (i = 0; i < size; i += numbytes)
110 {
111 reclen = maxrecsize;
112 numbytes = make_srec (srec, (CORE_ADDR) (addr + i), abfd, s,
113 i, &reclen, flags);
114
115 if (remote_debug)
116 {
117 srec[reclen] = '\0';
118 puts_debug ("sent -->", srec, "<--");
119 }
120
121 /* Repeatedly send the S-record until a good
122 acknowledgement is sent back. */
123 do
124 {
2cd58942 125 serial_write (desc, srec, reclen);
9a4105ab
AC
126 if (deprecated_ui_load_progress_hook)
127 if (deprecated_ui_load_progress_hook (section_name,
128 (unsigned long) i))
8a3fe4f8 129 error (_("Canceled the download"));
c906108c
SS
130 }
131 while (waitack != NULL && !waitack ());
132
133 if (hashmark)
134 {
135 putchar_unfiltered ('#');
136 gdb_flush (gdb_stdout);
137 }
0963b4bd 138 } /* Per-packet (or S-record) loop. */
c906108c 139
9a4105ab
AC
140 if (deprecated_ui_load_progress_hook)
141 if (deprecated_ui_load_progress_hook (section_name,
142 (unsigned long) i))
8a3fe4f8 143 error (_("Canceled the download"));
c906108c
SS
144 putchar_unfiltered ('\n');
145 }
146
c5aa993b 147 if (hashmark)
c906108c
SS
148 putchar_unfiltered ('\n');
149
150 end_time = time (NULL);
c5aa993b 151
c906108c
SS
152 /* Write a terminator record. */
153
154 reclen = maxrecsize;
155 make_srec (srec, abfd->start_address, NULL, NULL, 0, &reclen, flags);
156
157 if (remote_debug)
158 {
159 srec[reclen] = '\0';
160 puts_debug ("sent -->", srec, "<--");
161 }
162
2cd58942 163 serial_write (desc, srec, reclen);
c906108c
SS
164
165 /* Some monitors need these to wake up properly. (Which ones? -sts) */
2cd58942 166 serial_write (desc, "\r\r", 2);
c906108c
SS
167 if (remote_debug)
168 puts_debug ("sent -->", "\r\r", "<---");
169
2cd58942 170 serial_flush_input (desc);
c906108c
SS
171
172 report_transfer_performance (data_count, start_time, end_time);
173}
174
175/*
0963b4bd 176 * make_srec -- make an srecord. This writes each line, one at a
c5aa993b
JM
177 * time, each with it's own header and trailer line.
178 * An srecord looks like this:
c906108c
SS
179 *
180 * byte count-+ address
181 * start ---+ | | data +- checksum
c5aa993b
JM
182 * | | | |
183 * S01000006F6B692D746573742E73726563E4
184 * S315000448600000000000000000FC00005900000000E9
185 * S31A0004000023C1400037DE00F023604000377B009020825000348D
186 * S30B0004485A0000000000004E
187 * S70500040000F6
c906108c 188 *
c5aa993b 189 * S<type><length><address><data><checksum>
c906108c
SS
190 *
191 * Where
192 * - length
0963b4bd
MS
193 * is the number of bytes following upto the checksum. Note
194 * that this is not the number of chars following, since it
195 * takes two chars to represent a byte.
c906108c
SS
196 * - type
197 * is one of:
198 * 0) header record
199 * 1) two byte address data record
200 * 2) three byte address data record
201 * 3) four byte address data record
202 * 7) four byte address termination record
203 * 8) three byte address termination record
204 * 9) two byte address termination record
205 *
206 * - address
207 * is the start address of the data following, or in the case of
208 * a termination record, the start address of the image
209 * - data
210 * is the data.
211 * - checksum
c5aa993b 212 * is the sum of all the raw byte data in the record, from the length
c906108c
SS
213 * upwards, modulo 256 and subtracted from 255.
214 *
215 * This routine returns the length of the S-record.
216 *
217 */
218
219static int
fba45db2
KB
220make_srec (char *srec, CORE_ADDR targ_addr, bfd *abfd, asection *sect,
221 int sectoff, int *maxrecsize, int flags)
c906108c
SS
222{
223 unsigned char checksum;
224 int tmp;
225 const static char hextab[] = "0123456789ABCDEF";
226 const static char data_code_table[] = "123";
227 const static char term_code_table[] = "987";
228 const static char header_code_table[] = "000";
c906108c
SS
229 char const *code_table;
230 int addr_size;
231 int payload_size;
232 char *binbuf;
233 char *p;
234
235 if (sect)
236 {
237 tmp = flags; /* Data or header record */
238 code_table = abfd ? data_code_table : header_code_table;
c5aa993b 239 binbuf = alloca (*maxrecsize / 2);
c906108c
SS
240 }
241 else
242 {
c5aa993b 243 tmp = flags >> SREC_TERM_SHIFT; /* Term record */
c906108c 244 code_table = term_code_table;
93d56215 245 binbuf = NULL;
c906108c
SS
246 }
247
248 if ((tmp & SREC_2_BYTE_ADDR) && (targ_addr <= 0xffff))
249 addr_size = 2;
250 else if ((tmp & SREC_3_BYTE_ADDR) && (targ_addr <= 0xffffff))
251 addr_size = 3;
252 else if (tmp & SREC_4_BYTE_ADDR)
253 addr_size = 4;
254 else
8e65ff28 255 internal_error (__FILE__, __LINE__,
5af949e3
UW
256 _("make_srec: Bad address (%s), or bad flags (0x%x)."),
257 paddress (target_gdbarch, targ_addr), flags);
c906108c
SS
258
259 /* Now that we know the address size, we can figure out how much
260 data this record can hold. */
261
262 if (sect && abfd)
263 {
264 payload_size = (*maxrecsize - (1 + 1 + 2 + addr_size * 2 + 2)) / 2;
2c500098 265 payload_size = min (payload_size, bfd_get_section_size (sect) - sectoff);
c906108c
SS
266
267 bfd_get_section_contents (abfd, sect, binbuf, sectoff, payload_size);
268 }
269 else
0963b4bd 270 payload_size = 0; /* Term or header packets have no payload. */
c906108c
SS
271
272 /* Output the header. */
44160db3
AC
273 snprintf (srec, (*maxrecsize) + 1, "S%c%02X%0*X",
274 code_table[addr_size - 2],
275 addr_size + payload_size + 1,
276 addr_size * 2, (int) targ_addr);
c906108c
SS
277
278 /* Note that the checksum is calculated on the raw data, not the
279 hexified data. It includes the length, address and the data
280 portions of the packet. */
281
282 checksum = 0;
c5aa993b 283
c906108c 284 checksum += (payload_size + addr_size + 1 /* Packet length */
0963b4bd 285 + (targ_addr & 0xff) /* Address... */
c5aa993b 286 + ((targ_addr >> 8) & 0xff)
c906108c
SS
287 + ((targ_addr >> 16) & 0xff)
288 + ((targ_addr >> 24) & 0xff));
c5aa993b 289
44160db3
AC
290 /* NOTE: cagney/2003-08-10: The equation is old. Check that the
291 recent snprintf changes match that equation. */
292 gdb_assert (strlen (srec) == 1 + 1 + 2 + addr_size * 2);
c906108c
SS
293 p = srec + 1 + 1 + 2 + addr_size * 2;
294
295 /* Build the Srecord. */
296 for (tmp = 0; tmp < payload_size; tmp++)
297 {
298 unsigned char k;
299
300 k = binbuf[tmp];
c5aa993b
JM
301 *p++ = hextab[k >> 4];
302 *p++ = hextab[k & 0xf];
c906108c
SS
303 checksum += k;
304 }
305
306 checksum = ~checksum;
307
308 *p++ = hextab[checksum >> 4];
309 *p++ = hextab[checksum & 0xf];
310 *p++ = '\r';
311
312 *maxrecsize = p - srec;
313 return payload_size;
314}
This page took 0.811199 seconds and 4 git commands to generate.