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