-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkeyboard.cpp
596 lines (414 loc) · 9.48 KB
/
keyboard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*++
This is the part of NGdbg kernel debugger
keyboard.cpp
High-level PS/2 keyboard driver for the kernel debugger
--*/
#include <ntifs.h>
#include "dbgeng.h"
#include "i8042.h"
VOID WR_ENTER_DEBUGGER (BOOLEAN UserInitiated, PDBG_CALLBACK Callback, PVOID Argument);
VOID ProcessMouseInput (UCHAR Byte);
//
// Scan-Code tables
//
char KeybdAsciiCodes[] =
{
0,0,'1','2','3','4','5','6','7','8','9','0','-','=',0,0,
'q','w','e','r','t','y','u','i','o','p','[',']',10,0,
'a','s','d','f','g','h','j','k','l',';','\'', '`',0,
'\\','z','x','c','v','b','n','m',',','.','/',0,'*',0,
' ',0, 0,0,0,0,0,0,0,0,0,0, 0,0, '7','8','9','-','4','5',
'6','+','1','2','3','0','.', 0,0
};
char KeybdAsciiCodesShifted[] =
{
0,0,'!','@','#','$','%','^','&','*','(',')','_','+',0,0,
'Q','W','E','R','T','Y','U','I','O','P','{','}',10,0,
'A','S','D','F','G','H','J','K','L',':','"', '~',0,
'|','Z','X','C','V','B','N','M','<','>','?',0,'*',0,
' ',0, 0,0,0,0,0,0,0,0,0,0, 0,0, '7','8','9','-','4','5',
'6','+','1','2','3','0','.', 0,0
};
char *KeybdScanToAsciiTables[] = { KeybdAsciiCodes, KeybdAsciiCodesShifted };
#if 0
UCHAR KbdGetKeyPolled()
{
UCHAR ScanCode;
UCHAR AsciiCode;
UCHAR response;
UCHAR Byte;
//
// Poll until we get back a controller status value that
// indicates the output buffer is full
//
while (((response = KBD_GET_STATUS_BYTE()) & BUFFER_FULL) != I8042_OUTPUT_BUFFER_FULL)
{
if (response & I8042_AUXOUT_BUFFER_FULL)
{
//
// There is something in the i8042 output buffer,
// but it isn't from the device we want to get a byte from
// Eat the byte and try again
//
Byte = KBD_GET_DATA_BYTE ();
KdPrint(("KBDPOLL: Response %X ate %x\n", response, Byte));
}
else
{
//KdPrint(("KBDPOLL: Response %X Stalling\n", response));
KeStallExecutionProcessor (200);
}
}
//KdPrint(("KBDPOLL: Response %X, got\n", response));
ScanCode = KBD_GET_DATA_BYTE();
AsciiCode = KeybdScanCodeToAsciiCode (ScanCode);
#if 0
KdPrint (("KBDPOLL: exit with byte %02X ", ScanCode));
if (AsciiCode)
KdPrint(("(%c)", AsciiCode));
KdPrint(("\n"));
#endif
return ScanCode;
}
UCHAR KbdGetKeyAsychnorous()
{
UCHAR response;
UCHAR Byte;
//
// Poll until we get back a controller status value that indicates
// the output buffer is full.
//
while (((response = KBD_GET_STATUS_BYTE()) & BUFFER_FULL) != I8042_OUTPUT_BUFFER_FULL)
{
if (response & I8042_AUXOUT_BUFFER_FULL)
{
//
// There is something in the i8042 output buffer, but it
// isn't from the device we want to get a byte from. Eat
// the byte and try again.
//
Byte = KBD_GET_DATA_BYTE();
KdPrint(("KBDASYNCH: ate %X\n", Byte));
}
else
{
// Try again
}
}
Byte = KBD_GET_DATA_BYTE();
//KdPrint(("KBDASYNC: exit with byte %X\n", Byte));
return Byte;
}
#endif
UCHAR
KbdGetKeyPolled(
IN BOOLEAN AllowMouseCallback
)
/*++
Routine Description
Read scan-code in polled mode.
Simply call 8042 driver
Arguments
AllowMouseCallback
Should 8042.sys call mouse callback if it detects byte from mousr
Return Value
Scan-code or 0 if the timeout occured
Environment
This function is called at raised IRQL
--*/
{
NTSTATUS Status;
UCHAR Byte;
Status = I8xGetBytePolled (KeyboardDevice, &Byte, AllowMouseCallback);
if (!NT_SUCCESS(Status))
Byte = 0;
return Byte;
}
extern KDPC HotkeyResetStateDpc;
VOID
PutScanCode(
UCHAR ScanCode
)
/*++
Routine Description
This routine puts scan-code to 8042 output register
and requests keyboard IRQ as if user pressed key manually.
It is used to simulate some scan-codes.
Arguments
ScanCode
Scan code to put
Return Value
None
Environment
This function must be called at DISPATCH_LEVEL <= IRQL < keyboard DIRQL because
IRQ request should occur
--*/
{
//
// Raise IRQL to dispatch if it is less than dispatch level
//
KIRQL Irql;
Irql = KfRaiseIrql (DISPATCH_LEVEL);
// Tell 8042 that we want to write to output register
I8xPutBytePolled (CommandPort,
ControllerDevice,
FALSE,
(UCHAR)I8042_WRITE_OUTPUT_REGISTER);
// Write value to 8042 output register
I8xPutBytePolled (DataPort,
ControllerDevice,
FALSE,
ScanCode);
//
// Lower IRQL back.
// Check build of NT bugchecks if KeLowerIrql tries to lower
// irql greater than current value.
// So, use this function only at DISPATCH_LEVEL
//
KfLowerIrql (Irql);
}
BOOLEAN WindowsNum;
BOOLEAN WindowsCaps;
BOOLEAN WindowsScroll;
BOOLEAN DbgNum;
BOOLEAN DbgCaps;
VOID
KbdSetLeds(
BOOLEAN Num,
BOOLEAN Caps,
BOOLEAN Scroll
)
/*++
Routine Description
Set keyboard LEDs.
This function is always called at high irql.
Arguments
Num, Caps, Scroll - new values for keyboard indicators
Return Value
None
Environment
Raised IRQL
--*/
{
UCHAR LedStatus = 0;
if (Num)
LedStatus |= 2;
if (Caps)
LedStatus |= 4;
if (Scroll)
LedStatus |= 1;
I8xPutBytePolled (DataPort, KeyboardDevice, TRUE, (UCHAR) KBD_SET_INDICATORS);
I8xPutBytePolled (DataPort, KeyboardDevice, TRUE, LedStatus);
}
VOID
HotkeyResetStateDeferredRoutine(
IN struct _KDPC *Dpc,
IN PVOID DeferredContext,
IN PVOID SystemArgument1,
IN PVOID SystemArgument2
)
/*++
Routine Description
CALLBACK
DPC deferred routine.
Put break codes for ctrl, alt shift and F12
to let i8042 know that user have released them and
restore windows LED states
Arguments
Dpc
Pointer to KPDC
DeferredContext
SystemArgument1
SystemArgument2
Always NULL
Return Value
None
Environment
This is DPC routine, so it executes at DISPATCH_LEVEL
--*/
{
KdPrint(("Ctrl-Shift-Alt restore DPC7\n"));
PutScanCode (0xAA); // Shift UP
PutScanCode (0x9D); // Ctrl UP
PutScanCode (0xB8); // Alt UP
PutScanCode (0xD8); // F12 UP
KbdSetLeds (WindowsNum, WindowsCaps, WindowsScroll);
}
VOID
ProcessLockKeys (
UCHAR ScanCode,
BOOLEAN UP
)
/*++
Routine Description
This function is called when kernel debugger is active to process scan-code
Process num and caps lock. Scroll lock is always ON when kernel gui is active
Argument
ScanCode
Low 7 bits of scan code
UP
High 8th bit of scan code
Return Value
None
Environment
Raised IRQL, kernel debugger active
--*/
{
switch (ScanCode)
{
case 0x45: // NUM
if (!UP)
{
DbgNum = !DbgNum;
KbdSetLeds (DbgNum, DbgCaps, 1);
KdPrint(("dbg num lock %s\n", DbgNum ? "on" : "off"));
}
break;
case 0x3A: // CAPS
if (!UP)
{
DbgCaps = !DbgCaps;
KbdSetLeds (DbgNum, DbgCaps, 1);
KdPrint(("dbg caps lock %s\n", DbgCaps ? "on" : "off"));
}
break;
}
}
// Shift, Ctrl and Alt states
BOOLEAN Shift = FALSE, Ctrl = FALSE, Alt = FALSE;
VOID
ProcessControlKeys (
UCHAR ScanCode,
BOOLEAN UP
)
/*++
Routine Description
This function is called at every key press.
Action: Process control keys - shift, control, alt.
Arguments
ScanCode
Low 7 bits of scan code
UP
High 8th bit of scan code
Return Value
None
Environment
Raised IRQL
--*/
{
switch (ScanCode)
{
case 42:
case 54:
Shift = !UP;
break;
case 0x1D:
Ctrl = !UP;
break;
case 56:
Alt = !UP;
break;
}
}
VOID
KeybProcessUserInputLocked (
UCHAR ScanCode
)
/*++
Routine Description
Process user input when kernel debugger is active.
Arguments
Full 8-bit scan code
Return Value
None
Environment
Raised IRQL, kernel debugger active
--*/
{
BOOLEAN UP;
UP = ScanCode >> 7;
ScanCode &= 0x7F;
ProcessControlKeys (ScanCode, UP);
ProcessLockKeys (ScanCode, UP);
}
extern BOOLEAN BootDebuggingInitiated;
VOID
ProcessScanCode (
UCHAR ScanCode
)
/*++
Routine Description
Process key press.
Arguments
ScanCode
Full 8-bit scan code
Return Value
None
Environment
Raised IRQL, called from hook set by IOCTL_INTERNAL_I8042_HOOK_KEYBOARD
--*/
{
BOOLEAN UP;
UP = ScanCode >> 7;
ScanCode &= 0x7F;
ProcessControlKeys (ScanCode, UP);
switch (ScanCode)
{
// Copy windows lock key states to global var.
case 0x3A: // CAPS
if (!UP)
{
WindowsCaps = !WindowsCaps;
KdPrint(("win caps lock %s\n", WindowsCaps ? "on" : "off"));
}
break;
case 0x45: // NUM
if (!UP)
{
WindowsNum = !WindowsNum;
KdPrint(("win num lock %s\n", WindowsNum ? "on" : "off"));
}
break;
case 0x46: // SCROLL
if (!UP)
{
WindowsScroll = !WindowsScroll;
KdPrint(("win scrl lock %s\n", WindowsScroll ? "on" : "off"));
}
break;
case 0x58:
if (Ctrl && Alt && Shift)
{
KdPrint(("Enter debugger\n"));
WR_ENTER_DEBUGGER (TRUE, NULL, NULL);
KdPrint(("Exit debugger\n"));
if (BootDebuggingInitiated == FALSE)
{
//
// Put break codes for ctrl, alt and shift
// to let i8042 know that user have released them.
//
KeInsertQueueDpc (
&HotkeyResetStateDpc,
NULL,
NULL
);
}
}
break;
}
}
//
// Convert scan code to ascii code
//
UCHAR KeybdScanCodeToAsciiCode (UCHAR ScanCode)
{
BOOLEAN Shifted = Shift;
if (DbgCaps)
{
Shifted = !Shifted;
}
if (ScanCode < sizeof(KeybdAsciiCodes))
return KeybdScanToAsciiTables[Shifted][ScanCode];
return 0;
}