Commit | Line | Data |
---|---|---|
c906108c | 1 | /* S-record download support for GDB, the GNU debugger. |
197e01b6 | 2 | Copyright (C) 1995, 1996, 1997, 1999, 2000, 2001, 2003, 2004 |
b6ba6518 | 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 | |
197e01b6 EZ |
19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, 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" |
cdcd5552 | 27 | #include "gdb_string.h" |
c906108c | 28 | |
a14ed312 | 29 | extern void report_transfer_performance (unsigned long, time_t, time_t); |
c906108c SS |
30 | |
31 | extern int remote_debug; | |
32 | ||
a14ed312 KB |
33 | static int make_srec (char *srec, CORE_ADDR targ_addr, bfd * abfd, |
34 | asection * sect, int sectoff, int *maxrecsize, | |
35 | int flags); | |
c906108c SS |
36 | |
37 | /* Download an executable by converting it to S records. DESC is a | |
819cc324 AC |
38 | `struct serial *' to send the data to. FILE is the name of the |
39 | file to be loaded. LOAD_OFFSET is the offset into memory to load | |
40 | data into. It is usually specified by the user and is useful with | |
41 | the a.out file format. MAXRECSIZE is the length in chars of the | |
42 | largest S-record the host can accomodate. This is measured from | |
43 | the starting `S' to the last char of the checksum. FLAGS is | |
44 | various random flags, and HASHMARK is non-zero to cause a `#' to be | |
c5aa993b | 45 | printed out for each record loaded. WAITACK, if non-NULL, is a |
819cc324 AC |
46 | function that waits for an acknowledgement after each S-record, and |
47 | returns non-zero if the ack is read correctly. */ | |
c906108c SS |
48 | |
49 | void | |
819cc324 AC |
50 | load_srec (struct serial *desc, const char *file, bfd_vma load_offset, |
51 | int maxrecsize, | |
9df3df99 | 52 | int flags, int hashmark, int (*waitack) (void)) |
c906108c SS |
53 | { |
54 | bfd *abfd; | |
55 | asection *s; | |
56 | char *srec; | |
57 | int i; | |
58 | int reclen; | |
59 | time_t start_time, end_time; | |
60 | unsigned long data_count = 0; | |
61 | ||
62 | srec = (char *) alloca (maxrecsize + 1); | |
63 | ||
64 | abfd = bfd_openr (file, 0); | |
65 | if (!abfd) | |
66 | { | |
a3f17187 | 67 | printf_filtered (_("Unable to open file %s\n"), file); |
c906108c SS |
68 | return; |
69 | } | |
70 | ||
71 | if (bfd_check_format (abfd, bfd_object) == 0) | |
72 | { | |
a3f17187 | 73 | printf_filtered (_("File is not an object file\n")); |
c906108c SS |
74 | return; |
75 | } | |
76 | ||
77 | start_time = time (NULL); | |
78 | ||
79 | /* Write a type 0 header record. no data for a type 0, and there | |
80 | is no data, so len is 0. */ | |
81 | ||
82 | reclen = maxrecsize; | |
c5aa993b | 83 | make_srec (srec, 0, NULL, (asection *) 1, 0, &reclen, flags); |
c906108c SS |
84 | if (remote_debug) |
85 | { | |
86 | srec[reclen] = '\0'; | |
87 | puts_debug ("sent -->", srec, "<--"); | |
88 | } | |
2cd58942 | 89 | serial_write (desc, srec, reclen); |
c906108c SS |
90 | |
91 | for (s = abfd->sections; s; s = s->next) | |
92 | if (s->flags & SEC_LOAD) | |
93 | { | |
94 | int numbytes; | |
95 | bfd_vma addr = bfd_get_section_vma (abfd, s) + load_offset; | |
2c500098 | 96 | bfd_size_type size = bfd_get_section_size (s); |
c5aa993b | 97 | char *section_name = (char *) bfd_get_section_name (abfd, s); |
d4f3574e SS |
98 | /* Both GDB and BFD have mechanisms for printing addresses. |
99 | In the below, GDB's is used so that the address is | |
100 | consistent with the rest of GDB. BFD's printf_vma() could | |
101 | have also been used. cagney 1999-09-01 */ | |
102 | printf_filtered ("%s\t: 0x%s .. 0x%s ", | |
103 | section_name, | |
104 | paddr (addr), | |
105 | paddr (addr + size)); | |
c906108c SS |
106 | gdb_flush (gdb_stdout); |
107 | ||
108 | data_count += size; | |
109 | ||
110 | for (i = 0; i < size; i += numbytes) | |
111 | { | |
112 | reclen = maxrecsize; | |
113 | numbytes = make_srec (srec, (CORE_ADDR) (addr + i), abfd, s, | |
114 | i, &reclen, flags); | |
115 | ||
116 | if (remote_debug) | |
117 | { | |
118 | srec[reclen] = '\0'; | |
119 | puts_debug ("sent -->", srec, "<--"); | |
120 | } | |
121 | ||
122 | /* Repeatedly send the S-record until a good | |
123 | acknowledgement is sent back. */ | |
124 | do | |
125 | { | |
2cd58942 | 126 | serial_write (desc, srec, reclen); |
9a4105ab AC |
127 | if (deprecated_ui_load_progress_hook) |
128 | if (deprecated_ui_load_progress_hook (section_name, | |
129 | (unsigned long) i)) | |
8a3fe4f8 | 130 | error (_("Canceled the download")); |
c906108c SS |
131 | } |
132 | while (waitack != NULL && !waitack ()); | |
133 | ||
134 | if (hashmark) | |
135 | { | |
136 | putchar_unfiltered ('#'); | |
137 | gdb_flush (gdb_stdout); | |
138 | } | |
139 | } /* Per-packet (or S-record) loop */ | |
140 | ||
9a4105ab AC |
141 | if (deprecated_ui_load_progress_hook) |
142 | if (deprecated_ui_load_progress_hook (section_name, | |
143 | (unsigned long) i)) | |
8a3fe4f8 | 144 | error (_("Canceled the download")); |
c906108c SS |
145 | putchar_unfiltered ('\n'); |
146 | } | |
147 | ||
c5aa993b | 148 | if (hashmark) |
c906108c SS |
149 | putchar_unfiltered ('\n'); |
150 | ||
151 | end_time = time (NULL); | |
c5aa993b | 152 | |
c906108c SS |
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 | ||
2cd58942 | 164 | serial_write (desc, srec, reclen); |
c906108c SS |
165 | |
166 | /* Some monitors need these to wake up properly. (Which ones? -sts) */ | |
2cd58942 | 167 | serial_write (desc, "\r\r", 2); |
c906108c SS |
168 | if (remote_debug) |
169 | puts_debug ("sent -->", "\r\r", "<---"); | |
170 | ||
2cd58942 | 171 | serial_flush_input (desc); |
c906108c SS |
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 | |
c5aa993b JM |
178 | * time, each with it's own header and trailer line. |
179 | * An srecord looks like this: | |
c906108c SS |
180 | * |
181 | * byte count-+ address | |
182 | * start ---+ | | data +- checksum | |
c5aa993b JM |
183 | * | | | | |
184 | * S01000006F6B692D746573742E73726563E4 | |
185 | * S315000448600000000000000000FC00005900000000E9 | |
186 | * S31A0004000023C1400037DE00F023604000377B009020825000348D | |
187 | * S30B0004485A0000000000004E | |
188 | * S70500040000F6 | |
c906108c | 189 | * |
c5aa993b | 190 | * S<type><length><address><data><checksum> |
c906108c SS |
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 | |
c5aa993b | 213 | * is the sum of all the raw byte data in the record, from the length |
c906108c SS |
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 | |
fba45db2 KB |
221 | make_srec (char *srec, CORE_ADDR targ_addr, bfd *abfd, asection *sect, |
222 | int sectoff, int *maxrecsize, int flags) | |
c906108c SS |
223 | { |
224 | unsigned char checksum; | |
225 | int tmp; | |
226 | const static char hextab[] = "0123456789ABCDEF"; | |
227 | const static char data_code_table[] = "123"; | |
228 | const static char term_code_table[] = "987"; | |
229 | const static char header_code_table[] = "000"; | |
c906108c SS |
230 | char const *code_table; |
231 | int addr_size; | |
232 | int payload_size; | |
233 | char *binbuf; | |
234 | char *p; | |
235 | ||
236 | if (sect) | |
237 | { | |
238 | tmp = flags; /* Data or header record */ | |
239 | code_table = abfd ? data_code_table : header_code_table; | |
c5aa993b | 240 | binbuf = alloca (*maxrecsize / 2); |
c906108c SS |
241 | } |
242 | else | |
243 | { | |
c5aa993b | 244 | tmp = flags >> SREC_TERM_SHIFT; /* Term record */ |
c906108c | 245 | code_table = term_code_table; |
93d56215 | 246 | binbuf = NULL; |
c906108c SS |
247 | } |
248 | ||
249 | if ((tmp & SREC_2_BYTE_ADDR) && (targ_addr <= 0xffff)) | |
250 | addr_size = 2; | |
251 | else if ((tmp & SREC_3_BYTE_ADDR) && (targ_addr <= 0xffffff)) | |
252 | addr_size = 3; | |
253 | else if (tmp & SREC_4_BYTE_ADDR) | |
254 | addr_size = 4; | |
255 | else | |
8e65ff28 | 256 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 257 | _("make_srec: Bad address (0x%s), or bad flags (0x%x)."), |
58841d58 | 258 | paddr (targ_addr), flags); |
c906108c SS |
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; | |
2c500098 | 266 | payload_size = min (payload_size, bfd_get_section_size (sect) - sectoff); |
c906108c SS |
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. */ | |
44160db3 AC |
274 | snprintf (srec, (*maxrecsize) + 1, "S%c%02X%0*X", |
275 | code_table[addr_size - 2], | |
276 | addr_size + payload_size + 1, | |
277 | addr_size * 2, (int) targ_addr); | |
c906108c SS |
278 | |
279 | /* Note that the checksum is calculated on the raw data, not the | |
280 | hexified data. It includes the length, address and the data | |
281 | portions of the packet. */ | |
282 | ||
283 | checksum = 0; | |
c5aa993b | 284 | |
c906108c | 285 | checksum += (payload_size + addr_size + 1 /* Packet length */ |
c5aa993b JM |
286 | + (targ_addr & 0xff) /* Address... */ |
287 | + ((targ_addr >> 8) & 0xff) | |
c906108c SS |
288 | + ((targ_addr >> 16) & 0xff) |
289 | + ((targ_addr >> 24) & 0xff)); | |
c5aa993b | 290 | |
44160db3 AC |
291 | /* NOTE: cagney/2003-08-10: The equation is old. Check that the |
292 | recent snprintf changes match that equation. */ | |
293 | gdb_assert (strlen (srec) == 1 + 1 + 2 + addr_size * 2); | |
c906108c SS |
294 | p = srec + 1 + 1 + 2 + addr_size * 2; |
295 | ||
296 | /* Build the Srecord. */ | |
297 | for (tmp = 0; tmp < payload_size; tmp++) | |
298 | { | |
299 | unsigned char k; | |
300 | ||
301 | k = binbuf[tmp]; | |
c5aa993b JM |
302 | *p++ = hextab[k >> 4]; |
303 | *p++ = hextab[k & 0xf]; | |
c906108c SS |
304 | checksum += k; |
305 | } | |
306 | ||
307 | checksum = ~checksum; | |
308 | ||
309 | *p++ = hextab[checksum >> 4]; | |
310 | *p++ = hextab[checksum & 0xf]; | |
311 | *p++ = '\r'; | |
312 | ||
313 | *maxrecsize = p - srec; | |
314 | return payload_size; | |
315 | } |