[FREELDR] Some ARC-spec compatibility refactoring + simplifications & fixes.
[reactos.git] / boot / freeldr / freeldr / linuxboot.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 #ifndef _M_ARM
21
22 #ifdef _M_IX86
23
24 /* INCLUDES *******************************************************************/
25
26 #include <freeldr.h>
27 #include <debug.h>
28
29 DBG_DEFAULT_CHANNEL(LINUX);
30
31 /* GLOBALS ********************************************************************/
32
33 #define LINUX_READ_CHUNK_SIZE 0x20000 // Read 128k at a time
34
35 PLINUX_BOOTSECTOR LinuxBootSector = NULL;
36 PLINUX_SETUPSECTOR LinuxSetupSector = NULL;
37 ULONG SetupSectorSize = 0;
38 BOOLEAN NewStyleLinuxKernel = FALSE;
39 ULONG LinuxKernelSize = 0;
40 ULONG LinuxInitrdSize = 0;
41 PCSTR LinuxKernelName = NULL;
42 PCSTR LinuxInitrdName = NULL;
43 PSTR LinuxCommandLine = NULL;
44 ULONG LinuxCommandLineSize = 0;
45 PVOID LinuxKernelLoadAddress = NULL;
46 PVOID LinuxInitrdLoadAddress = NULL;
47 CHAR LinuxBootDescription[80];
48 PCSTR LinuxBootPath = NULL;
49
50 /* FUNCTIONS ******************************************************************/
51
52 VOID
53 RemoveQuotes(
54 IN OUT PSTR QuotedString)
55 {
56 PCHAR p;
57 PSTR Start;
58 SIZE_T Size;
59
60 /* Skip spaces up to " */
61 p = QuotedString;
62 while (*p == ' ' || *p == '\t' || *p == '"')
63 ++p;
64 Start = p;
65
66 /* Go up to next " */
67 while (*p != ANSI_NULL && *p != '"')
68 ++p;
69 /* NULL-terminate */
70 *p = ANSI_NULL;
71
72 /* Move the NULL-terminated string back into 'QuotedString' in place */
73 Size = (strlen(Start) + 1) * sizeof(CHAR);
74 memmove(QuotedString, Start, Size);
75 }
76
77 ARC_STATUS
78 LoadAndBootLinux(
79 IN ULONG Argc,
80 IN PCHAR Argv[],
81 IN PCHAR Envp[])
82 {
83 PCSTR Description;
84 PFILE LinuxKernel = 0;
85 PFILE LinuxInitrdFile = 0;
86
87 Description = GetArgumentValue(Argc, Argv, "LoadIdentifier");
88 if (Description)
89 RtlStringCbPrintfA(LinuxBootDescription, sizeof(LinuxBootDescription), "Loading %s...", Description);
90 else
91 strcpy(LinuxBootDescription, "Loading Linux...");
92
93 UiDrawBackdrop();
94 UiDrawStatusText(LinuxBootDescription);
95 UiDrawProgressBarCenter(0, 100, LinuxBootDescription);
96
97 /* Find all the message box settings and run them */
98 UiShowMessageBoxesInArgv(Argc, Argv);
99
100 /* Parse the .ini file section */
101 if (!LinuxParseIniSection(Argc, Argv))
102 goto LinuxBootFailed;
103
104 /* Open the kernel */
105 LinuxKernel = FsOpenFile(LinuxKernelName);
106 if (!LinuxKernel)
107 {
108 UiMessageBox("Linux kernel \'%s\' not found.", LinuxKernelName);
109 goto LinuxBootFailed;
110 }
111
112 /* Open the initrd file image (if necessary) */
113 if (LinuxInitrdName)
114 {
115 LinuxInitrdFile = FsOpenFile(LinuxInitrdName);
116 if (!LinuxInitrdFile)
117 {
118 UiMessageBox("Linux initrd image \'%s\' not found.", LinuxInitrdName);
119 goto LinuxBootFailed;
120 }
121 }
122
123 /* Read the boot sector */
124 if (!LinuxReadBootSector(LinuxKernel))
125 goto LinuxBootFailed;
126
127 /* Read the setup sector */
128 if (!LinuxReadSetupSector(LinuxKernel))
129 goto LinuxBootFailed;
130
131 /* Calc kernel size */
132 LinuxKernelSize = FsGetFileSize(LinuxKernel) - (512 + SetupSectorSize);
133
134 /* Get the file size */
135 LinuxInitrdSize = FsGetFileSize(LinuxInitrdFile);
136
137 /* Read the kernel */
138 if (!LinuxReadKernel(LinuxKernel))
139 goto LinuxBootFailed;
140
141 /* Read the initrd (if necessary) */
142 if (LinuxInitrdName)
143 {
144 if (!LinuxReadInitrd(LinuxInitrdFile))
145 goto LinuxBootFailed;
146 }
147
148 // If the default root device is set to FLOPPY (0000h), change to /dev/fd0 (0200h)
149 if (LinuxBootSector->RootDevice == 0x0000)
150 LinuxBootSector->RootDevice = 0x0200;
151
152 if (LinuxSetupSector->Version >= 0x0202)
153 {
154 LinuxSetupSector->CommandLinePointer = 0x99000;
155 }
156 else
157 {
158 LinuxBootSector->CommandLineMagic = LINUX_COMMAND_LINE_MAGIC;
159 LinuxBootSector->CommandLineOffset = 0x9000;
160 }
161
162 if (NewStyleLinuxKernel)
163 LinuxSetupSector->TypeOfLoader = LINUX_LOADER_TYPE_FREELOADER;
164 else
165 LinuxSetupSector->LoadFlags = 0;
166
167 RtlCopyMemory((PVOID)0x90000, LinuxBootSector, 512);
168 RtlCopyMemory((PVOID)0x90200, LinuxSetupSector, SetupSectorSize);
169 RtlCopyMemory((PVOID)0x99000,
170 LinuxCommandLine ? LinuxCommandLine : "",
171 LinuxCommandLine ? LinuxCommandLineSize : sizeof(ANSI_NULL));
172
173 UiUnInitialize("Booting Linux...");
174 IniCleanup();
175
176 DiskStopFloppyMotor();
177
178 if (LinuxSetupSector->LoadFlags & LINUX_FLAG_LOAD_HIGH)
179 BootNewLinuxKernel();
180 else
181 BootOldLinuxKernel(LinuxKernelSize);
182
183
184 LinuxBootFailed:
185
186 if (LinuxKernel)
187 FsCloseFile(LinuxKernel);
188
189 if (LinuxInitrdFile)
190 FsCloseFile(LinuxInitrdFile);
191
192 if (LinuxBootSector != NULL)
193 MmFreeMemory(LinuxBootSector);
194
195 if (LinuxSetupSector != NULL)
196 MmFreeMemory(LinuxSetupSector);
197
198 if (LinuxKernelLoadAddress != NULL)
199 MmFreeMemory(LinuxKernelLoadAddress);
200
201 if (LinuxInitrdLoadAddress != NULL)
202 MmFreeMemory(LinuxInitrdLoadAddress);
203
204 LinuxBootSector = NULL;
205 LinuxSetupSector = NULL;
206 SetupSectorSize = 0;
207 NewStyleLinuxKernel = FALSE;
208 LinuxKernelSize = 0;
209 LinuxInitrdSize = 0;
210 LinuxKernelName = NULL;
211 LinuxInitrdName = NULL;
212 LinuxCommandLine = NULL;
213 LinuxCommandLineSize = 0;
214 LinuxKernelLoadAddress = NULL;
215 LinuxInitrdLoadAddress = NULL;
216 *LinuxBootDescription = ANSI_NULL;
217 LinuxBootPath = NULL;
218
219 return ENOEXEC;
220 }
221
222 BOOLEAN
223 LinuxParseIniSection(
224 IN ULONG Argc,
225 IN PCHAR Argv[])
226 {
227 LinuxBootPath = GetArgumentValue(Argc, Argv, "BootPath");
228 if (!LinuxBootPath)
229 {
230 UiMessageBox("Boot path not specified for selected OS!");
231 return FALSE;
232 }
233
234 /* Get the kernel name */
235 LinuxKernelName = GetArgumentValue(Argc, Argv, "Kernel");
236 if (!LinuxKernelName)
237 {
238 UiMessageBox("Linux kernel filename not specified for selected OS!");
239 return FALSE;
240 }
241
242 /* Get the initrd name (optional) */
243 LinuxInitrdName = GetArgumentValue(Argc, Argv, "Initrd");
244
245 /* Get the command line (optional) */
246 LinuxCommandLineSize = 0;
247 LinuxCommandLine = GetArgumentValue(Argc, Argv, "CommandLine");
248 if (LinuxCommandLine)
249 {
250 RemoveQuotes(LinuxCommandLine);
251 LinuxCommandLineSize = min(strlen(LinuxCommandLine) + 1, 260);
252 }
253
254 return TRUE;
255 }
256
257 BOOLEAN LinuxReadBootSector(PFILE LinuxKernelFile)
258 {
259 /* Allocate memory for boot sector */
260 LinuxBootSector = MmAllocateMemoryWithType(512, LoaderSystemCode);
261 if (LinuxBootSector == NULL)
262 return FALSE;
263
264 /* Read linux boot sector */
265 FsSetFilePointer(LinuxKernelFile, 0);
266 if (!FsReadFile(LinuxKernelFile, 512, NULL, LinuxBootSector))
267 return FALSE;
268
269 /* Check for validity */
270 if (LinuxBootSector->BootFlag != LINUX_BOOT_SECTOR_MAGIC)
271 {
272 UiMessageBox("Invalid boot sector magic (0xaa55)");
273 return FALSE;
274 }
275
276 // DbgDumpBuffer(DPRINT_LINUX, LinuxBootSector, 512);
277
278 TRACE("SetupSectors: %d\n" , LinuxBootSector->SetupSectors);
279 TRACE("RootFlags: 0x%x\n", LinuxBootSector->RootFlags);
280 TRACE("SystemSize: 0x%x\n", LinuxBootSector->SystemSize);
281 TRACE("SwapDevice: 0x%x\n", LinuxBootSector->SwapDevice);
282 TRACE("RamSize: 0x%x\n", LinuxBootSector->RamSize);
283 TRACE("VideoMode: 0x%x\n", LinuxBootSector->VideoMode);
284 TRACE("RootDevice: 0x%x\n", LinuxBootSector->RootDevice);
285 TRACE("BootFlag: 0x%x\n", LinuxBootSector->BootFlag);
286
287 return TRUE;
288 }
289
290 BOOLEAN LinuxReadSetupSector(PFILE LinuxKernelFile)
291 {
292 UCHAR TempLinuxSetupSector[512];
293
294 /* Read first linux setup sector */
295 FsSetFilePointer(LinuxKernelFile, 512);
296 if (!FsReadFile(LinuxKernelFile, 512, NULL, TempLinuxSetupSector))
297 return FALSE;
298
299 /* Check the kernel version */
300 LinuxSetupSector = (PLINUX_SETUPSECTOR)TempLinuxSetupSector;
301 if (!LinuxCheckKernelVersion())
302 return FALSE;
303
304 if (NewStyleLinuxKernel)
305 SetupSectorSize = 512 * LinuxBootSector->SetupSectors;
306 else
307 SetupSectorSize = 512 * 4; // Always 4 setup sectors
308
309 /* Allocate memory for setup sectors */
310 LinuxSetupSector = MmAllocateMemoryWithType(SetupSectorSize, LoaderSystemCode);
311 if (LinuxSetupSector == NULL)
312 return FALSE;
313
314 /* Copy over first setup sector */
315 RtlCopyMemory(LinuxSetupSector, TempLinuxSetupSector, 512);
316
317 /* Read in the rest of the linux setup sectors */
318 FsSetFilePointer(LinuxKernelFile, 1024);
319 if (!FsReadFile(LinuxKernelFile, SetupSectorSize - 512, NULL, (PVOID)((ULONG_PTR)LinuxSetupSector + 512)))
320 return FALSE;
321
322 // DbgDumpBuffer(DPRINT_LINUX, LinuxSetupSector, SetupSectorSize);
323
324 TRACE("SetupHeaderSignature: 0x%x (HdrS)\n", LinuxSetupSector->SetupHeaderSignature);
325 TRACE("Version: 0x%x\n", LinuxSetupSector->Version);
326 TRACE("RealModeSwitch: 0x%x\n", LinuxSetupSector->RealModeSwitch);
327 TRACE("SetupSeg: 0x%x\n", LinuxSetupSector->SetupSeg);
328 TRACE("StartSystemSeg: 0x%x\n", LinuxSetupSector->StartSystemSeg);
329 TRACE("KernelVersion: 0x%x\n", LinuxSetupSector->KernelVersion);
330 TRACE("TypeOfLoader: 0x%x\n", LinuxSetupSector->TypeOfLoader);
331 TRACE("LoadFlags: 0x%x\n", LinuxSetupSector->LoadFlags);
332 TRACE("SetupMoveSize: 0x%x\n", LinuxSetupSector->SetupMoveSize);
333 TRACE("Code32Start: 0x%x\n", LinuxSetupSector->Code32Start);
334 TRACE("RamdiskAddress: 0x%x\n", LinuxSetupSector->RamdiskAddress);
335 TRACE("RamdiskSize: 0x%x\n", LinuxSetupSector->RamdiskSize);
336 TRACE("BootSectKludgeOffset: 0x%x\n", LinuxSetupSector->BootSectKludgeOffset);
337 TRACE("BootSectKludgeSegment: 0x%x\n", LinuxSetupSector->BootSectKludgeSegment);
338 TRACE("HeapEnd: 0x%x\n", LinuxSetupSector->HeapEnd);
339
340 return TRUE;
341 }
342
343 BOOLEAN LinuxReadKernel(PFILE LinuxKernelFile)
344 {
345 ULONG BytesLoaded;
346 CHAR StatusText[260];
347 PVOID LoadAddress;
348
349 RtlStringCbPrintfA(StatusText, sizeof(StatusText), "Loading %s", LinuxKernelName);
350 UiDrawStatusText(StatusText);
351
352 /* Allocate memory for Linux kernel */
353 LinuxKernelLoadAddress = MmAllocateMemoryAtAddress(LinuxKernelSize, (PVOID)LINUX_KERNEL_LOAD_ADDRESS, LoaderSystemCode);
354 if (LinuxKernelLoadAddress != (PVOID)LINUX_KERNEL_LOAD_ADDRESS)
355 {
356 return FALSE;
357 }
358
359 LoadAddress = LinuxKernelLoadAddress;
360
361 /* Read linux kernel to 0x100000 (1mb) */
362 FsSetFilePointer(LinuxKernelFile, 512 + SetupSectorSize);
363 for (BytesLoaded=0; BytesLoaded<LinuxKernelSize; )
364 {
365 if (!FsReadFile(LinuxKernelFile, LINUX_READ_CHUNK_SIZE, NULL, LoadAddress))
366 return FALSE;
367
368 BytesLoaded += LINUX_READ_CHUNK_SIZE;
369 LoadAddress = (PVOID)((ULONG_PTR)LoadAddress + LINUX_READ_CHUNK_SIZE);
370
371 UiDrawProgressBarCenter(BytesLoaded, LinuxKernelSize + LinuxInitrdSize, LinuxBootDescription);
372 }
373
374 return TRUE;
375 }
376
377 BOOLEAN LinuxCheckKernelVersion(VOID)
378 {
379 /* Just assume old kernel until we find otherwise */
380 NewStyleLinuxKernel = FALSE;
381
382 /* Check for new style setup header */
383 if (LinuxSetupSector->SetupHeaderSignature != LINUX_SETUP_HEADER_ID)
384 {
385 NewStyleLinuxKernel = FALSE;
386 }
387 /* Check for version below 2.0 */
388 else if (LinuxSetupSector->Version < 0x0200)
389 {
390 NewStyleLinuxKernel = FALSE;
391 }
392 /* Check for version 2.0 */
393 else if (LinuxSetupSector->Version == 0x0200)
394 {
395 NewStyleLinuxKernel = TRUE;
396 }
397 /* Check for version 2.01+ */
398 else if (LinuxSetupSector->Version >= 0x0201)
399 {
400 NewStyleLinuxKernel = TRUE;
401 LinuxSetupSector->HeapEnd = 0x9000;
402 LinuxSetupSector->LoadFlags |= LINUX_FLAG_CAN_USE_HEAP;
403 }
404
405 if ((NewStyleLinuxKernel == FALSE) && (LinuxInitrdName))
406 {
407 UiMessageBox("Error: Cannot load a ramdisk (initrd) with an old kernel image.");
408 return FALSE;
409 }
410
411 return TRUE;
412 }
413
414 BOOLEAN LinuxReadInitrd(PFILE LinuxInitrdFile)
415 {
416 ULONG BytesLoaded;
417 CHAR StatusText[260];
418
419 RtlStringCbPrintfA(StatusText, sizeof(StatusText), "Loading %s", LinuxInitrdName);
420 UiDrawStatusText(StatusText);
421
422 // Allocate memory for the ramdisk
423 //LinuxInitrdLoadAddress = MmAllocateMemory(LinuxInitrdSize);
424 // Try to align it at the next MB boundary after the kernel
425 //LinuxInitrdLoadAddress = MmAllocateMemoryAtAddress(LinuxInitrdSize, (PVOID)ROUND_UP((LINUX_KERNEL_LOAD_ADDRESS + LinuxKernelSize), 0x100000));
426 if (LinuxSetupSector->Version <= 0x0202)
427 {
428 LinuxInitrdLoadAddress = MmAllocateHighestMemoryBelowAddress(LinuxInitrdSize, (PVOID)LINUX_MAX_INITRD_ADDRESS, LoaderSystemCode);
429 }
430 else
431 {
432 LinuxInitrdLoadAddress = MmAllocateHighestMemoryBelowAddress(LinuxInitrdSize, (PVOID)LinuxSetupSector->InitrdAddressMax, LoaderSystemCode);
433 }
434 if (LinuxInitrdLoadAddress == NULL)
435 {
436 return FALSE;
437 }
438
439 /* Set the information in the setup struct */
440 LinuxSetupSector->RamdiskAddress = (ULONG)LinuxInitrdLoadAddress;
441 LinuxSetupSector->RamdiskSize = LinuxInitrdSize;
442
443 TRACE("RamdiskAddress: 0x%x\n", LinuxSetupSector->RamdiskAddress);
444 TRACE("RamdiskSize: 0x%x\n", LinuxSetupSector->RamdiskSize);
445
446 if (LinuxSetupSector->Version >= 0x0203)
447 {
448 TRACE("InitrdAddressMax: 0x%x\n", LinuxSetupSector->InitrdAddressMax);
449 }
450
451 /* Read in the ramdisk */
452 for (BytesLoaded=0; BytesLoaded<LinuxInitrdSize; )
453 {
454 if (!FsReadFile(LinuxInitrdFile, LINUX_READ_CHUNK_SIZE, NULL, (PVOID)LinuxInitrdLoadAddress))
455 return FALSE;
456
457 BytesLoaded += LINUX_READ_CHUNK_SIZE;
458 LinuxInitrdLoadAddress = (PVOID)((ULONG_PTR)LinuxInitrdLoadAddress + LINUX_READ_CHUNK_SIZE);
459
460 UiDrawProgressBarCenter(BytesLoaded + LinuxKernelSize, LinuxInitrdSize + LinuxKernelSize, LinuxBootDescription);
461 }
462
463 return TRUE;
464 }
465
466 #endif // _M_IX86
467
468 #endif