]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / VariableSmmRuntimeDxe.c
CommitLineData
8a2d4996 1/** @file\r
8a2d4996 2 Implement all four UEFI Runtime Variable services for the nonvolatile\r
3 and volatile storage space and install variable architecture protocol\r
4 based on SMM variable module.\r
5\r
18a7dbbc
SZ
6 Caution: This module requires additional review when modified.\r
7 This driver will have external input - variable data.\r
8 This external input must be validated carefully to avoid security issue like\r
9 buffer overflow, integer overflow.\r
10\r
11 RuntimeServiceGetVariable() and RuntimeServiceSetVariable() are external API\r
12 to receive data buffer. The size should be checked carefully.\r
13\r
14 InitCommunicateBuffer() is really function to check the variable data size.\r
15\r
dc9bd6ed 16Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>\r
9d510e61 17SPDX-License-Identifier: BSD-2-Clause-Patent\r
8a2d4996 18\r
19**/\r
d00ed85e 20#include <PiDxe.h>\r
8a2d4996 21#include <Protocol/VariableWrite.h>\r
22#include <Protocol/Variable.h>\r
23#include <Protocol/SmmCommunication.h>\r
d00ed85e 24#include <Protocol/SmmVariable.h>\r
ff843847 25#include <Protocol/VariableLock.h>\r
efb01a10 26#include <Protocol/VarCheck.h>\r
8a2d4996 27\r
28#include <Library/UefiBootServicesTableLib.h>\r
29#include <Library/UefiRuntimeServicesTableLib.h>\r
30#include <Library/MemoryAllocationLib.h>\r
31#include <Library/UefiDriverEntryPoint.h>\r
32#include <Library/UefiRuntimeLib.h>\r
33#include <Library/BaseMemoryLib.h>\r
34#include <Library/DebugLib.h>\r
8a2d4996 35#include <Library/UefiLib.h>\r
36#include <Library/BaseLib.h>\r
37\r
38#include <Guid/EventGroup.h>\r
d00ed85e 39#include <Guid/SmmVariableCommon.h>\r
8a2d4996 40\r
00663d04
LE
41#include "PrivilegePolymorphic.h"\r
42\r
fa0737a8 43EFI_HANDLE mHandle = NULL;\r
8a2d4996 44EFI_SMM_VARIABLE_PROTOCOL *mSmmVariable = NULL;\r
45EFI_EVENT mVirtualAddressChangeEvent = NULL;\r
46EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL;\r
47UINT8 *mVariableBuffer = NULL;\r
48UINT8 *mVariableBufferPhysical = NULL;\r
8a2d4996 49UINTN mVariableBufferSize;\r
5e5bb2a9 50UINTN mVariableBufferPayloadSize;\r
6ed1ec59 51EFI_LOCK mVariableServicesLock;\r
ff843847 52EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock;\r
efb01a10 53EDKII_VAR_CHECK_PROTOCOL mVarCheck;\r
8a2d4996 54\r
dc9bd6ed
ZC
55/**\r
56 Some Secure Boot Policy Variable may update following other variable changes(SecureBoot follows PK change, etc).\r
57 Record their initial State when variable write service is ready.\r
58\r
59**/\r
60VOID\r
61EFIAPI\r
62RecordSecureBootPolicyVarData(\r
63 VOID\r
64 );\r
65\r
6ed1ec59
SZ
66/**\r
67 Acquires lock only at boot time. Simply returns at runtime.\r
68\r
69 This is a temperary function that will be removed when\r
70 EfiAcquireLock() in UefiLib can handle the call in UEFI\r
71 Runtimer driver in RT phase.\r
72 It calls EfiAcquireLock() at boot time, and simply returns\r
73 at runtime.\r
74\r
75 @param Lock A pointer to the lock to acquire.\r
76\r
77**/\r
78VOID\r
79AcquireLockOnlyAtBootTime (\r
80 IN EFI_LOCK *Lock\r
81 )\r
82{\r
83 if (!EfiAtRuntime ()) {\r
84 EfiAcquireLock (Lock);\r
85 }\r
86}\r
87\r
88/**\r
89 Releases lock only at boot time. Simply returns at runtime.\r
90\r
91 This is a temperary function which will be removed when\r
92 EfiReleaseLock() in UefiLib can handle the call in UEFI\r
93 Runtimer driver in RT phase.\r
94 It calls EfiReleaseLock() at boot time and simply returns\r
95 at runtime.\r
96\r
97 @param Lock A pointer to the lock to release.\r
98\r
99**/\r
100VOID\r
101ReleaseLockOnlyAtBootTime (\r
102 IN EFI_LOCK *Lock\r
103 )\r
104{\r
105 if (!EfiAtRuntime ()) {\r
106 EfiReleaseLock (Lock);\r
107 }\r
108}\r
8a2d4996 109\r
110/**\r
111 Initialize the communicate buffer using DataSize and Function.\r
112\r
113 The communicate size is: SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +\r
114 DataSize.\r
115\r
18a7dbbc
SZ
116 Caution: This function may receive untrusted input.\r
117 The data size external input, so this function will validate it carefully to avoid buffer overflow.\r
118\r
8a2d4996 119 @param[out] DataPtr Points to the data in the communicate buffer.\r
120 @param[in] DataSize The data size to send to SMM.\r
121 @param[in] Function The function number to initialize the communicate header.\r
fa0737a8 122\r
8a2d4996 123 @retval EFI_INVALID_PARAMETER The data size is too big.\r
124 @retval EFI_SUCCESS Find the specified variable.\r
125\r
126**/\r
127EFI_STATUS\r
128InitCommunicateBuffer (\r
129 OUT VOID **DataPtr OPTIONAL,\r
130 IN UINTN DataSize,\r
131 IN UINTN Function\r
132 )\r
133{\r
fa0737a8
SZ
134 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader;\r
135 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;\r
136\r
8a2d4996 137\r
8a2d4996 138 if (DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE > mVariableBufferSize) {\r
139 return EFI_INVALID_PARAMETER;\r
140 }\r
141\r
142 SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) mVariableBuffer;\r
143 CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);\r
144 SmmCommunicateHeader->MessageLength = DataSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
fa0737a8 145\r
8a2d4996 146 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader->Data;\r
147 SmmVariableFunctionHeader->Function = Function;\r
148 if (DataPtr != NULL) {\r
149 *DataPtr = SmmVariableFunctionHeader->Data;\r
150 }\r
151\r
152 return EFI_SUCCESS;\r
153}\r
154\r
155\r
156/**\r
157 Send the data in communicate buffer to SMM.\r
158\r
159 @param[in] DataSize This size of the function header and the data.\r
160\r
32732a33 161 @retval EFI_SUCCESS Success is returned from the functin in SMM.\r
fa0737a8
SZ
162 @retval Others Failure is returned from the function in SMM.\r
163\r
8a2d4996 164**/\r
165EFI_STATUS\r
166SendCommunicateBuffer (\r
167 IN UINTN DataSize\r
168 )\r
169{\r
170 EFI_STATUS Status;\r
171 UINTN CommSize;\r
fa0737a8 172 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader;\r
8a2d4996 173 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;\r
fa0737a8 174\r
8a2d4996 175 CommSize = DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
176 Status = mSmmCommunication->Communicate (mSmmCommunication, mVariableBufferPhysical, &CommSize);\r
177 ASSERT_EFI_ERROR (Status);\r
178\r
179 SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) mVariableBuffer;\r
180 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)SmmCommunicateHeader->Data;\r
181 return SmmVariableFunctionHeader->ReturnStatus;\r
182}\r
183\r
ff843847
RN
184/**\r
185 Mark a variable that will become read-only after leaving the DXE phase of execution.\r
186\r
187 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.\r
188 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.\r
189 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.\r
190\r
191 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked\r
192 as pending to be read-only.\r
193 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.\r
194 Or VariableName is an empty string.\r
195 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
196 already been signaled.\r
197 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.\r
198**/\r
199EFI_STATUS\r
200EFIAPI\r
201VariableLockRequestToLock (\r
202 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This,\r
203 IN CHAR16 *VariableName,\r
204 IN EFI_GUID *VendorGuid\r
205 )\r
206{\r
207 EFI_STATUS Status;\r
51547bb8 208 UINTN VariableNameSize;\r
ff843847
RN
209 UINTN PayloadSize;\r
210 SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *VariableToLock;\r
211\r
212 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
213 return EFI_INVALID_PARAMETER;\r
214 }\r
215\r
51547bb8 216 VariableNameSize = StrSize (VariableName);\r
4e1005ec 217 VariableToLock = NULL;\r
51547bb8
RN
218\r
219 //\r
220 // If VariableName exceeds SMM payload limit. Return failure\r
221 //\r
222 if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE, Name)) {\r
223 return EFI_INVALID_PARAMETER;\r
224 }\r
225\r
ff843847
RN
226 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
227\r
228 //\r
229 // Init the communicate buffer. The buffer data size is:\r
230 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
231 //\r
51547bb8 232 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE, Name) + VariableNameSize;\r
ff843847
RN
233 Status = InitCommunicateBuffer ((VOID **) &VariableToLock, PayloadSize, SMM_VARIABLE_FUNCTION_LOCK_VARIABLE);\r
234 if (EFI_ERROR (Status)) {\r
235 goto Done;\r
236 }\r
237 ASSERT (VariableToLock != NULL);\r
238\r
239 CopyGuid (&VariableToLock->Guid, VendorGuid);\r
51547bb8 240 VariableToLock->NameSize = VariableNameSize;\r
ff843847
RN
241 CopyMem (VariableToLock->Name, VariableName, VariableToLock->NameSize);\r
242\r
243 //\r
244 // Send data to SMM.\r
245 //\r
246 Status = SendCommunicateBuffer (PayloadSize);\r
247\r
248Done:\r
249 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
250 return Status;\r
251}\r
8a2d4996 252\r
efb01a10
SZ
253/**\r
254 Register SetVariable check handler.\r
255\r
256 @param[in] Handler Pointer to check handler.\r
257\r
258 @retval EFI_SUCCESS The SetVariable check handler was registered successfully.\r
259 @retval EFI_INVALID_PARAMETER Handler is NULL.\r
260 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
261 already been signaled.\r
262 @retval EFI_OUT_OF_RESOURCES There is not enough resource for the SetVariable check handler register request.\r
263 @retval EFI_UNSUPPORTED This interface is not implemented.\r
264 For example, it is unsupported in VarCheck protocol if both VarCheck and SmmVarCheck protocols are present.\r
265\r
266**/\r
267EFI_STATUS\r
268EFIAPI\r
269VarCheckRegisterSetVariableCheckHandler (\r
270 IN VAR_CHECK_SET_VARIABLE_CHECK_HANDLER Handler\r
271 )\r
272{\r
273 return EFI_UNSUPPORTED;\r
274}\r
275\r
276/**\r
277 Variable property set.\r
278\r
279 @param[in] Name Pointer to the variable name.\r
280 @param[in] Guid Pointer to the vendor GUID.\r
281 @param[in] VariableProperty Pointer to the input variable property.\r
282\r
283 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was set successfully.\r
284 @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string,\r
285 or the fields of VariableProperty are not valid.\r
286 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
287 already been signaled.\r
288 @retval EFI_OUT_OF_RESOURCES There is not enough resource for the variable property set request.\r
289\r
290**/\r
291EFI_STATUS\r
292EFIAPI\r
293VarCheckVariablePropertySet (\r
294 IN CHAR16 *Name,\r
295 IN EFI_GUID *Guid,\r
296 IN VAR_CHECK_VARIABLE_PROPERTY *VariableProperty\r
297 )\r
298{\r
299 EFI_STATUS Status;\r
300 UINTN VariableNameSize;\r
301 UINTN PayloadSize;\r
302 SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProperty;\r
303\r
304 if (Name == NULL || Name[0] == 0 || Guid == NULL) {\r
305 return EFI_INVALID_PARAMETER;\r
306 }\r
307\r
308 if (VariableProperty == NULL) {\r
309 return EFI_INVALID_PARAMETER;\r
310 }\r
311\r
312 if (VariableProperty->Revision != VAR_CHECK_VARIABLE_PROPERTY_REVISION) {\r
313 return EFI_INVALID_PARAMETER;\r
314 }\r
315\r
316 VariableNameSize = StrSize (Name);\r
317 CommVariableProperty = NULL;\r
318\r
319 //\r
320 // If VariableName exceeds SMM payload limit. Return failure\r
321 //\r
322 if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) {\r
323 return EFI_INVALID_PARAMETER;\r
324 }\r
325\r
326 AcquireLockOnlyAtBootTime (&mVariableServicesLock);\r
327\r
328 //\r
329 // Init the communicate buffer. The buffer data size is:\r
330 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
331 //\r
332 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) + VariableNameSize;\r
333 Status = InitCommunicateBuffer ((VOID **) &CommVariableProperty, PayloadSize, SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_SET);\r
334 if (EFI_ERROR (Status)) {\r
335 goto Done;\r
336 }\r
337 ASSERT (CommVariableProperty != NULL);\r
338\r
339 CopyGuid (&CommVariableProperty->Guid, Guid);\r
340 CopyMem (&CommVariableProperty->VariableProperty, VariableProperty, sizeof (*VariableProperty));\r
341 CommVariableProperty->NameSize = VariableNameSize;\r
342 CopyMem (CommVariableProperty->Name, Name, CommVariableProperty->NameSize);\r
343\r
344 //\r
345 // Send data to SMM.\r
346 //\r
347 Status = SendCommunicateBuffer (PayloadSize);\r
348\r
349Done:\r
350 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
351 return Status;\r
352}\r
353\r
354/**\r
355 Variable property get.\r
356\r
357 @param[in] Name Pointer to the variable name.\r
358 @param[in] Guid Pointer to the vendor GUID.\r
359 @param[out] VariableProperty Pointer to the output variable property.\r
360\r
361 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was got successfully.\r
362 @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string.\r
363 @retval EFI_NOT_FOUND The property of variable specified by the Name and Guid was not found.\r
364\r
365**/\r
366EFI_STATUS\r
367EFIAPI\r
368VarCheckVariablePropertyGet (\r
369 IN CHAR16 *Name,\r
370 IN EFI_GUID *Guid,\r
371 OUT VAR_CHECK_VARIABLE_PROPERTY *VariableProperty\r
372 )\r
373{\r
374 EFI_STATUS Status;\r
375 UINTN VariableNameSize;\r
376 UINTN PayloadSize;\r
377 SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProperty;\r
378\r
379 if (Name == NULL || Name[0] == 0 || Guid == NULL) {\r
380 return EFI_INVALID_PARAMETER;\r
381 }\r
382\r
383 if (VariableProperty == NULL) {\r
384 return EFI_INVALID_PARAMETER;\r
385 }\r
386\r
387 VariableNameSize = StrSize (Name);\r
388 CommVariableProperty = NULL;\r
389\r
390 //\r
391 // If VariableName exceeds SMM payload limit. Return failure\r
392 //\r
393 if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) {\r
394 return EFI_INVALID_PARAMETER;\r
395 }\r
396\r
397 AcquireLockOnlyAtBootTime (&mVariableServicesLock);\r
398\r
399 //\r
400 // Init the communicate buffer. The buffer data size is:\r
401 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
402 //\r
403 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) + VariableNameSize;\r
404 Status = InitCommunicateBuffer ((VOID **) &CommVariableProperty, PayloadSize, SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET);\r
405 if (EFI_ERROR (Status)) {\r
406 goto Done;\r
407 }\r
408 ASSERT (CommVariableProperty != NULL);\r
409\r
410 CopyGuid (&CommVariableProperty->Guid, Guid);\r
411 CommVariableProperty->NameSize = VariableNameSize;\r
412 CopyMem (CommVariableProperty->Name, Name, CommVariableProperty->NameSize);\r
413\r
414 //\r
415 // Send data to SMM.\r
416 //\r
417 Status = SendCommunicateBuffer (PayloadSize);\r
418 if (Status == EFI_SUCCESS) {\r
419 CopyMem (VariableProperty, &CommVariableProperty->VariableProperty, sizeof (*VariableProperty));\r
420 }\r
421\r
422Done:\r
423 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
424 return Status;\r
425}\r
426\r
8a2d4996 427/**\r
428 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
429\r
18a7dbbc
SZ
430 Caution: This function may receive untrusted input.\r
431 The data size is external input, so this function will validate it carefully to avoid buffer overflow.\r
432\r
8a2d4996 433 @param[in] VariableName Name of Variable to be found.\r
434 @param[in] VendorGuid Variable vendor GUID.\r
435 @param[out] Attributes Attribute value of the variable found.\r
436 @param[in, out] DataSize Size of Data found. If size is less than the\r
437 data, this value contains the required size.\r
438 @param[out] Data Data pointer.\r
fa0737a8 439\r
8a2d4996 440 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
441 @retval EFI_SUCCESS Find the specified variable.\r
442 @retval EFI_NOT_FOUND Not found.\r
443 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
444\r
445**/\r
446EFI_STATUS\r
447EFIAPI\r
448RuntimeServiceGetVariable (\r
449 IN CHAR16 *VariableName,\r
450 IN EFI_GUID *VendorGuid,\r
451 OUT UINT32 *Attributes OPTIONAL,\r
452 IN OUT UINTN *DataSize,\r
453 OUT VOID *Data\r
454 )\r
455{\r
456 EFI_STATUS Status;\r
457 UINTN PayloadSize;\r
458 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;\r
3a146f2a 459 UINTN TempDataSize;\r
9d00d20e 460 UINTN VariableNameSize;\r
8a2d4996 461\r
462 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
463 return EFI_INVALID_PARAMETER;\r
464 }\r
465\r
3a146f2a 466 TempDataSize = *DataSize;\r
9d00d20e 467 VariableNameSize = StrSize (VariableName);\r
4e1005ec 468 SmmVariableHeader = NULL;\r
3a146f2a 469\r
470 //\r
471 // If VariableName exceeds SMM payload limit. Return failure\r
472 //\r
5e5bb2a9 473 if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {\r
3588bb35
SZ
474 return EFI_INVALID_PARAMETER;\r
475 }\r
476\r
6ed1ec59
SZ
477 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
478\r
8a2d4996 479 //\r
480 // Init the communicate buffer. The buffer data size is:\r
6ed1ec59 481 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
8a2d4996 482 //\r
5e5bb2a9 483 if (TempDataSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize) {\r
3a146f2a 484 //\r
485 // If output data buffer exceed SMM payload limit. Trim output buffer to SMM payload size\r
486 //\r
5e5bb2a9 487 TempDataSize = mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize;\r
3a146f2a 488 }\r
9d00d20e 489 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + VariableNameSize + TempDataSize;\r
3a146f2a 490\r
5c7fa429 491 Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);\r
8a2d4996 492 if (EFI_ERROR (Status)) {\r
6ed1ec59 493 goto Done;\r
8a2d4996 494 }\r
495 ASSERT (SmmVariableHeader != NULL);\r
496\r
497 CopyGuid (&SmmVariableHeader->Guid, VendorGuid);\r
3a146f2a 498 SmmVariableHeader->DataSize = TempDataSize;\r
9d00d20e 499 SmmVariableHeader->NameSize = VariableNameSize;\r
8a2d4996 500 if (Attributes == NULL) {\r
501 SmmVariableHeader->Attributes = 0;\r
502 } else {\r
503 SmmVariableHeader->Attributes = *Attributes;\r
504 }\r
505 CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);\r
506\r
507 //\r
508 // Send data to SMM.\r
509 //\r
510 Status = SendCommunicateBuffer (PayloadSize);\r
511\r
512 //\r
513 // Get data from SMM.\r
514 //\r
3a146f2a 515 if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {\r
516 //\r
517 // SMM CommBuffer DataSize can be a trimed value\r
518 // Only update DataSize when needed\r
519 //\r
520 *DataSize = SmmVariableHeader->DataSize;\r
521 }\r
8a2d4996 522 if (Attributes != NULL) {\r
523 *Attributes = SmmVariableHeader->Attributes;\r
524 }\r
525\r
526 if (EFI_ERROR (Status)) {\r
6ed1ec59 527 goto Done;\r
8a2d4996 528 }\r
529\r
82e47eb2
SZ
530 if (Data != NULL) {\r
531 CopyMem (Data, (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize, SmmVariableHeader->DataSize);\r
532 } else {\r
533 Status = EFI_INVALID_PARAMETER;\r
534 }\r
8a2d4996 535\r
6ed1ec59
SZ
536Done:\r
537 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
8a2d4996 538 return Status;\r
539}\r
540\r
541\r
542/**\r
543 This code Finds the Next available variable.\r
544\r
545 @param[in, out] VariableNameSize Size of the variable name.\r
546 @param[in, out] VariableName Pointer to variable name.\r
547 @param[in, out] VendorGuid Variable Vendor Guid.\r
548\r
549 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
550 @retval EFI_SUCCESS Find the specified variable.\r
551 @retval EFI_NOT_FOUND Not found.\r
552 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
553\r
554**/\r
555EFI_STATUS\r
556EFIAPI\r
557RuntimeServiceGetNextVariableName (\r
558 IN OUT UINTN *VariableNameSize,\r
559 IN OUT CHAR16 *VariableName,\r
560 IN OUT EFI_GUID *VendorGuid\r
561 )\r
562{\r
563 EFI_STATUS Status;\r
564 UINTN PayloadSize;\r
565 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *SmmGetNextVariableName;\r
9d00d20e
SZ
566 UINTN OutVariableNameSize;\r
567 UINTN InVariableNameSize;\r
8a2d4996 568\r
569 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
570 return EFI_INVALID_PARAMETER;\r
571 }\r
6ed1ec59 572\r
9d00d20e
SZ
573 OutVariableNameSize = *VariableNameSize;\r
574 InVariableNameSize = StrSize (VariableName);\r
4e1005ec 575 SmmGetNextVariableName = NULL;\r
0c55190f 576\r
577 //\r
578 // If input string exceeds SMM payload limit. Return failure\r
579 //\r
5e5bb2a9 580 if (InVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
3588bb35
SZ
581 return EFI_INVALID_PARAMETER;\r
582 }\r
583\r
6ed1ec59
SZ
584 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
585\r
8a2d4996 586 //\r
587 // Init the communicate buffer. The buffer data size is:\r
588 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
589 //\r
5e5bb2a9 590 if (OutVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
0c55190f 591 //\r
592 // If output buffer exceed SMM payload limit. Trim output buffer to SMM payload size\r
593 //\r
5e5bb2a9 594 OutVariableNameSize = mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);\r
0c55190f 595 }\r
596 //\r
597 // Payload should be Guid + NameSize + MAX of Input & Output buffer\r
598 //\r
9d00d20e 599 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + MAX (OutVariableNameSize, InVariableNameSize);\r
0c55190f 600\r
5c7fa429 601 Status = InitCommunicateBuffer ((VOID **)&SmmGetNextVariableName, PayloadSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME);\r
8a2d4996 602 if (EFI_ERROR (Status)) {\r
6ed1ec59 603 goto Done;\r
8a2d4996 604 }\r
605 ASSERT (SmmGetNextVariableName != NULL);\r
606\r
0c55190f 607 //\r
608 // SMM comm buffer->NameSize is buffer size for return string\r
609 //\r
9d00d20e 610 SmmGetNextVariableName->NameSize = OutVariableNameSize;\r
0c55190f 611\r
8a2d4996 612 CopyGuid (&SmmGetNextVariableName->Guid, VendorGuid);\r
0c55190f 613 //\r
614 // Copy whole string\r
615 //\r
9d00d20e
SZ
616 CopyMem (SmmGetNextVariableName->Name, VariableName, InVariableNameSize);\r
617 if (OutVariableNameSize > InVariableNameSize) {\r
618 ZeroMem ((UINT8 *) SmmGetNextVariableName->Name + InVariableNameSize, OutVariableNameSize - InVariableNameSize);\r
619 }\r
8a2d4996 620\r
621 //\r
622 // Send data to SMM\r
623 //\r
624 Status = SendCommunicateBuffer (PayloadSize);\r
625\r
626 //\r
627 // Get data from SMM.\r
628 //\r
9d00d20e
SZ
629 if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {\r
630 //\r
631 // SMM CommBuffer NameSize can be a trimed value\r
632 // Only update VariableNameSize when needed\r
633 //\r
634 *VariableNameSize = SmmGetNextVariableName->NameSize;\r
635 }\r
8a2d4996 636 if (EFI_ERROR (Status)) {\r
6ed1ec59 637 goto Done;\r
8a2d4996 638 }\r
fa0737a8 639\r
8a2d4996 640 CopyGuid (VendorGuid, &SmmGetNextVariableName->Guid);\r
fa0737a8 641 CopyMem (VariableName, SmmGetNextVariableName->Name, SmmGetNextVariableName->NameSize);\r
8a2d4996 642\r
6ed1ec59
SZ
643Done:\r
644 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
8a2d4996 645 return Status;\r
646}\r
647\r
648/**\r
649 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
650\r
18a7dbbc
SZ
651 Caution: This function may receive untrusted input.\r
652 The data size and data are external input, so this function will validate it carefully to avoid buffer overflow.\r
653\r
8a2d4996 654 @param[in] VariableName Name of Variable to be found.\r
655 @param[in] VendorGuid Variable vendor GUID.\r
656 @param[in] Attributes Attribute value of the variable found\r
657 @param[in] DataSize Size of Data found. If size is less than the\r
658 data, this value contains the required size.\r
659 @param[in] Data Data pointer.\r
660\r
661 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
662 @retval EFI_SUCCESS Set successfully.\r
663 @retval EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
664 @retval EFI_NOT_FOUND Not found.\r
665 @retval EFI_WRITE_PROTECTED Variable is read-only.\r
666\r
667**/\r
668EFI_STATUS\r
669EFIAPI\r
670RuntimeServiceSetVariable (\r
671 IN CHAR16 *VariableName,\r
672 IN EFI_GUID *VendorGuid,\r
673 IN UINT32 Attributes,\r
674 IN UINTN DataSize,\r
675 IN VOID *Data\r
676 )\r
677{\r
678 EFI_STATUS Status;\r
fa0737a8 679 UINTN PayloadSize;\r
8a2d4996 680 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;\r
9d00d20e 681 UINTN VariableNameSize;\r
fa0737a8 682\r
8a2d4996 683 //\r
684 // Check input parameters.\r
685 //\r
686 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
687 return EFI_INVALID_PARAMETER;\r
fa0737a8 688 }\r
8a2d4996 689\r
690 if (DataSize != 0 && Data == NULL) {\r
691 return EFI_INVALID_PARAMETER;\r
692 }\r
6ed1ec59 693\r
9d00d20e 694 VariableNameSize = StrSize (VariableName);\r
4e1005ec 695 SmmVariableHeader = NULL;\r
3588bb35 696\r
5e5bb2a9
SZ
697 //\r
698 // If VariableName or DataSize exceeds SMM payload limit. Return failure\r
699 //\r
700 if ((VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||\r
701 (DataSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize)){\r
56251c66 702 return EFI_INVALID_PARAMETER;\r
703 }\r
704\r
6ed1ec59 705 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
fa0737a8 706\r
8a2d4996 707 //\r
708 // Init the communicate buffer. The buffer data size is:\r
709 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
710 //\r
9d00d20e 711 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + VariableNameSize + DataSize;\r
5c7fa429 712 Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_SET_VARIABLE);\r
8a2d4996 713 if (EFI_ERROR (Status)) {\r
6ed1ec59 714 goto Done;\r
8a2d4996 715 }\r
716 ASSERT (SmmVariableHeader != NULL);\r
717\r
718 CopyGuid ((EFI_GUID *) &SmmVariableHeader->Guid, VendorGuid);\r
719 SmmVariableHeader->DataSize = DataSize;\r
9d00d20e 720 SmmVariableHeader->NameSize = VariableNameSize;\r
8a2d4996 721 SmmVariableHeader->Attributes = Attributes;\r
722 CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);\r
723 CopyMem ((UINT8 *) SmmVariableHeader->Name + SmmVariableHeader->NameSize, Data, DataSize);\r
724\r
725 //\r
726 // Send data to SMM.\r
727 //\r
728 Status = SendCommunicateBuffer (PayloadSize);\r
6ed1ec59
SZ
729\r
730Done:\r
731 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
fa0737a8
SZ
732\r
733 if (!EfiAtRuntime ()) {\r
734 if (!EFI_ERROR (Status)) {\r
735 SecureBootHook (\r
736 VariableName,\r
737 VendorGuid\r
738 );\r
739 }\r
740 }\r
8a2d4996 741 return Status;\r
742}\r
743\r
744\r
745/**\r
746 This code returns information about the EFI variables.\r
747\r
748 @param[in] Attributes Attributes bitmask to specify the type of variables\r
749 on which to return information.\r
750 @param[out] MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
751 for the EFI variables associated with the attributes specified.\r
752 @param[out] RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
753 for EFI variables associated with the attributes specified.\r
754 @param[out] MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
755 associated with the attributes specified.\r
756\r
757 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
758 @retval EFI_SUCCESS Query successfully.\r
759 @retval EFI_UNSUPPORTED The attribute is not supported on this platform.\r
760\r
761**/\r
762EFI_STATUS\r
763EFIAPI\r
764RuntimeServiceQueryVariableInfo (\r
765 IN UINT32 Attributes,\r
766 OUT UINT64 *MaximumVariableStorageSize,\r
767 OUT UINT64 *RemainingVariableStorageSize,\r
768 OUT UINT64 *MaximumVariableSize\r
769 )\r
770{\r
771 EFI_STATUS Status;\r
772 UINTN PayloadSize;\r
773 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *SmmQueryVariableInfo;\r
774\r
4e1005ec
ED
775 SmmQueryVariableInfo = NULL;\r
776\r
8a2d4996 777 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
778 return EFI_INVALID_PARAMETER;\r
779 }\r
6ed1ec59
SZ
780\r
781 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
782\r
8a2d4996 783 //\r
784 // Init the communicate buffer. The buffer data size is:\r
785 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize;\r
786 //\r
d00ed85e 787 PayloadSize = sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO);\r
5c7fa429 788 Status = InitCommunicateBuffer ((VOID **)&SmmQueryVariableInfo, PayloadSize, SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO);\r
8a2d4996 789 if (EFI_ERROR (Status)) {\r
6ed1ec59 790 goto Done;\r
8a2d4996 791 }\r
792 ASSERT (SmmQueryVariableInfo != NULL);\r
793\r
794 SmmQueryVariableInfo->Attributes = Attributes;\r
795\r
796 //\r
797 // Send data to SMM.\r
798 //\r
799 Status = SendCommunicateBuffer (PayloadSize);\r
800 if (EFI_ERROR (Status)) {\r
6ed1ec59 801 goto Done;\r
8a2d4996 802 }\r
803\r
804 //\r
805 // Get data from SMM.\r
806 //\r
807 *MaximumVariableSize = SmmQueryVariableInfo->MaximumVariableSize;\r
808 *MaximumVariableStorageSize = SmmQueryVariableInfo->MaximumVariableStorageSize;\r
fa0737a8 809 *RemainingVariableStorageSize = SmmQueryVariableInfo->RemainingVariableStorageSize;\r
6ed1ec59
SZ
810\r
811Done:\r
812 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
aab9212f 813 return Status;\r
8a2d4996 814}\r
815\r
816\r
817/**\r
818 Exit Boot Services Event notification handler.\r
819\r
820 Notify SMM variable driver about the event.\r
821\r
822 @param[in] Event Event whose notification function is being invoked.\r
823 @param[in] Context Pointer to the notification function's context.\r
824\r
825**/\r
826VOID\r
827EFIAPI\r
828OnExitBootServices (\r
829 IN EFI_EVENT Event,\r
830 IN VOID *Context\r
831 )\r
832{\r
833 //\r
834 // Init the communicate buffer. The buffer data size is:\r
835 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.\r
836 //\r
fa0737a8 837 InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE);\r
8a2d4996 838\r
839 //\r
840 // Send data to SMM.\r
841 //\r
842 SendCommunicateBuffer (0);\r
843}\r
844\r
845\r
846/**\r
847 On Ready To Boot Services Event notification handler.\r
848\r
849 Notify SMM variable driver about the event.\r
850\r
851 @param[in] Event Event whose notification function is being invoked\r
852 @param[in] Context Pointer to the notification function's context\r
853\r
854**/\r
855VOID\r
856EFIAPI\r
857OnReadyToBoot (\r
858 IN EFI_EVENT Event,\r
859 IN VOID *Context\r
860 )\r
861{\r
862 //\r
863 // Init the communicate buffer. The buffer data size is:\r
864 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.\r
865 //\r
866 InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_READY_TO_BOOT);\r
fa0737a8 867\r
8a2d4996 868 //\r
869 // Send data to SMM.\r
870 //\r
871 SendCommunicateBuffer (0);\r
fa0737a8
SZ
872\r
873 gBS->CloseEvent (Event);\r
8a2d4996 874}\r
875\r
876\r
877/**\r
878 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
879\r
880 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
881 It convers pointer to new virtual address.\r
882\r
883 @param[in] Event Event whose notification function is being invoked.\r
884 @param[in] Context Pointer to the notification function's context.\r
885\r
886**/\r
887VOID\r
888EFIAPI\r
889VariableAddressChangeEvent (\r
890 IN EFI_EVENT Event,\r
891 IN VOID *Context\r
892 )\r
893{\r
894 EfiConvertPointer (0x0, (VOID **) &mVariableBuffer);\r
895 EfiConvertPointer (0x0, (VOID **) &mSmmCommunication);\r
896}\r
897\r
fa0737a8
SZ
898/**\r
899 This code gets variable payload size.\r
900\r
901 @param[out] VariablePayloadSize Output pointer to variable payload size.\r
902\r
903 @retval EFI_SUCCESS Get successfully.\r
904 @retval Others Get unsuccessfully.\r
905\r
906**/\r
907EFI_STATUS\r
908EFIAPI\r
909GetVariablePayloadSize (\r
910 OUT UINTN *VariablePayloadSize\r
911 )\r
912{\r
913 EFI_STATUS Status;\r
914 SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE *SmmGetPayloadSize;\r
915 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader;\r
916 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;\r
917 UINTN CommSize;\r
918 UINT8 *CommBuffer;\r
919\r
920 SmmGetPayloadSize = NULL;\r
921 CommBuffer = NULL;\r
922\r
923 if(VariablePayloadSize == NULL) {\r
924 return EFI_INVALID_PARAMETER;\r
925 }\r
926\r
927 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
928\r
929 //\r
930 // Init the communicate buffer. The buffer data size is:\r
931 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE);\r
932 //\r
933 CommSize = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE);\r
934 CommBuffer = AllocateZeroPool (CommSize);\r
935 if (CommBuffer == NULL) {\r
936 Status = EFI_OUT_OF_RESOURCES;\r
937 goto Done;\r
938 }\r
939\r
940 SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) CommBuffer;\r
941 CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);\r
942 SmmCommunicateHeader->MessageLength = SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE);\r
943\r
944 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader->Data;\r
945 SmmVariableFunctionHeader->Function = SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE;\r
946 SmmGetPayloadSize = (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE *) SmmVariableFunctionHeader->Data;\r
947\r
948 //\r
949 // Send data to SMM.\r
950 //\r
951 Status = mSmmCommunication->Communicate (mSmmCommunication, CommBuffer, &CommSize);\r
952 ASSERT_EFI_ERROR (Status);\r
953\r
954 Status = SmmVariableFunctionHeader->ReturnStatus;\r
955 if (EFI_ERROR (Status)) {\r
956 goto Done;\r
957 }\r
958\r
959 //\r
960 // Get data from SMM.\r
961 //\r
962 *VariablePayloadSize = SmmGetPayloadSize->VariablePayloadSize;\r
963\r
964Done:\r
965 if (CommBuffer != NULL) {\r
966 FreePool (CommBuffer);\r
967 }\r
968 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
969 return Status;\r
970}\r
8a2d4996 971\r
972/**\r
973 Initialize variable service and install Variable Architectural protocol.\r
974\r
975 @param[in] Event Event whose notification function is being invoked.\r
976 @param[in] Context Pointer to the notification function's context.\r
fa0737a8 977\r
8a2d4996 978**/\r
979VOID\r
980EFIAPI\r
981SmmVariableReady (\r
982 IN EFI_EVENT Event,\r
983 IN VOID *Context\r
984 )\r
985{\r
986 EFI_STATUS Status;\r
987\r
988 Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID **)&mSmmVariable);\r
989 if (EFI_ERROR (Status)) {\r
990 return;\r
991 }\r
fa0737a8 992\r
8a2d4996 993 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);\r
994 ASSERT_EFI_ERROR (Status);\r
fa0737a8 995\r
8a2d4996 996 //\r
5e5bb2a9 997 // Allocate memory for variable communicate buffer.\r
8a2d4996 998 //\r
fa0737a8
SZ
999 Status = GetVariablePayloadSize (&mVariableBufferPayloadSize);\r
1000 ASSERT_EFI_ERROR (Status);\r
5e5bb2a9 1001 mVariableBufferSize = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + mVariableBufferPayloadSize;\r
8a2d4996 1002 mVariableBuffer = AllocateRuntimePool (mVariableBufferSize);\r
1003 ASSERT (mVariableBuffer != NULL);\r
1004\r
1005 //\r
1006 // Save the buffer physical address used for SMM conmunication.\r
1007 //\r
1008 mVariableBufferPhysical = mVariableBuffer;\r
1009\r
1010 gRT->GetVariable = RuntimeServiceGetVariable;\r
1011 gRT->GetNextVariableName = RuntimeServiceGetNextVariableName;\r
1012 gRT->SetVariable = RuntimeServiceSetVariable;\r
1013 gRT->QueryVariableInfo = RuntimeServiceQueryVariableInfo;\r
fa0737a8 1014\r
8a2d4996 1015 //\r
1016 // Install the Variable Architectural Protocol on a new handle.\r
1017 //\r
1018 Status = gBS->InstallProtocolInterface (\r
1019 &mHandle,\r
fa0737a8 1020 &gEfiVariableArchProtocolGuid,\r
8a2d4996 1021 EFI_NATIVE_INTERFACE,\r
1022 NULL\r
1023 );\r
1024 ASSERT_EFI_ERROR (Status);\r
aa2868b3
SZ
1025\r
1026 mVariableLock.RequestToLock = VariableLockRequestToLock;\r
1027 Status = gBS->InstallMultipleProtocolInterfaces (\r
1028 &mHandle,\r
1029 &gEdkiiVariableLockProtocolGuid,\r
1030 &mVariableLock,\r
1031 NULL\r
1032 );\r
1033 ASSERT_EFI_ERROR (Status);\r
1034\r
1035 mVarCheck.RegisterSetVariableCheckHandler = VarCheckRegisterSetVariableCheckHandler;\r
1036 mVarCheck.VariablePropertySet = VarCheckVariablePropertySet;\r
1037 mVarCheck.VariablePropertyGet = VarCheckVariablePropertyGet;\r
1038 Status = gBS->InstallMultipleProtocolInterfaces (\r
1039 &mHandle,\r
1040 &gEdkiiVarCheckProtocolGuid,\r
1041 &mVarCheck,\r
1042 NULL\r
1043 );\r
1044 ASSERT_EFI_ERROR (Status);\r
fa0737a8
SZ
1045\r
1046 gBS->CloseEvent (Event);\r
8a2d4996 1047}\r
1048\r
1049\r
1050/**\r
1051 SMM Non-Volatile variable write service is ready notify event handler.\r
1052\r
1053 @param[in] Event Event whose notification function is being invoked.\r
1054 @param[in] Context Pointer to the notification function's context.\r
fa0737a8 1055\r
8a2d4996 1056**/\r
1057VOID\r
1058EFIAPI\r
1059SmmVariableWriteReady (\r
1060 IN EFI_EVENT Event,\r
1061 IN VOID *Context\r
1062 )\r
1063{\r
1064 EFI_STATUS Status;\r
1065 VOID *ProtocolOps;\r
1066\r
1067 //\r
1068 // Check whether the protocol is installed or not.\r
1069 //\r
d00ed85e 1070 Status = gBS->LocateProtocol (&gSmmVariableWriteGuid, NULL, (VOID **) &ProtocolOps);\r
8a2d4996 1071 if (EFI_ERROR (Status)) {\r
1072 return;\r
1073 }\r
fa0737a8 1074\r
dc9bd6ed
ZC
1075 //\r
1076 // Some Secure Boot Policy Var (SecureBoot, etc) updates following other\r
1077 // Secure Boot Policy Variable change. Record their initial value.\r
1078 //\r
1079 RecordSecureBootPolicyVarData();\r
1080\r
8a2d4996 1081 Status = gBS->InstallProtocolInterface (\r
1082 &mHandle,\r
fa0737a8 1083 &gEfiVariableWriteArchProtocolGuid,\r
8a2d4996 1084 EFI_NATIVE_INTERFACE,\r
1085 NULL\r
1086 );\r
fa0737a8
SZ
1087 ASSERT_EFI_ERROR (Status);\r
1088\r
1089 gBS->CloseEvent (Event);\r
8a2d4996 1090}\r
1091\r
1092\r
1093/**\r
1094 Variable Driver main entry point. The Variable driver places the 4 EFI\r
fa0737a8 1095 runtime services in the EFI System Table and installs arch protocols\r
d00ed85e 1096 for variable read and write services being available. It also registers\r
8a2d4996 1097 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
1098\r
fa0737a8 1099 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
8a2d4996 1100 @param[in] SystemTable A pointer to the EFI System Table.\r
fa0737a8 1101\r
8a2d4996 1102 @retval EFI_SUCCESS Variable service successfully initialized.\r
1103\r
1104**/\r
1105EFI_STATUS\r
1106EFIAPI\r
1107VariableSmmRuntimeInitialize (\r
1108 IN EFI_HANDLE ImageHandle,\r
1109 IN EFI_SYSTEM_TABLE *SystemTable\r
1110 )\r
1111{\r
1112 VOID *SmmVariableRegistration;\r
1113 VOID *SmmVariableWriteRegistration;\r
1114 EFI_EVENT OnReadyToBootEvent;\r
1115 EFI_EVENT ExitBootServiceEvent;\r
d3460bcb 1116 EFI_EVENT LegacyBootEvent;\r
6ed1ec59
SZ
1117\r
1118 EfiInitializeLock (&mVariableServicesLock, TPL_NOTIFY);\r
1119\r
8a2d4996 1120 //\r
1121 // Smm variable service is ready\r
1122 //\r
1123 EfiCreateProtocolNotifyEvent (\r
fa0737a8
SZ
1124 &gEfiSmmVariableProtocolGuid,\r
1125 TPL_CALLBACK,\r
1126 SmmVariableReady,\r
1127 NULL,\r
8a2d4996 1128 &SmmVariableRegistration\r
1129 );\r
1130\r
1131 //\r
1132 // Smm Non-Volatile variable write service is ready\r
1133 //\r
1134 EfiCreateProtocolNotifyEvent (\r
fa0737a8
SZ
1135 &gSmmVariableWriteGuid,\r
1136 TPL_CALLBACK,\r
1137 SmmVariableWriteReady,\r
1138 NULL,\r
8a2d4996 1139 &SmmVariableWriteRegistration\r
1140 );\r
1141\r
1142 //\r
1143 // Register the event to reclaim variable for OS usage.\r
1144 //\r
1145 EfiCreateEventReadyToBootEx (\r
fa0737a8
SZ
1146 TPL_NOTIFY,\r
1147 OnReadyToBoot,\r
1148 NULL,\r
8a2d4996 1149 &OnReadyToBootEvent\r
fa0737a8 1150 );\r
8a2d4996 1151\r
1152 //\r
1153 // Register the event to inform SMM variable that it is at runtime.\r
1154 //\r
1155 gBS->CreateEventEx (\r
1156 EVT_NOTIFY_SIGNAL,\r
1157 TPL_NOTIFY,\r
1158 OnExitBootServices,\r
1159 NULL,\r
1160 &gEfiEventExitBootServicesGuid,\r
1161 &ExitBootServiceEvent\r
fa0737a8 1162 );\r
8a2d4996 1163\r
d3460bcb
SZ
1164 //\r
1165 // Register the event to inform SMM variable that it is at runtime for legacy boot.\r
1166 // Reuse OnExitBootServices() here.\r
1167 //\r
1168 EfiCreateEventLegacyBootEx(\r
1169 TPL_NOTIFY,\r
1170 OnExitBootServices,\r
1171 NULL,\r
1172 &LegacyBootEvent\r
1173 );\r
1174\r
8a2d4996 1175 //\r
1176 // Register the event to convert the pointer for runtime.\r
1177 //\r
1178 gBS->CreateEventEx (\r
1179 EVT_NOTIFY_SIGNAL,\r
1180 TPL_NOTIFY,\r
1181 VariableAddressChangeEvent,\r
1182 NULL,\r
1183 &gEfiEventVirtualAddressChangeGuid,\r
1184 &mVirtualAddressChangeEvent\r
1185 );\r
fa0737a8 1186\r
8a2d4996 1187 return EFI_SUCCESS;\r
1188}\r
1189\r