Update
[gdb.git] / gdb / dsrec.c
blob2d353e7f028576247066158a10bc01fe34892a76
1 /* S-record download support for GDB, the GNU debugger.
2 Copyright (C) 1995, 1996, 1997, 1999, 2000, 2001, 2003, 2004, 2007, 2008
3 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
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"
27 extern void report_transfer_performance (unsigned long, time_t, time_t);
29 extern int remote_debug;
31 static int make_srec (char *srec, CORE_ADDR targ_addr, bfd * abfd,
32 asection * sect, int sectoff, int *maxrecsize,
33 int flags);
35 /* Download an executable by converting it to S records. DESC is a
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
43 printed out for each record loaded. WAITACK, if non-NULL, is a
44 function that waits for an acknowledgement after each S-record, and
45 returns non-zero if the ack is read correctly. */
47 void
48 load_srec (struct serial *desc, const char *file, bfd_vma load_offset,
49 int maxrecsize,
50 int flags, int hashmark, int (*waitack) (void))
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;
60 srec = (char *) alloca (maxrecsize + 1);
62 abfd = bfd_openr (file, 0);
63 if (!abfd)
65 printf_filtered (_("Unable to open file %s\n"), file);
66 return;
69 if (bfd_check_format (abfd, bfd_object) == 0)
71 printf_filtered (_("File is not an object file\n"));
72 return;
75 start_time = time (NULL);
77 /* Write a type 0 header record. no data for a type 0, and there
78 is no data, so len is 0. */
80 reclen = maxrecsize;
81 make_srec (srec, 0, NULL, (asection *) 1, 0, &reclen, flags);
82 if (remote_debug)
84 srec[reclen] = '\0';
85 puts_debug ("sent -->", srec, "<--");
87 serial_write (desc, srec, reclen);
89 for (s = abfd->sections; s; s = s->next)
90 if (s->flags & SEC_LOAD)
92 int numbytes;
93 bfd_vma addr = bfd_get_section_vma (abfd, s) + load_offset;
94 bfd_size_type size = bfd_get_section_size (s);
95 char *section_name = (char *) bfd_get_section_name (abfd, s);
96 /* Both GDB and BFD have mechanisms for printing addresses.
97 In the below, GDB's is used so that the address is
98 consistent with the rest of GDB. BFD's printf_vma() could
99 have also been used. cagney 1999-09-01 */
100 printf_filtered ("%s\t: 0x%s .. 0x%s ",
101 section_name,
102 paddr (addr),
103 paddr (addr + size));
104 gdb_flush (gdb_stdout);
106 data_count += size;
108 for (i = 0; i < size; i += numbytes)
110 reclen = maxrecsize;
111 numbytes = make_srec (srec, (CORE_ADDR) (addr + i), abfd, s,
112 i, &reclen, flags);
114 if (remote_debug)
116 srec[reclen] = '\0';
117 puts_debug ("sent -->", srec, "<--");
120 /* Repeatedly send the S-record until a good
121 acknowledgement is sent back. */
124 serial_write (desc, srec, reclen);
125 if (deprecated_ui_load_progress_hook)
126 if (deprecated_ui_load_progress_hook (section_name,
127 (unsigned long) i))
128 error (_("Canceled the download"));
130 while (waitack != NULL && !waitack ());
132 if (hashmark)
134 putchar_unfiltered ('#');
135 gdb_flush (gdb_stdout);
137 } /* Per-packet (or S-record) loop */
139 if (deprecated_ui_load_progress_hook)
140 if (deprecated_ui_load_progress_hook (section_name,
141 (unsigned long) i))
142 error (_("Canceled the download"));
143 putchar_unfiltered ('\n');
146 if (hashmark)
147 putchar_unfiltered ('\n');
149 end_time = time (NULL);
151 /* Write a terminator record. */
153 reclen = maxrecsize;
154 make_srec (srec, abfd->start_address, NULL, NULL, 0, &reclen, flags);
156 if (remote_debug)
158 srec[reclen] = '\0';
159 puts_debug ("sent -->", srec, "<--");
162 serial_write (desc, srec, reclen);
164 /* Some monitors need these to wake up properly. (Which ones? -sts) */
165 serial_write (desc, "\r\r", 2);
166 if (remote_debug)
167 puts_debug ("sent -->", "\r\r", "<---");
169 serial_flush_input (desc);
171 report_transfer_performance (data_count, start_time, end_time);
175 * make_srec -- make an srecord. This writes each line, one at a
176 * time, each with it's own header and trailer line.
177 * An srecord looks like this:
179 * byte count-+ address
180 * start ---+ | | data +- checksum
181 * | | | |
182 * S01000006F6B692D746573742E73726563E4
183 * S315000448600000000000000000FC00005900000000E9
184 * S31A0004000023C1400037DE00F023604000377B009020825000348D
185 * S30B0004485A0000000000004E
186 * S70500040000F6
188 * S<type><length><address><data><checksum>
190 * Where
191 * - length
192 * is the number of bytes following upto the checksum. Note that
193 * this is not the number of chars following, since it takes two
194 * chars to represent a byte.
195 * - type
196 * is one of:
197 * 0) header record
198 * 1) two byte address data record
199 * 2) three byte address data record
200 * 3) four byte address data record
201 * 7) four byte address termination record
202 * 8) three byte address termination record
203 * 9) two byte address termination record
205 * - address
206 * is the start address of the data following, or in the case of
207 * a termination record, the start address of the image
208 * - data
209 * is the data.
210 * - checksum
211 * is the sum of all the raw byte data in the record, from the length
212 * upwards, modulo 256 and subtracted from 255.
214 * This routine returns the length of the S-record.
218 static int
219 make_srec (char *srec, CORE_ADDR targ_addr, bfd *abfd, asection *sect,
220 int sectoff, int *maxrecsize, int flags)
222 unsigned char checksum;
223 int tmp;
224 const static char hextab[] = "0123456789ABCDEF";
225 const static char data_code_table[] = "123";
226 const static char term_code_table[] = "987";
227 const static char header_code_table[] = "000";
228 char const *code_table;
229 int addr_size;
230 int payload_size;
231 char *binbuf;
232 char *p;
234 if (sect)
236 tmp = flags; /* Data or header record */
237 code_table = abfd ? data_code_table : header_code_table;
238 binbuf = alloca (*maxrecsize / 2);
240 else
242 tmp = flags >> SREC_TERM_SHIFT; /* Term record */
243 code_table = term_code_table;
244 binbuf = NULL;
247 if ((tmp & SREC_2_BYTE_ADDR) && (targ_addr <= 0xffff))
248 addr_size = 2;
249 else if ((tmp & SREC_3_BYTE_ADDR) && (targ_addr <= 0xffffff))
250 addr_size = 3;
251 else if (tmp & SREC_4_BYTE_ADDR)
252 addr_size = 4;
253 else
254 internal_error (__FILE__, __LINE__,
255 _("make_srec: Bad address (0x%s), or bad flags (0x%x)."),
256 paddr (targ_addr), flags);
258 /* Now that we know the address size, we can figure out how much
259 data this record can hold. */
261 if (sect && abfd)
263 payload_size = (*maxrecsize - (1 + 1 + 2 + addr_size * 2 + 2)) / 2;
264 payload_size = min (payload_size, bfd_get_section_size (sect) - sectoff);
266 bfd_get_section_contents (abfd, sect, binbuf, sectoff, payload_size);
268 else
269 payload_size = 0; /* Term or header packets have no payload */
271 /* Output the header. */
272 snprintf (srec, (*maxrecsize) + 1, "S%c%02X%0*X",
273 code_table[addr_size - 2],
274 addr_size + payload_size + 1,
275 addr_size * 2, (int) targ_addr);
277 /* Note that the checksum is calculated on the raw data, not the
278 hexified data. It includes the length, address and the data
279 portions of the packet. */
281 checksum = 0;
283 checksum += (payload_size + addr_size + 1 /* Packet length */
284 + (targ_addr & 0xff) /* Address... */
285 + ((targ_addr >> 8) & 0xff)
286 + ((targ_addr >> 16) & 0xff)
287 + ((targ_addr >> 24) & 0xff));
289 /* NOTE: cagney/2003-08-10: The equation is old. Check that the
290 recent snprintf changes match that equation. */
291 gdb_assert (strlen (srec) == 1 + 1 + 2 + addr_size * 2);
292 p = srec + 1 + 1 + 2 + addr_size * 2;
294 /* Build the Srecord. */
295 for (tmp = 0; tmp < payload_size; tmp++)
297 unsigned char k;
299 k = binbuf[tmp];
300 *p++ = hextab[k >> 4];
301 *p++ = hextab[k & 0xf];
302 checksum += k;
305 checksum = ~checksum;
307 *p++ = hextab[checksum >> 4];
308 *p++ = hextab[checksum & 0xf];
309 *p++ = '\r';
311 *maxrecsize = p - srec;
312 return payload_size;