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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [utils/] [sparclite/] [eload.c] - Blame information for rev 107

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

Line No. Rev Author Line
1 106 markom
/* Program to load an image into the SPARClite monitor board via Ethernet
2
   Copyright 1993 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
   eload PROG HOSTNAME
21
 
22
(HOSTNAME is the name (or IP address) of your eval board)
23
 
24
ie: eload hello sparky
25
 
26
*/
27
 
28
#include <stdio.h>
29
 
30
#include "ansidecl.h"
31
 
32
#ifdef ANSI_PROTOTYPES
33
#include <stdarg.h>
34
#else
35
#include <varargs.h>
36
#endif
37
 
38
#include "libiberty.h"
39
#include "bfd.h"
40
 
41
#include <errno.h>
42
#include <fcntl.h>
43
#include <termios.h>
44
#include <unistd.h>
45
 
46
#include <sys/types.h>
47
#include <sys/socket.h>
48
#include <netdb.h>
49
#include <sys/socket.h>
50
#include <netinet/in.h>
51
#include <fcntl.h>
52
#include <sys/time.h>
53
 
54
#define min(A, B) (((A) < (B)) ? (A) : (B))
55
 
56
/* Where the code goes by default. */
57
 
58
#ifndef LOAD_ADDRESS
59
#define LOAD_ADDRESS 0x40000000
60
#endif
61
 
62
static void
63
usage()
64
{
65
  fprintf (stderr, "usage: eload executable-file network-name\n");
66
  exit (1);
67
}
68
 
69
static void
70
#ifdef ANSI_PROTOTYPES
71
sys_error (char *msg, ...)
72
#else
73
sys_error (va_alist)
74
     va_dcl
75
#endif
76
{
77
  int e = errno;
78
  va_list args;
79
 
80
#ifdef ANSI_PROTOTYPES
81
  va_start (args, msg);
82
#else
83
  va_start (args);
84
#endif
85
 
86
#ifdef ANSI_PROTOTYPES
87
  vfprintf (stderr, msg, args);
88
#else
89
  {
90
    char *msg1;
91
 
92
    msg1 = va_arg (args, char *);
93
    vfprintf (stderr, msg1, args);
94
  }
95
#endif
96
  va_end (args);
97
 
98
  fprintf (stderr, ": %s\n", strerror(e));
99
  exit (1);
100
}
101
 
102
static void
103
#ifdef ANSI_PROTOTYPES
104
error (char *msg, ...)
105
#else
106
error (va_alist)
107
     va_dcl
108
#endif
109
{
110
  va_list args;
111
 
112
#ifdef ANSI_PROTOTYPES
113
  va_start (args, msg);
114
#else
115
  va_start (args);
116
#endif
117
 
118
#ifdef ANSI_PROTOTYPES
119
  vfprintf (stderr, msg, args);
120
#else
121
  {
122
    char *msg1;
123
 
124
    msg1 = va_arg (args, char *);
125
    vfprintf (stderr, msg1, args);
126
  }
127
#endif
128
  va_end (args);
129
 
130
  fputc ('\n', stderr);
131
  exit (1);
132
}
133
 
134
int netfd;
135
 
136
static int
137
recv_buf (fd, buf, len, timeout)
138
     int fd, len;
139
     unsigned char *buf;
140
     int timeout;
141
{
142
  int cc;
143
  fd_set readfds;
144
 
145
  FD_ZERO (&readfds);
146
  FD_SET (fd, &readfds);
147
 
148
  if (timeout >= 0)
149
    {
150
      struct timeval timebuf;
151
 
152
      timebuf.tv_sec = timeout;
153
      timebuf.tv_usec = 0;
154
      cc = select (fd + 1, &readfds, 0, 0, &timebuf);
155
    }
156
  else
157
    cc = select (fd + 1, &readfds, 0, 0, 0);
158
 
159
  if (cc == 0)
160
    return 0;
161
 
162
  if (cc != 1)
163
    sys_error ("recv_buf: Bad return value from select:");
164
 
165
  cc = recv (fd, buf, len, 0);
166
 
167
  if (cc < 0)
168
    sys_error ("Got an error from recv: ");
169
 
170
  return cc;
171
}
172
 
173
static void
174
send_buf (fd, buf, len)
175
     int fd, len;
