3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
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.
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.
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.
26 #define LINUX_READ_CHUNK_SIZE 0x20000 // Read 128k at a time
29 PLINUX_BOOTSECTOR LinuxBootSector
= NULL
;
30 PLINUX_SETUPSECTOR LinuxSetupSector
= NULL
;
31 ULONG SetupSectorSize
= 0;
32 BOOLEAN NewStyleLinuxKernel
= FALSE
;
33 ULONG LinuxKernelSize
= 0;
34 ULONG LinuxInitrdSize
= 0;
35 CHAR LinuxKernelName
[260];
36 CHAR LinuxInitrdName
[260];
37 BOOLEAN LinuxHasInitrd
= FALSE
;
38 CHAR LinuxCommandLine
[260] = "";
39 ULONG LinuxCommandLineSize
= 0;
40 PVOID LinuxKernelLoadAddress
= NULL
;
41 PVOID LinuxInitrdLoadAddress
= NULL
;
42 CHAR LinuxBootDescription
[80];
43 CHAR LinuxBootPath
[260] = "";
45 VOID
LoadAndBootLinux(PCSTR OperatingSystemName
, PCSTR Description
)
47 PFILE LinuxKernel
= NULL
;
48 PFILE LinuxInitrdFile
= NULL
;
55 sprintf(LinuxBootDescription
, "Loading %s...", Description
);
59 strcpy(LinuxBootDescription
, "Loading Linux...");
62 UiDrawStatusText(LinuxBootDescription
);
63 UiDrawProgressBarCenter(0, 100, LinuxBootDescription
);
65 // Parse the .ini file section
66 if (!LinuxParseIniSection(OperatingSystemName
))
71 // Open the boot volume
72 if (!MachDiskNormalizeSystemPath(LinuxBootPath
, sizeof(LinuxBootPath
)))
74 UiMessageBox("Invalid boot path");
77 if (!FsOpenSystemVolume(LinuxBootPath
, NULL
, NULL
))
79 UiMessageBox("Failed to open boot drive.");
84 LinuxKernel
= FsOpenFile(LinuxKernelName
);
85 if (LinuxKernel
== NULL
)
87 sprintf(TempString
, "Linux kernel \'%s\' not found.", LinuxKernelName
);
88 UiMessageBox(TempString
);
92 // Open the initrd file image (if necessary)
95 LinuxInitrdFile
= FsOpenFile(LinuxInitrdName
);
96 if (LinuxInitrdFile
== NULL
)
98 sprintf(TempString
, "Linux initrd image \'%s\' not found.", LinuxInitrdName
);
99 UiMessageBox(TempString
);
100 goto LinuxBootFailed
;
104 // Read the boot sector
105 if (!LinuxReadBootSector(LinuxKernel
))
107 goto LinuxBootFailed
;
110 // Read the setup sector
111 if (!LinuxReadSetupSector(LinuxKernel
))
113 goto LinuxBootFailed
;
117 LinuxKernelSize
= FsGetFileSize(LinuxKernel
) - (512 + SetupSectorSize
);
120 LinuxInitrdSize
= FsGetFileSize(LinuxInitrdFile
);
123 if (!LinuxReadKernel(LinuxKernel
))
125 goto LinuxBootFailed
;
128 // Read the initrd (if necessary)
131 if (!LinuxReadInitrd(LinuxInitrdFile
))
133 goto LinuxBootFailed
;
137 // If the default root device is set to FLOPPY (0000h), change to /dev/fd0 (0200h)
138 if (LinuxBootSector
->RootDevice
== 0x0000)
140 LinuxBootSector
->RootDevice
= 0x0200;
143 if (LinuxSetupSector
->Version
>= 0x0202)
145 LinuxSetupSector
->CommandLinePointer
= 0x99000;
149 LinuxBootSector
->CommandLineMagic
= LINUX_COMMAND_LINE_MAGIC
;
150 LinuxBootSector
->CommandLineOffset
= 0x9000;
153 if (NewStyleLinuxKernel
)
155 LinuxSetupSector
->TypeOfLoader
= LINUX_LOADER_TYPE_FREELOADER
;
159 LinuxSetupSector
->LoadFlags
= 0;
162 RtlCopyMemory((PVOID
)0x90000, LinuxBootSector
, 512);
163 RtlCopyMemory((PVOID
)0x90200, LinuxSetupSector
, SetupSectorSize
);
164 RtlCopyMemory((PVOID
)0x99000, LinuxCommandLine
, LinuxCommandLineSize
);
166 UiUnInitialize("Booting Linux...");
168 DiskStopFloppyMotor();
170 if (LinuxSetupSector
->LoadFlags
& LINUX_FLAG_LOAD_HIGH
)
172 BootNewLinuxKernel();
176 BootOldLinuxKernel(LinuxKernelSize
);
182 if (LinuxKernel
!= NULL
)
184 FsCloseFile(LinuxKernel
);
186 if (LinuxInitrdFile
!= NULL
)
188 FsCloseFile(LinuxInitrdFile
);
191 if (LinuxBootSector
!= NULL
)
193 MmFreeMemory(LinuxBootSector
);
195 if (LinuxSetupSector
!= NULL
)
197 MmFreeMemory(LinuxSetupSector
);
199 if (LinuxKernelLoadAddress
!= NULL
)
201 MmFreeMemory(LinuxKernelLoadAddress
);
203 if (LinuxInitrdLoadAddress
!= NULL
)
205 MmFreeMemory(LinuxInitrdLoadAddress
);
208 LinuxBootSector
= NULL
;
209 LinuxSetupSector
= NULL
;
210 LinuxKernelLoadAddress
= NULL
;
211 LinuxInitrdLoadAddress
= NULL
;
213 NewStyleLinuxKernel
= FALSE
;
215 LinuxHasInitrd
= FALSE
;
216 strcpy(LinuxCommandLine
, "");
217 LinuxCommandLineSize
= 0;
220 BOOLEAN
LinuxParseIniSection(PCSTR OperatingSystemName
)
222 CHAR SettingName
[260];
225 // Find all the message box settings and run them
226 UiShowMessageBoxesInSection(OperatingSystemName
);
228 // Try to open the operating system section in the .ini file
229 if (!IniOpenSection(OperatingSystemName
, &SectionId
))
231 sprintf(SettingName
, "Section [%s] not found in freeldr.ini.\n", OperatingSystemName
);
232 UiMessageBox(SettingName
);
236 if (!IniReadSettingByName(SectionId
, "BootPath", LinuxBootPath
, 260))
238 UiMessageBox("Boot path not specified for selected OS!");
242 // Get the kernel name
243 if (!IniReadSettingByName(SectionId
, "Kernel", LinuxKernelName
, 260))
245 UiMessageBox("Linux kernel filename not specified for selected OS!");
249 // Get the initrd name
250 if (IniReadSettingByName(SectionId
, "Initrd", LinuxInitrdName
, 260))
252 LinuxHasInitrd
= TRUE
;
255 // Get the command line
256 if (IniReadSettingByName(SectionId
, "CommandLine", LinuxCommandLine
, 260))
258 RemoveQuotes(LinuxCommandLine
);
259 LinuxCommandLineSize
= strlen(LinuxCommandLine
) + 1;
265 BOOLEAN
LinuxReadBootSector(PFILE LinuxKernelFile
)
267 // Allocate memory for boot sector
268 LinuxBootSector
= (PLINUX_BOOTSECTOR
)MmAllocateMemory(512);
269 if (LinuxBootSector
== NULL
)
274 // Read linux boot sector
275 FsSetFilePointer(LinuxKernelFile
, 0);
276 if (!FsReadFile(LinuxKernelFile
, 512, NULL
, LinuxBootSector
))
281 // Check for validity
282 if (LinuxBootSector
->BootFlag
!= LINUX_BOOT_SECTOR_MAGIC
)
284 UiMessageBox("Invalid boot sector magic (0xaa55)");
288 DbgDumpBuffer(DPRINT_LINUX
, LinuxBootSector
, 512);
290 DbgPrint((DPRINT_LINUX
, "SetupSectors: %d\n", LinuxBootSector
->SetupSectors
));
291 DbgPrint((DPRINT_LINUX
, "RootFlags: 0x%x\n", LinuxBootSector
->RootFlags
));
292 DbgPrint((DPRINT_LINUX
, "SystemSize: 0x%x\n", LinuxBootSector
->SystemSize
));
293 DbgPrint((DPRINT_LINUX
, "SwapDevice: 0x%x\n", LinuxBootSector
->SwapDevice
));
294 DbgPrint((DPRINT_LINUX
, "RamSize: 0x%x\n", LinuxBootSector
->RamSize
));
295 DbgPrint((DPRINT_LINUX
, "VideoMode: 0x%x\n", LinuxBootSector
->VideoMode
));
296 DbgPrint((DPRINT_LINUX
, "RootDevice: 0x%x\n", LinuxBootSector
->RootDevice
));
297 DbgPrint((DPRINT_LINUX
, "BootFlag: 0x%x\n", LinuxBootSector
->BootFlag
));
302 BOOLEAN
LinuxReadSetupSector(PFILE LinuxKernelFile
)
304 UCHAR TempLinuxSetupSector
[512];
306 LinuxSetupSector
= (PLINUX_SETUPSECTOR
)TempLinuxSetupSector
;
308 // Read first linux setup sector
309 FsSetFilePointer(LinuxKernelFile
, 512);
310 if (!FsReadFile(LinuxKernelFile
, 512, NULL
, TempLinuxSetupSector
))
315 // Check the kernel version
316 if (!LinuxCheckKernelVersion())
321 if (NewStyleLinuxKernel
)
323 SetupSectorSize
= 512 * LinuxBootSector
->SetupSectors
;
327 SetupSectorSize
= 4 * 512; // Always 4 setup sectors
330 // Allocate memory for setup sectors
331 LinuxSetupSector
= (PLINUX_SETUPSECTOR
)MmAllocateMemory(SetupSectorSize
);
332 if (LinuxSetupSector
== NULL
)
337 // Copy over first setup sector
338 RtlCopyMemory(LinuxSetupSector
, TempLinuxSetupSector
, 512);
340 // Read in the rest of the linux setup sectors
341 FsSetFilePointer(LinuxKernelFile
, 1024);
342 if (!FsReadFile(LinuxKernelFile
, SetupSectorSize
- 512, NULL
, (PVOID
)((ULONG_PTR
)LinuxSetupSector
+ 512)))
347 DbgDumpBuffer(DPRINT_LINUX
, LinuxSetupSector
, SetupSectorSize
);
349 DbgPrint((DPRINT_LINUX
, "SetupHeaderSignature: 0x%x (HdrS)\n", LinuxSetupSector
->SetupHeaderSignature
));
350 DbgPrint((DPRINT_LINUX
, "Version: 0x%x\n", LinuxSetupSector
->Version
));
351 DbgPrint((DPRINT_LINUX
, "RealModeSwitch: 0x%x\n", LinuxSetupSector
->RealModeSwitch
));
352 DbgPrint((DPRINT_LINUX
, "SetupSeg: 0x%x\n", LinuxSetupSector
->SetupSeg
));
353 DbgPrint((DPRINT_LINUX
, "StartSystemSeg: 0x%x\n", LinuxSetupSector
->StartSystemSeg
));
354 DbgPrint((DPRINT_LINUX
, "KernelVersion: 0x%x\n", LinuxSetupSector
->KernelVersion
));
355 DbgPrint((DPRINT_LINUX
, "TypeOfLoader: 0x%x\n", LinuxSetupSector
->TypeOfLoader
));
356 DbgPrint((DPRINT_LINUX
, "LoadFlags: 0x%x\n", LinuxSetupSector
->LoadFlags
));
357 DbgPrint((DPRINT_LINUX
, "SetupMoveSize: 0x%x\n", LinuxSetupSector
->SetupMoveSize
));
358 DbgPrint((DPRINT_LINUX
, "Code32Start: 0x%x\n", LinuxSetupSector
->Code32Start
));
359 DbgPrint((DPRINT_LINUX
, "RamdiskAddress: 0x%x\n", LinuxSetupSector
->RamdiskAddress
));
360 DbgPrint((DPRINT_LINUX
, "RamdiskSize: 0x%x\n", LinuxSetupSector
->RamdiskSize
));
361 DbgPrint((DPRINT_LINUX
, "BootSectKludgeOffset: 0x%x\n", LinuxSetupSector
->BootSectKludgeOffset
));
362 DbgPrint((DPRINT_LINUX
, "BootSectKludgeSegment: 0x%x\n", LinuxSetupSector
->BootSectKludgeSegment
));
363 DbgPrint((DPRINT_LINUX
, "HeapEnd: 0x%x\n", LinuxSetupSector
->HeapEnd
));
368 BOOLEAN
LinuxReadKernel(PFILE LinuxKernelFile
)
371 CHAR StatusText
[260];
374 sprintf(StatusText
, "Loading %s", LinuxKernelName
);
375 UiDrawStatusText(StatusText
);
377 // Allocate memory for Linux kernel
378 LinuxKernelLoadAddress
= MmAllocateMemoryAtAddress(LinuxKernelSize
, (PVOID
)LINUX_KERNEL_LOAD_ADDRESS
);
379 if (LinuxKernelLoadAddress
!= (PVOID
)LINUX_KERNEL_LOAD_ADDRESS
)
384 LoadAddress
= LinuxKernelLoadAddress
;
386 // Read linux kernel to 0x100000 (1mb)
387 FsSetFilePointer(LinuxKernelFile
, 512 + SetupSectorSize
);
388 for (BytesLoaded
=0; BytesLoaded
<LinuxKernelSize
; )
390 if (!FsReadFile(LinuxKernelFile
, LINUX_READ_CHUNK_SIZE
, NULL
, LoadAddress
))
395 BytesLoaded
+= LINUX_READ_CHUNK_SIZE
;
396 LoadAddress
= (PVOID
)((ULONG_PTR
)LoadAddress
+ LINUX_READ_CHUNK_SIZE
);
398 UiDrawProgressBarCenter(BytesLoaded
, LinuxKernelSize
+ LinuxInitrdSize
, LinuxBootDescription
);
404 BOOLEAN
LinuxCheckKernelVersion(VOID
)
406 // Just assume old kernel until we find otherwise
407 NewStyleLinuxKernel
= FALSE
;
409 // Check for new style setup header
410 if (LinuxSetupSector
->SetupHeaderSignature
!= LINUX_SETUP_HEADER_ID
)
412 NewStyleLinuxKernel
= FALSE
;
414 // Check for version below 2.0
415 else if (LinuxSetupSector
->Version
< 0x0200)
417 NewStyleLinuxKernel
= FALSE
;
419 // Check for version 2.0
420 else if (LinuxSetupSector
->Version
== 0x0200)
422 NewStyleLinuxKernel
= TRUE
;
424 // Check for version 2.01+
425 else if (LinuxSetupSector
->Version
>= 0x0201)
427 NewStyleLinuxKernel
= TRUE
;
428 LinuxSetupSector
->HeapEnd
= 0x9000;
429 LinuxSetupSector
->LoadFlags
|= LINUX_FLAG_CAN_USE_HEAP
;
432 if ((NewStyleLinuxKernel
== FALSE
) && (LinuxHasInitrd
== TRUE
))
434 UiMessageBox("Error: Cannot load a ramdisk (initrd) with an old kernel image.");
441 BOOLEAN
LinuxReadInitrd(PFILE LinuxInitrdFile
)
444 CHAR StatusText
[260];
446 sprintf(StatusText
, "Loading %s", LinuxInitrdName
);
447 UiDrawStatusText(StatusText
);
449 // Allocate memory for the ramdisk
450 //LinuxInitrdLoadAddress = MmAllocateMemory(LinuxInitrdSize);
451 // Try to align it at the next MB boundary after the kernel
452 //LinuxInitrdLoadAddress = MmAllocateMemoryAtAddress(LinuxInitrdSize, (PVOID)ROUND_UP((LINUX_KERNEL_LOAD_ADDRESS + LinuxKernelSize), 0x100000));
453 if (LinuxSetupSector
->Version
<= 0x0202)
455 LinuxInitrdLoadAddress
= MmAllocateHighestMemoryBelowAddress(LinuxInitrdSize
, (PVOID
)LINUX_MAX_INITRD_ADDRESS
);
459 LinuxInitrdLoadAddress
= MmAllocateHighestMemoryBelowAddress(LinuxInitrdSize
, (PVOID
)LinuxSetupSector
->InitrdAddressMax
);
461 if (LinuxInitrdLoadAddress
== NULL
)
466 // Set the information in the setup struct
467 LinuxSetupSector
->RamdiskAddress
= (ULONG
)LinuxInitrdLoadAddress
;
468 LinuxSetupSector
->RamdiskSize
= LinuxInitrdSize
;
470 DbgPrint((DPRINT_LINUX
, "RamdiskAddress: 0x%x\n", LinuxSetupSector
->RamdiskAddress
));
471 DbgPrint((DPRINT_LINUX
, "RamdiskSize: 0x%x\n", LinuxSetupSector
->RamdiskSize
));
473 if (LinuxSetupSector
->Version
>= 0x0203)
475 DbgPrint((DPRINT_LINUX
, "InitrdAddressMax: 0x%x\n", LinuxSetupSector
->InitrdAddressMax
));
478 // Read in the ramdisk
479 for (BytesLoaded
=0; BytesLoaded
<LinuxInitrdSize
; )
481 if (!FsReadFile(LinuxInitrdFile
, LINUX_READ_CHUNK_SIZE
, NULL
, (PVOID
)LinuxInitrdLoadAddress
))
486 BytesLoaded
+= LINUX_READ_CHUNK_SIZE
;
487 LinuxInitrdLoadAddress
= (PVOID
)((ULONG_PTR
)LinuxInitrdLoadAddress
+ LINUX_READ_CHUNK_SIZE
);
489 UiDrawProgressBarCenter(BytesLoaded
+ LinuxKernelSize
, LinuxInitrdSize
+ LinuxKernelSize
, LinuxBootDescription
);