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.

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