vaseboot/VasEBoot-core/loader/i386/pc/linux.c

495 lines
14 KiB
C

/* linux.c - boot Linux zImage or bzImage */
/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 Free Software Foundation, Inc.
*
* VAS_EBOOT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VAS_EBOOT 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 VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/loader.h>
#include <VasEBoot/file.h>
#include <VasEBoot/err.h>
#include <VasEBoot/device.h>
#include <VasEBoot/disk.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/types.h>
#include <VasEBoot/memory.h>
#include <VasEBoot/dl.h>
#include <VasEBoot/cpu/linux.h>
#include <VasEBoot/command.h>
#include <VasEBoot/i18n.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/cpu/relocator.h>
#include <VasEBoot/video.h>
#include <VasEBoot/i386/floppy.h>
#include <VasEBoot/lib/cmdline.h>
#include <VasEBoot/linux.h>
#include <VasEBoot/safemath.h>
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
#define VAS_EBOOT_LINUX_CL_OFFSET 0x9000
static VasEBoot_dl_t my_mod;
static VasEBoot_size_t linux_mem_size;
static int loaded;
static struct VasEBoot_relocator *relocator = NULL;
static VasEBoot_addr_t VasEBoot_linux_real_target;
static char *VasEBoot_linux_real_chunk;
static VasEBoot_size_t VasEBoot_linux16_prot_size;
static VasEBoot_size_t maximal_cmdline_size;
static VasEBoot_err_t
VasEBoot_linux16_boot (void)
{
VasEBoot_uint16_t segment;
struct VasEBoot_relocator16_state state = {0};
segment = VasEBoot_linux_real_target >> 4;
state.gs = state.fs = state.es = state.ds = state.ss = segment;
state.sp = VAS_EBOOT_LINUX_SETUP_STACK;
state.cs = segment + 0x20;
state.ip = 0;
state.a20 = 1;
VasEBoot_video_set_mode ("text", 0, 0);
VasEBoot_stop_floppy ();
return VasEBoot_relocator16_boot (relocator, state);
}
static VasEBoot_err_t
VasEBoot_linux_unload (void)
{
VasEBoot_dl_unref (my_mod);
loaded = 0;
VasEBoot_relocator_unload (relocator);
relocator = NULL;
return VAS_EBOOT_ERR_NONE;
}
static int
target_hook (VasEBoot_uint64_t addr, VasEBoot_uint64_t size, VasEBoot_memory_type_t type,
void *data)
{
VasEBoot_uint64_t *result = data;
VasEBoot_uint64_t candidate;
if (type != VAS_EBOOT_MEMORY_AVAILABLE)
return 0;
if (addr >= 0xa0000)
return 0;
if (addr + size >= 0xa0000)
size = 0xa0000 - addr;
/* Put the real mode part at as a high location as possible. */
candidate = addr + size - (VAS_EBOOT_LINUX_CL_OFFSET + maximal_cmdline_size);
/* But it must not exceed the traditional area. */
if (candidate > VAS_EBOOT_LINUX_OLD_REAL_MODE_ADDR)
candidate = VAS_EBOOT_LINUX_OLD_REAL_MODE_ADDR;
if (candidate < addr)
return 0;
if (candidate > *result || *result == (VasEBoot_uint64_t) -1)
*result = candidate;
return 0;
}
static VasEBoot_addr_t
VasEBoot_find_real_target (void)
{
VasEBoot_uint64_t result = (VasEBoot_uint64_t) -1;
VasEBoot_mmap_iterate (target_hook, &result);
return result;
}
static VasEBoot_err_t
VasEBoot_cmd_linux (VasEBoot_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
VasEBoot_file_t file = 0;
struct linux_i386_kernel_header lh;
VasEBoot_uint8_t setup_sects;
VasEBoot_size_t real_size;
VasEBoot_ssize_t len;
int i;
char *VasEBoot_linux_prot_chunk;
int VasEBoot_linux_is_bzimage;
VasEBoot_addr_t VasEBoot_linux_prot_target;
VasEBoot_err_t err;
VasEBoot_dl_ref (my_mod);
if (argc == 0)
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("filename expected"));
goto fail;
}
file = VasEBoot_file_open (argv[0], VAS_EBOOT_FILE_TYPE_LINUX_KERNEL);
if (! file)
goto fail;
if (VasEBoot_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
{
if (!VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"),
argv[0]);
goto fail;
}
if (lh.boot_flag != VasEBoot_cpu_to_le16_compile_time (0xaa55))
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "invalid magic number");
goto fail;
}
if (lh.setup_sects > VAS_EBOOT_LINUX_MAX_SETUP_SECTS)
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "too many setup sectors");
goto fail;
}
VasEBoot_linux_is_bzimage = 0;
setup_sects = lh.setup_sects;
linux_mem_size = 0;
maximal_cmdline_size = 256;
if (lh.header == VasEBoot_cpu_to_le32_compile_time (VAS_EBOOT_LINUX_I386_MAGIC_SIGNATURE)
&& VasEBoot_le_to_cpu16 (lh.version) >= 0x0200)
{
VasEBoot_linux_is_bzimage = (lh.loadflags & VAS_EBOOT_LINUX_FLAG_BIG_KERNEL);
lh.type_of_loader = VAS_EBOOT_LINUX_BOOT_LOADER_TYPE;
if (VasEBoot_le_to_cpu16 (lh.version) >= 0x0206)
maximal_cmdline_size = VasEBoot_le_to_cpu32 (lh.cmdline_size) + 1;
VasEBoot_linux_real_target = VasEBoot_find_real_target ();
if (VasEBoot_linux_real_target == (VasEBoot_addr_t)-1)
{
VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE,
"no appropriate low memory found");
goto fail;
}
if (VasEBoot_le_to_cpu16 (lh.version) >= 0x0201)
{
lh.heap_end_ptr = VasEBoot_cpu_to_le16_compile_time (VAS_EBOOT_LINUX_HEAP_END_OFFSET);
lh.loadflags |= VAS_EBOOT_LINUX_FLAG_CAN_USE_HEAP;
}
if (VasEBoot_le_to_cpu16 (lh.version) >= 0x0202)
lh.cmd_line_ptr = VasEBoot_linux_real_target + VAS_EBOOT_LINUX_CL_OFFSET;
else
{
lh.cl_magic = VasEBoot_cpu_to_le16_compile_time (VAS_EBOOT_LINUX_CL_MAGIC);
lh.cl_offset = VasEBoot_cpu_to_le16_compile_time (VAS_EBOOT_LINUX_CL_OFFSET);
lh.setup_move_size = VasEBoot_cpu_to_le16_compile_time (VAS_EBOOT_LINUX_CL_OFFSET
+ maximal_cmdline_size);
}
}
else
{
/* Your kernel is quite old... */
lh.cl_magic = VasEBoot_cpu_to_le16_compile_time (VAS_EBOOT_LINUX_CL_MAGIC);
lh.cl_offset = VasEBoot_cpu_to_le16_compile_time (VAS_EBOOT_LINUX_CL_OFFSET);
setup_sects = VAS_EBOOT_LINUX_DEFAULT_SETUP_SECTS;
VasEBoot_linux_real_target = VAS_EBOOT_LINUX_OLD_REAL_MODE_ADDR;
}
/* If SETUP_SECTS is not set, set it to the default (4). */
if (! setup_sects)
setup_sects = VAS_EBOOT_LINUX_DEFAULT_SETUP_SECTS;
real_size = setup_sects << VAS_EBOOT_DISK_SECTOR_BITS;
if (VasEBoot_sub (VasEBoot_file_size (file), real_size, &VasEBoot_linux16_prot_size) ||
VasEBoot_sub (VasEBoot_linux16_prot_size, VAS_EBOOT_DISK_SECTOR_SIZE, &VasEBoot_linux16_prot_size))
{
VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE, N_("overflow is detected"));
goto fail;
}
if (! VasEBoot_linux_is_bzimage
&& VAS_EBOOT_LINUX_ZIMAGE_ADDR + VasEBoot_linux16_prot_size
> VasEBoot_linux_real_target)
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "too big zImage (0x%" PRIxVAS_EBOOT_SIZE
" > 0x%" PRIxVAS_EBOOT_ADDR "), use bzImage instead",
VAS_EBOOT_LINUX_ZIMAGE_ADDR + VasEBoot_linux16_prot_size,
VasEBoot_linux_real_target);
goto fail;
}
VasEBoot_dprintf ("linux", "[Linux-%s, setup=0x%x, size=0x%x]\n",
VasEBoot_linux_is_bzimage ? "bzImage" : "zImage",
(unsigned) real_size,
(unsigned) VasEBoot_linux16_prot_size);
for (i = 1; i < argc; i++)
if (VasEBoot_memcmp (argv[i], "vga=", 4) == 0)
{
/* Video mode selection support. */
VasEBoot_uint16_t vid_mode;
char *val = argv[i] + 4;
if (VasEBoot_strcmp (val, "normal") == 0)
vid_mode = VAS_EBOOT_LINUX_VID_MODE_NORMAL;
else if (VasEBoot_strcmp (val, "ext") == 0)
vid_mode = VAS_EBOOT_LINUX_VID_MODE_EXTENDED;
else if (VasEBoot_strcmp (val, "ask") == 0)
vid_mode = VAS_EBOOT_LINUX_VID_MODE_ASK;
else
vid_mode = (VasEBoot_uint16_t) VasEBoot_strtoul (val, 0, 0);
if (VasEBoot_errno)
goto fail;
lh.vid_mode = VasEBoot_cpu_to_le16 (vid_mode);
}
else if (VasEBoot_memcmp (argv[i], "mem=", 4) == 0)
{
const char *val = argv[i] + 4;
linux_mem_size = VasEBoot_strtoul (val, &val, 0);
if (VasEBoot_errno)
{
VasEBoot_errno = VAS_EBOOT_ERR_NONE;
linux_mem_size = 0;
}
else
{
int shift = 0;
switch (VasEBoot_tolower (val[0]))
{
case 'g':
shift += 10;
/* Fallthrough. */
case 'm':
shift += 10;
/* Fallthrough. */
case 'k':
shift += 10;
/* Fallthrough. */
default:
break;
}
/* Check an overflow. */
if (linux_mem_size > (~0UL >> shift))
linux_mem_size = 0;
else
linux_mem_size <<= shift;
}
}
relocator = VasEBoot_relocator_new ();
if (!relocator)
goto fail;
{
VasEBoot_relocator_chunk_t ch;
err = VasEBoot_relocator_alloc_chunk_addr (relocator, &ch,
VasEBoot_linux_real_target,
VAS_EBOOT_LINUX_CL_OFFSET
+ maximal_cmdline_size);
if (err)
return err;
VasEBoot_linux_real_chunk = get_virtual_current_address (ch);
}
/* Put the real mode code at the temporary address. */
VasEBoot_memmove (VasEBoot_linux_real_chunk, &lh, sizeof (lh));
len = real_size + VAS_EBOOT_DISK_SECTOR_SIZE - sizeof (lh);
if (VasEBoot_file_read (file, VasEBoot_linux_real_chunk + sizeof (lh), len) != len)
{
if (!VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"),
argv[0]);
goto fail;
}
if (lh.header != VasEBoot_cpu_to_le32_compile_time (VAS_EBOOT_LINUX_I386_MAGIC_SIGNATURE)
|| VasEBoot_le_to_cpu16 (lh.version) < 0x0200)
/* Clear the heap space. */
VasEBoot_memset (VasEBoot_linux_real_chunk
+ ((setup_sects + 1) << VAS_EBOOT_DISK_SECTOR_BITS),
0,
((VAS_EBOOT_LINUX_MAX_SETUP_SECTS - setup_sects - 1)
<< VAS_EBOOT_DISK_SECTOR_BITS));
/* Create kernel command line. */
VasEBoot_memcpy ((char *)VasEBoot_linux_real_chunk + VAS_EBOOT_LINUX_CL_OFFSET,
LINUX_IMAGE, sizeof (LINUX_IMAGE));
err = VasEBoot_create_loader_cmdline (argc, argv,
(char *)VasEBoot_linux_real_chunk
+ VAS_EBOOT_LINUX_CL_OFFSET + sizeof (LINUX_IMAGE) - 1,
maximal_cmdline_size
- (sizeof (LINUX_IMAGE) - 1),
VAS_EBOOT_VERIFY_KERNEL_CMDLINE);
if (err)
goto fail;
if (VasEBoot_linux_is_bzimage)
VasEBoot_linux_prot_target = VAS_EBOOT_LINUX_BZIMAGE_ADDR;
else
VasEBoot_linux_prot_target = VAS_EBOOT_LINUX_ZIMAGE_ADDR;
{
VasEBoot_relocator_chunk_t ch;
err = VasEBoot_relocator_alloc_chunk_addr (relocator, &ch,
VasEBoot_linux_prot_target,
VasEBoot_linux16_prot_size);
if (err)
return err;
VasEBoot_linux_prot_chunk = get_virtual_current_address (ch);
}
len = VasEBoot_linux16_prot_size;
if (VasEBoot_file_read (file, VasEBoot_linux_prot_chunk, len) != len && !VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"),
argv[0]);
if (VasEBoot_errno == VAS_EBOOT_ERR_NONE)
{
VasEBoot_loader_set (VasEBoot_linux16_boot, VasEBoot_linux_unload, 0);
loaded = 1;
}
fail:
if (file)
VasEBoot_file_close (file);
if (VasEBoot_errno != VAS_EBOOT_ERR_NONE)
{
VasEBoot_dl_unref (my_mod);
loaded = 0;
VasEBoot_relocator_unload (relocator);
}
return VasEBoot_errno;
}
static VasEBoot_err_t
VasEBoot_cmd_initrd (VasEBoot_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
VasEBoot_size_t size = 0;
VasEBoot_addr_t addr_max, addr_min;
struct linux_i386_kernel_header *lh;
VasEBoot_uint8_t *initrd_chunk;
VasEBoot_addr_t initrd_addr;
VasEBoot_err_t err;
struct VasEBoot_linux_initrd_context initrd_ctx = { 0, 0, 0 };
if (argc == 0)
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("filename expected"));
goto fail;
}
if (!loaded)
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("you need to load the kernel first"));
goto fail;
}
lh = (struct linux_i386_kernel_header *) VasEBoot_linux_real_chunk;
if (!(lh->header == VasEBoot_cpu_to_le32_compile_time (VAS_EBOOT_LINUX_I386_MAGIC_SIGNATURE)
&& VasEBoot_le_to_cpu16 (lh->version) >= 0x0200))
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "the kernel is too old for initrd");
goto fail;
}
/* Get the highest address available for the initrd. */
if (VasEBoot_le_to_cpu16 (lh->version) >= 0x0203)
{
addr_max = VasEBoot_cpu_to_le32 (lh->initrd_addr_max);
/* XXX in reality, Linux specifies a bogus value, so
it is necessary to make sure that ADDR_MAX does not exceed
0x3fffffff. */
if (addr_max > VAS_EBOOT_LINUX_INITRD_MAX_ADDRESS)
addr_max = VAS_EBOOT_LINUX_INITRD_MAX_ADDRESS;
}
else
addr_max = VAS_EBOOT_LINUX_INITRD_MAX_ADDRESS;
if (linux_mem_size != 0 && linux_mem_size < addr_max)
addr_max = linux_mem_size;
/* Linux 2.3.xx has a bug in the memory range check, so avoid
the last page.
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. */
addr_max -= 0x10000;
addr_min = VAS_EBOOT_LINUX_BZIMAGE_ADDR + VasEBoot_linux16_prot_size;
if (VasEBoot_initrd_init (argc, argv, &initrd_ctx))
goto fail;
size = VasEBoot_get_initrd_size (&initrd_ctx);
{
VasEBoot_relocator_chunk_t ch;
err = VasEBoot_relocator_alloc_chunk_align_safe (relocator, &ch, addr_min, addr_max, size,
0x1000, VAS_EBOOT_RELOCATOR_PREFERENCE_HIGH, 0);
if (err)
goto fail;
initrd_chunk = get_virtual_current_address (ch);
initrd_addr = get_physical_target_address (ch);
}
if (VasEBoot_initrd_load (&initrd_ctx, initrd_chunk))
goto fail;
lh->ramdisk_image = initrd_addr;
lh->ramdisk_size = size;
fail:
VasEBoot_initrd_close (&initrd_ctx);
return VasEBoot_errno;
}
static VasEBoot_command_t cmd_linux, cmd_initrd;
VAS_EBOOT_MOD_INIT(linux16)
{
cmd_linux =
VasEBoot_register_command ("linux16", VasEBoot_cmd_linux,
0, N_("Load Linux."));
cmd_initrd =
VasEBoot_register_command ("initrd16", VasEBoot_cmd_initrd,
0, N_("Load initrd."));
my_mod = mod;
}
VAS_EBOOT_MOD_FINI(linux16)
{
VasEBoot_unregister_command (cmd_linux);
VasEBoot_unregister_command (cmd_initrd);
}