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/BaseMemoryLib.h>
20 #include <Library/MemoryAllocationLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
23 #include <Guid/VariableFormat.h>
24 #include <Guid/SystemNvDataGuid.h>
26 #include "NorFlashDxe.h"
30 /// The Firmware Volume Block Protocol is the low-level interface
31 /// to a firmware volume. File-level access to a firmware volume
32 /// should not be done using the Firmware Volume Block Protocol.
33 /// Normal access to a firmware volume must use the Firmware
34 /// Volume Protocol. Typically, only the file system driver that
35 /// produces the Firmware Volume Protocol will bind to the
36 /// Firmware Volume Block Protocol.
40 Initialises the FV Header and Variable Store Header
41 to support variable operations.
43 @param[in] Ptr - Location to initialise the headers
47 InitializeFvAndVariableStoreHeaders (
48 IN NOR_FLASH_INSTANCE
*Instance
54 EFI_FIRMWARE_VOLUME_HEADER
*FirmwareVolumeHeader
;
55 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
57 if (!Instance
->Initialized
) {
58 Instance
->Initialize(Instance
);
61 HeadersLength
= sizeof(EFI_FIRMWARE_VOLUME_HEADER
) + sizeof(EFI_FV_BLOCK_MAP_ENTRY
) + sizeof(VARIABLE_STORE_HEADER
);
62 Headers
= AllocateZeroPool(HeadersLength
);
64 // FirmwareVolumeHeader->FvLength is declared to have the Variable area AND the FTW working area AND the FTW Spare contiguous.
65 ASSERT(PcdGet32(PcdFlashNvStorageVariableBase
) + PcdGet32(PcdFlashNvStorageVariableSize
) == PcdGet32(PcdFlashNvStorageFtwWorkingBase
));
66 ASSERT(PcdGet32(PcdFlashNvStorageFtwWorkingBase
) + PcdGet32(PcdFlashNvStorageFtwWorkingSize
) == PcdGet32(PcdFlashNvStorageFtwSpareBase
));
68 // Check if the size of the area is at least one block size
69 ASSERT((PcdGet32(PcdFlashNvStorageVariableSize
) > 0) && (PcdGet32(PcdFlashNvStorageVariableSize
) / Instance
->Media
.BlockSize
> 0));
70 ASSERT((PcdGet32(PcdFlashNvStorageFtwWorkingSize
) > 0) && (PcdGet32(PcdFlashNvStorageFtwWorkingSize
) / Instance
->Media
.BlockSize
> 0));
71 ASSERT((PcdGet32(PcdFlashNvStorageFtwSpareSize
) > 0) && (PcdGet32(PcdFlashNvStorageFtwSpareSize
) / Instance
->Media
.BlockSize
> 0));
73 // Ensure the Variable area Base Addresses are aligned on a block size boundaries
74 ASSERT(PcdGet32(PcdFlashNvStorageVariableBase
) % Instance
->Media
.BlockSize
== 0);
75 ASSERT(PcdGet32(PcdFlashNvStorageFtwWorkingBase
) % Instance
->Media
.BlockSize
== 0);
76 ASSERT(PcdGet32(PcdFlashNvStorageFtwSpareBase
) % Instance
->Media
.BlockSize
== 0);
79 // EFI_FIRMWARE_VOLUME_HEADER
81 FirmwareVolumeHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)Headers
;
82 CopyGuid (&FirmwareVolumeHeader
->FileSystemGuid
, &gEfiSystemNvDataFvGuid
);
83 FirmwareVolumeHeader
->FvLength
=
84 PcdGet32(PcdFlashNvStorageVariableSize
) +
85 PcdGet32(PcdFlashNvStorageFtwWorkingSize
) +
86 PcdGet32(PcdFlashNvStorageFtwSpareSize
);
87 FirmwareVolumeHeader
->Signature
= EFI_FVH_SIGNATURE
;
88 FirmwareVolumeHeader
->Attributes
= (EFI_FVB_ATTRIBUTES_2
) (
89 EFI_FVB2_READ_ENABLED_CAP
| // Reads may be enabled
90 EFI_FVB2_READ_STATUS
| // Reads are currently enabled
91 EFI_FVB2_STICKY_WRITE
| // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
92 EFI_FVB2_MEMORY_MAPPED
| // It is memory mapped
93 EFI_FVB2_ERASE_POLARITY
| // After erasure all bits take this value (i.e. '1')
94 EFI_FVB2_WRITE_STATUS
| // Writes are currently enabled
95 EFI_FVB2_WRITE_ENABLED_CAP
// Writes may be enabled
97 FirmwareVolumeHeader
->HeaderLength
= sizeof(EFI_FIRMWARE_VOLUME_HEADER
) + sizeof(EFI_FV_BLOCK_MAP_ENTRY
);
98 FirmwareVolumeHeader
->Revision
= EFI_FVH_REVISION
;
99 FirmwareVolumeHeader
->BlockMap
[0].NumBlocks
= Instance
->Media
.LastBlock
+ 1;
100 FirmwareVolumeHeader
->BlockMap
[0].Length
= Instance
->Media
.BlockSize
;
101 FirmwareVolumeHeader
->BlockMap
[1].NumBlocks
= 0;
102 FirmwareVolumeHeader
->BlockMap
[1].Length
= 0;
103 FirmwareVolumeHeader
->Checksum
= CalculateCheckSum16 ((UINT16
*)FirmwareVolumeHeader
,FirmwareVolumeHeader
->HeaderLength
);
106 // VARIABLE_STORE_HEADER
108 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)((UINT32
)Headers
+ FirmwareVolumeHeader
->HeaderLength
);
109 CopyGuid (&VariableStoreHeader
->Signature
, &gEfiVariableGuid
);
110 VariableStoreHeader
->Size
= PcdGet32(PcdFlashNvStorageVariableSize
) - FirmwareVolumeHeader
->HeaderLength
;
111 VariableStoreHeader
->Format
= VARIABLE_STORE_FORMATTED
;
112 VariableStoreHeader
->State
= VARIABLE_STORE_HEALTHY
;
114 // Install the combined super-header in the NorFlash
115 Status
= FvbWrite (&Instance
->FvbProtocol
, 0, 0, &HeadersLength
, Headers
);
122 Check the integrity of firmware volume header.
124 @param[in] FwVolHeader - A pointer to a firmware volume header
126 @retval EFI_SUCCESS - The firmware volume is consistent
127 @retval EFI_NOT_FOUND - The firmware volume has been corrupted.
132 IN NOR_FLASH_INSTANCE
*Instance
136 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
137 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
138 UINTN VariableStoreLength
;
141 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)Instance
->BaseAddress
;
143 FvLength
= PcdGet32(PcdFlashNvStorageVariableSize
) + PcdGet32(PcdFlashNvStorageFtwWorkingSize
) +
144 PcdGet32(PcdFlashNvStorageFtwSpareSize
);
147 // Verify the header revision, header signature, length
148 // Length of FvBlock cannot be 2**64-1
149 // HeaderLength cannot be an odd number
151 if ( (FwVolHeader
->Revision
!= EFI_FVH_REVISION
)
152 || (FwVolHeader
->Signature
!= EFI_FVH_SIGNATURE
)
153 || (FwVolHeader
->FvLength
!= FvLength
)
156 DEBUG ((EFI_D_ERROR
, "ValidateFvHeader: No Firmware Volume header present\n"));
157 return EFI_NOT_FOUND
;
160 // Check the Firmware Volume Guid
161 if( CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiSystemNvDataFvGuid
) == FALSE
) {
162 DEBUG ((EFI_D_ERROR
, "ValidateFvHeader: Firmware Volume Guid non-compatible\n"));
163 return EFI_NOT_FOUND
;
166 // Verify the header checksum
167 Checksum
= CalculateSum16((UINT16
*)FwVolHeader
, FwVolHeader
->HeaderLength
);
169 DEBUG ((EFI_D_ERROR
, "ValidateFvHeader: FV checksum is invalid (Checksum:0x%X)\n",Checksum
));
170 return EFI_NOT_FOUND
;
173 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)((UINT32
)FwVolHeader
+ FwVolHeader
->HeaderLength
);
175 // Check the Variable Store Guid
176 if( CompareGuid (&VariableStoreHeader
->Signature
, &gEfiVariableGuid
) == FALSE
) {
177 DEBUG ((EFI_D_ERROR
, "ValidateFvHeader: Variable Store Guid non-compatible\n"));
178 return EFI_NOT_FOUND
;
181 VariableStoreLength
= PcdGet32 (PcdFlashNvStorageVariableSize
) - FwVolHeader
->HeaderLength
;
182 if (VariableStoreHeader
->Size
!= VariableStoreLength
) {
183 DEBUG ((EFI_D_ERROR
, "ValidateFvHeader: Variable Store Length does not match\n"));
184 return EFI_NOT_FOUND
;
191 The GetAttributes() function retrieves the attributes and
192 current settings of the block.
194 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
196 @param Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the attributes and
197 current settings are returned.
198 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
200 @retval EFI_SUCCESS The firmware volume attributes were returned.
206 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
207 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
210 EFI_FVB_ATTRIBUTES_2 FlashFvbAttributes
;
211 NOR_FLASH_INSTANCE
*Instance
;
213 Instance
= INSTANCE_FROM_FVB_THIS(This
);
215 FlashFvbAttributes
= (EFI_FVB_ATTRIBUTES_2
) (
217 EFI_FVB2_READ_ENABLED_CAP
| // Reads may be enabled
218 EFI_FVB2_READ_STATUS
| // Reads are currently enabled
219 EFI_FVB2_STICKY_WRITE
| // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
220 EFI_FVB2_MEMORY_MAPPED
| // It is memory mapped
221 EFI_FVB2_ERASE_POLARITY
// After erasure all bits take this value (i.e. '1')
225 // Check if it is write protected
226 if (Instance
->Media
.ReadOnly
!= TRUE
) {
228 FlashFvbAttributes
= FlashFvbAttributes
|
229 EFI_FVB2_WRITE_STATUS
| // Writes are currently enabled
230 EFI_FVB2_WRITE_ENABLED_CAP
; // Writes may be enabled
233 *Attributes
= FlashFvbAttributes
;
235 DEBUG ((DEBUG_BLKIO
, "FvbGetAttributes(0x%X)\n", *Attributes
));
241 The SetAttributes() function sets configurable firmware volume attributes
242 and returns the new settings of the firmware volume.
245 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
247 @param Attributes On input, Attributes is a pointer to EFI_FVB_ATTRIBUTES_2
248 that contains the desired firmware volume settings.
249 On successful return, it contains the new settings of
251 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
253 @retval EFI_SUCCESS The firmware volume attributes were returned.
255 @retval EFI_INVALID_PARAMETER The attributes requested are in conflict with the capabilities
256 as declared in the firmware volume header.
262 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
263 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
266 DEBUG ((DEBUG_BLKIO
, "FvbSetAttributes(0x%X) is not supported\n",*Attributes
));
267 return EFI_UNSUPPORTED
;
271 The GetPhysicalAddress() function retrieves the base address of
272 a memory-mapped firmware volume. This function should be called
273 only for memory-mapped firmware volumes.
275 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
277 @param Address Pointer to a caller-allocated
278 EFI_PHYSICAL_ADDRESS that, on successful
279 return from GetPhysicalAddress(), contains the
280 base address of the firmware volume.
282 @retval EFI_SUCCESS The firmware volume base address was returned.
284 @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.
289 FvbGetPhysicalAddress(
290 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
291 OUT EFI_PHYSICAL_ADDRESS
*Address
294 NOR_FLASH_INSTANCE
*Instance
;
296 Instance
= INSTANCE_FROM_FVB_THIS(This
);
298 DEBUG ((DEBUG_BLKIO
, "FvbGetPhysicalAddress(BaseAddress=0x%08x)\n", Instance
->BaseAddress
));
300 ASSERT(Address
!= NULL
);
302 *Address
= PcdGet32 (PcdFlashNvStorageVariableBase
);
307 The GetBlockSize() function retrieves the size of the requested
308 block. It also returns the number of additional blocks with
309 the identical size. The GetBlockSize() function is used to
310 retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).
313 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
315 @param Lba Indicates the block for which to return the size.
317 @param BlockSize Pointer to a caller-allocated UINTN in which
318 the size of the block is returned.
320 @param NumberOfBlocks Pointer to a caller-allocated UINTN in
321 which the number of consecutive blocks,
322 starting with Lba, is returned. All
323 blocks in this range have a size of
327 @retval EFI_SUCCESS The firmware volume base address was returned.
329 @retval EFI_INVALID_PARAMETER The requested LBA is out of range.
335 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
337 OUT UINTN
*BlockSize
,
338 OUT UINTN
*NumberOfBlocks
342 NOR_FLASH_INSTANCE
*Instance
;
344 Instance
= INSTANCE_FROM_FVB_THIS(This
);
346 DEBUG ((DEBUG_BLKIO
, "FvbGetBlockSize(Lba=%ld, BlockSize=0x%x, LastBlock=%ld)\n", Lba
, Instance
->Media
.BlockSize
, Instance
->Media
.LastBlock
));
348 if (Lba
> Instance
->Media
.LastBlock
) {
349 DEBUG ((EFI_D_ERROR
, "FvbGetBlockSize: ERROR - Parameter LBA %ld is beyond the last Lba (%ld).\n", Lba
, Instance
->Media
.LastBlock
));
350 Status
= EFI_INVALID_PARAMETER
;
352 // This is easy because in this platform each NorFlash device has equal sized blocks.
353 *BlockSize
= (UINTN
) Instance
->Media
.BlockSize
;
354 *NumberOfBlocks
= (UINTN
) (Instance
->Media
.LastBlock
- Lba
+ 1);
356 DEBUG ((DEBUG_BLKIO
, "FvbGetBlockSize: *BlockSize=0x%x, *NumberOfBlocks=0x%x.\n", *BlockSize
, *NumberOfBlocks
));
358 Status
= EFI_SUCCESS
;
365 Reads the specified number of bytes into a buffer from the specified block.
367 The Read() function reads the requested number of bytes from the
368 requested block and stores them in the provided buffer.
369 Implementations should be mindful that the firmware volume
370 might be in the ReadDisabled state. If it is in this state,
371 the Read() function must return the status code
372 EFI_ACCESS_DENIED without modifying the contents of the
373 buffer. The Read() function must also prevent spanning block
374 boundaries. If a read is requested that would span a block
375 boundary, the read must read up to the boundary but not
376 beyond. The output parameter NumBytes must be set to correctly
377 indicate the number of bytes actually read. The caller must be
378 aware that a read may be partially completed.
380 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
382 @param Lba The starting logical block index from which to read.
384 @param Offset Offset into the block at which to begin reading.
386 @param NumBytes Pointer to a UINTN.
387 At entry, *NumBytes contains the total size of the buffer.
388 At exit, *NumBytes contains the total number of bytes read.
390 @param Buffer Pointer to a caller-allocated buffer that will be used
391 to hold the data that is read.
393 @retval EFI_SUCCESS The firmware volume was read successfully, and contents are
396 @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.
397 On output, NumBytes contains the total number of bytes
400 @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state.
402 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be read.
408 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
411 IN OUT UINTN
*NumBytes
,
416 EFI_STATUS TempStatus
;
419 NOR_FLASH_INSTANCE
*Instance
;
421 Instance
= INSTANCE_FROM_FVB_THIS(This
);
423 DEBUG ((DEBUG_BLKIO
, "FvbRead(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Instance
->StartLba
+ Lba
, Offset
, *NumBytes
, Buffer
));
425 if (!Instance
->Initialized
) {
426 Instance
->Initialize(Instance
);
429 Status
= 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 // FixMe: Allow an arbitrary number of bytes to be read out, not just a multiple of block size.
453 // Allocate runtime memory to read in the NOR Flash data. Variable Services are runtime.
454 BlockBuffer
= AllocateRuntimePool(BlockSize
);
456 // Check if the memory allocation was successful
457 if (BlockBuffer
== NULL
) {
458 DEBUG ((EFI_D_ERROR
, "FvbRead: ERROR - Could not allocate BlockBuffer @ 0x%08x.\n", BlockBuffer
));
459 return EFI_DEVICE_ERROR
;
462 // Read NOR Flash data into shadow buffer
463 TempStatus
= NorFlashReadBlocks (Instance
, Instance
->StartLba
+ Lba
, BlockSize
, BlockBuffer
);
464 if (EFI_ERROR (TempStatus
)) {
465 // Return one of the pre-approved error statuses
466 Status
= EFI_DEVICE_ERROR
;
470 // Put the data at the appropriate location inside the buffer area
471 DEBUG ((DEBUG_BLKIO
, "FvbRead: CopyMem( Dst=0x%08x, Src=0x%08x, Size=0x%x ).\n", Buffer
, BlockBuffer
+ Offset
, *NumBytes
));
473 CopyMem(Buffer
, BlockBuffer
+ Offset
, *NumBytes
);
476 FreePool(BlockBuffer
);
481 Writes the specified number of bytes from the input buffer to the block.
483 The Write() function writes the specified number of bytes from
484 the provided buffer to the specified block and offset. If the
485 firmware volume is sticky write, the caller must ensure that
486 all the bits of the specified range to write are in the
487 EFI_FVB_ERASE_POLARITY state before calling the Write()
488 function, or else the result will be unpredictable. This
489 unpredictability arises because, for a sticky-write firmware
490 volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY
491 state but cannot flip it back again. Before calling the
492 Write() function, it is recommended for the caller to first call
493 the EraseBlocks() function to erase the specified block to
494 write. A block erase cycle will transition bits from the
495 (NOT)EFI_FVB_ERASE_POLARITY state back to the
496 EFI_FVB_ERASE_POLARITY state. Implementations should be
497 mindful that the firmware volume might be in the WriteDisabled
498 state. If it is in this state, the Write() function must
499 return the status code EFI_ACCESS_DENIED without modifying the
500 contents of the firmware volume. The Write() function must
501 also prevent spanning block boundaries. If a write is
502 requested that spans a block boundary, the write must store up
503 to the boundary but not beyond. The output parameter NumBytes
504 must be set to correctly indicate the number of bytes actually
505 written. The caller must be aware that a write may be
506 partially completed. All writes, partial or otherwise, must be
507 fully flushed to the hardware before the Write() service
510 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
512 @param Lba The starting logical block index to write to.
514 @param Offset Offset into the block at which to begin writing.
516 @param NumBytes The pointer to a UINTN.
517 At entry, *NumBytes contains the total size of the buffer.
518 At exit, *NumBytes contains the total number of bytes actually written.
520 @param Buffer The pointer to a caller-allocated buffer that contains the source for the write.
522 @retval EFI_SUCCESS The firmware volume was written successfully.
524 @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
525 On output, NumBytes contains the total number of bytes
528 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
530 @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not be written.
537 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
540 IN OUT UINTN
*NumBytes
,
545 EFI_STATUS TempStatus
;
548 NOR_FLASH_INSTANCE
*Instance
;
550 Instance
= INSTANCE_FROM_FVB_THIS(This
);
552 if (!Instance
->Initialized
) {
553 Instance
->Initialize(Instance
);
556 DEBUG ((DEBUG_BLKIO
, "FvbWrite(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Instance
->StartLba
+ Lba
, Offset
, *NumBytes
, Buffer
));
558 Status
= EFI_SUCCESS
;
561 // Detect WriteDisabled state
562 if (Instance
->Media
.ReadOnly
== TRUE
) {
563 DEBUG ((EFI_D_ERROR
, "FvbWrite: ERROR - Can not write: Device is in WriteDisabled state.\n"));
564 // It is in WriteDisabled state, return an error right away
565 return EFI_ACCESS_DENIED
;
568 // Cache the block size to avoid de-referencing pointers all the time
569 BlockSize
= Instance
->Media
.BlockSize
;
571 // The write must not span block boundaries.
572 // We need to check each variable individually because adding two large values together overflows.
573 if ( ( Offset
>= BlockSize
) ||
574 ( *NumBytes
> BlockSize
) ||
575 ( (Offset
+ *NumBytes
) > BlockSize
) ) {
576 DEBUG ((EFI_D_ERROR
, "FvbWrite: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset
, *NumBytes
, BlockSize
));
577 return EFI_BAD_BUFFER_SIZE
;
580 // We must have some bytes to write
581 if (*NumBytes
== 0) {
582 DEBUG ((EFI_D_ERROR
, "FvbWrite: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset
, *NumBytes
, BlockSize
));
583 return EFI_BAD_BUFFER_SIZE
;
586 // Allocate runtime memory to read in the NOR Flash data.
587 // Since the intention is to use this with Variable Services and since these are runtime,
588 // allocate the memory from the runtime pool.
589 BlockBuffer
= AllocateRuntimePool(BlockSize
);
591 // Check we did get some memory
592 if( BlockBuffer
== NULL
) {
593 DEBUG ((EFI_D_ERROR
, "FvbWrite: ERROR - Can not allocate BlockBuffer @ 0x%08x.\n", BlockBuffer
));
594 return EFI_DEVICE_ERROR
;
597 // Read NOR Flash data into shadow buffer
598 TempStatus
= NorFlashReadBlocks(Instance
, Instance
->StartLba
+ Lba
, BlockSize
, BlockBuffer
);
599 if (EFI_ERROR (TempStatus
)) {
600 // Return one of the pre-approved error statuses
601 Status
= EFI_DEVICE_ERROR
;
605 // Put the data at the appropriate location inside the buffer area
606 CopyMem((BlockBuffer
+ Offset
), Buffer
, *NumBytes
);
608 // Write the modified buffer back to the NorFlash
609 Status
= NorFlashWriteBlocks(Instance
, Instance
->StartLba
+ Lba
, BlockSize
, BlockBuffer
);
610 if (EFI_ERROR (TempStatus
)) {
611 // Return one of the pre-approved error statuses
612 Status
= EFI_DEVICE_ERROR
;
617 FreePool(BlockBuffer
);
622 Erases and initialises a firmware volume block.
624 The EraseBlocks() function erases one or more blocks as denoted
625 by the variable argument list. The entire parameter list of
626 blocks must be verified before erasing any blocks. If a block is
627 requested that does not exist within the associated firmware
628 volume (it has a larger index than the last block of the
629 firmware volume), the EraseBlocks() function must return the
630 status code EFI_INVALID_PARAMETER without modifying the contents
631 of the firmware volume. Implementations should be mindful that
632 the firmware volume might be in the WriteDisabled state. If it
633 is in this state, the EraseBlocks() function must return the
634 status code EFI_ACCESS_DENIED without modifying the contents of
635 the firmware volume. All calls to EraseBlocks() must be fully
636 flushed to the hardware before the EraseBlocks() service
639 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
642 @param ... The variable argument list is a list of tuples.
643 Each tuple describes a range of LBAs to erase
644 and consists of the following:
645 - An EFI_LBA that indicates the starting LBA
646 - A UINTN that indicates the number of blocks to erase.
648 The list is terminated with an EFI_LBA_LIST_TERMINATOR.
649 For example, the following indicates that two ranges of blocks
650 (5-7 and 10-11) are to be erased:
651 EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);
653 @retval EFI_SUCCESS The erase request successfully completed.
655 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
657 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be written.
658 The firmware device may have been partially erased.
660 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the variable argument list do
661 not exist in the firmware volume.
667 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*This
,
673 UINTN BlockAddress
; // Physical address of Lba to erase
674 EFI_LBA StartingLba
; // Lba from which we start erasing
675 UINTN NumOfLba
; // Number of Lba blocks to erase
676 NOR_FLASH_INSTANCE
*Instance
;
678 Instance
= INSTANCE_FROM_FVB_THIS(This
);
680 DEBUG ((DEBUG_BLKIO
, "FvbEraseBlocks()\n"));
682 Status
= EFI_SUCCESS
;
684 // Detect WriteDisabled state
685 if (Instance
->Media
.ReadOnly
== TRUE
) {
686 // Firmware volume is in WriteDisabled state
687 DEBUG ((EFI_D_ERROR
, "FvbEraseBlocks: ERROR - Device is in WriteDisabled state.\n"));
688 return EFI_ACCESS_DENIED
;
691 // Before erasing, check the entire list of parameters to ensure all specified blocks are valid
693 VA_START (Args
, This
);
695 // Get the Lba from which we start erasing
696 StartingLba
= VA_ARG (Args
, EFI_LBA
);
698 // Have we reached the end of the list?
699 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
700 //Exit the while loop
704 // How many Lba blocks are we requested to erase?
705 NumOfLba
= VA_ARG (Args
, UINT32
);
707 // All blocks must be within range
708 DEBUG ((DEBUG_BLKIO
, "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%d - 1 ) > LastBlock=%ld.\n", Instance
->StartLba
+ StartingLba
, NumOfLba
, Instance
->Media
.LastBlock
));
709 if ((NumOfLba
== 0) || ((Instance
->StartLba
+ StartingLba
+ NumOfLba
- 1) > Instance
->Media
.LastBlock
)) {
711 DEBUG ((EFI_D_ERROR
, "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n"));
712 Status
= EFI_INVALID_PARAMETER
;
720 // To get here, all must be ok, so start erasing
722 VA_START (Args
, This
);
724 // Get the Lba from which we start erasing
725 StartingLba
= VA_ARG (Args
, EFI_LBA
);
727 // Have we reached the end of the list?
728 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
729 // Exit the while loop
733 // How many Lba blocks are we requested to erase?
734 NumOfLba
= VA_ARG (Args
, UINT32
);
736 // Go through each one and erase it
737 while (NumOfLba
> 0) {
739 // Get the physical address of Lba to erase
740 BlockAddress
= GET_NOR_BLOCK_ADDRESS (
741 Instance
->BaseAddress
,
742 Instance
->StartLba
+ StartingLba
,
743 Instance
->Media
.BlockSize
747 DEBUG ((DEBUG_BLKIO
, "FvbEraseBlocks: Erasing Lba=%ld @ 0x%08x.\n", Instance
->StartLba
+ StartingLba
, BlockAddress
));
748 Status
= NorFlashUnlockAndEraseSingleBlock (BlockAddress
);
749 if (EFI_ERROR(Status
)) {
751 Status
= EFI_DEVICE_ERROR
;
755 // Move to the next Lba
768 NorFlashFvbInitialize (
769 IN NOR_FLASH_INSTANCE
* Instance
775 DEBUG((DEBUG_BLKIO
,"NorFlashFvbInitialize\n"));
777 Status
= NorFlashBlkIoInitialize (Instance
);
778 if (EFI_ERROR(Status
)) {
779 DEBUG((EFI_D_ERROR
,"NorFlashFvbInitialize: ERROR - Failed to initialize FVB\n"));
782 Instance
->Initialized
= TRUE
;
784 // Set the index of the first LBA for the FVB
785 Instance
->StartLba
= (PcdGet32 (PcdFlashNvStorageVariableBase
) - Instance
->BaseAddress
) / Instance
->Media
.BlockSize
;
787 // Determine if there is a valid header at the beginning of the NorFlash
788 Status
= ValidateFvHeader (Instance
);
789 if (EFI_ERROR(Status
)) {
790 // There is no valid header, so time to install one.
791 DEBUG((EFI_D_ERROR
,"NorFlashFvbInitialize: ERROR - The FVB Header is not valid. Installing a correct one for this volume.\n"));
793 // Erase all the NorFlash that is reserved for variable storage
794 FvbNumLba
= (PcdGet32(PcdFlashNvStorageVariableSize
) + PcdGet32(PcdFlashNvStorageFtwWorkingSize
) + PcdGet32(PcdFlashNvStorageFtwSpareSize
)) / Instance
->Media
.BlockSize
;
796 Status
= FvbEraseBlocks (&Instance
->FvbProtocol
, (EFI_LBA
)0, FvbNumLba
, EFI_LBA_LIST_TERMINATOR
);
797 if (EFI_ERROR(Status
)) {
801 // Install all appropriate headers
802 Status
= InitializeFvAndVariableStoreHeaders (Instance
);
803 if (EFI_ERROR(Status
)) {