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.
24 /* INCLUDES *******************************************************************/
29 DBG_DEFAULT_CHANNEL(LINUX
);
31 /* GLOBALS ********************************************************************/
33 #define LINUX_READ_CHUNK_SIZE 0x20000 // Read 128k at a time
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 CHAR LinuxKernelName
[260];
42 CHAR LinuxInitrdName
[260];
43 BOOLEAN LinuxHasInitrd
= FALSE
;
44 CHAR LinuxCommandLine
[260] = "";
45 ULONG LinuxCommandLineSize
= 0;
46 PVOID LinuxKernelLoadAddress
= NULL
;
47 PVOID LinuxInitrdLoadAddress
= NULL
;
48 CHAR LinuxBootDescription
[80];
49 CHAR LinuxBootPath
[260] = "";
51 /* FUNCTIONS ******************************************************************/
53 BOOLEAN
RemoveQuotes(PCHAR QuotedString
)
59 /* Skip spaces up to " */
61 while (*p
== ' ' || *p
== '"')
66 while (*p
!= '"' && *p
!= ANSI_NULL
)
71 strcpy(TempString
, Start
);
72 strcpy(QuotedString
, TempString
);
78 LoadAndBootLinux(IN OperatingSystemItem
* OperatingSystem
,
79 IN USHORT OperatingSystemVersion
)
81 PCSTR SectionName
= OperatingSystem
->SystemPartition
;
82 PCSTR Description
= OperatingSystem
->LoadIdentifier
;
83 PFILE LinuxKernel
= 0;
84 PFILE LinuxInitrdFile
= 0;
87 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(SectionName
))
100 LinuxKernel
= FsOpenFile(LinuxKernelName
);
103 UiMessageBox("Linux kernel \'%s\' not found.", LinuxKernelName
);
104 goto LinuxBootFailed
;
107 /* Open the initrd file image (if necessary) */
110 LinuxInitrdFile
= FsOpenFile(LinuxInitrdName
);
111 if (!LinuxInitrdFile
)
113 UiMessageBox("Linux initrd image \'%s\' not found.", LinuxInitrdName
);
114 goto LinuxBootFailed
;
118 /* Read the boot sector */
119 if (!LinuxReadBootSector(LinuxKernel
))
120 goto LinuxBootFailed
;
122 /* Read the setup sector */
123 if (!LinuxReadSetupSector(LinuxKernel
))
124 goto LinuxBootFailed
;
126 /* Calc kernel size */
127 LinuxKernelSize
= FsGetFileSize(LinuxKernel
) - (512 + SetupSectorSize
);
129 /* Get the file size */
130 LinuxInitrdSize
= FsGetFileSize(LinuxInitrdFile
);
132 /* Read the kernel */
133 if (!LinuxReadKernel(LinuxKernel
))
134 goto LinuxBootFailed
;
136 /* Read the initrd (if necessary) */
139 if (!LinuxReadInitrd(LinuxInitrdFile
))
140 goto LinuxBootFailed
;
143 // If the default root device is set to FLOPPY (0000h), change to /dev/fd0 (0200h)
144 if (LinuxBootSector
->RootDevice
== 0x0000)
145 LinuxBootSector
->RootDevice
= 0x0200;
147 if (LinuxSetupSector
->Version
>= 0x0202)
149 LinuxSetupSector
->CommandLinePointer
= 0x99000;
153 LinuxBootSector
->CommandLineMagic
= LINUX_COMMAND_LINE_MAGIC
;
154 LinuxBootSector
->CommandLineOffset
= 0x9000;
157 if (NewStyleLinuxKernel
)
158 LinuxSetupSector
->TypeOfLoader
= LINUX_LOADER_TYPE_FREELOADER
;
160 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
)
171 BootNewLinuxKernel();
173 BootOldLinuxKernel(LinuxKernelSize
);
179 FsCloseFile(LinuxKernel
);
182 FsCloseFile(LinuxInitrdFile
);
184 if (LinuxBootSector
!= NULL
)
185 MmFreeMemory(LinuxBootSector
);
187 if (LinuxSetupSector
!= NULL
)
188 MmFreeMemory(LinuxSetupSector
);
190 if (LinuxKernelLoadAddress
!= NULL
)
191 MmFreeMemory(LinuxKernelLoadAddress
);
193 if (LinuxInitrdLoadAddress
!= NULL
)
194 MmFreeMemory(LinuxInitrdLoadAddress
);
196 LinuxBootSector
= NULL
;
197 LinuxSetupSector
= NULL
;
198 LinuxKernelLoadAddress
= NULL
;
199 LinuxInitrdLoadAddress
= NULL
;
201 NewStyleLinuxKernel
= FALSE
;
203 LinuxHasInitrd
= FALSE
;
204 strcpy(LinuxCommandLine
, "");
205 LinuxCommandLineSize
= 0;
208 BOOLEAN
LinuxParseIniSection(PCSTR SectionName
)
212 /* Find all the message box settings and run them */
213 UiShowMessageBoxesInSection(SectionName
);
215 /* Try to open the operating system section in the .ini file */
216 if (!IniOpenSection(SectionName
, &SectionId
))
218 UiMessageBox("Section [%s] not found in freeldr.ini.", SectionName
);
222 if (!IniReadSettingByName(SectionId
, "BootPath", LinuxBootPath
, sizeof(LinuxBootPath
)))
224 UiMessageBox("Boot path not specified for selected OS!");
228 /* Get the kernel name */
229 if (!IniReadSettingByName(SectionId
, "Kernel", LinuxKernelName
, sizeof(LinuxKernelName
)))
231 UiMessageBox("Linux kernel filename not specified for selected OS!");
235 /* Get the initrd name */
236 if (IniReadSettingByName(SectionId
, "Initrd", LinuxInitrdName
, sizeof(LinuxInitrdName
)))
238 LinuxHasInitrd
= TRUE
;
241 /* Get the command line */
242 if (IniReadSettingByName(SectionId
, "CommandLine", LinuxCommandLine
, sizeof(LinuxCommandLine
)))
244 RemoveQuotes(LinuxCommandLine
);
245 LinuxCommandLineSize
= strlen(LinuxCommandLine
) + 1;
251 BOOLEAN
LinuxReadBootSector(PFILE LinuxKernelFile
)
253 /* Allocate memory for boot sector */
254 LinuxBootSector
= MmAllocateMemoryWithType(512, LoaderSystemCode
);
255 if (LinuxBootSector
== NULL
)
258 /* Read linux boot sector */
259 FsSetFilePointer(LinuxKernelFile
, 0);
260 if (!FsReadFile(LinuxKernelFile
, 512, NULL
, LinuxBootSector
))
263 /* Check for validity */
264 if (LinuxBootSector
->BootFlag
!= LINUX_BOOT_SECTOR_MAGIC
)
266 UiMessageBox("Invalid boot sector magic (0xaa55)");
270 // DbgDumpBuffer(DPRINT_LINUX, LinuxBootSector, 512);
272 TRACE("SetupSectors: %d\n", LinuxBootSector
->SetupSectors
);
273 TRACE("RootFlags: 0x%x\n", LinuxBootSector
->RootFlags
);
274 TRACE("SystemSize: 0x%x\n", LinuxBootSector
->SystemSize
);
275 TRACE("SwapDevice: 0x%x\n", LinuxBootSector
->SwapDevice
);
276 TRACE("RamSize: 0x%x\n", LinuxBootSector
->RamSize
);
277 TRACE("VideoMode: 0x%x\n", LinuxBootSector
->VideoMode
);
278 TRACE("RootDevice: 0x%x\n", LinuxBootSector
->RootDevice
);
279 TRACE("BootFlag: 0x%x\n", LinuxBootSector
->BootFlag
);
284 BOOLEAN
LinuxReadSetupSector(PFILE LinuxKernelFile
)
286 UCHAR TempLinuxSetupSector
[512];
288 LinuxSetupSector
= (PLINUX_SETUPSECTOR
)TempLinuxSetupSector
;
290 /* Read first linux setup sector */
291 FsSetFilePointer(LinuxKernelFile
, 512);
292 if (!FsReadFile(LinuxKernelFile
, 512, NULL
, TempLinuxSetupSector
))
295 /* Check the kernel version */
296 if (!LinuxCheckKernelVersion())
299 if (NewStyleLinuxKernel
)
300 SetupSectorSize
= 512 * LinuxBootSector
->SetupSectors
;
302 SetupSectorSize
= 512 * 4; // Always 4 setup sectors
304 /* Allocate memory for setup sectors */
305 LinuxSetupSector
= MmAllocateMemoryWithType(SetupSectorSize
, LoaderSystemCode
);
306 if (LinuxSetupSector
== NULL
)
309 /* Copy over first setup sector */
310 RtlCopyMemory(LinuxSetupSector
, TempLinuxSetupSector
, 512);
312 /* Read in the rest of the linux setup sectors */
313 FsSetFilePointer(LinuxKernelFile
, 1024);
314 if (!FsReadFile(LinuxKernelFile
, SetupSectorSize
- 512, NULL
, (PVOID
)((ULONG_PTR
)LinuxSetupSector
+ 512)))
317 // DbgDumpBuffer(DPRINT_LINUX, LinuxSetupSector, SetupSectorSize);
319 TRACE("SetupHeaderSignature: 0x%x (HdrS)\n", LinuxSetupSector
->SetupHeaderSignature
);
320 TRACE("Version: 0x%x\n", LinuxSetupSector
->Version
);
321 TRACE("RealModeSwitch: 0x%x\n", LinuxSetupSector
->RealModeSwitch
);
322 TRACE("SetupSeg: 0x%x\n", LinuxSetupSector
->SetupSeg
);
323 TRACE("StartSystemSeg: 0x%x\n", LinuxSetupSector
->StartSystemSeg
);
324 TRACE("KernelVersion: 0x%x\n", LinuxSetupSector
->KernelVersion
);
325 TRACE("TypeOfLoader: 0x%x\n", LinuxSetupSector
->TypeOfLoader
);
326 TRACE("LoadFlags: 0x%x\n", LinuxSetupSector
->LoadFlags
);
327 TRACE("SetupMoveSize: 0x%x\n", LinuxSetupSector
->SetupMoveSize
);
328 TRACE("Code32Start: 0x%x\n", LinuxSetupSector
->Code32Start
);
329 TRACE("RamdiskAddress: 0x%x\n", LinuxSetupSector
->RamdiskAddress
);
330 TRACE("RamdiskSize: 0x%x\n", LinuxSetupSector
->RamdiskSize
);
331 TRACE("BootSectKludgeOffset: 0x%x\n", LinuxSetupSector
->BootSectKludgeOffset
);
332 TRACE("BootSectKludgeSegment: 0x%x\n", LinuxSetupSector
->BootSectKludgeSegment
);
333 TRACE("HeapEnd: 0x%x\n", LinuxSetupSector
->HeapEnd
);
338 BOOLEAN
LinuxReadKernel(PFILE LinuxKernelFile
)
341 CHAR StatusText
[260];
344 sprintf(StatusText
, "Loading %s", LinuxKernelName
);
345 UiDrawStatusText(StatusText
);
347 /* Allocate memory for Linux kernel */
348 LinuxKernelLoadAddress
= MmAllocateMemoryAtAddress(LinuxKernelSize
, (PVOID
)LINUX_KERNEL_LOAD_ADDRESS
, LoaderSystemCode
);
349 if (LinuxKernelLoadAddress
!= (PVOID
)LINUX_KERNEL_LOAD_ADDRESS
)
354 LoadAddress
= LinuxKernelLoadAddress
;
356 /* Read linux kernel to 0x100000 (1mb) */
357 FsSetFilePointer(LinuxKernelFile
, 512 + SetupSectorSize
);
358 for (BytesLoaded
=0; BytesLoaded
<LinuxKernelSize
; )
360 if (!FsReadFile(LinuxKernelFile
, LINUX_READ_CHUNK_SIZE
, NULL
, LoadAddress
))
363 BytesLoaded
+= LINUX_READ_CHUNK_SIZE
;
364 LoadAddress
= (PVOID
)((ULONG_PTR
)LoadAddress
+ LINUX_READ_CHUNK_SIZE
);
366 UiDrawProgressBarCenter(BytesLoaded
, LinuxKernelSize
+ LinuxInitrdSize
, LinuxBootDescription
);
372 BOOLEAN
LinuxCheckKernelVersion(VOID
)
374 /* Just assume old kernel until we find otherwise */
375 NewStyleLinuxKernel
= FALSE
;
377 /* Check for new style setup header */
378 if (LinuxSetupSector
->SetupHeaderSignature
!= LINUX_SETUP_HEADER_ID
)
380 NewStyleLinuxKernel
= FALSE
;
382 /* Check for version below 2.0 */
383 else if (LinuxSetupSector
->Version
< 0x0200)
385 NewStyleLinuxKernel
= FALSE
;
387 /* Check for version 2.0 */
388 else if (LinuxSetupSector
->Version
== 0x0200)
390 NewStyleLinuxKernel
= TRUE
;
392 /* Check for version 2.01+ */
393 else if (LinuxSetupSector
->Version
>= 0x0201)
395 NewStyleLinuxKernel
= TRUE
;
396 LinuxSetupSector
->HeapEnd
= 0x9000;
397 LinuxSetupSector
->LoadFlags
|= LINUX_FLAG_CAN_USE_HEAP
;
400 if ((NewStyleLinuxKernel
== FALSE
) && (LinuxHasInitrd
))
402 UiMessageBox("Error: Cannot load a ramdisk (initrd) with an old kernel image.");
409 BOOLEAN
LinuxReadInitrd(PFILE LinuxInitrdFile
)
412 CHAR StatusText
[260];
414 sprintf(StatusText
, "Loading %s", LinuxInitrdName
);
415 UiDrawStatusText(StatusText
);
417 // Allocate memory for the ramdisk
418 //LinuxInitrdLoadAddress = MmAllocateMemory(LinuxInitrdSize);
419 // Try to align it at the next MB boundary after the kernel
420 //LinuxInitrdLoadAddress = MmAllocateMemoryAtAddress(LinuxInitrdSize, (PVOID)ROUND_UP((LINUX_KERNEL_LOAD_ADDRESS + LinuxKernelSize), 0x100000));
421 if (LinuxSetupSector
->Version
<= 0x0202)
423 LinuxInitrdLoadAddress
= MmAllocateHighestMemoryBelowAddress(LinuxInitrdSize
, (PVOID
)LINUX_MAX_INITRD_ADDRESS
, LoaderSystemCode
);
427 LinuxInitrdLoadAddress
= MmAllocateHighestMemoryBelowAddress(LinuxInitrdSize
, (PVOID
)LinuxSetupSector
->InitrdAddressMax
, LoaderSystemCode
);
429 if (LinuxInitrdLoadAddress
== NULL
)
434 /* Set the information in the setup struct */
435 LinuxSetupSector
->RamdiskAddress
= (ULONG
)LinuxInitrdLoadAddress
;
436 LinuxSetupSector
->RamdiskSize
= LinuxInitrdSize
;
438 TRACE("RamdiskAddress: 0x%x\n", LinuxSetupSector
->RamdiskAddress
);
439 TRACE("RamdiskSize: 0x%x\n", LinuxSetupSector
->RamdiskSize
);
441 if (LinuxSetupSector
->Version
>= 0x0203)
443 TRACE("InitrdAddressMax: 0x%x\n", LinuxSetupSector
->InitrdAddressMax
);
446 /* Read in the ramdisk */
447 for (BytesLoaded
=0; BytesLoaded
<LinuxInitrdSize
; )
449 if (!FsReadFile(LinuxInitrdFile
, LINUX_READ_CHUNK_SIZE
, NULL
, (PVOID
)LinuxInitrdLoadAddress
))
452 BytesLoaded
+= LINUX_READ_CHUNK_SIZE
;
453 LinuxInitrdLoadAddress
= (PVOID
)((ULONG_PTR
)LinuxInitrdLoadAddress
+ LINUX_READ_CHUNK_SIZE
);
455 UiDrawProgressBarCenter(BytesLoaded
+ LinuxKernelSize
, LinuxInitrdSize
+ LinuxKernelSize
, LinuxBootDescription
);