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