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