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