2 Register a status code handler for printing the Boot Manager's LoadImage()
3 and StartImage() preparations, and return codes, to the UEFI console.
5 This feature enables users that are not accustomed to analyzing the firmware
6 log to glean some information about UEFI boot option processing (loading and
9 This library instance filters out (ignores) status codes that are not
10 reported by the containing firmware module. The intent is to link this
11 library instance into BdsDxe via PlatformBootManagerLib (which BdsDxe depends
12 upon), then catch only those status codes that BdsDxe reports (which happens
13 via UefiBootManagerLib). Status codes reported by other modules (such as
14 UiApp), via UefiBootManagerLib or otherwise, are meant to be ignored.
16 Copyright (C) 2019, Red Hat, Inc.
18 SPDX-License-Identifier: BSD-2-Clause-Patent
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/DevicePathLib.h>
24 #include <Library/MemoryAllocationLib.h>
25 #include <Library/PcdLib.h>
26 #include <Library/PrintLib.h>
27 #include <Library/UefiBootManagerLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/UefiLib.h>
30 #include <Library/UefiRuntimeServicesTableLib.h>
32 #include <Protocol/ReportStatusCodeHandler.h>
34 #include <Guid/GlobalVariable.h>
35 #include <Guid/StatusCodeDataTypeId.h>
37 #include <Pi/PiStatusCode.h>
41 // Convenience variables for the status codes that are relevant for LoadImage()
42 // and StartImage() preparations and return codes.
44 STATIC EFI_STATUS_CODE_VALUE mLoadPrep
;
45 STATIC EFI_STATUS_CODE_VALUE mLoadFail
;
46 STATIC EFI_STATUS_CODE_VALUE mStartPrep
;
47 STATIC EFI_STATUS_CODE_VALUE mStartFail
;
51 Handle status codes reported through ReportStatusCodeLib /
52 EFI_STATUS_CODE_PROTOCOL.ReportStatusCode(). Format matching status codes to
55 The highest TPL at which this handler can be registered with
56 EFI_RSC_HANDLER_PROTOCOL.Register() is TPL_CALLBACK. That's because
57 HandleStatusCode() uses the UEFI variable services.
59 The parameter list of this function precisely matches that of
60 EFI_STATUS_CODE_PROTOCOL.ReportStatusCode().
62 The return status of this function is ignored by the caller, but the function
63 still returns sensible codes:
65 @retval EFI_SUCCESS The status code has been processed; either
66 as a no-op, due to filtering, or by
67 formatting it to the system console.
69 @retval EFI_INVALID_PARAMETER Unknown or malformed contents have been
72 @retval EFI_INCOMPATIBLE_VERSION Unexpected UEFI variable behavior has been
75 @return Error codes propagated from underlying
82 IN EFI_STATUS_CODE_TYPE CodeType
,
83 IN EFI_STATUS_CODE_VALUE Value
,
85 IN EFI_GUID
*CallerId
,
86 IN EFI_STATUS_CODE_DATA
*Data
92 CHAR16 BootOptionName
[ARRAY_SIZE (L
"Boot####")];
93 EFI_BOOT_MANAGER_LOAD_OPTION BmBootOption
;
94 BOOLEAN DevPathStringIsDynamic
;
95 CHAR16
*DevPathString
;
98 // Ignore all status codes that are irrelevant for LoadImage() and
99 // StartImage() preparations and return codes.
101 if (Value
!= mLoadPrep
&& Value
!= mLoadFail
&&
102 Value
!= mStartPrep
&& Value
!= mStartFail
) {
106 // Ignore status codes that are not reported by the same containing module.
108 if (!CompareGuid (CallerId
, &gEfiCallerIdGuid
)) {
113 // Sanity-check Data in case of failure reports.
115 if ((Value
== mLoadFail
|| Value
== mStartFail
) &&
117 Data
->HeaderSize
!= sizeof *Data
||
118 Data
->Size
!= sizeof (EFI_RETURN_STATUS_EXTENDED_DATA
) - sizeof *Data
||
119 !CompareGuid (&Data
->Type
, &gEfiStatusCodeSpecificDataGuid
))) {
120 DEBUG ((DEBUG_ERROR
, "%a:%a: malformed Data\n", gEfiCallerBaseName
,
122 return EFI_INVALID_PARAMETER
;
126 // Get the number of the Boot#### option that the status code applies to.
128 VariableSize
= sizeof BootCurrent
;
129 Status
= gRT
->GetVariable (EFI_BOOT_CURRENT_VARIABLE_NAME
,
130 &gEfiGlobalVariableGuid
, NULL
/* Attributes */,
131 &VariableSize
, &BootCurrent
);
132 if (EFI_ERROR (Status
)) {
133 DEBUG ((DEBUG_ERROR
, "%a:%a: failed to get %g:\"%s\": %r\n",
134 gEfiCallerBaseName
, __FUNCTION__
, &gEfiGlobalVariableGuid
,
135 EFI_BOOT_CURRENT_VARIABLE_NAME
, Status
));
138 if (VariableSize
!= sizeof BootCurrent
) {
139 DEBUG ((DEBUG_ERROR
, "%a:%a: got %Lu bytes for %g:\"%s\", expected %Lu\n",
140 gEfiCallerBaseName
, __FUNCTION__
, (UINT64
)VariableSize
,
141 &gEfiGlobalVariableGuid
, EFI_BOOT_CURRENT_VARIABLE_NAME
,
142 (UINT64
)sizeof BootCurrent
));
143 return EFI_INCOMPATIBLE_VERSION
;
147 // Get the Boot#### option that the status code applies to.
149 UnicodeSPrint (BootOptionName
, sizeof BootOptionName
, L
"Boot%04x",
151 Status
= EfiBootManagerVariableToLoadOption (BootOptionName
, &BmBootOption
);
152 if (EFI_ERROR (Status
)) {
154 "%a:%a: EfiBootManagerVariableToLoadOption(\"%s\"): %r\n",
155 gEfiCallerBaseName
, __FUNCTION__
, BootOptionName
, Status
));
160 // Format the device path.
162 DevPathStringIsDynamic
= TRUE
;
163 DevPathString
= ConvertDevicePathToText (
164 BmBootOption
.FilePath
,
165 FALSE
, // DisplayOnly
166 FALSE
// AllowShortcuts
168 if (DevPathString
== NULL
) {
169 DevPathStringIsDynamic
= FALSE
;
170 DevPathString
= L
"<out of memory while formatting device path>";
174 // Print the message to the console.
176 if (Value
== mLoadPrep
|| Value
== mStartPrep
) {
178 L
"%a: %a %s \"%s\" from %s\n",
180 Value
== mLoadPrep
? "loading" : "starting",
182 BmBootOption
.Description
,
187 L
"%a: failed to %a %s \"%s\" from %s: %r\n",
189 Value
== mLoadFail
? "load" : "start",
191 BmBootOption
.Description
,
193 ((EFI_RETURN_STATUS_EXTENDED_DATA
*)Data
)->ReturnStatus
200 if (DevPathStringIsDynamic
) {
201 FreePool (DevPathString
);
203 EfiBootManagerFreeLoadOption (&BmBootOption
);
209 Unregister HandleStatusCode() at ExitBootServices().
211 (See EFI_RSC_HANDLER_PROTOCOL in Volume 3 of the Platform Init spec.)
213 @param[in] Event Event whose notification function is being invoked.
215 @param[in] Context Pointer to EFI_RSC_HANDLER_PROTOCOL, originally looked up
216 when HandleStatusCode() was registered.
221 UnregisterAtExitBootServices (
226 EFI_RSC_HANDLER_PROTOCOL
*StatusCodeRouter
;
228 StatusCodeRouter
= Context
;
229 StatusCodeRouter
->Unregister (HandleStatusCode
);
234 Register a status code handler for printing the Boot Manager's LoadImage()
235 and StartImage() preparations, and return codes, to the UEFI console.
237 @retval EFI_SUCCESS The status code handler has been successfully
240 @return Error codes propagated from boot services and from
241 EFI_RSC_HANDLER_PROTOCOL.
245 PlatformBmPrintScRegisterHandler (
250 EFI_RSC_HANDLER_PROTOCOL
*StatusCodeRouter
;
251 EFI_EVENT ExitBootEvent
;
253 Status
= gBS
->LocateProtocol (&gEfiRscHandlerProtocolGuid
,
254 NULL
/* Registration */, (VOID
**)&StatusCodeRouter
);
255 ASSERT_EFI_ERROR (Status
);
256 if (EFI_ERROR (Status
)) {
261 // Set the EFI_STATUS_CODE_VALUE convenience variables.
263 mLoadPrep
= PcdGet32 (PcdProgressCodeOsLoaderLoad
);
264 mLoadFail
= (EFI_SOFTWARE_DXE_BS_DRIVER
|
265 EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR
);
266 mStartPrep
= PcdGet32 (PcdProgressCodeOsLoaderStart
);
267 mStartFail
= (EFI_SOFTWARE_DXE_BS_DRIVER
|
268 EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED
);
271 // Register the handler callback.
273 Status
= StatusCodeRouter
->Register (HandleStatusCode
, TPL_CALLBACK
);
274 if (EFI_ERROR (Status
)) {
275 DEBUG ((DEBUG_ERROR
, "%a:%a: failed to register status code handler: %r\n",
276 gEfiCallerBaseName
, __FUNCTION__
, Status
));
281 // Status code reporting and routing/handling extend into OS runtime. Since
282 // we don't want our handler to survive the BDS phase, we have to unregister
283 // the callback at ExitBootServices(). (See EFI_RSC_HANDLER_PROTOCOL in
284 // Volume 3 of the Platform Init spec.)
286 Status
= gBS
->CreateEvent (
287 EVT_SIGNAL_EXIT_BOOT_SERVICES
, // Type
288 TPL_CALLBACK
, // NotifyTpl
289 UnregisterAtExitBootServices
, // NotifyFunction
290 StatusCodeRouter
, // NotifyContext
291 &ExitBootEvent
// Event
293 if (EFI_ERROR (Status
)) {
295 // We have to unregister the callback right now, and fail the function.
297 DEBUG ((DEBUG_ERROR
, "%a:%a: failed to create ExitBootServices() event: "
298 "%r\n", gEfiCallerBaseName
, __FUNCTION__
, Status
));
299 StatusCodeRouter
->Unregister (HandleStatusCode
);