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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.3/] [utils/] [sparclite/] [aload.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1181 sfurman
/* Program to load an image into the SPARClite monitor board
2
   Copyright 1993, 1994, 1995 Free Software Foundation, Inc.
3
 
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
8
 
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU General Public License for more details.
13
 
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
 
18
/* Call with:
19
 
20
   aload PROG TTY
21
 
22
ie: aload hello /dev/ttya
23
 
24
*/
25
 
26
#include <stdio.h>
27
 
28
#include "ansidecl.h"
29
 
30
#ifdef ANSI_PROTOTYPES
31
#include <stdarg.h>
32
#else
33
#include <varargs.h>
34
#endif
35
 
36
#include "libiberty.h"
37
#include "bfd.h"
38
 
39
#include <errno.h>
40
#include <unistd.h>
41
#include <fcntl.h>
42
 
43
#ifndef HAVE_TERMIOS
44
you lose
45
#endif
46
 
47
#if defined(HAVE_TERMIOS)
48
#include <termios.h>
49
#elif defined(HAVE_TERMIO)
50
#include <termio.h>
51
#elif defined(HAVE_SGTTY)
52
#include <sgtty.h>
53
#endif
54
 
55
#define min(A, B) (((A) < (B)) ? (A) : (B))
56
 
57
/* Where the code goes by default. */
58
 
59
#ifndef LOAD_ADDRESS
60
#define LOAD_ADDRESS 0x40000000
61
#endif
62
 
63
int quiet = 0;
64
 
65
static void
66
usage ()
67
{
68
  fprintf (stderr, "usage: aload [-q] file device\n");
69
  exit (1);
70
}
71
 
72
static void
73
#ifdef ANSI_PROTOTYPES
74
sys_error (char *msg, ...)
75
#else
76
sys_error (va_alist)
77
     va_dcl
78
#endif
79
{
80
  int e = errno;
81
  va_list args;
82
 
83
#ifdef ANSI_PROTOTYPES
84
  va_start (args, msg);
85
#else
86
  va_start (args);
87
#endif
88
 
89
#ifdef ANSI_PROTOTYPES
90
  vfprintf (stderr, msg, args);
91
#else
92
  {
93
    char *msg1;
94
 
95
    msg1 = va_arg (args, char *);
96
    vfprintf (stderr, msg1, args);
97
  }
98
#endif
99
  va_end (args);
100
 
101
  fprintf (stderr, ": %s\n", strerror(e));
102
  exit (1);
103
}
104
 
105
static void
106
#ifdef ANSI_PROTOTYPES
107
error (char *msg, ...)
108
#else
109
error (va_alist)
110
     va_dcl
111
#endif
112
{
113
  va_list args;
114
 
115
#ifdef ANSI_PROTOTYPES
116
  va_start (args, msg);
117
#else
118
  va_start (args);
119
#endif
120
 
121
#ifdef ANSI_PROTOTYPES
122
  vfprintf (stderr, msg, args);
123
#else
124
  {
125
    char *msg1;
126
 
127
    msg1 = va_arg (args, char *);
128
    vfprintf (stderr, msg1, args);
129
  }
130
#endif
131
  va_end (args);
132
 
133
  fputc ('\n', stderr);
134
  exit (1);
135
}
136
 
137
static int ttyfd;
138
 
139
static void
140
sendex (outtxt, outlen, intxt, inlen, id)
141
     unsigned char *outtxt;
142
     int outlen;
143
     unsigned char *intxt;
144
     int inlen;
145
     char *id;
146
{
147
  char buf[100];
148
  int cc;
149
 
150
  if (outlen > 0)
151
    {
152
      cc = write (ttyfd, outtxt, outlen);
153
      if (cc != outlen)
154
        sys_error ("Write %s failed", id);
155
    }
156
 
157
  if (inlen > 0)
158
    {
159
      cc = read (ttyfd, buf, inlen);    /* Get reply */
160
      if (cc != inlen)
161
        sys_error ("Read %s reply failed", id);
162
      if (bcmp (buf, intxt, inlen) != 0)
163
        error ("Bad reply to %s", id);
164
    }
165
}
166
 
167
extern int optind;
168
 
169
int
170
main (argc, argv)
171
     int argc;
172
     char **argv;
173
{
174
  struct termios termios;
175
  asection *section;
176
  bfd *pbfd;
177
  unsigned long entry;
178
  int c;
179
 
180
  while ((c = getopt (argc, argv, "q")) != EOF)
181
    {
182
      switch (c)
183
        {
184
        case 'q':
185
          quiet = 1;
186
          break;
187
        default:
188
          usage();
189
        }
190
    }
191
  argc -= optind;
192
  argv += optind;
193
 
194
  if (argc != 2)
195
    usage();
196
 
197
  pbfd = bfd_openr (argv[0], 0);
198
 
199
  if (pbfd == NULL)
200
    sys_error ("Open of PROG failed");
201
 
202
/* setup the tty.  Must be raw, no flow control, 9600 baud */
203
 
204
  ttyfd = open (argv[1], O_RDWR);
205
  if (ttyfd == -1)
206
    sys_error ("Open of TTY failed");
207
 
208
  if (tcgetattr(ttyfd, &termios))
209
    sys_error ("tcgetattr failed");
210
 
211
  termios.c_iflag = 0;
212
  termios.c_oflag = 0;
213
  termios.c_cflag = CS8 | CREAD | CLOCAL;
214
  termios.c_lflag = 0;
215
  termios.c_cc[VMIN] = 1;
216
  termios.c_cc[VTIME] = 0;
217
 
218
  if (cfsetospeed (&termios, B9600)
219
      || cfsetispeed (&termios, B9600))
220
    sys_error ("cfset{i|o}speed failed");
221
 
222
  if (tcsetattr (ttyfd, TCSANOW, &termios))
223
    sys_error ("tcsetattr failed");
224
 
225
  /* The char is documented as 0xaa, \252 is portable octal form.   */
226
  sendex("", 1, "\252", 1, "alive?");
227
  sendex ("U", 1, "U", 1, "alive");
228
  if (!quiet)
229
    printf ("[SPARClite appears to be alive]\n");
230
 
231
  if (!bfd_check_format (pbfd, bfd_object))
232
    error ("It doesn't seem to be an object file");
233
 
234
  for (section = pbfd->sections; section; section = section->next)
235
    {
236
      if (bfd_get_section_flags (pbfd, section) & SEC_ALLOC)
237
        {
238
          bfd_vma section_address;
239
          unsigned long section_size;
240
          const char *section_name;
241
 
242
          section_name = bfd_get_section_name (pbfd, section);
243
 
244
          section_address = bfd_get_section_vma (pbfd, section);
245
          /* Adjust sections from a.out files, since they don't
246
             carry their addresses with.  */
247
          if (bfd_get_flavour (pbfd) == bfd_target_aout_flavour)
248
            section_address += LOAD_ADDRESS;
249
          section_size = bfd_section_size (pbfd, section);
250
 
251
          if (!quiet)
252
            printf ("[Loading section %s at %lx (%ld bytes)]\n",
253
                    section_name, section_address, section_size);
254
 
255
          /* Text, data or lit */
256
          if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
257
            {
258
              file_ptr fptr;
259
 
260
              fptr = 0;
261
 
262
              while (section_size > 0)
263
                {
264
                  char buffer[1024];
265
                  int count, i;
266
                  unsigned char checksum;
267
                  static char inds[] = "|/-\\";
268
                  static int k = 0;
269
 
270
                  count = min (section_size, 1024);
271
 
272
                  bfd_get_section_contents (pbfd, section, buffer, fptr,
273
                                            count);
274
 
275
                  checksum = 0;
276
                  for (i = 0; i < count; i++)
277
                    checksum += buffer[i];
278
 
279
                  if (!quiet)
280
                    {
281
                      printf ("\r%c", inds[k++ % 4]);
282
                      fflush (stdout);
283
                    }
284
 
285
                  sendex ("\001", 1, "Z", 1, "load command");
286
                  sendex (&section_address, 4, NULL, 0, "load address");
287
                  sendex (&count, 4, NULL, 0, "program size");
288
                  sendex (buffer, count, &checksum, 1, "program");
289
 
290
                  section_address += count;
291
                  fptr += count;
292
                  section_size -= count;
293
                }
294
            }
295
          else                  /* BSS */
296
            {
297
              if (!quiet)
298
                printf ("Not loading BSS \n");
299
            }
300
        }
301
    }
302
 
303
  entry = bfd_get_start_address (pbfd);
304
 
305
  if (!quiet)
306
    printf ("[Starting %s at 0x%lx]\n", argv[0], entry);
307
 
308
  sendex ("\003", 1, NULL, 0, "exec command");
309
  sendex (&entry, 4, "U", 1, "program start");
310
 
311
  exit (0);
312
}

powered by: WebSVN 2.1.0

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