176
     unsigned char *buf;
177
{
178
  int cc;
179
 
180
  cc = send (fd, buf, len, 0);
181
 
182
  if (cc == len)
183
    return;
184
 
185
  if (cc < 0)
186
    sys_error ("Got an error from send: ");
187
 
188
  printf ("Short count in send: tried %d, sent %d\n", len, cc);
189
}
190
 
191
static unsigned short
192
calc_checksum (buffer, count)
193
     unsigned char *buffer;
194
     int count;
195
{
196
  unsigned short checksum;
197
  unsigned short *s;
198
 
199
  s = (unsigned short *)buffer;
200
 
201
  checksum = 0;
202
  for (; count > 0; count -= 2)
203
    checksum += *s++;
204
 
205
  if (count != 0)
206
    checksum += (*s & 0xff00);
207
 
208
  return checksum;
209
}
210
 
211
static void
212
send_data (buffer, fd, addr, count)
213
     unsigned char *buffer;
214
     int fd;
215
     unsigned long addr;
216
     int count;
217
{
218
  int cc, i;
219
  static int pkt_num = 0;
220
  unsigned char snd_buf[2000];
221
  unsigned short checksum;
222
  static unsigned long old_addr = -1;
223
 
224
  while (1)
225
    {
226
      if (addr != old_addr)
227
        {
228
          snd_buf[0] = 0x1;      /* Load command */
229
          snd_buf[1] = 0x1;     /* Loading address */
230
          snd_buf[2] = addr >> 24;
231
          snd_buf[3] = addr >> 16;
232
          snd_buf[4] = addr >> 8;
233
          snd_buf[5] = addr;
234
 
235
          checksum = 0;
236
          for (i = 0; i < 6; i++)
237
            checksum += snd_buf[i];
238
          checksum &= 0xff;
239
 
240
          send_buf (fd, snd_buf, 6);
241
          cc = recv_buf (fd, snd_buf, sizeof snd_buf, -1);
242
 
243
          if (cc < 1)
244
            {
245
              fprintf (stderr, "Got back short checksum for load addr\n");
246
              exit (1);
247
            }
248
 
249
          if (checksum != snd_buf[0])
250
            {
251
              fprintf (stderr, "Got back bad checksum for load addr\n");
252
              exit (1);
253
            }
254
          pkt_num = 0;           /* Load addr resets packet seq # */
255
          old_addr = addr;
256
        }
257
 
258
      memcpy (snd_buf + 6, buffer, count);
259
 
260
      checksum = calc_checksum (buffer, count);
261
 
262
      snd_buf[0] = 0x1;          /* Load command */
263
      snd_buf[1] = 0x2;         /* Loading data */
264
      snd_buf[2] = pkt_num >> 8;
265
      snd_buf[3] = pkt_num;
266
      snd_buf[4] = checksum >> 8;
267
      snd_buf[5] = checksum;
268
 
269
      send_buf (fd, snd_buf, count + 6);
270
      cc = recv_buf (fd, snd_buf, sizeof snd_buf, 3);
271
 
272
      if (cc == 0)
273
        {
274
          fprintf (stderr
275
                   , "send_data: timeout sending %d bytes to addr 0x%lx, retrying\n", count, addr);
276
          continue;
277
        }
278
 
279
      if (cc < 1)
280
        {
281
          fprintf (stderr, "Got back short response for load data\n");
282
          exit (1);
283
        }
284
 
285
      if (snd_buf[0] != 0xff)
286
        {
287
          fprintf (stderr, "Got back bad response for load data\n");
288
          exit (1);
289
        }
290
 
291
      old_addr += count;
292
      pkt_num++;
293
 
294
      return;
295
    }
296
}
297
 
298
extern int optind;
299
 
300
int
301
main (argc, argv)
302
     int argc;
303
     char **argv;
