]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
Fix SMM Variable driver stack GetVariable return INVALID_PARAMETER when DataSize...
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / VariableSmmRuntimeDxe.c
1 /** @file
2
3 Implement all four UEFI Runtime Variable services for the nonvolatile
4 and volatile storage space and install variable architecture protocol
5 based on SMM variable module.
6
7 Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17 #include <PiDxe.h>
18 #include <Protocol/VariableWrite.h>
19 #include <Protocol/Variable.h>
20 #include <Protocol/SmmCommunication.h>
21 #include <Protocol/SmmVariable.h>
22
23 #include <Library/UefiBootServicesTableLib.h>
24 #include <Library/UefiRuntimeServicesTableLib.h>
25 #include <Library/MemoryAllocationLib.h>
26 #include <Library/UefiDriverEntryPoint.h>
27 #include <Library/UefiRuntimeLib.h>
28 #include <Library/BaseMemoryLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/PcdLib.h>
31 #include <Library/UefiLib.h>
32 #include <Library/BaseLib.h>
33
34 #include <Guid/EventGroup.h>
35 #include <Guid/VariableFormat.h>
36 #include <Guid/SmmVariableCommon.h>
37
38 EFI_HANDLE mHandle = NULL;
39 EFI_SMM_VARIABLE_PROTOCOL *mSmmVariable = NULL;
40 EFI_EVENT mVirtualAddressChangeEvent = NULL;
41 EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL;
42 UINT8 *mVariableBuffer = NULL;
43 UINT8 *mVariableBufferPhysical = NULL;
44 UINTN mVariableBufferSize;
45 EFI_LOCK mVariableServicesLock;
46
47 /**
48 Acquires lock only at boot time. Simply returns at runtime.
49
50 This is a temperary function that will be removed when
51 EfiAcquireLock() in UefiLib can handle the call in UEFI
52 Runtimer driver in RT phase.
53 It calls EfiAcquireLock() at boot time, and simply returns
54 at runtime.
55
56 @param Lock A pointer to the lock to acquire.
57
58 **/
59 VOID
60 AcquireLockOnlyAtBootTime (
61 IN EFI_LOCK *Lock
62 )
63 {
64 if (!EfiAtRuntime ()) {
65 EfiAcquireLock (Lock);
66 }
67 }
68
69 /**
70 Releases lock only at boot time. Simply returns at runtime.
71
72 This is a temperary function which will be removed when
73 EfiReleaseLock() in UefiLib can handle the call in UEFI
74 Runtimer driver in RT phase.
75 It calls EfiReleaseLock() at boot time and simply returns
76 at runtime.
77
78 @param Lock A pointer to the lock to release.
79
80 **/
81 VOID
82 ReleaseLockOnlyAtBootTime (
83 IN EFI_LOCK *Lock
84 )
85 {
86 if (!EfiAtRuntime ()) {
87 EfiReleaseLock (Lock);
88 }
89 }
90
91 /**
92 Initialize the communicate buffer using DataSize and Function.
93
94 The communicate size is: SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +
95 DataSize.
96
97 @param[out] DataPtr Points to the data in the communicate buffer.
98 @param[in] DataSize The data size to send to SMM.
99 @param[in] Function The function number to initialize the communicate header.
100
101 @retval EFI_INVALID_PARAMETER The data size is too big.
102 @retval EFI_SUCCESS Find the specified variable.
103
104 **/
105 EFI_STATUS
106 InitCommunicateBuffer (
107 OUT VOID **DataPtr OPTIONAL,
108 IN UINTN DataSize,
109 IN UINTN Function
110 )
111 {
112 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader;
113 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;
114
115
116 if (DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE > mVariableBufferSize) {
117 return EFI_INVALID_PARAMETER;
118 }
119
120 SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) mVariableBuffer;
121 CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);
122 SmmCommunicateHeader->MessageLength = DataSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;
123
124 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader->Data;
125 SmmVariableFunctionHeader->Function = Function;
126 if (DataPtr != NULL) {
127 *DataPtr = SmmVariableFunctionHeader->Data;
128 }
129
130 return EFI_SUCCESS;
131 }
132
133
134 /**
135 Send the data in communicate buffer to SMM.
136
137 @param[in] DataSize This size of the function header and the data.
138
139 @retval EFI_SUCCESS Success is returned from the functin in SMM.
140 @retval Others Failure is returned from the function in SMM.
141
142 **/
143 EFI_STATUS
144 SendCommunicateBuffer (
145 IN UINTN DataSize
146 )
147 {
148 EFI_STATUS Status;
149 UINTN CommSize;
150 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader;
151 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;
152
153 CommSize = DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;
154 Status = mSmmCommunication->Communicate (mSmmCommunication, mVariableBufferPhysical, &CommSize);
155 ASSERT_EFI_ERROR (Status);
156
157 SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) mVariableBuffer;
158 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)SmmCommunicateHeader->Data;
159 return SmmVariableFunctionHeader->ReturnStatus;
160 }
161
162
163 /**
164 This code finds variable in storage blocks (Volatile or Non-Volatile).
165
166 @param[in] VariableName Name of Variable to be found.
167 @param[in] VendorGuid Variable vendor GUID.
168 @param[out] Attributes Attribute value of the variable found.
169 @param[in, out] DataSize Size of Data found. If size is less than the
170 data, this value contains the required size.
171 @param[out] Data Data pointer.
172
173 @retval EFI_INVALID_PARAMETER Invalid parameter.
174 @retval EFI_SUCCESS Find the specified variable.
175 @retval EFI_NOT_FOUND Not found.
176 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.
177
178 **/
179 EFI_STATUS
180 EFIAPI
181 RuntimeServiceGetVariable (
182 IN CHAR16 *VariableName,
183 IN EFI_GUID *VendorGuid,
184 OUT UINT32 *Attributes OPTIONAL,
185 IN OUT UINTN *DataSize,
186 OUT VOID *Data
187 )
188 {
189 EFI_STATUS Status;
190 UINTN PayloadSize;
191 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;
192 UINTN SmmCommBufPayloadSize;
193 UINTN TempDataSize;
194
195 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
196 return EFI_INVALID_PARAMETER;
197 }
198
199 if ((*DataSize != 0) && (Data == NULL)) {
200 return EFI_INVALID_PARAMETER;
201 }
202
203 //
204 // SMM Communication Buffer max payload size
205 //
206 SmmCommBufPayloadSize = mVariableBufferSize - (SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE);
207 TempDataSize = *DataSize;
208
209 //
210 // If VariableName exceeds SMM payload limit. Return failure
211 //
212 if (StrSize (VariableName) > SmmCommBufPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {
213 return EFI_INVALID_PARAMETER;
214 }
215
216 AcquireLockOnlyAtBootTime(&mVariableServicesLock);
217
218 //
219 // Init the communicate buffer. The buffer data size is:
220 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
221 //
222 if (TempDataSize > SmmCommBufPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - StrSize (VariableName)) {
223 //
224 // If output data buffer exceed SMM payload limit. Trim output buffer to SMM payload size
225 //
226 TempDataSize = SmmCommBufPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - StrSize (VariableName);
227 }
228 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + StrSize (VariableName) + TempDataSize;
229
230 Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);
231 if (EFI_ERROR (Status)) {
232 goto Done;
233 }
234 ASSERT (SmmVariableHeader != NULL);
235
236 CopyGuid (&SmmVariableHeader->Guid, VendorGuid);
237 SmmVariableHeader->DataSize = TempDataSize;
238 SmmVariableHeader->NameSize = StrSize (VariableName);
239 if (Attributes == NULL) {
240 SmmVariableHeader->Attributes = 0;
241 } else {
242 SmmVariableHeader->Attributes = *Attributes;
243 }
244 CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);
245
246 //
247 // Send data to SMM.
248 //
249 Status = SendCommunicateBuffer (PayloadSize);
250
251 //
252 // Get data from SMM.
253 //
254 if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {
255 //
256 // SMM CommBuffer DataSize can be a trimed value
257 // Only update DataSize when needed
258 //
259 *DataSize = SmmVariableHeader->DataSize;
260 }
261 if (Attributes != NULL) {
262 *Attributes = SmmVariableHeader->Attributes;
263 }
264
265 if (EFI_ERROR (Status)) {
266 goto Done;
267 }
268
269 CopyMem (Data, (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize, SmmVariableHeader->DataSize);
270
271 Done:
272 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
273 return Status;
274 }
275
276
277 /**
278 This code Finds the Next available variable.
279
280 @param[in, out] VariableNameSize Size of the variable name.
281 @param[in, out] VariableName Pointer to variable name.
282 @param[in, out] VendorGuid Variable Vendor Guid.
283
284 @retval EFI_INVALID_PARAMETER Invalid parameter.
285 @retval EFI_SUCCESS Find the specified variable.
286 @retval EFI_NOT_FOUND Not found.
287 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.
288
289 **/
290 EFI_STATUS
291 EFIAPI
292 RuntimeServiceGetNextVariableName (
293 IN OUT UINTN *VariableNameSize,
294 IN OUT CHAR16 *VariableName,
295 IN OUT EFI_GUID *VendorGuid
296 )
297 {
298 EFI_STATUS Status;
299 UINTN PayloadSize;
300 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *SmmGetNextVariableName;
301 UINTN SmmCommBufPayloadSize;
302
303 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
304 return EFI_INVALID_PARAMETER;
305 }
306
307 //
308 // SMM Communication Buffer max payload size
309 //
310 SmmCommBufPayloadSize = mVariableBufferSize - (SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE);
311
312 //
313 // If input string exceeds SMM payload limit. Return failure
314 //
315 if (StrSize (VariableName) > SmmCommBufPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {
316 return EFI_INVALID_PARAMETER;
317 }
318
319 AcquireLockOnlyAtBootTime(&mVariableServicesLock);
320
321 //
322 // Init the communicate buffer. The buffer data size is:
323 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
324 //
325 if (*VariableNameSize > SmmCommBufPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {
326 //
327 // If output buffer exceed SMM payload limit. Trim output buffer to SMM payload size
328 //
329 *VariableNameSize = SmmCommBufPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);
330 }
331 //
332 // Payload should be Guid + NameSize + MAX of Input & Output buffer
333 //
334 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + MAX (*VariableNameSize, StrSize (VariableName));
335
336
337 Status = InitCommunicateBuffer ((VOID **)&SmmGetNextVariableName, PayloadSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME);
338 if (EFI_ERROR (Status)) {
339 goto Done;
340 }
341 ASSERT (SmmGetNextVariableName != NULL);
342
343 //
344 // SMM comm buffer->NameSize is buffer size for return string
345 //
346 SmmGetNextVariableName->NameSize = *VariableNameSize;
347
348 CopyGuid (&SmmGetNextVariableName->Guid, VendorGuid);
349 //
350 // Copy whole string
351 //
352 CopyMem (SmmGetNextVariableName->Name, VariableName, StrSize (VariableName));
353
354 //
355 // Send data to SMM
356 //
357 Status = SendCommunicateBuffer (PayloadSize);
358
359 //
360 // Get data from SMM.
361 //
362 *VariableNameSize = SmmGetNextVariableName->NameSize;
363 if (EFI_ERROR (Status)) {
364 goto Done;
365 }
366
367 CopyGuid (VendorGuid, &SmmGetNextVariableName->Guid);
368 CopyMem (VariableName, SmmGetNextVariableName->Name, SmmGetNextVariableName->NameSize);
369
370 Done:
371 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
372 return Status;
373 }
374
375 /**
376 This code sets variable in storage blocks (Volatile or Non-Volatile).
377
378 @param[in] VariableName Name of Variable to be found.
379 @param[in] VendorGuid Variable vendor GUID.
380 @param[in] Attributes Attribute value of the variable found
381 @param[in] DataSize Size of Data found. If size is less than the
382 data, this value contains the required size.
383 @param[in] Data Data pointer.
384
385 @retval EFI_INVALID_PARAMETER Invalid parameter.
386 @retval EFI_SUCCESS Set successfully.
387 @retval EFI_OUT_OF_RESOURCES Resource not enough to set variable.
388 @retval EFI_NOT_FOUND Not found.
389 @retval EFI_WRITE_PROTECTED Variable is read-only.
390
391 **/
392 EFI_STATUS
393 EFIAPI
394 RuntimeServiceSetVariable (
395 IN CHAR16 *VariableName,
396 IN EFI_GUID *VendorGuid,
397 IN UINT32 Attributes,
398 IN UINTN DataSize,
399 IN VOID *Data
400 )
401 {
402 EFI_STATUS Status;
403 UINTN PayloadSize;
404 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;
405
406 //
407 // Check input parameters.
408 //
409 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
410 return EFI_INVALID_PARAMETER;
411 }
412
413 if (DataSize != 0 && Data == NULL) {
414 return EFI_INVALID_PARAMETER;
415 }
416
417 if (DataSize >= mVariableBufferSize) {
418 //
419 // DataSize may be near MAX_ADDRESS incorrectly, this can cause the computed PayLoadSize to
420 // overflow to a small value and pass the check in InitCommunicateBuffer().
421 // To protect against this vulnerability, return EFI_INVALID_PARAMETER if DataSize is >= mVariableBufferSize.
422 // And there will be further check to ensure the total size is also not > mVariableBufferSize.
423 //
424 return EFI_INVALID_PARAMETER;
425 }
426
427 AcquireLockOnlyAtBootTime(&mVariableServicesLock);
428
429 //
430 // Init the communicate buffer. The buffer data size is:
431 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
432 //
433 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + StrSize (VariableName) + DataSize;
434 Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_SET_VARIABLE);
435 if (EFI_ERROR (Status)) {
436 goto Done;
437 }
438 ASSERT (SmmVariableHeader != NULL);
439
440 CopyGuid ((EFI_GUID *) &SmmVariableHeader->Guid, VendorGuid);
441 SmmVariableHeader->DataSize = DataSize;
442 SmmVariableHeader->NameSize = StrSize (VariableName);
443 SmmVariableHeader->Attributes = Attributes;
444 CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);
445 CopyMem ((UINT8 *) SmmVariableHeader->Name + SmmVariableHeader->NameSize, Data, DataSize);
446
447 //
448 // Send data to SMM.
449 //
450 Status = SendCommunicateBuffer (PayloadSize);
451
452 Done:
453 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
454 return Status;
455 }
456
457
458 /**
459 This code returns information about the EFI variables.
460
461 @param[in] Attributes Attributes bitmask to specify the type of variables
462 on which to return information.
463 @param[out] MaximumVariableStorageSize Pointer to the maximum size of the storage space available
464 for the EFI variables associated with the attributes specified.
465 @param[out] RemainingVariableStorageSize Pointer to the remaining size of the storage space available
466 for EFI variables associated with the attributes specified.
467 @param[out] MaximumVariableSize Pointer to the maximum size of an individual EFI variables
468 associated with the attributes specified.
469
470 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
471 @retval EFI_SUCCESS Query successfully.
472 @retval EFI_UNSUPPORTED The attribute is not supported on this platform.
473
474 **/
475 EFI_STATUS
476 EFIAPI
477 RuntimeServiceQueryVariableInfo (
478 IN UINT32 Attributes,
479 OUT UINT64 *MaximumVariableStorageSize,
480 OUT UINT64 *RemainingVariableStorageSize,
481 OUT UINT64 *MaximumVariableSize
482 )
483 {
484 EFI_STATUS Status;
485 UINTN PayloadSize;
486 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *SmmQueryVariableInfo;
487
488 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {
489 return EFI_INVALID_PARAMETER;
490 }
491
492 AcquireLockOnlyAtBootTime(&mVariableServicesLock);
493
494 //
495 // Init the communicate buffer. The buffer data size is:
496 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize;
497 //
498 PayloadSize = sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO);
499 Status = InitCommunicateBuffer ((VOID **)&SmmQueryVariableInfo, PayloadSize, SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO);
500 if (EFI_ERROR (Status)) {
501 goto Done;
502 }
503 ASSERT (SmmQueryVariableInfo != NULL);
504
505 SmmQueryVariableInfo->Attributes = Attributes;
506
507 //
508 // Send data to SMM.
509 //
510 Status = SendCommunicateBuffer (PayloadSize);
511 if (EFI_ERROR (Status)) {
512 goto Done;
513 }
514
515 //
516 // Get data from SMM.
517 //
518 *MaximumVariableSize = SmmQueryVariableInfo->MaximumVariableSize;
519 *MaximumVariableStorageSize = SmmQueryVariableInfo->MaximumVariableStorageSize;
520 *RemainingVariableStorageSize = SmmQueryVariableInfo->RemainingVariableStorageSize;
521
522 Done:
523 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
524 return Status;
525 }
526
527
528 /**
529 Exit Boot Services Event notification handler.
530
531 Notify SMM variable driver about the event.
532
533 @param[in] Event Event whose notification function is being invoked.
534 @param[in] Context Pointer to the notification function's context.
535
536 **/
537 VOID
538 EFIAPI
539 OnExitBootServices (
540 IN EFI_EVENT Event,
541 IN VOID *Context
542 )
543 {
544 //
545 // Init the communicate buffer. The buffer data size is:
546 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
547 //
548 InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE);
549
550 //
551 // Send data to SMM.
552 //
553 SendCommunicateBuffer (0);
554 }
555
556
557 /**
558 On Ready To Boot Services Event notification handler.
559
560 Notify SMM variable driver about the event.
561
562 @param[in] Event Event whose notification function is being invoked
563 @param[in] Context Pointer to the notification function's context
564
565 **/
566 VOID
567 EFIAPI
568 OnReadyToBoot (
569 IN EFI_EVENT Event,
570 IN VOID *Context
571 )
572 {
573 //
574 // Init the communicate buffer. The buffer data size is:
575 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
576 //
577 InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_READY_TO_BOOT);
578
579 //
580 // Send data to SMM.
581 //
582 SendCommunicateBuffer (0);
583 }
584
585
586 /**
587 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
588
589 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
590 It convers pointer to new virtual address.
591
592 @param[in] Event Event whose notification function is being invoked.
593 @param[in] Context Pointer to the notification function's context.
594
595 **/
596 VOID
597 EFIAPI
598 VariableAddressChangeEvent (
599 IN EFI_EVENT Event,
600 IN VOID *Context
601 )
602 {
603 EfiConvertPointer (0x0, (VOID **) &mVariableBuffer);
604 EfiConvertPointer (0x0, (VOID **) &mSmmCommunication);
605 }
606
607
608 /**
609 Initialize variable service and install Variable Architectural protocol.
610
611 @param[in] Event Event whose notification function is being invoked.
612 @param[in] Context Pointer to the notification function's context.
613
614 **/
615 VOID
616 EFIAPI
617 SmmVariableReady (
618 IN EFI_EVENT Event,
619 IN VOID *Context
620 )
621 {
622 EFI_STATUS Status;
623
624 Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID **)&mSmmVariable);
625 if (EFI_ERROR (Status)) {
626 return;
627 }
628
629 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);
630 ASSERT_EFI_ERROR (Status);
631
632 //
633 // Allocate memory for variable store.
634 //
635 mVariableBufferSize = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;
636 mVariableBufferSize += MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));
637 mVariableBuffer = AllocateRuntimePool (mVariableBufferSize);
638 ASSERT (mVariableBuffer != NULL);
639
640 //
641 // Save the buffer physical address used for SMM conmunication.
642 //
643 mVariableBufferPhysical = mVariableBuffer;
644
645 gRT->GetVariable = RuntimeServiceGetVariable;
646 gRT->GetNextVariableName = RuntimeServiceGetNextVariableName;
647 gRT->SetVariable = RuntimeServiceSetVariable;
648 gRT->QueryVariableInfo = RuntimeServiceQueryVariableInfo;
649
650 //
651 // Install the Variable Architectural Protocol on a new handle.
652 //
653 Status = gBS->InstallProtocolInterface (
654 &mHandle,
655 &gEfiVariableArchProtocolGuid,
656 EFI_NATIVE_INTERFACE,
657 NULL
658 );
659 ASSERT_EFI_ERROR (Status);
660 }
661
662
663 /**
664 SMM Non-Volatile variable write service is ready notify event handler.
665
666 @param[in] Event Event whose notification function is being invoked.
667 @param[in] Context Pointer to the notification function's context.
668
669 **/
670 VOID
671 EFIAPI
672 SmmVariableWriteReady (
673 IN EFI_EVENT Event,
674 IN VOID *Context
675 )
676 {
677 EFI_STATUS Status;
678 VOID *ProtocolOps;
679
680 //
681 // Check whether the protocol is installed or not.
682 //
683 Status = gBS->LocateProtocol (&gSmmVariableWriteGuid, NULL, (VOID **) &ProtocolOps);
684 if (EFI_ERROR (Status)) {
685 return;
686 }
687
688 Status = gBS->InstallProtocolInterface (
689 &mHandle,
690 &gEfiVariableWriteArchProtocolGuid,
691 EFI_NATIVE_INTERFACE,
692 NULL
693 );
694 ASSERT_EFI_ERROR (Status);
695 }
696
697
698 /**
699 Variable Driver main entry point. The Variable driver places the 4 EFI
700 runtime services in the EFI System Table and installs arch protocols
701 for variable read and write services being available. It also registers
702 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
703
704 @param[in] ImageHandle The firmware allocated handle for the EFI image.
705 @param[in] SystemTable A pointer to the EFI System Table.
706
707 @retval EFI_SUCCESS Variable service successfully initialized.
708
709 **/
710 EFI_STATUS
711 EFIAPI
712 VariableSmmRuntimeInitialize (
713 IN EFI_HANDLE ImageHandle,
714 IN EFI_SYSTEM_TABLE *SystemTable
715 )
716 {
717 VOID *SmmVariableRegistration;
718 VOID *SmmVariableWriteRegistration;
719 EFI_EVENT OnReadyToBootEvent;
720 EFI_EVENT ExitBootServiceEvent;
721
722 EfiInitializeLock (&mVariableServicesLock, TPL_NOTIFY);
723
724 //
725 // Smm variable service is ready
726 //
727 EfiCreateProtocolNotifyEvent (
728 &gEfiSmmVariableProtocolGuid,
729 TPL_CALLBACK,
730 SmmVariableReady,
731 NULL,
732 &SmmVariableRegistration
733 );
734
735 //
736 // Smm Non-Volatile variable write service is ready
737 //
738 EfiCreateProtocolNotifyEvent (
739 &gSmmVariableWriteGuid,
740 TPL_CALLBACK,
741 SmmVariableWriteReady,
742 NULL,
743 &SmmVariableWriteRegistration
744 );
745
746 //
747 // Register the event to reclaim variable for OS usage.
748 //
749 EfiCreateEventReadyToBootEx (
750 TPL_NOTIFY,
751 OnReadyToBoot,
752 NULL,
753 &OnReadyToBootEvent
754 );
755
756 //
757 // Register the event to inform SMM variable that it is at runtime.
758 //
759 gBS->CreateEventEx (
760 EVT_NOTIFY_SIGNAL,
761 TPL_NOTIFY,
762 OnExitBootServices,
763 NULL,
764 &gEfiEventExitBootServicesGuid,
765 &ExitBootServiceEvent
766 );
767
768 //
769 // Register the event to convert the pointer for runtime.
770 //
771 gBS->CreateEventEx (
772 EVT_NOTIFY_SIGNAL,
773 TPL_NOTIFY,
774 VariableAddressChangeEvent,
775 NULL,
776 &gEfiEventVirtualAddressChangeGuid,
777 &mVirtualAddressChangeEvent
778 );
779
780 return EFI_SUCCESS;
781 }
782