]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Dxe/FwVol/FwVol.c
Add core FFS3 support, DxeCore.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / FwVol / FwVol.c
CommitLineData
162ed594 1/** @file\r
28a00297 2 Firmware File System driver that produce Firmware Volume protocol.\r
022c6d45 3 Layers on top of Firmware Block protocol to produce a file abstraction\r
28a00297 4 of FV based files.\r
23c98c94 5\r
6c85d162 6Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
cd5ebaa0 7This program and the accompanying materials\r
23c98c94 8are licensed and made available under the terms and conditions of the BSD License\r
9which accompanies this distribution. The full text of the license may be found at\r
10http://opensource.org/licenses/bsd-license.php\r
11\r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
28a00297 14\r
15**/\r
16\r
9c4ac31c 17#include "DxeMain.h"\r
ec90508b 18#include "FwVolDriver.h"\r
28a00297 19\r
28a00297 20\r
21//\r
22// Protocol notify related globals\r
23//\r
24VOID *gEfiFwVolBlockNotifyReg;\r
25EFI_EVENT gEfiFwVolBlockEvent;\r
26\r
27FV_DEVICE mFvDevice = {\r
0c2b5da8 28 FV2_DEVICE_SIGNATURE,\r
28a00297 29 NULL,\r
30 NULL,\r
31 {\r
32 FvGetVolumeAttributes,\r
33 FvSetVolumeAttributes,\r
34 FvReadFile,\r
35 FvReadFileSection,\r
36 FvWriteFile,\r
ec90508b 37 FvGetNextFile, \r
38 sizeof (UINTN),\r
0c2b5da8 39 NULL,\r
40 FvGetVolumeInfo,\r
41 FvSetVolumeInfo\r
28a00297 42 },\r
43 NULL,\r
44 NULL,\r
45 NULL,\r
46 NULL,\r
47 { NULL, NULL },\r
48 0\r
49};\r
50\r
51\r
52//\r
53// FFS helper functions\r
54//\r
162ed594 55/**\r
657abcff
LG
56 Read data from Firmware Block by FVB protocol Read. \r
57 The data may cross the multi block ranges.\r
58\r
59 @param Fvb The FW_VOL_BLOCK_PROTOCOL instance from which to read data.\r
60 @param StartLba Pointer to StartLba.\r
61 On input, the start logical block index from which to read.\r
62 On output,the end logical block index after reading.\r
63 @param Offset Pointer to Offset\r
64 On input, offset into the block at which to begin reading.\r
65 On output, offset into the end block after reading.\r
66 @param DataSize Size of data to be read.\r
67 @param Data Pointer to Buffer that the data will be read into.\r
68\r
69 @retval EFI_SUCCESS Successfully read data from firmware block.\r
70 @retval others\r
71**/\r
72EFI_STATUS\r
73ReadFvbData (\r
74 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb,\r
75 IN OUT EFI_LBA *StartLba,\r
76 IN OUT UINTN *Offset,\r
77 IN UINTN DataSize,\r
78 OUT UINT8 *Data\r
79 )\r
80{\r
81 UINTN BlockSize;\r
82 UINTN NumberOfBlocks;\r
83 UINTN BlockIndex;\r
84 UINTN ReadDataSize;\r
85 EFI_STATUS Status;\r
86 \r
87 //\r
88 // Try read data in current block\r
89 //\r
90 BlockIndex = 0;\r
91 ReadDataSize = DataSize;\r
92 Status = Fvb->Read (Fvb, *StartLba, *Offset, &ReadDataSize, Data);\r
93 if (Status == EFI_SUCCESS) {\r
94 *Offset += DataSize;\r
95 return EFI_SUCCESS;\r
96 } else if (Status != EFI_BAD_BUFFER_SIZE) {\r
97 //\r
98 // other error will direct return\r
99 //\r
100 return Status;\r
101 }\r
102 \r
103 //\r
104 // Data crosses the blocks, read data from next block\r
105 //\r
106 DataSize -= ReadDataSize;\r
107 Data += ReadDataSize;\r
108 *StartLba = *StartLba + 1;\r
109 while (DataSize > 0) {\r
110 Status = Fvb->GetBlockSize (Fvb, *StartLba, &BlockSize, &NumberOfBlocks);\r
111 if (EFI_ERROR (Status)) {\r
112 return Status;\r
113 }\r
114\r
115 //\r
116 // Read data from the crossing blocks\r
117 //\r
118 BlockIndex = 0; \r
119 while (BlockIndex < NumberOfBlocks && DataSize >= BlockSize) {\r
120 Status = Fvb->Read (Fvb, *StartLba + BlockIndex, 0, &BlockSize, Data);\r
121 if (EFI_ERROR (Status)) {\r
122 return Status;\r
123 }\r
124 Data += BlockSize;\r
125 DataSize -= BlockSize;\r
126 BlockIndex ++;\r
127 }\r
128 \r
129 //\r
130 // Data doesn't exceed the current block range.\r
131 //\r
132 if (DataSize < BlockSize) {\r
133 break;\r
134 }\r
135 \r
136 //\r
137 // Data must be got from the next block range.\r
138 //\r
139 *StartLba += NumberOfBlocks;\r
140 }\r
141 \r
142 //\r
143 // read the remaining data\r
144 //\r
145 if (DataSize > 0) {\r
146 Status = Fvb->Read (Fvb, *StartLba + BlockIndex, 0, &DataSize, Data);\r
147 if (EFI_ERROR (Status)) {\r
148 return Status;\r
149 }\r
150 }\r
151 \r
152 //\r
153 // Update Lba and Offset used by the following read.\r
154 //\r
155 *StartLba += BlockIndex;\r
156 *Offset = DataSize;\r
157\r
158 return EFI_SUCCESS;\r
159}\r
160\r
161/**\r
162 Given the supplied FW_VOL_BLOCK_PROTOCOL, allocate a buffer for output and\r
163 copy the real length volume header into it.\r
28a00297 164\r
022c6d45 165 @param Fvb The FW_VOL_BLOCK_PROTOCOL instance from which to\r
166 read the volume header\r
167 @param FwVolHeader Pointer to pointer to allocated buffer in which\r
168 the volume header is returned.\r
28a00297 169\r
022c6d45 170 @retval EFI_OUT_OF_RESOURCES No enough buffer could be allocated.\r
171 @retval EFI_SUCCESS Successfully read volume header to the allocated\r
162ed594 172 buffer.\r
28a00297 173\r
162ed594 174**/\r
175EFI_STATUS\r
176GetFwVolHeader (\r
177 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb,\r
178 OUT EFI_FIRMWARE_VOLUME_HEADER **FwVolHeader\r
179 )\r
28a00297 180{\r
181 EFI_STATUS Status;\r
182 EFI_FIRMWARE_VOLUME_HEADER TempFvh;\r
183 UINTN FvhLength;\r
657abcff
LG
184 EFI_LBA StartLba;\r
185 UINTN Offset;\r
28a00297 186 UINT8 *Buffer;\r
657abcff 187 \r
28a00297 188 //\r
657abcff 189 // Read the standard FV header\r
28a00297 190 //\r
657abcff
LG
191 StartLba = 0;\r
192 Offset = 0;\r
28a00297 193 FvhLength = sizeof (EFI_FIRMWARE_VOLUME_HEADER);\r
657abcff 194 Status = ReadFvbData (Fvb, &StartLba, &Offset, FvhLength, (UINT8 *)&TempFvh);\r
13492369 195 if (EFI_ERROR (Status)) {\r
196 return Status;\r
197 }\r
28a00297 198\r
199 //\r
200 // Allocate a buffer for the caller\r
201 //\r
9c4ac31c 202 *FwVolHeader = AllocatePool (TempFvh.HeaderLength);\r
28a00297 203 if (*FwVolHeader == NULL) {\r
204 return EFI_OUT_OF_RESOURCES;\r
205 }\r
206\r
207 //\r
208 // Copy the standard header into the buffer\r
209 //\r
210 CopyMem (*FwVolHeader, &TempFvh, sizeof (EFI_FIRMWARE_VOLUME_HEADER));\r
211\r
212 //\r
213 // Read the rest of the header\r
214 //\r
215 FvhLength = TempFvh.HeaderLength - sizeof (EFI_FIRMWARE_VOLUME_HEADER);\r
216 Buffer = (UINT8 *)*FwVolHeader + sizeof (EFI_FIRMWARE_VOLUME_HEADER);\r
657abcff 217 Status = ReadFvbData (Fvb, &StartLba, &Offset, FvhLength, Buffer);\r
28a00297 218 if (EFI_ERROR (Status)) {\r
219 //\r
220 // Read failed so free buffer\r
221 //\r
222 CoreFreePool (*FwVolHeader);\r
223 }\r
022c6d45 224\r
28a00297 225 return Status;\r
226}\r
227\r
228\r
28a00297 229\r
162ed594 230/**\r
28a00297 231 Free FvDevice resource when error happens\r
232\r
022c6d45 233 @param FvDevice pointer to the FvDevice to be freed.\r
28a00297 234\r
162ed594 235**/\r
162ed594 236VOID\r
237FreeFvDeviceResource (\r
238 IN FV_DEVICE *FvDevice\r
239 )\r
28a00297 240{\r
241 FFS_FILE_LIST_ENTRY *FfsFileEntry;\r
242 LIST_ENTRY *NextEntry;\r
243\r
244 //\r
245 // Free File List Entry\r
246 //\r
247 FfsFileEntry = (FFS_FILE_LIST_ENTRY *)FvDevice->FfsFileListHeader.ForwardLink;\r
248 while (&FfsFileEntry->Link != &FvDevice->FfsFileListHeader) {\r
249 NextEntry = (&FfsFileEntry->Link)->ForwardLink;\r
022c6d45 250\r
28a00297 251 if (FfsFileEntry->StreamHandle != 0) {\r
252 //\r
253 // Close stream and free resources from SEP\r
254 //\r
797a9d67 255 CloseSectionStream (FfsFileEntry->StreamHandle);\r
28a00297 256 }\r
257\r
258 CoreFreePool (FfsFileEntry);\r
259\r
e94a9ff7 260 FfsFileEntry = (FFS_FILE_LIST_ENTRY *) NextEntry;\r
28a00297 261 }\r
262\r
263\r
264 //\r
265 // Free the cache\r
266 //\r
267 CoreFreePool (FvDevice->CachedFv);\r
268\r
269 //\r
270 // Free Volume Header\r
271 //\r
272 CoreFreePool (FvDevice->FwVolHeader);\r
273\r
274 return;\r
275}\r
276\r
277\r
28a00297 278\r
162ed594 279/**\r
e94a9ff7 280 Check if an FV is consistent and allocate cache for it.\r
28a00297 281\r
022c6d45 282 @param FvDevice A pointer to the FvDevice to be checked.\r
28a00297 283\r
022c6d45 284 @retval EFI_OUT_OF_RESOURCES No enough buffer could be allocated.\r
285 @retval EFI_SUCCESS FV is consistent and cache is allocated.\r
162ed594 286 @retval EFI_VOLUME_CORRUPTED File system is corrupted.\r
28a00297 287\r
162ed594 288**/\r
289EFI_STATUS\r
290FvCheck (\r
291 IN OUT FV_DEVICE *FvDevice\r
292 )\r
28a00297 293{\r
294 EFI_STATUS Status;\r
295 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
296 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
8ee3a199 297 EFI_FVB_ATTRIBUTES_2 FvbAttributes;\r
28a00297 298 EFI_FV_BLOCK_MAP_ENTRY *BlockMap;\r
299 FFS_FILE_LIST_ENTRY *FfsFileEntry;\r
300 EFI_FFS_FILE_HEADER *FfsHeader;\r
301 UINT8 *CacheLocation;\r
302 UINTN LbaOffset;\r
657abcff 303 UINTN HeaderSize;\r
28a00297 304 UINTN Index;\r
305 EFI_LBA LbaIndex;\r
306 UINTN Size;\r
28a00297 307 EFI_FFS_FILE_STATE FileState;\r
308 UINT8 *TopFvAddress;\r
309 UINTN TestLength;\r
310\r
311\r
312 Fvb = FvDevice->Fvb;\r
313 FwVolHeader = FvDevice->FwVolHeader;\r
022c6d45 314\r
28a00297 315 Status = Fvb->GetAttributes (Fvb, &FvbAttributes);\r
316 if (EFI_ERROR (Status)) {\r
317 return Status;\r
318 }\r
319\r
320 //\r
321 // Size is the size of the FV minus the head. We have already allocated\r
322 // the header to check to make sure the volume is valid\r
323 //\r
324 Size = (UINTN)(FwVolHeader->FvLength - FwVolHeader->HeaderLength);\r
9c4ac31c 325 FvDevice->CachedFv = AllocatePool (Size);\r
28a00297 326\r
327 if (FvDevice->CachedFv == NULL) {\r
328 return EFI_OUT_OF_RESOURCES;\r
329 }\r
330\r
331 //\r
332 // Remember a pointer to the end fo the CachedFv\r
333 //\r
334 FvDevice->EndOfCachedFv = FvDevice->CachedFv + Size;\r
335\r
336 //\r
337 // Copy FV minus header into memory using the block map we have all ready\r
338 // read into memory.\r
339 //\r
340 BlockMap = FwVolHeader->BlockMap;\r
341 CacheLocation = FvDevice->CachedFv;\r
342 LbaIndex = 0;\r
657abcff
LG
343 LbaOffset = 0;\r
344 HeaderSize = FwVolHeader->HeaderLength;\r
28a00297 345 while ((BlockMap->NumBlocks != 0) || (BlockMap->Length != 0)) {\r
657abcff
LG
346 Index = 0;\r
347 Size = BlockMap->Length;\r
348 if (HeaderSize > 0) {\r
349 //\r
350 // Skip header size\r
351 //\r
352 for (; Index < BlockMap->NumBlocks && HeaderSize >= BlockMap->Length; Index ++) {\r
353 HeaderSize -= BlockMap->Length;\r
354 LbaIndex ++;\r
355 }\r
022c6d45 356\r
657abcff
LG
357 //\r
358 // Check whether FvHeader is crossing the multi block range.\r
359 //\r
360 if (HeaderSize > BlockMap->Length) {\r
361 BlockMap++;\r
362 continue;\r
363 } else if (HeaderSize > 0) {\r
364 LbaOffset = HeaderSize;\r
365 Size = BlockMap->Length - HeaderSize;\r
366 HeaderSize = 0;\r
28a00297 367 }\r
657abcff
LG
368 }\r
369 \r
370 //\r
371 // read the FV data \r
372 //\r
373 for (; Index < BlockMap->NumBlocks; Index ++) {\r
28a00297 374 Status = Fvb->Read (Fvb,\r
e94a9ff7 375 LbaIndex,\r
376 LbaOffset,\r
377 &Size,\r
378 CacheLocation\r
379 );\r
657abcff 380\r
28a00297 381 //\r
382 // Not check EFI_BAD_BUFFER_SIZE, for Size = BlockMap->Length\r
383 //\r
384 if (EFI_ERROR (Status)) {\r
385 goto Done;\r
386 }\r
022c6d45 387\r
657abcff
LG
388 LbaIndex++;\r
389 CacheLocation += Size;\r
390\r
28a00297 391 //\r
392 // After we skip Fv Header always read from start of block\r
393 //\r
394 LbaOffset = 0;\r
657abcff 395 Size = BlockMap->Length;\r
28a00297 396 }\r
657abcff 397\r
28a00297 398 BlockMap++;\r
399 }\r
400\r
401 //\r
402 // Scan to check the free space & File list\r
403 //\r
71f68914 404 if ((FvbAttributes & EFI_FVB2_ERASE_POLARITY) != 0) {\r
28a00297 405 FvDevice->ErasePolarity = 1;\r
406 } else {\r
407 FvDevice->ErasePolarity = 0;\r
022c6d45 408 }\r
28a00297 409\r
410\r
411 //\r
412 // go through the whole FV cache, check the consistence of the FV.\r
413 // Make a linked list off all the Ffs file headers\r
414 //\r
415 Status = EFI_SUCCESS;\r
416 InitializeListHead (&FvDevice->FfsFileListHeader);\r
417\r
418 //\r
419 // Build FFS list\r
420 //\r
e94a9ff7 421 FfsHeader = (EFI_FFS_FILE_HEADER *) FvDevice->CachedFv;\r
28a00297 422 TopFvAddress = FvDevice->EndOfCachedFv;\r
e94a9ff7 423 while ((UINT8 *) FfsHeader < TopFvAddress) {\r
28a00297 424\r
e94a9ff7 425 TestLength = TopFvAddress - ((UINT8 *) FfsHeader);\r
28a00297 426 if (TestLength > sizeof (EFI_FFS_FILE_HEADER)) {\r
427 TestLength = sizeof (EFI_FFS_FILE_HEADER);\r
428 }\r
429\r
430 if (IsBufferErased (FvDevice->ErasePolarity, FfsHeader, TestLength)) {\r
431 //\r
432 // We have found the free space so we are done!\r
433 //\r
434 goto Done;\r
435 }\r
436\r
437 if (!IsValidFfsHeader (FvDevice->ErasePolarity, FfsHeader, &FileState)) {\r
022c6d45 438 if ((FileState == EFI_FILE_HEADER_INVALID) ||\r
28a00297 439 (FileState == EFI_FILE_HEADER_CONSTRUCTION)) {\r
6c85d162
SZ
440 if (IS_FFS_FILE2 (FfsHeader)) {\r
441 if (!FvDevice->IsFfs3Fv) {\r
442 DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsHeader->Name));\r
443 }\r
444 FfsHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsHeader + sizeof (EFI_FFS_FILE_HEADER2));\r
445 } else {\r
446 FfsHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsHeader + sizeof (EFI_FFS_FILE_HEADER));\r
447 }\r
28a00297 448 continue;\r
28a00297 449 } else {\r
450 //\r
451 // File system is corrputed\r
452 //\r
453 Status = EFI_VOLUME_CORRUPTED;\r
454 goto Done;\r
455 }\r
456 }\r
457\r
458 if (!IsValidFfsFile (FvDevice->ErasePolarity, FfsHeader)) {\r
459 //\r
460 // File system is corrupted\r
461 //\r
462 Status = EFI_VOLUME_CORRUPTED;\r
463 goto Done;\r
464 }\r
465\r
6c85d162
SZ
466 if (IS_FFS_FILE2 (FfsHeader)) {\r
467 ASSERT (FFS_FILE2_SIZE (FfsHeader) > 0x00FFFFFF);\r
468 if (!FvDevice->IsFfs3Fv) {\r
469 DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsHeader->Name));\r
470 FfsHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsHeader + FFS_FILE2_SIZE (FfsHeader));\r
471 //\r
472 // Adjust pointer to the next 8-byte aligned boundry.\r
473 //\r
474 FfsHeader = (EFI_FFS_FILE_HEADER *) (((UINTN) FfsHeader + 7) & ~0x07);\r
475 continue;\r
476 }\r
477 }\r
28a00297 478\r
479 FileState = GetFileState (FvDevice->ErasePolarity, FfsHeader);\r
022c6d45 480\r
28a00297 481 //\r
482 // check for non-deleted file\r
483 //\r
484 if (FileState != EFI_FILE_DELETED) {\r
485 //\r
486 // Create a FFS list entry for each non-deleted file\r
487 //\r
9c4ac31c 488 FfsFileEntry = AllocateZeroPool (sizeof (FFS_FILE_LIST_ENTRY));\r
28a00297 489 if (FfsFileEntry == NULL) {\r
490 Status = EFI_OUT_OF_RESOURCES;\r
491 goto Done;\r
492 }\r
022c6d45 493\r
28a00297 494 FfsFileEntry->FfsHeader = FfsHeader;\r
495 InsertTailList (&FvDevice->FfsFileListHeader, &FfsFileEntry->Link);\r
496 }\r
497\r
6c85d162
SZ
498 if (IS_FFS_FILE2 (FfsHeader)) {\r
499 FfsHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsHeader + FFS_FILE2_SIZE (FfsHeader));\r
500 } else {\r
501 FfsHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsHeader + FFS_FILE_SIZE (FfsHeader));\r
502 }\r
022c6d45 503\r
28a00297 504 //\r
505 // Adjust pointer to the next 8-byte aligned boundry.\r
506 //\r
507 FfsHeader = (EFI_FFS_FILE_HEADER *)(((UINTN)FfsHeader + 7) & ~0x07);\r
022c6d45 508\r
28a00297 509 }\r
510\r
511Done:\r
512 if (EFI_ERROR (Status)) {\r
513 FreeFvDeviceResource (FvDevice);\r
514 }\r
515\r
516 return Status;\r
517}\r
518\r
519\r
162ed594 520\r
521/**\r
522 This notification function is invoked when an instance of the\r
6c85d162 523 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL is produced. It layers an instance of the\r
162ed594 524 EFI_FIRMWARE_VOLUME2_PROTOCOL on the same handle. This is the function where\r
525 the actual initialization of the EFI_FIRMWARE_VOLUME2_PROTOCOL is done.\r
526\r
022c6d45 527 @param Event The event that occured\r
162ed594 528 @param Context For EFI compatiblity. Not used.\r
529\r
530**/\r
28a00297 531VOID\r
532EFIAPI\r
533NotifyFwVolBlock (\r
534 IN EFI_EVENT Event,\r
535 IN VOID *Context\r
536 )\r
28a00297 537{\r
538 EFI_HANDLE Handle;\r
539 EFI_STATUS Status;\r
540 UINTN BufferSize;\r
541 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
0c2b5da8 542 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
28a00297 543 FV_DEVICE *FvDevice;\r
544 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
545 //\r
546 // Examine all new handles\r
547 //\r
548 for (;;) {\r
549 //\r
550 // Get the next handle\r
551 //\r
552 BufferSize = sizeof (Handle);\r
553 Status = CoreLocateHandle (\r
554 ByRegisterNotify,\r
555 NULL,\r
556 gEfiFwVolBlockNotifyReg,\r
557 &BufferSize,\r
558 &Handle\r
559 );\r
560\r
561 //\r
562 // If not found, we're done\r
563 //\r
564 if (EFI_NOT_FOUND == Status) {\r
565 break;\r
566 }\r
567\r
568 if (EFI_ERROR (Status)) {\r
569 continue;\r
570 }\r
022c6d45 571\r
28a00297 572 //\r
573 // Get the FirmwareVolumeBlock protocol on that handle\r
574 //\r
022c6d45 575 Status = CoreHandleProtocol (Handle, &gEfiFirmwareVolumeBlockProtocolGuid, (VOID **)&Fvb);\r
28a00297 576 ASSERT_EFI_ERROR (Status);\r
20bcdbcb 577 ASSERT (Fvb != NULL);\r
28a00297 578\r
579 //\r
580 // Make sure the Fv Header is O.K.\r
581 //\r
582 Status = GetFwVolHeader (Fvb, &FwVolHeader);\r
583 if (EFI_ERROR (Status)) {\r
584 return;\r
585 }\r
d2fbaaab 586 ASSERT (FwVolHeader != NULL);\r
28a00297 587\r
588 if (!VerifyFvHeaderChecksum (FwVolHeader)) {\r
589 CoreFreePool (FwVolHeader);\r
590 continue;\r
591 }\r
592\r
593\r
594 //\r
595 // Check to see that the file system is indeed formatted in a way we can\r
596 // understand it...\r
597 //\r
6c85d162
SZ
598 if ((!CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem2Guid)) &&\r
599 (!CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem3Guid))) {\r
28a00297 600 continue;\r
601 }\r
602\r
603 //\r
604 // Check if there is an FV protocol already installed in that handle\r
605 //\r
0c2b5da8 606 Status = CoreHandleProtocol (Handle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);\r
28a00297 607 if (!EFI_ERROR (Status)) {\r
608 //\r
609 // Update Fv to use a new Fvb\r
610 //\r
50d7ebad 611 FvDevice = BASE_CR (Fv, FV_DEVICE, Fv);\r
0c2b5da8 612 if (FvDevice->Signature == FV2_DEVICE_SIGNATURE) {\r
28a00297 613 //\r
614 // Only write into our device structure if it's our device structure\r
615 //\r
616 FvDevice->Fvb = Fvb;\r
617 }\r
618\r
619 } else {\r
620 //\r
621 // No FwVol protocol on the handle so create a new one\r
622 //\r
9c4ac31c 623 FvDevice = AllocateCopyPool (sizeof (FV_DEVICE), &mFvDevice);\r
28a00297 624 if (FvDevice == NULL) {\r
625 return;\r
626 }\r
022c6d45 627\r
e94a9ff7 628 FvDevice->Fvb = Fvb;\r
629 FvDevice->Handle = Handle;\r
630 FvDevice->FwVolHeader = FwVolHeader;\r
28a00297 631 FvDevice->Fv.ParentHandle = Fvb->ParentHandle;\r
6c85d162 632 FvDevice->IsFfs3Fv = CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem3Guid);\r
022c6d45 633\r
28a00297 634 //\r
635 // Install an New FV protocol on the existing handle\r
636 //\r
637 Status = CoreInstallProtocolInterface (\r
638 &Handle,\r
0c2b5da8 639 &gEfiFirmwareVolume2ProtocolGuid,\r
28a00297 640 EFI_NATIVE_INTERFACE,\r
641 &FvDevice->Fv\r
642 );\r
643 ASSERT_EFI_ERROR (Status);\r
644 }\r
645 }\r
022c6d45 646\r
28a00297 647 return;\r
648}\r
649\r
650\r
162ed594 651\r
652/**\r
13492369 653 This routine is the driver initialization entry point. It registers\r
654 a notification function. This notification function are responsible\r
655 for building the FV stack dynamically.\r
162ed594 656\r
022c6d45 657 @param ImageHandle The image handle.\r
658 @param SystemTable The system table.\r
162ed594 659\r
660 @retval EFI_SUCCESS Function successfully returned.\r
661\r
662**/\r
28a00297 663EFI_STATUS\r
664EFIAPI\r
665FwVolDriverInit (\r
666 IN EFI_HANDLE ImageHandle,\r
667 IN EFI_SYSTEM_TABLE *SystemTable\r
668 )\r
28a00297 669{\r
7899b797 670 gEfiFwVolBlockEvent = EfiCreateProtocolNotifyEvent (\r
28a00297 671 &gEfiFirmwareVolumeBlockProtocolGuid,\r
672 TPL_CALLBACK,\r
673 NotifyFwVolBlock,\r
674 NULL,\r
7899b797 675 &gEfiFwVolBlockNotifyReg\r
28a00297 676 );\r
677 return EFI_SUCCESS;\r
678}\r
679\r
162ed594 680\r