]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Tcg/Opal/OpalPasswordDxe/OpalDriver.c
SecurityPkg Tcg2PPLib: Support BlockSID related actions
[mirror_edk2.git] / SecurityPkg / Tcg / Opal / OpalPasswordDxe / OpalDriver.c
CommitLineData
a06875e1
ED
1/** @file\r
2 Entrypoint of Opal UEFI Driver and contains all the logic to\r
3 register for new Opal device instances.\r
4\r
5Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>\r
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full 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// This UEFI driver consumes EFI_STORAGE_SECURITY_PROTOCOL instances and installs an\r
17// HII GUI to manage Opal features if the device is Opal capable\r
18// If the Opal device is being managed by the UEFI Driver, it shall provide a popup\r
19// window during boot requesting a user password\r
20\r
21#include "OpalDriver.h"\r
22#include "OpalDriverPrivate.h"\r
23#include "OpalHii.h"\r
24\r
25OPAL_DRIVER mOpalDriver;\r
26\r
a06875e1
ED
27#define MAX_PASSWORD_SIZE 32\r
28#define MAX_PASSWORD_TRY_COUNT 5\r
29\r
30//\r
31// Globals\r
32//\r
33EFI_DRIVER_BINDING_PROTOCOL gOpalDriverBinding = {\r
34 OpalEfiDriverBindingSupported,\r
35 OpalEfiDriverBindingStart,\r
36 OpalEfiDriverBindingStop,\r
37 0x1b,\r
38 NULL,\r
39 NULL\r
40};\r
41\r
42\r
43/**\r
44 Add new device to the global device list.\r
45\r
46 @param Dev New create device.\r
47\r
48**/\r
49VOID\r
50AddDeviceToTail(\r
51 IN OPAL_DRIVER_DEVICE *Dev\r
52 )\r
53{\r
54 OPAL_DRIVER_DEVICE *TmpDev;\r
55\r
56 if (mOpalDriver.DeviceList == NULL) {\r
57 mOpalDriver.DeviceList = Dev;\r
58 } else {\r
59 TmpDev = mOpalDriver.DeviceList;\r
60 while (TmpDev->Next != NULL) {\r
61 TmpDev = TmpDev->Next;\r
62 }\r
63\r
64 TmpDev->Next = Dev;\r
65 }\r
66}\r
67\r
68/**\r
69 Remove one device in the global device list.\r
70\r
71 @param Dev The device need to be removed.\r
72\r
73**/\r
74VOID\r
75RemoveDevice (\r
76 IN OPAL_DRIVER_DEVICE *Dev\r
77 )\r
78{\r
79 OPAL_DRIVER_DEVICE *TmpDev;\r
80\r
81 if (mOpalDriver.DeviceList == NULL) {\r
82 return;\r
83 }\r
84\r
85 if (mOpalDriver.DeviceList == Dev) {\r
86 mOpalDriver.DeviceList = NULL;\r
87 return;\r
88 }\r
89\r
90 TmpDev = mOpalDriver.DeviceList;\r
91 while (TmpDev->Next != NULL) {\r
92 if (TmpDev->Next == Dev) {\r
93 TmpDev->Next = Dev->Next;\r
94 break;\r
95 }\r
96 }\r
97}\r
98\r
99/**\r
100 Get current device count.\r
101\r
102 @retval return the current created device count.\r
103\r
104**/\r
105UINT8\r
106GetDeviceCount (\r
107 VOID\r
108 )\r
109{\r
110 UINT8 Count;\r
111 OPAL_DRIVER_DEVICE *TmpDev;\r
112\r
113 Count = 0;\r
114 TmpDev = mOpalDriver.DeviceList;\r
115\r
116 while (TmpDev != NULL) {\r
117 Count++;\r
118 TmpDev = TmpDev->Next;\r
119 }\r
120\r
121 return Count;\r
122}\r
123\r
124/**\r
125 Get password input from the popup windows, and unlock the device.\r
126\r
127 @param[in] Dev The device which need to be unlock.\r
128 @param[out] PressEsc Whether user escape function through Press ESC.\r
129\r
130 @retval Password string if success. NULL if failed.\r
131\r
132**/\r
133CHAR8 *\r
134OpalDriverPopUpHddPassword (\r
135 IN OPAL_DRIVER_DEVICE *Dev,\r
136 OUT BOOLEAN *PressEsc\r
137 )\r
138{\r
139 EFI_INPUT_KEY InputKey;\r
140 UINTN InputLength;\r
141 CHAR16 Mask[MAX_PASSWORD_SIZE + 1];\r
142 CHAR16 Unicode[MAX_PASSWORD_SIZE + 1];\r
143 CHAR8 *Ascii;\r
144 CHAR16 *PopUpString;\r
145 UINTN StrLength;\r
146\r
147 ZeroMem(Unicode, sizeof(Unicode));\r
148 ZeroMem(Mask, sizeof(Mask));\r
149\r
150 StrLength = StrLen(Dev->Name16);\r
151 PopUpString = (CHAR16*) AllocateZeroPool ((8 + StrLength) * 2);\r
152 *PressEsc = FALSE;\r
153\r
154 if (Dev->Name16 == NULL) {\r
155 UnicodeSPrint(PopUpString, StrLen(L"Unlock Disk") + 1, L"Unlock Disk");\r
156 } else {\r
157 UnicodeSPrint(PopUpString, StrLen(L"Unlock ") + StrLength + 1, L"Unlock %s", Dev->Name16);\r
158 }\r
159\r
160 gST->ConOut->ClearScreen(gST->ConOut);\r
161\r
162 InputLength = 0;\r
163 while (TRUE) {\r
164 Mask[InputLength] = L'_';\r
165 CreatePopUp(\r
166 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
167 &InputKey,\r
168 PopUpString,\r
169 L"---------------------",\r
170 Mask,\r
171 NULL\r
172 );\r
173\r
174 //\r
175 // Check key.\r
176 //\r
177 if (InputKey.ScanCode == SCAN_NULL) {\r
178 //\r
179 // password finished\r
180 //\r
181 if (InputKey.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
182 //\r
183 // Add the null terminator.\r
184 //\r
185 Unicode[InputLength] = 0;\r
186 InputLength++;\r
187 break;\r
188 } else if ((InputKey.UnicodeChar == CHAR_NULL) ||\r
189 (InputKey.UnicodeChar == CHAR_TAB) ||\r
190 (InputKey.UnicodeChar == CHAR_LINEFEED)\r
191 ) {\r
192 continue;\r
193 } else {\r
194 //\r
195 // delete last key entered\r
196 //\r
197 if (InputKey.UnicodeChar == CHAR_BACKSPACE) {\r
198 if (InputLength > 0) {\r
199 Unicode[InputLength] = 0;\r
200 Mask[InputLength] = 0;\r
201 InputLength--;\r
202 }\r
203 } else {\r
204 //\r
205 // add Next key entry\r
206 //\r
207 Unicode[InputLength] = InputKey.UnicodeChar;\r
208 Mask[InputLength] = L'*';\r
209 InputLength++;\r
210 if (InputLength == MAX_PASSWORD_SIZE) {\r
211 //\r
212 // Add the null terminator.\r
213 //\r
214 Unicode[InputLength] = 0;\r
215 Mask[InputLength] = 0;\r
216 break;\r
217 }\r
218 }\r
219 }\r
220 }\r
221\r
222 //\r
223 // exit on ESC\r
224 //\r
225 if (InputKey.ScanCode == SCAN_ESC) {\r
226 *PressEsc = TRUE;\r
227 break;\r
228 }\r
229 }\r
230\r
231 gST->ConOut->ClearScreen(gST->ConOut);\r
232\r
233 if (InputLength == 0 || InputKey.ScanCode == SCAN_ESC) {\r
234 return NULL;\r
235 }\r
236\r
237 Ascii = AllocateZeroPool (MAX_PASSWORD_SIZE + 1);\r
238 if (Ascii == NULL) {\r
239 return NULL;\r
240 }\r
241\r
b7c71793 242 UnicodeStrToAsciiStrS (Unicode, Ascii, MAX_PASSWORD_SIZE + 1);\r
bee13c00 243 ZeroMem (Unicode, sizeof (Unicode));\r
a06875e1
ED
244\r
245 return Ascii;\r
246}\r
247\r
248/**\r
249 Check if disk is locked, show popup window and ask for password if it is\r
250\r
251 @param[in] Dev The device which need to be unlock.\r
252\r
253**/\r
254VOID\r
255OpalDriverRequestPassword (\r
256 OPAL_DRIVER_DEVICE *Dev\r
257 )\r
258{\r
259 UINT8 Count;\r
260 BOOLEAN IsEnabled;\r
261 CHAR8 *Password;\r
262 UINT32 PasswordLen;\r
263 TCG_RESULT Ret;\r
264 EFI_INPUT_KEY Key;\r
265 OPAL_SESSION Session;\r
266 BOOLEAN PressEsc;\r
72a05f84 267 BOOLEAN Locked;\r
a06875e1
ED
268\r
269 if (Dev == NULL) {\r
270 return;\r
271 }\r
272\r
273 Count = 0;\r
274\r
275 IsEnabled = OpalFeatureEnabled (&Dev->OpalDisk.SupportedAttributes, &Dev->OpalDisk.LockingFeature);\r
276 if (IsEnabled) {\r
277 ZeroMem(&Session, sizeof(Session));\r
278 Session.Sscp = Dev->OpalDisk.Sscp;\r
279 Session.MediaId = Dev->OpalDisk.MediaId;\r
280 Session.OpalBaseComId = Dev->OpalDisk.OpalBaseComId;\r
281\r
72a05f84
ED
282 Locked = OpalDeviceLocked (&Dev->OpalDisk.SupportedAttributes, &Dev->OpalDisk.LockingFeature);\r
283\r
a06875e1
ED
284 while (Count < MAX_PASSWORD_TRY_COUNT) {\r
285 Password = OpalDriverPopUpHddPassword (Dev, &PressEsc);\r
286 if (PressEsc) {\r
72a05f84 287 if (Locked) {\r
a06875e1 288 //\r
72a05f84
ED
289 // Current device in the lock status and\r
290 // User not input password and press ESC,\r
291 // keep device in lock status and continue boot.\r
a06875e1 292 //\r
72a05f84
ED
293 do {\r
294 CreatePopUp (\r
295 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
296 &Key,\r
297 L"Press ENTER to skip password, Press ESC to input password",\r
298 NULL\r
299 );\r
300 } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));\r
301\r
302 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
303 gST->ConOut->ClearScreen(gST->ConOut);\r
304 //\r
305 // Keep lock and continue boot.\r
306 //\r
307 return;\r
308 } else {\r
309 //\r
310 // Let user input password again.\r
311 //\r
312 continue;\r
313 }\r
a06875e1
ED
314 } else {\r
315 //\r
72a05f84
ED
316 // Current device in the unlock status and\r
317 // User not input password and press ESC,\r
318 // Shutdown the device.\r
a06875e1 319 //\r
72a05f84
ED
320 do {\r
321 CreatePopUp (\r
322 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
323 &Key,\r
324 L"Press ENTER to shutdown, Press ESC to input password",\r
325 NULL\r
326 );\r
327 } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));\r
328\r
329 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
330 gRT->ResetSystem (EfiResetShutdown, EFI_SUCCESS, 0, NULL);\r
331 } else {\r
332 //\r
333 // Let user input password again.\r
334 //\r
335 continue;\r
336 }\r
a06875e1
ED
337 }\r
338 }\r
339\r
340 if (Password == NULL) {\r
341 Count ++;\r
342 continue;\r
343 }\r
344 PasswordLen = (UINT32) AsciiStrLen(Password);\r
345\r
72a05f84 346 if (Locked) {\r
a06875e1
ED
347 Ret = OpalSupportUnlock(&Session, Password, PasswordLen, Dev->OpalDevicePath);\r
348 } else {\r
349 Ret = OpalSupportLock(&Session, Password, PasswordLen, Dev->OpalDevicePath);\r
350 if (Ret == TcgResultSuccess) {\r
351 Ret = OpalSupportUnlock(&Session, Password, PasswordLen, Dev->OpalDevicePath);\r
352 }\r
353 }\r
354\r
355 if (Password != NULL) {\r
356 ZeroMem (Password, PasswordLen);\r
357 FreePool (Password);\r
358 }\r
359\r
360 if (Ret == TcgResultSuccess) {\r
361 break;\r
362 }\r
363\r
364 Count++;\r
365\r
366 do {\r
367 CreatePopUp (\r
368 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
369 &Key,\r
370 L"Invalid password.",\r
371 L"Press ENTER to retry",\r
372 NULL\r
373 );\r
374 } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN);\r
375 }\r
376\r
377 if (Count >= MAX_PASSWORD_TRY_COUNT) {\r
378 do {\r
379 CreatePopUp (\r
380 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
381 &Key,\r
72a05f84
ED
382 L"Opal password retry count exceeds the limit. Must shutdown!",\r
383 L"Press ENTER to shutdown",\r
a06875e1
ED
384 NULL\r
385 );\r
386 } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN);\r
72a05f84
ED
387\r
388 gRT->ResetSystem (EfiResetShutdown, EFI_SUCCESS, 0, NULL);\r
a06875e1
ED
389 }\r
390 }\r
391}\r
392\r
393/**\r
394 Get devcie list info.\r
395\r
396 @retval return the device list pointer.\r
397**/\r
398OPAL_DRIVER_DEVICE*\r
399OpalDriverGetDeviceList(\r
400 VOID\r
401 )\r
402{\r
403 return mOpalDriver.DeviceList;\r
404}\r
405\r
406/**\r
407 ReadyToBoot callback to send BlockSid command.\r
408\r
409 @param Event Pointer to this event\r
0ab475c9 410 @param Context Event handler private Data\r
a06875e1
ED
411\r
412**/\r
413VOID\r
414EFIAPI\r
415ReadyToBootCallback (\r
416 IN EFI_EVENT Event,\r
417 IN VOID *Context\r
418 )\r
419{\r
252b891b
ED
420 EFI_STATUS Status;\r
421 OPAL_DRIVER_DEVICE* Itr;\r
422 TCG_RESULT Result;\r
423 OPAL_EXTRA_INFO_VAR OpalExtraInfo;\r
424 UINTN DataSize;\r
425 OPAL_SESSION Session;\r
a06875e1 426\r
a06875e1
ED
427 gBS->CloseEvent (Event);\r
428\r
252b891b
ED
429 DataSize = sizeof (OPAL_EXTRA_INFO_VAR);\r
430 Status = gRT->GetVariable (\r
431 OPAL_EXTRA_INFO_VAR_NAME,\r
432 &gOpalExtraInfoVariableGuid,\r
433 NULL,\r
434 &DataSize,\r
435 &OpalExtraInfo\r
436 );\r
437 if (EFI_ERROR (Status)) {\r
438 return;\r
439 }\r
440\r
441 if (OpalExtraInfo.EnableBlockSid == TRUE) {\r
a06875e1
ED
442 //\r
443 // Send BlockSID command to each Opal disk\r
444 //\r
445 Itr = mOpalDriver.DeviceList;\r
a06875e1 446 while (Itr != NULL) {\r
be087553
ED
447 if (Itr->OpalDisk.SupportedAttributes.BlockSid) {\r
448 ZeroMem(&Session, sizeof(Session));\r
449 Session.Sscp = Itr->OpalDisk.Sscp;\r
450 Session.MediaId = Itr->OpalDisk.MediaId;\r
451 Session.OpalBaseComId = Itr->OpalDisk.OpalBaseComId;\r
452\r
453 Result = OpalBlockSid (&Session, TRUE); // HardwareReset must always be TRUE\r
454 if (Result != TcgResultSuccess) {\r
455 DEBUG ((DEBUG_ERROR, "OpalBlockSid fail\n"));\r
456 break;\r
457 }\r
a06875e1
ED
458 }\r
459\r
460 Itr = Itr->Next;\r
a06875e1
ED
461 }\r
462 }\r
463}\r
464\r
465/**\r
466 Stop this Controller.\r
467\r
468 @param Dev The device need to be stopped.\r
469\r
470**/\r
471VOID\r
472OpalDriverStopDevice (\r
473 OPAL_DRIVER_DEVICE *Dev\r
474 )\r
475{\r
476 //\r
477 // free each name\r
478 //\r
479 FreePool(Dev->Name16);\r
480\r
481 //\r
482 // remove OPAL_DRIVER_DEVICE from the list\r
483 // it updates the controllerList pointer\r
484 //\r
485 RemoveDevice(Dev);\r
486\r
487 //\r
488 // close protocols that were opened\r
489 //\r
490 gBS->CloseProtocol(\r
491 Dev->Handle,\r
492 &gEfiStorageSecurityCommandProtocolGuid,\r
493 gOpalDriverBinding.DriverBindingHandle,\r
494 Dev->Handle\r
495 );\r
496\r
497 gBS->CloseProtocol(\r
498 Dev->Handle,\r
499 &gEfiBlockIoProtocolGuid,\r
500 gOpalDriverBinding.DriverBindingHandle,\r
501 Dev->Handle\r
502 );\r
503\r
504 FreePool(Dev);\r
505}\r
506\r
507/**\r
508 Get devcie name through the component name protocol.\r
509\r
510 @param[in] AllHandlesBuffer The handle buffer for current system.\r
511 @param[in] NumAllHandles The number of handles for the handle buffer.\r
512 @param[in] Dev The device which need to get name.\r
513 @param[in] UseComp1 Whether use component name or name2 protocol.\r
514\r
515 @retval TRUE Find the name for this device.\r
516 @retval FALSE Not found the name for this device.\r
517**/\r
518BOOLEAN\r
519OpalDriverGetDeviceNameByProtocol(\r
520 EFI_HANDLE *AllHandlesBuffer,\r
521 UINTN NumAllHandles,\r
522 OPAL_DRIVER_DEVICE *Dev,\r
523 BOOLEAN UseComp1\r
524 )\r
525{\r
526 EFI_HANDLE* ProtocolHandlesBuffer;\r
527 UINTN NumProtocolHandles;\r
528 EFI_STATUS Status;\r
529 EFI_COMPONENT_NAME2_PROTOCOL* Cnp1_2; // efi component name and componentName2 have same layout\r
530 EFI_GUID Protocol;\r
531 UINTN StrLength;\r
532 EFI_DEVICE_PATH_PROTOCOL* TmpDevPath;\r
533 UINTN Index1;\r
534 UINTN Index2;\r
535 EFI_HANDLE TmpHandle;\r
536 CHAR16 *DevName;\r
537\r
538 if (Dev == NULL || AllHandlesBuffer == NULL || NumAllHandles == 0) {\r
539 return FALSE;\r
540 }\r
541\r
542 Protocol = UseComp1 ? gEfiComponentNameProtocolGuid : gEfiComponentName2ProtocolGuid;\r
543\r
544 //\r
545 // Find all EFI_HANDLES with protocol\r
546 //\r
547 Status = gBS->LocateHandleBuffer(\r
548 ByProtocol,\r
549 &Protocol,\r
550 NULL,\r
551 &NumProtocolHandles,\r
552 &ProtocolHandlesBuffer\r
553 );\r
554 if (EFI_ERROR(Status)) {\r
555 return FALSE;\r
556 }\r
557\r
558\r
559 //\r
560 // Exit early if no supported devices\r
561 //\r
562 if (NumProtocolHandles == 0) {\r
563 return FALSE;\r
564 }\r
565\r
566 //\r
567 // Get printable name by iterating through all protocols\r
568 // using the handle as the child, and iterate through all handles for the controller\r
569 // exit loop early once found, if not found, then delete device\r
570 // storage security protocol instances already exist, add them to internal list\r
571 //\r
572 Status = EFI_DEVICE_ERROR;\r
573 for (Index1 = 0; Index1 < NumProtocolHandles; Index1++) {\r
574 DevName = NULL;\r
575\r
576 if (Dev->Name16 != NULL) {\r
577 return TRUE;\r
578 }\r
579\r
580 TmpHandle = ProtocolHandlesBuffer[Index1];\r
581\r
582 Status = gBS->OpenProtocol(\r
583 TmpHandle,\r
584 &Protocol,\r
585 (VOID**)&Cnp1_2,\r
586 gImageHandle,\r
587 NULL,\r
588 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
589 );\r
590 if (EFI_ERROR(Status) || Cnp1_2 == NULL) {\r
591 continue;\r
592 }\r
593\r
594 //\r
595 // Use all handles array as controller handle\r
596 //\r
597 for (Index2 = 0; Index2 < NumAllHandles; Index2++) {\r
598 Status = Cnp1_2->GetControllerName(\r
599 Cnp1_2,\r
600 AllHandlesBuffer[Index2],\r
601 Dev->Handle,\r
602 LANGUAGE_ISO_639_2_ENGLISH,\r
603 &DevName\r
604 );\r
605 if (EFI_ERROR(Status)) {\r
606 Status = Cnp1_2->GetControllerName(\r
607 Cnp1_2,\r
608 AllHandlesBuffer[Index2],\r
609 Dev->Handle,\r
610 LANGUAGE_RFC_3066_ENGLISH,\r
611 &DevName\r
612 );\r
613 }\r
614 if (!EFI_ERROR(Status) && DevName != NULL) {\r
615 StrLength = StrLen(DevName) + 1; // Add one for NULL terminator\r
616 Dev->Name16 = AllocateZeroPool(StrLength * sizeof (CHAR16));\r
617 ASSERT (Dev->Name16 != NULL);\r
618 StrCpyS (Dev->Name16, StrLength, DevName);\r
619 Dev->NameZ = (CHAR8*)AllocateZeroPool(StrLength);\r
b7c71793 620 UnicodeStrToAsciiStrS (DevName, Dev->NameZ, StrLength);\r
a06875e1
ED
621\r
622 //\r
623 // Retrieve bridge BDF info and port number or namespace depending on type\r
624 //\r
625 TmpDevPath = NULL;\r
626 Status = gBS->OpenProtocol(\r
627 Dev->Handle,\r
628 &gEfiDevicePathProtocolGuid,\r
629 (VOID**)&TmpDevPath,\r
630 gImageHandle,\r
631 NULL,\r
632 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
633 );\r
634 if (!EFI_ERROR(Status)) {\r
635 Dev->OpalDevicePath = DuplicateDevicePath (TmpDevPath);\r
636 return TRUE;\r
637 }\r
638\r
639 if (Dev->Name16 != NULL) {\r
640 FreePool(Dev->Name16);\r
641 Dev->Name16 = NULL;\r
642 }\r
643 if (Dev->NameZ != NULL) {\r
644 FreePool(Dev->NameZ);\r
645 Dev->NameZ = NULL;\r
646 }\r
647 }\r
648 }\r
649 }\r
650\r
651 return FALSE;\r
652}\r
653\r
654/**\r
655 Get devcie name through the component name protocol.\r
656\r
657 @param[in] Dev The device which need to get name.\r
658\r
659 @retval TRUE Find the name for this device.\r
660 @retval FALSE Not found the name for this device.\r
661**/\r
662BOOLEAN\r
663OpalDriverGetDriverDeviceName(\r
664 OPAL_DRIVER_DEVICE *Dev\r
665 )\r
666{\r
667 EFI_HANDLE* AllHandlesBuffer;\r
668 UINTN NumAllHandles;\r
669 EFI_STATUS Status;\r
670\r
671 if (Dev == NULL) {\r
672 DEBUG((DEBUG_ERROR | DEBUG_INIT, "OpalDriverGetDriverDeviceName Exiting, Dev=NULL\n"));\r
673 return FALSE;\r
674 }\r
675\r
676 //\r
677 // Iterate through ComponentName2 handles to get name, if fails, try ComponentName\r
678 //\r
679 if (Dev->Name16 == NULL) {\r
680 DEBUG((DEBUG_ERROR | DEBUG_INIT, "Name is null, update it\n"));\r
681 //\r
682 // Find all EFI_HANDLES\r
683 //\r
684 Status = gBS->LocateHandleBuffer(\r
685 AllHandles,\r
686 NULL,\r
687 NULL,\r
688 &NumAllHandles,\r
689 &AllHandlesBuffer\r
690 );\r
691 if (EFI_ERROR(Status)) {\r
692 DEBUG ((DEBUG_INFO, "LocateHandleBuffer for AllHandles failed %r\n", Status ));\r
693 return FALSE;\r
694 }\r
695\r
696 //\r
697 // Try component Name2\r
698 //\r
699 if (!OpalDriverGetDeviceNameByProtocol(AllHandlesBuffer, NumAllHandles, Dev, FALSE)) {\r
700 DEBUG((DEBUG_ERROR | DEBUG_INIT, "ComponentName2 failed to get device name, try ComponentName\n"));\r
701 if (!OpalDriverGetDeviceNameByProtocol(AllHandlesBuffer, NumAllHandles, Dev, TRUE)) {\r
702 DEBUG((DEBUG_ERROR | DEBUG_INIT, "ComponentName failed to get device name, skip device\n"));\r
703 return FALSE;\r
704 }\r
705 }\r
706 }\r
707\r
708 return TRUE;\r
709}\r
710\r
711/**\r
712 Main entry for this driver.\r
713\r
714 @param ImageHandle Image Handle this driver.\r
715 @param SystemTable Pointer to SystemTable.\r
716\r
717 @retval EFI_SUCESS This function always complete successfully.\r
718**/\r
719EFI_STATUS\r
720EFIAPI\r
721EfiDriverEntryPoint(\r
722 IN EFI_HANDLE ImageHandle,\r
723 IN EFI_SYSTEM_TABLE* SystemTable\r
724 )\r
725{\r
726 EFI_STATUS Status;\r
727 EFI_EVENT ReadyToBootEvent;\r
728\r
729 Status = EfiLibInstallDriverBindingComponentName2 (\r
730 ImageHandle,\r
731 SystemTable,\r
732 &gOpalDriverBinding,\r
733 ImageHandle,\r
734 &gOpalComponentName,\r
735 &gOpalComponentName2\r
736 );\r
737\r
738 if (EFI_ERROR(Status)) {\r
739 DEBUG((DEBUG_ERROR, "Install protocols to Opal driver Handle failed\n"));\r
740 return Status ;\r
741 }\r
742\r
743 //\r
744 // Initialize Driver object\r
745 //\r
746 ZeroMem(&mOpalDriver, sizeof(mOpalDriver));\r
747 mOpalDriver.Handle = ImageHandle;\r
748\r
749 //\r
750 // register a ReadyToBoot event callback for sending BlockSid command\r
751 //\r
752 Status = EfiCreateEventReadyToBootEx (\r
753 TPL_CALLBACK,\r
754 ReadyToBootCallback,\r
755 (VOID *) &ImageHandle,\r
756 &ReadyToBootEvent\r
757 );\r
758\r
f1430748
ED
759 //\r
760 // Install Hii packages.\r
761 //\r
762 HiiInstall();\r
763\r
a06875e1
ED
764 return Status;\r
765}\r
766\r
767/**\r
768 Tests to see if this driver supports a given controller.\r
769\r
770 This function checks to see if the controller contains an instance of the\r
771 EFI_STORAGE_SECURITY_COMMAND_PROTOCOL and the EFI_BLOCK_IO_PROTOCL\r
772 and returns EFI_SUCCESS if it does.\r
773\r
774 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
775 @param[in] ControllerHandle The Handle of the controller to test. This Handle\r
776 must support a protocol interface that supplies\r
777 an I/O abstraction to the driver.\r
778 @param[in] RemainingDevicePath This parameter is ignored.\r
779\r
780 @retval EFI_SUCCESS The device contains required protocols\r
781 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
782 RemainingDevicePath is already being managed by the driver\r
783 specified by This.\r
784 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
785 RemainingDevicePath is already being managed by a different\r
786 driver or an application that requires exclusive access.\r
787 Currently not implemented.\r
788 @retval EFI_UNSUPPORTED The device does not contain requires protocols\r
789\r
790**/\r
791EFI_STATUS\r
792EFIAPI\r
793OpalEfiDriverBindingSupported(\r
794 IN EFI_DRIVER_BINDING_PROTOCOL* This,\r
795 IN EFI_HANDLE Controller,\r
796 IN EFI_DEVICE_PATH_PROTOCOL* RemainingDevicePath\r
797 )\r
798{\r
799 EFI_STATUS Status;\r
800 EFI_STORAGE_SECURITY_COMMAND_PROTOCOL* SecurityCommand;\r
801 EFI_BLOCK_IO_PROTOCOL* BlkIo;\r
802\r
803 //\r
804 // Test EFI_STORAGE_SECURITY_COMMAND_PROTOCOL on controller Handle.\r
805 //\r
806 Status = gBS->OpenProtocol(\r
807 Controller,\r
808 &gEfiStorageSecurityCommandProtocolGuid,\r
809 ( VOID ** )&SecurityCommand,\r
810 This->DriverBindingHandle,\r
811 Controller,\r
812 EFI_OPEN_PROTOCOL_BY_DRIVER\r
813 );\r
814\r
815 if (Status == EFI_ALREADY_STARTED) {\r
816 return EFI_SUCCESS;\r
817 }\r
818\r
819 if (EFI_ERROR(Status)) {\r
820 return Status;\r
821 }\r
822\r
823 //\r
824 // Close protocol and reopen in Start call\r
825 //\r
826 gBS->CloseProtocol(\r
827 Controller,\r
828 &gEfiStorageSecurityCommandProtocolGuid,\r
829 This->DriverBindingHandle,\r
830 Controller\r
831 );\r
832\r
833 //\r
834 // Test EFI_BLOCK_IO_PROTOCOL on controller Handle, required by EFI_STORAGE_SECURITY_COMMAND_PROTOCOL\r
835 // function APIs\r
836 //\r
837 Status = gBS->OpenProtocol(\r
838 Controller,\r
839 &gEfiBlockIoProtocolGuid,\r
840 (VOID **)&BlkIo,\r
841 This->DriverBindingHandle,\r
842 Controller,\r
843 EFI_OPEN_PROTOCOL_BY_DRIVER\r
844 );\r
845\r
846 if (EFI_ERROR(Status)) {\r
847 DEBUG((DEBUG_INFO, "No EFI_BLOCK_IO_PROTOCOL on controller\n"));\r
848 return Status;\r
849 }\r
850\r
851 //\r
852 // Close protocol and reopen in Start call\r
853 //\r
854 gBS->CloseProtocol(\r
855 Controller,\r
856 &gEfiBlockIoProtocolGuid,\r
857 This->DriverBindingHandle,\r
858 Controller\r
859 );\r
860\r
861 return EFI_SUCCESS;\r
862}\r
863\r
864/**\r
865 Enables Opal Management on a supported device if available.\r
866\r
867 The start function is designed to be called after the Opal UEFI Driver has confirmed the\r
868 "controller", which is a child Handle, contains the EF_STORAGE_SECURITY_COMMAND protocols.\r
869 This function will complete the other necessary checks, such as verifying the device supports\r
870 the correct version of Opal. Upon verification, it will add the device to the\r
871 Opal HII list in order to expose Opal managmeent options.\r
872\r
873 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
874 @param[in] ControllerHandle The Handle of the controller to start. This Handle\r
875 must support a protocol interface that supplies\r
876 an I/O abstraction to the driver.\r
877 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
878 parameter is ignored by device drivers, and is optional for bus\r
879 drivers. For a bus driver, if this parameter is NULL, then handles\r
880 for all the children of Controller are created by this driver.\r
881 If this parameter is not NULL and the first Device Path Node is\r
882 not the End of Device Path Node, then only the Handle for the\r
883 child device specified by the first Device Path Node of\r
884 RemainingDevicePath is created by this driver.\r
885 If the first Device Path Node of RemainingDevicePath is\r
886 the End of Device Path Node, no child Handle is created by this\r
887 driver.\r
888\r
889 @retval EFI_SUCCESS Opal management was enabled.\r
890 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.\r
891 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
892 @retval Others The driver failed to start the device.\r
893\r
894**/\r
895EFI_STATUS\r
896EFIAPI\r
897OpalEfiDriverBindingStart(\r
898 IN EFI_DRIVER_BINDING_PROTOCOL* This,\r
899 IN EFI_HANDLE Controller,\r
900 IN EFI_DEVICE_PATH_PROTOCOL* RemainingDevicePath\r
901 )\r
902{\r
903 EFI_STATUS Status;\r
904 EFI_BLOCK_IO_PROTOCOL *BlkIo;\r
905 OPAL_DRIVER_DEVICE *Dev;\r
906 OPAL_DRIVER_DEVICE *Itr;\r
907 BOOLEAN Result;\r
908\r
909 Itr = mOpalDriver.DeviceList;\r
910 while (Itr != NULL) {\r
911 if (Controller == Itr->Handle) {\r
912 return EFI_SUCCESS;\r
913 }\r
914 Itr = Itr->Next;\r
915 }\r
916\r
917 //\r
918 // Create internal device for tracking. This allows all disks to be tracked\r
919 // by same HII form\r
920 //\r
921 Dev = (OPAL_DRIVER_DEVICE*)AllocateZeroPool(sizeof(OPAL_DRIVER_DEVICE));\r
922 if (Dev == NULL) {\r
923 return EFI_OUT_OF_RESOURCES;\r
924 }\r
925 Dev->Handle = Controller;\r
926\r
927 //\r
928 // Open EFI_STORAGE_SECURITY_COMMAND_PROTOCOL to perform Opal supported checks\r
929 //\r
930 Status = gBS->OpenProtocol(\r
931 Controller,\r
932 &gEfiStorageSecurityCommandProtocolGuid,\r
933 (VOID **)&Dev->Sscp,\r
934 This->DriverBindingHandle,\r
935 Controller,\r
936 EFI_OPEN_PROTOCOL_BY_DRIVER\r
937 );\r
938 if (EFI_ERROR(Status)) {\r
939 FreePool(Dev);\r
940 return Status;\r
941 }\r
942\r
943 //\r
944 // Open EFI_BLOCK_IO_PROTOCOL on controller Handle, required by EFI_STORAGE_SECURITY_COMMAND_PROTOCOL\r
945 // function APIs\r
946 //\r
947 Status = gBS->OpenProtocol(\r
948 Controller,\r
949 &gEfiBlockIoProtocolGuid,\r
950 (VOID **)&BlkIo,\r
951 This->DriverBindingHandle,\r
952 Controller,\r
953 EFI_OPEN_PROTOCOL_BY_DRIVER\r
954 );\r
955 if (EFI_ERROR(Status)) {\r
956 //\r
957 // Close storage security that was opened\r
958 //\r
959 gBS->CloseProtocol(\r
960 Controller,\r
961 &gEfiStorageSecurityCommandProtocolGuid,\r
962 This->DriverBindingHandle,\r
963 Controller\r
964 );\r
965\r
966 FreePool(Dev);\r
967 return Status;\r
968 }\r
969\r
970 //\r
971 // Save mediaId\r
972 //\r
973 Dev->MediaId = BlkIo->Media->MediaId;\r
974\r
975 gBS->CloseProtocol(\r
976 Controller,\r
977 &gEfiBlockIoProtocolGuid,\r
978 This->DriverBindingHandle,\r
979 Controller\r
980 );\r
981\r
982 //\r
983 // Acquire Ascii printable name of child, if not found, then ignore device\r
984 //\r
985 Result = OpalDriverGetDriverDeviceName (Dev);\r
986 if (!Result) {\r
987 goto Done;\r
988 }\r
989\r
990 Status = OpalDiskInitialize (Dev);\r
991 if (EFI_ERROR (Status)) {\r
992 goto Done;\r
993 }\r
994\r
995 AddDeviceToTail(Dev);\r
996\r
a06875e1
ED
997 //\r
998 // check if device is locked and prompt for password\r
999 //\r
1000 OpalDriverRequestPassword (Dev);\r
1001\r
1002 return EFI_SUCCESS;\r
1003\r
1004Done:\r
1005 //\r
1006 // free device, close protocols and exit\r
1007 //\r
1008 gBS->CloseProtocol(\r
1009 Controller,\r
1010 &gEfiStorageSecurityCommandProtocolGuid,\r
1011 This->DriverBindingHandle,\r
1012 Controller\r
1013 );\r
1014\r
1015 FreePool(Dev);\r
1016\r
1017 return EFI_DEVICE_ERROR;\r
1018}\r
1019\r
1020/**\r
1021 Stop this driver on Controller.\r
1022\r
1023 @param This Protocol instance pointer.\r
1024 @param Controller Handle of device to stop driver on\r
1025 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r
1026 children is zero stop the entire bus driver.\r
1027 @param ChildHandleBuffer List of Child Handles to Stop.\r
1028\r
1029 @retval EFI_SUCCESS This driver is removed Controller.\r
1030 @retval other This driver could not be removed from this device.\r
1031\r
1032**/\r
1033EFI_STATUS\r
1034EFIAPI\r
1035OpalEfiDriverBindingStop(\r
1036 EFI_DRIVER_BINDING_PROTOCOL* This,\r
1037 EFI_HANDLE Controller,\r
1038 UINTN NumberOfChildren,\r
1039 EFI_HANDLE* ChildHandleBuffer\r
1040 )\r
1041{\r
1042 OPAL_DRIVER_DEVICE* Itr;\r
1043\r
1044 Itr = mOpalDriver.DeviceList;\r
1045\r
1046 //\r
1047 // does Controller match any of the devices we are managing for Opal\r
1048 //\r
1049 while (Itr != NULL) {\r
1050 if (Itr->Handle == Controller) {\r
1051 OpalDriverStopDevice (Itr);\r
1052 return EFI_SUCCESS;\r
1053 }\r
1054\r
1055 Itr = Itr->Next;\r
1056 }\r
1057\r
1058 return EFI_NOT_FOUND;\r
1059}\r
1060\r
1061\r
1062/**\r
1063 Unloads UEFI Driver. Very useful for debugging and testing.\r
1064\r
1065 @param ImageHandle Image Handle this driver.\r
1066\r
1067 @retval EFI_SUCCESS This function always complete successfully.\r
1068 @retval EFI_INVALID_PARAMETER The input ImageHandle is not valid.\r
1069**/\r
1070EFI_STATUS\r
1071EFIAPI\r
1072OpalEfiDriverUnload (\r
1073 IN EFI_HANDLE ImageHandle\r
1074 )\r
1075{\r
1076 EFI_STATUS Status;\r
1077 OPAL_DRIVER_DEVICE *Itr;\r
1078\r
1079 Status = EFI_SUCCESS;\r
1080\r
1081 if (ImageHandle != gImageHandle) {\r
1082 return (EFI_INVALID_PARAMETER);\r
1083 }\r
1084\r
1085 //\r
1086 // Uninstall any interface added to each device by us\r
1087 //\r
1088 while (mOpalDriver.DeviceList) {\r
1089 Itr = mOpalDriver.DeviceList;\r
1090 //\r
1091 // Remove OPAL_DRIVER_DEVICE from the list\r
1092 // it updates the controllerList pointer\r
1093 //\r
1094 OpalDriverStopDevice(Itr);\r
1095 }\r
1096\r
1097 //\r
1098 // Uninstall the HII capability\r
1099 //\r
1100 Status = HiiUninstall();\r
1101\r
1102 return Status;\r
1103}\r
1104\r