[comp.sys.mac.programmer] SCSI HELP

Eliot.Henry@samba.acs.unc.edu (BBS Account) (08/24/90)

Help! I am writing a program to completly erase disks. I need to access the
hard drive on a sector level. Do i need to figure out how to use the SCSI
manager? Can I use the same lowlevel calls as on floppies but
use the appropriate driverefnum and driver num? Any help would be
greatly appreciated: especially sample source code!

Thanks in Advance

stevec@Apple.COM (Steve Christensen) (08/25/90)

In article <917@beguine.UUCP> Eliot.Henry@samba.acs.unc.edu writes:
>Help! I am writing a program to completly erase disks. I need to access the
>hard drive on a sector level. Do i need to figure out how to use the SCSI
>manager? Can I use the same lowlevel calls as on floppies but
>use the appropriate driverefnum and driver num? Any help would be
>greatly appreciated: especially sample source code!

If all you want to do is write all zeroes to all blocks on the disk, then
just make write requests to the driver.  You can do it in a simple loop
something like this:

	allocate a 512 byte block and fill it with zeroes
	for blockNum = 1 to NumberOfBlocks do
	  write the 512 byte block to drive block blockNum

OK, so you wanted real code, but you get the idea.

steve  :-)

-- 
____________________________________________________________________

  Steve Christensen             Internet:   stevec@goofy.apple.com
  Apple Computer, Inc.          AppleLink:  STEVEC
  20525 Mariani Ave, MS 81-CS   CompuServe: 76174,1712
  Cupertino, CA  95014

  "You just contradicted me."  "No I didn't."
____________________________________________________________________

ts@cup.portal.com (Tim W Smith) (08/25/90)

What do you mean by "completely erase"?  Do you want to wipe out the
partition table and the driver descriptor map and the driver?  If so,
you (probably) have to issue SCSI commands, since the driver will
(probably) only let you write in the HFS partition.

I say "probably" because I know of at least one driver that accepts
negative block addresses.  Block -1 is the block just before the
partition, etc.  A loop of the form

	for ( i = -1; ; --i )
	{
		write over block i, stop when errorr
	}

will allow you to erase these parts of the disk.  This is probably
a bug in the driver, however.

							Tim Smith

Eliot.Henry@samba.acs.unc.edu (BBS Account) (08/28/90)

Thanks for the info. Yes I want to completley erase the hard drive partition and all partition info etc . So would i use this routine:?

FUNCTION FSWrite (refNum: INTEGER; VAR count: LONGINT; buffPtr: Ptr)
                    : OSErr;  [Not in ROM]

and simply send it the refnum of the driver for the hard drive (obtained from
going through the volume list and doing pbhgetvinfo on each drive)

Simple!.

Steve: Will I be able to get at the whole disk this way or do i need to use
       SCSI calls?
 

dwal@ellis.uchicago.edu (David Walton) (08/29/90)

In article <936@beguine.UUCP> Eliot.Henry@samba.acs.unc.edu (BBS Account) writes:

>Thanks for the info. Yes I want to completley erase the hard drive partition and all partition info etc . So would i use this routine:
>
>FUNCTION FSWrite (refNum: INTEGER; VAR count: LONGINT; buffPtr: Ptr)
>                    : OSErr;  [Not in ROM]
>
>and simply send it the refnum of the driver for the hard drive (obtained from
>going through the volume list and doing pbhgetvinfo on each drive)

No.  First, the refnum in FSWrite isn't the driver refnum, it's an
access path refnum to an open file managed by the file Manager.
Second, this call applies to the partition that houses the Macintosh
file system on the disk, so you wouldn't be able to redo the partition
information this way.  I don't think you would use this call to erase
the disk in any case, because this one just writes to a particular
file.

Before you go wiping out partition information, I suggest that you
read up a bit.  The following chapters of Inside Macintosh might be
helpful:

Volume II, Ch. 4 (esp. Data organization on volumes), Ch. 6 (Structure
	of a Device Driver & Writing Your Own), Ch. 14 (Disk
	Initialization Package)

Volume IV, Ch. 19 (Data organization on volumes revisited), Ch. 26
	(Disk Initialization Package revisited), Ch. 31 (SCSI
	Manager),

Volume V, Ch. 31 (SCSI Manager revisited)


These recommendations are based on a scanty knowledge of how to manage
low-level interactions with SCSI devices and drivers.  Others, I'm
sure, will have additional (and probably better) references.

