Reviewed-by: Liming Gao <liming....@intel.com>

> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-boun...@lists.01.org] On Behalf Of 
> Yonghong Zhu
> Sent: Monday, November 09, 2015 3:33 PM
> To: edk2-devel@lists.01.org
> Cc: Gao, Liming; carl.mil...@pnnl.gov
> Subject: [edk2] [Patch] BaseTools/GenFw: add new option to not zero PE/COFF 
> optional header fields
> 
> Add new option --keepoptionalheader and that flag does not zero PE/COFF
> optional header fields including the version fields. It can support the
> case that the PE/COFF optional header would be kept.
> 
> Contributed-under: TianoCore Contribution Agreement 1.0
> Signed-off-by: Yonghong Zhu <yonghong....@intel.com>
> ---
>  Source/C/GenFw/GenFw.c | 82 
> +++++++++++++++++++++++++++++---------------------
>  1 file changed, 48 insertions(+), 34 deletions(-)
> 
> diff --git a/Source/C/GenFw/GenFw.c b/Source/C/GenFw/GenFw.c
> index 4756c52..9ddd18e 100644
> --- a/Source/C/GenFw/GenFw.c
> +++ b/Source/C/GenFw/GenFw.c
> @@ -1,9 +1,9 @@
>  /** @file
>  Converts a pe32+ image to an FW, Te image type, or other specific image.
> 
> -Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2004 - 2015, Intel Corporation. All rights reserved.<BR>
>  This program and the accompanying materials
>  are licensed and made available under the terms and conditions of the BSD 
> License
>  which accompanies this distribution.  The full text of the license may be 
> found at
>  http://opensource.org/licenses/bsd-license.php
> 
> @@ -166,11 +166,11 @@ Returns:
>    fprintf (stdout, "\nUsage: %s [options] <input_file>\n\n", UTILITY_NAME);
> 
>    //
>    // Copyright declaration
>    //
> -  fprintf (stdout, "Copyright (c) 2007 - 2014, Intel Corporation. All rights 
> reserved.\n\n");
> +  fprintf (stdout, "Copyright (c) 2007 - 2015, Intel Corporation. All rights 
> reserved.\n\n");
> 
>    //
>    // Details Option
>    //
>    fprintf (stdout, "Options:\n");
> @@ -182,23 +182,23 @@ Returns:
>                          SEC, DXE_SAL_DRIVER, UEFI_DRIVER, 
> DXE_RUNTIME_DRIVER,\n\
>                          DXE_SMM_DRIVER, SECURITY_CORE, 
> COMBINED_PEIM_DRIVER,\n\
>                          PIC_PEIM, RELOCATABLE_PEIM, BS_DRIVER, RT_DRIVER,\n\
>                          APPLICATION, SAL_RT_DRIVER to support all module 
> types\n\
>                          It can only be used together with 
> --keepexceptiontable,\n\
> -                        --keepzeropending, -r, -o option.It is a action 
> option.\n\
> -                        If it is combined with other action options, the 
> later\n\
> -                        input action option will override the previous 
> one.\n");
> +                        --keepzeropending, --keepoptionalheader, -r, -o 
> option.\n\
> +                        It is a action option. If it is combined with other 
> action options,\n\
> +                        the later input action option will override the 
> previous one.\n");
>    fprintf (stdout, "  -c, --acpi            Create Acpi table.\n\
>                          It can't be combined with other action options\n\
>                          except for -o, -r option. It is a action option.\n\
>                          If it is combined with other action options, the 
> later\n\
>                          input action option will override the previous 
> one.\n");
>    fprintf (stdout, "  -t, --terse           Create Te Image.\n\
>                          It can only be used together with 
> --keepexceptiontable,\n\
> -                        --keepzeropending, -r, -o option.It is a action 
> option.\n\
> -                        If it is combined with other action options, the 
> later\n\
> -                        input action option will override the previous 
> one.\n");
> +                        --keepzeropending, --keepoptionalheader, -r, -o 
> option.\n\
> +                        It is a action option. If it is combined with other 
> action options,\n\
> +                        the later input action option will override the 
> previous one.\n");
>    fprintf (stdout, "  -u, --dump            Dump TeImage Header.\n\
>                          It can't be combined with other action options\n\
>                          except for -o, -r option. It is a action option.\n\
>                          If it is combined with other action options, the 
> later\n\
>                          input action option will override the previous 
> one.\n");
> @@ -244,10 +244,13 @@ Returns:
>    fprintf (stdout, "  -p NUM, --pad NUM     NUM is one HEX or DEC format 
> padding value.\n\
>                          This option is only used together with -j 
> option.\n");
>    fprintf (stdout, "  --keepexceptiontable  Don't clear exception table.\n\
>                          This option can be used together with -e or -t.\n\
>                          It doesn't work for other options.\n");
> +  fprintf (stdout, "  --keepoptionalheader  Don't zero PE/COFF optional 
> header fields.\n\
> +                        This option can be used together with -e or -t.\n\
> +                        It doesn't work for other options.\n");
>    fprintf (stdout, "  --keepzeropending     Don't strip zero pending of 
> .reloc.\n\
>                          This option can be used together with -e or -t.\n\
>                          It doesn't work for other options.\n");
>    fprintf (stdout, "  -r, --replace         Overwrite the input file with 
> the output content.\n\
>                          If more input files are specified,\n\
> @@ -1077,10 +1080,11 @@ Returns:
>    RUNTIME_FUNCTION                 *RuntimeFunction;
>    UNWIND_INFO                      *UnwindInfo;
>    STATUS                           Status;
>    BOOLEAN                          ReplaceFlag;
>    BOOLEAN                          KeepExceptionTableFlag;
> +  BOOLEAN                          KeepOptionalHeaderFlag;
>    BOOLEAN                          KeepZeroPendingFlag;
>    UINT64                           LogLevel;
>    EFI_TE_IMAGE_HEADER              TEImageHeader;
>    EFI_TE_IMAGE_HEADER              *TeHdr;
>    EFI_IMAGE_SECTION_HEADER         *SectionHeader;
> @@ -1139,10 +1143,11 @@ Returns:
>    InputFileBuffer   = NULL;
>    InputFileLength   = 0;
>    Optional32        = NULL;
>    Optional64        = NULL;
>    KeepExceptionTableFlag = FALSE;
> +  KeepOptionalHeaderFlag = FALSE;
>    KeepZeroPendingFlag    = FALSE;
>    NumberOfFormPacakge    = 0;
>    HiiPackageListBuffer   = NULL;
>    HiiPackageDataPointer  = NULL;
>    EndPackage.Length      = sizeof (EFI_HII_PACKAGE_HEADER);
> @@ -1267,10 +1272,17 @@ Returns:
>        argc --;
>        argv ++;
>        continue;
>      }
> 
> +    if (stricmp(argv[0], "--keepoptionalheader") == 0) {
> +      KeepOptionalHeaderFlag = TRUE;
> +      argc--;
> +      argv++;
> +      continue;
> +    }
> +
>      if (stricmp (argv[0], "--keepzeropending") == 0) {
>        KeepZeroPendingFlag = TRUE;
>        argc --;
>        argv ++;
>        continue;
> @@ -2301,23 +2313,24 @@ Returns:
>    //
>    PeHdr->Pe32.OptionalHeader.Subsystem = (UINT16) Type;
> 
>    if (PeHdr->Pe32.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) 
> {
>      Optional32 = (EFI_IMAGE_OPTIONAL_HEADER32 *)&PeHdr->Pe32.OptionalHeader;
> -    Optional32->MajorOperatingSystemVersion = 0;
> -    Optional32->MinorOperatingSystemVersion = 0;
> -    Optional32->MajorImageVersion           = 0;
> -    Optional32->MinorImageVersion           = 0;
> -    Optional32->MajorSubsystemVersion       = 0;
> -    Optional32->MinorSubsystemVersion       = 0;
> -    Optional32->Win32VersionValue           = 0;
> -    Optional32->CheckSum                    = 0;
> -    Optional32->SizeOfStackReserve = 0;
> -    Optional32->SizeOfStackCommit  = 0;
> -    Optional32->SizeOfHeapReserve  = 0;
> -    Optional32->SizeOfHeapCommit   = 0;
> -
> +    if (!KeepOptionalHeaderFlag) {
> +      Optional32->MajorOperatingSystemVersion = 0;
> +      Optional32->MinorOperatingSystemVersion = 0;
> +      Optional32->MajorImageVersion = 0;
> +      Optional32->MinorImageVersion = 0;
> +      Optional32->MajorSubsystemVersion = 0;
> +      Optional32->MinorSubsystemVersion = 0;
> +      Optional32->Win32VersionValue = 0;
> +      Optional32->CheckSum = 0;
> +      Optional32->SizeOfStackReserve = 0;
> +      Optional32->SizeOfStackCommit = 0;
> +      Optional32->SizeOfHeapReserve = 0;
> +      Optional32->SizeOfHeapCommit = 0;
> +    }
>      TEImageHeader.AddressOfEntryPoint = Optional32->AddressOfEntryPoint;
>      TEImageHeader.BaseOfCode          = Optional32->BaseOfCode;
>      TEImageHeader.ImageBase           = (UINT64) (Optional32->ImageBase);
> 
>      if (Optional32->NumberOfRvaAndSizes > 
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
> @@ -2393,23 +2406,24 @@ Returns:
>          }
>        }
>      }
>    } else if (PeHdr->Pe32.OptionalHeader.Magic == 
> EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
>      Optional64 = (EFI_IMAGE_OPTIONAL_HEADER64 *)&PeHdr->Pe32.OptionalHeader;
> -    Optional64->MajorOperatingSystemVersion = 0;
> -    Optional64->MinorOperatingSystemVersion = 0;
> -    Optional64->MajorImageVersion           = 0;
> -    Optional64->MinorImageVersion           = 0;
> -    Optional64->MajorSubsystemVersion       = 0;
> -    Optional64->MinorSubsystemVersion       = 0;
> -    Optional64->Win32VersionValue           = 0;
> -    Optional64->CheckSum                    = 0;
> -    Optional64->SizeOfStackReserve = 0;
> -    Optional64->SizeOfStackCommit  = 0;
> -    Optional64->SizeOfHeapReserve  = 0;
> -    Optional64->SizeOfHeapCommit   = 0;
> -
> +    if (!KeepOptionalHeaderFlag) {
> +      Optional64->MajorOperatingSystemVersion = 0;
> +      Optional64->MinorOperatingSystemVersion = 0;
> +      Optional64->MajorImageVersion = 0;
> +      Optional64->MinorImageVersion = 0;
> +      Optional64->MajorSubsystemVersion = 0;
> +      Optional64->MinorSubsystemVersion = 0;
> +      Optional64->Win32VersionValue = 0;
> +      Optional64->CheckSum = 0;
> +      Optional64->SizeOfStackReserve = 0;
> +      Optional64->SizeOfStackCommit = 0;
> +      Optional64->SizeOfHeapReserve = 0;
> +      Optional64->SizeOfHeapCommit = 0;
> +    }
>      TEImageHeader.AddressOfEntryPoint = Optional64->AddressOfEntryPoint;
>      TEImageHeader.BaseOfCode          = Optional64->BaseOfCode;
>      TEImageHeader.ImageBase           = (UINT64) (Optional64->ImageBase);
> 
>      if (Optional64->NumberOfRvaAndSizes > 
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
> --
> 2.6.1.windows.1
> 
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel

Reply via email to