Classic Computer Magazine Archive ANTIC VOL. 4, NO. 10 / FEBRUARY 1986

ST DISK SECRETS

Layout of tracks, sectors, files...

by Rick Oliver, Software Engineer, Hippopotamus Software

Hippopotamus Software visited Antic not long ago to demonstrate their new line of ST software. Of particular interest to our programmers was Hippo Disk Utilities. This program – which is mouse-controlled and completely written under GEM – lets you examine and edit the tracks and sectors' of ST disks. You can also restore deleted files, find file information, or search and alter RAM (Look for a review of this product soon in the ST Section.) We asked Wendell Brown, president of Hippopotamus, if they would share with Antic readers some of the ST disk secrets they had discovered while developing Hippo Disk Utilities. The following is their very generous response. – ANTIC ED

Before discussing the ST disk operating system (ST DOS), it helps to know how the disk is physically laid out. For now we will limit the discussion to the single-sided, quad-density, 3.5-inch floppy-the type used in the Atari SF354 disk drive.

DISK STRUCTURE

The disk is divided into 80 concentric circles called "tracks", which are numbered from zero at the outside to 79 near the center. Each track is divided into nine "sectors" numbered from one to nine, giving the disk a total of 720 sectors. And each sector can hold 512 bytes of information.

Multiplying this out, we find a total raw storage capacity of 360K on one floppy disk. But as we will see, not all of that space is available to the user for data storage.

Since data on a disk is stored in files, not as tracks or sectors, ST DOS requires some disk space to chart which sectors belong to which files, what the names of the files are, and other file-related information.

Some of this information is stored in the "directory," which occupies seven sectors of the disk. Another part is stored in the "File Allocation Table" (FAT), which is five sectors long.

The FAT contains the information necessary to piece together files, and it is deemed important enough to have two copies stored on the disk. ST DOS also sets aside one sector on each disk, called the "boot record," which has information concerning the format of the disk plus a program to load the system at startup time.

Adding it all up, we see that ST DOS uses up 18 sectors, or 9K, of raw disk space. All remaining space – the data space – is available for user file storage.

CLUSTERS

If a file is written to a newly formatted disk, it will be stored in contiguous sectors. But usually a file is fragmented into different size groups and scattered around the disk to take best advantage of free disk space.

The smallest unit of disk data recognized by ST DOS is called a "cluster". One cluster equals two contiguous sectors.

Thus, the smallest size a file or file fragment can be is one cluster. And since each cluster consists of two 512-byte sectors, any file will use at least one kilobyte of disk space.

THE FAT

The FAT – File Allocation Table – is used to keep track of which clusters belong to which files.

The FAT takes up 5 sectors on the disk, but it is best to look at it as one long string of 12-bit table "entries." Since every byte is 8 bits, this means that every 1 1/2 bytes of the FAT contain an entry.

The first two entries of the FAT, numbered zero and one, identify the format of the disk. The first byte contains the identifying code ($F7 for 80 track, quad-density), and the remaining two bytes are filled with $FF.

Each following entry represents one cluster in the data space of the disk. The first cluster of the actual data space will be numbered two, so that it corresponds to the third FAT entry, also numbered two.

Since each FAT entry is 12 bits, it can have a value from 0 to 4095 ($FFF). If a FAT entry has a value of zero, its corresponding cluster is unused and therefore available to store new data. A value from 4081 to 4087 ($FF1 to $FF7) means the cluster is unusable because of a formatting error. Usually this means that at least one of the sectors in the cluster is physically bad, but it isn't a problem since ST DOS won't try to use that cluster.

If a FAT entry has a value from 4088 to 4095 ($FF8 to $FFF), its corresponding cluster contains the end of a file. Any other value in a FAT entry (2 through 4080, or $FF0) indicates that its corresponding cluster is part of a file.

The way ST DOS sees it, a file is actually a list of clusters in the data space. The first item in this list (the number of the first cluster of the file) is kept in the directory entry for that file. The corresponding FAT entry for the first cluster of the file contains the number of the second cluster. The FAT entry for the second cluster contains the number of the third cluster, and so on until a cluster is reached with a FAT entry between 4088 and 4095 ($FF8 and $FFF). This would be the last cluster of the file.

So the clusters of a file may be anywhere in the disk's data space, and ST DOS just traces its way through the FAT entries to find all of the pieces.

The important part of understanding how it works is in seeing that the data of a file is stored in clusters, that there is a one-to-one mapping between the clusters of a file and the entries in the FAT, and that each FAT entry shows where the NEXT cluster is found.

DESK DIRECTORY

The disk directory is seven sectors long and contains all of the information about a file, except for the information stored in the FAT. Each entry in the directory is 32 bytes long and has eight fields:

			Directory Fields
		1)  Filename		 8 bytes
		2)  Filename extension	 3 bytes
		3)  Attributes		 1 byte
		4)  Reserved		10 bytes
		5)  Time of last change	 2 bytes
		6)  Date of last change	 2 bytes
		7)  First cluster	 2 bytes
		8)  File size		 4 bytes

