]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/UefiBootManagerLib/BmDriverHealth.c
MdeModulePkg: Fix SortLib library class name typo.
[mirror_edk2.git] / MdeModulePkg / Library / UefiBootManagerLib / BmDriverHealth.c
1 /** @file
2 Library functions which relates with driver health.
3
4 Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "InternalBm.h"
16
17 GLOBAL_REMOVE_IF_UNREFERENCED
18 CHAR16 *mBmHealthStatusText[] = {
19 L"Healthy",
20 L"Repair Required",
21 L"Configuration Required",
22 L"Failed",
23 L"Reconnect Required",
24 L"Reboot Required"
25 };
26
27 /**
28 Return the controller name.
29
30 @param DriverHealthHandle The handle on which the Driver Health protocol instance is retrieved.
31 @param ControllerHandle The handle of a controller that the driver specified by DriverBindingHandle is managing.
32 This handle specifies the controller whose name is to be returned.
33 @param ChildHandle The handle of the child controller to retrieve the name of. This is an
34 optional parameter that may be NULL. It will be NULL for device drivers.
35 It will also be NULL for bus drivers that attempt to retrieve the name
36 of the bus controller. It will not be NULL for a bus driver that attempts
37 to retrieve the name of a child controller.
38
39 @return A pointer to the Unicode string to return. This Unicode string is the name of the controller
40 specified by ControllerHandle and ChildHandle.
41 **/
42 CHAR16 *
43 BmGetControllerName (
44 IN EFI_HANDLE DriverHealthHandle,
45 IN EFI_HANDLE ControllerHandle,
46 IN EFI_HANDLE ChildHandle
47 )
48 {
49 EFI_STATUS Status;
50 CHAR16 *ControllerName;
51 CHAR8 *LanguageVariable;
52 CHAR8 *BestLanguage;
53 BOOLEAN Iso639Language;
54 EFI_COMPONENT_NAME_PROTOCOL *ComponentName;
55
56 ControllerName = NULL;
57
58 //
59 // Locate Component Name (2) protocol on the driver binging handle.
60 //
61 Iso639Language = FALSE;
62 Status = gBS->HandleProtocol (
63 DriverHealthHandle,
64 &gEfiComponentName2ProtocolGuid,
65 (VOID **) &ComponentName
66 );
67 if (EFI_ERROR (Status)) {
68 Status = gBS->HandleProtocol (
69 DriverHealthHandle,
70 &gEfiComponentNameProtocolGuid,
71 (VOID **) &ComponentName
72 );
73 if (!EFI_ERROR (Status)) {
74 Iso639Language = TRUE;
75 }
76 }
77
78 if (!EFI_ERROR (Status)) {
79 LanguageVariable = GetEfiGlobalVariable (Iso639Language ? L"Lang" : L"PlatformLang");
80 BestLanguage = GetBestLanguage(
81 ComponentName->SupportedLanguages,
82 Iso639Language,
83 (LanguageVariable != NULL) ? LanguageVariable : "",
84 Iso639Language ? "eng" : "en-US",
85 NULL
86 );
87 if (LanguageVariable != NULL) {
88 FreePool (LanguageVariable);
89 }
90
91 Status = ComponentName->GetControllerName (
92 ComponentName,
93 ControllerHandle,
94 ChildHandle,
95 BestLanguage,
96 &ControllerName
97 );
98 }
99
100 if (!EFI_ERROR (Status)) {
101 return AllocateCopyPool (StrSize (ControllerName), ControllerName);
102 } else {
103 return ConvertDevicePathToText (
104 DevicePathFromHandle (ChildHandle != NULL ? ChildHandle : ControllerHandle),
105 FALSE,
106 FALSE
107 );
108 }
109 }
110
111 /**
112 Display a set of messages returned by the GetHealthStatus () service of the EFI Driver Health Protocol
113
114 @param DriverHealthInfo Pointer to the Driver Health information entry.
115 **/
116 VOID
117 BmDisplayMessages (
118 IN EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO *DriverHealthInfo
119 )
120 {
121 UINTN Index;
122 EFI_STRING String;
123 CHAR16 *ControllerName;
124
125 if (DriverHealthInfo->MessageList == NULL ||
126 DriverHealthInfo->MessageList[0].HiiHandle == NULL) {
127 return;
128 }
129
130 ControllerName = BmGetControllerName (
131 DriverHealthInfo->DriverHealthHandle,
132 DriverHealthInfo->ControllerHandle,
133 DriverHealthInfo->ChildHandle
134 );
135
136 DEBUG ((EFI_D_INFO, "Controller: %s\n", ControllerName));
137 Print (L"Controller: %s\n", ControllerName);
138 for (Index = 0; DriverHealthInfo->MessageList[Index].HiiHandle != NULL; Index++) {
139 String = HiiGetString (
140 DriverHealthInfo->MessageList[Index].HiiHandle,
141 DriverHealthInfo->MessageList[Index].StringId,
142 NULL
143 );
144 if (String != NULL) {
145 Print (L" %s\n", String);
146 DEBUG ((EFI_D_INFO, " %s\n", String));
147 FreePool (String);
148 }
149 }
150
151 if (ControllerName != NULL) {
152 FreePool (ControllerName);
153 }
154 }
155
156 /**
157 The repair notify function.
158 @param Value A value between 0 and Limit that identifies the current progress
159 of the repair operation.
160 @param Limit The maximum value of Value for the current repair operation.
161 If Limit is 0, then the completion progress is indeterminate.
162 For example, a driver that wants to specify progress in percent
163 would use a Limit value of 100.
164
165 @retval EFI_SUCCESS Successfully return from the notify function.
166 **/
167 EFI_STATUS
168 EFIAPI
169 BmRepairNotify (
170 IN UINTN Value,
171 IN UINTN Limit
172 )
173 {
174 DEBUG ((EFI_D_INFO, "[BDS]RepairNotify: %d/%d\n", Value, Limit));
175 Print (L"[BDS]RepairNotify: %d/%d\n", Value, Limit);
176
177 return EFI_SUCCESS;
178 }
179
180 /**
181 Collect the Driver Health status of a single controller.
182
183 @param DriverHealthInfo A pointer to the array containing all of the platform driver health information.
184 @param Count Return the updated array count.
185 @param DriverHealthHandle The handle on which the Driver Health protocol instance is retrieved.
186 @param ControllerHandle The handle of the controller..
187 @param ChildHandle The handle of the child controller to retrieve the health
188 status on. This is an optional parameter that may be NULL.
189
190 @retval Status The status returned from GetHealthStatus.
191 @retval EFI_ABORTED The health status is healthy so no further query is needed.
192
193 **/
194 EFI_STATUS
195 BmGetSingleControllerHealthStatus (
196 IN OUT EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO **DriverHealthInfo,
197 IN OUT UINTN *Count,
198 IN EFI_HANDLE DriverHealthHandle,
199 IN EFI_HANDLE ControllerHandle, OPTIONAL
200 IN EFI_HANDLE ChildHandle OPTIONAL
201 )
202 {
203 EFI_STATUS Status;
204 EFI_DRIVER_HEALTH_PROTOCOL *DriverHealth;
205 EFI_DRIVER_HEALTH_HII_MESSAGE *MessageList;
206 EFI_HII_HANDLE FormHiiHandle;
207 EFI_DRIVER_HEALTH_STATUS HealthStatus;
208
209 ASSERT (DriverHealthHandle != NULL);
210 //
211 // Retrieve the Driver Health Protocol from DriverHandle
212 //
213 Status = gBS->HandleProtocol (
214 DriverHealthHandle,
215 &gEfiDriverHealthProtocolGuid,
216 (VOID **) &DriverHealth
217 );
218 ASSERT_EFI_ERROR (Status);
219
220
221 if (ControllerHandle == NULL) {
222 //
223 // If ControllerHandle is NULL, the return the cumulative health status of the driver
224 //
225 Status = DriverHealth->GetHealthStatus (DriverHealth, NULL, NULL, &HealthStatus, NULL, NULL);
226 if (!EFI_ERROR (Status) && HealthStatus == EfiDriverHealthStatusHealthy) {
227 *DriverHealthInfo = ReallocatePool (
228 (*Count) * sizeof (EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO),
229 (*Count + 1) * sizeof (EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO),
230 *DriverHealthInfo
231 );
232 ASSERT (*DriverHealthInfo != NULL);
233
234 (*DriverHealthInfo)[*Count].DriverHealthHandle = DriverHealthHandle;
235 (*DriverHealthInfo)[*Count].DriverHealth = DriverHealth;
236 (*DriverHealthInfo)[*Count].HealthStatus = HealthStatus;
237
238 *Count = *Count + 1;
239
240 Status = EFI_ABORTED;
241 }
242 return Status;
243 }
244
245 MessageList = NULL;
246 FormHiiHandle = NULL;
247
248 //
249 // Collect the health status with the optional HII message list
250 //
251 Status = DriverHealth->GetHealthStatus (DriverHealth, ControllerHandle, ChildHandle, &HealthStatus, &MessageList, &FormHiiHandle);
252 if (!EFI_ERROR (Status)) {
253 *DriverHealthInfo = ReallocatePool (
254 (*Count) * sizeof (EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO),
255 (*Count + 1) * sizeof (EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO),
256 *DriverHealthInfo
257 );
258 ASSERT (*DriverHealthInfo != NULL);
259 (*DriverHealthInfo)[*Count].DriverHealth = DriverHealth;
260 (*DriverHealthInfo)[*Count].DriverHealthHandle = DriverHealthHandle;
261 (*DriverHealthInfo)[*Count].ControllerHandle = ControllerHandle;
262 (*DriverHealthInfo)[*Count].ChildHandle = ChildHandle;
263 (*DriverHealthInfo)[*Count].HiiHandle = FormHiiHandle;
264 (*DriverHealthInfo)[*Count].MessageList = MessageList;
265 (*DriverHealthInfo)[*Count].HealthStatus = HealthStatus;
266
267 *Count = *Count + 1;
268 }
269
270 return Status;
271 }
272
273 /**
274 Return all the Driver Health information.
275
276 When the cumulative health status of all the controllers managed by the
277 driver who produces the EFI_DRIVER_HEALTH_PROTOCOL is healthy, only one
278 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO entry is created for such
279 EFI_DRIVER_HEALTH_PROTOCOL instance.
280 Otherwise, every controller creates one EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO
281 entry. Additionally every child controller creates one
282 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO entry if the driver is a bus driver.
283
284 @param Count Return the count of the Driver Health information.
285
286 @retval NULL No Driver Health information is returned.
287 @retval !NULL Pointer to the Driver Health information array.
288 **/
289 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO *
290 EFIAPI
291 EfiBootManagerGetDriverHealthInfo (
292 UINTN *Count
293 )
294 {
295 EFI_STATUS Status;
296 UINTN NumHandles;
297 EFI_HANDLE *DriverHealthHandles;
298 EFI_DRIVER_HEALTH_STATUS HealthStatus;
299 UINTN DriverHealthIndex;
300 EFI_HANDLE *Handles;
301 UINTN HandleCount;
302 UINTN ControllerIndex;
303 UINTN ChildIndex;
304 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO *DriverHealthInfo;
305
306 //
307 // Initialize local variables
308 //
309 *Count = 0;
310 DriverHealthInfo = NULL;
311 Handles = NULL;
312 DriverHealthHandles = NULL;
313 NumHandles = 0;
314 HandleCount = 0;
315
316 HealthStatus = EfiDriverHealthStatusHealthy;
317
318 Status = gBS->LocateHandleBuffer (
319 ByProtocol,
320 &gEfiDriverHealthProtocolGuid,
321 NULL,
322 &NumHandles,
323 &DriverHealthHandles
324 );
325
326 if (Status == EFI_NOT_FOUND || NumHandles == 0) {
327 //
328 // If there are no Driver Health Protocols handles, then return EFI_NOT_FOUND
329 //
330 return NULL;
331 }
332
333 ASSERT_EFI_ERROR (Status);
334 ASSERT (DriverHealthHandles != NULL);
335
336 //
337 // Check the health status of all controllers in the platform
338 // Start by looping through all the Driver Health Protocol handles in the handle database
339 //
340 for (DriverHealthIndex = 0; DriverHealthIndex < NumHandles; DriverHealthIndex++) {
341 //
342 // Get the cumulative health status of the driver
343 //
344 Status = BmGetSingleControllerHealthStatus (&DriverHealthInfo, Count, DriverHealthHandles[DriverHealthIndex], NULL, NULL);
345 if (EFI_ERROR (Status)) {
346 continue;
347 }
348
349 //
350 // See if the list of all handles in the handle database has been retrieved
351 //
352 //
353 if (Handles == NULL) {
354 //
355 // Retrieve the list of all handles from the handle database
356 //
357 Status = gBS->LocateHandleBuffer (
358 AllHandles,
359 NULL,
360 NULL,
361 &HandleCount,
362 &Handles
363 );
364 ASSERT_EFI_ERROR (Status);
365 }
366 //
367 // Loop through all the controller handles in the handle database
368 //
369 for (ControllerIndex = 0; ControllerIndex < HandleCount; ControllerIndex++) {
370 Status = BmGetSingleControllerHealthStatus (&DriverHealthInfo, Count, DriverHealthHandles[DriverHealthIndex], Handles[ControllerIndex], NULL);
371 if (EFI_ERROR (Status)) {
372 continue;
373 }
374
375 //
376 // Loop through all the child handles in the handle database
377 //
378 for (ChildIndex = 0; ChildIndex < HandleCount; ChildIndex++) {
379 Status = BmGetSingleControllerHealthStatus (&DriverHealthInfo, Count, DriverHealthHandles[DriverHealthIndex], Handles[ControllerIndex], Handles[ChildIndex]);
380 if (EFI_ERROR (Status)) {
381 continue;
382 }
383 }
384 }
385 }
386
387 Status = EFI_SUCCESS;
388
389 if (Handles != NULL) {
390 FreePool (Handles);
391 }
392 if (DriverHealthHandles != NULL) {
393 FreePool (DriverHealthHandles);
394 }
395
396 return DriverHealthInfo;
397 }
398
399 /**
400 Free the Driver Health information array.
401
402 @param DriverHealthInfo Pointer to array of the Driver Health information.
403 @param Count Count of the array.
404
405 @retval EFI_SUCCESS The array is freed.
406 @retval EFI_INVALID_PARAMETER The array is NULL.
407 **/
408 EFI_STATUS
409 EFIAPI
410 EfiBootManagerFreeDriverHealthInfo (
411 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO *DriverHealthInfo,
412 UINTN Count
413 )
414 {
415 UINTN Index;
416
417 for (Index = 0; Index < Count; Index++) {
418 if (DriverHealthInfo[Index].MessageList != NULL) {
419 FreePool (DriverHealthInfo[Index].MessageList);
420 }
421 }
422 return gBS->FreePool (DriverHealthInfo);
423 }
424
425 /**
426 Repair all the controllers according to the Driver Health status queried.
427 **/
428 VOID
429 BmRepairAllControllers (
430 VOID
431 )
432 {
433 EFI_STATUS Status;
434 EFI_BOOT_MANAGER_DRIVER_HEALTH_INFO *DriverHealthInfo;
435 EFI_DRIVER_HEALTH_STATUS HealthStatus;
436 UINTN Count;
437 UINTN Index;
438 BOOLEAN RepairRequired;
439 BOOLEAN ConfigurationRequired;
440 BOOLEAN ReconnectRequired;
441 BOOLEAN RebootRequired;
442 EFI_HII_HANDLE *HiiHandles;
443 EFI_FORM_BROWSER2_PROTOCOL *FormBrowser2;
444
445 //
446 // Configure PcdDriverHealthConfigureForm to ZeroGuid to disable driver health check.
447 //
448 if (CompareGuid (PcdGetPtr (PcdDriverHealthConfigureForm), &gZeroGuid)) {
449 return;
450 }
451
452 Status = gBS->LocateProtocol (&gEfiFormBrowser2ProtocolGuid, NULL, (VOID **) &FormBrowser2);
453 ASSERT_EFI_ERROR (Status);
454
455 do {
456 RepairRequired = FALSE;
457 ConfigurationRequired = FALSE;
458
459 //
460 // Deal with Repair Required
461 //
462 DriverHealthInfo = EfiBootManagerGetDriverHealthInfo (&Count);
463 for (Index = 0; Index < Count; Index++) {
464 if (DriverHealthInfo[Index].HealthStatus == EfiDriverHealthStatusConfigurationRequired) {
465 ConfigurationRequired = TRUE;
466 }
467
468 if (DriverHealthInfo[Index].HealthStatus == EfiDriverHealthStatusRepairRequired) {
469 RepairRequired = TRUE;
470
471 BmDisplayMessages (&DriverHealthInfo[Index]);
472
473 Status = DriverHealthInfo[Index].DriverHealth->Repair (
474 DriverHealthInfo[Index].DriverHealth,
475 DriverHealthInfo[Index].ControllerHandle,
476 DriverHealthInfo[Index].ChildHandle,
477 BmRepairNotify
478 );
479 if (!EFI_ERROR (Status) && !ConfigurationRequired) {
480 Status = DriverHealthInfo[Index].DriverHealth->GetHealthStatus (
481 DriverHealthInfo[Index].DriverHealth,
482 DriverHealthInfo[Index].ControllerHandle,
483 DriverHealthInfo[Index].ChildHandle,
484 &HealthStatus,
485 NULL,
486 NULL
487 );
488 if (!EFI_ERROR (Status) && (HealthStatus == EfiDriverHealthStatusConfigurationRequired)) {
489 ConfigurationRequired = TRUE;
490 }
491 }
492 }
493 }
494
495 if (ConfigurationRequired) {
496 HiiHandles = HiiGetHiiHandles (NULL);
497 if (HiiHandles != NULL) {
498 for (Index = 0; HiiHandles[Index] != NULL; Index++) {
499 Status = FormBrowser2->SendForm (
500 FormBrowser2,
501 &HiiHandles[Index],
502 1,
503 PcdGetPtr (PcdDriverHealthConfigureForm),
504 0,
505 NULL,
506 NULL
507 );
508 if (!EFI_ERROR (Status)) {
509 break;
510 }
511 }
512 FreePool (HiiHandles);
513 }
514 }
515
516 EfiBootManagerFreeDriverHealthInfo (DriverHealthInfo, Count);
517 } while (RepairRequired || ConfigurationRequired);
518
519 RebootRequired = FALSE;
520 ReconnectRequired = FALSE;
521 DriverHealthInfo = EfiBootManagerGetDriverHealthInfo (&Count);
522 for (Index = 0; Index < Count; Index++) {
523
524 BmDisplayMessages (&DriverHealthInfo[Index]);
525
526 if (DriverHealthInfo[Index].HealthStatus == EfiDriverHealthStatusReconnectRequired) {
527 Status = gBS->DisconnectController (DriverHealthInfo[Index].ControllerHandle, NULL, NULL);
528 if (EFI_ERROR (Status)) {
529 //
530 // Disconnect failed. Need to promote reconnect to a reboot.
531 //
532 RebootRequired = TRUE;
533 } else {
534 gBS->ConnectController (DriverHealthInfo[Index].ControllerHandle, NULL, NULL, TRUE);
535 ReconnectRequired = TRUE;
536 }
537 }
538
539 if (DriverHealthInfo[Index].HealthStatus == EfiDriverHealthStatusRebootRequired) {
540 RebootRequired = TRUE;
541 }
542 }
543 EfiBootManagerFreeDriverHealthInfo (DriverHealthInfo, Count);
544
545
546 if (ReconnectRequired) {
547 BmRepairAllControllers ();
548 }
549
550 DEBUG_CODE (
551 CHAR16 *ControllerName;
552
553 DriverHealthInfo = EfiBootManagerGetDriverHealthInfo (&Count);
554 for (Index = 0; Index < Count; Index++) {
555 ControllerName = BmGetControllerName (
556 DriverHealthInfo[Index].DriverHealthHandle,
557 DriverHealthInfo[Index].ControllerHandle,
558 DriverHealthInfo[Index].ChildHandle
559 );
560 DEBUG ((
561 EFI_D_INFO,
562 "%02d: %s - %s\n",
563 Index,
564 ControllerName,
565 mBmHealthStatusText[DriverHealthInfo[Index].HealthStatus]
566 ));
567 if (ControllerName != NULL) {
568 FreePool (ControllerName);
569 }
570 }
571 EfiBootManagerFreeDriverHealthInfo (DriverHealthInfo, Count);
572 );
573
574 if (RebootRequired) {
575 DEBUG ((EFI_D_INFO, "[BDS] One of the Driver Health instances requires rebooting.\n"));
576 gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);
577 }
578 }