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