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