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