Skip to content

Commit

Permalink
[UEFI] Enable UEFI boot for i386 and amd64
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkFire01 committed Aug 6, 2023
1 parent e4c4894 commit f4b171a
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 14 deletions.
6 changes: 0 additions & 6 deletions boot/freeldr/freeldr/arch/uefi/stubs.c
Expand Up @@ -43,12 +43,6 @@ UefiGetExtendedBIOSData(PULONG ExtendedBIOSDataArea,

}

PCONFIGURATION_COMPONENT_DATA
UefiHwDetect(VOID)
{
return 0;
}

VOID
UefiPcBeep(VOID)
{
Expand Down
1 change: 0 additions & 1 deletion boot/freeldr/freeldr/arch/uefi/ueficon.c
Expand Up @@ -138,6 +138,5 @@ UefiConsGetCh(VOID)
/* UEFI will stack input requests, we have to clear it */
Key.UnicodeChar = 0;
Key.ScanCode = 0;
GlobalSystemTable->ConIn->Reset(GlobalSystemTable->ConIn, FALSE);
return KeyOutput;
}
149 changes: 149 additions & 0 deletions boot/freeldr/freeldr/arch/uefi/uefihw.c
@@ -0,0 +1,149 @@
/*
* PROJECT: FreeLoader UEFI Support
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
* PURPOSE: Hardware detection routines
* COPYRIGHT: Copyright 2022 Justin Miller <justinmiller100@gmail.com>
*/

/* INCLUDES ******************************************************************/

#include <uefildr.h>

#include <debug.h>
DBG_DEFAULT_CHANNEL(WARNING);

/* GLOBALS *******************************************************************/

extern EFI_SYSTEM_TABLE * GlobalSystemTable;
extern EFI_HANDLE GlobalImageHandle;
extern UCHAR PcBiosDiskCount;
extern EFI_MEMORY_DESCRIPTOR* EfiMemoryMap;
extern UINT32 FreeldrDescCount;

BOOLEAN AcpiPresent = FALSE;

/* FUNCTIONS *****************************************************************/

static
PRSDP_DESCRIPTOR
FindAcpiBios(VOID)
{
UINTN i;
RSDP_DESCRIPTOR* rsdp = NULL;
EFI_GUID acpi2_guid = EFI_ACPI_20_TABLE_GUID;

for (i = 0; i < GlobalSystemTable->NumberOfTableEntries; i++)
{
if (!memcmp(&GlobalSystemTable->ConfigurationTable[i].VendorGuid, &acpi2_guid, sizeof(acpi2_guid)))
{
rsdp = (RSDP_DESCRIPTOR*)GlobalSystemTable->ConfigurationTable[i].VendorTable;
break;
}
}

return rsdp;
}

