/* Program to load an image into the SPARClite monitor board
|
/* Program to load an image into the SPARClite monitor board
|
Copyright 1993, 1994, 1995 Free Software Foundation, Inc.
|
Copyright 1993, 1994, 1995 Free Software Foundation, Inc.
|
|
|
This program is free software; you can redistribute it and/or modify
|
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
|
it under the terms of the GNU General Public License as published by
|
the Free Software Foundation; either version 2 of the License, or
|
the Free Software Foundation; either version 2 of the License, or
|
(at your option) any later version.
|
(at your option) any later version.
|
|
|
This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU General Public License for more details.
|
GNU General Public License for more details.
|
|
|
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
along with this program; if not, write to the Free Software
|
along with this program; if not, write to the Free Software
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
|
|
/* Call with:
|
/* Call with:
|
|
|
aload PROG TTY
|
aload PROG TTY
|
|
|
ie: aload hello /dev/ttya
|
ie: aload hello /dev/ttya
|
|
|
*/
|
*/
|
|
|
#include <stdio.h>
|
#include <stdio.h>
|
|
|
#include "ansidecl.h"
|
#include "ansidecl.h"
|
|
|
#ifdef ANSI_PROTOTYPES
|
#ifdef ANSI_PROTOTYPES
|
#include <stdarg.h>
|
#include <stdarg.h>
|
#else
|
#else
|
#include <varargs.h>
|
#include <varargs.h>
|
#endif
|
#endif
|
|
|
#include "libiberty.h"
|
#include "libiberty.h"
|
#include "bfd.h"
|
#include "bfd.h"
|
|
|
#include <errno.h>
|
#include <errno.h>
|
#include <unistd.h>
|
#include <unistd.h>
|
#include <fcntl.h>
|
#include <fcntl.h>
|
|
|
#ifndef HAVE_TERMIOS
|
#ifndef HAVE_TERMIOS
|
you lose
|
you lose
|
#endif
|
#endif
|
|
|
#if defined(HAVE_TERMIOS)
|
#if defined(HAVE_TERMIOS)
|
#include <termios.h>
|
#include <termios.h>
|
#elif defined(HAVE_TERMIO)
|
#elif defined(HAVE_TERMIO)
|
#include <termio.h>
|
#include <termio.h>
|
#elif defined(HAVE_SGTTY)
|
#elif defined(HAVE_SGTTY)
|
#include <sgtty.h>
|
#include <sgtty.h>
|
#endif
|
#endif
|
|
|
#define min(A, B) (((A) < (B)) ? (A) : (B))
|
#define min(A, B) (((A) < (B)) ? (A) : (B))
|
|
|
/* Where the code goes by default. */
|
/* Where the code goes by default. */
|
|
|
#ifndef LOAD_ADDRESS
|
#ifndef LOAD_ADDRESS
|
#define LOAD_ADDRESS 0x40000000
|
#define LOAD_ADDRESS 0x40000000
|
#endif
|
#endif
|
|
|
int quiet = 0;
|
int quiet = 0;
|
|
|
static void
|
static void
|
usage ()
|
usage ()
|
{
|
{
|
fprintf (stderr, "usage: aload [-q] file device\n");
|
fprintf (stderr, "usage: aload [-q] file device\n");
|
exit (1);
|
exit (1);
|
}
|
}
|
|
|
static void
|
static void
|
#ifdef ANSI_PROTOTYPES
|
#ifdef ANSI_PROTOTYPES
|
sys_error (char *msg, ...)
|
sys_error (char *msg, ...)
|
#else
|
#else
|
sys_error (va_alist)
|
sys_error (va_alist)
|
va_dcl
|
va_dcl
|
#endif
|
#endif
|
{
|
{
|
int e = errno;
|
int e = errno;
|
va_list args;
|
va_list args;
|
|
|
#ifdef ANSI_PROTOTYPES
|
#ifdef ANSI_PROTOTYPES
|
va_start (args, msg);
|
va_start (args, msg);
|
#else
|
#else
|
va_start (args);
|
va_start (args);
|
#endif
|
#endif
|
|
|
#ifdef ANSI_PROTOTYPES
|
#ifdef ANSI_PROTOTYPES
|
vfprintf (stderr, msg, args);
|
vfprintf (stderr, msg, args);
|
#else
|
#else
|
{
|
{
|
char *msg1;
|
char *msg1;
|
|
|
msg1 = va_arg (args, char *);
|
msg1 = va_arg (args, char *);
|
vfprintf (stderr, msg1, args);
|
vfprintf (stderr, msg1, args);
|
}
|
}
|
#endif
|
#endif
|
va_end (args);
|
va_end (args);
|
|
|
fprintf (stderr, ": %s\n", strerror(e));
|
fprintf (stderr, ": %s\n", strerror(e));
|
exit (1);
|
exit (1);
|
}
|
}
|
|
|
static void
|
static void
|
#ifdef ANSI_PROTOTYPES
|
#ifdef ANSI_PROTOTYPES
|
error (char *msg, ...)
|
error (char *msg, ...)
|
#else
|
#else
|
error (va_alist)
|
error (va_alist)
|
va_dcl
|
va_dcl
|
#endif
|
#endif
|
{
|
{
|
va_list args;
|
va_list args;
|
|
|
#ifdef ANSI_PROTOTYPES
|
#ifdef ANSI_PROTOTYPES
|
va_start (args, msg);
|
va_start (args, msg);
|
#else
|
#else
|
va_start (args);
|
va_start (args);
|
#endif
|
#endif
|
|
|
#ifdef ANSI_PROTOTYPES
|
#ifdef ANSI_PROTOTYPES
|
vfprintf (stderr, msg, args);
|
vfprintf (stderr, msg, args);
|
#else
|
#else
|
{
|
{
|
char *msg1;
|
char *msg1;
|
|
|
msg1 = va_arg (args, char *);
|
msg1 = va_arg (args, char *);
|
vfprintf (stderr, msg1, args);
|
vfprintf (stderr, msg1, args);
|
}
|
}
|
#endif
|
#endif
|
va_end (args);
|
va_end (args);
|
|
|
fputc ('\n', stderr);
|
fputc ('\n', stderr);
|
exit (1);
|
exit (1);
|
}
|
}
|
|
|
static int ttyfd;
|
static int ttyfd;
|
|
|
static void
|
static void
|
sendex (outtxt, outlen, intxt, inlen, id)
|
sendex (outtxt, outlen, intxt, inlen, id)
|
unsigned char *outtxt;
|
unsigned char *outtxt;
|
int outlen;
|
int outlen;
|
unsigned char *intxt;
|
unsigned char *intxt;
|
int inlen;
|
int inlen;
|
char *id;
|
char *id;
|
{
|
{
|
char buf[100];
|
char buf[100];
|
int cc;
|
int cc;
|
|
|
if (outlen > 0)
|
if (outlen > 0)
|
{
|
{
|
cc = write (ttyfd, outtxt, outlen);
|
cc = write (ttyfd, outtxt, outlen);
|
if (cc != outlen)
|
if (cc != outlen)
|
sys_error ("Write %s failed", id);
|
sys_error ("Write %s failed", id);
|
}
|
}
|
|
|
if (inlen > 0)
|
if (inlen > 0)
|
{
|
{
|
cc = read (ttyfd, buf, inlen); /* Get reply */
|
cc = read (ttyfd, buf, inlen); /* Get reply */
|
if (cc != inlen)
|
if (cc != inlen)
|
sys_error ("Read %s reply failed", id);
|
sys_error ("Read %s reply failed", id);
|
if (bcmp (buf, intxt, inlen) != 0)
|
if (bcmp (buf, intxt, inlen) != 0)
|
error ("Bad reply to %s", id);
|
error ("Bad reply to %s", id);
|
}
|
}
|
}
|
}
|
|
|
extern int optind;
|
extern int optind;
|
|
|
int
|
int
|
main (argc, argv)
|
main (argc, argv)
|
int argc;
|
int argc;
|
char **argv;
|
char **argv;
|
{
|
{
|
struct termios termios;
|
struct termios termios;
|
asection *section;
|
asection *section;
|
bfd *pbfd;
|
bfd *pbfd;
|
unsigned long entry;
|
unsigned long entry;
|
int c;
|
int c;
|
|
|
while ((c = getopt (argc, argv, "q")) != EOF)
|
while ((c = getopt (argc, argv, "q")) != EOF)
|
{
|
{
|
switch (c)
|
switch (c)
|
{
|
{
|
case 'q':
|
case 'q':
|
quiet = 1;
|
quiet = 1;
|
break;
|
break;
|
default:
|
default:
|
usage();
|
usage();
|
}
|
}
|
}
|
}
|
argc -= optind;
|
argc -= optind;
|
argv += optind;
|
argv += optind;
|
|
|
if (argc != 2)
|
if (argc != 2)
|
usage();
|
usage();
|
|
|
pbfd = bfd_openr (argv[0], 0);
|
pbfd = bfd_openr (argv[0], 0);
|
|
|
if (pbfd == NULL)
|
if (pbfd == NULL)
|
sys_error ("Open of PROG failed");
|
sys_error ("Open of PROG failed");
|
|
|
/* setup the tty. Must be raw, no flow control, 9600 baud */
|
/* setup the tty. Must be raw, no flow control, 9600 baud */
|
|
|
ttyfd = open (argv[1], O_RDWR);
|
ttyfd = open (argv[1], O_RDWR);
|
if (ttyfd == -1)
|
if (ttyfd == -1)
|
sys_error ("Open of TTY failed");
|
sys_error ("Open of TTY failed");
|
|
|
if (tcgetattr(ttyfd, &termios))
|
if (tcgetattr(ttyfd, &termios))
|
sys_error ("tcgetattr failed");
|
sys_error ("tcgetattr failed");
|
|
|
termios.c_iflag = 0;
|
termios.c_iflag = 0;
|
termios.c_oflag = 0;
|
termios.c_oflag = 0;
|
termios.c_cflag = CS8 | CREAD | CLOCAL;
|
termios.c_cflag = CS8 | CREAD | CLOCAL;
|
termios.c_lflag = 0;
|
termios.c_lflag = 0;
|
termios.c_cc[VMIN] = 1;
|
termios.c_cc[VMIN] = 1;
|
termios.c_cc[VTIME] = 0;
|
termios.c_cc[VTIME] = 0;
|
|
|
if (cfsetospeed (&termios, B9600)
|
if (cfsetospeed (&termios, B9600)
|
|| cfsetispeed (&termios, B9600))
|
|| cfsetispeed (&termios, B9600))
|
sys_error ("cfset{i|o}speed failed");
|
sys_error ("cfset{i|o}speed failed");
|
|
|
if (tcsetattr (ttyfd, TCSANOW, &termios))
|
if (tcsetattr (ttyfd, TCSANOW, &termios))
|
sys_error ("tcsetattr failed");
|
sys_error ("tcsetattr failed");
|
|
|
/* The char is documented as 0xaa, \252 is portable octal form. */
|
/* The char is documented as 0xaa, \252 is portable octal form. */
|
sendex("", 1, "\252", 1, "alive?");
|
sendex("", 1, "\252", 1, "alive?");
|
sendex ("U", 1, "U", 1, "alive");
|
sendex ("U", 1, "U", 1, "alive");
|
if (!quiet)
|
if (!quiet)
|
printf ("[SPARClite appears to be alive]\n");
|
printf ("[SPARClite appears to be alive]\n");
|
|
|
if (!bfd_check_format (pbfd, bfd_object))
|
if (!bfd_check_format (pbfd, bfd_object))
|
error ("It doesn't seem to be an object file");
|
error ("It doesn't seem to be an object file");
|
|
|
for (section = pbfd->sections; section; section = section->next)
|
for (section = pbfd->sections; section; section = section->next)
|
{
|
{
|
if (bfd_get_section_flags (pbfd, section) & SEC_ALLOC)
|
if (bfd_get_section_flags (pbfd, section) & SEC_ALLOC)
|
{
|
{
|
bfd_vma section_address;
|
bfd_vma section_address;
|
unsigned long section_size;
|
unsigned long section_size;
|
const char *section_name;
|
const char *section_name;
|
|
|
section_name = bfd_get_section_name (pbfd, section);
|
section_name = bfd_get_section_name (pbfd, section);
|
|
|
section_address = bfd_get_section_vma (pbfd, section);
|
section_address = bfd_get_section_vma (pbfd, section);
|
/* Adjust sections from a.out files, since they don't
|
/* Adjust sections from a.out files, since they don't
|
carry their addresses with. */
|
carry their addresses with. */
|
if (bfd_get_flavour (pbfd) == bfd_target_aout_flavour)
|
if (bfd_get_flavour (pbfd) == bfd_target_aout_flavour)
|
section_address += LOAD_ADDRESS;
|
section_address += LOAD_ADDRESS;
|
section_size = bfd_section_size (pbfd, section);
|
section_size = bfd_section_size (pbfd, section);
|
|
|
if (!quiet)
|
if (!quiet)
|
printf ("[Loading section %s at %lx (%ld bytes)]\n",
|
printf ("[Loading section %s at %lx (%ld bytes)]\n",
|
section_name, section_address, section_size);
|
section_name, section_address, section_size);
|
|
|
/* Text, data or lit */
|
/* Text, data or lit */
|
if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
|
if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
|
{
|
{
|
file_ptr fptr;
|
file_ptr fptr;
|
|
|
fptr = 0;
|
fptr = 0;
|
|
|
while (section_size > 0)
|
while (section_size > 0)
|
{
|
{
|
char buffer[1024];
|
char buffer[1024];
|
int count, i;
|
int count, i;
|
unsigned char checksum;
|
unsigned char checksum;
|
static char inds[] = "|/-\\";
|
static char inds[] = "|/-\\";
|
static int k = 0;
|
static int k = 0;
|
|
|
count = min (section_size, 1024);
|
count = min (section_size, 1024);
|
|
|
bfd_get_section_contents (pbfd, section, buffer, fptr,
|
bfd_get_section_contents (pbfd, section, buffer, fptr,
|
count);
|
count);
|
|
|
checksum = 0;
|
checksum = 0;
|
for (i = 0; i < count; i++)
|
for (i = 0; i < count; i++)
|
checksum += buffer[i];
|
checksum += buffer[i];
|
|
|
if (!quiet)
|
if (!quiet)
|
{
|
{
|
printf ("\r%c", inds[k++ % 4]);
|
printf ("\r%c", inds[k++ % 4]);
|
fflush (stdout);
|
fflush (stdout);
|
}
|
}
|
|
|
sendex ("\001", 1, "Z", 1, "load command");
|
sendex ("\001", 1, "Z", 1, "load command");
|
sendex (§ion_address, 4, NULL, 0, "load address");
|
sendex (§ion_address, 4, NULL, 0, "load address");
|
sendex (&count, 4, NULL, 0, "program size");
|
sendex (&count, 4, NULL, 0, "program size");
|
sendex (buffer, count, &checksum, 1, "program");
|
sendex (buffer, count, &checksum, 1, "program");
|
|
|
section_address += count;
|
section_address += count;
|
fptr += count;
|
fptr += count;
|
section_size -= count;
|
section_size -= count;
|
}
|
}
|
}
|
}
|
else /* BSS */
|
else /* BSS */
|
{
|
{
|
if (!quiet)
|
if (!quiet)
|
printf ("Not loading BSS \n");
|
printf ("Not loading BSS \n");
|
}
|
}
|
}
|
}
|
}
|
}
|
|
|
entry = bfd_get_start_address (pbfd);
|
entry = bfd_get_start_address (pbfd);
|
|
|
if (!quiet)
|
if (!quiet)
|
printf ("[Starting %s at 0x%lx]\n", argv[0], entry);
|
printf ("[Starting %s at 0x%lx]\n", argv[0], entry);
|
|
|
sendex ("\003", 1, NULL, 0, "exec command");
|
sendex ("\003", 1, NULL, 0, "exec command");
|
sendex (&entry, 4, "U", 1, "program start");
|
sendex (&entry, 4, "U", 1, "program start");
|
|
|
exit (0);
|
exit (0);
|
}
|
}
|
|
|