Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
13 KiB

  1. A WALK THROUGH FORMAT
  2. ---------------------
  3. Ver.5.00.410Beta
  4. ----------------
  5. Upon entry to Format, the routine Main_Init is called. This
  6. performs all the parsing of the command line, and determines the
  7. default value for DeviceParameters, which represents the highest
  8. capacity format for the disk drive being used. Upon return, a copy of
  9. SwitchMap (a word value which holds all the switches entered on the
  10. command line) is saved in SwitchCopy.
  11. The memory block allocated to the Format code is then resized
  12. using Int 21h function 4Ah, so as to free extra un-needed memory
  13. allocated for the FORMAT.COM program when it was loaded.
  14. Module GLBLINIT is responsible for allocating all the buffers
  15. needed by Format. These buffers are listed below.
  16. Name Size Primary Usage
  17. ---------------------------------------------------------------------
  18. DirectorySector 1 sector (512 bytes) Holds a zeroed
  19. sector to be written to
  20. disk as the root dir.
  21. FatSpace FAT+RootDir+ Holds new FAT.
  22. 7 sectors Reserves space for
  23. buffers needed by
  24. Mirror.
  25. FatSector 1 sector Read in old FAT
  26. one sector at a time.
  27. DirBuf 1 sector General-purpose use e.g.
  28. reading in boot sector,
  29. first sector of root dir.
  30. ClustBound_Buffer_Seg 1 cluster Read in bad clusters
  31. on fixed disks.
  32. mStart Biggest block of Read in system files,
  33. if needed.
  34. A buffer for system files will be allocated only if /S is specified.
  35. If this is present, system files will be read in at this time. The system
  36. files are read in now so as to prevent an extra disk swap later, i.e. it is
  37. assumed that the system disk will be in the default drive at this point.
  38. In case the buffer was not big enough for all the system files (IO.SYS,
  39. MSDOS.SYS, COMMAND.COM), they will be read in as much as
  40. possible. The remainder will then be read in when the system files
  41. are being written out to disk, after completion of formatting (This idea
  42. is now prevented: if the files cannot be all read prior to format, format
  43. will fail).
  44. The 1-cluster buffer will be allocated only if the drive being
  45. formatted is a fixed disk.
  46. This module also determines the default value for
  47. DeviceParameters (by a call to routine GetDeviceParameters, which
  48. obtains them through Int 21h function 440Dh, subfunction 60h). These
  49. default parameters are copied to SavedParams.
  50. A failure of any function call in GLBLINIT will result in the
  51. termination of Format.
  52. The portion after the call to Global_Init is the main loop of the
  53. program. The code between 'SysLoop' and 'call More' is repeated for
  54. each disk to be formatted.
  55. The routine InitSysParm restores the state of several variables
  56. so that they are in the original state at the outset for each disk being
  57. formatted. Thus DeviceParameters are restored from SavedParams
  58. and SwitchMap is restored from SwitchCopy.
  59. All allocated buffers are zeroed out in the routine
  60. ZeroAllBuffers.
  61. After prompting the user for the disk to be formatted, disk
  62. access is ensured through a call to Get_Disk_Access.
  63. Now module PHASE1 is invoked. This module primarily
  64. determines the final value of DeviceParameters to be used in
  65. formatting the disk. It also handles all the logical requirements to
  66. validate a safe format.
  67. In Phase1Initialisation firstly a media sensing call is made, for
  68. removable media. This will utilize new media sensing capabilities of
  69. the hardware to determine what type of disk is being used. If this
  70. function is supported, the BPB field in DeviceParameters will be set to
  71. that of the media detected. Media sensing can be supported only on
  72. 3.5in disks.
  73. The routine Set_BPB_Info is now called to handle the case
  74. when the disk to be formatted is a non-FAT disk. This routine will set
  75. DeviceParameters appropriately in this case.
  76. Note: This case has not been tested, or ever observed, for that matter.
  77. The CheckSwitches routine takes action based on the size
  78. specification switches (/F /N /T /1 /4 /8) entered by the user. If /F was
  79. specified, it will be translated into the appropriate value for /N & /T.
  80. Thus SwitchMap may be modified. Also, DeviceParameters will be set
  81. appropriately, for all the formats supported.
  82. Note: CheckSwitches does NOT modify DeviceParameters if it detects
  83. that the size specification entered by the user is equal to the default
  84. size for that drive. This property is taken into account in determining
  85. the validity of a safe format in PHASE1.
  86. Upon return from CheckSwitches, a copy of DeviceParameters
  87. is saved in SwitchDevParams.
  88. If the user had specified an unconditional format (/U present),
  89. the format will be done either according to the entered size
  90. specification, or else to the default size for the drive (which is its
  91. maximum capacity).
  92. If /U is not present, the routine DetermineExistingFormat is
  93. called. This routine reads the first 2 sectos and validates the boot
  94. sector (the first sector) on the disk. If the existing format is found
  95. to be valid, then ResetDeviceParameters is called to copy the BPB read
  96. off the boot sector into that of DeviceParameters. Further, for removable
  97. media the field DP_CYLINDERS is calculated and set.
  98. If the disk is not found to have a valid format at this point,
  99. another routine is called to check in case a disk with a CP/M-type boot
  100. sector is present (for 160K and 180K disks). The routine DetermineCPMFormat
  101. validates the first sector of the FAT (the second sector read in
  102. DetermineExistingFormat) by examining the media descriptor byte. If this is
  103. found to be a CP/M media descriptor, DeviceParameters will be modified.
  104. A table of customized BPBs is used for this purpose. <In short, this is
  105. an extensive routine which will almost always have to be called, while
  106. being applicable only in obsolete cases.>
  107. If the disk if found NOT to have a valid format, a check is made
  108. to see if there was an error reading the disk due to an open door, etc.
  109. If so, a message will be given and Format will be terminated.
  110. Otherwise SwitchMap is checked to see if /Q was specified. Since
  111. there is no valid existing format, Quick format cannot be done, so a
  112. warning message will be issued, and the user will be prompted
  113. whether or not to continue with an unconditional format. If the user
  114. chooses to continue, /Q will be turned off, and /U will be turned on.
  115. There is one exception to this logic: the user is allowed to
  116. enter /Q together with a size specification, which means "Quick format
  117. all pre-formatted disks with their original format, but format new disks
  118. with the specified size". Thus, if /Q is present, SwitchMap is checked
  119. for (/F, /N or /T) and if these are present, the warning message is not
  120. given. Instead, the message is jumped over, and the program
  121. continues as if the user had entered "Yes" to the continue prompt.
  122. The code will now continue, with DeviceParameters finalized, at
  123. DevParamsOk.
  124. In case the disk is found to have a valid existing format, then
  125. the values of SwitchDevParams and DeviceParameters are compared
  126. for equality. If found to be equal, execution continues at
  127. DevParamsOk (i.e. there is no conflict between any user-specified
  128. size and the existing format on disk). Otherwise a warning message
  129. will be issued since there is a conflict between the entered size or
  130. default size andthe format on disk, and the user will be asked
  131. whether to proceed with an unconditional format. If the user chooses
  132. to continue, /U will be turned on and SwitchDevParams will be copied
  133. into DeviceParameters, so as to utilize the size specification given by
  134. the user.
  135. The only exception to the last case is when /Q is present
  136. together with a size specification, as explained above. Then a safe
  137. format will be done using the current disk format.
  138. After having finalized DeviceParameters, the track layout in
  139. DeviceParameters is initialized, in the loop LoadSectorTable. Some
  140. other initialisation is performed here also, such as determining the
  141. starting sector, and whether we have a 12 or 16-bit FAT. The total
  142. number of clusters is also computed.
  143. After PHASE1, the MIR_MAIN module will be invoked to create
  144. the recovery file in case /U has not been turned on. In order to
  145. accomodate any buffer space needed by the Mirror utility, it is
  146. necessary to release the space used by the FatSpace buffer. This
  147. buffer is originally made big enough to accomodate all the buffer
  148. requirements of Mirror (this is FAT + Root Dir + Boot Sector + 6 extra
  149. sectors + 1 surplus sector to allow for arena headers). The FatSpace
  150. buffer is re-allocated upon successful return from Mirror. In case the
  151. creation of the recovery file failed, the user will be given the option of
  152. continuing with an unconditional format.
  153. Phase2Initialisation performs some calculations relating to the
  154. format to be performed.
  155. The routine ShowFormatSize is now called to display the size
  156. of the format to be done.
  157. Finally, the actual disk formatting is done in module
  158. DSKFRMT. The FatSpace buffer is initialised and loaded with the
  159. media descriptor byte at the start. The drive parameters are set to
  160. those of the intended format through a call to SetDeviceParameters,
  161. using the DeviceParameters parameter block. For a Quick format, the
  162. routine QuickFormat is called. This pseudo-formats the disk by
  163. copying all bad cluster markers from the old FAT into the new FAT.
  164. The old FAT is traversed sequentially, and is read in 1 sector at a
  165. time, into FatSector.
  166. In the case of a regular format, SwitchMap is first checked to
  167. see if /U is present. If it is, the 'Format and Verify Track' operation will
  168. be performed on each track. Otherwise only 'Verify Track' will be
  169. performed.
  170. The routine Write_Disk_Info writes out the new boot sector,
  171. root directory and FAT, as well as the system files, if requested. Great
  172. care is taken not to over-write any old files if a system transfer is
  173. being done together with a safe format. There are four cases which
  174. must be addressed here. These are as follows.
  175. [1] Unconditional format without system files
  176. [2] Unconditional format with system files
  177. [3] Safe format without system files
  178. [4] Safe format with system files
  179. Cases [1], [2] and [3] are all handled the same way. This is as
  180. follows. The boot sector is written out, followed by a zeroed root
  181. directory, and the new FAT (which is zero except for bad cluster
  182. markers). If needed, the system files can now be written out.
  183. Case [4] is handled in a special way, as described below. The
  184. boot sector and a zeroed root directory is written out. The disk is now
  185. checked to see if there is enough space for the system files. At this
  186. point the old FAT on the disk is still there, so the free space cannot
  187. overlap any allocated clusters.
  188. If there is insufficient space, the user will be given a warning
  189. message and prompted whether to perform a system transfer which
  190. will prevent later recovery, or just not to transfer the system files. If
  191. the user chooses to continue with the transfer, then the old FAT will
  192. first be over-written with the new FAT, and then system files will be
  193. written out. If the user chooses not to continue, the new FAT will be
  194. written out, and system files will not be transferred.
  195. If there is sufficient space, then it is necessary to ensure that
  196. IO.SYS (the first system file) is written out to a location on disk so that
  197. it is contiguous. This is achieved by calculating the number of
  198. clusters needed to accomodate a size of 1.5Kbytes (the maximum
  199. size of IO.SYS). This value will be between 1 and 3. The old FAT will
  200. then be scanned to find a free cluster block of this size, and disk
  201. allocation will be forced to start from there, using an undocumented
  202. DPB function. In case such a cluster block cannot be found (extremely
  203. unlikely), the user will be given a warning that system transfer will
  204. destroy disk files and the program will proceed as described above.
  205. Once the cluster block is found, the system files are now written out to
  206. disk. Since the old FAT is still on disk, this method obviates the
  207. possibility of over-writing old disk files. After transfer is complete, the
  208. FAT chains for the three system files will be copied from the old FAT
  209. into the new FAT. Note that the root directory entries will automatically
  210. appear in the new root directory as the system files are written out.
  211. The new FAT will then be written out to disk, over the old FAT.
  212. The routine More prompts the user whether another disk is to
  213. be formatted. If the user chooses to format another disk, execution
  214. will resume at SysLoop, where the original state of parameters will be
  215. restored.