]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
MdeModulePkg Variable: Add emulated variable NV mode support
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / VariableSmm.c
CommitLineData
8a2d4996 1/** @file\r
fa0737a8
SZ
2 The sample implementation for SMM variable protocol. And this driver\r
3 implements an SMI handler to communicate with the DXE runtime driver\r
8a2d4996 4 to provide variable services.\r
5\r
2445a70e 6 Caution: This module requires additional review when modified.\r
7 This driver will have external input - variable data and communicate buffer in SMM mode.\r
8 This external input must be validated carefully to avoid security issue like\r
9 buffer overflow, integer overflow.\r
10\r
11 SmmVariableHandler() will receive untrusted input and do basic validation.\r
12\r
fa0737a8
SZ
13 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),\r
14 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),\r
2445a70e 15 SmmVariableGetStatistics() should also do validation based on its own knowledge.\r
16\r
904e0ca9 17Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.<BR>\r
a855f63e 18Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>\r
fa0737a8
SZ
19This program and the accompanying materials\r
20are licensed and made available under the terms and conditions of the BSD License\r
21which accompanies this distribution. The full text of the license may be found at\r
2445a70e 22http://opensource.org/licenses/bsd-license.php\r
8a2d4996 23\r
fa0737a8
SZ
24THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
25WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
8a2d4996 26\r
27**/\r
fa0737a8 28\r
d00ed85e 29#include <Protocol/SmmVariable.h>\r
30#include <Protocol/SmmFirmwareVolumeBlock.h>\r
8a2d4996 31#include <Protocol/SmmFaultTolerantWrite.h>\r
a855f63e 32#include <Protocol/MmEndOfDxe.h>\r
efb01a10 33#include <Protocol/SmmVarCheck.h>\r
2445a70e 34\r
a855f63e 35#include <Library/MmServicesTableLib.h>\r
8a2d4996 36\r
d00ed85e 37#include <Guid/SmmVariableCommon.h>\r
8a2d4996 38#include "Variable.h"\r
8a2d4996 39\r
8a2d4996 40BOOLEAN mAtRuntime = FALSE;\r
5e5bb2a9
SZ
41UINT8 *mVariableBufferPayload = NULL;\r
42UINTN mVariableBufferPayloadSize;\r
ff843847 43\r
fa0737a8
SZ
44/**\r
45 SecureBoot Hook for SetVariable.\r
46\r
47 @param[in] VariableName Name of Variable to be found.\r
48 @param[in] VendorGuid Variable vendor GUID.\r
49\r
50**/\r
51VOID\r
52EFIAPI\r
53SecureBootHook (\r
54 IN CHAR16 *VariableName,\r
55 IN EFI_GUID *VendorGuid\r
56 )\r
57{\r
58 return ;\r
59}\r
60\r
ff843847
RN
61/**\r
62\r
63 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
64\r
65 @param VariableName Name of Variable to be found.\r
66 @param VendorGuid Variable vendor GUID.\r
67 @param Attributes Attribute value of the variable found\r
68 @param DataSize Size of Data found. If size is less than the\r
69 data, this value contains the required size.\r
70 @param Data Data pointer.\r
71\r
72 @return EFI_INVALID_PARAMETER Invalid parameter.\r
73 @return EFI_SUCCESS Set successfully.\r
74 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
75 @return EFI_NOT_FOUND Not found.\r
76 @return EFI_WRITE_PROTECTED Variable is read-only.\r
77\r
78**/\r
79EFI_STATUS\r
80EFIAPI\r
81SmmVariableSetVariable (\r
82 IN CHAR16 *VariableName,\r
83 IN EFI_GUID *VendorGuid,\r
84 IN UINT32 Attributes,\r
85 IN UINTN DataSize,\r
86 IN VOID *Data\r
87 )\r
88{\r
89 EFI_STATUS Status;\r
90\r
91 //\r
92 // Disable write protection when the calling SetVariable() through EFI_SMM_VARIABLE_PROTOCOL.\r
93 //\r
8021f4c7 94 mRequestSource = VarCheckFromTrusted;\r
ff843847
RN
95 Status = VariableServiceSetVariable (\r
96 VariableName,\r
97 VendorGuid,\r
98 Attributes,\r
99 DataSize,\r
100 Data\r
101 );\r
8021f4c7 102 mRequestSource = VarCheckFromUntrusted;\r
ff843847
RN
103 return Status;\r
104}\r
5e5bb2a9 105\r
8a2d4996 106EFI_SMM_VARIABLE_PROTOCOL gSmmVariable = {\r
107 VariableServiceGetVariable,\r
108 VariableServiceGetNextVariableName,\r
ff843847 109 SmmVariableSetVariable,\r
8a2d4996 110 VariableServiceQueryVariableInfo\r
111};\r
112\r
efb01a10
SZ
113EDKII_SMM_VAR_CHECK_PROTOCOL mSmmVarCheck = { VarCheckRegisterSetVariableCheckHandler,\r
114 VarCheckVariablePropertySet,\r
115 VarCheckVariablePropertyGet };\r
116\r
8a2d4996 117/**\r
118 Return TRUE if ExitBootServices () has been called.\r
fa0737a8 119\r
8a2d4996 120 @retval TRUE If ExitBootServices () has been called.\r
121**/\r
122BOOLEAN\r
123AtRuntime (\r
124 VOID\r
125 )\r
126{\r
127 return mAtRuntime;\r
128}\r
129\r
130/**\r
131 Initializes a basic mutual exclusion lock.\r
132\r
fa0737a8
SZ
133 This function initializes a basic mutual exclusion lock to the released state\r
134 and returns the lock. Each lock provides mutual exclusion access at its task\r
8a2d4996 135 priority level. Since there is no preemption or multiprocessor support in EFI,\r
136 acquiring the lock only consists of raising to the locks TPL.\r
137 If Lock is NULL, then ASSERT().\r
138 If Priority is not a valid TPL value, then ASSERT().\r
139\r
140 @param Lock A pointer to the lock data structure to initialize.\r
141 @param Priority EFI TPL is associated with the lock.\r
142\r
143 @return The lock.\r
144\r
145**/\r
146EFI_LOCK *\r
147InitializeLock (\r
148 IN OUT EFI_LOCK *Lock,\r
149 IN EFI_TPL Priority\r
150 )\r
151{\r
152 return Lock;\r
153}\r
154\r
155/**\r
156 Acquires lock only at boot time. Simply returns at runtime.\r
157\r
158 This is a temperary function that will be removed when\r
159 EfiAcquireLock() in UefiLib can handle the call in UEFI\r
160 Runtimer driver in RT phase.\r
161 It calls EfiAcquireLock() at boot time, and simply returns\r
162 at runtime.\r
163\r
164 @param Lock A pointer to the lock to acquire.\r
165\r
166**/\r
167VOID\r
168AcquireLockOnlyAtBootTime (\r
169 IN EFI_LOCK *Lock\r
170 )\r
171{\r
172\r
173}\r
174\r
175\r
176/**\r
177 Releases lock only at boot time. Simply returns at runtime.\r
178\r
179 This is a temperary function which will be removed when\r
180 EfiReleaseLock() in UefiLib can handle the call in UEFI\r
181 Runtimer driver in RT phase.\r
182 It calls EfiReleaseLock() at boot time and simply returns\r
183 at runtime.\r
184\r
185 @param Lock A pointer to the lock to release.\r
186\r
187**/\r
188VOID\r
189ReleaseLockOnlyAtBootTime (\r
190 IN EFI_LOCK *Lock\r
191 )\r
192{\r
193\r
194}\r
195\r
196/**\r
0a18956d 197 Retrieve the SMM Fault Tolerent Write protocol interface.\r
8a2d4996 198\r
199 @param[out] FtwProtocol The interface of SMM Ftw protocol\r
200\r
201 @retval EFI_SUCCESS The SMM FTW protocol instance was found and returned in FtwProtocol.\r
202 @retval EFI_NOT_FOUND The SMM FTW protocol instance was not found.\r
203 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.\r
204\r
205**/\r
206EFI_STATUS\r
207GetFtwProtocol (\r
208 OUT VOID **FtwProtocol\r
209 )\r
210{\r
211 EFI_STATUS Status;\r
212\r
213 //\r
214 // Locate Smm Fault Tolerent Write protocol\r
215 //\r
a855f63e 216 Status = gMmst->MmLocateProtocol (\r
fa0737a8
SZ
217 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
218 NULL,\r
8a2d4996 219 FtwProtocol\r
220 );\r
221 return Status;\r
222}\r
223\r
224\r
225/**\r
0a18956d 226 Retrieve the SMM FVB protocol interface by HANDLE.\r
8a2d4996 227\r
228 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for\r
229 reading, writing, and erasing the target block.\r
230 @param[out] FvBlock The interface of SMM FVB protocol\r
231\r
232 @retval EFI_SUCCESS The interface information for the specified protocol was returned.\r
233 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.\r
234 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.\r
235\r
236**/\r
237EFI_STATUS\r
238GetFvbByHandle (\r
239 IN EFI_HANDLE FvBlockHandle,\r
240 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r
241 )\r
242{\r
243 //\r
244 // To get the SMM FVB protocol interface on the handle\r
245 //\r
a855f63e 246 return gMmst->MmHandleProtocol (\r
8a2d4996 247 FvBlockHandle,\r
248 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
249 (VOID **) FvBlock\r
250 );\r
251}\r
252\r
253\r
254/**\r
255 Function returns an array of handles that support the SMM FVB protocol\r
fa0737a8 256 in a buffer allocated from pool.\r
8a2d4996 257\r
258 @param[out] NumberHandles The number of handles returned in Buffer.\r
259 @param[out] Buffer A pointer to the buffer to return the requested\r
260 array of handles that support SMM FVB protocol.\r
261\r
262 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of\r
263 handles in Buffer was returned in NumberHandles.\r
264 @retval EFI_NOT_FOUND No SMM FVB handle was found.\r
265 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.\r
266 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.\r
267\r
268**/\r
269EFI_STATUS\r
270GetFvbCountAndBuffer (\r
271 OUT UINTN *NumberHandles,\r
272 OUT EFI_HANDLE **Buffer\r
273 )\r
274{\r
275 EFI_STATUS Status;\r
276 UINTN BufferSize;\r
277\r
278 if ((NumberHandles == NULL) || (Buffer == NULL)) {\r
279 return EFI_INVALID_PARAMETER;\r
280 }\r
281\r
282 BufferSize = 0;\r
283 *NumberHandles = 0;\r
284 *Buffer = NULL;\r
a855f63e 285 Status = gMmst->MmLocateHandle (\r
8a2d4996 286 ByProtocol,\r
287 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
288 NULL,\r
289 &BufferSize,\r
290 *Buffer\r
291 );\r
292 if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {\r
293 return EFI_NOT_FOUND;\r
294 }\r
295\r
296 *Buffer = AllocatePool (BufferSize);\r
297 if (*Buffer == NULL) {\r
298 return EFI_OUT_OF_RESOURCES;\r
299 }\r
300\r
a855f63e 301 Status = gMmst->MmLocateHandle (\r
8a2d4996 302 ByProtocol,\r
303 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
304 NULL,\r
305 &BufferSize,\r
306 *Buffer\r
307 );\r
308\r
309 *NumberHandles = BufferSize / sizeof(EFI_HANDLE);\r
310 if (EFI_ERROR(Status)) {\r
311 *NumberHandles = 0;\r
5e5bb2a9
SZ
312 FreePool (*Buffer);\r
313 *Buffer = NULL;\r
8a2d4996 314 }\r
315\r
316 return Status;\r
317}\r
318\r
319\r
320/**\r
321 Get the variable statistics information from the information buffer pointed by gVariableInfo.\r
322\r
2445a70e 323 Caution: This function may be invoked at SMM runtime.\r
324 InfoEntry and InfoSize are external input. Care must be taken to make sure not security issue at runtime.\r
325\r
326 @param[in, out] InfoEntry A pointer to the buffer of variable information entry.\r
fa0737a8 327 On input, point to the variable information returned last time. if\r
2445a70e 328 InfoEntry->VendorGuid is zero, return the first information.\r
329 On output, point to the next variable information.\r
330 @param[in, out] InfoSize On input, the size of the variable information buffer.\r
331 On output, the returned variable information size.\r
8a2d4996 332\r
fa0737a8
SZ
333 @retval EFI_SUCCESS The variable information is found and returned successfully.\r
334 @retval EFI_UNSUPPORTED No variable inoformation exists in variable driver. The\r
335 PcdVariableCollectStatistics should be set TRUE to support it.\r
336 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the next variable information.\r
337 @retval EFI_INVALID_PARAMETER Input parameter is invalid.\r
8a2d4996 338\r
339**/\r
340EFI_STATUS\r
341SmmVariableGetStatistics (\r
d00ed85e 342 IN OUT VARIABLE_INFO_ENTRY *InfoEntry,\r
8a2d4996 343 IN OUT UINTN *InfoSize\r
344 )\r
345{\r
d00ed85e 346 VARIABLE_INFO_ENTRY *VariableInfo;\r
d5aef955 347 UINTN NameSize;\r
8a2d4996 348 UINTN StatisticsInfoSize;\r
349 CHAR16 *InfoName;\r
d5aef955 350 UINTN InfoNameMaxSize;\r
5e5bb2a9
SZ
351 EFI_GUID VendorGuid;\r
352\r
fa0737a8
SZ
353 if (InfoEntry == NULL) {\r
354 return EFI_INVALID_PARAMETER;\r
355 }\r
356\r
357 VariableInfo = gVariableInfo;\r
8a2d4996 358 if (VariableInfo == NULL) {\r
359 return EFI_UNSUPPORTED;\r
360 }\r
361\r
d5aef955 362 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY);\r
eb96e4f2 363 if (*InfoSize < StatisticsInfoSize) {\r
8a2d4996 364 *InfoSize = StatisticsInfoSize;\r
365 return EFI_BUFFER_TOO_SMALL;\r
366 }\r
367 InfoName = (CHAR16 *)(InfoEntry + 1);\r
d5aef955 368 InfoNameMaxSize = (*InfoSize - sizeof (VARIABLE_INFO_ENTRY));\r
8a2d4996 369\r
5e5bb2a9
SZ
370 CopyGuid (&VendorGuid, &InfoEntry->VendorGuid);\r
371\r
39cde03c 372 if (IsZeroGuid (&VendorGuid)) {\r
8a2d4996 373 //\r
374 // Return the first variable info\r
375 //\r
d5aef955
SZ
376 NameSize = StrSize (VariableInfo->Name);\r
377 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + NameSize;\r
378 if (*InfoSize < StatisticsInfoSize) {\r
379 *InfoSize = StatisticsInfoSize;\r
380 return EFI_BUFFER_TOO_SMALL;\r
381 }\r
d00ed85e 382 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));\r
d5aef955 383 CopyMem (InfoName, VariableInfo->Name, NameSize);\r
8a2d4996 384 *InfoSize = StatisticsInfoSize;\r
385 return EFI_SUCCESS;\r
386 }\r
387\r
388 //\r
389 // Get the next variable info\r
390 //\r
391 while (VariableInfo != NULL) {\r
5e5bb2a9 392 if (CompareGuid (&VariableInfo->VendorGuid, &VendorGuid)) {\r
d5aef955
SZ
393 NameSize = StrSize (VariableInfo->Name);\r
394 if (NameSize <= InfoNameMaxSize) {\r
395 if (CompareMem (VariableInfo->Name, InfoName, NameSize) == 0) {\r
8a2d4996 396 //\r
397 // Find the match one\r
398 //\r
399 VariableInfo = VariableInfo->Next;\r
400 break;\r
401 }\r
402 }\r
403 }\r
404 VariableInfo = VariableInfo->Next;\r
405 };\r
fa0737a8 406\r
8a2d4996 407 if (VariableInfo == NULL) {\r
408 *InfoSize = 0;\r
409 return EFI_SUCCESS;\r
410 }\r
411\r
412 //\r
413 // Output the new variable info\r
414 //\r
d5aef955
SZ
415 NameSize = StrSize (VariableInfo->Name);\r
416 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + NameSize;\r
8a2d4996 417 if (*InfoSize < StatisticsInfoSize) {\r
418 *InfoSize = StatisticsInfoSize;\r
419 return EFI_BUFFER_TOO_SMALL;\r
420 }\r
421\r
d00ed85e 422 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));\r
d5aef955 423 CopyMem (InfoName, VariableInfo->Name, NameSize);\r
8a2d4996 424 *InfoSize = StatisticsInfoSize;\r
fa0737a8 425\r
8a2d4996 426 return EFI_SUCCESS;\r
427}\r
428\r
429\r
430/**\r
431 Communication service SMI Handler entry.\r
432\r
433 This SMI handler provides services for the variable wrapper driver.\r
434\r
2445a70e 435 Caution: This function may receive untrusted input.\r
436 This variable data and communicate buffer are external input, so this function will do basic validation.\r
fa0737a8
SZ
437 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),\r
438 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),\r
2445a70e 439 SmmVariableGetStatistics() should also do validation based on its own knowledge.\r
440\r
8a2d4996 441 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
442 @param[in] RegisterContext Points to an optional handler context which was specified when the\r
443 handler was registered.\r
444 @param[in, out] CommBuffer A pointer to a collection of data in memory that will\r
445 be conveyed from a non-SMM environment into an SMM environment.\r
446 @param[in, out] CommBufferSize The size of the CommBuffer.\r
447\r
fa0737a8 448 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers\r
8a2d4996 449 should still be called.\r
fa0737a8 450 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should\r
8a2d4996 451 still be called.\r
fa0737a8 452 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still\r
8a2d4996 453 be called.\r
454 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.\r
455**/\r
456EFI_STATUS\r
457EFIAPI\r
458SmmVariableHandler (\r
459 IN EFI_HANDLE DispatchHandle,\r
460 IN CONST VOID *RegisterContext,\r
461 IN OUT VOID *CommBuffer,\r
462 IN OUT UINTN *CommBufferSize\r
463 )\r
464{\r
465 EFI_STATUS Status;\r
466 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;\r
467 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;\r
468 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *GetNextVariableName;\r
469 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *QueryVariableInfo;\r
fa0737a8 470 SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE *GetPayloadSize;\r
d00ed85e 471 VARIABLE_INFO_ENTRY *VariableInfo;\r
ff843847 472 SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *VariableToLock;\r
efb01a10 473 SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProperty;\r
8a2d4996 474 UINTN InfoSize;\r
9d00d20e 475 UINTN NameBufferSize;\r
5e5bb2a9 476 UINTN CommBufferPayloadSize;\r
164a9b67 477 UINTN TempCommBufferSize;\r
8a2d4996 478\r
2445a70e 479 //\r
480 // If input is invalid, stop processing this SMI\r
481 //\r
482 if (CommBuffer == NULL || CommBufferSize == NULL) {\r
483 return EFI_SUCCESS;\r
484 }\r
485\r
164a9b67
SZ
486 TempCommBufferSize = *CommBufferSize;\r
487\r
488 if (TempCommBufferSize < SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {\r
5e5bb2a9
SZ
489 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer size invalid!\n"));\r
490 return EFI_SUCCESS;\r
491 }\r
164a9b67 492 CommBufferPayloadSize = TempCommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
5e5bb2a9
SZ
493 if (CommBufferPayloadSize > mVariableBufferPayloadSize) {\r
494 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer payload size invalid!\n"));\r
2445a70e 495 return EFI_SUCCESS;\r
496 }\r
497\r
a855f63e 498 if (!VariableSmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {\r
5e5bb2a9 499 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer in SMRAM or overflow!\n"));\r
2445a70e 500 return EFI_SUCCESS;\r
501 }\r
8a2d4996 502\r
503 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)CommBuffer;\r
504 switch (SmmVariableFunctionHeader->Function) {\r
505 case SMM_VARIABLE_FUNCTION_GET_VARIABLE:\r
5e5bb2a9
SZ
506 if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {\r
507 DEBUG ((EFI_D_ERROR, "GetVariable: SMM communication buffer size invalid!\n"));\r
508 return EFI_SUCCESS;\r
509 }\r
510 //\r
511 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
512 //\r
513 CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
514 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) mVariableBufferPayload;\r
9d00d20e
SZ
515 if (((UINTN)(~0) - SmmVariableHeader->DataSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||\r
516 ((UINTN)(~0) - SmmVariableHeader->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize)) {\r
517 //\r
518 // Prevent InfoSize overflow happen\r
519 //\r
520 Status = EFI_ACCESS_DENIED;\r
521 goto EXIT;\r
522 }\r
fa0737a8 523 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)\r
2445a70e 524 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;\r
525\r
526 //\r
527 // SMRAM range check already covered before\r
528 //\r
5e5bb2a9
SZ
529 if (InfoSize > CommBufferPayloadSize) {\r
530 DEBUG ((EFI_D_ERROR, "GetVariable: Data size exceed communication buffer size limit!\n"));\r
2445a70e 531 Status = EFI_ACCESS_DENIED;\r
532 goto EXIT;\r
533 }\r
534\r
e83d841f 535 //\r
49395ea0
HW
536 // The VariableSpeculationBarrier() call here is to ensure the previous\r
537 // range/content checks for the CommBuffer have been completed before the\r
538 // subsequent consumption of the CommBuffer content.\r
e83d841f 539 //\r
49395ea0 540 VariableSpeculationBarrier ();\r
9d00d20e
SZ
541 if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') {\r
542 //\r
543 // Make sure VariableName is A Null-terminated string.\r
544 //\r
545 Status = EFI_ACCESS_DENIED;\r
546 goto EXIT;\r
547 }\r
548\r
8a2d4996 549 Status = VariableServiceGetVariable (\r
550 SmmVariableHeader->Name,\r
551 &SmmVariableHeader->Guid,\r
552 &SmmVariableHeader->Attributes,\r
553 &SmmVariableHeader->DataSize,\r
554 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize\r
555 );\r
5e5bb2a9 556 CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);\r
8a2d4996 557 break;\r
fa0737a8 558\r
8a2d4996 559 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME:\r
5e5bb2a9
SZ
560 if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
561 DEBUG ((EFI_D_ERROR, "GetNextVariableName: SMM communication buffer size invalid!\n"));\r
562 return EFI_SUCCESS;\r
563 }\r
564 //\r
565 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
566 //\r
567 CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
568 GetNextVariableName = (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *) mVariableBufferPayload;\r
9d00d20e
SZ
569 if ((UINTN)(~0) - GetNextVariableName->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
570 //\r
571 // Prevent InfoSize overflow happen\r
572 //\r
573 Status = EFI_ACCESS_DENIED;\r
574 goto EXIT;\r
575 }\r
2445a70e 576 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + GetNextVariableName->NameSize;\r
577\r
578 //\r
579 // SMRAM range check already covered before\r
580 //\r
5e5bb2a9
SZ
581 if (InfoSize > CommBufferPayloadSize) {\r
582 DEBUG ((EFI_D_ERROR, "GetNextVariableName: Data size exceed communication buffer size limit!\n"));\r
2445a70e 583 Status = EFI_ACCESS_DENIED;\r
584 goto EXIT;\r
585 }\r
586\r
5e5bb2a9 587 NameBufferSize = CommBufferPayloadSize - OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);\r
9d00d20e
SZ
588 if (NameBufferSize < sizeof (CHAR16) || GetNextVariableName->Name[NameBufferSize/sizeof (CHAR16) - 1] != L'\0') {\r
589 //\r
590 // Make sure input VariableName is A Null-terminated string.\r
591 //\r
592 Status = EFI_ACCESS_DENIED;\r
593 goto EXIT;\r
594 }\r
595\r
8a2d4996 596 Status = VariableServiceGetNextVariableName (\r
597 &GetNextVariableName->NameSize,\r
598 GetNextVariableName->Name,\r
599 &GetNextVariableName->Guid\r
600 );\r
5e5bb2a9 601 CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);\r
8a2d4996 602 break;\r
fa0737a8 603\r
8a2d4996 604 case SMM_VARIABLE_FUNCTION_SET_VARIABLE:\r
5e5bb2a9
SZ
605 if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {\r
606 DEBUG ((EFI_D_ERROR, "SetVariable: SMM communication buffer size invalid!\n"));\r
607 return EFI_SUCCESS;\r
608 }\r
609 //\r
610 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
611 //\r
612 CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
613 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) mVariableBufferPayload;\r
9d00d20e
SZ
614 if (((UINTN)(~0) - SmmVariableHeader->DataSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||\r
615 ((UINTN)(~0) - SmmVariableHeader->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize)) {\r
616 //\r
617 // Prevent InfoSize overflow happen\r
618 //\r
619 Status = EFI_ACCESS_DENIED;\r
620 goto EXIT;\r
621 }\r
d17c4eac 622 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)\r
623 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;\r
624\r
625 //\r
626 // SMRAM range check already covered before\r
627 // Data buffer should not contain SMM range\r
628 //\r
5e5bb2a9
SZ
629 if (InfoSize > CommBufferPayloadSize) {\r
630 DEBUG ((EFI_D_ERROR, "SetVariable: Data size exceed communication buffer size limit!\n"));\r
d17c4eac 631 Status = EFI_ACCESS_DENIED;\r
632 goto EXIT;\r
633 }\r
634\r
e83d841f 635 //\r
49395ea0
HW
636 // The VariableSpeculationBarrier() call here is to ensure the previous\r
637 // range/content checks for the CommBuffer have been completed before the\r
638 // subsequent consumption of the CommBuffer content.\r
e83d841f 639 //\r
49395ea0 640 VariableSpeculationBarrier ();\r
9d00d20e
SZ
641 if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') {\r
642 //\r
643 // Make sure VariableName is A Null-terminated string.\r
644 //\r
645 Status = EFI_ACCESS_DENIED;\r
646 goto EXIT;\r
647 }\r
648\r
8a2d4996 649 Status = VariableServiceSetVariable (\r
650 SmmVariableHeader->Name,\r
651 &SmmVariableHeader->Guid,\r
652 SmmVariableHeader->Attributes,\r
653 SmmVariableHeader->DataSize,\r
654 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize\r
655 );\r
656 break;\r
fa0737a8 657\r
8a2d4996 658 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO:\r
5e5bb2a9
SZ
659 if (CommBufferPayloadSize < sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO)) {\r
660 DEBUG ((EFI_D_ERROR, "QueryVariableInfo: SMM communication buffer size invalid!\n"));\r
661 return EFI_SUCCESS;\r
2445a70e 662 }\r
5e5bb2a9 663 QueryVariableInfo = (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *) SmmVariableFunctionHeader->Data;\r
2445a70e 664\r
8a2d4996 665 Status = VariableServiceQueryVariableInfo (\r
666 QueryVariableInfo->Attributes,\r
667 &QueryVariableInfo->MaximumVariableStorageSize,\r
668 &QueryVariableInfo->RemainingVariableStorageSize,\r
669 &QueryVariableInfo->MaximumVariableSize\r
670 );\r
671 break;\r
672\r
fa0737a8
SZ
673 case SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE:\r
674 if (CommBufferPayloadSize < sizeof (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE)) {\r
675 DEBUG ((EFI_D_ERROR, "GetPayloadSize: SMM communication buffer size invalid!\n"));\r
676 return EFI_SUCCESS;\r
677 }\r
678 GetPayloadSize = (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE *) SmmVariableFunctionHeader->Data;\r
679 GetPayloadSize->VariablePayloadSize = mVariableBufferPayloadSize;\r
680 Status = EFI_SUCCESS;\r
681 break;\r
682\r
8a2d4996 683 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT:\r
876ac395 684 if (AtRuntime()) {\r
685 Status = EFI_UNSUPPORTED;\r
686 break;\r
687 }\r
8021f4c7 688 if (!mEndOfDxe) {\r
f1304280 689 MorLockInitAtEndOfDxe ();\r
8021f4c7
SZ
690 mEndOfDxe = TRUE;\r
691 VarCheckLibInitializeAtEndOfDxe (NULL);\r
692 //\r
693 // The initialization for variable quota.\r
694 //\r
695 InitializeVariableQuota ();\r
696 }\r
8a2d4996 697 ReclaimForOS ();\r
698 Status = EFI_SUCCESS;\r
699 break;\r
fa0737a8 700\r
8a2d4996 701 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE:\r
702 mAtRuntime = TRUE;\r
703 Status = EFI_SUCCESS;\r
704 break;\r
705\r
706 case SMM_VARIABLE_FUNCTION_GET_STATISTICS:\r
d00ed85e 707 VariableInfo = (VARIABLE_INFO_ENTRY *) SmmVariableFunctionHeader->Data;\r
164a9b67 708 InfoSize = TempCommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
2445a70e 709\r
710 //\r
fa0737a8
SZ
711 // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here.\r
712 // It is covered by previous CommBuffer check\r
2445a70e 713 //\r
fa0737a8 714\r
62016c1e
SZ
715 //\r
716 // Do not need to check CommBufferSize buffer as it should point to SMRAM\r
717 // that was used by SMM core to cache CommSize from SmmCommunication protocol.\r
718 //\r
2445a70e 719\r
8a2d4996 720 Status = SmmVariableGetStatistics (VariableInfo, &InfoSize);\r
3f5c168f 721 *CommBufferSize = InfoSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
8a2d4996 722 break;\r
723\r
ff843847 724 case SMM_VARIABLE_FUNCTION_LOCK_VARIABLE:\r
51547bb8 725 if (mEndOfDxe) {\r
ff843847 726 Status = EFI_ACCESS_DENIED;\r
51547bb8
RN
727 } else {\r
728 VariableToLock = (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *) SmmVariableFunctionHeader->Data;\r
729 Status = VariableLockRequestToLock (\r
730 NULL,\r
731 VariableToLock->Name,\r
732 &VariableToLock->Guid\r
733 );\r
ff843847 734 }\r
ff843847 735 break;\r
efb01a10
SZ
736 case SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_SET:\r
737 if (mEndOfDxe) {\r
738 Status = EFI_ACCESS_DENIED;\r
739 } else {\r
740 CommVariableProperty = (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *) SmmVariableFunctionHeader->Data;\r
741 Status = VarCheckVariablePropertySet (\r
742 CommVariableProperty->Name,\r
743 &CommVariableProperty->Guid,\r
744 &CommVariableProperty->VariableProperty\r
745 );\r
746 }\r
747 break;\r
748 case SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET:\r
749 if (CommBufferPayloadSize < OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) {\r
750 DEBUG ((EFI_D_ERROR, "VarCheckVariablePropertyGet: SMM communication buffer size invalid!\n"));\r
751 return EFI_SUCCESS;\r
752 }\r
753 //\r
754 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
755 //\r
756 CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
757 CommVariableProperty = (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *) mVariableBufferPayload;\r
758 if ((UINTN) (~0) - CommVariableProperty->NameSize < OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) {\r
759 //\r
760 // Prevent InfoSize overflow happen\r
761 //\r
762 Status = EFI_ACCESS_DENIED;\r
763 goto EXIT;\r
764 }\r
765 InfoSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) + CommVariableProperty->NameSize;\r
766\r
767 //\r
768 // SMRAM range check already covered before\r
769 //\r
770 if (InfoSize > CommBufferPayloadSize) {\r
771 DEBUG ((EFI_D_ERROR, "VarCheckVariablePropertyGet: Data size exceed communication buffer size limit!\n"));\r
772 Status = EFI_ACCESS_DENIED;\r
773 goto EXIT;\r
774 }\r
775\r
e83d841f 776 //\r
49395ea0
HW
777 // The VariableSpeculationBarrier() call here is to ensure the previous\r
778 // range/content checks for the CommBuffer have been completed before the\r
779 // subsequent consumption of the CommBuffer content.\r
e83d841f 780 //\r
49395ea0 781 VariableSpeculationBarrier ();\r
efb01a10
SZ
782 if (CommVariableProperty->NameSize < sizeof (CHAR16) || CommVariableProperty->Name[CommVariableProperty->NameSize/sizeof (CHAR16) - 1] != L'\0') {\r
783 //\r
784 // Make sure VariableName is A Null-terminated string.\r
785 //\r
786 Status = EFI_ACCESS_DENIED;\r
787 goto EXIT;\r
788 }\r
789\r
790 Status = VarCheckVariablePropertyGet (\r
791 CommVariableProperty->Name,\r
792 &CommVariableProperty->Guid,\r
793 &CommVariableProperty->VariableProperty\r
794 );\r
795 CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);\r
796 break;\r
ff843847 797\r
8a2d4996 798 default:\r
8a2d4996 799 Status = EFI_UNSUPPORTED;\r
800 }\r
801\r
2445a70e 802EXIT:\r
803\r
8a2d4996 804 SmmVariableFunctionHeader->ReturnStatus = Status;\r
805\r
806 return EFI_SUCCESS;\r
807}\r
808\r
ff843847
RN
809/**\r
810 SMM END_OF_DXE protocol notification event handler.\r
811\r
812 @param Protocol Points to the protocol's unique identifier\r
813 @param Interface Points to the interface instance\r
814 @param Handle The handle on which the interface was installed\r
815\r
816 @retval EFI_SUCCESS SmmEndOfDxeCallback runs successfully\r
817\r
818**/\r
819EFI_STATUS\r
820EFIAPI\r
821SmmEndOfDxeCallback (\r
822 IN CONST EFI_GUID *Protocol,\r
823 IN VOID *Interface,\r
824 IN EFI_HANDLE Handle\r
825 )\r
826{\r
8021f4c7 827 DEBUG ((EFI_D_INFO, "[Variable]SMM_END_OF_DXE is signaled\n"));\r
f1304280 828 MorLockInitAtEndOfDxe ();\r
ff843847 829 mEndOfDxe = TRUE;\r
8021f4c7 830 VarCheckLibInitializeAtEndOfDxe (NULL);\r
4edb1866
SZ
831 //\r
832 // The initialization for variable quota.\r
833 //\r
834 InitializeVariableQuota ();\r
0fb5e515
SZ
835 if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe)) {\r
836 ReclaimForOS ();\r
837 }\r
8021f4c7 838\r
ff843847
RN
839 return EFI_SUCCESS;\r
840}\r
8a2d4996 841\r
b59fd889
SZ
842/**\r
843 Initializes variable write service for SMM.\r
844\r
845**/\r
846VOID\r
847VariableWriteServiceInitializeSmm (\r
848 VOID\r
849 )\r
850{\r
851 EFI_STATUS Status;\r
852\r
853 Status = VariableWriteServiceInitialize ();\r
854 if (EFI_ERROR (Status)) {\r
855 DEBUG ((DEBUG_ERROR, "Variable write service initialization failed. Status = %r\n", Status));\r
856 }\r
857\r
858 //\r
859 // Notify the variable wrapper driver the variable write service is ready\r
860 //\r
861 VariableNotifySmmWriteReady ();\r
862}\r
863\r
8a2d4996 864/**\r
865 SMM Fault Tolerant Write protocol notification event handler.\r
866\r
fa0737a8 867 Non-Volatile variable write may needs FTW protocol to reclaim when\r
8a2d4996 868 writting variable.\r
fa0737a8 869\r
8a2d4996 870 @param Protocol Points to the protocol's unique identifier\r
871 @param Interface Points to the interface instance\r
872 @param Handle The handle on which the interface was installed\r
873\r
874 @retval EFI_SUCCESS SmmEventCallback runs successfully\r
875 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.\r
fa0737a8 876\r
8a2d4996 877 **/\r
878EFI_STATUS\r
879EFIAPI\r
880SmmFtwNotificationEvent (\r
881 IN CONST EFI_GUID *Protocol,\r
882 IN VOID *Interface,\r
883 IN EFI_HANDLE Handle\r
884 )\r
885{\r
886 EFI_STATUS Status;\r
904e0ca9 887 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
8a2d4996 888 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;\r
889 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
890 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;\r
2c4b18e0 891 UINTN FtwMaxBlockSize;\r
fa0737a8 892\r
8a2d4996 893 if (mVariableModuleGlobal->FvbInstance != NULL) {\r
894 return EFI_SUCCESS;\r
895 }\r
896\r
897 //\r
898 // Ensure SMM FTW protocol is installed.\r
899 //\r
5c7fa429 900 Status = GetFtwProtocol ((VOID **)&FtwProtocol);\r
8a2d4996 901 if (EFI_ERROR (Status)) {\r
902 return Status;\r
903 }\r
904\r
2c4b18e0
SZ
905 Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize);\r
906 if (!EFI_ERROR (Status)) {\r
907 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) <= FtwMaxBlockSize);\r
908 }\r
909\r
904e0ca9
SZ
910 NvStorageVariableBase = NV_STORAGE_VARIABLE_BASE;\r
911 VariableStoreBase = NvStorageVariableBase + mNvFvHeaderCache->HeaderLength;\r
912\r
913 //\r
914 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.\r
915 //\r
916 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
917\r
8a2d4996 918 //\r
919 // Find the proper FVB protocol for variable.\r
920 //\r
8a2d4996 921 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);\r
922 if (EFI_ERROR (Status)) {\r
923 return EFI_NOT_FOUND;\r
924 }\r
925\r
926 mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
fa0737a8 927\r
8a2d4996 928 //\r
b59fd889 929 // Initializes variable write service after FTW was ready.\r
8a2d4996 930 //\r
b59fd889 931 VariableWriteServiceInitializeSmm ();\r
fa0737a8 932\r
8a2d4996 933 return EFI_SUCCESS;\r
934}\r
935\r
936\r
937/**\r
938 Variable Driver main entry point. The Variable driver places the 4 EFI\r
fa0737a8 939 runtime services in the EFI System Table and installs arch protocols\r
d00ed85e 940 for variable read and write services being available. It also registers\r
8a2d4996 941 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
942\r
8a2d4996 943 @retval EFI_SUCCESS Variable service successfully initialized.\r
944\r
945**/\r
946EFI_STATUS\r
947EFIAPI\r
a855f63e
AB
948MmVariableServiceInitialize (\r
949 VOID\r
8a2d4996 950 )\r
951{\r
952 EFI_STATUS Status;\r
953 EFI_HANDLE VariableHandle;\r
954 VOID *SmmFtwRegistration;\r
ff843847 955 VOID *SmmEndOfDxeRegistration;\r
2445a70e 956\r
8a2d4996 957 //\r
958 // Variable initialize.\r
959 //\r
960 Status = VariableCommonInitialize ();\r
961 ASSERT_EFI_ERROR (Status);\r
962\r
963 //\r
964 // Install the Smm Variable Protocol on a new handle.\r
965 //\r
966 VariableHandle = NULL;\r
a855f63e 967 Status = gMmst->MmInstallProtocolInterface (\r
8a2d4996 968 &VariableHandle,\r
969 &gEfiSmmVariableProtocolGuid,\r
970 EFI_NATIVE_INTERFACE,\r
971 &gSmmVariable\r
972 );\r
973 ASSERT_EFI_ERROR (Status);\r
974\r
a855f63e 975 Status = gMmst->MmInstallProtocolInterface (\r
efb01a10
SZ
976 &VariableHandle,\r
977 &gEdkiiSmmVarCheckProtocolGuid,\r
978 EFI_NATIVE_INTERFACE,\r
979 &mSmmVarCheck\r
980 );\r
981 ASSERT_EFI_ERROR (Status);\r
982\r
9b4a2032 983 mVariableBufferPayloadSize = GetMaxVariableSize () +\r
fa0737a8 984 OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) - GetVariableHeaderSize ();\r
5e5bb2a9 985\r
a855f63e 986 Status = gMmst->MmAllocatePool (\r
5e5bb2a9
SZ
987 EfiRuntimeServicesData,\r
988 mVariableBufferPayloadSize,\r
989 (VOID **)&mVariableBufferPayload\r
990 );\r
991 ASSERT_EFI_ERROR (Status);\r
992\r
8a2d4996 993 ///\r
994 /// Register SMM variable SMI handler\r
995 ///\r
996 VariableHandle = NULL;\r
a855f63e 997 Status = gMmst->MmiHandlerRegister (SmmVariableHandler, &gEfiSmmVariableProtocolGuid, &VariableHandle);\r
8a2d4996 998 ASSERT_EFI_ERROR (Status);\r
fa0737a8 999\r
8a2d4996 1000 //\r
1001 // Notify the variable wrapper driver the variable service is ready\r
1002 //\r
a855f63e 1003 VariableNotifySmmReady ();\r
fa0737a8 1004\r
ff843847
RN
1005 //\r
1006 // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.\r
1007 //\r
a855f63e
AB
1008 Status = gMmst->MmRegisterProtocolNotify (\r
1009 &gEfiMmEndOfDxeProtocolGuid,\r
ff843847
RN
1010 SmmEndOfDxeCallback,\r
1011 &SmmEndOfDxeRegistration\r
1012 );\r
1013 ASSERT_EFI_ERROR (Status);\r
1014\r
7cd69959
SZ
1015 if (!PcdGetBool (PcdEmuVariableNvModeEnable)) {\r
1016 //\r
1017 // Register FtwNotificationEvent () notify function.\r
1018 //\r
1019 Status = gMmst->MmRegisterProtocolNotify (\r
1020 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
1021 SmmFtwNotificationEvent,\r
1022 &SmmFtwRegistration\r
1023 );\r
1024 ASSERT_EFI_ERROR (Status);\r
1025\r
1026 SmmFtwNotificationEvent (NULL, NULL, NULL);\r
1027 } else {\r
1028 //\r
1029 // Emulated non-volatile variable mode does not depend on FVB and FTW.\r
1030 //\r
1031 VariableWriteServiceInitializeSmm ();\r
1032 }\r
fa0737a8 1033\r
8a2d4996 1034 return EFI_SUCCESS;\r
1035}\r
1036\r
1037\r