home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / DOSREF18.ZIP / CHAPTER.008 < prev    next >
Encoding:
Text File  |  1990-11-07  |  43.7 KB  |  764 lines

  1.  
  2.    **  Programmer's  Technical  Reference  for  MSDOS  and  the  IBM  PC **
  3.                         ┌─────────────────────────────┐
  4.                         │ Shareware Version, 10/26/90 │
  5.                         │  Please Register Your Copy  │
  6.                         └─────────────────────────────┘
  7.                     Copyright (c) 1987, 1990 Dave Williams
  8.                           USA copyright TXG 392-616
  9.                      ISBN 1-878830-02-3 (disk-based text)
  10.  
  11.  
  12.                            C H A P T E R   E I G H T
  13.  
  14.                              DOS DISK INFORMATION
  15.  
  16.  
  17. THE DOS AREA├──────────────────────────────────────────────────────────────────
  18.  
  19.  All disks and diskettes formatted by DOS are created with a sector size of 512
  20. bytes. The DOS area (entire area for a diskette, DOS partition for hard disks)
  21. is formatted as follows:
  22.  
  23.            partition table          - variable size (hard disk only)
  24.            boot record              - 1 sector
  25.            first copy of the FAT    - variable size
  26.            second copy of the FAT   - same size as first copy
  27.            root directory           - variable size
  28.            data area                - variable depending on disk size
  29.  
  30. The following sections describe each of the allocated areas: 
  31.  
  32.  
  33. THE BOOT RECORD├───────────────────────────────────────────────────────────────
  34.  
  35.  The boot record resides on track 0, sector 1, side 0 of every diskette 
  36. formatted by the DOS FORMAT program. For hard disks the boot record resides on
  37. the first sector of the DOS partition. It is put on all disks to provide an
  38. error message if you try to start up with a nonsystem disk in drive A:. If the 
  39. disk is a system disk, the boot record contains a JMP instruction pointing to 
  40. the first byte of the operating system.
  41.  
  42.  If the device is IBM compatible, it must be true that the first sector of the 
  43. first FAT is located at the same sector for all possible media. This is 
  44. because the FAT sector is read before the media is actually determined.
  45.  The information relating to the BPB for a particular media is kept in the boot
  46. sector for the media. In particular, the format of the boot sector is:
  47.  
  48. ┌──────────────────────────────────────────────────────────────────────────────┐
  49. │                        D O S   B O O T   R E C O R D                         │
  50. ├───┬───────┬──────────────────────────────────────────────────────────────────┤
  51. │00h│3 bytes│  JMP to executable code. For DOS 2.x, 3 byte near jump (0E9h).   │
  52. │   │       │  For DOS 3.x, 2 byte near jump (0EBh) followed by a NOP (90h)    │
  53. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  54. │03h│8 bytes│  optional OEM name and version  (such as IBM 3.3)                │
  55. ├───┼───────┼─────┬────────────────────────────────────────────────────────────┤
  56. │0Dh│ byte  │     │  sectors per allocation unit (must be a power of 2)        │
  57. ├───┼───────┤     ├────────────────────────────────────────────────────────────┤
  58. │0Eh│2 bytes│  B  │  reserved sectors (starting at logical sector 0)           │
  59. ├───┼───────┤     ├────────────────────────────────────────────────────────────┤
  60. │10h│ byte  │     │  number of FATs                                            │
  61. ├───┼───────┤     ├────────────────────────────────────────────────────────────┤
  62. │11h│2 bytes│     │  maximum number of root directory entries                  │
  63. ├───┼───────┤     ├────────────────────────────────────────────────────────────┤
  64. │13h│2 bytes│  P  │  number of sectors in logical image (total number of       │
  65. │   │       │     │  sectors in media, including boot sector directories, etc.)│
  66. │   │       │     │  If logical disk size is geater than 32Mb, this value is 0 │
  67. │   │       │     │  and the actual size is reported at offset XXXX            │
  68. ├───┼───────┤     ├────────────────────────────────────────────────────────────┤
  69. │15h│ byte  │  B  │  media descriptor byte                                     │
  70. ├───┼───────┤     ├────────────────────────────────────────────────────────────┤
  71. │16h│2 bytes│     │  number of sectors occupied by a single FAT                │
  72. ├───┼───────┼─────┴────────────────────────────────────────────────────────────┤
  73. │18h│2 bytes│  sectors per track                                               │
  74. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  75. │1Ah│2 bytes│  number of heads                                                 │
  76. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  77. │1Ch│2 bytes│  number of hidden sectors                                        │
  78. └───┴───────┼──────────────────────────────────────────────────────────────────┤
  79.             │                EXTENDED BOOT RECORD (DOS 4.0+)                   │
  80. ┌───┬───────┼──────────────────────────────────────────────────────────────────┤
  81. │1Eh│2 bytes│  number of sectors per track                                     │
  82. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  83. │20h│2 bytes│  number of heads                                                 │
  84. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  85. │22h│2 bytes│  number of hidden sectors                                        │
  86. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  87. │26h│4 bytes│  total number of sectors in media (32MB or larger indicated here)│
  88. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  89. │27h│ byte  │  physical drive number                                           │
  90. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  91. │28h│ byte  │  reserved                                                        │
  92. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  93. │29h│ byte  │  extended boot record signature                                  │
  94. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  95. │30h│4 bytes│  volume serial number (assigned with a random function)          │
  96. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  97. │34h│11bytes│  volume label                                                    │
  98. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  99. │3Fh│8 bytes│  reserved                                                        │
  100. └───┴───────┴──────────────────────────────────────────────────────────────────┘
  101.  
  102.  The three words at the end return information about the media. The number of
  103. heads is useful for supporting different multihead drives that have the same
  104. storage capacity but a different number of surfaces. The number of hidden
  105. sectors is useful for drive partitioning schemes.
  106.  
  107.  DOS 3.2 uses a table called the BIOS Parameter Block (BPB) to determine if a 
  108. disk has a valid File Allocation Table. The BPB is located in the first sector 
  109. of a floppy disk. Although the BPB is supposed to be on every formatted floppy 
  110. disk, some earlier versions of DOS did not create a BPB and instead assumed that
  111. the FAT begins at the second sector of the disk and that the first FAT byte 
  112. (Media Descriptor Byte) describes the disk format.
  113.  DOS 3.2 reads in the whole of the BPB and tries to use it - although,
  114. strangely enough, it seems as if DOS is prepared to cope with a BPB that is
  115. more or less totally blank (it seems to ignore the descriptor byte and treat it
  116. as a DSDD 9-sector disk).
  117.  DOS 3.2 determines if a disk has a valid boot sector by examining the first 
  118. byte of logical sector 0. If that byte it a jump instruction E9h, DOS 3.2 
  119. assumes the rest of the sector is a valid boot sector with a BPB. If the first 
  120. byte is not E9h DOS 3.2 behaves like previous versions, assumes the boot sector
  121. is invalid and uses the first byte of the FAT to determine the media type. 
  122.  If the first byte on the disk happens to be E9h, but the disk does not have a 
  123. BPB, DOS 3.2 will return a disk error message. 
  124.  The real problems occur if some of the BPB data is valid and some isn't.
  125. Apparently some OEMs have assumed that DOS would continue to ignore the
  126. formatting data on the disk, and have failed to write much there during FORMAT
  127. except the media descriptor byte (or, worse, have allowed random junk to be
  128. written there). While this error is understandable, and perhaps even
  129. forgiveable, it remains their problem, not IBMs, since the BPB area has always
  130. been documented as containing the format information that IBM DOS 3.2 now
  131. requires to be there. 
  132.  
  133.  
  134.  
  135. THE DOS FILE ALLOCATION TABLE (FAT)├───────────────────────────────────────────
  136.                                                         
  137.  The File Allocation Table, or FAT, has three pain purposes:
  138.         1) to mark bad sectors on the media
  139.         2) to determine which sectors are free for use
  140.         3) to determine the physical location(s) of a file on the media.
  141.  
  142.  DOS uses one of four different schemes for defining the File Allocation Table:
  143.         1) the 12-bit FAT, for DOS 1.x, 2.x, all floppies, and small hard disks
  144.         2) the 16-bit FAT, for DOS 3.x+ hard disks from 16.8 to 32Mb
  145.         3) the 32-bit FAT used by Compaq DOS 3.31 and others
  146.         4) the 32-bit FAT used by DOS 4.0 over-32Mb logical drives
  147.  
  148.  This section explains how DOS uses the FAT to convert the clusters of a file 
  149. into logical sector numbers. We recommend that system utilities use the DOS 
  150. handle calls rather than interpreting the FAT.
  151.  
  152.  The FAT is used by DOS to allocate disk space for files, one cluster at a time.
  153. In DOS 4.0, clusters are referred to as "allocation units." It means the same 
  154. things; the smallest logical portion of a drive.
  155.  
  156.  The FAT consists of a 12 bit entry (1.5 bytes) for each cluster on the disk or
  157. a 16 bit (2 bytes) entry when a hard disk has more than 20740 sectors as is the
  158. case with fixed disks larger than 10Mb.
  159.  The first two FAT entries map a portion of the directory; these FAT entries 
  160. contain indicators of the size and format of the disk. The FAT can be in a 12 
  161. or 16 bit format. DOS determines whether a disk has a 12 or 16 bit FAT by 
  162. looking at the total number of allocation units on a disk. For all diskettes 
  163. and hard disks with DOS partitions less than 20,740 sectors, the FAT uses a 12 
  164. bit value to map a cluster. For larger partitions, DOS uses a 16 bit value.
  165.  The second, third, and fourth bit applicable for 16 bit FAT bytes always 
  166. contains 0FFFFh. The first byte is used as follows:
  167.  
  168. ┌──────────────────────────────────────────────────────────────────────────────┐
  169. │                 M E D I A    D E S C R I P T O R    B Y T E                  │
  170. ├──────────┬──────────────────────────────────┬────────────────────────────────┤
  171. │hex value │              meaning             │         normally used          │
  172. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  173. │    F0    │ double sided  18 sector diskette │ PS/2 1.44 meg DSHD             │
  174. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  175. │    F8    │ hard disk                        │ bootable hard disk at C:800    │
  176. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  177. │    F9    │ double sided  15 sector diskette │ AT 1.2 meg DSHD                │
  178. │          │ double sided  9  sector diskette │ Convertible 720k DSQD          │
  179. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  180. │    FA    │ IBM Displaywriter System disk    │ 287k                           │
  181. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  182. │    FB    │ IBM Displaywriter System disk    │ 1 meg                          │
  183. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  184. │    FC    │ single sided  9  sector diskette │ DOS 2.0, 180k SSDD             │
  185. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  186. │    FD    │ double sided  9  sector diskette │ DOS 2.0, 360k DSDD             │
  187. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  188. │    FE    │ single sided  8  sector diskette │ DOS 1.0, 160k SSDD             │
  189. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  190. │    FF    │ double sided  8  sector diskette │ DOS 1.1, 320k SSDD             │
  191. ├──────────┴───────────┬──────────────────────┴────────────────────────────────┘
  192. │for 8 inch diskettes: │
  193. ├──────────┬───────────┴──────────────────────┬────────────────────────────────┐
  194. │    FD    │ double sided  26 sector diskette │ IBM 3740 format DSSD           │
  195. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  196. │    FE    │ single sided  26 sector diskette │ IBM 3740 format SSSD           │
  197. │          ├──────────────────────────────────┼────────────────────────────────┤
  198. │          │ double sided  8  sector diskette │ IBM 3740 format DSDD           │
  199. └──────────┴──────────────────────────────────┴────────────────────────────────┘
  200.  
  201.  
  202. The third FAT entry begins mapping the data area (cluster 002).
  203.  
  204. NOTE: These values are provided as a reference. Therefore, programs should not 
  205.       make use of these values.
  206.  
  207.  Each entry contains a hexadecimal character (or 4 for 16 bit FATs). () 
  208. indicates the high order four bit value in the case of 16 bit FAT entries. 
  209. They can be:
  210.  
  211.             (0)000h  if the cluster is unused and availible
  212.  
  213. (0F)FF8h - (0F)FFFh  to indicate the last cluster of a file
  214.  
  215.             (X)XXXh  any other hexadecimal numbers that are the cluster number
  216.                      of the next cluster in the file. The cluster number is the
  217.                      first cluster in the file that is kept in the file's 
  218.                      directory entry.
  219.  
  220.  The values (0F)FF0h - (0F)FF7h are used to indicate reserved clusters. 
  221. (0F)FF7h indicates a bad cluster if it is not part of the allocation chain. 
  222. (0F)FF8h - (0F)FFFh are used as end of file markers.
  223.  
  224.  The file allocation table always occupies the sector or sectors immediately 
  225. following the boot record. If the FAT is larger than 1 sector, the sectors 
  226. occupy consecutive sector numbers. Two copies of the FAT are written, one 
  227. following the other, for integrity. The FAT is read into one of the DOS buffers
  228. whenever needed (open, allocate more space, etc).
  229.  
  230.  
  231.  
  232. USE OF THE 12 BIT FILE ALLOCATION TABLE├───────────────────────────────────────
  233.  
  234. Obtain the starting cluster of the file from the directory entry.
  235.  
  236. Now, to locate each subsequent sector of the file:
  237.  
  238. 1. Multiply the cluster number just used by 1.5 (each FAT entry is 1.5 
  239.    bytes long).
  240. 2. The whole part of the product is offset into the FAT, pointing to the entry 
  241.    that maps the cluster just used. That entry contains the cluster number of 
  242.    the next cluster in the file.
  243. 3. Use a MOV instruction to move the word at the calculated FAT into a register.
  244. 4. If the last cluster used was an even number, keep the low order 12 bits of 
  245.    the register, otherwise, keep the high order 12 bits.
  246. 5. If the resultant 12 bits are (0FF8h-0FFFh) no more clusters are in the file.
  247.    Otherwise, the next 12 bits contain the cluster number of the next cluster in
  248.    the file. 
  249.  
  250.   To convert the cluster to a logical sector number (relative sector, such as 
  251. that used by int 25h and 26h and DEBUG):
  252.  
  253. 1. Subtract 2 from the cluster number
  254. 2. Multiply the result by the number of sectors per cluster.
  255. 3. Add the logical sector number of the beginning of the data area.
  256.  
  257. 12-bit FAT if DOS partition is smaller than 32,680 sectors (16.340 MB).
  258.  
  259.  
  260. USE OF THE 16 BIT FILE ALLOCATION TABLE├───────────────────────────────────────
  261.  
  262.  Obtain the starting cluster of the file from the directory entry. Now to 
  263. locate each subsequent cluster of the file:
  264.  
  265. 1.  Multiply the cluster number used by 2 (each FAT entry is 2 bytes long).
  266. 2.  Use the MOV word instruction to move the word at the calculated FAT offset 
  267.     into a register.
  268. 3.  If the resultant 16 bits are (0FF8h-0FFFFh) no more clusters are in the 
  269.     file. Otherwise, the 16 bits contain the cluster number of the next cluster 
  270.     in the file. 
  271.  
  272.  Compaq DOS makes availible a new disk type (6) with 32 bit partition values, 
  273. allowing 512 megabytes per hard disk (Compaq DOS 3.3.1)
  274.  
  275.  
  276.  
  277. DOS DISK DIRECTORY├────────────────────────────────────────────────────────────
  278.  
  279.  The FORMAT command initially builds the root directory for all disks. Its 
  280. location (logical sector number) and the maximum number of entries are 
  281. availible through the device driver interfaces.
  282.  
  283.  Since directories other than the root directory are actually files, there is 
  284. no limit to the number of entries that they may contain. 
  285.  
  286.  All directory entries are 32 bytes long, and are in the following format:
  287. ┌───────┬──────────────────────────────────────────────────────────────────────
  288. │ BYTES │                      DISK DIRECTORY ENTRY
  289. ├───────┼──────────────────────────────────────────────────────────────────────
  290. │  0-7  │ 8 bytes filename
  291. └───────┼──────────────────────────────────────────────────────────────────────
  292.         │ The first byte of the filename indicates the file status.
  293.         │ The file status byte can contain the following values:
  294.         ├──────┬───────────────────────────────────────────────────────────────
  295.         │  00h │ Directory entry has never been used. This is used to limit
  296.         │      │ the length of directory searches, for performance reasons.
  297.         │  05h │ Indicates that the first character of the filename actually
  298.         │      │ has an 0EDh character.
  299.         │ 0E5h │ Filename has been used but the file has been erased.
  300.         │  2Eh │ This entry is for a directory. If the second byte is also
  301.         │      │ 2Eh, the cluster field contains the cluster number of this
  302.         │      │ directory's parent directory. (0000h if the parent directory
  303.         │      │ is the root directory).
  304.         ├──────┴───────────────────────────────────────────────────────────────
  305.         │ Any other character is the first character of a filename. Filenames
  306.         │ are left-aligned and if nescessary padded with blanks.
  307. ┌───────┼──────────────────────────────────────────────────────────────────────
  308. │  8-A  │ 3 bytes filename extension if any
  309. └───────┼──────────────────────────────────────────────────────────────────────
  310.         │ Three characters, left-aligned and padded with blanks if nescessary.
  311.         │ If there is no file extension, this field contains all blanks
  312. ┌───────┼──────────────────────────────────────────────────────────────────────
  313. │   B   │ 1 byte file attributes
  314. └───────┼──────────────────────────────────────────────────────────────────────
  315.         │  The attribute byte is mapped as follows:
  316.         ├─────┬───┬────────────────────────────────────────────────────────────
  317.         │ hex │bit│                        meaning
  318.         ├─────┼───┼────────────────────────────────────────────────────────────
  319.         │ 00h │   │ (no bits set) normal; can be read or written w/o restriction
  320.         │ 01h │ 0 │ file is marked read-only. An attempt to open the file for
  321.         │     │   │ output using function call 3Dh will fail and an error code
  322.         │     │   │ will be returned. This value can be used with other values
  323.         │     │   │ below.
  324.         │ 02h │ 1 │ indicates a hidden file. The file is excluded from normal
  325.         │     │   │ directory searches.
  326.         │ 04h │ 2 │ indicates a system file. This file is excluded from normal
  327.         │     │   │ directory searches.
  328.         │ 08h │ 3 │ indicates that the entry contains the volume label in the
  329.         │     │   │ first 11 bytes. The entry has no other usable information
  330.         │     │   │ and may exist only in the root directory.
  331.         │ 10h │ 4 │ indicates that the file is a subdirectory
  332.         │ 20h │ 5 │ indicates an archive bit. This bit is set on whenever the
  333.         │     │   │ file is written to and closed. Used by BACKUP and RESTORE.
  334.         │     │ 6 │ reserved
  335.         │     │ 7 │ reserved
  336.         ├─────┴───┴────────────────────────────────────────────────────────────
  337.         │ Bits 6 and 7 are reserved, and must be 0. They may be used in OS/2.
  338.         │ note: Attributes 08h and 10h cannot be changed using int21/43h.
  339.         │
  340.         │ The system files IBMBIO.COM and IBMDOS.COM (or customized equivalent)
  341.         │ are marked as read-only, hidden, and system files. Files can be
  342.         │ marked hidden when they are created. Also, the read-only, hidden,
  343.         │ system and archive attributes may be changed with int21/43h.
  344. ┌───────┼──────────────────────────────────────────────────────────────────────
  345. │ 0C-15 │ 10 bytes reserved by DOS; value unknown
  346. ├───────┼──────────────────────────────────────────────────────────────────────
  347. │ 16-17 │ 2 bytes file timestamp
  348. └───────┼──────────────────────────────────────────────────────────────────────
  349.         │ These bytes contain the time when the file was created or last
  350.         │  updated. The time is mapped in the bits as follows:
  351.         ├───────────────────────────────┬───────────────────────────────┐
  352.         │         B Y T E   16h         │         B Y T E   17h         │
  353.         ├───────────────────────────────┼───────────────────────────────┤
  354.         │ F   E   D   C   B   A   9   8 │ 7   6   5   4   3   2   1   0 │
  355.         ├───────────────────┬───────────┴───────────┬───────────────────┤
  356.         │ H   H   H   H   H │ M   M   M   M   M   M │ D   D   D   D   D │
  357.         ├───────────────────┼───────────────────────┼───────────────────┤
  358.         │ binary # hrs 0-23 │ binary # minutes 0-59 │ bin. # 2-sec incr │
  359.         ├───────────────────┴───────────────────────┴───────────────────┘
  360.         │ note: The time is stored with the least significant byte first.
  361. ┌───────┼──────────────────────────────────────────────────────────────────────
  362. │ 18-19 │ 2 bytes file datestamp
  363. └───────┼──────────────────────────────────────────────────────────────────────
  364.         │ This area contains the date when the file was created or last
  365.         │ updated. The mm/dd/yy are mapped in the bits as follows:
  366.         ├───────────────────────────────┬───────────────────────────────┐
  367.         │         B Y T E   18h         │         B Y T E   19h         │
  368.         ├───────────────────────────────┼───────────────────────────────┤
  369.         │ F   E   D   C   B   A   9   8 │ 7   6   5   4   3   2   1   0 │
  370.         ├───────────────────────────┬───┴───────────┬───────────────────┤
  371.         │ Y   Y   Y   Y   Y   Y   Y │ M   M   M   M │ D   D   D   D   D │
  372.         ├───────────────────────────┼───────────────┼───────────────────┤
  373.         │     0-119 (1980-2099)     │     1-12      │       1-31        │
  374.         ├───────────────────────────┴───────────────┴───────────────────┘
  375.         │ note: The time is stored with the least significant byte first.
  376. ┌───────┼──────────────────────────────────────────────────────────────────────
  377. │ 1A-1B │ 2 bytes first file cluster number 
  378. └───────┼──────────────────────────────────────────────────────────────────────
  379.         │ * (reserved in DOS 2, documented in DOS 3+)
  380.         │ This area contains the starting cluster number of the first cluster
  381.         │ in the file. The first cluster for data space on all fixed disks and
  382.         │ floppy disks is always cluster 002. The cluster number is stored
  383.         │ with the least significant byte first.
  384. ┌───────┼──────────────────────────────────────────────────────────────────────
  385. │ 1C-1F │ 4 bytes filesize
  386. └───────┼──────────────────────────────────────────────────────────────────────
  387.         │ This area contains the file size in bytes. The first word contains
  388.         │ the low order part of the size. Both words are stored with the least
  389.         │ significant byte first.
  390.         └──────────────────────────────────────────────────────────────────────
  391.  
  392.  
  393.  
  394. THE DATA AREA├─────────────────────────────────────────────────────────────────
  395.  
  396.  Allocation of space for a file (in the data area) is done only when needed 
  397. (it is not preallocated). The space is allocated one cluser (unit allocation) 
  398. at a time. A cluster is always one or more consecutive sector numbers, and all 
  399. of the clusters in a file are "chained" together in the FAT. 
  400.  
  401.  The clusters are arranged on disk to minimize head movement for multisided 
  402. media. All of the space on a track (or cylinder) is allocated before moving 
  403. on to the next track. This is accomplished by using the sequential sector 
  404. numbers on the lowest-numbered head, then all the sector numbers on the next 
  405. head, and so on until all sectors of all heads of the track are used. Then the 
  406. next sector used will be sector 1 of head 0 on the next track.
  407.  
  408.  An interesting innovation that was introduced in MS-DOS 3.0: disk space that 
  409. is freed by erasing a file is not re-used immediately, unlike earlier versions 
  410. of DOS. Instead, free space is obtained from the area not yet used during the 
  411. current session, until all of it is used up. Only then will space that is freed
  412. during the current session be re-used.
  413.  
  414.  This feature minimizes fragmentation of files, since never-before-used space
  415. is always contiguous. However, once any space has been freed by deleting a file,
  416. that advantage vanishes at the next system boot. The feature also greatly
  417. simplifies un-erasing files, provided that the need to do an un-erase is found
  418. during the same session and also provided that the file occupies contiguous 
  419. clusters.
  420.  
  421.  However, when one is using programs which make extensive use of temporary
  422. files, each of which may be created and erased many times during a session,
  423. the feature becomes a nuisance; it forces the permanent files to move farther
  424. and farther into the inner tracks of the disk, thus increasing rather than
  425. decreasing the amount of fragmentation which occurs.
  426.  
  427.  The feature is implemented in DOS by means of a single 16-bit "last cluster
  428. used" (LCU) pointer for each physical disk drive; this pointer is a part of
  429. the physical drive table maintained by DOS. At boot time, the LCU pointer is
  430. zeroed. Each time another cluster is obtained from the free-space pool (the
  431. FAT), its number is written into the LCU pointer. Each time a fresh cluster
  432. is required, the FAT is searched to locate a free one; in older versions of
  433. DOS this search always began at Cluster 0000, but in 3.x it begins at the
  434. cluster pointed to by the LCU pointer.
  435.  
  436.  For hard disks, the size of the file allocation table and directory are 
  437. determined when FORMAT initializes it and are based on the size of the DOS 
  438. partition.
  439.  
  440.  Files in the data area are not nescessarily written sequentially on the first.
  441. The data area space is allocated one cluster at a time, skipping over clusters 
  442. already allocated. The first free cluster found is the next cluster allocated, 
  443. regardless of its physical location on the disk. This permits the most efficient
  444. utilization of disk space because clusters freed by erasing files can be 
  445. allocated for new files. Refer back to the description of the DOS FAT in this 
  446. chapter for more information.
  447.  
  448.         SSDD    single sided, double density    (160-180k)     5¼
  449.         DSDD    double sided, double density    (320-360k)     5¼
  450.         DSQD    double sided, quad density      (720k)         5¼, 3½
  451.         DSHD    double sided, high density      (1.2-1.44M)    5¼, 3½
  452.  
  453.  Much of the trouble with AT 1.2 meg drives has been through the inadverdent 
  454. use of quad density disks in the high density drives. The high density disks 
  455. use a higher-coercivity media than the quads, and quads are not completely 
  456. reliable as 1.2M. Make sure you have the correct disk for your application.
  457.      
  458.  
  459.  
  460. HARD DISK LAYOUT├──────────────────────────────────────────────────────────────
  461.  
  462.  The DOS hard disk routines perform the following services:
  463.  
  464. 1) Allow multiple operating systems to utilize the hard disk without the need 
  465.    to backup and restore files when changing operating systems.
  466.  
  467. 2) Allow a user-selected operating system to be started from the hard disk.
  468.    
  469.    I) In order to share the hard disk among operating systems, the disk may be 
  470.       logically divided into 1 to 4 partitions. The space within a given 
  471.       partition is contiguous, and can be dedicated to a specific operating 
  472.       system. Each operating system may "own" only one partition in DOS versions
  473.       2.0 through 3.2. PCDOS 3.3 introduced the "Extended DOS Partition" which 
  474.       allows multiple DOS partitions on the same hard disk. The FDISK.COM (or 
  475.       similar program from other DOS vendors) utility allows the user to select
  476.       the number, type, and size of each partition. The partition information is
  477.       kept in a partition table that is embedded in the master fixed disk boot 
  478.       record on the first sector of the disk. The format of this table varies 
  479.       from version to version of DOS.
  480.  
  481.   II) An operating system must consider its partition to be the entire disk, 
  482.       and must ensure that its functions and utilities do not access other 
  483.       partitions on the disk. 
  484.  
  485.  III) Each partition may contain a boot record on its first sector, and any 
  486.       other programs or data that you choose - including a copy of an operating 
  487.       system. For example, the DOS FORMAT command may be used to format and 
  488.       place a copy of DOS in the DOS partition in the same manner that a 
  489.       diskette is formatted. With the FDISK utility, you may designate a 
  490.       partition as "active" (bootable). The master hard disk boot record causes
  491.       that partition's boot record to receive control when the system is started
  492.       or reset. Additional disk partitions could be FORTH, UNIX, Pick, CP/M-86,
  493.       or the UCSD p-System.
  494.  
  495.  
  496.  
  497. SYSTEM INITIALIZATION├─────────────────────────────────────────────────────────
  498.  
  499. The boot sequence is as follows:
  500.  
  501. 1. System initialization first attempts to load an operating system from 
  502.    diskette drive A. If the drive is not ready or a read error occurs, it then 
  503.    attempts to read a master hard disk boot record on the first sector of the 
  504.    first hard disk in the system. If unsuccessful, or if no hard disk is 
  505.    present, it invokes ROM BASIC in an IBM PC or displays a disk error 
  506.    message on most compatibles.
  507.  
  508. 2. If initialization is successful, the master hard disk boot record is given 
  509.    control and it examines the partition table embedded within it. If one of 
  510.    the entries indicates an active (bootable) partition, its boot record is 
  511.    read from the partition's first sector and given control.
  512.  
  513. 3. If none of the partitions is bootable, ROM BASIC is invoked on an IBM PC or
  514.     a disk error on most compatibles.
  515.  
  516. 4. If any of the boot indicators are invalid, or if more than one indicator is 
  517.    marked as bootable, the message INVALID PARTITION TABLE is displayed and the 
  518.    system stops.
  519.  
  520. 5. If the partition's boot record cannot be successfully read within five 
  521.    retries due to read errors, the message ERROR LOADING OPERATING SYSTEM 
  522.    appears and the system stops.
  523.  
  524. 6. If the partition's boot record does not contain a valid "signature", the 
  525.    message MISSING OPERATING SYSTEM appears, and the system stops.
  526.  
  527. NOTE: When changing the size or location of any partition, you must ensure that
  528.       all existing data on the disk has been backed up. The partitioning program
  529.       will destroy the data on the disk.
  530.  
  531.  System programmers designing a utility to initialize/manage a hard disk must 
  532. provide the following functions at a minimum:
  533.  
  534. 1. Write the master disk boot record/partition table to the disk's first 
  535.    sector to initialize it.
  536.  
  537. 2. Perform partitioning of the disk-that is, create or update the partition 
  538.    table information (all fields for the partition) when the user wishes 
  539.    to create a partition. This may be limited to creating a partition for only 
  540.    one type of operating systewm, but must allow repartitoning the entire disk,
  541.    or adding a partition without interfering with existing partitions (user's 
  542.    choice).
  543.  
  544. 3. Provide a means for marking a user-specified partition as bootable and 
  545.    resetting the bootable indicator bytes for all other partitions at the same 
  546.    time.
  547.  
  548. 4. Such utilities should not change or move any partition information that 
  549.    belongs to another operating system.
  550.  
  551.  
  552.  
  553.  
  554. BOOT RECORD/PARTITION TABLE├───────────────────────────────────────────────────
  555.  
  556.  A boot record must be written on the first sector of all hard disks, and 
  557. must contain the following:
  558.  
  559. 1. Code to load and give control to the boot record for one of four possible 
  560.    operating systems.
  561.  
  562. 2. A partition table at the end of the boot record. Each table entry is 16 
  563.    bytes long, and contains the starting and ending cylinder, sector, and head 
  564.    for each of four possible partitions, as well as the number of sectors 
  565.    preceding the partition and the number of sectors occupied by the partition. 
  566.    The "boot indicator" byte is used by the boot record to determine if one of 
  567.    the partitions contains a loadable operating system. FDISK initialization 
  568.    utilities mark a user-selected partition as "bootable" by placing a value 
  569.    of 80h in the corresponding partition's boot indicator (setting all other 
  570.    partitions' indicators to 0 at the same time). The presence of the 80h tells 
  571.    the standard boot routine to load the sector whose location is contained in 
  572.    the following three bytes. That sector is the actual boot record for the 
  573.    selected operating system, and it is responsible for the remainder of the 
  574.    system's loading process (as it is from the diskette). All boot records are 
  575.    loaded at absolute address 0:7C00.
  576.  
  577. The partition table with its offsets into the boot record is:
  578. (except for Wyse DOS 3.2 with 32 bit allocation table, and DOS 3.3-up)
  579. ┌──────┬────────┬──────────────────┬──────────┬──────────┬──────────┬──────────┐
  580. │Offset│Partit'n│     Purpose      │          │   Head   │  Sector  │ Cylinder │
  581. ├──────┼────────┼──────────────────┼──────────┼──────────┼──────────┼──────────┤
  582. │ 1BEh │ part 1 │ begin            │ boot ind │    H     │    S     │   cyl    │
  583. ├──────┼────────┼──────────────────┼──────────┼──────────┼──────────┼──────────┤
  584. │ 1C2h │        │ end              │ syst ind │    H     │    S     │   cyl    │
  585. ├──────┤        ├──────────────────┼──────────┴──────────┼──────────┴──────────┤
  586. │ 1C6h │        │ relative sector  │      low word       │      high word      │
  587. ├──────┤        ├──────────────────┼─────────────────────┼─────────────────────┤
  588. │ 1CAh │        │ # sectors        │      low word       │      high word      │
  589. ├──────┼────────┼──────────────────┼──────────┬──────────┼──────────┬──────────┤
  590. │ 1CEh │ part 2 │ begin            │ boot ind │    H     │    S     │   cyl    │
  591. ├──────┤        ├──────────────────┼──────────┼──────────┼──────────┼──────────┤
  592. │ 1D2h │        │ end              │ syst ind │    H     │    S     │   cyl    │
  593. ├──────┤        ├──────────────────┼──────────┴──────────┼──────────┴──────────┤
  594. │ 1D6h │        │ relative sector  │      low word       │      high word      │
  595. ├──────┤        ├──────────────────┼─────────────────────┼─────────────────────┤
  596. │ 1DAh │        │ # sectors        │      low word       │      high word      │
  597. ├──────┼────────┼──────────────────┼──────────┬──────────┼──────────┬──────────┤
  598. │ 1DEh │ part 3 │ begin            │ boot ind │    H     │    S     │   cyl    │
  599. ├──────┤        ├──────────────────┼──────────┼──────────┼──────────┼──────────┤
  600. │ 1E2h │        │ end              │ syst ind │    H     │    S     │   cyl    │
  601. ├──────┤        ├──────────────────┼──────────┴──────────┼──────────┴──────────┤
  602. │ 1E6h │        │ relative sector  │      low word       │      high word      │
  603. ├──────┤        ├──────────────────┼─────────────────────┼─────────────────────┤
  604. │ 1EAh │        │ # sectors        │      low word       │      high word      │
  605. ├──────┼────────┼──────────────────┼──────────┬──────────┼──────────┬──────────┤
  606. │ 1EEh │ part 4 │ begin            │ boot ind │    H     │    S     │   cyl    │
  607. ├──────┤        ├──────────────────┼──────────┼──────────┼──────────┼──────────┤
  608. │ 1F2h │        │ end              │ syst ind │    H     │    S     │   cyl    │
  609. ├──────┤        ├──────────────────┼──────────┴──────────┼──────────┴──────────┤
  610. │ 1F6h │        │ relative sector  │      low word       │      high word      │
  611. ├──────┤        ├──────────────────┼─────────────────────┼─────────────────────┤
  612. │ 1FAh │        │ # sectors        │      low word       │      high word      │
  613. ├──────┼────────┴──────────────────┼──────────┬──────────┼─────────────────────┘
  614. │ 1FEh │         signature         │  hex 55  │  hex AA  │
  615. └──────┴───────────────────────────┴──────────┴──────────┘
  616.  
  617.  
  618.  Boot indicator (boot ind): The boot indicator byte must contain 0 for a non- 
  619. bootable partition or 80h for a bootable partition. Only one partition can be 
  620. marked as bootable at a time.
  621.  
  622.  System Indicator (sys ind): The sys ind field contains an indicator of the 
  623. operating system that "owns" the partition. IBM PC-DOS can only "own" one 
  624. partition, though some versions of MSDOS allow all four partitions to be used 
  625. by DOS. 
  626.  The system indicators are:
  627.  
  628.         ┌─────────────────────────────────────────────────────────────┐
  629.         │                 System Indicator  (sys ind)                 │
  630.         ├───────┬─────────────────────────────────────────────────────┤
  631.         │  00h  │  unknown or unspecified                             │
  632.         ├───────┼─────────────────────────────────────────────────────┤
  633.         │  01h  │  DOS 12 bit FAT  (DOS 2.x all and 3.x under 10 Mb)  │
  634.         ├───────┼─────────────────────────────────────────────────────┤
  635.         │  04h  │  DOS 16 bit FAT  (DOS 3.x+. Not recognized by 2.x)  │
  636.         ├───────┼─────────────────────────────────────────────────────┤
  637.         │  0DBh │  DRI Concurrent DOS                                 │
  638.         ├───────┼─────────────────────────────────────────────────────┤
  639.         │  0F2h │  2nd DOS partition, some 3.2 and all 3.3            │
  640.         └───────┴─────────────────────────────────────────────────────┘
  641.  
  642.  There are bytes for XENIX, and other operating systems. Some manufacturers
  643. (such as Zenith, Wyse, and Tandon) diddle with these system bytes to implement
  644. more than one DOS partition per disk.
  645.  
  646.  
  647.  Cylinder (CYL) and Sector (S): The 1 byte fields labelled CYL contain the low 
  648. order 8 bits of the cylinder number - the high order 2 bits are in the high 
  649. order 2 bits of the sector (S) field. This corresponds with the ROM BIOS 
  650. interrupt 13h (disk I/O) requirements, to allow for a 10 bit cylinder number.
  651.  
  652.  The fields are ordered in such a manner that only two MOV instructions are 
  653. required to properly set up the DX and CX registers for a ROM BIOS call to 
  654. load the appropriate boot record (hard disk booting is only possible from the 
  655. first hard disk in the system, where a BIOS drive number of 80h corresponds 
  656. to the boot indicator byte).
  657.  
  658.  All partitions are allocated in cylinder multiples and begin on sector 1,
  659. head 0, with the exception that the partition that is allocated at the beginning
  660. of the disk starts at sector 2, to account for the hard disk's master boot
  661. record.
  662.  
  663.  Relative Sector (rel sect): The number of sectors preceding each partition 
  664. on the disk is kept in the 4 byte field labelled "rel sect". This value is 
  665. obtained by counting the sectors beginning with cylinder 0, sector 1, head 0 
  666. of the disk, and incrementing the sector, head, and then track values up to 
  667. the beginning of the partition. This, if the disk has 17 sectors per track and 
  668. 4 heads, and the second partition begins at cylinder 1, sector 1, head 0,and 
  669. the partition's starting relative sector is 68 (decimal) - there were 17 
  670. sectors on each of 4 heads on 1 track allocated ahead of it. The field is stored
  671. with the least significant word first.
  672.  
  673.  Number of sectors (#sects): The number of sectors allocated to the partition 
  674. is kept in the "# of sects" field. This is a 4 byte field stored least 
  675. significant word first.
  676.  
  677.  Signature: The last 2 bytes of the boot record (55AAh) are used as a signature
  678. to identify a valid boot record. Both this record and the partition boot record 
  679. are required to contain the signature at offset 1FEh.
  680.  
  681.  
  682. HARD DISK TECHNICAL INFORMATION├───────────────────────────────────────────────
  683.  
  684.  Western Digital's hard disk installation manuals make the claim that MSDOS 
  685. can support only 2 hard drives. This is entirely false, and their purpose for 
  686. making the claim is unclear. DOS merely performs a function call pointed at 
  687. the hard disk driver, which is normally in one of three locations; a ROM at 
  688. absolute address C:800, the main BIOS ROM if the machine is an AT, or a device 
  689. driver installed through the CONFIG.SYS file. Two hard disk controller cards 
  690. can normally not reside in the same machine due to lack of interrupt 
  691. arbitration. Perstor's ARLL controller and some cards marketed by Novell can 
  692. coexist with other controllers. Perstor's technical department has had four 
  693. controllers and eight hard disks in the same IBM XT functioning concurrently.
  694.  
  695.  A valid hard disk has a boot record arranged in the following manner:
  696.  
  697.         DB      drive   ; 0 or 80h  (80h marks a bootable, active partition)
  698.         DB      head1   ; starting heads
  699.         DW      trksec1 ; starting track/sector (CX value for INT 13)
  700.         DB      system  ; see below
  701.         DB      head2   ; ending head
  702.         DW      trksec2 ; ending track/sector
  703.         DD      sector1 ; absolute # of starting sector
  704.         DD      sector2 ; absolute # of last sector
  705.  
  706.  The master disk boot record invokes ROM BASIC if no indicator byte reflects a 
  707. bootable system.
  708.  
  709.  When a partition's boot record is given control, it is passed its partition 
  710. table entry address in the DS:SI registers.
  711.  
  712.  
  713.  
  714. DETERMINING FIXED DISK ALLOCATION├─────────────────────────────────────────────
  715.  
  716. DOS determines disk allocation using the following formula:
  717.               
  718.                                          D * BPD
  719.                             TS - RS -  ───────────
  720.                                            BPS
  721.                       SPF = ──────────────────────────────
  722.                                         BPS * SPC
  723.                                  CF + ──────────────
  724.                                            BPC
  725. where:
  726.  
  727.      TS      total sectors on disk
  728.      RS      the number of sectrs at the beginning of the disk that are reserved
  729.              for the boot record. DOS reserves 1 sector.
  730.      D       The number of directory entries in the root directory.
  731.      BPD     the number of bytes per directory entry. This is always 32.
  732.      BPS     the number of bytes per logical sector. Typically 512, but you can
  733.              specify a different number with VDISK.
  734.      CF      The number of FATS per disk. Usually 2. VDISK is 1.
  735.      SPF     the number of sectors per FAT. Maximum 64.
  736.      SPC     The number of sectors per allocation unit.
  737.      BPC     the number of bytes per FAT entry. BPC is 1.5 for 12 bit FATs.
  738.              2 for 16 bit FATS.
  739.  
  740.  
  741. To calculate the minimum partition size that will force a 16-bit FAT:
  742.  
  743.         CYL = (max clusters * 8)/(HEADS * SPT)
  744.  
  745. where:
  746.      CYL     number of cylinders on the disk
  747.      max clusters  4092 (maximum number of clusters for a 12 bit FAT)
  748.      HEADS   number of heads on the hard disk
  749.      SPT     sectors per track  (normally 17 on MFM)
  750.  
  751.  
  752.  DOS 2.0 uses a "first fit" algorithm when allocating file space on the hard
  753. disk. Each time an application requests disk space, it will scan from the 
  754. beginning of the FAT until it finds a contiguous piece of storage large enough 
  755. for the file.
  756.  DOS 3.0 keeps a pointer into the disk space, and begins its search from the 
  757. point it last left off. This pointer is lost when the system is rebooted. 
  758. This is called the "next fit" algorithm. It is faster than the first fit and 
  759. helps minimize fragmentation.
  760.  In either case, if the FCB function calls are used instead of the handle 
  761. function calls, the file will be broken into pieces starting with the first 
  762. availible space on the disk.
  763.  
  764.