]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/dsrec.c
import gdb-1999-09-08 snapshot
[thirdparty/binutils-gdb.git] / gdb / dsrec.c
CommitLineData
c906108c
SS
1/* S-record download support for GDB, the GNU debugger.
2 Copyright 1995, 1996, 1997 Free Software Foundation, Inc.
3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
c906108c 10
c5aa993b
JM
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
c906108c 15
c5aa993b
JM
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
c906108c
SS
20
21#include "defs.h"
22#include "serial.h"
23#include "srec.h"
24#include <time.h>
25
26int (*ui_load_progress_hook) PARAMS ((char *, unsigned long));
27extern void report_transfer_performance PARAMS ((unsigned long, time_t, time_t));
28
29extern int remote_debug;
30
c5aa993b
JM
31static int make_srec PARAMS ((char *srec, CORE_ADDR targ_addr, bfd * abfd,
32 asection * sect, int sectoff, int *maxrecsize,
c906108c
SS
33 int flags));
34
35/* Download an executable by converting it to S records. DESC is a
c5aa993b
JM
36 serial_t to send the data to. FILE is the name of the file to be
37 loaded. LOAD_OFFSET is the offset into memory to load data into.
38 It is usually specified by the user and is useful with the a.out
39 file format. MAXRECSIZE is the length in chars of the largest
40 S-record the host can accomodate. This is measured from the
41 starting `S' to the last char of the checksum. FLAGS is various
42 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,
45 and returns non-zero if the ack is read correctly. */
c906108c
SS
46
47void
48load_srec (desc, file, load_offset, maxrecsize, flags, hashmark, waitack)
49 serial_t desc;
50 const char *file;
51 bfd_vma load_offset;
52 int maxrecsize;
53 int flags;
54 int hashmark;
55 int (*waitack) PARAMS ((void));
56{
57 bfd *abfd;
58 asection *s;
59 char *srec;
60 int i;
61 int reclen;
62 time_t start_time, end_time;
63 unsigned long data_count = 0;
64
65 srec = (char *) alloca (maxrecsize + 1);
66
67 abfd = bfd_openr (file, 0);
68 if (!abfd)
69 {
70 printf_filtered ("Unable to open file %s\n", file);
71 return;
72 }
73
74 if (bfd_check_format (abfd, bfd_object) == 0)
75 {
76 printf_filtered ("File is not an object file\n");
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;
c5aa993b 86 make_srec (srec, 0, NULL, (asection *) 1, 0, &reclen, flags);
c906108c
SS
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 bfd_vma addr = bfd_get_section_vma (abfd, s) + load_offset;
99 bfd_size_type size = bfd_get_section_size_before_reloc (s);
c5aa993b 100 char *section_name = (char *) bfd_get_section_name (abfd, s);
d4f3574e
SS
101 /* Both GDB and BFD have mechanisms for printing addresses.
102 In the below, GDB's is used so that the address is
103 consistent with the rest of GDB. BFD's printf_vma() could
104 have also been used. cagney 1999-09-01 */
105 printf_filtered ("%s\t: 0x%s .. 0x%s ",
106 section_name,
107 paddr (addr),
108 paddr (addr + size));
c906108c
SS
109 gdb_flush (gdb_stdout);
110
111 data_count += size;
112
113 for (i = 0; i < size; i += numbytes)
114 {
115 reclen = maxrecsize;
116 numbytes = make_srec (srec, (CORE_ADDR) (addr + i), abfd, s,
117 i, &reclen, flags);
118
119 if (remote_debug)
120 {
121 srec[reclen] = '\0';
122 puts_debug ("sent -->", srec, "<--");
123 }
124
125 /* Repeatedly send the S-record until a good
126 acknowledgement is sent back. */
127 do
128 {
c5aa993b 129 SERIAL_WRITE (desc, srec, reclen);
c906108c
SS
130 if (ui_load_progress_hook)
131 if (ui_load_progress_hook (section_name, (unsigned long) i))
132 error ("Canceled the download");
133 }
134 while (waitack != NULL && !waitack ());
135
136 if (hashmark)
137 {
138 putchar_unfiltered ('#');
139 gdb_flush (gdb_stdout);
140 }
141 } /* Per-packet (or S-record) loop */
142
143 if (ui_load_progress_hook)
144 if (ui_load_progress_hook (section_name, (unsigned long) i))
145 error ("Canceled the download");
146 putchar_unfiltered ('\n');
147 }
148
c5aa993b 149 if (hashmark)
c906108c
SS
150 putchar_unfiltered ('\n');
151
152 end_time = time (NULL);
c5aa993b 153
c906108c
SS
154 /* Write a terminator record. */
155
156 reclen = maxrecsize;
157 make_srec (srec, abfd->start_address, NULL, NULL, 0, &reclen, flags);
158
159 if (remote_debug)
160 {
161 srec[reclen] = '\0';
162 puts_debug ("sent -->", srec, "<--");
163 }
164
165 SERIAL_WRITE (desc, srec, reclen);
166
167 /* Some monitors need these to wake up properly. (Which ones? -sts) */
168 SERIAL_WRITE (desc, "\r\r", 2);
169 if (remote_debug)
170 puts_debug ("sent -->", "\r\r", "<---");
171
172 SERIAL_FLUSH_INPUT (desc);
173
174 report_transfer_performance (data_count, start_time, end_time);
175}
176
177/*
178 * make_srec -- make an srecord. This writes each line, one at a
c5aa993b
JM
179 * time, each with it's own header and trailer line.
180 * An srecord looks like this:
c906108c
SS
181 *
182 * byte count-+ address
183 * start ---+ | | data +- checksum
c5aa993b
JM
184 * | | | |
185 * S01000006F6B692D746573742E73726563E4
186 * S315000448600000000000000000FC00005900000000E9
187 * S31A0004000023C1400037DE00F023604000377B009020825000348D
188 * S30B0004485A0000000000004E
189 * S70500040000F6
c906108c 190 *
c5aa993b 191 * S<type><length><address><data><checksum>
c906108c
SS
192 *
193 * Where
194 * - length
195 * is the number of bytes following upto the checksum. Note that
196 * this is not the number of chars following, since it takes two
197 * chars to represent a byte.
198 * - type
199 * is one of:
200 * 0) header record
201 * 1) two byte address data record
202 * 2) three byte address data record
203 * 3) four byte address data record
204 * 7) four byte address termination record
205 * 8) three byte address termination record
206 * 9) two byte address termination record
207 *
208 * - address
209 * is the start address of the data following, or in the case of
210 * a termination record, the start address of the image
211 * - data
212 * is the data.
213 * - checksum
c5aa993b 214 * is the sum of all the raw byte data in the record, from the length
c906108c
SS
215 * upwards, modulo 256 and subtracted from 255.
216 *
217 * This routine returns the length of the S-record.
218 *
219 */
220
221static int
222make_srec (srec, targ_addr, abfd, sect, sectoff, maxrecsize, flags)
223 char *srec;
224 CORE_ADDR targ_addr;
225 bfd *abfd;
226 asection *sect;
227 int sectoff;
228 int *maxrecsize;
229 int flags;
230{
231 unsigned char checksum;
232 int tmp;
233 const static char hextab[] = "0123456789ABCDEF";
234 const static char data_code_table[] = "123";
235 const static char term_code_table[] = "987";
236 const static char header_code_table[] = "000";
c5aa993b
JM
237 const static char *formats[] =
238 {"S%c%02X%04X",
239 "S%c%02X%06X",
240 "S%c%02X%08X"};
c906108c
SS
241 char const *code_table;
242 int addr_size;
243 int payload_size;
244 char *binbuf;
245 char *p;
246
247 if (sect)
248 {
249 tmp = flags; /* Data or header record */
250 code_table = abfd ? data_code_table : header_code_table;
c5aa993b 251 binbuf = alloca (*maxrecsize / 2);
c906108c
SS
252 }
253 else
254 {
c5aa993b 255 tmp = flags >> SREC_TERM_SHIFT; /* Term record */
c906108c
SS
256 code_table = term_code_table;
257 }
258
259 if ((tmp & SREC_2_BYTE_ADDR) && (targ_addr <= 0xffff))
260 addr_size = 2;
261 else if ((tmp & SREC_3_BYTE_ADDR) && (targ_addr <= 0xffffff))
262 addr_size = 3;
263 else if (tmp & SREC_4_BYTE_ADDR)
264 addr_size = 4;
265 else
96baa820
JM
266 internal_error ("make_srec: Bad address (0x%x), or bad flags (0x%x).",
267 targ_addr, flags);
c906108c
SS
268
269 /* Now that we know the address size, we can figure out how much
270 data this record can hold. */
271
272 if (sect && abfd)
273 {
274 payload_size = (*maxrecsize - (1 + 1 + 2 + addr_size * 2 + 2)) / 2;
275 payload_size = min (payload_size, sect->_raw_size - sectoff);
276
277 bfd_get_section_contents (abfd, sect, binbuf, sectoff, payload_size);
278 }
279 else
280 payload_size = 0; /* Term or header packets have no payload */
281
282 /* Output the header. */
283
284 sprintf (srec, formats[addr_size - 2], code_table[addr_size - 2],
c5aa993b 285 addr_size + payload_size + 1, (int) targ_addr);
c906108c
SS
286
287 /* Note that the checksum is calculated on the raw data, not the
288 hexified data. It includes the length, address and the data
289 portions of the packet. */
290
291 checksum = 0;
c5aa993b 292
c906108c 293 checksum += (payload_size + addr_size + 1 /* Packet length */
c5aa993b
JM
294 + (targ_addr & 0xff) /* Address... */
295 + ((targ_addr >> 8) & 0xff)
c906108c
SS
296 + ((targ_addr >> 16) & 0xff)
297 + ((targ_addr >> 24) & 0xff));
c5aa993b 298
c906108c
SS
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];
c5aa993b
JM
307 *p++ = hextab[k >> 4];
308 *p++ = hextab[k & 0xf];
c906108c
SS
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}