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