]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbBusDxe/UsbBus.c
MdeModulePkg/UsbBusDxe: Add UsbControlTransfer() error check
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusDxe / UsbBus.c
index 873cf2519afb501c886810547a8af3f2f513629b..7c58fe8d36a26a67dea4d3f9712f17fd88b27b7b 100644 (file)
@@ -2,7 +2,7 @@
 \r
     Usb Bus Driver Binding and Bus IO Protocol.\r
 \r
-Copyright (c) 2004 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2017, 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
@@ -76,6 +76,7 @@ UsbIoControlTransfer (
   USB_ENDPOINT_DESC       *EpDesc;\r
   EFI_TPL                 OldTpl;\r
   EFI_STATUS              Status;\r
+  UINTN                   RequestedDataLength;\r
 \r
   if (UsbStatus == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -86,6 +87,7 @@ UsbIoControlTransfer (
   UsbIf  = USB_INTERFACE_FROM_USBIO (This);\r
   Dev    = UsbIf->Device;\r
 \r
+  RequestedDataLength = DataLength;\r
   Status = UsbHcControlTransfer (\r
              Dev->Bus,\r
              Dev->Address,\r
@@ -99,6 +101,18 @@ UsbIoControlTransfer (
              &Dev->Translator,\r
              UsbStatus\r
              );\r
+  //\r
+  // If the request completed sucessfully and the Direction of the request is\r
+  // EfiUsbDataIn or EfiUsbDataOut, then make sure the actual number of bytes\r
+  // transfered is the same as the number of bytes requested.  If a different\r
+  // number of bytes were transfered, then return EFI_DEVICE_ERROR.\r
+  //\r
+  if (!EFI_ERROR (Status)) {\r
+    if (Direction != EfiUsbNoData && DataLength != RequestedDataLength) {\r
+      Status = EFI_DEVICE_ERROR;\r
+      goto ON_EXIT;\r
+    }\r
+  }\r
 \r
   if (EFI_ERROR (Status) || (*UsbStatus != EFI_USB_NOERROR)) {\r
     //\r
@@ -813,6 +827,7 @@ UsbIoPortReset (
   USB_DEVICE              *Dev;\r
   EFI_TPL                 OldTpl;\r
   EFI_STATUS              Status;\r
+  UINT8                   DevAddress;\r
 \r
   OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
 \r
@@ -834,12 +849,17 @@ UsbIoPortReset (
     goto ON_EXIT;\r
   }\r
 \r
+  HubIf->HubApi->ClearPortChange (HubIf, Dev->ParentPort);\r
+\r
   //\r
   // Reset the device to its current address. The device now has an address\r
   // of ZERO after port reset, so need to set Dev->Address to the device again for\r
   // host to communicate with it.\r
   //\r
-  Status  = UsbSetAddress (Dev, Dev->Address);\r
+  DevAddress   = Dev->Address;\r
+  Dev->Address = 0;\r
+  Status  = UsbSetAddress (Dev, DevAddress);\r
+  Dev->Address = DevAddress;\r
 \r
   gBS->Stall (USB_SET_DEVICE_ADDRESS_STALL);\r
   \r
@@ -964,14 +984,12 @@ UsbBusBuildProtocol (
     // The EFI_USB2_HC_PROTOCOL is produced for XHCI support.\r
     // Then its max supported devices are 256. Otherwise it's 128.\r
     //\r
+    ASSERT (UsbBus->Usb2Hc != NULL);\r
     if (UsbBus->Usb2Hc->MajorRevision == 0x3) {\r
       UsbBus->MaxDevices = 256;\r
     }\r
   }\r
 \r
-  UsbHcReset (UsbBus, EFI_USB_HC_RESET_GLOBAL);\r
-  UsbHcSetState (UsbBus, EfiUsbHcStateOperational);\r
-\r
   //\r
   // Install an EFI_USB_BUS_PROTOCOL to host controller to identify it.\r
   //\r
@@ -1395,6 +1413,7 @@ UsbBusControllerDriverStop (
   EFI_TPL               OldTpl;\r
   UINTN                 Index;\r
   EFI_STATUS            Status;\r
+  EFI_STATUS            ReturnStatus;\r
 \r
   Status  = EFI_SUCCESS;\r
 \r
@@ -1404,6 +1423,7 @@ UsbBusControllerDriverStop (
     //\r
     OldTpl   = gBS->RaiseTPL (TPL_CALLBACK);\r
 \r
+    ReturnStatus = EFI_SUCCESS;\r
     for (Index = 0; Index < NumberOfChildren; Index++) {\r
       Status = gBS->OpenProtocol (\r
                       ChildHandleBuffer[Index],\r
@@ -1427,11 +1447,11 @@ UsbBusControllerDriverStop (
       UsbIf   = USB_INTERFACE_FROM_USBIO (UsbIo);\r
       UsbDev  = UsbIf->Device;\r
 \r
-      UsbRemoveDevice (UsbDev);\r
+      ReturnStatus = UsbRemoveDevice (UsbDev);\r
     }\r
 \r
     gBS->RestoreTPL (OldTpl);\r
-    return EFI_SUCCESS;\r
+    return ReturnStatus;\r
   }\r
 \r
   DEBUG (( EFI_D_INFO, "UsbBusStop: usb bus stopped on %p\n", Controller));\r
@@ -1464,53 +1484,60 @@ UsbBusControllerDriverStop (
   RootHub = Bus->Devices[0];\r
   RootIf  = RootHub->Interfaces[0];\r
 \r
-  mUsbRootHubApi.Release (RootIf);\r
-\r
   ASSERT (Bus->MaxDevices <= 256);\r
+  ReturnStatus = EFI_SUCCESS;\r
   for (Index = 1; Index < Bus->MaxDevices; Index++) {\r
     if (Bus->Devices[Index] != NULL) {\r
-      UsbRemoveDevice (Bus->Devices[Index]);\r
+      Status = UsbRemoveDevice (Bus->Devices[Index]);\r
+      if (EFI_ERROR (Status)) {\r
+        ReturnStatus = Status;\r
+      }\r
     }\r
   }\r
 \r
   gBS->RestoreTPL (OldTpl);\r
 \r
-  gBS->FreePool   (RootIf);\r
-  gBS->FreePool   (RootHub);\r
-  Status = UsbBusFreeUsbDPList (&Bus->WantedUsbIoDPList);\r
-  ASSERT (!EFI_ERROR (Status));\r
+  if (!EFI_ERROR (ReturnStatus)) {\r
+    mUsbRootHubApi.Release (RootIf);\r
+    gBS->FreePool   (RootIf);\r
+    gBS->FreePool   (RootHub);\r
 \r
-  //\r
-  // Uninstall the bus identifier and close USB_HC/USB2_HC protocols\r
-  //\r
-  gBS->UninstallProtocolInterface (Controller, &gEfiCallerIdGuid, &Bus->BusId);\r
+    Status = UsbBusFreeUsbDPList (&Bus->WantedUsbIoDPList);\r
+    ASSERT (!EFI_ERROR (Status));\r
 \r
-  if (Bus->Usb2Hc != NULL) {\r
-    gBS->CloseProtocol (\r
-           Controller,\r
-           &gEfiUsb2HcProtocolGuid,\r
-           This->DriverBindingHandle,\r
-           Controller\r
-           );\r
-  }\r
+    //\r
+    // Uninstall the bus identifier and close USB_HC/USB2_HC protocols\r
+    //\r
+    gBS->UninstallProtocolInterface (Controller, &gEfiCallerIdGuid, &Bus->BusId);\r
 \r
-  if (Bus->UsbHc != NULL) {\r
-    gBS->CloseProtocol (\r
-           Controller,\r
-           &gEfiUsbHcProtocolGuid,\r
-           This->DriverBindingHandle,\r
-           Controller\r
-           );\r
-  }\r
+    if (Bus->Usb2Hc != NULL) {\r
+      Status = gBS->CloseProtocol (\r
+                      Controller,\r
+                      &gEfiUsb2HcProtocolGuid,\r
+                      This->DriverBindingHandle,\r
+                      Controller\r
+                      );\r
+    }\r
 \r
-  gBS->CloseProtocol (\r
-         Controller,\r
-         &gEfiDevicePathProtocolGuid,\r
-         This->DriverBindingHandle,\r
-         Controller\r
-         );\r
+    if (Bus->UsbHc != NULL) {\r
+      Status = gBS->CloseProtocol (\r
+                      Controller,\r
+                      &gEfiUsbHcProtocolGuid,\r
+                      This->DriverBindingHandle,\r
+                      Controller\r
+                      );\r
+    }\r
 \r
-  gBS->FreePool (Bus);\r
+    if (!EFI_ERROR (Status)) {\r
+      gBS->CloseProtocol (\r
+             Controller,\r
+             &gEfiDevicePathProtocolGuid,\r
+             This->DriverBindingHandle,\r
+             Controller\r
+             );\r
 \r
+      gBS->FreePool (Bus);\r
+    }\r
+  }\r
   return Status;\r
 }\r