1 /* S-record download support for GDB, the GNU debugger.
2 Copyright 1995 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
24 extern int remote_debug
;
26 static int make_srec
PARAMS ((char *srec
, CORE_ADDR targ_addr
, bfd
*abfd
,
27 asection
*sect
, int sectoff
, int *maxrecsize
,
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. */
38 load_srec (desc
, file
, maxrecsize
, flags
, hashmark
)
47 char *buffer
, srec
[1024];
51 buffer
= alloca (maxrecsize
+ 1);
53 abfd
= bfd_openr (file
, 0);
56 printf_filtered ("Unable to open file %s\n", file
);
60 if (bfd_check_format (abfd
, bfd_object
) == 0)
62 printf_filtered ("File is not an object file\n");
66 for (s
= abfd
->sections
; s
; s
= s
->next
)
67 if (s
->flags
& SEC_LOAD
)
71 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s
->name
, s
->vma
,
72 s
->vma
+ s
->_raw_size
);
73 gdb_flush (gdb_stdout
);
75 for (i
= 0; i
< s
->_raw_size
; i
+= numbytes
)
78 numbytes
= make_srec (srec
, s
->vma
+ i
, abfd
, s
, i
, &reclen
,
82 fprintf_unfiltered (gdb_stderr
, "%.*s\\r\n", reclen
-1, srec
);
83 SERIAL_WRITE (desc
, srec
, reclen
);
87 putchar_unfiltered ('#');
88 gdb_flush (gdb_stdout
);
90 } /* Per-packet (or S-record) loop */
92 putchar_unfiltered ('\n');
93 } /* Loadable sections */
96 putchar_unfiltered ('\n');
98 /* Write a type 7 terminator record. no data for a type 7, and there
99 is no data, so len is 0. */
102 make_srec (srec
, abfd
->start_address
, NULL
, NULL
, 0, &reclen
, flags
);
105 fprintf_unfiltered (gdb_stderr
, "%.*s\\r\n", reclen
-1, srec
);
106 SERIAL_WRITE (desc
, srec
, reclen
);
108 SERIAL_WRITE (desc
, "\r\r", 3); /* Some monitors need these to wake up */
110 SERIAL_FLUSH_INPUT (desc
);
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:
118 * byte count-+ address
119 * start ---+ | | data +- checksum
121 * S01000006F6B692D746573742E73726563E4
122 * S315000448600000000000000000FC00005900000000E9
123 * S31A0004000023C1400037DE00F023604000377B009020825000348D
124 * S30B0004485A0000000000004E
127 * S<type><length><address><data><checksum>
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.
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
145 * is the start address of the data following, or in the case of
146 * a termination record, the start address of the image
150 * is the sum of all the raw byte data in the record, from the length
151 * upwards, modulo 256 and subtracted from 255.
153 * This routine returns the length of the S-record.
158 make_srec (srec
, targ_addr
, abfd
, sect
, sectoff
, maxrecsize
, flags
)
167 unsigned char checksum
;
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",
174 char const *code_table
;
183 tmp
= flags
; /* Data record */
184 code_table
= data_code_table
;
185 binbuf
= alloca (*maxrecsize
/2);
189 tmp
= flags
>> SREC_TERM_SHIFT
; /* Term record */
190 code_table
= term_code_table
;
193 if (tmp
& SREC_4_BYTE_ADDR
&& targ_addr
> 0xffffff)
195 else if (tmp
& SREC_3_BYTE_ADDR
&& targ_addr
> 0xffff)
197 else if (tmp
& SREC_2_BYTE_ADDR
&& targ_addr
> 0xff)
200 fatal ("make_srec: Bad address (0x%x), or bad flags (0x%x).", targ_addr
,
203 /* Now that we know the address size, we can figure out how much data this
208 payload_size
= (*maxrecsize
- (1 + 1 + 2 + addr_size
* 2 + 2)) / 2;
209 payload_size
= min (payload_size
, sect
->_raw_size
- sectoff
);
211 bfd_get_section_contents (abfd
, sect
, binbuf
, sectoff
, payload_size
);
214 payload_size
= 0; /* Term packets have no payload */
216 /* Output the header. */
218 sprintf (srec
, formats
[addr_size
], code_table
[addr_size
],
219 addr_size
+ payload_size
+ 1, targ_addr
);
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
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));
233 p
= srec
+ 1 + 1 + 2 + addr_size
* 2;
235 /* build the srecord */
236 for (tmp
= 0; tmp
< payload_size
; tmp
++)
241 *p
++ = hextab
[k
>> 4];
242 *p
++ = hextab
[k
& 0xf];
246 checksum
= ~checksum
;
248 *p
++ = hextab
[checksum
>> 4];
249 *p
++ = hextab
[checksum
& 0xf];
252 *maxrecsize
= p
- srec
;