9ca9f128387837431c42ec48d7dee9a68db439ab
[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 LoaderBlock.Flags |= MB_FLAGS_ACPI_TABLE;
69
70 /* Get BIOS memory map */
71 RtlZeroMemory(BiosMemoryMap, sizeof(BiosMemoryMap));
72 BiosMemoryMapEntryCount = PcMemGetMemoryMap(BiosMemoryMap,
73 sizeof(BiosMemoryMap) / sizeof(BIOS_MEMORY_MAP));
74
75 /* Calculate the table size */
76 TableSize = BiosMemoryMapEntryCount * sizeof(BIOS_MEMORY_MAP) +
77 sizeof(ACPI_BIOS_DATA) - sizeof(BIOS_MEMORY_MAP);
78
79 /* Set 'Configuration Data' value */
80 PartialResourceList =
81 MmHeapAlloc(sizeof(CM_PARTIAL_RESOURCE_LIST) + TableSize);
82
83 if (PartialResourceList == NULL)
84 {
85 DPRINTM(DPRINT_HWDETECT,
86 "Failed to allocate resource descriptor\n");
87 return;
88 }
89
90 memset(PartialResourceList, 0, sizeof(CM_PARTIAL_RESOURCE_LIST) + TableSize);
91 PartialResourceList->Version = 0;
92 PartialResourceList->Revision = 0;
93 PartialResourceList->Count = 1;
94
95 PartialDescriptor = &PartialResourceList->PartialDescriptors[0];
96 PartialDescriptor->Type = CmResourceTypeDeviceSpecific;
97 PartialDescriptor->ShareDisposition = CmResourceShareUndetermined;
98 PartialDescriptor->u.DeviceSpecificData.DataSize = TableSize;
99
100 /* Fill the table */
101 AcpiBiosData = (PACPI_BIOS_DATA)&PartialResourceList->PartialDescriptors[1];
102 AcpiBiosData->RSDTAddress.LowPart = Rsdp->rsdt_physical_address;
103 AcpiBiosData->Count = BiosMemoryMapEntryCount;
104 memcpy(AcpiBiosData->MemoryMap, BiosMemoryMap,
105 BiosMemoryMapEntryCount * sizeof(BIOS_MEMORY_MAP));
106
107 DPRINTM(DPRINT_HWDETECT, "RSDT %p, data size %x\n", Rsdp->rsdt_physical_address,
108 TableSize);
109
110 /* Create new bus key */
111 FldrCreateComponentKey(SystemKey,
112 AdapterClass,
113 MultiFunctionAdapter,
114 0x0,
115 0x0,
116 0xFFFFFFFF,
117 "ACPI BIOS",
118 PartialResourceList,
119 sizeof(CM_PARTIAL_RESOURCE_LIST) + TableSize,
120 &BiosKey);
121
122 /* Increment bus number */
123 (*BusNumber)++;
124
125 MmHeapFree(PartialResourceList);
126 }
127 }
128
129 /* EOF */