public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [edk2-devel][Patch] MdeModulePkg/CapsuleApp: Enhance Capsule-On-Disk related functions.
@ 2019-05-24  9:02 Xu, Wei6
  2019-05-28  0:34 ` Zhang, Chao B
  2019-05-28  0:53 ` Wu, Hao A
  0 siblings, 2 replies; 6+ messages in thread
From: Xu, Wei6 @ 2019-05-24  9:02 UTC (permalink / raw)
  To: devel; +Cc: Jian J Wang, Hao A Wu, Chao B Zhang, Wei6 Xu

BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=1840

1. Add missing '\n' in usage.
2. Fix the dead loop of CapsuleApp -L.
3. Fix the bug that CapsuleApp -OD cannot perform capsules in sub-
folder.
4. Optimize the handling for option -NR and -OD to support both
'CapsuleApp <Capsule> -OD -NR' and 'CapsuleApp <Capsule> -NR -OD'.
5. Check if Capsule-On-Disk is supported by "OsIndicationsSupported"
variable firstly before processing capsules. If not supported, prompt
an error message and quit the process.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Chao B Zhang <chao.b.zhang@intel.com>
Signed-off-by: Wei6 Xu <wei6.xu@intel.com>
---
 MdeModulePkg/Application/CapsuleApp/CapsuleApp.c   | 41 ++++++++--------
 MdeModulePkg/Application/CapsuleApp/CapsuleDump.c  |  4 +-
 .../Application/CapsuleApp/CapsuleOnDisk.c         | 56 +++++++++++++++++++++-
 3 files changed, 77 insertions(+), 24 deletions(-)

diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
index e3c591dbf3..0b5f7c8684 100644
--- a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
+++ b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
@@ -847,11 +847,11 @@ PrintUsage (
   Print(L"  CapsuleApp -D <Capsule>\n");
   Print(L"  CapsuleApp -P GET <ImageTypeId> <Index> -O <FileName>\n");
   Print(L"Parameter:\n");
   Print(L"  -NR: No reset will be triggered for the capsule\n");
   Print(L"       with CAPSULE_FLAGS_PERSIST_ACROSS_RESET and without CAPSULE_FLAGS_INITIATE_RESET.\n");
-  Print(L"  -OD: Delivery of Capsules via file on Mass Storage device.");
+  Print(L"  -OD: Delivery of Capsules via file on Mass Storage device.\n");
   Print(L"  -S:  Dump capsule report variable (EFI_CAPSULE_REPORT_GUID),\n");
   Print(L"       which is defined in UEFI specification.\n");
   Print(L"  -C:  Clear capsule report variable (EFI_CAPSULE_REPORT_GUID),\n");
   Print(L"       which is defined in UEFI specification.\n");
   Print(L"  -P:  Dump UEFI FMP protocol info, or get image with specified\n");
@@ -1018,44 +1018,43 @@ UefiMain (
       ParaNrIndex = Index;
       NoReset = TRUE;
     }
   }
 
