/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by Mike Parker.			    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

/*
 * Usage:
 *
 *	sboot <cpu> <file>
 *
 * <cpu> is 1, 2, or 3 indicating one of the three possible auxiliary
 *  cpus.  <file> is a file containing the data to be executed via the
 *  PROM bootstrap mechanism.
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>

#include "../sys/rciext/kaioctl.h"

char *sbrk();

extern char **argvec;

#define MINCPU 1
#define MAXCPU 3

int cpu;
char *bootarea;
int bootfd;
char *bootfn;
char kadev[64];
char kafmt[] = "/dev/ka%d";
int kafd;
struct stat stb;
int bootsize;
struct blkdesc bootdesc;

main(ac,av)
int ac;
char **av;
{
 int i;

 if (ac != 3)
  { fprintf(stderr,"Usage: %s <cpu> <bootfile>\n",argvec[0]);
    exit(1);
  }
 cpu = xatoi(av[1]);
 sprintf(kadev,kafmt,cpu);
 kafd = open(kadev,O_RDWR,0);
 if (kafd < 0)
  { fprintf(stderr,"%s: can't open %s: ",argvec[0],kadev);
    perror((char *)0);
    exit(1);
  }
 bootfn = av[2];
 bootfd = open(bootfn,O_RDONLY,0);
 if (bootfd < 0)
  { fprintf(stderr,"%s: can't open %s: ",argvec[0],bootfn);
    perror((char *)0);
    exit(1);
  }
 if (fstat(bootfd,&stb) < 0)
  { fprintf(stderr,"%s: can't fstat %s: ",argvec[0],bootfn);
    perror((char *)0);
    exit(1);
  }
 bootsize = stb.st_size;
 bootarea = sbrk(bootsize);
 if (bootarea == (char *)-1)
  { fprintf(stderr,"%s: can't allocate boot area memory: ",argvec[0]);
    perror((char *)0);
    exit(1);
  }
 if (read(bootfd,bootarea,bootsize) != bootsize)
  { fprintf(stderr,"%s: can't read entire boot\n",argvec[0]);
    exit(1);
  }
 bootdesc.size = bootsize;
 bootdesc.addr = bootarea;
 if (ioctl(kafd,KAIOC_BOOT,&bootdesc) < 0)
  { perror("KAIOC_BOOT ioctl");
    exit(1);
  }
 exit(0);
}