VOID
DetectAcpiBios(PCONFIGURATION_COMPONENT_DATA SystemKey, ULONG *BusNumber)
{
PCONFIGURATION_COMPONENT_DATA BiosKey;
PCM_PARTIAL_RESOURCE_LIST PartialResourceList;
PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor;
PRSDP_DESCRIPTOR Rsdp;
PACPI_BIOS_DATA AcpiBiosData;
ULONG TableSize;

Rsdp = FindAcpiBios();

if (Rsdp)
{
/* Set up the flag in the loader block */
AcpiPresent = TRUE;

/* Calculate the table size */
TableSize = FreeldrDescCount * sizeof(BIOS_MEMORY_MAP) +
sizeof(ACPI_BIOS_DATA) - sizeof(BIOS_MEMORY_MAP);

/* Set 'Configuration Data' value */
PartialResourceList = FrLdrHeapAlloc(sizeof(CM_PARTIAL_RESOURCE_LIST) +
TableSize, TAG_HW_RESOURCE_LIST);
if (PartialResourceList == NULL)
{
ERR("Failed to allocate resource descriptor\n");
return;
}

RtlZeroMemory(PartialResourceList, sizeof(CM_PARTIAL_RESOURCE_LIST) + TableSize);
PartialResourceList->Version = 0;
PartialResourceList->Revision = 0;
PartialResourceList->Count = 1;

PartialDescriptor = &PartialResourceList->PartialDescriptors[0];
PartialDescriptor->Type = CmResourceTypeDeviceSpecific;
PartialDescriptor->ShareDisposition = CmResourceShareUndetermined;
PartialDescriptor->u.DeviceSpecificData.DataSize = TableSize;

/* Fill the table */
AcpiBiosData = (PACPI_BIOS_DATA)&PartialResourceList->PartialDescriptors[1];

if (Rsdp->revision > 0)
{
TRACE("ACPI >1.0, using XSDT address\n");
AcpiBiosData->RSDTAddress.QuadPart = Rsdp->xsdt_physical_address;
}
else
{
TRACE("ACPI 1.0, using RSDT address\n");
AcpiBiosData->RSDTAddress.LowPart = Rsdp->rsdt_physical_address;
}

AcpiBiosData->Count = FreeldrDescCount;
memcpy(AcpiBiosData->MemoryMap, EfiMemoryMap,
FreeldrDescCount * sizeof(BIOS_MEMORY_MAP));

TRACE("RSDT %p, data size %x\n", Rsdp->rsdt_physical_address,
TableSize);

/* Create new bus key */
FldrCreateComponentKey(SystemKey,
AdapterClass,
MultiFunctionAdapter,
0x0,
0x0,
0xFFFFFFFF,
"ACPI BIOS",
PartialResourceList,
sizeof(CM_PARTIAL_RESOURCE_LIST) + TableSize,
&BiosKey);

/* Increment bus number */
(*BusNumber)++;
}
}

PCONFIGURATION_COMPONENT_DATA
UefiHwDetect(VOID)
{
PCONFIGURATION_COMPONENT_DATA SystemKey;
ULONG BusNumber = 0;

TRACE("DetectHardware()\n");

/* Create the 'System' key */
#if defined(_M_IX86) || defined(_M_AMD64)
FldrCreateSystemKey(&SystemKey, "AT/AT COMPATIBLE");
#elif defined(_M_IA64)
FldrCreateSystemKey(&SystemKey, "Intel Itanium processor family");
#elif defined(_M_ARM) || defined(_M_ARM64)
FldrCreateSystemKey(&SystemKey, "ARM processor family");
#else
#error Please define a system key for your architecture
#endif

/* Detect ACPI */
DetectAcpiBios(SystemKey, &BusNumber);

TRACE("DetectHardware() Done\n");
return SystemKey;
}
26 changes: 24 additions & 2 deletions boot/freeldr/freeldr/arch/uefi/uefimem.c
Expand Up @@ -27,6 +27,7 @@ AddMemoryDescriptor(

/* GLOBALS *******************************************************************/

extern ULONG LoaderPagesSpanned;
extern EFI_SYSTEM_TABLE* GlobalSystemTable;
extern EFI_HANDLE GlobalImageHandle;
extern REACTOS_INTERNAL_BGCONTEXT framebufferData;
Expand Down Expand Up @@ -256,6 +257,16 @@ UefiMemGetMemoryMap(ULONG *MemoryMapSize)
}
}

/* Sometimes our loader can be loaded into higher memory than we ever allocate */
if (MemoryType == LoaderLoadedProgram)
{
if ((MapEntry->PhysicalStart / EFI_PAGE_SIZE) > LoaderPagesSpanned)
{
/* This value needs to be adjusted if this occurs */
LoaderPagesSpanned = (MapEntry->PhysicalStart / EFI_PAGE_SIZE);
}
}

UefiSetMemory(FreeldrMem,
MapEntry->PhysicalStart,
MapEntry->NumberOfPages,
Expand Down Expand Up @@ -303,10 +314,21 @@ UefiExitBootServices(VOID)
}
}


