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