]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/FwVol/FwVol.c
fc8d57c071495e7b9f78045b5a3be12acea215df
[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 - 2010, Intel Corporation. <BR>
7 All rights reserved. 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 UINTN FileLength;
308 EFI_FFS_FILE_STATE FileState;
309 UINT8 *TopFvAddress;
310 UINTN TestLength;
311
312
313 Fvb = FvDevice->Fvb;
314 FwVolHeader = FvDevice->FwVolHeader;
315
316 Status = Fvb->GetAttributes (Fvb, &FvbAttributes);
317 if (EFI_ERROR (Status)) {
318 return Status;
319 }
320
321 //
322 // Size is the size of the FV minus the head. We have already allocated
323 // the header to check to make sure the volume is valid
324 //
325 Size = (UINTN)(FwVolHeader->FvLength - FwVolHeader->HeaderLength);
326 FvDevice->CachedFv = AllocatePool (Size);
327
328 if (FvDevice->CachedFv == NULL) {
329 return EFI_OUT_OF_RESOURCES;
330 }
331
332 //
333 // Remember a pointer to the end fo the CachedFv
334 //
335 FvDevice->EndOfCachedFv = FvDevice->CachedFv + Size;
336
337 //
338 // Copy FV minus header into memory using the block map we have all ready
339 // read into memory.
340 //
341 BlockMap = FwVolHeader->BlockMap;
342 CacheLocation = FvDevice->CachedFv;
343 LbaIndex = 0;
344 LbaOffset = 0;
345 HeaderSize = FwVolHeader->HeaderLength;
346 while ((BlockMap->NumBlocks != 0) || (BlockMap->Length != 0)) {
347 Index = 0;
348 Size = BlockMap->Length;
349 if (HeaderSize > 0) {
350 //
351 // Skip header size
352 //
353 for (; Index < BlockMap->NumBlocks && HeaderSize >= BlockMap->Length; Index ++) {
354 HeaderSize -= BlockMap->Length;
355 LbaIndex ++;
356 }
357
358 //
359 // Check whether FvHeader is crossing the multi block range.
360 //
361 if (HeaderSize > BlockMap->Length) {
362 BlockMap++;
363 continue;
364 } else if (HeaderSize > 0) {
365 LbaOffset = HeaderSize;
366 Size = BlockMap->Length - HeaderSize;
367 HeaderSize = 0;
368 }
369 }
370
371 //
372 // read the FV data
373 //
374 for (; Index < BlockMap->NumBlocks; Index ++) {
375 Status = Fvb->Read (Fvb,
376 LbaIndex,
377 LbaOffset,
378 &Size,
379 CacheLocation
380 );
381
382 //
383 // Not check EFI_BAD_BUFFER_SIZE, for Size = BlockMap->Length
384 //
385 if (EFI_ERROR (Status)) {
386 goto Done;
387 }
388
389 LbaIndex++;
390 CacheLocation += Size;
391
392 //
393 // After we skip Fv Header always read from start of block
394 //
395 LbaOffset = 0;
396 Size = BlockMap->Length;
397 }
398
399 BlockMap++;
400 }
401
402 //
403 // Scan to check the free space & File list
404 //
405 if ((FvbAttributes & EFI_FVB2_ERASE_POLARITY) != 0) {
406 FvDevice->ErasePolarity = 1;
407 } else {
408 FvDevice->ErasePolarity = 0;
409 }
410
411
412 //
413 // go through the whole FV cache, check the consistence of the FV.
414 // Make a linked list off all the Ffs file headers
415 //
416 Status = EFI_SUCCESS;
417 InitializeListHead (&FvDevice->FfsFileListHeader);
418
419 //
420 // Build FFS list
421 //
422 FfsHeader = (EFI_FFS_FILE_HEADER *) FvDevice->CachedFv;
423 TopFvAddress = FvDevice->EndOfCachedFv;
424 while ((UINT8 *) FfsHeader < TopFvAddress) {
425
426 TestLength = TopFvAddress - ((UINT8 *) FfsHeader);
427 if (TestLength > sizeof (EFI_FFS_FILE_HEADER)) {
428 TestLength = sizeof (EFI_FFS_FILE_HEADER);
429 }
430
431 if (IsBufferErased (FvDevice->ErasePolarity, FfsHeader, TestLength)) {
432 //
433 // We have found the free space so we are done!
434 //
435 goto Done;
436 }
437
438 if (!IsValidFfsHeader (FvDevice->ErasePolarity, FfsHeader, &FileState)) {
439 if ((FileState == EFI_FILE_HEADER_INVALID) ||
440 (FileState == EFI_FILE_HEADER_CONSTRUCTION)) {
441 FfsHeader++;
442 continue;
443 } else {
444 //
445 // File system is corrputed
446 //
447 Status = EFI_VOLUME_CORRUPTED;
448 goto Done;
449 }
450 }
451
452 if (!IsValidFfsFile (FvDevice->ErasePolarity, FfsHeader)) {
453 //
454 // File system is corrupted
455 //
456 Status = EFI_VOLUME_CORRUPTED;
457 goto Done;
458 }
459
460 //
461 // Size[3] is a three byte array, read 4 bytes and throw one away
462 //
463 FileLength = *(UINT32 *)&FfsHeader->Size[0] & 0x00FFFFFF;
464
465 FileState = GetFileState (FvDevice->ErasePolarity, FfsHeader);
466
467 //
468 // check for non-deleted file
469 //
470 if (FileState != EFI_FILE_DELETED) {
471 //
472 // Create a FFS list entry for each non-deleted file
473 //
474 FfsFileEntry = AllocateZeroPool (sizeof (FFS_FILE_LIST_ENTRY));
475 if (FfsFileEntry == NULL) {
476 Status = EFI_OUT_OF_RESOURCES;
477 goto Done;
478 }
479
480 FfsFileEntry->FfsHeader = FfsHeader;
481 InsertTailList (&FvDevice->FfsFileListHeader, &FfsFileEntry->Link);
482 }
483
484 FfsHeader = (EFI_FFS_FILE_HEADER *)(((UINT8 *)FfsHeader) + FileLength);
485
486 //
487 // Adjust pointer to the next 8-byte aligned boundry.
488 //
489 FfsHeader = (EFI_FFS_FILE_HEADER *)(((UINTN)FfsHeader + 7) & ~0x07);
490
491 }
492
493 Done:
494 if (EFI_ERROR (Status)) {
495 FreeFvDeviceResource (FvDevice);
496 }
497
498 return Status;
499 }
500
501
502
503 /**
504 This notification function is invoked when an instance of the
505 EFI_FW_VOLUME_BLOCK_PROTOCOL is produced. It layers an instance of the
506 EFI_FIRMWARE_VOLUME2_PROTOCOL on the same handle. This is the function where
507 the actual initialization of the EFI_FIRMWARE_VOLUME2_PROTOCOL is done.
508
509 @param Event The event that occured
510 @param Context For EFI compatiblity. Not used.
511
512 **/
513 VOID
514 EFIAPI
515 NotifyFwVolBlock (
516 IN EFI_EVENT Event,
517 IN VOID *Context
518 )
519 {
520 EFI_HANDLE Handle;
521 EFI_STATUS Status;
522 UINTN BufferSize;
523 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
524 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
525 FV_DEVICE *FvDevice;
526 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
527 //
528 // Examine all new handles
529 //
530 for (;;) {
531 //
532 // Get the next handle
533 //
534 BufferSize = sizeof (Handle);
535 Status = CoreLocateHandle (
536 ByRegisterNotify,
537 NULL,
538 gEfiFwVolBlockNotifyReg,
539 &BufferSize,
540 &Handle
541 );
542
543 //
544 // If not found, we're done
545 //
546 if (EFI_NOT_FOUND == Status) {
547 break;
548 }
549
550 if (EFI_ERROR (Status)) {
551 continue;
552 }
553
554 //
555 // Get the FirmwareVolumeBlock protocol on that handle
556 //
557 Status = CoreHandleProtocol (Handle, &gEfiFirmwareVolumeBlockProtocolGuid, (VOID **)&Fvb);
558 ASSERT_EFI_ERROR (Status);
559 ASSERT (Fvb != NULL);
560
561 //
562 // Make sure the Fv Header is O.K.
563 //
564 Status = GetFwVolHeader (Fvb, &FwVolHeader);
565 if (EFI_ERROR (Status)) {
566 return;
567 }
568 ASSERT (FwVolHeader != NULL);
569
570 if (!VerifyFvHeaderChecksum (FwVolHeader)) {
571 CoreFreePool (FwVolHeader);
572 continue;
573 }
574
575
576 //
577 // Check to see that the file system is indeed formatted in a way we can
578 // understand it...
579 //
580 if (!CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem2Guid)) {
581 continue;
582 }
583
584 //
585 // Check if there is an FV protocol already installed in that handle
586 //
587 Status = CoreHandleProtocol (Handle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);
588 if (!EFI_ERROR (Status)) {
589 //
590 // Update Fv to use a new Fvb
591 //
592 FvDevice = BASE_CR (Fv, FV_DEVICE, Fv);
593 if (FvDevice->Signature == FV2_DEVICE_SIGNATURE) {
594 //
595 // Only write into our device structure if it's our device structure
596 //
597 FvDevice->Fvb = Fvb;
598 }
599
600 } else {
601 //
602 // No FwVol protocol on the handle so create a new one
603 //
604 FvDevice = AllocateCopyPool (sizeof (FV_DEVICE), &mFvDevice);
605 if (FvDevice == NULL) {
606 return;
607 }
608
609 FvDevice->Fvb = Fvb;
610 FvDevice->Handle = Handle;
611 FvDevice->FwVolHeader = FwVolHeader;
612 FvDevice->Fv.ParentHandle = Fvb->ParentHandle;
613
614 //
615 // Install an New FV protocol on the existing handle
616 //
617 Status = CoreInstallProtocolInterface (
618 &Handle,
619 &gEfiFirmwareVolume2ProtocolGuid,
620 EFI_NATIVE_INTERFACE,
621 &FvDevice->Fv
622 );
623 ASSERT_EFI_ERROR (Status);
624 }
625 }
626
627 return;
628 }
629
630
631
632 /**
633 This routine is the driver initialization entry point. It registers
634 a notification function. This notification function are responsible
635 for building the FV stack dynamically.
636
637 @param ImageHandle The image handle.
638 @param SystemTable The system table.
639
640 @retval EFI_SUCCESS Function successfully returned.
641
642 **/
643 EFI_STATUS
644 EFIAPI
645 FwVolDriverInit (
646 IN EFI_HANDLE ImageHandle,
647 IN EFI_SYSTEM_TABLE *SystemTable
648 )
649 {
650 gEfiFwVolBlockEvent = EfiCreateProtocolNotifyEvent (
651 &gEfiFirmwareVolumeBlockProtocolGuid,
652 TPL_CALLBACK,
653 NotifyFwVolBlock,
654 NULL,
655 &gEfiFwVolBlockNotifyReg
656 );
657 return EFI_SUCCESS;
658 }
659
660