]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.c
SecurityPkg Variable: Add the check for PcdFlashNvStorageVariableSize <= PcdFlashNvSt...
[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 - 2013, 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 #include <Protocol/SmmEndOfDxe.h>
33
34 #include <Library/SmmServicesTableLib.h>
35
36 #include <Guid/AuthenticatedVariableFormat.h>
37 #include <Guid/SmmVariableCommon.h>
38 #include "Variable.h"
39
40 EFI_SMRAM_DESCRIPTOR *mSmramRanges;
41 UINTN mSmramRangeCount;
42
43 extern VARIABLE_INFO_ENTRY *gVariableInfo;
44 EFI_HANDLE mSmmVariableHandle = NULL;
45 EFI_HANDLE mVariableHandle = NULL;
46 BOOLEAN mAtRuntime = FALSE;
47 EFI_GUID mZeroGuid = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};
48 UINT8 *mVariableBufferPayload = NULL;
49 UINTN mVariableBufferPayloadSize;
50 extern BOOLEAN mEndOfDxe;
51 extern BOOLEAN mEnableLocking;
52
53 /**
54
55 This code sets variable in storage blocks (Volatile or Non-Volatile).
56
57 @param VariableName Name of Variable to be found.
58 @param VendorGuid Variable vendor GUID.
59 @param Attributes Attribute value of the variable found
60 @param DataSize Size of Data found. If size is less than the
61 data, this value contains the required size.
62 @param Data Data pointer.
63
64 @return EFI_INVALID_PARAMETER Invalid parameter.
65 @return EFI_SUCCESS Set successfully.
66 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
67 @return EFI_NOT_FOUND Not found.
68 @return EFI_WRITE_PROTECTED Variable is read-only.
69
70 **/
71 EFI_STATUS
72 EFIAPI
73 SmmVariableSetVariable (
74 IN CHAR16 *VariableName,
75 IN EFI_GUID *VendorGuid,
76 IN UINT32 Attributes,
77 IN UINTN DataSize,
78 IN VOID *Data
79 )
80 {
81 EFI_STATUS Status;
82
83 //
84 // Disable write protection when the calling SetVariable() through EFI_SMM_VARIABLE_PROTOCOL.
85 //
86 mEnableLocking = FALSE;
87 Status = VariableServiceSetVariable (
88 VariableName,
89 VendorGuid,
90 Attributes,
91 DataSize,
92 Data
93 );
94 mEnableLocking = TRUE;
95 return Status;
96 }
97
98 EFI_SMM_VARIABLE_PROTOCOL gSmmVariable = {
99 VariableServiceGetVariable,
100 VariableServiceGetNextVariableName,
101 SmmVariableSetVariable,
102 VariableServiceQueryVariableInfo
103 };
104
105 /**
106 Return TRUE if ExitBootServices () has been called.
107
108 @retval TRUE If ExitBootServices () has been called.
109 **/
110 BOOLEAN
111 AtRuntime (
112 VOID
113 )
114 {
115 return mAtRuntime;
116 }
117
118 /**
119 This function check if the address is in SMRAM.
120
121 @param Buffer the buffer address to be checked.
122 @param Length the buffer length to be checked.
123
124 @retval TRUE this address is in SMRAM.
125 @retval FALSE this address is NOT in SMRAM.
126 **/
127 BOOLEAN
128 InternalIsAddressInSmram (
129 IN EFI_PHYSICAL_ADDRESS Buffer,
130 IN UINT64 Length
131 )
132 {
133 UINTN Index;
134
135 for (Index = 0; Index < mSmramRangeCount; Index ++) {
136 if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) ||
137 ((mSmramRanges[Index].CpuStart >= Buffer) && (mSmramRanges[Index].CpuStart < Buffer + Length))) {
138 return TRUE;
139 }
140 }
141
142 return FALSE;
143 }
144
145 /**
146 This function check if the address refered by Buffer and Length is valid.
147
148 @param Buffer the buffer address to be checked.
149 @param Length the buffer length to be checked.
150
151 @retval TRUE this address is valid.
152 @retval FALSE this address is NOT valid.
153 **/
154 BOOLEAN
155 InternalIsAddressValid (
156 IN UINTN Buffer,
157 IN UINTN Length
158 )
159 {
160 if (Buffer > (MAX_ADDRESS - Length)) {
161 //
162 // Overflow happen
163 //
164 return FALSE;
165 }
166 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)Buffer, (UINT64)Length)) {
167 return FALSE;
168 }
169 return TRUE;
170 }
171
172 /**
173 Initializes a basic mutual exclusion lock.
174
175 This function initializes a basic mutual exclusion lock to the released state
176 and returns the lock. Each lock provides mutual exclusion access at its task
177 priority level. Since there is no preemption or multiprocessor support in EFI,
178 acquiring the lock only consists of raising to the locks TPL.
179 If Lock is NULL, then ASSERT().
180 If Priority is not a valid TPL value, then ASSERT().
181
182 @param Lock A pointer to the lock data structure to initialize.
183 @param Priority EFI TPL is associated with the lock.
184
185 @return The lock.
186
187 **/
188 EFI_LOCK *
189 InitializeLock (
190 IN OUT EFI_LOCK *Lock,
191 IN EFI_TPL Priority
192 )
193 {
194 return Lock;
195 }
196
197 /**
198 Acquires lock only at boot time. Simply returns at runtime.
199
200 This is a temperary function that will be removed when
201 EfiAcquireLock() in UefiLib can handle the call in UEFI
202 Runtimer driver in RT phase.
203 It calls EfiAcquireLock() at boot time, and simply returns
204 at runtime.
205
206 @param Lock A pointer to the lock to acquire.
207
208 **/
209 VOID
210 AcquireLockOnlyAtBootTime (
211 IN EFI_LOCK *Lock
212 )
213 {
214
215 }
216
217
218 /**
219 Releases lock only at boot time. Simply returns at runtime.
220
221 This is a temperary function which will be removed when
222 EfiReleaseLock() in UefiLib can handle the call in UEFI
223 Runtimer driver in RT phase.
224 It calls EfiReleaseLock() at boot time and simply returns
225 at runtime.
226
227 @param Lock A pointer to the lock to release.
228
229 **/
230 VOID
231 ReleaseLockOnlyAtBootTime (
232 IN EFI_LOCK *Lock
233 )
234 {
235
236 }
237
238 /**
239 Retrive the SMM Fault Tolerent Write protocol interface.
240
241 @param[out] FtwProtocol The interface of SMM Ftw protocol
242
243 @retval EFI_SUCCESS The SMM FTW protocol instance was found and returned in FtwProtocol.
244 @retval EFI_NOT_FOUND The SMM FTW protocol instance was not found.
245 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
246
247 **/
248 EFI_STATUS
249 GetFtwProtocol (
250 OUT VOID **FtwProtocol
251 )
252 {
253 EFI_STATUS Status;
254
255 //
256 // Locate Smm Fault Tolerent Write protocol
257 //
258 Status = gSmst->SmmLocateProtocol (
259 &gEfiSmmFaultTolerantWriteProtocolGuid,
260 NULL,
261 FtwProtocol
262 );
263 return Status;
264 }
265
266
267 /**
268 Retrive the SMM FVB protocol interface by HANDLE.
269
270 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for
271 reading, writing, and erasing the target block.
272 @param[out] FvBlock The interface of SMM FVB protocol
273
274 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
275 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.
276 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
277
278 **/
279 EFI_STATUS
280 GetFvbByHandle (
281 IN EFI_HANDLE FvBlockHandle,
282 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
283 )
284 {
285 //
286 // To get the SMM FVB protocol interface on the handle
287 //
288 return gSmst->SmmHandleProtocol (
289 FvBlockHandle,
290 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
291 (VOID **) FvBlock
292 );
293 }
294
295
296 /**
297 Function returns an array of handles that support the SMM FVB protocol
298 in a buffer allocated from pool.
299
300 @param[out] NumberHandles The number of handles returned in Buffer.
301 @param[out] Buffer A pointer to the buffer to return the requested
302 array of handles that support SMM FVB protocol.
303
304 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
305 handles in Buffer was returned in NumberHandles.
306 @retval EFI_NOT_FOUND No SMM FVB handle was found.
307 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
308 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
309
310 **/
311 EFI_STATUS
312 GetFvbCountAndBuffer (
313 OUT UINTN *NumberHandles,
314 OUT EFI_HANDLE **Buffer
315 )
316 {
317 EFI_STATUS Status;
318 UINTN BufferSize;
319
320 if ((NumberHandles == NULL) || (Buffer == NULL)) {
321 return EFI_INVALID_PARAMETER;
322 }
323
324 BufferSize = 0;
325 *NumberHandles = 0;
326 *Buffer = NULL;
327 Status = gSmst->SmmLocateHandle (
328 ByProtocol,
329 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
330 NULL,
331 &BufferSize,
332 *Buffer
333 );
334 if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {
335 return EFI_NOT_FOUND;
336 }
337
338 *Buffer = AllocatePool (BufferSize);
339 if (*Buffer == NULL) {
340 return EFI_OUT_OF_RESOURCES;
341 }
342
343 Status = gSmst->SmmLocateHandle (
344 ByProtocol,
345 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
346 NULL,
347 &BufferSize,
348 *Buffer
349 );
350
351 *NumberHandles = BufferSize / sizeof(EFI_HANDLE);
352 if (EFI_ERROR(Status)) {
353 *NumberHandles = 0;
354 FreePool (*Buffer);
355 *Buffer = NULL;
356 }
357
358 return Status;
359 }
360
361
362 /**
363 Get the variable statistics information from the information buffer pointed by gVariableInfo.
364
365 Caution: This function may be invoked at SMM runtime.
366 InfoEntry and InfoSize are external input. Care must be taken to make sure not security issue at runtime.
367
368 @param[in, out] InfoEntry A pointer to the buffer of variable information entry.
369 On input, point to the variable information returned last time. if
370 InfoEntry->VendorGuid is zero, return the first information.
371 On output, point to the next variable information.
372 @param[in, out] InfoSize On input, the size of the variable information buffer.
373 On output, the returned variable information size.
374
375 @retval EFI_SUCCESS The variable information is found and returned successfully.
376 @retval EFI_UNSUPPORTED No variable inoformation exists in variable driver. The
377 PcdVariableCollectStatistics should be set TRUE to support it.
378 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the next variable information.
379 @retval EFI_INVALID_PARAMETER Input parameter is invalid.
380
381 **/
382 EFI_STATUS
383 SmmVariableGetStatistics (
384 IN OUT VARIABLE_INFO_ENTRY *InfoEntry,
385 IN OUT UINTN *InfoSize
386 )
387 {
388 VARIABLE_INFO_ENTRY *VariableInfo;
389 UINTN NameLength;
390 UINTN StatisticsInfoSize;
391 CHAR16 *InfoName;
392 EFI_GUID VendorGuid;
393
394 if (InfoEntry == NULL) {
395 return EFI_INVALID_PARAMETER;
396 }
397
398 VariableInfo = gVariableInfo;
399 if (VariableInfo == NULL) {
400 return EFI_UNSUPPORTED;
401 }
402
403 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);
404 if (*InfoSize < StatisticsInfoSize) {
405 *InfoSize = StatisticsInfoSize;
406 return EFI_BUFFER_TOO_SMALL;
407 }
408 InfoName = (CHAR16 *)(InfoEntry + 1);
409
410 CopyGuid (&VendorGuid, &InfoEntry->VendorGuid);
411
412 if (CompareGuid (&VendorGuid, &mZeroGuid)) {
413 //
414 // Return the first variable info
415 //
416 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));
417 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));
418 *InfoSize = StatisticsInfoSize;
419 return EFI_SUCCESS;
420 }
421
422 //
423 // Get the next variable info
424 //
425 while (VariableInfo != NULL) {
426 if (CompareGuid (&VariableInfo->VendorGuid, &VendorGuid)) {
427 NameLength = StrSize (VariableInfo->Name);
428 if (NameLength == StrSize (InfoName)) {
429 if (CompareMem (VariableInfo->Name, InfoName, NameLength) == 0) {
430 //
431 // Find the match one
432 //
433 VariableInfo = VariableInfo->Next;
434 break;
435 }
436 }
437 }
438 VariableInfo = VariableInfo->Next;
439 };
440
441 if (VariableInfo == NULL) {
442 *InfoSize = 0;
443 return EFI_SUCCESS;
444 }
445
446 //
447 // Output the new variable info
448 //
449 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);
450 if (*InfoSize < StatisticsInfoSize) {
451 *InfoSize = StatisticsInfoSize;
452 return EFI_BUFFER_TOO_SMALL;
453 }
454
455 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));
456 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));
457 *InfoSize = StatisticsInfoSize;
458
459 return EFI_SUCCESS;
460 }
461
462
463 /**
464 Communication service SMI Handler entry.
465
466 This SMI handler provides services for the variable wrapper driver.
467
468 Caution: This function may receive untrusted input.
469 This variable data and communicate buffer are external input, so this function will do basic validation.
470 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),
471 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),
472 SmmVariableGetStatistics() should also do validation based on its own knowledge.
473
474 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
475 @param[in] RegisterContext Points to an optional handler context which was specified when the
476 handler was registered.
477 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
478 be conveyed from a non-SMM environment into an SMM environment.
479 @param[in, out] CommBufferSize The size of the CommBuffer.
480
481 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
482 should still be called.
483 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
484 still be called.
485 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
486 be called.
487 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
488
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 UINTN InfoSize;
507 UINTN NameBufferSize;
508 UINTN CommBufferPayloadSize;
509 UINTN TempCommBufferSize;
510
511 //
512 // If input is invalid, stop processing this SMI
513 //
514 if (CommBuffer == NULL || CommBufferSize == NULL) {
515 return EFI_SUCCESS;
516 }
517
518 TempCommBufferSize = *CommBufferSize;
519
520 if (TempCommBufferSize < SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
521 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer size invalid!\n"));
522 return EFI_SUCCESS;
523 }
524 CommBufferPayloadSize = TempCommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;
525 if (CommBufferPayloadSize > mVariableBufferPayloadSize) {
526 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer payload size invalid!\n"));
527 return EFI_SUCCESS;
528 }
529
530 if (!InternalIsAddressValid ((UINTN)CommBuffer, TempCommBufferSize)) {
531 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer in SMRAM or overflow!\n"));
532 return EFI_SUCCESS;
533 }
534
535 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)CommBuffer;
536
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
741 default:
742 Status = EFI_UNSUPPORTED;
743 }
744
745 EXIT:
746
747 SmmVariableFunctionHeader->ReturnStatus = Status;
748 return EFI_SUCCESS;
749 }
750
751 /**
752 SMM END_OF_DXE protocol notification event handler.
753
754 @param Protocol Points to the protocol's unique identifier
755 @param Interface Points to the interface instance
756 @param Handle The handle on which the interface was installed
757
758 @retval EFI_SUCCESS SmmEndOfDxeCallback runs successfully
759
760 **/
761 EFI_STATUS
762 EFIAPI
763 SmmEndOfDxeCallback (
764 IN CONST EFI_GUID *Protocol,
765 IN VOID *Interface,
766 IN EFI_HANDLE Handle
767 )
768 {
769 DEBUG ((EFI_D_INFO, "[Variable]END_OF_DXE is signaled\n"));
770 mEndOfDxe = TRUE;
771 return EFI_SUCCESS;
772 }
773
774 /**
775 SMM Fault Tolerant Write protocol notification event handler.
776
777 Non-Volatile variable write may needs FTW protocol to reclaim when
778 writting variable.
779
780 @param Protocol Points to the protocol's unique identifier
781 @param Interface Points to the interface instance
782 @param Handle The handle on which the interface was installed
783
784 @retval EFI_SUCCESS SmmEventCallback runs successfully
785 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.
786
787 **/
788 EFI_STATUS
789 EFIAPI
790 SmmFtwNotificationEvent (
791 IN CONST EFI_GUID *Protocol,
792 IN VOID *Interface,
793 IN EFI_HANDLE Handle
794 )
795 {
796 EFI_STATUS Status;
797 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;
798 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;
799 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;
800 UINTN FtwMaxBlockSize;
801
802 if (mVariableModuleGlobal->FvbInstance != NULL) {
803 return EFI_SUCCESS;
804 }
805
806 //
807 // Ensure SMM FTW protocol is installed.
808 //
809 Status = GetFtwProtocol ((VOID **)&FtwProtocol);
810 if (EFI_ERROR (Status)) {
811 return Status;
812 }
813
814 Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize);
815 if (!EFI_ERROR (Status)) {
816 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) <= FtwMaxBlockSize);
817 }
818
819 //
820 // Find the proper FVB protocol for variable.
821 //
822 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);
823 if (NvStorageVariableBase == 0) {
824 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);
825 }
826 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);
827 if (EFI_ERROR (Status)) {
828 return EFI_NOT_FOUND;
829 }
830
831 mVariableModuleGlobal->FvbInstance = FvbProtocol;
832
833 Status = VariableWriteServiceInitialize ();
834 ASSERT_EFI_ERROR (Status);
835
836 //
837 // Notify the variable wrapper driver the variable write service is ready
838 //
839 Status = gBS->InstallProtocolInterface (
840 &mSmmVariableHandle,
841 &gSmmVariableWriteGuid,
842 EFI_NATIVE_INTERFACE,
843 NULL
844 );
845 ASSERT_EFI_ERROR (Status);
846
847 return EFI_SUCCESS;
848 }
849
850
851 /**
852 Variable Driver main entry point. The Variable driver places the 4 EFI
853 runtime services in the EFI System Table and installs arch protocols
854 for variable read and write services being available. It also registers
855 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
856
857 @param[in] ImageHandle The firmware allocated handle for the EFI image.
858 @param[in] SystemTable A pointer to the EFI System Table.
859
860 @retval EFI_SUCCESS Variable service successfully initialized.
861
862 **/
863 EFI_STATUS
864 EFIAPI
865 VariableServiceInitialize (
866 IN EFI_HANDLE ImageHandle,
867 IN EFI_SYSTEM_TABLE *SystemTable
868 )
869 {
870 EFI_STATUS Status;
871 EFI_HANDLE VariableHandle;
872 VOID *SmmFtwRegistration;
873 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;
874 UINTN Size;
875 VOID *SmmEndOfDxeRegistration;
876
877 //
878 // Variable initialize.
879 //
880 Status = VariableCommonInitialize ();
881 ASSERT_EFI_ERROR (Status);
882
883 //
884 // Install the Smm Variable Protocol on a new handle.
885 //
886 VariableHandle = NULL;
887 Status = gSmst->SmmInstallProtocolInterface (
888 &VariableHandle,
889 &gEfiSmmVariableProtocolGuid,
890 EFI_NATIVE_INTERFACE,
891 &gSmmVariable
892 );
893 ASSERT_EFI_ERROR (Status);
894
895 //
896 // Get SMRAM information
897 //
898 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);
899 ASSERT_EFI_ERROR (Status);
900
901 Size = 0;
902 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);
903 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
904
905 Status = gSmst->SmmAllocatePool (
906 EfiRuntimeServicesData,
907 Size,
908 (VOID **)&mSmramRanges
909 );
910 ASSERT_EFI_ERROR (Status);
911
912 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);
913 ASSERT_EFI_ERROR (Status);
914
915 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
916
917 mVariableBufferPayloadSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize)) +
918 OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - sizeof (VARIABLE_HEADER);
919
920 Status = gSmst->SmmAllocatePool (
921 EfiRuntimeServicesData,
922 mVariableBufferPayloadSize,
923 (VOID **)&mVariableBufferPayload
924 );
925 ASSERT_EFI_ERROR (Status);
926
927 ///
928 /// Register SMM variable SMI handler
929 ///
930 VariableHandle = NULL;
931 Status = gSmst->SmiHandlerRegister (SmmVariableHandler, &gEfiSmmVariableProtocolGuid, &VariableHandle);
932 ASSERT_EFI_ERROR (Status);
933
934 //
935 // Notify the variable wrapper driver the variable service is ready
936 //
937 Status = SystemTable->BootServices->InstallProtocolInterface (
938 &mVariableHandle,
939 &gEfiSmmVariableProtocolGuid,
940 EFI_NATIVE_INTERFACE,
941 &gSmmVariable
942 );
943 ASSERT_EFI_ERROR (Status);
944
945 //
946 // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.
947 //
948 Status = gSmst->SmmRegisterProtocolNotify (
949 &gEfiSmmEndOfDxeProtocolGuid,
950 SmmEndOfDxeCallback,
951 &SmmEndOfDxeRegistration
952 );
953 ASSERT_EFI_ERROR (Status);
954
955 //
956 // Register FtwNotificationEvent () notify function.
957 //
958 Status = gSmst->SmmRegisterProtocolNotify (
959 &gEfiSmmFaultTolerantWriteProtocolGuid,
960 SmmFtwNotificationEvent,
961 &SmmFtwRegistration
962 );
963 ASSERT_EFI_ERROR (Status);
964
965 SmmFtwNotificationEvent (NULL, NULL, NULL);
966
967 return EFI_SUCCESS;
968 }
969
970