This commit was generated by cvs2svn to track changes on a CVS vendor
[deliverable/binutils-gdb.git] / gdb / dsrec.c
1 /* S-record download support for GDB, the GNU debugger.
2 Copyright 1995, 1996, 1997 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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, Boston, MA 02111-1307, USA. */
19
20 #include "defs.h"
21 #include "serial.h"
22 #include "srec.h"
23 #include <time.h>
24
25 int (*ui_load_progress_hook) PARAMS ((char *, unsigned long));
26 extern void report_transfer_performance PARAMS ((unsigned long, time_t, time_t));
27
28 extern int remote_debug;
29
30 static int make_srec PARAMS ((char *srec, CORE_ADDR targ_addr, bfd *abfd,
31 asection *sect, int sectoff, int *maxrecsize,
32 int flags));
33
34 /* Download an executable by converting it to S records. DESC is a
35 serial_t to send the data to. FILE is the name of the file to be
36 loaded. LOAD_OFFSET is the offset into memory to load data into.
37 It is usually specified by the user and is useful with the a.out
38 file format. MAXRECSIZE is the length in chars of the largest
39 S-record the host can accomodate. This is measured from the
40 starting `S' to the last char of the checksum. FLAGS is various
41 random flags, and HASHMARK is non-zero to cause a `#' to be
42 printed out for each record loaded. WAITACK, if non-NULL, is a
43 function that waits for an acknowledgement after each S-record,
44 and returns non-zero if the ack is read correctly. */
45
46 void
47 load_srec (desc, file, load_offset, maxrecsize, flags, hashmark, waitack)
48 serial_t desc;
49 const char *file;
50 bfd_vma load_offset;
51 int maxrecsize;
52 int flags;
53 int hashmark;
54 int (*waitack) PARAMS ((void));
55 {
56 bfd *abfd;
57 asection *s;
58 char *srec;
59 int i;
60 int reclen;
61 time_t start_time, end_time;
62 unsigned long data_count = 0;
63
64 srec = (char *) alloca (maxrecsize + 1);
65
66 abfd = bfd_openr (file, 0);
67 if (!abfd)
68 {
69 printf_filtered ("Unable to open file %s\n", file);
70 return;
71 }
72
73 if (bfd_check_format (abfd, bfd_object) == 0)
74 {
75 printf_filtered ("File is not an object file\n");
76 return;
77 }
78
79 start_time = time (NULL);
80
81 /* Write a type 0 header record. no data for a type 0, and there
82 is no data, so len is 0. */
83
84 reclen = maxrecsize;
85 make_srec (srec, 0, NULL, (asection *)1, 0, &reclen, flags);
86 if (remote_debug)
87 {
88 srec[reclen] = '\0';
89 puts_debug ("sent -->", srec, "<--");
90 }
91 SERIAL_WRITE (desc, srec, reclen);
92
93 for (s = abfd->sections; s; s = s->next)
94 if (s->flags & SEC_LOAD)
95 {
96 int numbytes;
97 bfd_vma addr = bfd_get_section_vma (abfd, s) + load_offset;
98 bfd_size_type size = bfd_get_section_size_before_reloc (s);
99 char * section_name = (char *)bfd_get_section_name (abfd, s);
100 printf_filtered ("%s\t: 0x%08x .. 0x%08x ",
101 section_name, (int) addr, (int) addr + size);
102 gdb_flush (gdb_stdout);
103
104 data_count += size;
105
106 for (i = 0; i < size; i += numbytes)
107 {
108 reclen = maxrecsize;
109 numbytes = make_srec (srec, (CORE_ADDR) (addr + i), abfd, s,
110 i, &reclen, flags);
111
112 if (remote_debug)
113 {
114 srec[reclen] = '\0';
115 puts_debug ("sent -->", srec, "<--");
116 }
117
118 /* Repeatedly send the S-record until a good
119 acknowledgement is sent back. */
120 do
121 {
122 SERIAL_WRITE (desc, srec, reclen);
123 if (ui_load_progress_hook)
124 if (ui_load_progress_hook (section_name, (unsigned long) i))
125 error ("Canceled the download");
126 }
127 while (waitack != NULL && !waitack ());
128
129 if (hashmark)
130 {
131 putchar_unfiltered ('#');
132 gdb_flush (gdb_stdout);
133 }
134 } /* Per-packet (or S-record) loop */
135
136 if (ui_load_progress_hook)
137 if (ui_load_progress_hook (section_name, (unsigned long) i))
138 error ("Canceled the download");
139 putchar_unfiltered ('\n');
140 }
141
142 if (hashmark)
143 putchar_unfiltered ('\n');
144
145 end_time = time (NULL);
146
147 /* Write a terminator record. */
148
149 reclen = maxrecsize;
150 make_srec (srec, abfd->start_address, NULL, NULL, 0, &reclen, flags);
151
152 if (remote_debug)
153 {
154 srec[reclen] = '\0';
155 puts_debug ("sent -->", srec, "<--");
156 }
157
158 SERIAL_WRITE (desc, srec, reclen);
159
160 /* Some monitors need these to wake up properly. (Which ones? -sts) */
161 SERIAL_WRITE (desc, "\r\r", 2);
162 puts_debug ("sent -->", "\r\r", "<---");
163
164 SERIAL_FLUSH_INPUT (desc);
165
166 report_transfer_performance (data_count, start_time, end_time);
167 }
168
169 /*
170 * make_srec -- make an srecord. This writes each line, one at a
171 * time, each with it's own header and trailer line.
172 * An srecord looks like this:
173 *
174 * byte count-+ address
175 * start ---+ | | data +- checksum
176 * | | | |
177 * S01000006F6B692D746573742E73726563E4
178 * S315000448600000000000000000FC00005900000000E9
179 * S31A0004000023C1400037DE00F023604000377B009020825000348D
180 * S30B0004485A0000000000004E
181 * S70500040000F6
182 *
183 * S<type><length><address><data><checksum>
184 *
185 * Where
186 * - length
187 * is the number of bytes following upto the checksum. Note that
188 * this is not the number of chars following, since it takes two
189 * chars to represent a byte.
190 * - type
191 * is one of:
192 * 0) header record
193 * 1) two byte address data record
194 * 2) three byte address data record
195 * 3) four byte address data record
196 * 7) four byte address termination record
197 * 8) three byte address termination record
198 * 9) two byte address termination record
199 *
200 * - address
201 * is the start address of the data following, or in the case of
202 * a termination record, the start address of the image
203 * - data
204 * is the data.
205 * - checksum
206 * is the sum of all the raw byte data in the record, from the length
207 * upwards, modulo 256 and subtracted from 255.
208 *
209 * This routine returns the length of the S-record.
210 *
211 */
212
213 static int
214 make_srec (srec, targ_addr, abfd, sect, sectoff, maxrecsize, flags)
215 char *srec;
216 CORE_ADDR targ_addr;
217 bfd *abfd;
218 asection *sect;
219 int sectoff;
220 int *maxrecsize;
221 int flags;
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";
229 const static char *formats[] = { "S%c%02X%04X",
230 "S%c%02X%06X",
231 "S%c%02X%08X" };
232 char const *code_table;
233 int addr_size;
234 int payload_size;
235 char *binbuf;
236 char *p;
237
238 if (sect)
239 {
240 tmp = flags; /* Data or header record */
241 code_table = abfd ? data_code_table : header_code_table;
242 binbuf = alloca (*maxrecsize/2);
243 }
244 else
245 {
246 tmp = flags >> SREC_TERM_SHIFT; /* Term record */
247 code_table = term_code_table;
248 }
249
250 if ((tmp & SREC_2_BYTE_ADDR) && (targ_addr <= 0xffff))
251 addr_size = 2;
252 else if ((tmp & SREC_3_BYTE_ADDR) && (targ_addr <= 0xffffff))
253 addr_size = 3;
254 else if (tmp & SREC_4_BYTE_ADDR)
255 addr_size = 4;
256 else
257 fatal ("make_srec: Bad address (0x%x), or bad flags (0x%x).",
258 targ_addr, flags);
259
260 /* Now that we know the address size, we can figure out how much
261 data this record can hold. */
262
263 if (sect && abfd)
264 {
265 payload_size = (*maxrecsize - (1 + 1 + 2 + addr_size * 2 + 2)) / 2;
266 payload_size = min (payload_size, sect->_raw_size - sectoff);
267
268 bfd_get_section_contents (abfd, sect, binbuf, sectoff, payload_size);
269 }
270 else
271 payload_size = 0; /* Term or header packets have no payload */
272
273 /* Output the header. */
274
275 sprintf (srec, formats[addr_size - 2], code_table[addr_size - 2],
276 addr_size + payload_size + 1, targ_addr);
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;
283
284 checksum += (payload_size + addr_size + 1 /* Packet length */
285 + (targ_addr & 0xff) /* Address... */
286 + ((targ_addr >> 8) & 0xff)
287 + ((targ_addr >> 16) & 0xff)
288 + ((targ_addr >> 24) & 0xff));
289
290 p = srec + 1 + 1 + 2 + addr_size * 2;
291
292 /* Build the Srecord. */
293 for (tmp = 0; tmp < payload_size; tmp++)
294 {
295 unsigned char k;
296
297 k = binbuf[tmp];
298 *p++ = hextab [k >> 4];
299 *p++ = hextab [k & 0xf];
300 checksum += k;
301 }
302
303 checksum = ~checksum;
304
305 *p++ = hextab[checksum >> 4];
306 *p++ = hextab[checksum & 0xf];
307 *p++ = '\r';
308
309 *maxrecsize = p - srec;
310 return payload_size;
311 }
This page took 0.038531 seconds and 5 git commands to generate.