[edk2] [PATCH] BaseTools: argument genfds-multi-thread create GenSec command issue

Feng, YunhuaX posted 1 patch 6 years ago
Failed in applying to current master (apply log)
BaseTools/Source/C/GenSec/GenSec.c | 133 +++++++++++++++++++++++++++++++++++--
1 file changed, 126 insertions(+), 7 deletions(-)
[edk2] [PATCH] BaseTools: argument genfds-multi-thread create GenSec command issue
Posted by Feng, YunhuaX 6 years ago
Issue:
  genfds-multi-thread create makefile before section file generation,
  so it get alignment is zero from empty file. It is incorrect.
solution:
  GenSec get section alignment from input file when the input alignment
  is zero.

Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Yunhua Feng <yunhuax.feng@intel.com>
---
 BaseTools/Source/C/GenSec/GenSec.c | 133 +++++++++++++++++++++++++++++++++++--
 1 file changed, 126 insertions(+), 7 deletions(-)

diff --git a/BaseTools/Source/C/GenSec/GenSec.c b/BaseTools/Source/C/GenSec/GenSec.c
index fb5bc5e992..fdffc44f0e 100644
--- a/BaseTools/Source/C/GenSec/GenSec.c
+++ b/BaseTools/Source/C/GenSec/GenSec.c
@@ -9,10 +9,16 @@ http://opensource.org/licenses/bsd-license.php
                                                                                           
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             
 
 **/
+#ifndef __GNUC__
+#include <windows.h>
+#include <io.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#endif
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
@@ -25,10 +31,12 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include "CommonLib.h"
 #include "Compress.h"
 #include "Crc32.h"
 #include "EfiUtilityMsgs.h"
 #include "ParseInf.h"
+#include "FvLib.h"
+#include "PeCoffLib.h"
 
 //
 // GenSec Tool Information
 //
 #define UTILITY_NAME            "GenSec"
@@ -183,12 +191,13 @@ Returns:
   fprintf (stdout, "  -j Number, --buildnumber Number\n\
                         Number is an integer value between 0 and 65535\n\
                         used in Ver section.\n");
   fprintf (stdout, "  --sectionalign SectionAlign\n\
                         SectionAlign points to section alignment, which support\n\
-                        the alignment scope 1~16M. It is specified in same\n\
-                        order that the section file is input.\n");
+                        the alignment scope 0~16M. If SectionAlign is specified\n\
+                        as 0, tool get alignment value from SectionFile.It is\n\
+                        specified in same order that the section file is input.\n");
   fprintf (stdout, "  --dummy dummyfile\n\
                         compare dummpyfile with input_file to decide whether\n\
                         need to set PROCESSING_REQUIRED attribute.\n");
   fprintf (stdout, "  -v, --verbose         Turn on verbose output with informational messages.\n");
   fprintf (stdout, "  -q, --quiet           Disable all messages except key message and fatal error\n");
@@ -983,10 +992,107 @@ Returns:
   *OutFileBuffer = FileBuffer;
 
   return EFI_SUCCESS;
 }
 
