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