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