]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EdkModulePkg/Universal/ConPlatform/Dxe/ConPlatform.c
Move ConPlatform module from EdkNT32 package to EdkModulePkg, and remove EdkPeCoffLoa...
[mirror_edk2.git] / EdkModulePkg / Universal / ConPlatform / Dxe / ConPlatform.c
diff --git a/EdkModulePkg/Universal/ConPlatform/Dxe/ConPlatform.c b/EdkModulePkg/Universal/ConPlatform/Dxe/ConPlatform.c
new file mode 100644 (file)
index 0000000..4b7d279
--- /dev/null
@@ -0,0 +1,811 @@
+/*++\r
+\r
+Copyright (c) 2006, Intel Corporation                                                         \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
+\r
+Module Name:\r
+\r
+    ConPlatform.c\r
+    \r
+Abstract:\r
+\r
+--*/\r
+\r
+#include "ConPlatform.h"\r
+\r
+EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextInDriverBinding = {\r
+  ConPlatformTextInDriverBindingSupported,\r
+  ConPlatformTextInDriverBindingStart,\r
+  ConPlatformDriverBindingStop,\r
+  0x10,\r
+  NULL,\r
+  NULL\r
+};\r
+\r
+EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextOutDriverBinding = {\r
+  ConPlatformTextOutDriverBindingSupported,\r
+  ConPlatformTextOutDriverBindingStart,\r
+  ConPlatformDriverBindingStop,\r
+  0x10,\r
+  NULL,\r
+  NULL\r
+};\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+ConPlatformTextInDriverBindingSupported (\r
+  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,\r
+  IN  EFI_HANDLE                   ControllerHandle,\r
+  IN  EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Supported \r
+\r
+Arguments:\r
+  (Standard DriverBinding Protocol Supported() function)\r
+\r
+Returns:\r
+\r
+  None\r
+\r
+--*/\r
+{\r
+  return ConPlatformDriverBindingSupported (\r
+          This,\r
+          ControllerHandle,\r
+          RemainingDevicePath,\r
+          &gEfiSimpleTextInProtocolGuid\r
+          );\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+ConPlatformTextOutDriverBindingSupported (\r
+  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,\r
+  IN  EFI_HANDLE                   ControllerHandle,\r
+  IN  EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Supported \r
+\r
+Arguments:\r
+  (Standard DriverBinding Protocol Supported() function)\r
+\r
+Returns:\r
+\r
+  None\r
+\r
+--*/\r
+{\r
+  return ConPlatformDriverBindingSupported (\r
+          This,\r
+          ControllerHandle,\r
+          RemainingDevicePath,\r
+          &gEfiSimpleTextOutProtocolGuid\r
+          );\r
+}\r
+\r
+EFI_STATUS\r
+ConPlatformDriverBindingSupported (\r
+  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,\r
+  IN  EFI_HANDLE                   ControllerHandle,\r
+  IN  EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath,\r
+  IN  EFI_GUID                     *ProtocolGuid\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Supported \r
+\r
+Arguments:\r
+  (Standard DriverBinding Protocol Supported() function)\r
+\r
+Returns:\r
+\r
+  None\r
+\r
+--*/\r
+{\r
+  EFI_STATUS  Status;\r
+  VOID        *Interface;\r
+\r
+  //\r
+  // Test to see if this is a physical device by checking to see if\r
+  // it has a Device Path Protocol\r
+  //\r
+  Status = gBS->OpenProtocol (\r
+                  ControllerHandle,\r
+                  &gEfiDevicePathProtocolGuid,\r
+                  NULL,\r
+                  This->DriverBindingHandle,\r
+                  ControllerHandle,\r
+                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  //\r
+  // Test to see if this device supports the Simple Text Output Protocol\r
+  //\r
+  Status = gBS->OpenProtocol (\r
+                  ControllerHandle,\r
+                  ProtocolGuid,\r
+                  (VOID **) &Interface,\r
+                  This->DriverBindingHandle,\r
+                  ControllerHandle,\r
+                  EFI_OPEN_PROTOCOL_BY_DRIVER\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  gBS->CloseProtocol (\r
+        ControllerHandle,\r
+        ProtocolGuid,\r
+        This->DriverBindingHandle,\r
+        ControllerHandle\r
+        );\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+ConPlatformTextInDriverBindingStart (\r
+  IN  EFI_DRIVER_BINDING_PROTOCOL   *This,\r
+  IN  EFI_HANDLE                    ControllerHandle,\r
+  IN  EFI_DEVICE_PATH_PROTOCOL      *RemainingDevicePath\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+\r
+Arguments:\r
+  (Standard DriverBinding Protocol Start() function)\r
+\r
+Returns:\r
+\r
+\r
+--*/\r
+{\r
+  EFI_STATUS                  Status;\r
+  EFI_DEVICE_PATH_PROTOCOL    *DevicePath;\r
+  EFI_SIMPLE_TEXT_IN_PROTOCOL *TextIn;\r
+\r
+  //\r
+  // Get the Device Path Protocol so the environment variables can be updated\r
+  //\r
+  Status = gBS->OpenProtocol (\r
+                  ControllerHandle,\r
+                  &gEfiDevicePathProtocolGuid,\r
+                  (VOID **) &DevicePath,\r
+                  This->DriverBindingHandle,\r
+                  ControllerHandle,\r
+                  EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  //\r
+  // Open the Simple Input Protocol BY_DRIVER\r
+  //\r
+  Status = gBS->OpenProtocol (\r
+                  ControllerHandle,\r
+                  &gEfiSimpleTextInProtocolGuid,\r
+                  (VOID **) &TextIn,\r
+                  This->DriverBindingHandle,\r
+                  ControllerHandle,\r
+                  EFI_OPEN_PROTOCOL_BY_DRIVER\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  //\r
+  // Check the device handle, if it is a hot plug device,\r
+  // do not put the device path into ConInDev, and install\r
+  // gEfiConsoleInDeviceGuid to the device handle directly.\r
+  // The policy is, make hot plug device plug in and play immediately.\r
+  //\r
+  if (IsHotPlugDevice (This->DriverBindingHandle, ControllerHandle)) {\r
+    gBS->InstallMultipleProtocolInterfaces (\r
+          &ControllerHandle,\r
+          &gEfiConsoleInDeviceGuid,\r
+          NULL,\r
+          NULL\r
+          );\r
+  } else {\r
+    //\r
+    // Append the device path to the ConInDev environment variable\r
+    //\r
+    ConPlatformUpdateDeviceVariable (\r
+      VarConsoleInpDev,\r
+      DevicePath,\r
+      APPEND\r
+      );\r
+\r
+    //\r
+    // If the device path is an instance in the ConIn environment variable,\r
+    // then install EfiConsoleInDeviceGuid onto ControllerHandle\r
+    //\r
+    Status = ConPlatformUpdateDeviceVariable (\r
+              VarConsoleInp,\r
+              DevicePath,\r
+              CHECK\r
+              );\r
+\r
+    if (!EFI_ERROR (Status)) {\r
+      gBS->InstallMultipleProtocolInterfaces (\r
+            &ControllerHandle,\r
+            &gEfiConsoleInDeviceGuid,\r
+            NULL,\r
+            NULL\r
+            );\r
+    } else {\r
+      gBS->CloseProtocol (\r
+            ControllerHandle,\r
+            &gEfiSimpleTextInProtocolGuid,\r
+            This->DriverBindingHandle,\r
+            ControllerHandle\r
+            );\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+ConPlatformTextOutDriverBindingStart (\r
+  IN  EFI_DRIVER_BINDING_PROTOCOL   *This,\r
+  IN  EFI_HANDLE                    ControllerHandle,\r
+  IN  EFI_DEVICE_PATH_PROTOCOL      *RemainingDevicePath\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+\r
+Arguments:\r
+  (Standard DriverBinding Protocol Start() function)\r
+\r
+Returns:\r
+\r
+\r
+--*/\r
+{\r
+  EFI_STATUS                    Status;\r
+  EFI_DEVICE_PATH_PROTOCOL      *DevicePath;\r
+  EFI_SIMPLE_TEXT_OUT_PROTOCOL  *TextOut;\r
+\r
+  BOOLEAN                       NeedClose;\r
+\r
+  NeedClose = TRUE;\r
+\r
+  //\r
+  // Get the Device Path Protocol so the environment variables can be updated\r
+  //\r
+  Status = gBS->OpenProtocol (\r
+                  ControllerHandle,\r
+                  &gEfiDevicePathProtocolGuid,\r
+                  (VOID **) &DevicePath,\r
+                  This->DriverBindingHandle,\r
+                  ControllerHandle,\r
+                  EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  //\r
+  // Open the Simple Text Output Protocol BY_DRIVER\r
+  //\r
+  Status = gBS->OpenProtocol (\r
+                  ControllerHandle,\r
+                  &gEfiSimpleTextOutProtocolGuid,\r
+                  (VOID **) &TextOut,\r
+                  This->DriverBindingHandle,\r
+                  ControllerHandle,\r
+                  EFI_OPEN_PROTOCOL_BY_DRIVER\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  //\r
+  // Check the device handle, if it is a hot plug device,\r
+  // do not put the device path into ConOutDev and StdErrDev,\r
+  // and install gEfiConsoleOutDeviceGuid to the device handle directly.\r
+  // The policy is, make hot plug device plug in and play immediately.\r
+  //\r
+  if (IsHotPlugDevice (This->DriverBindingHandle, ControllerHandle)) {\r
+    gBS->InstallMultipleProtocolInterfaces (\r
+          &ControllerHandle,\r
+          &gEfiConsoleOutDeviceGuid,\r
+          NULL,\r
+          NULL\r
+          );\r
+  } else {\r
+    //\r
+    // Append the device path to the ConOutDev environment variable\r
+    //\r
+    ConPlatformUpdateDeviceVariable (\r
+      VarConsoleOutDev,\r
+      DevicePath,\r
+      APPEND\r
+      );\r
+    //\r
+    // Append the device path to the StdErrDev environment variable\r
+    //\r
+    ConPlatformUpdateDeviceVariable (\r
+      VarErrorOutDev,\r
+      DevicePath,\r
+      APPEND\r
+      );\r
+\r
+    //\r
+    // If the device path is an instance in the ConOut environment variable,\r
+    // then install EfiConsoleOutDeviceGuid onto ControllerHandle\r
+    //\r
+    Status = ConPlatformUpdateDeviceVariable (\r
+              VarConsoleOut,\r
+              DevicePath,\r
+              CHECK\r
+              );\r
+    if (!EFI_ERROR (Status)) {\r
+      NeedClose = FALSE;\r
+      Status = gBS->InstallMultipleProtocolInterfaces (\r
+                      &ControllerHandle,\r
+                      &gEfiConsoleOutDeviceGuid,\r
+                      NULL,\r
+                      NULL\r
+                      );\r
+    }\r
+    //\r
+    // If the device path is an instance in the StdErr environment variable,\r
+    // then install EfiStandardErrorDeviceGuid onto ControllerHandle\r
+    //\r
+    Status = ConPlatformUpdateDeviceVariable (\r
+              VarErrorOut,\r
+              DevicePath,\r
+              CHECK\r
+              );\r
+    if (!EFI_ERROR (Status)) {\r
+      NeedClose = FALSE;\r
+      gBS->InstallMultipleProtocolInterfaces (\r
+            &ControllerHandle,\r
+            &gEfiStandardErrorDeviceGuid,\r
+            NULL,\r
+            NULL\r
+            );\r
+    }\r
+\r
+    if (NeedClose) {\r
+      gBS->CloseProtocol (\r
+            ControllerHandle,\r
+            &gEfiSimpleTextOutProtocolGuid,\r
+            This->DriverBindingHandle,\r
+            ControllerHandle\r
+            );\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+ConPlatformDriverBindingStop (\r
+  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,\r
+  IN  EFI_HANDLE                   ControllerHandle,\r
+  IN  UINTN                        NumberOfChildren,\r
+  IN  EFI_HANDLE                   *ChildHandleBuffer\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+  (Standard DriverBinding Protocol Stop() function)\r
+\r
+Returns:\r
+\r
+  None\r
+\r
+--*/\r
+{\r
+  EFI_STATUS                Status;\r
+  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
+\r
+  //\r
+  // hot plug device is not included into the console associated variables,\r
+  // so no need to check variable for those hot plug devices.\r
+  //\r
+  if (!IsHotPlugDevice (This->DriverBindingHandle, ControllerHandle)) {\r
+    //\r
+    // Get the Device Path Protocol so the environment variables can be updated\r
+    //\r
+    Status = gBS->OpenProtocol (\r
+                    ControllerHandle,\r
+                    &gEfiDevicePathProtocolGuid,\r
+                    (VOID **) &DevicePath,\r
+                    This->DriverBindingHandle,\r
+                    ControllerHandle,\r
+                    EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                    );\r
+    if (!EFI_ERROR (Status)) {\r
+      //\r
+      // Remove DevicePath from ConInDev, ConOutDev, and StdErrDev\r
+      //\r
+      ConPlatformUpdateDeviceVariable (\r
+        VarConsoleInpDev,\r
+        DevicePath,\r
+        DELETE\r
+        );\r
+      ConPlatformUpdateDeviceVariable (\r
+        VarConsoleOutDev,\r
+        DevicePath,\r
+        DELETE\r
+        );\r
+      ConPlatformUpdateDeviceVariable (\r
+        VarErrorOutDev,\r
+        DevicePath,\r
+        DELETE\r
+        );\r
+    }\r
+  }\r
+  //\r
+  // Uninstall the Console Device GUIDs from Controller Handle\r
+  //\r
+  ConPlatformUnInstallProtocol (\r
+    This,\r
+    ControllerHandle,\r
+    &gEfiConsoleInDeviceGuid\r
+    );\r
+\r
+  ConPlatformUnInstallProtocol (\r
+    This,\r
+    ControllerHandle,\r
+    &gEfiConsoleOutDeviceGuid\r
+    );\r
+\r
+  ConPlatformUnInstallProtocol (\r
+    This,\r
+    ControllerHandle,\r
+    &gEfiStandardErrorDeviceGuid\r
+    );\r
+\r
+  //\r
+  // Close the Simple Input and Simple Text Output Protocols\r
+  //\r
+  gBS->CloseProtocol (\r
+        ControllerHandle,\r
+        &gEfiSimpleTextInProtocolGuid,\r
+        This->DriverBindingHandle,\r
+        ControllerHandle\r
+        );\r
+\r
+  gBS->CloseProtocol (\r
+        ControllerHandle,\r
+        &gEfiSimpleTextOutProtocolGuid,\r
+        This->DriverBindingHandle,\r
+        ControllerHandle\r
+        );\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+VOID\r
+ConPlatformUnInstallProtocol (\r
+  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,\r
+  IN  EFI_HANDLE                   Handle,\r
+  IN  EFI_GUID                     *ProtocolGuid\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  Status = gBS->OpenProtocol (\r
+                  Handle,\r
+                  ProtocolGuid,\r
+                  NULL,\r
+                  This->DriverBindingHandle,\r
+                  Handle,\r
+                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
+                  );\r
+\r
+  if (!EFI_ERROR (Status)) {\r
+    gBS->UninstallMultipleProtocolInterfaces (\r
+          Handle,\r
+          ProtocolGuid,\r
+          NULL,\r
+          NULL\r
+          );\r
+  }\r
+\r
+  return ;\r
+}\r
+\r
+VOID *\r
+ConPlatformGetVariable (\r
+  IN  CHAR16    *Name\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Read the EFI variable (Name) and return a dynamically allocated\r
+  buffer, and the size of the buffer. On failure return NULL.\r
+\r
+Arguments:\r
+  Name       - String part of EFI variable name\r
+\r
+Returns:\r
+  Dynamically allocated memory that contains a copy of the EFI variable.\r
+  Caller is repsoncible freeing the buffer.\r
+\r
+  NULL - Variable was not read\r
+  \r
+--*/\r
+{\r
+  EFI_STATUS  Status;\r
+  VOID        *Buffer;\r
+  UINTN       BufferSize;\r
+\r
+  BufferSize  = 0;\r
+  Buffer      = NULL;\r
+\r
+  //\r
+  // Test to see if the variable exists.  If it doesn't reuturn NULL\r
+  //\r
+  Status = gRT->GetVariable (\r
+                  Name,\r
+                  &gEfiGlobalVariableGuid,\r
+                  NULL,\r
+                  &BufferSize,\r
+                  Buffer\r
+                  );\r
+\r
+  if (Status == EFI_BUFFER_TOO_SMALL) {\r
+    //\r
+    // Allocate the buffer to return\r
+    //\r
+    Status = gBS->AllocatePool (EfiBootServicesData, BufferSize, &Buffer);\r
+    if (EFI_ERROR (Status)) {\r
+      return NULL;\r
+    }\r
+    //\r
+    // Read variable into the allocated buffer.\r
+    //\r
+    Status = gRT->GetVariable (\r
+                    Name,\r
+                    &gEfiGlobalVariableGuid,\r
+                    NULL,\r
+                    &BufferSize,\r
+                    Buffer\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      gBS->FreePool (Buffer);\r
+      Buffer = NULL;\r
+    }\r
+  }\r
+\r
+  return Buffer;\r
+}\r
+\r
+EFI_STATUS\r
+ConPlatformMatchDevicePaths (\r
+  IN  EFI_DEVICE_PATH_PROTOCOL  * Multi,\r
+  IN  EFI_DEVICE_PATH_PROTOCOL  * Single,\r
+  IN  EFI_DEVICE_PATH_PROTOCOL  **NewDevicePath OPTIONAL,\r
+  IN  BOOLEAN                   Delete\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Function compares a device path data structure to that of all the nodes of a\r
+  second device path instance.\r
+\r
+Arguments:\r
+  Multi        - A pointer to a multi-instance device path data structure.\r
+\r
+  Single       - A pointer to a single-instance device path data structure.\r
+  \r
+  NewDevicePath - If Delete is TRUE, this parameter must not be null, and it\r
+                  points to the remaining device path data structure. \r
+                  (remaining device path = Multi - Single.)\r
+  \r
+  Delete        - If TRUE, means removing Single from Multi.\r
+                  If FALSE, the routine just check whether Single matches \r
+                  with any instance in Multi.\r
+\r
+Returns:\r
+\r
+  The function returns EFI_SUCCESS if the Single is contained within Multi.  \r
+  Otherwise, EFI_NOT_FOUND is returned.\r
+\r
+--*/\r
+{\r
+  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL  *TempDevicePath1;\r
+  EFI_DEVICE_PATH_PROTOCOL  *TempDevicePath2;\r
+  EFI_DEVICE_PATH_PROTOCOL  *DevicePathInst;\r
+  UINTN                     Size;\r
+\r
+  //\r
+  // The passed in DevicePath should not be NULL\r
+  //\r
+  if ((!Multi) || (!Single)) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  //\r
+  // if performing Delete operation, the NewDevicePath must not be NULL.\r
+  //\r
+  TempDevicePath1 = NULL;\r
+\r
+  DevicePath      = Multi;\r
+  DevicePathInst  = GetNextDevicePathInstance (&DevicePath, &Size);\r
+\r
+  //\r
+  // search for the match of 'Single' in 'Multi'\r
+  //\r
+  while (DevicePathInst) {\r
+    if (CompareMem (Single, DevicePathInst, Size) == 0) {\r
+      if (!Delete) {\r
+        gBS->FreePool (DevicePathInst);\r
+        return EFI_SUCCESS;\r
+      }\r
+    } else {\r
+      if (Delete) {\r
+        TempDevicePath2 = AppendDevicePathInstance (\r
+                            TempDevicePath1,\r
+                            DevicePathInst\r
+                            );\r
+        gBS->FreePool (TempDevicePath1);\r
+        TempDevicePath1 = TempDevicePath2;\r
+      }\r
+    }\r
+\r
+    gBS->FreePool (DevicePathInst);\r
+    DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
+  }\r
+\r
+  if (Delete) {\r
+    *NewDevicePath = TempDevicePath1;\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  return EFI_NOT_FOUND;\r
+}\r
+\r
+EFI_STATUS\r
+ConPlatformUpdateDeviceVariable (\r
+  IN  CHAR16                    *VariableName,\r
+  IN  EFI_DEVICE_PATH_PROTOCOL  *DevicePath,\r
+  IN  CONPLATFORM_VAR_OPERATION Operation\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+\r
+Arguments:\r
+\r
+Returns:\r
+\r
+  None\r
+\r
+--*/\r
+{\r
+  EFI_STATUS                Status;\r
+  EFI_DEVICE_PATH_PROTOCOL  *VariableDevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL  *NewVariableDevicePath;\r
+\r
+  VariableDevicePath    = NULL;\r
+  NewVariableDevicePath = NULL;\r
+\r
+  //\r
+  // Get Variable according to variable name.\r
+  // The memory for Variable is allocated within ConPlatformGetVarible(),\r
+  // it is the caller's responsibility to free the memory before return.\r
+  //\r
+  VariableDevicePath = ConPlatformGetVariable (VariableName);\r
+\r
+  if (Operation != DELETE) {\r
+\r
+    Status = ConPlatformMatchDevicePaths (\r
+              VariableDevicePath,\r
+              DevicePath,\r
+              NULL,\r
+              FALSE\r
+              );\r
+\r
+    if ((Operation == CHECK) || (!EFI_ERROR (Status))) {\r
+      //\r
+      // The device path is already in the variable\r
+      //\r
+      gBS->FreePool (VariableDevicePath);\r
+\r
+      return Status;\r
+    }\r
+    //\r
+    // The device path is not in variable. Append DevicePath to the\r
+    // environment variable that is a multi-instance device path.\r
+    //\r
+    Status = EFI_SUCCESS;\r
+    NewVariableDevicePath = AppendDevicePathInstance (\r
+                              VariableDevicePath,\r
+                              DevicePath\r
+                              );\r
+    if (NewVariableDevicePath == NULL) {\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+    }\r
+\r
+  } else {\r
+    //\r
+    // Remove DevicePath from the environment variable that\r
+    // is a multi-instance device path.\r
+    //\r
+    Status = ConPlatformMatchDevicePaths (\r
+              VariableDevicePath,\r
+              DevicePath,\r
+              &NewVariableDevicePath,\r
+              TRUE\r
+              );\r
+  }\r
+\r
+  gBS->FreePool (VariableDevicePath);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Status = gRT->SetVariable (\r
+                  VariableName,\r
+                  &gEfiGlobalVariableGuid,\r
+                  EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
+                  GetDevicePathSize (NewVariableDevicePath),\r
+                  NewVariableDevicePath\r
+                  );\r
+\r
+  gBS->FreePool (NewVariableDevicePath);\r
+\r
+  return Status;\r
+}\r
+\r
+BOOLEAN\r
+IsHotPlugDevice (\r
+  EFI_HANDLE    DriverBindingHandle,\r
+  EFI_HANDLE    ControllerHandle\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  //\r
+  // HotPlugDeviceGuid indicates ControllerHandle stands for a hot plug device.\r
+  //\r
+  Status = gBS->OpenProtocol (\r
+                  ControllerHandle,\r
+                  &gEfiHotPlugDeviceGuid,\r
+                  NULL,\r
+                  DriverBindingHandle,\r
+                  ControllerHandle,\r
+                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return FALSE;\r
+  }\r
+\r
+  return TRUE;\r
+}\r