-  if (ParaOdIndex != 0) {
-    if (ParaOdIndex == Argc - 1) {
+  if (ParaOdIndex > ParaNrIndex) {
+    if (ParaNrIndex != 0) {
+      CapsuleLastIndex = ParaNrIndex - 1;
+    } else {
+      CapsuleLastIndex = ParaOdIndex - 1;
+    }
+
+    if (ParaOdIndex == Argc -1) {
       MapFsStr = NULL;
     } else if (ParaOdIndex == Argc - 2) {
       MapFsStr = Argv[Argc-1];
     } else {
-      Print (L"CapsuleApp: Invalid Position for -OD Options\n");
+      Print (L"CapsuleApp: Cannot specify more than one FS mapping!\n");
       Status = EFI_INVALID_PARAMETER;
       goto Done;
     }
-
-    if (ParaNrIndex != 0) {
-      if (ParaNrIndex + 1 == ParaOdIndex) {
-        CapsuleLastIndex = ParaNrIndex - 1;
-      } else {
-        Print (L"CapsuleApp: Invalid Position for -NR Options\n");
-        Status = EFI_INVALID_PARAMETER;
-        goto Done;
-      }
-    } else {
+  } else if (ParaOdIndex < ParaNrIndex) {
+    if (ParaOdIndex != 0) {
       CapsuleLastIndex = ParaOdIndex - 1;
-    }
-  } else {
-    if (ParaNrIndex != 0) {
-      if (ParaNrIndex == Argc -1) {
-        CapsuleLastIndex = ParaNrIndex - 1;
+      if (ParaOdIndex == ParaNrIndex - 1) {
+        MapFsStr = NULL;
+      } else if (ParaOdIndex == ParaNrIndex - 2) {
+        MapFsStr = Argv[ParaOdIndex + 1];
       } else {
-        Print (L"CapsuleApp: Invalid Position for -NR Options\n");
+        Print (L"CapsuleApp: Cannot specify more than one FS mapping!\n");
         Status = EFI_INVALID_PARAMETER;
         goto Done;
       }
     } else {
-      CapsuleLastIndex = Argc - 1;
+      CapsuleLastIndex = ParaNrIndex - 1;
     }
+  } else {
+    CapsuleLastIndex = Argc - 1;
   }
 
   CapsuleNum = CapsuleLastIndex - CapsuleFirstIndex + 1;
 
   if (CapsuleFirstIndex > CapsuleLastIndex) {
diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
index b81c5b7b3a..0e0c566702 100644
--- a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
+++ b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
@@ -810,12 +810,12 @@ DumpCapsuleFromDisk (
   }
 
   //
   // Get file count first
   //
+  Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
   do {
-    Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
     if (EFI_ERROR (Status) || FileInfo == NULL) {
       Print (L"Get File Info Fail. Status = %r\n", Status);
       goto Done;
     }
 
@@ -844,12 +844,12 @@ DumpCapsuleFromDisk (
   NoFile = FALSE;
 
   //
   // Get all file info
   //
+  Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
   do {
-    Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
     if (EFI_ERROR (Status) || FileInfo == NULL) {
       Print (L"Get File Info Fail. Status = %r\n", Status);
       goto Done;
     }
 
diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c b/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
index a11683d66c..df43a436f2 100644
--- a/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
+++ b/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
@@ -21,10 +21,12 @@
 #include <Protocol/Shell.h>
 #include <Guid/FileInfo.h>
 #include <Guid/GlobalVariable.h>
 #include <Guid/Gpt.h>
 
+#define MAX_CAPSULE_NUM 10
+
 EFI_GUID mCapsuleOnDiskBootOptionGuid = { 0x4CC29BB7, 0x2413, 0x40A2, { 0xB0, 0x6D, 0x25, 0x3E, 0x37, 0x10, 0xF5, 0x32 } };
 
 /**
   Get shell protocol.
 
@@ -742,10 +744,45 @@ SetCapsuleStatusVariable (
                   );
 
   return Status;
 }
 
+/**
+  Check if Capsule On Disk is supported.
+
+  @retval TRUE              Capsule On Disk is supported.
+  @retval FALSE             Capsule On Disk is not supported.
+
+**/
+BOOLEAN
+IsCapsuleOnDiskSupported (
+  VOID
+  )
+{
+  EFI_STATUS                    Status;
+  UINT64                        OsIndicationsSupported;
+  UINTN                         DataSize;
+
+  DataSize = sizeof(UINT64);
+  Status = gRT->GetVariable (
+                  L"OsIndicationsSupported",
+                  &gEfiGlobalVariableGuid,
+                  NULL,
+                  &DataSize,
+                  &OsIndicationsSupported
+                  );
+  if (EFI_ERROR (Status)) {
+    return FALSE;
+  }
+
+  if (OsIndicationsSupported & EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) {
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
 /**
   Process Capsule On Disk.
 
   @param[in]  CapsuleBuffer       An array of pointer to capsule images
   @param[in]  CapsuleBufferSize   An array of UINTN to capsule images size
@@ -768,10 +805,20 @@ ProcessCapsuleOnDisk (
 {
   EFI_STATUS                      Status;
   UINT16                          BootNext;
   EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Fs;
   BOOLEAN                         UpdateBootNext;
+  CHAR16                          *FileName[MAX_CAPSULE_NUM];
+  UINTN                           Index;
+
+  //
+  // Check if Capsule On Disk is supported
+  //
+  if (!IsCapsuleOnDiskSupported ()) {
+    Print (L"CapsuleApp: Capsule On Disk is not supported.\n");
+    return EFI_UNSUPPORTED;
+  }
 
   //
   // Get a valid file system from boot path
   //
   Fs = NULL;
@@ -780,14 +827,21 @@ ProcessCapsuleOnDisk (
   if (EFI_ERROR (Status)) {
     Print (L"CapsuleApp: cannot find a valid file system on boot devies. Status = %r\n", Status);
     return Status;
   }
 
+  //
+  // Get file name from file path
+  //
+  for (Index = 0; Index < CapsuleNum; Index ++) {
+    FileName[Index] = GetFileNameFromPath (FilePath[Index]);
+  }
+
   //
   // Copy capsule image to '\efi\UpdateCapsule\'
   //
-  Status = WriteUpdateFile (CapsuleBuffer, CapsuleBufferSize, FilePath, CapsuleNum, Fs);
+  Status = WriteUpdateFile (CapsuleBuffer, CapsuleBufferSize, FileName, CapsuleNum, Fs);
   if (EFI_ERROR (Status)) {
     Print (L"CapsuleApp: capsule image could not be copied for update.\n");
     return Status;
   }
 
-- 
2.16.2.windows.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [edk2-devel][Patch] MdeModulePkg/CapsuleApp: Enhance Capsule-On-Disk related functions.
  2019-05-24  9:02 Xu, Wei6
@ 2019-05-28  0:34 ` Zhang, Chao B
  2019-05-28  0:53 ` Wu, Hao A
  1 sibling, 0 replies; 6+ messages in thread
From: Zhang, Chao B @ 2019-05-28  0:34 UTC (permalink / raw)
  To: devel@edk2.groups.io, Xu, Wei6; +Cc: Wang, Jian J, Wu, Hao A

Reviewed-by: Chao Zhang <chao.b.zhang@intel.com>

-----Original Message-----
From: devel@edk2.groups.io [mailto:devel@edk2.groups.io] On Behalf Of Xu, Wei6
Sent: Friday, May 24, 2019 5:02 PM
To: devel@edk2.groups.io
Cc: Wang, Jian J <jian.j.wang@intel.com>; Wu, Hao A <hao.a.wu@intel.com>; Zhang, Chao B <chao.b.zhang@intel.com>; Xu, Wei6 <wei6.xu@intel.com>
Subject: [edk2-devel][Patch] MdeModulePkg/CapsuleApp: Enhance Capsule-On-Disk related functions.

BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=1840

1. Add missing '\n' in usage.
2. Fix the dead loop of CapsuleApp -L.
3. Fix the bug that CapsuleApp -OD cannot perform capsules in sub- folder.
4. Optimize the handling for option -NR and -OD to support both 'CapsuleApp <Capsule> -OD -NR' and 'CapsuleApp <Capsule> -NR -OD'.
5. Check if Capsule-On-Disk is supported by "OsIndicationsSupported"
variable firstly before processing capsules. If not supported, prompt an error message and quit the process.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Chao B Zhang <chao.b.zhang@intel.com>
Signed-off-by: Wei6 Xu <wei6.xu@intel.com>
---
 MdeModulePkg/Application/CapsuleApp/CapsuleApp.c   | 41 ++++++++--------
 MdeModulePkg/Application/CapsuleApp/CapsuleDump.c  |  4 +-
 .../Application/CapsuleApp/CapsuleOnDisk.c         | 56 +++++++++++++++++++++-
 3 files changed, 77 insertions(+), 24 deletions(-)

diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
index e3c591dbf3..0b5f7c8684 100644
--- a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
+++ b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
@@ -847,11 +847,11 @@ PrintUsage (
   Print(L"  CapsuleApp -D <Capsule>\n");
   Print(L"  CapsuleApp -P GET <ImageTypeId> <Index> -O <FileName>\n");
   Print(L"Parameter:\n");
   Print(L"  -NR: No reset will be triggered for the capsule\n");
   Print(L"       with CAPSULE_FLAGS_PERSIST_ACROSS_RESET and without CAPSULE_FLAGS_INITIATE_RESET.\n");
-  Print(L"  -OD: Delivery of Capsules via file on Mass Storage device.");
+  Print(L"  -OD: Delivery of Capsules via file on Mass Storage 
+ device.\n");
   Print(L"  -S:  Dump capsule report variable (EFI_CAPSULE_REPORT_GUID),\n");
   Print(L"       which is defined in UEFI specification.\n");
   Print(L"  -C:  Clear capsule report variable (EFI_CAPSULE_REPORT_GUID),\n");
   Print(L"       which is defined in UEFI specification.\n");
   Print(L"  -P:  Dump UEFI FMP protocol info, or get image with specified\n"); @@ -1018,44 +1018,43 @@ UefiMain (
       ParaNrIndex = Index;
       NoReset = TRUE;
     }
   }
 
-  if (ParaOdIndex != 0) {
-    if (ParaOdIndex == Argc - 1) {
+  if (ParaOdIndex > ParaNrIndex) {
+    if (ParaNrIndex != 0) {
+      CapsuleLastIndex = ParaNrIndex - 1;
+    } else {
+      CapsuleLastIndex = ParaOdIndex - 1;
+    }
+
+    if (ParaOdIndex == Argc -1) {
       MapFsStr = NULL;
     } else if (ParaOdIndex == Argc - 2) {
       MapFsStr = Argv[Argc-1];
     } else {
-      Print (L"CapsuleApp: Invalid Position for -OD Options\n");
+      Print (L"CapsuleApp: Cannot specify more than one FS 
+ mapping!\n");
       Status = EFI_INVALID_PARAMETER;
       goto Done;
     }
-
-    if (ParaNrIndex != 0) {
-      if (ParaNrIndex + 1 == ParaOdIndex) {
-        CapsuleLastIndex = ParaNrIndex - 1;
-      } else {
-        Print (L"CapsuleApp: Invalid Position for -NR Options\n");
-        Status = EFI_INVALID_PARAMETER;
-        goto Done;
-      }
-    } else {
+  } else if (ParaOdIndex < ParaNrIndex) {
+    if (ParaOdIndex != 0) {
       CapsuleLastIndex = ParaOdIndex - 1;
-    }
-  } else {
-    if (ParaNrIndex != 0) {
-      if (ParaNrIndex == Argc -1) {
-        CapsuleLastIndex = ParaNrIndex - 1;
+      if (ParaOdIndex == ParaNrIndex - 1) {
+        MapFsStr = NULL;
+      } else if (ParaOdIndex == ParaNrIndex - 2) {
+        MapFsStr = Argv[ParaOdIndex + 1];
       } else {
-        Print (L"CapsuleApp: Invalid Position for -NR Options\n");
+        Print (L"CapsuleApp: Cannot specify more than one FS 
+ mapping!\n");
         Status = EFI_INVALID_PARAMETER;
         goto Done;
       }
     } else {
-      CapsuleLastIndex = Argc - 1;
+      CapsuleLastIndex = ParaNrIndex - 1;
     }
+  } else {
+    CapsuleLastIndex = Argc - 1;
   }
 
   CapsuleNum = CapsuleLastIndex - CapsuleFirstIndex + 1;
 
   if (CapsuleFirstIndex > CapsuleLastIndex) { diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
index b81c5b7b3a..0e0c566702 100644
--- a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
+++ b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
@@ -810,12 +810,12 @@ DumpCapsuleFromDisk (
   }
 
   //
   // Get file count first
   //
+  Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
   do {
-    Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
     if (EFI_ERROR (Status) || FileInfo == NULL) {
       Print (L"Get File Info Fail. Status = %r\n", Status);
       goto Done;
     }
 
@@ -844,12 +844,12 @@ DumpCapsuleFromDisk (
   NoFile = FALSE;
 
   //
   // Get all file info
   //
+  Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
   do {
-    Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
     if (EFI_ERROR (Status) || FileInfo == NULL) {
       Print (L"Get File Info Fail. Status = %r\n", Status);
       goto Done;
     }
 
diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c b/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
index a11683d66c..df43a436f2 100644
--- a/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
+++ b/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
@@ -21,10 +21,12 @@
 #include <Protocol/Shell.h>
 #include <Guid/FileInfo.h>
 #include <Guid/GlobalVariable.h>
 #include <Guid/Gpt.h>
 
+#define MAX_CAPSULE_NUM 10
+
 EFI_GUID mCapsuleOnDiskBootOptionGuid = { 0x4CC29BB7, 0x2413, 0x40A2, { 0xB0, 0x6D, 0x25, 0x3E, 0x37, 0x10, 0xF5, 0x32 } };
 
 /**
   Get shell protocol.
 
@@ -742,10 +744,45 @@ SetCapsuleStatusVariable (
                   );
 
   return Status;
 }
 
+/**
+  Check if Capsule On Disk is supported.
+
+  @retval TRUE              Capsule On Disk is supported.
+  @retval FALSE             Capsule On Disk is not supported.
+
+**/
+BOOLEAN
+IsCapsuleOnDiskSupported (
+  VOID
+  )
+{
+  EFI_STATUS                    Status;
+  UINT64                        OsIndicationsSupported;
+  UINTN                         DataSize;
+
+  DataSize = sizeof(UINT64);
+  Status = gRT->GetVariable (
+                  L"OsIndicationsSupported",
+                  &gEfiGlobalVariableGuid,
+                  NULL,
+                  &DataSize,
+                  &OsIndicationsSupported
+                  );
+  if (EFI_ERROR (Status)) {
+    return FALSE;
+  }
+
+  if (OsIndicationsSupported & EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) {
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
 /**
   Process Capsule On Disk.
 
   @param[in]  CapsuleBuffer       An array of pointer to capsule images
   @param[in]  CapsuleBufferSize   An array of UINTN to capsule images size
@@ -768,10 +805,20 @@ ProcessCapsuleOnDisk (  {
   EFI_STATUS                      Status;
   UINT16                          BootNext;
   EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Fs;
   BOOLEAN                         UpdateBootNext;
+  CHAR16                          *FileName[MAX_CAPSULE_NUM];
+  UINTN                           Index;
+
+  //
+  // Check if Capsule On Disk is supported  //  if 
+ (!IsCapsuleOnDiskSupported ()) {
+    Print (L"CapsuleApp: Capsule On Disk is not supported.\n");
+    return EFI_UNSUPPORTED;
+  }
 
   //
   // Get a valid file system from boot path
   //
   Fs = NULL;
@@ -780,14 +827,21 @@ ProcessCapsuleOnDisk (
   if (EFI_ERROR (Status)) {
     Print (L"CapsuleApp: cannot find a valid file system on boot devies. Status = %r\n", Status);
     return Status;
   }
 
+  //
+  // Get file name from file path
+  //
+  for (Index = 0; Index < CapsuleNum; Index ++) {
+    FileName[Index] = GetFileNameFromPath (FilePath[Index]);  }
+
   //
   // Copy capsule image to '\efi\UpdateCapsule\'
   //
-  Status = WriteUpdateFile (CapsuleBuffer, CapsuleBufferSize, FilePath, CapsuleNum, Fs);
+  Status = WriteUpdateFile (CapsuleBuffer, CapsuleBufferSize, FileName, 
+ CapsuleNum, Fs);
   if (EFI_ERROR (Status)) {
     Print (L"CapsuleApp: capsule image could not be copied for update.\n");
     return Status;
   }
 
--
2.16.2.windows.1





^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [edk2-devel][Patch] MdeModulePkg/CapsuleApp: Enhance Capsule-On-Disk related functions.
  2019-05-24  9:02 Xu, Wei6
  2019-05-28  0:34 ` Zhang, Chao B
@ 2019-05-28  0:53 ` Wu, Hao A
  1 sibling, 0 replies; 6+ messages in thread
From: Wu, Hao A @ 2019-05-28  0:53 UTC (permalink / raw)
  To: Xu, Wei6, devel@edk2.groups.io; +Cc: Wang, Jian J, Zhang, Chao B

> -----Original Message-----
> From: Xu, Wei6
> Sent: Friday, May 24, 2019 5:02 PM
> To: devel@edk2.groups.io
> Cc: Wang, Jian J; Wu, Hao A; Zhang, Chao B; Xu, Wei6
> Subject: [edk2-devel][Patch] MdeModulePkg/CapsuleApp: Enhance
> Capsule-On-Disk related functions.
> 
> BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=1840
> 
> 1. Add missing '\n' in usage.
> 2. Fix the dead loop of CapsuleApp -L.
> 3. Fix the bug that CapsuleApp -OD cannot perform capsules in sub-
> folder.
> 4. Optimize the handling for option -NR and -OD to support both
> 'CapsuleApp <Capsule> -OD -NR' and 'CapsuleApp <Capsule> -NR -OD'.
> 5. Check if Capsule-On-Disk is supported by "OsIndicationsSupported"
> variable firstly before processing capsules. If not supported, prompt
> an error message and quit the process.

I think this one belongs to a bug fix.
Acked-by: Hao A Wu <hao.a.wu@intel.com>

Best Regards,
Hao Wu

> 
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Chao B Zhang <chao.b.zhang@intel.com>
> Signed-off-by: Wei6 Xu <wei6.xu@intel.com>
> ---
>  MdeModulePkg/Application/CapsuleApp/CapsuleApp.c   | 41 ++++++++-----
> ---
>  MdeModulePkg/Application/CapsuleApp/CapsuleDump.c  |  4 +-
>  .../Application/CapsuleApp/CapsuleOnDisk.c         | 56
> +++++++++++++++++++++-
>  3 files changed, 77 insertions(+), 24 deletions(-)
> 
> diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
> b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
> index e3c591dbf3..0b5f7c8684 100644
> --- a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
> +++ b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
> @@ -847,11 +847,11 @@ PrintUsage (
>    Print(L"  CapsuleApp -D <Capsule>\n");
>    Print(L"  CapsuleApp -P GET <ImageTypeId> <Index> -O <FileName>\n");
>    Print(L"Parameter:\n");
>    Print(L"  -NR: No reset will be triggered for the capsule\n");
>    Print(L"       with CAPSULE_FLAGS_PERSIST_ACROSS_RESET and without
> CAPSULE_FLAGS_INITIATE_RESET.\n");
> -  Print(L"  -OD: Delivery of Capsules via file on Mass Storage device.");
> +  Print(L"  -OD: Delivery of Capsules via file on Mass Storage device.\n");
>    Print(L"  -S:  Dump capsule report variable
> (EFI_CAPSULE_REPORT_GUID),\n");
>    Print(L"       which is defined in UEFI specification.\n");
>    Print(L"  -C:  Clear capsule report variable
> (EFI_CAPSULE_REPORT_GUID),\n");
>    Print(L"       which is defined in UEFI specification.\n");
>    Print(L"  -P:  Dump UEFI FMP protocol info, or get image with specified\n");
> @@ -1018,44 +1018,43 @@ UefiMain (
>        ParaNrIndex = Index;
>        NoReset = TRUE;
>      }
>    }
> 
> -  if (ParaOdIndex != 0) {
> -    if (ParaOdIndex == Argc - 1) {
> +  if (ParaOdIndex > ParaNrIndex) {
> +    if (ParaNrIndex != 0) {
> +      CapsuleLastIndex = ParaNrIndex - 1;
> +    } else {
> +      CapsuleLastIndex = ParaOdIndex - 1;
> +    }
> +
> +    if (ParaOdIndex == Argc -1) {
>        MapFsStr = NULL;
>      } else if (ParaOdIndex == Argc - 2) {
>        MapFsStr = Argv[Argc-1];
>      } else {
> -      Print (L"CapsuleApp: Invalid Position for -OD Options\n");
> +      Print (L"CapsuleApp: Cannot specify more than one FS mapping!\n");
>        Status = EFI_INVALID_PARAMETER;
>        goto Done;
>      }
> -
> -    if (ParaNrIndex != 0) {
> -      if (ParaNrIndex + 1 == ParaOdIndex) {
> -        CapsuleLastIndex = ParaNrIndex - 1;
> -      } else {
> -        Print (L"CapsuleApp: Invalid Position for -NR Options\n");
> -        Status = EFI_INVALID_PARAMETER;
> -        goto Done;
> -      }
> -    } else {
> +  } else if (ParaOdIndex < ParaNrIndex) {
> +    if (ParaOdIndex != 0) {
>        CapsuleLastIndex = ParaOdIndex - 1;
> -    }
> -  } else {
> -    if (ParaNrIndex != 0) {
> -      if (ParaNrIndex == Argc -1) {
> -        CapsuleLastIndex = ParaNrIndex - 1;
> +      if (ParaOdIndex == ParaNrIndex - 1) {
> +        MapFsStr = NULL;
> +      } else if (ParaOdIndex == ParaNrIndex - 2) {
> +        MapFsStr = Argv[ParaOdIndex + 1];
>        } else {
> -        Print (L"CapsuleApp: Invalid Position for -NR Options\n");
> +        Print (L"CapsuleApp: Cannot specify more than one FS mapping!\n");
>          Status = EFI_INVALID_PARAMETER;
>          goto Done;
>        }
>      } else {
> -      CapsuleLastIndex = Argc - 1;
> +      CapsuleLastIndex = ParaNrIndex - 1;
>      }
> +  } else {
> +    CapsuleLastIndex = Argc - 1;
>    }
> 
>    CapsuleNum = CapsuleLastIndex - CapsuleFirstIndex + 1;
> 
>    if (CapsuleFirstIndex > CapsuleLastIndex) {
> diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
> b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
> index b81c5b7b3a..0e0c566702 100644
> --- a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
> +++ b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
> @@ -810,12 +810,12 @@ DumpCapsuleFromDisk (
>    }
> 
>    //
>    // Get file count first
>    //
> +  Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
>    do {
> -    Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
>      if (EFI_ERROR (Status) || FileInfo == NULL) {
>        Print (L"Get File Info Fail. Status = %r\n", Status);
>        goto Done;
>      }
> 
> @@ -844,12 +844,12 @@ DumpCapsuleFromDisk (
>    NoFile = FALSE;
> 
>    //
>    // Get all file info
>    //
> +  Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
>    do {
> -    Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
>      if (EFI_ERROR (Status) || FileInfo == NULL) {
>        Print (L"Get File Info Fail. Status = %r\n", Status);
>        goto Done;
>      }
> 
> diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
> b/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
> index a11683d66c..df43a436f2 100644
> --- a/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
> +++ b/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
> @@ -21,10 +21,12 @@
>  #include <Protocol/Shell.h>
>  #include <Guid/FileInfo.h>
>  #include <Guid/GlobalVariable.h>
>  #include <Guid/Gpt.h>
> 
> +#define MAX_CAPSULE_NUM 10
> +
>  EFI_GUID mCapsuleOnDiskBootOptionGuid = { 0x4CC29BB7, 0x2413, 0x40A2,
> { 0xB0, 0x6D, 0x25, 0x3E, 0x37, 0x10, 0xF5, 0x32 } };
> 
>  /**
>    Get shell protocol.
> 
> @@ -742,10 +744,45 @@ SetCapsuleStatusVariable (
>                    );
> 
>    return Status;
>  }
> 
> +/**
> +  Check if Capsule On Disk is supported.
> +
> +  @retval TRUE              Capsule On Disk is supported.
> +  @retval FALSE             Capsule On Disk is not supported.
> +
> +**/
> +BOOLEAN
> +IsCapsuleOnDiskSupported (
> +  VOID
> +  )
> +{
> +  EFI_STATUS                    Status;
> +  UINT64                        OsIndicationsSupported;
> +  UINTN                         DataSize;
> +
> +  DataSize = sizeof(UINT64);
> +  Status = gRT->GetVariable (
> +                  L"OsIndicationsSupported",
> +                  &gEfiGlobalVariableGuid,
> +                  NULL,
> +                  &DataSize,
> +                  &OsIndicationsSupported
> +                  );
> +  if (EFI_ERROR (Status)) {
> +    return FALSE;
> +  }
> +
> +  if (OsIndicationsSupported &
> EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) {
> +    return TRUE;
> +  }
> +
> +  return FALSE;
> +}
> +
>  /**
>    Process Capsule On Disk.
> 
>    @param[in]  CapsuleBuffer       An array of pointer to capsule images
>    @param[in]  CapsuleBufferSize   An array of UINTN to capsule images size
> @@ -768,10 +805,20 @@ ProcessCapsuleOnDisk (
>  {
>    EFI_STATUS                      Status;
>    UINT16                          BootNext;
>    EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Fs;
>    BOOLEAN                         UpdateBootNext;
> +  CHAR16                          *FileName[MAX_CAPSULE_NUM];
> +  UINTN                           Index;
> +
> +  //
> +  // Check if Capsule On Disk is supported
> +  //
> +  if (!IsCapsuleOnDiskSupported ()) {
> +    Print (L"CapsuleApp: Capsule On Disk is not supported.\n");
> +    return EFI_UNSUPPORTED;
> +  }
> 
>    //
>    // Get a valid file system from boot path
>    //
>    Fs = NULL;
> @@ -780,14 +827,21 @@ ProcessCapsuleOnDisk (
>    if (EFI_ERROR (Status)) {
>      Print (L"CapsuleApp: cannot find a valid file system on boot devies. Status
> = %r\n", Status);
>      return Status;
>    }
> 
> +  //
> +  // Get file name from file path
> +  //
> +  for (Index = 0; Index < CapsuleNum; Index ++) {
> +    FileName[Index] = GetFileNameFromPath (FilePath[Index]);
> +  }
> +
>    //
>    // Copy capsule image to '\efi\UpdateCapsule\'
>    //
> -  Status = WriteUpdateFile (CapsuleBuffer, CapsuleBufferSize, FilePath,
> CapsuleNum, Fs);
> +  Status = WriteUpdateFile (CapsuleBuffer, CapsuleBufferSize, FileName,
> CapsuleNum, Fs);
>    if (EFI_ERROR (Status)) {
>      Print (L"CapsuleApp: capsule image could not be copied for update.\n");
>      return Status;
>    }
> 
> --
> 2.16.2.windows.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [edk2-devel][Patch] MdeModulePkg/CapsuleApp: Enhance Capsule-On-Disk related functions.
@ 2019-06-25  6:51 Xu, Wei6
  2019-06-26  1:45 ` Wu, Hao A
  2019-06-26  2:06 ` Zhang, Chao B
  0 siblings, 2 replies; 6+ messages in thread
From: Xu, Wei6 @ 2019-06-25  6:51 UTC (permalink / raw)
  To: devel; +Cc: Jian J Wang, Hao A Wu, Chao B Zhang

BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=1840

1. Introduce an internal header file to put definitions in it.
2. Add missing '\n' in usage.
3. Fix the dead loop of CapsuleApp -L.
4. Fix the bug that CapsuleApp -OD cannot perform capsules in sub-
folder.
5. Optimize the handling for option -NR and -OD to support both
'CapsuleApp <Capsule> -OD -NR' and 'CapsuleApp <Capsule> -NR -OD'.
6. Check if Capsule-On-Disk is supported by "OsIndicationsSupported"
variable firstly before processing capsules. If not supported, prompt
an error message and quit the process.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Chao B Zhang <chao.b.zhang@intel.com>
Signed-off-by: Wei6 Xu <wei6.xu@intel.com>
---
 MdeModulePkg/Application/CapsuleApp/AppSupport.c   |  13 +-
 MdeModulePkg/Application/CapsuleApp/CapsuleApp.c   | 215 ++----------------
 MdeModulePkg/Application/CapsuleApp/CapsuleApp.h   | 240 +++++++++++++++++++++
 MdeModulePkg/Application/CapsuleApp/CapsuleApp.inf |   3 +-
 MdeModulePkg/Application/CapsuleApp/CapsuleDump.c  |  98 +--------
 .../Application/CapsuleApp/CapsuleOnDisk.c         |  86 +++++---
 6 files changed, 323 insertions(+), 332 deletions(-)
 create mode 100644 MdeModulePkg/Application/CapsuleApp/CapsuleApp.h

diff --git a/MdeModulePkg/Application/CapsuleApp/AppSupport.c b/MdeModulePkg/Application/CapsuleApp/AppSupport.c
index d9ce1b4843..8fe70dc3b6 100644
--- a/MdeModulePkg/Application/CapsuleApp/AppSupport.c
+++ b/MdeModulePkg/Application/CapsuleApp/AppSupport.c
@@ -1,23 +1,14 @@
 /** @file
   A shell application that triggers capsule update process.
 
-  Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
-#include <Uefi.h>
-#include <Library/BaseLib.h>
-#include <Library/DebugLib.h>
-#include <Library/BaseMemoryLib.h>
-#include <Library/MemoryAllocationLib.h>
-#include <Library/UefiBootServicesTableLib.h>
-#include <Protocol/SimpleFileSystem.h>
-#include <Protocol/ShellParameters.h>
-#include <Protocol/Shell.h>
-#include <Guid/FileInfo.h>
+#include "CapsuleApp.h"
 
 UINTN  Argc;
 CHAR16 **Argv;
 EFI_SHELL_PROTOCOL      *mShellProtocol = NULL;
 
diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
index e3c591dbf3..3439ce5feb 100644
--- a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
+++ b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
@@ -4,191 +4,19 @@
   Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
-#include <Uefi.h>
-#include <Library/BaseLib.h>
-#include <Library/DebugLib.h>
-#include <Library/BaseMemoryLib.h>
-#include <Library/MemoryAllocationLib.h>
-#include <Library/UefiBootServicesTableLib.h>
-#include <Library/UefiRuntimeServicesTableLib.h>
-#include <Library/UefiLib.h>
-#include <Library/PrintLib.h>
-#include <Library/BmpSupportLib.h>
-#include <Protocol/GraphicsOutput.h>
-#include <Guid/GlobalVariable.h>
-#include <Guid/CapsuleReport.h>
-#include <Guid/SystemResourceTable.h>
-#include <Guid/FmpCapsule.h>
-#include <IndustryStandard/WindowsUxCapsule.h>
-
-#define CAPSULE_HEADER_SIZE  0x20
-
-#define NESTED_CAPSULE_HEADER_SIZE  SIZE_4KB
-#define SYSTEM_FIRMWARE_FLAG 0x50000
-#define DEVICE_FIRMWARE_FLAG 0x78010
-
-#define MAJOR_VERSION   1
-#define MINOR_VERSION   0
-
-#define MAX_CAPSULE_NUM 10
-
-extern UINTN  Argc;
-extern CHAR16 **Argv;
+#include "CapsuleApp.h"
 
 //
 // Define how many block descriptors we want to test with.
 //
 UINTN  NumberOfDescriptors = 1;
 UINTN  CapsuleFirstIndex;
 UINTN  CapsuleLastIndex;
 
-/**
-  Dump capsule information
-
-  @param[in] CapsuleName  The name of the capsule image.
-
-  @retval EFI_SUCCESS            The capsule information is dumped.
-  @retval EFI_UNSUPPORTED        Input parameter is not valid.
-**/
-EFI_STATUS
-DumpCapsule (
-  IN CHAR16                                        *CapsuleName
-  );
-
-/**
-  Dump capsule status variable.
-
-  @retval EFI_SUCCESS            The capsule status variable is dumped.
-  @retval EFI_UNSUPPORTED        Input parameter is not valid.
-**/
-EFI_STATUS
-DumpCapsuleStatusVariable (
-  VOID
-  );
-
-/**
-  Dump FMP protocol info.
-**/
-VOID
-DumpFmpData (
-  VOID
-  );
-
-/**
-  Dump FMP image data.
-
-  @param[in]  ImageTypeId   The ImageTypeId of the FMP image.
-                            It is used to identify the FMP protocol.
-  @param[in]  ImageIndex    The ImageIndex of the FMP image.
-                            It is the input parameter for FMP->GetImage().
-  @param[in]  ImageName     The file name to hold the output FMP image.
-**/
-VOID
-DumpFmpImage (
-  IN EFI_GUID  *ImageTypeId,
-  IN UINTN     ImageIndex,
-  IN CHAR16    *ImageName
-  );
-
-/**
-  Dump ESRT info.
-**/
-VOID
-DumpEsrtData (
-  VOID
-  );
-
-/**
-  Dump Provisioned Capsule.
-
-  @param[in]  DumpCapsuleInfo  The flag to indicate whether to dump the capsule inforomation.
-**/
-VOID
-DumpProvisionedCapsule (
-  IN BOOLEAN                      DumpCapsuleInfo
-  );
-
-/**
-  Dump all EFI System Partition.
-**/
-VOID
-DumpAllEfiSysPartition (
-  VOID
-  );
-
-/**
-  Process Capsule On Disk.
-
-  @param[in]  CapsuleBuffer       An array of pointer to capsule images
-  @param[in]  CapsuleBufferSize   An array of UINTN to capsule images size
-  @param[in]  FilePath            An array of capsule images file path
-  @param[in]  Map                 File system mapping string
-  @param[in]  CapsuleNum          The count of capsule images
-
-  @retval EFI_SUCCESS       Capsule on disk success.
-  @retval others            Capsule on disk fail.
-
-**/
-EFI_STATUS
-ProcessCapsuleOnDisk (
-  IN VOID                          **CapsuleBuffer,
-  IN UINTN                         *CapsuleBufferSize,
-  IN CHAR16                        **FilePath,
-  IN CHAR16                        *Map,
-  IN UINTN                         CapsuleNum
-  );
-
-/**
-  Read a file.
-
-  @param[in]  FileName        The file to be read.
-  @param[out] BufferSize      The file buffer size
-  @param[out] Buffer          The file buffer
-
-  @retval EFI_SUCCESS    Read file successfully
-  @retval EFI_NOT_FOUND  Shell protocol or file not found
-  @retval others         Read file failed
-**/
-EFI_STATUS
-ReadFileToBuffer (
-  IN  CHAR16                               *FileName,
-  OUT UINTN                                *BufferSize,
-  OUT VOID                                 **Buffer
-  );
-
-/**
-  Write a file.
-
-  @param[in] FileName        The file to be written.
-  @param[in] BufferSize      The file buffer size
-  @param[in] Buffer          The file buffer
-
-  @retval EFI_SUCCESS    Write file successfully
-  @retval EFI_NOT_FOUND  Shell protocol not found
-  @retval others         Write file failed
-**/
-EFI_STATUS
-WriteFileFromBuffer (
-  IN  CHAR16                               *FileName,
-  IN  UINTN                                BufferSize,
-  IN  VOID                                 *Buffer
-  );
-
-/**
-
-  This function parse application ARG.
-
-  @return Status
-**/
-EFI_STATUS
-GetArg (
-  VOID
-  );
-
 /**
   Create UX capsule.
 
   @retval EFI_SUCCESS            The capsule header is appended.
   @retval EFI_UNSUPPORTED        Input parameter is not valid.
@@ -847,11 +675,11 @@ PrintUsage (
   Print(L"  CapsuleApp -D <Capsule>\n");
   Print(L"  CapsuleApp -P GET <ImageTypeId> <Index> -O <FileName>\n");
   Print(L"Parameter:\n");
   Print(L"  -NR: No reset will be triggered for the capsule\n");
   Print(L"       with CAPSULE_FLAGS_PERSIST_ACROSS_RESET and without CAPSULE_FLAGS_INITIATE_RESET.\n");
-  Print(L"  -OD: Delivery of Capsules via file on Mass Storage device.");
+  Print(L"  -OD: Delivery of Capsules via file on Mass Storage device.\n");
   Print(L"  -S:  Dump capsule report variable (EFI_CAPSULE_REPORT_GUID),\n");
   Print(L"       which is defined in UEFI specification.\n");
   Print(L"  -C:  Clear capsule report variable (EFI_CAPSULE_REPORT_GUID),\n");
   Print(L"       which is defined in UEFI specification.\n");
   Print(L"  -P:  Dump UEFI FMP protocol info, or get image with specified\n");
@@ -1018,44 +846,43 @@ UefiMain (
       ParaNrIndex = Index;
       NoReset = TRUE;
     }
   }
 
-  if (ParaOdIndex != 0) {
-    if (ParaOdIndex == Argc - 1) {
+  if (ParaOdIndex > ParaNrIndex) {
+    if (ParaNrIndex != 0) {
+      CapsuleLastIndex = ParaNrIndex - 1;
+    } else {
+      CapsuleLastIndex = ParaOdIndex - 1;
+    }
+
+    if (ParaOdIndex == Argc -1) {
       MapFsStr = NULL;
     } else if (ParaOdIndex == Argc - 2) {
       MapFsStr = Argv[Argc-1];
     } else {
-      Print (L"CapsuleApp: Invalid Position for -OD Options\n");
+      Print (L"CapsuleApp: Cannot specify more than one FS mapping!\n");
       Status = EFI_INVALID_PARAMETER;
       goto Done;
     }
-
-    if (ParaNrIndex != 0) {
-      if (ParaNrIndex + 1 == ParaOdIndex) {
-        CapsuleLastIndex = ParaNrIndex - 1;
-      } else {
-        Print (L"CapsuleApp: Invalid Position for -NR Options\n");
-        Status = EFI_INVALID_PARAMETER;
-        goto Done;
-      }
-    } else {
+  } else if (ParaOdIndex < ParaNrIndex) {
+    if (ParaOdIndex != 0) {
       CapsuleLastIndex = ParaOdIndex - 1;
-    }
-  } else {
-    if (ParaNrIndex != 0) {
-      if (ParaNrIndex == Argc -1) {
-        CapsuleLastIndex = ParaNrIndex - 1;
+      if (ParaOdIndex == ParaNrIndex - 1) {
+        MapFsStr = NULL;
+      } else if (ParaOdIndex == ParaNrIndex - 2) {
+        MapFsStr = Argv[ParaOdIndex + 1];
       } else {
-        Print (L"CapsuleApp: Invalid Position for -NR Options\n");
+        Print (L"CapsuleApp: Cannot specify more than one FS mapping!\n");
         Status = EFI_INVALID_PARAMETER;
         goto Done;
       }
     } else {
-      CapsuleLastIndex = Argc - 1;
+      CapsuleLastIndex = ParaNrIndex - 1;
     }
+  } else {
+    CapsuleLastIndex = Argc - 1;
   }
 
   CapsuleNum = CapsuleLastIndex - CapsuleFirstIndex + 1;
 
   if (CapsuleFirstIndex > CapsuleLastIndex) {
diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.h b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.h
new file mode 100644
index 0000000000..270d2359a3
--- /dev/null
+++ b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.h
@@ -0,0 +1,240 @@
+/** @file
+  A shell application that triggers capsule update process.
+
+  Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+
+#ifndef _CAPSULE_APP_H_
+#define _CAPSULE_APP_H_
+
+#include <Uefi.h>
+#include <Library/BaseLib.h>
+#include <Library/DebugLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/UefiRuntimeServicesTableLib.h>
+#include <Library/UefiLib.h>
+#include <Library/PrintLib.h>
+#include <Library/BmpSupportLib.h>
+#include <Library/FileHandleLib.h>
+#include <Library/SortLib.h>
+#include <Library/UefiBootManagerLib.h>
+#include <Library/DevicePathLib.h>
+#include <Protocol/GraphicsOutput.h>
+#include <Protocol/SimpleFileSystem.h>
+#include <Protocol/ShellParameters.h>
+#include <Protocol/Shell.h>
+#include <Protocol/FirmwareManagement.h>
+#include <Guid/GlobalVariable.h>
+#include <Guid/CapsuleReport.h>
+#include <Guid/SystemResourceTable.h>
+#include <Guid/FmpCapsule.h>
+#include <Guid/FileInfo.h>
+#include <Guid/ImageAuthentication.h>
+#include <Guid/CapsuleVendor.h>
+#include <Guid/Gpt.h>
+#include <IndustryStandard/WindowsUxCapsule.h>
+
+#define CAPSULE_HEADER_SIZE  0x20
+
+#define NESTED_CAPSULE_HEADER_SIZE  SIZE_4KB
+#define SYSTEM_FIRMWARE_FLAG 0x50000
+#define DEVICE_FIRMWARE_FLAG 0x78010
+
+#define MAJOR_VERSION   1
+#define MINOR_VERSION   0
+
+#define MAX_CAPSULE_NUM 10
+
+//
+// (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)
+//
+#define MAX_FILE_NAME_SIZE   522
+#define MAX_FILE_NAME_LEN    (MAX_FILE_NAME_SIZE / sizeof(CHAR16))
+
+extern UINTN  Argc;
+extern CHAR16 **Argv;
+
+/**
+
+  This function parse application ARG.
+
+  @return Status
+**/
+EFI_STATUS
+GetArg (
+  VOID
+  );
+
+/**
+  Get shell protocol.
+
+  @return Pointer to shell protocol.
+
+**/
+EFI_SHELL_PROTOCOL *
+GetShellProtocol (
+  VOID
+  );
+
+
+/**
+  Read a file.
+
+  @param[in]  FileName        The file to be read.
+  @param[out] BufferSize      The file buffer size
+  @param[out] Buffer          The file buffer
+
+  @retval EFI_SUCCESS    Read file successfully
+  @retval EFI_NOT_FOUND  Shell protocol or file not found
+  @retval others         Read file failed
+**/
+EFI_STATUS
+ReadFileToBuffer (
+  IN  CHAR16                               *FileName,
+  OUT UINTN                                *BufferSize,
+  OUT VOID                                 **Buffer
+  );
+
+/**
+  Write a file.
+
+  @param[in] FileName        The file to be written.
+  @param[in] BufferSize      The file buffer size
+  @param[in] Buffer          The file buffer
+
+  @retval EFI_SUCCESS    Write file successfully
+  @retval EFI_NOT_FOUND  Shell protocol not found
+  @retval others         Write file failed
+**/
+EFI_STATUS
+WriteFileFromBuffer (
+  IN  CHAR16                               *FileName,
+  IN  UINTN                                BufferSize,
+  IN  VOID                                 *Buffer
+  );
+
+
+/**
+  Dump capsule information
+
+  @param[in] CapsuleName  The name of the capsule image.
+
+  @retval EFI_SUCCESS            The capsule information is dumped.
+  @retval EFI_UNSUPPORTED        Input parameter is not valid.
+**/
+EFI_STATUS
+DumpCapsule (
+  IN CHAR16                                        *CapsuleName
+  );
+
+/**
+  Dump capsule status variable.
+
+  @retval EFI_SUCCESS            The capsule status variable is dumped.
+  @retval EFI_UNSUPPORTED        Input parameter is not valid.
+**/
+EFI_STATUS
+DumpCapsuleStatusVariable (
+  VOID
+  );
+
+/**
+  Dump FMP protocol info.
+**/
+VOID
+DumpFmpData (
+  VOID
+  );
+
+/**
+  Dump FMP image data.
+
+  @param[in]  ImageTypeId   The ImageTypeId of the FMP image.
+                            It is used to identify the FMP protocol.
+  @param[in]  ImageIndex    The ImageIndex of the FMP image.
+                            It is the input parameter for FMP->GetImage().
+  @param[in]  ImageName     The file name to hold the output FMP image.
+**/
+VOID
+DumpFmpImage (
+  IN EFI_GUID  *ImageTypeId,
+  IN UINTN     ImageIndex,
+  IN CHAR16    *ImageName
+  );
+
+/**
+  Dump ESRT info.
+**/
+VOID
+DumpEsrtData (
+  VOID
+  );
+
+/**
+  Dump Provisioned Capsule.
+
+  @param[in]  DumpCapsuleInfo  The flag to indicate whether to dump the capsule inforomation.
+**/
+VOID
+DumpProvisionedCapsule (
+  IN BOOLEAN                      DumpCapsuleInfo
+  );
+
+/**
+  Dump all EFI System Partition.
+**/
+VOID
+DumpAllEfiSysPartition (
+  VOID
+  );
+
+
+/**
+  Get SimpleFileSystem from boot option file path.
+
+  @param[in]  DevicePath     The file path of boot option
+  @param[out] FullPath       The full device path of boot device
+  @param[out] Fs             The file system within EfiSysPartition
+
+  @retval EFI_SUCCESS    Get file system successfully
+  @retval EFI_NOT_FOUND  No valid file system found
+  @retval others         Get file system failed
+
+**/
+EFI_STATUS
+GetEfiSysPartitionFromBootOptionFilePath (
+  IN  EFI_DEVICE_PATH_PROTOCOL         *DevicePath,
+  OUT EFI_DEVICE_PATH_PROTOCOL         **FullPath,
+  OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  **Fs
+  );
+
+
+/**
+  Process Capsule On Disk.
+
+  @param[in]  CapsuleBuffer       An array of pointer to capsule images
+  @param[in]  CapsuleBufferSize   An array of UINTN to capsule images size
+  @param[in]  FilePath            An array of capsule images file path
+  @param[in]  Map                 File system mapping string
+  @param[in]  CapsuleNum          The count of capsule images
+
+  @retval EFI_SUCCESS       Capsule on disk success.
+  @retval others            Capsule on disk fail.
+
+**/
+EFI_STATUS
+ProcessCapsuleOnDisk (
+  IN VOID                          **CapsuleBuffer,
+  IN UINTN                         *CapsuleBufferSize,
+  IN CHAR16                        **FilePath,
+  IN CHAR16                        *Map,
+  IN UINTN                         CapsuleNum
+  );
+
+#endif
+
diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.inf b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.inf
index 8ca5e04104..6ed0659835 100644
--- a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.inf
+++ b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.inf
@@ -2,11 +2,11 @@
 #  A shell application that triggers capsule update process.
 #
 # This application can trigger capsule update process. It can also
 # generate capsule image, or dump capsule variable information.
 #
-#  Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
 ##
 
 [Defines]
@@ -24,10 +24,11 @@
 #  VALID_ARCHITECTURES           = IA32 X64
 #
 
 [Sources]
   CapsuleApp.c
+  CapsuleApp.h
   CapsuleDump.c
   CapsuleOnDisk.c
   AppSupport.c
 
 [Packages]
diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
index b81c5b7b3a..58a93568d0 100644
--- a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
+++ b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
@@ -4,102 +4,11 @@
   Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
-#include <PiDxe.h>
-#include <Library/BaseLib.h>
-#include <Library/DebugLib.h>
-#include <Library/BaseMemoryLib.h>
-#include <Library/MemoryAllocationLib.h>
-#include <Library/UefiBootServicesTableLib.h>
-#include <Library/UefiRuntimeServicesTableLib.h>
-#include <Library/UefiLib.h>
-#include <Library/PrintLib.h>
-#include <Library/FileHandleLib.h>
-#include <Library/SortLib.h>
-#include <Library/UefiBootManagerLib.h>
-#include <Library/DevicePathLib.h>
-#include <Protocol/FirmwareManagement.h>
-#include <Protocol/SimpleFileSystem.h>
-#include <Protocol/Shell.h>
-#include <Guid/ImageAuthentication.h>
-#include <Guid/CapsuleReport.h>
-#include <Guid/SystemResourceTable.h>
-#include <Guid/FmpCapsule.h>
-#include <Guid/CapsuleVendor.h>
-#include <IndustryStandard/WindowsUxCapsule.h>
-
-//
-// (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)
-//
-#define MAX_FILE_NAME_SIZE   522
-#define MAX_FILE_NAME_LEN    (MAX_FILE_NAME_SIZE / sizeof(CHAR16))
-
-/**
-  Read a file.
-
-  @param[in]  FileName        The file to be read.
-  @param[out] BufferSize      The file buffer size
-  @param[out] Buffer          The file buffer
-
-  @retval EFI_SUCCESS    Read file successfully
-  @retval EFI_NOT_FOUND  File not found
-**/
-EFI_STATUS
-ReadFileToBuffer (
-  IN  CHAR16                               *FileName,
-  OUT UINTN                                *BufferSize,
-  OUT VOID                                 **Buffer
-  );
-
-/**
-  Write a file.
-
-  @param[in] FileName        The file to be written.
-  @param[in] BufferSize      The file buffer size
-  @param[in] Buffer          The file buffer
-
-  @retval EFI_SUCCESS    Write file successfully
-**/
-EFI_STATUS
-WriteFileFromBuffer (
-  IN  CHAR16                               *FileName,
-  IN  UINTN                                BufferSize,
-  IN  VOID                                 *Buffer
-  );
-
-/**
-  Get shell protocol.
-
-  @return Pointer to shell protocol.
-
-**/
-EFI_SHELL_PROTOCOL *
-GetShellProtocol (
-  VOID
-  );
-
-/**
-  Get SimpleFileSystem from boot option file path.
-
-  @param[in]  DevicePath     The file path of boot option
-  @param[out] FullPath       The full device path of boot device
-  @param[out] Fs             The file system within EfiSysPartition
-
-  @retval EFI_SUCCESS    Get file system successfully
-  @retval EFI_NOT_FOUND  No valid file system found
-  @retval others         Get file system failed
-
-**/
-EFI_STATUS
-EFIAPI
-GetEfiSysPartitionFromBootOptionFilePath (
-  IN  EFI_DEVICE_PATH_PROTOCOL         *DevicePath,
-  OUT EFI_DEVICE_PATH_PROTOCOL         **FullPath,
-  OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  **Fs
-  );
+#include "CapsuleApp.h"
 
 /**
   Validate if it is valid capsule header
 
   This function assumes the caller provided correct CapsuleHeader pointer
@@ -707,11 +616,10 @@ SplitFileNameExtension (
   @return <0                 Buffer1 is less than Buffer2.
   @return >0                 Buffer1 is greater than Buffer2.
 
 **/
 INTN
-EFIAPI
 CompareFileNameInAlphabet (
   IN VOID                         *Left,
   IN VOID                         *Right
   )
 {
@@ -810,12 +718,12 @@ DumpCapsuleFromDisk (
   }
 
   //
   // Get file count first
   //
+  Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
   do {
-    Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
     if (EFI_ERROR (Status) || FileInfo == NULL) {
       Print (L"Get File Info Fail. Status = %r\n", Status);
       goto Done;
     }
 
@@ -844,12 +752,12 @@ DumpCapsuleFromDisk (
   NoFile = FALSE;
 
   //
   // Get all file info
   //
+  Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
   do {
-    Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
     if (EFI_ERROR (Status) || FileInfo == NULL) {
       Print (L"Get File Info Fail. Status = %r\n", Status);
       goto Done;
     }
 
diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c b/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
index a11683d66c..382efa9aa0 100644
--- a/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
+++ b/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
@@ -3,40 +3,14 @@
 
   Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
-#include <Uefi.h>
-#include <Library/BaseLib.h>
-#include <Library/DebugLib.h>
-#include <Library/BaseMemoryLib.h>
-#include <Library/MemoryAllocationLib.h>
-#include <Library/UefiBootServicesTableLib.h>
-#include <Library/UefiRuntimeServicesTableLib.h>
-#include <Library/UefiLib.h>
-#include <Library/PrintLib.h>
-#include <Library/DevicePathLib.h>
-#include <Library/FileHandleLib.h>
-#include <Library/UefiBootManagerLib.h>
-#include <Protocol/SimpleFileSystem.h>
-#include <Protocol/Shell.h>
-#include <Guid/FileInfo.h>
-#include <Guid/GlobalVariable.h>
-#include <Guid/Gpt.h>
 
-EFI_GUID mCapsuleOnDiskBootOptionGuid = { 0x4CC29BB7, 0x2413, 0x40A2, { 0xB0, 0x6D, 0x25, 0x3E, 0x37, 0x10, 0xF5, 0x32 } };
-
-/**
-  Get shell protocol.
+#include "CapsuleApp.h"
 
-  @return Pointer to shell protocol.
-
-**/
-EFI_SHELL_PROTOCOL *
-GetShellProtocol (
-  VOID
-  );
+EFI_GUID mCapsuleOnDiskBootOptionGuid = { 0x4CC29BB7, 0x2413, 0x40A2, { 0xB0, 0x6D, 0x25, 0x3E, 0x37, 0x10, 0xF5, 0x32 } };
 
 /**
   Get file name from file path.
 
   @param  FilePath    File path.
@@ -328,11 +302,10 @@ GetEfiSysPartitionFromDevPath (
   @retval EFI_NOT_FOUND  No valid file system found
   @retval others         Get file system failed
 
 **/
 EFI_STATUS
-EFIAPI
 GetEfiSysPartitionFromBootOptionFilePath (
   IN  EFI_DEVICE_PATH_PROTOCOL         *DevicePath,
   OUT EFI_DEVICE_PATH_PROTOCOL         **FullPath,
   OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  **Fs
   )
@@ -396,11 +369,10 @@ GetEfiSysPartitionFromBootOptionFilePath (
   @retval EFI_NOT_FOUND  No valid FS found
   @retval others         Get FS failed
 
 **/
 EFI_STATUS
-EFIAPI
 GetUpdateFileSystem (
   IN  CHAR16                           *Map,
   OUT UINT16                           *BootNext,
   OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  **Fs,
   OUT BOOLEAN                          *UpdateBootNext
@@ -742,10 +714,45 @@ SetCapsuleStatusVariable (
                   );
 
   return Status;
 }
 
+/**
+  Check if Capsule On Disk is supported.
+
+  @retval TRUE              Capsule On Disk is supported.
+  @retval FALSE             Capsule On Disk is not supported.
+
+**/
+BOOLEAN
+IsCapsuleOnDiskSupported (
+  VOID
+  )
+{
+  EFI_STATUS                    Status;
+  UINT64                        OsIndicationsSupported;
+  UINTN                         DataSize;
+
+  DataSize = sizeof(UINT64);
+  Status = gRT->GetVariable (
+                  L"OsIndicationsSupported",
+                  &gEfiGlobalVariableGuid,
+                  NULL,
+                  &DataSize,
+                  &OsIndicationsSupported
+                  );
+  if (EFI_ERROR (Status)) {
+    return FALSE;
+  }
+
+  if (OsIndicationsSupported & EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) {
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
 /**
   Process Capsule On Disk.
 
   @param[in]  CapsuleBuffer       An array of pointer to capsule images
   @param[in]  CapsuleBufferSize   An array of UINTN to capsule images size
@@ -768,10 +775,20 @@ ProcessCapsuleOnDisk (
 {
   EFI_STATUS                      Status;
   UINT16                          BootNext;
   EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Fs;
   BOOLEAN                         UpdateBootNext;
+  CHAR16                          *FileName[MAX_CAPSULE_NUM];
+  UINTN                           Index;
+
+  //
+  // Check if Capsule On Disk is supported
+  //
+  if (!IsCapsuleOnDiskSupported ()) {
+    Print (L"CapsuleApp: Capsule On Disk is not supported.\n");
+    return EFI_UNSUPPORTED;
+  }
 
   //
   // Get a valid file system from boot path
   //
   Fs = NULL;
@@ -780,14 +797,21 @@ ProcessCapsuleOnDisk (
   if (EFI_ERROR (Status)) {
     Print (L"CapsuleApp: cannot find a valid file system on boot devies. Status = %r\n", Status);
     return Status;
   }
 
+  //
+  // Get file name from file path
+  //
+  for (Index = 0; Index < CapsuleNum; Index ++) {
+    FileName[Index] = GetFileNameFromPath (FilePath[Index]);
+  }
+
   //
   // Copy capsule image to '\efi\UpdateCapsule\'
   //
-  Status = WriteUpdateFile (CapsuleBuffer, CapsuleBufferSize, FilePath, CapsuleNum, Fs);
+  Status = WriteUpdateFile (CapsuleBuffer, CapsuleBufferSize, FileName, CapsuleNum, Fs);
   if (EFI_ERROR (Status)) {
     Print (L"CapsuleApp: capsule image could not be copied for update.\n");
     return Status;
   }
 
-- 
2.16.2.windows.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [edk2-devel][Patch] MdeModulePkg/CapsuleApp: Enhance Capsule-On-Disk related functions.
  2019-06-25  6:51 [edk2-devel][Patch] MdeModulePkg/CapsuleApp: Enhance Capsule-On-Disk related functions Xu, Wei6
@ 2019-06-26  1:45 ` Wu, Hao A
  2019-06-26  2:06 ` Zhang, Chao B
  1 sibling, 0 replies; 6+ messages in thread
From: Wu, Hao A @ 2019-06-26  1:45 UTC (permalink / raw)
  To: Zhang, Chao B, Xu, Wei6, devel@edk2.groups.io; +Cc: Wang, Jian J

> -----Original Message-----
> From: Xu, Wei6
> Sent: Tuesday, June 25, 2019 2:51 PM
> To: devel@edk2.groups.io
> Cc: Wang, Jian J; Wu, Hao A; Zhang, Chao B
> Subject: [edk2-devel][Patch] MdeModulePkg/CapsuleApp: Enhance
> Capsule-On-Disk related functions.
> 
> BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=1840
> 
> 1. Introduce an internal header file to put definitions in it.
> 2. Add missing '\n' in usage.
> 3. Fix the dead loop of CapsuleApp -L.
> 4. Fix the bug that CapsuleApp -OD cannot perform capsules in sub-
> folder.
> 5. Optimize the handling for option -NR and -OD to support both
> 'CapsuleApp <Capsule> -OD -NR' and 'CapsuleApp <Capsule> -NR -OD'.
> 6. Check if Capsule-On-Disk is supported by "OsIndicationsSupported"
> variable firstly before processing capsules. If not supported, prompt
> an error message and quit the process.


Acked-by: Hao A Wu <hao.a.wu@intel.com>
Hello Chao, could you help to review this patch? Thanks in advance.

Best Regards,
Hao Wu


> 
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Chao B Zhang <chao.b.zhang@intel.com>
> Signed-off-by: Wei6 Xu <wei6.xu@intel.com>
> ---
>  MdeModulePkg/Application/CapsuleApp/AppSupport.c   |  13 +-
>  MdeModulePkg/Application/CapsuleApp/CapsuleApp.c   | 215 ++-------------
> ---
>  MdeModulePkg/Application/CapsuleApp/CapsuleApp.h   | 240
> +++++++++++++++++++++
>  MdeModulePkg/Application/CapsuleApp/CapsuleApp.inf |   3 +-
>  MdeModulePkg/Application/CapsuleApp/CapsuleDump.c  |  98 +--------
>  .../Application/CapsuleApp/CapsuleOnDisk.c         |  86 +++++---
>  6 files changed, 323 insertions(+), 332 deletions(-)
>  create mode 100644
> MdeModulePkg/Application/CapsuleApp/CapsuleApp.h
> 
> diff --git a/MdeModulePkg/Application/CapsuleApp/AppSupport.c
> b/MdeModulePkg/Application/CapsuleApp/AppSupport.c
> index d9ce1b4843..8fe70dc3b6 100644
> --- a/MdeModulePkg/Application/CapsuleApp/AppSupport.c
> +++ b/MdeModulePkg/Application/CapsuleApp/AppSupport.c
> @@ -1,23 +1,14 @@
>  /** @file
>    A shell application that triggers capsule update process.
> 
> -  Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> 
> -#include <Uefi.h>
> -#include <Library/BaseLib.h>
> -#include <Library/DebugLib.h>
> -#include <Library/BaseMemoryLib.h>
> -#include <Library/MemoryAllocationLib.h>
> -#include <Library/UefiBootServicesTableLib.h>
> -#include <Protocol/SimpleFileSystem.h>
> -#include <Protocol/ShellParameters.h>
> -#include <Protocol/Shell.h>
> -#include <Guid/FileInfo.h>
> +#include "CapsuleApp.h"
> 
>  UINTN  Argc;
>  CHAR16 **Argv;
>  EFI_SHELL_PROTOCOL      *mShellProtocol = NULL;
> 
> diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
> b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
> index e3c591dbf3..3439ce5feb 100644
> --- a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
> +++ b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
> @@ -4,191 +4,19 @@
>    Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> 
> -#include <Uefi.h>
> -#include <Library/BaseLib.h>
> -#include <Library/DebugLib.h>
> -#include <Library/BaseMemoryLib.h>
> -#include <Library/MemoryAllocationLib.h>
> -#include <Library/UefiBootServicesTableLib.h>
> -#include <Library/UefiRuntimeServicesTableLib.h>
> -#include <Library/UefiLib.h>
> -#include <Library/PrintLib.h>
> -#include <Library/BmpSupportLib.h>
> -#include <Protocol/GraphicsOutput.h>
> -#include <Guid/GlobalVariable.h>
> -#include <Guid/CapsuleReport.h>
> -#include <Guid/SystemResourceTable.h>
> -#include <Guid/FmpCapsule.h>
> -#include <IndustryStandard/WindowsUxCapsule.h>
> -
> -#define CAPSULE_HEADER_SIZE  0x20
> -
> -#define NESTED_CAPSULE_HEADER_SIZE  SIZE_4KB
> -#define SYSTEM_FIRMWARE_FLAG 0x50000
> -#define DEVICE_FIRMWARE_FLAG 0x78010
> -
> -#define MAJOR_VERSION   1
> -#define MINOR_VERSION   0
> -
> -#define MAX_CAPSULE_NUM 10
> -
> -extern UINTN  Argc;
> -extern CHAR16 **Argv;
> +#include "CapsuleApp.h"
> 
>  //
>  // Define how many block descriptors we want to test with.
>  //
>  UINTN  NumberOfDescriptors = 1;
>  UINTN  CapsuleFirstIndex;
>  UINTN  CapsuleLastIndex;
> 
> -/**
> -  Dump capsule information
> -
> -  @param[in] CapsuleName  The name of the capsule image.
> -
> -  @retval EFI_SUCCESS            The capsule information is dumped.
> -  @retval EFI_UNSUPPORTED        Input parameter is not valid.
> -**/
> -EFI_STATUS
> -DumpCapsule (
> -  IN CHAR16                                        *CapsuleName
> -  );
> -
> -/**
> -  Dump capsule status variable.
> -
> -  @retval EFI_SUCCESS            The capsule status variable is dumped.
> -  @retval EFI_UNSUPPORTED        Input parameter is not valid.
> -**/
> -EFI_STATUS
> -DumpCapsuleStatusVariable (
> -  VOID
> -  );
> -
> -/**
> -  Dump FMP protocol info.
> -**/
> -VOID
> -DumpFmpData (
> -  VOID
> -  );
> -
> -/**
> -  Dump FMP image data.
> -
> -  @param[in]  ImageTypeId   The ImageTypeId of the FMP image.
> -                            It is used to identify the FMP protocol.
> -  @param[in]  ImageIndex    The ImageIndex of the FMP image.
> -                            It is the input parameter for FMP->GetImage().
> -  @param[in]  ImageName     The file name to hold the output FMP image.
> -**/
> -VOID
> -DumpFmpImage (
> -  IN EFI_GUID  *ImageTypeId,
> -  IN UINTN     ImageIndex,
> -  IN CHAR16    *ImageName
> -  );
> -
> -/**
> -  Dump ESRT info.
> -**/
> -VOID
> -DumpEsrtData (
> -  VOID
> -  );
> -
> -/**
> -  Dump Provisioned Capsule.
> -
> -  @param[in]  DumpCapsuleInfo  The flag to indicate whether to dump the
> capsule inforomation.
> -**/
> -VOID
> -DumpProvisionedCapsule (
> -  IN BOOLEAN                      DumpCapsuleInfo
> -  );
> -
> -/**
> -  Dump all EFI System Partition.
> -**/
> -VOID
> -DumpAllEfiSysPartition (
> -  VOID
> -  );
> -
> -/**
> -  Process Capsule On Disk.
> -
> -  @param[in]  CapsuleBuffer       An array of pointer to capsule images
> -  @param[in]  CapsuleBufferSize   An array of UINTN to capsule images size
> -  @param[in]  FilePath            An array of capsule images file path
> -  @param[in]  Map                 File system mapping string
> -  @param[in]  CapsuleNum          The count of capsule images
> -
> -  @retval EFI_SUCCESS       Capsule on disk success.
> -  @retval others            Capsule on disk fail.
> -
> -**/
> -EFI_STATUS
> -ProcessCapsuleOnDisk (
> -  IN VOID                          **CapsuleBuffer,
> -  IN UINTN                         *CapsuleBufferSize,
> -  IN CHAR16                        **FilePath,
> -  IN CHAR16                        *Map,
> -  IN UINTN                         CapsuleNum
> -  );
> -
> -/**
> -  Read a file.
> -
> -  @param[in]  FileName        The file to be read.
> -  @param[out] BufferSize      The file buffer size
> -  @param[out] Buffer          The file buffer
> -
> -  @retval EFI_SUCCESS    Read file successfully
> -  @retval EFI_NOT_FOUND  Shell protocol or file not found
> -  @retval others         Read file failed
> -**/
> -EFI_STATUS
> -ReadFileToBuffer (
> -  IN  CHAR16                               *FileName,
> -  OUT UINTN                                *BufferSize,
> -  OUT VOID                                 **Buffer
> -  );
> -
> -/**
> -  Write a file.
> -
> -  @param[in] FileName        The file to be written.
> -  @param[in] BufferSize      The file buffer size
> -  @param[in] Buffer          The file buffer
> -
> -  @retval EFI_SUCCESS    Write file successfully
> -  @retval EFI_NOT_FOUND  Shell protocol not found
> -  @retval others         Write file failed
> -**/
> -EFI_STATUS
> -WriteFileFromBuffer (
> -  IN  CHAR16                               *FileName,
> -  IN  UINTN                                BufferSize,
> -  IN  VOID                                 *Buffer
> -  );
> -
> -/**
> -
> -  This function parse application ARG.
> -
> -  @return Status
> -**/
> -EFI_STATUS
> -GetArg (
> -  VOID
> -  );
> -
>  /**
>    Create UX capsule.
> 
>    @retval EFI_SUCCESS            The capsule header is appended.
>    @retval EFI_UNSUPPORTED        Input parameter is not valid.
> @@ -847,11 +675,11 @@ PrintUsage (
>    Print(L"  CapsuleApp -D <Capsule>\n");
>    Print(L"  CapsuleApp -P GET <ImageTypeId> <Index> -O <FileName>\n");
>    Print(L"Parameter:\n");
>    Print(L"  -NR: No reset will be triggered for the capsule\n");
>    Print(L"       with CAPSULE_FLAGS_PERSIST_ACROSS_RESET and without
> CAPSULE_FLAGS_INITIATE_RESET.\n");
> -  Print(L"  -OD: Delivery of Capsules via file on Mass Storage device.");
> +  Print(L"  -OD: Delivery of Capsules via file on Mass Storage device.\n");
>    Print(L"  -S:  Dump capsule report variable
> (EFI_CAPSULE_REPORT_GUID),\n");
>    Print(L"       which is defined in UEFI specification.\n");
>    Print(L"  -C:  Clear capsule report variable
> (EFI_CAPSULE_REPORT_GUID),\n");
>    Print(L"       which is defined in UEFI specification.\n");
>    Print(L"  -P:  Dump UEFI FMP protocol info, or get image with specified\n");
> @@ -1018,44 +846,43 @@ UefiMain (
>        ParaNrIndex = Index;
>        NoReset = TRUE;
>      }
>    }
> 
> -  if (ParaOdIndex != 0) {
> -    if (ParaOdIndex == Argc - 1) {
> +  if (ParaOdIndex > ParaNrIndex) {
> +    if (ParaNrIndex != 0) {
> +      CapsuleLastIndex = ParaNrIndex - 1;
> +    } else {
> +      CapsuleLastIndex = ParaOdIndex - 1;
> +    }
> +
> +    if (ParaOdIndex == Argc -1) {
>        MapFsStr = NULL;
>      } else if (ParaOdIndex == Argc - 2) {
>        MapFsStr = Argv[Argc-1];
>      } else {
> -      Print (L"CapsuleApp: Invalid Position for -OD Options\n");
> +      Print (L"CapsuleApp: Cannot specify more than one FS mapping!\n");
>        Status = EFI_INVALID_PARAMETER;
>        goto Done;
>      }
> -
> -    if (ParaNrIndex != 0) {
> -      if (ParaNrIndex + 1 == ParaOdIndex) {
> -        CapsuleLastIndex = ParaNrIndex - 1;
> -      } else {
> -        Print (L"CapsuleApp: Invalid Position for -NR Options\n");
> -        Status = EFI_INVALID_PARAMETER;
> -        goto Done;
> -      }
> -    } else {
> +  } else if (ParaOdIndex < ParaNrIndex) {
> +    if (ParaOdIndex != 0) {
>        CapsuleLastIndex = ParaOdIndex - 1;
> -    }
> -  } else {
> -    if (ParaNrIndex != 0) {
> -      if (ParaNrIndex == Argc -1) {
> -        CapsuleLastIndex = ParaNrIndex - 1;
> +      if (ParaOdIndex == ParaNrIndex - 1) {
> +        MapFsStr = NULL;
> +      } else if (ParaOdIndex == ParaNrIndex - 2) {
> +        MapFsStr = Argv[ParaOdIndex + 1];
>        } else {
> -        Print (L"CapsuleApp: Invalid Position for -NR Options\n");
> +        Print (L"CapsuleApp: Cannot specify more than one FS mapping!\n");
>          Status = EFI_INVALID_PARAMETER;
>          goto Done;
>        }
>      } else {
> -      CapsuleLastIndex = Argc - 1;
> +      CapsuleLastIndex = ParaNrIndex - 1;
>      }
> +  } else {
> +    CapsuleLastIndex = Argc - 1;
>    }
> 
>    CapsuleNum = CapsuleLastIndex - CapsuleFirstIndex + 1;
> 
>    if (CapsuleFirstIndex > CapsuleLastIndex) {
> diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.h
> b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.h
> new file mode 100644
> index 0000000000..270d2359a3
> --- /dev/null
> +++ b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.h
> @@ -0,0 +1,240 @@
> +/** @file
> +  A shell application that triggers capsule update process.
> +
> +  Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +
> +#ifndef _CAPSULE_APP_H_
> +#define _CAPSULE_APP_H_
> +
> +#include <Uefi.h>
> +#include <Library/BaseLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/MemoryAllocationLib.h>
> +#include <Library/UefiBootServicesTableLib.h>
> +#include <Library/UefiRuntimeServicesTableLib.h>
> +#include <Library/UefiLib.h>
> +#include <Library/PrintLib.h>
> +#include <Library/BmpSupportLib.h>
> +#include <Library/FileHandleLib.h>
> +#include <Library/SortLib.h>
> +#include <Library/UefiBootManagerLib.h>
> +#include <Library/DevicePathLib.h>
> +#include <Protocol/GraphicsOutput.h>
> +#include <Protocol/SimpleFileSystem.h>
> +#include <Protocol/ShellParameters.h>
> +#include <Protocol/Shell.h>
> +#include <Protocol/FirmwareManagement.h>
> +#include <Guid/GlobalVariable.h>
> +#include <Guid/CapsuleReport.h>
> +#include <Guid/SystemResourceTable.h>
> +#include <Guid/FmpCapsule.h>
> +#include <Guid/FileInfo.h>
> +#include <Guid/ImageAuthentication.h>
> +#include <Guid/CapsuleVendor.h>
> +#include <Guid/Gpt.h>
> +#include <IndustryStandard/WindowsUxCapsule.h>
> +
> +#define CAPSULE_HEADER_SIZE  0x20
> +
> +#define NESTED_CAPSULE_HEADER_SIZE  SIZE_4KB
> +#define SYSTEM_FIRMWARE_FLAG 0x50000
> +#define DEVICE_FIRMWARE_FLAG 0x78010
> +
> +#define MAJOR_VERSION   1
> +#define MINOR_VERSION   0
> +
> +#define MAX_CAPSULE_NUM 10
> +
> +//
> +// (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for
> bytes)
> +//
> +#define MAX_FILE_NAME_SIZE   522
> +#define MAX_FILE_NAME_LEN    (MAX_FILE_NAME_SIZE / sizeof(CHAR16))
> +
> +extern UINTN  Argc;
> +extern CHAR16 **Argv;
> +
> +/**
> +
> +  This function parse application ARG.
> +
> +  @return Status
> +**/
> +EFI_STATUS
> +GetArg (
> +  VOID
> +  );
> +
> +/**
> +  Get shell protocol.
> +
> +  @return Pointer to shell protocol.
> +
> +**/
> +EFI_SHELL_PROTOCOL *
> +GetShellProtocol (
> +  VOID
> +  );
> +
> +
> +/**
> +  Read a file.
> +
> +  @param[in]  FileName        The file to be read.
> +  @param[out] BufferSize      The file buffer size
> +  @param[out] Buffer          The file buffer
> +
> +  @retval EFI_SUCCESS    Read file successfully
> +  @retval EFI_NOT_FOUND  Shell protocol or file not found
> +  @retval others         Read file failed
> +**/
> +EFI_STATUS
> +ReadFileToBuffer (
> +  IN  CHAR16                               *FileName,
> +  OUT UINTN                                *BufferSize,
> +  OUT VOID                                 **Buffer
> +  );
> +
> +/**
> +  Write a file.
> +
> +  @param[in] FileName        The file to be written.
> +  @param[in] BufferSize      The file buffer size
> +  @param[in] Buffer          The file buffer
> +
> +  @retval EFI_SUCCESS    Write file successfully
> +  @retval EFI_NOT_FOUND  Shell protocol not found
> +  @retval others         Write file failed
> +**/
> +EFI_STATUS
> +WriteFileFromBuffer (
> +  IN  CHAR16                               *FileName,
> +  IN  UINTN                                BufferSize,
> +  IN  VOID                                 *Buffer
> +  );
> +
> +
> +/**
> +  Dump capsule information
> +
> +  @param[in] CapsuleName  The name of the capsule image.
> +
> +  @retval EFI_SUCCESS            The capsule information is dumped.
> +  @retval EFI_UNSUPPORTED        Input parameter is not valid.
> +**/
> +EFI_STATUS
> +DumpCapsule (
> +  IN CHAR16                                        *CapsuleName
> +  );
> +
> +/**
> +  Dump capsule status variable.
> +
> +  @retval EFI_SUCCESS            The capsule status variable is dumped.
> +  @retval EFI_UNSUPPORTED        Input parameter is not valid.
> +**/
> +EFI_STATUS
> +DumpCapsuleStatusVariable (
> +  VOID
> +  );
> +
> +/**
> +  Dump FMP protocol info.
> +**/
> +VOID
> +DumpFmpData (
> +  VOID
> +  );
> +
> +/**
> +  Dump FMP image data.
> +
> +  @param[in]  ImageTypeId   The ImageTypeId of the FMP image.
> +                            It is used to identify the FMP protocol.
> +  @param[in]  ImageIndex    The ImageIndex of the FMP image.
> +                            It is the input parameter for FMP->GetImage().
> +  @param[in]  ImageName     The file name to hold the output FMP image.
> +**/
> +VOID
> +DumpFmpImage (
> +  IN EFI_GUID  *ImageTypeId,
> +  IN UINTN     ImageIndex,
> +  IN CHAR16    *ImageName
> +  );
> +
> +/**
> +  Dump ESRT info.
> +**/
> +VOID
> +DumpEsrtData (
> +  VOID
> +  );
> +
> +/**
> +  Dump Provisioned Capsule.
> +
> +  @param[in]  DumpCapsuleInfo  The flag to indicate whether to dump the
> capsule inforomation.
> +**/
> +VOID
> +DumpProvisionedCapsule (
> +  IN BOOLEAN                      DumpCapsuleInfo
> +  );
> +
> +/**
> +  Dump all EFI System Partition.
> +**/
> +VOID
> +DumpAllEfiSysPartition (
> +  VOID
> +  );
> +
> +
> +/**
> +  Get SimpleFileSystem from boot option file path.
> +
> +  @param[in]  DevicePath     The file path of boot option
> +  @param[out] FullPath       The full device path of boot device
> +  @param[out] Fs             The file system within EfiSysPartition
> +
> +  @retval EFI_SUCCESS    Get file system successfully
> +  @retval EFI_NOT_FOUND  No valid file system found
> +  @retval others         Get file system failed
> +
> +**/
> +EFI_STATUS
> +GetEfiSysPartitionFromBootOptionFilePath (
> +  IN  EFI_DEVICE_PATH_PROTOCOL         *DevicePath,
> +  OUT EFI_DEVICE_PATH_PROTOCOL         **FullPath,
> +  OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  **Fs
> +  );
> +
> +
> +/**
> +  Process Capsule On Disk.
> +
> +  @param[in]  CapsuleBuffer       An array of pointer to capsule images
> +  @param[in]  CapsuleBufferSize   An array of UINTN to capsule images size
> +  @param[in]  FilePath            An array of capsule images file path
> +  @param[in]  Map                 File system mapping string
> +  @param[in]  CapsuleNum          The count of capsule images
> +
> +  @retval EFI_SUCCESS       Capsule on disk success.
> +  @retval others            Capsule on disk fail.
> +
> +**/
> +EFI_STATUS
> +ProcessCapsuleOnDisk (
> +  IN VOID                          **CapsuleBuffer,
> +  IN UINTN                         *CapsuleBufferSize,
> +  IN CHAR16                        **FilePath,
> +  IN CHAR16                        *Map,
> +  IN UINTN                         CapsuleNum
> +  );
> +
> +#endif
> +
> diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.inf
> b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.inf
> index 8ca5e04104..6ed0659835 100644
> --- a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.inf
> +++ b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.inf
> @@ -2,11 +2,11 @@
>  #  A shell application that triggers capsule update process.
>  #
>  # This application can trigger capsule update process. It can also
>  # generate capsule image, or dump capsule variable information.
>  #
> -#  Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
> +#  Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
>  #  SPDX-License-Identifier: BSD-2-Clause-Patent
>  #
>  ##
> 
>  [Defines]
> @@ -24,10 +24,11 @@
>  #  VALID_ARCHITECTURES           = IA32 X64
>  #
> 
>  [Sources]
>    CapsuleApp.c
> +  CapsuleApp.h
>    CapsuleDump.c
>    CapsuleOnDisk.c
>    AppSupport.c
> 
>  [Packages]
> diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
> b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
> index b81c5b7b3a..58a93568d0 100644
> --- a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
> +++ b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
> @@ -4,102 +4,11 @@
>    Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> 
> -#include <PiDxe.h>
> -#include <Library/BaseLib.h>
> -#include <Library/DebugLib.h>
> -#include <Library/BaseMemoryLib.h>
> -#include <Library/MemoryAllocationLib.h>
> -#include <Library/UefiBootServicesTableLib.h>
> -#include <Library/UefiRuntimeServicesTableLib.h>
> -#include <Library/UefiLib.h>
> -#include <Library/PrintLib.h>
> -#include <Library/FileHandleLib.h>
> -#include <Library/SortLib.h>
> -#include <Library/UefiBootManagerLib.h>
> -#include <Library/DevicePathLib.h>
> -#include <Protocol/FirmwareManagement.h>
> -#include <Protocol/SimpleFileSystem.h>
> -#include <Protocol/Shell.h>
> -#include <Guid/ImageAuthentication.h>
> -#include <Guid/CapsuleReport.h>
> -#include <Guid/SystemResourceTable.h>
> -#include <Guid/FmpCapsule.h>
> -#include <Guid/CapsuleVendor.h>
> -#include <IndustryStandard/WindowsUxCapsule.h>
> -
> -//
> -// (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for
> bytes)
> -//
> -#define MAX_FILE_NAME_SIZE   522
> -#define MAX_FILE_NAME_LEN    (MAX_FILE_NAME_SIZE / sizeof(CHAR16))
> -
> -/**
> -  Read a file.
> -
> -  @param[in]  FileName        The file to be read.
> -  @param[out] BufferSize      The file buffer size
> -  @param[out] Buffer          The file buffer
> -
> -  @retval EFI_SUCCESS    Read file successfully
> -  @retval EFI_NOT_FOUND  File not found
> -**/
> -EFI_STATUS
> -ReadFileToBuffer (
> -  IN  CHAR16                               *FileName,
> -  OUT UINTN                                *BufferSize,
> -  OUT VOID                                 **Buffer
> -  );
> -
> -/**
> -  Write a file.
> -
> -  @param[in] FileName        The file to be written.
> -  @param[in] BufferSize      The file buffer size
> -  @param[in] Buffer          The file buffer
> -
> -  @retval EFI_SUCCESS    Write file successfully
> -**/
> -EFI_STATUS
> -WriteFileFromBuffer (
> -  IN  CHAR16                               *FileName,
> -  IN  UINTN                                BufferSize,
> -  IN  VOID                                 *Buffer
> -  );
> -
> -/**
> -  Get shell protocol.
> -
> -  @return Pointer to shell protocol.
> -
> -**/
> -EFI_SHELL_PROTOCOL *
> -GetShellProtocol (
> -  VOID
> -  );
> -
> -/**
> -  Get SimpleFileSystem from boot option file path.
> -
> -  @param[in]  DevicePath     The file path of boot option
> -  @param[out] FullPath       The full device path of boot device
> -  @param[out] Fs             The file system within EfiSysPartition
> -
> -  @retval EFI_SUCCESS    Get file system successfully
> -  @retval EFI_NOT_FOUND  No valid file system found
> -  @retval others         Get file system failed
> -
> -**/
> -EFI_STATUS
> -EFIAPI
> -GetEfiSysPartitionFromBootOptionFilePath (
> -  IN  EFI_DEVICE_PATH_PROTOCOL         *DevicePath,
> -  OUT EFI_DEVICE_PATH_PROTOCOL         **FullPath,
> -  OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  **Fs
> -  );
> +#include "CapsuleApp.h"
> 
>  /**
>    Validate if it is valid capsule header
> 
>    This function assumes the caller provided correct CapsuleHeader pointer
> @@ -707,11 +616,10 @@ SplitFileNameExtension (
>    @return <0                 Buffer1 is less than Buffer2.
>    @return >0                 Buffer1 is greater than Buffer2.
> 
>  **/
>  INTN
> -EFIAPI
>  CompareFileNameInAlphabet (
>    IN VOID                         *Left,
>    IN VOID                         *Right
>    )
>  {
> @@ -810,12 +718,12 @@ DumpCapsuleFromDisk (
>    }
> 
>    //
>    // Get file count first
>    //
> +  Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
>    do {
> -    Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
>      if (EFI_ERROR (Status) || FileInfo == NULL) {
>        Print (L"Get File Info Fail. Status = %r\n", Status);
>        goto Done;
>      }
> 
> @@ -844,12 +752,12 @@ DumpCapsuleFromDisk (
>    NoFile = FALSE;
> 
>    //
>    // Get all file info
>    //
> +  Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
>    do {
> -    Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
>      if (EFI_ERROR (Status) || FileInfo == NULL) {
>        Print (L"Get File Info Fail. Status = %r\n", Status);
>        goto Done;
>      }
> 
> diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
> b/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
> index a11683d66c..382efa9aa0 100644
> --- a/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
> +++ b/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
> @@ -3,40 +3,14 @@
> 
>    Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> -#include <Uefi.h>
> -#include <Library/BaseLib.h>
> -#include <Library/DebugLib.h>
> -#include <Library/BaseMemoryLib.h>
> -#include <Library/MemoryAllocationLib.h>
> -#include <Library/UefiBootServicesTableLib.h>
> -#include <Library/UefiRuntimeServicesTableLib.h>
> -#include <Library/UefiLib.h>
> -#include <Library/PrintLib.h>
> -#include <Library/DevicePathLib.h>
> -#include <Library/FileHandleLib.h>
> -#include <Library/UefiBootManagerLib.h>
> -#include <Protocol/SimpleFileSystem.h>
> -#include <Protocol/Shell.h>
> -#include <Guid/FileInfo.h>
> -#include <Guid/GlobalVariable.h>
> -#include <Guid/Gpt.h>
> 
> -EFI_GUID mCapsuleOnDiskBootOptionGuid = { 0x4CC29BB7, 0x2413, 0x40A2,
> { 0xB0, 0x6D, 0x25, 0x3E, 0x37, 0x10, 0xF5, 0x32 } };
> -
> -/**
> -  Get shell protocol.
> +#include "CapsuleApp.h"
> 
> -  @return Pointer to shell protocol.
> -
> -**/
> -EFI_SHELL_PROTOCOL *
> -GetShellProtocol (
> -  VOID
> -  );
> +EFI_GUID mCapsuleOnDiskBootOptionGuid = { 0x4CC29BB7, 0x2413, 0x40A2,
> { 0xB0, 0x6D, 0x25, 0x3E, 0x37, 0x10, 0xF5, 0x32 } };
> 
>  /**
>    Get file name from file path.
> 
>    @param  FilePath    File path.
> @@ -328,11 +302,10 @@ GetEfiSysPartitionFromDevPath (
>    @retval EFI_NOT_FOUND  No valid file system found
>    @retval others         Get file system failed
> 
>  **/
>  EFI_STATUS
> -EFIAPI
>  GetEfiSysPartitionFromBootOptionFilePath (
>    IN  EFI_DEVICE_PATH_PROTOCOL         *DevicePath,
>    OUT EFI_DEVICE_PATH_PROTOCOL         **FullPath,
>    OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  **Fs
>    )
> @@ -396,11 +369,10 @@ GetEfiSysPartitionFromBootOptionFilePath (
>    @retval EFI_NOT_FOUND  No valid FS found
>    @retval others         Get FS failed
> 
>  **/
>  EFI_STATUS
> -EFIAPI
>  GetUpdateFileSystem (
>    IN  CHAR16                           *Map,
>    OUT UINT16                           *BootNext,
>    OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  **Fs,
>    OUT BOOLEAN                          *UpdateBootNext
> @@ -742,10 +714,45 @@ SetCapsuleStatusVariable (
>                    );
> 
>    return Status;
>  }
> 
> +/**
> +  Check if Capsule On Disk is supported.
> +
> +  @retval TRUE              Capsule On Disk is supported.
> +  @retval FALSE             Capsule On Disk is not supported.
> +
> +**/
> +BOOLEAN
> +IsCapsuleOnDiskSupported (
> +  VOID
> +  )
> +{
> +  EFI_STATUS                    Status;
> +  UINT64                        OsIndicationsSupported;
> +  UINTN                         DataSize;
> +
> +  DataSize = sizeof(UINT64);
> +  Status = gRT->GetVariable (
> +                  L"OsIndicationsSupported",
> +                  &gEfiGlobalVariableGuid,
> +                  NULL,
> +                  &DataSize,
> +                  &OsIndicationsSupported
> +                  );
> +  if (EFI_ERROR (Status)) {
> +    return FALSE;
> +  }
> +
> +  if (OsIndicationsSupported &
> EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) {
> +    return TRUE;
> +  }
> +
> +  return FALSE;
> +}
> +
>  /**
>    Process Capsule On Disk.
> 
>    @param[in]  CapsuleBuffer       An array of pointer to capsule images
>    @param[in]  CapsuleBufferSize   An array of UINTN to capsule images size
> @@ -768,10 +775,20 @@ ProcessCapsuleOnDisk (
>  {
>    EFI_STATUS                      Status;
>    UINT16                          BootNext;
>    EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Fs;
>    BOOLEAN                         UpdateBootNext;
> +  CHAR16                          *FileName[MAX_CAPSULE_NUM];
> +  UINTN                           Index;
> +
> +  //
> +  // Check if Capsule On Disk is supported
> +  //
> +  if (!IsCapsuleOnDiskSupported ()) {
> +    Print (L"CapsuleApp: Capsule On Disk is not supported.\n");
> +    return EFI_UNSUPPORTED;
> +  }
> 
>    //
>    // Get a valid file system from boot path
>    //
>    Fs = NULL;
> @@ -780,14 +797,21 @@ ProcessCapsuleOnDisk (
>    if (EFI_ERROR (Status)) {
>      Print (L"CapsuleApp: cannot find a valid file system on boot devies. Status
> = %r\n", Status);
>      return Status;
>    }
> 
> +  //
> +  // Get file name from file path
> +  //
> +  for (Index = 0; Index < CapsuleNum; Index ++) {
> +    FileName[Index] = GetFileNameFromPath (FilePath[Index]);
> +  }
> +
>    //
>    // Copy capsule image to '\efi\UpdateCapsule\'
>    //
> -  Status = WriteUpdateFile (CapsuleBuffer, CapsuleBufferSize, FilePath,
> CapsuleNum, Fs);
> +  Status = WriteUpdateFile (CapsuleBuffer, CapsuleBufferSize, FileName,
> CapsuleNum, Fs);
>    if (EFI_ERROR (Status)) {
>      Print (L"CapsuleApp: capsule image could not be copied for update.\n");
>      return Status;
>    }
> 
> --
> 2.16.2.windows.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [edk2-devel][Patch] MdeModulePkg/CapsuleApp: Enhance Capsule-On-Disk related functions.
  2019-06-25  6:51 [edk2-devel][Patch] MdeModulePkg/CapsuleApp: Enhance Capsule-On-Disk related functions Xu, Wei6
  2019-06-26  1:45 ` Wu, Hao A
@ 2019-06-26  2:06 ` Zhang, Chao B
  1 sibling, 0 replies; 6+ messages in thread
From: Zhang, Chao B @ 2019-06-26  2:06 UTC (permalink / raw)
  To: Xu, Wei6, devel@edk2.groups.io; +Cc: Wang, Jian J, Wu, Hao A

Reviewed-by : Chao Zhang <chao.b.zhang@intel.com>

-----Original Message-----
From: Xu, Wei6 
Sent: Tuesday, June 25, 2019 2:51 PM
To: devel@edk2.groups.io
Cc: Wang, Jian J <jian.j.wang@intel.com>; Wu, Hao A <hao.a.wu@intel.com>; Zhang, Chao B <chao.b.zhang@intel.com>
Subject: [edk2-devel][Patch] MdeModulePkg/CapsuleApp: Enhance Capsule-On-Disk related functions.

BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=1840

1. Introduce an internal header file to put definitions in it.
2. Add missing '\n' in usage.
3. Fix the dead loop of CapsuleApp -L.
4. Fix the bug that CapsuleApp -OD cannot perform capsules in sub- folder.
5. Optimize the handling for option -NR and -OD to support both 'CapsuleApp <Capsule> -OD -NR' and 'CapsuleApp <Capsule> -NR -OD'.
6. Check if Capsule-On-Disk is supported by "OsIndicationsSupported"
variable firstly before processing capsules. If not supported, prompt an error message and quit the process.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Chao B Zhang <chao.b.zhang@intel.com>
Signed-off-by: Wei6 Xu <wei6.xu@intel.com>
---
 MdeModulePkg/Application/CapsuleApp/AppSupport.c   |  13 +-
 MdeModulePkg/Application/CapsuleApp/CapsuleApp.c   | 215 ++----------------
 MdeModulePkg/Application/CapsuleApp/CapsuleApp.h   | 240 +++++++++++++++++++++
 MdeModulePkg/Application/CapsuleApp/CapsuleApp.inf |   3 +-
 MdeModulePkg/Application/CapsuleApp/CapsuleDump.c  |  98 +--------
 .../Application/CapsuleApp/CapsuleOnDisk.c         |  86 +++++---
 6 files changed, 323 insertions(+), 332 deletions(-)  create mode 100644 MdeModulePkg/Application/CapsuleApp/CapsuleApp.h

diff --git a/MdeModulePkg/Application/CapsuleApp/AppSupport.c b/MdeModulePkg/Application/CapsuleApp/AppSupport.c
index d9ce1b4843..8fe70dc3b6 100644
--- a/MdeModulePkg/Application/CapsuleApp/AppSupport.c
+++ b/MdeModulePkg/Application/CapsuleApp/AppSupport.c
@@ -1,23 +1,14 @@
 /** @file
   A shell application that triggers capsule update process.
 
-  Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2019, Intel Corporation. All rights 
+ reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
-#include <Uefi.h>
-#include <Library/BaseLib.h>
-#include <Library/DebugLib.h>
-#include <Library/BaseMemoryLib.h>
-#include <Library/MemoryAllocationLib.h> -#include <Library/UefiBootServicesTableLib.h>
-#include <Protocol/SimpleFileSystem.h>
-#include <Protocol/ShellParameters.h>
-#include <Protocol/Shell.h>
-#include <Guid/FileInfo.h>
+#include "CapsuleApp.h"
 
 UINTN  Argc;
 CHAR16 **Argv;
 EFI_SHELL_PROTOCOL      *mShellProtocol = NULL;
 
diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
index e3c591dbf3..3439ce5feb 100644
--- a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
+++ b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.c
@@ -4,191 +4,19 @@
   Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
-#include <Uefi.h>
-#include <Library/BaseLib.h>
-#include <Library/DebugLib.h>
-#include <Library/BaseMemoryLib.h>
-#include <Library/MemoryAllocationLib.h> -#include <Library/UefiBootServicesTableLib.h>
-#include <Library/UefiRuntimeServicesTableLib.h>
-#include <Library/UefiLib.h>
-#include <Library/PrintLib.h>
-#include <Library/BmpSupportLib.h>
-#include <Protocol/GraphicsOutput.h>
-#include <Guid/GlobalVariable.h>
-#include <Guid/CapsuleReport.h>
-#include <Guid/SystemResourceTable.h>
-#include <Guid/FmpCapsule.h>
-#include <IndustryStandard/WindowsUxCapsule.h>
-
-#define CAPSULE_HEADER_SIZE  0x20
-
-#define NESTED_CAPSULE_HEADER_SIZE  SIZE_4KB -#define SYSTEM_FIRMWARE_FLAG 0x50000 -#define DEVICE_FIRMWARE_FLAG 0x78010
-
-#define MAJOR_VERSION   1
-#define MINOR_VERSION   0
-
-#define MAX_CAPSULE_NUM 10
-
-extern UINTN  Argc;
-extern CHAR16 **Argv;
+#include "CapsuleApp.h"
 
 //
 // Define how many block descriptors we want to test with.
 //
 UINTN  NumberOfDescriptors = 1;
 UINTN  CapsuleFirstIndex;
 UINTN  CapsuleLastIndex;
 
-/**
-  Dump capsule information
-
-  @param[in] CapsuleName  The name of the capsule image.
-
-  @retval EFI_SUCCESS            The capsule information is dumped.
-  @retval EFI_UNSUPPORTED        Input parameter is not valid.
-**/
-EFI_STATUS
-DumpCapsule (
-  IN CHAR16                                        *CapsuleName
-  );
-
-/**
-  Dump capsule status variable.
-
-  @retval EFI_SUCCESS            The capsule status variable is dumped.
-  @retval EFI_UNSUPPORTED        Input parameter is not valid.
-**/
-EFI_STATUS
-DumpCapsuleStatusVariable (
-  VOID
-  );
-
-/**
-  Dump FMP protocol info.
-**/
-VOID
-DumpFmpData (
-  VOID
-  );
-
-/**
-  Dump FMP image data.
-
-  @param[in]  ImageTypeId   The ImageTypeId of the FMP image.
-                            It is used to identify the FMP protocol.
-  @param[in]  ImageIndex    The ImageIndex of the FMP image.
-                            It is the input parameter for FMP->GetImage().
-  @param[in]  ImageName     The file name to hold the output FMP image.
-**/
-VOID
-DumpFmpImage (
-  IN EFI_GUID  *ImageTypeId,
-  IN UINTN     ImageIndex,
-  IN CHAR16    *ImageName
-  );
-
-/**
-  Dump ESRT info.
-**/
-VOID
-DumpEsrtData (
-  VOID
-  );
-
-/**
-  Dump Provisioned Capsule.
-
-  @param[in]  DumpCapsuleInfo  The flag to indicate whether to dump the capsule inforomation.
-**/
-VOID
-DumpProvisionedCapsule (
-  IN BOOLEAN                      DumpCapsuleInfo
-  );
-
-/**
-  Dump all EFI System Partition.
-**/
-VOID
-DumpAllEfiSysPartition (
-  VOID
-  );
-
-/**
-  Process Capsule On Disk.
-
-  @param[in]  CapsuleBuffer       An array of pointer to capsule images
-  @param[in]  CapsuleBufferSize   An array of UINTN to capsule images size
-  @param[in]  FilePath            An array of capsule images file path
-  @param[in]  Map                 File system mapping string
-  @param[in]  CapsuleNum          The count of capsule images
-
-  @retval EFI_SUCCESS       Capsule on disk success.
-  @retval others            Capsule on disk fail.
-
-**/
-EFI_STATUS
-ProcessCapsuleOnDisk (
-  IN VOID                          **CapsuleBuffer,
-  IN UINTN                         *CapsuleBufferSize,
-  IN CHAR16                        **FilePath,
-  IN CHAR16                        *Map,
-  IN UINTN                         CapsuleNum
-  );
-
-/**
-  Read a file.
-
-  @param[in]  FileName        The file to be read.
-  @param[out] BufferSize      The file buffer size
-  @param[out] Buffer          The file buffer
-
-  @retval EFI_SUCCESS    Read file successfully
-  @retval EFI_NOT_FOUND  Shell protocol or file not found
-  @retval others         Read file failed
-**/
-EFI_STATUS
-ReadFileToBuffer (
-  IN  CHAR16                               *FileName,
-  OUT UINTN                                *BufferSize,
-  OUT VOID                                 **Buffer
-  );
-
-/**
-  Write a file.
-
-  @param[in] FileName        The file to be written.
-  @param[in] BufferSize      The file buffer size
-  @param[in] Buffer          The file buffer
-
-  @retval EFI_SUCCESS    Write file successfully
-  @retval EFI_NOT_FOUND  Shell protocol not found
-  @retval others         Write file failed
-**/
-EFI_STATUS
-WriteFileFromBuffer (
-  IN  CHAR16                               *FileName,
-  IN  UINTN                                BufferSize,
-  IN  VOID                                 *Buffer
-  );
-
-/**
-
-  This function parse application ARG.
-
-  @return Status
-**/
-EFI_STATUS
-GetArg (
-  VOID
-  );
-
 /**
   Create UX capsule.
 
   @retval EFI_SUCCESS            The capsule header is appended.
   @retval EFI_UNSUPPORTED        Input parameter is not valid.
@@ -847,11 +675,11 @@ PrintUsage (
   Print(L"  CapsuleApp -D <Capsule>\n");
   Print(L"  CapsuleApp -P GET <ImageTypeId> <Index> -O <FileName>\n");
   Print(L"Parameter:\n");
   Print(L"  -NR: No reset will be triggered for the capsule\n");
   Print(L"       with CAPSULE_FLAGS_PERSIST_ACROSS_RESET and without CAPSULE_FLAGS_INITIATE_RESET.\n");
-  Print(L"  -OD: Delivery of Capsules via file on Mass Storage device.");
+  Print(L"  -OD: Delivery of Capsules via file on Mass Storage 
+ device.\n");
   Print(L"  -S:  Dump capsule report variable (EFI_CAPSULE_REPORT_GUID),\n");
   Print(L"       which is defined in UEFI specification.\n");
   Print(L"  -C:  Clear capsule report variable (EFI_CAPSULE_REPORT_GUID),\n");
   Print(L"       which is defined in UEFI specification.\n");
   Print(L"  -P:  Dump UEFI FMP protocol info, or get image with specified\n"); @@ -1018,44 +846,43 @@ UefiMain (
       ParaNrIndex = Index;
       NoReset = TRUE;
     }
   }
 
-  if (ParaOdIndex != 0) {
-    if (ParaOdIndex == Argc - 1) {
+  if (ParaOdIndex > ParaNrIndex) {
+    if (ParaNrIndex != 0) {
+      CapsuleLastIndex = ParaNrIndex - 1;
+    } else {
+      CapsuleLastIndex = ParaOdIndex - 1;
+    }
+
+    if (ParaOdIndex == Argc -1) {
       MapFsStr = NULL;
     } else if (ParaOdIndex == Argc - 2) {
       MapFsStr = Argv[Argc-1];
     } else {
-      Print (L"CapsuleApp: Invalid Position for -OD Options\n");
+      Print (L"CapsuleApp: Cannot specify more than one FS 
+ mapping!\n");
       Status = EFI_INVALID_PARAMETER;
       goto Done;
     }
-
-    if (ParaNrIndex != 0) {
-      if (ParaNrIndex + 1 == ParaOdIndex) {
-        CapsuleLastIndex = ParaNrIndex - 1;
-      } else {
-        Print (L"CapsuleApp: Invalid Position for -NR Options\n");
-        Status = EFI_INVALID_PARAMETER;
-        goto Done;
-      }
-    } else {
+  } else if (ParaOdIndex < ParaNrIndex) {
+    if (ParaOdIndex != 0) {
       CapsuleLastIndex = ParaOdIndex - 1;
-    }
-  } else {
-    if (ParaNrIndex != 0) {
-      if (ParaNrIndex == Argc -1) {
-        CapsuleLastIndex = ParaNrIndex - 1;
+      if (ParaOdIndex == ParaNrIndex - 1) {
+        MapFsStr = NULL;
+      } else if (ParaOdIndex == ParaNrIndex - 2) {
+        MapFsStr = Argv[ParaOdIndex + 1];
       } else {
-        Print (L"CapsuleApp: Invalid Position for -NR Options\n");
+        Print (L"CapsuleApp: Cannot specify more than one FS 
+ mapping!\n");
         Status = EFI_INVALID_PARAMETER;
         goto Done;
       }
     } else {
-      CapsuleLastIndex = Argc - 1;
+      CapsuleLastIndex = ParaNrIndex - 1;
     }
+  } else {
+    CapsuleLastIndex = Argc - 1;
   }
 
   CapsuleNum = CapsuleLastIndex - CapsuleFirstIndex + 1;
 
   if (CapsuleFirstIndex > CapsuleLastIndex) { diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.h b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.h
new file mode 100644
index 0000000000..270d2359a3
--- /dev/null
+++ b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.h
@@ -0,0 +1,240 @@
+/** @file
+  A shell application that triggers capsule update process.
+
+  Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+
+#ifndef _CAPSULE_APP_H_
+#define _CAPSULE_APP_H_
+
+#include <Uefi.h>
+#include <Library/BaseLib.h>
+#include <Library/DebugLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/MemoryAllocationLib.h> #include 
+<Library/UefiBootServicesTableLib.h>
+#include <Library/UefiRuntimeServicesTableLib.h>
+#include <Library/UefiLib.h>
+#include <Library/PrintLib.h>
+#include <Library/BmpSupportLib.h>
+#include <Library/FileHandleLib.h>
+#include <Library/SortLib.h>
+#include <Library/UefiBootManagerLib.h> #include 
+<Library/DevicePathLib.h> #include <Protocol/GraphicsOutput.h> #include 
+<Protocol/SimpleFileSystem.h> #include <Protocol/ShellParameters.h> 
+#include <Protocol/Shell.h> #include <Protocol/FirmwareManagement.h> 
+#include <Guid/GlobalVariable.h> #include <Guid/CapsuleReport.h> 
+#include <Guid/SystemResourceTable.h> #include <Guid/FmpCapsule.h> 
+#include <Guid/FileInfo.h> #include <Guid/ImageAuthentication.h> 
+#include <Guid/CapsuleVendor.h> #include <Guid/Gpt.h> #include 
+<IndustryStandard/WindowsUxCapsule.h>
+
+#define CAPSULE_HEADER_SIZE  0x20
+
+#define NESTED_CAPSULE_HEADER_SIZE  SIZE_4KB #define 
+SYSTEM_FIRMWARE_FLAG 0x50000 #define DEVICE_FIRMWARE_FLAG 0x78010
+
+#define MAJOR_VERSION   1
+#define MINOR_VERSION   0
+
+#define MAX_CAPSULE_NUM 10
+
+//
+// (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for 
+bytes) //
+#define MAX_FILE_NAME_SIZE   522
+#define MAX_FILE_NAME_LEN    (MAX_FILE_NAME_SIZE / sizeof(CHAR16))
+
+extern UINTN  Argc;
+extern CHAR16 **Argv;
+
+/**
+
+  This function parse application ARG.
+
+  @return Status
+**/
+EFI_STATUS
+GetArg (
+  VOID
+  );
+
+/**
+  Get shell protocol.
+
+  @return Pointer to shell protocol.
+
+**/
+EFI_SHELL_PROTOCOL *
+GetShellProtocol (
+  VOID
+  );
+
+
+/**
+  Read a file.
+
+  @param[in]  FileName        The file to be read.
+  @param[out] BufferSize      The file buffer size
+  @param[out] Buffer          The file buffer
+
+  @retval EFI_SUCCESS    Read file successfully
+  @retval EFI_NOT_FOUND  Shell protocol or file not found
+  @retval others         Read file failed
+**/
+EFI_STATUS
+ReadFileToBuffer (
+  IN  CHAR16                               *FileName,
+  OUT UINTN                                *BufferSize,
+  OUT VOID                                 **Buffer
+  );
+
+/**
+  Write a file.
+
+  @param[in] FileName        The file to be written.
+  @param[in] BufferSize      The file buffer size
+  @param[in] Buffer          The file buffer
+
+  @retval EFI_SUCCESS    Write file successfully
+  @retval EFI_NOT_FOUND  Shell protocol not found
+  @retval others         Write file failed
+**/
+EFI_STATUS
+WriteFileFromBuffer (
+  IN  CHAR16                               *FileName,
+  IN  UINTN                                BufferSize,
+  IN  VOID                                 *Buffer
+  );
+
+
+/**
+  Dump capsule information
+
+  @param[in] CapsuleName  The name of the capsule image.
+
+  @retval EFI_SUCCESS            The capsule information is dumped.
+  @retval EFI_UNSUPPORTED        Input parameter is not valid.
+**/
+EFI_STATUS
+DumpCapsule (
+  IN CHAR16                                        *CapsuleName
+  );
+
+/**
+  Dump capsule status variable.
+
+  @retval EFI_SUCCESS            The capsule status variable is dumped.
+  @retval EFI_UNSUPPORTED        Input parameter is not valid.
+**/
+EFI_STATUS
+DumpCapsuleStatusVariable (
+  VOID
+  );
+
+/**
+  Dump FMP protocol info.
+**/
+VOID
+DumpFmpData (
+  VOID
+  );
+
+/**
+  Dump FMP image data.
+
+  @param[in]  ImageTypeId   The ImageTypeId of the FMP image.
+                            It is used to identify the FMP protocol.
+  @param[in]  ImageIndex    The ImageIndex of the FMP image.
+                            It is the input parameter for FMP->GetImage().
+  @param[in]  ImageName     The file name to hold the output FMP image.
+**/
+VOID
+DumpFmpImage (
+  IN EFI_GUID  *ImageTypeId,
+  IN UINTN     ImageIndex,
+  IN CHAR16    *ImageName
+  );
+
+/**
+  Dump ESRT info.
+**/
+VOID
+DumpEsrtData (
+  VOID
+  );
+
+/**
+  Dump Provisioned Capsule.
+
+  @param[in]  DumpCapsuleInfo  The flag to indicate whether to dump the capsule inforomation.
+**/
+VOID
+DumpProvisionedCapsule (
+  IN BOOLEAN                      DumpCapsuleInfo
+  );
+
+/**
+  Dump all EFI System Partition.
+**/
+VOID
+DumpAllEfiSysPartition (
+  VOID
+  );
+
+
+/**
+  Get SimpleFileSystem from boot option file path.
+
+  @param[in]  DevicePath     The file path of boot option
+  @param[out] FullPath       The full device path of boot device
+  @param[out] Fs             The file system within EfiSysPartition
+
+  @retval EFI_SUCCESS    Get file system successfully
+  @retval EFI_NOT_FOUND  No valid file system found
+  @retval others         Get file system failed
+
+**/
+EFI_STATUS
+GetEfiSysPartitionFromBootOptionFilePath (
+  IN  EFI_DEVICE_PATH_PROTOCOL         *DevicePath,
+  OUT EFI_DEVICE_PATH_PROTOCOL         **FullPath,
+  OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  **Fs
+  );
+
+
+/**
+  Process Capsule On Disk.
+
+  @param[in]  CapsuleBuffer       An array of pointer to capsule images
+  @param[in]  CapsuleBufferSize   An array of UINTN to capsule images size
+  @param[in]  FilePath            An array of capsule images file path
+  @param[in]  Map                 File system mapping string
+  @param[in]  CapsuleNum          The count of capsule images
+
+  @retval EFI_SUCCESS       Capsule on disk success.
+  @retval others            Capsule on disk fail.
+
+**/
+EFI_STATUS
+ProcessCapsuleOnDisk (
+  IN VOID                          **CapsuleBuffer,
+  IN UINTN                         *CapsuleBufferSize,
+  IN CHAR16                        **FilePath,
+  IN CHAR16                        *Map,
+  IN UINTN                         CapsuleNum
+  );
+
+#endif
+
diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.inf b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.inf
index 8ca5e04104..6ed0659835 100644
--- a/MdeModulePkg/Application/CapsuleApp/CapsuleApp.inf
+++ b/MdeModulePkg/Application/CapsuleApp/CapsuleApp.inf
@@ -2,11 +2,11 @@
 #  A shell application that triggers capsule update process.
 #
 # This application can trigger capsule update process. It can also  # generate capsule image, or dump capsule variable information.
 #
-#  Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2016 - 2019, Intel Corporation. All rights 
+reserved.<BR>
 #  SPDX-License-Identifier: BSD-2-Clause-Patent  #  ##
 
 [Defines]
@@ -24,10 +24,11 @@
 #  VALID_ARCHITECTURES           = IA32 X64
 #
 
 [Sources]
   CapsuleApp.c
+  CapsuleApp.h
   CapsuleDump.c
   CapsuleOnDisk.c
   AppSupport.c
 
 [Packages]
diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
index b81c5b7b3a..58a93568d0 100644
--- a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
+++ b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
@@ -4,102 +4,11 @@
   Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
-#include <PiDxe.h>
-#include <Library/BaseLib.h>
-#include <Library/DebugLib.h>
-#include <Library/BaseMemoryLib.h>
-#include <Library/MemoryAllocationLib.h> -#include <Library/UefiBootServicesTableLib.h>
-#include <Library/UefiRuntimeServicesTableLib.h>
-#include <Library/UefiLib.h>
-#include <Library/PrintLib.h>
-#include <Library/FileHandleLib.h>
-#include <Library/SortLib.h>
-#include <Library/UefiBootManagerLib.h> -#include <Library/DevicePathLib.h> -#include <Protocol/FirmwareManagement.h> -#include <Protocol/SimpleFileSystem.h> -#include <Protocol/Shell.h> -#include <Guid/ImageAuthentication.h> -#include <Guid/CapsuleReport.h> -#include <Guid/SystemResourceTable.h> -#include <Guid/FmpCapsule.h> -#include <Guid/CapsuleVendor.h> -#include <IndustryStandard/WindowsUxCapsule.h>
-
-//
-// (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes) -//
-#define MAX_FILE_NAME_SIZE   522
-#define MAX_FILE_NAME_LEN    (MAX_FILE_NAME_SIZE / sizeof(CHAR16))
-
-/**
-  Read a file.
-
-  @param[in]  FileName        The file to be read.
-  @param[out] BufferSize      The file buffer size
-  @param[out] Buffer          The file buffer
-
-  @retval EFI_SUCCESS    Read file successfully
-  @retval EFI_NOT_FOUND  File not found -**/ -EFI_STATUS -ReadFileToBuffer (
-  IN  CHAR16                               *FileName,
-  OUT UINTN                                *BufferSize,
-  OUT VOID                                 **Buffer
-  );
-
-/**
-  Write a file.
-
-  @param[in] FileName        The file to be written.
-  @param[in] BufferSize      The file buffer size
-  @param[in] Buffer          The file buffer
-
-  @retval EFI_SUCCESS    Write file successfully
-**/
-EFI_STATUS
-WriteFileFromBuffer (
-  IN  CHAR16                               *FileName,
-  IN  UINTN                                BufferSize,
-  IN  VOID                                 *Buffer
-  );
-
-/**
-  Get shell protocol.
-
-  @return Pointer to shell protocol.
-
-**/
-EFI_SHELL_PROTOCOL *
-GetShellProtocol (
-  VOID
-  );
-
-/**
-  Get SimpleFileSystem from boot option file path.
-
-  @param[in]  DevicePath     The file path of boot option
-  @param[out] FullPath       The full device path of boot device
-  @param[out] Fs             The file system within EfiSysPartition
-
-  @retval EFI_SUCCESS    Get file system successfully
-  @retval EFI_NOT_FOUND  No valid file system found
-  @retval others         Get file system failed
-
-**/
-EFI_STATUS
-EFIAPI
-GetEfiSysPartitionFromBootOptionFilePath (
-  IN  EFI_DEVICE_PATH_PROTOCOL         *DevicePath,
-  OUT EFI_DEVICE_PATH_PROTOCOL         **FullPath,
-  OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  **Fs
-  );
+#include "CapsuleApp.h"
 
 /**
   Validate if it is valid capsule header
 
   This function assumes the caller provided correct CapsuleHeader pointer @@ -707,11 +616,10 @@ SplitFileNameExtension (
   @return <0                 Buffer1 is less than Buffer2.
   @return >0                 Buffer1 is greater than Buffer2.
 
 **/
 INTN
-EFIAPI
 CompareFileNameInAlphabet (
   IN VOID                         *Left,
   IN VOID                         *Right
   )
 {
@@ -810,12 +718,12 @@ DumpCapsuleFromDisk (
   }
 
   //
   // Get file count first
   //
+  Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
   do {
-    Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
     if (EFI_ERROR (Status) || FileInfo == NULL) {
       Print (L"Get File Info Fail. Status = %r\n", Status);
       goto Done;
     }
 
@@ -844,12 +752,12 @@ DumpCapsuleFromDisk (
   NoFile = FALSE;
 
   //
   // Get all file info
   //
+  Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
   do {
-    Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
     if (EFI_ERROR (Status) || FileInfo == NULL) {
       Print (L"Get File Info Fail. Status = %r\n", Status);
       goto Done;
     }
 
diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c b/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
index a11683d66c..382efa9aa0 100644
--- a/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
+++ b/MdeModulePkg/Application/CapsuleApp/CapsuleOnDisk.c
@@ -3,40 +3,14 @@
 
   Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
-#include <Uefi.h>
-#include <Library/BaseLib.h>
-#include <Library/DebugLib.h>
-#include <Library/BaseMemoryLib.h>
-#include <Library/MemoryAllocationLib.h> -#include <Library/UefiBootServicesTableLib.h>
-#include <Library/UefiRuntimeServicesTableLib.h>
-#include <Library/UefiLib.h>
-#include <Library/PrintLib.h>
-#include <Library/DevicePathLib.h>
-#include <Library/FileHandleLib.h>
-#include <Library/UefiBootManagerLib.h> -#include <Protocol/SimpleFileSystem.h> -#include <Protocol/Shell.h> -#include <Guid/FileInfo.h> -#include <Guid/GlobalVariable.h> -#include <Guid/Gpt.h>
 
-EFI_GUID mCapsuleOnDiskBootOptionGuid = { 0x4CC29BB7, 0x2413, 0x40A2, { 0xB0, 0x6D, 0x25, 0x3E, 0x37, 0x10, 0xF5, 0x32 } };
-
-/**
-  Get shell protocol.
+#include "CapsuleApp.h"
 
-  @return Pointer to shell protocol.
-
-**/
-EFI_SHELL_PROTOCOL *
-GetShellProtocol (
-  VOID
-  );
+EFI_GUID mCapsuleOnDiskBootOptionGuid = { 0x4CC29BB7, 0x2413, 0x40A2, { 
+0xB0, 0x6D, 0x25, 0x3E, 0x37, 0x10, 0xF5, 0x32 } };
 
 /**
   Get file name from file path.
 
   @param  FilePath    File path.
@@ -328,11 +302,10 @@ GetEfiSysPartitionFromDevPath (
   @retval EFI_NOT_FOUND  No valid file system found
   @retval others         Get file system failed
 
 **/
 EFI_STATUS
-EFIAPI
 GetEfiSysPartitionFromBootOptionFilePath (
   IN  EFI_DEVICE_PATH_PROTOCOL         *DevicePath,
   OUT EFI_DEVICE_PATH_PROTOCOL         **FullPath,
   OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  **Fs
   )
@@ -396,11 +369,10 @@ GetEfiSysPartitionFromBootOptionFilePath (
   @retval EFI_NOT_FOUND  No valid FS found
   @retval others         Get FS failed
 
 **/
 EFI_STATUS
-EFIAPI
 GetUpdateFileSystem (
   IN  CHAR16                           *Map,
   OUT UINT16                           *BootNext,
   OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  **Fs,
   OUT BOOLEAN                          *UpdateBootNext
@@ -742,10 +714,45 @@ SetCapsuleStatusVariable (
                   );
 
   return Status;
 }
 
+/**
+  Check if Capsule On Disk is supported.
+
+  @retval TRUE              Capsule On Disk is supported.
+  @retval FALSE             Capsule On Disk is not supported.
+
+**/
+BOOLEAN
+IsCapsuleOnDiskSupported (
+  VOID
+  )
+{
+  EFI_STATUS                    Status;
+  UINT64                        OsIndicationsSupported;
+  UINTN                         DataSize;
+
+  DataSize = sizeof(UINT64);
+  Status = gRT->GetVariable (
+                  L"OsIndicationsSupported",
+                  &gEfiGlobalVariableGuid,
+                  NULL,
+                  &DataSize,
+                  &OsIndicationsSupported
+                  );
+  if (EFI_ERROR (Status)) {
+    return FALSE;
+  }
+
+  if (OsIndicationsSupported & EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) {
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
 /**
   Process Capsule On Disk.
 
   @param[in]  CapsuleBuffer       An array of pointer to capsule images
   @param[in]  CapsuleBufferSize   An array of UINTN to capsule images size
@@ -768,10 +775,20 @@ ProcessCapsuleOnDisk (  {
   EFI_STATUS                      Status;
   UINT16                          BootNext;
   EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Fs;
   BOOLEAN                         UpdateBootNext;
+  CHAR16                          *FileName[MAX_CAPSULE_NUM];
+  UINTN                           Index;
+
+  //
+  // Check if Capsule On Disk is supported  //  if 
+ (!IsCapsuleOnDiskSupported ()) {
+    Print (L"CapsuleApp: Capsule On Disk is not supported.\n");
+    return EFI_UNSUPPORTED;
+  }
 
   //
   // Get a valid file system from boot path
   //
   Fs = NULL;
@@ -780,14 +797,21 @@ ProcessCapsuleOnDisk (
   if (EFI_ERROR (Status)) {
     Print (L"CapsuleApp: cannot find a valid file system on boot devies. Status = %r\n", Status);
     return Status;
   }
 
+  //
+  // Get file name from file path
+  //
+  for (Index = 0; Index < CapsuleNum; Index ++) {
+    FileName[Index] = GetFileNameFromPath (FilePath[Index]);  }
+
   //
   // Copy capsule image to '\efi\UpdateCapsule\'
   //
-  Status = WriteUpdateFile (CapsuleBuffer, CapsuleBufferSize, FilePath, CapsuleNum, Fs);
+  Status = WriteUpdateFile (CapsuleBuffer, CapsuleBufferSize, FileName, 
+ CapsuleNum, Fs);
   if (EFI_ERROR (Status)) {
     Print (L"CapsuleApp: capsule image could not be copied for update.\n");
     return Status;
   }
 
--
2.16.2.windows.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-06-26  2:06 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-06-25  6:51 [edk2-devel][Patch] MdeModulePkg/CapsuleApp: Enhance Capsule-On-Disk related functions Xu, Wei6
2019-06-26  1:45 ` Wu, Hao A
2019-06-26  2:06 ` Zhang, Chao B
  -- strict thread matches above, loose matches on Subject: below --
2019-05-24  9:02 Xu, Wei6
2019-05-28  0:34 ` Zhang, Chao B
2019-05-28  0:53 ` Wu, Hao A

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox