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