]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbBusDxe/UsbDesc.c
MdeModulePkg: Apply uncrustify changes
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusDxe / UsbDesc.c
index 1713e3c421a3dedf81721890ec0356edc0e4c2bd..a620a670748cbe34edeec5fe99c36aad75950995 100644 (file)
@@ -2,20 +2,13 @@
 \r
     Manage Usb Descriptor List\r
 \r
-Copyright (c) 2007 - 2013, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include "UsbBus.h"\r
 \r
-\r
 /**\r
   Free the interface setting descriptor.\r
 \r
@@ -27,8 +20,8 @@ UsbFreeInterfaceDesc (
   IN USB_INTERFACE_SETTING  *Setting\r
   )\r
 {\r
-  USB_ENDPOINT_DESC       *Ep;\r
-  UINTN                   Index;\r
+  USB_ENDPOINT_DESC  *Ep;\r
+  UINTN              Index;\r
 \r
   if (Setting->Endpoints != NULL) {\r
     //\r
@@ -53,7 +46,6 @@ UsbFreeInterfaceDesc (
   FreePool (Setting);\r
 }\r
 \r
-\r
 /**\r
   Free a configuration descriptor with its interface\r
   descriptors. It may be initialized partially.\r
@@ -63,12 +55,12 @@ UsbFreeInterfaceDesc (
 **/\r
 VOID\r
 UsbFreeConfigDesc (\r
-  IN USB_CONFIG_DESC      *Config\r
+  IN USB_CONFIG_DESC  *Config\r
   )\r
 {\r
-  USB_INTERFACE_DESC      *Interface;\r
-  UINTN                   Index;\r
-  UINTN                   SetIndex;\r
+  USB_INTERFACE_DESC  *Interface;\r
+  UINTN               Index;\r
+  UINTN               SetIndex;\r
 \r
   if (Config->Interfaces != NULL) {\r
     //\r
@@ -97,10 +89,8 @@ UsbFreeConfigDesc (
   }\r
 \r
   FreePool (Config);\r
-\r
 }\r
 \r
-\r
 /**\r
   Free a device descriptor with its configurations.\r
 \r
@@ -109,10 +99,10 @@ UsbFreeConfigDesc (
 **/\r
 VOID\r
 UsbFreeDevDesc (\r
-  IN USB_DEVICE_DESC      *DevDesc\r
+  IN USB_DEVICE_DESC  *DevDesc\r
   )\r
 {\r
-  UINTN                   Index;\r
+  UINTN  Index;\r
 \r
   if (DevDesc->Configs != NULL) {\r
     for (Index = 0; Index < DevDesc->Desc.NumConfigurations; Index++) {\r
@@ -127,7 +117,6 @@ UsbFreeDevDesc (
   FreePool (DevDesc);\r
 }\r
 \r
-\r
 /**\r
   Create a descriptor.\r
 \r
@@ -141,42 +130,54 @@ UsbFreeDevDesc (
 **/\r
 VOID *\r
 UsbCreateDesc (\r
-  IN  UINT8               *DescBuf,\r
-  IN  INTN                Len,\r
-  IN  UINT8               Type,\r
-  OUT INTN                *Consumed\r
+  IN  UINT8  *DescBuf,\r
+  IN  UINTN  Len,\r
+  IN  UINT8  Type,\r
+  OUT UINTN  *Consumed\r
   )\r
 {\r
-  USB_DESC_HEAD           *Head;\r
-  INTN                    DescLen;\r
-  INTN                    CtrlLen;\r
-  INTN                    Offset;\r
-  VOID                    *Desc;\r
+  USB_DESC_HEAD  *Head;\r
+  UINTN          DescLen;\r
+  UINTN          CtrlLen;\r
+  UINTN          Offset;\r
+  VOID           *Desc;\r
 \r
   DescLen   = 0;\r
   CtrlLen   = 0;\r
   *Consumed = 0;\r
 \r
   switch (Type) {\r
-  case USB_DESC_TYPE_DEVICE:\r
-    DescLen = sizeof (EFI_USB_DEVICE_DESCRIPTOR);\r
-    CtrlLen = sizeof (USB_DEVICE_DESC);\r
-    break;\r
+    case USB_DESC_TYPE_DEVICE:\r
+      DescLen = sizeof (EFI_USB_DEVICE_DESCRIPTOR);\r
+      CtrlLen = sizeof (USB_DEVICE_DESC);\r
+      break;\r
+\r
+    case USB_DESC_TYPE_CONFIG:\r
+      DescLen = sizeof (EFI_USB_CONFIG_DESCRIPTOR);\r
+      CtrlLen = sizeof (USB_CONFIG_DESC);\r
+      break;\r
 \r
-  case USB_DESC_TYPE_CONFIG:\r
-    DescLen = sizeof (EFI_USB_CONFIG_DESCRIPTOR);\r
-    CtrlLen = sizeof (USB_CONFIG_DESC);\r
-    break;\r
+    case USB_DESC_TYPE_INTERFACE:\r
+      DescLen = sizeof (EFI_USB_INTERFACE_DESCRIPTOR);\r
+      CtrlLen = sizeof (USB_INTERFACE_SETTING);\r
+      break;\r
 \r
-  case USB_DESC_TYPE_INTERFACE:\r
-    DescLen = sizeof (EFI_USB_INTERFACE_DESCRIPTOR);\r
-    CtrlLen = sizeof (USB_INTERFACE_SETTING);\r
-    break;\r
+    case USB_DESC_TYPE_ENDPOINT:\r
+      DescLen = sizeof (EFI_USB_ENDPOINT_DESCRIPTOR);\r
+      CtrlLen = sizeof (USB_ENDPOINT_DESC);\r
+      break;\r
 \r
-  case USB_DESC_TYPE_ENDPOINT:\r
-    DescLen = sizeof (EFI_USB_ENDPOINT_DESCRIPTOR);\r
-    CtrlLen = sizeof (USB_ENDPOINT_DESC);\r
-    break;\r
+    default:\r
+      ASSERT (FALSE);\r
+      return NULL;\r
+  }\r
+\r
+  //\r
+  // Total length is too small that cannot hold the single descriptor header plus data.\r
+  //\r
+  if (Len <= sizeof (USB_DESC_HEAD)) {\r
+    DEBUG ((DEBUG_ERROR, "UsbCreateDesc: met mal-format descriptor, total length = %d!\n", Len));\r
+    return NULL;\r
   }\r
 \r
   //\r
@@ -184,32 +185,59 @@ UsbCreateDesc (
   // format. Skip the descriptor that isn't of this Type\r
   //\r
   Offset = 0;\r
-  Head   = (USB_DESC_HEAD*)DescBuf;\r
+  Head   = (USB_DESC_HEAD *)DescBuf;\r
+  while (Offset < Len - sizeof (USB_DESC_HEAD)) {\r
+    //\r
+    // Above condition make sure Head->Len and Head->Type are safe to access\r
+    //\r
+    Head = (USB_DESC_HEAD *)&DescBuf[Offset];\r
+\r
+    if (Head->Len == 0) {\r
+      DEBUG ((DEBUG_ERROR, "UsbCreateDesc: met mal-format descriptor, Head->Len = 0!\n"));\r
+      return NULL;\r
+    }\r
+\r
+    //\r
+    // Make sure no overflow when adding Head->Len to Offset.\r
+    //\r
+    if (Head->Len > MAX_UINTN - Offset) {\r
+      DEBUG ((DEBUG_ERROR, "UsbCreateDesc: met mal-format descriptor, Head->Len = %d!\n", Head->Len));\r
+      return NULL;\r
+    }\r
 \r
-  while ((Offset < Len) && (Head->Type != Type)) {\r
     Offset += Head->Len;\r
-    Head    = (USB_DESC_HEAD*)(DescBuf + Offset);\r
+\r
+    if (Head->Type == Type) {\r
+      break;\r
+    }\r
   }\r
 \r
-  if ((Len <= Offset)      || (Len < Offset + DescLen) ||\r
-      (Head->Type != Type) || (Head->Len != DescLen)) {\r
-    DEBUG (( EFI_D_ERROR, "UsbCreateDesc: met mal-format descriptor\n"));\r
+  //\r
+  // Head->Len is invalid resulting data beyond boundary, or\r
+  // Descriptor cannot be found: No such type.\r
+  //\r
+  if (Len < Offset) {\r
+    DEBUG ((DEBUG_ERROR, "UsbCreateDesc: met mal-format descriptor, Offset/Len = %d/%d!\n", Offset, Len));\r
+    return NULL;\r
+  }\r
+\r
+  if ((Head->Type != Type) || (Head->Len < DescLen)) {\r
+    DEBUG ((DEBUG_ERROR, "UsbCreateDesc: descriptor cannot be found, Header(T/L) = %d/%d!\n", Head->Type, Head->Len));\r
     return NULL;\r
   }\r
 \r
-  Desc = AllocateZeroPool ((UINTN) CtrlLen);\r
+  Desc = AllocateZeroPool ((UINTN)CtrlLen);\r
   if (Desc == NULL) {\r
     return NULL;\r
   }\r
 \r
-  CopyMem (Desc, Head, (UINTN) DescLen);\r
+  CopyMem (Desc, Head, (UINTN)DescLen);\r
 \r
-  *Consumed = Offset + Head->Len;\r
+  *Consumed = Offset;\r
 \r
   return Desc;\r
 }\r
 \r
-\r
 /**\r
   Parse an interface descriptor and its endpoints.\r
 \r
@@ -222,23 +250,23 @@ UsbCreateDesc (
 **/\r
 USB_INTERFACE_SETTING *\r
 UsbParseInterfaceDesc (\r
-  IN  UINT8               *DescBuf,\r
-  IN  INTN                Len,\r
-  OUT INTN                *Consumed\r
+  IN  UINT8  *DescBuf,\r
+  IN  UINTN  Len,\r
+  OUT UINTN  *Consumed\r
   )\r
 {\r
-  USB_INTERFACE_SETTING   *Setting;\r
-  USB_ENDPOINT_DESC       *Ep;\r
-  UINTN                   Index;\r
-  UINTN                   NumEp;\r
-  INTN                    Used;\r
-  INTN                    Offset;\r
+  USB_INTERFACE_SETTING  *Setting;\r
+  USB_ENDPOINT_DESC      *Ep;\r
+  UINTN                  Index;\r
+  UINTN                  NumEp;\r
+  UINTN                  Used;\r
+  UINTN                  Offset;\r
 \r
   *Consumed = 0;\r
   Setting   = UsbCreateDesc (DescBuf, Len, USB_DESC_TYPE_INTERFACE, &Used);\r
 \r
   if (Setting == NULL) {\r
-    DEBUG (( EFI_D_ERROR, "UsbParseInterfaceDesc: failed to create interface descriptor\n"));\r
+    DEBUG ((DEBUG_ERROR, "UsbParseInterfaceDesc: failed to create interface descriptor\n"));\r
     return NULL;\r
   }\r
 \r
@@ -247,16 +275,21 @@ UsbParseInterfaceDesc (
   //\r
   // Create an array to hold the interface's endpoints\r
   //\r
-  NumEp  = Setting->Desc.NumEndpoints;\r
+  NumEp = Setting->Desc.NumEndpoints;\r
 \r
-  DEBUG (( EFI_D_INFO, "UsbParseInterfaceDesc: interface %d(setting %d) has %d endpoints\n",\r
-              Setting->Desc.InterfaceNumber, Setting->Desc.AlternateSetting, (UINT32)NumEp));\r
+  DEBUG ((\r
+    DEBUG_INFO,\r
+    "UsbParseInterfaceDesc: interface %d(setting %d) has %d endpoints\n",\r
+    Setting->Desc.InterfaceNumber,\r
+    Setting->Desc.AlternateSetting,\r
+    (UINT32)NumEp\r
+    ));\r
 \r
   if (NumEp == 0) {\r
     goto ON_EXIT;\r
   }\r
 \r
-  Setting->Endpoints  = AllocateZeroPool (sizeof (USB_ENDPOINT_DESC *) * NumEp);\r
+  Setting->Endpoints = AllocateZeroPool (sizeof (USB_ENDPOINT_DESC *) * NumEp);\r
 \r
   if (Setting->Endpoints == NULL) {\r
     goto ON_ERROR;\r
@@ -265,19 +298,18 @@ UsbParseInterfaceDesc (
   //\r
   // Create the endpoints for this interface\r
   //\r
-  for (Index = 0; Index < NumEp; Index++) {\r
+  for (Index = 0; (Index < NumEp) && (Offset < Len); Index++) {\r
     Ep = UsbCreateDesc (DescBuf + Offset, Len - Offset, USB_DESC_TYPE_ENDPOINT, &Used);\r
 \r
     if (Ep == NULL) {\r
-      DEBUG (( EFI_D_ERROR, "UsbParseInterfaceDesc: failed to create endpoint(index %d)\n", (UINT32)Index));\r
+      DEBUG ((DEBUG_ERROR, "UsbParseInterfaceDesc: failed to create endpoint(index %d)\n", (UINT32)Index));\r
       goto ON_ERROR;\r
     }\r
 \r
-    Setting->Endpoints[Index]  = Ep;\r
-    Offset                    += Used;\r
+    Setting->Endpoints[Index] = Ep;\r
+    Offset                   += Used;\r
   }\r
 \r
-\r
 ON_EXIT:\r
   *Consumed = Offset;\r
   return Setting;\r
@@ -287,7 +319,6 @@ ON_ERROR:
   return NULL;\r
 }\r
 \r
-\r
 /**\r
   Parse the configuration descriptor and its interfaces.\r
 \r
@@ -299,16 +330,16 @@ ON_ERROR:
 **/\r
 USB_CONFIG_DESC *\r
 UsbParseConfigDesc (\r
-  IN UINT8                *DescBuf,\r
-  IN INTN                 Len\r
+  IN UINT8  *DescBuf,\r
+  IN UINTN  Len\r
   )\r
 {\r
-  USB_CONFIG_DESC         *Config;\r
-  USB_INTERFACE_SETTING   *Setting;\r
-  USB_INTERFACE_DESC      *Interface;\r
-  UINTN                   Index;\r
-  UINTN                   NumIf;\r
-  INTN                    Consumed;\r
+  USB_CONFIG_DESC        *Config;\r
+  USB_INTERFACE_SETTING  *Setting;\r
+  USB_INTERFACE_DESC     *Interface;\r
+  UINTN                  Index;\r
+  UINTN                  NumIf;\r
+  UINTN                  Consumed;\r
 \r
   ASSERT (DescBuf != NULL);\r
 \r
@@ -321,15 +352,19 @@ UsbParseConfigDesc (
   //\r
   // Initialize an array of setting for the configuration's interfaces.\r
   //\r
-  NumIf               = Config->Desc.NumInterfaces;\r
-  Config->Interfaces  = AllocateZeroPool (sizeof (USB_INTERFACE_DESC *) * NumIf);\r
+  NumIf              = Config->Desc.NumInterfaces;\r
+  Config->Interfaces = AllocateZeroPool (sizeof (USB_INTERFACE_DESC *) * NumIf);\r
 \r
   if (Config->Interfaces == NULL) {\r
     goto ON_ERROR;\r
   }\r
 \r
-  DEBUG (( EFI_D_INFO, "UsbParseConfigDesc: config %d has %d interfaces\n",\r
-                Config->Desc.ConfigurationValue, (UINT32)NumIf));\r
+  DEBUG ((\r
+    DEBUG_INFO,\r
+    "UsbParseConfigDesc: config %d has %d interfaces\n",\r
+    Config->Desc.ConfigurationValue,\r
+    (UINT32)NumIf\r
+    ));\r
 \r
   for (Index = 0; Index < NumIf; Index++) {\r
     Interface = AllocateZeroPool (sizeof (USB_INTERFACE_DESC));\r
@@ -353,18 +388,17 @@ UsbParseConfigDesc (
   Len     -= Consumed;\r
 \r
   //\r
-  // Make allowances for devices that return extra data at the \r
+  // Make allowances for devices that return extra data at the\r
   // end of their config descriptors\r
   //\r
   while (Len >= sizeof (EFI_USB_INTERFACE_DESCRIPTOR)) {\r
     Setting = UsbParseInterfaceDesc (DescBuf, Len, &Consumed);\r
 \r
     if (Setting == NULL) {\r
-      DEBUG (( EFI_D_ERROR, "UsbParseConfigDesc: warning: failed to get interface setting, stop parsing now.\n"));\r
+      DEBUG ((DEBUG_ERROR, "UsbParseConfigDesc: warning: failed to get interface setting, stop parsing now.\n"));\r
       break;\r
-\r
     } else if (Setting->Desc.InterfaceNumber >= NumIf) {\r
-      DEBUG (( EFI_D_ERROR, "UsbParseConfigDesc: mal-formated interface descriptor\n"));\r
+      DEBUG ((DEBUG_ERROR, "UsbParseConfigDesc: malformatted interface descriptor\n"));\r
 \r
       UsbFreeInterfaceDesc (Setting);\r
       goto ON_ERROR;\r
@@ -393,7 +427,6 @@ ON_ERROR:
   return NULL;\r
 }\r
 \r
-\r
 /**\r
   USB standard control transfer support routine. This\r
   function is used by USB device. It is possible that\r
@@ -416,15 +449,15 @@ ON_ERROR:
 **/\r
 EFI_STATUS\r
 UsbCtrlRequest (\r
-  IN USB_DEVICE             *UsbDev,\r
-  IN EFI_USB_DATA_DIRECTION Direction,\r
-  IN UINTN                  Type,\r
-  IN UINTN                  Target,\r
-  IN UINTN                  Request,\r
-  IN UINT16                 Value,\r
-  IN UINT16                 Index,\r
-  IN OUT VOID               *Buf,\r
-  IN UINTN                  Length\r
+  IN USB_DEVICE              *UsbDev,\r
+  IN EFI_USB_DATA_DIRECTION  Direction,\r
+  IN UINTN                   Type,\r
+  IN UINTN                   Target,\r
+  IN UINTN                   Request,\r
+  IN UINT16                  Value,\r
+  IN UINT16                  Index,\r
+  IN OUT VOID                *Buf,\r
+  IN UINTN                   Length\r
   )\r
 {\r
   EFI_USB_DEVICE_REQUEST  DevReq;\r
@@ -434,13 +467,13 @@ UsbCtrlRequest (
 \r
   ASSERT ((UsbDev != NULL) && (UsbDev->Bus != NULL));\r
 \r
-  DevReq.RequestType  = USB_REQUEST_TYPE (Direction, Type, Target);\r
-  DevReq.Request      = (UINT8) Request;\r
-  DevReq.Value        = Value;\r
-  DevReq.Index        = Index;\r
-  DevReq.Length       = (UINT16) Length;\r
+  DevReq.RequestType = USB_REQUEST_TYPE (Direction, Type, Target);\r
+  DevReq.Request     = (UINT8)Request;\r
+  DevReq.Value       = Value;\r
+  DevReq.Index       = Index;\r
+  DevReq.Length      = (UINT16)Length;\r
 \r
-  Len                 = Length;\r
+  Len    = Length;\r
   Status = UsbHcControlTransfer (\r
              UsbDev->Bus,\r
              UsbDev->Address,\r
@@ -458,7 +491,6 @@ UsbCtrlRequest (
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Get the standard descriptors.\r
 \r
@@ -476,15 +508,15 @@ UsbCtrlRequest (
 **/\r
 EFI_STATUS\r
 UsbCtrlGetDesc (\r
-  IN  USB_DEVICE          *UsbDev,\r
-  IN  UINTN               DescType,\r
-  IN  UINTN               DescIndex,\r
-  IN  UINT16              LangId,\r
-  OUT VOID                *Buf,\r
-  IN  UINTN               Length\r
+  IN  USB_DEVICE  *UsbDev,\r
+  IN  UINTN       DescType,\r
+  IN  UINTN       DescIndex,\r
+  IN  UINT16      LangId,\r
+  OUT VOID        *Buf,\r
+  IN  UINTN       Length\r
   )\r
 {\r
-  EFI_STATUS              Status;\r
+  EFI_STATUS  Status;\r
 \r
   Status = UsbCtrlRequest (\r
              UsbDev,\r
@@ -492,7 +524,7 @@ UsbCtrlGetDesc (
              USB_REQ_TYPE_STANDARD,\r
              USB_TARGET_DEVICE,\r
              USB_REQ_GET_DESCRIPTOR,\r
-             (UINT16) ((DescType << 8) | DescIndex),\r
+             (UINT16)((DescType << 8) | DescIndex),\r
              LangId,\r
              Buf,\r
              Length\r
@@ -501,7 +533,6 @@ UsbCtrlGetDesc (
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Return the max packet size for endpoint zero. This function\r
   is the first function called to get descriptors during bus\r
@@ -515,13 +546,12 @@ UsbCtrlGetDesc (
 **/\r
 EFI_STATUS\r
 UsbGetMaxPacketSize0 (\r
-  IN USB_DEVICE           *UsbDev\r
+  IN USB_DEVICE  *UsbDev\r
   )\r
 {\r
-  EFI_USB_DEVICE_DESCRIPTOR DevDesc;\r
-  EFI_STATUS                Status;\r
-  UINTN                     Index;\r
-\r
+  EFI_USB_DEVICE_DESCRIPTOR  DevDesc;\r
+  EFI_STATUS                 Status;\r
+  UINTN                      Index;\r
 \r
   //\r
   // Get the first 8 bytes of the device descriptor which contains\r
@@ -531,10 +561,11 @@ UsbGetMaxPacketSize0 (
     Status = UsbCtrlGetDesc (UsbDev, USB_DESC_TYPE_DEVICE, 0, 0, &DevDesc, 8);\r
 \r
     if (!EFI_ERROR (Status)) {\r
-      if ((DevDesc.BcdUSB == 0x0300) && (DevDesc.MaxPacketSize0 == 9)) {\r
+      if ((DevDesc.BcdUSB >= 0x0300) && (DevDesc.MaxPacketSize0 == 9)) {\r
         UsbDev->MaxPacket0 = 1 << 9;\r
         return EFI_SUCCESS;\r
       }\r
+\r
       UsbDev->MaxPacket0 = DevDesc.MaxPacketSize0;\r
       return EFI_SUCCESS;\r
     }\r
@@ -545,7 +576,6 @@ UsbGetMaxPacketSize0 (
   return EFI_DEVICE_ERROR;\r
 }\r
 \r
-\r
 /**\r
   Get the device descriptor for the device.\r
 \r
@@ -557,11 +587,11 @@ UsbGetMaxPacketSize0 (
 **/\r
 EFI_STATUS\r
 UsbGetDevDesc (\r
-  IN USB_DEVICE           *UsbDev\r
+  IN USB_DEVICE  *UsbDev\r
   )\r
 {\r
-  USB_DEVICE_DESC         *DevDesc;\r
-  EFI_STATUS              Status;\r
+  USB_DEVICE_DESC  *DevDesc;\r
+  EFI_STATUS       Status;\r
 \r
   DevDesc = AllocateZeroPool (sizeof (USB_DEVICE_DESC));\r
 \r
@@ -569,14 +599,14 @@ UsbGetDevDesc (
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  Status  = UsbCtrlGetDesc (\r
-              UsbDev,\r
-              USB_DESC_TYPE_DEVICE,\r
-              0,\r
-              0,\r
-              DevDesc,\r
-              sizeof (EFI_USB_DEVICE_DESCRIPTOR)\r
-              );\r
+  Status = UsbCtrlGetDesc (\r
+             UsbDev,\r
+             USB_DESC_TYPE_DEVICE,\r
+             0,\r
+             0,\r
+             DevDesc,\r
+             sizeof (EFI_USB_DEVICE_DESCRIPTOR)\r
+             );\r
 \r
   if (EFI_ERROR (Status)) {\r
     gBS->FreePool (DevDesc);\r
@@ -587,7 +617,6 @@ UsbGetDevDesc (
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Retrieve the indexed string for the language. It requires two\r
   steps to get a string, first to get the string's length. Then\r
@@ -602,21 +631,28 @@ UsbGetDevDesc (
 **/\r
 EFI_USB_STRING_DESCRIPTOR *\r
 UsbGetOneString (\r
-  IN     USB_DEVICE       *UsbDev,\r
-  IN     UINT8            Index,\r
-  IN     UINT16           LangId\r
+  IN     USB_DEVICE  *UsbDev,\r
+  IN     UINT8       Index,\r
+  IN     UINT16      LangId\r
   )\r
 {\r
-  EFI_USB_STRING_DESCRIPTOR Desc;\r
-  EFI_STATUS                Status;\r
-  UINT8                     *Buf;\r
+  EFI_USB_STRING_DESCRIPTOR  Desc;\r
+  EFI_STATUS                 Status;\r
+  UINT8                      *Buf;\r
 \r
   //\r
   // First get two bytes which contains the string length.\r
   //\r
   Status = UsbCtrlGetDesc (UsbDev, USB_DESC_TYPE_STRING, Index, LangId, &Desc, 2);\r
 \r
-  if (EFI_ERROR (Status)) {\r
+  //\r
+  // Reject if Length even cannot cover itself, or odd because Unicode string byte length should be even.\r
+  //\r
+  if (EFI_ERROR (Status) ||\r
+      (Desc.Length < OFFSET_OF (EFI_USB_STRING_DESCRIPTOR, Length) + sizeof (Desc.Length)) ||\r
+      (Desc.Length % 2 != 0)\r
+      )\r
+  {\r
     return NULL;\r
   }\r
 \r
@@ -640,10 +676,9 @@ UsbGetOneString (
     return NULL;\r
   }\r
 \r
-  return (EFI_USB_STRING_DESCRIPTOR *) Buf;\r
+  return (EFI_USB_STRING_DESCRIPTOR *)Buf;\r
 }\r
 \r
-\r
 /**\r
   Build the language ID table for string descriptors.\r
 \r
@@ -654,14 +689,14 @@ UsbGetOneString (
 **/\r
 EFI_STATUS\r
 UsbBuildLangTable (\r
-  IN USB_DEVICE           *UsbDev\r
+  IN USB_DEVICE  *UsbDev\r
   )\r
 {\r
-  EFI_USB_STRING_DESCRIPTOR *Desc;\r
-  EFI_STATUS                Status;\r
-  UINTN                     Index;\r
-  UINTN                     Max;\r
-  UINT16                    *Point;\r
+  EFI_USB_STRING_DESCRIPTOR  *Desc;\r
+  EFI_STATUS                 Status;\r
+  UINTN                      Index;\r
+  UINTN                      Max;\r
+  UINT16                     *Point;\r
 \r
   //\r
   // The string of language ID zero returns the supported languages\r
@@ -679,9 +714,9 @@ UsbBuildLangTable (
 \r
   Status = EFI_SUCCESS;\r
 \r
-  Max   = (Desc->Length - 2) / 2;\r
-  Max   = MIN(Max, USB_MAX_LANG_ID);\r
-  \r
+  Max = (Desc->Length - 2) / 2;\r
+  Max = MIN (Max, USB_MAX_LANG_ID);\r
+\r
   Point = Desc->String;\r
   for (Index = 0; Index < Max; Index++) {\r
     UsbDev->LangId[Index] = *Point;\r
@@ -695,7 +730,6 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Retrieve the indexed configure for the device. USB device\r
   returns the configuration together with the interfaces for\r
@@ -710,13 +744,13 @@ ON_EXIT:
 **/\r
 EFI_USB_CONFIG_DESCRIPTOR *\r
 UsbGetOneConfig (\r
-  IN USB_DEVICE           *UsbDev,\r
-  IN UINT8                Index\r
+  IN USB_DEVICE  *UsbDev,\r
+  IN UINT8       Index\r
   )\r
 {\r
-  EFI_USB_CONFIG_DESCRIPTOR Desc;\r
-  EFI_STATUS                Status;\r
-  VOID                      *Buf;\r
+  EFI_USB_CONFIG_DESCRIPTOR  Desc;\r
+  EFI_STATUS                 Status;\r
+  VOID                       *Buf;\r
 \r
   //\r
   // First get four bytes which contains the total length\r
@@ -725,13 +759,24 @@ UsbGetOneConfig (
   Status = UsbCtrlGetDesc (UsbDev, USB_DESC_TYPE_CONFIG, Index, 0, &Desc, 8);\r
 \r
   if (EFI_ERROR (Status)) {\r
-    DEBUG (( EFI_D_ERROR, "UsbGetOneConfig: failed to get descript length(%d) %r\n",\r
-                Desc.TotalLength, Status));\r
+    DEBUG ((\r
+      DEBUG_ERROR,\r
+      "UsbGetOneConfig: failed to get descript length(%d) %r\n",\r
+      Desc.TotalLength,\r
+      Status\r
+      ));\r
 \r
     return NULL;\r
   }\r
 \r
-  DEBUG (( EFI_D_INFO, "UsbGetOneConfig: total length is %d\n", Desc.TotalLength));\r
+  DEBUG ((DEBUG_INFO, "UsbGetOneConfig: total length is %d\n", Desc.TotalLength));\r
+\r
+  //\r
+  // Reject if TotalLength even cannot cover itself.\r
+  //\r
+  if (Desc.TotalLength < OFFSET_OF (EFI_USB_CONFIG_DESCRIPTOR, TotalLength) + sizeof (Desc.TotalLength)) {\r
+    return NULL;\r
+  }\r
 \r
   Buf = AllocateZeroPool (Desc.TotalLength);\r
 \r
@@ -742,7 +787,7 @@ UsbGetOneConfig (
   Status = UsbCtrlGetDesc (UsbDev, USB_DESC_TYPE_CONFIG, Index, 0, Buf, Desc.TotalLength);\r
 \r
   if (EFI_ERROR (Status)) {\r
-    DEBUG (( EFI_D_ERROR, "UsbGetOneConfig: failed to get full descript %r\n", Status));\r
+    DEBUG ((DEBUG_ERROR, "UsbGetOneConfig: failed to get full descript %r\n", Status));\r
 \r
     FreePool (Buf);\r
     return NULL;\r
@@ -751,7 +796,6 @@ UsbGetOneConfig (
   return Buf;\r
 }\r
 \r
-\r
 /**\r
   Build the whole array of descriptors. This function must\r
   be called after UsbGetMaxPacketSize0 returns the max packet\r
@@ -765,15 +809,15 @@ UsbGetOneConfig (
 **/\r
 EFI_STATUS\r
 UsbBuildDescTable (\r
-  IN USB_DEVICE           *UsbDev\r
+  IN USB_DEVICE  *UsbDev\r
   )\r
 {\r
-  EFI_USB_CONFIG_DESCRIPTOR *Config;\r
-  USB_DEVICE_DESC           *DevDesc;\r
-  USB_CONFIG_DESC           *ConfigDesc;\r
-  UINT8                     NumConfig;\r
-  EFI_STATUS                Status;\r
-  UINT8                     Index;\r
+  EFI_USB_CONFIG_DESCRIPTOR  *Config;\r
+  USB_DEVICE_DESC            *DevDesc;\r
+  USB_CONFIG_DESC            *ConfigDesc;\r
+  UINT8                      NumConfig;\r
+  EFI_STATUS                 Status;\r
+  UINT8                      Index;\r
 \r
   //\r
   // Get the device descriptor, then allocate the configure\r
@@ -782,7 +826,7 @@ UsbBuildDescTable (
   Status = UsbGetDevDesc (UsbDev);\r
 \r
   if (EFI_ERROR (Status)) {\r
-    DEBUG (( EFI_D_ERROR, "UsbBuildDescTable: failed to get device descriptor - %r\n", Status));\r
+    DEBUG ((DEBUG_ERROR, "UsbBuildDescTable: failed to get device descriptor - %r\n", Status));\r
     return Status;\r
   }\r
 \r
@@ -797,7 +841,7 @@ UsbBuildDescTable (
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  DEBUG (( EFI_D_INFO, "UsbBuildDescTable: device has %d configures\n", NumConfig));\r
+  DEBUG ((DEBUG_INFO, "UsbBuildDescTable: device has %d configures\n", NumConfig));\r
 \r
   //\r
   // Read each configurations, then parse them\r
@@ -806,7 +850,7 @@ UsbBuildDescTable (
     Config = UsbGetOneConfig (UsbDev, Index);\r
 \r
     if (Config == NULL) {\r
-      DEBUG (( EFI_D_ERROR, "UsbBuildDescTable: failed to get configure (index %d)\n", Index));\r
+      DEBUG ((DEBUG_ERROR, "UsbBuildDescTable: failed to get configure (index %d)\n", Index));\r
 \r
       //\r
       // If we can get the default descriptor, it is likely that the\r
@@ -819,12 +863,12 @@ UsbBuildDescTable (
       break;\r
     }\r
 \r
-    ConfigDesc = UsbParseConfigDesc ((UINT8 *) Config, Config->TotalLength);\r
+    ConfigDesc = UsbParseConfigDesc ((UINT8 *)Config, Config->TotalLength);\r
 \r
     FreePool (Config);\r
 \r
     if (ConfigDesc == NULL) {\r
-      DEBUG (( EFI_D_ERROR, "UsbBuildDescTable: failed to parse configure (index %d)\n", Index));\r
+      DEBUG ((DEBUG_ERROR, "UsbBuildDescTable: failed to parse configure (index %d)\n", Index));\r
 \r
       //\r
       // If we can get the default descriptor, it is likely that the\r
@@ -847,13 +891,12 @@ UsbBuildDescTable (
   Status = UsbBuildLangTable (UsbDev);\r
 \r
   if (EFI_ERROR (Status)) {\r
-    DEBUG (( EFI_D_ERROR, "UsbBuildDescTable: get language ID table %r\n", Status));\r
+    DEBUG ((DEBUG_INFO, "UsbBuildDescTable: get language ID table %r\n", Status));\r
   }\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Set the device's address.\r
 \r
@@ -866,11 +909,11 @@ UsbBuildDescTable (
 **/\r
 EFI_STATUS\r
 UsbSetAddress (\r
-  IN USB_DEVICE           *UsbDev,\r
-  IN UINT8                Address\r
+  IN USB_DEVICE  *UsbDev,\r
+  IN UINT8       Address\r
   )\r
 {\r
-  EFI_STATUS              Status;\r
+  EFI_STATUS  Status;\r
 \r
   Status = UsbCtrlRequest (\r
              UsbDev,\r
@@ -887,7 +930,6 @@ UsbSetAddress (
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Set the device's configuration. This function changes\r
   the device's internal state. UsbSelectConfig changes\r
@@ -902,11 +944,11 @@ UsbSetAddress (
 **/\r
 EFI_STATUS\r
 UsbSetConfig (\r
-  IN USB_DEVICE           *UsbDev,\r
-  IN UINT8                ConfigIndex\r
+  IN USB_DEVICE  *UsbDev,\r
+  IN UINT8       ConfigIndex\r
   )\r
 {\r
-  EFI_STATUS              Status;\r
+  EFI_STATUS  Status;\r
 \r
   Status = UsbCtrlRequest (\r
              UsbDev,\r
@@ -918,12 +960,11 @@ UsbSetConfig (
              0,\r
              NULL,\r
              0\r
-            );\r
+             );\r
 \r
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Usb UsbIo interface to clear the feature. This is should\r
   only be used by HUB which is considered a device driver\r
@@ -940,21 +981,21 @@ UsbSetConfig (
 **/\r
 EFI_STATUS\r
 UsbIoClearFeature (\r
-  IN  EFI_USB_IO_PROTOCOL *UsbIo,\r
-  IN  UINTN               Target,\r
-  IN  UINT16              Feature,\r
-  IN  UINT16              Index\r
+  IN  EFI_USB_IO_PROTOCOL  *UsbIo,\r
+  IN  UINTN                Target,\r
+  IN  UINT16               Feature,\r
+  IN  UINT16               Index\r
   )\r
 {\r
   EFI_USB_DEVICE_REQUEST  DevReq;\r
   UINT32                  UsbResult;\r
   EFI_STATUS              Status;\r
 \r
-  DevReq.RequestType  = USB_REQUEST_TYPE (EfiUsbNoData, USB_REQ_TYPE_STANDARD, Target);\r
-  DevReq.Request      = USB_REQ_CLEAR_FEATURE;\r
-  DevReq.Value        = Feature;\r
-  DevReq.Index        = Index;\r
-  DevReq.Length       = 0;\r
+  DevReq.RequestType = USB_REQUEST_TYPE (EfiUsbNoData, USB_REQ_TYPE_STANDARD, Target);\r
+  DevReq.Request     = USB_REQ_CLEAR_FEATURE;\r
+  DevReq.Value       = Feature;\r
+  DevReq.Index       = Index;\r
+  DevReq.Length      = 0;\r
 \r
   Status = UsbIo->UsbControlTransfer (\r
                     UsbIo,\r