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