+EFI_STATUS
+FfsRebaseImageRead (
+    IN      VOID    *FileHandle,
+    IN      UINTN   FileOffset,
+    IN OUT  UINT32  *ReadSize,
+    OUT     VOID    *Buffer
+    )
+  /*++
+
+    Routine Description:
+
+    Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
+
+    Arguments:
+
+   FileHandle - The handle to the PE/COFF file
+
+   FileOffset - The offset, in bytes, into the file to read
+
+   ReadSize   - The number of bytes to read from the file starting at FileOffset
+
+   Buffer     - A pointer to the buffer to read the data into.
+
+   Returns:
+
+   EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
+
+   --*/
+{
+  CHAR8   *Destination8;
+  CHAR8   *Source8;
+  UINT32  Length;
+
+  Destination8  = Buffer;
+  Source8       = (CHAR8 *) ((UINTN) FileHandle + FileOffset);
+  Length        = *ReadSize;
+  while (Length--) {
+    *(Destination8++) = *(Source8++);
+  }
+
+  return EFI_SUCCESS;
+}
+
+STATIC
+EFI_STATUS
+GetAlignmentFromFile(char *InFile, UINT32 *Alignment)
+  /*
+    InFile is input file for getting alignment
+    return the alignment
+    */
+{
+  FILE                           *InFileHandle;
+  UINT8                          *PeFileBuffer;
+  UINTN                          PeFileSize;
+  UINT32                         CurSecHdrSize;
+  PE_COFF_LOADER_IMAGE_CONTEXT   ImageContext;
+  EFI_COMMON_SECTION_HEADER      *CommonHeader;
+  EFI_STATUS                     Status;
+
+  InFileHandle        = NULL;
+  PeFileBuffer        = NULL;
+  *Alignment          = 0;
+
+  memset (&ImageContext, 0, sizeof (ImageContext));
+
+  InFileHandle = fopen(LongFilePath(InFile), "rb");
+  if (InFileHandle == NULL){
+    Error (NULL, 0, 0001, "Error opening file", InFile);
+    return EFI_ABORTED;
+  }
+  PeFileSize = _filelength (fileno(InFileHandle));
+  PeFileBuffer = (UINT8 *) malloc (PeFileSize);
+  if (PeFileBuffer == NULL) {
+    fclose (InFileHandle);
+    Error(NULL, 0, 4001, "Resource", "memory cannot be allocated  of %s", InFileHandle);
+    return EFI_OUT_OF_RESOURCES;
+  }
+  fread (PeFileBuffer, sizeof (UINT8), PeFileSize, InFileHandle);
+  fclose (InFileHandle);
+  CommonHeader = (EFI_COMMON_SECTION_HEADER *) PeFileBuffer;
+  CurSecHdrSize = GetSectionHeaderLength(CommonHeader);
+  ImageContext.Handle = (VOID *) ((UINTN)PeFileBuffer + CurSecHdrSize);
+  ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE)FfsRebaseImageRead;
+  Status               = PeCoffLoaderGetImageInfo(&ImageContext);
+  if (EFI_ERROR (Status)) {
+    Error (NULL, 0, 3000, "Invalid PeImage", "The input file is %s and return status is %x", InFile, (int) Status);
+    return Status;
+   }
+  *Alignment = ImageContext.SectionAlignment;
+  // Free the allocated memory resource
+  if (PeFileBuffer != NULL) {
+    free (PeFileBuffer);
+    PeFileBuffer = NULL;
+  }
+  return EFI_SUCCESS;
+}
+
 int
 main (
   int  argc,
   char *argv[]
   )
@@ -1267,15 +1373,18 @@ Returns:
           Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
           goto Finish;
         }
         memset (&(InputFileAlign[InputFileNum]), 1, (MAXIMUM_INPUT_FILE_NUM * sizeof (UINT32)));
       }
-      
-      Status = StringtoAlignment (argv[1], &(InputFileAlign[InputFileAlignNum]));
-      if (EFI_ERROR (Status)) {
-        Error (NULL, 0, 1003, "Invalid option value", "%s = %s", argv[0], argv[1]);
-        goto Finish;
+      if (stricmp(argv[1], "0") == 0) {
+        InputFileAlign[InputFileAlignNum] = 0;
+      } else {
+        Status = StringtoAlignment (argv[1], &(InputFileAlign[InputFileAlignNum]));
+        if (EFI_ERROR (Status)) {
+          Error (NULL, 0, 1003, "Invalid option value", "%s = %s", argv[0], argv[1]);
+          goto Finish;
+        }
       }
       argc -= 2;
       argv += 2;
       InputFileAlignNum ++;
       continue; 
@@ -1314,10 +1423,20 @@ Returns:
 
   if (InputFileAlignNum > 0 && InputFileAlignNum != InputFileNum) {
     Error (NULL, 0, 1003, "Invalid option", "section alignment must be set for each section");
     goto Finish;
   }
+  for (Index = 0; Index < InputFileAlignNum; Index++)
+  {
+    if (InputFileAlign[Index] == 0) {
+      Status = GetAlignmentFromFile(InputFileName[Index], &(InputFileAlign[Index]));
+      if (EFI_ERROR(Status)) {
+        Error (NULL, 0, 1003, "Fail to get Alignment from %s", InputFileName[InputFileNum]);
+        goto Finish;
+      }
+    }
+  }
 
   VerboseMsg ("%s tool start.", UTILITY_NAME);
 
   if (DummyFileName != NULL) {
       //
-- 
2.12.2.windows.2

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel