Merge FldrCreateComponentKey and FldrSetComponentInformation
[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 FldrSetIdentifier(IN PCONFIGURATION_COMPONENT_DATA ComponentData,
57 IN PCHAR IdentifierString)
58 {
59 ULONG IdentifierLength;
60 PCONFIGURATION_COMPONENT Component = &ComponentData->ComponentEntry;
61 PCHAR Identifier;
62
63 /* Allocate memory for the identifier */
64 IdentifierLength = strlen(IdentifierString) + 1;
65 Identifier = FldrpHwHeapAlloc(IdentifierLength);
66 if (!Identifier) return;
67
68 /* Copy the identifier */
69 RtlCopyMemory(Identifier, IdentifierString, IdentifierLength);
70
71 /* Set component information */
72 Component->IdentifierLength = IdentifierLength;
73 Component->Identifier = Identifier;
74 }
75
76 VOID
77 NTAPI
78 FldrCreateSystemKey(OUT PCONFIGURATION_COMPONENT_DATA *SystemNode)
79 {
80 PCONFIGURATION_COMPONENT Component;
81
82 /* Allocate the root */
83 FldrArcHwTreeRoot = FldrpHwHeapAlloc(sizeof(CONFIGURATION_COMPONENT_DATA));
84 if (!FldrArcHwTreeRoot) return;
85
86 /* Set it up */
87 Component = &FldrArcHwTreeRoot->ComponentEntry;
88 Component->Class = SystemClass;
89 Component->Type = MaximumType;
90 Component->ConfigurationDataLength = 0;
91 Component->Identifier = 0;
92 Component->IdentifierLength = 0;
93 Component->Flags = 0;
94 Component->Version = 0;
95 Component->Revision = 0;
96 Component->Key = 0;
97 Component->AffinityMask = 0xFFFFFFFF;
98
99 /* Return the node */
100 *SystemNode = FldrArcHwTreeRoot;
101 }
102
103 VOID
104 NTAPI
105 FldrLinkToParent(IN PCONFIGURATION_COMPONENT_DATA Parent,
106 IN PCONFIGURATION_COMPONENT_DATA Child)
107 {
108 PCONFIGURATION_COMPONENT_DATA Sibling;
109
110 /* Get the first sibling */
111 Sibling = Parent->Child;
112
113 /* If no sibling exists, then we are the first child */
114 if (!Sibling)
115 {
116 /* Link us in */
117 Parent->Child = Child;
118 }
119 else
120 {
121 /* Loop each sibling */
122 do
123 {
124 /* This is now the parent */
125 Parent = Sibling;
126 } while ((Sibling = Sibling->Sibling));
127
128 /* Found the lowest sibling; mark us as its sibling too */
129 Parent->Sibling = Child;
130 }
131 }
132
133 VOID
134 NTAPI
135 FldrCreateComponentKey(IN PCONFIGURATION_COMPONENT_DATA SystemNode,
136 IN CONFIGURATION_CLASS Class,
137 IN CONFIGURATION_TYPE Type,
138 IN IDENTIFIER_FLAG Flags,
139 IN ULONG Key,
140 IN ULONG Affinity,
141 OUT PCONFIGURATION_COMPONENT_DATA *ComponentKey)
142 {
143 PCONFIGURATION_COMPONENT_DATA ComponentData;
144 PCONFIGURATION_COMPONENT Component;
145
146 /* Allocate the node for this component */
147 ComponentData = FldrpHwHeapAlloc(sizeof(CONFIGURATION_COMPONENT_DATA));
148 if (!ComponentData) return;
149
150 /* Now save our parent */
151 ComponentData->Parent = SystemNode;
152
153 /* Link us to the parent */
154 FldrLinkToParent(SystemNode, ComponentData);
155
156 /* Set us up */
157 Component = &ComponentData->ComponentEntry;
158 Component->Class = Class;
159 Component->Type = Type;
160
161 /* Return the child */
162 *ComponentKey = ComponentData;
163 }
164
165 VOID
166 NTAPI
167 FldrSetConfigurationData(IN PCONFIGURATION_COMPONENT_DATA ComponentData,
168 IN PCM_PARTIAL_RESOURCE_LIST ResourceList,
169 IN ULONG Size)
170 {
171 PCONFIGURATION_COMPONENT Component = &ComponentData->ComponentEntry;
172 PVOID ConfigurationData;
173
174 /* Allocate a buffer from the hardware heap */
175 ConfigurationData = FldrpHwHeapAlloc(Size);
176 if (!ConfigurationData) return;
177
178 /* Copy component information */
179 RtlCopyMemory(ConfigurationData, ResourceList, Size);
180
181 /* Set component information */
182 ComponentData->ConfigurationData = ConfigurationData;
183 Component->ConfigurationDataLength = Size;
184 }