multiboot_elfxx.c 7.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 1999,2000,2001,2002,2003,2004,2005,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/>.
 */

#if defined(MULTIBOOT_LOAD_ELF32)
# define XX		32
21
# define E_MACHINE	MULTIBOOT_ELF32_MACHINE
22 23 24
# define ELFCLASSXX	ELFCLASS32
# define Elf_Ehdr	Elf32_Ehdr
# define Elf_Phdr	Elf32_Phdr
25
# define Elf_Shdr	Elf32_Shdr
26 27
#elif defined(MULTIBOOT_LOAD_ELF64)
# define XX		64
28
# define E_MACHINE	MULTIBOOT_ELF64_MACHINE
29 30 31
# define ELFCLASSXX	ELFCLASS64
# define Elf_Ehdr	Elf64_Ehdr
# define Elf_Phdr	Elf64_Phdr
32
# define Elf_Shdr	Elf64_Shdr
33 34 35 36
#else
#error "I'm confused"
#endif

37 38
#include <grub/i386/relocator.h>

39 40 41
#define CONCAT(a,b)	CONCAT_(a, b)
#define CONCAT_(a,b)	a ## b

42 43
#pragma GCC diagnostic ignored "-Wcast-align"

44 45 46 47 48 49 50 51 52 53
/* Check if BUFFER contains ELF32 (or ELF64).  */
static int
CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
{
  Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;

  return ehdr->e_ident[EI_CLASS] == ELFCLASSXX;
}

static grub_err_t
54
CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, void *buffer)
55 56 57 58 59 60 61 62 63
{
  Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
  char *phdr_base;
  int i;

  if (ehdr->e_ident[EI_MAG0] != ELFMAG0
      || ehdr->e_ident[EI_MAG1] != ELFMAG1
      || ehdr->e_ident[EI_MAG2] != ELFMAG2
      || ehdr->e_ident[EI_MAG3] != ELFMAG3
64
      || ehdr->e_ident[EI_DATA] != ELFDATA2LSB)
65
    return grub_error(GRUB_ERR_UNKNOWN_OS, N_("invalid arch-independent ELF magic"));
66 67 68

  if (ehdr->e_ident[EI_CLASS] != ELFCLASSXX || ehdr->e_machine != E_MACHINE
      || ehdr->e_version != EV_CURRENT)
69
    return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF magic"));
70

71
  if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
72
    return grub_error (GRUB_ERR_UNKNOWN_OS, N_("this ELF file is not of the right type"));
73

74
  /* FIXME: Should we support program headers at strange locations?  */
75
  if (ehdr->e_phoff + (grub_uint32_t) ehdr->e_phnum * ehdr->e_phentsize > MULTIBOOT_SEARCH)
76 77 78 79 80 81 82 83
    return grub_error (GRUB_ERR_BAD_OS, "program header at a too high offset");

  phdr_base = (char *) buffer + ehdr->e_phoff;
#define phdr(i)			((Elf_Phdr *) (phdr_base + (i) * ehdr->e_phentsize))

  /* Load every loadable segment in memory.  */
  for (i = 0; i < ehdr->e_phnum; i++)
    {
84
      if (phdr(i)->p_type == PT_LOAD)
85
        {
86 87
	  grub_err_t err;
	  void *source;
88

89 90 91
	  if (phdr(i)->p_paddr + phdr(i)->p_memsz > highest_load)
	    highest_load = phdr(i)->p_paddr + phdr(i)->p_memsz;

92 93
	  grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n",
			i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
94

95 96 97 98 99 100 101 102 103 104 105 106
	  {
	    grub_relocator_chunk_t ch;
	    err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
						   &ch, phdr(i)->p_paddr,
						   phdr(i)->p_memsz);
	    if (err)
	      {
		grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i);
		return err;
	      }
	    source = get_virtual_current_address (ch);
	  }
107

108 109 110 111
	  if (phdr(i)->p_filesz != 0)
	    {
	      if (grub_file_seek (file, (grub_off_t) phdr(i)->p_offset)
		  == (grub_off_t) -1)
112
		return grub_errno;
113 114 115

	      if (grub_file_read (file, source, phdr(i)->p_filesz)
		  != (grub_ssize_t) phdr(i)->p_filesz)
116 117 118 119 120 121
		{
		  if (!grub_errno)
		    grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
				filename);
		  return grub_errno;
		}
122
	    }
123 124

          if (phdr(i)->p_filesz < phdr(i)->p_memsz)
