2 Library functions which relates with driver health.
4 Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
6 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "InternalBm.h"
19 GLOBAL_REMOVE_IF_UNREFERENCED
20 CHAR16
*mBmHealthStatusText
[] = {
23 L
"Configuration Required",
25 L
"Reconnect Required",
30 Return the controller name.
32 @param DriverHealthHandle The handle on which the Driver Health protocol instance is retrieved.
33 @param ControllerHandle The handle of a controller that the driver specified by DriverBindingHandle is managing.
34 This handle specifies the controller whose name is to be returned.
35 @param ChildHandle The handle of the child controller to retrieve the name of. This is an
36 optional parameter that may be NULL. It will be NULL for device drivers.
37 It will also be NULL for bus drivers that attempt to retrieve the name
38 of the bus controller. It will not be NULL for a bus driver that attempts
39 to retrieve the name of a child controller.
41 @return A pointer to the Unicode string to return. This Unicode string is the name of the controller
42 specified by ControllerHandle and ChildHandle.
46 IN EFI_HANDLE DriverHealthHandle
,
47 IN EFI_HANDLE ControllerHandle
,
48 IN EFI_HANDLE ChildHandle
52 CHAR16
*ControllerName
;
53 CHAR8
*LanguageVariable
;
55 BOOLEAN Iso639Language
;
56 EFI_COMPONENT_NAME_PROTOCOL
*ComponentName
;
58 ControllerName
= NULL
;
61 // Locate Component Name (2) protocol on the driver binging handle.
63 Iso639Language
= FALSE
;
64 Status
= gBS
->HandleProtocol (
66 &gEfiComponentName2ProtocolGuid
,
67 (VOID
**) &ComponentName
69 if (EFI_ERROR (Status
)) {
70 Status
= gBS
->HandleProtocol (
72 &gEfiComponentNameProtocolGuid
,
73 (VOID
**) &ComponentName
75 if (!EFI_ERROR (Status
)) {
76 Iso639Language
= TRUE
;
80 if (!EFI_ERROR (Status
)) {
81 GetEfiGlobalVariable2 (Iso639Language
? L
"Lang" : L
"PlatformLang", (VOID
**)&LanguageVariable
, NULL
);
82 BestLanguage
= GetBestLanguage(
83 ComponentName
->SupportedLanguages
,
85 (LanguageVariable
!= NULL
) ? LanguageVariable
: "",
86 Iso639Language
? "eng" : "en-US",
89 if (LanguageVariable
!= NULL
) {
90 FreePool (LanguageVariable
);
93 Status
= ComponentName
->GetControllerName (
102 if (!EFI_ERROR (Status
)) {
103 return AllocateCopyPool (StrSize (ControllerName
), ControllerName
);
105 return ConvertDevicePathToText (
106 DevicePathFromHandle (ChildHandle
!= NULL
? ChildHandle
: ControllerHandle
),
114 Display a set of messages returned by the GetHealthStatus () service of the EFI Driver Health Protocol
116 @param DriverHealthInfo Pointer to the Driver Health information entry.
120 IN EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
*DriverHealthInfo
125 CHAR16
*ControllerName
;
127 if (DriverHealthInfo
->MessageList
== NULL
||
128 DriverHealthInfo
->MessageList
[0].HiiHandle
== NULL
) {
132 ControllerName
= BmGetControllerName (
133 DriverHealthInfo
->DriverHealthHandle
,
134 DriverHealthInfo
->ControllerHandle
,
135 DriverHealthInfo
->ChildHandle
138 DEBUG ((EFI_D_INFO
, "Controller: %s\n", ControllerName
));
139 Print (L
"Controller: %s\n", ControllerName
);
140 for (Index
= 0; DriverHealthInfo
->MessageList
[Index
].HiiHandle
!= NULL
; Index
++) {
141 String
= HiiGetString (
142 DriverHealthInfo
->MessageList
[Index
].HiiHandle
,
143 DriverHealthInfo
->MessageList
[Index
].StringId
,
146 if (String
!= NULL
) {
147 Print (L
" %s\n", String
);
148 DEBUG ((EFI_D_INFO
, " %s\n", String
));
153 if (ControllerName
!= NULL
) {
154 FreePool (ControllerName
);
159 The repair notify function.
160 @param Value A value between 0 and Limit that identifies the current progress
161 of the repair operation.
162 @param Limit The maximum value of Value for the current repair operation.
163 If Limit is 0, then the completion progress is indeterminate.
164 For example, a driver that wants to specify progress in percent
165 would use a Limit value of 100.
167 @retval EFI_SUCCESS Successfully return from the notify function.
176 DEBUG ((EFI_D_INFO
, "[BDS]RepairNotify: %d/%d\n", Value
, Limit
));
177 Print (L
"[BDS]RepairNotify: %d/%d\n", Value
, Limit
);
183 Collect the Driver Health status of a single controller.
185 @param DriverHealthInfo A pointer to the array containing all of the platform driver health information.
186 @param Count Return the updated array count.
187 @param DriverHealthHandle The handle on which the Driver Health protocol instance is retrieved.
188 @param ControllerHandle The handle of the controller..
189 @param ChildHandle The handle of the child controller to retrieve the health
190 status on. This is an optional parameter that may be NULL.
192 @retval Status The status returned from GetHealthStatus.
193 @retval EFI_ABORTED The health status is healthy so no further query is needed.
197 BmGetSingleControllerHealthStatus (
198 IN OUT EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
**DriverHealthInfo
,
200 IN EFI_HANDLE DriverHealthHandle
,
201 IN EFI_HANDLE ControllerHandle
, OPTIONAL
202 IN EFI_HANDLE ChildHandle OPTIONAL
206 EFI_DRIVER_HEALTH_PROTOCOL
*DriverHealth
;
207 EFI_DRIVER_HEALTH_HII_MESSAGE
*MessageList
;
208 EFI_HII_HANDLE FormHiiHandle
;
209 EFI_DRIVER_HEALTH_STATUS HealthStatus
;
211 ASSERT (DriverHealthHandle
!= NULL
);
213 // Retrieve the Driver Health Protocol from DriverHandle
215 Status
= gBS
->HandleProtocol (
217 &gEfiDriverHealthProtocolGuid
,
218 (VOID
**) &DriverHealth
220 ASSERT_EFI_ERROR (Status
);
223 if (ControllerHandle
== NULL
) {
225 // If ControllerHandle is NULL, the return the cumulative health status of the driver
227 Status
= DriverHealth
->GetHealthStatus (DriverHealth
, NULL
, NULL
, &HealthStatus
, NULL
, NULL
);
228 if (!EFI_ERROR (Status
) && HealthStatus
== EfiDriverHealthStatusHealthy
) {
229 *DriverHealthInfo
= ReallocatePool (
230 (*Count
) * sizeof (EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
),
231 (*Count
+ 1) * sizeof (EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
),
234 ASSERT (*DriverHealthInfo
!= NULL
);
236 (*DriverHealthInfo
)[*Count
].DriverHealthHandle
= DriverHealthHandle
;
237 (*DriverHealthInfo
)[*Count
].DriverHealth
= DriverHealth
;
238 (*DriverHealthInfo
)[*Count
].HealthStatus
= HealthStatus
;
242 Status
= EFI_ABORTED
;
248 FormHiiHandle
= NULL
;
251 // Collect the health status with the optional HII message list
253 Status
= DriverHealth
->GetHealthStatus (DriverHealth
, ControllerHandle
, ChildHandle
, &HealthStatus
, &MessageList
, &FormHiiHandle
);
254 if (!EFI_ERROR (Status
)) {
255 *DriverHealthInfo
= ReallocatePool (
256 (*Count
) * sizeof (EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
),
257 (*Count
+ 1) * sizeof (EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
),
260 ASSERT (*DriverHealthInfo
!= NULL
);
261 (*DriverHealthInfo
)[*Count
].DriverHealth
= DriverHealth
;
262 (*DriverHealthInfo
)[*Count
].DriverHealthHandle
= DriverHealthHandle
;
263 (*DriverHealthInfo
)[*Count
].ControllerHandle
= ControllerHandle
;
264 (*DriverHealthInfo
)[*Count
].ChildHandle
= ChildHandle
;
265 (*DriverHealthInfo
)[*Count
].HiiHandle
= FormHiiHandle
;
266 (*DriverHealthInfo
)[*Count
].MessageList
= MessageList
;
267 (*DriverHealthInfo
)[*Count
].HealthStatus
= HealthStatus
;
276 Return all the Driver Health information.
278 When the cumulative health status of all the controllers managed by the
279 driver who produces the EFI_DRIVER_HEALTH_PROTOCOL is healthy, only one
280 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO entry is created for such
281 EFI_DRIVER_HEALTH_PROTOCOL instance.
282 Otherwise, every controller creates one EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
283 entry. Additionally every child controller creates one
284 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO entry if the driver is a bus driver.
286 @param Count Return the count of the Driver Health information.
288 @retval NULL No Driver Health information is returned.
289 @retval !NULL Pointer to the Driver Health information array.
291 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
*
293 EfiBootManagerGetDriverHealthInfo (
299 EFI_HANDLE
*DriverHealthHandles
;
300 UINTN DriverHealthIndex
;
303 UINTN ControllerIndex
;
305 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
*DriverHealthInfo
;
308 // Initialize local variables
311 DriverHealthInfo
= NULL
;
313 DriverHealthHandles
= NULL
;
317 Status
= gBS
->LocateHandleBuffer (
319 &gEfiDriverHealthProtocolGuid
,
325 if (Status
== EFI_NOT_FOUND
|| NumHandles
== 0) {
327 // If there are no Driver Health Protocols handles, then return EFI_NOT_FOUND
332 ASSERT_EFI_ERROR (Status
);
333 ASSERT (DriverHealthHandles
!= NULL
);
336 // Check the health status of all controllers in the platform
337 // Start by looping through all the Driver Health Protocol handles in the handle database
339 for (DriverHealthIndex
= 0; DriverHealthIndex
< NumHandles
; DriverHealthIndex
++) {
341 // Get the cumulative health status of the driver
343 Status
= BmGetSingleControllerHealthStatus (&DriverHealthInfo
, Count
, DriverHealthHandles
[DriverHealthIndex
], NULL
, NULL
);
344 if (EFI_ERROR (Status
)) {
349 // See if the list of all handles in the handle database has been retrieved
352 if (Handles
== NULL
) {
354 // Retrieve the list of all handles from the handle database
356 Status
= gBS
->LocateHandleBuffer (
363 ASSERT_EFI_ERROR (Status
);
366 // Loop through all the controller handles in the handle database
368 for (ControllerIndex
= 0; ControllerIndex
< HandleCount
; ControllerIndex
++) {
369 Status
= BmGetSingleControllerHealthStatus (&DriverHealthInfo
, Count
, DriverHealthHandles
[DriverHealthIndex
], Handles
[ControllerIndex
], NULL
);
370 if (EFI_ERROR (Status
)) {
375 // Loop through all the child handles in the handle database
377 for (ChildIndex
= 0; ChildIndex
< HandleCount
; ChildIndex
++) {
378 Status
= BmGetSingleControllerHealthStatus (&DriverHealthInfo
, Count
, DriverHealthHandles
[DriverHealthIndex
], Handles
[ControllerIndex
], Handles
[ChildIndex
]);
379 if (EFI_ERROR (Status
)) {
386 Status
= EFI_SUCCESS
;
388 if (Handles
!= NULL
) {
391 if (DriverHealthHandles
!= NULL
) {
392 FreePool (DriverHealthHandles
);
395 return DriverHealthInfo
;
399 Free the Driver Health information array.
401 @param DriverHealthInfo Pointer to array of the Driver Health information.
402 @param Count Count of the array.
404 @retval EFI_SUCCESS The array is freed.
405 @retval EFI_INVALID_PARAMETER The array is NULL.
409 EfiBootManagerFreeDriverHealthInfo (
410 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
*DriverHealthInfo
,
416 for (Index
= 0; Index
< Count
; Index
++) {
417 if (DriverHealthInfo
[Index
].MessageList
!= NULL
) {
418 FreePool (DriverHealthInfo
[Index
].MessageList
);
421 return gBS
->FreePool (DriverHealthInfo
);
425 Repair all the controllers according to the Driver Health status queried.
427 @param ReconnectRepairCount To record the number of recursive call of
428 this function itself.
431 BmRepairAllControllers (
432 UINTN ReconnectRepairCount
436 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
*DriverHealthInfo
;
437 EFI_DRIVER_HEALTH_STATUS HealthStatus
;
440 BOOLEAN RepairRequired
;
441 BOOLEAN ConfigurationRequired
;
442 BOOLEAN ReconnectRequired
;
443 BOOLEAN RebootRequired
;
444 EFI_HII_HANDLE
*HiiHandles
;
445 EFI_FORM_BROWSER2_PROTOCOL
*FormBrowser2
;
446 UINT32 MaxRepairCount
;
450 // Configure PcdDriverHealthConfigureForm to ZeroGuid to disable driver health check.
452 if (IsZeroGuid (PcdGetPtr (PcdDriverHealthConfigureForm
))) {
456 Status
= gBS
->LocateProtocol (&gEfiFormBrowser2ProtocolGuid
, NULL
, (VOID
**) &FormBrowser2
);
457 ASSERT_EFI_ERROR (Status
);
459 MaxRepairCount
= PcdGet32 (PcdMaxRepairCount
);
463 RepairRequired
= FALSE
;
464 ConfigurationRequired
= FALSE
;
467 // Deal with Repair Required
469 DriverHealthInfo
= EfiBootManagerGetDriverHealthInfo (&Count
);
470 for (Index
= 0; Index
< Count
; Index
++) {
471 if (DriverHealthInfo
[Index
].HealthStatus
== EfiDriverHealthStatusConfigurationRequired
) {
472 ConfigurationRequired
= TRUE
;
475 if (DriverHealthInfo
[Index
].HealthStatus
== EfiDriverHealthStatusRepairRequired
) {
476 RepairRequired
= TRUE
;
478 BmDisplayMessages (&DriverHealthInfo
[Index
]);
480 Status
= DriverHealthInfo
[Index
].DriverHealth
->Repair (
481 DriverHealthInfo
[Index
].DriverHealth
,
482 DriverHealthInfo
[Index
].ControllerHandle
,
483 DriverHealthInfo
[Index
].ChildHandle
,
486 if (!EFI_ERROR (Status
) && !ConfigurationRequired
) {
487 Status
= DriverHealthInfo
[Index
].DriverHealth
->GetHealthStatus (
488 DriverHealthInfo
[Index
].DriverHealth
,
489 DriverHealthInfo
[Index
].ControllerHandle
,
490 DriverHealthInfo
[Index
].ChildHandle
,
495 if (!EFI_ERROR (Status
) && (HealthStatus
== EfiDriverHealthStatusConfigurationRequired
)) {
496 ConfigurationRequired
= TRUE
;
502 if (ConfigurationRequired
) {
503 HiiHandles
= HiiGetHiiHandles (NULL
);
504 if (HiiHandles
!= NULL
) {
505 for (Index
= 0; HiiHandles
[Index
] != NULL
; Index
++) {
506 Status
= FormBrowser2
->SendForm (
510 PcdGetPtr (PcdDriverHealthConfigureForm
),
515 if (!EFI_ERROR (Status
)) {
519 FreePool (HiiHandles
);
523 EfiBootManagerFreeDriverHealthInfo (DriverHealthInfo
, Count
);
525 } while ((RepairRequired
|| ConfigurationRequired
) && ((MaxRepairCount
== 0) || (RepairCount
< MaxRepairCount
)));
527 RebootRequired
= FALSE
;
528 ReconnectRequired
= FALSE
;
529 DriverHealthInfo
= EfiBootManagerGetDriverHealthInfo (&Count
);
530 for (Index
= 0; Index
< Count
; Index
++) {
532 BmDisplayMessages (&DriverHealthInfo
[Index
]);
534 if (DriverHealthInfo
[Index
].HealthStatus
== EfiDriverHealthStatusReconnectRequired
) {
535 Status
= gBS
->DisconnectController (DriverHealthInfo
[Index
].ControllerHandle
, NULL
, NULL
);
536 if (EFI_ERROR (Status
)) {
538 // Disconnect failed. Need to promote reconnect to a reboot.
540 RebootRequired
= TRUE
;
542 gBS
->ConnectController (DriverHealthInfo
[Index
].ControllerHandle
, NULL
, NULL
, TRUE
);
543 ReconnectRequired
= TRUE
;
547 if (DriverHealthInfo
[Index
].HealthStatus
== EfiDriverHealthStatusRebootRequired
) {
548 RebootRequired
= TRUE
;
551 EfiBootManagerFreeDriverHealthInfo (DriverHealthInfo
, Count
);
555 CHAR16
*ControllerName
;
557 DriverHealthInfo
= EfiBootManagerGetDriverHealthInfo (&Count
);
558 for (Index
= 0; Index
< Count
; Index
++) {
559 ControllerName
= BmGetControllerName (
560 DriverHealthInfo
[Index
].DriverHealthHandle
,
561 DriverHealthInfo
[Index
].ControllerHandle
,
562 DriverHealthInfo
[Index
].ChildHandle
569 mBmHealthStatusText
[DriverHealthInfo
[Index
].HealthStatus
]
571 if (ControllerName
!= NULL
) {
572 FreePool (ControllerName
);
575 EfiBootManagerFreeDriverHealthInfo (DriverHealthInfo
, Count
);
578 if (ReconnectRequired
) {
579 if (ReconnectRepairCount
< MAX_RECONNECT_REPAIR
) {
580 BmRepairAllControllers (ReconnectRepairCount
+ 1);
582 DEBUG ((DEBUG_ERROR
, "[%a:%d] Repair failed after %d retries.\n",
583 __FUNCTION__
, __LINE__
, ReconnectRepairCount
));
587 if (RebootRequired
) {
588 DEBUG ((EFI_D_INFO
, "[BDS] One of the Driver Health instances requires rebooting.\n"));
589 gRT
->ResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);