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.

343 lines
7.8 KiB

  1. /*++
  2. Copyright (c) 1991-1998 Microsoft Corporation
  3. Module Name:
  4. sdcard.c
  5. Abstract:
  6. Author:
  7. Neil Sandlin (neilsa) 1-Jan-01
  8. Environment:
  9. Kernel mode only.
  10. --*/
  11. #include "pch.h"
  12. #include "ntddsd.h"
  13. NTSTATUS
  14. SdCardRead(
  15. IN PSFFDISK_EXTENSION sffdiskExtension,
  16. IN PIRP Irp
  17. );
  18. NTSTATUS
  19. SdCardWrite(
  20. IN PSFFDISK_EXTENSION sffdiskExtension,
  21. IN PIRP Irp
  22. );
  23. NTSTATUS
  24. SdCardInitialize(
  25. IN PSFFDISK_EXTENSION sffdiskExtension
  26. );
  27. NTSTATUS
  28. SdCardDeleteDevice(
  29. IN PSFFDISK_EXTENSION sffdiskExtension
  30. );
  31. NTSTATUS
  32. SdCardGetDiskParameters(
  33. IN PSFFDISK_EXTENSION sffdiskExtension
  34. );
  35. BOOLEAN
  36. SdCardIsWriteProtected(
  37. IN PSFFDISK_EXTENSION sffdiskExtension
  38. );
  39. #ifdef ALLOC_PRAGMA
  40. #pragma alloc_text(PAGE,SdCardRead)
  41. #pragma alloc_text(PAGE,SdCardWrite)
  42. #pragma alloc_text(PAGE,SdCardInitialize)
  43. #pragma alloc_text(PAGE,SdCardDeleteDevice)
  44. #pragma alloc_text(PAGE,SdCardGetDiskParameters)
  45. #pragma alloc_text(PAGE,SdCardIsWriteProtected)
  46. #endif
  47. #pragma pack(1)
  48. typedef struct _SD_MASTER_BOOT_RECORD {
  49. UCHAR Ignore1[446];
  50. UCHAR BootIndicator;
  51. UCHAR StartingHead;
  52. USHORT StartingSectorCyl;
  53. UCHAR SystemId;
  54. UCHAR EndingHead;
  55. USHORT EndingSectorCyl;
  56. ULONG RelativeSector;
  57. ULONG TotalSector;
  58. UCHAR Ignore2[16*3];
  59. USHORT SignatureWord;
  60. } SD_MASTER_BOOT_RECORD, *PSD_MASTER_BOOT_RECORD;
  61. #pragma pack()
  62. SFFDISK_FUNCTION_BLOCK SdCardSupportFns = {
  63. SdCardInitialize,
  64. SdCardDeleteDevice,
  65. SdCardGetDiskParameters,
  66. SdCardIsWriteProtected,
  67. SdCardRead,
  68. SdCardWrite
  69. };
  70. NTSTATUS
  71. SdCardInitialize(
  72. IN PSFFDISK_EXTENSION sffdiskExtension
  73. )
  74. /*++
  75. Routine Description:
  76. Arguments:
  77. Return Value:
  78. --*/
  79. {
  80. NTSTATUS status;
  81. SDBUS_INTERFACE_DATA interfaceData;
  82. RtlZeroMemory(&interfaceData, sizeof(SDBUS_INTERFACE_DATA));
  83. interfaceData.Size = sizeof(SDBUS_INTERFACE_DATA);
  84. interfaceData.Version = SDBUS_INTERFACE_VERSION;
  85. interfaceData.TargetObject = sffdiskExtension->TargetObject;
  86. status = SdBusOpenInterface(&interfaceData, &sffdiskExtension->SdbusInterfaceContext);
  87. if (!NT_SUCCESS(status)) {
  88. return status;
  89. }
  90. try{
  91. SD_MASTER_BOOT_RECORD partitionTable;
  92. ULONG lengthRead;
  93. status = SdBusReadMemory(sffdiskExtension->SdbusInterfaceContext,
  94. 0,
  95. &partitionTable,
  96. 512,
  97. &lengthRead);
  98. if (!NT_SUCCESS(status)) {
  99. leave;
  100. }
  101. if (partitionTable.SignatureWord != 0xAA55) {
  102. SffDiskDump( SFFDISKSHOW, ("Invalid partition table signature - %.4x\n",
  103. partitionTable.SignatureWord));
  104. status = STATUS_UNSUCCESSFUL;
  105. leave;
  106. }
  107. SffDiskDump( SFFDISKSHOW, ("SFFDISK: SD device relative=%.8x total=%.8x\n",
  108. partitionTable.RelativeSector, partitionTable.TotalSector));
  109. sffdiskExtension->RelativeOffset = partitionTable.RelativeSector * 512;
  110. sffdiskExtension->SystemId = partitionTable.SystemId;
  111. } finally {
  112. if (!NT_SUCCESS(status)) {
  113. SdBusCloseInterface(sffdiskExtension->SdbusInterfaceContext);
  114. }
  115. }
  116. return status;
  117. }
  118. NTSTATUS
  119. SdCardDeleteDevice(
  120. IN PSFFDISK_EXTENSION sffdiskExtension
  121. )
  122. {
  123. NTSTATUS status;
  124. status = SdBusCloseInterface(sffdiskExtension->SdbusInterfaceContext);
  125. return status;
  126. }
  127. BOOLEAN
  128. SdCardIsWriteProtected(
  129. IN PSFFDISK_EXTENSION sffdiskExtension
  130. )
  131. /*++
  132. Routine Description:
  133. Arguments:
  134. Return Value:
  135. --*/
  136. {
  137. NTSTATUS status;
  138. SDBUS_DEVICE_PARAMETERS deviceParameters;
  139. BOOLEAN writeProtected = TRUE;
  140. status = SdBusGetDeviceParameters(sffdiskExtension->SdbusInterfaceContext,
  141. &deviceParameters,
  142. sizeof(deviceParameters));
  143. if (NT_SUCCESS(status)) {
  144. writeProtected = deviceParameters.WriteProtected;
  145. }
  146. return writeProtected;
  147. }
  148. NTSTATUS
  149. SdCardGetDiskParameters(
  150. IN PSFFDISK_EXTENSION sffdiskExtension
  151. )
  152. /*++
  153. Routine Description:
  154. Arguments:
  155. device extension for the card
  156. Return Value:
  157. --*/
  158. {
  159. NTSTATUS status;
  160. SDBUS_DEVICE_PARAMETERS deviceParameters;
  161. status = SdBusGetDeviceParameters(sffdiskExtension->SdbusInterfaceContext,
  162. &deviceParameters,
  163. sizeof(deviceParameters));
  164. if (NT_SUCCESS(status)) {
  165. sffdiskExtension->ByteCapacity = (ULONG) deviceParameters.Capacity;
  166. sffdiskExtension->Cylinders = sffdiskExtension->ByteCapacity / (8 * 2 * 512);
  167. sffdiskExtension->TracksPerCylinder = 2;
  168. sffdiskExtension->SectorsPerTrack = 8;
  169. sffdiskExtension->BytesPerSector = 512;
  170. }
  171. return status;
  172. }
  173. NTSTATUS
  174. SdCardRead(
  175. IN PSFFDISK_EXTENSION sffdiskExtension,
  176. IN PIRP Irp
  177. )
  178. /*++
  179. Routine Description:
  180. This routine is called to read/write data to/from the memory card.
  181. Arguments:
  182. DeviceObject - a pointer to the object that represents the device
  183. that I/O is to be done on.
  184. Irp - a pointer to the I/O Request Packet for this request.
  185. Return Value:
  186. STATUS_SUCCESS if the packet was successfully read or written; the
  187. appropriate error is propogated otherwise.
  188. --*/
  189. {
  190. NTSTATUS status;
  191. PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(Irp);
  192. ULONG lengthRead;
  193. status = SdBusReadMemory(sffdiskExtension->SdbusInterfaceContext,
  194. irpSp->Parameters.Read.ByteOffset.QuadPart + sffdiskExtension->RelativeOffset,
  195. MmGetSystemAddressForMdl(Irp->MdlAddress),
  196. irpSp->Parameters.Read.Length,
  197. &lengthRead);
  198. Irp->IoStatus.Status = status;
  199. if (NT_SUCCESS(status)) {
  200. Irp->IoStatus.Information = lengthRead;
  201. }
  202. return status;
  203. }
  204. NTSTATUS
  205. SdCardWrite(
  206. IN PSFFDISK_EXTENSION sffdiskExtension,
  207. IN PIRP Irp
  208. )
  209. /*++
  210. Routine Description:
  211. This routine is called to read/write data to/from the memory card.
  212. Arguments:
  213. DeviceObject - a pointer to the object that represents the device
  214. that I/O is to be done on.
  215. Irp - a pointer to the I/O Request Packet for this request.
  216. Return Value:
  217. STATUS_SUCCESS if the packet was successfully read or written; the
  218. appropriate error is propogated otherwise.
  219. --*/
  220. {
  221. NTSTATUS status;
  222. PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(Irp);
  223. ULONG lengthWritten;
  224. status = SdBusWriteMemory(sffdiskExtension->SdbusInterfaceContext,
  225. irpSp->Parameters.Write.ByteOffset.QuadPart + sffdiskExtension->RelativeOffset,
  226. MmGetSystemAddressForMdl(Irp->MdlAddress),
  227. irpSp->Parameters.Write.Length,
  228. &lengthWritten);
  229. Irp->IoStatus.Status = status;
  230. if (NT_SUCCESS(status)) {
  231. Irp->IoStatus.Information = lengthWritten;
  232. }
  233. return status;
  234. }