3 Firmware Volume Block Protocol Runtime Abstraction
5 mFvbEntry is an array of Handle Fvb pairs. The Fvb Lib Instance matches the
6 index in the mFvbEntry array. This should be the same sequence as the FVB's
7 were described in the HOB. We have to remember the handle so we can tell if
8 the protocol has been reinstalled and it needs updateing.
10 If you are using any of these lib functions.you must first call FvbInitialize ().
12 Copyright (c) 2006, Intel Corporation
13 All rights reserved. This program and the accompanying materials
14 are licensed and made available under the terms and conditions of the BSD License
15 which accompanies this distribution. The full text of the license may be found at
16 http://opensource.org/licenses/bsd-license.php
18 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
19 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
26 // Event for Set Virtual Map Changed Event
28 STATIC EFI_EVENT mSetVirtualMapChangedEvent
= NULL
;
31 // Lib will ASSERT if more FVB devices than this are added to the system.
33 STATIC FVB_ENTRY
*mFvbEntry
;
34 STATIC EFI_EVENT mFvbRegistration
;
35 STATIC UINTN mFvbCount
;
38 Check whether an address is runtime memory or not.
40 @param Address The Address being checked.
42 @retval TRUE The address is runtime memory.
43 @retval FALSE The address is not runtime memory.
51 UINT8 TmpMemoryMap
[1];
54 UINT32 DescriptorVersion
;
56 EFI_MEMORY_DESCRIPTOR
*MemoryMap
;
57 EFI_MEMORY_DESCRIPTOR
*MemoryMapPtr
;
64 // Get System MemoryMapSize
67 Status
= gBS
->GetMemoryMap (
69 (EFI_MEMORY_DESCRIPTOR
*)TmpMemoryMap
,
74 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
76 // Enlarge space here, because we will allocate pool now.
78 MemoryMapSize
+= EFI_PAGE_SIZE
;
79 Status
= gBS
->AllocatePool (
84 ASSERT_EFI_ERROR (Status
);
87 // Get System MemoryMap
89 Status
= gBS
->GetMemoryMap (
96 ASSERT_EFI_ERROR (Status
);
98 MemoryMapPtr
= MemoryMap
;
100 // Search the request Address
102 for (Index
= 0; Index
< (MemoryMapSize
/ DescriptorSize
); Index
++) {
103 if (((EFI_PHYSICAL_ADDRESS
)(UINTN
)Address
>= MemoryMap
->PhysicalStart
) &&
104 ((EFI_PHYSICAL_ADDRESS
)(UINTN
)Address
< MemoryMap
->PhysicalStart
105 + LShiftU64 (MemoryMap
->NumberOfPages
, EFI_PAGE_SHIFT
))) {
109 if (MemoryMap
->Attribute
& EFI_MEMORY_RUNTIME
) {
117 MemoryMap
= (EFI_MEMORY_DESCRIPTOR
*)((UINTN
)MemoryMap
+ DescriptorSize
);
123 gBS
->FreePool (MemoryMapPtr
);
129 Update mFvbEntry. Add new entry, or update existing entry if Fvb protocol is
132 @param Event The Event that is being processed
133 @param Context Event Context
139 FvbNotificationEvent (
151 BufferSize
= sizeof (Handle
);
152 Status
= gBS
->LocateHandle (
154 &gEfiFirmwareVolumeBlockProtocolGuid
,
159 if (EFI_ERROR (Status
)) {
161 // Exit Path of While Loop....
166 UpdateIndex
= MAX_FVB_COUNT
;
167 for (Index
= 0; Index
< mFvbCount
; Index
++) {
168 if (mFvbEntry
[Index
].Handle
== Handle
) {
170 // If the handle is already in the table just update the protocol
177 if (UpdateIndex
== MAX_FVB_COUNT
) {
179 // Use the next free slot for a new entry
181 UpdateIndex
= mFvbCount
++;
183 // Check the UpdateIndex whether exceed the maximum value.
185 ASSERT (UpdateIndex
< MAX_FVB_COUNT
);
186 mFvbEntry
[UpdateIndex
].Handle
= Handle
;
189 // The array does not have enough entries
191 ASSERT (UpdateIndex
< MAX_FVB_COUNT
);
194 // Get the interface pointer and if it's ours, skip it
196 Status
= gBS
->HandleProtocol (
198 &gEfiFirmwareVolumeBlockProtocolGuid
,
199 (VOID
**) &mFvbEntry
[UpdateIndex
].Fvb
201 ASSERT_EFI_ERROR (Status
);
203 Status
= gBS
->HandleProtocol (
205 &gEfiFvbExtensionProtocolGuid
,
206 (VOID
**) &mFvbEntry
[UpdateIndex
].FvbExtension
208 if (Status
!= EFI_SUCCESS
) {
209 mFvbEntry
[UpdateIndex
].FvbExtension
= NULL
;
213 // Check the FVB can be accessed in RUNTIME, The FVBs in FVB handle list comes
215 // 1) Dxe Core. (FVB information is transferred from FV HOB).
217 // The FVB produced Dxe core is used for discoverying DXE driver and dispatch. These
218 // FVBs can only be accessed in boot time.
219 // FVB driver will discovery all FV in FLASH and these FVBs can be accessed in runtime.
220 // The FVB itself produced by FVB driver is allocated in runtime memory. So we can
221 // determine the what FVB can be accessed in RUNTIME by judging whether FVB itself is allocated
222 // in RUNTIME memory.
224 mFvbEntry
[UpdateIndex
].IsRuntimeAccess
= IsRuntimeMemory (mFvbEntry
[UpdateIndex
].Fvb
);
229 Convert all pointers in mFvbEntry after ExitBootServices.
231 @param Event The Event that is being processed
232 @param Context Event Context
237 FvbVirtualAddressChangeNotifyEvent (
243 if (mFvbEntry
!= NULL
) {
244 for (Index
= 0; Index
< MAX_FVB_COUNT
; Index
++) {
245 if (!mFvbEntry
[Index
].IsRuntimeAccess
) {
249 if (NULL
!= mFvbEntry
[Index
].Fvb
) {
250 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].Fvb
->GetBlockSize
);
251 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].Fvb
->GetPhysicalAddress
);
252 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].Fvb
->GetAttributes
);
253 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].Fvb
->SetAttributes
);
254 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].Fvb
->Read
);
255 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].Fvb
->Write
);
256 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].Fvb
->EraseBlocks
);
257 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].Fvb
);
260 if (NULL
!= mFvbEntry
[Index
].FvbExtension
) {
261 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].FvbExtension
->EraseFvbCustomBlock
);
262 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
[Index
].FvbExtension
);
266 EfiConvertPointer (0x0, (VOID
**) &mFvbEntry
);
271 Library constructor function entry.
273 @param ImageHandle The handle of image who call this libary.
274 @param SystemTable The point of System Table.
276 @retval EFI_SUCESS Sucess construct this library.
277 @retval Others Fail to contruct this libary.
282 IN EFI_HANDLE ImageHandle
,
283 IN EFI_SYSTEM_TABLE
*SystemTable
289 Status
= gBS
->AllocatePool (
290 EfiRuntimeServicesData
,
291 (UINTN
) sizeof (FVB_ENTRY
) * MAX_FVB_COUNT
,
295 if (EFI_ERROR (Status
)) {
299 ZeroMem (mFvbEntry
, sizeof (FVB_ENTRY
) * MAX_FVB_COUNT
);
301 EfiCreateProtocolNotifyEvent (
302 &gEfiFirmwareVolumeBlockProtocolGuid
,
304 FvbNotificationEvent
,
310 // Register SetVirtualAddressMap () notify function
312 Status
= gBS
->CreateEvent (
313 EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
,
315 FvbVirtualAddressChangeNotifyEvent
,
317 &mSetVirtualMapChangedEvent
319 ASSERT_EFI_ERROR (Status
);
325 // =============================================================================
326 // The following functions wrap Fvb protocol in the Runtime Lib functions.
327 // The Instance translates into Fvb instance. The Fvb order defined by HOBs and
328 // thus the sequence of FVB protocol addition define Instance.
330 // EfiFvbInitialize () must be called before any of the following functions
332 // =============================================================================
336 Reads specified number of bytes into a buffer from the specified block
338 @param Instance The FV instance to be read from.
339 @param Lba The logical block address to be read from
340 @param Offset Offset into the block at which to begin reading
341 @param NumBytes Pointer that on input contains the total size of
342 the buffer. On output, it contains the total number
344 @param Buffer Pointer to a caller allocated buffer that will be
345 used to hold the data read
347 @retval EFI_INVALID_PARAMETER Invalid parameter
348 @retval EFI_SUCESS Sucess to Read block
349 @retval Others Fail to read block
356 IN OUT UINTN
*NumBytes
,
360 if (Instance
>= mFvbCount
) {
361 return EFI_INVALID_PARAMETER
;
364 if (EfiAtRuntime() && !mFvbEntry
[Instance
].IsRuntimeAccess
) {
365 return EFI_INVALID_PARAMETER
;
368 return mFvbEntry
[Instance
].Fvb
->Read (mFvbEntry
[Instance
].Fvb
, Lba
, Offset
, NumBytes
, Buffer
);
372 Writes specified number of bytes from the input buffer to the block
374 @param Instance The FV instance to be written to
375 @param Lba The starting logical block index to write to
376 @param Offset Offset into the block at which to begin writing
377 @param NumBytes Pointer that on input contains the total size of
378 the buffer. On output, it contains the total number
379 of bytes actually written
380 @param Buffer Pointer to a caller allocated buffer that contains
381 the source for the write
383 @retval EFI_INVALID_PARAMETER Invalid parameter
384 @retval EFI_SUCESS Sucess to write block
385 @retval Others Fail to write block
392 IN OUT UINTN
*NumBytes
,
396 if (Instance
>= mFvbCount
) {
397 return EFI_INVALID_PARAMETER
;
400 if (EfiAtRuntime() && !mFvbEntry
[Instance
].IsRuntimeAccess
) {
401 return EFI_INVALID_PARAMETER
;
404 return mFvbEntry
[Instance
].Fvb
->Write (mFvbEntry
[Instance
].Fvb
, Lba
, Offset
, NumBytes
, Buffer
);
408 Erases and initializes a firmware volume block
410 @param Instance The FV instance to be erased
411 @param Lba The logical block index to be erased
413 @retval EFI_INVALID_PARAMETER Invalid parameter
414 @retval EFI_SUCESS Sucess to erase block
415 @retval Others Fail to erase block
423 if (Instance
>= mFvbCount
) {
424 return EFI_INVALID_PARAMETER
;
427 if (EfiAtRuntime() && !mFvbEntry
[Instance
].IsRuntimeAccess
) {
428 return EFI_INVALID_PARAMETER
;
431 return mFvbEntry
[Instance
].Fvb
->EraseBlocks (mFvbEntry
[Instance
].Fvb
, Lba
, -1);
435 Retrieves attributes, insures positive polarity of attribute bits, returns
436 resulting attributes in output parameter
438 @param Instance The FV instance whose attributes is going to be returned
439 @param Attributes Output buffer which contains attributes
441 @retval EFI_INVALID_PARAMETER Invalid parameter
442 @retval EFI_SUCESS Sucess to get Fv attribute
443 @retval Others Fail to get Fv attribute
446 EfiFvbGetVolumeAttributes (
448 OUT EFI_FVB_ATTRIBUTES
*Attributes
451 if (Instance
>= mFvbCount
) {
452 return EFI_INVALID_PARAMETER
;
455 if (EfiAtRuntime() && !mFvbEntry
[Instance
].IsRuntimeAccess
) {
456 return EFI_INVALID_PARAMETER
;
459 return mFvbEntry
[Instance
].Fvb
->GetAttributes (mFvbEntry
[Instance
].Fvb
, Attributes
);
463 Modifies the current settings of the firmware volume according to the
464 input parameter, and returns the new setting of the volume
466 @param Instance The FV instance whose attributes is going to be
468 @param Attributes On input, it is a pointer to EFI_FVB_ATTRIBUTES
469 containing the desired firmware volume settings.
470 On successful return, it contains the new settings
471 of the firmware volume
473 @retval EFI_INVALID_PARAMETER Invalid parameter
474 @retval EFI_SUCESS Sucess to set Fv attribute
475 @retval Others Fail to set Fv attribute
478 EfiFvbSetVolumeAttributes (
480 IN OUT EFI_FVB_ATTRIBUTES
*Attributes
483 if (Instance
>= mFvbCount
) {
484 return EFI_INVALID_PARAMETER
;
487 if (EfiAtRuntime() && !mFvbEntry
[Instance
].IsRuntimeAccess
) {
488 return EFI_INVALID_PARAMETER
;
491 return mFvbEntry
[Instance
].Fvb
->SetAttributes (mFvbEntry
[Instance
].Fvb
, Attributes
);
495 Retrieves the physical address of a memory mapped FV
497 @param Instance The FV instance whose base address is going to be
499 @param BaseAddress Pointer to a caller allocated EFI_PHYSICAL_ADDRESS
500 that on successful return, contains the base address
501 of the firmware volume.
503 @retval EFI_INVALID_PARAMETER Invalid parameter
504 @retval EFI_SUCESS Sucess to get physical address
505 @retval Others Fail to get physical address
508 EfiFvbGetPhysicalAddress (
510 OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
513 if (Instance
>= mFvbCount
) {
514 return EFI_INVALID_PARAMETER
;
517 if (EfiAtRuntime() && !mFvbEntry
[Instance
].IsRuntimeAccess
) {
518 return EFI_INVALID_PARAMETER
;
521 return mFvbEntry
[Instance
].Fvb
->GetPhysicalAddress (mFvbEntry
[Instance
].Fvb
, BaseAddress
);
525 Retrieve the size of a logical block
527 @param Instance The FV instance whose block size is going to be
529 @param Lba Indicates which block to return the size for.
530 @param BlockSize A pointer to a caller allocated UINTN in which
531 the size of the block is returned
532 @param NumOfBlocks a pointer to a caller allocated UINTN in which the
533 number of consecutive blocks starting with Lba is
534 returned. All blocks in this range have a size of
537 @retval EFI_INVALID_PARAMETER Invalid parameter
538 @retval EFI_SUCESS Sucess to get block size
539 @retval Others Fail to get block size
545 OUT UINTN
*BlockSize
,
546 OUT UINTN
*NumOfBlocks
549 if (Instance
>= mFvbCount
) {
550 return EFI_INVALID_PARAMETER
;
553 if (EfiAtRuntime() && !mFvbEntry
[Instance
].IsRuntimeAccess
) {
554 return EFI_INVALID_PARAMETER
;
557 return mFvbEntry
[Instance
].Fvb
->GetBlockSize (mFvbEntry
[Instance
].Fvb
, Lba
, BlockSize
, NumOfBlocks
);
561 Erases and initializes a specified range of a firmware volume
563 @param Instance The FV instance to be erased
564 @param StartLba The starting logical block index to be erased
565 @param OffsetStartLba Offset into the starting block at which to
567 @param LastLba The last logical block index to be erased
568 @param OffsetLastLba Offset into the last block at which to end erasing
570 @retval EFI_INVALID_PARAMETER Invalid parameter
571 @retval EFI_SUCESS Sucess to erase custom block range
572 @retval Others Fail to erase custom block range
575 EfiFvbEraseCustomBlockRange (
578 IN UINTN OffsetStartLba
,
580 IN UINTN OffsetLastLba
583 if (Instance
>= mFvbCount
) {
584 return EFI_INVALID_PARAMETER
;
587 if (EfiAtRuntime() && !mFvbEntry
[Instance
].IsRuntimeAccess
) {
588 return EFI_INVALID_PARAMETER
;
591 if (!(mFvbEntry
[Instance
].FvbExtension
)) {
592 return EFI_UNSUPPORTED
;
595 if (!(mFvbEntry
[Instance
].FvbExtension
->EraseFvbCustomBlock
)) {
596 return EFI_UNSUPPORTED
;
599 return mFvbEntry
[Instance
].FvbExtension
->EraseFvbCustomBlock (
600 mFvbEntry
[Instance
].FvbExtension
,