]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
MdeModulePkg/Variable: Add RT GetVariable() cache support
[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
aab3b9b9 16Copyright (c) 2010 - 2019, 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 41#include "PrivilegePolymorphic.h"\r
aab3b9b9 42#include "VariableParsing.h"\r
00663d04 43\r
fa0737a8 44EFI_HANDLE mHandle = NULL;\r
8a2d4996 45EFI_SMM_VARIABLE_PROTOCOL *mSmmVariable = NULL;\r
46EFI_EVENT mVirtualAddressChangeEvent = NULL;\r
47EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL;\r
48UINT8 *mVariableBuffer = NULL;\r
49UINT8 *mVariableBufferPhysical = NULL;\r
aab3b9b9
MK
50VARIABLE_INFO_ENTRY *mVariableInfo = NULL;\r
51VARIABLE_STORE_HEADER *mVariableRuntimeHobCacheBuffer = NULL;\r
52VARIABLE_STORE_HEADER *mVariableRuntimeNvCacheBuffer = NULL;\r
53VARIABLE_STORE_HEADER *mVariableRuntimeVolatileCacheBuffer = NULL;\r
8a2d4996 54UINTN mVariableBufferSize;\r
aab3b9b9
MK
55UINTN mVariableRuntimeHobCacheBufferSize;\r
56UINTN mVariableRuntimeNvCacheBufferSize;\r
57UINTN mVariableRuntimeVolatileCacheBufferSize;\r
5e5bb2a9 58UINTN mVariableBufferPayloadSize;\r
aab3b9b9
MK
59BOOLEAN mVariableRuntimeCachePendingUpdate;\r
60BOOLEAN mVariableRuntimeCacheReadLock;\r
61BOOLEAN mVariableAuthFormat;\r
62BOOLEAN mHobFlushComplete;\r
6ed1ec59 63EFI_LOCK mVariableServicesLock;\r
ff843847 64EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock;\r
efb01a10 65EDKII_VAR_CHECK_PROTOCOL mVarCheck;\r
8a2d4996 66\r
dc9bd6ed
ZC
67/**\r
68 Some Secure Boot Policy Variable may update following other variable changes(SecureBoot follows PK change, etc).\r
69 Record their initial State when variable write service is ready.\r
70\r
71**/\r
72VOID\r
73EFIAPI\r
74RecordSecureBootPolicyVarData(\r
75 VOID\r
76 );\r
77\r
6ed1ec59
SZ
78/**\r
79 Acquires lock only at boot time. Simply returns at runtime.\r
80\r
81 This is a temperary function that will be removed when\r
82 EfiAcquireLock() in UefiLib can handle the call in UEFI\r
83 Runtimer driver in RT phase.\r
84 It calls EfiAcquireLock() at boot time, and simply returns\r
85 at runtime.\r
86\r
87 @param Lock A pointer to the lock to acquire.\r
88\r
89**/\r
90VOID\r
91AcquireLockOnlyAtBootTime (\r
92 IN EFI_LOCK *Lock\r
93 )\r
94{\r
95 if (!EfiAtRuntime ()) {\r
96 EfiAcquireLock (Lock);\r
97 }\r
98}\r
99\r
100/**\r
101 Releases lock only at boot time. Simply returns at runtime.\r
102\r
103 This is a temperary function which will be removed when\r
104 EfiReleaseLock() in UefiLib can handle the call in UEFI\r
105 Runtimer driver in RT phase.\r
106 It calls EfiReleaseLock() at boot time and simply returns\r
107 at runtime.\r
108\r
109 @param Lock A pointer to the lock to release.\r
110\r
111**/\r
112VOID\r
113ReleaseLockOnlyAtBootTime (\r
114 IN EFI_LOCK *Lock\r
115 )\r
116{\r
117 if (!EfiAtRuntime ()) {\r
118 EfiReleaseLock (Lock);\r
119 }\r
120}\r
8a2d4996 121\r
aab3b9b9
MK
122/**\r
123 Return TRUE if ExitBootServices () has been called.\r
124\r
125 @retval TRUE If ExitBootServices () has been called. FALSE if ExitBootServices () has not been called.\r
126**/\r
127BOOLEAN\r
128AtRuntime (\r
129 VOID\r
130 )\r
131{\r
132 return EfiAtRuntime ();\r
133}\r
134\r
135/**\r
136 Initialize the variable cache buffer as an empty variable store.\r
137\r
138 @param[out] VariableCacheBuffer A pointer to pointer of a cache variable store.\r
139 @param[in,out] TotalVariableCacheSize On input, the minimum size needed for the UEFI variable store cache\r
140 buffer that is allocated. On output, the actual size of the buffer allocated.\r
141 If TotalVariableCacheSize is zero, a buffer will not be allocated and the\r
142 function will return with EFI_SUCCESS.\r
143\r
144 @retval EFI_SUCCESS The variable cache was allocated and initialized successfully.\r
145 @retval EFI_INVALID_PARAMETER A given pointer is NULL or an invalid variable store size was specified.\r
146 @retval EFI_OUT_OF_RESOURCES Insufficient resources are available to allocate the variable store cache buffer.\r
147\r
148**/\r
149EFI_STATUS\r
150InitVariableCache (\r
151 OUT VARIABLE_STORE_HEADER **VariableCacheBuffer,\r
152 IN OUT UINTN *TotalVariableCacheSize\r
153 )\r
154{\r
155 VARIABLE_STORE_HEADER *VariableCacheStorePtr;\r
156\r
157 if (TotalVariableCacheSize == NULL) {\r
158 return EFI_INVALID_PARAMETER;\r
159 }\r
160 if (*TotalVariableCacheSize == 0) {\r
161 return EFI_SUCCESS;\r
162 }\r
163 if (VariableCacheBuffer == NULL || *TotalVariableCacheSize < sizeof (VARIABLE_STORE_HEADER)) {\r
164 return EFI_INVALID_PARAMETER;\r
165 }\r
166 *TotalVariableCacheSize = ALIGN_VALUE (*TotalVariableCacheSize, sizeof (UINT32));\r
167\r
168 //\r
169 // Allocate NV Storage Cache and initialize it to all 1's (like an erased FV)\r
170 //\r
171 *VariableCacheBuffer = (VARIABLE_STORE_HEADER *) AllocateRuntimePages (\r
172 EFI_SIZE_TO_PAGES (*TotalVariableCacheSize)\r
173 );\r
174 if (*VariableCacheBuffer == NULL) {\r
175 return EFI_OUT_OF_RESOURCES;\r
176 }\r
177 VariableCacheStorePtr = *VariableCacheBuffer;\r
178 SetMem32 ((VOID *) VariableCacheStorePtr, *TotalVariableCacheSize, (UINT32) 0xFFFFFFFF);\r
179\r
180 ZeroMem ((VOID *) VariableCacheStorePtr, sizeof (VARIABLE_STORE_HEADER));\r
181 VariableCacheStorePtr->Size = (UINT32) *TotalVariableCacheSize;\r
182 VariableCacheStorePtr->Format = VARIABLE_STORE_FORMATTED;\r
183 VariableCacheStorePtr->State = VARIABLE_STORE_HEALTHY;\r
184\r
185 return EFI_SUCCESS;\r
186}\r
187\r
8a2d4996 188/**\r
189 Initialize the communicate buffer using DataSize and Function.\r
190\r
191 The communicate size is: SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +\r
192 DataSize.\r
193\r
18a7dbbc
SZ
194 Caution: This function may receive untrusted input.\r
195 The data size external input, so this function will validate it carefully to avoid buffer overflow.\r
196\r
8a2d4996 197 @param[out] DataPtr Points to the data in the communicate buffer.\r
198 @param[in] DataSize The data size to send to SMM.\r
199 @param[in] Function The function number to initialize the communicate header.\r
fa0737a8 200\r
8a2d4996 201 @retval EFI_INVALID_PARAMETER The data size is too big.\r
202 @retval EFI_SUCCESS Find the specified variable.\r
203\r
204**/\r
205EFI_STATUS\r
206InitCommunicateBuffer (\r
207 OUT VOID **DataPtr OPTIONAL,\r
208 IN UINTN DataSize,\r
209 IN UINTN Function\r
210 )\r
211{\r
fa0737a8
SZ
212 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader;\r
213 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;\r
214\r
8a2d4996 215\r
8a2d4996 216 if (DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE > mVariableBufferSize) {\r
217 return EFI_INVALID_PARAMETER;\r
218 }\r
219\r
220 SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) mVariableBuffer;\r
221 CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);\r
222 SmmCommunicateHeader->MessageLength = DataSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
fa0737a8 223\r
8a2d4996 224 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader->Data;\r
225 SmmVariableFunctionHeader->Function = Function;\r
226 if (DataPtr != NULL) {\r
227 *DataPtr = SmmVariableFunctionHeader->Data;\r
228 }\r
229\r
230 return EFI_SUCCESS;\r
231}\r
232\r
233\r
234/**\r
235 Send the data in communicate buffer to SMM.\r
236\r
237 @param[in] DataSize This size of the function header and the data.\r
238\r
32732a33 239 @retval EFI_SUCCESS Success is returned from the functin in SMM.\r
fa0737a8
SZ
240 @retval Others Failure is returned from the function in SMM.\r
241\r
8a2d4996 242**/\r
243EFI_STATUS\r
244SendCommunicateBuffer (\r
245 IN UINTN DataSize\r
246 )\r
247{\r
248 EFI_STATUS Status;\r
249 UINTN CommSize;\r
fa0737a8 250 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader;\r
8a2d4996 251 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;\r
fa0737a8 252\r
8a2d4996 253 CommSize = DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
254 Status = mSmmCommunication->Communicate (mSmmCommunication, mVariableBufferPhysical, &CommSize);\r
255 ASSERT_EFI_ERROR (Status);\r
256\r
257 SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) mVariableBuffer;\r
258 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)SmmCommunicateHeader->Data;\r
259 return SmmVariableFunctionHeader->ReturnStatus;\r
260}\r
261\r
ff843847
RN
262/**\r
263 Mark a variable that will become read-only after leaving the DXE phase of execution.\r
264\r
265 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.\r
266 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.\r
267 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.\r
268\r
269 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked\r
270 as pending to be read-only.\r
271 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.\r
272 Or VariableName is an empty string.\r
273 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
274 already been signaled.\r
275 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.\r
276**/\r
277EFI_STATUS\r
278EFIAPI\r
279VariableLockRequestToLock (\r
280 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This,\r
281 IN CHAR16 *VariableName,\r
282 IN EFI_GUID *VendorGuid\r
283 )\r
284{\r
285 EFI_STATUS Status;\r
51547bb8 286 UINTN VariableNameSize;\r
ff843847
RN
287 UINTN PayloadSize;\r
288 SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *VariableToLock;\r
289\r
290 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
291 return EFI_INVALID_PARAMETER;\r
292 }\r
293\r
51547bb8 294 VariableNameSize = StrSize (VariableName);\r
4e1005ec 295 VariableToLock = NULL;\r
51547bb8
RN
296\r
297 //\r
298 // If VariableName exceeds SMM payload limit. Return failure\r
299 //\r
300 if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE, Name)) {\r
301 return EFI_INVALID_PARAMETER;\r
302 }\r
303\r
ff843847
RN
304 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
305\r
306 //\r
307 // Init the communicate buffer. The buffer data size is:\r
308 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
309 //\r
51547bb8 310 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE, Name) + VariableNameSize;\r
ff843847
RN
311 Status = InitCommunicateBuffer ((VOID **) &VariableToLock, PayloadSize, SMM_VARIABLE_FUNCTION_LOCK_VARIABLE);\r
312 if (EFI_ERROR (Status)) {\r
313 goto Done;\r
314 }\r
315 ASSERT (VariableToLock != NULL);\r
316\r
317 CopyGuid (&VariableToLock->Guid, VendorGuid);\r
51547bb8 318 VariableToLock->NameSize = VariableNameSize;\r
ff843847
RN
319 CopyMem (VariableToLock->Name, VariableName, VariableToLock->NameSize);\r
320\r
321 //\r
322 // Send data to SMM.\r
323 //\r
324 Status = SendCommunicateBuffer (PayloadSize);\r
325\r
326Done:\r
327 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
328 return Status;\r
329}\r
8a2d4996 330\r
efb01a10
SZ
331/**\r
332 Register SetVariable check handler.\r
333\r
334 @param[in] Handler Pointer to check handler.\r
335\r
336 @retval EFI_SUCCESS The SetVariable check handler was registered successfully.\r
337 @retval EFI_INVALID_PARAMETER Handler is NULL.\r
338 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
339 already been signaled.\r
340 @retval EFI_OUT_OF_RESOURCES There is not enough resource for the SetVariable check handler register request.\r
341 @retval EFI_UNSUPPORTED This interface is not implemented.\r
342 For example, it is unsupported in VarCheck protocol if both VarCheck and SmmVarCheck protocols are present.\r
343\r
344**/\r
345EFI_STATUS\r
346EFIAPI\r
347VarCheckRegisterSetVariableCheckHandler (\r
348 IN VAR_CHECK_SET_VARIABLE_CHECK_HANDLER Handler\r
349 )\r
350{\r
351 return EFI_UNSUPPORTED;\r
352}\r
353\r
354/**\r
355 Variable property set.\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[in] VariableProperty Pointer to the input variable property.\r
360\r
361 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was set successfully.\r
362 @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string,\r
363 or the fields of VariableProperty are not valid.\r
364 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
365 already been signaled.\r
366 @retval EFI_OUT_OF_RESOURCES There is not enough resource for the variable property set request.\r
367\r
368**/\r
369EFI_STATUS\r
370EFIAPI\r
371VarCheckVariablePropertySet (\r
372 IN CHAR16 *Name,\r
373 IN EFI_GUID *Guid,\r
374 IN VAR_CHECK_VARIABLE_PROPERTY *VariableProperty\r
375 )\r
376{\r
377 EFI_STATUS Status;\r
378 UINTN VariableNameSize;\r
379 UINTN PayloadSize;\r
380 SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProperty;\r
381\r
382 if (Name == NULL || Name[0] == 0 || Guid == NULL) {\r
383 return EFI_INVALID_PARAMETER;\r
384 }\r
385\r
386 if (VariableProperty == NULL) {\r
387 return EFI_INVALID_PARAMETER;\r
388 }\r
389\r
390 if (VariableProperty->Revision != VAR_CHECK_VARIABLE_PROPERTY_REVISION) {\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_SET);\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 CopyMem (&CommVariableProperty->VariableProperty, VariableProperty, sizeof (*VariableProperty));\r
419 CommVariableProperty->NameSize = VariableNameSize;\r
420 CopyMem (CommVariableProperty->Name, Name, CommVariableProperty->NameSize);\r
421\r
422 //\r
423 // Send data to SMM.\r
424 //\r
425 Status = SendCommunicateBuffer (PayloadSize);\r
426\r
427Done:\r
428 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
429 return Status;\r
430}\r
431\r
432/**\r
433 Variable property get.\r
434\r
435 @param[in] Name Pointer to the variable name.\r
436 @param[in] Guid Pointer to the vendor GUID.\r
437 @param[out] VariableProperty Pointer to the output variable property.\r
438\r
439 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was got successfully.\r
440 @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string.\r
441 @retval EFI_NOT_FOUND The property of variable specified by the Name and Guid was not found.\r
442\r
443**/\r
444EFI_STATUS\r
445EFIAPI\r
446VarCheckVariablePropertyGet (\r
447 IN CHAR16 *Name,\r
448 IN EFI_GUID *Guid,\r
449 OUT VAR_CHECK_VARIABLE_PROPERTY *VariableProperty\r
450 )\r
451{\r
452 EFI_STATUS Status;\r
453 UINTN VariableNameSize;\r
454 UINTN PayloadSize;\r
455 SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProperty;\r
456\r
457 if (Name == NULL || Name[0] == 0 || Guid == NULL) {\r
458 return EFI_INVALID_PARAMETER;\r
459 }\r
460\r
461 if (VariableProperty == NULL) {\r
462 return EFI_INVALID_PARAMETER;\r
463 }\r
464\r
465 VariableNameSize = StrSize (Name);\r
466 CommVariableProperty = NULL;\r
467\r
468 //\r
469 // If VariableName exceeds SMM payload limit. Return failure\r
470 //\r
471 if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) {\r
472 return EFI_INVALID_PARAMETER;\r
473 }\r
474\r
475 AcquireLockOnlyAtBootTime (&mVariableServicesLock);\r
476\r
477 //\r
478 // Init the communicate buffer. The buffer data size is:\r
479 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
480 //\r
481 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) + VariableNameSize;\r
482 Status = InitCommunicateBuffer ((VOID **) &CommVariableProperty, PayloadSize, SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET);\r
483 if (EFI_ERROR (Status)) {\r
484 goto Done;\r
485 }\r
486 ASSERT (CommVariableProperty != NULL);\r
487\r
488 CopyGuid (&CommVariableProperty->Guid, Guid);\r
489 CommVariableProperty->NameSize = VariableNameSize;\r
490 CopyMem (CommVariableProperty->Name, Name, CommVariableProperty->NameSize);\r
491\r
492 //\r
493 // Send data to SMM.\r
494 //\r
495 Status = SendCommunicateBuffer (PayloadSize);\r
496 if (Status == EFI_SUCCESS) {\r
497 CopyMem (VariableProperty, &CommVariableProperty->VariableProperty, sizeof (*VariableProperty));\r
498 }\r
499\r
500Done:\r
501 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
502 return Status;\r
503}\r
504\r
8a2d4996 505/**\r
aab3b9b9
MK
506 Signals SMM to synchronize any pending variable updates with the runtime cache(s).\r
507\r
508**/\r
509VOID\r
510SyncRuntimeCache (\r
511 VOID\r
512 )\r
513{\r
514 //\r
515 // Init the communicate buffer. The buffer data size is:\r
516 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.\r
517 //\r
518 InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE);\r
519\r
520 //\r
521 // Send data to SMM.\r
522 //\r
523 SendCommunicateBuffer (0);\r
524}\r
525\r
526/**\r
527 Check whether a SMI must be triggered to retrieve pending cache updates.\r
528\r
529 If the variable HOB was finished being flushed since the last check for a runtime cache update, this function\r
530 will prevent the HOB cache from being used for future runtime cache hits.\r
531\r
532**/\r
533VOID\r
534CheckForRuntimeCacheSync (\r
535 VOID\r
536 )\r
537{\r
538 if (mVariableRuntimeCachePendingUpdate) {\r
539 SyncRuntimeCache ();\r
540 }\r
541 ASSERT (!mVariableRuntimeCachePendingUpdate);\r
542\r
543 //\r
544 // The HOB variable data may have finished being flushed in the runtime cache sync update\r
545 //\r
546 if (mHobFlushComplete && mVariableRuntimeHobCacheBuffer != NULL) {\r
547 if (!EfiAtRuntime ()) {\r
548 FreePages (mVariableRuntimeHobCacheBuffer, EFI_SIZE_TO_PAGES (mVariableRuntimeHobCacheBufferSize));\r
549 }\r
550 mVariableRuntimeHobCacheBuffer = NULL;\r
551 }\r
552}\r
553\r
554/**\r
555 Finds the given variable in a runtime cache variable store.\r
8a2d4996 556\r
18a7dbbc
SZ
557 Caution: This function may receive untrusted input.\r
558 The data size is external input, so this function will validate it carefully to avoid buffer overflow.\r
559\r
8a2d4996 560 @param[in] VariableName Name of Variable to be found.\r
561 @param[in] VendorGuid Variable vendor GUID.\r
562 @param[out] Attributes Attribute value of the variable found.\r
563 @param[in, out] DataSize Size of Data found. If size is less than the\r
564 data, this value contains the required size.\r
565 @param[out] Data Data pointer.\r
fa0737a8 566\r
aab3b9b9 567 @retval EFI_SUCCESS Found the specified variable.\r
8a2d4996 568 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
aab3b9b9 569 @retval EFI_NOT_FOUND The specified variable could not be found.\r
8a2d4996 570\r
571**/\r
572EFI_STATUS\r
aab3b9b9 573FindVariableInRuntimeCache (\r
8a2d4996 574 IN CHAR16 *VariableName,\r
575 IN EFI_GUID *VendorGuid,\r
576 OUT UINT32 *Attributes OPTIONAL,\r
577 IN OUT UINTN *DataSize,\r
aab3b9b9
MK
578 OUT VOID *Data OPTIONAL\r
579 )\r
580{\r
581 EFI_STATUS Status;\r
582 UINTN TempDataSize;\r
583 VARIABLE_POINTER_TRACK RtPtrTrack;\r
584 VARIABLE_STORE_TYPE StoreType;\r
585 VARIABLE_STORE_HEADER *VariableStoreList[VariableStoreTypeMax];\r
586\r
587 Status = EFI_NOT_FOUND;\r
588\r
589 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
590 return EFI_INVALID_PARAMETER;\r
591 }\r
592\r
593 //\r
594 // The UEFI specification restricts Runtime Services callers from invoking the same or certain other Runtime Service\r
595 // functions prior to completion and return from a previous Runtime Service call. These restrictions prevent\r
596 // a GetVariable () or GetNextVariable () call from being issued until a prior call has returned. The runtime\r
597 // cache read lock should always be free when entering this function.\r
598 //\r
599 ASSERT (!mVariableRuntimeCacheReadLock);\r
600\r
601 mVariableRuntimeCacheReadLock = TRUE;\r
602 CheckForRuntimeCacheSync ();\r
603\r
604 if (!mVariableRuntimeCachePendingUpdate) {\r
605 //\r
606 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
607 // The index and attributes mapping must be kept in this order as FindVariable\r
608 // makes use of this mapping to implement search algorithm.\r
609 //\r
610 VariableStoreList[VariableStoreTypeVolatile] = mVariableRuntimeVolatileCacheBuffer;\r
611 VariableStoreList[VariableStoreTypeHob] = mVariableRuntimeHobCacheBuffer;\r
612 VariableStoreList[VariableStoreTypeNv] = mVariableRuntimeNvCacheBuffer;\r
613\r
614 for (StoreType = (VARIABLE_STORE_TYPE) 0; StoreType < VariableStoreTypeMax; StoreType++) {\r
615 if (VariableStoreList[StoreType] == NULL) {\r
616 continue;\r
617 }\r
618\r
619 RtPtrTrack.StartPtr = GetStartPointer (VariableStoreList[StoreType]);\r
620 RtPtrTrack.EndPtr = GetEndPointer (VariableStoreList[StoreType]);\r
621 RtPtrTrack.Volatile = (BOOLEAN) (StoreType == VariableStoreTypeVolatile);\r
622\r
623 Status = FindVariableEx (VariableName, VendorGuid, FALSE, &RtPtrTrack, mVariableAuthFormat);\r
624 if (!EFI_ERROR (Status)) {\r
625 break;\r
626 }\r
627 }\r
628\r
629 if (!EFI_ERROR (Status)) {\r
630 //\r
631 // Get data size\r
632 //\r
633 TempDataSize = DataSizeOfVariable (RtPtrTrack.CurrPtr, mVariableAuthFormat);\r
634 ASSERT (TempDataSize != 0);\r
635\r
636 if (*DataSize >= TempDataSize) {\r
637 if (Data == NULL) {\r
638 Status = EFI_INVALID_PARAMETER;\r
639 goto Done;\r
640 }\r
641\r
642 CopyMem (Data, GetVariableDataPtr (RtPtrTrack.CurrPtr, mVariableAuthFormat), TempDataSize);\r
643 if (Attributes != NULL) {\r
644 *Attributes = RtPtrTrack.CurrPtr->Attributes;\r
645 }\r
646\r
647 *DataSize = TempDataSize;\r
648\r
649 UpdateVariableInfo (VariableName, VendorGuid, RtPtrTrack.Volatile, TRUE, FALSE, FALSE, TRUE, &mVariableInfo);\r
650\r
651 Status = EFI_SUCCESS;\r
652 goto Done;\r
653 } else {\r
654 *DataSize = TempDataSize;\r
655 Status = EFI_BUFFER_TOO_SMALL;\r
656 goto Done;\r
657 }\r
658 }\r
659 }\r
660\r
661Done:\r
662 mVariableRuntimeCacheReadLock = FALSE;\r
663\r
664 return Status;\r
665}\r
666\r
667/**\r
668 Finds the given variable in a variable store in SMM.\r
669\r
670 Caution: This function may receive untrusted input.\r
671 The data size is external input, so this function will validate it carefully to avoid buffer overflow.\r
672\r
673 @param[in] VariableName Name of Variable to be found.\r
674 @param[in] VendorGuid Variable vendor GUID.\r
675 @param[out] Attributes Attribute value of the variable found.\r
676 @param[in, out] DataSize Size of Data found. If size is less than the\r
677 data, this value contains the required size.\r
678 @param[out] Data Data pointer.\r
679\r
680 @retval EFI_SUCCESS Found the specified variable.\r
681 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
682 @retval EFI_NOT_FOUND The specified variable could not be found.\r
683\r
684**/\r
685EFI_STATUS\r
686FindVariableInSmm (\r
687 IN CHAR16 *VariableName,\r
688 IN EFI_GUID *VendorGuid,\r
689 OUT UINT32 *Attributes OPTIONAL,\r
690 IN OUT UINTN *DataSize,\r
691 OUT VOID *Data OPTIONAL\r
8a2d4996 692 )\r
693{\r
694 EFI_STATUS Status;\r
695 UINTN PayloadSize;\r
696 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;\r
3a146f2a 697 UINTN TempDataSize;\r
9d00d20e 698 UINTN VariableNameSize;\r
8a2d4996 699\r
700 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
701 return EFI_INVALID_PARAMETER;\r
702 }\r
703\r
3a146f2a 704 TempDataSize = *DataSize;\r
9d00d20e 705 VariableNameSize = StrSize (VariableName);\r
4e1005ec 706 SmmVariableHeader = NULL;\r
3a146f2a 707\r
708 //\r
709 // If VariableName exceeds SMM payload limit. Return failure\r
710 //\r
5e5bb2a9 711 if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {\r
3588bb35
SZ
712 return EFI_INVALID_PARAMETER;\r
713 }\r
714\r
8a2d4996 715 //\r
716 // Init the communicate buffer. The buffer data size is:\r
6ed1ec59 717 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
8a2d4996 718 //\r
5e5bb2a9 719 if (TempDataSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize) {\r
3a146f2a 720 //\r
721 // If output data buffer exceed SMM payload limit. Trim output buffer to SMM payload size\r
722 //\r
5e5bb2a9 723 TempDataSize = mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize;\r
3a146f2a 724 }\r
9d00d20e 725 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + VariableNameSize + TempDataSize;\r
3a146f2a 726\r
aab3b9b9 727 Status = InitCommunicateBuffer ((VOID **) &SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);\r
8a2d4996 728 if (EFI_ERROR (Status)) {\r
6ed1ec59 729 goto Done;\r
8a2d4996 730 }\r
731 ASSERT (SmmVariableHeader != NULL);\r
732\r
733 CopyGuid (&SmmVariableHeader->Guid, VendorGuid);\r
3a146f2a 734 SmmVariableHeader->DataSize = TempDataSize;\r
9d00d20e 735 SmmVariableHeader->NameSize = VariableNameSize;\r
8a2d4996 736 if (Attributes == NULL) {\r
737 SmmVariableHeader->Attributes = 0;\r
738 } else {\r
739 SmmVariableHeader->Attributes = *Attributes;\r
740 }\r
741 CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);\r
742\r
743 //\r
744 // Send data to SMM.\r
745 //\r
746 Status = SendCommunicateBuffer (PayloadSize);\r
747\r
748 //\r
749 // Get data from SMM.\r
750 //\r
3a146f2a 751 if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {\r
752 //\r
753 // SMM CommBuffer DataSize can be a trimed value\r
754 // Only update DataSize when needed\r
755 //\r
756 *DataSize = SmmVariableHeader->DataSize;\r
757 }\r
8a2d4996 758 if (Attributes != NULL) {\r
759 *Attributes = SmmVariableHeader->Attributes;\r
760 }\r
761\r
762 if (EFI_ERROR (Status)) {\r
6ed1ec59 763 goto Done;\r
8a2d4996 764 }\r
765\r
82e47eb2
SZ
766 if (Data != NULL) {\r
767 CopyMem (Data, (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize, SmmVariableHeader->DataSize);\r
768 } else {\r
769 Status = EFI_INVALID_PARAMETER;\r
770 }\r
8a2d4996 771\r
6ed1ec59 772Done:\r
8a2d4996 773 return Status;\r
774}\r
775\r
aab3b9b9
MK
776/**\r
777 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
778\r
779 Caution: This function may receive untrusted input.\r
780 The data size is external input, so this function will validate it carefully to avoid buffer overflow.\r
781\r
782 @param[in] VariableName Name of Variable to be found.\r
783 @param[in] VendorGuid Variable vendor GUID.\r
784 @param[out] Attributes Attribute value of the variable found.\r
785 @param[in, out] DataSize Size of Data found. If size is less than the\r
786 data, this value contains the required size.\r
787 @param[out] Data Data pointer.\r
788\r
789 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
790 @retval EFI_SUCCESS Find the specified variable.\r
791 @retval EFI_NOT_FOUND Not found.\r
792 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
793\r
794**/\r
795EFI_STATUS\r
796EFIAPI\r
797RuntimeServiceGetVariable (\r
798 IN CHAR16 *VariableName,\r
799 IN EFI_GUID *VendorGuid,\r
800 OUT UINT32 *Attributes OPTIONAL,\r
801 IN OUT UINTN *DataSize,\r
802 OUT VOID *Data\r
803 )\r
804{\r
805 EFI_STATUS Status;\r
806\r
807 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
808 return EFI_INVALID_PARAMETER;\r
809 }\r
810 if (VariableName[0] == 0) {\r
811 return EFI_NOT_FOUND;\r
812 }\r
813\r
814 AcquireLockOnlyAtBootTime (&mVariableServicesLock);\r
815 if (FeaturePcdGet (PcdEnableVariableRuntimeCache)) {\r
816 Status = FindVariableInRuntimeCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r
817 } else {\r
818 Status = FindVariableInSmm (VariableName, VendorGuid, Attributes, DataSize, Data);\r
819 }\r
820 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
821\r
822 return Status;\r
823}\r
8a2d4996 824\r
825/**\r
826 This code Finds the Next available variable.\r
827\r
828 @param[in, out] VariableNameSize Size of the variable name.\r
829 @param[in, out] VariableName Pointer to variable name.\r
830 @param[in, out] VendorGuid Variable Vendor Guid.\r
831\r
832 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
833 @retval EFI_SUCCESS Find the specified variable.\r
834 @retval EFI_NOT_FOUND Not found.\r
835 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
836\r
837**/\r
838EFI_STATUS\r
839EFIAPI\r
840RuntimeServiceGetNextVariableName (\r
841 IN OUT UINTN *VariableNameSize,\r
842 IN OUT CHAR16 *VariableName,\r
843 IN OUT EFI_GUID *VendorGuid\r
844 )\r
845{\r
846 EFI_STATUS Status;\r
847 UINTN PayloadSize;\r
848 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *SmmGetNextVariableName;\r
9d00d20e
SZ
849 UINTN OutVariableNameSize;\r
850 UINTN InVariableNameSize;\r
8a2d4996 851\r
852 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
853 return EFI_INVALID_PARAMETER;\r
854 }\r
6ed1ec59 855\r
9d00d20e
SZ
856 OutVariableNameSize = *VariableNameSize;\r
857 InVariableNameSize = StrSize (VariableName);\r
4e1005ec 858 SmmGetNextVariableName = NULL;\r
0c55190f 859\r
860 //\r
861 // If input string exceeds SMM payload limit. Return failure\r
862 //\r
5e5bb2a9 863 if (InVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
3588bb35
SZ
864 return EFI_INVALID_PARAMETER;\r
865 }\r
866\r
6ed1ec59
SZ
867 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
868\r
8a2d4996 869 //\r
870 // Init the communicate buffer. The buffer data size is:\r
871 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
872 //\r
5e5bb2a9 873 if (OutVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
0c55190f 874 //\r
875 // If output buffer exceed SMM payload limit. Trim output buffer to SMM payload size\r
876 //\r
5e5bb2a9 877 OutVariableNameSize = mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);\r
0c55190f 878 }\r
879 //\r
880 // Payload should be Guid + NameSize + MAX of Input & Output buffer\r
881 //\r
9d00d20e 882 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + MAX (OutVariableNameSize, InVariableNameSize);\r
0c55190f 883\r
5c7fa429 884 Status = InitCommunicateBuffer ((VOID **)&SmmGetNextVariableName, PayloadSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME);\r
8a2d4996 885 if (EFI_ERROR (Status)) {\r
6ed1ec59 886 goto Done;\r
8a2d4996 887 }\r
888 ASSERT (SmmGetNextVariableName != NULL);\r
889\r
0c55190f 890 //\r
891 // SMM comm buffer->NameSize is buffer size for return string\r
892 //\r
9d00d20e 893 SmmGetNextVariableName->NameSize = OutVariableNameSize;\r
0c55190f 894\r
8a2d4996 895 CopyGuid (&SmmGetNextVariableName->Guid, VendorGuid);\r
0c55190f 896 //\r
897 // Copy whole string\r
898 //\r
9d00d20e
SZ
899 CopyMem (SmmGetNextVariableName->Name, VariableName, InVariableNameSize);\r
900 if (OutVariableNameSize > InVariableNameSize) {\r
901 ZeroMem ((UINT8 *) SmmGetNextVariableName->Name + InVariableNameSize, OutVariableNameSize - InVariableNameSize);\r
902 }\r
8a2d4996 903\r
904 //\r
905 // Send data to SMM\r
906 //\r
907 Status = SendCommunicateBuffer (PayloadSize);\r
908\r
909 //\r
910 // Get data from SMM.\r
911 //\r
9d00d20e
SZ
912 if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {\r
913 //\r
914 // SMM CommBuffer NameSize can be a trimed value\r
915 // Only update VariableNameSize when needed\r
916 //\r
917 *VariableNameSize = SmmGetNextVariableName->NameSize;\r
918 }\r
8a2d4996 919 if (EFI_ERROR (Status)) {\r
6ed1ec59 920 goto Done;\r
8a2d4996 921 }\r
fa0737a8 922\r
8a2d4996 923 CopyGuid (VendorGuid, &SmmGetNextVariableName->Guid);\r
fa0737a8 924 CopyMem (VariableName, SmmGetNextVariableName->Name, SmmGetNextVariableName->NameSize);\r
8a2d4996 925\r
6ed1ec59
SZ
926Done:\r
927 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
8a2d4996 928 return Status;\r
929}\r
930\r
931/**\r
932 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
933\r
18a7dbbc
SZ
934 Caution: This function may receive untrusted input.\r
935 The data size and data are external input, so this function will validate it carefully to avoid buffer overflow.\r
936\r
8a2d4996 937 @param[in] VariableName Name of Variable to be found.\r
938 @param[in] VendorGuid Variable vendor GUID.\r
939 @param[in] Attributes Attribute value of the variable found\r
940 @param[in] DataSize Size of Data found. If size is less than the\r
941 data, this value contains the required size.\r
942 @param[in] Data Data pointer.\r
943\r
944 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
945 @retval EFI_SUCCESS Set successfully.\r
946 @retval EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
947 @retval EFI_NOT_FOUND Not found.\r
948 @retval EFI_WRITE_PROTECTED Variable is read-only.\r
949\r
950**/\r
951EFI_STATUS\r
952EFIAPI\r
953RuntimeServiceSetVariable (\r
954 IN CHAR16 *VariableName,\r
955 IN EFI_GUID *VendorGuid,\r
956 IN UINT32 Attributes,\r
957 IN UINTN DataSize,\r
958 IN VOID *Data\r
959 )\r
960{\r
961 EFI_STATUS Status;\r
fa0737a8 962 UINTN PayloadSize;\r
8a2d4996 963 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;\r
9d00d20e 964 UINTN VariableNameSize;\r
fa0737a8 965\r
8a2d4996 966 //\r
967 // Check input parameters.\r
968 //\r
969 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
970 return EFI_INVALID_PARAMETER;\r
fa0737a8 971 }\r
8a2d4996 972\r
973 if (DataSize != 0 && Data == NULL) {\r
974 return EFI_INVALID_PARAMETER;\r
975 }\r
6ed1ec59 976\r
9d00d20e 977 VariableNameSize = StrSize (VariableName);\r
4e1005ec 978 SmmVariableHeader = NULL;\r
3588bb35 979\r
5e5bb2a9
SZ
980 //\r
981 // If VariableName or DataSize exceeds SMM payload limit. Return failure\r
982 //\r
983 if ((VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||\r
984 (DataSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize)){\r
56251c66 985 return EFI_INVALID_PARAMETER;\r
986 }\r
987\r
6ed1ec59 988 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
fa0737a8 989\r
8a2d4996 990 //\r
991 // Init the communicate buffer. The buffer data size is:\r
992 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
993 //\r
9d00d20e 994 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + VariableNameSize + DataSize;\r
5c7fa429 995 Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_SET_VARIABLE);\r
8a2d4996 996 if (EFI_ERROR (Status)) {\r
6ed1ec59 997 goto Done;\r
8a2d4996 998 }\r
999 ASSERT (SmmVariableHeader != NULL);\r
1000\r
1001 CopyGuid ((EFI_GUID *) &SmmVariableHeader->Guid, VendorGuid);\r
1002 SmmVariableHeader->DataSize = DataSize;\r
9d00d20e 1003 SmmVariableHeader->NameSize = VariableNameSize;\r
8a2d4996 1004 SmmVariableHeader->Attributes = Attributes;\r
1005 CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);\r
1006 CopyMem ((UINT8 *) SmmVariableHeader->Name + SmmVariableHeader->NameSize, Data, DataSize);\r
1007\r
1008 //\r
1009 // Send data to SMM.\r
1010 //\r
1011 Status = SendCommunicateBuffer (PayloadSize);\r
6ed1ec59
SZ
1012\r
1013Done:\r
1014 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
fa0737a8
SZ
1015\r
1016 if (!EfiAtRuntime ()) {\r
1017 if (!EFI_ERROR (Status)) {\r
1018 SecureBootHook (\r
1019 VariableName,\r
1020 VendorGuid\r
1021 );\r
1022 }\r
1023 }\r
8a2d4996 1024 return Status;\r
1025}\r
1026\r
1027\r
1028/**\r
1029 This code returns information about the EFI variables.\r
1030\r
1031 @param[in] Attributes Attributes bitmask to specify the type of variables\r
1032 on which to return information.\r
1033 @param[out] MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
1034 for the EFI variables associated with the attributes specified.\r
1035 @param[out] RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
1036 for EFI variables associated with the attributes specified.\r
1037 @param[out] MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
1038 associated with the attributes specified.\r
1039\r
1040 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
1041 @retval EFI_SUCCESS Query successfully.\r
1042 @retval EFI_UNSUPPORTED The attribute is not supported on this platform.\r
1043\r
1044**/\r
1045EFI_STATUS\r
1046EFIAPI\r
1047RuntimeServiceQueryVariableInfo (\r
1048 IN UINT32 Attributes,\r
1049 OUT UINT64 *MaximumVariableStorageSize,\r
1050 OUT UINT64 *RemainingVariableStorageSize,\r
1051 OUT UINT64 *MaximumVariableSize\r
1052 )\r
1053{\r
1054 EFI_STATUS Status;\r
1055 UINTN PayloadSize;\r
1056 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *SmmQueryVariableInfo;\r
1057\r
4e1005ec
ED
1058 SmmQueryVariableInfo = NULL;\r
1059\r
8a2d4996 1060 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
1061 return EFI_INVALID_PARAMETER;\r
1062 }\r
6ed1ec59
SZ
1063\r
1064 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
1065\r
8a2d4996 1066 //\r
1067 // Init the communicate buffer. The buffer data size is:\r
1068 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize;\r
1069 //\r
d00ed85e 1070 PayloadSize = sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO);\r
5c7fa429 1071 Status = InitCommunicateBuffer ((VOID **)&SmmQueryVariableInfo, PayloadSize, SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO);\r
8a2d4996 1072 if (EFI_ERROR (Status)) {\r
6ed1ec59 1073 goto Done;\r
8a2d4996 1074 }\r
1075 ASSERT (SmmQueryVariableInfo != NULL);\r
1076\r
1077 SmmQueryVariableInfo->Attributes = Attributes;\r
1078\r
1079 //\r
1080 // Send data to SMM.\r
1081 //\r
1082 Status = SendCommunicateBuffer (PayloadSize);\r
1083 if (EFI_ERROR (Status)) {\r
6ed1ec59 1084 goto Done;\r
8a2d4996 1085 }\r
1086\r
1087 //\r
1088 // Get data from SMM.\r
1089 //\r
1090 *MaximumVariableSize = SmmQueryVariableInfo->MaximumVariableSize;\r
1091 *MaximumVariableStorageSize = SmmQueryVariableInfo->MaximumVariableStorageSize;\r
fa0737a8 1092 *RemainingVariableStorageSize = SmmQueryVariableInfo->RemainingVariableStorageSize;\r
6ed1ec59
SZ
1093\r
1094Done:\r
1095 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
aab9212f 1096 return Status;\r
8a2d4996 1097}\r
1098\r
1099\r
1100/**\r
1101 Exit Boot Services Event notification handler.\r
1102\r
1103 Notify SMM variable driver about the event.\r
1104\r
1105 @param[in] Event Event whose notification function is being invoked.\r
1106 @param[in] Context Pointer to the notification function's context.\r
1107\r
1108**/\r
1109VOID\r
1110EFIAPI\r
1111OnExitBootServices (\r
1112 IN EFI_EVENT Event,\r
1113 IN VOID *Context\r
1114 )\r
1115{\r
1116 //\r
1117 // Init the communicate buffer. The buffer data size is:\r
1118 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.\r
1119 //\r
fa0737a8 1120 InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE);\r
8a2d4996 1121\r
1122 //\r
1123 // Send data to SMM.\r
1124 //\r
1125 SendCommunicateBuffer (0);\r
1126}\r
1127\r
1128\r
1129/**\r
1130 On Ready To Boot Services Event notification handler.\r
1131\r
1132 Notify SMM variable driver about the event.\r
1133\r
1134 @param[in] Event Event whose notification function is being invoked\r
1135 @param[in] Context Pointer to the notification function's context\r
1136\r
1137**/\r
1138VOID\r
1139EFIAPI\r
1140OnReadyToBoot (\r
1141 IN EFI_EVENT Event,\r
1142 IN VOID *Context\r
1143 )\r
1144{\r
1145 //\r
1146 // Init the communicate buffer. The buffer data size is:\r
1147 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.\r
1148 //\r
1149 InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_READY_TO_BOOT);\r
fa0737a8 1150\r
8a2d4996 1151 //\r
1152 // Send data to SMM.\r
1153 //\r
1154 SendCommunicateBuffer (0);\r
fa0737a8 1155\r
aab3b9b9
MK
1156 //\r
1157 // Install the system configuration table for variable info data captured\r
1158 //\r
1159 if (FeaturePcdGet (PcdEnableVariableRuntimeCache) && FeaturePcdGet (PcdVariableCollectStatistics)) {\r
1160 if (mVariableAuthFormat) {\r
1161 gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid, mVariableInfo);\r
1162 } else {\r
1163 gBS->InstallConfigurationTable (&gEfiVariableGuid, mVariableInfo);\r
1164 }\r
1165 }\r
1166\r
fa0737a8 1167 gBS->CloseEvent (Event);\r
8a2d4996 1168}\r
1169\r
1170\r
1171/**\r
1172 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
1173\r
1174 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
1175 It convers pointer to new virtual address.\r
1176\r
1177 @param[in] Event Event whose notification function is being invoked.\r
1178 @param[in] Context Pointer to the notification function's context.\r
1179\r
1180**/\r
1181VOID\r
1182EFIAPI\r
1183VariableAddressChangeEvent (\r
1184 IN EFI_EVENT Event,\r
1185 IN VOID *Context\r
1186 )\r
1187{\r
1188 EfiConvertPointer (0x0, (VOID **) &mVariableBuffer);\r
1189 EfiConvertPointer (0x0, (VOID **) &mSmmCommunication);\r
aab3b9b9
MK
1190 EfiConvertPointer (EFI_OPTIONAL_PTR, (VOID **) &mVariableRuntimeHobCacheBuffer);\r
1191 EfiConvertPointer (EFI_OPTIONAL_PTR, (VOID **) &mVariableRuntimeNvCacheBuffer);\r
1192 EfiConvertPointer (EFI_OPTIONAL_PTR, (VOID **) &mVariableRuntimeVolatileCacheBuffer);\r
8a2d4996 1193}\r
1194\r
fa0737a8
SZ
1195/**\r
1196 This code gets variable payload size.\r
1197\r
1198 @param[out] VariablePayloadSize Output pointer to variable payload size.\r
1199\r
1200 @retval EFI_SUCCESS Get successfully.\r
1201 @retval Others Get unsuccessfully.\r
1202\r
1203**/\r
1204EFI_STATUS\r
1205EFIAPI\r
1206GetVariablePayloadSize (\r
1207 OUT UINTN *VariablePayloadSize\r
1208 )\r
1209{\r
1210 EFI_STATUS Status;\r
1211 SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE *SmmGetPayloadSize;\r
1212 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader;\r
1213 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;\r
1214 UINTN CommSize;\r
1215 UINT8 *CommBuffer;\r
1216\r
1217 SmmGetPayloadSize = NULL;\r
1218 CommBuffer = NULL;\r
1219\r
1220 if(VariablePayloadSize == NULL) {\r
1221 return EFI_INVALID_PARAMETER;\r
1222 }\r
1223\r
1224 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
1225\r
1226 //\r
1227 // Init the communicate buffer. The buffer data size is:\r
1228 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE);\r
1229 //\r
1230 CommSize = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE);\r
1231 CommBuffer = AllocateZeroPool (CommSize);\r
1232 if (CommBuffer == NULL) {\r
1233 Status = EFI_OUT_OF_RESOURCES;\r
1234 goto Done;\r
1235 }\r
1236\r
1237 SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) CommBuffer;\r
1238 CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);\r
1239 SmmCommunicateHeader->MessageLength = SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE);\r
1240\r
1241 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader->Data;\r
1242 SmmVariableFunctionHeader->Function = SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE;\r
1243 SmmGetPayloadSize = (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE *) SmmVariableFunctionHeader->Data;\r
1244\r
1245 //\r
1246 // Send data to SMM.\r
1247 //\r
1248 Status = mSmmCommunication->Communicate (mSmmCommunication, CommBuffer, &CommSize);\r
1249 ASSERT_EFI_ERROR (Status);\r
1250\r
1251 Status = SmmVariableFunctionHeader->ReturnStatus;\r
1252 if (EFI_ERROR (Status)) {\r
1253 goto Done;\r
1254 }\r
1255\r
1256 //\r
1257 // Get data from SMM.\r
1258 //\r
1259 *VariablePayloadSize = SmmGetPayloadSize->VariablePayloadSize;\r
1260\r
1261Done:\r
1262 if (CommBuffer != NULL) {\r
1263 FreePool (CommBuffer);\r
1264 }\r
1265 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
1266 return Status;\r
1267}\r
8a2d4996 1268\r
aab3b9b9
MK
1269/**\r
1270 This code gets information needed from SMM for runtime cache initialization.\r
1271\r
1272 @param[out] TotalHobStorageSize Output pointer for the total HOB storage size in bytes.\r
1273 @param[out] TotalNvStorageSize Output pointer for the total non-volatile storage size in bytes.\r
1274 @param[out] TotalVolatileStorageSize Output pointer for the total volatile storage size in bytes.\r
1275 @param[out] AuthenticatedVariableUsage Output pointer that indicates if authenticated variables are to be used.\r
1276\r
1277 @retval EFI_SUCCESS Retrieved the size successfully.\r
1278 @retval EFI_INVALID_PARAMETER TotalNvStorageSize parameter is NULL.\r
1279 @retval EFI_OUT_OF_RESOURCES The memory resources needed for a CommBuffer are not available.\r
1280 @retval Others Could not retrieve the size successfully.\r
1281\r
1282**/\r
1283EFI_STATUS\r
1284GetRuntimeCacheInfo (\r
1285 OUT UINTN *TotalHobStorageSize,\r
1286 OUT UINTN *TotalNvStorageSize,\r
1287 OUT UINTN *TotalVolatileStorageSize,\r
1288 OUT BOOLEAN *AuthenticatedVariableUsage\r
1289 )\r
1290{\r
1291 EFI_STATUS Status;\r
1292 SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *SmmGetRuntimeCacheInfo;\r
1293 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader;\r
1294 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;\r
1295 UINTN CommSize;\r
1296 UINT8 *CommBuffer;\r
1297\r
1298 SmmGetRuntimeCacheInfo = NULL;\r
1299 CommBuffer = mVariableBuffer;\r
1300\r
1301 if (TotalHobStorageSize == NULL || TotalNvStorageSize == NULL || TotalVolatileStorageSize == NULL || AuthenticatedVariableUsage == NULL) {\r
1302 return EFI_INVALID_PARAMETER;\r
1303 }\r
1304\r
1305 if (CommBuffer == NULL) {\r
1306 return EFI_OUT_OF_RESOURCES;\r
1307 }\r
1308\r
1309 AcquireLockOnlyAtBootTime (&mVariableServicesLock);\r
1310\r
1311 CommSize = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);\r
1312 ZeroMem (CommBuffer, CommSize);\r
1313\r
1314 SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) CommBuffer;\r
1315 CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);\r
1316 SmmCommunicateHeader->MessageLength = SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);\r
1317\r
1318 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader->Data;\r
1319 SmmVariableFunctionHeader->Function = SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO;\r
1320 SmmGetRuntimeCacheInfo = (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *) SmmVariableFunctionHeader->Data;\r
1321\r
1322 //\r
1323 // Send data to SMM.\r
1324 //\r
1325 Status = mSmmCommunication->Communicate (mSmmCommunication, CommBuffer, &CommSize);\r
1326 ASSERT_EFI_ERROR (Status);\r
1327 if (CommSize <= SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {\r
1328 Status = EFI_BAD_BUFFER_SIZE;\r
1329 goto Done;\r
1330 }\r
1331\r
1332 Status = SmmVariableFunctionHeader->ReturnStatus;\r
1333 if (EFI_ERROR (Status)) {\r
1334 goto Done;\r
1335 }\r
1336\r
1337 //\r
1338 // Get data from SMM.\r
1339 //\r
1340 *TotalHobStorageSize = SmmGetRuntimeCacheInfo->TotalHobStorageSize;\r
1341 *TotalNvStorageSize = SmmGetRuntimeCacheInfo->TotalNvStorageSize;\r
1342 *TotalVolatileStorageSize = SmmGetRuntimeCacheInfo->TotalVolatileStorageSize;\r
1343 *AuthenticatedVariableUsage = SmmGetRuntimeCacheInfo->AuthenticatedVariableUsage;\r
1344\r
1345Done:\r
1346 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
1347 return Status;\r
1348}\r
1349\r
1350/**\r
1351 Sends the runtime variable cache context information to SMM.\r
1352\r
1353 @retval EFI_SUCCESS Retrieved the size successfully.\r
1354 @retval EFI_INVALID_PARAMETER TotalNvStorageSize parameter is NULL.\r
1355 @retval EFI_OUT_OF_RESOURCES The memory resources needed for a CommBuffer are not available.\r
1356 @retval Others Could not retrieve the size successfully.;\r
1357\r
1358**/\r
1359EFI_STATUS\r
1360SendRuntimeVariableCacheContextToSmm (\r
1361 VOID\r
1362 )\r
1363{\r
1364 EFI_STATUS Status;\r
1365 SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT *SmmRuntimeVarCacheContext;\r
1366 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader;\r
1367 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;\r
1368 UINTN CommSize;\r
1369 UINT8 *CommBuffer;\r
1370\r
1371 SmmRuntimeVarCacheContext = NULL;\r
1372 CommBuffer = mVariableBuffer;\r
1373\r
1374 if (CommBuffer == NULL) {\r
1375 return EFI_OUT_OF_RESOURCES;\r
1376 }\r
1377\r
1378 AcquireLockOnlyAtBootTime (&mVariableServicesLock);\r
1379\r
1380 //\r
1381 // Init the communicate buffer. The buffer data size is:\r
1382 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT);\r
1383 //\r
1384 CommSize = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT);\r
1385 ZeroMem (CommBuffer, CommSize);\r
1386\r
1387 SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) CommBuffer;\r
1388 CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);\r
1389 SmmCommunicateHeader->MessageLength = SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT);\r
1390\r
1391 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader->Data;\r
1392 SmmVariableFunctionHeader->Function = SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT;\r
1393 SmmRuntimeVarCacheContext = (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT *) SmmVariableFunctionHeader->Data;\r
1394\r
1395 SmmRuntimeVarCacheContext->RuntimeHobCache = mVariableRuntimeHobCacheBuffer;\r
1396 SmmRuntimeVarCacheContext->RuntimeVolatileCache = mVariableRuntimeVolatileCacheBuffer;\r
1397 SmmRuntimeVarCacheContext->RuntimeNvCache = mVariableRuntimeNvCacheBuffer;\r
1398 SmmRuntimeVarCacheContext->PendingUpdate = &mVariableRuntimeCachePendingUpdate;\r
1399 SmmRuntimeVarCacheContext->ReadLock = &mVariableRuntimeCacheReadLock;\r
1400 SmmRuntimeVarCacheContext->HobFlushComplete = &mHobFlushComplete;\r
1401\r
1402 //\r
1403 // Send data to SMM.\r
1404 //\r
1405 Status = mSmmCommunication->Communicate (mSmmCommunication, CommBuffer, &CommSize);\r
1406 ASSERT_EFI_ERROR (Status);\r
1407 if (CommSize <= SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {\r
1408 Status = EFI_BAD_BUFFER_SIZE;\r
1409 goto Done;\r
1410 }\r
1411\r
1412 Status = SmmVariableFunctionHeader->ReturnStatus;\r
1413 if (EFI_ERROR (Status)) {\r
1414 goto Done;\r
1415 }\r
1416\r
1417Done:\r
1418 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
1419 return Status;\r
1420}\r
1421\r
8a2d4996 1422/**\r
1423 Initialize variable service and install Variable Architectural protocol.\r
1424\r
1425 @param[in] Event Event whose notification function is being invoked.\r
1426 @param[in] Context Pointer to the notification function's context.\r
fa0737a8 1427\r
8a2d4996 1428**/\r
1429VOID\r
1430EFIAPI\r
1431SmmVariableReady (\r
1432 IN EFI_EVENT Event,\r
1433 IN VOID *Context\r
1434 )\r
1435{\r
1436 EFI_STATUS Status;\r
1437\r
aab3b9b9 1438 Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID **) &mSmmVariable);\r
8a2d4996 1439 if (EFI_ERROR (Status)) {\r
1440 return;\r
1441 }\r
fa0737a8 1442\r
8a2d4996 1443 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);\r
1444 ASSERT_EFI_ERROR (Status);\r
fa0737a8 1445\r
8a2d4996 1446 //\r
5e5bb2a9 1447 // Allocate memory for variable communicate buffer.\r
8a2d4996 1448 //\r
fa0737a8
SZ
1449 Status = GetVariablePayloadSize (&mVariableBufferPayloadSize);\r
1450 ASSERT_EFI_ERROR (Status);\r
5e5bb2a9 1451 mVariableBufferSize = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + mVariableBufferPayloadSize;\r
8a2d4996 1452 mVariableBuffer = AllocateRuntimePool (mVariableBufferSize);\r
1453 ASSERT (mVariableBuffer != NULL);\r
1454\r
1455 //\r
1456 // Save the buffer physical address used for SMM conmunication.\r
1457 //\r
1458 mVariableBufferPhysical = mVariableBuffer;\r
1459\r
aab3b9b9
MK
1460 if (FeaturePcdGet (PcdEnableVariableRuntimeCache)) {\r
1461 DEBUG ((DEBUG_INFO, "Variable driver runtime cache is enabled.\n"));\r
1462 //\r
1463 // Allocate runtime variable cache memory buffers.\r
1464 //\r
1465 Status = GetRuntimeCacheInfo (\r
1466 &mVariableRuntimeHobCacheBufferSize,\r
1467 &mVariableRuntimeNvCacheBufferSize,\r
1468 &mVariableRuntimeVolatileCacheBufferSize,\r
1469 &mVariableAuthFormat\r
1470 );\r
1471 if (!EFI_ERROR (Status)) {\r
1472 Status = InitVariableCache (&mVariableRuntimeHobCacheBuffer, &mVariableRuntimeHobCacheBufferSize);\r
1473 if (!EFI_ERROR (Status)) {\r
1474 Status = InitVariableCache (&mVariableRuntimeNvCacheBuffer, &mVariableRuntimeNvCacheBufferSize);\r
1475 if (!EFI_ERROR (Status)) {\r
1476 Status = InitVariableCache (&mVariableRuntimeVolatileCacheBuffer, &mVariableRuntimeVolatileCacheBufferSize);\r
1477 if (!EFI_ERROR (Status)) {\r
1478 Status = SendRuntimeVariableCacheContextToSmm ();\r
1479 if (!EFI_ERROR (Status)) {\r
1480 SyncRuntimeCache ();\r
1481 }\r
1482 }\r
1483 }\r
1484 }\r
1485 if (EFI_ERROR (Status)) {\r
1486 mVariableRuntimeHobCacheBuffer = NULL;\r
1487 mVariableRuntimeNvCacheBuffer = NULL;\r
1488 mVariableRuntimeVolatileCacheBuffer = NULL;\r
1489 }\r
1490 }\r
1491 ASSERT_EFI_ERROR (Status);\r
1492 } else {\r
1493 DEBUG ((DEBUG_INFO, "Variable driver runtime cache is disabled.\n"));\r
1494 }\r
1495\r
8a2d4996 1496 gRT->GetVariable = RuntimeServiceGetVariable;\r
1497 gRT->GetNextVariableName = RuntimeServiceGetNextVariableName;\r
1498 gRT->SetVariable = RuntimeServiceSetVariable;\r
1499 gRT->QueryVariableInfo = RuntimeServiceQueryVariableInfo;\r
fa0737a8 1500\r
8a2d4996 1501 //\r
1502 // Install the Variable Architectural Protocol on a new handle.\r
1503 //\r
1504 Status = gBS->InstallProtocolInterface (\r
1505 &mHandle,\r
fa0737a8 1506 &gEfiVariableArchProtocolGuid,\r
8a2d4996 1507 EFI_NATIVE_INTERFACE,\r
1508 NULL\r
1509 );\r
1510 ASSERT_EFI_ERROR (Status);\r
aa2868b3
SZ
1511\r
1512 mVariableLock.RequestToLock = VariableLockRequestToLock;\r
1513 Status = gBS->InstallMultipleProtocolInterfaces (\r
1514 &mHandle,\r
1515 &gEdkiiVariableLockProtocolGuid,\r
1516 &mVariableLock,\r
1517 NULL\r
1518 );\r
1519 ASSERT_EFI_ERROR (Status);\r
1520\r
1521 mVarCheck.RegisterSetVariableCheckHandler = VarCheckRegisterSetVariableCheckHandler;\r
1522 mVarCheck.VariablePropertySet = VarCheckVariablePropertySet;\r
1523 mVarCheck.VariablePropertyGet = VarCheckVariablePropertyGet;\r
1524 Status = gBS->InstallMultipleProtocolInterfaces (\r
1525 &mHandle,\r
1526 &gEdkiiVarCheckProtocolGuid,\r
1527 &mVarCheck,\r
1528 NULL\r
1529 );\r
1530 ASSERT_EFI_ERROR (Status);\r
fa0737a8
SZ
1531\r
1532 gBS->CloseEvent (Event);\r
8a2d4996 1533}\r
1534\r
1535\r
1536/**\r
1537 SMM Non-Volatile variable write service is ready notify event handler.\r
1538\r
1539 @param[in] Event Event whose notification function is being invoked.\r
1540 @param[in] Context Pointer to the notification function's context.\r
fa0737a8 1541\r
8a2d4996 1542**/\r
1543VOID\r
1544EFIAPI\r
1545SmmVariableWriteReady (\r
1546 IN EFI_EVENT Event,\r
1547 IN VOID *Context\r
1548 )\r
1549{\r
1550 EFI_STATUS Status;\r
1551 VOID *ProtocolOps;\r
1552\r
1553 //\r
1554 // Check whether the protocol is installed or not.\r
1555 //\r
d00ed85e 1556 Status = gBS->LocateProtocol (&gSmmVariableWriteGuid, NULL, (VOID **) &ProtocolOps);\r
8a2d4996 1557 if (EFI_ERROR (Status)) {\r
1558 return;\r
1559 }\r
fa0737a8 1560\r
dc9bd6ed
ZC
1561 //\r
1562 // Some Secure Boot Policy Var (SecureBoot, etc) updates following other\r
1563 // Secure Boot Policy Variable change. Record their initial value.\r
1564 //\r
1565 RecordSecureBootPolicyVarData();\r
1566\r
8a2d4996 1567 Status = gBS->InstallProtocolInterface (\r
1568 &mHandle,\r
fa0737a8 1569 &gEfiVariableWriteArchProtocolGuid,\r
8a2d4996 1570 EFI_NATIVE_INTERFACE,\r
1571 NULL\r
1572 );\r
fa0737a8
SZ
1573 ASSERT_EFI_ERROR (Status);\r
1574\r
1575 gBS->CloseEvent (Event);\r
8a2d4996 1576}\r
1577\r
1578\r
1579/**\r
1580 Variable Driver main entry point. The Variable driver places the 4 EFI\r
fa0737a8 1581 runtime services in the EFI System Table and installs arch protocols\r
d00ed85e 1582 for variable read and write services being available. It also registers\r
8a2d4996 1583 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
1584\r
fa0737a8 1585 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
8a2d4996 1586 @param[in] SystemTable A pointer to the EFI System Table.\r
fa0737a8 1587\r
8a2d4996 1588 @retval EFI_SUCCESS Variable service successfully initialized.\r
1589\r
1590**/\r
1591EFI_STATUS\r
1592EFIAPI\r
1593VariableSmmRuntimeInitialize (\r
1594 IN EFI_HANDLE ImageHandle,\r
1595 IN EFI_SYSTEM_TABLE *SystemTable\r
1596 )\r
1597{\r
1598 VOID *SmmVariableRegistration;\r
1599 VOID *SmmVariableWriteRegistration;\r
1600 EFI_EVENT OnReadyToBootEvent;\r
1601 EFI_EVENT ExitBootServiceEvent;\r
d3460bcb 1602 EFI_EVENT LegacyBootEvent;\r
6ed1ec59
SZ
1603\r
1604 EfiInitializeLock (&mVariableServicesLock, TPL_NOTIFY);\r
1605\r
8a2d4996 1606 //\r
1607 // Smm variable service is ready\r
1608 //\r
1609 EfiCreateProtocolNotifyEvent (\r
fa0737a8
SZ
1610 &gEfiSmmVariableProtocolGuid,\r
1611 TPL_CALLBACK,\r
1612 SmmVariableReady,\r
1613 NULL,\r
8a2d4996 1614 &SmmVariableRegistration\r
1615 );\r
1616\r
1617 //\r
1618 // Smm Non-Volatile variable write service is ready\r
1619 //\r
1620 EfiCreateProtocolNotifyEvent (\r
fa0737a8
SZ
1621 &gSmmVariableWriteGuid,\r
1622 TPL_CALLBACK,\r
1623 SmmVariableWriteReady,\r
1624 NULL,\r
8a2d4996 1625 &SmmVariableWriteRegistration\r
1626 );\r
1627\r
1628 //\r
1629 // Register the event to reclaim variable for OS usage.\r
1630 //\r
1631 EfiCreateEventReadyToBootEx (\r
fa0737a8
SZ
1632 TPL_NOTIFY,\r
1633 OnReadyToBoot,\r
1634 NULL,\r
8a2d4996 1635 &OnReadyToBootEvent\r
fa0737a8 1636 );\r
8a2d4996 1637\r
1638 //\r
1639 // Register the event to inform SMM variable that it is at runtime.\r
1640 //\r
1641 gBS->CreateEventEx (\r
1642 EVT_NOTIFY_SIGNAL,\r
1643 TPL_NOTIFY,\r
1644 OnExitBootServices,\r
1645 NULL,\r
1646 &gEfiEventExitBootServicesGuid,\r
1647 &ExitBootServiceEvent\r
fa0737a8 1648 );\r
8a2d4996 1649\r
d3460bcb
SZ
1650 //\r
1651 // Register the event to inform SMM variable that it is at runtime for legacy boot.\r
1652 // Reuse OnExitBootServices() here.\r
1653 //\r
1654 EfiCreateEventLegacyBootEx(\r
1655 TPL_NOTIFY,\r
1656 OnExitBootServices,\r
1657 NULL,\r
1658 &LegacyBootEvent\r
1659 );\r
1660\r
8a2d4996 1661 //\r
1662 // Register the event to convert the pointer for runtime.\r
1663 //\r
1664 gBS->CreateEventEx (\r
1665 EVT_NOTIFY_SIGNAL,\r
1666 TPL_NOTIFY,\r
1667 VariableAddressChangeEvent,\r
1668 NULL,\r
1669 &gEfiEventVirtualAddressChangeGuid,\r
1670 &mVirtualAddressChangeEvent\r
1671 );\r
fa0737a8 1672\r
8a2d4996 1673 return EFI_SUCCESS;\r
1674}\r
1675\r