]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/GenFv/GenFvInternalLib.c
Sync BaseTool trunk (version r2460) into EDKII BaseTools. The change mainly includes:
[mirror_edk2.git] / BaseTools / Source / C / GenFv / GenFvInternalLib.c
1 /** @file
2
3 Copyright (c) 2004 - 2011, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 GenFvInternalLib.c
15
16 Abstract:
17
18 This file contains the internal functions required to generate a Firmware Volume.
19
20 **/
21
22 //
23 // Include files
24 //
25 #ifdef __GNUC__
26 #include <uuid/uuid.h>
27 #include <sys/stat.h>
28 #endif
29 #include <string.h>
30 #ifndef __GNUC__
31 #include <io.h>
32 #endif
33 #include <assert.h>
34
35 #include "GenFvInternalLib.h"
36 #include "FvLib.h"
37 #include "PeCoffLib.h"
38 #include "WinNtInclude.h"
39
40 BOOLEAN mArm = FALSE;
41 STATIC UINT32 MaxFfsAlignment = 0;
42
43 EFI_GUID mEfiFirmwareVolumeTopFileGuid = EFI_FFS_VOLUME_TOP_FILE_GUID;
44 EFI_GUID mFileGuidArray [MAX_NUMBER_OF_FILES_IN_FV];
45 EFI_GUID mZeroGuid = {0x0, 0x0, 0x0, {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}};
46 EFI_GUID mDefaultCapsuleGuid = {0x3B6686BD, 0x0D76, 0x4030, { 0xB7, 0x0E, 0xB5, 0x51, 0x9E, 0x2F, 0xC5, 0xA0 }};
47
48 CHAR8 *mFvbAttributeName[] = {
49 EFI_FVB2_READ_DISABLED_CAP_STRING,
50 EFI_FVB2_READ_ENABLED_CAP_STRING,
51 EFI_FVB2_READ_STATUS_STRING,
52 EFI_FVB2_WRITE_DISABLED_CAP_STRING,
53 EFI_FVB2_WRITE_ENABLED_CAP_STRING,
54 EFI_FVB2_WRITE_STATUS_STRING,
55 EFI_FVB2_LOCK_CAP_STRING,
56 EFI_FVB2_LOCK_STATUS_STRING,
57 NULL,
58 EFI_FVB2_STICKY_WRITE_STRING,
59 EFI_FVB2_MEMORY_MAPPED_STRING,
60 EFI_FVB2_ERASE_POLARITY_STRING,
61 EFI_FVB2_READ_LOCK_CAP_STRING,
62 EFI_FVB2_READ_LOCK_STATUS_STRING,
63 EFI_FVB2_WRITE_LOCK_CAP_STRING,
64 EFI_FVB2_WRITE_LOCK_STATUS_STRING
65 };
66
67 CHAR8 *mFvbAlignmentName[] = {
68 EFI_FVB2_ALIGNMENT_1_STRING,
69 EFI_FVB2_ALIGNMENT_2_STRING,
70 EFI_FVB2_ALIGNMENT_4_STRING,
71 EFI_FVB2_ALIGNMENT_8_STRING,
72 EFI_FVB2_ALIGNMENT_16_STRING,
73 EFI_FVB2_ALIGNMENT_32_STRING,
74 EFI_FVB2_ALIGNMENT_64_STRING,
75 EFI_FVB2_ALIGNMENT_128_STRING,
76 EFI_FVB2_ALIGNMENT_256_STRING,
77 EFI_FVB2_ALIGNMENT_512_STRING,
78 EFI_FVB2_ALIGNMENT_1K_STRING,
79 EFI_FVB2_ALIGNMENT_2K_STRING,
80 EFI_FVB2_ALIGNMENT_4K_STRING,
81 EFI_FVB2_ALIGNMENT_8K_STRING,
82 EFI_FVB2_ALIGNMENT_16K_STRING,
83 EFI_FVB2_ALIGNMENT_32K_STRING,
84 EFI_FVB2_ALIGNMENT_64K_STRING,
85 EFI_FVB2_ALIGNMENT_128K_STRING,
86 EFI_FVB2_ALIGNMENT_256K_STRING,
87 EFI_FVB2_ALIGNMENT_512K_STRING,
88 EFI_FVB2_ALIGNMENT_1M_STRING,
89 EFI_FVB2_ALIGNMENT_2M_STRING,
90 EFI_FVB2_ALIGNMENT_4M_STRING,
91 EFI_FVB2_ALIGNMENT_8M_STRING,
92 EFI_FVB2_ALIGNMENT_16M_STRING,
93 EFI_FVB2_ALIGNMENT_32M_STRING,
94 EFI_FVB2_ALIGNMENT_64M_STRING,
95 EFI_FVB2_ALIGNMENT_128M_STRING,
96 EFI_FVB2_ALIGNMENT_256M_STRING,
97 EFI_FVB2_ALIGNMENT_512M_STRING,
98 EFI_FVB2_ALIGNMENT_1G_STRING,
99 EFI_FVB2_ALIGNMENT_2G_STRING
100 };
101
102 //
103 // This data array will be located at the base of the Firmware Volume Header (FVH)
104 // in the boot block. It must not exceed 14 bytes of code. The last 2 bytes
105 // will be used to keep the FVH checksum consistent.
106 // This code will be run in response to a starutp IPI for HT-enabled systems.
107 //
108 #define SIZEOF_STARTUP_DATA_ARRAY 0x10
109
110 UINT8 m128kRecoveryStartupApDataArray[SIZEOF_STARTUP_DATA_ARRAY] = {
111 //
112 // EA D0 FF 00 F0 ; far jmp F000:FFD0
113 // 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
114 // 0, 0 ; Checksum Padding
115 //
116 0xEA,
117 0xD0,
118 0xFF,
119 0x0,
120 0xF0,
121 0x00,
122 0x00,
123 0x00,
124 0x00,
125 0x00,
126 0x00,
127 0x00,
128 0x00,
129 0x00,
130 0x00,
131 0x00
132 };
133
134 UINT8 m64kRecoveryStartupApDataArray[SIZEOF_STARTUP_DATA_ARRAY] = {
135 //
136 // EB CE ; jmp short ($-0x30)
137 // ; (from offset 0x0 to offset 0xFFD0)
138 // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
139 // 0, 0 ; Checksum Padding
140 //
141 0xEB,
142 0xCE,
143 0x00,
144 0x00,
145 0x00,
146 0x00,
147 0x00,
148 0x00,
149 0x00,
150 0x00,
151 0x00,
152 0x00,
153 0x00,
154 0x00,
155 0x00,
156 0x00
157 };
158
159 FV_INFO mFvDataInfo;
160 CAP_INFO mCapDataInfo;
161
162 EFI_PHYSICAL_ADDRESS mFvBaseAddress[0x10];
163 UINT32 mFvBaseAddressNumber = 0;
164
165 EFI_STATUS
166 ParseFvInf (
167 IN MEMORY_FILE *InfFile,
168 OUT FV_INFO *FvInfo
169 )
170 /*++
171
172 Routine Description:
173
174 This function parses a FV.INF file and copies info into a FV_INFO structure.
175
176 Arguments:
177
178 InfFile Memory file image.
179 FvInfo Information read from INF file.
180
181 Returns:
182
183 EFI_SUCCESS INF file information successfully retrieved.
184 EFI_ABORTED INF file has an invalid format.
185 EFI_NOT_FOUND A required string was not found in the INF file.
186 --*/
187 {
188 CHAR8 Value[_MAX_PATH];
189 UINT64 Value64;
190 UINTN Index;
191 UINTN Number;
192 EFI_STATUS Status;
193 EFI_GUID GuidValue;
194
195 //
196 // Read the FV base address
197 //
198 if (!mFvDataInfo.BaseAddressSet) {
199 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_FV_BASE_ADDRESS_STRING, 0, Value);
200 if (Status == EFI_SUCCESS) {
201 //
202 // Get the base address
203 //
204 Status = AsciiStringToUint64 (Value, FALSE, &Value64);
205 if (EFI_ERROR (Status)) {
206 Error (NULL, 0, 2000, "Invalid parameter", "%s = %s", EFI_FV_BASE_ADDRESS_STRING, Value);
207 return EFI_ABORTED;
208 }
209 DebugMsg (NULL, 0, 9, "rebase address", "%s = %s", EFI_FV_BASE_ADDRESS_STRING, Value);
210
211 FvInfo->BaseAddress = Value64;
212 FvInfo->BaseAddressSet = TRUE;
213 }
214 }
215
216 //
217 // Read the FV File System Guid
218 //
219 if (!FvInfo->FvFileSystemGuidSet) {
220 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_FV_FILESYSTEMGUID_STRING, 0, Value);
221 if (Status == EFI_SUCCESS) {
222 //
223 // Get the guid value
224 //
225 Status = StringToGuid (Value, &GuidValue);
226 if (EFI_ERROR (Status)) {
227 Error (NULL, 0, 2000, "Invalid parameter", "%s = %s", EFI_FV_FILESYSTEMGUID_STRING, Value);
228 return EFI_ABORTED;
229 }
230 memcpy (&FvInfo->FvFileSystemGuid, &GuidValue, sizeof (EFI_GUID));
231 FvInfo->FvFileSystemGuidSet = TRUE;
232 }
233 }
234
235 //
236 // Read the FV Extension Header File Name
237 //
238 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FV_EXT_HEADER_FILE_NAME, 0, Value);
239 if (Status == EFI_SUCCESS) {
240 strcpy (FvInfo->FvExtHeaderFile, Value);
241 }
242
243 //
244 // Read the FV file name
245 //
246 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_FV_FILE_NAME_STRING, 0, Value);
247 if (Status == EFI_SUCCESS) {
248 //
249 // copy the file name
250 //
251 strcpy (FvInfo->FvName, Value);
252 }
253
254 //
255 // Read Fv Attribute
256 //
257 for (Index = 0; Index < sizeof (mFvbAttributeName)/sizeof (CHAR8 *); Index ++) {
258 if ((mFvbAttributeName [Index] != NULL) && \
259 (FindToken (InfFile, ATTRIBUTES_SECTION_STRING, mFvbAttributeName [Index], 0, Value) == EFI_SUCCESS)) {
260 if ((strcmp (Value, TRUE_STRING) == 0) || (strcmp (Value, ONE_STRING) == 0)) {
261 FvInfo->FvAttributes |= 1 << Index;
262 } else if ((strcmp (Value, FALSE_STRING) != 0) && (strcmp (Value, ZERO_STRING) != 0)) {
263 Error (NULL, 0, 2000, "Invalid parameter", "%s expected %s | %s", mFvbAttributeName [Index], TRUE_STRING, FALSE_STRING);
264 return EFI_ABORTED;
265 }
266 }
267 }
268
269 //
270 // Read Fv Alignment
271 //
272 for (Index = 0; Index < sizeof (mFvbAlignmentName)/sizeof (CHAR8 *); Index ++) {
273 if (FindToken (InfFile, ATTRIBUTES_SECTION_STRING, mFvbAlignmentName [Index], 0, Value) == EFI_SUCCESS) {
274 if (strcmp (Value, TRUE_STRING) == 0) {
275 FvInfo->FvAttributes |= Index << 16;
276 DebugMsg (NULL, 0, 9, "FV file alignment", "Align = %s", mFvbAlignmentName [Index]);
277 break;
278 }
279 }
280 }
281
282 //
283 // Read block maps
284 //
285 for (Index = 0; Index < MAX_NUMBER_OF_FV_BLOCKS; Index++) {
286 if (FvInfo->FvBlocks[Index].Length == 0) {
287 //
288 // Read block size
289 //
290 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_BLOCK_SIZE_STRING, Index, Value);
291
292 if (Status == EFI_SUCCESS) {
293 //
294 // Update the size of block
295 //
296 Status = AsciiStringToUint64 (Value, FALSE, &Value64);
297 if (EFI_ERROR (Status)) {
298 Error (NULL, 0, 2000, "Invalid parameter", "%s = %s", EFI_BLOCK_SIZE_STRING, Value);
299 return EFI_ABORTED;
300 }
301
302 FvInfo->FvBlocks[Index].Length = (UINT32) Value64;
303 DebugMsg (NULL, 0, 9, "FV Block Size", "%s = %s", EFI_BLOCK_SIZE_STRING, Value);
304 } else {
305 //
306 // If there is no blocks size, but there is the number of block, then we have a mismatched pair
307 // and should return an error.
308 //
309 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_NUM_BLOCKS_STRING, Index, Value);
310 if (!EFI_ERROR (Status)) {
311 Error (NULL, 0, 2000, "Invalid parameter", "both %s and %s must be specified.", EFI_NUM_BLOCKS_STRING, EFI_BLOCK_SIZE_STRING);
312 return EFI_ABORTED;
313 } else {
314 //
315 // We are done
316 //
317 break;
318 }
319 }
320
321 //
322 // Read blocks number
323 //
324 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_NUM_BLOCKS_STRING, Index, Value);
325
326 if (Status == EFI_SUCCESS) {
327 //
328 // Update the number of blocks
329 //
330 Status = AsciiStringToUint64 (Value, FALSE, &Value64);
331 if (EFI_ERROR (Status)) {
332 Error (NULL, 0, 2000, "Invalid parameter", "%s = %s", EFI_NUM_BLOCKS_STRING, Value);
333 return EFI_ABORTED;
334 }
335
336 FvInfo->FvBlocks[Index].NumBlocks = (UINT32) Value64;
337 DebugMsg (NULL, 0, 9, "FV Block Number", "%s = %s", EFI_NUM_BLOCKS_STRING, Value);
338 }
339 }
340 }
341
342 if (Index == 0) {
343 Error (NULL, 0, 2001, "Missing required argument", "block size.");
344 return EFI_ABORTED;
345 }
346
347 //
348 // Read files
349 //
350 Number = 0;
351 for (Number = 0; Number < MAX_NUMBER_OF_FILES_IN_FV; Number ++) {
352 if (FvInfo->FvFiles[Number][0] == '\0') {
353 break;
354 }
355 }
356
357 for (Index = 0; Index < MAX_NUMBER_OF_FILES_IN_FV; Index++) {
358 //
359 // Read the FFS file list
360 //
361 Status = FindToken (InfFile, FILES_SECTION_STRING, EFI_FILE_NAME_STRING, Index, Value);
362
363 if (Status == EFI_SUCCESS) {
364 //
365 // Add the file
366 //
367 strcpy (FvInfo->FvFiles[Number + Index], Value);
368 DebugMsg (NULL, 0, 9, "FV component file", "the %uth name is %s", (unsigned) Index, Value);
369 } else {
370 break;
371 }
372 }
373
374 if ((Index + Number) == 0) {
375 Warning (NULL, 0, 0, "FV components are not specified.", NULL);
376 }
377
378 return EFI_SUCCESS;
379 }
380
381 VOID
382 UpdateFfsFileState (
383 IN EFI_FFS_FILE_HEADER *FfsFile,
384 IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader
385 )
386 /*++
387
388 Routine Description:
389
390 This function changes the FFS file attributes based on the erase polarity
391 of the FV. Update the reserved bits of State to EFI_FVB2_ERASE_POLARITY.
392
393 Arguments:
394
395 FfsFile File header.
396 FvHeader FV header.
397
398 Returns:
399
400 None
401
402 --*/
403 {
404 if (FvHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {
405 FfsFile->State = (UINT8)~(FfsFile->State);
406 // FfsFile->State |= ~(UINT8) EFI_FILE_ALL_STATE_BITS;
407 }
408 }
409
410 EFI_STATUS
411 ReadFfsAlignment (
412 IN EFI_FFS_FILE_HEADER *FfsFile,
413 IN OUT UINT32 *Alignment
414 )
415 /*++
416
417 Routine Description:
418
419 This function determines the alignment of the FFS input file from the file
420 attributes.
421
422 Arguments:
423
424 FfsFile FFS file to parse
425 Alignment The minimum required alignment offset of the FFS file
426
427 Returns:
428
429 EFI_SUCCESS The function completed successfully.
430 EFI_INVALID_PARAMETER One of the input parameters was invalid.
431 EFI_ABORTED An error occurred.
432
433 --*/
434 {
435 //
436 // Verify input parameters.
437 //
438 if (FfsFile == NULL || Alignment == NULL) {
439 return EFI_INVALID_PARAMETER;
440 }
441
442 switch ((FfsFile->Attributes >> 3) & 0x07) {
443
444 case 0:
445 //
446 // 8 byte alignment, mini alignment requirement for FFS file.
447 //
448 *Alignment = 3;
449 break;
450
451 case 1:
452 //
453 // 16 byte alignment
454 //
455 *Alignment = 4;
456 break;
457
458 case 2:
459 //
460 // 128 byte alignment
461 //
462 *Alignment = 7;
463 break;
464
465 case 3:
466 //
467 // 512 byte alignment
468 //
469 *Alignment = 9;
470 break;
471
472 case 4:
473 //
474 // 1K byte alignment
475 //
476 *Alignment = 10;
477 break;
478
479 case 5:
480 //
481 // 4K byte alignment
482 //
483 *Alignment = 12;
484 break;
485
486 case 6:
487 //
488 // 32K byte alignment
489 //
490 *Alignment = 15;
491 break;
492
493 case 7:
494 //
495 // 64K byte alignment
496 //
497 *Alignment = 16;
498 break;
499
500 default:
501 break;
502 }
503
504 return EFI_SUCCESS;
505 }
506
507 EFI_STATUS
508 AddPadFile (
509 IN OUT MEMORY_FILE *FvImage,
510 IN UINT32 DataAlignment,
511 IN VOID *FvEnd,
512 IN EFI_FIRMWARE_VOLUME_EXT_HEADER *ExtHeader
513 )
514 /*++
515
516 Routine Description:
517
518 This function adds a pad file to the FV image if it required to align the
519 data of the next file.
520
521 Arguments:
522
523 FvImage The memory image of the FV to add it to.
524 The current offset must be valid.
525 DataAlignment The data alignment of the next FFS file.
526 FvEnd End of the empty data in FvImage.
527 ExtHeader PI FvExtHeader Optional
528
529 Returns:
530
531 EFI_SUCCESS The function completed successfully.
532 EFI_INVALID_PARAMETER One of the input parameters was invalid.
533 EFI_OUT_OF_RESOURCES Insufficient resources exist in the FV to complete
534 the pad file add.
535
536 --*/
537 {
538 EFI_FFS_FILE_HEADER *PadFile;
539 UINTN PadFileSize;
540
541 //
542 // Verify input parameters.
543 //
544 if (FvImage == NULL) {
545 return EFI_INVALID_PARAMETER;
546 }
547
548 //
549 // Check if a pad file is necessary
550 //
551 if ((ExtHeader == NULL) && (((UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage + sizeof (EFI_FFS_FILE_HEADER)) % DataAlignment == 0)) {
552 return EFI_SUCCESS;
553 }
554
555 //
556 // Calculate the pad file size
557 //
558 //
559 // This is the earliest possible valid offset (current plus pad file header
560 // plus the next file header)
561 //
562 PadFileSize = (UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage + (sizeof (EFI_FFS_FILE_HEADER) * 2);
563
564 //
565 // Add whatever it takes to get to the next aligned address
566 //
567 while ((PadFileSize % DataAlignment) != 0) {
568 PadFileSize++;
569 }
570 //
571 // Subtract the next file header size
572 //
573 PadFileSize -= sizeof (EFI_FFS_FILE_HEADER);
574
575 //
576 // Subtract the starting offset to get size
577 //
578 PadFileSize -= (UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage;
579
580 //
581 // Append extension header size
582 //
583 if (ExtHeader != NULL) {
584 PadFileSize = PadFileSize + ExtHeader->ExtHeaderSize;
585 }
586
587 //
588 // Verify that we have enough space for the file header
589 //
590 if (((UINTN) FvImage->CurrentFilePointer + PadFileSize) > (UINTN) FvEnd) {
591 return EFI_OUT_OF_RESOURCES;
592 }
593
594 //
595 // Write pad file header
596 //
597 PadFile = (EFI_FFS_FILE_HEADER *) FvImage->CurrentFilePointer;
598
599 //
600 // Write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
601 //
602 PadFile->Type = EFI_FV_FILETYPE_FFS_PAD;
603 PadFile->Attributes = 0;
604
605 //
606 // Write pad file size (calculated size minus next file header size)
607 //
608 PadFile->Size[0] = (UINT8) (PadFileSize & 0xFF);
609 PadFile->Size[1] = (UINT8) ((PadFileSize >> 8) & 0xFF);
610 PadFile->Size[2] = (UINT8) ((PadFileSize >> 16) & 0xFF);
611
612 //
613 // Fill in checksums and state, they must be 0 for checksumming.
614 //
615 PadFile->IntegrityCheck.Checksum.Header = 0;
616 PadFile->IntegrityCheck.Checksum.File = 0;
617 PadFile->State = 0;
618 PadFile->IntegrityCheck.Checksum.Header = CalculateChecksum8 ((UINT8 *) PadFile, sizeof (EFI_FFS_FILE_HEADER));
619 PadFile->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;
620
621 PadFile->State = EFI_FILE_HEADER_CONSTRUCTION | EFI_FILE_HEADER_VALID | EFI_FILE_DATA_VALID;
622 UpdateFfsFileState (
623 (EFI_FFS_FILE_HEADER *) PadFile,
624 (EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage
625 );
626
627 //
628 // Update the current FV pointer
629 //
630 FvImage->CurrentFilePointer += PadFileSize;
631
632 if (ExtHeader != NULL) {
633 //
634 // Copy Fv Extension Header and Set Fv Extension header offset
635 //
636 memcpy (PadFile + 1, ExtHeader, ExtHeader->ExtHeaderSize);
637 ((EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage)->ExtHeaderOffset = (UINT16) ((UINTN) (PadFile + 1) - (UINTN) FvImage->FileImage);
638 //
639 // Make next file start at QWord Boundry
640 //
641 while (((UINTN) FvImage->CurrentFilePointer & (EFI_FFS_FILE_HEADER_ALIGNMENT - 1)) != 0) {
642 FvImage->CurrentFilePointer++;
643 }
644 }
645
646 return EFI_SUCCESS;
647 }
648
649 BOOLEAN
650 IsVtfFile (
651 IN EFI_FFS_FILE_HEADER *FileBuffer
652 )
653 /*++
654
655 Routine Description:
656
657 This function checks the header to validate if it is a VTF file
658
659 Arguments:
660
661 FileBuffer Buffer in which content of a file has been read.
662
663 Returns:
664
665 TRUE If this is a VTF file
666 FALSE If this is not a VTF file
667
668 --*/
669 {
670 if (!memcmp (&FileBuffer->Name, &mEfiFirmwareVolumeTopFileGuid, sizeof (EFI_GUID))) {
671 return TRUE;
672 } else {
673 return FALSE;
674 }
675 }
676
677 EFI_STATUS
678 WriteMapFile (
679 IN OUT FILE *FvMapFile,
680 IN CHAR8 *FileName,
681 IN EFI_FFS_FILE_HEADER *FfsFile,
682 IN EFI_PHYSICAL_ADDRESS ImageBaseAddress,
683 IN PE_COFF_LOADER_IMAGE_CONTEXT *pImageContext
684 )
685 /*++
686
687 Routine Description:
688
689 This function gets the basic debug information (entrypoint, baseaddress, .text, .data section base address)
690 from PE/COFF image and abstracts Pe Map file information and add them into FvMap file for Debug.
691
692 Arguments:
693
694 FvMapFile A pointer to FvMap File
695 FileName Ffs File PathName
696 FfsFile A pointer to Ffs file image.
697 ImageBaseAddress PeImage Base Address.
698 pImageContext Image Context Information.
699
700 Returns:
701
702 EFI_SUCCESS Added required map information.
703
704 --*/
705 {
706 CHAR8 PeMapFileName [_MAX_PATH];
707 CHAR8 *Cptr, *Cptr2;
708 CHAR8 FileGuidName [MAX_LINE_LEN];
709 FILE *PeMapFile;
710 CHAR8 Line [MAX_LINE_LEN];
711 CHAR8 KeyWord [MAX_LINE_LEN];
712 CHAR8 FunctionName [MAX_LINE_LEN];
713 EFI_PHYSICAL_ADDRESS FunctionAddress;
714 UINT32 FunctionType;
715 CHAR8 FunctionTypeName [MAX_LINE_LEN];
716 UINT32 Index;
717 UINT32 AddressOfEntryPoint;
718 UINT32 Offset;
719 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
720 EFI_TE_IMAGE_HEADER *TEImageHeader;
721 EFI_IMAGE_SECTION_HEADER *SectionHeader;
722 unsigned long long TempLongAddress;
723 UINT32 TextVirtualAddress;
724 UINT32 DataVirtualAddress;
725 EFI_PHYSICAL_ADDRESS LinkTimeBaseAddress;
726
727 //
728 // Init local variable
729 //
730 FunctionType = 0;
731 //
732 // Print FileGuid to string buffer.
733 //
734 PrintGuidToBuffer (&FfsFile->Name, (UINT8 *)FileGuidName, MAX_LINE_LEN, TRUE);
735
736 //
737 // Construct Map file Name
738 //
739 strcpy (PeMapFileName, FileName);
740
741 //
742 // Change '\\' to '/', unified path format.
743 //
744 Cptr = PeMapFileName;
745 while (*Cptr != '\0') {
746 if (*Cptr == '\\') {
747 *Cptr = FILE_SEP_CHAR;
748 }
749 Cptr ++;
750 }
751
752 //
753 // Get Map file
754 //
755 Cptr = PeMapFileName + strlen (PeMapFileName);
756 while ((*Cptr != '.') && (Cptr >= PeMapFileName)) {
757 Cptr --;
758 }
759 if (Cptr < PeMapFileName) {
760 return EFI_NOT_FOUND;
761 } else {
762 *(Cptr + 1) = 'm';
763 *(Cptr + 2) = 'a';
764 *(Cptr + 3) = 'p';
765 *(Cptr + 4) = '\0';
766 }
767
768 //
769 // Get module Name
770 //
771 Cptr2 = Cptr;
772 while ((*Cptr != FILE_SEP_CHAR) && (Cptr >= PeMapFileName)) {
773 Cptr --;
774 }
775 *Cptr2 = '\0';
776 strcpy (KeyWord, Cptr + 1);
777 *Cptr2 = '.';
778
779 //
780 // AddressOfEntryPoint and Offset in Image
781 //
782 if (!pImageContext->IsTeImage) {
783 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *) ((UINT8 *) pImageContext->Handle + pImageContext->PeCoffHeaderOffset);
784 AddressOfEntryPoint = ImgHdr->Pe32.OptionalHeader.AddressOfEntryPoint;
785 Offset = 0;
786 SectionHeader = (EFI_IMAGE_SECTION_HEADER *) (
787 (UINT8 *) ImgHdr +
788 sizeof (UINT32) +
789 sizeof (EFI_IMAGE_FILE_HEADER) +
790 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader
791 );
792 Index = ImgHdr->Pe32.FileHeader.NumberOfSections;
793 } else {
794 TEImageHeader = (EFI_TE_IMAGE_HEADER *) pImageContext->Handle;
795 AddressOfEntryPoint = TEImageHeader->AddressOfEntryPoint;
796 Offset = TEImageHeader->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER);
797 SectionHeader = (EFI_IMAGE_SECTION_HEADER *) (TEImageHeader + 1);
798 Index = TEImageHeader->NumberOfSections;
799 }
800
801 //
802 // module information output
803 //
804 if (ImageBaseAddress == 0) {
805 fprintf (FvMapFile, "%s (dummy) (", KeyWord);
806 fprintf (FvMapFile, "BaseAddress=%010llx, ", (unsigned long long) ImageBaseAddress);
807 } else {
808 fprintf (FvMapFile, "%s (Fixed Flash Address, ", KeyWord);
809 fprintf (FvMapFile, "BaseAddress=0x%010llx, ", (unsigned long long) (ImageBaseAddress + Offset));
810 }
811
812 if (FfsFile->Type != EFI_FV_FILETYPE_SECURITY_CORE && pImageContext->Machine == EFI_IMAGE_MACHINE_IA64) {
813 //
814 // Process IPF PLABEL to get the real address after the image has been rebased.
815 // PLABEL structure is got by AddressOfEntryPoint offset to ImageBuffer stored in pImageContext->Handle.
816 //
817 fprintf (FvMapFile, "EntryPoint=0x%010llx", (unsigned long long) (*(UINT64 *)((UINTN) pImageContext->Handle + (UINTN) AddressOfEntryPoint)));
818 } else {
819 fprintf (FvMapFile, "EntryPoint=0x%010llx", (unsigned long long) (ImageBaseAddress + AddressOfEntryPoint));
820 }
821 fprintf (FvMapFile, ")\n");
822
823 fprintf (FvMapFile, "(GUID=%s", FileGuidName);
824 TextVirtualAddress = 0;
825 DataVirtualAddress = 0;
826 for (; Index > 0; Index --, SectionHeader ++) {
827 if (stricmp ((CHAR8 *)SectionHeader->Name, ".text") == 0) {
828 TextVirtualAddress = SectionHeader->VirtualAddress;
829 } else if (stricmp ((CHAR8 *)SectionHeader->Name, ".data") == 0) {
830 DataVirtualAddress = SectionHeader->VirtualAddress;
831 } else if (stricmp ((CHAR8 *)SectionHeader->Name, ".sdata") == 0) {
832 DataVirtualAddress = SectionHeader->VirtualAddress;
833 }
834 }
835 fprintf (FvMapFile, " .textbaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress + TextVirtualAddress));
836 fprintf (FvMapFile, " .databaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress + DataVirtualAddress));
837 fprintf (FvMapFile, ")\n\n");
838
839 //
840 // Open PeMapFile
841 //
842 PeMapFile = fopen (PeMapFileName, "r");
843 if (PeMapFile == NULL) {
844 // fprintf (stdout, "can't open %s file to reading\n", PeMapFileName);
845 return EFI_ABORTED;
846 }
847 VerboseMsg ("The map file is %s", PeMapFileName);
848
849 //
850 // Output Functions information into Fv Map file
851 //
852 LinkTimeBaseAddress = 0;
853 while (fgets (Line, MAX_LINE_LEN, PeMapFile) != NULL) {
854 //
855 // Skip blank line
856 //
857 if (Line[0] == 0x0a) {
858 FunctionType = 0;
859 continue;
860 }
861 //
862 // By Address and Static keyword
863 //
864 if (FunctionType == 0) {
865 sscanf (Line, "%s", KeyWord);
866 if (stricmp (KeyWord, "Address") == 0) {
867 //
868 // function list
869 //
870 FunctionType = 1;
871 fgets (Line, MAX_LINE_LEN, PeMapFile);
872 } else if (stricmp (KeyWord, "Static") == 0) {
873 //
874 // static function list
875 //
876 FunctionType = 2;
877 fgets (Line, MAX_LINE_LEN, PeMapFile);
878 } else if (stricmp (KeyWord, "Preferred") ==0) {
879 sscanf (Line + strlen (" Preferred load address is"), "%llx", &TempLongAddress);
880 LinkTimeBaseAddress = (UINT64) TempLongAddress;
881 }
882 continue;
883 }
884 //
885 // Printf Function Information
886 //
887 if (FunctionType == 1) {
888 sscanf (Line, "%s %s %llx %s", KeyWord, FunctionName, &TempLongAddress, FunctionTypeName);
889 FunctionAddress = (UINT64) TempLongAddress;
890 if (FunctionTypeName [1] == '\0' && (FunctionTypeName [0] == 'f' || FunctionTypeName [0] == 'F')) {
891 fprintf (FvMapFile, " 0x%010llx ", (unsigned long long) (ImageBaseAddress + FunctionAddress - LinkTimeBaseAddress));
892 fprintf (FvMapFile, "%s\n", FunctionName);
893 }
894 } else if (FunctionType == 2) {
895 sscanf (Line, "%s %s %llx %s", KeyWord, FunctionName, &TempLongAddress, FunctionTypeName);
896 FunctionAddress = (UINT64) TempLongAddress;
897 if (FunctionTypeName [1] == '\0' && (FunctionTypeName [0] == 'f' || FunctionTypeName [0] == 'F')) {
898 fprintf (FvMapFile, " 0x%010llx ", (unsigned long long) (ImageBaseAddress + FunctionAddress - LinkTimeBaseAddress));
899 fprintf (FvMapFile, "%s\n", FunctionName);
900 }
901 }
902 }
903 //
904 // Close PeMap file
905 //
906 fprintf (FvMapFile, "\n\n");
907 fclose (PeMapFile);
908
909 return EFI_SUCCESS;
910 }
911
912 EFI_STATUS
913 AddFile (
914 IN OUT MEMORY_FILE *FvImage,
915 IN FV_INFO *FvInfo,
916 IN UINTN Index,
917 IN OUT EFI_FFS_FILE_HEADER **VtfFileImage,
918 IN FILE *FvMapFile,
919 IN FILE *FvReportFile
920 )
921 /*++
922
923 Routine Description:
924
925 This function adds a file to the FV image. The file will pad to the
926 appropriate alignment if required.
927
928 Arguments:
929
930 FvImage The memory image of the FV to add it to. The current offset
931 must be valid.
932 FvInfo Pointer to information about the FV.
933 Index The file in the FvInfo file list to add.
934 VtfFileImage A pointer to the VTF file within the FvImage. If this is equal
935 to the end of the FvImage then no VTF previously found.
936 FvMapFile Pointer to FvMap File
937 FvReportFile Pointer to FvReport File
938
939 Returns:
940
941 EFI_SUCCESS The function completed successfully.
942 EFI_INVALID_PARAMETER One of the input parameters was invalid.
943 EFI_ABORTED An error occurred.
944 EFI_OUT_OF_RESOURCES Insufficient resources exist to complete the add.
945
946 --*/
947 {
948 FILE *NewFile;
949 UINTN FileSize;
950 UINT8 *FileBuffer;
951 UINTN NumBytesRead;
952 UINT32 CurrentFileAlignment;
953 EFI_STATUS Status;
954 UINTN Index1;
955 UINT8 FileGuidString[PRINTED_GUID_BUFFER_SIZE];
956
957 Index1 = 0;
958 //
959 // Verify input parameters.
960 //
961 if (FvImage == NULL || FvInfo == NULL || FvInfo->FvFiles[Index][0] == 0 || VtfFileImage == NULL) {
962 return EFI_INVALID_PARAMETER;
963 }
964
965 //
966 // Read the file to add
967 //
968 NewFile = fopen (FvInfo->FvFiles[Index], "rb");
969
970 if (NewFile == NULL) {
971 Error (NULL, 0, 0001, "Error opening file", FvInfo->FvFiles[Index]);
972 return EFI_ABORTED;
973 }
974
975 //
976 // Get the file size
977 //
978 FileSize = _filelength (fileno (NewFile));
979
980 //
981 // Read the file into a buffer
982 //
983 FileBuffer = malloc (FileSize);
984 if (FileBuffer == NULL) {
985 Error (NULL, 0, 4001, "Resouce", "memory cannot be allocated!");
986 return EFI_OUT_OF_RESOURCES;
987 }
988
989 NumBytesRead = fread (FileBuffer, sizeof (UINT8), FileSize, NewFile);
990
991 //
992 // Done with the file, from this point on we will just use the buffer read.
993 //
994 fclose (NewFile);
995
996 //
997 // Verify read successful
998 //
999 if (NumBytesRead != sizeof (UINT8) * FileSize) {
1000 free (FileBuffer);
1001 Error (NULL, 0, 0004, "Error reading file", FvInfo->FvFiles[Index]);
1002 return EFI_ABORTED;
1003 }
1004
1005 //
1006 // For None PI Ffs file, directly add them into FvImage.
1007 //
1008 if (!FvInfo->IsPiFvImage) {
1009 memcpy (FvImage->CurrentFilePointer, FileBuffer, FileSize);
1010 if (FvInfo->SizeofFvFiles[Index] > FileSize) {
1011 FvImage->CurrentFilePointer += FvInfo->SizeofFvFiles[Index];
1012 } else {
1013 FvImage->CurrentFilePointer += FileSize;
1014 }
1015 goto Done;
1016 }
1017
1018 //
1019 // Verify Ffs file
1020 //
1021 Status = VerifyFfsFile ((EFI_FFS_FILE_HEADER *)FileBuffer);
1022 if (EFI_ERROR (Status)) {
1023 free (FileBuffer);
1024 Error (NULL, 0, 3000, "Invalid", "%s is not a valid FFS file.", FvInfo->FvFiles[Index]);
1025 return EFI_INVALID_PARAMETER;
1026 }
1027
1028 //
1029 // Verify space exists to add the file
1030 //
1031 if (FileSize > (UINTN) ((UINTN) *VtfFileImage - (UINTN) FvImage->CurrentFilePointer)) {
1032 free (FileBuffer);
1033 Error (NULL, 0, 4002, "Resource", "FV space is full, not enough room to add file %s.", FvInfo->FvFiles[Index]);
1034 return EFI_OUT_OF_RESOURCES;
1035 }
1036
1037 //
1038 // Verify the input file is the duplicated file in this Fv image
1039 //
1040 for (Index1 = 0; Index1 < Index; Index1 ++) {
1041 if (CompareGuid ((EFI_GUID *) FileBuffer, &mFileGuidArray [Index1]) == 0) {
1042 Error (NULL, 0, 2000, "Invalid parameter", "the %dth file and %uth file have the same file GUID.", (unsigned) Index1 + 1, (unsigned) Index + 1);
1043 PrintGuid ((EFI_GUID *) FileBuffer);
1044 return EFI_INVALID_PARAMETER;
1045 }
1046 }
1047 CopyMem (&mFileGuidArray [Index], FileBuffer, sizeof (EFI_GUID));
1048
1049 //
1050 // Update the file state based on polarity of the FV.
1051 //
1052 UpdateFfsFileState (
1053 (EFI_FFS_FILE_HEADER *) FileBuffer,
1054 (EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage
1055 );
1056
1057 //
1058 // Check if alignment is required
1059 //
1060 ReadFfsAlignment ((EFI_FFS_FILE_HEADER *) FileBuffer, &CurrentFileAlignment);
1061
1062 //
1063 // Find the largest alignment of all the FFS files in the FV
1064 //
1065 if (CurrentFileAlignment > MaxFfsAlignment) {
1066 MaxFfsAlignment = CurrentFileAlignment;
1067 }
1068 //
1069 // If we have a VTF file, add it at the top.
1070 //
1071 if (IsVtfFile ((EFI_FFS_FILE_HEADER *) FileBuffer)) {
1072 if ((UINTN) *VtfFileImage == (UINTN) FvImage->Eof) {
1073 //
1074 // No previous VTF, add this one.
1075 //
1076 *VtfFileImage = (EFI_FFS_FILE_HEADER *) (UINTN) ((UINTN) FvImage->FileImage + FvInfo->Size - FileSize);
1077 //
1078 // Sanity check. The file MUST align appropriately
1079 //
1080 if (((UINTN) *VtfFileImage + sizeof (EFI_FFS_FILE_HEADER) - (UINTN) FvImage->FileImage) % (1 << CurrentFileAlignment)) {
1081 Error (NULL, 0, 3000, "Invalid", "VTF file cannot be aligned on a %u-byte boundary.", (unsigned) (1 << CurrentFileAlignment));
1082 free (FileBuffer);
1083 return EFI_ABORTED;
1084 }
1085 //
1086 // Rebase the PE or TE image in FileBuffer of FFS file for XIP
1087 // Rebase for the debug genfvmap tool
1088 //
1089 Status = FfsRebase (FvInfo, FvInfo->FvFiles[Index], (EFI_FFS_FILE_HEADER *) FileBuffer, (UINTN) *VtfFileImage - (UINTN) FvImage->FileImage, FvMapFile);
1090 if (EFI_ERROR (Status)) {
1091 Error (NULL, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo->FvFiles[Index]);
1092 return Status;
1093 }
1094 //
1095 // copy VTF File
1096 //
1097 memcpy (*VtfFileImage, FileBuffer, FileSize);
1098
1099 PrintGuidToBuffer ((EFI_GUID *) FileBuffer, FileGuidString, sizeof (FileGuidString), TRUE);
1100 fprintf (FvReportFile, "0x%08X %s\n", (unsigned)(UINTN) (((UINT8 *)*VtfFileImage) - (UINTN)FvImage->FileImage), FileGuidString);
1101
1102 free (FileBuffer);
1103 DebugMsg (NULL, 0, 9, "Add VTF FFS file in FV image", NULL);
1104 return EFI_SUCCESS;
1105 } else {
1106 //
1107 // Already found a VTF file.
1108 //
1109 Error (NULL, 0, 3000, "Invalid", "multiple VTF files are not permitted within a single FV.");
1110 free (FileBuffer);
1111 return EFI_ABORTED;
1112 }
1113 }
1114
1115 //
1116 // Add pad file if necessary
1117 //
1118 Status = AddPadFile (FvImage, 1 << CurrentFileAlignment, *VtfFileImage, NULL);
1119 if (EFI_ERROR (Status)) {
1120 Error (NULL, 0, 4002, "Resource", "FV space is full, could not add pad file for data alignment property.");
1121 free (FileBuffer);
1122 return EFI_ABORTED;
1123 }
1124 //
1125 // Add file
1126 //
1127 if ((UINTN) (FvImage->CurrentFilePointer + FileSize) <= (UINTN) (*VtfFileImage)) {
1128 //
1129 // Rebase the PE or TE image in FileBuffer of FFS file for XIP.
1130 // Rebase Bs and Rt drivers for the debug genfvmap tool.
1131 //
1132 Status = FfsRebase (FvInfo, FvInfo->FvFiles[Index], (EFI_FFS_FILE_HEADER *) FileBuffer, (UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage, FvMapFile);
1133 if (EFI_ERROR (Status)) {
1134 Error (NULL, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo->FvFiles[Index]);
1135 return Status;
1136 }
1137 //
1138 // Copy the file
1139 //
1140 memcpy (FvImage->CurrentFilePointer, FileBuffer, FileSize);
1141 PrintGuidToBuffer ((EFI_GUID *) FileBuffer, FileGuidString, sizeof (FileGuidString), TRUE);
1142 fprintf (FvReportFile, "0x%08X %s\n", (unsigned) (FvImage->CurrentFilePointer - FvImage->FileImage), FileGuidString);
1143 FvImage->CurrentFilePointer += FileSize;
1144 } else {
1145 Error (NULL, 0, 4002, "Resource", "FV space is full, cannot add file %s.", FvInfo->FvFiles[Index]);
1146 free (FileBuffer);
1147 return EFI_ABORTED;
1148 }
1149 //
1150 // Make next file start at QWord Boundry
1151 //
1152 while (((UINTN) FvImage->CurrentFilePointer & (EFI_FFS_FILE_HEADER_ALIGNMENT - 1)) != 0) {
1153 FvImage->CurrentFilePointer++;
1154 }
1155
1156 Done:
1157 //
1158 // Free allocated memory.
1159 //
1160 free (FileBuffer);
1161
1162 return EFI_SUCCESS;
1163 }
1164
1165 EFI_STATUS
1166 PadFvImage (
1167 IN MEMORY_FILE *FvImage,
1168 IN EFI_FFS_FILE_HEADER *VtfFileImage
1169 )
1170 /*++
1171
1172 Routine Description:
1173
1174 This function places a pad file between the last file in the FV and the VTF
1175 file if the VTF file exists.
1176
1177 Arguments:
1178
1179 FvImage Memory file for the FV memory image
1180 VtfFileImage The address of the VTF file. If this is the end of the FV
1181 image, no VTF exists and no pad file is needed.
1182
1183 Returns:
1184
1185 EFI_SUCCESS Completed successfully.
1186 EFI_INVALID_PARAMETER One of the input parameters was NULL.
1187
1188 --*/
1189 {
1190 EFI_FFS_FILE_HEADER *PadFile;
1191 UINTN FileSize;
1192
1193 //
1194 // If there is no VTF or the VTF naturally follows the previous file without a
1195 // pad file, then there's nothing to do
1196 //
1197 if ((UINTN) VtfFileImage == (UINTN) FvImage->Eof || \
1198 ((UINTN) VtfFileImage == (UINTN) FvImage->CurrentFilePointer)) {
1199 return EFI_SUCCESS;
1200 }
1201
1202 if ((UINTN) VtfFileImage < (UINTN) FvImage->CurrentFilePointer) {
1203 return EFI_INVALID_PARAMETER;
1204 }
1205
1206 //
1207 // Pad file starts at beginning of free space
1208 //
1209 PadFile = (EFI_FFS_FILE_HEADER *) FvImage->CurrentFilePointer;
1210
1211 //
1212 // write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
1213 //
1214 PadFile->Type = EFI_FV_FILETYPE_FFS_PAD;
1215 PadFile->Attributes = 0;
1216
1217 //
1218 // FileSize includes the EFI_FFS_FILE_HEADER
1219 //
1220 FileSize = (UINTN) VtfFileImage - (UINTN) FvImage->CurrentFilePointer;
1221 PadFile->Size[0] = (UINT8) (FileSize & 0x000000FF);
1222 PadFile->Size[1] = (UINT8) ((FileSize & 0x0000FF00) >> 8);
1223 PadFile->Size[2] = (UINT8) ((FileSize & 0x00FF0000) >> 16);
1224
1225 //
1226 // Fill in checksums and state, must be zero during checksum calculation.
1227 //
1228 PadFile->IntegrityCheck.Checksum.Header = 0;
1229 PadFile->IntegrityCheck.Checksum.File = 0;
1230 PadFile->State = 0;
1231 PadFile->IntegrityCheck.Checksum.Header = CalculateChecksum8 ((UINT8 *) PadFile, sizeof (EFI_FFS_FILE_HEADER));
1232 PadFile->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;
1233
1234 PadFile->State = EFI_FILE_HEADER_CONSTRUCTION | EFI_FILE_HEADER_VALID | EFI_FILE_DATA_VALID;
1235
1236 UpdateFfsFileState (
1237 (EFI_FFS_FILE_HEADER *) PadFile,
1238 (EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage
1239 );
1240 //
1241 // Update the current FV pointer
1242 //
1243 FvImage->CurrentFilePointer = FvImage->Eof;
1244
1245 return EFI_SUCCESS;
1246 }
1247
1248 EFI_STATUS
1249 UpdateResetVector (
1250 IN MEMORY_FILE *FvImage,
1251 IN FV_INFO *FvInfo,
1252 IN EFI_FFS_FILE_HEADER *VtfFile
1253 )
1254 /*++
1255
1256 Routine Description:
1257
1258 This parses the FV looking for the PEI core and then plugs the address into
1259 the SALE_ENTRY point of the BSF/VTF for IPF and does BUGBUG TBD action to
1260 complete an IA32 Bootstrap FV.
1261
1262 Arguments:
1263
1264 FvImage Memory file for the FV memory image
1265 FvInfo Information read from INF file.
1266 VtfFile Pointer to the VTF file in the FV image.
1267
1268 Returns:
1269
1270 EFI_SUCCESS Function Completed successfully.
1271 EFI_ABORTED Error encountered.
1272 EFI_INVALID_PARAMETER A required parameter was NULL.
1273 EFI_NOT_FOUND PEI Core file not found.
1274
1275 --*/
1276 {
1277 EFI_FFS_FILE_HEADER *PeiCoreFile;
1278 EFI_FFS_FILE_HEADER *SecCoreFile;
1279 EFI_STATUS Status;
1280 EFI_FILE_SECTION_POINTER Pe32Section;
1281 UINT32 EntryPoint;
1282 UINT32 BaseOfCode;
1283 UINT16 MachineType;
1284 EFI_PHYSICAL_ADDRESS PeiCorePhysicalAddress;
1285 EFI_PHYSICAL_ADDRESS SecCorePhysicalAddress;
1286 EFI_PHYSICAL_ADDRESS *SecCoreEntryAddressPtr;
1287 INT32 Ia32SecEntryOffset;
1288 UINT32 *Ia32ResetAddressPtr;
1289 UINT8 *BytePointer;
1290 UINT8 *BytePointer2;
1291 UINT16 *WordPointer;
1292 UINT16 CheckSum;
1293 UINT32 IpiVector;
1294 UINTN Index;
1295 EFI_FFS_FILE_STATE SavedState;
1296 UINT64 FitAddress;
1297 FIT_TABLE *FitTablePtr;
1298 BOOLEAN Vtf0Detected;
1299
1300 //
1301 // Verify input parameters
1302 //
1303 if (FvImage == NULL || FvInfo == NULL || VtfFile == NULL) {
1304 return EFI_INVALID_PARAMETER;
1305 }
1306 //
1307 // Initialize FV library
1308 //
1309 InitializeFvLib (FvImage->FileImage, FvInfo->Size);
1310
1311 //
1312 // Verify VTF file
1313 //
1314 Status = VerifyFfsFile (VtfFile);
1315 if (EFI_ERROR (Status)) {
1316 return EFI_INVALID_PARAMETER;
1317 }
1318
1319 if (
1320 (((UINTN)FvImage->Eof - (UINTN)FvImage->FileImage) >=
1321 IA32_X64_VTF_SIGNATURE_OFFSET) &&
1322 (*(UINT32 *)(VOID*)((UINTN) FvImage->Eof -
1323 IA32_X64_VTF_SIGNATURE_OFFSET) ==
1324 IA32_X64_VTF0_SIGNATURE)
1325 ) {
1326 Vtf0Detected = TRUE;
1327 } else {
1328 Vtf0Detected = FALSE;
1329 }
1330
1331 //
1332 // Find the Sec Core
1333 //
1334 Status = GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE, 1, &SecCoreFile);
1335 if (EFI_ERROR (Status) || SecCoreFile == NULL) {
1336 if (Vtf0Detected) {
1337 //
1338 // If the SEC core file is not found, but the VTF-0 signature
1339 // is found, we'll treat it as a VTF-0 'Volume Top File'.
1340 // This means no modifications are required to the VTF.
1341 //
1342 return EFI_SUCCESS;
1343 }
1344
1345 Error (NULL, 0, 3000, "Invalid", "could not find the SEC core file in the FV.");
1346 return EFI_ABORTED;
1347 }
1348 //
1349 // Sec Core found, now find PE32 section
1350 //
1351 Status = GetSectionByType (SecCoreFile, EFI_SECTION_PE32, 1, &Pe32Section);
1352 if (Status == EFI_NOT_FOUND) {
1353 Status = GetSectionByType (SecCoreFile, EFI_SECTION_TE, 1, &Pe32Section);
1354 }
1355
1356 if (EFI_ERROR (Status)) {
1357 Error (NULL, 0, 3000, "Invalid", "could not find a PE32 section in the SEC core file.");
1358 return EFI_ABORTED;
1359 }
1360
1361 Status = GetPe32Info (
1362 (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),
1363 &EntryPoint,
1364 &BaseOfCode,
1365 &MachineType
1366 );
1367
1368 if (EFI_ERROR (Status)) {
1369 Error (NULL, 0, 3000, "Invalid", "could not get the PE32 entry point for the SEC core.");
1370 return EFI_ABORTED;
1371 }
1372
1373 if (
1374 Vtf0Detected &&
1375 (MachineType == EFI_IMAGE_MACHINE_IA32 ||
1376 MachineType == EFI_IMAGE_MACHINE_X64)
1377 ) {
1378 //
1379 // If the SEC core code is IA32 or X64 and the VTF-0 signature
1380 // is found, we'll treat it as a VTF-0 'Volume Top File'.
1381 // This means no modifications are required to the VTF.
1382 //
1383 return EFI_SUCCESS;
1384 }
1385
1386 //
1387 // Physical address is FV base + offset of PE32 + offset of the entry point
1388 //
1389 SecCorePhysicalAddress = FvInfo->BaseAddress;
1390 SecCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32) - (UINTN) FvImage->FileImage;
1391 SecCorePhysicalAddress += EntryPoint;
1392 DebugMsg (NULL, 0, 9, "SecCore physical entry point address", "Address = 0x%llX", (unsigned long long) SecCorePhysicalAddress);
1393
1394 //
1395 // Find the PEI Core
1396 //
1397 Status = GetFileByType (EFI_FV_FILETYPE_PEI_CORE, 1, &PeiCoreFile);
1398 if (EFI_ERROR (Status) || PeiCoreFile == NULL) {
1399 Error (NULL, 0, 3000, "Invalid", "could not find the PEI core in the FV.");
1400 return EFI_ABORTED;
1401 }
1402 //
1403 // PEI Core found, now find PE32 or TE section
1404 //
1405 Status = GetSectionByType (PeiCoreFile, EFI_SECTION_PE32, 1, &Pe32Section);
1406 if (Status == EFI_NOT_FOUND) {
1407 Status = GetSectionByType (PeiCoreFile, EFI_SECTION_TE, 1, &Pe32Section);
1408 }
1409
1410 if (EFI_ERROR (Status)) {
1411 Error (NULL, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file.");
1412 return EFI_ABORTED;
1413 }
1414
1415 Status = GetPe32Info (
1416 (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),
1417 &EntryPoint,
1418 &BaseOfCode,
1419 &MachineType
1420 );
1421
1422 if (EFI_ERROR (Status)) {
1423 Error (NULL, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core.");
1424 return EFI_ABORTED;
1425 }
1426 //
1427 // Physical address is FV base + offset of PE32 + offset of the entry point
1428 //
1429 PeiCorePhysicalAddress = FvInfo->BaseAddress;
1430 PeiCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32) - (UINTN) FvImage->FileImage;
1431 PeiCorePhysicalAddress += EntryPoint;
1432 DebugMsg (NULL, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress);
1433
1434 if (MachineType == EFI_IMAGE_MACHINE_IA64) {
1435 //
1436 // Update PEI_CORE address
1437 //
1438 //
1439 // Set the uncached attribute bit in the physical address
1440 //
1441 PeiCorePhysicalAddress |= 0x8000000000000000ULL;
1442
1443 //
1444 // Check if address is aligned on a 16 byte boundary
1445 //
1446 if (PeiCorePhysicalAddress & 0xF) {
1447 Error (NULL, 0, 3000, "Invalid",
1448 "PEI_CORE entry point is not aligned on a 16 byte boundary, address specified is %llXh.",
1449 (unsigned long long) PeiCorePhysicalAddress
1450 );
1451 return EFI_ABORTED;
1452 }
1453 //
1454 // First Get the FIT table address
1455 //
1456 FitAddress = (*(UINT64 *) (FvImage->Eof - IPF_FIT_ADDRESS_OFFSET)) & 0xFFFFFFFF;
1457
1458 FitTablePtr = (FIT_TABLE *) (FvImage->FileImage + (FitAddress - FvInfo->BaseAddress));
1459
1460 Status = UpdatePeiCoreEntryInFit (FitTablePtr, PeiCorePhysicalAddress);
1461
1462 if (!EFI_ERROR (Status)) {
1463 UpdateFitCheckSum (FitTablePtr);
1464 }
1465
1466 //
1467 // Update SEC_CORE address
1468 //
1469 //
1470 // Set the uncached attribute bit in the physical address
1471 //
1472 SecCorePhysicalAddress |= 0x8000000000000000ULL;
1473 //
1474 // Check if address is aligned on a 16 byte boundary
1475 //
1476 if (SecCorePhysicalAddress & 0xF) {
1477 Error (NULL, 0, 3000, "Invalid",
1478 "SALE_ENTRY entry point is not aligned on a 16 byte boundary, address specified is %llXh.",
1479 (unsigned long long) SecCorePhysicalAddress
1480 );
1481 return EFI_ABORTED;
1482 }
1483 //
1484 // Update the address
1485 //
1486 SecCoreEntryAddressPtr = (EFI_PHYSICAL_ADDRESS *) ((UINTN) FvImage->Eof - IPF_SALE_ENTRY_ADDRESS_OFFSET);
1487 *SecCoreEntryAddressPtr = SecCorePhysicalAddress;
1488
1489 } else if (MachineType == EFI_IMAGE_MACHINE_IA32 || MachineType == EFI_IMAGE_MACHINE_X64) {
1490 //
1491 // Get the location to update
1492 //
1493 Ia32ResetAddressPtr = (UINT32 *) ((UINTN) FvImage->Eof - IA32_PEI_CORE_ENTRY_OFFSET);
1494
1495 //
1496 // Write lower 32 bits of physical address for Pei Core entry
1497 //
1498 *Ia32ResetAddressPtr = (UINT32) PeiCorePhysicalAddress;
1499
1500 //
1501 // Write SecCore Entry point relative address into the jmp instruction in reset vector.
1502 //
1503 Ia32ResetAddressPtr = (UINT32 *) ((UINTN) FvImage->Eof - IA32_SEC_CORE_ENTRY_OFFSET);
1504
1505 Ia32SecEntryOffset = (INT32) (SecCorePhysicalAddress - (FV_IMAGES_TOP_ADDRESS - IA32_SEC_CORE_ENTRY_OFFSET + 2));
1506 if (Ia32SecEntryOffset <= -65536) {
1507 Error (NULL, 0, 3000, "Invalid", "The SEC EXE file size is too large, it must be less than 64K.");
1508 return STATUS_ERROR;
1509 }
1510
1511 *(UINT16 *) Ia32ResetAddressPtr = (UINT16) Ia32SecEntryOffset;
1512
1513 //
1514 // Update the BFV base address
1515 //
1516 Ia32ResetAddressPtr = (UINT32 *) ((UINTN) FvImage->Eof - 4);
1517 *Ia32ResetAddressPtr = (UINT32) (FvInfo->BaseAddress);
1518 DebugMsg (NULL, 0, 9, "update BFV base address in the top FV image", "BFV base address = 0x%llX.", (unsigned long long) FvInfo->BaseAddress);
1519
1520 //
1521 // Update the Startup AP in the FVH header block ZeroVector region.
1522 //
1523 BytePointer = (UINT8 *) ((UINTN) FvImage->FileImage);
1524 if (FvInfo->Size <= 0x10000) {
1525 BytePointer2 = m64kRecoveryStartupApDataArray;
1526 } else if (FvInfo->Size <= 0x20000) {
1527 BytePointer2 = m128kRecoveryStartupApDataArray;
1528 } else {
1529 BytePointer2 = m128kRecoveryStartupApDataArray;
1530 //
1531 // Find the position to place Ap reset vector, the offset
1532 // between the position and the end of Fvrecovery.fv file
1533 // should not exceed 128kB to prevent Ap reset vector from
1534 // outside legacy E and F segment
1535 //
1536 Status = FindApResetVectorPosition (FvImage, &BytePointer);
1537 if (EFI_ERROR (Status)) {
1538 Error (NULL, 0, 3000, "Invalid", "FV image does not have enough space to place AP reset vector. The FV image needs to reserve at least 4KB of unused space.");
1539 return EFI_ABORTED;
1540 }
1541 }
1542
1543 for (Index = 0; Index < SIZEOF_STARTUP_DATA_ARRAY; Index++) {
1544 BytePointer[Index] = BytePointer2[Index];
1545 }
1546 //
1547 // Calculate the checksum
1548 //
1549 CheckSum = 0x0000;
1550 WordPointer = (UINT16 *) (BytePointer);
1551 for (Index = 0; Index < SIZEOF_STARTUP_DATA_ARRAY / 2; Index++) {
1552 CheckSum = (UINT16) (CheckSum + ((UINT16) *WordPointer));
1553 WordPointer++;
1554 }
1555 //
1556 // Update the checksum field
1557 //
1558 WordPointer = (UINT16 *) (BytePointer + SIZEOF_STARTUP_DATA_ARRAY - 2);
1559 *WordPointer = (UINT16) (0x10000 - (UINT32) CheckSum);
1560
1561 //
1562 // IpiVector at the 4k aligned address in the top 2 blocks in the PEI FV.
1563 //
1564 IpiVector = (UINT32) (FV_IMAGES_TOP_ADDRESS - ((UINTN) FvImage->Eof - (UINTN) BytePointer));
1565 DebugMsg (NULL, 0, 9, "Startup AP Vector address", "IpiVector at 0x%X", (unsigned) IpiVector);
1566 if ((IpiVector & 0xFFF) != 0) {
1567 Error (NULL, 0, 3000, "Invalid", "Startup AP Vector address are not 4K aligned, because the FV size is not 4K aligned");
1568 return EFI_ABORTED;
1569 }
1570 IpiVector = IpiVector >> 12;
1571 IpiVector = IpiVector & 0xFF;
1572
1573 //
1574 // Write IPI Vector at Offset FvrecoveryFileSize - 8
1575 //
1576 Ia32ResetAddressPtr = (UINT32 *) ((UINTN) FvImage->Eof - 8);
1577 *Ia32ResetAddressPtr = IpiVector;
1578 } else if (MachineType == EFI_IMAGE_MACHINE_ARMT) {
1579 //
1580 // Since the ARM reset vector is in the FV Header you really don't need a
1581 // Volume Top File, but if you have one for some reason don't crash...
1582 //
1583 } else {
1584 Error (NULL, 0, 3000, "Invalid", "machine type=0x%X in PEI core.", MachineType);
1585 return EFI_ABORTED;
1586 }
1587
1588 //
1589 // Now update file checksum
1590 //
1591 SavedState = VtfFile->State;
1592 VtfFile->IntegrityCheck.Checksum.File = 0;
1593 VtfFile->State = 0;
1594 if (VtfFile->Attributes & FFS_ATTRIB_CHECKSUM) {
1595 VtfFile->IntegrityCheck.Checksum.File = CalculateChecksum8 (
1596 (UINT8 *) (VtfFile + 1),
1597 GetLength (VtfFile->Size) - sizeof (EFI_FFS_FILE_HEADER)
1598 );
1599 } else {
1600 VtfFile->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;
1601 }
1602
1603 VtfFile->State = SavedState;
1604
1605 return EFI_SUCCESS;
1606 }
1607
1608
1609 EFI_STATUS
1610 UpdateArmResetVectorIfNeeded (
1611 IN MEMORY_FILE *FvImage,
1612 IN FV_INFO *FvInfo
1613 )
1614 /*++
1615
1616 Routine Description:
1617 This parses the FV looking for SEC and patches that address into the
1618 beginning of the FV header.
1619
1620 For ARM the reset vector is at 0x00000000 or 0xFFFF0000.
1621 This would commonly map to the first entry in the ROM.
1622 ARM Exceptions:
1623 Reset +0
1624 Undefined +4
1625 SWI +8
1626 Prefetch Abort +12
1627 Data Abort +16
1628 IRQ +20
1629 FIQ +24
1630
1631 We support two schemes on ARM.
1632 1) Beginning of the FV is the reset vector
1633 2) Reset vector is data bytes FDF file and that code branches to reset vector
1634 in the beginning of the FV (fixed size offset).
1635
1636
1637 Need to have the jump for the reset vector at location zero.
1638 We also need to store the address or PEI (if it exists).
1639 We stub out a return from interrupt in case the debugger
1640 is using SWI.
1641 The optional entry to the common exception handler is
1642 to support full featured exception handling from ROM and is currently
1643 not support by this tool.
1644
1645 Arguments:
1646 FvImage Memory file for the FV memory image
1647 FvInfo Information read from INF file.
1648
1649 Returns:
1650
1651 EFI_SUCCESS Function Completed successfully.
1652 EFI_ABORTED Error encountered.
1653 EFI_INVALID_PARAMETER A required parameter was NULL.
1654 EFI_NOT_FOUND PEI Core file not found.
1655
1656 --*/
1657 {
1658 EFI_FFS_FILE_HEADER *PeiCoreFile;
1659 EFI_FFS_FILE_HEADER *SecCoreFile;
1660 EFI_STATUS Status;
1661 EFI_FILE_SECTION_POINTER Pe32Section;
1662 UINT32 EntryPoint;
1663 UINT32 BaseOfCode;
1664 UINT16 MachineType;
1665 EFI_PHYSICAL_ADDRESS PeiCorePhysicalAddress;
1666 EFI_PHYSICAL_ADDRESS SecCorePhysicalAddress;
1667 INT32 ResetVector[4]; // 0 - is branch relative to SEC entry point
1668 // 1 - PEI Entry Point
1669 // 2 - movs pc,lr for a SWI handler
1670 // 3 - Place holder for Common Exception Handler
1671
1672 //
1673 // Verify input parameters
1674 //
1675 if (FvImage == NULL || FvInfo == NULL) {
1676 return EFI_INVALID_PARAMETER;
1677 }
1678 //
1679 // Initialize FV library
1680 //
1681 InitializeFvLib (FvImage->FileImage, FvInfo->Size);
1682
1683 //
1684 // Find the Sec Core
1685 //
1686 Status = GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE, 1, &SecCoreFile);
1687 if (EFI_ERROR (Status) || SecCoreFile == NULL) {
1688 //
1689 // Maybe hardware does SEC job and we only have PEI Core?
1690 //
1691
1692 //
1693 // Find the PEI Core. It may not exist if SEC loads DXE core directly
1694 //
1695 PeiCorePhysicalAddress = 0;
1696 Status = GetFileByType (EFI_FV_FILETYPE_PEI_CORE, 1, &PeiCoreFile);
1697 if (!EFI_ERROR (Status) && PeiCoreFile != NULL) {
1698 //
1699 // PEI Core found, now find PE32 or TE section
1700 //
1701 Status = GetSectionByType (PeiCoreFile, EFI_SECTION_PE32, 1, &Pe32Section);
1702 if (Status == EFI_NOT_FOUND) {
1703 Status = GetSectionByType (PeiCoreFile, EFI_SECTION_TE, 1, &Pe32Section);
1704 }
1705
1706 if (EFI_ERROR (Status)) {
1707 Error (NULL, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file!");
1708 return EFI_ABORTED;
1709 }
1710
1711 Status = GetPe32Info (
1712 (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),
1713 &EntryPoint,
1714 &BaseOfCode,
1715 &MachineType
1716 );
1717
1718 if (EFI_ERROR (Status)) {
1719 Error (NULL, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core!");
1720 return EFI_ABORTED;
1721 }
1722 //
1723 // Physical address is FV base + offset of PE32 + offset of the entry point
1724 //
1725 PeiCorePhysicalAddress = FvInfo->BaseAddress;
1726 PeiCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32) - (UINTN) FvImage->FileImage;
1727 PeiCorePhysicalAddress += EntryPoint;
1728 DebugMsg (NULL, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress);
1729
1730 if (MachineType == EFI_IMAGE_MACHINE_ARMT) {
1731 memset (ResetVector, 0, sizeof (ResetVector));
1732 // Address of PEI Core, if we have one
1733 ResetVector[1] = (UINT32)PeiCorePhysicalAddress;
1734 }
1735
1736 //
1737 // Copy to the beginning of the FV
1738 //
1739 memcpy ((UINT8 *) ((UINTN) FvImage->FileImage), ResetVector, sizeof (ResetVector));
1740
1741 }
1742
1743 return EFI_SUCCESS;
1744 }
1745
1746 //
1747 // Sec Core found, now find PE32 section
1748 //
1749 Status = GetSectionByType (SecCoreFile, EFI_SECTION_PE32, 1, &Pe32Section);
1750 if (Status == EFI_NOT_FOUND) {
1751 Status = GetSectionByType (SecCoreFile, EFI_SECTION_TE, 1, &Pe32Section);
1752 }
1753
1754 if (EFI_ERROR (Status)) {
1755 Error (NULL, 0, 3000, "Invalid", "could not find a PE32 section in the SEC core file.");
1756 return EFI_ABORTED;
1757 }
1758
1759 Status = GetPe32Info (
1760 (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),
1761 &EntryPoint,
1762 &BaseOfCode,
1763 &MachineType
1764 );
1765 if (EFI_ERROR (Status)) {
1766 Error (NULL, 0, 3000, "Invalid", "could not get the PE32 entry point for the SEC core.");
1767 return EFI_ABORTED;
1768 }
1769
1770 if (MachineType != EFI_IMAGE_MACHINE_ARMT) {
1771 //
1772 // If SEC is not ARM we have nothing to do
1773 //
1774 return EFI_SUCCESS;
1775 }
1776
1777 //
1778 // Physical address is FV base + offset of PE32 + offset of the entry point
1779 //
1780 SecCorePhysicalAddress = FvInfo->BaseAddress;
1781 SecCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32) - (UINTN) FvImage->FileImage;
1782 SecCorePhysicalAddress += EntryPoint;
1783 DebugMsg (NULL, 0, 9, "SecCore physical entry point address", "Address = 0x%llX", (unsigned long long) SecCorePhysicalAddress);
1784
1785 //
1786 // Find the PEI Core. It may not exist if SEC loads DXE core directly
1787 //
1788 PeiCorePhysicalAddress = 0;
1789 Status = GetFileByType (EFI_FV_FILETYPE_PEI_CORE, 1, &PeiCoreFile);
1790 if (!EFI_ERROR (Status) && PeiCoreFile != NULL) {
1791 //
1792 // PEI Core found, now find PE32 or TE section
1793 //
1794 Status = GetSectionByType (PeiCoreFile, EFI_SECTION_PE32, 1, &Pe32Section);
1795 if (Status == EFI_NOT_FOUND) {
1796 Status = GetSectionByType (PeiCoreFile, EFI_SECTION_TE, 1, &Pe32Section);
1797 }
1798
1799 if (EFI_ERROR (Status)) {
1800 Error (NULL, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file!");
1801 return EFI_ABORTED;
1802 }
1803
1804 Status = GetPe32Info (
1805 (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),
1806 &EntryPoint,
1807 &BaseOfCode,
1808 &MachineType
1809 );
1810
1811 if (EFI_ERROR (Status)) {
1812 Error (NULL, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core!");
1813 return EFI_ABORTED;
1814 }
1815 //
1816 // Physical address is FV base + offset of PE32 + offset of the entry point
1817 //
1818 PeiCorePhysicalAddress = FvInfo->BaseAddress;
1819 PeiCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32) - (UINTN) FvImage->FileImage;
1820 PeiCorePhysicalAddress += EntryPoint;
1821 DebugMsg (NULL, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress);
1822 }
1823
1824
1825 // B SecEntryPoint - signed_immed_24 part +/-32MB offset
1826 // on ARM, the PC is always 8 ahead, so we're not really jumping from the base address, but from base address + 8
1827 ResetVector[0] = (INT32)(SecCorePhysicalAddress - FvInfo->BaseAddress - 8) >> 2;
1828
1829 if (ResetVector[0] > 0x00FFFFFF) {
1830 Error (NULL, 0, 3000, "Invalid", "SEC Entry point must be within 32MB of the start of the FV");
1831 return EFI_ABORTED;
1832 }
1833
1834 // Add opcode for an uncondional branch with no link. AKA B SecEntryPoint
1835 ResetVector[0] |= 0xEB000000;
1836
1837
1838 // Address of PEI Core, if we have one
1839 ResetVector[1] = (UINT32)PeiCorePhysicalAddress;
1840
1841 // SWI handler movs pc,lr. Just in case a debugger uses SWI
1842 ResetVector[2] = 0xE1B0F07E;
1843
1844 // Place holder to support a common interrupt handler from ROM.
1845 // Currently not suppprted. For this to be used the reset vector would not be in this FV
1846 // and the exception vectors would be hard coded in the ROM and just through this address
1847 // to find a common handler in the a module in the FV.
1848 ResetVector[3] = 0;
1849
1850 //
1851 // Copy to the beginning of the FV
1852 //
1853 memcpy ((UINT8 *) ((UINTN) FvImage->FileImage), ResetVector, sizeof (ResetVector));
1854
1855 DebugMsg (NULL, 0, 9, "Update Reset vector in FV Header", NULL);
1856
1857 return EFI_SUCCESS;
1858 }
1859
1860 EFI_STATUS
1861 GetPe32Info (
1862 IN UINT8 *Pe32,
1863 OUT UINT32 *EntryPoint,
1864 OUT UINT32 *BaseOfCode,
1865 OUT UINT16 *MachineType
1866 )
1867 /*++
1868
1869 Routine Description:
1870
1871 Retrieves the PE32 entry point offset and machine type from PE image or TeImage.
1872 See EfiImage.h for machine types. The entry point offset is from the beginning
1873 of the PE32 buffer passed in.
1874
1875 Arguments:
1876
1877 Pe32 Beginning of the PE32.
1878 EntryPoint Offset from the beginning of the PE32 to the image entry point.
1879 BaseOfCode Base address of code.
1880 MachineType Magic number for the machine type.
1881
1882 Returns:
1883
1884 EFI_SUCCESS Function completed successfully.
1885 EFI_ABORTED Error encountered.
1886 EFI_INVALID_PARAMETER A required parameter was NULL.
1887 EFI_UNSUPPORTED The operation is unsupported.
1888
1889 --*/
1890 {
1891 EFI_IMAGE_DOS_HEADER *DosHeader;
1892 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
1893 EFI_TE_IMAGE_HEADER *TeHeader;
1894
1895 //
1896 // Verify input parameters
1897 //
1898 if (Pe32 == NULL) {
1899 return EFI_INVALID_PARAMETER;
1900 }
1901
1902 //
1903 // First check whether it is one TE Image.
1904 //
1905 TeHeader = (EFI_TE_IMAGE_HEADER *) Pe32;
1906 if (TeHeader->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
1907 //
1908 // By TeImage Header to get output
1909 //
1910 *EntryPoint = TeHeader->AddressOfEntryPoint + sizeof (EFI_TE_IMAGE_HEADER) - TeHeader->StrippedSize;
1911 *BaseOfCode = TeHeader->BaseOfCode + sizeof (EFI_TE_IMAGE_HEADER) - TeHeader->StrippedSize;
1912 *MachineType = TeHeader->Machine;
1913 } else {
1914
1915 //
1916 // Then check whether
1917 // First is the DOS header
1918 //
1919 DosHeader = (EFI_IMAGE_DOS_HEADER *) Pe32;
1920
1921 //
1922 // Verify DOS header is expected
1923 //
1924 if (DosHeader->e_magic != EFI_IMAGE_DOS_SIGNATURE) {
1925 Error (NULL, 0, 3000, "Invalid", "Unknown magic number in the DOS header, 0x%04X.", DosHeader->e_magic);
1926 return EFI_UNSUPPORTED;
1927 }
1928 //
1929 // Immediately following is the NT header.
1930 //
1931 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *) ((UINTN) Pe32 + DosHeader->e_lfanew);
1932
1933 //
1934 // Verify NT header is expected
1935 //
1936 if (ImgHdr->Pe32.Signature != EFI_IMAGE_NT_SIGNATURE) {
1937 Error (NULL, 0, 3000, "Invalid", "Unrecognized image signature 0x%08X.", (unsigned) ImgHdr->Pe32.Signature);
1938 return EFI_UNSUPPORTED;
1939 }
1940 //
1941 // Get output
1942 //
1943 *EntryPoint = ImgHdr->Pe32.OptionalHeader.AddressOfEntryPoint;
1944 *BaseOfCode = ImgHdr->Pe32.OptionalHeader.BaseOfCode;
1945 *MachineType = ImgHdr->Pe32.FileHeader.Machine;
1946 }
1947
1948 //
1949 // Verify machine type is supported
1950 //
1951 if (*MachineType != EFI_IMAGE_MACHINE_IA32 && *MachineType != EFI_IMAGE_MACHINE_IA64 && *MachineType != EFI_IMAGE_MACHINE_X64 && *MachineType != EFI_IMAGE_MACHINE_EBC &&
1952 *MachineType != EFI_IMAGE_MACHINE_ARMT) {
1953 Error (NULL, 0, 3000, "Invalid", "Unrecognized machine type in the PE32 file.");
1954 return EFI_UNSUPPORTED;
1955 }
1956
1957 return EFI_SUCCESS;
1958 }
1959
1960 EFI_STATUS
1961 GenerateFvImage (
1962 IN CHAR8 *InfFileImage,
1963 IN UINTN InfFileSize,
1964 IN CHAR8 *FvFileName,
1965 IN CHAR8 *MapFileName
1966 )
1967 /*++
1968
1969 Routine Description:
1970
1971 This is the main function which will be called from application.
1972
1973 Arguments:
1974
1975 InfFileImage Buffer containing the INF file contents.
1976 InfFileSize Size of the contents of the InfFileImage buffer.
1977 FvFileName Requested name for the FV file.
1978 MapFileName Fv map file to log fv driver information.
1979
1980 Returns:
1981
1982 EFI_SUCCESS Function completed successfully.
1983 EFI_OUT_OF_RESOURCES Could not allocate required resources.
1984 EFI_ABORTED Error encountered.
1985 EFI_INVALID_PARAMETER A required parameter was NULL.
1986
1987 --*/
1988 {
1989 EFI_STATUS Status;
1990 MEMORY_FILE InfMemoryFile;
1991 MEMORY_FILE FvImageMemoryFile;
1992 UINTN Index;
1993 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
1994 EFI_FFS_FILE_HEADER *VtfFileImage;
1995 UINT8 *FvBufferHeader; // to make sure fvimage header 8 type alignment.
1996 UINT8 *FvImage;
1997 UINTN FvImageSize;
1998 FILE *FvFile;
1999 CHAR8 FvMapName [_MAX_PATH];
2000 FILE *FvMapFile;
2001 EFI_FIRMWARE_VOLUME_EXT_HEADER *FvExtHeader;
2002 FILE *FvExtHeaderFile;
2003 UINTN FileSize;
2004 CHAR8 FvReportName[_MAX_PATH];
2005 FILE *FvReportFile;
2006
2007 FvBufferHeader = NULL;
2008 FvFile = NULL;
2009 FvMapFile = NULL;
2010 FvReportFile = NULL;
2011
2012 if (InfFileImage != NULL) {
2013 //
2014 // Initialize file structures
2015 //
2016 InfMemoryFile.FileImage = InfFileImage;
2017 InfMemoryFile.CurrentFilePointer = InfFileImage;
2018 InfMemoryFile.Eof = InfFileImage + InfFileSize;
2019
2020 //
2021 // Parse the FV inf file for header information
2022 //
2023 Status = ParseFvInf (&InfMemoryFile, &mFvDataInfo);
2024 if (EFI_ERROR (Status)) {
2025 Error (NULL, 0, 0003, "Error parsing file", "the input FV INF file.");
2026 return Status;
2027 }
2028 }
2029
2030 //
2031 // Update the file name return values
2032 //
2033 if (FvFileName == NULL && mFvDataInfo.FvName[0] != '\0') {
2034 FvFileName = mFvDataInfo.FvName;
2035 }
2036
2037 if (FvFileName == NULL) {
2038 Error (NULL, 0, 1001, "Missing option", "Output file name");
2039 return EFI_ABORTED;
2040 }
2041
2042 if (mFvDataInfo.FvBlocks[0].Length == 0) {
2043 Error (NULL, 0, 1001, "Missing required argument", "Block Size");
2044 return EFI_ABORTED;
2045 }
2046
2047 //
2048 // Debug message Fv File System Guid
2049 //
2050 if (mFvDataInfo.FvFileSystemGuidSet) {
2051 DebugMsg (NULL, 0, 9, "FV File System Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
2052 (unsigned) mFvDataInfo.FvFileSystemGuid.Data1,
2053 mFvDataInfo.FvFileSystemGuid.Data2,
2054 mFvDataInfo.FvFileSystemGuid.Data3,
2055 mFvDataInfo.FvFileSystemGuid.Data4[0],
2056 mFvDataInfo.FvFileSystemGuid.Data4[1],
2057 mFvDataInfo.FvFileSystemGuid.Data4[2],
2058 mFvDataInfo.FvFileSystemGuid.Data4[3],
2059 mFvDataInfo.FvFileSystemGuid.Data4[4],
2060 mFvDataInfo.FvFileSystemGuid.Data4[5],
2061 mFvDataInfo.FvFileSystemGuid.Data4[6],
2062 mFvDataInfo.FvFileSystemGuid.Data4[7]);
2063 }
2064
2065 //
2066 // Add PI FV extension header
2067 //
2068 FvExtHeader = NULL;
2069 FvExtHeaderFile = NULL;
2070 if (mFvDataInfo.FvExtHeaderFile[0] != 0) {
2071 //
2072 // Open the FV Extension Header file
2073 //
2074 FvExtHeaderFile = fopen (mFvDataInfo.FvExtHeaderFile, "rb");
2075
2076 //
2077 // Get the file size
2078 //
2079 FileSize = _filelength (fileno (FvExtHeaderFile));
2080
2081 //
2082 // Allocate a buffer for the FV Extension Header
2083 //
2084 FvExtHeader = malloc(FileSize);
2085 if (FvExtHeader == NULL) {
2086 fclose (FvExtHeaderFile);
2087 return EFI_OUT_OF_RESOURCES;
2088 }
2089
2090 //
2091 // Read the FV Extension Header
2092 //
2093 fread (FvExtHeader, sizeof (UINT8), FileSize, FvExtHeaderFile);
2094 fclose (FvExtHeaderFile);
2095
2096 //
2097 // See if there is an override for the FV Name GUID
2098 //
2099 if (mFvDataInfo.FvNameGuidSet) {
2100 memcpy (&FvExtHeader->FvName, &mFvDataInfo.FvNameGuid, sizeof (EFI_GUID));
2101 }
2102 memcpy (&mFvDataInfo.FvNameGuid, &FvExtHeader->FvName, sizeof (EFI_GUID));
2103 mFvDataInfo.FvNameGuidSet = TRUE;
2104 } else if (mFvDataInfo.FvNameGuidSet) {
2105 //
2106 // Allocate a buffer for the FV Extension Header
2107 //
2108 FvExtHeader = malloc(sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER));
2109 if (FvExtHeader == NULL) {
2110 return EFI_OUT_OF_RESOURCES;
2111 }
2112 memcpy (&FvExtHeader->FvName, &mFvDataInfo.FvNameGuid, sizeof (EFI_GUID));
2113 FvExtHeader->ExtHeaderSize = sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER);
2114 }
2115
2116 //
2117 // Debug message Fv Name Guid
2118 //
2119 if (mFvDataInfo.FvNameGuidSet) {
2120 DebugMsg (NULL, 0, 9, "FV Name Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
2121 (unsigned) mFvDataInfo.FvNameGuid.Data1,
2122 mFvDataInfo.FvNameGuid.Data2,
2123 mFvDataInfo.FvNameGuid.Data3,
2124 mFvDataInfo.FvNameGuid.Data4[0],
2125 mFvDataInfo.FvNameGuid.Data4[1],
2126 mFvDataInfo.FvNameGuid.Data4[2],
2127 mFvDataInfo.FvNameGuid.Data4[3],
2128 mFvDataInfo.FvNameGuid.Data4[4],
2129 mFvDataInfo.FvNameGuid.Data4[5],
2130 mFvDataInfo.FvNameGuid.Data4[6],
2131 mFvDataInfo.FvNameGuid.Data4[7]);
2132 }
2133
2134 if (CompareGuid (&mFvDataInfo.FvFileSystemGuid, &mEfiFirmwareFileSystem2Guid) == 0) {
2135 mFvDataInfo.IsPiFvImage = TRUE;
2136 }
2137
2138 //
2139 // FvMap file to log the function address of all modules in one Fvimage
2140 //
2141 if (MapFileName != NULL) {
2142 strcpy (FvMapName, MapFileName);
2143 } else {
2144 strcpy (FvMapName, FvFileName);
2145 strcat (FvMapName, ".map");
2146 }
2147 VerboseMsg ("FV Map file name is %s", FvMapName);
2148
2149 //
2150 // FvReport file to log the FV information in one Fvimage
2151 //
2152 strcpy (FvReportName, FvFileName);
2153 strcat (FvReportName, ".txt");
2154
2155 //
2156 // Calculate the FV size and Update Fv Size based on the actual FFS files.
2157 // And Update mFvDataInfo data.
2158 //
2159 Status = CalculateFvSize (&mFvDataInfo);
2160 if (EFI_ERROR (Status)) {
2161 return Status;
2162 }
2163 VerboseMsg ("the generated FV image size is %u bytes", (unsigned) mFvDataInfo.Size);
2164
2165 //
2166 // support fv image and empty fv image
2167 //
2168 FvImageSize = mFvDataInfo.Size;
2169
2170 //
2171 // Allocate the FV, assure FvImage Header 8 byte alignment
2172 //
2173 FvBufferHeader = malloc (FvImageSize + sizeof (UINT64));
2174 if (FvBufferHeader == NULL) {
2175 return EFI_OUT_OF_RESOURCES;
2176 }
2177 FvImage = (UINT8 *) (((UINTN) FvBufferHeader + 7) & ~7);
2178
2179 //
2180 // Initialize the FV to the erase polarity
2181 //
2182 if (mFvDataInfo.FvAttributes == 0) {
2183 //
2184 // Set Default Fv Attribute
2185 //
2186 mFvDataInfo.FvAttributes = FV_DEFAULT_ATTRIBUTE;
2187 }
2188 if (mFvDataInfo.FvAttributes & EFI_FVB2_ERASE_POLARITY) {
2189 memset (FvImage, -1, FvImageSize);
2190 } else {
2191 memset (FvImage, 0, FvImageSize);
2192 }
2193
2194 //
2195 // Initialize FV header
2196 //
2197 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) FvImage;
2198
2199 //
2200 // Initialize the zero vector to all zeros.
2201 //
2202 memset (FvHeader->ZeroVector, 0, 16);
2203
2204 //
2205 // Copy the Fv file system GUID
2206 //
2207 memcpy (&FvHeader->FileSystemGuid, &mFvDataInfo.FvFileSystemGuid, sizeof (EFI_GUID));
2208
2209 FvHeader->FvLength = FvImageSize;
2210 FvHeader->Signature = EFI_FVH_SIGNATURE;
2211 FvHeader->Attributes = mFvDataInfo.FvAttributes;
2212 FvHeader->Revision = EFI_FVH_REVISION;
2213 FvHeader->ExtHeaderOffset = 0;
2214 FvHeader->Reserved[0] = 0;
2215
2216 //
2217 // Copy firmware block map
2218 //
2219 for (Index = 0; mFvDataInfo.FvBlocks[Index].Length != 0; Index++) {
2220 FvHeader->BlockMap[Index].NumBlocks = mFvDataInfo.FvBlocks[Index].NumBlocks;
2221 FvHeader->BlockMap[Index].Length = mFvDataInfo.FvBlocks[Index].Length;
2222 }
2223
2224 //
2225 // Add block map terminator
2226 //
2227 FvHeader->BlockMap[Index].NumBlocks = 0;
2228 FvHeader->BlockMap[Index].Length = 0;
2229
2230 //
2231 // Complete the header
2232 //
2233 FvHeader->HeaderLength = (UINT16) (((UINTN) &(FvHeader->BlockMap[Index + 1])) - (UINTN) FvImage);
2234 FvHeader->Checksum = 0;
2235 FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));
2236
2237 //
2238 // If there is no FFS file, generate one empty FV
2239 //
2240 if (mFvDataInfo.FvFiles[0][0] == 0 && !mFvDataInfo.FvNameGuidSet) {
2241 goto WriteFile;
2242 }
2243
2244 //
2245 // Initialize our "file" view of the buffer
2246 //
2247 FvImageMemoryFile.FileImage = (CHAR8 *)FvImage;
2248 FvImageMemoryFile.CurrentFilePointer = (CHAR8 *)FvImage + FvHeader->HeaderLength;
2249 FvImageMemoryFile.Eof = (CHAR8 *)FvImage + FvImageSize;
2250
2251 //
2252 // Initialize the FV library.
2253 //
2254 InitializeFvLib (FvImageMemoryFile.FileImage, FvImageSize);
2255
2256 //
2257 // Initialize the VTF file address.
2258 //
2259 VtfFileImage = (EFI_FFS_FILE_HEADER *) FvImageMemoryFile.Eof;
2260
2261 //
2262 // Open FvMap file
2263 //
2264 FvMapFile = fopen (FvMapName, "w");
2265 if (FvMapFile == NULL) {
2266 Error (NULL, 0, 0001, "Error opening file", FvMapName);
2267 return EFI_ABORTED;
2268 }
2269
2270 //
2271 // Open FvReport file
2272 //
2273 FvReportFile = fopen(FvReportName, "w");
2274 if (FvReportFile == NULL) {
2275 Error (NULL, 0, 0001, "Error opening file", FvReportName);
2276 return EFI_ABORTED;
2277 }
2278 //
2279 // record FV size information into FvMap file.
2280 //
2281 if (mFvTotalSize != 0) {
2282 fprintf (FvMapFile, EFI_FV_TOTAL_SIZE_STRING);
2283 fprintf (FvMapFile, " = 0x%x\n", (unsigned) mFvTotalSize);
2284 }
2285 if (mFvTakenSize != 0) {
2286 fprintf (FvMapFile, EFI_FV_TAKEN_SIZE_STRING);
2287 fprintf (FvMapFile, " = 0x%x\n", (unsigned) mFvTakenSize);
2288 }
2289 if (mFvTotalSize != 0 && mFvTakenSize != 0) {
2290 fprintf (FvMapFile, EFI_FV_SPACE_SIZE_STRING);
2291 fprintf (FvMapFile, " = 0x%x\n\n", (unsigned) (mFvTotalSize - mFvTakenSize));
2292 }
2293
2294 //
2295 // record FV size information to FvReportFile.
2296 //
2297 fprintf (FvReportFile, "%s = 0x%x\n", EFI_FV_TOTAL_SIZE_STRING, (unsigned) mFvTotalSize);
2298 fprintf (FvReportFile, "%s = 0x%x\n", EFI_FV_TAKEN_SIZE_STRING, (unsigned) mFvTakenSize);
2299
2300 //
2301 // Add PI FV extension header
2302 //
2303 if (FvExtHeader != NULL) {
2304 //
2305 // Add FV Extended Header contents to the FV as a PAD file
2306 //
2307 AddPadFile (&FvImageMemoryFile, 4, VtfFileImage, FvExtHeader);
2308
2309 //
2310 // Fv Extension header change update Fv Header Check sum
2311 //
2312 FvHeader->Checksum = 0;
2313 FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));
2314 }
2315
2316 //
2317 // Add files to FV
2318 //
2319 for (Index = 0; mFvDataInfo.FvFiles[Index][0] != 0; Index++) {
2320 //
2321 // Add the file
2322 //
2323 Status = AddFile (&FvImageMemoryFile, &mFvDataInfo, Index, &VtfFileImage, FvMapFile, FvReportFile);
2324
2325 //
2326 // Exit if error detected while adding the file
2327 //
2328 if (EFI_ERROR (Status)) {
2329 goto Finish;
2330 }
2331 }
2332
2333 //
2334 // If there is a VTF file, some special actions need to occur.
2335 //
2336 if ((UINTN) VtfFileImage != (UINTN) FvImageMemoryFile.Eof) {
2337 //
2338 // Pad from the end of the last file to the beginning of the VTF file.
2339 // If the left space is less than sizeof (EFI_FFS_FILE_HEADER)?
2340 //
2341 Status = PadFvImage (&FvImageMemoryFile, VtfFileImage);
2342 if (EFI_ERROR (Status)) {
2343 Error (NULL, 0, 4002, "Resource", "FV space is full, cannot add pad file between the last file and the VTF file.");
2344 goto Finish;
2345 }
2346 if (!mArm) {
2347 //
2348 // Update reset vector (SALE_ENTRY for IPF)
2349 // Now for IA32 and IA64 platform, the fv which has bsf file must have the
2350 // EndAddress of 0xFFFFFFFF. Thus, only this type fv needs to update the
2351 // reset vector. If the PEI Core is found, the VTF file will probably get
2352 // corrupted by updating the entry point.
2353 //
2354 if ((mFvDataInfo.BaseAddress + mFvDataInfo.Size) == FV_IMAGES_TOP_ADDRESS) {
2355 Status = UpdateResetVector (&FvImageMemoryFile, &mFvDataInfo, VtfFileImage);
2356 if (EFI_ERROR(Status)) {
2357 Error (NULL, 0, 3000, "Invalid", "Could not update the reset vector.");
2358 goto Finish;
2359 }
2360 DebugMsg (NULL, 0, 9, "Update Reset vector in VTF file", NULL);
2361 }
2362 }
2363 }
2364
2365 if (mArm) {
2366 Status = UpdateArmResetVectorIfNeeded (&FvImageMemoryFile, &mFvDataInfo);
2367 if (EFI_ERROR (Status)) {
2368 Error (NULL, 0, 3000, "Invalid", "Could not update the reset vector.");
2369 goto Finish;
2370 }
2371
2372 //
2373 // Update Checksum for FvHeader
2374 //
2375 FvHeader->Checksum = 0;
2376 FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));
2377 }
2378
2379 //
2380 // Update FV Alignment attribute to the largest alignment of all the FFS files in the FV
2381 //
2382 if ((((FvHeader->Attributes & EFI_FVB2_ALIGNMENT) >> 16)) < MaxFfsAlignment) {
2383 FvHeader->Attributes = ((MaxFfsAlignment << 16) | (FvHeader->Attributes & 0xFFFF));
2384 //
2385 // Update Checksum for FvHeader
2386 //
2387 FvHeader->Checksum = 0;
2388 FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));
2389 }
2390
2391 WriteFile:
2392 //
2393 // Write fv file
2394 //
2395 FvFile = fopen (FvFileName, "wb");
2396 if (FvFile == NULL) {
2397 Error (NULL, 0, 0001, "Error opening file", FvFileName);
2398 Status = EFI_ABORTED;
2399 goto Finish;
2400 }
2401
2402 if (fwrite (FvImage, 1, FvImageSize, FvFile) != FvImageSize) {
2403 Error (NULL, 0, 0002, "Error writing file", FvFileName);
2404 Status = EFI_ABORTED;
2405 goto Finish;
2406 }
2407
2408 Finish:
2409 if (FvBufferHeader != NULL) {
2410 free (FvBufferHeader);
2411 }
2412
2413 if (FvExtHeader != NULL) {
2414 free (FvExtHeader);
2415 }
2416
2417 if (FvFile != NULL) {
2418 fflush (FvFile);
2419 fclose (FvFile);
2420 }
2421
2422 if (FvMapFile != NULL) {
2423 fflush (FvMapFile);
2424 fclose (FvMapFile);
2425 }
2426
2427 if (FvReportFile != NULL) {
2428 fflush (FvReportFile);
2429 fclose (FvReportFile);
2430 }
2431 return Status;
2432 }
2433
2434 EFI_STATUS
2435 UpdatePeiCoreEntryInFit (
2436 IN FIT_TABLE *FitTablePtr,
2437 IN UINT64 PeiCorePhysicalAddress
2438 )
2439 /*++
2440
2441 Routine Description:
2442
2443 This function is used to update the Pei Core address in FIT, this can be used by Sec core to pass control from
2444 Sec to Pei Core
2445
2446 Arguments:
2447
2448 FitTablePtr - The pointer of FIT_TABLE.
2449 PeiCorePhysicalAddress - The address of Pei Core entry.
2450
2451 Returns:
2452
2453 EFI_SUCCESS - The PEI_CORE FIT entry was updated successfully.
2454 EFI_NOT_FOUND - Not found the PEI_CORE FIT entry.
2455
2456 --*/
2457 {
2458 FIT_TABLE *TmpFitPtr;
2459 UINTN Index;
2460 UINTN NumFitComponents;
2461
2462 TmpFitPtr = FitTablePtr;
2463 NumFitComponents = TmpFitPtr->CompSize;
2464
2465 for (Index = 0; Index < NumFitComponents; Index++) {
2466 if ((TmpFitPtr->CvAndType & FIT_TYPE_MASK) == COMP_TYPE_FIT_PEICORE) {
2467 TmpFitPtr->CompAddress = PeiCorePhysicalAddress;
2468 return EFI_SUCCESS;
2469 }
2470
2471 TmpFitPtr++;
2472 }
2473
2474 return EFI_NOT_FOUND;
2475 }
2476
2477 VOID
2478 UpdateFitCheckSum (
2479 IN FIT_TABLE *FitTablePtr
2480 )
2481 /*++
2482
2483 Routine Description:
2484
2485 This function is used to update the checksum for FIT.
2486
2487
2488 Arguments:
2489
2490 FitTablePtr - The pointer of FIT_TABLE.
2491
2492 Returns:
2493
2494 None.
2495
2496 --*/
2497 {
2498 if ((FitTablePtr->CvAndType & CHECKSUM_BIT_MASK) >> 7) {
2499 FitTablePtr->CheckSum = 0;
2500 FitTablePtr->CheckSum = CalculateChecksum8 ((UINT8 *) FitTablePtr, FitTablePtr->CompSize * 16);
2501 }
2502 }
2503
2504 EFI_STATUS
2505 CalculateFvSize (
2506 FV_INFO *FvInfoPtr
2507 )
2508 /*++
2509 Routine Description:
2510 Calculate the FV size and Update Fv Size based on the actual FFS files.
2511 And Update FvInfo data.
2512
2513 Arguments:
2514 FvInfoPtr - The pointer to FV_INFO structure.
2515
2516 Returns:
2517 EFI_ABORTED - Ffs Image Error
2518 EFI_SUCCESS - Successfully update FvSize
2519 --*/
2520 {
2521 UINTN CurrentOffset;
2522 UINTN Index;
2523 FILE *fpin;
2524 UINTN FfsFileSize;
2525 UINTN FvExtendHeaderSize;
2526 UINT32 FfsAlignment;
2527 EFI_FFS_FILE_HEADER FfsHeader;
2528 BOOLEAN VtfFileFlag;
2529 UINTN VtfFileSize;
2530
2531 FvExtendHeaderSize = 0;
2532 VtfFileSize = 0;
2533 VtfFileFlag = FALSE;
2534 fpin = NULL;
2535 Index = 0;
2536
2537 //
2538 // Compute size for easy access later
2539 //
2540 FvInfoPtr->Size = 0;
2541 for (Index = 0; FvInfoPtr->FvBlocks[Index].NumBlocks > 0 && FvInfoPtr->FvBlocks[Index].Length > 0; Index++) {
2542 FvInfoPtr->Size += FvInfoPtr->FvBlocks[Index].NumBlocks * FvInfoPtr->FvBlocks[Index].Length;
2543 }
2544
2545 //
2546 // Caculate the required sizes for all FFS files.
2547 //
2548 CurrentOffset = sizeof (EFI_FIRMWARE_VOLUME_HEADER);
2549
2550 for (Index = 1;; Index ++) {
2551 CurrentOffset += sizeof (EFI_FV_BLOCK_MAP_ENTRY);
2552 if (FvInfoPtr->FvBlocks[Index].NumBlocks == 0 || FvInfoPtr->FvBlocks[Index].Length == 0) {
2553 break;
2554 }
2555 }
2556
2557 //
2558 // Calculate PI extension header
2559 //
2560 if (mFvDataInfo.FvExtHeaderFile[0] != '\0') {
2561 fpin = fopen (mFvDataInfo.FvExtHeaderFile, "rb");
2562 if (fpin == NULL) {
2563 Error (NULL, 0, 0001, "Error opening file", mFvDataInfo.FvExtHeaderFile);
2564 return EFI_ABORTED;
2565 }
2566 FvExtendHeaderSize = _filelength (fileno (fpin));
2567 fclose (fpin);
2568 CurrentOffset += sizeof (EFI_FFS_FILE_HEADER) + FvExtendHeaderSize;
2569 CurrentOffset = (CurrentOffset + 7) & (~7);
2570 } else if (mFvDataInfo.FvNameGuidSet) {
2571 CurrentOffset += sizeof (EFI_FFS_FILE_HEADER) + sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER);
2572 CurrentOffset = (CurrentOffset + 7) & (~7);
2573 }
2574
2575 //
2576 // Accumlate every FFS file size.
2577 //
2578 for (Index = 0; FvInfoPtr->FvFiles[Index][0] != 0; Index++) {
2579 //
2580 // Open FFS file
2581 //
2582 fpin = NULL;
2583 fpin = fopen (FvInfoPtr->FvFiles[Index], "rb");
2584 if (fpin == NULL) {
2585 Error (NULL, 0, 0001, "Error opening file", FvInfoPtr->FvFiles[Index]);
2586 return EFI_ABORTED;
2587 }
2588 //
2589 // Get the file size
2590 //
2591 FfsFileSize = _filelength (fileno (fpin));
2592 //
2593 // Read Ffs File header
2594 //
2595 fread (&FfsHeader, sizeof (UINT8), sizeof (EFI_FFS_FILE_HEADER), fpin);
2596 //
2597 // close file
2598 //
2599 fclose (fpin);
2600
2601 if (FvInfoPtr->IsPiFvImage) {
2602 //
2603 // Check whether this ffs file is vtf file
2604 //
2605 if (IsVtfFile (&FfsHeader)) {
2606 if (VtfFileFlag) {
2607 //
2608 // One Fv image can't have two vtf files.
2609 //
2610 return EFI_ABORTED;
2611 }
2612 VtfFileFlag = TRUE;
2613 VtfFileSize = FfsFileSize;
2614 continue;
2615 }
2616
2617 //
2618 // Get the alignment of FFS file
2619 //
2620 ReadFfsAlignment (&FfsHeader, &FfsAlignment);
2621 FfsAlignment = 1 << FfsAlignment;
2622 //
2623 // Add Pad file
2624 //
2625 if (((CurrentOffset + sizeof (EFI_FFS_FILE_HEADER)) % FfsAlignment) != 0) {
2626 CurrentOffset = (CurrentOffset + sizeof (EFI_FFS_FILE_HEADER) * 2 + FfsAlignment - 1) & ~(FfsAlignment - 1);
2627 CurrentOffset -= sizeof (EFI_FFS_FILE_HEADER);
2628 }
2629 }
2630
2631 //
2632 // Add ffs file size
2633 //
2634 if (FvInfoPtr->SizeofFvFiles[Index] > FfsFileSize) {
2635 CurrentOffset += FvInfoPtr->SizeofFvFiles[Index];
2636 } else {
2637 CurrentOffset += FfsFileSize;
2638 }
2639
2640 //
2641 // Make next ffs file start at QWord Boundry
2642 //
2643 if (FvInfoPtr->IsPiFvImage) {
2644 CurrentOffset = (CurrentOffset + EFI_FFS_FILE_HEADER_ALIGNMENT - 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT - 1);
2645 }
2646 }
2647 CurrentOffset += VtfFileSize;
2648 DebugMsg (NULL, 0, 9, "FvImage size", "The caculated fv image size is 0x%x and the current set fv image size is 0x%x", (unsigned) CurrentOffset, (unsigned) FvInfoPtr->Size);
2649
2650 if (FvInfoPtr->Size == 0) {
2651 //
2652 // Update FvInfo data
2653 //
2654 FvInfoPtr->FvBlocks[0].NumBlocks = CurrentOffset / FvInfoPtr->FvBlocks[0].Length + ((CurrentOffset % FvInfoPtr->FvBlocks[0].Length)?1:0);
2655 FvInfoPtr->Size = FvInfoPtr->FvBlocks[0].NumBlocks * FvInfoPtr->FvBlocks[0].Length;
2656 FvInfoPtr->FvBlocks[1].NumBlocks = 0;
2657 FvInfoPtr->FvBlocks[1].Length = 0;
2658 } else if (FvInfoPtr->Size < CurrentOffset) {
2659 //
2660 // Not invalid
2661 //
2662 Error (NULL, 0, 3000, "Invalid", "the required fv image size 0x%x exceeds the set fv image size 0x%x", (unsigned) CurrentOffset, (unsigned) FvInfoPtr->Size);
2663 return EFI_INVALID_PARAMETER;
2664 }
2665
2666 //
2667 // Set Fv Size Information
2668 //
2669 mFvTotalSize = FvInfoPtr->Size;
2670 mFvTakenSize = CurrentOffset;
2671
2672 return EFI_SUCCESS;
2673 }
2674
2675 EFI_STATUS
2676 FfsRebaseImageRead (
2677 IN VOID *FileHandle,
2678 IN UINTN FileOffset,
2679 IN OUT UINT32 *ReadSize,
2680 OUT VOID *Buffer
2681 )
2682 /*++
2683
2684 Routine Description:
2685
2686 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
2687
2688 Arguments:
2689
2690 FileHandle - The handle to the PE/COFF file
2691
2692 FileOffset - The offset, in bytes, into the file to read
2693
2694 ReadSize - The number of bytes to read from the file starting at FileOffset
2695
2696 Buffer - A pointer to the buffer to read the data into.
2697
2698 Returns:
2699
2700 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
2701
2702 --*/
2703 {
2704 CHAR8 *Destination8;
2705 CHAR8 *Source8;
2706 UINT32 Length;
2707
2708 Destination8 = Buffer;
2709 Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);
2710 Length = *ReadSize;
2711 while (Length--) {
2712 *(Destination8++) = *(Source8++);
2713 }
2714
2715 return EFI_SUCCESS;
2716 }
2717
2718 EFI_STATUS
2719 GetChildFvFromFfs (
2720 IN FV_INFO *FvInfo,
2721 IN EFI_FFS_FILE_HEADER *FfsFile,
2722 IN UINTN XipOffset
2723 )
2724 /*++
2725
2726 Routine Description:
2727
2728 This function gets all child FvImages in the input FfsFile, and records
2729 their base address to the parent image.
2730
2731 Arguments:
2732 FvInfo A pointer to FV_INFO struture.
2733 FfsFile A pointer to Ffs file image that may contain FvImage.
2734 XipOffset The offset address to the parent FvImage base.
2735
2736 Returns:
2737
2738 EFI_SUCCESS Base address of child Fv image is recorded.
2739 --*/
2740 {
2741 EFI_STATUS Status;
2742 UINTN Index;
2743 EFI_FILE_SECTION_POINTER SubFvSection;
2744 EFI_FIRMWARE_VOLUME_HEADER *SubFvImageHeader;
2745 EFI_PHYSICAL_ADDRESS SubFvBaseAddress;
2746
2747 for (Index = 1;; Index++) {
2748 //
2749 // Find FV section
2750 //
2751 Status = GetSectionByType (FfsFile, EFI_SECTION_FIRMWARE_VOLUME_IMAGE, Index, &SubFvSection);
2752 if (EFI_ERROR (Status)) {
2753 break;
2754 }
2755 SubFvImageHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINT8 *) SubFvSection.FVImageSection + sizeof (EFI_FIRMWARE_VOLUME_IMAGE_SECTION));
2756 //
2757 // Rebase on Flash
2758 //
2759 SubFvBaseAddress = FvInfo->BaseAddress + (UINTN) SubFvImageHeader - (UINTN) FfsFile + XipOffset;
2760 mFvBaseAddress[mFvBaseAddressNumber ++ ] = SubFvBaseAddress;
2761 }
2762
2763 return EFI_SUCCESS;
2764 }
2765
2766 EFI_STATUS
2767 FfsRebase (
2768 IN OUT FV_INFO *FvInfo,
2769 IN CHAR8 *FileName,
2770 IN OUT EFI_FFS_FILE_HEADER *FfsFile,
2771 IN UINTN XipOffset,
2772 IN FILE *FvMapFile
2773 )
2774 /*++
2775
2776 Routine Description:
2777
2778 This function determines if a file is XIP and should be rebased. It will
2779 rebase any PE32 sections found in the file using the base address.
2780
2781 Arguments:
2782
2783 FvInfo A pointer to FV_INFO struture.
2784 FileName Ffs File PathName
2785 FfsFile A pointer to Ffs file image.
2786 XipOffset The offset address to use for rebasing the XIP file image.
2787 FvMapFile FvMapFile to record the function address in one Fvimage
2788
2789 Returns:
2790
2791 EFI_SUCCESS The image was properly rebased.
2792 EFI_INVALID_PARAMETER An input parameter is invalid.
2793 EFI_ABORTED An error occurred while rebasing the input file image.
2794 EFI_OUT_OF_RESOURCES Could not allocate a required resource.
2795 EFI_NOT_FOUND No compressed sections could be found.
2796
2797 --*/
2798 {
2799 EFI_STATUS Status;
2800 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
2801 PE_COFF_LOADER_IMAGE_CONTEXT OrigImageContext;
2802 EFI_PHYSICAL_ADDRESS XipBase;
2803 EFI_PHYSICAL_ADDRESS NewPe32BaseAddress;
2804 UINTN Index;
2805 EFI_FILE_SECTION_POINTER CurrentPe32Section;
2806 EFI_FFS_FILE_STATE SavedState;
2807 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
2808 EFI_TE_IMAGE_HEADER *TEImageHeader;
2809 UINT8 *MemoryImagePointer;
2810 EFI_IMAGE_SECTION_HEADER *SectionHeader;
2811 CHAR8 PeFileName [_MAX_PATH];
2812 CHAR8 *Cptr;
2813 FILE *PeFile;
2814 UINT8 *PeFileBuffer;
2815 UINT32 PeFileSize;
2816 CHAR8 *PdbPointer;
2817
2818 Index = 0;
2819 MemoryImagePointer = NULL;
2820 TEImageHeader = NULL;
2821 ImgHdr = NULL;
2822 SectionHeader = NULL;
2823 Cptr = NULL;
2824 PeFile = NULL;
2825 PeFileBuffer = NULL;
2826
2827 //
2828 // Don't need to relocate image when BaseAddress is zero and no ForceRebase Flag specified.
2829 //
2830 if ((FvInfo->BaseAddress == 0) && (FvInfo->ForceRebase == -1)) {
2831 return EFI_SUCCESS;
2832 }
2833
2834 //
2835 // If ForceRebase Flag specified to FALSE, will always not take rebase action.
2836 //
2837 if (FvInfo->ForceRebase == 0) {
2838 return EFI_SUCCESS;
2839 }
2840
2841
2842 XipBase = FvInfo->BaseAddress + XipOffset;
2843
2844 //
2845 // We only process files potentially containing PE32 sections.
2846 //
2847 switch (FfsFile->Type) {
2848 case EFI_FV_FILETYPE_SECURITY_CORE:
2849 case EFI_FV_FILETYPE_PEI_CORE:
2850 case EFI_FV_FILETYPE_PEIM:
2851 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER:
2852 case EFI_FV_FILETYPE_DRIVER:
2853 case EFI_FV_FILETYPE_DXE_CORE:
2854 break;
2855 case EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE:
2856 //
2857 // Rebase the inside FvImage.
2858 //
2859 GetChildFvFromFfs (FvInfo, FfsFile, XipOffset);
2860
2861 //
2862 // Search PE/TE section in FV sectin.
2863 //
2864 break;
2865 default:
2866 return EFI_SUCCESS;
2867 }
2868 //
2869 // Rebase each PE32 section
2870 //
2871 Status = EFI_SUCCESS;
2872 for (Index = 1;; Index++) {
2873 //
2874 // Init Value
2875 //
2876 NewPe32BaseAddress = 0;
2877
2878 //
2879 // Find Pe Image
2880 //
2881 Status = GetSectionByType (FfsFile, EFI_SECTION_PE32, Index, &CurrentPe32Section);
2882 if (EFI_ERROR (Status)) {
2883 break;
2884 }
2885
2886 //
2887 // Initialize context
2888 //
2889 memset (&ImageContext, 0, sizeof (ImageContext));
2890 ImageContext.Handle = (VOID *) ((UINTN) CurrentPe32Section.Pe32Section + sizeof (EFI_PE32_SECTION));
2891 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) FfsRebaseImageRead;
2892 Status = PeCoffLoaderGetImageInfo (&ImageContext);
2893 if (EFI_ERROR (Status)) {
2894 Error (NULL, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName, (int) Status);
2895 return Status;
2896 }
2897
2898 if (ImageContext.Machine == EFI_IMAGE_MACHINE_ARMT) {
2899 mArm = TRUE;
2900 }
2901
2902 //
2903 // Keep Image Context for PE image in FV
2904 //
2905 memcpy (&OrigImageContext, &ImageContext, sizeof (ImageContext));
2906
2907 //
2908 // Get File PdbPointer
2909 //
2910 PdbPointer = PeCoffLoaderGetPdbPointer (ImageContext.Handle);
2911
2912 //
2913 // Get PeHeader pointer
2914 //
2915 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((UINTN) CurrentPe32Section.Pe32Section + sizeof (EFI_PE32_SECTION) + ImageContext.PeCoffHeaderOffset);
2916
2917 //
2918 // Calculate the PE32 base address, based on file type
2919 //
2920 switch (FfsFile->Type) {
2921 case EFI_FV_FILETYPE_SECURITY_CORE:
2922 case EFI_FV_FILETYPE_PEI_CORE:
2923 case EFI_FV_FILETYPE_PEIM:
2924 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER:
2925 //
2926 // Check if section-alignment and file-alignment match or not
2927 //
2928 if ((ImgHdr->Pe32.OptionalHeader.SectionAlignment != ImgHdr->Pe32.OptionalHeader.FileAlignment)) {
2929 //
2930 // Xip module has the same section alignment and file alignment.
2931 //
2932 Error (NULL, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName);
2933 return EFI_ABORTED;
2934 }
2935 //
2936 // PeImage has no reloc section. It will try to get reloc data from the original EFI image.
2937 //
2938 if (ImageContext.RelocationsStripped) {
2939 //
2940 // Construct the original efi file Name
2941 //
2942 strcpy (PeFileName, FileName);
2943 Cptr = PeFileName + strlen (PeFileName);
2944 while (*Cptr != '.') {
2945 Cptr --;
2946 }
2947 if (*Cptr != '.') {
2948 Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
2949 return EFI_ABORTED;
2950 } else {
2951 *(Cptr + 1) = 'e';
2952 *(Cptr + 2) = 'f';
2953 *(Cptr + 3) = 'i';
2954 *(Cptr + 4) = '\0';
2955 }
2956 PeFile = fopen (PeFileName, "rb");
2957 if (PeFile == NULL) {
2958 Warning (NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);
2959 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
2960 //return EFI_ABORTED;
2961 break;
2962 }
2963 //
2964 // Get the file size
2965 //
2966 PeFileSize = _filelength (fileno (PeFile));
2967 PeFileBuffer = (UINT8 *) malloc (PeFileSize);
2968 if (PeFileBuffer == NULL) {
2969 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);
2970 return EFI_OUT_OF_RESOURCES;
2971 }
2972 //
2973 // Read Pe File
2974 //
2975 fread (PeFileBuffer, sizeof (UINT8), PeFileSize, PeFile);
2976 //
2977 // close file
2978 //
2979 fclose (PeFile);
2980 //
2981 // Handle pointer to the original efi image.
2982 //
2983 ImageContext.Handle = PeFileBuffer;
2984 Status = PeCoffLoaderGetImageInfo (&ImageContext);
2985 if (EFI_ERROR (Status)) {
2986 Error (NULL, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName, (int) Status);
2987 return Status;
2988 }
2989 ImageContext.RelocationsStripped = FALSE;
2990 }
2991
2992 NewPe32BaseAddress = XipBase + (UINTN) CurrentPe32Section.Pe32Section + sizeof (EFI_PE32_SECTION) - (UINTN)FfsFile;
2993 break;
2994
2995 case EFI_FV_FILETYPE_DRIVER:
2996 case EFI_FV_FILETYPE_DXE_CORE:
2997 //
2998 // Check if section-alignment and file-alignment match or not
2999 //
3000 if ((ImgHdr->Pe32.OptionalHeader.SectionAlignment != ImgHdr->Pe32.OptionalHeader.FileAlignment)) {
3001 //
3002 // Xip module has the same section alignment and file alignment.
3003 //
3004 Error (NULL, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName);
3005 return EFI_ABORTED;
3006 }
3007 NewPe32BaseAddress = XipBase + (UINTN) CurrentPe32Section.Pe32Section + sizeof (EFI_PE32_SECTION) - (UINTN)FfsFile;
3008 break;
3009
3010 default:
3011 //
3012 // Not supported file type
3013 //
3014 return EFI_SUCCESS;
3015 }
3016
3017 //
3018 // Relocation doesn't exist
3019 //
3020 if (ImageContext.RelocationsStripped) {
3021 Warning (NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);
3022 continue;
3023 }
3024
3025 //
3026 // Relocation exist and rebase
3027 //
3028 //
3029 // Load and Relocate Image Data
3030 //
3031 MemoryImagePointer = (UINT8 *) malloc ((UINTN) ImageContext.ImageSize + ImageContext.SectionAlignment);
3032 if (MemoryImagePointer == NULL) {
3033 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);
3034 return EFI_OUT_OF_RESOURCES;
3035 }
3036 memset ((VOID *) MemoryImagePointer, 0, (UINTN) ImageContext.ImageSize + ImageContext.SectionAlignment);
3037 ImageContext.ImageAddress = ((UINTN) MemoryImagePointer + ImageContext.SectionAlignment - 1) & (~((UINTN) ImageContext.SectionAlignment - 1));
3038
3039 Status = PeCoffLoaderLoadImage (&ImageContext);
3040 if (EFI_ERROR (Status)) {
3041 Error (NULL, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName);
3042 free ((VOID *) MemoryImagePointer);
3043 return Status;
3044 }
3045
3046 ImageContext.DestinationAddress = NewPe32BaseAddress;
3047 Status = PeCoffLoaderRelocateImage (&ImageContext);
3048 if (EFI_ERROR (Status)) {
3049 Error (NULL, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of %s", FileName);
3050 free ((VOID *) MemoryImagePointer);
3051 return Status;
3052 }
3053
3054 //
3055 // Copy Relocated data to raw image file.
3056 //
3057 SectionHeader = (EFI_IMAGE_SECTION_HEADER *) (
3058 (UINTN) ImgHdr +
3059 sizeof (UINT32) +
3060 sizeof (EFI_IMAGE_FILE_HEADER) +
3061 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader
3062 );
3063
3064 for (Index = 0; Index < ImgHdr->Pe32.FileHeader.NumberOfSections; Index ++, SectionHeader ++) {
3065 CopyMem (
3066 (UINT8 *) CurrentPe32Section.Pe32Section + sizeof (EFI_COMMON_SECTION_HEADER) + SectionHeader->PointerToRawData,
3067 (VOID*) (UINTN) (ImageContext.ImageAddress + SectionHeader->VirtualAddress),
3068 SectionHeader->SizeOfRawData
3069 );
3070 }
3071
3072 free ((VOID *) MemoryImagePointer);
3073 MemoryImagePointer = NULL;
3074 if (PeFileBuffer != NULL) {
3075 free (PeFileBuffer);
3076 PeFileBuffer = NULL;
3077 }
3078
3079 //
3080 // Update Image Base Address
3081 //
3082 if (ImgHdr->Pe32.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
3083 ImgHdr->Pe32.OptionalHeader.ImageBase = (UINT32) NewPe32BaseAddress;
3084 } else if (ImgHdr->Pe32Plus.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
3085 ImgHdr->Pe32Plus.OptionalHeader.ImageBase = NewPe32BaseAddress;
3086 } else {
3087 Error (NULL, 0, 3000, "Invalid", "unknown PE magic signature %X in PE32 image %s",
3088 ImgHdr->Pe32.OptionalHeader.Magic,
3089 FileName
3090 );
3091 return EFI_ABORTED;
3092 }
3093
3094 //
3095 // Now update file checksum
3096 //
3097 if (FfsFile->Attributes & FFS_ATTRIB_CHECKSUM) {
3098 SavedState = FfsFile->State;
3099 FfsFile->IntegrityCheck.Checksum.File = 0;
3100 FfsFile->State = 0;
3101 FfsFile->IntegrityCheck.Checksum.File = CalculateChecksum8 (
3102 (UINT8 *) (FfsFile + 1),
3103 GetLength (FfsFile->Size) - sizeof (EFI_FFS_FILE_HEADER)
3104 );
3105 FfsFile->State = SavedState;
3106 }
3107
3108 //
3109 // Get this module function address from ModulePeMapFile and add them into FvMap file
3110 //
3111
3112 //
3113 // Default use FileName as map file path
3114 //
3115 if (PdbPointer == NULL) {
3116 PdbPointer = FileName;
3117 }
3118
3119 WriteMapFile (FvMapFile, PdbPointer, FfsFile, NewPe32BaseAddress, &OrigImageContext);
3120 }
3121
3122 if (FfsFile->Type != EFI_FV_FILETYPE_SECURITY_CORE &&
3123 FfsFile->Type != EFI_FV_FILETYPE_PEI_CORE &&
3124 FfsFile->Type != EFI_FV_FILETYPE_PEIM &&
3125 FfsFile->Type != EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER &&
3126 FfsFile->Type != EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
3127 ) {
3128 //
3129 // Only Peim code may have a TE section
3130 //
3131 return EFI_SUCCESS;
3132 }
3133
3134 //
3135 // Now process TE sections
3136 //
3137 for (Index = 1;; Index++) {
3138 NewPe32BaseAddress = 0;
3139
3140 //
3141 // Find Te Image
3142 //
3143 Status = GetSectionByType (FfsFile, EFI_SECTION_TE, Index, &CurrentPe32Section);
3144 if (EFI_ERROR (Status)) {
3145 break;
3146 }
3147
3148 //
3149 // Calculate the TE base address, the FFS file base plus the offset of the TE section less the size stripped off
3150 // by GenTEImage
3151 //
3152 TEImageHeader = (EFI_TE_IMAGE_HEADER *) ((UINT8 *) CurrentPe32Section.Pe32Section + sizeof (EFI_COMMON_SECTION_HEADER));
3153
3154 //
3155 // Initialize context, load image info.
3156 //
3157 memset (&ImageContext, 0, sizeof (ImageContext));
3158 ImageContext.Handle = (VOID *) TEImageHeader;
3159 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) FfsRebaseImageRead;
3160 Status = PeCoffLoaderGetImageInfo (&ImageContext);
3161 if (EFI_ERROR (Status)) {
3162 Error (NULL, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName, (int) Status);
3163 return Status;
3164 }
3165
3166 if (ImageContext.Machine == EFI_IMAGE_MACHINE_ARMT) {
3167 mArm = TRUE;
3168 }
3169
3170 //
3171 // Keep Image Context for TE image in FV
3172 //
3173 memcpy (&OrigImageContext, &ImageContext, sizeof (ImageContext));
3174
3175 //
3176 // Get File PdbPointer
3177 //
3178 PdbPointer = PeCoffLoaderGetPdbPointer (ImageContext.Handle);
3179
3180 //
3181 // Set new rebased address.
3182 //
3183 NewPe32BaseAddress = XipBase + (UINTN) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) \
3184 - TEImageHeader->StrippedSize - (UINTN) FfsFile;
3185
3186 //
3187 // if reloc is stripped, try to get the original efi image to get reloc info.
3188 //
3189 if (ImageContext.RelocationsStripped) {
3190 //
3191 // Construct the original efi file name
3192 //
3193 strcpy (PeFileName, FileName);
3194 Cptr = PeFileName + strlen (PeFileName);
3195 while (*Cptr != '.') {
3196 Cptr --;
3197 }
3198
3199 if (*Cptr != '.') {
3200 Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3201 return EFI_ABORTED;
3202 } else {
3203 *(Cptr + 1) = 'e';
3204 *(Cptr + 2) = 'f';
3205 *(Cptr + 3) = 'i';
3206 *(Cptr + 4) = '\0';
3207 }
3208
3209 PeFile = fopen (PeFileName, "rb");
3210 if (PeFile == NULL) {
3211 Warning (NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);
3212 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3213 //return EFI_ABORTED;
3214 } else {
3215 //
3216 // Get the file size
3217 //
3218 PeFileSize = _filelength (fileno (PeFile));
3219 PeFileBuffer = (UINT8 *) malloc (PeFileSize);
3220 if (PeFileBuffer == NULL) {
3221 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);
3222 return EFI_OUT_OF_RESOURCES;
3223 }
3224 //
3225 // Read Pe File
3226 //
3227 fread (PeFileBuffer, sizeof (UINT8), PeFileSize, PeFile);
3228 //
3229 // close file
3230 //
3231 fclose (PeFile);
3232 //
3233 // Append reloc section into TeImage
3234 //
3235 ImageContext.Handle = PeFileBuffer;
3236 Status = PeCoffLoaderGetImageInfo (&ImageContext);
3237 if (EFI_ERROR (Status)) {
3238 Error (NULL, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName, (int) Status);
3239 return Status;
3240 }
3241 ImageContext.RelocationsStripped = FALSE;
3242 }
3243 }
3244 //
3245 // Relocation doesn't exist
3246 //
3247 if (ImageContext.RelocationsStripped) {
3248 Warning (NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);
3249 continue;
3250 }
3251
3252 //
3253 // Relocation exist and rebase
3254 //
3255 //
3256 // Load and Relocate Image Data
3257 //
3258 MemoryImagePointer = (UINT8 *) malloc ((UINTN) ImageContext.ImageSize + ImageContext.SectionAlignment);
3259 if (MemoryImagePointer == NULL) {
3260 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);
3261 return EFI_OUT_OF_RESOURCES;
3262 }
3263 memset ((VOID *) MemoryImagePointer, 0, (UINTN) ImageContext.ImageSize + ImageContext.SectionAlignment);
3264 ImageContext.ImageAddress = ((UINTN) MemoryImagePointer + ImageContext.SectionAlignment - 1) & (~((UINTN) ImageContext.SectionAlignment - 1));
3265
3266 Status = PeCoffLoaderLoadImage (&ImageContext);
3267 if (EFI_ERROR (Status)) {
3268 Error (NULL, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName);
3269 free ((VOID *) MemoryImagePointer);
3270 return Status;
3271 }
3272 //
3273 // Reloacate TeImage
3274 //
3275 ImageContext.DestinationAddress = NewPe32BaseAddress;
3276 Status = PeCoffLoaderRelocateImage (&ImageContext);
3277 if (EFI_ERROR (Status)) {
3278 Error (NULL, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of TE image %s", FileName);
3279 free ((VOID *) MemoryImagePointer);
3280 return Status;
3281 }
3282
3283 //
3284 // Copy the relocated image into raw image file.
3285 //
3286 SectionHeader = (EFI_IMAGE_SECTION_HEADER *) (TEImageHeader + 1);
3287 for (Index = 0; Index < TEImageHeader->NumberOfSections; Index ++, SectionHeader ++) {
3288 if (!ImageContext.IsTeImage) {
3289 CopyMem (
3290 (UINT8 *) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize + SectionHeader->PointerToRawData,
3291 (VOID*) (UINTN) (ImageContext.ImageAddress + SectionHeader->VirtualAddress),
3292 SectionHeader->SizeOfRawData
3293 );
3294 } else {
3295 CopyMem (
3296 (UINT8 *) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize + SectionHeader->PointerToRawData,
3297 (VOID*) (UINTN) (ImageContext.ImageAddress + sizeof (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize + SectionHeader->VirtualAddress),
3298 SectionHeader->SizeOfRawData
3299 );
3300 }
3301 }
3302
3303 //
3304 // Free the allocated memory resource
3305 //
3306 free ((VOID *) MemoryImagePointer);
3307 MemoryImagePointer = NULL;
3308 if (PeFileBuffer != NULL) {
3309 free (PeFileBuffer);
3310 PeFileBuffer = NULL;
3311 }
3312
3313 //
3314 // Update Image Base Address
3315 //
3316 TEImageHeader->ImageBase = NewPe32BaseAddress;
3317
3318 //
3319 // Now update file checksum
3320 //
3321 if (FfsFile->Attributes & FFS_ATTRIB_CHECKSUM) {
3322 SavedState = FfsFile->State;
3323 FfsFile->IntegrityCheck.Checksum.File = 0;
3324 FfsFile->State = 0;
3325 FfsFile->IntegrityCheck.Checksum.File = CalculateChecksum8 (
3326 (UINT8 *)(FfsFile + 1),
3327 GetLength (FfsFile->Size) - sizeof (EFI_FFS_FILE_HEADER)
3328 );
3329 FfsFile->State = SavedState;
3330 }
3331 //
3332 // Get this module function address from ModulePeMapFile and add them into FvMap file
3333 //
3334
3335 //
3336 // Default use FileName as map file path
3337 //
3338 if (PdbPointer == NULL) {
3339 PdbPointer = FileName;
3340 }
3341
3342 WriteMapFile (
3343 FvMapFile,
3344 PdbPointer,
3345 FfsFile,
3346 NewPe32BaseAddress,
3347 &OrigImageContext
3348 );
3349 }
3350
3351 return EFI_SUCCESS;
3352 }
3353
3354 EFI_STATUS
3355 FindApResetVectorPosition (
3356 IN MEMORY_FILE *FvImage,
3357 OUT UINT8 **Pointer
3358 )
3359 /*++
3360
3361 Routine Description:
3362
3363 Find the position in this FvImage to place Ap reset vector.
3364
3365 Arguments:
3366
3367 FvImage Memory file for the FV memory image.
3368 Pointer Pointer to pointer to position.
3369
3370 Returns:
3371
3372 EFI_NOT_FOUND - No satisfied position is found.
3373 EFI_SUCCESS - The suitable position is return.
3374
3375 --*/
3376 {
3377 EFI_FFS_FILE_HEADER *PadFile;
3378 UINT32 Index;
3379 EFI_STATUS Status;
3380 UINT8 *FixPoint;
3381 UINT32 FileLength;
3382
3383 for (Index = 1; ;Index ++) {
3384 //
3385 // Find Pad File to add ApResetVector info
3386 //
3387 Status = GetFileByType (EFI_FV_FILETYPE_FFS_PAD, Index, &PadFile);
3388 if (EFI_ERROR (Status) || (PadFile == NULL)) {
3389 //
3390 // No Pad file to be found.
3391 //
3392 break;
3393 }
3394 //
3395 // Get Pad file size.
3396 //
3397 FileLength = (*(UINT32 *)(PadFile->Size)) & 0x00FFFFFF;
3398 FileLength = (FileLength + EFI_FFS_FILE_HEADER_ALIGNMENT - 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT - 1);
3399 //
3400 // FixPoint must be align on 0x1000 relative to FvImage Header
3401 //
3402 FixPoint = (UINT8*) PadFile + sizeof (EFI_FFS_FILE_HEADER);
3403 FixPoint = FixPoint + 0x1000 - (((UINTN) FixPoint - (UINTN) FvImage->FileImage) & 0xFFF);
3404 //
3405 // FixPoint be larger at the last place of one fv image.
3406 //
3407 while (((UINTN) FixPoint + SIZEOF_STARTUP_DATA_ARRAY - (UINTN) PadFile) <= FileLength) {
3408 FixPoint += 0x1000;
3409 }
3410 FixPoint -= 0x1000;
3411
3412 if ((UINTN) FixPoint < ((UINTN) PadFile + sizeof (EFI_FFS_FILE_HEADER))) {
3413 //
3414 // No alignment FixPoint in this Pad File.
3415 //
3416 continue;
3417 }
3418
3419 if ((UINTN) FvImage->Eof - (UINTN)FixPoint <= 0x20000) {
3420 //
3421 // Find the position to place ApResetVector
3422 //
3423 *Pointer = FixPoint;
3424 return EFI_SUCCESS;
3425 }
3426 }
3427
3428 return EFI_NOT_FOUND;
3429 }
3430
3431 EFI_STATUS
3432 ParseCapInf (
3433 IN MEMORY_FILE *InfFile,
3434 OUT CAP_INFO *CapInfo
3435 )
3436 /*++
3437
3438 Routine Description:
3439
3440 This function parses a Cap.INF file and copies info into a CAP_INFO structure.
3441
3442 Arguments:
3443
3444 InfFile Memory file image.
3445 CapInfo Information read from INF file.
3446
3447 Returns:
3448
3449 EFI_SUCCESS INF file information successfully retrieved.
3450 EFI_ABORTED INF file has an invalid format.
3451 EFI_NOT_FOUND A required string was not found in the INF file.
3452 --*/
3453 {
3454 CHAR8 Value[_MAX_PATH];
3455 UINT64 Value64;
3456 UINTN Index, Number;
3457 EFI_STATUS Status;
3458
3459 //
3460 // Initialize Cap info
3461 //
3462 // memset (CapInfo, 0, sizeof (CAP_INFO));
3463 //
3464
3465 //
3466 // Read the Capsule Guid
3467 //
3468 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_CAPSULE_GUID_STRING, 0, Value);
3469 if (Status == EFI_SUCCESS) {
3470 //
3471 // Get the Capsule Guid
3472 //
3473 Status = StringToGuid (Value, &CapInfo->CapGuid);
3474 if (EFI_ERROR (Status)) {
3475 Error (NULL, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_GUID_STRING, Value);
3476 return EFI_ABORTED;
3477 }
3478 DebugMsg (NULL, 0, 9, "Capsule Guid", "%s = %s", EFI_CAPSULE_GUID_STRING, Value);
3479 }
3480
3481 //
3482 // Read the Capsule Header Size
3483 //
3484 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_CAPSULE_HEADER_SIZE_STRING, 0, Value);
3485 if (Status == EFI_SUCCESS) {
3486 Status = AsciiStringToUint64 (Value, FALSE, &Value64);
3487 if (EFI_ERROR (Status)) {
3488 Error (NULL, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING, Value);
3489 return EFI_ABORTED;
3490 }
3491 CapInfo->HeaderSize = (UINT32) Value64;
3492 DebugMsg (NULL, 0, 9, "Capsule Header size", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING, Value);
3493 }
3494
3495 //
3496 // Read the Capsule Flag
3497 //
3498 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_CAPSULE_FLAGS_STRING, 0, Value);
3499 if (Status == EFI_SUCCESS) {
3500 if (strstr (Value, "PopulateSystemTable") != NULL) {
3501 CapInfo->Flags |= CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE;
3502 if (strstr (Value, "InitiateReset") != NULL) {
3503 CapInfo->Flags |= CAPSULE_FLAGS_INITIATE_RESET;
3504 }
3505 } else if (strstr (Value, "PersistAcrossReset") != NULL) {
3506 CapInfo->Flags |= CAPSULE_FLAGS_PERSIST_ACROSS_RESET;
3507 if (strstr (Value, "InitiateReset") != NULL) {
3508 CapInfo->Flags |= CAPSULE_FLAGS_INITIATE_RESET;
3509 }
3510 } else {
3511 Error (NULL, 0, 2000, "Invalid parameter", "invalid Flag setting for %s.", EFI_CAPSULE_FLAGS_STRING);
3512 return EFI_ABORTED;
3513 }
3514 DebugMsg (NULL, 0, 9, "Capsule Flag", Value);
3515 }
3516
3517 //
3518 // Read Capsule File name
3519 //
3520 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_FILE_NAME_STRING, 0, Value);
3521 if (Status == EFI_SUCCESS) {
3522 //
3523 // Get output file name
3524 //
3525 strcpy (CapInfo->CapName, Value);
3526 }
3527
3528 //
3529 // Read the Capsule FileImage
3530 //
3531 Number = 0;
3532 for (Index = 0; Index < MAX_NUMBER_OF_FILES_IN_CAP; Index++) {
3533 if (CapInfo->CapFiles[Index][0] != '\0') {
3534 continue;
3535 }
3536 //
3537 // Read the capsule file name
3538 //
3539 Status = FindToken (InfFile, FILES_SECTION_STRING, EFI_FILE_NAME_STRING, Number++, Value);
3540
3541 if (Status == EFI_SUCCESS) {
3542 //
3543 // Add the file
3544 //
3545 strcpy (CapInfo->CapFiles[Index], Value);
3546 DebugMsg (NULL, 0, 9, "Capsule component file", "the %uth file name is %s", (unsigned) Index, CapInfo->CapFiles[Index]);
3547 } else {
3548 break;
3549 }
3550 }
3551
3552 if (Index == 0) {
3553 Warning (NULL, 0, 0, "Capsule components are not specified.", NULL);
3554 }
3555
3556 return EFI_SUCCESS;
3557 }
3558
3559 EFI_STATUS
3560 GenerateCapImage (
3561 IN CHAR8 *InfFileImage,
3562 IN UINTN InfFileSize,
3563 IN CHAR8 *CapFileName
3564 )
3565 /*++
3566
3567 Routine Description:
3568
3569 This is the main function which will be called from application to create UEFI Capsule image.
3570
3571 Arguments:
3572
3573 InfFileImage Buffer containing the INF file contents.
3574 InfFileSize Size of the contents of the InfFileImage buffer.
3575 CapFileName Requested name for the Cap file.
3576
3577 Returns:
3578
3579 EFI_SUCCESS Function completed successfully.
3580 EFI_OUT_OF_RESOURCES Could not allocate required resources.
3581 EFI_ABORTED Error encountered.
3582 EFI_INVALID_PARAMETER A required parameter was NULL.
3583
3584 --*/
3585 {
3586 UINT32 CapSize;
3587 UINT8 *CapBuffer;
3588 EFI_CAPSULE_HEADER *CapsuleHeader;
3589 MEMORY_FILE InfMemoryFile;
3590 UINT32 FileSize;
3591 UINT32 Index;
3592 FILE *fpin, *fpout;
3593 EFI_STATUS Status;
3594
3595 if (InfFileImage != NULL) {
3596 //
3597 // Initialize file structures
3598 //
3599 InfMemoryFile.FileImage = InfFileImage;
3600 InfMemoryFile.CurrentFilePointer = InfFileImage;
3601 InfMemoryFile.Eof = InfFileImage + InfFileSize;
3602
3603 //
3604 // Parse the Cap inf file for header information
3605 //
3606 Status = ParseCapInf (&InfMemoryFile, &mCapDataInfo);
3607 if (Status != EFI_SUCCESS) {
3608 return Status;
3609 }
3610 }
3611
3612 if (mCapDataInfo.HeaderSize == 0) {
3613 //
3614 // make header size align 16 bytes.
3615 //
3616 mCapDataInfo.HeaderSize = sizeof (EFI_CAPSULE_HEADER);
3617 mCapDataInfo.HeaderSize = (mCapDataInfo.HeaderSize + 0xF) & ~0xF;
3618 }
3619
3620 if (mCapDataInfo.HeaderSize < sizeof (EFI_CAPSULE_HEADER)) {
3621 Error (NULL, 0, 2000, "Invalid parameter", "The specified HeaderSize cannot be less than the size of EFI_CAPSULE_HEADER.");
3622 return EFI_INVALID_PARAMETER;
3623 }
3624
3625 if (CapFileName == NULL && mCapDataInfo.CapName[0] != '\0') {
3626 CapFileName = mCapDataInfo.CapName;
3627 }
3628
3629 if (CapFileName == NULL) {
3630 Error (NULL, 0, 2001, "Missing required argument", "Output Capsule file name");
3631 return EFI_INVALID_PARAMETER;
3632 }
3633
3634 //
3635 // Set Default Capsule Guid value
3636 //
3637 if (CompareGuid (&mCapDataInfo.CapGuid, &mZeroGuid) == 0) {
3638 memcpy (&mCapDataInfo.CapGuid, &mDefaultCapsuleGuid, sizeof (EFI_GUID));
3639 }
3640 //
3641 // Calculate the size of capsule image.
3642 //
3643 Index = 0;
3644 FileSize = 0;
3645 CapSize = mCapDataInfo.HeaderSize;
3646 while (mCapDataInfo.CapFiles [Index][0] != '\0') {
3647 fpin = fopen (mCapDataInfo.CapFiles[Index], "rb");
3648 if (fpin == NULL) {
3649 Error (NULL, 0, 0001, "Error opening file", mCapDataInfo.CapFiles[Index]);
3650 return EFI_ABORTED;
3651 }
3652 FileSize = _filelength (fileno (fpin));
3653 CapSize += FileSize;
3654 fclose (fpin);
3655 Index ++;
3656 }
3657
3658 //
3659 // Allocate buffer for capsule image.
3660 //
3661 CapBuffer = (UINT8 *) malloc (CapSize);
3662 if (CapBuffer == NULL) {
3663 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated for creating the capsule.");
3664 return EFI_OUT_OF_RESOURCES;
3665 }
3666
3667 //
3668 // Initialize the capsule header to zero
3669 //
3670 memset (CapBuffer, 0, mCapDataInfo.HeaderSize);
3671
3672 //
3673 // create capsule header and get capsule body
3674 //
3675 CapsuleHeader = (EFI_CAPSULE_HEADER *) CapBuffer;
3676 memcpy (&CapsuleHeader->CapsuleGuid, &mCapDataInfo.CapGuid, sizeof (EFI_GUID));
3677 CapsuleHeader->HeaderSize = mCapDataInfo.HeaderSize;
3678 CapsuleHeader->Flags = mCapDataInfo.Flags;
3679 CapsuleHeader->CapsuleImageSize = CapSize;
3680
3681 Index = 0;
3682 FileSize = 0;
3683 CapSize = CapsuleHeader->HeaderSize;
3684 while (mCapDataInfo.CapFiles [Index][0] != '\0') {
3685 fpin = fopen (mCapDataInfo.CapFiles[Index], "rb");
3686 if (fpin == NULL) {
3687 Error (NULL, 0, 0001, "Error opening file", mCapDataInfo.CapFiles[Index]);
3688 free (CapBuffer);
3689 return EFI_ABORTED;
3690 }
3691 FileSize = _filelength (fileno (fpin));
3692 fread (CapBuffer + CapSize, 1, FileSize, fpin);
3693 fclose (fpin);
3694 Index ++;
3695 CapSize += FileSize;
3696 }
3697
3698 //
3699 // write capsule data into the output file
3700 //
3701 fpout = fopen (CapFileName, "wb");
3702 if (fpout == NULL) {
3703 Error (NULL, 0, 0001, "Error opening file", CapFileName);
3704 free (CapBuffer);
3705 return EFI_ABORTED;
3706 }
3707
3708 fwrite (CapBuffer, 1, CapSize, fpout);
3709 fclose (fpout);
3710
3711 VerboseMsg ("The size of the generated capsule image is %u bytes", (unsigned) CapSize);
3712
3713 return EFI_SUCCESS;
3714 }