[edk2-devel] [PATCH v2 5/6] ShellPkg/AcpiView: Refactor DumpAcpiTableToFile

Tomas Pilar (tpilar) posted 6 patches 7 weeks ago

[edk2-devel] [PATCH v2 5/6] ShellPkg/AcpiView: Refactor DumpAcpiTableToFile

Posted by Tomas Pilar (tpilar) 7 weeks ago
Method is refactored into two parts. A new method is
created that dumps arbitrary buffers into a newly created
file. This method is called from core code after the core code
determined the appropriate filename to be used.

This improves the modular design.

Cc: Ray Ni <ray.ni@intel.com>
Cc: Zhichao Gao <zhichao.gao@intel.com>
Signed-off-by: Tomas Pilar <tomas.pilar@arm.com>
---
 ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.c                    | 35 +----------------------------------
 ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLib.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLib.h | 17 +++++++++++++++++
 3 files changed, 76 insertions(+), 34 deletions(-)

diff --git a/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.c b/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.c
index a3160ed6f0a2..e866b84c6844 100644
--- a/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.c
+++ b/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.c
@@ -48,13 +48,10 @@ DumpAcpiTableToFile (
   IN CONST UINTN   Length
   )
 {
-  EFI_STATUS          Status;
   CHAR16              FileNameBuffer[MAX_FILE_NAME_LEN];
-  SHELL_FILE_HANDLE   DumpFileHandle;
   UINTN               TransferBytes;
   SELECTED_ACPI_TABLE SelectedTable;
 
-  DumpFileHandle = NULL;
   TransferBytes = Length;
   GetSelectedAcpiTable (&SelectedTable);
 
@@ -66,39 +63,9 @@ DumpAcpiTableToFile (
     mBinTableCount++
     );
 
-  Status = ShellOpenFileByName (
-             FileNameBuffer,
-             &DumpFileHandle,
-             EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
-             0
-             );
-  if (EFI_ERROR (Status)) {
-    ShellPrintHiiEx (
-      -1,
-      -1,
-      NULL,
-      STRING_TOKEN (STR_GEN_READONLY_MEDIA),
-      gShellAcpiViewHiiHandle,
-      L"acpiview"
-      );
-    return FALSE;
-  }
-
   Print (L"Dumping ACPI table to : %s ... ", FileNameBuffer);
 
-  Status = ShellWriteFile (
-             DumpFileHandle,
-             &TransferBytes,
-             (VOID*)Ptr
-             );
-  if (EFI_ERROR (Status)) {
-    Print (L"ERROR: Failed to dump table to binary file.\n");
-    TransferBytes = 0;
-  } else {
-    Print (L"DONE.\n");
-  }
-
-  ShellCloseFile (&DumpFileHandle);
+  TransferBytes = DumpFile (FileNameBuffer, Ptr, Length);
   return (Length == TransferBytes);
 }
 
diff --git a/ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLib.c b/ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLib.c
index c3942ad24e5b..adf3cce6cfe2 100644
--- a/ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLib.c
+++ b/ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLib.c
@@ -98,6 +98,64 @@ RegisterAllParsers (
   return Status;
 }
 
