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