From: "Feng, YunhuaX" <yunhuax.feng@intel.com>
To: "edk2-devel@lists.01.org" <edk2-devel@lists.01.org>
Cc: "Zhu, Yonghong" <yonghong.zhu@intel.com>,
"Gao, Liming" <liming.gao@intel.com>
Subject: [PATCH 2/3] BaseTools: argument genfds-multi-thread create GenSec command issue
Date: Tue, 10 Apr 2018 01:10:41 +0000 [thread overview]
Message-ID: <47C64442C08CCD4089DC43B6B5E46BC484BAA5@shsmsx102.ccr.corp.intel.com> (raw)
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 56767d5a9b..9f592742e1 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
next reply other threads:[~2018-04-10 1:10 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-04-10 1:10 Feng, YunhuaX [this message]
2018-04-11 6:33 ` [PATCH 2/3] BaseTools: argument genfds-multi-thread create GenSec command issue Zhu, Yonghong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=47C64442C08CCD4089DC43B6B5E46BC484BAA5@shsmsx102.ccr.corp.intel.com \
--to=devel@edk2.groups.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox