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.
32 #include <oslist.h> // For RemoveQuotes()
38 #define LINUX_READ_CHUNK_SIZE 0x20000 // Read 128k at a time
41 PLINUX_BOOTSECTOR LinuxBootSector
= NULL
;
42 PLINUX_SETUPSECTOR LinuxSetupSector
= NULL
;
43 ULONG SetupSectorSize
= 0;
44 BOOL NewStyleLinuxKernel
= FALSE
;
45 ULONG LinuxKernelSize
= 0;
46 ULONG LinuxInitrdSize
= 0;
47 CHAR LinuxKernelName
[260];
48 CHAR LinuxInitrdName
[260];
49 BOOL LinuxHasInitrd
= FALSE
;
50 CHAR LinuxCommandLine
[260] = "";
51 ULONG LinuxCommandLineSize
= 0;
52 PVOID LinuxKernelLoadAddress
= NULL
;
53 PVOID LinuxInitrdLoadAddress
= NULL
;
54 CHAR LinuxBootDescription
[80];
55 CHAR LinuxBootPath
[260] = "";
57 VOID
LoadAndBootLinux(PCHAR OperatingSystemName
, PCHAR Description
)
59 PFILE LinuxKernel
= NULL
;
60 PFILE LinuxInitrdFile
= NULL
;
67 sprintf(LinuxBootDescription
, "Loading %s...", Description
);
71 strcpy(LinuxBootDescription
, "Loading Linux...");
74 UiDrawStatusText(LinuxBootDescription
);
75 UiDrawProgressBarCenter(0, 100, LinuxBootDescription
);
77 // Parse the .ini file section
78 if (!LinuxParseIniSection(OperatingSystemName
))
83 // Open the boot volume
84 if (!FsOpenSystemVolume(LinuxBootPath
, NULL
, NULL
))
86 UiMessageBox("Failed to open boot drive.");
91 LinuxKernel
= FsOpenFile(LinuxKernelName
);
92 if (LinuxKernel
== NULL
)
94 sprintf(TempString
, "Linux kernel \'%s\' not found.", LinuxKernelName
);
95 UiMessageBox(TempString
);
99 // Open the initrd file image (if necessary)
102 LinuxInitrdFile
= FsOpenFile(LinuxInitrdName
);
103 if (LinuxInitrdFile
== NULL
)
105 sprintf(TempString
, "Linux initrd image \'%s\' not found.", LinuxInitrdName
);
106 UiMessageBox(TempString
);
107 goto LinuxBootFailed
;
111 // Read the boot sector
112 if (!LinuxReadBootSector(LinuxKernel
))
114 goto LinuxBootFailed
;
117 // Read the setup sector
118 if (!LinuxReadSetupSector(LinuxKernel
))
120 goto LinuxBootFailed
;
124 LinuxKernelSize
= FsGetFileSize(LinuxKernel
) - (512 + SetupSectorSize
);
127 LinuxInitrdSize
= FsGetFileSize(LinuxInitrdFile
);
130 if (!LinuxReadKernel(LinuxKernel
))
132 goto LinuxBootFailed
;
135 // Read the initrd (if necessary)
138 if (!LinuxReadInitrd(LinuxInitrdFile
))
140 goto LinuxBootFailed
;
144 // If the default root device is set to FLOPPY (0000h), change to /dev/fd0 (0200h)
145 if (LinuxBootSector
->RootDevice
== 0x0000)
147 LinuxBootSector
->RootDevice
= 0x0200;
150 if (LinuxSetupSector
->Version
>= 0x0202)
152 LinuxSetupSector
->CommandLinePointer
= 0x99000;
156 LinuxBootSector
->CommandLineMagic
= LINUX_COMMAND_LINE_MAGIC
;
157 LinuxBootSector
->CommandLineOffset
= 0x9000;
160 if (NewStyleLinuxKernel
)
162 LinuxSetupSector
->TypeOfLoader
= LINUX_LOADER_TYPE_FREELOADER
;
166 LinuxSetupSector
->LoadFlags
= 0;
169 RtlCopyMemory((PVOID
)0x90000, LinuxBootSector
, 512);
170 RtlCopyMemory((PVOID
)0x90200, LinuxSetupSector
, SetupSectorSize
);
171 RtlCopyMemory((PVOID
)0x99000, LinuxCommandLine
, LinuxCommandLineSize
);
173 UiUnInitialize("Booting Linux...");
175 DiskStopFloppyMotor();
177 if (LinuxSetupSector
->LoadFlags
& LINUX_FLAG_LOAD_HIGH
)
179 BootNewLinuxKernel();
183 BootOldLinuxKernel(LinuxKernelSize
);
189 if (LinuxKernel
!= NULL
)
191 FsCloseFile(LinuxKernel
);
193 if (LinuxInitrdFile
!= NULL
)
195 FsCloseFile(LinuxInitrdFile
);
198 if (LinuxBootSector
!= NULL
)
200 MmFreeMemory(LinuxBootSector
);
202 if (LinuxSetupSector
!= NULL
)
204 MmFreeMemory(LinuxSetupSector
);
206 if (LinuxKernelLoadAddress
!= NULL
)
208 MmFreeMemory(LinuxKernelLoadAddress
);
210 if (LinuxInitrdLoadAddress
!= NULL
)
212 MmFreeMemory(LinuxInitrdLoadAddress
);
215 LinuxBootSector
= NULL
;
216 LinuxSetupSector
= NULL
;
217 LinuxKernelLoadAddress
= NULL
;
218 LinuxInitrdLoadAddress
= NULL
;
220 NewStyleLinuxKernel
= FALSE
;
222 LinuxHasInitrd
= FALSE
;
223 strcpy(LinuxCommandLine
, "");
224 LinuxCommandLineSize
= 0;
227 BOOL
LinuxParseIniSection(PCHAR OperatingSystemName
)
229 CHAR SettingName
[260];
232 // Find all the message box settings and run them
233 UiShowMessageBoxesInSection(OperatingSystemName
);
235 // Try to open the operating system section in the .ini file
236 if (!IniOpenSection(OperatingSystemName
, &SectionId
))
238 sprintf(SettingName
, "Section [%s] not found in freeldr.ini.\n", OperatingSystemName
);
239 UiMessageBox(SettingName
);
243 if (!IniReadSettingByName(SectionId
, "BootPath", LinuxBootPath
, 260))
245 UiMessageBox("Boot path not specified for selected OS!");
249 // Get the kernel name
250 if (!IniReadSettingByName(SectionId
, "Kernel", LinuxKernelName
, 260))
252 UiMessageBox("Linux kernel filename not specified for selected OS!");
256 // Get the initrd name
257 if (IniReadSettingByName(SectionId
, "Initrd", LinuxInitrdName
, 260))
259 LinuxHasInitrd
= TRUE
;
262 // Get the command line
263 if (IniReadSettingByName(SectionId
, "CommandLine", LinuxCommandLine
, 260))
265 RemoveQuotes(LinuxCommandLine
);
266 LinuxCommandLineSize
= strlen(LinuxCommandLine
) + 1;
272 BOOL
LinuxReadBootSector(PFILE LinuxKernelFile
)
274 // Allocate memory for boot sector
275 LinuxBootSector
= (PLINUX_BOOTSECTOR
)MmAllocateMemory(512);
276 if (LinuxBootSector
== NULL
)
281 // Read linux boot sector
282 FsSetFilePointer(LinuxKernelFile
, 0);
283 if (!FsReadFile(LinuxKernelFile
, 512, NULL
, LinuxBootSector
))
288 // Check for validity
289 if (LinuxBootSector
->BootFlag
!= LINUX_BOOT_SECTOR_MAGIC
)
291 UiMessageBox("Invalid boot sector magic (0xaa55)");
295 DbgDumpBuffer(DPRINT_LINUX
, LinuxBootSector
, 512);
297 DbgPrint((DPRINT_LINUX
, "SetupSectors: %d\n", LinuxBootSector
->SetupSectors
));
298 DbgPrint((DPRINT_LINUX
, "RootFlags: 0x%x\n", LinuxBootSector
->RootFlags
));
299 DbgPrint((DPRINT_LINUX
, "SystemSize: 0x%x\n", LinuxBootSector
->SystemSize
));
300 DbgPrint((DPRINT_LINUX
, "SwapDevice: 0x%x\n", LinuxBootSector
->SwapDevice
));
301 DbgPrint((DPRINT_LINUX
, "RamSize: 0x%x\n", LinuxBootSector
->RamSize
));
302 DbgPrint((DPRINT_LINUX
, "VideoMode: 0x%x\n", LinuxBootSector
->VideoMode
));
303 DbgPrint((DPRINT_LINUX
, "RootDevice: 0x%x\n", LinuxBootSector
->RootDevice
));
304 DbgPrint((DPRINT_LINUX
, "BootFlag: 0x%x\n", LinuxBootSector
->BootFlag
));
309 BOOL
LinuxReadSetupSector(PFILE LinuxKernelFile
)
311 UCHAR TempLinuxSetupSector
[512];
313 LinuxSetupSector
= (PLINUX_SETUPSECTOR
)TempLinuxSetupSector
;
315 // Read first linux setup sector
316 FsSetFilePointer(LinuxKernelFile
, 512);
317 if (!FsReadFile(LinuxKernelFile
, 512, NULL
, TempLinuxSetupSector
))
322 // Check the kernel version
323 if (!LinuxCheckKernelVersion())
328 if (NewStyleLinuxKernel
)
330 SetupSectorSize
= 512 * LinuxBootSector
->SetupSectors
;
334 SetupSectorSize
= 4 * 512; // Always 4 setup sectors
337 // Allocate memory for setup sectors
338 LinuxSetupSector
= (PLINUX_SETUPSECTOR
)MmAllocateMemory(SetupSectorSize
);
339 if (LinuxSetupSector
== NULL
)
344 // Copy over first setup sector
345 RtlCopyMemory(LinuxSetupSector
, TempLinuxSetupSector
, 512);
347 // Read in the rest of the linux setup sectors
348 FsSetFilePointer(LinuxKernelFile
, 1024);
349 if (!FsReadFile(LinuxKernelFile
, SetupSectorSize
- 512, NULL
, ((PVOID
)LinuxSetupSector
) + 512))
354 DbgDumpBuffer(DPRINT_LINUX
, LinuxSetupSector
, SetupSectorSize
);
356 DbgPrint((DPRINT_LINUX
, "SetupHeaderSignature: 0x%x (HdrS)\n", LinuxSetupSector
->SetupHeaderSignature
));
357 DbgPrint((DPRINT_LINUX
, "Version: 0x%x\n", LinuxSetupSector
->Version
));
358 DbgPrint((DPRINT_LINUX
, "RealModeSwitch: 0x%x\n", LinuxSetupSector
->RealModeSwitch
));
359 DbgPrint((DPRINT_LINUX
, "SetupSeg: 0x%x\n", LinuxSetupSector
->SetupSeg
));
360 DbgPrint((DPRINT_LINUX
, "StartSystemSeg: 0x%x\n", LinuxSetupSector
->StartSystemSeg
));
361 DbgPrint((DPRINT_LINUX
, "KernelVersion: 0x%x\n", LinuxSetupSector
->KernelVersion
));
362 DbgPrint((DPRINT_LINUX
, "TypeOfLoader: 0x%x\n", LinuxSetupSector
->TypeOfLoader
));
363 DbgPrint((DPRINT_LINUX
, "LoadFlags: 0x%x\n", LinuxSetupSector
->LoadFlags
));
364 DbgPrint((DPRINT_LINUX
, "SetupMoveSize: 0x%x\n", LinuxSetupSector
->SetupMoveSize
));
365 DbgPrint((DPRINT_LINUX
, "Code32Start: 0x%x\n", LinuxSetupSector
->Code32Start
));
366 DbgPrint((DPRINT_LINUX
, "RamdiskAddress: 0x%x\n", LinuxSetupSector
->RamdiskAddress
));
367 DbgPrint((DPRINT_LINUX
, "RamdiskSize: 0x%x\n", LinuxSetupSector
->RamdiskSize
));
368 DbgPrint((DPRINT_LINUX
, "BootSectKludgeOffset: 0x%x\n", LinuxSetupSector
->BootSectKludgeOffset
));
369 DbgPrint((DPRINT_LINUX
, "BootSectKludgeSegment: 0x%x\n", LinuxSetupSector
->BootSectKludgeSegment
));
370 DbgPrint((DPRINT_LINUX
, "HeapEnd: 0x%x\n", LinuxSetupSector
->HeapEnd
));
375 BOOL
LinuxReadKernel(PFILE LinuxKernelFile
)
378 CHAR StatusText
[260];
381 sprintf(StatusText
, "Loading %s", LinuxKernelName
);
382 UiDrawStatusText(StatusText
);
384 // Allocate memory for Linux kernel
385 LinuxKernelLoadAddress
= MmAllocateMemoryAtAddress(LinuxKernelSize
, (PVOID
)LINUX_KERNEL_LOAD_ADDRESS
);
386 if (LinuxKernelLoadAddress
!= (PVOID
)LINUX_KERNEL_LOAD_ADDRESS
)
391 LoadAddress
= LinuxKernelLoadAddress
;
393 // Read linux kernel to 0x100000 (1mb)
394 FsSetFilePointer(LinuxKernelFile
, 512 + SetupSectorSize
);
395 for (BytesLoaded
=0; BytesLoaded
<LinuxKernelSize
; )
397 if (!FsReadFile(LinuxKernelFile
, LINUX_READ_CHUNK_SIZE
, NULL
, LoadAddress
))
402 BytesLoaded
+= LINUX_READ_CHUNK_SIZE
;
403 LoadAddress
+= LINUX_READ_CHUNK_SIZE
;
405 UiDrawProgressBarCenter(BytesLoaded
, LinuxKernelSize
+ LinuxInitrdSize
, LinuxBootDescription
);
411 BOOL
LinuxCheckKernelVersion(VOID
)
413 // Just assume old kernel until we find otherwise
414 NewStyleLinuxKernel
= FALSE
;
416 // Check for new style setup header
417 if (LinuxSetupSector
->SetupHeaderSignature
!= LINUX_SETUP_HEADER_ID
)
419 NewStyleLinuxKernel
= FALSE
;
421 // Check for version below 2.0
422 else if (LinuxSetupSector
->Version
< 0x0200)
424 NewStyleLinuxKernel
= FALSE
;
426 // Check for version 2.0
427 else if (LinuxSetupSector
->Version
== 0x0200)
429 NewStyleLinuxKernel
= TRUE
;
431 // Check for version 2.01+
432 else if (LinuxSetupSector
->Version
>= 0x0201)
434 NewStyleLinuxKernel
= TRUE
;
435 LinuxSetupSector
->HeapEnd
= 0x9000;
436 LinuxSetupSector
->LoadFlags
|= LINUX_FLAG_CAN_USE_HEAP
;
439 if ((NewStyleLinuxKernel
== FALSE
) && (LinuxHasInitrd
== TRUE
))
441 UiMessageBox("Error: Cannot load a ramdisk (initrd) with an old kernel image.");
448 BOOL
LinuxReadInitrd(PFILE LinuxInitrdFile
)
451 CHAR StatusText
[260];
453 sprintf(StatusText
, "Loading %s", LinuxInitrdName
);
454 UiDrawStatusText(StatusText
);
456 // Allocate memory for the ramdisk
457 //LinuxInitrdLoadAddress = MmAllocateMemory(LinuxInitrdSize);
458 // Try to align it at the next MB boundary after the kernel
459 //LinuxInitrdLoadAddress = MmAllocateMemoryAtAddress(LinuxInitrdSize, (PVOID)ROUND_UP((LINUX_KERNEL_LOAD_ADDRESS + LinuxKernelSize), 0x100000));
460 if (LinuxSetupSector
->Version
<= 0x0202)
462 LinuxInitrdLoadAddress
= MmAllocateHighestMemoryBelowAddress(LinuxInitrdSize
, (PVOID
)LINUX_MAX_INITRD_ADDRESS
);
466 LinuxInitrdLoadAddress
= MmAllocateHighestMemoryBelowAddress(LinuxInitrdSize
, (PVOID
)LinuxSetupSector
->InitrdAddressMax
);
468 if (LinuxInitrdLoadAddress
== NULL
)
473 // Set the information in the setup struct
474 LinuxSetupSector
->RamdiskAddress
= (ULONG
)LinuxInitrdLoadAddress
;
475 LinuxSetupSector
->RamdiskSize
= LinuxInitrdSize
;
477 DbgPrint((DPRINT_LINUX
, "RamdiskAddress: 0x%x\n", LinuxSetupSector
->RamdiskAddress
));
478 DbgPrint((DPRINT_LINUX
, "RamdiskSize: 0x%x\n", LinuxSetupSector
->RamdiskSize
));
480 if (LinuxSetupSector
->Version
>= 0x0203)
482 DbgPrint((DPRINT_LINUX
, "InitrdAddressMax: 0x%x\n", LinuxSetupSector
->InitrdAddressMax
));
485 // Read in the ramdisk
486 for (BytesLoaded
=0; BytesLoaded
<LinuxInitrdSize
; )
488 if (!FsReadFile(LinuxInitrdFile
, LINUX_READ_CHUNK_SIZE
, NULL
, (PVOID
)LinuxInitrdLoadAddress
))
493 BytesLoaded
+= LINUX_READ_CHUNK_SIZE
;
494 LinuxInitrdLoadAddress
+= LINUX_READ_CHUNK_SIZE
;
496 UiDrawProgressBarCenter(BytesLoaded
+ LinuxKernelSize
, LinuxInitrdSize
+ LinuxKernelSize
, LinuxBootDescription
);