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.
25 #define LINUX_READ_CHUNK_SIZE 0x20000 // Read 128k at a time
28 PLINUX_BOOTSECTOR LinuxBootSector
= NULL
;
29 PLINUX_SETUPSECTOR LinuxSetupSector
= NULL
;
30 ULONG SetupSectorSize
= 0;
31 BOOLEAN NewStyleLinuxKernel
= FALSE
;
32 ULONG LinuxKernelSize
= 0;
33 ULONG LinuxInitrdSize
= 0;
34 CHAR LinuxKernelName
[260];
35 CHAR LinuxInitrdName
[260];
36 BOOLEAN LinuxHasInitrd
= FALSE
;
37 CHAR LinuxCommandLine
[260] = "";
38 ULONG LinuxCommandLineSize
= 0;
39 PVOID LinuxKernelLoadAddress
= NULL
;
40 PVOID LinuxInitrdLoadAddress
= NULL
;
41 CHAR LinuxBootDescription
[80];
42 CHAR LinuxBootPath
[260] = "";
44 VOID
LoadAndBootLinux(PCSTR OperatingSystemName
, PCSTR Description
)
46 PFILE LinuxKernel
= NULL
;
47 PFILE LinuxInitrdFile
= NULL
;
54 sprintf(LinuxBootDescription
, "Loading %s...", Description
);
58 strcpy(LinuxBootDescription
, "Loading Linux...");
61 UiDrawStatusText(LinuxBootDescription
);
62 UiDrawProgressBarCenter(0, 100, LinuxBootDescription
);
64 // Parse the .ini file section
65 if (!LinuxParseIniSection(OperatingSystemName
))
70 // Open the boot volume
71 if (!MachDiskNormalizeSystemPath(LinuxBootPath
, sizeof(LinuxBootPath
)))
73 UiMessageBox("Invalid boot path");
76 if (!FsOpenSystemVolume(LinuxBootPath
, NULL
, NULL
))
78 UiMessageBox("Failed to open boot drive.");
83 LinuxKernel
= FsOpenFile(LinuxKernelName
);
84 if (LinuxKernel
== NULL
)
86 sprintf(TempString
, "Linux kernel \'%s\' not found.", LinuxKernelName
);
87 UiMessageBox(TempString
);
91 // Open the initrd file image (if necessary)
94 LinuxInitrdFile
= FsOpenFile(LinuxInitrdName
);
95 if (LinuxInitrdFile
== NULL
)
97 sprintf(TempString
, "Linux initrd image \'%s\' not found.", LinuxInitrdName
);
98 UiMessageBox(TempString
);
103 // Read the boot sector
104 if (!LinuxReadBootSector(LinuxKernel
))
106 goto LinuxBootFailed
;
109 // Read the setup sector
110 if (!LinuxReadSetupSector(LinuxKernel
))
112 goto LinuxBootFailed
;
116 LinuxKernelSize
= FsGetFileSize(LinuxKernel
) - (512 + SetupSectorSize
);
119 LinuxInitrdSize
= FsGetFileSize(LinuxInitrdFile
);
122 if (!LinuxReadKernel(LinuxKernel
))
124 goto LinuxBootFailed
;
127 // Read the initrd (if necessary)
130 if (!LinuxReadInitrd(LinuxInitrdFile
))
132 goto LinuxBootFailed
;
136 // If the default root device is set to FLOPPY (0000h), change to /dev/fd0 (0200h)
137 if (LinuxBootSector
->RootDevice
== 0x0000)
139 LinuxBootSector
->RootDevice
= 0x0200;
142 if (LinuxSetupSector
->Version
>= 0x0202)
144 LinuxSetupSector
->CommandLinePointer
= 0x99000;
148 LinuxBootSector
->CommandLineMagic
= LINUX_COMMAND_LINE_MAGIC
;
149 LinuxBootSector
->CommandLineOffset
= 0x9000;
152 if (NewStyleLinuxKernel
)
154 LinuxSetupSector
->TypeOfLoader
= LINUX_LOADER_TYPE_FREELOADER
;
158 LinuxSetupSector
->LoadFlags
= 0;
161 RtlCopyMemory((PVOID
)0x90000, LinuxBootSector
, 512);
162 RtlCopyMemory((PVOID
)0x90200, LinuxSetupSector
, SetupSectorSize
);
163 RtlCopyMemory((PVOID
)0x99000, LinuxCommandLine
, LinuxCommandLineSize
);
165 UiUnInitialize("Booting Linux...");
167 DiskStopFloppyMotor();
169 if (LinuxSetupSector
->LoadFlags
& LINUX_FLAG_LOAD_HIGH
)
171 BootNewLinuxKernel();
175 BootOldLinuxKernel(LinuxKernelSize
);
181 if (LinuxKernel
!= NULL
)
183 FsCloseFile(LinuxKernel
);
185 if (LinuxInitrdFile
!= NULL
)
187 FsCloseFile(LinuxInitrdFile
);
190 if (LinuxBootSector
!= NULL
)
192 MmFreeMemory(LinuxBootSector
);
194 if (LinuxSetupSector
!= NULL
)
196 MmFreeMemory(LinuxSetupSector
);
198 if (LinuxKernelLoadAddress
!= NULL
)
200 MmFreeMemory(LinuxKernelLoadAddress
);
202 if (LinuxInitrdLoadAddress
!= NULL
)
204 MmFreeMemory(LinuxInitrdLoadAddress
);
207 LinuxBootSector
= NULL
;
208 LinuxSetupSector
= NULL
;
209 LinuxKernelLoadAddress
= NULL
;
210 LinuxInitrdLoadAddress
= NULL
;
212 NewStyleLinuxKernel
= FALSE
;
214 LinuxHasInitrd
= FALSE
;
215 strcpy(LinuxCommandLine
, "");
216 LinuxCommandLineSize
= 0;
219 BOOLEAN
LinuxParseIniSection(PCSTR OperatingSystemName
)
221 CHAR SettingName
[260];
224 // Find all the message box settings and run them
225 UiShowMessageBoxesInSection(OperatingSystemName
);
227 // Try to open the operating system section in the .ini file
228 if (!IniOpenSection(OperatingSystemName
, &SectionId
))
230 sprintf(SettingName
, "Section [%s] not found in freeldr.ini.\n", OperatingSystemName
);
231 UiMessageBox(SettingName
);
235 if (!IniReadSettingByName(SectionId
, "BootPath", LinuxBootPath
, sizeof(LinuxBootPath
)))
237 UiMessageBox("Boot path not specified for selected OS!");
241 // Get the kernel name
242 if (!IniReadSettingByName(SectionId
, "Kernel", LinuxKernelName
, sizeof(LinuxKernelName
)))
244 UiMessageBox("Linux kernel filename not specified for selected OS!");
248 // Get the initrd name
249 if (IniReadSettingByName(SectionId
, "Initrd", LinuxInitrdName
, sizeof(LinuxInitrdName
)))
251 LinuxHasInitrd
= TRUE
;
254 // Get the command line
255 if (IniReadSettingByName(SectionId
, "CommandLine", LinuxCommandLine
, sizeof(LinuxCommandLine
)))
257 RemoveQuotes(LinuxCommandLine
);
258 LinuxCommandLineSize
= strlen(LinuxCommandLine
) + 1;
264 BOOLEAN
LinuxReadBootSector(PFILE LinuxKernelFile
)
266 // Allocate memory for boot sector
267 LinuxBootSector
= (PLINUX_BOOTSECTOR
)MmAllocateMemory(512);
268 if (LinuxBootSector
== NULL
)
273 // Read linux boot sector
274 FsSetFilePointer(LinuxKernelFile
, 0);
275 if (!FsReadFile(LinuxKernelFile
, 512, NULL
, LinuxBootSector
))
280 // Check for validity
281 if (LinuxBootSector
->BootFlag
!= LINUX_BOOT_SECTOR_MAGIC
)
283 UiMessageBox("Invalid boot sector magic (0xaa55)");
287 DbgDumpBuffer(DPRINT_LINUX
, LinuxBootSector
, 512);
289 DbgPrint((DPRINT_LINUX
, "SetupSectors: %d\n", LinuxBootSector
->SetupSectors
));
290 DbgPrint((DPRINT_LINUX
, "RootFlags: 0x%x\n", LinuxBootSector
->RootFlags
));
291 DbgPrint((DPRINT_LINUX
, "SystemSize: 0x%x\n", LinuxBootSector
->SystemSize
));
292 DbgPrint((DPRINT_LINUX
, "SwapDevice: 0x%x\n", LinuxBootSector
->SwapDevice
));
293 DbgPrint((DPRINT_LINUX
, "RamSize: 0x%x\n", LinuxBootSector
->RamSize
));
294 DbgPrint((DPRINT_LINUX
, "VideoMode: 0x%x\n", LinuxBootSector
->VideoMode
));
295 DbgPrint((DPRINT_LINUX
, "RootDevice: 0x%x\n", LinuxBootSector
->RootDevice
));
296 DbgPrint((DPRINT_LINUX
, "BootFlag: 0x%x\n", LinuxBootSector
->BootFlag
));
301 BOOLEAN
LinuxReadSetupSector(PFILE LinuxKernelFile
)
303 UCHAR TempLinuxSetupSector
[512];
305 LinuxSetupSector
= (PLINUX_SETUPSECTOR
)TempLinuxSetupSector
;
307 // Read first linux setup sector
308 FsSetFilePointer(LinuxKernelFile
, 512);
309 if (!FsReadFile(LinuxKernelFile
, 512, NULL
, TempLinuxSetupSector
))
314 // Check the kernel version
315 if (!LinuxCheckKernelVersion())
320 if (NewStyleLinuxKernel
)
322 SetupSectorSize
= 512 * LinuxBootSector
->SetupSectors
;
326 SetupSectorSize
= 4 * 512; // Always 4 setup sectors
329 // Allocate memory for setup sectors
330 LinuxSetupSector
= (PLINUX_SETUPSECTOR
)MmAllocateMemory(SetupSectorSize
);
331 if (LinuxSetupSector
== NULL
)
336 // Copy over first setup sector
337 RtlCopyMemory(LinuxSetupSector
, TempLinuxSetupSector
, 512);
339 // Read in the rest of the linux setup sectors
340 FsSetFilePointer(LinuxKernelFile
, 1024);
341 if (!FsReadFile(LinuxKernelFile
, SetupSectorSize
- 512, NULL
, (PVOID
)((ULONG_PTR
)LinuxSetupSector
+ 512)))
346 DbgDumpBuffer(DPRINT_LINUX
, LinuxSetupSector
, SetupSectorSize
);
348 DbgPrint((DPRINT_LINUX
, "SetupHeaderSignature: 0x%x (HdrS)\n", LinuxSetupSector
->SetupHeaderSignature
));
349 DbgPrint((DPRINT_LINUX
, "Version: 0x%x\n", LinuxSetupSector
->Version
));
350 DbgPrint((DPRINT_LINUX
, "RealModeSwitch: 0x%x\n", LinuxSetupSector
->RealModeSwitch
));
351 DbgPrint((DPRINT_LINUX
, "SetupSeg: 0x%x\n", LinuxSetupSector
->SetupSeg
));
352 DbgPrint((DPRINT_LINUX
, "StartSystemSeg: 0x%x\n", LinuxSetupSector
->StartSystemSeg
));
353 DbgPrint((DPRINT_LINUX
, "KernelVersion: 0x%x\n", LinuxSetupSector
->KernelVersion
));
354 DbgPrint((DPRINT_LINUX
, "TypeOfLoader: 0x%x\n", LinuxSetupSector
->TypeOfLoader
));
355 DbgPrint((DPRINT_LINUX
, "LoadFlags: 0x%x\n", LinuxSetupSector
->LoadFlags
));
356 DbgPrint((DPRINT_LINUX
, "SetupMoveSize: 0x%x\n", LinuxSetupSector
->SetupMoveSize
));
357 DbgPrint((DPRINT_LINUX
, "Code32Start: 0x%x\n", LinuxSetupSector
->Code32Start
));
358 DbgPrint((DPRINT_LINUX
, "RamdiskAddress: 0x%x\n", LinuxSetupSector
->RamdiskAddress
));
359 DbgPrint((DPRINT_LINUX
, "RamdiskSize: 0x%x\n", LinuxSetupSector
->RamdiskSize
));
360 DbgPrint((DPRINT_LINUX
, "BootSectKludgeOffset: 0x%x\n", LinuxSetupSector
->BootSectKludgeOffset
));
361 DbgPrint((DPRINT_LINUX
, "BootSectKludgeSegment: 0x%x\n", LinuxSetupSector
->BootSectKludgeSegment
));
362 DbgPrint((DPRINT_LINUX
, "HeapEnd: 0x%x\n", LinuxSetupSector
->HeapEnd
));
367 BOOLEAN
LinuxReadKernel(PFILE LinuxKernelFile
)
370 CHAR StatusText
[260];
373 sprintf(StatusText
, "Loading %s", LinuxKernelName
);
374 UiDrawStatusText(StatusText
);
376 // Allocate memory for Linux kernel
377 LinuxKernelLoadAddress
= MmAllocateMemoryAtAddress(LinuxKernelSize
, (PVOID
)LINUX_KERNEL_LOAD_ADDRESS
, LoaderSystemCode
);
378 if (LinuxKernelLoadAddress
!= (PVOID
)LINUX_KERNEL_LOAD_ADDRESS
)
383 LoadAddress
= LinuxKernelLoadAddress
;
385 // Read linux kernel to 0x100000 (1mb)
386 FsSetFilePointer(LinuxKernelFile
, 512 + SetupSectorSize
);
387 for (BytesLoaded
=0; BytesLoaded
<LinuxKernelSize
; )
389 if (!FsReadFile(LinuxKernelFile
, LINUX_READ_CHUNK_SIZE
, NULL
, LoadAddress
))
394 BytesLoaded
+= LINUX_READ_CHUNK_SIZE
;
395 LoadAddress
= (PVOID
)((ULONG_PTR
)LoadAddress
+ LINUX_READ_CHUNK_SIZE
);
397 UiDrawProgressBarCenter(BytesLoaded
, LinuxKernelSize
+ LinuxInitrdSize
, LinuxBootDescription
);
403 BOOLEAN
LinuxCheckKernelVersion(VOID
)
405 // Just assume old kernel until we find otherwise
406 NewStyleLinuxKernel
= FALSE
;
408 // Check for new style setup header
409 if (LinuxSetupSector
->SetupHeaderSignature
!= LINUX_SETUP_HEADER_ID
)
411 NewStyleLinuxKernel
= FALSE
;
413 // Check for version below 2.0
414 else if (LinuxSetupSector
->Version
< 0x0200)
416 NewStyleLinuxKernel
= FALSE
;
418 // Check for version 2.0
419 else if (LinuxSetupSector
->Version
== 0x0200)
421 NewStyleLinuxKernel
= TRUE
;
423 // Check for version 2.01+
424 else if (LinuxSetupSector
->Version
>= 0x0201)
426 NewStyleLinuxKernel
= TRUE
;
427 LinuxSetupSector
->HeapEnd
= 0x9000;
428 LinuxSetupSector
->LoadFlags
|= LINUX_FLAG_CAN_USE_HEAP
;
431 if ((NewStyleLinuxKernel
== FALSE
) && (LinuxHasInitrd
== TRUE
))
433 UiMessageBox("Error: Cannot load a ramdisk (initrd) with an old kernel image.");
440 BOOLEAN
LinuxReadInitrd(PFILE LinuxInitrdFile
)
443 CHAR StatusText
[260];
445 sprintf(StatusText
, "Loading %s", LinuxInitrdName
);
446 UiDrawStatusText(StatusText
);
448 // Allocate memory for the ramdisk
449 //LinuxInitrdLoadAddress = MmAllocateMemory(LinuxInitrdSize);
450 // Try to align it at the next MB boundary after the kernel
451 //LinuxInitrdLoadAddress = MmAllocateMemoryAtAddress(LinuxInitrdSize, (PVOID)ROUND_UP((LINUX_KERNEL_LOAD_ADDRESS + LinuxKernelSize), 0x100000));
452 if (LinuxSetupSector
->Version
<= 0x0202)
454 LinuxInitrdLoadAddress
= MmAllocateHighestMemoryBelowAddress(LinuxInitrdSize
, (PVOID
)LINUX_MAX_INITRD_ADDRESS
, LoaderSystemCode
);
458 LinuxInitrdLoadAddress
= MmAllocateHighestMemoryBelowAddress(LinuxInitrdSize
, (PVOID
)LinuxSetupSector
->InitrdAddressMax
, LoaderSystemCode
);
460 if (LinuxInitrdLoadAddress
== NULL
)
465 // Set the information in the setup struct
466 LinuxSetupSector
->RamdiskAddress
= (ULONG
)LinuxInitrdLoadAddress
;
467 LinuxSetupSector
->RamdiskSize
= LinuxInitrdSize
;
469 DbgPrint((DPRINT_LINUX
, "RamdiskAddress: 0x%x\n", LinuxSetupSector
->RamdiskAddress
));
470 DbgPrint((DPRINT_LINUX
, "RamdiskSize: 0x%x\n", LinuxSetupSector
->RamdiskSize
));
472 if (LinuxSetupSector
->Version
>= 0x0203)
474 DbgPrint((DPRINT_LINUX
, "InitrdAddressMax: 0x%x\n", LinuxSetupSector
->InitrdAddressMax
));
477 // Read in the ramdisk
478 for (BytesLoaded
=0; BytesLoaded
<LinuxInitrdSize
; )
480 if (!FsReadFile(LinuxInitrdFile
, LINUX_READ_CHUNK_SIZE
, NULL
, (PVOID
)LinuxInitrdLoadAddress
))
485 BytesLoaded
+= LINUX_READ_CHUNK_SIZE
;
486 LinuxInitrdLoadAddress
= (PVOID
)((ULONG_PTR
)LinuxInitrdLoadAddress
+ LINUX_READ_CHUNK_SIZE
);
488 UiDrawProgressBarCenter(BytesLoaded
+ LinuxKernelSize
, LinuxInitrdSize
+ LinuxKernelSize
, LinuxBootDescription
);
493 #endif /* __i386__ */