Source code of Windows XP (NT5)
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.

322 lines
15 KiB

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