;
;[1m[32m        ____     ___        ___     ___ ___ ___ ___ ____      ___ ___ ___[0m
;[1m[32m         /  /__//_         /__ /  //_ //__ /  //_    /  / / //_ //__//_  [0m
;[1m[32m        /  /  //__        ___//__//__/___//__//     /  /_/_//  //  \/__  [0m
;
;[1m[32m-----------------------------------------------------------------------------[0m
;   - FOR ABO'S CONTACT US AT:   BP:28 1170 WATERMAEL-BOITSFORT 1 BELGIUM!!...
;
;[1m[32m-----------------------------------------------------------------------------[0m
;                           - DON'T SEND ANY DISKS -
;
;[1m[32m-----------------------------------------------------------------------------[0m
;
;
;
;                      [33m THE LEGENDARY >> SUBSOFTWARE << IN 87/88/89/90/91!!...
;                      [1m[32m ------------------------------------------------------
;
/*
** ReadCapacity.c - read a drive's capacity using HD_SCSICMD
** Copyright (C) 1990 by Ralph Babel, Falkenweg 3, D-6204 Taunusstein, FRG
** all rights reserved - alle Rechte vorbehalten
**
** 02-Jun-1990 created
*/

/*** included files ***/

#include <exec/types.h>
#include <exec/io.h>
#include <exec/memory.h>
#include <devices/scsidisk.h>
#include <libraries/dos.h>
#include <proto/exec.h>
#include <proto/dos.h>

/*** external symbol references ***/

void fprintf(BPTR, const char *, ...);

/*** constants ***/

#define BOARD 0 /* controller board */
#define TID   0 /* SCSI target ID */
#define LUN   0 /* logical unit */

#define UNIT (BOARD * 100 + LUN * 10 + TID)

#define MAXBUF 252

/*** structures ***/

struct CapacityData
 {
 ULONG HighSector;
 ULONG SectorSize;
 };

/*** entry point (RXStartUp.obj) ***/

void __stdargs __saveds main(
ULONG argc,
const char *const argv[])
 {
 BPTR fh;
 UBYTE *sensedata;
 struct MsgPort *mp;
 struct IOStdReq *io;
 struct CapacityData *cd;
 struct SCSICmd SC;
 UBYTE command[10];

 if(argc != 0) /* CLI only! */
  {
  fh = Output();

  if((cd = AllocMem(sizeof(struct CapacityData), MEMF_CHIP)) != NULL)
   {
   if((sensedata = AllocMem(MAXBUF, MEMF_CHIP)) != NULL)
    {
    if((mp = CreatePort(NULL, 0)) != NULL)
     {
     if((io = CreateStdIO(mp)) != NULL)
      {
      if(OpenDevice("gvpscsi.device", UNIT, (struct IORequest *)io, 0) == 0)
       {
       io->io_Command = HD_SCSICMD;
       io->io_Length  = sizeof(struct SCSICmd);
       io->io_Data    = (APTR)&SC;

       SC.scsi_Data        = (UWORD *)cd;
       SC.scsi_Length      = sizeof(struct CapacityData);
       SC.scsi_Command     = command;
       SC.scsi_CmdLength   = 6;

#ifdef SCSIF_AUTOSENSE

       SC.scsi_Flags       = SCSIF_READ | SCSIF_AUTOSENSE;
       SC.scsi_SenseData   = sensedata;
       SC.scsi_SenseLength = MAXBUF;
       SC.scsi_SenseActual = 0;

#else

       SC.scsi_Flags       = SCSIF_READ;

#endif

       command[0] = 0x25; /* READ CAPACITY */
       command[1] = LUN << 5;
       command[2] = 0;
       command[3] = 0;
       command[4] = 0;
       command[5] = 0;
       command[6] = 0;
       command[7] = 0;
       command[8] = 0;
       command[9] = 0;

       (void)DoIO((struct IORequest *)io);

       fprintf(fh, "io_Error         = %d\n",  io->io_Error);
       fprintf(fh, "scsi_Status      = %d\n",  SC.scsi_Status);
       fprintf(fh, "scsi_CmdActual   = %d\n",  SC.scsi_CmdActual);
       fprintf(fh, "scsi_Actual      = %ld\n", SC.scsi_Actual);

#ifdef SCSIF_AUTOSENSE

       fprintf(fh, "scsi_SenseActual = %d\n",  SC.scsi_SenseActual);

       if(SC.scsi_SenseActual != 0)
        {
        UWORD i;

        fprintf(fh, "\nSenseData:");

        for(i = 0; i < SC.scsi_SenseActual; ++i)
         fprintf(fh, " %02x", sensedata[i]);

        fprintf(fh, "\n");
        }

#endif

       if(io->io_Error == 0)
        {
        fprintf(fh, "\nHighSector = %ld\n", cd->HighSector);
        fprintf(fh, "SectorSize = %ld\n",   cd->SectorSize);
        }

       CloseDevice((struct IORequest *)io);
       }
      else
       fprintf(fh, "Error %d while opening SCSI unit %ld.\n",
        io->io_Error, (long)UNIT);

      DeleteStdIO(io);
      }
     else
      fprintf(fh, "Could not create I/O request.\n");

     DeletePort(mp);
     }
    else
     fprintf(fh, "Could not create message port.\n");

    FreeMem(sensedata, MAXBUF);
    }
   else
    fprintf(fh, "Insufficient free store.\n");

   FreeMem(cd, sizeof(struct CapacityData));
   }
  else
   fprintf(fh, "Insufficient free store.\n");
  }
 }