Hope this helps.




--
David Walton            Internet: dwal@midway.uchicago.edu
University of Chicago   {  Any opinions found herein are mine, not  }
Computing Organizations {  those of my employers (or anybody else). }

stevec@Apple.COM (Steve Christensen) (08/30/90)

In article <936@beguine.UUCP> Eliot.Henry@samba.acs.unc.edu writes:
>Thanks for the info. Yes I want to completley erase the hard drive partition
>and all partition info etc . So would i use this routine:?
>
>FUNCTION FSWrite (refNum: INTEGER; VAR count: LONGINT; buffPtr: Ptr)
>                    : OSErr;  [Not in ROM]
>
>and simply send it the refnum of the driver for the hard drive (obtained from
>going through the volume list and doing pbhgetvinfo on each drive)
>
>Simple!.
>
>Steve: Will I be able to get at the whole disk this way or do i need to use
>       SCSI calls?

Aha!  It sounds like you want to erase every block on the entire disk, not
just a particular partition.  In that case you have some setup to do before
you can erase anything.  First, you have to unmount any volumes associated
with that disk.  Then you need to go thru the drive queue and remove drive
entries, since the partition map and all partitions are going away.  Finally
you can erase the whole disk by calling the SCSI Manager.  If you want to
be able to use the drive after setting up a new partition map, then you'll
have to somehow let the driver know how big the new partition is.  I don't
know how you'd do that, so I'll leave it as an exercise for the reader.
BTW, why are you trying to do this yourself instead of letting something
like HDSC Setup do it for you?

steve

-- 
____________________________________________________________________

  Steve Christensen             Internet:   stevec@goofy.apple.com
  Apple Computer, Inc.          AppleLink:  STEVEC
  20525 Mariani Ave, MS 81-CS   CompuServe: 76174,1712
  Cupertino, CA  95014

  "You just contradicted me."  "No I didn't."
____________________________________________________________________

russotto@eng.umd.edu (Matthew T. Russotto) (08/30/90)

In article <9927@goofy.Apple.COM> stevec@Apple.COM (Steve Christensen) writes:
>
>If all you want to do is write all zeroes to all blocks on the disk, then
>just make write requests to the driver.  You can do it in a simple loop
>something like this:
>
>	allocate a 512 byte block and fill it with zeroes
>	for blockNum = 1 to NumberOfBlocks do
>	  write the 512 byte block to drive block blockNum
>
>OK, so you wanted real code, but you get the idea.

Why not:
DIFormat(drvnum);
?????
On every hard drive I've seen this will erase the disk to all zeros.
--
Matthew T. Russotto	russotto@eng.umd.edu	russotto@wam.umd.edu
      .sig under construction, like the rest of this campus.

Eliot.Henry@samba.acs.unc.edu (BBS Account) (09/06/90)

Thanks for all the help. I still could use just a short segment of pascal or C that would show me what format command blocks should be in and how I will be
able to signal the end of a set of commands. A code segment that read 5 blocks
from say the beginning of the disk all as one chunk would probably be enough of
an example to help me figure out how to pass the commands. I will look into
getting the full ansi spec and perhaps some specs on my hard drive but don't
think i should actually need these. Am I wrong?

Thanks again

ts@cup.portal.com (Tim W Smith) (09/06/90)

Here are some examples of using the SCSI Manager.  None of them
do exactly what you want, but they will show you how things
work.

The first function writes a single block of data to the device.
The device is assumed to be a disk.  It is assumed that TARGET_ID
is defined as the SCSI ID of the device you wish to talk to.
There is minimal error handling.

int WriteBlock( long blockNumber, void * dataPtr )
{
	char cmd[6];
	SCSIInstr tib[2];
	int stat, mesg;
	int ret;
	
	cmd[0] = 10;
	cmd[1] = (blockNumber >> 16) & 0x1f;
	cmd[2] = blockNumber >> 8;
	cmd[3] = blockNumber;
	cmd[4] = 1;
	cmd[5] = 0;
	
	tib[0].scOpcode = scNoInc;
	tib[0].scParam1 = (long)dataPtr;
	tib[0].scParam2 = 512;
	tib[1].scOpcode = scStop;
	
	if ( SCSIGet() )
		return -1;
	if ( SCSISelect( TARGET_ID ) )
		return -1;
	if ( SCSICmd( cmd, 6 ) )
		return -1;
	ret = 0;
	if ( SCSIWrite( tib ) )
		ret = -1;
	if ( SCSIComplete( &stat, &mesg, 10L ) )
		return -1;
	return ret;
}


