]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.c
Pass ECC check.
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbKbDxe / KeyBoard.c
index 79598c5a01dc002b95d15299d824b637ad75e73e..cf682505b9499924187be40729f9026a6d7ceb76 100644 (file)
@@ -286,8 +286,6 @@ UINT8 ModifierValueToEfiScanCodeConvertionTable[] = {
   SCAN_F12,        // EFI_FUNCTION_KEY_TWELVE_MODIFIER\r
 };\r
 \r
-EFI_GUID  mKeyboardLayoutEventGuid = EFI_HII_SET_KEYBOARD_LAYOUT_EVENT_GUID;\r
-\r
 /**\r
   Initialize Key Convertion Table by using default keyboard layout.\r
 \r
@@ -413,7 +411,7 @@ GetCurrentKeyboardLayout (
                             KeyboardLayout\r
                             );\r
     if (EFI_ERROR (Status)) {\r
-      gBS->FreePool (KeyboardLayout);\r
+      FreePool (KeyboardLayout);\r
       KeyboardLayout = NULL;\r
     }\r
   }\r
@@ -613,7 +611,7 @@ SetKeyboardLayoutEvent (
       NsKey = KeyDescriptor + 1;\r
       for (Index2 = Index + 1; Index2 < KeyboardLayout->DescriptorCount; Index2++) {\r
         CopyMem (&TempKey, NsKey, sizeof (EFI_KEY_DESCRIPTOR));\r
-        if (TempKey.Modifier & EFI_NS_KEY_DEPENDENCY_MODIFIER) {\r
+        if (TempKey.Modifier == EFI_NS_KEY_DEPENDENCY_MODIFIER) {\r
           KeyCount++;\r
         } else {\r
           break;\r
@@ -719,7 +717,7 @@ InitKeyboardLayout (
                   TPL_NOTIFY,\r
                   SetKeyboardLayoutEvent,\r
                   UsbKeyboardDevice,\r
-                  &mKeyboardLayoutEventGuid,\r
+                  &gEfiHiiKeyBoardLayoutGuid,\r
                   &UsbKeyboardDevice->KeyboardLayoutEvent\r
                   );\r
   if (EFI_ERROR (Status)) {\r
@@ -814,7 +812,6 @@ InitUSBKeyboard (
   //\r
   // Set boot protocol for the USB Keyboard.\r
   // This driver only supports boot protocol.\r
-  // The device that does not support boot protocol is not supported.\r
   //\r
   if (Protocol != BOOT_PROTOCOL) {\r
     UsbSetProtocolRequest (\r
@@ -871,33 +868,36 @@ InitUSBKeyboard (
   ZeroMem (UsbKeyboardDevice->LastKeyCodeArray, sizeof (UINT8) * 8);\r
 \r
   //\r
-  // Set a timer for repeat keys' generation.\r
+  // Create event for repeat keys' generation.\r
   //\r
   if (UsbKeyboardDevice->RepeatTimer != NULL) {\r
     gBS->CloseEvent (UsbKeyboardDevice->RepeatTimer);\r
     UsbKeyboardDevice->RepeatTimer = NULL;\r
   }\r
 \r
-  Status = gBS->CreateEvent (\r
-                  EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
-                  TPL_NOTIFY,\r
-                  USBKeyboardRepeatHandler,\r
-                  UsbKeyboardDevice,\r
-                  &UsbKeyboardDevice->RepeatTimer\r
-                  );\r
+  gBS->CreateEvent (\r
+         EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
+         TPL_NOTIFY,\r
+         USBKeyboardRepeatHandler,\r
+         UsbKeyboardDevice,\r
+         &UsbKeyboardDevice->RepeatTimer\r
+         );\r
 \r
+  //\r
+  // Create event for delayed recovery, which deals with device error.\r
+  //\r
   if (UsbKeyboardDevice->DelayedRecoveryEvent != NULL) {\r
     gBS->CloseEvent (UsbKeyboardDevice->DelayedRecoveryEvent);\r
     UsbKeyboardDevice->DelayedRecoveryEvent = NULL;\r
   }\r
 \r
-  Status = gBS->CreateEvent (\r
-                  EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
-                  TPL_NOTIFY,\r
-                  USBKeyboardRecoveryHandler,\r
-                  UsbKeyboardDevice,\r
-                  &UsbKeyboardDevice->DelayedRecoveryEvent\r
-                  );\r
+  gBS->CreateEvent (\r
+         EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
+         TPL_NOTIFY,\r
+         USBKeyboardRecoveryHandler,\r
+         UsbKeyboardDevice,\r
+         &UsbKeyboardDevice->DelayedRecoveryEvent\r
+         );\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -1161,6 +1161,8 @@ KeyboardHandler (
       // Handle repeat key\r
       //\r
       KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, CurKeyCodeBuffer[Index]);\r
+      ASSERT (KeyDescriptor != NULL);\r
+\r
       if (KeyDescriptor->Modifier == EFI_NUM_LOCK_MODIFIER || KeyDescriptor->Modifier == EFI_CAPS_LOCK_MODIFIER) {\r
         //\r
         // For NumLock or CapsLock pressed, there is no need to handle repeat key for them.\r
@@ -1192,6 +1194,7 @@ KeyboardHandler (
     RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey);\r
 \r
     KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, UsbKey.KeyCode);\r
+    ASSERT (KeyDescriptor != NULL);\r
 \r
     switch (KeyDescriptor->Modifier) {\r
 \r
@@ -1302,6 +1305,8 @@ USBParseKey (
     RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey);\r
 \r
     KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, UsbKey.KeyCode);\r
+    ASSERT (KeyDescriptor != NULL);\r
+\r
     if (!UsbKey.Down) {\r
       //\r
       // Key is released.\r
@@ -1546,6 +1551,7 @@ USBParseKey (
   @retval EFI_INVALID_PARAMETER KeyCode is not in the range of 0x4 to 0x65.\r
   @retval EFI_INVALID_PARAMETER Translated EFI_INPUT_KEY has zero for both ScanCode and UnicodeChar.\r
   @retval EFI_NOT_READY         KeyCode represents a dead key with EFI_NS_KEY_MODIFIER\r
+  @retval EFI_DEVICE_ERROR      Keyboard layout is invalid.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1569,6 +1575,7 @@ UsbKeyCodeToEfiInputKey (
   }\r
 \r
   KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, KeyCode);\r
+  ASSERT (KeyDescriptor != NULL);\r
 \r
   if (KeyDescriptor->Modifier == EFI_NS_KEY_MODIFIER) {\r
     //\r
@@ -1587,6 +1594,13 @@ UsbKeyCodeToEfiInputKey (
     UsbKeyboardDevice->CurrentNsKey = NULL;\r
   }\r
 \r
+  //\r
+  // Make sure modifier of Key Descriptor is in the valid range according to UEFI spec.\r
+  //\r
+  if (KeyDescriptor->Modifier > EFI_FUNCTION_KEY_TWELVE_MODIFIER) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   Key->ScanCode = ModifierValueToEfiScanCodeConvertionTable[KeyDescriptor->Modifier];\r
   Key->UnicodeChar = KeyDescriptor->Unicode;\r
 \r
@@ -1640,7 +1654,7 @@ UsbKeyCodeToEfiInputKey (
     }\r
   }\r
 \r
-  if (KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_NUM_LOCK) {\r
+  if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_NUM_LOCK) != 0) {\r
     //\r
     // For key affected by NumLock, if NumLock is on and Shift is not pressed, then it means\r
     // normal key, instead of original control key. So the ScanCode should be cleaned.\r
@@ -1804,6 +1818,8 @@ InsertKeyCode (
     RemoveKeyCode (KeyboardBuffer, &UsbKey);\r
   }\r
 \r
+  ASSERT (KeyboardBuffer->BufferTail <= MAX_KEY_ALLOWED);\r
+\r
   KeyboardBuffer->Buffer[KeyboardBuffer->BufferTail].KeyCode = Key;\r
   KeyboardBuffer->Buffer[KeyboardBuffer->BufferTail].Down    = Down;\r
 \r
@@ -1835,6 +1851,8 @@ RemoveKeyCode (
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
+  ASSERT (KeyboardBuffer->BufferHead <= MAX_KEY_ALLOWED);\r
+\r
   UsbKey->KeyCode = KeyboardBuffer->Buffer[KeyboardBuffer->BufferHead].KeyCode;\r
   UsbKey->Down    = KeyboardBuffer->Buffer[KeyboardBuffer->BufferHead].Down;\r
 \r