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