2 Implementations for Firmware Volume Block protocol.
4 It consumes FV HOBs and creates read-lonly Firmare Volume Block protocol
5 instances for each of them.
7 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include "FwVolBlock.h"
22 EFI_FW_VOL_BLOCK_DEVICE mFwVolBlock
= {
31 (UINT8
)(sizeof (MEMMAP_DEVICE_PATH
)),
32 (UINT8
)(sizeof (MEMMAP_DEVICE_PATH
) >> 8)
36 (EFI_PHYSICAL_ADDRESS
) 0,
37 (EFI_PHYSICAL_ADDRESS
) 0,
41 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
43 END_DEVICE_PATH_LENGTH
,
49 FwVolBlockGetAttributes
,
50 (EFI_FVB_SET_ATTRIBUTES
)FwVolBlockSetAttributes
,
51 FwVolBlockGetPhysicalAddress
,
52 FwVolBlockGetBlockSize
,
54 (EFI_FVB_WRITE
)FwVolBlockWriteBlock
,
55 (EFI_FVB_ERASE_BLOCKS
)FwVolBlockEraseBlock
,
67 Retrieves Volume attributes. No polarity translations are done.
69 @param This Calling context
70 @param Attributes output buffer which contains attributes
72 @retval EFI_SUCCESS The firmware volume attributes were returned.
77 FwVolBlockGetAttributes (
78 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
79 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
82 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
84 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
87 // Since we are read only, it's safe to get attributes data from our in-memory copy.
89 *Attributes
= FvbDevice
->FvbAttributes
& ~EFI_FVB2_WRITE_STATUS
;
97 Modifies the current settings of the firmware volume according to the input parameter.
99 @param This Calling context
100 @param Attributes input buffer which contains attributes
102 @retval EFI_SUCCESS The firmware volume attributes were returned.
103 @retval EFI_INVALID_PARAMETER The attributes requested are in conflict with
104 the capabilities as declared in the firmware
106 @retval EFI_UNSUPPORTED Not supported.
111 FwVolBlockSetAttributes (
112 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
113 IN CONST EFI_FVB_ATTRIBUTES_2
*Attributes
116 return EFI_UNSUPPORTED
;
122 The EraseBlock() function erases one or more blocks as denoted by the
123 variable argument list. The entire parameter list of blocks must be verified
124 prior to erasing any blocks. If a block is requested that does not exist
125 within the associated firmware volume (it has a larger index than the last
126 block of the firmware volume), the EraseBlock() function must return
127 EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.
129 @param This Calling context
130 @param ... Starting LBA followed by Number of Lba to erase.
131 a -1 to terminate the list.
133 @retval EFI_SUCCESS The erase request was successfully completed.
134 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled
136 @retval EFI_DEVICE_ERROR The block device is not functioning correctly
137 and could not be written. The firmware device
138 may have been partially erased.
139 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the variable
141 @retval EFI_UNSUPPORTED Not supported.
146 FwVolBlockEraseBlock (
147 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
151 return EFI_UNSUPPORTED
;
157 Read the specified number of bytes from the block to the input buffer.
159 @param This Indicates the calling context.
160 @param Lba The starting logical block index to read.
161 @param Offset Offset into the block at which to begin reading.
162 @param NumBytes Pointer to a UINT32. At entry, *NumBytes
163 contains the total size of the buffer. At exit,
164 *NumBytes contains the total number of bytes
166 @param Buffer Pinter to a caller-allocated buffer that
167 contains the destine for the read.
169 @retval EFI_SUCCESS The firmware volume was read successfully.
170 @retval EFI_BAD_BUFFER_SIZE The read was attempted across an LBA boundary.
171 @retval EFI_ACCESS_DENIED Access denied.
172 @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not
178 FwVolBlockReadBlock (
179 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
180 IN CONST EFI_LBA Lba
,
181 IN CONST UINTN Offset
,
182 IN OUT UINTN
*NumBytes
,
186 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
187 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
190 UINTN NumOfBytesRead
;
193 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
196 // Check if This FW can be read
198 if ((FvbDevice
->FvbAttributes
& EFI_FVB2_READ_STATUS
) == 0) {
199 return EFI_ACCESS_DENIED
;
202 LbaIndex
= (UINTN
) Lba
;
203 if (LbaIndex
>= FvbDevice
->NumBlocks
) {
205 // Invalid Lba, read nothing.
208 return EFI_BAD_BUFFER_SIZE
;
211 if (Offset
> FvbDevice
->LbaCache
[LbaIndex
].Length
) {
213 // all exceed boundry, read nothing.
216 return EFI_BAD_BUFFER_SIZE
;
219 NumOfBytesRead
= *NumBytes
;
220 if (Offset
+ NumOfBytesRead
> FvbDevice
->LbaCache
[LbaIndex
].Length
) {
222 // partial exceed boundry, read data from current postion to end.
224 NumOfBytesRead
= FvbDevice
->LbaCache
[LbaIndex
].Length
- Offset
;
227 LbaStart
= FvbDevice
->LbaCache
[LbaIndex
].Base
;
228 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)((UINTN
) FvbDevice
->BaseAddress
);
229 LbaOffset
= (UINT8
*) FwVolHeader
+ LbaStart
+ Offset
;
232 // Perform read operation
234 CopyMem (Buffer
, LbaOffset
, NumOfBytesRead
);
236 if (NumOfBytesRead
== *NumBytes
) {
240 *NumBytes
= NumOfBytesRead
;
241 return EFI_BAD_BUFFER_SIZE
;
247 Writes the specified number of bytes from the input buffer to the block.
249 @param This Indicates the calling context.
250 @param Lba The starting logical block index to write to.
251 @param Offset Offset into the block at which to begin writing.
252 @param NumBytes Pointer to a UINT32. At entry, *NumBytes
253 contains the total size of the buffer. At exit,
254 *NumBytes contains the total number of bytes
256 @param Buffer Pinter to a caller-allocated buffer that
257 contains the source for the write.
259 @retval EFI_SUCCESS The firmware volume was written successfully.
260 @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
261 On output, NumBytes contains the total number of
262 bytes actually written.
263 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled
265 @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not
267 @retval EFI_UNSUPPORTED Not supported.
272 FwVolBlockWriteBlock (
273 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
276 IN OUT UINTN
*NumBytes
,
280 return EFI_UNSUPPORTED
;
286 Get Fvb's base address.
288 @param This Indicates the calling context.
289 @param Address Fvb device base address.
291 @retval EFI_SUCCESS Successfully got Fvb's base address.
292 @retval EFI_UNSUPPORTED Not supported.
297 FwVolBlockGetPhysicalAddress (
298 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
299 OUT EFI_PHYSICAL_ADDRESS
*Address
302 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
304 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
306 if ((FvbDevice
->FvbAttributes
& EFI_FVB2_MEMORY_MAPPED
) != 0) {
307 *Address
= FvbDevice
->BaseAddress
;
311 return EFI_UNSUPPORTED
;
317 Retrieves the size in bytes of a specific block within a firmware volume.
319 @param This Indicates the calling context.
320 @param Lba Indicates the block for which to return the
322 @param BlockSize Pointer to a caller-allocated UINTN in which the
323 size of the block is returned.
324 @param NumberOfBlocks Pointer to a caller-allocated UINTN in which the
325 number of consecutive blocks starting with Lba
326 is returned. All blocks in this range have a
329 @retval EFI_SUCCESS The firmware volume base address is returned.
330 @retval EFI_INVALID_PARAMETER The requested LBA is out of range.
335 FwVolBlockGetBlockSize (
336 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
337 IN CONST EFI_LBA Lba
,
338 IN OUT UINTN
*BlockSize
,
339 IN OUT UINTN
*NumberOfBlocks
343 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
344 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
345 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
347 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
350 // Do parameter checking
352 if (Lba
>= FvbDevice
->NumBlocks
) {
353 return EFI_INVALID_PARAMETER
;
356 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)((UINTN
)FvbDevice
->BaseAddress
);
358 PtrBlockMapEntry
= FwVolHeader
->BlockMap
;
361 // Search the block map for the given block
364 while ((PtrBlockMapEntry
->NumBlocks
!= 0) || (PtrBlockMapEntry
->Length
!=0 )) {
365 TotalBlocks
+= PtrBlockMapEntry
->NumBlocks
;
366 if (Lba
< TotalBlocks
) {
376 *BlockSize
= PtrBlockMapEntry
->Length
;
377 *NumberOfBlocks
= TotalBlocks
- (UINTN
)Lba
;
385 This routine produces a firmware volume block protocol on a given
388 @param BaseAddress base address of the firmware volume image
389 @param Length length of the firmware volume image
390 @param ParentHandle handle of parent firmware volume, if this image
391 came from an FV image file in another firmware
392 volume (ala capsules)
393 @param FvProtocol Firmware volume block protocol produced.
395 @retval EFI_VOLUME_CORRUPTED Volume corrupted.
396 @retval EFI_OUT_OF_RESOURCES No enough buffer to be allocated.
397 @retval EFI_SUCCESS Successfully produced a FVB protocol on given
402 ProduceFVBProtocolOnBuffer (
403 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
405 IN EFI_HANDLE ParentHandle
,
406 OUT EFI_HANDLE
*FvProtocol OPTIONAL
410 EFI_FW_VOL_BLOCK_DEVICE
*FvbDev
;
411 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
416 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
419 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
) BaseAddress
;
421 // Validate FV Header, if not as expected, return
423 if (FwVolHeader
->Signature
!= EFI_FVH_SIGNATURE
) {
424 return EFI_VOLUME_CORRUPTED
;
427 // Get FvHeader alignment
429 FvAlignment
= 1 << ((FwVolHeader
->Attributes
& EFI_FVB2_ALIGNMENT
) >> 16);
430 if (FvAlignment
< 8) {
433 if ((UINTN
)BaseAddress
% FvAlignment
!= 0) {
435 // FvImage buffer is not at its required alignment.
437 return EFI_VOLUME_CORRUPTED
;
440 // Allocate EFI_FW_VOL_BLOCK_DEVICE
442 FvbDev
= AllocateCopyPool (sizeof (EFI_FW_VOL_BLOCK_DEVICE
), &mFwVolBlock
);
443 if (FvbDev
== NULL
) {
444 return EFI_OUT_OF_RESOURCES
;
447 FvbDev
->BaseAddress
= BaseAddress
;
448 FvbDev
->FvbAttributes
= FwVolHeader
->Attributes
;
449 FvbDev
->FwVolBlockInstance
.ParentHandle
= ParentHandle
;
452 // Init the block caching fields of the device
453 // First, count the number of blocks
455 FvbDev
->NumBlocks
= 0;
456 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
;
457 PtrBlockMapEntry
->NumBlocks
!= 0;
458 PtrBlockMapEntry
++) {
459 FvbDev
->NumBlocks
+= PtrBlockMapEntry
->NumBlocks
;
462 // Second, allocate the cache
464 FvbDev
->LbaCache
= AllocatePool (FvbDev
->NumBlocks
* sizeof (LBA_CACHE
));
465 if (FvbDev
->LbaCache
== NULL
) {
466 CoreFreePool (FvbDev
);
467 return EFI_OUT_OF_RESOURCES
;
470 // Last, fill in the cache with the linear address of the blocks
474 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
;
475 PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
476 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
477 FvbDev
->LbaCache
[BlockIndex
].Base
= LinearOffset
;
478 FvbDev
->LbaCache
[BlockIndex
].Length
= PtrBlockMapEntry
->Length
;
479 LinearOffset
+= PtrBlockMapEntry
->Length
;
485 // Set up the devicepath
487 FvbDev
->DevicePath
.MemMapDevPath
.StartingAddress
= BaseAddress
;
488 FvbDev
->DevicePath
.MemMapDevPath
.EndingAddress
= BaseAddress
+ FwVolHeader
->FvLength
- 1;
492 // Attach FvVolBlock Protocol to new handle
494 Status
= CoreInstallMultipleProtocolInterfaces (
496 &gEfiFirmwareVolumeBlockProtocolGuid
, &FvbDev
->FwVolBlockInstance
,
497 &gEfiDevicePathProtocolGuid
, &FvbDev
->DevicePath
,
502 // If they want the handle back, set it.
504 if (FvProtocol
!= NULL
) {
505 *FvProtocol
= FvbDev
->Handle
;
514 This routine consumes FV hobs and produces instances of FW_VOL_BLOCK_PROTOCOL as appropriate.
516 @param ImageHandle The image handle.
517 @param SystemTable The system table.
519 @retval EFI_SUCCESS Successfully initialized firmware volume block
525 FwVolBlockDriverInit (
526 IN EFI_HANDLE ImageHandle
,
527 IN EFI_SYSTEM_TABLE
*SystemTable
530 EFI_PEI_HOB_POINTERS FvHob
;
533 // Core Needs Firmware Volumes to function
535 FvHob
.Raw
= GetHobList ();
536 while ((FvHob
.Raw
= GetNextHob (EFI_HOB_TYPE_FV
, FvHob
.Raw
)) != NULL
) {
538 // Produce an FVB protocol for it
540 ProduceFVBProtocolOnBuffer (FvHob
.FirmwareVolume
->BaseAddress
, FvHob
.FirmwareVolume
->Length
, NULL
, NULL
);
541 FvHob
.Raw
= GET_NEXT_HOB (FvHob
);
550 This DXE service routine is used to process a firmware volume. In
551 particular, it can be called by BDS to process a single firmware
552 volume found in a capsule.
554 @param FvHeader pointer to a firmware volume header
555 @param Size the size of the buffer pointed to by FvHeader
556 @param FVProtocolHandle the handle on which a firmware volume protocol
557 was produced for the firmware volume passed in.
559 @retval EFI_OUT_OF_RESOURCES if an FVB could not be produced due to lack of
561 @retval EFI_VOLUME_CORRUPTED if the volume was corrupted
562 @retval EFI_SUCCESS a firmware volume protocol was produced for the
568 CoreProcessFirmwareVolume (
571 OUT EFI_HANDLE
*FVProtocolHandle
577 *FVProtocolHandle
= NULL
;
578 Status
= ProduceFVBProtocolOnBuffer (
579 (EFI_PHYSICAL_ADDRESS
) (UINTN
) FvHeader
,
585 // Since in our implementation we use register-protocol-notify to put a
586 // FV protocol on the FVB protocol handle, we can't directly verify that
587 // the FV protocol was produced. Therefore here we will check the handle
588 // and make sure an FV protocol is on it. This indicates that all went
589 // well. Otherwise we have to assume that the volume was corrupted
592 if (!EFI_ERROR(Status
)) {
594 Status
= CoreHandleProtocol (*FVProtocolHandle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**) &Ptr
);
595 if (EFI_ERROR(Status
) || (Ptr
== NULL
)) {
596 return EFI_VOLUME_CORRUPTED
;