OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
AIKTarget.c
Go to the documentation of this file.
1
15#include "AIKTarget.h"
16#include "AIKTranslate.h"
17
18#include <Library/BaseMemoryLib.h>
19#include <Library/DebugLib.h>
20#include <Library/UefiBootServicesTableLib.h>
21
22EFI_STATUS
24 IN OUT AIK_TARGET *Target,
25 IN UINT8 KeyForgotThreshold
26 )
27{
28 EFI_STATUS Status;
29
30 Status = EFI_SUCCESS;
31
32 Target->KeyForgotThreshold = KeyForgotThreshold;
33
34 if (Target->KeyMapDb == NULL) {
35 Status = gBS->LocateProtocol (&gAppleKeyMapDatabaseProtocolGuid, NULL, (VOID **)&Target->KeyMapDb);
36
37 if (EFI_ERROR (Status)) {
38 DEBUG ((DEBUG_INFO, "AppleKeyMapDatabaseProtocol is unavailable - %r\n", Status));
39 return EFI_NOT_FOUND;
40 }
41
42 Status = Target->KeyMapDb->CreateKeyStrokesBuffer (
43 Target->KeyMapDb,
45 &Target->KeyMapDbIndex
46 );
47
48 if (EFI_ERROR (Status)) {
49 DEBUG ((DEBUG_INFO, "CreateKeyStrokesBuffer failed - %r\n", Status));
50 Target->KeyMapDb = NULL;
51 }
52 }
53
54 return Status;
55}
56
57VOID
59 IN OUT AIK_TARGET *Target
60 )
61{
62 if (Target->KeyMapDb != NULL) {
63 Target->KeyMapDb->RemoveKeyStrokesBuffer (Target->KeyMapDb, Target->KeyMapDbIndex);
64 Target->KeyMapDb = NULL;
65 }
66
67 Target->NumberOfKeys = 0;
68 Target->Modifiers = 0;
69 Target->ModifierCounter = 0;
70 ZeroMem (Target->Keys, sizeof (Target->Keys));
71 ZeroMem (Target->KeyCounters, sizeof (Target->KeyCounters));
72}
73
74UINT64
76 IN OUT AIK_TARGET *Target
77 )
78{
79 UINTN Index;
80 UINTN Left;
81
82 Target->Counter++;
83
84 for (Index = 0; Index < Target->NumberOfKeys; Index++) {
85 //
86 // We reported this key Target->KeyForgetThreshold times, time to say goodbye.
87 //
88 if (Target->KeyCounters[Index] + Target->KeyForgotThreshold <= Target->Counter) {
89 Left = Target->NumberOfKeys - (Index + 1);
90 if (Left > 0) {
91 CopyMem (
92 &Target->KeyCounters[Index],
93 &Target->KeyCounters[Index+1],
94 sizeof (Target->KeyCounters[0]) * Left
95 );
96 CopyMem (
97 &Target->Keys[Index],
98 &Target->Keys[Index+1],
99 sizeof (Target->Keys[0]) * Left
100 );
101 }
102
103 Target->NumberOfKeys--;
104 }
105 }
106
107 //
108 // No keys were pressed, so we did not enter AIKTargetWriteEntry.
109 // However, we still need to reset modifiers after time.
110 //
111 if (Target->ModifierCounter + Target->KeyForgotThreshold <= Target->Counter) {
112 Target->Modifiers = 0;
113 }
114
115 return Target->Counter;
116}
117
118VOID
120 IN OUT AIK_TARGET *Target,
121 IN AMI_EFI_KEY_DATA *KeyData
122 )
123{
124 APPLE_MODIFIER_MAP Modifiers;
125 APPLE_KEY_CODE Key;
126 UINTN Index;
127 UINTN InsertIndex;
128 UINT64 OldestCounter;
129
130 AIKTranslate (KeyData, &Modifiers, &Key);
131
132 Target->Modifiers = Modifiers;
133 Target->ModifierCounter = Target->Counter;
134
135 if (Key == UsbHidUndefined) {
136 //
137 // This is just a modifier or an unsupported key.
138 //
139 return;
140 }
141
142 for (Index = 0; Index < Target->NumberOfKeys; Index++) {
143 if (Target->Keys[Index] == Key) {
144 //
145 // This key was added previously, just update its counter.
146 //
147 Target->KeyCounters[Index] = Target->Counter;
148 return;
149 }
150 }
151
152 InsertIndex = Target->NumberOfKeys;
153
154 //
155 // This should not happen, but we have no room, replace the oldest key.
156 //
157 if (InsertIndex == AIK_TARGET_BUFFER_SIZE) {
158 InsertIndex = 0;
159 OldestCounter = Target->KeyCounters[InsertIndex];
160 for (Index = 1; Index < Target->NumberOfKeys; Index++) {
161 if (OldestCounter > Target->KeyCounters[Index]) {
162 OldestCounter = Target->KeyCounters[Index];
163 InsertIndex = Index;
164 }
165 }
166
167 Target->NumberOfKeys--;
168 }
169
170 //
171 // Insert the new key
172 //
173 Target->Keys[InsertIndex] = Key;
174 Target->KeyCounters[InsertIndex] = Target->Counter;
175 Target->NumberOfKeys++;
176}
177
178VOID
180 IN OUT AIK_TARGET *Target
181 )
182{
183 EFI_STATUS Status;
184
185 if (Target->KeyMapDb != NULL) {
186 Status = Target->KeyMapDb->SetKeyStrokeBufferKeys (
187 Target->KeyMapDb,
188 Target->KeyMapDbIndex,
189 Target->Modifiers,
190 Target->NumberOfKeys,
191 Target->Keys
192 );
193 } else {
194 Status = EFI_NOT_FOUND;
195 }
196
197 if (EFI_ERROR (Status)) {
198 DEBUG ((DEBUG_INFO, "Failed to submit keys to AppleMapDb - %r\n", Status));
199 }
200}
VOID AIKTargetSubmit(IN OUT AIK_TARGET *Target)
Definition AIKTarget.c:179
VOID AIKTargetWriteEntry(IN OUT AIK_TARGET *Target, IN AMI_EFI_KEY_DATA *KeyData)
Definition AIKTarget.c:119
UINT64 AIKTargetRefresh(IN OUT AIK_TARGET *Target)
Definition AIKTarget.c:75
EFI_STATUS AIKTargetInstall(IN OUT AIK_TARGET *Target, IN UINT8 KeyForgotThreshold)
Definition AIKTarget.c:23
VOID AIKTargetUninstall(IN OUT AIK_TARGET *Target)
Definition AIKTarget.c:58
#define AIK_TARGET_BUFFER_SIZE
Definition AIKTarget.h:25
VOID AIKTranslate(IN AMI_EFI_KEY_DATA *KeyData, OUT APPLE_MODIFIER_MAP *Modifiers, OUT APPLE_KEY_CODE *Key)
APPLE_HID_USAGE APPLE_KEY_CODE
Definition AppleHid.h:317
UINT16 APPLE_MODIFIER_MAP
Definition AppleHid.h:102
EFI_GUID gAppleKeyMapDatabaseProtocolGuid
EFI_BOOT_SERVICES * gBS
@ UsbHidUndefined
Definition UsbHid.h:26
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)