Fixed GetExtendedMemorySize(). My i486-DX4/75 didn't like the old routine.
[reactos.git] / freeldr / freeldr / parseini.c
1 /*
2 * FreeLoader
3 * Copyright (C) 2001 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 "parseini.h"
22 #include "tui.h"
23 #include "fs.h"
24 #include "stdlib.h"
25 #include "memory.h"
26 #include "debug.h"
27
28 PUCHAR FreeLoaderIniFileData = NULL;
29 ULONG FreeLoaderIniFileSize = 0;
30
31 BOOL ParseIniFile(VOID)
32 {
33 //int i;
34 //char name[1024];
35 //char value[1024];
36 PFILE Freeldr_Ini; // File handle for freeldr.ini
37
38 // Open the boot drive for file access
39 if (!OpenDiskDrive(BootDrive, 0))
40 {
41 printf("Error opening boot drive for file access.\n");
42 return FALSE;
43 }
44
45 // Try to open freeldr.ini or fail
46 Freeldr_Ini = OpenFile("freeldr.ini");
47 if (Freeldr_Ini == NULL)
48 {
49 printf("FREELDR.INI not found.\nYou need to re-install FreeLoader.\n");
50 return FALSE;
51 }
52
53 // Get the file size & allocate enough memory for it
54 FreeLoaderIniFileSize = GetFileSize(Freeldr_Ini);
55 FreeLoaderIniFileData = AllocateMemory(FreeLoaderIniFileSize);
56
57 // If we are out of memory then return FALSE
58 if (FreeLoaderIniFileData == NULL)
59 {
60 printf("Out of memory while loading FREELDR.INI.\n");
61 CloseFile(Freeldr_Ini);
62 return FALSE;
63 }
64
65 // Read freeldr.ini off the disk
66 ReadFile(Freeldr_Ini, FreeLoaderIniFileSize, NULL, FreeLoaderIniFileData);
67 CloseFile(Freeldr_Ini);
68
69 // Make sure the [FREELOADER] section exists
70 /*if (OpenSection("FREELOADER", NULL))
71 {
72 printf("Section [FREELOADER] not found in FREELDR.INI.\nYou need to re-install FreeLoader.\n");
73 return FALSE;
74 }
75
76 // Validate the settings in the [FREELOADER] section
77 for (i=1; i<=GetNumSectionItems("FREELOADER"); i++)
78 {
79 ReadSectionSettingByNumber("FREELOADER", i, name, value);
80 if (!IsValidSetting(name, value))
81 {
82 printf("Invalid setting in freeldr.ini.\nName: \"%s\", Value: \"%s\"\n", name, value);
83 printf("Press any key to continue.\n");
84 getch();
85 }
86 else
87 SetSetting(name, value);
88 }*/
89
90 return TRUE;
91 }
92
93 ULONG GetNextLineOfFileData(PUCHAR Buffer, ULONG BufferSize, ULONG CurrentOffset)
94 {
95 ULONG Idx;
96
97 // Loop through grabbing chars until we hit the end of the
98 // file or we encounter a new line char
99 for (Idx=0; (CurrentOffset < FreeLoaderIniFileSize); CurrentOffset++)
100 {
101 // If we haven't exceeded our buffer size yet
102 // then store another char
103 if (Idx < (BufferSize - 1))
104 {
105 Buffer[Idx++] = FreeLoaderIniFileData[CurrentOffset];
106 }
107
108 // Check for new line char
109 if (FreeLoaderIniFileData[CurrentOffset] == '\n')
110 {
111 CurrentOffset++;
112 break;
113 }
114 }
115
116 // Terminate the string
117 Buffer[Idx] = '\0';
118
119 // Get rid of newline & linefeed characters (if any)
120 if((Buffer[strlen(Buffer)-1] == '\n') || (Buffer[strlen(Buffer)-1] == '\r'))
121 Buffer[strlen(Buffer)-1] = '\0';
122 if((Buffer[strlen(Buffer)-1] == '\n') || (Buffer[strlen(Buffer)-1] == '\r'))
123 Buffer[strlen(Buffer)-1] = '\0';
124
125 // Send back new offset
126 return CurrentOffset;
127 }
128
129 BOOL OpenSection(PUCHAR SectionName, PULONG SectionId)
130 {
131 UCHAR TempString[80];
132 UCHAR RealSectionName[80];
133 ULONG FileOffset;
134 BOOL SectionFound = FALSE;
135
136 //
137 // Get the real section name
138 //
139 strcpy(RealSectionName, "[");
140 strcat(RealSectionName, SectionName);
141 strcat(RealSectionName, "]");
142
143 //
144 // Get to the beginning of the file
145 //
146 FileOffset = 0;
147
148 //
149 // Find the section
150 //
151 while (FileOffset < FreeLoaderIniFileSize)
152 {
153 //
154 // Read a line
155 //
156 FileOffset = GetNextLineOfFileData(TempString, 80, FileOffset);
157
158 //
159 // If it isn't a section header then continue on
160 //
161 if (TempString[0] != '[')
162 continue;
163
164 //
165 // Check and see if we found it
166 //
167 if (stricmp(TempString, RealSectionName) == 0)
168 {
169 SectionFound = TRUE;
170 break;
171 }
172 }
173
174 if (SectionId)
175 {
176 *SectionId = FileOffset;
177 }
178
179 return SectionFound;
180 }
181
182 ULONG GetNumSectionItems(ULONG SectionId)
183 {
184 UCHAR TempString[80];
185 ULONG SectionItemCount = 0;
186
187 // Now count how many settings are in this section
188 while (SectionId < FreeLoaderIniFileSize)
189 {
190 // Read a line
191 SectionId = GetNextLineOfFileData(TempString, 80, SectionId);
192
193 // If we hit a new section then we're done
194 if (TempString[0] == '[')
195 break;
196
197 // Skip comments
198 if (TempString[0] == '#')
199 continue;
200
201 // Skip blank lines
202 if (!strlen(TempString))
203 continue;
204
205 SectionItemCount++;
206 }
207
208 return SectionItemCount;
209 }
210
211 BOOL ReadSectionSettingByNumber(ULONG SectionId, ULONG SettingNumber, PUCHAR SettingName, ULONG NameSize, PUCHAR SettingValue, ULONG ValueSize)
212 {
213 UCHAR TempString[1024];
214 ULONG SectionItemCount = 0;
215 ULONG Idx;
216 ULONG FileOffset;
217
218 //
219 // Get to the beginning of the section
220 //
221 FileOffset = SectionId;
222
223 //
224 // Now find the setting we are looking for
225 //
226 do
227 {
228 // Read a line
229 FileOffset = GetNextLineOfFileData(TempString, 1024, FileOffset);
230
231 // Skip comments
232 if (TempString[0] == '#')
233 continue;
234
235 // Skip blank lines
236 if (!strlen(TempString))
237 continue;
238
239 // If we hit a new section then we're done
240 if (TempString[0] == '[')
241 break;
242
243 // Check and see if we found the setting
244 if (SectionItemCount == SettingNumber)
245 {
246 for (Idx=0; Idx<strlen(TempString); Idx++)
247 {
248 // Check and see if this character is the separator
249 if (TempString[Idx] == '=')
250 {
251 SettingName[Idx] = '\0';
252
253 strncpy(SettingValue, TempString + Idx + 1, ValueSize);
254
255 return TRUE;
256 }
257 else if (Idx < NameSize)
258 {
259 SettingName[Idx] = TempString[Idx];
260 }
261 }
262 }
263
264 // Increment setting number
265 SectionItemCount++;
266 }
267 while (FileOffset < FreeLoaderIniFileSize);
268
269 return FALSE;
270 }
271
272 BOOL ReadSectionSettingByName(ULONG SectionId, PUCHAR SettingName, PUCHAR Buffer, ULONG BufferSize)
273 {
274 UCHAR TempString[1024];
275 UCHAR TempBuffer[80];
276 ULONG Idx;
277 ULONG FileOffset;
278
279 //
280 // Get to the beginning of the section
281 //
282 FileOffset = SectionId;
283
284 //
285 // Now find the setting we are looking for
286 //
287 while (FileOffset < FreeLoaderIniFileSize)
288 {
289 // Read a line
290 FileOffset = GetNextLineOfFileData(TempString, 1024, FileOffset);
291
292 // Skip comments
293 if (TempString[0] == '#')
294 continue;
295
296 // Skip blank lines
297 if (!strlen(TempString))
298 continue;
299
300 // If we hit a new section then we're done
301 if (TempString[0] == '[')
302 break;
303
304 // Extract the setting name
305 for (Idx=0; Idx<strlen(TempString); Idx++)
306 {
307 if (TempString[Idx] != '=')
308 TempBuffer[Idx] = TempString[Idx];
309 else
310 {
311 TempBuffer[Idx] = '\0';
312 break;
313 }
314 }
315
316 // Check and see if we found the setting
317 if (stricmp(TempBuffer, SettingName) == 0)
318 {
319 for (Idx=0; Idx<strlen(TempString); Idx++)
320 {
321 // Check and see if this character is the separator
322 if (TempString[Idx] == '=')
323 {
324 strcpy(Buffer, TempString + Idx + 1);
325
326 return TRUE;
327 }
328 }
329 }
330 }
331
332 return FALSE;
333 }
334
335 BOOL IsValidSetting(char *setting, char *value)
336 {
337 if(stricmp(setting, "MessageBox") == 0)
338 return TRUE;
339 else if(stricmp(setting, "MessageLine") == 0)
340 return TRUE;
341 else if(stricmp(setting, "TitleText") == 0)
342 return TRUE;
343 else if(stricmp(setting, "StatusBarColor") == 0)
344 {
345 if(IsValidColor(value))
346 return TRUE;
347 }
348 else if(stricmp(setting, "StatusBarTextColor") == 0)
349 {
350 if(IsValidColor(value))
351 return TRUE;
352 }
353 else if(stricmp(setting, "BackdropTextColor") == 0)
354 {
355 if(IsValidColor(value))
356 return TRUE;
357 }
358 else if(stricmp(setting, "BackdropColor") == 0)
359 {
360 if(IsValidColor(value))
361 return TRUE;
362 }
363 else if(stricmp(setting, "BackdropFillStyle") == 0)
364 {
365 if(IsValidFillStyle(value))
366 return TRUE;
367 }
368 else if(stricmp(setting, "TitleBoxTextColor") == 0)
369 {
370 if(IsValidColor(value))
371 return TRUE;
372 }
373 else if(stricmp(setting, "TitleBoxColor") == 0)
374 {
375 if(IsValidColor(value))
376 return TRUE;
377 }
378 else if(stricmp(setting, "MessageBoxTextColor") == 0)
379 {
380 if(IsValidColor(value))
381 return TRUE;
382 }
383 else if(stricmp(setting, "MessageBoxColor") == 0)
384 {
385 if(IsValidColor(value))
386 return TRUE;
387 }
388 else if(stricmp(setting, "MenuTextColor") == 0)
389 {
390 if(IsValidColor(value))
391 return TRUE;
392 }
393 else if(stricmp(setting, "MenuColor") == 0)
394 {
395 if(IsValidColor(value))
396 return TRUE;
397 }
398 else if(stricmp(setting, "TextColor") == 0)
399 {
400 if(IsValidColor(value))
401 return TRUE;
402 }
403 else if(stricmp(setting, "SelectedTextColor") == 0)
404 {
405 if(IsValidColor(value))
406 return TRUE;
407 }
408 else if(stricmp(setting, "SelectedColor") == 0)
409 {
410 if(IsValidColor(value))
411 return TRUE;
412 }
413 else if(stricmp(setting, "OS") == 0)
414 return TRUE;
415 else if(stricmp(setting, "TimeOut") == 0)
416 return TRUE;
417 /*else if(stricmp(setting, "") == 0)
418 return TRUE;
419 else if(stricmp(setting, "") == 0)
420 return TRUE;
421 else if(stricmp(setting, "") == 0)
422 return TRUE;
423 else if(stricmp(setting, "") == 0)
424 return TRUE;
425 else if(stricmp(setting, "") == 0)
426 return TRUE;
427 else if(stricmp(setting, "") == 0)
428 return TRUE;
429 else if(stricmp(setting, "") == 0)
430 return TRUE;*/
431
432 return FALSE;
433 }
434
435 /*void SetSetting(char *setting, char *value)
436 {
437 char v[260];
438
439 if(stricmp(setting, "TitleText") == 0)
440 strcpy(szTitleBoxTitleText, value);
441 else if(stricmp(setting, "StatusBarColor") == 0)
442 cStatusBarBgColor = TextToColor(value);
443 else if(stricmp(setting, "StatusBarTextColor") == 0)
444 cStatusBarFgColor = TextToColor(value);
445 else if(stricmp(setting, "BackdropTextColor") == 0)
446 cBackdropFgColor = TextToColor(value);
447 else if(stricmp(setting, "BackdropColor") == 0)
448 cBackdropBgColor = TextToColor(value);
449 else if(stricmp(setting, "BackdropFillStyle") == 0)
450 cBackdropFillStyle = TextToFillStyle(value);
451 else if(stricmp(setting, "TitleBoxTextColor") == 0)
452 cTitleBoxFgColor = TextToColor(value);
453 else if(stricmp(setting, "TitleBoxColor") == 0)
454 cTitleBoxBgColor = TextToColor(value);
455 else if(stricmp(setting, "MessageBoxTextColor") == 0)
456 cMessageBoxFgColor = TextToColor(value);
457 else if(stricmp(setting, "MessageBoxColor") == 0)
458 cMessageBoxBgColor = TextToColor(value);
459 else if(stricmp(setting, "MenuTextColor") == 0)
460 cMenuFgColor = TextToColor(value);
461 else if(stricmp(setting, "MenuColor") == 0)
462 cMenuBgColor = TextToColor(value);
463 else if(stricmp(setting, "TextColor") == 0)
464 cTextColor = TextToColor(value);
465 else if(stricmp(setting, "SelectedTextColor") == 0)
466 cSelectedTextColor = TextToColor(value);
467 else if(stricmp(setting, "SelectedColor") == 0)
468 cSelectedTextBgColor = TextToColor(value);
469 else if(stricmp(setting, "OS") == 0)
470 {
471 if(nNumOS >= 16)
472 {
473 printf("Error: you can only boot to at most 16 different operating systems.\n");
474 printf("Press any key to continue\n");
475 getch();
476 return;
477 }
478
479 if(!GetNumSectionItems(value))
480 {
481 printf("Error: OS \"%s\" listed.\n", value);
482 printf("It does not have it's own [section], or it is empty.\n");
483 printf("Press any key to continue\n");
484 getch();
485 return;
486 }
487
488 strcpy(OSList[nNumOS].name, value);
489
490 if (!ReadSectionSettingByName(value, "BootType", v))
491 {
492 printf("Unknown BootType for OS \"%s\"\n", value);
493 printf("Press any key to continue\n");
494 getch();
495 return;
496 }
497
498 if (stricmp(v, "ReactOS") == 0)
499 OSList[nNumOS].nOSType = OSTYPE_REACTOS;
500 else if (stricmp(v, "Linux") == 0)
501 OSList[nNumOS].nOSType = OSTYPE_LINUX;
502 else if (stricmp(v, "BootSector") == 0)
503 OSList[nNumOS].nOSType = OSTYPE_BOOTSECTOR;
504 else if (stricmp(v, "Partition") == 0)
505 OSList[nNumOS].nOSType = OSTYPE_PARTITION;
506 else if (stricmp(v, "Drive") == 0)
507 OSList[nNumOS].nOSType = OSTYPE_DRIVE;
508 else
509 {
510 printf("Unknown BootType for OS \"%s\"\n", value);
511 printf("Press any key to continue\n");
512 getch();
513 return;
514 }
515
516 nNumOS++;
517 }
518 else if(stricmp(setting, "TimeOut") == 0)
519 nTimeOut = atoi(value);
520 }*/