]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Hand/DriverSupport.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Hand / DriverSupport.c
index 9db124c9f3f672190342a4274af8d42e808aec67..0b824c62b7087b33385c1bdd56207b009ae9a172 100644 (file)
@@ -1,39 +1,42 @@
 /** @file\r
   Support functions to connect/disconnect UEFI Driver model Protocol\r
 \r
-Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
-All rights reserved. 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) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include "DxeMain.h"\r
-\r
+#include "Handle.h"\r
 \r
 //\r
 // Driver Support Functions\r
 //\r
+\r
 /**\r
   Connects one or more drivers to a controller.\r
 \r
-  @param  ControllerHandle                      Handle of the controller to be\r
-                                                connected.\r
-  @param  DriverImageHandle                     DriverImageHandle A pointer to an\r
-                                                ordered list of driver image\r
-                                                handles.\r
-  @param  RemainingDevicePath                   RemainingDevicePath A pointer to\r
-                                                the device path that specifies a\r
-                                                child of the controller specified\r
-                                                by ControllerHandle.\r
-  @param  Recursive                             Whether the function would be\r
-                                                called recursively or not.\r
-\r
-  @return Status code.\r
+  @param  ControllerHandle      The handle of the controller to which driver(s) are to be connected.\r
+  @param  DriverImageHandle     A pointer to an ordered list handles that support the\r
+                                EFI_DRIVER_BINDING_PROTOCOL.\r
+  @param  RemainingDevicePath   A pointer to the device path that specifies a child of the\r
+                                controller specified by ControllerHandle.\r
+  @param  Recursive             If TRUE, then ConnectController() is called recursively\r
+                                until the entire tree of controllers below the controller specified\r
+                                by ControllerHandle have been created. If FALSE, then\r
+                                the tree of controllers is only expanded one level.\r
+\r
+  @retval EFI_SUCCESS           1) One or more drivers were connected to ControllerHandle.\r
+                                2) No drivers were connected to ControllerHandle, but\r
+                                RemainingDevicePath is not NULL, and it is an End Device\r
+                                Path Node.\r
+  @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.\r
+  @retval EFI_NOT_FOUND         1) There are no EFI_DRIVER_BINDING_PROTOCOL instances\r
+                                present in the system.\r
+                                2) No drivers were connected to ControllerHandle.\r
+  @retval EFI_SECURITY_VIOLATION\r
+                                The user has no permission to start UEFI device drivers on the device path\r
+                                associated with the ControllerHandle or specified by the RemainingDevicePath.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -45,26 +48,72 @@ CoreConnectController (
   IN  BOOLEAN                   Recursive\r
   )\r
 {\r
-  EFI_STATUS                           Status;\r
-  EFI_STATUS                           ReturnStatus;\r
-  IHANDLE                              *Handle;\r
-  PROTOCOL_INTERFACE                   *Prot;\r
-  LIST_ENTRY                           *Link;\r
-  LIST_ENTRY                           *ProtLink;\r
-  OPEN_PROTOCOL_DATA                   *OpenData;\r
-  EFI_DEVICE_PATH_PROTOCOL             *AlignedRemainingDevicePath;\r
-  EFI_HANDLE                           *ChildHandleBuffer;\r
-  UINTN                                ChildHandleCount;\r
-  UINTN                                Index;\r
+  EFI_STATUS                Status;\r
+  EFI_STATUS                ReturnStatus;\r
+  IHANDLE                   *Handle;\r
+  PROTOCOL_INTERFACE        *Prot;\r
+  LIST_ENTRY                *Link;\r
+  LIST_ENTRY                *ProtLink;\r
+  OPEN_PROTOCOL_DATA        *OpenData;\r
+  EFI_DEVICE_PATH_PROTOCOL  *AlignedRemainingDevicePath;\r
+  EFI_HANDLE                *ChildHandleBuffer;\r
+  UINTN                     ChildHandleCount;\r
+  UINTN                     Index;\r
+  UINTN                     HandleFilePathSize;\r
+  UINTN                     RemainingDevicePathSize;\r
+  EFI_DEVICE_PATH_PROTOCOL  *HandleFilePath;\r
+  EFI_DEVICE_PATH_PROTOCOL  *FilePath;\r
+  EFI_DEVICE_PATH_PROTOCOL  *TempFilePath;\r
 \r
   //\r
   // Make sure ControllerHandle is valid\r
   //\r
+  CoreAcquireProtocolLock ();\r
+\r
   Status = CoreValidateHandle (ControllerHandle);\r
+\r
+  CoreReleaseProtocolLock ();\r
+\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
+  if (gSecurity2 != NULL) {\r
+    //\r
+    // Check whether the user has permission to start UEFI device drivers.\r
+    //\r
+    Status = CoreHandleProtocol (ControllerHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath);\r
+    if (!EFI_ERROR (Status)) {\r
+      ASSERT (HandleFilePath != NULL);\r
+      FilePath     = HandleFilePath;\r
+      TempFilePath = NULL;\r
+      if ((RemainingDevicePath != NULL) && !Recursive) {\r
+        HandleFilePathSize      = GetDevicePathSize (HandleFilePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
+        RemainingDevicePathSize = GetDevicePathSize (RemainingDevicePath);\r
+        TempFilePath            = AllocateZeroPool (HandleFilePathSize + RemainingDevicePathSize);\r
+        ASSERT (TempFilePath != NULL);\r
+        CopyMem (TempFilePath, HandleFilePath, HandleFilePathSize);\r
+        CopyMem ((UINT8 *)TempFilePath + HandleFilePathSize, RemainingDevicePath, RemainingDevicePathSize);\r
+        FilePath = TempFilePath;\r
+      }\r
+\r
+      Status = gSecurity2->FileAuthentication (\r
+                             gSecurity2,\r
+                             FilePath,\r
+                             NULL,\r
+                             0,\r
+                             FALSE\r
+                             );\r
+      if (TempFilePath != NULL) {\r
+        FreePool (TempFilePath);\r
+      }\r
+\r
+      if (EFI_ERROR (Status)) {\r
+        return Status;\r
+      }\r
+    }\r
+  }\r
+\r
   Handle = ControllerHandle;\r
 \r
   //\r
@@ -73,6 +122,10 @@ CoreConnectController (
   AlignedRemainingDevicePath = NULL;\r
   if (RemainingDevicePath != NULL) {\r
     AlignedRemainingDevicePath = DuplicateDevicePath (RemainingDevicePath);\r
+\r
+    if (AlignedRemainingDevicePath == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
   }\r
 \r
   //\r
@@ -118,15 +171,15 @@ CoreConnectController (
       return ReturnStatus;\r
     }\r
 \r
-\r
     //\r
     // Count ControllerHandle's children\r
     //\r
     for (Link = Handle->Protocols.ForwardLink, ChildHandleCount = 0; Link != &Handle->Protocols; Link = Link->ForwardLink) {\r
-      Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);\r
+      Prot = CR (Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);\r
       for (ProtLink = Prot->OpenList.ForwardLink;\r
-          ProtLink != &Prot->OpenList;\r
-          ProtLink = ProtLink->ForwardLink) {\r
+           ProtLink != &Prot->OpenList;\r
+           ProtLink = ProtLink->ForwardLink)\r
+      {\r
         OpenData = CR (ProtLink, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE);\r
         if ((OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r
           ChildHandleCount++;\r
@@ -137,16 +190,21 @@ CoreConnectController (
     //\r
     // Allocate a handle buffer for ControllerHandle's children\r
     //\r
-    ChildHandleBuffer = AllocatePool (ChildHandleCount * sizeof(EFI_HANDLE));\r
+    ChildHandleBuffer = AllocatePool (ChildHandleCount * sizeof (EFI_HANDLE));\r
+    if (ChildHandleBuffer == NULL) {\r
+      CoreReleaseProtocolLock ();\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
 \r
     //\r
     // Fill in a handle buffer with ControllerHandle's children\r
     //\r
     for (Link = Handle->Protocols.ForwardLink, ChildHandleCount = 0; Link != &Handle->Protocols; Link = Link->ForwardLink) {\r
-      Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);\r
+      Prot = CR (Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);\r
       for (ProtLink = Prot->OpenList.ForwardLink;\r
-          ProtLink != &Prot->OpenList;\r
-          ProtLink = ProtLink->ForwardLink) {\r
+           ProtLink != &Prot->OpenList;\r
+           ProtLink = ProtLink->ForwardLink)\r
+      {\r
         OpenData = CR (ProtLink, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE);\r
         if ((OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r
           ChildHandleBuffer[ChildHandleCount] = OpenData->ControllerHandle;\r
@@ -181,7 +239,6 @@ CoreConnectController (
   return ReturnStatus;\r
 }\r
 \r
-\r
 /**\r
   Add Driver Binding Protocols from Context Driver Image Handles to sorted\r
   Driver Binding Protocol list.\r
@@ -218,7 +275,12 @@ AddSortedDriverBindingProtocol (
   //\r
   // Make sure the DriverBindingHandle is valid\r
   //\r
+  CoreAcquireProtocolLock ();\r
+\r
   Status = CoreValidateHandle (DriverBindingHandle);\r
+\r
+  CoreReleaseProtocolLock ();\r
+\r
   if (EFI_ERROR (Status)) {\r
     return;\r
   }\r
@@ -236,11 +298,11 @@ AddSortedDriverBindingProtocol (
       // Retrieve the Driver Binding Protocol associated with each Driver Binding Handle\r
       //\r
       Status = CoreHandleProtocol (\r
-                DriverBindingHandleBuffer[Index],\r
-                &gEfiDriverBindingProtocolGuid,\r
-                (VOID **) &DriverBinding\r
-                );\r
-      if (EFI_ERROR (Status) || DriverBinding == NULL) {\r
+                 DriverBindingHandleBuffer[Index],\r
+                 &gEfiDriverBindingProtocolGuid,\r
+                 (VOID **)&DriverBinding\r
+                 );\r
+      if (EFI_ERROR (Status) || (DriverBinding == NULL)) {\r
         continue;\r
       }\r
 \r
@@ -259,21 +321,22 @@ AddSortedDriverBindingProtocol (
           );\r
       }\r
     }\r
+\r
     return;\r
   }\r
 \r
   //\r
   // Retrieve the Driver Binding Protocol from DriverBindingHandle\r
   //\r
-  Status = CoreHandleProtocol(\r
+  Status = CoreHandleProtocol (\r
              DriverBindingHandle,\r
              &gEfiDriverBindingProtocolGuid,\r
-             (VOID **) &DriverBinding\r
+             (VOID **)&DriverBinding\r
              );\r
   //\r
   // If DriverBindingHandle does not support the Driver Binding Protocol then return\r
   //\r
-  if (EFI_ERROR (Status) || DriverBinding == NULL) {\r
+  if (EFI_ERROR (Status) || (DriverBinding == NULL)) {\r
     return;\r
   }\r
 \r
@@ -292,6 +355,7 @@ AddSortedDriverBindingProtocol (
   if (*NumberOfSortedDriverBindingProtocols < DriverBindingHandleCount) {\r
     SortedDriverBindingProtocols[*NumberOfSortedDriverBindingProtocols] = DriverBinding;\r
   }\r
+\r
   *NumberOfSortedDriverBindingProtocols = *NumberOfSortedDriverBindingProtocols + 1;\r
 \r
   //\r
@@ -304,7 +368,6 @@ AddSortedDriverBindingProtocol (
   }\r
 }\r
 \r
-\r
 /**\r
   Connects a controller to a driver.\r
 \r
@@ -343,8 +406,10 @@ CoreConnectSingleController (
   UINTN                                      NewDriverBindingHandleCount;\r
   EFI_HANDLE                                 *NewDriverBindingHandleBuffer;\r
   EFI_DRIVER_BINDING_PROTOCOL                *DriverBinding;\r
+  EFI_DRIVER_FAMILY_OVERRIDE_PROTOCOL        *DriverFamilyOverride;\r
   UINTN                                      NumberOfSortedDriverBindingProtocols;\r
   EFI_DRIVER_BINDING_PROTOCOL                **SortedDriverBindingProtocols;\r
+  UINT32                                     DriverFamilyOverrideVersion;\r
   UINT32                                     HighestVersion;\r
   UINTN                                      HighestIndex;\r
   UINTN                                      SortIndex;\r
@@ -354,10 +419,12 @@ CoreConnectSingleController (
   //\r
   // Initialize local variables\r
   //\r
-  DriverBindingHandleCount              = 0;\r
-  DriverBindingHandleBuffer             = NULL;\r
-  NumberOfSortedDriverBindingProtocols  = 0;\r
-  SortedDriverBindingProtocols          = NULL;\r
+  DriverBindingHandleCount             = 0;\r
+  DriverBindingHandleBuffer            = NULL;\r
+  NumberOfSortedDriverBindingProtocols = 0;\r
+  SortedDriverBindingProtocols         = NULL;\r
+  PlatformDriverOverride               = NULL;\r
+  NewDriverBindingHandleBuffer         = NULL;\r
 \r
   //\r
   // Get list of all Driver Binding Protocol Instances\r
@@ -404,7 +471,7 @@ CoreConnectSingleController (
   Status = CoreLocateProtocol (\r
              &gEfiPlatformDriverOverrideProtocolGuid,\r
              NULL,\r
-             (VOID **) &PlatformDriverOverride\r
+             (VOID **)&PlatformDriverOverride\r
              );\r
   if (!EFI_ERROR (Status) && (PlatformDriverOverride != NULL)) {\r
     DriverImageHandle = NULL;\r
@@ -427,13 +494,48 @@ CoreConnectSingleController (
     } while (!EFI_ERROR (Status));\r
   }\r
 \r
+  //\r
+  // Add the Driver Family Override Protocol drivers for ControllerHandle\r
+  //\r
+  while (TRUE) {\r
+    HighestIndex   = DriverBindingHandleCount;\r
+    HighestVersion = 0;\r
+    for (Index = 0; Index < DriverBindingHandleCount; Index++) {\r
+      Status = CoreHandleProtocol (\r
+                 DriverBindingHandleBuffer[Index],\r
+                 &gEfiDriverFamilyOverrideProtocolGuid,\r
+                 (VOID **)&DriverFamilyOverride\r
+                 );\r
+      if (!EFI_ERROR (Status) && (DriverFamilyOverride != NULL)) {\r
+        DriverFamilyOverrideVersion = DriverFamilyOverride->GetVersion (DriverFamilyOverride);\r
+        if ((HighestIndex == DriverBindingHandleCount) || (DriverFamilyOverrideVersion > HighestVersion)) {\r
+          HighestVersion = DriverFamilyOverrideVersion;\r
+          HighestIndex   = Index;\r
+        }\r
+      }\r
+    }\r
+\r
+    if (HighestIndex == DriverBindingHandleCount) {\r
+      break;\r
+    }\r
+\r
+    AddSortedDriverBindingProtocol (\r
+      DriverBindingHandleBuffer[HighestIndex],\r
+      &NumberOfSortedDriverBindingProtocols,\r
+      SortedDriverBindingProtocols,\r
+      DriverBindingHandleCount,\r
+      DriverBindingHandleBuffer,\r
+      FALSE\r
+      );\r
+  }\r
+\r
   //\r
   // Get the Bus Specific Driver Override Protocol instance on the Controller Handle\r
   //\r
   Status = CoreHandleProtocol (\r
              ControllerHandle,\r
              &gEfiBusSpecificDriverOverrideProtocolGuid,\r
-             (VOID **) &BusSpecificDriverOverride\r
+             (VOID **)&BusSpecificDriverOverride\r
              );\r
   if (!EFI_ERROR (Status) && (BusSpecificDriverOverride != NULL)) {\r
     DriverImageHandle = NULL;\r
@@ -509,9 +611,10 @@ CoreConnectSingleController (
         HighestIndex   = Index;\r
       }\r
     }\r
+\r
     if (SortIndex != HighestIndex) {\r
-      DriverBinding = SortedDriverBindingProtocols[SortIndex];\r
-      SortedDriverBindingProtocols[SortIndex] = SortedDriverBindingProtocols[HighestIndex];\r
+      DriverBinding                              = SortedDriverBindingProtocols[SortIndex];\r
+      SortedDriverBindingProtocols[SortIndex]    = SortedDriverBindingProtocols[HighestIndex];\r
       SortedDriverBindingProtocols[HighestIndex] = DriverBinding;\r
     }\r
   }\r
@@ -521,37 +624,38 @@ CoreConnectSingleController (
   //\r
   OneStarted = FALSE;\r
   do {\r
-\r
     //\r
     // Loop through the sorted Driver Binding Protocol Instances in order, and see if\r
     // any of the Driver Binding Protocols support the controller specified by\r
     // ControllerHandle.\r
     //\r
     DriverBinding = NULL;\r
-    DriverFound = FALSE;\r
+    DriverFound   = FALSE;\r
     for (Index = 0; (Index < NumberOfSortedDriverBindingProtocols) && !DriverFound; Index++) {\r
       if (SortedDriverBindingProtocols[Index] != NULL) {\r
         DriverBinding = SortedDriverBindingProtocols[Index];\r
-        Status = DriverBinding->Supported(\r
+        PERF_DRIVER_BINDING_SUPPORT_BEGIN (DriverBinding->DriverBindingHandle, ControllerHandle);\r
+        Status = DriverBinding->Supported (\r
                                   DriverBinding,\r
                                   ControllerHandle,\r
                                   RemainingDevicePath\r
                                   );\r
+        PERF_DRIVER_BINDING_SUPPORT_END (DriverBinding->DriverBindingHandle, ControllerHandle);\r
         if (!EFI_ERROR (Status)) {\r
           SortedDriverBindingProtocols[Index] = NULL;\r
-          DriverFound = TRUE;\r
+          DriverFound                         = TRUE;\r
 \r
           //\r
           // A driver was found that supports ControllerHandle, so attempt to start the driver\r
           // on ControllerHandle.\r
           //\r
-          PERF_START (DriverBinding->DriverBindingHandle, DRIVERBINDING_START_TOK, NULL, 0);\r
+          PERF_DRIVER_BINDING_START_BEGIN (DriverBinding->DriverBindingHandle, ControllerHandle);\r
           Status = DriverBinding->Start (\r
                                     DriverBinding,\r
                                     ControllerHandle,\r
                                     RemainingDevicePath\r
                                     );\r
-          PERF_END (DriverBinding->DriverBindingHandle, DRIVERBINDING_START_TOK, NULL, 0);\r
+          PERF_DRIVER_BINDING_START_END (DriverBinding->DriverBindingHandle, ControllerHandle);\r
 \r
           if (!EFI_ERROR (Status)) {\r
             //\r
@@ -591,8 +695,6 @@ CoreConnectSingleController (
   return EFI_NOT_FOUND;\r
 }\r
 \r
-\r
-\r
 /**\r
   Disonnects a controller from a driver\r
 \r
@@ -612,8 +714,7 @@ CoreConnectSingleController (
   @retval EFI_SUCCESS                           DriverImageHandle is not NULL,\r
                                                 and on entry DriverImageHandle is\r
                                                 not managing ControllerHandle.\r
-  @retval EFI_INVALID_PARAMETER                 ControllerHandle is not a valid\r
-                                                EFI_HANDLE.\r
+  @retval EFI_INVALID_PARAMETER                 ControllerHandle is NULL.\r
   @retval EFI_INVALID_PARAMETER                 DriverImageHandle is not NULL,\r
                                                 and it is not a valid EFI_HANDLE.\r
   @retval EFI_INVALID_PARAMETER                 ChildHandle is not NULL, and it\r
@@ -634,30 +735,33 @@ CoreDisconnectController (
   IN  EFI_HANDLE  ChildHandle        OPTIONAL\r
   )\r
 {\r
-  EFI_STATUS                          Status;\r
-  IHANDLE                             *Handle;\r
-  EFI_HANDLE                          *DriverImageHandleBuffer;\r
-  EFI_HANDLE                          *ChildBuffer;\r
-  UINTN                               Index;\r
-  UINTN                               HandleIndex;\r
-  UINTN                               DriverImageHandleCount;\r
-  UINTN                               ChildrenToStop;\r
-  UINTN                               ChildBufferCount;\r
-  UINTN                               StopCount;\r
-  BOOLEAN                             Duplicate;\r
-  BOOLEAN                             ChildHandleValid;\r
-  BOOLEAN                             DriverImageHandleValid;\r
-  LIST_ENTRY                          *Link;\r
-  LIST_ENTRY                          *ProtLink;\r
-  OPEN_PROTOCOL_DATA                  *OpenData;\r
-  PROTOCOL_INTERFACE                  *Prot;\r
-  EFI_DRIVER_BINDING_PROTOCOL         *DriverBinding;\r
+  EFI_STATUS                   Status;\r
+  IHANDLE                      *Handle;\r
+  EFI_HANDLE                   *DriverImageHandleBuffer;\r
+  EFI_HANDLE                   *ChildBuffer;\r
+  UINTN                        Index;\r
+  UINTN                        HandleIndex;\r
+  UINTN                        DriverImageHandleCount;\r
+  UINTN                        ChildrenToStop;\r
+  UINTN                        ChildBufferCount;\r
+  UINTN                        StopCount;\r
+  BOOLEAN                      Duplicate;\r
+  BOOLEAN                      ChildHandleValid;\r
+  BOOLEAN                      DriverImageHandleValid;\r
+  LIST_ENTRY                   *Link;\r
+  LIST_ENTRY                   *ProtLink;\r
+  OPEN_PROTOCOL_DATA           *OpenData;\r
+  PROTOCOL_INTERFACE           *Prot;\r
+  EFI_DRIVER_BINDING_PROTOCOL  *DriverBinding;\r
 \r
   //\r
   // Make sure ControllerHandle is valid\r
   //\r
+  CoreAcquireProtocolLock ();\r
+\r
   Status = CoreValidateHandle (ControllerHandle);\r
   if (EFI_ERROR (Status)) {\r
+    CoreReleaseProtocolLock ();\r
     return Status;\r
   }\r
 \r
@@ -667,10 +771,13 @@ CoreDisconnectController (
   if (ChildHandle != NULL) {\r
     Status = CoreValidateHandle (ChildHandle);\r
     if (EFI_ERROR (Status)) {\r
+      CoreReleaseProtocolLock ();\r
       return Status;\r
     }\r
   }\r
 \r
+  CoreReleaseProtocolLock ();\r
+\r
   Handle = ControllerHandle;\r
 \r
   //\r
@@ -687,16 +794,18 @@ CoreDisconnectController (
 \r
     CoreAcquireProtocolLock ();\r
     for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link = Link->ForwardLink) {\r
-      Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);\r
+      Prot = CR (Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);\r
       for (ProtLink = Prot->OpenList.ForwardLink;\r
            ProtLink != &Prot->OpenList;\r
-           ProtLink = ProtLink->ForwardLink) {\r
+           ProtLink = ProtLink->ForwardLink)\r
+      {\r
         OpenData = CR (ProtLink, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE);\r
         if ((OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) {\r
           DriverImageHandleCount++;\r
         }\r
       }\r
     }\r
+\r
     CoreReleaseProtocolLock ();\r
 \r
     //\r
@@ -717,19 +826,21 @@ CoreDisconnectController (
 \r
     CoreAcquireProtocolLock ();\r
     for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link = Link->ForwardLink) {\r
-      Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);\r
+      Prot = CR (Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);\r
       for (ProtLink = Prot->OpenList.ForwardLink;\r
            ProtLink != &Prot->OpenList;\r
-           ProtLink = ProtLink->ForwardLink) {\r
+           ProtLink = ProtLink->ForwardLink)\r
+      {\r
         OpenData = CR (ProtLink, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE);\r
         if ((OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) {\r
           Duplicate = FALSE;\r
-          for (Index = 0; Index< DriverImageHandleCount; Index++) {\r
+          for (Index = 0; Index < DriverImageHandleCount; Index++) {\r
             if (DriverImageHandleBuffer[Index] == OpenData->AgentHandle) {\r
               Duplicate = TRUE;\r
               break;\r
             }\r
           }\r
+\r
           if (!Duplicate) {\r
             DriverImageHandleBuffer[DriverImageHandleCount] = OpenData->AgentHandle;\r
             DriverImageHandleCount++;\r
@@ -737,12 +848,12 @@ CoreDisconnectController (
         }\r
       }\r
     }\r
+\r
     CoreReleaseProtocolLock ();\r
   }\r
 \r
   StopCount = 0;\r
   for (HandleIndex = 0; HandleIndex < DriverImageHandleCount; HandleIndex++) {\r
-\r
     if (DriverImageHandleBuffer != NULL) {\r
       DriverImageHandle = DriverImageHandleBuffer[HandleIndex];\r
     }\r
@@ -755,7 +866,7 @@ CoreDisconnectController (
                &gEfiDriverBindingProtocolGuid,\r
                (VOID **)&DriverBinding\r
                );\r
-    if (EFI_ERROR (Status)) {\r
+    if (EFI_ERROR (Status) || (DriverBinding == NULL)) {\r
       Status = EFI_INVALID_PARAMETER;\r
       goto Done;\r
     }\r
@@ -764,30 +875,33 @@ CoreDisconnectController (
     // Look at each protocol interface for a match\r
     //\r
     DriverImageHandleValid = FALSE;\r
-    ChildBufferCount = 0;\r
+    ChildBufferCount       = 0;\r
 \r
     CoreAcquireProtocolLock ();\r
     for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link = Link->ForwardLink) {\r
-      Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);\r
+      Prot = CR (Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);\r
       for (ProtLink = Prot->OpenList.ForwardLink;\r
            ProtLink != &Prot->OpenList;\r
-           ProtLink = ProtLink->ForwardLink) {\r
+           ProtLink = ProtLink->ForwardLink)\r
+      {\r
         OpenData = CR (ProtLink, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE);\r
         if (OpenData->AgentHandle == DriverImageHandle) {\r
           if ((OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r
             ChildBufferCount++;\r
           }\r
+\r
           if ((OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) {\r
             DriverImageHandleValid = TRUE;\r
           }\r
         }\r
       }\r
     }\r
+\r
     CoreReleaseProtocolLock ();\r
 \r
     if (DriverImageHandleValid) {\r
       ChildHandleValid = FALSE;\r
-      ChildBuffer = NULL;\r
+      ChildBuffer      = NULL;\r
       if (ChildBufferCount != 0) {\r
         ChildBuffer = AllocatePool (sizeof (EFI_HANDLE) * ChildBufferCount);\r
         if (ChildBuffer == NULL) {\r
@@ -799,13 +913,15 @@ CoreDisconnectController (
 \r
         CoreAcquireProtocolLock ();\r
         for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link = Link->ForwardLink) {\r
-          Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);\r
+          Prot = CR (Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);\r
           for (ProtLink = Prot->OpenList.ForwardLink;\r
                ProtLink != &Prot->OpenList;\r
-               ProtLink = ProtLink->ForwardLink) {\r
+               ProtLink = ProtLink->ForwardLink)\r
+          {\r
             OpenData = CR (ProtLink, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE);\r
             if ((OpenData->AgentHandle == DriverImageHandle) &&\r
-                ((OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0)) {\r
+                ((OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0))\r
+            {\r
               Duplicate = FALSE;\r
               for (Index = 0; Index < ChildBufferCount; Index++) {\r
                 if (ChildBuffer[Index] == OpenData->ControllerHandle) {\r
@@ -813,34 +929,39 @@ CoreDisconnectController (
                   break;\r
                 }\r
               }\r
+\r
               if (!Duplicate) {\r
                 ChildBuffer[ChildBufferCount] = OpenData->ControllerHandle;\r
                 if (ChildHandle == ChildBuffer[ChildBufferCount]) {\r
                   ChildHandleValid = TRUE;\r
                 }\r
+\r
                 ChildBufferCount++;\r
               }\r
             }\r
           }\r
         }\r
+\r
         CoreReleaseProtocolLock ();\r
       }\r
 \r
-      if (ChildHandle == NULL || ChildHandleValid) {\r
+      if ((ChildHandle == NULL) || ChildHandleValid) {\r
         ChildrenToStop = 0;\r
-        Status = EFI_SUCCESS;\r
+        Status         = EFI_SUCCESS;\r
         if (ChildBufferCount > 0) {\r
           if (ChildHandle != NULL) {\r
             ChildrenToStop = 1;\r
-            Status = DriverBinding->Stop (DriverBinding, ControllerHandle, ChildrenToStop, &ChildHandle);\r
+            Status         = DriverBinding->Stop (DriverBinding, ControllerHandle, ChildrenToStop, &ChildHandle);\r
           } else {\r
             ChildrenToStop = ChildBufferCount;\r
-            Status = DriverBinding->Stop (DriverBinding, ControllerHandle, ChildrenToStop, ChildBuffer);\r
+            Status         = DriverBinding->Stop (DriverBinding, ControllerHandle, ChildrenToStop, ChildBuffer);\r
           }\r
         }\r
+\r
         if (!EFI_ERROR (Status) && ((ChildHandle == NULL) || (ChildBufferCount == ChildrenToStop))) {\r
           Status = DriverBinding->Stop (DriverBinding, ControllerHandle, 0, NULL);\r
         }\r
+\r
         if (!EFI_ERROR (Status)) {\r
           StopCount++;\r
         }\r