]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/BdsDxe/BootMaint/BootOption.c
Remove SafeFreePool from MemoryAllocationLib as this API's name is misleading. Its...
[mirror_edk2.git] / MdeModulePkg / Universal / BdsDxe / BootMaint / BootOption.c
index b0838bbeee25674c2c31de6ef87628cff9e167b5..85ba61707faa4afa5cec427deeb1b196f6065098 100644 (file)
@@ -19,31 +19,26 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include "BootMaint.h"\r
 #include "BBSsupport.h"\r
 \r
+/**\r
+  Create a menu entry give a Menu type.\r
+\r
+  @param MenuType        The Menu type to be created.\r
+\r
+  @retval NULL           If failed to create the menu.\r
+  @return                The menu.\r
+\r
+**/\r
 BM_MENU_ENTRY *\r
 BOpt_CreateMenuEntry (\r
   UINTN           MenuType\r
   )\r
-/*++\r
-\r
-Routine Description\r
-  Create Menu Entry for future use, make all types together\r
-  in order to reduce code size\r
-\r
-Arguments:\r
-  MenuType            Use this parameter to identify current\r
-                      Menu type\r
-\r
-Returns:\r
-  NULL                Cannot allocate memory for current menu\r
-                      entry\r
-  Others              A valid pointer pointing to the allocated\r
-                      memory pool for current menu entry\r
-\r
---*/\r
 {\r
   BM_MENU_ENTRY *MenuEntry;\r
   UINTN         ContextSize;\r
 \r
+  //\r
+  // Get context size according to menu type\r
+  //\r
   switch (MenuType) {\r
   case BM_LOAD_CONTEXT_SELECT:\r
     ContextSize = sizeof (BM_LOAD_CONTEXT);\r
@@ -72,23 +67,24 @@ Returns:
   default:\r
     ContextSize = 0;\r
     break;\r
-\r
   }\r
 \r
-  if (0 == ContextSize) {\r
+  if (ContextSize == 0) {\r
     return NULL;\r
   }\r
 \r
-  MenuEntry = EfiAllocateZeroPool (sizeof (BM_MENU_ENTRY));\r
+  //\r
+  // Create new menu entry\r
+  //\r
+  MenuEntry = AllocateZeroPool (sizeof (BM_MENU_ENTRY));\r
   if (NULL == MenuEntry) {\r
     return MenuEntry;\r
   }\r
 \r
-  MenuEntry->VariableContext = EfiAllocateZeroPool (ContextSize);\r
+  MenuEntry->VariableContext = AllocateZeroPool (ContextSize);\r
   if (NULL == MenuEntry->VariableContext) {\r
-    SafeFreePool (MenuEntry);\r
-    MenuEntry = NULL;\r
-    return MenuEntry;\r
+    FreePool (MenuEntry);\r
+    return NULL;\r
   }\r
 \r
   MenuEntry->Signature        = BM_MENU_ENTRY_SIGNATURE;\r
@@ -96,21 +92,16 @@ Returns:
   return MenuEntry;\r
 }\r
 \r
+/**\r
+  Free up all resource allocated for a BM_MENU_ENTRY.\r
+\r
+  @param MenuEntry   A pointer to BM_MENU_ENTRY.\r
+\r
+**/\r
 VOID\r
 BOpt_DestroyMenuEntry (\r
   BM_MENU_ENTRY         *MenuEntry\r
   )\r
-/*++\r
-  Routine Description :\r
-    Destroy the menu entry passed in\r
-\r
-  Arguments :\r
-    The menu entry need to be destroyed\r
-\r
-  Returns :\r
-    None\r
-\r
---*/\r
 {\r
   BM_LOAD_CONTEXT           *LoadContext;\r
   BM_FILE_CONTEXT           *FileContext;\r
@@ -125,88 +116,90 @@ BOpt_DestroyMenuEntry (
   switch (MenuEntry->ContextSelection) {\r
   case BM_LOAD_CONTEXT_SELECT:\r
     LoadContext = (BM_LOAD_CONTEXT *) MenuEntry->VariableContext;\r
-    SafeFreePool (LoadContext->FilePathList);\r
-    SafeFreePool (LoadContext->LoadOption);\r
-    SafeFreePool (LoadContext->OptionalData);\r
-    SafeFreePool (LoadContext);\r
+    FreePool (LoadContext->FilePathList);\r
+    FreePool (LoadContext->LoadOption);\r
+    if (LoadContext->OptionalData != NULL) {\r
+      FreePool (LoadContext->OptionalData);\r
+    }\r
+    FreePool (LoadContext);\r
     break;\r
 \r
   case BM_FILE_CONTEXT_SELECT:\r
     FileContext = (BM_FILE_CONTEXT *) MenuEntry->VariableContext;\r
 \r
     if (!FileContext->IsRoot) {\r
-      SafeFreePool (FileContext->DevicePath);\r
+      FreePool (FileContext->DevicePath);\r
     } else {\r
       if (FileContext->FHandle != NULL) {\r
         FileContext->FHandle->Close (FileContext->FHandle);\r
       }\r
     }\r
 \r
-    SafeFreePool (FileContext->FileName);\r
-    SafeFreePool (FileContext->Info);\r
-    SafeFreePool (FileContext);\r
+    if (FileContext->FileName != NULL) {\r
+      FreePool (FileContext->FileName);\r
+    }\r
+    if (FileContext->Info != NULL) {\r
+      FreePool (FileContext->Info);\r
+    }\r
+    FreePool (FileContext);\r
     break;\r
 \r
   case BM_CONSOLE_CONTEXT_SELECT:\r
     ConsoleContext = (BM_CONSOLE_CONTEXT *) MenuEntry->VariableContext;\r
-    SafeFreePool (ConsoleContext->DevicePath);\r
-    SafeFreePool (ConsoleContext);\r
+    FreePool (ConsoleContext->DevicePath);\r
+    FreePool (ConsoleContext);\r
     break;\r
 \r
   case BM_TERMINAL_CONTEXT_SELECT:\r
     TerminalContext = (BM_TERMINAL_CONTEXT *) MenuEntry->VariableContext;\r
-    SafeFreePool (TerminalContext->DevicePath);\r
-    SafeFreePool (TerminalContext);\r
+    FreePool (TerminalContext->DevicePath);\r
+    FreePool (TerminalContext);\r
     break;\r
 \r
   case BM_HANDLE_CONTEXT_SELECT:\r
     HandleContext = (BM_HANDLE_CONTEXT *) MenuEntry->VariableContext;\r
-    SafeFreePool (HandleContext);\r
+    FreePool (HandleContext);\r
     break;\r
 \r
   case BM_LEGACY_DEV_CONTEXT_SELECT:\r
     LegacyDevContext = (BM_LEGACY_DEVICE_CONTEXT *) MenuEntry->VariableContext;\r
-    SafeFreePool (LegacyDevContext);\r
+    FreePool (LegacyDevContext);\r
 \r
   default:\r
     break;\r
   }\r
 \r
-  SafeFreePool (MenuEntry->DisplayString);\r
+  FreePool (MenuEntry->DisplayString);\r
   if (NULL != MenuEntry->HelpString) {\r
-    SafeFreePool (MenuEntry->HelpString);\r
+    FreePool (MenuEntry->HelpString);\r
   }\r
 \r
-  SafeFreePool (MenuEntry);\r
+  FreePool (MenuEntry);\r
 }\r
 \r
+/**\r
+  Get the Menu Entry from the list in Menu Entry List.\r
+\r
+  If MenuNumber is great or equal to the number of Menu\r
+  Entry in the list, then ASSERT.\r
+\r
+  @param MenuOption      The Menu Entry List to read the menu entry.\r
+  @param MenuNumber      The index of Menu Entry.\r
+\r
+  @return The Menu Entry.\r
+\r
+**/\r
 BM_MENU_ENTRY *\r
 BOpt_GetMenuEntry (\r
   BM_MENU_OPTION      *MenuOption,\r
   UINTN               MenuNumber\r
   )\r
-/*++\r
-  Rountine Description :\r
-    Use this routine to get one particular menu entry in specified\r
-    menu\r
-\r
-  Arguments :\r
-    MenuOption        The menu that we will search\r
-\r
-    MenuNumber        The menunubmer that we want\r
-\r
-  Returns :\r
-    The desired menu entry\r
-\r
---*/\r
 {\r
   BM_MENU_ENTRY   *NewMenuEntry;\r
   UINTN           Index;\r
   LIST_ENTRY      *List;\r
 \r
-  if (MenuNumber >= MenuOption->MenuNumber) {\r
-    return NULL;\r
-  }\r
+  ASSERT (MenuNumber < MenuOption->MenuNumber);\r
 \r
   List = MenuOption->Head.ForwardLink;\r
   for (Index = 0; Index < MenuNumber; Index++) {\r
@@ -218,29 +211,22 @@ BOpt_GetMenuEntry (
   return NewMenuEntry;\r
 }\r
 \r
+/**\r
+  This function build the FsOptionMenu list which records all\r
+  available file system in the system. They includes all instances\r
+  of EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, all instances of EFI_LOAD_FILE_SYSTEM\r
+  and all type of legacy boot device.\r
+\r
+  @param CallbackData    BMM context data\r
+\r
+  @retval  EFI_SUCCESS             Success find the file system\r
+  @retval  EFI_OUT_OF_RESOURCES    Can not create menu entry\r
+\r
+**/\r
 EFI_STATUS\r
 BOpt_FindFileSystem (\r
   IN BMM_CALLBACK_DATA          *CallbackData\r
   )\r
-/*++\r
-\r
-Routine Description\r
-  Find file systems for current Extensible Firmware\r
-  Including Handles that support Simple File System\r
-  protocol, Load File protocol.\r
-\r
-  Building up the FileSystem Menu for user selection\r
-  All file system will be stored in FsOptionMenu\r
-  for future use.\r
-\r
-Arguments:\r
-  CallbackData           -   BMM context data\r
-\r
-Returns:\r
-  EFI_SUCCESS            -   Success find the file system\r
-  EFI_OUT_OF_RESOURCES   -   Can not create menu entry\r
-\r
---*/\r
 {\r
   UINTN                     NoBlkIoHandles;\r
   UINTN                     NoSimpleFsHandles;\r
@@ -296,9 +282,9 @@ Returns:
       // Issue a dummy read to trigger reinstall of BlockIo protocol for removable media\r
       //\r
       if (BlkIo->Media->RemovableMedia) {\r
-        Buffer = EfiAllocateZeroPool (BlkIo->Media->BlockSize);\r
+        Buffer = AllocateZeroPool (BlkIo->Media->BlockSize);\r
         if (NULL == Buffer) {\r
-          SafeFreePool (BlkIoHandle);\r
+          FreePool (BlkIoHandle);\r
           return EFI_OUT_OF_RESOURCES;\r
         }\r
 \r
@@ -309,10 +295,10 @@ Returns:
                 BlkIo->Media->BlockSize,\r
                 Buffer\r
                 );\r
-        SafeFreePool (Buffer);\r
+        FreePool (Buffer);\r
       }\r
     }\r
-    SafeFreePool (BlkIoHandle);\r
+    FreePool (BlkIoHandle);\r
   }\r
 \r
   //\r
@@ -352,7 +338,7 @@ Returns:
       //\r
       MenuEntry = BOpt_CreateMenuEntry (BM_FILE_CONTEXT_SELECT);\r
       if (NULL == MenuEntry) {\r
-        SafeFreePool (SimpleFsHandle);\r
+        FreePool (SimpleFsHandle);\r
         return EFI_OUT_OF_RESOURCES;\r
       }\r
 \r
@@ -361,7 +347,7 @@ Returns:
       FileContext->Handle     = SimpleFsHandle[Index];\r
       MenuEntry->OptionNumber = Index;\r
       FileContext->FHandle    = EfiLibOpenRoot (FileContext->Handle);\r
-      if (!FileContext->FHandle) {\r
+      if (FileContext->FHandle == NULL) {\r
         BOpt_DestroyMenuEntry (MenuEntry);\r
         continue;\r
       }\r
@@ -395,7 +381,7 @@ Returns:
       }\r
 \r
       TempStr                   = MenuEntry->HelpString;\r
-      MenuEntry->DisplayString  = EfiAllocateZeroPool (MAX_CHAR);\r
+      MenuEntry->DisplayString  = AllocateZeroPool (MAX_CHAR);\r
       ASSERT (MenuEntry->DisplayString != NULL);\r
       UnicodeSPrint (\r
         MenuEntry->DisplayString,\r
@@ -410,7 +396,7 @@ Returns:
   }\r
 \r
   if (NoSimpleFsHandles != 0) {\r
-    SafeFreePool (SimpleFsHandle);\r
+    FreePool (SimpleFsHandle);\r
   }\r
   //\r
   // Searching for handles that support Load File protocol\r
@@ -427,7 +413,7 @@ Returns:
     for (Index = 0; Index < NoLoadFileHandles; Index++) {\r
       MenuEntry = BOpt_CreateMenuEntry (BM_FILE_CONTEXT_SELECT);\r
       if (NULL == MenuEntry) {\r
-        SafeFreePool (LoadFileHandle);\r
+        FreePool (LoadFileHandle);\r
         return EFI_OUT_OF_RESOURCES;\r
       }\r
 \r
@@ -442,7 +428,7 @@ Returns:
       MenuEntry->HelpString     = DevicePathToStr (FileContext->DevicePath);\r
 \r
       TempStr                   = MenuEntry->HelpString;\r
-      MenuEntry->DisplayString  = EfiAllocateZeroPool (MAX_CHAR);\r
+      MenuEntry->DisplayString  = AllocateZeroPool (MAX_CHAR);\r
       ASSERT (MenuEntry->DisplayString != NULL);\r
       UnicodeSPrint (\r
         MenuEntry->DisplayString,\r
@@ -458,7 +444,7 @@ Returns:
   }\r
 \r
   if (NoLoadFileHandles != 0) {\r
-    SafeFreePool (LoadFileHandle);\r
+    FreePool (LoadFileHandle);\r
   }\r
 \r
   //\r
@@ -501,7 +487,7 @@ Returns:
       MenuEntry->HelpString     = DevicePathToStr (FileContext->DevicePath);\r
 \r
       TempStr                   = MenuEntry->HelpString;\r
-      MenuEntry->DisplayString  = EfiAllocateZeroPool (MAX_CHAR);\r
+      MenuEntry->DisplayString  = AllocateZeroPool (MAX_CHAR);\r
       ASSERT (MenuEntry->DisplayString != NULL);\r
       UnicodeSPrint (\r
         MenuEntry->DisplayString,\r
@@ -521,22 +507,15 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Free resources allocated in Allocate Rountine.\r
+\r
+  @param FreeMenu        Menu to be freed\r
+**/\r
 VOID\r
 BOpt_FreeMenu (\r
   BM_MENU_OPTION        *FreeMenu\r
   )\r
-/*++\r
-\r
-Routine Description\r
-  Free resources allocated in Allocate Rountine\r
-\r
-Arguments:\r
-  FreeMenu        Menu to be freed\r
-\r
-Returns:\r
-  VOID\r
-\r
---*/\r
 {\r
   BM_MENU_ENTRY *MenuEntry;\r
   while (!IsListEmpty (&FreeMenu->Head)) {\r
@@ -551,26 +530,23 @@ Returns:
   }\r
 }\r
 \r
-EFI_STATUS\r
-BOpt_FindFiles (\r
-  IN BMM_CALLBACK_DATA          *CallbackData,\r
-  IN BM_MENU_ENTRY              *MenuEntry\r
-  )\r
-/*++\r
-\r
-Routine Description\r
+/**\r
   Find files under current directory\r
   All files and sub-directories in current directory\r
   will be stored in DirectoryMenu for future use.\r
 \r
-Arguments:\r
-  FileOption   -- Pointer for Dir to explore\r
+  @param CallbackData  The BMM context data.\r
+  @param MenuEntry     The Menu Entry.\r
 \r
-Returns:\r
-  TRUE         -- Get files from current dir successfully\r
-  FALSE        -- Can't get files from current dir\r
+  @retval EFI_SUCCESS         Get files from current dir successfully.\r
+  @return Other value if can't get files from current dir.\r
 \r
---*/\r
+**/\r
+EFI_STATUS\r
+BOpt_FindFiles (\r
+  IN BMM_CALLBACK_DATA          *CallbackData,\r
+  IN BM_MENU_ENTRY              *MenuEntry\r
+  )\r
 {\r
   EFI_FILE_HANDLE NewDir;\r
   EFI_FILE_HANDLE Dir;\r
@@ -606,7 +582,7 @@ Returns:
   }\r
 \r
   DirInfo = EfiLibFileInfo (NewDir);\r
-  if (!DirInfo) {\r
+  if (DirInfo == NULL) {\r
     return EFI_NOT_FOUND;\r
   }\r
 \r
@@ -620,8 +596,8 @@ Returns:
                               );\r
 \r
   DirBufferSize = sizeof (EFI_FILE_INFO) + 1024;\r
-  DirInfo       = EfiAllocateZeroPool (DirBufferSize);\r
-  if (!DirInfo) {\r
+  DirInfo       = AllocateZeroPool (DirBufferSize);\r
+  if (DirInfo == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
   //\r
@@ -682,7 +658,7 @@ Returns:
 \r
       if (NewFileContext->IsDir) {\r
         BufferSize                  = StrLen (DirInfo->FileName) * 2 + 6;\r
-        NewMenuEntry->DisplayString = EfiAllocateZeroPool (BufferSize);\r
+        NewMenuEntry->DisplayString = AllocateZeroPool (BufferSize);\r
 \r
         UnicodeSPrint (\r
           NewMenuEntry->DisplayString,\r
@@ -706,26 +682,21 @@ Returns:
   }\r
 \r
   DirectoryMenu.MenuNumber = OptionNumber;\r
-  SafeFreePool (DirInfo);\r
+  FreePool (DirInfo);\r
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Build the LegacyFDMenu LegacyHDMenu LegacyCDMenu according to LegacyBios.GetBbsInfo().\r
+\r
+  @retval EFI_SUCCESS The function complete successfully.\r
+  @retval EFI_OUT_OF_RESOURCES No enough memory to complete this function.\r
+\r
+**/\r
 EFI_STATUS\r
 BOpt_GetLegacyOptions (\r
   VOID\r
   )\r
-/*++\r
-Routine Description:\r
-\r
-  Build the LegacyFDMenu LegacyHDMenu LegacyCDMenu according to LegacyBios.GetBbsInfo().\r
-\r
-Arguments:\r
-  None\r
-\r
-Returns:\r
-  The device info of legacy device.\r
-\r
---*/\r
 {\r
   BM_MENU_ENTRY             *NewMenuEntry;\r
   BM_LEGACY_DEVICE_CONTEXT  *NewLegacyDevContext;\r
@@ -803,7 +774,7 @@ Returns:
       sizeof (DescString),\r
       DescString\r
       );\r
-    NewLegacyDevContext->Description = EfiAllocateZeroPool (StrSize (DescString));\r
+    NewLegacyDevContext->Description = AllocateZeroPool (StrSize (DescString));\r
     if (NULL == NewLegacyDevContext->Description) {\r
       break;\r
     }\r
@@ -853,6 +824,10 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Free out resouce allocated from Legacy Boot Options.\r
+\r
+**/\r
 VOID\r
 BOpt_FreeLegacyOptions (\r
   VOID\r
@@ -865,24 +840,20 @@ BOpt_FreeLegacyOptions (
   BOpt_FreeMenu (&LegacyBEVMenu);\r
 }\r
 \r
-EFI_STATUS\r
-BOpt_GetBootOptions (\r
-  IN  BMM_CALLBACK_DATA         *CallbackData\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
+/**\r
 \r
   Build the BootOptionMenu according to BootOrder Variable.\r
-  This Routine will access the Boot#### to get EFI_LOAD_OPTION\r
+  This Routine will access the Boot#### to get EFI_LOAD_OPTION.\r
 \r
-Arguments:\r
-  None\r
+  @param CallbackData The BMM context data.\r
 \r
-Returns:\r
-  The number of the Var Boot####\r
+  @return The number of the Var Boot####.\r
 \r
---*/\r
+**/\r
+EFI_STATUS\r
+BOpt_GetBootOptions (\r
+  IN  BMM_CALLBACK_DATA         *CallbackData\r
+  )\r
 {\r
   UINTN                     Index;\r
   UINT16                    BootString[10];\r
@@ -931,9 +902,9 @@ Returns:
               &BootNextSize\r
               );\r
 \r
-  if (BootNext) {\r
+  if (BootNext != NULL) {\r
     if (BootNextSize != sizeof (UINT16)) {\r
-      SafeFreePool (BootNext);\r
+      FreePool (BootNext);\r
       BootNext = NULL;\r
     }\r
   }\r
@@ -948,26 +919,26 @@ Returns:
                           &gEfiGlobalVariableGuid,\r
                           &BootOptionSize\r
                           );\r
-    if (!LoadOptionFromVar) {\r
+    if (LoadOptionFromVar == NULL) {\r
       continue;\r
     }\r
 \r
-    LoadOption = EfiAllocateZeroPool (BootOptionSize);\r
-    if (!LoadOption) {\r
+    LoadOption = AllocateZeroPool (BootOptionSize);\r
+    if (LoadOption == NULL) {\r
       continue;\r
     }\r
 \r
     CopyMem (LoadOption, LoadOptionFromVar, BootOptionSize);\r
-    SafeFreePool (LoadOptionFromVar);\r
+    FreePool (LoadOptionFromVar);\r
 \r
-    if (BootNext) {\r
+    if (BootNext != NULL) {\r
       BootNextFlag = (BOOLEAN) (*BootNext == BootOrderList[Index]);\r
     } else {\r
       BootNextFlag = FALSE;\r
     }\r
 \r
     if (0 == (*((UINT32 *) LoadOption) & LOAD_OPTION_ACTIVE)) {\r
-      SafeFreePool (LoadOption);\r
+      FreePool (LoadOption);\r
       continue;\r
     }\r
     //\r
@@ -1037,7 +1008,7 @@ Returns:
     LoadOptionPtr += sizeof (UINT16);\r
 \r
     StringSize                  = StrSize ((UINT16 *) LoadOptionPtr);\r
-    NewLoadContext->Description = EfiAllocateZeroPool (StringSize);\r
+    NewLoadContext->Description = AllocateZeroPool (StringSize);\r
     ASSERT (NewLoadContext->Description != NULL);\r
     CopyMem (\r
       NewLoadContext->Description,\r
@@ -1048,7 +1019,7 @@ Returns:
 \r
     LoadOptionPtr += StringSize;\r
 \r
-    NewLoadContext->FilePathList = EfiAllocateZeroPool (NewLoadContext->FilePathListLength);\r
+    NewLoadContext->FilePathList = AllocateZeroPool (NewLoadContext->FilePathListLength);\r
     ASSERT (NewLoadContext->FilePathList != NULL);\r
     CopyMem (\r
       NewLoadContext->FilePathList,\r
@@ -1074,7 +1045,7 @@ Returns:
         StringSize -\r
         NewLoadContext->FilePathListLength;\r
 \r
-      NewLoadContext->OptionalData = EfiAllocateZeroPool (OptionalDataSize);\r
+      NewLoadContext->OptionalData = AllocateZeroPool (OptionalDataSize);\r
       ASSERT (NewLoadContext->OptionalData != NULL);\r
       CopyMem (\r
         NewLoadContext->OptionalData,\r
@@ -1089,64 +1060,48 @@ Returns:
     MenuCount++;\r
   }\r
 \r
-  SafeFreePool (BootNext);\r
-  SafeFreePool (BootOrderList);\r
+  if (BootNext != NULL) {\r
+    FreePool (BootNext);\r
+  }\r
+  if (BootOrderList != NULL) {\r
+    FreePool (BootOrderList);\r
+  }\r
   BootOptionMenu.MenuNumber = MenuCount;\r
   return MenuCount;\r
 }\r
 \r
-CHAR16 *\r
-BdsStrCpy (\r
-  OUT     CHAR16                    *Destination,\r
-  IN      CONST CHAR16              *Source\r
-  )\r
-{\r
-  CHAR16                            *ReturnValue;\r
+/**\r
 \r
-  //\r
-  // Destination cannot be NULL\r
-  //\r
-  ASSERT (Destination != NULL);\r
+  Append file name to existing file name.\r
 \r
-  ReturnValue = Destination;\r
-  while (*Source) {\r
-    *(Destination++) = *(Source++);\r
-  }\r
-  *Destination = 0;\r
-  return ReturnValue;\r
-}\r
+  @param Str1  The existing file name\r
+  @param Str2  The file name to be appended\r
+\r
+  @return Allocate a new string to hold the appended result.\r
+          Caller is responsible to free the returned string.\r
 \r
+**/\r
 CHAR16 *\r
 BOpt_AppendFileName (\r
   IN  CHAR16  *Str1,\r
   IN  CHAR16  *Str2\r
   )\r
-/*++\r
-\r
-Routine Description\r
-  Append file name to existing file name.\r
-\r
-Arguments:\r
-  Str1  -   existing file name\r
-  Str2  -   file name to be appended\r
-\r
-Returns:\r
-  Allocate a new string to hold the appended result.\r
-  Caller is responsible to free the returned string.\r
-\r
---*/\r
 {\r
   UINTN   Size1;\r
   UINTN   Size2;\r
   CHAR16  *Str;\r
+  CHAR16  *TmpStr;\r
   CHAR16  *Ptr;\r
   CHAR16  *LastSlash;\r
 \r
   Size1 = StrSize (Str1);\r
   Size2 = StrSize (Str2);\r
-  Str   = EfiAllocateZeroPool (Size1 + Size2 + sizeof (CHAR16));\r
+  Str   = AllocateZeroPool (Size1 + Size2 + sizeof (CHAR16));\r
   ASSERT (Str != NULL);\r
 \r
+  TmpStr = AllocateZeroPool (Size1 + Size2 + sizeof (CHAR16)); \r
+  ASSERT (TmpStr != NULL);\r
+\r
   StrCat (Str, Str1);\r
   if (!((*Str == '\\') && (*(Str + 1) == 0))) {\r
     StrCat (Str, L"\\");\r
@@ -1163,13 +1118,25 @@ Returns:
       // DO NOT convert the .. if it is at the end of the string. This will\r
       // break the .. behavior in changing directories.\r
       //\r
-      BdsStrCpy (LastSlash, Ptr + 3);\r
+\r
+      //\r
+      // Use TmpStr as a backup, as StrCpy in BaseLib does not handle copy of two strings \r
+      // that overlap.\r
+      //\r
+      StrCpy (TmpStr, Ptr + 3);\r
+      StrCpy (LastSlash, TmpStr);\r
       Ptr = LastSlash;\r
     } else if (*Ptr == '\\' && *(Ptr + 1) == '.' && *(Ptr + 2) == '\\') {\r
       //\r
       // Convert a "\.\" to a "\"\r
       //\r
-      BdsStrCpy (Ptr, Ptr + 2);\r
+\r
+      //\r
+      // Use TmpStr as a backup, as StrCpy in BaseLib does not handle copy of two strings \r
+      // that overlap.\r
+      //\r
+      StrCpy (TmpStr, Ptr + 2);\r
+      StrCpy (Ptr, TmpStr);\r
       Ptr = LastSlash;\r
     } else if (*Ptr == '\\') {\r
       LastSlash = Ptr;\r
@@ -1178,32 +1145,31 @@ Returns:
     Ptr++;\r
   }\r
 \r
+  FreePool (TmpStr);\r
+  \r
   return Str;\r
 }\r
 \r
-BOOLEAN\r
-BOpt_IsEfiImageName (\r
-  IN UINT16  *FileName\r
-  )\r
-/*++\r
+/**\r
 \r
-Routine Description\r
   Check whether current FileName point to a valid\r
   Efi Image File.\r
 \r
-Arguments:\r
-  FileName  -   File need to be checked.\r
+  @param FileName  File need to be checked.\r
 \r
-Returns:\r
-  TRUE  -   Is Efi Image\r
-  FALSE -   Not a valid Efi Image\r
+  @retval TRUE  Is Efi Image\r
+  @retval FALSE Not a valid Efi Image\r
 \r
---*/\r
+**/\r
+BOOLEAN\r
+BOpt_IsEfiImageName (\r
+  IN UINT16  *FileName\r
+  )\r
 {\r
   //\r
   // Search for ".efi" extension\r
   //\r
-  while (*FileName) {\r
+  while (*FileName != L'\0') {\r
     if (FileName[0] == '.') {\r
       if (FileName[1] == 'e' || FileName[1] == 'E') {\r
         if (FileName[2] == 'f' || FileName[2] == 'F') {\r
@@ -1226,25 +1192,22 @@ Returns:
   return FALSE;\r
 }\r
 \r
-BOOLEAN\r
-BOpt_IsEfiApp (\r
-  IN EFI_FILE_HANDLE Dir,\r
-  IN UINT16          *FileName\r
-  )\r
-/*++\r
+/**\r
 \r
-Routine Description:\r
   Check whether current FileName point to a valid Efi Application\r
 \r
-Arguments:\r
-  Dir       -   Pointer to current Directory\r
-  FileName  -   Pointer to current File name.\r
+  @param Dir       Pointer to current Directory\r
+  @param FileName  Pointer to current File name.\r
 \r
-Returns:\r
-  TRUE      -   Is a valid Efi Application\r
-  FALSE     -   not a valid Efi Application\r
+  @retval TRUE      Is a valid Efi Application\r
+  @retval FALSE     not a valid Efi Application\r
 \r
---*/\r
+**/\r
+BOOLEAN\r
+BOpt_IsEfiApp (\r
+  IN EFI_FILE_HANDLE Dir,\r
+  IN UINT16          *FileName\r
+  )\r
 {\r
   UINTN                       BufferSize;\r
   EFI_IMAGE_DOS_HEADER        DosHdr;\r
@@ -1293,26 +1256,21 @@ Returns:
   }\r
 }\r
 \r
-EFI_STATUS\r
-BOpt_FindDrivers (\r
-  VOID\r
-  )\r
-/*++\r
+/**\r
 \r
-Routine Description\r
   Find drivers that will be added as Driver#### variables from handles\r
   in current system environment\r
   All valid handles in the system except those consume SimpleFs, LoadFile\r
   are stored in DriverMenu for future use.\r
 \r
-Arguments:\r
-  None\r
-\r
-Returns:\r
-  EFI_SUCCESS\r
-  Others\r
+  @retval EFI_SUCCESS The function complets successfully.\r
+  @return Other value if failed to build the DriverMenu.\r
 \r
---*/\r
+**/\r
+EFI_STATUS\r
+BOpt_FindDrivers (\r
+  VOID\r
+  )\r
 {\r
   UINTN                           NoDevicePathHandles;\r
   EFI_HANDLE                      *DevicePathHandle;\r
@@ -1350,10 +1308,6 @@ Returns:
   for (Index = 0; Index < NoDevicePathHandles; Index++) {\r
     CurHandle = DevicePathHandle[Index];\r
 \r
-    //\r
-    //  Check whether this handle support\r
-    //  driver binding\r
-    //\r
     Status = gBS->HandleProtocol (\r
                     CurHandle,\r
                     &gEfiSimpleFileSystemProtocolGuid,\r
@@ -1374,7 +1328,7 @@ Returns:
 \r
     NewMenuEntry = BOpt_CreateMenuEntry (BM_HANDLE_CONTEXT_SELECT);\r
     if (NULL == NewMenuEntry) {\r
-      SafeFreePool (DevicePathHandle);\r
+      FreePool (DevicePathHandle);\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
 \r
@@ -1388,27 +1342,26 @@ Returns:
     InsertTailList (&DriverMenu.Head, &NewMenuEntry->Link);\r
 \r
   }\r
-  SafeFreePool (DevicePathHandle);\r
+\r
+  if (DevicePathHandle != NULL) {\r
+    FreePool (DevicePathHandle);\r
+  }\r
 \r
   DriverMenu.MenuNumber = OptionNumber;\r
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+\r
+  Get the Option Number that has not been allocated for use.\r
+\r
+  @return The available Option Number.\r
+\r
+**/\r
 UINT16\r
 BOpt_GetBootOptionNumber (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Get the Option Number that does not used\r
-\r
-Arguments:\r
-\r
-Returns:\r
-  The Option Number\r
-\r
---*/\r
 {\r
   BM_MENU_ENTRY *NewMenuEntry;\r
   UINT16        *BootOrderList;\r
@@ -1429,7 +1382,7 @@ Returns:
                     &gEfiGlobalVariableGuid,\r
                     &BootOrderListSize\r
                     );\r
-  if (BootOrderList) {\r
+  if (BootOrderList != NULL) {\r
     //\r
     // already have Boot####\r
     //\r
@@ -1446,15 +1399,16 @@ Returns:
       }\r
 \r
       if (Found) {\r
-     UnicodeSPrint (StrTemp, 100, L"Boot%04x", Index);\r
-     DEBUG((DEBUG_ERROR,"INdex= %s\n", StrTemp));\r
-          OptionBuffer = BdsLibGetVariableAndSize (\r
-                    StrTemp,\r
-                    &gEfiGlobalVariableGuid,\r
-                    &OptionSize\r
-                    );\r
-         if (NULL == OptionBuffer)\r
-            break;\r
+        UnicodeSPrint (StrTemp, 100, L"Boot%04x", Index);\r
+        DEBUG((DEBUG_ERROR,"INdex= %s\n", StrTemp));\r
+        OptionBuffer = BdsLibGetVariableAndSize (\r
+                          StrTemp,\r
+                          &gEfiGlobalVariableGuid,\r
+                          &OptionSize\r
+                          );\r
+        if (NULL == OptionBuffer) {\r
+          break;\r
+        }\r
       }\r
     }\r
     //\r
@@ -1471,21 +1425,17 @@ Returns:
   return Number;\r
 }\r
 \r
+/**\r
+\r
+  Get the Option Number that is not in use.\r
+\r
+  @return The unused Option Number.\r
+\r
+**/\r
 UINT16\r
 BOpt_GetDriverOptionNumber (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Get the Option Number that does not used\r
-\r
-Arguments:\r
-\r
-Returns:\r
-  The Option Number\r
-\r
---*/\r
 {\r
   BM_MENU_ENTRY *NewMenuEntry;\r
   UINT16        *DriverOrderList;\r
@@ -1503,7 +1453,7 @@ Returns:
                       &gEfiGlobalVariableGuid,\r
                       &DriverOrderListSize\r
                       );\r
-  if (DriverOrderList) {\r
+  if (DriverOrderList != NULL) {\r
     //\r
     // already have Driver####\r
     //\r
@@ -1537,21 +1487,21 @@ Returns:
   return Number;\r
 }\r
 \r
-EFI_STATUS\r
-BOpt_GetDriverOptions (\r
-  IN  BMM_CALLBACK_DATA         *CallbackData\r
-  )\r
-/*++\r
+/**\r
 \r
-Routine Description:\r
   Build up all DriverOptionMenu\r
 \r
-Arguments:\r
+  @param CallbackData The BMM context data.\r
 \r
-Returns:\r
-  The Option Number\r
+  @return EFI_SUCESS The functin completes successfully.\r
+  @retval EFI_OUT_OF_RESOURCES Not enough memory to compete the operation.\r
+  \r
 \r
---*/\r
+**/\r
+EFI_STATUS\r
+BOpt_GetDriverOptions (\r
+  IN  BMM_CALLBACK_DATA         *CallbackData\r
+  )\r
 {\r
   UINTN           Index;\r
   UINT16          DriverString[12];\r
@@ -1598,17 +1548,17 @@ Returns:
                           &gEfiGlobalVariableGuid,\r
                           &DriverOptionSize\r
                           );\r
-    if (!LoadOptionFromVar) {\r
+    if (LoadOptionFromVar == NULL) {\r
       continue;\r
     }\r
 \r
-    LoadOption = EfiAllocateZeroPool (DriverOptionSize);\r
-    if (!LoadOption) {\r
+    LoadOption = AllocateZeroPool (DriverOptionSize);\r
+    if (LoadOption == NULL) {\r
       continue;\r
     }\r
 \r
     CopyMem (LoadOption, LoadOptionFromVar, DriverOptionSize);\r
-    SafeFreePool (LoadOptionFromVar);\r
+    FreePool (LoadOptionFromVar);\r
 \r
     NewMenuEntry = BOpt_CreateMenuEntry (BM_LOAD_CONTEXT_SELECT);\r
     if (NULL == NewMenuEntry) {\r
@@ -1642,7 +1592,7 @@ Returns:
     LoadOptionPtr += sizeof (UINT16);\r
 \r
     StringSize                  = StrSize ((UINT16 *) LoadOptionPtr);\r
-    NewLoadContext->Description = EfiAllocateZeroPool (StringSize);\r
+    NewLoadContext->Description = AllocateZeroPool (StringSize);\r
     ASSERT (NewLoadContext->Description != NULL);\r
     CopyMem (\r
       NewLoadContext->Description,\r
@@ -1653,7 +1603,7 @@ Returns:
 \r
     LoadOptionPtr += StringSize;\r
 \r
-    NewLoadContext->FilePathList = EfiAllocateZeroPool (NewLoadContext->FilePathListLength);\r
+    NewLoadContext->FilePathList = AllocateZeroPool (NewLoadContext->FilePathListLength);\r
     ASSERT (NewLoadContext->FilePathList != NULL);\r
     CopyMem (\r
       NewLoadContext->FilePathList,\r
@@ -1679,7 +1629,7 @@ Returns:
         StringSize -\r
         NewLoadContext->FilePathListLength;\r
 \r
-      NewLoadContext->OptionalData = EfiAllocateZeroPool (OptionalDataSize);\r
+      NewLoadContext->OptionalData = AllocateZeroPool (OptionalDataSize);\r
       ASSERT (NewLoadContext->OptionalData != NULL);\r
       CopyMem (\r
         NewLoadContext->OptionalData,\r
@@ -1694,7 +1644,9 @@ Returns:
 \r
   }\r
 \r
-  SafeFreePool (DriverOrderList);\r
+  if (DriverOrderList != NULL) {\r
+    FreePool (DriverOrderList);\r
+  }\r
   DriverOptionMenu.MenuNumber = Index;\r
   return EFI_SUCCESS;\r
 \r