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