Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Peter Norton Programmer's Guide - Norton Guide http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]

  The file allocation table (FAT) is DOS's map of how space is utilized in
  the files area of a disk. We've already discussed how space for the FAT
  itself is reserved on a diskette or in a fixed-disk partition. Now we'll
  describe how the FAT is formatted and used.

  For most disk formats, DOS maintains two copies of the FAT, just in case
  one of them is damaged or becomes unreadable. Curiously, the CHKDSK
  program, which tests for most errors that can occur in the FAT and in
  directories, does not even notice if the two FATs are different.

  The organization of the FAT is simple: There is one entry in the FAT for
  each cluster in the files area. A FAT entry can contain any of the values
  listed in Figure 5-13. If the value in a FAT entry doesn't mark an
  unused, reserved, or defective cluster, then the cluster that corresponds
  to the FAT entry is part of a file, and the value in the FAT entry itself
  indicates the next cluster in the file.

  This means that the space that belongs to a given file is mapped by a
  chain of FAT entries, each of which points to the next entry in the chain.
  (See Figure 5-14.) The first cluster number in the chain is the starting
  cluster number in the file's directory entry. When a file is created or
  extended, DOS allocates clusters to the file by searching the FAT for
  unused clusters (that is, clusters whose FAT entries are 0) and adding
  them to the chain. Conversely, when a file is truncated or deleted, DOS
  frees the clusters that had been allocated to the file by clearing the
  corresponding FAT entries.

  12-bit Value             16-bit Value            Meaning
  --------------------------------------------------------------------------
  0                        0                       Unused cluster
  FF0-FF6H                 FFF0-FFF6H              Reserved cluster
  FF7H                     FFF7H                   Bad cluster
  FF8-FFFH                 FFF8-FFFFH              Last cluster in a file
  (other values)                                   Next cluster in a file
  --------------------------------------------------------------------------

  Figure 5-13.  FAT values.

               File-  Extension            Starting
               name                        cluster
  Directory   +-----------------------------------+
      entry   |ALPHA|TEXT|                 |0003| |
              +-----------------------------------+
                     +------------------------+
                     |+-------------++---------+
            ---------.--------------.----------.----------
    FAT       |0000|0006|0000|0000|0000|0008|FFFFH|0000|
            ----------------------------------------------
                 2    3    4    5    6    7     8    9

  Figure 5-14.  Disk-space allocation using the FAT.

  The FAT can be formatted with either 12-bit or 16-bit entries. The 12-bit
  format is used for diskettes and fixed-disk partitions with no more than
  4078 clusters. (A fixed-disk's partition table indicates whether a DOS
  partition's FAT uses 12-bit or 16-bit entries.) The entries in a 12-bit
  FAT are harder to access because they don't fit neatly into the 16-bit
  word size of the 8086 family of microprocessors, but a 12-bit FAT takes up
  less room on a diskette, where disk space is scarcer.

  The first two entries in the FAT are reserved for use by DOS. The first
  byte of the FAT contains the same media descriptor value that appears in
  the BIOS parameter block in the disk boot sector. The remaining bytes of
  the first two entries are filled with the value 0FFH. Because the first
  two cluster numbers (0 and 1) are reserved, cluster number 2 corresponds
  to the first cluster of available disk space in the files area.

  Reading the values in the FAT is simple enough for a 16-bit FAT: Multiply
  a given cluster number by 2 to find the byte offset of the corresponding
  FAT entry. In the 16-bit FAT in Figure 5-15, for example, the byte offset
  of the FAT entry for cluster 2 is 04H, and the value in that entry is
  0003; the byte offset of the FAT entry for cluster 3 is 06H, and the value
  in that entry is 0004; and so on.

  For a 12-bit FAT, the computation is a bit trickier, because each pair of
  FAT entries occupies 3 bytes (0 and 1 occupy the first 3 bytes, 2 and 3
  occupy the next 3 bytes, and so forth). Given any cluster number, you can
  find the FAT entry by multiplying the cluster number by 3, dividing by 2,
  and then using the whole number of the result as a displacement into the
  FAT. By grabbing a word at that address, you have the three hex digits of
  the FAT entry, plus one extraneous hex digit, which can be removed by any
  one of several quick machine-language instructions. If the cluster number
  is even, you discard the high-order digit; if it is odd, you discard the
  low-order digit. Try this on the 12-bit FAT in Figure 5-15. You'll find
  that the entries are the same as in the 16-bit FAT in Figure 5-15.

  (a) 16-bit FAT

      Reserved     Cluster Cluster Cluster Cluster Cluster Cluster Cluster Cluster
                      2       3       4       5       6       7       8       9
   +------------+  +----+  +----+  +----+  +----+  +----+  +----+  +----+  +----+
   F8  FF  FF  FF  03  00  04  00  05  00  06  00  0A  00  08  00  FF  FF  23  01



  (b) 12-bit FAT

    Reserved    Clusters    Clusters    Clusters    Clusters
                2 and 3     4 and 5     6 and 7     8 and 9
   +--------+  +--------+  +--------+  +--------+  +--------+
   FO  FF  FF  03  40  00  05  60  00  0A  80  00  FF  3F  12

  Figure 5-15.  The first few entries in a 16-bit FAT (a) and in a 12-bit
  FAT (b).

  As we have said, the first two FAT entries, in both 12-bit and 16-bit
  formats, are not used to indicate the status of clusters; instead, they
  are set aside so that the very first byte of the FAT can be used as a
  media descriptor byte that indicates the format of the disk. (See Figure
  5-16.) However, you should not assume that these IDs uniquely identify
  formats: they don't necessarily. If you considered every disk format in
  use, you'd find quite a few duplications. Beware.

                                                   Sectors      Media
  Disk                     Capacity    Heads       per Track    Descriptor
  --------------------------------------------------------------------------
  51/4-inch diskette       160 KB      1             8          FEH
                           320 KB      2             8          FFH
                           180 KB      1             9          FCH
                           360 KB      2             9          FDH
                           1.2 MB      2            15          F9H
  31/2-inch diskette       720 KB      2             9          F9H
                           1.44 MB     2            18          F0H
  Fixed disk                                                    F8H
  --------------------------------------------------------------------------

  Figure 5-16.  DOS media descriptor values.

  Your programs can learn the format of a disk by reading and inspecting the
  FAT media descriptor byte. The easy way to do this is to use DOS function
  1BH (decimal 27). For more information about this function, see page 335.

  Special notes on the FAT

  Normally, programs do not look at or change a disk's FAT; they leave the
  FAT completely under the supervision of DOS. The only exceptions are
  programs that perform space-allocation functions not supported by DOS--for
  example, programs that recover erased files, such as the UnErase program
  in the Norton Utilities program set.

  Be aware that a FAT can be logically damaged; for example, an allocation
  chain can be circular, referring back to a previous link in the chain; or
  two chains can converge on one cluster; or a cluster can be orphaned,
  meaning that it is marked as in use even though it is not part of any
  valid allocation chain. Also, an end-of-file marker (FFFH or FFFFH) may be
  missing. The DOS programs CHKDSK and RECOVER are designed to detect and
  repair most of these problems as well as can reasonably be done.

  For special notes on the interaction of the space allocation chain in the
  FAT and DOS's record of a file's size, see page 117.

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson