--- /dev/null
+/** @file\r
+\r
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include "LegacyBiosInterface.h"\r
+#include <IndustryStandard/Pci.h>\r
+\r
+// Give floppy 3 states\r
+// FLOPPY_PRESENT_WITH_MEDIA = Floppy controller present and media is inserted\r
+// FLOPPY_NOT_PRESENT = No floppy controller present\r
+// FLOPPY_PRESENT_NO_MEDIA = Floppy controller present but no media inserted\r
+//\r
+#define FLOPPY_NOT_PRESENT 0\r
+#define FLOPPY_PRESENT_WITH_MEDIA 1\r
+#define FLOPPY_PRESENT_NO_MEDIA 2\r
+\r
+BBS_TABLE *mBbsTable;\r
+BOOLEAN mBbsTableDoneFlag = FALSE;\r
+BOOLEAN IsHaveMediaInFloppy = TRUE;\r
+\r
+/**\r
+ Checks the state of the floppy and if media is inserted.\r
+\r
+ This routine checks the state of the floppy and if media is inserted.\r
+ There are 3 cases:\r
+ No floppy present - Set BBS entry to ignore\r
+ Floppy present & no media - Set BBS entry to lowest priority. We cannot\r
+ set it to ignore since 16-bit CSM will\r
+ indicate no floppy and thus drive A: is\r
+ unusable. CSM-16 will not try floppy since\r
+ lowest priority and thus not incur boot\r
+ time penality.\r
+ Floppy present & media - Set BBS entry to some priority.\r
+\r
+ @return State of floppy media\r
+\r
+**/\r
+UINT8\r
+HasMediaInFloppy (\r
+ VOID\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINTN HandleCount;\r
+ EFI_HANDLE *HandleBuffer;\r
+ UINTN Index;\r
+ EFI_ISA_IO_PROTOCOL *IsaIo;\r
+ EFI_BLOCK_IO_PROTOCOL *BlkIo;\r
+\r
+ HandleBuffer = NULL;\r
+ HandleCount = 0;\r
+\r
+ gBS->LocateHandleBuffer (\r
+ ByProtocol,\r
+ &gEfiIsaIoProtocolGuid,\r
+ NULL,\r
+ &HandleCount,\r
+ &HandleBuffer\r
+ );\r
+\r
+ //\r
+ // If don't find any ISA/IO protocol assume no floppy. Need for floppy\r
+ // free system\r
+ //\r
+ if (HandleCount == 0) {\r
+ return FLOPPY_NOT_PRESENT;\r
+ }\r
+\r
+ ASSERT (HandleBuffer != NULL);\r
+\r
+ for (Index = 0; Index < HandleCount; Index++) {\r
+ Status = gBS->HandleProtocol (\r
+ HandleBuffer[Index],\r
+ &gEfiIsaIoProtocolGuid,\r
+ (VOID **) &IsaIo\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ continue;\r
+ }\r
+\r
+ if (IsaIo->ResourceList->Device.HID != EISA_PNP_ID (0x604)) {\r
+ continue;\r
+ }\r
+ //\r
+ // Update blockio in case the floppy is inserted in during BdsTimeout\r
+ //\r
+ Status = gBS->DisconnectController (HandleBuffer[Index], NULL, NULL);\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ continue;\r
+ }\r
+\r
+ Status = gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE);\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ continue;\r
+ }\r
+\r
+ Status = gBS->HandleProtocol (\r
+ HandleBuffer[Index],\r
+ &gEfiBlockIoProtocolGuid,\r
+ (VOID **) &BlkIo\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ continue;\r
+ }\r
+\r
+ if (BlkIo->Media->MediaPresent) {\r
+ FreePool (HandleBuffer);\r
+ return FLOPPY_PRESENT_WITH_MEDIA;\r
+ } else {\r
+ FreePool (HandleBuffer);\r
+ return FLOPPY_PRESENT_NO_MEDIA;\r
+ }\r
+ }\r
+\r
+ FreePool (HandleBuffer);\r
+\r
+ return FLOPPY_NOT_PRESENT;\r
+\r
+}\r
+\r
+\r
+/**\r
+ Complete build of BBS TABLE.\r
+\r
+ @param Private Legacy BIOS Instance data\r
+ @param BbsTable BBS Table passed to 16-bit code\r
+\r
+ @retval EFI_SUCCESS Removable media not present\r
+\r
+**/\r
+EFI_STATUS\r
+LegacyBiosBuildBbs (\r
+ IN LEGACY_BIOS_INSTANCE *Private,\r
+ IN BBS_TABLE *BbsTable\r
+ )\r
+{\r
+ UINTN BbsIndex;\r
+ HDD_INFO *HddInfo;\r
+ UINTN HddIndex;\r
+ UINTN Index;\r
+\r
+ //\r
+ // First entry is floppy.\r
+ // Next 2*MAX_IDE_CONTROLLER entries are for onboard IDE.\r
+ // Next n entries are filled in after each ROM is dispatched.\r
+ // Entry filled in if follow BBS spec. See LegacyPci.c\r
+ // Next entries are for non-BBS compliant ROMS. They are filled in by\r
+ // 16-bit code during Legacy16UpdateBbs invocation. Final BootPriority\r
+ // occurs after that invocation.\r
+ //\r
+ // Floppy\r
+ // Set default state.\r
+ //\r
+ IsHaveMediaInFloppy = HasMediaInFloppy ();\r
+ if (IsHaveMediaInFloppy == FLOPPY_PRESENT_WITH_MEDIA) {\r
+ BbsTable[0].BootPriority = BBS_UNPRIORITIZED_ENTRY;\r
+ } else {\r
+ if (IsHaveMediaInFloppy == FLOPPY_PRESENT_NO_MEDIA) {\r
+ BbsTable[0].BootPriority = BBS_LOWEST_PRIORITY;\r
+ } else {\r
+ BbsTable[0].BootPriority = BBS_IGNORE_ENTRY;\r
+ }\r
+ }\r
+\r
+ BbsTable[0].Bus = 0xff;\r
+ BbsTable[0].Device = 0xff;\r
+ BbsTable[0].Function = 0xff;\r
+ BbsTable[0].DeviceType = BBS_FLOPPY;\r
+ BbsTable[0].Class = 01;\r
+ BbsTable[0].SubClass = 02;\r
+ BbsTable[0].StatusFlags.OldPosition = 0;\r
+ BbsTable[0].StatusFlags.Reserved1 = 0;\r
+ BbsTable[0].StatusFlags.Enabled = 0;\r
+ BbsTable[0].StatusFlags.Failed = 0;\r
+ BbsTable[0].StatusFlags.MediaPresent = 0;\r
+ BbsTable[0].StatusFlags.Reserved2 = 0;\r
+\r
+ //\r
+ // Onboard HDD - Note Each HDD controller controls 2 drives\r
+ // Master & Slave\r
+ //\r
+ HddInfo = &Private->IntThunk->EfiToLegacy16BootTable.HddInfo[0];\r
+ //\r
+ // Get IDE Drive Info\r
+ //\r
+ LegacyBiosBuildIdeData (Private, &HddInfo, 0);\r
+\r
+ for (HddIndex = 0; HddIndex < MAX_IDE_CONTROLLER; HddIndex++) {\r
+\r
+ BbsIndex = HddIndex * 2 + 1;\r
+ for (Index = 0; Index < 2; ++Index) {\r
+\r
+ BbsTable[BbsIndex + Index].Bus = HddInfo[HddIndex].Bus;\r
+ BbsTable[BbsIndex + Index].Device = HddInfo[HddIndex].Device;\r
+ BbsTable[BbsIndex + Index].Function = HddInfo[HddIndex].Function;\r
+ BbsTable[BbsIndex + Index].Class = 01;\r
+ BbsTable[BbsIndex + Index].SubClass = 01;\r
+ BbsTable[BbsIndex + Index].StatusFlags.OldPosition = 0;\r
+ BbsTable[BbsIndex + Index].StatusFlags.Reserved1 = 0;\r
+ BbsTable[BbsIndex + Index].StatusFlags.Enabled = 0;\r
+ BbsTable[BbsIndex + Index].StatusFlags.Failed = 0;\r
+ BbsTable[BbsIndex + Index].StatusFlags.MediaPresent = 0;\r
+ BbsTable[BbsIndex + Index].StatusFlags.Reserved2 = 0;\r
+\r
+ //\r
+ // If no controller found or no device found set to ignore\r
+ // else set to unprioritized and set device type\r
+ //\r
+ if (HddInfo[HddIndex].CommandBaseAddress == 0) {\r
+ BbsTable[BbsIndex + Index].BootPriority = BBS_IGNORE_ENTRY;\r
+ } else {\r
+ if (Index == 0) {\r
+ if ((HddInfo[HddIndex].Status & (HDD_MASTER_IDE | HDD_MASTER_ATAPI_CDROM | HDD_MASTER_ATAPI_ZIPDISK)) != 0) {\r
+ BbsTable[BbsIndex + Index].BootPriority = BBS_UNPRIORITIZED_ENTRY;\r
+ if ((HddInfo[HddIndex].Status & HDD_MASTER_IDE) != 0) {\r
+ BbsTable[BbsIndex + Index].DeviceType = BBS_HARDDISK;\r
+ } else if ((HddInfo[HddIndex].Status & HDD_MASTER_ATAPI_CDROM) != 0) {\r
+ BbsTable[BbsIndex + Index].DeviceType = BBS_CDROM;\r
+ } else {\r
+ //\r
+ // for ZIPDISK\r
+ //\r
+ BbsTable[BbsIndex + Index].DeviceType = BBS_HARDDISK;\r
+ }\r
+ } else {\r
+ BbsTable[BbsIndex + Index].BootPriority = BBS_IGNORE_ENTRY;\r
+ }\r
+ } else {\r
+ if ((HddInfo[HddIndex].Status & (HDD_SLAVE_IDE | HDD_SLAVE_ATAPI_CDROM | HDD_SLAVE_ATAPI_ZIPDISK)) != 0) {\r
+ BbsTable[BbsIndex + Index].BootPriority = BBS_UNPRIORITIZED_ENTRY;\r
+ if ((HddInfo[HddIndex].Status & HDD_SLAVE_IDE) != 0) {\r
+ BbsTable[BbsIndex + Index].DeviceType = BBS_HARDDISK;\r
+ } else if ((HddInfo[HddIndex].Status & HDD_SLAVE_ATAPI_CDROM) != 0) {\r
+ BbsTable[BbsIndex + Index].DeviceType = BBS_CDROM;\r
+ } else {\r
+ //\r
+ // for ZIPDISK\r
+ //\r
+ BbsTable[BbsIndex + Index].DeviceType = BBS_HARDDISK;\r
+ }\r
+ } else {\r
+ BbsTable[BbsIndex + Index].BootPriority = BBS_IGNORE_ENTRY;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+\r
+}\r
+\r
+\r
+/**\r
+ Get all BBS info\r
+\r
+ @param This Protocol instance pointer.\r
+ @param HddCount Number of HDD_INFO structures\r
+ @param HddInfo Onboard IDE controller information\r
+ @param BbsCount Number of BBS_TABLE structures\r
+ @param BbsTable List BBS entries\r
+\r
+ @retval EFI_SUCCESS Tables returned\r
+ @retval EFI_NOT_FOUND resource not found\r
+ @retval EFI_DEVICE_ERROR can not get BBS table\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+LegacyBiosGetBbsInfo (\r
+ IN EFI_LEGACY_BIOS_PROTOCOL *This,\r
+ OUT UINT16 *HddCount,\r
+ OUT HDD_INFO **HddInfo,\r
+ OUT UINT16 *BbsCount,\r
+ OUT BBS_TABLE **BbsTable\r
+ )\r
+{\r
+ LEGACY_BIOS_INSTANCE *Private;\r
+ EFI_IA32_REGISTER_SET Regs;\r
+ EFI_TO_COMPATIBILITY16_BOOT_TABLE *EfiToLegacy16BootTable;\r
+// HDD_INFO *LocalHddInfo;\r
+// IN BBS_TABLE *LocalBbsTable;\r
+ UINTN NumHandles;\r
+ EFI_HANDLE *HandleBuffer;\r
+ UINTN Index;\r
+ UINTN TempData;\r
+ UINT32 Granularity;\r
+\r
+ HandleBuffer = NULL;\r
+\r
+ Private = LEGACY_BIOS_INSTANCE_FROM_THIS (This);\r
+ EfiToLegacy16BootTable = &Private->IntThunk->EfiToLegacy16BootTable;\r
+// LocalHddInfo = EfiToLegacy16BootTable->HddInfo;\r
+// LocalBbsTable = (BBS_TABLE*)(UINTN)EfiToLegacy16BootTable->BbsTable;\r
+\r
+ if (!mBbsTableDoneFlag) {\r
+ mBbsTable = Private->BbsTablePtr;\r
+\r
+ //\r
+ // Always enable disk controllers so 16-bit CSM code has valid information for all\r
+ // drives.\r
+ //\r
+ //\r
+ // Get PciRootBridgeIO protocol\r
+ //\r
+ gBS->LocateHandleBuffer (\r
+ ByProtocol,\r
+ &gEfiPciRootBridgeIoProtocolGuid,\r
+ NULL,\r
+ &NumHandles,\r
+ &HandleBuffer\r
+ );\r
+\r
+ if (NumHandles == 0) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ mBbsTableDoneFlag = TRUE;\r
+ for (Index = 0; Index < NumHandles; Index++) {\r
+ //\r
+ // Connect PciRootBridgeIO protocol handle with FALSE parameter to let\r
+ // PCI bus driver enumerate all subsequent handles\r
+ //\r
+ gBS->ConnectController (HandleBuffer[Index], NULL, NULL, FALSE);\r
+\r
+ }\r
+\r
+ LegacyBiosBuildBbs (Private, mBbsTable);\r
+\r
+ Private->LegacyRegion->UnLock (Private->LegacyRegion, 0xe0000, 0x20000, &Granularity);\r
+\r
+ //\r
+ // Call into Legacy16 code to add to BBS table for non BBS compliant OPROMs.\r
+ //\r
+ ZeroMem (&Regs, sizeof (EFI_IA32_REGISTER_SET));\r
+ Regs.X.AX = Legacy16UpdateBbs;\r
+\r
+ //\r
+ // Pass in handoff data\r
+ //\r
+ TempData = (UINTN) EfiToLegacy16BootTable;\r
+ Regs.X.ES = NORMALIZE_EFI_SEGMENT ((UINT32) TempData);\r
+ Regs.X.BX = NORMALIZE_EFI_OFFSET ((UINT32) TempData);\r
+\r
+ Private->LegacyBios.FarCall86 (\r
+ This,\r
+ Private->Legacy16CallSegment,\r
+ Private->Legacy16CallOffset,\r
+ &Regs,\r
+ NULL,\r
+ 0\r
+ );\r
+\r
+ Private->Cpu->FlushDataCache (Private->Cpu, 0xE0000, 0x20000, EfiCpuFlushTypeWriteBackInvalidate);\r
+ Private->LegacyRegion->Lock (Private->LegacyRegion, 0xe0000, 0x20000, &Granularity);\r
+\r
+ if (Regs.X.AX != 0) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+ }\r
+\r
+ if (HandleBuffer != NULL) {\r
+ FreePool (HandleBuffer);\r
+ }\r
+\r
+ *HddCount = MAX_IDE_CONTROLLER;\r
+ *HddInfo = EfiToLegacy16BootTable->HddInfo;\r
+ *BbsTable = (BBS_TABLE*)(UINTN)EfiToLegacy16BootTable->BbsTable;\r
+ *BbsCount = (UINT16) (sizeof (Private->IntThunk->BbsTable) / sizeof (BBS_TABLE));\r
+ return EFI_SUCCESS;\r
+}\r