The above was for a simple program that just need infrequent access
to the SCSI device.  For something whose needs are more I/O intense,
use a TIB like this:

	SCSIInstr tib[3] = {
		{ scInc, BufPtr, 512 },
		{ scLoop,-10L, BlkCount },
		{ scStop, 0L, 0L }
	};

where BufPtr is a pointer to the data buffer, and BlkCount is how many
block of data will be transfered.  Then use SCSIRBlind or SCSIWBlind
instead of SCSIRead or SCSIWrite.

The reason for the loop form of the TIB is that the SCSI Manager will
synchronize with the SCSI device each time the scInc instruction is
executed.  This minimizes the chance of blind mode causing a problem
on a Mac plus (burbles in the transfer from the device are likely to
happen only at block boundaries).

The ANSI SCSI spec is close to useless.  Nearly everything useful is
optional and vendor unique.  Most of the vendors have settled on a
common form for many of these optional commands, so if you get a
manual for a reasonable device, you will have enough information
to deal with most devices of that type.

The manuals for Quantum drives are excellent.  They include enough
about SCSI in general so that you can get everything about SCSI you
would need from the ANSI spec, and since Quantum implements all the
things common to most drives, the Quantum manual contains almost all
you will need to talk to nearly any drive.

The spec for the ANSI SCSI-2 standard has standardized all these option
things, so it would be good reading.  You can get more information on
this by calling the SCSI BBS with your modem.

The number for the SCSI BBS is:

	316-636-8700

The SCSI BBS has a copy of the SCSI-2 spec online.  If you can deal
with IBM PC files, there is a ZIPed copy that is 300K.  It's in
WordStar format.  There are programs available that turn WordStar
files into ASCII files.  There is one on the SCSI BBS, but it sucks.

A place called Global Engineering Documents, which is, I think,
in Aneheim, CA, sells this document for, I think $25.  You can
get their number from the SCSI BBS.  You can also get it from
ANSI, I think.

					Tim Smith

ps: if you ever start dealing with SCSI devices at a low level, be
aware that some of these have bugs.  For example, let's just say
that you wanted to play around with synchronous data transfer.
So you grab an Apple 40 meg hard disk that you happen to have
lying about and hook it up to a SCSI card that can support
synchronous.  You will be in for quite a surprise.  The Sony
40 meg drive that Apple uses has a screwed up implementation
of SCSI.  I mean, those guys at Sony just blew it.  It violates
the SCSI message protocol when you try to negotiate for synchronous
transfer.

minow@mountn.dec.com (Martin Minow) (09/12/90)

In article <9927@goofy.Apple.COM> stevec@Apple.COM (Steve Christensen) writes:
>If all you want to do is write all zeroes to all blocks on the disk, then
>just make write requests to the driver.

Sorry, but this doesn't write to "hidden" blocks, such as the partition
maps, blocks outside the partition,  and the blocks containing the driver
itself.  Also, there may be blocks on the disk that are not addressable by
the driver (that the disk uses for bad block replacement).

While you can write direct calls to the scsi driver without much
difficulty (you'll need the disk specification and/or the Ansi SCSI-II
draft specification), you may still have problems if, for example, you
need to handle errors.  Also, if you are trying to prevent all forms
of "covert channel" attacks, you will also have to reset all drive
control parameters (mode selects).

If all you want to do is erase the disk, use the "HD Setup" program
to reformat it, and DiskExpress to write zeros.

There are several products, such as Ft Knox that erase disks to
U.S. DoD standards.  I would recommend using one of these, rather
than trying to write your own.

Martin Minow
minow@bolt.enet.dec.com

Eliot.Henry@samba.acs.unc.edu (BBS Account) (09/13/90)

Thank you for your thoughts! It sounds like I may not be be easy to get access
to all the blocks. Any thoughts or suggestions how to do it. Could you explain a bit more about your refrence to covert channel attacks and reseting all drive
control parameters.

Steve: Any comments on this person's thoughts? Is he right?
--

Eliot.Henry@samba.acs.unc.edu (BBS Account) (10/03/90)

I just want to thank all of those who helped me get the info I needed on
SCSI hard drives. There is s very little easily accessible information on this
Thereis the SCSI ansi spec, hard drive manuals, IM, and one electrical spec
published by NCR.

Thanks again

--