3 Copyright (c) 1999 - 2002 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.
45 #define TOOLVERSION "0.1"
58 GC_TODO: Add function description
62 ActualSize - GC_TODO: add argument description
63 Alignment - GC_TODO: add argument description
67 GC_TODO: add return values
73 OccupiedSize
= ActualSize
;
74 while ((OccupiedSize
& (Alignment
- 1)) != 0) {
90 Reads in Firmware Volume information from the volume header file.
94 In: Firmware Volume header file to read from
95 FvSize: Size of Firmware Volume
99 int: Number of bytes read
103 EFI_FIRMWARE_VOLUME_HEADER VolumeHeader
;
104 EFI_FV_BLOCK_MAP_ENTRY BlockMap
;
115 printf ("Error: Input file is NULL.\n");
119 fread (&VolumeHeader
, sizeof (EFI_FIRMWARE_VOLUME_HEADER
) - sizeof (EFI_FV_BLOCK_MAP_ENTRY
), 1, In
);
120 bytesread
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
) - sizeof (EFI_FV_BLOCK_MAP_ENTRY
);
121 SigTemp
[0] = VolumeHeader
.Signature
;
124 if (VolumeHeader
.Attributes
& EFI_FVB_ERASE_POLARITY
) {
129 fread (&BlockMap
, sizeof (EFI_FV_BLOCK_MAP_ENTRY
), 1, In
);
130 bytesread
+= sizeof (EFI_FV_BLOCK_MAP_ENTRY
);
132 if (BlockMap
.NumBlocks
!= 0) {
133 size
+= BlockMap
.NumBlocks
* BlockMap
.BlockLength
;
136 } while (BlockMap
.NumBlocks
!= 0);
156 Converts a UINT8[3] array to a UINT32
160 Length A pointer to a 3 byte array
168 return *Length
& 0x00FFFFFF;
181 GC_TODO: Add function description
185 FvImage - GC_TODO: add argument description
186 bytes - GC_TODO: add argument description
187 Invert - GC_TODO: add argument description
191 GC_TODO: add return values
196 UINT32 OccupiedFileLength
;
197 EFI_FFS_FILE_HEADER
*FileHeader
;
201 UINT32 SectionLength
;
202 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
203 EFI_IMAGE_NT_HEADERS
*PeHeader
;
204 UINT32 PeiCoreOffset
;
206 Ptr
= FvImage
+ bytes
;
208 FileHeader
= (EFI_FFS_FILE_HEADER
*) Ptr
;
210 FileState
= GetFileState ((UINT8
) Invert
, FileHeader
);
213 case EFI_FILE_HEADER_CONSTRUCTION
:
214 case EFI_FILE_HEADER_INVALID
:
215 return sizeof (EFI_FFS_FILE_HEADER
);
217 case EFI_FILE_HEADER_VALID
:
218 Checksum
= CalculateSum8 ((UINT8
*) FileHeader
, sizeof (EFI_FFS_FILE_HEADER
));
219 Checksum
= (UINT8
) (Checksum
- FileHeader
->IntegrityCheck
.Checksum
.File
);
220 Checksum
= (UINT8
) (Checksum
- FileHeader
->State
);
225 // Now do the fixup stuff - begin
227 if (FileHeader
->Type
== EFI_FV_FILETYPE_PEI_CORE
) {
228 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) FileHeader
+ sizeof (EFI_FFS_FILE_HEADER
);
229 SectionLength
= GetSectionLength ((UINT32
*) &SectionHeader
->Size
[0]);
231 printf ("Section length is 0x%X\n", SectionLength
);
233 if (SectionHeader
->Type
== EFI_SECTION_PE32
) {
235 gFixup
= bytes
+ sizeof (EFI_FFS_FILE_HEADER
) + sizeof (EFI_COMMON_SECTION_HEADER
);
237 PeHeader
= (EFI_IMAGE_NT_HEADERS
*) Ptr
+ sizeof (EFI_FFS_FILE_HEADER
) + sizeof (EFI_COMMON_SECTION_HEADER
);
239 if (((EFI_IMAGE_DOS_HEADER
*) PeHeader
)->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
241 // DOS image header is present, so read the PE header after the DOS image header
243 PeHeader
= (EFI_IMAGE_NT_HEADERS
*) ((UINTN
) PeHeader
+ (UINTN
) ((((EFI_IMAGE_DOS_HEADER
*) PeHeader
)->e_lfanew
) & 0x0ffff));
247 PeiCoreOffset
= (UINTN
) ((UINTN
) (PeHeader
->OptionalHeader
.AddressOfEntryPoint
& 0x0ffffffff));
249 gFixup
+= PeiCoreOffset
;
253 FileLength
= GetLength (FileHeader
->Size
);
254 OccupiedFileLength
= GetOccupiedSize (FileLength
, 8);
255 return OccupiedFileLength
;
257 case EFI_FILE_DATA_VALID
:
259 // Calculate header checksum
261 Checksum
= CalculateSum8 ((UINT8
*) FileHeader
, sizeof (EFI_FFS_FILE_HEADER
));
262 Checksum
= (UINT8
) (Checksum
- FileHeader
->IntegrityCheck
.Checksum
.File
);
263 Checksum
= (UINT8
) (Checksum
- FileHeader
->State
);
268 // Determine file length
270 FileLength
= GetLength (FileHeader
->Size
);
271 OccupiedFileLength
= GetOccupiedSize (FileLength
, 8);
274 // Determine if file checksum is valid or fixed
276 if (FileHeader
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
277 Checksum
= CalculateSum8 (Ptr
, FileLength
);
278 Checksum
= (UINT8
) (Checksum
- FileHeader
->State
);
283 if (FileHeader
->IntegrityCheck
.Checksum
.File
!= FFS_FIXED_CHECKSUM
) {
289 case EFI_FILE_MARKED_FOR_UPDATE
:
290 case EFI_FILE_DELETED
:
292 // Calculate header checksum
294 Checksum
= CalculateSum8 ((UINT8
*) FileHeader
, sizeof (EFI_FFS_FILE_HEADER
));
295 Checksum
= (UINT8
) (Checksum
- FileHeader
->IntegrityCheck
.Checksum
.File
);
296 Checksum
= (UINT8
) (Checksum
- FileHeader
->State
);
301 // Determine file length
303 FileLength
= GetLength (FileHeader
->Size
);
304 OccupiedFileLength
= GetOccupiedSize (FileLength
, 8);
307 // Determine if file checksum is valid or fixed
309 if (FileHeader
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
310 Checksum
= CalculateSum8 (Ptr
, FileLength
);
311 Checksum
= (UINT8
) (Checksum
- FileHeader
->State
);
316 if (FileHeader
->IntegrityCheck
.Checksum
.File
!= FFS_FIXED_CHECKSUM
) {
321 return OccupiedFileLength
;
324 return sizeof (EFI_FFS_FILE_HEADER
);
327 return OccupiedFileLength
;
343 argc: number of command line arguments
345 arg[0] = This file name
346 arg[1] = Firmware Volume Name
347 arg[2] = Base Address to relocate
348 arg[3] = Relative offset of the fixup to perform
349 arg[4] = Output File Name
353 int: 0 code success, -1 code failure
374 "GenBsfFixup EFI 2.0. Version %s, %s\n""Copyright (c) 2000-2001, Intel Corporation\n\n",
380 printf ("Usage:\n"" GenBsfFixup FvVolumeImageFile AddressOfFvInMemory OffsetOfFixup OutputFileName\n");
384 In
= fopen (argv
[1], "rb");
387 printf ("Unable to open FV image file \"%s\"\n", argv
[1]);
391 ByteStart
= ReadHeader (In
, &FvSize
);
398 FvImage
= malloc (FvSize
);
399 if (FvImage
== NULL
) {
400 printf ("Cannot allocate memory\n");
405 ByteRead
= fread (FvImage
, 1, FvSize
, In
);
407 if ((unsigned int) ByteRead
!= FvSize
) {
408 printf ("Read File error\n");
414 while ((unsigned int) ByteStart
< FvSize
&& cnt
!= -1) {
415 cnt
= Readfile (FvImage
, ByteStart
, Invert
);
421 if (cnt
!= sizeof (EFI_FFS_FILE_HEADER
)) {
427 printf ("Firmware Volume image corrupted\n");
433 Out
= fopen (argv
[4], "wb");
436 printf ("Unable to open FV image file \"%s\"\n", argv
[4]);
440 In
= fopen (argv
[1], "rb");
443 printf ("Unable to open FV image file \"%s\"\n", argv
[1]);
449 printf ("Fixup of 0x%X\n", gFixup
);
451 Status
= AsciiStringToUint64 (argv
[2], TRUE
, &FvOffset
);
453 gFixup
+= (UINT32
) FvOffset
;
455 ByteStart
= ReadHeader (In
, &FvSize
);
457 Readfile (FvImage
, ByteStart
, Invert
);
460 Status
= AsciiStringToUint64 (argv
[3], TRUE
, &delta
);
463 In
= fopen (argv
[1], "rb");
466 printf ("Unable to open FV image file \"%s\"\n", argv
[1]);
470 for (Idx
= 0; Idx
< delta
- FvOffset
; Idx
++) {
471 fputc (fgetc (In
), Out
);
474 fwrite (&gFixup
, sizeof (UINT32
), 1, Out
);
475 fseek (In
, sizeof (UINT32
), SEEK_CUR
);
477 for (Idx
= 0; Idx
< FvSize
- (delta
- FvOffset
) - sizeof (UINT32
); Idx
++) {
478 fputc (fgetc (In
), Out
);
484 printf ("There was no fixup to perform\n");