/*-=-=-=-=-=-=-=-=-=-=-=- 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.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

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

doimport(kafd,cpu,addr,size)
int kafd;
int cpu;
int addr;
int size;
{
 int i;
 char type;
 struct qballocreq qbrq;
 struct sharedesc shd;

 printf("doimport: addr=%x size=%d\n",addr,size);
 qbrq.size = size;
 qbrq.cpu = cpu;
 if (ioctl(kafd,KAIOC_QBALLOC,&qbrq) < 0)
  { perror("KAIOC_QBALLOC ioctl");
    exit(1);
  }
 shd.size = size;
 shd.addr = (char *)addr;
 shd.qbhandle = qbrq.qbhandle;
 if (ioctl(kafd,KAIOC_IMPORT,&shd) < 0)
  { perror("KAIOC_IMPORT ioctl");
    exit(1);
  }
 shd.size = size;
 shd.addr = (char *)addr;
 shd.qbhandle = qbrq.qbhandle;
 if (ioctl(kafd,KAIOC_SEXPORT,&shd) < 0)
  { perror("KAIOC_SEXPORT ioctl");
    exit(1);
  }
}

doexport(kafd,addr,size)
int kafd;
int addr;
int size;
{
 int i;
 char type;
 struct qballocreq qbrq;
 struct sharedesc shd;

 printf("doexport: addr=%x size=%d\n",addr,size);
 qbrq.size = size;
 qbrq.cpu = 0;
 if (ioctl(kafd,KAIOC_QBALLOC,&qbrq) < 0)
  { perror("KAIOC_QBALLOC ioctl");
    exit(1);
  }
 shd.size = size;
 shd.addr = (char *)addr;
 shd.qbhandle = qbrq.qbhandle;
 if (ioctl(kafd,KAIOC_EXPORT,&shd) < 0)
  { perror("KAIOC_EXPORT ioctl");
    exit(1);
  }
 shd.size = size;
 shd.addr = (char *)addr;
 shd.qbhandle = qbrq.qbhandle;
 if (ioctl(kafd,KAIOC_SIMPORT,&shd) < 0)
  { perror("KAIOC_SIMPORT ioctl");
    exit(1);
  }
}

sendclear(kafd)
int kafd;
{
 if (ioctl(kafd,KAIOC_CLEAR,0) < 0)
  { perror("KAIOC_CLEAR ioctl");
    exit(1);
  }
}

sendtext(kafd)
int kafd;
{
 extern char etext;
 struct blkadesc blad;
 struct blkdesc bld;

 blad.size = (int) &etext;
 blad.haddr = 0;
 blad.saddr = 0;
 if (ioctl(kafd,KAIOC_DOWNLOAD,&blad) < 0)
  { perror("KAIOC_DOWNLOAD ioctl");
    exit(1);
  }
 bld.size = (int) &etext;
 bld.addr = 0;
 if (ioctl(kafd,KAIOC_READONLY,&bld) < 0)
  { perror("KAIOC_READONLY ioctl");
    exit(1);
  }
}

senddata(kafd)
int kafd;
{
 extern char etext;
 extern char end;
 struct blkadesc blad;

 blad.size = ((int)&end) - ((int)&etext);
 blad.haddr = (char *) &etext;
 blad.saddr = blad.haddr;
 if (ioctl(kafd,KAIOC_DOWNLOAD,&blad) < 0)
  { perror("KAIOC_DOWNLOAD ioctl");
    exit(1);
  }
}

sendexec(kafd,entrypt)
int kafd;
int (*entrypt)();
{
 int ep;

 ep = ((int)entrypt) + 2;
 if (ioctl(kafd,KAIOC_START,&ep) < 0)
  { perror("KAIOC_START ioctl");
    exit(1);
  }
}
