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