125
            grub_memset ((grub_uint8_t *) source + phdr(i)->p_filesz, 0,
126 127 128 129
			 phdr(i)->p_memsz - phdr(i)->p_filesz);
        }
    }

130
  for (i = 0; i < ehdr->e_phnum; i++)
131
    if (phdr(i)->p_vaddr <= ehdr->e_entry
132 133
	&& phdr(i)->p_vaddr + phdr(i)->p_memsz > ehdr->e_entry)
      {
134 135
	grub_multiboot_payload_eip = (ehdr->e_entry - phdr(i)->p_vaddr)
	  + phdr(i)->p_paddr;
136 137 138 139 140 141 142 143 144 145 146 147 148
#ifdef MULTIBOOT_LOAD_ELF64
# ifdef __mips
  /* We still in 32-bit mode.  */
  if ((ehdr->e_entry - phdr(i)->p_vaddr)
      + phdr(i)->p_paddr < 0xffffffff80000000ULL)
    return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64");
# else
  /* We still in 32-bit mode.  */
  if ((ehdr->e_entry - phdr(i)->p_vaddr)
      + phdr(i)->p_paddr > 0xffffffff)
    return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64");
# endif
#endif
149 150 151 152 153
	break;
      }

  if (i == ehdr->e_phnum)
    return grub_error (GRUB_ERR_BAD_OS, "entry point isn't in a segment");
154

155 156 157 158 159 160 161 162
#if defined (__i386__) || defined (__x86_64__)
  
#elif defined (__mips)
  grub_multiboot_payload_eip |= 0x80000000;
#else
#error Please complete this
#endif

163 164 165 166
  if (ehdr->e_shnum)
    {
      grub_uint8_t *shdr, *shdrptr;

167
      shdr = grub_malloc ((grub_uint32_t) ehdr->e_shnum * ehdr->e_shentsize);
168 169 170 171
      if (!shdr)
	return grub_errno;
      
      if (grub_file_seek (file, ehdr->e_shoff) == (grub_off_t) -1)
172 173 174 175
	{
	  grub_free (shdr);
	  return grub_errno;
	}
176

177
      if (grub_file_read (file, shdr, (grub_uint32_t) ehdr->e_shnum * ehdr->e_shentsize)
178
              != (grub_ssize_t) ehdr->e_shnum * ehdr->e_shentsize)
179 180 181 182 183 184
	{
	  if (!grub_errno)
	    grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
			filename);
	  return grub_errno;
	}
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
      
      for (shdrptr = shdr, i = 0; i < ehdr->e_shnum;
	   shdrptr += ehdr->e_shentsize, i++)
	{
	  Elf_Shdr *sh = (Elf_Shdr *) shdrptr;
	  void *src;
	  grub_addr_t target;
	  grub_err_t err;

	  /* This section is a loaded section,
	     so we don't care.  */
	  if (sh->sh_addr != 0)
	    continue;
		      
	  /* This section is empty, so we don't care.  */
	  if (sh->sh_size == 0)
	    continue;

203 204 205 206 207 208 209
	  {
	    grub_relocator_chunk_t ch;
	    err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator,
						    &ch, 0,
						    (0xffffffff - sh->sh_size)
						    + 1, sh->sh_size,
						    sh->sh_addralign,
210 211
						    GRUB_RELOCATOR_PREFERENCE_NONE,
						    0);
212 213 214 215 216 217 218 219
	    if (err)
	      {
		grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i);
		return err;
	      }
	    src = get_virtual_current_address (ch);
	    target = get_physical_target_address (ch);
	  }
220 221

	  if (grub_file_seek (file, sh->sh_offset) == (grub_off_t) -1)
222
	    return grub_errno;
223 224 225

          if (grub_file_read (file, src, sh->sh_size)
              != (grub_ssize_t) sh->sh_size)
226 227 228 229 230 231
	    {
	      if (!grub_errno)
		grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
			    filename);
	      return grub_errno;
	    }
232 233 234 235 236 237
	  sh->sh_addr = target;
	}
      grub_multiboot_add_elfsyms (ehdr->e_shnum, ehdr->e_shentsize,
				  ehdr->e_shstrndx, shdr);
    }

238 239 240 241 242 243 244 245 246 247
#undef phdr

  return grub_errno;
}

#undef XX
#undef E_MACHINE
#undef ELFCLASSXX
#undef Elf_Ehdr
#undef Elf_Phdr
248
#undef Elf_Shdr