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 HeadersLength
= sizeof(EFI_FIRMWARE_VOLUME_HEADER
) + sizeof(EFI_FV_BLOCK_MAP_ENTRY
) + sizeof(VARIABLE_STORE_HEADER
);
63 Headers
= AllocateZeroPool(HeadersLength
);
65 // FirmwareVolumeHeader->FvLength is declared to have the Variable area AND the FTW working area AND the FTW Spare contiguous.
66 ASSERT(PcdGet32(PcdFlashNvStorageVariableBase
) + PcdGet32(PcdFlashNvStorageVariableSize
) == PcdGet32(PcdFlashNvStorageFtwWorkingBase
));
67 ASSERT(PcdGet32(PcdFlashNvStorageFtwWorkingBase
) + PcdGet32(PcdFlashNvStorageFtwWorkingSize
) == PcdGet32(PcdFlashNvStorageFtwSpareBase
));
69 // Check if the size of the area is at least one block size
70 ASSERT((PcdGet32(PcdFlashNvStorageVariableSize
) > 0) && (PcdGet32(PcdFlashNvStorageVariableSize
) / Instance
->Media
.BlockSize
> 0));
71 ASSERT((PcdGet32(PcdFlashNvStorageFtwWorkingSize
) > 0) && (PcdGet32(PcdFlashNvStorageFtwWorkingSize
) / Instance
->Media
.BlockSize
> 0));
72 ASSERT((PcdGet32(PcdFlashNvStorageFtwSpareSize
) > 0) && (PcdGet32(PcdFlashNvStorageFtwSpareSize
) / Instance
->Media
.BlockSize
> 0));
74 // Ensure the Variable area Base Addresses are aligned on a block size boundaries
75 ASSERT(PcdGet32(PcdFlashNvStorageVariableBase
) % Instance
->Media
.BlockSize
== 0);
76 ASSERT(PcdGet32(PcdFlashNvStorageFtwWorkingBase
) % Instance
->Media
.BlockSize
== 0);
77 ASSERT(PcdGet32(PcdFlashNvStorageFtwSpareBase
) % Instance
->Media
.BlockSize
== 0);
80 // EFI_FIRMWARE_VOLUME_HEADER
82 FirmwareVolumeHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)Headers
;
83 CopyGuid (&FirmwareVolumeHeader
->FileSystemGuid
, &gEfiSystemNvDataFvGuid
);
84 FirmwareVolumeHeader
->FvLength
=
85 PcdGet32(PcdFlashNvStorageVariableSize
) +
86 PcdGet32(PcdFlashNvStorageFtwWorkingSize
) +
87 PcdGet32(PcdFlashNvStorageFtwSpareSize
);
88 FirmwareVolumeHeader
->Signature
= EFI_FVH_SIGNATURE
;
89 FirmwareVolumeHeader
->Attributes
= (EFI_FVB_ATTRIBUTES_2
) (
90 EFI_FVB2_READ_ENABLED_CAP
| // Reads may be enabled
91 EFI_FVB2_READ_STATUS
| // Reads are currently enabled
92 EFI_FVB2_STICKY_WRITE
| // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
93 EFI_FVB2_MEMORY_MAPPED
| // It is memory mapped
94 EFI_FVB2_ERASE_POLARITY
| // After erasure all bits take this value (i.e. '1')
95 EFI_FVB2_WRITE_STATUS
| // Writes are currently enabled
96 EFI_FVB2_WRITE_ENABLED_CAP
// Writes may be enabled
98 FirmwareVolumeHeader
->HeaderLength
= sizeof(EFI_FIRMWARE_VOLUME_HEADER
) + sizeof(EFI_FV_BLOCK_MAP_ENTRY
);
99 FirmwareVolumeHeader
->Revision
= EFI_FVH_REVISION
;
100 FirmwareVolumeHeader
->BlockMap
[0].NumBlocks
= Instance
->Media
.LastBlock
+ 1;
101 FirmwareVolumeHeader
->BlockMap
[0].Length
= Instance
->Media
.BlockSize
;
102 FirmwareVolumeHeader
->BlockMap
[1].NumBlocks
= 0;
103 FirmwareVolumeHeader
->BlockMap
[1].Length
= 0;
104 FirmwareVolumeHeader
->Checksum
= CalculateCheckSum16 ((UINT16
*)FirmwareVolumeHeader
,FirmwareVolumeHeader
->HeaderLength
);
107 // VARIABLE_STORE_HEADER
109 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)((UINTN
)Headers
+ FirmwareVolumeHeader
->HeaderLength
);
110 CopyGuid (&VariableStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
);
111 VariableStoreHeader
->Size
= PcdGet32(PcdFlashNvStorageVariableSize
) - FirmwareVolumeHeader
->HeaderLength
;
112 VariableStoreHeader
->Format
= VARIABLE_STORE_FORMATTED
;
113 VariableStoreHeader
->State
= VARIABLE_STORE_HEALTHY
;
115 // Install the combined super-header in the NorFlash
116 Status
= FvbWrite (&Instance
->FvbProtocol
, 0, 0, &HeadersLength
, Headers
);
123 Check the integrity of firmware volume header.
125 @param[in] FwVolHeader - A pointer to a firmware volume header
127 @retval EFI_SUCCESS - The firmware volume is consistent
128 @retval EFI_NOT_FOUND - The firmware volume has been corrupted.
133 IN NOR_FLASH_INSTANCE
*Instance
137 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
138 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
139 UINTN VariableStoreLength
;
142 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)Instance
->RegionBaseAddress
;
144 FvLength
= PcdGet32(PcdFlashNvStorageVariableSize
) + PcdGet32(PcdFlashNvStorageFtwWorkingSize
) +
145 PcdGet32(PcdFlashNvStorageFtwSpareSize
);
148 // Verify the header revision, header signature, length
149 // Length of FvBlock cannot be 2**64-1
150 // HeaderLength cannot be an odd number
152 if ( (FwVolHeader
->Revision
!= EFI_FVH_REVISION
)
153 || (FwVolHeader
->Signature
!= EFI_FVH_SIGNATURE
)
154 || (FwVolHeader
->FvLength
!= FvLength
)
157 DEBUG ((EFI_D_INFO
, "%a: No Firmware Volume header present\n",
159 return EFI_NOT_FOUND
;
162 // Check the Firmware Volume Guid
163 if( CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiSystemNvDataFvGuid
) == FALSE
) {
164 DEBUG ((EFI_D_INFO
, "%a: Firmware Volume Guid non-compatible\n",
166 return EFI_NOT_FOUND
;
169 // Verify the header checksum
170 Checksum
= CalculateSum16((UINT16
*)FwVolHeader
, FwVolHeader
->HeaderLength
);
172 DEBUG ((EFI_D_INFO
, "%a: FV checksum is invalid (Checksum:0x%X)\n",
173 __FUNCTION__
, Checksum
));
174 return EFI_NOT_FOUND
;
177 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)((UINTN
)FwVolHeader
+ FwVolHeader
->HeaderLength
);
179 // Check the Variable Store Guid
180 if (!CompareGuid (&VariableStoreHeader
->Signature
, &gEfiVariableGuid
) &&
181 !CompareGuid (&VariableStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
)) {
182 DEBUG ((EFI_D_INFO
, "%a: Variable Store Guid non-compatible\n",
184 return EFI_NOT_FOUND
;
187 VariableStoreLength
= PcdGet32 (PcdFlashNvStorageVariableSize
) - FwVolHeader
->HeaderLength
;
188 if (VariableStoreHeader
->Size
!= VariableStoreLength
) {
189 DEBUG ((EFI_D_INFO
, "%a: Variable Store Length does not match\n",
191 return EFI_NOT_FOUND
;
198 The GetAttributes() function retrieves the attributes and
199 current settings of the block.
201 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
203 @param Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the attributes and
204 current settings are returned.
205 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
207 @retval EFI_SUCCESS The firmware volume attributes were returned.
213 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
214 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
217 EFI_FVB_ATTRIBUTES_2 FlashFvbAttributes
;
218 NOR_FLASH_INSTANCE
*Instance
;
220 Instance
= INSTANCE_FROM_FVB_THIS(This
);
222 FlashFvbAttributes
= (EFI_FVB_ATTRIBUTES_2
) (
224 EFI_FVB2_READ_ENABLED_CAP
| // Reads may be enabled
225 EFI_FVB2_READ_STATUS
| // Reads are currently enabled
226 EFI_FVB2_STICKY_WRITE
| // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
227 EFI_FVB2_MEMORY_MAPPED
| // It is memory mapped
228 EFI_FVB2_ERASE_POLARITY
// After erasure all bits take this value (i.e. '1')
232 // Check if it is write protected
233 if (Instance
->Media
.ReadOnly
!= TRUE
) {
235 FlashFvbAttributes
= FlashFvbAttributes
|
236 EFI_FVB2_WRITE_STATUS
| // Writes are currently enabled
237 EFI_FVB2_WRITE_ENABLED_CAP
; // Writes may be enabled
240 *Attributes
= FlashFvbAttributes
;
242 DEBUG ((DEBUG_BLKIO
, "FvbGetAttributes(0x%X)\n", *Attributes
));
248 The SetAttributes() function sets configurable firmware volume attributes
249 and returns the new settings of the firmware volume.
252 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
254 @param Attributes On input, Attributes is a pointer to EFI_FVB_ATTRIBUTES_2
255 that contains the desired firmware volume settings.
256 On successful return, it contains the new settings of
258 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
260 @retval EFI_SUCCESS The firmware volume attributes were returned.
262 @retval EFI_INVALID_PARAMETER The attributes requested are in conflict with the capabilities
263 as declared in the firmware volume header.
269 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
270 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
273 DEBUG ((DEBUG_BLKIO
, "FvbSetAttributes(0x%X) is not supported\n",*Attributes
));
274 return EFI_UNSUPPORTED
;
278 The GetPhysicalAddress() function retrieves the base address of
279 a memory-mapped firmware volume. This function should be called
280 only for memory-mapped firmware volumes.
282 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
284 @param Address Pointer to a caller-allocated
285 EFI_PHYSICAL_ADDRESS that, on successful
286 return from GetPhysicalAddress(), contains the
287 base address of the firmware volume.
289 @retval EFI_SUCCESS The firmware volume base address was returned.
291 @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.
296 FvbGetPhysicalAddress (
297 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
298 OUT EFI_PHYSICAL_ADDRESS
*Address
301 NOR_FLASH_INSTANCE
*Instance
;
303 Instance
= INSTANCE_FROM_FVB_THIS(This
);
305 DEBUG ((DEBUG_BLKIO
, "FvbGetPhysicalAddress(BaseAddress=0x%08x)\n", Instance
->RegionBaseAddress
));
307 ASSERT(Address
!= NULL
);
309 *Address
= mFlashNvStorageVariableBase
;
314 The GetBlockSize() function retrieves the size of the requested
315 block. It also returns the number of additional blocks with
316 the identical size. The GetBlockSize() function is used to
317 retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).
320 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
322 @param Lba Indicates the block for which to return the size.
324 @param BlockSize Pointer to a caller-allocated UINTN in which
325 the size of the block is returned.
327 @param NumberOfBlocks Pointer to a caller-allocated UINTN in
328 which the number of consecutive blocks,
329 starting with Lba, is returned. All
330 blocks in this range have a size of
334 @retval EFI_SUCCESS The firmware volume base address was returned.
336 @retval EFI_INVALID_PARAMETER The requested LBA is out of range.
342 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
344 OUT UINTN
*BlockSize
,
345 OUT UINTN
*NumberOfBlocks
349 NOR_FLASH_INSTANCE
*Instance
;
351 Instance
= INSTANCE_FROM_FVB_THIS(This
);
353 DEBUG ((DEBUG_BLKIO
, "FvbGetBlockSize(Lba=%ld, BlockSize=0x%x, LastBlock=%ld)\n", Lba
, Instance
->Media
.BlockSize
, Instance
->Media
.LastBlock
));
355 if (Lba
> Instance
->Media
.LastBlock
) {
356 DEBUG ((EFI_D_ERROR
, "FvbGetBlockSize: ERROR - Parameter LBA %ld is beyond the last Lba (%ld).\n", Lba
, Instance
->Media
.LastBlock
));
357 Status
= EFI_INVALID_PARAMETER
;
359 // This is easy because in this platform each NorFlash device has equal sized blocks.
360 *BlockSize
= (UINTN
) Instance
->Media
.BlockSize
;
361 *NumberOfBlocks
= (UINTN
) (Instance
->Media
.LastBlock
- Lba
+ 1);
363 DEBUG ((DEBUG_BLKIO
, "FvbGetBlockSize: *BlockSize=0x%x, *NumberOfBlocks=0x%x.\n", *BlockSize
, *NumberOfBlocks
));
365 Status
= EFI_SUCCESS
;
372 Reads the specified number of bytes into a buffer from the specified block.
374 The Read() function reads the requested number of bytes from the
375 requested block and stores them in the provided buffer.
376 Implementations should be mindful that the firmware volume
377 might be in the ReadDisabled state. If it is in this state,
378 the Read() function must return the status code
379 EFI_ACCESS_DENIED without modifying the contents of the
380 buffer. The Read() function must also prevent spanning block
381 boundaries. If a read is requested that would span a block
382 boundary, the read must read up to the boundary but not
383 beyond. The output parameter NumBytes must be set to correctly
384 indicate the number of bytes actually read. The caller must be
385 aware that a read may be partially completed.
387 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
389 @param Lba The starting logical block index from which to read.
391 @param Offset Offset into the block at which to begin reading.
393 @param NumBytes Pointer to a UINTN.
394 At entry, *NumBytes contains the total size of the buffer.
395 At exit, *NumBytes contains the total number of bytes read.
397 @param Buffer Pointer to a caller-allocated buffer that will be used
398 to hold the data that is read.
400 @retval EFI_SUCCESS The firmware volume was read successfully, and contents are
403 @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.
404 On output, NumBytes contains the total number of bytes
407 @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state.
409 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be read.
415 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
418 IN OUT UINTN
*NumBytes
,
422 EFI_STATUS TempStatus
;
424 NOR_FLASH_INSTANCE
*Instance
;
426 Instance
= INSTANCE_FROM_FVB_THIS(This
);
428 DEBUG ((DEBUG_BLKIO
, "FvbRead(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Instance
->StartLba
+ Lba
, Offset
, *NumBytes
, Buffer
));
430 TempStatus
= EFI_SUCCESS
;
432 // Cache the block size to avoid de-referencing pointers all the time
433 BlockSize
= Instance
->Media
.BlockSize
;
435 DEBUG ((DEBUG_BLKIO
, "FvbRead: Check if (Offset=0x%x + NumBytes=0x%x) <= BlockSize=0x%x\n", Offset
, *NumBytes
, BlockSize
));
437 // The read must not span block boundaries.
438 // We need to check each variable individually because adding two large values together overflows.
439 if ((Offset
>= BlockSize
) ||
440 (*NumBytes
> BlockSize
) ||
441 ((Offset
+ *NumBytes
) > BlockSize
)) {
442 DEBUG ((EFI_D_ERROR
, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset
, *NumBytes
, BlockSize
));
443 return EFI_BAD_BUFFER_SIZE
;
446 // We must have some bytes to read
447 if (*NumBytes
== 0) {
448 return EFI_BAD_BUFFER_SIZE
;
451 // Decide if we are doing full block reads or not.
452 if (*NumBytes
% BlockSize
!= 0) {
453 TempStatus
= NorFlashRead (Instance
, Instance
->StartLba
+ Lba
, Offset
, *NumBytes
, Buffer
);
454 if (EFI_ERROR (TempStatus
)) {
455 return EFI_DEVICE_ERROR
;
458 // Read NOR Flash data into shadow buffer
459 TempStatus
= NorFlashReadBlocks (Instance
, Instance
->StartLba
+ Lba
, BlockSize
, Buffer
);
460 if (EFI_ERROR (TempStatus
)) {
461 // Return one of the pre-approved error statuses
462 return EFI_DEVICE_ERROR
;
469 Writes the specified number of bytes from the input buffer to the block.
471 The Write() function writes the specified number of bytes from
472 the provided buffer to the specified block and offset. If the
473 firmware volume is sticky write, the caller must ensure that
474 all the bits of the specified range to write are in the
475 EFI_FVB_ERASE_POLARITY state before calling the Write()
476 function, or else the result will be unpredictable. This
477 unpredictability arises because, for a sticky-write firmware
478 volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY
479 state but cannot flip it back again. Before calling the
480 Write() function, it is recommended for the caller to first call
481 the EraseBlocks() function to erase the specified block to
482 write. A block erase cycle will transition bits from the
483 (NOT)EFI_FVB_ERASE_POLARITY state back to the
484 EFI_FVB_ERASE_POLARITY state. Implementations should be
485 mindful that the firmware volume might be in the WriteDisabled
486 state. If it is in this state, the Write() function must
487 return the status code EFI_ACCESS_DENIED without modifying the
488 contents of the firmware volume. The Write() function must
489 also prevent spanning block boundaries. If a write is
490 requested that spans a block boundary, the write must store up
491 to the boundary but not beyond. The output parameter NumBytes
492 must be set to correctly indicate the number of bytes actually
493 written. The caller must be aware that a write may be
494 partially completed. All writes, partial or otherwise, must be
495 fully flushed to the hardware before the Write() service
498 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
500 @param Lba The starting logical block index to write to.
502 @param Offset Offset into the block at which to begin writing.
504 @param NumBytes The pointer to a UINTN.
505 At entry, *NumBytes contains the total size of the buffer.
506 At exit, *NumBytes contains the total number of bytes actually written.
508 @param Buffer The pointer to a caller-allocated buffer that contains the source for the write.
510 @retval EFI_SUCCESS The firmware volume was written successfully.
512 @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
513 On output, NumBytes contains the total number of bytes
516 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
518 @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not be written.
525 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
528 IN OUT UINTN
*NumBytes
,
532 NOR_FLASH_INSTANCE
*Instance
;
534 Instance
= INSTANCE_FROM_FVB_THIS (This
);
536 return NorFlashWriteSingleBlock (Instance
, Instance
->StartLba
+ Lba
, Offset
, NumBytes
, Buffer
);
540 Erases and initialises a firmware volume block.
542 The EraseBlocks() function erases one or more blocks as denoted
543 by the variable argument list. The entire parameter list of
544 blocks must be verified before erasing any blocks. If a block is
545 requested that does not exist within the associated firmware
546 volume (it has a larger index than the last block of the
547 firmware volume), the EraseBlocks() function must return the
548 status code EFI_INVALID_PARAMETER without modifying the contents
549 of the firmware volume. Implementations should be mindful that
550 the firmware volume might be in the WriteDisabled state. If it
551 is in this state, the EraseBlocks() function must return the
552 status code EFI_ACCESS_DENIED without modifying the contents of
553 the firmware volume. All calls to EraseBlocks() must be fully
554 flushed to the hardware before the EraseBlocks() service
557 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
560 @param ... The variable argument list is a list of tuples.
561 Each tuple describes a range of LBAs to erase
562 and consists of the following:
563 - An EFI_LBA that indicates the starting LBA
564 - A UINTN that indicates the number of blocks to erase.
566 The list is terminated with an EFI_LBA_LIST_TERMINATOR.
567 For example, the following indicates that two ranges of blocks
568 (5-7 and 10-11) are to be erased:
569 EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);
571 @retval EFI_SUCCESS The erase request successfully completed.
573 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
575 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be written.
576 The firmware device may have been partially erased.
578 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the variable argument list do
579 not exist in the firmware volume.
585 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
591 UINTN BlockAddress
; // Physical address of Lba to erase
592 EFI_LBA StartingLba
; // Lba from which we start erasing
593 UINTN NumOfLba
; // Number of Lba blocks to erase
594 NOR_FLASH_INSTANCE
*Instance
;
596 Instance
= INSTANCE_FROM_FVB_THIS(This
);
598 DEBUG ((DEBUG_BLKIO
, "FvbEraseBlocks()\n"));
600 Status
= EFI_SUCCESS
;
602 // Detect WriteDisabled state
603 if (Instance
->Media
.ReadOnly
== TRUE
) {
604 // Firmware volume is in WriteDisabled state
605 DEBUG ((EFI_D_ERROR
, "FvbEraseBlocks: ERROR - Device is in WriteDisabled state.\n"));
606 return EFI_ACCESS_DENIED
;
609 // Before erasing, check the entire list of parameters to ensure all specified blocks are valid
611 VA_START (Args
, This
);
613 // Get the Lba from which we start erasing
614 StartingLba
= VA_ARG (Args
, EFI_LBA
);
616 // Have we reached the end of the list?
617 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
618 //Exit the while loop
622 // How many Lba blocks are we requested to erase?
623 NumOfLba
= VA_ARG (Args
, UINTN
);
625 // All blocks must be within range
628 "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%Lu - 1 ) > LastBlock=%ld.\n",
629 Instance
->StartLba
+ StartingLba
,
631 Instance
->Media
.LastBlock
633 if ((NumOfLba
== 0) || ((Instance
->StartLba
+ StartingLba
+ NumOfLba
- 1) > Instance
->Media
.LastBlock
)) {
635 DEBUG ((EFI_D_ERROR
, "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n"));
636 Status
= EFI_INVALID_PARAMETER
;
643 // To get here, all must be ok, so start erasing
645 VA_START (Args
, This
);
647 // Get the Lba from which we start erasing
648 StartingLba
= VA_ARG (Args
, EFI_LBA
);
650 // Have we reached the end of the list?
651 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
652 // Exit the while loop
656 // How many Lba blocks are we requested to erase?
657 NumOfLba
= VA_ARG (Args
, UINTN
);
659 // Go through each one and erase it
660 while (NumOfLba
> 0) {
662 // Get the physical address of Lba to erase
663 BlockAddress
= GET_NOR_BLOCK_ADDRESS (
664 Instance
->RegionBaseAddress
,
665 Instance
->StartLba
+ StartingLba
,
666 Instance
->Media
.BlockSize
670 DEBUG ((DEBUG_BLKIO
, "FvbEraseBlocks: Erasing Lba=%ld @ 0x%08x.\n", Instance
->StartLba
+ StartingLba
, BlockAddress
));
671 Status
= NorFlashUnlockAndEraseSingleBlock (Instance
, BlockAddress
);
672 if (EFI_ERROR(Status
)) {
674 Status
= EFI_DEVICE_ERROR
;
678 // Move to the next Lba
690 Fixup internal data so that EFI can be call in virtual mode.
691 Call the passed in Child Notify event and convert any pointers in
694 @param[in] Event The Event that is being processed
695 @param[in] Context Event Context
699 FvbVirtualNotifyEvent (
704 EfiConvertPointer (0x0, (VOID
**)&mFlashNvStorageVariableBase
);
710 NorFlashFvbInitialize (
711 IN NOR_FLASH_INSTANCE
* Instance
716 EFI_BOOT_MODE BootMode
;
717 UINTN RuntimeMmioRegionSize
;
719 DEBUG((DEBUG_BLKIO
,"NorFlashFvbInitialize\n"));
720 ASSERT((Instance
!= NULL
));
723 // Declare the Non-Volatile storage as EFI_MEMORY_RUNTIME
726 // Note: all the NOR Flash region needs to be reserved into the UEFI Runtime memory;
727 // even if we only use the small block region at the top of the NOR Flash.
728 // The reason is when the NOR Flash memory is set into program mode, the command
729 // is written as the base of the flash region (ie: Instance->DeviceBaseAddress)
730 RuntimeMmioRegionSize
= (Instance
->RegionBaseAddress
- Instance
->DeviceBaseAddress
) + Instance
->Size
;
732 Status
= gDS
->AddMemorySpace (
733 EfiGcdMemoryTypeMemoryMappedIo
,
734 Instance
->DeviceBaseAddress
, RuntimeMmioRegionSize
,
735 EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
737 ASSERT_EFI_ERROR (Status
);
739 Status
= gDS
->SetMemorySpaceAttributes (
740 Instance
->DeviceBaseAddress
, RuntimeMmioRegionSize
,
741 EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
);
742 ASSERT_EFI_ERROR (Status
);
744 mFlashNvStorageVariableBase
= FixedPcdGet32 (PcdFlashNvStorageVariableBase
);
746 // Set the index of the first LBA for the FVB
747 Instance
->StartLba
= (PcdGet32 (PcdFlashNvStorageVariableBase
) - Instance
->RegionBaseAddress
) / Instance
->Media
.BlockSize
;
749 BootMode
= GetBootModeHob ();
750 if (BootMode
== BOOT_WITH_DEFAULT_SETTINGS
) {
751 Status
= EFI_INVALID_PARAMETER
;
753 // Determine if there is a valid header at the beginning of the NorFlash
754 Status
= ValidateFvHeader (Instance
);
757 // Install the Default FVB header if required
758 if (EFI_ERROR(Status
)) {
759 // There is no valid header, so time to install one.
760 DEBUG ((EFI_D_INFO
, "%a: The FVB Header is not valid.\n", __FUNCTION__
));
761 DEBUG ((EFI_D_INFO
, "%a: Installing a correct one for this volume.\n",
764 // Erase all the NorFlash that is reserved for variable storage
765 FvbNumLba
= (PcdGet32(PcdFlashNvStorageVariableSize
) + PcdGet32(PcdFlashNvStorageFtwWorkingSize
) + PcdGet32(PcdFlashNvStorageFtwSpareSize
)) / Instance
->Media
.BlockSize
;
767 Status
= FvbEraseBlocks (&Instance
->FvbProtocol
, (EFI_LBA
)0, FvbNumLba
, EFI_LBA_LIST_TERMINATOR
);
768 if (EFI_ERROR(Status
)) {
772 // Install all appropriate headers
773 Status
= InitializeFvAndVariableStoreHeaders (Instance
);
774 if (EFI_ERROR(Status
)) {
780 // Register for the virtual address change event
782 Status
= gBS
->CreateEventEx (
785 FvbVirtualNotifyEvent
,
787 &gEfiEventVirtualAddressChangeGuid
,
788 &mFvbVirtualAddrChangeEvent
790 ASSERT_EFI_ERROR (Status
);