]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
MdeModulePkg/CapsuleApp: Enhance CapsuleApp to support Capsule-on-Disk
[mirror_edk2.git] / MdeModulePkg / Application / CapsuleApp / CapsuleApp.c
index 4d907242f38b55d441fdad8dacc34dc454f83446..896acd3304c90e11b8fdac33c1cd0d5aa51c91df 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   A shell application that triggers capsule update process.\r
 \r
-  Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2016 - 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
@@ -23,6 +23,7 @@
 #include <Library/PrintLib.h>\r
 #include <Library/BmpSupportLib.h>\r
 #include <Protocol/GraphicsOutput.h>\r
+#include <Guid/GlobalVariable.h>\r
 #include <Guid/CapsuleReport.h>\r
 #include <Guid/SystemResourceTable.h>\r
 #include <Guid/FmpCapsule.h>\r
@@ -105,6 +106,46 @@ DumpEsrtData (
   VOID\r
   );\r
 \r
+/**\r
+  Dump Provisioned Capsule.\r
+\r
+  @param[in]  DumpCapsuleInfo  The flag to indicate whether to dump the capsule inforomation.\r
+**/\r
+VOID\r
+DumpProvisionedCapsule (\r
+  IN BOOLEAN                      DumpCapsuleInfo\r
+  );\r
+\r
+/**\r
+  Dump all EFI System Partition.\r
+**/\r
+VOID\r
+DumpAllEfiSysPartition (\r
+  VOID\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
 /**\r
   Read a file.\r
 \r
@@ -799,19 +840,22 @@ PrintUsage (
   )\r
 {\r
   Print(L"CapsuleApp:  usage\n");\r
-  Print(L"  CapsuleApp <Capsule...> [-NR]\n");\r
+  Print(L"  CapsuleApp <Capsule...> [-NR] [-OD [FSx]]\n");\r
   Print(L"  CapsuleApp -S\n");\r
   Print(L"  CapsuleApp -C\n");\r
   Print(L"  CapsuleApp -P\n");\r
   Print(L"  CapsuleApp -E\n");\r
+  Print(L"  CapsuleApp -L\n");\r
+  Print(L"  CapsuleApp -L INFO\n");\r
+  Print(L"  CapsuleApp -F\n");\r
   Print(L"  CapsuleApp -G <BMP> -O <Capsule>\n");\r
   Print(L"  CapsuleApp -N <Capsule> -O <NestedCapsule>\n");\r
   Print(L"  CapsuleApp -D <Capsule>\n");\r
   Print(L"  CapsuleApp -P GET <ImageTypeId> <Index> -O <FileName>\n");\r
   Print(L"Parameter:\n");\r
-  Print(L"  -NR: No reset will be triggered for the capsule with\n");\r
-  Print(L"       CAPSULE_FLAGS_PERSIST_ACROSS_RESET and without\n");\r
-  Print(L"       CAPSULE_FLAGS_INITIATE_RESET.\n");\r
+  Print(L"  -NR: No reset will be triggered for the capsule\n");\r
+  Print(L"       with CAPSULE_FLAGS_PERSIST_ACROSS_RESET and without CAPSULE_FLAGS_INITIATE_RESET.\n");\r
+  Print(L"  -OD: Delivery of Capsules via file on Mass Storage device.");\r
   Print(L"  -S:  Dump capsule report variable (EFI_CAPSULE_REPORT_GUID),\n");\r
   Print(L"       which is defined in UEFI specification.\n");\r
   Print(L"  -C:  Clear capsule report variable (EFI_CAPSULE_REPORT_GUID),\n");\r
@@ -820,6 +864,8 @@ PrintUsage (
   Print(L"       ImageTypeId and Index (decimal format) to a file if 'GET'\n");\r
   Print(L"       option is used.\n");\r
   Print(L"  -E:  Dump UEFI ESRT table info.\n");\r
+  Print(L"  -L:  Dump provisioned capsule image information.\n");\r
+  Print(L"  -F:  Dump all EFI System Partition.\n");\r
   Print(L"  -G:  Convert a BMP file to be an UX capsule,\n");\r
   Print(L"       according to Windows Firmware Update document\n");\r
   Print(L"  -N:  Append a Capsule Header to an existing FMP capsule image\n");\r
@@ -851,7 +897,7 @@ UefiMain (
 {\r
   EFI_STATUS                    Status;\r
   RETURN_STATUS                 RStatus;\r
-  UINTN                         FileSize[MAX_CAPSULE_NUM];\r
+  UINTN                         CapsuleBufferSize[MAX_CAPSULE_NUM];\r
   VOID                          *CapsuleBuffer[MAX_CAPSULE_NUM];\r
   EFI_CAPSULE_BLOCK_DESCRIPTOR  *BlockDescriptors;\r
   EFI_CAPSULE_HEADER            *CapsuleHeaderArray[MAX_CAPSULE_NUM + 1];\r
@@ -859,12 +905,20 @@ UefiMain (
   EFI_RESET_TYPE                ResetType;\r
   BOOLEAN                       NeedReset;\r
   BOOLEAN                       NoReset;\r
+  BOOLEAN                       CapsuleOnDisk;\r
   CHAR16                        *CapsuleName;\r
+  CHAR16                        *CapsuleNames[MAX_CAPSULE_NUM];\r
+  CHAR16                        *MapFsStr;\r
   UINTN                         CapsuleNum;\r
   UINTN                         Index;\r
+  UINTN                         ParaOdIndex;\r
+  UINTN                         ParaNrIndex;\r
   EFI_GUID                      ImageTypeId;\r
   UINTN                         ImageIndex;\r
 \r
+  MapFsStr = NULL;\r
+  CapsuleNum = 0;\r
+\r
   Status = GetArg();\r
   if (EFI_ERROR(Status)) {\r
     Print(L"Please use UEFI SHELL to run this application!\n", Status);\r
@@ -936,6 +990,20 @@ UefiMain (
     return EFI_SUCCESS;\r
   }\r
 \r
+  if (StrCmp(Argv[1], L"-L") == 0) {\r
+    if (Argc >= 3 && StrCmp(Argv[2], L"INFO") == 0) {\r
+      DumpProvisionedCapsule(TRUE);\r
+    } else {\r
+      DumpProvisionedCapsule(FALSE);\r
+    }\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  if (StrCmp(Argv[1], L"-F") == 0) {\r
+    DumpAllEfiSysPartition();\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
   if (Argv[1][0] == L'-') {\r
     Print(L"CapsuleApp: Unrecognized option(%s).\n", Argv[1]);\r
     return EFI_UNSUPPORTED;\r
@@ -943,12 +1011,56 @@ UefiMain (
 \r
   CapsuleFirstIndex = 1;\r
   NoReset = FALSE;\r
-  if ((Argc > 1) && (StrCmp(Argv[Argc - 1], L"-NR") == 0)) {\r
-    NoReset = TRUE;\r
-    CapsuleLastIndex = Argc - 2;\r
+  CapsuleOnDisk = FALSE;\r
+  ParaOdIndex = 0;\r
+  ParaNrIndex = 0;\r
+\r
+  for (Index = 1; Index < Argc; Index++) {\r
+    if (StrCmp(Argv[Index], L"-OD") == 0) {\r
+      ParaOdIndex = Index;\r
+      CapsuleOnDisk = TRUE;\r
+    } else if (StrCmp(Argv[Index], L"-NR") == 0) {\r
+      ParaNrIndex = Index;\r
+      NoReset = TRUE;\r
+    }\r
+  }\r
+\r
+  if (ParaOdIndex != 0) {\r
+    if (ParaOdIndex == Argc - 1) {\r
+      MapFsStr = NULL;\r
+    } else if (ParaOdIndex == Argc - 2) {\r
+      MapFsStr = Argv[Argc-1];\r
+    } else {\r
+      Print (L"CapsuleApp: Invalid Position for -OD Options\n");\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto Done;\r
+    }\r
+\r
+    if (ParaNrIndex != 0) {\r
+      if (ParaNrIndex + 1 == ParaOdIndex) {\r
+        CapsuleLastIndex = ParaNrIndex - 1;\r
+      } else {\r
+        Print (L"CapsuleApp: Invalid Position for -NR Options\n");\r
+        Status = EFI_INVALID_PARAMETER;\r
+        goto Done;\r
+      }\r
+    } else {\r
+      CapsuleLastIndex = ParaOdIndex - 1;\r
+    }\r
   } else {\r
-    CapsuleLastIndex = Argc - 1;\r
+    if (ParaNrIndex != 0) {\r
+      if (ParaNrIndex == Argc -1) {\r
+        CapsuleLastIndex = ParaNrIndex - 1;\r
+      } else {\r
+        Print (L"CapsuleApp: Invalid Position for -NR Options\n");\r
+        Status = EFI_INVALID_PARAMETER;\r
+        goto Done;\r
+      }\r
+    } else {\r
+      CapsuleLastIndex = Argc - 1;\r
+    }\r
   }\r
+\r
   CapsuleNum = CapsuleLastIndex - CapsuleFirstIndex + 1;\r
 \r
   if (CapsuleFirstIndex > CapsuleLastIndex) {\r
@@ -961,26 +1073,27 @@ UefiMain (
   }\r
 \r
   ZeroMem(&CapsuleBuffer, sizeof(CapsuleBuffer));\r
-  ZeroMem(&FileSize, sizeof(FileSize));\r
+  ZeroMem(&CapsuleBufferSize, sizeof(CapsuleBufferSize));\r
   BlockDescriptors = NULL;\r
 \r
   for (Index = 0; Index < CapsuleNum; Index++) {\r
     CapsuleName = Argv[CapsuleFirstIndex + Index];\r
-    Status = ReadFileToBuffer(CapsuleName, &FileSize[Index], &CapsuleBuffer[Index]);\r
+    Status = ReadFileToBuffer(CapsuleName, &CapsuleBufferSize[Index], &CapsuleBuffer[Index]);\r
     if (EFI_ERROR(Status)) {\r
       Print(L"CapsuleApp: capsule image (%s) is not found.\n", CapsuleName);\r
       goto Done;\r
     }\r
-    if (!IsValidCapsuleHeader (CapsuleBuffer[Index], FileSize[Index])) {\r
+    if (!IsValidCapsuleHeader (CapsuleBuffer[Index], CapsuleBufferSize[Index])) {\r
       Print(L"CapsuleApp: Capsule image (%s) is not a valid capsule.\n", CapsuleName);\r
       return EFI_INVALID_PARAMETER;\r
     }\r
+    CapsuleNames[Index] = CapsuleName;\r
   }\r
 \r
   //\r
   // Every capsule use 2 descriptor 1 for data 1 for end\r
   //\r
-  Status = BuildGatherList(CapsuleBuffer, FileSize, CapsuleNum, &BlockDescriptors);\r
+  Status = BuildGatherList(CapsuleBuffer, CapsuleBufferSize, CapsuleNum, &BlockDescriptors);\r
   if (EFI_ERROR(Status)) {\r
     goto Done;\r
   }\r
@@ -1007,13 +1120,30 @@ UefiMain (
   }\r
 \r
   for (Index = 0; Index < CapsuleNum; Index++) {\r
-    if (FileSize[Index] > MaxCapsuleSize) {\r
+    if (CapsuleBufferSize[Index] > MaxCapsuleSize) {\r
       Print (L"CapsuleApp: capsule is too large to update, %ld is allowed\n", MaxCapsuleSize);\r
       Status = EFI_UNSUPPORTED;\r
       goto Done;\r
     }\r
   }\r
 \r
+  //\r
+  // Check whether is capsule on disk.\r
+  //\r
+  if (CapsuleOnDisk) {\r
+    Status = ProcessCapsuleOnDisk (CapsuleBuffer, CapsuleBufferSize, CapsuleNames, MapFsStr, CapsuleNum);\r
+    if (Status != EFI_SUCCESS) {\r
+      Print (L"CapsuleApp: failed to update capsule - %r\n", Status);\r
+      goto Done;\r
+    } else {\r
+      if (!NoReset) {\r
+        gRT->ResetSystem (ResetType, EFI_SUCCESS, 0, NULL);\r
+      } else {\r
+        goto Done;\r
+      }\r
+    }\r
+  }\r
+\r
   //\r
   // Check whether the input capsule image has the flag of persist across system reset.\r
   //\r