]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/PlatformBootManagerLib/PlatformBm.c
ArmPkg/PlatformBootManagerLib: fall back to the UiApp on boot failure
[mirror_edk2.git] / ArmPkg / Library / PlatformBootManagerLib / PlatformBm.c
1 /** @file
2 Implementation for PlatformBootManagerLib library class interfaces.
3
4 Copyright (C) 2015-2016, Red Hat, Inc.
5 Copyright (c) 2014 - 2019, ARM Ltd. All rights reserved.<BR>
6 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
7 Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>
8
9 SPDX-License-Identifier: BSD-2-Clause-Patent
10
11 **/
12
13 #include <IndustryStandard/Pci22.h>
14 #include <Library/BootLogoLib.h>
15 #include <Library/CapsuleLib.h>
16 #include <Library/DevicePathLib.h>
17 #include <Library/HobLib.h>
18 #include <Library/PcdLib.h>
19 #include <Library/UefiBootManagerLib.h>
20 #include <Library/UefiLib.h>
21 #include <Library/UefiRuntimeServicesTableLib.h>
22 #include <Protocol/DevicePath.h>
23 #include <Protocol/EsrtManagement.h>
24 #include <Protocol/GraphicsOutput.h>
25 #include <Protocol/LoadedImage.h>
26 #include <Protocol/NonDiscoverableDevice.h>
27 #include <Protocol/PciIo.h>
28 #include <Protocol/PciRootBridgeIo.h>
29 #include <Protocol/PlatformBootManager.h>
30 #include <Guid/EventGroup.h>
31 #include <Guid/NonDiscoverableDevice.h>
32 #include <Guid/TtyTerm.h>
33 #include <Guid/SerialPortLibVendor.h>
34
35 #include "PlatformBm.h"
36
37 #define DP_NODE_LEN(Type) { (UINT8)sizeof (Type), (UINT8)(sizeof (Type) >> 8) }
38
39 #pragma pack (1)
40 typedef struct {
41 VENDOR_DEVICE_PATH SerialDxe;
42 UART_DEVICE_PATH Uart;
43 VENDOR_DEFINED_DEVICE_PATH TermType;
44 EFI_DEVICE_PATH_PROTOCOL End;
45 } PLATFORM_SERIAL_CONSOLE;
46 #pragma pack ()
47
48 STATIC PLATFORM_SERIAL_CONSOLE mSerialConsole = {
49 //
50 // VENDOR_DEVICE_PATH SerialDxe
51 //
52 {
53 { HARDWARE_DEVICE_PATH, HW_VENDOR_DP, DP_NODE_LEN (VENDOR_DEVICE_PATH) },
54 EDKII_SERIAL_PORT_LIB_VENDOR_GUID
55 },
56
57 //
58 // UART_DEVICE_PATH Uart
59 //
60 {
61 { MESSAGING_DEVICE_PATH, MSG_UART_DP, DP_NODE_LEN (UART_DEVICE_PATH) },
62 0, // Reserved
63 FixedPcdGet64 (PcdUartDefaultBaudRate), // BaudRate
64 FixedPcdGet8 (PcdUartDefaultDataBits), // DataBits
65 FixedPcdGet8 (PcdUartDefaultParity), // Parity
66 FixedPcdGet8 (PcdUartDefaultStopBits) // StopBits
67 },
68
69 //
70 // VENDOR_DEFINED_DEVICE_PATH TermType
71 //
72 {
73 {
74 MESSAGING_DEVICE_PATH, MSG_VENDOR_DP,
75 DP_NODE_LEN (VENDOR_DEFINED_DEVICE_PATH)
76 }
77 //
78 // Guid to be filled in dynamically
79 //
80 },
81
82 //
83 // EFI_DEVICE_PATH_PROTOCOL End
84 //
85 {
86 END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,
87 DP_NODE_LEN (EFI_DEVICE_PATH_PROTOCOL)
88 }
89 };
90
91
92 #pragma pack (1)
93 typedef struct {
94 USB_CLASS_DEVICE_PATH Keyboard;
95 EFI_DEVICE_PATH_PROTOCOL End;
96 } PLATFORM_USB_KEYBOARD;
97 #pragma pack ()
98
99 STATIC PLATFORM_USB_KEYBOARD mUsbKeyboard = {
100 //
101 // USB_CLASS_DEVICE_PATH Keyboard
102 //
103 {
104 {
105 MESSAGING_DEVICE_PATH, MSG_USB_CLASS_DP,
106 DP_NODE_LEN (USB_CLASS_DEVICE_PATH)
107 },
108 0xFFFF, // VendorId: any
109 0xFFFF, // ProductId: any
110 3, // DeviceClass: HID
111 1, // DeviceSubClass: boot
112 1 // DeviceProtocol: keyboard
113 },
114
115 //
116 // EFI_DEVICE_PATH_PROTOCOL End
117 //
118 {
119 END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,
120 DP_NODE_LEN (EFI_DEVICE_PATH_PROTOCOL)
121 }
122 };
123
124
125 /**
126 Check if the handle satisfies a particular condition.
127
128 @param[in] Handle The handle to check.
129 @param[in] ReportText A caller-allocated string passed in for reporting
130 purposes. It must never be NULL.
131
132 @retval TRUE The condition is satisfied.
133 @retval FALSE Otherwise. This includes the case when the condition could not
134 be fully evaluated due to an error.
135 **/
136 typedef
137 BOOLEAN
138 (EFIAPI *FILTER_FUNCTION) (
139 IN EFI_HANDLE Handle,
140 IN CONST CHAR16 *ReportText
141 );
142
143
144 /**
145 Process a handle.
146
147 @param[in] Handle The handle to process.
148 @param[in] ReportText A caller-allocated string passed in for reporting
149 purposes. It must never be NULL.
150 **/
151 typedef
152 VOID
153 (EFIAPI *CALLBACK_FUNCTION) (
154 IN EFI_HANDLE Handle,
155 IN CONST CHAR16 *ReportText
156 );
157
158 /**
159 Locate all handles that carry the specified protocol, filter them with a
160 callback function, and pass each handle that passes the filter to another
161 callback.
162
163 @param[in] ProtocolGuid The protocol to look for.
164
165 @param[in] Filter The filter function to pass each handle to. If this
166 parameter is NULL, then all handles are processed.
167
168 @param[in] Process The callback function to pass each handle to that
169 clears the filter.
170 **/
171 STATIC
172 VOID
173 FilterAndProcess (
174 IN EFI_GUID *ProtocolGuid,
175 IN FILTER_FUNCTION Filter OPTIONAL,
176 IN CALLBACK_FUNCTION Process
177 )
178 {
179 EFI_STATUS Status;
180 EFI_HANDLE *Handles;
181 UINTN NoHandles;
182 UINTN Idx;
183
184 Status = gBS->LocateHandleBuffer (ByProtocol, ProtocolGuid,
185 NULL /* SearchKey */, &NoHandles, &Handles);
186 if (EFI_ERROR (Status)) {
187 //
188 // This is not an error, just an informative condition.
189 //
190 DEBUG ((EFI_D_VERBOSE, "%a: %g: %r\n", __FUNCTION__, ProtocolGuid,
191 Status));
192 return;
193 }
194
195 ASSERT (NoHandles > 0);
196 for (Idx = 0; Idx < NoHandles; ++Idx) {
197 CHAR16 *DevicePathText;
198 STATIC CHAR16 Fallback[] = L"<device path unavailable>";
199
200 //
201 // The ConvertDevicePathToText() function handles NULL input transparently.
202 //
203 DevicePathText = ConvertDevicePathToText (
204 DevicePathFromHandle (Handles[Idx]),
205 FALSE, // DisplayOnly
206 FALSE // AllowShortcuts
207 );
208 if (DevicePathText == NULL) {
209 DevicePathText = Fallback;
210 }
211
212 if (Filter == NULL || Filter (Handles[Idx], DevicePathText)) {
213 Process (Handles[Idx], DevicePathText);
214 }
215
216 if (DevicePathText != Fallback) {
217 FreePool (DevicePathText);
218 }
219 }
220 gBS->FreePool (Handles);
221 }
222
223
224 /**
225 This FILTER_FUNCTION checks if a handle corresponds to a PCI display device.
226 **/
227 STATIC
228 BOOLEAN
229 EFIAPI
230 IsPciDisplay (
231 IN EFI_HANDLE Handle,
232 IN CONST CHAR16 *ReportText
233 )
234 {
235 EFI_STATUS Status;
236 EFI_PCI_IO_PROTOCOL *PciIo;
237 PCI_TYPE00 Pci;
238
239 Status = gBS->HandleProtocol (Handle, &gEfiPciIoProtocolGuid,
240 (VOID**)&PciIo);
241 if (EFI_ERROR (Status)) {
242 //
243 // This is not an error worth reporting.
244 //
245 return FALSE;
246 }
247
248 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint32, 0 /* Offset */,
249 sizeof Pci / sizeof (UINT32), &Pci);
250 if (EFI_ERROR (Status)) {
251 DEBUG ((EFI_D_ERROR, "%a: %s: %r\n", __FUNCTION__, ReportText, Status));
252 return FALSE;
253 }
254
255 return IS_PCI_DISPLAY (&Pci);
256 }
257
258
259 /**
260 This FILTER_FUNCTION checks if a handle corresponds to a non-discoverable
261 USB host controller.
262 **/
263 STATIC
264 BOOLEAN
265 EFIAPI
266 IsUsbHost (
267 IN EFI_HANDLE Handle,
268 IN CONST CHAR16 *ReportText
269 )
270 {
271 NON_DISCOVERABLE_DEVICE *Device;
272 EFI_STATUS Status;
273
274 Status = gBS->HandleProtocol (Handle,
275 &gEdkiiNonDiscoverableDeviceProtocolGuid,
276 (VOID **)&Device);
277 if (EFI_ERROR (Status)) {
278 return FALSE;
279 }
280
281 if (CompareGuid (Device->Type, &gEdkiiNonDiscoverableUhciDeviceGuid) ||
282 CompareGuid (Device->Type, &gEdkiiNonDiscoverableEhciDeviceGuid) ||
283 CompareGuid (Device->Type, &gEdkiiNonDiscoverableXhciDeviceGuid)) {
284 return TRUE;
285 }
286 return FALSE;
287 }
288
289
290 /**
291 This CALLBACK_FUNCTION attempts to connect a handle non-recursively, asking
292 the matching driver to produce all first-level child handles.
293 **/
294 STATIC
295 VOID
296 EFIAPI
297 Connect (
298 IN EFI_HANDLE Handle,
299 IN CONST CHAR16 *ReportText
300 )
301 {
302 EFI_STATUS Status;
303
304 Status = gBS->ConnectController (
305 Handle, // ControllerHandle
306 NULL, // DriverImageHandle
307 NULL, // RemainingDevicePath -- produce all children
308 FALSE // Recursive
309 );
310 DEBUG ((EFI_ERROR (Status) ? EFI_D_ERROR : EFI_D_VERBOSE, "%a: %s: %r\n",
311 __FUNCTION__, ReportText, Status));
312 }
313
314
315 /**
316 This CALLBACK_FUNCTION retrieves the EFI_DEVICE_PATH_PROTOCOL from the
317 handle, and adds it to ConOut and ErrOut.
318 **/
319 STATIC
320 VOID
321 EFIAPI
322 AddOutput (
323 IN EFI_HANDLE Handle,
324 IN CONST CHAR16 *ReportText
325 )
326 {
327 EFI_STATUS Status;
328 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
329
330 DevicePath = DevicePathFromHandle (Handle);
331 if (DevicePath == NULL) {
332 DEBUG ((EFI_D_ERROR, "%a: %s: handle %p: device path not found\n",
333 __FUNCTION__, ReportText, Handle));
334 return;
335 }
336
337 Status = EfiBootManagerUpdateConsoleVariable (ConOut, DevicePath, NULL);
338 if (EFI_ERROR (Status)) {
339 DEBUG ((EFI_D_ERROR, "%a: %s: adding to ConOut: %r\n", __FUNCTION__,
340 ReportText, Status));
341 return;
342 }
343
344 Status = EfiBootManagerUpdateConsoleVariable (ErrOut, DevicePath, NULL);
345 if (EFI_ERROR (Status)) {
346 DEBUG ((EFI_D_ERROR, "%a: %s: adding to ErrOut: %r\n", __FUNCTION__,
347 ReportText, Status));
348 return;
349 }
350
351 DEBUG ((EFI_D_VERBOSE, "%a: %s: added to ConOut and ErrOut\n", __FUNCTION__,
352 ReportText));
353 }
354
355 STATIC
356 VOID
357 PlatformRegisterFvBootOption (
358 CONST EFI_GUID *FileGuid,
359 CHAR16 *Description,
360 UINT32 Attributes,
361 EFI_INPUT_KEY *Key
362 )
363 {
364 EFI_STATUS Status;
365 INTN OptionIndex;
366 EFI_BOOT_MANAGER_LOAD_OPTION NewOption;
367 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;
368 UINTN BootOptionCount;
369 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FileNode;
370 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
371 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
372
373 Status = gBS->HandleProtocol (
374 gImageHandle,
375 &gEfiLoadedImageProtocolGuid,
376 (VOID **) &LoadedImage
377 );
378 ASSERT_EFI_ERROR (Status);
379
380 EfiInitializeFwVolDevicepathNode (&FileNode, FileGuid);
381 DevicePath = DevicePathFromHandle (LoadedImage->DeviceHandle);
382 ASSERT (DevicePath != NULL);
383 DevicePath = AppendDevicePathNode (
384 DevicePath,
385 (EFI_DEVICE_PATH_PROTOCOL *) &FileNode
386 );
387 ASSERT (DevicePath != NULL);
388
389 Status = EfiBootManagerInitializeLoadOption (
390 &NewOption,
391 LoadOptionNumberUnassigned,
392 LoadOptionTypeBoot,
393 Attributes,
394 Description,
395 DevicePath,
396 NULL,
397 0
398 );
399 ASSERT_EFI_ERROR (Status);
400 FreePool (DevicePath);
401
402 BootOptions = EfiBootManagerGetLoadOptions (
403 &BootOptionCount, LoadOptionTypeBoot
404 );
405
406 OptionIndex = EfiBootManagerFindLoadOption (
407 &NewOption, BootOptions, BootOptionCount
408 );
409
410 if (OptionIndex == -1) {
411 Status = EfiBootManagerAddLoadOptionVariable (&NewOption, MAX_UINTN);
412 ASSERT_EFI_ERROR (Status);
413 Status = EfiBootManagerAddKeyOptionVariable (NULL,
414 (UINT16)NewOption.OptionNumber, 0, Key, NULL);
415 ASSERT (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED);
416 }
417 EfiBootManagerFreeLoadOption (&NewOption);
418 EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);
419 }
420
421
422 STATIC
423 VOID
424 GetPlatformOptions (
425 VOID
426 )
427 {
428 EFI_STATUS Status;
429 EFI_BOOT_MANAGER_LOAD_OPTION *CurrentBootOptions;
430 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;
431 EFI_INPUT_KEY *BootKeys;
432 PLATFORM_BOOT_MANAGER_PROTOCOL *PlatformBootManager;
433 UINTN CurrentBootOptionCount;
434 UINTN Index;
435 UINTN BootCount;
436
437 Status = gBS->LocateProtocol (&gPlatformBootManagerProtocolGuid, NULL,
438 (VOID **)&PlatformBootManager);
439 if (EFI_ERROR (Status)) {
440 return;
441 }
442 Status = PlatformBootManager->GetPlatformBootOptionsAndKeys (
443 &BootCount,
444 &BootOptions,
445 &BootKeys
446 );
447 if (EFI_ERROR (Status)) {
448 return;
449 }
450 //
451 // Fetch the existent boot options. If there are none, CurrentBootCount
452 // will be zeroed.
453 //
454 CurrentBootOptions = EfiBootManagerGetLoadOptions (
455 &CurrentBootOptionCount,
456 LoadOptionTypeBoot
457 );
458 //
459 // Process the platform boot options.
460 //
461 for (Index = 0; Index < BootCount; Index++) {
462 INTN Match;
463 UINTN BootOptionNumber;
464
465 //
466 // If there are any preexistent boot options, and the subject platform boot
467 // option is already among them, then don't try to add it. Just get its
468 // assigned boot option number so we can associate a hotkey with it. Note
469 // that EfiBootManagerFindLoadOption() deals fine with (CurrentBootOptions
470 // == NULL) if (CurrentBootCount == 0).
471 //
472 Match = EfiBootManagerFindLoadOption (
473 &BootOptions[Index],
474 CurrentBootOptions,
475 CurrentBootOptionCount
476 );
477 if (Match >= 0) {
478 BootOptionNumber = CurrentBootOptions[Match].OptionNumber;
479 } else {
480 //
481 // Add the platform boot options as a new one, at the end of the boot
482 // order. Note that if the platform provided this boot option with an
483 // unassigned option number, then the below function call will assign a
484 // number.
485 //
486 Status = EfiBootManagerAddLoadOptionVariable (
487 &BootOptions[Index],
488 MAX_UINTN
489 );
490 if (EFI_ERROR (Status)) {
491 DEBUG ((DEBUG_ERROR, "%a: failed to register \"%s\": %r\n",
492 __FUNCTION__, BootOptions[Index].Description, Status));
493 continue;
494 }
495 BootOptionNumber = BootOptions[Index].OptionNumber;
496 }
497
498 //
499 // Register a hotkey with the boot option, if requested.
500 //
501 if (BootKeys[Index].UnicodeChar == L'\0') {
502 continue;
503 }
504
505 Status = EfiBootManagerAddKeyOptionVariable (
506 NULL,
507 BootOptionNumber,
508 0,
509 &BootKeys[Index],
510 NULL
511 );
512 if (EFI_ERROR (Status)) {
513 DEBUG ((DEBUG_ERROR, "%a: failed to register hotkey for \"%s\": %r\n",
514 __FUNCTION__, BootOptions[Index].Description, Status));
515 }
516 }
517 EfiBootManagerFreeLoadOptions (CurrentBootOptions, CurrentBootOptionCount);
518 EfiBootManagerFreeLoadOptions (BootOptions, BootCount);
519 FreePool (BootKeys);
520 }
521
522 STATIC
523 VOID
524 PlatformRegisterOptionsAndKeys (
525 VOID
526 )
527 {
528 EFI_STATUS Status;
529 EFI_INPUT_KEY Enter;
530 EFI_INPUT_KEY F2;
531 EFI_INPUT_KEY Esc;
532 EFI_BOOT_MANAGER_LOAD_OPTION BootOption;
533
534 GetPlatformOptions ();
535
536 //
537 // Register ENTER as CONTINUE key
538 //
539 Enter.ScanCode = SCAN_NULL;
540 Enter.UnicodeChar = CHAR_CARRIAGE_RETURN;
541 Status = EfiBootManagerRegisterContinueKeyOption (0, &Enter, NULL);
542 ASSERT_EFI_ERROR (Status);
543
544 //
545 // Map F2 and ESC to Boot Manager Menu
546 //
547 F2.ScanCode = SCAN_F2;
548 F2.UnicodeChar = CHAR_NULL;
549 Esc.ScanCode = SCAN_ESC;
550 Esc.UnicodeChar = CHAR_NULL;
551 Status = EfiBootManagerGetBootManagerMenu (&BootOption);
552 ASSERT_EFI_ERROR (Status);
553 Status = EfiBootManagerAddKeyOptionVariable (
554 NULL, (UINT16) BootOption.OptionNumber, 0, &F2, NULL
555 );
556 ASSERT (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED);
557 Status = EfiBootManagerAddKeyOptionVariable (
558 NULL, (UINT16) BootOption.OptionNumber, 0, &Esc, NULL
559 );
560 ASSERT (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED);
561 }
562
563
564 //
565 // BDS Platform Functions
566 //
567 /**
568 Do the platform init, can be customized by OEM/IBV
569 Possible things that can be done in PlatformBootManagerBeforeConsole:
570 > Update console variable: 1. include hot-plug devices;
571 > 2. Clear ConIn and add SOL for AMT
572 > Register new Driver#### or Boot####
573 > Register new Key####: e.g.: F12
574 > Signal ReadyToLock event
575 > Authentication action: 1. connect Auth devices;
576 > 2. Identify auto logon user.
577 **/
578 VOID
579 EFIAPI
580 PlatformBootManagerBeforeConsole (
581 VOID
582 )
583 {
584 //
585 // Signal EndOfDxe PI Event
586 //
587 EfiEventGroupSignal (&gEfiEndOfDxeEventGroupGuid);
588
589 //
590 // Dispatch deferred images after EndOfDxe event.
591 //
592 EfiBootManagerDispatchDeferredImages ();
593
594 //
595 // Locate the PCI root bridges and make the PCI bus driver connect each,
596 // non-recursively. This will produce a number of child handles with PciIo on
597 // them.
598 //
599 FilterAndProcess (&gEfiPciRootBridgeIoProtocolGuid, NULL, Connect);
600
601 //
602 // Find all display class PCI devices (using the handles from the previous
603 // step), and connect them non-recursively. This should produce a number of
604 // child handles with GOPs on them.
605 //
606 FilterAndProcess (&gEfiPciIoProtocolGuid, IsPciDisplay, Connect);
607
608 //
609 // Now add the device path of all handles with GOP on them to ConOut and
610 // ErrOut.
611 //
612 FilterAndProcess (&gEfiGraphicsOutputProtocolGuid, NULL, AddOutput);
613
614 //
615 // The core BDS code connects short-form USB device paths by explicitly
616 // looking for handles with PCI I/O installed, and checking the PCI class
617 // code whether it matches the one for a USB host controller. This means
618 // non-discoverable USB host controllers need to have the non-discoverable
619 // PCI driver attached first.
620 //
621 FilterAndProcess (&gEdkiiNonDiscoverableDeviceProtocolGuid, IsUsbHost, Connect);
622
623 //
624 // Add the hardcoded short-form USB keyboard device path to ConIn.
625 //
626 EfiBootManagerUpdateConsoleVariable (ConIn,
627 (EFI_DEVICE_PATH_PROTOCOL *)&mUsbKeyboard, NULL);
628
629 //
630 // Add the hardcoded serial console device path to ConIn, ConOut, ErrOut.
631 //
632 STATIC_ASSERT (FixedPcdGet8 (PcdDefaultTerminalType) == 4,
633 "PcdDefaultTerminalType must be TTYTERM");
634 STATIC_ASSERT (FixedPcdGet8 (PcdUartDefaultParity) != 0,
635 "PcdUartDefaultParity must be set to an actual value, not 'default'");
636 STATIC_ASSERT (FixedPcdGet8 (PcdUartDefaultStopBits) != 0,
637 "PcdUartDefaultStopBits must be set to an actual value, not 'default'");
638
639 CopyGuid (&mSerialConsole.TermType.Guid, &gEfiTtyTermGuid);
640
641 EfiBootManagerUpdateConsoleVariable (ConIn,
642 (EFI_DEVICE_PATH_PROTOCOL *)&mSerialConsole, NULL);
643 EfiBootManagerUpdateConsoleVariable (ConOut,
644 (EFI_DEVICE_PATH_PROTOCOL *)&mSerialConsole, NULL);
645 EfiBootManagerUpdateConsoleVariable (ErrOut,
646 (EFI_DEVICE_PATH_PROTOCOL *)&mSerialConsole, NULL);
647
648 //
649 // Register platform-specific boot options and keyboard shortcuts.
650 //
651 PlatformRegisterOptionsAndKeys ();
652 }
653
654 STATIC
655 VOID
656 HandleCapsules (
657 VOID
658 )
659 {
660 ESRT_MANAGEMENT_PROTOCOL *EsrtManagement;
661 EFI_PEI_HOB_POINTERS HobPointer;
662 EFI_CAPSULE_HEADER *CapsuleHeader;
663 BOOLEAN NeedReset;
664 EFI_STATUS Status;
665
666 DEBUG ((DEBUG_INFO, "%a: processing capsules ...\n", __FUNCTION__));
667
668 Status = gBS->LocateProtocol (&gEsrtManagementProtocolGuid, NULL,
669 (VOID **)&EsrtManagement);
670 if (!EFI_ERROR (Status)) {
671 EsrtManagement->SyncEsrtFmp ();
672 }
673
674 //
675 // Find all capsule images from hob
676 //
677 HobPointer.Raw = GetHobList ();
678 NeedReset = FALSE;
679 while ((HobPointer.Raw = GetNextHob (EFI_HOB_TYPE_UEFI_CAPSULE,
680 HobPointer.Raw)) != NULL) {
681 CapsuleHeader = (VOID *)(UINTN)HobPointer.Capsule->BaseAddress;
682
683 Status = ProcessCapsuleImage (CapsuleHeader);
684 if (EFI_ERROR (Status)) {
685 DEBUG ((DEBUG_ERROR, "%a: failed to process capsule %p - %r\n",
686 __FUNCTION__, CapsuleHeader, Status));
687 return;
688 }
689
690 NeedReset = TRUE;
691 HobPointer.Raw = GET_NEXT_HOB (HobPointer);
692 }
693
694 if (NeedReset) {
695 DEBUG ((DEBUG_WARN, "%a: capsule update successful, resetting ...\n",
696 __FUNCTION__));
697
698 gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);
699 CpuDeadLoop();
700 }
701 }
702
703
704 #define VERSION_STRING_PREFIX L"Tianocore/EDK2 firmware version "
705
706 /**
707 Do the platform specific action after the console is ready
708 Possible things that can be done in PlatformBootManagerAfterConsole:
709 > Console post action:
710 > Dynamically switch output mode from 100x31 to 80x25 for certain scenario
711 > Signal console ready platform customized event
712 > Run diagnostics like memory testing
713 > Connect certain devices
714 > Dispatch additional option roms
715 > Special boot: e.g.: USB boot, enter UI
716 **/
717 VOID
718 EFIAPI
719 PlatformBootManagerAfterConsole (
720 VOID
721 )
722 {
723 EFI_STATUS Status;
724 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
725 UINTN FirmwareVerLength;
726 UINTN PosX;
727 UINTN PosY;
728 EFI_INPUT_KEY Key;
729
730 FirmwareVerLength = StrLen (PcdGetPtr (PcdFirmwareVersionString));
731
732 //
733 // Show the splash screen.
734 //
735 Status = BootLogoEnableLogo ();
736 if (EFI_ERROR (Status)) {
737 if (FirmwareVerLength > 0) {
738 Print (VERSION_STRING_PREFIX L"%s\n",
739 PcdGetPtr (PcdFirmwareVersionString));
740 }
741 Print (L"Press ESCAPE for boot options ");
742 } else if (FirmwareVerLength > 0) {
743 Status = gBS->HandleProtocol (gST->ConsoleOutHandle,
744 &gEfiGraphicsOutputProtocolGuid, (VOID **)&GraphicsOutput);
745 if (!EFI_ERROR (Status)) {
746 PosX = (GraphicsOutput->Mode->Info->HorizontalResolution -
747 (StrLen (VERSION_STRING_PREFIX) + FirmwareVerLength) *
748 EFI_GLYPH_WIDTH) / 2;
749 PosY = 0;
750
751 PrintXY (PosX, PosY, NULL, NULL, VERSION_STRING_PREFIX L"%s",
752 PcdGetPtr (PcdFirmwareVersionString));
753 }
754 }
755
756 //
757 // Connect the rest of the devices.
758 //
759 EfiBootManagerConnectAll ();
760
761 //
762 // On ARM, there is currently no reason to use the phased capsule
763 // update approach where some capsules are dispatched before EndOfDxe
764 // and some are dispatched after. So just handle all capsules here,
765 // when the console is up and we can actually give the user some
766 // feedback about what is going on.
767 //
768 HandleCapsules ();
769
770 //
771 // Enumerate all possible boot options.
772 //
773 EfiBootManagerRefreshAllBootOption ();
774
775 //
776 // Register UEFI Shell
777 //
778 Key.ScanCode = SCAN_NULL;
779 Key.UnicodeChar = L's';
780 PlatformRegisterFvBootOption (
781 &gUefiShellFileGuid, L"UEFI Shell", LOAD_OPTION_ACTIVE, &Key
782 );
783 }
784
785 /**
786 This function is called each second during the boot manager waits the
787 timeout.
788
789 @param TimeoutRemain The remaining timeout.
790 **/
791 VOID
792 EFIAPI
793 PlatformBootManagerWaitCallback (
794 UINT16 TimeoutRemain
795 )
796 {
797 EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION Black;
798 EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION White;
799 UINT16 Timeout;
800 EFI_STATUS Status;
801
802 Timeout = PcdGet16 (PcdPlatformBootTimeOut);
803
804 Black.Raw = 0x00000000;
805 White.Raw = 0x00FFFFFF;
806
807 Status = BootLogoUpdateProgress (
808 White.Pixel,
809 Black.Pixel,
810 L"Press ESCAPE for boot options",
811 White.Pixel,
812 (Timeout - TimeoutRemain) * 100 / Timeout,
813 0
814 );
815 if (EFI_ERROR (Status)) {
816 Print (L".");
817 }
818 }
819
820 /**
821 The function is called when no boot option could be launched,
822 including platform recovery options and options pointing to applications
823 built into firmware volumes.
824
825 If this function returns, BDS attempts to enter an infinite loop.
826 **/
827 VOID
828 EFIAPI
829 PlatformBootManagerUnableToBoot (
830 VOID
831 )
832 {
833 EFI_STATUS Status;
834 EFI_BOOT_MANAGER_LOAD_OPTION BootManagerMenu;
835
836 Status = EfiBootManagerGetBootManagerMenu (&BootManagerMenu);
837 if (EFI_ERROR (Status)) {
838 return;
839 }
840
841 for (;;) {
842 EfiBootManagerBoot (&BootManagerMenu);
843 }
844 }