OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [gdb/] [dsrec.c] - Blame information for rev 1767

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.