]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.c
Add security package to repository.
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / RuntimeDxe / VariableSmm.c
1 /** @file
2 The sample implementation for SMM variable protocol. And this driver
3 implements an SMI handler to communicate with the DXE runtime driver
4 to provide variable services.
5
6 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include <Protocol/SmmVariable.h>
18 #include <Protocol/SmmFirmwareVolumeBlock.h>
19 #include <Protocol/SmmFaultTolerantWrite.h>
20 #include <Library/SmmServicesTableLib.h>
21
22 #include <Guid/AuthenticatedVariableFormat.h>
23 #include <Guid/SmmVariableCommon.h>
24 #include "Variable.h"
25
26 extern VARIABLE_INFO_ENTRY *gVariableInfo;
27 EFI_HANDLE mSmmVariableHandle = NULL;
28 EFI_HANDLE mVariableHandle = NULL;
29 BOOLEAN mAtRuntime = FALSE;
30 EFI_GUID mZeroGuid = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};
31
32 EFI_SMM_VARIABLE_PROTOCOL gSmmVariable = {
33 VariableServiceGetVariable,
34 VariableServiceGetNextVariableName,
35 VariableServiceSetVariable,
36 VariableServiceQueryVariableInfo
37 };
38
39
40 /**
41 Return TRUE if ExitBootServices () has been called.
42
43 @retval TRUE If ExitBootServices () has been called.
44 **/
45 BOOLEAN
46 AtRuntime (
47 VOID
48 )
49 {
50 return mAtRuntime;
51 }
52
53 /**
54 Initializes a basic mutual exclusion lock.
55
56 This function initializes a basic mutual exclusion lock to the released state
57 and returns the lock. Each lock provides mutual exclusion access at its task
58 priority level. Since there is no preemption or multiprocessor support in EFI,
59 acquiring the lock only consists of raising to the locks TPL.
60 If Lock is NULL, then ASSERT().
61 If Priority is not a valid TPL value, then ASSERT().
62
63 @param Lock A pointer to the lock data structure to initialize.
64 @param Priority EFI TPL is associated with the lock.
65
66 @return The lock.
67
68 **/
69 EFI_LOCK *
70 InitializeLock (
71 IN OUT EFI_LOCK *Lock,
72 IN EFI_TPL Priority
73 )
74 {
75 return Lock;
76 }
77
78 /**
79 Acquires lock only at boot time. Simply returns at runtime.
80
81 This is a temperary function that will be removed when
82 EfiAcquireLock() in UefiLib can handle the call in UEFI
83 Runtimer driver in RT phase.
84 It calls EfiAcquireLock() at boot time, and simply returns
85 at runtime.
86
87 @param Lock A pointer to the lock to acquire.
88
89 **/
90 VOID
91 AcquireLockOnlyAtBootTime (
92 IN EFI_LOCK *Lock
93 )
94 {
95
96 }
97
98
99 /**
100 Releases lock only at boot time. Simply returns at runtime.
101
102 This is a temperary function which will be removed when
103 EfiReleaseLock() in UefiLib can handle the call in UEFI
104 Runtimer driver in RT phase.
105 It calls EfiReleaseLock() at boot time and simply returns
106 at runtime.
107
108 @param Lock A pointer to the lock to release.
109
110 **/
111 VOID
112 ReleaseLockOnlyAtBootTime (
113 IN EFI_LOCK *Lock
114 )
115 {
116
117 }
118
119 /**
120 Retrive the SMM Fault Tolerent Write protocol interface.
121
122 @param[out] FtwProtocol The interface of SMM Ftw protocol
123
124 @retval EFI_SUCCESS The SMM FTW protocol instance was found and returned in FtwProtocol.
125 @retval EFI_NOT_FOUND The SMM FTW protocol instance was not found.
126 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
127
128 **/
129 EFI_STATUS
130 GetFtwProtocol (
131 OUT VOID **FtwProtocol
132 )
133 {
134 EFI_STATUS Status;
135
136 //
137 // Locate Smm Fault Tolerent Write protocol
138 //
139 Status = gSmst->SmmLocateProtocol (
140 &gEfiSmmFaultTolerantWriteProtocolGuid,
141 NULL,
142 FtwProtocol
143 );
144 return Status;
145 }
146
147
148 /**
149 Retrive the SMM FVB protocol interface by HANDLE.
150
151 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for
152 reading, writing, and erasing the target block.
153 @param[out] FvBlock The interface of SMM FVB protocol
154
155 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
156 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.
157 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
158
159 **/
160 EFI_STATUS
161 GetFvbByHandle (
162 IN EFI_HANDLE FvBlockHandle,
163 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
164 )
165 {
166 //
167 // To get the SMM FVB protocol interface on the handle
168 //
169 return gSmst->SmmHandleProtocol (
170 FvBlockHandle,
171 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
172 (VOID **) FvBlock
173 );
174 }
175
176
177 /**
178 Function returns an array of handles that support the SMM FVB protocol
179 in a buffer allocated from pool.
180
181 @param[out] NumberHandles The number of handles returned in Buffer.
182 @param[out] Buffer A pointer to the buffer to return the requested
183 array of handles that support SMM FVB protocol.
184
185 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
186 handles in Buffer was returned in NumberHandles.
187 @retval EFI_NOT_FOUND No SMM FVB handle was found.
188 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
189 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
190
191 **/
192 EFI_STATUS
193 GetFvbCountAndBuffer (
194 OUT UINTN *NumberHandles,
195 OUT EFI_HANDLE **Buffer
196 )
197 {
198 EFI_STATUS Status;
199 UINTN BufferSize;
200
201 if ((NumberHandles == NULL) || (Buffer == NULL)) {
202 return EFI_INVALID_PARAMETER;
203 }
204
205 BufferSize = 0;
206 *NumberHandles = 0;
207 *Buffer = NULL;
208 Status = gSmst->SmmLocateHandle (
209 ByProtocol,
210 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
211 NULL,
212 &BufferSize,
213 *Buffer
214 );
215 if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {
216 return EFI_NOT_FOUND;
217 }
218
219 *Buffer = AllocatePool (BufferSize);
220 if (*Buffer == NULL) {
221 return EFI_OUT_OF_RESOURCES;
222 }
223
224 Status = gSmst->SmmLocateHandle (
225 ByProtocol,
226 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
227 NULL,
228 &BufferSize,
229 *Buffer
230 );
231
232 *NumberHandles = BufferSize / sizeof(EFI_HANDLE);
233 if (EFI_ERROR(Status)) {
234 *NumberHandles = 0;
235 }
236
237 return Status;
238 }
239
240
241 /**
242 Get the variable statistics information from the information buffer pointed by gVariableInfo.
243
244 @param[in, out] InfoEntry A pointer to the buffer of variable information entry.
245 On input, point to the variable information returned last time. if
246 InfoEntry->VendorGuid is zero, return the first information.
247 On output, point to the next variable information.
248 @param[in, out] InfoSize On input, the size of the variable information buffer.
249 On output, the returned variable information size.
250
251 @retval EFI_SUCCESS The variable information is found and returned successfully.
252 @retval EFI_UNSUPPORTED No variable inoformation exists in variable driver. The
253 PcdVariableCollectStatistics should be set TRUE to support it.
254 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the next variable information.
255
256 **/
257 EFI_STATUS
258 SmmVariableGetStatistics (
259 IN OUT VARIABLE_INFO_ENTRY *InfoEntry,
260 IN OUT UINTN *InfoSize
261 )
262 {
263 VARIABLE_INFO_ENTRY *VariableInfo;
264 UINTN NameLength;
265 UINTN StatisticsInfoSize;
266 CHAR16 *InfoName;
267
268 ASSERT (InfoEntry != NULL);
269 VariableInfo = gVariableInfo;
270 if (VariableInfo == NULL) {
271 return EFI_UNSUPPORTED;
272 }
273
274 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);
275 if (*InfoSize < sizeof (VARIABLE_INFO_ENTRY)) {
276 *InfoSize = StatisticsInfoSize;
277 return EFI_BUFFER_TOO_SMALL;
278 }
279 InfoName = (CHAR16 *)(InfoEntry + 1);
280
281 if (CompareGuid (&InfoEntry->VendorGuid, &mZeroGuid)) {
282 //
283 // Return the first variable info
284 //
285 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));
286 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));
287 *InfoSize = StatisticsInfoSize;
288 return EFI_SUCCESS;
289 }
290
291 //
292 // Get the next variable info
293 //
294 while (VariableInfo != NULL) {
295 if (CompareGuid (&VariableInfo->VendorGuid, &InfoEntry->VendorGuid)) {
296 NameLength = StrSize (VariableInfo->Name);
297 if (NameLength == StrSize (InfoName)) {
298 if (CompareMem (VariableInfo->Name, InfoName, NameLength) == 0) {
299 //
300 // Find the match one
301 //
302 VariableInfo = VariableInfo->Next;
303 break;
304 }
305 }
306 }
307 VariableInfo = VariableInfo->Next;
308 };
309
310 if (VariableInfo == NULL) {
311 *InfoSize = 0;
312 return EFI_SUCCESS;
313 }
314
315 //
316 // Output the new variable info
317 //
318 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);
319 if (*InfoSize < StatisticsInfoSize) {
320 *InfoSize = StatisticsInfoSize;
321 return EFI_BUFFER_TOO_SMALL;
322 }
323
324 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));
325 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));
326 *InfoSize = StatisticsInfoSize;
327
328 return EFI_SUCCESS;
329 }
330
331
332 /**
333 Communication service SMI Handler entry.
334
335 This SMI handler provides services for the variable wrapper driver.
336
337 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
338 @param[in] RegisterContext Points to an optional handler context which was specified when the
339 handler was registered.
340 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
341 be conveyed from a non-SMM environment into an SMM environment.
342 @param[in, out] CommBufferSize The size of the CommBuffer.
343
344 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
345 should still be called.
346 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
347 still be called.
348 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
349 be called.
350 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
351 **/
352 EFI_STATUS
353 EFIAPI
354 SmmVariableHandler (
355 IN EFI_HANDLE DispatchHandle,
356 IN CONST VOID *RegisterContext,
357 IN OUT VOID *CommBuffer,
358 IN OUT UINTN *CommBufferSize
359 )
360 {
361 EFI_STATUS Status;
362 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;
363 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;
364 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *GetNextVariableName;
365 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *QueryVariableInfo;
366 VARIABLE_INFO_ENTRY *VariableInfo;
367 UINTN InfoSize;
368
369 ASSERT (CommBuffer != NULL);
370
371 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)CommBuffer;
372 switch (SmmVariableFunctionHeader->Function) {
373 case SMM_VARIABLE_FUNCTION_GET_VARIABLE:
374 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) SmmVariableFunctionHeader->Data;
375 Status = VariableServiceGetVariable (
376 SmmVariableHeader->Name,
377 &SmmVariableHeader->Guid,
378 &SmmVariableHeader->Attributes,
379 &SmmVariableHeader->DataSize,
380 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize
381 );
382 break;
383
384 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME:
385 GetNextVariableName = (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *) SmmVariableFunctionHeader->Data;
386 Status = VariableServiceGetNextVariableName (
387 &GetNextVariableName->NameSize,
388 GetNextVariableName->Name,
389 &GetNextVariableName->Guid
390 );
391 break;
392
393 case SMM_VARIABLE_FUNCTION_SET_VARIABLE:
394 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) SmmVariableFunctionHeader->Data;
395 Status = VariableServiceSetVariable (
396 SmmVariableHeader->Name,
397 &SmmVariableHeader->Guid,
398 SmmVariableHeader->Attributes,
399 SmmVariableHeader->DataSize,
400 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize
401 );
402 break;
403
404 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO:
405 QueryVariableInfo = (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *) SmmVariableFunctionHeader->Data;
406 Status = VariableServiceQueryVariableInfo (
407 QueryVariableInfo->Attributes,
408 &QueryVariableInfo->MaximumVariableStorageSize,
409 &QueryVariableInfo->RemainingVariableStorageSize,
410 &QueryVariableInfo->MaximumVariableSize
411 );
412 break;
413
414 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT:
415 ReclaimForOS ();
416 Status = EFI_SUCCESS;
417 break;
418
419 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE:
420 mAtRuntime = TRUE;
421 Status = EFI_SUCCESS;
422 break;
423
424 case SMM_VARIABLE_FUNCTION_GET_STATISTICS:
425 VariableInfo = (VARIABLE_INFO_ENTRY *) SmmVariableFunctionHeader->Data;
426 InfoSize = *CommBufferSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data);
427 Status = SmmVariableGetStatistics (VariableInfo, &InfoSize);
428 *CommBufferSize = InfoSize + OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data);
429 break;
430
431 default:
432 ASSERT (FALSE);
433 Status = EFI_UNSUPPORTED;
434 }
435
436 SmmVariableFunctionHeader->ReturnStatus = Status;
437
438 return EFI_SUCCESS;
439 }
440
441
442 /**
443 SMM Fault Tolerant Write protocol notification event handler.
444
445 Non-Volatile variable write may needs FTW protocol to reclaim when
446 writting variable.
447
448 @param Protocol Points to the protocol's unique identifier
449 @param Interface Points to the interface instance
450 @param Handle The handle on which the interface was installed
451
452 @retval EFI_SUCCESS SmmEventCallback runs successfully
453 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.
454
455 **/
456 EFI_STATUS
457 EFIAPI
458 SmmFtwNotificationEvent (
459 IN CONST EFI_GUID *Protocol,
460 IN VOID *Interface,
461 IN EFI_HANDLE Handle
462 )
463 {
464 EFI_STATUS Status;
465 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;
466 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;
467 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;
468
469 if (mVariableModuleGlobal->FvbInstance != NULL) {
470 return EFI_SUCCESS;
471 }
472
473 //
474 // Ensure SMM FTW protocol is installed.
475 //
476 Status = GetFtwProtocol ((VOID **)&FtwProtocol);
477 if (EFI_ERROR (Status)) {
478 return Status;
479 }
480
481 //
482 // Find the proper FVB protocol for variable.
483 //
484 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);
485 if (NvStorageVariableBase == 0) {
486 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);
487 }
488 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);
489 if (EFI_ERROR (Status)) {
490 return EFI_NOT_FOUND;
491 }
492
493 mVariableModuleGlobal->FvbInstance = FvbProtocol;
494
495 Status = VariableWriteServiceInitialize ();
496 ASSERT_EFI_ERROR (Status);
497
498 //
499 // Notify the variable wrapper driver the variable write service is ready
500 //
501 Status = gBS->InstallProtocolInterface (
502 &mSmmVariableHandle,
503 &gSmmVariableWriteGuid,
504 EFI_NATIVE_INTERFACE,
505 NULL
506 );
507 ASSERT_EFI_ERROR (Status);
508
509 return EFI_SUCCESS;
510 }
511
512
513 /**
514 Variable Driver main entry point. The Variable driver places the 4 EFI
515 runtime services in the EFI System Table and installs arch protocols
516 for variable read and write services being available. It also registers
517 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
518
519 @param[in] ImageHandle The firmware allocated handle for the EFI image.
520 @param[in] SystemTable A pointer to the EFI System Table.
521
522 @retval EFI_SUCCESS Variable service successfully initialized.
523
524 **/
525 EFI_STATUS
526 EFIAPI
527 VariableServiceInitialize (
528 IN EFI_HANDLE ImageHandle,
529 IN EFI_SYSTEM_TABLE *SystemTable
530 )
531 {
532 EFI_STATUS Status;
533 EFI_HANDLE VariableHandle;
534 VOID *SmmFtwRegistration;
535
536 //
537 // Variable initialize.
538 //
539 Status = VariableCommonInitialize ();
540 ASSERT_EFI_ERROR (Status);
541
542 //
543 // Install the Smm Variable Protocol on a new handle.
544 //
545 VariableHandle = NULL;
546 Status = gSmst->SmmInstallProtocolInterface (
547 &VariableHandle,
548 &gEfiSmmVariableProtocolGuid,
549 EFI_NATIVE_INTERFACE,
550 &gSmmVariable
551 );
552 ASSERT_EFI_ERROR (Status);
553
554 ///
555 /// Register SMM variable SMI handler
556 ///
557 VariableHandle = NULL;
558 Status = gSmst->SmiHandlerRegister (SmmVariableHandler, &gEfiSmmVariableProtocolGuid, &VariableHandle);
559 ASSERT_EFI_ERROR (Status);
560
561 //
562 // Notify the variable wrapper driver the variable service is ready
563 //
564 Status = SystemTable->BootServices->InstallProtocolInterface (
565 &mVariableHandle,
566 &gEfiSmmVariableProtocolGuid,
567 EFI_NATIVE_INTERFACE,
568 &gSmmVariable
569 );
570 ASSERT_EFI_ERROR (Status);
571
572 //
573 // Register FtwNotificationEvent () notify function.
574 //
575 Status = gSmst->SmmRegisterProtocolNotify (
576 &gEfiSmmFaultTolerantWriteProtocolGuid,
577 SmmFtwNotificationEvent,
578 &SmmFtwRegistration
579 );
580 ASSERT_EFI_ERROR (Status);
581
582 SmmFtwNotificationEvent (NULL, NULL, NULL);
583
584 return EFI_SUCCESS;
585 }
586
587