BOOLEAN
MempSetupPaging(IN PFN_NUMBER StartPage,
IN PFN_NUMBER NumberOfPages,
IN BOOLEAN KernelMapping);

ULONG* StackPtrNew;
ULONG* StackPtrEnd;
VOID
UefiPrepareForReactOS(VOID)
{
ULONG StackVar = 0;
StackPtrNew = &StackVar;
StackPtrEnd = (&StackVar + 0x2000); /* Technically UEFI defines that stack space as 32kb*/
UefiExitBootServices();
ExitStack = MmAllocateMemoryWithType(EXIT_STACK_SIZE, LoaderOsloaderStack);
EndofExitStack = (PVOID)((ULONG_PTR)ExitStack + EXIT_STACK_SIZE);
//TODO: Let's take another look at this
MempSetupPaging((ULONG_PTR)StackPtrEnd >> MM_PAGE_SHIFT, 0x2000 >> MM_PAGE_SHIFT, FALSE);
}
1 change: 0 additions & 1 deletion boot/freeldr/freeldr/arch/uefi/uefisetup.c
Expand Up @@ -14,7 +14,6 @@ DBG_DEFAULT_CHANNEL(WARNING);

extern EFI_SYSTEM_TABLE* GlobalSystemTable;
extern EFI_HANDLE GlobalImageHandle;
BOOLEAN AcpiPresent = FALSE;

/* FUNCTIONS ******************************************************************/

Expand Down
1 change: 1 addition & 0 deletions boot/freeldr/freeldr/ntldr/winldr.h
Expand Up @@ -172,6 +172,7 @@ MempSetupPaging(IN PFN_NUMBER StartPage,
IN PFN_NUMBER NumberOfPages,
IN BOOLEAN KernelMapping);


VOID
MempUnmapPage(PFN_NUMBER Page);

Expand Down
8 changes: 7 additions & 1 deletion boot/freeldr/freeldr/ntldr/wlmemory.c
Expand Up @@ -176,7 +176,10 @@ MempSetupPagingForRegion(
#ifdef _M_ARM
#define PKTSS PVOID
#endif

#ifdef UEFIBOOT
extern PVOID OsLoaderBase;
extern SIZE_T OsLoaderSize;
#endif
BOOLEAN
WinLdrSetupMemoryLayout(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock)
{
Expand Down Expand Up @@ -330,6 +333,9 @@ WinLdrSetupMemoryLayout(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock)

WinLdrpDumpMemoryDescriptors(LoaderBlock); //FIXME: Delete!

#ifdef UEFIBOOT
MempSetupPaging((ULONG_PTR)OsLoaderBase >> MM_PAGE_SHIFT, OsLoaderSize >> MM_PAGE_SHIFT, FALSE);
#endif
// Map our loader image, so we can continue running
/*Status = MempSetupPaging(OsLoaderBase >> MM_PAGE_SHIFT, OsLoaderSize >> MM_PAGE_SHIFT);
if (!Status)
Expand Down
4 changes: 1 addition & 3 deletions boot/freeldr/freeldr/uefi.cmake
Expand Up @@ -16,6 +16,7 @@ list(APPEND UEFILDR_ARC_SOURCE
arch/uefi/stubs.c
arch/uefi/ueficon.c
arch/uefi/uefidisk.c
arch/uefi/uefihw.c
arch/uefi/uefimem.c
arch/uefi/uefisetup.c
arch/uefi/uefiutil.c
Expand All @@ -25,9 +26,6 @@ list(APPEND UEFILDR_ARC_SOURCE
if(ARCH STREQUAL "i386")
list(APPEND UEFILDR_COMMON_ASM_SOURCE
arch/i386/i386trap.S)

elseif(ARCH STREQUAL "amd64")
#TBD
elseif(ARCH STREQUAL "arm")
list(APPEND UEFILDR_ARC_SOURCE
arch/arm/macharm.c
Expand Down

0 comments on commit f4b171a

Please sign in to comment.