3 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "LegacyBiosInterface.h"
11 #define PHYSICAL_ADDRESS_TO_POINTER(Address) ((VOID *) ((UINTN) Address))
14 // define maximum number of HDD system supports
16 #define MAX_HDD_ENTRIES 0x30
20 // Since this driver will only ever produce one instance of the Private Data
21 // protocol you are not required to dynamically allocate the PrivateData.
23 LEGACY_BIOS_INSTANCE mPrivateData
;
26 // The SMBIOS table in EfiRuntimeServicesData memory
28 VOID
*mRuntimeSmbiosEntryPoint
= NULL
;
31 // The SMBIOS table in EfiReservedMemoryType memory
33 EFI_PHYSICAL_ADDRESS mReserveSmbiosEntryPoint
= 0;
34 EFI_PHYSICAL_ADDRESS mStructureTableAddress
= 0;
35 UINTN mStructureTablePages
= 0;
36 BOOLEAN mEndOfDxe
= FALSE
;
39 Allocate memory for legacy usage. The memory is executable.
41 @param AllocateType The type of allocation to perform.
42 @param MemoryType The type of memory to allocate.
43 @param StartPageAddress Start address of range
44 @param Pages Number of pages to allocate
45 @param Result Result of allocation
47 @retval EFI_SUCCESS Legacy memory is allocated successfully.
48 @retval Other Legacy memory is not allocated.
52 AllocateLegacyMemory (
53 IN EFI_ALLOCATE_TYPE AllocateType
,
54 IN EFI_MEMORY_TYPE MemoryType
,
55 IN EFI_PHYSICAL_ADDRESS StartPageAddress
,
57 OUT EFI_PHYSICAL_ADDRESS
*Result
61 EFI_PHYSICAL_ADDRESS MemPage
;
62 EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc
;
65 // Allocate Pages of memory less <= StartPageAddress
67 MemPage
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) StartPageAddress
;
68 Status
= gBS
->AllocatePages (
75 // Do not ASSERT on Status error but let caller decide since some cases
76 // memory is already taken but that is ok.
78 if (!EFI_ERROR (Status
)) {
79 if (MemoryType
!= EfiBootServicesCode
) {
81 // Make sure that the buffer can be used to store code.
83 Status
= gDS
->GetMemorySpaceDescriptor (MemPage
, &MemDesc
);
84 if (!EFI_ERROR (Status
) && (MemDesc
.Attributes
& EFI_MEMORY_XP
) != 0) {
85 Status
= gDS
->SetMemorySpaceAttributes (
87 EFI_PAGES_TO_SIZE (Pages
),
88 MemDesc
.Attributes
& (~EFI_MEMORY_XP
)
91 if (EFI_ERROR (Status
)) {
92 gBS
->FreePages (MemPage
, Pages
);
97 if (!EFI_ERROR (Status
)) {
98 *Result
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) MemPage
;
106 This function is called when EFI needs to reserve an area in the 0xE0000 or 0xF0000
109 Note: inconsistency with the Framework CSM spec. Per the spec, this function may be
110 invoked only once. This limitation is relaxed to allow multiple calls in this implementation.
112 @param This Protocol instance pointer.
113 @param LegacyMemorySize Size of required region
114 @param Region Region to use. 00 = Either 0xE0000 or 0xF0000
115 block Bit0 = 1 0xF0000 block Bit1 = 1 0xE0000
117 @param Alignment Address alignment. Bit mapped. First non-zero
118 bit from right is alignment.
119 @param LegacyMemoryAddress Region Assigned
121 @retval EFI_SUCCESS Region assigned
122 @retval EFI_ACCESS_DENIED Procedure previously invoked
123 @retval Other Region not assigned
128 LegacyBiosGetLegacyRegion (
129 IN EFI_LEGACY_BIOS_PROTOCOL
*This
,
130 IN UINTN LegacyMemorySize
,
133 OUT VOID
**LegacyMemoryAddress
137 LEGACY_BIOS_INSTANCE
*Private
;
138 EFI_IA32_REGISTER_SET Regs
;
142 Private
= LEGACY_BIOS_INSTANCE_FROM_THIS (This
);
143 Private
->LegacyRegion
->UnLock (Private
->LegacyRegion
, 0xE0000, 0x20000, &Granularity
);
145 ZeroMem (&Regs
, sizeof (EFI_IA32_REGISTER_SET
));
146 Regs
.X
.AX
= Legacy16GetTableAddress
;
147 Regs
.X
.BX
= (UINT16
) Region
;
148 Regs
.X
.CX
= (UINT16
) LegacyMemorySize
;
149 Regs
.X
.DX
= (UINT16
) Alignment
;
150 Private
->LegacyBios
.FarCall86 (
151 &Private
->LegacyBios
,
152 Private
->Legacy16CallSegment
,
153 Private
->Legacy16CallOffset
,
159 if (Regs
.X
.AX
== 0) {
160 *LegacyMemoryAddress
= (VOID
*) (((UINTN
) Regs
.X
.DS
<< 4) + Regs
.X
.BX
);
161 Status
= EFI_SUCCESS
;
163 Status
= EFI_OUT_OF_RESOURCES
;
166 Private
->Cpu
->FlushDataCache (Private
->Cpu
, 0xE0000, 0x20000, EfiCpuFlushTypeWriteBackInvalidate
);
167 Private
->LegacyRegion
->Lock (Private
->LegacyRegion
, 0xE0000, 0x20000, &Granularity
);
174 This function is called when copying data to the region assigned by
175 EFI_LEGACY_BIOS_PROTOCOL.GetLegacyRegion().
177 @param This Protocol instance pointer.
178 @param LegacyMemorySize Size of data to copy
179 @param LegacyMemoryAddress Legacy Region destination address Note: must
180 be in region assigned by
181 LegacyBiosGetLegacyRegion
182 @param LegacyMemorySourceAddress Source of data
184 @retval EFI_SUCCESS The data was copied successfully.
185 @retval EFI_ACCESS_DENIED Either the starting or ending address is out of bounds.
189 LegacyBiosCopyLegacyRegion (
190 IN EFI_LEGACY_BIOS_PROTOCOL
*This
,
191 IN UINTN LegacyMemorySize
,
192 IN VOID
*LegacyMemoryAddress
,
193 IN VOID
*LegacyMemorySourceAddress
197 LEGACY_BIOS_INSTANCE
*Private
;
200 if ((LegacyMemoryAddress
< (VOID
*)(UINTN
)0xE0000 ) ||
201 ((UINTN
) LegacyMemoryAddress
+ LegacyMemorySize
> (UINTN
) 0x100000)
203 return EFI_ACCESS_DENIED
;
206 // There is no protection from writes over lapping if this function is
207 // called multiple times.
209 Private
= LEGACY_BIOS_INSTANCE_FROM_THIS (This
);
210 Private
->LegacyRegion
->UnLock (Private
->LegacyRegion
, 0xE0000, 0x20000, &Granularity
);
211 CopyMem (LegacyMemoryAddress
, LegacyMemorySourceAddress
, LegacyMemorySize
);
213 Private
->Cpu
->FlushDataCache (Private
->Cpu
, 0xE0000, 0x20000, EfiCpuFlushTypeWriteBackInvalidate
);
214 Private
->LegacyRegion
->Lock (Private
->LegacyRegion
, 0xE0000, 0x20000, &Granularity
);
221 Find Legacy16 BIOS image in the FLASH device and shadow it into memory. Find
222 the $EFI table in the shadow area. Thunk into the Legacy16 code after it had
225 @param Private Legacy BIOS context data
227 @retval EFI_SUCCESS Legacy16 code loaded
228 @retval Other No protocol installed, unload driver.
232 ShadowAndStartLegacy16 (
233 IN LEGACY_BIOS_INSTANCE
*Private
240 EFI_COMPATIBILITY16_TABLE
*Table
;
242 EFI_IA32_REGISTER_SET Regs
;
243 EFI_TO_COMPATIBILITY16_INIT_TABLE
*EfiToLegacy16InitTable
;
244 EFI_TO_COMPATIBILITY16_BOOT_TABLE
*EfiToLegacy16BootTable
;
245 VOID
*LegacyBiosImage
;
246 UINTN LegacyBiosImageSize
;
250 LEGACY_EFI_HDD_TABLE
*LegacyEfiHddTable
;
253 VOID
*TpmBinaryImage
;
254 UINTN TpmBinaryImageSize
;
258 EFI_PHYSICAL_ADDRESS Address
;
262 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor
;
268 // we allocate the C/D/E/F segment as RT code so no one will use it any more.
271 gDS
->GetMemorySpaceDescriptor (Address
, &Descriptor
);
272 if (Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) {
274 // If it is already reserved, we should be safe, or else we allocate it.
276 Status
= gBS
->AllocatePages (
278 EfiRuntimeServicesCode
,
279 0x40000/EFI_PAGE_SIZE
,
282 if (EFI_ERROR (Status
)) {
284 // Bugbug: need to figure out whether C/D/E/F segment should be marked as reserved memory.
286 DEBUG ((DEBUG_ERROR
, "Failed to allocate the C/D/E/F segment Status = %r", Status
));
292 // GetTimerValue (&Ticker);
294 // gRT->SetVariable (L"StartLegacy",
295 // &gEfiGlobalVariableGuid,
296 // EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
302 EfiToLegacy16BootTable
= &Private
->IntThunk
->EfiToLegacy16BootTable
;
303 Status
= Private
->LegacyBiosPlatform
->GetPlatformInfo (
304 Private
->LegacyBiosPlatform
,
305 EfiGetPlatformBinarySystemRom
,
307 &LegacyBiosImageSize
,
313 if (EFI_ERROR (Status
)) {
317 Private
->BiosStart
= (UINT32
) (0x100000 - LegacyBiosImageSize
);
318 Private
->OptionRom
= 0xc0000;
319 Private
->LegacyBiosImageSize
= (UINT32
) LegacyBiosImageSize
;
322 // Can only shadow into memory allocated for legacy usage.
324 ASSERT (Private
->BiosStart
> Private
->OptionRom
);
327 // Shadow Legacy BIOS. Turn on memory and copy image
329 Private
->LegacyRegion
->UnLock (Private
->LegacyRegion
, 0xc0000, 0x40000, &Granularity
);
331 ClearPtr
= (VOID
*) ((UINTN
) 0xc0000);
334 // Initialize region from 0xc0000 to start of BIOS to all ffs. This allows unused
335 // regions to be used by EMM386 etc.
337 SetMem ((VOID
*) ClearPtr
, (UINTN
) (0x40000 - LegacyBiosImageSize
), 0xff);
339 TempData
= Private
->BiosStart
;
344 (UINTN
) LegacyBiosImageSize
347 Private
->Cpu
->FlushDataCache (Private
->Cpu
, 0xc0000, 0x40000, EfiCpuFlushTypeWriteBackInvalidate
);
350 // Search for Legacy16 table in Shadowed ROM
354 for (Ptr
= (UINT8
*) TempData
; Ptr
< (UINT8
*) ((UINTN
) 0x100000) && !Done
; Ptr
+= 0x10) {
355 if (*(UINT32
*) Ptr
== SIGNATURE_32 ('I', 'F', 'E', '$')) {
356 Table
= (EFI_COMPATIBILITY16_TABLE
*) Ptr
;
357 PtrEnd
= Ptr
+ Table
->TableLength
;
358 for (CheckSum
= 0; Ptr
< PtrEnd
; Ptr
++) {
359 CheckSum
= (UINT8
) (CheckSum
+*Ptr
);
367 DEBUG ((DEBUG_ERROR
, "No Legacy16 table found\n"));
368 return EFI_NOT_FOUND
;
373 // Legacy16 table header checksum error.
375 DEBUG ((DEBUG_ERROR
, "Legacy16 table found with bad talbe header checksum\n"));
379 // Remember location of the Legacy16 table
381 Private
->Legacy16Table
= Table
;
382 Private
->Legacy16CallSegment
= Table
->Compatibility16CallSegment
;
383 Private
->Legacy16CallOffset
= Table
->Compatibility16CallOffset
;
384 EfiToLegacy16InitTable
= &Private
->IntThunk
->EfiToLegacy16InitTable
;
385 Private
->Legacy16InitPtr
= EfiToLegacy16InitTable
;
386 Private
->Legacy16BootPtr
= &Private
->IntThunk
->EfiToLegacy16BootTable
;
387 Private
->InternalIrqRoutingTable
= NULL
;
388 Private
->NumberIrqRoutingEntries
= 0;
389 Private
->BbsTablePtr
= NULL
;
390 Private
->LegacyEfiHddTable
= NULL
;
391 Private
->DiskEnd
= 0;
392 Private
->Disk4075
= 0;
393 Private
->HddTablePtr
= &Private
->IntThunk
->EfiToLegacy16BootTable
.HddInfo
;
394 Private
->NumberHddControllers
= MAX_IDE_CONTROLLER
;
395 Private
->Dump
[0] = 'D';
396 Private
->Dump
[1] = 'U';
397 Private
->Dump
[2] = 'M';
398 Private
->Dump
[3] = 'P';
401 Private
->Legacy16BootPtr
,
402 sizeof (EFI_TO_COMPATIBILITY16_BOOT_TABLE
)
406 // Store away a copy of the EFI System Table
408 Table
->EfiSystemTable
= (UINT32
) (UINTN
) gST
;
411 // IPF CSM integration -Bug
413 // Construct the Legacy16 boot memory map. This sets up number of
416 LegacyBiosBuildE820 (Private
, &E820Size
);
418 // Initialize BDA and EBDA standard values needed to load Legacy16 code
420 LegacyBiosInitBda (Private
);
421 LegacyBiosInitCmos (Private
);
424 // All legacy interrupt should be masked when do initialization work from legacy 16 code.
426 Private
->Legacy8259
->GetMask(Private
->Legacy8259
, &OldMask
, NULL
, NULL
, NULL
);
428 Private
->Legacy8259
->SetMask(Private
->Legacy8259
, &NewMask
, NULL
, NULL
, NULL
);
431 // Call into Legacy16 code to do an INIT
433 ZeroMem (&Regs
, sizeof (EFI_IA32_REGISTER_SET
));
434 Regs
.X
.AX
= Legacy16InitializeYourself
;
435 Regs
.X
.ES
= EFI_SEGMENT (*((UINT32
*) &EfiToLegacy16InitTable
));
436 Regs
.X
.BX
= EFI_OFFSET (*((UINT32
*) &EfiToLegacy16InitTable
));
438 Private
->LegacyBios
.FarCall86 (
439 &Private
->LegacyBios
,
440 Table
->Compatibility16CallSegment
,
441 Table
->Compatibility16CallOffset
,
448 // Restore original legacy interrupt mask value
450 Private
->Legacy8259
->SetMask(Private
->Legacy8259
, &OldMask
, NULL
, NULL
, NULL
);
452 if (Regs
.X
.AX
!= 0) {
453 return EFI_DEVICE_ERROR
;
458 // GetTimerValue (&Ticker);
460 // gRT->SetVariable (L"BackFromInitYourself",
461 // &gEfiGlobalVariableGuid,
462 // EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
468 // Copy E820 table after InitializeYourself is completed
470 ZeroMem (&Regs
, sizeof (EFI_IA32_REGISTER_SET
));
471 Regs
.X
.AX
= Legacy16GetTableAddress
;
472 Regs
.X
.CX
= (UINT16
) E820Size
;
474 Private
->LegacyBios
.FarCall86 (
475 &Private
->LegacyBios
,
476 Table
->Compatibility16CallSegment
,
477 Table
->Compatibility16CallOffset
,
483 Table
->E820Pointer
= (UINT32
) (Regs
.X
.DS
* 16 + Regs
.X
.BX
);
484 Table
->E820Length
= (UINT32
) E820Size
;
485 if (Regs
.X
.AX
!= 0) {
486 DEBUG ((DEBUG_ERROR
, "Legacy16 E820 length insufficient\n"));
488 TempData
= Table
->E820Pointer
;
489 CopyMem ((VOID
*) TempData
, Private
->E820Table
, E820Size
);
492 // Get PnPInstallationCheck Info.
494 Private
->PnPInstallationCheckSegment
= Table
->PnPInstallationCheckSegment
;
495 Private
->PnPInstallationCheckOffset
= Table
->PnPInstallationCheckOffset
;
498 // Check if PCI Express is supported. If yes, Save base address.
500 Status
= Private
->LegacyBiosPlatform
->GetPlatformInfo (
501 Private
->LegacyBiosPlatform
,
502 EfiGetPlatformPciExpressBase
,
510 if (!EFI_ERROR (Status
)) {
511 Private
->Legacy16Table
->PciExpressBase
= (UINT32
)Location
;
515 // Check if TPM is supported. If yes get a region in E0000,F0000 to copy it
516 // into, copy it and update pointer to binary image. This needs to be
517 // done prior to any OPROM for security purposes.
519 Status
= Private
->LegacyBiosPlatform
->GetPlatformInfo (
520 Private
->LegacyBiosPlatform
,
521 EfiGetPlatformBinaryTpmBinary
,
529 if (!EFI_ERROR (Status
)) {
531 ZeroMem (&Regs
, sizeof (EFI_IA32_REGISTER_SET
));
532 Regs
.X
.AX
= Legacy16GetTableAddress
;
533 Regs
.X
.CX
= (UINT16
) TpmBinaryImageSize
;
535 Private
->LegacyBios
.FarCall86 (
536 &Private
->LegacyBios
,
537 Table
->Compatibility16CallSegment
,
538 Table
->Compatibility16CallOffset
,
544 TpmPointer
= (UINT32
) (Regs
.X
.DS
* 16 + Regs
.X
.BX
);
545 if (Regs
.X
.AX
!= 0) {
546 DEBUG ((DEBUG_ERROR
, "TPM cannot be loaded\n"));
548 CopyMem ((VOID
*) (UINTN
)TpmPointer
, TpmBinaryImage
, TpmBinaryImageSize
);
549 Table
->TpmSegment
= Regs
.X
.DS
;
550 Table
->TpmOffset
= Regs
.X
.BX
;
555 // Lock the Legacy BIOS region
557 Private
->Cpu
->FlushDataCache (Private
->Cpu
, Private
->BiosStart
, (UINT32
) LegacyBiosImageSize
, EfiCpuFlushTypeWriteBackInvalidate
);
558 Private
->LegacyRegion
->Lock (Private
->LegacyRegion
, Private
->BiosStart
, (UINT32
) LegacyBiosImageSize
, &Granularity
);
561 // Get the BbsTable from LOW_MEMORY_THUNK
563 BbsTable
= (BBS_TABLE
*)(UINTN
)Private
->IntThunk
->BbsTable
;
564 ZeroMem ((VOID
*)BbsTable
, sizeof (Private
->IntThunk
->BbsTable
));
566 EfiToLegacy16BootTable
->BbsTable
= (UINT32
)(UINTN
)BbsTable
;
567 Private
->BbsTablePtr
= (VOID
*) BbsTable
;
570 // Populate entire table with BBS_IGNORE_ENTRY
572 EfiToLegacy16BootTable
->NumberBbsEntries
= MAX_BBS_ENTRIES
;
574 for (Index
= 0; Index
< MAX_BBS_ENTRIES
; Index
++) {
575 BbsTable
[Index
].BootPriority
= BBS_IGNORE_ENTRY
;
578 // Allocate space for Legacy HDD table
580 LegacyEfiHddTable
= (LEGACY_EFI_HDD_TABLE
*) AllocateZeroPool ((UINTN
) MAX_HDD_ENTRIES
* sizeof (LEGACY_EFI_HDD_TABLE
));
581 ASSERT (LegacyEfiHddTable
);
583 Private
->LegacyEfiHddTable
= LegacyEfiHddTable
;
584 Private
->LegacyEfiHddTableIndex
= 0x00;
588 // GetTimerValue (&Ticker);
590 // gRT->SetVariable (L"EndOfLoadFv",
591 // &gEfiGlobalVariableGuid,
592 // EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
602 Shadow all legacy16 OPROMs that haven't been shadowed.
603 Warning: Use this with caution. This routine disconnects all EFI
604 drivers. If used externally then caller must re-connect EFI
607 @param This Protocol instance pointer.
609 @retval EFI_SUCCESS OPROMs shadowed
614 LegacyBiosShadowAllLegacyOproms (
615 IN EFI_LEGACY_BIOS_PROTOCOL
*This
618 LEGACY_BIOS_INSTANCE
*Private
;
621 // EFI_LEGACY_BIOS_PLATFORM_PROTOCOL *LegacyBiosPlatform;
622 // EFI_LEGACY16_TABLE *Legacy16Table;
624 Private
= LEGACY_BIOS_INSTANCE_FROM_THIS (This
);
627 // LegacyBiosPlatform = Private->LegacyBiosPlatform;
628 // Legacy16Table = Private->Legacy16Table;
630 // Shadow PCI ROMs. We must do this near the end since this will kick
631 // of Native EFI drivers that may be needed to collect info for Legacy16
633 // WARNING: PciIo is gone after this call.
635 PciProgramAllInterruptLineRegisters (Private
);
637 PciShadowRoms (Private
);
640 // Shadow PXE base code, BIS etc.
642 // LegacyBiosPlatform->ShadowServiceRoms (LegacyBiosPlatform,
643 // &Private->OptionRom,
650 Get the PCI BIOS interface version.
652 @param Private Driver private data.
654 @return The PCI interface version number in Binary Coded Decimal (BCD) format.
655 E.g.: 0x0210 indicates 2.10, 0x0300 indicates 3.00
659 GetPciInterfaceVersion (
660 IN LEGACY_BIOS_INSTANCE
*Private
663 EFI_IA32_REGISTER_SET Reg
;
665 UINT16 PciInterfaceVersion
;
667 PciInterfaceVersion
= 0;
672 ThunkFailed
= Private
->LegacyBios
.Int86 (&Private
->LegacyBios
, 0x1A, &Reg
);
675 // From PCI Firmware 3.0 Specification:
676 // If the CARRY FLAG [CF] is cleared and AH is set to 00h, it is still necessary to examine the
677 // contents of [EDX] for the presence of the string "PCI" + (trailing space) to fully validate the
678 // presence of the PCI function set. [BX] will further indicate the version level, with enough
679 // granularity to allow for incremental changes in the code that don't affect the function interface.
680 // Version numbers are stored as Binary Coded Decimal (BCD) values. For example, Version 2.10
681 // would be returned as a 02h in the [BH] registers and 10h in the [BL] registers.
683 if ((Reg
.X
.Flags
.CF
== 0) && (Reg
.H
.AH
== 0) && (Reg
.E
.EDX
== SIGNATURE_32 ('P', 'C', 'I', ' '))) {
684 PciInterfaceVersion
= Reg
.X
.BX
;
687 return PciInterfaceVersion
;
691 Callback function to calculate SMBIOS table size, and allocate memory for SMBIOS table.
692 SMBIOS table will be copied into EfiReservedMemoryType memory in legacy boot path.
694 @param Event Event whose notification function is being invoked.
695 @param Context The pointer to the notification function's context,
696 which is implementation-dependent.
701 InstallSmbiosEventCallback (
707 SMBIOS_TABLE_ENTRY_POINT
*EntryPointStructure
;
710 // Get SMBIOS table from EFI configuration table
712 Status
= EfiGetSystemConfigurationTable (
713 &gEfiSmbiosTableGuid
,
714 &mRuntimeSmbiosEntryPoint
716 if ((EFI_ERROR (Status
)) || (mRuntimeSmbiosEntryPoint
== NULL
)) {
720 EntryPointStructure
= (SMBIOS_TABLE_ENTRY_POINT
*) mRuntimeSmbiosEntryPoint
;
723 // Allocate memory for SMBIOS Entry Point Structure.
724 // CSM framework spec requires SMBIOS table below 4GB in EFI_TO_COMPATIBILITY16_BOOT_TABLE.
726 if (mReserveSmbiosEntryPoint
== 0) {
728 // Entrypoint structure with fixed size is allocated only once.
730 mReserveSmbiosEntryPoint
= SIZE_4GB
- 1;
731 Status
= gBS
->AllocatePages (
733 EfiReservedMemoryType
,
734 EFI_SIZE_TO_PAGES ((UINTN
) (EntryPointStructure
->EntryPointLength
)),
735 &mReserveSmbiosEntryPoint
737 if (EFI_ERROR (Status
)) {
738 mReserveSmbiosEntryPoint
= 0;
741 DEBUG ((DEBUG_INFO
, "Allocate memory for Smbios Entry Point Structure\n"));
744 if ((mStructureTableAddress
!= 0) &&
745 (mStructureTablePages
< EFI_SIZE_TO_PAGES ((UINT32
)EntryPointStructure
->TableLength
))) {
747 // If original buffer is not enough for the new SMBIOS table, free original buffer and re-allocate
749 gBS
->FreePages (mStructureTableAddress
, mStructureTablePages
);
750 mStructureTableAddress
= 0;
751 mStructureTablePages
= 0;
752 DEBUG ((DEBUG_INFO
, "Original size is not enough. Re-allocate the memory.\n"));
755 if (mStructureTableAddress
== 0) {
757 // Allocate reserved memory below 4GB.
758 // Smbios spec requires the structure table is below 4GB.
760 mStructureTableAddress
= SIZE_4GB
- 1;
761 mStructureTablePages
= EFI_SIZE_TO_PAGES (EntryPointStructure
->TableLength
);
762 Status
= gBS
->AllocatePages (
764 EfiReservedMemoryType
,
765 mStructureTablePages
,
766 &mStructureTableAddress
768 if (EFI_ERROR (Status
)) {
770 mReserveSmbiosEntryPoint
,
771 EFI_SIZE_TO_PAGES ((UINTN
) (EntryPointStructure
->EntryPointLength
))
773 mReserveSmbiosEntryPoint
= 0;
774 mStructureTableAddress
= 0;
775 mStructureTablePages
= 0;
778 DEBUG ((DEBUG_INFO
, "Allocate memory for Smbios Structure Table\n"));
783 Callback function to toggle EndOfDxe status. NULL pointer detection needs
784 this status to decide if it's necessary to change attributes of page 0.
786 @param Event Event whose notification function is being invoked.
787 @param Context The pointer to the notification function's context,
788 which is implementation-dependent.
793 ToggleEndOfDxeStatus (
803 Install Driver to produce Legacy BIOS protocol.
805 @param ImageHandle Handle of driver image.
806 @param SystemTable Pointer to system table.
808 @retval EFI_SUCCESS Legacy BIOS protocol installed
809 @retval No protocol installed, unload driver.
815 IN EFI_HANDLE ImageHandle
,
816 IN EFI_SYSTEM_TABLE
*SystemTable
820 LEGACY_BIOS_INSTANCE
*Private
;
821 EFI_TO_COMPATIBILITY16_INIT_TABLE
*EfiToLegacy16InitTable
;
822 EFI_PHYSICAL_ADDRESS MemoryAddress
;
823 EFI_PHYSICAL_ADDRESS EbdaReservedBaseAddress
;
825 EFI_PHYSICAL_ADDRESS MemoryAddressUnder1MB
;
827 UINT32
*BaseVectorMaster
;
828 EFI_PHYSICAL_ADDRESS StartAddress
;
830 EFI_PHYSICAL_ADDRESS MemStart
;
835 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor
;
838 EFI_EVENT InstallSmbiosEvent
;
839 EFI_EVENT EndOfDxeEvent
;
842 // Load this driver's image to memory
844 Status
= RelocateImageUnder4GIfNeeded (ImageHandle
, SystemTable
);
845 if (EFI_ERROR (Status
)) {
850 // When UEFI Secure Boot is enabled, CSM module will not start any more.
853 GetEfiGlobalVariable2 (EFI_SECURE_BOOT_MODE_NAME
, (VOID
**)&SecureBoot
, NULL
);
854 if ((SecureBoot
!= NULL
) && (*SecureBoot
== SECURE_BOOT_MODE_ENABLE
)) {
855 FreePool (SecureBoot
);
856 return EFI_SECURITY_VIOLATION
;
859 if (SecureBoot
!= NULL
) {
860 FreePool (SecureBoot
);
863 Private
= &mPrivateData
;
864 ZeroMem (Private
, sizeof (LEGACY_BIOS_INSTANCE
));
867 // Grab a copy of all the protocols we depend on. Any error would
868 // be a dispatcher bug!.
870 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**) &Private
->Cpu
);
871 ASSERT_EFI_ERROR (Status
);
873 Status
= gBS
->LocateProtocol (&gEfiTimerArchProtocolGuid
, NULL
, (VOID
**) &Private
->Timer
);
874 ASSERT_EFI_ERROR (Status
);
876 Status
= gBS
->LocateProtocol (&gEfiLegacyRegion2ProtocolGuid
, NULL
, (VOID
**) &Private
->LegacyRegion
);
877 ASSERT_EFI_ERROR (Status
);
879 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosPlatformProtocolGuid
, NULL
, (VOID
**) &Private
->LegacyBiosPlatform
);
880 ASSERT_EFI_ERROR (Status
);
882 Status
= gBS
->LocateProtocol (&gEfiLegacy8259ProtocolGuid
, NULL
, (VOID
**) &Private
->Legacy8259
);
883 ASSERT_EFI_ERROR (Status
);
885 Status
= gBS
->LocateProtocol (&gEfiLegacyInterruptProtocolGuid
, NULL
, (VOID
**) &Private
->LegacyInterrupt
);
886 ASSERT_EFI_ERROR (Status
);
889 // Locate Memory Test Protocol if exists
891 Status
= gBS
->LocateProtocol (
892 &gEfiGenericMemTestProtocolGuid
,
894 (VOID
**) &Private
->GenericMemoryTest
896 ASSERT_EFI_ERROR (Status
);
899 // Make sure all memory from 0-640K is tested
901 for (StartAddress
= 0; StartAddress
< 0xa0000; ) {
902 gDS
->GetMemorySpaceDescriptor (StartAddress
, &Descriptor
);
903 if (Descriptor
.GcdMemoryType
!= EfiGcdMemoryTypeReserved
) {
904 StartAddress
= Descriptor
.BaseAddress
+ Descriptor
.Length
;
907 Length
= MIN (Descriptor
.Length
, 0xa0000 - StartAddress
);
908 Private
->GenericMemoryTest
->CompatibleRangeTest (
909 Private
->GenericMemoryTest
,
913 StartAddress
= StartAddress
+ Length
;
916 // Make sure all memory from 1MB to 16MB is tested and added to memory map
918 for (StartAddress
= BASE_1MB
; StartAddress
< BASE_16MB
; ) {
919 gDS
->GetMemorySpaceDescriptor (StartAddress
, &Descriptor
);
920 if (Descriptor
.GcdMemoryType
!= EfiGcdMemoryTypeReserved
) {
921 StartAddress
= Descriptor
.BaseAddress
+ Descriptor
.Length
;
924 Length
= MIN (Descriptor
.Length
, BASE_16MB
- StartAddress
);
925 Private
->GenericMemoryTest
->CompatibleRangeTest (
926 Private
->GenericMemoryTest
,
930 StartAddress
= StartAddress
+ Length
;
933 Private
->Signature
= LEGACY_BIOS_INSTANCE_SIGNATURE
;
935 Private
->LegacyBios
.Int86
= LegacyBiosInt86
;
936 Private
->LegacyBios
.FarCall86
= LegacyBiosFarCall86
;
937 Private
->LegacyBios
.CheckPciRom
= LegacyBiosCheckPciRom
;
938 Private
->LegacyBios
.InstallPciRom
= LegacyBiosInstallPciRom
;
939 Private
->LegacyBios
.LegacyBoot
= LegacyBiosLegacyBoot
;
940 Private
->LegacyBios
.UpdateKeyboardLedStatus
= LegacyBiosUpdateKeyboardLedStatus
;
941 Private
->LegacyBios
.GetBbsInfo
= LegacyBiosGetBbsInfo
;
942 Private
->LegacyBios
.ShadowAllLegacyOproms
= LegacyBiosShadowAllLegacyOproms
;
943 Private
->LegacyBios
.PrepareToBootEfi
= LegacyBiosPrepareToBootEfi
;
944 Private
->LegacyBios
.GetLegacyRegion
= LegacyBiosGetLegacyRegion
;
945 Private
->LegacyBios
.CopyLegacyRegion
= LegacyBiosCopyLegacyRegion
;
946 Private
->LegacyBios
.BootUnconventionalDevice
= LegacyBiosBootUnconventionalDevice
;
948 Private
->ImageHandle
= ImageHandle
;
951 // Enable read attribute of legacy region.
954 Private
->LegacyRegion
->Decode (
955 Private
->LegacyRegion
,
962 // Set Cachebility for legacy region
963 // BUGBUG: Comments about this legacy region cacheability setting
964 // This setting will make D865GCHProduction CSM Unhappy
966 if (PcdGetBool (PcdLegacyBiosCacheLegacyRegion
)) {
967 gDS
->SetMemorySpaceAttributes (
972 gDS
->SetMemorySpaceAttributes (
979 gDS
->SetMemorySpaceAttributes (
986 // Allocate 0 - 4K for real mode interrupt vectors and BDA.
988 AllocateLegacyMemory (
990 EfiReservedMemoryType
,
995 ASSERT (MemoryAddress
== 0x000000000);
997 ClearPtr
= (VOID
*) ((UINTN
) 0x0000);
1000 // Initialize region from 0x0000 to 4k. This initializes interrupt vector
1004 gBS
->SetMem ((VOID
*) ClearPtr
, 0x400, INITIAL_VALUE_BELOW_1K
);
1005 ZeroMem ((VOID
*) ((UINTN
)ClearPtr
+ 0x400), 0xC00);
1009 // Allocate pages for OPROM usage
1011 MemorySize
= PcdGet32 (PcdEbdaReservedMemorySize
);
1012 ASSERT ((MemorySize
& 0xFFF) == 0);
1014 Status
= AllocateLegacyMemory (
1016 EfiReservedMemoryType
,
1017 CONVENTIONAL_MEMORY_TOP
- MemorySize
,
1018 EFI_SIZE_TO_PAGES (MemorySize
),
1021 ASSERT_EFI_ERROR (Status
);
1023 ZeroMem ((VOID
*) ((UINTN
) MemoryAddress
), MemorySize
);
1026 // Allocate all 32k chunks from 0x60000 ~ 0x88000 for Legacy OPROMs that
1027 // don't use PMM but look for zeroed memory. Note that various non-BBS
1028 // OpROMs expect different areas to be free
1030 EbdaReservedBaseAddress
= MemoryAddress
;
1031 MemoryAddress
= PcdGet32 (PcdOpromReservedMemoryBase
);
1032 MemorySize
= PcdGet32 (PcdOpromReservedMemorySize
);
1034 // Check if base address and size for reserved memory are 4KB aligned.
1036 ASSERT ((MemoryAddress
& 0xFFF) == 0);
1037 ASSERT ((MemorySize
& 0xFFF) == 0);
1039 // Check if the reserved memory is below EBDA reserved range.
1041 ASSERT ((MemoryAddress
< EbdaReservedBaseAddress
) && ((MemoryAddress
+ MemorySize
- 1) < EbdaReservedBaseAddress
));
1042 for (MemStart
= MemoryAddress
; MemStart
< MemoryAddress
+ MemorySize
; MemStart
+= 0x1000) {
1043 Status
= AllocateLegacyMemory (
1045 EfiBootServicesCode
,
1050 if (!EFI_ERROR (Status
)) {
1051 MemoryPtr
= (VOID
*) ((UINTN
) StartAddress
);
1052 ZeroMem (MemoryPtr
, 0x1000);
1054 DEBUG ((DEBUG_ERROR
, "WARNING: Allocate legacy memory fail for SCSI card - %x\n", MemStart
));
1059 // Allocate low PMM memory and zero it out
1061 MemorySize
= PcdGet32 (PcdLowPmmMemorySize
);
1062 ASSERT ((MemorySize
& 0xFFF) == 0);
1063 Status
= AllocateLegacyMemory (
1065 EfiBootServicesCode
,
1066 CONVENTIONAL_MEMORY_TOP
,
1067 EFI_SIZE_TO_PAGES (MemorySize
),
1068 &MemoryAddressUnder1MB
1070 ASSERT_EFI_ERROR (Status
);
1072 ZeroMem ((VOID
*) ((UINTN
) MemoryAddressUnder1MB
), MemorySize
);
1075 // Allocate space for thunker and Init Thunker
1077 Status
= AllocateLegacyMemory (
1079 EfiReservedMemoryType
,
1080 CONVENTIONAL_MEMORY_TOP
,
1081 (sizeof (LOW_MEMORY_THUNK
) / EFI_PAGE_SIZE
) + 2,
1084 ASSERT_EFI_ERROR (Status
);
1085 Private
->IntThunk
= (LOW_MEMORY_THUNK
*) (UINTN
) MemoryAddress
;
1086 EfiToLegacy16InitTable
= &Private
->IntThunk
->EfiToLegacy16InitTable
;
1087 EfiToLegacy16InitTable
->ThunkStart
= (UINT32
) (EFI_PHYSICAL_ADDRESS
) (UINTN
) MemoryAddress
;
1088 EfiToLegacy16InitTable
->ThunkSizeInBytes
= (UINT32
) (sizeof (LOW_MEMORY_THUNK
));
1090 Status
= LegacyBiosInitializeThunk (Private
);
1091 ASSERT_EFI_ERROR (Status
);
1094 // Init the legacy memory map in memory < 1 MB.
1096 EfiToLegacy16InitTable
->BiosLessThan1MB
= (UINT32
) MemoryAddressUnder1MB
;
1097 EfiToLegacy16InitTable
->LowPmmMemory
= (UINT32
) MemoryAddressUnder1MB
;
1098 EfiToLegacy16InitTable
->LowPmmMemorySizeInBytes
= MemorySize
;
1100 MemorySize
= PcdGet32 (PcdHighPmmMemorySize
);
1101 ASSERT ((MemorySize
& 0xFFF) == 0);
1103 // Allocate high PMM Memory under 16 MB
1105 Status
= AllocateLegacyMemory (
1107 EfiBootServicesCode
,
1109 EFI_SIZE_TO_PAGES (MemorySize
),
1112 if (EFI_ERROR (Status
)) {
1114 // If it fails, allocate high PMM Memory under 4GB
1116 Status
= AllocateLegacyMemory (
1118 EfiBootServicesCode
,
1120 EFI_SIZE_TO_PAGES (MemorySize
),
1124 if (!EFI_ERROR (Status
)) {
1125 EfiToLegacy16InitTable
->HiPmmMemory
= (UINT32
) (EFI_PHYSICAL_ADDRESS
) (UINTN
) MemoryAddress
;
1126 EfiToLegacy16InitTable
->HiPmmMemorySizeInBytes
= MemorySize
;
1132 // Start the Legacy BIOS;
1134 Status
= ShadowAndStartLegacy16 (Private
);
1135 if (EFI_ERROR (Status
)) {
1139 // Initialize interrupt redirection code and entries;
1140 // IDT Vectors 0x68-0x6f must be redirected to IDT Vectors 0x08-0x0f.
1143 Private
->IntThunk
->InterruptRedirectionCode
,
1144 (VOID
*) (UINTN
) InterruptRedirectionTemplate
,
1145 sizeof (Private
->IntThunk
->InterruptRedirectionCode
)
1149 // Save Unexpected interrupt vector so can restore it just prior to boot
1152 BaseVectorMaster
= (UINT32
*) (sizeof (UINT32
) * PROTECTED_MODE_BASE_VECTOR_MASTER
);
1153 Private
->BiosUnexpectedInt
= BaseVectorMaster
[0];
1154 IntRedirCode
= (UINT32
) (UINTN
) Private
->IntThunk
->InterruptRedirectionCode
;
1155 for (Index
= 0; Index
< 8; Index
++) {
1156 BaseVectorMaster
[Index
] = (EFI_SEGMENT (IntRedirCode
+ Index
* 4) << 16) | EFI_OFFSET (IntRedirCode
+ Index
* 4);
1163 Private
->ThunkSeg
= (UINT16
) (EFI_SEGMENT (IntRedirCode
));
1166 // Allocate reserved memory for SMBIOS table used in legacy boot if SMBIOS table exists
1168 InstallSmbiosEventCallback (NULL
, NULL
);
1171 // Create callback function to update the size of reserved memory after LegacyBiosDxe starts
1173 Status
= gBS
->CreateEventEx (
1176 InstallSmbiosEventCallback
,
1178 &gEfiSmbiosTableGuid
,
1181 ASSERT_EFI_ERROR (Status
);
1184 // Create callback to update status of EndOfDxe, which is needed by NULL
1185 // pointer detection
1187 Status
= gBS
->CreateEventEx (
1190 ToggleEndOfDxeStatus
,
1192 &gEfiEndOfDxeEventGroupGuid
,
1195 ASSERT_EFI_ERROR (Status
);
1198 // Make a new handle and install the protocol
1200 Private
->Handle
= NULL
;
1201 Status
= gBS
->InstallProtocolInterface (
1203 &gEfiLegacyBiosProtocolGuid
,
1204 EFI_NATIVE_INTERFACE
,
1205 &Private
->LegacyBios
1207 Private
->Csm16PciInterfaceVersion
= GetPciInterfaceVersion (Private
);
1209 DEBUG ((DEBUG_INFO
, "CSM16 PCI BIOS Interface Version: %02x.%02x\n",
1210 (UINT8
) (Private
->Csm16PciInterfaceVersion
>> 8),
1211 (UINT8
) Private
->Csm16PciInterfaceVersion
1213 ASSERT (Private
->Csm16PciInterfaceVersion
!= 0);