partlib.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*++
  2. Copyright (c) 2014 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. partlib.h
  9. Abstract:
  10. This header contains definitions for using the partition support library.
  11. Author:
  12. Evan Green 30-Jan-2014
  13. --*/
  14. //
  15. // ------------------------------------------------------------------- Includes
  16. //
  17. #include <minoca/devinfo/part.h>
  18. //
  19. // ---------------------------------------------------------------- Definitions
  20. //
  21. //
  22. // ------------------------------------------------------ Data Type Definitions
  23. //
  24. /*++
  25. Structure Description:
  26. This structure stores information about a particular partition.
  27. Members:
  28. StartOffset - Stores the starting offset of the partition (the address of
  29. the first block of the partition).
  30. EndOffset - Stores the ending offset of the partition, exclusive (the
  31. first address just beyond the end of the partition).
  32. Attributes - Stores a bitfield of attributes about the partition.
  33. Number - Stores a partition number, starting at 1.
  34. ParentNumber - Stores the partition number of the extended partition this
  35. logical partition is in. This parameter is unused for anything other
  36. than logical partitions.
  37. Flags - Stores a bitfield of flags about the partition. See
  38. PARTITION_FLAG_* definitions.
  39. PartitionType - Stores a recognized partition type.
  40. TypeIdentifier - Stores the partition type identifier. For MBR disks this
  41. is just the system ID byte, for GPT disks this is the partition type
  42. GUID.
  43. Identifier - Stores the partition identifier. For MBR disks this contains
  44. the partition number and disk signature (a cobbled together identifier).
  45. For GPT disks this contains the unique partition GUID.
  46. --*/
  47. typedef struct _PARTITION_INFORMATION {
  48. ULONGLONG StartOffset;
  49. ULONGLONG EndOffset;
  50. ULONGLONG Attributes;
  51. ULONG Number;
  52. ULONG ParentNumber;
  53. ULONG Flags;
  54. PARTITION_TYPE PartitionType;
  55. UCHAR TypeIdentifier[PARTITION_TYPE_SIZE];
  56. UCHAR Identifier[PARTITION_IDENTIFIER_SIZE];
  57. } PARTITION_INFORMATION, *PPARTITION_INFORMATION;
  58. typedef struct _PARTITION_CONTEXT PARTITION_CONTEXT, *PPARTITION_CONTEXT;
  59. //
  60. // Define functions called by the partition library.
  61. //
  62. typedef
  63. PVOID
  64. (*PPARTITION_ALLOCATE) (
  65. UINTN Size
  66. );
  67. /*++
  68. Routine Description:
  69. This routine is called when the partition library needs to allocate memory.
  70. Arguments:
  71. Size - Supplies the size of the allocation request, in bytes.
  72. Return Value:
  73. Returns a pointer to the allocation if successful, or NULL if the
  74. allocation failed.
  75. --*/
  76. typedef
  77. VOID
  78. (*PPARTITION_FREE) (
  79. PVOID Memory
  80. );
  81. /*++
  82. Routine Description:
  83. This routine is called when the partition library needs to free allocated
  84. memory.
  85. Arguments:
  86. Memory - Supplies the allocation returned by the allocation routine.
  87. Return Value:
  88. None.
  89. --*/
  90. typedef
  91. KSTATUS
  92. (*PPARTITION_READ) (
  93. PPARTITION_CONTEXT Context,
  94. ULONGLONG BlockAddress,
  95. PVOID Buffer
  96. );
  97. /*++
  98. Routine Description:
  99. This routine is called when the partition library needs to read a sector
  100. from the disk.
  101. Arguments:
  102. Context - Supplies the partition context identifying the disk.
  103. BlockAddress - Supplies the block address to read.
  104. Buffer - Supplies a pointer where the data will be returned on success.
  105. This buffer is expected to be one block in size (as specified in the
  106. partition context).
  107. Return Value:
  108. Status code.
  109. --*/
  110. typedef
  111. KSTATUS
  112. (*PPARTITION_WRITE) (
  113. PPARTITION_CONTEXT Context,
  114. ULONGLONG BlockAddress,
  115. PVOID Buffer
  116. );
  117. /*++
  118. Routine Description:
  119. This routine is called when the partition library needs to write a sector
  120. to the disk.
  121. Arguments:
  122. Context - Supplies the partition context identifying the disk.
  123. BlockAddress - Supplies the block address to read.
  124. Buffer - Supplies a pointer where the data will be returned on success.
  125. This buffer is expected to be one block in size (as specified in the
  126. partition context).
  127. Return Value:
  128. Status code.
  129. --*/
  130. typedef
  131. VOID
  132. (*PPARTITION_FILL_RANDOM) (
  133. PPARTITION_CONTEXT Context,
  134. PUCHAR Buffer,
  135. ULONG BufferSize
  136. );
  137. /*++
  138. Routine Description:
  139. This routine is called when the partition library needs to fill a buffer
  140. with random bytes.
  141. Arguments:
  142. Context - Supplies the partition context identifying the disk.
  143. Buffer - Supplies a pointer to the buffer to fill with random bytes.
  144. BufferSize - Supplies the size of the buffer in bytes.
  145. Return Value:
  146. None.
  147. --*/
  148. /*++
  149. Structure Description:
  150. This structure stores information about a partition manager context to a
  151. disk.
  152. Members:
  153. AllocateFunction - Stores a pointer to a function the partition library
  154. uses to allocate memory.
  155. FreeFunction - Stores a pointer to a function the partition library uses to
  156. free previously allocated memory.
  157. ReadFunction - Stores a pointer to a function the partition library uses to
  158. read a block from the disk.
  159. WriteFunction - Stores a pointer to a function the partition library uses
  160. to write a block to the disk. This is optional if editing partitions
  161. is not needed.
  162. FillRandomFunction - Stores a pointer to a function the partition library
  163. uses to fill a buffer with random data.
  164. BlockSize - Stores the block size of the disk.
  165. BlockShift - Stores the power of two of the block size.
  166. Alignment - Stores the required alignment of buffers that do I/O. Zero or
  167. one specifies no alignment requirement.
  168. BlockCount - Stores the number of blocks in the disk (one beyond the last
  169. valid LBA).
  170. SectorsPerHead - Stores the maximum valid sector number in legacy CHS
  171. geometry. This value probably shouldn't be greater than 63.
  172. HeadsPerCylinder - Stores the number of heads per cylinder for legacy CHS
  173. geometry.
  174. Format - Stores the partition format of this disk.
  175. DiskIdentifier - Stores the disk identifier information.
  176. PartitionCount - Stores the number of partitions on this disk.
  177. Partitions - Stores a pointer to the array of partition information
  178. structures describing this disk.
  179. --*/
  180. struct _PARTITION_CONTEXT {
  181. PPARTITION_ALLOCATE AllocateFunction;
  182. PPARTITION_FREE FreeFunction;
  183. PPARTITION_READ ReadFunction;
  184. PPARTITION_WRITE WriteFunction;
  185. PPARTITION_FILL_RANDOM FillRandomFunction;
  186. ULONG BlockSize;
  187. ULONG BlockShift;
  188. ULONG Alignment;
  189. ULONGLONG BlockCount;
  190. ULONG SectorsPerHead;
  191. ULONG HeadsPerCylinder;
  192. PARTITION_FORMAT Format;
  193. UCHAR DiskIdentifier[DISK_IDENTIFIER_SIZE];
  194. ULONG PartitionCount;
  195. PPARTITION_INFORMATION Partitions;
  196. };
  197. //
  198. // -------------------------------------------------------------------- Globals
  199. //
  200. //
  201. // -------------------------------------------------------- Function Prototypes
  202. //
  203. KSTATUS
  204. PartInitialize (
  205. PPARTITION_CONTEXT Context
  206. );
  207. /*++
  208. Routine Description:
  209. This routine initializes a partition context. The caller is expected to
  210. have filled in pointers to the allocate, free, and read sector functions.
  211. The caller is also expected to have filled in the block size and disk
  212. geometry information (if needed).
  213. Arguments:
  214. Context - Supplies a pointer to the context, partially initialized by the
  215. caller.
  216. Return Value:
  217. STATUS_SUCCESS on success.
  218. STATUS_INVALID_PARAMETER if the context was not initialized properly by
  219. the caller.
  220. --*/
  221. VOID
  222. PartDestroy (
  223. PPARTITION_CONTEXT Context
  224. );
  225. /*++
  226. Routine Description:
  227. This routine destroys a partition context.
  228. Arguments:
  229. Context - Supplies a pointer to the context.
  230. Return Value:
  231. None.
  232. --*/
  233. KSTATUS
  234. PartEnumeratePartitions (
  235. PPARTITION_CONTEXT Context
  236. );
  237. /*++
  238. Routine Description:
  239. This routine is called to read the partition information from the disk
  240. and enumerate the list of partitions. The caller must have just called
  241. the initialize context function.
  242. Arguments:
  243. Context - Supplies a pointer to the initialized context.
  244. Return Value:
  245. STATUS_SUCCESS if the partition information could be determined. There
  246. could still be zero partitions in this case.
  247. STATUS_NO_ELIGIBLE_DEVICES if the partition table is invalid.
  248. Error codes on device read or allocation failure.
  249. --*/
  250. KSTATUS
  251. PartWritePartitionLayout (
  252. PPARTITION_CONTEXT Context,
  253. PARTITION_FORMAT Format,
  254. PPARTITION_INFORMATION Partitions,
  255. ULONG PartitionCount,
  256. BOOL CleanMbr
  257. );
  258. /*++
  259. Routine Description:
  260. This routine writes a partition layout to the disk. This usually wipes out
  261. all data on the disk.
  262. Arguments:
  263. Context - Supplies a pointer to the partition context.
  264. Format - Supplies the partition format to use.
  265. Partitions - Supplies a pointer to the new partition layout.
  266. PartitionCount - Supplies the number of partitions in the new layout.
  267. CleanMbr - Supplies a boolean indicating if only the partition entries of
  268. the MBR should be modified (FALSE) or if the whole MBR should be
  269. zeroed before being written (TRUE).
  270. Return Value:
  271. STATUS_SUCCESS if the valid block count is non-zero.
  272. STATUS_OUT_OF_BOUNDS if the block address is beyond the end of the
  273. partition.
  274. --*/
  275. KSTATUS
  276. PartTranslateIo (
  277. PPARTITION_INFORMATION Partition,
  278. PULONGLONG BlockAddress,
  279. PULONGLONG BlockCount
  280. );
  281. /*++
  282. Routine Description:
  283. This routine performs a translation from a partition-relative offset to a
  284. global disk offset.
  285. Arguments:
  286. Partition - Supplies a pointer to the partition to translate for.
  287. BlockAddress - Supplies a pointer that on input contains the
  288. partition-relative block address. On successful output, this will
  289. contain the globla address.
  290. BlockCount - Supplies a pointer that on input contains the number of blocks
  291. to read or write. On output, the number of valid blocks will be
  292. returned. This number may be reduced on output if the caller tried to
  293. do I/O off the end of the partition.
  294. Return Value:
  295. STATUS_SUCCESS if the valid block count is non-zero.
  296. STATUS_OUT_OF_BOUNDS if the block address is beyond the end of the
  297. partition.
  298. --*/
  299. PARTITION_TYPE
  300. PartConvertToPartitionType (
  301. PARTITION_FORMAT Format,
  302. UCHAR PartitionTypeId[PARTITION_TYPE_SIZE]
  303. );
  304. /*++
  305. Routine Description:
  306. This routine converts a partition type ID into a known partition type.
  307. Arguments:
  308. Format - Supplies the format. Valid values are MBR and GPT.
  309. PartitionTypeId - Supplies the partition type ID bytes.
  310. Return Value:
  311. Returns the partition type that corresponds with the given partition
  312. type ID.
  313. PartitionTypeInvalid if the format is invalid.
  314. PartitionTypeUnknown if the partition type ID is unknown.
  315. --*/