home *** CD-ROM | disk | FTP | other *** search
/ The Best of Mecomp Multimedia 2 / MECOMP-CD-II.iso / amiga / tools / disk / format64 / source / dofullformat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-10  |  5.1 KB  |  184 lines

  1. #include <exec/types.h>
  2. #include <exec/exec.h>
  3. #include <intuition/intuition.h>
  4. #include <dos/dos.h>
  5. #include <dos/filehandler.h>
  6. #include <workbench/startup.h>
  7. #include <libraries/gadtools.h>
  8. #include <workbench/icon.h>
  9. #include <devices/trackdisk.h>
  10. #include <dos/rdargs.h>
  11.  
  12. #include <proto/exec.h>
  13. #include <proto/intuition.h>
  14. #include <proto/dos.h>
  15. #include <proto/gadtools.h>
  16. #include <proto/icon.h>
  17. #include <proto/graphics.h>
  18.  
  19. #include "Format.h"
  20. #include "string.h"
  21. #include "stdio.h"
  22.  
  23. /* External symbols */
  24.  
  25. extern BOOL FFS = FALSE;
  26. extern BOOL QuickFmt = FALSE;
  27. extern BOOL Verify = TRUE;
  28. extern BOOL Icon = TRUE;
  29. extern struct Library *ibase = NULL;
  30. extern struct Library *gbase = NULL;
  31. extern struct Library *GadToolsBase = NULL;
  32. extern struct Library *IconBase = NULL;
  33. extern BPTR StdErr = NULL;
  34. extern LONG args[6] = { NULL,NULL,0,0,0,0 };
  35. extern struct WBStartup *WBenchMsg;
  36.  
  37. /* Internal prototypes for this specific C file.*/
  38.  
  39. BOOL checkfor64bitcommandset(struct IOStdReq *io);
  40. void nsd_add(ULONG *bf, ULONG value1, ULONG value2);
  41.  
  42. /*This function does a full (low-level) format of a disk*/
  43. BOOL doFullFormat(DriveLayout *layout,char *statString,char *devName,
  44.              struct IOExtTD *io1)
  45.     {
  46.    ULONG *write, nsdbuffer[2], nsdbuffer2[2];
  47.    ULONG trackSize, numTracks, i, track;
  48.     UWORD formatcommand = TD_FORMAT, verifycommand = CMD_READ;
  49.    int c;
  50.    UBYTE error=2;
  51.    BOOL errorB=FALSE, command64 = FALSE;
  52.  
  53.    /*Get the size of a track (#surfaces*#blocks*#longwordsPerTrack*4*/
  54.    trackSize=layout->surfaces*layout->blockSize*4*layout->BPT;
  55.  
  56.     command64 = checkfor64bitcommandset((struct IOStdReq *)io1);
  57.     if (command64 == TRUE)
  58.         {
  59.         formatcommand = NSCMD_TD_FORMAT64;
  60.         verifycommand = NSCMD_TD_READ64;
  61.         }
  62.  
  63.     nsdbuffer[0] = 0; nsdbuffer[1] = 0;
  64.  
  65.    /*Allocate enough memory for one track*/
  66.    write = AllocMem(trackSize,layout->memType|MEMF_CLEAR);
  67.    if(write != NULL)
  68.         {
  69.       /*Initialize the IORequest*/
  70.       io1->iotd_Req.io_Data=write;
  71.       io1->iotd_Req.io_Length=trackSize;
  72.  
  73.  
  74.       /*Get the starting byte position in the volume*/
  75.         for (i=0; i<layout->lowCyl; i++) nsd_add((ULONG*)&nsdbuffer, nsdbuffer[1], trackSize);
  76.  
  77.         nsdbuffer2[0] = nsdbuffer[0]; nsdbuffer2[1] = nsdbuffer[1];
  78.         for (i=layout->lowCyl; i<layout->highCyl; i++) nsd_add((ULONG*)&nsdbuffer2, nsdbuffer2[1], trackSize);
  79.  
  80.         if (nsdbuffer2[0] != 0)
  81.             if (command64 == FALSE)
  82.                 {
  83.                 printf("\nYou are trying to format beyond 4GB border although\nyour device does not support 64 bit commands. Due to this\nfact, format operation is aborted in order to save your drive.\n\n");
  84.                 FreeMem(write, trackSize);
  85.                 return(FALSE);
  86.                 }
  87.  
  88.       numTracks=layout->highCyl-layout->lowCyl+1;
  89.  
  90.       /*Clear the status window*/
  91.       error=(updateStatWindow(" ",0)) ? 0 : error;
  92.  
  93.       /*For each track*/
  94.       for(track=0;track<numTracks && error!=0;track++)
  95.             {
  96.             /*Setup to format the disk*/
  97.             io1->iotd_Req.io_Command = formatcommand;
  98.             io1->iotd_Req.io_Actual  = (ULONG)nsdbuffer[0];
  99.             io1->iotd_Req.io_Offset  = (ULONG)nsdbuffer[1];
  100. /*
  101.             printf("%u %u %u %u command $%lx\n",
  102.                 nsdbuffer[0],
  103.                 io1->iotd_Req.io_Actual,
  104.                 nsdbuffer[1],
  105.                 io1->iotd_Req.io_Offset,
  106.                 io1->iotd_Req.io_Command);
  107. */
  108.             /*Update the status window*/
  109.             sprintf(statString, "Formatting cylinder %ld, %ld to go  ",
  110.                 track+layout->lowCyl, numTracks-track-1);
  111.  
  112.             error = (updateStatWindow(statString,track*1000/numTracks)) ?0:error;
  113.  
  114.             /*If the user didnt press Stop*/
  115.             if(error != 0)
  116.                 {
  117.                 error=(DoIO((struct IORequest *)io1)) ? 0 : error;
  118.                 if(error == 0) printError("A formatting error occured",NULL,NULL);
  119.                 }
  120.  
  121.             /*If we're suppossed to verify, and the user didn't press 'Stop'*/
  122.             if(Verify && error!=0)
  123.                 {
  124.                 /*Setup the same IORequest to do a read*/
  125.                 io1->iotd_Req.io_Command = verifycommand;
  126.                 io1->iotd_Req.io_Actual  = (ULONG)nsdbuffer[0];
  127.                 io1->iotd_Req.io_Offset  = (ULONG)nsdbuffer[1];
  128.  
  129.                 /*Update the status*/
  130.                 sprintf(statString, "Verifying cylinder %ld, %ld to go  ",
  131.                     track+layout->lowCyl, numTracks-track-1);
  132.  
  133.                 error = (updateStatWindow(statString,
  134.                     ( (1+(track*2))*1000)/(2*numTracks) )) ? 0 : error;
  135.  
  136.  
  137.                 /*If the user hasnt pressed Stop*/
  138.                 if(error != 0)
  139.                     {
  140.                     error = (DoIO((struct IORequest *)io1)) ? 0 : error;
  141.                     if (error == 0)
  142.                         {
  143.                         printError("\nA verify error occurred",NULL,NULL);
  144.                         errorB=TRUE;
  145.                         }
  146.                     }
  147.  
  148.                 /*Check the data that was read back.*/
  149.                 for(c=0;c<trackSize>>2 && error!=0;c++)
  150.                     {
  151.                     /*If there was an error*/
  152.                     if (write[c] != 0L)
  153.                         {
  154.                         /*Record the fact*/
  155.                         if(errorB != TRUE)
  156.                             {
  157.                             errorB=TRUE;
  158.                             //if(--error==0)
  159.                             printError("\nA verify error occurred",NULL,NULL);
  160.                             }
  161.                         write[c]=0L;
  162.                         }
  163.                     }
  164.                 if(!errorB && error!=2) error=2;
  165.                 }
  166.             /*Get the byte position of the next track*/
  167. /*            if(errorB)
  168.                 {
  169.                 errorB=FALSE;
  170.                 track--;
  171.                 }
  172. */            if (errorB == TRUE) break;
  173.             else nsd_add((ULONG*)&nsdbuffer, nsdbuffer[1], trackSize);
  174.             }
  175.  
  176.         /*Were done, so free the track memory*/
  177.         FreeMem(write,trackSize);
  178.         if (errorB == TRUE) return(0);
  179.         }
  180.    else printError("Couldn't allocate write buffer",NULL,NULL);
  181.  
  182.    return(error!=0);
  183.     }
  184.