]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxSmmLib.c
MdeModulePkg: Add SMM LockBox
[mirror_edk2.git] / MdeModulePkg / Library / SmmLockBoxLib / SmmLockBoxSmmLib.c
... / ...
CommitLineData
1/** @file\r
2\r
3Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
4\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions\r
7of the BSD License which accompanies this distribution. The\r
8full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include <PiSmm.h>\r
17#include <Library/SmmServicesTableLib.h>\r
18#include <Library/BaseLib.h>\r
19#include <Library/BaseMemoryLib.h>\r
20#include <Library/LockBoxLib.h>\r
21#include <Library/DebugLib.h>\r
22#include <Guid/SmmLockBox.h>\r
23\r
24#include "SmmLockBoxLibPrivate.h"\r
25\r
26/**\r
27 We need handle this library carefully. Only one library instance will construct the environment.\r
28 Below 2 global variable can only be used in constructor. They should NOT be used in any other library functions.\r
29**/\r
30SMM_LOCK_BOX_CONTEXT mSmmLockBoxContext;\r
31LIST_ENTRY mLockBoxQueue = INITIALIZE_LIST_HEAD_VARIABLE (mLockBoxQueue);\r
32\r
33/**\r
34 This function return SmmLockBox context from SMST.\r
35\r
36 @return SmmLockBox context from SMST.\r
37**/\r
38SMM_LOCK_BOX_CONTEXT *\r
39InternalGetSmmLockBoxContext (\r
40 VOID\r
41 )\r
42{\r
43 UINTN Index;\r
44\r
45 //\r
46 // Check if gEfiSmmLockBoxCommunicationGuid is installed by someone\r
47 //\r
48 for (Index = 0; Index < gSmst->NumberOfTableEntries; Index++) {\r
49 if (CompareGuid (&gSmst->SmmConfigurationTable[Index].VendorGuid, &gEfiSmmLockBoxCommunicationGuid)) {\r
50 //\r
51 // Found. That means some other library instance is already run.\r
52 // No need to install again, just return.\r
53 //\r
54 return (SMM_LOCK_BOX_CONTEXT *)gSmst->SmmConfigurationTable[Index].VendorTable;\r
55 }\r
56 }\r
57\r
58 //\r
59 // Not found.\r
60 //\r
61 return NULL;\r
62}\r
63\r
64/**\r
65 Constructor for SmmLockBox library.\r
66 This is used to set SmmLockBox context, which will be used in PEI phase in S3 boot path later.\r
67\r
68 @param[in] ImageHandle Image handle of this driver.\r
69 @param[in] SystemTable A Pointer to the EFI System Table.\r
70\r
71 @retval EFI_SUCEESS \r
72 @return Others Some error occurs.\r
73**/\r
74EFI_STATUS\r
75EFIAPI\r
76SmmLockBoxSmmConstructuor (\r
77 IN EFI_HANDLE ImageHandle,\r
78 IN EFI_SYSTEM_TABLE *SystemTable\r
79 )\r
80{\r
81 EFI_STATUS Status;\r
82 SMM_LOCK_BOX_CONTEXT *SmmLockBoxContext;\r
83\r
84 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib SmmLockBoxSmmConstructuor - Enter\n"));\r
85\r
86 //\r
87 // Check if gEfiSmmLockBoxCommunicationGuid is installed by someone\r
88 //\r
89 SmmLockBoxContext = InternalGetSmmLockBoxContext ();\r
90 if (SmmLockBoxContext != NULL) {\r
91 //\r
92 // Find it. That means some other library instance is already run.\r
93 // No need to install again, just return.\r
94 //\r
95 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib SmmLockBoxContext - already installed\n"));\r
96 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib SmmLockBoxSmmConstructuor - Exit\n"));\r
97 return EFI_SUCCESS;\r
98 }\r
99\r
100 //\r
101 // If no one install this, it means this is first instance. Install it.\r
102 //\r
103 if (sizeof(UINTN) == sizeof(UINT64)) {\r
104 mSmmLockBoxContext.Signature = SMM_LOCK_BOX_SIGNATURE_64;\r
105 } else {\r
106 mSmmLockBoxContext.Signature = SMM_LOCK_BOX_SIGNATURE_32;\r
107 }\r
108 mSmmLockBoxContext.LockBoxDataAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)&mLockBoxQueue;\r
109\r
110 Status = gSmst->SmmInstallConfigurationTable (\r
111 gSmst,\r
112 &gEfiSmmLockBoxCommunicationGuid,\r
113 &mSmmLockBoxContext,\r
114 sizeof(mSmmLockBoxContext)\r
115 );\r
116 ASSERT_EFI_ERROR (Status);\r
117\r
118 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib SmmLockBoxContext - %x\n", (UINTN)&mSmmLockBoxContext));\r
119 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib LockBoxDataAddress - %x\n", (UINTN)&mLockBoxQueue));\r
120 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib SmmLockBoxSmmConstructuor - Exit\n"));\r
121\r
122 return Status;\r
123}\r
124\r
125/**\r
126 This function return SmmLockBox queue address.\r
127\r
128 @return SmmLockBox queue address.\r
129**/\r
130LIST_ENTRY *\r
131InternalGetLockBoxQueue (\r
132 VOID\r
133 )\r
134{\r
135 SMM_LOCK_BOX_CONTEXT *SmmLockBoxContext;\r
136\r
137 SmmLockBoxContext = InternalGetSmmLockBoxContext ();\r
138 ASSERT (SmmLockBoxContext != NULL);\r
139 if (SmmLockBoxContext == NULL) {\r
140 return NULL;\r
141 }\r
142 return (LIST_ENTRY *)(UINTN)SmmLockBoxContext->LockBoxDataAddress;\r
143}\r
144\r
145/**\r
146 This function find LockBox by GUID.\r
147\r
148 @param Guid The guid to indentify the LockBox\r
149\r
150 @return LockBoxData\r
151**/\r
152SMM_LOCK_BOX_DATA *\r
153InternalFindLockBoxByGuid (\r
154 IN EFI_GUID *Guid\r
155 )\r
156{\r
157 LIST_ENTRY *Link;\r
158 SMM_LOCK_BOX_DATA *LockBox;\r
159 LIST_ENTRY *LockBoxQueue;\r
160\r
161 LockBoxQueue = InternalGetLockBoxQueue ();\r
162 ASSERT (LockBoxQueue != NULL);\r
163\r
164 for (Link = LockBoxQueue->ForwardLink;\r
165 Link != LockBoxQueue;\r
166 Link = Link->ForwardLink) {\r
167 LockBox = BASE_CR (\r
168 Link,\r
169 SMM_LOCK_BOX_DATA,\r
170 Link\r
171 );\r
172 if (CompareGuid (&LockBox->Guid, Guid)) {\r
173 return LockBox;\r
174 }\r
175 }\r
176 return NULL;\r
177}\r
178\r
179/**\r
180 This function will save confidential information to lockbox.\r
181\r
182 @param Guid the guid to identify the confidential information\r
183 @param Buffer the address of the confidential information\r
184 @param Length the length of the confidential information\r
185\r
186 @retval RETURN_SUCCESS the information is saved successfully.\r
187 @retval RETURN_INVALID_PARAMETER the Guid is NULL, or Buffer is NULL, or Length is 0\r
188 @retval RETURN_ALREADY_STARTED the requested GUID already exist.\r
189 @retval RETURN_OUT_OF_RESOURCES no enough resource to save the information.\r
190 @retval RETURN_ACCESS_DENIED it is too late to invoke this interface\r
191 @retval RETURN_NOT_STARTED it is too early to invoke this interface\r
192 @retval RETURN_UNSUPPORTED the service is not supported by implementaion.\r
193**/\r
194RETURN_STATUS\r
195EFIAPI\r
196SaveLockBox (\r
197 IN GUID *Guid,\r
198 IN VOID *Buffer,\r
199 IN UINTN Length\r
200 )\r
201{\r
202 SMM_LOCK_BOX_DATA *LockBox;\r
203 EFI_PHYSICAL_ADDRESS SmramBuffer;\r
204 EFI_STATUS Status;\r
205 LIST_ENTRY *LockBoxQueue;\r
206\r
207 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib SaveLockBox - Enter\n"));\r
208\r
209 //\r
210 // Basic check\r
211 //\r
212 if ((Guid == NULL) || (Buffer == NULL) || (Length == 0)) {\r
213 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib SaveLockBox - Exit (%r)\n", EFI_INVALID_PARAMETER));\r
214 return EFI_INVALID_PARAMETER;\r
215 }\r
216\r
217 //\r
218 // Find LockBox\r
219 //\r
220 LockBox = InternalFindLockBoxByGuid (Guid);\r
221 if (LockBox != NULL) {\r
222 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib SaveLockBox - Exit (%r)\n", EFI_ALREADY_STARTED));\r
223 return EFI_ALREADY_STARTED;\r
224 }\r
225\r
226 //\r
227 // Allocate SMRAM buffer\r
228 //\r
229 Status = gSmst->SmmAllocatePages (\r
230 AllocateAnyPages,\r
231 EfiRuntimeServicesData,\r
232 EFI_SIZE_TO_PAGES (Length),\r
233 &SmramBuffer\r
234 );\r
235 ASSERT_EFI_ERROR (Status);\r
236 if (EFI_ERROR (Status)) {\r
237 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib SaveLockBox - Exit (%r)\n", EFI_OUT_OF_RESOURCES));\r
238 return EFI_OUT_OF_RESOURCES;\r
239 }\r
240\r
241 //\r
242 // Allocate LockBox\r
243 //\r
244 Status = gSmst->SmmAllocatePool (\r
245 EfiRuntimeServicesData,\r
246 sizeof(*LockBox),\r
247 (VOID **)&LockBox\r
248 );\r
249 ASSERT_EFI_ERROR (Status);\r
250 if (EFI_ERROR (Status)) {\r
251 gSmst->SmmFreePages (SmramBuffer, EFI_SIZE_TO_PAGES (Length));\r
252 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib SaveLockBox - Exit (%r)\n", EFI_OUT_OF_RESOURCES));\r
253 return EFI_OUT_OF_RESOURCES;\r
254 }\r
255\r
256 //\r
257 // Save data\r
258 //\r
259 CopyMem ((VOID *)(UINTN)SmramBuffer, (VOID *)(UINTN)Buffer, Length);\r
260\r
261 //\r
262 // Insert LockBox to queue\r
263 //\r
264 LockBox->Signature = SMM_LOCK_BOX_DATA_SIGNATURE;\r
265 CopyMem (&LockBox->Guid, Guid, sizeof(EFI_GUID));\r
266 LockBox->Buffer = (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer;\r
267 LockBox->Length = (UINT64)Length;\r
268 LockBox->SmramBuffer = SmramBuffer;\r
269 \r
270 LockBoxQueue = InternalGetLockBoxQueue ();\r
271 ASSERT (LockBoxQueue != NULL);\r
272 InsertTailList (LockBoxQueue, &LockBox->Link);\r
273\r
274 //\r
275 // Done\r
276 //\r
277 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib SaveLockBox - Exit (%r)\n", EFI_SUCCESS));\r
278 return EFI_SUCCESS;\r
279}\r
280\r
281/**\r
282 This function will set lockbox attributes.\r
283\r
284 @param Guid the guid to identify the confidential information\r
285 @param Attributes the attributes of the lockbox\r
286\r
287 @retval RETURN_SUCCESS the information is saved successfully.\r
288 @retval RETURN_INVALID_PARAMETER attributes is invalid.\r
289 @retval RETURN_NOT_FOUND the requested GUID not found.\r
290 @retval RETURN_ACCESS_DENIED it is too late to invoke this interface\r
291 @retval RETURN_NOT_STARTED it is too early to invoke this interface\r
292 @retval RETURN_UNSUPPORTED the service is not supported by implementaion.\r
293**/\r
294RETURN_STATUS\r
295EFIAPI\r
296SetLockBoxAttributes (\r
297 IN GUID *Guid,\r
298 IN UINT64 Attributes\r
299 )\r
300{\r
301 SMM_LOCK_BOX_DATA *LockBox;\r
302\r
303 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib SetLockBoxAttributes - Enter\n"));\r
304\r
305 //\r
306 // Basic check\r
307 //\r
308 if ((Guid == NULL) ||\r
309 ((Attributes & ~LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE) != 0)) {\r
310 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib SetLockBoxAttributes - Exit (%r)\n", EFI_INVALID_PARAMETER));\r
311 return EFI_INVALID_PARAMETER;\r
312 }\r
313\r
314 //\r
315 // Find LockBox\r
316 //\r
317 LockBox = InternalFindLockBoxByGuid (Guid);\r
318 if (LockBox == NULL) {\r
319 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib SetLockBoxAttributes - Exit (%r)\n", EFI_NOT_FOUND));\r
320 return EFI_NOT_FOUND;\r
321 }\r
322\r
323 //\r
324 // Update data\r
325 //\r
326 LockBox->Attributes = Attributes;\r
327\r
328 //\r
329 // Done\r
330 //\r
331 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib SetLockBoxAttributes - Exit (%r)\n", EFI_SUCCESS));\r
332 return EFI_SUCCESS;\r
333}\r
334\r
335/**\r
336 This function will update confidential information to lockbox.\r
337\r
338 @param Guid the guid to identify the original confidential information\r
339 @param Offset the offset of the original confidential information\r
340 @param Buffer the address of the updated confidential information\r
341 @param Length the length of the updated confidential information\r
342\r
343 @retval RETURN_SUCCESS the information is saved successfully.\r
344 @retval RETURN_INVALID_PARAMETER the Guid is NULL, or Buffer is NULL, or Length is 0.\r
345 @retval RETURN_NOT_FOUND the requested GUID not found.\r
346 @retval RETURN_BUFFER_TOO_SMALL the original buffer to too small to hold new information.\r
347 @retval RETURN_ACCESS_DENIED it is too late to invoke this interface\r
348 @retval RETURN_NOT_STARTED it is too early to invoke this interface\r
349 @retval RETURN_UNSUPPORTED the service is not supported by implementaion.\r
350**/\r
351RETURN_STATUS\r
352EFIAPI\r
353UpdateLockBox (\r
354 IN GUID *Guid,\r
355 IN UINTN Offset,\r
356 IN VOID *Buffer,\r
357 IN UINTN Length\r
358 )\r
359{\r
360 SMM_LOCK_BOX_DATA *LockBox;\r
361\r
362 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib UpdateLockBox - Enter\n"));\r
363\r
364 //\r
365 // Basic check\r
366 //\r
367 if ((Guid == NULL) || (Buffer == NULL) || (Length == 0)) {\r
368 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib UpdateLockBox - Exit (%r)\n", EFI_INVALID_PARAMETER));\r
369 return EFI_INVALID_PARAMETER;\r
370 }\r
371\r
372 //\r
373 // Find LockBox\r
374 //\r
375 LockBox = InternalFindLockBoxByGuid (Guid);\r
376 if (LockBox == NULL) {\r
377 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib UpdateLockBox - Exit (%r)\n", EFI_NOT_FOUND));\r
378 return EFI_NOT_FOUND;\r
379 }\r
380\r
381 //\r
382 // Update data\r
383 //\r
384 if (LockBox->Length < Offset + Length) {\r
385 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib UpdateLockBox - Exit (%r)\n", EFI_BUFFER_TOO_SMALL));\r
386 return EFI_BUFFER_TOO_SMALL;\r
387 }\r
388 CopyMem ((VOID *)((UINTN)LockBox->SmramBuffer + Offset), Buffer, Length);\r
389\r
390 //\r
391 // Done\r
392 //\r
393 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib UpdateLockBox - Exit (%r)\n", EFI_SUCCESS));\r
394 return EFI_SUCCESS;\r
395}\r
396\r
397/**\r
398 This function will restore confidential information from lockbox.\r
399\r
400 @param Guid the guid to identify the confidential information\r
401 @param Buffer the address of the restored confidential information\r
402 NULL means restored to original address, Length MUST be NULL at same time.\r
403 @param Length the length of the restored confidential information\r
404\r
405 @retval RETURN_SUCCESS the information is restored successfully.\r
406 @retval RETURN_INVALID_PARAMETER the Guid is NULL, or one of Buffer and Length is NULL.\r
407 @retval RETURN_WRITE_PROTECTED Buffer and Length are NULL, but the LockBox has no \r
408 LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE attribute.\r
409 @retval RETURN_BUFFER_TOO_SMALL the Length is too small to hold the confidential information.\r
410 @retval RETURN_NOT_FOUND the requested GUID not found.\r
411 @retval RETURN_NOT_STARTED it is too early to invoke this interface\r
412 @retval RETURN_ACCESS_DENIED not allow to restore to the address\r
413 @retval RETURN_UNSUPPORTED the service is not supported by implementaion.\r
414**/\r
415RETURN_STATUS\r
416EFIAPI\r
417RestoreLockBox (\r
418 IN GUID *Guid,\r
419 IN VOID *Buffer, OPTIONAL\r
420 IN OUT UINTN *Length OPTIONAL\r
421 )\r
422{\r
423 SMM_LOCK_BOX_DATA *LockBox;\r
424 VOID *RestoreBuffer;\r
425\r
426 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib RestoreLockBox - Enter\n"));\r
427\r
428 //\r
429 // Restore this, Buffer and Length MUST be both NULL or both non-NULL\r
430 //\r
431 if ((Guid == NULL) ||\r
432 ((Buffer == NULL) && (Length != NULL)) ||\r
433 ((Buffer != NULL) && (Length == NULL))) {\r
434 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib RestoreLockBox - Exit (%r)\n", EFI_INVALID_PARAMETER));\r
435 return EFI_INVALID_PARAMETER;\r
436 }\r
437\r
438 //\r
439 // Find LockBox\r
440 //\r
441 LockBox = InternalFindLockBoxByGuid (Guid);\r
442 if (LockBox == NULL) {\r
443 //\r
444 // Not found\r
445 //\r
446 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib RestoreLockBox - Exit (%r)\n", EFI_NOT_FOUND));\r
447 return EFI_NOT_FOUND;\r
448 }\r
449\r
450 //\r
451 // Set RestoreBuffer\r
452 //\r
453 if (Buffer != NULL) {\r
454 //\r
455 // restore to new buffer\r
456 //\r
457 RestoreBuffer = Buffer;\r
458 } else {\r
459 //\r
460 // restore to original buffer\r
461 //\r
462 if ((LockBox->Attributes & LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE) == 0) {\r
463 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib RestoreLockBox - Exit (%r)\n", EFI_WRITE_PROTECTED));\r
464 return EFI_WRITE_PROTECTED;\r
465 }\r
466 RestoreBuffer = (VOID *)(UINTN)LockBox->Buffer;\r
467 }\r
468\r
469 //\r
470 // Set RestoreLength\r
471 //\r
472 if (Length != NULL) {\r
473 if (*Length < (UINTN)LockBox->Length) {\r
474 //\r
475 // Input buffer is too small to hold all data.\r
476 //\r
477 *Length = (UINTN)LockBox->Length;\r
478 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib RestoreLockBox - Exit (%r)\n", EFI_BUFFER_TOO_SMALL));\r
479 return EFI_BUFFER_TOO_SMALL;\r
480 }\r
481 *Length = (UINTN)LockBox->Length;\r
482 }\r
483\r
484 //\r
485 // Restore data\r
486 //\r
487 CopyMem (RestoreBuffer, (VOID *)(UINTN)LockBox->SmramBuffer, (UINTN)LockBox->Length);\r
488\r
489 //\r
490 // Done\r
491 //\r
492 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib RestoreLockBox - Exit (%r)\n", EFI_SUCCESS));\r
493 return EFI_SUCCESS;\r
494}\r
495\r
496/**\r
497 This function will restore confidential information from all lockbox which have RestoreInPlace attribute.\r
498\r
499 @retval RETURN_SUCCESS the information is restored successfully.\r
500 @retval RETURN_NOT_STARTED it is too early to invoke this interface\r
501 @retval RETURN_UNSUPPORTED the service is not supported by implementaion.\r
502**/\r
503RETURN_STATUS\r
504EFIAPI\r
505RestoreAllLockBoxInPlace (\r
506 VOID\r
507 )\r
508{\r
509 SMM_LOCK_BOX_DATA *LockBox;\r
510 LIST_ENTRY *Link;\r
511 LIST_ENTRY *LockBoxQueue;\r
512\r
513 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib RestoreAllLockBoxInPlace - Enter\n"));\r
514\r
515 LockBoxQueue = InternalGetLockBoxQueue ();\r
516 ASSERT (LockBoxQueue != NULL);\r
517\r
518 //\r
519 // Restore all, Buffer and Length MUST be NULL\r
520 //\r
521 for (Link = LockBoxQueue->ForwardLink;\r
522 Link != LockBoxQueue;\r
523 Link = Link->ForwardLink) {\r
524 LockBox = BASE_CR (\r
525 Link,\r
526 SMM_LOCK_BOX_DATA,\r
527 Link\r
528 );\r
529 if ((LockBox->Attributes & LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE) != 0) {\r
530 //\r
531 // Restore data\r
532 //\r
533 CopyMem ((VOID *)(UINTN)LockBox->Buffer, (VOID *)(UINTN)LockBox->SmramBuffer, (UINTN)LockBox->Length);\r
534 }\r
535 }\r
536 //\r
537 // Done\r
538 //\r
539 DEBUG ((EFI_D_INFO, "SmmLockBoxSmmLib RestoreAllLockBoxInPlace - Exit (%r)\n", EFI_SUCCESS));\r
540 return EFI_SUCCESS;\r
541}\r
542\r