* Sync up to trunk head (r64959).
[reactos.git] / 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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <freeldr.h>
21
22 #define TAG_STRING ' rtS'
23 #define TAG_OS_ITEM 'tISO'
24
25 static PCSTR CopyString(PCSTR Source)
26 {
27 PSTR Dest;
28
29 if (!Source)
30 return NULL;
31 Dest = FrLdrHeapAlloc(strlen(Source) + 1, TAG_STRING);
32 if (Dest)
33 {
34 strcpy(Dest, Source);
35 }
36
37 return Dest;
38 }
39
40 OperatingSystemItem* InitOperatingSystemList(ULONG* OperatingSystemCountPointer)
41 {
42 ULONG Idx;
43 CHAR SettingName[260];
44 CHAR SettingValue[260];
45 ULONG_PTR SectionId;
46 PCHAR TitleStart, TitleEnd;
47 PCSTR OsLoadOptions;
48 ULONG Count;
49 OperatingSystemItem* Items;
50
51 //
52 // Open the [FreeLoader] section
53 //
54 if (!IniOpenSection("Operating Systems", &SectionId))
55 {
56 return NULL;
57 }
58
59 //
60 // Count number of operating systems in the section
61 //
62 Count = IniGetNumSectionItems(SectionId);
63
64 //
65 // Allocate memory to hold operating system lists
66 //
67 Items = FrLdrHeapAlloc(Count * sizeof(OperatingSystemItem), TAG_OS_ITEM);
68 if (!Items)
69 {
70 return NULL;
71 }
72
73 //
74 // Now loop through and read the operating system section and display names
75 //
76 for (Idx = 0; Idx < Count; Idx++)
77 {
78 IniReadSettingByNumber(SectionId, Idx, SettingName, sizeof(SettingName), SettingValue, sizeof(SettingValue));
79
80 //
81 // Search start and end of the title
82 //
83 OsLoadOptions = NULL;
84 TitleStart = SettingValue;
85 while (*TitleStart == ' ' || *TitleStart == '"')
86 TitleStart++;
87 TitleEnd = TitleStart;
88 if (*TitleEnd != ANSI_NULL)
89 TitleEnd++;
90 while (*TitleEnd != ANSI_NULL && *TitleEnd != '"')
91 TitleEnd++;
92 if (*TitleEnd != ANSI_NULL)
93 {
94 *TitleEnd = ANSI_NULL;
95 OsLoadOptions = TitleEnd + 1;
96 }
97
98 //
99 // Copy the system partition, identifier and options
100 //
101 Items[Idx].SystemPartition = CopyString(SettingName);
102 Items[Idx].LoadIdentifier = CopyString(TitleStart);
103 Items[Idx].OsLoadOptions = CopyString(OsLoadOptions);
104 }
105
106 //
107 // Return success
108 //
109 *OperatingSystemCountPointer = Count;
110 return Items;
111 }