3 Copyright (c) 2006 - 2017, 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"
18 #define PHYSICAL_ADDRESS_TO_POINTER(Address) ((VOID *) ((UINTN) Address))
21 // define maximum number of HDD system supports
23 #define MAX_HDD_ENTRIES 0x30
27 // Since this driver will only ever produce one instance of the Private Data
28 // protocol you are not required to dynamically allocate the PrivateData.
30 LEGACY_BIOS_INSTANCE mPrivateData
;
33 // The SMBIOS table in EfiRuntimeServicesData memory
35 VOID
*mRuntimeSmbiosEntryPoint
= NULL
;
38 // The SMBIOS table in EfiReservedMemoryType memory
40 EFI_PHYSICAL_ADDRESS mReserveSmbiosEntryPoint
= 0;
41 EFI_PHYSICAL_ADDRESS mStructureTableAddress
= 0;
42 UINTN mStructureTablePages
= 0;
43 BOOLEAN mEndOfDxe
= FALSE
;
46 Do an AllocatePages () of type AllocateMaxAddress for EfiBootServicesCode
49 @param AllocateType Allocated Legacy Memory Type
50 @param StartPageAddress Start address of range
51 @param Pages Number of pages to allocate
52 @param Result Result of allocation
54 @retval EFI_SUCCESS Legacy16 code loaded
55 @retval Other No protocol installed, unload driver.
59 AllocateLegacyMemory (
60 IN EFI_ALLOCATE_TYPE AllocateType
,
61 IN EFI_PHYSICAL_ADDRESS StartPageAddress
,
63 OUT EFI_PHYSICAL_ADDRESS
*Result
67 EFI_PHYSICAL_ADDRESS MemPage
;
70 // Allocate Pages of memory less <= StartPageAddress
72 MemPage
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) StartPageAddress
;
73 Status
= gBS
->AllocatePages (
80 // Do not ASSERT on Status error but let caller decide since some cases
81 // memory is already taken but that is ok.
83 if (!EFI_ERROR (Status
)) {
84 *Result
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) MemPage
;
87 // If reach here the status = EFI_SUCCESS
94 This function is called when EFI needs to reserve an area in the 0xE0000 or 0xF0000
97 Note: inconsistency with the Framework CSM spec. Per the spec, this function may be
98 invoked only once. This limitation is relaxed to allow multiple calls in this implemenation.
100 @param This Protocol instance pointer.
101 @param LegacyMemorySize Size of required region
102 @param Region Region to use. 00 = Either 0xE0000 or 0xF0000
103 block Bit0 = 1 0xF0000 block Bit1 = 1 0xE0000
105 @param Alignment Address alignment. Bit mapped. First non-zero
106 bit from right is alignment.
107 @param LegacyMemoryAddress Region Assigned
109 @retval EFI_SUCCESS Region assigned
110 @retval EFI_ACCESS_DENIED Procedure previously invoked
111 @retval Other Region not assigned
116 LegacyBiosGetLegacyRegion (
117 IN EFI_LEGACY_BIOS_PROTOCOL
*This
,
118 IN UINTN LegacyMemorySize
,
121 OUT VOID
**LegacyMemoryAddress
125 LEGACY_BIOS_INSTANCE
*Private
;
126 EFI_IA32_REGISTER_SET Regs
;
130 Private
= LEGACY_BIOS_INSTANCE_FROM_THIS (This
);
131 Private
->LegacyRegion
->UnLock (Private
->LegacyRegion
, 0xE0000, 0x20000, &Granularity
);
133 ZeroMem (&Regs
, sizeof (EFI_IA32_REGISTER_SET
));
134 Regs
.X
.AX
= Legacy16GetTableAddress
;
135 Regs
.X
.BX
= (UINT16
) Region
;
136 Regs
.X
.CX
= (UINT16
) LegacyMemorySize
;
137 Regs
.X
.DX
= (UINT16
) Alignment
;
138 Private
->LegacyBios
.FarCall86 (
139 &Private
->LegacyBios
,
140 Private
->Legacy16CallSegment
,
141 Private
->Legacy16CallOffset
,
147 if (Regs
.X
.AX
== 0) {
148 *LegacyMemoryAddress
= (VOID
*) (((UINTN
) Regs
.X
.DS
<< 4) + Regs
.X
.BX
);
149 Status
= EFI_SUCCESS
;
151 Status
= EFI_OUT_OF_RESOURCES
;
154 Private
->Cpu
->FlushDataCache (Private
->Cpu
, 0xE0000, 0x20000, EfiCpuFlushTypeWriteBackInvalidate
);
155 Private
->LegacyRegion
->Lock (Private
->LegacyRegion
, 0xE0000, 0x20000, &Granularity
);
162 This function is called when copying data to the region assigned by
163 EFI_LEGACY_BIOS_PROTOCOL.GetLegacyRegion().
165 @param This Protocol instance pointer.
166 @param LegacyMemorySize Size of data to copy
167 @param LegacyMemoryAddress Legacy Region destination address Note: must
168 be in region assigned by
169 LegacyBiosGetLegacyRegion
170 @param LegacyMemorySourceAddress Source of data
172 @retval EFI_SUCCESS The data was copied successfully.
173 @retval EFI_ACCESS_DENIED Either the starting or ending address is out of bounds.
177 LegacyBiosCopyLegacyRegion (
178 IN EFI_LEGACY_BIOS_PROTOCOL
*This
,
179 IN UINTN LegacyMemorySize
,
180 IN VOID
*LegacyMemoryAddress
,
181 IN VOID
*LegacyMemorySourceAddress
185 LEGACY_BIOS_INSTANCE
*Private
;
188 if ((LegacyMemoryAddress
< (VOID
*)(UINTN
)0xE0000 ) ||
189 ((UINTN
) LegacyMemoryAddress
+ LegacyMemorySize
> (UINTN
) 0x100000)
191 return EFI_ACCESS_DENIED
;
194 // There is no protection from writes over lapping if this function is
195 // called multiple times.
197 Private
= LEGACY_BIOS_INSTANCE_FROM_THIS (This
);
198 Private
->LegacyRegion
->UnLock (Private
->LegacyRegion
, 0xE0000, 0x20000, &Granularity
);
199 CopyMem (LegacyMemoryAddress
, LegacyMemorySourceAddress
, LegacyMemorySize
);
201 Private
->Cpu
->FlushDataCache (Private
->Cpu
, 0xE0000, 0x20000, EfiCpuFlushTypeWriteBackInvalidate
);
202 Private
->LegacyRegion
->Lock (Private
->LegacyRegion
, 0xE0000, 0x20000, &Granularity
);
209 Find Legacy16 BIOS image in the FLASH device and shadow it into memory. Find
210 the $EFI table in the shadow area. Thunk into the Legacy16 code after it had
213 @param Private Legacy BIOS context data
215 @retval EFI_SUCCESS Legacy16 code loaded
216 @retval Other No protocol installed, unload driver.
220 ShadowAndStartLegacy16 (
221 IN LEGACY_BIOS_INSTANCE
*Private
228 EFI_COMPATIBILITY16_TABLE
*Table
;
230 EFI_IA32_REGISTER_SET Regs
;
231 EFI_TO_COMPATIBILITY16_INIT_TABLE
*EfiToLegacy16InitTable
;
232 EFI_TO_COMPATIBILITY16_BOOT_TABLE
*EfiToLegacy16BootTable
;
233 VOID
*LegacyBiosImage
;
234 UINTN LegacyBiosImageSize
;
238 LEGACY_EFI_HDD_TABLE
*LegacyEfiHddTable
;
241 VOID
*TpmBinaryImage
;
242 UINTN TpmBinaryImageSize
;
246 EFI_PHYSICAL_ADDRESS Address
;
250 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor
;
256 // we allocate the C/D/E/F segment as RT code so no one will use it any more.
259 gDS
->GetMemorySpaceDescriptor (Address
, &Descriptor
);
260 if (Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) {
262 // If it is already reserved, we should be safe, or else we allocate it.
264 Status
= gBS
->AllocatePages (
266 EfiRuntimeServicesCode
,
267 0x40000/EFI_PAGE_SIZE
,
270 if (EFI_ERROR (Status
)) {
272 // Bugbug: need to figure out whether C/D/E/F segment should be marked as reserved memory.
274 DEBUG ((DEBUG_ERROR
, "Failed to allocate the C/D/E/F segment Status = %r", Status
));
280 // GetTimerValue (&Ticker);
282 // gRT->SetVariable (L"StartLegacy",
283 // &gEfiGlobalVariableGuid,
284 // EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
290 EfiToLegacy16BootTable
= &Private
->IntThunk
->EfiToLegacy16BootTable
;
291 Status
= Private
->LegacyBiosPlatform
->GetPlatformInfo (
292 Private
->LegacyBiosPlatform
,
293 EfiGetPlatformBinarySystemRom
,
295 &LegacyBiosImageSize
,
301 if (EFI_ERROR (Status
)) {
305 Private
->BiosStart
= (UINT32
) (0x100000 - LegacyBiosImageSize
);
306 Private
->OptionRom
= 0xc0000;
307 Private
->LegacyBiosImageSize
= (UINT32
) LegacyBiosImageSize
;
310 // Can only shadow into memory allocated for legacy useage.
312 ASSERT (Private
->BiosStart
> Private
->OptionRom
);
315 // Shadow Legacy BIOS. Turn on memory and copy image
317 Private
->LegacyRegion
->UnLock (Private
->LegacyRegion
, 0xc0000, 0x40000, &Granularity
);
319 ClearPtr
= (VOID
*) ((UINTN
) 0xc0000);
322 // Initialize region from 0xc0000 to start of BIOS to all ffs. This allows unused
323 // regions to be used by EMM386 etc.
325 SetMem ((VOID
*) ClearPtr
, (UINTN
) (0x40000 - LegacyBiosImageSize
), 0xff);
327 TempData
= Private
->BiosStart
;
332 (UINTN
) LegacyBiosImageSize
335 Private
->Cpu
->FlushDataCache (Private
->Cpu
, 0xc0000, 0x40000, EfiCpuFlushTypeWriteBackInvalidate
);
338 // Search for Legacy16 table in Shadowed ROM
342 for (Ptr
= (UINT8
*) TempData
; Ptr
< (UINT8
*) ((UINTN
) 0x100000) && !Done
; Ptr
+= 0x10) {
343 if (*(UINT32
*) Ptr
== SIGNATURE_32 ('I', 'F', 'E', '$')) {
344 Table
= (EFI_COMPATIBILITY16_TABLE
*) Ptr
;
345 PtrEnd
= Ptr
+ Table
->TableLength
;
346 for (CheckSum
= 0; Ptr
< PtrEnd
; Ptr
++) {
347 CheckSum
= (UINT8
) (CheckSum
+*Ptr
);
355 DEBUG ((EFI_D_ERROR
, "No Legacy16 table found\n"));
356 return EFI_NOT_FOUND
;
361 // Legacy16 table header checksum error.
363 DEBUG ((EFI_D_ERROR
, "Legacy16 table found with bad talbe header checksum\n"));
367 // Remember location of the Legacy16 table
369 Private
->Legacy16Table
= Table
;
370 Private
->Legacy16CallSegment
= Table
->Compatibility16CallSegment
;
371 Private
->Legacy16CallOffset
= Table
->Compatibility16CallOffset
;
372 EfiToLegacy16InitTable
= &Private
->IntThunk
->EfiToLegacy16InitTable
;
373 Private
->Legacy16InitPtr
= EfiToLegacy16InitTable
;
374 Private
->Legacy16BootPtr
= &Private
->IntThunk
->EfiToLegacy16BootTable
;
375 Private
->InternalIrqRoutingTable
= NULL
;
376 Private
->NumberIrqRoutingEntries
= 0;
377 Private
->BbsTablePtr
= NULL
;
378 Private
->LegacyEfiHddTable
= NULL
;
379 Private
->DiskEnd
= 0;
380 Private
->Disk4075
= 0;
381 Private
->HddTablePtr
= &Private
->IntThunk
->EfiToLegacy16BootTable
.HddInfo
;
382 Private
->NumberHddControllers
= MAX_IDE_CONTROLLER
;
383 Private
->Dump
[0] = 'D';
384 Private
->Dump
[1] = 'U';
385 Private
->Dump
[2] = 'M';
386 Private
->Dump
[3] = 'P';
389 Private
->Legacy16BootPtr
,
390 sizeof (EFI_TO_COMPATIBILITY16_BOOT_TABLE
)
394 // Store away a copy of the EFI System Table
396 Table
->EfiSystemTable
= (UINT32
) (UINTN
) gST
;
399 // IPF CSM integration -Bug
401 // Construct the Legacy16 boot memory map. This sets up number of
404 LegacyBiosBuildE820 (Private
, &E820Size
);
406 // Initialize BDA and EBDA standard values needed to load Legacy16 code
408 LegacyBiosInitBda (Private
);
409 LegacyBiosInitCmos (Private
);
412 // All legacy interrupt should be masked when do initialization work from legacy 16 code.
414 Private
->Legacy8259
->GetMask(Private
->Legacy8259
, &OldMask
, NULL
, NULL
, NULL
);
416 Private
->Legacy8259
->SetMask(Private
->Legacy8259
, &NewMask
, NULL
, NULL
, NULL
);
419 // Call into Legacy16 code to do an INIT
421 ZeroMem (&Regs
, sizeof (EFI_IA32_REGISTER_SET
));
422 Regs
.X
.AX
= Legacy16InitializeYourself
;
423 Regs
.X
.ES
= EFI_SEGMENT (*((UINT32
*) &EfiToLegacy16InitTable
));
424 Regs
.X
.BX
= EFI_OFFSET (*((UINT32
*) &EfiToLegacy16InitTable
));
426 Private
->LegacyBios
.FarCall86 (
427 &Private
->LegacyBios
,
428 Table
->Compatibility16CallSegment
,
429 Table
->Compatibility16CallOffset
,
436 // Restore original legacy interrupt mask value
438 Private
->Legacy8259
->SetMask(Private
->Legacy8259
, &OldMask
, NULL
, NULL
, NULL
);
440 if (Regs
.X
.AX
!= 0) {
441 return EFI_DEVICE_ERROR
;
446 // GetTimerValue (&Ticker);
448 // gRT->SetVariable (L"BackFromInitYourself",
449 // &gEfiGlobalVariableGuid,
450 // EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
456 // Copy E820 table after InitializeYourself is completed
458 ZeroMem (&Regs
, sizeof (EFI_IA32_REGISTER_SET
));
459 Regs
.X
.AX
= Legacy16GetTableAddress
;
460 Regs
.X
.CX
= (UINT16
) E820Size
;
462 Private
->LegacyBios
.FarCall86 (
463 &Private
->LegacyBios
,
464 Table
->Compatibility16CallSegment
,
465 Table
->Compatibility16CallOffset
,
471 Table
->E820Pointer
= (UINT32
) (Regs
.X
.DS
* 16 + Regs
.X
.BX
);
472 Table
->E820Length
= (UINT32
) E820Size
;
473 if (Regs
.X
.AX
!= 0) {
474 DEBUG ((EFI_D_ERROR
, "Legacy16 E820 length insufficient\n"));
476 TempData
= Table
->E820Pointer
;
477 CopyMem ((VOID
*) TempData
, Private
->E820Table
, E820Size
);
480 // Get PnPInstallationCheck Info.
482 Private
->PnPInstallationCheckSegment
= Table
->PnPInstallationCheckSegment
;
483 Private
->PnPInstallationCheckOffset
= Table
->PnPInstallationCheckOffset
;
486 // Check if PCI Express is supported. If yes, Save base address.
488 Status
= Private
->LegacyBiosPlatform
->GetPlatformInfo (
489 Private
->LegacyBiosPlatform
,
490 EfiGetPlatformPciExpressBase
,
498 if (!EFI_ERROR (Status
)) {
499 Private
->Legacy16Table
->PciExpressBase
= (UINT32
)Location
;
503 // Check if TPM is supported. If yes get a region in E0000,F0000 to copy it
504 // into, copy it and update pointer to binary image. This needs to be
505 // done prior to any OPROM for security purposes.
507 Status
= Private
->LegacyBiosPlatform
->GetPlatformInfo (
508 Private
->LegacyBiosPlatform
,
509 EfiGetPlatformBinaryTpmBinary
,
517 if (!EFI_ERROR (Status
)) {
519 ZeroMem (&Regs
, sizeof (EFI_IA32_REGISTER_SET
));
520 Regs
.X
.AX
= Legacy16GetTableAddress
;
521 Regs
.X
.CX
= (UINT16
) TpmBinaryImageSize
;
523 Private
->LegacyBios
.FarCall86 (
524 &Private
->LegacyBios
,
525 Table
->Compatibility16CallSegment
,
526 Table
->Compatibility16CallOffset
,
532 TpmPointer
= (UINT32
) (Regs
.X
.DS
* 16 + Regs
.X
.BX
);
533 if (Regs
.X
.AX
!= 0) {
534 DEBUG ((EFI_D_ERROR
, "TPM cannot be loaded\n"));
536 CopyMem ((VOID
*) (UINTN
)TpmPointer
, TpmBinaryImage
, TpmBinaryImageSize
);
537 Table
->TpmSegment
= Regs
.X
.DS
;
538 Table
->TpmOffset
= Regs
.X
.BX
;
543 // Lock the Legacy BIOS region
545 Private
->Cpu
->FlushDataCache (Private
->Cpu
, Private
->BiosStart
, (UINT32
) LegacyBiosImageSize
, EfiCpuFlushTypeWriteBackInvalidate
);
546 Private
->LegacyRegion
->Lock (Private
->LegacyRegion
, Private
->BiosStart
, (UINT32
) LegacyBiosImageSize
, &Granularity
);
549 // Get the BbsTable from LOW_MEMORY_THUNK
551 BbsTable
= (BBS_TABLE
*)(UINTN
)Private
->IntThunk
->BbsTable
;
552 ZeroMem ((VOID
*)BbsTable
, sizeof (Private
->IntThunk
->BbsTable
));
554 EfiToLegacy16BootTable
->BbsTable
= (UINT32
)(UINTN
)BbsTable
;
555 Private
->BbsTablePtr
= (VOID
*) BbsTable
;
557 // Skip Floppy and possible onboard IDE drives
559 EfiToLegacy16BootTable
->NumberBbsEntries
= 1 + 2 * MAX_IDE_CONTROLLER
;
561 for (Index
= 0; Index
< (sizeof (Private
->IntThunk
->BbsTable
) / sizeof (BBS_TABLE
)); Index
++) {
562 BbsTable
[Index
].BootPriority
= BBS_IGNORE_ENTRY
;
565 // Allocate space for Legacy HDD table
567 LegacyEfiHddTable
= (LEGACY_EFI_HDD_TABLE
*) AllocateZeroPool ((UINTN
) MAX_HDD_ENTRIES
* sizeof (LEGACY_EFI_HDD_TABLE
));
568 ASSERT (LegacyEfiHddTable
);
570 Private
->LegacyEfiHddTable
= LegacyEfiHddTable
;
571 Private
->LegacyEfiHddTableIndex
= 0x00;
575 // GetTimerValue (&Ticker);
577 // gRT->SetVariable (L"EndOfLoadFv",
578 // &gEfiGlobalVariableGuid,
579 // EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
589 Shadow all legacy16 OPROMs that haven't been shadowed.
590 Warning: Use this with caution. This routine disconnects all EFI
591 drivers. If used externally then caller must re-connect EFI
594 @param This Protocol instance pointer.
596 @retval EFI_SUCCESS OPROMs shadowed
601 LegacyBiosShadowAllLegacyOproms (
602 IN EFI_LEGACY_BIOS_PROTOCOL
*This
605 LEGACY_BIOS_INSTANCE
*Private
;
608 // EFI_LEGACY_BIOS_PLATFORM_PROTOCOL *LegacyBiosPlatform;
609 // EFI_LEGACY16_TABLE *Legacy16Table;
611 Private
= LEGACY_BIOS_INSTANCE_FROM_THIS (This
);
614 // LegacyBiosPlatform = Private->LegacyBiosPlatform;
615 // Legacy16Table = Private->Legacy16Table;
617 // Shadow PCI ROMs. We must do this near the end since this will kick
618 // of Native EFI drivers that may be needed to collect info for Legacy16
620 // WARNING: PciIo is gone after this call.
622 PciProgramAllInterruptLineRegisters (Private
);
624 PciShadowRoms (Private
);
627 // Shadow PXE base code, BIS etc.
629 // LegacyBiosPlatform->ShadowServiceRoms (LegacyBiosPlatform,
630 // &Private->OptionRom,
637 Get the PCI BIOS interface version.
639 @param Private Driver private data.
641 @return The PCI interface version number in Binary Coded Decimal (BCD) format.
642 E.g.: 0x0210 indicates 2.10, 0x0300 indicates 3.00
646 GetPciInterfaceVersion (
647 IN LEGACY_BIOS_INSTANCE
*Private
650 EFI_IA32_REGISTER_SET Reg
;
652 UINT16 PciInterfaceVersion
;
654 PciInterfaceVersion
= 0;
659 ThunkFailed
= Private
->LegacyBios
.Int86 (&Private
->LegacyBios
, 0x1A, &Reg
);
662 // From PCI Firmware 3.0 Specification:
663 // If the CARRY FLAG [CF] is cleared and AH is set to 00h, it is still necessary to examine the
664 // contents of [EDX] for the presence of the string "PCI" + (trailing space) to fully validate the
665 // presence of the PCI function set. [BX] will further indicate the version level, with enough
666 // granularity to allow for incremental changes in the code that don't affect the function interface.
667 // Version numbers are stored as Binary Coded Decimal (BCD) values. For example, Version 2.10
668 // would be returned as a 02h in the [BH] registers and 10h in the [BL] registers.
670 if ((Reg
.X
.Flags
.CF
== 0) && (Reg
.H
.AH
== 0) && (Reg
.E
.EDX
== SIGNATURE_32 ('P', 'C', 'I', ' '))) {
671 PciInterfaceVersion
= Reg
.X
.BX
;
674 return PciInterfaceVersion
;
678 Callback function to calculate SMBIOS table size, and allocate memory for SMBIOS table.
679 SMBIOS table will be copied into EfiReservedMemoryType memory in legacy boot path.
681 @param Event Event whose notification function is being invoked.
682 @param Context The pointer to the notification function's context,
683 which is implementation-dependent.
688 InstallSmbiosEventCallback (
694 SMBIOS_TABLE_ENTRY_POINT
*EntryPointStructure
;
697 // Get SMBIOS table from EFI configuration table
699 Status
= EfiGetSystemConfigurationTable (
700 &gEfiSmbiosTableGuid
,
701 &mRuntimeSmbiosEntryPoint
703 if ((EFI_ERROR (Status
)) || (mRuntimeSmbiosEntryPoint
== NULL
)) {
707 EntryPointStructure
= (SMBIOS_TABLE_ENTRY_POINT
*) mRuntimeSmbiosEntryPoint
;
710 // Allocate memory for SMBIOS Entry Point Structure.
711 // CSM framework spec requires SMBIOS table below 4GB in EFI_TO_COMPATIBILITY16_BOOT_TABLE.
713 if (mReserveSmbiosEntryPoint
== 0) {
715 // Entrypoint structure with fixed size is allocated only once.
717 mReserveSmbiosEntryPoint
= SIZE_4GB
- 1;
718 Status
= gBS
->AllocatePages (
720 EfiReservedMemoryType
,
721 EFI_SIZE_TO_PAGES ((UINTN
) (EntryPointStructure
->EntryPointLength
)),
722 &mReserveSmbiosEntryPoint
724 if (EFI_ERROR (Status
)) {
725 mReserveSmbiosEntryPoint
= 0;
728 DEBUG ((EFI_D_INFO
, "Allocate memory for Smbios Entry Point Structure\n"));
731 if ((mStructureTableAddress
!= 0) &&
732 (mStructureTablePages
< EFI_SIZE_TO_PAGES ((UINT32
)EntryPointStructure
->TableLength
))) {
734 // If original buffer is not enough for the new SMBIOS table, free original buffer and re-allocate
736 gBS
->FreePages (mStructureTableAddress
, mStructureTablePages
);
737 mStructureTableAddress
= 0;
738 mStructureTablePages
= 0;
739 DEBUG ((EFI_D_INFO
, "Original size is not enough. Re-allocate the memory.\n"));
742 if (mStructureTableAddress
== 0) {
744 // Allocate reserved memory below 4GB.
745 // Smbios spec requires the structure table is below 4GB.
747 mStructureTableAddress
= SIZE_4GB
- 1;
748 mStructureTablePages
= EFI_SIZE_TO_PAGES (EntryPointStructure
->TableLength
);
749 Status
= gBS
->AllocatePages (
751 EfiReservedMemoryType
,
752 mStructureTablePages
,
753 &mStructureTableAddress
755 if (EFI_ERROR (Status
)) {
757 mReserveSmbiosEntryPoint
,
758 EFI_SIZE_TO_PAGES ((UINTN
) (EntryPointStructure
->EntryPointLength
))
760 mReserveSmbiosEntryPoint
= 0;
761 mStructureTableAddress
= 0;
762 mStructureTablePages
= 0;
765 DEBUG ((EFI_D_INFO
, "Allocate memory for Smbios Structure Table\n"));
770 Callback function to toggle EndOfDxe status. NULL pointer detection needs
771 this status to decide if it's necessary to change attributes of page 0.
773 @param Event Event whose notification function is being invoked.
774 @param Context The pointer to the notification function's context,
775 which is implementation-dependent.
780 ToggleEndOfDxeStatus (
790 Install Driver to produce Legacy BIOS protocol.
792 @param ImageHandle Handle of driver image.
793 @param SystemTable Pointer to system table.
795 @retval EFI_SUCCESS Legacy BIOS protocol installed
796 @retval No protocol installed, unload driver.
802 IN EFI_HANDLE ImageHandle
,
803 IN EFI_SYSTEM_TABLE
*SystemTable
807 LEGACY_BIOS_INSTANCE
*Private
;
808 EFI_TO_COMPATIBILITY16_INIT_TABLE
*EfiToLegacy16InitTable
;
809 EFI_PHYSICAL_ADDRESS MemoryAddress
;
810 EFI_PHYSICAL_ADDRESS EbdaReservedBaseAddress
;
812 EFI_PHYSICAL_ADDRESS MemoryAddressUnder1MB
;
814 UINT32
*BaseVectorMaster
;
815 EFI_PHYSICAL_ADDRESS StartAddress
;
817 EFI_PHYSICAL_ADDRESS MemStart
;
822 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor
;
825 EFI_EVENT InstallSmbiosEvent
;
826 EFI_EVENT EndOfDxeEvent
;
829 // Load this driver's image to memory
831 Status
= RelocateImageUnder4GIfNeeded (ImageHandle
, SystemTable
);
832 if (EFI_ERROR (Status
)) {
837 // When UEFI Secure Boot is enabled, CSM module will not start any more.
840 GetEfiGlobalVariable2 (EFI_SECURE_BOOT_MODE_NAME
, (VOID
**)&SecureBoot
, NULL
);
841 if ((SecureBoot
!= NULL
) && (*SecureBoot
== SECURE_BOOT_MODE_ENABLE
)) {
842 FreePool (SecureBoot
);
843 return EFI_SECURITY_VIOLATION
;
846 if (SecureBoot
!= NULL
) {
847 FreePool (SecureBoot
);
850 Private
= &mPrivateData
;
851 ZeroMem (Private
, sizeof (LEGACY_BIOS_INSTANCE
));
854 // Grab a copy of all the protocols we depend on. Any error would
855 // be a dispatcher bug!.
857 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**) &Private
->Cpu
);
858 ASSERT_EFI_ERROR (Status
);
860 Status
= gBS
->LocateProtocol (&gEfiTimerArchProtocolGuid
, NULL
, (VOID
**) &Private
->Timer
);
861 ASSERT_EFI_ERROR (Status
);
863 Status
= gBS
->LocateProtocol (&gEfiLegacyRegion2ProtocolGuid
, NULL
, (VOID
**) &Private
->LegacyRegion
);
864 ASSERT_EFI_ERROR (Status
);
866 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosPlatformProtocolGuid
, NULL
, (VOID
**) &Private
->LegacyBiosPlatform
);
867 ASSERT_EFI_ERROR (Status
);
869 Status
= gBS
->LocateProtocol (&gEfiLegacy8259ProtocolGuid
, NULL
, (VOID
**) &Private
->Legacy8259
);
870 ASSERT_EFI_ERROR (Status
);
872 Status
= gBS
->LocateProtocol (&gEfiLegacyInterruptProtocolGuid
, NULL
, (VOID
**) &Private
->LegacyInterrupt
);
873 ASSERT_EFI_ERROR (Status
);
876 // Locate Memory Test Protocol if exists
878 Status
= gBS
->LocateProtocol (
879 &gEfiGenericMemTestProtocolGuid
,
881 (VOID
**) &Private
->GenericMemoryTest
883 ASSERT_EFI_ERROR (Status
);
886 // Make sure all memory from 0-640K is tested
888 for (StartAddress
= 0; StartAddress
< 0xa0000; ) {
889 gDS
->GetMemorySpaceDescriptor (StartAddress
, &Descriptor
);
890 if (Descriptor
.GcdMemoryType
!= EfiGcdMemoryTypeReserved
) {
891 StartAddress
= Descriptor
.BaseAddress
+ Descriptor
.Length
;
894 Length
= MIN (Descriptor
.Length
, 0xa0000 - StartAddress
);
895 Private
->GenericMemoryTest
->CompatibleRangeTest (
896 Private
->GenericMemoryTest
,
900 StartAddress
= StartAddress
+ Length
;
903 // Make sure all memory from 1MB to 16MB is tested and added to memory map
905 for (StartAddress
= BASE_1MB
; StartAddress
< BASE_16MB
; ) {
906 gDS
->GetMemorySpaceDescriptor (StartAddress
, &Descriptor
);
907 if (Descriptor
.GcdMemoryType
!= EfiGcdMemoryTypeReserved
) {
908 StartAddress
= Descriptor
.BaseAddress
+ Descriptor
.Length
;
911 Length
= MIN (Descriptor
.Length
, BASE_16MB
- StartAddress
);
912 Private
->GenericMemoryTest
->CompatibleRangeTest (
913 Private
->GenericMemoryTest
,
917 StartAddress
= StartAddress
+ Length
;
920 Private
->Signature
= LEGACY_BIOS_INSTANCE_SIGNATURE
;
922 Private
->LegacyBios
.Int86
= LegacyBiosInt86
;
923 Private
->LegacyBios
.FarCall86
= LegacyBiosFarCall86
;
924 Private
->LegacyBios
.CheckPciRom
= LegacyBiosCheckPciRom
;
925 Private
->LegacyBios
.InstallPciRom
= LegacyBiosInstallPciRom
;
926 Private
->LegacyBios
.LegacyBoot
= LegacyBiosLegacyBoot
;
927 Private
->LegacyBios
.UpdateKeyboardLedStatus
= LegacyBiosUpdateKeyboardLedStatus
;
928 Private
->LegacyBios
.GetBbsInfo
= LegacyBiosGetBbsInfo
;
929 Private
->LegacyBios
.ShadowAllLegacyOproms
= LegacyBiosShadowAllLegacyOproms
;
930 Private
->LegacyBios
.PrepareToBootEfi
= LegacyBiosPrepareToBootEfi
;
931 Private
->LegacyBios
.GetLegacyRegion
= LegacyBiosGetLegacyRegion
;
932 Private
->LegacyBios
.CopyLegacyRegion
= LegacyBiosCopyLegacyRegion
;
933 Private
->LegacyBios
.BootUnconventionalDevice
= LegacyBiosBootUnconventionalDevice
;
935 Private
->ImageHandle
= ImageHandle
;
938 // Enable read attribute of legacy region.
941 Private
->LegacyRegion
->Decode (
942 Private
->LegacyRegion
,
949 // Set Cachebility for legacy region
950 // BUGBUG: Comments about this legacy region cacheability setting
951 // This setting will make D865GCHProduction CSM Unhappy
953 if (PcdGetBool (PcdLegacyBiosCacheLegacyRegion
)) {
954 gDS
->SetMemorySpaceAttributes (
959 gDS
->SetMemorySpaceAttributes (
966 gDS
->SetMemorySpaceAttributes (
973 // Allocate 0 - 4K for real mode interupt vectors and BDA.
975 AllocateLegacyMemory (
981 ASSERT (MemoryAddress
== 0x000000000);
983 ClearPtr
= (VOID
*) ((UINTN
) 0x0000);
986 // Initialize region from 0x0000 to 4k. This initializes interrupt vector
990 gBS
->SetMem ((VOID
*) ClearPtr
, 0x400, INITIAL_VALUE_BELOW_1K
);
991 ZeroMem ((VOID
*) ((UINTN
)ClearPtr
+ 0x400), 0xC00);
995 // Allocate pages for OPROM usage
997 MemorySize
= PcdGet32 (PcdEbdaReservedMemorySize
);
998 ASSERT ((MemorySize
& 0xFFF) == 0);
1000 Status
= AllocateLegacyMemory (
1002 CONVENTIONAL_MEMORY_TOP
- MemorySize
,
1003 EFI_SIZE_TO_PAGES (MemorySize
),
1006 ASSERT_EFI_ERROR (Status
);
1008 ZeroMem ((VOID
*) ((UINTN
) MemoryAddress
), MemorySize
);
1011 // Allocate all 32k chunks from 0x60000 ~ 0x88000 for Legacy OPROMs that
1012 // don't use PMM but look for zeroed memory. Note that various non-BBS
1013 // OpROMs expect different areas to be free
1015 EbdaReservedBaseAddress
= MemoryAddress
;
1016 MemoryAddress
= PcdGet32 (PcdOpromReservedMemoryBase
);
1017 MemorySize
= PcdGet32 (PcdOpromReservedMemorySize
);
1019 // Check if base address and size for reserved memory are 4KB aligned.
1021 ASSERT ((MemoryAddress
& 0xFFF) == 0);
1022 ASSERT ((MemorySize
& 0xFFF) == 0);
1024 // Check if the reserved memory is below EBDA reserved range.
1026 ASSERT ((MemoryAddress
< EbdaReservedBaseAddress
) && ((MemoryAddress
+ MemorySize
- 1) < EbdaReservedBaseAddress
));
1027 for (MemStart
= MemoryAddress
; MemStart
< MemoryAddress
+ MemorySize
; MemStart
+= 0x1000) {
1028 Status
= AllocateLegacyMemory (
1034 if (!EFI_ERROR (Status
)) {
1035 MemoryPtr
= (VOID
*) ((UINTN
) StartAddress
);
1036 ZeroMem (MemoryPtr
, 0x1000);
1038 DEBUG ((EFI_D_ERROR
, "WARNING: Allocate legacy memory fail for SCSI card - %x\n", MemStart
));
1043 // Allocate low PMM memory and zero it out
1045 MemorySize
= PcdGet32 (PcdLowPmmMemorySize
);
1046 ASSERT ((MemorySize
& 0xFFF) == 0);
1047 Status
= AllocateLegacyMemory (
1049 CONVENTIONAL_MEMORY_TOP
,
1050 EFI_SIZE_TO_PAGES (MemorySize
),
1051 &MemoryAddressUnder1MB
1053 ASSERT_EFI_ERROR (Status
);
1055 ZeroMem ((VOID
*) ((UINTN
) MemoryAddressUnder1MB
), MemorySize
);
1058 // Allocate space for thunker and Init Thunker
1060 Status
= AllocateLegacyMemory (
1062 CONVENTIONAL_MEMORY_TOP
,
1063 (sizeof (LOW_MEMORY_THUNK
) / EFI_PAGE_SIZE
) + 2,
1066 ASSERT_EFI_ERROR (Status
);
1067 Private
->IntThunk
= (LOW_MEMORY_THUNK
*) (UINTN
) MemoryAddress
;
1068 EfiToLegacy16InitTable
= &Private
->IntThunk
->EfiToLegacy16InitTable
;
1069 EfiToLegacy16InitTable
->ThunkStart
= (UINT32
) (EFI_PHYSICAL_ADDRESS
) (UINTN
) MemoryAddress
;
1070 EfiToLegacy16InitTable
->ThunkSizeInBytes
= (UINT32
) (sizeof (LOW_MEMORY_THUNK
));
1072 Status
= LegacyBiosInitializeThunk (Private
);
1073 ASSERT_EFI_ERROR (Status
);
1076 // Init the legacy memory map in memory < 1 MB.
1078 EfiToLegacy16InitTable
->BiosLessThan1MB
= (UINT32
) MemoryAddressUnder1MB
;
1079 EfiToLegacy16InitTable
->LowPmmMemory
= (UINT32
) MemoryAddressUnder1MB
;
1080 EfiToLegacy16InitTable
->LowPmmMemorySizeInBytes
= MemorySize
;
1082 MemorySize
= PcdGet32 (PcdHighPmmMemorySize
);
1083 ASSERT ((MemorySize
& 0xFFF) == 0);
1085 // Allocate high PMM Memory under 16 MB
1087 Status
= AllocateLegacyMemory (
1090 EFI_SIZE_TO_PAGES (MemorySize
),
1093 if (EFI_ERROR (Status
)) {
1095 // If it fails, allocate high PMM Memory under 4GB
1097 Status
= AllocateLegacyMemory (
1100 EFI_SIZE_TO_PAGES (MemorySize
),
1104 if (!EFI_ERROR (Status
)) {
1105 EfiToLegacy16InitTable
->HiPmmMemory
= (UINT32
) (EFI_PHYSICAL_ADDRESS
) (UINTN
) MemoryAddress
;
1106 EfiToLegacy16InitTable
->HiPmmMemorySizeInBytes
= MemorySize
;
1112 // Start the Legacy BIOS;
1114 Status
= ShadowAndStartLegacy16 (Private
);
1115 if (EFI_ERROR (Status
)) {
1119 // Initialize interrupt redirection code and entries;
1120 // IDT Vectors 0x68-0x6f must be redirected to IDT Vectors 0x08-0x0f.
1123 Private
->IntThunk
->InterruptRedirectionCode
,
1124 (VOID
*) (UINTN
) InterruptRedirectionTemplate
,
1125 sizeof (Private
->IntThunk
->InterruptRedirectionCode
)
1129 // Save Unexpected interrupt vector so can restore it just prior to boot
1132 BaseVectorMaster
= (UINT32
*) (sizeof (UINT32
) * PROTECTED_MODE_BASE_VECTOR_MASTER
);
1133 Private
->BiosUnexpectedInt
= BaseVectorMaster
[0];
1134 IntRedirCode
= (UINT32
) (UINTN
) Private
->IntThunk
->InterruptRedirectionCode
;
1135 for (Index
= 0; Index
< 8; Index
++) {
1136 BaseVectorMaster
[Index
] = (EFI_SEGMENT (IntRedirCode
+ Index
* 4) << 16) | EFI_OFFSET (IntRedirCode
+ Index
* 4);
1143 Private
->ThunkSeg
= (UINT16
) (EFI_SEGMENT (IntRedirCode
));
1146 // Allocate reserved memory for SMBIOS table used in legacy boot if SMBIOS table exists
1148 InstallSmbiosEventCallback (NULL
, NULL
);
1151 // Create callback function to update the size of reserved memory after LegacyBiosDxe starts
1153 Status
= gBS
->CreateEventEx (
1156 InstallSmbiosEventCallback
,
1158 &gEfiSmbiosTableGuid
,
1161 ASSERT_EFI_ERROR (Status
);
1164 // Create callback to update status of EndOfDxe, which is needed by NULL
1165 // pointer detection
1167 Status
= gBS
->CreateEventEx (
1170 ToggleEndOfDxeStatus
,
1172 &gEfiEndOfDxeEventGroupGuid
,
1175 ASSERT_EFI_ERROR (Status
);
1178 // Make a new handle and install the protocol
1180 Private
->Handle
= NULL
;
1181 Status
= gBS
->InstallProtocolInterface (
1183 &gEfiLegacyBiosProtocolGuid
,
1184 EFI_NATIVE_INTERFACE
,
1185 &Private
->LegacyBios
1187 Private
->Csm16PciInterfaceVersion
= GetPciInterfaceVersion (Private
);
1189 DEBUG ((EFI_D_INFO
, "CSM16 PCI BIOS Interface Version: %02x.%02x\n",
1190 (UINT8
) (Private
->Csm16PciInterfaceVersion
>> 8),
1191 (UINT8
) Private
->Csm16PciInterfaceVersion
1193 ASSERT (Private
->Csm16PciInterfaceVersion
!= 0);