* Sync to recent trunk (r52563).
[reactos.git] / boot / freeldr / freeldr / arch / i386 / hwacpi.c
1 /*
2 * FreeLoader
3 *
4 * Copyright (C) 2004 Eric Kohl
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <freeldr.h>
22 #include <debug.h>
23
24 BOOLEAN AcpiPresent = FALSE;
25
26 static PRSDP_DESCRIPTOR
27 FindAcpiBios(VOID)
28 {
29 PUCHAR Ptr;
30
31 /* Find the 'Root System Descriptor Table Pointer' */
32 Ptr = (PUCHAR)0xE0000;
33 while ((ULONG_PTR)Ptr < 0x100000)
34 {
35 if (!memcmp(Ptr, "RSD PTR ", 8))
36 {
37 DPRINTM(DPRINT_HWDETECT, "ACPI supported\n");
38
39 return (PRSDP_DESCRIPTOR)Ptr;
40 }
41
42 Ptr = (PUCHAR)((ULONG_PTR)Ptr + 0x10);
43 }
44
45 DPRINTM(DPRINT_HWDETECT, "ACPI not supported\n");
46
47 return NULL;
48 }
49
50
51 VOID
52 DetectAcpiBios(PCONFIGURATION_COMPONENT_DATA SystemKey, ULONG *BusNumber)
53 {
54 PCONFIGURATION_COMPONENT_DATA BiosKey;
55 PCM_PARTIAL_RESOURCE_LIST PartialResourceList;
56 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor;
57 PRSDP_DESCRIPTOR Rsdp;
58 PACPI_BIOS_DATA AcpiBiosData;
59 BIOS_MEMORY_MAP BiosMemoryMap[32];
60 ULONG BiosMemoryMapEntryCount, TableSize;
61
62 Rsdp = FindAcpiBios();
63
64 if (Rsdp)
65 {
66 /* Set up the flag in the loader block */
67 AcpiPresent = TRUE;
68
69 /* Get BIOS memory map */
70 RtlZeroMemory(BiosMemoryMap, sizeof(BiosMemoryMap));
71 BiosMemoryMapEntryCount = PcMemGetMemoryMap(BiosMemoryMap,
72 sizeof(BiosMemoryMap) / sizeof(BIOS_MEMORY_MAP));
73
74 /* Calculate the table size */
75 TableSize = BiosMemoryMapEntryCount * sizeof(BIOS_MEMORY_MAP) +
76 sizeof(ACPI_BIOS_DATA) - sizeof(BIOS_MEMORY_MAP);
77
78 /* Set 'Configuration Data' value */
79 PartialResourceList =
80 MmHeapAlloc(sizeof(CM_PARTIAL_RESOURCE_LIST) + TableSize);
81
82 if (PartialResourceList == NULL)
83 {
84 DPRINTM(DPRINT_HWDETECT,
85 "Failed to allocate resource descriptor\n");
86 return;
87 }
88
89 memset(PartialResourceList, 0, sizeof(CM_PARTIAL_RESOURCE_LIST) + TableSize);
90 PartialResourceList->Version = 0;
91 PartialResourceList->Revision = 0;
92 PartialResourceList->Count = 1;
93
94 PartialDescriptor = &PartialResourceList->PartialDescriptors[0];
95 PartialDescriptor->Type = CmResourceTypeDeviceSpecific;
96 PartialDescriptor->ShareDisposition = CmResourceShareUndetermined;
97 PartialDescriptor->u.DeviceSpecificData.DataSize = TableSize;
98
99 /* Fill the table */
100 AcpiBiosData = (PACPI_BIOS_DATA)&PartialResourceList->PartialDescriptors[1];
101 AcpiBiosData->RSDTAddress.LowPart = Rsdp->rsdt_physical_address;
102 AcpiBiosData->Count = BiosMemoryMapEntryCount;
103 memcpy(AcpiBiosData->MemoryMap, BiosMemoryMap,
104 BiosMemoryMapEntryCount * sizeof(BIOS_MEMORY_MAP));
105
106 DPRINTM(DPRINT_HWDETECT, "RSDT %p, data size %x\n", Rsdp->rsdt_physical_address,
107 TableSize);
108
109 /* Create new bus key */
110 FldrCreateComponentKey(SystemKey,
111 AdapterClass,
112 MultiFunctionAdapter,
113 0x0,
114 0x0,
115 0xFFFFFFFF,
116 "ACPI BIOS",
117 PartialResourceList,
118 sizeof(CM_PARTIAL_RESOURCE_LIST) + TableSize,
119 &BiosKey);
120
121 /* Increment bus number */
122 (*BusNumber)++;
123
124 MmHeapFree(PartialResourceList);
125 }
126 }
127
128 /* EOF */