1 /*++ @file NorFlashFvbDxe.c
3 Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/PcdLib.h>
18 #include <Library/BaseLib.h>
19 #include <Library/HobLib.h>
20 #include <Library/UefiLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/DxeServicesTableLib.h>
24 #include <Library/UefiBootServicesTableLib.h>
26 #include <Guid/VariableFormat.h>
27 #include <Guid/SystemNvDataGuid.h>
29 #include "NorFlashDxe.h"
31 STATIC EFI_EVENT mFvbVirtualAddrChangeEvent
;
32 STATIC UINTN mFlashNvStorageVariableBase
;
35 /// The Firmware Volume Block Protocol is the low-level interface
36 /// to a firmware volume. File-level access to a firmware volume
37 /// should not be done using the Firmware Volume Block Protocol.
38 /// Normal access to a firmware volume must use the Firmware
39 /// Volume Protocol. Typically, only the file system driver that
40 /// produces the Firmware Volume Protocol will bind to the
41 /// Firmware Volume Block Protocol.
45 Initialises the FV Header and Variable Store Header
46 to support variable operations.
48 @param[in] Ptr - Location to initialise the headers
52 InitializeFvAndVariableStoreHeaders (
53 IN NOR_FLASH_INSTANCE
*Instance
59 EFI_FIRMWARE_VOLUME_HEADER
*FirmwareVolumeHeader
;
60 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
62 if (!Instance
->Initialized
&& Instance
->Initialize
) {
63 Instance
->Initialize (Instance
);
66 HeadersLength
= sizeof(EFI_FIRMWARE_VOLUME_HEADER
) + sizeof(EFI_FV_BLOCK_MAP_ENTRY
) + sizeof(VARIABLE_STORE_HEADER
);
67 Headers
= AllocateZeroPool(HeadersLength
);
69 // FirmwareVolumeHeader->FvLength is declared to have the Variable area AND the FTW working area AND the FTW Spare contiguous.
70 ASSERT(PcdGet32(PcdFlashNvStorageVariableBase
) + PcdGet32(PcdFlashNvStorageVariableSize
) == PcdGet32(PcdFlashNvStorageFtwWorkingBase
));
71 ASSERT(PcdGet32(PcdFlashNvStorageFtwWorkingBase
) + PcdGet32(PcdFlashNvStorageFtwWorkingSize
) == PcdGet32(PcdFlashNvStorageFtwSpareBase
));
73 // Check if the size of the area is at least one block size
74 ASSERT((PcdGet32(PcdFlashNvStorageVariableSize
) > 0) && (PcdGet32(PcdFlashNvStorageVariableSize
) / Instance
->Media
.BlockSize
> 0));
75 ASSERT((PcdGet32(PcdFlashNvStorageFtwWorkingSize
) > 0) && (PcdGet32(PcdFlashNvStorageFtwWorkingSize
) / Instance
->Media
.BlockSize
> 0));
76 ASSERT((PcdGet32(PcdFlashNvStorageFtwSpareSize
) > 0) && (PcdGet32(PcdFlashNvStorageFtwSpareSize
) / Instance
->Media
.BlockSize
> 0));
78 // Ensure the Variable area Base Addresses are aligned on a block size boundaries
79 ASSERT(PcdGet32(PcdFlashNvStorageVariableBase
) % Instance
->Media
.BlockSize
== 0);
80 ASSERT(PcdGet32(PcdFlashNvStorageFtwWorkingBase
) % Instance
->Media
.BlockSize
== 0);
81 ASSERT(PcdGet32(PcdFlashNvStorageFtwSpareBase
) % Instance
->Media
.BlockSize
== 0);
84 // EFI_FIRMWARE_VOLUME_HEADER
86 FirmwareVolumeHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)Headers
;
87 CopyGuid (&FirmwareVolumeHeader
->FileSystemGuid
, &gEfiSystemNvDataFvGuid
);
88 FirmwareVolumeHeader
->FvLength
=
89 PcdGet32(PcdFlashNvStorageVariableSize
) +
90 PcdGet32(PcdFlashNvStorageFtwWorkingSize
) +
91 PcdGet32(PcdFlashNvStorageFtwSpareSize
);
92 FirmwareVolumeHeader
->Signature
= EFI_FVH_SIGNATURE
;
93 FirmwareVolumeHeader
->Attributes
= (EFI_FVB_ATTRIBUTES_2
) (
94 EFI_FVB2_READ_ENABLED_CAP
| // Reads may be enabled
95 EFI_FVB2_READ_STATUS
| // Reads are currently enabled
96 EFI_FVB2_STICKY_WRITE
| // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
97 EFI_FVB2_MEMORY_MAPPED
| // It is memory mapped
98 EFI_FVB2_ERASE_POLARITY
| // After erasure all bits take this value (i.e. '1')
99 EFI_FVB2_WRITE_STATUS
| // Writes are currently enabled
100 EFI_FVB2_WRITE_ENABLED_CAP
// Writes may be enabled
102 FirmwareVolumeHeader
->HeaderLength
= sizeof(EFI_FIRMWARE_VOLUME_HEADER
) + sizeof(EFI_FV_BLOCK_MAP_ENTRY
);
103 FirmwareVolumeHeader
->Revision
= EFI_FVH_REVISION
;
104 FirmwareVolumeHeader
->BlockMap
[0].NumBlocks
= Instance
->Media
.LastBlock
+ 1;
105 FirmwareVolumeHeader
->BlockMap
[0].Length
= Instance
->Media
.BlockSize
;
106 FirmwareVolumeHeader
->BlockMap
[1].NumBlocks
= 0;
107 FirmwareVolumeHeader
->BlockMap
[1].Length
= 0;
108 FirmwareVolumeHeader
->Checksum
= CalculateCheckSum16 ((UINT16
*)FirmwareVolumeHeader
,FirmwareVolumeHeader
->HeaderLength
);
111 // VARIABLE_STORE_HEADER
113 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)((UINTN
)Headers
+ FirmwareVolumeHeader
->HeaderLength
);
114 CopyGuid (&VariableStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
);
115 VariableStoreHeader
->Size
= PcdGet32(PcdFlashNvStorageVariableSize
) - FirmwareVolumeHeader
->HeaderLength
;
116 VariableStoreHeader
->Format
= VARIABLE_STORE_FORMATTED
;
117 VariableStoreHeader
->State
= VARIABLE_STORE_HEALTHY
;
119 // Install the combined super-header in the NorFlash
120 Status
= FvbWrite (&Instance
->FvbProtocol
, 0, 0, &HeadersLength
, Headers
);
127 Check the integrity of firmware volume header.
129 @param[in] FwVolHeader - A pointer to a firmware volume header
131 @retval EFI_SUCCESS - The firmware volume is consistent
132 @retval EFI_NOT_FOUND - The firmware volume has been corrupted.
137 IN NOR_FLASH_INSTANCE
*Instance
141 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
142 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
143 UINTN VariableStoreLength
;
146 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)Instance
->RegionBaseAddress
;
148 FvLength
= PcdGet32(PcdFlashNvStorageVariableSize
) + PcdGet32(PcdFlashNvStorageFtwWorkingSize
) +
149 PcdGet32(PcdFlashNvStorageFtwSpareSize
);
152 // Verify the header revision, header signature, length
153 // Length of FvBlock cannot be 2**64-1
154 // HeaderLength cannot be an odd number
156 if ( (FwVolHeader
->Revision
!= EFI_FVH_REVISION
)
157 || (FwVolHeader
->Signature
!= EFI_FVH_SIGNATURE
)
158 || (FwVolHeader
->FvLength
!= FvLength
)
161 DEBUG ((EFI_D_INFO
, "%a: No Firmware Volume header present\n",
163 return EFI_NOT_FOUND
;
166 // Check the Firmware Volume Guid
167 if( CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiSystemNvDataFvGuid
) == FALSE
) {
168 DEBUG ((EFI_D_INFO
, "%a: Firmware Volume Guid non-compatible\n",
170 return EFI_NOT_FOUND
;
173 // Verify the header checksum
174 Checksum
= CalculateSum16((UINT16
*)FwVolHeader
, FwVolHeader
->HeaderLength
);
176 DEBUG ((EFI_D_INFO
, "%a: FV checksum is invalid (Checksum:0x%X)\n",
177 __FUNCTION__
, Checksum
));
178 return EFI_NOT_FOUND
;
181 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)((UINTN
)FwVolHeader
+ FwVolHeader
->HeaderLength
);
183 // Check the Variable Store Guid
184 if (!CompareGuid (&VariableStoreHeader
->Signature
, &gEfiVariableGuid
) &&
185 !CompareGuid (&VariableStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
)) {
186 DEBUG ((EFI_D_INFO
, "%a: Variable Store Guid non-compatible\n",
188 return EFI_NOT_FOUND
;
191 VariableStoreLength
= PcdGet32 (PcdFlashNvStorageVariableSize
) - FwVolHeader
->HeaderLength
;
192 if (VariableStoreHeader
->Size
!= VariableStoreLength
) {
193 DEBUG ((EFI_D_INFO
, "%a: Variable Store Length does not match\n",
195 return EFI_NOT_FOUND
;
202 The GetAttributes() function retrieves the attributes and
203 current settings of the block.
205 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
207 @param Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the attributes and
208 current settings are returned.
209 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
211 @retval EFI_SUCCESS The firmware volume attributes were returned.
217 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
218 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
221 EFI_FVB_ATTRIBUTES_2 FlashFvbAttributes
;
222 NOR_FLASH_INSTANCE
*Instance
;
224 Instance
= INSTANCE_FROM_FVB_THIS(This
);
226 FlashFvbAttributes
= (EFI_FVB_ATTRIBUTES_2
) (
228 EFI_FVB2_READ_ENABLED_CAP
| // Reads may be enabled
229 EFI_FVB2_READ_STATUS
| // Reads are currently enabled
230 EFI_FVB2_STICKY_WRITE
| // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
231 EFI_FVB2_MEMORY_MAPPED
| // It is memory mapped
232 EFI_FVB2_ERASE_POLARITY
// After erasure all bits take this value (i.e. '1')
236 // Check if it is write protected
237 if (Instance
->Media
.ReadOnly
!= TRUE
) {
239 FlashFvbAttributes
= FlashFvbAttributes
|
240 EFI_FVB2_WRITE_STATUS
| // Writes are currently enabled
241 EFI_FVB2_WRITE_ENABLED_CAP
; // Writes may be enabled
244 *Attributes
= FlashFvbAttributes
;
246 DEBUG ((DEBUG_BLKIO
, "FvbGetAttributes(0x%X)\n", *Attributes
));
252 The SetAttributes() function sets configurable firmware volume attributes
253 and returns the new settings of the firmware volume.
256 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
258 @param Attributes On input, Attributes is a pointer to EFI_FVB_ATTRIBUTES_2
259 that contains the desired firmware volume settings.
260 On successful return, it contains the new settings of
262 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
264 @retval EFI_SUCCESS The firmware volume attributes were returned.
266 @retval EFI_INVALID_PARAMETER The attributes requested are in conflict with the capabilities
267 as declared in the firmware volume header.
273 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
274 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
277 DEBUG ((DEBUG_BLKIO
, "FvbSetAttributes(0x%X) is not supported\n",*Attributes
));
278 return EFI_UNSUPPORTED
;
282 The GetPhysicalAddress() function retrieves the base address of
283 a memory-mapped firmware volume. This function should be called
284 only for memory-mapped firmware volumes.
286 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
288 @param Address Pointer to a caller-allocated
289 EFI_PHYSICAL_ADDRESS that, on successful
290 return from GetPhysicalAddress(), contains the
291 base address of the firmware volume.
293 @retval EFI_SUCCESS The firmware volume base address was returned.
295 @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.
300 FvbGetPhysicalAddress (
301 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
302 OUT EFI_PHYSICAL_ADDRESS
*Address
305 NOR_FLASH_INSTANCE
*Instance
;
307 Instance
= INSTANCE_FROM_FVB_THIS(This
);
309 DEBUG ((DEBUG_BLKIO
, "FvbGetPhysicalAddress(BaseAddress=0x%08x)\n", Instance
->RegionBaseAddress
));
311 ASSERT(Address
!= NULL
);
313 *Address
= mFlashNvStorageVariableBase
;
318 The GetBlockSize() function retrieves the size of the requested
319 block. It also returns the number of additional blocks with
320 the identical size. The GetBlockSize() function is used to
321 retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).
324 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
326 @param Lba Indicates the block for which to return the size.
328 @param BlockSize Pointer to a caller-allocated UINTN in which
329 the size of the block is returned.
331 @param NumberOfBlocks Pointer to a caller-allocated UINTN in
332 which the number of consecutive blocks,
333 starting with Lba, is returned. All
334 blocks in this range have a size of
338 @retval EFI_SUCCESS The firmware volume base address was returned.
340 @retval EFI_INVALID_PARAMETER The requested LBA is out of range.
346 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
348 OUT UINTN
*BlockSize
,
349 OUT UINTN
*NumberOfBlocks
353 NOR_FLASH_INSTANCE
*Instance
;
355 Instance
= INSTANCE_FROM_FVB_THIS(This
);
357 DEBUG ((DEBUG_BLKIO
, "FvbGetBlockSize(Lba=%ld, BlockSize=0x%x, LastBlock=%ld)\n", Lba
, Instance
->Media
.BlockSize
, Instance
->Media
.LastBlock
));
359 if (Lba
> Instance
->Media
.LastBlock
) {
360 DEBUG ((EFI_D_ERROR
, "FvbGetBlockSize: ERROR - Parameter LBA %ld is beyond the last Lba (%ld).\n", Lba
, Instance
->Media
.LastBlock
));
361 Status
= EFI_INVALID_PARAMETER
;
363 // This is easy because in this platform each NorFlash device has equal sized blocks.
364 *BlockSize
= (UINTN
) Instance
->Media
.BlockSize
;
365 *NumberOfBlocks
= (UINTN
) (Instance
->Media
.LastBlock
- Lba
+ 1);
367 DEBUG ((DEBUG_BLKIO
, "FvbGetBlockSize: *BlockSize=0x%x, *NumberOfBlocks=0x%x.\n", *BlockSize
, *NumberOfBlocks
));
369 Status
= EFI_SUCCESS
;
376 Reads the specified number of bytes into a buffer from the specified block.
378 The Read() function reads the requested number of bytes from the
379 requested block and stores them in the provided buffer.
380 Implementations should be mindful that the firmware volume
381 might be in the ReadDisabled state. If it is in this state,
382 the Read() function must return the status code
383 EFI_ACCESS_DENIED without modifying the contents of the
384 buffer. The Read() function must also prevent spanning block
385 boundaries. If a read is requested that would span a block
386 boundary, the read must read up to the boundary but not
387 beyond. The output parameter NumBytes must be set to correctly
388 indicate the number of bytes actually read. The caller must be
389 aware that a read may be partially completed.
391 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
393 @param Lba The starting logical block index from which to read.
395 @param Offset Offset into the block at which to begin reading.
397 @param NumBytes Pointer to a UINTN.
398 At entry, *NumBytes contains the total size of the buffer.
399 At exit, *NumBytes contains the total number of bytes read.
401 @param Buffer Pointer to a caller-allocated buffer that will be used
402 to hold the data that is read.
404 @retval EFI_SUCCESS The firmware volume was read successfully, and contents are
407 @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.
408 On output, NumBytes contains the total number of bytes
411 @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state.
413 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be read.
419 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
422 IN OUT UINTN
*NumBytes
,
426 EFI_STATUS TempStatus
;
428 NOR_FLASH_INSTANCE
*Instance
;
430 Instance
= INSTANCE_FROM_FVB_THIS(This
);
432 DEBUG ((DEBUG_BLKIO
, "FvbRead(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Instance
->StartLba
+ Lba
, Offset
, *NumBytes
, Buffer
));
434 if (!Instance
->Initialized
&& Instance
->Initialize
) {
435 Instance
->Initialize(Instance
);
438 TempStatus
= EFI_SUCCESS
;
440 // Cache the block size to avoid de-referencing pointers all the time
441 BlockSize
= Instance
->Media
.BlockSize
;
443 DEBUG ((DEBUG_BLKIO
, "FvbRead: Check if (Offset=0x%x + NumBytes=0x%x) <= BlockSize=0x%x\n", Offset
, *NumBytes
, BlockSize
));
445 // The read must not span block boundaries.
446 // We need to check each variable individually because adding two large values together overflows.
447 if ((Offset
>= BlockSize
) ||
448 (*NumBytes
> BlockSize
) ||
449 ((Offset
+ *NumBytes
) > BlockSize
)) {
450 DEBUG ((EFI_D_ERROR
, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset
, *NumBytes
, BlockSize
));
451 return EFI_BAD_BUFFER_SIZE
;
454 // We must have some bytes to read
455 if (*NumBytes
== 0) {
456 return EFI_BAD_BUFFER_SIZE
;
459 // Decide if we are doing full block reads or not.
460 if (*NumBytes
% BlockSize
!= 0) {
461 TempStatus
= NorFlashRead (Instance
, Instance
->StartLba
+ Lba
, Offset
, *NumBytes
, Buffer
);
462 if (EFI_ERROR (TempStatus
)) {
463 return EFI_DEVICE_ERROR
;
466 // Read NOR Flash data into shadow buffer
467 TempStatus
= NorFlashReadBlocks (Instance
, Instance
->StartLba
+ Lba
, BlockSize
, Buffer
);
468 if (EFI_ERROR (TempStatus
)) {
469 // Return one of the pre-approved error statuses
470 return EFI_DEVICE_ERROR
;
477 Writes the specified number of bytes from the input buffer to the block.
479 The Write() function writes the specified number of bytes from
480 the provided buffer to the specified block and offset. If the
481 firmware volume is sticky write, the caller must ensure that
482 all the bits of the specified range to write are in the
483 EFI_FVB_ERASE_POLARITY state before calling the Write()
484 function, or else the result will be unpredictable. This
485 unpredictability arises because, for a sticky-write firmware
486 volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY
487 state but cannot flip it back again. Before calling the
488 Write() function, it is recommended for the caller to first call
489 the EraseBlocks() function to erase the specified block to
490 write. A block erase cycle will transition bits from the
491 (NOT)EFI_FVB_ERASE_POLARITY state back to the
492 EFI_FVB_ERASE_POLARITY state. Implementations should be
493 mindful that the firmware volume might be in the WriteDisabled
494 state. If it is in this state, the Write() function must
495 return the status code EFI_ACCESS_DENIED without modifying the
496 contents of the firmware volume. The Write() function must
497 also prevent spanning block boundaries. If a write is
498 requested that spans a block boundary, the write must store up
499 to the boundary but not beyond. The output parameter NumBytes
500 must be set to correctly indicate the number of bytes actually
501 written. The caller must be aware that a write may be
502 partially completed. All writes, partial or otherwise, must be
503 fully flushed to the hardware before the Write() service
506 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
508 @param Lba The starting logical block index to write to.
510 @param Offset Offset into the block at which to begin writing.
512 @param NumBytes The pointer to a UINTN.
513 At entry, *NumBytes contains the total size of the buffer.
514 At exit, *NumBytes contains the total number of bytes actually written.
516 @param Buffer The pointer to a caller-allocated buffer that contains the source for the write.
518 @retval EFI_SUCCESS The firmware volume was written successfully.
520 @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
521 On output, NumBytes contains the total number of bytes
524 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
526 @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not be written.
533 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
536 IN OUT UINTN
*NumBytes
,
540 NOR_FLASH_INSTANCE
*Instance
;
542 Instance
= INSTANCE_FROM_FVB_THIS (This
);
544 return NorFlashWriteSingleBlock (Instance
, Instance
->StartLba
+ Lba
, Offset
, NumBytes
, Buffer
);
548 Erases and initialises a firmware volume block.
550 The EraseBlocks() function erases one or more blocks as denoted
551 by the variable argument list. The entire parameter list of
552 blocks must be verified before erasing any blocks. If a block is
553 requested that does not exist within the associated firmware
554 volume (it has a larger index than the last block of the
555 firmware volume), the EraseBlocks() function must return the
556 status code EFI_INVALID_PARAMETER without modifying the contents
557 of the firmware volume. Implementations should be mindful that
558 the firmware volume might be in the WriteDisabled state. If it
559 is in this state, the EraseBlocks() function must return the
560 status code EFI_ACCESS_DENIED without modifying the contents of
561 the firmware volume. All calls to EraseBlocks() must be fully
562 flushed to the hardware before the EraseBlocks() service
565 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
568 @param ... The variable argument list is a list of tuples.
569 Each tuple describes a range of LBAs to erase
570 and consists of the following:
571 - An EFI_LBA that indicates the starting LBA
572 - A UINTN that indicates the number of blocks to erase.
574 The list is terminated with an EFI_LBA_LIST_TERMINATOR.
575 For example, the following indicates that two ranges of blocks
576 (5-7 and 10-11) are to be erased:
577 EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);
579 @retval EFI_SUCCESS The erase request successfully completed.
581 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
583 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be written.
584 The firmware device may have been partially erased.
586 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the variable argument list do
587 not exist in the firmware volume.
593 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
599 UINTN BlockAddress
; // Physical address of Lba to erase
600 EFI_LBA StartingLba
; // Lba from which we start erasing
601 UINTN NumOfLba
; // Number of Lba blocks to erase
602 NOR_FLASH_INSTANCE
*Instance
;
604 Instance
= INSTANCE_FROM_FVB_THIS(This
);
606 DEBUG ((DEBUG_BLKIO
, "FvbEraseBlocks()\n"));
608 Status
= EFI_SUCCESS
;
610 // Detect WriteDisabled state
611 if (Instance
->Media
.ReadOnly
== TRUE
) {
612 // Firmware volume is in WriteDisabled state
613 DEBUG ((EFI_D_ERROR
, "FvbEraseBlocks: ERROR - Device is in WriteDisabled state.\n"));
614 return EFI_ACCESS_DENIED
;
617 // Before erasing, check the entire list of parameters to ensure all specified blocks are valid
619 VA_START (Args
, This
);
621 // Get the Lba from which we start erasing
622 StartingLba
= VA_ARG (Args
, EFI_LBA
);
624 // Have we reached the end of the list?
625 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
626 //Exit the while loop
630 // How many Lba blocks are we requested to erase?
631 NumOfLba
= VA_ARG (Args
, UINT32
);
633 // All blocks must be within range
634 DEBUG ((DEBUG_BLKIO
, "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%d - 1 ) > LastBlock=%ld.\n", Instance
->StartLba
+ StartingLba
, NumOfLba
, Instance
->Media
.LastBlock
));
635 if ((NumOfLba
== 0) || ((Instance
->StartLba
+ StartingLba
+ NumOfLba
- 1) > Instance
->Media
.LastBlock
)) {
637 DEBUG ((EFI_D_ERROR
, "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n"));
638 Status
= EFI_INVALID_PARAMETER
;
645 // To get here, all must be ok, so start erasing
647 VA_START (Args
, This
);
649 // Get the Lba from which we start erasing
650 StartingLba
= VA_ARG (Args
, EFI_LBA
);
652 // Have we reached the end of the list?
653 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
654 // Exit the while loop
658 // How many Lba blocks are we requested to erase?
659 NumOfLba
= VA_ARG (Args
, UINT32
);
661 // Go through each one and erase it
662 while (NumOfLba
> 0) {
664 // Get the physical address of Lba to erase
665 BlockAddress
= GET_NOR_BLOCK_ADDRESS (
666 Instance
->RegionBaseAddress
,
667 Instance
->StartLba
+ StartingLba
,
668 Instance
->Media
.BlockSize
672 DEBUG ((DEBUG_BLKIO
, "FvbEraseBlocks: Erasing Lba=%ld @ 0x%08x.\n", Instance
->StartLba
+ StartingLba
, BlockAddress
));
673 Status
= NorFlashUnlockAndEraseSingleBlock (Instance
, BlockAddress
);
674 if (EFI_ERROR(Status
)) {
676 Status
= EFI_DEVICE_ERROR
;
680 // Move to the next Lba
692 Fixup internal data so that EFI can be call in virtual mode.
693 Call the passed in Child Notify event and convert any pointers in
696 @param[in] Event The Event that is being processed
697 @param[in] Context Event Context
701 FvbVirtualNotifyEvent (
706 EfiConvertPointer (0x0, (VOID
**)&mFlashNvStorageVariableBase
);
712 NorFlashFvbInitialize (
713 IN NOR_FLASH_INSTANCE
* Instance
718 EFI_BOOT_MODE BootMode
;
719 UINTN RuntimeMmioRegionSize
;
721 DEBUG((DEBUG_BLKIO
,"NorFlashFvbInitialize\n"));
722 ASSERT((Instance
!= NULL
));
725 // Declare the Non-Volatile storage as EFI_MEMORY_RUNTIME
728 // Note: all the NOR Flash region needs to be reserved into the UEFI Runtime memory;
729 // even if we only use the small block region at the top of the NOR Flash.
730 // The reason is when the NOR Flash memory is set into program mode, the command
731 // is written as the base of the flash region (ie: Instance->DeviceBaseAddress)
732 RuntimeMmioRegionSize
= (Instance
->RegionBaseAddress
- Instance
->DeviceBaseAddress
) + Instance
->Size
;
734 Status
= gDS
->AddMemorySpace (
735 EfiGcdMemoryTypeMemoryMappedIo
,
736 Instance
->DeviceBaseAddress
, RuntimeMmioRegionSize
,
737 EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
739 ASSERT_EFI_ERROR (Status
);
741 Status
= gDS
->SetMemorySpaceAttributes (
742 Instance
->DeviceBaseAddress
, RuntimeMmioRegionSize
,
743 EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
);
744 ASSERT_EFI_ERROR (Status
);
746 Instance
->Initialized
= TRUE
;
747 mFlashNvStorageVariableBase
= FixedPcdGet32 (PcdFlashNvStorageVariableBase
);
749 // Set the index of the first LBA for the FVB
750 Instance
->StartLba
= (PcdGet32 (PcdFlashNvStorageVariableBase
) - Instance
->RegionBaseAddress
) / Instance
->Media
.BlockSize
;
752 BootMode
= GetBootModeHob ();
753 if (BootMode
== BOOT_WITH_DEFAULT_SETTINGS
) {
754 Status
= EFI_INVALID_PARAMETER
;
756 // Determine if there is a valid header at the beginning of the NorFlash
757 Status
= ValidateFvHeader (Instance
);
760 // Install the Default FVB header if required
761 if (EFI_ERROR(Status
)) {
762 // There is no valid header, so time to install one.
763 DEBUG ((EFI_D_INFO
, "%a: The FVB Header is not valid.\n", __FUNCTION__
));
764 DEBUG ((EFI_D_INFO
, "%a: Installing a correct one for this volume.\n",
767 // Erase all the NorFlash that is reserved for variable storage
768 FvbNumLba
= (PcdGet32(PcdFlashNvStorageVariableSize
) + PcdGet32(PcdFlashNvStorageFtwWorkingSize
) + PcdGet32(PcdFlashNvStorageFtwSpareSize
)) / Instance
->Media
.BlockSize
;
770 Status
= FvbEraseBlocks (&Instance
->FvbProtocol
, (EFI_LBA
)0, FvbNumLba
, EFI_LBA_LIST_TERMINATOR
);
771 if (EFI_ERROR(Status
)) {
775 // Install all appropriate headers
776 Status
= InitializeFvAndVariableStoreHeaders (Instance
);
777 if (EFI_ERROR(Status
)) {
783 // Register for the virtual address change event
785 Status
= gBS
->CreateEventEx (
788 FvbVirtualNotifyEvent
,
790 &gEfiEventVirtualAddressChangeGuid
,
791 &mFvbVirtualAddrChangeEvent
793 ASSERT_EFI_ERROR (Status
);