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