Fix a couple of problems with FreeLDR portability.
[reactos.git] / reactos / 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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 < 0x100000)
34 {
35 if (!memcmp(Ptr, "RSD PTR ", 8))
36 {
37 DbgPrint((DPRINT_HWDETECT, "ACPI supported\n"));
38
39 return (PRSDP_DESCRIPTOR)Ptr;
40 }
41
42 Ptr = (PUCHAR)((ULONG)Ptr + 0x10);
43 }
44
45 DbgPrint((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 /* Create new bus key */
71 FldrCreateComponentKey(SystemKey,
72 L"MultifunctionAdapter",
73 *BusNumber,
74 AdapterClass,
75 MultiFunctionAdapter,
76 &BiosKey);
77
78 /* Set 'Component Information' */
79 FldrSetComponentInformation(BiosKey,
80 0x0,
81 0x0,
82 0xFFFFFFFF);
83
84 /* Get BIOS memory map */
85 RtlZeroMemory(BiosMemoryMap, sizeof(BIOS_MEMORY_MAP) * 32);
86 BiosMemoryMapEntryCount = MachGetMemoryMap(BiosMemoryMap,
87 sizeof(BiosMemoryMap) / sizeof(BIOS_MEMORY_MAP));
88
89 /* Calculate the table size */
90 TableSize = BiosMemoryMapEntryCount * sizeof(BIOS_MEMORY_MAP) +
91 sizeof(ACPI_BIOS_DATA) - sizeof(BIOS_MEMORY_MAP);
92
93 /* Set 'Configuration Data' value */
94 PartialResourceList =
95 MmHeapAlloc(sizeof(CM_PARTIAL_RESOURCE_LIST) + TableSize);
96 memset(PartialResourceList, 0, sizeof(CM_PARTIAL_RESOURCE_LIST) + TableSize);
97 PartialResourceList->Version = 0;
98 PartialResourceList->Revision = 0;
99 PartialResourceList->Count = 1;
100
101 PartialDescriptor = &PartialResourceList->PartialDescriptors[0];
102 PartialDescriptor->Type = CmResourceTypeDeviceSpecific;
103 PartialDescriptor->ShareDisposition = CmResourceShareUndetermined;
104 PartialDescriptor->u.DeviceSpecificData.DataSize = TableSize;
105
106 /* Fill the table */
107 AcpiBiosData = (PACPI_BIOS_DATA)&PartialResourceList->PartialDescriptors[1];
108 AcpiBiosData->RSDTAddress.LowPart = Rsdp->rsdt_physical_address;
109 AcpiBiosData->Count = BiosMemoryMapEntryCount;
110 memcpy(AcpiBiosData->MemoryMap, BiosMemoryMap,
111 BiosMemoryMapEntryCount * sizeof(BIOS_MEMORY_MAP));
112
113 DbgPrint((DPRINT_HWDETECT, "RSDT %p, data size %x\n", Rsdp->rsdt_physical_address,
114 TableSize));
115
116 FldrSetConfigurationData(BiosKey,
117 PartialResourceList,
118 sizeof(CM_PARTIAL_RESOURCE_LIST) + TableSize
119 );
120
121 /* Increment bus number */
122 (*BusNumber)++;
123
124 /* Set 'Identifier' value */
125 FldrSetIdentifier(BiosKey, "ACPI BIOS");
126 MmFreeMemory(PartialResourceList);
127 }
128 }
129
130 /* EOF */