1 |
104 |
markom |
/* 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,
|
19 |
|
|
Boston, MA 02111-1307, USA. */
|
20 |
|
|
|
21 |
|
|
#include "defs.h"
|
22 |
|
|
#include "serial.h"
|
23 |
|
|
#include "srec.h"
|
24 |
|
|
#include <time.h>
|
25 |
|
|
|
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 |
|
|
/* 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
|
103 |
|
|
have also been used. cagney 1999-09-01 */
|
104 |
|
|
printf_filtered ("%s\t: 0x%s .. 0x%s ",
|
105 |
|
|
section_name,
|
106 |
|
|
paddr (addr),
|
107 |
|
|
paddr (addr + size));
|
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 |
|
|
{
|
128 |
|
|
SERIAL_WRITE (desc, srec, reclen);
|
129 |
|
|
if (ui_load_progress_hook)
|
130 |
|
|
if (ui_load_progress_hook (section_name, (unsigned long) i))
|
131 |
|
|
error ("Canceled the download");
|
132 |
|
|
}
|
133 |
|
|
while (waitack != NULL && !waitack ());
|
134 |
|
|
|
135 |
|
|
if (hashmark)
|
136 |
|
|
{
|
137 |
|
|
putchar_unfiltered ('#');
|
138 |
|
|
gdb_flush (gdb_stdout);
|
139 |
|
|
}
|
140 |
|
|
} /* Per-packet (or S-record) loop */
|
141 |
|
|
|
142 |
|
|
if (ui_load_progress_hook)
|
143 |
|
|
if (ui_load_progress_hook (section_name, (unsigned long) i))
|
144 |
|
|
error ("Canceled the download");
|
145 |
|
|
putchar_unfiltered ('\n');
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
if (hashmark)
|
149 |
|
|
putchar_unfiltered ('\n');
|
150 |
|
|
|
151 |
|
|
end_time = time (NULL);
|
152 |
|
|
|
153 |
|
|
/* Write a terminator record. */
|
154 |
|
|
|
155 |
|
|
reclen = maxrecsize;
|
156 |
|
|
make_srec (srec, abfd->start_address, NULL, NULL, 0, &reclen, flags);
|
157 |
|
|
|
158 |
|
|
if (remote_debug)
|
159 |
|
|
{
|
160 |
|
|
srec[reclen] = '\0';
|
161 |
|
|
puts_debug ("sent -->", srec, "<--");
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
SERIAL_WRITE (desc, srec, reclen);
|
165 |
|
|
|
166 |
|
|
/* Some monitors need these to wake up properly. (Which ones? -sts) */
|
167 |
|
|
SERIAL_WRITE (desc, "\r\r", 2);
|
168 |
|
|
if (remote_debug)
|
169 |
|
|
puts_debug ("sent -->", "\r\r", "<---");
|
170 |
|
|
|
171 |
|
|
SERIAL_FLUSH_INPUT (desc);
|
172 |
|
|
|
173 |
|
|
report_transfer_performance (data_count, start_time, end_time);
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
/*
|
177 |
|
|
* make_srec -- make an srecord. This writes each line, one at a
|
178 |
|
|
* time, each with it's own header and trailer line.
|
179 |
|
|
* An srecord looks like this:
|
180 |
|
|
*
|
181 |
|
|
* byte count-+ address
|
182 |
|
|
* start ---+ | | data +- checksum
|
183 |
|
|
* | | | |
|
184 |
|
|
* S01000006F6B692D746573742E73726563E4
|
185 |
|
|
* S315000448600000000000000000FC00005900000000E9
|
186 |
|
|
* S31A0004000023C1400037DE00F023604000377B009020825000348D
|
187 |
|
|
* S30B0004485A0000000000004E
|
188 |
|
|
* S70500040000F6
|
189 |
|
|
*
|
190 |
|
|
* S<type><length><address><data><checksum>
|
191 |
|
|
*
|
192 |
|
|
* Where
|
193 |
|
|
* - length
|
194 |
|
|
* is the number of bytes following upto the checksum. Note that
|
195 |
|
|
* this is not the number of chars following, since it takes two
|
196 |
|
|
* chars to represent a byte.
|
197 |
|
|
* - type
|
198 |
|
|
* is one of:
|
199 |
|
|
* 0) header record
|
200 |
|
|
* 1) two byte address data record
|
201 |
|
|
* 2) three byte address data record
|
202 |
|
|
* 3) four byte address data record
|
203 |
|
|
* 7) four byte address termination record
|
204 |
|
|
* 8) three byte address termination record
|
205 |
|
|
* 9) two byte address termination record
|
206 |
|
|
*
|
207 |
|
|
* - address
|
208 |
|
|
* is the start address of the data following, or in the case of
|
209 |
|
|
* a termination record, the start address of the image
|
210 |
|
|
* - data
|
211 |
|
|
* is the data.
|
212 |
|
|
* - checksum
|
213 |
|
|
* is the sum of all the raw byte data in the record, from the length
|
214 |
|
|
* upwards, modulo 256 and subtracted from 255.
|
215 |
|
|
*
|
216 |
|
|
* This routine returns the length of the S-record.
|
217 |
|
|
*
|
218 |
|
|
*/
|
219 |
|
|
|
220 |
|
|
static int
|
221 |
|
|
make_srec (srec, targ_addr, abfd, sect, sectoff, maxrecsize, flags)
|
222 |
|
|
char *srec;
|
223 |
|
|
CORE_ADDR targ_addr;
|
224 |
|
|
bfd *abfd;
|
225 |
|
|
asection *sect;
|
226 |
|
|
int sectoff;
|
227 |
|
|
int *maxrecsize;
|
228 |
|
|
int flags;
|
229 |
|
|
{
|
230 |
|
|
unsigned char checksum;
|
231 |
|
|
int tmp;
|
232 |
|
|
const static char hextab[] = "0123456789ABCDEF";
|
233 |
|
|
const static char data_code_table[] = "123";
|
234 |
|
|
const static char term_code_table[] = "987";
|
235 |
|
|
const static char header_code_table[] = "000";
|
236 |
|
|
const static char *formats[] =
|
237 |
|
|
{"S%c%02X%04X",
|
238 |
|
|
"S%c%02X%06X",
|
239 |
|
|
"S%c%02X%08X"};
|
240 |
|
|
char const *code_table;
|
241 |
|
|
int addr_size;
|
242 |
|
|
int payload_size;
|
243 |
|
|
char *binbuf;
|
244 |
|
|
char *p;
|
245 |
|
|
|
246 |
|
|
if (sect)
|
247 |
|
|
{
|
248 |
|
|
tmp = flags; /* Data or header record */
|
249 |
|
|
code_table = abfd ? data_code_table : header_code_table;
|
250 |
|
|
binbuf = alloca (*maxrecsize / 2);
|
251 |
|
|
}
|
252 |
|
|
else
|
253 |
|
|
{
|
254 |
|
|
tmp = flags >> SREC_TERM_SHIFT; /* Term record */
|
255 |
|
|
code_table = term_code_table;
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
if ((tmp & SREC_2_BYTE_ADDR) && (targ_addr <= 0xffff))
|
259 |
|
|
addr_size = 2;
|
260 |
|
|
else if ((tmp & SREC_3_BYTE_ADDR) && (targ_addr <= 0xffffff))
|
261 |
|
|
addr_size = 3;
|
262 |
|
|
else if (tmp & SREC_4_BYTE_ADDR)
|
263 |
|
|
addr_size = 4;
|
264 |
|
|
else
|
265 |
|
|
internal_error ("make_srec: Bad address (0x%x), or bad flags (0x%x).",
|
266 |
|
|
targ_addr, flags);
|
267 |
|
|
|
268 |
|
|
/* Now that we know the address size, we can figure out how much
|
269 |
|
|
data this record can hold. */
|
270 |
|
|
|
271 |
|
|
if (sect && abfd)
|
272 |
|
|
{
|
273 |
|
|
payload_size = (*maxrecsize - (1 + 1 + 2 + addr_size * 2 + 2)) / 2;
|
274 |
|
|
payload_size = min (payload_size, sect->_raw_size - sectoff);
|
275 |
|
|
|
276 |
|
|
bfd_get_section_contents (abfd, sect, binbuf, sectoff, payload_size);
|
277 |
|
|
}
|
278 |
|
|
else
|
279 |
|
|
payload_size = 0; /* Term or header packets have no payload */
|
280 |
|
|
|
281 |
|
|
/* Output the header. */
|
282 |
|
|
|
283 |
|
|
sprintf (srec, formats[addr_size - 2], code_table[addr_size - 2],
|
284 |
|
|
addr_size + payload_size + 1, (int) targ_addr);
|
285 |
|
|
|
286 |
|
|
/* Note that the checksum is calculated on the raw data, not the
|
287 |
|
|
hexified data. It includes the length, address and the data
|
288 |
|
|
portions of the packet. */
|
289 |
|
|
|
290 |
|
|
checksum = 0;
|
291 |
|
|
|
292 |
|
|
checksum += (payload_size + addr_size + 1 /* Packet length */
|
293 |
|
|
+ (targ_addr & 0xff) /* Address... */
|
294 |
|
|
+ ((targ_addr >> 8) & 0xff)
|
295 |
|
|
+ ((targ_addr >> 16) & 0xff)
|
296 |
|
|
+ ((targ_addr >> 24) & 0xff));
|
297 |
|
|
|
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];
|
306 |
|
|
*p++ = hextab[k >> 4];
|
307 |
|
|
*p++ = hextab[k & 0xf];
|
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 |
|
|
}
|