From 3eb9473ea9a949badfe06ae61d2d3fcfa53651c7 Mon Sep 17 00:00:00 2001 From: qwang12 Date: Thu, 28 Jun 2007 07:00:39 +0000 Subject: Add in the 1st version of ECP. git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@2832 6f19259b-4bc3-4df7-8a09-765794883524 --- .../Tools/Source/GenCRC32Section/GenCRC32Section.c | 299 +++++++++++++++++++++ .../Tools/Source/GenCRC32Section/GenCRC32Section.h | 43 +++ .../Sample/Tools/Source/GenCRC32Section/makefile | 85 ++++++ 3 files changed, 427 insertions(+) create mode 100644 EdkCompatibilityPkg/Sample/Tools/Source/GenCRC32Section/GenCRC32Section.c create mode 100644 EdkCompatibilityPkg/Sample/Tools/Source/GenCRC32Section/GenCRC32Section.h create mode 100644 EdkCompatibilityPkg/Sample/Tools/Source/GenCRC32Section/makefile (limited to 'EdkCompatibilityPkg/Sample/Tools/Source/GenCRC32Section') diff --git a/EdkCompatibilityPkg/Sample/Tools/Source/GenCRC32Section/GenCRC32Section.c b/EdkCompatibilityPkg/Sample/Tools/Source/GenCRC32Section/GenCRC32Section.c new file mode 100644 index 0000000000..27a02a4da9 --- /dev/null +++ b/EdkCompatibilityPkg/Sample/Tools/Source/GenCRC32Section/GenCRC32Section.c @@ -0,0 +1,299 @@ +/*++ + +Copyright (c) 2004 - 2006, Intel Corporation +All rights reserved. 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 + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +Module Name: + + GenCRC32Section.c + +Abstract: + + This file contains functions required to generate a Firmware File System + file. The code is compliant with the Tiano C Coding standards. + +--*/ + +#include "TianoCommon.h" +#include "EfiFirmwareFileSystem.h" +#include "EfiFirmwareVolumeHeader.h" +#include "ParseInf.h" +#include "crc32.h" +#include "EfiUtilityMsgs.h" +#include "GenCRC32Section.h" +#include +#include +#include +#include +#include "CommonLib.h" + +#include EFI_PROTOCOL_DEFINITION (GuidedSectionExtraction) + +#define TOOLVERSION "0.2" + +#define UTILITY_NAME "GenCrc32Section" + +EFI_GUID gEfiCrc32SectionGuid = EFI_CRC32_GUIDED_SECTION_EXTRACTION_PROTOCOL_GUID; + +EFI_STATUS +SignSectionWithCrc32 ( + IN OUT UINT8 *FileBuffer, + IN OUT UINT32 *BufferSize, + IN UINT32 DataSize + ) +/*++ + +Routine Description: + + Signs the section with CRC32 and add GUIDed section header for the + signed data. data stays in same location (overwrites source data). + +Arguments: + + FileBuffer - Buffer containing data to sign + + BufferSize - On input, the size of FileBuffer. On output, the size of + actual section data (including added section header). + + DataSize - Length of data to Sign + + Key - Key to use when signing. Currently only CRC32 is supported. + +Returns: + + EFI_SUCCESS - Successful + EFI_OUT_OF_RESOURCES - Not enough resource to complete the operation. + +--*/ +{ + + UINT32 Crc32Checksum; + EFI_STATUS Status; + UINT32 TotalSize; + CRC32_SECTION_HEADER Crc32Header; + UINT8 *SwapBuffer; + + Crc32Checksum = 0; + SwapBuffer = NULL; + + if (DataSize == 0) { + *BufferSize = 0; + + return EFI_SUCCESS; + } + + Status = CalculateCrc32 (FileBuffer, DataSize, &Crc32Checksum); + if (EFI_ERROR (Status)) { + return Status; + } + + TotalSize = DataSize + CRC32_SECTION_HEADER_SIZE; + Crc32Header.GuidSectionHeader.CommonHeader.Type = EFI_SECTION_GUID_DEFINED; + Crc32Header.GuidSectionHeader.CommonHeader.Size[0] = (UINT8) (TotalSize & 0xff); + Crc32Header.GuidSectionHeader.CommonHeader.Size[1] = (UINT8) ((TotalSize & 0xff00) >> 8); + Crc32Header.GuidSectionHeader.CommonHeader.Size[2] = (UINT8) ((TotalSize & 0xff0000) >> 16); + memcpy (&(Crc32Header.GuidSectionHeader.SectionDefinitionGuid), &gEfiCrc32SectionGuid, sizeof (EFI_GUID)); + Crc32Header.GuidSectionHeader.Attributes = EFI_GUIDED_SECTION_AUTH_STATUS_VALID; + Crc32Header.GuidSectionHeader.DataOffset = CRC32_SECTION_HEADER_SIZE; + Crc32Header.CRC32Checksum = Crc32Checksum; + + SwapBuffer = (UINT8 *) malloc (DataSize); + if (SwapBuffer == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + memcpy (SwapBuffer, FileBuffer, DataSize); + memcpy (FileBuffer, &Crc32Header, CRC32_SECTION_HEADER_SIZE); + memcpy (FileBuffer + CRC32_SECTION_HEADER_SIZE, SwapBuffer, DataSize); + + // + // Make sure section ends on a DWORD boundary + // + while ((TotalSize & 0x03) != 0) { + FileBuffer[TotalSize] = 0; + TotalSize++; + } + + *BufferSize = TotalSize; + + if (SwapBuffer != NULL) { + free (SwapBuffer); + } + + return EFI_SUCCESS; +} + +VOID +PrintUsage ( + VOID + ) +{ + printf ("Usage:\n"); + printf (UTILITY_NAME " -i \"inputfile1\" \"inputfile2\" -o \"outputfile\" \n"); + printf (" -i \"inputfile\":\n "); + printf (" specifies the input files that would be signed to CRC32 Guided section.\n"); + printf (" -o \"outputfile\":\n"); + printf (" specifies the output file that is a CRC32 Guided section.\n"); +} + +INT32 +ReadFilesContentsIntoBuffer ( + IN CHAR8 *argv[], + IN INT32 Start, + IN OUT UINT8 **FileBuffer, + IN OUT UINT32 *BufferSize, + OUT UINT32 *ContentSize, + IN INT32 MaximumArguments + ) +{ + INT32 Index; + CHAR8 *FileName; + FILE *InputFile; + UINT8 Temp; + UINT32 Size; + + FileName = NULL; + InputFile = NULL; + Size = 0; + Index = 0; + + // + // read all input files into one file buffer + // + while (argv[Start + Index][0] != '-') { + + FileName = argv[Start + Index]; + InputFile = fopen (FileName, "rb"); + if (InputFile == NULL) { + Error (NULL, 0, 0, FileName, "failed to open input binary file"); + return -1; + } + + fread (&Temp, sizeof (UINT8), 1, InputFile); + while (!feof (InputFile)) { + (*FileBuffer)[Size++] = Temp; + fread (&Temp, sizeof (UINT8), 1, InputFile); + } + + fclose (InputFile); + InputFile = NULL; + + // + // Make sure section ends on a DWORD boundary + // + while ((Size & 0x03) != 0) { + (*FileBuffer)[Size] = 0; + Size++; + } + + Index++; + if (Index == MaximumArguments) { + break; + } + } + + *ContentSize = Size; + return Index; +} + +INT32 +main ( + INT32 argc, + CHAR8 *argv[] + ) +{ + FILE *OutputFile; + UINT8 *FileBuffer; + UINT32 BufferSize; + EFI_STATUS Status; + UINT32 ContentSize; + CHAR8 *OutputFileName; + INT32 ReturnValue; + INT32 Index; + + OutputFile = NULL; + FileBuffer = NULL; + ContentSize = 0; + OutputFileName = NULL; + + SetUtilityName (UTILITY_NAME); + + if (argc == 1) { + PrintUsage (); + return -1; + } + + BufferSize = 1024 * 1024 * 16; + FileBuffer = (UINT8 *) malloc (BufferSize * sizeof (UINT8)); + if (FileBuffer == NULL) { + Error (NULL, 0, 0, "memory allocation failed", NULL); + return -1; + } + + ZeroMem (FileBuffer, BufferSize); + + for (Index = 0; Index < argc; Index++) { + if (_strcmpi (argv[Index], "-i") == 0) { + ReturnValue = ReadFilesContentsIntoBuffer ( + argv, + (Index + 1), + &FileBuffer, + &BufferSize, + &ContentSize, + (argc - (Index + 1)) + ); + if (ReturnValue == -1) { + Error (NULL, 0, 0, "failed to read file contents", NULL); + return -1; + } + + Index += ReturnValue; + } + + if (_strcmpi (argv[Index], "-o") == 0) { + OutputFileName = argv[Index + 1]; + } + } + + OutputFile = fopen (OutputFileName, "wb"); + if (OutputFile == NULL) { + Error (NULL, 0, 0, OutputFileName, "failed to open output binary file"); + free (FileBuffer); + return -1; + } + + /* + // + // make sure section ends on a DWORD boundary ?? + // + while ( (Size & 0x03) != 0 ) { + FileBuffer[Size] = 0; + Size ++; + } +*/ + Status = SignSectionWithCrc32 (FileBuffer, &BufferSize, ContentSize); + if (EFI_ERROR (Status)) { + Error (NULL, 0, 0, "failed to sign section", NULL); + free (FileBuffer); + fclose (OutputFile); + return -1; + } + + ContentSize = fwrite (FileBuffer, sizeof (UINT8), BufferSize, OutputFile); + if (ContentSize != BufferSize) { + Error (NULL, 0, 0, "failed to write output buffer", NULL); + ReturnValue = -1; + } else { + ReturnValue = 0; + } + + free (FileBuffer); + fclose (OutputFile); + return ReturnValue; +} diff --git a/EdkCompatibilityPkg/Sample/Tools/Source/GenCRC32Section/GenCRC32Section.h b/EdkCompatibilityPkg/Sample/Tools/Source/GenCRC32Section/GenCRC32Section.h new file mode 100644 index 0000000000..7f88be2364 --- /dev/null +++ b/EdkCompatibilityPkg/Sample/Tools/Source/GenCRC32Section/GenCRC32Section.h @@ -0,0 +1,43 @@ +/*++ + +Copyright (c) 2004, Intel Corporation +All rights reserved. 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 + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +Module Name: + + GenCRC32Section.h + +Abstract: + + Header file for GenFfsFile. Mainly defines the header of section + header for CRC32 GUID defined sections. Share with GenSection.c + +--*/ + +// +// Module Coded to Tiano Coding Conventions +// +#ifndef _EFI_GEN_CRC32_SECTION_H +#define _EFI_GEN_CRC32_SECTION_H + +// +// External Files Referenced +// +#include "TianoCommon.h" +#include "EfiImageFormat.h" + +typedef struct { + EFI_GUID_DEFINED_SECTION GuidSectionHeader; + UINT32 CRC32Checksum; +} CRC32_SECTION_HEADER; + +#define EFI_SECTION_CRC32_GUID_DEFINED 0 +#define CRC32_SECTION_HEADER_SIZE (sizeof (CRC32_SECTION_HEADER)) + +#endif diff --git a/EdkCompatibilityPkg/Sample/Tools/Source/GenCRC32Section/makefile b/EdkCompatibilityPkg/Sample/Tools/Source/GenCRC32Section/makefile new file mode 100644 index 0000000000..f6c07f7340 --- /dev/null +++ b/EdkCompatibilityPkg/Sample/Tools/Source/GenCRC32Section/makefile @@ -0,0 +1,85 @@ +#/*++ +# +# Copyright (c) 2004 - 2007, Intel Corporation +# All rights reserved. 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 +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# +# Module Name: makefile +# +# Abstract: +# +# This file is used to build the EFI utility. +# +#--*/ + +# +# Do this if you want to compile from this directory +# +!IFNDEF TOOLCHAIN +TOOLCHAIN = TOOLCHAIN_MSVC +!ENDIF + +!INCLUDE $(BUILD_DIR)\PlatformTools.env + +# +# Common information +# + +INC=$(INC) + +# +# Target specific information +# + +TARGET_NAME=GenCRC32Section + +TARGET_SOURCE_DIR = $(EDK_TOOLS_SOURCE)\$(TARGET_NAME) + +TARGET_EXE = $(EDK_TOOLS_OUTPUT)\$(TARGET_NAME).exe + +TARGET_EXE_SOURCE = "$(TARGET_SOURCE_DIR)\GenCRC32Section.c" +TARGET_EXE_INCLUDE = "$(EDK_SOURCE)\Foundation\Include\TianoCommon.h" \ + "$(EDK_SOURCE)\Foundation\Framework\Include\EfiFirmwareFileSystem.h" \ + "$(EDK_SOURCE)\Foundation\Framework\Include\EfiFirmwareVolumeHeader.h" \ + "$(EDK_TOOLS_COMMON)\ParseInf.h" +TARGET_EXE_LIBS = "$(EDK_TOOLS_OUTPUT)\Common.lib" + + +# +# Build targets +# + +all: $(TARGET_EXE) + +# +# Build EXE +# + +$(EDK_TOOLS_OUTPUT)\$(TARGET_NAME).obj: $(TARGET_EXE_SOURCE) $(TARGET_EXE_INCLUDE) + $(CC) $(C_FLAGS) $(INC) $(TARGET_EXE_SOURCE) /Fo$(EDK_TOOLS_OUTPUT)\$(TARGET_NAME).obj + +# +# Add Binary Build description for this tool. +# + +!IF (("$(EFI_BINARY_TOOLS)" == "YES") && EXIST($(EFI_PLATFORM_BIN)\Tools\$(TARGET_NAME).exe)) +$(TARGET_EXE): $(EFI_PLATFORM_BIN)\Tools\$(TARGET_NAME).exe + copy $(EFI_PLATFORM_BIN)\Tools\$(TARGET_NAME).exe $(TARGET_EXE) /Y + if exist $(EFI_PLATFORM_BIN)\Tools\$(TARGET_NAME).pdb \ + copy $(EFI_PLATFORM_BIN)\Tools\$(TARGET_NAME).pdb $(EDK_TOOLS_OUTPUT)\$(TARGET_NAME).pdb /Y +!ELSE +$(TARGET_EXE): $(EDK_TOOLS_OUTPUT)\$(TARGET_NAME).obj $(TARGET_EXE_LIBS) $(TARGET_DLL) + $(LINK) $(MSVS_LINK_LIBPATHS) $(L_FLAGS) $(LIBS) /out:$(TARGET_EXE) $(EDK_TOOLS_OUTPUT)\$(TARGET_NAME).obj $(TARGET_LIB) $(TARGET_EXE_LIBS) + if not exist $(EFI_PLATFORM_BIN)\Tools mkdir $(EFI_PLATFORM_BIN)\Tools + if exist $(TARGET_EXE) copy $(TARGET_EXE) $(EFI_PLATFORM_BIN)\tools\$(TARGET_NAME).exe /Y + if exist $(EDK_TOOLS_OUTPUT)\$(TARGET_NAME).pdb \ + copy $(EDK_TOOLS_OUTPUT)\$(TARGET_NAME).pdb $(EFI_PLATFORM_BIN)\Tools\$(TARGET_NAME).pdb /Y +!ENDIF + +clean: + @if exist $(EDK_TOOLS_OUTPUT)\$(TARGET_NAME).* del $(EDK_TOOLS_OUTPUT)\$(TARGET_NAME).* > NUL -- cgit v1.2.3