home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 573b.lha / FastMax_v3 / FastMax.c < prev    next >
C/C++ Source or Header  |  1991-05-28  |  7KB  |  190 lines

  1. /*============================================================================*/
  2.  
  3. /* FastMax.c version 3,  written by John O'Leary,  90.5.28 */
  4.  
  5. /* Must be compiled so that parameters are passed on the stack */
  6. /* and function return values are passed back in D0 */
  7. /* and size of  char == byte,  short == word,  long == longword */
  8.  
  9. /*============================================================================*/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. FILE  * F;
  15. char  * InFile = "A-Max Startup";
  16. char  * BackupFile = "Original A-Max Startup";
  17. char  * OutFile = "A-Max Fast Startup";
  18. char  * ROMsFile = "Mac ROMs";
  19. unsigned short  far Buffer [40000], far ROMs [1 << 16];
  20. extern far ReadFile;
  21. unsigned long  (* ReadROM) (unsigned short *, long);
  22.   /* ReadROM is a pointer to a function returning an unsigned long value */
  23. unsigned long  ROM_ID, ROMSize, FileSize, Start, Finish, I;
  24.  
  25. void  main (int, char * []);
  26. void  CleanUp (unsigned short int, char *, char *, char *);
  27.  
  28. /*============================================================================*/
  29.  
  30. void main (argc, argv)
  31.  
  32. int  argc;
  33. char  * argv [];
  34.  
  35.    {
  36.    if (argc > 1 & argv [1] [0] == '?' | argc > 3)
  37.       CleanUp (0, "A-Max program patch\nUSAGE: ", argv [0],
  38.        " [Original_filename [Backup_filename]]\n");
  39.    if (argc > 1)  InFile = argv [1];
  40.    if (argc > 2)  BackupFile = argv [2];
  41.  
  42. /*------------------------------------------------------------------------------
  43.     Read original program file
  44. ------------------------------------------------------------------------------*/
  45.  
  46.    if ((F = fopen (InFile, "r")) == 0)
  47.       CleanUp (0, "Couldn't open file '", InFile, "' for input\n");
  48.  
  49.    printf ("Reading original program file '%s'... ", InFile);
  50.    FileSize = fread ((unsigned char *) Buffer, 2, 40000, F);
  51.    fclose (F);
  52.    if (FileSize == 0)
  53.       CleanUp (0, "error.\n", "", "");
  54.    printf ("%ld bytes.\n", FileSize * 2);
  55.  
  56. /*------------------------------------------------------------------------------
  57.    Search for beginning and end of ReadROM routine
  58. ------------------------------------------------------------------------------*/
  59.  
  60.    printf ("Searching program for ReadROM routine... ");
  61.    for (Start = 0;
  62.     Buffer [Start] != 0x48E7 | Buffer [Start+1] != 0x7FFE |
  63.      Buffer [Start+2] != 0x7804 | Buffer [Start+3] != 0x206F;
  64.     Start ++)
  65.       if (Start >= FileSize-100)
  66.          CleanUp (0, "not found.\n", "", "");
  67.    /* Start is index of first word of ReadROM routine */
  68.  
  69.    for (Finish = Start+10;
  70.     Buffer [Finish-3] != 0xE3EB | Buffer [Finish-2] != 0xE7EF |
  71.      Buffer [Finish-1] != 0xF3FB | Buffer [Finish] != 0xF7FF;
  72.     Finish ++)
  73.       if (Finish >= FileSize-1)
  74.          CleanUp (0, "not found.\n", "", "");
  75.    /* Finish is index of word immediately following ReadROM routine */
  76.  
  77. /*------------------------------------------------------------------------------
  78.    Patch (DiskResource -> dr_Flags) handling in ReadROM routine
  79.    of A-Max version 1 for compatibility with SetPatch
  80. ------------------------------------------------------------------------------*/
  81.  
  82.    for (I = Start;  I < Finish;  I ++)
  83.       if (Buffer [I] == 0x08E9 & Buffer [I+1] == 0x0007)
  84.          Buffer [I] = 0x0829;  /* replace BSET with BTST */
  85.    printf ("found.\n");
  86.  
  87. /*------------------------------------------------------------------------------
  88.    Call original ReadROM routine with pointer to ROMs buffer and
  89.    zero longword (needed by ReadROM of A-Max version > 2.0) on the stack
  90.    Amigas with MMUs may not like this!
  91. ------------------------------------------------------------------------------*/
  92.  
  93.    printf ("WARNING:  Interrupts will be disabled for up to 30 seconds... Press RETURN");
  94.    getchar ();
  95.    printf ("Looking for Macintosh ROMs... ");
  96.    ReadROM = (unsigned long (*) ()) & Buffer [Start];
  97.    ROM_ID = ReadROM (ROMs, 0L);
  98.  
  99. /*------------------------------------------------------------------------------
  100.    The ROM ID is returned in D0
  101.    High word == 0x69 for 64K ROMs, 0x75 for 128K ROMs
  102.    Low word == bit number for drive SEL
  103. ------------------------------------------------------------------------------*/
  104.  
  105.    if (ROM_ID >> 16 == 0x69)
  106.       ROMSize = 1 << 15  /* 32 Kwords */;
  107.    else if (ROM_ID >> 16 == 0x75)
  108.       ROMSize = 1 << 16  /* 64 Kwords */;
  109.    else
  110.          CleanUp (0, "not found.\n", "", "");
  111.    printf ("%dK ROMs found in drive number %d\n",
  112.     (short) (ROMSize >> 9), (short) ROM_ID - 3);
  113.  
  114. /*------------------------------------------------------------------------------
  115.    Write ROM ID and contents of ROMs to file
  116. ------------------------------------------------------------------------------*/
  117.  
  118.    if ((F = fopen (ROMsFile, "w")) == 0)
  119.       CleanUp (0, "couldn't open file '", ROMsFile, "' for output\n");
  120.    printf ("Writing contents of ROMs to file '%s'... ", ROMsFile);
  121.    if (fwrite ((unsigned char *) & ROM_ID, 4, 1, F) != 1)
  122.       CleanUp (1, "error.\n", "", "");
  123.    if (fwrite ((unsigned char *) ROMs, 2, ROMSize, F) != ROMSize)
  124.       CleanUp (1, "error.\n", "", "");
  125.    fclose (F);
  126.    printf ("done.\n");
  127.  
  128. /*------------------------------------------------------------------------------
  129.    Write new program file, with ReadROM replaced by ReadFile routine
  130. ------------------------------------------------------------------------------*/
  131.  
  132.    if ((F = fopen (OutFile, "w")) == 0)
  133.       CleanUp (0, "couldn't open file '", OutFile, "' for output\n");
  134.    printf ("Writing patched program file '%s'... ", OutFile);
  135.    if (fwrite ((unsigned char *) Buffer, 2, Start, F) != Start)
  136.       CleanUp (2, "error.\n", "", "");
  137.    if (fwrite ((unsigned char *) & ReadFile, 2, Finish - Start, F) != Finish - Start)
  138.       CleanUp (2, "error.\n", "", "");
  139.    if (fwrite ((unsigned char *) & Buffer [Finish], 2, FileSize - Finish, F) != FileSize - Finish)
  140.       CleanUp (2, "error.\n", "", "");
  141.    printf ("done.\n");
  142.    fclose (F);
  143.  
  144. /*------------------------------------------------------------------------------
  145.    Backup original program file
  146. ------------------------------------------------------------------------------*/
  147.  
  148.    printf ("Backing up original '%s' to '%s'... ", InFile, BackupFile);
  149.    remove (BackupFile);
  150.    if (rename (InFile, BackupFile) != 0)
  151.       CleanUp (0, "couldn't rename file.\n", "", "");
  152.    printf ("done.\n");
  153.  
  154. /*------------------------------------------------------------------------------
  155.    Rename patched program file
  156. ------------------------------------------------------------------------------*/
  157.  
  158.    printf ("Renaming new file '%s' as '%s'... ", OutFile, InFile);
  159.    if (rename (OutFile, InFile) != 0)
  160.       CleanUp (0, "couldn't rename file.\n", "", "");
  161.    printf ("done.\n");
  162.    }
  163.  
  164. /*============================================================================*/
  165.  
  166. void CleanUp (Level, Message1, Message2, Message3)
  167.  
  168.    unsigned short int  Level;
  169.    char  * Message1, * Message2, * Message3;
  170.  
  171.    {
  172.    printf (Message1);
  173.    printf (Message2);
  174.    printf (Message3);
  175.    switch (Level)
  176.       {
  177.       case (2):
  178.          fclose (F);
  179.          remove (OutFile);
  180.          printf ("%s deleted.\n", OutFile);
  181.          break;
  182.       case (1):
  183.          fclose (F);
  184.          remove (ROMsFile);
  185.          printf ("%s deleted.\n", ROMsFile);
  186.          break;
  187.       }
  188.    exit (20 + Level);
  189.    }
  190.