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.

386 lines
9.3 KiB

  1. /*++
  2. Copyright (C) 1997-99 Microsoft Corporation
  3. Module Name:
  4. regutil.c
  5. Abstract:
  6. This contain registry access routines
  7. Author:
  8. Joe Dai (joedai)
  9. Environment:
  10. kernel mode only
  11. Notes:
  12. Revision History:
  13. --*/
  14. #include "ideport.h"
  15. #ifdef ALLOC_PRAGMA
  16. #pragma alloc_text(PAGE, IdePortGetParameterFromServiceSubKey)
  17. #pragma alloc_text(PAGE, IdePortRegQueryRoutine)
  18. #pragma alloc_text(PAGE, IdePortGetDeviceParameter)
  19. #pragma alloc_text(PAGE, IdePortSaveDeviceParameter)
  20. #pragma alloc_text(PAGE, IdePortOpenServiceSubKey)
  21. #pragma alloc_text(PAGE, IdePortCloseServiceSubKey)
  22. #endif // ALLOC_PRAGMA
  23. NTSTATUS
  24. IdePortGetParameterFromServiceSubKey (
  25. IN PDRIVER_OBJECT DriverObject,
  26. IN PWSTR ParameterName,
  27. IN ULONG ParameterType,
  28. IN BOOLEAN Read,
  29. OUT PVOID *ParameterValue,
  30. IN ULONG ParameterValueWriteSize
  31. )
  32. {
  33. NTSTATUS status;
  34. HANDLE deviceParameterHandle;
  35. RTL_QUERY_REGISTRY_TABLE queryTable[2];
  36. ULONG defaultParameterValue;
  37. CCHAR deviceBuffer[50];
  38. ANSI_STRING ansiString;
  39. UNICODE_STRING subKeyPath;
  40. HANDLE subServiceKey;
  41. UNICODE_STRING unicodeParameterName;
  42. PAGED_CODE();
  43. *ParameterValue = NULL;
  44. sprintf (deviceBuffer, DRIVER_PARAMETER_SUBKEY);
  45. RtlInitAnsiString(&ansiString, deviceBuffer);
  46. status = RtlAnsiStringToUnicodeString(&subKeyPath, &ansiString, TRUE);
  47. if (NT_SUCCESS(status)) {
  48. subServiceKey = IdePortOpenServiceSubKey (
  49. DriverObject,
  50. &subKeyPath
  51. );
  52. RtlFreeUnicodeString (&subKeyPath);
  53. if (subServiceKey) {
  54. if (Read) {
  55. RtlZeroMemory(&queryTable, sizeof(queryTable));
  56. queryTable->QueryRoutine = IdePortRegQueryRoutine;
  57. queryTable->Flags = RTL_QUERY_REGISTRY_REQUIRED | RTL_QUERY_REGISTRY_NOEXPAND;
  58. queryTable->Name = ParameterName;
  59. queryTable->EntryContext = ParameterValue;
  60. queryTable->DefaultType = 0;
  61. queryTable->DefaultData = NULL;
  62. queryTable->DefaultLength = 0;
  63. status = RtlQueryRegistryValues(RTL_REGISTRY_HANDLE,
  64. (PWSTR) subServiceKey,
  65. queryTable,
  66. ULongToPtr( ParameterType ),
  67. NULL);
  68. } else {
  69. RtlInitUnicodeString (&unicodeParameterName, ParameterName);
  70. status = ZwSetValueKey(
  71. subServiceKey,
  72. &unicodeParameterName,
  73. 0,
  74. ParameterType,
  75. ParameterValue,
  76. ParameterValueWriteSize
  77. );
  78. }
  79. //
  80. // close what we open
  81. //
  82. IdePortCloseServiceSubKey (
  83. subServiceKey
  84. );
  85. }
  86. }
  87. return status;
  88. }
  89. NTSTATUS
  90. IdePortRegQueryRoutine (
  91. IN PWSTR ValueName,
  92. IN ULONG ValueType,
  93. IN PVOID ValueData,
  94. IN ULONG ValueLength,
  95. IN PVOID Context,
  96. IN PVOID EntryContext
  97. )
  98. {
  99. PVOID *parameterValue = EntryContext;
  100. ULONG parameterType = PtrToUlong(Context);
  101. PAGED_CODE();
  102. if (ValueType == parameterType) {
  103. if (ValueType == REG_MULTI_SZ) {
  104. *parameterValue = ExAllocatePool(PagedPool, ValueLength);
  105. if (*parameterValue) {
  106. RtlMoveMemory(*parameterValue, ValueData, ValueLength);
  107. return STATUS_SUCCESS;
  108. }
  109. } else if (ValueType == REG_DWORD) {
  110. PULONG ulongValue;
  111. ulongValue = (PULONG) parameterValue;
  112. *ulongValue = *((PULONG) ValueData);
  113. return STATUS_SUCCESS;
  114. }
  115. }
  116. return STATUS_UNSUCCESSFUL;
  117. }
  118. NTSTATUS
  119. IdePortGetDeviceParameter (
  120. IN PFDO_EXTENSION FdoExtension,
  121. IN PWSTR ParameterName,
  122. IN OUT PULONG ParameterValue
  123. )
  124. /*++
  125. Routine Description:
  126. retrieve a devnode registry parameter
  127. Arguments:
  128. FdoExtension - FDO Extension
  129. ParameterName - parameter name to look up
  130. ParameterValuse - default parameter value
  131. Return Value:
  132. NT Status
  133. --*/
  134. {
  135. NTSTATUS status;
  136. HANDLE deviceParameterHandle;
  137. RTL_QUERY_REGISTRY_TABLE queryTable[2];
  138. ULONG defaultParameterValue;
  139. PAGED_CODE();
  140. //
  141. // open the given parameter
  142. //
  143. status = IoOpenDeviceRegistryKey(FdoExtension->AttacheePdo,
  144. PLUGPLAY_REGKEY_DRIVER,
  145. KEY_READ,
  146. &deviceParameterHandle);
  147. if(!NT_SUCCESS(status)) {
  148. return status;
  149. }
  150. RtlZeroMemory(&queryTable, sizeof(queryTable));
  151. defaultParameterValue = *ParameterValue;
  152. queryTable->Flags = RTL_QUERY_REGISTRY_DIRECT | RTL_QUERY_REGISTRY_REQUIRED;
  153. queryTable->Name = ParameterName;
  154. queryTable->EntryContext = ParameterValue;
  155. queryTable->DefaultType = REG_DWORD;
  156. queryTable->DefaultData = &defaultParameterValue;
  157. queryTable->DefaultLength = sizeof(ULONG);
  158. status = RtlQueryRegistryValues(RTL_REGISTRY_HANDLE,
  159. (PWSTR) deviceParameterHandle,
  160. queryTable,
  161. NULL,
  162. NULL);
  163. if (!NT_SUCCESS(status)) {
  164. *ParameterValue = defaultParameterValue;
  165. }
  166. //
  167. // close what we open
  168. //
  169. ZwClose(deviceParameterHandle);
  170. return status;
  171. } // IdePortGetDeviceParameter
  172. NTSTATUS
  173. IdePortSaveDeviceParameter (
  174. IN PFDO_EXTENSION FdoExtension,
  175. IN PWSTR ParameterName,
  176. IN ULONG ParameterValue
  177. )
  178. /*++
  179. Routine Description:
  180. save a devnode registry parameter
  181. Arguments:
  182. FdoExtension - FDO Extension
  183. ParameterName - parameter name to save
  184. ParameterValuse - parameter value to save
  185. Return Value:
  186. NT Status
  187. --*/
  188. {
  189. NTSTATUS status;
  190. HANDLE deviceParameterHandle;
  191. PAGED_CODE();
  192. //
  193. // open the given parameter
  194. //
  195. status = IoOpenDeviceRegistryKey(FdoExtension->AttacheePdo,
  196. PLUGPLAY_REGKEY_DRIVER,
  197. KEY_WRITE,
  198. &deviceParameterHandle);
  199. if(!NT_SUCCESS(status)) {
  200. DebugPrint((DBG_WARNING,
  201. "IdePortSaveDeviceParameter: IoOpenDeviceRegistryKey() returns 0x%x\n",
  202. status));
  203. return status;
  204. }
  205. //
  206. // write the parameter value
  207. //
  208. status = RtlWriteRegistryValue(
  209. RTL_REGISTRY_HANDLE,
  210. (PWSTR) deviceParameterHandle,
  211. ParameterName,
  212. REG_DWORD,
  213. &ParameterValue,
  214. sizeof (ParameterValue)
  215. );
  216. if(!NT_SUCCESS(status)) {
  217. DebugPrint((DBG_WARNING,
  218. "IdePortSaveDeviceParameter: RtlWriteRegistryValue() returns 0x%x\n",
  219. status));
  220. }
  221. //
  222. // close what we open
  223. //
  224. ZwClose(deviceParameterHandle);
  225. return status;
  226. } // IdePortSaveDeviceParameter
  227. HANDLE
  228. IdePortOpenServiceSubKey (
  229. IN PDRIVER_OBJECT DriverObject,
  230. IN PUNICODE_STRING SubKeyPath
  231. )
  232. {
  233. PIDEDRIVER_EXTENSION ideDriverExtension;
  234. OBJECT_ATTRIBUTES objectAttributes;
  235. HANDLE serviceKey;
  236. HANDLE subServiceKey;
  237. NTSTATUS status;
  238. ideDriverExtension = IoGetDriverObjectExtension(
  239. DriverObject,
  240. DRIVER_OBJECT_EXTENSION_ID
  241. );
  242. if (!ideDriverExtension) {
  243. return NULL;
  244. }
  245. InitializeObjectAttributes(&objectAttributes,
  246. &ideDriverExtension->RegistryPath,
  247. OBJ_CASE_INSENSITIVE,
  248. NULL,
  249. (PSECURITY_DESCRIPTOR) NULL);
  250. status = ZwOpenKey(&serviceKey,
  251. KEY_ALL_ACCESS,
  252. &objectAttributes);
  253. if (!NT_SUCCESS(status)) {
  254. return NULL;
  255. }
  256. InitializeObjectAttributes(&objectAttributes,
  257. SubKeyPath,
  258. OBJ_CASE_INSENSITIVE,
  259. serviceKey,
  260. (PSECURITY_DESCRIPTOR) NULL);
  261. status = ZwOpenKey(&subServiceKey,
  262. KEY_READ,
  263. &objectAttributes);
  264. ZwClose(serviceKey);
  265. if (NT_SUCCESS(status)) {
  266. return subServiceKey;
  267. } else {
  268. return NULL;
  269. }
  270. }
  271. VOID
  272. IdePortCloseServiceSubKey (
  273. IN HANDLE SubServiceKey
  274. )
  275. {
  276. ZwClose(SubServiceKey);
  277. }