ARP 1.1 (Disk 85) (Mar 1988) : DriveTest / DiskVerify.c

/*	DiskVerify.c - Track read of disk for verification */

/*
 *	Written by Michael L. Hitch
 *	Bozeman, MT
*/

#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <exec/memory.h>
#include <exec/interrupts.h>
#include <exec/ports.h>
#include <exec/libraries.h>
#include <exec/io.h>
#include <exec/tasks.h>
#include <exec/execbase.h>
#include <exec/devices.h>
#include <devices/trackdisk.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <libraries/filehandler.h>

#define	TD_READ	CMD_READ

extern struct DosLibrary *DOSBase;		/* DOS Library base pointer */

SHORT error;
struct MsgPort *read_port;
struct IOExtTD *read_req;

char *device_name;

unsigned long unit;					/* unit number */
unsigned long numsects;				/* number of sectors/track */
unsigned long numheads;				/* number of heads/cylinder */
unsigned long locyl;				/* low cylinder number */
unsigned long hicyl;				/* high cylinder number */

BYTE *diskbuffer;

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

main(argc, argv)
int argc;
char *argv[];
{
	SHORT cylinder, head;
	char buf[128];
	
	read_port = CreatePort(0, 0);
	if (read_port == NULL)
		quit(100);
	read_req = (struct IOExtTD *) CreateExtIO(read_port,
	    sizeof(struct IOExtTD));
	if (read_req == NULL)
		quit(200);
	strcpy(buf, "df1");
	if (argc > 1)
		strcpy(buf, argv[1]);
	if (buf[3] == ':')
		buf[3] = 0;
	init_device (buf);
	error = OpenDevice (device_name, unit, read_req, 0);
	if (error) {
		printf("OpenDevice return was: %lx\n", error);
		quit(1);
	}
	if (argc > 2)
		printf ("LowCyl=%ld, HighCyl=%ld, NumHeads=%ld, %NumSects=%ld\n",
			locyl, hicyl, numheads, numsects);
	if ((diskbuffer = (BYTE *) AllocMem (TD_SECTOR * numsects * numheads
	    , MEMF_CHIP)) == NULL) {
		printf("Unable to allocate cylinder buffer\n");
		CloseDevice(read_req);
		quit(1);
	}
	printf("Insert disk to be verified in drive %s: and enter return: ",
		buf);
	if (Read (Input(), buf, 128) <= 0)
		printf("\n");
	printf("\233\060 p");		/* turn cursor off */
	for (cylinder = locyl; cylinder <= hicyl; ++cylinder) {
		printf("\rCyl=%ld", cylinder);
		ReadCyl(cylinder);
		if (read_req->iotd_Req.io_Error != 0) {
			/* compute head from io_Actual */
			head = read_req->iotd_Req.io_Actual;
			head = head / (TD_SECTOR * numsects);
			head = head % numheads;
			printf(
			  "\2337m\nError on Cyl=%ld, Hd=%ld, Error=%ld\233m\n",
			    cylinder, head,
			    read_req->iotd_Req.io_Error);
		}
		if (read_req->iotd_Req.io_Error == TDERR_DiskChanged)
			break;
		Chk_Abort();
	}
	printf("\nVerify completed\n");
	MotorOff();
	CloseDevice(read_req);
	quit(0);
}

ReadCyl(cyl)
SHORT cyl;
{
	LONG offset;

	read_req->iotd_Req.io_Length = TD_SECTOR * numsects * numheads;
	read_req->iotd_Req.io_Data = (APTR) diskbuffer;
	read_req->iotd_Req.io_Command = CMD_READ;
	offset = cyl * numheads * numsects * TD_SECTOR;
	read_req->iotd_Req.io_Offset = offset;
	DoIO(read_req);
	return(0);
}

MotorOff()
{
	read_req->iotd_Req.io_Length = 0;
	read_req->iotd_Req.io_Command = TD_MOTOR;
	DoIO(read_req);
	return(0);
}

quit(return_code)
{
	if (diskbuffer)
		FreeMem (diskbuffer, TD_SECTOR * numsects * numheads);
	if (read_req)
		DeleteExtIO(read_req);
	if (read_port)
		DeletePort(read_port);
	printf("\233 p");		/* turn cursor back on */
	exit(return_code);
}

Chk_Abort()
{
	if (SetSignal(0, SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D) &
	    (SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D)) {
		MotorOff ();
		CloseDevice (read_req);
		printf("\n");
		quit (0);
	}
}

init_device (name)
register char *name;
{
	struct RootNode *root_node;
	struct DosInfo *info_node;
	register struct DeviceNode *device_node;
	struct FileSysStartupMsg *startup_msg;
	unsigned long *envptr;
	root_node = (struct RootNode *) DOSBase->dl_Root;
	info_node = (struct DosInfo *) BADDR (root_node->rn_Info);
	device_node = (struct DeviceNode *) BADDR (info_node->di_DevInfo);
	while (device_node) {
		if (device_node->dn_Type == DLT_DEVICE ) {
			if (match (name, BADDR (device_node->dn_Name) + 1) == 0)
				break;
		}
		device_node = (struct DeviceNode *) BADDR (device_node->dn_Next);
	}
	if (device_node == 0) {
		printf ("Device %s: not found\n", name);
		quit (1);
	}
	startup_msg = (struct FileSysStartupMsg *) BADDR (device_node->dn_Startup);
	unit = startup_msg->fssm_Unit;		/* unit number */
	device_name = (char *) BADDR (startup_msg->fssm_Device) + 1;
	envptr = (long *) BADDR (startup_msg->fssm_Environ);
	locyl = envptr[DE_LOWCYL];
	hicyl = envptr[DE_UPPERCYL];
	numheads = envptr[DE_NUMHEADS];
	numsects = envptr[DE_BLKSPERTRACK];
}

match (s1, s2)
register char *s1, *s2;
{
	register char c;
	while ((c = toupper (*s1++)) == toupper (*s2++))
		if (c == 0)
			return (0);
	return (1);
}