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