]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
Return EFI_UNSUPPORTED if READY_TO_BOOT function is invoked at SMM runtime.
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / VariableSmm.c
1 /** @file
2
3 The sample implementation for SMM variable protocol. And this driver
4 implements an SMI handler to communicate with the DXE runtime driver
5 to provide variable services.
6
7 Caution: This module requires additional review when modified.
8 This driver will have external input - variable data and communicate buffer in SMM mode.
9 This external input must be validated carefully to avoid security issue like
10 buffer overflow, integer overflow.
11
12 SmmVariableHandler() will receive untrusted input and do basic validation.
13
14 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),
15 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),
16 SmmVariableGetStatistics() should also do validation based on its own knowledge.
17
18 Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>
19 This program and the accompanying materials
20 are licensed and made available under the terms and conditions of the BSD License
21 which accompanies this distribution. The full text of the license may be found at
22 http://opensource.org/licenses/bsd-license.php
23
24 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
25 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
26
27 **/
28 #include <Protocol/SmmVariable.h>
29 #include <Protocol/SmmFirmwareVolumeBlock.h>
30 #include <Protocol/SmmFaultTolerantWrite.h>
31 #include <Protocol/SmmAccess2.h>
32
33 #include <Library/SmmServicesTableLib.h>
34
35 #include <Guid/VariableFormat.h>
36 #include <Guid/SmmVariableCommon.h>
37 #include "Variable.h"
38
39 EFI_SMRAM_DESCRIPTOR *mSmramRanges;
40 UINTN mSmramRangeCount;
41
42 extern VARIABLE_INFO_ENTRY *gVariableInfo;
43 EFI_HANDLE mSmmVariableHandle = NULL;
44 EFI_HANDLE mVariableHandle = NULL;
45 BOOLEAN mAtRuntime = FALSE;
46 EFI_GUID mZeroGuid = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};
47
48 EFI_SMM_VARIABLE_PROTOCOL gSmmVariable = {
49 VariableServiceGetVariable,
50 VariableServiceGetNextVariableName,
51 VariableServiceSetVariable,
52 VariableServiceQueryVariableInfo
53 };
54
55
56 /**
57 Return TRUE if ExitBootServices () has been called.
58
59 @retval TRUE If ExitBootServices () has been called.
60 **/
61 BOOLEAN
62 AtRuntime (
63 VOID
64 )
65 {
66 return mAtRuntime;
67 }
68
69 /**
70 This function check if the address is in SMRAM.
71
72 @param Buffer the buffer address to be checked.
73 @param Length the buffer length to be checked.
74
75 @retval TRUE this address is in SMRAM.
76 @retval FALSE this address is NOT in SMRAM.
77 **/
78 BOOLEAN
79 InternalIsAddressInSmram (
80 IN EFI_PHYSICAL_ADDRESS Buffer,
81 IN UINT64 Length
82 )
83 {
84 UINTN Index;
85
86 for (Index = 0; Index < mSmramRangeCount; Index ++) {
87 if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) ||
88 ((mSmramRanges[Index].CpuStart >= Buffer) && (mSmramRanges[Index].CpuStart < Buffer + Length))) {
89 return TRUE;
90 }
91 }
92
93 return FALSE;
94 }
95
96
97 /**
98 Initializes a basic mutual exclusion lock.
99
100 This function initializes a basic mutual exclusion lock to the released state
101 and returns the lock. Each lock provides mutual exclusion access at its task
102 priority level. Since there is no preemption or multiprocessor support in EFI,
103 acquiring the lock only consists of raising to the locks TPL.
104 If Lock is NULL, then ASSERT().
105 If Priority is not a valid TPL value, then ASSERT().
106
107 @param Lock A pointer to the lock data structure to initialize.
108 @param Priority EFI TPL is associated with the lock.
109
110 @return The lock.
111
112 **/
113 EFI_LOCK *
114 InitializeLock (
115 IN OUT EFI_LOCK *Lock,
116 IN EFI_TPL Priority
117 )
118 {
119 return Lock;
120 }
121
122 /**
123 Acquires lock only at boot time. Simply returns at runtime.
124
125 This is a temperary function that will be removed when
126 EfiAcquireLock() in UefiLib can handle the call in UEFI
127 Runtimer driver in RT phase.
128 It calls EfiAcquireLock() at boot time, and simply returns
129 at runtime.
130
131 @param Lock A pointer to the lock to acquire.
132
133 **/
134 VOID
135 AcquireLockOnlyAtBootTime (
136 IN EFI_LOCK *Lock
137 )
138 {
139
140 }
141
142
143 /**
144 Releases lock only at boot time. Simply returns at runtime.
145
146 This is a temperary function which will be removed when
147 EfiReleaseLock() in UefiLib can handle the call in UEFI
148 Runtimer driver in RT phase.
149 It calls EfiReleaseLock() at boot time and simply returns
150 at runtime.
151
152 @param Lock A pointer to the lock to release.
153
154 **/
155 VOID
156 ReleaseLockOnlyAtBootTime (
157 IN EFI_LOCK *Lock
158 )
159 {
160
161 }
162
163 /**
164 Retrive the SMM Fault Tolerent Write protocol interface.
165
166 @param[out] FtwProtocol The interface of SMM Ftw protocol
167
168 @retval EFI_SUCCESS The SMM FTW protocol instance was found and returned in FtwProtocol.
169 @retval EFI_NOT_FOUND The SMM FTW protocol instance was not found.
170 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
171
172 **/
173 EFI_STATUS
174 GetFtwProtocol (
175 OUT VOID **FtwProtocol
176 )
177 {
178 EFI_STATUS Status;
179
180 //
181 // Locate Smm Fault Tolerent Write protocol
182 //
183 Status = gSmst->SmmLocateProtocol (
184 &gEfiSmmFaultTolerantWriteProtocolGuid,
185 NULL,
186 FtwProtocol
187 );
188 return Status;
189 }
190
191
192 /**
193 Retrive the SMM FVB protocol interface by HANDLE.
194
195 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for
196 reading, writing, and erasing the target block.
197 @param[out] FvBlock The interface of SMM FVB protocol
198
199 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
200 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.
201 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
202
203 **/
204 EFI_STATUS
205 GetFvbByHandle (
206 IN EFI_HANDLE FvBlockHandle,
207 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
208 )
209 {
210 //
211 // To get the SMM FVB protocol interface on the handle
212 //
213 return gSmst->SmmHandleProtocol (
214 FvBlockHandle,
215 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
216 (VOID **) FvBlock
217 );
218 }
219
220
221 /**
222 Function returns an array of handles that support the SMM FVB protocol
223 in a buffer allocated from pool.
224
225 @param[out] NumberHandles The number of handles returned in Buffer.
226 @param[out] Buffer A pointer to the buffer to return the requested
227 array of handles that support SMM FVB protocol.
228
229 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
230 handles in Buffer was returned in NumberHandles.
231 @retval EFI_NOT_FOUND No SMM FVB handle was found.
232 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
233 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
234
235 **/
236 EFI_STATUS
237 GetFvbCountAndBuffer (
238 OUT UINTN *NumberHandles,
239 OUT EFI_HANDLE **Buffer
240 )
241 {
242 EFI_STATUS Status;
243 UINTN BufferSize;
244
245 if ((NumberHandles == NULL) || (Buffer == NULL)) {
246 return EFI_INVALID_PARAMETER;
247 }
248
249 BufferSize = 0;
250 *NumberHandles = 0;
251 *Buffer = NULL;
252 Status = gSmst->SmmLocateHandle (
253 ByProtocol,
254 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
255 NULL,
256 &BufferSize,
257 *Buffer
258 );
259 if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {
260 return EFI_NOT_FOUND;
261 }
262
263 *Buffer = AllocatePool (BufferSize);
264 if (*Buffer == NULL) {
265 return EFI_OUT_OF_RESOURCES;
266 }
267
268 Status = gSmst->SmmLocateHandle (
269 ByProtocol,
270 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
271 NULL,
272 &BufferSize,
273 *Buffer
274 );
275
276 *NumberHandles = BufferSize / sizeof(EFI_HANDLE);
277 if (EFI_ERROR(Status)) {
278 *NumberHandles = 0;
279 }
280
281 return Status;
282 }
283
284
285 /**
286 Get the variable statistics information from the information buffer pointed by gVariableInfo.
287
288 Caution: This function may be invoked at SMM runtime.
289 InfoEntry and InfoSize are external input. Care must be taken to make sure not security issue at runtime.
290
291 @param[in, out] InfoEntry A pointer to the buffer of variable information entry.
292 On input, point to the variable information returned last time. if
293 InfoEntry->VendorGuid is zero, return the first information.
294 On output, point to the next variable information.
295 @param[in, out] InfoSize On input, the size of the variable information buffer.
296 On output, the returned variable information size.
297
298 @retval EFI_SUCCESS The variable information is found and returned successfully.
299 @retval EFI_UNSUPPORTED No variable inoformation exists in variable driver. The
300 PcdVariableCollectStatistics should be set TRUE to support it.
301 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the next variable information.
302
303 **/
304 EFI_STATUS
305 SmmVariableGetStatistics (
306 IN OUT VARIABLE_INFO_ENTRY *InfoEntry,
307 IN OUT UINTN *InfoSize
308 )
309 {
310 VARIABLE_INFO_ENTRY *VariableInfo;
311 UINTN NameLength;
312 UINTN StatisticsInfoSize;
313 CHAR16 *InfoName;
314
315 ASSERT (InfoEntry != NULL);
316 VariableInfo = gVariableInfo;
317 if (VariableInfo == NULL) {
318 return EFI_UNSUPPORTED;
319 }
320
321 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);
322 if (*InfoSize < StatisticsInfoSize) {
323 *InfoSize = StatisticsInfoSize;
324 return EFI_BUFFER_TOO_SMALL;
325 }
326 InfoName = (CHAR16 *)(InfoEntry + 1);
327
328 if (CompareGuid (&InfoEntry->VendorGuid, &mZeroGuid)) {
329 //
330 // Return the first variable info
331 //
332 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));
333 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));
334 *InfoSize = StatisticsInfoSize;
335 return EFI_SUCCESS;
336 }
337
338 //
339 // Get the next variable info
340 //
341 while (VariableInfo != NULL) {
342 if (CompareGuid (&VariableInfo->VendorGuid, &InfoEntry->VendorGuid)) {
343 NameLength = StrSize (VariableInfo->Name);
344 if (NameLength == StrSize (InfoName)) {
345 if (CompareMem (VariableInfo->Name, InfoName, NameLength) == 0) {
346 //
347 // Find the match one
348 //
349 VariableInfo = VariableInfo->Next;
350 break;
351 }
352 }
353 }
354 VariableInfo = VariableInfo->Next;
355 };
356
357 if (VariableInfo == NULL) {
358 *InfoSize = 0;
359 return EFI_SUCCESS;
360 }
361
362 //
363 // Output the new variable info
364 //
365 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);
366 if (*InfoSize < StatisticsInfoSize) {
367 *InfoSize = StatisticsInfoSize;
368 return EFI_BUFFER_TOO_SMALL;
369 }
370
371 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));
372 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));
373 *InfoSize = StatisticsInfoSize;
374
375 return EFI_SUCCESS;
376 }
377
378
379 /**
380 Communication service SMI Handler entry.
381
382 This SMI handler provides services for the variable wrapper driver.
383
384 Caution: This function may receive untrusted input.
385 This variable data and communicate buffer are external input, so this function will do basic validation.
386 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),
387 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),
388 SmmVariableGetStatistics() should also do validation based on its own knowledge.
389
390 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
391 @param[in] RegisterContext Points to an optional handler context which was specified when the
392 handler was registered.
393 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
394 be conveyed from a non-SMM environment into an SMM environment.
395 @param[in, out] CommBufferSize The size of the CommBuffer.
396
397 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
398 should still be called.
399 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
400 still be called.
401 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
402 be called.
403 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
404 **/
405 EFI_STATUS
406 EFIAPI
407 SmmVariableHandler (
408 IN EFI_HANDLE DispatchHandle,
409 IN CONST VOID *RegisterContext,
410 IN OUT VOID *CommBuffer,
411 IN OUT UINTN *CommBufferSize
412 )
413 {
414 EFI_STATUS Status;
415 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;
416 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;
417 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *GetNextVariableName;
418 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *QueryVariableInfo;
419 VARIABLE_INFO_ENTRY *VariableInfo;
420 UINTN InfoSize;
421
422 //
423 // If input is invalid, stop processing this SMI
424 //
425 if (CommBuffer == NULL || CommBufferSize == NULL) {
426 return EFI_SUCCESS;
427 }
428
429 if (*CommBufferSize < sizeof(SMM_VARIABLE_COMMUNICATE_HEADER) - 1) {
430 return EFI_SUCCESS;
431 }
432
433 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBuffer, *CommBufferSize)) {
434 DEBUG ((EFI_D_ERROR, "SMM communication buffer size is in SMRAM!\n"));
435 return EFI_SUCCESS;
436 }
437
438 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)CommBuffer;
439 switch (SmmVariableFunctionHeader->Function) {
440 case SMM_VARIABLE_FUNCTION_GET_VARIABLE:
441 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) SmmVariableFunctionHeader->Data;
442 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)
443 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;
444
445 //
446 // SMRAM range check already covered before
447 //
448 if (InfoSize > *CommBufferSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data)) {
449 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));
450 Status = EFI_ACCESS_DENIED;
451 goto EXIT;
452 }
453
454 Status = VariableServiceGetVariable (
455 SmmVariableHeader->Name,
456 &SmmVariableHeader->Guid,
457 &SmmVariableHeader->Attributes,
458 &SmmVariableHeader->DataSize,
459 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize
460 );
461 break;
462
463 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME:
464 GetNextVariableName = (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *) SmmVariableFunctionHeader->Data;
465 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + GetNextVariableName->NameSize;
466
467 //
468 // SMRAM range check already covered before
469 //
470 if (InfoSize > *CommBufferSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data)) {
471 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));
472 Status = EFI_ACCESS_DENIED;
473 goto EXIT;
474 }
475
476 Status = VariableServiceGetNextVariableName (
477 &GetNextVariableName->NameSize,
478 GetNextVariableName->Name,
479 &GetNextVariableName->Guid
480 );
481 break;
482
483 case SMM_VARIABLE_FUNCTION_SET_VARIABLE:
484 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) SmmVariableFunctionHeader->Data;
485 Status = VariableServiceSetVariable (
486 SmmVariableHeader->Name,
487 &SmmVariableHeader->Guid,
488 SmmVariableHeader->Attributes,
489 SmmVariableHeader->DataSize,
490 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize
491 );
492 break;
493
494 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO:
495 QueryVariableInfo = (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *) SmmVariableFunctionHeader->Data;
496 InfoSize = sizeof(SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO);
497
498 //
499 // SMRAM range check already covered before
500 //
501 if (InfoSize > *CommBufferSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data)) {
502 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));
503 Status = EFI_ACCESS_DENIED;
504 goto EXIT;
505 }
506
507 Status = VariableServiceQueryVariableInfo (
508 QueryVariableInfo->Attributes,
509 &QueryVariableInfo->MaximumVariableStorageSize,
510 &QueryVariableInfo->RemainingVariableStorageSize,
511 &QueryVariableInfo->MaximumVariableSize
512 );
513 break;
514
515 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT:
516 if (AtRuntime()) {
517 Status = EFI_UNSUPPORTED;
518 break;
519 }
520 ReclaimForOS ();
521 Status = EFI_SUCCESS;
522 break;
523
524 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE:
525 mAtRuntime = TRUE;
526 Status = EFI_SUCCESS;
527 break;
528
529 case SMM_VARIABLE_FUNCTION_GET_STATISTICS:
530 VariableInfo = (VARIABLE_INFO_ENTRY *) SmmVariableFunctionHeader->Data;
531 InfoSize = *CommBufferSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data);
532
533 //
534 // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here.
535 // It is covered by previous CommBuffer check
536 //
537
538 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBufferSize, sizeof(UINTN))) {
539 DEBUG ((EFI_D_ERROR, "SMM communication buffer size is in SMRAM!\n"));
540 Status = EFI_ACCESS_DENIED;
541 goto EXIT;
542 }
543
544 Status = SmmVariableGetStatistics (VariableInfo, &InfoSize);
545 *CommBufferSize = InfoSize + OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data);
546 break;
547
548 default:
549 Status = EFI_UNSUPPORTED;
550 }
551
552 EXIT:
553
554 SmmVariableFunctionHeader->ReturnStatus = Status;
555
556 return EFI_SUCCESS;
557 }
558
559
560 /**
561 SMM Fault Tolerant Write protocol notification event handler.
562
563 Non-Volatile variable write may needs FTW protocol to reclaim when
564 writting variable.
565
566 @param Protocol Points to the protocol's unique identifier
567 @param Interface Points to the interface instance
568 @param Handle The handle on which the interface was installed
569
570 @retval EFI_SUCCESS SmmEventCallback runs successfully
571 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.
572
573 **/
574 EFI_STATUS
575 EFIAPI
576 SmmFtwNotificationEvent (
577 IN CONST EFI_GUID *Protocol,
578 IN VOID *Interface,
579 IN EFI_HANDLE Handle
580 )
581 {
582 EFI_STATUS Status;
583 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;
584 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;
585 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;
586
587 if (mVariableModuleGlobal->FvbInstance != NULL) {
588 return EFI_SUCCESS;
589 }
590
591 //
592 // Ensure SMM FTW protocol is installed.
593 //
594 Status = GetFtwProtocol ((VOID **)&FtwProtocol);
595 if (EFI_ERROR (Status)) {
596 return Status;
597 }
598
599 //
600 // Find the proper FVB protocol for variable.
601 //
602 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);
603 if (NvStorageVariableBase == 0) {
604 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);
605 }
606 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);
607 if (EFI_ERROR (Status)) {
608 return EFI_NOT_FOUND;
609 }
610
611 mVariableModuleGlobal->FvbInstance = FvbProtocol;
612
613 Status = VariableWriteServiceInitialize ();
614 ASSERT_EFI_ERROR (Status);
615
616 //
617 // Notify the variable wrapper driver the variable write service is ready
618 //
619 Status = gBS->InstallProtocolInterface (
620 &mSmmVariableHandle,
621 &gSmmVariableWriteGuid,
622 EFI_NATIVE_INTERFACE,
623 NULL
624 );
625 ASSERT_EFI_ERROR (Status);
626
627 return EFI_SUCCESS;
628 }
629
630
631 /**
632 Variable Driver main entry point. The Variable driver places the 4 EFI
633 runtime services in the EFI System Table and installs arch protocols
634 for variable read and write services being available. It also registers
635 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
636
637 @param[in] ImageHandle The firmware allocated handle for the EFI image.
638 @param[in] SystemTable A pointer to the EFI System Table.
639
640 @retval EFI_SUCCESS Variable service successfully initialized.
641
642 **/
643 EFI_STATUS
644 EFIAPI
645 VariableServiceInitialize (
646 IN EFI_HANDLE ImageHandle,
647 IN EFI_SYSTEM_TABLE *SystemTable
648 )
649 {
650 EFI_STATUS Status;
651 EFI_HANDLE VariableHandle;
652 VOID *SmmFtwRegistration;
653 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;
654 UINTN Size;
655
656 //
657 // Variable initialize.
658 //
659 Status = VariableCommonInitialize ();
660 ASSERT_EFI_ERROR (Status);
661
662 //
663 // Install the Smm Variable Protocol on a new handle.
664 //
665 VariableHandle = NULL;
666 Status = gSmst->SmmInstallProtocolInterface (
667 &VariableHandle,
668 &gEfiSmmVariableProtocolGuid,
669 EFI_NATIVE_INTERFACE,
670 &gSmmVariable
671 );
672 ASSERT_EFI_ERROR (Status);
673
674 //
675 // Get SMRAM information
676 //
677 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);
678 ASSERT_EFI_ERROR (Status);
679
680 Size = 0;
681 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);
682 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
683
684 Status = gSmst->SmmAllocatePool (
685 EfiRuntimeServicesData,
686 Size,
687 (VOID **)&mSmramRanges
688 );
689 ASSERT_EFI_ERROR (Status);
690
691 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);
692 ASSERT_EFI_ERROR (Status);
693
694 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
695
696 ///
697 /// Register SMM variable SMI handler
698 ///
699 VariableHandle = NULL;
700 Status = gSmst->SmiHandlerRegister (SmmVariableHandler, &gEfiSmmVariableProtocolGuid, &VariableHandle);
701 ASSERT_EFI_ERROR (Status);
702
703 //
704 // Notify the variable wrapper driver the variable service is ready
705 //
706 Status = SystemTable->BootServices->InstallProtocolInterface (
707 &mVariableHandle,
708 &gEfiSmmVariableProtocolGuid,
709 EFI_NATIVE_INTERFACE,
710 &gSmmVariable
711 );
712 ASSERT_EFI_ERROR (Status);
713
714 //
715 // Register FtwNotificationEvent () notify function.
716 //
717 Status = gSmst->SmmRegisterProtocolNotify (
718 &gEfiSmmFaultTolerantWriteProtocolGuid,
719 SmmFtwNotificationEvent,
720 &SmmFtwRegistration
721 );
722 ASSERT_EFI_ERROR (Status);
723
724 SmmFtwNotificationEvent (NULL, NULL, NULL);
725
726 return EFI_SUCCESS;
727 }
728
729