]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashFvbDxe.c
c3e6489f398f896811d16999cb3a09292425329f
[mirror_edk2.git] / ArmPlatformPkg / Drivers / NorFlashDxe / NorFlashFvbDxe.c
1 /*++ @file NorFlashFvbDxe.c
2
3 Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.<BR>
4
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
9
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.
12
13 --*/
14
15 #include <PiDxe.h>
16
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>
25
26 #include <Guid/VariableFormat.h>
27 #include <Guid/SystemNvDataGuid.h>
28
29 #include "NorFlashDxe.h"
30
31 STATIC EFI_EVENT mFvbVirtualAddrChangeEvent;
32 STATIC UINTN mFlashNvStorageVariableBase;
33
34 ///
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.
42 ///
43
44 /**
45 Initialises the FV Header and Variable Store Header
46 to support variable operations.
47
48 @param[in] Ptr - Location to initialise the headers
49
50 **/
51 EFI_STATUS
52 InitializeFvAndVariableStoreHeaders (
53 IN NOR_FLASH_INSTANCE *Instance
54 )
55 {
56 EFI_STATUS Status;
57 VOID* Headers;
58 UINTN HeadersLength;
59 EFI_FIRMWARE_VOLUME_HEADER *FirmwareVolumeHeader;
60 VARIABLE_STORE_HEADER *VariableStoreHeader;
61
62 HeadersLength = sizeof(EFI_FIRMWARE_VOLUME_HEADER) + sizeof(EFI_FV_BLOCK_MAP_ENTRY) + sizeof(VARIABLE_STORE_HEADER);
63 Headers = AllocateZeroPool(HeadersLength);
64
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));
68
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));
73
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);
78
79 //
80 // EFI_FIRMWARE_VOLUME_HEADER
81 //
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
97 );
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);
105
106 //
107 // VARIABLE_STORE_HEADER
108 //
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;
114
115 // Install the combined super-header in the NorFlash
116 Status = FvbWrite (&Instance->FvbProtocol, 0, 0, &HeadersLength, Headers);
117
118 FreePool (Headers);
119 return Status;
120 }
121
122 /**
123 Check the integrity of firmware volume header.
124
125 @param[in] FwVolHeader - A pointer to a firmware volume header
126
127 @retval EFI_SUCCESS - The firmware volume is consistent
128 @retval EFI_NOT_FOUND - The firmware volume has been corrupted.
129
130 **/
131 EFI_STATUS
132 ValidateFvHeader (
133 IN NOR_FLASH_INSTANCE *Instance
134 )
135 {
136 UINT16 Checksum;
137 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
138 VARIABLE_STORE_HEADER *VariableStoreHeader;
139 UINTN VariableStoreLength;
140 UINTN FvLength;
141
142 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER*)Instance->RegionBaseAddress;
143
144 FvLength = PcdGet32(PcdFlashNvStorageVariableSize) + PcdGet32(PcdFlashNvStorageFtwWorkingSize) +
145 PcdGet32(PcdFlashNvStorageFtwSpareSize);
146
147 //
148 // Verify the header revision, header signature, length
149 // Length of FvBlock cannot be 2**64-1
150 // HeaderLength cannot be an odd number
151 //
152 if ( (FwVolHeader->Revision != EFI_FVH_REVISION)
153 || (FwVolHeader->Signature != EFI_FVH_SIGNATURE)
154 || (FwVolHeader->FvLength != FvLength)
155 )
156 {
157 DEBUG ((EFI_D_INFO, "%a: No Firmware Volume header present\n",
158 __FUNCTION__));
159 return EFI_NOT_FOUND;
160 }
161
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",
165 __FUNCTION__));
166 return EFI_NOT_FOUND;
167 }
168
169 // Verify the header checksum
170 Checksum = CalculateSum16((UINT16*)FwVolHeader, FwVolHeader->HeaderLength);
171 if (Checksum != 0) {
172 DEBUG ((EFI_D_INFO, "%a: FV checksum is invalid (Checksum:0x%X)\n",
173 __FUNCTION__, Checksum));
174 return EFI_NOT_FOUND;
175 }
176
177 VariableStoreHeader = (VARIABLE_STORE_HEADER*)((UINTN)FwVolHeader + FwVolHeader->HeaderLength);
178
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",
183 __FUNCTION__));
184 return EFI_NOT_FOUND;
185 }
186
187 VariableStoreLength = PcdGet32 (PcdFlashNvStorageVariableSize) - FwVolHeader->HeaderLength;
188 if (VariableStoreHeader->Size != VariableStoreLength) {
189 DEBUG ((EFI_D_INFO, "%a: Variable Store Length does not match\n",
190 __FUNCTION__));
191 return EFI_NOT_FOUND;
192 }
193
194 return EFI_SUCCESS;
195 }
196
197 /**
198 The GetAttributes() function retrieves the attributes and
199 current settings of the block.
200
201 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
202
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.
206
207 @retval EFI_SUCCESS The firmware volume attributes were returned.
208
209 **/
210 EFI_STATUS
211 EFIAPI
212 FvbGetAttributes(
213 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
214 OUT EFI_FVB_ATTRIBUTES_2 *Attributes
215 )
216 {
217 EFI_FVB_ATTRIBUTES_2 FlashFvbAttributes;
218 NOR_FLASH_INSTANCE *Instance;
219
220 Instance = INSTANCE_FROM_FVB_THIS(This);
221
222 FlashFvbAttributes = (EFI_FVB_ATTRIBUTES_2) (
223
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')
229
230 );
231
232 // Check if it is write protected
233 if (Instance->Media.ReadOnly != TRUE) {
234
235 FlashFvbAttributes = FlashFvbAttributes |
236 EFI_FVB2_WRITE_STATUS | // Writes are currently enabled
237 EFI_FVB2_WRITE_ENABLED_CAP; // Writes may be enabled
238 }
239
240 *Attributes = FlashFvbAttributes;
241
242 DEBUG ((DEBUG_BLKIO, "FvbGetAttributes(0x%X)\n", *Attributes));
243
244 return EFI_SUCCESS;
245 }
246
247 /**
248 The SetAttributes() function sets configurable firmware volume attributes
249 and returns the new settings of the firmware volume.
250
251
252 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
253
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
257 the firmware volume.
258 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
259
260 @retval EFI_SUCCESS The firmware volume attributes were returned.
261
262 @retval EFI_INVALID_PARAMETER The attributes requested are in conflict with the capabilities
263 as declared in the firmware volume header.
264
265 **/
266 EFI_STATUS
267 EFIAPI
268 FvbSetAttributes(
269 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
270 IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
271 )
272 {
273 DEBUG ((DEBUG_BLKIO, "FvbSetAttributes(0x%X) is not supported\n",*Attributes));
274 return EFI_UNSUPPORTED;
275 }
276
277 /**
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.
281
282 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
283
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.
288
289 @retval EFI_SUCCESS The firmware volume base address was returned.
290
291 @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.
292
293 **/
294 EFI_STATUS
295 EFIAPI
296 FvbGetPhysicalAddress (
297 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
298 OUT EFI_PHYSICAL_ADDRESS *Address
299 )
300 {
301 NOR_FLASH_INSTANCE *Instance;
302
303 Instance = INSTANCE_FROM_FVB_THIS(This);
304
305 DEBUG ((DEBUG_BLKIO, "FvbGetPhysicalAddress(BaseAddress=0x%08x)\n", Instance->RegionBaseAddress));
306
307 ASSERT(Address != NULL);
308
309 *Address = mFlashNvStorageVariableBase;
310 return EFI_SUCCESS;
311 }
312
313 /**
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).
318
319
320 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
321
322 @param Lba Indicates the block for which to return the size.
323
324 @param BlockSize Pointer to a caller-allocated UINTN in which
325 the size of the block is returned.
326
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
331 BlockSize.
332
333
334 @retval EFI_SUCCESS The firmware volume base address was returned.
335
336 @retval EFI_INVALID_PARAMETER The requested LBA is out of range.
337
338 **/
339 EFI_STATUS
340 EFIAPI
341 FvbGetBlockSize (
342 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
343 IN EFI_LBA Lba,
344 OUT UINTN *BlockSize,
345 OUT UINTN *NumberOfBlocks
346 )
347 {
348 EFI_STATUS Status;
349 NOR_FLASH_INSTANCE *Instance;
350
351 Instance = INSTANCE_FROM_FVB_THIS(This);
352
353 DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize(Lba=%ld, BlockSize=0x%x, LastBlock=%ld)\n", Lba, Instance->Media.BlockSize, Instance->Media.LastBlock));
354
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;
358 } else {
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);
362
363 DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize: *BlockSize=0x%x, *NumberOfBlocks=0x%x.\n", *BlockSize, *NumberOfBlocks));
364
365 Status = EFI_SUCCESS;
366 }
367
368 return Status;
369 }
370
371 /**
372 Reads the specified number of bytes into a buffer from the specified block.
373
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.
386
387 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
388
389 @param Lba The starting logical block index from which to read.
390
391 @param Offset Offset into the block at which to begin reading.
392
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.
396
397 @param Buffer Pointer to a caller-allocated buffer that will be used
398 to hold the data that is read.
399
400 @retval EFI_SUCCESS The firmware volume was read successfully, and contents are
401 in Buffer.
402
403 @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.
404 On output, NumBytes contains the total number of bytes
405 returned in Buffer.
406
407 @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state.
408
409 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be read.
410
411 **/
412 EFI_STATUS
413 EFIAPI
414 FvbRead (
415 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
416 IN EFI_LBA Lba,
417 IN UINTN Offset,
418 IN OUT UINTN *NumBytes,
419 IN OUT UINT8 *Buffer
420 )
421 {
422 EFI_STATUS TempStatus;
423 UINTN BlockSize;
424 NOR_FLASH_INSTANCE *Instance;
425
426 Instance = INSTANCE_FROM_FVB_THIS(This);
427
428 DEBUG ((DEBUG_BLKIO, "FvbRead(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Instance->StartLba + Lba, Offset, *NumBytes, Buffer));
429
430 TempStatus = EFI_SUCCESS;
431
432 // Cache the block size to avoid de-referencing pointers all the time
433 BlockSize = Instance->Media.BlockSize;
434
435 DEBUG ((DEBUG_BLKIO, "FvbRead: Check if (Offset=0x%x + NumBytes=0x%x) <= BlockSize=0x%x\n", Offset, *NumBytes, BlockSize ));
436
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;
444 }
445
446 // We must have some bytes to read
447 if (*NumBytes == 0) {
448 return EFI_BAD_BUFFER_SIZE;
449 }
450
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;
456 }
457 } else {
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;
463 }
464 }
465 return EFI_SUCCESS;
466 }
467
468 /**
469 Writes the specified number of bytes from the input buffer to the block.
470
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
496 returns.
497
498 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
499
500 @param Lba The starting logical block index to write to.
501
502 @param Offset Offset into the block at which to begin writing.
503
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.
507
508 @param Buffer The pointer to a caller-allocated buffer that contains the source for the write.
509
510 @retval EFI_SUCCESS The firmware volume was written successfully.
511
512 @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
513 On output, NumBytes contains the total number of bytes
514 actually written.
515
516 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
517
518 @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not be written.
519
520
521 **/
522 EFI_STATUS
523 EFIAPI
524 FvbWrite (
525 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
526 IN EFI_LBA Lba,
527 IN UINTN Offset,
528 IN OUT UINTN *NumBytes,
529 IN UINT8 *Buffer
530 )
531 {
532 NOR_FLASH_INSTANCE *Instance;
533
534 Instance = INSTANCE_FROM_FVB_THIS (This);
535
536 return NorFlashWriteSingleBlock (Instance, Instance->StartLba + Lba, Offset, NumBytes, Buffer);
537 }
538
539 /**
540 Erases and initialises a firmware volume block.
541
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
555 returns.
556
557 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
558 instance.
559
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.
565
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);
570
571 @retval EFI_SUCCESS The erase request successfully completed.
572
573 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
574
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.
577
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.
580
581 **/
582 EFI_STATUS
583 EFIAPI
584 FvbEraseBlocks (
585 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
586 ...
587 )
588 {
589 EFI_STATUS Status;
590 VA_LIST Args;
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;
595
596 Instance = INSTANCE_FROM_FVB_THIS(This);
597
598 DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks()\n"));
599
600 Status = EFI_SUCCESS;
601
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;
607 }
608
609 // Before erasing, check the entire list of parameters to ensure all specified blocks are valid
610
611 VA_START (Args, This);
612 do {
613 // Get the Lba from which we start erasing
614 StartingLba = VA_ARG (Args, EFI_LBA);
615
616 // Have we reached the end of the list?
617 if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
618 //Exit the while loop
619 break;
620 }
621
622 // How many Lba blocks are we requested to erase?
623 NumOfLba = VA_ARG (Args, UINTN);
624
625 // All blocks must be within range
626 DEBUG ((
627 DEBUG_BLKIO,
628 "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%Lu - 1 ) > LastBlock=%ld.\n",
629 Instance->StartLba + StartingLba,
630 (UINT64)NumOfLba,
631 Instance->Media.LastBlock
632 ));
633 if ((NumOfLba == 0) || ((Instance->StartLba + StartingLba + NumOfLba - 1) > Instance->Media.LastBlock)) {
634 VA_END (Args);
635 DEBUG ((EFI_D_ERROR, "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n"));
636 Status = EFI_INVALID_PARAMETER;
637 goto EXIT;
638 }
639 } while (TRUE);
640 VA_END (Args);
641
642 //
643 // To get here, all must be ok, so start erasing
644 //
645 VA_START (Args, This);
646 do {
647 // Get the Lba from which we start erasing
648 StartingLba = VA_ARG (Args, EFI_LBA);
649
650 // Have we reached the end of the list?
651 if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
652 // Exit the while loop
653 break;
654 }
655
656 // How many Lba blocks are we requested to erase?
657 NumOfLba = VA_ARG (Args, UINTN);
658
659 // Go through each one and erase it
660 while (NumOfLba > 0) {
661
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
667 );
668
669 // Erase it
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)) {
673 VA_END (Args);
674 Status = EFI_DEVICE_ERROR;
675 goto EXIT;
676 }
677
678 // Move to the next Lba
679 StartingLba++;
680 NumOfLba--;
681 }
682 } while (TRUE);
683 VA_END (Args);
684
685 EXIT:
686 return Status;
687 }
688
689 /**
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
692 lib to virtual mode.
693
694 @param[in] Event The Event that is being processed
695 @param[in] Context Event Context
696 **/
697 VOID
698 EFIAPI
699 FvbVirtualNotifyEvent (
700 IN EFI_EVENT Event,
701 IN VOID *Context
702 )
703 {
704 EfiConvertPointer (0x0, (VOID**)&mFlashNvStorageVariableBase);
705 return;
706 }
707
708 EFI_STATUS
709 EFIAPI
710 NorFlashFvbInitialize (
711 IN NOR_FLASH_INSTANCE* Instance
712 )
713 {
714 EFI_STATUS Status;
715 UINT32 FvbNumLba;
716 EFI_BOOT_MODE BootMode;
717 UINTN RuntimeMmioRegionSize;
718
719 DEBUG((DEBUG_BLKIO,"NorFlashFvbInitialize\n"));
720 ASSERT((Instance != NULL));
721
722 //
723 // Declare the Non-Volatile storage as EFI_MEMORY_RUNTIME
724 //
725
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;
731
732 Status = gDS->AddMemorySpace (
733 EfiGcdMemoryTypeMemoryMappedIo,
734 Instance->DeviceBaseAddress, RuntimeMmioRegionSize,
735 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
736 );
737 ASSERT_EFI_ERROR (Status);
738
739 Status = gDS->SetMemorySpaceAttributes (
740 Instance->DeviceBaseAddress, RuntimeMmioRegionSize,
741 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);
742 ASSERT_EFI_ERROR (Status);
743
744 mFlashNvStorageVariableBase = FixedPcdGet32 (PcdFlashNvStorageVariableBase);
745
746 // Set the index of the first LBA for the FVB
747 Instance->StartLba = (PcdGet32 (PcdFlashNvStorageVariableBase) - Instance->RegionBaseAddress) / Instance->Media.BlockSize;
748
749 BootMode = GetBootModeHob ();
750 if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {
751 Status = EFI_INVALID_PARAMETER;
752 } else {
753 // Determine if there is a valid header at the beginning of the NorFlash
754 Status = ValidateFvHeader (Instance);
755 }
756
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",
762 __FUNCTION__));
763
764 // Erase all the NorFlash that is reserved for variable storage
765 FvbNumLba = (PcdGet32(PcdFlashNvStorageVariableSize) + PcdGet32(PcdFlashNvStorageFtwWorkingSize) + PcdGet32(PcdFlashNvStorageFtwSpareSize)) / Instance->Media.BlockSize;
766
767 Status = FvbEraseBlocks (&Instance->FvbProtocol, (EFI_LBA)0, FvbNumLba, EFI_LBA_LIST_TERMINATOR);
768 if (EFI_ERROR(Status)) {
769 return Status;
770 }
771
772 // Install all appropriate headers
773 Status = InitializeFvAndVariableStoreHeaders (Instance);
774 if (EFI_ERROR(Status)) {
775 return Status;
776 }
777 }
778
779 //
780 // Register for the virtual address change event
781 //
782 Status = gBS->CreateEventEx (
783 EVT_NOTIFY_SIGNAL,
784 TPL_NOTIFY,
785 FvbVirtualNotifyEvent,
786 NULL,
787 &gEfiEventVirtualAddressChangeGuid,
788 &mFvbVirtualAddrChangeEvent
789 );
790 ASSERT_EFI_ERROR (Status);
791
792 return Status;
793 }