ReactOS  0.4.13-dev-92-gf251225
patch.c
Go to the documentation of this file.
1 /*++
2 
3 Copyright (c) 1998-2001 Klaus P. Gerlicher
4 
5 Module Name:
6 
7  patch.c
8 
9 Abstract:
10 
11  hooking of kernel internal keyboard interrupt handler
12 
13 Environment:
14 
15  Kernel mode only
16 
17 Author:
18 
19  Klaus P. Gerlicher
20  ReactOS Port: Eugene Ingerman
21 
22 Revision History:
23 
24  10-Jul-1999: created
25  15-Nov-2000: general cleanup of source files
26  12/1/2001 reactos port
27 
28 Copyright notice:
29 
30  This file may be distributed under the terms of the GNU Public License.
31 
32 --*/
33 
35 // INCLUDES
37 #include "remods.h"
38 #include "precomp.h"
39 
40 //#include <asm/system.h>
41 
42 #include <ntddkbd.h>
43 #include <ntdd8042.h>
44 #include <rosrtl/string.h>
45 
47 // GLOBALS
49 
51 static ULONG ulOldOffset = 0;
53 
55 char tempPatch[256];
56 UCHAR ucBreakKey = 'd'; // key that will break into debugger in combination with CTRL
57 
59 // FUNCTIONS
61 
62 //***********************************************************************************
63 // PiceKbdIsr - keyboard isr hook routine.
64 // IsrContext - context that we passed to keyboard driver in internal iocontrol
65 // pCurrentInput, pCurrentOutput - not implemented yet
66 // StatusByte - keyboard status register
67 // pByte - pointer to the byte read from keyboard data port. can be changed.
68 // pContinueProcessing - should keyboard driver continue processing this byte.
69 //***********************************************************************************
71  PVOID IsrContext,
72  PKEYBOARD_INPUT_DATA pCurrentInput,
73  POUTPUT_PACKET pCurrentOutput,
74  UCHAR StatusByte,
75  PUCHAR pByte,
76  PBOOLEAN pContinueProcessing,
77  PKEYBOARD_SCAN_STATE pScanState
78  )
79 {
80  static BOOLEAN bControl = FALSE;
81  BOOLEAN bForward=TRUE; // should we let keyboard driver process this keystroke
82  BOOLEAN isDown=!(*pByte & 0x80);
83  UCHAR ucKey = *pByte & 0x7f;
84 
85  ENTER_FUNC();
86 
87  // BUG?? should protect with spinlock since bControl is static.
88  DPRINT((0,"PiceKbdIsr(pByte: %x, val: %x,%u)\n",pByte,*pByte,isDown));
89  DPRINT((0,"PiceKbdIsr(1): bControl = %u bForward = %u bEnterNow = %u\n",bControl,bForward,bEnterNow));
90 
91  if(isDown)
92  {
93  DPRINT((0,"bControl: %x, ucKey: %x, breakkey: %x\n", bControl, ucKey, AsciiToScan(ucBreakKey)));
94  // CTRL pressed
95  if(ucKey==0x1d)
96  {
97  bControl=TRUE;
98  }
99  else if(bControl==TRUE && ucKey==AsciiToScan(ucBreakKey)) // CTRL-D
100  {
101  // fake a CTRL-D release call
102  bEnterNow=TRUE;
103  bControl=FALSE;
104  // simulate an initial break
105  __asm__("\n\t \
106  pushfl\n\t \
107  pushl %cs\n\t \
108  pushl $returnpoint\n\t \
109  pushl $" STR(REASON_CTRLF) "\n\t \
110  jmp NewInt31Handler\n\t \
111  returnpoint:");
112  *pByte = 0x1d | 0x80 | 0x7f;
113  bForward=TRUE;
114  }
115  else if((ucKey == 66|| ucKey == 68) && bStepping)
116  {
117  bForward=FALSE;
118  }
119 
120  }
121  else
122  {
123  // CTRL released
124  if(ucKey==0x1d)
125  {
126  bControl=FALSE;
127  }
128  else if((ucKey == 66|| ucKey == 68) && bStepping)
129  {
130  bForward=FALSE;
131  }
132  }
133  *pContinueProcessing = bForward;
134  DPRINT((5,"*pContinueProcessing: %d\n", *pContinueProcessing));
135  LEAVE_FUNC();
136  return TRUE;
137 }
138 
139 //***********************************************************************************
140 // PiceSendIoctl - send internal_io_control to the driver
141 // Target - Device Object that receives control request
142 // Ioctl - request
143 // InputBuffer - Type3Buffer will be pointing here
144 // InputBufferLength - length of inputbuffer
145 //***********************************************************************************
148 {
149  KEVENT event;
152  PIRP irp;
153 
156  FALSE
157  );
158 
159  if (NULL == (irp = IoBuildDeviceIoControlRequest(Ioctl,
160  Target,
161  InputBuffer,
163  0,
164  0,
165  TRUE,
166  &event,
167  &iosb))) {
168  DPRINT((0,"PiceSendIoctl: STATUS_INSUFFICIENT_RESOURCES\n"));
170  }
171 
172  status = IoCallDriver(Target, irp);
173 
174  if (STATUS_PENDING == status) {
175 
177  Executive,
178  KernelMode,
179  FALSE,
180  NULL);
181 
183  status = iosb.Status;
184  }
185  DPRINT((0,"PiceSendIoctl: status: %d\n",NT_SUCCESS(status)));
186  return status;
187 }
188 
189 //**************************************************
190 // PatchKeyboardDriver - set keyboard driver hook.
191 // We use interface supported by standard keyboard drivers.
192 //**************************************************
194 {
196  //When we have i8042 driver this should be changed!!!!!!!
197  UNICODE_STRING DevName = ROS_STRING_INITIALIZER(L"\\Device\\Keyboard");
198  PDEVICE_OBJECT kbdDevice = NULL;
199  PFILE_OBJECT FO = NULL;
201 
202  ENTER_FUNC();
203 
204  //Get pointer to keyboard device
205  if( !NT_SUCCESS( status = IoGetDeviceObjectPointer( &DevName, FILE_READ_ACCESS, &FO, &kbdDevice ) ) )
206  {
207  DPRINT((0,"PatchKeyboardDriver: IoGetDeviceObjectPointer status: %x\n", status));
208  return FALSE;
209  }
210  phkData = ExAllocatePool( PagedPool, sizeof( INTERNAL_I8042_HOOK_KEYBOARD ) );
211  RtlZeroMemory( phkData, sizeof( INTERNAL_I8042_HOOK_KEYBOARD ) );
212 
214  phkData->Context = (PVOID) NULL; //DeviceObject;
215 
216  //call keyboard device internal io control to hook keyboard input stream
218  phkData, sizeof( INTERNAL_I8042_HOOK_KEYBOARD ) );
219  DPRINT((0,"PatchKeyboardDriver: PiceSendIoctl status: %x\n", status));
220 
221 
223  ExFreePool(phkData);
224 
225  LEAVE_FUNC();
226 
227  return NT_SUCCESS(status);
228 }
229 
231 {
232  ENTER_FUNC();
233  DbgPrint("RestoreKeyboardDriver: Not Implemented yet!!!\n");
234  LEAVE_FUNC();
235 }
#define FILE_READ_ACCESS
Definition: nt_native.h:610
volatile BOOLEAN bEnterNow
Definition: shell.c:75
static PIO_STATUS_BLOCK iosb
Definition: file.c:98
#define TRUE
Definition: types.h:120
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
static ULONG ulOldOffset
Definition: patch.c:51
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
#define DbgPrint
Definition: loader.c:25
static PUCHAR pPatchAddress
Definition: patch.c:50
unsigned char * PUCHAR
Definition: retypes.h:3
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS PiceSendIoctl(PDEVICE_OBJECT Target, ULONG Ioctl, PVOID InputBuffer, ULONG InputBufferLength)
Definition: patch.c:146
void(* old_handle_scancode)(UCHAR, int)
Definition: patch.c:54
#define LEAVE_FUNC()
Definition: debug.h:43
NTSTATUS NTAPI IoGetDeviceObjectPointer(IN PUNICODE_STRING ObjectName, IN ACCESS_MASK DesiredAccess, OUT PFILE_OBJECT *FileObject, OUT PDEVICE_OBJECT *DeviceObject)
Definition: device.c:1434
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
BOOLEAN PiceKbdIsr(PVOID IsrContext, PKEYBOARD_INPUT_DATA pCurrentInput, POUTPUT_PACKET pCurrentOutput, UCHAR StatusByte, PUCHAR pByte, PBOOLEAN pContinueProcessing, PKEYBOARD_SCAN_STATE pScanState)
Definition: patch.c:70
BOOLEAN bStepping
Definition: parse.c:63
CHAR InputBuffer[80]
Definition: conmgr.c:33
#define STR(x)
Definition: utils.h:34
OUT PI8042_KEYBOARD_ISR IsrRoutine
Definition: ntdd8042.h:179
__asm__("\t.globl GetPhys\n" "GetPhys:\t\n" "mflr 0\n\t" "stwu 0,-16(1)\n\t" "mfmsr 5\n\t" "andi. 6,5,0xffef\n\t" "mtmsr 6\n\t" "isync\n\t" "sync\n\t" "lwz 3,0(3)\n\t" "mtmsr 5\n\t" "isync\n\t" "sync\n\t" "lwz 0,0(1)\n\t" "addi 1,1,16\n\t" "mtlr 0\n\t" "blr")
UCHAR ucBreakKey
Definition: patch.c:56
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
void * PVOID
Definition: retypes.h:9
volatile BOOLEAN bControl
Definition: shell.c:70
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_PENDING
Definition: ntstatus.h:82
UCHAR AsciiToScan(UCHAR s)
Definition: utils.c:2113
_Must_inspect_result_ typedef _In_ ULONG _In_ BOOLEAN Target
Definition: iotypes.h:1067
* PFILE_OBJECT
Definition: iotypes.h:1954
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
BOOLEAN PatchKeyboardDriver(void)
Definition: patch.c:193
unsigned char UCHAR
Definition: xmlstorage.h:181
char * PBOOLEAN
Definition: retypes.h:11
static const WCHAR L[]
Definition: oid.c:1250
#define ExAllocatePool(type, size)
Definition: fbtusb.h:44
struct _cl_event * event
Definition: glext.h:7739
_Must_inspect_result_ _In_ PFILE_OBJECT _In_ ULONG _In_ ULONG InputBufferLength
Definition: fltkernel.h:1372
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
static ULONG ulKeyPatchFlags
Definition: patch.c:52
void RestoreKeyboardDriver(void)
Definition: patch.c:230
char tempPatch[256]
Definition: patch.c:55
NTSTATUS NTAPI IoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1218
PIRP NTAPI IoBuildDeviceIoControlRequest(IN ULONG IoControlCode, IN PDEVICE_OBJECT DeviceObject, IN PVOID InputBuffer, IN ULONG InputBufferLength, IN PVOID OutputBuffer, IN ULONG OutputBufferLength, IN BOOLEAN InternalDeviceIoControl, IN PKEVENT Event, IN PIO_STATUS_BLOCK IoStatusBlock)
Definition: irp.c:881
#define IOCTL_INTERNAL_I8042_HOOK_KEYBOARD
Definition: ntdd8042.h:36
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
#define ENTER_FUNC()
Definition: debug.h:42
enum _KEYBOARD_SCAN_STATE * PKEYBOARD_SCAN_STATE
BOOLEAN(NTAPI * PI8042_KEYBOARD_ISR)(PVOID IsrContext, PKEYBOARD_INPUT_DATA CurrentInput, POUTPUT_PACKET CurrentOutput, UCHAR StatusByte, PUCHAR Byte, PBOOLEAN ContinueProcessing, PKEYBOARD_SCAN_STATE ScanState)
Definition: ntdd8042.h:167
return STATUS_SUCCESS
Definition: btrfs.c:2725
static SERVICE_STATUS status
Definition: service.c:31
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
Definition: ps.c:97
#define REASON_CTRLF
Definition: shell.h:73