2006-11-03 Hollis Blanchard <hollis@penguinppc.org>
* conf/i386-efi.rmk (grub_emu_SOURCES): Add kern/elf.c. * conf/i386-pc.rmk (grub_emu_SOURCES): Likewise. * conf/powerpc-ieee1275.rmk (grub_emu_SOURCES): Likewise. (kernel_elf_SOURCES): Likewise. * conf/i386-efi.rmk (kernel_mod_HEADERS): Add elfload.h and cache.h. * conf/i386-pc.rmk (kernel_mod_HEADERS): Likewise. * conf/powerpc-ieee1275.rmk (kernel_elf_HEADERS): Likewise. * conf/sparc64-ieee1275.rmk (kernel_elf_HEADERS): Likewise. * conf/common.rmk (pkgdata_MODULES): Add elf.mod. (elf_mod_SOURCES): New variable. (elf_mod_CFLAGS): Likewise. (elf_mod_LDFLAGS): Likewise. * include/grub/types.h (__unused): New macro. * include/grub/elfload.h: New file. * kern/elf.c: Likewise. * loader/powerpc/ieee1275/linux.c: Include elfload.h. (ELF32_LOADMASK): New macro. (ELF64_LOADMASK): Likewise. (vmlinux): Removed. (grub_linux_load32): New function. (grub_linux_load64): Likewise. (grub_rescue_cmd_linux): Call grub_linux_load32 or grub_linux_load64. Use grub_elf_t instead of grub_file_t.
This commit is contained in:
parent
a09d5aa53e
commit
2166cc8349
13 changed files with 299 additions and 184 deletions
|
@ -19,6 +19,7 @@
|
|||
*/
|
||||
|
||||
#include <grub/elf.h>
|
||||
#include <grub/elfload.h>
|
||||
#include <grub/loader.h>
|
||||
#include <grub/dl.h>
|
||||
#include <grub/mm.h>
|
||||
|
@ -27,10 +28,12 @@
|
|||
#include <grub/ieee1275/ieee1275.h>
|
||||
#include <grub/machine/loader.h>
|
||||
|
||||
#define ELF32_LOADMASK (0xc0000000UL)
|
||||
#define ELF64_LOADMASK (0xc000000000000000ULL)
|
||||
|
||||
static grub_dl_t my_mod;
|
||||
|
||||
static int loaded;
|
||||
static int vmlinux;
|
||||
|
||||
static grub_addr_t initrd_addr;
|
||||
static grub_size_t initrd_size;
|
||||
|
@ -97,16 +100,97 @@ grub_linux_unload (void)
|
|||
return err;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_linux_load32 (grub_elf_t elf)
|
||||
{
|
||||
Elf32_Addr entry;
|
||||
int found_addr = 0;
|
||||
|
||||
/* Linux's entry point incorrectly contains a virtual address. */
|
||||
entry = elf->ehdr.ehdr32.e_entry & ~ELF32_LOADMASK;
|
||||
if (entry == 0)
|
||||
entry = 0x01400000;
|
||||
|
||||
linux_size = grub_elf32_size (elf);
|
||||
if (linux_size == 0)
|
||||
return grub_errno;
|
||||
/* Pad it; the kernel scribbles over memory beyond its load address. */
|
||||
linux_size += 0x100000;
|
||||
|
||||
/* On some systems, firmware occupies the memory we're trying to use.
|
||||
* Happily, Linux can be loaded anywhere (it relocates itself). Iterate
|
||||
* until we find an open area. */
|
||||
for (linux_addr = entry; linux_addr < entry + 200 * 0x100000; linux_addr += 0x100000)
|
||||
{
|
||||
grub_dprintf ("loader", "Attempting to claim at 0x%x, size 0x%x.\n",
|
||||
linux_addr, linux_size);
|
||||
found_addr = grub_claimmap (linux_addr, linux_size);
|
||||
if (found_addr != -1)
|
||||
break;
|
||||
}
|
||||
if (found_addr == -1)
|
||||
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "Could not claim memory.");
|
||||
|
||||
/* Now load the segments into the area we claimed. */
|
||||
auto int offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr);
|
||||
int offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr)
|
||||
{
|
||||
/* Linux's program headers incorrectly contain virtual addresses.
|
||||
* Translate those to physical, and offset to the area we claimed. */
|
||||
*addr = (phdr->p_paddr & ~ELF32_LOADMASK) + linux_addr;
|
||||
return 0;
|
||||
}
|
||||
return grub_elf32_load (elf, offset_phdr);
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_linux_load64 (grub_elf_t elf)
|
||||
{
|
||||
Elf64_Addr entry;
|
||||
int found_addr = 0;
|
||||
|
||||
/* Linux's entry point incorrectly contains a virtual address. */
|
||||
entry = elf->ehdr.ehdr64.e_entry & ~ELF64_LOADMASK;
|
||||
if (entry == 0)
|
||||
entry = 0x01400000;
|
||||
|
||||
linux_size = grub_elf64_size (elf);
|
||||
if (linux_size == 0)
|
||||
return grub_errno;
|
||||
/* Pad it; the kernel scribbles over memory beyond its load address. */
|
||||
linux_size += 0x100000;
|
||||
|
||||
/* On some systems, firmware occupies the memory we're trying to use.
|
||||
* Happily, Linux can be loaded anywhere (it relocates itself). Iterate
|
||||
* until we find an open area. */
|
||||
for (linux_addr = entry; linux_addr < entry + 200 * 0x100000; linux_addr += 0x100000)
|
||||
{
|
||||
grub_dprintf ("loader", "Attempting to claim at 0x%x, size 0x%x.\n",
|
||||
linux_addr, linux_size);
|
||||
found_addr = grub_claimmap (linux_addr, linux_size);
|
||||
if (found_addr != -1)
|
||||
break;
|
||||
}
|
||||
if (found_addr == -1)
|
||||
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "Could not claim memory.");
|
||||
|
||||
/* Now load the segments into the area we claimed. */
|
||||
auto int offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr);
|
||||
int offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr)
|
||||
{
|
||||
/* Linux's program headers incorrectly contain virtual addresses.
|
||||
* Translate those to physical, and offset to the area we claimed. */
|
||||
*addr = (phdr->p_paddr & ~ELF64_LOADMASK) + linux_addr;
|
||||
return 0;
|
||||
}
|
||||
return grub_elf64_load (elf, offset_phdr);
|
||||
}
|
||||
|
||||
void
|
||||
grub_rescue_cmd_linux (int argc, char *argv[])
|
||||
{
|
||||
grub_file_t file = 0;
|
||||
Elf32_Ehdr ehdr;
|
||||
Elf32_Phdr *phdrs = 0;
|
||||
grub_elf_t elf = 0;
|
||||
int i;
|
||||
int offset = 0;
|
||||
grub_addr_t entry;
|
||||
int found_addr = 0;
|
||||
int size;
|
||||
char *dest;
|
||||
|
||||
|
@ -115,106 +199,32 @@ grub_rescue_cmd_linux (int argc, char *argv[])
|
|||
if (argc == 0)
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
|
||||
goto fail;
|
||||
goto out;
|
||||
}
|
||||
|
||||
file = grub_file_open (argv[0]);
|
||||
if (! file)
|
||||
goto fail;
|
||||
elf = grub_elf_open (argv[0]);
|
||||
if (! elf)
|
||||
goto out;
|
||||
|
||||
if (grub_file_read (file, (char *) &ehdr, sizeof (ehdr)) != sizeof (ehdr))
|
||||
{
|
||||
grub_error (GRUB_ERR_READ_ERROR, "cannot read the linux elf header");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (grub_dl_check_header (&ehdr, sizeof(ehdr)))
|
||||
{
|
||||
grub_error (GRUB_ERR_UNKNOWN_OS, "No valid ELF header found");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (ehdr.e_type != ET_EXEC)
|
||||
if (elf->ehdr.ehdr32.e_type != ET_EXEC)
|
||||
{
|
||||
grub_error (GRUB_ERR_UNKNOWN_OS,
|
||||
"This ELF file is not of the right type\n");
|
||||
goto fail;
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* Read the sections. */
|
||||
entry = ehdr.e_entry;
|
||||
if (entry == 0xc0000000)
|
||||
{
|
||||
entry = 0x01400000;
|
||||
vmlinux = 1;
|
||||
}
|
||||
else
|
||||
vmlinux = 0;
|
||||
|
||||
phdrs = (Elf32_Phdr *) grub_malloc (ehdr.e_phnum * ehdr.e_phentsize);
|
||||
grub_file_read (file, (void *) phdrs, ehdr.e_phnum * ehdr.e_phentsize);
|
||||
|
||||
/* Release the previously used memory. */
|
||||
grub_loader_unset ();
|
||||
|
||||
/* Determine the amount of memory that is required. */
|
||||
linux_size = 0;
|
||||
for (i = 0; i < ehdr.e_phnum; i++)
|
||||
if (grub_elf_is_elf32 (elf))
|
||||
grub_linux_load32 (elf);
|
||||
else
|
||||
if (grub_elf_is_elf64 (elf))
|
||||
grub_linux_load64 (elf);
|
||||
else
|
||||
{
|
||||
Elf32_Phdr *phdr = phdrs + i;
|
||||
/* XXX: Is this calculation correct? */
|
||||
linux_size += phdr->p_memsz + phdr->p_filesz;
|
||||
}
|
||||
|
||||
/* Reserve memory for the kernel. */
|
||||
linux_size += 0x100000;
|
||||
|
||||
/* For some vmlinux kernels the address set above won't work. Just
|
||||
try some other addresses just like yaboot does. */
|
||||
for (linux_addr = entry; linux_addr < entry + 200 * 0x100000; linux_addr += 0x100000)
|
||||
{
|
||||
grub_dprintf ("loader", "Attempting to claim at 0x%x, size 0x%x.\n",
|
||||
linux_addr, linux_size);
|
||||
found_addr = grub_claimmap (linux_addr, linux_size);
|
||||
if (found_addr != -1)
|
||||
break;
|
||||
}
|
||||
|
||||
if (found_addr == -1)
|
||||
{
|
||||
grub_error (GRUB_ERR_OUT_OF_MEMORY, "Can not claim memory");
|
||||
goto fail;
|
||||
}
|
||||
entry = linux_addr;
|
||||
|
||||
/* Load every loadable segment in memory. */
|
||||
for (i = 0; i < ehdr.e_phnum; i++)
|
||||
{
|
||||
Elf32_Phdr *phdr = phdrs + i;
|
||||
|
||||
if (phdr->p_type == PT_LOAD)
|
||||
{
|
||||
void *segment_addr = ((char *) entry) + offset;
|
||||
|
||||
if (grub_file_seek (file, phdr->p_offset) == (grub_off_t) -1)
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_OS, "Invalid offset in program header");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
grub_dprintf ("loader", "Loading segment %d at %p, size 0x%x\n", i,
|
||||
segment_addr, phdr->p_filesz);
|
||||
|
||||
if (grub_file_read (file, segment_addr, phdr->p_filesz)
|
||||
!= (grub_ssize_t) phdr->p_filesz)
|
||||
goto fail;
|
||||
|
||||
if (phdr->p_filesz < phdr->p_memsz)
|
||||
grub_memset ((char *) (((char *) entry) + offset) + phdr->p_filesz, 0,
|
||||
phdr->p_memsz - phdr->p_filesz);
|
||||
|
||||
offset += phdr->p_filesz;
|
||||
}
|
||||
grub_error (GRUB_ERR_BAD_FILE_TYPE, "Unknown ELF class");
|
||||
goto out;
|
||||
}
|
||||
|
||||
size = sizeof ("BOOT_IMAGE=") + grub_strlen (argv[0]);
|
||||
|
@ -223,7 +233,7 @@ grub_rescue_cmd_linux (int argc, char *argv[])
|
|||
|
||||
linux_args = grub_malloc (size);
|
||||
if (! linux_args)
|
||||
goto fail;
|
||||
goto out;
|
||||
|
||||
/* Specify the boot file. */
|
||||
dest = grub_stpcpy (linux_args, "BOOT_IMAGE=");
|
||||
|
@ -235,12 +245,10 @@ grub_rescue_cmd_linux (int argc, char *argv[])
|
|||
dest = grub_stpcpy (dest, argv[i]);
|
||||
}
|
||||
|
||||
fail:
|
||||
out:
|
||||
|
||||
if (file)
|
||||
grub_file_close (file);
|
||||
|
||||
grub_free (phdrs);
|
||||
if (elf)
|
||||
grub_elf_close (elf);
|
||||
|
||||
if (grub_errno != GRUB_ERR_NONE)
|
||||
{
|
||||
|
@ -254,8 +262,6 @@ grub_rescue_cmd_linux (int argc, char *argv[])
|
|||
initrd_addr = 0;
|
||||
loaded = 1;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue