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