]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmRuntimeDxe.c
1. Fix TOCTOU issue in VariableSmm, FtwSmm, FpdtSmm, SmmCorePerformance SMM handler...
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / RuntimeDxe / VariableSmmRuntimeDxe.c
CommitLineData
0c18794e 1/** @file\r
2 Implement all four UEFI Runtime Variable services for the nonvolatile\r
3 and volatile storage space and install variable architecture protocol\r
4 based on SMM variable module.\r
5\r
dc204d5a
JY
6 Caution: This module requires additional review when modified.\r
7 This driver will have external input - variable data.\r
8 This external input must be validated carefully to avoid security issue like\r
9 buffer overflow, integer overflow.\r
10\r
11 RuntimeServiceGetVariable() and RuntimeServiceSetVariable() are external API\r
12 to receive data buffer. The size should be checked carefully.\r
13\r
14 InitCommunicateBuffer() is really function to check the variable data size.\r
15\r
aab9212f 16Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.<BR>\r
0c18794e 17This program and the accompanying materials \r
18are licensed and made available under the terms and conditions of the BSD License \r
19which accompanies this distribution. The full text of the license may be found at \r
20http://opensource.org/licenses/bsd-license.php\r
21\r
22THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
23WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
24\r
25**/\r
26\r
27#include <PiDxe.h>\r
28#include <Protocol/VariableWrite.h>\r
29#include <Protocol/Variable.h>\r
30#include <Protocol/SmmCommunication.h>\r
31#include <Protocol/SmmVariable.h>\r
32\r
33#include <Library/UefiBootServicesTableLib.h>\r
34#include <Library/UefiRuntimeServicesTableLib.h>\r
35#include <Library/MemoryAllocationLib.h>\r
36#include <Library/UefiDriverEntryPoint.h>\r
37#include <Library/UefiRuntimeLib.h>\r
38#include <Library/BaseMemoryLib.h>\r
39#include <Library/DebugLib.h>\r
40#include <Library/PcdLib.h>\r
41#include <Library/UefiLib.h>\r
42#include <Library/BaseLib.h>\r
43\r
44#include <Guid/EventGroup.h>\r
45#include <Guid/AuthenticatedVariableFormat.h>\r
46#include <Guid/SmmVariableCommon.h>\r
47\r
48EFI_HANDLE mHandle = NULL; \r
49EFI_SMM_VARIABLE_PROTOCOL *mSmmVariable = NULL;\r
50EFI_EVENT mVirtualAddressChangeEvent = NULL;\r
51EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL;\r
52UINT8 *mVariableBuffer = NULL;\r
53UINT8 *mVariableBufferPhysical = NULL;\r
54UINTN mVariableBufferSize;\r
5e5bb2a9 55UINTN mVariableBufferPayloadSize;\r
6ed1ec59 56EFI_LOCK mVariableServicesLock;\r
0c18794e 57\r
6ed1ec59
SZ
58/**\r
59 Acquires lock only at boot time. Simply returns at runtime.\r
60\r
61 This is a temperary function that will be removed when\r
62 EfiAcquireLock() in UefiLib can handle the call in UEFI\r
63 Runtimer driver in RT phase.\r
64 It calls EfiAcquireLock() at boot time, and simply returns\r
65 at runtime.\r
66\r
67 @param Lock A pointer to the lock to acquire.\r
68\r
69**/\r
70VOID\r
71AcquireLockOnlyAtBootTime (\r
72 IN EFI_LOCK *Lock\r
73 )\r
74{\r
75 if (!EfiAtRuntime ()) {\r
76 EfiAcquireLock (Lock);\r
77 }\r
78}\r
79\r
80/**\r
81 Releases lock only at boot time. Simply returns at runtime.\r
82\r
83 This is a temperary function which will be removed when\r
84 EfiReleaseLock() in UefiLib can handle the call in UEFI\r
85 Runtimer driver in RT phase.\r
86 It calls EfiReleaseLock() at boot time and simply returns\r
87 at runtime.\r
88\r
89 @param Lock A pointer to the lock to release.\r
90\r
91**/\r
92VOID\r
93ReleaseLockOnlyAtBootTime (\r
94 IN EFI_LOCK *Lock\r
95 )\r
96{\r
97 if (!EfiAtRuntime ()) {\r
98 EfiReleaseLock (Lock);\r
99 }\r
100}\r
0c18794e 101\r
102/**\r
103 Initialize the communicate buffer using DataSize and Function.\r
104\r
105 The communicate size is: SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +\r
106 DataSize.\r
107\r
dc204d5a
JY
108 Caution: This function may receive untrusted input.\r
109 The data size external input, so this function will validate it carefully to avoid buffer overflow.\r
110\r
0c18794e 111 @param[out] DataPtr Points to the data in the communicate buffer.\r
112 @param[in] DataSize The data size to send to SMM.\r
113 @param[in] Function The function number to initialize the communicate header.\r
114 \r
115 @retval EFI_INVALID_PARAMETER The data size is too big.\r
116 @retval EFI_SUCCESS Find the specified variable.\r
117\r
118**/\r
119EFI_STATUS\r
120InitCommunicateBuffer (\r
121 OUT VOID **DataPtr OPTIONAL,\r
122 IN UINTN DataSize,\r
123 IN UINTN Function\r
124 )\r
125{\r
126 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader; \r
127 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader; \r
128\r
129 \r
130 if (DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE > mVariableBufferSize) {\r
131 return EFI_INVALID_PARAMETER;\r
132 }\r
133\r
134 SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) mVariableBuffer;\r
135 CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);\r
136 SmmCommunicateHeader->MessageLength = DataSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
137 \r
138 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader->Data;\r
139 SmmVariableFunctionHeader->Function = Function;\r
140 if (DataPtr != NULL) {\r
141 *DataPtr = SmmVariableFunctionHeader->Data;\r
142 }\r
143\r
144 return EFI_SUCCESS;\r
145}\r
146\r
147\r
148/**\r
149 Send the data in communicate buffer to SMM.\r
150\r
151 @param[in] DataSize This size of the function header and the data.\r
152\r
153 @retval EFI_SUCCESS Success is returned from the functin in SMM.\r
154 @retval Others Failure is returned from the function in SMM. \r
155 \r
156**/\r
157EFI_STATUS\r
158SendCommunicateBuffer (\r
159 IN UINTN DataSize\r
160 )\r
161{\r
162 EFI_STATUS Status;\r
163 UINTN CommSize;\r
164 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader; \r
165 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;\r
166 \r
167 CommSize = DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
168 Status = mSmmCommunication->Communicate (mSmmCommunication, mVariableBufferPhysical, &CommSize);\r
169 ASSERT_EFI_ERROR (Status);\r
170\r
171 SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) mVariableBuffer;\r
172 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)SmmCommunicateHeader->Data;\r
173 return SmmVariableFunctionHeader->ReturnStatus;\r
174}\r
175\r
176\r
177/**\r
178 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
179\r
dc204d5a
JY
180 Caution: This function may receive untrusted input.\r
181 The data size is external input, so this function will validate it carefully to avoid buffer overflow.\r
182\r
0c18794e 183 @param[in] VariableName Name of Variable to be found.\r
184 @param[in] VendorGuid Variable vendor GUID.\r
185 @param[out] Attributes Attribute value of the variable found.\r
186 @param[in, out] DataSize Size of Data found. If size is less than the\r
187 data, this value contains the required size.\r
188 @param[out] Data Data pointer.\r
189 \r
190 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
191 @retval EFI_SUCCESS Find the specified variable.\r
192 @retval EFI_NOT_FOUND Not found.\r
193 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
194\r
195**/\r
196EFI_STATUS\r
197EFIAPI\r
198RuntimeServiceGetVariable (\r
199 IN CHAR16 *VariableName,\r
200 IN EFI_GUID *VendorGuid,\r
201 OUT UINT32 *Attributes OPTIONAL,\r
202 IN OUT UINTN *DataSize,\r
203 OUT VOID *Data\r
204 )\r
205{\r
206 EFI_STATUS Status;\r
207 UINTN PayloadSize;\r
208 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;\r
3a146f2a 209 UINTN TempDataSize;\r
9d00d20e 210 UINTN VariableNameSize;\r
0c18794e 211\r
212 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
213 return EFI_INVALID_PARAMETER;\r
214 }\r
215\r
216 if ((*DataSize != 0) && (Data == NULL)) {\r
217 return EFI_INVALID_PARAMETER;\r
218 }\r
6ed1ec59 219\r
3a146f2a 220 TempDataSize = *DataSize;\r
9d00d20e 221 VariableNameSize = StrSize (VariableName);\r
3a146f2a 222\r
223 //\r
224 // If VariableName exceeds SMM payload limit. Return failure\r
225 //\r
5e5bb2a9 226 if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {\r
3588bb35
SZ
227 return EFI_INVALID_PARAMETER;\r
228 }\r
229\r
6ed1ec59
SZ
230 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
231\r
0c18794e 232 //\r
233 // Init the communicate buffer. The buffer data size is:\r
6ed1ec59 234 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
0c18794e 235 //\r
5e5bb2a9 236 if (TempDataSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize) {\r
3a146f2a 237 //\r
238 // If output data buffer exceed SMM payload limit. Trim output buffer to SMM payload size\r
239 //\r
5e5bb2a9 240 TempDataSize = mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize;\r
3a146f2a 241 }\r
9d00d20e 242 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + VariableNameSize + TempDataSize;\r
3a146f2a 243\r
0c18794e 244 Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);\r
245 if (EFI_ERROR (Status)) {\r
6ed1ec59 246 goto Done;\r
0c18794e 247 }\r
248 ASSERT (SmmVariableHeader != NULL);\r
249\r
250 CopyGuid (&SmmVariableHeader->Guid, VendorGuid);\r
3a146f2a 251 SmmVariableHeader->DataSize = TempDataSize;\r
9d00d20e 252 SmmVariableHeader->NameSize = VariableNameSize;\r
0c18794e 253 if (Attributes == NULL) {\r
254 SmmVariableHeader->Attributes = 0;\r
255 } else {\r
256 SmmVariableHeader->Attributes = *Attributes;\r
257 }\r
258 CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);\r
259\r
260 //\r
261 // Send data to SMM.\r
262 //\r
263 Status = SendCommunicateBuffer (PayloadSize);\r
264\r
265 //\r
266 // Get data from SMM.\r
267 //\r
3a146f2a 268 if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {\r
269 //\r
270 // SMM CommBuffer DataSize can be a trimed value\r
271 // Only update DataSize when needed\r
272 //\r
273 *DataSize = SmmVariableHeader->DataSize;\r
274 }\r
0c18794e 275 if (Attributes != NULL) {\r
276 *Attributes = SmmVariableHeader->Attributes;\r
277 }\r
278\r
279 if (EFI_ERROR (Status)) {\r
6ed1ec59 280 goto Done;\r
0c18794e 281 }\r
282\r
283 CopyMem (Data, (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize, SmmVariableHeader->DataSize);\r
284\r
6ed1ec59
SZ
285Done:\r
286 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
0c18794e 287 return Status;\r
288}\r
289\r
290\r
291/**\r
292 This code Finds the Next available variable.\r
293\r
294 @param[in, out] VariableNameSize Size of the variable name.\r
295 @param[in, out] VariableName Pointer to variable name.\r
296 @param[in, out] VendorGuid Variable Vendor Guid.\r
297\r
298 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
299 @retval EFI_SUCCESS Find the specified variable.\r
300 @retval EFI_NOT_FOUND Not found.\r
301 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
302\r
303**/\r
304EFI_STATUS\r
305EFIAPI\r
306RuntimeServiceGetNextVariableName (\r
307 IN OUT UINTN *VariableNameSize,\r
308 IN OUT CHAR16 *VariableName,\r
309 IN OUT EFI_GUID *VendorGuid\r
310 )\r
311{\r
312 EFI_STATUS Status;\r
313 UINTN PayloadSize;\r
314 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *SmmGetNextVariableName;\r
9d00d20e
SZ
315 UINTN OutVariableNameSize;\r
316 UINTN InVariableNameSize;\r
0c18794e 317\r
318 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
319 return EFI_INVALID_PARAMETER;\r
320 }\r
6ed1ec59 321\r
9d00d20e
SZ
322 OutVariableNameSize = *VariableNameSize;\r
323 InVariableNameSize = StrSize (VariableName);\r
0c55190f 324\r
325 //\r
326 // If input string exceeds SMM payload limit. Return failure\r
327 //\r
5e5bb2a9 328 if (InVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
3588bb35
SZ
329 return EFI_INVALID_PARAMETER;\r
330 }\r
331\r
6ed1ec59
SZ
332 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
333\r
0c18794e 334 //\r
335 // Init the communicate buffer. The buffer data size is:\r
336 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
337 //\r
5e5bb2a9 338 if (OutVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
0c55190f 339 //\r
340 // If output buffer exceed SMM payload limit. Trim output buffer to SMM payload size\r
341 //\r
5e5bb2a9 342 OutVariableNameSize = mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);\r
0c55190f 343 }\r
344 //\r
345 // Payload should be Guid + NameSize + MAX of Input & Output buffer\r
346 //\r
9d00d20e 347 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + MAX (OutVariableNameSize, InVariableNameSize);\r
0c55190f 348\r
0c18794e 349 Status = InitCommunicateBuffer ((VOID **)&SmmGetNextVariableName, PayloadSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME);\r
350 if (EFI_ERROR (Status)) {\r
6ed1ec59 351 goto Done;\r
0c18794e 352 }\r
353 ASSERT (SmmGetNextVariableName != NULL);\r
354\r
0c55190f 355 //\r
356 // SMM comm buffer->NameSize is buffer size for return string\r
357 //\r
9d00d20e 358 SmmGetNextVariableName->NameSize = OutVariableNameSize;\r
0c55190f 359\r
0c18794e 360 CopyGuid (&SmmGetNextVariableName->Guid, VendorGuid);\r
0c55190f 361 //\r
362 // Copy whole string\r
363 //\r
9d00d20e
SZ
364 CopyMem (SmmGetNextVariableName->Name, VariableName, InVariableNameSize);\r
365 if (OutVariableNameSize > InVariableNameSize) {\r
366 ZeroMem ((UINT8 *) SmmGetNextVariableName->Name + InVariableNameSize, OutVariableNameSize - InVariableNameSize);\r
367 }\r
0c18794e 368\r
369 //\r
370 // Send data to SMM\r
371 //\r
372 Status = SendCommunicateBuffer (PayloadSize);\r
373\r
374 //\r
375 // Get data from SMM.\r
376 //\r
9d00d20e
SZ
377 if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {\r
378 //\r
379 // SMM CommBuffer NameSize can be a trimed value\r
380 // Only update VariableNameSize when needed\r
381 //\r
382 *VariableNameSize = SmmGetNextVariableName->NameSize;\r
383 }\r
0c18794e 384 if (EFI_ERROR (Status)) {\r
6ed1ec59 385 goto Done;\r
0c18794e 386 }\r
387 \r
388 CopyGuid (VendorGuid, &SmmGetNextVariableName->Guid);\r
389 CopyMem (VariableName, SmmGetNextVariableName->Name, SmmGetNextVariableName->NameSize); \r
390\r
6ed1ec59
SZ
391Done:\r
392 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
0c18794e 393 return Status;\r
394}\r
395\r
396/**\r
397 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
398\r
dc204d5a
JY
399 Caution: This function may receive untrusted input.\r
400 The data size and data are external input, so this function will validate it carefully to avoid buffer overflow.\r
401\r
0c18794e 402 @param[in] VariableName Name of Variable to be found.\r
403 @param[in] VendorGuid Variable vendor GUID.\r
404 @param[in] Attributes Attribute value of the variable found\r
405 @param[in] DataSize Size of Data found. If size is less than the\r
406 data, this value contains the required size.\r
407 @param[in] Data Data pointer.\r
408\r
409 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
410 @retval EFI_SUCCESS Set successfully.\r
411 @retval EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
412 @retval EFI_NOT_FOUND Not found.\r
413 @retval EFI_WRITE_PROTECTED Variable is read-only.\r
414\r
415**/\r
416EFI_STATUS\r
417EFIAPI\r
418RuntimeServiceSetVariable (\r
419 IN CHAR16 *VariableName,\r
420 IN EFI_GUID *VendorGuid,\r
421 IN UINT32 Attributes,\r
422 IN UINTN DataSize,\r
423 IN VOID *Data\r
424 )\r
425{\r
426 EFI_STATUS Status;\r
427 UINTN PayloadSize; \r
428 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;\r
9d00d20e 429 UINTN VariableNameSize;\r
0c18794e 430 \r
431 //\r
432 // Check input parameters.\r
433 //\r
434 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
435 return EFI_INVALID_PARAMETER;\r
436 } \r
437\r
438 if (DataSize != 0 && Data == NULL) {\r
439 return EFI_INVALID_PARAMETER;\r
440 }\r
6ed1ec59 441\r
9d00d20e 442 VariableNameSize = StrSize (VariableName);\r
3588bb35 443\r
5e5bb2a9
SZ
444 //\r
445 // If VariableName or DataSize exceeds SMM payload limit. Return failure\r
446 //\r
447 if ((VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||\r
448 (DataSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize)){\r
56251c66 449 return EFI_INVALID_PARAMETER;\r
450 }\r
451\r
6ed1ec59 452 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
56251c66 453\r
0c18794e 454 //\r
455 // Init the communicate buffer. The buffer data size is:\r
456 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
457 //\r
9d00d20e 458 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + VariableNameSize + DataSize;\r
0c18794e 459 Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_SET_VARIABLE);\r
460 if (EFI_ERROR (Status)) {\r
6ed1ec59 461 goto Done;\r
0c18794e 462 }\r
463 ASSERT (SmmVariableHeader != NULL);\r
464\r
465 CopyGuid ((EFI_GUID *) &SmmVariableHeader->Guid, VendorGuid);\r
466 SmmVariableHeader->DataSize = DataSize;\r
9d00d20e 467 SmmVariableHeader->NameSize = VariableNameSize;\r
0c18794e 468 SmmVariableHeader->Attributes = Attributes;\r
469 CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);\r
470 CopyMem ((UINT8 *) SmmVariableHeader->Name + SmmVariableHeader->NameSize, Data, DataSize);\r
471\r
472 //\r
473 // Send data to SMM.\r
474 //\r
475 Status = SendCommunicateBuffer (PayloadSize);\r
6ed1ec59
SZ
476\r
477Done:\r
478 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
0c18794e 479 return Status;\r
480}\r
481\r
482\r
483/**\r
484 This code returns information about the EFI variables.\r
485\r
486 @param[in] Attributes Attributes bitmask to specify the type of variables\r
487 on which to return information.\r
488 @param[out] MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
489 for the EFI variables associated with the attributes specified.\r
490 @param[out] RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
491 for EFI variables associated with the attributes specified.\r
492 @param[out] MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
493 associated with the attributes specified.\r
494\r
495 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
496 @retval EFI_SUCCESS Query successfully.\r
497 @retval EFI_UNSUPPORTED The attribute is not supported on this platform.\r
498\r
499**/\r
500EFI_STATUS\r
501EFIAPI\r
502RuntimeServiceQueryVariableInfo (\r
503 IN UINT32 Attributes,\r
504 OUT UINT64 *MaximumVariableStorageSize,\r
505 OUT UINT64 *RemainingVariableStorageSize,\r
506 OUT UINT64 *MaximumVariableSize\r
507 )\r
508{\r
509 EFI_STATUS Status;\r
510 UINTN PayloadSize;\r
511 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *SmmQueryVariableInfo;\r
512\r
513 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
514 return EFI_INVALID_PARAMETER;\r
515 }\r
6ed1ec59
SZ
516\r
517 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
518\r
0c18794e 519 //\r
520 // Init the communicate buffer. The buffer data size is:\r
521 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize;\r
522 //\r
523 PayloadSize = sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO);\r
524 Status = InitCommunicateBuffer ((VOID **)&SmmQueryVariableInfo, PayloadSize, SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO);\r
525 if (EFI_ERROR (Status)) {\r
6ed1ec59 526 goto Done;\r
0c18794e 527 }\r
528 ASSERT (SmmQueryVariableInfo != NULL);\r
529\r
530 SmmQueryVariableInfo->Attributes = Attributes;\r
531\r
532 //\r
533 // Send data to SMM.\r
534 //\r
535 Status = SendCommunicateBuffer (PayloadSize);\r
536 if (EFI_ERROR (Status)) {\r
6ed1ec59 537 goto Done;\r
0c18794e 538 }\r
539\r
540 //\r
541 // Get data from SMM.\r
542 //\r
543 *MaximumVariableSize = SmmQueryVariableInfo->MaximumVariableSize;\r
544 *MaximumVariableStorageSize = SmmQueryVariableInfo->MaximumVariableStorageSize;\r
545 *RemainingVariableStorageSize = SmmQueryVariableInfo->RemainingVariableStorageSize; \r
6ed1ec59
SZ
546\r
547Done:\r
548 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
aab9212f 549 return Status;\r
0c18794e 550}\r
551\r
552\r
553/**\r
554 Exit Boot Services Event notification handler.\r
555\r
556 Notify SMM variable driver about the event.\r
557\r
558 @param[in] Event Event whose notification function is being invoked.\r
559 @param[in] Context Pointer to the notification function's context.\r
560\r
561**/\r
562VOID\r
563EFIAPI\r
564OnExitBootServices (\r
565 IN EFI_EVENT Event,\r
566 IN VOID *Context\r
567 )\r
568{\r
569 //\r
570 // Init the communicate buffer. The buffer data size is:\r
571 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.\r
572 //\r
573 InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE); \r
574\r
575 //\r
576 // Send data to SMM.\r
577 //\r
578 SendCommunicateBuffer (0);\r
579}\r
580\r
581\r
582/**\r
583 On Ready To Boot Services Event notification handler.\r
584\r
585 Notify SMM variable driver about the event.\r
586\r
587 @param[in] Event Event whose notification function is being invoked\r
588 @param[in] Context Pointer to the notification function's context\r
589\r
590**/\r
591VOID\r
592EFIAPI\r
593OnReadyToBoot (\r
594 IN EFI_EVENT Event,\r
595 IN VOID *Context\r
596 )\r
597{\r
598 //\r
599 // Init the communicate buffer. The buffer data size is:\r
600 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.\r
601 //\r
602 InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_READY_TO_BOOT);\r
603 \r
604 //\r
605 // Send data to SMM.\r
606 //\r
607 SendCommunicateBuffer (0);\r
608}\r
609\r
610\r
611/**\r
612 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
613\r
614 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
615 It convers pointer to new virtual address.\r
616\r
617 @param[in] Event Event whose notification function is being invoked.\r
618 @param[in] Context Pointer to the notification function's context.\r
619\r
620**/\r
621VOID\r
622EFIAPI\r
623VariableAddressChangeEvent (\r
624 IN EFI_EVENT Event,\r
625 IN VOID *Context\r
626 )\r
627{\r
628 EfiConvertPointer (0x0, (VOID **) &mVariableBuffer);\r
629 EfiConvertPointer (0x0, (VOID **) &mSmmCommunication);\r
630}\r
631\r
632\r
633/**\r
634 Initialize variable service and install Variable Architectural protocol.\r
635\r
636 @param[in] Event Event whose notification function is being invoked.\r
637 @param[in] Context Pointer to the notification function's context.\r
638 \r
639**/\r
640VOID\r
641EFIAPI\r
642SmmVariableReady (\r
643 IN EFI_EVENT Event,\r
644 IN VOID *Context\r
645 )\r
646{\r
647 EFI_STATUS Status;\r
648\r
649 Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID **)&mSmmVariable);\r
650 if (EFI_ERROR (Status)) {\r
651 return;\r
652 }\r
653 \r
654 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);\r
655 ASSERT_EFI_ERROR (Status);\r
656 \r
657 //\r
5e5bb2a9 658 // Allocate memory for variable communicate buffer.\r
0c18794e 659 //\r
5e5bb2a9
SZ
660 mVariableBufferPayloadSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize)) +\r
661 OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - sizeof (VARIABLE_HEADER);\r
662 mVariableBufferSize = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + mVariableBufferPayloadSize;\r
0c18794e 663 mVariableBuffer = AllocateRuntimePool (mVariableBufferSize);\r
664 ASSERT (mVariableBuffer != NULL);\r
665\r
666 //\r
667 // Save the buffer physical address used for SMM conmunication.\r
668 //\r
669 mVariableBufferPhysical = mVariableBuffer;\r
670\r
671 gRT->GetVariable = RuntimeServiceGetVariable;\r
672 gRT->GetNextVariableName = RuntimeServiceGetNextVariableName;\r
673 gRT->SetVariable = RuntimeServiceSetVariable;\r
674 gRT->QueryVariableInfo = RuntimeServiceQueryVariableInfo;\r
675 \r
676 //\r
677 // Install the Variable Architectural Protocol on a new handle.\r
678 //\r
679 Status = gBS->InstallProtocolInterface (\r
680 &mHandle,\r
681 &gEfiVariableArchProtocolGuid, \r
682 EFI_NATIVE_INTERFACE,\r
683 NULL\r
684 );\r
685 ASSERT_EFI_ERROR (Status);\r
686}\r
687\r
688\r
689/**\r
690 SMM Non-Volatile variable write service is ready notify event handler.\r
691\r
692 @param[in] Event Event whose notification function is being invoked.\r
693 @param[in] Context Pointer to the notification function's context.\r
694 \r
695**/\r
696VOID\r
697EFIAPI\r
698SmmVariableWriteReady (\r
699 IN EFI_EVENT Event,\r
700 IN VOID *Context\r
701 )\r
702{\r
703 EFI_STATUS Status;\r
704 VOID *ProtocolOps;\r
705\r
706 //\r
707 // Check whether the protocol is installed or not.\r
708 //\r
709 Status = gBS->LocateProtocol (&gSmmVariableWriteGuid, NULL, (VOID **) &ProtocolOps);\r
710 if (EFI_ERROR (Status)) {\r
711 return;\r
712 }\r
713 \r
714 Status = gBS->InstallProtocolInterface (\r
715 &mHandle,\r
716 &gEfiVariableWriteArchProtocolGuid, \r
717 EFI_NATIVE_INTERFACE,\r
718 NULL\r
719 );\r
720 ASSERT_EFI_ERROR (Status); \r
721}\r
722\r
723\r
724/**\r
725 Variable Driver main entry point. The Variable driver places the 4 EFI\r
726 runtime services in the EFI System Table and installs arch protocols \r
727 for variable read and write services being available. It also registers\r
728 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
729\r
730 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
731 @param[in] SystemTable A pointer to the EFI System Table.\r
732 \r
733 @retval EFI_SUCCESS Variable service successfully initialized.\r
734\r
735**/\r
736EFI_STATUS\r
737EFIAPI\r
738VariableSmmRuntimeInitialize (\r
739 IN EFI_HANDLE ImageHandle,\r
740 IN EFI_SYSTEM_TABLE *SystemTable\r
741 )\r
742{\r
743 VOID *SmmVariableRegistration;\r
744 VOID *SmmVariableWriteRegistration;\r
745 EFI_EVENT OnReadyToBootEvent;\r
746 EFI_EVENT ExitBootServiceEvent;\r
6ed1ec59
SZ
747\r
748 EfiInitializeLock (&mVariableServicesLock, TPL_NOTIFY);\r
749\r
0c18794e 750 //\r
751 // Smm variable service is ready\r
752 //\r
753 EfiCreateProtocolNotifyEvent (\r
754 &gEfiSmmVariableProtocolGuid, \r
755 TPL_CALLBACK, \r
756 SmmVariableReady, \r
757 NULL, \r
758 &SmmVariableRegistration\r
759 );\r
760\r
761 //\r
762 // Smm Non-Volatile variable write service is ready\r
763 //\r
764 EfiCreateProtocolNotifyEvent (\r
765 &gSmmVariableWriteGuid, \r
766 TPL_CALLBACK, \r
767 SmmVariableWriteReady, \r
768 NULL, \r
769 &SmmVariableWriteRegistration\r
770 );\r
771\r
772 //\r
773 // Register the event to reclaim variable for OS usage.\r
774 //\r
775 EfiCreateEventReadyToBootEx (\r
776 TPL_NOTIFY, \r
777 OnReadyToBoot, \r
778 NULL, \r
779 &OnReadyToBootEvent\r
780 ); \r
781\r
782 //\r
783 // Register the event to inform SMM variable that it is at runtime.\r
784 //\r
785 gBS->CreateEventEx (\r
786 EVT_NOTIFY_SIGNAL,\r
787 TPL_NOTIFY,\r
788 OnExitBootServices,\r
789 NULL,\r
790 &gEfiEventExitBootServicesGuid,\r
791 &ExitBootServiceEvent\r
792 ); \r
793\r
794 //\r
795 // Register the event to convert the pointer for runtime.\r
796 //\r
797 gBS->CreateEventEx (\r
798 EVT_NOTIFY_SIGNAL,\r
799 TPL_NOTIFY,\r
800 VariableAddressChangeEvent,\r
801 NULL,\r
802 &gEfiEventVirtualAddressChangeGuid,\r
803 &mVirtualAddressChangeEvent\r
804 );\r
805 \r
806 return EFI_SUCCESS;\r
807}\r
808\r