]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Universal/FirmwareVolume/FwVolDxe/FwVol.c
IntelFrameworkModulePkg: Fix typos in comments
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / FirmwareVolume / FwVolDxe / FwVol.c
CommitLineData
c2df8e13 1/** @file\r
2\r
3 Firmware File System driver that produce full Firmware Volume2 protocol.\r
4 Layers on top of Firmware Block protocol to produce a file abstraction\r
5 of FV based files.\r
6\r
4888d15e 7 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
c2df8e13 8\r
9 This program and the accompanying materials\r
10 are licensed and made available under the terms and conditions\r
11 of the BSD License which accompanies this distribution. The\r
12 full text of the license may be found at\r
13 http://opensource.org/licenses/bsd-license.php\r
14\r
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
17\r
18**/\r
19\r
20#include "FwVolDriver.h"\r
21\r
22#define KEYSIZE sizeof (UINTN)\r
23\r
24/**\r
25 Given the supplied FW_VOL_BLOCK_PROTOCOL, allocate a buffer for output and\r
26 copy the real length volume header into it.\r
27\r
28 @param Fvb The FW_VOL_BLOCK_PROTOCOL instance from which to\r
29 read the volume header\r
30 @param FwVolHeader Pointer to pointer to allocated buffer in which\r
31 the volume header is returned.\r
32\r
33 @retval EFI_OUT_OF_RESOURCES No enough buffer could be allocated.\r
34 @retval EFI_SUCCESS Successfully read volume header to the allocated\r
35 buffer.\r
36 @retval EFI_ACCESS_DENIED Read status of FV is not enabled.\r
4888d15e
SZ
37 @retval EFI_INVALID_PARAMETER The FV Header signature is not as expected or\r
38 the file system could not be understood.\r
c2df8e13 39**/\r
40EFI_STATUS\r
41GetFwVolHeader (\r
42 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb,\r
43 OUT EFI_FIRMWARE_VOLUME_HEADER **FwVolHeader\r
44 )\r
45{\r
46 EFI_STATUS Status;\r
47 EFI_FIRMWARE_VOLUME_HEADER TempFvh;\r
48 EFI_FVB_ATTRIBUTES_2 FvbAttributes;\r
49 UINTN FvhLength;\r
50 EFI_PHYSICAL_ADDRESS BaseAddress;\r
51\r
52 //\r
53 // Determine the real length of FV header\r
54 //\r
55 Status = Fvb->GetAttributes (\r
56 Fvb,\r
57 &FvbAttributes\r
58 );\r
59 if (EFI_ERROR (Status)) {\r
60 return Status;\r
61 }\r
62\r
63 if ((FvbAttributes & EFI_FVB2_READ_STATUS) == 0) {\r
64 return EFI_ACCESS_DENIED;\r
65 }\r
66\r
67 //\r
68 // Just avoid compiling warning\r
69 //\r
70 BaseAddress = 0;\r
71 FvhLength = sizeof (EFI_FIRMWARE_VOLUME_HEADER);\r
72\r
73 //\r
74 // memory-mapped FV and non memory-mapped has different ways to read\r
75 //\r
76 if ((FvbAttributes & EFI_FVB2_MEMORY_MAPPED) != 0) {\r
77 Status = Fvb->GetPhysicalAddress (\r
78 Fvb,\r
79 &BaseAddress\r
80 );\r
81 if (EFI_ERROR (Status)) {\r
82 return Status;\r
83 }\r
84 CopyMem (&TempFvh, (VOID *) (UINTN) BaseAddress, FvhLength);\r
85 } else {\r
86 Status = Fvb->Read (\r
87 Fvb,\r
88 0,\r
89 0,\r
90 &FvhLength,\r
91 (UINT8 *) &TempFvh\r
92 );\r
93 }\r
94\r
4888d15e
SZ
95 //\r
96 // Validate FV Header signature, if not as expected, continue.\r
97 //\r
98 if (TempFvh.Signature != EFI_FVH_SIGNATURE) {\r
99 return EFI_INVALID_PARAMETER;\r
100 }\r
101\r
102 //\r
103 // Check to see that the file system is indeed formatted in a way we can\r
104 // understand it...\r
105 //\r
106 if ((!CompareGuid (&TempFvh.FileSystemGuid, &gEfiFirmwareFileSystem2Guid)) &&\r
107 (!CompareGuid (&TempFvh.FileSystemGuid, &gEfiFirmwareFileSystem3Guid))) {\r
108 return EFI_INVALID_PARAMETER;\r
109 }\r
110\r
c2df8e13 111 *FwVolHeader = AllocatePool (TempFvh.HeaderLength);\r
112 if (*FwVolHeader == NULL) {\r
113 return EFI_OUT_OF_RESOURCES;\r
114 }\r
115 //\r
116 // Read the whole header\r
117 //\r
118 if ((FvbAttributes & EFI_FVB2_MEMORY_MAPPED) != 0) {\r
119 CopyMem (*FwVolHeader, (VOID *) (UINTN) BaseAddress, TempFvh.HeaderLength);\r
120 } else {\r
121 //\r
122 // Assumed the first block is bigger than the length of Fv headder\r
123 //\r
124 FvhLength = TempFvh.HeaderLength;\r
125 Status = Fvb->Read (\r
126 Fvb,\r
127 0,\r
128 0,\r
129 &FvhLength,\r
130 (UINT8 *) *FwVolHeader\r
131 );\r
132 //\r
133 // Check whether Read successes.\r
134 //\r
135 if (EFI_ERROR (Status)) {\r
136 FreePool (*FwVolHeader);\r
137 *FwVolHeader = NULL;\r
138 return Status;\r
139 }\r
140 }\r
141\r
142 return EFI_SUCCESS;\r
143}\r
144\r
145/**\r
146 Free FvDevice resource when error happens.\r
147\r
148 @param FvDevice Pointer to the FvDevice to be freed.\r
149**/\r
150VOID\r
151FreeFvDeviceResource (\r
152 IN FV_DEVICE *FvDevice\r
153 )\r
154{\r
155 LBA_ENTRY *LbaEntry;\r
156 FREE_SPACE_ENTRY *FreeSpaceEntry;\r
157 FFS_FILE_LIST_ENTRY *FfsFileEntry;\r
158 LIST_ENTRY *NextEntry;\r
159\r
160 //\r
161 // Free LAB Entry\r
162 //\r
163 LbaEntry = (LBA_ENTRY *) FvDevice->LbaHeader.ForwardLink;\r
164 while (&LbaEntry->Link != &FvDevice->LbaHeader) {\r
165 NextEntry = (&LbaEntry->Link)->ForwardLink;\r
166 FreePool (LbaEntry);\r
167 LbaEntry = (LBA_ENTRY *) NextEntry;\r
168 }\r
169 //\r
170 // Free File List Entry\r
171 //\r
172 FfsFileEntry = (FFS_FILE_LIST_ENTRY *) FvDevice->FfsFileListHeader.ForwardLink;\r
173 while (&FfsFileEntry->Link != &FvDevice->FfsFileListHeader) {\r
174 NextEntry = (&FfsFileEntry->Link)->ForwardLink;\r
175 FreePool (FfsFileEntry);\r
176 FfsFileEntry = (FFS_FILE_LIST_ENTRY *) NextEntry;\r
177 }\r
178 //\r
179 // Free Space Entry\r
180 //\r
181 FreeSpaceEntry = (FREE_SPACE_ENTRY *) FvDevice->FreeSpaceHeader.ForwardLink;\r
182 while (&FreeSpaceEntry->Link != &FvDevice->FreeSpaceHeader) {\r
183 NextEntry = (&FreeSpaceEntry->Link)->ForwardLink;\r
184 FreePool (FreeSpaceEntry);\r
185 FreeSpaceEntry = (FREE_SPACE_ENTRY *) NextEntry;\r
186 }\r
187 //\r
188 // Free the cache\r
189 //\r
190 FreePool ((UINT8 *) (UINTN) FvDevice->CachedFv);\r
191\r
192 return ;\r
193}\r
194\r
0c3a1db4
SZ
195/**\r
196\r
197 Firmware volume inherits authentication status from the FV image file and section(in another firmware volume)\r
198 where it came from.\r
199\r
200 @param FvDevice A pointer to the FvDevice.\r
201\r
202**/\r
203VOID\r
204FwVolInheritAuthenticationStatus (\r
205 IN FV_DEVICE *FvDevice\r
206 )\r
207{\r
208 EFI_STATUS Status;\r
209 EFI_FIRMWARE_VOLUME_HEADER *CachedFvHeader;\r
210 EFI_FIRMWARE_VOLUME_EXT_HEADER *CachedFvExtHeader;\r
211 EFI_FIRMWARE_VOLUME2_PROTOCOL *ParentFvProtocol;\r
212 UINTN Key;\r
213 EFI_GUID FileNameGuid;\r
214 EFI_FV_FILETYPE FileType;\r
215 EFI_FV_FILE_ATTRIBUTES FileAttributes;\r
216 UINTN FileSize;\r
217 EFI_SECTION_TYPE SectionType;\r
218 UINT32 AuthenticationStatus;\r
219 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
220 EFI_FIRMWARE_VOLUME_EXT_HEADER *FvExtHeader;\r
221 UINTN BufferSize;\r
222\r
223 CachedFvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) FvDevice->CachedFv;\r
224\r
225 if (FvDevice->Fv.ParentHandle != NULL) {\r
226 //\r
227 // By Parent Handle, find out the FV image file and section(in another firmware volume) where the firmware volume came from \r
228 //\r
229 Status = gBS->HandleProtocol (FvDevice->Fv.ParentHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **) &ParentFvProtocol);\r
230 if (!EFI_ERROR (Status) && (ParentFvProtocol != NULL)) {\r
231 Key = 0;\r
232 do {\r
233 FileType = EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE;\r
234 Status = ParentFvProtocol->GetNextFile (\r
235 ParentFvProtocol,\r
236 &Key,\r
237 &FileType,\r
238 &FileNameGuid,\r
239 &FileAttributes,\r
240 &FileSize\r
241 );\r
242 if (EFI_ERROR (Status)) {\r
243 return;\r
244 }\r
245\r
246 SectionType = EFI_SECTION_FIRMWARE_VOLUME_IMAGE;\r
247 FvHeader = NULL;\r
248 BufferSize = 0;\r
249 Status = ParentFvProtocol->ReadSection (\r
250 ParentFvProtocol,\r
251 &FileNameGuid,\r
252 SectionType,\r
253 0,\r
254 (VOID **) &FvHeader,\r
255 &BufferSize,\r
256 &AuthenticationStatus\r
257 );\r
258 if (!EFI_ERROR (Status)) {\r
259 if ((FvHeader->FvLength == CachedFvHeader->FvLength) &&\r
260 (FvHeader->ExtHeaderOffset == CachedFvHeader->ExtHeaderOffset)) {\r
261 if (FvHeader->ExtHeaderOffset !=0) {\r
262 //\r
263 // Both FVs contain extension header, then compare their FV Name GUID\r
264 //\r
265 FvExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *) ((UINTN) FvHeader + FvHeader->ExtHeaderOffset);\r
266 CachedFvExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *) ((UINTN) CachedFvHeader + CachedFvHeader->ExtHeaderOffset);\r
267 if (CompareGuid (&FvExtHeader->FvName, &CachedFvExtHeader->FvName)) {\r
268 //\r
269 // Found the FV image section where the firmware volume came from,\r
270 // and then inherit authentication status from it.\r
271 //\r
272 FvDevice->AuthenticationStatus = AuthenticationStatus;\r
273 FreePool ((VOID *) FvHeader);\r
274 return;\r
275 }\r
276 } else {\r
277 //\r
278 // Both FVs don't contain extension header, then compare their whole FV Image.\r
279 //\r
25fbcc0a 280 if (CompareMem ((VOID *) FvHeader, (VOID *) CachedFvHeader, (UINTN) FvHeader->FvLength) == 0) {\r
0c3a1db4
SZ
281 //\r
282 // Found the FV image section where the firmware volume came from\r
283 // and then inherit authentication status from it.\r
284 //\r
285 FvDevice->AuthenticationStatus = AuthenticationStatus;\r
286 FreePool ((VOID *) FvHeader);\r
287 return;\r
288 }\r
289 }\r
290 }\r
291 FreePool ((VOID *) FvHeader);\r
292 }\r
293 } while (TRUE);\r
294 }\r
295 }\r
296}\r
297\r
c2df8e13 298/**\r
299 Check if an FV is consistent and allocate cache for it.\r
300\r
301 @param FvDevice A pointer to the FvDevice to be checked.\r
302\r
303 @retval EFI_OUT_OF_RESOURCES No enough buffer could be allocated.\r
304 @retval EFI_VOLUME_CORRUPTED File system is corrupted.\r
305 @retval EFI_SUCCESS FV is consistent and cache is allocated.\r
306\r
307**/\r
308EFI_STATUS\r
309FvCheck (\r
310 IN FV_DEVICE *FvDevice\r
311 )\r
312{\r
313 EFI_STATUS Status;\r
314 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
315 EFI_FVB_ATTRIBUTES_2 FvbAttributes;\r
316 EFI_FV_BLOCK_MAP_ENTRY *BlockMap;\r
317 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
f95f107c 318 EFI_FIRMWARE_VOLUME_EXT_HEADER *FwVolExtHeader;\r
c2df8e13 319 UINT8 *FwCache;\r
320 LBA_ENTRY *LbaEntry;\r
321 FREE_SPACE_ENTRY *FreeSpaceEntry;\r
322 FFS_FILE_LIST_ENTRY *FfsFileEntry;\r
323 UINT8 *LbaStart;\r
324 UINTN Index;\r
325 EFI_LBA LbaIndex;\r
326 UINT8 *Ptr;\r
327 UINTN Size;\r
328 UINT8 *FreeStart;\r
329 UINTN FreeSize;\r
330 UINT8 ErasePolarity;\r
c2df8e13 331 EFI_FFS_FILE_STATE FileState;\r
332 UINT8 *TopFvAddress;\r
333 UINTN TestLength;\r
334 EFI_PHYSICAL_ADDRESS BaseAddress;\r
335\r
336 Fvb = FvDevice->Fvb;\r
337\r
338 Status = Fvb->GetAttributes (Fvb, &FvbAttributes);\r
339 if (EFI_ERROR (Status)) {\r
340 return Status;\r
341 }\r
342\r
343 InitializeListHead (&FvDevice->LbaHeader);\r
344 InitializeListHead (&FvDevice->FreeSpaceHeader);\r
345 InitializeListHead (&FvDevice->FfsFileListHeader);\r
346\r
347 FwVolHeader = NULL;\r
348 Status = GetFwVolHeader (Fvb, &FwVolHeader);\r
349 if (EFI_ERROR (Status)) {\r
350 return Status;\r
351 }\r
352 ASSERT (FwVolHeader != NULL);\r
353\r
23491d5c
SZ
354 FvDevice->IsFfs3Fv = CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem3Guid);\r
355\r
c2df8e13 356 //\r
357 // Double Check firmware volume header here\r
358 //\r
359 if (!VerifyFvHeaderChecksum (FwVolHeader)) {\r
360 FreePool (FwVolHeader);\r
361 return EFI_VOLUME_CORRUPTED;\r
362 }\r
363\r
364 BlockMap = FwVolHeader->BlockMap;\r
365\r
366 //\r
367 // FwVolHeader->FvLength is the whole FV length including FV header\r
368 //\r
369 FwCache = AllocateZeroPool ((UINTN) FwVolHeader->FvLength);\r
370 if (FwCache == NULL) {\r
371 FreePool (FwVolHeader);\r
372 return EFI_OUT_OF_RESOURCES;\r
373 }\r
374\r
375 FvDevice->CachedFv = (EFI_PHYSICAL_ADDRESS) (UINTN) FwCache;\r
376\r
377 //\r
378 // Copy to memory\r
379 //\r
380 LbaStart = FwCache;\r
381 LbaIndex = 0;\r
382 Ptr = NULL;\r
383\r
384 if ((FvbAttributes & EFI_FVB2_MEMORY_MAPPED) != 0) {\r
385 //\r
386 // Get volume base address\r
387 //\r
388 Status = Fvb->GetPhysicalAddress (Fvb, &BaseAddress);\r
389 if (EFI_ERROR (Status)) {\r
390 FreePool (FwVolHeader);\r
391 return Status;\r
392 }\r
393\r
394 Ptr = (UINT8 *) ((UINTN) BaseAddress);\r
395\r
396 DEBUG((EFI_D_INFO, "Fv Base Address is 0x%LX\n", BaseAddress));\r
397 }\r
398 //\r
399 // Copy whole FV into the memory\r
400 //\r
401 while ((BlockMap->NumBlocks != 0) || (BlockMap->Length != 0)) {\r
402\r
403 for (Index = 0; Index < BlockMap->NumBlocks; Index++) {\r
404 LbaEntry = AllocatePool (sizeof (LBA_ENTRY));\r
405 if (LbaEntry == NULL) {\r
406 FreePool (FwVolHeader);\r
407 FreeFvDeviceResource (FvDevice);\r
408 return EFI_OUT_OF_RESOURCES;\r
409 }\r
410\r
411 LbaEntry->LbaIndex = LbaIndex;\r
412 LbaEntry->StartingAddress = LbaStart;\r
413 LbaEntry->BlockLength = BlockMap->Length;\r
414\r
415 //\r
416 // Copy each LBA into memory\r
417 //\r
418 if ((FvbAttributes & EFI_FVB2_MEMORY_MAPPED) != 0) {\r
419\r
420 CopyMem (LbaStart, Ptr, BlockMap->Length);\r
421 Ptr += BlockMap->Length;\r
422\r
423 } else {\r
424\r
425 Size = BlockMap->Length;\r
426 Status = Fvb->Read (\r
427 Fvb,\r
428 LbaIndex,\r
429 0,\r
430 &Size,\r
431 LbaStart\r
432 );\r
433 //\r
434 // Not check EFI_BAD_BUFFER_SIZE, for Size = BlockMap->Length\r
435 //\r
436 if (EFI_ERROR (Status)) {\r
437 FreePool (FwVolHeader);\r
438 FreeFvDeviceResource (FvDevice);\r
439 return Status;\r
440 }\r
441\r
442 }\r
443\r
444 LbaIndex++;\r
445 LbaStart += BlockMap->Length;\r
446\r
447 InsertTailList (&FvDevice->LbaHeader, &LbaEntry->Link);\r
448 }\r
449\r
450 BlockMap++;\r
451 }\r
452\r
453 FvDevice->FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) FwCache;\r
454\r
455 //\r
456 // it is not used any more, so free FwVolHeader\r
457 //\r
458 FreePool (FwVolHeader);\r
459\r
460 //\r
461 // Scan to check the free space & File list\r
462 //\r
463 if ((FvbAttributes & EFI_FVB2_ERASE_POLARITY) != 0) {\r
464 ErasePolarity = 1;\r
465 } else {\r
466 ErasePolarity = 0;\r
467 }\r
468\r
469 FvDevice->ErasePolarity = ErasePolarity;\r
470\r
471 //\r
472 // go through the whole FV cache, check the consistence of the FV\r
473 //\r
f95f107c
SZ
474 if (FvDevice->FwVolHeader->ExtHeaderOffset != 0) {\r
475 //\r
476 // Searching for files starts on an 8 byte aligned boundary after the end of the Extended Header if it exists.\r
477 //\r
478 FwVolExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *) (UINTN) (FvDevice->CachedFv + FvDevice->FwVolHeader->ExtHeaderOffset);\r
479 Ptr = (UINT8 *) FwVolExtHeader + FwVolExtHeader->ExtHeaderSize;\r
480 Ptr = (UINT8 *) ALIGN_POINTER (Ptr, 8);\r
481 } else {\r
482 Ptr = (UINT8 *) (UINTN) (FvDevice->CachedFv + FvDevice->FwVolHeader->HeaderLength);\r
483 }\r
484 TopFvAddress = (UINT8 *) (UINTN) (FvDevice->CachedFv + FvDevice->FwVolHeader->FvLength);\r
c2df8e13 485\r
486 //\r
487 // Build FFS list & Free Space List here\r
488 //\r
f95f107c
SZ
489 while (Ptr < TopFvAddress) {\r
490 TestLength = TopFvAddress - Ptr;\r
c2df8e13 491\r
492 if (TestLength > sizeof (EFI_FFS_FILE_HEADER)) {\r
493 TestLength = sizeof (EFI_FFS_FILE_HEADER);\r
494 }\r
495\r
496 if (IsBufferErased (ErasePolarity, Ptr, TestLength)) {\r
497 //\r
498 // We found free space\r
499 //\r
500 FreeStart = Ptr;\r
501 FreeSize = 0;\r
502\r
503 do {\r
f95f107c 504 TestLength = TopFvAddress - Ptr;\r
c2df8e13 505\r
506 if (TestLength > sizeof (EFI_FFS_FILE_HEADER)) {\r
507 TestLength = sizeof (EFI_FFS_FILE_HEADER);\r
508 }\r
509\r
510 if (!IsBufferErased (ErasePolarity, Ptr, TestLength)) {\r
511 break;\r
512 }\r
513\r
514 FreeSize += TestLength;\r
515 Ptr += TestLength;\r
f95f107c 516 } while (Ptr < TopFvAddress);\r
c2df8e13 517\r
518 FreeSpaceEntry = AllocateZeroPool (sizeof (FREE_SPACE_ENTRY));\r
519 if (FreeSpaceEntry == NULL) {\r
520 FreeFvDeviceResource (FvDevice);\r
521 return EFI_OUT_OF_RESOURCES;\r
522 }\r
523 //\r
524 // Create a Free space entry\r
525 //\r
526 FreeSpaceEntry->StartingAddress = FreeStart;\r
527 FreeSpaceEntry->Length = FreeSize;\r
528 InsertTailList (&FvDevice->FreeSpaceHeader, &FreeSpaceEntry->Link);\r
529 continue;\r
530 }\r
531 //\r
70d3fe9d 532 // double check boundary\r
c2df8e13 533 //\r
534 if (TestLength < sizeof (EFI_FFS_FILE_HEADER)) {\r
535 break;\r
536 }\r
537\r
538 if (!IsValidFFSHeader (\r
539 FvDevice->ErasePolarity,\r
540 (EFI_FFS_FILE_HEADER *) Ptr\r
541 )) {\r
542 FileState = GetFileState (\r
543 FvDevice->ErasePolarity,\r
544 (EFI_FFS_FILE_HEADER *) Ptr\r
545 );\r
546 if ((FileState == EFI_FILE_HEADER_INVALID) || (FileState == EFI_FILE_HEADER_CONSTRUCTION)) {\r
23491d5c
SZ
547 if (IS_FFS_FILE2 (Ptr)) {\r
548 if (!FvDevice->IsFfs3Fv) {\r
549 DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &((EFI_FFS_FILE_HEADER *) Ptr)->Name));\r
550 }\r
551 Ptr = Ptr + sizeof (EFI_FFS_FILE_HEADER2);\r
552 } else {\r
553 Ptr = Ptr + sizeof (EFI_FFS_FILE_HEADER);\r
554 }\r
c2df8e13 555\r
556 continue;\r
557\r
558 } else {\r
559 //\r
560 // File system is corrputed, return\r
561 //\r
562 FreeFvDeviceResource (FvDevice);\r
563 return EFI_VOLUME_CORRUPTED;\r
564 }\r
565 }\r
566\r
23491d5c
SZ
567 if (IS_FFS_FILE2 (Ptr)) {\r
568 ASSERT (FFS_FILE2_SIZE (Ptr) > 0x00FFFFFF);\r
569 if (!FvDevice->IsFfs3Fv) {\r
570 DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &((EFI_FFS_FILE_HEADER *) Ptr)->Name));\r
571 Ptr = Ptr + FFS_FILE2_SIZE (Ptr);\r
572 //\r
70d3fe9d 573 // Adjust Ptr to the next 8-byte aligned boundary.\r
23491d5c
SZ
574 //\r
575 while (((UINTN) Ptr & 0x07) != 0) {\r
576 Ptr++;\r
577 }\r
578 continue;\r
579 }\r
580 }\r
581\r
c2df8e13 582 if (IsValidFFSFile (FvDevice, (EFI_FFS_FILE_HEADER *) Ptr)) {\r
c2df8e13 583 FileState = GetFileState (\r
584 FvDevice->ErasePolarity,\r
585 (EFI_FFS_FILE_HEADER *) Ptr\r
586 );\r
587\r
588 //\r
589 // check for non-deleted file\r
590 //\r
591 if (FileState != EFI_FILE_DELETED) {\r
592 //\r
593 // Create a FFS list entry for each non-deleted file\r
594 //\r
595 FfsFileEntry = AllocateZeroPool (sizeof (FFS_FILE_LIST_ENTRY));\r
596 if (FfsFileEntry == NULL) {\r
597 FreeFvDeviceResource (FvDevice);\r
598 return EFI_OUT_OF_RESOURCES;\r
599 }\r
600\r
601 FfsFileEntry->FfsHeader = Ptr;\r
602 InsertTailList (&FvDevice->FfsFileListHeader, &FfsFileEntry->Link);\r
603 }\r
604\r
23491d5c
SZ
605 if (IS_FFS_FILE2 (Ptr)) {\r
606 Ptr = Ptr + FFS_FILE2_SIZE (Ptr);\r
607 } else {\r
608 Ptr = Ptr + FFS_FILE_SIZE (Ptr);\r
609 }\r
c2df8e13 610\r
611 //\r
70d3fe9d 612 // Adjust Ptr to the next 8-byte aligned boundary.\r
c2df8e13 613 //\r
614 while (((UINTN) Ptr & 0x07) != 0) {\r
615 Ptr++;\r
616 }\r
617 } else {\r
618 //\r
619 // File system is corrupted, return\r
620 //\r
621 FreeFvDeviceResource (FvDevice);\r
622 return EFI_VOLUME_CORRUPTED;\r
623 }\r
624 }\r
625\r
626 FvDevice->CurrentFfsFile = NULL;\r
627\r
628 return EFI_SUCCESS;\r
629}\r
630\r
631/**\r
632 Entry point function does install/reinstall FV2 protocol with full functionality.\r
633\r
634 @param ImageHandle A handle for the image that is initializing this driver\r
635 @param SystemTable A pointer to the EFI system table\r
636\r
637 @retval EFI_SUCCESS At least one Fv protocol install/reinstall successfully.\r
638 @retval EFI_NOT_FOUND No FV protocol install/reinstall successfully.\r
639**/\r
640EFI_STATUS\r
641EFIAPI\r
642FwVolDriverInit (\r
643 IN EFI_HANDLE ImageHandle,\r
644 IN EFI_SYSTEM_TABLE *SystemTable\r
645 )\r
646{\r
647 EFI_STATUS Status;\r
648 EFI_HANDLE *HandleBuffer;\r
649 UINTN HandleCount;\r
650 UINTN Index;\r
651 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
652 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
653 FV_DEVICE *FvDevice;\r
654 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
655 BOOLEAN Reinstall;\r
656 BOOLEAN InstallFlag;\r
657\r
658 DEBUG ((EFI_D_INFO, "=========FwVol writable driver installed\n"));\r
659 InstallFlag = FALSE;\r
660 //\r
661 // Locate all handles of Fvb protocol\r
662 //\r
663 Status = gBS->LocateHandleBuffer (\r
664 ByProtocol,\r
665 &gEfiFirmwareVolumeBlockProtocolGuid,\r
666 NULL,\r
667 &HandleCount,\r
668 &HandleBuffer\r
669 );\r
670 if (EFI_ERROR (Status)) {\r
671 return EFI_NOT_FOUND;\r
672 }\r
23491d5c 673\r
c2df8e13 674 for (Index = 0; Index < HandleCount; Index += 1) {\r
675 Status = gBS->HandleProtocol (\r
676 HandleBuffer[Index],\r
677 &gEfiFirmwareVolumeBlockProtocolGuid,\r
678 (VOID **) &Fvb\r
679 );\r
680 if (EFI_ERROR (Status)) {\r
681 continue;\r
682 }\r
683\r
684 FwVolHeader = NULL;\r
685 Status = GetFwVolHeader (Fvb, &FwVolHeader);\r
686 if (EFI_ERROR (Status)) {\r
687 continue;\r
688 }\r
689 ASSERT (FwVolHeader != NULL);\r
c2df8e13 690 FreePool (FwVolHeader);\r
691\r
692 Reinstall = FALSE;\r
693 //\r
694 // Check if there is an FV protocol already installed in that handle\r
695 //\r
696 Status = gBS->HandleProtocol (\r
697 HandleBuffer[Index],\r
698 &gEfiFirmwareVolume2ProtocolGuid,\r
699 (VOID **) &Fv\r
700 );\r
701 if (!EFI_ERROR (Status)) {\r
702 Reinstall = TRUE;\r
703 }\r
704 //\r
705 // FwVol protocol on the handle so create a new one\r
706 //\r
707 FvDevice = AllocateZeroPool (sizeof (FV_DEVICE));\r
708 if (FvDevice == NULL) {\r
709 goto Done;\r
710 }\r
711\r
712 FvDevice->Signature = FV_DEVICE_SIGNATURE;\r
713 FvDevice->Fvb = Fvb;\r
714\r
715 //\r
716 // Firmware Volume Protocol interface\r
717 //\r
718 FvDevice->Fv.GetVolumeAttributes = FvGetVolumeAttributes;\r
719 FvDevice->Fv.SetVolumeAttributes = FvSetVolumeAttributes;\r
720 FvDevice->Fv.ReadFile = FvReadFile;\r
721 FvDevice->Fv.ReadSection = FvReadFileSection;\r
722 FvDevice->Fv.WriteFile = FvWriteFile;\r
723 FvDevice->Fv.GetNextFile = FvGetNextFile;\r
724 FvDevice->Fv.KeySize = KEYSIZE;\r
725 FvDevice->Fv.GetInfo = FvGetVolumeInfo;\r
726 FvDevice->Fv.SetInfo = FvSetVolumeInfo;\r
0c3a1db4 727 FvDevice->Fv.ParentHandle = Fvb->ParentHandle;\r
c2df8e13 728\r
729 Status = FvCheck (FvDevice);\r
730 if (EFI_ERROR (Status)) {\r
731 //\r
732 // The file system is not consistence\r
733 //\r
734 FreePool (FvDevice);\r
735 continue;\r
736 }\r
737\r
0c3a1db4
SZ
738 FwVolInheritAuthenticationStatus (FvDevice);\r
739\r
c2df8e13 740 if (Reinstall) {\r
741 //\r
742 // Reinstall an New FV protocol\r
743 //\r
744 // FvDevice = FV_DEVICE_FROM_THIS (Fv);\r
745 // FvDevice->Fvb = Fvb;\r
746 // FreeFvDeviceResource (FvDevice);\r
747 //\r
748 Status = gBS->ReinstallProtocolInterface (\r
749 HandleBuffer[Index],\r
750 &gEfiFirmwareVolume2ProtocolGuid,\r
751 Fv,\r
752 &FvDevice->Fv\r
753 );\r
754 if (!EFI_ERROR (Status)) {\r
755 InstallFlag = TRUE;\r
756 } else {\r
757 FreePool (FvDevice);\r
758 }\r
759 \r
760 DEBUG ((EFI_D_INFO, "Reinstall FV protocol as writable - %r\n", Status));\r
761 ASSERT_EFI_ERROR (Status);\r
762 } else {\r
763 //\r
764 // Install an New FV protocol\r
765 //\r
766 Status = gBS->InstallProtocolInterface (\r
767 &FvDevice->Handle,\r
768 &gEfiFirmwareVolume2ProtocolGuid,\r
769 EFI_NATIVE_INTERFACE,\r
770 &FvDevice->Fv\r
771 );\r
772 if (!EFI_ERROR (Status)) {\r
773 InstallFlag = TRUE;\r
774 } else {\r
775 FreePool (FvDevice);\r
776 }\r
777 \r
778 DEBUG ((EFI_D_INFO, "Install FV protocol as writable - %r\n", Status));\r
779 ASSERT_EFI_ERROR (Status);\r
780 }\r
781 }\r
782\r
783Done:\r
784 //\r
785 // As long as one Fv protocol install/reinstall successfully,\r
786 // success should return to ensure this image will be not unloaded.\r
787 // Otherwise, new Fv protocols are corrupted by other loaded driver.\r
788 //\r
789 if (InstallFlag) {\r
790 return EFI_SUCCESS;\r
791 }\r
792 \r
793 //\r
794 // No FV protocol install/reinstall successfully.\r
795 // EFI_NOT_FOUND should return to ensure this image will be unloaded.\r
796 //\r
797 return EFI_NOT_FOUND;\r
798}\r