]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - OvmfPkg/EmuVariableFvbRuntimeDxe/Fvb.c
Add missing parameter in functions header.
[mirror_edk2.git] / OvmfPkg / EmuVariableFvbRuntimeDxe / Fvb.c
... / ...
CommitLineData
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
5 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
6 This program and the accompanying materials\r
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#define EFI_AUTHENTICATED_VARIABLE_GUID \\r
38{ 0xaaf32c78, 0x947b, 0x439a, { 0xa1, 0x80, 0x2e, 0x14, 0x4e, 0xc3, 0x77, 0x92 } }\r
39\r
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
122 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r
123 \r
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
128 \r
129 @retval EFI_SUCCESS The firmware volume base address is returned.\r
130 \r
131 @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.\r
132\r
133**/\r
134EFI_STATUS\r
135EFIAPI\r
136FvbProtocolGetPhysicalAddress (\r
137 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r
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
158 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r
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
171 \r
172 @retval EFI_SUCCESS The firmware volume base address is returned.\r
173 \r
174 @retval EFI_INVALID_PARAMETER The requested LBA is out of range.\r
175\r
176**/\r
177EFI_STATUS\r
178EFIAPI\r
179FvbProtocolGetBlockSize (\r
180 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r
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
195 *NumberOfBlocks = (UINTN) (2 - (UINTN) Lba);\r
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
205 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r
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
219 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r
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
240 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r
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
249 \r
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
261 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r
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
287 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL\r
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
305 \r
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
314 not exist in the firmware volume. \r
315\r
316**/\r
317EFI_STATUS\r
318EFIAPI\r
319FvbProtocolEraseBlocks (\r
320 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r
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
343 NumOfLba = VA_ARG (args, UINT32);\r
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
370 ErasePtr = (VOID*) ((UINT8*)ErasePtr + FvbDevice->BlockSize);\r
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
383 VA_START (args, This);\r
384 PlatformFvbBlocksErased (This, args);\r
385 VA_END (args);\r
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
422 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r
423 \r
424 @param Lba The starting logical block index to write to.\r
425 \r
426 @param Offset Offset into the block at which to begin writing.\r
427 \r
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
432 \r
433 @param Buffer Pointer to a caller-allocated buffer that\r
434 contains the source for the write.\r
435 \r
436 @retval EFI_SUCCESS The firmware volume was written successfully.\r
437 \r
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
442 \r
443 @retval EFI_ACCESS_DENIED The firmware volume is in the\r
444 WriteDisabled state.\r
445 \r
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
454 IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r
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
477 MultU64x32 (Lba, (UINT32) FvbDevice->BlockSize) +\r
478 Offset;\r
479\r
480 if (*NumBytes > 0) {\r
481 CopyMem (FvbDataPtr, Buffer, *NumBytes);\r
482 PlatformFvbDataWritten (This, Lba, Offset, *NumBytes, Buffer);\r
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
505 @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r
506 \r
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
522 \r
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
527 \r
528 @retval EFI_ACCESS_DENIED The firmware volume is in the\r
529 ReadDisabled state.\r
530 \r
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
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 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
561 MultU64x32 (Lba, (UINT32) FvbDevice->BlockSize) +\r
562 Offset;\r
563\r
564 if (*NumBytes > 0) {\r
565 CopyMem (Buffer, FvbDataPtr, *NumBytes);\r
566 PlatformFvbDataRead (This, Lba, Offset, *NumBytes, Buffer);\r
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
628 //\r
629 // Templates for standard (non-authenticated) variable FV header\r
630 //\r
631 STATIC FVB_FV_HDR_AND_VARS_TEMPLATE FvAndVarTemplate = {\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
658 0,\r
659\r
660 // UINT8 Revision;\r
661 EFI_FVH_REVISION,\r
662\r
663 // EFI_FV_BLOCK_MAP_ENTRY BlockMap[1];\r
664 { 2, // UINT32 NumBlocks;\r
665 EMU_FVB_BLOCK_SIZE // UINT32 Length;\r
666 }\r
667 },\r
668 // EFI_FV_BLOCK_MAP_ENTRY EndBlockMap;\r
669 { 0, 0 }, // End of block map\r
670 { // VARIABLE_STORE_HEADER VarHdr;\r
671 // EFI_GUID Signature;\r
672 EFI_VARIABLE_GUID,\r
673\r
674 // UINT32 Size;\r
675 (\r
676 FixedPcdGet32 (PcdVariableStoreSize) -\r
677 OFFSET_OF (FVB_FV_HDR_AND_VARS_TEMPLATE, VarHdr)\r
678 ),\r
679\r
680 // UINT8 Format;\r
681 VARIABLE_STORE_FORMATTED,\r
682\r
683 // UINT8 State;\r
684 VARIABLE_STORE_HEALTHY,\r
685\r
686 // UINT16 Reserved;\r
687 0,\r
688\r
689 // UINT32 Reserved1;\r
690 0\r
691 }\r
692 };\r
693\r
694 //\r
695 // Templates for authenticated variable FV header\r
696 //\r
697 STATIC FVB_FV_HDR_AND_VARS_TEMPLATE FvAndAuthenticatedVarTemplate = {\r
698 { // EFI_FIRMWARE_VOLUME_HEADER FvHdr;\r
699 // UINT8 ZeroVector[16];\r
700 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },\r
701\r
702 // EFI_GUID FileSystemGuid;\r
703 EFI_SYSTEM_NV_DATA_FV_GUID,\r
704\r
705 // UINT64 FvLength;\r
706 EMU_FVB_SIZE,\r
707\r
708 // UINT32 Signature;\r
709 EFI_FVH_SIGNATURE,\r
710\r
711 // EFI_FVB_ATTRIBUTES_2 Attributes;\r
712 0x4feff,\r
713\r
714 // UINT16 HeaderLength;\r
715 EMU_FV_HEADER_LENGTH,\r
716\r
717 // UINT16 Checksum;\r
718 0,\r
719\r
720 // UINT16 ExtHeaderOffset;\r
721 0,\r
722\r
723 // UINT8 Reserved[1];\r
724 0,\r
725\r
726 // UINT8 Revision;\r
727 EFI_FVH_REVISION,\r
728\r
729 // EFI_FV_BLOCK_MAP_ENTRY BlockMap[1];\r
730 { 2, // UINT32 NumBlocks;\r
731 EMU_FVB_BLOCK_SIZE // UINT32 Length;\r
732 }\r
733 },\r
734 // EFI_FV_BLOCK_MAP_ENTRY EndBlockMap;\r
735 { 0, 0 }, // End of block map\r
736 { // VARIABLE_STORE_HEADER VarHdr;\r
737 // EFI_GUID Signature; // need authenticated variables for secure boot\r
738 EFI_AUTHENTICATED_VARIABLE_GUID,\r
739\r
740 // UINT32 Size;\r
741 (\r
742 FixedPcdGet32 (PcdVariableStoreSize) -\r
743 OFFSET_OF (FVB_FV_HDR_AND_VARS_TEMPLATE, VarHdr)\r
744 ),\r
745\r
746 // UINT8 Format;\r
747 VARIABLE_STORE_FORMATTED,\r
748\r
749 // UINT8 State;\r
750 VARIABLE_STORE_HEALTHY,\r
751\r
752 // UINT16 Reserved;\r
753 0,\r
754\r
755 // UINT32 Reserved1;\r
756 0\r
757 }\r
758 };\r
759\r
760 EFI_FIRMWARE_VOLUME_HEADER *Fv;\r
761\r
762 //\r
763 // Copy the template structure into the location\r
764 //\r
765 if (FeaturePcdGet (PcdSecureBootEnable) == FALSE) {\r
766 CopyMem (Ptr, (VOID*)&FvAndVarTemplate, sizeof (FvAndVarTemplate));\r
767 } else {\r
768 CopyMem (Ptr, (VOID*)&FvAndAuthenticatedVarTemplate, sizeof (FvAndAuthenticatedVarTemplate));\r
769 }\r
770\r
771 //\r
772 // Update the checksum for the FV header\r
773 //\r
774 Fv = (EFI_FIRMWARE_VOLUME_HEADER*) Ptr;\r
775 Fv->Checksum = CalculateCheckSum16 (Ptr, Fv->HeaderLength);\r
776}\r
777\r
778\r
779/**\r
780 Initializes the Fault Tolerant Write data structure\r
781\r
782 This data structure is used by the Fault Tolerant Write driver.\r
783\r
784 @param[in] Buffer - Location for the FTW data structure\r
785\r
786**/\r
787VOID\r
788InitializeFtwState (\r
789 IN VOID *Buffer\r
790 )\r
791{\r
792 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *Hdr;\r
793 UINT32 TempCrc;\r
794 STATIC EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER DefaultFtw = {\r
795 EFI_SYSTEM_NV_DATA_FV_GUID, // EFI_GUID Signature;\r
796 ERASED_UINT32, // UINT32 Crc;\r
797 ERASED_BIT, // UINT8 WorkingBlockValid : 1;\r
798 ERASED_BIT, // UINT8 WorkingBlockInvalid : 1;\r
799 0, // UINT8 Reserved : 6;\r
800 { 0, 0, 0 }, // UINT8 Reserved3[3];\r
801 FTW_WRITE_QUEUE_SIZE // UINT64 WriteQueueSize;\r
802 };\r
803\r
804 CopyMem (Buffer, (VOID*) &DefaultFtw, sizeof (DefaultFtw));\r
805\r
806 Hdr = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER*) Buffer;\r
807\r
808 //\r
809 // Calculate checksum.\r
810 //\r
811 // The Crc, WorkingBlockValid and WorkingBlockInvalid bits should\r
812 // be set to the erased state before computing the checksum.\r
813 //\r
814 gBS->CalculateCrc32 (Buffer, sizeof (DefaultFtw), &TempCrc);\r
815 Hdr->Crc = TempCrc;\r
816\r
817 //\r
818 // Mark as valid.\r
819 //\r
820 Hdr->WorkingBlockValid = NOT_ERASED_BIT;\r
821}\r
822\r
823\r
824/**\r
825 Main entry point.\r
826\r
827 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
828 @param[in] SystemTable A pointer to the EFI System Table.\r
829 \r
830 @retval EFI_SUCCESS Successfully initialized.\r
831\r
832**/\r
833EFI_STATUS\r
834EFIAPI\r
835FvbInitialize (\r
836 IN EFI_HANDLE ImageHandle,\r
837 IN EFI_SYSTEM_TABLE *SystemTable\r
838 )\r
839{\r
840 EFI_STATUS Status;\r
841 VOID *Ptr;\r
842 VOID *SubPtr;\r
843 BOOLEAN Initialize;\r
844 EFI_HANDLE Handle;\r
845 EFI_PHYSICAL_ADDRESS Address;\r
846\r
847 DEBUG ((EFI_D_INFO, "EMU Variable FVB Started\n"));\r
848\r
849 //\r
850 // Verify that the PCD's are set correctly.\r
851 //\r
852 if (\r
853 (PcdGet32 (PcdVariableStoreSize) +\r
854 PcdGet32 (PcdFlashNvStorageFtwWorkingSize)\r
855 ) >\r
856 EMU_FVB_BLOCK_SIZE\r
857 ) {\r
858 DEBUG ((EFI_D_ERROR, "EMU Variable invalid PCD sizes\n"));\r
859 return EFI_INVALID_PARAMETER;\r
860 }\r
861\r
862 //\r
863 // By default we will initialize the FV contents. But, if\r
864 // PcdEmuVariableNvStoreReserved is non-zero, then we will\r
865 // use this location for our buffer.\r
866 //\r
867 // If this location does not have a proper FV header, then\r
868 // we will initialize it.\r
869 //\r
870 Initialize = TRUE;\r
871 if (PcdGet64 (PcdEmuVariableNvStoreReserved) != 0) {\r
872 Ptr = (VOID*)(UINTN) PcdGet64 (PcdEmuVariableNvStoreReserved);\r
873 DEBUG ((\r
874 EFI_D_INFO,\r
875 "EMU Variable FVB: Using pre-reserved block at %p\n",\r
876 Ptr\r
877 ));\r
878 Status = ValidateFvHeader (Ptr);\r
879 if (!EFI_ERROR (Status)) {\r
880 DEBUG ((EFI_D_INFO, "EMU Variable FVB: Found valid pre-existing FV\n"));\r
881 Initialize = FALSE;\r
882 }\r
883 } else {\r
884 Ptr = AllocateAlignedRuntimePages (\r
885 EFI_SIZE_TO_PAGES (EMU_FVB_SIZE),\r
886 SIZE_64KB\r
887 );\r
888 }\r
889\r
890 mEmuVarsFvb.BufferPtr = Ptr;\r
891\r
892 //\r
893 // Initialize the main FV header and variable store header\r
894 //\r
895 if (Initialize) {\r
896 SetMem (Ptr, EMU_FVB_SIZE, ERASED_UINT8);\r
897 InitializeFvAndVariableStoreHeaders (Ptr);\r
898 }\r
899 PcdSet64 (PcdFlashNvStorageVariableBase64, (UINT32)(UINTN) Ptr);\r
900\r
901 //\r
902 // Initialize the Fault Tolerant Write data area\r
903 //\r
904 SubPtr = (VOID*) ((UINT8*) Ptr + PcdGet32 (PcdVariableStoreSize));\r
905 if (Initialize) {\r
906 InitializeFtwState (SubPtr);\r
907 }\r
908 PcdSet32 (PcdFlashNvStorageFtwWorkingBase, (UINT32)(UINTN) SubPtr);\r
909\r
910 //\r
911 // Initialize the Fault Tolerant Write spare block\r
912 //\r
913 SubPtr = (VOID*) ((UINT8*) Ptr + EMU_FVB_BLOCK_SIZE);\r
914 PcdSet32 (PcdFlashNvStorageFtwSpareBase, (UINT32)(UINTN) SubPtr);\r
915\r
916 //\r
917 // Setup FVB device path\r
918 //\r
919 Address = (EFI_PHYSICAL_ADDRESS)(UINTN) Ptr;\r
920 mEmuVarsFvb.DevicePath.MemMapDevPath.StartingAddress = Address;\r
921 mEmuVarsFvb.DevicePath.MemMapDevPath.EndingAddress = Address + EMU_FVB_SIZE - 1;\r
922\r
923 //\r
924 // Install the protocols\r
925 //\r
926 DEBUG ((EFI_D_INFO, "Installing FVB for EMU Variable support\n"));\r
927 Handle = 0;\r
928 Status = gBS->InstallMultipleProtocolInterfaces (\r
929 &Handle,\r
930 &gEfiFirmwareVolumeBlock2ProtocolGuid,\r
931 &mEmuVarsFvb.FwVolBlockInstance,\r
932 &gEfiDevicePathProtocolGuid,\r
933 &mEmuVarsFvb.DevicePath,\r
934 NULL\r
935 );\r
936 ASSERT_EFI_ERROR (Status);\r
937\r
938 //\r
939 // Register for the virtual address change event\r
940 //\r
941 Status = gBS->CreateEventEx (\r
942 EVT_NOTIFY_SIGNAL,\r
943 TPL_NOTIFY,\r
944 FvbVirtualAddressChangeEvent,\r
945 NULL,\r
946 &gEfiEventVirtualAddressChangeGuid,\r
947 &mEmuVarsFvbAddrChangeEvent\r
948 );\r
949 ASSERT_EFI_ERROR (Status);\r
950\r
951 return EFI_SUCCESS;\r
952}\r
953\r
954\r