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