]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
MdeModulePkg Variable: Move VariableLock install into SmmVariableReady().
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / VariableSmmRuntimeDxe.c
1 /** @file
2
3 Implement all four UEFI Runtime Variable services for the nonvolatile
4 and volatile storage space and install variable architecture protocol
5 based on SMM variable module.
6
7 Caution: This module requires additional review when modified.
8 This driver will have external input - variable data.
9 This external input must be validated carefully to avoid security issue like
10 buffer overflow, integer overflow.
11
12 RuntimeServiceGetVariable() and RuntimeServiceSetVariable() are external API
13 to receive data buffer. The size should be checked carefully.
14
15 InitCommunicateBuffer() is really function to check the variable data size.
16
17 Copyright (c) 2010 - 2015, 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 #include <PiDxe.h>
28 #include <Protocol/VariableWrite.h>
29 #include <Protocol/Variable.h>
30 #include <Protocol/SmmCommunication.h>
31 #include <Protocol/SmmVariable.h>
32 #include <Protocol/VariableLock.h>
33 #include <Protocol/VarCheck.h>
34
35 #include <Library/UefiBootServicesTableLib.h>
36 #include <Library/UefiRuntimeServicesTableLib.h>
37 #include <Library/MemoryAllocationLib.h>
38 #include <Library/UefiDriverEntryPoint.h>
39 #include <Library/UefiRuntimeLib.h>
40 #include <Library/BaseMemoryLib.h>
41 #include <Library/DebugLib.h>
42 #include <Library/PcdLib.h>
43 #include <Library/UefiLib.h>
44 #include <Library/BaseLib.h>
45
46 #include <Guid/EventGroup.h>
47 #include <Guid/VariableFormat.h>
48 #include <Guid/SmmVariableCommon.h>
49
50 EFI_HANDLE mHandle = NULL;
51 EFI_SMM_VARIABLE_PROTOCOL *mSmmVariable = NULL;
52 EFI_EVENT mVirtualAddressChangeEvent = NULL;
53 EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL;
54 UINT8 *mVariableBuffer = NULL;
55 UINT8 *mVariableBufferPhysical = NULL;
56 UINTN mVariableBufferSize;
57 UINTN mVariableBufferPayloadSize;
58 EFI_LOCK mVariableServicesLock;
59 EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock;
60 EDKII_VAR_CHECK_PROTOCOL mVarCheck;
61
62 /**
63 Acquires lock only at boot time. Simply returns at runtime.
64
65 This is a temperary function that will be removed when
66 EfiAcquireLock() in UefiLib can handle the call in UEFI
67 Runtimer driver in RT phase.
68 It calls EfiAcquireLock() at boot time, and simply returns
69 at runtime.
70
71 @param Lock A pointer to the lock to acquire.
72
73 **/
74 VOID
75 AcquireLockOnlyAtBootTime (
76 IN EFI_LOCK *Lock
77 )
78 {
79 if (!EfiAtRuntime ()) {
80 EfiAcquireLock (Lock);
81 }
82 }
83
84 /**
85 Releases lock only at boot time. Simply returns at runtime.
86
87 This is a temperary function which will be removed when
88 EfiReleaseLock() in UefiLib can handle the call in UEFI
89 Runtimer driver in RT phase.
90 It calls EfiReleaseLock() at boot time and simply returns
91 at runtime.
92
93 @param Lock A pointer to the lock to release.
94
95 **/
96 VOID
97 ReleaseLockOnlyAtBootTime (
98 IN EFI_LOCK *Lock
99 )
100 {
101 if (!EfiAtRuntime ()) {
102 EfiReleaseLock (Lock);
103 }
104 }
105
106 /**
107 Initialize the communicate buffer using DataSize and Function.
108
109 The communicate size is: SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +
110 DataSize.
111
112 Caution: This function may receive untrusted input.
113 The data size external input, so this function will validate it carefully to avoid buffer overflow.
114
115 @param[out] DataPtr Points to the data in the communicate buffer.
116 @param[in] DataSize The data size to send to SMM.
117 @param[in] Function The function number to initialize the communicate header.
118
119 @retval EFI_INVALID_PARAMETER The data size is too big.
120 @retval EFI_SUCCESS Find the specified variable.
121
122 **/
123 EFI_STATUS
124 InitCommunicateBuffer (
125 OUT VOID **DataPtr OPTIONAL,
126 IN UINTN DataSize,
127 IN UINTN Function
128 )
129 {
130 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader;
131 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;
132
133
134 if (DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE > mVariableBufferSize) {
135 return EFI_INVALID_PARAMETER;
136 }
137
138 SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) mVariableBuffer;
139 CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);
140 SmmCommunicateHeader->MessageLength = DataSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;
141
142 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader->Data;
143 SmmVariableFunctionHeader->Function = Function;
144 if (DataPtr != NULL) {
145 *DataPtr = SmmVariableFunctionHeader->Data;
146 }
147
148 return EFI_SUCCESS;
149 }
150
151
152 /**
153 Send the data in communicate buffer to SMM.
154
155 @param[in] DataSize This size of the function header and the data.
156
157 @retval EFI_SUCCESS Success is returned from the functin in SMM.
158 @retval Others Failure is returned from the function in SMM.
159
160 **/
161 EFI_STATUS
162 SendCommunicateBuffer (
163 IN UINTN DataSize
164 )
165 {
166 EFI_STATUS Status;
167 UINTN CommSize;
168 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader;
169 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;
170
171 CommSize = DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;
172 Status = mSmmCommunication->Communicate (mSmmCommunication, mVariableBufferPhysical, &CommSize);
173 ASSERT_EFI_ERROR (Status);
174
175 SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) mVariableBuffer;
176 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)SmmCommunicateHeader->Data;
177 return SmmVariableFunctionHeader->ReturnStatus;
178 }
179
180 /**
181 Mark a variable that will become read-only after leaving the DXE phase of execution.
182
183 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.
184 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.
185 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.
186
187 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked
188 as pending to be read-only.
189 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.
190 Or VariableName is an empty string.
191 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
192 already been signaled.
193 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.
194 **/
195 EFI_STATUS
196 EFIAPI
197 VariableLockRequestToLock (
198 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This,
199 IN CHAR16 *VariableName,
200 IN EFI_GUID *VendorGuid
201 )
202 {
203 EFI_STATUS Status;
204 UINTN VariableNameSize;
205 UINTN PayloadSize;
206 SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *VariableToLock;
207
208 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
209 return EFI_INVALID_PARAMETER;
210 }
211
212 VariableNameSize = StrSize (VariableName);
213 VariableToLock = NULL;
214
215 //
216 // If VariableName exceeds SMM payload limit. Return failure
217 //
218 if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE, Name)) {
219 return EFI_INVALID_PARAMETER;
220 }
221
222 AcquireLockOnlyAtBootTime(&mVariableServicesLock);
223
224 //
225 // Init the communicate buffer. The buffer data size is:
226 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
227 //
228 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE, Name) + VariableNameSize;
229 Status = InitCommunicateBuffer ((VOID **) &VariableToLock, PayloadSize, SMM_VARIABLE_FUNCTION_LOCK_VARIABLE);
230 if (EFI_ERROR (Status)) {
231 goto Done;
232 }
233 ASSERT (VariableToLock != NULL);
234
235 CopyGuid (&VariableToLock->Guid, VendorGuid);
236 VariableToLock->NameSize = VariableNameSize;
237 CopyMem (VariableToLock->Name, VariableName, VariableToLock->NameSize);
238
239 //
240 // Send data to SMM.
241 //
242 Status = SendCommunicateBuffer (PayloadSize);
243
244 Done:
245 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
246 return Status;
247 }
248
249 /**
250 Register SetVariable check handler.
251
252 @param[in] Handler Pointer to check handler.
253
254 @retval EFI_SUCCESS The SetVariable check handler was registered successfully.
255 @retval EFI_INVALID_PARAMETER Handler is NULL.
256 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
257 already been signaled.
258 @retval EFI_OUT_OF_RESOURCES There is not enough resource for the SetVariable check handler register request.
259 @retval EFI_UNSUPPORTED This interface is not implemented.
260 For example, it is unsupported in VarCheck protocol if both VarCheck and SmmVarCheck protocols are present.
261
262 **/
263 EFI_STATUS
264 EFIAPI
265 VarCheckRegisterSetVariableCheckHandler (
266 IN VAR_CHECK_SET_VARIABLE_CHECK_HANDLER Handler
267 )
268 {
269 return EFI_UNSUPPORTED;
270 }
271
272 /**
273 Variable property set.
274
275 @param[in] Name Pointer to the variable name.
276 @param[in] Guid Pointer to the vendor GUID.
277 @param[in] VariableProperty Pointer to the input variable property.
278
279 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was set successfully.
280 @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string,
281 or the fields of VariableProperty are not valid.
282 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
283 already been signaled.
284 @retval EFI_OUT_OF_RESOURCES There is not enough resource for the variable property set request.
285
286 **/
287 EFI_STATUS
288 EFIAPI
289 VarCheckVariablePropertySet (
290 IN CHAR16 *Name,
291 IN EFI_GUID *Guid,
292 IN VAR_CHECK_VARIABLE_PROPERTY *VariableProperty
293 )
294 {
295 EFI_STATUS Status;
296 UINTN VariableNameSize;
297 UINTN PayloadSize;
298 SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProperty;
299
300 if (Name == NULL || Name[0] == 0 || Guid == NULL) {
301 return EFI_INVALID_PARAMETER;
302 }
303
304 if (VariableProperty == NULL) {
305 return EFI_INVALID_PARAMETER;
306 }
307
308 if (VariableProperty->Revision != VAR_CHECK_VARIABLE_PROPERTY_REVISION) {
309 return EFI_INVALID_PARAMETER;
310 }
311
312 VariableNameSize = StrSize (Name);
313 CommVariableProperty = NULL;
314
315 //
316 // If VariableName exceeds SMM payload limit. Return failure
317 //
318 if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) {
319 return EFI_INVALID_PARAMETER;
320 }
321
322 AcquireLockOnlyAtBootTime (&mVariableServicesLock);
323
324 //
325 // Init the communicate buffer. The buffer data size is:
326 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
327 //
328 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) + VariableNameSize;
329 Status = InitCommunicateBuffer ((VOID **) &CommVariableProperty, PayloadSize, SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_SET);
330 if (EFI_ERROR (Status)) {
331 goto Done;
332 }
333 ASSERT (CommVariableProperty != NULL);
334
335 CopyGuid (&CommVariableProperty->Guid, Guid);
336 CopyMem (&CommVariableProperty->VariableProperty, VariableProperty, sizeof (*VariableProperty));
337 CommVariableProperty->NameSize = VariableNameSize;
338 CopyMem (CommVariableProperty->Name, Name, CommVariableProperty->NameSize);
339
340 //
341 // Send data to SMM.
342 //
343 Status = SendCommunicateBuffer (PayloadSize);
344
345 Done:
346 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
347 return Status;
348 }
349
350 /**
351 Variable property get.
352
353 @param[in] Name Pointer to the variable name.
354 @param[in] Guid Pointer to the vendor GUID.
355 @param[out] VariableProperty Pointer to the output variable property.
356
357 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was got successfully.
358 @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string.
359 @retval EFI_NOT_FOUND The property of variable specified by the Name and Guid was not found.
360
361 **/
362 EFI_STATUS
363 EFIAPI
364 VarCheckVariablePropertyGet (
365 IN CHAR16 *Name,
366 IN EFI_GUID *Guid,
367 OUT VAR_CHECK_VARIABLE_PROPERTY *VariableProperty
368 )
369 {
370 EFI_STATUS Status;
371 UINTN VariableNameSize;
372 UINTN PayloadSize;
373 SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProperty;
374
375 if (Name == NULL || Name[0] == 0 || Guid == NULL) {
376 return EFI_INVALID_PARAMETER;
377 }
378
379 if (VariableProperty == NULL) {
380 return EFI_INVALID_PARAMETER;
381 }
382
383 VariableNameSize = StrSize (Name);
384 CommVariableProperty = NULL;
385
386 //
387 // If VariableName exceeds SMM payload limit. Return failure
388 //
389 if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) {
390 return EFI_INVALID_PARAMETER;
391 }
392
393 AcquireLockOnlyAtBootTime (&mVariableServicesLock);
394
395 //
396 // Init the communicate buffer. The buffer data size is:
397 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
398 //
399 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) + VariableNameSize;
400 Status = InitCommunicateBuffer ((VOID **) &CommVariableProperty, PayloadSize, SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET);
401 if (EFI_ERROR (Status)) {
402 goto Done;
403 }
404 ASSERT (CommVariableProperty != NULL);
405
406 CopyGuid (&CommVariableProperty->Guid, Guid);
407 CommVariableProperty->NameSize = VariableNameSize;
408 CopyMem (CommVariableProperty->Name, Name, CommVariableProperty->NameSize);
409
410 //
411 // Send data to SMM.
412 //
413 Status = SendCommunicateBuffer (PayloadSize);
414 if (Status == EFI_SUCCESS) {
415 CopyMem (VariableProperty, &CommVariableProperty->VariableProperty, sizeof (*VariableProperty));
416 }
417
418 Done:
419 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
420 return Status;
421 }
422
423 /**
424 This code finds variable in storage blocks (Volatile or Non-Volatile).
425
426 Caution: This function may receive untrusted input.
427 The data size is external input, so this function will validate it carefully to avoid buffer overflow.
428
429 @param[in] VariableName Name of Variable to be found.
430 @param[in] VendorGuid Variable vendor GUID.
431 @param[out] Attributes Attribute value of the variable found.
432 @param[in, out] DataSize Size of Data found. If size is less than the
433 data, this value contains the required size.
434 @param[out] Data Data pointer.
435
436 @retval EFI_INVALID_PARAMETER Invalid parameter.
437 @retval EFI_SUCCESS Find the specified variable.
438 @retval EFI_NOT_FOUND Not found.
439 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.
440
441 **/
442 EFI_STATUS
443 EFIAPI
444 RuntimeServiceGetVariable (
445 IN CHAR16 *VariableName,
446 IN EFI_GUID *VendorGuid,
447 OUT UINT32 *Attributes OPTIONAL,
448 IN OUT UINTN *DataSize,
449 OUT VOID *Data
450 )
451 {
452 EFI_STATUS Status;
453 UINTN PayloadSize;
454 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;
455 UINTN TempDataSize;
456 UINTN VariableNameSize;
457
458 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
459 return EFI_INVALID_PARAMETER;
460 }
461
462 TempDataSize = *DataSize;
463 VariableNameSize = StrSize (VariableName);
464 SmmVariableHeader = NULL;
465
466 //
467 // If VariableName exceeds SMM payload limit. Return failure
468 //
469 if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {
470 return EFI_INVALID_PARAMETER;
471 }
472
473 AcquireLockOnlyAtBootTime(&mVariableServicesLock);
474
475 //
476 // Init the communicate buffer. The buffer data size is:
477 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
478 //
479 if (TempDataSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize) {
480 //
481 // If output data buffer exceed SMM payload limit. Trim output buffer to SMM payload size
482 //
483 TempDataSize = mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize;
484 }
485 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + VariableNameSize + TempDataSize;
486
487 Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);
488 if (EFI_ERROR (Status)) {
489 goto Done;
490 }
491 ASSERT (SmmVariableHeader != NULL);
492
493 CopyGuid (&SmmVariableHeader->Guid, VendorGuid);
494 SmmVariableHeader->DataSize = TempDataSize;
495 SmmVariableHeader->NameSize = VariableNameSize;
496 if (Attributes == NULL) {
497 SmmVariableHeader->Attributes = 0;
498 } else {
499 SmmVariableHeader->Attributes = *Attributes;
500 }
501 CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);
502
503 //
504 // Send data to SMM.
505 //
506 Status = SendCommunicateBuffer (PayloadSize);
507
508 //
509 // Get data from SMM.
510 //
511 if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {
512 //
513 // SMM CommBuffer DataSize can be a trimed value
514 // Only update DataSize when needed
515 //
516 *DataSize = SmmVariableHeader->DataSize;
517 }
518 if (Attributes != NULL) {
519 *Attributes = SmmVariableHeader->Attributes;
520 }
521
522 if (EFI_ERROR (Status)) {
523 goto Done;
524 }
525
526 if (Data != NULL) {
527 CopyMem (Data, (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize, SmmVariableHeader->DataSize);
528 } else {
529 Status = EFI_INVALID_PARAMETER;
530 }
531
532 Done:
533 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
534 return Status;
535 }
536
537
538 /**
539 This code Finds the Next available variable.
540
541 @param[in, out] VariableNameSize Size of the variable name.
542 @param[in, out] VariableName Pointer to variable name.
543 @param[in, out] VendorGuid Variable Vendor Guid.
544
545 @retval EFI_INVALID_PARAMETER Invalid parameter.
546 @retval EFI_SUCCESS Find the specified variable.
547 @retval EFI_NOT_FOUND Not found.
548 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.
549
550 **/
551 EFI_STATUS
552 EFIAPI
553 RuntimeServiceGetNextVariableName (
554 IN OUT UINTN *VariableNameSize,
555 IN OUT CHAR16 *VariableName,
556 IN OUT EFI_GUID *VendorGuid
557 )
558 {
559 EFI_STATUS Status;
560 UINTN PayloadSize;
561 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *SmmGetNextVariableName;
562 UINTN OutVariableNameSize;
563 UINTN InVariableNameSize;
564
565 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
566 return EFI_INVALID_PARAMETER;
567 }
568
569 OutVariableNameSize = *VariableNameSize;
570 InVariableNameSize = StrSize (VariableName);
571 SmmGetNextVariableName = NULL;
572
573 //
574 // If input string exceeds SMM payload limit. Return failure
575 //
576 if (InVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {
577 return EFI_INVALID_PARAMETER;
578 }
579
580 AcquireLockOnlyAtBootTime(&mVariableServicesLock);
581
582 //
583 // Init the communicate buffer. The buffer data size is:
584 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
585 //
586 if (OutVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {
587 //
588 // If output buffer exceed SMM payload limit. Trim output buffer to SMM payload size
589 //
590 OutVariableNameSize = mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);
591 }
592 //
593 // Payload should be Guid + NameSize + MAX of Input & Output buffer
594 //
595 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + MAX (OutVariableNameSize, InVariableNameSize);
596
597
598 Status = InitCommunicateBuffer ((VOID **)&SmmGetNextVariableName, PayloadSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME);
599 if (EFI_ERROR (Status)) {
600 goto Done;
601 }
602 ASSERT (SmmGetNextVariableName != NULL);
603
604 //
605 // SMM comm buffer->NameSize is buffer size for return string
606 //
607 SmmGetNextVariableName->NameSize = OutVariableNameSize;
608
609 CopyGuid (&SmmGetNextVariableName->Guid, VendorGuid);
610 //
611 // Copy whole string
612 //
613 CopyMem (SmmGetNextVariableName->Name, VariableName, InVariableNameSize);
614 if (OutVariableNameSize > InVariableNameSize) {
615 ZeroMem ((UINT8 *) SmmGetNextVariableName->Name + InVariableNameSize, OutVariableNameSize - InVariableNameSize);
616 }
617
618 //
619 // Send data to SMM
620 //
621 Status = SendCommunicateBuffer (PayloadSize);
622
623 //
624 // Get data from SMM.
625 //
626 if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {
627 //
628 // SMM CommBuffer NameSize can be a trimed value
629 // Only update VariableNameSize when needed
630 //
631 *VariableNameSize = SmmGetNextVariableName->NameSize;
632 }
633 if (EFI_ERROR (Status)) {
634 goto Done;
635 }
636
637 CopyGuid (VendorGuid, &SmmGetNextVariableName->Guid);
638 CopyMem (VariableName, SmmGetNextVariableName->Name, SmmGetNextVariableName->NameSize);
639
640 Done:
641 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
642 return Status;
643 }
644
645 /**
646 This code sets variable in storage blocks (Volatile or Non-Volatile).
647
648 Caution: This function may receive untrusted input.
649 The data size and data are external input, so this function will validate it carefully to avoid buffer overflow.
650
651 @param[in] VariableName Name of Variable to be found.
652 @param[in] VendorGuid Variable vendor GUID.
653 @param[in] Attributes Attribute value of the variable found
654 @param[in] DataSize Size of Data found. If size is less than the
655 data, this value contains the required size.
656 @param[in] Data Data pointer.
657
658 @retval EFI_INVALID_PARAMETER Invalid parameter.
659 @retval EFI_SUCCESS Set successfully.
660 @retval EFI_OUT_OF_RESOURCES Resource not enough to set variable.
661 @retval EFI_NOT_FOUND Not found.
662 @retval EFI_WRITE_PROTECTED Variable is read-only.
663
664 **/
665 EFI_STATUS
666 EFIAPI
667 RuntimeServiceSetVariable (
668 IN CHAR16 *VariableName,
669 IN EFI_GUID *VendorGuid,
670 IN UINT32 Attributes,
671 IN UINTN DataSize,
672 IN VOID *Data
673 )
674 {
675 EFI_STATUS Status;
676 UINTN PayloadSize;
677 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;
678 UINTN VariableNameSize;
679
680 //
681 // Check input parameters.
682 //
683 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
684 return EFI_INVALID_PARAMETER;
685 }
686
687 if (DataSize != 0 && Data == NULL) {
688 return EFI_INVALID_PARAMETER;
689 }
690
691 VariableNameSize = StrSize (VariableName);
692 SmmVariableHeader = NULL;
693
694 //
695 // If VariableName or DataSize exceeds SMM payload limit. Return failure
696 //
697 if ((VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||
698 (DataSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize)){
699 return EFI_INVALID_PARAMETER;
700 }
701
702 AcquireLockOnlyAtBootTime(&mVariableServicesLock);
703
704 //
705 // Init the communicate buffer. The buffer data size is:
706 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
707 //
708 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + VariableNameSize + DataSize;
709 Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_SET_VARIABLE);
710 if (EFI_ERROR (Status)) {
711 goto Done;
712 }
713 ASSERT (SmmVariableHeader != NULL);
714
715 CopyGuid ((EFI_GUID *) &SmmVariableHeader->Guid, VendorGuid);
716 SmmVariableHeader->DataSize = DataSize;
717 SmmVariableHeader->NameSize = VariableNameSize;
718 SmmVariableHeader->Attributes = Attributes;
719 CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);
720 CopyMem ((UINT8 *) SmmVariableHeader->Name + SmmVariableHeader->NameSize, Data, DataSize);
721
722 //
723 // Send data to SMM.
724 //
725 Status = SendCommunicateBuffer (PayloadSize);
726
727 Done:
728 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
729 return Status;
730 }
731
732
733 /**
734 This code returns information about the EFI variables.
735
736 @param[in] Attributes Attributes bitmask to specify the type of variables
737 on which to return information.
738 @param[out] MaximumVariableStorageSize Pointer to the maximum size of the storage space available
739 for the EFI variables associated with the attributes specified.
740 @param[out] RemainingVariableStorageSize Pointer to the remaining size of the storage space available
741 for EFI variables associated with the attributes specified.
742 @param[out] MaximumVariableSize Pointer to the maximum size of an individual EFI variables
743 associated with the attributes specified.
744
745 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
746 @retval EFI_SUCCESS Query successfully.
747 @retval EFI_UNSUPPORTED The attribute is not supported on this platform.
748
749 **/
750 EFI_STATUS
751 EFIAPI
752 RuntimeServiceQueryVariableInfo (
753 IN UINT32 Attributes,
754 OUT UINT64 *MaximumVariableStorageSize,
755 OUT UINT64 *RemainingVariableStorageSize,
756 OUT UINT64 *MaximumVariableSize
757 )
758 {
759 EFI_STATUS Status;
760 UINTN PayloadSize;
761 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *SmmQueryVariableInfo;
762
763 SmmQueryVariableInfo = NULL;
764
765 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {
766 return EFI_INVALID_PARAMETER;
767 }
768
769 AcquireLockOnlyAtBootTime(&mVariableServicesLock);
770
771 //
772 // Init the communicate buffer. The buffer data size is:
773 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize;
774 //
775 PayloadSize = sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO);
776 Status = InitCommunicateBuffer ((VOID **)&SmmQueryVariableInfo, PayloadSize, SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO);
777 if (EFI_ERROR (Status)) {
778 goto Done;
779 }
780 ASSERT (SmmQueryVariableInfo != NULL);
781
782 SmmQueryVariableInfo->Attributes = Attributes;
783
784 //
785 // Send data to SMM.
786 //
787 Status = SendCommunicateBuffer (PayloadSize);
788 if (EFI_ERROR (Status)) {
789 goto Done;
790 }
791
792 //
793 // Get data from SMM.
794 //
795 *MaximumVariableSize = SmmQueryVariableInfo->MaximumVariableSize;
796 *MaximumVariableStorageSize = SmmQueryVariableInfo->MaximumVariableStorageSize;
797 *RemainingVariableStorageSize = SmmQueryVariableInfo->RemainingVariableStorageSize;
798
799 Done:
800 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
801 return Status;
802 }
803
804
805 /**
806 Exit Boot Services Event notification handler.
807
808 Notify SMM variable driver about the event.
809
810 @param[in] Event Event whose notification function is being invoked.
811 @param[in] Context Pointer to the notification function's context.
812
813 **/
814 VOID
815 EFIAPI
816 OnExitBootServices (
817 IN EFI_EVENT Event,
818 IN VOID *Context
819 )
820 {
821 //
822 // Init the communicate buffer. The buffer data size is:
823 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
824 //
825 InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE);
826
827 //
828 // Send data to SMM.
829 //
830 SendCommunicateBuffer (0);
831 }
832
833
834 /**
835 On Ready To Boot Services Event notification handler.
836
837 Notify SMM variable driver about the event.
838
839 @param[in] Event Event whose notification function is being invoked
840 @param[in] Context Pointer to the notification function's context
841
842 **/
843 VOID
844 EFIAPI
845 OnReadyToBoot (
846 IN EFI_EVENT Event,
847 IN VOID *Context
848 )
849 {
850 //
851 // Init the communicate buffer. The buffer data size is:
852 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
853 //
854 InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_READY_TO_BOOT);
855
856 //
857 // Send data to SMM.
858 //
859 SendCommunicateBuffer (0);
860 }
861
862
863 /**
864 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
865
866 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
867 It convers pointer to new virtual address.
868
869 @param[in] Event Event whose notification function is being invoked.
870 @param[in] Context Pointer to the notification function's context.
871
872 **/
873 VOID
874 EFIAPI
875 VariableAddressChangeEvent (
876 IN EFI_EVENT Event,
877 IN VOID *Context
878 )
879 {
880 EfiConvertPointer (0x0, (VOID **) &mVariableBuffer);
881 EfiConvertPointer (0x0, (VOID **) &mSmmCommunication);
882 }
883
884
885 /**
886 Initialize variable service and install Variable Architectural protocol.
887
888 @param[in] Event Event whose notification function is being invoked.
889 @param[in] Context Pointer to the notification function's context.
890
891 **/
892 VOID
893 EFIAPI
894 SmmVariableReady (
895 IN EFI_EVENT Event,
896 IN VOID *Context
897 )
898 {
899 EFI_STATUS Status;
900
901 Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID **)&mSmmVariable);
902 if (EFI_ERROR (Status)) {
903 return;
904 }
905
906 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);
907 ASSERT_EFI_ERROR (Status);
908
909 //
910 // Allocate memory for variable communicate buffer.
911 //
912 mVariableBufferPayloadSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize)) +
913 OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) - sizeof (VARIABLE_HEADER);
914 mVariableBufferSize = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + mVariableBufferPayloadSize;
915 mVariableBuffer = AllocateRuntimePool (mVariableBufferSize);
916 ASSERT (mVariableBuffer != NULL);
917
918 //
919 // Save the buffer physical address used for SMM conmunication.
920 //
921 mVariableBufferPhysical = mVariableBuffer;
922
923 gRT->GetVariable = RuntimeServiceGetVariable;
924 gRT->GetNextVariableName = RuntimeServiceGetNextVariableName;
925 gRT->SetVariable = RuntimeServiceSetVariable;
926 gRT->QueryVariableInfo = RuntimeServiceQueryVariableInfo;
927
928 //
929 // Install the Variable Architectural Protocol on a new handle.
930 //
931 Status = gBS->InstallProtocolInterface (
932 &mHandle,
933 &gEfiVariableArchProtocolGuid,
934 EFI_NATIVE_INTERFACE,
935 NULL
936 );
937 ASSERT_EFI_ERROR (Status);
938
939 mVariableLock.RequestToLock = VariableLockRequestToLock;
940 Status = gBS->InstallMultipleProtocolInterfaces (
941 &mHandle,
942 &gEdkiiVariableLockProtocolGuid,
943 &mVariableLock,
944 NULL
945 );
946 ASSERT_EFI_ERROR (Status);
947
948 mVarCheck.RegisterSetVariableCheckHandler = VarCheckRegisterSetVariableCheckHandler;
949 mVarCheck.VariablePropertySet = VarCheckVariablePropertySet;
950 mVarCheck.VariablePropertyGet = VarCheckVariablePropertyGet;
951 Status = gBS->InstallMultipleProtocolInterfaces (
952 &mHandle,
953 &gEdkiiVarCheckProtocolGuid,
954 &mVarCheck,
955 NULL
956 );
957 ASSERT_EFI_ERROR (Status);
958 }
959
960
961 /**
962 SMM Non-Volatile variable write service is ready notify event handler.
963
964 @param[in] Event Event whose notification function is being invoked.
965 @param[in] Context Pointer to the notification function's context.
966
967 **/
968 VOID
969 EFIAPI
970 SmmVariableWriteReady (
971 IN EFI_EVENT Event,
972 IN VOID *Context
973 )
974 {
975 EFI_STATUS Status;
976 VOID *ProtocolOps;
977
978 //
979 // Check whether the protocol is installed or not.
980 //
981 Status = gBS->LocateProtocol (&gSmmVariableWriteGuid, NULL, (VOID **) &ProtocolOps);
982 if (EFI_ERROR (Status)) {
983 return;
984 }
985
986 Status = gBS->InstallProtocolInterface (
987 &mHandle,
988 &gEfiVariableWriteArchProtocolGuid,
989 EFI_NATIVE_INTERFACE,
990 NULL
991 );
992 ASSERT_EFI_ERROR (Status);
993 }
994
995
996 /**
997 Variable Driver main entry point. The Variable driver places the 4 EFI
998 runtime services in the EFI System Table and installs arch protocols
999 for variable read and write services being available. It also registers
1000 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
1001
1002 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1003 @param[in] SystemTable A pointer to the EFI System Table.
1004
1005 @retval EFI_SUCCESS Variable service successfully initialized.
1006
1007 **/
1008 EFI_STATUS
1009 EFIAPI
1010 VariableSmmRuntimeInitialize (
1011 IN EFI_HANDLE ImageHandle,
1012 IN EFI_SYSTEM_TABLE *SystemTable
1013 )
1014 {
1015 VOID *SmmVariableRegistration;
1016 VOID *SmmVariableWriteRegistration;
1017 EFI_EVENT OnReadyToBootEvent;
1018 EFI_EVENT ExitBootServiceEvent;
1019 EFI_EVENT LegacyBootEvent;
1020
1021 EfiInitializeLock (&mVariableServicesLock, TPL_NOTIFY);
1022
1023 //
1024 // Smm variable service is ready
1025 //
1026 EfiCreateProtocolNotifyEvent (
1027 &gEfiSmmVariableProtocolGuid,
1028 TPL_CALLBACK,
1029 SmmVariableReady,
1030 NULL,
1031 &SmmVariableRegistration
1032 );
1033
1034 //
1035 // Smm Non-Volatile variable write service is ready
1036 //
1037 EfiCreateProtocolNotifyEvent (
1038 &gSmmVariableWriteGuid,
1039 TPL_CALLBACK,
1040 SmmVariableWriteReady,
1041 NULL,
1042 &SmmVariableWriteRegistration
1043 );
1044
1045 //
1046 // Register the event to reclaim variable for OS usage.
1047 //
1048 EfiCreateEventReadyToBootEx (
1049 TPL_NOTIFY,
1050 OnReadyToBoot,
1051 NULL,
1052 &OnReadyToBootEvent
1053 );
1054
1055 //
1056 // Register the event to inform SMM variable that it is at runtime.
1057 //
1058 gBS->CreateEventEx (
1059 EVT_NOTIFY_SIGNAL,
1060 TPL_NOTIFY,
1061 OnExitBootServices,
1062 NULL,
1063 &gEfiEventExitBootServicesGuid,
1064 &ExitBootServiceEvent
1065 );
1066
1067 //
1068 // Register the event to inform SMM variable that it is at runtime for legacy boot.
1069 // Reuse OnExitBootServices() here.
1070 //
1071 EfiCreateEventLegacyBootEx(
1072 TPL_NOTIFY,
1073 OnExitBootServices,
1074 NULL,
1075 &LegacyBootEvent
1076 );
1077
1078 //
1079 // Register the event to convert the pointer for runtime.
1080 //
1081 gBS->CreateEventEx (
1082 EVT_NOTIFY_SIGNAL,
1083 TPL_NOTIFY,
1084 VariableAddressChangeEvent,
1085 NULL,
1086 &gEfiEventVirtualAddressChangeGuid,
1087 &mVirtualAddressChangeEvent
1088 );
1089
1090 return EFI_SUCCESS;
1091 }
1092