+/**
+  Dump a buffer to a file
+
+  @param[in] FileName   The filename that shall be created to contain the buffer
+  @param[in] Buffer     Pointer to buffer that shall be dumped
+  @param[in] BufferSize The size of buffer to be dumped in bytes
+
+  @return The number of bytes that were written
+**/
+UINTN
+EFIAPI
+DumpFile (
+  IN CONST CHAR16* FileNameBuffer,
+  IN CONST VOID*   Buffer,
+  IN CONST UINTN   BufferSize
+  )
+{
+  EFI_STATUS          Status;
+  SHELL_FILE_HANDLE   DumpFileHandle;
+  UINTN               TransferBytes;
+
+  Status = ShellOpenFileByName (
+             FileNameBuffer,
+             &DumpFileHandle,
+             EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
+             0
+             );
+
+  if (EFI_ERROR (Status)) {
+    ShellPrintHiiEx (
+      -1,
+      -1,
+      NULL,
+      STRING_TOKEN (STR_GEN_READONLY_MEDIA),
+      gShellAcpiViewHiiHandle,
+      L"acpiview"
+      );
+    return 0;
+  }
+
+  TransferBytes = BufferSize;
+  Status = ShellWriteFile (
+             DumpFileHandle,
+             &TransferBytes,
+             (VOID *) Buffer
+             );
+
+  if (EFI_ERROR (Status)) {
+    Print (L"ERROR: Failed to write binary file.\n");
+    TransferBytes = 0;
+  } else {
+    Print (L"DONE.\n");
+  }
+
+  ShellCloseFile (&DumpFileHandle);
+  return TransferBytes;
+}
+
 /**
   Return the file name of the help text file if not using HII.
 
diff --git a/ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLib.h b/ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLib.h
index a3a29164004d..9dd39cf4b3f0 100644
--- a/ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLib.h
+++ b/ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLib.h
@@ -10,6 +10,23 @@
 
 extern EFI_HII_HANDLE gShellAcpiViewHiiHandle;
 
+/**
+  Dump a buffer to a file
+
+  @param[in] FileName   The filename that shall be created to contain the buffer
+  @param[in] Buffer     Pointer to buffer that shall be dumped
+  @param[in] BufferSize The size of buffer to be dumped in bytes
+
+  @return The number of bytes that were written
+**/
+UINTN
+EFIAPI
+DumpFile (
+  IN CONST CHAR16* FileNameBuffer,
+  IN CONST VOID*   Buffer,
+  IN CONST UINTN   BufferSize
+  );
+
 /**
   Function for 'acpiview' command.
 
-- 
2.24.1.windows.2



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#60064): https://edk2.groups.io/g/devel/message/60064
Mute This Topic: https://groups.io/mt/74382417/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub  [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-

Re: [edk2-devel] [PATCH v2 5/6] ShellPkg/AcpiView: Refactor DumpAcpiTableToFile

Posted by Gao, Zhichao 4 weeks ago
See below.

> -----Original Message-----
> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Tomas Pilar
> (tpilar)
> Sent: Friday, May 22, 2020 2:44 AM
> To: devel@edk2.groups.io
> Cc: nd@arm.com; Ni, Ray <ray.ni@intel.com>; Gao, Zhichao
> <zhichao.gao@intel.com>
> Subject: [edk2-devel] [PATCH v2 5/6] ShellPkg/AcpiView: Refactor
> DumpAcpiTableToFile
> 
> Method is refactored into two parts. A new method is created that dumps
> arbitrary buffers into a newly created file. This method is called from core code
> after the core code determined the appropriate filename to be used.
> 
> This improves the modular design.
> 
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Zhichao Gao <zhichao.gao@intel.com>
> Signed-off-by: Tomas Pilar <tomas.pilar@arm.com>
> ---
>  ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.c                    | 35 +------
> ----------------------------
> 
> ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLib.c
> | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 
> ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLib.
> h | 17 +++++++++++++++++
>  3 files changed, 76 insertions(+), 34 deletions(-)
> 
> diff --git a/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.c
> b/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.c
> index a3160ed6f0a2..e866b84c6844 100644
> --- a/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.c
> +++ b/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.c
> @@ -48,13 +48,10 @@ DumpAcpiTableToFile (
>    IN CONST UINTN   Length
>    )
>  {
> -  EFI_STATUS          Status;
>    CHAR16              FileNameBuffer[MAX_FILE_NAME_LEN];
> -  SHELL_FILE_HANDLE   DumpFileHandle;
>    UINTN               TransferBytes;
>    SELECTED_ACPI_TABLE SelectedTable;
> 
> -  DumpFileHandle = NULL;
>    TransferBytes = Length;

I think the assignment of TransferBytes can be removed.
Without this, Reviewed-by: Zhichao Gao <zhichao.gao@intel.com>

Thanks,
Zhichao

>    GetSelectedAcpiTable (&SelectedTable);
> 
> @@ -66,39 +63,9 @@ DumpAcpiTableToFile (
>      mBinTableCount++
>      );
> 
> -  Status = ShellOpenFileByName (
> -             FileNameBuffer,
> -             &DumpFileHandle,
> -             EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
> EFI_FILE_MODE_CREATE,
> -             0
> -             );
> -  if (EFI_ERROR (Status)) {
> -    ShellPrintHiiEx (
> -      -1,
> -      -1,
> -      NULL,
> -      STRING_TOKEN (STR_GEN_READONLY_MEDIA),
> -      gShellAcpiViewHiiHandle,
> -      L"acpiview"
> -      );
> -    return FALSE;
> -  }
> -
>    Print (L"Dumping ACPI table to : %s ... ", FileNameBuffer);
> 
> -  Status = ShellWriteFile (
> -             DumpFileHandle,
> -             &TransferBytes,
> -             (VOID*)Ptr
> -             );
> -  if (EFI_ERROR (Status)) {
> -    Print (L"ERROR: Failed to dump table to binary file.\n");
> -    TransferBytes = 0;
> -  } else {
> -    Print (L"DONE.\n");
> -  }
> -
> -  ShellCloseFile (&DumpFileHandle);
> +  TransferBytes = DumpFile (FileNameBuffer, Ptr, Length);
>    return (Length == TransferBytes);
>  }
> 
> diff --git
> a/ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLi
> b.c
> b/ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLi
> b.c
> index c3942ad24e5b..adf3cce6cfe2 100644
> ---
> a/ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLi
> b.c
> +++ b/ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewComm
> +++ andLib.c
> @@ -98,6 +98,64 @@ RegisterAllParsers (
>    return Status;
>  }
> 
> +/**
> +  Dump a buffer to a file
> +
> +  @param[in] FileName   The filename that shall be created to contain the buffer
> +  @param[in] Buffer     Pointer to buffer that shall be dumped
> +  @param[in] BufferSize The size of buffer to be dumped in bytes
> +
> +  @return The number of bytes that were written **/ UINTN EFIAPI
> +DumpFile (
> +  IN CONST CHAR16* FileNameBuffer,
> +  IN CONST VOID*   Buffer,
> +  IN CONST UINTN   BufferSize
> +  )
> +{
> +  EFI_STATUS          Status;
> +  SHELL_FILE_HANDLE   DumpFileHandle;
> +  UINTN               TransferBytes;
> +
> +  Status = ShellOpenFileByName (
> +             FileNameBuffer,
> +             &DumpFileHandle,
> +             EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
> EFI_FILE_MODE_CREATE,
> +             0
> +             );
> +
> +  if (EFI_ERROR (Status)) {
> +    ShellPrintHiiEx (
> +      -1,
> +      -1,
> +      NULL,
> +      STRING_TOKEN (STR_GEN_READONLY_MEDIA),
> +      gShellAcpiViewHiiHandle,
> +      L"acpiview"
> +      );
> +    return 0;
> +  }
> +
> +  TransferBytes = BufferSize;
> +  Status = ShellWriteFile (
> +             DumpFileHandle,
> +             &TransferBytes,
> +             (VOID *) Buffer
> +             );
> +
> +  if (EFI_ERROR (Status)) {
> +    Print (L"ERROR: Failed to write binary file.\n");
> +    TransferBytes = 0;
> +  } else {
> +    Print (L"DONE.\n");
> +  }
> +
> +  ShellCloseFile (&DumpFileHandle);
> +  return TransferBytes;
> +}
> +
>  /**
>    Return the file name of the help text file if not using HII.
> 
> diff --git
> a/ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLi
> b.h
> b/ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLi
> b.h
> index a3a29164004d..9dd39cf4b3f0 100644
> ---
> a/ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLi
> b.h
> +++ b/ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewComm
> +++ andLib.h
> @@ -10,6 +10,23 @@
> 
>  extern EFI_HII_HANDLE gShellAcpiViewHiiHandle;
> 
> +/**
> +  Dump a buffer to a file
> +
> +  @param[in] FileName   The filename that shall be created to contain the buffer
> +  @param[in] Buffer     Pointer to buffer that shall be dumped
> +  @param[in] BufferSize The size of buffer to be dumped in bytes
> +
> +  @return The number of bytes that were written **/ UINTN EFIAPI
> +DumpFile (
> +  IN CONST CHAR16* FileNameBuffer,
> +  IN CONST VOID*   Buffer,
> +  IN CONST UINTN   BufferSize
> +  );
> +
>  /**
>    Function for 'acpiview' command.
> 
> --
> 2.24.1.windows.2
> 
> 
> 
> 


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#61188): https://edk2.groups.io/g/devel/message/61188
Mute This Topic: https://groups.io/mt/74382417/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub  [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-