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