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