Fix some Rtl Prototype inconsistencies, more are in ntifs/winddk but i have fixed...
[reactos.git] / reactos / boot / freeldr / freeldr / oslist.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <freeldr.h>
21 #include <inifile.h>
22 #include <oslist.h>
23 #include <rtl.h>
24 #include <mm.h>
25 #include <ui.h>
26
27 BOOL InitOperatingSystemList(PUCHAR **SectionNamesPointer, PUCHAR **DisplayNamesPointer, U32* OperatingSystemCountPointer)
28 {
29 U32 Idx;
30 U32 CurrentOperatingSystemIndex;
31 UCHAR SettingName[260];
32 UCHAR SettingValue[260];
33 U32 OperatingSystemCount;
34 U32 SectionId;
35 U32 OperatingSystemSectionId;
36 U32 SectionSettingCount;
37 PUCHAR *OperatingSystemSectionNames;
38 PUCHAR *OperatingSystemDisplayNames;
39
40 //
41 // Open the [FreeLoader] section
42 //
43 if (!IniOpenSection("Operating Systems", &SectionId))
44 {
45 UiMessageBox("Section [Operating Systems] not found in freeldr.ini.");
46 return FALSE;
47 }
48
49 SectionSettingCount = IniGetNumSectionItems(SectionId);
50 OperatingSystemCount = CountOperatingSystems(SectionId);
51
52 //
53 // Allocate memory to hold operating system lists
54 //
55 if (!AllocateListMemory(&OperatingSystemSectionNames, &OperatingSystemDisplayNames, OperatingSystemCount))
56 {
57 return FALSE;
58 }
59
60 //
61 // Now loop through and read the operating system section and display names
62 //
63 CurrentOperatingSystemIndex = 0;
64 for (Idx=0; Idx<SectionSettingCount; Idx++)
65 {
66 IniReadSettingByNumber(SectionId, Idx, SettingName, 260, SettingValue, 260);
67
68 if (IniOpenSection(SettingName, &OperatingSystemSectionId))
69 {
70 // Copy the section name
71 strcpy(OperatingSystemSectionNames[CurrentOperatingSystemIndex], SettingName);
72
73 // Copy the display name
74 RemoveQuotes(SettingValue);
75 strcpy(OperatingSystemDisplayNames[CurrentOperatingSystemIndex], SettingValue);
76
77 CurrentOperatingSystemIndex++;
78 }
79 }
80
81 *OperatingSystemCountPointer = OperatingSystemCount;
82 *SectionNamesPointer = OperatingSystemSectionNames;
83 *DisplayNamesPointer = OperatingSystemDisplayNames;
84
85 return TRUE;
86 }
87
88 U32 CountOperatingSystems(U32 SectionId)
89 {
90 U32 Idx;
91 UCHAR SettingName[260];
92 UCHAR SettingValue[260];
93 U32 OperatingSystemCount = 0;
94 U32 SectionSettingCount;
95
96 //
97 // Loop through and count the operating systems
98 //
99 SectionSettingCount = IniGetNumSectionItems(SectionId);
100 for (Idx=0; Idx<SectionSettingCount; Idx++)
101 {
102 IniReadSettingByNumber(SectionId, Idx, SettingName, 260, SettingValue, 260);
103
104 if (IniOpenSection(SettingName, NULL))
105 {
106 OperatingSystemCount++;
107 }
108 else
109 {
110 sprintf(SettingName, "Operating System '%s' is listed in\nfreeldr.ini but doesn't have a [section].", SettingValue);
111 UiMessageBox(SettingName);
112 }
113 }
114
115 return OperatingSystemCount;
116 }
117
118 BOOL AllocateListMemory(PUCHAR **SectionNamesPointer, PUCHAR **DisplayNamesPointer, U32 OperatingSystemCount)
119 {
120 U32 Idx;
121 PUCHAR *OperatingSystemSectionNames = NULL;
122 PUCHAR *OperatingSystemDisplayNames = NULL;
123
124 //
125 // Allocate memory to hold operating system list arrays
126 //
127 OperatingSystemSectionNames = (PUCHAR*) MmAllocateMemory( sizeof(PUCHAR) * OperatingSystemCount);
128 OperatingSystemDisplayNames = (PUCHAR*) MmAllocateMemory( sizeof(PUCHAR) * OperatingSystemCount);
129
130 //
131 // If either allocation failed then return FALSE
132 //
133 if ( (OperatingSystemSectionNames == NULL) || (OperatingSystemDisplayNames == NULL) )
134 {
135 if (OperatingSystemSectionNames != NULL)
136 {
137 MmFreeMemory(OperatingSystemSectionNames);
138 }
139
140 if (OperatingSystemDisplayNames != NULL)
141 {
142 MmFreeMemory(OperatingSystemDisplayNames);
143 }
144
145 return FALSE;
146 }
147
148 //
149 // Clear our newly allocated memory
150 //
151 memset(OperatingSystemSectionNames, 0, sizeof(PUCHAR) * OperatingSystemCount);
152 memset(OperatingSystemDisplayNames, 0, sizeof(PUCHAR) * OperatingSystemCount);
153
154 //
155 // Loop through each array element and allocate it's string memory
156 //
157 for (Idx=0; Idx<OperatingSystemCount; Idx++)
158 {
159 OperatingSystemSectionNames[Idx] = (PUCHAR) MmAllocateMemory(80);
160 OperatingSystemDisplayNames[Idx] = (PUCHAR) MmAllocateMemory(80);
161
162 //
163 // If it failed then jump to the cleanup code
164 //
165 if ( (OperatingSystemSectionNames[Idx] == NULL) || (OperatingSystemDisplayNames[Idx] == NULL))
166 {
167 goto AllocateListMemoryFailed;
168 }
169 }
170
171 *SectionNamesPointer = OperatingSystemSectionNames;
172 *DisplayNamesPointer = OperatingSystemDisplayNames;
173
174 return TRUE;
175
176 AllocateListMemoryFailed:
177
178 //
179 // Loop through each array element and free it's string memory
180 //
181 for (Idx=0; Idx<OperatingSystemCount; Idx++)
182 {
183 if (OperatingSystemSectionNames[Idx] != NULL)
184 {
185 MmFreeMemory(OperatingSystemSectionNames[Idx]);
186 }
187
188 if (OperatingSystemDisplayNames[Idx] != NULL)
189 {
190 MmFreeMemory(OperatingSystemDisplayNames[Idx]);
191 }
192 }
193
194 //
195 // Free operating system list arrays
196 //
197 MmFreeMemory(OperatingSystemSectionNames);
198 MmFreeMemory(OperatingSystemDisplayNames);
199
200 return FALSE;
201 }
202
203 BOOL RemoveQuotes(PUCHAR QuotedString)
204 {
205 UCHAR TempString[200];
206
207 //
208 // If this string is not quoted then return FALSE
209 //
210 if ((QuotedString[0] != '\"') && (QuotedString[strlen(QuotedString)-1] != '\"'))
211 {
212 return FALSE;
213 }
214
215 if (QuotedString[0] == '\"')
216 {
217 strcpy(TempString, (QuotedString + 1));
218 }
219 else
220 {
221 strcpy(TempString, QuotedString);
222 }
223
224 if (TempString[strlen(TempString)-1] == '\"')
225 {
226 TempString[strlen(TempString)-1] = '\0';
227 }
228
229 strcpy(QuotedString, TempString);
230
231 return TRUE;
232 }