* symtab.c (check_field): Now static. Move from...
[deliverable/binutils-gdb.git] / gdb / dsrec.c
CommitLineData
c906108c 1/* S-record download support for GDB, the GNU debugger.
0b302171
JB
2 Copyright (C) 1995-1997, 1999-2001, 2003-2004, 2007-2012 Free
3 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"
735522c2 23#include <sys/time.h>
c906108c 24#include <time.h>
44160db3 25#include "gdb_assert.h"
cdcd5552 26#include "gdb_string.h"
cbb099e8 27#include "gdb_bfd.h"
c906108c 28
c906108c
SS
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;
735522c2 57 struct timeval start_time, end_time;
c906108c 58 unsigned long data_count = 0;
d6ad71ba 59 struct cleanup *cleanup;
c906108c
SS
60
61 srec = (char *) alloca (maxrecsize + 1);
62
1c00ec6b 63 abfd = gdb_bfd_open (file, NULL, -1);
c906108c
SS
64 if (!abfd)
65 {
a3f17187 66 printf_filtered (_("Unable to open file %s\n"), file);
c906108c
SS
67 return;
68 }
69
f9a062ff 70 cleanup = make_cleanup_bfd_unref (abfd);
c906108c
SS
71 if (bfd_check_format (abfd, bfd_object) == 0)
72 {
a3f17187 73 printf_filtered (_("File is not an object file\n"));
d6ad71ba 74 do_cleanups (cleanup);
c906108c
SS
75 return;
76 }
77
735522c2 78 gettimeofday (&start_time, NULL);
c906108c
SS
79
80 /* Write a type 0 header record. no data for a type 0, and there
81 is no data, so len is 0. */
82
83 reclen = maxrecsize;
c5aa993b 84 make_srec (srec, 0, NULL, (asection *) 1, 0, &reclen, flags);
c906108c
SS
85 if (remote_debug)
86 {
87 srec[reclen] = '\0';
88 puts_debug ("sent -->", srec, "<--");
89 }
2cd58942 90 serial_write (desc, srec, reclen);
c906108c
SS
91
92 for (s = abfd->sections; s; s = s->next)
93 if (s->flags & SEC_LOAD)
94 {
95 int numbytes;
9a619af0 96
c906108c 97 bfd_vma addr = bfd_get_section_vma (abfd, s) + load_offset;
2c500098 98 bfd_size_type size = bfd_get_section_size (s);
c5aa993b 99 char *section_name = (char *) bfd_get_section_name (abfd, s);
d4f3574e
SS
100 /* Both GDB and BFD have mechanisms for printing addresses.
101 In the below, GDB's is used so that the address is
102 consistent with the rest of GDB. BFD's printf_vma() could
0963b4bd 103 have also been used. cagney 1999-09-01 */
5af949e3 104 printf_filtered ("%s\t: %s .. %s ",
d4f3574e 105 section_name,
f5656ead
TT
106 paddress (target_gdbarch (), addr),
107 paddress (target_gdbarch (), addr + size));
c906108c
SS
108 gdb_flush (gdb_stdout);
109
110 data_count += size;
111
112 for (i = 0; i < size; i += numbytes)
113 {
114 reclen = maxrecsize;
115 numbytes = make_srec (srec, (CORE_ADDR) (addr + i), abfd, s,
116 i, &reclen, flags);
117
118 if (remote_debug)
119 {
120 srec[reclen] = '\0';
121 puts_debug ("sent -->", srec, "<--");
122 }
123
124 /* Repeatedly send the S-record until a good
125 acknowledgement is sent back. */
126 do
127 {
2cd58942 128 serial_write (desc, srec, reclen);
9a4105ab
AC
129 if (deprecated_ui_load_progress_hook)
130 if (deprecated_ui_load_progress_hook (section_name,
131 (unsigned long) i))
8a3fe4f8 132 error (_("Canceled the download"));
c906108c
SS
133 }
134 while (waitack != NULL && !waitack ());
135
136 if (hashmark)
137 {
138 putchar_unfiltered ('#');
139 gdb_flush (gdb_stdout);
140 }
0963b4bd 141 } /* Per-packet (or S-record) loop. */
c906108c 142
9a4105ab
AC
143 if (deprecated_ui_load_progress_hook)
144 if (deprecated_ui_load_progress_hook (section_name,
145 (unsigned long) i))
8a3fe4f8 146 error (_("Canceled the download"));
c906108c
SS
147 putchar_unfiltered ('\n');
148 }
149
c5aa993b 150 if (hashmark)
c906108c
SS
151 putchar_unfiltered ('\n');
152
735522c2 153 gettimeofday (&end_time, NULL);
c5aa993b 154
c906108c
SS
155 /* Write a terminator record. */
156
157 reclen = maxrecsize;
158 make_srec (srec, abfd->start_address, NULL, NULL, 0, &reclen, flags);
159
160 if (remote_debug)
161 {
162 srec[reclen] = '\0';
163 puts_debug ("sent -->", srec, "<--");
164 }
165
2cd58942 166 serial_write (desc, srec, reclen);
c906108c
SS
167
168 /* Some monitors need these to wake up properly. (Which ones? -sts) */
2cd58942 169 serial_write (desc, "\r\r", 2);
c906108c
SS
170 if (remote_debug)
171 puts_debug ("sent -->", "\r\r", "<---");
172
2cd58942 173 serial_flush_input (desc);
c906108c 174
735522c2
TT
175 print_transfer_performance (gdb_stdout, data_count, 0,
176 &start_time, &end_time);
d6ad71ba 177 do_cleanups (cleanup);
c906108c
SS
178}
179
180/*
0963b4bd 181 * make_srec -- make an srecord. This writes each line, one at a
c5aa993b
JM
182 * time, each with it's own header and trailer line.
183 * An srecord looks like this:
c906108c
SS
184 *
185 * byte count-+ address
186 * start ---+ | | data +- checksum
c5aa993b
JM
187 * | | | |
188 * S01000006F6B692D746573742E73726563E4
189 * S315000448600000000000000000FC00005900000000E9
190 * S31A0004000023C1400037DE00F023604000377B009020825000348D
191 * S30B0004485A0000000000004E
192 * S70500040000F6
c906108c 193 *
c5aa993b 194 * S<type><length><address><data><checksum>
c906108c
SS
195 *
196 * Where
197 * - length
0963b4bd
MS
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.
c906108c
SS
201 * - type
202 * is one of:
203 * 0) header record
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
210 *
211 * - address
212 * is the start address of the data following, or in the case of
213 * a termination record, the start address of the image
214 * - data
215 * is the data.
216 * - checksum
c5aa993b 217 * is the sum of all the raw byte data in the record, from the length
c906108c
SS
218 * upwards, modulo 256 and subtracted from 255.
219 *
220 * This routine returns the length of the S-record.
221 *
222 */
223
224static int
fba45db2
KB
225make_srec (char *srec, CORE_ADDR targ_addr, bfd *abfd, asection *sect,
226 int sectoff, int *maxrecsize, int flags)
c906108c
SS
227{
228 unsigned char checksum;
229 int tmp;
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";
c906108c
SS
234 char const *code_table;
235 int addr_size;
236 int payload_size;
237 char *binbuf;
238 char *p;
239
240 if (sect)
241 {
242 tmp = flags; /* Data or header record */
243 code_table = abfd ? data_code_table : header_code_table;
c5aa993b 244 binbuf = alloca (*maxrecsize / 2);
c906108c
SS
245 }
246 else
247 {
c5aa993b 248 tmp = flags >> SREC_TERM_SHIFT; /* Term record */
c906108c 249 code_table = term_code_table;
93d56215 250 binbuf = NULL;
c906108c
SS
251 }
252
253 if ((tmp & SREC_2_BYTE_ADDR) && (targ_addr <= 0xffff))
254 addr_size = 2;
255 else if ((tmp & SREC_3_BYTE_ADDR) && (targ_addr <= 0xffffff))
256 addr_size = 3;
257 else if (tmp & SREC_4_BYTE_ADDR)
258 addr_size = 4;
259 else
8e65ff28 260 internal_error (__FILE__, __LINE__,
5af949e3 261 _("make_srec: Bad address (%s), or bad flags (0x%x)."),
f5656ead 262 paddress (target_gdbarch (), targ_addr), flags);
c906108c
SS
263
264 /* Now that we know the address size, we can figure out how much
265 data this record can hold. */
266
267 if (sect && abfd)
268 {
269 payload_size = (*maxrecsize - (1 + 1 + 2 + addr_size * 2 + 2)) / 2;
2c500098 270 payload_size = min (payload_size, bfd_get_section_size (sect) - sectoff);
c906108c
SS
271
272 bfd_get_section_contents (abfd, sect, binbuf, sectoff, payload_size);
273 }
274 else
0963b4bd 275 payload_size = 0; /* Term or header packets have no payload. */
c906108c
SS
276
277 /* Output the header. */
44160db3
AC
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);
c906108c
SS
282
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. */
286
287 checksum = 0;
c5aa993b 288
c906108c 289 checksum += (payload_size + addr_size + 1 /* Packet length */
0963b4bd 290 + (targ_addr & 0xff) /* Address... */
c5aa993b 291 + ((targ_addr >> 8) & 0xff)
c906108c
SS
292 + ((targ_addr >> 16) & 0xff)
293 + ((targ_addr >> 24) & 0xff));
c5aa993b 294
44160db3
AC
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);
c906108c
SS
298 p = srec + 1 + 1 + 2 + addr_size * 2;
299
300 /* Build the Srecord. */
301 for (tmp = 0; tmp < payload_size; tmp++)
302 {
303 unsigned char k;
304
305 k = binbuf[tmp];
c5aa993b
JM
306 *p++ = hextab[k >> 4];
307 *p++ = hextab[k & 0xf];
c906108c
SS
308 checksum += k;
309 }
310
311 checksum = ~checksum;
312
313 *p++ = hextab[checksum >> 4];
314 *p++ = hextab[checksum & 0xf];
315 *p++ = '\r';
316
317 *maxrecsize = p - srec;
318 return payload_size;
319}
This page took 0.957174 seconds and 4 git commands to generate.