2 Library functions which relates with driver health.
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2011 - 2015, 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
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.
16 #include "InternalBm.h"
18 GLOBAL_REMOVE_IF_UNREFERENCED
19 CHAR16
*mBmHealthStatusText
[] = {
22 L
"Configuration Required",
24 L
"Reconnect Required",
29 Return the controller name.
31 @param DriverHealthHandle The handle on which the Driver Health protocol instance is retrieved.
32 @param ControllerHandle The handle of a controller that the driver specified by DriverBindingHandle is managing.
33 This handle specifies the controller whose name is to be returned.
34 @param ChildHandle The handle of the child controller to retrieve the name of. This is an
35 optional parameter that may be NULL. It will be NULL for device drivers.
36 It will also be NULL for bus drivers that attempt to retrieve the name
37 of the bus controller. It will not be NULL for a bus driver that attempts
38 to retrieve the name of a child controller.
40 @return A pointer to the Unicode string to return. This Unicode string is the name of the controller
41 specified by ControllerHandle and ChildHandle.
45 IN EFI_HANDLE DriverHealthHandle
,
46 IN EFI_HANDLE ControllerHandle
,
47 IN EFI_HANDLE ChildHandle
51 CHAR16
*ControllerName
;
52 CHAR8
*LanguageVariable
;
54 BOOLEAN Iso639Language
;
55 EFI_COMPONENT_NAME_PROTOCOL
*ComponentName
;
57 ControllerName
= NULL
;
60 // Locate Component Name (2) protocol on the driver binging handle.
62 Iso639Language
= FALSE
;
63 Status
= gBS
->HandleProtocol (
65 &gEfiComponentName2ProtocolGuid
,
66 (VOID
**) &ComponentName
68 if (EFI_ERROR (Status
)) {
69 Status
= gBS
->HandleProtocol (
71 &gEfiComponentNameProtocolGuid
,
72 (VOID
**) &ComponentName
74 if (!EFI_ERROR (Status
)) {
75 Iso639Language
= TRUE
;
79 if (!EFI_ERROR (Status
)) {
80 GetEfiGlobalVariable2 (Iso639Language
? L
"Lang" : L
"PlatformLang", (VOID
**)&LanguageVariable
, NULL
);
81 BestLanguage
= GetBestLanguage(
82 ComponentName
->SupportedLanguages
,
84 (LanguageVariable
!= NULL
) ? LanguageVariable
: "",
85 Iso639Language
? "eng" : "en-US",
88 if (LanguageVariable
!= NULL
) {
89 FreePool (LanguageVariable
);
92 Status
= ComponentName
->GetControllerName (
101 if (!EFI_ERROR (Status
)) {
102 return AllocateCopyPool (StrSize (ControllerName
), ControllerName
);
104 return ConvertDevicePathToText (
105 DevicePathFromHandle (ChildHandle
!= NULL
? ChildHandle
: ControllerHandle
),
113 Display a set of messages returned by the GetHealthStatus () service of the EFI Driver Health Protocol
115 @param DriverHealthInfo Pointer to the Driver Health information entry.
119 IN EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
*DriverHealthInfo
124 CHAR16
*ControllerName
;
126 if (DriverHealthInfo
->MessageList
== NULL
||
127 DriverHealthInfo
->MessageList
[0].HiiHandle
== NULL
) {
131 ControllerName
= BmGetControllerName (
132 DriverHealthInfo
->DriverHealthHandle
,
133 DriverHealthInfo
->ControllerHandle
,
134 DriverHealthInfo
->ChildHandle
137 DEBUG ((EFI_D_INFO
, "Controller: %s\n", ControllerName
));
138 Print (L
"Controller: %s\n", ControllerName
);
139 for (Index
= 0; DriverHealthInfo
->MessageList
[Index
].HiiHandle
!= NULL
; Index
++) {
140 String
= HiiGetString (
141 DriverHealthInfo
->MessageList
[Index
].HiiHandle
,
142 DriverHealthInfo
->MessageList
[Index
].StringId
,
145 if (String
!= NULL
) {
146 Print (L
" %s\n", String
);
147 DEBUG ((EFI_D_INFO
, " %s\n", String
));
152 if (ControllerName
!= NULL
) {
153 FreePool (ControllerName
);
158 The repair notify function.
159 @param Value A value between 0 and Limit that identifies the current progress
160 of the repair operation.
161 @param Limit The maximum value of Value for the current repair operation.
162 If Limit is 0, then the completion progress is indeterminate.
163 For example, a driver that wants to specify progress in percent
164 would use a Limit value of 100.
166 @retval EFI_SUCCESS Successfully return from the notify function.
175 DEBUG ((EFI_D_INFO
, "[BDS]RepairNotify: %d/%d\n", Value
, Limit
));
176 Print (L
"[BDS]RepairNotify: %d/%d\n", Value
, Limit
);
182 Collect the Driver Health status of a single controller.
184 @param DriverHealthInfo A pointer to the array containing all of the platform driver health information.
185 @param Count Return the updated array count.
186 @param DriverHealthHandle The handle on which the Driver Health protocol instance is retrieved.
187 @param ControllerHandle The handle of the controller..
188 @param ChildHandle The handle of the child controller to retrieve the health
189 status on. This is an optional parameter that may be NULL.
191 @retval Status The status returned from GetHealthStatus.
192 @retval EFI_ABORTED The health status is healthy so no further query is needed.
196 BmGetSingleControllerHealthStatus (
197 IN OUT EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
**DriverHealthInfo
,
199 IN EFI_HANDLE DriverHealthHandle
,
200 IN EFI_HANDLE ControllerHandle
, OPTIONAL
201 IN EFI_HANDLE ChildHandle OPTIONAL
205 EFI_DRIVER_HEALTH_PROTOCOL
*DriverHealth
;
206 EFI_DRIVER_HEALTH_HII_MESSAGE
*MessageList
;
207 EFI_HII_HANDLE FormHiiHandle
;
208 EFI_DRIVER_HEALTH_STATUS HealthStatus
;
210 ASSERT (DriverHealthHandle
!= NULL
);
212 // Retrieve the Driver Health Protocol from DriverHandle
214 Status
= gBS
->HandleProtocol (
216 &gEfiDriverHealthProtocolGuid
,
217 (VOID
**) &DriverHealth
219 ASSERT_EFI_ERROR (Status
);
222 if (ControllerHandle
== NULL
) {
224 // If ControllerHandle is NULL, the return the cumulative health status of the driver
226 Status
= DriverHealth
->GetHealthStatus (DriverHealth
, NULL
, NULL
, &HealthStatus
, NULL
, NULL
);
227 if (!EFI_ERROR (Status
) && HealthStatus
== EfiDriverHealthStatusHealthy
) {
228 *DriverHealthInfo
= ReallocatePool (
229 (*Count
) * sizeof (EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
),
230 (*Count
+ 1) * sizeof (EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
),
233 ASSERT (*DriverHealthInfo
!= NULL
);
235 (*DriverHealthInfo
)[*Count
].DriverHealthHandle
= DriverHealthHandle
;
236 (*DriverHealthInfo
)[*Count
].DriverHealth
= DriverHealth
;
237 (*DriverHealthInfo
)[*Count
].HealthStatus
= HealthStatus
;
241 Status
= EFI_ABORTED
;
247 FormHiiHandle
= NULL
;
250 // Collect the health status with the optional HII message list
252 Status
= DriverHealth
->GetHealthStatus (DriverHealth
, ControllerHandle
, ChildHandle
, &HealthStatus
, &MessageList
, &FormHiiHandle
);
253 if (!EFI_ERROR (Status
)) {
254 *DriverHealthInfo
= ReallocatePool (
255 (*Count
) * sizeof (EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
),
256 (*Count
+ 1) * sizeof (EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
),
259 ASSERT (*DriverHealthInfo
!= NULL
);
260 (*DriverHealthInfo
)[*Count
].DriverHealth
= DriverHealth
;
261 (*DriverHealthInfo
)[*Count
].DriverHealthHandle
= DriverHealthHandle
;
262 (*DriverHealthInfo
)[*Count
].ControllerHandle
= ControllerHandle
;
263 (*DriverHealthInfo
)[*Count
].ChildHandle
= ChildHandle
;
264 (*DriverHealthInfo
)[*Count
].HiiHandle
= FormHiiHandle
;
265 (*DriverHealthInfo
)[*Count
].MessageList
= MessageList
;
266 (*DriverHealthInfo
)[*Count
].HealthStatus
= HealthStatus
;
275 Return all the Driver Health information.
277 When the cumulative health status of all the controllers managed by the
278 driver who produces the EFI_DRIVER_HEALTH_PROTOCOL is healthy, only one
279 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO entry is created for such
280 EFI_DRIVER_HEALTH_PROTOCOL instance.
281 Otherwise, every controller creates one EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
282 entry. Additionally every child controller creates one
283 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO entry if the driver is a bus driver.
285 @param Count Return the count of the Driver Health information.
287 @retval NULL No Driver Health information is returned.
288 @retval !NULL Pointer to the Driver Health information array.
290 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
*
292 EfiBootManagerGetDriverHealthInfo (
298 EFI_HANDLE
*DriverHealthHandles
;
299 UINTN DriverHealthIndex
;
302 UINTN ControllerIndex
;
304 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
*DriverHealthInfo
;
307 // Initialize local variables
310 DriverHealthInfo
= NULL
;
312 DriverHealthHandles
= NULL
;
316 Status
= gBS
->LocateHandleBuffer (
318 &gEfiDriverHealthProtocolGuid
,
324 if (Status
== EFI_NOT_FOUND
|| NumHandles
== 0) {
326 // If there are no Driver Health Protocols handles, then return EFI_NOT_FOUND
331 ASSERT_EFI_ERROR (Status
);
332 ASSERT (DriverHealthHandles
!= NULL
);
335 // Check the health status of all controllers in the platform
336 // Start by looping through all the Driver Health Protocol handles in the handle database
338 for (DriverHealthIndex
= 0; DriverHealthIndex
< NumHandles
; DriverHealthIndex
++) {
340 // Get the cumulative health status of the driver
342 Status
= BmGetSingleControllerHealthStatus (&DriverHealthInfo
, Count
, DriverHealthHandles
[DriverHealthIndex
], NULL
, NULL
);
343 if (EFI_ERROR (Status
)) {
348 // See if the list of all handles in the handle database has been retrieved
351 if (Handles
== NULL
) {
353 // Retrieve the list of all handles from the handle database
355 Status
= gBS
->LocateHandleBuffer (
362 ASSERT_EFI_ERROR (Status
);
365 // Loop through all the controller handles in the handle database
367 for (ControllerIndex
= 0; ControllerIndex
< HandleCount
; ControllerIndex
++) {
368 Status
= BmGetSingleControllerHealthStatus (&DriverHealthInfo
, Count
, DriverHealthHandles
[DriverHealthIndex
], Handles
[ControllerIndex
], NULL
);
369 if (EFI_ERROR (Status
)) {
374 // Loop through all the child handles in the handle database
376 for (ChildIndex
= 0; ChildIndex
< HandleCount
; ChildIndex
++) {
377 Status
= BmGetSingleControllerHealthStatus (&DriverHealthInfo
, Count
, DriverHealthHandles
[DriverHealthIndex
], Handles
[ControllerIndex
], Handles
[ChildIndex
]);
378 if (EFI_ERROR (Status
)) {
385 Status
= EFI_SUCCESS
;
387 if (Handles
!= NULL
) {
390 if (DriverHealthHandles
!= NULL
) {
391 FreePool (DriverHealthHandles
);
394 return DriverHealthInfo
;
398 Free the Driver Health information array.
400 @param DriverHealthInfo Pointer to array of the Driver Health information.
401 @param Count Count of the array.
403 @retval EFI_SUCCESS The array is freed.
404 @retval EFI_INVALID_PARAMETER The array is NULL.
408 EfiBootManagerFreeDriverHealthInfo (
409 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
*DriverHealthInfo
,
415 for (Index
= 0; Index
< Count
; Index
++) {
416 if (DriverHealthInfo
[Index
].MessageList
!= NULL
) {
417 FreePool (DriverHealthInfo
[Index
].MessageList
);
420 return gBS
->FreePool (DriverHealthInfo
);
424 Repair all the controllers according to the Driver Health status queried.
427 BmRepairAllControllers (
432 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
*DriverHealthInfo
;
433 EFI_DRIVER_HEALTH_STATUS HealthStatus
;
436 BOOLEAN RepairRequired
;
437 BOOLEAN ConfigurationRequired
;
438 BOOLEAN ReconnectRequired
;
439 BOOLEAN RebootRequired
;
440 EFI_HII_HANDLE
*HiiHandles
;
441 EFI_FORM_BROWSER2_PROTOCOL
*FormBrowser2
;
444 // Configure PcdDriverHealthConfigureForm to ZeroGuid to disable driver health check.
446 if (CompareGuid (PcdGetPtr (PcdDriverHealthConfigureForm
), &gZeroGuid
)) {
450 Status
= gBS
->LocateProtocol (&gEfiFormBrowser2ProtocolGuid
, NULL
, (VOID
**) &FormBrowser2
);
451 ASSERT_EFI_ERROR (Status
);
454 RepairRequired
= FALSE
;
455 ConfigurationRequired
= FALSE
;
458 // Deal with Repair Required
460 DriverHealthInfo
= EfiBootManagerGetDriverHealthInfo (&Count
);
461 for (Index
= 0; Index
< Count
; Index
++) {
462 if (DriverHealthInfo
[Index
].HealthStatus
== EfiDriverHealthStatusConfigurationRequired
) {
463 ConfigurationRequired
= TRUE
;
466 if (DriverHealthInfo
[Index
].HealthStatus
== EfiDriverHealthStatusRepairRequired
) {
467 RepairRequired
= TRUE
;
469 BmDisplayMessages (&DriverHealthInfo
[Index
]);
471 Status
= DriverHealthInfo
[Index
].DriverHealth
->Repair (
472 DriverHealthInfo
[Index
].DriverHealth
,
473 DriverHealthInfo
[Index
].ControllerHandle
,
474 DriverHealthInfo
[Index
].ChildHandle
,
477 if (!EFI_ERROR (Status
) && !ConfigurationRequired
) {
478 Status
= DriverHealthInfo
[Index
].DriverHealth
->GetHealthStatus (
479 DriverHealthInfo
[Index
].DriverHealth
,
480 DriverHealthInfo
[Index
].ControllerHandle
,
481 DriverHealthInfo
[Index
].ChildHandle
,
486 if (!EFI_ERROR (Status
) && (HealthStatus
== EfiDriverHealthStatusConfigurationRequired
)) {
487 ConfigurationRequired
= TRUE
;
493 if (ConfigurationRequired
) {
494 HiiHandles
= HiiGetHiiHandles (NULL
);
495 if (HiiHandles
!= NULL
) {
496 for (Index
= 0; HiiHandles
[Index
] != NULL
; Index
++) {
497 Status
= FormBrowser2
->SendForm (
501 PcdGetPtr (PcdDriverHealthConfigureForm
),
506 if (!EFI_ERROR (Status
)) {
510 FreePool (HiiHandles
);
514 EfiBootManagerFreeDriverHealthInfo (DriverHealthInfo
, Count
);
515 } while (RepairRequired
|| ConfigurationRequired
);
517 RebootRequired
= FALSE
;
518 ReconnectRequired
= FALSE
;
519 DriverHealthInfo
= EfiBootManagerGetDriverHealthInfo (&Count
);
520 for (Index
= 0; Index
< Count
; Index
++) {
522 BmDisplayMessages (&DriverHealthInfo
[Index
]);
524 if (DriverHealthInfo
[Index
].HealthStatus
== EfiDriverHealthStatusReconnectRequired
) {
525 Status
= gBS
->DisconnectController (DriverHealthInfo
[Index
].ControllerHandle
, NULL
, NULL
);
526 if (EFI_ERROR (Status
)) {
528 // Disconnect failed. Need to promote reconnect to a reboot.
530 RebootRequired
= TRUE
;
532 gBS
->ConnectController (DriverHealthInfo
[Index
].ControllerHandle
, NULL
, NULL
, TRUE
);
533 ReconnectRequired
= TRUE
;
537 if (DriverHealthInfo
[Index
].HealthStatus
== EfiDriverHealthStatusRebootRequired
) {
538 RebootRequired
= TRUE
;
541 EfiBootManagerFreeDriverHealthInfo (DriverHealthInfo
, Count
);
544 if (ReconnectRequired
) {
545 BmRepairAllControllers ();
549 CHAR16
*ControllerName
;
551 DriverHealthInfo
= EfiBootManagerGetDriverHealthInfo (&Count
);
552 for (Index
= 0; Index
< Count
; Index
++) {
553 ControllerName
= BmGetControllerName (
554 DriverHealthInfo
[Index
].DriverHealthHandle
,
555 DriverHealthInfo
[Index
].ControllerHandle
,
556 DriverHealthInfo
[Index
].ChildHandle
563 mBmHealthStatusText
[DriverHealthInfo
[Index
].HealthStatus
]
565 if (ControllerName
!= NULL
) {
566 FreePool (ControllerName
);
569 EfiBootManagerFreeDriverHealthInfo (DriverHealthInfo
, Count
);
572 if (RebootRequired
) {
573 DEBUG ((EFI_D_INFO
, "[BDS] One of the Driver Health instances requires rebooting.\n"));
574 gRT
->ResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);