5cbb942537fb3379ae90a685c8367efc895a4e29
[reactos.git] / reactos / boot / freeldr / freeldr / reactos / archwsup.c
1 /*
2 * PROJECT: ReactOS Boot Loader (FreeLDR)
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: boot/freeldr/freeldr/reactos/archwsup.c
5 * PURPOSE: Routines for ARC Hardware Tree and Configuration Data
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #include <freeldr.h>
12 #define NDEBUG
13 #include <debug.h>
14
15 /* GLOBALS ********************************************************************/
16
17 extern CHAR reactos_arc_hardware_data[];
18 ULONG FldrpHwHeapLocation;
19 PCONFIGURATION_COMPONENT_DATA FldrArcHwTreeRoot;
20
21 BOOLEAN UseRealHeap = FALSE;
22
23 /* FUNCTIONS ******************************************************************/
24
25 PVOID
26 NTAPI
27 FldrpHwHeapAlloc(IN ULONG Size)
28 {
29 PVOID Buffer;
30
31 if (UseRealHeap)
32 {
33 /* Allocate memory from generic bootloader heap */
34 Buffer = MmHeapAlloc(Size);
35 }
36 else
37 {
38 /* Return a block of memory from the ARC Hardware Heap */
39 Buffer = &reactos_arc_hardware_data[FldrpHwHeapLocation];
40
41 /* Increment the heap location */
42 FldrpHwHeapLocation += Size;
43 if (FldrpHwHeapLocation > HW_MAX_ARC_HEAP_SIZE) Buffer = NULL;
44 }
45
46 /* Clear it */
47 if (Buffer)
48 RtlZeroMemory(Buffer, Size);
49
50 /* Return the buffer */
51 return Buffer;
52 }
53
54 VOID
55 NTAPI
56 FldrSetComponentInformation(IN PCONFIGURATION_COMPONENT_DATA ComponentData,
57 IN IDENTIFIER_FLAG Flags,
58 IN ULONG Key,
59 IN ULONG Affinity)
60 {
61 PCONFIGURATION_COMPONENT Component = &ComponentData->ComponentEntry;
62
63 /* Set component information */
64 Component->Flags = Flags;
65 Component->Version = 0;
66 Component->Revision = 0;
67 Component->Key = Key;
68 Component->AffinityMask = Affinity;
69 }
70
71 VOID
72 NTAPI
73 FldrSetIdentifier(IN PCONFIGURATION_COMPONENT_DATA ComponentData,
74 IN PCHAR IdentifierString)
75 {
76 ULONG IdentifierLength;
77 PCONFIGURATION_COMPONENT Component = &ComponentData->ComponentEntry;
78 PCHAR Identifier;
79
80 /* Allocate memory for the identifier */
81 IdentifierLength = strlen(IdentifierString) + 1;
82 Identifier = FldrpHwHeapAlloc(IdentifierLength);
83 if (!Identifier) return;
84
85 /* Copy the identifier */
86 RtlCopyMemory(Identifier, IdentifierString, IdentifierLength);
87
88 /* Set component information */
89 Component->IdentifierLength = IdentifierLength;
90 Component->Identifier = Identifier;
91 }
92
93 VOID
94 NTAPI
95 FldrCreateSystemKey(OUT PCONFIGURATION_COMPONENT_DATA *SystemNode)
96 {
97 PCONFIGURATION_COMPONENT Component;
98
99 /* Allocate the root */
100 FldrArcHwTreeRoot = FldrpHwHeapAlloc(sizeof(CONFIGURATION_COMPONENT_DATA));
101 if (!FldrArcHwTreeRoot) return;
102
103 /* Set it up */
104 Component = &FldrArcHwTreeRoot->ComponentEntry;
105 Component->Class = SystemClass;
106 Component->Type = MaximumType;
107 Component->ConfigurationDataLength = 0;
108 Component->Identifier = 0;
109 Component->IdentifierLength = 0;
110
111 /* Return the node */
112 *SystemNode = FldrArcHwTreeRoot;
113 }
114
115 VOID
116 NTAPI
117 FldrLinkToParent(IN PCONFIGURATION_COMPONENT_DATA Parent,
118 IN PCONFIGURATION_COMPONENT_DATA Child)
119 {
120 PCONFIGURATION_COMPONENT_DATA Sibling;
121
122 /* Get the first sibling */
123 Sibling = Parent->Child;
124
125 /* If no sibling exists, then we are the first child */
126 if (!Sibling)
127 {
128 /* Link us in */
129 Parent->Child = Child;
130 }
131 else
132 {
133 /* Loop each sibling */
134 do
135 {
136 /* This is now the parent */
137 Parent = Sibling;
138 } while ((Sibling = Sibling->Sibling));
139
140 /* Found the lowest sibling; mark us as its sibling too */
141 Parent->Sibling = Child;
142 }
143 }
144
145 VOID
146 NTAPI
147 FldrCreateComponentKey(IN PCONFIGURATION_COMPONENT_DATA SystemNode,
148 IN PWCHAR BusName,
149 IN ULONG BusNumber,
150 IN CONFIGURATION_CLASS Class,
151 IN CONFIGURATION_TYPE Type,
152 OUT PCONFIGURATION_COMPONENT_DATA *ComponentKey)
153 {
154 PCONFIGURATION_COMPONENT_DATA ComponentData;
155 PCONFIGURATION_COMPONENT Component;
156
157 /* Allocate the node for this component */
158 ComponentData = FldrpHwHeapAlloc(sizeof(CONFIGURATION_COMPONENT_DATA));
159 if (!ComponentData) return;
160
161 /* Now save our parent */
162 ComponentData->Parent = SystemNode;
163
164 /* Link us to the parent */
165 FldrLinkToParent(SystemNode, ComponentData);
166
167 /* Set us up */
168 Component = &ComponentData->ComponentEntry;
169 Component->Class = Class;
170 Component->Type = Type;
171
172 /* Return the child */
173 *ComponentKey = ComponentData;
174 }
175
176 VOID
177 NTAPI
178 FldrSetConfigurationData(IN PCONFIGURATION_COMPONENT_DATA ComponentData,
179 IN PCM_PARTIAL_RESOURCE_LIST ResourceList,
180 IN ULONG Size)
181 {
182 PCONFIGURATION_COMPONENT Component = &ComponentData->ComponentEntry;
183 PVOID ConfigurationData;
184
185 /* Allocate a buffer from the hardware heap */
186 ConfigurationData = FldrpHwHeapAlloc(Size);
187 if (!ConfigurationData) return;
188
189 /* Copy component information */
190 RtlCopyMemory(ConfigurationData, ResourceList, Size);
191
192 /* Set component information */
193 ComponentData->ConfigurationData = ConfigurationData;
194 Component->ConfigurationDataLength = Size;
195 }