]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/EmuVariableFvbRuntimeDxe/Fvb.c
OvmfPkg/EmuVariableFvbRuntimeDxe: strip trailing whitespace
[mirror_edk2.git] / OvmfPkg / EmuVariableFvbRuntimeDxe / Fvb.c
CommitLineData
670d495b 1/** @file\r
2 Firmware Block Services to support emulating non-volatile variables\r
3 by pretending that a memory buffer is storage for the NV variables.\r
4\r
c30f1e09 5 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
56d7640a 6 This program and the accompanying materials\r
670d495b 7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "PiDxe.h"\r
17#include <Guid/EventGroup.h>\r
18#include <Guid/SystemNvDataGuid.h>\r
19#include <Guid/VariableFormat.h>\r
20\r
21#include <Protocol/FirmwareVolumeBlock.h>\r
22#include <Protocol/DevicePath.h>\r
23\r
24#include <Library/UefiLib.h>\r
25#include <Library/UefiDriverEntryPoint.h>\r
26#include <Library/BaseLib.h>\r
27#include <Library/UefiRuntimeLib.h>\r
28#include <Library/DebugLib.h>\r
29#include <Library/BaseMemoryLib.h>\r
30#include <Library/MemoryAllocationLib.h>\r
31#include <Library/UefiBootServicesTableLib.h>\r
32#include <Library/DevicePathLib.h>\r
33#include <Library/PcdLib.h>\r
34#include <Library/PlatformFvbLib.h>\r
35#include "Fvb.h"\r
36\r
bdf93df8 37#define EFI_AUTHENTICATED_VARIABLE_GUID \\r
38{ 0xaaf32c78, 0x947b, 0x439a, { 0xa1, 0x80, 0x2e, 0x14, 0x4e, 0xc3, 0x77, 0x92 } }\r
39\r
670d495b 40//\r
41// Virtual Address Change Event\r
42//\r
43// This is needed for runtime variable access.\r
44//\r
45EFI_EVENT mEmuVarsFvbAddrChangeEvent = NULL;\r
46\r
47//\r
48// This is the single instance supported by this driver. It\r
49// supports the FVB and Device Path protocols.\r
50//\r
51EFI_FW_VOL_BLOCK_DEVICE mEmuVarsFvb = {\r
52 FVB_DEVICE_SIGNATURE,\r
53 { // DevicePath\r
54 {\r
55 {\r
56 HARDWARE_DEVICE_PATH,\r
57 HW_MEMMAP_DP,\r
58 {\r
59 sizeof (MEMMAP_DEVICE_PATH),\r
60 0\r
61 }\r
62 },\r
63 EfiMemoryMappedIO,\r
64 0,\r
65 0,\r
66 },\r
67 {\r
68 END_DEVICE_PATH_TYPE,\r
69 END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
70 {\r
71 sizeof (EFI_DEVICE_PATH_PROTOCOL),\r
72 0\r
73 }\r
74 }\r
75 },\r
76 NULL, // BufferPtr\r
77 FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize), // BlockSize\r
78 2 * FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize), // Size\r
79 { // FwVolBlockInstance\r
80 FvbProtocolGetAttributes,\r
81 FvbProtocolSetAttributes,\r
82 FvbProtocolGetPhysicalAddress,\r
83 FvbProtocolGetBlockSize,\r
84 FvbProtocolRead,\r
85 FvbProtocolWrite,\r
86 FvbProtocolEraseBlocks,\r
87 NULL\r
88 },\r
89};\r
90\r
91\r
92/**\r
93 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
94\r
95 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
96 It converts pointer to new virtual address.\r
97\r
98 @param Event Event whose notification function is being invoked.\r
99 @param Context Pointer to the notification function's context.\r
100\r
101**/\r
102VOID\r
103EFIAPI\r
104FvbVirtualAddressChangeEvent (\r
105 IN EFI_EVENT Event,\r
106 IN VOID *Context\r
107 )\r
108{\r
109 EfiConvertPointer (0x0, &mEmuVarsFvb.BufferPtr);\r
110}\r
111\r
112\r
113//\r
114// FVB protocol APIs\r
115//\r
116\r
117/**\r
118 The GetPhysicalAddress() function retrieves the base address of\r
119 a memory-mapped firmware volume. This function should be called\r
120 only for memory-mapped firmware volumes.\r
121\r
949b0f3b 122 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r
89f385ce 123\r
670d495b 124 @param Address Pointer to a caller-allocated\r
125 EFI_PHYSICAL_ADDRESS that, on successful\r
126 return from GetPhysicalAddress(), contains the\r
127 base address of the firmware volume.\r
89f385ce 128\r
670d495b 129 @retval EFI_SUCCESS The firmware volume base address is returned.\r
89f385ce 130\r
670d495b 131 @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.\r
132\r
133**/\r
134EFI_STATUS\r
135EFIAPI\r
136FvbProtocolGetPhysicalAddress (\r
949b0f3b 137 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r
670d495b 138 OUT EFI_PHYSICAL_ADDRESS *Address\r
139 )\r
140{\r
141 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
142\r
143 FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
144\r
145 *Address = (EFI_PHYSICAL_ADDRESS)(UINTN) FvbDevice->BufferPtr;\r
146\r
147 return EFI_SUCCESS;\r
148}\r
149\r
150\r
151/**\r
152 The GetBlockSize() function retrieves the size of the requested\r
153 block. It also returns the number of additional blocks with\r
154 the identical size. The GetBlockSize() function is used to\r
155 retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).\r
156\r
157\r
949b0f3b 158 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r
670d495b 159\r
160 @param Lba Indicates the block for which to return the size.\r
161\r
162 @param BlockSize Pointer to a caller-allocated UINTN in which\r
163 the size of the block is returned.\r
164\r
165 @param NumberOfBlocks Pointer to a caller-allocated UINTN in\r
166 which the number of consecutive blocks,\r
167 starting with Lba, is returned. All\r
168 blocks in this range have a size of\r
169 BlockSize.\r
170\r
89f385ce 171\r
670d495b 172 @retval EFI_SUCCESS The firmware volume base address is returned.\r
89f385ce 173\r
670d495b 174 @retval EFI_INVALID_PARAMETER The requested LBA is out of range.\r
175\r
176**/\r
177EFI_STATUS\r
178EFIAPI\r
179FvbProtocolGetBlockSize (\r
949b0f3b 180 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r
670d495b 181 IN EFI_LBA Lba,\r
182 OUT UINTN *BlockSize,\r
183 OUT UINTN *NumberOfBlocks\r
184 )\r
185{\r
186 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
187\r
188 if (Lba > 1) {\r
189 return EFI_INVALID_PARAMETER;\r
190 }\r
191\r
192 FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
193\r
194 *BlockSize = FvbDevice->BlockSize;\r
02e12420 195 *NumberOfBlocks = (UINTN) (2 - (UINTN) Lba);\r
670d495b 196\r
197 return EFI_SUCCESS;\r
198}\r
199\r
200\r
201/**\r
202 The GetAttributes() function retrieves the attributes and\r
203 current settings of the block. Status Codes Returned\r
204\r
949b0f3b 205 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r
670d495b 206\r
207 @param Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the\r
208 attributes and current settings are\r
209 returned. Type EFI_FVB_ATTRIBUTES_2 is defined\r
210 in EFI_FIRMWARE_VOLUME_HEADER.\r
211\r
212 @retval EFI_SUCCESS The firmware volume attributes were\r
213 returned.\r
214\r
215**/\r
216EFI_STATUS\r
217EFIAPI\r
218FvbProtocolGetAttributes (\r
949b0f3b 219 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r
670d495b 220 OUT EFI_FVB_ATTRIBUTES_2 *Attributes\r
221 )\r
222{\r
223 *Attributes =\r
224 (EFI_FVB_ATTRIBUTES_2) (\r
225 EFI_FVB2_READ_ENABLED_CAP |\r
226 EFI_FVB2_READ_STATUS |\r
227 EFI_FVB2_WRITE_ENABLED_CAP |\r
228 EFI_FVB2_WRITE_STATUS |\r
229 EFI_FVB2_ERASE_POLARITY\r
230 );\r
231\r
232 return EFI_SUCCESS;\r
233}\r
234\r
235\r
236/**\r
237 The SetAttributes() function sets configurable firmware volume\r
238 attributes and returns the new settings of the firmware volume.\r
239\r
949b0f3b 240 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r
670d495b 241\r
242 @param Attributes On input, Attributes is a pointer to\r
243 EFI_FVB_ATTRIBUTES_2 that contains the\r
244 desired firmware volume settings. On\r
245 successful return, it contains the new\r
246 settings of the firmware volume. Type\r
247 EFI_FVB_ATTRIBUTES_2 is defined in\r
248 EFI_FIRMWARE_VOLUME_HEADER.\r
89f385ce 249\r
670d495b 250 @retval EFI_SUCCESS The firmware volume attributes were returned.\r
251\r
252 @retval EFI_INVALID_PARAMETER The attributes requested are in\r
253 conflict with the capabilities\r
254 as declared in the firmware\r
255 volume header.\r
256\r
257**/\r
258EFI_STATUS\r
259EFIAPI\r
260FvbProtocolSetAttributes (\r
949b0f3b 261 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r
670d495b 262 IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes\r
263 )\r
264{\r
265 return EFI_ACCESS_DENIED;\r
266}\r
267\r
268\r
269/**\r
270 Erases and initializes a firmware volume block.\r
271\r
272 The EraseBlocks() function erases one or more blocks as denoted\r
273 by the variable argument list. The entire parameter list of\r
274 blocks must be verified before erasing any blocks. If a block is\r
275 requested that does not exist within the associated firmware\r
276 volume (it has a larger index than the last block of the\r
277 firmware volume), the EraseBlocks() function must return the\r
278 status code EFI_INVALID_PARAMETER without modifying the contents\r
279 of the firmware volume. Implementations should be mindful that\r
280 the firmware volume might be in the WriteDisabled state. If it\r
281 is in this state, the EraseBlocks() function must return the\r
282 status code EFI_ACCESS_DENIED without modifying the contents of\r
283 the firmware volume. All calls to EraseBlocks() must be fully\r
284 flushed to the hardware before the EraseBlocks() service\r
285 returns.\r
286\r
949b0f3b 287 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL\r
670d495b 288 instance.\r
289\r
290 @param ... The variable argument list is a list of tuples.\r
291 Each tuple describes a range of LBAs to erase\r
292 and consists of the following:\r
293 - An EFI_LBA that indicates the starting LBA\r
294 - A UINTN that indicates the number of blocks to\r
295 erase\r
296\r
297 The list is terminated with an\r
298 EFI_LBA_LIST_TERMINATOR. For example, the\r
299 following indicates that two ranges of blocks\r
300 (5-7 and 10-11) are to be erased: EraseBlocks\r
301 (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);\r
302\r
303 @retval EFI_SUCCESS The erase request was successfully\r
304 completed.\r
89f385ce 305\r
670d495b 306 @retval EFI_ACCESS_DENIED The firmware volume is in the\r
307 WriteDisabled state.\r
308 @retval EFI_DEVICE_ERROR The block device is not functioning\r
309 correctly and could not be written.\r
310 The firmware device may have been\r
311 partially erased.\r
312 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed\r
313 in the variable argument list do\r
89f385ce 314 not exist in the firmware volume.\r
670d495b 315\r
316**/\r
317EFI_STATUS\r
318EFIAPI\r
319FvbProtocolEraseBlocks (\r
949b0f3b 320 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r
670d495b 321 ...\r
322 )\r
323{\r
324 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
325 VA_LIST args;\r
326 EFI_LBA StartingLba;\r
327 UINTN NumOfLba;\r
328 UINT8 Erase;\r
329 VOID *ErasePtr;\r
330 UINTN EraseSize;\r
331\r
332 FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
333 Erase = 0;\r
334\r
335 VA_START (args, This);\r
336\r
337 do {\r
338 StartingLba = VA_ARG (args, EFI_LBA);\r
339 if (StartingLba == EFI_LBA_LIST_TERMINATOR) {\r
340 break;\r
341 }\r
342\r
9c4eef65 343 NumOfLba = VA_ARG (args, UINTN);\r
670d495b 344\r
345 //\r
346 // Check input parameters\r
347 //\r
348 if ((NumOfLba == 0) || (StartingLba > 1) || ((StartingLba + NumOfLba) > 2)) {\r
349 VA_END (args);\r
350 return EFI_INVALID_PARAMETER;\r
351 }\r
352\r
353 if (StartingLba == 0) {\r
354 Erase = (UINT8) (Erase | BIT0);\r
355 }\r
356 if ((StartingLba + NumOfLba) == 2) {\r
357 Erase = (UINT8) (Erase | BIT1);\r
358 }\r
359\r
360 } while (1);\r
361\r
362 VA_END (args);\r
363\r
364 ErasePtr = (UINT8*) FvbDevice->BufferPtr;\r
365 EraseSize = 0;\r
366\r
367 if ((Erase & BIT0) != 0) {\r
368 EraseSize = EraseSize + FvbDevice->BlockSize;\r
369 } else {\r
02e12420 370 ErasePtr = (VOID*) ((UINT8*)ErasePtr + FvbDevice->BlockSize);\r
670d495b 371 }\r
372\r
373 if ((Erase & BIT1) != 0) {\r
374 EraseSize = EraseSize + FvbDevice->BlockSize;\r
375 }\r
376\r
377 if (EraseSize != 0) {\r
378 SetMem (\r
379 (VOID*) ErasePtr,\r
380 EraseSize,\r
381 ERASED_UINT8\r
382 );\r
3a4ddfc5 383 VA_START (args, This);\r
384 PlatformFvbBlocksErased (This, args);\r
3bbe68a3 385 VA_END (args);\r
670d495b 386 }\r
387\r
388 return EFI_SUCCESS;\r
389}\r
390\r
391\r
392/**\r
393 Writes the specified number of bytes from the input buffer to the block.\r
394\r
395 The Write() function writes the specified number of bytes from\r
396 the provided buffer to the specified block and offset. If the\r
397 firmware volume is sticky write, the caller must ensure that\r
398 all the bits of the specified range to write are in the\r
399 EFI_FVB_ERASE_POLARITY state before calling the Write()\r
400 function, or else the result will be unpredictable. This\r
401 unpredictability arises because, for a sticky-write firmware\r
402 volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY\r
403 state but cannot flip it back again. In general, before\r
404 calling the Write() function, the caller should call the\r
405 EraseBlocks() function first to erase the specified block to\r
406 write. A block erase cycle will transition bits from the\r
407 (NOT)EFI_FVB_ERASE_POLARITY state back to the\r
408 EFI_FVB_ERASE_POLARITY state. Implementations should be\r
409 mindful that the firmware volume might be in the WriteDisabled\r
410 state. If it is in this state, the Write() function must\r
411 return the status code EFI_ACCESS_DENIED without modifying the\r
412 contents of the firmware volume. The Write() function must\r
413 also prevent spanning block boundaries. If a write is\r
414 requested that spans a block boundary, the write must store up\r
415 to the boundary but not beyond. The output parameter NumBytes\r
416 must be set to correctly indicate the number of bytes actually\r
417 written. The caller must be aware that a write may be\r
418 partially completed. All writes, partial or otherwise, must be\r
419 fully flushed to the hardware before the Write() service\r
420 returns.\r
421\r
949b0f3b 422 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r
89f385ce 423\r
670d495b 424 @param Lba The starting logical block index to write to.\r
89f385ce 425\r
670d495b 426 @param Offset Offset into the block at which to begin writing.\r
89f385ce 427\r
670d495b 428 @param NumBytes Pointer to a UINTN. At entry, *NumBytes\r
429 contains the total size of the buffer. At\r
430 exit, *NumBytes contains the total number of\r
431 bytes actually written.\r
89f385ce 432\r
670d495b 433 @param Buffer Pointer to a caller-allocated buffer that\r
434 contains the source for the write.\r
89f385ce 435\r
670d495b 436 @retval EFI_SUCCESS The firmware volume was written successfully.\r
89f385ce 437\r
670d495b 438 @retval EFI_BAD_BUFFER_SIZE The write was attempted across an\r
439 LBA boundary. On output, NumBytes\r
440 contains the total number of bytes\r
441 actually written.\r
89f385ce 442\r
670d495b 443 @retval EFI_ACCESS_DENIED The firmware volume is in the\r
444 WriteDisabled state.\r
89f385ce 445\r
670d495b 446 @retval EFI_DEVICE_ERROR The block device is malfunctioning\r
447 and could not be written.\r
448\r
449\r
450**/\r
451EFI_STATUS\r
452EFIAPI\r
453FvbProtocolWrite (\r
949b0f3b 454 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r
670d495b 455 IN EFI_LBA Lba,\r
456 IN UINTN Offset,\r
457 IN OUT UINTN *NumBytes,\r
458 IN UINT8 *Buffer\r
459 )\r
460{\r
461\r
462 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
463 UINT8 *FvbDataPtr;\r
464\r
465 FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
466\r
467 if ((Lba > 1) || (Offset > FvbDevice->BlockSize)) {\r
468 return EFI_INVALID_PARAMETER;\r
469 }\r
470\r
471 if ((Offset + *NumBytes) > FvbDevice->BlockSize) {\r
472 *NumBytes = FvbDevice->BlockSize - Offset;\r
473 }\r
474\r
475 FvbDataPtr =\r
476 (UINT8*) FvbDevice->BufferPtr +\r
02e12420 477 MultU64x32 (Lba, (UINT32) FvbDevice->BlockSize) +\r
670d495b 478 Offset;\r
479\r
480 if (*NumBytes > 0) {\r
481 CopyMem (FvbDataPtr, Buffer, *NumBytes);\r
9529d0ff 482 PlatformFvbDataWritten (This, Lba, Offset, *NumBytes, Buffer);\r
670d495b 483 }\r
484\r
485 return EFI_SUCCESS;\r
486}\r
487\r
488\r
489/**\r
490 Reads the specified number of bytes into a buffer from the specified block.\r
491\r
492 The Read() function reads the requested number of bytes from the\r
493 requested block and stores them in the provided buffer.\r
494 Implementations should be mindful that the firmware volume\r
495 might be in the ReadDisabled state. If it is in this state,\r
496 the Read() function must return the status code\r
497 EFI_ACCESS_DENIED without modifying the contents of the\r
498 buffer. The Read() function must also prevent spanning block\r
499 boundaries. If a read is requested that would span a block\r
500 boundary, the read must read up to the boundary but not\r
501 beyond. The output parameter NumBytes must be set to correctly\r
502 indicate the number of bytes actually read. The caller must be\r
503 aware that a read may be partially completed.\r
504\r
949b0f3b 505 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r
89f385ce 506\r
670d495b 507 @param Lba The starting logical block index\r
508 from which to read.\r
509\r
510 @param Offset Offset into the block at which to begin reading.\r
511\r
512 @param NumBytes Pointer to a UINTN. At entry, *NumBytes\r
513 contains the total size of the buffer. At\r
514 exit, *NumBytes contains the total number of\r
515 bytes read.\r
516\r
517 @param Buffer Pointer to a caller-allocated buffer that will\r
518 be used to hold the data that is read.\r
519\r
520 @retval EFI_SUCCESS The firmware volume was read successfully\r
521 and contents are in Buffer.\r
89f385ce 522\r
670d495b 523 @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA\r
524 boundary. On output, NumBytes\r
525 contains the total number of bytes\r
526 returned in Buffer.\r
89f385ce 527\r
670d495b 528 @retval EFI_ACCESS_DENIED The firmware volume is in the\r
529 ReadDisabled state.\r
89f385ce 530\r
670d495b 531 @retval EFI_DEVICE_ERROR The block device is not\r
532 functioning correctly and could\r
533 not be read.\r
534\r
535**/\r
536EFI_STATUS\r
537EFIAPI\r
538FvbProtocolRead (\r
949b0f3b 539 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r
670d495b 540 IN EFI_LBA Lba,\r
541 IN UINTN Offset,\r
542 IN OUT UINTN *NumBytes,\r
543 IN OUT UINT8 *Buffer\r
544 )\r
545{\r
546 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
547 UINT8 *FvbDataPtr;\r
548\r
549 FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
550\r
551 if ((Lba > 1) || (Offset > FvbDevice->BlockSize)) {\r
552 return EFI_INVALID_PARAMETER;\r
553 }\r
554\r
555 if ((Offset + *NumBytes) > FvbDevice->BlockSize) {\r
556 *NumBytes = FvbDevice->BlockSize - Offset;\r
557 }\r
558\r
559 FvbDataPtr =\r
560 (UINT8*) FvbDevice->BufferPtr +\r
02e12420 561 MultU64x32 (Lba, (UINT32) FvbDevice->BlockSize) +\r
670d495b 562 Offset;\r
563\r
564 if (*NumBytes > 0) {\r
565 CopyMem (Buffer, FvbDataPtr, *NumBytes);\r
3a4ddfc5 566 PlatformFvbDataRead (This, Lba, Offset, *NumBytes, Buffer);\r
670d495b 567 }\r
568\r
569 return EFI_SUCCESS;\r
570}\r
571\r
572\r
573/**\r
574 Check the integrity of firmware volume header.\r
575\r
576 @param[in] FwVolHeader - A pointer to a firmware volume header\r
577\r
578 @retval EFI_SUCCESS - The firmware volume is consistent\r
579 @retval EFI_NOT_FOUND - The firmware volume has been corrupted.\r
580\r
581**/\r
582EFI_STATUS\r
583ValidateFvHeader (\r
584 IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader\r
585 )\r
586{\r
587 UINT16 Checksum;\r
588\r
589 //\r
590 // Verify the header revision, header signature, length\r
591 // Length of FvBlock cannot be 2**64-1\r
592 // HeaderLength cannot be an odd number\r
593 //\r
594 if ((FwVolHeader->Revision != EFI_FVH_REVISION) ||\r
595 (FwVolHeader->Signature != EFI_FVH_SIGNATURE) ||\r
596 (FwVolHeader->FvLength != EMU_FVB_SIZE) ||\r
597 (FwVolHeader->HeaderLength != EMU_FV_HEADER_LENGTH)\r
598 ) {\r
599 DEBUG ((EFI_D_INFO, "EMU Variable FVB: Basic FV headers were invalid\n"));\r
600 return EFI_NOT_FOUND;\r
601 }\r
602 //\r
603 // Verify the header checksum\r
604 //\r
605 Checksum = CalculateSum16((VOID*) FwVolHeader, FwVolHeader->HeaderLength);\r
606\r
607 if (Checksum != 0) {\r
608 DEBUG ((EFI_D_INFO, "EMU Variable FVB: FV checksum was invalid\n"));\r
609 return EFI_NOT_FOUND;\r
610 }\r
611\r
612 return EFI_SUCCESS;\r
613}\r
614\r
615\r
616/**\r
617 Initializes the FV Header and Variable Store Header\r
618 to support variable operations.\r
619\r
620 @param[in] Ptr - Location to initialize the headers\r
621\r
622**/\r
623VOID\r
624InitializeFvAndVariableStoreHeaders (\r
625 IN VOID *Ptr\r
626 )\r
627{\r
bdf93df8 628 //\r
629 // Templates for authenticated variable FV header\r
630 //\r
631 STATIC FVB_FV_HDR_AND_VARS_TEMPLATE FvAndAuthenticatedVarTemplate = {\r
632 { // EFI_FIRMWARE_VOLUME_HEADER FvHdr;\r
633 // UINT8 ZeroVector[16];\r
634 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },\r
635\r
636 // EFI_GUID FileSystemGuid;\r
637 EFI_SYSTEM_NV_DATA_FV_GUID,\r
638\r
639 // UINT64 FvLength;\r
640 EMU_FVB_SIZE,\r
641\r
642 // UINT32 Signature;\r
643 EFI_FVH_SIGNATURE,\r
644\r
645 // EFI_FVB_ATTRIBUTES_2 Attributes;\r
646 0x4feff,\r
647\r
648 // UINT16 HeaderLength;\r
649 EMU_FV_HEADER_LENGTH,\r
650\r
651 // UINT16 Checksum;\r
652 0,\r
653\r
654 // UINT16 ExtHeaderOffset;\r
655 0,\r
656\r
657 // UINT8 Reserved[1];\r
ce68d3bc 658 {0},\r
bdf93df8 659\r
660 // UINT8 Revision;\r
661 EFI_FVH_REVISION,\r
662\r
663 // EFI_FV_BLOCK_MAP_ENTRY BlockMap[1];\r
ce68d3bc
SZ
664 {\r
665 {\r
666 2, // UINT32 NumBlocks;\r
667 EMU_FVB_BLOCK_SIZE // UINT32 Length;\r
668 }\r
bdf93df8 669 }\r
670 },\r
671 // EFI_FV_BLOCK_MAP_ENTRY EndBlockMap;\r
672 { 0, 0 }, // End of block map\r
673 { // VARIABLE_STORE_HEADER VarHdr;\r
674 // EFI_GUID Signature; // need authenticated variables for secure boot\r
675 EFI_AUTHENTICATED_VARIABLE_GUID,\r
676\r
677 // UINT32 Size;\r
678 (\r
679 FixedPcdGet32 (PcdVariableStoreSize) -\r
680 OFFSET_OF (FVB_FV_HDR_AND_VARS_TEMPLATE, VarHdr)\r
681 ),\r
682\r
683 // UINT8 Format;\r
684 VARIABLE_STORE_FORMATTED,\r
685\r
686 // UINT8 State;\r
687 VARIABLE_STORE_HEALTHY,\r
688\r
689 // UINT16 Reserved;\r
690 0,\r
691\r
692 // UINT32 Reserved1;\r
693 0\r
694 }\r
695 };\r
696\r
670d495b 697 EFI_FIRMWARE_VOLUME_HEADER *Fv;\r
698\r
699 //\r
700 // Copy the template structure into the location\r
701 //\r
6d7af0c9
LE
702 CopyMem (\r
703 Ptr,\r
704 &FvAndAuthenticatedVarTemplate,\r
705 sizeof FvAndAuthenticatedVarTemplate\r
706 );\r
670d495b 707\r
708 //\r
709 // Update the checksum for the FV header\r
710 //\r
711 Fv = (EFI_FIRMWARE_VOLUME_HEADER*) Ptr;\r
712 Fv->Checksum = CalculateCheckSum16 (Ptr, Fv->HeaderLength);\r
713}\r
714\r
670d495b 715/**\r
716 Main entry point.\r
717\r
89f385ce 718 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
670d495b 719 @param[in] SystemTable A pointer to the EFI System Table.\r
89f385ce 720\r
670d495b 721 @retval EFI_SUCCESS Successfully initialized.\r
722\r
723**/\r
724EFI_STATUS\r
725EFIAPI\r
726FvbInitialize (\r
727 IN EFI_HANDLE ImageHandle,\r
728 IN EFI_SYSTEM_TABLE *SystemTable\r
729 )\r
730{\r
731 EFI_STATUS Status;\r
732 VOID *Ptr;\r
733 VOID *SubPtr;\r
734 BOOLEAN Initialize;\r
735 EFI_HANDLE Handle;\r
736 EFI_PHYSICAL_ADDRESS Address;\r
6348eea6 737 RETURN_STATUS PcdStatus;\r
670d495b 738\r
739 DEBUG ((EFI_D_INFO, "EMU Variable FVB Started\n"));\r
740\r
741 //\r
742 // Verify that the PCD's are set correctly.\r
743 //\r
744 if (\r
29a3f139 745 (PcdGet32 (PcdVariableStoreSize) +\r
746 PcdGet32 (PcdFlashNvStorageFtwWorkingSize)\r
670d495b 747 ) >\r
748 EMU_FVB_BLOCK_SIZE\r
749 ) {\r
750 DEBUG ((EFI_D_ERROR, "EMU Variable invalid PCD sizes\n"));\r
751 return EFI_INVALID_PARAMETER;\r
752 }\r
753\r
4313b26d
JJ
754 if (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0) {\r
755 DEBUG ((EFI_D_INFO, "Disabling EMU Variable FVB since "\r
756 "flash variables appear to be supported.\n"));\r
757 return EFI_ABORTED;\r
758 }\r
759\r
670d495b 760 //\r
761 // By default we will initialize the FV contents. But, if\r
762 // PcdEmuVariableNvStoreReserved is non-zero, then we will\r
763 // use this location for our buffer.\r
764 //\r
765 // If this location does not have a proper FV header, then\r
766 // we will initialize it.\r
767 //\r
768 Initialize = TRUE;\r
769 if (PcdGet64 (PcdEmuVariableNvStoreReserved) != 0) {\r
770 Ptr = (VOID*)(UINTN) PcdGet64 (PcdEmuVariableNvStoreReserved);\r
771 DEBUG ((\r
772 EFI_D_INFO,\r
773 "EMU Variable FVB: Using pre-reserved block at %p\n",\r
774 Ptr\r
775 ));\r
776 Status = ValidateFvHeader (Ptr);\r
777 if (!EFI_ERROR (Status)) {\r
778 DEBUG ((EFI_D_INFO, "EMU Variable FVB: Found valid pre-existing FV\n"));\r
779 Initialize = FALSE;\r
780 }\r
781 } else {\r
782 Ptr = AllocateAlignedRuntimePages (\r
783 EFI_SIZE_TO_PAGES (EMU_FVB_SIZE),\r
784 SIZE_64KB\r
785 );\r
786 }\r
787\r
788 mEmuVarsFvb.BufferPtr = Ptr;\r
789\r
790 //\r
791 // Initialize the main FV header and variable store header\r
792 //\r
793 if (Initialize) {\r
794 SetMem (Ptr, EMU_FVB_SIZE, ERASED_UINT8);\r
795 InitializeFvAndVariableStoreHeaders (Ptr);\r
796 }\r
6348eea6
LE
797 PcdStatus = PcdSet64S (PcdFlashNvStorageVariableBase64, (UINT32)(UINTN) Ptr);\r
798 ASSERT_RETURN_ERROR (PcdStatus);\r
670d495b 799\r
800 //\r
801 // Initialize the Fault Tolerant Write data area\r
802 //\r
29a3f139 803 SubPtr = (VOID*) ((UINT8*) Ptr + PcdGet32 (PcdVariableStoreSize));\r
6348eea6
LE
804 PcdStatus = PcdSet32S (PcdFlashNvStorageFtwWorkingBase,\r
805 (UINT32)(UINTN) SubPtr);\r
806 ASSERT_RETURN_ERROR (PcdStatus);\r
670d495b 807\r
808 //\r
809 // Initialize the Fault Tolerant Write spare block\r
810 //\r
811 SubPtr = (VOID*) ((UINT8*) Ptr + EMU_FVB_BLOCK_SIZE);\r
6348eea6
LE
812 PcdStatus = PcdSet32S (PcdFlashNvStorageFtwSpareBase,\r
813 (UINT32)(UINTN) SubPtr);\r
814 ASSERT_RETURN_ERROR (PcdStatus);\r
670d495b 815\r
816 //\r
817 // Setup FVB device path\r
818 //\r
819 Address = (EFI_PHYSICAL_ADDRESS)(UINTN) Ptr;\r
820 mEmuVarsFvb.DevicePath.MemMapDevPath.StartingAddress = Address;\r
821 mEmuVarsFvb.DevicePath.MemMapDevPath.EndingAddress = Address + EMU_FVB_SIZE - 1;\r
822\r
823 //\r
824 // Install the protocols\r
825 //\r
826 DEBUG ((EFI_D_INFO, "Installing FVB for EMU Variable support\n"));\r
827 Handle = 0;\r
828 Status = gBS->InstallMultipleProtocolInterfaces (\r
829 &Handle,\r
949b0f3b 830 &gEfiFirmwareVolumeBlock2ProtocolGuid,\r
670d495b 831 &mEmuVarsFvb.FwVolBlockInstance,\r
832 &gEfiDevicePathProtocolGuid,\r
833 &mEmuVarsFvb.DevicePath,\r
834 NULL\r
835 );\r
836 ASSERT_EFI_ERROR (Status);\r
837\r
838 //\r
839 // Register for the virtual address change event\r
840 //\r
841 Status = gBS->CreateEventEx (\r
842 EVT_NOTIFY_SIGNAL,\r
843 TPL_NOTIFY,\r
844 FvbVirtualAddressChangeEvent,\r
845 NULL,\r
846 &gEfiEventVirtualAddressChangeGuid,\r
847 &mEmuVarsFvbAddrChangeEvent\r
848 );\r
849 ASSERT_EFI_ERROR (Status);\r
850\r
851 return EFI_SUCCESS;\r
852}\r
853\r
854\r