]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
MdeModulePkg/CapsuleApp: Add functions to support Capsule-on-Disk
[mirror_edk2.git] / MdeModulePkg / Application / CapsuleApp / CapsuleOnDisk.c
diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c b/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
new file mode 100644 (file)
index 0000000..393b7ae
--- /dev/null
@@ -0,0 +1,808 @@
+/** @file\r
+  Process Capsule On Disk.\r
+\r
+  Copyright (c) 2019, 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
+#include <Uefi.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/UefiRuntimeServicesTableLib.h>\r
+#include <Library/UefiLib.h>\r
+#include <Library/PrintLib.h>\r
+#include <Library/DevicePathLib.h>\r
+#include <Library/FileHandleLib.h>\r
+#include <Library/UefiBootManagerLib.h>\r
+#include <Protocol/SimpleFileSystem.h>\r
+#include <Protocol/Shell.h>\r
+#include <Guid/FileInfo.h>\r
+#include <Guid/GlobalVariable.h>\r
+#include <Guid/Gpt.h>\r
+\r
+EFI_GUID mCapsuleOnDiskBootOptionGuid = { 0x4CC29BB7, 0x2413, 0x40A2, { 0xB0, 0x6D, 0x25, 0x3E, 0x37, 0x10, 0xF5, 0x32 } };\r
+\r
+/**\r
+  Get shell protocol.\r
+\r
+  @return Pointer to shell protocol.\r
+\r
+**/\r
+EFI_SHELL_PROTOCOL *\r
+GetShellProtocol (\r
+  VOID\r
+  );\r
+\r
+/**\r
+  Get file name from file path.\r
+\r
+  @param  FilePath    File path.\r
+\r
+  @return Pointer to file name.\r
+\r
+**/\r
+CHAR16 *\r
+GetFileNameFromPath (\r
+  CHAR16                            *FilePath\r
+  )\r
+{\r
+  EFI_STATUS                        Status;\r
+  EFI_SHELL_PROTOCOL                *ShellProtocol;\r
+  SHELL_FILE_HANDLE                 Handle;\r
+  EFI_FILE_INFO                     *FileInfo;\r
+\r
+  ShellProtocol = GetShellProtocol ();\r
+  if (ShellProtocol == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  //\r
+  // Open file by FileName.\r
+  //\r
+  Status = ShellProtocol->OpenFileByName (\r
+                            FilePath,\r
+                            &Handle,\r
+                            EFI_FILE_MODE_READ\r
+                            );\r
+  if (EFI_ERROR (Status)) {\r
+    return NULL;\r
+  }\r
+\r
+  //\r
+  // Get file name from EFI_FILE_INFO.\r
+  //\r
+  FileInfo = ShellProtocol->GetFileInfo (Handle);\r
+  ShellProtocol->CloseFile (Handle);\r
+  if (FileInfo == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  return FileInfo->FileName;\r
+}\r
+\r
+/**\r
+  Check if the device path is EFI system Partition.\r
+\r
+  @param  DevicePath    The ESP device path.\r
+\r
+  @retval TRUE    DevicePath is a device path for ESP.\r
+  @retval FALSE   DevicePath is not a device path for ESP.\r
+\r
+**/\r
+BOOLEAN\r
+IsEfiSysPartitionDevicePath (\r
+  EFI_DEVICE_PATH_PROTOCOL   *DevicePath\r
+  )\r
+{\r
+  EFI_STATUS                 Status;\r
+  EFI_DEVICE_PATH_PROTOCOL   *TempDevicePath;\r
+  HARDDRIVE_DEVICE_PATH      *Hd;\r
+  EFI_HANDLE                 Handle;\r
+\r
+  //\r
+  // Check if the device path contains GPT node\r
+  //\r
+  TempDevicePath = DevicePath;\r
+\r
+  while (!IsDevicePathEnd (TempDevicePath)) {\r
+    if ((DevicePathType (TempDevicePath) == MEDIA_DEVICE_PATH) &&\r
+      (DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP)) {\r
+      Hd = (HARDDRIVE_DEVICE_PATH *)TempDevicePath;\r
+      if (Hd->MBRType == MBR_TYPE_EFI_PARTITION_TABLE_HEADER) {\r
+        break;\r
+      }\r
+    }\r
+    TempDevicePath = NextDevicePathNode (TempDevicePath);\r
+  }\r
+\r
+  if (!IsDevicePathEnd (TempDevicePath)) {\r
+    //\r
+    // Search for EFI system partition protocol on full device path in Boot Option\r
+    //\r
+    Status = gBS->LocateDevicePath (&gEfiPartTypeSystemPartGuid, &DevicePath, &Handle);\r
+    return EFI_ERROR (Status) ? FALSE : TRUE;\r
+  } else {\r
+    return FALSE;\r
+  }\r
+}\r
+\r
+/**\r
+  Dump all EFI System Partition.\r
+\r
+**/\r
+VOID\r
+DumpAllEfiSysPartition (\r
+  VOID\r
+  )\r
+{\r
+  EFI_HANDLE                 *SimpleFileSystemHandles;\r
+  UINTN                      NumberSimpleFileSystemHandles;\r
+  UINTN                      Index;\r
+  EFI_DEVICE_PATH_PROTOCOL   *DevicePath;\r
+  UINTN                      NumberEfiSystemPartitions;\r
+  EFI_SHELL_PROTOCOL         *ShellProtocol;\r
+\r
+  ShellProtocol = GetShellProtocol ();\r
+  NumberEfiSystemPartitions = 0;\r
+\r
+  Print (L"EFI System Partition list:\n");\r
+\r
+  gBS->LocateHandleBuffer (\r
+         ByProtocol,\r
+         &gEfiSimpleFileSystemProtocolGuid,\r
+         NULL,\r
+         &NumberSimpleFileSystemHandles,\r
+         &SimpleFileSystemHandles\r
+         );\r
+\r
+  for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {\r
+    DevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);\r
+    if (IsEfiSysPartitionDevicePath (DevicePath)) {\r
+      NumberEfiSystemPartitions++;\r
+      Print(L"    %s\n        %s\n", ShellProtocol->GetMapFromDevicePath (&DevicePath), ConvertDevicePathToText (DevicePath, TRUE, TRUE));\r
+    }\r
+  }\r
+\r
+  if (NumberEfiSystemPartitions == 0) {\r
+    Print(L"    No ESP found.\n");\r
+  }\r
+}\r
+\r
+/**\r
+  Check if capsule is provisioned.\r
+\r
+  @retval TRUE    Capsule is provisioned previously.\r
+  @retval FALSE   No capsule is provisioned.\r
+\r
+**/\r
+BOOLEAN\r
+IsCapsuleProvisioned (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS            Status;\r
+  UINT64                OsIndication;\r
+  UINTN                 DataSize;\r
+\r
+  OsIndication = 0;\r
+  DataSize = sizeof(UINT64);\r
+  Status = gRT->GetVariable (\r
+                  L"OsIndications",\r
+                  &gEfiGlobalVariableGuid,\r
+                  NULL,\r
+                  &DataSize,\r
+                  &OsIndication\r
+                  );\r
+  if (!EFI_ERROR (Status) &&\r
+      (OsIndication & EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) != 0) {\r
+    return TRUE;\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
+/**\r
+  Get one active Efi System Partition.\r
+\r
+  @param[out] FsDevicePath   The device path of Fs\r
+  @param[out] Fs             The file system within EfiSysPartition\r
+\r
+  @retval EFI_SUCCESS     Get file system successfully\r
+  @retval EFI_NOT_FOUND   No valid file system found\r
+\r
+**/\r
+EFI_STATUS\r
+GetEfiSysPartition (\r
+  OUT EFI_DEVICE_PATH_PROTOCOL         **FsDevicePath,\r
+  OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  **Fs\r
+  )\r
+{\r
+  EFI_HANDLE                 *SimpleFileSystemHandles;\r
+  UINTN                      NumberSimpleFileSystemHandles;\r
+  UINTN                      Index;\r
+  EFI_DEVICE_PATH_PROTOCOL   *DevicePath;\r
+  EFI_STATUS                 Status;\r
+\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  &gEfiSimpleFileSystemProtocolGuid,\r
+                  NULL,\r
+                  &NumberSimpleFileSystemHandles,\r
+                  &SimpleFileSystemHandles\r
+                  );\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {\r
+    DevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);\r
+    if (IsEfiSysPartitionDevicePath (DevicePath)) {\r
+      Status = gBS->HandleProtocol (SimpleFileSystemHandles[Index], &gEfiSimpleFileSystemProtocolGuid, (VOID **)Fs);\r
+      if (!EFI_ERROR (Status)) {\r
+        *FsDevicePath = DevicePath;\r
+        return EFI_SUCCESS;\r
+      }\r
+    }\r
+  }\r
+\r
+  return EFI_NOT_FOUND;\r
+}\r
+\r
+/**\r
+  Check if Active Efi System Partition within GPT is in the device path.\r
+\r
+  @param[in]  DevicePath     The device path\r
+  @param[out] FsDevicePath   The device path of Fs\r
+  @param[out] Fs             The file system within EfiSysPartition\r
+\r
+  @retval EFI_SUCCESS    Get file system successfully\r
+  @retval EFI_NOT_FOUND  No valid file system found\r
+  @retval others         Get file system failed\r
+\r
+**/\r
+EFI_STATUS\r
+GetEfiSysPartitionFromDevPath (\r
+  IN  EFI_DEVICE_PATH_PROTOCOL        *DevicePath,\r
+  OUT EFI_DEVICE_PATH_PROTOCOL        **FsDevicePath,\r
+  OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL **Fs\r
+  )\r
+{\r
+  EFI_STATUS                 Status;\r
+  EFI_DEVICE_PATH_PROTOCOL   *TempDevicePath;\r
+  HARDDRIVE_DEVICE_PATH      *Hd;\r
+  EFI_HANDLE                 Handle;\r
+\r
+  //\r
+  // Check if the device path contains GPT node\r
+  //\r
+  TempDevicePath = DevicePath;\r
+  while (!IsDevicePathEnd (TempDevicePath)) {\r
+    if ((DevicePathType (TempDevicePath) == MEDIA_DEVICE_PATH) &&\r
+       (DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP)) {\r
+      Hd = (HARDDRIVE_DEVICE_PATH *)TempDevicePath;\r
+      if (Hd->MBRType == MBR_TYPE_EFI_PARTITION_TABLE_HEADER) {\r
+        break;\r
+      }\r
+    }\r
+    TempDevicePath = NextDevicePathNode (TempDevicePath);\r
+  }\r
+\r
+  if (!IsDevicePathEnd (TempDevicePath)) {\r
+    //\r
+    // Search for EFI system partition protocol on full device path in Boot Option\r
+    //\r
+    Status = gBS->LocateDevicePath (&gEfiPartTypeSystemPartGuid, &DevicePath, &Handle);\r
+\r
+    //\r
+    // Search for simple file system on this handler\r
+    //\r
+    if (!EFI_ERROR (Status)) {\r
+      Status = gBS->HandleProtocol (Handle, &gEfiSimpleFileSystemProtocolGuid, (VOID **)Fs);\r
+      if (!EFI_ERROR (Status)) {\r
+        *FsDevicePath = DevicePathFromHandle (Handle);\r
+        return EFI_SUCCESS;\r
+      }\r
+    }\r
+  }\r
+\r
+  return EFI_NOT_FOUND;\r
+}\r
+\r
+/**\r
+  Get SimpleFileSystem from boot option file path.\r
+\r
+  @param[in]  DevicePath     The file path of boot option\r
+  @param[out] FullPath       The full device path of boot device\r
+  @param[out] Fs             The file system within EfiSysPartition\r
+\r
+  @retval EFI_SUCCESS    Get file system successfully\r
+  @retval EFI_NOT_FOUND  No valid file system found\r
+  @retval others         Get file system failed\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetEfiSysPartitionFromBootOptionFilePath (\r
+  IN  EFI_DEVICE_PATH_PROTOCOL         *DevicePath,\r
+  OUT EFI_DEVICE_PATH_PROTOCOL         **FullPath,\r
+  OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  **Fs\r
+  )\r
+{\r
+  EFI_STATUS                        Status;\r
+  EFI_DEVICE_PATH_PROTOCOL          *CurFullPath;\r
+  EFI_DEVICE_PATH_PROTOCOL          *PreFullPath;\r
+  EFI_DEVICE_PATH_PROTOCOL          *FsFullPath;\r
+\r
+  CurFullPath = NULL;\r
+  FsFullPath = NULL;\r
+  //\r
+  // Try every full device Path generated from bootoption\r
+  //\r
+  do {\r
+    PreFullPath = CurFullPath;\r
+    CurFullPath = EfiBootManagerGetNextFullDevicePath (DevicePath, CurFullPath);\r
+\r
+    if (PreFullPath != NULL) {\r
+      FreePool (PreFullPath);\r
+    }\r
+\r
+    if (CurFullPath == NULL) {\r
+      //\r
+      // No Active EFI system partition is found in BootOption device path\r
+      //\r
+      Status = EFI_NOT_FOUND;\r
+      break;\r
+    }\r
+\r
+    DEBUG_CODE (\r
+      CHAR16 *DevicePathStr;\r
+\r
+      DevicePathStr = ConvertDevicePathToText (CurFullPath, TRUE, TRUE);\r
+      if (DevicePathStr != NULL){\r
+        DEBUG ((DEBUG_INFO, "Full device path %s\n", DevicePathStr));\r
+        FreePool (DevicePathStr);\r
+      }\r
+    );\r
+\r
+    Status = GetEfiSysPartitionFromDevPath (CurFullPath, &FsFullPath, Fs);\r
+  } while (EFI_ERROR (Status));\r
+\r
+  if (*Fs != NULL) {\r
+    *FullPath = FsFullPath;\r
+    return EFI_SUCCESS;\r
+  } else {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+}\r
+\r
+/**\r
+  Get a valid SimpleFileSystem within EFI system partition.\r
+\r
+  @param[in]  Map             The FS mapping capsule write to\r
+  @param[out] BootNext        The value of BootNext Variable\r
+  @param[out] Fs              The file system within EfiSysPartition\r
+  @param[out] UpdateBootNext  The flag to indicate whether update BootNext Variable\r
+\r
+  @retval EFI_SUCCESS    Get FS successfully\r
+  @retval EFI_NOT_FOUND  No valid FS found\r
+  @retval others         Get FS failed\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetUpdateFileSystem (\r
+  IN  CHAR16                           *Map,\r
+  OUT UINT16                           *BootNext,\r
+  OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  **Fs,\r
+  OUT BOOLEAN                          *UpdateBootNext\r
+)\r
+{\r
+  EFI_STATUS                      Status;\r
+  CHAR16                          BootOptionName[20];\r
+  UINTN                           Index;\r
+  CONST EFI_DEVICE_PATH_PROTOCOL  *MappedDevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL        *DevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL        *FullPath;\r
+  UINT16                          *BootNextData;\r
+  EFI_BOOT_MANAGER_LOAD_OPTION    BootNextOption;\r
+  EFI_BOOT_MANAGER_LOAD_OPTION    *BootOptionBuffer;\r
+  UINTN                           BootOptionCount;\r
+  EFI_SHELL_PROTOCOL              *ShellProtocol;\r
+  EFI_BOOT_MANAGER_LOAD_OPTION    NewOption;\r
+\r
+  MappedDevicePath = NULL;\r
+  ShellProtocol = GetShellProtocol ();\r
+\r
+  //\r
+  // 1. If Fs is not assigned and there are capsule provisioned before,\r
+  // Get EFI system partition from BootNext.\r
+  //\r
+  if (IsCapsuleProvisioned () && Map == NULL) {\r
+    Status = GetVariable2 (\r
+               L"BootNext",\r
+               &gEfiGlobalVariableGuid,\r
+               (VOID **)&BootNextData,\r
+               NULL\r
+               );\r
+    if (!EFI_ERROR (Status)) {\r
+      UnicodeSPrint (BootOptionName, sizeof (BootOptionName), L"Boot%04x", *BootNextData);\r
+      Status = EfiBootManagerVariableToLoadOption (BootOptionName, &BootNextOption);\r
+      if (!EFI_ERROR (Status)) {\r
+        DevicePath = BootNextOption.FilePath;\r
+        Status = GetEfiSysPartitionFromBootOptionFilePath (DevicePath, &FullPath, Fs);\r
+        if (!EFI_ERROR (Status)) {\r
+          *UpdateBootNext = FALSE;\r
+          Print(L"Get EFI system partition from BootNext : %s\n", BootNextOption.Description);\r
+          Print(L"%s %s\n", ShellProtocol->GetMapFromDevicePath (&FullPath), ConvertDevicePathToText (FullPath, TRUE, TRUE));\r
+          return EFI_SUCCESS;\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  //\r
+  // Check if Map is valid.\r
+  //\r
+  if (Map != NULL) {\r
+    MappedDevicePath = ShellProtocol->GetDevicePathFromMap (Map);\r
+    if (MappedDevicePath == NULL) {\r
+      Print(L"'%s' is not a valid mapping.\n", Map);\r
+      return EFI_INVALID_PARAMETER;\r
+    } else if (!IsEfiSysPartitionDevicePath (DuplicateDevicePath (MappedDevicePath))) {\r
+      Print(L"'%s' is not a EFI System Partition.\n", Map);\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+  }\r
+\r
+  //\r
+  // 2. Get EFI system partition form boot options.\r
+  //\r
+  BootOptionBuffer = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot);\r
+  if (BootOptionCount == 0 && Map == NULL) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  for (Index = 0; Index < BootOptionCount; Index++) {\r
+    //\r
+    // Get the boot option from the link list\r
+    //\r
+    DevicePath  = BootOptionBuffer[Index].FilePath;\r
+\r
+    //\r
+    // Skip inactive or legacy boot options\r
+    //\r
+    if ((BootOptionBuffer[Index].Attributes & LOAD_OPTION_ACTIVE) == 0 ||\r
+        DevicePathType (DevicePath) == BBS_DEVICE_PATH) {\r
+      continue;\r
+    }\r
+\r
+    DEBUG_CODE (\r
+      CHAR16 *DevicePathStr;\r
+\r
+      DevicePathStr = ConvertDevicePathToText (DevicePath, TRUE, TRUE);\r
+      if (DevicePathStr != NULL){\r
+        DEBUG ((DEBUG_INFO, "Try BootOption %s\n", DevicePathStr));\r
+        FreePool (DevicePathStr);\r
+      } else {\r
+        DEBUG ((DEBUG_INFO, "DevicePathToStr failed\n"));\r
+      }\r
+    );\r
+\r
+    Status = GetEfiSysPartitionFromBootOptionFilePath (DevicePath, &FullPath, Fs);\r
+    if (!EFI_ERROR (Status)) {\r
+      if (Map == NULL) {\r
+        *BootNext = (UINT16) BootOptionBuffer[Index].OptionNumber;\r
+        *UpdateBootNext = TRUE;\r
+        Print (L"Found EFI system partition on Boot%04x: %s\n", *BootNext, BootOptionBuffer[Index].Description);\r
+        Print (L"%s %s\n", ShellProtocol->GetMapFromDevicePath (&FullPath), ConvertDevicePathToText (FullPath, TRUE, TRUE));\r
+        return EFI_SUCCESS;\r
+      }\r
+\r
+      if (StrnCmp (Map, ShellProtocol->GetMapFromDevicePath (&FullPath), StrLen (Map)) == 0) {\r
+        *BootNext = (UINT16) BootOptionBuffer[Index].OptionNumber;\r
+        *UpdateBootNext = TRUE;\r
+        Print (L"Found Boot Option on %s : %s\n", Map, BootOptionBuffer[Index].Description);\r
+        return EFI_SUCCESS;\r
+      }\r
+    }\r
+  }\r
+\r
+  //\r
+  // 3. If no ESP is found on boot option, try to find a ESP and create boot option for it.\r
+  //\r
+  if (Map != NULL) {\r
+    //\r
+    // If map is assigned, try to get ESP from mapped Fs.\r
+    //\r
+    DevicePath = DuplicateDevicePath (MappedDevicePath);\r
+    Status = GetEfiSysPartitionFromDevPath (DevicePath, &FullPath, Fs);\r
+    if (EFI_ERROR (Status)) {\r
+      Print (L"Error: Cannot get EFI system partiion from '%s' - %r\n", Map, Status);\r
+      return EFI_NOT_FOUND;\r
+    }\r
+    Print (L"Warning: Cannot find Boot Option on '%s'!\n", Map);\r
+  } else {\r
+    Status = GetEfiSysPartition (&DevicePath, Fs);\r
+    if (EFI_ERROR (Status)) {\r
+      Print (L"Error: Cannot find a EFI system partition!\n");\r
+      return EFI_NOT_FOUND;\r
+    }\r
+  }\r
+\r
+  Print (L"Create Boot option for capsule on disk:\n");\r
+  Status = EfiBootManagerInitializeLoadOption (\r
+             &NewOption,\r
+             LoadOptionNumberUnassigned,\r
+             LoadOptionTypeBoot,\r
+             LOAD_OPTION_ACTIVE,\r
+             L"UEFI Capsule On Disk",\r
+             DevicePath,\r
+             (UINT8 *) &mCapsuleOnDiskBootOptionGuid,\r
+             sizeof(EFI_GUID)\r
+             );\r
+  if (!EFI_ERROR (Status)) {\r
+    Status = EfiBootManagerAddLoadOptionVariable (&NewOption, (UINTN) -1); {\r
+      if (!EFI_ERROR (Status)) {\r
+        *UpdateBootNext = TRUE;\r
+        *BootNext = (UINT16) NewOption.OptionNumber;\r
+        Print (L"  Boot%04x: %s\n", *BootNext, ConvertDevicePathToText(DevicePath, TRUE, TRUE));\r
+        return EFI_SUCCESS;\r
+      }\r
+    }\r
+  }\r
+\r
+  Print (L"ERROR: Cannot create boot option! - %r\n", Status);\r
+\r
+  return EFI_NOT_FOUND;\r
+}\r
+\r
+/**\r
+  Write files to a given SimpleFileSystem.\r
+\r
+  @param[in] Buffer          The buffer array\r
+  @param[in] BufferSize      The buffer size array\r
+  @param[in] FileName        The file name array\r
+  @param[in] BufferNum       The buffer number\r
+  @param[in] Fs              The SimpleFileSystem handle to be written\r
+\r
+  @retval EFI_SUCCESS    Write file successfully\r
+  @retval EFI_NOT_FOUND  SFS protocol not found\r
+  @retval others         Write file failed\r
+\r
+**/\r
+EFI_STATUS\r
+WriteUpdateFile (\r
+  IN  VOID                                 **Buffer,\r
+  IN  UINTN                                *BufferSize,\r
+  IN  CHAR16                               **FileName,\r
+  IN  UINTN                                BufferNum,\r
+  IN  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL      *Fs\r
+)\r
+{\r
+  EFI_STATUS                          Status;\r
+  EFI_FILE                            *Root;\r
+  EFI_FILE                            *FileHandle;\r
+  EFI_FILE_PROTOCOL                   *DirHandle;\r
+  UINT64                              FileInfo;\r
+  VOID                                *Filebuffer;\r
+  UINTN                               FileSize;\r
+  UINTN                               Index;\r
+\r
+  DirHandle   = NULL;\r
+  FileHandle  = NULL;\r
+  Index       = 0;\r
+\r
+  //\r
+  // Open Root from SFS\r
+  //\r
+  Status = Fs->OpenVolume (Fs, &Root);\r
+  if (EFI_ERROR (Status)) {\r
+    Print (L"Cannot open volume. Status = %r\n", Status);\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  //\r
+  // Ensure that efi and updatecapsule directories exist\r
+  //\r
+  Status = Root->Open (Root, &DirHandle, L"\\EFI", EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE, 0);\r
+  if (EFI_ERROR (Status)) {\r
+    Status = Root->Open (Root, &DirHandle, L"\\EFI", EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, EFI_FILE_DIRECTORY);\r
+    if (EFI_ERROR (Status)) {\r
+      Print(L"Unable to create %s directory\n", L"\\EFI");\r
+      return EFI_NOT_FOUND;\r
+    }\r
+  }\r
+  Status = Root->Open (Root, &DirHandle, EFI_CAPSULE_FILE_DIRECTORY, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE , 0);\r
+  if (EFI_ERROR (Status)) {\r
+    Status = Root->Open (Root, &DirHandle, EFI_CAPSULE_FILE_DIRECTORY, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, EFI_FILE_DIRECTORY);\r
+    if (EFI_ERROR (Status)) {\r
+      Print(L"Unable to create %s directory\n", EFI_CAPSULE_FILE_DIRECTORY);\r
+      return EFI_NOT_FOUND;\r
+    }\r
+  }\r
+\r
+  for (Index = 0; Index < BufferNum; Index++) {\r
+    FileHandle = NULL;\r
+\r
+    //\r
+    // Open UpdateCapsule file\r
+    //\r
+    Status = DirHandle->Open (DirHandle, &FileHandle, FileName[Index], EFI_FILE_MODE_CREATE | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ, 0);\r
+    if (EFI_ERROR (Status)) {\r
+      Print (L"Unable to create %s file\n", FileName[Index]);\r
+      return EFI_NOT_FOUND;\r
+    }\r
+\r
+    //\r
+    // Empty the file contents\r
+    //\r
+    Status = FileHandleGetSize (FileHandle, &FileInfo);\r
+    if (EFI_ERROR (Status)) {\r
+      FileHandleClose (FileHandle);\r
+      Print (L"Error Reading %s\n", FileName[Index]);\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
+\r
+    //\r
+    // If the file size is already 0, then it has been empty.\r
+    //\r
+    if (FileInfo != 0) {\r
+      //\r
+      // Set the file size to 0.\r
+      //\r
+      FileInfo = 0;\r
+      Status = FileHandleSetSize (FileHandle, FileInfo);\r
+      if (EFI_ERROR (Status)) {\r
+        Print (L"Error Deleting %s\n", FileName[Index]);\r
+        FileHandleClose (FileHandle);\r
+        return Status;\r
+      }\r
+    }\r
+\r
+    //\r
+    // Write Filebuffer to file\r
+    //\r
+    Filebuffer = Buffer[Index];\r
+    FileSize = BufferSize[Index];\r
+    Status = FileHandleWrite (FileHandle, &FileSize, Filebuffer);\r
+    if (EFI_ERROR (Status)) {\r
+      Print (L"Unable to write Capsule Update to %s, Status = %r\n", FileName[Index], Status);\r
+      return EFI_NOT_FOUND;\r
+    }\r
+\r
+    Print (L"Succeed to write %s\n", FileName[Index]);\r
+    FileHandleClose (FileHandle);\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Set capsule status variable.\r
+\r
+  @param[in] SetCap     Set or clear the capsule flag.\r
+\r
+  @retval EFI_SUCCESS   Succeed to set SetCap variable.\r
+  @retval others        Fail to set the variable.\r
+\r
+**/\r
+EFI_STATUS\r
+SetCapsuleStatusVariable (\r
+  BOOLEAN                       SetCap\r
+  )\r
+{\r
+  EFI_STATUS                    Status;\r
+  UINT64                        OsIndication;\r
+  UINTN                         DataSize;\r
+\r
+  OsIndication = 0;\r
+  DataSize = sizeof(UINT64);\r
+  Status = gRT->GetVariable (\r
+                  L"OsIndications",\r
+                  &gEfiGlobalVariableGuid,\r
+                  NULL,\r
+                  &DataSize,\r
+                  &OsIndication\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    OsIndication = 0;\r
+  }\r
+  if (SetCap) {\r
+    OsIndication |= ((UINT64)EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED);\r
+  }\r
+  else {\r
+    OsIndication &= ~((UINT64)EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED);\r
+  }\r
+  Status = gRT->SetVariable (\r
+                  L"OsIndications",\r
+                  &gEfiGlobalVariableGuid,\r
+                  EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+                  sizeof(UINT64),\r
+                  &OsIndication\r
+                  );\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Process Capsule On Disk.\r
+\r
+  @param[in]  CapsuleBuffer       An array of pointer to capsule images\r
+  @param[in]  CapsuleBufferSize   An array of UINTN to capsule images size\r
+  @param[in]  FilePath            An array of capsule images file path\r
+  @param[in]  Map                 File system mapping string\r
+  @param[in]  CapsuleNum          The count of capsule images\r
+\r
+  @retval EFI_SUCCESS       Capsule on disk success.\r
+  @retval others            Capsule on disk fail.\r
+\r
+**/\r
+EFI_STATUS\r
+ProcessCapsuleOnDisk (\r
+  IN VOID                          **CapsuleBuffer,\r
+  IN UINTN                         *CapsuleBufferSize,\r
+  IN CHAR16                        **FilePath,\r
+  IN CHAR16                        *Map,\r
+  IN UINTN                         CapsuleNum\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+  UINT16                          BootNext;\r
+  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Fs;\r
+  BOOLEAN                         UpdateBootNext;\r
+\r
+  //\r
+  // Get a valid file system from boot path\r
+  //\r
+  Fs = NULL;\r
+\r
+  Status = GetUpdateFileSystem (Map, &BootNext, &Fs, &UpdateBootNext);\r
+  if (EFI_ERROR (Status)) {\r
+    Print (L"CapsuleApp: cannot find a valid file system on boot devies. Status = %r\n", Status);\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Copy capsule image to '\efi\UpdateCapsule\'\r
+  //\r
+  Status = WriteUpdateFile (CapsuleBuffer, CapsuleBufferSize, FilePath, CapsuleNum, Fs);\r
+  if (EFI_ERROR (Status)) {\r
+    Print (L"CapsuleApp: capsule image could not be copied for update.\n");\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Set variable then reset\r
+  //\r
+  Status = SetCapsuleStatusVariable (TRUE);\r
+  if (EFI_ERROR (Status)) {\r
+    Print (L"CapsuleApp: unable to set OSIndication variable.\n");\r
+    return Status;\r
+  }\r
+\r
+  if (UpdateBootNext) {\r
+    Status = gRT->SetVariable (\r
+      L"BootNext",\r
+      &gEfiGlobalVariableGuid,\r
+      EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+      sizeof(UINT16),\r
+      &BootNext\r
+      );\r
+    if (EFI_ERROR (Status)){\r
+      Print (L"CapsuleApp: unable to set BootNext variable.\n");\r
+      return Status;\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r