* ld-srec/srec.exp: Add setup_xfail for i960 COFF targets.
[deliverable/binutils-gdb.git] / gdb / srec.c
CommitLineData
5c8ba017
SG
1/* S-record download support for GDB, the GNU debugger.
2 Copyright 1995 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, 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
24extern int remote_debug;
25
26static int make_srec PARAMS ((char *srec, CORE_ADDR targ_addr, bfd *abfd,
27 asection *sect, int sectoff, int *maxrecsize,
28 int flags));
29
30/* Download an executable by converting it to S records. DESC is a serial_t
31 to send the data to. FILE is the name of the file to be loaded.
32 MAXRECSIZE is the length in chars of the largest S-record the host can
33 accomodate. This is measured from the starting `S' to the last char of the
34 checksum. FLAGS is various random flags, and HASHMARK is non-zero to cause
35 a `#' to be printed out for each record loaded. */
36
37void
38load_srec (desc, file, maxrecsize, flags, hashmark)
39 serial_t desc;
40 const char *file;
41 int maxrecsize;
42 int flags;
43 int hashmark;
44{
45 bfd *abfd;
46 asection *s;
47 char *buffer, srec[1024];
48 int i;
49 int reclen;
50
51 buffer = alloca (maxrecsize + 1);
52
53 abfd = bfd_openr (file, 0);
54 if (!abfd)
55 {
56 printf_filtered ("Unable to open file %s\n", file);
57 return;
58 }
59
60 if (bfd_check_format (abfd, bfd_object) == 0)
61 {
62 printf_filtered ("File is not an object file\n");
63 return;
64 }
65
66 for (s = abfd->sections; s; s = s->next)
67 if (s->flags & SEC_LOAD)
68 {
69 int numbytes;
70
71 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma,
72 s->vma + s->_raw_size);
73 gdb_flush (gdb_stdout);
74
75 for (i = 0; i < s->_raw_size; i += numbytes)
76 {
77 reclen = maxrecsize;
78 numbytes = make_srec (srec, s->vma + i, abfd, s, i, &reclen,
79 flags);
80
81 if (remote_debug)
82 fprintf_unfiltered (gdb_stderr, "%.*s", reclen, srec);
83 SERIAL_WRITE (desc, srec, reclen);
84
85 if (hashmark)
86 {
87 putchar_unfiltered ('#');
88 gdb_flush (gdb_stdout);
89 }
90 } /* Per-packet (or S-record) loop */
91
92 putchar_unfiltered ('\n');
93 } /* Loadable sections */
94
95 if (hashmark)
96 putchar_unfiltered ('\n');
97
98 /* Write a type 7 terminator record. no data for a type 7, and there
99 is no data, so len is 0. */
100
101 reclen = maxrecsize;
102 make_srec (srec, abfd->start_address, NULL, NULL, 0, &reclen, flags);
103
104 if (remote_debug)
105 fprintf_unfiltered (gdb_stderr, "%.*s", reclen, srec);
106 SERIAL_WRITE (desc, srec, reclen);
107
108 SERIAL_WRITE (desc, "\r\r", 2); /* Some monitors need these to wake up */
109
110 SERIAL_FLUSH_INPUT (desc);
111}
112
113/*
114 * make_srec -- make an srecord. This writes each line, one at a
115 * time, each with it's own header and trailer line.
116 * An srecord looks like this:
117 *
118 * byte count-+ address
119 * start ---+ | | data +- checksum
120 * | | | |
121 * S01000006F6B692D746573742E73726563E4
122 * S315000448600000000000000000FC00005900000000E9
123 * S31A0004000023C1400037DE00F023604000377B009020825000348D
124 * S30B0004485A0000000000004E
125 * S70500040000F6
126 *
127 * S<type><length><address><data><checksum>
128 *
129 * Where
130 * - length
131 * is the number of bytes following upto the checksum. Note that
132 * this is not the number of chars following, since it takes two
133 * chars to represent a byte.
134 * - type
135 * is one of:
136 * 0) header record
137 * 1) two byte address data record
138 * 2) three byte address data record
139 * 3) four byte address data record
140 * 7) four byte address termination record
141 * 8) three byte address termination record
142 * 9) two byte address termination record
143 *
144 * - address
145 * is the start address of the data following, or in the case of
146 * a termination record, the start address of the image
147 * - data
148 * is the data.
149 * - checksum
150 * is the sum of all the raw byte data in the record, from the length
151 * upwards, modulo 256 and subtracted from 255.
152 *
153 * This routine returns the length of the S-record.
154 *
155 */
156
157static int
158make_srec (srec, targ_addr, abfd, sect, sectoff, maxrecsize, flags)
159 char *srec;
160 CORE_ADDR targ_addr;
161 bfd *abfd;
162 asection *sect;
163 int sectoff;
164 int *maxrecsize;
165 int flags;
166{
167 unsigned char checksum;
168 int tmp;
169 const static char hextab[] = "0123456789ABCDEF";
170 const static char data_code_table[] = "xx123";
171 const static char term_code_table[] = "xx987";
172 const static char *formats[] = {NULL, NULL, "S%c%02X%04X", "S%c%02X%06X",
173 "S%c%02X%08X"};
174 char const *code_table;
175 int addr_size;
176 int payload_size;
177 int type_code;
178 char *binbuf;
179 char *p;
180
181 if (sect)
182 {
183 tmp = flags; /* Data record */
184 code_table = data_code_table;
185 binbuf = alloca (*maxrecsize/2);
186 }
187 else
188 {
189 tmp = flags >> SREC_TERM_SHIFT; /* Term record */
190 code_table = term_code_table;
191 }
192
193 if (tmp & SREC_4_BYTE_ADDR && targ_addr > 0xffffff)
194 addr_size = 4;
195 else if (tmp & SREC_3_BYTE_ADDR && targ_addr > 0xffff)
196 addr_size = 3;
197 else if (tmp & SREC_2_BYTE_ADDR && targ_addr > 0xff)
198 addr_size = 2;
199 else
200 fatal ("make_srec: Bad address (0x%x), or bad flags (0x%x).", targ_addr,
201 flags);
202
203/* Now that we know the address size, we can figure out how much data this
204 record can hold. */
205
206 if (sect)
207 {
208 payload_size = (*maxrecsize - (1 + 1 + 2 + addr_size * 2 + 2)) / 2;
209 payload_size = min (payload_size, sect->_raw_size - sectoff);
210
211 bfd_get_section_contents (abfd, sect, binbuf, sectoff, payload_size);
212 }
213 else
214 payload_size = 0; /* Term packets have no payload */
215
216/* Output the header. */
217
218 sprintf (srec, formats[addr_size], code_table[addr_size],
219 addr_size + payload_size + 1, targ_addr);
220
221/* Note that the checksum is calculated on the raw data, not the hexified
222 data. It includes the length, address and the data portions of the
223 packet. */
224
225 checksum = 0;
226
227 checksum += (payload_size + addr_size + 1 /* Packet length */
228 + (targ_addr & 0xff) /* Address... */
229 + ((targ_addr >> 8) & 0xff)
230 + ((targ_addr >> 16) & 0xff)
231 + ((targ_addr >> 24) & 0xff));
232
233 p = srec + 1 + 1 + 2 + addr_size * 2;
234
235 /* build the srecord */
236 for (tmp = 0; tmp < payload_size; tmp++)
237 {
238 unsigned char k;
239
240 k = binbuf[tmp];
241 *p++ = hextab [k >> 4];
242 *p++ = hextab [k & 0xf];
243 checksum += k;
244 }
245
246 checksum = ~checksum;
247
248 *p++ = hextab[checksum >> 4];
249 *p++ = hextab[checksum & 0xf];
250 *p++ = '\r';
251
252 *maxrecsize = p - srec;
253 return payload_size;
254}
This page took 0.032649 seconds and 4 git commands to generate.