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 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.
25 #define LINUX_READ_CHUNK_SIZE 0x20000 // Read 128k at a time
27 DBG_DEFAULT_CHANNEL(LINUX
);
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 BOOLEAN
RemoveQuotes(PCHAR QuotedString
)
52 // Skip spaces up to "
55 while (*p
== ' ' || *p
== '"')
62 while (*p
!= '"' && *p
!= ANSI_NULL
)
69 strcpy(TempString
, Start
);
70 strcpy(QuotedString
, TempString
);
75 VOID
LoadAndBootLinux(PCSTR OperatingSystemName
, PCSTR Description
)
77 PFILE LinuxKernel
= 0;
78 PFILE LinuxInitrdFile
= 0;
85 sprintf(LinuxBootDescription
, "Loading %s...", Description
);
89 strcpy(LinuxBootDescription
, "Loading Linux...");
92 UiDrawStatusText(LinuxBootDescription
);
93 UiDrawProgressBarCenter(0, 100, LinuxBootDescription
);
95 // Parse the .ini file section
96 if (!LinuxParseIniSection(OperatingSystemName
))
102 LinuxKernel
= FsOpenFile(LinuxKernelName
);
105 sprintf(TempString
, "Linux kernel \'%s\' not found.", LinuxKernelName
);
106 UiMessageBox(TempString
);
107 goto LinuxBootFailed
;
110 // Open the initrd file image (if necessary)
113 LinuxInitrdFile
= FsOpenFile(LinuxInitrdName
);
114 if (!LinuxInitrdFile
)
116 sprintf(TempString
, "Linux initrd image \'%s\' not found.", LinuxInitrdName
);
117 UiMessageBox(TempString
);
118 goto LinuxBootFailed
;
122 // Read the boot sector
123 if (!LinuxReadBootSector(LinuxKernel
))
125 goto LinuxBootFailed
;
128 // Read the setup sector
129 if (!LinuxReadSetupSector(LinuxKernel
))
131 goto LinuxBootFailed
;
135 LinuxKernelSize
= FsGetFileSize(LinuxKernel
) - (512 + SetupSectorSize
);
138 LinuxInitrdSize
= FsGetFileSize(LinuxInitrdFile
);
141 if (!LinuxReadKernel(LinuxKernel
))
143 goto LinuxBootFailed
;
146 // Read the initrd (if necessary)
149 if (!LinuxReadInitrd(LinuxInitrdFile
))
151 goto LinuxBootFailed
;
155 // If the default root device is set to FLOPPY (0000h), change to /dev/fd0 (0200h)
156 if (LinuxBootSector
->RootDevice
== 0x0000)
158 LinuxBootSector
->RootDevice
= 0x0200;
161 if (LinuxSetupSector
->Version
>= 0x0202)
163 LinuxSetupSector
->CommandLinePointer
= 0x99000;
167 LinuxBootSector
->CommandLineMagic
= LINUX_COMMAND_LINE_MAGIC
;
168 LinuxBootSector
->CommandLineOffset
= 0x9000;
171 if (NewStyleLinuxKernel
)
173 LinuxSetupSector
->TypeOfLoader
= LINUX_LOADER_TYPE_FREELOADER
;
177 LinuxSetupSector
->LoadFlags
= 0;
180 RtlCopyMemory((PVOID
)0x90000, LinuxBootSector
, 512);
181 RtlCopyMemory((PVOID
)0x90200, LinuxSetupSector
, SetupSectorSize
);
182 RtlCopyMemory((PVOID
)0x99000, LinuxCommandLine
, LinuxCommandLineSize
);
184 UiUnInitialize("Booting Linux...");
186 DiskStopFloppyMotor();
188 if (LinuxSetupSector
->LoadFlags
& LINUX_FLAG_LOAD_HIGH
)
190 BootNewLinuxKernel();
194 BootOldLinuxKernel(LinuxKernelSize
);
202 FsCloseFile(LinuxKernel
);
206 FsCloseFile(LinuxInitrdFile
);
209 if (LinuxBootSector
!= NULL
)
211 MmFreeMemory(LinuxBootSector
);
213 if (LinuxSetupSector
!= NULL
)
215 MmFreeMemory(LinuxSetupSector
);
217 if (LinuxKernelLoadAddress
!= NULL
)
219 MmFreeMemory(LinuxKernelLoadAddress
);
221 if (LinuxInitrdLoadAddress
!= NULL
)
223 MmFreeMemory(LinuxInitrdLoadAddress
);
226 LinuxBootSector
= NULL
;
227 LinuxSetupSector
= NULL
;
228 LinuxKernelLoadAddress
= NULL
;
229 LinuxInitrdLoadAddress
= NULL
;
231 NewStyleLinuxKernel
= FALSE
;
233 LinuxHasInitrd
= FALSE
;
234 strcpy(LinuxCommandLine
, "");
235 LinuxCommandLineSize
= 0;
238 BOOLEAN
LinuxParseIniSection(PCSTR OperatingSystemName
)
240 CHAR SettingName
[260];
243 // Find all the message box settings and run them
244 UiShowMessageBoxesInSection(OperatingSystemName
);
246 // Try to open the operating system section in the .ini file
247 if (!IniOpenSection(OperatingSystemName
, &SectionId
))
249 sprintf(SettingName
, "Section [%s] not found in freeldr.ini.\n", OperatingSystemName
);
250 UiMessageBox(SettingName
);
254 if (!IniReadSettingByName(SectionId
, "BootPath", LinuxBootPath
, sizeof(LinuxBootPath
)))
256 UiMessageBox("Boot path not specified for selected OS!");
260 // Get the kernel name
261 if (!IniReadSettingByName(SectionId
, "Kernel", LinuxKernelName
, sizeof(LinuxKernelName
)))
263 UiMessageBox("Linux kernel filename not specified for selected OS!");
267 // Get the initrd name
268 if (IniReadSettingByName(SectionId
, "Initrd", LinuxInitrdName
, sizeof(LinuxInitrdName
)))
270 LinuxHasInitrd
= TRUE
;
273 // Get the command line
274 if (IniReadSettingByName(SectionId
, "CommandLine", LinuxCommandLine
, sizeof(LinuxCommandLine
)))
276 RemoveQuotes(LinuxCommandLine
);
277 LinuxCommandLineSize
= strlen(LinuxCommandLine
) + 1;
283 BOOLEAN
LinuxReadBootSector(PFILE LinuxKernelFile
)
285 // Allocate memory for boot sector
286 LinuxBootSector
= MmAllocateMemoryWithType(512, LoaderSystemCode
);
287 if (LinuxBootSector
== NULL
)
292 // Read linux boot sector
293 FsSetFilePointer(LinuxKernelFile
, 0);
294 if (!FsReadFile(LinuxKernelFile
, 512, NULL
, LinuxBootSector
))
299 // Check for validity
300 if (LinuxBootSector
->BootFlag
!= LINUX_BOOT_SECTOR_MAGIC
)
302 UiMessageBox("Invalid boot sector magic (0xaa55)");
306 DbgDumpBuffer(DPRINT_LINUX
, LinuxBootSector
, 512);
308 TRACE("SetupSectors: %d\n", LinuxBootSector
->SetupSectors
);
309 TRACE("RootFlags: 0x%x\n", LinuxBootSector
->RootFlags
);
310 TRACE("SystemSize: 0x%x\n", LinuxBootSector
->SystemSize
);
311 TRACE("SwapDevice: 0x%x\n", LinuxBootSector
->SwapDevice
);
312 TRACE("RamSize: 0x%x\n", LinuxBootSector
->RamSize
);
313 TRACE("VideoMode: 0x%x\n", LinuxBootSector
->VideoMode
);
314 TRACE("RootDevice: 0x%x\n", LinuxBootSector
->RootDevice
);
315 TRACE("BootFlag: 0x%x\n", LinuxBootSector
->BootFlag
);
320 BOOLEAN
LinuxReadSetupSector(PFILE LinuxKernelFile
)
322 UCHAR TempLinuxSetupSector
[512];
324 LinuxSetupSector
= (PLINUX_SETUPSECTOR
)TempLinuxSetupSector
;
326 // Read first linux setup sector
327 FsSetFilePointer(LinuxKernelFile
, 512);
328 if (!FsReadFile(LinuxKernelFile
, 512, NULL
, TempLinuxSetupSector
))
333 // Check the kernel version
334 if (!LinuxCheckKernelVersion())
339 if (NewStyleLinuxKernel
)
341 SetupSectorSize
= 512 * LinuxBootSector
->SetupSectors
;
345 SetupSectorSize
= 4 * 512; // Always 4 setup sectors
348 // Allocate memory for setup sectors
349 LinuxSetupSector
= MmAllocateMemoryWithType(SetupSectorSize
, LoaderSystemCode
);
350 if (LinuxSetupSector
== NULL
)
355 // Copy over first setup sector
356 RtlCopyMemory(LinuxSetupSector
, TempLinuxSetupSector
, 512);
358 // Read in the rest of the linux setup sectors
359 FsSetFilePointer(LinuxKernelFile
, 1024);
360 if (!FsReadFile(LinuxKernelFile
, SetupSectorSize
- 512, NULL
, (PVOID
)((ULONG_PTR
)LinuxSetupSector
+ 512)))
365 DbgDumpBuffer(DPRINT_LINUX
, LinuxSetupSector
, SetupSectorSize
);
367 TRACE("SetupHeaderSignature: 0x%x (HdrS)\n", LinuxSetupSector
->SetupHeaderSignature
);
368 TRACE("Version: 0x%x\n", LinuxSetupSector
->Version
);
369 TRACE("RealModeSwitch: 0x%x\n", LinuxSetupSector
->RealModeSwitch
);
370 TRACE("SetupSeg: 0x%x\n", LinuxSetupSector
->SetupSeg
);
371 TRACE("StartSystemSeg: 0x%x\n", LinuxSetupSector
->StartSystemSeg
);
372 TRACE("KernelVersion: 0x%x\n", LinuxSetupSector
->KernelVersion
);
373 TRACE("TypeOfLoader: 0x%x\n", LinuxSetupSector
->TypeOfLoader
);
374 TRACE("LoadFlags: 0x%x\n", LinuxSetupSector
->LoadFlags
);
375 TRACE("SetupMoveSize: 0x%x\n", LinuxSetupSector
->SetupMoveSize
);
376 TRACE("Code32Start: 0x%x\n", LinuxSetupSector
->Code32Start
);
377 TRACE("RamdiskAddress: 0x%x\n", LinuxSetupSector
->RamdiskAddress
);
378 TRACE("RamdiskSize: 0x%x\n", LinuxSetupSector
->RamdiskSize
);
379 TRACE("BootSectKludgeOffset: 0x%x\n", LinuxSetupSector
->BootSectKludgeOffset
);
380 TRACE("BootSectKludgeSegment: 0x%x\n", LinuxSetupSector
->BootSectKludgeSegment
);
381 TRACE("HeapEnd: 0x%x\n", LinuxSetupSector
->HeapEnd
);
386 BOOLEAN
LinuxReadKernel(PFILE LinuxKernelFile
)
389 CHAR StatusText
[260];
392 sprintf(StatusText
, "Loading %s", LinuxKernelName
);
393 UiDrawStatusText(StatusText
);
395 // Allocate memory for Linux kernel
396 LinuxKernelLoadAddress
= MmAllocateMemoryAtAddress(LinuxKernelSize
, (PVOID
)LINUX_KERNEL_LOAD_ADDRESS
, LoaderSystemCode
);
397 if (LinuxKernelLoadAddress
!= (PVOID
)LINUX_KERNEL_LOAD_ADDRESS
)
402 LoadAddress
= LinuxKernelLoadAddress
;
404 // Read linux kernel to 0x100000 (1mb)
405 FsSetFilePointer(LinuxKernelFile
, 512 + SetupSectorSize
);
406 for (BytesLoaded
=0; BytesLoaded
<LinuxKernelSize
; )
408 if (!FsReadFile(LinuxKernelFile
, LINUX_READ_CHUNK_SIZE
, NULL
, LoadAddress
))
413 BytesLoaded
+= LINUX_READ_CHUNK_SIZE
;
414 LoadAddress
= (PVOID
)((ULONG_PTR
)LoadAddress
+ LINUX_READ_CHUNK_SIZE
);
416 UiDrawProgressBarCenter(BytesLoaded
, LinuxKernelSize
+ LinuxInitrdSize
, LinuxBootDescription
);
422 BOOLEAN
LinuxCheckKernelVersion(VOID
)
424 // Just assume old kernel until we find otherwise
425 NewStyleLinuxKernel
= FALSE
;
427 // Check for new style setup header
428 if (LinuxSetupSector
->SetupHeaderSignature
!= LINUX_SETUP_HEADER_ID
)
430 NewStyleLinuxKernel
= FALSE
;
432 // Check for version below 2.0
433 else if (LinuxSetupSector
->Version
< 0x0200)
435 NewStyleLinuxKernel
= FALSE
;
437 // Check for version 2.0
438 else if (LinuxSetupSector
->Version
== 0x0200)
440 NewStyleLinuxKernel
= TRUE
;
442 // Check for version 2.01+
443 else if (LinuxSetupSector
->Version
>= 0x0201)
445 NewStyleLinuxKernel
= TRUE
;
446 LinuxSetupSector
->HeapEnd
= 0x9000;
447 LinuxSetupSector
->LoadFlags
|= LINUX_FLAG_CAN_USE_HEAP
;
450 if ((NewStyleLinuxKernel
== FALSE
) && (LinuxHasInitrd
== TRUE
))
452 UiMessageBox("Error: Cannot load a ramdisk (initrd) with an old kernel image.");
459 BOOLEAN
LinuxReadInitrd(PFILE LinuxInitrdFile
)
462 CHAR StatusText
[260];
464 sprintf(StatusText
, "Loading %s", LinuxInitrdName
);
465 UiDrawStatusText(StatusText
);
467 // Allocate memory for the ramdisk
468 //LinuxInitrdLoadAddress = MmAllocateMemory(LinuxInitrdSize);
469 // Try to align it at the next MB boundary after the kernel
470 //LinuxInitrdLoadAddress = MmAllocateMemoryAtAddress(LinuxInitrdSize, (PVOID)ROUND_UP((LINUX_KERNEL_LOAD_ADDRESS + LinuxKernelSize), 0x100000));
471 if (LinuxSetupSector
->Version
<= 0x0202)
473 LinuxInitrdLoadAddress
= MmAllocateHighestMemoryBelowAddress(LinuxInitrdSize
, (PVOID
)LINUX_MAX_INITRD_ADDRESS
, LoaderSystemCode
);
477 LinuxInitrdLoadAddress
= MmAllocateHighestMemoryBelowAddress(LinuxInitrdSize
, (PVOID
)LinuxSetupSector
->InitrdAddressMax
, LoaderSystemCode
);
479 if (LinuxInitrdLoadAddress
== NULL
)
484 // Set the information in the setup struct
485 LinuxSetupSector
->RamdiskAddress
= (ULONG
)LinuxInitrdLoadAddress
;
486 LinuxSetupSector
->RamdiskSize
= LinuxInitrdSize
;
488 TRACE("RamdiskAddress: 0x%x\n", LinuxSetupSector
->RamdiskAddress
);
489 TRACE("RamdiskSize: 0x%x\n", LinuxSetupSector
->RamdiskSize
);
491 if (LinuxSetupSector
->Version
>= 0x0203)
493 TRACE("InitrdAddressMax: 0x%x\n", LinuxSetupSector
->InitrdAddressMax
);
496 // Read in the ramdisk
497 for (BytesLoaded
=0; BytesLoaded
<LinuxInitrdSize
; )
499 if (!FsReadFile(LinuxInitrdFile
, LINUX_READ_CHUNK_SIZE
, NULL
, (PVOID
)LinuxInitrdLoadAddress
))
504 BytesLoaded
+= LINUX_READ_CHUNK_SIZE
;
505 LinuxInitrdLoadAddress
= (PVOID
)((ULONG_PTR
)LinuxInitrdLoadAddress
+ LINUX_READ_CHUNK_SIZE
);
507 UiDrawProgressBarCenter(BytesLoaded
+ LinuxKernelSize
, LinuxInitrdSize
+ LinuxKernelSize
, LinuxBootDescription
);
512 #endif /* __i386__ */