]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
1. Use the check IsAddressValid() to prevent SMM communication buffer overflow in...
[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 UINTN VariableNameSize;
195
196 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
197 return EFI_INVALID_PARAMETER;
198 }
199
200 if ((*DataSize != 0) && (Data == NULL)) {
201 return EFI_INVALID_PARAMETER;
202 }
203
204 //
205 // SMM Communication Buffer max payload size
206 //
207 SmmCommBufPayloadSize = mVariableBufferSize - (SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE);
208 TempDataSize = *DataSize;
209 VariableNameSize = StrSize (VariableName);
210
211 //
212 // If VariableName exceeds SMM payload limit. Return failure
213 //
214 if (VariableNameSize > SmmCommBufPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {
215 return EFI_INVALID_PARAMETER;
216 }
217
218 AcquireLockOnlyAtBootTime(&mVariableServicesLock);
219
220 //
221 // Init the communicate buffer. The buffer data size is:
222 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
223 //
224 if (TempDataSize > SmmCommBufPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize) {
225 //
226 // If output data buffer exceed SMM payload limit. Trim output buffer to SMM payload size
227 //
228 TempDataSize = SmmCommBufPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize;
229 }
230 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + VariableNameSize + TempDataSize;
231
232 Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);
233 if (EFI_ERROR (Status)) {
234 goto Done;
235 }
236 ASSERT (SmmVariableHeader != NULL);
237
238 CopyGuid (&SmmVariableHeader->Guid, VendorGuid);
239 SmmVariableHeader->DataSize = TempDataSize;
240 SmmVariableHeader->NameSize = VariableNameSize;
241 if (Attributes == NULL) {
242 SmmVariableHeader->Attributes = 0;
243 } else {
244 SmmVariableHeader->Attributes = *Attributes;
245 }
246 CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);
247
248 //
249 // Send data to SMM.
250 //
251 Status = SendCommunicateBuffer (PayloadSize);
252
253 //
254 // Get data from SMM.
255 //
256 if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {
257 //
258 // SMM CommBuffer DataSize can be a trimed value
259 // Only update DataSize when needed
260 //
261 *DataSize = SmmVariableHeader->DataSize;
262 }
263 if (Attributes != NULL) {
264 *Attributes = SmmVariableHeader->Attributes;
265 }
266
267 if (EFI_ERROR (Status)) {
268 goto Done;
269 }
270
271 CopyMem (Data, (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize, SmmVariableHeader->DataSize);
272
273 Done:
274 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
275 return Status;
276 }
277
278
279 /**
280 This code Finds the Next available variable.
281
282 @param[in, out] VariableNameSize Size of the variable name.
283 @param[in, out] VariableName Pointer to variable name.
284 @param[in, out] VendorGuid Variable Vendor Guid.
285
286 @retval EFI_INVALID_PARAMETER Invalid parameter.
287 @retval EFI_SUCCESS Find the specified variable.
288 @retval EFI_NOT_FOUND Not found.
289 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.
290
291 **/
292 EFI_STATUS
293 EFIAPI
294 RuntimeServiceGetNextVariableName (
295 IN OUT UINTN *VariableNameSize,
296 IN OUT CHAR16 *VariableName,
297 IN OUT EFI_GUID *VendorGuid
298 )
299 {
300 EFI_STATUS Status;
301 UINTN PayloadSize;
302 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *SmmGetNextVariableName;
303 UINTN SmmCommBufPayloadSize;
304 UINTN OutVariableNameSize;
305 UINTN InVariableNameSize;
306
307 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
308 return EFI_INVALID_PARAMETER;
309 }
310
311 //
312 // SMM Communication Buffer max payload size
313 //
314 SmmCommBufPayloadSize = mVariableBufferSize - (SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE);
315 OutVariableNameSize = *VariableNameSize;
316 InVariableNameSize = StrSize (VariableName);
317
318 //
319 // If input string exceeds SMM payload limit. Return failure
320 //
321 if (InVariableNameSize > SmmCommBufPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {
322 return EFI_INVALID_PARAMETER;
323 }
324
325 AcquireLockOnlyAtBootTime(&mVariableServicesLock);
326
327 //
328 // Init the communicate buffer. The buffer data size is:
329 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
330 //
331 if (OutVariableNameSize > SmmCommBufPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {
332 //
333 // If output buffer exceed SMM payload limit. Trim output buffer to SMM payload size
334 //
335 OutVariableNameSize = SmmCommBufPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);
336 }
337 //
338 // Payload should be Guid + NameSize + MAX of Input & Output buffer
339 //
340 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + MAX (OutVariableNameSize, InVariableNameSize);
341
342
343 Status = InitCommunicateBuffer ((VOID **)&SmmGetNextVariableName, PayloadSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME);
344 if (EFI_ERROR (Status)) {
345 goto Done;
346 }
347 ASSERT (SmmGetNextVariableName != NULL);
348
349 //
350 // SMM comm buffer->NameSize is buffer size for return string
351 //
352 SmmGetNextVariableName->NameSize = OutVariableNameSize;
353
354 CopyGuid (&SmmGetNextVariableName->Guid, VendorGuid);
355 //
356 // Copy whole string
357 //
358 CopyMem (SmmGetNextVariableName->Name, VariableName, InVariableNameSize);
359 if (OutVariableNameSize > InVariableNameSize) {
360 ZeroMem ((UINT8 *) SmmGetNextVariableName->Name + InVariableNameSize, OutVariableNameSize - InVariableNameSize);
361 }
362
363 //
364 // Send data to SMM
365 //
366 Status = SendCommunicateBuffer (PayloadSize);
367
368 //
369 // Get data from SMM.
370 //
371 if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {
372 //
373 // SMM CommBuffer NameSize can be a trimed value
374 // Only update VariableNameSize when needed
375 //
376 *VariableNameSize = SmmGetNextVariableName->NameSize;
377 }
378 if (EFI_ERROR (Status)) {
379 goto Done;
380 }
381
382 CopyGuid (VendorGuid, &SmmGetNextVariableName->Guid);
383 CopyMem (VariableName, SmmGetNextVariableName->Name, SmmGetNextVariableName->NameSize);
384
385 Done:
386 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
387 return Status;
388 }
389
390 /**
391 This code sets variable in storage blocks (Volatile or Non-Volatile).
392
393 @param[in] VariableName Name of Variable to be found.
394 @param[in] VendorGuid Variable vendor GUID.
395 @param[in] Attributes Attribute value of the variable found
396 @param[in] DataSize Size of Data found. If size is less than the
397 data, this value contains the required size.
398 @param[in] Data Data pointer.
399
400 @retval EFI_INVALID_PARAMETER Invalid parameter.
401 @retval EFI_SUCCESS Set successfully.
402 @retval EFI_OUT_OF_RESOURCES Resource not enough to set variable.
403 @retval EFI_NOT_FOUND Not found.
404 @retval EFI_WRITE_PROTECTED Variable is read-only.
405
406 **/
407 EFI_STATUS
408 EFIAPI
409 RuntimeServiceSetVariable (
410 IN CHAR16 *VariableName,
411 IN EFI_GUID *VendorGuid,
412 IN UINT32 Attributes,
413 IN UINTN DataSize,
414 IN VOID *Data
415 )
416 {
417 EFI_STATUS Status;
418 UINTN PayloadSize;
419 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;
420 UINTN VariableNameSize;
421
422 //
423 // Check input parameters.
424 //
425 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
426 return EFI_INVALID_PARAMETER;
427 }
428
429 if (DataSize != 0 && Data == NULL) {
430 return EFI_INVALID_PARAMETER;
431 }
432
433 if (DataSize >= mVariableBufferSize) {
434 //
435 // DataSize may be near MAX_ADDRESS incorrectly, this can cause the computed PayLoadSize to
436 // overflow to a small value and pass the check in InitCommunicateBuffer().
437 // To protect against this vulnerability, return EFI_INVALID_PARAMETER if DataSize is >= mVariableBufferSize.
438 // And there will be further check to ensure the total size is also not > mVariableBufferSize.
439 //
440 return EFI_INVALID_PARAMETER;
441 }
442 VariableNameSize = StrSize (VariableName);
443
444 if ((UINTN)(~0) - VariableNameSize < OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + DataSize) {
445 //
446 // Prevent PayloadSize overflow
447 //
448 return EFI_INVALID_PARAMETER;
449 }
450
451 AcquireLockOnlyAtBootTime(&mVariableServicesLock);
452
453 //
454 // Init the communicate buffer. The buffer data size is:
455 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
456 //
457 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + VariableNameSize + DataSize;
458 Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_SET_VARIABLE);
459 if (EFI_ERROR (Status)) {
460 goto Done;
461 }
462 ASSERT (SmmVariableHeader != NULL);
463
464 CopyGuid ((EFI_GUID *) &SmmVariableHeader->Guid, VendorGuid);
465 SmmVariableHeader->DataSize = DataSize;
466 SmmVariableHeader->NameSize = VariableNameSize;
467 SmmVariableHeader->Attributes = Attributes;
468 CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);
469 CopyMem ((UINT8 *) SmmVariableHeader->Name + SmmVariableHeader->NameSize, Data, DataSize);
470
471 //
472 // Send data to SMM.
473 //
474 Status = SendCommunicateBuffer (PayloadSize);
475
476 Done:
477 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
478 return Status;
479 }
480
481
482 /**
483 This code returns information about the EFI variables.
484
485 @param[in] Attributes Attributes bitmask to specify the type of variables
486 on which to return information.
487 @param[out] MaximumVariableStorageSize Pointer to the maximum size of the storage space available
488 for the EFI variables associated with the attributes specified.
489 @param[out] RemainingVariableStorageSize Pointer to the remaining size of the storage space available
490 for EFI variables associated with the attributes specified.
491 @param[out] MaximumVariableSize Pointer to the maximum size of an individual EFI variables
492 associated with the attributes specified.
493
494 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
495 @retval EFI_SUCCESS Query successfully.
496 @retval EFI_UNSUPPORTED The attribute is not supported on this platform.
497
498 **/
499 EFI_STATUS
500 EFIAPI
501 RuntimeServiceQueryVariableInfo (
502 IN UINT32 Attributes,
503 OUT UINT64 *MaximumVariableStorageSize,
504 OUT UINT64 *RemainingVariableStorageSize,
505 OUT UINT64 *MaximumVariableSize
506 )
507 {
508 EFI_STATUS Status;
509 UINTN PayloadSize;
510 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *SmmQueryVariableInfo;
511
512 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {
513 return EFI_INVALID_PARAMETER;
514 }
515
516 AcquireLockOnlyAtBootTime(&mVariableServicesLock);
517
518 //
519 // Init the communicate buffer. The buffer data size is:
520 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize;
521 //
522 PayloadSize = sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO);
523 Status = InitCommunicateBuffer ((VOID **)&SmmQueryVariableInfo, PayloadSize, SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO);
524 if (EFI_ERROR (Status)) {
525 goto Done;
526 }
527 ASSERT (SmmQueryVariableInfo != NULL);
528
529 SmmQueryVariableInfo->Attributes = Attributes;
530
531 //
532 // Send data to SMM.
533 //
534 Status = SendCommunicateBuffer (PayloadSize);
535 if (EFI_ERROR (Status)) {
536 goto Done;
537 }
538
539 //
540 // Get data from SMM.
541 //
542 *MaximumVariableSize = SmmQueryVariableInfo->MaximumVariableSize;
543 *MaximumVariableStorageSize = SmmQueryVariableInfo->MaximumVariableStorageSize;
544 *RemainingVariableStorageSize = SmmQueryVariableInfo->RemainingVariableStorageSize;
545
546 Done:
547 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
548 return Status;
549 }
550
551
552 /**
553 Exit Boot Services Event notification handler.
554
555 Notify SMM variable driver about the event.
556
557 @param[in] Event Event whose notification function is being invoked.
558 @param[in] Context Pointer to the notification function's context.
559
560 **/
561 VOID
562 EFIAPI
563 OnExitBootServices (
564 IN EFI_EVENT Event,
565 IN VOID *Context
566 )
567 {
568 //
569 // Init the communicate buffer. The buffer data size is:
570 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
571 //
572 InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE);
573
574 //
575 // Send data to SMM.
576 //
577 SendCommunicateBuffer (0);
578 }
579
580
581 /**
582 On Ready To Boot Services Event notification handler.
583
584 Notify SMM variable driver about the event.
585
586 @param[in] Event Event whose notification function is being invoked
587 @param[in] Context Pointer to the notification function's context
588
589 **/
590 VOID
591 EFIAPI
592 OnReadyToBoot (
593 IN EFI_EVENT Event,
594 IN VOID *Context
595 )
596 {
597 //
598 // Init the communicate buffer. The buffer data size is:
599 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
600 //
601 InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_READY_TO_BOOT);
602
603 //
604 // Send data to SMM.
605 //
606 SendCommunicateBuffer (0);
607 }
608
609
610 /**
611 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
612
613 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
614 It convers pointer to new virtual address.
615
616 @param[in] Event Event whose notification function is being invoked.
617 @param[in] Context Pointer to the notification function's context.
618
619 **/
620 VOID
621 EFIAPI
622 VariableAddressChangeEvent (
623 IN EFI_EVENT Event,
624 IN VOID *Context
625 )
626 {
627 EfiConvertPointer (0x0, (VOID **) &mVariableBuffer);
628 EfiConvertPointer (0x0, (VOID **) &mSmmCommunication);
629 }
630
631
632 /**
633 Initialize variable service and install Variable Architectural protocol.
634
635 @param[in] Event Event whose notification function is being invoked.
636 @param[in] Context Pointer to the notification function's context.
637
638 **/
639 VOID
640 EFIAPI
641 SmmVariableReady (
642 IN EFI_EVENT Event,
643 IN VOID *Context
644 )
645 {
646 EFI_STATUS Status;
647
648 Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID **)&mSmmVariable);
649 if (EFI_ERROR (Status)) {
650 return;
651 }
652
653 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);
654 ASSERT_EFI_ERROR (Status);
655
656 //
657 // Allocate memory for variable store.
658 //
659 mVariableBufferSize = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;
660 mVariableBufferSize += MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));
661 mVariableBuffer = AllocateRuntimePool (mVariableBufferSize);
662 ASSERT (mVariableBuffer != NULL);
663
664 //
665 // Save the buffer physical address used for SMM conmunication.
666 //
667 mVariableBufferPhysical = mVariableBuffer;
668
669 gRT->GetVariable = RuntimeServiceGetVariable;
670 gRT->GetNextVariableName = RuntimeServiceGetNextVariableName;
671 gRT->SetVariable = RuntimeServiceSetVariable;
672 gRT->QueryVariableInfo = RuntimeServiceQueryVariableInfo;
673
674 //
675 // Install the Variable Architectural Protocol on a new handle.
676 //
677 Status = gBS->InstallProtocolInterface (
678 &mHandle,
679 &gEfiVariableArchProtocolGuid,
680 EFI_NATIVE_INTERFACE,
681 NULL
682 );
683 ASSERT_EFI_ERROR (Status);
684 }
685
686
687 /**
688 SMM Non-Volatile variable write service is ready notify event handler.
689
690 @param[in] Event Event whose notification function is being invoked.
691 @param[in] Context Pointer to the notification function's context.
692
693 **/
694 VOID
695 EFIAPI
696 SmmVariableWriteReady (
697 IN EFI_EVENT Event,
698 IN VOID *Context
699 )
700 {
701 EFI_STATUS Status;
702 VOID *ProtocolOps;
703
704 //
705 // Check whether the protocol is installed or not.
706 //
707 Status = gBS->LocateProtocol (&gSmmVariableWriteGuid, NULL, (VOID **) &ProtocolOps);
708 if (EFI_ERROR (Status)) {
709 return;
710 }
711
712 Status = gBS->InstallProtocolInterface (
713 &mHandle,
714 &gEfiVariableWriteArchProtocolGuid,
715 EFI_NATIVE_INTERFACE,
716 NULL
717 );
718 ASSERT_EFI_ERROR (Status);
719 }
720
721
722 /**
723 Variable Driver main entry point. The Variable driver places the 4 EFI
724 runtime services in the EFI System Table and installs arch protocols
725 for variable read and write services being available. It also registers
726 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
727
728 @param[in] ImageHandle The firmware allocated handle for the EFI image.
729 @param[in] SystemTable A pointer to the EFI System Table.
730
731 @retval EFI_SUCCESS Variable service successfully initialized.
732
733 **/
734 EFI_STATUS
735 EFIAPI
736 VariableSmmRuntimeInitialize (
737 IN EFI_HANDLE ImageHandle,
738 IN EFI_SYSTEM_TABLE *SystemTable
739 )
740 {
741 VOID *SmmVariableRegistration;
742 VOID *SmmVariableWriteRegistration;
743 EFI_EVENT OnReadyToBootEvent;
744 EFI_EVENT ExitBootServiceEvent;
745
746 EfiInitializeLock (&mVariableServicesLock, TPL_NOTIFY);
747
748 //
749 // Smm variable service is ready
750 //
751 EfiCreateProtocolNotifyEvent (
752 &gEfiSmmVariableProtocolGuid,
753 TPL_CALLBACK,
754 SmmVariableReady,
755 NULL,
756 &SmmVariableRegistration
757 );
758
759 //
760 // Smm Non-Volatile variable write service is ready
761 //
762 EfiCreateProtocolNotifyEvent (
763 &gSmmVariableWriteGuid,
764 TPL_CALLBACK,
765 SmmVariableWriteReady,
766 NULL,
767 &SmmVariableWriteRegistration
768 );
769
770 //
771 // Register the event to reclaim variable for OS usage.
772 //
773 EfiCreateEventReadyToBootEx (
774 TPL_NOTIFY,
775 OnReadyToBoot,
776 NULL,
777 &OnReadyToBootEvent
778 );
779
780 //
781 // Register the event to inform SMM variable that it is at runtime.
782 //
783 gBS->CreateEventEx (
784 EVT_NOTIFY_SIGNAL,
785 TPL_NOTIFY,
786 OnExitBootServices,
787 NULL,
788 &gEfiEventExitBootServicesGuid,
789 &ExitBootServiceEvent
790 );
791
792 //
793 // Register the event to convert the pointer for runtime.
794 //
795 gBS->CreateEventEx (
796 EVT_NOTIFY_SIGNAL,
797 TPL_NOTIFY,
798 VariableAddressChangeEvent,
799 NULL,
800 &gEfiEventVirtualAddressChangeGuid,
801 &mVirtualAddressChangeEvent
802 );
803
804 return EFI_SUCCESS;
805 }
806