ReactOS 0.4.16-dev-2613-g9533ad7
uefihw.c
Go to the documentation of this file.
1/*
2 * PROJECT: FreeLoader UEFI Support
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Hardware detection routines
5 * COPYRIGHT: Copyright 2022 Justin Miller <justinmiller100@gmail.com>
6 */
7
8/* INCLUDES ******************************************************************/
9
10#include <uefildr.h>
11#include "../vidfb.h"
12
13#include <debug.h>
15
16/* GLOBALS *******************************************************************/
17
20
21/* From uefivid.c */
23extern ULONG VramSize;
25
28
29/* FUNCTIONS *****************************************************************/
30
31VOID
33{
34 GlobalSystemTable->BootServices->Stall(Microseconds);
35}
36
37VOID
39{
43
44 /* Keep one timer event around and arm it each idle tick */
45 if (IdleTimerEvent == NULL)
46 {
47 Status = BootServices->CreateEvent(EVT_TIMER,
49 NULL,
50 NULL,
52 if (EFI_ERROR(Status))
53 {
54 StallExecutionProcessor(10000); /* 10 ms fallback */
55 return;
56 }
57 }
58
59 /* Set a 10ms (100,000 * 100ns) relative timer */
60 Status = BootServices->SetTimer(IdleTimerEvent, TimerRelative, 100000);
61 if (!EFI_ERROR(Status))
62 Status = BootServices->WaitForEvent(1, &IdleTimerEvent, &Index);
63 if (EFI_ERROR(Status))
64 StallExecutionProcessor(10000); /* 10 ms fallback */
65}
66
68{
69 return AcpiPresent;
70}
71
72static
75{
76 UINTN i;
77 RSDP_DESCRIPTOR* rsdp = NULL;
79
81 {
83 &acpi2_guid, sizeof(acpi2_guid)))
84 {
86 break;
87 }
88 }
89
90 return rsdp;
91}
92
96{
99
100 Rsdp = FindAcpiBios();
101 if (Rsdp == NULL)
102 return NULL;
103
104 if ((Rsdp->revision > 0) && (Rsdp->xsdt_physical_address != 0))
105 {
107
108 if ((Xsdt != NULL) && (Xsdt->Header.Length >= sizeof(Xsdt->Header)))
109 {
110 Count = (Xsdt->Header.Length - sizeof(Xsdt->Header)) / sizeof(Xsdt->Tables[0]);
111 for (Index = 0; Index < Count; ++Index)
112 {
115
116 if ((Header != NULL) && (Header->Signature == Signature))
117 return Header;
118 }
119 }
120 }
121
122 if (Rsdp->rsdt_physical_address != 0)
123 {
125
126 if ((Rsdt != NULL) && (Rsdt->Header.Length >= sizeof(Rsdt->Header)))
127 {
128 Count = (Rsdt->Header.Length - sizeof(Rsdt->Header)) / sizeof(Rsdt->Tables[0]);
129 for (Index = 0; Index < Count; ++Index)
130 {
133
134 if ((Header != NULL) && (Header->Signature == Signature))
135 return Header;
136 }
137 }
138 }
139
140 return NULL;
141}
142
143VOID
145{
147 PCM_PARTIAL_RESOURCE_LIST PartialResourceList;
148 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor;
149 PRSDP_DESCRIPTOR Rsdp;
150 PACPI_BIOS_DATA AcpiBiosData;
152
153 Rsdp = FindAcpiBios();
154
155 if (Rsdp)
156 {
157 /* Set up the flag in the loader block */
159
160 /* Calculate the table size */
161 TableSize = sizeof(ACPI_BIOS_DATA);
162
163 /* Set 'Configuration Data' value */
164 Size = FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST, PartialDescriptors[1]) + TableSize;
165 PartialResourceList = FrLdrHeapAlloc(Size, TAG_HW_RESOURCE_LIST);
166 if (PartialResourceList == NULL)
167 {
168 ERR("Failed to allocate resource descriptor\n");
169 return;
170 }
171
172 RtlZeroMemory(PartialResourceList, Size);
173 PartialResourceList->Version = 0;
174 PartialResourceList->Revision = 0;
175 PartialResourceList->Count = 1;
176
177 PartialDescriptor = &PartialResourceList->PartialDescriptors[0];
178 PartialDescriptor->Type = CmResourceTypeDeviceSpecific;
180 PartialDescriptor->u.DeviceSpecificData.DataSize = TableSize;
181
182 /* Fill the table */
183 AcpiBiosData = (PACPI_BIOS_DATA)(PartialDescriptor + 1);
184
185 if (Rsdp->revision > 0)
186 {
187 TRACE("ACPI >1.0, using XSDT address\n");
188 AcpiBiosData->RSDTAddress.QuadPart = Rsdp->xsdt_physical_address;
189 }
190 else
191 {
192 TRACE("ACPI 1.0, using RSDT address\n");
193 AcpiBiosData->RSDTAddress.LowPart = Rsdp->rsdt_physical_address;
194 }
195
196 AcpiBiosData->Count = 0;
197
198 TRACE("RSDT %p, data size %x\n", Rsdp->rsdt_physical_address, TableSize);
199
200 /* Create new bus key */
201 FldrCreateComponentKey(SystemKey,
204 0x0,
205 0x0,
206 0xFFFFFFFF,
207 "ACPI BIOS",
208 PartialResourceList,
209 Size,
210 &BiosKey);
211
212 /* Increment bus number */
213 (*BusNumber)++;
214 }
215}
216
217static VOID
220{
221 PCONFIGURATION_COMPONENT_DATA ControllerKey;
222 PCM_PARTIAL_RESOURCE_LIST PartialResourceList;
223 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor;
224 PCM_FRAMEBUF_DEVICE_DATA FramebufData;
225 ULONG Size;
226
227 if (!VramAddress || (VramSize == 0) || !FrameBufferData)
228 return;
229
230 Size = FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST, PartialDescriptors[2]) + sizeof(*FramebufData);
231 PartialResourceList = FrLdrHeapAlloc(Size, TAG_HW_RESOURCE_LIST);
232 if (PartialResourceList == NULL)
233 {
234 ERR("Failed to allocate resource descriptor\n");
235 return;
236 }
237
238 /* Initialize resource descriptor */
239 RtlZeroMemory(PartialResourceList, Size);
240 PartialResourceList->Version = 1;
241 PartialResourceList->Revision = 2;
242 PartialResourceList->Count = 2;
243
244 /* Set Memory */
245 PartialDescriptor = &PartialResourceList->PartialDescriptors[0];
246 PartialDescriptor->Type = CmResourceTypeMemory;
248 PartialDescriptor->Flags = CM_RESOURCE_MEMORY_READ_WRITE;
249 PartialDescriptor->u.Memory.Start.QuadPart = VramAddress;
250 PartialDescriptor->u.Memory.Length = VramSize;
251
252 /* Set framebuffer-specific data */
253 PartialDescriptor = &PartialResourceList->PartialDescriptors[1];
254 PartialDescriptor->Type = CmResourceTypeDeviceSpecific;
256 PartialDescriptor->Flags = 0;
257 PartialDescriptor->u.DeviceSpecificData.DataSize = sizeof(*FramebufData);
258
259 /* Get pointer to framebuffer-specific data */
260 FramebufData = (PCM_FRAMEBUF_DEVICE_DATA)(PartialDescriptor + 1);
261 RtlCopyMemory(FramebufData, FrameBufferData, sizeof(*FrameBufferData));
262 FramebufData->Version = 1;
263 FramebufData->Revision = 3;
264 FramebufData->VideoClock = 0; // FIXME: Use EDID
265
270 0,
271 0xFFFFFFFF,
272 "UEFI GOP Framebuffer",
273 PartialResourceList,
274 Size,
275 &ControllerKey);
276
277 // NOTE: Don't add a MonitorPeripheral for now.
278 // We should use EDID data for it.
279}
280
281static
282VOID
284{
285 PCM_PARTIAL_RESOURCE_LIST PartialResourceList;
287 ULONG Size;
288
289 /* Set 'Configuration Data' value */
290 Size = FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST, PartialDescriptors);
291 PartialResourceList = FrLdrHeapAlloc(Size, TAG_HW_RESOURCE_LIST);
292 if (PartialResourceList == NULL)
293 {
294 ERR("Failed to allocate resource descriptor\n");
295 return;
296 }
297
298 /* Initialize resource descriptor */
299 RtlZeroMemory(PartialResourceList, Size);
300 PartialResourceList->Version = 1;
301 PartialResourceList->Revision = 1;
302 PartialResourceList->Count = 0;
303
304 /* Create new bus key */
305 FldrCreateComponentKey(SystemKey,
308 0,
309 0,
310 0xFFFFFFFF,
311 "UEFI Internal",
312 PartialResourceList,
313 Size,
314 &BusKey);
315
316 /* Increment bus number */
317 (*BusNumber)++;
318
319 /* Detect devices that do not belong to "standard" buses */
321
322 /* FIXME: Detect more devices */
323}
324
328{
330 ULONG BusNumber = 0;
331
332 TRACE("DetectHardware()\n");
333
334 /* Create the 'System' key */
335#if defined(_M_IX86) || defined(_M_AMD64)
336 FldrCreateSystemKey(&SystemKey, "AT/AT COMPATIBLE");
337#elif defined(_M_IA64)
338 FldrCreateSystemKey(&SystemKey, "Intel Itanium processor family");
339#elif defined(_M_ARM) || defined(_M_ARM64)
340 FldrCreateSystemKey(&SystemKey, "ARM processor family");
341#else
342 #error Please define a system key for your architecture
343#endif
344
345 /* Detect buses */
346 DetectInternal(SystemKey, &BusNumber);
347 // TODO: DetectPciBios
348 DetectAcpiBios(SystemKey, &BusNumber);
349
350 TRACE("DetectHardware() Done\n");
351 return SystemKey;
352}
#define EFI_ACPI_20_TABLE_GUID
Definition: Acpi.h:40
UINT32 UINTN
#define EFI_ERROR(A)
Definition: UefiBaseType.h:165
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:31
#define TPL_APPLICATION
Definition: UefiSpec.h:561
@ TimerRelative
Definition: UefiSpec.h:464
#define EVT_TIMER
Definition: UefiSpec.h:362
unsigned char BOOLEAN
Definition: actypes.h:127
VOID FldrCreateSystemKey(_Out_ PCONFIGURATION_COMPONENT_DATA *SystemNode, _In_ PCSTR IdentifierString)
Definition: archwsup.c:161
VOID FldrCreateComponentKey(_In_ PCONFIGURATION_COMPONENT_DATA SystemNode, _In_ CONFIGURATION_CLASS Class, _In_ CONFIGURATION_TYPE Type, _In_ IDENTIFIER_FLAG Flags, _In_ ULONG Key, _In_ ULONG Affinity, _In_ PCSTR IdentifierString, _In_ PCM_PARTIAL_RESOURCE_LIST ResourceList, _In_ ULONG Size, _Out_ PCONFIGURATION_COMPONENT_DATA *ComponentKey)
Definition: archwsup.c:224
#define ERR(fmt,...)
Definition: precomp.h:57
#define DBG_DEFAULT_CHANNEL(ch)
Definition: debug.h:106
PVOID FrLdrHeapAlloc(SIZE_T MemorySize, ULONG Tag)
Definition: heap.c:533
Definition: Header.h:9
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
_ACRTIMP int __cdecl memcmp(const void *, const void *, size_t)
Definition: string.c:2802
static const WCHAR Signature[]
Definition: parser.c:141
Status
Definition: gdiplustypes.h:25
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define CM_RESOURCE_MEMORY_READ_WRITE
Definition: cmtypes.h:121
#define _In_
Definition: no_sal2.h:158
#define _In_opt_
Definition: no_sal2.h:212
int Count
Definition: noreturn.cpp:7
struct _ACPI_BIOS_DATA ACPI_BIOS_DATA
struct _ACPI_BIOS_DATA * PACPI_BIOS_DATA
#define CmResourceTypeMemory
Definition: restypes.h:106
#define CmResourceTypeDeviceSpecific
Definition: restypes.h:108
@ ControllerClass
Definition: arc.h:103
@ AdapterClass
Definition: arc.h:102
@ MultiFunctionAdapter
Definition: arc.h:125
@ DisplayController
Definition: arc.h:132
@ ConsoleOut
Definition: arc.h:92
@ Output
Definition: arc.h:94
RSDT * PRSDT
Definition: acpi.h:206
DESCRIPTION_HEADER * PDESCRIPTION_HEADER
Definition: acpi.h:109
XSDT * PXSDT
Definition: acpi.h:214
struct _CM_FRAMEBUF_DEVICE_DATA * PCM_FRAMEBUF_DEVICE_DATA
#define TRACE(s)
Definition: solgame.cpp:4
EFI_SET_TIMER SetTimer
Definition: UefiSpec.h:1820
EFI_CREATE_EVENT CreateEvent
Definition: UefiSpec.h:1819
EFI_WAIT_FOR_EVENT WaitForEvent
Definition: UefiSpec.h:1821
EFI_STALL Stall
Definition: UefiSpec.h:1852
EFI_BOOT_SERVICES * BootServices
Definition: UefiSpec.h:1959
EFI_CONFIGURATION_TABLE * ConfigurationTable
Definition: UefiSpec.h:1968
UINTN NumberOfTableEntries
Definition: UefiSpec.h:1963
ULONG rsdt_physical_address
Definition: winldr.h:26
UCHAR revision
Definition: winldr.h:25
ULONGLONG xsdt_physical_address
Definition: winldr.h:28
PHYSICAL_ADDRESS RSDTAddress
Definition: pcbios.h:77
ULONGLONG Count
Definition: pcbios.h:78
ReactOS Framebuffer-specific video device configuration data.
Definition: framebuf.h:35
union _CM_PARTIAL_RESOURCE_DESCRIPTOR::@384 u
struct _CM_PARTIAL_RESOURCE_DESCRIPTOR::@384::@393 DeviceSpecificData
struct _CM_PARTIAL_RESOURCE_DESCRIPTOR::@384::@389 Memory
CM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptors[1]
Definition: restypes.h:100
Definition: acpi.h:201
ULONG Tables[ANYSIZE_ARRAY]
Definition: acpi.h:204
DESCRIPTION_HEADER Header
Definition: acpi.h:203
Definition: acpi.h:209
PHYSICAL_ADDRESS Tables[ANYSIZE_ARRAY]
Definition: acpi.h:212
DESCRIPTION_HEADER Header
Definition: acpi.h:211
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:255
const char * PCSTR
Definition: typedefs.h:52
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG
Definition: typedefs.h:59
#define TAG_HW_RESOURCE_LIST
Definition: uefidisk.c:15
VOID DetectAcpiBios(PCONFIGURATION_COMPONENT_DATA SystemKey, ULONG *BusNumber)
Definition: uefihw.c:144
static VOID DetectDisplayController(_In_ PCONFIGURATION_COMPONENT_DATA BusKey)
Definition: uefihw.c:218
static VOID DetectInternal(PCONFIGURATION_COMPONENT_DATA SystemKey, ULONG *BusNumber)
Definition: uefihw.c:283
PDESCRIPTION_HEADER UefiFindAcpiTable(_In_ ULONG Signature)
Definition: uefihw.c:94
ULONG_PTR VramAddress
Definition: uefivid.c:21
VOID UefiHwIdle(VOID)
Definition: uefihw.c:38
BOOLEAN IsAcpiPresent(VOID)
Definition: uefihw.c:67
BOOLEAN AcpiPresent
Definition: uefihw.c:26
PCONFIGURATION_COMPONENT_DATA UefiHwDetect(_In_opt_ PCSTR Options)
Definition: uefihw.c:326
EFI_SYSTEM_TABLE * GlobalSystemTable
Definition: uefildr.c:16
VOID StallExecutionProcessor(ULONG Microseconds)
Definition: uefihw.c:32
PCM_FRAMEBUF_DEVICE_DATA FrameBufferData
Definition: xboxvideo.c:31
static PRSDP_DESCRIPTOR FindAcpiBios(VOID)
Definition: uefihw.c:74
ULONG VramSize
Definition: uefivid.c:22
static EFI_EVENT IdleTimerEvent
Definition: uefihw.c:27
EFI_HANDLE GlobalImageHandle
Definition: uefildr.c:15
LONGLONG QuadPart
Definition: typedefs.h:114
ULONG LowPart
Definition: typedefs.h:106
_In_ WDFCOLLECTION _In_ ULONG Index
_In_ PWDFDEVICE_INIT _In_ PWDF_REMOVE_LOCK_OPTIONS Options
Definition: wdfdevice.h:3540
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4539
@ CmResourceShareDeviceExclusive
Definition: cmtypes.h:241
@ CmResourceShareUndetermined
Definition: cmtypes.h:240
_In_opt_ PUNICODE_STRING _In_ PDRIVER_OBJECT _In_ PDEVICE_OBJECT _In_ INTERFACE_TYPE _In_ ULONG BusNumber
Definition: halfuncs.h:160
_Must_inspect_result_ typedef _Out_ PULONG TableSize
Definition: iotypes.h:4330