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