]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/CCode/Source/GenFvImage/GenFvImageLib.c
Modify GenFvImage tool to record the largest alignment of all the FFS files in the...
[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
d3f75347 51static UINT32 MaxFfsAlignment = 0;\r
878ddf1f 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
d3f75347 1034 // 8 byte alignment, mini alignment requirement for FFS file. \r
878ddf1f 1035 //\r
d3f75347 1036 *Alignment = (1 << 3);\r
878ddf1f 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
d3f75347
LG
1744 \r
1745 //\r
1746 // Find the largest alignment of all the FFS files in the FV\r
1747 //\r
1748 if (CurrentFileAlignment > MaxFfsAlignment) {\r
1749 MaxFfsAlignment = CurrentFileAlignment;\r
1750 }\r
878ddf1f 1751 //\r
1752 // Add pad file if necessary\r
1753 //\r
1754 Status = AddPadFile (FvImage, CurrentFileAlignment);\r
1755 if (EFI_ERROR (Status)) {\r
1756 printf ("ERROR: Could not align the file data properly.\n");\r
1757 free (FileBuffer);\r
1758 return EFI_ABORTED;\r
1759 }\r
1760 //\r
1761 // Add file\r
1762 //\r
1763 if ((FvImage->CurrentFilePointer + FileSize) < FvImage->Eof) {\r
1764 //\r
1765 // Copy the file\r
1766 //\r
1767 memcpy (FvImage->CurrentFilePointer, FileBuffer, FileSize);\r
1768\r
1769 //\r
1770 // If the file is XIP, rebase\r
1771 //\r
1772 CurrentFileBaseAddress = FvInfo->BaseAddress + ((UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage);\r
1773 //\r
1774 // Status = RebaseFfsFile ((EFI_FFS_FILE_HEADER*) FvImage->CurrentFilePointer, CurrentFileBaseAddress);\r
1775 // if (EFI_ERROR(Status)) {\r
1776 // printf ("ERROR: Could not rebase the file %s.\n", FvInfo->FvFiles[Index]);\r
1777 // return EFI_ABORTED;\r
1778 // }\r
1779 //\r
1780 // Update Symbol file\r
1781 //\r
1782 Status = AddSymFile (\r
1783 CurrentFileBaseAddress,\r
1784 (EFI_FFS_FILE_HEADER *) FvImage->CurrentFilePointer,\r
1785 SymImage,\r
1786 FvInfo->FvFiles[Index]\r
1787 );\r
1788 assert (!EFI_ERROR (Status));\r
1789\r
1790 //\r
1791 // Update the current pointer in the FV image\r
1792 //\r
1793 FvImage->CurrentFilePointer += FileSize;\r
1794 } else {\r
1795 printf ("ERROR: The firmware volume is out of space, could not add file %s.\n", FvInfo->FvFiles[Index]);\r
1796 return EFI_ABORTED;\r
1797 }\r
1798 //\r
1799 // Make next file start at QWord Boundry\r
1800 //\r
1801 while (((UINTN) FvImage->CurrentFilePointer & 0x07) != 0) {\r
1802 FvImage->CurrentFilePointer++;\r
1803 }\r
1804 //\r
1805 // Free allocated memory.\r
1806 //\r
1807 free (FileBuffer);\r
1808\r
1809 return EFI_SUCCESS;\r
1810}\r
1811\r
1812EFI_STATUS\r
1813AddVariableBlock (\r
1814 IN UINT8 *FvImage,\r
1815 IN UINTN Size,\r
1816 IN FV_INFO *FvInfo\r
1817 )\r
1818{\r
1819 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
1820 VARIABLE_STORE_HEADER *VarStoreHeader;\r
1821 //\r
1822 // Variable block should exclude FvHeader. Since the length of\r
1823 // FvHeader depends on the block map, which is variable length,\r
1824 // we could only decide the actual variable block length here.\r
1825 //\r
1826 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) FvImage;\r
1827 FvImage = FvImage + FvHeader->HeaderLength;\r
1828\r
1829 VarStoreHeader = (VARIABLE_STORE_HEADER *) FvImage;\r
1830\r
1831 VarStoreHeader->Signature = VARIABLE_STORE_SIGNATURE;\r
1832 VarStoreHeader->Size = Size - FvHeader->HeaderLength;\r
1833 VarStoreHeader->Format = VARIABLE_STORE_FORMATTED;\r
1834 VarStoreHeader->State = VARIABLE_STORE_HEALTHY;\r
1835 VarStoreHeader->Reserved = 0;\r
1836 VarStoreHeader->Reserved1 = 0;\r
1837\r
1838 return EFI_SUCCESS;\r
1839}\r
1840\r
1841EFI_STATUS\r
1842AddEventLogBlock (\r
1843 IN UINT8 *FvImage,\r
1844 IN UINTN Size,\r
1845 IN FV_INFO *FvInfo\r
1846 )\r
1847{\r
1848 return EFI_SUCCESS;\r
1849}\r
1850\r
1851EFI_STATUS\r
1852AddFTWWorkingBlock (\r
1853 IN UINT8 *FvImage,\r
1854 IN UINTN Size,\r
1855 IN FV_INFO *FvInfo\r
1856 )\r
1857{\r
1858 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *FTWHeader;\r
1859 UINT32 Crc32;\r
1860\r
1861 Crc32 = 0;\r
1862 FTWHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) FvImage;\r
1863 memcpy (&FTWHeader->Signature, &(FvInfo->FvGuid), sizeof (EFI_GUID));\r
1864 FTWHeader->WriteQueueSize = Size - sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER);\r
1865 CalculateCrc32 (FvImage, sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER), &Crc32);\r
1866 FTWHeader->Crc = Crc32;\r
1867 if (FvInfo->FvAttributes & EFI_FVB_ERASE_POLARITY) {\r
1868 FTWHeader->WorkingBlockValid = 0;\r
1869 FTWHeader->WorkingBlockInvalid = 1;\r
1870 } else {\r
1871 FTWHeader->WorkingBlockValid = 1;\r
1872 FTWHeader->WorkingBlockInvalid = 0;\r
1873 }\r
1874\r
1875 return EFI_SUCCESS;\r
1876}\r
1877\r
1878EFI_STATUS\r
1879AddFTWSpareBlock (\r
1880 IN UINT8 *FvImage,\r
1881 IN UINTN Size,\r
1882 IN FV_INFO *FvInfo\r
1883 )\r
1884{\r
1885 return EFI_SUCCESS;\r
1886}\r
1887\r
1888EFI_STATUS\r
1889GenNonFFSFv (\r
1890 IN UINT8 *FvImage,\r
1891 IN FV_INFO *FvInfo\r
1892 )\r
1893/*++\r
1894\r
1895Routine Description:\r
1896\r
1897 This function generate the non FFS FV image, such as the working block\r
1898 and spare block. How each component of the FV is built is component\r
1899 specific.\r
1900\r
1901Arguments:\r
1902\r
1903 FvImage The memory image of the FV to add it to. The current offset\r
1904 must be valid.\r
1905 FvInfo Pointer to information about the FV.\r
1906\r
1907Returns:\r
1908\r
1909 EFI_SUCCESS The function completed successfully.\r
1910 EFI_INVALID_PARAMETER One of the input parameters was invalid.\r
1911 EFI_ABORTED An error occurred.\r
1912 EFI_OUT_OF_RESOURCES Insufficient resources exist to complete the add.\r
1913\r
1914--*/\r
1915{\r
1916 UINTN Index;\r
1917 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
1918 UINT64 TotalSize;\r
1919\r
1920 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) FvImage;\r
1921 TotalSize = 0;\r
1922\r
1923 for (Index = 0; FvInfo->FvComponents[Index].Size != 0; Index++) {\r
1924 if (stricmp (FvInfo->FvComponents[Index].ComponentName, EFI_NV_VARIABLE_STRING) == 0) {\r
1925 AddVariableBlock (FvImage, FvInfo->FvComponents[Index].Size, FvInfo);\r
1926 } else if (stricmp (FvInfo->FvComponents[Index].ComponentName, EFI_NV_EVENT_LOG_STRING) == 0) {\r
1927 AddEventLogBlock (FvImage, FvInfo->FvComponents[Index].Size, FvInfo);\r
1928 } else if (stricmp (FvInfo->FvComponents[Index].ComponentName, EFI_NV_FTW_WORKING_STRING) == 0) {\r
1929 AddFTWWorkingBlock (FvImage, FvInfo->FvComponents[Index].Size, FvInfo);\r
1930 } else if (stricmp (FvInfo->FvComponents[Index].ComponentName, EFI_NV_FTW_SPARE_STRING) == 0) {\r
1931 AddFTWSpareBlock (FvImage, FvInfo->FvComponents[Index].Size, FvInfo);\r
1932 } else {\r
1933 printf ("Error. Unknown Non-FFS block %s \n", FvInfo->FvComponents[Index].ComponentName);\r
1934 return EFI_ABORTED;\r
1935 }\r
1936\r
1937 FvImage = FvImage + FvInfo->FvComponents[Index].Size;\r
1938 TotalSize = TotalSize + FvInfo->FvComponents[Index].Size;\r
1939 }\r
1940 //\r
1941 // Index and TotalSize is zero mean there's no component, so this is an empty fv\r
1942 //\r
1943 if ((Index != 0 || TotalSize != 0) && TotalSize != FvInfo->Size) {\r
1944 printf ("Error. Component size does not sum up to FV size.\n");\r
1945 return EFI_ABORTED;\r
1946 }\r
1947\r
1948 return EFI_SUCCESS;\r
1949}\r
1950\r
1951EFI_STATUS\r
1952PadFvImage (\r
1953 IN MEMORY_FILE *FvImage,\r
1954 IN EFI_FFS_FILE_HEADER *VtfFileImage\r
1955 )\r
1956/*++\r
1957\r
1958Routine Description:\r
1959\r
1960 This function places a pad file between the last file in the FV and the VTF\r
1961 file if the VTF file exists.\r
1962\r
1963Arguments:\r
1964\r
1965 FvImage Memory file for the FV memory image\r
1966 VtfFileImage The address of the VTF file. If this is the end of the FV\r
1967 image, no VTF exists and no pad file is needed.\r
1968\r
1969Returns:\r
1970\r
1971 EFI_SUCCESS Completed successfully.\r
1972 EFI_INVALID_PARAMETER One of the input parameters was NULL.\r
1973\r
1974--*/\r
1975{\r
1976 EFI_FFS_FILE_HEADER *PadFile;\r
1977 UINTN FileSize;\r
1978\r
1979 //\r
1980 // If there is no VTF or the VTF naturally follows the previous file without a\r
1981 // pad file, then there's nothing to do\r
1982 //\r
1983 if ((UINTN) VtfFileImage == (UINTN) FvImage->Eof || (void *) FvImage->CurrentFilePointer == (void *) VtfFileImage) {\r
1984 return EFI_SUCCESS;\r
1985 }\r
1986 //\r
1987 // Pad file starts at beginning of free space\r
1988 //\r
1989 PadFile = (EFI_FFS_FILE_HEADER *) FvImage->CurrentFilePointer;\r
1990\r
1991 //\r
1992 // write header\r
1993 //\r
1994 memset (PadFile, 0, sizeof (EFI_FFS_FILE_HEADER));\r
1995 memcpy (&PadFile->Name, &DefaultFvPadFileNameGuid, sizeof (EFI_GUID));\r
1996 PadFile->Type = EFI_FV_FILETYPE_FFS_PAD;\r
1997 PadFile->Attributes = 0;\r
1998\r
1999 //\r
2000 // FileSize includes the EFI_FFS_FILE_HEADER\r
2001 //\r
2002 FileSize = (UINTN) VtfFileImage - (UINTN) FvImage->CurrentFilePointer;\r
2003 PadFile->Size[0] = (UINT8) (FileSize & 0x000000FF);\r
2004 PadFile->Size[1] = (UINT8) ((FileSize & 0x0000FF00) >> 8);\r
2005 PadFile->Size[2] = (UINT8) ((FileSize & 0x00FF0000) >> 16);\r
2006\r
2007 //\r
2008 // Fill in checksums and state, must be zero during checksum calculation.\r
2009 //\r
2010 PadFile->IntegrityCheck.Checksum.Header = 0;\r
2011 PadFile->IntegrityCheck.Checksum.File = 0;\r
2012 PadFile->State = 0;\r
2013 PadFile->IntegrityCheck.Checksum.Header = CalculateChecksum8 ((UINT8 *) PadFile, sizeof (EFI_FFS_FILE_HEADER));\r
2014 if (PadFile->Attributes & FFS_ATTRIB_CHECKSUM) {\r
2015 PadFile->IntegrityCheck.Checksum.File = CalculateChecksum8 ((UINT8 *) PadFile, FileSize);\r
2016 } else {\r
2017 PadFile->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;\r
2018 }\r
2019\r
2020 PadFile->State = EFI_FILE_HEADER_CONSTRUCTION | EFI_FILE_HEADER_VALID | EFI_FILE_DATA_VALID;\r
2021\r
2022 UpdateFfsFileState (\r
2023 (EFI_FFS_FILE_HEADER *) PadFile,\r
2024 (EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage\r
2025 );\r
2026 //\r
2027 // Update the current FV pointer\r
2028 //\r
2029 FvImage->CurrentFilePointer = FvImage->Eof;\r
2030\r
2031 return EFI_SUCCESS;\r
2032}\r
2033\r
2034EFI_STATUS\r
2035UpdateResetVector (\r
2036 IN MEMORY_FILE *FvImage,\r
2037 IN FV_INFO *FvInfo,\r
2038 IN EFI_FFS_FILE_HEADER *VtfFile\r
2039 )\r
2040/*++\r
2041\r
2042Routine Description:\r
2043\r
2044 This parses the FV looking for the PEI core and then plugs the address into\r
2045 the SALE_ENTRY point of the BSF/VTF for IPF and does BUGBUG TBD action to\r
2046 complete an IA32 Bootstrap FV.\r
2047\r
2048Arguments:\r
2049\r
2050 FvImage Memory file for the FV memory image\r
2051 FvInfo Information read from INF file.\r
2052 VtfFile Pointer to the VTF file in the FV image.\r
2053\r
2054Returns:\r
2055\r
2056 EFI_SUCCESS Function Completed successfully.\r
2057 EFI_ABORTED Error encountered.\r
2058 EFI_INVALID_PARAMETER A required parameter was NULL.\r
2059 EFI_NOT_FOUND PEI Core file not found.\r
2060\r
2061--*/\r
2062{\r
2063 EFI_FFS_FILE_HEADER *PeiCoreFile;\r
2064 EFI_FFS_FILE_HEADER *SecCoreFile;\r
2065 EFI_STATUS Status;\r
2066 EFI_FILE_SECTION_POINTER Pe32Section;\r
2067 UINT32 EntryPoint;\r
2068 UINT32 BaseOfCode;\r
2069 UINT16 MachineType;\r
2070 EFI_PHYSICAL_ADDRESS PeiCorePhysicalAddress;\r
2071 EFI_PHYSICAL_ADDRESS SecCorePhysicalAddress;\r
2072 EFI_PHYSICAL_ADDRESS *SecCoreEntryAddressPtr;\r
2073 UINT32 *Ia32ResetAddressPtr;\r
2074 UINT8 *BytePointer;\r
2075 UINT8 *BytePointer2;\r
2076 UINT16 *WordPointer;\r
2077 UINT16 CheckSum;\r
2078 UINTN Index;\r
2079 EFI_FFS_FILE_STATE SavedState;\r
2080 EFI_FFS_FILE_TAIL TailValue;\r
2081 UINT32 TailSize;\r
2082 UINT64 FitAddress;\r
2083 FIT_TABLE *FitTablePtr;\r
2084\r
2085 //\r
2086 // Verify input parameters\r
2087 //\r
2088 if (FvImage == NULL || FvInfo == NULL || VtfFile == NULL) {\r
2089 return EFI_INVALID_PARAMETER;\r
2090 }\r
2091 //\r
2092 // Initialize FV library\r
2093 //\r
2094 InitializeFvLib (FvImage->FileImage, (UINTN) FvImage->Eof - (UINTN) FvImage->FileImage);\r
2095\r
2096 //\r
2097 // Verify VTF file\r
2098 //\r
2099 Status = VerifyFfsFile (VtfFile);\r
2100 if (EFI_ERROR (Status)) {\r
2101 return EFI_INVALID_PARAMETER;\r
2102 }\r
2103 //\r
2104 // Find the PEI Core\r
2105 //\r
2106 Status = GetFileByType (EFI_FV_FILETYPE_PEI_CORE, 1, &PeiCoreFile);\r
2107 if (EFI_ERROR (Status) || PeiCoreFile == NULL) {\r
2108 Error (NULL, 0, 0, "could not find the PEI core in the FV", NULL);\r
2109 return EFI_ABORTED;\r
2110 }\r
a651e231 2111\r
878ddf1f 2112 //\r
a651e231 2113 // PEI Core found, now find PE32 or TE section\r
878ddf1f 2114 //\r
2115 Status = GetSectionByType (PeiCoreFile, EFI_SECTION_PE32, 1, &Pe32Section);\r
a651e231
LG
2116 if (Status == EFI_NOT_FOUND) {\r
2117 Status = GetSectionByType (PeiCoreFile, EFI_SECTION_TE, 1, &Pe32Section);\r
2118 }\r
2119\r
878ddf1f 2120 if (EFI_ERROR (Status)) {\r
a651e231 2121 Error (NULL, 0, 0, "could not find PE32 or TE section in PEI core file", NULL);\r
878ddf1f 2122 return EFI_ABORTED;\r
2123 }\r
2124\r
2125 Status = GetPe32Info (\r
2126 (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),\r
2127 &EntryPoint,\r
2128 &BaseOfCode,\r
2129 &MachineType\r
2130 );\r
a651e231 2131\r
878ddf1f 2132 if (EFI_ERROR (Status)) {\r
2133 Error (NULL, 0, 0, "could not get PE32 entry point for PEI core", NULL);\r
2134 return EFI_ABORTED;\r
2135 }\r
2136 //\r
2137 // Physical address is FV base + offset of PE32 + offset of the entry point\r
2138 //\r
2139 PeiCorePhysicalAddress = FvInfo->BaseAddress;\r
2140 PeiCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32) - (UINTN) FvImage->FileImage;\r
2141 PeiCorePhysicalAddress += EntryPoint;\r
2142\r
2143 if (MachineType == EFI_IMAGE_MACHINE_IA64) {\r
2144 //\r
2145 // Update PEI_CORE address\r
2146 //\r
2147 //\r
2148 // Set the uncached attribute bit in the physical address\r
2149 //\r
2150 PeiCorePhysicalAddress |= 0x8000000000000000ULL;\r
2151\r
2152 //\r
2153 // Check if address is aligned on a 16 byte boundary\r
2154 //\r
2155 if (PeiCorePhysicalAddress & 0xF) {\r
2156 printf (\r
2157 "ERROR: PEI_CORE entry point is not aligned on a 16 byte boundary, address specified is %Xh.\n",\r
2158 PeiCorePhysicalAddress\r
2159 );\r
2160 return EFI_ABORTED;\r
2161 }\r
2162 //\r
2163 // First Get the FIT table address\r
2164 //\r
2165 FitAddress = (*(UINT64 *) (FvImage->Eof - IPF_FIT_ADDRESS_OFFSET)) & 0xFFFFFFFF;\r
2166\r
2167 FitTablePtr = (FIT_TABLE *) (FvImage->FileImage + (FitAddress - FvInfo->BaseAddress));\r
2168\r
2169 Status = UpdatePeiCoreEntryInFit (FitTablePtr, PeiCorePhysicalAddress);\r
2170\r
2171 if (!EFI_ERROR (Status)) {\r
2172 UpdateFitCheckSum (FitTablePtr);\r
2173 }\r
2174 //\r
2175 // Find the Sec Core\r
2176 //\r
2177 Status = GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE, 1, &SecCoreFile);\r
2178 if (EFI_ERROR (Status) || SecCoreFile == NULL) {\r
2179 Error (NULL, 0, 0, "could not find the Sec core in the FV", NULL);\r
2180 return EFI_ABORTED;\r
2181 }\r
2182 //\r
2183 // Sec Core found, now find PE32 section\r
2184 //\r
2185 Status = GetSectionByType (SecCoreFile, EFI_SECTION_PE32, 1, &Pe32Section);\r
2186 if (EFI_ERROR (Status)) {\r
2187 Error (NULL, 0, 0, "could not find PE32 section in SEC core file", NULL);\r
2188 return EFI_ABORTED;\r
2189 }\r
2190\r
2191 Status = GetPe32Info (\r
2192 (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),\r
2193 &EntryPoint,\r
2194 &BaseOfCode,\r
2195 &MachineType\r
2196 );\r
2197 if (EFI_ERROR (Status)) {\r
2198 Error (NULL, 0, 0, "could not get PE32 entry point for SEC core", NULL);\r
2199 return EFI_ABORTED;\r
2200 }\r
2201 //\r
2202 // Physical address is FV base + offset of PE32 + offset of the entry point\r
2203 //\r
2204 SecCorePhysicalAddress = FvInfo->BaseAddress;\r
2205 SecCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32) - (UINTN) FvImage->FileImage;\r
2206 SecCorePhysicalAddress += EntryPoint;\r
2207\r
2208 //\r
2209 // Update SEC_CORE address\r
2210 //\r
2211 //\r
2212 // Set the uncached attribute bit in the physical address\r
2213 //\r
2214 SecCorePhysicalAddress |= 0x8000000000000000ULL;\r
2215\r
2216 //\r
2217 // Update the address\r
2218 //\r
2219 SecCoreEntryAddressPtr = (EFI_PHYSICAL_ADDRESS *) ((UINTN) FvImage->Eof - IPF_SALE_ENTRY_ADDRESS_OFFSET);\r
2220 *SecCoreEntryAddressPtr = SecCorePhysicalAddress;\r
2221\r
2222 //\r
2223 // Check if address is aligned on a 16 byte boundary\r
2224 //\r
2225 if (SecCorePhysicalAddress & 0xF) {\r
2226 printf (\r
2227 "ERROR: SALE_ENTRY entry point is not aligned on a 16 byte boundary, address specified is %Xh.\n",\r
2228 SecCorePhysicalAddress\r
2229 );\r
2230 return EFI_ABORTED;\r
2231 }\r
2232 } else if (MachineType == EFI_IMAGE_MACHINE_IA32) {\r
2233 //\r
2234 // Get the location to update\r
2235 //\r
2236 Ia32ResetAddressPtr = (UINT32 *) ((UINTN) FvImage->Eof - IA32_PEI_CORE_ENTRY_OFFSET);\r
2237\r
2238 //\r
2239 // Write lower 32 bits of physical address\r
2240 //\r
2241 *Ia32ResetAddressPtr = (UINT32) PeiCorePhysicalAddress;\r
2242\r
2243 //\r
2244 // Update the BFV base address\r
2245 //\r
2246 Ia32ResetAddressPtr = (UINT32 *) ((UINTN) FvImage->Eof - 4);\r
2247 *Ia32ResetAddressPtr = (UINT32) (FvInfo->BaseAddress);\r
2248\r
2249 CheckSum = 0x0000;\r
2250\r
2251 //\r
2252 // Update the Startup AP in the FVH header block ZeroVector region.\r
2253 //\r
2254 BytePointer = (UINT8 *) ((UINTN) FvImage->FileImage);\r
2255 BytePointer2 = (FvInfo->Size == 0x10000) ? m64kRecoveryStartupApDataArray : m128kRecoveryStartupApDataArray;\r
2256 for (Index = 0; Index < SIZEOF_STARTUP_DATA_ARRAY; Index++) {\r
2257 *BytePointer++ = *BytePointer2++;\r
2258 }\r
2259 //\r
2260 // Calculate the checksum\r
2261 //\r
2262 WordPointer = (UINT16 *) ((UINTN) FvImage->FileImage);\r
2263 for (Index = 0; Index < SIZEOF_STARTUP_DATA_ARRAY / 2; Index++) {\r
2264 CheckSum = (UINT16) (CheckSum + ((UINT16) *WordPointer));\r
2265 WordPointer++;\r
2266 }\r
2267 //\r
2268 // Update the checksum field\r
2269 //\r
2270 BytePointer = (UINT8 *) ((UINTN) FvImage->FileImage);\r
2271 BytePointer += (SIZEOF_STARTUP_DATA_ARRAY - 2);\r
2272 WordPointer = (UINT16 *) BytePointer;\r
2273 *WordPointer = (UINT16) (0x10000 - (UINT32) CheckSum);\r
2274 } else {\r
2275 Error (NULL, 0, 0, "invalid machine type in PEI core", "machine type=0x%X", (UINT32) MachineType);\r
2276 return EFI_ABORTED;\r
2277 }\r
2278 //\r
2279 // Determine if it has an FFS file tail.\r
2280 //\r
2281 if (VtfFile->Attributes & FFS_ATTRIB_TAIL_PRESENT) {\r
2282 TailSize = sizeof (EFI_FFS_FILE_TAIL);\r
2283 } else {\r
2284 TailSize = 0;\r
2285 }\r
2286 //\r
2287 // Now update file checksum\r
2288 //\r
2289 SavedState = VtfFile->State;\r
2290 VtfFile->IntegrityCheck.Checksum.File = 0;\r
2291 VtfFile->State = 0;\r
2292 if (VtfFile->Attributes & FFS_ATTRIB_CHECKSUM) {\r
2293 VtfFile->IntegrityCheck.Checksum.File = CalculateChecksum8 (\r
2294 (UINT8 *) VtfFile,\r
2295 GetLength (VtfFile->Size) - TailSize\r
2296 );\r
2297 } else {\r
2298 VtfFile->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;\r
2299 }\r
2300\r
2301 VtfFile->State = SavedState;\r
2302\r
2303 //\r
2304 // Update tail if present\r
2305 //\r
2306 if (VtfFile->Attributes & FFS_ATTRIB_TAIL_PRESENT) {\r
2307 TailValue = (EFI_FFS_FILE_TAIL) (~(VtfFile->IntegrityCheck.TailReference));\r
2308 *(EFI_FFS_FILE_TAIL *) (((UINTN) (VtfFile) + GetLength (VtfFile->Size) - sizeof (EFI_FFS_FILE_TAIL))) = TailValue;\r
2309 }\r
2310\r
2311 return EFI_SUCCESS;\r
2312}\r
2313\r
2314EFI_STATUS\r
2315GetPe32Info (\r
2316 IN UINT8 *Pe32,\r
2317 OUT UINT32 *EntryPoint,\r
2318 OUT UINT32 *BaseOfCode,\r
2319 OUT UINT16 *MachineType\r
2320 )\r
2321/*++\r
2322\r
2323Routine Description:\r
2324\r
a651e231
LG
2325 Retrieves the PE32 entry point offset and machine type from PE image or TeImage. \r
2326 See EfiImage.h for machine types. The entry point offset is from the beginning \r
2327 of the PE32 buffer passed in.\r
878ddf1f 2328\r
2329Arguments:\r
2330\r
2331 Pe32 Beginning of the PE32.\r
2332 EntryPoint Offset from the beginning of the PE32 to the image entry point.\r
2333 BaseOfCode Base address of code.\r
2334 MachineType Magic number for the machine type.\r
2335\r
2336Returns:\r
2337\r
2338 EFI_SUCCESS Function completed successfully.\r
2339 EFI_ABORTED Error encountered.\r
2340 EFI_INVALID_PARAMETER A required parameter was NULL.\r
2341 EFI_UNSUPPORTED The operation is unsupported.\r
2342\r
2343--*/\r
2344{\r
2345 EFI_IMAGE_DOS_HEADER *DosHeader;\r
2346 EFI_IMAGE_NT_HEADERS *NtHeader;\r
a651e231 2347 EFI_TE_IMAGE_HEADER *TeHeader;\r
878ddf1f 2348\r
2349 //\r
2350 // Verify input parameters\r
2351 //\r
2352 if (Pe32 == NULL) {\r
2353 return EFI_INVALID_PARAMETER;\r
2354 }\r
878ddf1f 2355\r
2356 //\r
a651e231 2357 // First check whether it is one TE Image.\r
878ddf1f 2358 //\r
a651e231
LG
2359 TeHeader = (EFI_TE_IMAGE_HEADER *) Pe32;\r
2360 if (TeHeader->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
2361 //\r
2362 // By TeImage Header to get output\r
2363 //\r
2364 *EntryPoint = TeHeader->AddressOfEntryPoint + sizeof (EFI_TE_IMAGE_HEADER) - TeHeader->StrippedSize;\r
2365 *BaseOfCode = TeHeader->BaseOfCode + sizeof (EFI_TE_IMAGE_HEADER) - TeHeader->StrippedSize;\r
2366 *MachineType = TeHeader->Machine;\r
2367 } else {\r
2368 \r
2369 //\r
2370 // Then check whether \r
2371 // First is the DOS header\r
2372 //\r
2373 DosHeader = (EFI_IMAGE_DOS_HEADER *) Pe32;\r
2374 \r
2375 //\r
2376 // Verify DOS header is expected\r
2377 //\r
2378 if (DosHeader->e_magic != EFI_IMAGE_DOS_SIGNATURE) {\r
2379 printf ("ERROR: Unknown magic number in the DOS header, 0x%04X.\n", DosHeader->e_magic);\r
2380 return EFI_UNSUPPORTED;\r
2381 }\r
2382 //\r
2383 // Immediately following is the NT header.\r
2384 //\r
2385 NtHeader = (EFI_IMAGE_NT_HEADERS *) ((UINTN) Pe32 + DosHeader->e_lfanew);\r
2386 \r
2387 //\r
2388 // Verify NT header is expected\r
2389 //\r
2390 if (NtHeader->Signature != EFI_IMAGE_NT_SIGNATURE) {\r
2391 printf ("ERROR: Unrecognized image signature 0x%08X.\n", NtHeader->Signature);\r
2392 return EFI_UNSUPPORTED;\r
2393 }\r
2394 //\r
2395 // Get output\r
2396 //\r
2397 *EntryPoint = NtHeader->OptionalHeader.AddressOfEntryPoint;\r
2398 *BaseOfCode = NtHeader->OptionalHeader.BaseOfCode;\r
2399 *MachineType = NtHeader->FileHeader.Machine;\r
878ddf1f 2400 }\r
878ddf1f 2401\r
2402 //\r
2403 // Verify machine type is supported\r
2404 //\r
2405 if (*MachineType != EFI_IMAGE_MACHINE_IA32 && *MachineType != EFI_IMAGE_MACHINE_IA64 && *MachineType != EFI_IMAGE_MACHINE_X64 && *MachineType != EFI_IMAGE_MACHINE_EBC) {\r
2406 printf ("ERROR: Unrecognized machine type in the PE32 file.\n");\r
2407 return EFI_UNSUPPORTED;\r
2408 }\r
2409\r
2410 return EFI_SUCCESS;\r
2411}\r
2412//\r
2413// Exposed function implementations (prototypes are defined in GenFvImageLib.h)\r
2414//\r
2415EFI_STATUS\r
2416GenerateFvImage (\r
2417 IN CHAR8 *InfFileImage,\r
2418 IN UINTN InfFileSize,\r
2419 OUT UINT8 **FvImage,\r
2420 OUT UINTN *FvImageSize,\r
2421 OUT CHAR8 **FvFileName,\r
2422 OUT UINT8 **SymImage,\r
2423 OUT UINTN *SymImageSize,\r
2424 OUT CHAR8 **SymFileName\r
2425 )\r
2426/*++\r
2427\r
2428Routine Description:\r
2429\r
2430 This is the main function which will be called from application.\r
2431\r
2432Arguments:\r
2433\r
2434 InfFileImage Buffer containing the INF file contents.\r
2435 InfFileSize Size of the contents of the InfFileImage buffer.\r
2436 FvImage Pointer to the FV image created.\r
2437 FvImageSize Size of the FV image created and pointed to by FvImage.\r
2438 FvFileName Requested name for the FV file.\r
2439 SymImage Pointer to the Sym image created.\r
2440 SymImageSize Size of the Sym image created and pointed to by SymImage.\r
2441 SymFileName Requested name for the Sym file.\r
2442\r
2443Returns:\r
2444\r
2445 EFI_SUCCESS Function completed successfully.\r
2446 EFI_OUT_OF_RESOURCES Could not allocate required resources.\r
2447 EFI_ABORTED Error encountered.\r
2448 EFI_INVALID_PARAMETER A required parameter was NULL.\r
2449\r
2450--*/\r
2451{\r
2452 EFI_STATUS Status;\r
2453 MEMORY_FILE InfMemoryFile;\r
2454 MEMORY_FILE FvImageMemoryFile;\r
2455 MEMORY_FILE SymImageMemoryFile;\r
2456 FV_INFO FvInfo;\r
2457 UINTN Index;\r
2458 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
2459 EFI_FFS_FILE_HEADER *VtfFileImage;\r
2460\r
2461 //\r
2462 // Check for invalid parameter\r
2463 //\r
2464 if (InfFileImage == NULL || FvImage == NULL || FvImageSize == NULL || FvFileName == NULL) {\r
2465 return EFI_INVALID_PARAMETER;\r
2466 }\r
2467 //\r
2468 // Initialize file structures\r
2469 //\r
2470 InfMemoryFile.FileImage = InfFileImage;\r
2471 InfMemoryFile.CurrentFilePointer = InfFileImage;\r
2472 InfMemoryFile.Eof = InfFileImage + InfFileSize;\r
2473\r
2474 //\r
2475 // Parse the FV inf file for header information\r
2476 //\r
2477 Status = ParseFvInf (&InfMemoryFile, &FvInfo);\r
2478 if (EFI_ERROR (Status)) {\r
2479 printf ("ERROR: Could not parse the input INF file.\n");\r
2480 return EFI_ABORTED;\r
2481 }\r
2482 //\r
2483 // Update the file name return values\r
2484 //\r
2485 strcpy (*FvFileName, FvInfo.FvName);\r
2486 strcpy (*SymFileName, FvInfo.SymName);\r
2487\r
2488 //\r
2489 // Calculate the FV size\r
2490 //\r
2491 *FvImageSize = FvInfo.Size;\r
2492\r
2493 //\r
2494 // Allocate the FV\r
2495 //\r
2496 *FvImage = malloc (*FvImageSize);\r
2497 if (*FvImage == NULL) {\r
2498 return EFI_OUT_OF_RESOURCES;\r
2499 }\r
2500 //\r
2501 // Allocate space for symbol file storage\r
2502 //\r
2503 *SymImage = malloc (SYMBOL_FILE_SIZE);\r
2504 if (*SymImage == NULL) {\r
2505 return EFI_OUT_OF_RESOURCES;\r
2506 }\r
2507 //\r
2508 // Initialize the FV to the erase polarity\r
2509 //\r
2510 if (FvInfo.FvAttributes & EFI_FVB_ERASE_POLARITY) {\r
2511 memset (*FvImage, -1, *FvImageSize);\r
2512 } else {\r
2513 memset (*FvImage, 0, *FvImageSize);\r
2514 }\r
2515 //\r
2516 // Initialize FV header\r
2517 //\r
2518 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) *FvImage;\r
2519\r
2520 //\r
2521 // Initialize the zero vector to all zeros.\r
2522 //\r
2523 memset (FvHeader->ZeroVector, 0, 16);\r
2524\r
2525 //\r
2526 // Copy the FFS GUID\r
2527 //\r
2528 memcpy (&FvHeader->FileSystemGuid, &FvInfo.FvGuid, sizeof (EFI_GUID));\r
2529\r
2530 FvHeader->FvLength = *FvImageSize;\r
2531 FvHeader->Signature = EFI_FVH_SIGNATURE;\r
2532 FvHeader->Attributes = FvInfo.FvAttributes;\r
2533 FvHeader->Revision = EFI_FVH_REVISION;\r
2534 FvHeader->Reserved[0] = 0;\r
2535 FvHeader->Reserved[1] = 0;\r
2536 FvHeader->Reserved[2] = 0;\r
2537\r
2538 //\r
2539 // Copy firmware block map\r
2540 //\r
2541 for (Index = 0; FvInfo.FvBlocks[Index].NumBlocks != 0; Index++) {\r
2542 FvHeader->FvBlockMap[Index].NumBlocks = FvInfo.FvBlocks[Index].NumBlocks;\r
2543 FvHeader->FvBlockMap[Index].BlockLength = FvInfo.FvBlocks[Index].BlockLength;\r
2544 }\r
2545 //\r
2546 // Add block map terminator\r
2547 //\r
2548 FvHeader->FvBlockMap[Index].NumBlocks = 0;\r
2549 FvHeader->FvBlockMap[Index].BlockLength = 0;\r
2550\r
2551 //\r
2552 // Complete the header\r
2553 //\r
2554 FvHeader->HeaderLength = (UINT16) (((UINTN) &(FvHeader->FvBlockMap[Index + 1])) - (UINTN) *FvImage);\r
2555 FvHeader->Checksum = 0;\r
2556 FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));\r
2557\r
2558 //\r
2559 // If there is no FFS file, find and generate each components of the FV\r
2560 //\r
2561 if (FvInfo.FvFiles[0][0] == 0) {\r
2562 Status = GenNonFFSFv (*FvImage, &FvInfo);\r
2563 if (EFI_ERROR (Status)) {\r
2564 printf ("ERROR: Could not generate NonFFS FV.\n");\r
2565 free (*FvImage);\r
2566 return EFI_ABORTED;\r
2567 }\r
2568\r
2569 return EFI_SUCCESS;\r
2570 }\r
2571 //\r
2572 // Initialize our "file" view of the buffer\r
2573 //\r
2574 FvImageMemoryFile.FileImage = *FvImage;\r
2575 FvImageMemoryFile.CurrentFilePointer = *FvImage + FvHeader->HeaderLength;\r
2576 FvImageMemoryFile.Eof = *FvImage +*FvImageSize;\r
2577\r
2578 //\r
2579 // Initialize our "file" view of the symbol file.\r
2580 //\r
2581 SymImageMemoryFile.FileImage = *SymImage;\r
2582 SymImageMemoryFile.CurrentFilePointer = *SymImage;\r
2583 SymImageMemoryFile.Eof = *FvImage + SYMBOL_FILE_SIZE;\r
2584\r
2585 //\r
2586 // Initialize the FV library.\r
2587 //\r
2588 InitializeFvLib (FvImageMemoryFile.FileImage, FvInfo.Size);\r
2589\r
2590 //\r
2591 // Files start on 8 byte alignments, so move to the next 8 byte aligned\r
2592 // address. For now, just assert if it isn't. Currently FV header is\r
2593 // always a multiple of 8 bytes.\r
2594 // BUGBUG: Handle this better\r
2595 //\r
2596 assert ((((UINTN) FvImageMemoryFile.CurrentFilePointer) % 8) == 0);\r
2597\r
2598 //\r
2599 // Initialize the VTF file address.\r
2600 //\r
2601 VtfFileImage = (EFI_FFS_FILE_HEADER *) FvImageMemoryFile.Eof;\r
2602\r
2603 //\r
2604 // Add files to FV\r
2605 //\r
2606 for (Index = 0; FvInfo.FvFiles[Index][0] != 0; Index++) {\r
2607 //\r
2608 // Add the file\r
2609 //\r
2610 Status = AddFile (&FvImageMemoryFile, &FvInfo, Index, &VtfFileImage, &SymImageMemoryFile);\r
2611\r
2612 //\r
2613 // Exit if error detected while adding the file\r
2614 //\r
2615 if (EFI_ERROR (Status)) {\r
2616 printf ("ERROR: Could not add file %s.\n", FvInfo.FvFiles[Index]);\r
2617 free (*FvImage);\r
2618 return EFI_ABORTED;\r
2619 }\r
2620 }\r
2621 //\r
2622 // If there is a VTF file, some special actions need to occur.\r
2623 //\r
2624 if ((UINTN) VtfFileImage != (UINTN) FvImageMemoryFile.Eof) {\r
2625 //\r
2626 // Pad from the end of the last file to the beginning of the VTF file.\r
2627 //\r
2628 Status = PadFvImage (&FvImageMemoryFile, VtfFileImage);\r
2629 if (EFI_ERROR (Status)) {\r
2630 printf ("ERROR: Could not create the pad file between the last file and the VTF file.\n");\r
2631 free (*FvImage);\r
2632 return EFI_ABORTED;\r
2633 }\r
2634 //\r
2635 // Update reset vector (SALE_ENTRY for IPF)\r
2636 // Now for IA32 and IA64 platform, the fv which has bsf file must have the \r
2637 // EndAddress of 0xFFFFFFFF. Thus, only this type fv needs to update the \r
2638 // reset vector. If the PEI Core is found, the VTF file will probably get \r
2639 // corrupted by updating the entry point. \r
2640 //\r
2641 if ((FvInfo.BaseAddress + FvInfo.Size) == FV_IMAGES_TOP_ADDRESS) { \r
2642 Status = UpdateResetVector (&FvImageMemoryFile, &FvInfo, VtfFileImage);\r
2643 if (EFI_ERROR(Status)) { \r
2644 printf ("ERROR: Could not update the reset vector.\n"); \r
2645 free (*FvImage); \r
2646 return EFI_ABORTED; \r
2647 } \r
2648 }\r
2649 } \r
2650 //\r
2651 // Determine final Sym file size\r
2652 //\r
2653 *SymImageSize = SymImageMemoryFile.CurrentFilePointer - SymImageMemoryFile.FileImage;\r
d3f75347
LG
2654 \r
2655 //\r
2656 // Update FV Alignment attribute to the largest alignment of all the FFS files in the FV\r
2657 //\r
2658 if (FvHeader->Attributes | EFI_FVB_ALIGNMENT_CAP) {\r
2659 for (Index = 1; Index <= 16; Index ++) {\r
2660 if ((1 << Index) < MaxFfsAlignment) {\r
2661 //\r
2662 // Unset the unsupported alignment attribute.\r
2663 //\r
2664 FvHeader->Attributes = FvHeader->Attributes & ~((1 << Index) * EFI_FVB_ALIGNMENT_CAP);\r
2665 } else {\r
2666 //\r
2667 // Set the supported alignment attribute.\r
2668 //\r
2669 FvHeader->Attributes = FvHeader->Attributes | ((1 << Index) * EFI_FVB_ALIGNMENT_CAP);\r
2670 }\r
2671 }\r
2672 \r
2673 //\r
2674 // Update Checksum for FvHeader\r
2675 //\r
2676 FvHeader->Checksum = 0;\r
2677 FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));\r
2678 }\r
878ddf1f 2679\r
2680 return EFI_SUCCESS;\r
2681}\r
2682\r
2683EFI_STATUS\r
2684UpdatePeiCoreEntryInFit (\r
2685 IN FIT_TABLE *FitTablePtr,\r
2686 IN UINT64 PeiCorePhysicalAddress\r
2687 )\r
2688/*++\r
2689\r
2690Routine Description:\r
2691\r
2692 This function is used to update the Pei Core address in FIT, this can be used by Sec core to pass control from\r
2693 Sec to Pei Core\r
2694\r
2695Arguments:\r
2696\r
2697 FitTablePtr - The pointer of FIT_TABLE.\r
2698 PeiCorePhysicalAddress - The address of Pei Core entry.\r
2699\r
2700Returns:\r
2701\r
2702 EFI_SUCCESS - The PEI_CORE FIT entry was updated successfully.\r
2703 EFI_NOT_FOUND - Not found the PEI_CORE FIT entry.\r
2704\r
2705--*/\r
2706{\r
2707 FIT_TABLE *TmpFitPtr;\r
2708 UINTN Index;\r
2709 UINTN NumFitComponents;\r
2710\r
2711 TmpFitPtr = FitTablePtr;\r
2712 NumFitComponents = TmpFitPtr->CompSize;\r
2713\r
2714 for (Index = 0; Index < NumFitComponents; Index++) {\r
2715 if ((TmpFitPtr->CvAndType & FIT_TYPE_MASK) == COMP_TYPE_FIT_PEICORE) {\r
2716 TmpFitPtr->CompAddress = PeiCorePhysicalAddress;\r
2717 return EFI_SUCCESS;\r
2718 }\r
2719\r
2720 TmpFitPtr++;\r
2721 }\r
2722\r
2723 return EFI_NOT_FOUND;\r
2724}\r
2725\r
2726VOID\r
2727UpdateFitCheckSum (\r
2728 IN FIT_TABLE *FitTablePtr\r
2729 )\r
2730/*++\r
2731\r
2732Routine Description:\r
2733\r
2734 This function is used to update the checksum for FIT.\r
2735\r
2736\r
2737Arguments:\r
2738\r
2739 FitTablePtr - The pointer of FIT_TABLE.\r
2740\r
2741Returns:\r
2742\r
2743 None.\r
2744\r
2745--*/\r
2746{\r
2747 if ((FitTablePtr->CvAndType & CHECKSUM_BIT_MASK) >> 7) {\r
2748 FitTablePtr->CheckSum = 0;\r
2749 FitTablePtr->CheckSum = CalculateChecksum8 ((UINT8 *) FitTablePtr, FitTablePtr->CompSize * 16);\r
2750 }\r
2751}\r