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