home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 426.lha / ZKick_v2.10 / zkick.c < prev    next >
C/C++ Source or Header  |  1990-10-03  |  7KB  |  328 lines

  1. /*
  2.  
  3.     ZKick V2.10b -- Copyright (C) 1990 by Daniel Zenchelsky
  4.  
  5.         This program may be freely copied, as long as all copyright
  6.         notices are left intact and unchanged.
  7.  
  8. */
  9.  
  10. /* #define DEBUG */
  11.  
  12. #include <stdio.h>
  13.  
  14. #include <libraries/configvars.h>
  15. #include <libraries/expansion.h>
  16. #include <exec/types.h>
  17. #include <exec/memory.h>
  18. #include <exec/tasks.h>
  19. #include <exec/execbase.h>
  20.  
  21. #define BUFSIZE     (64*1024)
  22. #define KICKSIZE     (512*1024)
  23. #define NUMBUFS     (KICKSIZE/BUFSIZE)
  24.  
  25. #define STARTKICK    (void *)0x200000
  26. #define ENDKICK    (void *)0x27FFFF
  27.  
  28. #define STACK_SIZE 1024
  29.  
  30. #define BOGUSMMU    0xffffffffL
  31.  
  32.  
  33. /* For the Assembly routines to use: */
  34.  
  35. long NumBufs=NUMBUFS;
  36. long BufSize=BUFSIZE;
  37. long StartKick=STARTKICK;
  38. long EndKick=ENDKICK;
  39.  
  40. APTR stack = NULL;
  41. struct Task *tc = NULL;
  42. char *taskname = "Zkick Task";
  43.  
  44. struct ExpansionBase *ExpansionBase=NULL;
  45. struct ExecBase *ExecBase=NULL;
  46.  
  47. void main();
  48. void cleanup();
  49. void DoAddCD();
  50. void GetMem();
  51. void KickLoad();
  52. void StartTask();
  53. extern void KickCopy();
  54. extern void MakeRomTag();
  55. extern long GetCPUType();
  56. extern long GetMMUType();
  57. extern long connum;
  58. extern struct ConfigDev config[8];
  59. extern long memnum;
  60. extern long memory[8][2];
  61. extern long Survive;
  62. extern long MMU;
  63. long CPU;
  64.  
  65. int CXBRK(void) { return(0); }  
  66.  
  67. void *MemArray[NUMBUFS];
  68.  
  69. void main(argc,argv)
  70. int argc;
  71. char *argv[];
  72. {
  73.     struct ConfigDev *MyConfigDev;
  74.     char *kickfile;
  75.  
  76.     printf("ZKick V2.10b Copyright (C) 1990 by Daniel Zenchelsky\n");
  77.     printf("----------------------------------------------------\n");
  78.     printf("      This program may be freely copied, as long\n");
  79.     printf("      as all copyright notices are left intact.\n\n");
  80.     connum=0;    
  81.  
  82.     if ((argc<2) || (argc>2 && strcmp(argv[1],"-die")!=0))
  83.     {
  84.         printf("Usage: ZKick [-die] KickFile\n");
  85.         cleanup();
  86.     }
  87.  
  88.     if (strcmp(argv[1],"-die")==0)
  89.     {
  90.         Survive=0;
  91.         kickfile=argv[2];
  92.     }
  93.         else
  94.     {
  95.         Survive=1;
  96.         kickfile=argv[1];
  97.     }
  98.  
  99.     ExpansionBase= (struct ExpansionBase *)OpenLibrary( EXPANSIONNAME,0);
  100.     if(ExpansionBase==NULL)
  101.     {
  102.         printf("Error opening expansion library!!\n");
  103.         cleanup();
  104.     }
  105.  
  106.     ExecBase= (struct ExecBase *)OpenLibrary("exec.library",0);
  107.     if(ExecBase==NULL)
  108.     {
  109.         printf("Error opening Exec library!!\n");
  110.         cleanup();
  111.     }
  112.  
  113.     if((stack = (APTR) AllocMem(STACK_SIZE, MEMF_CHIP | MEMF_CLEAR)) == NULL)
  114.     {
  115.          printf("Not enough memory for task stack\n");
  116.         cleanup();
  117.     }
  118.  
  119.     if ((tc = (struct Task *)
  120.         AllocMem(sizeof(struct Task),MEMF_CHIP | MEMF_CLEAR | MEMF_PUBLIC)) == NULL)
  121.     {
  122.         printf("Not enough memory for task structure\n");
  123.         cleanup();
  124.     }
  125.  
  126.     if (CheckForMem()==-1)
  127.     {
  128.         printf("You must have a 512k or greater memory board at $200000.\n");
  129.         cleanup();
  130.     }
  131.  
  132.     MyConfigDev=NULL;
  133.  
  134.     MyConfigDev=FindConfigDev(NULL,-1,-1);
  135.     
  136.     if(MyConfigDev==NULL) printf("No configured devices found\n");
  137.         else DoAddCD(MyConfigDev);
  138.  
  139.     while((MyConfigDev=FindConfigDev(MyConfigDev,-1,-1))!=NULL)
  140.     {
  141.         DoAddCD(MyConfigDev);
  142.     }
  143.  
  144.     printf("Found %d expansion devices, and %d memory boards.\n",connum,memnum);
  145.  
  146.     CPU=GetCPUType();
  147.     MMU=GetMMUType();
  148.  
  149.     if (MMU==BOGUSMMU) MMU=0;
  150.  
  151. #ifdef DEBUG
  152.     printf("CPU: %d, MMU: %d\n",CPU,MMU);
  153. #endif 
  154.     
  155.     GetMem();
  156.     KickLoad(kickfile);
  157.     MakeRomTag();
  158.     printf("Rebooting in 5 seconds...\n");
  159.     Delay(50*5);
  160.     StartTask();
  161.  
  162.     Wait(0L);        /* Hang around forever (not very long, actually.) */
  163. }
  164.  
  165. void cleanup()
  166. {
  167.     if(stack) FreeMem((void *)stack,(ULONG)STACK_SIZE);
  168.     if(ExpansionBase) CloseLibrary(ExpansionBase);
  169.     if(ExecBase) CloseLibrary(ExecBase);
  170.     exit(0);
  171. }
  172.  
  173. void DoAddCD(dev)
  174. struct ConfigDev *dev;
  175. {
  176.     if ((dev->cd_Rom.er_Type & ERTF_MEMLIST) == 0)
  177.     {
  178.         config[connum]=*dev;
  179.         config[connum].cd_Rom.er_Reserved0c=0;
  180.         config[connum].cd_Rom.er_Reserved0d=0;
  181.         config[connum].cd_Rom.er_Reserved0e=0;
  182.         config[connum].cd_Rom.er_Reserved0f=0;
  183.         config[connum].cd_Flags |= CDF_CONFIGME;
  184.         config[connum].cd_Driver=NULL;
  185.  
  186. #ifdef DEBUG
  187.         printf("Adding board at $%x to list.\n",(long)(dev->cd_BoardAddr));
  188. #endif
  189.         if (connum<8) connum++;
  190.             else
  191.         {
  192.             printf("Too many expansion boards!\n");
  193.             cleanup();
  194.         }
  195.     }
  196.         else
  197.     {
  198.         memory[memnum][0]=(long)dev->cd_BoardAddr;
  199.         memory[memnum][1]=(long)dev->cd_BoardSize;
  200.  
  201.         if (memory[memnum][0]==0x200000)
  202.         {
  203.             memory[memnum][0]+=0x080000;
  204.             memory[memnum][1]-=0x080000;
  205.         }
  206.  
  207.         if (memory[memnum][1]>0)
  208.         {
  209.  
  210. #ifdef DEBUG
  211.             printf("Adding memory at $%x, size $%x to list.\n",
  212.                 memory[memnum][0],memory[memnum][1]);
  213. #endif
  214.             if (memnum<8) memnum++;
  215.                 else
  216.             {
  217.                 printf("Too many memory boards!\n");
  218.                 cleanup();
  219.             }
  220.         }
  221.     }
  222. }
  223.  
  224. /* Allocate NUMBUFS (8) buffers of size BUFSIZE (64k) _NOT_ within the final
  225.    KickStart address region.  Yeah, yeah, I know I should have just written
  226.    a memory copy routine which could handle overlap, but... 
  227. */
  228.  
  229. void GetMem()
  230. {
  231.     int BufNum,BadNum=0,x;
  232.     void *MemBad[NUMBUFS];
  233.  
  234.     for (BufNum=0;BufNum<NUMBUFS;BufNum++)
  235.     {
  236.         MemArray[BufNum]=(void *)AllocMem(BUFSIZE,0);
  237.         while (MemArray[BufNum]!=(void *)NULL && MemArray[BufNum]>=STARTKICK && MemArray[BufNum]<=ENDKICK)
  238.         {
  239.             MemBad[BadNum++]=MemArray[BufNum];    
  240. #ifdef DEBUG
  241.             printf("* UNUSED * Memory allocated at location $%x\n",(long)MemBad[BadNum-1]);
  242. #endif
  243.             MemArray[BufNum]=(void *)AllocMem(BUFSIZE,0);
  244.         }
  245.         if (MemArray[BufNum]==(void *)NULL)
  246.         {
  247.             if (BufNum>0)
  248.                 for (x=0;x<BufNum-1;x++) FreeMem(MemArray[x],BUFSIZE);
  249.             if (BadNum>0)
  250.                 for (x=0;x<BadNum-1;x++) FreeMem(MemBad[x],BUFSIZE);
  251.             printf("Couldn't allocate memory.\n");
  252.             cleanup();
  253.         }
  254. #ifdef DEBUG
  255.         printf("Memory allocated at location $%x\n",(long)MemArray[BufNum]);
  256. #endif
  257.     }
  258.  
  259. /* Free memory that was allocated inside KickStart area */
  260.  
  261.     if (BadNum>0)
  262.         for (x=0;x<BadNum-1;x++) FreeMem(MemBad[x],BUFSIZE);
  263.  
  264. }
  265.  
  266. void KickLoad(filename)
  267. char filename[];
  268. {
  269.     int BufNum;
  270.     int file;
  271.  
  272.     file=open(filename,0);
  273.  
  274.     if (file==-1)
  275.     {
  276.         printf("Error opening %s\n",filename);
  277.         for (BufNum=0;BufNum<NUMBUFS;BufNum++) FreeMem(MemArray[BufNum],(ULONG)BUFSIZE);
  278.         cleanup();
  279.     }
  280.  
  281.     lseek(file,8,0);
  282.  
  283.     for (BufNum=0;BufNum<NUMBUFS;BufNum++)
  284.     {
  285. #ifdef DEBUG
  286.         printf("Reading %d\n",BufNum*BUFSIZE);
  287. #endif
  288.         if (read(file,MemArray[BufNum],BUFSIZE)!=BUFSIZE)
  289.         {
  290.             printf("Error reading %s\n",filename);
  291.             for (BufNum=0;BufNum<NUMBUFS;BufNum++) FreeMem(MemArray[BufNum],(ULONG)BUFSIZE);
  292.             cleanup();
  293.         }
  294.     }
  295. }
  296.  
  297. void StartTask()
  298. {
  299.     /* Initialize necessary fields, others were cleared by MEMF_CLEAR */
  300.     tc->tc_Node.ln_Type = NT_TASK;
  301.     tc->tc_Node.ln_Name = taskname;
  302.     tc->tc_SPLower = (APTR)stack;
  303.     tc->tc_SPUpper = (APTR)(STACK_SIZE + (ULONG)stack);
  304.     tc->tc_SPReg   = tc->tc_SPUpper;
  305.  
  306.     AddTask(tc, KickCopy, 0L);
  307. }
  308.  
  309. CheckForMem()
  310. {
  311.    struct MemHeader  *mem;
  312.    BOOL flag=FALSE;
  313.  
  314.    Forbid();
  315.    for (mem = (struct MemHeader *)ExecBase->MemList.lh_Head;
  316.         mem->mh_Node.ln_Succ;
  317.         mem = (struct MemHeader *)mem->mh_Node.ln_Succ)
  318.     {
  319.         if (((void *) mem->mh_Lower == STARTKICK) && ((void *) mem->mh_Upper >= ENDKICK))
  320.             flag=TRUE;
  321.     }
  322.    Permit();
  323.  
  324.    if (flag=TRUE) return(0);
  325.     else return(-1);
  326. }
  327.  
  328.