+++ /dev/null
-/*++\r
-\r
-Copyright (c) 2004-2007 Intel Corporation. All rights reserved\r
-This program and the accompanying materials are licensed and made available \r
-under the terms and conditions of the BSD License which accompanies this \r
-distribution. The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-\r
-Module Name:\r
-\r
- SecApResetVectorFixup.c\r
-\r
-Abstract:\r
-\r
- This utility is part of build process for IA32 Fvrecovery.fv whose total size\r
- is larger than 128kB so that we cannot use GenFvImage utility to put Ap reset \r
- vector at the zero vector of Fv header.\r
- \r
- PEI FV after using the tool\r
- \r
- -------------------------\r
- |zzz |\r
- | |\r
- | |\r
- | FFS |\r
- | |\r
- | |\r
- | |\r
- |---------------------- |\r
- | PAD |\r
- | |\r
- |.......................| --- \r
- | | |\r
- |xxx | | 128K \r
- |---------------------- | | \r
- | VTF (SEC) | |\r
- ------------------------- ---\r
- \r
- 1. zzz --> Zero vector, which is beyond the 128K limited address space\r
- 2. xxx --> AP reset vector at 4K alignment below 128K and it is in the PAD\r
- file area.\r
- 3. After the build process ,the PAD guid is changed to a new GUID to avoid \r
- the PAD definition confusing. If there is some problem, try to disable\r
- UpdatePadFileGuid\r
- \r
- \r
- \r
---*/\r
-\r
-#include "SecApResetVectorFixup.h"\r
-\r
-\r
-EFI_GUID DefaultFvPadFileNameGuid = { 0x78f54d4, 0xcc22, 0x4048, 0x9e, 0x94, 0x87, 0x9c, 0x21, 0x4d, 0x56, 0x2f };\r
-EFI_GUID NewFvPadFileNameGuid = { 0x145372bc, 0x66b9, 0x476d, 0x81, 0xbc, 0x21, 0x27, 0xc3, 0x76, 0xbb, 0x66 };\r
-\r
-//\r
-// jmp 0xf000:0xffd0 (0xFFFFFFD0)\r
-//\r
-UINT8 ApResetVector[5] = {0xEA, 0xD0, 0xFF, 0x00, 0xF0};\r
-\r
-VOID\r
-Version (\r
- VOID\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Displays the standard utility information to SDTOUT\r
-\r
-Arguments:\r
-\r
- None\r
-\r
-Returns:\r
-\r
- None\r
-\r
---*/\r
-{\r
- printf ("%s v%d.%d -Tiano IA32 SEC Ap Reset Vector Fixup Utility.\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);\r
- printf ("Copyright (c) 1999-2007 Intel Corporation. All rights reserved.\n");\r
-}\r
-\r
-VOID\r
-Usage (\r
- VOID\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Displays the utility usage syntax to STDOUT\r
-\r
-Arguments:\r
-\r
- None\r
-\r
-Returns:\r
-\r
- None\r
-\r
---*/\r
-{\r
- Version();\r
- \r
- printf ("\nUsage: %s InputFvrecoveryFile OutputFvrecoveryFile\n", UTILITY_NAME);\r
- printf (" Where:\n");\r
- printf (" InputFvrecoveryFile - Name of the IA32 input Fvrecovery.fv file.\n");\r
- printf (" OutputFvrecoveryFile - Name of the IA32 output Fvrecovery.fv file.\n");\r
-}\r
-\r
-\r
-VOID \r
-UpdatePadFileGuid (\r
- IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader,\r
- IN EFI_FFS_FILE_HEADER *FileHeader,\r
- IN UINT32 FileLength,\r
- IN OUT EFI_GUID *Guid\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Update the Pad File Guid to change it to other guid and update\r
- the checksum\r
-\r
-Arguments:\r
- FvHeader - EFI_FIRMWARE_VOLUME_HEADER \r
- FileHeader - The FFS PAD file header.\r
- FileLength - The FFS PAD file length.\r
- Guid - The Guid to compare and if it is PAD Guid, update it to new Guid\r
-Returns:\r
- VOID\r
---*/\r
-\r
-{\r
- if ((CompareGuid (Guid, (EFI_GUID *)&DefaultFvPadFileNameGuid)) == 0) {\r
- //\r
- // Set new Pad file guid\r
- // \r
- memcpy (Guid, &NewFvPadFileNameGuid, sizeof (EFI_GUID));\r
-\r
-\r
-\r
- FileHeader->Type = EFI_FV_FILETYPE_FFS_PAD;\r
- FileHeader->Attributes = 0;\r
- //\r
- // Fill in checksums and state, must be zero during checksum calculation.\r
- //\r
- FileHeader->IntegrityCheck.Checksum.Header = 0;\r
- FileHeader->IntegrityCheck.Checksum.File = 0;\r
- FileHeader->State = 0;\r
- FileHeader->IntegrityCheck.Checksum.Header = CalculateChecksum8 ((UINT8 *) FileHeader, sizeof (EFI_FFS_FILE_HEADER));\r
- if (FileHeader->Attributes & FFS_ATTRIB_CHECKSUM) {\r
- FileHeader->IntegrityCheck.Checksum.File = CalculateChecksum8 ((UINT8 *) FileHeader, FileLength);\r
- } else {\r
- FileHeader->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;\r
- }\r
-\r
- FileHeader->State = EFI_FILE_HEADER_CONSTRUCTION | EFI_FILE_HEADER_VALID | EFI_FILE_DATA_VALID;\r
-\r
- if (FvHeader->Attributes & EFI_FVB_ERASE_POLARITY) {\r
- FileHeader->State = (UINT8)~(FileHeader->State);\r
- } \r
- }\r
- \r
-}\r
-\r
-VOID\r
-SetHeaderChecksum (\r
- IN EFI_FFS_FILE_HEADER *FfsHeader\r
- )\r
-/*++\r
-\r
- Routine Description:\r
- Caculate the checksum for the FFS header.\r
-\r
- Parameters:\r
- FfsHeader - FFS File Header which needs to caculate the checksum\r
-\r
- Return:\r
- N/A\r
-\r
---*/\r
-{\r
- EFI_FFS_FILE_STATE State;\r
- UINT8 HeaderChecksum;\r
- UINT8 FileChecksum;\r
-\r
- //\r
- // The state and the File checksum are not included\r
- //\r
- State = FfsHeader->State;\r
- FfsHeader->State = 0;\r
-\r
- FileChecksum = FfsHeader->IntegrityCheck.Checksum.File;\r
- FfsHeader->IntegrityCheck.Checksum.File = 0;\r
-\r
- FfsHeader->IntegrityCheck.Checksum.Header = 0;\r
-\r
- HeaderChecksum = CalculateChecksum8 ((UINT8 *)FfsHeader,sizeof (EFI_FFS_FILE_HEADER));\r
-\r
- FfsHeader->IntegrityCheck.Checksum.Header = (UINT8) (~(0x100-HeaderChecksum) + 1);\r
-\r
- FfsHeader->State = State;\r
- FfsHeader->IntegrityCheck.Checksum.File = FileChecksum;\r
-\r
- return ;\r
-}\r
-\r
-VOID\r
-SetFileChecksum (\r
- IN EFI_FFS_FILE_HEADER *FfsHeader,\r
- IN UINTN ActualFileSize\r
- )\r
-/*++\r
-\r
- Routine Description:\r
- Caculate the checksum for the FFS File, usually it is caculated before\r
- the file tail is set.\r
-\r
- Parameters:\r
- FfsHeader - FFS File Header which needs to caculate the checksum\r
- ActualFileSize - The whole Ffs File Length, including the FFS Tail\r
- if exists, but at this time, it is 0.\r
- Return:\r
- N/A\r
-\r
---*/\r
-{\r
- EFI_FFS_FILE_STATE State;\r
- UINT8 FileChecksum;\r
- UINTN ActualSize;\r
-\r
- if (FfsHeader->Attributes & FFS_ATTRIB_CHECKSUM) {\r
- //\r
- // The file state is not included\r
- //\r
- State = FfsHeader->State;\r
- FfsHeader->State = 0;\r
-\r
- FfsHeader->IntegrityCheck.Checksum.File = 0;\r
-\r
- if (FfsHeader->Attributes & FFS_ATTRIB_TAIL_PRESENT) {\r
- ActualSize = ActualFileSize - 2;\r
- } else {\r
- ActualSize = ActualFileSize;\r
- }\r
- //\r
- // File checksum does not including the file tail\r
- //\r
- FileChecksum = CalculateChecksum8 ((UINT8 *)FfsHeader,sizeof (EFI_FFS_FILE_HEADER));\r
-\r
- FfsHeader->IntegrityCheck.Checksum.File = (UINT8) (~(0x100-FileChecksum) + 1);\r
-\r
- FfsHeader->State = State;\r
-\r
- } else {\r
-\r
- FfsHeader->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;\r
-\r
- }\r
-\r
- return ;\r
-}\r
-\r
-VOID\r
-SetFileTail (\r
- IN EFI_FFS_FILE_HEADER *FfsHeader,\r
- IN UINTN ActualFileSize\r
- )\r
-/*++\r
-\r
- Routine Description:\r
- Set the file tail if needed\r
-\r
- Parameters:\r
- FfsHeader - FFS File Header which needs to caculate the checksum\r
- ActualFileSize - The whole Ffs File Length, including the FFS Tail\r
- if exists.\r
- Return:\r
- N/A\r
-\r
---*/\r
-{\r
- UINT8 TailLow;\r
- UINT8 TailHigh;\r
- UINT16 Tail;\r
-\r
- if (FfsHeader->Attributes & FFS_ATTRIB_TAIL_PRESENT) {\r
- //\r
- // Insert tail here, since tail may not aligned on an even\r
- // address, we need to do byte operation here.\r
- //\r
- Tail = (UINT16)~FfsHeader->IntegrityCheck.TailReference;\r
- TailLow = (UINT8) Tail;\r
- TailHigh = (UINT8) (Tail >> 8);\r
- *((UINT8 *) FfsHeader + ActualFileSize - 2) = TailLow;\r
- *((UINT8 *) FfsHeader + ActualFileSize - 1) = TailHigh;\r
- }\r
-\r
- return ;\r
-}\r
-\r
-STATUS\r
-main (\r
- IN INTN argc,\r
- IN CHAR8 **argv\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Main function.\r
-\r
-Arguments:\r
-\r
- argc - Number of command line parameters.\r
- argv - Array of pointers to parameter strings.\r
-\r
-Returns:\r
- STATUS_SUCCESS - Utility exits successfully.\r
- STATUS_ERROR - Some error occurred during execution.\r
-\r
---*/\r
-{\r
- FILE *FpIn;\r
- FILE *FpOut;\r
- UINT32 FvrecoveryFileSize;\r
- UINT8 *FileBuffer;\r
- UINT8 *FileBufferRaw;\r
- UINT64 FvLength;\r
- UINT32 Offset;\r
- UINT32 FileLength;\r
- UINT32 FileOccupiedSize;\r
- EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
- EFI_FFS_FILE_HEADER *FileHeader;\r
- EFI_GUID *TempGuid;\r
- UINT8 *FixPoint;\r
- UINT32 TempResult;\r
- UINT32 Index;\r
- UINT32 IpiVector;\r
- STATUS Status;\r
-\r
- TempGuid = NULL;\r
- SetUtilityName (UTILITY_NAME);\r
-\r
- if (argc == 1) {\r
- Usage();\r
- return STATUS_ERROR;\r
- }\r
- \r
- if ((strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0) ||\r
- (strcmp(argv[1], "-?") == 0) || (strcmp(argv[1], "/?") == 0)) {\r
- Usage();\r
- return STATUS_ERROR;\r
- }\r
- \r
- if ((strcmp(argv[1], "-V") == 0) || (strcmp(argv[1], "--version") == 0)) {\r
- Version();\r
- return STATUS_ERROR;\r
- }\r
- \r
- //\r
- // Verify the correct number of arguments\r
- //\r
- if (argc != MAX_ARGS) {\r
- Error (NULL, 0, 0, "invalid number of input parameters specified", NULL);\r
- Usage ();\r
- return STATUS_ERROR;\r
- }\r
- //\r
- // Open the Input Fvrecovery.fv file\r
- //\r
- if ((FpIn = fopen (argv[1], "rb")) == NULL) {\r
- Error (NULL, 0, 0, "Unable to open file", argv[1]);\r
- return STATUS_ERROR;\r
- }\r
- //\r
- // Get the Input Fvrecovery.fv file size\r
- //\r
- fseek (FpIn, 0, SEEK_END);\r
- FvrecoveryFileSize = ftell (FpIn);\r
- //\r
- // Read the contents of input file to memory buffer\r
- //\r
- FileBuffer = NULL;\r
- FileBufferRaw = NULL;\r
- FileBufferRaw = (UINT8 *) malloc (FvrecoveryFileSize + 0x10000);\r
- if (NULL == FileBufferRaw) {\r
- Error (NULL, 0, 0, "No sufficient memory to allocate!", NULL);\r
- fclose (FpIn);\r
- return STATUS_ERROR;\r
- }\r
- TempResult = 0x10000 - ((UINT32)FileBufferRaw & 0x0FFFF);\r
- FileBuffer = (UINT8 *)((UINT32)FileBufferRaw + TempResult);\r
- fseek (FpIn, 0, SEEK_SET);\r
- TempResult = fread (FileBuffer, 1, FvrecoveryFileSize, FpIn);\r
- if (TempResult != FvrecoveryFileSize) {\r
- Error (NULL, 0, 0, "Read input file error!", NULL);\r
- free ((VOID *)FileBufferRaw);\r
- fclose (FpIn);\r
- return STATUS_ERROR;\r
- }\r
- \r
- //\r
- // Prepare to walk the FV image\r
- //\r
- InitializeFvLib (FileBuffer, FvrecoveryFileSize);\r
- \r
- //\r
- // Close the input Fvrecovery.fv file\r
- //\r
- fclose (FpIn);\r
- \r
- //\r
- // Find the pad FFS file\r
- //\r
- FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)FileBuffer;\r
- FvLength = FvHeader->FvLength;\r
- FileHeader = (EFI_FFS_FILE_HEADER *)(FileBuffer + FvHeader->HeaderLength);\r
- FileLength = (*(UINT32 *)(FileHeader->Size)) & 0x00FFFFFF;\r
- FileOccupiedSize = GETOCCUPIEDSIZE(FileLength, 8);\r
- Offset = (UINT32)FileHeader - (UINT32)FileBuffer;\r
- \r
- while (Offset < FvLength) {\r
- TempGuid = (EFI_GUID *)&(FileHeader->Name);\r
- FileLength = (*(UINT32 *)(FileHeader->Size)) & 0x00FFFFFF;\r
- FileOccupiedSize = GETOCCUPIEDSIZE(FileLength, 8);\r
- if ((CompareGuid (TempGuid, (EFI_GUID *)&DefaultFvPadFileNameGuid)) == 0) {\r
- break;\r
- }\r
- FileHeader = (EFI_FFS_FILE_HEADER *)((UINT32)FileHeader + FileOccupiedSize);\r
- Offset = (UINT32)FileHeader - (UINT32)FileBuffer;\r
- }\r
-\r
- if (Offset >= FvLength) {\r
- Error (NULL, 0, 0, "No pad file found!", NULL);\r
- free ((VOID *)FileBufferRaw);\r
- return STATUS_ERROR;\r
- }\r
- //\r
- // Find the position to place Ap reset vector, the offset\r
- // between the position and the end of Fvrecovery.fv file\r
- // should not exceed 128kB to prevent Ap reset vector from\r
- // outside legacy E and F segment\r
- //\r
- FixPoint = (UINT8 *)(FileHeader + sizeof(EFI_FFS_FILE_HEADER));\r
- TempResult = 0x1000 - ((UINT32)FixPoint & 0x0FFF);\r
- FixPoint +=TempResult;\r
- if (((UINT32)FixPoint - (UINT32)FileHeader + 5) > FileOccupiedSize) {\r
- Error (NULL, 0, 0, "No appropriate space in pad file to add Ap reset vector!", NULL);\r
- free ((VOID *)FileBufferRaw);\r
- return STATUS_ERROR; \r
- }\r
- while (((UINT32)FixPoint - (UINT32)FileHeader + 5) <= FileOccupiedSize) {\r
- FixPoint += 0x1000;\r
- }\r
- FixPoint -= 0x1000;\r
- if ((UINT32)FvHeader + FvLength - (UINT32)FixPoint > 0x20000) {\r
- Error (NULL, 0, 0, "The position to place Ap reset vector is not in E and F segment!", NULL);\r
- free ((VOID *)FileBufferRaw);\r
- return STATUS_ERROR; \r
- }\r
-\r
- //\r
- // Fix up Ap reset vector and calculate the IPI vector\r
- //\r
- for (Index = 0; Index < 5; Index++) {\r
- FixPoint[Index] = ApResetVector[Index];\r
- } \r
- TempResult = 0x0FFFFFFFF - ((UINT32)FvHeader + (UINT32)FvLength - 1 - (UINT32)FixPoint);\r
- TempResult >>= 12;\r
- IpiVector = TempResult & 0x0FF;\r
- \r
- //\r
- // Update Pad file and checksum\r
- //\r
- UpdatePadFileGuid (FvHeader, FileHeader, FileLength, TempGuid);\r
- \r
- //\r
- // Get FileHeader of SEC Ffs\r
- //\r
- Status = GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE, 1, &FileHeader);\r
- \r
- //\r
- // Write IPI Vector at Offset FvrecoveryFileSize - 8\r
- //\r
- *(UINT32 *)((UINTN)(FileBuffer + FvrecoveryFileSize - 8)) = IpiVector;\r
-\r
- if (Status == STATUS_SUCCESS) {\r
- FileLength = (*(UINT32 *)(FileHeader->Size)) & 0x00FFFFFF;\r
- //\r
- // Update the Checksum of SEC ffs\r
- //\r
- SetHeaderChecksum (FileHeader);\r
- SetFileChecksum (FileHeader, FileLength);\r
- SetFileTail (FileHeader, FileLength);\r
- } else {\r
- Error (NULL, 0, 0, "Do not get SEC FFS File Header!", NULL);\r
- }\r
- //\r
- // Open the output Fvrecovery.fv file\r
- //\r
- if ((FpOut = fopen (argv[2], "w+b")) == NULL) {\r
- Error (NULL, 0, 0, "Unable to open file", argv[2]);\r
- free ((VOID *)FileBufferRaw);\r
- return STATUS_ERROR;\r
- }\r
- //\r
- // Write the output Fvrecovery.fv file\r
- //\r
- if ((fwrite (FileBuffer, 1, FvrecoveryFileSize, FpOut)) != FvrecoveryFileSize) {\r
- Error (NULL, 0, 0, "Write output file error!", NULL);\r
- free ((VOID *)FileBufferRaw);\r
- return STATUS_ERROR; \r
- }\r
-\r
- //\r
- // Close the output Fvrecovery.fv file\r
- //\r
- fclose (FpOut);\r
- free ((VOID *)FileBufferRaw);\r
- return STATUS_SUCCESS;\r
-}\r
-\r