]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/CCode/Source/GenFvImage/GenFvImageLib.c
Enhance DxeIpl module to support EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE ffs type,...
[mirror_edk2.git] / Tools / CCode / Source / GenFvImage / GenFvImageLib.c
CommitLineData
878ddf1f 1/*++\r
2i\r
3\r
4Copyright (c) 2004, Intel Corporation \r
5All rights reserved. This program and the accompanying materials \r
6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php \r
9 \r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12\r
13Module Name:\r
14\r
15 GenFvImageLib.c\r
16\r
17Abstract:\r
18\r
19 This file contains functions required to generate a Firmware Volume.\r
20\r
21--*/\r
22\r
23//\r
24// Include files\r
25//\r
878ddf1f 26#ifdef __GNUC__\r
27#include <uuid/uuid.h>\r
28#include <sys/stat.h>\r
29#endif\r
30#include <string.h>\r
31#ifndef __GNUC__\r
32#include <io.h>\r
33#endif\r
34#include <assert.h>\r
ce53a8c3 35\r
36#include <Common/UefiBaseTypes.h>\r
37#include <Common/FirmwareVolumeImageFormat.h>\r
38#include <Common/Variable.h>\r
39#include <Common/WorkingBlockHeader.h>\r
40#include <Guid/FirmwareFileSystem.h>\r
41\r
878ddf1f 42#include "GenFvImageLib.h"\r
43#include "GenFvImageLibInternal.h"\r
ce53a8c3 44#include "CommonLib.h"\r
45#include "Crc32.h"\r
46#include "EfiUtilityMsgs.h"\r
47#include "FvLib.h"\r
48#include "EfiCompress.h"\r
49#include "WinNtInclude.h"\r
878ddf1f 50\r
51\r
52//\r
53// Local function prototypes\r
54//\r
55EFI_STATUS\r
56GetPe32Info (\r
57 IN UINT8 *Pe32,\r
58 OUT UINT32 *EntryPoint,\r
59 OUT UINT32 *BaseOfCode,\r
60 OUT UINT16 *MachineType\r
61 );\r
62\r
63//\r
64// Local function implementations.\r
65//\r
66EFI_GUID FfsGuid = EFI_FIRMWARE_FILE_SYSTEM_GUID;\r
67EFI_GUID DefaultFvPadFileNameGuid = { 0x78f54d4, 0xcc22, 0x4048, 0x9e, 0x94, 0x87, 0x9c, 0x21, 0x4d, 0x56, 0x2f };\r
68\r
69//\r
70// This data array will be located at the base of the Firmware Volume Header (FVH)\r
71// in the boot block. It must not exceed 14 bytes of code. The last 2 bytes\r
72// will be used to keep the FVH checksum consistent.\r
73// This code will be run in response to a starutp IPI for HT-enabled systems.\r
74//\r
75#define SIZEOF_STARTUP_DATA_ARRAY 0x10\r
76\r
77UINT8 m128kRecoveryStartupApDataArray[SIZEOF_STARTUP_DATA_ARRAY] = {\r
78 //\r
79 // EA D0 FF 00 F0 ; far jmp F000:FFD0\r
80 // 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes\r
81 // 0, 0 ; Checksum Padding\r
82 //\r
83 0xEA,\r
84 0xD0,\r
85 0xFF,\r
86 0x0,\r
87 0xF0,\r
88 0x00,\r
89 0x00,\r
90 0x00,\r
91 0x00,\r
92 0x00,\r
93 0x00,\r
94 0x00,\r
95 0x00,\r
96 0x00,\r
97 0x00,\r
98 0x00\r
99};\r
100\r
101UINT8 m64kRecoveryStartupApDataArray[SIZEOF_STARTUP_DATA_ARRAY] = {\r
102 //\r
103 // EB CE ; jmp short ($-0x30)\r
104 // ; (from offset 0x0 to offset 0xFFD0)\r
105 // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes\r
106 // 0, 0 ; Checksum Padding\r
107 //\r
108 0xEB,\r
109 0xCE,\r
110 0x00,\r
111 0x00,\r
112 0x00,\r
113 0x00,\r
114 0x00,\r
115 0x00,\r
116 0x00,\r
117 0x00,\r
118 0x00,\r
119 0x00,\r
120 0x00,\r
121 0x00,\r
122 0x00,\r
123 0x00\r
124};\r
125\r
126EFI_STATUS\r
127ParseFvInf (\r
128 IN MEMORY_FILE *InfFile,\r
129 IN FV_INFO *FvInfo\r
130 )\r
131/*++\r
132\r
133Routine Description:\r
134\r
135 This function parses a FV.INF file and copies info into a FV_INFO structure.\r
136\r
137Arguments:\r
138\r
139 InfFile Memory file image.\r
140 FvInfo Information read from INF file.\r
141\r
142Returns:\r
143\r
144 EFI_SUCCESS INF file information successfully retrieved.\r
145 EFI_ABORTED INF file has an invalid format.\r
146 EFI_NOT_FOUND A required string was not found in the INF file.\r
147--*/\r
148{\r
149 CHAR8 Value[_MAX_PATH];\r
150 UINT64 Value64;\r
151 UINTN Index;\r
152 EFI_STATUS Status;\r
153\r
154 //\r
155 // Initialize FV info\r
156 //\r
157 memset (FvInfo, 0, sizeof (FV_INFO));\r
158\r
159 //\r
160 // Read the FV base address\r
161 //\r
162 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_FV_BASE_ADDRESS_STRING, 0, Value);\r
163\r
164 if (Status == EFI_SUCCESS) {\r
165 //\r
166 // Get the base address\r
167 //\r
168 Status = AsciiStringToUint64 (Value, FALSE, &Value64);\r
169 if (EFI_ERROR (Status)) {\r
170 Error (NULL, 0, 0, EFI_FV_BASE_ADDRESS_STRING, "invalid value");\r
171 return EFI_ABORTED;\r
172 }\r
173\r
174 FvInfo->BaseAddress = Value64;\r
175 } else {\r
176 Error (NULL, 0, 0, EFI_FV_BASE_ADDRESS_STRING, "could not find value");\r
177 return EFI_ABORTED;\r
178 }\r
179 //\r
180 // Read the FV Guid\r
181 //\r
182 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_FV_GUID_STRING, 0, Value);\r
183\r
184 if (Status == EFI_SUCCESS) {\r
185 //\r
186 // Get the guid value\r
187 //\r
188 Status = StringToGuid (Value, &FvInfo->FvGuid);\r
189 if (EFI_ERROR (Status)) {\r
190 memcpy (&FvInfo->FvGuid, &FfsGuid, sizeof (EFI_GUID));\r
191 }\r
192 } else {\r
193 memcpy (&FvInfo->FvGuid, &FfsGuid, sizeof (EFI_GUID));\r
194 }\r
195 //\r
196 // Read the FV file name\r
197 //\r
198 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_FV_FILE_NAME_STRING, 0, Value);\r
199\r
200 if (Status == EFI_SUCCESS) {\r
201 //\r
202 // copy the file name\r
203 //\r
204 strcpy (FvInfo->FvName, Value);\r
205 } else {\r
206 Error (NULL, 0, 0, EFI_FV_FILE_NAME_STRING, "value not specified");\r
207 return EFI_ABORTED;\r
208 }\r
209 //\r
210 // Read the Sym file name\r
211 //\r
212 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_SYM_FILE_NAME_STRING, 0, Value);\r
213\r
214 if (Status == EFI_SUCCESS) {\r
215 //\r
216 // copy the file name\r
217 //\r
218 strcpy (FvInfo->SymName, Value);\r
219 } else {\r
220 //\r
221 // Symbols not required, so init to NULL.\r
222 //\r
223 strcpy (FvInfo->SymName, "");\r
224 }\r
225 //\r
226 // Read the read disabled capability attribute\r
227 //\r
228 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_READ_DISABLED_CAP_STRING, 0, Value);\r
229\r
230 if (Status == EFI_SUCCESS) {\r
231 //\r
232 // Update the read disabled flag\r
233 //\r
234 if (strcmp (Value, TRUE_STRING) == 0) {\r
235 FvInfo->FvAttributes |= EFI_FVB_READ_DISABLED_CAP;\r
236 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
237 Error (NULL, 0, 0, EFI_FVB_READ_DISABLED_CAP_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
238 return EFI_ABORTED;\r
239 }\r
240 } else {\r
241 Error (NULL, 0, 0, EFI_FVB_READ_DISABLED_CAP_STRING, "value not specified");\r
242 return Status;\r
243 }\r
244 //\r
245 // Read the read enabled capability attribute\r
246 //\r
247 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_READ_ENABLED_CAP_STRING, 0, Value);\r
248\r
249 if (Status == EFI_SUCCESS) {\r
250 //\r
251 // Update the read disabled flag\r
252 //\r
253 if (strcmp (Value, TRUE_STRING) == 0) {\r
254 FvInfo->FvAttributes |= EFI_FVB_READ_ENABLED_CAP;\r
255 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
256 Error (NULL, 0, 0, EFI_FVB_READ_ENABLED_CAP_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
257 return EFI_ABORTED;\r
258 }\r
259 } else {\r
260 Error (NULL, 0, 0, EFI_FVB_READ_ENABLED_CAP_STRING, "value not specified");\r
261 return Status;\r
262 }\r
263 //\r
264 // Read the read status attribute\r
265 //\r
266 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_READ_STATUS_STRING, 0, Value);\r
267\r
268 if (Status == EFI_SUCCESS) {\r
269 //\r
270 // Update the read disabled flag\r
271 //\r
272 if (strcmp (Value, TRUE_STRING) == 0) {\r
273 FvInfo->FvAttributes |= EFI_FVB_READ_STATUS;\r
274 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
275 Error (NULL, 0, 0, EFI_FVB_READ_STATUS_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
276 return EFI_ABORTED;\r
277 }\r
278 } else {\r
279 Error (NULL, 0, 0, EFI_FVB_READ_STATUS_STRING, "value not specified");\r
280 return Status;\r
281 }\r
282 //\r
283 // Read the write disabled capability attribute\r
284 //\r
285 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_WRITE_DISABLED_CAP_STRING, 0, Value);\r
286\r
287 if (Status == EFI_SUCCESS) {\r
288 //\r
289 // Update the write disabled flag\r
290 //\r
291 if (strcmp (Value, TRUE_STRING) == 0) {\r
292 FvInfo->FvAttributes |= EFI_FVB_WRITE_DISABLED_CAP;\r
293 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
294 Error (NULL, 0, 0, EFI_FVB_WRITE_DISABLED_CAP_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
295 return EFI_ABORTED;\r
296 }\r
297 } else {\r
298 Error (NULL, 0, 0, EFI_FVB_WRITE_DISABLED_CAP_STRING, "value not specified");\r
299 return Status;\r
300 }\r
301 //\r
302 // Read the write enabled capability attribute\r
303 //\r
304 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_WRITE_ENABLED_CAP_STRING, 0, Value);\r
305\r
306 if (Status == EFI_SUCCESS) {\r
307 //\r
308 // Update the write disabled flag\r
309 //\r
310 if (strcmp (Value, TRUE_STRING) == 0) {\r
311 FvInfo->FvAttributes |= EFI_FVB_WRITE_ENABLED_CAP;\r
312 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
313 Error (NULL, 0, 0, EFI_FVB_WRITE_ENABLED_CAP_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
314 return EFI_ABORTED;\r
315 }\r
316 } else {\r
317 Error (NULL, 0, 0, EFI_FVB_WRITE_ENABLED_CAP_STRING, "value not specified");\r
318 return Status;\r
319 }\r
320 //\r
321 // Read the write status attribute\r
322 //\r
323 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_WRITE_STATUS_STRING, 0, Value);\r
324\r
325 if (Status == EFI_SUCCESS) {\r
326 //\r
327 // Update the write disabled flag\r
328 //\r
329 if (strcmp (Value, TRUE_STRING) == 0) {\r
330 FvInfo->FvAttributes |= EFI_FVB_WRITE_STATUS;\r
331 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
332 Error (NULL, 0, 0, EFI_FVB_WRITE_STATUS_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
333 return EFI_ABORTED;\r
334 }\r
335 } else {\r
336 Error (NULL, 0, 0, EFI_FVB_WRITE_STATUS_STRING, "value not specified");\r
337 return Status;\r
338 }\r
339 //\r
340 // Read the lock capability attribute\r
341 //\r
342 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_LOCK_CAP_STRING, 0, Value);\r
343\r
344 if (Status == EFI_SUCCESS) {\r
345 //\r
346 // Update the attribute flag\r
347 //\r
348 if (strcmp (Value, TRUE_STRING) == 0) {\r
349 FvInfo->FvAttributes |= EFI_FVB_LOCK_CAP;\r
350 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
351 Error (NULL, 0, 0, EFI_FVB_LOCK_CAP_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
352 return EFI_ABORTED;\r
353 }\r
354 } else {\r
355 Error (NULL, 0, 0, EFI_FVB_LOCK_CAP_STRING, "value not specified");\r
356 return Status;\r
357 }\r
358 //\r
359 // Read the lock status attribute\r
360 //\r
361 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_LOCK_STATUS_STRING, 0, Value);\r
362\r
363 if (Status == EFI_SUCCESS) {\r
364 //\r
365 // Update the attribute flag\r
366 //\r
367 if (strcmp (Value, TRUE_STRING) == 0) {\r
368 FvInfo->FvAttributes |= EFI_FVB_LOCK_STATUS;\r
369 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
370 Error (NULL, 0, 0, EFI_FVB_LOCK_STATUS_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
371 return EFI_ABORTED;\r
372 }\r
373 } else {\r
374 Error (NULL, 0, 0, EFI_FVB_LOCK_STATUS_STRING, "value not specified");\r
375 return Status;\r
376 }\r
377 //\r
378 // Read the sticky write attribute\r
379 //\r
380 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_STICKY_WRITE_STRING, 0, Value);\r
381\r
382 if (Status == EFI_SUCCESS) {\r
383 //\r
384 // Update the attribute flag\r
385 //\r
386 if (strcmp (Value, TRUE_STRING) == 0) {\r
387 FvInfo->FvAttributes |= EFI_FVB_STICKY_WRITE;\r
388 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
389 Error (NULL, 0, 0, EFI_FVB_STICKY_WRITE_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
390 return EFI_ABORTED;\r
391 }\r
392 } else {\r
393 Error (NULL, 0, 0, EFI_FVB_STICKY_WRITE_STRING, "value not specified");\r
394 return Status;\r
395 }\r
396 //\r
397 // Read the memory mapped attribute\r
398 //\r
399 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_MEMORY_MAPPED_STRING, 0, Value);\r
400\r
401 if (Status == EFI_SUCCESS) {\r
402 //\r
403 // Update the attribute flag\r
404 //\r
405 if (strcmp (Value, TRUE_STRING) == 0) {\r
406 FvInfo->FvAttributes |= EFI_FVB_MEMORY_MAPPED;\r
407 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
408 Error (NULL, 0, 0, EFI_FVB_MEMORY_MAPPED_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
409 return EFI_ABORTED;\r
410 }\r
411 } else {\r
412 Error (NULL, 0, 0, EFI_FVB_MEMORY_MAPPED_STRING, "value not specified");\r
413 return Status;\r
414 }\r
415 //\r
416 // Read the erase polarity attribute\r
417 //\r
418 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ERASE_POLARITY_STRING, 0, Value);\r
419\r
420 if (Status == EFI_SUCCESS) {\r
421 //\r
422 // Update the attribute flag\r
423 //\r
424 if (strcmp (Value, ONE_STRING) == 0) {\r
425 FvInfo->FvAttributes |= EFI_FVB_ERASE_POLARITY;\r
426 } else if (strcmp (Value, ZERO_STRING) != 0) {\r
427 Error (NULL, 0, 0, EFI_FVB_ERASE_POLARITY_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
428 return EFI_ABORTED;\r
429 }\r
430 } else {\r
431 Error (NULL, 0, 0, EFI_FVB_ERASE_POLARITY_STRING, "value not specified");\r
432 return Status;\r
433 }\r
434 //\r
435 // Read the alignment capabilities attribute\r
436 //\r
437 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_CAP_STRING, 0, Value);\r
438\r
439 if (Status == EFI_SUCCESS) {\r
440 //\r
441 // Update attribute\r
442 //\r
443 if (strcmp (Value, TRUE_STRING) == 0) {\r
444 FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_CAP;\r
445 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
446 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_CAP_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
447 return EFI_ABORTED;\r
448 }\r
449 } else {\r
450 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_CAP_STRING, "value not specified");\r
451 return Status;\r
452 }\r
453 //\r
454 // Read the word alignment capability attribute\r
455 //\r
456 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_2_STRING, 0, Value);\r
457\r
458 if (Status == EFI_SUCCESS) {\r
459 //\r
460 // Update attribute\r
461 //\r
462 if (strcmp (Value, TRUE_STRING) == 0) {\r
463 FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_2;\r
464 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
465 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_2_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
466 return EFI_ABORTED;\r
467 }\r
468 } else {\r
469 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_2_STRING, "value not specified");\r
470 return Status;\r
471 }\r
472 //\r
473 // Read the dword alignment capability attribute\r
474 //\r
475 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_4_STRING, 0, Value);\r
476\r
477 if (Status == EFI_SUCCESS) {\r
478 //\r
479 // Update attribute\r
480 //\r
481 if (strcmp (Value, TRUE_STRING) == 0) {\r
482 FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_4;\r
483 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
484 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_4_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
485 return EFI_ABORTED;\r
486 }\r
487 } else {\r
488 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_4_STRING, "value not specified");\r
489 return Status;\r
490 }\r
491 //\r
492 // Read the word alignment capability attribute\r
493 //\r
494 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_8_STRING, 0, Value);\r
495\r
496 if (Status == EFI_SUCCESS) {\r
497 //\r
498 // Update attribute\r
499 //\r
500 if (strcmp (Value, TRUE_STRING) == 0) {\r
501 FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_8;\r
502 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
503 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_8_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
504 return EFI_ABORTED;\r
505 }\r
506 } else {\r
507 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_8_STRING, "value not specified");\r
508 return Status;\r
509 }\r
510 //\r
511 // Read the qword alignment capability attribute\r
512 //\r
513 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_16_STRING, 0, Value);\r
514\r
515 if (Status == EFI_SUCCESS) {\r
516 //\r
517 // Update attribute\r
518 //\r
519 if (strcmp (Value, TRUE_STRING) == 0) {\r
520 FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_16;\r
521 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
522 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_16_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
523 return EFI_ABORTED;\r
524 }\r
525 } else {\r
526 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_16_STRING, "value not specified");\r
527 return Status;\r
528 }\r
529 //\r
530 // Read the 32 byte alignment capability attribute\r
531 //\r
532 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_32_STRING, 0, Value);\r
533\r
534 if (Status == EFI_SUCCESS) {\r
535 //\r
536 // Update attribute\r
537 //\r
538 if (strcmp (Value, TRUE_STRING) == 0) {\r
539 FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_32;\r
540 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
541 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_32_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
542 return EFI_ABORTED;\r
543 }\r
544 } else {\r
545 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_32_STRING, "value not specified");\r
546 return Status;\r
547 }\r
548 //\r
549 // Read the 64 byte alignment capability attribute\r
550 //\r
551 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_64_STRING, 0, Value);\r
552\r
553 if (Status == EFI_SUCCESS) {\r
554 //\r
555 // Update attribute\r
556 //\r
557 if (strcmp (Value, TRUE_STRING) == 0) {\r
558 FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_64;\r
559 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
560 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_64_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
561 return EFI_ABORTED;\r
562 }\r
563 } else {\r
564 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_64_STRING, "value not specified");\r
565 return Status;\r
566 }\r
567 //\r
568 // Read the 128 byte alignment capability attribute\r
569 //\r
570 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_128_STRING, 0, Value);\r
571\r
572 if (Status == EFI_SUCCESS) {\r
573 //\r
574 // Update attribute\r
575 //\r
576 if (strcmp (Value, TRUE_STRING) == 0) {\r
577 FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_128;\r
578 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
579 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_128_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
580 return EFI_ABORTED;\r
581 }\r
582 } else {\r
583 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_128_STRING, "value not specified");\r
584 return Status;\r
585 }\r
586 //\r
587 // Read the 256 byte alignment capability attribute\r
588 //\r
589 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_256_STRING, 0, Value);\r
590\r
591 if (Status == EFI_SUCCESS) {\r
592 //\r
593 // Update attribute\r
594 //\r
595 if (strcmp (Value, TRUE_STRING) == 0) {\r
596 FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_256;\r
597 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
598 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_256_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
599 return EFI_ABORTED;\r
600 }\r
601 } else {\r
602 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_256_STRING, "value not specified");\r
603 return Status;\r
604 }\r
605 //\r
606 // Read the 512 byte alignment capability attribute\r
607 //\r
608 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_512_STRING, 0, Value);\r
609\r
610 if (Status == EFI_SUCCESS) {\r
611 //\r
612 // Update attribute\r
613 //\r
614 if (strcmp (Value, TRUE_STRING) == 0) {\r
615 FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_512;\r
616 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
617 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_512_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
618 return EFI_ABORTED;\r
619 }\r
620 } else {\r
621 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_512_STRING, "value not specified");\r
622 return Status;\r
623 }\r
624 //\r
625 // Read the 1K byte alignment capability attribute\r
626 //\r
627 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_1K_STRING, 0, Value);\r
628\r
629 if (Status == EFI_SUCCESS) {\r
630 //\r
631 // Update attribute\r
632 //\r
633 if (strcmp (Value, TRUE_STRING) == 0) {\r
634 FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_1K;\r
635 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
636 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_1K_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
637 return EFI_ABORTED;\r
638 }\r
639 } else {\r
640 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_1K_STRING, "value not specified");\r
641 return Status;\r
642 }\r
643 //\r
644 // Read the 2K byte alignment capability attribute\r
645 //\r
646 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_2K_STRING, 0, Value);\r
647\r
648 if (Status == EFI_SUCCESS) {\r
649 //\r
650 // Update attribute\r
651 //\r
652 if (strcmp (Value, TRUE_STRING) == 0) {\r
653 FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_2K;\r
654 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
655 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_2K_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
656 return EFI_ABORTED;\r
657 }\r
658 } else {\r
659 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_2K_STRING, "value not specified");\r
660 return Status;\r
661 }\r
662 //\r
663 // Read the 4K byte alignment capability attribute\r
664 //\r
665 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_4K_STRING, 0, Value);\r
666\r
667 if (Status == EFI_SUCCESS) {\r
668 //\r
669 // Update attribute\r
670 //\r
671 if (strcmp (Value, TRUE_STRING) == 0) {\r
672 FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_4K;\r
673 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
674 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_4K_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
675 return EFI_ABORTED;\r
676 }\r
677 } else {\r
678 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_4K_STRING, "value not specified");\r
679 return Status;\r
680 }\r
681 //\r
682 // Read the 8K byte alignment capability attribute\r
683 //\r
684 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_8K_STRING, 0, Value);\r
685\r
686 if (Status == EFI_SUCCESS) {\r
687 //\r
688 // Update attribute\r
689 //\r
690 if (strcmp (Value, TRUE_STRING) == 0) {\r
691 FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_8K;\r
692 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
693 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_8K_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
694 return EFI_ABORTED;\r
695 }\r
696 } else {\r
697 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_8K_STRING, "value not specified");\r
698 return Status;\r
699 }\r
700 //\r
701 // Read the 16K byte alignment capability attribute\r
702 //\r
703 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_16K_STRING, 0, Value);\r
704\r
705 if (Status == EFI_SUCCESS) {\r
706 //\r
707 // Update attribute\r
708 //\r
709 if (strcmp (Value, TRUE_STRING) == 0) {\r
710 FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_16K;\r
711 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
712 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_16K_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
713 return EFI_ABORTED;\r
714 }\r
715 } else {\r
716 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_16K_STRING, "value not specified");\r
717 return Status;\r
718 }\r
719 //\r
720 // Read the 32K byte alignment capability attribute\r
721 //\r
722 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_32K_STRING, 0, Value);\r
723\r
724 if (Status == EFI_SUCCESS) {\r
725 //\r
726 // Update attribute\r
727 //\r
728 if (strcmp (Value, TRUE_STRING) == 0) {\r
729 FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_32K;\r
730 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
731 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_32K_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
732 return EFI_ABORTED;\r
733 }\r
734 } else {\r
735 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_32K_STRING, "value not specified");\r
736 return Status;\r
737 }\r
738 //\r
739 // Read the 64K byte alignment capability attribute\r
740 //\r
741 Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_64K_STRING, 0, Value);\r
742\r
743 if (Status == EFI_SUCCESS) {\r
744 //\r
745 // Update attribute\r
746 //\r
747 if (strcmp (Value, TRUE_STRING) == 0) {\r
748 FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_64K;\r
749 } else if (strcmp (Value, FALSE_STRING) != 0) {\r
750 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_64K_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);\r
751 return EFI_ABORTED;\r
752 }\r
753 } else {\r
754 Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_64K_STRING, "value not specified");\r
755 return Status;\r
756 }\r
757\r
758 if (!(FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_CAP) &&\r
759 (\r
760 (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_2) ||\r
761 (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_4) ||\r
762 (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_8) ||\r
763 (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_16) ||\r
764 (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_32) ||\r
765 (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_64) ||\r
766 (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_128) ||\r
767 (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_256) ||\r
768 (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_512) ||\r
769 (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_1K) ||\r
770 (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_2K) ||\r
771 (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_4K) ||\r
772 (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_8K) ||\r
773 (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_16K) ||\r
774 (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_32K) ||\r
775 (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_64K)\r
776 )\r
777 ) {\r
778 Error (\r
779 NULL,\r
780 0,\r
781 0,\r
782 "illegal combination of alignment attributes",\r
783 "if %s is not %s, no individual alignments can be %s",\r
784 EFI_FVB_ALIGNMENT_CAP_STRING,\r
785 TRUE_STRING,\r
786 TRUE_STRING\r
787 );\r
788 return EFI_ABORTED;\r
789 }\r
790 //\r
791 // Read block maps\r
792 //\r
793 for (Index = 0; Index < MAX_NUMBER_OF_FV_BLOCKS; Index++) {\r
794 //\r
795 // Read the number of blocks\r
796 //\r
797 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_NUM_BLOCKS_STRING, Index, Value);\r
798\r
799 if (Status == EFI_SUCCESS) {\r
800 //\r
801 // Update the number of blocks\r
802 //\r
803 Status = AsciiStringToUint64 (Value, FALSE, &Value64);\r
804 if (EFI_ERROR (Status)) {\r
805 Error (NULL, 0, 0, Value, "invalid value for %s", EFI_NUM_BLOCKS_STRING);\r
806 return EFI_ABORTED;\r
807 }\r
808\r
809 FvInfo->FvBlocks[Index].NumBlocks = (UINT32) Value64;\r
810 } else {\r
811 //\r
812 // If there is no number of blocks, but there is a size, then we have a mismatched pair\r
813 // and should return an error.\r
814 //\r
815 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_BLOCK_SIZE_STRING, Index, Value);\r
816 if (!EFI_ERROR (Status)) {\r
817 Error (NULL, 0, 0, "must specify both", "%s and %s", EFI_NUM_BLOCKS_STRING, EFI_BLOCK_SIZE_STRING);\r
818 return EFI_ABORTED;\r
819 } else {\r
820 //\r
821 // We are done\r
822 //\r
823 break;\r
824 }\r
825 }\r
826 //\r
827 // Read the size of blocks\r
828 //\r
829 Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_BLOCK_SIZE_STRING, Index, Value);\r
830\r
831 if (Status == EFI_SUCCESS) {\r
832 //\r
833 // Update the number of blocks\r
834 //\r
835 Status = AsciiStringToUint64 (Value, FALSE, &Value64);\r
836 if (EFI_ERROR (Status)) {\r
837 Error (NULL, 0, 0, Value, "invalid value specified for %s", EFI_BLOCK_SIZE_STRING);\r
838 return EFI_ABORTED;\r
839 }\r
840\r
841 FvInfo->FvBlocks[Index].BlockLength = (UINT32) Value64;\r
842 } else {\r
843 //\r
844 // There is a number of blocks, but there is no size, so we have a mismatched pair\r
845 // and should return an error.\r
846 //\r
847 Error (NULL, 0, 0, "must specify both", "%s and %s", EFI_NUM_BLOCKS_STRING, EFI_BLOCK_SIZE_STRING);\r
848 return EFI_ABORTED;\r
849 }\r
850 }\r
851 //\r
852 // Read files\r
853 //\r
854 for (Index = 0; Index < MAX_NUMBER_OF_FILES_IN_FV; Index++) {\r
855 //\r
856 // Read the number of blocks\r
857 //\r
858 Status = FindToken (InfFile, FILES_SECTION_STRING, EFI_FILE_NAME_STRING, Index, Value);\r
859\r
860 if (Status == EFI_SUCCESS) {\r
861 //\r
862 // Add the file\r
863 //\r
864 strcpy (FvInfo->FvFiles[Index], Value);\r
865 } else {\r
866 break;\r
867 }\r
868 }\r
869\r
870 if (FindSection (InfFile, COMPONENT_SECTION_STRING)) {\r
871 Index = 0;\r
872 //\r
873 // Read component FV_VARIABLE\r
874 //\r
875 Status = FindToken (InfFile, COMPONENT_SECTION_STRING, EFI_NV_VARIABLE_STRING, 0, Value);\r
876\r
877 if (Status == EFI_SUCCESS) {\r
878 //\r
879 // Add the component\r
880 //\r
881 strcpy (FvInfo->FvComponents[Index].ComponentName, EFI_NV_VARIABLE_STRING);\r
882 Status = AsciiStringToUint64 (Value, FALSE, &Value64);\r
883 if (EFI_ERROR (Status)) {\r
884 printf ("ERROR: %s is not a valid integer.\n", EFI_NV_VARIABLE_STRING);\r
885 return EFI_ABORTED;\r
886 }\r
887\r
888 FvInfo->FvComponents[Index].Size = (UINTN) Value64;\r
889 } else {\r
890 printf ("WARNING: Could not read %s.\n", EFI_NV_VARIABLE_STRING);\r
891 }\r
892\r
893 Index++;\r
894 //\r
895 // Read component FV_EVENT_LOG\r
896 //\r
897 Status = FindToken (InfFile, COMPONENT_SECTION_STRING, EFI_NV_EVENT_LOG_STRING, 0, Value);\r
898\r
899 if (Status == EFI_SUCCESS) {\r
900 //\r
901 // Add the component\r
902 //\r
903 strcpy (FvInfo->FvComponents[Index].ComponentName, EFI_NV_EVENT_LOG_STRING);\r
904 Status = AsciiStringToUint64 (Value, FALSE, &Value64);\r
905 if (EFI_ERROR (Status)) {\r
906 printf ("ERROR: %s is not a valid integer.\n", EFI_NV_EVENT_LOG_STRING);\r
907 return EFI_ABORTED;\r
908 }\r
909\r
910 FvInfo->FvComponents[Index].Size = (UINTN) Value64;\r
911 } else {\r
912 printf ("WARNING: Could not read %s.\n", EFI_NV_EVENT_LOG_STRING);\r
913 }\r
914\r
915 Index++;\r
916 //\r
917 // Read component FV_FTW_WORKING\r
918 //\r
919 Status = FindToken (InfFile, COMPONENT_SECTION_STRING, EFI_NV_FTW_WORKING_STRING, 0, Value);\r
920\r
921 if (Status == EFI_SUCCESS) {\r
922 //\r
923 // Add the component\r
924 //\r
925 strcpy (FvInfo->FvComponents[Index].ComponentName, EFI_NV_FTW_WORKING_STRING);\r
926 Status = AsciiStringToUint64 (Value, FALSE, &Value64);\r
927 if (EFI_ERROR (Status)) {\r
928 printf ("ERROR: %s is not a valid integer.\n", EFI_NV_FTW_WORKING_STRING);\r
929 return EFI_ABORTED;\r
930 }\r
931\r
932 FvInfo->FvComponents[Index].Size = (UINTN) Value64;\r
933 } else {\r
934 printf ("WARNING: Could not read %s.\n", EFI_NV_FTW_WORKING_STRING);\r
935 }\r
936\r
937 Index++;\r
938 //\r
939 // Read component FV_FTW_SPARE\r
940 //\r
941 Status = FindToken (InfFile, COMPONENT_SECTION_STRING, EFI_NV_FTW_SPARE_STRING, 0, Value);\r
942\r
943 if (Status == EFI_SUCCESS) {\r
944 //\r
945 // Add the component\r
946 //\r
947 strcpy (FvInfo->FvComponents[Index].ComponentName, EFI_NV_FTW_SPARE_STRING);\r
948 Status = AsciiStringToUint64 (Value, FALSE, &Value64);\r
949 if (EFI_ERROR (Status)) {\r
950 printf ("ERROR: %s is not a valid integer.\n", EFI_NV_FTW_SPARE_STRING);\r
951 return EFI_ABORTED;\r
952 }\r
953\r
954 FvInfo->FvComponents[Index].Size = (UINTN) Value64;\r
955 } else {\r
956 printf ("WARNING: Could not read %s.\n", EFI_NV_FTW_SPARE_STRING);\r
957 }\r
958 }\r
959 //\r
960 // Compute size for easy access later\r
961 //\r
962 FvInfo->Size = 0;\r
963 for (Index = 0; FvInfo->FvBlocks[Index].NumBlocks; Index++) {\r
964 FvInfo->Size += FvInfo->FvBlocks[Index].NumBlocks * FvInfo->FvBlocks[Index].BlockLength;\r
965 }\r
966\r
967 return EFI_SUCCESS;\r
968}\r
969\r
970VOID\r
971UpdateFfsFileState (\r
972 IN EFI_FFS_FILE_HEADER *FfsFile,\r
973 IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader\r
974 )\r
975/*++\r
976\r
977Routine Description:\r
978\r
979 This function changes the FFS file attributes based on the erase polarity\r
980 of the FV.\r
981\r
982Arguments:\r
983\r
984 FfsFile File header.\r
985 FvHeader FV header.\r
986\r
987Returns:\r
988\r
989 None\r
990\r
991--*/\r
992{\r
993 if (FvHeader->Attributes & EFI_FVB_ERASE_POLARITY) {\r
994 FfsFile->State = (UINT8)~(FfsFile->State);\r
995 }\r
996}\r
997\r
998EFI_STATUS\r
999ReadFfsAlignment (\r
1000 IN EFI_FFS_FILE_HEADER *FfsFile,\r
1001 IN OUT UINT32 *Alignment\r
1002 )\r
1003/*++\r
1004\r
1005Routine Description:\r
1006\r
1007 This function determines the alignment of the FFS input file from the file\r
1008 attributes.\r
1009\r
1010Arguments:\r
1011\r
1012 FfsFile FFS file to parse\r
1013 Alignment The minimum required alignment of the FFS file, in bytes\r
1014\r
1015Returns:\r
1016\r
1017 EFI_SUCCESS The function completed successfully.\r
1018 EFI_INVALID_PARAMETER One of the input parameters was invalid.\r
1019 EFI_ABORTED An error occurred.\r
1020\r
1021--*/\r
1022{\r
1023 //\r
1024 // Verify input parameters.\r
1025 //\r
1026 if (FfsFile == NULL || Alignment == NULL) {\r
1027 return EFI_INVALID_PARAMETER;\r
1028 }\r
1029\r
1030 switch ((FfsFile->Attributes >> 3) & 0x07) {\r
1031\r
1032 case 0:\r
1033 //\r
1034 // 1 byte alignment\r
1035 //\r
1036 *Alignment = (1 << 0);\r
1037 break;\r
1038\r
1039 case 1:\r
1040 //\r
1041 // 16 byte alignment\r
1042 //\r
1043 *Alignment = (1 << 4);\r
1044 break;\r
1045\r
1046 case 2:\r
1047 //\r
1048 // 128 byte alignment\r
1049 //\r
1050 *Alignment = (1 << 7);\r
1051 break;\r
1052\r
1053 case 3:\r
1054 //\r
1055 // 512 byte alignment\r
1056 //\r
1057 *Alignment = (1 << 9);\r
1058 break;\r
1059\r
1060 case 4:\r
1061 //\r
1062 // 1K byte alignment\r
1063 //\r
1064 *Alignment = (1 << 10);\r
1065 break;\r
1066\r
1067 case 5:\r
1068 //\r
1069 // 4K byte alignment\r
1070 //\r
1071 *Alignment = (1 << 12);\r
1072 break;\r
1073\r
1074 case 6:\r
1075 //\r
1076 // 32K byte alignment\r
1077 //\r
1078 *Alignment = (1 << 15);\r
1079 break;\r
1080\r
1081 case 7:\r
1082 //\r
1083 // 64K byte alignment\r
1084 //\r
1085 *Alignment = (1 << 16);\r
1086 break;\r
1087\r
1088 default:\r
1089 Error (NULL, 0, 0, "nvalid file attribute calculated, this is most likely a utility error", NULL);\r
1090 return EFI_ABORTED;\r
1091 }\r
1092\r
1093 return EFI_SUCCESS;\r
1094}\r
1095\r
1096EFI_STATUS\r
1097AddPadFile (\r
1098 IN OUT MEMORY_FILE *FvImage,\r
1099 IN UINT32 DataAlignment\r
1100 )\r
1101/*++\r
1102\r
1103Routine Description:\r
1104\r
1105 This function adds a pad file to the FV image if it required to align the\r
1106 data of the next file.\r
1107\r
1108Arguments:\r
1109\r
1110 FvImage The memory image of the FV to add it to. The current offset\r
1111 must be valid.\r
1112 DataAlignment The data alignment of the next FFS file.\r
1113\r
1114Returns:\r
1115\r
1116 EFI_SUCCESS The function completed successfully.\r
1117 EFI_INVALID_PARAMETER One of the input parameters was invalid.\r
1118 EFI_OUT_OF_RESOURCES Insufficient resources exist in the FV to complete\r
1119 the pad file add.\r
1120\r
1121--*/\r
1122{\r
1123 EFI_FFS_FILE_HEADER *PadFile;\r
1124 EFI_GUID PadFileGuid;\r
1125 UINTN PadFileSize;\r
1126\r
1127 //\r
1128 // Verify input parameters.\r
1129 //\r
1130 if (FvImage == NULL) {\r
1131 return EFI_INVALID_PARAMETER;\r
1132 }\r
1133 //\r
1134 // Basic assumption is we start from an 8 byte aligned address\r
1135 // and our file header is a multiple of 8 bytes\r
1136 //\r
1137 assert ((UINTN) FvImage->CurrentFilePointer % 8 == 0);\r
1138 assert (sizeof (EFI_FFS_FILE_HEADER) % 8 == 0);\r
1139\r
1140 //\r
1141 // Check if a pad file is necessary\r
1142 //\r
1143 if (((UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage + sizeof (EFI_FFS_FILE_HEADER)) % DataAlignment == 0) {\r
1144 return EFI_SUCCESS;\r
1145 }\r
1146 //\r
1147 // Write pad file header\r
1148 //\r
1149 PadFile = (EFI_FFS_FILE_HEADER *) FvImage->CurrentFilePointer;\r
1150\r
1151 //\r
1152 // Verify that we have enough space for the file header\r
1153 //\r
1154 if ((UINTN) (PadFile + sizeof (EFI_FFS_FILE_HEADER)) >= (UINTN) FvImage->Eof) {\r
1155 return EFI_OUT_OF_RESOURCES;\r
1156 }\r
1157\r
1158#ifdef __GNUC__\r
1159 {\r
1160 uuid_t tmp_id;\r
1161 uuid_generate (tmp_id);\r
1162 memcpy (&PadFileGuid, tmp_id, sizeof (EFI_GUID));\r
1163 }\r
1164#else\r
1165 UuidCreate (&PadFileGuid);\r
1166#endif\r
1167 memset (PadFile, 0, sizeof (EFI_FFS_FILE_HEADER));\r
1168 memcpy (&PadFile->Name, &PadFileGuid, sizeof (EFI_GUID));\r
1169 PadFile->Type = EFI_FV_FILETYPE_FFS_PAD;\r
1170 PadFile->Attributes = 0;\r
1171\r
1172 //\r
1173 // Calculate the pad file size\r
1174 //\r
1175 //\r
1176 // This is the earliest possible valid offset (current plus pad file header\r
1177 // plus the next file header)\r
1178 //\r
1179 PadFileSize = (UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage + (sizeof (EFI_FFS_FILE_HEADER) * 2);\r
1180\r
1181 //\r
1182 // Add whatever it takes to get to the next aligned address\r
1183 //\r
1184 while ((PadFileSize % DataAlignment) != 0) {\r
1185 PadFileSize++;\r
1186 }\r
1187 //\r
1188 // Subtract the next file header size\r
1189 //\r
1190 PadFileSize -= sizeof (EFI_FFS_FILE_HEADER);\r
1191\r
1192 //\r
1193 // Subtract the starting offset to get size\r
1194 //\r
1195 PadFileSize -= (UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage;\r
1196\r
1197 //\r
1198 // Write pad file size (calculated size minus next file header size)\r
1199 //\r
1200 PadFile->Size[0] = (UINT8) (PadFileSize & 0xFF);\r
1201 PadFile->Size[1] = (UINT8) ((PadFileSize >> 8) & 0xFF);\r
1202 PadFile->Size[2] = (UINT8) ((PadFileSize >> 16) & 0xFF);\r
1203\r
1204 //\r
1205 // Fill in checksums and state, they must be 0 for checksumming.\r
1206 //\r
1207 PadFile->IntegrityCheck.Checksum.Header = 0;\r
1208 PadFile->IntegrityCheck.Checksum.File = 0;\r
1209 PadFile->State = 0;\r
1210 PadFile->IntegrityCheck.Checksum.Header = CalculateChecksum8 ((UINT8 *) PadFile, sizeof (EFI_FFS_FILE_HEADER));\r
1211 if (PadFile->Attributes & FFS_ATTRIB_CHECKSUM) {\r
1212 PadFile->IntegrityCheck.Checksum.File = CalculateChecksum8 ((UINT8 *) PadFile, PadFileSize);\r
1213 } else {\r
1214 PadFile->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;\r
1215 }\r
1216\r
1217 PadFile->State = EFI_FILE_HEADER_CONSTRUCTION | EFI_FILE_HEADER_VALID | EFI_FILE_DATA_VALID;\r
1218 UpdateFfsFileState (\r
1219 (EFI_FFS_FILE_HEADER *) PadFile,\r
1220 (EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage\r
1221 );\r
1222\r
1223 //\r
1224 // Verify that we have enough space (including the padding\r
1225 //\r
1226 if ((UINTN) (PadFile + sizeof (EFI_FFS_FILE_HEADER)) >= (UINTN) FvImage->Eof) {\r
1227 return EFI_OUT_OF_RESOURCES;\r
1228 }\r
1229 //\r
1230 // Update the current FV pointer\r
1231 //\r
1232 FvImage->CurrentFilePointer += PadFileSize;\r
1233\r
1234 return EFI_SUCCESS;\r
1235}\r
1236\r
1237BOOLEAN\r
1238IsVtfFile (\r
1239 IN EFI_FFS_FILE_HEADER *FileBuffer\r
1240 )\r
1241/*++\r
1242\r
1243Routine Description:\r
1244\r
1245 This function checks the header to validate if it is a VTF file\r
1246\r
1247Arguments:\r
1248\r
1249 FileBuffer Buffer in which content of a file has been read.\r
1250\r
1251Returns:\r
1252\r
1253 TRUE If this is a VTF file\r
1254 FALSE If this is not a VTF file\r
1255\r
1256--*/\r
1257{\r
1258 EFI_GUID VtfGuid = EFI_FFS_VOLUME_TOP_FILE_GUID;\r
1259 if (!memcmp (&FileBuffer->Name, &VtfGuid, sizeof (EFI_GUID))) {\r
1260 return TRUE;\r
1261 } else {\r
1262 return FALSE;\r
1263 }\r
1264}\r
1265\r
1266EFI_STATUS\r
1267FfsRebaseImageRead (\r
1268 IN VOID *FileHandle,\r
1269 IN UINTN FileOffset,\r
1270 IN OUT UINT32 *ReadSize,\r
1271 OUT VOID *Buffer\r
1272 )\r
1273/*++\r
1274\r
1275Routine Description:\r
1276\r
1277 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r
1278\r
1279Arguments:\r
1280\r
1281 FileHandle - The handle to the PE/COFF file\r
1282\r
1283 FileOffset - The offset, in bytes, into the file to read\r
1284\r
1285 ReadSize - The number of bytes to read from the file starting at FileOffset\r
1286\r
1287 Buffer - A pointer to the buffer to read the data into.\r
1288\r
1289Returns:\r
1290\r
1291 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r
1292\r
1293--*/\r
1294{\r
1295 CHAR8 *Destination8;\r
1296 CHAR8 *Source8;\r
1297 UINT32 Length;\r
1298\r
1299 Destination8 = Buffer;\r
1300 Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r
1301 Length = *ReadSize;\r
1302 while (Length--) {\r
1303 *(Destination8++) = *(Source8++);\r
1304 }\r
1305\r
1306 return EFI_SUCCESS;\r
1307}\r
1308\r
878ddf1f 1309\r
1310EFI_STATUS\r
1311AddSymFile (\r
1312 IN UINT64 BaseAddress,\r
1313 IN EFI_FFS_FILE_HEADER *FfsFile,\r
1314 IN OUT MEMORY_FILE *SymImage,\r
1315 IN CHAR8 *SourceFileName\r
1316 )\r
1317/*++\r
1318\r
1319Routine Description:\r
1320\r
1321 This function adds the SYM tokens in the source file to the destination file.\r
1322 The SYM tokens are updated to reflect the base address.\r
1323\r
1324Arguments:\r
1325\r
1326 BaseAddress The base address for the new SYM tokens.\r
1327 FfsFile Pointer to the beginning of the FFS file in question.\r
1328 SymImage The memory file to update with symbol information.\r
1329 SourceFileName The source file.\r
1330\r
1331Returns:\r
1332\r
1333 EFI_SUCCESS The function completed successfully.\r
1334 EFI_INVALID_PARAMETER One of the input parameters was invalid.\r
1335 EFI_ABORTED An error occurred.\r
1336\r
1337--*/\r
1338{\r
1339 FILE *SourceFile;\r
1340\r
1341 CHAR8 Buffer[_MAX_PATH];\r
1342 CHAR8 Type[_MAX_PATH];\r
1343 CHAR8 Address[_MAX_PATH];\r
1344 CHAR8 Section[_MAX_PATH];\r
1345 CHAR8 Token[_MAX_PATH];\r
1346 CHAR8 SymFileName[_MAX_PATH];\r
1347 CHAR8 CodeModuleName[_MAX_PATH];\r
1348 CHAR8 *Ptr;\r
1349\r
1350 UINT64 TokenAddress;\r
1351\r
1352 EFI_STATUS Status;\r
1353 EFI_FILE_SECTION_POINTER Pe32Section;\r
1354 UINT32 EntryPoint;\r
1355 UINT32 BaseOfCode;\r
1356 UINT16 MachineType;\r
878ddf1f 1357\r
878ddf1f 1358 //\r
1359 // Verify input parameters.\r
1360 //\r
1361 if (BaseAddress == 0 || FfsFile == NULL || SymImage == NULL || SourceFileName == NULL) {\r
1362 Error (NULL, 0, 0, "invalid parameter passed to AddSymFile()", NULL);\r
1363 return EFI_INVALID_PARAMETER;\r
1364 }\r
1365 //\r
1366 // Check if we want to add this file\r
1367 //\r
1368 //\r
1369 // Get the file name\r
1370 //\r
1371 strcpy (Buffer, SourceFileName);\r
1372\r
1373 //\r
1374 // Copy the file name for the path of the sym file and truncate the name portion.\r
1375 //\r
1376 strcpy (SymFileName, Buffer);\r
1377 Ptr = strrchr (SymFileName, '\\');\r
1378 assert (Ptr);\r
1379 Ptr[0] = 0;\r
1380\r
1381 //\r
1382 // Find the file extension and make it lower case\r
1383 //\r
1384 Ptr = strrchr (SymFileName, '.');\r
1385 if (Ptr != NULL) {\r
1386 strlwr (Ptr);\r
1387 }\r
1388 //\r
1389 // Check if it is PEI file\r
1390 //\r
1391 if (strstr (Buffer, ".pei") != NULL) {\r
1392 //\r
1393 // Find the human readable portion\r
1394 //\r
1395 if (!strtok (Buffer, "-") ||\r
1396 !strtok (NULL, "-") ||\r
1397 !strtok (NULL, "-") ||\r
1398 !strtok (NULL, "-") ||\r
1399 !strtok (NULL, "-") ||\r
1400 !strcpy (Buffer, strtok (NULL, "."))\r
1401 ) {\r
1402 Error (NULL, 0, 0, "failed to find human readable portion of the file name in AddSymFile()", NULL);\r
1403 return EFI_ABORTED;\r
1404 }\r
1405 //\r
1406 // Save code module name\r
1407 //\r
1408 strcpy (CodeModuleName, Buffer);\r
1409\r
1410 //\r
1411 // Add the symbol file name and extension to the file path.\r
1412 //\r
1413 strcat (Buffer, ".sym");\r
1414 strcat (SymFileName, "\\");\r
1415 strcat (SymFileName, Buffer);\r
1416 } else {\r
1417 //\r
1418 // Only handle PEIM files.\r
1419 //\r
1420 return EFI_SUCCESS;\r
1421 }\r
1422 //\r
1423 // Find PE32 section\r
1424 //\r
1425 Status = GetSectionByType (FfsFile, EFI_SECTION_PE32, 1, &Pe32Section);\r
1426\r
1427 //\r
1428 // BUGBUG: Assume if no PE32 section it is PIC and hardcode base address\r
1429 //\r
1430 if (Status == EFI_NOT_FOUND) {\r
1431 Status = GetSectionByType (FfsFile, EFI_SECTION_TE, 1, &Pe32Section);\r
a651e231
LG
1432 }\r
1433\r
1434 if (Status == EFI_SUCCESS) {\r
878ddf1f 1435 Status = GetPe32Info (\r
1436 (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),\r
1437 &EntryPoint,\r
1438 &BaseOfCode,\r
1439 &MachineType\r
1440 );\r
a651e231
LG
1441 } else if (Status == EFI_NOT_FOUND) {\r
1442 //\r
1443 // For PIC, hardcode.\r
1444 //\r
1445 BaseOfCode = 0x60;\r
1446 Status = EFI_SUCCESS;\r
1447 } else {\r
1448 Error (NULL, 0, 0, "could not parse a PE32 section from the PEI file", NULL);\r
1449 return Status;\r
878ddf1f 1450 }\r
a651e231
LG
1451\r
1452 if (EFI_ERROR (Status)) {\r
1453 Error (NULL, 0, 0, "GetPe32Info() could not get PE32 entry point for PEI file", NULL);\r
1454 return Status;\r
1455 }\r
1456\r
878ddf1f 1457 //\r
1458 // Open the source file\r
1459 //\r
1460 SourceFile = fopen (SymFileName, "r");\r
1461 if (SourceFile == NULL) {\r
1462 //\r
1463 // SYM files are not required.\r
1464 //\r
1465 return EFI_SUCCESS;\r
1466 }\r
1467 //\r
1468 // Read the first line\r
1469 //\r
1470 if (fgets (Buffer, _MAX_PATH, SourceFile) == NULL) {\r
1471 Buffer[0] = 0;\r
1472 }\r
1473 //\r
1474 // Make sure it matches the expected sym format\r
1475 //\r
1476 if (strcmp (Buffer, "TEXTSYM format | V1.0\n")) {\r
1477 fclose (SourceFile);\r
1478 Error (NULL, 0, 0, "AddSymFile() found unexpected sym format in input file", NULL);\r
1479 return EFI_ABORTED;\r
1480 }\r
1481 //\r
1482 // Read in the file\r
1483 //\r
1484 while (feof (SourceFile) == 0) {\r
1485 //\r
1486 // Read a line\r
1487 //\r
1488 if (fscanf (\r
1489 SourceFile,\r
1490 "%s | %s | %s | %s\n",\r
1491 Type,\r
1492 Address,\r
1493 Section,\r
1494 Token\r
1495 ) == 4) {\r
1496 //\r
1497 // If the token starts with "??" ignore it\r
1498 //\r
1499 if (Token[0] == '?' && Token[1] == '?') {\r
1500 continue;\r
1501 }\r
1502 //\r
1503 // Get the token address\r
1504 //\r
1505 AsciiStringToUint64 (Address, TRUE, &TokenAddress);\r
1506\r
1507 //\r
1508 // Add the base address\r
1509 //\r
1510 TokenAddress += BaseAddress;\r
1511\r
1512 //\r
a651e231 1513 // If PE32 or TE section then find the start of code. For PIC it is hardcoded.\r
878ddf1f 1514 //\r
a651e231 1515 if (Pe32Section.Pe32Section) {\r
878ddf1f 1516 //\r
1517 // Add the offset of the PE32 section\r
1518 //\r
1519 TokenAddress += (UINTN) Pe32Section.Pe32Section - (UINTN) FfsFile;\r
1520\r
1521 //\r
1522 // Add the size of the PE32 section header\r
1523 //\r
1524 TokenAddress += sizeof (EFI_PE32_SECTION);\r
878ddf1f 1525 } else {\r
1526 //\r
a651e231 1527 // For PIC hardcoded.\r
878ddf1f 1528 //\r
1529 TokenAddress += 0x28;\r
1530 }\r
a651e231 1531\r
878ddf1f 1532 //\r
1533 // Add the beginning of the code\r
1534 //\r
1535 TokenAddress += BaseOfCode;\r
1536\r
1537 sprintf (\r
1538 Buffer,\r
1539 "%s | %016I64X | %s | _%s%s\n",\r
1540 Type,\r
1541 TokenAddress,\r
1542 Section,\r
1543 CodeModuleName,\r
1544 Token\r
1545 );\r
1546 memcpy (SymImage->CurrentFilePointer, Buffer, strlen (Buffer) + 1);\r
1547 SymImage->CurrentFilePointer = (UINT8 *) (((UINTN) SymImage->CurrentFilePointer) + strlen (Buffer) + 1);\r
1548 }\r
1549 }\r
1550\r
1551 fclose (SourceFile);\r
1552 return EFI_SUCCESS;\r
1553}\r
1554\r
1555EFI_STATUS\r
1556AddFile (\r
1557 IN OUT MEMORY_FILE *FvImage,\r
1558 IN FV_INFO *FvInfo,\r
1559 IN UINTN Index,\r
1560 IN OUT EFI_FFS_FILE_HEADER **VtfFileImage,\r
1561 IN OUT MEMORY_FILE *SymImage\r
1562 )\r
1563/*++\r
1564\r
1565Routine Description:\r
1566\r
1567 This function adds a file to the FV image. The file will pad to the\r
1568 appropriate alignment if required.\r
1569\r
1570Arguments:\r
1571\r
1572 FvImage The memory image of the FV to add it to. The current offset\r
1573 must be valid.\r
1574 FvInfo Pointer to information about the FV.\r
1575 Index The file in the FvInfo file list to add.\r
1576 VtfFileImage A pointer to the VTF file within the FvImage. If this is equal\r
1577 to the end of the FvImage then no VTF previously found.\r
1578 SymImage The memory image of the Sym file to update if symbols are present.\r
1579 The current offset must be valid.\r
1580\r
1581Returns:\r
1582\r
1583 EFI_SUCCESS The function completed successfully.\r
1584 EFI_INVALID_PARAMETER One of the input parameters was invalid.\r
1585 EFI_ABORTED An error occurred.\r
1586 EFI_OUT_OF_RESOURCES Insufficient resources exist to complete the add.\r
1587\r
1588--*/\r
1589{\r
1590 FILE *NewFile;\r
1591 UINTN FileSize;\r
1592 UINT8 *FileBuffer;\r
1593 UINTN NumBytesRead;\r
1594 UINT32 CurrentFileAlignment;\r
1595 EFI_STATUS Status;\r
1596 EFI_PHYSICAL_ADDRESS CurrentFileBaseAddress;\r
1597 UINT8 VtfHeaderChecksum;\r
1598 UINT8 VtfFileChecksum;\r
1599 UINT8 FileState;\r
1600 EFI_FFS_FILE_TAIL TailValue;\r
1601 UINT32 TailSize;\r
1602 //\r
1603 // Verify input parameters.\r
1604 //\r
1605 if (FvImage == NULL || FvInfo == NULL || FvInfo->FvFiles[Index][0] == 0 || VtfFileImage == NULL || SymImage == NULL) {\r
1606 return EFI_INVALID_PARAMETER;\r
1607 }\r
1608 //\r
1609 // Read the file to add\r
1610 //\r
1611 NewFile = fopen (FvInfo->FvFiles[Index], "rb");\r
1612\r
1613 if (NewFile == NULL) {\r
1614 Error (NULL, 0, 0, FvInfo->FvFiles[Index], "failed to open file for reading");\r
1615 return EFI_ABORTED;\r
1616 }\r
1617 //\r
1618 // Get the file size\r
1619 //\r
878ddf1f 1620 FileSize = _filelength (fileno (NewFile));\r
878ddf1f 1621\r
1622 //\r
1623 // Read the file into a buffer\r
1624 //\r
1625 FileBuffer = malloc (FileSize);\r
1626 if (FileBuffer == NULL) {\r
1627 Error (NULL, 0, 0, "memory allocation failure", NULL);\r
1628 return EFI_OUT_OF_RESOURCES;\r
1629 }\r
1630\r
1631 NumBytesRead = fread (FileBuffer, sizeof (UINT8), FileSize, NewFile);\r
1632\r
1633 //\r
1634 // Done with the file, from this point on we will just use the buffer read.\r
1635 //\r
1636 fclose (NewFile);\r
1637\r
1638 //\r
1639 // Verify read successful\r
1640 //\r
1641 if (NumBytesRead != sizeof (UINT8) * FileSize) {\r
1642 free (FileBuffer);\r
1643 Error (NULL, 0, 0, FvInfo->FvFiles[Index], "failed to read input file contents");\r
1644 return EFI_ABORTED;\r
1645 }\r
1646 //\r
1647 // Verify space exists to add the file\r
1648 //\r
1649 if (FileSize > (UINTN) ((UINTN) *VtfFileImage - (UINTN) FvImage->CurrentFilePointer)) {\r
1650 Error (NULL, 0, 0, FvInfo->FvFiles[Index], "insufficient space remains to add the file");\r
1651 return EFI_OUT_OF_RESOURCES;\r
1652 }\r
1653 //\r
1654 // Update the file state based on polarity of the FV.\r
1655 //\r
1656 UpdateFfsFileState (\r
1657 (EFI_FFS_FILE_HEADER *) FileBuffer,\r
1658 (EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage\r
1659 );\r
1660\r
1661 //\r
1662 // If we have a VTF file, add it at the top.\r
1663 //\r
1664 if (IsVtfFile ((EFI_FFS_FILE_HEADER *) FileBuffer)) {\r
1665 if ((UINTN) *VtfFileImage == (UINTN) FvImage->Eof) {\r
1666 //\r
1667 // No previous VTF, add this one.\r
1668 //\r
1669 *VtfFileImage = (EFI_FFS_FILE_HEADER *) (UINTN) ((UINTN) FvImage->FileImage + FvInfo->Size - FileSize);\r
1670 //\r
1671 // Sanity check. The file MUST align appropriately\r
1672 //\r
1673 if ((((UINTN) *VtfFileImage) & 0x07) != 0) {\r
1674 Error (NULL, 0, 0, "VTF file does not align on 8-byte boundary", NULL);\r
1675 }\r
1676 //\r
1677 // copy VTF File Header\r
1678 //\r
1679 memcpy (*VtfFileImage, FileBuffer, sizeof (EFI_FFS_FILE_HEADER));\r
1680\r
1681 //\r
1682 // Copy VTF body\r
1683 //\r
1684 memcpy (\r
1685 (UINT8 *) *VtfFileImage + sizeof (EFI_FFS_FILE_HEADER),\r
1686 FileBuffer + sizeof (EFI_FFS_FILE_HEADER),\r
1687 FileSize - sizeof (EFI_FFS_FILE_HEADER)\r
1688 );\r
1689\r
1690 //\r
1691 // re-calculate the VTF File Header\r
1692 //\r
1693 FileState = (*VtfFileImage)->State;\r
1694 (*VtfFileImage)->State = 0;\r
1695 *(UINT32 *) ((*VtfFileImage)->Size) = FileSize;\r
1696 (*VtfFileImage)->IntegrityCheck.Checksum.Header = 0;\r
1697 (*VtfFileImage)->IntegrityCheck.Checksum.File = 0;\r
1698\r
1699 VtfHeaderChecksum = CalculateChecksum8 ((UINT8 *) *VtfFileImage, sizeof (EFI_FFS_FILE_HEADER));\r
1700 (*VtfFileImage)->IntegrityCheck.Checksum.Header = VtfHeaderChecksum;\r
1701 //\r
1702 // Determine if it has a tail\r
1703 //\r
1704 if ((*VtfFileImage)->Attributes & FFS_ATTRIB_TAIL_PRESENT) {\r
1705 TailSize = sizeof (EFI_FFS_FILE_TAIL);\r
1706 } else {\r
1707 TailSize = 0;\r
1708 }\r
1709\r
1710 if ((*VtfFileImage)->Attributes & FFS_ATTRIB_CHECKSUM) {\r
1711 VtfFileChecksum = CalculateChecksum8 ((UINT8 *) *VtfFileImage, FileSize - TailSize);\r
1712 (*VtfFileImage)->IntegrityCheck.Checksum.File = VtfFileChecksum;\r
1713 } else {\r
1714 (*VtfFileImage)->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;\r
1715 }\r
1716 //\r
1717 // If it has a file tail, update it\r
1718 //\r
1719 if ((*VtfFileImage)->Attributes & FFS_ATTRIB_TAIL_PRESENT) {\r
1720 TailValue = (EFI_FFS_FILE_TAIL) (~((*VtfFileImage)->IntegrityCheck.TailReference));\r
1721 *(EFI_FFS_FILE_TAIL *) (((UINTN) (*VtfFileImage) + GetLength ((*VtfFileImage)->Size) - sizeof (EFI_FFS_FILE_TAIL))) = TailValue;\r
1722 }\r
1723 (*VtfFileImage)->State = FileState;\r
1724 free (FileBuffer);\r
1725 return EFI_SUCCESS;\r
1726 } else {\r
1727 //\r
1728 // Already found a VTF file.\r
1729 //\r
1730 Error (NULL, 0, 0, "multiple VTF files are illegal in a single FV", NULL);\r
1731 free (FileBuffer);\r
1732 return EFI_ABORTED;\r
1733 }\r
1734 }\r
1735 //\r
1736 // Check if alignment is required\r
1737 //\r
1738 Status = ReadFfsAlignment ((EFI_FFS_FILE_HEADER *) FileBuffer, &CurrentFileAlignment);\r
1739 if (EFI_ERROR (Status)) {\r
1740 printf ("ERROR: Could not determine alignment of file %s.\n", FvInfo->FvFiles[Index]);\r
1741 free (FileBuffer);\r
1742 return EFI_ABORTED;\r
1743 }\r
1744 //\r
1745 // Add pad file if necessary\r
1746 //\r
1747 Status = AddPadFile (FvImage, CurrentFileAlignment);\r
1748 if (EFI_ERROR (Status)) {\r
1749 printf ("ERROR: Could not align the file data properly.\n");\r
1750 free (FileBuffer);\r
1751 return EFI_ABORTED;\r
1752 }\r
1753 //\r
1754 // Add file\r
1755 //\r
1756 if ((FvImage->CurrentFilePointer + FileSize) < FvImage->Eof) {\r
1757 //\r
1758 // Copy the file\r
1759 //\r
1760 memcpy (FvImage->CurrentFilePointer, FileBuffer, FileSize);\r
1761\r
1762 //\r
1763 // If the file is XIP, rebase\r
1764 //\r
1765 CurrentFileBaseAddress = FvInfo->BaseAddress + ((UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage);\r
1766 //\r
1767 // Status = RebaseFfsFile ((EFI_FFS_FILE_HEADER*) FvImage->CurrentFilePointer, CurrentFileBaseAddress);\r
1768 // if (EFI_ERROR(Status)) {\r
1769 // printf ("ERROR: Could not rebase the file %s.\n", FvInfo->FvFiles[Index]);\r
1770 // return EFI_ABORTED;\r
1771 // }\r
1772 //\r
1773 // Update Symbol file\r
1774 //\r
1775 Status = AddSymFile (\r
1776 CurrentFileBaseAddress,\r
1777 (EFI_FFS_FILE_HEADER *) FvImage->CurrentFilePointer,\r
1778 SymImage,\r
1779 FvInfo->FvFiles[Index]\r
1780 );\r
1781 assert (!EFI_ERROR (Status));\r
1782\r
1783 //\r
1784 // Update the current pointer in the FV image\r
1785 //\r
1786 FvImage->CurrentFilePointer += FileSize;\r
1787 } else {\r
1788 printf ("ERROR: The firmware volume is out of space, could not add file %s.\n", FvInfo->FvFiles[Index]);\r
1789 return EFI_ABORTED;\r
1790 }\r
1791 //\r
1792 // Make next file start at QWord Boundry\r
1793 //\r
1794 while (((UINTN) FvImage->CurrentFilePointer & 0x07) != 0) {\r
1795 FvImage->CurrentFilePointer++;\r
1796 }\r
1797 //\r
1798 // Free allocated memory.\r
1799 //\r
1800 free (FileBuffer);\r
1801\r
1802 return EFI_SUCCESS;\r
1803}\r
1804\r
1805EFI_STATUS\r
1806AddVariableBlock (\r
1807 IN UINT8 *FvImage,\r
1808 IN UINTN Size,\r
1809 IN FV_INFO *FvInfo\r
1810 )\r
1811{\r
1812 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
1813 VARIABLE_STORE_HEADER *VarStoreHeader;\r
1814 //\r
1815 // Variable block should exclude FvHeader. Since the length of\r
1816 // FvHeader depends on the block map, which is variable length,\r
1817 // we could only decide the actual variable block length here.\r
1818 //\r
1819 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) FvImage;\r
1820 FvImage = FvImage + FvHeader->HeaderLength;\r
1821\r
1822 VarStoreHeader = (VARIABLE_STORE_HEADER *) FvImage;\r
1823\r
1824 VarStoreHeader->Signature = VARIABLE_STORE_SIGNATURE;\r
1825 VarStoreHeader->Size = Size - FvHeader->HeaderLength;\r
1826 VarStoreHeader->Format = VARIABLE_STORE_FORMATTED;\r
1827 VarStoreHeader->State = VARIABLE_STORE_HEALTHY;\r
1828 VarStoreHeader->Reserved = 0;\r
1829 VarStoreHeader->Reserved1 = 0;\r
1830\r
1831 return EFI_SUCCESS;\r
1832}\r
1833\r
1834EFI_STATUS\r
1835AddEventLogBlock (\r
1836 IN UINT8 *FvImage,\r
1837 IN UINTN Size,\r
1838 IN FV_INFO *FvInfo\r
1839 )\r
1840{\r
1841 return EFI_SUCCESS;\r
1842}\r
1843\r
1844EFI_STATUS\r
1845AddFTWWorkingBlock (\r
1846 IN UINT8 *FvImage,\r
1847 IN UINTN Size,\r
1848 IN FV_INFO *FvInfo\r
1849 )\r
1850{\r
1851 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *FTWHeader;\r
1852 UINT32 Crc32;\r
1853\r
1854 Crc32 = 0;\r
1855 FTWHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) FvImage;\r
1856 memcpy (&FTWHeader->Signature, &(FvInfo->FvGuid), sizeof (EFI_GUID));\r
1857 FTWHeader->WriteQueueSize = Size - sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER);\r
1858 CalculateCrc32 (FvImage, sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER), &Crc32);\r
1859 FTWHeader->Crc = Crc32;\r
1860 if (FvInfo->FvAttributes & EFI_FVB_ERASE_POLARITY) {\r
1861 FTWHeader->WorkingBlockValid = 0;\r
1862 FTWHeader->WorkingBlockInvalid = 1;\r
1863 } else {\r
1864 FTWHeader->WorkingBlockValid = 1;\r
1865 FTWHeader->WorkingBlockInvalid = 0;\r
1866 }\r
1867\r
1868 return EFI_SUCCESS;\r
1869}\r
1870\r
1871EFI_STATUS\r
1872AddFTWSpareBlock (\r
1873 IN UINT8 *FvImage,\r
1874 IN UINTN Size,\r
1875 IN FV_INFO *FvInfo\r
1876 )\r
1877{\r
1878 return EFI_SUCCESS;\r
1879}\r
1880\r
1881EFI_STATUS\r
1882GenNonFFSFv (\r
1883 IN UINT8 *FvImage,\r
1884 IN FV_INFO *FvInfo\r
1885 )\r
1886/*++\r
1887\r
1888Routine Description:\r
1889\r
1890 This function generate the non FFS FV image, such as the working block\r
1891 and spare block. How each component of the FV is built is component\r
1892 specific.\r
1893\r
1894Arguments:\r
1895\r
1896 FvImage The memory image of the FV to add it to. The current offset\r
1897 must be valid.\r
1898 FvInfo Pointer to information about the FV.\r
1899\r
1900Returns:\r
1901\r
1902 EFI_SUCCESS The function completed successfully.\r
1903 EFI_INVALID_PARAMETER One of the input parameters was invalid.\r
1904 EFI_ABORTED An error occurred.\r
1905 EFI_OUT_OF_RESOURCES Insufficient resources exist to complete the add.\r
1906\r
1907--*/\r
1908{\r
1909 UINTN Index;\r
1910 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
1911 UINT64 TotalSize;\r
1912\r
1913 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) FvImage;\r
1914 TotalSize = 0;\r
1915\r
1916 for (Index = 0; FvInfo->FvComponents[Index].Size != 0; Index++) {\r
1917 if (stricmp (FvInfo->FvComponents[Index].ComponentName, EFI_NV_VARIABLE_STRING) == 0) {\r
1918 AddVariableBlock (FvImage, FvInfo->FvComponents[Index].Size, FvInfo);\r
1919 } else if (stricmp (FvInfo->FvComponents[Index].ComponentName, EFI_NV_EVENT_LOG_STRING) == 0) {\r
1920 AddEventLogBlock (FvImage, FvInfo->FvComponents[Index].Size, FvInfo);\r
1921 } else if (stricmp (FvInfo->FvComponents[Index].ComponentName, EFI_NV_FTW_WORKING_STRING) == 0) {\r
1922 AddFTWWorkingBlock (FvImage, FvInfo->FvComponents[Index].Size, FvInfo);\r
1923 } else if (stricmp (FvInfo->FvComponents[Index].ComponentName, EFI_NV_FTW_SPARE_STRING) == 0) {\r
1924 AddFTWSpareBlock (FvImage, FvInfo->FvComponents[Index].Size, FvInfo);\r
1925 } else {\r
1926 printf ("Error. Unknown Non-FFS block %s \n", FvInfo->FvComponents[Index].ComponentName);\r
1927 return EFI_ABORTED;\r
1928 }\r
1929\r
1930 FvImage = FvImage + FvInfo->FvComponents[Index].Size;\r
1931 TotalSize = TotalSize + FvInfo->FvComponents[Index].Size;\r
1932 }\r
1933 //\r
1934 // Index and TotalSize is zero mean there's no component, so this is an empty fv\r
1935 //\r
1936 if ((Index != 0 || TotalSize != 0) && TotalSize != FvInfo->Size) {\r
1937 printf ("Error. Component size does not sum up to FV size.\n");\r
1938 return EFI_ABORTED;\r
1939 }\r
1940\r
1941 return EFI_SUCCESS;\r
1942}\r
1943\r
1944EFI_STATUS\r
1945PadFvImage (\r
1946 IN MEMORY_FILE *FvImage,\r
1947 IN EFI_FFS_FILE_HEADER *VtfFileImage\r
1948 )\r
1949/*++\r
1950\r
1951Routine Description:\r
1952\r
1953 This function places a pad file between the last file in the FV and the VTF\r
1954 file if the VTF file exists.\r
1955\r
1956Arguments:\r
1957\r
1958 FvImage Memory file for the FV memory image\r
1959 VtfFileImage The address of the VTF file. If this is the end of the FV\r
1960 image, no VTF exists and no pad file is needed.\r
1961\r
1962Returns:\r
1963\r
1964 EFI_SUCCESS Completed successfully.\r
1965 EFI_INVALID_PARAMETER One of the input parameters was NULL.\r
1966\r
1967--*/\r
1968{\r
1969 EFI_FFS_FILE_HEADER *PadFile;\r
1970 UINTN FileSize;\r
1971\r
1972 //\r
1973 // If there is no VTF or the VTF naturally follows the previous file without a\r
1974 // pad file, then there's nothing to do\r
1975 //\r
1976 if ((UINTN) VtfFileImage == (UINTN) FvImage->Eof || (void *) FvImage->CurrentFilePointer == (void *) VtfFileImage) {\r
1977 return EFI_SUCCESS;\r
1978 }\r
1979 //\r
1980 // Pad file starts at beginning of free space\r
1981 //\r
1982 PadFile = (EFI_FFS_FILE_HEADER *) FvImage->CurrentFilePointer;\r
1983\r
1984 //\r
1985 // write header\r
1986 //\r
1987 memset (PadFile, 0, sizeof (EFI_FFS_FILE_HEADER));\r
1988 memcpy (&PadFile->Name, &DefaultFvPadFileNameGuid, sizeof (EFI_GUID));\r
1989 PadFile->Type = EFI_FV_FILETYPE_FFS_PAD;\r
1990 PadFile->Attributes = 0;\r
1991\r
1992 //\r
1993 // FileSize includes the EFI_FFS_FILE_HEADER\r
1994 //\r
1995 FileSize = (UINTN) VtfFileImage - (UINTN) FvImage->CurrentFilePointer;\r
1996 PadFile->Size[0] = (UINT8) (FileSize & 0x000000FF);\r
1997 PadFile->Size[1] = (UINT8) ((FileSize & 0x0000FF00) >> 8);\r
1998 PadFile->Size[2] = (UINT8) ((FileSize & 0x00FF0000) >> 16);\r
1999\r
2000 //\r
2001 // Fill in checksums and state, must be zero during checksum calculation.\r
2002 //\r
2003 PadFile->IntegrityCheck.Checksum.Header = 0;\r
2004 PadFile->IntegrityCheck.Checksum.File = 0;\r
2005 PadFile->State = 0;\r
2006 PadFile->IntegrityCheck.Checksum.Header = CalculateChecksum8 ((UINT8 *) PadFile, sizeof (EFI_FFS_FILE_HEADER));\r
2007 if (PadFile->Attributes & FFS_ATTRIB_CHECKSUM) {\r
2008 PadFile->IntegrityCheck.Checksum.File = CalculateChecksum8 ((UINT8 *) PadFile, FileSize);\r
2009 } else {\r
2010 PadFile->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;\r
2011 }\r
2012\r
2013 PadFile->State = EFI_FILE_HEADER_CONSTRUCTION | EFI_FILE_HEADER_VALID | EFI_FILE_DATA_VALID;\r
2014\r
2015 UpdateFfsFileState (\r
2016 (EFI_FFS_FILE_HEADER *) PadFile,\r
2017 (EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage\r
2018 );\r
2019 //\r
2020 // Update the current FV pointer\r
2021 //\r
2022 FvImage->CurrentFilePointer = FvImage->Eof;\r
2023\r
2024 return EFI_SUCCESS;\r
2025}\r
2026\r
2027EFI_STATUS\r
2028UpdateResetVector (\r
2029 IN MEMORY_FILE *FvImage,\r
2030 IN FV_INFO *FvInfo,\r
2031 IN EFI_FFS_FILE_HEADER *VtfFile\r
2032 )\r
2033/*++\r
2034\r
2035Routine Description:\r
2036\r
2037 This parses the FV looking for the PEI core and then plugs the address into\r
2038 the SALE_ENTRY point of the BSF/VTF for IPF and does BUGBUG TBD action to\r
2039 complete an IA32 Bootstrap FV.\r
2040\r
2041Arguments:\r
2042\r
2043 FvImage Memory file for the FV memory image\r
2044 FvInfo Information read from INF file.\r
2045 VtfFile Pointer to the VTF file in the FV image.\r
2046\r
2047Returns:\r
2048\r
2049 EFI_SUCCESS Function Completed successfully.\r
2050 EFI_ABORTED Error encountered.\r
2051 EFI_INVALID_PARAMETER A required parameter was NULL.\r
2052 EFI_NOT_FOUND PEI Core file not found.\r
2053\r
2054--*/\r
2055{\r
2056 EFI_FFS_FILE_HEADER *PeiCoreFile;\r
2057 EFI_FFS_FILE_HEADER *SecCoreFile;\r
2058 EFI_STATUS Status;\r
2059 EFI_FILE_SECTION_POINTER Pe32Section;\r
2060 UINT32 EntryPoint;\r
2061 UINT32 BaseOfCode;\r
2062 UINT16 MachineType;\r
2063 EFI_PHYSICAL_ADDRESS PeiCorePhysicalAddress;\r
2064 EFI_PHYSICAL_ADDRESS SecCorePhysicalAddress;\r
2065 EFI_PHYSICAL_ADDRESS *SecCoreEntryAddressPtr;\r
2066 UINT32 *Ia32ResetAddressPtr;\r
2067 UINT8 *BytePointer;\r
2068 UINT8 *BytePointer2;\r
2069 UINT16 *WordPointer;\r
2070 UINT16 CheckSum;\r
2071 UINTN Index;\r
2072 EFI_FFS_FILE_STATE SavedState;\r
2073 EFI_FFS_FILE_TAIL TailValue;\r
2074 UINT32 TailSize;\r
2075 UINT64 FitAddress;\r
2076 FIT_TABLE *FitTablePtr;\r
2077\r
2078 //\r
2079 // Verify input parameters\r
2080 //\r
2081 if (FvImage == NULL || FvInfo == NULL || VtfFile == NULL) {\r
2082 return EFI_INVALID_PARAMETER;\r
2083 }\r
2084 //\r
2085 // Initialize FV library\r
2086 //\r
2087 InitializeFvLib (FvImage->FileImage, (UINTN) FvImage->Eof - (UINTN) FvImage->FileImage);\r
2088\r
2089 //\r
2090 // Verify VTF file\r
2091 //\r
2092 Status = VerifyFfsFile (VtfFile);\r
2093 if (EFI_ERROR (Status)) {\r
2094 return EFI_INVALID_PARAMETER;\r
2095 }\r
2096 //\r
2097 // Find the PEI Core\r
2098 //\r
2099 Status = GetFileByType (EFI_FV_FILETYPE_PEI_CORE, 1, &PeiCoreFile);\r
2100 if (EFI_ERROR (Status) || PeiCoreFile == NULL) {\r
2101 Error (NULL, 0, 0, "could not find the PEI core in the FV", NULL);\r
2102 return EFI_ABORTED;\r
2103 }\r
a651e231 2104\r
878ddf1f 2105 //\r
a651e231 2106 // PEI Core found, now find PE32 or TE section\r
878ddf1f 2107 //\r
2108 Status = GetSectionByType (PeiCoreFile, EFI_SECTION_PE32, 1, &Pe32Section);\r
a651e231
LG
2109 if (Status == EFI_NOT_FOUND) {\r
2110 Status = GetSectionByType (PeiCoreFile, EFI_SECTION_TE, 1, &Pe32Section);\r
2111 }\r
2112\r
878ddf1f 2113 if (EFI_ERROR (Status)) {\r
a651e231 2114 Error (NULL, 0, 0, "could not find PE32 or TE section in PEI core file", NULL);\r
878ddf1f 2115 return EFI_ABORTED;\r
2116 }\r
2117\r
2118 Status = GetPe32Info (\r
2119 (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),\r
2120 &EntryPoint,\r
2121 &BaseOfCode,\r
2122 &MachineType\r
2123 );\r
a651e231 2124\r
878ddf1f 2125 if (EFI_ERROR (Status)) {\r
2126 Error (NULL, 0, 0, "could not get PE32 entry point for PEI core", NULL);\r
2127 return EFI_ABORTED;\r
2128 }\r
2129 //\r
2130 // Physical address is FV base + offset of PE32 + offset of the entry point\r
2131 //\r
2132 PeiCorePhysicalAddress = FvInfo->BaseAddress;\r
2133 PeiCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32) - (UINTN) FvImage->FileImage;\r
2134 PeiCorePhysicalAddress += EntryPoint;\r
2135\r
2136 if (MachineType == EFI_IMAGE_MACHINE_IA64) {\r
2137 //\r
2138 // Update PEI_CORE address\r
2139 //\r
2140 //\r
2141 // Set the uncached attribute bit in the physical address\r
2142 //\r
2143 PeiCorePhysicalAddress |= 0x8000000000000000ULL;\r
2144\r
2145 //\r
2146 // Check if address is aligned on a 16 byte boundary\r
2147 //\r
2148 if (PeiCorePhysicalAddress & 0xF) {\r
2149 printf (\r
2150 "ERROR: PEI_CORE entry point is not aligned on a 16 byte boundary, address specified is %Xh.\n",\r
2151 PeiCorePhysicalAddress\r
2152 );\r
2153 return EFI_ABORTED;\r
2154 }\r
2155 //\r
2156 // First Get the FIT table address\r
2157 //\r
2158 FitAddress = (*(UINT64 *) (FvImage->Eof - IPF_FIT_ADDRESS_OFFSET)) & 0xFFFFFFFF;\r
2159\r
2160 FitTablePtr = (FIT_TABLE *) (FvImage->FileImage + (FitAddress - FvInfo->BaseAddress));\r
2161\r
2162 Status = UpdatePeiCoreEntryInFit (FitTablePtr, PeiCorePhysicalAddress);\r
2163\r
2164 if (!EFI_ERROR (Status)) {\r
2165 UpdateFitCheckSum (FitTablePtr);\r
2166 }\r
2167 //\r
2168 // Find the Sec Core\r
2169 //\r
2170 Status = GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE, 1, &SecCoreFile);\r
2171 if (EFI_ERROR (Status) || SecCoreFile == NULL) {\r
2172 Error (NULL, 0, 0, "could not find the Sec core in the FV", NULL);\r
2173 return EFI_ABORTED;\r
2174 }\r
2175 //\r
2176 // Sec Core found, now find PE32 section\r
2177 //\r
2178 Status = GetSectionByType (SecCoreFile, EFI_SECTION_PE32, 1, &Pe32Section);\r
2179 if (EFI_ERROR (Status)) {\r
2180 Error (NULL, 0, 0, "could not find PE32 section in SEC core file", NULL);\r
2181 return EFI_ABORTED;\r
2182 }\r
2183\r
2184 Status = GetPe32Info (\r
2185 (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),\r
2186 &EntryPoint,\r
2187 &BaseOfCode,\r
2188 &MachineType\r
2189 );\r
2190 if (EFI_ERROR (Status)) {\r
2191 Error (NULL, 0, 0, "could not get PE32 entry point for SEC core", NULL);\r
2192 return EFI_ABORTED;\r
2193 }\r
2194 //\r
2195 // Physical address is FV base + offset of PE32 + offset of the entry point\r
2196 //\r
2197 SecCorePhysicalAddress = FvInfo->BaseAddress;\r
2198 SecCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32) - (UINTN) FvImage->FileImage;\r
2199 SecCorePhysicalAddress += EntryPoint;\r
2200\r
2201 //\r
2202 // Update SEC_CORE address\r
2203 //\r
2204 //\r
2205 // Set the uncached attribute bit in the physical address\r
2206 //\r
2207 SecCorePhysicalAddress |= 0x8000000000000000ULL;\r
2208\r
2209 //\r
2210 // Update the address\r
2211 //\r
2212 SecCoreEntryAddressPtr = (EFI_PHYSICAL_ADDRESS *) ((UINTN) FvImage->Eof - IPF_SALE_ENTRY_ADDRESS_OFFSET);\r
2213 *SecCoreEntryAddressPtr = SecCorePhysicalAddress;\r
2214\r
2215 //\r
2216 // Check if address is aligned on a 16 byte boundary\r
2217 //\r
2218 if (SecCorePhysicalAddress & 0xF) {\r
2219 printf (\r
2220 "ERROR: SALE_ENTRY entry point is not aligned on a 16 byte boundary, address specified is %Xh.\n",\r
2221 SecCorePhysicalAddress\r
2222 );\r
2223 return EFI_ABORTED;\r
2224 }\r
2225 } else if (MachineType == EFI_IMAGE_MACHINE_IA32) {\r
2226 //\r
2227 // Get the location to update\r
2228 //\r
2229 Ia32ResetAddressPtr = (UINT32 *) ((UINTN) FvImage->Eof - IA32_PEI_CORE_ENTRY_OFFSET);\r
2230\r
2231 //\r
2232 // Write lower 32 bits of physical address\r
2233 //\r
2234 *Ia32ResetAddressPtr = (UINT32) PeiCorePhysicalAddress;\r
2235\r
2236 //\r
2237 // Update the BFV base address\r
2238 //\r
2239 Ia32ResetAddressPtr = (UINT32 *) ((UINTN) FvImage->Eof - 4);\r
2240 *Ia32ResetAddressPtr = (UINT32) (FvInfo->BaseAddress);\r
2241\r
2242 CheckSum = 0x0000;\r
2243\r
2244 //\r
2245 // Update the Startup AP in the FVH header block ZeroVector region.\r
2246 //\r
2247 BytePointer = (UINT8 *) ((UINTN) FvImage->FileImage);\r
2248 BytePointer2 = (FvInfo->Size == 0x10000) ? m64kRecoveryStartupApDataArray : m128kRecoveryStartupApDataArray;\r
2249 for (Index = 0; Index < SIZEOF_STARTUP_DATA_ARRAY; Index++) {\r
2250 *BytePointer++ = *BytePointer2++;\r
2251 }\r
2252 //\r
2253 // Calculate the checksum\r
2254 //\r
2255 WordPointer = (UINT16 *) ((UINTN) FvImage->FileImage);\r
2256 for (Index = 0; Index < SIZEOF_STARTUP_DATA_ARRAY / 2; Index++) {\r
2257 CheckSum = (UINT16) (CheckSum + ((UINT16) *WordPointer));\r
2258 WordPointer++;\r
2259 }\r
2260 //\r
2261 // Update the checksum field\r
2262 //\r
2263 BytePointer = (UINT8 *) ((UINTN) FvImage->FileImage);\r
2264 BytePointer += (SIZEOF_STARTUP_DATA_ARRAY - 2);\r
2265 WordPointer = (UINT16 *) BytePointer;\r
2266 *WordPointer = (UINT16) (0x10000 - (UINT32) CheckSum);\r
2267 } else {\r
2268 Error (NULL, 0, 0, "invalid machine type in PEI core", "machine type=0x%X", (UINT32) MachineType);\r
2269 return EFI_ABORTED;\r
2270 }\r
2271 //\r
2272 // Determine if it has an FFS file tail.\r
2273 //\r
2274 if (VtfFile->Attributes & FFS_ATTRIB_TAIL_PRESENT) {\r
2275 TailSize = sizeof (EFI_FFS_FILE_TAIL);\r
2276 } else {\r
2277 TailSize = 0;\r
2278 }\r
2279 //\r
2280 // Now update file checksum\r
2281 //\r
2282 SavedState = VtfFile->State;\r
2283 VtfFile->IntegrityCheck.Checksum.File = 0;\r
2284 VtfFile->State = 0;\r
2285 if (VtfFile->Attributes & FFS_ATTRIB_CHECKSUM) {\r
2286 VtfFile->IntegrityCheck.Checksum.File = CalculateChecksum8 (\r
2287 (UINT8 *) VtfFile,\r
2288 GetLength (VtfFile->Size) - TailSize\r
2289 );\r
2290 } else {\r
2291 VtfFile->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;\r
2292 }\r
2293\r
2294 VtfFile->State = SavedState;\r
2295\r
2296 //\r
2297 // Update tail if present\r
2298 //\r
2299 if (VtfFile->Attributes & FFS_ATTRIB_TAIL_PRESENT) {\r
2300 TailValue = (EFI_FFS_FILE_TAIL) (~(VtfFile->IntegrityCheck.TailReference));\r
2301 *(EFI_FFS_FILE_TAIL *) (((UINTN) (VtfFile) + GetLength (VtfFile->Size) - sizeof (EFI_FFS_FILE_TAIL))) = TailValue;\r
2302 }\r
2303\r
2304 return EFI_SUCCESS;\r
2305}\r
2306\r
2307EFI_STATUS\r
2308GetPe32Info (\r
2309 IN UINT8 *Pe32,\r
2310 OUT UINT32 *EntryPoint,\r
2311 OUT UINT32 *BaseOfCode,\r
2312 OUT UINT16 *MachineType\r
2313 )\r
2314/*++\r
2315\r
2316Routine Description:\r
2317\r
a651e231
LG
2318 Retrieves the PE32 entry point offset and machine type from PE image or TeImage. \r
2319 See EfiImage.h for machine types. The entry point offset is from the beginning \r
2320 of the PE32 buffer passed in.\r
878ddf1f 2321\r
2322Arguments:\r
2323\r
2324 Pe32 Beginning of the PE32.\r
2325 EntryPoint Offset from the beginning of the PE32 to the image entry point.\r
2326 BaseOfCode Base address of code.\r
2327 MachineType Magic number for the machine type.\r
2328\r
2329Returns:\r
2330\r
2331 EFI_SUCCESS Function completed successfully.\r
2332 EFI_ABORTED Error encountered.\r
2333 EFI_INVALID_PARAMETER A required parameter was NULL.\r
2334 EFI_UNSUPPORTED The operation is unsupported.\r
2335\r
2336--*/\r
2337{\r
2338 EFI_IMAGE_DOS_HEADER *DosHeader;\r
2339 EFI_IMAGE_NT_HEADERS *NtHeader;\r
a651e231 2340 EFI_TE_IMAGE_HEADER *TeHeader;\r
878ddf1f 2341\r
2342 //\r
2343 // Verify input parameters\r
2344 //\r
2345 if (Pe32 == NULL) {\r
2346 return EFI_INVALID_PARAMETER;\r
2347 }\r
878ddf1f 2348\r
2349 //\r
a651e231 2350 // First check whether it is one TE Image.\r
878ddf1f 2351 //\r
a651e231
LG
2352 TeHeader = (EFI_TE_IMAGE_HEADER *) Pe32;\r
2353 if (TeHeader->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
2354 //\r
2355 // By TeImage Header to get output\r
2356 //\r
2357 *EntryPoint = TeHeader->AddressOfEntryPoint + sizeof (EFI_TE_IMAGE_HEADER) - TeHeader->StrippedSize;\r
2358 *BaseOfCode = TeHeader->BaseOfCode + sizeof (EFI_TE_IMAGE_HEADER) - TeHeader->StrippedSize;\r
2359 *MachineType = TeHeader->Machine;\r
2360 } else {\r
2361 \r
2362 //\r
2363 // Then check whether \r
2364 // First is the DOS header\r
2365 //\r
2366 DosHeader = (EFI_IMAGE_DOS_HEADER *) Pe32;\r
2367 \r
2368 //\r
2369 // Verify DOS header is expected\r
2370 //\r
2371 if (DosHeader->e_magic != EFI_IMAGE_DOS_SIGNATURE) {\r
2372 printf ("ERROR: Unknown magic number in the DOS header, 0x%04X.\n", DosHeader->e_magic);\r
2373 return EFI_UNSUPPORTED;\r
2374 }\r
2375 //\r
2376 // Immediately following is the NT header.\r
2377 //\r
2378 NtHeader = (EFI_IMAGE_NT_HEADERS *) ((UINTN) Pe32 + DosHeader->e_lfanew);\r
2379 \r
2380 //\r
2381 // Verify NT header is expected\r
2382 //\r
2383 if (NtHeader->Signature != EFI_IMAGE_NT_SIGNATURE) {\r
2384 printf ("ERROR: Unrecognized image signature 0x%08X.\n", NtHeader->Signature);\r
2385 return EFI_UNSUPPORTED;\r
2386 }\r
2387 //\r
2388 // Get output\r
2389 //\r
2390 *EntryPoint = NtHeader->OptionalHeader.AddressOfEntryPoint;\r
2391 *BaseOfCode = NtHeader->OptionalHeader.BaseOfCode;\r
2392 *MachineType = NtHeader->FileHeader.Machine;\r
878ddf1f 2393 }\r
878ddf1f 2394\r
2395 //\r
2396 // Verify machine type is supported\r
2397 //\r
2398 if (*MachineType != EFI_IMAGE_MACHINE_IA32 && *MachineType != EFI_IMAGE_MACHINE_IA64 && *MachineType != EFI_IMAGE_MACHINE_X64 && *MachineType != EFI_IMAGE_MACHINE_EBC) {\r
2399 printf ("ERROR: Unrecognized machine type in the PE32 file.\n");\r
2400 return EFI_UNSUPPORTED;\r
2401 }\r
2402\r
2403 return EFI_SUCCESS;\r
2404}\r
2405//\r
2406// Exposed function implementations (prototypes are defined in GenFvImageLib.h)\r
2407//\r
2408EFI_STATUS\r
2409GenerateFvImage (\r
2410 IN CHAR8 *InfFileImage,\r
2411 IN UINTN InfFileSize,\r
2412 OUT UINT8 **FvImage,\r
2413 OUT UINTN *FvImageSize,\r
2414 OUT CHAR8 **FvFileName,\r
2415 OUT UINT8 **SymImage,\r
2416 OUT UINTN *SymImageSize,\r
2417 OUT CHAR8 **SymFileName\r
2418 )\r
2419/*++\r
2420\r
2421Routine Description:\r
2422\r
2423 This is the main function which will be called from application.\r
2424\r
2425Arguments:\r
2426\r
2427 InfFileImage Buffer containing the INF file contents.\r
2428 InfFileSize Size of the contents of the InfFileImage buffer.\r
2429 FvImage Pointer to the FV image created.\r
2430 FvImageSize Size of the FV image created and pointed to by FvImage.\r
2431 FvFileName Requested name for the FV file.\r
2432 SymImage Pointer to the Sym image created.\r
2433 SymImageSize Size of the Sym image created and pointed to by SymImage.\r
2434 SymFileName Requested name for the Sym file.\r
2435\r
2436Returns:\r
2437\r
2438 EFI_SUCCESS Function completed successfully.\r
2439 EFI_OUT_OF_RESOURCES Could not allocate required resources.\r
2440 EFI_ABORTED Error encountered.\r
2441 EFI_INVALID_PARAMETER A required parameter was NULL.\r
2442\r
2443--*/\r
2444{\r
2445 EFI_STATUS Status;\r
2446 MEMORY_FILE InfMemoryFile;\r
2447 MEMORY_FILE FvImageMemoryFile;\r
2448 MEMORY_FILE SymImageMemoryFile;\r
2449 FV_INFO FvInfo;\r
2450 UINTN Index;\r
2451 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
2452 EFI_FFS_FILE_HEADER *VtfFileImage;\r
2453\r
2454 //\r
2455 // Check for invalid parameter\r
2456 //\r
2457 if (InfFileImage == NULL || FvImage == NULL || FvImageSize == NULL || FvFileName == NULL) {\r
2458 return EFI_INVALID_PARAMETER;\r
2459 }\r
2460 //\r
2461 // Initialize file structures\r
2462 //\r
2463 InfMemoryFile.FileImage = InfFileImage;\r
2464 InfMemoryFile.CurrentFilePointer = InfFileImage;\r
2465 InfMemoryFile.Eof = InfFileImage + InfFileSize;\r
2466\r
2467 //\r
2468 // Parse the FV inf file for header information\r
2469 //\r
2470 Status = ParseFvInf (&InfMemoryFile, &FvInfo);\r
2471 if (EFI_ERROR (Status)) {\r
2472 printf ("ERROR: Could not parse the input INF file.\n");\r
2473 return EFI_ABORTED;\r
2474 }\r
2475 //\r
2476 // Update the file name return values\r
2477 //\r
2478 strcpy (*FvFileName, FvInfo.FvName);\r
2479 strcpy (*SymFileName, FvInfo.SymName);\r
2480\r
2481 //\r
2482 // Calculate the FV size\r
2483 //\r
2484 *FvImageSize = FvInfo.Size;\r
2485\r
2486 //\r
2487 // Allocate the FV\r
2488 //\r
2489 *FvImage = malloc (*FvImageSize);\r
2490 if (*FvImage == NULL) {\r
2491 return EFI_OUT_OF_RESOURCES;\r
2492 }\r
2493 //\r
2494 // Allocate space for symbol file storage\r
2495 //\r
2496 *SymImage = malloc (SYMBOL_FILE_SIZE);\r
2497 if (*SymImage == NULL) {\r
2498 return EFI_OUT_OF_RESOURCES;\r
2499 }\r
2500 //\r
2501 // Initialize the FV to the erase polarity\r
2502 //\r
2503 if (FvInfo.FvAttributes & EFI_FVB_ERASE_POLARITY) {\r
2504 memset (*FvImage, -1, *FvImageSize);\r
2505 } else {\r
2506 memset (*FvImage, 0, *FvImageSize);\r
2507 }\r
2508 //\r
2509 // Initialize FV header\r
2510 //\r
2511 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) *FvImage;\r
2512\r
2513 //\r
2514 // Initialize the zero vector to all zeros.\r
2515 //\r
2516 memset (FvHeader->ZeroVector, 0, 16);\r
2517\r
2518 //\r
2519 // Copy the FFS GUID\r
2520 //\r
2521 memcpy (&FvHeader->FileSystemGuid, &FvInfo.FvGuid, sizeof (EFI_GUID));\r
2522\r
2523 FvHeader->FvLength = *FvImageSize;\r
2524 FvHeader->Signature = EFI_FVH_SIGNATURE;\r
2525 FvHeader->Attributes = FvInfo.FvAttributes;\r
2526 FvHeader->Revision = EFI_FVH_REVISION;\r
2527 FvHeader->Reserved[0] = 0;\r
2528 FvHeader->Reserved[1] = 0;\r
2529 FvHeader->Reserved[2] = 0;\r
2530\r
2531 //\r
2532 // Copy firmware block map\r
2533 //\r
2534 for (Index = 0; FvInfo.FvBlocks[Index].NumBlocks != 0; Index++) {\r
2535 FvHeader->FvBlockMap[Index].NumBlocks = FvInfo.FvBlocks[Index].NumBlocks;\r
2536 FvHeader->FvBlockMap[Index].BlockLength = FvInfo.FvBlocks[Index].BlockLength;\r
2537 }\r
2538 //\r
2539 // Add block map terminator\r
2540 //\r
2541 FvHeader->FvBlockMap[Index].NumBlocks = 0;\r
2542 FvHeader->FvBlockMap[Index].BlockLength = 0;\r
2543\r
2544 //\r
2545 // Complete the header\r
2546 //\r
2547 FvHeader->HeaderLength = (UINT16) (((UINTN) &(FvHeader->FvBlockMap[Index + 1])) - (UINTN) *FvImage);\r
2548 FvHeader->Checksum = 0;\r
2549 FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));\r
2550\r
2551 //\r
2552 // If there is no FFS file, find and generate each components of the FV\r
2553 //\r
2554 if (FvInfo.FvFiles[0][0] == 0) {\r
2555 Status = GenNonFFSFv (*FvImage, &FvInfo);\r
2556 if (EFI_ERROR (Status)) {\r
2557 printf ("ERROR: Could not generate NonFFS FV.\n");\r
2558 free (*FvImage);\r
2559 return EFI_ABORTED;\r
2560 }\r
2561\r
2562 return EFI_SUCCESS;\r
2563 }\r
2564 //\r
2565 // Initialize our "file" view of the buffer\r
2566 //\r
2567 FvImageMemoryFile.FileImage = *FvImage;\r
2568 FvImageMemoryFile.CurrentFilePointer = *FvImage + FvHeader->HeaderLength;\r
2569 FvImageMemoryFile.Eof = *FvImage +*FvImageSize;\r
2570\r
2571 //\r
2572 // Initialize our "file" view of the symbol file.\r
2573 //\r
2574 SymImageMemoryFile.FileImage = *SymImage;\r
2575 SymImageMemoryFile.CurrentFilePointer = *SymImage;\r
2576 SymImageMemoryFile.Eof = *FvImage + SYMBOL_FILE_SIZE;\r
2577\r
2578 //\r
2579 // Initialize the FV library.\r
2580 //\r
2581 InitializeFvLib (FvImageMemoryFile.FileImage, FvInfo.Size);\r
2582\r
2583 //\r
2584 // Files start on 8 byte alignments, so move to the next 8 byte aligned\r
2585 // address. For now, just assert if it isn't. Currently FV header is\r
2586 // always a multiple of 8 bytes.\r
2587 // BUGBUG: Handle this better\r
2588 //\r
2589 assert ((((UINTN) FvImageMemoryFile.CurrentFilePointer) % 8) == 0);\r
2590\r
2591 //\r
2592 // Initialize the VTF file address.\r
2593 //\r
2594 VtfFileImage = (EFI_FFS_FILE_HEADER *) FvImageMemoryFile.Eof;\r
2595\r
2596 //\r
2597 // Add files to FV\r
2598 //\r
2599 for (Index = 0; FvInfo.FvFiles[Index][0] != 0; Index++) {\r
2600 //\r
2601 // Add the file\r
2602 //\r
2603 Status = AddFile (&FvImageMemoryFile, &FvInfo, Index, &VtfFileImage, &SymImageMemoryFile);\r
2604\r
2605 //\r
2606 // Exit if error detected while adding the file\r
2607 //\r
2608 if (EFI_ERROR (Status)) {\r
2609 printf ("ERROR: Could not add file %s.\n", FvInfo.FvFiles[Index]);\r
2610 free (*FvImage);\r
2611 return EFI_ABORTED;\r
2612 }\r
2613 }\r
2614 //\r
2615 // If there is a VTF file, some special actions need to occur.\r
2616 //\r
2617 if ((UINTN) VtfFileImage != (UINTN) FvImageMemoryFile.Eof) {\r
2618 //\r
2619 // Pad from the end of the last file to the beginning of the VTF file.\r
2620 //\r
2621 Status = PadFvImage (&FvImageMemoryFile, VtfFileImage);\r
2622 if (EFI_ERROR (Status)) {\r
2623 printf ("ERROR: Could not create the pad file between the last file and the VTF file.\n");\r
2624 free (*FvImage);\r
2625 return EFI_ABORTED;\r
2626 }\r
2627 //\r
2628 // Update reset vector (SALE_ENTRY for IPF)\r
2629 // Now for IA32 and IA64 platform, the fv which has bsf file must have the \r
2630 // EndAddress of 0xFFFFFFFF. Thus, only this type fv needs to update the \r
2631 // reset vector. If the PEI Core is found, the VTF file will probably get \r
2632 // corrupted by updating the entry point. \r
2633 //\r
2634 if ((FvInfo.BaseAddress + FvInfo.Size) == FV_IMAGES_TOP_ADDRESS) { \r
2635 Status = UpdateResetVector (&FvImageMemoryFile, &FvInfo, VtfFileImage);\r
2636 if (EFI_ERROR(Status)) { \r
2637 printf ("ERROR: Could not update the reset vector.\n"); \r
2638 free (*FvImage); \r
2639 return EFI_ABORTED; \r
2640 } \r
2641 }\r
2642 } \r
2643 //\r
2644 // Determine final Sym file size\r
2645 //\r
2646 *SymImageSize = SymImageMemoryFile.CurrentFilePointer - SymImageMemoryFile.FileImage;\r
2647\r
2648 return EFI_SUCCESS;\r
2649}\r
2650\r
2651EFI_STATUS\r
2652UpdatePeiCoreEntryInFit (\r
2653 IN FIT_TABLE *FitTablePtr,\r
2654 IN UINT64 PeiCorePhysicalAddress\r
2655 )\r
2656/*++\r
2657\r
2658Routine Description:\r
2659\r
2660 This function is used to update the Pei Core address in FIT, this can be used by Sec core to pass control from\r
2661 Sec to Pei Core\r
2662\r
2663Arguments:\r
2664\r
2665 FitTablePtr - The pointer of FIT_TABLE.\r
2666 PeiCorePhysicalAddress - The address of Pei Core entry.\r
2667\r
2668Returns:\r
2669\r
2670 EFI_SUCCESS - The PEI_CORE FIT entry was updated successfully.\r
2671 EFI_NOT_FOUND - Not found the PEI_CORE FIT entry.\r
2672\r
2673--*/\r
2674{\r
2675 FIT_TABLE *TmpFitPtr;\r
2676 UINTN Index;\r
2677 UINTN NumFitComponents;\r
2678\r
2679 TmpFitPtr = FitTablePtr;\r
2680 NumFitComponents = TmpFitPtr->CompSize;\r
2681\r
2682 for (Index = 0; Index < NumFitComponents; Index++) {\r
2683 if ((TmpFitPtr->CvAndType & FIT_TYPE_MASK) == COMP_TYPE_FIT_PEICORE) {\r
2684 TmpFitPtr->CompAddress = PeiCorePhysicalAddress;\r
2685 return EFI_SUCCESS;\r
2686 }\r
2687\r
2688 TmpFitPtr++;\r
2689 }\r
2690\r
2691 return EFI_NOT_FOUND;\r
2692}\r
2693\r
2694VOID\r
2695UpdateFitCheckSum (\r
2696 IN FIT_TABLE *FitTablePtr\r
2697 )\r
2698/*++\r
2699\r
2700Routine Description:\r
2701\r
2702 This function is used to update the checksum for FIT.\r
2703\r
2704\r
2705Arguments:\r
2706\r
2707 FitTablePtr - The pointer of FIT_TABLE.\r
2708\r
2709Returns:\r
2710\r
2711 None.\r
2712\r
2713--*/\r
2714{\r
2715 if ((FitTablePtr->CvAndType & CHECKSUM_BIT_MASK) >> 7) {\r
2716 FitTablePtr->CheckSum = 0;\r
2717 FitTablePtr->CheckSum = CalculateChecksum8 ((UINT8 *) FitTablePtr, FitTablePtr->CompSize * 16);\r
2718 }\r
2719}\r