3 Firmware Volume Block Protocol Runtime Interface Abstraction
4 And FVB Extension protocol Runtime Interface Abstraction
6 mFvbEntry is an array of Handle Fvb pairs. The Fvb Lib Instance matches the
7 index in the mFvbEntry array. This should be the same sequence as the FVB's
8 were described in the HOB. We have to remember the handle so we can tell if
9 the protocol has been reinstalled and it needs updateing.
11 If you are using any of these lib functions.you must first call FvbInitialize ().
13 Copyright (c) 2006 - 2008, Intel Corporation
14 All rights reserved. This program and the accompanying materials
15 are licensed and made available under the terms and conditions of the BSD License
16 which accompanies this distribution. The full text of the license may be found at
17 http://opensource.org/licenses/bsd-license.php
19 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
20 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
28 // Event for Set Virtual Map Changed Event
30 EFI_EVENT mSetVirtualMapChangedEvent
= NULL
;
33 // Lib will ASSERT if more FVB devices than this are added to the system.
36 EFI_EVENT mFvbRegistration
;
40 Check whether an address is runtime memory or not.
42 @param Address The Address being checked.
44 @retval TRUE The address is runtime memory.
45 @retval FALSE The address is not runtime memory.
53 UINT8 TmpMemoryMap
[1];
56 UINT32 DescriptorVersion
;
58 EFI_MEMORY_DESCRIPTOR
*MemoryMap
;
59 EFI_MEMORY_DESCRIPTOR
*MemoryMapPtr
;
66 // Get System MemoryMapSize
69 Status
= gBS
->GetMemoryMap (
71 (EFI_MEMORY_DESCRIPTOR
*)TmpMemoryMap
,
76 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
78 // Enlarge space here, because we will allocate pool now.
80 MemoryMapSize
+= EFI_PAGE_SIZE
;
81 Status
= gBS
->AllocatePool (
86 ASSERT_EFI_ERROR (Status
);
89 // Get System MemoryMap
91 Status
= gBS
->GetMemoryMap (
98 ASSERT_EFI_ERROR (Status
);
100 MemoryMapPtr
= MemoryMap
;
102 // Search the request Address
104 for (Index
= 0; Index
< (MemoryMapSize
/ DescriptorSize
); Index
++) {
105 if (((EFI_PHYSICAL_ADDRESS
)(UINTN
)Address
>= MemoryMap
->PhysicalStart
) &&
106 ((EFI_PHYSICAL_ADDRESS
)(UINTN
)Address
< MemoryMap
->PhysicalStart
107 + LShiftU64 (MemoryMap
->NumberOfPages
, EFI_PAGE_SHIFT
))) {
111 if (MemoryMap
->Attribute
& EFI_MEMORY_RUNTIME
) {
119 MemoryMap
= (EFI_MEMORY_DESCRIPTOR
*)((UINTN
)MemoryMap
+ DescriptorSize
);
125 gBS
->FreePool (MemoryMapPtr
);
131 Update mFvbEntry. Add new entry, or update existing entry if Fvb protocol is
134 @param Event The Event that is being processed
135 @param Context Event Context
140 FvbNotificationEvent (
152 BufferSize
= sizeof (Handle
);
153 Status
= gBS
->LocateHandle (
155 &gEfiFirmwareVolumeBlockProtocolGuid
,
160 if (EFI_ERROR (Status
)) {
162 // Exit Path of While Loop....
167 UpdateIndex
= MAX_FVB_COUNT
;
168 for (Index
= 0; Index
< mFvbCount
; Index
++) {
169 if (mFvbEntry
[Index
].Handle
== Handle
) {
171 // If the handle is already in the table just update the protocol
178 if (UpdateIndex
== MAX_FVB_COUNT
) {
180 // Use the next free slot for a new entry
182 UpdateIndex
= mFvbCount
++;
184 // Check the UpdateIndex whether exceed the maximum value.
186 ASSERT (UpdateIndex
< MAX_FVB_COUNT
);
187 mFvbEntry
[UpdateIndex
].Handle
= Handle
;
190 // The array does not have enough entries
192 ASSERT (UpdateIndex
< MAX_FVB_COUNT
);
195 // Get the interface pointer and if it's ours, skip it
197 Status
= gBS
->HandleProtocol (
199 &gEfiFirmwareVolumeBlockProtocolGuid
,
200 (VOID
**) &mFvbEntry
[UpdateIndex
].Fvb
202 ASSERT_EFI_ERROR (Status
);
204 Status
= gBS
->HandleProtocol (
206 &gEfiFvbExtensionProtocolGuid
,
207 (VOID
**) &mFvbEntry
[UpdateIndex
].FvbExtension
209 if (Status
!= EFI_SUCCESS
) {
210 mFvbEntry
[UpdateIndex
].FvbExtension
= NULL
;
214 // Check the FVB can be accessed in RUNTIME, The FVBs in FVB handle list comes
216 // 1) Dxe Core. (FVB information is transferred from FV HOB).
218 // The FVB produced Dxe core is used for discoverying DXE driver and dispatch. These
219 // FVBs can only be accessed in boot time.
220 // FVB driver will discovery all FV in FLASH and these FVBs can be accessed in runtime.
221 // The FVB itself produced by FVB driver is allocated in runtime memory. So we can
222 // determine the what FVB can be accessed in RUNTIME by judging whether FVB itself is allocated
223 // in RUNTIME memory.
225 mFvbEntry
[UpdateIndex
].IsRuntimeAccess
= IsRuntimeMemory (mFvbEntry
[UpdateIndex
].Fvb
);
230 Convert all pointers in mFvbEntry after ExitBootServices.
232 @param Event The Event that is being processed
233 @param Context Event Context
238 FvbVirtualAddressChangeNotifyEvent (
244 if (mFvbEntry
!= NULL
) {
245 for (Index
= 0; Index
< MAX_FVB_COUNT
; Index
++) {
246 if (!mFvbEntry
[Index
].IsRuntimeAccess
) {
250 if (NULL
!= mFvbEntry
[Index
].Fvb
) {
251 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].Fvb
->GetBlockSize
);
252 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].Fvb
->GetPhysicalAddress
);
253 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].Fvb
->GetAttributes
);
254 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].Fvb
->SetAttributes
);
255 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].Fvb
->Read
);
256 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].Fvb
->Write
);
257 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].Fvb
->EraseBlocks
);
258 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].Fvb
);
261 if (NULL
!= mFvbEntry
[Index
].FvbExtension
) {
262 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].FvbExtension
->EraseFvbCustomBlock
);
263 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].FvbExtension
);
267 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
);
272 Library constructor function entry.
274 @param ImageHandle The handle of image who call this libary.
275 @param SystemTable The point of System Table.
277 @retval EFI_SUCESS Sucess construct this library.
278 @retval Others Fail to contruct this libary.
283 IN EFI_HANDLE ImageHandle
,
284 IN EFI_SYSTEM_TABLE
*SystemTable
290 Status
= gBS
->AllocatePool (
291 EfiRuntimeServicesData
,
292 (UINTN
) sizeof (FVB_ENTRY
) * MAX_FVB_COUNT
,
296 if (EFI_ERROR (Status
)) {
300 ZeroMem (mFvbEntry
, sizeof (FVB_ENTRY
) * MAX_FVB_COUNT
);
302 EfiCreateProtocolNotifyEvent (
303 &gEfiFirmwareVolumeBlockProtocolGuid
,
305 FvbNotificationEvent
,
311 // Register SetVirtualAddressMap () notify function
313 Status
= gBS
->CreateEvent (
314 EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
,
316 FvbVirtualAddressChangeNotifyEvent
,
318 &mSetVirtualMapChangedEvent
320 ASSERT_EFI_ERROR (Status
);
326 // =============================================================================
327 // The following functions wrap Fvb protocol in the Runtime Lib functions.
328 // The Instance translates into Fvb instance. The Fvb order defined by HOBs and
329 // thus the sequence of FVB protocol addition define Instance.
331 // EfiFvbInitialize () must be called before any of the following functions
333 // =============================================================================
337 Reads specified number of bytes into a buffer from the specified block.
339 The EfiFvbReadBlock() function reads the requested number of bytes from
340 the requested block in the specified firmware volume and stores them in
341 the provided buffer. Implementations should be mindful that the firmware
342 volume might be in the ReadDisabled state. If it is in this state, the
343 EfiFvbReadBlock() function must return the status code EFI_ACCESS_DENIED
344 without modifying the contents of the buffer.
346 The EfiFvbReadBlock() function must also prevent spanning block boundaries.
347 If a read is requested that would span a block boundary, the read must read
348 up to the boundary but not beyond. The output parameter NumBytes must be
349 set to correctly indicate the number of bytes actually read.
350 The caller must be aware that a read may be partially completed.
352 If NumBytes is NULL, then ASSERT().
354 If Buffer is NULL, then ASSERT().
356 @param[in] Instance The FV instance to be read from.
357 @param[in] Lba The logical block address to be read from
358 @param[in] Offset The offset relative to the block, at which to begin reading.
359 @param[in, out] NumBytes Pointer to a UINTN. On input, *NumBytes contains the total
360 size of the buffer. On output, it contains the actual number
362 @param[out] Buffer Pointer to a caller allocated buffer that will be
363 used to hold the data read.
365 @retval EFI_SUCCESS The firmware volume was read successfully and contents are in Buffer.
366 @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary. On output, NumBytes contains the total number of bytes returned in Buffer.
367 @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state.
368 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be read.
369 @retval EFI_INVALID_PARAMETER Invalid parameter, Instance is larger than the max FVB number. Lba index is larger than the last block of the firmware volume. Offset is larger than the block size.
377 IN OUT UINTN
*NumBytes
,
381 ASSERT (NumBytes
!= NULL
);
382 ASSERT (Buffer
!= NULL
);
384 if (Instance
>= mFvbCount
) {
385 return EFI_INVALID_PARAMETER
;
388 if (EfiAtRuntime() && !mFvbEntry
[Instance
].IsRuntimeAccess
) {
389 return EFI_INVALID_PARAMETER
;
392 return mFvbEntry
[Instance
].Fvb
->Read (mFvbEntry
[Instance
].Fvb
, Lba
, Offset
, NumBytes
, Buffer
);
396 Writes specified number of bytes from the input buffer to the block
398 The EfiFvbWriteBlock() function writes the specified number of bytes
399 from the provided buffer to the specified block and offset in the
400 requested firmware volume.
402 If the firmware volume is sticky write, the caller must ensure that
403 all the bits of the specified range to write are in the EFI_FVB_ERASE_POLARITY
404 state before calling the EfiFvbWriteBlock() function, or else the
405 result will be unpredictable. This unpredictability arises because,
406 for a sticky-write firmware volume, a write may negate a bit in the
407 EFI_FVB_ERASE_POLARITY state but it cannot flip it back again. In
408 general, before calling the EfiFvbWriteBlock() function, the caller
409 should call the EfiFvbEraseBlock() function first to erase the specified
410 block to write. A block erase cycle will transition bits from the
411 (NOT)EFI_FVB_ERASE_POLARITY state back to the EFI_FVB_ERASE_POLARITY state.
412 Implementations should be mindful that the firmware volume might be
413 in the WriteDisabled state. If it is in this state, the EfiFvbWriteBlock()
414 function must return the status code EFI_ACCESS_DENIED without modifying
415 the contents of the firmware volume.
417 The EfiFvbWriteBlock() function must also prevent spanning block boundaries.
418 If a write is requested that spans a block boundary, the write must store
419 up to the boundary but not beyond. The output parameter NumBytes must be
420 set to correctly indicate the number of bytes actually written. The caller
421 must be aware that a write may be partially completed.
422 All writes, partial or otherwise, must be fully flushed to the hardware
423 before the EfiFvbWriteBlock() function returns.
425 If NumBytes is NULL, then ASSERT().
427 @param Instance The FV instance to be written to
428 @param Lba The starting logical block index to write to
429 @param Offset The offset relative to the block, at which to begin writting.
430 @param NumBytes Pointer to a UINTN. On input, *NumBytes contains
431 the total size of the buffer. On output, it contains
432 the actual number of bytes written.
433 @param Buffer Pointer to a caller allocated buffer that contains
434 the source for the write
436 @retval EFI_SUCCESS The firmware volume was written successfully.
437 @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
438 On output, NumBytes contains the total number of bytes actually written.
439 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
440 @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not be written.
441 @retval EFI_INVALID_PARAMETER Invalid parameter, Instance is larger than the max FVB number.
442 Lba index is larger than the last block of the firmware volume.
443 Offset is larger than the block size.
450 IN OUT UINTN
*NumBytes
,
454 ASSERT (NumBytes
!= NULL
);
456 if (Instance
>= mFvbCount
) {
457 return EFI_INVALID_PARAMETER
;
460 if (EfiAtRuntime() && !mFvbEntry
[Instance
].IsRuntimeAccess
) {
461 return EFI_INVALID_PARAMETER
;
464 return mFvbEntry
[Instance
].Fvb
->Write (mFvbEntry
[Instance
].Fvb
, Lba
, Offset
, NumBytes
, Buffer
);
468 Erases and initializes a firmware volume block.
470 The EfiFvbEraseBlock() function erases one block specified by Lba.
471 Implementations should be mindful that the firmware volume might
472 be in the WriteDisabled state. If it is in this state, the EfiFvbEraseBlock()
473 function must return the status code EFI_ACCESS_DENIED without
474 modifying the contents of the firmware volume. If Instance is
475 larger than the max FVB number, or Lba index is larger than the
476 last block of the firmware volume, this function return the status
477 code EFI_INVALID_PARAMETER.
479 All calls to EfiFvbEraseBlock() must be fully flushed to the
480 hardware before this function returns.
482 @param[in] Instance The FV instance to be erased.
483 @param[in] Lba The logical block index to be erased from.
485 @retval EFI_SUCCESS The erase request was successfully completed.
486 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
487 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
488 could not be written. The firmware device may
489 have been partially erased.
490 @retval EFI_INVALID_PARAMETER Invalid parameter. Instance is larger than the max
491 FVB number. Lba index is larger than the last block
492 of the firmware volume.
501 if (Instance
>= mFvbCount
) {
502 return EFI_INVALID_PARAMETER
;
505 if (EfiAtRuntime() && !mFvbEntry
[Instance
].IsRuntimeAccess
) {
506 return EFI_INVALID_PARAMETER
;
509 return mFvbEntry
[Instance
].Fvb
->EraseBlocks (mFvbEntry
[Instance
].Fvb
, Lba
, 1, EFI_LBA_LIST_TERMINATOR
);
513 Retrieves the attributes and current settings of the specified block,
514 returns resulting attributes in output parameter.
516 The EfiFvbGetAttributes() function retrieves the attributes and current
517 settings of the block specified by Instance. If Instance is larger than
518 the max FVB number, this function returns the status code EFI_INVALID_PARAMETER.
520 If Attributes is NULL, then ASSERT().
522 @param[in] Instance The FV instance to be operated.
523 @param[out] Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the
524 attributes and current settings are returned.
526 @retval EFI_EFI_SUCCESS The firmware volume attributes were returned.
527 @retval EFI_INVALID_PARAMETER Invalid parameter. Instance is larger than the max FVB number.
530 EfiFvbGetVolumeAttributes (
532 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
535 ASSERT (Attributes
!= NULL
);
537 if (Instance
>= mFvbCount
) {
538 return EFI_INVALID_PARAMETER
;
541 if (EfiAtRuntime() && !mFvbEntry
[Instance
].IsRuntimeAccess
) {
542 return EFI_INVALID_PARAMETER
;
545 return mFvbEntry
[Instance
].Fvb
->GetAttributes (mFvbEntry
[Instance
].Fvb
, Attributes
);
549 Modify the attributes and current settings of the specified block
550 according to the input parameter.
552 The EfiFvbSetAttributes() function sets configurable firmware volume
553 attributes and returns the new settings of the firmware volume specified
554 by Instance. If Instance is larger than the max FVB number, this function
555 returns the status code EFI_INVALID_PARAMETER.
557 If Attributes is NULL, then ASSERT().
559 @param[in] Instance The FV instance to be operated.
560 @param[in, out]Attributes On input, Attributes is a pointer to EFI_FVB_ATTRIBUTES_2
561 that contains the desired firmware volume settings.
562 On successful return, it contains the new settings of the firmware volume.
564 @retval EFI_EFI_SUCCESS The firmware volume attributes were modified successfully.
565 @retval EFI_INVALID_PARAMETER Invalid parameter. Instance is larger than the max FVB number.
569 EfiFvbSetVolumeAttributes (
571 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
574 ASSERT (Attributes
!= NULL
);
576 if (Instance
>= mFvbCount
) {
577 return EFI_INVALID_PARAMETER
;
580 if (EfiAtRuntime() && !mFvbEntry
[Instance
].IsRuntimeAccess
) {
581 return EFI_INVALID_PARAMETER
;
584 return mFvbEntry
[Instance
].Fvb
->SetAttributes (mFvbEntry
[Instance
].Fvb
, Attributes
);
588 Retrieves the physical address of the specified memory mapped FV.
590 Retrieve the base address of a memory-mapped firmware volume specified by Instance.
591 If Instance is larger than the max FVB number, this function returns the status
592 code EFI_INVALID_PARAMETER.
594 If BaseAddress is NULL, then ASSERT().
596 @param[in] Instance The FV instance to be operated.
597 @param[out] BaseAddress Pointer to a caller allocated EFI_PHYSICAL_ADDRESS
598 that on successful return, contains the base address
599 of the firmware volume.
601 @retval EFI_EFI_SUCCESS The firmware volume base address is returned.
602 @retval EFI_INVALID_PARAMETER Invalid parameter. Instance is larger than the max FVB number.
606 EfiFvbGetPhysicalAddress (
608 OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
611 ASSERT (BaseAddress
!= NULL
);
613 if (Instance
>= mFvbCount
) {
614 return EFI_INVALID_PARAMETER
;
617 if (EfiAtRuntime() && !mFvbEntry
[Instance
].IsRuntimeAccess
) {
618 return EFI_INVALID_PARAMETER
;
621 return mFvbEntry
[Instance
].Fvb
->GetPhysicalAddress (mFvbEntry
[Instance
].Fvb
, BaseAddress
);
625 Retrieve the block size of the specified fv.
627 The EfiFvbGetBlockSize() function retrieves the size of the requested block.
628 It also returns the number of additional blocks with the identical size.
629 If Instance is larger than the max FVB number, or Lba index is larger than
630 the last block of the firmware volume, this function return the status code
631 EFI_INVALID_PARAMETER.
633 If BlockSize is NULL, then ASSERT().
635 If NumOfBlocks is NULL, then ASSERT().
637 @param[in] Instance The FV instance to be operated.
638 @param[in] Lba Indicates which block to return the size for.
639 @param[out] BlockSize Pointer to a caller-allocated UINTN in which the
640 size of the block is returned.
641 @param[out] NumOfBlocks Pointer to a caller-allocated UINTN in which the
642 number of consecutive blocks, starting with Lba,
643 is returned. All blocks in this range have a size of BlockSize.
645 @retval EFI_EFI_SUCCESS The firmware volume base address is returned.
646 @retval EFI_INVALID_PARAMETER Invalid parameter. Instance is larger than the max FVB number.
647 Lba index is larger than the last block of the firmware volume.
654 OUT UINTN
*BlockSize
,
655 OUT UINTN
*NumOfBlocks
658 ASSERT (BlockSize
!= NULL
);
659 ASSERT (NumOfBlocks
!= NULL
);
661 if (Instance
>= mFvbCount
) {
662 return EFI_INVALID_PARAMETER
;
665 if (EfiAtRuntime() && !mFvbEntry
[Instance
].IsRuntimeAccess
) {
666 return EFI_INVALID_PARAMETER
;
669 return mFvbEntry
[Instance
].Fvb
->GetBlockSize (mFvbEntry
[Instance
].Fvb
, Lba
, BlockSize
, NumOfBlocks
);
673 Erases and initializes a specified range of a firmware volume.
675 The EfiFvbEraseCustomBlockRange() function erases the specified range in the firmware
676 volume index by Instance. If Instance is larger than the max FVB number, StartLba or
677 LastLba index is larger than the last block of the firmware volume, StartLba > LastLba
678 or StartLba equal to LastLba but OffsetStartLba > OffsetLastLba, this function return
679 the status code EFI_INVALID_PARAMETER.
681 @param[in] Instance The FV instance to be operated.
682 @param[in] StartLba The starting logical block index to be erased.
683 @param[in] OffsetStartLba Offset into the starting block at which to
685 @param[in] LastLba The last logical block index to be erased.
686 @param[in] OffsetLastLba Offset into the last block at which to end erasing.
688 @retval EFI_EFI_SUCCESS Successfully erase custom block range
689 @retval EFI_INVALID_PARAMETER Invalid parameter. Instance is larger than the max FVB number.
690 @retval EFI_UNSUPPORTED Firmware volume block device has no this capability.
694 EfiFvbEraseCustomBlockRange (
697 IN UINTN OffsetStartLba
,
699 IN UINTN OffsetLastLba
702 if (Instance
>= mFvbCount
) {
703 return EFI_INVALID_PARAMETER
;
706 if (EfiAtRuntime() && !mFvbEntry
[Instance
].IsRuntimeAccess
) {
707 return EFI_INVALID_PARAMETER
;
710 if (!(mFvbEntry
[Instance
].FvbExtension
)) {
711 return EFI_UNSUPPORTED
;
714 if (!(mFvbEntry
[Instance
].FvbExtension
->EraseFvbCustomBlock
)) {
715 return EFI_UNSUPPORTED
;
718 return mFvbEntry
[Instance
].FvbExtension
->EraseFvbCustomBlock (
719 mFvbEntry
[Instance
].FvbExtension
,