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