3 Copyright (c) 1999 - 2006 Intel Corporation. All rights reserved
4 This software and associated documentation (if any) is furnished
5 under a license and may only be used or copied in accordance
6 with the terms of the license. Except as permitted by such
7 license, no part of this software or documentation may be
8 reproduced, stored in a retrieval system, or transmitted in any
9 form or by any means without the express written consent of
19 Utility to Fixup the SEC component for IA32. This is an
20 interim tool in place of the full GenBsfImage support for
22 This tool supports the Synch3 update
26 #include "BaseTypes.h"
27 #include "UefiBaseTypes.h"
29 #include "FirmwareVolumeHeader.h"
30 #include "FirmwareVolumeImageFormat.h"
32 #include "CommonLib.h"
33 #include "FirmwareFileSystem.h"
41 // BugBug -- this port to the new FFS is really weird.
42 // A lot of the file-header stuff has been ported, but
43 // not the section information.
46 #define UTILITY_NAME "GenBsfFixup"
47 #define UTILITY_MAJOR_VERSION 0
48 #define UTILITY_MINOR_VERSION 1
61 GC_TODO: Add function description
65 ActualSize - GC_TODO: add argument description
66 Alignment - GC_TODO: add argument description
70 GC_TODO: add return values
76 OccupiedSize
= ActualSize
;
77 while ((OccupiedSize
& (Alignment
- 1)) != 0) {
90 printf ("%s v%d.%d -EDK Utility to Fixup the SEC component for IA32.\n", UTILITY_NAME
, UTILITY_MAJOR_VERSION
, UTILITY_MINOR_VERSION
);
91 printf ("Copyright (c) 1999-2006 Intel Corporation. All rights reserved.\n");
101 printf ("\nUsage: " UTILITY_NAME
" FvVolumeImageFile AddressOfFvInMemory OffsetOfFixup OutputFileName \n");
113 Reads in Firmware Volume information from the volume header file.
117 In: Firmware Volume header file to read from
118 FvSize: Size of Firmware Volume
122 int: Number of bytes read
126 EFI_FIRMWARE_VOLUME_HEADER VolumeHeader
;
127 EFI_FV_BLOCK_MAP_ENTRY BlockMap
;
138 printf ("Error: Input file is NULL.\n");
142 fread (&VolumeHeader
, sizeof (EFI_FIRMWARE_VOLUME_HEADER
) - sizeof (EFI_FV_BLOCK_MAP_ENTRY
), 1, In
);
143 bytesread
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
) - sizeof (EFI_FV_BLOCK_MAP_ENTRY
);
144 SigTemp
[0] = VolumeHeader
.Signature
;
147 if (VolumeHeader
.Attributes
& EFI_FVB_ERASE_POLARITY
) {
152 fread (&BlockMap
, sizeof (EFI_FV_BLOCK_MAP_ENTRY
), 1, In
);
153 bytesread
+= sizeof (EFI_FV_BLOCK_MAP_ENTRY
);
155 if (BlockMap
.NumBlocks
!= 0) {
156 size
+= BlockMap
.NumBlocks
* BlockMap
.BlockLength
;
159 } while (BlockMap
.NumBlocks
!= 0);
179 Converts a UINT8[3] array to a UINT32
183 Length A pointer to a 3 byte array
191 return *Length
& 0x00FFFFFF;
204 GC_TODO: Add function description
208 FvImage - GC_TODO: add argument description
209 bytes - GC_TODO: add argument description
210 Invert - GC_TODO: add argument description
214 GC_TODO: add return values
219 UINT32 OccupiedFileLength
;
220 EFI_FFS_FILE_HEADER
*FileHeader
;
224 UINT32 SectionLength
;
225 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
226 EFI_IMAGE_NT_HEADERS
*PeHeader
;
227 UINT32 PeiCoreOffset
;
229 Ptr
= FvImage
+ bytes
;
231 FileHeader
= (EFI_FFS_FILE_HEADER
*) Ptr
;
233 FileState
= GetFileState ((UINT8
) Invert
, FileHeader
);
236 case EFI_FILE_HEADER_CONSTRUCTION
:
237 case EFI_FILE_HEADER_INVALID
:
238 return sizeof (EFI_FFS_FILE_HEADER
);
240 case EFI_FILE_HEADER_VALID
:
241 Checksum
= CalculateSum8 ((UINT8
*) FileHeader
, sizeof (EFI_FFS_FILE_HEADER
));
242 Checksum
= (UINT8
) (Checksum
- FileHeader
->IntegrityCheck
.Checksum
.File
);
243 Checksum
= (UINT8
) (Checksum
- FileHeader
->State
);
248 // Now do the fixup stuff - begin
250 if (FileHeader
->Type
== EFI_FV_FILETYPE_PEI_CORE
) {
251 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) FileHeader
+ sizeof (EFI_FFS_FILE_HEADER
);
252 SectionLength
= GetSectionLength ((UINT32
*) &SectionHeader
->Size
[0]);
254 printf ("Section length is 0x%X\n", SectionLength
);
256 if (SectionHeader
->Type
== EFI_SECTION_PE32
) {
258 gFixup
= bytes
+ sizeof (EFI_FFS_FILE_HEADER
) + sizeof (EFI_COMMON_SECTION_HEADER
);
260 PeHeader
= (EFI_IMAGE_NT_HEADERS
*) Ptr
+ sizeof (EFI_FFS_FILE_HEADER
) + sizeof (EFI_COMMON_SECTION_HEADER
);
262 if (((EFI_IMAGE_DOS_HEADER
*) PeHeader
)->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
264 // DOS image header is present, so read the PE header after the DOS image header
266 PeHeader
= (EFI_IMAGE_NT_HEADERS
*) ((UINTN
) PeHeader
+ (UINTN
) ((((EFI_IMAGE_DOS_HEADER
*) PeHeader
)->e_lfanew
) & 0x0ffff));
270 PeiCoreOffset
= (UINTN
) ((UINTN
) (PeHeader
->OptionalHeader
.AddressOfEntryPoint
& 0x0ffffffff));
272 gFixup
+= PeiCoreOffset
;
276 FileLength
= GetLength (FileHeader
->Size
);
277 OccupiedFileLength
= GetOccupiedSize (FileLength
, 8);
278 return OccupiedFileLength
;
280 case EFI_FILE_DATA_VALID
:
282 // Calculate header checksum
284 Checksum
= CalculateSum8 ((UINT8
*) FileHeader
, sizeof (EFI_FFS_FILE_HEADER
));
285 Checksum
= (UINT8
) (Checksum
- FileHeader
->IntegrityCheck
.Checksum
.File
);
286 Checksum
= (UINT8
) (Checksum
- FileHeader
->State
);
291 // Determine file length
293 FileLength
= GetLength (FileHeader
->Size
);
294 OccupiedFileLength
= GetOccupiedSize (FileLength
, 8);
297 // Determine if file checksum is valid or fixed
299 if (FileHeader
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
300 Checksum
= CalculateSum8 (Ptr
, FileLength
);
301 Checksum
= (UINT8
) (Checksum
- FileHeader
->State
);
306 if (FileHeader
->IntegrityCheck
.Checksum
.File
!= FFS_FIXED_CHECKSUM
) {
312 case EFI_FILE_MARKED_FOR_UPDATE
:
313 case EFI_FILE_DELETED
:
315 // Calculate header checksum
317 Checksum
= CalculateSum8 ((UINT8
*) FileHeader
, sizeof (EFI_FFS_FILE_HEADER
));
318 Checksum
= (UINT8
) (Checksum
- FileHeader
->IntegrityCheck
.Checksum
.File
);
319 Checksum
= (UINT8
) (Checksum
- FileHeader
->State
);
324 // Determine file length
326 FileLength
= GetLength (FileHeader
->Size
);
327 OccupiedFileLength
= GetOccupiedSize (FileLength
, 8);
330 // Determine if file checksum is valid or fixed
332 if (FileHeader
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
333 Checksum
= CalculateSum8 (Ptr
, FileLength
);
334 Checksum
= (UINT8
) (Checksum
- FileHeader
->State
);
339 if (FileHeader
->IntegrityCheck
.Checksum
.File
!= FFS_FIXED_CHECKSUM
) {
344 return OccupiedFileLength
;
347 return sizeof (EFI_FFS_FILE_HEADER
);
350 return OccupiedFileLength
;
366 argc: number of command line arguments
368 arg[0] = This file name
369 arg[1] = Firmware Volume Name
370 arg[2] = Base Address to relocate
371 arg[3] = Relative offset of the fixup to perform
372 arg[4] = Output File Name
376 int: 0 code success, -1 code failure
402 if ((strcmp(argv
[1], "-h") == 0) || (strcmp(argv
[1], "--help") == 0) ||
403 (strcmp(argv
[1], "-?") == 0) || (strcmp(argv
[1], "/?") == 0)) {
408 if ((strcmp(argv
[1], "-V") == 0) || (strcmp(argv
[1], "--version") == 0)) {
418 In
= fopen (argv
[1], "rb");
421 printf ("Unable to open FV image file \"%s\"\n", argv
[1]);
425 ByteStart
= ReadHeader (In
, &FvSize
);
432 FvImage
= malloc (FvSize
);
433 if (FvImage
== NULL
) {
434 printf ("Cannot allocate memory\n");
439 ByteRead
= fread (FvImage
, 1, FvSize
, In
);
441 if ((unsigned int) ByteRead
!= FvSize
) {
442 printf ("Read File error\n");
448 while ((unsigned int) ByteStart
< FvSize
&& cnt
!= -1) {
449 cnt
= Readfile (FvImage
, ByteStart
, Invert
);
455 if (cnt
!= sizeof (EFI_FFS_FILE_HEADER
)) {
461 printf ("Firmware Volume image corrupted\n");
467 Out
= fopen (argv
[4], "wb");
470 printf ("Unable to open FV image file \"%s\"\n", argv
[4]);
474 In
= fopen (argv
[1], "rb");
477 printf ("Unable to open FV image file \"%s\"\n", argv
[1]);
483 printf ("Fixup of 0x%X\n", gFixup
);
485 Status
= AsciiStringToUint64 (argv
[2], TRUE
, &FvOffset
);
487 gFixup
+= (UINT32
) FvOffset
;
489 ByteStart
= ReadHeader (In
, &FvSize
);
491 Readfile (FvImage
, ByteStart
, Invert
);
494 Status
= AsciiStringToUint64 (argv
[3], TRUE
, &delta
);
497 In
= fopen (argv
[1], "rb");
500 printf ("Unable to open FV image file \"%s\"\n", argv
[1]);
504 for (Idx
= 0; Idx
< delta
- FvOffset
; Idx
++) {
505 fputc (fgetc (In
), Out
);
508 fwrite (&gFixup
, sizeof (UINT32
), 1, Out
);
509 fseek (In
, sizeof (UINT32
), SEEK_CUR
);
511 for (Idx
= 0; Idx
< FvSize
- (delta
- FvOffset
) - sizeof (UINT32
); Idx
++) {
512 fputc (fgetc (In
), Out
);
518 printf ("There was no fixup to perform\n");