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