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