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.

337 lines
5.6 KiB

  1. /*++
  2. Copyright (c) 1991-1998 Microsoft Corporation
  3. Module Name:
  4. acpitl.c
  5. Abstract:
  6. Environment:
  7. kernel mode only
  8. Notes:
  9. Things acpi.sys needs to do.
  10. 1) Call AmliHook_InitTestHookInterface() in its DriverEntry() very early.
  11. This functyion will hook the amli functions if acpiver.sys is installed.
  12. 2) Call AmliHook_UnInitTestHookInterface() on driver unload.
  13. This is not inteded to be called to disable Amli Hooking at runtime.
  14. --*/
  15. /*
  16. #include "wdm.h"
  17. #include "ntdddisk.h"
  18. #include "stdarg.h"
  19. #include "stdio.h" */
  20. //#include "wdm.h"
  21. #include "pch.h"
  22. //#include "amlihook.h"
  23. #ifdef POOL_TAGGING
  24. #ifdef ExAllocatePool
  25. #undef ExAllocatePool
  26. #endif
  27. #define ExAllocatePool(a,b) ExAllocatePoolWithTag(a,b,'ihVA')
  28. #endif
  29. //
  30. // Globals
  31. //
  32. PCALLBACK_OBJECT g_AmliHookCallbackObject = NULL;
  33. ULONG g_AmliHookTestFlags=0;
  34. ULONG g_AmliHookIdCounter=0;
  35. ULONG g_AmliHookEnabled = 0;
  36. //
  37. // -- Get dbg flags
  38. //
  39. extern NTSTATUS OSGetRegistryValue(
  40. IN HANDLE ParentHandle,
  41. IN PWSTR ValueName,
  42. OUT PKEY_VALUE_PARTIAL_INFORMATION_ALIGN64 *Information);
  43. extern NTSTATUS OSOpenUnicodeHandle(
  44. PUNICODE_STRING UnicodeKey,
  45. HANDLE ParentHandle,
  46. PHANDLE ChildHandle);
  47. extern NTSTATUS
  48. OSCloseHandle(
  49. HANDLE Key);
  50. //
  51. // Internal function defines.
  52. //
  53. ULONG
  54. AmliHook_GetUniqueId(
  55. VOID);
  56. //
  57. // Functions
  58. //
  59. ULONG
  60. AmliHook_GetDbgFlags(
  61. VOID)
  62. {
  63. UNICODE_STRING DriverKey;
  64. HANDLE DriverKeyHandle;
  65. NTSTATUS status;
  66. PKEY_VALUE_PARTIAL_INFORMATION_ALIGN64 RegValue=NULL;
  67. ULONG DebugFlags;
  68. RtlInitUnicodeString( &DriverKey,
  69. L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\acpiver");
  70. status = OSOpenUnicodeHandle(
  71. &DriverKey,
  72. NULL,
  73. &DriverKeyHandle);
  74. if (!NT_SUCCESS(status))
  75. {
  76. return(0);
  77. }
  78. status = OSGetRegistryValue(
  79. DriverKeyHandle,
  80. L"AcpiCtrl",
  81. &RegValue);
  82. if (!NT_SUCCESS(status))
  83. {
  84. OSCloseHandle(DriverKeyHandle);
  85. return(0);
  86. }
  87. if(RegValue->DataLength == 0 ||
  88. RegValue->Type != REG_DWORD)
  89. {
  90. ExFreePool(RegValue);
  91. return(0);
  92. }
  93. DebugFlags =
  94. *((ULONG*)( ((PUCHAR)RegValue->Data)));
  95. ExFreePool(RegValue);
  96. return(DebugFlags);
  97. }
  98. ULONG
  99. AmliHook_GetUniqueId(
  100. VOID)
  101. {
  102. // BUGBUG For some reason acpi.sys
  103. // will not link with this.
  104. // Acpiver doesn't use the ID yet.
  105. //
  106. //return(InterlockedIncrement(
  107. // &g_AmliHookIdCounter));
  108. g_AmliHookIdCounter++;
  109. return(g_AmliHookIdCounter);
  110. }
  111. VOID
  112. AmliHook_InitTestData(
  113. PAMLIHOOK_DATA Data)
  114. {
  115. RtlZeroMemory(Data,sizeof(AMLIHOOK_DATA));
  116. Data->Id = AmliHook_GetUniqueId();
  117. }
  118. PAMLIHOOK_DATA
  119. AmliHook_AllocAndInitTestData(
  120. VOID)
  121. {
  122. PAMLIHOOK_DATA Data = ExAllocatePool(NonPagedPool,sizeof(AMLIHOOK_DATA));
  123. if(!Data)
  124. {
  125. AmliHook_ProcessInternalError();
  126. return(NULL);
  127. }
  128. AmliHook_InitTestData(Data);
  129. return(Data);
  130. }
  131. //
  132. // AmliHook_UnInitTestHookInterface
  133. //
  134. VOID
  135. AmliHook_UnInitTestHookInterface(
  136. VOID)
  137. {
  138. if(g_AmliHookCallbackObject)
  139. ObDereferenceObject(g_AmliHookCallbackObject);
  140. }
  141. //
  142. // AmliHook_InitTestHookInterface
  143. //
  144. NTSTATUS
  145. AmliHook_InitTestHookInterface(
  146. VOID)
  147. {
  148. NTSTATUS status = STATUS_SUCCESS;
  149. g_AmliHookCallbackObject = NULL;
  150. g_AmliHookIdCounter = 0;
  151. g_AmliHookEnabled = 0;
  152. g_AmliHookTestFlags = AmliHook_GetDbgFlags();
  153. if(g_AmliHookTestFlags & AMLIHOOK_TEST_FLAGS_HOOK_MASK)
  154. {
  155. //
  156. //--- We want to hook the AMLI.api interface.
  157. //--- So create the notify interface.
  158. //
  159. OBJECT_ATTRIBUTES objectAttributes;
  160. UNICODE_STRING CallBackName;
  161. RtlInitUnicodeString(&CallBackName,AMLIHOOK_CALLBACK_NAME);
  162. InitializeObjectAttributes (
  163. &objectAttributes,
  164. &CallBackName,
  165. OBJ_CASE_INSENSITIVE | OBJ_PERMANENT ,
  166. NULL,
  167. NULL);
  168. status = ExCreateCallback(
  169. &g_AmliHookCallbackObject,
  170. &objectAttributes,
  171. TRUE,
  172. TRUE);
  173. if(!NT_SUCCESS(status))
  174. {
  175. //
  176. //--- Failed
  177. //
  178. AmliHook_ProcessInternalError();
  179. g_AmliHookCallbackObject = NULL;
  180. return(status);
  181. }
  182. else
  183. {
  184. //
  185. //--- Functions are hooked.
  186. //
  187. g_AmliHookEnabled = AMLIHOOK_ENABLED_VALUE;
  188. }
  189. }
  190. return(status);
  191. }
  192. //
  193. // AmliHook_TestNotify
  194. //
  195. NTSTATUS
  196. AmliHook_TestNotify(
  197. PAMLIHOOK_DATA Data)
  198. {
  199. if(g_AmliHookTestFlags & AMLIHOOK_TEST_FLAGS_NO_NOTIFY_ON_CALL)
  200. {
  201. //
  202. //--- do not notify on call,
  203. //
  204. if(Data->State & AMLIHOOK_TEST_DATA_CALL_STATE_MASK)
  205. return(STATUS_SUCCESS);
  206. }
  207. if(!g_AmliHookCallbackObject)
  208. {
  209. AmliHook_ProcessInternalError();
  210. return(STATUS_UNSUCCESSFUL);
  211. }
  212. ExNotifyCallback(
  213. g_AmliHookCallbackObject,
  214. Data,
  215. NULL);
  216. return(STATUS_SUCCESS);
  217. }
  218. NTSTATUS
  219. AmliHook_TestNotifyRet(
  220. PAMLIHOOK_DATA Data,
  221. NTSTATUS Status)
  222. {
  223. if(!g_AmliHookCallbackObject)
  224. {
  225. AmliHook_ProcessInternalError();
  226. return(STATUS_UNSUCCESSFUL);
  227. }
  228. Data->State = AMLIHOOK_TEST_DATA_STATE_RETURN;
  229. Data->Ret = Status;
  230. ExNotifyCallback(
  231. g_AmliHookCallbackObject,
  232. Data,
  233. NULL);
  234. return(Data->Ret);
  235. }
  236. VOID
  237. AmliHook_ProcessInternalError(
  238. VOID)
  239. {
  240. if(g_AmliHookTestFlags & AMLIHOOK_TEST_FLAGS_DBG_ON_ERROR)
  241. DbgBreakPoint();
  242. }