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