]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Code Scrub for DxePlatDriOverLib.
authorxli24 <xli24@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 6 Nov 2008 07:47:11 +0000 (07:47 +0000)
committerxli24 <xli24@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 6 Nov 2008 07:47:11 +0000 (07:47 +0000)
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@6410 6f19259b-4bc3-4df7-8a09-765794883524

MdeModulePkg/Library/DxePlatDriOverLib/DxePlatDriOverLib.inf
MdeModulePkg/Library/DxePlatDriOverLib/PlatDriOver.h
MdeModulePkg/Library/DxePlatDriOverLib/PlatDriOverLib.c

index cb0eb29b8b827a5bf3c4b12cc4d82770b7643123..9effc3ed4c27924522f06d6438afbeb20ce83b8a 100644 (file)
@@ -1,8 +1,7 @@
 #/** @file\r
-# Component name for module PlatDriOverLib\r
+# Instance of Platform Driver Override Library.\r
 #\r
-# FIX ME!\r
-# Copyright (c) 2007, Intel Corporation. All rights reserved.\r
+# Copyright (c) 2007 - 2008, Intel Corporation. All rights reserved.\r
 #\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
@@ -21,7 +20,7 @@
   FILE_GUID                      = 8bd8d711-2736-46d7-8c81-5de68e0a9e88\r
   MODULE_TYPE                    = DXE_DRIVER\r
   VERSION_STRING                 = 1.0\r
-  LIBRARY_CLASS                  = PlatDriOverLib\r
+  LIBRARY_CLASS                  = PlatDriOverLib|DXE_DRIVER UEFI_DRIVER UEFI_APPLICATION\r
   EDK_RELEASE_VERSION            = 0x00020000\r
   EFI_SPECIFICATION_VERSION      = 0x00020000\r
 \r
index b63b2c8f1631f6f76aecaf78ff7019a851b86891..db2d6e5dab1eb00930c4c2fc6c58c131598a31af 100644 (file)
@@ -1,20 +1,14 @@
 /** @file\r
+  Internal include file for Platform Driver Override Library implementation.\r
+  \r
+  Copyright (c) 2007 - 2008, 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
-Copyright (c) 2007, 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
-    PlatDriOver.h\r
-\r
-Abstract:\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
 **/\r
 \r
@@ -46,17 +40,20 @@ Abstract:
 \r
 \r
 #define PLATFORM_OVERRIDE_ITEM_SIGNATURE      EFI_SIGNATURE_32('p','d','o','i')\r
