2N/A/* Load runtime image of EFIemu. Functions specific to 32/64-bit mode */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/err.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/efiemu/efiemu.h>
2N/A#include <grub/cpu/efiemu.h>
2N/A#include <grub/elf.h>
2N/A
2N/A/* ELF symbols and their values */
2N/Astatic struct grub_efiemu_elf_sym *grub_efiemu_elfsyms = 0;
2N/Astatic int grub_efiemu_nelfsyms = 0;
2N/A
2N/A/* Return the address of a section whose index is N. */
2N/Astatic grub_err_t
2N/Agrub_efiemu_get_section_addr (grub_efiemu_segment_t segs, unsigned n,
2N/A int *handle, grub_off_t *off)
2N/A{
2N/A grub_efiemu_segment_t seg;
2N/A
2N/A for (seg = segs; seg; seg = seg->next)
2N/A if (seg->section == n)
2N/A {
2N/A *handle = seg->handle;
2N/A *off = seg->off;
2N/A return GRUB_ERR_NONE;
2N/A }
2N/A
2N/A return grub_error (GRUB_ERR_BAD_OS, "section %d not found", n);
2N/A}
2N/A
2N/Agrub_err_t
2N/ASUFFIX (grub_efiemu_loadcore_unload) (void)
2N/A{
2N/A grub_free (grub_efiemu_elfsyms);
2N/A grub_efiemu_elfsyms = 0;
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/A/* Check if EHDR is a valid ELF header. */
2N/Aint
2N/ASUFFIX (grub_efiemu_check_header) (void *ehdr, grub_size_t size)
2N/A{
2N/A Elf_Ehdr *e = ehdr;
2N/A
2N/A /* Check the header size. */
2N/A if (size < sizeof (Elf_Ehdr))
2N/A return 0;
2N/A
2N/A /* Check the magic numbers. */
2N/A if (!SUFFIX (grub_arch_efiemu_check_header) (ehdr)
2N/A || e->e_ident[EI_MAG0] != ELFMAG0
2N/A || e->e_ident[EI_MAG1] != ELFMAG1
2N/A || e->e_ident[EI_MAG2] != ELFMAG2
2N/A || e->e_ident[EI_MAG3] != ELFMAG3
2N/A || e->e_ident[EI_VERSION] != EV_CURRENT
2N/A || e->e_version != EV_CURRENT)
2N/A return 0;
2N/A
2N/A return 1;
2N/A}
2N/A
2N/A/* Load all segments from memory specified by E. */
2N/Astatic grub_err_t
2N/Agrub_efiemu_load_segments (grub_efiemu_segment_t segs, const Elf_Ehdr *e)
2N/A{
2N/A Elf_Shdr *s;
2N/A grub_efiemu_segment_t cur;
2N/A
2N/A grub_dprintf ("efiemu", "loading segments\n");
2N/A
2N/A for (cur=segs; cur; cur = cur->next)
2N/A {
2N/A s = (Elf_Shdr *)cur->srcptr;
2N/A
2N/A if ((s->sh_flags & SHF_ALLOC) && s->sh_size)
2N/A {
2N/A void *addr;
2N/A
2N/A addr = (grub_uint8_t *) grub_efiemu_mm_obtain_request (cur->handle)
2N/A + cur->off;
2N/A
2N/A switch (s->sh_type)
2N/A {
2N/A case SHT_PROGBITS:
2N/A grub_memcpy (addr, (char *) e + s->sh_offset, s->sh_size);
2N/A break;
2N/A case SHT_NOBITS:
2N/A grub_memset (addr, 0, s->sh_size);
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/A/* Get a string at offset OFFSET from strtab */
2N/Astatic char *
2N/Agrub_efiemu_get_string (unsigned offset, const Elf_Ehdr *e)
2N/A{
2N/A unsigned i;
2N/A Elf_Shdr *s;
2N/A
2N/A for (i = 0, s = (Elf_Shdr *)((char *) e + e->e_shoff);
2N/A i < e->e_shnum;
2N/A i++, s = (Elf_Shdr *)((char *) s + e->e_shentsize))
2N/A if (s->sh_type == SHT_STRTAB && offset < s->sh_size)
2N/A return (char *) e + s->sh_offset + offset;
2N/A return 0;
2N/A}
2N/A
2N/A/* Request memory for segments and fill segments info */
2N/Astatic grub_err_t
2N/Agrub_efiemu_init_segments (grub_efiemu_segment_t *segs, const Elf_Ehdr *e)
2N/A{
2N/A unsigned i;
2N/A Elf_Shdr *s;
2N/A
2N/A for (i = 0, s = (Elf_Shdr *)((char *) e + e->e_shoff);
2N/A i < e->e_shnum;
2N/A i++, s = (Elf_Shdr *)((char *) s + e->e_shentsize))
2N/A {
2N/A if (s->sh_flags & SHF_ALLOC)
2N/A {
2N/A grub_efiemu_segment_t seg;
2N/A seg = (grub_efiemu_segment_t) grub_malloc (sizeof (*seg));
2N/A if (! seg)
2N/A return grub_errno;
2N/A
2N/A if (s->sh_size)
2N/A {
2N/A seg->handle
2N/A = grub_efiemu_request_memalign
2N/A (s->sh_addralign, s->sh_size,
2N/A s->sh_flags & SHF_EXECINSTR ? GRUB_EFI_RUNTIME_SERVICES_CODE
2N/A : GRUB_EFI_RUNTIME_SERVICES_DATA);
2N/A if (seg->handle < 0)
2N/A return grub_errno;
2N/A seg->off = 0;
2N/A }
2N/A
2N/A /*
2N/A .text-physical doesn't need to be relocated when switching to
2N/A virtual mode
2N/A */
2N/A if (!grub_strcmp (grub_efiemu_get_string (s->sh_name, e),
2N/A ".text-physical"))
2N/A seg->ptv_rel_needed = 0;
2N/A else
2N/A seg->ptv_rel_needed = 1;
2N/A seg->size = s->sh_size;
2N/A seg->section = i;
2N/A seg->next = *segs;
2N/A seg->srcptr = s;
2N/A *segs = seg;
2N/A }
2N/A }
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/A/* Count symbols and relocators and allocate/request memory for them */
2N/Astatic grub_err_t
2N/Agrub_efiemu_count_symbols (const Elf_Ehdr *e)
2N/A{
2N/A unsigned i;
2N/A Elf_Shdr *s;
2N/A int num = 0;
2N/A
2N/A /* Symbols */
2N/A for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
2N/A i < e->e_shnum;
2N/A i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
2N/A if (s->sh_type == SHT_SYMTAB)
2N/A break;
2N/A
2N/A if (i == e->e_shnum)
2N/A return grub_error (GRUB_ERR_BAD_OS, "no symbol table");
2N/A
2N/A grub_efiemu_nelfsyms = (unsigned) s->sh_size / (unsigned) s->sh_entsize;
2N/A grub_efiemu_elfsyms = (struct grub_efiemu_elf_sym *)
2N/A grub_malloc (sizeof (struct grub_efiemu_elf_sym) * grub_efiemu_nelfsyms);
2N/A
2N/A /* Relocators */
2N/A for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
2N/A i < e->e_shnum;
2N/A i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
2N/A if (s->sh_type == SHT_REL || s->sh_type == SHT_RELA)
2N/A num += ((unsigned) s->sh_size) / ((unsigned) s->sh_entsize);
2N/A
2N/A grub_efiemu_request_symbols (num);
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/A/* Fill grub_efiemu_elfsyms with symbol values */
2N/Astatic grub_err_t
2N/Agrub_efiemu_resolve_symbols (grub_efiemu_segment_t segs, Elf_Ehdr *e)
2N/A{
2N/A unsigned i;
2N/A Elf_Shdr *s;
2N/A Elf_Sym *sym;
2N/A const char *str;
2N/A Elf_Word size, entsize;
2N/A
2N/A grub_dprintf ("efiemu", "resolving symbols\n");
2N/A
2N/A for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
2N/A i < e->e_shnum;
2N/A i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
2N/A if (s->sh_type == SHT_SYMTAB)
2N/A break;
2N/A
2N/A if (i == e->e_shnum)
2N/A return grub_error (GRUB_ERR_BAD_OS, "no symbol table");
2N/A
2N/A sym = (Elf_Sym *) ((char *) e + s->sh_offset);
2N/A size = s->sh_size;
2N/A entsize = s->sh_entsize;
2N/A
2N/A s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shentsize * s->sh_link);
2N/A str = (char *) e + s->sh_offset;
2N/A
2N/A for (i = 0;
2N/A i < size / entsize;
2N/A i++, sym = (Elf_Sym *) ((char *) sym + entsize))
2N/A {
2N/A unsigned char type = ELF_ST_TYPE (sym->st_info);
2N/A unsigned char bind = ELF_ST_BIND (sym->st_info);
2N/A int handle;
2N/A grub_off_t off;
2N/A grub_err_t err;
2N/A const char *name = str + sym->st_name;
2N/A grub_efiemu_elfsyms[i].section = sym->st_shndx;
2N/A switch (type)
2N/A {
2N/A case STT_NOTYPE:
2N/A /* Resolve a global symbol. */
2N/A if (sym->st_name != 0 && sym->st_shndx == 0)
2N/A {
2N/A if ((err = grub_efiemu_resolve_symbol (name, &handle, &off)))
2N/A return err;
2N/A grub_efiemu_elfsyms[i].handle = handle;
2N/A grub_efiemu_elfsyms[i].off = off;
2N/A }
2N/A else
2N/A sym->st_value = 0;
2N/A break;
2N/A
2N/A case STT_OBJECT:
2N/A if ((err = grub_efiemu_get_section_addr
2N/A (segs, sym->st_shndx, &handle, &off)))
2N/A return err;
2N/A
2N/A off += sym->st_value;
2N/A if (bind != STB_LOCAL)
2N/A if ((err = grub_efiemu_register_symbol (name, handle, off)))
2N/A return err;
2N/A grub_efiemu_elfsyms[i].handle = handle;
2N/A grub_efiemu_elfsyms[i].off = off;
2N/A break;
2N/A
2N/A case STT_FUNC:
2N/A if ((err = grub_efiemu_get_section_addr
2N/A (segs, sym->st_shndx, &handle, &off)))
2N/A return err;
2N/A
2N/A off += sym->st_value;
2N/A if (bind != STB_LOCAL)
2N/A if ((err = grub_efiemu_register_symbol (name, handle, off)))
2N/A return err;
2N/A grub_efiemu_elfsyms[i].handle = handle;
2N/A grub_efiemu_elfsyms[i].off = off;
2N/A break;
2N/A
2N/A case STT_SECTION:
2N/A if ((err = grub_efiemu_get_section_addr
2N/A (segs, sym->st_shndx, &handle, &off)))
2N/A {
2N/A grub_efiemu_elfsyms[i].handle = 0;
2N/A grub_efiemu_elfsyms[i].off = 0;
2N/A grub_errno = GRUB_ERR_NONE;
2N/A break;
2N/A }
2N/A
2N/A grub_efiemu_elfsyms[i].handle = handle;
2N/A grub_efiemu_elfsyms[i].off = off;
2N/A break;
2N/A
2N/A case STT_FILE:
2N/A grub_efiemu_elfsyms[i].handle = 0;
2N/A grub_efiemu_elfsyms[i].off = 0;
2N/A break;
2N/A
2N/A default:
2N/A return grub_error (GRUB_ERR_BAD_MODULE,
2N/A "unknown symbol type `%d'", (int) type);
2N/A }
2N/A }
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/A/* Load runtime to the memory and request memory for definitive location*/
2N/Agrub_err_t
2N/ASUFFIX (grub_efiemu_loadcore_init) (void *core, grub_size_t core_size,
2N/A grub_efiemu_segment_t *segments)
2N/A{
2N/A Elf_Ehdr *e = (Elf_Ehdr *) core;
2N/A grub_err_t err;
2N/A
2N/A if (e->e_type != ET_REL)
2N/A return grub_error (GRUB_ERR_BAD_MODULE, "invalid ELF file type");
2N/A
2N/A /* Make sure that every section is within the core. */
2N/A if ((grub_size_t) core_size < e->e_shoff + e->e_shentsize * e->e_shnum)
2N/A return grub_error (GRUB_ERR_BAD_OS, "ELF sections outside core");
2N/A
2N/A if ((err = grub_efiemu_init_segments (segments, core)))
2N/A return err;
2N/A if ((err = grub_efiemu_count_symbols (core)))
2N/A return err;
2N/A
2N/A grub_efiemu_request_symbols (1);
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/A/* Load runtime definitively */
2N/Agrub_err_t
2N/ASUFFIX (grub_efiemu_loadcore_load) (void *core,
2N/A grub_size_t core_size
2N/A __attribute__ ((unused)),
2N/A grub_efiemu_segment_t segments)
2N/A{
2N/A grub_err_t err;
2N/A err = grub_efiemu_load_segments (segments, core);
2N/A if (err)
2N/A return err;
2N/A
2N/A err = grub_efiemu_resolve_symbols (segments, core);
2N/A if (err)
2N/A return err;
2N/A
2N/A err = SUFFIX (grub_arch_efiemu_relocate_symbols) (segments,
2N/A grub_efiemu_elfsyms,
2N/A core);
2N/A if (err)
2N/A return err;
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}