3 Copyright (c) 2006 - 2008, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 This file include all platform action which can be customized
23 #include "BdsPlatform.h"
25 #define IS_PCI_ISA_PDECODE(_p) IS_CLASS3 (_p, PCI_CLASS_BRIDGE, PCI_CLASS_BRIDGE_ISA_PDECODE, 0)
27 CHAR16 mFirmwareVendor
[] = L
"TianoCore.org";
28 extern BOOLEAN gConnectAllHappened
;
29 extern USB_CLASS_FORMAT_DEVICE_PATH gUsbClassKeyboardDevicePath
;
31 EFI_GUID
*gTableGuidArray
[] = {
32 &gEfiAcpi20TableGuid
, &gEfiAcpiTableGuid
, &gEfiSmbiosTableGuid
, &gEfiMpsTableGuid
36 // BDS Platform Functions
40 GetSystemTablesFromHob (
46 Find GUID'ed HOBs that contain EFI_PHYSICAL_ADDRESS of ACPI, SMBIOS, MPs tables
56 EFI_PEI_HOB_POINTERS GuidHob
;
57 EFI_PEI_HOB_POINTERS HobStart
;
58 EFI_PHYSICAL_ADDRESS
*Table
;
64 HobStart
.Raw
= GetHobList ();
66 // Iteratively add ACPI Table, SMBIOS Table, MPS Table to EFI System Table
68 for (Index
= 0; Index
< sizeof (gTableGuidArray
) / sizeof (*gTableGuidArray
); ++Index
) {
69 GuidHob
.Raw
= GetNextGuidHob (gTableGuidArray
[Index
], HobStart
.Raw
);
70 if (GuidHob
.Raw
!= NULL
) {
71 Table
= GET_GUID_HOB_DATA (GuidHob
.Guid
);
74 // Check if Mps Table/Smbios Table/Acpi Table exists in E/F seg,
75 // According to UEFI Spec, we should make sure Smbios table,
76 // ACPI table and Mps tables kept in memory of specified type
78 ConvertSystemTable(gTableGuidArray
[Index
], (VOID
**)&Table
);
79 gBS
->InstallConfigurationTable (gTableGuidArray
[Index
], (VOID
*)Table
);
87 #define EFI_LDR_MEMORY_DESCRIPTOR_GUID \
88 { 0x7701d7e5, 0x7d1d, 0x4432, {0xa4, 0x68, 0x67, 0x3d, 0xab, 0x8a, 0xde, 0x60 }}
90 EFI_GUID gEfiLdrMemoryDescriptorGuid
= EFI_LDR_MEMORY_DESCRIPTOR_GUID
;
95 EFI_HOB_GUID_TYPE Hob
;
97 EFI_MEMORY_DESCRIPTOR
*MemDesc
;
108 EFI_MEMORY_DESCRIPTOR
*MemMap
;
109 EFI_MEMORY_DESCRIPTOR
*MemMapPtr
;
111 UINTN MapKey
, DescriptorSize
;
113 UINT32 DescriptorVersion
;
119 Status
= gBS
->GetMemoryMap (&MemMapSize
, MemMap
, &MapKey
, &DescriptorSize
, &DescriptorVersion
);
120 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
121 MemMapSize
+= EFI_PAGE_SIZE
;
122 Status
= gBS
->AllocatePool (EfiBootServicesData
, MemMapSize
, &MemMap
);
123 ASSERT (Status
== EFI_SUCCESS
);
124 Status
= gBS
->GetMemoryMap (&MemMapSize
, MemMap
, &MapKey
, &DescriptorSize
, &DescriptorVersion
);
125 ASSERT (Status
== EFI_SUCCESS
);
128 ASSERT (DescriptorVersion
== EFI_MEMORY_DESCRIPTOR_VERSION
);
130 for (Index
= 0; Index
< MemMapSize
/ DescriptorSize
; Index
++) {
131 Bytes
= LShiftU64 (MemMap
->NumberOfPages
, 12);
132 DEBUG ((EFI_D_ERROR
, "%lX-%lX %lX %lX %X\n",
133 MemMap
->PhysicalStart
,
134 MemMap
->PhysicalStart
+ Bytes
- 1,
135 MemMap
->NumberOfPages
,
137 (UINTN
)MemMap
->Type
));
138 MemMap
= (EFI_MEMORY_DESCRIPTOR
*)((UINTN
)MemMap
+ DescriptorSize
);
141 gBS
->FreePool (MemMapPtr
);
151 EFI_PEI_HOB_POINTERS GuidHob
;
153 MEMORY_DESC_HOB MemoryDescHob
;
155 EFI_PHYSICAL_ADDRESS Memory
;
160 GuidHob
.Raw
= GetHobList();
162 GuidHob
.Raw
= GetNextGuidHob (&gEfiLdrMemoryDescriptorGuid
, GuidHob
.Raw
);
163 if (GuidHob
.Raw
== NULL
) {
164 DEBUG ((EFI_D_ERROR
, "Fail to get gEfiLdrMemoryDescriptorGuid from GUID HOB LIST!\n"));
167 Table
= GET_GUID_HOB_DATA (GuidHob
.Guid
);
169 DEBUG ((EFI_D_ERROR
, "Fail to get gEfiLdrMemoryDescriptorGuid from GUID HOB LIST!\n"));
172 MemoryDescHob
.MemDescCount
= *(UINTN
*)Table
;
173 MemoryDescHob
.MemDesc
= *(EFI_MEMORY_DESCRIPTOR
**)((UINTN
)Table
+ sizeof(UINTN
));
176 // Add ACPINVS, ACPIReclaim, and Reserved memory to MemoryMap
178 for (Index
= 0; Index
< MemoryDescHob
.MemDescCount
; Index
++) {
179 if (MemoryDescHob
.MemDesc
[Index
].PhysicalStart
< 0x100000) {
182 if (MemoryDescHob
.MemDesc
[Index
].PhysicalStart
>= 0x100000000ULL
) {
185 if ((MemoryDescHob
.MemDesc
[Index
].Type
== EfiReservedMemoryType
) ||
186 (MemoryDescHob
.MemDesc
[Index
].Type
== EfiRuntimeServicesData
) ||
187 (MemoryDescHob
.MemDesc
[Index
].Type
== EfiRuntimeServicesCode
) ||
188 (MemoryDescHob
.MemDesc
[Index
].Type
== EfiACPIReclaimMemory
) ||
189 (MemoryDescHob
.MemDesc
[Index
].Type
== EfiACPIMemoryNVS
)) {
190 DEBUG ((EFI_D_ERROR
, "PhysicalStart - 0x%x, ", MemoryDescHob
.MemDesc
[Index
].PhysicalStart
));
191 DEBUG ((EFI_D_ERROR
, "PageNumber - 0x%x, ", MemoryDescHob
.MemDesc
[Index
].NumberOfPages
));
192 DEBUG ((EFI_D_ERROR
, "Type - 0x%x\n", MemoryDescHob
.MemDesc
[Index
].Type
));
193 if ((MemoryDescHob
.MemDesc
[Index
].Type
== EfiRuntimeServicesData
) ||
194 (MemoryDescHob
.MemDesc
[Index
].Type
== EfiRuntimeServicesCode
)) {
196 // Skip RuntimeSevicesData and RuntimeServicesCode, they are BFV
200 Status
= gDS
->AddMemorySpace (
201 EfiGcdMemoryTypeSystemMemory
,
202 MemoryDescHob
.MemDesc
[Index
].PhysicalStart
,
203 LShiftU64 (MemoryDescHob
.MemDesc
[Index
].NumberOfPages
, EFI_PAGE_SHIFT
),
204 MemoryDescHob
.MemDesc
[Index
].Attribute
206 if (EFI_ERROR (Status
)) {
207 DEBUG ((EFI_D_ERROR
, "AddMemorySpace fail!\n"));
208 if ((MemoryDescHob
.MemDesc
[Index
].Type
== EfiACPIReclaimMemory
) ||
209 (MemoryDescHob
.MemDesc
[Index
].Type
== EfiACPIMemoryNVS
)) {
211 // For EfiACPIReclaimMemory and EfiACPIMemoryNVS, it must success.
212 // For EfiReservedMemoryType, there maybe overlap. So skip check here.
214 // ASSERT_EFI_ERROR (Status);
219 Memory
= MemoryDescHob
.MemDesc
[Index
].PhysicalStart
;
220 Status
= gBS
->AllocatePages (
222 (EFI_MEMORY_TYPE
)MemoryDescHob
.MemDesc
[Index
].Type
,
223 (UINTN
)MemoryDescHob
.MemDesc
[Index
].NumberOfPages
,
226 if (EFI_ERROR (Status
)) {
227 DEBUG ((EFI_D_ERROR
, "AllocatePages fail!\n"));
229 // For the page added, it must be allocated.
231 // ASSERT_EFI_ERROR (Status);
240 DisableUsbLegacySupport(
246 Disabble the USB legacy Support in all Ehci and Uhci.
247 This function assume all PciIo handles have been created in system.
258 EFI_HANDLE
*HandleArray
;
259 UINTN HandleArrayCount
;
261 EFI_PCI_IO_PROTOCOL
*PciIo
;
270 // Find the usb host controller
272 Status
= gBS
->LocateHandleBuffer (
274 &gEfiPciIoProtocolGuid
,
279 if (!EFI_ERROR (Status
)) {
280 for (Index
= 0; Index
< HandleArrayCount
; Index
++) {
281 Status
= gBS
->HandleProtocol (
283 &gEfiPciIoProtocolGuid
,
286 if (!EFI_ERROR (Status
)) {
288 // Find the USB host controller controller
290 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, 0x09, 3, &Class
);
291 if (!EFI_ERROR (Status
)) {
292 if ((PCI_CLASS_SERIAL
== Class
[2]) &&
293 (PCI_CLASS_SERIAL_USB
== Class
[1])) {
294 if (PCI_CLASSC_PI_UHCI
== Class
[0]) {
296 // Found the UHCI, then disable the legacy support
299 Status
= PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint16
, 0xC0, 1, &Command
);
300 } else if (PCI_CLASSC_PI_EHCI
== Class
[0]) {
302 // Found the EHCI, then disable the legacy support
304 Status
= PciIo
->Mem
.Read (
308 (UINT64
) 0x08, //EHC_HCCPARAMS_OFFSET
313 ExtendCap
= (HcCapParams
>> 8) & 0xFF;
315 // Disable the SMI in USBLEGCTLSTS firstly
317 PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint32
, ExtendCap
+ 0x4, 1, &Value
);
319 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, ExtendCap
+ 0x4, 1, &Value
);
322 // Get EHCI Ownership from legacy bios
324 PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint32
, ExtendCap
, 1, &Value
);
325 Value
|= (0x1 << 24);
326 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, ExtendCap
, 1, &Value
);
332 PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint32
, ExtendCap
, 1, &Value
);
334 if ((Value
& 0x01010000) == 0x01000000) {
346 gBS
->FreePool (HandleArray
);
360 Platform Bds init. Incude the platform firmware vendor, revision
372 // set firmwarevendor, here can be IBV/OEM customize
374 gST
->FirmwareVendor
= AllocateRuntimeCopyPool (
375 sizeof (mFirmwareVendor
),
378 ASSERT (gST
->FirmwareVendor
!= NULL
);
380 gST
->FirmwareRevision
= 0;
383 // Fixup Tasble CRC after we updated Firmware Vendor and Revision
385 gBS
->CalculateCrc32 ((VOID
*) gST
, sizeof (EFI_SYSTEM_TABLE
), &gST
->Hdr
.CRC32
);
387 GetSystemTablesFromHob ();
392 // Append Usb Keyboard short form DevicePath into "ConInDev"
394 BdsLibUpdateConsoleVariable (
396 (EFI_DEVICE_PATH_PROTOCOL
*) &gUsbClassKeyboardDevicePath
,
402 GetPciExpressBaseAddressForRootBridge (
403 IN UINTN HostBridgeNumber
,
404 IN UINTN RootBridgeNumber
409 This routine is to get PciExpress Base Address for this RootBridge
412 HostBridgeNumber - The number of HostBridge
413 RootBridgeNumber - The number of RootBridge
416 UINT64 - PciExpressBaseAddress for this HostBridge and RootBridge
420 EFI_PCI_EXPRESS_BASE_ADDRESS_INFORMATION
*PciExpressBaseAddressInfo
;
424 EFI_PEI_HOB_POINTERS GuidHob
;
427 // Get PciExpressAddressInfo Hob
429 PciExpressBaseAddressInfo
= NULL
;
431 GuidHob
.Raw
= GetFirstGuidHob (&gEfiPciExpressBaseAddressGuid
);
432 if (GuidHob
.Raw
!= NULL
) {
433 PciExpressBaseAddressInfo
= GET_GUID_HOB_DATA (GuidHob
.Guid
);
434 BufferSize
= GET_GUID_HOB_DATA_SIZE (GuidHob
.Guid
);
440 // Search the PciExpress Base Address in the Hob for current RootBridge
442 Number
= (UINT32
)(BufferSize
/ sizeof(EFI_PCI_EXPRESS_BASE_ADDRESS_INFORMATION
));
443 for (Index
= 0; Index
< Number
; Index
++) {
444 if ((PciExpressBaseAddressInfo
[Index
].HostBridgeNumber
== HostBridgeNumber
) &&
445 (PciExpressBaseAddressInfo
[Index
].RootBridgeNumber
== RootBridgeNumber
)) {
446 return PciExpressBaseAddressInfo
[Index
].PciExpressBaseAddress
;
451 // Do not find the PciExpress Base Address in the Hob
457 PatchPciRootBridgeDevicePath (
458 IN UINTN HostBridgeNumber
,
459 IN UINTN RootBridgeNumber
,
460 IN PLATFORM_ROOT_BRIDGE_DEVICE_PATH
*RootBridge
463 UINT64 PciExpressBase
;
465 PciExpressBase
= GetPciExpressBaseAddressForRootBridge (HostBridgeNumber
, RootBridgeNumber
);
467 DEBUG ((EFI_D_INFO
, "Get PciExpress Address from Hob: 0x%X\n", PciExpressBase
));
469 if (PciExpressBase
!= 0) {
470 RootBridge
->PciRootBridge
.HID
= EISA_PNP_ID(0x0A08);
490 EFI_SUCCESS - Connect RootBridge successfully.
491 EFI_STATUS - Connect RootBridge fail.
496 EFI_HANDLE RootHandle
;
499 // Patch Pci Root Bridge Device Path
501 PatchPciRootBridgeDevicePath (0, 0, &gPlatformRootBridge0
);
504 // Make all the PCI_IO protocols on PCI Seg 0 show up
506 BdsLibConnectDevicePath (gPlatformRootBridges
[0]);
508 Status
= gBS
->LocateDevicePath (
509 &gEfiDevicePathProtocolGuid
,
510 &gPlatformRootBridges
[0],
513 DEBUG ((EFI_D_INFO
, "Pci Root bridge handle is 0x%X\n", RootHandle
));
515 if (EFI_ERROR (Status
)) {
519 Status
= gBS
->ConnectController (RootHandle
, NULL
, NULL
, FALSE
);
520 if (EFI_ERROR (Status
)) {
528 PrepareLpcBridgeDevicePath (
529 IN EFI_HANDLE DeviceHandle
535 Add IsaKeyboard to ConIn,
536 add IsaSerial to ConOut, ConIn, ErrOut.
541 DeviceHandle - Handle of PCIIO protocol.
545 EFI_SUCCESS - LPC bridge is added to ConOut, ConIn, and ErrOut.
546 EFI_STATUS - No LPC bridge is added.
551 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
552 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
555 Status
= gBS
->HandleProtocol (
557 &gEfiDevicePathProtocolGuid
,
560 if (EFI_ERROR (Status
)) {
563 TempDevicePath
= DevicePath
;
568 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)&gPnpPs2KeyboardDeviceNode
);
570 BdsLibUpdateConsoleVariable (VarConsoleInp
, DevicePath
, NULL
);
575 DevicePath
= TempDevicePath
;
576 gPnp16550ComPortDeviceNode
.UID
= 0;
578 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)&gPnp16550ComPortDeviceNode
);
579 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)&gUartDeviceNode
);
580 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)&gTerminalTypeDeviceNode
);
582 BdsLibUpdateConsoleVariable (VarConsoleOut
, DevicePath
, NULL
);
583 BdsLibUpdateConsoleVariable (VarConsoleInp
, DevicePath
, NULL
);
584 BdsLibUpdateConsoleVariable (VarErrorOut
, DevicePath
, NULL
);
589 DevicePath
= TempDevicePath
;
590 gPnp16550ComPortDeviceNode
.UID
= 1;
592 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)&gPnp16550ComPortDeviceNode
);
593 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)&gUartDeviceNode
);
594 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)&gTerminalTypeDeviceNode
);
596 BdsLibUpdateConsoleVariable (VarConsoleOut
, DevicePath
, NULL
);
597 BdsLibUpdateConsoleVariable (VarConsoleInp
, DevicePath
, NULL
);
598 BdsLibUpdateConsoleVariable (VarErrorOut
, DevicePath
, NULL
);
605 IN EFI_DEVICE_PATH_PROTOCOL
*PciDevicePath
,
606 OUT EFI_DEVICE_PATH_PROTOCOL
**GopDevicePath
611 EFI_HANDLE PciDeviceHandle
;
612 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
613 EFI_DEVICE_PATH_PROTOCOL
*TempPciDevicePath
;
614 UINTN GopHandleCount
;
615 EFI_HANDLE
*GopHandleBuffer
;
617 if (PciDevicePath
== NULL
|| GopDevicePath
== NULL
) {
618 return EFI_INVALID_PARAMETER
;
622 // Initialize the GopDevicePath to be PciDevicePath
624 *GopDevicePath
= PciDevicePath
;
625 TempPciDevicePath
= PciDevicePath
;
627 Status
= gBS
->LocateDevicePath (
628 &gEfiDevicePathProtocolGuid
,
632 if (EFI_ERROR (Status
)) {
637 // Try to connect this handle, so that GOP dirver could start on this
638 // device and create child handles with GraphicsOutput Protocol installed
639 // on them, then we get device paths of these child handles and select
640 // them as possible console device.
642 gBS
->ConnectController (PciDeviceHandle
, NULL
, NULL
, FALSE
);
644 Status
= gBS
->LocateHandleBuffer (
646 &gEfiGraphicsOutputProtocolGuid
,
651 if (!EFI_ERROR (Status
)) {
653 // Add all the child handles as possible Console Device
655 for (Index
= 0; Index
< GopHandleCount
; Index
++) {
656 Status
= gBS
->HandleProtocol (GopHandleBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*)&TempDevicePath
);
657 if (EFI_ERROR (Status
)) {
663 GetDevicePathSize (PciDevicePath
) - END_DEVICE_PATH_LENGTH
666 // In current implementation, we only enable one of the child handles
667 // as console device, i.e. sotre one of the child handle's device
668 // path to variable "ConOut"
669 // In futhure, we could select all child handles to be console device
672 *GopDevicePath
= TempDevicePath
;
675 // Delete the PCI device's path that added by GetPlugInPciVgaDevicePath()
676 // Add the integrity GOP device path.
678 BdsLibUpdateConsoleVariable (VarConsoleOutDev
, NULL
, PciDevicePath
);
679 BdsLibUpdateConsoleVariable (VarConsoleOutDev
, TempDevicePath
, NULL
);
682 gBS
->FreePool (GopHandleBuffer
);
689 PreparePciVgaDevicePath (
690 IN EFI_HANDLE DeviceHandle
696 Add PCI VGA to ConOut.
701 DeviceHandle - Handle of PCIIO protocol.
705 EFI_SUCCESS - PCI VGA is added to ConOut.
706 EFI_STATUS - No PCI VGA device is added.
711 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
712 EFI_DEVICE_PATH_PROTOCOL
*GopDevicePath
;
715 Status
= gBS
->HandleProtocol (
717 &gEfiDevicePathProtocolGuid
,
720 if (EFI_ERROR (Status
)) {
724 GetGopDevicePath (DevicePath
, &GopDevicePath
);
725 DevicePath
= GopDevicePath
;
727 BdsLibUpdateConsoleVariable (VarConsoleOut
, DevicePath
, NULL
);
733 PreparePciSerialDevicePath (
734 IN EFI_HANDLE DeviceHandle
740 Add PCI Serial to ConOut, ConIn, ErrOut.
745 DeviceHandle - Handle of PCIIO protocol.
749 EFI_SUCCESS - PCI Serial is added to ConOut, ConIn, and ErrOut.
750 EFI_STATUS - No PCI Serial device is added.
755 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
758 Status
= gBS
->HandleProtocol (
760 &gEfiDevicePathProtocolGuid
,
763 if (EFI_ERROR (Status
)) {
767 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)&gUartDeviceNode
);
768 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)&gTerminalTypeDeviceNode
);
770 BdsLibUpdateConsoleVariable (VarConsoleOut
, DevicePath
, NULL
);
771 BdsLibUpdateConsoleVariable (VarConsoleInp
, DevicePath
, NULL
);
772 BdsLibUpdateConsoleVariable (VarErrorOut
, DevicePath
, NULL
);
778 DetectAndPreparePlatformPciDevicePath (
779 BOOLEAN DetectVgaOnly
785 Do platform specific PCI Device check and add them to ConOut, ConIn, ErrOut
789 DetectVgaOnly - Only detect VGA device if it's TRUE.
793 EFI_SUCCESS - PCI Device check and Console variable update successfully.
794 EFI_STATUS - PCI Device check or Console variable update fail.
800 EFI_HANDLE
*HandleBuffer
;
802 EFI_PCI_IO_PROTOCOL
*PciIo
;
806 // Start to check all the PciIo to find all possible device
810 Status
= gBS
->LocateHandleBuffer (
812 &gEfiPciIoProtocolGuid
,
817 if (EFI_ERROR (Status
)) {
821 for (Index
= 0; Index
< HandleCount
; Index
++) {
822 Status
= gBS
->HandleProtocol (HandleBuffer
[Index
], &gEfiPciIoProtocolGuid
, (VOID
*)&PciIo
);
823 if (EFI_ERROR (Status
)) {
828 // Check for all PCI device
830 Status
= PciIo
->Pci
.Read (
834 sizeof (Pci
) / sizeof (UINT32
),
837 if (EFI_ERROR (Status
)) {
841 if (!DetectVgaOnly
) {
843 // Here we decide whether it is LPC Bridge
845 if ((IS_PCI_LPC (&Pci
)) ||
846 ((IS_PCI_ISA_PDECODE (&Pci
)) && (Pci
.Hdr
.VendorId
== 0x8086) && (Pci
.Hdr
.DeviceId
== 0x7110))) {
848 // Add IsaKeyboard to ConIn,
849 // add IsaSerial to ConOut, ConIn, ErrOut
851 DEBUG ((EFI_D_INFO
, "Find the LPC Bridge device\n"));
852 PrepareLpcBridgeDevicePath (HandleBuffer
[Index
]);
856 // Here we decide which Serial device to enable in PCI bus
858 if (IS_PCI_16550SERIAL (&Pci
)) {
860 // Add them to ConOut, ConIn, ErrOut.
862 DEBUG ((EFI_D_INFO
, "Find the 16550 SERIAL device\n"));
863 PreparePciSerialDevicePath (HandleBuffer
[Index
]);
869 // Here we decide which VGA device to enable in PCI bus
871 if (IS_PCI_VGA (&Pci
)) {
873 // Add them to ConOut.
875 DEBUG ((EFI_D_INFO
, "Find the VGA device\n"));
876 PreparePciVgaDevicePath (HandleBuffer
[Index
]);
881 gBS
->FreePool (HandleBuffer
);
887 PlatformBdsConnectConsole (
888 IN BDS_CONSOLE_CONNECT_ENTRY
*PlatformConsole
894 Connect the predefined platform default console device. Always try to find
895 and enable the vga device if have.
899 PlatformConsole - Predfined platform default console device array.
903 EFI_SUCCESS - Success connect at least one ConIn and ConOut
904 device, there must have one ConOut device is
907 EFI_STATUS - Return the status of
908 BdsLibConnectAllDefaultConsoles ()
914 EFI_DEVICE_PATH_PROTOCOL
*VarConout
;
915 EFI_DEVICE_PATH_PROTOCOL
*VarConin
;
916 UINTN DevicePathSize
;
919 // Connect RootBridge
921 ConnectRootBridge ();
923 VarConout
= BdsLibGetVariableAndSize (
925 &gEfiGlobalVariableGuid
,
928 VarConin
= BdsLibGetVariableAndSize (
930 &gEfiGlobalVariableGuid
,
934 if (VarConout
== NULL
|| VarConin
== NULL
) {
936 // Do platform specific PCI Device check and add them to ConOut, ConIn, ErrOut
938 DetectAndPreparePlatformPciDevicePath (FALSE
);
941 // Have chance to connect the platform default console,
942 // the platform default console is the minimue device group
943 // the platform should support
945 for (Index
= 0; PlatformConsole
[Index
].DevicePath
!= NULL
; ++Index
) {
947 // Update the console variable with the connect type
949 if ((PlatformConsole
[Index
].ConnectType
& CONSOLE_IN
) == CONSOLE_IN
) {
950 BdsLibUpdateConsoleVariable (VarConsoleInp
, PlatformConsole
[Index
].DevicePath
, NULL
);
952 if ((PlatformConsole
[Index
].ConnectType
& CONSOLE_OUT
) == CONSOLE_OUT
) {
953 BdsLibUpdateConsoleVariable (VarConsoleOut
, PlatformConsole
[Index
].DevicePath
, NULL
);
955 if ((PlatformConsole
[Index
].ConnectType
& STD_ERROR
) == STD_ERROR
) {
956 BdsLibUpdateConsoleVariable (VarErrorOut
, PlatformConsole
[Index
].DevicePath
, NULL
);
961 // Only detect VGA device and add them to ConOut
963 DetectAndPreparePlatformPciDevicePath (TRUE
);
967 // The ConIn devices connection will start the USB bus, should disable all
968 // Usb legacy support firstly.
969 // Caution: Must ensure the PCI bus driver has been started. Since the
970 // ConnectRootBridge() will create all the PciIo protocol, it's safe here now
972 Status
= DisableUsbLegacySupport();
975 // Connect the all the default console with current cosole variable
977 Status
= BdsLibConnectAllDefaultConsoles ();
978 if (EFI_ERROR (Status
)) {
986 PlatformBdsConnectSequence (
993 Connect with predeined platform connect sequence,
994 the OEM/IBV can customize with their own connect sequence.
1011 // Here we can get the customized platform connect sequence
1012 // Notes: we can connect with new variable which record the
1013 // last time boots connect device path sequence
1015 while (gPlatformConnectSequence
[Index
] != NULL
) {
1017 // Build the platform boot option
1019 BdsLibConnectDevicePath (gPlatformConnectSequence
[Index
]);
1026 PlatformBdsGetDriverOption (
1027 IN OUT LIST_ENTRY
*BdsDriverLists
1031 Routine Description:
1033 Load the predefined driver option, OEM/IBV can customize this
1034 to load their own drivers
1038 BdsDriverLists - The header of the driver option link list.
1051 // Here we can get the customized platform driver option
1053 while (gPlatformDriverOption
[Index
] != NULL
) {
1055 // Build the platform boot option
1057 BdsLibRegisterNewOption (BdsDriverLists
, gPlatformDriverOption
[Index
], NULL
, L
"DriverOrder");
1064 PlatformBdsDiagnostics (
1065 IN EXTENDMEM_COVERAGE_LEVEL MemoryTestLevel
,
1066 IN BOOLEAN QuietBoot
1070 Routine Description:
1072 Perform the platform diagnostic, such like test memory. OEM/IBV also
1073 can customize this fuction to support specific platform diagnostic.
1077 MemoryTestLevel - The memory test intensive level
1079 QuietBoot - Indicate if need to enable the quiet boot
1090 // Here we can decide if we need to show
1091 // the diagnostics screen
1092 // Notes: this quiet boot code should be remove
1093 // from the graphic lib
1096 Status
= EnableQuietBoot (&gEfiDefaultBmpLogoGuid
);
1097 if (EFI_ERROR (Status
)) {
1098 DisableQuietBoot ();
1103 // Perform system diagnostic
1105 Status
= BdsMemoryTest (MemoryTestLevel
);
1106 if (EFI_ERROR (Status
)) {
1107 DisableQuietBoot ();
1113 // Perform system diagnostic
1115 Status
= BdsMemoryTest (MemoryTestLevel
);
1120 PlatformBdsPolicyBehavior (
1121 IN OUT LIST_ENTRY
*DriverOptionList
,
1122 IN OUT LIST_ENTRY
*BootOptionList
1126 Routine Description:
1128 The function will excute with as the platform policy, current policy
1129 is driven by boot mode. IBV/OEM can customize this code for their specific
1134 DriverOptionList - The header of the driver option link list
1136 BootOptionList - The header of the boot option link list
1146 EFI_EVENT UserInputDurationTime
;
1148 BDS_COMMON_OPTION
*BootOption
;
1152 EFI_BOOT_MODE BootMode
;
1155 // Init the time out value
1157 Timeout
= PcdGet16 (PcdPlatformBootTimeOut
);
1160 // Load the driver option as the driver option list
1162 PlatformBdsGetDriverOption (DriverOptionList
);
1165 // Get current Boot Mode
1167 Status
= BdsLibGetBootMode (&BootMode
);
1168 DEBUG ((EFI_D_ERROR
, "Boot Mode:%x\n", BootMode
));
1171 // Go the different platform policy with different boot mode
1172 // Notes: this part code can be change with the table policy
1174 ASSERT (BootMode
== BOOT_WITH_FULL_CONFIGURATION
);
1176 // Connect platform console
1178 Status
= PlatformBdsConnectConsole (gPlatformConsole
);
1179 if (EFI_ERROR (Status
)) {
1181 // Here OEM/IBV can customize with defined action
1183 PlatformBdsNoConsoleAction ();
1186 // Create a 300ms duration event to ensure user has enough input time to enter Setup
1188 Status
= gBS
->CreateEvent (
1193 &UserInputDurationTime
1195 ASSERT (Status
== EFI_SUCCESS
);
1196 Status
= gBS
->SetTimer (UserInputDurationTime
, TimerRelative
, 3000000);
1197 ASSERT (Status
== EFI_SUCCESS
);
1199 // Memory test and Logo show
1201 PlatformBdsDiagnostics (IGNORE
, TRUE
);
1204 // Perform some platform specific connect sequence
1206 PlatformBdsConnectSequence ();
1209 // Give one chance to enter the setup if we
1210 // have the time out
1212 // BUGBUG: hard code timeout to 5 second to show logo in graphic mode.
1215 PlatformBdsEnterFrontPage (Timeout
, FALSE
);
1219 //BdsLibConnectAll ();
1220 //BdsLibEnumerateAllBootOption (BootOptionList);
1223 // Please uncomment above ConnectAll and EnumerateAll code and remove following first boot
1224 // checking code in real production tip.
1226 // In BOOT_WITH_FULL_CONFIGURATION boot mode, should always connect every device
1227 // and do enumerate all the default boot options. But in development system board, the boot mode
1228 // cannot be BOOT_ASSUMING_NO_CONFIGURATION_CHANGES because the machine box
1229 // is always open. So the following code only do the ConnectAll and EnumerateAll at first boot.
1231 Status
= BdsLibBuildOptionFromVar (BootOptionList
, L
"BootOrder");
1232 if (EFI_ERROR(Status
)) {
1234 // If cannot find "BootOrder" variable, it may be first boot.
1235 // Try to connect all devices and enumerate all boot options here.
1237 BdsLibConnectAll ();
1238 BdsLibEnumerateAllBootOption (BootOptionList
);
1242 // To give the User a chance to enter Setup here, if user set TimeOut is 0.
1243 // BDS should still give user a chance to enter Setup
1245 // Connect first boot option, and then check user input before exit
1247 for (Link
= BootOptionList
->ForwardLink
; Link
!= BootOptionList
;Link
= Link
->ForwardLink
) {
1248 BootOption
= CR (Link
, BDS_COMMON_OPTION
, Link
, BDS_LOAD_OPTION_SIGNATURE
);
1249 if (!IS_LOAD_OPTION_TYPE (BootOption
->Attribute
, LOAD_OPTION_ACTIVE
)) {
1251 // skip the header of the link list, becuase it has no boot option
1256 // Make sure the boot option device path connected, but ignore the BBS device path
1258 if (DevicePathType (BootOption
->DevicePath
) != BBS_DEVICE_PATH
) {
1259 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1266 // Check whether the user input after the duration time has expired
1268 OldTpl
= EfiGetCurrentTpl();
1269 gBS
->RestoreTPL (TPL_APPLICATION
);
1270 gBS
->WaitForEvent (1, &UserInputDurationTime
, &Index
);
1271 gBS
->CloseEvent (UserInputDurationTime
);
1272 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
1273 gBS
->RaiseTPL (OldTpl
);
1275 if (!EFI_ERROR (Status
)) {
1277 // Enter Setup if user input
1280 PlatformBdsEnterFrontPage (Timeout
, FALSE
);
1289 PlatformBdsBootSuccess (
1290 IN BDS_COMMON_OPTION
*Option
1294 Routine Description:
1296 Hook point after a boot attempt succeeds. We don't expect a boot option to
1297 return, so the EFI 1.0 specification defines that you will default to an
1298 interactive mode and stop processing the BootOrder list in this case. This
1299 is alos a platform implementation and can be customized by IBV/OEM.
1303 Option - Pointer to Boot Option that succeeded to boot.
1314 // If Boot returned with EFI_SUCCESS and there is not in the boot device
1315 // select loop then we need to pop up a UI and wait for user input.
1317 TmpStr
= Option
->StatusString
;
1318 if (TmpStr
!= NULL
) {
1319 BdsLibOutputStrings (gST
->ConOut
, TmpStr
, Option
->Description
, L
"\n\r", NULL
);
1320 gBS
->FreePool (TmpStr
);
1326 PlatformBdsBootFail (
1327 IN BDS_COMMON_OPTION
*Option
,
1328 IN EFI_STATUS Status
,
1329 IN CHAR16
*ExitData
,
1330 IN UINTN ExitDataSize
1334 Routine Description:
1336 Hook point after a boot attempt fails.
1340 Option - Pointer to Boot Option that failed to boot.
1342 Status - Status returned from failed boot.
1344 ExitData - Exit data returned from failed boot.
1346 ExitDataSize - Exit data size returned from failed boot.
1357 // If Boot returned with failed status then we need to pop up a UI and wait
1360 TmpStr
= Option
->StatusString
;
1361 if (TmpStr
!= NULL
) {
1362 BdsLibOutputStrings (gST
->ConOut
, TmpStr
, Option
->Description
, L
"\n\r", NULL
);
1363 gBS
->FreePool (TmpStr
);
1369 PlatformBdsNoConsoleAction (
1374 Routine Description:
1376 This function is remained for IBV/OEM to do some platform action,
1377 if there no console device can be connected.
1385 EFI_SUCCESS - Direct return success now.
1393 ConvertSystemTable (
1394 IN EFI_GUID
*TableGuid
,
1399 Routine Description:
1400 Convert ACPI Table /Smbios Table /MP Table if its location is lower than Address:0x100000
1402 As in legacy Bios, ACPI/Smbios/MP table is required to place in E/F Seg,
1403 So here we just check if the range is E/F seg,
1404 and if Not, assume the Memory type is EfiACPIReclaimMemory/EfiACPIMemoryNVS
1407 TableGuid - Guid of the table
1408 Table - pointer to the table
1411 EFI_SUCEESS - Convert Table successfully
1421 // If match acpi guid (1.0, 2.0, or later), Convert ACPI table according to version.
1423 AcpiHeader
= (VOID
*)(UINTN
)(*(UINT64
*)(*Table
));
1425 if (CompareGuid(TableGuid
, &gEfiAcpiTableGuid
) || CompareGuid(TableGuid
, &gEfiAcpi20TableGuid
)){
1426 if (((EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER
*)AcpiHeader
)->Reserved
== 0x00){
1428 // If Acpi 1.0 Table, then RSDP structure doesn't contain Length field, use structure size
1430 AcpiTableLen
= sizeof (EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER
);
1431 } else if (((EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER
*)AcpiHeader
)->Reserved
>= 0x02){
1433 // If Acpi 2.0 or later, use RSDP Length fied.
1435 AcpiTableLen
= ((EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER
*)AcpiHeader
)->Length
;
1438 // Invalid Acpi Version, return
1440 return EFI_UNSUPPORTED
;
1442 Status
= ConvertAcpiTable (AcpiTableLen
, Table
);
1447 // If matches smbios guid, convert Smbios table.
1449 if (CompareGuid(TableGuid
, &gEfiSmbiosTableGuid
)){
1450 Status
= ConvertSmbiosTable (Table
);
1455 // If the table is MP table?
1457 if (CompareGuid(TableGuid
, &gEfiMpsTableGuid
)){
1458 Status
= ConvertMpsTable (Table
);
1462 return EFI_UNSUPPORTED
;
1472 Routine Description:
1473 Caculate buffer checksum (8-bit)
1476 Buffer - Pointer to Buffer that to be caculated
1477 Length - How many bytes are to be caculated
1480 Checksum of the buffer
1488 Ptr8
= (UINT8
*) Buffer
;
1490 while (Length
> 0) {
1491 CheckSum
= (UINT8
) (CheckSum
+ *Ptr8
++);
1495 return (UINT8
)((0xFF - CheckSum
) + 1);
1505 Routine Description:
1506 Convert RSDP of ACPI Table if its location is lower than Address:0x100000
1508 As in legacy Bios, ACPI table is required to place in E/F Seg,
1509 So here we just check if the range is E/F seg,
1510 and if Not, assume the Memory type is EfiACPIReclaimMemory/EfiACPIMemoryNVS
1513 TableLen - Acpi RSDP length
1514 Table - pointer to the table
1517 EFI_SUCEESS - Convert Table successfully
1525 EFI_PHYSICAL_ADDRESS BufferPtr
;
1528 AcpiTableOri
= (VOID
*)(UINTN
)(*(UINT64
*)(*Table
));
1529 if (((UINTN
)AcpiTableOri
< 0x100000) && ((UINTN
)AcpiTableOri
> 0xE0000)) {
1530 BufferPtr
= EFI_SYSTEM_TABLE_MAX_ADDRESS
;
1531 Status
= gBS
->AllocatePages (
1534 EFI_SIZE_TO_PAGES(TableLen
),
1537 ASSERT_EFI_ERROR (Status
);
1538 AcpiTableNew
= (VOID
*)(UINTN
)BufferPtr
;
1539 CopyMem (AcpiTableNew
, AcpiTableOri
, TableLen
);
1541 AcpiTableNew
= AcpiTableOri
;
1544 // Change configuration table Pointer
1546 *Table
= AcpiTableNew
;
1552 ConvertSmbiosTable (
1557 Routine Description:
1559 Convert Smbios Table if the Location of the SMBios Table is lower than Addres 0x100000
1561 As in legacy Bios, Smbios table is required to place in E/F Seg,
1562 So here we just check if the range is F seg,
1563 and if Not, assume the Memory type is EfiACPIMemoryNVS/EfiRuntimeServicesData
1565 Table - pointer to the table
1568 EFI_SUCEESS - Convert Table successfully
1573 SMBIOS_TABLE_ENTRY_POINT
*SmbiosTableNew
;
1574 SMBIOS_TABLE_ENTRY_POINT
*SmbiosTableOri
;
1576 UINT32 SmbiosEntryLen
;
1578 EFI_PHYSICAL_ADDRESS BufferPtr
;
1580 SmbiosTableNew
= NULL
;
1581 SmbiosTableOri
= NULL
;
1584 // Get Smibos configuration Table
1586 SmbiosTableOri
= (SMBIOS_TABLE_ENTRY_POINT
*)(UINTN
)(*(UINT64
*)(*Table
));
1588 if ((SmbiosTableOri
== NULL
) ||
1589 ((UINTN
)SmbiosTableOri
> 0x100000) ||
1590 ((UINTN
)SmbiosTableOri
< 0xF0000)){
1594 // Relocate the Smibos memory
1596 BufferPtr
= EFI_SYSTEM_TABLE_MAX_ADDRESS
;
1597 if (SmbiosTableOri
->SmbiosBcdRevision
!= 0x21) {
1598 SmbiosEntryLen
= SmbiosTableOri
->EntryPointLength
;
1601 // According to Smbios Spec 2.4, we should set entry point length as 0x1F if version is 2.1
1603 SmbiosEntryLen
= 0x1F;
1605 BufferLen
= SmbiosEntryLen
+ SYS_TABLE_PAD(SmbiosEntryLen
) + SmbiosTableOri
->TableLength
;
1606 Status
= gBS
->AllocatePages (
1609 EFI_SIZE_TO_PAGES(BufferLen
),
1612 ASSERT_EFI_ERROR (Status
);
1613 SmbiosTableNew
= (SMBIOS_TABLE_ENTRY_POINT
*)(UINTN
)BufferPtr
;
1620 // Get Smbios Structure table address, and make sure the start address is 32-bit align
1622 BufferPtr
+= SmbiosEntryLen
+ SYS_TABLE_PAD(SmbiosEntryLen
);
1624 (VOID
*)(UINTN
)BufferPtr
,
1625 (VOID
*)(UINTN
)(SmbiosTableOri
->TableAddress
),
1626 SmbiosTableOri
->TableLength
1628 SmbiosTableNew
->TableAddress
= (UINT32
)BufferPtr
;
1629 SmbiosTableNew
->IntermediateChecksum
= 0;
1630 SmbiosTableNew
->IntermediateChecksum
=
1631 GetBufferCheckSum ((UINT8
*)SmbiosTableNew
+ 0x10, SmbiosEntryLen
-0x10);
1633 // Change the SMBIOS pointer
1635 *Table
= SmbiosTableNew
;
1646 Routine Description:
1648 Convert MP Table if the Location of the SMBios Table is lower than Addres 0x100000
1650 As in legacy Bios, MP table is required to place in E/F Seg,
1651 So here we just check if the range is E/F seg,
1652 and if Not, assume the Memory type is EfiACPIMemoryNVS/EfiRuntimeServicesData
1654 Table - pointer to the table
1657 EFI_SUCEESS - Convert Table successfully
1664 EFI_LEGACY_MP_TABLE_FLOATING_POINTER
*MpsFloatingPointerOri
;
1665 EFI_LEGACY_MP_TABLE_FLOATING_POINTER
*MpsFloatingPointerNew
;
1666 EFI_LEGACY_MP_TABLE_HEADER
*MpsTableOri
;
1667 EFI_LEGACY_MP_TABLE_HEADER
*MpsTableNew
;
1671 EFI_PHYSICAL_ADDRESS BufferPtr
;
1674 // Get MP configuration Table
1676 MpsFloatingPointerOri
= (EFI_LEGACY_MP_TABLE_FLOATING_POINTER
*)(UINTN
)(*(UINT64
*)(*Table
));
1677 if (!(((UINTN
)MpsFloatingPointerOri
<= 0x100000) &&
1678 ((UINTN
)MpsFloatingPointerOri
>= 0xF0000))){
1682 // Get Floating pointer structure length
1684 FPLength
= MpsFloatingPointerOri
->Length
* 16;
1685 Data32
= FPLength
+ SYS_TABLE_PAD (FPLength
);
1686 MpsTableOri
= (EFI_LEGACY_MP_TABLE_HEADER
*)(UINTN
)(MpsFloatingPointerOri
->PhysicalAddress
);
1687 if (MpsTableOri
!= NULL
) {
1688 Data32
+= MpsTableOri
->BaseTableLength
;
1689 Data32
+= MpsTableOri
->ExtendedTableLength
;
1690 if (MpsTableOri
->OemTablePointer
!= 0x00) {
1691 Data32
+= SYS_TABLE_PAD (Data32
);
1692 Data32
+= MpsTableOri
->OemTableSize
;
1700 BufferPtr
= EFI_SYSTEM_TABLE_MAX_ADDRESS
;
1701 Status
= gBS
->AllocatePages (
1704 EFI_SIZE_TO_PAGES(Data32
),
1707 ASSERT_EFI_ERROR (Status
);
1708 MpsFloatingPointerNew
= (EFI_LEGACY_MP_TABLE_FLOATING_POINTER
*)(UINTN
)BufferPtr
;
1709 CopyMem (MpsFloatingPointerNew
, MpsFloatingPointerOri
, FPLength
);
1711 // If Mp Table exists
1713 if (MpsTableOri
!= NULL
) {
1715 // Get Mps table length, including Ext table
1717 BufferPtr
= BufferPtr
+ FPLength
+ SYS_TABLE_PAD (FPLength
);
1718 MpsTableNew
= (EFI_LEGACY_MP_TABLE_HEADER
*)(UINTN
)BufferPtr
;
1719 CopyMem (MpsTableNew
, MpsTableOri
, MpsTableOri
->BaseTableLength
+ MpsTableOri
->ExtendedTableLength
);
1721 if ((MpsTableOri
->OemTableSize
!= 0x0000) && (MpsTableOri
->OemTablePointer
!= 0x0000)){
1722 BufferPtr
+= MpsTableOri
->BaseTableLength
+ MpsTableOri
->ExtendedTableLength
;
1723 BufferPtr
+= SYS_TABLE_PAD (BufferPtr
);
1724 OemTableNew
= (VOID
*)(UINTN
)BufferPtr
;
1725 OemTableOri
= (VOID
*)(UINTN
)MpsTableOri
->OemTablePointer
;
1726 CopyMem (OemTableNew
, OemTableOri
, MpsTableOri
->OemTableSize
);
1727 MpsTableNew
->OemTablePointer
= (UINT32
)(UINTN
)OemTableNew
;
1729 MpsTableNew
->Checksum
= 0;
1730 MpsTableNew
->Checksum
= GetBufferCheckSum (MpsTableNew
, MpsTableOri
->BaseTableLength
);
1731 MpsFloatingPointerNew
->PhysicalAddress
= (UINT32
)(UINTN
)MpsTableNew
;
1732 MpsFloatingPointerNew
->Checksum
= 0;
1733 MpsFloatingPointerNew
->Checksum
= GetBufferCheckSum (MpsFloatingPointerNew
, FPLength
);
1736 // Change the pointer
1738 *Table
= MpsFloatingPointerNew
;