60c218f5fdda75453f77be9d7b69785597a39f20
[reactos.git] / reactos / boot / freeldr / freeldr / windows / setupldr.c
1 /*
2 * FreeLoader
3 *
4 * Copyright (C) 2009 Aleksey Bragin <aleksey@reactos.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <freeldr.h>
22
23 #include <ndk/ldrtypes.h>
24 #include <arc/setupblk.h>
25
26 #include <debug.h>
27
28 DBG_DEFAULT_CHANNEL(WINDOWS);
29
30 void
31 WinLdrSetupMachineDependent(PLOADER_PARAMETER_BLOCK LoaderBlock);
32
33 VOID
34 WinLdrSetProcessorContext(void);
35
36 // TODO: Move to .h
37 VOID AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK *OutLoaderBlock);
38
39 static VOID
40 SetupLdrLoadNlsData(PLOADER_PARAMETER_BLOCK LoaderBlock, HINF InfHandle, LPCSTR SearchPath)
41 {
42 INFCONTEXT InfContext;
43 BOOLEAN Status;
44 LPCSTR AnsiName, OemName, LangName;
45
46 /* Get ANSI codepage file */
47 if (!InfFindFirstLine(InfHandle, "NLS", "AnsiCodepage", &InfContext))
48 {
49 ERR("Failed to find 'NLS/AnsiCodepage'\n");
50 return;
51 }
52 if (!InfGetDataField(&InfContext, 1, &AnsiName))
53 {
54 ERR("Failed to get load options\n");
55 return;
56 }
57
58 /* Get OEM codepage file */
59 if (!InfFindFirstLine(InfHandle, "NLS", "OemCodepage", &InfContext))
60 {
61 ERR("Failed to find 'NLS/AnsiCodepage'\n");
62 return;
63 }
64 if (!InfGetDataField(&InfContext, 1, &OemName))
65 {
66 ERR("Failed to get load options\n");
67 return;
68 }
69
70 if (!InfFindFirstLine(InfHandle, "NLS", "UnicodeCasetable", &InfContext))
71 {
72 ERR("Failed to find 'NLS/AnsiCodepage'\n");
73 return;
74 }
75 if (!InfGetDataField(&InfContext, 1, &LangName))
76 {
77 ERR("Failed to get load options\n");
78 return;
79 }
80
81 Status = WinLdrLoadNLSData(LoaderBlock, SearchPath, AnsiName, OemName, LangName);
82 TRACE("NLS data loaded with status %d\n", Status);
83 }
84
85 static VOID
86 SetupLdrScanBootDrivers(PLIST_ENTRY BootDriverListHead, HINF InfHandle, LPCSTR SearchPath)
87 {
88 INFCONTEXT InfContext, dirContext;
89 BOOLEAN Status;
90 LPCSTR Media, DriverName, dirIndex, ImagePath;
91 WCHAR ServiceName[256];
92 WCHAR ImagePathW[256];
93
94 /* Open inf section */
95 if (!InfFindFirstLine(InfHandle, "SourceDisksFiles", NULL, &InfContext))
96 return;
97
98 /* Load all listed boot drivers */
99 do
100 {
101 if (InfGetDataField(&InfContext, 7, &Media) &&
102 InfGetDataField(&InfContext, 0, &DriverName) &&
103 InfGetDataField(&InfContext, 13, &dirIndex))
104 {
105 if ((strcmp(Media, "x") == 0) &&
106 InfFindFirstLine(InfHandle, "Directories", dirIndex, &dirContext) &&
107 InfGetDataField(&dirContext, 1, &ImagePath))
108 {
109 /* Convert name to widechar */
110 swprintf(ServiceName, L"%S", DriverName);
111
112 /* Prepare image path */
113 swprintf(ImagePathW, L"%S", ImagePath);
114 wcscat(ImagePathW, L"\\");
115 wcscat(ImagePathW, ServiceName);
116
117 /* Remove .sys extension */
118 ServiceName[wcslen(ServiceName) - 4] = 0;
119
120 /* Add it to the list */
121 Status = WinLdrAddDriverToList(BootDriverListHead,
122 L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\",
123 ImagePathW,
124 ServiceName);
125
126 if (!Status)
127 {
128 ERR("could not add boot driver %s, %s\n", SearchPath, DriverName);
129 return;
130 }
131 }
132 }
133 } while (InfFindNextLine(&InfContext, &InfContext));
134 }
135
136 VOID LoadReactOSSetup(VOID)
137 {
138 CHAR FileName[512];
139 CHAR BootPath[512];
140 LPCSTR LoadOptions, BootOptions;
141 BOOLEAN BootFromFloppy;
142 ULONG i, ErrorLine;
143 HINF InfHandle;
144 INFCONTEXT InfContext;
145 PLOADER_PARAMETER_BLOCK LoaderBlock;
146 PSETUP_LOADER_BLOCK SetupBlock;
147 LPCSTR SystemPath;
148 LPCSTR SourcePaths[] =
149 {
150 "\\", /* Only for floppy boot */
151 #if defined(_M_IX86)
152 "\\I386\\",
153 #elif defined(_M_MPPC)
154 "\\PPC\\",
155 #elif defined(_M_MRX000)
156 "\\MIPS\\",
157 #endif
158 "\\reactos\\",
159 NULL
160 };
161
162 /* Get boot path */
163 MachDiskGetBootPath(BootPath, sizeof(BootPath));
164
165 /* And check if we booted from floppy */
166 BootFromFloppy = strstr(BootPath, "fdisk") != NULL;
167
168 /* Open 'txtsetup.sif' from any of source paths */
169 for (i = BootFromFloppy ? 0 : 1; ; i++)
170 {
171 SystemPath = SourcePaths[i];
172 if (!SystemPath)
173 {
174 ERR("Failed to open txtsetup.sif\n");
175 return;
176 }
177 sprintf(FileName, "%stxtsetup.sif", SystemPath);
178 if (InfOpenFile (&InfHandle, FileName, &ErrorLine))
179 {
180 strcat(BootPath, SystemPath);
181 break;
182 }
183 }
184
185 TRACE("BootPath: '%s', SystemPath: '%s'\n", BootPath, SystemPath);
186
187 /* Get Load options - debug and non-debug */
188 if (!InfFindFirstLine(InfHandle, "SetupData", "OsLoadOptions", &InfContext))
189 {
190 ERR("Failed to find 'SetupData/OsLoadOptions'\n");
191 return;
192 }
193
194 if (!InfGetDataField (&InfContext, 1, &LoadOptions))
195 {
196 ERR("Failed to get load options\n");
197 return;
198 }
199
200 BootOptions = LoadOptions;
201
202 #if DBG
203 /* Get debug load options and use them */
204 if (InfFindFirstLine(InfHandle, "SetupData", "DbgOsLoadOptions", &InfContext))
205 {
206 if (InfGetDataField(&InfContext, 1, &LoadOptions))
207 BootOptions = LoadOptions;
208 }
209 #endif
210
211 TRACE("BootOptions: '%s'\n", BootOptions);
212
213 //SetupUiInitialize();
214 UiDrawStatusText("");
215
216 /* Allocate and minimalistic-initialize LPB */
217 AllocateAndInitLPB(&LoaderBlock);
218
219 /* Allocate and initialize setup loader block */
220 SetupBlock = &WinLdrSystemBlock->SetupBlock;
221 LoaderBlock->SetupLdrBlock = SetupBlock;
222
223 /* Set textmode setup flag */
224 SetupBlock->Flags = SETUPLDR_TEXT_MODE;
225
226 /* Load NLS data, they are in system32 */
227 strcpy(FileName, BootPath);
228 strcat(FileName, "system32\\");
229 SetupLdrLoadNlsData(LoaderBlock, InfHandle, FileName);
230
231 /* Get a list of boot drivers */
232 SetupLdrScanBootDrivers(&LoaderBlock->BootDriverListHead, InfHandle, BootPath);
233
234
235 LoadAndBootWindowsCommon(_WIN32_WINNT_WS03,
236 LoaderBlock,
237 BootOptions,
238 BootPath,
239 1);
240 }