1
0

partio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. partio.c
  9. Abstract:
  10. This module implements support for doing I/O directly to a partition in the
  11. setup application.
  12. Author:
  13. Evan Green 11-Apr-2014
  14. Environment:
  15. User
  16. --*/
  17. //
  18. // ------------------------------------------------------------------- Includes
  19. //
  20. #include <assert.h>
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <getopt.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <sys/types.h>
  27. #include "setup.h"
  28. //
  29. // ---------------------------------------------------------------- Definitions
  30. //
  31. //
  32. // ------------------------------------------------------ Data Type Definitions
  33. //
  34. //
  35. // ----------------------------------------------- Internal Function Prototypes
  36. //
  37. //
  38. // -------------------------------------------------------------------- Globals
  39. //
  40. //
  41. // ------------------------------------------------------------------ Functions
  42. //
  43. PVOID
  44. SetupPartitionOpen (
  45. PSETUP_CONTEXT Context,
  46. PSETUP_DESTINATION Destination,
  47. PPARTITION_DEVICE_INFORMATION PartitionInformation
  48. )
  49. /*++
  50. Routine Description:
  51. This routine opens a handle to a given partition destination.
  52. Arguments:
  53. Context - Supplies a pointer to the application context.
  54. Destination - Supplies a pointer to the destination to open.
  55. PartitionInformation - Supplies an optional pointer where the partition
  56. information will be returned on success.
  57. Return Value:
  58. Returns a pointer to an opaque context on success.
  59. NULL on failure.
  60. --*/
  61. {
  62. PVOID Handle;
  63. INT Result;
  64. Handle = SetupOpenDestination(Destination, O_RDWR, 0);
  65. if (Handle != NULL) {
  66. SetupPartitionSeek(Context, Handle, 0);
  67. if (PartitionInformation != NULL) {
  68. Result = SetupOsGetPartitionInformation(Destination,
  69. PartitionInformation);
  70. if (Result != 0) {
  71. SetupPartitionClose(Context, Handle);
  72. Handle = NULL;
  73. }
  74. }
  75. }
  76. return Handle;
  77. }
  78. VOID
  79. SetupPartitionClose (
  80. PSETUP_CONTEXT Context,
  81. PVOID Handle
  82. )
  83. /*++
  84. Routine Description:
  85. This routine closes a partition.
  86. Arguments:
  87. Context - Supplies a pointer to the application context.
  88. Handle - Supplies the open handle.
  89. Return Value:
  90. None.
  91. --*/
  92. {
  93. SetupClose(Handle);
  94. return;
  95. }
  96. ssize_t
  97. SetupPartitionRead (
  98. PSETUP_CONTEXT Context,
  99. PVOID Handle,
  100. void *Buffer,
  101. size_t ByteCount
  102. )
  103. /*++
  104. Routine Description:
  105. This routine reads from a partition.
  106. Arguments:
  107. Context - Supplies a pointer to the application context.
  108. Handle - Supplies the handle.
  109. Buffer - Supplies a pointer where the read bytes will be returned.
  110. ByteCount - Supplies the number of bytes to read.
  111. Return Value:
  112. Returns the number of bytes read on success.
  113. --*/
  114. {
  115. return SetupRead(Handle, Buffer, ByteCount);
  116. }
  117. ssize_t
  118. SetupPartitionWrite (
  119. PSETUP_CONTEXT Context,
  120. PVOID Handle,
  121. void *Buffer,
  122. size_t ByteCount
  123. )
  124. /*++
  125. Routine Description:
  126. This routine writes to a partition.
  127. Arguments:
  128. Context - Supplies a pointer to the application context.
  129. Handle - Supplies the handle.
  130. Buffer - Supplies a pointer to the data to write.
  131. ByteCount - Supplies the number of bytes to read.
  132. Return Value:
  133. Returns the number of bytes written.
  134. --*/
  135. {
  136. return SetupWrite(Handle, Buffer, ByteCount);
  137. }
  138. LONGLONG
  139. SetupPartitionSeek (
  140. PSETUP_CONTEXT Context,
  141. PVOID Handle,
  142. LONGLONG Offset
  143. )
  144. /*++
  145. Routine Description:
  146. This routine seeks in the current file or device.
  147. Arguments:
  148. Context - Supplies a pointer to the application context.
  149. Handle - Supplies the handle.
  150. Offset - Supplies the offset in blocks to seek to.
  151. Return Value:
  152. Returns the resulting file offset in blocks after the operation.
  153. -1 on failure, and errno will contain more information. The file offset
  154. will remain unchanged.
  155. --*/
  156. {
  157. LONGLONG NewOffset;
  158. NewOffset = SetupSeek(
  159. Handle,
  160. (Offset + Context->CurrentPartitionOffset) * SETUP_BLOCK_SIZE);
  161. NewOffset -= Context->CurrentPartitionOffset * SETUP_BLOCK_SIZE;
  162. return NewOffset / SETUP_BLOCK_SIZE;
  163. }
  164. //
  165. // --------------------------------------------------------- Internal Functions
  166. //