]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.c
Fix a potential SMM memory dump issue. If pass communication buffer with DataBuffer...
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / RuntimeDxe / VariableSmm.c
1 /** @file
2 The sample implementation for SMM variable protocol. And this driver
3 implements an SMI handler to communicate with the DXE runtime driver
4 to provide variable services.
5
6 Caution: This module requires additional review when modified.
7 This driver will have external input - variable data and communicate buffer in SMM mode.
8 This external input must be validated carefully to avoid security issue like
9 buffer overflow, integer overflow.
10
11 SmmVariableHandler() will receive untrusted input and do basic validation.
12
13 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),
14 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),
15 SmmVariableGetStatistics() should also do validation based on its own knowledge.
16
17 Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>
18 This program and the accompanying materials
19 are licensed and made available under the terms and conditions of the BSD License
20 which accompanies this distribution. The full text of the license may be found at
21 http://opensource.org/licenses/bsd-license.php
22
23 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
24 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
25
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/AuthenticatedVariableFormat.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 @retval EFI_INVALID_PARAMETER Input parameter is invalid.
303
304 **/
305 EFI_STATUS
306 SmmVariableGetStatistics (
307 IN OUT VARIABLE_INFO_ENTRY *InfoEntry,
308 IN OUT UINTN *InfoSize
309 )
310 {
311 VARIABLE_INFO_ENTRY *VariableInfo;
312 UINTN NameLength;
313 UINTN StatisticsInfoSize;
314 CHAR16 *InfoName;
315
316 if (InfoEntry == NULL) {
317 return EFI_INVALID_PARAMETER;
318 }
319
320 VariableInfo = gVariableInfo;
321 if (VariableInfo == NULL) {
322 return EFI_UNSUPPORTED;
323 }
324
325 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);
326 if (*InfoSize < StatisticsInfoSize) {
327 *InfoSize = StatisticsInfoSize;
328 return EFI_BUFFER_TOO_SMALL;
329 }
330 InfoName = (CHAR16 *)(InfoEntry + 1);
331
332 if (CompareGuid (&InfoEntry->VendorGuid, &mZeroGuid)) {
333 //
334 // Return the first variable info
335 //
336 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));
337 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));
338 *InfoSize = StatisticsInfoSize;
339 return EFI_SUCCESS;
340 }
341
342 //
343 // Get the next variable info
344 //
345 while (VariableInfo != NULL) {
346 if (CompareGuid (&VariableInfo->VendorGuid, &InfoEntry->VendorGuid)) {
347 NameLength = StrSize (VariableInfo->Name);
348 if (NameLength == StrSize (InfoName)) {
349 if (CompareMem (VariableInfo->Name, InfoName, NameLength) == 0) {
350 //
351 // Find the match one
352 //
353 VariableInfo = VariableInfo->Next;
354 break;
355 }
356 }
357 }
358 VariableInfo = VariableInfo->Next;
359 };
360
361 if (VariableInfo == NULL) {
362 *InfoSize = 0;
363 return EFI_SUCCESS;
364 }
365
366 //
367 // Output the new variable info
368 //
369 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);
370 if (*InfoSize < StatisticsInfoSize) {
371 *InfoSize = StatisticsInfoSize;
372 return EFI_BUFFER_TOO_SMALL;
373 }
374
375 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));
376 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));
377 *InfoSize = StatisticsInfoSize;
378
379 return EFI_SUCCESS;
380 }
381
382
383 /**
384 Communication service SMI Handler entry.
385
386 This SMI handler provides services for the variable wrapper driver.
387
388 Caution: This function may receive untrusted input.
389 This variable data and communicate buffer are external input, so this function will do basic validation.
390 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),
391 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),
392 SmmVariableGetStatistics() should also do validation based on its own knowledge.
393
394 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
395 @param[in] RegisterContext Points to an optional handler context which was specified when the
396 handler was registered.
397 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
398 be conveyed from a non-SMM environment into an SMM environment.
399 @param[in, out] CommBufferSize The size of the CommBuffer.
400
401 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
402 should still be called.
403 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
404 still be called.
405 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
406 be called.
407 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
408
409 **/
410 EFI_STATUS
411 EFIAPI
412 SmmVariableHandler (
413 IN EFI_HANDLE DispatchHandle,
414 IN CONST VOID *RegisterContext,
415 IN OUT VOID *CommBuffer,
416 IN OUT UINTN *CommBufferSize
417 )
418 {
419 EFI_STATUS Status;
420 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;
421 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;
422 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *GetNextVariableName;
423 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *QueryVariableInfo;
424 VARIABLE_INFO_ENTRY *VariableInfo;
425 UINTN InfoSize;
426
427 //
428 // If input is invalid, stop processing this SMI
429 //
430 if (CommBuffer == NULL || CommBufferSize == NULL) {
431 return EFI_SUCCESS;
432 }
433
434 if (*CommBufferSize < SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
435 return EFI_SUCCESS;
436 }
437
438 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBuffer, *CommBufferSize)) {
439 DEBUG ((EFI_D_ERROR, "SMM communication buffer size is in SMRAM!\n"));
440 return EFI_SUCCESS;
441 }
442
443 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)CommBuffer;
444
445 switch (SmmVariableFunctionHeader->Function) {
446 case SMM_VARIABLE_FUNCTION_GET_VARIABLE:
447 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) SmmVariableFunctionHeader->Data;
448 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)
449 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;
450
451 //
452 // SMRAM range check already covered before
453 //
454 if (InfoSize > *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
455 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));
456 Status = EFI_ACCESS_DENIED;
457 goto EXIT;
458 }
459
460 Status = VariableServiceGetVariable (
461 SmmVariableHeader->Name,
462 &SmmVariableHeader->Guid,
463 &SmmVariableHeader->Attributes,
464 &SmmVariableHeader->DataSize,
465 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize
466 );
467 break;
468
469 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME:
470 GetNextVariableName = (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *) SmmVariableFunctionHeader->Data;
471 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + GetNextVariableName->NameSize;
472
473 //
474 // SMRAM range check already covered before
475 //
476 if (InfoSize > *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
477 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));
478 Status = EFI_ACCESS_DENIED;
479 goto EXIT;
480 }
481
482 Status = VariableServiceGetNextVariableName (
483 &GetNextVariableName->NameSize,
484 GetNextVariableName->Name,
485 &GetNextVariableName->Guid
486 );
487 break;
488
489 case SMM_VARIABLE_FUNCTION_SET_VARIABLE:
490 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) SmmVariableFunctionHeader->Data;
491 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)
492 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;
493
494 //
495 // SMRAM range check already covered before
496 // Data buffer should not contain SMM range
497 //
498 if (InfoSize > *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
499 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));
500 Status = EFI_ACCESS_DENIED;
501 goto EXIT;
502 }
503
504 Status = VariableServiceSetVariable (
505 SmmVariableHeader->Name,
506 &SmmVariableHeader->Guid,
507 SmmVariableHeader->Attributes,
508 SmmVariableHeader->DataSize,
509 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize
510 );
511 break;
512
513 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO:
514 QueryVariableInfo = (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *) SmmVariableFunctionHeader->Data;
515 InfoSize = sizeof(SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO);
516
517 //
518 // SMRAM range check already covered before
519 //
520 if (InfoSize > *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
521 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));
522 Status = EFI_ACCESS_DENIED;
523 goto EXIT;
524 }
525
526 Status = VariableServiceQueryVariableInfo (
527 QueryVariableInfo->Attributes,
528 &QueryVariableInfo->MaximumVariableStorageSize,
529 &QueryVariableInfo->RemainingVariableStorageSize,
530 &QueryVariableInfo->MaximumVariableSize
531 );
532 break;
533
534 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT:
535 if (AtRuntime()) {
536 Status = EFI_UNSUPPORTED;
537 break;
538 }
539 ReclaimForOS ();
540 Status = EFI_SUCCESS;
541 break;
542
543 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE:
544 mAtRuntime = TRUE;
545 Status = EFI_SUCCESS;
546 break;
547
548 case SMM_VARIABLE_FUNCTION_GET_STATISTICS:
549 VariableInfo = (VARIABLE_INFO_ENTRY *) SmmVariableFunctionHeader->Data;
550 InfoSize = *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;
551
552 //
553 // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here.
554 // It is covered by previous CommBuffer check
555 //
556
557 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBufferSize, sizeof(UINTN))) {
558 DEBUG ((EFI_D_ERROR, "SMM communication buffer size is in SMRAM!\n"));
559 Status = EFI_ACCESS_DENIED;
560 goto EXIT;
561 }
562
563 Status = SmmVariableGetStatistics (VariableInfo, &InfoSize);
564 *CommBufferSize = InfoSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;
565 break;
566
567 default:
568 Status = EFI_UNSUPPORTED;
569 }
570
571 EXIT:
572
573 SmmVariableFunctionHeader->ReturnStatus = Status;
574 return EFI_SUCCESS;
575 }
576
577
578 /**
579 SMM Fault Tolerant Write protocol notification event handler.
580
581 Non-Volatile variable write may needs FTW protocol to reclaim when
582 writting variable.
583
584 @param Protocol Points to the protocol's unique identifier
585 @param Interface Points to the interface instance
586 @param Handle The handle on which the interface was installed
587
588 @retval EFI_SUCCESS SmmEventCallback runs successfully
589 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.
590
591 **/
592 EFI_STATUS
593 EFIAPI
594 SmmFtwNotificationEvent (
595 IN CONST EFI_GUID *Protocol,
596 IN VOID *Interface,
597 IN EFI_HANDLE Handle
598 )
599 {
600 EFI_STATUS Status;
601 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;
602 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;
603 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;
604
605 if (mVariableModuleGlobal->FvbInstance != NULL) {
606 return EFI_SUCCESS;
607 }
608
609 //
610 // Ensure SMM FTW protocol is installed.
611 //
612 Status = GetFtwProtocol ((VOID **)&FtwProtocol);
613 if (EFI_ERROR (Status)) {
614 return Status;
615 }
616
617 //
618 // Find the proper FVB protocol for variable.
619 //
620 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);
621 if (NvStorageVariableBase == 0) {
622 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);
623 }
624 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);
625 if (EFI_ERROR (Status)) {
626 return EFI_NOT_FOUND;
627 }
628
629 mVariableModuleGlobal->FvbInstance = FvbProtocol;
630
631 Status = VariableWriteServiceInitialize ();
632 ASSERT_EFI_ERROR (Status);
633
634 //
635 // Notify the variable wrapper driver the variable write service is ready
636 //
637 Status = gBS->InstallProtocolInterface (
638 &mSmmVariableHandle,
639 &gSmmVariableWriteGuid,
640 EFI_NATIVE_INTERFACE,
641 NULL
642 );
643 ASSERT_EFI_ERROR (Status);
644
645 return EFI_SUCCESS;
646 }
647
648
649 /**
650 Variable Driver main entry point. The Variable driver places the 4 EFI
651 runtime services in the EFI System Table and installs arch protocols
652 for variable read and write services being available. It also registers
653 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
654
655 @param[in] ImageHandle The firmware allocated handle for the EFI image.
656 @param[in] SystemTable A pointer to the EFI System Table.
657
658 @retval EFI_SUCCESS Variable service successfully initialized.
659
660 **/
661 EFI_STATUS
662 EFIAPI
663 VariableServiceInitialize (
664 IN EFI_HANDLE ImageHandle,
665 IN EFI_SYSTEM_TABLE *SystemTable
666 )
667 {
668 EFI_STATUS Status;
669 EFI_HANDLE VariableHandle;
670 VOID *SmmFtwRegistration;
671 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;
672 UINTN Size;
673
674 //
675 // Variable initialize.
676 //
677 Status = VariableCommonInitialize ();
678 ASSERT_EFI_ERROR (Status);
679
680 //
681 // Install the Smm Variable Protocol on a new handle.
682 //
683 VariableHandle = NULL;
684 Status = gSmst->SmmInstallProtocolInterface (
685 &VariableHandle,
686 &gEfiSmmVariableProtocolGuid,
687 EFI_NATIVE_INTERFACE,
688 &gSmmVariable
689 );
690 ASSERT_EFI_ERROR (Status);
691
692 //
693 // Get SMRAM information
694 //
695 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);
696 ASSERT_EFI_ERROR (Status);
697
698 Size = 0;
699 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);
700 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
701
702 Status = gSmst->SmmAllocatePool (
703 EfiRuntimeServicesData,
704 Size,
705 (VOID **)&mSmramRanges
706 );
707 ASSERT_EFI_ERROR (Status);
708
709 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);
710 ASSERT_EFI_ERROR (Status);
711
712 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
713
714 ///
715 /// Register SMM variable SMI handler
716 ///
717 VariableHandle = NULL;
718 Status = gSmst->SmiHandlerRegister (SmmVariableHandler, &gEfiSmmVariableProtocolGuid, &VariableHandle);
719 ASSERT_EFI_ERROR (Status);
720
721 //
722 // Notify the variable wrapper driver the variable service is ready
723 //
724 Status = SystemTable->BootServices->InstallProtocolInterface (
725 &mVariableHandle,
726 &gEfiSmmVariableProtocolGuid,
727 EFI_NATIVE_INTERFACE,
728 &gSmmVariable
729 );
730 ASSERT_EFI_ERROR (Status);
731
732 //
733 // Register FtwNotificationEvent () notify function.
734 //
735 Status = gSmst->SmmRegisterProtocolNotify (
736 &gEfiSmmFaultTolerantWriteProtocolGuid,
737 SmmFtwNotificationEvent,
738 &SmmFtwRegistration
739 );
740 ASSERT_EFI_ERROR (Status);
741
742 SmmFtwNotificationEvent (NULL, NULL, NULL);
743
744 return EFI_SUCCESS;
745 }
746
747