]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/GenFv/GenFvInternalLib.c
There is a limitation on WINDOWS OS for the length of entire file path can’t be large...
[mirror_edk2.git] / BaseTools / Source / C / GenFv / GenFvInternalLib.c
1 /** @file
2
3 Copyright (c) 2004 - 2014, 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_LONG_FILE_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 // 1 byte alignment
462 //
463 *Alignment = 0;
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_LONG_FILE_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 (LongFilePath (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 (LongFilePath (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_LONG_FILE_PATH];
2081 FILE *FvMapFile;
2082 EFI_FIRMWARE_VOLUME_EXT_HEADER *FvExtHeader;
2083 FILE *FvExtHeaderFile;
2084 UINTN FileSize;
2085 CHAR8 FvReportName[MAX_LONG_FILE_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 (LongFilePath (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 CompareGuid (&mFvDataInfo.FvFileSystemGuid, &mEfiFirmwareFileSystem3Guid) == 0) {
2217 mFvDataInfo.IsPiFvImage = TRUE;
2218 }
2219
2220 //
2221 // FvMap file to log the function address of all modules in one Fvimage
2222 //
2223 if (MapFileName != NULL) {
2224 strcpy (FvMapName, MapFileName);
2225 } else {
2226 strcpy (FvMapName, FvFileName);
2227 strcat (FvMapName, ".map");
2228 }
2229 VerboseMsg ("FV Map file name is %s", FvMapName);
2230
2231 //
2232 // FvReport file to log the FV information in one Fvimage
2233 //
2234 strcpy (FvReportName, FvFileName);
2235 strcat (FvReportName, ".txt");
2236
2237 //
2238 // Calculate the FV size and Update Fv Size based on the actual FFS files.
2239 // And Update mFvDataInfo data.
2240 //
2241 Status = CalculateFvSize (&mFvDataInfo);
2242 if (EFI_ERROR (Status)) {
2243 return Status;
2244 }
2245 VerboseMsg ("the generated FV image size is %u bytes", (unsigned) mFvDataInfo.Size);
2246
2247 //
2248 // support fv image and empty fv image
2249 //
2250 FvImageSize = mFvDataInfo.Size;
2251
2252 //
2253 // Allocate the FV, assure FvImage Header 8 byte alignment
2254 //
2255 FvBufferHeader = malloc (FvImageSize + sizeof (UINT64));
2256 if (FvBufferHeader == NULL) {
2257 return EFI_OUT_OF_RESOURCES;
2258 }
2259 FvImage = (UINT8 *) (((UINTN) FvBufferHeader + 7) & ~7);
2260
2261 //
2262 // Initialize the FV to the erase polarity
2263 //
2264 if (mFvDataInfo.FvAttributes == 0) {
2265 //
2266 // Set Default Fv Attribute
2267 //
2268 mFvDataInfo.FvAttributes = FV_DEFAULT_ATTRIBUTE;
2269 }
2270 if (mFvDataInfo.FvAttributes & EFI_FVB2_ERASE_POLARITY) {
2271 memset (FvImage, -1, FvImageSize);
2272 } else {
2273 memset (FvImage, 0, FvImageSize);
2274 }
2275
2276 //
2277 // Initialize FV header
2278 //
2279 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) FvImage;
2280
2281 //
2282 // Initialize the zero vector to all zeros.
2283 //
2284 memset (FvHeader->ZeroVector, 0, 16);
2285
2286 //
2287 // Copy the Fv file system GUID
2288 //
2289 memcpy (&FvHeader->FileSystemGuid, &mFvDataInfo.FvFileSystemGuid, sizeof (EFI_GUID));
2290
2291 FvHeader->FvLength = FvImageSize;
2292 FvHeader->Signature = EFI_FVH_SIGNATURE;
2293 FvHeader->Attributes = mFvDataInfo.FvAttributes;
2294 FvHeader->Revision = EFI_FVH_REVISION;
2295 FvHeader->ExtHeaderOffset = 0;
2296 FvHeader->Reserved[0] = 0;
2297
2298 //
2299 // Copy firmware block map
2300 //
2301 for (Index = 0; mFvDataInfo.FvBlocks[Index].Length != 0; Index++) {
2302 FvHeader->BlockMap[Index].NumBlocks = mFvDataInfo.FvBlocks[Index].NumBlocks;
2303 FvHeader->BlockMap[Index].Length = mFvDataInfo.FvBlocks[Index].Length;
2304 }
2305
2306 //
2307 // Add block map terminator
2308 //
2309 FvHeader->BlockMap[Index].NumBlocks = 0;
2310 FvHeader->BlockMap[Index].Length = 0;
2311
2312 //
2313 // Complete the header
2314 //
2315 FvHeader->HeaderLength = (UINT16) (((UINTN) &(FvHeader->BlockMap[Index + 1])) - (UINTN) FvImage);
2316 FvHeader->Checksum = 0;
2317 FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));
2318
2319 //
2320 // If there is no FFS file, generate one empty FV
2321 //
2322 if (mFvDataInfo.FvFiles[0][0] == 0 && !mFvDataInfo.FvNameGuidSet) {
2323 goto WriteFile;
2324 }
2325
2326 //
2327 // Initialize our "file" view of the buffer
2328 //
2329 FvImageMemoryFile.FileImage = (CHAR8 *)FvImage;
2330 FvImageMemoryFile.CurrentFilePointer = (CHAR8 *)FvImage + FvHeader->HeaderLength;
2331 FvImageMemoryFile.Eof = (CHAR8 *)FvImage + FvImageSize;
2332
2333 //
2334 // Initialize the FV library.
2335 //
2336 InitializeFvLib (FvImageMemoryFile.FileImage, FvImageSize);
2337
2338 //
2339 // Initialize the VTF file address.
2340 //
2341 VtfFileImage = (EFI_FFS_FILE_HEADER *) FvImageMemoryFile.Eof;
2342
2343 //
2344 // Open FvMap file
2345 //
2346 FvMapFile = fopen (LongFilePath (FvMapName), "w");
2347 if (FvMapFile == NULL) {
2348 Error (NULL, 0, 0001, "Error opening file", FvMapName);
2349 return EFI_ABORTED;
2350 }
2351
2352 //
2353 // Open FvReport file
2354 //
2355 FvReportFile = fopen (LongFilePath (FvReportName), "w");
2356 if (FvReportFile == NULL) {
2357 Error (NULL, 0, 0001, "Error opening file", FvReportName);
2358 return EFI_ABORTED;
2359 }
2360 //
2361 // record FV size information into FvMap file.
2362 //
2363 if (mFvTotalSize != 0) {
2364 fprintf (FvMapFile, EFI_FV_TOTAL_SIZE_STRING);
2365 fprintf (FvMapFile, " = 0x%x\n", (unsigned) mFvTotalSize);
2366 }
2367 if (mFvTakenSize != 0) {
2368 fprintf (FvMapFile, EFI_FV_TAKEN_SIZE_STRING);
2369 fprintf (FvMapFile, " = 0x%x\n", (unsigned) mFvTakenSize);
2370 }
2371 if (mFvTotalSize != 0 && mFvTakenSize != 0) {
2372 fprintf (FvMapFile, EFI_FV_SPACE_SIZE_STRING);
2373 fprintf (FvMapFile, " = 0x%x\n\n", (unsigned) (mFvTotalSize - mFvTakenSize));
2374 }
2375
2376 //
2377 // record FV size information to FvReportFile.
2378 //
2379 fprintf (FvReportFile, "%s = 0x%x\n", EFI_FV_TOTAL_SIZE_STRING, (unsigned) mFvTotalSize);
2380 fprintf (FvReportFile, "%s = 0x%x\n", EFI_FV_TAKEN_SIZE_STRING, (unsigned) mFvTakenSize);
2381
2382 //
2383 // Add PI FV extension header
2384 //
2385 if (FvExtHeader != NULL) {
2386 //
2387 // Add FV Extended Header contents to the FV as a PAD file
2388 //
2389 AddPadFile (&FvImageMemoryFile, 4, VtfFileImage, FvExtHeader, 0);
2390
2391 //
2392 // Fv Extension header change update Fv Header Check sum
2393 //
2394 FvHeader->Checksum = 0;
2395 FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));
2396 }
2397
2398 //
2399 // Add files to FV
2400 //
2401 for (Index = 0; mFvDataInfo.FvFiles[Index][0] != 0; Index++) {
2402 //
2403 // Add the file
2404 //
2405 Status = AddFile (&FvImageMemoryFile, &mFvDataInfo, Index, &VtfFileImage, FvMapFile, FvReportFile);
2406
2407 //
2408 // Exit if error detected while adding the file
2409 //
2410 if (EFI_ERROR (Status)) {
2411 goto Finish;
2412 }
2413 }
2414
2415 //
2416 // If there is a VTF file, some special actions need to occur.
2417 //
2418 if ((UINTN) VtfFileImage != (UINTN) FvImageMemoryFile.Eof) {
2419 //
2420 // Pad from the end of the last file to the beginning of the VTF file.
2421 // If the left space is less than sizeof (EFI_FFS_FILE_HEADER)?
2422 //
2423 Status = PadFvImage (&FvImageMemoryFile, VtfFileImage);
2424 if (EFI_ERROR (Status)) {
2425 Error (NULL, 0, 4002, "Resource", "FV space is full, cannot add pad file between the last file and the VTF file.");
2426 goto Finish;
2427 }
2428 if (!mArm) {
2429 //
2430 // Update reset vector (SALE_ENTRY for IPF)
2431 // Now for IA32 and IA64 platform, the fv which has bsf file must have the
2432 // EndAddress of 0xFFFFFFFF. Thus, only this type fv needs to update the
2433 // reset vector. If the PEI Core is found, the VTF file will probably get
2434 // corrupted by updating the entry point.
2435 //
2436 if ((mFvDataInfo.BaseAddress + mFvDataInfo.Size) == FV_IMAGES_TOP_ADDRESS) {
2437 Status = UpdateResetVector (&FvImageMemoryFile, &mFvDataInfo, VtfFileImage);
2438 if (EFI_ERROR(Status)) {
2439 Error (NULL, 0, 3000, "Invalid", "Could not update the reset vector.");
2440 goto Finish;
2441 }
2442 DebugMsg (NULL, 0, 9, "Update Reset vector in VTF file", NULL);
2443 }
2444 }
2445 }
2446
2447 if (mArm) {
2448 Status = UpdateArmResetVectorIfNeeded (&FvImageMemoryFile, &mFvDataInfo);
2449 if (EFI_ERROR (Status)) {
2450 Error (NULL, 0, 3000, "Invalid", "Could not update the reset vector.");
2451 goto Finish;
2452 }
2453
2454 //
2455 // Update Checksum for FvHeader
2456 //
2457 FvHeader->Checksum = 0;
2458 FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));
2459 }
2460
2461 //
2462 // Update FV Alignment attribute to the largest alignment of all the FFS files in the FV
2463 //
2464 if (((FvHeader->Attributes & EFI_FVB2_WEAK_ALIGNMENT) != EFI_FVB2_WEAK_ALIGNMENT) &&
2465 (((FvHeader->Attributes & EFI_FVB2_ALIGNMENT) >> 16)) < MaxFfsAlignment) {
2466 FvHeader->Attributes = ((MaxFfsAlignment << 16) | (FvHeader->Attributes & 0xFFFF));
2467 //
2468 // Update Checksum for FvHeader
2469 //
2470 FvHeader->Checksum = 0;
2471 FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));
2472 }
2473
2474 //
2475 // If there are large FFS in FV, the file system GUID should set to system 3 GUID.
2476 //
2477 if (mIsLargeFfs && CompareGuid (&FvHeader->FileSystemGuid, &mEfiFirmwareFileSystem2Guid) == 0) {
2478 memcpy (&FvHeader->FileSystemGuid, &mEfiFirmwareFileSystem3Guid, sizeof (EFI_GUID));
2479 FvHeader->Checksum = 0;
2480 FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));
2481 }
2482
2483 WriteFile:
2484 //
2485 // Write fv file
2486 //
2487 FvFile = fopen (LongFilePath (FvFileName), "wb");
2488 if (FvFile == NULL) {
2489 Error (NULL, 0, 0001, "Error opening file", FvFileName);
2490 Status = EFI_ABORTED;
2491 goto Finish;
2492 }
2493
2494 if (fwrite (FvImage, 1, FvImageSize, FvFile) != FvImageSize) {
2495 Error (NULL, 0, 0002, "Error writing file", FvFileName);
2496 Status = EFI_ABORTED;
2497 goto Finish;
2498 }
2499
2500 Finish:
2501 if (FvBufferHeader != NULL) {
2502 free (FvBufferHeader);
2503 }
2504
2505 if (FvExtHeader != NULL) {
2506 free (FvExtHeader);
2507 }
2508
2509 if (FvFile != NULL) {
2510 fflush (FvFile);
2511 fclose (FvFile);
2512 }
2513
2514 if (FvMapFile != NULL) {
2515 fflush (FvMapFile);
2516 fclose (FvMapFile);
2517 }
2518
2519 if (FvReportFile != NULL) {
2520 fflush (FvReportFile);
2521 fclose (FvReportFile);
2522 }
2523 return Status;
2524 }
2525
2526 EFI_STATUS
2527 UpdatePeiCoreEntryInFit (
2528 IN FIT_TABLE *FitTablePtr,
2529 IN UINT64 PeiCorePhysicalAddress
2530 )
2531 /*++
2532
2533 Routine Description:
2534
2535 This function is used to update the Pei Core address in FIT, this can be used by Sec core to pass control from
2536 Sec to Pei Core
2537
2538 Arguments:
2539
2540 FitTablePtr - The pointer of FIT_TABLE.
2541 PeiCorePhysicalAddress - The address of Pei Core entry.
2542
2543 Returns:
2544
2545 EFI_SUCCESS - The PEI_CORE FIT entry was updated successfully.
2546 EFI_NOT_FOUND - Not found the PEI_CORE FIT entry.
2547
2548 --*/
2549 {
2550 FIT_TABLE *TmpFitPtr;
2551 UINTN Index;
2552 UINTN NumFitComponents;
2553
2554 TmpFitPtr = FitTablePtr;
2555 NumFitComponents = TmpFitPtr->CompSize;
2556
2557 for (Index = 0; Index < NumFitComponents; Index++) {
2558 if ((TmpFitPtr->CvAndType & FIT_TYPE_MASK) == COMP_TYPE_FIT_PEICORE) {
2559 TmpFitPtr->CompAddress = PeiCorePhysicalAddress;
2560 return EFI_SUCCESS;
2561 }
2562
2563 TmpFitPtr++;
2564 }
2565
2566 return EFI_NOT_FOUND;
2567 }
2568
2569 VOID
2570 UpdateFitCheckSum (
2571 IN FIT_TABLE *FitTablePtr
2572 )
2573 /*++
2574
2575 Routine Description:
2576
2577 This function is used to update the checksum for FIT.
2578
2579
2580 Arguments:
2581
2582 FitTablePtr - The pointer of FIT_TABLE.
2583
2584 Returns:
2585
2586 None.
2587
2588 --*/
2589 {
2590 if ((FitTablePtr->CvAndType & CHECKSUM_BIT_MASK) >> 7) {
2591 FitTablePtr->CheckSum = 0;
2592 FitTablePtr->CheckSum = CalculateChecksum8 ((UINT8 *) FitTablePtr, FitTablePtr->CompSize * 16);
2593 }
2594 }
2595
2596 EFI_STATUS
2597 CalculateFvSize (
2598 FV_INFO *FvInfoPtr
2599 )
2600 /*++
2601 Routine Description:
2602 Calculate the FV size and Update Fv Size based on the actual FFS files.
2603 And Update FvInfo data.
2604
2605 Arguments:
2606 FvInfoPtr - The pointer to FV_INFO structure.
2607
2608 Returns:
2609 EFI_ABORTED - Ffs Image Error
2610 EFI_SUCCESS - Successfully update FvSize
2611 --*/
2612 {
2613 UINTN CurrentOffset;
2614 UINTN Index;
2615 FILE *fpin;
2616 UINTN FfsFileSize;
2617 UINTN FvExtendHeaderSize;
2618 UINT32 FfsAlignment;
2619 UINT32 FfsHeaderSize;
2620 EFI_FFS_FILE_HEADER FfsHeader;
2621 BOOLEAN VtfFileFlag;
2622 UINTN VtfFileSize;
2623
2624 FvExtendHeaderSize = 0;
2625 VtfFileSize = 0;
2626 VtfFileFlag = FALSE;
2627 fpin = NULL;
2628 Index = 0;
2629
2630 //
2631 // Compute size for easy access later
2632 //
2633 FvInfoPtr->Size = 0;
2634 for (Index = 0; FvInfoPtr->FvBlocks[Index].NumBlocks > 0 && FvInfoPtr->FvBlocks[Index].Length > 0; Index++) {
2635 FvInfoPtr->Size += FvInfoPtr->FvBlocks[Index].NumBlocks * FvInfoPtr->FvBlocks[Index].Length;
2636 }
2637
2638 //
2639 // Caculate the required sizes for all FFS files.
2640 //
2641 CurrentOffset = sizeof (EFI_FIRMWARE_VOLUME_HEADER);
2642
2643 for (Index = 1;; Index ++) {
2644 CurrentOffset += sizeof (EFI_FV_BLOCK_MAP_ENTRY);
2645 if (FvInfoPtr->FvBlocks[Index].NumBlocks == 0 || FvInfoPtr->FvBlocks[Index].Length == 0) {
2646 break;
2647 }
2648 }
2649
2650 //
2651 // Calculate PI extension header
2652 //
2653 if (mFvDataInfo.FvExtHeaderFile[0] != '\0') {
2654 fpin = fopen (LongFilePath (mFvDataInfo.FvExtHeaderFile), "rb");
2655 if (fpin == NULL) {
2656 Error (NULL, 0, 0001, "Error opening file", mFvDataInfo.FvExtHeaderFile);
2657 return EFI_ABORTED;
2658 }
2659 FvExtendHeaderSize = _filelength (fileno (fpin));
2660 fclose (fpin);
2661 if (sizeof (EFI_FFS_FILE_HEADER) + FvExtendHeaderSize >= MAX_FFS_SIZE) {
2662 CurrentOffset += sizeof (EFI_FFS_FILE_HEADER2) + FvExtendHeaderSize;
2663 mIsLargeFfs = TRUE;
2664 } else {
2665 CurrentOffset += sizeof (EFI_FFS_FILE_HEADER) + FvExtendHeaderSize;
2666 }
2667 CurrentOffset = (CurrentOffset + 7) & (~7);
2668 } else if (mFvDataInfo.FvNameGuidSet) {
2669 CurrentOffset += sizeof (EFI_FFS_FILE_HEADER) + sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER);
2670 CurrentOffset = (CurrentOffset + 7) & (~7);
2671 }
2672
2673 //
2674 // Accumlate every FFS file size.
2675 //
2676 for (Index = 0; FvInfoPtr->FvFiles[Index][0] != 0; Index++) {
2677 //
2678 // Open FFS file
2679 //
2680 fpin = NULL;
2681 fpin = fopen (LongFilePath (FvInfoPtr->FvFiles[Index]), "rb");
2682 if (fpin == NULL) {
2683 Error (NULL, 0, 0001, "Error opening file", FvInfoPtr->FvFiles[Index]);
2684 return EFI_ABORTED;
2685 }
2686 //
2687 // Get the file size
2688 //
2689 FfsFileSize = _filelength (fileno (fpin));
2690 if (FfsFileSize >= MAX_FFS_SIZE) {
2691 FfsHeaderSize = sizeof(EFI_FFS_FILE_HEADER2);
2692 mIsLargeFfs = TRUE;
2693 } else {
2694 FfsHeaderSize = sizeof(EFI_FFS_FILE_HEADER);
2695 }
2696 //
2697 // Read Ffs File header
2698 //
2699 fread (&FfsHeader, sizeof (UINT8), sizeof (EFI_FFS_FILE_HEADER), fpin);
2700 //
2701 // close file
2702 //
2703 fclose (fpin);
2704
2705 if (FvInfoPtr->IsPiFvImage) {
2706 //
2707 // Check whether this ffs file is vtf file
2708 //
2709 if (IsVtfFile (&FfsHeader)) {
2710 if (VtfFileFlag) {
2711 //
2712 // One Fv image can't have two vtf files.
2713 //
2714 return EFI_ABORTED;
2715 }
2716 VtfFileFlag = TRUE;
2717 VtfFileSize = FfsFileSize;
2718 continue;
2719 }
2720
2721 //
2722 // Get the alignment of FFS file
2723 //
2724 ReadFfsAlignment (&FfsHeader, &FfsAlignment);
2725 FfsAlignment = 1 << FfsAlignment;
2726 //
2727 // Add Pad file
2728 //
2729 if (((CurrentOffset + FfsHeaderSize) % FfsAlignment) != 0) {
2730 //
2731 // Only EFI_FFS_FILE_HEADER is needed for a pad section.
2732 //
2733 CurrentOffset = (CurrentOffset + FfsHeaderSize + sizeof(EFI_FFS_FILE_HEADER) + FfsAlignment - 1) & ~(FfsAlignment - 1);
2734 CurrentOffset -= FfsHeaderSize;
2735 }
2736 }
2737
2738 //
2739 // Add ffs file size
2740 //
2741 if (FvInfoPtr->SizeofFvFiles[Index] > FfsFileSize) {
2742 CurrentOffset += FvInfoPtr->SizeofFvFiles[Index];
2743 } else {
2744 CurrentOffset += FfsFileSize;
2745 }
2746
2747 //
2748 // Make next ffs file start at QWord Boundry
2749 //
2750 if (FvInfoPtr->IsPiFvImage) {
2751 CurrentOffset = (CurrentOffset + EFI_FFS_FILE_HEADER_ALIGNMENT - 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT - 1);
2752 }
2753 }
2754 CurrentOffset += VtfFileSize;
2755 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);
2756
2757 if (FvInfoPtr->Size == 0) {
2758 //
2759 // Update FvInfo data
2760 //
2761 FvInfoPtr->FvBlocks[0].NumBlocks = CurrentOffset / FvInfoPtr->FvBlocks[0].Length + ((CurrentOffset % FvInfoPtr->FvBlocks[0].Length)?1:0);
2762 FvInfoPtr->Size = FvInfoPtr->FvBlocks[0].NumBlocks * FvInfoPtr->FvBlocks[0].Length;
2763 FvInfoPtr->FvBlocks[1].NumBlocks = 0;
2764 FvInfoPtr->FvBlocks[1].Length = 0;
2765 } else if (FvInfoPtr->Size < CurrentOffset) {
2766 //
2767 // Not invalid
2768 //
2769 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);
2770 return EFI_INVALID_PARAMETER;
2771 }
2772
2773 //
2774 // Set Fv Size Information
2775 //
2776 mFvTotalSize = FvInfoPtr->Size;
2777 mFvTakenSize = CurrentOffset;
2778
2779 return EFI_SUCCESS;
2780 }
2781
2782 EFI_STATUS
2783 FfsRebaseImageRead (
2784 IN VOID *FileHandle,
2785 IN UINTN FileOffset,
2786 IN OUT UINT32 *ReadSize,
2787 OUT VOID *Buffer
2788 )
2789 /*++
2790
2791 Routine Description:
2792
2793 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
2794
2795 Arguments:
2796
2797 FileHandle - The handle to the PE/COFF file
2798
2799 FileOffset - The offset, in bytes, into the file to read
2800
2801 ReadSize - The number of bytes to read from the file starting at FileOffset
2802
2803 Buffer - A pointer to the buffer to read the data into.
2804
2805 Returns:
2806
2807 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
2808
2809 --*/
2810 {
2811 CHAR8 *Destination8;
2812 CHAR8 *Source8;
2813 UINT32 Length;
2814
2815 Destination8 = Buffer;
2816 Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);
2817 Length = *ReadSize;
2818 while (Length--) {
2819 *(Destination8++) = *(Source8++);
2820 }
2821
2822 return EFI_SUCCESS;
2823 }
2824
2825 EFI_STATUS
2826 GetChildFvFromFfs (
2827 IN FV_INFO *FvInfo,
2828 IN EFI_FFS_FILE_HEADER *FfsFile,
2829 IN UINTN XipOffset
2830 )
2831 /*++
2832
2833 Routine Description:
2834
2835 This function gets all child FvImages in the input FfsFile, and records
2836 their base address to the parent image.
2837
2838 Arguments:
2839 FvInfo A pointer to FV_INFO struture.
2840 FfsFile A pointer to Ffs file image that may contain FvImage.
2841 XipOffset The offset address to the parent FvImage base.
2842
2843 Returns:
2844
2845 EFI_SUCCESS Base address of child Fv image is recorded.
2846 --*/
2847 {
2848 EFI_STATUS Status;
2849 UINTN Index;
2850 EFI_FILE_SECTION_POINTER SubFvSection;
2851 EFI_FIRMWARE_VOLUME_HEADER *SubFvImageHeader;
2852 EFI_PHYSICAL_ADDRESS SubFvBaseAddress;
2853
2854 for (Index = 1;; Index++) {
2855 //
2856 // Find FV section
2857 //
2858 Status = GetSectionByType (FfsFile, EFI_SECTION_FIRMWARE_VOLUME_IMAGE, Index, &SubFvSection);
2859 if (EFI_ERROR (Status)) {
2860 break;
2861 }
2862 SubFvImageHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINT8 *) SubFvSection.FVImageSection + GetSectionHeaderLength(SubFvSection.FVImageSection));
2863 //
2864 // Rebase on Flash
2865 //
2866 SubFvBaseAddress = FvInfo->BaseAddress + (UINTN) SubFvImageHeader - (UINTN) FfsFile + XipOffset;
2867 mFvBaseAddress[mFvBaseAddressNumber ++ ] = SubFvBaseAddress;
2868 }
2869
2870 return EFI_SUCCESS;
2871 }
2872
2873 EFI_STATUS
2874 FfsRebase (
2875 IN OUT FV_INFO *FvInfo,
2876 IN CHAR8 *FileName,
2877 IN OUT EFI_FFS_FILE_HEADER *FfsFile,
2878 IN UINTN XipOffset,
2879 IN FILE *FvMapFile
2880 )
2881 /*++
2882
2883 Routine Description:
2884
2885 This function determines if a file is XIP and should be rebased. It will
2886 rebase any PE32 sections found in the file using the base address.
2887
2888 Arguments:
2889
2890 FvInfo A pointer to FV_INFO struture.
2891 FileName Ffs File PathName
2892 FfsFile A pointer to Ffs file image.
2893 XipOffset The offset address to use for rebasing the XIP file image.
2894 FvMapFile FvMapFile to record the function address in one Fvimage
2895
2896 Returns:
2897
2898 EFI_SUCCESS The image was properly rebased.
2899 EFI_INVALID_PARAMETER An input parameter is invalid.
2900 EFI_ABORTED An error occurred while rebasing the input file image.
2901 EFI_OUT_OF_RESOURCES Could not allocate a required resource.
2902 EFI_NOT_FOUND No compressed sections could be found.
2903
2904 --*/
2905 {
2906 EFI_STATUS Status;
2907 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
2908 PE_COFF_LOADER_IMAGE_CONTEXT OrigImageContext;
2909 EFI_PHYSICAL_ADDRESS XipBase;
2910 EFI_PHYSICAL_ADDRESS NewPe32BaseAddress;
2911 UINTN Index;
2912 EFI_FILE_SECTION_POINTER CurrentPe32Section;
2913 EFI_FFS_FILE_STATE SavedState;
2914 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
2915 EFI_TE_IMAGE_HEADER *TEImageHeader;
2916 UINT8 *MemoryImagePointer;
2917 EFI_IMAGE_SECTION_HEADER *SectionHeader;
2918 CHAR8 PeFileName [MAX_LONG_FILE_PATH];
2919 CHAR8 *Cptr;
2920 FILE *PeFile;
2921 UINT8 *PeFileBuffer;
2922 UINT32 PeFileSize;
2923 CHAR8 *PdbPointer;
2924 UINT32 FfsHeaderSize;
2925 UINT32 CurSecHdrSize;
2926
2927 Index = 0;
2928 MemoryImagePointer = NULL;
2929 TEImageHeader = NULL;
2930 ImgHdr = NULL;
2931 SectionHeader = NULL;
2932 Cptr = NULL;
2933 PeFile = NULL;
2934 PeFileBuffer = NULL;
2935
2936 //
2937 // Don't need to relocate image when BaseAddress is zero and no ForceRebase Flag specified.
2938 //
2939 if ((FvInfo->BaseAddress == 0) && (FvInfo->ForceRebase == -1)) {
2940 return EFI_SUCCESS;
2941 }
2942
2943 //
2944 // If ForceRebase Flag specified to FALSE, will always not take rebase action.
2945 //
2946 if (FvInfo->ForceRebase == 0) {
2947 return EFI_SUCCESS;
2948 }
2949
2950
2951 XipBase = FvInfo->BaseAddress + XipOffset;
2952
2953 //
2954 // We only process files potentially containing PE32 sections.
2955 //
2956 switch (FfsFile->Type) {
2957 case EFI_FV_FILETYPE_SECURITY_CORE:
2958 case EFI_FV_FILETYPE_PEI_CORE:
2959 case EFI_FV_FILETYPE_PEIM:
2960 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER:
2961 case EFI_FV_FILETYPE_DRIVER:
2962 case EFI_FV_FILETYPE_DXE_CORE:
2963 break;
2964 case EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE:
2965 //
2966 // Rebase the inside FvImage.
2967 //
2968 GetChildFvFromFfs (FvInfo, FfsFile, XipOffset);
2969
2970 //
2971 // Search PE/TE section in FV sectin.
2972 //
2973 break;
2974 default:
2975 return EFI_SUCCESS;
2976 }
2977
2978 FfsHeaderSize = GetFfsHeaderLength(FfsFile);
2979 //
2980 // Rebase each PE32 section
2981 //
2982 Status = EFI_SUCCESS;
2983 for (Index = 1;; Index++) {
2984 //
2985 // Init Value
2986 //
2987 NewPe32BaseAddress = 0;
2988
2989 //
2990 // Find Pe Image
2991 //
2992 Status = GetSectionByType (FfsFile, EFI_SECTION_PE32, Index, &CurrentPe32Section);
2993 if (EFI_ERROR (Status)) {
2994 break;
2995 }
2996 CurSecHdrSize = GetSectionHeaderLength(CurrentPe32Section.CommonHeader);
2997
2998 //
2999 // Initialize context
3000 //
3001 memset (&ImageContext, 0, sizeof (ImageContext));
3002 ImageContext.Handle = (VOID *) ((UINTN) CurrentPe32Section.Pe32Section + CurSecHdrSize);
3003 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) FfsRebaseImageRead;
3004 Status = PeCoffLoaderGetImageInfo (&ImageContext);
3005 if (EFI_ERROR (Status)) {
3006 Error (NULL, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName, (int) Status);
3007 return Status;
3008 }
3009
3010 if ( (ImageContext.Machine == EFI_IMAGE_MACHINE_ARMT) ||
3011 (ImageContext.Machine == EFI_IMAGE_MACHINE_AARCH64) ) {
3012 mArm = TRUE;
3013 }
3014
3015 //
3016 // Keep Image Context for PE image in FV
3017 //
3018 memcpy (&OrigImageContext, &ImageContext, sizeof (ImageContext));
3019
3020 //
3021 // Get File PdbPointer
3022 //
3023 PdbPointer = PeCoffLoaderGetPdbPointer (ImageContext.Handle);
3024
3025 //
3026 // Get PeHeader pointer
3027 //
3028 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((UINTN) CurrentPe32Section.Pe32Section + CurSecHdrSize + ImageContext.PeCoffHeaderOffset);
3029
3030 //
3031 // Calculate the PE32 base address, based on file type
3032 //
3033 switch (FfsFile->Type) {
3034 case EFI_FV_FILETYPE_SECURITY_CORE:
3035 case EFI_FV_FILETYPE_PEI_CORE:
3036 case EFI_FV_FILETYPE_PEIM:
3037 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER:
3038 //
3039 // Check if section-alignment and file-alignment match or not
3040 //
3041 if ((ImgHdr->Pe32.OptionalHeader.SectionAlignment != ImgHdr->Pe32.OptionalHeader.FileAlignment)) {
3042 //
3043 // Xip module has the same section alignment and file alignment.
3044 //
3045 Error (NULL, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName);
3046 return EFI_ABORTED;
3047 }
3048 //
3049 // PeImage has no reloc section. It will try to get reloc data from the original EFI image.
3050 //
3051 if (ImageContext.RelocationsStripped) {
3052 //
3053 // Construct the original efi file Name
3054 //
3055 strcpy (PeFileName, FileName);
3056 Cptr = PeFileName + strlen (PeFileName);
3057 while (*Cptr != '.') {
3058 Cptr --;
3059 }
3060 if (*Cptr != '.') {
3061 Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3062 return EFI_ABORTED;
3063 } else {
3064 *(Cptr + 1) = 'e';
3065 *(Cptr + 2) = 'f';
3066 *(Cptr + 3) = 'i';
3067 *(Cptr + 4) = '\0';
3068 }
3069 PeFile = fopen (LongFilePath (PeFileName), "rb");
3070 if (PeFile == NULL) {
3071 Warning (NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);
3072 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3073 //return EFI_ABORTED;
3074 break;
3075 }
3076 //
3077 // Get the file size
3078 //
3079 PeFileSize = _filelength (fileno (PeFile));
3080 PeFileBuffer = (UINT8 *) malloc (PeFileSize);
3081 if (PeFileBuffer == NULL) {
3082 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);
3083 return EFI_OUT_OF_RESOURCES;
3084 }
3085 //
3086 // Read Pe File
3087 //
3088 fread (PeFileBuffer, sizeof (UINT8), PeFileSize, PeFile);
3089 //
3090 // close file
3091 //
3092 fclose (PeFile);
3093 //
3094 // Handle pointer to the original efi image.
3095 //
3096 ImageContext.Handle = PeFileBuffer;
3097 Status = PeCoffLoaderGetImageInfo (&ImageContext);
3098 if (EFI_ERROR (Status)) {
3099 Error (NULL, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName, (int) Status);
3100 return Status;
3101 }
3102 ImageContext.RelocationsStripped = FALSE;
3103 }
3104
3105 NewPe32BaseAddress = XipBase + (UINTN) CurrentPe32Section.Pe32Section + CurSecHdrSize - (UINTN)FfsFile;
3106 break;
3107
3108 case EFI_FV_FILETYPE_DRIVER:
3109 case EFI_FV_FILETYPE_DXE_CORE:
3110 //
3111 // Check if section-alignment and file-alignment match or not
3112 //
3113 if ((ImgHdr->Pe32.OptionalHeader.SectionAlignment != ImgHdr->Pe32.OptionalHeader.FileAlignment)) {
3114 //
3115 // Xip module has the same section alignment and file alignment.
3116 //
3117 Error (NULL, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName);
3118 return EFI_ABORTED;
3119 }
3120 NewPe32BaseAddress = XipBase + (UINTN) CurrentPe32Section.Pe32Section + CurSecHdrSize - (UINTN)FfsFile;
3121 break;
3122
3123 default:
3124 //
3125 // Not supported file type
3126 //
3127 return EFI_SUCCESS;
3128 }
3129
3130 //
3131 // Relocation doesn't exist
3132 //
3133 if (ImageContext.RelocationsStripped) {
3134 Warning (NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);
3135 continue;
3136 }
3137
3138 //
3139 // Relocation exist and rebase
3140 //
3141 //
3142 // Load and Relocate Image Data
3143 //
3144 MemoryImagePointer = (UINT8 *) malloc ((UINTN) ImageContext.ImageSize + ImageContext.SectionAlignment);
3145 if (MemoryImagePointer == NULL) {
3146 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);
3147 return EFI_OUT_OF_RESOURCES;
3148 }
3149 memset ((VOID *) MemoryImagePointer, 0, (UINTN) ImageContext.ImageSize + ImageContext.SectionAlignment);
3150 ImageContext.ImageAddress = ((UINTN) MemoryImagePointer + ImageContext.SectionAlignment - 1) & (~((UINTN) ImageContext.SectionAlignment - 1));
3151
3152 Status = PeCoffLoaderLoadImage (&ImageContext);
3153 if (EFI_ERROR (Status)) {
3154 Error (NULL, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName);
3155 free ((VOID *) MemoryImagePointer);
3156 return Status;
3157 }
3158
3159 ImageContext.DestinationAddress = NewPe32BaseAddress;
3160 Status = PeCoffLoaderRelocateImage (&ImageContext);
3161 if (EFI_ERROR (Status)) {
3162 Error (NULL, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of %s", FileName);
3163 free ((VOID *) MemoryImagePointer);
3164 return Status;
3165 }
3166
3167 //
3168 // Copy Relocated data to raw image file.
3169 //
3170 SectionHeader = (EFI_IMAGE_SECTION_HEADER *) (
3171 (UINTN) ImgHdr +
3172 sizeof (UINT32) +
3173 sizeof (EFI_IMAGE_FILE_HEADER) +
3174 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader
3175 );
3176
3177 for (Index = 0; Index < ImgHdr->Pe32.FileHeader.NumberOfSections; Index ++, SectionHeader ++) {
3178 CopyMem (
3179 (UINT8 *) CurrentPe32Section.Pe32Section + CurSecHdrSize + SectionHeader->PointerToRawData,
3180 (VOID*) (UINTN) (ImageContext.ImageAddress + SectionHeader->VirtualAddress),
3181 SectionHeader->SizeOfRawData
3182 );
3183 }
3184
3185 free ((VOID *) MemoryImagePointer);
3186 MemoryImagePointer = NULL;
3187 if (PeFileBuffer != NULL) {
3188 free (PeFileBuffer);
3189 PeFileBuffer = NULL;
3190 }
3191
3192 //
3193 // Update Image Base Address
3194 //
3195 if (ImgHdr->Pe32.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
3196 ImgHdr->Pe32.OptionalHeader.ImageBase = (UINT32) NewPe32BaseAddress;
3197 } else if (ImgHdr->Pe32Plus.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
3198 ImgHdr->Pe32Plus.OptionalHeader.ImageBase = NewPe32BaseAddress;
3199 } else {
3200 Error (NULL, 0, 3000, "Invalid", "unknown PE magic signature %X in PE32 image %s",
3201 ImgHdr->Pe32.OptionalHeader.Magic,
3202 FileName
3203 );
3204 return EFI_ABORTED;
3205 }
3206
3207 //
3208 // Now update file checksum
3209 //
3210 if (FfsFile->Attributes & FFS_ATTRIB_CHECKSUM) {
3211 SavedState = FfsFile->State;
3212 FfsFile->IntegrityCheck.Checksum.File = 0;
3213 FfsFile->State = 0;
3214 FfsFile->IntegrityCheck.Checksum.File = CalculateChecksum8 (
3215 (UINT8 *) ((UINT8 *)FfsFile + FfsHeaderSize),
3216 GetFfsFileLength (FfsFile) - FfsHeaderSize
3217 );
3218 FfsFile->State = SavedState;
3219 }
3220
3221 //
3222 // Get this module function address from ModulePeMapFile and add them into FvMap file
3223 //
3224
3225 //
3226 // Default use FileName as map file path
3227 //
3228 if (PdbPointer == NULL) {
3229 PdbPointer = FileName;
3230 }
3231
3232 WriteMapFile (FvMapFile, PdbPointer, FfsFile, NewPe32BaseAddress, &OrigImageContext);
3233 }
3234
3235 if (FfsFile->Type != EFI_FV_FILETYPE_SECURITY_CORE &&
3236 FfsFile->Type != EFI_FV_FILETYPE_PEI_CORE &&
3237 FfsFile->Type != EFI_FV_FILETYPE_PEIM &&
3238 FfsFile->Type != EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER &&
3239 FfsFile->Type != EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
3240 ) {
3241 //
3242 // Only Peim code may have a TE section
3243 //
3244 return EFI_SUCCESS;
3245 }
3246
3247 //
3248 // Now process TE sections
3249 //
3250 for (Index = 1;; Index++) {
3251 NewPe32BaseAddress = 0;
3252
3253 //
3254 // Find Te Image
3255 //
3256 Status = GetSectionByType (FfsFile, EFI_SECTION_TE, Index, &CurrentPe32Section);
3257 if (EFI_ERROR (Status)) {
3258 break;
3259 }
3260
3261 CurSecHdrSize = GetSectionHeaderLength(CurrentPe32Section.CommonHeader);
3262
3263 //
3264 // Calculate the TE base address, the FFS file base plus the offset of the TE section less the size stripped off
3265 // by GenTEImage
3266 //
3267 TEImageHeader = (EFI_TE_IMAGE_HEADER *) ((UINT8 *) CurrentPe32Section.Pe32Section + CurSecHdrSize);
3268
3269 //
3270 // Initialize context, load image info.
3271 //
3272 memset (&ImageContext, 0, sizeof (ImageContext));
3273 ImageContext.Handle = (VOID *) TEImageHeader;
3274 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) FfsRebaseImageRead;
3275 Status = PeCoffLoaderGetImageInfo (&ImageContext);
3276 if (EFI_ERROR (Status)) {
3277 Error (NULL, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName, (int) Status);
3278 return Status;
3279 }
3280
3281 if ( (ImageContext.Machine == EFI_IMAGE_MACHINE_ARMT) ||
3282 (ImageContext.Machine == EFI_IMAGE_MACHINE_AARCH64) ) {
3283 mArm = TRUE;
3284 }
3285
3286 //
3287 // Keep Image Context for TE image in FV
3288 //
3289 memcpy (&OrigImageContext, &ImageContext, sizeof (ImageContext));
3290
3291 //
3292 // Get File PdbPointer
3293 //
3294 PdbPointer = PeCoffLoaderGetPdbPointer (ImageContext.Handle);
3295
3296 //
3297 // Set new rebased address.
3298 //
3299 NewPe32BaseAddress = XipBase + (UINTN) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) \
3300 - TEImageHeader->StrippedSize - (UINTN) FfsFile;
3301
3302 //
3303 // if reloc is stripped, try to get the original efi image to get reloc info.
3304 //
3305 if (ImageContext.RelocationsStripped) {
3306 //
3307 // Construct the original efi file name
3308 //
3309 strcpy (PeFileName, FileName);
3310 Cptr = PeFileName + strlen (PeFileName);
3311 while (*Cptr != '.') {
3312 Cptr --;
3313 }
3314
3315 if (*Cptr != '.') {
3316 Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3317 return EFI_ABORTED;
3318 } else {
3319 *(Cptr + 1) = 'e';
3320 *(Cptr + 2) = 'f';
3321 *(Cptr + 3) = 'i';
3322 *(Cptr + 4) = '\0';
3323 }
3324
3325 PeFile = fopen (LongFilePath (PeFileName), "rb");
3326 if (PeFile == NULL) {
3327 Warning (NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);
3328 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3329 //return EFI_ABORTED;
3330 } else {
3331 //
3332 // Get the file size
3333 //
3334 PeFileSize = _filelength (fileno (PeFile));
3335 PeFileBuffer = (UINT8 *) malloc (PeFileSize);
3336 if (PeFileBuffer == NULL) {
3337 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);
3338 return EFI_OUT_OF_RESOURCES;
3339 }
3340 //
3341 // Read Pe File
3342 //
3343 fread (PeFileBuffer, sizeof (UINT8), PeFileSize, PeFile);
3344 //
3345 // close file
3346 //
3347 fclose (PeFile);
3348 //
3349 // Append reloc section into TeImage
3350 //
3351 ImageContext.Handle = PeFileBuffer;
3352 Status = PeCoffLoaderGetImageInfo (&ImageContext);
3353 if (EFI_ERROR (Status)) {
3354 Error (NULL, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName, (int) Status);
3355 return Status;
3356 }
3357 ImageContext.RelocationsStripped = FALSE;
3358 }
3359 }
3360 //
3361 // Relocation doesn't exist
3362 //
3363 if (ImageContext.RelocationsStripped) {
3364 Warning (NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);
3365 continue;
3366 }
3367
3368 //
3369 // Relocation exist and rebase
3370 //
3371 //
3372 // Load and Relocate Image Data
3373 //
3374 MemoryImagePointer = (UINT8 *) malloc ((UINTN) ImageContext.ImageSize + ImageContext.SectionAlignment);
3375 if (MemoryImagePointer == NULL) {
3376 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);
3377 return EFI_OUT_OF_RESOURCES;
3378 }
3379 memset ((VOID *) MemoryImagePointer, 0, (UINTN) ImageContext.ImageSize + ImageContext.SectionAlignment);
3380 ImageContext.ImageAddress = ((UINTN) MemoryImagePointer + ImageContext.SectionAlignment - 1) & (~((UINTN) ImageContext.SectionAlignment - 1));
3381
3382 Status = PeCoffLoaderLoadImage (&ImageContext);
3383 if (EFI_ERROR (Status)) {
3384 Error (NULL, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName);
3385 free ((VOID *) MemoryImagePointer);
3386 return Status;
3387 }
3388 //
3389 // Reloacate TeImage
3390 //
3391 ImageContext.DestinationAddress = NewPe32BaseAddress;
3392 Status = PeCoffLoaderRelocateImage (&ImageContext);
3393 if (EFI_ERROR (Status)) {
3394 Error (NULL, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of TE image %s", FileName);
3395 free ((VOID *) MemoryImagePointer);
3396 return Status;
3397 }
3398
3399 //
3400 // Copy the relocated image into raw image file.
3401 //
3402 SectionHeader = (EFI_IMAGE_SECTION_HEADER *) (TEImageHeader + 1);
3403 for (Index = 0; Index < TEImageHeader->NumberOfSections; Index ++, SectionHeader ++) {
3404 if (!ImageContext.IsTeImage) {
3405 CopyMem (
3406 (UINT8 *) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize + SectionHeader->PointerToRawData,
3407 (VOID*) (UINTN) (ImageContext.ImageAddress + SectionHeader->VirtualAddress),
3408 SectionHeader->SizeOfRawData
3409 );
3410 } else {
3411 CopyMem (
3412 (UINT8 *) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize + SectionHeader->PointerToRawData,
3413 (VOID*) (UINTN) (ImageContext.ImageAddress + sizeof (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize + SectionHeader->VirtualAddress),
3414 SectionHeader->SizeOfRawData
3415 );
3416 }
3417 }
3418
3419 //
3420 // Free the allocated memory resource
3421 //
3422 free ((VOID *) MemoryImagePointer);
3423 MemoryImagePointer = NULL;
3424 if (PeFileBuffer != NULL) {
3425 free (PeFileBuffer);
3426 PeFileBuffer = NULL;
3427 }
3428
3429 //
3430 // Update Image Base Address
3431 //
3432 TEImageHeader->ImageBase = NewPe32BaseAddress;
3433
3434 //
3435 // Now update file checksum
3436 //
3437 if (FfsFile->Attributes & FFS_ATTRIB_CHECKSUM) {
3438 SavedState = FfsFile->State;
3439 FfsFile->IntegrityCheck.Checksum.File = 0;
3440 FfsFile->State = 0;
3441 FfsFile->IntegrityCheck.Checksum.File = CalculateChecksum8 (
3442 (UINT8 *)((UINT8 *)FfsFile + FfsHeaderSize),
3443 GetFfsFileLength (FfsFile) - FfsHeaderSize
3444 );
3445 FfsFile->State = SavedState;
3446 }
3447 //
3448 // Get this module function address from ModulePeMapFile and add them into FvMap file
3449 //
3450
3451 //
3452 // Default use FileName as map file path
3453 //
3454 if (PdbPointer == NULL) {
3455 PdbPointer = FileName;
3456 }
3457
3458 WriteMapFile (
3459 FvMapFile,
3460 PdbPointer,
3461 FfsFile,
3462 NewPe32BaseAddress,
3463 &OrigImageContext
3464 );
3465 }
3466
3467 return EFI_SUCCESS;
3468 }
3469
3470 EFI_STATUS
3471 FindApResetVectorPosition (
3472 IN MEMORY_FILE *FvImage,
3473 OUT UINT8 **Pointer
3474 )
3475 /*++
3476
3477 Routine Description:
3478
3479 Find the position in this FvImage to place Ap reset vector.
3480
3481 Arguments:
3482
3483 FvImage Memory file for the FV memory image.
3484 Pointer Pointer to pointer to position.
3485
3486 Returns:
3487
3488 EFI_NOT_FOUND - No satisfied position is found.
3489 EFI_SUCCESS - The suitable position is return.
3490
3491 --*/
3492 {
3493 EFI_FFS_FILE_HEADER *PadFile;
3494 UINT32 Index;
3495 EFI_STATUS Status;
3496 UINT8 *FixPoint;
3497 UINT32 FileLength;
3498
3499 for (Index = 1; ;Index ++) {
3500 //
3501 // Find Pad File to add ApResetVector info
3502 //
3503 Status = GetFileByType (EFI_FV_FILETYPE_FFS_PAD, Index, &PadFile);
3504 if (EFI_ERROR (Status) || (PadFile == NULL)) {
3505 //
3506 // No Pad file to be found.
3507 //
3508 break;
3509 }
3510 //
3511 // Get Pad file size.
3512 //
3513 FileLength = GetFfsFileLength(PadFile);
3514 FileLength = (FileLength + EFI_FFS_FILE_HEADER_ALIGNMENT - 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT - 1);
3515 //
3516 // FixPoint must be align on 0x1000 relative to FvImage Header
3517 //
3518 FixPoint = (UINT8*) PadFile + GetFfsHeaderLength(PadFile);
3519 FixPoint = FixPoint + 0x1000 - (((UINTN) FixPoint - (UINTN) FvImage->FileImage) & 0xFFF);
3520 //
3521 // FixPoint be larger at the last place of one fv image.
3522 //
3523 while (((UINTN) FixPoint + SIZEOF_STARTUP_DATA_ARRAY - (UINTN) PadFile) <= FileLength) {
3524 FixPoint += 0x1000;
3525 }
3526 FixPoint -= 0x1000;
3527
3528 if ((UINTN) FixPoint < ((UINTN) PadFile + GetFfsHeaderLength(PadFile))) {
3529 //
3530 // No alignment FixPoint in this Pad File.
3531 //
3532 continue;
3533 }
3534
3535 if ((UINTN) FvImage->Eof - (UINTN)FixPoint <= 0x20000) {
3536 //
3537 // Find the position to place ApResetVector
3538 //
3539 *Pointer = FixPoint;
3540 return EFI_SUCCESS;
3541 }
3542 }
3543
3544 return EFI_NOT_FOUND;
3545 }
3546
3547 EFI_STATUS
3548 ParseCapInf (
3549 IN MEMORY_FILE *InfFile,
3550 OUT CAP_INFO *CapInfo
3551 )
3552 /*++
3553
3554 Routine Description:
3555
3556 This function parses a Cap.INF file and copies info into a CAP_INFO structure.
3557
3558 Arguments:
3559
3560 InfFile Memory file image.
3561 CapInfo Information read from INF file.
3562
3563 Returns:
3564
3565 EFI_SUCCESS INF file information successfully retrieved.
3566 EFI_ABORTED INF file has an invalid format.
3567 EFI_NOT_FOUND A required string was not found in the INF file.
3568 --*/
3569 {
3570 CHAR8 Value[MAX_LONG_FILE_PATH];
3571 UINT64 Value64;
3572 UINTN Index, Number;
3573 EFI_STATUS Status;
3574
3575 //
3576 // Initialize Cap info
3577 //
3578 // memset (CapInfo, 0, sizeof (CAP_INFO));
3579 //
3580
3581 //
3582 // Read the Capsule Guid
3583 //
3584 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_CAPSULE_GUID_STRING, 0, Value);
3585 if (Status == EFI_SUCCESS) {
3586 //
3587 // Get the Capsule Guid
3588 //
3589 Status = StringToGuid (Value, &CapInfo->CapGuid);
3590 if (EFI_ERROR (Status)) {
3591 Error (NULL, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_GUID_STRING, Value);
3592 return EFI_ABORTED;
3593 }
3594 DebugMsg (NULL, 0, 9, "Capsule Guid", "%s = %s", EFI_CAPSULE_GUID_STRING, Value);
3595 }
3596
3597 //
3598 // Read the Capsule Header Size
3599 //
3600 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_CAPSULE_HEADER_SIZE_STRING, 0, Value);
3601 if (Status == EFI_SUCCESS) {
3602 Status = AsciiStringToUint64 (Value, FALSE, &Value64);
3603 if (EFI_ERROR (Status)) {
3604 Error (NULL, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING, Value);
3605 return EFI_ABORTED;
3606 }
3607 CapInfo->HeaderSize = (UINT32) Value64;
3608 DebugMsg (NULL, 0, 9, "Capsule Header size", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING, Value);
3609 }
3610
3611 //
3612 // Read the Capsule Flag
3613 //
3614 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_CAPSULE_FLAGS_STRING, 0, Value);
3615 if (Status == EFI_SUCCESS) {
3616 if (strstr (Value, "PopulateSystemTable") != NULL) {
3617 CapInfo->Flags |= CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE;
3618 if (strstr (Value, "InitiateReset") != NULL) {
3619 CapInfo->Flags |= CAPSULE_FLAGS_INITIATE_RESET;
3620 }
3621 } else if (strstr (Value, "PersistAcrossReset") != NULL) {
3622 CapInfo->Flags |= CAPSULE_FLAGS_PERSIST_ACROSS_RESET;
3623 if (strstr (Value, "InitiateReset") != NULL) {
3624 CapInfo->Flags |= CAPSULE_FLAGS_INITIATE_RESET;
3625 }
3626 } else {
3627 Error (NULL, 0, 2000, "Invalid parameter", "invalid Flag setting for %s.", EFI_CAPSULE_FLAGS_STRING);
3628 return EFI_ABORTED;
3629 }
3630 DebugMsg (NULL, 0, 9, "Capsule Flag", Value);
3631 }
3632
3633 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_OEM_CAPSULE_FLAGS_STRING, 0, Value);
3634 if (Status == EFI_SUCCESS) {
3635 Status = AsciiStringToUint64 (Value, FALSE, &Value64);
3636 if (EFI_ERROR (Status) || Value64 > 0xffff) {
3637 Error (NULL, 0, 2000, "Invalid parameter",
3638 "invalid Flag setting for %s. Must be integer value between 0x0000 and 0xffff.",
3639 EFI_OEM_CAPSULE_FLAGS_STRING);
3640 return EFI_ABORTED;
3641 }
3642 CapInfo->Flags |= Value64;
3643 DebugMsg (NULL, 0, 9, "Capsule Extend Flag", Value);
3644 }
3645
3646 //
3647 // Read Capsule File name
3648 //
3649 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_FILE_NAME_STRING, 0, Value);
3650 if (Status == EFI_SUCCESS) {
3651 //
3652 // Get output file name
3653 //
3654 strcpy (CapInfo->CapName, Value);
3655 }
3656
3657 //
3658 // Read the Capsule FileImage
3659 //
3660 Number = 0;
3661 for (Index = 0; Index < MAX_NUMBER_OF_FILES_IN_CAP; Index++) {
3662 if (CapInfo->CapFiles[Index][0] != '\0') {
3663 continue;
3664 }
3665 //
3666 // Read the capsule file name
3667 //
3668 Status = FindToken (InfFile, FILES_SECTION_STRING, EFI_FILE_NAME_STRING, Number++, Value);
3669
3670 if (Status == EFI_SUCCESS) {
3671 //
3672 // Add the file
3673 //
3674 strcpy (CapInfo->CapFiles[Index], Value);
3675 DebugMsg (NULL, 0, 9, "Capsule component file", "the %uth file name is %s", (unsigned) Index, CapInfo->CapFiles[Index]);
3676 } else {
3677 break;
3678 }
3679 }
3680
3681 if (Index == 0) {
3682 Warning (NULL, 0, 0, "Capsule components are not specified.", NULL);
3683 }
3684
3685 return EFI_SUCCESS;
3686 }
3687
3688 EFI_STATUS
3689 GenerateCapImage (
3690 IN CHAR8 *InfFileImage,
3691 IN UINTN InfFileSize,
3692 IN CHAR8 *CapFileName
3693 )
3694 /*++
3695
3696 Routine Description:
3697
3698 This is the main function which will be called from application to create UEFI Capsule image.
3699
3700 Arguments:
3701
3702 InfFileImage Buffer containing the INF file contents.
3703 InfFileSize Size of the contents of the InfFileImage buffer.
3704 CapFileName Requested name for the Cap file.
3705
3706 Returns:
3707
3708 EFI_SUCCESS Function completed successfully.
3709 EFI_OUT_OF_RESOURCES Could not allocate required resources.
3710 EFI_ABORTED Error encountered.
3711 EFI_INVALID_PARAMETER A required parameter was NULL.
3712
3713 --*/
3714 {
3715 UINT32 CapSize;
3716 UINT8 *CapBuffer;
3717 EFI_CAPSULE_HEADER *CapsuleHeader;
3718 MEMORY_FILE InfMemoryFile;
3719 UINT32 FileSize;
3720 UINT32 Index;
3721 FILE *fpin, *fpout;
3722 EFI_STATUS Status;
3723
3724 if (InfFileImage != NULL) {
3725 //
3726 // Initialize file structures
3727 //
3728 InfMemoryFile.FileImage = InfFileImage;
3729 InfMemoryFile.CurrentFilePointer = InfFileImage;
3730 InfMemoryFile.Eof = InfFileImage + InfFileSize;
3731
3732 //
3733 // Parse the Cap inf file for header information
3734 //
3735 Status = ParseCapInf (&InfMemoryFile, &mCapDataInfo);
3736 if (Status != EFI_SUCCESS) {
3737 return Status;
3738 }
3739 }
3740
3741 if (mCapDataInfo.HeaderSize == 0) {
3742 //
3743 // make header size align 16 bytes.
3744 //
3745 mCapDataInfo.HeaderSize = sizeof (EFI_CAPSULE_HEADER);
3746 mCapDataInfo.HeaderSize = (mCapDataInfo.HeaderSize + 0xF) & ~0xF;
3747 }
3748
3749 if (mCapDataInfo.HeaderSize < sizeof (EFI_CAPSULE_HEADER)) {
3750 Error (NULL, 0, 2000, "Invalid parameter", "The specified HeaderSize cannot be less than the size of EFI_CAPSULE_HEADER.");
3751 return EFI_INVALID_PARAMETER;
3752 }
3753
3754 if (CapFileName == NULL && mCapDataInfo.CapName[0] != '\0') {
3755 CapFileName = mCapDataInfo.CapName;
3756 }
3757
3758 if (CapFileName == NULL) {
3759 Error (NULL, 0, 2001, "Missing required argument", "Output Capsule file name");
3760 return EFI_INVALID_PARAMETER;
3761 }
3762
3763 //
3764 // Set Default Capsule Guid value
3765 //
3766 if (CompareGuid (&mCapDataInfo.CapGuid, &mZeroGuid) == 0) {
3767 memcpy (&mCapDataInfo.CapGuid, &mDefaultCapsuleGuid, sizeof (EFI_GUID));
3768 }
3769 //
3770 // Calculate the size of capsule image.
3771 //
3772 Index = 0;
3773 FileSize = 0;
3774 CapSize = mCapDataInfo.HeaderSize;
3775 while (mCapDataInfo.CapFiles [Index][0] != '\0') {
3776 fpin = fopen (LongFilePath (mCapDataInfo.CapFiles[Index]), "rb");
3777 if (fpin == NULL) {
3778 Error (NULL, 0, 0001, "Error opening file", mCapDataInfo.CapFiles[Index]);
3779 return EFI_ABORTED;
3780 }
3781 FileSize = _filelength (fileno (fpin));
3782 CapSize += FileSize;
3783 fclose (fpin);
3784 Index ++;
3785 }
3786
3787 //
3788 // Allocate buffer for capsule image.
3789 //
3790 CapBuffer = (UINT8 *) malloc (CapSize);
3791 if (CapBuffer == NULL) {
3792 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated for creating the capsule.");
3793 return EFI_OUT_OF_RESOURCES;
3794 }
3795
3796 //
3797 // Initialize the capsule header to zero
3798 //
3799 memset (CapBuffer, 0, mCapDataInfo.HeaderSize);
3800
3801 //
3802 // create capsule header and get capsule body
3803 //
3804 CapsuleHeader = (EFI_CAPSULE_HEADER *) CapBuffer;
3805 memcpy (&CapsuleHeader->CapsuleGuid, &mCapDataInfo.CapGuid, sizeof (EFI_GUID));
3806 CapsuleHeader->HeaderSize = mCapDataInfo.HeaderSize;
3807 CapsuleHeader->Flags = mCapDataInfo.Flags;
3808 CapsuleHeader->CapsuleImageSize = CapSize;
3809
3810 Index = 0;
3811 FileSize = 0;
3812 CapSize = CapsuleHeader->HeaderSize;
3813 while (mCapDataInfo.CapFiles [Index][0] != '\0') {
3814 fpin = fopen (LongFilePath (mCapDataInfo.CapFiles[Index]), "rb");
3815 if (fpin == NULL) {
3816 Error (NULL, 0, 0001, "Error opening file", mCapDataInfo.CapFiles[Index]);
3817 free (CapBuffer);
3818 return EFI_ABORTED;
3819 }
3820 FileSize = _filelength (fileno (fpin));
3821 fread (CapBuffer + CapSize, 1, FileSize, fpin);
3822 fclose (fpin);
3823 Index ++;
3824 CapSize += FileSize;
3825 }
3826
3827 //
3828 // write capsule data into the output file
3829 //
3830 fpout = fopen (LongFilePath (CapFileName), "wb");
3831 if (fpout == NULL) {
3832 Error (NULL, 0, 0001, "Error opening file", CapFileName);
3833 free (CapBuffer);
3834 return EFI_ABORTED;
3835 }
3836
3837 fwrite (CapBuffer, 1, CapSize, fpout);
3838 fclose (fpout);
3839
3840 VerboseMsg ("The size of the generated capsule image is %u bytes", (unsigned) CapSize);
3841
3842 return EFI_SUCCESS;
3843 }