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