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