]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Tcg/Opal/OpalPasswordDxe/OpalDriver.c
SecurityPkg OpalPassword: Add solution without SMM device code
[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
34c2ce65
ED
420 OPAL_DRIVER_DEVICE *Itr;\r
421 TCG_RESULT Result;\r
422 OPAL_SESSION Session;\r
423 UINT32 PpStorageFlag;\r
a06875e1 424\r
a06875e1
ED
425 gBS->CloseEvent (Event);\r
426\r
34c2ce65
ED
427 PpStorageFlag = Tcg2PhysicalPresenceLibGetManagementFlags ();\r
428 if ((PpStorageFlag & TCG2_BIOS_STORAGE_MANAGEMENT_FLAG_ENABLE_BLOCK_SID) != 0) {\r
a06875e1
ED
429 //\r
430 // Send BlockSID command to each Opal disk\r
431 //\r
432 Itr = mOpalDriver.DeviceList;\r
a06875e1 433 while (Itr != NULL) {\r
be087553
ED
434 if (Itr->OpalDisk.SupportedAttributes.BlockSid) {\r
435 ZeroMem(&Session, sizeof(Session));\r
436 Session.Sscp = Itr->OpalDisk.Sscp;\r
437 Session.MediaId = Itr->OpalDisk.MediaId;\r
438 Session.OpalBaseComId = Itr->OpalDisk.OpalBaseComId;\r
439\r
440 Result = OpalBlockSid (&Session, TRUE); // HardwareReset must always be TRUE\r
441 if (Result != TcgResultSuccess) {\r
442 DEBUG ((DEBUG_ERROR, "OpalBlockSid fail\n"));\r
443 break;\r
444 }\r
a06875e1
ED
445 }\r
446\r
447 Itr = Itr->Next;\r
a06875e1
ED
448 }\r
449 }\r
450}\r
451\r
452/**\r
453 Stop this Controller.\r
454\r
455 @param Dev The device need to be stopped.\r
456\r
457**/\r
458VOID\r
459OpalDriverStopDevice (\r
460 OPAL_DRIVER_DEVICE *Dev\r
461 )\r
462{\r
463 //\r
464 // free each name\r
465 //\r
466 FreePool(Dev->Name16);\r
467\r
468 //\r
469 // remove OPAL_DRIVER_DEVICE from the list\r
470 // it updates the controllerList pointer\r
471 //\r
472 RemoveDevice(Dev);\r
473\r
474 //\r
475 // close protocols that were opened\r
476 //\r
477 gBS->CloseProtocol(\r
478 Dev->Handle,\r
479 &gEfiStorageSecurityCommandProtocolGuid,\r
480 gOpalDriverBinding.DriverBindingHandle,\r
481 Dev->Handle\r
482 );\r
483\r
484 gBS->CloseProtocol(\r
485 Dev->Handle,\r
486 &gEfiBlockIoProtocolGuid,\r
487 gOpalDriverBinding.DriverBindingHandle,\r
488 Dev->Handle\r
489 );\r
490\r
491 FreePool(Dev);\r
492}\r
493\r
494/**\r
495 Get devcie name through the component name protocol.\r
496\r
497 @param[in] AllHandlesBuffer The handle buffer for current system.\r
498 @param[in] NumAllHandles The number of handles for the handle buffer.\r
499 @param[in] Dev The device which need to get name.\r
500 @param[in] UseComp1 Whether use component name or name2 protocol.\r
501\r
502 @retval TRUE Find the name for this device.\r
503 @retval FALSE Not found the name for this device.\r
504**/\r
505BOOLEAN\r
506OpalDriverGetDeviceNameByProtocol(\r
507 EFI_HANDLE *AllHandlesBuffer,\r
508 UINTN NumAllHandles,\r
509 OPAL_DRIVER_DEVICE *Dev,\r
510 BOOLEAN UseComp1\r
511 )\r
512{\r
513 EFI_HANDLE* ProtocolHandlesBuffer;\r
514 UINTN NumProtocolHandles;\r
515 EFI_STATUS Status;\r
516 EFI_COMPONENT_NAME2_PROTOCOL* Cnp1_2; // efi component name and componentName2 have same layout\r
517 EFI_GUID Protocol;\r
518 UINTN StrLength;\r
519 EFI_DEVICE_PATH_PROTOCOL* TmpDevPath;\r
520 UINTN Index1;\r
521 UINTN Index2;\r
522 EFI_HANDLE TmpHandle;\r
523 CHAR16 *DevName;\r
524\r
525 if (Dev == NULL || AllHandlesBuffer == NULL || NumAllHandles == 0) {\r
526 return FALSE;\r
527 }\r
528\r
529 Protocol = UseComp1 ? gEfiComponentNameProtocolGuid : gEfiComponentName2ProtocolGuid;\r
530\r
531 //\r
532 // Find all EFI_HANDLES with protocol\r
533 //\r
534 Status = gBS->LocateHandleBuffer(\r
535 ByProtocol,\r
536 &Protocol,\r
537 NULL,\r
538 &NumProtocolHandles,\r
539 &ProtocolHandlesBuffer\r
540 );\r
541 if (EFI_ERROR(Status)) {\r
542 return FALSE;\r
543 }\r
544\r
545\r
546 //\r
547 // Exit early if no supported devices\r
548 //\r
549 if (NumProtocolHandles == 0) {\r
550 return FALSE;\r
551 }\r
552\r
553 //\r
554 // Get printable name by iterating through all protocols\r
555 // using the handle as the child, and iterate through all handles for the controller\r
556 // exit loop early once found, if not found, then delete device\r
557 // storage security protocol instances already exist, add them to internal list\r
558 //\r
559 Status = EFI_DEVICE_ERROR;\r
560 for (Index1 = 0; Index1 < NumProtocolHandles; Index1++) {\r
561 DevName = NULL;\r
562\r
563 if (Dev->Name16 != NULL) {\r
564 return TRUE;\r
565 }\r
566\r
567 TmpHandle = ProtocolHandlesBuffer[Index1];\r
568\r
569 Status = gBS->OpenProtocol(\r
570 TmpHandle,\r
571 &Protocol,\r
572 (VOID**)&Cnp1_2,\r
573 gImageHandle,\r
574 NULL,\r
575 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
576 );\r
577 if (EFI_ERROR(Status) || Cnp1_2 == NULL) {\r
578 continue;\r
579 }\r
580\r
581 //\r
582 // Use all handles array as controller handle\r
583 //\r
584 for (Index2 = 0; Index2 < NumAllHandles; Index2++) {\r
585 Status = Cnp1_2->GetControllerName(\r
586 Cnp1_2,\r
587 AllHandlesBuffer[Index2],\r
588 Dev->Handle,\r
589 LANGUAGE_ISO_639_2_ENGLISH,\r
590 &DevName\r
591 );\r
592 if (EFI_ERROR(Status)) {\r
593 Status = Cnp1_2->GetControllerName(\r
594 Cnp1_2,\r
595 AllHandlesBuffer[Index2],\r
596 Dev->Handle,\r
597 LANGUAGE_RFC_3066_ENGLISH,\r
598 &DevName\r
599 );\r
600 }\r
601 if (!EFI_ERROR(Status) && DevName != NULL) {\r
602 StrLength = StrLen(DevName) + 1; // Add one for NULL terminator\r
603 Dev->Name16 = AllocateZeroPool(StrLength * sizeof (CHAR16));\r
604 ASSERT (Dev->Name16 != NULL);\r
605 StrCpyS (Dev->Name16, StrLength, DevName);\r
606 Dev->NameZ = (CHAR8*)AllocateZeroPool(StrLength);\r
b7c71793 607 UnicodeStrToAsciiStrS (DevName, Dev->NameZ, StrLength);\r
a06875e1
ED
608\r
609 //\r
610 // Retrieve bridge BDF info and port number or namespace depending on type\r
611 //\r
612 TmpDevPath = NULL;\r
613 Status = gBS->OpenProtocol(\r
614 Dev->Handle,\r
615 &gEfiDevicePathProtocolGuid,\r
616 (VOID**)&TmpDevPath,\r
617 gImageHandle,\r
618 NULL,\r
619 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
620 );\r
621 if (!EFI_ERROR(Status)) {\r
622 Dev->OpalDevicePath = DuplicateDevicePath (TmpDevPath);\r
623 return TRUE;\r
624 }\r
625\r
626 if (Dev->Name16 != NULL) {\r
627 FreePool(Dev->Name16);\r
628 Dev->Name16 = NULL;\r
629 }\r
630 if (Dev->NameZ != NULL) {\r
631 FreePool(Dev->NameZ);\r
632 Dev->NameZ = NULL;\r
633 }\r
634 }\r
635 }\r
636 }\r
637\r
638 return FALSE;\r
639}\r
640\r
641/**\r
642 Get devcie name through the component name protocol.\r
643\r
644 @param[in] Dev The device which need to get name.\r
645\r
646 @retval TRUE Find the name for this device.\r
647 @retval FALSE Not found the name for this device.\r
648**/\r
649BOOLEAN\r
650OpalDriverGetDriverDeviceName(\r
651 OPAL_DRIVER_DEVICE *Dev\r
652 )\r
653{\r
654 EFI_HANDLE* AllHandlesBuffer;\r
655 UINTN NumAllHandles;\r
656 EFI_STATUS Status;\r
657\r
658 if (Dev == NULL) {\r
659 DEBUG((DEBUG_ERROR | DEBUG_INIT, "OpalDriverGetDriverDeviceName Exiting, Dev=NULL\n"));\r
660 return FALSE;\r
661 }\r
662\r
663 //\r
664 // Iterate through ComponentName2 handles to get name, if fails, try ComponentName\r
665 //\r
666 if (Dev->Name16 == NULL) {\r
667 DEBUG((DEBUG_ERROR | DEBUG_INIT, "Name is null, update it\n"));\r
668 //\r
669 // Find all EFI_HANDLES\r
670 //\r
671 Status = gBS->LocateHandleBuffer(\r
672 AllHandles,\r
673 NULL,\r
674 NULL,\r
675 &NumAllHandles,\r
676 &AllHandlesBuffer\r
677 );\r
678 if (EFI_ERROR(Status)) {\r
679 DEBUG ((DEBUG_INFO, "LocateHandleBuffer for AllHandles failed %r\n", Status ));\r
680 return FALSE;\r
681 }\r
682\r
683 //\r
684 // Try component Name2\r
685 //\r
686 if (!OpalDriverGetDeviceNameByProtocol(AllHandlesBuffer, NumAllHandles, Dev, FALSE)) {\r
687 DEBUG((DEBUG_ERROR | DEBUG_INIT, "ComponentName2 failed to get device name, try ComponentName\n"));\r
688 if (!OpalDriverGetDeviceNameByProtocol(AllHandlesBuffer, NumAllHandles, Dev, TRUE)) {\r
689 DEBUG((DEBUG_ERROR | DEBUG_INIT, "ComponentName failed to get device name, skip device\n"));\r
690 return FALSE;\r
691 }\r
692 }\r
693 }\r
694\r
695 return TRUE;\r
696}\r
697\r
698/**\r
699 Main entry for this driver.\r
700\r
701 @param ImageHandle Image Handle this driver.\r
702 @param SystemTable Pointer to SystemTable.\r
703\r
704 @retval EFI_SUCESS This function always complete successfully.\r
705**/\r
706EFI_STATUS\r
707EFIAPI\r
708EfiDriverEntryPoint(\r
709 IN EFI_HANDLE ImageHandle,\r
710 IN EFI_SYSTEM_TABLE* SystemTable\r
711 )\r
712{\r
713 EFI_STATUS Status;\r
714 EFI_EVENT ReadyToBootEvent;\r
715\r
716 Status = EfiLibInstallDriverBindingComponentName2 (\r
717 ImageHandle,\r
718 SystemTable,\r
719 &gOpalDriverBinding,\r
720 ImageHandle,\r
721 &gOpalComponentName,\r
722 &gOpalComponentName2\r
723 );\r
724\r
725 if (EFI_ERROR(Status)) {\r
726 DEBUG((DEBUG_ERROR, "Install protocols to Opal driver Handle failed\n"));\r
727 return Status ;\r
728 }\r
729\r
730 //\r
731 // Initialize Driver object\r
732 //\r
733 ZeroMem(&mOpalDriver, sizeof(mOpalDriver));\r
734 mOpalDriver.Handle = ImageHandle;\r
735\r
736 //\r
737 // register a ReadyToBoot event callback for sending BlockSid command\r
738 //\r
739 Status = EfiCreateEventReadyToBootEx (\r
740 TPL_CALLBACK,\r
741 ReadyToBootCallback,\r
742 (VOID *) &ImageHandle,\r
743 &ReadyToBootEvent\r
744 );\r
745\r
f1430748
ED
746 //\r
747 // Install Hii packages.\r
748 //\r
749 HiiInstall();\r
750\r
a06875e1
ED
751 return Status;\r
752}\r
753\r
754/**\r
755 Tests to see if this driver supports a given controller.\r
756\r
757 This function checks to see if the controller contains an instance of the\r
758 EFI_STORAGE_SECURITY_COMMAND_PROTOCOL and the EFI_BLOCK_IO_PROTOCL\r
759 and returns EFI_SUCCESS if it does.\r
760\r
761 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
762 @param[in] ControllerHandle The Handle of the controller to test. This Handle\r
763 must support a protocol interface that supplies\r
764 an I/O abstraction to the driver.\r
765 @param[in] RemainingDevicePath This parameter is ignored.\r
766\r
767 @retval EFI_SUCCESS The device contains required protocols\r
768 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
769 RemainingDevicePath is already being managed by the driver\r
770 specified by This.\r
771 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
772 RemainingDevicePath is already being managed by a different\r
773 driver or an application that requires exclusive access.\r
774 Currently not implemented.\r
775 @retval EFI_UNSUPPORTED The device does not contain requires protocols\r
776\r
777**/\r
778EFI_STATUS\r
779EFIAPI\r
780OpalEfiDriverBindingSupported(\r
781 IN EFI_DRIVER_BINDING_PROTOCOL* This,\r
782 IN EFI_HANDLE Controller,\r
783 IN EFI_DEVICE_PATH_PROTOCOL* RemainingDevicePath\r
784 )\r
785{\r
786 EFI_STATUS Status;\r
787 EFI_STORAGE_SECURITY_COMMAND_PROTOCOL* SecurityCommand;\r
788 EFI_BLOCK_IO_PROTOCOL* BlkIo;\r
789\r
790 //\r
791 // Test EFI_STORAGE_SECURITY_COMMAND_PROTOCOL on controller Handle.\r
792 //\r
793 Status = gBS->OpenProtocol(\r
794 Controller,\r
795 &gEfiStorageSecurityCommandProtocolGuid,\r
796 ( VOID ** )&SecurityCommand,\r
797 This->DriverBindingHandle,\r
798 Controller,\r
799 EFI_OPEN_PROTOCOL_BY_DRIVER\r
800 );\r
801\r
802 if (Status == EFI_ALREADY_STARTED) {\r
803 return EFI_SUCCESS;\r
804 }\r
805\r
806 if (EFI_ERROR(Status)) {\r
807 return Status;\r
808 }\r
809\r
810 //\r
811 // Close protocol and reopen in Start call\r
812 //\r
813 gBS->CloseProtocol(\r
814 Controller,\r
815 &gEfiStorageSecurityCommandProtocolGuid,\r
816 This->DriverBindingHandle,\r
817 Controller\r
818 );\r
819\r
820 //\r
821 // Test EFI_BLOCK_IO_PROTOCOL on controller Handle, required by EFI_STORAGE_SECURITY_COMMAND_PROTOCOL\r
822 // function APIs\r
823 //\r
824 Status = gBS->OpenProtocol(\r
825 Controller,\r
826 &gEfiBlockIoProtocolGuid,\r
827 (VOID **)&BlkIo,\r
828 This->DriverBindingHandle,\r
829 Controller,\r
830 EFI_OPEN_PROTOCOL_BY_DRIVER\r
831 );\r
832\r
833 if (EFI_ERROR(Status)) {\r
834 DEBUG((DEBUG_INFO, "No EFI_BLOCK_IO_PROTOCOL on controller\n"));\r
835 return Status;\r
836 }\r
837\r
838 //\r
839 // Close protocol and reopen in Start call\r
840 //\r
841 gBS->CloseProtocol(\r
842 Controller,\r
843 &gEfiBlockIoProtocolGuid,\r
844 This->DriverBindingHandle,\r
845 Controller\r
846 );\r
847\r
848 return EFI_SUCCESS;\r
849}\r
850\r
851/**\r
852 Enables Opal Management on a supported device if available.\r
853\r
854 The start function is designed to be called after the Opal UEFI Driver has confirmed the\r
855 "controller", which is a child Handle, contains the EF_STORAGE_SECURITY_COMMAND protocols.\r
856 This function will complete the other necessary checks, such as verifying the device supports\r
857 the correct version of Opal. Upon verification, it will add the device to the\r
858 Opal HII list in order to expose Opal managmeent options.\r
859\r
860 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
861 @param[in] ControllerHandle The Handle of the controller to start. This Handle\r
862 must support a protocol interface that supplies\r
863 an I/O abstraction to the driver.\r
864 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
865 parameter is ignored by device drivers, and is optional for bus\r
866 drivers. For a bus driver, if this parameter is NULL, then handles\r
867 for all the children of Controller are created by this driver.\r
868 If this parameter is not NULL and the first Device Path Node is\r
869 not the End of Device Path Node, then only the Handle for the\r
870 child device specified by the first Device Path Node of\r
871 RemainingDevicePath is created by this driver.\r
872 If the first Device Path Node of RemainingDevicePath is\r
873 the End of Device Path Node, no child Handle is created by this\r
874 driver.\r
875\r
876 @retval EFI_SUCCESS Opal management was enabled.\r
877 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.\r
878 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
879 @retval Others The driver failed to start the device.\r
880\r
881**/\r
882EFI_STATUS\r
883EFIAPI\r
884OpalEfiDriverBindingStart(\r
885 IN EFI_DRIVER_BINDING_PROTOCOL* This,\r
886 IN EFI_HANDLE Controller,\r
887 IN EFI_DEVICE_PATH_PROTOCOL* RemainingDevicePath\r
888 )\r
889{\r
890 EFI_STATUS Status;\r
891 EFI_BLOCK_IO_PROTOCOL *BlkIo;\r
892 OPAL_DRIVER_DEVICE *Dev;\r
893 OPAL_DRIVER_DEVICE *Itr;\r
894 BOOLEAN Result;\r
895\r
896 Itr = mOpalDriver.DeviceList;\r
897 while (Itr != NULL) {\r
898 if (Controller == Itr->Handle) {\r
899 return EFI_SUCCESS;\r
900 }\r
901 Itr = Itr->Next;\r
902 }\r
903\r
904 //\r
905 // Create internal device for tracking. This allows all disks to be tracked\r
906 // by same HII form\r
907 //\r
908 Dev = (OPAL_DRIVER_DEVICE*)AllocateZeroPool(sizeof(OPAL_DRIVER_DEVICE));\r
909 if (Dev == NULL) {\r
910 return EFI_OUT_OF_RESOURCES;\r
911 }\r
912 Dev->Handle = Controller;\r
913\r
914 //\r
915 // Open EFI_STORAGE_SECURITY_COMMAND_PROTOCOL to perform Opal supported checks\r
916 //\r
917 Status = gBS->OpenProtocol(\r
918 Controller,\r
919 &gEfiStorageSecurityCommandProtocolGuid,\r
920 (VOID **)&Dev->Sscp,\r
921 This->DriverBindingHandle,\r
922 Controller,\r
923 EFI_OPEN_PROTOCOL_BY_DRIVER\r
924 );\r
925 if (EFI_ERROR(Status)) {\r
926 FreePool(Dev);\r
927 return Status;\r
928 }\r
929\r
930 //\r
931 // Open EFI_BLOCK_IO_PROTOCOL on controller Handle, required by EFI_STORAGE_SECURITY_COMMAND_PROTOCOL\r
932 // function APIs\r
933 //\r
934 Status = gBS->OpenProtocol(\r
935 Controller,\r
936 &gEfiBlockIoProtocolGuid,\r
937 (VOID **)&BlkIo,\r
938 This->DriverBindingHandle,\r
939 Controller,\r
940 EFI_OPEN_PROTOCOL_BY_DRIVER\r
941 );\r
942 if (EFI_ERROR(Status)) {\r
943 //\r
944 // Close storage security that was opened\r
945 //\r
946 gBS->CloseProtocol(\r
947 Controller,\r
948 &gEfiStorageSecurityCommandProtocolGuid,\r
949 This->DriverBindingHandle,\r
950 Controller\r
951 );\r
952\r
953 FreePool(Dev);\r
954 return Status;\r
955 }\r
956\r
957 //\r
958 // Save mediaId\r
959 //\r
960 Dev->MediaId = BlkIo->Media->MediaId;\r
961\r
962 gBS->CloseProtocol(\r
963 Controller,\r
964 &gEfiBlockIoProtocolGuid,\r
965 This->DriverBindingHandle,\r
966 Controller\r
967 );\r
968\r
969 //\r
970 // Acquire Ascii printable name of child, if not found, then ignore device\r
971 //\r
972 Result = OpalDriverGetDriverDeviceName (Dev);\r
973 if (!Result) {\r
974 goto Done;\r
975 }\r
976\r
977 Status = OpalDiskInitialize (Dev);\r
978 if (EFI_ERROR (Status)) {\r
979 goto Done;\r
980 }\r
981\r
982 AddDeviceToTail(Dev);\r
983\r
a06875e1
ED
984 //\r
985 // check if device is locked and prompt for password\r
986 //\r
987 OpalDriverRequestPassword (Dev);\r
988\r
989 return EFI_SUCCESS;\r
990\r
991Done:\r
992 //\r
993 // free device, close protocols and exit\r
994 //\r
995 gBS->CloseProtocol(\r
996 Controller,\r
997 &gEfiStorageSecurityCommandProtocolGuid,\r
998 This->DriverBindingHandle,\r
999 Controller\r
1000 );\r
1001\r
1002 FreePool(Dev);\r
1003\r
1004 return EFI_DEVICE_ERROR;\r
1005}\r
1006\r
1007/**\r
1008 Stop this driver on Controller.\r
1009\r
1010 @param This Protocol instance pointer.\r
1011 @param Controller Handle of device to stop driver on\r
1012 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r
1013 children is zero stop the entire bus driver.\r
1014 @param ChildHandleBuffer List of Child Handles to Stop.\r
1015\r
1016 @retval EFI_SUCCESS This driver is removed Controller.\r
1017 @retval other This driver could not be removed from this device.\r
1018\r
1019**/\r
1020EFI_STATUS\r
1021EFIAPI\r
1022OpalEfiDriverBindingStop(\r
1023 EFI_DRIVER_BINDING_PROTOCOL* This,\r
1024 EFI_HANDLE Controller,\r
1025 UINTN NumberOfChildren,\r
1026 EFI_HANDLE* ChildHandleBuffer\r
1027 )\r
1028{\r
1029 OPAL_DRIVER_DEVICE* Itr;\r
1030\r
1031 Itr = mOpalDriver.DeviceList;\r
1032\r
1033 //\r
1034 // does Controller match any of the devices we are managing for Opal\r
1035 //\r
1036 while (Itr != NULL) {\r
1037 if (Itr->Handle == Controller) {\r
1038 OpalDriverStopDevice (Itr);\r
1039 return EFI_SUCCESS;\r
1040 }\r
1041\r
1042 Itr = Itr->Next;\r
1043 }\r
1044\r
1045 return EFI_NOT_FOUND;\r
1046}\r
1047\r
1048\r
1049/**\r
1050 Unloads UEFI Driver. Very useful for debugging and testing.\r
1051\r
1052 @param ImageHandle Image Handle this driver.\r
1053\r
1054 @retval EFI_SUCCESS This function always complete successfully.\r
1055 @retval EFI_INVALID_PARAMETER The input ImageHandle is not valid.\r
1056**/\r
1057EFI_STATUS\r
1058EFIAPI\r
1059OpalEfiDriverUnload (\r
1060 IN EFI_HANDLE ImageHandle\r
1061 )\r
1062{\r
1063 EFI_STATUS Status;\r
1064 OPAL_DRIVER_DEVICE *Itr;\r
1065\r
1066 Status = EFI_SUCCESS;\r
1067\r
1068 if (ImageHandle != gImageHandle) {\r
1069 return (EFI_INVALID_PARAMETER);\r
1070 }\r
1071\r
1072 //\r
1073 // Uninstall any interface added to each device by us\r
1074 //\r
1075 while (mOpalDriver.DeviceList) {\r
1076 Itr = mOpalDriver.DeviceList;\r
1077 //\r
1078 // Remove OPAL_DRIVER_DEVICE from the list\r
1079 // it updates the controllerList pointer\r
1080 //\r
1081 OpalDriverStopDevice(Itr);\r
1082 }\r
1083\r
1084 //\r
1085 // Uninstall the HII capability\r
1086 //\r
1087 Status = HiiUninstall();\r
1088\r
1089 return Status;\r
1090}\r
1091\r