]> git.proxmox.com Git - mirror_edk2.git/blob - ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBm.c
MdeModulePkg: Introduce EDKII_SERIAL_PORT_LIB_VENDOR_GUID
[mirror_edk2.git] / ArmVirtPkg / 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, ARM Ltd. All rights reserved.<BR>
6 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
7
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9
10 **/
11
12 #include <IndustryStandard/Pci22.h>
13 #include <IndustryStandard/Virtio095.h>
14 #include <Library/BootLogoLib.h>
15 #include <Library/DevicePathLib.h>
16 #include <Library/PcdLib.h>
17 #include <Library/PlatformBmPrintScLib.h>
18 #include <Library/QemuBootOrderLib.h>
19 #include <Library/UefiBootManagerLib.h>
20 #include <Protocol/DevicePath.h>
21 #include <Protocol/FirmwareVolume2.h>
22 #include <Protocol/GraphicsOutput.h>
23 #include <Protocol/LoadedImage.h>
24 #include <Protocol/PciIo.h>
25 #include <Protocol/PciRootBridgeIo.h>
26 #include <Protocol/VirtioDevice.h>
27 #include <Guid/EventGroup.h>
28 #include <Guid/RootBridgesConnectedEventGroup.h>
29
30 #include "PlatformBm.h"
31
32 #define DP_NODE_LEN(Type) { (UINT8)sizeof (Type), (UINT8)(sizeof (Type) >> 8) }
33
34
35 #pragma pack (1)
36 typedef struct {
37 VENDOR_DEVICE_PATH SerialDxe;
38 UART_DEVICE_PATH Uart;
39 VENDOR_DEFINED_DEVICE_PATH TermType;
40 EFI_DEVICE_PATH_PROTOCOL End;
41 } PLATFORM_SERIAL_CONSOLE;
42 #pragma pack ()
43
44 #define SERIAL_DXE_FILE_GUID { \
45 0xD3987D4B, 0x971A, 0x435F, \
46 { 0x8C, 0xAF, 0x49, 0x67, 0xEB, 0x62, 0x72, 0x41 } \
47 }
48
49 STATIC PLATFORM_SERIAL_CONSOLE mSerialConsole = {
50 //
51 // VENDOR_DEVICE_PATH SerialDxe
52 //
53 {
54 { HARDWARE_DEVICE_PATH, HW_VENDOR_DP, DP_NODE_LEN (VENDOR_DEVICE_PATH) },
55 SERIAL_DXE_FILE_GUID
56 },
57
58 //
59 // UART_DEVICE_PATH Uart
60 //
61 {
62 { MESSAGING_DEVICE_PATH, MSG_UART_DP, DP_NODE_LEN (UART_DEVICE_PATH) },
63 0, // Reserved
64 FixedPcdGet64 (PcdUartDefaultBaudRate), // BaudRate
65 FixedPcdGet8 (PcdUartDefaultDataBits), // DataBits
66 FixedPcdGet8 (PcdUartDefaultParity), // Parity
67 FixedPcdGet8 (PcdUartDefaultStopBits) // StopBits
68 },
69
70 //
71 // VENDOR_DEFINED_DEVICE_PATH TermType
72 //
73 {
74 {
75 MESSAGING_DEVICE_PATH, MSG_VENDOR_DP,
76 DP_NODE_LEN (VENDOR_DEFINED_DEVICE_PATH)
77 }
78 //
79 // Guid to be filled in dynamically
80 //
81 },
82
83 //
84 // EFI_DEVICE_PATH_PROTOCOL End
85 //
86 {
87 END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,
88 DP_NODE_LEN (EFI_DEVICE_PATH_PROTOCOL)
89 }
90 };
91
92
93 #pragma pack (1)
94 typedef struct {
95 USB_CLASS_DEVICE_PATH Keyboard;
96 EFI_DEVICE_PATH_PROTOCOL End;
97 } PLATFORM_USB_KEYBOARD;
98 #pragma pack ()
99
100 STATIC PLATFORM_USB_KEYBOARD mUsbKeyboard = {
101 //
102 // USB_CLASS_DEVICE_PATH Keyboard
103 //
104 {
105 {
106 MESSAGING_DEVICE_PATH, MSG_USB_CLASS_DP,
107 DP_NODE_LEN (USB_CLASS_DEVICE_PATH)
108 },
109 0xFFFF, // VendorId: any
110 0xFFFF, // ProductId: any
111 3, // DeviceClass: HID
112 1, // DeviceSubClass: boot
113 1 // DeviceProtocol: keyboard
114 },
115
116 //
117 // EFI_DEVICE_PATH_PROTOCOL End
118 //
119 {
120 END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,
121 DP_NODE_LEN (EFI_DEVICE_PATH_PROTOCOL)
122 }
123 };
124
125
126 /**
127 Check if the handle satisfies a particular condition.
128
129 @param[in] Handle The handle to check.
130 @param[in] ReportText A caller-allocated string passed in for reporting
131 purposes. It must never be NULL.
132
133 @retval TRUE The condition is satisfied.
134 @retval FALSE Otherwise. This includes the case when the condition could not
135 be fully evaluated due to an error.
136 **/
137 typedef
138 BOOLEAN
139 (EFIAPI *FILTER_FUNCTION) (
140 IN EFI_HANDLE Handle,
141 IN CONST CHAR16 *ReportText
142 );
143
144
145 /**
146 Process a handle.
147
148 @param[in] Handle The handle to process.
149 @param[in] ReportText A caller-allocated string passed in for reporting
150 purposes. It must never be NULL.
151 **/
152 typedef
153 VOID
154 (EFIAPI *CALLBACK_FUNCTION) (
155 IN EFI_HANDLE Handle,
156 IN CONST CHAR16 *ReportText
157 );
158
159 /**
160 Locate all handles that carry the specified protocol, filter them with a
161 callback function, and pass each handle that passes the filter to another
162 callback.
163
164 @param[in] ProtocolGuid The protocol to look for.
165
166 @param[in] Filter The filter function to pass each handle to. If this
167 parameter is NULL, then all handles are processed.
168
169 @param[in] Process The callback function to pass each handle to that
170 clears the filter.
171 **/
172 STATIC
173 VOID
174 FilterAndProcess (
175 IN EFI_GUID *ProtocolGuid,
176 IN FILTER_FUNCTION Filter OPTIONAL,
177 IN CALLBACK_FUNCTION Process
178 )
179 {
180 EFI_STATUS Status;
181 EFI_HANDLE *Handles;
182 UINTN NoHandles;
183 UINTN Idx;
184
185 Status = gBS->LocateHandleBuffer (ByProtocol, ProtocolGuid,
186 NULL /* SearchKey */, &NoHandles, &Handles);
187 if (EFI_ERROR (Status)) {
188 //
189 // This is not an error, just an informative condition.
190 //
191 DEBUG ((EFI_D_VERBOSE, "%a: %g: %r\n", __FUNCTION__, ProtocolGuid,
192 Status));
193 return;
194 }
195
196 ASSERT (NoHandles > 0);
197 for (Idx = 0; Idx < NoHandles; ++Idx) {
198 CHAR16 *DevicePathText;
199 STATIC CHAR16 Fallback[] = L"<device path unavailable>";
200
201 //
202 // The ConvertDevicePathToText() function handles NULL input transparently.
203 //
204 DevicePathText = ConvertDevicePathToText (
205 DevicePathFromHandle (Handles[Idx]),
206 FALSE, // DisplayOnly
207 FALSE // AllowShortcuts
208 );
209 if (DevicePathText == NULL) {
210 DevicePathText = Fallback;
211 }
212
213 if (Filter == NULL || Filter (Handles[Idx], DevicePathText)) {
214 Process (Handles[Idx], DevicePathText);
215 }
216
217 if (DevicePathText != Fallback) {
218 FreePool (DevicePathText);
219 }
220 }
221 gBS->FreePool (Handles);
222 }
223
224
225 /**
226 This FILTER_FUNCTION checks if a handle corresponds to a PCI display device.
227 **/
228 STATIC
229 BOOLEAN
230 EFIAPI
231 IsPciDisplay (
232 IN EFI_HANDLE Handle,
233 IN CONST CHAR16 *ReportText
234 )
235 {
236 EFI_STATUS Status;
237 EFI_PCI_IO_PROTOCOL *PciIo;
238 PCI_TYPE00 Pci;
239
240 Status = gBS->HandleProtocol (Handle, &gEfiPciIoProtocolGuid,
241 (VOID**)&PciIo);
242 if (EFI_ERROR (Status)) {
243 //
244 // This is not an error worth reporting.
245 //
246 return FALSE;
247 }
248
249 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint32, 0 /* Offset */,
250 sizeof Pci / sizeof (UINT32), &Pci);
251 if (EFI_ERROR (Status)) {
252 DEBUG ((EFI_D_ERROR, "%a: %s: %r\n", __FUNCTION__, ReportText, Status));
253 return FALSE;
254 }
255
256 return IS_PCI_DISPLAY (&Pci);
257 }
258
259
260 /**
261 This FILTER_FUNCTION checks if a handle corresponds to a Virtio RNG device at
262 the VIRTIO_DEVICE_PROTOCOL level.
263 **/
264 STATIC
265 BOOLEAN
266 EFIAPI
267 IsVirtioRng (
268 IN EFI_HANDLE Handle,
269 IN CONST CHAR16 *ReportText
270 )
271 {
272 EFI_STATUS Status;
273 VIRTIO_DEVICE_PROTOCOL *VirtIo;
274
275 Status = gBS->HandleProtocol (Handle, &gVirtioDeviceProtocolGuid,
276 (VOID**)&VirtIo);
277 if (EFI_ERROR (Status)) {
278 return FALSE;
279 }
280 return (BOOLEAN)(VirtIo->SubSystemDeviceId ==
281 VIRTIO_SUBSYSTEM_ENTROPY_SOURCE);
282 }
283
284
285 /**
286 This FILTER_FUNCTION checks if a handle corresponds to a Virtio RNG device at
287 the EFI_PCI_IO_PROTOCOL level.
288 **/
289 STATIC
290 BOOLEAN
291 EFIAPI
292 IsVirtioPciRng (
293 IN EFI_HANDLE Handle,
294 IN CONST CHAR16 *ReportText
295 )
296 {
297 EFI_STATUS Status;
298 EFI_PCI_IO_PROTOCOL *PciIo;
299 UINT16 VendorId;
300 UINT16 DeviceId;
301 UINT8 RevisionId;
302 BOOLEAN Virtio10;
303 UINT16 SubsystemId;
304
305 Status = gBS->HandleProtocol (Handle, &gEfiPciIoProtocolGuid,
306 (VOID**)&PciIo);
307 if (EFI_ERROR (Status)) {
308 return FALSE;
309 }
310
311 //
312 // Read and check VendorId.
313 //
314 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16, PCI_VENDOR_ID_OFFSET,
315 1, &VendorId);
316 if (EFI_ERROR (Status)) {
317 goto PciError;
318 }
319 if (VendorId != VIRTIO_VENDOR_ID) {
320 return FALSE;
321 }
322
323 //
324 // Read DeviceId and RevisionId.
325 //
326 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16, PCI_DEVICE_ID_OFFSET,
327 1, &DeviceId);
328 if (EFI_ERROR (Status)) {
329 goto PciError;
330 }
331 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint8, PCI_REVISION_ID_OFFSET,
332 1, &RevisionId);
333 if (EFI_ERROR (Status)) {
334 goto PciError;
335 }
336
337 //
338 // From DeviceId and RevisionId, determine whether the device is a
339 // modern-only Virtio 1.0 device. In case of Virtio 1.0, DeviceId can
340 // immediately be restricted to VIRTIO_SUBSYSTEM_ENTROPY_SOURCE, and
341 // SubsystemId will only play a sanity-check role. Otherwise, DeviceId can
342 // only be sanity-checked, and SubsystemId will decide.
343 //
344 if (DeviceId == 0x1040 + VIRTIO_SUBSYSTEM_ENTROPY_SOURCE &&
345 RevisionId >= 0x01) {
346 Virtio10 = TRUE;
347 } else if (DeviceId >= 0x1000 && DeviceId <= 0x103F && RevisionId == 0x00) {
348 Virtio10 = FALSE;
349 } else {
350 return FALSE;
351 }
352
353 //
354 // Read and check SubsystemId as dictated by Virtio10.
355 //
356 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16,
357 PCI_SUBSYSTEM_ID_OFFSET, 1, &SubsystemId);
358 if (EFI_ERROR (Status)) {
359 goto PciError;
360 }
361 if (Virtio10 && SubsystemId >= 0x40) {
362 return TRUE;
363 }
364 if (!Virtio10 && SubsystemId == VIRTIO_SUBSYSTEM_ENTROPY_SOURCE) {
365 return TRUE;
366 }
367 return FALSE;
368
369 PciError:
370 DEBUG ((DEBUG_ERROR, "%a: %s: %r\n", __FUNCTION__, ReportText, Status));
371 return FALSE;
372 }
373
374
375 /**
376 This CALLBACK_FUNCTION attempts to connect a handle non-recursively, asking
377 the matching driver to produce all first-level child handles.
378 **/
379 STATIC
380 VOID
381 EFIAPI
382 Connect (
383 IN EFI_HANDLE Handle,
384 IN CONST CHAR16 *ReportText
385 )
386 {
387 EFI_STATUS Status;
388
389 Status = gBS->ConnectController (
390 Handle, // ControllerHandle
391 NULL, // DriverImageHandle
392 NULL, // RemainingDevicePath -- produce all children
393 FALSE // Recursive
394 );
395 DEBUG ((EFI_ERROR (Status) ? EFI_D_ERROR : EFI_D_VERBOSE, "%a: %s: %r\n",
396 __FUNCTION__, ReportText, Status));
397 }
398
399
400 /**
401 This CALLBACK_FUNCTION retrieves the EFI_DEVICE_PATH_PROTOCOL from the
402 handle, and adds it to ConOut and ErrOut.
403 **/
404 STATIC
405 VOID
406 EFIAPI
407 AddOutput (
408 IN EFI_HANDLE Handle,
409 IN CONST CHAR16 *ReportText
410 )
411 {
412 EFI_STATUS Status;
413 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
414
415 DevicePath = DevicePathFromHandle (Handle);
416 if (DevicePath == NULL) {
417 DEBUG ((EFI_D_ERROR, "%a: %s: handle %p: device path not found\n",
418 __FUNCTION__, ReportText, Handle));
419 return;
420 }
421
422 Status = EfiBootManagerUpdateConsoleVariable (ConOut, DevicePath, NULL);
423 if (EFI_ERROR (Status)) {
424 DEBUG ((EFI_D_ERROR, "%a: %s: adding to ConOut: %r\n", __FUNCTION__,
425 ReportText, Status));
426 return;
427 }
428
429 Status = EfiBootManagerUpdateConsoleVariable (ErrOut, DevicePath, NULL);
430 if (EFI_ERROR (Status)) {
431 DEBUG ((EFI_D_ERROR, "%a: %s: adding to ErrOut: %r\n", __FUNCTION__,
432 ReportText, Status));
433 return;
434 }
435
436 DEBUG ((EFI_D_VERBOSE, "%a: %s: added to ConOut and ErrOut\n", __FUNCTION__,
437 ReportText));
438 }
439
440 STATIC
441 VOID
442 PlatformRegisterFvBootOption (
443 EFI_GUID *FileGuid,
444 CHAR16 *Description,
445 UINT32 Attributes
446 )
447 {
448 EFI_STATUS Status;
449 INTN OptionIndex;
450 EFI_BOOT_MANAGER_LOAD_OPTION NewOption;
451 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;
452 UINTN BootOptionCount;
453 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FileNode;
454 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
455 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
456
457 Status = gBS->HandleProtocol (
458 gImageHandle,
459 &gEfiLoadedImageProtocolGuid,
460 (VOID **) &LoadedImage
461 );
462 ASSERT_EFI_ERROR (Status);
463
464 EfiInitializeFwVolDevicepathNode (&FileNode, FileGuid);
465 DevicePath = DevicePathFromHandle (LoadedImage->DeviceHandle);
466 ASSERT (DevicePath != NULL);
467 DevicePath = AppendDevicePathNode (
468 DevicePath,
469 (EFI_DEVICE_PATH_PROTOCOL *) &FileNode
470 );
471 ASSERT (DevicePath != NULL);
472
473 Status = EfiBootManagerInitializeLoadOption (
474 &NewOption,
475 LoadOptionNumberUnassigned,
476 LoadOptionTypeBoot,
477 Attributes,
478 Description,
479 DevicePath,
480 NULL,
481 0
482 );
483 ASSERT_EFI_ERROR (Status);
484 FreePool (DevicePath);
485
486 BootOptions = EfiBootManagerGetLoadOptions (
487 &BootOptionCount, LoadOptionTypeBoot
488 );
489
490 OptionIndex = EfiBootManagerFindLoadOption (
491 &NewOption, BootOptions, BootOptionCount
492 );
493
494 if (OptionIndex == -1) {
495 Status = EfiBootManagerAddLoadOptionVariable (&NewOption, MAX_UINTN);
496 ASSERT_EFI_ERROR (Status);
497 }
498 EfiBootManagerFreeLoadOption (&NewOption);
499 EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);
500 }
501
502
503 /**
504 Remove all MemoryMapped(...)/FvFile(...) and Fv(...)/FvFile(...) boot options
505 whose device paths do not resolve exactly to an FvFile in the system.
506
507 This removes any boot options that point to binaries built into the firmware
508 and have become stale due to any of the following:
509 - FvMain's base address or size changed (historical),
510 - FvMain's FvNameGuid changed,
511 - the FILE_GUID of the pointed-to binary changed,
512 - the referenced binary is no longer built into the firmware.
513
514 EfiBootManagerFindLoadOption() used in PlatformRegisterFvBootOption() only
515 avoids exact duplicates.
516 **/
517 STATIC
518 VOID
519 RemoveStaleFvFileOptions (
520 VOID
521 )
522 {
523 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;
524 UINTN BootOptionCount;
525 UINTN Index;
526
527 BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount,
528 LoadOptionTypeBoot);
529
530 for (Index = 0; Index < BootOptionCount; ++Index) {
531 EFI_DEVICE_PATH_PROTOCOL *Node1, *Node2, *SearchNode;
532 EFI_STATUS Status;
533 EFI_HANDLE FvHandle;
534
535 //
536 // If the device path starts with neither MemoryMapped(...) nor Fv(...),
537 // then keep the boot option.
538 //
539 Node1 = BootOptions[Index].FilePath;
540 if (!(DevicePathType (Node1) == HARDWARE_DEVICE_PATH &&
541 DevicePathSubType (Node1) == HW_MEMMAP_DP) &&
542 !(DevicePathType (Node1) == MEDIA_DEVICE_PATH &&
543 DevicePathSubType (Node1) == MEDIA_PIWG_FW_VOL_DP)) {
544 continue;
545 }
546
547 //
548 // If the second device path node is not FvFile(...), then keep the boot
549 // option.
550 //
551 Node2 = NextDevicePathNode (Node1);
552 if (DevicePathType (Node2) != MEDIA_DEVICE_PATH ||
553 DevicePathSubType (Node2) != MEDIA_PIWG_FW_FILE_DP) {
554 continue;
555 }
556
557 //
558 // Locate the Firmware Volume2 protocol instance that is denoted by the
559 // boot option. If this lookup fails (i.e., the boot option references a
560 // firmware volume that doesn't exist), then we'll proceed to delete the
561 // boot option.
562 //
563 SearchNode = Node1;
564 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid,
565 &SearchNode, &FvHandle);
566
567 if (!EFI_ERROR (Status)) {
568 //
569 // The firmware volume was found; now let's see if it contains the FvFile
570 // identified by GUID.
571 //
572 EFI_FIRMWARE_VOLUME2_PROTOCOL *FvProtocol;
573 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FvFileNode;
574 UINTN BufferSize;
575 EFI_FV_FILETYPE FoundType;
576 EFI_FV_FILE_ATTRIBUTES FileAttributes;
577 UINT32 AuthenticationStatus;
578
579 Status = gBS->HandleProtocol (FvHandle, &gEfiFirmwareVolume2ProtocolGuid,
580 (VOID **)&FvProtocol);
581 ASSERT_EFI_ERROR (Status);
582
583 FvFileNode = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)Node2;
584 //
585 // Buffer==NULL means we request metadata only: BufferSize, FoundType,
586 // FileAttributes.
587 //
588 Status = FvProtocol->ReadFile (
589 FvProtocol,
590 &FvFileNode->FvFileName, // NameGuid
591 NULL, // Buffer
592 &BufferSize,
593 &FoundType,
594 &FileAttributes,
595 &AuthenticationStatus
596 );
597 if (!EFI_ERROR (Status)) {
598 //
599 // The FvFile was found. Keep the boot option.
600 //
601 continue;
602 }
603 }
604
605 //
606 // Delete the boot option.
607 //
608 Status = EfiBootManagerDeleteLoadOptionVariable (
609 BootOptions[Index].OptionNumber, LoadOptionTypeBoot);
610 DEBUG_CODE (
611 CHAR16 *DevicePathString;
612
613 DevicePathString = ConvertDevicePathToText(BootOptions[Index].FilePath,
614 FALSE, FALSE);
615 DEBUG ((
616 EFI_ERROR (Status) ? EFI_D_WARN : EFI_D_VERBOSE,
617 "%a: removing stale Boot#%04x %s: %r\n",
618 __FUNCTION__,
619 (UINT32)BootOptions[Index].OptionNumber,
620 DevicePathString == NULL ? L"<unavailable>" : DevicePathString,
621 Status
622 ));
623 if (DevicePathString != NULL) {
624 FreePool (DevicePathString);
625 }
626 );
627 }
628
629 EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);
630 }
631
632
633 STATIC
634 VOID
635 PlatformRegisterOptionsAndKeys (
636 VOID
637 )
638 {
639 EFI_STATUS Status;
640 EFI_INPUT_KEY Enter;
641 EFI_INPUT_KEY F2;
642 EFI_INPUT_KEY Esc;
643 EFI_BOOT_MANAGER_LOAD_OPTION BootOption;
644
645 //
646 // Register ENTER as CONTINUE key
647 //
648 Enter.ScanCode = SCAN_NULL;
649 Enter.UnicodeChar = CHAR_CARRIAGE_RETURN;
650 Status = EfiBootManagerRegisterContinueKeyOption (0, &Enter, NULL);
651 ASSERT_EFI_ERROR (Status);
652
653 //
654 // Map F2 and ESC to Boot Manager Menu
655 //
656 F2.ScanCode = SCAN_F2;
657 F2.UnicodeChar = CHAR_NULL;
658 Esc.ScanCode = SCAN_ESC;
659 Esc.UnicodeChar = CHAR_NULL;
660 Status = EfiBootManagerGetBootManagerMenu (&BootOption);
661 ASSERT_EFI_ERROR (Status);
662 Status = EfiBootManagerAddKeyOptionVariable (
663 NULL, (UINT16) BootOption.OptionNumber, 0, &F2, NULL
664 );
665 ASSERT (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED);
666 Status = EfiBootManagerAddKeyOptionVariable (
667 NULL, (UINT16) BootOption.OptionNumber, 0, &Esc, NULL
668 );
669 ASSERT (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED);
670 }
671
672
673 //
674 // BDS Platform Functions
675 //
676 /**
677 Do the platform init, can be customized by OEM/IBV
678 Possible things that can be done in PlatformBootManagerBeforeConsole:
679 > Update console variable: 1. include hot-plug devices;
680 > 2. Clear ConIn and add SOL for AMT
681 > Register new Driver#### or Boot####
682 > Register new Key####: e.g.: F12
683 > Signal ReadyToLock event
684 > Authentication action: 1. connect Auth devices;
685 > 2. Identify auto logon user.
686 **/
687 VOID
688 EFIAPI
689 PlatformBootManagerBeforeConsole (
690 VOID
691 )
692 {
693 RETURN_STATUS PcdStatus;
694
695 //
696 // Signal EndOfDxe PI Event
697 //
698 EfiEventGroupSignal (&gEfiEndOfDxeEventGroupGuid);
699
700 //
701 // Dispatch deferred images after EndOfDxe event.
702 //
703 EfiBootManagerDispatchDeferredImages ();
704
705 //
706 // Locate the PCI root bridges and make the PCI bus driver connect each,
707 // non-recursively. This will produce a number of child handles with PciIo on
708 // them.
709 //
710 FilterAndProcess (&gEfiPciRootBridgeIoProtocolGuid, NULL, Connect);
711
712 //
713 // Signal the ACPI platform driver that it can download QEMU ACPI tables.
714 //
715 EfiEventGroupSignal (&gRootBridgesConnectedEventGroupGuid);
716
717 //
718 // Find all display class PCI devices (using the handles from the previous
719 // step), and connect them non-recursively. This should produce a number of
720 // child handles with GOPs on them.
721 //
722 FilterAndProcess (&gEfiPciIoProtocolGuid, IsPciDisplay, Connect);
723
724 //
725 // Now add the device path of all handles with GOP on them to ConOut and
726 // ErrOut.
727 //
728 FilterAndProcess (&gEfiGraphicsOutputProtocolGuid, NULL, AddOutput);
729
730 //
731 // Add the hardcoded short-form USB keyboard device path to ConIn.
732 //
733 EfiBootManagerUpdateConsoleVariable (ConIn,
734 (EFI_DEVICE_PATH_PROTOCOL *)&mUsbKeyboard, NULL);
735
736 //
737 // Add the hardcoded serial console device path to ConIn, ConOut, ErrOut.
738 //
739 CopyGuid (&mSerialConsole.TermType.Guid,
740 PcdGetPtr (PcdTerminalTypeGuidBuffer));
741 EfiBootManagerUpdateConsoleVariable (ConIn,
742 (EFI_DEVICE_PATH_PROTOCOL *)&mSerialConsole, NULL);
743 EfiBootManagerUpdateConsoleVariable (ConOut,
744 (EFI_DEVICE_PATH_PROTOCOL *)&mSerialConsole, NULL);
745 EfiBootManagerUpdateConsoleVariable (ErrOut,
746 (EFI_DEVICE_PATH_PROTOCOL *)&mSerialConsole, NULL);
747
748 //
749 // Set the front page timeout from the QEMU configuration.
750 //
751 PcdStatus = PcdSet16S (PcdPlatformBootTimeOut,
752 GetFrontPageTimeoutFromQemu ());
753 ASSERT_RETURN_ERROR (PcdStatus);
754
755 //
756 // Register platform-specific boot options and keyboard shortcuts.
757 //
758 PlatformRegisterOptionsAndKeys ();
759
760 //
761 // At this point, VIRTIO_DEVICE_PROTOCOL instances exist only for Virtio MMIO
762 // transports. Install EFI_RNG_PROTOCOL instances on Virtio MMIO RNG devices.
763 //
764 FilterAndProcess (&gVirtioDeviceProtocolGuid, IsVirtioRng, Connect);
765
766 //
767 // Install both VIRTIO_DEVICE_PROTOCOL and (dependent) EFI_RNG_PROTOCOL
768 // instances on Virtio PCI RNG devices.
769 //
770 FilterAndProcess (&gEfiPciIoProtocolGuid, IsVirtioPciRng, Connect);
771 }
772
773 /**
774 Do the platform specific action after the console is ready
775 Possible things that can be done in PlatformBootManagerAfterConsole:
776 > Console post action:
777 > Dynamically switch output mode from 100x31 to 80x25 for certain scenario
778 > Signal console ready platform customized event
779 > Run diagnostics like memory testing
780 > Connect certain devices
781 > Dispatch additional option roms
782 > Special boot: e.g.: USB boot, enter UI
783 **/
784 VOID
785 EFIAPI
786 PlatformBootManagerAfterConsole (
787 VOID
788 )
789 {
790 RETURN_STATUS Status;
791
792 //
793 // Show the splash screen.
794 //
795 BootLogoEnableLogo ();
796
797 //
798 // Process QEMU's -kernel command line option. The kernel booted this way
799 // will receive ACPI tables: in PlatformBootManagerBeforeConsole(), we
800 // connected any and all PCI root bridges, and then signaled the ACPI
801 // platform driver.
802 //
803 TryRunningQemuKernel ();
804
805 //
806 // Connect the purported boot devices.
807 //
808 Status = ConnectDevicesFromQemu ();
809 if (RETURN_ERROR (Status)) {
810 //
811 // Connect the rest of the devices.
812 //
813 EfiBootManagerConnectAll ();
814 }
815
816 //
817 // Enumerate all possible boot options, then filter and reorder them based on
818 // the QEMU configuration.
819 //
820 EfiBootManagerRefreshAllBootOption ();
821
822 //
823 // Register UEFI Shell
824 //
825 PlatformRegisterFvBootOption (
826 &gUefiShellFileGuid, L"EFI Internal Shell", LOAD_OPTION_ACTIVE
827 );
828
829 RemoveStaleFvFileOptions ();
830 SetBootOrderFromQemu ();
831
832 PlatformBmPrintScRegisterHandler ();
833 }
834
835 /**
836 This function is called each second during the boot manager waits the
837 timeout.
838
839 @param TimeoutRemain The remaining timeout.
840 **/
841 VOID
842 EFIAPI
843 PlatformBootManagerWaitCallback (
844 UINT16 TimeoutRemain
845 )
846 {
847 EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION Black;
848 EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION White;
849 UINT16 Timeout;
850
851 Timeout = PcdGet16 (PcdPlatformBootTimeOut);
852
853 Black.Raw = 0x00000000;
854 White.Raw = 0x00FFFFFF;
855
856 BootLogoUpdateProgress (
857 White.Pixel,
858 Black.Pixel,
859 L"Start boot option",
860 White.Pixel,
861 (Timeout - TimeoutRemain) * 100 / Timeout,
862 0
863 );
864 }
865
866 /**
867 The function is called when no boot option could be launched,
868 including platform recovery options and options pointing to applications
869 built into firmware volumes.
870
871 If this function returns, BDS attempts to enter an infinite loop.
872 **/
873 VOID
874 EFIAPI
875 PlatformBootManagerUnableToBoot (
876 VOID
877 )
878 {
879 EFI_STATUS Status;
880 EFI_INPUT_KEY Key;
881 EFI_BOOT_MANAGER_LOAD_OPTION BootManagerMenu;
882 UINTN Index;
883
884 //
885 // BootManagerMenu doesn't contain the correct information when return status
886 // is EFI_NOT_FOUND.
887 //
888 Status = EfiBootManagerGetBootManagerMenu (&BootManagerMenu);
889 if (EFI_ERROR (Status)) {
890 return;
891 }
892 //
893 // Normally BdsDxe does not print anything to the system console, but this is
894 // a last resort -- the end-user will likely not see any DEBUG messages
895 // logged in this situation.
896 //
897 // AsciiPrint() will NULL-check gST->ConOut internally. We check gST->ConIn
898 // here to see if it makes sense to request and wait for a keypress.
899 //
900 if (gST->ConIn != NULL) {
901 AsciiPrint (
902 "%a: No bootable option or device was found.\n"
903 "%a: Press any key to enter the Boot Manager Menu.\n",
904 gEfiCallerBaseName,
905 gEfiCallerBaseName
906 );
907 Status = gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &Index);
908 ASSERT_EFI_ERROR (Status);
909 ASSERT (Index == 0);
910
911 //
912 // Drain any queued keys.
913 //
914 while (!EFI_ERROR (gST->ConIn->ReadKeyStroke (gST->ConIn, &Key))) {
915 //
916 // just throw away Key
917 //
918 }
919 }
920
921 for (;;) {
922 EfiBootManagerBoot (&BootManagerMenu);
923 }
924 }