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.

326 lines
16 KiB

  1. /*++
  2. Copyright (c) 1993 Microsoft Corporation
  3. Module Name:
  4. bpb.hxx
  5. Abstract:
  6. This module contains the declarations for packed and
  7. unpacked Bios Parameter Block
  8. Author:
  9. Bill McJohn [BillMc] 24-September-1993
  10. Revision History:
  11. --*/
  12. #if !defined( _IFSUTIL_BPB_DEFN_ )
  13. #define _IFSUTIL_BPB_DEFN_
  14. #define cOEM 8
  15. #define cLABEL 11
  16. #define cSYSID 8
  17. typedef struct _PACKED_BIOS_PARAMETER_BLOCK {
  18. UCHAR BytesPerSector[2]; // offset = 0x000
  19. UCHAR SectorsPerCluster[1]; // offset = 0x002
  20. UCHAR ReservedSectors[2]; // offset = 0x003
  21. UCHAR Fats[1]; // offset = 0x005
  22. UCHAR RootEntries[2]; // offset = 0x006
  23. UCHAR Sectors[2]; // offset = 0x008
  24. UCHAR Media[1]; // offset = 0x00A
  25. UCHAR SectorsPerFat[2]; // offset = 0x00B
  26. UCHAR SectorsPerTrack[2]; // offset = 0x00D
  27. UCHAR Heads[2]; // offset = 0x00F
  28. UCHAR HiddenSectors[4]; // offset = 0x011
  29. UCHAR LargeSectors[4]; // offset = 0x015
  30. UCHAR BigSectorsPerFat[4]; // offset = 0x019 25
  31. UCHAR ExtFlags[2]; // offset = 0x01D 29
  32. UCHAR FS_Version[2]; // offset = 0x01F 31
  33. UCHAR RootDirStrtClus[4]; // offset = 0x021 33
  34. UCHAR FSInfoSec[2]; // offset = 0x025 37
  35. UCHAR BkUpBootSec[2]; // offset = 0x027 39
  36. UCHAR Reserved[12]; // offset = 0x029 41
  37. } PACKED_BIOS_PARAMETER_BLOCK; // sizeof = 0x035 53
  38. typedef PACKED_BIOS_PARAMETER_BLOCK *PPACKED_BIOS_PARAMETER_BLOCK;
  39. // DEFINE THIS UNCHANGED PBPB for use on NTFS drive PBootSector
  40. typedef struct _OLD_PACKED_BIOS_PARAMETER_BLOCK {
  41. UCHAR BytesPerSector[2]; // offset = 0x000
  42. UCHAR SectorsPerCluster[1]; // offset = 0x002
  43. UCHAR ReservedSectors[2]; // offset = 0x003
  44. UCHAR Fats[1]; // offset = 0x005
  45. UCHAR RootEntries[2]; // offset = 0x006
  46. UCHAR Sectors[2]; // offset = 0x008
  47. UCHAR Media[1]; // offset = 0x00A
  48. UCHAR SectorsPerFat[2]; // offset = 0x00B
  49. UCHAR SectorsPerTrack[2]; // offset = 0x00D
  50. UCHAR Heads[2]; // offset = 0x00F
  51. UCHAR HiddenSectors[4]; // offset = 0x011
  52. UCHAR LargeSectors[4]; // offset = 0x015
  53. } OLD_PACKED_BIOS_PARAMETER_BLOCK; // sizeof = 0x019
  54. typedef struct BIOS_PARAMETER_BLOCK {
  55. USHORT BytesPerSector;
  56. UCHAR SectorsPerCluster;
  57. USHORT ReservedSectors;
  58. UCHAR Fats;
  59. USHORT RootEntries;
  60. USHORT Sectors;
  61. UCHAR Media;
  62. USHORT SectorsPerFat;
  63. USHORT SectorsPerTrack;
  64. USHORT Heads;
  65. ULONG HiddenSectors;
  66. ULONG LargeSectors;
  67. ULONG BigSectorsPerFat;
  68. USHORT ExtFlags;
  69. USHORT FS_Version;
  70. ULONG RootDirStrtClus;
  71. USHORT FSInfoSec;
  72. USHORT BkUpBootSec;
  73. } BIOS_PARAMETER_BLOCK;
  74. typedef BIOS_PARAMETER_BLOCK *PBIOS_PARAMETER_BLOCK;
  75. #if !defined( _UCHAR_DEFINED_ )
  76. #define _UCHAR_DEFINED_
  77. //
  78. // The following types and macros are used to help unpack the packed and
  79. // misaligned fields found in the Bios parameter block
  80. //
  81. typedef union _UCHAR1 {
  82. UCHAR Uchar[1];
  83. UCHAR ForceAlignment;
  84. } UCHAR1, *PUCHAR1;
  85. typedef union _UCHAR2 {
  86. UCHAR Uchar[2];
  87. USHORT ForceAlignment;
  88. } UCHAR2, *PUCHAR2;
  89. typedef union _UCHAR4 {
  90. UCHAR Uchar[4];
  91. ULONG ForceAlignment;
  92. } UCHAR4, *PUCHAR4;
  93. #define CopyUchar1(Dst,Src) { \
  94. *((UCHAR1 *)(Dst)) = *((UNALIGNED UCHAR1 *)(Src)); \
  95. }
  96. #define CopyUchar2(Dst,Src) { \
  97. *((UCHAR2 *)(Dst)) = *((UNALIGNED UCHAR2 *)(Src)); \
  98. }
  99. #define CopyU2char(Dst,Src) { \
  100. *((UNALIGNED UCHAR2 *)(Dst)) = *((UCHAR2 *)(Src)); \
  101. }
  102. #define CopyUchar4(Dst,Src) { \
  103. *((UCHAR4 *)(Dst)) = *((UNALIGNED UCHAR4 *)((ULONG_PTR)(Src))); \
  104. }
  105. #define CopyU4char(Dst, Src) { \
  106. *((UNALIGNED UCHAR4 *)(Dst)) = *((UCHAR4 *)(Src)); \
  107. }
  108. #endif // _UCHAR_DEFINED_
  109. //
  110. // This macro takes a Packed BPB and fills in its Unpacked equivalent
  111. //
  112. #define UnpackBiosExt32(Bios,Pbios) { \
  113. CopyUchar2(&((Bios)->BytesPerSector), (Pbios)->BytesPerSector ); \
  114. CopyUchar1(&((Bios)->SectorsPerCluster), (Pbios)->SectorsPerCluster); \
  115. CopyUchar2(&((Bios)->ReservedSectors), (Pbios)->ReservedSectors ); \
  116. CopyUchar1(&((Bios)->Fats), (Pbios)->Fats ); \
  117. CopyUchar2(&((Bios)->RootEntries), (Pbios)->RootEntries ); \
  118. CopyUchar2(&((Bios)->Sectors), (Pbios)->Sectors ); \
  119. CopyUchar1(&((Bios)->Media), (Pbios)->Media ); \
  120. CopyUchar2(&((Bios)->SectorsPerFat), (Pbios)->SectorsPerFat ); \
  121. CopyUchar2(&((Bios)->SectorsPerTrack), (Pbios)->SectorsPerTrack ); \
  122. CopyUchar2(&((Bios)->Heads), (Pbios)->Heads ); \
  123. CopyUchar4(&((Bios)->HiddenSectors), (Pbios)->HiddenSectors ); \
  124. CopyUchar4(&((Bios)->LargeSectors), (Pbios)->LargeSectors ); \
  125. CopyUchar4(&((Bios)->BigSectorsPerFat), (Pbios)->BigSectorsPerFat ); \
  126. CopyUchar2(&((Bios)->ExtFlags), (Pbios)->ExtFlags ); \
  127. CopyUchar2(&((Bios)->FS_Version), (Pbios)->FS_Version ); \
  128. CopyUchar4(&((Bios)->RootDirStrtClus), (Pbios)->RootDirStrtClus ); \
  129. CopyUchar2(&((Bios)->FSInfoSec), (Pbios)->FSInfoSec ); \
  130. CopyUchar2(&((Bios)->BkUpBootSec), (Pbios)->BkUpBootSec ); \
  131. }
  132. #define UnpackBios(Bios,Pbios) { \
  133. CopyUchar2(&((Bios)->BytesPerSector), (Pbios)->BytesPerSector ); \
  134. CopyUchar1(&((Bios)->SectorsPerCluster), (Pbios)->SectorsPerCluster); \
  135. CopyUchar2(&((Bios)->ReservedSectors), (Pbios)->ReservedSectors ); \
  136. CopyUchar1(&((Bios)->Fats), (Pbios)->Fats ); \
  137. CopyUchar2(&((Bios)->RootEntries), (Pbios)->RootEntries ); \
  138. CopyUchar2(&((Bios)->Sectors), (Pbios)->Sectors ); \
  139. CopyUchar1(&((Bios)->Media), (Pbios)->Media ); \
  140. CopyUchar2(&((Bios)->SectorsPerFat), (Pbios)->SectorsPerFat ); \
  141. CopyUchar2(&((Bios)->SectorsPerTrack), (Pbios)->SectorsPerTrack ); \
  142. CopyUchar2(&((Bios)->Heads), (Pbios)->Heads ); \
  143. CopyUchar4(&((Bios)->HiddenSectors), (Pbios)->HiddenSectors ); \
  144. CopyUchar4(&((Bios)->LargeSectors), (Pbios)->LargeSectors ); \
  145. }
  146. //
  147. // This macro takes an Unpacked BPB and fills in its Packed equivalent
  148. //
  149. #define PackBios32(Bios,Pbios) { \
  150. CopyU2char((Pbios)->BytesPerSector, &((Bios)->BytesPerSector) ); \
  151. CopyUchar1((Pbios)->SectorsPerCluster, &((Bios)->SectorsPerCluster)); \
  152. CopyU2char((Pbios)->ReservedSectors, &((Bios)->ReservedSectors) ); \
  153. CopyUchar1((Pbios)->Fats, &((Bios)->Fats) ); \
  154. CopyU2char((Pbios)->RootEntries, &((Bios)->RootEntries) ); \
  155. CopyU2char((Pbios)->Sectors, &((Bios)->Sectors) ); \
  156. CopyUchar1((Pbios)->Media, &((Bios)->Media) ); \
  157. CopyU2char((Pbios)->SectorsPerFat, &((Bios)->SectorsPerFat) ); \
  158. CopyU2char((Pbios)->SectorsPerTrack, &((Bios)->SectorsPerTrack) ); \
  159. CopyU2char((Pbios)->Heads, &((Bios)->Heads) ); \
  160. CopyU4char((Pbios)->HiddenSectors, &((Bios)->HiddenSectors) ); \
  161. CopyU4char((Pbios)->LargeSectors, &((Bios)->LargeSectors) ); \
  162. CopyU4char((Pbios)->BigSectorsPerFat, &((Bios)->BigSectorsPerFat) ); \
  163. CopyU2char((Pbios)->ExtFlags, &((Bios)->ExtFlags) ); \
  164. CopyU2char((Pbios)->FS_Version, &((Bios)->FS_Version) ); \
  165. CopyU4char((Pbios)->RootDirStrtClus, &((Bios)->RootDirStrtClus) ); \
  166. CopyU2char((Pbios)->FSInfoSec, &((Bios)->FSInfoSec) ); \
  167. CopyU2char((Pbios)->BkUpBootSec, &((Bios)->BkUpBootSec) ); \
  168. }
  169. #define PackBios(Bios,Pbios) { \
  170. CopyU2char((Pbios)->BytesPerSector, &((Bios)->BytesPerSector) ); \
  171. CopyUchar1((Pbios)->SectorsPerCluster, &((Bios)->SectorsPerCluster)); \
  172. CopyU2char((Pbios)->ReservedSectors, &((Bios)->ReservedSectors) ); \
  173. CopyUchar1((Pbios)->Fats, &((Bios)->Fats) ); \
  174. CopyU2char((Pbios)->RootEntries, &((Bios)->RootEntries) ); \
  175. CopyU2char((Pbios)->Sectors, &((Bios)->Sectors) ); \
  176. CopyUchar1((Pbios)->Media, &((Bios)->Media) ); \
  177. CopyU2char((Pbios)->SectorsPerFat, &((Bios)->SectorsPerFat) ); \
  178. CopyU2char((Pbios)->SectorsPerTrack, &((Bios)->SectorsPerTrack) ); \
  179. CopyU2char((Pbios)->Heads, &((Bios)->Heads) ); \
  180. CopyU4char((Pbios)->HiddenSectors, &((Bios)->HiddenSectors) ); \
  181. CopyU4char((Pbios)->LargeSectors, &((Bios)->LargeSectors) ); \
  182. }
  183. //
  184. // And now, an extended BPB:
  185. //
  186. typedef struct _PACKED_EXTENDED_BIOS_PARAMETER_BLOCK {
  187. UCHAR IntelNearJumpCommand[1];
  188. UCHAR BootStrapJumpOffset[2];
  189. UCHAR OemData[cOEM];
  190. PACKED_BIOS_PARAMETER_BLOCK Bpb;
  191. UCHAR PhysicalDrive[1]; // 0 = removable, 80h = fixed
  192. UCHAR CurrentHead[1]; // used for dirty partition info
  193. UCHAR Signature[1]; // boot signature
  194. UCHAR SerialNumber[4]; // volume serial number
  195. UCHAR Label[cLABEL]; // volume label, padded with spaces
  196. UCHAR SystemIdText[cSYSID]; // system ID, (e.g. FAT or HPFS)
  197. UCHAR StartBootCode; // first byte of boot code
  198. } PACKED_EXTENDED_BIOS_PARAMETER_BLOCK, *PPACKED_EXTENDED_BIOS_PARAMETER_BLOCK;
  199. typedef struct _OLD_PACKED_EXTENDED_BIOS_PARAMETER_BLOCK {
  200. UCHAR IntelNearJumpCommand[1];
  201. UCHAR BootStrapJumpOffset[2];
  202. UCHAR OemData[cOEM];
  203. OLD_PACKED_BIOS_PARAMETER_BLOCK Bpb;
  204. UCHAR PhysicalDrive[1]; // 0 = removable, 80h = fixed
  205. UCHAR CurrentHead[1]; // used for dirty partition info
  206. UCHAR Signature[1]; // boot signature
  207. UCHAR SerialNumber[4]; // volume serial number
  208. UCHAR Label[cLABEL]; // volume label, padded with spaces
  209. UCHAR SystemIdText[cSYSID]; // system ID, (e.g. FAT or HPFS)
  210. UCHAR StartBootCode; // first byte of boot code
  211. } OLD_PACKED_EXTENDED_BIOS_PARAMETER_BLOCK, *POLD_PACKED_EXTENDED_BIOS_PARAMETER_BLOCK;
  212. typedef struct _EXTENDED_BIOS_PARAMETER_BLOCK {
  213. UCHAR IntelNearJumpCommand;
  214. USHORT BootStrapJumpOffset;
  215. UCHAR OemData[cOEM];
  216. BIOS_PARAMETER_BLOCK Bpb;
  217. UCHAR PhysicalDrive;
  218. UCHAR CurrentHead;
  219. UCHAR Signature;
  220. ULONG SerialNumber;
  221. UCHAR Label[11];
  222. UCHAR SystemIdText[8];
  223. } EXTENDED_BIOS_PARAMETER_BLOCK, *PEXTENDED_BIOS_PARAMETER_BLOCK;
  224. //
  225. // This macro unpacks a Packed Extended BPB.
  226. //
  227. // The UnPACK with SectorsPerFat gets FAT 16 boot sector elements properly loaded for ChkDsk
  228. #define UnpackExtendedBios( Bios, Pbios ) { \
  229. CopyUchar1( &((Bios)->IntelNearJumpCommand), (Pbios)->IntelNearJumpCommand ); \
  230. CopyUchar2( &((Bios)->BootStrapJumpOffset), (Pbios)->BootStrapJumpOffset ); \
  231. memcpy( (Bios)->OemData, (Pbios)->OemData, cOEM ); \
  232. UnpackBiosExt32( &((Bios)->Bpb), &((Pbios)->Bpb)); \
  233. if ((Bios)->Bpb.SectorsPerFat) { \
  234. CopyUchar1( &((Bios)->PhysicalDrive), ((POLD_PACKED_EXTENDED_BIOS_PARAMETER_BLOCK)(Pbios))->PhysicalDrive ); \
  235. CopyUchar1( &((Bios)->CurrentHead), ((POLD_PACKED_EXTENDED_BIOS_PARAMETER_BLOCK)(Pbios))->CurrentHead ); \
  236. CopyUchar1( &((Bios)->Signature), ((POLD_PACKED_EXTENDED_BIOS_PARAMETER_BLOCK)(Pbios))->Signature ); \
  237. CopyUchar4( &((Bios)->SerialNumber), ((POLD_PACKED_EXTENDED_BIOS_PARAMETER_BLOCK)(Pbios))->SerialNumber ); \
  238. memcpy( (Bios)->Label, ((POLD_PACKED_EXTENDED_BIOS_PARAMETER_BLOCK)(Pbios))->Label, cLABEL ); \
  239. memcpy( (Bios)->SystemIdText, ((POLD_PACKED_EXTENDED_BIOS_PARAMETER_BLOCK)(Pbios))->SystemIdText, cSYSID ); \
  240. } else { \
  241. CopyUchar1( &((Bios)->PhysicalDrive), (Pbios)->PhysicalDrive ); \
  242. CopyUchar1( &((Bios)->CurrentHead), (Pbios)->CurrentHead ); \
  243. CopyUchar1( &((Bios)->Signature), (Pbios)->Signature ); \
  244. CopyUchar4( &((Bios)->SerialNumber), (Pbios)->SerialNumber ); \
  245. memcpy( (Bios)->Label, (Pbios)->Label, cLABEL ); \
  246. memcpy( (Bios)->SystemIdText, (Pbios)->SystemIdText, cSYSID ); \
  247. } \
  248. }
  249. //
  250. // This macro packs a Packed Extended BPB.
  251. //
  252. // The PACK with SectorsPerFat keeps FAT 16 boot sector unchanged on FORMAT
  253. #define PackExtendedBios( Bios, Pbios ) { \
  254. if ((Bios)->Bpb.SectorsPerFat) { \
  255. PackBios( &((Bios)->Bpb), &((Pbios)->Bpb)); \
  256. CopyUchar1( ((POLD_PACKED_EXTENDED_BIOS_PARAMETER_BLOCK)(Pbios))->PhysicalDrive, &((Bios)->PhysicalDrive )); \
  257. CopyUchar1( ((POLD_PACKED_EXTENDED_BIOS_PARAMETER_BLOCK)(Pbios))->CurrentHead, &((Bios)->CurrentHead )); \
  258. CopyUchar1( ((POLD_PACKED_EXTENDED_BIOS_PARAMETER_BLOCK)(Pbios))->Signature, &((Bios)->Signature)); \
  259. CopyU4char( ((POLD_PACKED_EXTENDED_BIOS_PARAMETER_BLOCK)(Pbios))->SerialNumber, &((Bios)->SerialNumber )); \
  260. memcpy( ((POLD_PACKED_EXTENDED_BIOS_PARAMETER_BLOCK)(Pbios))->Label, (Bios)->Label, cLABEL ); \
  261. memcpy( ((POLD_PACKED_EXTENDED_BIOS_PARAMETER_BLOCK)(Pbios))->SystemIdText, (Bios)->SystemIdText, cSYSID ); \
  262. } else { \
  263. PackBios32( &((Bios)->Bpb), &((Pbios)->Bpb)); \
  264. CopyUchar1( (Pbios)->PhysicalDrive, &((Bios)->PhysicalDrive )); \
  265. CopyUchar1( (Pbios)->CurrentHead, &((Bios)->CurrentHead )); \
  266. CopyUchar1( (Pbios)->Signature, &((Bios)->Signature)); \
  267. CopyU4char( (Pbios)->SerialNumber, &((Bios)->SerialNumber )); \
  268. memcpy( (Pbios)->Label, (Bios)->Label, cLABEL ); \
  269. memcpy( (Pbios)->SystemIdText, (Bios)->SystemIdText, cSYSID ); \
  270. } \
  271. CopyUchar1( (Pbios)->IntelNearJumpCommand, &((Bios)->IntelNearJumpCommand) ); \
  272. CopyU2char( (Pbios)->BootStrapJumpOffset, &((Bios)->BootStrapJumpOffset) ); \
  273. memcpy( (Pbios)->OemData, (Bios)->OemData, cOEM ); \
  274. }
  275. #endif