1 /*++ @file NorFlashFvbDxe.c
3 Copyright (c) 2011 - 2021, Arm Limited. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/PcdLib.h>
12 #include <Library/BaseLib.h>
13 #include <Library/UefiLib.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/MemoryAllocationLib.h>
17 #include <Guid/VariableFormat.h>
18 #include <Guid/SystemNvDataGuid.h>
19 #include <Guid/NvVarStoreFormatted.h>
23 extern UINTN mFlashNvStorageVariableBase
;
25 /// The Firmware Volume Block Protocol is the low-level interface
26 /// to a firmware volume. File-level access to a firmware volume
27 /// should not be done using the Firmware Volume Block Protocol.
28 /// Normal access to a firmware volume must use the Firmware
29 /// Volume Protocol. Typically, only the file system driver that
30 /// produces the Firmware Volume Protocol will bind to the
31 /// Firmware Volume Block Protocol.
35 Initialises the FV Header and Variable Store Header
36 to support variable operations.
38 @param[in] Ptr - Location to initialise the headers
42 InitializeFvAndVariableStoreHeaders (
43 IN NOR_FLASH_INSTANCE
*Instance
49 EFI_FIRMWARE_VOLUME_HEADER
*FirmwareVolumeHeader
;
50 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
51 UINT32 NvStorageFtwSpareSize
;
52 UINT32 NvStorageFtwWorkingSize
;
53 UINT32 NvStorageVariableSize
;
54 UINT64 NvStorageFtwSpareBase
;
55 UINT64 NvStorageFtwWorkingBase
;
56 UINT64 NvStorageVariableBase
;
58 HeadersLength
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
) + sizeof (EFI_FV_BLOCK_MAP_ENTRY
) + sizeof (VARIABLE_STORE_HEADER
);
59 Headers
= AllocateZeroPool (HeadersLength
);
61 NvStorageFtwWorkingSize
= PcdGet32 (PcdFlashNvStorageFtwWorkingSize
);
62 NvStorageFtwSpareSize
= PcdGet32 (PcdFlashNvStorageFtwSpareSize
);
63 NvStorageVariableSize
= PcdGet32 (PcdFlashNvStorageVariableSize
);
65 NvStorageFtwSpareBase
= (PcdGet64 (PcdFlashNvStorageFtwSpareBase64
) != 0) ?
66 PcdGet64 (PcdFlashNvStorageFtwSpareBase64
) : PcdGet32 (PcdFlashNvStorageFtwSpareBase
);
67 NvStorageFtwWorkingBase
= (PcdGet64 (PcdFlashNvStorageFtwWorkingBase64
) != 0) ?
68 PcdGet64 (PcdFlashNvStorageFtwWorkingBase64
) : PcdGet32 (PcdFlashNvStorageFtwWorkingBase
);
69 NvStorageVariableBase
= (PcdGet64 (PcdFlashNvStorageVariableBase64
) != 0) ?
70 PcdGet64 (PcdFlashNvStorageVariableBase64
) : PcdGet32 (PcdFlashNvStorageVariableBase
);
72 // FirmwareVolumeHeader->FvLength is declared to have the Variable area AND the FTW working area AND the FTW Spare contiguous.
73 if ((NvStorageVariableBase
+ NvStorageVariableSize
) != NvStorageFtwWorkingBase
) {
76 "%a: NvStorageFtwWorkingBase is not contiguous with NvStorageVariableBase region\n",
79 return EFI_INVALID_PARAMETER
;
82 if ((NvStorageFtwWorkingBase
+ NvStorageFtwWorkingSize
) != NvStorageFtwSpareBase
) {
85 "%a: NvStorageFtwSpareBase is not contiguous with NvStorageFtwWorkingBase region\n",
88 return EFI_INVALID_PARAMETER
;
91 // Check if the size of the area is at least one block size
92 if ((NvStorageVariableSize
<= 0) || (NvStorageVariableSize
/ Instance
->Media
.BlockSize
<= 0)) {
95 "%a: NvStorageVariableSize is 0x%x, should be atleast one block size\n",
99 return EFI_INVALID_PARAMETER
;
102 if ((NvStorageFtwWorkingSize
<= 0) || (NvStorageFtwWorkingSize
/ Instance
->Media
.BlockSize
<= 0)) {
105 "%a: NvStorageFtwWorkingSize is 0x%x, should be atleast one block size\n",
107 NvStorageFtwWorkingSize
109 return EFI_INVALID_PARAMETER
;
112 if ((NvStorageFtwSpareSize
<= 0) || (NvStorageFtwSpareSize
/ Instance
->Media
.BlockSize
<= 0)) {
115 "%a: NvStorageFtwSpareSize is 0x%x, should be atleast one block size\n",
117 NvStorageFtwSpareSize
119 return EFI_INVALID_PARAMETER
;
122 // Ensure the Variable area Base Addresses are aligned on a block size boundaries
123 if ((NvStorageVariableBase
% Instance
->Media
.BlockSize
!= 0) ||
124 (NvStorageFtwWorkingBase
% Instance
->Media
.BlockSize
!= 0) ||
125 (NvStorageFtwSpareBase
% Instance
->Media
.BlockSize
!= 0))
127 DEBUG ((DEBUG_ERROR
, "%a: NvStorage Base addresses must be aligned to block size boundaries", __FUNCTION__
));
128 return EFI_INVALID_PARAMETER
;
132 // EFI_FIRMWARE_VOLUME_HEADER
134 FirmwareVolumeHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)Headers
;
135 CopyGuid (&FirmwareVolumeHeader
->FileSystemGuid
, &gEfiSystemNvDataFvGuid
);
136 FirmwareVolumeHeader
->FvLength
=
137 PcdGet32 (PcdFlashNvStorageVariableSize
) +
138 PcdGet32 (PcdFlashNvStorageFtwWorkingSize
) +
139 PcdGet32 (PcdFlashNvStorageFtwSpareSize
);
140 FirmwareVolumeHeader
->Signature
= EFI_FVH_SIGNATURE
;
141 FirmwareVolumeHeader
->Attributes
= (EFI_FVB_ATTRIBUTES_2
)(
142 EFI_FVB2_READ_ENABLED_CAP
| // Reads may be enabled
143 EFI_FVB2_READ_STATUS
| // Reads are currently enabled
144 EFI_FVB2_STICKY_WRITE
| // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
145 EFI_FVB2_MEMORY_MAPPED
| // It is memory mapped
146 EFI_FVB2_ERASE_POLARITY
| // After erasure all bits take this value (i.e. '1')
147 EFI_FVB2_WRITE_STATUS
| // Writes are currently enabled
148 EFI_FVB2_WRITE_ENABLED_CAP
// Writes may be enabled
150 FirmwareVolumeHeader
->HeaderLength
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
) + sizeof (EFI_FV_BLOCK_MAP_ENTRY
);
151 FirmwareVolumeHeader
->Revision
= EFI_FVH_REVISION
;
152 FirmwareVolumeHeader
->BlockMap
[0].NumBlocks
= Instance
->Media
.LastBlock
+ 1;
153 FirmwareVolumeHeader
->BlockMap
[0].Length
= Instance
->Media
.BlockSize
;
154 FirmwareVolumeHeader
->BlockMap
[1].NumBlocks
= 0;
155 FirmwareVolumeHeader
->BlockMap
[1].Length
= 0;
156 FirmwareVolumeHeader
->Checksum
= CalculateCheckSum16 ((UINT16
*)FirmwareVolumeHeader
, FirmwareVolumeHeader
->HeaderLength
);
159 // VARIABLE_STORE_HEADER
161 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)((UINTN
)Headers
+ FirmwareVolumeHeader
->HeaderLength
);
162 CopyGuid (&VariableStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
);
163 VariableStoreHeader
->Size
= PcdGet32 (PcdFlashNvStorageVariableSize
) - FirmwareVolumeHeader
->HeaderLength
;
164 VariableStoreHeader
->Format
= VARIABLE_STORE_FORMATTED
;
165 VariableStoreHeader
->State
= VARIABLE_STORE_HEALTHY
;
167 // Install the combined super-header in the NorFlash
168 Status
= FvbWrite (&Instance
->FvbProtocol
, 0, 0, &HeadersLength
, Headers
);
175 Check the integrity of firmware volume header.
177 @param[in] FwVolHeader - A pointer to a firmware volume header
179 @retval EFI_SUCCESS - The firmware volume is consistent
180 @retval EFI_NOT_FOUND - The firmware volume has been corrupted.
185 IN NOR_FLASH_INSTANCE
*Instance
189 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
190 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
191 UINTN VariableStoreLength
;
194 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)Instance
->RegionBaseAddress
;
196 FvLength
= PcdGet32 (PcdFlashNvStorageVariableSize
) + PcdGet32 (PcdFlashNvStorageFtwWorkingSize
) +
197 PcdGet32 (PcdFlashNvStorageFtwSpareSize
);
200 // Verify the header revision, header signature, length
201 // Length of FvBlock cannot be 2**64-1
202 // HeaderLength cannot be an odd number
204 if ( (FwVolHeader
->Revision
!= EFI_FVH_REVISION
)
205 || (FwVolHeader
->Signature
!= EFI_FVH_SIGNATURE
)
206 || (FwVolHeader
->FvLength
!= FvLength
)
211 "%a: No Firmware Volume header present\n",
214 return EFI_NOT_FOUND
;
217 // Check the Firmware Volume Guid
218 if ( CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiSystemNvDataFvGuid
) == FALSE
) {
221 "%a: Firmware Volume Guid non-compatible\n",
224 return EFI_NOT_FOUND
;
227 // Verify the header checksum
228 Checksum
= CalculateSum16 ((UINT16
*)FwVolHeader
, FwVolHeader
->HeaderLength
);
232 "%a: FV checksum is invalid (Checksum:0x%X)\n",
236 return EFI_NOT_FOUND
;
239 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)((UINTN
)FwVolHeader
+ FwVolHeader
->HeaderLength
);
241 // Check the Variable Store Guid
242 if (!CompareGuid (&VariableStoreHeader
->Signature
, &gEfiVariableGuid
) &&
243 !CompareGuid (&VariableStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
))
247 "%a: Variable Store Guid non-compatible\n",
250 return EFI_NOT_FOUND
;
253 VariableStoreLength
= PcdGet32 (PcdFlashNvStorageVariableSize
) - FwVolHeader
->HeaderLength
;
254 if (VariableStoreHeader
->Size
!= VariableStoreLength
) {
257 "%a: Variable Store Length does not match\n",
260 return EFI_NOT_FOUND
;
267 The GetAttributes() function retrieves the attributes and
268 current settings of the block.
270 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
272 @param Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the attributes and
273 current settings are returned.
274 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
276 @retval EFI_SUCCESS The firmware volume attributes were returned.
282 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
283 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
286 EFI_FVB_ATTRIBUTES_2 FlashFvbAttributes
;
287 NOR_FLASH_INSTANCE
*Instance
;
289 Instance
= INSTANCE_FROM_FVB_THIS (This
);
291 FlashFvbAttributes
= (EFI_FVB_ATTRIBUTES_2
)(
293 EFI_FVB2_READ_ENABLED_CAP
| // Reads may be enabled
294 EFI_FVB2_READ_STATUS
| // Reads are currently enabled
295 EFI_FVB2_STICKY_WRITE
| // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
296 EFI_FVB2_MEMORY_MAPPED
| // It is memory mapped
297 EFI_FVB2_ERASE_POLARITY
// After erasure all bits take this value (i.e. '1')
301 // Check if it is write protected
302 if (Instance
->Media
.ReadOnly
!= TRUE
) {
303 FlashFvbAttributes
= FlashFvbAttributes
|
304 EFI_FVB2_WRITE_STATUS
| // Writes are currently enabled
305 EFI_FVB2_WRITE_ENABLED_CAP
; // Writes may be enabled
308 *Attributes
= FlashFvbAttributes
;
310 DEBUG ((DEBUG_BLKIO
, "FvbGetAttributes(0x%X)\n", *Attributes
));
316 The SetAttributes() function sets configurable firmware volume attributes
317 and returns the new settings of the firmware volume.
320 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
322 @param Attributes On input, Attributes is a pointer to EFI_FVB_ATTRIBUTES_2
323 that contains the desired firmware volume settings.
324 On successful return, it contains the new settings of
326 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
328 @retval EFI_SUCCESS The firmware volume attributes were returned.
330 @retval EFI_INVALID_PARAMETER The attributes requested are in conflict with the capabilities
331 as declared in the firmware volume header.
337 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
338 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
341 DEBUG ((DEBUG_BLKIO
, "FvbSetAttributes(0x%X) is not supported\n", *Attributes
));
342 return EFI_UNSUPPORTED
;
346 The GetPhysicalAddress() function retrieves the base address of
347 a memory-mapped firmware volume. This function should be called
348 only for memory-mapped firmware volumes.
350 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
352 @param Address Pointer to a caller-allocated
353 EFI_PHYSICAL_ADDRESS that, on successful
354 return from GetPhysicalAddress(), contains the
355 base address of the firmware volume.
357 @retval EFI_SUCCESS The firmware volume base address was returned.
359 @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.
364 FvbGetPhysicalAddress (
365 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
366 OUT EFI_PHYSICAL_ADDRESS
*Address
369 NOR_FLASH_INSTANCE
*Instance
;
371 Instance
= INSTANCE_FROM_FVB_THIS (This
);
373 DEBUG ((DEBUG_BLKIO
, "FvbGetPhysicalAddress(BaseAddress=0x%08x)\n", Instance
->RegionBaseAddress
));
375 ASSERT (Address
!= NULL
);
377 *Address
= mFlashNvStorageVariableBase
;
382 The GetBlockSize() function retrieves the size of the requested
383 block. It also returns the number of additional blocks with
384 the identical size. The GetBlockSize() function is used to
385 retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).
388 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
390 @param Lba Indicates the block for which to return the size.
392 @param BlockSize Pointer to a caller-allocated UINTN in which
393 the size of the block is returned.
395 @param NumberOfBlocks Pointer to a caller-allocated UINTN in
396 which the number of consecutive blocks,
397 starting with Lba, is returned. All
398 blocks in this range have a size of
402 @retval EFI_SUCCESS The firmware volume base address was returned.
404 @retval EFI_INVALID_PARAMETER The requested LBA is out of range.
410 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
412 OUT UINTN
*BlockSize
,
413 OUT UINTN
*NumberOfBlocks
417 NOR_FLASH_INSTANCE
*Instance
;
419 Instance
= INSTANCE_FROM_FVB_THIS (This
);
421 DEBUG ((DEBUG_BLKIO
, "FvbGetBlockSize(Lba=%ld, BlockSize=0x%x, LastBlock=%ld)\n", Lba
, Instance
->Media
.BlockSize
, Instance
->Media
.LastBlock
));
423 if (Lba
> Instance
->Media
.LastBlock
) {
424 DEBUG ((DEBUG_ERROR
, "FvbGetBlockSize: ERROR - Parameter LBA %ld is beyond the last Lba (%ld).\n", Lba
, Instance
->Media
.LastBlock
));
425 Status
= EFI_INVALID_PARAMETER
;
427 // This is easy because in this platform each NorFlash device has equal sized blocks.
428 *BlockSize
= (UINTN
)Instance
->Media
.BlockSize
;
429 *NumberOfBlocks
= (UINTN
)(Instance
->Media
.LastBlock
- Lba
+ 1);
431 DEBUG ((DEBUG_BLKIO
, "FvbGetBlockSize: *BlockSize=0x%x, *NumberOfBlocks=0x%x.\n", *BlockSize
, *NumberOfBlocks
));
433 Status
= EFI_SUCCESS
;
440 Reads the specified number of bytes into a buffer from the specified block.
442 The Read() function reads the requested number of bytes from the
443 requested block and stores them in the provided buffer.
444 Implementations should be mindful that the firmware volume
445 might be in the ReadDisabled state. If it is in this state,
446 the Read() function must return the status code
447 EFI_ACCESS_DENIED without modifying the contents of the
448 buffer. The Read() function must also prevent spanning block
449 boundaries. If a read is requested that would span a block
450 boundary, the read must read up to the boundary but not
451 beyond. The output parameter NumBytes must be set to correctly
452 indicate the number of bytes actually read. The caller must be
453 aware that a read may be partially completed.
455 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
457 @param Lba The starting logical block index from which to read.
459 @param Offset Offset into the block at which to begin reading.
461 @param NumBytes Pointer to a UINTN.
462 At entry, *NumBytes contains the total size of the buffer.
463 At exit, *NumBytes contains the total number of bytes read.
465 @param Buffer Pointer to a caller-allocated buffer that will be used
466 to hold the data that is read.
468 @retval EFI_SUCCESS The firmware volume was read successfully, and contents are
471 @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.
472 On output, NumBytes contains the total number of bytes
475 @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state.
477 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be read.
483 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
486 IN OUT UINTN
*NumBytes
,
490 EFI_STATUS TempStatus
;
492 NOR_FLASH_INSTANCE
*Instance
;
494 Instance
= INSTANCE_FROM_FVB_THIS (This
);
496 DEBUG ((DEBUG_BLKIO
, "FvbRead(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Instance
->StartLba
+ Lba
, Offset
, *NumBytes
, Buffer
));
498 TempStatus
= EFI_SUCCESS
;
500 // Cache the block size to avoid de-referencing pointers all the time
501 BlockSize
= Instance
->Media
.BlockSize
;
503 DEBUG ((DEBUG_BLKIO
, "FvbRead: Check if (Offset=0x%x + NumBytes=0x%x) <= BlockSize=0x%x\n", Offset
, *NumBytes
, BlockSize
));
505 // The read must not span block boundaries.
506 // We need to check each variable individually because adding two large values together overflows.
507 if ((Offset
>= BlockSize
) ||
508 (*NumBytes
> BlockSize
) ||
509 ((Offset
+ *NumBytes
) > BlockSize
))
511 DEBUG ((DEBUG_ERROR
, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset
, *NumBytes
, BlockSize
));
512 return EFI_BAD_BUFFER_SIZE
;
515 // We must have some bytes to read
516 if (*NumBytes
== 0) {
517 return EFI_BAD_BUFFER_SIZE
;
520 // Decide if we are doing full block reads or not.
521 if (*NumBytes
% BlockSize
!= 0) {
522 TempStatus
= NorFlashRead (Instance
, Instance
->StartLba
+ Lba
, Offset
, *NumBytes
, Buffer
);
523 if (EFI_ERROR (TempStatus
)) {
524 return EFI_DEVICE_ERROR
;
527 // Read NOR Flash data into shadow buffer
528 TempStatus
= NorFlashReadBlocks (Instance
, Instance
->StartLba
+ Lba
, BlockSize
, Buffer
);
529 if (EFI_ERROR (TempStatus
)) {
530 // Return one of the pre-approved error statuses
531 return EFI_DEVICE_ERROR
;
539 Writes the specified number of bytes from the input buffer to the block.
541 The Write() function writes the specified number of bytes from
542 the provided buffer to the specified block and offset. If the
543 firmware volume is sticky write, the caller must ensure that
544 all the bits of the specified range to write are in the
545 EFI_FVB_ERASE_POLARITY state before calling the Write()
546 function, or else the result will be unpredictable. This
547 unpredictability arises because, for a sticky-write firmware
548 volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY
549 state but cannot flip it back again. Before calling the
550 Write() function, it is recommended for the caller to first call
551 the EraseBlocks() function to erase the specified block to
552 write. A block erase cycle will transition bits from the
553 (NOT)EFI_FVB_ERASE_POLARITY state back to the
554 EFI_FVB_ERASE_POLARITY state. Implementations should be
555 mindful that the firmware volume might be in the WriteDisabled
556 state. If it is in this state, the Write() function must
557 return the status code EFI_ACCESS_DENIED without modifying the
558 contents of the firmware volume. The Write() function must
559 also prevent spanning block boundaries. If a write is
560 requested that spans a block boundary, the write must store up
561 to the boundary but not beyond. The output parameter NumBytes
562 must be set to correctly indicate the number of bytes actually
563 written. The caller must be aware that a write may be
564 partially completed. All writes, partial or otherwise, must be
565 fully flushed to the hardware before the Write() service
568 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
570 @param Lba The starting logical block index to write to.
572 @param Offset Offset into the block at which to begin writing.
574 @param NumBytes The pointer to a UINTN.
575 At entry, *NumBytes contains the total size of the buffer.
576 At exit, *NumBytes contains the total number of bytes actually written.
578 @param Buffer The pointer to a caller-allocated buffer that contains the source for the write.
580 @retval EFI_SUCCESS The firmware volume was written successfully.
582 @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
583 On output, NumBytes contains the total number of bytes
586 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
588 @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not be written.
595 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
598 IN OUT UINTN
*NumBytes
,
602 NOR_FLASH_INSTANCE
*Instance
;
604 Instance
= INSTANCE_FROM_FVB_THIS (This
);
606 return NorFlashWriteSingleBlock (Instance
, Instance
->StartLba
+ Lba
, Offset
, NumBytes
, Buffer
);
610 Erases and initialises a firmware volume block.
612 The EraseBlocks() function erases one or more blocks as denoted
613 by the variable argument list. The entire parameter list of
614 blocks must be verified before erasing any blocks. If a block is
615 requested that does not exist within the associated firmware
616 volume (it has a larger index than the last block of the
617 firmware volume), the EraseBlocks() function must return the
618 status code EFI_INVALID_PARAMETER without modifying the contents
619 of the firmware volume. Implementations should be mindful that
620 the firmware volume might be in the WriteDisabled state. If it
621 is in this state, the EraseBlocks() function must return the
622 status code EFI_ACCESS_DENIED without modifying the contents of
623 the firmware volume. All calls to EraseBlocks() must be fully
624 flushed to the hardware before the EraseBlocks() service
627 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
630 @param ... The variable argument list is a list of tuples.
631 Each tuple describes a range of LBAs to erase
632 and consists of the following:
633 - An EFI_LBA that indicates the starting LBA
634 - A UINTN that indicates the number of blocks to erase.
636 The list is terminated with an EFI_LBA_LIST_TERMINATOR.
637 For example, the following indicates that two ranges of blocks
638 (5-7 and 10-11) are to be erased:
639 EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);
641 @retval EFI_SUCCESS The erase request successfully completed.
643 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
645 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be written.
646 The firmware device may have been partially erased.
648 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the variable argument list do
649 not exist in the firmware volume.
655 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
661 UINTN BlockAddress
; // Physical address of Lba to erase
662 EFI_LBA StartingLba
; // Lba from which we start erasing
663 UINTN NumOfLba
; // Number of Lba blocks to erase
664 NOR_FLASH_INSTANCE
*Instance
;
666 Instance
= INSTANCE_FROM_FVB_THIS (This
);
668 DEBUG ((DEBUG_BLKIO
, "FvbEraseBlocks()\n"));
670 Status
= EFI_SUCCESS
;
672 // Detect WriteDisabled state
673 if (Instance
->Media
.ReadOnly
== TRUE
) {
674 // Firmware volume is in WriteDisabled state
675 DEBUG ((DEBUG_ERROR
, "FvbEraseBlocks: ERROR - Device is in WriteDisabled state.\n"));
676 return EFI_ACCESS_DENIED
;
679 // Before erasing, check the entire list of parameters to ensure all specified blocks are valid
681 VA_START (Args
, This
);
683 // Get the Lba from which we start erasing
684 StartingLba
= VA_ARG (Args
, EFI_LBA
);
686 // Have we reached the end of the list?
687 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
688 // Exit the while loop
692 // How many Lba blocks are we requested to erase?
693 NumOfLba
= VA_ARG (Args
, UINTN
);
695 // All blocks must be within range
698 "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%Lu - 1 ) > LastBlock=%ld.\n",
699 Instance
->StartLba
+ StartingLba
,
701 Instance
->Media
.LastBlock
703 if ((NumOfLba
== 0) || ((Instance
->StartLba
+ StartingLba
+ NumOfLba
- 1) > Instance
->Media
.LastBlock
)) {
705 DEBUG ((DEBUG_ERROR
, "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n"));
706 Status
= EFI_INVALID_PARAMETER
;
714 // To get here, all must be ok, so start erasing
716 VA_START (Args
, This
);
718 // Get the Lba from which we start erasing
719 StartingLba
= VA_ARG (Args
, EFI_LBA
);
721 // Have we reached the end of the list?
722 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
723 // Exit the while loop
727 // How many Lba blocks are we requested to erase?
728 NumOfLba
= VA_ARG (Args
, UINTN
);
730 // Go through each one and erase it
731 while (NumOfLba
> 0) {
732 // Get the physical address of Lba to erase
733 BlockAddress
= GET_NOR_BLOCK_ADDRESS (
734 Instance
->RegionBaseAddress
,
735 Instance
->StartLba
+ StartingLba
,
736 Instance
->Media
.BlockSize
740 DEBUG ((DEBUG_BLKIO
, "FvbEraseBlocks: Erasing Lba=%ld @ 0x%08x.\n", Instance
->StartLba
+ StartingLba
, BlockAddress
));
741 Status
= NorFlashUnlockAndEraseSingleBlock (Instance
, BlockAddress
);
742 if (EFI_ERROR (Status
)) {
744 Status
= EFI_DEVICE_ERROR
;
748 // Move to the next Lba
761 Fixup internal data so that EFI can be call in virtual mode.
762 Call the passed in Child Notify event and convert any pointers in
765 @param[in] Event The Event that is being processed
766 @param[in] Context Event Context
770 FvbVirtualNotifyEvent (
775 EfiConvertPointer (0x0, (VOID
**)&mFlashNvStorageVariableBase
);