Skip to content

Commit 7918e8e

Browse files
[FREELDR] Hacks to UEFI-boot Windows
1 parent 106dbed commit 7918e8e

File tree

4 files changed

+205
-33
lines changed

4 files changed

+205
-33
lines changed

boot/freeldr/freeldr/arch/uefi/uefidisk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ UefiGetBootPartitionEntry(
9393
TRACE("UefiGetBootPartitionEntry: DriveNumber: %d\n", DriveNumber - FIRST_BIOS_DISK);
9494
/* UefiBootRoot is the offset into the array of handles where the raw disk of the boot drive is.
9595
* Partitions start with 1 in ARC, but UEFI root drive identitfier is also first partition. */
96-
PartitionNum = (OffsetToBoot - UefiBootRootIdentifier);
96+
PartitionNum = 0;
9797
if (PartitionNum == 0)
9898
{
9999
TRACE("Boot PartitionNumber is 0\n");

boot/freeldr/freeldr/arch/uefi/uefihw.c

Lines changed: 196 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/* INCLUDES ******************************************************************/
99

1010
#include <uefildr.h>
11+
#include <drivers/bootvid/framebuf.h>
1112

1213
#include <debug.h>
1314
DBG_DEFAULT_CHANNEL(WARNING);
@@ -68,8 +69,7 @@ DetectAcpiBios(PCONFIGURATION_COMPONENT_DATA SystemKey, ULONG *BusNumber)
6869
AcpiPresent = TRUE;
6970

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

7474
/* Set 'Configuration Data' value */
7575
PartialResourceList = FrLdrHeapAlloc(sizeof(CM_PARTIAL_RESOURCE_LIST) +
@@ -104,9 +104,7 @@ DetectAcpiBios(PCONFIGURATION_COMPONENT_DATA SystemKey, ULONG *BusNumber)
104104
AcpiBiosData->RSDTAddress.LowPart = Rsdp->rsdt_physical_address;
105105
}
106106

107-
AcpiBiosData->Count = FreeldrDescCount;
108-
memcpy(AcpiBiosData->MemoryMap, EfiMemoryMap,
109-
FreeldrDescCount * sizeof(BIOS_MEMORY_MAP));
107+
AcpiBiosData->Count = 0;
110108

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

@@ -127,6 +125,196 @@ DetectAcpiBios(PCONFIGURATION_COMPONENT_DATA SystemKey, ULONG *BusNumber)
127125
}
128126
}
129127

128+
extern REACTOS_INTERNAL_BGCONTEXT framebufferData;
129+
/****/extern EFI_PIXEL_BITMASK UefiGopPixelBitmask;/****/
130+
131+
static VOID
132+
DetectDisplayController(PCONFIGURATION_COMPONENT_DATA BusKey)
133+
{
134+
PCONFIGURATION_COMPONENT_DATA ControllerKey;
135+
PCM_PARTIAL_RESOURCE_LIST PartialResourceList;
136+
PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor;
137+
PCM_FRAMEBUF_DEVICE_DATA FramebufferData;
138+
ULONG Size;
139+
140+
if (framebufferData.BufferSize == 0)
141+
return;
142+
143+
ERR("\nStructure sizes:\n"
144+
" sizeof(CM_PARTIAL_RESOURCE_LIST) = %lu\n"
145+
" sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) = %lu\n"
146+
" sizeof(CM_FRAMEBUF_DEVICE_DATA) = %lu\n\n",
147+
sizeof(CM_PARTIAL_RESOURCE_LIST),
148+
sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR),
149+
sizeof(CM_FRAMEBUF_DEVICE_DATA));
150+
151+
Size = sizeof(CM_PARTIAL_RESOURCE_LIST) +
152+
sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) +
153+
sizeof(CM_FRAMEBUF_DEVICE_DATA);
154+
PartialResourceList = FrLdrHeapAlloc(Size, TAG_HW_RESOURCE_LIST);
155+
if (PartialResourceList == NULL)
156+
{
157+
ERR("Failed to allocate resource descriptor\n");
158+
return;
159+
}
160+
161+
/* Initialize resource descriptor */
162+
RtlZeroMemory(PartialResourceList, Size);
163+
PartialResourceList->Version = 1;
164+
PartialResourceList->Revision = 2;
165+
PartialResourceList->Count = 2;
166+
167+
/* Set Memory */
168+
PartialDescriptor = &PartialResourceList->PartialDescriptors[0];
169+
PartialDescriptor->Type = CmResourceTypeMemory;
170+
PartialDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
171+
PartialDescriptor->Flags = CM_RESOURCE_MEMORY_READ_WRITE;
172+
PartialDescriptor->u.Memory.Start.QuadPart = framebufferData.BaseAddress;
173+
PartialDescriptor->u.Memory.Length = framebufferData.BufferSize;
174+
175+
/* Set framebuffer-specific data */
176+
PartialDescriptor = &PartialResourceList->PartialDescriptors[1];
177+
PartialDescriptor->Type = CmResourceTypeDeviceSpecific;
178+
PartialDescriptor->ShareDisposition = CmResourceShareUndetermined;
179+
PartialDescriptor->Flags = 0;
180+
PartialDescriptor->u.DeviceSpecificData.DataSize =
181+
sizeof(CM_FRAMEBUF_DEVICE_DATA);
182+
183+
/* Get pointer to framebuffer-specific data */
184+
FramebufferData = (PVOID)(PartialDescriptor + 1);
185+
RtlZeroMemory(FramebufferData, sizeof(*FramebufferData));
186+
FramebufferData->Version = 2;
187+
FramebufferData->Revision = 0;
188+
189+
FramebufferData->VideoClock = 0; // FIXME: Use EDID
190+
191+
/* Horizontal and Vertical resolution in pixels */
192+
FramebufferData->ScreenWidth = framebufferData.ScreenWidth;
193+
FramebufferData->ScreenHeight = framebufferData.ScreenHeight;
194+
195+
/* Number of pixel elements per video memory line */
196+
FramebufferData->PixelsPerScanLine = framebufferData.PixelsPerScanLine;
197+
198+
//
199+
// TODO: Investigate display rotation!
200+
//
201+
// See OpenCorePkg OcConsoleLib/ConsoleGop.c
202+
// if ((mGop.Rotation == 90) || (mGop.Rotation == 270))
203+
if (FramebufferData->ScreenWidth < FramebufferData->ScreenHeight)
204+
{
205+
#define SWAP(x, y) { (x) ^= (y); (y) ^= (x); (x) ^= (y); }
206+
SWAP(FramebufferData->ScreenWidth, FramebufferData->ScreenHeight);
207+
FramebufferData->PixelsPerScanLine = FramebufferData->ScreenWidth;
208+
#undef SWAP
209+
}
210+
211+
/* Physical format of the pixel */
212+
// ASSERT(sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL) == 4);
213+
switch (framebufferData.PixelFormat)
214+
{
215+
case PixelRedGreenBlueReserved8BitPerColor:
216+
{
217+
FramebufferData->BitsPerPixel = (8 * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
218+
*(EFI_PIXEL_BITMASK*)&FramebufferData->PixelInformation = EfiPixelMasks[framebufferData.PixelFormat];
219+
break;
220+
}
221+
222+
case PixelBlueGreenRedReserved8BitPerColor:
223+
{
224+
FramebufferData->BitsPerPixel = (8 * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
225+
FramebufferData->PixelInformation.RedMask = 0x00FF0000;
226+
FramebufferData->PixelInformation.GreenMask = 0x0000FF00;
227+
FramebufferData->PixelInformation.BlueMask = 0x000000FF;
228+
FramebufferData->PixelInformation.ReservedMask = 0xFF000000;
229+
break;
230+
}
231+
232+
case PixelBitMask:
233+
{
234+
FramebufferData->BitsPerPixel =
235+
PixelBitmasksToBpp(UefiGopPixelBitmask.RedMask,
236+
UefiGopPixelBitmask.GreenMask,
237+
UefiGopPixelBitmask.BlueMask,
238+
UefiGopPixelBitmask.ReservedMask);
239+
*(EFI_PIXEL_BITMASK*)&FramebufferData->PixelInformation = UefiGopPixelBitmask;
240+
//FramebufferData->PixelInformation.RedMask = UefiGopPixelBitmask.RedMask;
241+
//FramebufferData->PixelInformation.GreenMask = UefiGopPixelBitmask.GreenMask;
242+
//FramebufferData->PixelInformation.BlueMask = UefiGopPixelBitmask.BlueMask;
243+
//FramebufferData->PixelInformation.ReservedMask = UefiGopPixelBitmask.ReservedMask;
244+
break;
245+
}
246+
247+
case PixelBltOnly:
248+
default:
249+
{
250+
ERR("Unsupported UFEI GOP format %lu\n", framebufferData.PixelFormat);
251+
FramebufferData->BitsPerPixel = 0;
252+
RtlZeroMemory(&FramebufferData->PixelInformation,
253+
sizeof(FramebufferData->PixelInformation));
254+
break;
255+
}
256+
}
257+
258+
FldrCreateComponentKey(BusKey,
259+
ControllerClass,
260+
DisplayController,
261+
Output | ConsoleOut,
262+
0,
263+
0xFFFFFFFF,
264+
"UEFI GOP Framebuffer",
265+
PartialResourceList,
266+
Size,
267+
&ControllerKey);
268+
269+
// NOTE: Don't add a MonitorPeripheral for now...
270+
// We should use EDID data for it.
271+
}
272+
273+
static
274+
VOID
275+
DetectInternal(PCONFIGURATION_COMPONENT_DATA SystemKey, ULONG *BusNumber)
276+
{
277+
PCM_PARTIAL_RESOURCE_LIST PartialResourceList;
278+
PCONFIGURATION_COMPONENT_DATA BusKey;
279+
ULONG Size;
280+
281+
/* Set 'Configuration Data' value */
282+
Size = sizeof(CM_PARTIAL_RESOURCE_LIST) -
283+
sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR);
284+
PartialResourceList = FrLdrHeapAlloc(Size, TAG_HW_RESOURCE_LIST);
285+
if (PartialResourceList == NULL)
286+
{
287+
ERR("Failed to allocate resource descriptor\n");
288+
return;
289+
}
290+
291+
/* Initialize resource descriptor */
292+
RtlZeroMemory(PartialResourceList, Size);
293+
PartialResourceList->Version = 1;
294+
PartialResourceList->Revision = 2;
295+
PartialResourceList->Count = 0;
296+
297+
/* Create new bus key */
298+
FldrCreateComponentKey(SystemKey,
299+
AdapterClass,
300+
MultiFunctionAdapter,
301+
0,
302+
0,
303+
0xFFFFFFFF,
304+
"UEFI Internal",
305+
PartialResourceList,
306+
Size,
307+
&BusKey);
308+
309+
/* Increment bus number */
310+
(*BusNumber)++;
311+
312+
/* Detect devices that do not belong to "standard" buses */
313+
DetectDisplayController(BusKey);
314+
315+
/* FIXME: Detect more devices */
316+
}
317+
130318
PCONFIGURATION_COMPONENT_DATA
131319
UefiHwDetect(
132320
_In_opt_ PCSTR Options)
@@ -138,7 +326,7 @@ UefiHwDetect(
138326

139327
/* Create the 'System' key */
140328
#if defined(_M_IX86) || defined(_M_AMD64)
141-
FldrCreateSystemKey(&SystemKey, "AT/AT COMPATIBLE");
329+
FldrCreateSystemKey(&SystemKey, "UEFI-based Device");
142330
#elif defined(_M_IA64)
143331
FldrCreateSystemKey(&SystemKey, "Intel Itanium processor family");
144332
#elif defined(_M_ARM) || defined(_M_ARM64)
@@ -149,6 +337,8 @@ UefiHwDetect(
149337

150338
/* Detect ACPI */
151339
DetectAcpiBios(SystemKey, &BusNumber);
340+
DetectInternal(SystemKey, &BusNumber);
341+
152342

153343
TRACE("DetectHardware() Done\n");
154344
return SystemKey;

boot/freeldr/freeldr/arch/uefi/uefimem.c

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,36 +110,17 @@ UefiConvertToFreeldrDesc(EFI_MEMORY_TYPE EfiMemoryType)
110110
{
111111
switch (EfiMemoryType)
112112
{
113-
case EfiReservedMemoryType:
114-
return LoaderReserve;
115-
case EfiLoaderCode:
116-
return LoaderLoadedProgram;
117-
case EfiLoaderData:
118-
return LoaderLoadedProgram;
119113
case EfiBootServicesCode:
120-
return LoaderFirmwareTemporary;
121114
case EfiBootServicesData:
122-
return LoaderFirmwareTemporary;
123-
case EfiRuntimeServicesCode:
124-
return LoaderFirmwarePermanent;
125-
case EfiRuntimeServicesData:
126-
return LoaderFirmwarePermanent;
127115
case EfiConventionalMemory:
116+
case EfiLoaderCode:
117+
case EfiLoaderData:
128118
return LoaderFree;
129119
case EfiUnusableMemory:
130120
return LoaderBad;
131-
case EfiACPIReclaimMemory:
132-
return LoaderFirmwareTemporary;
133-
case EfiACPIMemoryNVS:
134-
return LoaderReserve;
135-
case EfiMemoryMappedIO:
136-
return LoaderReserve;
137-
case EfiMemoryMappedIOPortSpace:
138-
return LoaderReserve;
139121
default:
140-
break;
122+
return LoaderSpecialMemory;
141123
}
142-
return LoaderReserve;
143124
}
144125

145126
PFREELDR_MEMORY_DESCRIPTOR
@@ -212,7 +193,7 @@ UefiMemGetMemoryMap(ULONG *MemoryMapSize)
212193
if (Status != EFI_SUCCESS)
213194
{
214195
/* We failed to reserve the page, so change its type */
215-
MemoryType = LoaderFirmwareTemporary;
196+
MemoryType = LoaderFirmwarePermanent;
216197
}
217198
}
218199

@@ -240,7 +221,7 @@ UefiMemGetMemoryMap(ULONG *MemoryMapSize)
240221

241222
/* Windows expects the first page to be reserved, otherwise it asserts.
242223
* However it can be just a free page on some UEFI systems. */
243-
UefiSetMemory(FreeldrMem, 0x000000, 1, LoaderFirmwarePermanent);
224+
UefiSetMemory(FreeldrMem, 0x000000, 1, LoaderSpecialMemory);
244225
*MemoryMapSize = FreeldrDescCount;
245226
return FreeldrMem;
246227
}

