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