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