home *** CD-ROM | disk | FTP | other *** search
/ For Beginners & Professional Hackers / cd.iso / docum / advdos.doc / s1c10 < prev    next >
Encoding:
Text File  |  1992-04-21  |  38.1 KB  |  757 lines

  1. ────────────────────────────────────────────────────────────────────────────
  2. Chapter 10  Disk Internals
  3.  
  4.   MS-DOS disks are organized according to a rather rigid scheme that is
  5.   easily understood and therefore easily manipulated. Although you will
  6.   probably never need to access the special control areas of a disk
  7.   directly, an understanding of their internal structure leads to a better
  8.   understanding of the behavior and performance of MS-DOS as a whole.
  9.  
  10.   From the application programmer's viewpoint, MS-DOS presents disk devices
  11.   as logical volumes that are associated with a drive code (A, B, C, and so
  12.   on) and that have a volume name (optional), a root directory, and from
  13.   zero to many additional directories and files. MS-DOS shields the
  14.   programmer from the physical characteristics of the medium by providing a
  15.   battery of disk services through Int 21H. Using these services, the
  16.   programmer can create, open, read, write, close, and delete files in a
  17.   uniform way, regardless of the disk drive's size, speed, number of
  18.   read/write heads, number of tracks, and so forth.
  19.  
  20.   Requests from an application program for file operations actually go
  21.   through two levels of translation before resulting in the physical
  22.   transfer of data between the disk device and random-access memory:
  23.  
  24.   1.  Beneath the surface, MS-DOS views each logical volume, whether it is
  25.       an entire physical unit such as a floppy disk or only a part of a
  26.       fixed disk, as a continuous sequence of logical sectors, starting at
  27.       sector 0. (A logical disk volume can also be implemented on other
  28.       types of storage. For example, RAM disks map a disk structure onto an
  29.       area of random-access memory.) MS-DOS translates an application
  30.       program's Int 21H file-management requests into requests for transfers
  31.       of logical sectors, using the information found in the volume's
  32.       directories and allocation tables. (For those rare situations where it
  33.       is appropriate, programs can also access logical sectors directly with
  34.       Int 25H and Int 26H.)
  35.  
  36.   2.  MS-DOS then passes the requests for logical sectors to the disk
  37.       device's driver, which maps them onto actual physical addresses (head,
  38.       track, and sector). Disk drivers are extremely hardware dependent and
  39.       are always written in assembly language for maximum speed. In most
  40.       versions of MS-DOS, a driver for IBM-compatible floppy- and fixed-disk
  41.       drives is built into the MS-DOS BIOS module (IO.SYS) and is always
  42.       loaded during system initialization; you can install additional
  43.       drivers for non-IBM-compatible disk devices by including the
  44.       appropriate DEVICE directives in the CONFIG.SYS file.
  45.  
  46.   Each MS-DOS logical volume is divided into several fixed-size control
  47.   areas and a files area (Figure 10-1). The size of each control area
  48.   depends on several factors──the size of the volume and the version of
  49.   FORMAT used to initialize the volume, for example──but all of the
  50.   information needed to interpret the structure of a particular logical
  51.   volume can be found on the volume itself in the boot sector.
  52.  
  53.   ┌───────────────────────────────────────────────────────┐
  54.   │                      Boot sector                      │
  55.   │                     Reserved area                     │
  56.   ├───────────────────────────────────────────────────────┤
  57.   │               File allocation table #1                │
  58.   ├───────────────────────────────────────────────────────┤
  59.   │           Possible additional copies of FAT           │
  60.   ├───────────────────────────────────────────────────────┤
  61.   │                    Root directory                     │
  62.   ├───────────────────────────────────────────────────────┤
  63.   │                                                       │
  64.   │                      Files area                       │
  65.   │                                                       │
  66.   └───────────────────────────────────────────────────────┘
  67.  
  68.   Figure 10-1.  Map of a typical MS-DOS logical volume. The boot sector
  69.   (logical sector 0) contains the OEM identification, BIOS parameter block
  70.   (BPB), and disk bootstrap. The remaining sectors are divided among an
  71.   optional reserved area, one or more copies of the file allocation table,
  72.   the root directory, and the files area.
  73.  
  74.  
  75. The Boot Sector
  76.  
  77.   Logical sector 0, known as the boot sector, contains all of the critical
  78.   information regarding the disk mediu 's characteristics (Figure 10-2).
  79.   The first byte in the sector is always an 80x86 jump instruction──either a
  80.   normal intrasegment JMP (opcode 0E9H) followed by a 16-bit displacement or
  81.   a "short" JMP (opcode 0EBH) followed by an 8-bit displacement and then by
  82.   an NOP (opcode 90H). If neither of these two JMP opcodes is present, the
  83.   disk has not been formatted or was not formatted for use with MS-DOS. (Of
  84.   course, the presence of the JMP opcode does not in itself ensure that the
  85.   disk has an MS-DOS format.)
  86.  
  87.   Following the initial JMP instruction is an 8-byte field that is reserved
  88.   by Microsoft for OEM identification. The disk-formatting program, which is
  89.   specialized for each brand of computer, disk controller, and medium, fills
  90.   in this area with the name of the computer manufacturer and the
  91.   manufacturer's internal MS-DOS version number.
  92.  
  93.   00H ┌───────────────────────────────────────────────┐
  94.       │             E9 XX XX or EB XX 90              │
  95.   03H ├───────────────────────────────────────────────┤
  96.       │             OEM name and version              │
  97.       │                   (8 bytes)                   │
  98.   OBH ├───────────────────────────────────────────────┤─┐
  99.       │          Bytes per sector (2 bytes)           │ │
  100.   ODH ├───────────────────────────────────────────────┤ │
  101.       │     Sectors per allocation unit (1 byte)      │ │
  102.   0EH ├───────────────────────────────────────────────┤ │
  103.       │   Reserved sectors, starting at 0 (2 bytes)   │ │
  104.   10H ├───────────────────────────────────────────────┤ │
  105.       │            Number of FATs (1 byte)            │ B
  106.   11H ├───────────────────────────────────────────────┤ P
  107.       │  Number of root-directory entries (2 bytes)   │ B
  108.   13H ├───────────────────────────────────────────────┤ │
  109.       │   Total sectors in logical volume (2 bytes)   │ │
  110.   15H ├───────────────────────────────────────────────┤ │ MS-DOS
  111.       │             Media descriptor byte             │ │ version 2.0
  112.   16H ├───────────────────────────────────────────────┤ │
  113.       │      Number of sectors per FAT (2 bytes)      │ │
  114.   18H ├───────────────────────────────────────────────┤═╡
  115.       │          Sectors per track (2 bytes)          │ │
  116.   1AH ├───────────────────────────────────────────────┤ │
  117.       │           Number of heads (2 bytes)           │ │ MS-DOS
  118.   1CH ├───────────────────────────────────────────────┤ │ version 3.0
  119.       │      Number of hidden sectors (4 bytes)       │═╡
  120.   20H ├───────────────────────────────────────────────┤ │ MS-DOS
  121.       │        Total sectors in logical volume        │ │ version 4.0
  122.       │      (MS-DOS 4.0 and volume size >32 MB)      │ │
  123.   24H ├───────────────────────────────────────────────┤═╡
  124.       │             Physical drive number             │ │
  125.   25H ├───────────────────────────────────────────────┤ │
  126.       │                   Reserved                    │ │
  127.   26H ├───────────────────────────────────────────────┤ │
  128.       │     Extended boot signature record (29H)      │ │ Additional
  129.   27H ├───────────────────────────────────────────────┤ │ MS-DOS 4.0
  130.       │            32-bit binary volume ID            │ │ information
  131.   2BH ├───────────────────────────────────────────────┤ │
  132.       │            Volume label (11 bytes)            │ │
  133.   36H ├───────────────────────────────────────────────┤ │
  134.       │              Reserved (8 bytes)               │ │
  135.   3EH ├───────────────────────────────────────────────┤─┘
  136.       │                   Bootstrap                   │
  137.       └───────────────────────────────────────────────┘
  138.  
  139.   Figure 10-2.  Map of the boot sector of an MS-DOS disk. Note the JMP at
  140.   offset 0, the OEM identification field, the MS-DOS version 2 compatible
  141.   BIOS parameter block (bytes 0BH─17H), the three additional WORD fields for
  142.   MS-DOS version 3, the double-word number-of-sectors field and 32-bit
  143.   binary volume ID for MS-DOS version 4.0, and the bootstrap code.
  144.  
  145.   The third major component of the boot sector is the BIOS parameter block
  146.   (BPB) in bytes 0BH through 17H. (Additional fields are present in MS-DOS
  147.   versions 3.0 and later.) This data structure describes the physical disk
  148.   characteristics and allows the device driver to calculate the proper
  149.   physical disk address for a given logical-sector number; it also contains
  150.   information that is used by MS-DOS and various system utilities to
  151.   calculate the address and size of each of the disk control areas (file
  152.   allocation tables and root directory).
  153.  
  154.   The final element of the boot sector is the disk bootstrap routine. The
  155.   disk bootstrap is usually read into memory by the ROM bootstrap, which is
  156.   executed automatically when the computer is turned on. The ROM bootstrap
  157.   is usually just smart enough to home the head of the disk drive (move it
  158.   to track 0), read the first physical sector into RAM at a predetermined
  159.   location, and jump to it. The disk bootstrap is more sophisticated. It
  160.   calculates the physical disk address of the beginning of the files area,
  161.   reads the files containing the operating system into memory, and transfers
  162.   control to the BIOS module at location 0070:0000H. (See Chapter 2.)
  163.  
  164.   Figures 10-3 and 10-4 show a partial hex dump and disassembly of a
  165.   PC-DOS 3.3 floppy-disk boot sector.
  166.  
  167.   ──────────────────────────────────────────────────────────────────────────
  168.          0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
  169.   0000  EB 34 90 49 42 4D 20 20 33 2E 33 00 02 02 01 00  .4.IBM  3.3.....
  170.   0010  02 70 00 D0 02 FD 02 00 09 00 02 00 00 00 00 00  .p..............
  171.   0020  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12  ................
  172.   0030  00 00 00 00 01 00 FA 33 C0 8E D0 BC 00 7C 16 07  .......3.....|..
  173.         .
  174.         .
  175.         .
  176.   01C0  0D 0A 44 69 73 6B 20 42 6F 6F 74 20 66 61 69 6C  ..Disk Boot fail
  177.   01D0  75 72 65 0D 0A 00 49 42 4D 42 49 4F 20 20 43 4F  ure...IBMBIO  CO
  178.   01E0  4D 49 42 4D 44 4F 53 20 20 43 4F 4D 00 00 00 00  MIBMDOS  COM....
  179.   01F0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA  ..............U.
  180.   ──────────────────────────────────────────────────────────────────────────
  181.  
  182.   Figure 10-3.  Partial hex dump of the boot sector (track 0, head 0, sector
  183.   1) of a PC-DOS version 3.3 floppy disk. This sector contains the OEM
  184.   identification, a copy of the BIOS parameter block describing the medium,
  185.   and the bootstrap routine that reads the BIOS into memory and transfers
  186.   control to it. See also Figures 10-2 and 10-4.
  187.  
  188.   ──────────────────────────────────────────────────────────────────────────
  189.           jmp     $+54            ; jump to bootstrap
  190.           nop
  191.  
  192.           db      'IBM  3.3'      ; OEM identification
  193.  
  194.                                   ; BIOS parameter block
  195.           dw      512             ; bytes per sector
  196.           db      2               ; sectors per cluster
  197.           dw      1               ; reserved sectors
  198.           db      2               ; number of FATs
  199.           dw      112             ; root directory entries
  200.           dw      720             ; total sectors
  201.           db      0fdh            ; media descriptor byte
  202.           dw      2               ; sectors per FAT
  203.  
  204.           dw      9               ; sectors per track
  205.           dw      2               ; number of heads
  206.           dd      0               ; hidden sectors
  207.           .
  208.           .
  209.           .
  210.   ──────────────────────────────────────────────────────────────────────────
  211.  
  212.   Figure 10-4.  Partial disassembly of the boot sector shown in Figure
  213.   10-3.
  214.  
  215.  
  216. The Reserved Area
  217.  
  218.   The boot sector is actually part of a reserved area that can span from one
  219.   to several sectors. The reserved-sectors word in the BPB, at offset 0EH in
  220.   the boot sector, describes the size of this area. Remember that the number
  221.   in the BPB field includes the boot sector itself, so if the value is 1 (as
  222.   it is on IBM PC floppy disks), the length of the reserved area is actually
  223.   0 sectors.
  224.  
  225.  
  226. The File Allocation Table
  227.  
  228.   When a file is created or extended, MS-DOS assigns it groups of disk
  229.   sectors from the files area in powers of 2. These are known as allocation
  230.   units or clusters. The number of sectors per cluster for a given medium is
  231.   defined in the BPB and can be found at offset 0DH in the disk's boot
  232.   sector. Below are some example cluster sizes:
  233.  
  234.   Disk type                     Power of 2    Sectors/cluster
  235.   ──────────────────────────────────────────────────────────────────────────
  236.   5.25" 180 KB floppy disk      0             1
  237.   5.25" 360 KB floppy disk      1             2
  238.   PC/AT fixed disk              2             4
  239.   PC/XT fixed disk              3             8
  240.   ──────────────────────────────────────────────────────────────────────────
  241.  
  242.   The file allocation table (FAT) is divided into fields that correspond
  243.   directly to the assignable clusters on the disk. These fields are 12 bits
  244.   in MS-DOS versions 1 and 2 and may be either 12 bits or 16 bits in
  245.   versions 3.0 and later, depending on the size of the medium (12 bits if
  246.   the disk contains fewer than 4087 clusters, 16 bits otherwise).
  247.  
  248.   The first two fields in the FAT are always reserved. On IBM-compatible
  249.   media, the first 8 bits of the first reserved FAT entry contain a copy of
  250.   the media descriptor byte, which is also found in the BPB in the boot
  251.   sector. The second, third, and (if applicable) fourth bytes, which
  252.   constitute the remainder of the first two reserved FAT fields, always
  253.   contain 0FFH. The currently defined IBM-format media descriptor bytes are
  254.   as follows:
  255.  
  256.                                                              MS-DOS version
  257.                                                              where first
  258.   Descriptor     Medium                                      supported
  259.   ──────────────────────────────────────────────────────────────────────────
  260.   0F0H           3.5" floppy disk, 2-sided, 18-sector        3.3
  261.   0F8H           Fixed disk                                  2.0
  262.   0F9H           5.25" floppy disk, 2-sided, 15-sector       3.0
  263.                  3.5" floppy disk, 2-sided, 9-sector         3.2
  264.   0FCH           5.25" floppy disk, 1-sided, 9-sector        2.0
  265.   0FDH           5.25" floppy disk, 2-sided, 9-sector        2.0
  266.                  8" floppy disk, 1-sided, single-density
  267.   0FEH           5.25" floppy disk, 1-sided, 8-sector        1.0
  268.                  8" floppy disk, 1-sided, single-density
  269.                  8" floppy disk, 2-sided, double-density
  270.   0FFH           5.25" floppy disk, 2-sided, 8-sector        1.1
  271.   ──────────────────────────────────────────────────────────────────────────
  272.  
  273.   The remainder of the FAT entries describe the use of their corresponding
  274.   disk clusters. The contents of the FAT fields are interpreted as follows:
  275.  
  276.   Value              Meaning
  277.   ──────────────────────────────────────────────────────────────────────────
  278.   (0)000H            Cluster available
  279.   (F)FF0─(F)FF6H     Reserved cluster
  280.   (F)FF7H            Bad cluster, if not part of chain
  281.   (F)FF8─(F)FFFH     Last cluster of file
  282.   (X)XXX             Next cluster in file
  283.   ──────────────────────────────────────────────────────────────────────────
  284.  
  285.   Each file's entry in a directory contains the number of the first cluster
  286.   assigned to that file, which is used as an entry point into the FAT. From
  287.   the entry point on, each FAT slot contains the cluster number of the next
  288.   cluster in the file, until a last-cluster mark is encountered.
  289.  
  290.   At the computer manufacturer's option, MS-DOS can maintain two or more
  291.   identical copies of the FAT on each volume. MS-DOS updates all copies
  292.   simultaneously whenever files are extended or the directory is modified.
  293.   If access to a sector in a FAT fails due to a read error, MS-DOS tries the
  294.   other copies until a successful disk read is obtained or all copies are
  295.   exhausted. Thus, if one copy of the FAT becomes unreadable due to wear or
  296.   a software accident, the other copies may still make it possible to
  297.   salvage the files on the disk. As part of its procedure for checking the
  298.   integrity of a disk, the CHKDSK program compares the multiple copies
  299.   (usually two) of the FAT to make sure they are all readable and
  300.   consistent.
  301.  
  302.  
  303. The Root Directory
  304.  
  305.   Following the file allocation tables is an area known in MS-DOS versions
  306.   2.0 and later as the root directory. (Under MS-DOS version 1, it was the
  307.   only directory on the disk.) The root directory contains 32-byte entries
  308.   that describe files, other directories, and the optional volume label
  309.   (Figure 10-5). An entry beginning with the byte value E5H is available
  310.   for reuse; it represents a file or directory that has been erased. An
  311.   entry beginning with a null (zero) byte is the logical end-of-directory;
  312.   that entry and all subsequent entries have never been used.
  313.  
  314.   00H ┌──────────────────────────────┐
  315.       │           Filename           │ Note 1
  316.   08H ├──────────────────────────────┤
  317.       │          Extension           │
  318.   0BH ├──────────────────────────────┤
  319.       │        File attribute        │ Note 2
  320.   0CH ├──────────────────────────────┤
  321.       │           Reserved           │
  322.   16H ├──────────────────────────────┤
  323.       │ Time created or last updated │ Note 3
  324.   18H ├──────────────────────────────┤
  325.       │ Date created or last updated │ Note 4
  326.   1AH ├──────────────────────────────┤
  327.       │       Starting cluster       │
  328.   1CH ├──────────────────────────────┤
  329.       │      File size, 4 bytes      │ Note 5
  330.   20H └──────────────────────────────┘
  331.  
  332.   Figure 10-5.  Format of a single entry in a disk directory. Total length
  333.   is 32 bytes (20H bytes).
  334.  
  335.   ──────────────────────────────────────────────────────────────────────────
  336.   Notes for Figure 10-5
  337.     1.  The first byte of the filename field of a directory entry may
  338.         contain the following special information:
  339.  
  340.     Value             Meaning
  341.     ────────────────────────────────────────────────────────────────────────
  342.     00H               Directory entry has never been used; end of occupied
  343.                       portion of directory.
  344.     05H               First character of filename is actually E5H.
  345.     2EH               Entry is an alias for the current or parent directory.
  346.                       If the next byte is also 2EH, the cluster field
  347.                       contains the cluster number of the parent directory
  348.                       (zero if the parent directory is the root directory).
  349.     E5H               File has been erased.
  350.     ────────────────────────────────────────────────────────────────────────
  351.  
  352.     2.  The attribute byte of the directory entry is mapped as follows:
  353.  
  354.     Bit               Meaning
  355.     ────────────────────────────────────────────────────────────────────────
  356.     0                 Read-only; attempts to open file for write or to
  357.                       delete file will fail.
  358.     1                 Hidden file; excluded from normal searches.
  359.     2                 System file; excluded from normal searches.
  360.     3                 Volume label; can exist only in root directory.
  361.     4                 Directory; excluded from normal searches.
  362.     5                 Archive bit; set whenever file is modified.
  363.     6                 Reserved.
  364.     7                 Reserved.
  365.     ────────────────────────────────────────────────────────────────────────
  366.  
  367.     3.  The time field is encoded as follows:
  368.  
  369.     Bits              Contents
  370.     ────────────────────────────────────────────────────────────────────────
  371.     00H─04H           Binary number of 2-second increments (0─29,
  372.                       corresponding to 0─58 seconds)
  373.     05H─0AH           Binary number of minutes (0─59)
  374.     0BH─0FH           Binary number of hours (0─23)
  375.     ────────────────────────────────────────────────────────────────────────
  376.  
  377.     4.  The date field is encoded as follows:
  378.  
  379.     Bits              Contents
  380.     ────────────────────────────────────────────────────────────────────────
  381.     00H─04H           Day of month (1─31)
  382.     05H─08H           Month (1─12)
  383.     09H─0FH           Year (relative to 1980)
  384.     ────────────────────────────────────────────────────────────────────────
  385.  
  386.     5.  The file-size field is interpreted as a 4-byte integer, with the
  387.         low-order 2 bytes of the number stored first.
  388.  
  389.   ──────────────────────────────────────────────────────────────────────────
  390.  
  391.   The root directory has a number of special properties. Its size and
  392.   position are fixed and are determined by the FORMAT program when a disk is
  393.   initialized. This information can be obtained from the boot sector's BPB.
  394.   If the disk is bootable, the first two entries in the root directory
  395.   always describe the files containing the MS-DOS BIOS and the MS-DOS
  396.   kernel. The disk bootstrap routine uses these entries to bring the
  397.   operating system into memory and start it up.
  398.  
  399.   Figure 10-6 shows a partial hex dump of the first sector of the root
  400.   directory on a bootable PC-DOS 3.3 floppy disk.
  401.  
  402.   ──────────────────────────────────────────────────────────────────────────
  403.          0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
  404.   0000  49 42 4D 42 49 4F 20 20 43 4F 4D 27 00 00 00 00  IBMBIO  COM'....
  405.   0010  00 00 00 00 00 00 00 60 72 0E 02 00 54 56 00 00  .......'r...TV..
  406.   0020  49 42 4D 44 4F 53 20 20 43 4F 4D 27 00 00 00 00  IBMDOS  COM'....
  407.   0030  00 00 00 00 00 00 00 60 71 0E 18 00 CF 75 00 00  .......'q....u..
  408.   0040  43 4F 4D 4D 41 4E 44 20 43 4F 4D 20 00 00 00 00  COMMAND COM ....
  409.   0050  00 00 00 00 00 00 00 60 71 0E 36 00 DB 62 00 00  .......'q.6..b..
  410.   0060  42 4F 4F 54 44 49 53 4B 20 20 20 28 00 00 00 00  BOOTDISK   (....
  411.   0070  00 00 00 00 00 00 A1 00 21 00 00 00 00 00 00 00  ........!.......
  412.   0080  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
  413.   0090  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
  414.         .
  415.         .
  416.         .
  417.   ──────────────────────────────────────────────────────────────────────────
  418.  
  419.   Figure 10-6.  Partial hex dump of the first sector of the root directory
  420.   for a PC-DOS 3.3 disk containing the three system files and a volume
  421.   label.
  422.  
  423.  
  424. The Files Area
  425.  
  426.   The remainder of the volume after the root directory is known as the files
  427.   area. MS-DOS views the sectors in this area as a pool of clusters, each
  428.   containing one or more logical sectors, depending on the disk format. Each
  429.   cluster has a corresponding entry in the FAT that describes its current
  430.   use: available, reserved, assigned to a file, or unusable (because of
  431.   defects in the medium). Because the first two fields of the FAT are
  432.   reserved, the first cluster in the files area is assigned the number 2.
  433.  
  434.   When a file is extended under versions 1 and 2, MS-DOS searches the FAT
  435.   from the beginning until it finds a free cluster (designated by a zero FAT
  436.   field); it then changes that FAT field to a last-cluster mark and updates
  437.   the previous last cluster of the file's chain to point to the new last
  438.   cluster. Under versions 3.0 and later, however, MS-DOS searches the FAT
  439.   from the most recently allocated cluster; this reduces file fragmentation
  440.   and improves overall access times.
  441.  
  442.   Directories other than the root directory are simply a special type of
  443.   file. Their storage is allocated from the files area, and their contents
  444.   are 32-byte entries──in the same format as those used in the root
  445.   directory──that describe files or other directories. Directory entries
  446.   that describe other directories contain an attribute byte with bit 4 set,
  447.   zero in the file-length field, and the date and time that the directory
  448.   was created (Figure 10-7). The first cluster field points, of course, to
  449.   the first cluster in the files area that belongs to the directory. (The
  450.   directory's other clusters can be found only by tracing through the FAT.)
  451.  
  452.   All directories except the root directory contain two special directory
  453.   entries with the names . and ... MS-DOS puts these entries in place when
  454.   it creates a directory, and they cannot be deleted. The . entry is an
  455.   alias for the current directory; its cluster field points to the cluster
  456.   in which it is found. The .. entry is an alias for the directory's parent
  457.   (the directory immediately above it in the tree structure); its cluster
  458.   field points to the first cluster of the parent directory. If the parent
  459.   is the root directory, the cluster field of the .. entry contains zero
  460.   (Figure 10-8).
  461.  
  462.   ──────────────────────────────────────────────────────────────────────────
  463.         .
  464.         .
  465.         .
  466.   0080  4D 59 44 49 52 20 20 20 20 20 20 10 00 00 00 00  MYDIR      .....
  467.   0090  00 00 00 00 00 00 87 9A 9B 0A 2A 00 00 00 00 00  ..........*.....
  468.         .
  469.         .
  470.         .
  471.   ──────────────────────────────────────────────────────────────────────────
  472.  
  473.   Figure 10-7.  Extract from the root directory of an MS-DOS disk, showing
  474.   the entry for a subdirectory named MYDIR. Bit 4 in the attribute byte is
  475.   set, the cluster field points to the first cluster of the subdirectory
  476.   file, the date and time stamps are valid, but the file length is zero.
  477.  
  478.   ──────────────────────────────────────────────────────────────────────────
  479.          0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
  480.   0000  2E 20 20 20 20 20 20 20 20 20 20 10 00 00 00 00  .         .....
  481.   0010  00 00 00 00 00 00 87 9A 9B 0A 2A 00 00 00 00 00  ..........*.....
  482.   0020  2E 2E 20 20 20 20 20 20 20 20 20 10 00 00 00 00  ..        .....
  483.   0030  00 00 00 00 00 00 87 9A 9B 0A 00 00 00 00 00 00  ................
  484.   0040  4D 59 46 49 4C 45 20 20 44 41 54 20 00 00 00 00  MYFILE  DAT ....
  485.   0050  00 00 00 00 00 00 98 9A 9B 0A 2B 00 15 00 00 00  ..........+.....
  486.   0060  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
  487.   0070  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
  488.         .
  489.         .
  490.         .
  491.   ──────────────────────────────────────────────────────────────────────────
  492.  
  493.   Figure 10-8.  Hex dump of the first block of the directory MYDIR. Note the
  494.   . and .. entries. This directory contains exactly one file, MYFILE.DAT.
  495.  
  496.  
  497. Interpreting the File Allocation Table
  498.  
  499.   Now that we understand how the disk is structured, let's see how we can
  500.   use this knowledge to find a FAT position from a cluster number.
  501.  
  502.   If the FAT has 12-bit entries, use the following procedure:
  503.  
  504.   1.  Use the directory entry to find the starting cluster of the file in
  505.       question.
  506.  
  507.   2.  Multiply the cluster number by 1.5.
  508.  
  509.   3.  Use the integral part of the product as the offset into the FAT and
  510.       move the word at that offset into a register. Remember that a FAT
  511.       position can span a physical disk-sector boundary.
  512.  
  513.   4.  If the product is a whole number, AND the register with 0FFFH.
  514.  
  515.   5.  Otherwise, "logical shift" the register right 4 bits.
  516.  
  517.   6.  If the result is a value from 0FF8H through 0FFFH, the file has no
  518.       more clusters. Otherwise, the result is the number of the next cluster
  519.       in the file.
  520.  
  521.   On disks with at least 4087 clusters formatted under MS-DOS version 3.0 or
  522.   later, the FAT entries use 16 bits, and the extraction of a cluster number
  523.   from the table is much simpler:
  524.  
  525.   1.  Use the directory entry to find the starting cluster of the file in
  526.       question.
  527.  
  528.   2.  Multiply the cluster number by 2.
  529.  
  530.   3.  Use the product as the offset into the FAT and move the word at that
  531.       offset into a register.
  532.  
  533.   4.  If the result is a value from 0FFF8H through 0FFFFH, the file has no
  534.       more clusters. Otherwise, the result is the number of the next cluster
  535.       in the file.
  536.  
  537.   To convert cluster numbers to logical sectors, subtract 2, multiply the
  538.   result by the number of sectors per cluster, and add the logical-sector
  539.   number of the beginning of the data area (this can be calculated from the
  540.   information in the BPB).
  541.  
  542.   As an example, let's work out the disk location of the file IBMBIO.COM,
  543.   which is the first entry in the directory shown in Figure 10-6. First, we
  544.   need some information from the BPB, which is in the boot sector of the
  545.   medium. (See Figures 10-3 and 10-4.) The BPB tells us that there are
  546.  
  547.   ■  512 bytes per sector
  548.  
  549.   ■  2 sectors per cluster
  550.  
  551.   ■  2 sectors per FAT
  552.  
  553.   ■  2 FATs
  554.  
  555.   ■  112 entries in the root directory
  556.  
  557.   From the BPB information, we can calculate the starting logical-sector
  558.   number of each of the disk's control areas and the files area by
  559.   constructing a table, as follows:
  560.  
  561.                                                    Length       Sector
  562.   Area                                             (sectors)    numbers
  563.   ──────────────────────────────────────────────────────────────────────────
  564.   Boot sector                                      1            00H
  565.   2 FATs * 2 sectors/FAT                           4            01H─04H
  566.   112 directory entries                            7            05H─0BH
  567.     *32 bytes/entry
  568.     /512 bytes/sector
  569.   Total sectors occupied by bootstrap, FATs, and   12
  570.   root directory
  571.   ──────────────────────────────────────────────────────────────────────────
  572.  
  573.   Therefore, the first sector of the files area is 12 (0CH).
  574.  
  575.   The word at offset 01AH in the directory entry for IBMBIO.COM gives us the
  576.   starting cluster number for that file: cluster 2. To find the
  577.   logical-sector number of the first block in the file, we can follow the
  578.   procedure given earlier:
  579.  
  580.   1.  Cluster number - 2 = 2 - 2 = 0.
  581.  
  582.   2.  Multiply by sectors per cluster = 0 * 2 = 0.
  583.  
  584.   3.  Add logical-sector number of start of the files area = 0 + 0CH = 0CH.
  585.  
  586.   So the calculated sector number of the beginning of the file IBMBIO.COM is
  587.   0CH, which is exactly what we expect knowing that the FORMAT program
  588.   always places the system files in contiguous sectors at the beginning of
  589.   the data area.
  590.  
  591.   Now let's trace IBMBIO.COM's chain through the file allocation table
  592.   (Figures 10-9 and 10-10). This will be a little tedious, but a detailed
  593.   understanding of the process is crucial. In an actual program, we would
  594.   first read the boot sector using Int 25H, then calculate the address of
  595.   the FAT from the contents of the BPB, and finally read the FAT into
  596.   memory, again using Int 25H.
  597.  
  598.   From IBMBIO.COM's directory entry, we already know that the first cluster
  599.   in the file is cluster 2. To examine that cluster's entry in the FAT, we
  600.   multiply the cluster number by 1.5, which gives 0003H as the FAT offset,
  601.   and fetch the word at that offset (which contains 4003H). Because the
  602.   product of the cluster and 1.5 is a whole number, we AND the word from the
  603.   FAT with 0FFFH, yielding the number 3, which is the number of the second
  604.   cluster assigned to the file.
  605.  
  606.   ──────────────────────────────────────────────────────────────────────────
  607.          0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
  608.   0000  FD FF FF 03 40 00 05 60 00 07 80 00 09 A0 00 0B  ......'........
  609.   0010  C0 00 0D E0 00 0F 00 01 11 20 01 13 40 01 15 60  ......... ....'
  610.   0020  01 17 F0 FF 19 A0 01 1B C0 01 1D E0 01 1F 00 02  ................
  611.   0030  21 20 02 23 40 02 25 60 02 27 80 02 29 A0 02 2B  ! .#.%'.'..)..+
  612.         .
  613.         .
  614.         .
  615.   ──────────────────────────────────────────────────────────────────────────
  616.  
  617.   Figure 10-9.  Hex dump of the first block of the file allocation table
  618.   (track 0, head 0, sector 2) for the PC-DOS 3.3 disk whose root directory
  619.   is shown in Figure 10-6. Notice that the first byte of the FAT contains
  620.   the media descriptor byte for a 5.25-inch, 2-sided, 9-sector floppy disk.
  621.  
  622.   ──────────────────────────────────────────────────────────────────────────
  623.   getfat    proc      near      ; extracts the FAT field
  624.                                 ; for a given cluster
  625.                                 ; call    AX = cluster #
  626.                                 ;      DS:BX = addr of FAT
  627.                                 ; returns AX = FAT field
  628.                                 ; other registers unchanged
  629.  
  630.             push      bx        ; save affected registers
  631.             push      cx
  632.             mov       cx,ax
  633.             shl       ax,1      ; cluster * 2
  634.             add       ax,cx     ; cluster * 3
  635.             test      ax,1
  636.             pushf               ; save remainder in Z flag
  637.             shr       ax,1      ; cluster * 1.5
  638.             add       bx,ax
  639.             mov       ax,[bx]
  640.             popf                ; was cluster * 1.5 whole number?
  641.             jnz       getfat1   ; no, jump
  642.             and       ax,0fffh  ; yes, isolate bottom 12 bits
  643.             jmp       getfat2
  644.   getfat1:  mov       cx,4      ; shift word right 4 bits
  645.             shr       ax,cx
  646.   getfat2:  pop       cx        ; restore registers and exit
  647.             pop       bx
  648.             ret
  649.   getfat    endp
  650.   ──────────────────────────────────────────────────────────────────────────
  651.  
  652.   Figure 10-10.  Assembly-language procedure to access the file allocation
  653.   table (assumes 12-bit FAT fields). Given a cluster number, the procedure
  654.   returns the contents of that cluster's FAT entry in the AX register. This
  655.   simple example ignores the fact that FAT entries can span sector
  656.   boundaries.
  657.  
  658.   To examine cluster 3's entry in the FAT, we multiply 3 by 1.5, which gives
  659.   4.5, and fetch the word at offset 0004H (which contains 0040H). Because
  660.   the product of 3 and 1.5 is not a whole number, we shift the word right
  661.   4 bits, yielding the number 4, which is the number of the third cluster
  662.   assigned to IBMBIO.COM.
  663.  
  664.   In this manner, we can follow the chain through the FAT until we come to a
  665.   cluster (number 23, in this case) whose FAT entry contains the value
  666.   0FFFH, which is an end-of-file marker in FATs with 12-bit entries.
  667.  
  668.   We have now established that the file IBMBIO.COM contains clusters 2
  669.   through 23 (02H─17H), from which we can calculate that logical sectors 0CH
  670.   through 38H are assigned to the file. Of course, the last cluster may be
  671.   only partially filled with actual data; the portion of the last cluster
  672.   used is the remainder of the file's size in bytes (found in the directory
  673.   entry) divided by the bytes per cluster.
  674.  
  675.  
  676. Fixed-Disk Partitions
  677.  
  678.   Fixed disks have another layer of organization beyond the logical volume
  679.   structure already discussed: partitions. The FDISK utility divides a fixed
  680.   disk into one or more partitions consisting of an integral number of
  681.   cylinders. Each partition can contain an independent file system and, for
  682.   that matter, its own copy of an operating system.
  683.  
  684.   The first physical sector on a fixed disk (track 0, head 0, sector 1)
  685.   contains the master boot record, which is laid out as follows:
  686.  
  687.   Bytes              Contents
  688.   ──────────────────────────────────────────────────────────────────────────
  689.   000─1BDH           Reserved
  690.   1BE─1CDH           Partition #1 descriptor
  691.   1CE─1DDH           Partition #2 descriptor
  692.   1DE─1EDH           Partition #3 descriptor
  693.   1EE─1FDH           Partition #4 descriptor
  694.   1FE─1FFH           Signature word (AA55H)
  695.   ──────────────────────────────────────────────────────────────────────────
  696.  
  697.   The partition descriptors in the master boot record define the size,
  698.   location, and type of each partition, as follows:
  699.  
  700.   Byte(s)            Contents
  701.   ──────────────────────────────────────────────────────────────────────────
  702.   00H                Active flag (0 = not bootable, 80H = bootable)
  703.   01H                Starting head
  704.   02H─03H            Starting cylinder/sector
  705.   04H                Partition type
  706.   00H                not used
  707.   01H                FAT file system, 12-bit FAT entries
  708.   04H                FAT file system, 16-bit FAT entries
  709.   05H                extended partition
  710.   06H                "huge partition" (MS-DOS versions 4.0 and later)
  711.   05H                Ending head
  712.   06H─07H            Ending cylinder/sector
  713.   08H─0BH            Starting sector for partition, relative to beginning of
  714.                      disk
  715.   0CH─0FH            Partition length in sectorsThe active flag, which
  716.                      indicates that the partition is bootable, can be set on
  717.                      only one partition at a time.
  718.   ──────────────────────────────────────────────────────────────────────────
  719.  
  720.   MS-DOS treats partition types 1, 4, and 6 as normal logical volumes and
  721.   assigns them their own drive identifiers during the system boot process.
  722.   Partition type 5 can contain multiple logical volumes and has a special
  723.   extended boot record that describes each volume. The FORMAT utility
  724.   initializes MS-DOS fixed-disk partitions, creating the file system within
  725.   the partition (boot record, file allocation table, root directory, and
  726.   files area) and optionally placing a bootable copy of the operating system
  727.   in the file system.
  728.  
  729.   Figure 10-11 contains a partial hex dump of a master block from a fixed
  730.   disk formatted under PC-DOS version 3.3. This dump illustrates the
  731.   partition descriptors for a normal partition with a 16-bit FAT and an
  732.   extended partition.
  733.  
  734.   ──────────────────────────────────────────────────────────────────────────
  735.   0000   .
  736.          .
  737.          .
  738.   0180  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  739.   0190  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  740.   01A0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  741.   01B0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 01
  742.   01C0  01 00 04 04 D1 02 11 00 00 00 EE FF 00 00 00 00
  743.   01D0  C1 04 05 04 D1 FD 54 00 01 00 02 53 00 00 00 00
  744.   01E0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  745.   01F0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA
  746.   ──────────────────────────────────────────────────────────────────────────
  747.  
  748.   Figure 10-11.  A partial hex dump of a master block from a fixed disk
  749.   formatted under PC-DOS version 3.3. This disk contains two partitions. The
  750.   first partition has a 16-bit FAT and is marked "active" to indicate that
  751.   it contains a bootable copy of PC-DOS. The second partition is an
  752.   "extended" partition. The third and fourth partition entries are not used
  753.   in this example.
  754.  
  755.  
  756.  
  757.