]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashFvbDxe.c
ArmPlatformPkg/NorFlashDxe: cue the variable driver with NvVarStoreFormatted
[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 #include <Guid/NvVarStoreFormatted.h>
29
30 #include "NorFlashDxe.h"
31
32 STATIC EFI_EVENT mFvbVirtualAddrChangeEvent;
33 STATIC UINTN mFlashNvStorageVariableBase;
34
35 ///
36 /// The Firmware Volume Block Protocol is the low-level interface
37 /// to a firmware volume. File-level access to a firmware volume
38 /// should not be done using the Firmware Volume Block Protocol.
39 /// Normal access to a firmware volume must use the Firmware
40 /// Volume Protocol. Typically, only the file system driver that
41 /// produces the Firmware Volume Protocol will bind to the
42 /// Firmware Volume Block Protocol.
43 ///
44
45 /**
46 Initialises the FV Header and Variable Store Header
47 to support variable operations.
48
49 @param[in] Ptr - Location to initialise the headers
50
51 **/
52 EFI_STATUS
53 InitializeFvAndVariableStoreHeaders (
54 IN NOR_FLASH_INSTANCE *Instance
55 )
56 {
57 EFI_STATUS Status;
58 VOID* Headers;
59 UINTN HeadersLength;
60 EFI_FIRMWARE_VOLUME_HEADER *FirmwareVolumeHeader;
61 VARIABLE_STORE_HEADER *VariableStoreHeader;
62
63 HeadersLength = sizeof(EFI_FIRMWARE_VOLUME_HEADER) + sizeof(EFI_FV_BLOCK_MAP_ENTRY) + sizeof(VARIABLE_STORE_HEADER);
64 Headers = AllocateZeroPool(HeadersLength);
65
66 // FirmwareVolumeHeader->FvLength is declared to have the Variable area AND the FTW working area AND the FTW Spare contiguous.
67 ASSERT(PcdGet32(PcdFlashNvStorageVariableBase) + PcdGet32(PcdFlashNvStorageVariableSize) == PcdGet32(PcdFlashNvStorageFtwWorkingBase));
68 ASSERT(PcdGet32(PcdFlashNvStorageFtwWorkingBase) + PcdGet32(PcdFlashNvStorageFtwWorkingSize) == PcdGet32(PcdFlashNvStorageFtwSpareBase));
69
70 // Check if the size of the area is at least one block size
71 ASSERT((PcdGet32(PcdFlashNvStorageVariableSize) > 0) && (PcdGet32(PcdFlashNvStorageVariableSize) / Instance->Media.BlockSize > 0));
72 ASSERT((PcdGet32(PcdFlashNvStorageFtwWorkingSize) > 0) && (PcdGet32(PcdFlashNvStorageFtwWorkingSize) / Instance->Media.BlockSize > 0));
73 ASSERT((PcdGet32(PcdFlashNvStorageFtwSpareSize) > 0) && (PcdGet32(PcdFlashNvStorageFtwSpareSize) / Instance->Media.BlockSize > 0));
74
75 // Ensure the Variable area Base Addresses are aligned on a block size boundaries
76 ASSERT(PcdGet32(PcdFlashNvStorageVariableBase) % Instance->Media.BlockSize == 0);
77 ASSERT(PcdGet32(PcdFlashNvStorageFtwWorkingBase) % Instance->Media.BlockSize == 0);
78 ASSERT(PcdGet32(PcdFlashNvStorageFtwSpareBase) % Instance->Media.BlockSize == 0);
79
80 //
81 // EFI_FIRMWARE_VOLUME_HEADER
82 //
83 FirmwareVolumeHeader = (EFI_FIRMWARE_VOLUME_HEADER*)Headers;
84 CopyGuid (&FirmwareVolumeHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid);
85 FirmwareVolumeHeader->FvLength =
86 PcdGet32(PcdFlashNvStorageVariableSize) +
87 PcdGet32(PcdFlashNvStorageFtwWorkingSize) +
88 PcdGet32(PcdFlashNvStorageFtwSpareSize);
89 FirmwareVolumeHeader->Signature = EFI_FVH_SIGNATURE;
90 FirmwareVolumeHeader->Attributes = (EFI_FVB_ATTRIBUTES_2) (
91 EFI_FVB2_READ_ENABLED_CAP | // Reads may be enabled
92 EFI_FVB2_READ_STATUS | // Reads are currently enabled
93 EFI_FVB2_STICKY_WRITE | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
94 EFI_FVB2_MEMORY_MAPPED | // It is memory mapped
95 EFI_FVB2_ERASE_POLARITY | // After erasure all bits take this value (i.e. '1')
96 EFI_FVB2_WRITE_STATUS | // Writes are currently enabled
97 EFI_FVB2_WRITE_ENABLED_CAP // Writes may be enabled
98 );
99 FirmwareVolumeHeader->HeaderLength = sizeof(EFI_FIRMWARE_VOLUME_HEADER) + sizeof(EFI_FV_BLOCK_MAP_ENTRY);
100 FirmwareVolumeHeader->Revision = EFI_FVH_REVISION;
101 FirmwareVolumeHeader->BlockMap[0].NumBlocks = Instance->Media.LastBlock + 1;
102 FirmwareVolumeHeader->BlockMap[0].Length = Instance->Media.BlockSize;
103 FirmwareVolumeHeader->BlockMap[1].NumBlocks = 0;
104 FirmwareVolumeHeader->BlockMap[1].Length = 0;
105 FirmwareVolumeHeader->Checksum = CalculateCheckSum16 ((UINT16*)FirmwareVolumeHeader,FirmwareVolumeHeader->HeaderLength);
106
107 //
108 // VARIABLE_STORE_HEADER
109 //
110 VariableStoreHeader = (VARIABLE_STORE_HEADER*)((UINTN)Headers + FirmwareVolumeHeader->HeaderLength);
111 CopyGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGuid);
112 VariableStoreHeader->Size = PcdGet32(PcdFlashNvStorageVariableSize) - FirmwareVolumeHeader->HeaderLength;
113 VariableStoreHeader->Format = VARIABLE_STORE_FORMATTED;
114 VariableStoreHeader->State = VARIABLE_STORE_HEALTHY;
115
116 // Install the combined super-header in the NorFlash
117 Status = FvbWrite (&Instance->FvbProtocol, 0, 0, &HeadersLength, Headers);
118
119 FreePool (Headers);
120 return Status;
121 }
122
123 /**
124 Check the integrity of firmware volume header.
125
126 @param[in] FwVolHeader - A pointer to a firmware volume header
127
128 @retval EFI_SUCCESS - The firmware volume is consistent
129 @retval EFI_NOT_FOUND - The firmware volume has been corrupted.
130
131 **/
132 EFI_STATUS
133 ValidateFvHeader (
134 IN NOR_FLASH_INSTANCE *Instance
135 )
136 {
137 UINT16 Checksum;
138 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
139 VARIABLE_STORE_HEADER *VariableStoreHeader;
140 UINTN VariableStoreLength;
141 UINTN FvLength;
142
143 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER*)Instance->RegionBaseAddress;
144
145 FvLength = PcdGet32(PcdFlashNvStorageVariableSize) + PcdGet32(PcdFlashNvStorageFtwWorkingSize) +
146 PcdGet32(PcdFlashNvStorageFtwSpareSize);
147
148 //
149 // Verify the header revision, header signature, length
150 // Length of FvBlock cannot be 2**64-1
151 // HeaderLength cannot be an odd number
152 //
153 if ( (FwVolHeader->Revision != EFI_FVH_REVISION)
154 || (FwVolHeader->Signature != EFI_FVH_SIGNATURE)
155 || (FwVolHeader->FvLength != FvLength)
156 )
157 {
158 DEBUG ((EFI_D_INFO, "%a: No Firmware Volume header present\n",
159 __FUNCTION__));
160 return EFI_NOT_FOUND;
161 }
162
163 // Check the Firmware Volume Guid
164 if( CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid) == FALSE ) {
165 DEBUG ((EFI_D_INFO, "%a: Firmware Volume Guid non-compatible\n",
166 __FUNCTION__));
167 return EFI_NOT_FOUND;
168 }
169
170 // Verify the header checksum
171 Checksum = CalculateSum16((UINT16*)FwVolHeader, FwVolHeader->HeaderLength);
172 if (Checksum != 0) {
173 DEBUG ((EFI_D_INFO, "%a: FV checksum is invalid (Checksum:0x%X)\n",
174 __FUNCTION__, Checksum));
175 return EFI_NOT_FOUND;
176 }
177
178 VariableStoreHeader = (VARIABLE_STORE_HEADER*)((UINTN)FwVolHeader + FwVolHeader->HeaderLength);
179
180 // Check the Variable Store Guid
181 if (!CompareGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid) &&
182 !CompareGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGuid)) {
183 DEBUG ((EFI_D_INFO, "%a: Variable Store Guid non-compatible\n",
184 __FUNCTION__));
185 return EFI_NOT_FOUND;
186 }
187
188 VariableStoreLength = PcdGet32 (PcdFlashNvStorageVariableSize) - FwVolHeader->HeaderLength;
189 if (VariableStoreHeader->Size != VariableStoreLength) {
190 DEBUG ((EFI_D_INFO, "%a: Variable Store Length does not match\n",
191 __FUNCTION__));
192 return EFI_NOT_FOUND;
193 }
194
195 return EFI_SUCCESS;
196 }
197
198 /**
199 The GetAttributes() function retrieves the attributes and
200 current settings of the block.
201
202 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
203
204 @param Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the attributes and
205 current settings are returned.
206 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
207
208 @retval EFI_SUCCESS The firmware volume attributes were returned.
209
210 **/
211 EFI_STATUS
212 EFIAPI
213 FvbGetAttributes(
214 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
215 OUT EFI_FVB_ATTRIBUTES_2 *Attributes
216 )
217 {
218 EFI_FVB_ATTRIBUTES_2 FlashFvbAttributes;
219 NOR_FLASH_INSTANCE *Instance;
220
221 Instance = INSTANCE_FROM_FVB_THIS(This);
222
223 FlashFvbAttributes = (EFI_FVB_ATTRIBUTES_2) (
224
225 EFI_FVB2_READ_ENABLED_CAP | // Reads may be enabled
226 EFI_FVB2_READ_STATUS | // Reads are currently enabled
227 EFI_FVB2_STICKY_WRITE | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
228 EFI_FVB2_MEMORY_MAPPED | // It is memory mapped
229 EFI_FVB2_ERASE_POLARITY // After erasure all bits take this value (i.e. '1')
230
231 );
232
233 // Check if it is write protected
234 if (Instance->Media.ReadOnly != TRUE) {
235
236 FlashFvbAttributes = FlashFvbAttributes |
237 EFI_FVB2_WRITE_STATUS | // Writes are currently enabled
238 EFI_FVB2_WRITE_ENABLED_CAP; // Writes may be enabled
239 }
240
241 *Attributes = FlashFvbAttributes;
242
243 DEBUG ((DEBUG_BLKIO, "FvbGetAttributes(0x%X)\n", *Attributes));
244
245 return EFI_SUCCESS;
246 }
247
248 /**
249 The SetAttributes() function sets configurable firmware volume attributes
250 and returns the new settings of the firmware volume.
251
252
253 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
254
255 @param Attributes On input, Attributes is a pointer to EFI_FVB_ATTRIBUTES_2
256 that contains the desired firmware volume settings.
257 On successful return, it contains the new settings of
258 the firmware volume.
259 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
260
261 @retval EFI_SUCCESS The firmware volume attributes were returned.
262
263 @retval EFI_INVALID_PARAMETER The attributes requested are in conflict with the capabilities
264 as declared in the firmware volume header.
265
266 **/
267 EFI_STATUS
268 EFIAPI
269 FvbSetAttributes(
270 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
271 IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
272 )
273 {
274 DEBUG ((DEBUG_BLKIO, "FvbSetAttributes(0x%X) is not supported\n",*Attributes));
275 return EFI_UNSUPPORTED;
276 }
277
278 /**
279 The GetPhysicalAddress() function retrieves the base address of
280 a memory-mapped firmware volume. This function should be called
281 only for memory-mapped firmware volumes.
282
283 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
284
285 @param Address Pointer to a caller-allocated
286 EFI_PHYSICAL_ADDRESS that, on successful
287 return from GetPhysicalAddress(), contains the
288 base address of the firmware volume.
289
290 @retval EFI_SUCCESS The firmware volume base address was returned.
291
292 @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.
293
294 **/
295 EFI_STATUS
296 EFIAPI
297 FvbGetPhysicalAddress (
298 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
299 OUT EFI_PHYSICAL_ADDRESS *Address
300 )
301 {
302 NOR_FLASH_INSTANCE *Instance;
303
304 Instance = INSTANCE_FROM_FVB_THIS(This);
305
306 DEBUG ((DEBUG_BLKIO, "FvbGetPhysicalAddress(BaseAddress=0x%08x)\n", Instance->RegionBaseAddress));
307
308 ASSERT(Address != NULL);
309
310 *Address = mFlashNvStorageVariableBase;
311 return EFI_SUCCESS;
312 }
313
314 /**
315 The GetBlockSize() function retrieves the size of the requested
316 block. It also returns the number of additional blocks with
317 the identical size. The GetBlockSize() function is used to
318 retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).
319
320
321 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
322
323 @param Lba Indicates the block for which to return the size.
324
325 @param BlockSize Pointer to a caller-allocated UINTN in which
326 the size of the block is returned.
327
328 @param NumberOfBlocks Pointer to a caller-allocated UINTN in
329 which the number of consecutive blocks,
330 starting with Lba, is returned. All
331 blocks in this range have a size of
332 BlockSize.
333
334
335 @retval EFI_SUCCESS The firmware volume base address was returned.
336
337 @retval EFI_INVALID_PARAMETER The requested LBA is out of range.
338
339 **/
340 EFI_STATUS
341 EFIAPI
342 FvbGetBlockSize (
343 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
344 IN EFI_LBA Lba,
345 OUT UINTN *BlockSize,
346 OUT UINTN *NumberOfBlocks
347 )
348 {
349 EFI_STATUS Status;
350 NOR_FLASH_INSTANCE *Instance;
351
352 Instance = INSTANCE_FROM_FVB_THIS(This);
353
354 DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize(Lba=%ld, BlockSize=0x%x, LastBlock=%ld)\n", Lba, Instance->Media.BlockSize, Instance->Media.LastBlock));
355
356 if (Lba > Instance->Media.LastBlock) {
357 DEBUG ((EFI_D_ERROR, "FvbGetBlockSize: ERROR - Parameter LBA %ld is beyond the last Lba (%ld).\n", Lba, Instance->Media.LastBlock));
358 Status = EFI_INVALID_PARAMETER;
359 } else {
360 // This is easy because in this platform each NorFlash device has equal sized blocks.
361 *BlockSize = (UINTN) Instance->Media.BlockSize;
362 *NumberOfBlocks = (UINTN) (Instance->Media.LastBlock - Lba + 1);
363
364 DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize: *BlockSize=0x%x, *NumberOfBlocks=0x%x.\n", *BlockSize, *NumberOfBlocks));
365
366 Status = EFI_SUCCESS;
367 }
368
369 return Status;
370 }
371
372 /**
373 Reads the specified number of bytes into a buffer from the specified block.
374
375 The Read() function reads the requested number of bytes from the
376 requested block and stores them in the provided buffer.
377 Implementations should be mindful that the firmware volume
378 might be in the ReadDisabled state. If it is in this state,
379 the Read() function must return the status code
380 EFI_ACCESS_DENIED without modifying the contents of the
381 buffer. The Read() function must also prevent spanning block
382 boundaries. If a read is requested that would span a block
383 boundary, the read must read up to the boundary but not
384 beyond. The output parameter NumBytes must be set to correctly
385 indicate the number of bytes actually read. The caller must be
386 aware that a read may be partially completed.
387
388 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
389
390 @param Lba The starting logical block index from which to read.
391
392 @param Offset Offset into the block at which to begin reading.
393
394 @param NumBytes Pointer to a UINTN.
395 At entry, *NumBytes contains the total size of the buffer.
396 At exit, *NumBytes contains the total number of bytes read.
397
398 @param Buffer Pointer to a caller-allocated buffer that will be used
399 to hold the data that is read.
400
401 @retval EFI_SUCCESS The firmware volume was read successfully, and contents are
402 in Buffer.
403
404 @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.
405 On output, NumBytes contains the total number of bytes
406 returned in Buffer.
407
408 @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state.
409
410 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be read.
411
412 **/
413 EFI_STATUS
414 EFIAPI
415 FvbRead (
416 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
417 IN EFI_LBA Lba,
418 IN UINTN Offset,
419 IN OUT UINTN *NumBytes,
420 IN OUT UINT8 *Buffer
421 )
422 {
423 EFI_STATUS TempStatus;
424 UINTN BlockSize;
425 NOR_FLASH_INSTANCE *Instance;
426
427 Instance = INSTANCE_FROM_FVB_THIS(This);
428
429 DEBUG ((DEBUG_BLKIO, "FvbRead(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Instance->StartLba + Lba, Offset, *NumBytes, Buffer));
430
431 TempStatus = EFI_SUCCESS;
432
433 // Cache the block size to avoid de-referencing pointers all the time
434 BlockSize = Instance->Media.BlockSize;
435
436 DEBUG ((DEBUG_BLKIO, "FvbRead: Check if (Offset=0x%x + NumBytes=0x%x) <= BlockSize=0x%x\n", Offset, *NumBytes, BlockSize ));
437
438 // The read must not span block boundaries.
439 // We need to check each variable individually because adding two large values together overflows.
440 if ((Offset >= BlockSize) ||
441 (*NumBytes > BlockSize) ||
442 ((Offset + *NumBytes) > BlockSize)) {
443 DEBUG ((EFI_D_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize ));
444 return EFI_BAD_BUFFER_SIZE;
445 }
446
447 // We must have some bytes to read
448 if (*NumBytes == 0) {
449 return EFI_BAD_BUFFER_SIZE;
450 }
451
452 // Decide if we are doing full block reads or not.
453 if (*NumBytes % BlockSize != 0) {
454 TempStatus = NorFlashRead (Instance, Instance->StartLba + Lba, Offset, *NumBytes, Buffer);
455 if (EFI_ERROR (TempStatus)) {
456 return EFI_DEVICE_ERROR;
457 }
458 } else {
459 // Read NOR Flash data into shadow buffer
460 TempStatus = NorFlashReadBlocks (Instance, Instance->StartLba + Lba, BlockSize, Buffer);
461 if (EFI_ERROR (TempStatus)) {
462 // Return one of the pre-approved error statuses
463 return EFI_DEVICE_ERROR;
464 }
465 }
466 return EFI_SUCCESS;
467 }
468
469 /**
470 Writes the specified number of bytes from the input buffer to the block.
471
472 The Write() function writes the specified number of bytes from
473 the provided buffer to the specified block and offset. If the
474 firmware volume is sticky write, the caller must ensure that
475 all the bits of the specified range to write are in the
476 EFI_FVB_ERASE_POLARITY state before calling the Write()
477 function, or else the result will be unpredictable. This
478 unpredictability arises because, for a sticky-write firmware
479 volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY
480 state but cannot flip it back again. Before calling the
481 Write() function, it is recommended for the caller to first call
482 the EraseBlocks() function to erase the specified block to
483 write. A block erase cycle will transition bits from the
484 (NOT)EFI_FVB_ERASE_POLARITY state back to the
485 EFI_FVB_ERASE_POLARITY state. Implementations should be
486 mindful that the firmware volume might be in the WriteDisabled
487 state. If it is in this state, the Write() function must
488 return the status code EFI_ACCESS_DENIED without modifying the
489 contents of the firmware volume. The Write() function must
490 also prevent spanning block boundaries. If a write is
491 requested that spans a block boundary, the write must store up
492 to the boundary but not beyond. The output parameter NumBytes
493 must be set to correctly indicate the number of bytes actually
494 written. The caller must be aware that a write may be
495 partially completed. All writes, partial or otherwise, must be
496 fully flushed to the hardware before the Write() service
497 returns.
498
499 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
500
501 @param Lba The starting logical block index to write to.
502
503 @param Offset Offset into the block at which to begin writing.
504
505 @param NumBytes The pointer to a UINTN.
506 At entry, *NumBytes contains the total size of the buffer.
507 At exit, *NumBytes contains the total number of bytes actually written.
508
509 @param Buffer The pointer to a caller-allocated buffer that contains the source for the write.
510
511 @retval EFI_SUCCESS The firmware volume was written successfully.
512
513 @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
514 On output, NumBytes contains the total number of bytes
515 actually written.
516
517 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
518
519 @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not be written.
520
521
522 **/
523 EFI_STATUS
524 EFIAPI
525 FvbWrite (
526 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
527 IN EFI_LBA Lba,
528 IN UINTN Offset,
529 IN OUT UINTN *NumBytes,
530 IN UINT8 *Buffer
531 )
532 {
533 NOR_FLASH_INSTANCE *Instance;
534
535 Instance = INSTANCE_FROM_FVB_THIS (This);
536
537 return NorFlashWriteSingleBlock (Instance, Instance->StartLba + Lba, Offset, NumBytes, Buffer);
538 }
539
540 /**
541 Erases and initialises a firmware volume block.
542
543 The EraseBlocks() function erases one or more blocks as denoted
544 by the variable argument list. The entire parameter list of
545 blocks must be verified before erasing any blocks. If a block is
546 requested that does not exist within the associated firmware
547 volume (it has a larger index than the last block of the
548 firmware volume), the EraseBlocks() function must return the
549 status code EFI_INVALID_PARAMETER without modifying the contents
550 of the firmware volume. Implementations should be mindful that
551 the firmware volume might be in the WriteDisabled state. If it
552 is in this state, the EraseBlocks() function must return the
553 status code EFI_ACCESS_DENIED without modifying the contents of
554 the firmware volume. All calls to EraseBlocks() must be fully
555 flushed to the hardware before the EraseBlocks() service
556 returns.
557
558 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
559 instance.
560
561 @param ... The variable argument list is a list of tuples.
562 Each tuple describes a range of LBAs to erase
563 and consists of the following:
564 - An EFI_LBA that indicates the starting LBA
565 - A UINTN that indicates the number of blocks to erase.
566
567 The list is terminated with an EFI_LBA_LIST_TERMINATOR.
568 For example, the following indicates that two ranges of blocks
569 (5-7 and 10-11) are to be erased:
570 EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);
571
572 @retval EFI_SUCCESS The erase request successfully completed.
573
574 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
575
576 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be written.
577 The firmware device may have been partially erased.
578
579 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the variable argument list do
580 not exist in the firmware volume.
581
582 **/
583 EFI_STATUS
584 EFIAPI
585 FvbEraseBlocks (
586 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
587 ...
588 )
589 {
590 EFI_STATUS Status;
591 VA_LIST Args;
592 UINTN BlockAddress; // Physical address of Lba to erase
593 EFI_LBA StartingLba; // Lba from which we start erasing
594 UINTN NumOfLba; // Number of Lba blocks to erase
595 NOR_FLASH_INSTANCE *Instance;
596
597 Instance = INSTANCE_FROM_FVB_THIS(This);
598
599 DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks()\n"));
600
601 Status = EFI_SUCCESS;
602
603 // Detect WriteDisabled state
604 if (Instance->Media.ReadOnly == TRUE) {
605 // Firmware volume is in WriteDisabled state
606 DEBUG ((EFI_D_ERROR, "FvbEraseBlocks: ERROR - Device is in WriteDisabled state.\n"));
607 return EFI_ACCESS_DENIED;
608 }
609
610 // Before erasing, check the entire list of parameters to ensure all specified blocks are valid
611
612 VA_START (Args, This);
613 do {
614 // Get the Lba from which we start erasing
615 StartingLba = VA_ARG (Args, EFI_LBA);
616
617 // Have we reached the end of the list?
618 if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
619 //Exit the while loop
620 break;
621 }
622
623 // How many Lba blocks are we requested to erase?
624 NumOfLba = VA_ARG (Args, UINTN);
625
626 // All blocks must be within range
627 DEBUG ((
628 DEBUG_BLKIO,
629 "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%Lu - 1 ) > LastBlock=%ld.\n",
630 Instance->StartLba + StartingLba,
631 (UINT64)NumOfLba,
632 Instance->Media.LastBlock
633 ));
634 if ((NumOfLba == 0) || ((Instance->StartLba + StartingLba + NumOfLba - 1) > Instance->Media.LastBlock)) {
635 VA_END (Args);
636 DEBUG ((EFI_D_ERROR, "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n"));
637 Status = EFI_INVALID_PARAMETER;
638 goto EXIT;
639 }
640 } while (TRUE);
641 VA_END (Args);
642
643 //
644 // To get here, all must be ok, so start erasing
645 //
646 VA_START (Args, This);
647 do {
648 // Get the Lba from which we start erasing
649 StartingLba = VA_ARG (Args, EFI_LBA);
650
651 // Have we reached the end of the list?
652 if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
653 // Exit the while loop
654 break;
655 }
656
657 // How many Lba blocks are we requested to erase?
658 NumOfLba = VA_ARG (Args, UINTN);
659
660 // Go through each one and erase it
661 while (NumOfLba > 0) {
662
663 // Get the physical address of Lba to erase
664 BlockAddress = GET_NOR_BLOCK_ADDRESS (
665 Instance->RegionBaseAddress,
666 Instance->StartLba + StartingLba,
667 Instance->Media.BlockSize
668 );
669
670 // Erase it
671 DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks: Erasing Lba=%ld @ 0x%08x.\n", Instance->StartLba + StartingLba, BlockAddress));
672 Status = NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress);
673 if (EFI_ERROR(Status)) {
674 VA_END (Args);
675 Status = EFI_DEVICE_ERROR;
676 goto EXIT;
677 }
678
679 // Move to the next Lba
680 StartingLba++;
681 NumOfLba--;
682 }
683 } while (TRUE);
684 VA_END (Args);
685
686 EXIT:
687 return Status;
688 }
689
690 /**
691 Fixup internal data so that EFI can be call in virtual mode.
692 Call the passed in Child Notify event and convert any pointers in
693 lib to virtual mode.
694
695 @param[in] Event The Event that is being processed
696 @param[in] Context Event Context
697 **/
698 VOID
699 EFIAPI
700 FvbVirtualNotifyEvent (
701 IN EFI_EVENT Event,
702 IN VOID *Context
703 )
704 {
705 EfiConvertPointer (0x0, (VOID**)&mFlashNvStorageVariableBase);
706 return;
707 }
708
709 EFI_STATUS
710 EFIAPI
711 NorFlashFvbInitialize (
712 IN NOR_FLASH_INSTANCE* Instance
713 )
714 {
715 EFI_STATUS Status;
716 UINT32 FvbNumLba;
717 EFI_BOOT_MODE BootMode;
718 UINTN RuntimeMmioRegionSize;
719
720 DEBUG((DEBUG_BLKIO,"NorFlashFvbInitialize\n"));
721 ASSERT((Instance != NULL));
722
723 //
724 // Declare the Non-Volatile storage as EFI_MEMORY_RUNTIME
725 //
726
727 // Note: all the NOR Flash region needs to be reserved into the UEFI Runtime memory;
728 // even if we only use the small block region at the top of the NOR Flash.
729 // The reason is when the NOR Flash memory is set into program mode, the command
730 // is written as the base of the flash region (ie: Instance->DeviceBaseAddress)
731 RuntimeMmioRegionSize = (Instance->RegionBaseAddress - Instance->DeviceBaseAddress) + Instance->Size;
732
733 Status = gDS->AddMemorySpace (
734 EfiGcdMemoryTypeMemoryMappedIo,
735 Instance->DeviceBaseAddress, RuntimeMmioRegionSize,
736 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
737 );
738 ASSERT_EFI_ERROR (Status);
739
740 Status = gDS->SetMemorySpaceAttributes (
741 Instance->DeviceBaseAddress, RuntimeMmioRegionSize,
742 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);
743 ASSERT_EFI_ERROR (Status);
744
745 mFlashNvStorageVariableBase = FixedPcdGet32 (PcdFlashNvStorageVariableBase);
746
747 // Set the index of the first LBA for the FVB
748 Instance->StartLba = (PcdGet32 (PcdFlashNvStorageVariableBase) - Instance->RegionBaseAddress) / Instance->Media.BlockSize;
749
750 BootMode = GetBootModeHob ();
751 if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {
752 Status = EFI_INVALID_PARAMETER;
753 } else {
754 // Determine if there is a valid header at the beginning of the NorFlash
755 Status = ValidateFvHeader (Instance);
756 }
757
758 // Install the Default FVB header if required
759 if (EFI_ERROR(Status)) {
760 // There is no valid header, so time to install one.
761 DEBUG ((EFI_D_INFO, "%a: The FVB Header is not valid.\n", __FUNCTION__));
762 DEBUG ((EFI_D_INFO, "%a: Installing a correct one for this volume.\n",
763 __FUNCTION__));
764
765 // Erase all the NorFlash that is reserved for variable storage
766 FvbNumLba = (PcdGet32(PcdFlashNvStorageVariableSize) + PcdGet32(PcdFlashNvStorageFtwWorkingSize) + PcdGet32(PcdFlashNvStorageFtwSpareSize)) / Instance->Media.BlockSize;
767
768 Status = FvbEraseBlocks (&Instance->FvbProtocol, (EFI_LBA)0, FvbNumLba, EFI_LBA_LIST_TERMINATOR);
769 if (EFI_ERROR(Status)) {
770 return Status;
771 }
772
773 // Install all appropriate headers
774 Status = InitializeFvAndVariableStoreHeaders (Instance);
775 if (EFI_ERROR(Status)) {
776 return Status;
777 }
778 }
779
780 //
781 // The driver implementing the variable read service can now be dispatched;
782 // the varstore headers are in place.
783 //
784 Status = gBS->InstallProtocolInterface (
785 &gImageHandle,
786 &gEdkiiNvVarStoreFormattedGuid,
787 EFI_NATIVE_INTERFACE,
788 NULL
789 );
790 ASSERT_EFI_ERROR (Status);
791
792 //
793 // Register for the virtual address change event
794 //
795 Status = gBS->CreateEventEx (
796 EVT_NOTIFY_SIGNAL,
797 TPL_NOTIFY,
798 FvbVirtualNotifyEvent,
799 NULL,
800 &gEfiEventVirtualAddressChangeGuid,
801 &mFvbVirtualAddrChangeEvent
802 );
803 ASSERT_EFI_ERROR (Status);
804
805 return Status;
806 }