]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
6df1660a04c3ed914110724a7a71ef57dc5fdd49
[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 - 2015, 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 #include <Protocol/SmmEndOfDxe.h>
33 #include <Protocol/SmmVarCheck.h>
34
35 #include <Library/SmmServicesTableLib.h>
36
37 #include <Guid/VariableFormat.h>
38 #include <Guid/SmmVariableCommon.h>
39 #include "Variable.h"
40
41 EFI_SMRAM_DESCRIPTOR *mSmramRanges;
42 UINTN mSmramRangeCount;
43
44 extern VARIABLE_INFO_ENTRY *gVariableInfo;
45 EFI_HANDLE mSmmVariableHandle = NULL;
46 EFI_HANDLE mVariableHandle = NULL;
47 BOOLEAN mAtRuntime = FALSE;
48 EFI_GUID mZeroGuid = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};
49 UINT8 *mVariableBufferPayload = NULL;
50 UINTN mVariableBufferPayloadSize;
51 extern BOOLEAN mEndOfDxe;
52 extern BOOLEAN mEnableLocking;
53
54 /**
55
56 This code sets variable in storage blocks (Volatile or Non-Volatile).
57
58 @param VariableName Name of Variable to be found.
59 @param VendorGuid Variable vendor GUID.
60 @param Attributes Attribute value of the variable found
61 @param DataSize Size of Data found. If size is less than the
62 data, this value contains the required size.
63 @param Data Data pointer.
64
65 @return EFI_INVALID_PARAMETER Invalid parameter.
66 @return EFI_SUCCESS Set successfully.
67 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
68 @return EFI_NOT_FOUND Not found.
69 @return EFI_WRITE_PROTECTED Variable is read-only.
70
71 **/
72 EFI_STATUS
73 EFIAPI
74 SmmVariableSetVariable (
75 IN CHAR16 *VariableName,
76 IN EFI_GUID *VendorGuid,
77 IN UINT32 Attributes,
78 IN UINTN DataSize,
79 IN VOID *Data
80 )
81 {
82 EFI_STATUS Status;
83
84 //
85 // Disable write protection when the calling SetVariable() through EFI_SMM_VARIABLE_PROTOCOL.
86 //
87 mEnableLocking = FALSE;
88 Status = VariableServiceSetVariable (
89 VariableName,
90 VendorGuid,
91 Attributes,
92 DataSize,
93 Data
94 );
95 mEnableLocking = TRUE;
96 return Status;
97 }
98
99 EFI_SMM_VARIABLE_PROTOCOL gSmmVariable = {
100 VariableServiceGetVariable,
101 VariableServiceGetNextVariableName,
102 SmmVariableSetVariable,
103 VariableServiceQueryVariableInfo
104 };
105
106 EDKII_SMM_VAR_CHECK_PROTOCOL mSmmVarCheck = { VarCheckRegisterSetVariableCheckHandler,
107 VarCheckVariablePropertySet,
108 VarCheckVariablePropertyGet };
109
110 /**
111 Return TRUE if ExitBootServices () has been called.
112
113 @retval TRUE If ExitBootServices () has been called.
114 **/
115 BOOLEAN
116 AtRuntime (
117 VOID
118 )
119 {
120 return mAtRuntime;
121 }
122
123 /**
124 This function check if the address is in SMRAM.
125
126 @param Buffer the buffer address to be checked.
127 @param Length the buffer length to be checked.
128
129 @retval TRUE this address is in SMRAM.
130 @retval FALSE this address is NOT in SMRAM.
131 **/
132 BOOLEAN
133 InternalIsAddressInSmram (
134 IN EFI_PHYSICAL_ADDRESS Buffer,
135 IN UINT64 Length
136 )
137 {
138 UINTN Index;
139
140 for (Index = 0; Index < mSmramRangeCount; Index ++) {
141 if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) ||
142 ((mSmramRanges[Index].CpuStart >= Buffer) && (mSmramRanges[Index].CpuStart < Buffer + Length))) {
143 return TRUE;
144 }
145 }
146
147 return FALSE;
148 }
149
150 /**
151 This function check if the address refered by Buffer and Length is valid.
152
153 @param Buffer the buffer address to be checked.
154 @param Length the buffer length to be checked.
155
156 @retval TRUE this address is valid.
157 @retval FALSE this address is NOT valid.
158 **/
159 BOOLEAN
160 InternalIsAddressValid (
161 IN UINTN Buffer,
162 IN UINTN Length
163 )
164 {
165 if (Buffer > (MAX_ADDRESS - Length)) {
166 //
167 // Overflow happen
168 //
169 return FALSE;
170 }
171 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)Buffer, (UINT64)Length)) {
172 return FALSE;
173 }
174 return TRUE;
175 }
176
177 /**
178 Initializes a basic mutual exclusion lock.
179
180 This function initializes a basic mutual exclusion lock to the released state
181 and returns the lock. Each lock provides mutual exclusion access at its task
182 priority level. Since there is no preemption or multiprocessor support in EFI,
183 acquiring the lock only consists of raising to the locks TPL.
184 If Lock is NULL, then ASSERT().
185 If Priority is not a valid TPL value, then ASSERT().
186
187 @param Lock A pointer to the lock data structure to initialize.
188 @param Priority EFI TPL is associated with the lock.
189
190 @return The lock.
191
192 **/
193 EFI_LOCK *
194 InitializeLock (
195 IN OUT EFI_LOCK *Lock,
196 IN EFI_TPL Priority
197 )
198 {
199 return Lock;
200 }
201
202 /**
203 Acquires lock only at boot time. Simply returns at runtime.
204
205 This is a temperary function that will be removed when
206 EfiAcquireLock() in UefiLib can handle the call in UEFI
207 Runtimer driver in RT phase.
208 It calls EfiAcquireLock() at boot time, and simply returns
209 at runtime.
210
211 @param Lock A pointer to the lock to acquire.
212
213 **/
214 VOID
215 AcquireLockOnlyAtBootTime (
216 IN EFI_LOCK *Lock
217 )
218 {
219
220 }
221
222
223 /**
224 Releases lock only at boot time. Simply returns at runtime.
225
226 This is a temperary function which will be removed when
227 EfiReleaseLock() in UefiLib can handle the call in UEFI
228 Runtimer driver in RT phase.
229 It calls EfiReleaseLock() at boot time and simply returns
230 at runtime.
231
232 @param Lock A pointer to the lock to release.
233
234 **/
235 VOID
236 ReleaseLockOnlyAtBootTime (
237 IN EFI_LOCK *Lock
238 )
239 {
240
241 }
242
243 /**
244 Retrive the SMM Fault Tolerent Write protocol interface.
245
246 @param[out] FtwProtocol The interface of SMM Ftw protocol
247
248 @retval EFI_SUCCESS The SMM FTW protocol instance was found and returned in FtwProtocol.
249 @retval EFI_NOT_FOUND The SMM FTW protocol instance was not found.
250 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
251
252 **/
253 EFI_STATUS
254 GetFtwProtocol (
255 OUT VOID **FtwProtocol
256 )
257 {
258 EFI_STATUS Status;
259
260 //
261 // Locate Smm Fault Tolerent Write protocol
262 //
263 Status = gSmst->SmmLocateProtocol (
264 &gEfiSmmFaultTolerantWriteProtocolGuid,
265 NULL,
266 FtwProtocol
267 );
268 return Status;
269 }
270
271
272 /**
273 Retrive the SMM FVB protocol interface by HANDLE.
274
275 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for
276 reading, writing, and erasing the target block.
277 @param[out] FvBlock The interface of SMM FVB protocol
278
279 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
280 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.
281 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
282
283 **/
284 EFI_STATUS
285 GetFvbByHandle (
286 IN EFI_HANDLE FvBlockHandle,
287 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
288 )
289 {
290 //
291 // To get the SMM FVB protocol interface on the handle
292 //
293 return gSmst->SmmHandleProtocol (
294 FvBlockHandle,
295 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
296 (VOID **) FvBlock
297 );
298 }
299
300
301 /**
302 Function returns an array of handles that support the SMM FVB protocol
303 in a buffer allocated from pool.
304
305 @param[out] NumberHandles The number of handles returned in Buffer.
306 @param[out] Buffer A pointer to the buffer to return the requested
307 array of handles that support SMM FVB protocol.
308
309 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
310 handles in Buffer was returned in NumberHandles.
311 @retval EFI_NOT_FOUND No SMM FVB handle was found.
312 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
313 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
314
315 **/
316 EFI_STATUS
317 GetFvbCountAndBuffer (
318 OUT UINTN *NumberHandles,
319 OUT EFI_HANDLE **Buffer
320 )
321 {
322 EFI_STATUS Status;
323 UINTN BufferSize;
324
325 if ((NumberHandles == NULL) || (Buffer == NULL)) {
326 return EFI_INVALID_PARAMETER;
327 }
328
329 BufferSize = 0;
330 *NumberHandles = 0;
331 *Buffer = NULL;
332 Status = gSmst->SmmLocateHandle (
333 ByProtocol,
334 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
335 NULL,
336 &BufferSize,
337 *Buffer
338 );
339 if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {
340 return EFI_NOT_FOUND;
341 }
342
343 *Buffer = AllocatePool (BufferSize);
344 if (*Buffer == NULL) {
345 return EFI_OUT_OF_RESOURCES;
346 }
347
348 Status = gSmst->SmmLocateHandle (
349 ByProtocol,
350 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
351 NULL,
352 &BufferSize,
353 *Buffer
354 );
355
356 *NumberHandles = BufferSize / sizeof(EFI_HANDLE);
357 if (EFI_ERROR(Status)) {
358 *NumberHandles = 0;
359 FreePool (*Buffer);
360 *Buffer = NULL;
361 }
362
363 return Status;
364 }
365
366
367 /**
368 Get the variable statistics information from the information buffer pointed by gVariableInfo.
369
370 Caution: This function may be invoked at SMM runtime.
371 InfoEntry and InfoSize are external input. Care must be taken to make sure not security issue at runtime.
372
373 @param[in, out] InfoEntry A pointer to the buffer of variable information entry.
374 On input, point to the variable information returned last time. if
375 InfoEntry->VendorGuid is zero, return the first information.
376 On output, point to the next variable information.
377 @param[in, out] InfoSize On input, the size of the variable information buffer.
378 On output, the returned variable information size.
379
380 @retval EFI_SUCCESS The variable information is found and returned successfully.
381 @retval EFI_UNSUPPORTED No variable inoformation exists in variable driver. The
382 PcdVariableCollectStatistics should be set TRUE to support it.
383 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the next variable information.
384
385 **/
386 EFI_STATUS
387 SmmVariableGetStatistics (
388 IN OUT VARIABLE_INFO_ENTRY *InfoEntry,
389 IN OUT UINTN *InfoSize
390 )
391 {
392 VARIABLE_INFO_ENTRY *VariableInfo;
393 UINTN NameLength;
394 UINTN StatisticsInfoSize;
395 CHAR16 *InfoName;
396 EFI_GUID VendorGuid;
397
398 ASSERT (InfoEntry != NULL);
399 VariableInfo = gVariableInfo;
400 if (VariableInfo == NULL) {
401 return EFI_UNSUPPORTED;
402 }
403
404 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);
405 if (*InfoSize < StatisticsInfoSize) {
406 *InfoSize = StatisticsInfoSize;
407 return EFI_BUFFER_TOO_SMALL;
408 }
409 InfoName = (CHAR16 *)(InfoEntry + 1);
410
411 CopyGuid (&VendorGuid, &InfoEntry->VendorGuid);
412
413 if (CompareGuid (&VendorGuid, &mZeroGuid)) {
414 //
415 // Return the first variable info
416 //
417 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));
418 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));
419 *InfoSize = StatisticsInfoSize;
420 return EFI_SUCCESS;
421 }
422
423 //
424 // Get the next variable info
425 //
426 while (VariableInfo != NULL) {
427 if (CompareGuid (&VariableInfo->VendorGuid, &VendorGuid)) {
428 NameLength = StrSize (VariableInfo->Name);
429 if (NameLength == StrSize (InfoName)) {
430 if (CompareMem (VariableInfo->Name, InfoName, NameLength) == 0) {
431 //
432 // Find the match one
433 //
434 VariableInfo = VariableInfo->Next;
435 break;
436 }
437 }
438 }
439 VariableInfo = VariableInfo->Next;
440 };
441
442 if (VariableInfo == NULL) {
443 *InfoSize = 0;
444 return EFI_SUCCESS;
445 }
446
447 //
448 // Output the new variable info
449 //
450 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);
451 if (*InfoSize < StatisticsInfoSize) {
452 *InfoSize = StatisticsInfoSize;
453 return EFI_BUFFER_TOO_SMALL;
454 }
455
456 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));
457 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));
458 *InfoSize = StatisticsInfoSize;
459
460 return EFI_SUCCESS;
461 }
462
463
464 /**
465 Communication service SMI Handler entry.
466
467 This SMI handler provides services for the variable wrapper driver.
468
469 Caution: This function may receive untrusted input.
470 This variable data and communicate buffer are external input, so this function will do basic validation.
471 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),
472 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),
473 SmmVariableGetStatistics() should also do validation based on its own knowledge.
474
475 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
476 @param[in] RegisterContext Points to an optional handler context which was specified when the
477 handler was registered.
478 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
479 be conveyed from a non-SMM environment into an SMM environment.
480 @param[in, out] CommBufferSize The size of the CommBuffer.
481
482 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
483 should still be called.
484 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
485 still be called.
486 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
487 be called.
488 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
489 **/
490 EFI_STATUS
491 EFIAPI
492 SmmVariableHandler (
493 IN EFI_HANDLE DispatchHandle,
494 IN CONST VOID *RegisterContext,
495 IN OUT VOID *CommBuffer,
496 IN OUT UINTN *CommBufferSize
497 )
498 {
499 EFI_STATUS Status;
500 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;
501 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;
502 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *GetNextVariableName;
503 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *QueryVariableInfo;
504 VARIABLE_INFO_ENTRY *VariableInfo;
505 SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *VariableToLock;
506 SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProperty;
507 UINTN InfoSize;
508 UINTN NameBufferSize;
509 UINTN CommBufferPayloadSize;
510 UINTN TempCommBufferSize;
511
512 //
513 // If input is invalid, stop processing this SMI
514 //
515 if (CommBuffer == NULL || CommBufferSize == NULL) {
516 return EFI_SUCCESS;
517 }
518
519 TempCommBufferSize = *CommBufferSize;
520
521 if (TempCommBufferSize < SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
522 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer size invalid!\n"));
523 return EFI_SUCCESS;
524 }
525 CommBufferPayloadSize = TempCommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;
526 if (CommBufferPayloadSize > mVariableBufferPayloadSize) {
527 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer payload size invalid!\n"));
528 return EFI_SUCCESS;
529 }
530
531 if (!InternalIsAddressValid ((UINTN)CommBuffer, TempCommBufferSize)) {
532 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer in SMRAM or overflow!\n"));
533 return EFI_SUCCESS;
534 }
535
536 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)CommBuffer;
537 switch (SmmVariableFunctionHeader->Function) {
538 case SMM_VARIABLE_FUNCTION_GET_VARIABLE:
539 if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {
540 DEBUG ((EFI_D_ERROR, "GetVariable: SMM communication buffer size invalid!\n"));
541 return EFI_SUCCESS;
542 }
543 //
544 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
545 //
546 CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);
547 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) mVariableBufferPayload;
548 if (((UINTN)(~0) - SmmVariableHeader->DataSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||
549 ((UINTN)(~0) - SmmVariableHeader->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize)) {
550 //
551 // Prevent InfoSize overflow happen
552 //
553 Status = EFI_ACCESS_DENIED;
554 goto EXIT;
555 }
556 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)
557 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;
558
559 //
560 // SMRAM range check already covered before
561 //
562 if (InfoSize > CommBufferPayloadSize) {
563 DEBUG ((EFI_D_ERROR, "GetVariable: Data size exceed communication buffer size limit!\n"));
564 Status = EFI_ACCESS_DENIED;
565 goto EXIT;
566 }
567
568 if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') {
569 //
570 // Make sure VariableName is A Null-terminated string.
571 //
572 Status = EFI_ACCESS_DENIED;
573 goto EXIT;
574 }
575
576 Status = VariableServiceGetVariable (
577 SmmVariableHeader->Name,
578 &SmmVariableHeader->Guid,
579 &SmmVariableHeader->Attributes,
580 &SmmVariableHeader->DataSize,
581 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize
582 );
583 CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);
584 break;
585
586 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME:
587 if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {
588 DEBUG ((EFI_D_ERROR, "GetNextVariableName: SMM communication buffer size invalid!\n"));
589 return EFI_SUCCESS;
590 }
591 //
592 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
593 //
594 CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);
595 GetNextVariableName = (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *) mVariableBufferPayload;
596 if ((UINTN)(~0) - GetNextVariableName->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {
597 //
598 // Prevent InfoSize overflow happen
599 //
600 Status = EFI_ACCESS_DENIED;
601 goto EXIT;
602 }
603 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + GetNextVariableName->NameSize;
604
605 //
606 // SMRAM range check already covered before
607 //
608 if (InfoSize > CommBufferPayloadSize) {
609 DEBUG ((EFI_D_ERROR, "GetNextVariableName: Data size exceed communication buffer size limit!\n"));
610 Status = EFI_ACCESS_DENIED;
611 goto EXIT;
612 }
613
614 NameBufferSize = CommBufferPayloadSize - OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);
615 if (NameBufferSize < sizeof (CHAR16) || GetNextVariableName->Name[NameBufferSize/sizeof (CHAR16) - 1] != L'\0') {
616 //
617 // Make sure input VariableName is A Null-terminated string.
618 //
619 Status = EFI_ACCESS_DENIED;
620 goto EXIT;
621 }
622
623 Status = VariableServiceGetNextVariableName (
624 &GetNextVariableName->NameSize,
625 GetNextVariableName->Name,
626 &GetNextVariableName->Guid
627 );
628 CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);
629 break;
630
631 case SMM_VARIABLE_FUNCTION_SET_VARIABLE:
632 if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {
633 DEBUG ((EFI_D_ERROR, "SetVariable: SMM communication buffer size invalid!\n"));
634 return EFI_SUCCESS;
635 }
636 //
637 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
638 //
639 CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);
640 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) mVariableBufferPayload;
641 if (((UINTN)(~0) - SmmVariableHeader->DataSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||
642 ((UINTN)(~0) - SmmVariableHeader->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize)) {
643 //
644 // Prevent InfoSize overflow happen
645 //
646 Status = EFI_ACCESS_DENIED;
647 goto EXIT;
648 }
649 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)
650 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;
651
652 //
653 // SMRAM range check already covered before
654 // Data buffer should not contain SMM range
655 //
656 if (InfoSize > CommBufferPayloadSize) {
657 DEBUG ((EFI_D_ERROR, "SetVariable: Data size exceed communication buffer size limit!\n"));
658 Status = EFI_ACCESS_DENIED;
659 goto EXIT;
660 }
661
662 if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') {
663 //
664 // Make sure VariableName is A Null-terminated string.
665 //
666 Status = EFI_ACCESS_DENIED;
667 goto EXIT;
668 }
669
670 Status = VariableServiceSetVariable (
671 SmmVariableHeader->Name,
672 &SmmVariableHeader->Guid,
673 SmmVariableHeader->Attributes,
674 SmmVariableHeader->DataSize,
675 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize
676 );
677 break;
678
679 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO:
680 if (CommBufferPayloadSize < sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO)) {
681 DEBUG ((EFI_D_ERROR, "QueryVariableInfo: SMM communication buffer size invalid!\n"));
682 return EFI_SUCCESS;
683 }
684 QueryVariableInfo = (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *) SmmVariableFunctionHeader->Data;
685
686 Status = VariableServiceQueryVariableInfo (
687 QueryVariableInfo->Attributes,
688 &QueryVariableInfo->MaximumVariableStorageSize,
689 &QueryVariableInfo->RemainingVariableStorageSize,
690 &QueryVariableInfo->MaximumVariableSize
691 );
692 break;
693
694 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT:
695 mEndOfDxe = TRUE;
696 if (AtRuntime()) {
697 Status = EFI_UNSUPPORTED;
698 break;
699 }
700 ReclaimForOS ();
701 Status = EFI_SUCCESS;
702 break;
703
704 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE:
705 mAtRuntime = TRUE;
706 Status = EFI_SUCCESS;
707 break;
708
709 case SMM_VARIABLE_FUNCTION_GET_STATISTICS:
710 VariableInfo = (VARIABLE_INFO_ENTRY *) SmmVariableFunctionHeader->Data;
711 InfoSize = TempCommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;
712
713 //
714 // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here.
715 // It is covered by previous CommBuffer check
716 //
717
718 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBufferSize, sizeof(UINTN))) {
719 DEBUG ((EFI_D_ERROR, "GetStatistics: SMM communication buffer in SMRAM!\n"));
720 Status = EFI_ACCESS_DENIED;
721 goto EXIT;
722 }
723
724 Status = SmmVariableGetStatistics (VariableInfo, &InfoSize);
725 *CommBufferSize = InfoSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;
726 break;
727
728 case SMM_VARIABLE_FUNCTION_LOCK_VARIABLE:
729 if (mEndOfDxe) {
730 Status = EFI_ACCESS_DENIED;
731 } else {
732 VariableToLock = (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *) SmmVariableFunctionHeader->Data;
733 Status = VariableLockRequestToLock (
734 NULL,
735 VariableToLock->Name,
736 &VariableToLock->Guid
737 );
738 }
739 break;
740 case SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_SET:
741 if (mEndOfDxe) {
742 Status = EFI_ACCESS_DENIED;
743 } else {
744 CommVariableProperty = (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *) SmmVariableFunctionHeader->Data;
745 Status = VarCheckVariablePropertySet (
746 CommVariableProperty->Name,
747 &CommVariableProperty->Guid,
748 &CommVariableProperty->VariableProperty
749 );
750 }
751 break;
752 case SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET:
753 if (CommBufferPayloadSize < OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) {
754 DEBUG ((EFI_D_ERROR, "VarCheckVariablePropertyGet: SMM communication buffer size invalid!\n"));
755 return EFI_SUCCESS;
756 }
757 //
758 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
759 //
760 CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);
761 CommVariableProperty = (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *) mVariableBufferPayload;
762 if ((UINTN) (~0) - CommVariableProperty->NameSize < OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) {
763 //
764 // Prevent InfoSize overflow happen
765 //
766 Status = EFI_ACCESS_DENIED;
767 goto EXIT;
768 }
769 InfoSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) + CommVariableProperty->NameSize;
770
771 //
772 // SMRAM range check already covered before
773 //
774 if (InfoSize > CommBufferPayloadSize) {
775 DEBUG ((EFI_D_ERROR, "VarCheckVariablePropertyGet: Data size exceed communication buffer size limit!\n"));
776 Status = EFI_ACCESS_DENIED;
777 goto EXIT;
778 }
779
780 if (CommVariableProperty->NameSize < sizeof (CHAR16) || CommVariableProperty->Name[CommVariableProperty->NameSize/sizeof (CHAR16) - 1] != L'\0') {
781 //
782 // Make sure VariableName is A Null-terminated string.
783 //
784 Status = EFI_ACCESS_DENIED;
785 goto EXIT;
786 }
787
788 Status = VarCheckVariablePropertyGet (
789 CommVariableProperty->Name,
790 &CommVariableProperty->Guid,
791 &CommVariableProperty->VariableProperty
792 );
793 CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);
794 break;
795
796 default:
797 Status = EFI_UNSUPPORTED;
798 }
799
800 EXIT:
801
802 SmmVariableFunctionHeader->ReturnStatus = Status;
803
804 return EFI_SUCCESS;
805 }
806
807 /**
808 SMM END_OF_DXE protocol notification event handler.
809
810 @param Protocol Points to the protocol's unique identifier
811 @param Interface Points to the interface instance
812 @param Handle The handle on which the interface was installed
813
814 @retval EFI_SUCCESS SmmEndOfDxeCallback runs successfully
815
816 **/
817 EFI_STATUS
818 EFIAPI
819 SmmEndOfDxeCallback (
820 IN CONST EFI_GUID *Protocol,
821 IN VOID *Interface,
822 IN EFI_HANDLE Handle
823 )
824 {
825 DEBUG ((EFI_D_INFO, "[Variable]END_OF_DXE is signaled\n"));
826 mEndOfDxe = TRUE;
827 return EFI_SUCCESS;
828 }
829
830 /**
831 SMM Fault Tolerant Write protocol notification event handler.
832
833 Non-Volatile variable write may needs FTW protocol to reclaim when
834 writting variable.
835
836 @param Protocol Points to the protocol's unique identifier
837 @param Interface Points to the interface instance
838 @param Handle The handle on which the interface was installed
839
840 @retval EFI_SUCCESS SmmEventCallback runs successfully
841 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.
842
843 **/
844 EFI_STATUS
845 EFIAPI
846 SmmFtwNotificationEvent (
847 IN CONST EFI_GUID *Protocol,
848 IN VOID *Interface,
849 IN EFI_HANDLE Handle
850 )
851 {
852 EFI_STATUS Status;
853 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;
854 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;
855 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;
856 UINTN FtwMaxBlockSize;
857
858 if (mVariableModuleGlobal->FvbInstance != NULL) {
859 return EFI_SUCCESS;
860 }
861
862 //
863 // Ensure SMM FTW protocol is installed.
864 //
865 Status = GetFtwProtocol ((VOID **)&FtwProtocol);
866 if (EFI_ERROR (Status)) {
867 return Status;
868 }
869
870 Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize);
871 if (!EFI_ERROR (Status)) {
872 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) <= FtwMaxBlockSize);
873 }
874
875 //
876 // Find the proper FVB protocol for variable.
877 //
878 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);
879 if (NvStorageVariableBase == 0) {
880 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);
881 }
882 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);
883 if (EFI_ERROR (Status)) {
884 return EFI_NOT_FOUND;
885 }
886
887 mVariableModuleGlobal->FvbInstance = FvbProtocol;
888
889 Status = VariableWriteServiceInitialize ();
890 ASSERT_EFI_ERROR (Status);
891
892 //
893 // Notify the variable wrapper driver the variable write service is ready
894 //
895 Status = gBS->InstallProtocolInterface (
896 &mSmmVariableHandle,
897 &gSmmVariableWriteGuid,
898 EFI_NATIVE_INTERFACE,
899 NULL
900 );
901 ASSERT_EFI_ERROR (Status);
902
903 return EFI_SUCCESS;
904 }
905
906
907 /**
908 Variable Driver main entry point. The Variable driver places the 4 EFI
909 runtime services in the EFI System Table and installs arch protocols
910 for variable read and write services being available. It also registers
911 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
912
913 @param[in] ImageHandle The firmware allocated handle for the EFI image.
914 @param[in] SystemTable A pointer to the EFI System Table.
915
916 @retval EFI_SUCCESS Variable service successfully initialized.
917
918 **/
919 EFI_STATUS
920 EFIAPI
921 VariableServiceInitialize (
922 IN EFI_HANDLE ImageHandle,
923 IN EFI_SYSTEM_TABLE *SystemTable
924 )
925 {
926 EFI_STATUS Status;
927 EFI_HANDLE VariableHandle;
928 VOID *SmmFtwRegistration;
929 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;
930 UINTN Size;
931 VOID *SmmEndOfDxeRegistration;
932
933 //
934 // Variable initialize.
935 //
936 Status = VariableCommonInitialize ();
937 ASSERT_EFI_ERROR (Status);
938
939 //
940 // Install the Smm Variable Protocol on a new handle.
941 //
942 VariableHandle = NULL;
943 Status = gSmst->SmmInstallProtocolInterface (
944 &VariableHandle,
945 &gEfiSmmVariableProtocolGuid,
946 EFI_NATIVE_INTERFACE,
947 &gSmmVariable
948 );
949 ASSERT_EFI_ERROR (Status);
950
951 Status = gSmst->SmmInstallProtocolInterface (
952 &VariableHandle,
953 &gEdkiiSmmVarCheckProtocolGuid,
954 EFI_NATIVE_INTERFACE,
955 &mSmmVarCheck
956 );
957 ASSERT_EFI_ERROR (Status);
958
959 //
960 // Get SMRAM information
961 //
962 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);
963 ASSERT_EFI_ERROR (Status);
964
965 Size = 0;
966 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);
967 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
968
969 Status = gSmst->SmmAllocatePool (
970 EfiRuntimeServicesData,
971 Size,
972 (VOID **)&mSmramRanges
973 );
974 ASSERT_EFI_ERROR (Status);
975
976 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);
977 ASSERT_EFI_ERROR (Status);
978
979 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
980
981 mVariableBufferPayloadSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize)) +
982 OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) - sizeof (VARIABLE_HEADER);
983
984 Status = gSmst->SmmAllocatePool (
985 EfiRuntimeServicesData,
986 mVariableBufferPayloadSize,
987 (VOID **)&mVariableBufferPayload
988 );
989 ASSERT_EFI_ERROR (Status);
990
991 ///
992 /// Register SMM variable SMI handler
993 ///
994 VariableHandle = NULL;
995 Status = gSmst->SmiHandlerRegister (SmmVariableHandler, &gEfiSmmVariableProtocolGuid, &VariableHandle);
996 ASSERT_EFI_ERROR (Status);
997
998 //
999 // Notify the variable wrapper driver the variable service is ready
1000 //
1001 Status = SystemTable->BootServices->InstallProtocolInterface (
1002 &mVariableHandle,
1003 &gEfiSmmVariableProtocolGuid,
1004 EFI_NATIVE_INTERFACE,
1005 &gSmmVariable
1006 );
1007 ASSERT_EFI_ERROR (Status);
1008
1009 //
1010 // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.
1011 //
1012 Status = gSmst->SmmRegisterProtocolNotify (
1013 &gEfiSmmEndOfDxeProtocolGuid,
1014 SmmEndOfDxeCallback,
1015 &SmmEndOfDxeRegistration
1016 );
1017 ASSERT_EFI_ERROR (Status);
1018
1019 //
1020 // Register FtwNotificationEvent () notify function.
1021 //
1022 Status = gSmst->SmmRegisterProtocolNotify (
1023 &gEfiSmmFaultTolerantWriteProtocolGuid,
1024 SmmFtwNotificationEvent,
1025 &SmmFtwRegistration
1026 );
1027 ASSERT_EFI_ERROR (Status);
1028
1029 SmmFtwNotificationEvent (NULL, NULL, NULL);
1030
1031 return EFI_SUCCESS;
1032 }
1033
1034