304
{
305
  int cc, c;
306
  unsigned char buf[10];
307
  asection *section;
308
  bfd *pbfd;
309
  unsigned long entry;
310
  struct hostent *he;
311
  struct sockaddr_in sockaddr;
312
 
313
  while ((c = getopt(argc, argv, "")) != EOF)
314
    {
315
      switch (c)
316
        {
317
        default:
318
          usage();
319
        }
320
    }
321
  argc -= optind;
322
  argv += optind;
323
 
324
  if (argc != 2)
325
    usage ();
326
 
327
  pbfd = bfd_openr (argv[1], 0);
328
 
329
  if (pbfd == NULL)
330
    sys_error ("Open of PROG failed");
331
 
332
  /* Setup the socket.  Must be raw UDP. */
333
 
334
  he = gethostbyname (argv[2]);
335
 
336
  if (!he)
337
    sys_error ("No such host");
338
 
339
  netfd = socket (PF_INET, SOCK_DGRAM, 0);
340
 
341
  sockaddr.sin_family = PF_INET;
342
  sockaddr.sin_port = htons(7000);
343
  memcpy (&sockaddr.sin_addr.s_addr, he->h_addr, sizeof (struct in_addr));
344
 
345
  if (connect (netfd, &sockaddr, sizeof(sockaddr)))
346
    sys_error ("Connect failed");
347
 
348
  buf[0] = 0x5;
349
  buf[1] = 0;
350
 
351
  send_buf (netfd, buf, 2);     /* Request version */
352
  cc = recv_buf (netfd, buf, sizeof(buf), -1); /* Get response */
353
 
354
  if (cc < 3)
355
    {
356
      fprintf (stderr, "SPARClite appears to be ill\n");
357
      exit (1);
358
    }
359
 
360
  printf ("[SPARClite appears to be alive]\n");
361
 
362
  if (!bfd_check_format (pbfd, bfd_object))
363
    error ("It doesn't seem to be an object file");
364
 
365
  for (section = pbfd->sections; section; section = section->next)
366
    {
367
      if (bfd_get_section_flags (pbfd, section) & SEC_ALLOC)
368
        {
369
          bfd_vma section_address;
370
          unsigned long section_size;
371
          const char *section_name;
372
 
373
          section_name = bfd_get_section_name (pbfd, section);
374
 
375
          section_address = bfd_get_section_vma (pbfd, section);
376
          /* Adjust sections from a.out files, since they don't
377
             carry their addresses with.  */
378
          if (bfd_get_flavour (pbfd) == bfd_target_aout_flavour)
379
            section_address += LOAD_ADDRESS;
380
          section_size = bfd_section_size (pbfd, section);
381
 
382
          printf("[Loading section %s at %lx (%ld bytes)]\n",
383
                 section_name, section_address, section_size);
384
 
385
          /* Text, data or lit */
386
          if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
387
            {
388
              file_ptr fptr;
389
 
390
              fptr = 0;
391
 
392
              while (section_size > 0)
393
                {
394
                  char buffer[1024];
395
                  int count;
396
                  static char inds[] = "|/-\\";
397
                  static int k = 0;
398
 
399
                  count = min (section_size, 1024);
400
 
401
                  bfd_get_section_contents (pbfd, section, buffer, fptr,
402
                                            count);
403
 
404
#if 0
405
                  {
406
                    int i;
407
                    unsigned char checksum;
408
 
409
                    checksum = 0;
410
                    for (i=0; i < count; i++)
411
                      checksum += buffer[i];
412
                  }
413
#endif
414
 
415
                  printf ("\r%c", inds[k++ % 4]);
416
                  fflush (stdout);
417
 
418
                  send_data (buffer, netfd, section_address, count);
419
 
420
                  section_address += count;
421
                  fptr += count;
422
                  section_size -= count;
423
                }
424
            }
425
          else                  /* BSS */
426
            printf ("Not loading BSS \n");
427
        }
428
    }
429
 
430
  entry = bfd_get_start_address (pbfd);
431
 
432
  printf ("[Starting %s at 0x%lx]\n", argv[1], entry);
433
 
434
  buf[0] = 0x3;
435
  buf[1] = 0;
436
  buf[2] = entry >> 24;
437
  buf[3] = entry >> 16;
438
  buf[4] = entry >> 8;
439
  buf[5] = entry;
440
 
441
  send_buf (netfd, buf, 6);     /* Send start addr */
442
  cc = recv_buf (netfd, buf, sizeof(buf), -1); /* Get response */
443
 
444
  if (cc < 1 || buf[0] != 0x55)
445
    {
446
      fprintf (stderr, "Failed to take start address\n");
447
      exit (1);
448
    }
449
 
450
  exit (0);
451
}

powered by: WebSVN 2.1.0

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