Filename–Contains the name of the file in ASCII text. There is a maximum of eight letters, all of which are stored in upper case. If the name is less than eight characters, the remainder of the field is filled with blanks. A zero in the first byte of the filename indicates that the directory entry has never been used. A value of 229 ($E5) in this byte indicates that the file has been erased. Finally, a period in the first byte of the filename indicates that the entry is actually a special type of subdirectory, as explained below.

Filename Extension–Contains three characters for the filename extension, stored in uppper case. If the extension has less than 3 characters, the remainder of the field is filled with blanks.

File Attribute–This field uses six of the bits in its byte to store certain file attributes (see ATTRIBUTES below). The other two bits are unused.

Reserved–Currently unused and reserved for future use. All bytes in this field are normally set to zero.

Time of Last Change–This field is treated as a 16-bit value divided into three sections. The high five bits are used for the hour (0-23). The middle six bits are used for the minutes (0-59). The low five bits contain seconds in two-second increments (0-29). Multiply the seconds field by two to get the actual number of seconds.

Date of Last Change–Also treated as a 16-bit value which is divided into three parts. The high seven bits are used for the year minus 1980 (add 1980 to the year value to get the correct year). The next four bits are used for the month (1-12). The low five bits are used for the day of month (1-31).

First Cluster–Contains the number of the first cluster of the file. All following clusters in a file are found by tracing through the FAT, as explained above.

File Size–A four-byte (or 32-bit) number containing the size of the file in bytes. The effective size of a file, when read by ST DOS, may be shorter than this if the last cluster is reached in the FAT before this many bytes have been read.

ATTRIBUTES

As mentioned, the third field of the directory is one byte, of which six bits are used as file attribute flags. Currently, many of these attributes seem to be ignored by ST DOS, but the meanings are given here in case they get used by future releases of TOS.

			   File Attributes
	Bit	Decimal Value	Hex Value	Meaning
	0	    1		   1		Read Only
	1	    2		   2		Hidden
	2	    4		   4		System
	3	    8		   8		Volume Label
	4	    16		   10		Subdirectory
	5	    32		   20		Archive
	6	    64		   40		Unused
	7	    128		   80		Unused

Read Only–When set, will prevent a file from being deleted or written to. Will not prevent a file from being renamed, however.

Hidden and System–Have basically the same function. They should normally prevent the directory entry from appearing in a directory listing. These attributes are ignored by ST DOS and the Desktop.

Volume Label–Normally located only in the root directory of the directory structure. It marks a directory entry as containing the name of the volume or disk.

Subdirectory–Marks a directory entry as a subdirectory rather than a normal file. A subdirectory is the equivalent of a Desktop folder. Subdirectories are stored in the data space, just like files are. The structure of a subdirectory is the same as that of the main directory (usually called the root directory), except that a subdirectory is not fixed in size. A subdirectory only takes up as much space as it needs, but can grow to whatever size is necessary. There are two special entries in every directory which act as subdirectories themselves. They are "." and ".."–and they refer to the current directory and the parent directory, respectively The entry called ".." in the root directory refers to the root directory itself.

Archive–Normally used only with hard disks. It should be set if a file has changed since it was last backed up.

MULTIPLE FORMATS

Until now, we have been assuming a disk format of quad-density, single-sided, 3.5-inch floppy. But ST DOS can actually support a wide variety of formats, in which the number of sides, tracks per side, sectors per track, and bytes per sector may vary. Formats may differ from drive to drive, or even between disks used in the same drive.

The sides of a disk are numbered starting with zero. And disks are not limited to two sides. It is not uncommon for hard disks to have four sides – two platters with two sides each.

Regardless of format, the boot record is located on the first sector of track zero, side zero. It is one sector in length and is usually followed immediately by the FAT, two copies of which are stored in most formats.

The size of the FAT varies with the format of the disk. Directly after the FAT usually comes the directory, whose size also varies with disk format. Following the directory, and continuing to the end of the disk, is the data space which makes up the bulk of the disk.

One other thing that varies with disk format is the size of the entries in the FAT. For floppies, they are commonly 12 bits wide. But hard disks usually have 16-bit FAT entries. Although these entries work in a similar fashion to the 12-bit entries, their values are slightly different.

The first two 16-bit entries are still used to identify the disk format, And a FAT entry with the value zero still means that its cluster is unused and available. But the values for bad clusters on a hard disk become 65521-65527 ($FFF1-$FFF7), and the values for end-of-file become 65528-65535 ($FFF8-$FFFF). Any value from 2 to 65520 ($FFF0) means the corresponding cluster is part of a file, and files are still built in the same way.

CONCLUSION

Armed with a knowledge of the structure of a disk and a program to operate on that structure, it is usually possible to recover recently deleted files or to reconstruct most of a file if one of its sectors has gone bad.

Programs such as Hippo Disk Utilities have functions which automatically recover deleted files, and features that allow direct editing of the entire disk.

However, be careful if you venture into the realm of the disk doctor. It is always a good idea to perform any operations on a copy of your disk. That way, even if you destroy the clone, you won't lose the patient.