partio.c 4.3 KB

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