boot/freeldr/freeldr/arch/uefi/uefivid.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extern UCHAR BitmapFont8x16[256 * 16];
2424
UCHAR MachDefaultTextColor = COLOR_GRAY;
2525
REACTOS_INTERNAL_BGCONTEXT framebufferData;
2626
EFI_GUID EfiGraphicsOutputProtocol = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
27+
/****/EFI_PIXEL_BITMASK UefiGopPixelBitmask;/****/
2728

2829
/* FUNCTIONS ******************************************************************/
2930

@@ -42,15 +43,15 @@ UefiInitializeVideo(VOID)
4243
}
4344

4445
/* We don't need high resolutions for freeldr */
45-
gop->SetMode(gop, LOWEST_SUPPORTED_RES);
46+
//gop->SetMode(gop, LOWEST_SUPPORTED_RES);
4647

4748
framebufferData.BaseAddress = (ULONG_PTR)gop->Mode->FrameBufferBase;
4849
framebufferData.BufferSize = gop->Mode->FrameBufferSize;
4950
framebufferData.ScreenWidth = gop->Mode->Info->HorizontalResolution;
5051
framebufferData.ScreenHeight = gop->Mode->Info->VerticalResolution;
5152
framebufferData.PixelsPerScanLine = gop->Mode->Info->PixelsPerScanLine;
5253
framebufferData.PixelFormat = gop->Mode->Info->PixelFormat;
53-
54+
/****/UefiGopPixelBitmask = gop->Mode->Info->PixelInformation;/****/
5455
return Status;
5556
}
5657

0 commit comments

Comments
 (0)