1 /*++ @file NorFlashFvbDxe.c
3 Copyright (c) 2011, ARM Ltd. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/PcdLib.h>
17 #include <Library/BaseLib.h>
18 #include <Library/UefiLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/UefiBootServicesTableLib.h>
24 #include <Guid/VariableFormat.h>
25 #include <Guid/SystemNvDataGuid.h>
27 #include "NorFlashDxe.h"
31 /// The Firmware Volume Block Protocol is the low-level interface
32 /// to a firmware volume. File-level access to a firmware volume
33 /// should not be done using the Firmware Volume Block Protocol.
34 /// Normal access to a firmware volume must use the Firmware
35 /// Volume Protocol. Typically, only the file system driver that
36 /// produces the Firmware Volume Protocol will bind to the
37 /// Firmware Volume Block Protocol.
41 Initialises the FV Header and Variable Store Header
42 to support variable operations.
44 @param[in] Ptr - Location to initialise the headers
48 InitializeFvAndVariableStoreHeaders (
49 IN NOR_FLASH_INSTANCE
*Instance
55 EFI_FIRMWARE_VOLUME_HEADER
*FirmwareVolumeHeader
;
56 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
58 if (!Instance
->Initialized
) {
59 Instance
->Initialize(Instance
);
62 HeadersLength
= sizeof(EFI_FIRMWARE_VOLUME_HEADER
) + sizeof(EFI_FV_BLOCK_MAP_ENTRY
) + sizeof(VARIABLE_STORE_HEADER
);
63 Headers
= AllocateZeroPool(HeadersLength
);
66 // EFI_FIRMWARE_VOLUME_HEADER
68 FirmwareVolumeHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)Headers
;
69 CopyGuid (&FirmwareVolumeHeader
->FileSystemGuid
, &gEfiSystemNvDataFvGuid
);
70 FirmwareVolumeHeader
->FvLength
= Instance
->Media
.BlockSize
* (Instance
->Media
.LastBlock
+ 1);
71 FirmwareVolumeHeader
->Signature
= EFI_FVH_SIGNATURE
;
72 FirmwareVolumeHeader
->Attributes
= (EFI_FVB_ATTRIBUTES_2
) (
73 EFI_FVB2_READ_ENABLED_CAP
| // Reads may be enabled
74 EFI_FVB2_READ_STATUS
| // Reads are currently enabled
75 EFI_FVB2_STICKY_WRITE
| // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
76 EFI_FVB2_MEMORY_MAPPED
| // It is memory mapped
77 EFI_FVB2_ERASE_POLARITY
| // After erasure all bits take this value (i.e. '1')
78 EFI_FVB2_WRITE_STATUS
| // Writes are currently enabled
79 EFI_FVB2_WRITE_ENABLED_CAP
// Writes may be enabled
81 FirmwareVolumeHeader
->HeaderLength
= sizeof(EFI_FIRMWARE_VOLUME_HEADER
) + sizeof(EFI_FV_BLOCK_MAP_ENTRY
);
82 FirmwareVolumeHeader
->Revision
= EFI_FVH_REVISION
;
83 FirmwareVolumeHeader
->BlockMap
[0].NumBlocks
= Instance
->Media
.LastBlock
+ 1;
84 FirmwareVolumeHeader
->BlockMap
[0].Length
= Instance
->Media
.BlockSize
;
85 FirmwareVolumeHeader
->BlockMap
[1].NumBlocks
= 0;
86 FirmwareVolumeHeader
->BlockMap
[1].Length
= 0;
87 FirmwareVolumeHeader
->Checksum
= CalculateCheckSum16 ((UINT16
*)FirmwareVolumeHeader
,FirmwareVolumeHeader
->HeaderLength
);
90 // VARIABLE_STORE_HEADER
92 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)((UINT32
)Headers
+ FirmwareVolumeHeader
->HeaderLength
);
93 CopyGuid (&VariableStoreHeader
->Signature
, &gEfiVariableGuid
);
94 VariableStoreHeader
->Size
= PcdGet32(PcdFlashNvStorageVariableSize
) - FirmwareVolumeHeader
->HeaderLength
;
95 VariableStoreHeader
->Format
= VARIABLE_STORE_FORMATTED
;
96 VariableStoreHeader
->State
= VARIABLE_STORE_HEALTHY
;
98 // Install the combined super-header in the NorFlash
99 Status
= FvbWrite(&Instance
->FvbProtocol
, 0, 0, &HeadersLength
, Headers
);
106 Check the integrity of firmware volume header.
108 @param[in] FwVolHeader - A pointer to a firmware volume header
110 @retval EFI_SUCCESS - The firmware volume is consistent
111 @retval EFI_NOT_FOUND - The firmware volume has been corrupted.
116 IN NOR_FLASH_INSTANCE
*Instance
120 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
121 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
122 UINTN VariableStoreLength
;
124 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)Instance
->BaseAddress
;
127 // Verify the header revision, header signature, length
128 // Length of FvBlock cannot be 2**64-1
129 // HeaderLength cannot be an odd number
131 if ( ( FwVolHeader
->Revision
!= EFI_FVH_REVISION
)
132 || ( FwVolHeader
->Signature
!= EFI_FVH_SIGNATURE
)
133 || ( FwVolHeader
->FvLength
!= Instance
->Media
.BlockSize
* (Instance
->Media
.LastBlock
+ 1) )
135 DEBUG ((EFI_D_ERROR
, "ValidateFvHeader: No Firmware Volume header present\n"));
136 return EFI_NOT_FOUND
;
139 // Check the Firmware Volume Guid
140 if( CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiSystemNvDataFvGuid
) == FALSE
) {
141 DEBUG ((EFI_D_ERROR
, "ValidateFvHeader: Firmware Volume Guid non-compatible\n"));
142 return EFI_NOT_FOUND
;
145 // Verify the header checksum
146 Checksum
= CalculateSum16((UINT16
*)FwVolHeader
, FwVolHeader
->HeaderLength
);
148 DEBUG ((EFI_D_ERROR
, "ValidateFvHeader: FV checksum is invalid (Checksum:0x%X)\n",Checksum
));
149 return EFI_NOT_FOUND
;
152 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)((UINT32
)FwVolHeader
+ FwVolHeader
->HeaderLength
);
154 // Check the Variable Store Guid
155 if( CompareGuid (&VariableStoreHeader
->Signature
, &gEfiVariableGuid
) == FALSE
) {
156 DEBUG ((EFI_D_ERROR
, "ValidateFvHeader: Variable Store Guid non-compatible\n"));
157 return EFI_NOT_FOUND
;
160 VariableStoreLength
= PcdGet32 (PcdFlashNvStorageVariableSize
) - FwVolHeader
->HeaderLength
;
161 if (VariableStoreHeader
->Size
!= VariableStoreLength
) {
162 DEBUG ((EFI_D_ERROR
, "ValidateFvHeader: Variable Store Length does not match\n"));
163 return EFI_NOT_FOUND
;
170 The GetAttributes() function retrieves the attributes and
171 current settings of the block.
173 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
175 @param Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the attributes and
176 current settings are returned.
177 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
179 @retval EFI_SUCCESS The firmware volume attributes were returned.
185 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
186 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
189 EFI_FVB_ATTRIBUTES_2 FlashFvbAttributes
;
190 NOR_FLASH_INSTANCE
*Instance
;
192 Instance
= INSTANCE_FROM_FVB_THIS(This
);
194 FlashFvbAttributes
= (EFI_FVB_ATTRIBUTES_2
) (
196 EFI_FVB2_READ_ENABLED_CAP
| // Reads may be enabled
197 EFI_FVB2_READ_STATUS
| // Reads are currently enabled
198 EFI_FVB2_STICKY_WRITE
| // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
199 EFI_FVB2_MEMORY_MAPPED
| // It is memory mapped
200 EFI_FVB2_ERASE_POLARITY
// After erasure all bits take this value (i.e. '1')
204 // Check if it is write protected
205 if (Instance
->Media
.ReadOnly
!= TRUE
) {
207 FlashFvbAttributes
= FlashFvbAttributes
|
208 EFI_FVB2_WRITE_STATUS
| // Writes are currently enabled
209 EFI_FVB2_WRITE_ENABLED_CAP
; // Writes may be enabled
212 *Attributes
= FlashFvbAttributes
;
214 DEBUG ((DEBUG_BLKIO
, "FvbGetAttributes(0x%X)\n", *Attributes
));
220 The SetAttributes() function sets configurable firmware volume attributes
221 and returns the new settings of the firmware volume.
224 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
226 @param Attributes On input, Attributes is a pointer to EFI_FVB_ATTRIBUTES_2
227 that contains the desired firmware volume settings.
228 On successful return, it contains the new settings of
230 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
232 @retval EFI_SUCCESS The firmware volume attributes were returned.
234 @retval EFI_INVALID_PARAMETER The attributes requested are in conflict with the capabilities
235 as declared in the firmware volume header.
241 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
242 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
245 DEBUG ((DEBUG_BLKIO
, "FvbSetAttributes(0x%X) is not supported\n",*Attributes
));
246 return EFI_UNSUPPORTED
;
250 The GetPhysicalAddress() function retrieves the base address of
251 a memory-mapped firmware volume. This function should be called
252 only for memory-mapped firmware volumes.
254 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
256 @param Address Pointer to a caller-allocated
257 EFI_PHYSICAL_ADDRESS that, on successful
258 return from GetPhysicalAddress(), contains the
259 base address of the firmware volume.
261 @retval EFI_SUCCESS The firmware volume base address was returned.
263 @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.
268 FvbGetPhysicalAddress(
269 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
270 OUT EFI_PHYSICAL_ADDRESS
*Address
273 NOR_FLASH_INSTANCE
*Instance
;
275 Instance
= INSTANCE_FROM_FVB_THIS(This
);
277 DEBUG ((DEBUG_BLKIO
, "FvbGetPhysicalAddress(BaseAddress=0x%08x)\n", Instance
->BaseAddress
));
279 ASSERT(Address
!= NULL
);
281 *Address
= Instance
->BaseAddress
;
286 The GetBlockSize() function retrieves the size of the requested
287 block. It also returns the number of additional blocks with
288 the identical size. The GetBlockSize() function is used to
289 retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).
292 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
294 @param Lba Indicates the block for which to return the size.
296 @param BlockSize Pointer to a caller-allocated UINTN in which
297 the size of the block is returned.
299 @param NumberOfBlocks Pointer to a caller-allocated UINTN in
300 which the number of consecutive blocks,
301 starting with Lba, is returned. All
302 blocks in this range have a size of
306 @retval EFI_SUCCESS The firmware volume base address was returned.
308 @retval EFI_INVALID_PARAMETER The requested LBA is out of range.
314 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
316 OUT UINTN
*BlockSize
,
317 OUT UINTN
*NumberOfBlocks
321 NOR_FLASH_INSTANCE
*Instance
;
323 Instance
= INSTANCE_FROM_FVB_THIS(This
);
325 DEBUG ((DEBUG_BLKIO
, "FvbGetBlockSize(Lba=%ld, BlockSize=0x%x, LastBlock=%ld)\n", Lba
, Instance
->Media
.BlockSize
, Instance
->Media
.LastBlock
));
327 if (Lba
> Instance
->Media
.LastBlock
) {
328 DEBUG ((EFI_D_ERROR
, "FvbGetBlockSize: ERROR - Parameter LBA %ld is beyond the last Lba (%ld).\n", Lba
, Instance
->Media
.LastBlock
));
329 Status
= EFI_INVALID_PARAMETER
;
331 // This is easy because in this platform each NorFlash device has equal sized blocks.
332 *BlockSize
= (UINTN
) Instance
->Media
.BlockSize
;
333 *NumberOfBlocks
= (UINTN
) (Instance
->Media
.LastBlock
- Lba
+ 1);
335 DEBUG ((DEBUG_BLKIO
, "FvbGetBlockSize: *BlockSize=0x%x, *NumberOfBlocks=0x%x.\n", *BlockSize
, *NumberOfBlocks
));
337 Status
= EFI_SUCCESS
;
344 Reads the specified number of bytes into a buffer from the specified block.
346 The Read() function reads the requested number of bytes from the
347 requested block and stores them in the provided buffer.
348 Implementations should be mindful that the firmware volume
349 might be in the ReadDisabled state. If it is in this state,
350 the Read() function must return the status code
351 EFI_ACCESS_DENIED without modifying the contents of the
352 buffer. The Read() function must also prevent spanning block
353 boundaries. If a read is requested that would span a block
354 boundary, the read must read up to the boundary but not
355 beyond. The output parameter NumBytes must be set to correctly
356 indicate the number of bytes actually read. The caller must be
357 aware that a read may be partially completed.
359 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
361 @param Lba The starting logical block index from which to read.
363 @param Offset Offset into the block at which to begin reading.
365 @param NumBytes Pointer to a UINTN.
366 At entry, *NumBytes contains the total size of the buffer.
367 At exit, *NumBytes contains the total number of bytes read.
369 @param Buffer Pointer to a caller-allocated buffer that will be used
370 to hold the data that is read.
372 @retval EFI_SUCCESS The firmware volume was read successfully, and contents are
375 @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.
376 On output, NumBytes contains the total number of bytes
379 @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state.
381 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be read.
387 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
390 IN OUT UINTN
*NumBytes
,
395 EFI_STATUS TempStatus
;
398 NOR_FLASH_INSTANCE
*Instance
;
400 Instance
= INSTANCE_FROM_FVB_THIS(This
);
402 DEBUG ((DEBUG_BLKIO
, "FvbRead(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Lba
, Offset
, *NumBytes
, Buffer
));
404 if (!Instance
->Initialized
) {
405 Instance
->Initialize(Instance
);
408 Status
= EFI_SUCCESS
;
411 // Cache the block size to avoid de-referencing pointers all the time
412 BlockSize
= Instance
->Media
.BlockSize
;
414 DEBUG ((DEBUG_BLKIO
, "FvbRead: Check if (Offset=0x%x + NumBytes=0x%x) <= BlockSize=0x%x\n", Offset
, *NumBytes
, BlockSize
));
416 // The read must not span block boundaries.
417 // We need to check each variable individually because adding two large values together overflows.
418 if ( ( Offset
>= BlockSize
) ||
419 ( *NumBytes
> BlockSize
) ||
420 ( (Offset
+ *NumBytes
) > BlockSize
) ) {
421 DEBUG ((EFI_D_ERROR
, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset
, *NumBytes
, BlockSize
));
422 return EFI_BAD_BUFFER_SIZE
;
425 // We must have some bytes to read
426 if (*NumBytes
== 0) {
427 return EFI_BAD_BUFFER_SIZE
;
430 // FixMe: Allow an arbitrary number of bytes to be read out, not just a multiple of block size.
432 // Allocate runtime memory to read in the NOR Flash data. Variable Services are runtime.
433 BlockBuffer
= AllocateRuntimePool(BlockSize
);
435 // Check if the memory allocation was successful
436 if( BlockBuffer
== NULL
) {
437 DEBUG ((EFI_D_ERROR
, "FvbRead: ERROR - Could not allocate BlockBuffer @ 0x%08x.\n", BlockBuffer
));
438 return EFI_DEVICE_ERROR
;
441 // Read NOR Flash data into shadow buffer
442 TempStatus
= NorFlashReadBlocks(Instance
, Lba
, BlockSize
, BlockBuffer
);
443 if (EFI_ERROR (TempStatus
)) {
444 // Return one of the pre-approved error statuses
445 Status
= EFI_DEVICE_ERROR
;
449 // Put the data at the appropriate location inside the buffer area
450 DEBUG ((DEBUG_BLKIO
, "FvbRead: CopyMem( Dst=0x%08x, Src=0x%08x, Size=0x%x ).\n", Buffer
, BlockBuffer
+ Offset
, *NumBytes
));
452 CopyMem(Buffer
, BlockBuffer
+ Offset
, *NumBytes
);
455 FreePool(BlockBuffer
);
457 DEBUG ((DEBUG_BLKIO
, "FvbRead - end\n"));
462 Writes the specified number of bytes from the input buffer to the block.
464 The Write() function writes the specified number of bytes from
465 the provided buffer to the specified block and offset. If the
466 firmware volume is sticky write, the caller must ensure that
467 all the bits of the specified range to write are in the
468 EFI_FVB_ERASE_POLARITY state before calling the Write()
469 function, or else the result will be unpredictable. This
470 unpredictability arises because, for a sticky-write firmware
471 volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY
472 state but cannot flip it back again. Before calling the
473 Write() function, it is recommended for the caller to first call
474 the EraseBlocks() function to erase the specified block to
475 write. A block erase cycle will transition bits from the
476 (NOT)EFI_FVB_ERASE_POLARITY state back to the
477 EFI_FVB_ERASE_POLARITY state. Implementations should be
478 mindful that the firmware volume might be in the WriteDisabled
479 state. If it is in this state, the Write() function must
480 return the status code EFI_ACCESS_DENIED without modifying the
481 contents of the firmware volume. The Write() function must
482 also prevent spanning block boundaries. If a write is
483 requested that spans a block boundary, the write must store up
484 to the boundary but not beyond. The output parameter NumBytes
485 must be set to correctly indicate the number of bytes actually
486 written. The caller must be aware that a write may be
487 partially completed. All writes, partial or otherwise, must be
488 fully flushed to the hardware before the Write() service
491 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
493 @param Lba The starting logical block index to write to.
495 @param Offset Offset into the block at which to begin writing.
497 @param NumBytes The pointer to a UINTN.
498 At entry, *NumBytes contains the total size of the buffer.
499 At exit, *NumBytes contains the total number of bytes actually written.
501 @param Buffer The pointer to a caller-allocated buffer that contains the source for the write.
503 @retval EFI_SUCCESS The firmware volume was written successfully.
505 @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
506 On output, NumBytes contains the total number of bytes
509 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
511 @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not be written.
518 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
521 IN OUT UINTN
*NumBytes
,
526 EFI_STATUS TempStatus
;
529 NOR_FLASH_INSTANCE
*Instance
;
531 Instance
= INSTANCE_FROM_FVB_THIS(This
);
533 if (!Instance
->Initialized
) {
534 Instance
->Initialize(Instance
);
537 DEBUG ((DEBUG_BLKIO
, "FvbWrite(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Lba
, Offset
, *NumBytes
, Buffer
));
539 Status
= EFI_SUCCESS
;
542 // Detect WriteDisabled state
543 if (Instance
->Media
.ReadOnly
== TRUE
) {
544 DEBUG ((EFI_D_ERROR
, "FvbWrite: ERROR - Can not write: Device is in WriteDisabled state.\n"));
545 // It is in WriteDisabled state, return an error right away
546 return EFI_ACCESS_DENIED
;
549 // Cache the block size to avoid de-referencing pointers all the time
550 BlockSize
= Instance
->Media
.BlockSize
;
552 // The write must not span block boundaries.
553 // We need to check each variable individually because adding two large values together overflows.
554 if ( ( Offset
>= BlockSize
) ||
555 ( *NumBytes
> BlockSize
) ||
556 ( (Offset
+ *NumBytes
) > BlockSize
) ) {
557 DEBUG ((EFI_D_ERROR
, "FvbWrite: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset
, *NumBytes
, BlockSize
));
558 return EFI_BAD_BUFFER_SIZE
;
561 // We must have some bytes to write
562 if (*NumBytes
== 0) {
563 DEBUG ((EFI_D_ERROR
, "FvbWrite: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset
, *NumBytes
, BlockSize
));
564 return EFI_BAD_BUFFER_SIZE
;
567 // Allocate runtime memory to read in the NOR Flash data.
568 // Since the intention is to use this with Variable Services and since these are runtime,
569 // allocate the memory from the runtime pool.
570 BlockBuffer
= AllocateRuntimePool(BlockSize
);
572 // Check we did get some memory
573 if( BlockBuffer
== NULL
) {
574 DEBUG ((EFI_D_ERROR
, "FvbWrite: ERROR - Can not allocate BlockBuffer @ 0x%08x.\n", BlockBuffer
));
575 return EFI_DEVICE_ERROR
;
578 // Read NOR Flash data into shadow buffer
579 TempStatus
= NorFlashReadBlocks(Instance
, Lba
, BlockSize
, BlockBuffer
);
580 if (EFI_ERROR (TempStatus
)) {
581 // Return one of the pre-approved error statuses
582 Status
= EFI_DEVICE_ERROR
;
586 // Put the data at the appropriate location inside the buffer area
587 CopyMem((BlockBuffer
+ Offset
), Buffer
, *NumBytes
);
589 // Write the modified buffer back to the NorFlash
590 Status
= NorFlashWriteBlocks(Instance
, Lba
, BlockSize
, BlockBuffer
);
591 if (EFI_ERROR (TempStatus
)) {
592 // Return one of the pre-approved error statuses
593 Status
= EFI_DEVICE_ERROR
;
598 FreePool(BlockBuffer
);
603 Erases and initialises a firmware volume block.
605 The EraseBlocks() function erases one or more blocks as denoted
606 by the variable argument list. The entire parameter list of
607 blocks must be verified before erasing any blocks. If a block is
608 requested that does not exist within the associated firmware
609 volume (it has a larger index than the last block of the
610 firmware volume), the EraseBlocks() function must return the
611 status code EFI_INVALID_PARAMETER without modifying the contents
612 of the firmware volume. Implementations should be mindful that
613 the firmware volume might be in the WriteDisabled state. If it
614 is in this state, the EraseBlocks() function must return the
615 status code EFI_ACCESS_DENIED without modifying the contents of
616 the firmware volume. All calls to EraseBlocks() must be fully
617 flushed to the hardware before the EraseBlocks() service
620 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
623 @param ... The variable argument list is a list of tuples.
624 Each tuple describes a range of LBAs to erase
625 and consists of the following:
626 - An EFI_LBA that indicates the starting LBA
627 - A UINTN that indicates the number of blocks to erase.
629 The list is terminated with an EFI_LBA_LIST_TERMINATOR.
630 For example, the following indicates that two ranges of blocks
631 (5-7 and 10-11) are to be erased:
632 EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);
634 @retval EFI_SUCCESS The erase request successfully completed.
636 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
638 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be written.
639 The firmware device may have been partially erased.
641 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the variable argument list do
642 not exist in the firmware volume.
648 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
654 UINTN BlockAddress
; // Physical address of Lba to erase
655 EFI_LBA StartingLba
; // Lba from which we start erasing
656 UINTN NumOfLba
; // Number of Lba blocks to erase
657 NOR_FLASH_INSTANCE
*Instance
;
659 Instance
= INSTANCE_FROM_FVB_THIS(This
);
661 DEBUG ((DEBUG_BLKIO
, "FvbEraseBlocks()\n"));
663 Status
= EFI_SUCCESS
;
665 // Detect WriteDisabled state
666 if (Instance
->Media
.ReadOnly
== TRUE
) {
667 // Firmware volume is in WriteDisabled state
668 DEBUG ((EFI_D_ERROR
, "FvbEraseBlocks: ERROR - Device is in WriteDisabled state.\n"));
669 return EFI_ACCESS_DENIED
;
672 // Before erasing, check the entire list of parameters to ensure all specified blocks are valid
674 VA_START (args
, This
);
678 // Get the Lba from which we start erasing
679 StartingLba
= VA_ARG (args
, EFI_LBA
);
681 // Have we reached the end of the list?
682 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
683 //Exit the while loop
687 // How many Lba blocks are we requested to erase?
688 NumOfLba
= VA_ARG (args
, UINT32
);
690 // All blocks must be within range
691 DEBUG ((DEBUG_BLKIO
, "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%d - 1 ) > LastBlock=%ld.\n", StartingLba
, NumOfLba
, Instance
->Media
.LastBlock
));
692 if ((NumOfLba
== 0) || ((StartingLba
+ NumOfLba
- 1) > Instance
->Media
.LastBlock
)) {
694 DEBUG ((DEBUG_BLKIO
, "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%d - 1 ) > LastBlock=%ld.\n", StartingLba
, NumOfLba
, Instance
->Media
.LastBlock
));
695 DEBUG ((EFI_D_ERROR
, "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n"));
696 Status
= EFI_INVALID_PARAMETER
;
704 // To get here, all must be ok, so start erasing
706 VA_START (args
, This
);
710 // Get the Lba from which we start erasing
711 StartingLba
= VA_ARG (args
, EFI_LBA
);
713 // Have we reached the end of the list?
714 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
715 // Exit the while loop
719 // How many Lba blocks are we requested to erase?
720 NumOfLba
= VA_ARG (args
, UINT32
);
722 // Go through each one and erase it
723 while (NumOfLba
> 0) {
725 // Get the physical address of Lba to erase
726 BlockAddress
= GET_NOR_BLOCK_ADDRESS (
727 Instance
->BaseAddress
,
729 Instance
->Media
.BlockSize
733 DEBUG ((DEBUG_BLKIO
, "FvbEraseBlocks: Erasing Lba=%ld @ 0x%08x.\n", StartingLba
, BlockAddress
));
734 Status
= NorFlashUnlockAndEraseSingleBlock (BlockAddress
);
735 if (EFI_ERROR(Status
)) {
737 Status
= EFI_DEVICE_ERROR
;
741 // Move to the next Lba
756 NorFlashFvbInitialize (
757 IN NOR_FLASH_INSTANCE
* Instance
761 DEBUG((DEBUG_BLKIO
,"NorFlashFvbInitialize\n"));
763 Status
= NorFlashBlkIoInitialize(Instance
);
764 if (EFI_ERROR(Status
)) {
765 DEBUG((EFI_D_ERROR
,"NorFlashFvbInitialize: ERROR - Failed to initialize FVB\n"));
768 Instance
->Initialized
= TRUE
;
770 // Determine if there is a valid header at the beginning of the NorFlash
771 Status
= ValidateFvHeader (Instance
);
772 if (EFI_ERROR(Status
)) {
773 // There is no valid header, so time to install one.
774 DEBUG((EFI_D_ERROR
,"NorFlashFvbInitialize: ERROR - The FVB Header is not valid. Installing a correct one for this volume.\n"));
776 // Erase all the NorFlash that is reserved for variable storage
777 Status
= FvbEraseBlocks ( &Instance
->FvbProtocol
, (EFI_LBA
)0, (UINT32
)(Instance
->Media
.LastBlock
+ 1), EFI_LBA_LIST_TERMINATOR
);
778 if (EFI_ERROR(Status
)) {
782 // Install all appropriate headers
783 InitializeFvAndVariableStoreHeaders ( Instance
);
784 if (EFI_ERROR(Status
)) {