]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/EmuVariableFvbRuntimeDxe/Fvb.c
MdeModulePkg/PciBus: Add IOMMU support.
[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
670d495b 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
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
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
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
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
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
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
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
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
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
670d495b 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
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
670d495b 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
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 standard (non-authenticated) variable FV header\r
630 //\r
670d495b 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
ce68d3bc 658 {0},\r
670d495b 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
670d495b 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;\r
675 EFI_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
bdf93df8 696\r
697 //\r
698 // Templates for authenticated variable FV header\r
699 //\r
700 STATIC FVB_FV_HDR_AND_VARS_TEMPLATE FvAndAuthenticatedVarTemplate = {\r
701 { // EFI_FIRMWARE_VOLUME_HEADER FvHdr;\r
702 // UINT8 ZeroVector[16];\r
703 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },\r
704\r
705 // EFI_GUID FileSystemGuid;\r
706 EFI_SYSTEM_NV_DATA_FV_GUID,\r
707\r
708 // UINT64 FvLength;\r
709 EMU_FVB_SIZE,\r
710\r
711 // UINT32 Signature;\r
712 EFI_FVH_SIGNATURE,\r
713\r
714 // EFI_FVB_ATTRIBUTES_2 Attributes;\r
715 0x4feff,\r
716\r
717 // UINT16 HeaderLength;\r
718 EMU_FV_HEADER_LENGTH,\r
719\r
720 // UINT16 Checksum;\r
721 0,\r
722\r
723 // UINT16 ExtHeaderOffset;\r
724 0,\r
725\r
726 // UINT8 Reserved[1];\r
ce68d3bc 727 {0},\r
bdf93df8 728\r
729 // UINT8 Revision;\r
730 EFI_FVH_REVISION,\r
731\r
732 // EFI_FV_BLOCK_MAP_ENTRY BlockMap[1];\r
ce68d3bc
SZ
733 {\r
734 {\r
735 2, // UINT32 NumBlocks;\r
736 EMU_FVB_BLOCK_SIZE // UINT32 Length;\r
737 }\r
bdf93df8 738 }\r
739 },\r
740 // EFI_FV_BLOCK_MAP_ENTRY EndBlockMap;\r
741 { 0, 0 }, // End of block map\r
742 { // VARIABLE_STORE_HEADER VarHdr;\r
743 // EFI_GUID Signature; // need authenticated variables for secure boot\r
744 EFI_AUTHENTICATED_VARIABLE_GUID,\r
745\r
746 // UINT32 Size;\r
747 (\r
748 FixedPcdGet32 (PcdVariableStoreSize) -\r
749 OFFSET_OF (FVB_FV_HDR_AND_VARS_TEMPLATE, VarHdr)\r
750 ),\r
751\r
752 // UINT8 Format;\r
753 VARIABLE_STORE_FORMATTED,\r
754\r
755 // UINT8 State;\r
756 VARIABLE_STORE_HEALTHY,\r
757\r
758 // UINT16 Reserved;\r
759 0,\r
760\r
761 // UINT32 Reserved1;\r
762 0\r
763 }\r
764 };\r
765\r
670d495b 766 EFI_FIRMWARE_VOLUME_HEADER *Fv;\r
767\r
768 //\r
769 // Copy the template structure into the location\r
770 //\r
bdf93df8 771 if (FeaturePcdGet (PcdSecureBootEnable) == FALSE) {\r
772 CopyMem (Ptr, (VOID*)&FvAndVarTemplate, sizeof (FvAndVarTemplate));\r
773 } else {\r
774 CopyMem (Ptr, (VOID*)&FvAndAuthenticatedVarTemplate, sizeof (FvAndAuthenticatedVarTemplate));\r
775 }\r
670d495b 776\r
777 //\r
778 // Update the checksum for the FV header\r
779 //\r
780 Fv = (EFI_FIRMWARE_VOLUME_HEADER*) Ptr;\r
781 Fv->Checksum = CalculateCheckSum16 (Ptr, Fv->HeaderLength);\r
782}\r
783\r
670d495b 784/**\r
785 Main entry point.\r
786\r
787 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
788 @param[in] SystemTable A pointer to the EFI System Table.\r
789 \r
790 @retval EFI_SUCCESS Successfully initialized.\r
791\r
792**/\r
793EFI_STATUS\r
794EFIAPI\r
795FvbInitialize (\r
796 IN EFI_HANDLE ImageHandle,\r
797 IN EFI_SYSTEM_TABLE *SystemTable\r
798 )\r
799{\r
800 EFI_STATUS Status;\r
801 VOID *Ptr;\r
802 VOID *SubPtr;\r
803 BOOLEAN Initialize;\r
804 EFI_HANDLE Handle;\r
805 EFI_PHYSICAL_ADDRESS Address;\r
6348eea6 806 RETURN_STATUS PcdStatus;\r
670d495b 807\r
808 DEBUG ((EFI_D_INFO, "EMU Variable FVB Started\n"));\r
809\r
810 //\r
811 // Verify that the PCD's are set correctly.\r
812 //\r
813 if (\r
29a3f139 814 (PcdGet32 (PcdVariableStoreSize) +\r
815 PcdGet32 (PcdFlashNvStorageFtwWorkingSize)\r
670d495b 816 ) >\r
817 EMU_FVB_BLOCK_SIZE\r
818 ) {\r
819 DEBUG ((EFI_D_ERROR, "EMU Variable invalid PCD sizes\n"));\r
820 return EFI_INVALID_PARAMETER;\r
821 }\r
822\r
4313b26d
JJ
823 if (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0) {\r
824 DEBUG ((EFI_D_INFO, "Disabling EMU Variable FVB since "\r
825 "flash variables appear to be supported.\n"));\r
826 return EFI_ABORTED;\r
827 }\r
828\r
670d495b 829 //\r
830 // By default we will initialize the FV contents. But, if\r
831 // PcdEmuVariableNvStoreReserved is non-zero, then we will\r
832 // use this location for our buffer.\r
833 //\r
834 // If this location does not have a proper FV header, then\r
835 // we will initialize it.\r
836 //\r
837 Initialize = TRUE;\r
838 if (PcdGet64 (PcdEmuVariableNvStoreReserved) != 0) {\r
839 Ptr = (VOID*)(UINTN) PcdGet64 (PcdEmuVariableNvStoreReserved);\r
840 DEBUG ((\r
841 EFI_D_INFO,\r
842 "EMU Variable FVB: Using pre-reserved block at %p\n",\r
843 Ptr\r
844 ));\r
845 Status = ValidateFvHeader (Ptr);\r
846 if (!EFI_ERROR (Status)) {\r
847 DEBUG ((EFI_D_INFO, "EMU Variable FVB: Found valid pre-existing FV\n"));\r
848 Initialize = FALSE;\r
849 }\r
850 } else {\r
851 Ptr = AllocateAlignedRuntimePages (\r
852 EFI_SIZE_TO_PAGES (EMU_FVB_SIZE),\r
853 SIZE_64KB\r
854 );\r
855 }\r
856\r
857 mEmuVarsFvb.BufferPtr = Ptr;\r
858\r
859 //\r
860 // Initialize the main FV header and variable store header\r
861 //\r
862 if (Initialize) {\r
863 SetMem (Ptr, EMU_FVB_SIZE, ERASED_UINT8);\r
864 InitializeFvAndVariableStoreHeaders (Ptr);\r
865 }\r
6348eea6
LE
866 PcdStatus = PcdSet64S (PcdFlashNvStorageVariableBase64, (UINT32)(UINTN) Ptr);\r
867 ASSERT_RETURN_ERROR (PcdStatus);\r
670d495b 868\r
869 //\r
870 // Initialize the Fault Tolerant Write data area\r
871 //\r
29a3f139 872 SubPtr = (VOID*) ((UINT8*) Ptr + PcdGet32 (PcdVariableStoreSize));\r
6348eea6
LE
873 PcdStatus = PcdSet32S (PcdFlashNvStorageFtwWorkingBase,\r
874 (UINT32)(UINTN) SubPtr);\r
875 ASSERT_RETURN_ERROR (PcdStatus);\r
670d495b 876\r
877 //\r
878 // Initialize the Fault Tolerant Write spare block\r
879 //\r
880 SubPtr = (VOID*) ((UINT8*) Ptr + EMU_FVB_BLOCK_SIZE);\r
6348eea6
LE
881 PcdStatus = PcdSet32S (PcdFlashNvStorageFtwSpareBase,\r
882 (UINT32)(UINTN) SubPtr);\r
883 ASSERT_RETURN_ERROR (PcdStatus);\r
670d495b 884\r
885 //\r
886 // Setup FVB device path\r
887 //\r
888 Address = (EFI_PHYSICAL_ADDRESS)(UINTN) Ptr;\r
889 mEmuVarsFvb.DevicePath.MemMapDevPath.StartingAddress = Address;\r
890 mEmuVarsFvb.DevicePath.MemMapDevPath.EndingAddress = Address + EMU_FVB_SIZE - 1;\r
891\r
892 //\r
893 // Install the protocols\r
894 //\r
895 DEBUG ((EFI_D_INFO, "Installing FVB for EMU Variable support\n"));\r
896 Handle = 0;\r
897 Status = gBS->InstallMultipleProtocolInterfaces (\r
898 &Handle,\r
949b0f3b 899 &gEfiFirmwareVolumeBlock2ProtocolGuid,\r
670d495b 900 &mEmuVarsFvb.FwVolBlockInstance,\r
901 &gEfiDevicePathProtocolGuid,\r
902 &mEmuVarsFvb.DevicePath,\r
903 NULL\r
904 );\r
905 ASSERT_EFI_ERROR (Status);\r
906\r
907 //\r
908 // Register for the virtual address change event\r
909 //\r
910 Status = gBS->CreateEventEx (\r
911 EVT_NOTIFY_SIGNAL,\r
912 TPL_NOTIFY,\r
913 FvbVirtualAddressChangeEvent,\r
914 NULL,\r
915 &gEfiEventVirtualAddressChangeGuid,\r
916 &mEmuVarsFvbAddrChangeEvent\r
917 );\r
918 ASSERT_EFI_ERROR (Status);\r
919\r
920 return EFI_SUCCESS;\r
921}\r
922\r
923\r