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