/* checkdisk.c */
 
#include "exec/types.h"
#include "devices/trackdisk.h"

#define TD_READ CMD_READ
#define BLOCKSIZE TD_SECTOR

struct MsgPort *diskport;
struct IOExtTD *diskreq;
BYTE diskbuffer[BLOCKSIZE];

extern struct MsgPort *CreatePort();
extern struct IORequest *CreateExtIO();

void MotorOnOff(onoff)
long onoff;
{
	/* TURN ON/OFF DISK MOTOR ... old motor state is returned in io_Actual */
	diskreq->iotd_Req.io_Length = onoff;
	/* this says motor is to be turned on */
	diskreq->iotd_Req.io_Command = TD_MOTOR;
	/* do something with the motor */
	DoIO(diskreq);
}


main(argc,argv)
int argc;
char *argv[];
{
SHORT cylinder,head,sector;
register short headcylinderterm, cylinderterm;
LONG unit,error;

if (argc>1)
{
 unit = atoi(argv[1]);
 if( unit < 0 || unit > 3) exit(4);
}
else
 unit = 1;
	
diskport = CreatePort(0,0);
if(diskport == 0) exit(1);	/* error in createport */
diskreq = (struct IOExtTD *)CreateExtIO(diskport, sizeof(struct IOExtTD));     
		/* make an io request block for communicating with the disk */
if(diskreq == 0) { DeletePort(diskport); exit(2); }	

error = OpenDevice(TD_NAME,unit,diskreq,0);
if(error)
 {
  DeleteExtIO(diskreq, sizeof(struct IOExtTD));
  DeletePort(diskport);
  exit(3);
 }
MotorOnOff(1);

for(cylinder=0; cylinder<80; cylinder++)	/* tracks to test */
{
 cylinderterm = NUMSECS * NUMHEADS * cylinder;
 for(head=0; head<2; head++)	/* number of heads to test */
 {
  headcylinderterm = NUMSECS * head + cylinderterm; 
  for(sector=0; sector<11; sector++)	/* sectors to test */
  {
   diskreq->iotd_Req.io_Flags = 0;      
   diskreq->iotd_Req.io_Length = BLOCKSIZE;      
   diskreq->iotd_Req.io_Data = (APTR)diskbuffer;	
   diskreq->iotd_Req.io_Offset = (long) (TD_SECTOR * (sector + headcylinderterm));
   diskreq->iotd_Req.io_Command = ETD_READ;
		/* check that disk not changed before reading */
   diskreq->iotd_Count = 0xFFFFFFFF;
   diskreq->iotd_SecLabel = 0;    	
	
	/* convert from cylinder, head, sector to byte-offset value to get
   	 * right one (as dos and everyone else sees it)...*/
	
	/* driver reads one CYLINDER at a time (head does not move for
	 * 22 sequential sector reads, or better-put, head doesnt move for
	 * 2 sequential full track reads.)
	 */

   DoIO(diskreq);

   if(diskreq->iotd_Req.io_Error != 0) 
   printf("\nError nr. Error=%ld at Cyl=%ld, Sc=%ld, Hd=%ld",
		diskreq->iotd_Req.io_Error,cylinder,sector,head);
  }
 }
}
MotorOnOff(0);
CloseDevice(diskreq);

DeleteExtIO(diskreq, sizeof(struct IOExtTD));
DeletePort(diskport);
}	/* end of main */

