OVMF: Fix a hang after resetting when NV variables are preserved.
[edk2.git] / OvmfPkg / Library / PlatformBdsLib / BdsPlatform.c
blobe44c8253581f894b447f5e2505bdd01b29e8999f
1 /** @file
2 Platform BDS customizations.
4 Copyright (c) 2004 - 2008, Intel Corporation. <BR>
5 All rights reserved. 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
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.
13 **/
15 #include "BdsPlatform.h"
19 // Global data
22 VOID *mEfiDevPathNotifyReg;
23 EFI_EVENT mEfiDevPathEvent;
27 // Function prototypes
30 VOID
31 InstallDevicePathCallback (
32 VOID
35 STATIC
36 VOID
37 LoadVideoRom (
38 VOID
41 STATIC
42 EFI_STATUS
43 PciRomLoadEfiDriversFromRomImage (
44 IN EFI_PHYSICAL_ADDRESS Rom,
45 IN UINTN RomSize
49 // BDS Platform Functions
51 VOID
52 EFIAPI
53 PlatformBdsInit (
54 VOID
56 /*++
58 Routine Description:
60 Platform Bds init. Incude the platform firmware vendor, revision
61 and so crc check.
63 Arguments:
65 Returns:
67 None.
69 --*/
71 DEBUG ((EFI_D_INFO, "PlatformBdsInit\n"));
72 InstallDevicePathCallback ();
73 LoadVideoRom ();
77 EFI_STATUS
78 ConnectRootBridge (
79 VOID
81 /*++
83 Routine Description:
85 Connect RootBridge
87 Arguments:
89 None.
91 Returns:
93 EFI_SUCCESS - Connect RootBridge successfully.
94 EFI_STATUS - Connect RootBridge fail.
96 --*/
98 EFI_STATUS Status;
99 EFI_HANDLE RootHandle;
102 // Make all the PCI_IO protocols on PCI Seg 0 show up
104 BdsLibConnectDevicePath (gPlatformRootBridges[0]);
106 Status = gBS->LocateDevicePath (
107 &gEfiDevicePathProtocolGuid,
108 &gPlatformRootBridges[0],
109 &RootHandle
111 if (EFI_ERROR (Status)) {
112 return Status;
115 Status = gBS->ConnectController (RootHandle, NULL, NULL, FALSE);
116 if (EFI_ERROR (Status)) {
117 return Status;
120 return EFI_SUCCESS;
124 EFI_STATUS
125 PrepareLpcBridgeDevicePath (
126 IN EFI_HANDLE DeviceHandle
128 /*++
130 Routine Description:
132 Add IsaKeyboard to ConIn,
133 add IsaSerial to ConOut, ConIn, ErrOut.
134 LPC Bridge: 06 01 00
136 Arguments:
138 DeviceHandle - Handle of PCIIO protocol.
140 Returns:
142 EFI_SUCCESS - LPC bridge is added to ConOut, ConIn, and ErrOut.
143 EFI_STATUS - No LPC bridge is added.
145 --*/
147 EFI_STATUS Status;
148 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
149 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
150 CHAR16 *DevPathStr;
152 DevicePath = NULL;
153 Status = gBS->HandleProtocol (
154 DeviceHandle,
155 &gEfiDevicePathProtocolGuid,
156 (VOID*)&DevicePath
158 if (EFI_ERROR (Status)) {
159 return Status;
161 TempDevicePath = DevicePath;
164 // Register Keyboard
166 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gPnpPs2KeyboardDeviceNode);
168 BdsLibUpdateConsoleVariable (VarConsoleInp, DevicePath, NULL);
171 // Register COM1
173 DevicePath = TempDevicePath;
174 gPnp16550ComPortDeviceNode.UID = 0;
176 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gPnp16550ComPortDeviceNode);
177 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode);
178 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode);
181 // Print Device Path
183 DevPathStr = DevicePathToStr(DevicePath);
184 DEBUG((
185 EFI_D_INFO,
186 "BdsPlatform.c+%d: COM%d DevPath: %s\n",
187 __LINE__,
188 gPnp16550ComPortDeviceNode.UID + 1,
189 DevPathStr
191 FreePool(DevPathStr);
193 BdsLibUpdateConsoleVariable (VarConsoleOut, DevicePath, NULL);
194 BdsLibUpdateConsoleVariable (VarConsoleInp, DevicePath, NULL);
195 BdsLibUpdateConsoleVariable (VarErrorOut, DevicePath, NULL);
198 // Register COM2
200 DevicePath = TempDevicePath;
201 gPnp16550ComPortDeviceNode.UID = 1;
203 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gPnp16550ComPortDeviceNode);
204 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode);
205 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode);
208 // Print Device Path
210 DevPathStr = DevicePathToStr(DevicePath);
211 DEBUG((
212 EFI_D_INFO,
213 "BdsPlatform.c+%d: COM%d DevPath: %s\n",
214 __LINE__,
215 gPnp16550ComPortDeviceNode.UID + 1,
216 DevPathStr
218 FreePool(DevPathStr);
220 BdsLibUpdateConsoleVariable (VarConsoleOut, DevicePath, NULL);
221 BdsLibUpdateConsoleVariable (VarConsoleInp, DevicePath, NULL);
222 BdsLibUpdateConsoleVariable (VarErrorOut, DevicePath, NULL);
224 return EFI_SUCCESS;
227 EFI_STATUS
228 GetGopDevicePath (
229 IN EFI_DEVICE_PATH_PROTOCOL *PciDevicePath,
230 OUT EFI_DEVICE_PATH_PROTOCOL **GopDevicePath
233 UINTN Index;
234 EFI_STATUS Status;
235 EFI_HANDLE PciDeviceHandle;
236 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
237 EFI_DEVICE_PATH_PROTOCOL *TempPciDevicePath;
238 UINTN GopHandleCount;
239 EFI_HANDLE *GopHandleBuffer;
241 if (PciDevicePath == NULL || GopDevicePath == NULL) {
242 return EFI_INVALID_PARAMETER;
246 // Initialize the GopDevicePath to be PciDevicePath
248 *GopDevicePath = PciDevicePath;
249 TempPciDevicePath = PciDevicePath;
251 Status = gBS->LocateDevicePath (
252 &gEfiDevicePathProtocolGuid,
253 &TempPciDevicePath,
254 &PciDeviceHandle
256 if (EFI_ERROR (Status)) {
257 return Status;
261 // Try to connect this handle, so that GOP dirver could start on this
262 // device and create child handles with GraphicsOutput Protocol installed
263 // on them, then we get device paths of these child handles and select
264 // them as possible console device.
266 gBS->ConnectController (PciDeviceHandle, NULL, NULL, FALSE);
268 Status = gBS->LocateHandleBuffer (
269 ByProtocol,
270 &gEfiGraphicsOutputProtocolGuid,
271 NULL,
272 &GopHandleCount,
273 &GopHandleBuffer
275 if (!EFI_ERROR (Status)) {
277 // Add all the child handles as possible Console Device
279 for (Index = 0; Index < GopHandleCount; Index++) {
280 Status = gBS->HandleProtocol (GopHandleBuffer[Index], &gEfiDevicePathProtocolGuid, (VOID*)&TempDevicePath);
281 if (EFI_ERROR (Status)) {
282 continue;
284 if (CompareMem (
285 PciDevicePath,
286 TempDevicePath,
287 GetDevicePathSize (PciDevicePath) - END_DEVICE_PATH_LENGTH
288 ) == 0) {
290 // In current implementation, we only enable one of the child handles
291 // as console device, i.e. sotre one of the child handle's device
292 // path to variable "ConOut"
293 // In futhure, we could select all child handles to be console device
296 *GopDevicePath = TempDevicePath;
299 // Delete the PCI device's path that added by GetPlugInPciVgaDevicePath()
300 // Add the integrity GOP device path.
302 BdsLibUpdateConsoleVariable (VarConsoleOutDev, NULL, PciDevicePath);
303 BdsLibUpdateConsoleVariable (VarConsoleOutDev, TempDevicePath, NULL);
306 gBS->FreePool (GopHandleBuffer);
309 return EFI_SUCCESS;
312 EFI_STATUS
313 PreparePciVgaDevicePath (
314 IN EFI_HANDLE DeviceHandle
316 /*++
318 Routine Description:
320 Add PCI VGA to ConOut.
321 PCI VGA: 03 00 00
323 Arguments:
325 DeviceHandle - Handle of PCIIO protocol.
327 Returns:
329 EFI_SUCCESS - PCI VGA is added to ConOut.
330 EFI_STATUS - No PCI VGA device is added.
332 --*/
334 EFI_STATUS Status;
335 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
336 EFI_DEVICE_PATH_PROTOCOL *GopDevicePath;
338 DevicePath = NULL;
339 Status = gBS->HandleProtocol (
340 DeviceHandle,
341 &gEfiDevicePathProtocolGuid,
342 (VOID*)&DevicePath
344 if (EFI_ERROR (Status)) {
345 return Status;
348 GetGopDevicePath (DevicePath, &GopDevicePath);
349 DevicePath = GopDevicePath;
351 BdsLibUpdateConsoleVariable (VarConsoleOut, DevicePath, NULL);
353 return EFI_SUCCESS;
356 EFI_STATUS
357 PreparePciSerialDevicePath (
358 IN EFI_HANDLE DeviceHandle
360 /*++
362 Routine Description:
364 Add PCI Serial to ConOut, ConIn, ErrOut.
365 PCI Serial: 07 00 02
367 Arguments:
369 DeviceHandle - Handle of PCIIO protocol.
371 Returns:
373 EFI_SUCCESS - PCI Serial is added to ConOut, ConIn, and ErrOut.
374 EFI_STATUS - No PCI Serial device is added.
376 --*/
378 EFI_STATUS Status;
379 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
381 DevicePath = NULL;
382 Status = gBS->HandleProtocol (
383 DeviceHandle,
384 &gEfiDevicePathProtocolGuid,
385 (VOID*)&DevicePath
387 if (EFI_ERROR (Status)) {
388 return Status;
391 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode);
392 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode);
394 BdsLibUpdateConsoleVariable (VarConsoleOut, DevicePath, NULL);
395 BdsLibUpdateConsoleVariable (VarConsoleInp, DevicePath, NULL);
396 BdsLibUpdateConsoleVariable (VarErrorOut, DevicePath, NULL);
398 return EFI_SUCCESS;
401 EFI_STATUS
402 DetectAndPreparePlatformPciDevicePath (
403 BOOLEAN DetectVgaOnly
405 /*++
407 Routine Description:
409 Do platform specific PCI Device check and add them to ConOut, ConIn, ErrOut
411 Arguments:
413 DetectVgaOnly - Only detect VGA device if it's TRUE.
415 Returns:
417 EFI_SUCCESS - PCI Device check and Console variable update successfully.
418 EFI_STATUS - PCI Device check or Console variable update fail.
420 --*/
422 EFI_STATUS Status;
423 UINTN HandleCount;
424 EFI_HANDLE *HandleBuffer;
425 UINTN Index;
426 EFI_PCI_IO_PROTOCOL *PciIo;
427 PCI_TYPE00 Pci;
430 // Start to check all the PciIo to find all possible device
432 HandleCount = 0;
433 HandleBuffer = NULL;
434 Status = gBS->LocateHandleBuffer (
435 ByProtocol,
436 &gEfiPciIoProtocolGuid,
437 NULL,
438 &HandleCount,
439 &HandleBuffer
441 if (EFI_ERROR (Status)) {
442 return Status;
445 for (Index = 0; Index < HandleCount; Index++) {
446 Status = gBS->HandleProtocol (HandleBuffer[Index], &gEfiPciIoProtocolGuid, (VOID*)&PciIo);
447 if (EFI_ERROR (Status)) {
448 continue;
452 // Check for all PCI device
454 Status = PciIo->Pci.Read (
455 PciIo,
456 EfiPciIoWidthUint32,
458 sizeof (Pci) / sizeof (UINT32),
459 &Pci
461 if (EFI_ERROR (Status)) {
462 continue;
465 Status = PciIo->Attributes (
466 PciIo,
467 EfiPciIoAttributeOperationEnable,
468 EFI_PCI_DEVICE_ENABLE,
469 NULL
471 ASSERT_EFI_ERROR (Status);
473 if (!DetectVgaOnly) {
475 // Here we decide whether it is LPC Bridge
477 if ((IS_PCI_LPC (&Pci)) ||
478 ((IS_PCI_ISA_PDECODE (&Pci)) &&
479 (Pci.Hdr.VendorId == 0x8086) &&
480 (Pci.Hdr.DeviceId == 0x7000)
484 // Add IsaKeyboard to ConIn,
485 // add IsaSerial to ConOut, ConIn, ErrOut
487 DEBUG ((EFI_D_INFO, "Found LPC Bridge device\n"));
488 PrepareLpcBridgeDevicePath (HandleBuffer[Index]);
489 continue;
492 // Here we decide which Serial device to enable in PCI bus
494 if (IS_PCI_16550SERIAL (&Pci)) {
496 // Add them to ConOut, ConIn, ErrOut.
498 DEBUG ((EFI_D_INFO, "Found PCI 16550 SERIAL device\n"));
499 PreparePciSerialDevicePath (HandleBuffer[Index]);
500 continue;
505 // Here we decide which VGA device to enable in PCI bus
507 if (IS_PCI_VGA (&Pci)) {
509 // Add them to ConOut.
511 DEBUG ((EFI_D_INFO, "Found PCI VGA device\n"));
512 PreparePciVgaDevicePath (HandleBuffer[Index]);
513 continue;
517 gBS->FreePool (HandleBuffer);
519 return EFI_SUCCESS;
523 EFI_STATUS
524 PlatformBdsConnectConsole (
525 IN BDS_CONSOLE_CONNECT_ENTRY *PlatformConsole
527 /*++
529 Routine Description:
531 Connect the predefined platform default console device. Always try to find
532 and enable the vga device if have.
534 Arguments:
536 PlatformConsole - Predfined platform default console device array.
538 Returns:
540 EFI_SUCCESS - Success connect at least one ConIn and ConOut
541 device, there must have one ConOut device is
542 active vga device.
544 EFI_STATUS - Return the status of
545 BdsLibConnectAllDefaultConsoles ()
547 --*/
549 EFI_STATUS Status;
550 UINTN Index;
551 EFI_DEVICE_PATH_PROTOCOL *VarConout;
552 EFI_DEVICE_PATH_PROTOCOL *VarConin;
553 UINTN DevicePathSize;
556 // Connect RootBridge
558 ConnectRootBridge ();
560 VarConout = BdsLibGetVariableAndSize (
561 VarConsoleOut,
562 &gEfiGlobalVariableGuid,
563 &DevicePathSize
565 VarConin = BdsLibGetVariableAndSize (
566 VarConsoleInp,
567 &gEfiGlobalVariableGuid,
568 &DevicePathSize
571 if (VarConout == NULL || VarConin == NULL) {
573 // Do platform specific PCI Device check and add them to ConOut, ConIn, ErrOut
575 DetectAndPreparePlatformPciDevicePath (FALSE);
578 // Have chance to connect the platform default console,
579 // the platform default console is the minimue device group
580 // the platform should support
582 for (Index = 0; PlatformConsole[Index].DevicePath != NULL; ++Index) {
584 // Update the console variable with the connect type
586 if ((PlatformConsole[Index].ConnectType & CONSOLE_IN) == CONSOLE_IN) {
587 BdsLibUpdateConsoleVariable (VarConsoleInp, PlatformConsole[Index].DevicePath, NULL);
589 if ((PlatformConsole[Index].ConnectType & CONSOLE_OUT) == CONSOLE_OUT) {
590 BdsLibUpdateConsoleVariable (VarConsoleOut, PlatformConsole[Index].DevicePath, NULL);
592 if ((PlatformConsole[Index].ConnectType & STD_ERROR) == STD_ERROR) {
593 BdsLibUpdateConsoleVariable (VarErrorOut, PlatformConsole[Index].DevicePath, NULL);
596 } else {
598 // Only detect VGA device and add them to ConOut
600 DetectAndPreparePlatformPciDevicePath (TRUE);
604 // Connect the all the default console with current cosole variable
606 Status = BdsLibConnectAllDefaultConsoles ();
607 if (EFI_ERROR (Status)) {
608 return Status;
611 return EFI_SUCCESS;
615 VOID
616 PciInitialization (
620 // Bus 0, Device 0, Function 0 - Host to PCI Bridge
622 PciWrite8 (PCI_LIB_ADDRESS (0, 0, 0, 0x3c), 0x00);
625 // Bus 0, Device 1, Function 0 - PCI to ISA Bridge
627 PciWrite8 (PCI_LIB_ADDRESS (0, 1, 0, 0x3c), 0x00);
628 PciWrite8 (PCI_LIB_ADDRESS (0, 1, 0, 0x60), 0x0b);
629 PciWrite8 (PCI_LIB_ADDRESS (0, 1, 0, 0x61), 0x09);
630 PciWrite8 (PCI_LIB_ADDRESS (0, 1, 0, 0x62), 0x0b);
631 PciWrite8 (PCI_LIB_ADDRESS (0, 1, 0, 0x63), 0x09);
634 // Bus 0, Device 1, Function 1 - IDE Controller
636 PciWrite8 (PCI_LIB_ADDRESS (0, 1, 1, 0x3c), 0x00);
637 PciWrite8 (PCI_LIB_ADDRESS (0, 1, 1, 0x0d), 0x40);
640 // Bus 0, Device 1, Function 3 - Power Managment Controller
642 PciWrite8 (PCI_LIB_ADDRESS (0, 1, 3, 0x3c), 0x0b);
643 PciWrite8 (PCI_LIB_ADDRESS (0, 1, 3, 0x3d), 0x01);
646 // Bus 0, Device 2, Function 0 - Video Controller
648 PciWrite8 (PCI_LIB_ADDRESS (0, 2, 0, 0x3c), 0x00);
651 // Bus 0, Device 3, Function 0 - Network Controller
653 PciWrite8 (PCI_LIB_ADDRESS (0, 3, 0, 0x3c), 0x0b);
654 PciWrite8 (PCI_LIB_ADDRESS (0, 3, 0, 0x3d), 0x01);
657 // Bus 0, Device 4, Function 0 - RAM Memory
659 PciWrite8 (PCI_LIB_ADDRESS (0, 4, 0, 0x3c), 0x09);
660 PciWrite8 (PCI_LIB_ADDRESS (0, 4, 0, 0x3d), 0x01);
664 VOID
665 PlatformBdsConnectSequence (
666 VOID
668 /*++
670 Routine Description:
672 Connect with predeined platform connect sequence,
673 the OEM/IBV can customize with their own connect sequence.
675 Arguments:
677 None.
679 Returns:
681 None.
683 --*/
685 UINTN Index;
687 DEBUG ((EFI_D_INFO, "PlatformBdsConnectSequence\n"));
689 Index = 0;
692 // Here we can get the customized platform connect sequence
693 // Notes: we can connect with new variable which record the
694 // last time boots connect device path sequence
696 while (gPlatformConnectSequence[Index] != NULL) {
698 // Build the platform boot option
700 BdsLibConnectDevicePath (gPlatformConnectSequence[Index]);
701 Index++;
705 // Just use the simple policy to connect all devices
707 BdsLibConnectAll ();
709 PciInitialization ();
712 // Clear the logo after all devices are connected.
714 gST->ConOut->ClearScreen (gST->ConOut);
717 VOID
718 PlatformBdsGetDriverOption (
719 IN OUT LIST_ENTRY *BdsDriverLists
721 /*++
723 Routine Description:
725 Load the predefined driver option, OEM/IBV can customize this
726 to load their own drivers
728 Arguments:
730 BdsDriverLists - The header of the driver option link list.
732 Returns:
734 None.
736 --*/
738 DEBUG ((EFI_D_INFO, "PlatformBdsGetDriverOption\n"));
739 return;
742 VOID
743 PlatformBdsDiagnostics (
744 IN EXTENDMEM_COVERAGE_LEVEL MemoryTestLevel,
745 IN BOOLEAN QuietBoot,
746 IN BASEM_MEMORY_TEST BaseMemoryTest
748 /*++
750 Routine Description:
752 Perform the platform diagnostic, such like test memory. OEM/IBV also
753 can customize this fuction to support specific platform diagnostic.
755 Arguments:
757 MemoryTestLevel - The memory test intensive level
759 QuietBoot - Indicate if need to enable the quiet boot
761 BaseMemoryTest - A pointer to BaseMemoryTest()
763 Returns:
765 None.
767 --*/
769 EFI_STATUS Status;
771 DEBUG ((EFI_D_INFO, "PlatformBdsDiagnostics\n"));
774 // Here we can decide if we need to show
775 // the diagnostics screen
776 // Notes: this quiet boot code should be remove
777 // from the graphic lib
779 if (QuietBoot) {
780 EnableQuietBoot (PcdGetPtr(PcdLogoFile));
782 // Perform system diagnostic
784 Status = BaseMemoryTest (MemoryTestLevel);
785 if (EFI_ERROR (Status)) {
786 DisableQuietBoot ();
789 return ;
792 // Perform system diagnostic
794 Status = BaseMemoryTest (MemoryTestLevel);
798 VOID
799 EFIAPI
800 PlatformBdsPolicyBehavior (
801 IN OUT LIST_ENTRY *DriverOptionList,
802 IN OUT LIST_ENTRY *BootOptionList,
803 IN PROCESS_CAPSULES ProcessCapsules,
804 IN BASEM_MEMORY_TEST BaseMemoryTest
806 /*++
808 Routine Description:
810 The function will excute with as the platform policy, current policy
811 is driven by boot mode. IBV/OEM can customize this code for their specific
812 policy action.
814 Arguments:
816 DriverOptionList - The header of the driver option link list
818 BootOptionList - The header of the boot option link list
820 ProcessCapsules - A pointer to ProcessCapsules()
822 BaseMemoryTest - A pointer to BaseMemoryTest()
824 Returns:
826 None.
828 --*/
830 EFI_STATUS Status;
831 UINT16 Timeout;
832 EFI_EVENT UserInputDurationTime;
833 LIST_ENTRY *Link;
834 BDS_COMMON_OPTION *BootOption;
835 UINTN Index;
836 EFI_INPUT_KEY Key;
837 EFI_TPL OldTpl;
838 EFI_BOOT_MODE BootMode;
840 DEBUG ((EFI_D_INFO, "PlatformBdsPolicyBehavior\n"));
843 // Init the time out value
845 Timeout = PcdGet16 (PcdPlatformBootTimeOut);
848 // Load the driver option as the driver option list
850 PlatformBdsGetDriverOption (DriverOptionList);
853 // Get current Boot Mode
855 Status = BdsLibGetBootMode (&BootMode);
856 DEBUG ((EFI_D_ERROR, "Boot Mode:%x\n", BootMode));
859 // Go the different platform policy with different boot mode
860 // Notes: this part code can be change with the table policy
862 ASSERT (BootMode == BOOT_WITH_FULL_CONFIGURATION);
864 // Connect platform console
866 Status = PlatformBdsConnectConsole (gPlatformConsole);
867 if (EFI_ERROR (Status)) {
869 // Here OEM/IBV can customize with defined action
871 PlatformBdsNoConsoleAction ();
874 // Create a 300ms duration event to ensure user has enough input time to enter Setup
876 Status = gBS->CreateEvent (
877 EVT_TIMER,
879 NULL,
880 NULL,
881 &UserInputDurationTime
883 ASSERT (Status == EFI_SUCCESS);
884 Status = gBS->SetTimer (UserInputDurationTime, TimerRelative, 3000000);
885 ASSERT (Status == EFI_SUCCESS);
887 // Memory test and Logo show
889 PlatformBdsDiagnostics (IGNORE, TRUE, BaseMemoryTest);
892 // Perform some platform specific connect sequence
894 PlatformBdsConnectSequence ();
897 // Give one chance to enter the setup if we
898 // have the time out
900 if (Timeout != 0) {
901 //PlatformBdsEnterFrontPage (Timeout, FALSE);
904 DEBUG ((EFI_D_INFO, "BdsLibConnectAll\n"));
905 BdsLibConnectAll ();
906 BdsLibEnumerateAllBootOption (BootOptionList);
909 // Please uncomment above ConnectAll and EnumerateAll code and remove following first boot
910 // checking code in real production tip.
912 // In BOOT_WITH_FULL_CONFIGURATION boot mode, should always connect every device
913 // and do enumerate all the default boot options. But in development system board, the boot mode
914 // cannot be BOOT_ASSUMING_NO_CONFIGURATION_CHANGES because the machine box
915 // is always open. So the following code only do the ConnectAll and EnumerateAll at first boot.
917 Status = BdsLibBuildOptionFromVar (BootOptionList, L"BootOrder");
918 if (EFI_ERROR(Status)) {
920 // If cannot find "BootOrder" variable, it may be first boot.
921 // Try to connect all devices and enumerate all boot options here.
923 BdsLibConnectAll ();
924 BdsLibEnumerateAllBootOption (BootOptionList);
928 // To give the User a chance to enter Setup here, if user set TimeOut is 0.
929 // BDS should still give user a chance to enter Setup
931 // Connect first boot option, and then check user input before exit
933 for (Link = BootOptionList->ForwardLink; Link != BootOptionList;Link = Link->ForwardLink) {
934 BootOption = CR (Link, BDS_COMMON_OPTION, Link, BDS_LOAD_OPTION_SIGNATURE);
935 if (!IS_LOAD_OPTION_TYPE (BootOption->Attribute, LOAD_OPTION_ACTIVE)) {
937 // skip the header of the link list, becuase it has no boot option
939 continue;
940 } else {
942 // Make sure the boot option device path connected, but ignore the BBS device path
944 if (DevicePathType (BootOption->DevicePath) != BBS_DEVICE_PATH) {
945 BdsLibConnectDevicePath (BootOption->DevicePath);
947 break;
952 // Check whether the user input after the duration time has expired
954 OldTpl = EfiGetCurrentTpl();
955 gBS->RestoreTPL (TPL_APPLICATION);
956 gBS->WaitForEvent (1, &UserInputDurationTime, &Index);
957 gBS->CloseEvent (UserInputDurationTime);
958 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
959 gBS->RaiseTPL (OldTpl);
961 if (!EFI_ERROR (Status)) {
963 // Enter Setup if user input
965 Timeout = 0xffff;
966 PlatformBdsEnterFrontPage (Timeout, FALSE);
969 return ;
972 VOID
973 EFIAPI
974 PlatformBdsBootSuccess (
975 IN BDS_COMMON_OPTION *Option
977 /*++
979 Routine Description:
981 Hook point after a boot attempt succeeds. We don't expect a boot option to
982 return, so the EFI 1.0 specification defines that you will default to an
983 interactive mode and stop processing the BootOrder list in this case. This
984 is alos a platform implementation and can be customized by IBV/OEM.
986 Arguments:
988 Option - Pointer to Boot Option that succeeded to boot.
990 Returns:
992 None.
994 --*/
996 CHAR16 *TmpStr;
998 DEBUG ((EFI_D_INFO, "PlatformBdsBootSuccess\n"));
1000 // If Boot returned with EFI_SUCCESS and there is not in the boot device
1001 // select loop then we need to pop up a UI and wait for user input.
1003 TmpStr = Option->StatusString;
1004 if (TmpStr != NULL) {
1005 BdsLibOutputStrings (gST->ConOut, TmpStr, Option->Description, L"\n\r", NULL);
1006 FreePool (TmpStr);
1010 VOID
1011 EFIAPI
1012 PlatformBdsBootFail (
1013 IN BDS_COMMON_OPTION *Option,
1014 IN EFI_STATUS Status,
1015 IN CHAR16 *ExitData,
1016 IN UINTN ExitDataSize
1018 /*++
1020 Routine Description:
1022 Hook point after a boot attempt fails.
1024 Arguments:
1026 Option - Pointer to Boot Option that failed to boot.
1028 Status - Status returned from failed boot.
1030 ExitData - Exit data returned from failed boot.
1032 ExitDataSize - Exit data size returned from failed boot.
1034 Returns:
1036 None.
1038 --*/
1040 CHAR16 *TmpStr;
1042 DEBUG ((EFI_D_INFO, "PlatformBdsBootFail\n"));
1045 // If Boot returned with failed status then we need to pop up a UI and wait
1046 // for user input.
1048 TmpStr = Option->StatusString;
1049 if (TmpStr != NULL) {
1050 BdsLibOutputStrings (gST->ConOut, TmpStr, Option->Description, L"\n\r", NULL);
1051 FreePool (TmpStr);
1055 EFI_STATUS
1056 PlatformBdsNoConsoleAction (
1057 VOID
1059 /*++
1061 Routine Description:
1063 This function is remained for IBV/OEM to do some platform action,
1064 if there no console device can be connected.
1066 Arguments:
1068 None.
1070 Returns:
1072 EFI_SUCCESS - Direct return success now.
1074 --*/
1076 DEBUG ((EFI_D_INFO, "PlatformBdsNoConsoleAction\n"));
1077 return EFI_SUCCESS;
1080 VOID
1081 EFIAPI
1082 PlatformBdsLockNonUpdatableFlash (
1083 VOID
1086 DEBUG ((EFI_D_INFO, "PlatformBdsLockNonUpdatableFlash\n"));
1087 return;
1092 This notification function is invoked when an instance of the
1093 EFI_DEVICE_PATH_PROTOCOL is produced.
1095 @param Event The event that occured
1096 @param Context For EFI compatiblity. Not used.
1099 VOID
1100 EFIAPI
1101 NotifyDevPath (
1102 IN EFI_EVENT Event,
1103 IN VOID *Context
1106 EFI_HANDLE Handle;
1107 EFI_STATUS Status;
1108 UINTN BufferSize;
1109 EFI_DEVICE_PATH_PROTOCOL *DevPathNode;
1110 ATAPI_DEVICE_PATH *Atapi;
1113 // Examine all new handles
1115 for (;;) {
1117 // Get the next handle
1119 BufferSize = sizeof (Handle);
1120 Status = gBS->LocateHandle (
1121 ByRegisterNotify,
1122 NULL,
1123 mEfiDevPathNotifyReg,
1124 &BufferSize,
1125 &Handle
1129 // If not found, we're done
1131 if (EFI_NOT_FOUND == Status) {
1132 break;
1135 if (EFI_ERROR (Status)) {
1136 continue;
1140 // Get the DevicePath protocol on that handle
1142 Status = gBS->HandleProtocol (Handle, &gEfiDevicePathProtocolGuid, (VOID **)&DevPathNode);
1143 ASSERT_EFI_ERROR (Status);
1145 while (!IsDevicePathEnd (DevPathNode)) {
1147 // Find the handler to dump this device path node
1149 if (
1150 (DevicePathType(DevPathNode) == MESSAGING_DEVICE_PATH) &&
1151 (DevicePathSubType(DevPathNode) == MSG_ATAPI_DP)
1153 Atapi = (ATAPI_DEVICE_PATH*) DevPathNode;
1154 PciOr16 (
1155 PCI_LIB_ADDRESS (
1159 (Atapi->PrimarySecondary == 1) ? 0x42: 0x40
1161 BIT15
1166 // Next device path node
1168 DevPathNode = NextDevicePathNode (DevPathNode);
1172 return;
1176 VOID
1177 InstallDevicePathCallback (
1178 VOID
1181 DEBUG ((EFI_D_INFO, "Registered NotifyDevPath Event\n"));
1182 mEfiDevPathEvent = EfiCreateProtocolNotifyEvent (
1183 &gEfiDevicePathProtocolGuid,
1184 TPL_CALLBACK,
1185 NotifyDevPath,
1186 NULL,
1187 &mEfiDevPathNotifyReg
1192 Lock the ConsoleIn device in system table. All key
1193 presses will be ignored until the Password is typed in. The only way to
1194 disable the password is to type it in to a ConIn device.
1196 @param Password Password used to lock ConIn device.
1198 @retval EFI_SUCCESS lock the Console In Spliter virtual handle successfully.
1199 @retval EFI_UNSUPPORTED Password not found
1202 EFI_STATUS
1203 EFIAPI
1204 LockKeyboards (
1205 IN CHAR16 *Password
1208 return EFI_UNSUPPORTED;
1212 STATIC
1213 VOID
1214 LoadVideoRom (
1215 VOID
1218 PCI_DATA_STRUCTURE *Pcir;
1219 UINTN RomSize;
1222 // The virtual machines sometimes load the video rom image
1223 // directly at the legacy video BIOS location of C000:0000,
1224 // and do not implement the PCI expansion ROM feature.
1226 Pcir = (PCI_DATA_STRUCTURE *) (UINTN) 0xc0000;
1227 RomSize = Pcir->ImageLength * 512;
1228 PciRomLoadEfiDriversFromRomImage (0xc0000, RomSize);
1232 STATIC
1233 EFI_STATUS
1234 PciRomLoadEfiDriversFromRomImage (
1235 IN EFI_PHYSICAL_ADDRESS Rom,
1236 IN UINTN RomSize
1239 CHAR16 *FileName;
1240 EFI_PCI_EXPANSION_ROM_HEADER *EfiRomHeader;
1241 PCI_DATA_STRUCTURE *Pcir;
1242 UINTN ImageIndex;
1243 UINTN RomOffset;
1244 UINT32 ImageSize;
1245 UINT16 ImageOffset;
1246 EFI_HANDLE ImageHandle;
1247 EFI_STATUS Status;
1248 EFI_STATUS retStatus;
1249 EFI_DEVICE_PATH_PROTOCOL *FilePath;
1250 BOOLEAN SkipImage;
1251 UINT32 DestinationSize;
1252 UINT32 ScratchSize;
1253 UINT8 *Scratch;
1254 VOID *ImageBuffer;
1255 VOID *DecompressedImageBuffer;
1256 UINT32 ImageLength;
1257 EFI_DECOMPRESS_PROTOCOL *Decompress;
1259 FileName = L"PciRomInMemory";
1261 //FileName = L"PciRom Addr=0000000000000000";
1262 //HexToString (&FileName[12], Rom, 16);
1264 ImageIndex = 0;
1265 retStatus = EFI_NOT_FOUND;
1266 RomOffset = (UINTN) Rom;
1268 do {
1270 EfiRomHeader = (EFI_PCI_EXPANSION_ROM_HEADER *) (UINTN) RomOffset;
1272 if (EfiRomHeader->Signature != 0xaa55) {
1273 return retStatus;
1276 Pcir = (PCI_DATA_STRUCTURE *) (UINTN) (RomOffset + EfiRomHeader->PcirOffset);
1277 ImageSize = Pcir->ImageLength * 512;
1279 if ((Pcir->CodeType == PCI_CODE_TYPE_EFI_IMAGE) &&
1280 (EfiRomHeader->EfiSignature == EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE) ) {
1282 if ((EfiRomHeader->EfiSubsystem == EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER) ||
1283 (EfiRomHeader->EfiSubsystem == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) ) {
1285 ImageOffset = EfiRomHeader->EfiImageHeaderOffset;
1286 ImageSize = EfiRomHeader->InitializationSize * 512;
1288 ImageBuffer = (VOID *) (UINTN) (RomOffset + ImageOffset);
1289 ImageLength = ImageSize - ImageOffset;
1290 DecompressedImageBuffer = NULL;
1293 // decompress here if needed
1295 SkipImage = FALSE;
1296 if (EfiRomHeader->CompressionType > EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED) {
1297 SkipImage = TRUE;
1300 if (EfiRomHeader->CompressionType == EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED) {
1301 Status = gBS->LocateProtocol (&gEfiDecompressProtocolGuid, NULL, (VOID **) &Decompress);
1302 if (EFI_ERROR (Status)) {
1303 SkipImage = TRUE;
1304 } else {
1305 SkipImage = TRUE;
1306 Status = Decompress->GetInfo (
1307 Decompress,
1308 ImageBuffer,
1309 ImageLength,
1310 &DestinationSize,
1311 &ScratchSize
1313 if (!EFI_ERROR (Status)) {
1314 DecompressedImageBuffer = NULL;
1315 DecompressedImageBuffer = AllocatePool (DestinationSize);
1316 if (DecompressedImageBuffer != NULL) {
1317 Scratch = AllocatePool (ScratchSize);
1318 if (Scratch != NULL) {
1319 Status = Decompress->Decompress (
1320 Decompress,
1321 ImageBuffer,
1322 ImageLength,
1323 DecompressedImageBuffer,
1324 DestinationSize,
1325 Scratch,
1326 ScratchSize
1328 if (!EFI_ERROR (Status)) {
1329 ImageBuffer = DecompressedImageBuffer;
1330 ImageLength = DestinationSize;
1331 SkipImage = FALSE;
1334 gBS->FreePool (Scratch);
1341 if (!SkipImage) {
1344 // load image and start image
1347 FilePath = FileDevicePath (NULL, FileName);
1349 Status = gBS->LoadImage (
1350 FALSE,
1351 gImageHandle,
1352 FilePath,
1353 ImageBuffer,
1354 ImageLength,
1355 &ImageHandle
1357 if (!EFI_ERROR (Status)) {
1358 Status = gBS->StartImage (ImageHandle, NULL, NULL);
1359 if (!EFI_ERROR (Status)) {
1360 retStatus = Status;
1363 if (FilePath != NULL) {
1364 gBS->FreePool (FilePath);
1368 if (DecompressedImageBuffer != NULL) {
1369 gBS->FreePool (DecompressedImageBuffer);
1375 RomOffset = RomOffset + ImageSize;
1376 ImageIndex++;
1377 } while (((Pcir->Indicator & 0x80) == 0x00) && ((RomOffset - (UINTN) Rom) < RomSize));
1379 return retStatus;