* sparc64-tdep.h (sparc64_regnum): Fix comment.
[deliverable/binutils-gdb.git] / gdb / dsrec.c
CommitLineData
c906108c 1/* S-record download support for GDB, the GNU debugger.
b6ba6518
KB
2 Copyright 1995, 1996, 1997, 1999, 2000, 2001
3 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
9 the Free Software Foundation; either version 2 of the License, or
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
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22#include "defs.h"
23#include "serial.h"
24#include "srec.h"
25#include <time.h>
44160db3 26#include "gdb_assert.h"
c906108c 27
a14ed312 28extern void report_transfer_performance (unsigned long, time_t, time_t);
c906108c
SS
29
30extern int remote_debug;
31
a14ed312
KB
32static int make_srec (char *srec, CORE_ADDR targ_addr, bfd * abfd,
33 asection * sect, int sectoff, int *maxrecsize,
34 int flags);
c906108c
SS
35
36/* Download an executable by converting it to S records. DESC is a
819cc324
AC
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
c5aa993b 44 printed out for each record loaded. WAITACK, if non-NULL, is a
819cc324
AC
45 function that waits for an acknowledgement after each S-record, and
46 returns non-zero if the ack is read correctly. */
c906108c
SS
47
48void
819cc324
AC
49load_srec (struct serial *desc, const char *file, bfd_vma load_offset,
50 int maxrecsize,
9df3df99 51 int flags, int hashmark, int (*waitack) (void))
c906108c
SS
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
61 srec = (char *) alloca (maxrecsize + 1);
62
63 abfd = bfd_openr (file, 0);
64 if (!abfd)
65 {
66 printf_filtered ("Unable to open file %s\n", file);
67 return;
68 }
69
70 if (bfd_check_format (abfd, bfd_object) == 0)
71 {
72 printf_filtered ("File is not an object file\n");
73 return;
74 }
75
76 start_time = time (NULL);
77
78 /* Write a type 0 header record. no data for a type 0, and there
79 is no data, so len is 0. */
80
81 reclen = maxrecsize;
c5aa993b 82 make_srec (srec, 0, NULL, (asection *) 1, 0, &reclen, flags);
c906108c
SS
83 if (remote_debug)
84 {
85 srec[reclen] = '\0';
86 puts_debug ("sent -->", srec, "<--");
87 }
2cd58942 88 serial_write (desc, srec, reclen);
c906108c
SS
89
90 for (s = abfd->sections; s; s = s->next)
91 if (s->flags & SEC_LOAD)
92 {
93 int numbytes;
94 bfd_vma addr = bfd_get_section_vma (abfd, s) + load_offset;
95 bfd_size_type size = bfd_get_section_size_before_reloc (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
100 have also been used. cagney 1999-09-01 */
101 printf_filtered ("%s\t: 0x%s .. 0x%s ",
102 section_name,
103 paddr (addr),
104 paddr (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);
c906108c
SS
126 if (ui_load_progress_hook)
127 if (ui_load_progress_hook (section_name, (unsigned long) i))
128 error ("Canceled the download");
129 }
130 while (waitack != NULL && !waitack ());
131
132 if (hashmark)
133 {
134 putchar_unfiltered ('#');
135 gdb_flush (gdb_stdout);
136 }
137 } /* Per-packet (or S-record) loop */
138
139 if (ui_load_progress_hook)
140 if (ui_load_progress_hook (section_name, (unsigned long) i))
141 error ("Canceled the download");
142 putchar_unfiltered ('\n');
143 }
144
c5aa993b 145 if (hashmark)
c906108c
SS
146 putchar_unfiltered ('\n');
147
148 end_time = time (NULL);
c5aa993b 149
c906108c
SS
150 /* Write a terminator record. */
151
152 reclen = maxrecsize;
153 make_srec (srec, abfd->start_address, NULL, NULL, 0, &reclen, flags);
154
155 if (remote_debug)
156 {
157 srec[reclen] = '\0';
158 puts_debug ("sent -->", srec, "<--");
159 }
160
2cd58942 161 serial_write (desc, srec, reclen);
c906108c
SS
162
163 /* Some monitors need these to wake up properly. (Which ones? -sts) */
2cd58942 164 serial_write (desc, "\r\r", 2);
c906108c
SS
165 if (remote_debug)
166 puts_debug ("sent -->", "\r\r", "<---");
167
2cd58942 168 serial_flush_input (desc);
c906108c
SS
169
170 report_transfer_performance (data_count, start_time, end_time);
171}
172
173/*
174 * make_srec -- make an srecord. This writes each line, one at a
c5aa993b
JM
175 * time, each with it's own header and trailer line.
176 * An srecord looks like this:
c906108c
SS
177 *
178 * byte count-+ address
179 * start ---+ | | data +- checksum
c5aa993b
JM
180 * | | | |
181 * S01000006F6B692D746573742E73726563E4
182 * S315000448600000000000000000FC00005900000000E9
183 * S31A0004000023C1400037DE00F023604000377B009020825000348D
184 * S30B0004485A0000000000004E
185 * S70500040000F6
c906108c 186 *
c5aa993b 187 * S<type><length><address><data><checksum>
c906108c
SS
188 *
189 * Where
190 * - length
191 * is the number of bytes following upto the checksum. Note that
192 * this is not the number of chars following, since it takes two
193 * chars to represent a byte.
194 * - type
195 * is one of:
196 * 0) header record
197 * 1) two byte address data record
198 * 2) three byte address data record
199 * 3) four byte address data record
200 * 7) four byte address termination record
201 * 8) three byte address termination record
202 * 9) two byte address termination record
203 *
204 * - address
205 * is the start address of the data following, or in the case of
206 * a termination record, the start address of the image
207 * - data
208 * is the data.
209 * - checksum
c5aa993b 210 * is the sum of all the raw byte data in the record, from the length
c906108c
SS
211 * upwards, modulo 256 and subtracted from 255.
212 *
213 * This routine returns the length of the S-record.
214 *
215 */
216
217static int
fba45db2
KB
218make_srec (char *srec, CORE_ADDR targ_addr, bfd *abfd, asection *sect,
219 int sectoff, int *maxrecsize, int flags)
c906108c
SS
220{
221 unsigned char checksum;
222 int tmp;
223 const static char hextab[] = "0123456789ABCDEF";
224 const static char data_code_table[] = "123";
225 const static char term_code_table[] = "987";
226 const static char header_code_table[] = "000";
c906108c
SS
227 char const *code_table;
228 int addr_size;
229 int payload_size;
230 char *binbuf;
231 char *p;
232
233 if (sect)
234 {
235 tmp = flags; /* Data or header record */
236 code_table = abfd ? data_code_table : header_code_table;
c5aa993b 237 binbuf = alloca (*maxrecsize / 2);
c906108c
SS
238 }
239 else
240 {
c5aa993b 241 tmp = flags >> SREC_TERM_SHIFT; /* Term record */
c906108c 242 code_table = term_code_table;
93d56215 243 binbuf = NULL;
c906108c
SS
244 }
245
246 if ((tmp & SREC_2_BYTE_ADDR) && (targ_addr <= 0xffff))
247 addr_size = 2;
248 else if ((tmp & SREC_3_BYTE_ADDR) && (targ_addr <= 0xffffff))
249 addr_size = 3;
250 else if (tmp & SREC_4_BYTE_ADDR)
251 addr_size = 4;
252 else
8e65ff28 253 internal_error (__FILE__, __LINE__,
58841d58
AC
254 "make_srec: Bad address (0x%s), or bad flags (0x%x).",
255 paddr (targ_addr), flags);
c906108c
SS
256
257 /* Now that we know the address size, we can figure out how much
258 data this record can hold. */
259
260 if (sect && abfd)
261 {
262 payload_size = (*maxrecsize - (1 + 1 + 2 + addr_size * 2 + 2)) / 2;
263 payload_size = min (payload_size, sect->_raw_size - sectoff);
264
265 bfd_get_section_contents (abfd, sect, binbuf, sectoff, payload_size);
266 }
267 else
268 payload_size = 0; /* Term or header packets have no payload */
269
270 /* Output the header. */
44160db3
AC
271 snprintf (srec, (*maxrecsize) + 1, "S%c%02X%0*X",
272 code_table[addr_size - 2],
273 addr_size + payload_size + 1,
274 addr_size * 2, (int) targ_addr);
c906108c
SS
275
276 /* Note that the checksum is calculated on the raw data, not the
277 hexified data. It includes the length, address and the data
278 portions of the packet. */
279
280 checksum = 0;
c5aa993b 281
c906108c 282 checksum += (payload_size + addr_size + 1 /* Packet length */
c5aa993b
JM
283 + (targ_addr & 0xff) /* Address... */
284 + ((targ_addr >> 8) & 0xff)
c906108c
SS
285 + ((targ_addr >> 16) & 0xff)
286 + ((targ_addr >> 24) & 0xff));
c5aa993b 287
44160db3
AC
288 /* NOTE: cagney/2003-08-10: The equation is old. Check that the
289 recent snprintf changes match that equation. */
290 gdb_assert (strlen (srec) == 1 + 1 + 2 + addr_size * 2);
c906108c
SS
291 p = srec + 1 + 1 + 2 + addr_size * 2;
292
293 /* Build the Srecord. */
294 for (tmp = 0; tmp < payload_size; tmp++)
295 {
296 unsigned char k;
297
298 k = binbuf[tmp];
c5aa993b
JM
299 *p++ = hextab[k >> 4];
300 *p++ = hextab[k & 0xf];
c906108c
SS
301 checksum += k;
302 }
303
304 checksum = ~checksum;
305
306 *p++ = hextab[checksum >> 4];
307 *p++ = hextab[checksum & 0xf];
308 *p++ = '\r';
309
310 *maxrecsize = p - srec;
311 return payload_size;
312}
This page took 0.268418 seconds and 4 git commands to generate.