/* boot.c - load and bootstrap a kernel */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "shared.h"
#include "freebsd.h"
#include "imgact_aout.h"
#include "i386-elf.h"
static int cur_addr;
static int linux_mem_size;
/*
* The next two functions, 'load_image' and 'load_module', are the building
* blocks of the multiboot loader component. They handle essentially all
* of the gory details of loading in a bootable image and the modules.
*/
unsigned long load_flags)
{
union
{
}
pu;
/* presuming that MULTIBOOT_SEARCH is large enough to encompass an
executable header */
/* sets the header pointer to point to the beginning of the
buffer by default */
return KERNEL_TYPE_NONE;
{
grub_close ();
if (!errnum)
return KERNEL_TYPE_NONE;
}
for (i = 0; i < len; i++)
{
{
if (flags & MULTIBOOT_UNSUPPORTED)
{
grub_close ();
return KERNEL_TYPE_NONE;
}
str2 = "Multiboot";
break;
}
}
/* Use BUFFER as a linux kernel header, if the image is Linux zImage
or bzImage. */
/* ELF loading supported if multiboot, FreeBSD and NetBSD. */
if ((type == KERNEL_TYPE_MULTIBOOT
|| suggested_type == KERNEL_TYPE_NETBSD)
&& len > sizeof (Elf32_Ehdr)
{
if (type == KERNEL_TYPE_MULTIBOOT)
else
/* don't want to deal with ELF program header at some random
place in the file -- this generally won't happen */
>= len))
str = "elf";
if (type == KERNEL_TYPE_NONE)
{
/* At the moment, there is no way to identify a NetBSD ELF
kernel, so rely on the suggested type by the user. */
if (suggested_type == KERNEL_TYPE_NETBSD)
{
str2 = "NetBSD";
}
else
{
str2 = "FreeBSD";
}
}
}
else if (flags & MULTIBOOT_AOUT_KLUDGE)
{
/* first offset into file */
/* If the load end address is zero, load the whole contents. */
data_len = 0;
/* If the bss end address is zero, assume that there is no bss area. */
if (cur_addr < 0x100000)
exec_type = 2;
str = "kludge";
}
{
if (type == KERNEL_TYPE_NONE)
{
/*
* If it doesn't have a Multiboot header, then presume
* it is either a FreeBSD or NetBSD executable. If so,
* then use a magic number of normal ordering, ZMAGIC to
* determine if it is FreeBSD.
*
* This is all because freebsd and netbsd seem to require
* masking out some address bits... differently for each
* one... plus of course we need to know which booting
* method to use.
*/
{
cur_addr = (int) entry_addr;
str2 = "FreeBSD";
}
else
{
align_4k = 0;
str2 = "NetBSD";
}
}
/* first offset into file */
if (cur_addr < 0x100000)
exec_type = 1;
str = "a.out";
}
{
int big_linux = 0;
{
/* Put the real mode part at as a high location as possible. */
/* But it must not exceed the traditional area. */
if (linux_data_real_addr > (char *) LINUX_OLD_REAL_MODE_ADDR)
linux_data_real_addr = (char *) LINUX_OLD_REAL_MODE_ADDR;
{
}
else
{
}
}
else
{
/* Your kernel is quite old... */
linux_data_real_addr = (char *) LINUX_OLD_REAL_MODE_ADDR;
}
/* If SETUP_SECTS is not set, set it to the default (4). */
if (! setup_sects)
if (! big_linux
{
grub_printf (" linux 'zImage' kernel too big, try 'make bzImage'\n");
}
else if (linux_data_real_addr + LINUX_SETUP_MOVE_SIZE
else
{
grub_printf (" [Linux-%s, setup=0x%x, size=0x%x]\n",
/* Video mode selection support. What a mess! */
/* NOTE: Even the word "mess" is not still enough to
represent how wrong and bad the Linux video support is,
but I don't want to hear complaints from Linux fanatics
any more. -okuji */
{
char *vga;
/* Find the substring "vga=". */
if (vga)
{
int vid_mode;
/* Handle special strings. */
;
else
{
/* ERRNUM is already set inside the function
safe_parse_maxint. */
grub_close ();
return KERNEL_TYPE_NONE;
}
}
}
/* Check the mem= option to limit memory used for initrd. */
{
char *mem;
if (mem)
{
switch (errnum)
{
case ERR_NUMBER_OVERFLOW:
/* If an overflow occurs, use the maximum address for
initrd instead. This is good, because MAXINT is
greater than LINUX_INITRD_MAX_ADDRESS. */
break;
case ERR_NONE:
{
int shift = 0;
switch (grub_tolower (*value))
{
case 'g':
shift += 10;
case 'm':
shift += 10;
case 'k':
shift += 10;
default:
break;
}
/* Check an overflow. */
else
}
break;
default:
linux_mem_size = 0;
break;
}
}
else
linux_mem_size = 0;
}
/* It is possible that DATA_LEN + SECTOR_SIZE is greater than
MULTIBOOT_SEARCH, so the data may have been read partially. */
data_len + SECTOR_SIZE);
else
{
}
/* Clear the heap space. */
0,
/* Copy command-line plus memory hack to staging area.
NOTE: Linux has a bug that it doesn't handle multiple spaces
between two options and a space after a "mem=" option isn't
removed correctly so the arguments to init could be like
{"init", "", "", NULL}. This affects some not-very-clever
shells. Thus, the code below does a trick to avoid the bug.
That is, copy "mem=XXX" to the end of the command-line, and
avoid to copy spaces unnecessarily. Hell. */
{
/* Old Linux kernels have problems determining the amount of
the available memory. To work around this problem, we add
the "mem" option to the kernel command line. This has its
own drawbacks because newer kernels can determine the
memory map more accurately. Boot protocol 2.03, which
appeared in Linux 2.4.18, provides a pointer to the kernel
version string, so we could check it. But since kernel
2.4.18 and newer are known to detect memory reliably, boot
protocol 2.03 already implies that the kernel is new
enough. The "mem" option is added if neither of the
following conditions is met:
1) The "mem" option is already present.
2) The "kernel" command is used with "--no-mem-option".
3) GNU GRUB is configured not to pass the "mem" option.
4) The kernel supports boot protocol 2.03 or newer. */
&& ! (load_flags & KERNEL_LOAD_NO_MEM_OPTION)
{
*dest++ = ' ';
*dest++ = 'm';
*dest++ = 'e';
*dest++ = 'm';
*dest++ = '=';
*dest++ = 'K';
}
*dest = 0;
}
/* offset into file */
{
grub_close ();
/* Sanity check. */
if (suggested_type != KERNEL_TYPE_NONE
{
return KERNEL_TYPE_NONE;
}
/* Ugly hack. */
}
}
}
else /* no recognizable format */
/* return if error */
if (errnum)
{
grub_close ();
return KERNEL_TYPE_NONE;
}
/* fill the multiboot info structure */
mbi.mods_count = 0;
str = "";
if (exec_type) /* can be loaded like a.out */
{
if (flags & MULTIBOOT_AOUT_KLUDGE)
str = "-and-data";
/* read text, then read data */
{
if (!(flags & MULTIBOOT_AOUT_KLUDGE))
{
/* we have to align to a 4K boundary */
if (align_4k)
else
printf (", C");
!= data_len)
&& !errnum)
}
if (!errnum)
{
}
}
else if (!errnum)
{
/* we should align to a 4K boundary here for good measure */
if (align_4k)
cur_addr += sizeof (int);
{
if (grub_read ((char *) &i, sizeof (int)) == sizeof (int))
{
cur_addr += sizeof (int);
i -= sizeof (int);
printf (", strtab=0x%x", i);
!= i);
cur_addr += i;
}
else
symtab_err = 1;
}
else
symtab_err = 1;
if (symtab_err)
{
printf ("(bad)");
}
else
}
}
else
/* ELF executable */
{
/* reset this to zero for now */
cur_addr = 0;
/* scan for program segments */
{
phdr = (Elf32_Phdr *)
{
/* offset into file */
else
/* If the memory range contains the entry address, get the
physical address here. */
if (type == KERNEL_TYPE_MULTIBOOT
/* make sure we only load what we're supposed to! */
/* mark memory as used */
/* increment number of segments */
loaded++;
/* load the segment */
{
}
else
break;
}
}
if (! errnum)
{
if (! loaded)
else
{
/* Load ELF symbols. */
int symtab_err = 0;
/* We should align to a 4K boundary here for good measure. */
if (align_4k)
/*
* Should not need to call RAW_ADDR; cur_addr is already
* adjusted to account for grub_scratch_mem.
* XXX Linux might calculate cur_addr differently.
*/
== tab_size)
{
{
/* This section is a loaded section,
so we don't care. */
continue;
/* This section is empty, so we don't care. */
continue;
/* Align the section to a sh_addralign bits boundary. */
- (int) shdr[i].sh_addralign);
/*
* Should not need to call RAW_ADDR; cur_addr is already
* adjusted to account for grub_scratch_mem.
* XXX Linux might calculate cur_addr differently.
*/
== sec_size)))
{
symtab_err = 1;
break;
}
}
}
else
symtab_err = 1;
symtab_err = 1;
if (symtab_err)
{
printf ("(bad)");
cur_addr = 0;
}
else
}
}
}
if (! errnum)
{
/* If the entry address is physically different from that of the ELF
header, correct it here. */
if (real_entry_addr)
}
else
{
putchar ('\n');
}
grub_close ();
/* Sanity check. */
{
return KERNEL_TYPE_NONE;
}
return type;
}
int
{
int len;
/* if we are supposed to load on 4K boundaries */
return 0;
if (! len)
{
grub_close ();
return 0;
}
/* these two simply need to be set if any modules are loaded at all */
/* increment number of modules included */
mbi.mods_count++;
grub_close ();
return 1;
}
int
{
int len;
unsigned long moveto;
unsigned long max_addr;
#ifndef NO_DECOMPRESSION
no_decompression = 1;
#endif
goto fail;
if (! len)
{
grub_close ();
goto fail;
}
if (linux_mem_size)
else
/* XXX: Linux 2.3.xx has a bug in the memory range check, so avoid
the last page.
XXX: Linux 2.2.xx has a bug in the memory range check, which is
worse than that of Linux 2.3.xx, so avoid the last 64kb. *sigh* */
moveto -= 0x10000;
/* FIXME: Should check if the kernel supports INITRD. */
grub_close ();
fail:
#ifndef NO_DECOMPRESSION
no_decompression = 0;
#endif
return ! errnum;
}
#ifdef GRUB_UTIL
/* Dummy function to fake the *BSD boot. */
static void
{
stop ();
}
#endif
/*
* All "*_boot" commands depend on the images being loaded into memory
* correctly, the variables in this file being set up correctly, and
* the root partition being set in the 'saved_drive' and 'saved_partition'
* variables.
*/
void
{
char *str;
int clval = 0, i;
#ifdef GRUB_UTIL
#else
stop_floppy ();
#endif
while (*str)
{
if (*str == '-')
{
{
if (*str == 'C')
if (*str == 'a')
clval |= RB_ASKNAME;
if (*str == 'b')
if (*str == 'c')
if (*str == 'd')
if (*str == 'D')
clval |= RB_MULTIPLE;
if (*str == 'g')
if (*str == 'h')
if (*str == 'm')
if (*str == 'r')
clval |= RB_DFLTROOT;
if (*str == 's')
if (*str == 'v')
clval |= RB_VERBOSE;
str++;
}
continue;
}
str++;
}
if (type == KERNEL_TYPE_FREEBSD)
{
clval |= RB_BOOTINFO;
*arg = 0;
if (*arg == '/')
else
bi.bi_kernelname = 0;
bi.bi_nfs_diskless = 0;
for (i = 0; i < N_BIOS_GEOM; i++)
{
/* XXX Should check the return value. */
/* FIXME: If HEADS or SECTORS is greater than 255, then this will
break the geometry information. That is a drawback of BSD
but not of GRUB. */
}
{
}
#if 0
{
/* FIXME: Should check if a symbol table exists and, if exists,
pass the table to BI. */
}
#endif
else
{
bi.bi_esymtab = 0;
}
/* call entry point */
}
else
{
/*
* We now pass the various bootstrap parameters to the loaded
* image via the argument list.
*
* This is the official list:
*
* arg0 = 8 (magic)
* arg1 = boot flags
* arg2 = boot device
* arg3 = start of symbol table (0 if not loaded)
* arg4 = end of symbol table (0 if not loaded)
* arg5 = transfer address from image
* arg6 = transfer address for next image pointer
* arg7 = conventional memory size (640)
* arg8 = extended memory size (8196)
*
* ...in actuality, we just pass the parameters used by the kernel.
*/
/* call entry point */
unsigned long end_mark;
else
/* FIXME: it should be mbi.syms.e.size. */
end_mark = 0;
}
}