3 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions
7 of the BSD License which accompanies this distribution. The
8 full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "LegacyBiosInterface.h"
17 #include <IndustryStandard/Pci.h>
19 #define BOOT_LEGACY_OS 0
21 #define BOOT_UNCONVENTIONAL_DEVICE 2
23 UINT32 mLoadOptionsSize
= 0;
24 UINTN mBootMode
= BOOT_LEGACY_OS
;
25 VOID
*mLoadOptions
= NULL
;
26 BBS_BBS_DEVICE_PATH
*mBbsDevicePathPtr
= NULL
;
27 BBS_BBS_DEVICE_PATH mBbsDevicePathNode
;
28 UDC_ATTRIBUTES mAttributes
= { 0, 0, 0, 0 };
30 VOID
*mBeerData
= NULL
;
31 VOID
*mServiceAreaData
= NULL
;
32 UINT64 mLowWater
= 0xffffffffffffffffULL
;
34 extern BBS_TABLE
*mBbsTable
;
39 @param BbsTable The BBS table.
45 IN BBS_TABLE
*BbsTable
52 DEBUG ((EFI_D_INFO
, "\n"));
53 DEBUG ((EFI_D_INFO
, " NO Prio bb/dd/ff cl/sc Type Stat segm:offs mfgs:mfgo dess:deso\n"));
54 DEBUG ((EFI_D_INFO
, "=================================================================\n"));
55 for (Index
= 0; Index
< MAX_BBS_ENTRIES
; Index
++) {
59 if (BbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) {
65 " %02x: %04x %02x/%02x/%02x %02x/%02x %04x %04x",
67 (UINTN
) BbsTable
[Index
].BootPriority
,
68 (UINTN
) BbsTable
[Index
].Bus
,
69 (UINTN
) BbsTable
[Index
].Device
,
70 (UINTN
) BbsTable
[Index
].Function
,
71 (UINTN
) BbsTable
[Index
].Class
,
72 (UINTN
) BbsTable
[Index
].SubClass
,
73 (UINTN
) BbsTable
[Index
].DeviceType
,
74 (UINTN
) * (UINT16
*) &BbsTable
[Index
].StatusFlags
78 " %04x:%04x %04x:%04x %04x:%04x",
79 (UINTN
) BbsTable
[Index
].BootHandlerSegment
,
80 (UINTN
) BbsTable
[Index
].BootHandlerOffset
,
81 (UINTN
) BbsTable
[Index
].MfgStringSegment
,
82 (UINTN
) BbsTable
[Index
].MfgStringOffset
,
83 (UINTN
) BbsTable
[Index
].DescStringSegment
,
84 (UINTN
) BbsTable
[Index
].DescStringOffset
90 String
= (CHAR8
*)(UINTN
)((BbsTable
[Index
].DescStringSegment
<< 4) + BbsTable
[Index
].DescStringOffset
);
92 DEBUG ((EFI_D_INFO
," ("));
93 for (SubIndex
= 0; String
[SubIndex
] != 0; SubIndex
++) {
94 DEBUG ((EFI_D_INFO
, "%c", String
[SubIndex
]));
96 DEBUG ((EFI_D_INFO
,")"));
98 DEBUG ((EFI_D_INFO
,"\n"));
101 DEBUG ((EFI_D_INFO
, "\n"));
109 @param HddInfo The HddInfo table.
120 DEBUG ((EFI_D_INFO
, "\n"));
121 for (Index
= 0; Index
< MAX_IDE_CONTROLLER
; Index
++) {
122 DEBUG ((EFI_D_INFO
, "Index - %04x\n", Index
));
123 DEBUG ((EFI_D_INFO
, " Status - %04x\n", (UINTN
)HddInfo
[Index
].Status
));
124 DEBUG ((EFI_D_INFO
, " B/D/F - %02x/%02x/%02x\n", (UINTN
)HddInfo
[Index
].Bus
, (UINTN
)HddInfo
[Index
].Device
, (UINTN
)HddInfo
[Index
].Function
));
125 DEBUG ((EFI_D_INFO
, " Command - %04x\n", HddInfo
[Index
].CommandBaseAddress
));
126 DEBUG ((EFI_D_INFO
, " Control - %04x\n", HddInfo
[Index
].ControlBaseAddress
));
127 DEBUG ((EFI_D_INFO
, " BusMaster - %04x\n", HddInfo
[Index
].BusMasterAddress
));
128 DEBUG ((EFI_D_INFO
, " HddIrq - %02x\n", HddInfo
[Index
].HddIrq
));
129 DEBUG ((EFI_D_INFO
, " IdentifyDrive[0].Raw[0] - %x\n", HddInfo
[Index
].IdentifyDrive
[0].Raw
[0]));
130 DEBUG ((EFI_D_INFO
, " IdentifyDrive[1].Raw[0] - %x\n", HddInfo
[Index
].IdentifyDrive
[1].Raw
[0]));
133 DEBUG ((EFI_D_INFO
, "\n"));
139 Print the PCI Interrupt Line and Interrupt Pin registers.
142 PrintPciInterruptRegister (
150 EFI_PCI_IO_PROTOCOL
*PciIo
;
157 gBS
->LocateHandleBuffer (
159 &gEfiPciIoProtocolGuid
,
169 DEBUG ((EFI_D_INFO
, "\n"));
170 DEBUG ((EFI_D_INFO
, " bb/dd/ff interrupt line interrupt pin\n"));
171 DEBUG ((EFI_D_INFO
, "======================================\n"));
172 for (Index
= 0; Index
< HandleNum
; Index
++) {
173 Status
= gBS
->HandleProtocol (Handles
[Index
], &gEfiPciIoProtocolGuid
, (VOID
**) &PciIo
);
174 if (!EFI_ERROR (Status
)) {
175 Status
= PciIo
->Pci
.Read (
183 if (!EFI_ERROR (Status
)) {
184 Status
= PciIo
->GetLocation (
192 if (!EFI_ERROR (Status
)) {
193 DEBUG ((EFI_D_INFO
, " %02x/%02x/%02x 0x%02x 0x%02x\n",
194 Bus
, Device
, Function
, Interrupt
[0], Interrupt
[1]));
197 DEBUG ((EFI_D_INFO
, "\n"));
199 if (Handles
!= NULL
) {
205 Identify drive data must be updated to actual parameters before boot.
207 @param IdentifyDriveData ATA Identify Data
211 UpdateIdentifyDriveData (
212 IN UINT8
*IdentifyDriveData
218 @param Private Legacy BIOS Instance data
220 @retval EFI_SUCCESS Removable media not present
225 IN LEGACY_BIOS_INSTANCE
*Private
231 UINT8 LegacyInterrupts
[16];
232 EFI_LEGACY_IRQ_ROUTING_ENTRY
*RoutingTable
;
233 UINTN RoutingTableEntries
;
234 EFI_LEGACY_IRQ_PRIORITY_TABLE_ENTRY
*IrqPriorityTable
;
235 UINTN NumberPriorityEntries
;
236 EFI_TO_COMPATIBILITY16_BOOT_TABLE
*EfiToLegacy16BootTable
;
242 EFI_HANDLE
*HandleBuffer
;
243 EFI_ISA_IO_PROTOCOL
*IsaIo
;
248 EfiToLegacy16BootTable
= &Private
->IntThunk
->EfiToLegacy16BootTable
;
249 LegacyBiosBuildSioData (Private
);
250 SetMem (LegacyInterrupts
, sizeof (LegacyInterrupts
), 0);
253 // Create list of legacy interrupts.
255 for (Index
= 0; Index
< 4; Index
++) {
256 LegacyInterrupts
[Index
] = EfiToLegacy16BootTable
->SioData
.Serial
[Index
].Irq
;
259 for (Index
= 4; Index
< 7; Index
++) {
260 LegacyInterrupts
[Index
] = EfiToLegacy16BootTable
->SioData
.Parallel
[Index
- 4].Irq
;
263 LegacyInterrupts
[7] = EfiToLegacy16BootTable
->SioData
.Floppy
.Irq
;
266 // Get Legacy Hdd IRQs. If native mode treat as PCI
268 for (Index
= 0; Index
< 2; Index
++) {
269 HddIrq
= EfiToLegacy16BootTable
->HddInfo
[Index
].HddIrq
;
270 if ((HddIrq
!= 0) && ((HddIrq
== 15) || (HddIrq
== 14))) {
271 LegacyInterrupts
[Index
+ 8] = HddIrq
;
275 Private
->LegacyBiosPlatform
->GetRoutingTable (
276 Private
->LegacyBiosPlatform
,
277 (VOID
*) &RoutingTable
,
278 &RoutingTableEntries
,
281 (VOID
**) &IrqPriorityTable
,
282 &NumberPriorityEntries
285 // Remove legacy interrupts from the list of PCI interrupts available.
287 for (Index
= 0; Index
<= 0x0b; Index
++) {
288 for (Index1
= 0; Index1
<= NumberPriorityEntries
; Index1
++) {
289 if (LegacyInterrupts
[Index
] != 0) {
290 LegacyInt
= (UINT16
) (LegacyInt
| (1 << LegacyInterrupts
[Index
]));
291 if (LegacyInterrupts
[Index
] == IrqPriorityTable
[Index1
].Irq
) {
292 IrqPriorityTable
[Index1
].Used
= LEGACY_USED
;
298 Private
->Legacy8259
->GetMask (
307 // Set SIO interrupts and disable mouse. Let mouse driver
310 LegMask
= (UINT16
) ((LegMask
&~LegacyInt
) | 0x1000);
311 Private
->Legacy8259
->SetMask (
320 // Disable mouse in keyboard controller
323 Status
= gBS
->LocateHandleBuffer (
325 &gEfiIsaIoProtocolGuid
,
330 if (EFI_ERROR (Status
)) {
334 for (Index
= 0; Index
< HandleCount
; Index
++) {
335 Status
= gBS
->HandleProtocol (
337 &gEfiIsaIoProtocolGuid
,
340 ASSERT_EFI_ERROR (Status
);
341 IsaIo
->Io
.Write (IsaIo
, EfiIsaIoWidthUint8
, 0x64, 1, &Register
);
345 if (HandleBuffer
!= NULL
) {
346 FreePool (HandleBuffer
);
354 Identify drive data must be updated to actual parameters before boot.
355 This requires updating the checksum, if it exists.
357 @param IdentifyDriveData ATA Identify Data
358 @param Checksum checksum of the ATA Identify Data
360 @retval EFI_SUCCESS checksum calculated
361 @retval EFI_SECURITY_VIOLATION IdentifyData invalid
365 CalculateIdentifyDriveChecksum (
366 IN UINT8
*IdentifyDriveData
,
374 if (IdentifyDriveData
[510] != 0xA5) {
375 return EFI_SECURITY_VIOLATION
;
378 for (Index
= 0; Index
< 512; Index
++) {
379 LocalChecksum
= (UINT8
) (LocalChecksum
+ IdentifyDriveData
[Index
]);
382 *Checksum
= LocalChecksum
;
388 Identify drive data must be updated to actual parameters before boot.
390 @param IdentifyDriveData ATA Identify Data
395 UpdateIdentifyDriveData (
396 IN UINT8
*IdentifyDriveData
399 UINT16 NumberCylinders
;
401 UINT16 NumberSectorsTrack
;
402 UINT32 CapacityInSectors
;
403 UINT8 OriginalChecksum
;
406 ATAPI_IDENTIFY
*ReadInfo
;
409 // Status indicates if Integrity byte is correct. Checksum should be
412 ReadInfo
= (ATAPI_IDENTIFY
*) IdentifyDriveData
;
413 Status
= CalculateIdentifyDriveChecksum (IdentifyDriveData
, &OriginalChecksum
);
414 if (OriginalChecksum
!= 0) {
415 Status
= EFI_SECURITY_VIOLATION
;
418 // If NumberCylinders = 0 then do data(Controller present but don drive attached).
420 NumberCylinders
= ReadInfo
->Raw
[1];
421 if (NumberCylinders
!= 0) {
422 ReadInfo
->Raw
[54] = NumberCylinders
;
424 NumberHeads
= ReadInfo
->Raw
[3];
425 ReadInfo
->Raw
[55] = NumberHeads
;
427 NumberSectorsTrack
= ReadInfo
->Raw
[6];
428 ReadInfo
->Raw
[56] = NumberSectorsTrack
;
431 // Copy Multisector info and set valid bit.
433 ReadInfo
->Raw
[59] = (UINT16
) (ReadInfo
->Raw
[47] + 0x100);
434 CapacityInSectors
= (UINT32
) ((UINT32
) (NumberCylinders
) * (UINT32
) (NumberHeads
) * (UINT32
) (NumberSectorsTrack
));
435 ReadInfo
->Raw
[57] = (UINT16
) (CapacityInSectors
>> 16);
436 ReadInfo
->Raw
[58] = (UINT16
) (CapacityInSectors
& 0xffff);
437 if (Status
== EFI_SUCCESS
) {
439 // Forece checksum byte to 0 and get new checksum.
441 ReadInfo
->Raw
[255] &= 0xff;
442 CalculateIdentifyDriveChecksum (IdentifyDriveData
, &FinalChecksum
);
445 // Force new checksum such that sum is 0.
447 FinalChecksum
= (UINT8
) ((UINT8
)0 - FinalChecksum
);
448 ReadInfo
->Raw
[255] = (UINT16
) (ReadInfo
->Raw
[255] | (FinalChecksum
<< 8));
454 Identify drive data must be updated to actual parameters before boot.
457 @param Private Legacy BIOS Instance data
462 UpdateAllIdentifyDriveData (
463 IN LEGACY_BIOS_INSTANCE
*Private
469 HddInfo
= &Private
->IntThunk
->EfiToLegacy16BootTable
.HddInfo
[0];
471 for (Index
= 0; Index
< MAX_IDE_CONTROLLER
; Index
++) {
473 // Each controller can have 2 devices. Update for each device
475 if ((HddInfo
[Index
].Status
& HDD_MASTER_IDE
) != 0) {
476 UpdateIdentifyDriveData ((UINT8
*) (&HddInfo
[Index
].IdentifyDrive
[0].Raw
[0]));
479 if ((HddInfo
[Index
].Status
& HDD_SLAVE_IDE
) != 0) {
480 UpdateIdentifyDriveData ((UINT8
*) (&HddInfo
[Index
].IdentifyDrive
[1].Raw
[0]));
486 Enable ide controller. This gets disabled when LegacyBoot.c is about
487 to run the Option ROMs.
489 @param Private Legacy BIOS Instance data
494 EnableIdeController (
495 IN LEGACY_BIOS_INSTANCE
*Private
498 EFI_PCI_IO_PROTOCOL
*PciIo
;
500 EFI_HANDLE IdeController
;
503 EFI_HANDLE
*HandleBuffer
;
505 Status
= Private
->LegacyBiosPlatform
->GetPlatformHandle (
506 Private
->LegacyBiosPlatform
,
507 EfiGetPlatformIdeHandle
,
513 if (!EFI_ERROR (Status
)) {
514 IdeController
= HandleBuffer
[0];
515 Status
= gBS
->HandleProtocol (
517 &gEfiPciIoProtocolGuid
,
521 if (!EFI_ERROR (Status
)) {
522 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint8
, 0x04, 1, &ByteBuffer
);
529 Enable ide controller. This gets disabled when LegacyBoot.c is about
530 to run the Option ROMs.
532 @param Private Legacy BIOS Instance data
537 EnableAllControllers (
538 IN LEGACY_BIOS_INSTANCE
*Private
542 EFI_HANDLE
*HandleBuffer
;
544 EFI_PCI_IO_PROTOCOL
*PciIo
;
545 PCI_TYPE01 PciConfigHeader
;
551 EnableIdeController (Private
);
554 // Assumption is table is built from low bus to high bus numbers.
556 Status
= gBS
->LocateHandleBuffer (
558 &gEfiPciIoProtocolGuid
,
563 ASSERT_EFI_ERROR (Status
);
565 for (Index
= 0; Index
< HandleCount
; Index
++) {
566 Status
= gBS
->HandleProtocol (
568 &gEfiPciIoProtocolGuid
,
571 ASSERT_EFI_ERROR (Status
);
577 sizeof (PciConfigHeader
) / sizeof (UINT32
),
582 // We do not enable PPB here. This is for HotPlug Consideration.
583 // The Platform HotPlug Driver is responsible for Padding enough hot plug
584 // resources. It is also responsible for enable this bridge. If it
585 // does not pad it. It will cause some early Windows fail to installation.
586 // If the platform driver does not pad resource for PPB, PPB should be in
587 // un-enabled state to let Windows know that this PPB is not configured by
588 // BIOS. So Windows will allocate default resource for PPB.
590 // The reason for why we enable the command register is:
591 // The CSM will use the IO bar to detect some IRQ status, if the command
592 // is disabled, the IO resource will be out of scope.
594 // We installed a legacy IRQ handle for a PCI IDE controller. When IRQ
595 // comes up, the handle will check the IO space to identify is the
596 // controller generated the IRQ source.
597 // If the IO command is not enabled, the IRQ handler will has wrong
598 // information. It will cause IRQ storm when the correctly IRQ handler fails
601 if (!(IS_PCI_VGA (&PciConfigHeader
) ||
602 IS_PCI_OLD_VGA (&PciConfigHeader
) ||
603 IS_PCI_IDE (&PciConfigHeader
) ||
604 IS_PCI_P2P (&PciConfigHeader
) ||
605 IS_PCI_P2P_SUB (&PciConfigHeader
) ||
606 IS_PCI_LPC (&PciConfigHeader
) )) {
608 PciConfigHeader
.Hdr
.Command
|= 0x1f;
610 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, 4, 1, &PciConfigHeader
.Hdr
.Command
);
616 The following routines are identical in operation, so combine
618 EfiGetPlatformBinaryGetMpTable
619 EfiGetPlatformBinaryGetOemIntData
620 EfiGetPlatformBinaryGetOem32Data
621 EfiGetPlatformBinaryGetOem16Data
623 @param This Protocol instance pointer.
624 @param Id Table/Data identifier
626 @retval EFI_SUCCESS Success
627 @retval EFI_INVALID_PARAMETER Invalid ID
628 @retval EFI_OUT_OF_RESOURCES no resource to get data or table
632 LegacyGetDataOrTable (
633 IN EFI_LEGACY_BIOS_PROTOCOL
*This
,
634 IN EFI_GET_PLATFORM_INFO_MODE Id
643 EFI_LEGACY_BIOS_PLATFORM_PROTOCOL
*LegacyBiosPlatform
;
644 EFI_COMPATIBILITY16_TABLE
*Legacy16Table
;
645 EFI_IA32_REGISTER_SET Regs
;
646 LEGACY_BIOS_INSTANCE
*Private
;
648 Private
= LEGACY_BIOS_INSTANCE_FROM_THIS (This
);
650 LegacyBiosPlatform
= Private
->LegacyBiosPlatform
;
651 Legacy16Table
= Private
->Legacy16Table
;
654 // Phase 1 - get an address allocated in 16-bit code
658 case EfiGetPlatformBinaryMpTable
:
659 case EfiGetPlatformBinaryOemIntData
:
660 case EfiGetPlatformBinaryOem32Data
:
661 case EfiGetPlatformBinaryOem16Data
:
663 Status
= LegacyBiosPlatform
->GetPlatformInfo (
673 DEBUG ((EFI_D_INFO
, "LegacyGetDataOrTable - ID: %x, %r\n", (UINTN
)Id
, Status
));
674 DEBUG ((EFI_D_INFO
, " Table - %x, Size - %x, Location - %x, Alignment - %x\n", (UINTN
)Table
, (UINTN
)TableSize
, (UINTN
)Location
, (UINTN
)Alignment
));
680 return EFI_INVALID_PARAMETER
;
684 if (EFI_ERROR (Status
)) {
688 ZeroMem (&Regs
, sizeof (EFI_IA32_REGISTER_SET
));
689 Regs
.X
.AX
= Legacy16GetTableAddress
;
690 Regs
.X
.CX
= (UINT16
) TableSize
;
691 Regs
.X
.BX
= (UINT16
) Location
;
692 Regs
.X
.DX
= (UINT16
) Alignment
;
693 Private
->LegacyBios
.FarCall86 (
695 Private
->Legacy16CallSegment
,
696 Private
->Legacy16CallOffset
,
702 if (Regs
.X
.AX
!= 0) {
703 DEBUG ((EFI_D_ERROR
, "Table ID %x length insufficient\n", Id
));
704 return EFI_OUT_OF_RESOURCES
;
710 // Phase 2 Call routine second time with address to allow address adjustment
712 Status
= LegacyBiosPlatform
->GetPlatformInfo (
723 case EfiGetPlatformBinaryMpTable
:
725 Legacy16Table
->MpTablePtr
= (UINT32
) (Regs
.X
.DS
* 16 + Regs
.X
.BX
);
726 Legacy16Table
->MpTableLength
= (UINT32
)TableSize
;
727 DEBUG ((EFI_D_INFO
, "MP table in legacy region - %x\n", (UINTN
)Legacy16Table
->MpTablePtr
));
731 case EfiGetPlatformBinaryOemIntData
:
734 Legacy16Table
->OemIntSegment
= Regs
.X
.DS
;
735 Legacy16Table
->OemIntOffset
= Regs
.X
.BX
;
736 DEBUG ((EFI_D_INFO
, "OemInt table in legacy region - %04x:%04x\n", (UINTN
)Legacy16Table
->OemIntSegment
, (UINTN
)Legacy16Table
->OemIntOffset
));
740 case EfiGetPlatformBinaryOem32Data
:
742 Legacy16Table
->Oem32Segment
= Regs
.X
.DS
;
743 Legacy16Table
->Oem32Offset
= Regs
.X
.BX
;
744 DEBUG ((EFI_D_INFO
, "Oem32 table in legacy region - %04x:%04x\n", (UINTN
)Legacy16Table
->Oem32Segment
, (UINTN
)Legacy16Table
->Oem32Offset
));
748 case EfiGetPlatformBinaryOem16Data
:
751 // Legacy16Table->Oem16Segment = Regs.X.DS;
752 // Legacy16Table->Oem16Offset = Regs.X.BX;
753 DEBUG ((EFI_D_INFO
, "Oem16 table in legacy region - %04x:%04x\n", (UINTN
)Legacy16Table
->Oem16Segment
, (UINTN
)Legacy16Table
->Oem16Offset
));
759 return EFI_INVALID_PARAMETER
;
763 if (EFI_ERROR (Status
)) {
767 // Phase 3 Copy table to final location
769 TablePtr
= (UINT32
) (Regs
.X
.DS
* 16 + Regs
.X
.BX
);
772 (VOID
*) (UINTN
)TablePtr
,
782 Assign drive number to legacy HDD drives prior to booting an EFI
783 aware OS so the OS can access drives without an EFI driver.
784 Note: BBS compliant drives ARE NOT available until this call by
787 @param This Protocol instance pointer.
789 @retval EFI_SUCCESS Drive numbers assigned
794 IN EFI_LEGACY_BIOS_PROTOCOL
*This
798 LEGACY_BIOS_INSTANCE
*Private
;
799 EFI_IA32_REGISTER_SET Regs
;
800 EFI_TO_COMPATIBILITY16_BOOT_TABLE
*EfiToLegacy16BootTable
;
801 EFI_LEGACY_BIOS_PLATFORM_PROTOCOL
*LegacyBiosPlatform
;
805 HDD_INFO
*LocalHddInfo
;
807 EFI_COMPATIBILITY16_TABLE
*Legacy16Table
;
811 BBS_TABLE
*LocalBbsTable
;
812 UINT32
*BaseVectorMaster
;
815 EFI_HANDLE IdeController
;
817 EFI_HANDLE
*HandleBuffer
;
822 EFI_TIMER_ARCH_PROTOCOL
*Timer
;
828 LocalBbsTable
= NULL
;
831 Private
= LEGACY_BIOS_INSTANCE_FROM_THIS (This
);
833 DEBUG ((EFI_D_ERROR
, "Start of legacy boot\n"));
836 Legacy16Table
= Private
->Legacy16Table
;
837 EfiToLegacy16BootTable
= &Private
->IntThunk
->EfiToLegacy16BootTable
;
838 HddInfo
= &EfiToLegacy16BootTable
->HddInfo
[0];
840 LegacyBiosPlatform
= Private
->LegacyBiosPlatform
;
842 EfiToLegacy16BootTable
->MajorVersion
= EFI_TO_LEGACY_MAJOR_VERSION
;
843 EfiToLegacy16BootTable
->MinorVersion
= EFI_TO_LEGACY_MINOR_VERSION
;
846 // Before starting the Legacy boot check the system ticker.
848 Status
= gBS
->LocateProtocol (
849 &gEfiTimerArchProtocolGuid
,
853 if (EFI_ERROR (Status
)) {
857 Status
= Timer
->GetTimerPeriod (
861 if (EFI_ERROR (Status
)) {
865 if (TimerPeriod
!= DEFAULT_LAGACY_TIMER_TICK_DURATION
) {
866 Status
= Timer
->SetTimerPeriod (
868 DEFAULT_LAGACY_TIMER_TICK_DURATION
870 if (EFI_ERROR (Status
)) {
876 // If booting to a legacy OS then force HDD drives to the appropriate
877 // boot mode by calling GetIdeHandle.
878 // A reconnect -r can force all HDDs back to native mode.
880 IdeController
= NULL
;
881 if ((mBootMode
== BOOT_LEGACY_OS
) || (mBootMode
== BOOT_UNCONVENTIONAL_DEVICE
)) {
882 Status
= LegacyBiosPlatform
->GetPlatformHandle (
883 Private
->LegacyBiosPlatform
,
884 EfiGetPlatformIdeHandle
,
890 if (!EFI_ERROR (Status
)) {
891 IdeController
= HandleBuffer
[0];
895 // Unlock the Legacy BIOS region
897 Private
->LegacyRegion
->UnLock (
898 Private
->LegacyRegion
,
905 // Reconstruct the Legacy16 boot memory map
907 LegacyBiosBuildE820 (Private
, &CopySize
);
908 if (CopySize
> Private
->Legacy16Table
->E820Length
) {
909 ZeroMem (&Regs
, sizeof (EFI_IA32_REGISTER_SET
));
910 Regs
.X
.AX
= Legacy16GetTableAddress
;
911 Regs
.X
.CX
= (UINT16
) CopySize
;
912 Private
->LegacyBios
.FarCall86 (
913 &Private
->LegacyBios
,
914 Private
->Legacy16Table
->Compatibility16CallSegment
,
915 Private
->Legacy16Table
->Compatibility16CallOffset
,
921 Private
->Legacy16Table
->E820Pointer
= (UINT32
) (Regs
.X
.DS
* 16 + Regs
.X
.BX
);
922 Private
->Legacy16Table
->E820Length
= (UINT32
) CopySize
;
923 if (Regs
.X
.AX
!= 0) {
924 DEBUG ((EFI_D_ERROR
, "Legacy16 E820 length insufficient\n"));
927 (VOID
*)(UINTN
) Private
->Legacy16Table
->E820Pointer
,
934 (VOID
*)(UINTN
) Private
->Legacy16Table
->E820Pointer
,
938 Private
->Legacy16Table
->E820Length
= (UINT32
) CopySize
;
941 // Get SMBIOS and ACPI table pointers
944 EfiGetSystemConfigurationTable (
945 &gEfiSmbiosTableGuid
,
949 // We do not ASSERT if SmbiosTable not found. It is possbile that a platform does not produce SmbiosTable.
951 if (SmbiosTable
== NULL
) {
952 DEBUG ((EFI_D_INFO
, "Smbios table is not found!\n"));
954 EfiToLegacy16BootTable
->SmbiosTable
= (UINT32
)(UINTN
)SmbiosTable
;
957 Status
= EfiGetSystemConfigurationTable (
958 &gEfiAcpi20TableGuid
,
961 if (EFI_ERROR (Status
)) {
962 Status
= EfiGetSystemConfigurationTable (
963 &gEfiAcpi10TableGuid
,
968 // We do not ASSERT if AcpiTable not found. It is possbile that a platform does not produce AcpiTable.
970 if (AcpiTable
== NULL
) {
971 DEBUG ((EFI_D_INFO
, "ACPI table is not found!\n"));
973 EfiToLegacy16BootTable
->AcpiTable
= (UINT32
)(UINTN
)AcpiTable
;
976 // Get RSD Ptr table rev at offset 15 decimal
977 // Rev = 0 Length is 20 decimal
978 // Rev != 0 Length is UINT32 at offset 20 decimal
980 if (AcpiTable
!= NULL
) {
983 if (*((UINT8
*) AcpiPtr
+ 15) == 0) {
986 AcpiPtr
= ((UINT8
*) AcpiPtr
+ 20);
987 CopySize
= (*(UINT32
*) AcpiPtr
);
991 (VOID
*)(UINTN
) Private
->Legacy16Table
->AcpiRsdPtrPointer
,
997 // Make sure all PCI Interrupt Line register are programmed to match 8259
999 PciProgramAllInterruptLineRegisters (Private
);
1002 // Unlock the Legacy BIOS region as PciProgramAllInterruptLineRegisters
1005 Private
->LegacyRegion
->UnLock (
1006 Private
->LegacyRegion
,
1008 Private
->LegacyBiosImageSize
,
1013 // Configure Legacy Device Magic
1015 // Only do this code if booting legacy OS
1017 if ((mBootMode
== BOOT_LEGACY_OS
) || (mBootMode
== BOOT_UNCONVENTIONAL_DEVICE
)) {
1018 UpdateSioData (Private
);
1021 // Setup BDA and EBDA standard areas before Legacy Boot
1023 LegacyBiosCompleteBdaBeforeBoot (Private
);
1024 LegacyBiosCompleteStandardCmosBeforeBoot (Private
);
1027 // We must build IDE data, if it hasn't been done, before PciShadowRoms
1028 // to insure EFI drivers are connected.
1030 LegacyBiosBuildIdeData (Private
, &HddInfo
, 1);
1031 UpdateAllIdentifyDriveData (Private
);
1034 // Clear IO BAR, if IDE controller in legacy mode.
1036 InitLegacyIdeController (IdeController
);
1039 // Generate number of ticks since midnight for BDA. DOS requires this
1040 // for its time. We have to make assumptions as to how long following
1041 // code takes since after PciShadowRoms PciIo is gone. Place result in
1044 // Adjust value by 1 second.
1046 gRT
->GetTime (&BootTime
, NULL
);
1047 LocalTime
= BootTime
.Hour
* 3600 + BootTime
.Minute
* 60 + BootTime
.Second
;
1051 // Multiply result by 18.2 for number of ticks since midnight.
1052 // Use 182/10 to avoid floating point math.
1054 LocalTime
= (LocalTime
* 182) / 10;
1055 BdaPtr
= (UINT32
*) (UINTN
)0x46C;
1056 *BdaPtr
= LocalTime
;
1059 // Shadow PCI ROMs. We must do this near the end since this will kick
1060 // of Native EFI drivers that may be needed to collect info for Legacy16
1062 // WARNING: PciIo is gone after this call.
1064 PciShadowRoms (Private
);
1067 // Shadow PXE base code, BIS etc.
1069 Private
->LegacyRegion
->UnLock (Private
->LegacyRegion
, 0xc0000, 0x40000, &Granularity
);
1070 ShadowAddress
= Private
->OptionRom
;
1071 Private
->LegacyBiosPlatform
->PlatformHooks (
1072 Private
->LegacyBiosPlatform
,
1073 EfiPlatformHookShadowServiceRoms
,
1080 Private
->OptionRom
= (UINT32
)ShadowAddress
;
1082 // Register Legacy SMI Handler
1084 LegacyBiosPlatform
->SmmInit (
1086 EfiToLegacy16BootTable
1090 // Let platform code know the boot options
1092 LegacyBiosGetBbsInfo (
1101 PrintPciInterruptRegister ();
1102 PrintBbsTable (LocalBbsTable
);
1103 PrintHddInfo (LocalHddInfo
);
1106 // If drive wasn't spun up then BuildIdeData may have found new drives.
1107 // Need to update BBS boot priority.
1109 for (Index
= 0; Index
< MAX_IDE_CONTROLLER
; Index
++) {
1110 if ((LocalHddInfo
[Index
].IdentifyDrive
[0].Raw
[0] != 0) &&
1111 (LocalBbsTable
[2 * Index
+ 1].BootPriority
== BBS_IGNORE_ENTRY
)
1113 LocalBbsTable
[2 * Index
+ 1].BootPriority
= BBS_UNPRIORITIZED_ENTRY
;
1116 if ((LocalHddInfo
[Index
].IdentifyDrive
[1].Raw
[0] != 0) &&
1117 (LocalBbsTable
[2 * Index
+ 2].BootPriority
== BBS_IGNORE_ENTRY
)
1119 LocalBbsTable
[2 * Index
+ 2].BootPriority
= BBS_UNPRIORITIZED_ENTRY
;
1123 Private
->LegacyRegion
->UnLock (
1124 Private
->LegacyRegion
,
1130 LegacyBiosPlatform
->PrepareToBoot (
1136 (VOID
*) &Private
->IntThunk
->EfiToLegacy16BootTable
1140 // If no boot device return to BDS
1142 if ((mBootMode
== BOOT_LEGACY_OS
) || (mBootMode
== BOOT_UNCONVENTIONAL_DEVICE
)) {
1143 for (Index
= 0; Index
< BbsCount
; Index
++){
1144 if ((LocalBbsTable
[Index
].BootPriority
!= BBS_DO_NOT_BOOT_FROM
) &&
1145 (LocalBbsTable
[Index
].BootPriority
!= BBS_UNPRIORITIZED_ENTRY
) &&
1146 (LocalBbsTable
[Index
].BootPriority
!= BBS_IGNORE_ENTRY
)) {
1150 if (Index
== BbsCount
) {
1151 return EFI_DEVICE_ERROR
;
1155 // Let the Legacy16 code know the device path type for legacy boot
1157 EfiToLegacy16BootTable
->DevicePathType
= mBbsDevicePathPtr
->DeviceType
;
1160 // Copy MP table, if it exists.
1162 LegacyGetDataOrTable (This
, EfiGetPlatformBinaryMpTable
);
1164 if (!Private
->LegacyBootEntered
) {
1166 // Copy OEM INT Data, if it exists. Note: This code treats any data
1167 // as a bag of bits and knows nothing of the contents nor cares.
1168 // Contents are IBV specific.
1170 LegacyGetDataOrTable (This
, EfiGetPlatformBinaryOemIntData
);
1173 // Copy OEM16 Data, if it exists.Note: This code treats any data
1174 // as a bag of bits and knows nothing of the contents nor cares.
1175 // Contents are IBV specific.
1177 LegacyGetDataOrTable (This
, EfiGetPlatformBinaryOem16Data
);
1180 // Copy OEM32 Data, if it exists.Note: This code treats any data
1181 // as a bag of bits and knows nothing of the contents nor cares.
1182 // Contents are IBV specific.
1184 LegacyGetDataOrTable (This
, EfiGetPlatformBinaryOem32Data
);
1188 // Call into Legacy16 code to prepare for INT 19h
1190 ZeroMem (&Regs
, sizeof (EFI_IA32_REGISTER_SET
));
1191 Regs
.X
.AX
= Legacy16PrepareToBoot
;
1194 // Pass in handoff data
1196 Regs
.X
.ES
= EFI_SEGMENT ((UINTN
)EfiToLegacy16BootTable
);
1197 Regs
.X
.BX
= EFI_OFFSET ((UINTN
)EfiToLegacy16BootTable
);
1199 Private
->LegacyBios
.FarCall86 (
1201 Private
->Legacy16CallSegment
,
1202 Private
->Legacy16CallOffset
,
1208 if (Regs
.X
.AX
!= 0) {
1209 return EFI_DEVICE_ERROR
;
1212 // Lock the Legacy BIOS region
1214 Private
->LegacyRegion
->Lock (
1215 Private
->LegacyRegion
,
1221 // Lock attributes of the Legacy Region if chipset supports
1223 Private
->LegacyRegion
->BootLock (
1224 Private
->LegacyRegion
,
1231 // Call into Legacy16 code to do the INT 19h
1233 EnableAllControllers (Private
);
1234 if ((mBootMode
== BOOT_LEGACY_OS
) || (mBootMode
== BOOT_UNCONVENTIONAL_DEVICE
)) {
1236 // Signal all the events that are waiting on EVT_SIGNAL_LEGACY_BOOT
1238 EfiSignalEventLegacyBoot ();
1239 DEBUG ((EFI_D_INFO
, "Legacy INT19 Boot...\n"));
1241 // Raise TPL to high level to disable CPU interrupts
1243 gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
1246 // Put the 8259 into its legacy mode by reprogramming the vector bases
1248 Private
->Legacy8259
->SetVectorBase (Private
->Legacy8259
, LEGACY_MODE_BASE_VECTOR_MASTER
, LEGACY_MODE_BASE_VECTOR_SLAVE
);
1251 // The original PC used INT8-F for master PIC. Since these mapped over
1252 // processor exceptions TIANO moved the master PIC to INT68-6F.
1253 // We need to set these back to the Legacy16 unexpected interrupt(saved
1254 // in LegacyBios.c) since some OS see that these have values different from
1255 // what is expected and invoke them. Since the legacy OS corrupts EFI
1256 // memory, there is no handler for these interrupts and OS blows up.
1258 // We need to save the TIANO values for the rare case that the Legacy16
1259 // code cannot boot but knows memory hasn't been destroyed.
1261 // To compound the problem, video takes over one of these INTS and must be
1263 // @bug - determine if video hooks INT(in which case we must find new
1264 // set of TIANO vectors) or takes it over.
1267 BaseVectorMaster
= (UINT32
*) (sizeof (UINT32
) * PROTECTED_MODE_BASE_VECTOR_MASTER
);
1268 for (Index
= 0; Index
< 8; Index
++) {
1269 Private
->ThunkSavedInt
[Index
] = BaseVectorMaster
[Index
];
1270 if (Private
->ThunkSeg
== (UINT16
) (BaseVectorMaster
[Index
] >> 16)) {
1271 BaseVectorMaster
[Index
] = (UINT32
) (Private
->BiosUnexpectedInt
);
1275 ZeroMem (&Regs
, sizeof (EFI_IA32_REGISTER_SET
));
1276 Regs
.X
.AX
= Legacy16Boot
;
1278 Private
->LegacyBios
.FarCall86 (
1280 Private
->Legacy16CallSegment
,
1281 Private
->Legacy16CallOffset
,
1287 BaseVectorMaster
= (UINT32
*) (sizeof (UINT32
) * PROTECTED_MODE_BASE_VECTOR_MASTER
);
1288 for (Index
= 0; Index
< 8; Index
++) {
1289 BaseVectorMaster
[Index
] = Private
->ThunkSavedInt
[Index
];
1292 Private
->LegacyBootEntered
= TRUE
;
1293 if ((mBootMode
== BOOT_LEGACY_OS
) || (mBootMode
== BOOT_UNCONVENTIONAL_DEVICE
)) {
1295 // Should never return unless never passed control to 0:7c00(first stage
1296 // OS loader) and only then if no bootable device found.
1298 return EFI_DEVICE_ERROR
;
1301 // If boot to EFI then expect to return to caller
1309 Assign drive number to legacy HDD drives prior to booting an EFI
1310 aware OS so the OS can access drives without an EFI driver.
1311 Note: BBS compliant drives ARE NOT available until this call by
1312 either shell or EFI.
1314 @param This Protocol instance pointer.
1315 @param BbsCount Number of BBS_TABLE structures
1316 @param BbsTable List BBS entries
1318 @retval EFI_SUCCESS Drive numbers assigned
1323 LegacyBiosPrepareToBootEfi (
1324 IN EFI_LEGACY_BIOS_PROTOCOL
*This
,
1325 OUT UINT16
*BbsCount
,
1326 OUT BBS_TABLE
**BbsTable
1330 EFI_TO_COMPATIBILITY16_BOOT_TABLE
*EfiToLegacy16BootTable
;
1331 LEGACY_BIOS_INSTANCE
*Private
;
1333 Private
= LEGACY_BIOS_INSTANCE_FROM_THIS (This
);
1334 EfiToLegacy16BootTable
= &Private
->IntThunk
->EfiToLegacy16BootTable
;
1335 mBootMode
= BOOT_EFI_OS
;
1336 mBbsDevicePathPtr
= NULL
;
1337 Status
= GenericLegacyBoot (This
);
1338 *BbsTable
= (BBS_TABLE
*)(UINTN
)EfiToLegacy16BootTable
->BbsTable
;
1339 *BbsCount
= (UINT16
) (sizeof (Private
->IntThunk
->BbsTable
) / sizeof (BBS_TABLE
));
1344 To boot from an unconventional device like parties and/or execute HDD diagnostics.
1346 @param This Protocol instance pointer.
1347 @param Attributes How to interpret the other input parameters
1348 @param BbsEntry The 0-based index into the BbsTable for the parent
1350 @param BeerData Pointer to the 128 bytes of ram BEER data.
1351 @param ServiceAreaData Pointer to the 64 bytes of raw Service Area data. The
1352 caller must provide a pointer to the specific Service
1353 Area and not the start all Service Areas.
1355 @retval EFI_INVALID_PARAMETER if error. Does NOT return if no error.
1360 LegacyBiosBootUnconventionalDevice (
1361 IN EFI_LEGACY_BIOS_PROTOCOL
*This
,
1362 IN UDC_ATTRIBUTES Attributes
,
1365 IN VOID
*ServiceAreaData
1369 EFI_TO_COMPATIBILITY16_BOOT_TABLE
*EfiToLegacy16BootTable
;
1370 LEGACY_BIOS_INSTANCE
*Private
;
1373 UINT16 BootPriority
;
1374 BBS_TABLE
*BbsTable
;
1377 Private
= LEGACY_BIOS_INSTANCE_FROM_THIS (This
);
1378 mBootMode
= BOOT_UNCONVENTIONAL_DEVICE
;
1379 mBbsDevicePathPtr
= &mBbsDevicePathNode
;
1380 mAttributes
= Attributes
;
1381 mBbsEntry
= BbsEntry
;
1382 mBeerData
= BeerData
, mServiceAreaData
= ServiceAreaData
;
1384 EfiToLegacy16BootTable
= &Private
->IntThunk
->EfiToLegacy16BootTable
;
1387 // Do input parameter checking
1389 if ((Attributes
.DirectoryServiceValidity
== 0) &&
1390 (Attributes
.RabcaUsedFlag
== 0) &&
1391 (Attributes
.ExecuteHddDiagnosticsFlag
== 0)
1393 return EFI_INVALID_PARAMETER
;
1396 if (((Attributes
.DirectoryServiceValidity
!= 0) && (ServiceAreaData
== NULL
)) ||
1397 (((Attributes
.DirectoryServiceValidity
| Attributes
.RabcaUsedFlag
) != 0) && (BeerData
== NULL
))
1399 return EFI_INVALID_PARAMETER
;
1402 UcdTable
= (UD_TABLE
*) AllocatePool (
1405 if (NULL
== UcdTable
) {
1406 return EFI_OUT_OF_RESOURCES
;
1409 EfiToLegacy16BootTable
->UnconventionalDeviceTable
= (UINT32
)(UINTN
)UcdTable
;
1410 UcdTable
->Attributes
= Attributes
;
1411 UcdTable
->BbsTableEntryNumberForParentDevice
= (UINT8
) BbsEntry
;
1413 // Force all existing BBS entries to DoNotBoot. This allows 16-bit CSM
1414 // to assign drive numbers but bot boot from. Only newly created entries
1417 BbsTable
= (BBS_TABLE
*)(UINTN
)EfiToLegacy16BootTable
->BbsTable
;
1418 for (Index
= 0; Index
< EfiToLegacy16BootTable
->NumberBbsEntries
; Index
++) {
1419 BbsTable
[Index
].BootPriority
= BBS_DO_NOT_BOOT_FROM
;
1422 // If parent is onboard IDE then assign controller & device number
1425 if (BbsEntry
< MAX_IDE_CONTROLLER
* 2) {
1426 UcdTable
->DeviceNumber
= (UINT8
) ((BbsEntry
- 1) % 2);
1429 if (BeerData
!= NULL
) {
1431 (VOID
*) UcdTable
->BeerData
,
1437 if (ServiceAreaData
!= NULL
) {
1439 (VOID
*) UcdTable
->ServiceAreaData
,
1445 // For each new entry do the following:
1446 // 1. Increment current number of BBS entries
1447 // 2. Copy parent entry to new entry.
1448 // 3. Zero out BootHandler Offset & segment
1449 // 4. Set appropriate device type. BEV(0x80) for HDD diagnostics
1450 // and Floppy(0x01) for PARTIES boot.
1451 // 5. Assign new priority.
1453 if ((Attributes
.ExecuteHddDiagnosticsFlag
) != 0) {
1454 EfiToLegacy16BootTable
->NumberBbsEntries
+= 1;
1457 (VOID
*) &BbsTable
[EfiToLegacy16BootTable
->NumberBbsEntries
].BootPriority
,
1458 (VOID
*) &BbsTable
[BbsEntry
].BootPriority
,
1462 BbsTable
[EfiToLegacy16BootTable
->NumberBbsEntries
].BootHandlerOffset
= 0;
1463 BbsTable
[EfiToLegacy16BootTable
->NumberBbsEntries
].BootHandlerSegment
= 0;
1464 BbsTable
[EfiToLegacy16BootTable
->NumberBbsEntries
].DeviceType
= 0x80;
1466 UcdTable
->BbsTableEntryNumberForHddDiag
= (UINT8
) (EfiToLegacy16BootTable
->NumberBbsEntries
- 1);
1468 BbsTable
[EfiToLegacy16BootTable
->NumberBbsEntries
].BootPriority
= BootPriority
;
1472 // Set device type as BBS_TYPE_DEV for PARTIES diagnostic
1474 mBbsDevicePathNode
.DeviceType
= BBS_TYPE_BEV
;
1477 if (((Attributes
.DirectoryServiceValidity
| Attributes
.RabcaUsedFlag
)) != 0) {
1478 EfiToLegacy16BootTable
->NumberBbsEntries
+= 1;
1480 (VOID
*) &BbsTable
[EfiToLegacy16BootTable
->NumberBbsEntries
].BootPriority
,
1481 (VOID
*) &BbsTable
[BbsEntry
].BootPriority
,
1485 BbsTable
[EfiToLegacy16BootTable
->NumberBbsEntries
].BootHandlerOffset
= 0;
1486 BbsTable
[EfiToLegacy16BootTable
->NumberBbsEntries
].BootHandlerSegment
= 0;
1487 BbsTable
[EfiToLegacy16BootTable
->NumberBbsEntries
].DeviceType
= 0x01;
1488 UcdTable
->BbsTableEntryNumberForBoot
= (UINT8
) (EfiToLegacy16BootTable
->NumberBbsEntries
- 1);
1489 BbsTable
[EfiToLegacy16BootTable
->NumberBbsEntries
].BootPriority
= BootPriority
;
1492 // Set device type as BBS_TYPE_FLOPPY for PARTIES boot as floppy
1494 mBbsDevicePathNode
.DeviceType
= BBS_TYPE_FLOPPY
;
1497 // Build the BBS Device Path for this boot selection
1499 mBbsDevicePathNode
.Header
.Type
= BBS_DEVICE_PATH
;
1500 mBbsDevicePathNode
.Header
.SubType
= BBS_BBS_DP
;
1501 SetDevicePathNodeLength (&mBbsDevicePathNode
.Header
, sizeof (BBS_BBS_DEVICE_PATH
));
1502 mBbsDevicePathNode
.StatusFlag
= 0;
1503 mBbsDevicePathNode
.String
[0] = 0;
1505 Status
= GenericLegacyBoot (This
);
1510 Attempt to legacy boot the BootOption. If the EFI contexted has been
1511 compromised this function will not return.
1513 @param This Protocol instance pointer.
1514 @param BbsDevicePath EFI Device Path from BootXXXX variable.
1515 @param LoadOptionsSize Size of LoadOption in size.
1516 @param LoadOptions LoadOption from BootXXXX variable
1518 @retval EFI_SUCCESS Removable media not present
1523 LegacyBiosLegacyBoot (
1524 IN EFI_LEGACY_BIOS_PROTOCOL
*This
,
1525 IN BBS_BBS_DEVICE_PATH
*BbsDevicePath
,
1526 IN UINT32 LoadOptionsSize
,
1527 IN VOID
*LoadOptions
1532 mBbsDevicePathPtr
= BbsDevicePath
;
1533 mLoadOptionsSize
= LoadOptionsSize
;
1534 mLoadOptions
= LoadOptions
;
1535 mBootMode
= BOOT_LEGACY_OS
;
1536 Status
= GenericLegacyBoot (This
);
1542 Convert EFI Memory Type to E820 Memory Type.
1544 @param Type EFI Memory Type
1546 @return ACPI Memory Type for EFI Memory Type
1549 EFI_ACPI_MEMORY_TYPE
1550 EfiMemoryTypeToE820Type (
1557 case EfiBootServicesCode
:
1558 case EfiBootServicesData
:
1559 case EfiConventionalMemory
:
1560 case EfiRuntimeServicesCode
:
1561 case EfiRuntimeServicesData
:
1562 return EfiAcpiAddressRangeMemory
;
1564 case EfiACPIReclaimMemory
:
1565 return EfiAcpiAddressRangeACPI
;
1567 case EfiACPIMemoryNVS
:
1568 return EfiAcpiAddressRangeNVS
;
1571 // All other types map to reserved.
1572 // Adding the code just waists FLASH space.
1574 // case EfiReservedMemoryType:
1575 // case EfiUnusableMemory:
1576 // case EfiMemoryMappedIO:
1577 // case EfiMemoryMappedIOPortSpace:
1581 return EfiAcpiAddressRangeReserved
;
1586 Build the E820 table.
1588 @param Private Legacy BIOS Instance data
1589 @param Size Size of E820 Table
1591 @retval EFI_SUCCESS It should always work.
1595 LegacyBiosBuildE820 (
1596 IN LEGACY_BIOS_INSTANCE
*Private
,
1601 EFI_E820_ENTRY64
*E820Table
;
1602 EFI_MEMORY_DESCRIPTOR
*EfiMemoryMap
;
1603 EFI_MEMORY_DESCRIPTOR
*EfiMemoryMapEnd
;
1604 EFI_MEMORY_DESCRIPTOR
*EfiEntry
;
1605 EFI_MEMORY_DESCRIPTOR
*NextEfiEntry
;
1606 EFI_MEMORY_DESCRIPTOR TempEfiEntry
;
1607 UINTN EfiMemoryMapSize
;
1609 UINTN EfiDescriptorSize
;
1610 UINT32 EfiDescriptorVersion
;
1612 EFI_PEI_HOB_POINTERS Hob
;
1613 EFI_HOB_RESOURCE_DESCRIPTOR
*ResourceHob
;
1616 UINTN TempNextIndex
;
1617 EFI_E820_ENTRY64 TempE820
;
1618 EFI_ACPI_MEMORY_TYPE TempType
;
1619 BOOLEAN ChangedFlag
;
1621 UINT64 MemoryBlockLength
;
1623 E820Table
= (EFI_E820_ENTRY64
*) Private
->E820Table
;
1626 // Get the EFI memory map.
1628 EfiMemoryMapSize
= 0;
1629 EfiMemoryMap
= NULL
;
1630 Status
= gBS
->GetMemoryMap (
1635 &EfiDescriptorVersion
1637 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
1641 // Use size returned back plus 1 descriptor for the AllocatePool.
1642 // We don't just multiply by 2 since the "for" loop below terminates on
1643 // EfiMemoryMapEnd which is dependent upon EfiMemoryMapSize. Otherwize
1644 // we process bogus entries and create bogus E820 entries.
1646 EfiMemoryMap
= (EFI_MEMORY_DESCRIPTOR
*) AllocatePool (EfiMemoryMapSize
);
1647 ASSERT (EfiMemoryMap
!= NULL
);
1648 Status
= gBS
->GetMemoryMap (
1653 &EfiDescriptorVersion
1655 if (EFI_ERROR (Status
)) {
1656 FreePool (EfiMemoryMap
);
1658 } while (Status
== EFI_BUFFER_TOO_SMALL
);
1660 ASSERT_EFI_ERROR (Status
);
1663 // Punch in the E820 table for memory less than 1 MB.
1664 // Assume ZeroMem () has been done on data structure.
1667 // First entry is 0 to (640k - EBDA)
1669 E820Table
[0].BaseAddr
= 0;
1670 E820Table
[0].Length
= (UINT64
) ((*(UINT16
*) (UINTN
)0x40E) << 4);
1671 E820Table
[0].Type
= EfiAcpiAddressRangeMemory
;
1674 // Second entry is (640k - EBDA) to 640k
1676 E820Table
[1].BaseAddr
= E820Table
[0].Length
;
1677 E820Table
[1].Length
= (UINT64
) ((640 * 1024) - E820Table
[0].Length
);
1678 E820Table
[1].Type
= EfiAcpiAddressRangeReserved
;
1681 // Third Entry is legacy BIOS
1682 // DO NOT CLAIM region from 0xA0000-0xDFFFF. OS can use free areas
1683 // to page in memory under 1MB.
1684 // Omit region from 0xE0000 to start of BIOS, if any. This can be
1685 // used for a multiple reasons including OPROMS.
1689 // The CSM binary image size is not the actually size that CSM binary used,
1690 // to avoid memory corrupt, we declare the 0E0000 - 0FFFFF is used by CSM binary.
1692 E820Table
[2].BaseAddr
= 0xE0000;
1693 E820Table
[2].Length
= 0x20000;
1694 E820Table
[2].Type
= EfiAcpiAddressRangeReserved
;
1699 // Process the EFI map to produce E820 map;
1703 // Sort memory map from low to high
1705 EfiEntry
= EfiMemoryMap
;
1706 NextEfiEntry
= NEXT_MEMORY_DESCRIPTOR (EfiEntry
, EfiDescriptorSize
);
1707 EfiMemoryMapEnd
= (EFI_MEMORY_DESCRIPTOR
*) ((UINT8
*) EfiMemoryMap
+ EfiMemoryMapSize
);
1708 while (EfiEntry
< EfiMemoryMapEnd
) {
1709 while (NextEfiEntry
< EfiMemoryMapEnd
) {
1710 if (EfiEntry
->PhysicalStart
> NextEfiEntry
->PhysicalStart
) {
1711 CopyMem (&TempEfiEntry
, EfiEntry
, sizeof (EFI_MEMORY_DESCRIPTOR
));
1712 CopyMem (EfiEntry
, NextEfiEntry
, sizeof (EFI_MEMORY_DESCRIPTOR
));
1713 CopyMem (NextEfiEntry
, &TempEfiEntry
, sizeof (EFI_MEMORY_DESCRIPTOR
));
1716 NextEfiEntry
= NEXT_MEMORY_DESCRIPTOR (NextEfiEntry
, EfiDescriptorSize
);
1719 EfiEntry
= NEXT_MEMORY_DESCRIPTOR (EfiEntry
, EfiDescriptorSize
);
1720 NextEfiEntry
= NEXT_MEMORY_DESCRIPTOR (EfiEntry
, EfiDescriptorSize
);
1723 EfiEntry
= EfiMemoryMap
;
1724 EfiMemoryMapEnd
= (EFI_MEMORY_DESCRIPTOR
*) ((UINT8
*) EfiMemoryMap
+ EfiMemoryMapSize
);
1725 for (Index
= Above1MIndex
; (EfiEntry
< EfiMemoryMapEnd
) && (Index
< EFI_MAX_E820_ENTRY
- 1); ) {
1726 MemoryBlockLength
= (UINT64
) (LShiftU64 (EfiEntry
->NumberOfPages
, 12));
1727 if ((EfiEntry
->PhysicalStart
+ MemoryBlockLength
) < 0x100000) {
1729 // Skip the memory block is under 1MB
1732 if (EfiEntry
->PhysicalStart
< 0x100000) {
1734 // When the memory block spans below 1MB, ensure the memory block start address is at least 1MB
1736 MemoryBlockLength
-= 0x100000 - EfiEntry
->PhysicalStart
;
1737 EfiEntry
->PhysicalStart
= 0x100000;
1741 // Convert memory type to E820 type
1743 TempType
= EfiMemoryTypeToE820Type (EfiEntry
->Type
);
1745 if ((E820Table
[Index
].Type
== TempType
) && (EfiEntry
->PhysicalStart
== (E820Table
[Index
].BaseAddr
+ E820Table
[Index
].Length
))) {
1747 // Grow an existing entry
1749 E820Table
[Index
].Length
+= MemoryBlockLength
;
1755 E820Table
[Index
].BaseAddr
= EfiEntry
->PhysicalStart
;
1756 E820Table
[Index
].Length
= MemoryBlockLength
;
1757 E820Table
[Index
].Type
= TempType
;
1760 EfiEntry
= NEXT_MEMORY_DESCRIPTOR (EfiEntry
, EfiDescriptorSize
);
1763 FreePool (EfiMemoryMap
);
1766 // Process the reserved memory map to produce E820 map ;
1768 for (Hob
.Raw
= GetHobList (); !END_OF_HOB_LIST (Hob
); Hob
.Raw
= GET_NEXT_HOB (Hob
)) {
1769 if (Hob
.Raw
!= NULL
&& GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
1770 ResourceHob
= Hob
.ResourceDescriptor
;
1771 if (((ResourceHob
->ResourceType
== EFI_RESOURCE_MEMORY_MAPPED_IO
) ||
1772 (ResourceHob
->ResourceType
== EFI_RESOURCE_FIRMWARE_DEVICE
) ||
1773 (ResourceHob
->ResourceType
== EFI_RESOURCE_MEMORY_RESERVED
) ) &&
1774 (ResourceHob
->PhysicalStart
> 0x100000) &&
1775 (Index
< EFI_MAX_E820_ENTRY
- 1)) {
1777 E820Table
[Index
].BaseAddr
= ResourceHob
->PhysicalStart
;
1778 E820Table
[Index
].Length
= ResourceHob
->ResourceLength
;
1779 E820Table
[Index
].Type
= EfiAcpiAddressRangeReserved
;
1785 Private
->IntThunk
->EfiToLegacy16InitTable
.NumberE820Entries
= (UINT32
)Index
;
1786 Private
->IntThunk
->EfiToLegacy16BootTable
.NumberE820Entries
= (UINT32
)Index
;
1787 Private
->NumberE820Entries
= (UINT32
)Index
;
1788 *Size
= (UINTN
) (Index
* sizeof (EFI_E820_ENTRY64
));
1791 // Sort E820Table from low to high
1793 for (TempIndex
= 0; TempIndex
< Index
; TempIndex
++) {
1794 ChangedFlag
= FALSE
;
1795 for (TempNextIndex
= 1; TempNextIndex
< Index
- TempIndex
; TempNextIndex
++) {
1796 if (E820Table
[TempNextIndex
- 1].BaseAddr
> E820Table
[TempNextIndex
].BaseAddr
) {
1798 TempE820
.BaseAddr
= E820Table
[TempNextIndex
- 1].BaseAddr
;
1799 TempE820
.Length
= E820Table
[TempNextIndex
- 1].Length
;
1800 TempE820
.Type
= E820Table
[TempNextIndex
- 1].Type
;
1802 E820Table
[TempNextIndex
- 1].BaseAddr
= E820Table
[TempNextIndex
].BaseAddr
;
1803 E820Table
[TempNextIndex
- 1].Length
= E820Table
[TempNextIndex
].Length
;
1804 E820Table
[TempNextIndex
- 1].Type
= E820Table
[TempNextIndex
].Type
;
1806 E820Table
[TempNextIndex
].BaseAddr
= TempE820
.BaseAddr
;
1807 E820Table
[TempNextIndex
].Length
= TempE820
.Length
;
1808 E820Table
[TempNextIndex
].Type
= TempE820
.Type
;
1818 // Remove the overlap range
1820 for (TempIndex
= 1; TempIndex
< Index
; TempIndex
++) {
1821 if (E820Table
[TempIndex
- 1].BaseAddr
<= E820Table
[TempIndex
].BaseAddr
&&
1822 ((E820Table
[TempIndex
- 1].BaseAddr
+ E820Table
[TempIndex
- 1].Length
) >=
1823 (E820Table
[TempIndex
].BaseAddr
+E820Table
[TempIndex
].Length
))) {
1825 //Overlap range is found
1827 ASSERT (E820Table
[TempIndex
- 1].Type
== E820Table
[TempIndex
].Type
);
1829 if (TempIndex
== Index
- 1) {
1830 E820Table
[TempIndex
].BaseAddr
= 0;
1831 E820Table
[TempIndex
].Length
= 0;
1832 E820Table
[TempIndex
].Type
= (EFI_ACPI_MEMORY_TYPE
) 0;
1836 for (IndexSort
= TempIndex
; IndexSort
< Index
- 1; IndexSort
++) {
1837 E820Table
[IndexSort
].BaseAddr
= E820Table
[IndexSort
+ 1].BaseAddr
;
1838 E820Table
[IndexSort
].Length
= E820Table
[IndexSort
+ 1].Length
;
1839 E820Table
[IndexSort
].Type
= E820Table
[IndexSort
+ 1].Type
;
1848 Private
->IntThunk
->EfiToLegacy16InitTable
.NumberE820Entries
= (UINT32
)Index
;
1849 Private
->IntThunk
->EfiToLegacy16BootTable
.NumberE820Entries
= (UINT32
)Index
;
1850 Private
->NumberE820Entries
= (UINT32
)Index
;
1851 *Size
= (UINTN
) (Index
* sizeof (EFI_E820_ENTRY64
));
1854 // Determine OS usable memory above 1Mb
1856 Private
->IntThunk
->EfiToLegacy16BootTable
.OsMemoryAbove1Mb
= 0x0000;
1857 for (TempIndex
= Above1MIndex
; TempIndex
< Index
; TempIndex
++) {
1858 if (E820Table
[TempIndex
].BaseAddr
>= 0x100000 && E820Table
[TempIndex
].BaseAddr
< 0x100000000ULL
) { // not include above 4G memory
1860 // ACPIReclaimMemory is also usable memory for ACPI OS, after OS dumps all ACPI tables.
1862 if ((E820Table
[TempIndex
].Type
== EfiAcpiAddressRangeMemory
) || (E820Table
[TempIndex
].Type
== EfiAcpiAddressRangeACPI
)) {
1863 Private
->IntThunk
->EfiToLegacy16BootTable
.OsMemoryAbove1Mb
+= (UINT32
) (E820Table
[TempIndex
].Length
);
1865 break; // break at first not normal memory, because SMM may use reserved memory.
1870 Private
->IntThunk
->EfiToLegacy16InitTable
.OsMemoryAbove1Mb
= Private
->IntThunk
->EfiToLegacy16BootTable
.OsMemoryAbove1Mb
;
1873 // Print DEBUG information
1875 for (TempIndex
= 0; TempIndex
< Index
; TempIndex
++) {
1876 DEBUG((EFI_D_INFO
, "E820[%2d]: 0x%16lx ---- 0x%16lx, Type = 0x%x \n",
1878 E820Table
[TempIndex
].BaseAddr
,
1879 (E820Table
[TempIndex
].BaseAddr
+ E820Table
[TempIndex
].Length
),
1880 E820Table
[TempIndex
].Type
1889 Fill in the standard BDA and EBDA stuff prior to legacy Boot
1891 @param Private Legacy BIOS Instance data
1893 @retval EFI_SUCCESS It should always work.
1897 LegacyBiosCompleteBdaBeforeBoot (
1898 IN LEGACY_BIOS_INSTANCE
*Private
1902 UINT16 MachineConfig
;
1903 DEVICE_PRODUCER_DATA_HEADER
*SioPtr
;
1905 Bda
= (BDA_STRUC
*) ((UINTN
) 0x400);
1908 SioPtr
= &(Private
->IntThunk
->EfiToLegacy16BootTable
.SioData
);
1909 Bda
->Com1
= SioPtr
->Serial
[0].Address
;
1910 Bda
->Com2
= SioPtr
->Serial
[1].Address
;
1911 Bda
->Com3
= SioPtr
->Serial
[2].Address
;
1912 Bda
->Com4
= SioPtr
->Serial
[3].Address
;
1914 if (SioPtr
->Serial
[0].Address
!= 0x00) {
1915 MachineConfig
+= 0x200;
1918 if (SioPtr
->Serial
[1].Address
!= 0x00) {
1919 MachineConfig
+= 0x200;
1922 if (SioPtr
->Serial
[2].Address
!= 0x00) {
1923 MachineConfig
+= 0x200;
1926 if (SioPtr
->Serial
[3].Address
!= 0x00) {
1927 MachineConfig
+= 0x200;
1930 Bda
->Lpt1
= SioPtr
->Parallel
[0].Address
;
1931 Bda
->Lpt2
= SioPtr
->Parallel
[1].Address
;
1932 Bda
->Lpt3
= SioPtr
->Parallel
[2].Address
;
1934 if (SioPtr
->Parallel
[0].Address
!= 0x00) {
1935 MachineConfig
+= 0x4000;
1938 if (SioPtr
->Parallel
[1].Address
!= 0x00) {
1939 MachineConfig
+= 0x4000;
1942 if (SioPtr
->Parallel
[2].Address
!= 0x00) {
1943 MachineConfig
+= 0x4000;
1946 Bda
->NumberOfDrives
= (UINT8
) (Bda
->NumberOfDrives
+ Private
->IdeDriveCount
);
1947 if (SioPtr
->Floppy
.NumberOfFloppy
!= 0x00) {
1948 MachineConfig
= (UINT16
) (MachineConfig
+ 0x01 + (SioPtr
->Floppy
.NumberOfFloppy
- 1) * 0x40);
1949 Bda
->FloppyXRate
= 0x07;
1952 Bda
->Lpt1_2Timeout
= 0x1414;
1953 Bda
->Lpt3_4Timeout
= 0x1414;
1954 Bda
->Com1_2Timeout
= 0x0101;
1955 Bda
->Com3_4Timeout
= 0x0101;
1958 // Force VGA and Coprocessor, indicate 101/102 keyboard
1960 MachineConfig
= (UINT16
) (MachineConfig
+ 0x00 + 0x02 + (SioPtr
->MousePresent
* 0x04));
1961 Bda
->MachineConfig
= MachineConfig
;
1967 Fill in the standard BDA for Keyboard LEDs
1969 @param This Protocol instance pointer.
1970 @param Leds Current LED status
1972 @retval EFI_SUCCESS It should always work.
1977 LegacyBiosUpdateKeyboardLedStatus (
1978 IN EFI_LEGACY_BIOS_PROTOCOL
*This
,
1982 LEGACY_BIOS_INSTANCE
*Private
;
1985 EFI_IA32_REGISTER_SET Regs
;
1987 Bda
= (BDA_STRUC
*) ((UINTN
) 0x400);
1989 Private
= LEGACY_BIOS_INSTANCE_FROM_THIS (This
);
1991 Bda
->LedStatus
= (UINT8
) ((Bda
->LedStatus
&~0x07) | LocalLeds
);
1992 LocalLeds
= (UINT8
) (LocalLeds
<< 4);
1993 Bda
->ShiftStatus
= (UINT8
) ((Bda
->ShiftStatus
&~0x70) | LocalLeds
);
1994 LocalLeds
= (UINT8
) (Leds
& 0x20);
1995 Bda
->KeyboardStatus
= (UINT8
) ((Bda
->KeyboardStatus
&~0x20) | LocalLeds
);
1997 // Call into Legacy16 code to allow it to do any processing
1999 ZeroMem (&Regs
, sizeof (EFI_IA32_REGISTER_SET
));
2000 Regs
.X
.AX
= Legacy16SetKeyboardLeds
;
2003 Private
->LegacyBios
.FarCall86 (
2004 &Private
->LegacyBios
,
2005 Private
->Legacy16Table
->Compatibility16CallSegment
,
2006 Private
->Legacy16Table
->Compatibility16CallOffset
,
2017 Fill in the standard CMOS stuff prior to legacy Boot
2019 @param Private Legacy BIOS Instance data
2021 @retval EFI_SUCCESS It should always work.
2025 LegacyBiosCompleteStandardCmosBeforeBoot (
2026 IN LEGACY_BIOS_INSTANCE
*Private
2034 // Update CMOS locations
2036 // 12,19,1A - ignore as OS don't use them and there is no standard due
2037 // to large capacity drives
2038 // CMOS 14 = BDA 40:10 plus bit 3(display enabled)
2040 Bda
= (UINT8
)(*((UINT8
*)((UINTN
)0x410)) | BIT3
);
2043 // Force display enabled
2046 if ((Bda
& BIT0
) != 0) {
2051 // Check if 2.88MB floppy set
2053 if ((Bda
& (BIT7
| BIT6
)) != 0) {
2054 Floppy
= (UINT8
)(Floppy
| BIT1
);
2057 LegacyWriteStandardCmos (CMOS_10
, Floppy
);
2058 LegacyWriteStandardCmos (CMOS_14
, Bda
);
2061 // Force Status Register A to set rate selection bits and divider
2063 LegacyWriteStandardCmos (CMOS_0A
, 0x26);
2066 // redo memory size since it can change
2068 Size
= 15 * SIZE_1MB
;
2069 if (Private
->IntThunk
->EfiToLegacy16InitTable
.OsMemoryAbove1Mb
< (15 * SIZE_1MB
)) {
2070 Size
= Private
->IntThunk
->EfiToLegacy16InitTable
.OsMemoryAbove1Mb
>> 10;
2073 LegacyWriteStandardCmos (CMOS_17
, (UINT8
)(Size
& 0xFF));
2074 LegacyWriteStandardCmos (CMOS_30
, (UINT8
)(Size
& 0xFF));
2075 LegacyWriteStandardCmos (CMOS_18
, (UINT8
)(Size
>> 8));
2076 LegacyWriteStandardCmos (CMOS_31
, (UINT8
)(Size
>> 8));
2078 LegacyCalculateWriteStandardCmosChecksum ();
2084 Relocate this image under 4G memory for IPF.
2086 @param ImageHandle Handle of driver image.
2087 @param SystemTable Pointer to system table.
2089 @retval EFI_SUCCESS Image successfully relocated.
2090 @retval EFI_ABORTED Failed to relocate image.
2094 RelocateImageUnder4GIfNeeded (
2095 IN EFI_HANDLE ImageHandle
,
2096 IN EFI_SYSTEM_TABLE
*SystemTable