Port linux16 to relocator framework

This commit is contained in:
Vladimir 'phcoder' Serbinenko 2010-01-12 22:17:26 +01:00
parent 14dacc815a
commit c911e8791a
2 changed files with 93 additions and 88 deletions

View file

@ -1,37 +0,0 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2002,2003,2004,2007,2008,2009 Free Software Foundation, Inc.
*
* GRUB 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.
*
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GRUB_LOADER_CPU_HEADER
#define GRUB_LOADER_CPU_HEADER 1
#include <grub/types.h>
#include <grub/err.h>
#include <grub/symbol.h>
extern grub_addr_t EXPORT_VAR(grub_os_area_addr);
extern grub_size_t EXPORT_VAR(grub_os_area_size);
#ifdef GRUB_MACHINE_PCBIOS
extern grub_uint32_t EXPORT_VAR(grub_linux_prot_size);
extern char *EXPORT_VAR(grub_linux_tmp_addr);
extern char *EXPORT_VAR(grub_linux_real_addr);
extern grub_int32_t EXPORT_VAR(grub_linux_is_bzimage);
grub_err_t EXPORT_FUNC(grub_linux16_boot) (void);
#endif
#endif /* ! GRUB_LOADER_CPU_HEADER */

View file

@ -31,6 +31,8 @@
#include <grub/cpu/linux.h>
#include <grub/command.h>
#include <grub/i18n.h>
#include <grub/mm.h>
#include <grub/cpu/relocator.h>
#define GRUB_LINUX_CL_OFFSET 0x9000
#define GRUB_LINUX_CL_END_OFFSET 0x90FF
@ -39,12 +41,34 @@ static grub_dl_t my_mod;
static grub_size_t linux_mem_size;
static int loaded;
static struct grub_relocator *relocator = NULL;
static grub_addr_t grub_linux_real_target;
static char *grub_linux_real_chunk;
static grub_size_t grub_linux16_prot_size;
static grub_err_t
grub_linux16_boot (void)
{
grub_uint16_t segment;
struct grub_relocator16_state state;
segment = grub_linux_real_target >> 4;
state.gs = state.fs = state.es = state.ds = state.ss = segment;
state.sp = GRUB_LINUX_SETUP_STACK;
state.cs = segment + 0x20;
state.ip = 0;
return grub_relocator16_boot (relocator, state);
}
static grub_err_t
grub_linux_unload (void)
{
grub_dl_unref (my_mod);
loaded = 0;
grub_relocator_unload (relocator);
relocator = NULL;
return GRUB_ERR_NONE;
}
@ -55,10 +79,14 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
grub_file_t file = 0;
struct linux_kernel_header lh;
grub_uint8_t setup_sects;
grub_size_t real_size, prot_size;
grub_size_t real_size;
grub_ssize_t len;
int i;
char *dest;
char *grub_linux_prot_chunk;
int grub_linux_is_bzimage;
grub_addr_t grub_linux_prot_target;
grub_err_t err;
grub_dl_ref (my_mod);
@ -72,14 +100,6 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
if (! file)
goto fail;
if ((grub_size_t) grub_file_size (file) > grub_os_area_size)
{
grub_error (GRUB_ERR_OUT_OF_RANGE, "too big kernel (0x%x > 0x%x)",
(grub_size_t) grub_file_size (file),
grub_os_area_size);
goto fail;
}
if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
{
grub_error (GRUB_ERR_READ_ERROR, "cannot read the Linux header");
@ -109,12 +129,11 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
lh.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
/* Put the real mode part at as a high location as possible. */
grub_linux_real_addr
= (char *) UINT_TO_PTR (grub_mmap_get_lower ()
- GRUB_LINUX_SETUP_MOVE_SIZE);
grub_linux_real_target = grub_mmap_get_lower ()
- GRUB_LINUX_SETUP_MOVE_SIZE;
/* But it must not exceed the traditional area. */
if (grub_linux_real_addr > (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR)
grub_linux_real_addr = (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR;
if (grub_linux_real_target > GRUB_LINUX_OLD_REAL_MODE_ADDR)
grub_linux_real_target = GRUB_LINUX_OLD_REAL_MODE_ADDR;
if (grub_le_to_cpu16 (lh.version) >= 0x0201)
{
@ -123,7 +142,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
}
if (grub_le_to_cpu16 (lh.version) >= 0x0202)
lh.cmd_line_ptr = grub_linux_real_addr + GRUB_LINUX_CL_OFFSET;
lh.cmd_line_ptr = grub_linux_real_target + GRUB_LINUX_CL_OFFSET;
else
{
lh.cl_magic = grub_cpu_to_le16 (GRUB_LINUX_CL_MAGIC);
@ -139,7 +158,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
grub_linux_real_addr = (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR;
grub_linux_real_target = GRUB_LINUX_OLD_REAL_MODE_ADDR;
}
/* If SETUP_SECTS is not set, set it to the default (4). */
@ -147,31 +166,36 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE;
grub_linux_tmp_addr = (char *) GRUB_LINUX_BZIMAGE_ADDR + prot_size;
grub_linux16_prot_size = grub_file_size (file)
- real_size - GRUB_DISK_SECTOR_SIZE;
if (! grub_linux_is_bzimage
&& ((char *) GRUB_LINUX_ZIMAGE_ADDR + prot_size > grub_linux_real_addr))
&& GRUB_LINUX_ZIMAGE_ADDR + grub_linux16_prot_size
> grub_linux_real_target)
{
grub_error (GRUB_ERR_BAD_OS, "too big zImage (0x%x > 0x%x), use bzImage instead",
(char *) GRUB_LINUX_ZIMAGE_ADDR + prot_size,
(grub_size_t) grub_linux_real_addr);
(char *) GRUB_LINUX_ZIMAGE_ADDR + grub_linux16_prot_size,
(grub_size_t) grub_linux_real_target);
goto fail;
}
if (grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE
> (char *) UINT_TO_PTR (grub_mmap_get_lower ()))
if (grub_linux_real_target + GRUB_LINUX_SETUP_MOVE_SIZE
> grub_mmap_get_lower ())
{
grub_error (GRUB_ERR_OUT_OF_RANGE,
"too small lower memory (0x%x > 0x%x)",
grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE,
grub_linux_real_target + GRUB_LINUX_SETUP_MOVE_SIZE,
(int) grub_mmap_get_lower ());
goto fail;
}
grub_printf (" [Linux-%s, setup=0x%x, size=0x%x]\n",
grub_linux_is_bzimage ? "bzImage" : "zImage", real_size, prot_size);
grub_linux_is_bzimage ? "bzImage" : "zImage", real_size,
grub_linux16_prot_size);
relocator = grub_relocator_new ();
if (!relocator)
goto fail;
for (i = 1; i < argc; i++)
if (grub_memcmp (argv[i], "vga=", 4) == 0)
@ -229,11 +253,18 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
}
}
err = grub_relocator_alloc_chunk_addr (relocator, (void **)
&grub_linux_real_chunk,
grub_linux_real_target,
GRUB_LINUX_SETUP_MOVE_SIZE);
if (err)
return err;
/* Put the real mode code at the temporary address. */
grub_memmove (grub_linux_tmp_addr, &lh, sizeof (lh));
grub_memmove (grub_linux_real_chunk, &lh, sizeof (lh));
len = real_size + GRUB_DISK_SECTOR_SIZE - sizeof (lh);
if (grub_file_read (file, grub_linux_tmp_addr + sizeof (lh), len) != len)
if (grub_file_read (file, grub_linux_real_chunk + sizeof (lh), len) != len)
{
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
goto fail;
@ -242,21 +273,21 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
if (lh.header != grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
|| grub_le_to_cpu16 (lh.version) < 0x0200)
/* Clear the heap space. */
grub_memset (grub_linux_tmp_addr
grub_memset (grub_linux_real_chunk
+ ((setup_sects + 1) << GRUB_DISK_SECTOR_BITS),
0,
((GRUB_LINUX_MAX_SETUP_SECTS - setup_sects - 1)
<< GRUB_DISK_SECTOR_BITS));
/* Specify the boot file. */
dest = grub_stpcpy (grub_linux_tmp_addr + GRUB_LINUX_CL_OFFSET,
dest = grub_stpcpy (grub_linux_real_chunk + GRUB_LINUX_CL_OFFSET,
"BOOT_IMAGE=");
dest = grub_stpcpy (dest, argv[0]);
/* Copy kernel parameters. */
for (i = 1;
i < argc
&& dest + grub_strlen (argv[i]) + 1 < (grub_linux_tmp_addr
&& dest + grub_strlen (argv[i]) + 1 < (grub_linux_real_chunk
+ GRUB_LINUX_CL_END_OFFSET);
i++)
{
@ -264,14 +295,25 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
dest = grub_stpcpy (dest, argv[i]);
}
len = prot_size;
if (grub_file_read (file, (void *) GRUB_LINUX_BZIMAGE_ADDR, len) != len)
if (grub_linux_is_bzimage)
grub_linux_prot_target = GRUB_LINUX_BZIMAGE_ADDR;
else
grub_linux_prot_target = GRUB_LINUX_ZIMAGE_ADDR;
err = grub_relocator_alloc_chunk_addr (relocator,
(void **) &grub_linux_prot_chunk,
grub_linux_prot_target,
grub_linux16_prot_size);
if (err)
return err;
len = grub_linux16_prot_size;
if (grub_file_read (file, grub_linux_prot_chunk, grub_linux16_prot_size)
!= (grub_ssize_t) grub_linux16_prot_size)
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
if (grub_errno == GRUB_ERR_NONE)
{
grub_linux_prot_size = prot_size;
grub_loader_set (grub_linux16_boot, grub_linux_unload, 1);
grub_loader_set (grub_linux16_boot, grub_linux_unload, 0);
loaded = 1;
}
@ -284,6 +326,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
{
grub_dl_unref (my_mod);
loaded = 0;
grub_relocator_unload (relocator);
}
return grub_errno;
@ -295,8 +338,11 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
{
grub_file_t file = 0;
grub_ssize_t size;
grub_addr_t addr_max, addr_min, addr;
grub_addr_t addr_max, addr_min;
struct linux_kernel_header *lh;
grub_uint8_t *initrd_chunk;
grub_addr_t initrd_addr;
grub_err_t err;
if (argc == 0)
{
@ -310,7 +356,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
goto fail;
}
lh = (struct linux_kernel_header *) grub_linux_tmp_addr;
lh = (struct linux_kernel_header *) grub_linux_real_chunk;
if (!(lh->header == grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
&& grub_le_to_cpu16 (lh->version) >= 0x0200))
@ -342,10 +388,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
worse than that of Linux 2.3.xx, so avoid the last 64kb. */
addr_max -= 0x10000;
if (addr_max > grub_os_area_addr + grub_os_area_size)
addr_max = grub_os_area_addr + grub_os_area_size;
addr_min = (grub_addr_t) grub_linux_tmp_addr + GRUB_LINUX_CL_END_OFFSET;
addr_min = GRUB_LINUX_BZIMAGE_ADDR + grub_linux16_prot_size;
file = grub_file_open (argv[0]);
if (!file)
@ -353,22 +396,21 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
size = grub_file_size (file);
/* Put the initrd as high as possible, 4KiB aligned. */
addr = (addr_max - size) & ~0xFFF;
err = grub_relocator_alloc_chunk_align (relocator, (void **) &initrd_chunk,
&initrd_addr,
addr_min, addr_max - size,
size, 0x1000,
GRUB_RELOCATOR_PREFERENCE_HIGH);
if (err)
return err;
if (addr < addr_min)
{
grub_error (GRUB_ERR_OUT_OF_RANGE, "the initrd is too big");
goto fail;
}
if (grub_file_read (file, (void *) addr, size) != size)
if (grub_file_read (file, initrd_chunk, size) != size)
{
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
goto fail;
}
lh->ramdisk_image = addr;
lh->ramdisk_image = initrd_addr;
lh->ramdisk_size = size;
fail: