]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/VariableAuthenticated/SecureBootConfigDxe/SecureBootConfigFileExplorer.c
1. Remove “Force clear PK” feature in AuthVarialbe driver.
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / SecureBootConfigDxe / SecureBootConfigFileExplorer.c
diff --git a/SecurityPkg/VariableAuthenticated/SecureBootConfigDxe/SecureBootConfigFileExplorer.c b/SecurityPkg/VariableAuthenticated/SecureBootConfigDxe/SecureBootConfigFileExplorer.c
new file mode 100644 (file)
index 0000000..deff87b
--- /dev/null
@@ -0,0 +1,1227 @@
+/** @file\r
+  Internal file explorer functions for SecureBoot configuration module.\r
+\r
+Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+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
+#include "SecureBootConfigImpl.h"\r
+\r
+///\r
+/// File system selection menu\r
+///\r
+SECUREBOOT_MENU_OPTION      FsOptionMenu = {\r
+  SECUREBOOT_MENU_OPTION_SIGNATURE,\r
+  {NULL},\r
+  0\r
+};\r
+\r
+///\r
+/// Files and sub-directories in current directory menu\r
+///\r
+SECUREBOOT_MENU_OPTION      DirectoryMenu = {\r
+  SECUREBOOT_MENU_OPTION_SIGNATURE,\r
+  {NULL},\r
+  0\r
+};\r
+\r
+VOID                  *mStartOpCodeHandle = NULL;\r
+VOID                  *mEndOpCodeHandle = NULL;\r
+EFI_IFR_GUID_LABEL    *mStartLabel = NULL;\r
+EFI_IFR_GUID_LABEL    *mEndLabel = NULL;\r
+\r
+/**\r
+  Duplicate a string.\r
+\r
+  @param[in]    Src             The source string.\r
+\r
+  @return      A new string which is duplicated copy of the source,\r
+               or NULL if there is not enough memory.\r
+\r
+**/\r
+CHAR16 *\r
+StrDuplicate (\r
+  IN CHAR16   *Src\r
+  )\r
+{\r
+  CHAR16  *Dest;\r
+  UINTN   Size;\r
+\r
+  Size  = StrSize (Src);\r
+  Dest  = AllocateZeroPool (Size);\r
+  ASSERT (Dest != NULL);\r
+  if (Dest != NULL) {\r
+    CopyMem (Dest, Src, Size);\r
+  }\r
+\r
+  return Dest;\r
+}\r
+\r
+/**\r
+  Helper function called as part of the code needed to allocate \r
+  the proper sized buffer for various EFI interfaces.\r
+\r
+  @param[in, out]   Status          Current status\r
+  @param[in, out]   Buffer          Current allocated buffer, or NULL\r
+  @param[in]        BufferSize      Current buffer size needed\r
+\r
+  @retval  TRUE         If the buffer was reallocated and the caller\r
+                        should try the API again.\r
+  @retval  FALSE        The caller should not call this function again.\r
+\r
+**/\r
+BOOLEAN\r
+GrowBuffer (\r
+  IN OUT EFI_STATUS   *Status,\r
+  IN OUT VOID         **Buffer,\r
+  IN UINTN            BufferSize\r
+  )\r
+{\r
+  BOOLEAN TryAgain;\r
+\r
+  //\r
+  // If this is an initial request, buffer will be null with a new buffer size\r
+  //\r
+  if ((*Buffer == NULL) && (BufferSize != 0)) {\r
+    *Status = EFI_BUFFER_TOO_SMALL;\r
+  }\r
+  //\r
+  // If the status code is "buffer too small", resize the buffer\r
+  //\r
+  TryAgain = FALSE;\r
+  if (*Status == EFI_BUFFER_TOO_SMALL) {\r
+\r
+    if (*Buffer != NULL) {\r
+      FreePool (*Buffer);\r
+    }\r
+\r
+    *Buffer = AllocateZeroPool (BufferSize);\r
+\r
+    if (*Buffer != NULL) {\r
+      TryAgain = TRUE;\r
+    } else {\r
+      *Status = EFI_OUT_OF_RESOURCES;\r
+    }\r
+  }\r
+  //\r
+  // If there's an error, free the buffer\r
+  //\r
+  if (!TryAgain && EFI_ERROR (*Status) && (*Buffer != NULL)) {\r
+    FreePool (*Buffer);\r
+    *Buffer = NULL;\r
+  }\r
+\r
+  return TryAgain;\r
+}\r
+\r
+/**\r
+  Append file name to existing file name, and allocate a new buffer \r
+  to hold the appended result.\r
+\r
+  @param[in]  Str1  The existing file name\r
+  @param[in]  Str2  The file name to be appended\r
+\r
+  @return      A new string with appended result.\r
+\r
+**/\r
+CHAR16 *\r
+AppendFileName (\r
+  IN  CHAR16  *Str1,\r
+  IN  CHAR16  *Str2\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   = 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
+  }\r
+\r
+  StrCat (Str, Str2);\r
+\r
+  Ptr       = Str;\r
+  LastSlash = Str;\r
+  while (*Ptr != 0) {\r
+    if (*Ptr == '\\' && *(Ptr + 1) == '.' && *(Ptr + 2) == '.' && *(Ptr + 3) == L'\\') {\r
+      //\r
+      // Convert "\Name\..\" to "\"\r
+      // DO NOT convert the .. if it is at the end of the string. This will\r
+      // break the .. behavior in changing directories.\r
+      //\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
+\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
+    }\r
+\r
+    Ptr++;\r
+  }\r
+\r
+  FreePool (TmpStr);\r
+  \r
+  return Str;\r
+}\r
+\r
+/**\r
+  Create a SECUREBOOT_MENU_ENTRY, and stores it in a buffer allocated from the pool.\r
+\r
+  @return           The new menu entry or NULL of error happens.\r
+\r
+**/\r
+SECUREBOOT_MENU_ENTRY *\r
+CreateMenuEntry (\r
+  VOID\r
+  )\r
+{\r
+  SECUREBOOT_MENU_ENTRY *MenuEntry;\r
+  UINTN                 ContextSize;\r
+\r
+  //\r
+  // Create new menu entry\r
+  //\r
+  MenuEntry = AllocateZeroPool (sizeof (SECUREBOOT_MENU_ENTRY));\r
+  if (MenuEntry == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  ContextSize = sizeof (SECUREBOOT_FILE_CONTEXT);\r
+  MenuEntry->FileContext = AllocateZeroPool (ContextSize);\r
+  if (MenuEntry->FileContext == NULL) {\r
+    FreePool (MenuEntry);\r
+    return NULL;\r
+  }\r
+\r
+  MenuEntry->Signature = SECUREBOOT_MENU_ENTRY_SIGNATURE;\r
+\r
+  return MenuEntry;\r
+}\r
+\r
+/**\r
+  Get Menu Entry from the Menu Entry List by MenuNumber.\r
+\r
+  If MenuNumber is great or equal to the number of Menu\r
+  Entry in the list, then ASSERT.\r
+\r
+  @param[in]  MenuOption      The Menu Entry List to read the menu entry.\r
+  @param[in]  MenuNumber      The index of Menu Entry.\r
+\r
+  @return     The Menu Entry.\r
+\r
+**/\r
+SECUREBOOT_MENU_ENTRY *\r
+GetMenuEntry (\r
+  IN  SECUREBOOT_MENU_OPTION      *MenuOption,\r
+  IN  UINTN                       MenuNumber\r
+  )\r
+{\r
+  SECUREBOOT_MENU_ENTRY       *NewMenuEntry;\r
+  UINTN                       Index;\r
+  LIST_ENTRY                  *List;\r
+\r
+  ASSERT (MenuNumber < MenuOption->MenuNumber);\r
+\r
+  List = MenuOption->Head.ForwardLink;\r
+  for (Index = 0; Index < MenuNumber; Index++) {\r
+    List = List->ForwardLink;\r
+  }\r
+\r
+  NewMenuEntry = CR (List, SECUREBOOT_MENU_ENTRY, Link, SECUREBOOT_MENU_ENTRY_SIGNATURE);\r
+\r
+  return NewMenuEntry;\r
+}\r
+\r
+/**\r
+  Create string tokens for a menu from its help strings and display strings.\r
+\r
+  @param[in] HiiHandle          Hii Handle of the package to be updated.\r
+  @param[in] MenuOption         The Menu whose string tokens need to be created.\r
+\r
+**/\r
+VOID\r
+CreateMenuStringToken (\r
+  IN EFI_HII_HANDLE                          HiiHandle,\r
+  IN SECUREBOOT_MENU_OPTION                  *MenuOption\r
+  )\r
+{\r
+  SECUREBOOT_MENU_ENTRY *NewMenuEntry;\r
+  UINTN         Index;\r
+\r
+  for (Index = 0; Index < MenuOption->MenuNumber; Index++) {\r
+    NewMenuEntry = GetMenuEntry (MenuOption, Index);\r
+\r
+    NewMenuEntry->DisplayStringToken = HiiSetString (\r
+                                         HiiHandle,\r
+                                         0,\r
+                                         NewMenuEntry->DisplayString,\r
+                                         NULL\r
+                                         );\r
+\r
+    if (NewMenuEntry->HelpString == NULL) {\r
+      NewMenuEntry->HelpStringToken = NewMenuEntry->DisplayStringToken;\r
+    } else {\r
+      NewMenuEntry->HelpStringToken = HiiSetString (\r
+                                        HiiHandle,\r
+                                        0,\r
+                                        NewMenuEntry->HelpString,\r
+                                        NULL\r
+                                        );\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  Free up all resources allocated for a SECUREBOOT_MENU_ENTRY.\r
+\r
+  @param[in, out]  MenuEntry   A pointer to SECUREBOOT_MENU_ENTRY.\r
+\r
+**/\r
+VOID\r
+DestroyMenuEntry (\r
+  IN OUT SECUREBOOT_MENU_ENTRY         *MenuEntry\r
+  )\r
+{\r
+  SECUREBOOT_FILE_CONTEXT           *FileContext;\r
+\r
+\r
+  FileContext = (SECUREBOOT_FILE_CONTEXT *) MenuEntry->FileContext;\r
+\r
+  if (!FileContext->IsRoot) {\r
+    FreePool (FileContext->DevicePath);\r
+  } else {\r
+    if (FileContext->FHandle != NULL) {\r
+      FileContext->FHandle->Close (FileContext->FHandle);\r
+    }\r
+  }\r
+\r
+  if (FileContext->FileName != NULL) {\r
+    FreePool (FileContext->FileName);\r
+  }\r
+  if (FileContext->Info != NULL) {\r
+    FreePool (FileContext->Info);\r
+  }\r
+\r
+  FreePool (FileContext);\r
+\r
+  FreePool (MenuEntry->DisplayString);\r
+  if (MenuEntry->HelpString != NULL) {\r
+    FreePool (MenuEntry->HelpString);\r
+  }\r
+\r
+  FreePool (MenuEntry);\r
+}\r
+\r
+/**\r
+  Free resources allocated in Allocate Rountine.\r
+\r
+  @param[in, out]  MenuOption        Menu to be freed\r
+  \r
+**/\r
+VOID\r
+FreeMenu (\r
+  IN OUT SECUREBOOT_MENU_OPTION        *MenuOption\r
+  )\r
+{\r
+  SECUREBOOT_MENU_ENTRY      *MenuEntry;\r
+  while (!IsListEmpty (&MenuOption->Head)) {\r
+    MenuEntry = CR (\r
+                  MenuOption->Head.ForwardLink,\r
+                  SECUREBOOT_MENU_ENTRY,\r
+                  Link,\r
+                  SECUREBOOT_MENU_ENTRY_SIGNATURE\r
+                  );\r
+    RemoveEntryList (&MenuEntry->Link);\r
+    DestroyMenuEntry (MenuEntry);\r
+  }\r
+  MenuOption->MenuNumber = 0;\r
+}\r
+\r
+/**\r
+  This function gets the file information from an open file descriptor, and stores it\r
+  in a buffer allocated from pool.\r
+\r
+  @param[in]  FHand           File Handle.\r
+\r
+  @return    A pointer to a buffer with file information or NULL is returned\r
+\r
+**/\r
+EFI_FILE_INFO *\r
+FileInfo (\r
+  IN EFI_FILE_HANDLE      FHand\r
+  )\r
+{\r
+  EFI_STATUS    Status;\r
+  EFI_FILE_INFO *Buffer;\r
+  UINTN         BufferSize;\r
+\r
+  //\r
+  // Initialize for GrowBuffer loop\r
+  //\r
+  Buffer      = NULL;\r
+  BufferSize  = SIZE_OF_EFI_FILE_INFO + 200;\r
+\r
+  //\r
+  // Call the real function\r
+  //\r
+  while (GrowBuffer (&Status, (VOID **) &Buffer, BufferSize)) {\r
+    Status = FHand->GetInfo (\r
+                      FHand,\r
+                      &gEfiFileInfoGuid,\r
+                      &BufferSize,\r
+                      Buffer\r
+                      );\r
+  }\r
+\r
+  return Buffer;\r
+}\r
+\r
+/**\r
+  This function gets the file system information from an open file descriptor,\r
+  and stores it in a buffer allocated from pool.\r
+\r
+  @param[in] FHand       The file handle.\r
+\r
+  @return                A pointer to a buffer with file information.\r
+  @retval                NULL is returned if failed to get Vaolume Label Info.\r
+\r
+**/\r
+EFI_FILE_SYSTEM_VOLUME_LABEL *\r
+FileSystemVolumeLabelInfo (\r
+  IN EFI_FILE_HANDLE      FHand\r
+  )\r
+{\r
+  EFI_STATUS                        Status;\r
+  EFI_FILE_SYSTEM_VOLUME_LABEL      *Buffer;\r
+  UINTN                             BufferSize;\r
+  //\r
+  // Initialize for GrowBuffer loop\r
+  //\r
+  Buffer      = NULL;\r
+  BufferSize  = SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL + 200;\r
+\r
+  //\r
+  // Call the real function\r
+  //\r
+  while (GrowBuffer (&Status, (VOID **) &Buffer, BufferSize)) {\r
+    Status = FHand->GetInfo (\r
+                      FHand,\r
+                      &gEfiFileSystemVolumeLabelInfoIdGuid,\r
+                      &BufferSize,\r
+                      Buffer\r
+                      );\r
+  }\r
+\r
+  return Buffer;\r
+}\r
+\r
+/**\r
+  This function will open a file or directory referenced by DevicePath.\r
+\r
+  This function opens a file with the open mode according to the file path. The\r
+  Attributes is valid only for EFI_FILE_MODE_CREATE.\r
+\r
+  @param[in, out]  FilePath        On input, the device path to the file.  \r
+                                   On output, the remaining device path.\r
+  @param[out]      FileHandle      Pointer to the file handle.\r
+  @param[in]       OpenMode        The mode to open the file with.\r
+  @param[in]       Attributes      The file's file attributes.\r
+\r
+  @retval EFI_SUCCESS              The information was set.\r
+  @retval EFI_INVALID_PARAMETER    One of the parameters has an invalid value.\r
+  @retval EFI_UNSUPPORTED          Could not open the file path.\r
+  @retval EFI_NOT_FOUND            The specified file could not be found on the\r
+                                   device or the file system could not be found on\r
+                                   the device.\r
+  @retval EFI_NO_MEDIA             The device has no medium.\r
+  @retval EFI_MEDIA_CHANGED        The device has a different medium in it or the\r
+                                   medium is no longer supported.\r
+  @retval EFI_DEVICE_ERROR         The device reported an error.\r
+  @retval EFI_VOLUME_CORRUPTED     The file system structures are corrupted.\r
+  @retval EFI_WRITE_PROTECTED      The file or medium is write protected.\r
+  @retval EFI_ACCESS_DENIED        The file was opened read only.\r
+  @retval EFI_OUT_OF_RESOURCES     Not enough resources were available to open the\r
+                                   file.\r
+  @retval EFI_VOLUME_FULL          The volume is full.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+OpenFileByDevicePath(\r
+  IN OUT EFI_DEVICE_PATH_PROTOCOL     **FilePath,\r
+  OUT EFI_FILE_HANDLE                 *FileHandle,\r
+  IN UINT64                           OpenMode,\r
+  IN UINT64                           Attributes\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;\r
+  EFI_FILE_PROTOCOL               *Handle1;\r
+  EFI_FILE_PROTOCOL               *Handle2;\r
+  EFI_HANDLE                      DeviceHandle; \r
+\r
+  if ((FilePath == NULL || FileHandle == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Status = gBS->LocateDevicePath (\r
+                  &gEfiSimpleFileSystemProtocolGuid,\r
+                  FilePath,\r
+                  &DeviceHandle\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Status = gBS->OpenProtocol(\r
+                  DeviceHandle,\r
+                  &gEfiSimpleFileSystemProtocolGuid,\r
+                  (VOID**)&EfiSimpleFileSystemProtocol,\r
+                  gImageHandle,\r
+                  NULL,\r
+                  EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);\r
+  if (EFI_ERROR (Status)) {\r
+    FileHandle = NULL;\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // go down directories one node at a time.\r
+  //\r
+  while (!IsDevicePathEnd (*FilePath)) {\r
+    //\r
+    // For file system access each node should be a file path component\r
+    //\r
+    if (DevicePathType    (*FilePath) != MEDIA_DEVICE_PATH ||\r
+        DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP\r
+       ) {\r
+      FileHandle = NULL;\r
+      return (EFI_INVALID_PARAMETER);\r
+    }\r
+    //\r
+    // Open this file path node\r
+    //\r
+    Handle2  = Handle1;\r
+    Handle1 = NULL;\r
+\r
+    //\r
+    // Try to test opening an existing file\r
+    //\r
+    Status = Handle2->Open (\r
+                          Handle2,\r
+                          &Handle1,\r
+                          ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,\r
+                          OpenMode &~EFI_FILE_MODE_CREATE,\r
+                          0\r
+                         );\r
+\r
+    //\r
+    // see if the error was that it needs to be created\r
+    //\r
+    if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {\r
+      Status = Handle2->Open (\r
+                            Handle2,\r
+                            &Handle1,\r
+                            ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,\r
+                            OpenMode,\r
+                            Attributes\r
+                           );\r
+    }\r
+    //\r
+    // Close the last node\r
+    //\r
+    Handle2->Close (Handle2);\r
+\r
+    if (EFI_ERROR(Status)) {\r
+      return (Status);\r
+    }\r
+\r
+    //\r
+    // Get the next node\r
+    //\r
+    *FilePath = NextDevicePathNode (*FilePath);\r
+  }\r
+\r
+  //\r
+  // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!\r
+  //\r
+  *FileHandle = (VOID*)Handle1;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Function opens and returns a file handle to the root directory of a volume.\r
+\r
+  @param[in]   DeviceHandle    A handle for a device\r
+\r
+  @return      A valid file handle or NULL if error happens.\r
+\r
+**/\r
+EFI_FILE_HANDLE\r
+OpenRoot (\r
+  IN EFI_HANDLE                   DeviceHandle\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r
+  EFI_FILE_HANDLE                 File;\r
+\r
+  File = NULL;\r
+\r
+  //\r
+  // File the file system interface to the device\r
+  //\r
+  Status = gBS->HandleProtocol (\r
+                  DeviceHandle,\r
+                  &gEfiSimpleFileSystemProtocolGuid,\r
+                  (VOID *) &Volume\r
+                  );\r
+\r
+  //\r
+  // Open the root directory of the volume\r
+  //\r
+  if (!EFI_ERROR (Status)) {\r
+    Status = Volume->OpenVolume (\r
+                      Volume,\r
+                      &File\r
+                      );\r
+  }\r
+  //\r
+  // Done\r
+  //\r
+  return EFI_ERROR (Status) ? NULL : File;\r
+}\r
+\r
+/**\r
+  This function builds the FsOptionMenu list which records all\r
+  available file system in the system. They include 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
+  @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
+FindFileSystem (\r
+  VOID\r
+  )\r
+{\r
+  UINTN                     NoBlkIoHandles;\r
+  UINTN                     NoSimpleFsHandles;\r
+  UINTN                     NoLoadFileHandles;\r
+  EFI_HANDLE                *BlkIoHandle;\r
+  EFI_HANDLE                *SimpleFsHandle;\r
+  UINT16                    *VolumeLabel;\r
+  EFI_BLOCK_IO_PROTOCOL     *BlkIo;\r
+  UINTN                     Index;\r
+  EFI_STATUS                Status;\r
+  SECUREBOOT_MENU_ENTRY     *MenuEntry;\r
+  SECUREBOOT_FILE_CONTEXT   *FileContext;\r
+  UINT16                    *TempStr;\r
+  UINTN                     OptionNumber;\r
+  VOID                      *Buffer;\r
+\r
+  BOOLEAN                   RemovableMedia;\r
+\r
+\r
+  NoSimpleFsHandles = 0;\r
+  NoLoadFileHandles = 0;\r
+  OptionNumber      = 0;\r
+  InitializeListHead (&FsOptionMenu.Head);\r
+\r
+  //\r
+  // Locate Handles that support BlockIo protocol\r
+  //\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  &gEfiBlockIoProtocolGuid,\r
+                  NULL,\r
+                  &NoBlkIoHandles,\r
+                  &BlkIoHandle\r
+                  );\r
+  if (!EFI_ERROR (Status)) {\r
+\r
+    for (Index = 0; Index < NoBlkIoHandles; Index++) {\r
+      Status = gBS->HandleProtocol (\r
+                      BlkIoHandle[Index],\r
+                      &gEfiBlockIoProtocolGuid,\r
+                      (VOID **) &BlkIo\r
+                      );\r
+\r
+      if (EFI_ERROR (Status)) {\r
+        continue;\r
+      }\r
+\r
+      //\r
+      // Issue a dummy read to trigger reinstall of BlockIo protocol for removable media\r
+      //\r
+      if (BlkIo->Media->RemovableMedia) {\r
+        Buffer = AllocateZeroPool (BlkIo->Media->BlockSize);\r
+        if (NULL == Buffer) {\r
+          FreePool (BlkIoHandle);\r
+          return EFI_OUT_OF_RESOURCES;\r
+        }\r
+\r
+        BlkIo->ReadBlocks (\r
+                BlkIo,\r
+                BlkIo->Media->MediaId,\r
+                0,\r
+                BlkIo->Media->BlockSize,\r
+                Buffer\r
+                );\r
+        FreePool (Buffer);\r
+      }\r
+    }\r
+    FreePool (BlkIoHandle);\r
+  }\r
+\r
+  //\r
+  // Locate Handles that support Simple File System protocol\r
+  //\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  &gEfiSimpleFileSystemProtocolGuid,\r
+                  NULL,\r
+                  &NoSimpleFsHandles,\r
+                  &SimpleFsHandle\r
+                  );\r
+  if (!EFI_ERROR (Status)) {\r
+    //\r
+    // Find all the instances of the File System prototocol\r
+    //\r
+    for (Index = 0; Index < NoSimpleFsHandles; Index++) {\r
+      Status = gBS->HandleProtocol (\r
+                      SimpleFsHandle[Index],\r
+                      &gEfiBlockIoProtocolGuid,\r
+                      (VOID **) &BlkIo\r
+                      );\r
+      if (EFI_ERROR (Status)) {\r
+        //\r
+        // If no block IO exists assume it's NOT a removable media\r
+        //\r
+        RemovableMedia = FALSE;\r
+      } else {\r
+        //\r
+        // If block IO exists check to see if it's remobable media\r
+        //\r
+        RemovableMedia = BlkIo->Media->RemovableMedia;\r
+      }\r
+\r
+      //\r
+      // Allocate pool for this instance.\r
+      //\r
+      MenuEntry = CreateMenuEntry ();\r
+      if (NULL == MenuEntry) {\r
+        FreePool (SimpleFsHandle);\r
+        return EFI_OUT_OF_RESOURCES;\r
+      }\r
+\r
+      FileContext = (SECUREBOOT_FILE_CONTEXT *) MenuEntry->FileContext;\r
+\r
+      FileContext->Handle     = SimpleFsHandle[Index];\r
+      MenuEntry->OptionNumber = Index;\r
+      FileContext->FHandle    = OpenRoot (FileContext->Handle);\r
+      if (FileContext->FHandle == NULL) {\r
+        DestroyMenuEntry (MenuEntry);\r
+        continue;\r
+      }\r
+\r
+      MenuEntry->HelpString = DevicePathToStr (DevicePathFromHandle (FileContext->Handle));\r
+      FileContext->Info = FileSystemVolumeLabelInfo (FileContext->FHandle);\r
+      FileContext->FileName = StrDuplicate (L"\\");\r
+      FileContext->DevicePath = FileDevicePath (\r
+                                  FileContext->Handle,\r
+                                  FileContext->FileName\r
+                                  );\r
+      FileContext->IsDir            = TRUE;\r
+      FileContext->IsRoot           = TRUE;\r
+      FileContext->IsRemovableMedia = RemovableMedia;\r
+      FileContext->IsLoadFile       = FALSE;\r
+\r
+      //\r
+      // Get current file system's Volume Label\r
+      //\r
+      if (FileContext->Info == NULL) {\r
+        VolumeLabel = L"NO FILE SYSTEM INFO";\r
+      } else {\r
+        if (FileContext->Info->VolumeLabel == NULL) {\r
+          VolumeLabel = L"NULL VOLUME LABEL";\r
+        } else {\r
+          VolumeLabel = FileContext->Info->VolumeLabel;\r
+          if (*VolumeLabel == 0x0000) {\r
+            VolumeLabel = L"NO VOLUME LABEL";\r
+          }\r
+        }\r
+      }\r
+\r
+      TempStr                   = MenuEntry->HelpString;\r
+      MenuEntry->DisplayString  = AllocateZeroPool (MAX_CHAR);\r
+      ASSERT (MenuEntry->DisplayString != NULL);\r
+      UnicodeSPrint (\r
+        MenuEntry->DisplayString,\r
+        MAX_CHAR,\r
+        L"%s, [%s]",\r
+        VolumeLabel,\r
+        TempStr\r
+        );\r
+      OptionNumber++;\r
+      InsertTailList (&FsOptionMenu.Head, &MenuEntry->Link);\r
+    }\r
+  }\r
+\r
+  if (NoSimpleFsHandles != 0) {\r
+    FreePool (SimpleFsHandle);\r
+  }\r
+  \r
+  //\r
+  // Remember how many file system options are here\r
+  //\r
+  FsOptionMenu.MenuNumber = OptionNumber;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Find files under the current directory. All files and sub-directories \r
+  in current directory will be stored in DirectoryMenu for future use.\r
+\r
+  @param[in] MenuEntry     The Menu Entry.\r
+\r
+  @retval EFI_SUCCESS      Get files from current dir successfully.\r
+  @return Other            Can't get files from current dir.\r
+\r
+**/\r
+EFI_STATUS\r
+FindFiles (\r
+  IN SECUREBOOT_MENU_ENTRY              *MenuEntry\r
+  )\r
+{\r
+  EFI_FILE_HANDLE           NewDir;\r
+  EFI_FILE_HANDLE           Dir;\r
+  EFI_FILE_INFO             *DirInfo;\r
+  UINTN                     BufferSize;\r
+  UINTN                     DirBufferSize;\r
+  SECUREBOOT_MENU_ENTRY     *NewMenuEntry;\r
+  SECUREBOOT_FILE_CONTEXT   *FileContext;\r
+  SECUREBOOT_FILE_CONTEXT   *NewFileContext;\r
+  UINTN                     Pass;\r
+  EFI_STATUS                Status;\r
+  UINTN                     OptionNumber;\r
+\r
+  FileContext   = (SECUREBOOT_FILE_CONTEXT *) MenuEntry->FileContext;\r
+  Dir           = FileContext->FHandle;\r
+  OptionNumber  = 0;\r
+  //\r
+  // Open current directory to get files from it\r
+  //\r
+  Status = Dir->Open (\r
+                  Dir,\r
+                  &NewDir,\r
+                  FileContext->FileName,\r
+                  EFI_FILE_READ_ONLY,\r
+                  0\r
+                  );\r
+  if (!FileContext->IsRoot) {\r
+    Dir->Close (Dir);\r
+  }\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  DirInfo = FileInfo (NewDir);\r
+  if (DirInfo == NULL) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  if ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  FileContext->DevicePath = FileDevicePath (\r
+                              FileContext->Handle,\r
+                              FileContext->FileName\r
+                              );\r
+\r
+  DirBufferSize = sizeof (EFI_FILE_INFO) + 1024;\r
+  DirInfo       = AllocateZeroPool (DirBufferSize);\r
+  if (DirInfo == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  \r
+  //\r
+  // Get all files in current directory\r
+  // Pass 1 to get Directories\r
+  // Pass 2 to get files that are EFI images\r
+  //\r
+  for (Pass = 1; Pass <= 2; Pass++) {\r
+    NewDir->SetPosition (NewDir, 0);\r
+    for (;;) {\r
+      BufferSize  = DirBufferSize;\r
+      Status      = NewDir->Read (NewDir, &BufferSize, DirInfo);\r
+      if (EFI_ERROR (Status) || BufferSize == 0) {\r
+        break;\r
+      }\r
+\r
+      if (((DirInfo->Attribute & EFI_FILE_DIRECTORY) != 0 && Pass == 2) ||\r
+          ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0 && Pass == 1)\r
+          ) {\r
+        //\r
+        // Pass 1 is for Directories\r
+        // Pass 2 is for file names\r
+        //\r
+        continue;\r
+      }\r
+\r
+      NewMenuEntry = CreateMenuEntry ();\r
+      if (NULL == NewMenuEntry) {\r
+        return EFI_OUT_OF_RESOURCES;\r
+      }\r
+\r
+      NewFileContext          = (SECUREBOOT_FILE_CONTEXT *) NewMenuEntry->FileContext;\r
+      NewFileContext->Handle  = FileContext->Handle;\r
+      NewFileContext->FileName = AppendFileName (\r
+                                  FileContext->FileName,\r
+                                  DirInfo->FileName\r
+                                  );\r
+      NewFileContext->FHandle = NewDir;\r
+      NewFileContext->DevicePath = FileDevicePath (\r
+                                    NewFileContext->Handle,\r
+                                    NewFileContext->FileName\r
+                                    );\r
+      NewMenuEntry->HelpString = NULL;\r
+      \r
+      NewFileContext->IsDir = (BOOLEAN) ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY);\r
+      if (NewFileContext->IsDir) {\r
+        BufferSize = StrLen (DirInfo->FileName) * 2 + 6;\r
+        NewMenuEntry->DisplayString = AllocateZeroPool (BufferSize);\r
+\r
+        UnicodeSPrint (\r
+          NewMenuEntry->DisplayString,\r
+          BufferSize,\r
+          L"<%s>",\r
+          DirInfo->FileName\r
+          );\r
+\r
+      } else {\r
+        NewMenuEntry->DisplayString = StrDuplicate (DirInfo->FileName);\r
+      }\r
+\r
+      NewFileContext->IsRoot            = FALSE;\r
+      NewFileContext->IsLoadFile        = FALSE;\r
+      NewFileContext->IsRemovableMedia  = FALSE;\r
+\r
+      NewMenuEntry->OptionNumber        = OptionNumber;\r
+      OptionNumber++;\r
+      InsertTailList (&DirectoryMenu.Head, &NewMenuEntry->Link);\r
+    }\r
+  }\r
+\r
+  DirectoryMenu.MenuNumber = OptionNumber;\r
+  FreePool (DirInfo);\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Refresh the global UpdateData structure.\r
+\r
+**/\r
+VOID\r
+RefreshUpdateData (\r
+  VOID\r
+  )\r
+{\r
+  //\r
+  // Free current updated date\r
+  //  \r
+  if (mStartOpCodeHandle != NULL) {\r
+    HiiFreeOpCodeHandle (mStartOpCodeHandle);\r
+  }\r
+\r
+  //\r
+  // Create new OpCode Handle\r
+  //\r
+  mStartOpCodeHandle = HiiAllocateOpCodeHandle ();\r
+\r
+  //\r
+  // Create Hii Extend Label OpCode as the start opcode\r
+  //\r
+  mStartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (\r
+                                         mStartOpCodeHandle,\r
+                                         &gEfiIfrTianoGuid,\r
+                                         NULL,\r
+                                         sizeof (EFI_IFR_GUID_LABEL)\r
+                                         );\r
+  mStartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
+}\r
+\r
+/**\r
+  Update the File Explore page.\r
+\r
+  @param[in] HiiHandle          Hii Handle of the package to be updated.\r
+  @param[in] MenuOption         The Menu whose string tokens need to be updated.\r
+  @param[in] FeCurrentState     Current file explorer state.\r
+\r
+**/\r
+VOID\r
+UpdateFileExplorePage (\r
+  IN EFI_HII_HANDLE               HiiHandle,\r
+  IN SECUREBOOT_MENU_OPTION       *MenuOption,\r
+  IN FILE_EXPLORER_STATE          FeCurrentState\r
+  )\r
+{\r
+  UINTN                   Index;\r
+  SECUREBOOT_MENU_ENTRY   *NewMenuEntry;\r
+  SECUREBOOT_FILE_CONTEXT *NewFileContext;\r
+  EFI_FORM_ID             FormId;\r
+  EFI_FORM_ID             FileFormId;\r
+\r
+  if (FeCurrentState == FileExplorerStateEnrollPkFile) {\r
+    FormId     = SECUREBOOT_ADD_PK_FILE_FORM_ID;\r
+    FileFormId = FORM_FILE_EXPLORER_ID_PK;\r
+  } else if (FeCurrentState == FileExplorerStateEnrollKekFile) {\r
+    FormId     = FORMID_ENROLL_KEK_FORM;\r
+    FileFormId = FORM_FILE_EXPLORER_ID_KEK;\r
+  } else if (FeCurrentState == FileExplorerStateEnrollSignatureFileToDb) {\r
+    FormId     = SECUREBOOT_ENROLL_SIGNATURE_TO_DB;\r
+    FileFormId = FORM_FILE_EXPLORER_ID_DB;\r
+  } else if (FeCurrentState == FileExplorerStateEnrollSignatureFileToDbx) {\r
+    FormId     = SECUREBOOT_ENROLL_SIGNATURE_TO_DBX;\r
+    FileFormId = FORM_FILE_EXPLORER_ID_DBX;\r
+  } else {\r
+    return;\r
+  }\r
+\r
+  NewMenuEntry    = NULL;\r
+  NewFileContext  = NULL;\r
+\r
+  RefreshUpdateData ();\r
+  mStartLabel->Number = FORM_FILE_EXPLORER_ID;\r
+\r
+  for (Index = 0; Index < MenuOption->MenuNumber; Index++) {\r
+    NewMenuEntry    = GetMenuEntry (MenuOption, Index);\r
+    NewFileContext  = (SECUREBOOT_FILE_CONTEXT *) NewMenuEntry->FileContext;\r
+\r
+    if (NewFileContext->IsDir) {\r
+      //\r
+      // Create Text opcode for directory.\r
+      //\r
+      HiiCreateActionOpCode (\r
+        mStartOpCodeHandle,\r
+        (UINT16) (FILE_OPTION_OFFSET + Index),\r
+        NewMenuEntry->DisplayStringToken,\r
+        STRING_TOKEN (STR_NULL),\r
+        EFI_IFR_FLAG_CALLBACK,\r
+        0\r
+        );\r
+    } else {\r
+\r
+      //\r
+      // Create Goto opcode for file.\r
+      //\r
+      HiiCreateGotoOpCode (\r
+        mStartOpCodeHandle,\r
+        FormId,\r
+        NewMenuEntry->DisplayStringToken,\r
+        STRING_TOKEN (STR_NULL),\r
+        EFI_IFR_FLAG_CALLBACK,\r
+        (UINT16) (FILE_OPTION_OFFSET + Index)\r
+        );\r
+    }\r
+  }\r
+\r
+  HiiUpdateForm (\r
+    HiiHandle,\r
+    &gSecureBootConfigFormSetGuid,\r
+    FileFormId,\r
+    mStartOpCodeHandle, // Label FORM_FILE_EXPLORER_ID\r
+    mEndOpCodeHandle    // LABEL_END\r
+    );\r
+}\r
+\r
+/**\r
+  Update the file explorer page with the refreshed file system.\r
+\r
+  @param[in] PrivateData     Module private data.\r
+  @param[in] KeyValue        Key value to identify the type of data to expect.\r
+\r
+  @retval  TRUE           Inform the caller to create a callback packet to exit file explorer.\r
+  @retval  FALSE          Indicate that there is no need to exit file explorer.\r
+\r
+**/\r
+BOOLEAN\r
+UpdateFileExplorer (\r
+  IN SECUREBOOT_CONFIG_PRIVATE_DATA   *PrivateData,\r
+  IN UINT16                           KeyValue\r
+  )\r
+{\r
+  UINT16                              FileOptionMask;\r
+  SECUREBOOT_MENU_ENTRY               *NewMenuEntry;\r
+  SECUREBOOT_FILE_CONTEXT             *NewFileContext;\r
+  EFI_FORM_ID                         FormId;\r
+  BOOLEAN                             ExitFileExplorer;\r
+  EFI_STATUS                          Status;\r
+  EFI_DEVICE_PATH_PROTOCOL            *TmpDevicePath;\r
+\r
+  NewMenuEntry      = NULL;\r
+  NewFileContext    = NULL;\r
+  ExitFileExplorer  = FALSE;\r
+  FileOptionMask    = (UINT16) (FILE_OPTION_MASK & KeyValue);\r
+\r
+  if (PrivateData->FeDisplayContext == FileExplorerDisplayUnknown) {\r
+    //\r
+    // First in, display file system.\r
+    //\r
+    FreeMenu (&FsOptionMenu);\r
+    FindFileSystem ();\r
+    \r
+    CreateMenuStringToken (PrivateData->HiiHandle, &FsOptionMenu);\r
+    UpdateFileExplorePage (PrivateData->HiiHandle, &FsOptionMenu, PrivateData->FeCurrentState);\r
+\r
+    PrivateData->FeDisplayContext = FileExplorerDisplayFileSystem;\r
+  } else {\r
+    if (PrivateData->FeDisplayContext == FileExplorerDisplayFileSystem) {\r
+      NewMenuEntry = GetMenuEntry (&FsOptionMenu, FileOptionMask);\r
+    } else if (PrivateData->FeDisplayContext == FileExplorerDisplayDirectory) {\r
+      NewMenuEntry = GetMenuEntry (&DirectoryMenu, FileOptionMask);\r
+    }\r
+\r
+    NewFileContext = (SECUREBOOT_FILE_CONTEXT *) NewMenuEntry->FileContext;\r
+\r
+    if (NewFileContext->IsDir ) {\r
+      PrivateData->FeDisplayContext = FileExplorerDisplayDirectory;\r
+\r
+      RemoveEntryList (&NewMenuEntry->Link);\r
+      FreeMenu (&DirectoryMenu);\r
+      Status = FindFiles (NewMenuEntry);\r
+       if (EFI_ERROR (Status)) {\r
+         ExitFileExplorer = TRUE;\r
+         goto OnExit;\r
+       }\r
+      CreateMenuStringToken (PrivateData->HiiHandle, &DirectoryMenu);\r
+      DestroyMenuEntry (NewMenuEntry);\r
+\r
+      UpdateFileExplorePage (PrivateData->HiiHandle, &DirectoryMenu, PrivateData->FeCurrentState);\r
+\r
+    } else {\r
+      if (PrivateData->FeCurrentState == FileExplorerStateEnrollPkFile) {\r
+        FormId = SECUREBOOT_ADD_PK_FILE_FORM_ID;\r
+      } else if (PrivateData->FeCurrentState == FileExplorerStateEnrollKekFile) {\r
+        FormId = FORMID_ENROLL_KEK_FORM;\r
+      } else if (PrivateData->FeCurrentState == FileExplorerStateEnrollSignatureFileToDb) {\r
+        FormId = SECUREBOOT_ENROLL_SIGNATURE_TO_DB;\r
+      } else if (PrivateData->FeCurrentState == FileExplorerStateEnrollSignatureFileToDbx) {\r
+        FormId = SECUREBOOT_ENROLL_SIGNATURE_TO_DBX;\r
+      } else {\r
+        return FALSE;\r
+      }\r
+\r
+      PrivateData->MenuEntry = NewMenuEntry;\r
+      PrivateData->FileContext->FileName = NewFileContext->FileName;\r
+      \r
+      TmpDevicePath = NewFileContext->DevicePath;\r
+      OpenFileByDevicePath (\r
+        &TmpDevicePath,\r
+        &PrivateData->FileContext->FHandle,\r
+        EFI_FILE_MODE_READ,\r
+        0\r
+        );\r
+\r
+      //\r
+      // Create Subtitle op-code for the display string of the option.\r
+      //\r
+      RefreshUpdateData ();\r
+      mStartLabel->Number = FormId;\r
+\r
+      HiiCreateSubTitleOpCode (\r
+        mStartOpCodeHandle,\r
+        NewMenuEntry->DisplayStringToken,\r
+        0,\r
+        0,\r
+        0\r
+        );\r
+\r
+      HiiUpdateForm (\r
+        PrivateData->HiiHandle,\r
+        &gSecureBootConfigFormSetGuid,\r
+        FormId,\r
+        mStartOpCodeHandle, // Label FormId\r
+        mEndOpCodeHandle    // LABEL_END\r
+        );\r
+    }\r
+  }\r
+\r
+OnExit:\r
+  return ExitFileExplorer;\r
+}\r
+\r
+/**\r
+  Clean up the dynamic opcode at label and form specified by both LabelId. \r
+\r
+  @param[in] LabelId         It is both the Form ID and Label ID for opcode deletion.\r
+  @param[in] PrivateData     Module private data.\r
+\r
+**/\r
+VOID\r
+CleanUpPage (\r
+  IN UINT16                           LabelId,\r
+  IN SECUREBOOT_CONFIG_PRIVATE_DATA   *PrivateData\r
+  )\r
+{\r
+  RefreshUpdateData ();\r
+\r
+  //\r
+  // Remove all op-codes from dynamic page\r
+  //\r
+  mStartLabel->Number = LabelId;\r
+  HiiUpdateForm (\r
+    PrivateData->HiiHandle,\r
+    &gSecureBootConfigFormSetGuid,\r
+    LabelId,\r
+    mStartOpCodeHandle, // Label LabelId\r
+    mEndOpCodeHandle    // LABEL_END\r
+    );\r
+}\r
+\r