- typedef struct _PLATFORM_OVERRIDE_ITEM{\r
+ typedef struct _PLATFORM_OVERRIDE_ITEM {\r
   UINTN                                 Signature;\r
   LIST_ENTRY                            Link;\r
   UINT32                                DriverInfoNum;\r
   EFI_DEVICE_PATH_PROTOCOL              *ControllerDevicePath;\r
-  LIST_ENTRY                            DriverInfoList;  //DRIVER_IMAGE_INFO List\r
+  ///\r
+  /// List of DRIVER_IMAGE_INFO\r
+  ///\r
+  LIST_ENTRY                            DriverInfoList;\r
   EFI_HANDLE                            LastReturnedImageHandle;\r
 } PLATFORM_OVERRIDE_ITEM;\r
 \r
 #define DRIVER_IMAGE_INFO_SIGNATURE           EFI_SIGNATURE_32('p','d','i','i')\r
-typedef struct _DRIVER_IMAGE_INFO{\r
+typedef struct _DRIVER_IMAGE_INFO {\r
   UINTN                                 Signature;\r
   LIST_ENTRY                            Link;\r
   EFI_HANDLE                            ImageHandle;\r
@@ -72,50 +69,121 @@ typedef struct _DEVICE_PATH_STACK_ITEM{
   EFI_DEVICE_PATH_PROTOCOL              *DevicePath;\r
 } DEVICE_PATH_STACK_ITEM;\r
 \r
+/**\r
+  Push a controller device path into a globle device path list\r
+\r
+  @param  DevicePath     The controller device path to push into stack\r
+\r
+  @retval EFI_SUCCESS    Device path successfully pushed into the stack.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PushDevPathStack (\r
   IN  EFI_DEVICE_PATH_PROTOCOL    *DevicePath\r
   );\r
 \r
+/**\r
+  Pop a controller device path from a globle device path list\r
+\r
+  @param  DevicePath     The controller device path popped from stack\r
+\r
+  @retval EFI_SUCCESS    Controller device path successfully popped.\r
+  @retval EFI_NOT_FOUND  Stack is empty.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PopDevPathStack (\r
   OUT  EFI_DEVICE_PATH_PROTOCOL    **DevicePath\r
   );\r
 \r
+/**\r
+  Check whether a controller device path is in a globle device path list\r
+\r
+  @param  DevicePath     The controller device path to check\r
+\r
+  @retval TRUE           DevicePath exists in the stack.\r
+  @retval FALSE          DevicePath does not exist in the stack.\r
+\r
+**/\r
 BOOLEAN\r
 EFIAPI\r
 CheckExistInStack (\r
   IN  EFI_DEVICE_PATH_PROTOCOL    *DevicePath\r
   );\r
 \r
+/**\r
+  According to a file GUID, check a Fv file device path is valid. If it is invalid,\r
+  try to return the valid device path.\r
+  FV address maybe changes for memory layout adjust from time to time, use this funciton\r
+  could promise the Fv file device path is right.\r
+\r
+  @param  DevicePath               On input, the FV file device path to check\r
+                                   On output, the updated valid FV file device path\r
+  @param  FileGuid                 The FV file GUID\r
+  @param  CallerImageHandle        Image handle of the caller\r
+\r
+  @retval EFI_INVALID_PARAMETER    the input DevicePath or FileGuid is invalid\r
+                                   parameter\r
+  @retval EFI_UNSUPPORTED          the input DevicePath does not contain FV file\r
+                                   GUID at all\r
+  @retval EFI_ALREADY_STARTED      the input DevicePath has pointed to FV file, it\r
+                                   is valid\r
+  @retval EFI_SUCCESS              Successfully updated the invalid DevicePath,\r
+                                   and return the updated device path in DevicePath\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 UpdateFvFileDevicePath (\r
-  IN  OUT EFI_DEVICE_PATH_PROTOCOL      ** DevicePath,\r
+  IN  OUT EFI_DEVICE_PATH_PROTOCOL      **DevicePath,\r
   IN  EFI_GUID                          *FileGuid,\r
   IN  EFI_HANDLE                        CallerImageHandle\r
   );\r
 \r
+/**\r
+  Read the EFI variable (VendorGuid/Name) and return a dynamically allocated\r
+  buffer, and the size of the buffer. If failure return NULL.\r
+\r
+  @param  Name                     String part of EFI variable name\r
+  @param  VendorGuid               GUID part of EFI variable name\r
+  @param  VariableSize             Returns the size of the EFI variable that was\r
+                                   read\r
+\r
+  @return Dynamically allocated memory that contains a copy of the EFI variable.\r
+          Caller is responsible freeing the buffer.\r
+  @retval NULL                     Variable was not read\r
+\r
+**/\r
 VOID *\r
+EFIAPI\r
 GetVariableAndSize (\r
   IN  CHAR16              *Name,\r
   IN  EFI_GUID            *VendorGuid,\r
   OUT UINTN               *VariableSize\r
   );\r
 \r
+/**\r
+  This function will create all handles associate with every device\r
+  path node. If the handle associate with one device path node can not\r
+  be created success, then still give one chance to do the dispatch,\r
+  which load the missing drivers if possible.\r
+\r
+  @param  DevicePathToConnect      The device path which will be connected, it can\r
+                                   be a multi-instance device path\r
+\r
+  @retval EFI_SUCCESS              All handles associate with every device path\r
+                                   node have been created\r
+  @retval EFI_OUT_OF_RESOURCES     There is no resource to create new handles\r
+  @retval EFI_NOT_FOUND            Create the handle associate with one device\r
+                                   path node failed\r
+\r
+**/\r
 EFI_STATUS\r
+EFIAPI\r
 ConnectDevicePath (\r
   IN EFI_DEVICE_PATH_PROTOCOL  *DevicePathToConnect\r
   );\r
 \r
-EFI_STATUS\r
-BdsConnectDeviceByPciClassType (\r
-  UINT8     ClassType,\r
-  UINT8     SubClassCode,\r
-  UINT8     PI,\r
-  BOOLEAN   Recursive\r
-  );\r
-\r
 #endif\r
index e6f37c8a5570c4a3e8b1ff24cac9a6e5f71e4917..21bfb8a7c288b771ec1c71cb3554c5f61e57c115 100644 (file)
@@ -1,20 +1,14 @@
 /** @file\r
+  Implementation of Platform Driver Override Library.\r
 \r
-Copyright (c) 2007, 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
-    PlatDriOverLib.c\r
-\r
-Abstract:\r
+  Copyright (c) 2007 - 2008, 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
 **/\r
 \r
@@ -24,15 +18,17 @@ LIST_ENTRY   mDevicePathStack = INITIALIZE_LIST_HEAD_VARIABLE (mDevicePathStack)
 \r
 \r
 /**\r
-  Install the Platform Driver Override Protocol, and ensure there is only one Platform Driver Override Protocol\r
-  in the system.\r
+  Installs the Platform Driver Override Protocol.\r
+\r
+  This function installs the Platform Driver Override Protocol, and ensure there is only one\r
+  Platform Driver Override Protocol in the system.\r
 \r
   @param  gPlatformDriverOverride  PlatformDriverOverride protocol interface which\r
                                    needs to be installed\r
 \r
+  @retval EFI_SUCCESS              The protocol is installed successfully.\r
   @retval EFI_ALREADY_STARTED      There has been a Platform Driver Override\r
                                    Protocol in the system, cannot install it again.\r
-  @retval Other                    Returned by InstallProtocolInterface\r
 \r
 **/\r
 EFI_STATUS\r
@@ -47,9 +43,9 @@ InstallPlatformDriverOverrideProtocol (
   EFI_HANDLE          *HandleBuffer;\r
 \r
   //\r
-  // There will be only one platform driver override protocol in the system\r
-  // If there is another out there, someone is trying to install us again,\r
-  // Fail that scenario.\r
+  // According to UEFI spec, there can be at most a single instance\r
+  // in the system of the EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL.\r
+  // So here we check the existence.\r
   //\r
   Status = gBS->LocateHandleBuffer (\r
                   ByProtocol,\r
@@ -78,17 +74,18 @@ InstallPlatformDriverOverrideProtocol (
                   EFI_NATIVE_INTERFACE,\r
                   gPlatformDriverOverride\r
                   );\r
-  return Status;\r
+  ASSERT_EFI_ERROR (Status);\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 \r
 /**\r
-  Free all the mapping database memory resource and initialize the mapping list entry\r
+  Free all the mapping database memory resource and initialize the mapping list entry.\r
 \r
   @param  MappingDataBase          Mapping database list entry pointer\r
 \r
-  @retval EFI_INVALID_PARAMETER    mapping database list entry is NULL\r
-  @retval EFI_SUCCESS              Free success\r
+  @retval EFI_SUCCESS              Mapping database successfully freed\r
+  @retval EFI_INVALID_PARAMETER    MappingDataBase is NULL\r
 \r
 **/\r
 EFI_STATUS\r
@@ -106,20 +103,20 @@ FreeMappingDatabase (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  OverrideItemListIndex = MappingDataBase->ForwardLink;\r
-  while (OverrideItemListIndex != MappingDataBase){\r
+  OverrideItemListIndex = GetFirstNode (MappingDataBase);\r
+  while (!IsNull (MappingDataBase, OverrideItemListIndex)) {\r
     OverrideItem = CR(OverrideItemListIndex, PLATFORM_OVERRIDE_ITEM, Link, PLATFORM_OVERRIDE_ITEM_SIGNATURE);\r
     //\r
     // Free PLATFORM_OVERRIDE_ITEM.ControllerDevicePath[]\r
     //\r
     if (OverrideItem->ControllerDevicePath != NULL){\r
-      FreePool(OverrideItem->ControllerDevicePath);\r
+      FreePool (OverrideItem->ControllerDevicePath);\r
     }\r
 \r
-    ImageInfoListIndex =  OverrideItem->DriverInfoList.ForwardLink;\r
-    while (ImageInfoListIndex != &OverrideItem->DriverInfoList){\r
+    ImageInfoListIndex = GetFirstNode (&OverrideItem->DriverInfoList);\r
+    while (!IsNull (&OverrideItem->DriverInfoList, ImageInfoListIndex)) {\r
       //\r
-      // Free all DRIVER_IMAGE_INFO.DriverImagePath[]\r
+      // Free DRIVER_IMAGE_INFO.DriverImagePath[]\r
       //\r
       DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);\r
       if (DriverImageInfo->DriverImagePath != NULL) {\r
@@ -128,14 +125,14 @@ FreeMappingDatabase (
       //\r
       // Free DRIVER_IMAGE_INFO itself\r
       //\r
-      ImageInfoListIndex = ImageInfoListIndex->ForwardLink;\r
+      ImageInfoListIndex = GetNextNode (&OverrideItem->DriverInfoList, ImageInfoListIndex);\r
       RemoveEntryList (&DriverImageInfo->Link);\r
       FreePool (DriverImageInfo);\r
     }\r
     //\r
     // Free PLATFORM_OVERRIDE_ITEM itself\r
     //\r
-    OverrideItemListIndex =  OverrideItemListIndex->ForwardLink;\r
+    OverrideItemListIndex = GetNextNode (MappingDataBase, OverrideItemListIndex);\r
     RemoveEntryList (&OverrideItem->Link);\r
     FreePool (OverrideItem);\r
   }\r
@@ -146,12 +143,14 @@ FreeMappingDatabase (
 \r
 \r
 /**\r
+  Create the mappging database according to variable.\r
+\r
   Read the environment variable(s) that contain the override mappings from Controller Device Path to\r
   a set of Driver Device Paths, and create the mapping database in memory with those variable info.\r
   VariableLayout{\r
   //\r
   // NotEnd indicate whether the variable is the last one, and has no subsequent variable need to load.\r
-  // Each variable has MaximumVariableSize limitation, so  we maybe need multi variables to store\r
+  // Each variable has MaximumVariableSize limitation, so we maybe need multiple variables to store\r
   // large mapping infos.\r
   // The variable(s) name rule is PlatDriOver, PlatDriOver1, PlatDriOver2, ....\r
   //\r
@@ -175,29 +174,13 @@ FreeMappingDatabase (
   EFI_DEVICE_PATH_PROTOCOL       DriverDevicePath[];\r
   ......\r
   }\r
-  typedef struct _PLATFORM_OVERRIDE_ITEM{\r
-  UINTN                          Signature;                  //EFI_SIGNATURE_32('p','d','o','i')\r
-  LIST_ENTRY                     Link;\r
-  UINT32                         DriverInfoNum;\r
-  EFI_DEVICE_PATH_PROTOCOL       *ControllerDevicePath;\r
-  LIST_ENTRY                     DriverInfoList;         //DRIVER_IMAGE_INFO List\r
-  } PLATFORM_OVERRIDE_ITEM;\r
-  typedef struct _DRIVER_IMAGE_INFO{\r
-  UINTN                          Signature;                  //EFI_SIGNATURE_32('p','d','i','i')\r
-  LIST_ENTRY                     Link;\r
-  EFI_HANDLE                     ImageHandle;\r
-  EFI_DEVICE_PATH_PROTOCOL       *DriverImagePath;\r
-  BOOLEAN                        UnLoadable;\r
-  BOOLEAN                        UnStartable;\r
-  } DRIVER_IMAGE_INFO;\r
 \r
   @param  MappingDataBase          Mapping database list entry pointer\r
 \r
+  @retval EFI_SUCCESS              Create the mapping database in memory successfully\r
   @retval EFI_INVALID_PARAMETER    MappingDataBase pointer is null\r
   @retval EFI_NOT_FOUND            Cannot find the 'PlatDriOver' NV variable\r
   @retval EFI_VOLUME_CORRUPTED     The found NV variable is corrupted\r
-  @retval EFI_SUCCESS              Create the mapping database in memory\r
-                                   successfully\r
 \r
 **/\r
 EFI_STATUS\r
@@ -225,38 +208,46 @@ InitOverridesMapping (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  VariableNum = 0;\r
-  Corrupted = FALSE;\r
   //\r
   // Check the environment variable(s) that contain the override mappings .\r
   //\r
   VariableBuffer = GetVariableAndSize (L"PlatDriOver", &gEfiOverrideVariableGuid, &BufferSize);\r
   ASSERT ((UINTN) VariableBuffer % sizeof(UINTN) == 0);\r
-  VariableNum ++;\r
   if (VariableBuffer == NULL) {\r
     return EFI_NOT_FOUND;\r
   }\r
 \r
+  //\r
+  // Traverse all variables.\r
+  //\r
+  VariableNum = 1;\r
+  Corrupted = FALSE;\r
   do {\r
     VariableIndex = VariableBuffer;\r
     NotEnd = *(UINT32*) VariableIndex;\r
+    //\r
+    // Traverse the entries containing the mapping that Controller Device Path\r
+    // to a set of Driver Device Paths within this variable.\r
+    //\r
     VariableIndex = VariableIndex + sizeof (UINT32);\r
     while (VariableIndex < ((UINT8 *)VariableBuffer + BufferSize)) {\r
-      OverrideItem = AllocateZeroPool (sizeof (PLATFORM_OVERRIDE_ITEM));\r
-      ASSERT (OverrideItem != NULL);\r
-      OverrideItem->Signature = PLATFORM_OVERRIDE_ITEM_SIGNATURE;\r
-      InitializeListHead (&OverrideItem->DriverInfoList);\r
       //\r
-      // Check SIGNATURE\r
+      // Check signature of this entry\r
       //\r
       Signature = *(UINT32 *) VariableIndex;\r
-      VariableIndex = VariableIndex + sizeof (UINT32);\r
       if (Signature != PLATFORM_OVERRIDE_ITEM_SIGNATURE) {\r
-        FreePool (OverrideItem);\r
         Corrupted = TRUE;\r
         break;\r
       }\r
       //\r
+      // Create PLATFORM_OVERRIDE_ITEM for this mapping\r
+      //\r
+      OverrideItem = AllocateZeroPool (sizeof (PLATFORM_OVERRIDE_ITEM));\r
+      ASSERT (OverrideItem != NULL);\r
+      OverrideItem->Signature = PLATFORM_OVERRIDE_ITEM_SIGNATURE;\r
+      InitializeListHead (&OverrideItem->DriverInfoList);\r
+      VariableIndex = VariableIndex + sizeof (UINT32);\r
+      //\r
       // Get DriverNum\r
       //\r
       DriverNumber = *(UINT32*) VariableIndex;\r
@@ -277,6 +268,9 @@ InitOverridesMapping (
       // Get all DriverDevicePath[]\r
       //\r
       for (Index = 0; Index < DriverNumber; Index++) {\r
+        //\r
+        // Create DRIVER_IMAGE_INFO for this DriverDevicePath[]\r
+        //\r
         DriverImageInfo = AllocateZeroPool (sizeof (DRIVER_IMAGE_INFO));\r
         ASSERT (DriverImageInfo != NULL);\r
         DriverImageInfo->Signature = DRIVER_IMAGE_INFO_SIGNATURE;\r
@@ -301,14 +295,14 @@ InitOverridesMapping (
     }\r
 \r
     //\r
-    // If has other variable(PlatDriOver1, PlatDriOver2, PlatDriOver3.....), get them.\r
-    // NotEnd indicate whether current variable is the end variable.\r
+    // If there are additional variables (PlatDriOver1, PlatDriOver2, PlatDriOver3.....), get them.\r
+    // NotEnd indicates whether current variable is the end variable.\r
     //\r
     if (NotEnd != 0) {\r
       UnicodeSPrint (OverrideVariableName, sizeof (OverrideVariableName), L"PlatDriOver%d", VariableNum);\r
       VariableBuffer = GetVariableAndSize (OverrideVariableName, &gEfiOverrideVariableGuid, &BufferSize);\r
       ASSERT ((UINTN) VariableBuffer % sizeof(UINTN) == 0);\r
-      VariableNum ++;\r
+      VariableNum++;\r
       if (VariableBuffer == NULL) {\r
         FreeMappingDatabase (MappingDataBase);\r
         return EFI_VOLUME_CORRUPTED;\r
@@ -322,10 +316,9 @@ InitOverridesMapping (
 \r
 \r
 /**\r
-  Calculate the needed size in NV variable for recording a specific PLATFORM_OVERRIDE_ITEM info\r
+  Calculate the needed size in NV variable for recording a specific PLATFORM_OVERRIDE_ITEM info.\r
 \r
-  @param  OverrideItemListIndex    a list entry point to a specific\r
-                                   PLATFORM_OVERRIDE_ITEM\r
+  @param  OverrideItemListIndex    Pointer to the list of a specific PLATFORM_OVERRIDE_ITEM\r
 \r
   @return The needed size number\r
 \r
@@ -340,28 +333,31 @@ GetOneItemNeededSize (
   PLATFORM_OVERRIDE_ITEM      *OverrideItem;\r
   LIST_ENTRY                  *ImageInfoListIndex;\r
   DRIVER_IMAGE_INFO           *DriverImageInfo;\r
-\r
+  UINTN                       DevicePathSize;\r
 \r
   NeededSize = 0;\r
   OverrideItem = CR(OverrideItemListIndex, PLATFORM_OVERRIDE_ITEM, Link, PLATFORM_OVERRIDE_ITEM_SIGNATURE);\r
   NeededSize += sizeof (UINT32); //UINT32  SIGNATURE;\r
   NeededSize += sizeof (UINT32); //UINT32  DriverNum;\r
-  NeededSize += GetDevicePathSize (OverrideItem->ControllerDevicePath); // ControllerDevicePath\r
+  DevicePathSize = GetDevicePathSize (OverrideItem->ControllerDevicePath);\r
+  NeededSize += DevicePathSize; // ControllerDevicePath\r
   //\r
   // Align the controller device path\r
   //\r
-  NeededSize += ((sizeof(UINT32) - ((UINTN) GetDevicePathSize (OverrideItem->ControllerDevicePath))) \\r
-                  & (sizeof(UINT32) - 1));\r
-  ImageInfoListIndex =  OverrideItem->DriverInfoList.ForwardLink;\r
-  while (ImageInfoListIndex != &OverrideItem->DriverInfoList){\r
+  NeededSize += ((sizeof(UINT32) - DevicePathSize) & (sizeof(UINT32) - 1));\r
+  //\r
+  // Traverse the Driver Info List of this Override Item\r
+  //\r
+  ImageInfoListIndex = GetFirstNode (&OverrideItem->DriverInfoList);\r
+  while (!IsNull (&OverrideItem->DriverInfoList, ImageInfoListIndex)) {\r
     DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);\r
-    NeededSize += GetDevicePathSize (DriverImageInfo->DriverImagePath); //DriverDevicePath\r
+    DevicePathSize = GetDevicePathSize (DriverImageInfo->DriverImagePath);\r
+    NeededSize += DevicePathSize; //DriverDevicePath\r
     //\r
     // Align the driver image device path\r
     //\r
-    NeededSize += ((sizeof(UINT32) - ((UINTN) GetDevicePathSize (DriverImageInfo->DriverImagePath))) \\r
-                    & (sizeof(UINT32) - 1));\r
-    ImageInfoListIndex = ImageInfoListIndex->ForwardLink;\r
+    NeededSize += ((sizeof(UINT32) - DevicePathSize) & (sizeof(UINT32) - 1));\r
+    ImageInfoListIndex = GetNextNode (&OverrideItem->DriverInfoList, ImageInfoListIndex);\r
   }\r
 \r
   return NeededSize;\r
@@ -370,12 +366,12 @@ GetOneItemNeededSize (
 \r
 \r
 /**\r
-  Save the memory mapping database into NV environment variable(s)\r
+  Save the memory mapping database into NV environment variable(s).\r
 \r
   @param  MappingDataBase          Mapping database list entry pointer\r
 \r
-  @retval EFI_INVALID_PARAMETER    MappingDataBase pointer is null\r
   @retval EFI_SUCCESS              Save memory mapping database successfully\r
+  @retval EFI_INVALID_PARAMETER    MappingDataBase pointer is null\r
 \r
 **/\r
 EFI_STATUS\r
@@ -396,7 +392,6 @@ SaveOverridesMapping (
   LIST_ENTRY                  *ItemIndex;\r
   LIST_ENTRY                  *ImageInfoListIndex;\r
   UINTN                       VariableNeededSize;\r
-  UINTN                       SavedSize;\r
   UINT64                      MaximumVariableStorageSize;\r
   UINT64                      RemainingVariableStorageSize;\r
   UINT64                      MaximumVariableSize;\r
@@ -406,8 +401,9 @@ SaveOverridesMapping (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (MappingDataBase->ForwardLink == MappingDataBase) {\r
+  if (IsListEmpty (MappingDataBase)) {\r
     Status = DeleteOverridesVariables ();\r
+    ASSERT_EFI_ERROR (Status);\r
     return EFI_SUCCESS;\r
   }\r
 \r
@@ -422,18 +418,24 @@ SaveOverridesMapping (
           );\r
 \r
   NumIndex = 0;\r
-  OverrideItemListIndex = MappingDataBase->ForwardLink;\r
-  while (OverrideItemListIndex != MappingDataBase) {\r
+  OverrideItemListIndex = GetFirstNode (MappingDataBase);\r
+  while (!IsNull (MappingDataBase, OverrideItemListIndex)) {\r
     //\r
-    // Try to find the most proper variable size which <= MaximumVariableSize, but can contain mapping info as much as possible\r
+    // Try to find the most proper variable size which <= MaximumVariableSize,\r
+    // but can contain mapping info as much as possible\r
     //\r
-    VariableNeededSize = 0;\r
-    VariableNeededSize += sizeof (UINT32); //BOOLEAN  NotEnd;\r
+    VariableNeededSize = sizeof (UINT32); // NotEnd;\r
     ItemIndex = OverrideItemListIndex;\r
     NotEnd = FALSE;\r
-\r
-    while (ItemIndex != MappingDataBase){\r
+    //\r
+    // Traverse all PLATFORM_OVERRIDE_ITEMs and get the total size.\r
+    //\r
+    while (!IsNull (MappingDataBase, ItemIndex)) {\r
       OneItemNeededSize = GetOneItemNeededSize (ItemIndex);\r
+      //\r
+      // If the total size exceeds the MaximumVariableSize, then we must use\r
+      // multiple variables.\r
+      //\r
       if ((VariableNeededSize +\r
            OneItemNeededSize +\r
            sizeof (VARIABLE_HEADER) +\r
@@ -444,11 +446,11 @@ SaveOverridesMapping (
         break;\r
       }\r
 \r
-      VariableNeededSize += GetOneItemNeededSize (ItemIndex);\r
-      ItemIndex =  ItemIndex->ForwardLink;\r
+      VariableNeededSize += OneItemNeededSize;\r
+      ItemIndex = GetNextNode (MappingDataBase, ItemIndex);\r
     }\r
 \r
-    if (NotEnd) {\r
+    if (NotEnd != 0) {\r
       if (VariableNeededSize == sizeof (UINT32)) {\r
         //\r
         // If an individual EFI variable cannot contain a single Item, return error\r
@@ -467,52 +469,46 @@ SaveOverridesMapping (
     //\r
     // Fill the variable buffer according to MappingDataBase\r
     //\r
-    SavedSize = 0;\r
     VariableIndex = VariableBuffer;\r
     *(UINT32 *) VariableIndex = NotEnd;\r
-    VariableIndex += sizeof (UINT32); // pass NoEnd\r
-    SavedSize += sizeof (UINT32);\r
+    VariableIndex += sizeof (UINT32); // pass NotEnd\r
     //\r
     // ItemIndex points to the next PLATFORM_OVERRIDE_ITEM which is not covered by VariableNeededSize\r
     //\r
     while (OverrideItemListIndex != ItemIndex){\r
       *(UINT32 *) VariableIndex = PLATFORM_OVERRIDE_ITEM_SIGNATURE;\r
       VariableIndex += sizeof (UINT32); // pass SIGNATURE\r
-      SavedSize += sizeof (UINT32);\r
 \r
       OverrideItem = CR(OverrideItemListIndex, PLATFORM_OVERRIDE_ITEM, Link, PLATFORM_OVERRIDE_ITEM_SIGNATURE);\r
       *(UINT32 *) VariableIndex = OverrideItem->DriverInfoNum;\r
       VariableIndex += sizeof (UINT32); // pass DriverNum\r
-      SavedSize += sizeof (UINT32);\r
 \r
       CopyMem (VariableIndex, OverrideItem->ControllerDevicePath, GetDevicePathSize (OverrideItem->ControllerDevicePath));\r
       VariableIndex += GetDevicePathSize (OverrideItem->ControllerDevicePath); // pass ControllerDevicePath\r
-      SavedSize += GetDevicePathSize (OverrideItem->ControllerDevicePath);\r
 \r
       //\r
       // Align the VariableIndex since the controller device path may not be aligned\r
       //\r
-      SavedSize += ((sizeof(UINT32) - ((UINTN) (VariableIndex))) & (sizeof(UINT32) - 1));\r
       VariableIndex += ((sizeof(UINT32) - ((UINTN) (VariableIndex))) & (sizeof(UINT32) - 1));\r
-\r
-      ImageInfoListIndex =  OverrideItem->DriverInfoList.ForwardLink;\r
-      while (ImageInfoListIndex != &OverrideItem->DriverInfoList){\r
+      //\r
+      // Save the Driver Info List of this PLATFORM_OVERRIDE_ITEM\r
+      //\r
+      ImageInfoListIndex = GetFirstNode (&OverrideItem->DriverInfoList);\r
+      while (!IsNull (&OverrideItem->DriverInfoList, ImageInfoListIndex)) {\r
         DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);\r
         CopyMem (VariableIndex, DriverImageInfo->DriverImagePath, GetDevicePathSize (DriverImageInfo->DriverImagePath));\r
         VariableIndex += GetDevicePathSize (DriverImageInfo->DriverImagePath); // pass DriverImageDevicePath\r
-        SavedSize += GetDevicePathSize (DriverImageInfo->DriverImagePath);\r
         //\r
         // Align the VariableIndex since the driver image device path may not be aligned\r
         //\r
-        SavedSize += ((sizeof(UINT32) - ((UINTN) (VariableIndex))) & (sizeof(UINT32) - 1));\r
         VariableIndex += ((sizeof(UINT32) - ((UINTN) (VariableIndex))) & (sizeof(UINT32) - 1));\r
-        ImageInfoListIndex = ImageInfoListIndex->ForwardLink;\r
+        ImageInfoListIndex = GetNextNode (&OverrideItem->DriverInfoList, ImageInfoListIndex);\r
       }\r
 \r
-      OverrideItemListIndex =  OverrideItemListIndex->ForwardLink;\r
+      OverrideItemListIndex =  GetNextNode (MappingDataBase, OverrideItemListIndex);\r
     }\r
 \r
-    ASSERT (SavedSize == VariableNeededSize);\r
+    ASSERT (((UINTN)VariableIndex - (UINTN)VariableBuffer) == VariableNeededSize);\r
 \r
     if (NumIndex == 0) {\r
       UnicodeSPrint (OverrideVariableName, sizeof (OverrideVariableName), L"PlatDriOver");\r
@@ -537,11 +533,14 @@ SaveOverridesMapping (
 }\r
 \r
 /**\r
-  Get the first Binding protocol which has the specific image handle\r
+  Get the first Binding protocol which has the specific image handle.\r
 \r
-  @param  Image          Image handle\r
+  @param  ImageHandle          The Image handle\r
+  @param  BindingHandle        The BindingHandle of the found Driver Binding protocol.\r
+                               If Binding protocol is not found, it is set to NULL. \r
 \r
-  @return Pointer into the Binding Protocol interface\r
+  @return                      Pointer into the Binding Protocol interface\r
+  @retval NULL                 The paramter is not valid or the binding protocol is not found.\r
 \r
 **/\r
 EFI_DRIVER_BINDING_PROTOCOL *\r
@@ -561,7 +560,7 @@ GetBindingProtocolFromImageHandle (
     return NULL;\r
   }\r
   //\r
-  // Get all driver which support binding protocol in second page\r
+  // Get all drivers which support driver binding protocol\r
   //\r
   DriverBindingHandleCount  = 0;\r
   Status = gBS->LocateHandleBuffer (\r
@@ -576,7 +575,7 @@ GetBindingProtocolFromImageHandle (
   }\r
 \r
   for (Index = 0; Index < DriverBindingHandleCount; Index++) {\r
-    DriverBindingInterface =NULL;\r
+    DriverBindingInterface = NULL;\r
     Status = gBS->OpenProtocol (\r
                     DriverBindingHandleBuffer[Index],\r
                     &gEfiDriverBindingProtocolGuid,\r
@@ -596,15 +595,16 @@ GetBindingProtocolFromImageHandle (
     }\r
   }\r
 \r
+  //\r
+  // If no Driver Binding Protocol instance is found\r
+  //\r
   FreePool (DriverBindingHandleBuffer);\r
   *BindingHandle = NULL;\r
   return NULL;\r
 }\r
 \r
 /**\r
-  return the current TPL, copied from the EDKII glue lib\r
-\r
-  @param  VOID\r
+  Return the current TPL.\r
 \r
   @return Current TPL\r
 \r
@@ -624,17 +624,18 @@ GetCurrentTpl (
 \r
 \r
 /**\r
-  Retrieves the image handle of the platform override driver for a controller in the system from the memory mapping database.\r
+  Retrieves the image handle of the platform override driver for a controller in\r
+  the system from the memory mapping database.\r
 \r
   @param  This                     A pointer to the\r
                                    EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL instance.\r
   @param  ControllerHandle         The device handle of the controller to check if\r
                                    a driver override exists.\r
-  @param  DriverImageHandle        On output, a pointer to the next driver handle.\r
+  @param  DriverImageHandle        On input, the previously returnd driver image handle.\r
+                                   On output, a pointer to the next driver handle.\r
                                    Passing in a pointer to NULL, will return the\r
                                    first driver handle for ControllerHandle.\r
-  @param  MappingDataBase          MappingDataBase - Mapping database list entry\r
-                                   pointer\r
+  @param  MappingDataBase          Mapping database list entry pointer\r
   @param  CallerImageHandle        The caller driver's image handle, for\r
                                    UpdateFvFileDevicePath use.\r
 \r
@@ -652,10 +653,10 @@ GetCurrentTpl (
 EFI_STATUS\r
 EFIAPI\r
 GetDriverFromMapping (\r
-  IN EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL              * This,\r
+  IN EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL              *This,\r
   IN     EFI_HANDLE                                     ControllerHandle,\r
-  IN OUT EFI_HANDLE                                     * DriverImageHandle,\r
-  IN     LIST_ENTRY                                     * MappingDataBase,\r
+  IN OUT EFI_HANDLE                                     *DriverImageHandle,\r
+  IN     LIST_ENTRY                                     *MappingDataBase,\r
   IN     EFI_HANDLE                                     CallerImageHandle\r
   )\r
 {\r
@@ -686,12 +687,14 @@ GetDriverFromMapping (
   if (ControllerHandle == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-\r
+  //\r
+  // Get the device path of ControllerHandle\r
+  //\r
   Status = gBS->HandleProtocol (\r
-              ControllerHandle,\r
-              &gEfiDevicePathProtocolGuid,\r
-              (VOID **) &ControllerDevicePath\r
-              );\r
+                  ControllerHandle,\r
+                  &gEfiDevicePathProtocolGuid,\r
+                  (VOID **) &ControllerDevicePath\r
+                  );\r
   if (EFI_ERROR (Status) || ControllerDevicePath == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
@@ -701,23 +704,23 @@ GetDriverFromMapping (
   //\r
   OverrideItem = NULL;\r
   ControllerFound = FALSE;\r
-  OverrideItemListIndex = MappingDataBase->ForwardLink;\r
-  while (OverrideItemListIndex != MappingDataBase){\r
+  DevicePathSize = GetDevicePathSize (ControllerDevicePath);\r
+\r
+  OverrideItemListIndex = GetFirstNode (MappingDataBase);\r
+  while (!IsNull (MappingDataBase, OverrideItemListIndex)) {\r
     OverrideItem = CR(OverrideItemListIndex, PLATFORM_OVERRIDE_ITEM, Link, PLATFORM_OVERRIDE_ITEM_SIGNATURE);\r
-    DevicePathSize = GetDevicePathSize (ControllerDevicePath);\r
     if (DevicePathSize == GetDevicePathSize (OverrideItem->ControllerDevicePath)) {\r
       if (CompareMem (\r
             ControllerDevicePath,\r
             OverrideItem->ControllerDevicePath,\r
-            GetDevicePathSize (OverrideItem->ControllerDevicePath)\r
+            DevicePathSize\r
             ) == 0\r
           ) {\r
         ControllerFound = TRUE;\r
         break;\r
       }\r
-\r
     }\r
-    OverrideItemListIndex =  OverrideItemListIndex->ForwardLink;\r
+    OverrideItemListIndex = GetNextNode (MappingDataBase, OverrideItemListIndex);\r
   }\r
 \r
   if (!ControllerFound) {\r
@@ -733,12 +736,12 @@ GetDriverFromMapping (
     }\r
   }\r
   //\r
-  // The GetDriverPath() maybe called recursively, because it use ConnectDevicePath() internally,\r
+  // The GetDriverPath() may be called recursively, because it use ConnectDevicePath() internally,\r
   //  so should check whether there is a dead loop.\r
   //  Here use a controller device path stack to record all processed controller device path during a GetDriverPath() call,\r
-  //  and check the  controller device path whether appear again during the  GetDriverPath() call.\r
+  //  and check the controller device path whether appear again during the GetDriverPath() call.\r
   //\r
-  if (CheckExistInStack(OverrideItem->ControllerDevicePath)) {\r
+  if (CheckExistInStack (OverrideItem->ControllerDevicePath)) {\r
     //\r
     // There is a dependecy dead loop if the ControllerDevicePath appear in stack twice\r
     //\r
@@ -749,8 +752,8 @@ GetDriverFromMapping (
   //\r
   // Check every override driver, try to load and start them\r
   //\r
-  ImageInfoListIndex =  OverrideItem->DriverInfoList.ForwardLink;\r
-  while (ImageInfoListIndex != &OverrideItem->DriverInfoList){\r
+  ImageInfoListIndex = GetFirstNode (&OverrideItem->DriverInfoList);\r
+  while (!IsNull (&OverrideItem->DriverInfoList, ImageInfoListIndex)) {\r
     DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);\r
     if (DriverImageInfo->ImageHandle == NULL) {\r
       //\r
@@ -759,12 +762,14 @@ GetDriverFromMapping (
       if ((!DriverImageInfo->UnLoadable) && ((!DriverImageInfo->UnStartable))) {\r
         TempDriverImagePath = DriverImageInfo->DriverImagePath;\r
         //\r
-        // If the image device path contain a FV node, check the Fv file device path is valid. If it is invalid, try to return the valid device path.\r
-        // FV address maybe changes for memory layout adjust from time to time, use this funciton could promise the Fv file device path is right.\r
+        // If the image device path contains an FV node, check the FV file device path is valid.\r
+        // If it is invalid, try to return the valid device path.\r
+        // FV address maybe changes for memory layout adjust from time to time,\r
+        // use this funciton could promise the FV file device path is right.\r
         //\r
         Status = UpdateFvFileDevicePath (&TempDriverImagePath, NULL, CallerImageHandle);\r
         if (!EFI_ERROR (Status)) {\r
-          FreePool(DriverImageInfo->DriverImagePath);\r
+          FreePool (DriverImageInfo->DriverImagePath);\r
           DriverImageInfo->DriverImagePath = TempDriverImagePath;\r
         }\r
         //\r
@@ -789,13 +794,13 @@ GetDriverFromMapping (
           //\r
           LoadedImageDevicePath = NULL;\r
           Status = gBS->HandleProtocol (\r
-                              ImageHandleBuffer[Index],\r
-                              &gEfiLoadedImageDevicePathProtocolGuid,\r
-                              (VOID **) &LoadedImageDevicePath\r
-                              );\r
+                          ImageHandleBuffer[Index],\r
+                          &gEfiLoadedImageDevicePathProtocolGuid,\r
+                          (VOID **) &LoadedImageDevicePath\r
+                          );\r
           if (EFI_ERROR (Status)) {\r
             //\r
-            // Maybe Not all EFI Loaded Image Device Path Protocol existed.\r
+            // Maybe not all EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL existed.\r
             //\r
             continue;\r
           }\r
@@ -817,19 +822,19 @@ GetDriverFromMapping (
         if (ImageFound) {\r
           //\r
           // Find its related driver binding protocol\r
-          // Driver binding handle may be different with its driver's Image handle,\r
+          // Driver binding handle may be different with its driver's Image Handle.\r
           //\r
           DriverBindingHandle = NULL;\r
           DriverBinding = GetBindingProtocolFromImageHandle (\r
-                                      ImageHandleBuffer[Index],\r
-                                      &DriverBindingHandle\r
-                                      );\r
+                            ImageHandleBuffer[Index],\r
+                            &DriverBindingHandle\r
+                            );\r
           ASSERT (DriverBinding != NULL);\r
           DriverImageInfo->ImageHandle = ImageHandleBuffer[Index];\r
         } else if (GetCurrentTpl() <= TPL_CALLBACK){\r
           //\r
-          // The driver image has not been loaded and started, need try to load and start it now\r
-          // Try to connect all device in the driver image path\r
+          // The driver image has not been loaded and started. Try to load and start it now.\r
+          // Try to connect all device in the driver image path.\r
           //\r
           // Note: LoadImage() and  StartImage() should be called under CALLBACK TPL in theory, but\r
           // since many device need to be connected in  CALLBACK level environment( e.g. Usb devices )\r
@@ -838,7 +843,8 @@ GetDriverFromMapping (
           //\r
           Status = ConnectDevicePath (DriverImageInfo->DriverImagePath);\r
           //\r
-          // check whether it points to a PCI Option Rom image, and try to use bus override protocol to get its first option rom image driver\r
+          // check whether it points to a PCI Option Rom image,\r
+          // and try to use bus override protocol to get its first option rom image driver\r
           //\r
           TempDriverImagePath = DriverImageInfo->DriverImagePath;\r
           gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &TempDriverImagePath, &Handle);\r
@@ -846,10 +852,10 @@ GetDriverFromMapping (
           // Get the Bus Specific Driver Override Protocol instance on the Controller Handle\r
           //\r
           Status = gBS->HandleProtocol(\r
-                           Handle,\r
-                           &gEfiBusSpecificDriverOverrideProtocolGuid,\r
-                           (VOID **) &BusSpecificDriverOverride\r
-                           );\r
+                          Handle,\r
+                          &gEfiBusSpecificDriverOverrideProtocolGuid,\r
+                          (VOID **) &BusSpecificDriverOverride\r
+                          );\r
           if (!EFI_ERROR (Status) && (BusSpecificDriverOverride != NULL)) {\r
             ImageHandle = NULL;\r
             Status = BusSpecificDriverOverride->GetDriver (\r
@@ -863,9 +869,9 @@ GetDriverFromMapping (
               //\r
               DriverBindingHandle = NULL;\r
               DriverBinding = GetBindingProtocolFromImageHandle (\r
-                                          ImageHandle,\r
-                                          &DriverBindingHandle\r
-                                          );\r
+                                ImageHandle,\r
+                                &DriverBindingHandle\r
+                                );\r
               ASSERT (DriverBinding != NULL);\r
               DriverImageInfo->ImageHandle = ImageHandle;\r
             }\r
@@ -907,9 +913,9 @@ GetDriverFromMapping (
                 //\r
                 DriverBindingHandle = NULL;\r
                 DriverBinding = GetBindingProtocolFromImageHandle (\r
-                                            ImageHandle,\r
-                                            &DriverBindingHandle\r
-                                            );\r
+                                   ImageHandle,\r
+                                   &DriverBindingHandle\r
+                                   );\r
                 ASSERT (DriverBinding != NULL);\r
                 DriverImageInfo->ImageHandle = ImageHandle;\r
               }\r
@@ -922,7 +928,7 @@ GetDriverFromMapping (
         FreePool (ImageHandleBuffer);\r
       }\r
     }\r
-    ImageInfoListIndex = ImageInfoListIndex->ForwardLink;\r
+    ImageInfoListIndex = GetNextNode (&OverrideItem->DriverInfoList, ImageInfoListIndex);\r
   }\r
   //\r
   // Finish try to load and start the override driver of a controller, popup the controller's device path\r
@@ -933,19 +939,27 @@ GetDriverFromMapping (
   // return the DriverImageHandle for ControllerHandle\r
   //\r
   FoundLastReturned = FALSE;\r
-  ImageInfoListIndex =  OverrideItem->DriverInfoList.ForwardLink;\r
-  while (ImageInfoListIndex != &OverrideItem->DriverInfoList){\r
+  ImageInfoListIndex = GetFirstNode (&OverrideItem->DriverInfoList);\r
+  while (!IsNull (&OverrideItem->DriverInfoList, ImageInfoListIndex)) {\r
     DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);\r
     if (DriverImageInfo->ImageHandle != NULL) {\r
       if ((*DriverImageHandle == NULL) || FoundLastReturned) {\r
+        //\r
+        // If DriverImageHandle is NULL, then we just need to return the first driver.\r
+        // If FoundLastReturned, this means we have just encountered the previously returned driver.\r
+        // For both cases, we just return the image handle of this driver.\r
+        //\r
         OverrideItem->LastReturnedImageHandle = DriverImageInfo->ImageHandle;\r
         *DriverImageHandle = DriverImageInfo->ImageHandle;\r
         return EFI_SUCCESS;\r
       } else if (*DriverImageHandle == DriverImageInfo->ImageHandle){\r
+        //\r
+        // We have found the previously returned driver.\r
+        //\r
         FoundLastReturned = TRUE;\r
       }\r
     }\r
-    ImageInfoListIndex = ImageInfoListIndex->ForwardLink;\r
+    ImageInfoListIndex = GetNextNode (&OverrideItem->DriverInfoList, ImageInfoListIndex);\r
   }\r
 \r
   return EFI_NOT_FOUND;\r
@@ -953,30 +967,30 @@ GetDriverFromMapping (
 \r
 \r
 /**\r
-  Check mapping database whether already has the  mapping info which\r
+  Check mapping database whether already has the mapping info which\r
   records the input Controller to input DriverImage.\r
-  If has, the controller's total override driver number and input DriverImage's order number is return.\r
 \r
-  @param  ControllerDevicePath     The controller device path need to add a\r
-                                   override driver image item\r
-  @param  DriverImageDevicePath    The driver image device path need to be insert\r
+  @param  ControllerDevicePath     The controller device path is to be check.\r
+  @param  DriverImageDevicePath    The driver image device path is to be check.\r
   @param  MappingDataBase          Mapping database list entry pointer\r
   @param  DriverInfoNum            the controller's total override driver number\r
-  @param  DriverImageNO            The inserted order number\r
-\r
-  @return EFI_INVALID_PARAMETER\r
-  @return EFI_NOT_FOUND\r
-  @return EFI_SUCCESS\r
-\r
+  @param  DriverImageNO            The driver order number for the input DriverImage.\r
+                                   If the DriverImageDevicePath is NULL, DriverImageNO is not set.\r
+\r
+  @retval EFI_INVALID_PARAMETER    ControllerDevicePath or MappingDataBase is NULL.\r
+  @retval EFI_NOT_FOUND            ControllerDevicePath is not found in MappingDataBase or\r
+                                   DriverImageDevicePath is not found in the found DriverImage Info list. \r
+  @retval EFI_SUCCESS              The controller's total override driver number and \r
+                                   input DriverImage's order number is correctly return.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 CheckMapping (\r
   IN     EFI_DEVICE_PATH_PROTOCOL                       *ControllerDevicePath,\r
-  IN     EFI_DEVICE_PATH_PROTOCOL                       *DriverImageDevicePath,\r
-  IN     LIST_ENTRY                                     * MappingDataBase,\r
-  OUT    UINT32                                         *DriverInfoNum,\r
-  OUT    UINT32                                         *DriverImageNO\r
+  IN     EFI_DEVICE_PATH_PROTOCOL                       *DriverImageDevicePath  OPTIONAL,\r
+  IN     LIST_ENTRY                                     *MappingDataBase,\r
+  OUT    UINT32                                         *DriverInfoNum  OPTIONAL,\r
+  OUT    UINT32                                         *DriverImageNO  OPTIONAL\r
   )\r
 {\r
   LIST_ENTRY                  *OverrideItemListIndex;\r
@@ -987,39 +1001,40 @@ CheckMapping (
   UINT32                      ImageNO;\r
   UINTN                       DevicePathSize;\r
 \r
-  //\r
-  // Check that ControllerHandle is a valid handle\r
-  //\r
   if (ControllerDevicePath == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
   if (MappingDataBase == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
+\r
   //\r
   // Search ControllerDevicePath in MappingDataBase\r
   //\r
   Found = FALSE;\r
   OverrideItem = NULL;\r
-  OverrideItemListIndex = MappingDataBase->ForwardLink;\r
-  while (OverrideItemListIndex != MappingDataBase){\r
+  OverrideItemListIndex = GetFirstNode (MappingDataBase);\r
+  while (!IsNull (MappingDataBase, OverrideItemListIndex)) {\r
     OverrideItem = CR(OverrideItemListIndex, PLATFORM_OVERRIDE_ITEM, Link, PLATFORM_OVERRIDE_ITEM_SIGNATURE);\r
     DevicePathSize = GetDevicePathSize (ControllerDevicePath);\r
     if (DevicePathSize == GetDevicePathSize (OverrideItem->ControllerDevicePath)) {\r
       if (CompareMem (\r
             ControllerDevicePath,\r
             OverrideItem->ControllerDevicePath,\r
-            GetDevicePathSize (OverrideItem->ControllerDevicePath)\r
+            DevicePathSize\r
             ) == 0\r
           ) {\r
         Found = TRUE;\r
         break;\r
       }\r
     }\r
-    OverrideItemListIndex =  OverrideItemListIndex->ForwardLink;\r
+    OverrideItemListIndex = GetNextNode (MappingDataBase, OverrideItemListIndex);\r
   }\r
 \r
   if (!Found) {\r
+    //\r
+    // ControllerDevicePath is not in MappingDataBase\r
+    //\r
     return EFI_NOT_FOUND;\r
   }\r
 \r
@@ -1028,7 +1043,10 @@ CheckMapping (
     *DriverInfoNum = OverrideItem->DriverInfoNum;\r
   }\r
 \r
-\r
+  //\r
+  // If DriverImageDevicePath is NULL, skip checking DriverImageDevicePath\r
+  // in the controller's Driver Image Info List\r
+  //\r
   if (DriverImageDevicePath == NULL) {\r
     return EFI_SUCCESS;\r
   }\r
@@ -1037,8 +1055,8 @@ CheckMapping (
   //\r
   ImageNO = 0;\r
   Found = FALSE;\r
-  ImageInfoListIndex =  OverrideItem->DriverInfoList.ForwardLink;\r
-  while (ImageInfoListIndex != &OverrideItem->DriverInfoList){\r
+  ImageInfoListIndex = GetFirstNode (&OverrideItem->DriverInfoList);\r
+  while (!IsNull (&OverrideItem->DriverInfoList, ImageInfoListIndex)) {\r
     DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);\r
     ImageNO++;\r
     DevicePathSize = GetDevicePathSize (DriverImageDevicePath);\r
@@ -1053,10 +1071,13 @@ CheckMapping (
         break;\r
       }\r
     }\r
-    ImageInfoListIndex = ImageInfoListIndex->ForwardLink;\r
+    ImageInfoListIndex = GetNextNode (&OverrideItem->DriverInfoList, ImageInfoListIndex);\r
   }\r
 \r
   if (!Found) {\r
+    //\r
+    // DriverImageDevicePath is not found in the controller's Driver Image Info List\r
+    //\r
     return EFI_NOT_FOUND;\r
   } else {\r
     if (DriverImageNO != NULL) {\r
@@ -1064,7 +1085,6 @@ CheckMapping (
     }\r
     return EFI_SUCCESS;\r
   }\r
-\r
 }\r
 \r
 \r
@@ -1076,11 +1096,15 @@ CheckMapping (
                                    override driver image item\r
   @param  DriverImageDevicePath    The driver image device path need to be insert\r
   @param  MappingDataBase          Mapping database list entry pointer\r
-  @param  DriverImageNO            The inserted order number\r
+  @param  DriverImageNO            The inserted order number. If this number is taken, \r
+                                   the larger available number will be used.\r
 \r
-  @return EFI_INVALID_PARAMETER\r
-  @return EFI_ALREADY_STARTED\r
-  @return EFI_SUCCESS\r
+  @retval EFI_INVALID_PARAMETER    ControllerDevicePath is NULL, or DriverImageDevicePath is NULL\r
+                                   or MappingDataBase is NULL\r
+  @retval EFI_ALREADY_STARTED      The input Controller to input DriverImage has been \r
+                                   recorded into the mapping database.\r
+  @retval EFI_SUCCESS              The Controller and DriverImage are inserted into \r
+                                   the mapping database successfully.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1101,9 +1125,6 @@ InsertDriverImage (
   UINT32                      ImageNO;\r
   UINTN                       DevicePathSize;\r
 \r
-  //\r
-  // Check that ControllerHandle is a valid handle\r
-  //\r
   if (ControllerDevicePath == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
@@ -1114,13 +1135,17 @@ InsertDriverImage (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  //\r
+  // If the driver is already in the controller's Driver Image Info List,\r
+  // just return EFI_ALREADY_STARTED.\r
+  //\r
   Status = CheckMapping (\r
-            ControllerDevicePath,\r
-            DriverImageDevicePath,\r
-            MappingDataBase,\r
-            NULL,\r
-            NULL\r
-            );\r
+             ControllerDevicePath,\r
+             DriverImageDevicePath,\r
+             MappingDataBase,\r
+             NULL,\r
+             NULL\r
+             );\r
   if (Status == EFI_SUCCESS) {\r
     return EFI_ALREADY_STARTED;\r
   }\r
@@ -1130,22 +1155,22 @@ InsertDriverImage (
   //\r
   Found = FALSE;\r
   OverrideItem = NULL;\r
-  OverrideItemListIndex = MappingDataBase->ForwardLink;\r
-  while (OverrideItemListIndex != MappingDataBase){\r
+  OverrideItemListIndex = GetFirstNode (MappingDataBase);\r
+  while (!IsNull (MappingDataBase, OverrideItemListIndex)) {\r
     OverrideItem = CR(OverrideItemListIndex, PLATFORM_OVERRIDE_ITEM, Link, PLATFORM_OVERRIDE_ITEM_SIGNATURE);\r
     DevicePathSize = GetDevicePathSize (ControllerDevicePath);\r
     if (DevicePathSize == GetDevicePathSize (OverrideItem->ControllerDevicePath)) {\r
       if (CompareMem (\r
             ControllerDevicePath,\r
             OverrideItem->ControllerDevicePath,\r
-            GetDevicePathSize (OverrideItem->ControllerDevicePath)\r
+            DevicePathSize\r
             ) == 0\r
           ) {\r
         Found = TRUE;\r
         break;\r
       }\r
     }\r
-    OverrideItemListIndex =  OverrideItemListIndex->ForwardLink;\r
+    OverrideItemListIndex = GetNextNode (MappingDataBase, OverrideItemListIndex);\r
   }\r
   //\r
   // If cannot find, this is a new controller item\r
@@ -1168,24 +1193,23 @@ InsertDriverImage (
   DriverImageInfo->Signature = DRIVER_IMAGE_INFO_SIGNATURE;\r
   DriverImageInfo->DriverImagePath = DuplicateDevicePath (DriverImageDevicePath);\r
   //\r
-  // Find the driver image wantted order location\r
+  // Find the driver image wanted order location\r
   //\r
   ImageNO = 0;\r
   Found = FALSE;\r
-  ImageInfoListIndex =  OverrideItem->DriverInfoList.ForwardLink;\r
-  while (ImageInfoListIndex != &OverrideItem->DriverInfoList){\r
+  ImageInfoListIndex = GetFirstNode (&OverrideItem->DriverInfoList);\r
+  while (!IsNull (&OverrideItem->DriverInfoList, ImageInfoListIndex)) {\r
     if (ImageNO == (DriverImageNO - 1)) {\r
       //\r
-      // find the wantted order location, insert it\r
+      // find the wanted order location, insert it\r
       //\r
       InsertTailList (ImageInfoListIndex, &DriverImageInfo->Link);\r
       OverrideItem->DriverInfoNum ++;\r
       Found = TRUE;\r
       break;\r
     }\r
-    //DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);\r
     ImageNO++;\r
-    ImageInfoListIndex = ImageInfoListIndex->ForwardLink;\r
+    ImageInfoListIndex = GetNextNode (&OverrideItem->DriverInfoList, ImageInfoListIndex);\r
   }\r
 \r
   if (!Found) {\r
@@ -1203,15 +1227,16 @@ InsertDriverImage (
 /**\r
   Delete a controller's override driver from the mapping database.\r
 \r
-  @param  ControllerDevicePath     The controller device path need to add a\r
-                                   override driver image item\r
-  @param  DriverImageDevicePath    The driver image device path need to be insert\r
+  @param  ControllerDevicePath     The controller device path will be deleted \r
+                                   when all drivers images on it are removed.\r
+  @param  DriverImageDevicePath    The driver image device path will be delete.\r
+                                   If NULL, all driver image will be delete.\r
   @param  MappingDataBase          Mapping database list entry pointer\r
-  @param  DriverImageNO            The inserted order number\r
 \r
-  @return EFI_INVALID_PARAMETER\r
-  @return EFI_NOT_FOUND\r
-  @return EFI_SUCCESS\r
+  @retval EFI_INVALID_PARAMETER    ControllerDevicePath is NULL, or MappingDataBase is NULL\r
+  @retval EFI_NOT_FOUND            ControllerDevicePath is not found in MappingDataBase or\r
+                                   DriverImageDevicePath is not found in the found DriverImage Info list. \r
+  @retval EFI_SUCCESS              Delete the specified driver successfully.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1230,9 +1255,6 @@ DeleteDriverImage (
   BOOLEAN                     Found;\r
   UINTN                       DevicePathSize;\r
 \r
-  //\r
-  // Check that ControllerHandle is a valid handle\r
-  //\r
   if (ControllerDevicePath == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
@@ -1241,14 +1263,17 @@ DeleteDriverImage (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-   Status = CheckMapping (\r
-              ControllerDevicePath,\r
-              DriverImageDevicePath,\r
-              MappingDataBase,\r
-              NULL,\r
-              NULL\r
-              );\r
-  if (Status == EFI_NOT_FOUND) {\r
+  //\r
+  // If ControllerDevicePath is not found in mapping database, return EFI_NOT_FOUND.\r
+  //\r
+  Status = CheckMapping (\r
+             ControllerDevicePath,\r
+             DriverImageDevicePath,\r
+             MappingDataBase,\r
+             NULL,\r
+             NULL\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
     return EFI_NOT_FOUND;\r
   }\r
 \r
@@ -1257,35 +1282,36 @@ DeleteDriverImage (
   //\r
   Found = FALSE;\r
   OverrideItem = NULL;\r
-  OverrideItemListIndex = MappingDataBase->ForwardLink;\r
-  while (OverrideItemListIndex != MappingDataBase){\r
+  OverrideItemListIndex = GetFirstNode (MappingDataBase);\r
+  while (!IsNull (MappingDataBase, OverrideItemListIndex)) {\r
     OverrideItem = CR(OverrideItemListIndex, PLATFORM_OVERRIDE_ITEM, Link, PLATFORM_OVERRIDE_ITEM_SIGNATURE);\r
     DevicePathSize = GetDevicePathSize (ControllerDevicePath);\r
     if (DevicePathSize == GetDevicePathSize (OverrideItem->ControllerDevicePath)) {\r
       if (CompareMem (\r
             ControllerDevicePath,\r
             OverrideItem->ControllerDevicePath,\r
-            GetDevicePathSize (OverrideItem->ControllerDevicePath)\r
+            DevicePathSize\r
             ) == 0\r
           ) {\r
         Found = TRUE;\r
         break;\r
       }\r
     }\r
-    OverrideItemListIndex =  OverrideItemListIndex->ForwardLink;\r
+    OverrideItemListIndex = GetNextNode (MappingDataBase, OverrideItemListIndex);\r
   }\r
 \r
   ASSERT (Found);\r
   ASSERT (OverrideItem->DriverInfoNum != 0);\r
-  //\r
-  //\r
-  //\r
+\r
   Found = FALSE;\r
-  ImageInfoListIndex =  OverrideItem->DriverInfoList.ForwardLink;\r
-  while (ImageInfoListIndex != &OverrideItem->DriverInfoList){\r
+  ImageInfoListIndex = GetFirstNode (&OverrideItem->DriverInfoList);\r
+  while (!IsNull (&OverrideItem->DriverInfoList, ImageInfoListIndex)) {\r
     DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);\r
-    ImageInfoListIndex = ImageInfoListIndex->ForwardLink;\r
+    ImageInfoListIndex = GetNextNode (&OverrideItem->DriverInfoList, ImageInfoListIndex);\r
     if (DriverImageDevicePath != NULL) {\r
+      //\r
+      // Search for the specified DriverImageDevicePath and remove it, then break.\r
+      //\r
       DevicePathSize = GetDevicePathSize (DriverImageDevicePath);\r
       if (DevicePathSize == GetDevicePathSize (DriverImageInfo->DriverImagePath)) {\r
         if (CompareMem (\r
@@ -1302,6 +1328,9 @@ DeleteDriverImage (
         }\r
       }\r
     } else {\r
+      //\r
+      // Remove all existing driver image info entries, so no break here.\r
+      //\r
       Found = TRUE;\r
       FreePool(DriverImageInfo->DriverImagePath);\r
       RemoveEntryList (&DriverImageInfo->Link);\r
@@ -1309,10 +1338,16 @@ DeleteDriverImage (
     }\r
   }\r
 \r
+  //\r
+  // Confirm all driver image info entries have been removed,\r
+  // if DriverImageDevicePath is NULL.\r
+  //\r
   if (DriverImageDevicePath == NULL) {\r
     ASSERT (OverrideItem->DriverInfoNum == 0);\r
   }\r
-\r
+  //\r
+  // If Override Item has no driver image info entry, then delete this item.\r
+  //\r
   if (OverrideItem->DriverInfoNum == 0) {\r
     FreePool(OverrideItem->ControllerDevicePath);\r
     RemoveEntryList (&OverrideItem->Link);\r
@@ -1320,6 +1355,10 @@ DeleteDriverImage (
   }\r
 \r
   if (!Found) {\r
+    //\r
+    // DriverImageDevicePath is not NULL and cannot be found in the controller's\r
+    // driver image info list.\r
+    //\r
     return EFI_NOT_FOUND;\r
   }\r
 \r
@@ -1331,9 +1370,7 @@ DeleteDriverImage (
   Deletes all environment variable(s) that contain the override mappings from Controller Device Path to\r
   a set of Driver Device Paths.\r
 \r
-  None\r
-\r
-  @return EFI_SUCCESS\r
+  @retval EFI_SUCCESS  Delete all variable(s) successfully.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1350,24 +1387,27 @@ DeleteOverridesVariables (
   CHAR16                      OverrideVariableName[40];\r
 \r
   //\r
-  // Get environment variable(s)  number\r
+  // Get environment variable(s) number\r
   //\r
-  VariableNum =0;\r
+  VariableNum = 0;\r
   VariableBuffer = GetVariableAndSize (L"PlatDriOver", &gEfiOverrideVariableGuid, &BufferSize);\r
-  VariableNum ++;\r
+  VariableNum++;\r
   if (VariableBuffer == NULL) {\r
     return EFI_NOT_FOUND;\r
   }\r
   //\r
   // Check NotEnd to get all PlatDriOverX variable(s)\r
   //\r
-  while (*(UINT32*)VariableBuffer) {\r
+  while ((*(UINT32*)VariableBuffer) != 0) {\r
     UnicodeSPrint (OverrideVariableName, sizeof (OverrideVariableName), L"PlatDriOver%d", VariableNum);\r
     VariableBuffer = GetVariableAndSize (OverrideVariableName, &gEfiOverrideVariableGuid, &BufferSize);\r
-    VariableNum ++;\r
+    VariableNum++;\r
     ASSERT (VariableBuffer != NULL);\r
   }\r
 \r
+  //\r
+  // Delete PlatDriOver and all additional variables, if exist.\r
+  //\r
   Status = gRT->SetVariable (\r
                   L"PlatDriOver",\r
                   &gEfiOverrideVariableGuid,\r
@@ -1392,12 +1432,11 @@ DeleteOverridesVariables (
 \r
 \r
 /**\r
-  Push a controller device path into a globle device path list\r
+  Push a controller device path into a globle device path list.\r
 \r
-  @param  ControllerDevicePath     The controller device path need to push into\r
-                                   stack\r
+  @param  DevicePath     The controller device path to push into stack\r
 \r
-  @return EFI_SUCCESS\r
+  @retval EFI_SUCCESS    Device path successfully pushed into the stack.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1420,10 +1459,10 @@ PushDevPathStack (
 /**\r
   Pop a controller device path from a globle device path list\r
 \r
-  @param  ControllerDevicePath     The controller device path retrieved from stack\r
+  @param  DevicePath     The controller device path popped from stack\r
 \r
-  @return EFI_SUCCESS\r
-  @return EFI_NOT_FOUND\r
+  @retval EFI_SUCCESS    Controller device path successfully popped.\r
+  @retval EFI_NOT_FOUND  Stack is empty.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1436,6 +1475,9 @@ PopDevPathStack (
   LIST_ENTRY              *ItemListIndex;\r
 \r
   ItemListIndex = mDevicePathStack.BackLink;\r
+  //\r
+  // Check if the stack is empty\r
+  //\r
   if (ItemListIndex != &mDevicePathStack){\r
     DevicePathStackItem = CR(ItemListIndex, DEVICE_PATH_STACK_ITEM, Link, DEVICE_PATH_STACK_ITEM_SIGNATURE);\r
     if (DevicePath != NULL) {\r
@@ -1453,10 +1495,10 @@ PopDevPathStack (
 /**\r
   Check whether a controller device path is in a globle device path list\r
 \r
-  @param  ControllerDevicePath     The controller device path need to check\r
+  @param  DevicePath     The controller device path to check\r
 \r
-  @return True\r
-  @return False\r
+  @retval TRUE           DevicePath exists in the stack.\r
+  @retval FALSE          DevicePath does not exist in the stack.\r
 \r
 **/\r
 BOOLEAN\r
@@ -1467,57 +1509,51 @@ CheckExistInStack (
 {\r
   DEVICE_PATH_STACK_ITEM  *DevicePathStackItem;\r
   LIST_ENTRY              *ItemListIndex;\r
-  BOOLEAN                 Found;\r
   UINTN                   DevicePathSize;\r
 \r
-  Found = FALSE;\r
   ItemListIndex = mDevicePathStack.BackLink;\r
   while (ItemListIndex != &mDevicePathStack){\r
     DevicePathStackItem = CR(ItemListIndex, DEVICE_PATH_STACK_ITEM, Link, DEVICE_PATH_STACK_ITEM_SIGNATURE);\r
     DevicePathSize = GetDevicePathSize (DevicePath);\r
     if (DevicePathSize == GetDevicePathSize (DevicePathStackItem->DevicePath)) {\r
-      if (CompareMem (\r
-            DevicePath,\r
-            DevicePathStackItem->DevicePath,\r
-            GetDevicePathSize (DevicePathStackItem->DevicePath)\r
-            ) == 0\r
-          ) {\r
-        Found = TRUE;\r
-        break;\r
+      if (CompareMem (DevicePath, DevicePathStackItem->DevicePath, DevicePathSize) == 0) {\r
+        return TRUE;\r
       }\r
     }\r
     ItemListIndex = ItemListIndex->BackLink;\r
   }\r
 \r
-  return Found;\r
+  return FALSE;\r
 }\r
 \r
 \r
 /**\r
-  According to a file guild, check a Fv file device path is valid. If it is invalid,\r
+  Update the FV file device path if it is not valid.\r
+\r
+  According to a file GUID, check a Fv file device path is valid. If it is invalid,\r
   try to return the valid device path.\r
   FV address maybe changes for memory layout adjust from time to time, use this funciton\r
   could promise the Fv file device path is right.\r
 \r
-  @param  DevicePath               on input, the Fv file device path need to check\r
-                                   on output, the updated valid Fv file device path\r
-  @param  FileGuid                 the Fv file guild\r
-  @param  CallerImageHandle\r
+  @param  DevicePath               On input, the FV file device path to check\r
+                                   On output, the updated valid FV file device path\r
+  @param  FileGuid                 The FV file GUID\r
+  @param  CallerImageHandle        Image handle of the caller\r
 \r
   @retval EFI_INVALID_PARAMETER    the input DevicePath or FileGuid is invalid\r
                                    parameter\r
-  @retval EFI_UNSUPPORTED          the input DevicePath does not contain Fv file\r
-                                   guild at all\r
-  @retval EFI_ALREADY_STARTED      the input DevicePath has pointed to Fv file, it\r
+  @retval EFI_UNSUPPORTED          the input DevicePath does not contain FV file\r
+                                   GUID at all\r
+  @retval EFI_ALREADY_STARTED      the input DevicePath has pointed to FV file, it\r
                                    is valid\r
-  @retval EFI_SUCCESS              has successfully updated the invalid DevicePath,\r
+  @retval EFI_SUCCESS              Successfully updated the invalid DevicePath,\r
                                    and return the updated device path in DevicePath\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 UpdateFvFileDevicePath (\r
-  IN  OUT EFI_DEVICE_PATH_PROTOCOL      ** DevicePath,\r
+  IN  OUT EFI_DEVICE_PATH_PROTOCOL      **DevicePath,\r
   IN  EFI_GUID                          *FileGuid,\r
   IN  EFI_HANDLE                        CallerImageHandle\r
   )\r
@@ -1539,27 +1575,29 @@ UpdateFvFileDevicePath (
   MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode;\r
   EFI_HANDLE                    FoundFvHandle;\r
   EFI_DEVICE_PATH_PROTOCOL      *NewDevicePath;\r
-  BOOLEAN                       HasFVNode;\r
+  BOOLEAN                       HasFvNode;\r
+\r
+  if (DevicePath == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
 \r
-  if ((DevicePath == NULL) || (*DevicePath == NULL)) {\r
+  if (*DevicePath == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   //\r
-  // Check whether the device path point to the default the input Fv file\r
+  // Check whether the device path points to the default the input FV file\r
   //\r
   TempDevicePath = *DevicePath;\r
   LastDeviceNode = TempDevicePath;\r
   while (!EfiIsDevicePathEnd (TempDevicePath)) {\r
-     LastDeviceNode = TempDevicePath;\r
-     TempDevicePath = EfiNextDevicePathNode (TempDevicePath);\r
+    LastDeviceNode = TempDevicePath;\r
+    TempDevicePath = EfiNextDevicePathNode (TempDevicePath);\r
   }\r
-  GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (\r
-                (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode\r
-                );\r
+  GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode);\r
   if (GuidPoint == NULL) {\r
     //\r
-    // if this option does not points to a Fv file, just return EFI_UNSUPPORTED\r
+    // If this option does not point to a FV file, just return EFI_UNSUPPORTED.\r
     //\r
     return EFI_UNSUPPORTED;\r
   }\r
@@ -1567,7 +1605,7 @@ UpdateFvFileDevicePath (
   if (FileGuid != NULL) {\r
     if (!CompareGuid (GuidPoint, FileGuid)) {\r
       //\r
-      // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED\r
+      // If the FV file is not the input file GUID, just return EFI_UNSUPPORTED\r
       //\r
       return EFI_UNSUPPORTED;\r
     }\r
@@ -1576,23 +1614,23 @@ UpdateFvFileDevicePath (
   }\r
 \r
   //\r
-  // Check to see if the device path contain memory map node\r
+  // Check to see if the device path contains memory map node\r
   //\r
   TempDevicePath = *DevicePath;\r
-  HasFVNode = FALSE;\r
+  HasFvNode = FALSE;\r
   while (!EfiIsDevicePathEnd (TempDevicePath)) {\r
     //\r
     // Use old Device Path\r
     //\r
     if (DevicePathType (TempDevicePath) == HARDWARE_DEVICE_PATH &&\r
         DevicePathSubType (TempDevicePath) == HW_MEMMAP_DP) {\r
-      HasFVNode = TRUE;\r
+      HasFvNode = TRUE;\r
       break;\r
     }\r
     TempDevicePath = EfiNextDevicePathNode (TempDevicePath);\r
   }\r
 \r
-  if (!HasFVNode) {\r
+  if (!HasFvNode) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
@@ -1617,14 +1655,14 @@ UpdateFvFileDevicePath (
       // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there\r
       //\r
       Status = Fv->ReadFile (\r
-                    Fv,\r
-                    FileGuid,\r
-                    NULL,\r
-                    &Size,\r
-                    &Type,\r
-                    &Attributes,\r
-                    &AuthenticationStatus\r
-                    );\r
+                     Fv,\r
+                     FileGuid,\r
+                     NULL,\r
+                     &Size,\r
+                     &Type,\r
+                     &Attributes,\r
+                     &AuthenticationStatus\r
+                     );\r
       if (!EFI_ERROR (Status)) {\r
         return EFI_ALREADY_STARTED;\r
       }\r
@@ -1638,10 +1676,10 @@ UpdateFvFileDevicePath (
   FindFvFile = FALSE;\r
   FoundFvHandle = NULL;\r
   Status = gBS->HandleProtocol (\r
-             CallerImageHandle,\r
-             &gEfiLoadedImageProtocolGuid,\r
-             (VOID **) &LoadedImage\r
-             );\r
+                  CallerImageHandle,\r
+                  &gEfiLoadedImageProtocolGuid,\r
+                  (VOID **) &LoadedImage\r
+                  );\r
   if (!EFI_ERROR (Status)) {\r
     Status = gBS->HandleProtocol (\r
                     LoadedImage->DeviceHandle,\r
@@ -1650,14 +1688,14 @@ UpdateFvFileDevicePath (
                     );\r
     if (!EFI_ERROR (Status)) {\r
       Status = Fv->ReadFile (\r
-                    Fv,\r
-                    FileGuid,\r
-                    NULL,\r
-                    &Size,\r
-                    &Type,\r
-                    &Attributes,\r
-                    &AuthenticationStatus\r
-                    );\r
+                     Fv,\r
+                     FileGuid,\r
+                     NULL,\r
+                     &Size,\r
+                     &Type,\r
+                     &Attributes,\r
+                     &AuthenticationStatus\r
+                     );\r
       if (!EFI_ERROR (Status)) {\r
         FindFvFile = TRUE;\r
         FoundFvHandle = LoadedImage->DeviceHandle;\r
@@ -1669,28 +1707,28 @@ UpdateFvFileDevicePath (
   //\r
   if (!FindFvFile) {\r
     gBS->LocateHandleBuffer (\r
-          ByProtocol,\r
-          &gEfiFirmwareVolume2ProtocolGuid,\r
-          NULL,\r
-          &FvHandleCount,\r
-          &FvHandleBuffer\r
-          );\r
+           ByProtocol,\r
+           &gEfiFirmwareVolume2ProtocolGuid,\r
+           NULL,\r
+           &FvHandleCount,\r
+           &FvHandleBuffer\r
+           );\r
     for (Index = 0; Index < FvHandleCount; Index++) {\r
       gBS->HandleProtocol (\r
-            FvHandleBuffer[Index],\r
-            &gEfiFirmwareVolume2ProtocolGuid,\r
-            (VOID **) &Fv\r
-            );\r
+             FvHandleBuffer[Index],\r
+             &gEfiFirmwareVolume2ProtocolGuid,\r
+             (VOID **) &Fv\r
+             );\r
 \r
       Status = Fv->ReadFile (\r
-                    Fv,\r
-                    FileGuid,\r
-                    NULL,\r
-                    &Size,\r
-                    &Type,\r
-                    &Attributes,\r
-                    &AuthenticationStatus\r
-                    );\r
+                     Fv,\r
+                     FileGuid,\r
+                     NULL,\r
+                     &Size,\r
+                     &Type,\r
+                     &Attributes,\r
+                     &AuthenticationStatus\r
+                     );\r
       if (EFI_ERROR (Status)) {\r
         //\r
         // Skip if input Fv file not in the FV\r
@@ -1718,6 +1756,8 @@ UpdateFvFileDevicePath (
 \r
 \r
 /**\r
+  Gets the data and size of a variable.\r
+\r
   Read the EFI variable (VendorGuid/Name) and return a dynamically allocated\r
   buffer, and the size of the buffer. If failure return NULL.\r
 \r
@@ -1727,7 +1767,7 @@ UpdateFvFileDevicePath (
                                    read\r
 \r
   @return Dynamically allocated memory that contains a copy of the EFI variable.\r
-  @return Caller is responsible freeing the buffer.\r
+          Caller is responsible freeing the buffer.\r
   @retval NULL                     Variable was not read\r
 \r
 **/\r
@@ -1773,6 +1813,8 @@ GetVariableAndSize (
 \r
 \r
 /**\r
+  Connect to the handle to a device on the device path.\r
+\r
   This function will create all handles associate with every device\r
   path node. If the handle associate with one device path node can not\r
   be created success, then still give one chance to do the dispatch,\r
@@ -1864,10 +1906,10 @@ ConnectDevicePath (
           // then still give the chance to do dispatch, because partial\r
           // RemainingDevicepath may be in the new FV\r
           //\r
-          // 1. If the connect fail, RemainingDevicepath and handle will not\r
+          // 1. If the connect fails, RemainingDevicepath and handle will not\r
           //    change, so next time will do the dispatch, then dispatch's status\r
           //    will take effect\r
-          // 2. If the connect success, the RemainingDevicepath and handle will\r
+          // 2. If the connect succeeds, the RemainingDevicepath and handle will\r
           //    change, then avoid the dispatch, we have chance to continue the\r
           //    next connection\r
           //\r