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