]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* S-record download support for GDB, the GNU debugger. |
32d0add0 | 2 | Copyright (C) 1995-2015 Free Software Foundation, Inc. |
c906108c | 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 | |
a9762ec7 | 8 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 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 | 16 | You should have received a copy of the GNU General Public License |
a9762ec7 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
18 | |
19 | #include "defs.h" | |
20 | #include "serial.h" | |
21 | #include "srec.h" | |
735522c2 | 22 | #include <sys/time.h> |
c906108c | 23 | #include <time.h> |
cbb099e8 | 24 | #include "gdb_bfd.h" |
c906108c | 25 | |
c906108c SS |
26 | extern int remote_debug; |
27 | ||
a14ed312 KB |
28 | static int make_srec (char *srec, CORE_ADDR targ_addr, bfd * abfd, |
29 | asection * sect, int sectoff, int *maxrecsize, | |
30 | int flags); | |
c906108c SS |
31 | |
32 | /* Download an executable by converting it to S records. DESC is a | |
819cc324 AC |
33 | `struct serial *' to send the data to. FILE is the name of the |
34 | file to be loaded. LOAD_OFFSET is the offset into memory to load | |
35 | data into. It is usually specified by the user and is useful with | |
36 | the a.out file format. MAXRECSIZE is the length in chars of the | |
37 | largest S-record the host can accomodate. This is measured from | |
38 | the starting `S' to the last char of the checksum. FLAGS is | |
39 | various random flags, and HASHMARK is non-zero to cause a `#' to be | |
c5aa993b | 40 | printed out for each record loaded. WAITACK, if non-NULL, is a |
819cc324 AC |
41 | function that waits for an acknowledgement after each S-record, and |
42 | returns non-zero if the ack is read correctly. */ | |
c906108c SS |
43 | |
44 | void | |
819cc324 AC |
45 | load_srec (struct serial *desc, const char *file, bfd_vma load_offset, |
46 | int maxrecsize, | |
9df3df99 | 47 | int flags, int hashmark, int (*waitack) (void)) |
c906108c SS |
48 | { |
49 | bfd *abfd; | |
50 | asection *s; | |
51 | char *srec; | |
52 | int i; | |
53 | int reclen; | |
735522c2 | 54 | struct timeval start_time, end_time; |
c906108c | 55 | unsigned long data_count = 0; |
d6ad71ba | 56 | struct cleanup *cleanup; |
c906108c SS |
57 | |
58 | srec = (char *) alloca (maxrecsize + 1); | |
59 | ||
1c00ec6b | 60 | abfd = gdb_bfd_open (file, NULL, -1); |
c906108c SS |
61 | if (!abfd) |
62 | { | |
a3f17187 | 63 | printf_filtered (_("Unable to open file %s\n"), file); |
c906108c SS |
64 | return; |
65 | } | |
66 | ||
f9a062ff | 67 | cleanup = make_cleanup_bfd_unref (abfd); |
c906108c SS |
68 | if (bfd_check_format (abfd, bfd_object) == 0) |
69 | { | |
a3f17187 | 70 | printf_filtered (_("File is not an object file\n")); |
d6ad71ba | 71 | do_cleanups (cleanup); |
c906108c SS |
72 | return; |
73 | } | |
74 | ||
735522c2 | 75 | gettimeofday (&start_time, NULL); |
c906108c SS |
76 | |
77 | /* Write a type 0 header record. no data for a type 0, and there | |
78 | is no data, so len is 0. */ | |
79 | ||
80 | reclen = maxrecsize; | |
c5aa993b | 81 | make_srec (srec, 0, NULL, (asection *) 1, 0, &reclen, flags); |
c906108c SS |
82 | if (remote_debug) |
83 | { | |
84 | srec[reclen] = '\0'; | |
85 | puts_debug ("sent -->", srec, "<--"); | |
86 | } | |
2cd58942 | 87 | serial_write (desc, srec, reclen); |
c906108c SS |
88 | |
89 | for (s = abfd->sections; s; s = s->next) | |
90 | if (s->flags & SEC_LOAD) | |
91 | { | |
92 | int numbytes; | |
9a619af0 | 93 | |
c906108c | 94 | bfd_vma addr = bfd_get_section_vma (abfd, s) + load_offset; |
2c500098 | 95 | bfd_size_type size = bfd_get_section_size (s); |
c5aa993b | 96 | char *section_name = (char *) bfd_get_section_name (abfd, s); |
d4f3574e SS |
97 | /* Both GDB and BFD have mechanisms for printing addresses. |
98 | In the below, GDB's is used so that the address is | |
99 | consistent with the rest of GDB. BFD's printf_vma() could | |
0963b4bd | 100 | have also been used. cagney 1999-09-01 */ |
5af949e3 | 101 | printf_filtered ("%s\t: %s .. %s ", |
d4f3574e | 102 | section_name, |
f5656ead TT |
103 | paddress (target_gdbarch (), addr), |
104 | paddress (target_gdbarch (), addr + size)); | |
c906108c SS |
105 | gdb_flush (gdb_stdout); |
106 | ||
107 | data_count += size; | |
108 | ||
109 | for (i = 0; i < size; i += numbytes) | |
110 | { | |
111 | reclen = maxrecsize; | |
112 | numbytes = make_srec (srec, (CORE_ADDR) (addr + i), abfd, s, | |
113 | i, &reclen, flags); | |
114 | ||
115 | if (remote_debug) | |
116 | { | |
117 | srec[reclen] = '\0'; | |
118 | puts_debug ("sent -->", srec, "<--"); | |
119 | } | |
120 | ||
121 | /* Repeatedly send the S-record until a good | |
122 | acknowledgement is sent back. */ | |
123 | do | |
124 | { | |
2cd58942 | 125 | serial_write (desc, srec, reclen); |
9a4105ab AC |
126 | if (deprecated_ui_load_progress_hook) |
127 | if (deprecated_ui_load_progress_hook (section_name, | |
128 | (unsigned long) i)) | |
8a3fe4f8 | 129 | error (_("Canceled the download")); |
c906108c SS |
130 | } |
131 | while (waitack != NULL && !waitack ()); | |
132 | ||
133 | if (hashmark) | |
134 | { | |
135 | putchar_unfiltered ('#'); | |
136 | gdb_flush (gdb_stdout); | |
137 | } | |
0963b4bd | 138 | } /* Per-packet (or S-record) loop. */ |
c906108c | 139 | |
9a4105ab AC |
140 | if (deprecated_ui_load_progress_hook) |
141 | if (deprecated_ui_load_progress_hook (section_name, | |
142 | (unsigned long) i)) | |
8a3fe4f8 | 143 | error (_("Canceled the download")); |
c906108c SS |
144 | putchar_unfiltered ('\n'); |
145 | } | |
146 | ||
c5aa993b | 147 | if (hashmark) |
c906108c SS |
148 | putchar_unfiltered ('\n'); |
149 | ||
735522c2 | 150 | gettimeofday (&end_time, NULL); |
c5aa993b | 151 | |
c906108c SS |
152 | /* Write a terminator record. */ |
153 | ||
154 | reclen = maxrecsize; | |
155 | make_srec (srec, abfd->start_address, NULL, NULL, 0, &reclen, flags); | |
156 | ||
157 | if (remote_debug) | |
158 | { | |
159 | srec[reclen] = '\0'; | |
160 | puts_debug ("sent -->", srec, "<--"); | |
161 | } | |
162 | ||
2cd58942 | 163 | serial_write (desc, srec, reclen); |
c906108c SS |
164 | |
165 | /* Some monitors need these to wake up properly. (Which ones? -sts) */ | |
2cd58942 | 166 | serial_write (desc, "\r\r", 2); |
c906108c SS |
167 | if (remote_debug) |
168 | puts_debug ("sent -->", "\r\r", "<---"); | |
169 | ||
2cd58942 | 170 | serial_flush_input (desc); |
c906108c | 171 | |
735522c2 TT |
172 | print_transfer_performance (gdb_stdout, data_count, 0, |
173 | &start_time, &end_time); | |
d6ad71ba | 174 | do_cleanups (cleanup); |
c906108c SS |
175 | } |
176 | ||
177 | /* | |
0963b4bd | 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 | |
0963b4bd MS |
195 | * is the number of bytes following upto the checksum. Note |
196 | * that this is not the number of chars following, since it | |
197 | * takes two chars to represent a byte. | |
c906108c SS |
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 | ||
221 | static int | |
fba45db2 KB |
222 | make_srec (char *srec, CORE_ADDR targ_addr, bfd *abfd, asection *sect, |
223 | int sectoff, int *maxrecsize, int flags) | |
c906108c SS |
224 | { |
225 | unsigned char checksum; | |
226 | int tmp; | |
44d100c3 TT |
227 | static const char hextab[] = "0123456789ABCDEF"; |
228 | static const char data_code_table[] = "123"; | |
229 | static const char term_code_table[] = "987"; | |
230 | static const char header_code_table[] = "000"; | |
c906108c SS |
231 | char const *code_table; |
232 | int addr_size; | |
233 | int payload_size; | |
234 | char *binbuf; | |
235 | char *p; | |
236 | ||
237 | if (sect) | |
238 | { | |
239 | tmp = flags; /* Data or header record */ | |
240 | code_table = abfd ? data_code_table : header_code_table; | |
c5aa993b | 241 | binbuf = alloca (*maxrecsize / 2); |
c906108c SS |
242 | } |
243 | else | |
244 | { | |
c5aa993b | 245 | tmp = flags >> SREC_TERM_SHIFT; /* Term record */ |
c906108c | 246 | code_table = term_code_table; |
93d56215 | 247 | binbuf = NULL; |
c906108c SS |
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 | |
8e65ff28 | 257 | internal_error (__FILE__, __LINE__, |
5af949e3 | 258 | _("make_srec: Bad address (%s), or bad flags (0x%x)."), |
f5656ead | 259 | paddress (target_gdbarch (), targ_addr), flags); |
c906108c SS |
260 | |
261 | /* Now that we know the address size, we can figure out how much | |
262 | data this record can hold. */ | |
263 | ||
264 | if (sect && abfd) | |
265 | { | |
266 | payload_size = (*maxrecsize - (1 + 1 + 2 + addr_size * 2 + 2)) / 2; | |
2c500098 | 267 | payload_size = min (payload_size, bfd_get_section_size (sect) - sectoff); |
c906108c SS |
268 | |
269 | bfd_get_section_contents (abfd, sect, binbuf, sectoff, payload_size); | |
270 | } | |
271 | else | |
0963b4bd | 272 | payload_size = 0; /* Term or header packets have no payload. */ |
c906108c SS |
273 | |
274 | /* Output the header. */ | |
44160db3 AC |
275 | snprintf (srec, (*maxrecsize) + 1, "S%c%02X%0*X", |
276 | code_table[addr_size - 2], | |
277 | addr_size + payload_size + 1, | |
278 | addr_size * 2, (int) targ_addr); | |
c906108c SS |
279 | |
280 | /* Note that the checksum is calculated on the raw data, not the | |
281 | hexified data. It includes the length, address and the data | |
282 | portions of the packet. */ | |
283 | ||
284 | checksum = 0; | |
c5aa993b | 285 | |
c906108c | 286 | checksum += (payload_size + addr_size + 1 /* Packet length */ |
0963b4bd | 287 | + (targ_addr & 0xff) /* Address... */ |
c5aa993b | 288 | + ((targ_addr >> 8) & 0xff) |
c906108c SS |
289 | + ((targ_addr >> 16) & 0xff) |
290 | + ((targ_addr >> 24) & 0xff)); | |
c5aa993b | 291 | |
44160db3 AC |
292 | /* NOTE: cagney/2003-08-10: The equation is old. Check that the |
293 | recent snprintf changes match that equation. */ | |
294 | gdb_assert (strlen (srec) == 1 + 1 + 2 + addr_size * 2); | |
c906108c SS |
295 | p = srec + 1 + 1 + 2 + addr_size * 2; |
296 | ||
297 | /* Build the Srecord. */ | |
298 | for (tmp = 0; tmp < payload_size; tmp++) | |
299 | { | |
300 | unsigned char k; | |
301 | ||
302 | k = binbuf[tmp]; | |
c5aa993b JM |
303 | *p++ = hextab[k >> 4]; |
304 | *p++ = hextab[k & 0xf]; | |
c906108c SS |
305 | checksum += k; |
306 | } | |
307 | ||
308 | checksum = ~checksum; | |
309 | ||
310 | *p++ = hextab[checksum >> 4]; | |
311 | *p++ = hextab[checksum & 0xf]; | |
312 | *p++ = '\r'; | |
313 | ||
314 | *maxrecsize = p - srec; | |
315 | return payload_size; | |
316 | } |