2 Firmware Volume Block protocol. Consumes FV hobs and creates
3 appropriate block protocols.
5 Also consumes NT_NON_MM_FV envinronment variable and produces appropriate
6 block protocols fro them also... (this is TBD)
8 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
9 All rights reserved. This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 EFI_FW_VOL_BLOCK_DEVICE mFwVolBlock
= {
30 { (UINT8
)(sizeof (MEMMAP_DEVICE_PATH
)), (UINT8
)(sizeof (MEMMAP_DEVICE_PATH
) >> 8) }
33 (EFI_PHYSICAL_ADDRESS
)0,
34 (EFI_PHYSICAL_ADDRESS
)0,
38 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
39 { END_DEVICE_PATH_LENGTH
, 0 }
43 FwVolBlockGetAttributes
,
44 (EFI_FVB_SET_ATTRIBUTES
)FwVolBlockSetAttributes
,
45 FwVolBlockGetPhysicalAddress
,
46 FwVolBlockGetBlockSize
,
48 (EFI_FVB_WRITE
)FwVolBlockWriteBlock
,
49 (EFI_FVB_ERASE_BLOCKS
)FwVolBlockEraseBlock
,
63 Retrieves Volume attributes. No polarity translations are done.
65 @param This Calling context
66 @param Attributes output buffer which contains attributes
68 @retval EFI_SUCCESS The firmware volume attributes were returned.
73 FwVolBlockGetAttributes (
74 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
75 OUT EFI_FVB_ATTRIBUTES
*Attributes
78 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
80 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
83 // Since we are read only, it's safe to get attributes data from our in-memory copy.
85 *Attributes
= FvbDevice
->FvbAttributes
;
93 Modifies the current settings of the firmware volume according to the input parameter.
95 @param This Calling context
96 @param Attributes input buffer which contains attributes
98 @retval EFI_SUCCESS The firmware volume attributes were returned.
99 @retval EFI_INVALID_PARAMETER The attributes requested are in conflict with
100 the capabilities as declared in the firmware
102 @retval EFI_UNSUPPORTED Not supported.
107 FwVolBlockSetAttributes (
108 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
109 IN CONST EFI_FVB_ATTRIBUTES
*Attributes
112 return EFI_UNSUPPORTED
;
118 The EraseBlock() function erases one or more blocks as denoted by the
119 variable argument list. The entire parameter list of blocks must be verified
120 prior to erasing any blocks. If a block is requested that does not exist
121 within the associated firmware volume (it has a larger index than the last
122 block of the firmware volume), the EraseBlock() function must return
123 EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.
125 @param This Calling context
126 @param ... Starting LBA followed by Number of Lba to erase.
127 a -1 to terminate the list.
129 @retval EFI_SUCCESS The erase request was successfully completed.
130 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled
132 @retval EFI_DEVICE_ERROR The block device is not functioning correctly
133 and could not be written. The firmware device
134 may have been partially erased.
135 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the variable
137 @retval EFI_UNSUPPORTED Not supported.
142 FwVolBlockEraseBlock (
143 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
147 return EFI_UNSUPPORTED
;
153 Read the specified number of bytes from the block to the input buffer.
155 @param This Indicates the calling context.
156 @param Lba The starting logical block index to read.
157 @param Offset Offset into the block at which to begin reading.
158 @param NumBytes Pointer to a UINT32. At entry, *NumBytes
159 contains the total size of the buffer. At exit,
160 *NumBytes contains the total number of bytes
162 @param Buffer Pinter to a caller-allocated buffer that
163 contains the destine for the read.
165 @retval EFI_SUCCESS The firmware volume was read successfully.
166 @retval EFI_BAD_BUFFER_SIZE The read was attempted across an LBA boundary.
167 @retval EFI_ACCESS_DENIED Access denied.
168 @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not
174 FwVolBlockReadBlock (
175 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
176 IN CONST EFI_LBA Lba
,
177 IN CONST UINTN Offset
,
178 IN OUT UINTN
*NumBytes
,
182 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
183 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
186 UINTN NumOfBytesRead
;
189 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
192 // Check if This FW can be read
194 if ((FvbDevice
->FvbAttributes
& EFI_FVB2_READ_STATUS
) == 0) {
195 return EFI_ACCESS_DENIED
;
198 LbaIndex
= (UINTN
)Lba
;
199 if (LbaIndex
>= FvbDevice
->NumBlocks
) {
201 // Invalid Lba, read nothing.
204 return EFI_BAD_BUFFER_SIZE
;
207 if (Offset
> FvbDevice
->LbaCache
[LbaIndex
].Length
) {
209 // all exceed boundry, read nothing.
212 return EFI_BAD_BUFFER_SIZE
;
215 NumOfBytesRead
= *NumBytes
;
216 if (Offset
+ NumOfBytesRead
> FvbDevice
->LbaCache
[LbaIndex
].Length
) {
218 // partial exceed boundry, read data from current postion to end.
220 NumOfBytesRead
= FvbDevice
->LbaCache
[LbaIndex
].Length
- Offset
;
223 LbaStart
= FvbDevice
->LbaCache
[LbaIndex
].Base
;
224 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)((UINTN
)FvbDevice
->BaseAddress
);
225 LbaOffset
= (UINT8
*)FwVolHeader
+ LbaStart
+ Offset
;
228 // Perform read operation
230 CopyMem (Buffer
, LbaOffset
, NumOfBytesRead
);
232 if (NumOfBytesRead
== *NumBytes
) {
236 *NumBytes
= NumOfBytesRead
;
237 return EFI_BAD_BUFFER_SIZE
;
243 Writes the specified number of bytes from the input buffer to the block.
245 @param This Indicates the calling context.
246 @param Lba The starting logical block index to write to.
247 @param Offset Offset into the block at which to begin writing.
248 @param NumBytes Pointer to a UINT32. At entry, *NumBytes
249 contains the total size of the buffer. At exit,
250 *NumBytes contains the total number of bytes
252 @param Buffer Pinter to a caller-allocated buffer that
253 contains the source for the write.
255 @retval EFI_SUCCESS The firmware volume was written successfully.
256 @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
257 On output, NumBytes contains the total number of
258 bytes actually written.
259 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled
261 @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not
263 @retval EFI_UNSUPPORTED Not supported.
268 FwVolBlockWriteBlock (
269 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
272 IN OUT UINTN
*NumBytes
,
276 return EFI_UNSUPPORTED
;
282 Get Fvb's base address.
284 @param This Indicates the calling context.
285 @param Address Fvb device base address.
287 @retval EFI_SUCCESS Successfully got Fvb's base address.
288 @retval EFI_UNSUPPORTED Not supported.
293 FwVolBlockGetPhysicalAddress (
294 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
295 OUT EFI_PHYSICAL_ADDRESS
*Address
298 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
300 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
302 if (FvbDevice
->FvbAttributes
& EFI_FVB2_MEMORY_MAPPED
) {
303 *Address
= FvbDevice
->BaseAddress
;
307 return EFI_UNSUPPORTED
;
313 Retrieves the size in bytes of a specific block within a firmware volume.
315 @param This Indicates the calling context.
316 @param Lba Indicates the block for which to return the
318 @param BlockSize Pointer to a caller-allocated UINTN in which the
319 size of the block is returned.
320 @param NumberOfBlocks Pointer to a caller-allocated UINTN in which the
321 number of consecutive blocks starting with Lba
322 is returned. All blocks in this range have a
325 @retval EFI_SUCCESS The firmware volume base address is returned.
326 @retval EFI_INVALID_PARAMETER The requested LBA is out of range.
331 FwVolBlockGetBlockSize (
332 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
333 IN CONST EFI_LBA Lba
,
334 IN OUT UINTN
*BlockSize
,
335 IN OUT UINTN
*NumberOfBlocks
339 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
340 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
341 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
343 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
346 // Do parameter checking
348 if (Lba
>= FvbDevice
->NumBlocks
) {
349 return EFI_INVALID_PARAMETER
;
352 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)((UINTN
)FvbDevice
->BaseAddress
);
354 PtrBlockMapEntry
= FwVolHeader
->BlockMap
;
357 // Search the block map for the given block
360 while ((PtrBlockMapEntry
->NumBlocks
!= 0) || (PtrBlockMapEntry
->Length
!=0 )) {
361 TotalBlocks
+= PtrBlockMapEntry
->NumBlocks
;
362 if (Lba
< TotalBlocks
) {
372 *BlockSize
= PtrBlockMapEntry
->Length
;
373 *NumberOfBlocks
= TotalBlocks
- (UINTN
)Lba
;
381 This routine produces a firmware volume block protocol on a given
384 @param BaseAddress base address of the firmware volume image
385 @param Length length of the firmware volume image
386 @param ParentHandle handle of parent firmware volume, if this image
387 came from an FV image file in another firmware
388 volume (ala capsules)
389 @param FvProtocol Firmware volume block protocol produced.
391 @retval EFI_VOLUME_CORRUPTED Volume corrupted.
392 @retval EFI_OUT_OF_RESOURCES No enough buffer to be allocated.
393 @retval EFI_SUCCESS Successfully produced a FVB protocol on given
398 ProduceFVBProtocolOnBuffer (
399 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
401 IN EFI_HANDLE ParentHandle
,
402 OUT EFI_HANDLE
*FvProtocol OPTIONAL
406 EFI_FW_VOL_BLOCK_DEVICE
*FvbDev
;
407 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
412 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
415 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)BaseAddress
;
417 // Validate FV Header, if not as expected, return
419 if (FwVolHeader
->Signature
!= EFI_FVH_SIGNATURE
) {
420 return EFI_VOLUME_CORRUPTED
;
423 // Get FvHeader alignment
425 FvAlignment
= 1 << ((FwVolHeader
->Attributes
& EFI_FVB2_ALIGNMENT
) >> 16);
426 if (FvAlignment
< 8) {
429 if ((UINTN
)BaseAddress
% FvAlignment
!= 0) {
431 // FvImage buffer is not at its required alignment.
433 return EFI_VOLUME_CORRUPTED
;
436 // Allocate EFI_FW_VOL_BLOCK_DEVICE
438 FvbDev
= CoreAllocateCopyPool (sizeof (EFI_FW_VOL_BLOCK_DEVICE
), &mFwVolBlock
);
439 if (FvbDev
== NULL
) {
440 return EFI_OUT_OF_RESOURCES
;
443 FvbDev
->BaseAddress
= BaseAddress
;
444 FvbDev
->FvbAttributes
= FwVolHeader
->Attributes
;
445 FvbDev
->FwVolBlockInstance
.ParentHandle
= ParentHandle
;
448 // Init the block caching fields of the device
449 // First, count the number of blocks
451 FvbDev
->NumBlocks
= 0;
452 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
;
453 PtrBlockMapEntry
->NumBlocks
!= 0;
454 PtrBlockMapEntry
++) {
455 FvbDev
->NumBlocks
+= PtrBlockMapEntry
->NumBlocks
;
458 // Second, allocate the cache
460 FvbDev
->LbaCache
= CoreAllocateBootServicesPool (FvbDev
->NumBlocks
* sizeof (LBA_CACHE
));
461 if (FvbDev
->LbaCache
== NULL
) {
462 CoreFreePool (FvbDev
);
463 return EFI_OUT_OF_RESOURCES
;
466 // Last, fill in the cache with the linear address of the blocks
470 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
;
471 PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
472 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
473 FvbDev
->LbaCache
[BlockIndex
].Base
= LinearOffset
;
474 FvbDev
->LbaCache
[BlockIndex
].Length
= PtrBlockMapEntry
->Length
;
475 LinearOffset
+= PtrBlockMapEntry
->Length
;
481 // Set up the devicepath
483 FvbDev
->DevicePath
.MemMapDevPath
.StartingAddress
= BaseAddress
;
484 FvbDev
->DevicePath
.MemMapDevPath
.EndingAddress
= BaseAddress
+ FwVolHeader
->FvLength
- 1;
488 // Attach FvVolBlock Protocol to new handle
490 Status
= CoreInstallMultipleProtocolInterfaces (
492 &gEfiFirmwareVolumeBlockProtocolGuid
, &FvbDev
->FwVolBlockInstance
,
493 &gEfiDevicePathProtocolGuid
, &FvbDev
->DevicePath
,
494 &gEfiFirmwareVolumeDispatchProtocolGuid
, NULL
,
499 // If they want the handle back, set it.
501 if (FvProtocol
!= NULL
) {
502 *FvProtocol
= FvbDev
->Handle
;
511 This routine is the driver initialization entry point. It initializes the
512 libraries, consumes FV hobs and NT_NON_MM_FV environment variable and
513 produces instances of FW_VOL_BLOCK_PROTOCOL as appropriate.
515 @param ImageHandle The image handle.
516 @param SystemTable The system table.
518 @retval EFI_SUCCESS Successfully initialized firmware volume block
524 FwVolBlockDriverInit (
525 IN EFI_HANDLE ImageHandle
,
526 IN EFI_SYSTEM_TABLE
*SystemTable
529 EFI_PEI_HOB_POINTERS FvHob
;
531 // Core Needs Firmware Volumes to function
533 FvHob
.Raw
= GetHobList ();
534 while ((FvHob
.Raw
= GetNextHob (EFI_HOB_TYPE_FV
, FvHob
.Raw
)) != NULL
) {
536 // Produce an FVB protocol for it
538 ProduceFVBProtocolOnBuffer (FvHob
.FirmwareVolume
->BaseAddress
, FvHob
.FirmwareVolume
->Length
, NULL
, NULL
);
539 FvHob
.Raw
= GET_NEXT_HOB (FvHob
);
547 This DXE service routine is used to process a firmware volume. In
548 particular, it can be called by BDS to process a single firmware
549 volume found in a capsule.
551 @param FvHeader pointer to a firmware volume header
552 @param Size the size of the buffer pointed to by FvHeader
553 @param FVProtocolHandle the handle on which a firmware volume protocol
554 was produced for the firmware volume passed in.
556 @retval EFI_OUT_OF_RESOURCES if an FVB could not be produced due to lack of
558 @retval EFI_VOLUME_CORRUPTED if the volume was corrupted
559 @retval EFI_SUCCESS a firmware volume protocol was produced for the
564 CoreProcessFirmwareVolume (
567 OUT EFI_HANDLE
*FVProtocolHandle
573 *FVProtocolHandle
= NULL
;
574 Status
= ProduceFVBProtocolOnBuffer (
575 (EFI_PHYSICAL_ADDRESS
) (UINTN
) FvHeader
,
581 // Since in our implementation we use register-protocol-notify to put a
582 // FV protocol on the FVB protocol handle, we can't directly verify that
583 // the FV protocol was produced. Therefore here we will check the handle
584 // and make sure an FV protocol is on it. This indicates that all went
585 // well. Otherwise we have to assume that the volume was corrupted
588 if (!EFI_ERROR(Status
)) {
590 Status
= CoreHandleProtocol (*FVProtocolHandle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&Ptr
);
591 if (EFI_ERROR(Status
) || (Ptr
== NULL
)) {
592 return EFI_VOLUME_CORRUPTED
;