4 Copyright (c) 2004, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 This file contains functions required to generate a Firmware Volume.
26 #define EFI_SPECIFICATION_VERSION 0x00020000
27 #define EDK_RELEASE_VERSION 0x00020000
29 #include <uuid/uuid.h>
38 #include <Library/PeCoffLib.h>
39 #include "GenFvImageLib.h"
40 #include "GenFvImageLibInternal.h"
41 #include <CommonLib.h>
42 #include <FirmwareVolumeImageFormat.h>
44 #include <EfiUtilityMsgs.h>
46 #include <EfiCompress.h>
48 #include <WinNtInclude.h>
49 #include <WorkingBlockHeader.h>
53 // Local function prototypes
58 OUT UINT32
*EntryPoint
,
59 OUT UINT32
*BaseOfCode
,
60 OUT UINT16
*MachineType
64 // Local function implementations.
66 EFI_GUID FfsGuid
= EFI_FIRMWARE_FILE_SYSTEM_GUID
;
67 EFI_GUID DefaultFvPadFileNameGuid
= { 0x78f54d4, 0xcc22, 0x4048, 0x9e, 0x94, 0x87, 0x9c, 0x21, 0x4d, 0x56, 0x2f };
70 // This data array will be located at the base of the Firmware Volume Header (FVH)
71 // in the boot block. It must not exceed 14 bytes of code. The last 2 bytes
72 // will be used to keep the FVH checksum consistent.
73 // This code will be run in response to a starutp IPI for HT-enabled systems.
75 #define SIZEOF_STARTUP_DATA_ARRAY 0x10
77 UINT8 m128kRecoveryStartupApDataArray
[SIZEOF_STARTUP_DATA_ARRAY
] = {
79 // EA D0 FF 00 F0 ; far jmp F000:FFD0
80 // 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
81 // 0, 0 ; Checksum Padding
101 UINT8 m64kRecoveryStartupApDataArray
[SIZEOF_STARTUP_DATA_ARRAY
] = {
103 // EB CE ; jmp short ($-0x30)
104 // ; (from offset 0x0 to offset 0xFFD0)
105 // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
106 // 0, 0 ; Checksum Padding
128 IN MEMORY_FILE
*InfFile
,
135 This function parses a FV.INF file and copies info into a FV_INFO structure.
139 InfFile Memory file image.
140 FvInfo Information read from INF file.
144 EFI_SUCCESS INF file information successfully retrieved.
145 EFI_ABORTED INF file has an invalid format.
146 EFI_NOT_FOUND A required string was not found in the INF file.
149 CHAR8 Value
[_MAX_PATH
];
155 // Initialize FV info
157 memset (FvInfo
, 0, sizeof (FV_INFO
));
160 // Read the FV base address
162 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_BASE_ADDRESS_STRING
, 0, Value
);
164 if (Status
== EFI_SUCCESS
) {
166 // Get the base address
168 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
169 if (EFI_ERROR (Status
)) {
170 Error (NULL
, 0, 0, EFI_FV_BASE_ADDRESS_STRING
, "invalid value");
174 FvInfo
->BaseAddress
= Value64
;
176 Error (NULL
, 0, 0, EFI_FV_BASE_ADDRESS_STRING
, "could not find value");
182 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_GUID_STRING
, 0, Value
);
184 if (Status
== EFI_SUCCESS
) {
186 // Get the guid value
188 Status
= StringToGuid (Value
, &FvInfo
->FvGuid
);
189 if (EFI_ERROR (Status
)) {
190 memcpy (&FvInfo
->FvGuid
, &FfsGuid
, sizeof (EFI_GUID
));
193 memcpy (&FvInfo
->FvGuid
, &FfsGuid
, sizeof (EFI_GUID
));
196 // Read the FV file name
198 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_FILE_NAME_STRING
, 0, Value
);
200 if (Status
== EFI_SUCCESS
) {
202 // copy the file name
204 strcpy (FvInfo
->FvName
, Value
);
206 Error (NULL
, 0, 0, EFI_FV_FILE_NAME_STRING
, "value not specified");
210 // Read the Sym file name
212 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_SYM_FILE_NAME_STRING
, 0, Value
);
214 if (Status
== EFI_SUCCESS
) {
216 // copy the file name
218 strcpy (FvInfo
->SymName
, Value
);
221 // Symbols not required, so init to NULL.
223 strcpy (FvInfo
->SymName
, "");
226 // Read the read disabled capability attribute
228 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_READ_DISABLED_CAP_STRING
, 0, Value
);
230 if (Status
== EFI_SUCCESS
) {
232 // Update the read disabled flag
234 if (strcmp (Value
, TRUE_STRING
) == 0) {
235 FvInfo
->FvAttributes
|= EFI_FVB_READ_DISABLED_CAP
;
236 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
237 Error (NULL
, 0, 0, EFI_FVB_READ_DISABLED_CAP_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
241 Error (NULL
, 0, 0, EFI_FVB_READ_DISABLED_CAP_STRING
, "value not specified");
245 // Read the read enabled capability attribute
247 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_READ_ENABLED_CAP_STRING
, 0, Value
);
249 if (Status
== EFI_SUCCESS
) {
251 // Update the read disabled flag
253 if (strcmp (Value
, TRUE_STRING
) == 0) {
254 FvInfo
->FvAttributes
|= EFI_FVB_READ_ENABLED_CAP
;
255 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
256 Error (NULL
, 0, 0, EFI_FVB_READ_ENABLED_CAP_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
260 Error (NULL
, 0, 0, EFI_FVB_READ_ENABLED_CAP_STRING
, "value not specified");
264 // Read the read status attribute
266 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_READ_STATUS_STRING
, 0, Value
);
268 if (Status
== EFI_SUCCESS
) {
270 // Update the read disabled flag
272 if (strcmp (Value
, TRUE_STRING
) == 0) {
273 FvInfo
->FvAttributes
|= EFI_FVB_READ_STATUS
;
274 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
275 Error (NULL
, 0, 0, EFI_FVB_READ_STATUS_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
279 Error (NULL
, 0, 0, EFI_FVB_READ_STATUS_STRING
, "value not specified");
283 // Read the write disabled capability attribute
285 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_WRITE_DISABLED_CAP_STRING
, 0, Value
);
287 if (Status
== EFI_SUCCESS
) {
289 // Update the write disabled flag
291 if (strcmp (Value
, TRUE_STRING
) == 0) {
292 FvInfo
->FvAttributes
|= EFI_FVB_WRITE_DISABLED_CAP
;
293 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
294 Error (NULL
, 0, 0, EFI_FVB_WRITE_DISABLED_CAP_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
298 Error (NULL
, 0, 0, EFI_FVB_WRITE_DISABLED_CAP_STRING
, "value not specified");
302 // Read the write enabled capability attribute
304 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_WRITE_ENABLED_CAP_STRING
, 0, Value
);
306 if (Status
== EFI_SUCCESS
) {
308 // Update the write disabled flag
310 if (strcmp (Value
, TRUE_STRING
) == 0) {
311 FvInfo
->FvAttributes
|= EFI_FVB_WRITE_ENABLED_CAP
;
312 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
313 Error (NULL
, 0, 0, EFI_FVB_WRITE_ENABLED_CAP_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
317 Error (NULL
, 0, 0, EFI_FVB_WRITE_ENABLED_CAP_STRING
, "value not specified");
321 // Read the write status attribute
323 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_WRITE_STATUS_STRING
, 0, Value
);
325 if (Status
== EFI_SUCCESS
) {
327 // Update the write disabled flag
329 if (strcmp (Value
, TRUE_STRING
) == 0) {
330 FvInfo
->FvAttributes
|= EFI_FVB_WRITE_STATUS
;
331 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
332 Error (NULL
, 0, 0, EFI_FVB_WRITE_STATUS_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
336 Error (NULL
, 0, 0, EFI_FVB_WRITE_STATUS_STRING
, "value not specified");
340 // Read the lock capability attribute
342 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_LOCK_CAP_STRING
, 0, Value
);
344 if (Status
== EFI_SUCCESS
) {
346 // Update the attribute flag
348 if (strcmp (Value
, TRUE_STRING
) == 0) {
349 FvInfo
->FvAttributes
|= EFI_FVB_LOCK_CAP
;
350 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
351 Error (NULL
, 0, 0, EFI_FVB_LOCK_CAP_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
355 Error (NULL
, 0, 0, EFI_FVB_LOCK_CAP_STRING
, "value not specified");
359 // Read the lock status attribute
361 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_LOCK_STATUS_STRING
, 0, Value
);
363 if (Status
== EFI_SUCCESS
) {
365 // Update the attribute flag
367 if (strcmp (Value
, TRUE_STRING
) == 0) {
368 FvInfo
->FvAttributes
|= EFI_FVB_LOCK_STATUS
;
369 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
370 Error (NULL
, 0, 0, EFI_FVB_LOCK_STATUS_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
374 Error (NULL
, 0, 0, EFI_FVB_LOCK_STATUS_STRING
, "value not specified");
378 // Read the sticky write attribute
380 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_STICKY_WRITE_STRING
, 0, Value
);
382 if (Status
== EFI_SUCCESS
) {
384 // Update the attribute flag
386 if (strcmp (Value
, TRUE_STRING
) == 0) {
387 FvInfo
->FvAttributes
|= EFI_FVB_STICKY_WRITE
;
388 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
389 Error (NULL
, 0, 0, EFI_FVB_STICKY_WRITE_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
393 Error (NULL
, 0, 0, EFI_FVB_STICKY_WRITE_STRING
, "value not specified");
397 // Read the memory mapped attribute
399 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_MEMORY_MAPPED_STRING
, 0, Value
);
401 if (Status
== EFI_SUCCESS
) {
403 // Update the attribute flag
405 if (strcmp (Value
, TRUE_STRING
) == 0) {
406 FvInfo
->FvAttributes
|= EFI_FVB_MEMORY_MAPPED
;
407 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
408 Error (NULL
, 0, 0, EFI_FVB_MEMORY_MAPPED_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
412 Error (NULL
, 0, 0, EFI_FVB_MEMORY_MAPPED_STRING
, "value not specified");
416 // Read the erase polarity attribute
418 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ERASE_POLARITY_STRING
, 0, Value
);
420 if (Status
== EFI_SUCCESS
) {
422 // Update the attribute flag
424 if (strcmp (Value
, ONE_STRING
) == 0) {
425 FvInfo
->FvAttributes
|= EFI_FVB_ERASE_POLARITY
;
426 } else if (strcmp (Value
, ZERO_STRING
) != 0) {
427 Error (NULL
, 0, 0, EFI_FVB_ERASE_POLARITY_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
431 Error (NULL
, 0, 0, EFI_FVB_ERASE_POLARITY_STRING
, "value not specified");
435 // Read the alignment capabilities attribute
437 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ALIGNMENT_CAP_STRING
, 0, Value
);
439 if (Status
== EFI_SUCCESS
) {
443 if (strcmp (Value
, TRUE_STRING
) == 0) {
444 FvInfo
->FvAttributes
|= EFI_FVB_ALIGNMENT_CAP
;
445 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
446 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_CAP_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
450 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_CAP_STRING
, "value not specified");
454 // Read the word alignment capability attribute
456 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ALIGNMENT_2_STRING
, 0, Value
);
458 if (Status
== EFI_SUCCESS
) {
462 if (strcmp (Value
, TRUE_STRING
) == 0) {
463 FvInfo
->FvAttributes
|= EFI_FVB_ALIGNMENT_2
;
464 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
465 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_2_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
469 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_2_STRING
, "value not specified");
473 // Read the dword alignment capability attribute
475 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ALIGNMENT_4_STRING
, 0, Value
);
477 if (Status
== EFI_SUCCESS
) {
481 if (strcmp (Value
, TRUE_STRING
) == 0) {
482 FvInfo
->FvAttributes
|= EFI_FVB_ALIGNMENT_4
;
483 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
484 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_4_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
488 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_4_STRING
, "value not specified");
492 // Read the word alignment capability attribute
494 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ALIGNMENT_8_STRING
, 0, Value
);
496 if (Status
== EFI_SUCCESS
) {
500 if (strcmp (Value
, TRUE_STRING
) == 0) {
501 FvInfo
->FvAttributes
|= EFI_FVB_ALIGNMENT_8
;
502 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
503 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_8_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
507 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_8_STRING
, "value not specified");
511 // Read the qword alignment capability attribute
513 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ALIGNMENT_16_STRING
, 0, Value
);
515 if (Status
== EFI_SUCCESS
) {
519 if (strcmp (Value
, TRUE_STRING
) == 0) {
520 FvInfo
->FvAttributes
|= EFI_FVB_ALIGNMENT_16
;
521 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
522 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_16_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
526 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_16_STRING
, "value not specified");
530 // Read the 32 byte alignment capability attribute
532 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ALIGNMENT_32_STRING
, 0, Value
);
534 if (Status
== EFI_SUCCESS
) {
538 if (strcmp (Value
, TRUE_STRING
) == 0) {
539 FvInfo
->FvAttributes
|= EFI_FVB_ALIGNMENT_32
;
540 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
541 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_32_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
545 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_32_STRING
, "value not specified");
549 // Read the 64 byte alignment capability attribute
551 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ALIGNMENT_64_STRING
, 0, Value
);
553 if (Status
== EFI_SUCCESS
) {
557 if (strcmp (Value
, TRUE_STRING
) == 0) {
558 FvInfo
->FvAttributes
|= EFI_FVB_ALIGNMENT_64
;
559 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
560 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_64_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
564 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_64_STRING
, "value not specified");
568 // Read the 128 byte alignment capability attribute
570 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ALIGNMENT_128_STRING
, 0, Value
);
572 if (Status
== EFI_SUCCESS
) {
576 if (strcmp (Value
, TRUE_STRING
) == 0) {
577 FvInfo
->FvAttributes
|= EFI_FVB_ALIGNMENT_128
;
578 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
579 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_128_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
583 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_128_STRING
, "value not specified");
587 // Read the 256 byte alignment capability attribute
589 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ALIGNMENT_256_STRING
, 0, Value
);
591 if (Status
== EFI_SUCCESS
) {
595 if (strcmp (Value
, TRUE_STRING
) == 0) {
596 FvInfo
->FvAttributes
|= EFI_FVB_ALIGNMENT_256
;
597 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
598 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_256_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
602 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_256_STRING
, "value not specified");
606 // Read the 512 byte alignment capability attribute
608 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ALIGNMENT_512_STRING
, 0, Value
);
610 if (Status
== EFI_SUCCESS
) {
614 if (strcmp (Value
, TRUE_STRING
) == 0) {
615 FvInfo
->FvAttributes
|= EFI_FVB_ALIGNMENT_512
;
616 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
617 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_512_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
621 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_512_STRING
, "value not specified");
625 // Read the 1K byte alignment capability attribute
627 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ALIGNMENT_1K_STRING
, 0, Value
);
629 if (Status
== EFI_SUCCESS
) {
633 if (strcmp (Value
, TRUE_STRING
) == 0) {
634 FvInfo
->FvAttributes
|= EFI_FVB_ALIGNMENT_1K
;
635 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
636 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_1K_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
640 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_1K_STRING
, "value not specified");
644 // Read the 2K byte alignment capability attribute
646 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ALIGNMENT_2K_STRING
, 0, Value
);
648 if (Status
== EFI_SUCCESS
) {
652 if (strcmp (Value
, TRUE_STRING
) == 0) {
653 FvInfo
->FvAttributes
|= EFI_FVB_ALIGNMENT_2K
;
654 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
655 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_2K_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
659 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_2K_STRING
, "value not specified");
663 // Read the 4K byte alignment capability attribute
665 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ALIGNMENT_4K_STRING
, 0, Value
);
667 if (Status
== EFI_SUCCESS
) {
671 if (strcmp (Value
, TRUE_STRING
) == 0) {
672 FvInfo
->FvAttributes
|= EFI_FVB_ALIGNMENT_4K
;
673 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
674 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_4K_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
678 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_4K_STRING
, "value not specified");
682 // Read the 8K byte alignment capability attribute
684 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ALIGNMENT_8K_STRING
, 0, Value
);
686 if (Status
== EFI_SUCCESS
) {
690 if (strcmp (Value
, TRUE_STRING
) == 0) {
691 FvInfo
->FvAttributes
|= EFI_FVB_ALIGNMENT_8K
;
692 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
693 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_8K_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
697 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_8K_STRING
, "value not specified");
701 // Read the 16K byte alignment capability attribute
703 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ALIGNMENT_16K_STRING
, 0, Value
);
705 if (Status
== EFI_SUCCESS
) {
709 if (strcmp (Value
, TRUE_STRING
) == 0) {
710 FvInfo
->FvAttributes
|= EFI_FVB_ALIGNMENT_16K
;
711 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
712 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_16K_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
716 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_16K_STRING
, "value not specified");
720 // Read the 32K byte alignment capability attribute
722 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ALIGNMENT_32K_STRING
, 0, Value
);
724 if (Status
== EFI_SUCCESS
) {
728 if (strcmp (Value
, TRUE_STRING
) == 0) {
729 FvInfo
->FvAttributes
|= EFI_FVB_ALIGNMENT_32K
;
730 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
731 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_32K_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
735 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_32K_STRING
, "value not specified");
739 // Read the 64K byte alignment capability attribute
741 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FVB_ALIGNMENT_64K_STRING
, 0, Value
);
743 if (Status
== EFI_SUCCESS
) {
747 if (strcmp (Value
, TRUE_STRING
) == 0) {
748 FvInfo
->FvAttributes
|= EFI_FVB_ALIGNMENT_64K
;
749 } else if (strcmp (Value
, FALSE_STRING
) != 0) {
750 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_64K_STRING
, "expected %s | %s", TRUE_STRING
, FALSE_STRING
);
754 Error (NULL
, 0, 0, EFI_FVB_ALIGNMENT_64K_STRING
, "value not specified");
758 if (!(FvInfo
->FvAttributes
& EFI_FVB_ALIGNMENT_CAP
) &&
760 (FvInfo
->FvAttributes
& EFI_FVB_ALIGNMENT_2
) ||
761 (FvInfo
->FvAttributes
& EFI_FVB_ALIGNMENT_4
) ||
762 (FvInfo
->FvAttributes
& EFI_FVB_ALIGNMENT_8
) ||
763 (FvInfo
->FvAttributes
& EFI_FVB_ALIGNMENT_16
) ||
764 (FvInfo
->FvAttributes
& EFI_FVB_ALIGNMENT_32
) ||
765 (FvInfo
->FvAttributes
& EFI_FVB_ALIGNMENT_64
) ||
766 (FvInfo
->FvAttributes
& EFI_FVB_ALIGNMENT_128
) ||
767 (FvInfo
->FvAttributes
& EFI_FVB_ALIGNMENT_256
) ||
768 (FvInfo
->FvAttributes
& EFI_FVB_ALIGNMENT_512
) ||
769 (FvInfo
->FvAttributes
& EFI_FVB_ALIGNMENT_1K
) ||
770 (FvInfo
->FvAttributes
& EFI_FVB_ALIGNMENT_2K
) ||
771 (FvInfo
->FvAttributes
& EFI_FVB_ALIGNMENT_4K
) ||
772 (FvInfo
->FvAttributes
& EFI_FVB_ALIGNMENT_8K
) ||
773 (FvInfo
->FvAttributes
& EFI_FVB_ALIGNMENT_16K
) ||
774 (FvInfo
->FvAttributes
& EFI_FVB_ALIGNMENT_32K
) ||
775 (FvInfo
->FvAttributes
& EFI_FVB_ALIGNMENT_64K
)
782 "illegal combination of alignment attributes",
783 "if %s is not %s, no individual alignments can be %s",
784 EFI_FVB_ALIGNMENT_CAP_STRING
,
793 for (Index
= 0; Index
< MAX_NUMBER_OF_FV_BLOCKS
; Index
++) {
795 // Read the number of blocks
797 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_NUM_BLOCKS_STRING
, Index
, Value
);
799 if (Status
== EFI_SUCCESS
) {
801 // Update the number of blocks
803 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
804 if (EFI_ERROR (Status
)) {
805 Error (NULL
, 0, 0, Value
, "invalid value for %s", EFI_NUM_BLOCKS_STRING
);
809 FvInfo
->FvBlocks
[Index
].NumBlocks
= (UINT32
) Value64
;
812 // If there is no number of blocks, but there is a size, then we have a mismatched pair
813 // and should return an error.
815 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_BLOCK_SIZE_STRING
, Index
, Value
);
816 if (!EFI_ERROR (Status
)) {
817 Error (NULL
, 0, 0, "must specify both", "%s and %s", EFI_NUM_BLOCKS_STRING
, EFI_BLOCK_SIZE_STRING
);
827 // Read the size of blocks
829 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_BLOCK_SIZE_STRING
, Index
, Value
);
831 if (Status
== EFI_SUCCESS
) {
833 // Update the number of blocks
835 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
836 if (EFI_ERROR (Status
)) {
837 Error (NULL
, 0, 0, Value
, "invalid value specified for %s", EFI_BLOCK_SIZE_STRING
);
841 FvInfo
->FvBlocks
[Index
].BlockLength
= (UINT32
) Value64
;
844 // There is a number of blocks, but there is no size, so we have a mismatched pair
845 // and should return an error.
847 Error (NULL
, 0, 0, "must specify both", "%s and %s", EFI_NUM_BLOCKS_STRING
, EFI_BLOCK_SIZE_STRING
);
854 for (Index
= 0; Index
< MAX_NUMBER_OF_FILES_IN_FV
; Index
++) {
856 // Read the number of blocks
858 Status
= FindToken (InfFile
, FILES_SECTION_STRING
, EFI_FILE_NAME_STRING
, Index
, Value
);
860 if (Status
== EFI_SUCCESS
) {
864 strcpy (FvInfo
->FvFiles
[Index
], Value
);
870 if (FindSection (InfFile
, COMPONENT_SECTION_STRING
)) {
873 // Read component FV_VARIABLE
875 Status
= FindToken (InfFile
, COMPONENT_SECTION_STRING
, EFI_NV_VARIABLE_STRING
, 0, Value
);
877 if (Status
== EFI_SUCCESS
) {
881 strcpy (FvInfo
->FvComponents
[Index
].ComponentName
, EFI_NV_VARIABLE_STRING
);
882 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
883 if (EFI_ERROR (Status
)) {
884 printf ("ERROR: %s is not a valid integer.\n", EFI_NV_VARIABLE_STRING
);
888 FvInfo
->FvComponents
[Index
].Size
= (UINTN
) Value64
;
890 printf ("WARNING: Could not read %s.\n", EFI_NV_VARIABLE_STRING
);
895 // Read component FV_EVENT_LOG
897 Status
= FindToken (InfFile
, COMPONENT_SECTION_STRING
, EFI_NV_EVENT_LOG_STRING
, 0, Value
);
899 if (Status
== EFI_SUCCESS
) {
903 strcpy (FvInfo
->FvComponents
[Index
].ComponentName
, EFI_NV_EVENT_LOG_STRING
);
904 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
905 if (EFI_ERROR (Status
)) {
906 printf ("ERROR: %s is not a valid integer.\n", EFI_NV_EVENT_LOG_STRING
);
910 FvInfo
->FvComponents
[Index
].Size
= (UINTN
) Value64
;
912 printf ("WARNING: Could not read %s.\n", EFI_NV_EVENT_LOG_STRING
);
917 // Read component FV_FTW_WORKING
919 Status
= FindToken (InfFile
, COMPONENT_SECTION_STRING
, EFI_NV_FTW_WORKING_STRING
, 0, Value
);
921 if (Status
== EFI_SUCCESS
) {
925 strcpy (FvInfo
->FvComponents
[Index
].ComponentName
, EFI_NV_FTW_WORKING_STRING
);
926 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
927 if (EFI_ERROR (Status
)) {
928 printf ("ERROR: %s is not a valid integer.\n", EFI_NV_FTW_WORKING_STRING
);
932 FvInfo
->FvComponents
[Index
].Size
= (UINTN
) Value64
;
934 printf ("WARNING: Could not read %s.\n", EFI_NV_FTW_WORKING_STRING
);
939 // Read component FV_FTW_SPARE
941 Status
= FindToken (InfFile
, COMPONENT_SECTION_STRING
, EFI_NV_FTW_SPARE_STRING
, 0, Value
);
943 if (Status
== EFI_SUCCESS
) {
947 strcpy (FvInfo
->FvComponents
[Index
].ComponentName
, EFI_NV_FTW_SPARE_STRING
);
948 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
949 if (EFI_ERROR (Status
)) {
950 printf ("ERROR: %s is not a valid integer.\n", EFI_NV_FTW_SPARE_STRING
);
954 FvInfo
->FvComponents
[Index
].Size
= (UINTN
) Value64
;
956 printf ("WARNING: Could not read %s.\n", EFI_NV_FTW_SPARE_STRING
);
960 // Compute size for easy access later
963 for (Index
= 0; FvInfo
->FvBlocks
[Index
].NumBlocks
; Index
++) {
964 FvInfo
->Size
+= FvInfo
->FvBlocks
[Index
].NumBlocks
* FvInfo
->FvBlocks
[Index
].BlockLength
;
972 IN EFI_FFS_FILE_HEADER
*FfsFile
,
973 IN EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
979 This function changes the FFS file attributes based on the erase polarity
993 if (FvHeader
->Attributes
& EFI_FVB_ERASE_POLARITY
) {
994 FfsFile
->State
= (UINT8
)~(FfsFile
->State
);
1000 IN EFI_FFS_FILE_HEADER
*FfsFile
,
1001 IN OUT UINT32
*Alignment
1005 Routine Description:
1007 This function determines the alignment of the FFS input file from the file
1012 FfsFile FFS file to parse
1013 Alignment The minimum required alignment of the FFS file, in bytes
1017 EFI_SUCCESS The function completed successfully.
1018 EFI_INVALID_PARAMETER One of the input parameters was invalid.
1019 EFI_ABORTED An error occurred.
1024 // Verify input parameters.
1026 if (FfsFile
== NULL
|| Alignment
== NULL
) {
1027 return EFI_INVALID_PARAMETER
;
1030 switch ((FfsFile
->Attributes
>> 3) & 0x07) {
1036 *Alignment
= (1 << 0);
1041 // 16 byte alignment
1043 *Alignment
= (1 << 4);
1048 // 128 byte alignment
1050 *Alignment
= (1 << 7);
1055 // 512 byte alignment
1057 *Alignment
= (1 << 9);
1062 // 1K byte alignment
1064 *Alignment
= (1 << 10);
1069 // 4K byte alignment
1071 *Alignment
= (1 << 12);
1076 // 32K byte alignment
1078 *Alignment
= (1 << 15);
1083 // 64K byte alignment
1085 *Alignment
= (1 << 16);
1089 Error (NULL
, 0, 0, "nvalid file attribute calculated, this is most likely a utility error", NULL
);
1098 IN OUT MEMORY_FILE
*FvImage
,
1099 IN UINT32 DataAlignment
1103 Routine Description:
1105 This function adds a pad file to the FV image if it required to align the
1106 data of the next file.
1110 FvImage The memory image of the FV to add it to. The current offset
1112 DataAlignment The data alignment of the next FFS file.
1116 EFI_SUCCESS The function completed successfully.
1117 EFI_INVALID_PARAMETER One of the input parameters was invalid.
1118 EFI_OUT_OF_RESOURCES Insufficient resources exist in the FV to complete
1123 EFI_FFS_FILE_HEADER
*PadFile
;
1124 EFI_GUID PadFileGuid
;
1128 // Verify input parameters.
1130 if (FvImage
== NULL
) {
1131 return EFI_INVALID_PARAMETER
;
1134 // Basic assumption is we start from an 8 byte aligned address
1135 // and our file header is a multiple of 8 bytes
1137 assert ((UINTN
) FvImage
->CurrentFilePointer
% 8 == 0);
1138 assert (sizeof (EFI_FFS_FILE_HEADER
) % 8 == 0);
1141 // Check if a pad file is necessary
1143 if (((UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
+ sizeof (EFI_FFS_FILE_HEADER
)) % DataAlignment
== 0) {
1147 // Write pad file header
1149 PadFile
= (EFI_FFS_FILE_HEADER
*) FvImage
->CurrentFilePointer
;
1152 // Verify that we have enough space for the file header
1154 if ((UINTN
) (PadFile
+ sizeof (EFI_FFS_FILE_HEADER
)) >= (UINTN
) FvImage
->Eof
) {
1155 return EFI_OUT_OF_RESOURCES
;
1161 uuid_generate (tmp_id
);
1162 memcpy (&PadFileGuid
, tmp_id
, sizeof (EFI_GUID
));
1165 UuidCreate (&PadFileGuid
);
1167 memset (PadFile
, 0, sizeof (EFI_FFS_FILE_HEADER
));
1168 memcpy (&PadFile
->Name
, &PadFileGuid
, sizeof (EFI_GUID
));
1169 PadFile
->Type
= EFI_FV_FILETYPE_FFS_PAD
;
1170 PadFile
->Attributes
= 0;
1173 // Calculate the pad file size
1176 // This is the earliest possible valid offset (current plus pad file header
1177 // plus the next file header)
1179 PadFileSize
= (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
+ (sizeof (EFI_FFS_FILE_HEADER
) * 2);
1182 // Add whatever it takes to get to the next aligned address
1184 while ((PadFileSize
% DataAlignment
) != 0) {
1188 // Subtract the next file header size
1190 PadFileSize
-= sizeof (EFI_FFS_FILE_HEADER
);
1193 // Subtract the starting offset to get size
1195 PadFileSize
-= (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
;
1198 // Write pad file size (calculated size minus next file header size)
1200 PadFile
->Size
[0] = (UINT8
) (PadFileSize
& 0xFF);
1201 PadFile
->Size
[1] = (UINT8
) ((PadFileSize
>> 8) & 0xFF);
1202 PadFile
->Size
[2] = (UINT8
) ((PadFileSize
>> 16) & 0xFF);
1205 // Fill in checksums and state, they must be 0 for checksumming.
1207 PadFile
->IntegrityCheck
.Checksum
.Header
= 0;
1208 PadFile
->IntegrityCheck
.Checksum
.File
= 0;
1210 PadFile
->IntegrityCheck
.Checksum
.Header
= CalculateChecksum8 ((UINT8
*) PadFile
, sizeof (EFI_FFS_FILE_HEADER
));
1211 if (PadFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
1212 PadFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 ((UINT8
*) PadFile
, PadFileSize
);
1214 PadFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
1217 PadFile
->State
= EFI_FILE_HEADER_CONSTRUCTION
| EFI_FILE_HEADER_VALID
| EFI_FILE_DATA_VALID
;
1218 UpdateFfsFileState (
1219 (EFI_FFS_FILE_HEADER
*) PadFile
,
1220 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
1224 // Verify that we have enough space (including the padding
1226 if ((UINTN
) (PadFile
+ sizeof (EFI_FFS_FILE_HEADER
)) >= (UINTN
) FvImage
->Eof
) {
1227 return EFI_OUT_OF_RESOURCES
;
1230 // Update the current FV pointer
1232 FvImage
->CurrentFilePointer
+= PadFileSize
;
1239 IN EFI_FFS_FILE_HEADER
*FileBuffer
1243 Routine Description:
1245 This function checks the header to validate if it is a VTF file
1249 FileBuffer Buffer in which content of a file has been read.
1253 TRUE If this is a VTF file
1254 FALSE If this is not a VTF file
1258 EFI_GUID VtfGuid
= EFI_FFS_VOLUME_TOP_FILE_GUID
;
1259 if (!memcmp (&FileBuffer
->Name
, &VtfGuid
, sizeof (EFI_GUID
))) {
1267 FfsRebaseImageRead (
1268 IN VOID
*FileHandle
,
1269 IN UINTN FileOffset
,
1270 IN OUT UINT32
*ReadSize
,
1275 Routine Description:
1277 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
1281 FileHandle - The handle to the PE/COFF file
1283 FileOffset - The offset, in bytes, into the file to read
1285 ReadSize - The number of bytes to read from the file starting at FileOffset
1287 Buffer - A pointer to the buffer to read the data into.
1291 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
1295 CHAR8
*Destination8
;
1299 Destination8
= Buffer
;
1300 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
1303 *(Destination8
++) = *(Source8
++);
1312 IN UINT64 BaseAddress
,
1313 IN EFI_FFS_FILE_HEADER
*FfsFile
,
1314 IN OUT MEMORY_FILE
*SymImage
,
1315 IN CHAR8
*SourceFileName
1319 Routine Description:
1321 This function adds the SYM tokens in the source file to the destination file.
1322 The SYM tokens are updated to reflect the base address.
1326 BaseAddress The base address for the new SYM tokens.
1327 FfsFile Pointer to the beginning of the FFS file in question.
1328 SymImage The memory file to update with symbol information.
1329 SourceFileName The source file.
1333 EFI_SUCCESS The function completed successfully.
1334 EFI_INVALID_PARAMETER One of the input parameters was invalid.
1335 EFI_ABORTED An error occurred.
1341 CHAR8 Buffer
[_MAX_PATH
];
1342 CHAR8 Type
[_MAX_PATH
];
1343 CHAR8 Address
[_MAX_PATH
];
1344 CHAR8 Section
[_MAX_PATH
];
1345 CHAR8 Token
[_MAX_PATH
];
1346 CHAR8 SymFileName
[_MAX_PATH
];
1347 CHAR8 CodeModuleName
[_MAX_PATH
];
1350 UINT64 TokenAddress
;
1353 EFI_FILE_SECTION_POINTER Pe32Section
;
1357 EFI_TE_IMAGE_HEADER
*TeHdr
;
1361 // Verify input parameters.
1363 if (BaseAddress
== 0 || FfsFile
== NULL
|| SymImage
== NULL
|| SourceFileName
== NULL
) {
1364 Error (NULL
, 0, 0, "invalid parameter passed to AddSymFile()", NULL
);
1365 return EFI_INVALID_PARAMETER
;
1368 // Check if we want to add this file
1371 // Get the file name
1373 strcpy (Buffer
, SourceFileName
);
1376 // Copy the file name for the path of the sym file and truncate the name portion.
1378 strcpy (SymFileName
, Buffer
);
1379 Ptr
= strrchr (SymFileName
, '\\');
1384 // Find the file extension and make it lower case
1386 Ptr
= strrchr (SymFileName
, '.');
1391 // Check if it is PEI file
1393 if (strstr (Buffer
, ".pei") != NULL
) {
1395 // Find the human readable portion
1397 if (!strtok (Buffer
, "-") ||
1398 !strtok (NULL
, "-") ||
1399 !strtok (NULL
, "-") ||
1400 !strtok (NULL
, "-") ||
1401 !strtok (NULL
, "-") ||
1402 !strcpy (Buffer
, strtok (NULL
, "."))
1404 Error (NULL
, 0, 0, "failed to find human readable portion of the file name in AddSymFile()", NULL
);
1408 // Save code module name
1410 strcpy (CodeModuleName
, Buffer
);
1413 // Add the symbol file name and extension to the file path.
1415 strcat (Buffer
, ".sym");
1416 strcat (SymFileName
, "\\");
1417 strcat (SymFileName
, Buffer
);
1420 // Only handle PEIM files.
1425 // Find PE32 section
1427 Status
= GetSectionByType (FfsFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1430 // BUGBUG: Assume if no PE32 section it is PIC and hardcode base address
1432 if (Status
== EFI_NOT_FOUND
) {
1433 Status
= GetSectionByType (FfsFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1434 if (Status
== EFI_NOT_FOUND
) {
1437 TeHdr
= (EFI_TE_IMAGE_HEADER
*) ((UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_COMMON_SECTION_HEADER
));
1438 BaseOfCode
= TeHdr
->BaseOfCode
- TeHdr
->StrippedSize
;
1440 } else if (EFI_ERROR (Status
)) {
1441 Error (NULL
, 0, 0, "could not parse a PE32 section from the PEI file", NULL
);
1444 Status
= GetPe32Info (
1445 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
)),
1450 if (EFI_ERROR (Status
)) {
1451 Error (NULL
, 0, 0, "GetPe32Info() could not get PE32 entry point for PEI file", NULL
);
1456 // Open the source file
1458 SourceFile
= fopen (SymFileName
, "r");
1459 if (SourceFile
== NULL
) {
1461 // SYM files are not required.
1466 // Read the first line
1468 if (fgets (Buffer
, _MAX_PATH
, SourceFile
) == NULL
) {
1472 // Make sure it matches the expected sym format
1474 if (strcmp (Buffer
, "TEXTSYM format | V1.0\n")) {
1475 fclose (SourceFile
);
1476 Error (NULL
, 0, 0, "AddSymFile() found unexpected sym format in input file", NULL
);
1482 while (feof (SourceFile
) == 0) {
1488 "%s | %s | %s | %s\n",
1495 // If the token starts with "??" ignore it
1497 if (Token
[0] == '?' && Token
[1] == '?') {
1501 // Get the token address
1503 AsciiStringToUint64 (Address
, TRUE
, &TokenAddress
);
1506 // Add the base address
1508 TokenAddress
+= BaseAddress
;
1511 // If PE32 then find the start of code. For PIC it is hardcoded.
1513 if (TeHdr
== NULL
&& Pe32Section
.Pe32Section
) {
1515 // Add the offset of the PE32 section
1517 TokenAddress
+= (UINTN
) Pe32Section
.Pe32Section
- (UINTN
) FfsFile
;
1520 // Add the size of the PE32 section header
1522 TokenAddress
+= sizeof (EFI_PE32_SECTION
);
1523 } else if (TeHdr
!= NULL
) {
1525 // Add the Te section and FfsHeader
1528 // BUGBUG: Don't know why this is 0x28 bytes.
1530 TokenAddress
+= (UINTN
) TeHdr
- (UINTN
) FfsFile
+ 0x28;
1533 // BUGBUG: Don't know why this is 0x28 bytes.
1535 TokenAddress
+= 0x28;
1538 // Add the beginning of the code
1540 TokenAddress
+= BaseOfCode
;
1544 "%s | %016I64X | %s | _%s%s\n",
1551 memcpy (SymImage
->CurrentFilePointer
, Buffer
, strlen (Buffer
) + 1);
1552 SymImage
->CurrentFilePointer
= (UINT8
*) (((UINTN
) SymImage
->CurrentFilePointer
) + strlen (Buffer
) + 1);
1556 fclose (SourceFile
);
1562 IN OUT MEMORY_FILE
*FvImage
,
1565 IN OUT EFI_FFS_FILE_HEADER
**VtfFileImage
,
1566 IN OUT MEMORY_FILE
*SymImage
1570 Routine Description:
1572 This function adds a file to the FV image. The file will pad to the
1573 appropriate alignment if required.
1577 FvImage The memory image of the FV to add it to. The current offset
1579 FvInfo Pointer to information about the FV.
1580 Index The file in the FvInfo file list to add.
1581 VtfFileImage A pointer to the VTF file within the FvImage. If this is equal
1582 to the end of the FvImage then no VTF previously found.
1583 SymImage The memory image of the Sym file to update if symbols are present.
1584 The current offset must be valid.
1588 EFI_SUCCESS The function completed successfully.
1589 EFI_INVALID_PARAMETER One of the input parameters was invalid.
1590 EFI_ABORTED An error occurred.
1591 EFI_OUT_OF_RESOURCES Insufficient resources exist to complete the add.
1599 UINT32 CurrentFileAlignment
;
1601 EFI_PHYSICAL_ADDRESS CurrentFileBaseAddress
;
1602 UINT8 VtfHeaderChecksum
;
1603 UINT8 VtfFileChecksum
;
1605 EFI_FFS_FILE_TAIL TailValue
;
1608 // Verify input parameters.
1610 if (FvImage
== NULL
|| FvInfo
== NULL
|| FvInfo
->FvFiles
[Index
][0] == 0 || VtfFileImage
== NULL
|| SymImage
== NULL
) {
1611 return EFI_INVALID_PARAMETER
;
1614 // Read the file to add
1616 NewFile
= fopen (FvInfo
->FvFiles
[Index
], "rb");
1618 if (NewFile
== NULL
) {
1619 Error (NULL
, 0, 0, FvInfo
->FvFiles
[Index
], "failed to open file for reading");
1623 // Get the file size
1627 struct stat stat_buf
;
1628 fstat(fileno(NewFile
), &stat_buf
);
1629 FileSize
= stat_buf
.st_size
;
1632 FileSize
= _filelength (fileno (NewFile
));
1636 // Read the file into a buffer
1638 FileBuffer
= malloc (FileSize
);
1639 if (FileBuffer
== NULL
) {
1640 Error (NULL
, 0, 0, "memory allocation failure", NULL
);
1641 return EFI_OUT_OF_RESOURCES
;
1644 NumBytesRead
= fread (FileBuffer
, sizeof (UINT8
), FileSize
, NewFile
);
1647 // Done with the file, from this point on we will just use the buffer read.
1652 // Verify read successful
1654 if (NumBytesRead
!= sizeof (UINT8
) * FileSize
) {
1656 Error (NULL
, 0, 0, FvInfo
->FvFiles
[Index
], "failed to read input file contents");
1660 // Verify space exists to add the file
1662 if (FileSize
> (UINTN
) ((UINTN
) *VtfFileImage
- (UINTN
) FvImage
->CurrentFilePointer
)) {
1663 Error (NULL
, 0, 0, FvInfo
->FvFiles
[Index
], "insufficient space remains to add the file");
1664 return EFI_OUT_OF_RESOURCES
;
1667 // Update the file state based on polarity of the FV.
1669 UpdateFfsFileState (
1670 (EFI_FFS_FILE_HEADER
*) FileBuffer
,
1671 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
1675 // If we have a VTF file, add it at the top.
1677 if (IsVtfFile ((EFI_FFS_FILE_HEADER
*) FileBuffer
)) {
1678 if ((UINTN
) *VtfFileImage
== (UINTN
) FvImage
->Eof
) {
1680 // No previous VTF, add this one.
1682 *VtfFileImage
= (EFI_FFS_FILE_HEADER
*) (UINTN
) ((UINTN
) FvImage
->FileImage
+ FvInfo
->Size
- FileSize
);
1684 // Sanity check. The file MUST align appropriately
1686 if ((((UINTN
) *VtfFileImage
) & 0x07) != 0) {
1687 Error (NULL
, 0, 0, "VTF file does not align on 8-byte boundary", NULL
);
1690 // copy VTF File Header
1692 memcpy (*VtfFileImage
, FileBuffer
, sizeof (EFI_FFS_FILE_HEADER
));
1698 (UINT8
*) *VtfFileImage
+ sizeof (EFI_FFS_FILE_HEADER
),
1699 FileBuffer
+ sizeof (EFI_FFS_FILE_HEADER
),
1700 FileSize
- sizeof (EFI_FFS_FILE_HEADER
)
1704 // re-calculate the VTF File Header
1706 FileState
= (*VtfFileImage
)->State
;
1707 (*VtfFileImage
)->State
= 0;
1708 *(UINT32
*) ((*VtfFileImage
)->Size
) = FileSize
;
1709 (*VtfFileImage
)->IntegrityCheck
.Checksum
.Header
= 0;
1710 (*VtfFileImage
)->IntegrityCheck
.Checksum
.File
= 0;
1712 VtfHeaderChecksum
= CalculateChecksum8 ((UINT8
*) *VtfFileImage
, sizeof (EFI_FFS_FILE_HEADER
));
1713 (*VtfFileImage
)->IntegrityCheck
.Checksum
.Header
= VtfHeaderChecksum
;
1715 // Determine if it has a tail
1717 if ((*VtfFileImage
)->Attributes
& FFS_ATTRIB_TAIL_PRESENT
) {
1718 TailSize
= sizeof (EFI_FFS_FILE_TAIL
);
1723 if ((*VtfFileImage
)->Attributes
& FFS_ATTRIB_CHECKSUM
) {
1724 VtfFileChecksum
= CalculateChecksum8 ((UINT8
*) *VtfFileImage
, FileSize
- TailSize
);
1725 (*VtfFileImage
)->IntegrityCheck
.Checksum
.File
= VtfFileChecksum
;
1727 (*VtfFileImage
)->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
1730 // If it has a file tail, update it
1732 if ((*VtfFileImage
)->Attributes
& FFS_ATTRIB_TAIL_PRESENT
) {
1733 TailValue
= (EFI_FFS_FILE_TAIL
) (~((*VtfFileImage
)->IntegrityCheck
.TailReference
));
1734 *(EFI_FFS_FILE_TAIL
*) (((UINTN
) (*VtfFileImage
) + GetLength ((*VtfFileImage
)->Size
) - sizeof (EFI_FFS_FILE_TAIL
))) = TailValue
;
1736 (*VtfFileImage
)->State
= FileState
;
1741 // Already found a VTF file.
1743 Error (NULL
, 0, 0, "multiple VTF files are illegal in a single FV", NULL
);
1749 // Check if alignment is required
1751 Status
= ReadFfsAlignment ((EFI_FFS_FILE_HEADER
*) FileBuffer
, &CurrentFileAlignment
);
1752 if (EFI_ERROR (Status
)) {
1753 printf ("ERROR: Could not determine alignment of file %s.\n", FvInfo
->FvFiles
[Index
]);
1758 // Add pad file if necessary
1760 Status
= AddPadFile (FvImage
, CurrentFileAlignment
);
1761 if (EFI_ERROR (Status
)) {
1762 printf ("ERROR: Could not align the file data properly.\n");
1769 if ((FvImage
->CurrentFilePointer
+ FileSize
) < FvImage
->Eof
) {
1773 memcpy (FvImage
->CurrentFilePointer
, FileBuffer
, FileSize
);
1776 // If the file is XIP, rebase
1778 CurrentFileBaseAddress
= FvInfo
->BaseAddress
+ ((UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
);
1780 // Status = RebaseFfsFile ((EFI_FFS_FILE_HEADER*) FvImage->CurrentFilePointer, CurrentFileBaseAddress);
1781 // if (EFI_ERROR(Status)) {
1782 // printf ("ERROR: Could not rebase the file %s.\n", FvInfo->FvFiles[Index]);
1783 // return EFI_ABORTED;
1786 // Update Symbol file
1788 Status
= AddSymFile (
1789 CurrentFileBaseAddress
,
1790 (EFI_FFS_FILE_HEADER
*) FvImage
->CurrentFilePointer
,
1792 FvInfo
->FvFiles
[Index
]
1794 assert (!EFI_ERROR (Status
));
1797 // Update the current pointer in the FV image
1799 FvImage
->CurrentFilePointer
+= FileSize
;
1801 printf ("ERROR: The firmware volume is out of space, could not add file %s.\n", FvInfo
->FvFiles
[Index
]);
1805 // Make next file start at QWord Boundry
1807 while (((UINTN
) FvImage
->CurrentFilePointer
& 0x07) != 0) {
1808 FvImage
->CurrentFilePointer
++;
1811 // Free allocated memory.
1825 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
1826 VARIABLE_STORE_HEADER
*VarStoreHeader
;
1828 // Variable block should exclude FvHeader. Since the length of
1829 // FvHeader depends on the block map, which is variable length,
1830 // we could only decide the actual variable block length here.
1832 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
;
1833 FvImage
= FvImage
+ FvHeader
->HeaderLength
;
1835 VarStoreHeader
= (VARIABLE_STORE_HEADER
*) FvImage
;
1837 VarStoreHeader
->Signature
= VARIABLE_STORE_SIGNATURE
;
1838 VarStoreHeader
->Size
= Size
- FvHeader
->HeaderLength
;
1839 VarStoreHeader
->Format
= VARIABLE_STORE_FORMATTED
;
1840 VarStoreHeader
->State
= VARIABLE_STORE_HEALTHY
;
1841 VarStoreHeader
->Reserved
= 0;
1842 VarStoreHeader
->Reserved1
= 0;
1858 AddFTWWorkingBlock (
1864 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*FTWHeader
;
1868 FTWHeader
= (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*) FvImage
;
1869 memcpy (&FTWHeader
->Signature
, &(FvInfo
->FvGuid
), sizeof (EFI_GUID
));
1870 FTWHeader
->WriteQueueSize
= Size
- sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
);
1871 CalculateCrc32 (FvImage
, sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
), &Crc32
);
1872 FTWHeader
->Crc
= Crc32
;
1873 if (FvInfo
->FvAttributes
& EFI_FVB_ERASE_POLARITY
) {
1874 FTWHeader
->WorkingBlockValid
= 0;
1875 FTWHeader
->WorkingBlockInvalid
= 1;
1877 FTWHeader
->WorkingBlockValid
= 1;
1878 FTWHeader
->WorkingBlockInvalid
= 0;
1901 Routine Description:
1903 This function generate the non FFS FV image, such as the working block
1904 and spare block. How each component of the FV is built is component
1909 FvImage The memory image of the FV to add it to. The current offset
1911 FvInfo Pointer to information about the FV.
1915 EFI_SUCCESS The function completed successfully.
1916 EFI_INVALID_PARAMETER One of the input parameters was invalid.
1917 EFI_ABORTED An error occurred.
1918 EFI_OUT_OF_RESOURCES Insufficient resources exist to complete the add.
1923 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
1926 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
;
1929 for (Index
= 0; FvInfo
->FvComponents
[Index
].Size
!= 0; Index
++) {
1930 if (stricmp (FvInfo
->FvComponents
[Index
].ComponentName
, EFI_NV_VARIABLE_STRING
) == 0) {
1931 AddVariableBlock (FvImage
, FvInfo
->FvComponents
[Index
].Size
, FvInfo
);
1932 } else if (stricmp (FvInfo
->FvComponents
[Index
].ComponentName
, EFI_NV_EVENT_LOG_STRING
) == 0) {
1933 AddEventLogBlock (FvImage
, FvInfo
->FvComponents
[Index
].Size
, FvInfo
);
1934 } else if (stricmp (FvInfo
->FvComponents
[Index
].ComponentName
, EFI_NV_FTW_WORKING_STRING
) == 0) {
1935 AddFTWWorkingBlock (FvImage
, FvInfo
->FvComponents
[Index
].Size
, FvInfo
);
1936 } else if (stricmp (FvInfo
->FvComponents
[Index
].ComponentName
, EFI_NV_FTW_SPARE_STRING
) == 0) {
1937 AddFTWSpareBlock (FvImage
, FvInfo
->FvComponents
[Index
].Size
, FvInfo
);
1939 printf ("Error. Unknown Non-FFS block %s \n", FvInfo
->FvComponents
[Index
].ComponentName
);
1943 FvImage
= FvImage
+ FvInfo
->FvComponents
[Index
].Size
;
1944 TotalSize
= TotalSize
+ FvInfo
->FvComponents
[Index
].Size
;
1947 // Index and TotalSize is zero mean there's no component, so this is an empty fv
1949 if ((Index
!= 0 || TotalSize
!= 0) && TotalSize
!= FvInfo
->Size
) {
1950 printf ("Error. Component size does not sum up to FV size.\n");
1959 IN MEMORY_FILE
*FvImage
,
1960 IN EFI_FFS_FILE_HEADER
*VtfFileImage
1964 Routine Description:
1966 This function places a pad file between the last file in the FV and the VTF
1967 file if the VTF file exists.
1971 FvImage Memory file for the FV memory image
1972 VtfFileImage The address of the VTF file. If this is the end of the FV
1973 image, no VTF exists and no pad file is needed.
1977 EFI_SUCCESS Completed successfully.
1978 EFI_INVALID_PARAMETER One of the input parameters was NULL.
1982 EFI_FFS_FILE_HEADER
*PadFile
;
1986 // If there is no VTF or the VTF naturally follows the previous file without a
1987 // pad file, then there's nothing to do
1989 if ((UINTN
) VtfFileImage
== (UINTN
) FvImage
->Eof
|| (void *) FvImage
->CurrentFilePointer
== (void *) VtfFileImage
) {
1993 // Pad file starts at beginning of free space
1995 PadFile
= (EFI_FFS_FILE_HEADER
*) FvImage
->CurrentFilePointer
;
2000 memset (PadFile
, 0, sizeof (EFI_FFS_FILE_HEADER
));
2001 memcpy (&PadFile
->Name
, &DefaultFvPadFileNameGuid
, sizeof (EFI_GUID
));
2002 PadFile
->Type
= EFI_FV_FILETYPE_FFS_PAD
;
2003 PadFile
->Attributes
= 0;
2006 // FileSize includes the EFI_FFS_FILE_HEADER
2008 FileSize
= (UINTN
) VtfFileImage
- (UINTN
) FvImage
->CurrentFilePointer
;
2009 PadFile
->Size
[0] = (UINT8
) (FileSize
& 0x000000FF);
2010 PadFile
->Size
[1] = (UINT8
) ((FileSize
& 0x0000FF00) >> 8);
2011 PadFile
->Size
[2] = (UINT8
) ((FileSize
& 0x00FF0000) >> 16);
2014 // Fill in checksums and state, must be zero during checksum calculation.
2016 PadFile
->IntegrityCheck
.Checksum
.Header
= 0;
2017 PadFile
->IntegrityCheck
.Checksum
.File
= 0;
2019 PadFile
->IntegrityCheck
.Checksum
.Header
= CalculateChecksum8 ((UINT8
*) PadFile
, sizeof (EFI_FFS_FILE_HEADER
));
2020 if (PadFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
2021 PadFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 ((UINT8
*) PadFile
, FileSize
);
2023 PadFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
2026 PadFile
->State
= EFI_FILE_HEADER_CONSTRUCTION
| EFI_FILE_HEADER_VALID
| EFI_FILE_DATA_VALID
;
2028 UpdateFfsFileState (
2029 (EFI_FFS_FILE_HEADER
*) PadFile
,
2030 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
2033 // Update the current FV pointer
2035 FvImage
->CurrentFilePointer
= FvImage
->Eof
;
2042 IN MEMORY_FILE
*FvImage
,
2044 IN EFI_FFS_FILE_HEADER
*VtfFile
2048 Routine Description:
2050 This parses the FV looking for the PEI core and then plugs the address into
2051 the SALE_ENTRY point of the BSF/VTF for IPF and does BUGBUG TBD action to
2052 complete an IA32 Bootstrap FV.
2056 FvImage Memory file for the FV memory image
2057 FvInfo Information read from INF file.
2058 VtfFile Pointer to the VTF file in the FV image.
2062 EFI_SUCCESS Function Completed successfully.
2063 EFI_ABORTED Error encountered.
2064 EFI_INVALID_PARAMETER A required parameter was NULL.
2065 EFI_NOT_FOUND PEI Core file not found.
2069 EFI_FFS_FILE_HEADER
*PeiCoreFile
;
2070 EFI_FFS_FILE_HEADER
*SecCoreFile
;
2072 EFI_FILE_SECTION_POINTER Pe32Section
;
2076 EFI_PHYSICAL_ADDRESS PeiCorePhysicalAddress
;
2077 EFI_PHYSICAL_ADDRESS SecCorePhysicalAddress
;
2078 EFI_PHYSICAL_ADDRESS
*SecCoreEntryAddressPtr
;
2079 UINT32
*Ia32ResetAddressPtr
;
2081 UINT8
*BytePointer2
;
2082 UINT16
*WordPointer
;
2085 EFI_FFS_FILE_STATE SavedState
;
2086 EFI_FFS_FILE_TAIL TailValue
;
2089 FIT_TABLE
*FitTablePtr
;
2092 // Verify input parameters
2094 if (FvImage
== NULL
|| FvInfo
== NULL
|| VtfFile
== NULL
) {
2095 return EFI_INVALID_PARAMETER
;
2098 // Initialize FV library
2100 InitializeFvLib (FvImage
->FileImage
, (UINTN
) FvImage
->Eof
- (UINTN
) FvImage
->FileImage
);
2105 Status
= VerifyFfsFile (VtfFile
);
2106 if (EFI_ERROR (Status
)) {
2107 return EFI_INVALID_PARAMETER
;
2110 // Find the PEI Core
2112 Status
= GetFileByType (EFI_FV_FILETYPE_PEI_CORE
, 1, &PeiCoreFile
);
2113 if (EFI_ERROR (Status
) || PeiCoreFile
== NULL
) {
2114 Error (NULL
, 0, 0, "could not find the PEI core in the FV", NULL
);
2118 // PEI Core found, now find PE32 section
2120 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
2121 if (EFI_ERROR (Status
)) {
2122 Error (NULL
, 0, 0, "could not find PE32 section in PEI core file", NULL
);
2126 Status
= GetPe32Info (
2127 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
)),
2132 if (EFI_ERROR (Status
)) {
2133 Error (NULL
, 0, 0, "could not get PE32 entry point for PEI core", NULL
);
2137 // Physical address is FV base + offset of PE32 + offset of the entry point
2139 PeiCorePhysicalAddress
= FvInfo
->BaseAddress
;
2140 PeiCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
) - (UINTN
) FvImage
->FileImage
;
2141 PeiCorePhysicalAddress
+= EntryPoint
;
2143 if (MachineType
== EFI_IMAGE_MACHINE_IA64
) {
2145 // Update PEI_CORE address
2148 // Set the uncached attribute bit in the physical address
2150 PeiCorePhysicalAddress
|= 0x8000000000000000ULL
;
2153 // Check if address is aligned on a 16 byte boundary
2155 if (PeiCorePhysicalAddress
& 0xF) {
2157 "ERROR: PEI_CORE entry point is not aligned on a 16 byte boundary, address specified is %Xh.\n",
2158 PeiCorePhysicalAddress
2163 // First Get the FIT table address
2165 FitAddress
= (*(UINT64
*) (FvImage
->Eof
- IPF_FIT_ADDRESS_OFFSET
)) & 0xFFFFFFFF;
2167 FitTablePtr
= (FIT_TABLE
*) (FvImage
->FileImage
+ (FitAddress
- FvInfo
->BaseAddress
));
2169 Status
= UpdatePeiCoreEntryInFit (FitTablePtr
, PeiCorePhysicalAddress
);
2171 if (!EFI_ERROR (Status
)) {
2172 UpdateFitCheckSum (FitTablePtr
);
2175 // Find the Sec Core
2177 Status
= GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE
, 1, &SecCoreFile
);
2178 if (EFI_ERROR (Status
) || SecCoreFile
== NULL
) {
2179 Error (NULL
, 0, 0, "could not find the Sec core in the FV", NULL
);
2183 // Sec Core found, now find PE32 section
2185 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
2186 if (EFI_ERROR (Status
)) {
2187 Error (NULL
, 0, 0, "could not find PE32 section in SEC core file", NULL
);
2191 Status
= GetPe32Info (
2192 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
)),
2197 if (EFI_ERROR (Status
)) {
2198 Error (NULL
, 0, 0, "could not get PE32 entry point for SEC core", NULL
);
2202 // Physical address is FV base + offset of PE32 + offset of the entry point
2204 SecCorePhysicalAddress
= FvInfo
->BaseAddress
;
2205 SecCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
) - (UINTN
) FvImage
->FileImage
;
2206 SecCorePhysicalAddress
+= EntryPoint
;
2209 // Update SEC_CORE address
2212 // Set the uncached attribute bit in the physical address
2214 SecCorePhysicalAddress
|= 0x8000000000000000ULL
;
2217 // Update the address
2219 SecCoreEntryAddressPtr
= (EFI_PHYSICAL_ADDRESS
*) ((UINTN
) FvImage
->Eof
- IPF_SALE_ENTRY_ADDRESS_OFFSET
);
2220 *SecCoreEntryAddressPtr
= SecCorePhysicalAddress
;
2223 // Check if address is aligned on a 16 byte boundary
2225 if (SecCorePhysicalAddress
& 0xF) {
2227 "ERROR: SALE_ENTRY entry point is not aligned on a 16 byte boundary, address specified is %Xh.\n",
2228 SecCorePhysicalAddress
2232 } else if (MachineType
== EFI_IMAGE_MACHINE_IA32
) {
2234 // Get the location to update
2236 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- IA32_PEI_CORE_ENTRY_OFFSET
);
2239 // Write lower 32 bits of physical address
2241 *Ia32ResetAddressPtr
= (UINT32
) PeiCorePhysicalAddress
;
2244 // Update the BFV base address
2246 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- 4);
2247 *Ia32ResetAddressPtr
= (UINT32
) (FvInfo
->BaseAddress
);
2252 // Update the Startup AP in the FVH header block ZeroVector region.
2254 BytePointer
= (UINT8
*) ((UINTN
) FvImage
->FileImage
);
2255 BytePointer2
= (FvInfo
->Size
== 0x10000) ? m64kRecoveryStartupApDataArray
: m128kRecoveryStartupApDataArray
;
2256 for (Index
= 0; Index
< SIZEOF_STARTUP_DATA_ARRAY
; Index
++) {
2257 *BytePointer
++ = *BytePointer2
++;
2260 // Calculate the checksum
2262 WordPointer
= (UINT16
*) ((UINTN
) FvImage
->FileImage
);
2263 for (Index
= 0; Index
< SIZEOF_STARTUP_DATA_ARRAY
/ 2; Index
++) {
2264 CheckSum
= (UINT16
) (CheckSum
+ ((UINT16
) *WordPointer
));
2268 // Update the checksum field
2270 BytePointer
= (UINT8
*) ((UINTN
) FvImage
->FileImage
);
2271 BytePointer
+= (SIZEOF_STARTUP_DATA_ARRAY
- 2);
2272 WordPointer
= (UINT16
*) BytePointer
;
2273 *WordPointer
= (UINT16
) (0x10000 - (UINT32
) CheckSum
);
2275 Error (NULL
, 0, 0, "invalid machine type in PEI core", "machine type=0x%X", (UINT32
) MachineType
);
2279 // Determine if it has an FFS file tail.
2281 if (VtfFile
->Attributes
& FFS_ATTRIB_TAIL_PRESENT
) {
2282 TailSize
= sizeof (EFI_FFS_FILE_TAIL
);
2287 // Now update file checksum
2289 SavedState
= VtfFile
->State
;
2290 VtfFile
->IntegrityCheck
.Checksum
.File
= 0;
2292 if (VtfFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
2293 VtfFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
2295 GetLength (VtfFile
->Size
) - TailSize
2298 VtfFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
2301 VtfFile
->State
= SavedState
;
2304 // Update tail if present
2306 if (VtfFile
->Attributes
& FFS_ATTRIB_TAIL_PRESENT
) {
2307 TailValue
= (EFI_FFS_FILE_TAIL
) (~(VtfFile
->IntegrityCheck
.TailReference
));
2308 *(EFI_FFS_FILE_TAIL
*) (((UINTN
) (VtfFile
) + GetLength (VtfFile
->Size
) - sizeof (EFI_FFS_FILE_TAIL
))) = TailValue
;
2317 OUT UINT32
*EntryPoint
,
2318 OUT UINT32
*BaseOfCode
,
2319 OUT UINT16
*MachineType
2323 Routine Description:
2325 Retrieves the PE32 entry point offset and machine type. See EfiImage.h for
2326 machine types. The entry point offset is from the beginning of the PE32
2331 Pe32 Beginning of the PE32.
2332 EntryPoint Offset from the beginning of the PE32 to the image entry point.
2333 BaseOfCode Base address of code.
2334 MachineType Magic number for the machine type.
2338 EFI_SUCCESS Function completed successfully.
2339 EFI_ABORTED Error encountered.
2340 EFI_INVALID_PARAMETER A required parameter was NULL.
2341 EFI_UNSUPPORTED The operation is unsupported.
2345 EFI_IMAGE_DOS_HEADER
*DosHeader
;
2346 EFI_IMAGE_NT_HEADERS
*NtHeader
;
2349 // Verify input parameters
2352 return EFI_INVALID_PARAMETER
;
2355 // First is the DOS header
2357 DosHeader
= (EFI_IMAGE_DOS_HEADER
*) Pe32
;
2360 // Verify DOS header is expected
2362 if (DosHeader
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
2363 printf ("ERROR: Unknown magic number in the DOS header, 0x%04X.\n", DosHeader
->e_magic
);
2364 return EFI_UNSUPPORTED
;
2367 // Immediately following is the NT header.
2369 NtHeader
= (EFI_IMAGE_NT_HEADERS
*) ((UINTN
) Pe32
+ DosHeader
->e_lfanew
);
2372 // Verify NT header is expected
2374 if (NtHeader
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
2375 printf ("ERROR: Unrecognized image signature 0x%08X.\n", NtHeader
->Signature
);
2376 return EFI_UNSUPPORTED
;
2381 *EntryPoint
= NtHeader
->OptionalHeader
.AddressOfEntryPoint
;
2382 *BaseOfCode
= NtHeader
->OptionalHeader
.BaseOfCode
;
2383 *MachineType
= NtHeader
->FileHeader
.Machine
;
2386 // Verify machine type is supported
2388 if (*MachineType
!= EFI_IMAGE_MACHINE_IA32
&& *MachineType
!= EFI_IMAGE_MACHINE_IA64
&& *MachineType
!= EFI_IMAGE_MACHINE_X64
&& *MachineType
!= EFI_IMAGE_MACHINE_EBC
) {
2389 printf ("ERROR: Unrecognized machine type in the PE32 file.\n");
2390 return EFI_UNSUPPORTED
;
2396 // Exposed function implementations (prototypes are defined in GenFvImageLib.h)
2400 IN CHAR8
*InfFileImage
,
2401 IN UINTN InfFileSize
,
2402 OUT UINT8
**FvImage
,
2403 OUT UINTN
*FvImageSize
,
2404 OUT CHAR8
**FvFileName
,
2405 OUT UINT8
**SymImage
,
2406 OUT UINTN
*SymImageSize
,
2407 OUT CHAR8
**SymFileName
2411 Routine Description:
2413 This is the main function which will be called from application.
2417 InfFileImage Buffer containing the INF file contents.
2418 InfFileSize Size of the contents of the InfFileImage buffer.
2419 FvImage Pointer to the FV image created.
2420 FvImageSize Size of the FV image created and pointed to by FvImage.
2421 FvFileName Requested name for the FV file.
2422 SymImage Pointer to the Sym image created.
2423 SymImageSize Size of the Sym image created and pointed to by SymImage.
2424 SymFileName Requested name for the Sym file.
2428 EFI_SUCCESS Function completed successfully.
2429 EFI_OUT_OF_RESOURCES Could not allocate required resources.
2430 EFI_ABORTED Error encountered.
2431 EFI_INVALID_PARAMETER A required parameter was NULL.
2436 MEMORY_FILE InfMemoryFile
;
2437 MEMORY_FILE FvImageMemoryFile
;
2438 MEMORY_FILE SymImageMemoryFile
;
2441 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
2442 EFI_FFS_FILE_HEADER
*VtfFileImage
;
2445 // Check for invalid parameter
2447 if (InfFileImage
== NULL
|| FvImage
== NULL
|| FvImageSize
== NULL
|| FvFileName
== NULL
) {
2448 return EFI_INVALID_PARAMETER
;
2451 // Initialize file structures
2453 InfMemoryFile
.FileImage
= InfFileImage
;
2454 InfMemoryFile
.CurrentFilePointer
= InfFileImage
;
2455 InfMemoryFile
.Eof
= InfFileImage
+ InfFileSize
;
2458 // Parse the FV inf file for header information
2460 Status
= ParseFvInf (&InfMemoryFile
, &FvInfo
);
2461 if (EFI_ERROR (Status
)) {
2462 printf ("ERROR: Could not parse the input INF file.\n");
2466 // Update the file name return values
2468 strcpy (*FvFileName
, FvInfo
.FvName
);
2469 strcpy (*SymFileName
, FvInfo
.SymName
);
2472 // Calculate the FV size
2474 *FvImageSize
= FvInfo
.Size
;
2479 *FvImage
= malloc (*FvImageSize
);
2480 if (*FvImage
== NULL
) {
2481 return EFI_OUT_OF_RESOURCES
;
2484 // Allocate space for symbol file storage
2486 *SymImage
= malloc (SYMBOL_FILE_SIZE
);
2487 if (*SymImage
== NULL
) {
2488 return EFI_OUT_OF_RESOURCES
;
2491 // Initialize the FV to the erase polarity
2493 if (FvInfo
.FvAttributes
& EFI_FVB_ERASE_POLARITY
) {
2494 memset (*FvImage
, -1, *FvImageSize
);
2496 memset (*FvImage
, 0, *FvImageSize
);
2499 // Initialize FV header
2501 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) *FvImage
;
2504 // Initialize the zero vector to all zeros.
2506 memset (FvHeader
->ZeroVector
, 0, 16);
2509 // Copy the FFS GUID
2511 memcpy (&FvHeader
->FileSystemGuid
, &FvInfo
.FvGuid
, sizeof (EFI_GUID
));
2513 FvHeader
->FvLength
= *FvImageSize
;
2514 FvHeader
->Signature
= EFI_FVH_SIGNATURE
;
2515 FvHeader
->Attributes
= FvInfo
.FvAttributes
;
2516 FvHeader
->Revision
= EFI_FVH_REVISION
;
2517 FvHeader
->Reserved
[0] = 0;
2518 FvHeader
->Reserved
[1] = 0;
2519 FvHeader
->Reserved
[2] = 0;
2522 // Copy firmware block map
2524 for (Index
= 0; FvInfo
.FvBlocks
[Index
].NumBlocks
!= 0; Index
++) {
2525 FvHeader
->FvBlockMap
[Index
].NumBlocks
= FvInfo
.FvBlocks
[Index
].NumBlocks
;
2526 FvHeader
->FvBlockMap
[Index
].BlockLength
= FvInfo
.FvBlocks
[Index
].BlockLength
;
2529 // Add block map terminator
2531 FvHeader
->FvBlockMap
[Index
].NumBlocks
= 0;
2532 FvHeader
->FvBlockMap
[Index
].BlockLength
= 0;
2535 // Complete the header
2537 FvHeader
->HeaderLength
= (UINT16
) (((UINTN
) &(FvHeader
->FvBlockMap
[Index
+ 1])) - (UINTN
) *FvImage
);
2538 FvHeader
->Checksum
= 0;
2539 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2542 // If there is no FFS file, find and generate each components of the FV
2544 if (FvInfo
.FvFiles
[0][0] == 0) {
2545 Status
= GenNonFFSFv (*FvImage
, &FvInfo
);
2546 if (EFI_ERROR (Status
)) {
2547 printf ("ERROR: Could not generate NonFFS FV.\n");
2555 // Initialize our "file" view of the buffer
2557 FvImageMemoryFile
.FileImage
= *FvImage
;
2558 FvImageMemoryFile
.CurrentFilePointer
= *FvImage
+ FvHeader
->HeaderLength
;
2559 FvImageMemoryFile
.Eof
= *FvImage
+*FvImageSize
;
2562 // Initialize our "file" view of the symbol file.
2564 SymImageMemoryFile
.FileImage
= *SymImage
;
2565 SymImageMemoryFile
.CurrentFilePointer
= *SymImage
;
2566 SymImageMemoryFile
.Eof
= *FvImage
+ SYMBOL_FILE_SIZE
;
2569 // Initialize the FV library.
2571 InitializeFvLib (FvImageMemoryFile
.FileImage
, FvInfo
.Size
);
2574 // Files start on 8 byte alignments, so move to the next 8 byte aligned
2575 // address. For now, just assert if it isn't. Currently FV header is
2576 // always a multiple of 8 bytes.
2577 // BUGBUG: Handle this better
2579 assert ((((UINTN
) FvImageMemoryFile
.CurrentFilePointer
) % 8) == 0);
2582 // Initialize the VTF file address.
2584 VtfFileImage
= (EFI_FFS_FILE_HEADER
*) FvImageMemoryFile
.Eof
;
2589 for (Index
= 0; FvInfo
.FvFiles
[Index
][0] != 0; Index
++) {
2593 Status
= AddFile (&FvImageMemoryFile
, &FvInfo
, Index
, &VtfFileImage
, &SymImageMemoryFile
);
2596 // Exit if error detected while adding the file
2598 if (EFI_ERROR (Status
)) {
2599 printf ("ERROR: Could not add file %s.\n", FvInfo
.FvFiles
[Index
]);
2605 // If there is a VTF file, some special actions need to occur.
2607 if ((UINTN
) VtfFileImage
!= (UINTN
) FvImageMemoryFile
.Eof
) {
2609 // Pad from the end of the last file to the beginning of the VTF file.
2611 Status
= PadFvImage (&FvImageMemoryFile
, VtfFileImage
);
2612 if (EFI_ERROR (Status
)) {
2613 printf ("ERROR: Could not create the pad file between the last file and the VTF file.\n");
2618 // Update reset vector (SALE_ENTRY for IPF)
2619 // Now for IA32 and IA64 platform, the fv which has bsf file must have the
2620 // EndAddress of 0xFFFFFFFF. Thus, only this type fv needs to update the
2621 // reset vector. If the PEI Core is found, the VTF file will probably get
2622 // corrupted by updating the entry point.
2624 if ((FvInfo
.BaseAddress
+ FvInfo
.Size
) == FV_IMAGES_TOP_ADDRESS
) {
2625 Status
= UpdateResetVector (&FvImageMemoryFile
, &FvInfo
, VtfFileImage
);
2626 if (EFI_ERROR(Status
)) {
2627 printf ("ERROR: Could not update the reset vector.\n");
2634 // Determine final Sym file size
2636 *SymImageSize
= SymImageMemoryFile
.CurrentFilePointer
- SymImageMemoryFile
.FileImage
;
2642 UpdatePeiCoreEntryInFit (
2643 IN FIT_TABLE
*FitTablePtr
,
2644 IN UINT64 PeiCorePhysicalAddress
2648 Routine Description:
2650 This function is used to update the Pei Core address in FIT, this can be used by Sec core to pass control from
2655 FitTablePtr - The pointer of FIT_TABLE.
2656 PeiCorePhysicalAddress - The address of Pei Core entry.
2660 EFI_SUCCESS - The PEI_CORE FIT entry was updated successfully.
2661 EFI_NOT_FOUND - Not found the PEI_CORE FIT entry.
2665 FIT_TABLE
*TmpFitPtr
;
2667 UINTN NumFitComponents
;
2669 TmpFitPtr
= FitTablePtr
;
2670 NumFitComponents
= TmpFitPtr
->CompSize
;
2672 for (Index
= 0; Index
< NumFitComponents
; Index
++) {
2673 if ((TmpFitPtr
->CvAndType
& FIT_TYPE_MASK
) == COMP_TYPE_FIT_PEICORE
) {
2674 TmpFitPtr
->CompAddress
= PeiCorePhysicalAddress
;
2681 return EFI_NOT_FOUND
;
2686 IN FIT_TABLE
*FitTablePtr
2690 Routine Description:
2692 This function is used to update the checksum for FIT.
2697 FitTablePtr - The pointer of FIT_TABLE.
2705 if ((FitTablePtr
->CvAndType
& CHECKSUM_BIT_MASK
) >> 7) {
2706 FitTablePtr
->CheckSum
= 0;
2707 FitTablePtr
->CheckSum
= CalculateChecksum8 ((UINT8
*) FitTablePtr
, FitTablePtr
->CompSize
* 16);