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