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