aboutsummaryrefslogtreecommitdiffstats
path: root/grub-core/efiemu/loadcore.c
blob: edadd45806c52c65d9d455d35b483fadcc33a87e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* Load runtime image of EFIemu. Functions specific to 32/64-bit mode */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 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/>.
 */

#include <grub/err.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/efiemu/efiemu.h>
#include <grub/cpu/efiemu.h>
#include <grub/elf.h>

/* ELF symbols and their values */
static struct grub_efiemu_elf_sym *grub_efiemu_elfsyms = 0;
static int grub_efiemu_nelfsyms = 0;

/* Return the address of a section whose index is N.  */
static grub_err_t
grub_efiemu_get_section_addr (grub_efiemu_segment_t segs, unsigned n,
			      int *handle, grub_off_t *off)
{
  grub_efiemu_segment_t seg;

  for (seg = segs; seg; seg = seg->next)
    if (seg->section == n)
      {
	*handle = seg->handle;
	*off = seg->off;
	return GRUB_ERR_NONE;
      }

  return grub_error (GRUB_ERR_BAD_OS, "section %d not found", n);
}

grub_err_t
SUFFIX (grub_efiemu_loadcore_unload) (void)
{
  grub_free (grub_efiemu_elfsyms);
  grub_efiemu_elfsyms = 0;
  return GRUB_ERR_NONE;
}

/* Check if EHDR is a valid ELF header.  */
int
SUFFIX (grub_efiemu_check_header) (void *ehdr, grub_size_t size)
{
  Elf_Ehdr *e = ehdr;

  /* Check the header size.  */
  if (size < sizeof (Elf_Ehdr))
    return 0;

  /* Check the magic numbers.  */
  if (!SUFFIX (grub_arch_efiemu_check_header) (ehdr)
      || e->e_ident[EI_MAG0] != ELFMAG0
      || e->e_ident[EI_MAG1] != ELFMAG1
      || e->e_ident[EI_MAG2] != ELFMAG2
      || e->e_ident[EI_MAG3] != ELFMAG3
      || e->e_ident[EI_VERSION] != EV_CURRENT
      || e->e_version != EV_CURRENT)
    return 0;

  return 1;
}

/* Load all segments from memory specified by E.  */
static grub_err_t
grub_efiemu_load_segments (grub_efiemu_segment_t segs, const Elf_Ehdr *e)
{
  Elf_Shdr *s;
  grub_efiemu_segment_t cur;

  grub_dprintf ("efiemu", "loading segments\n");

  for (cur=segs; cur; cur = cur->next)
    {
      s = (Elf_Shdr *)cur->srcptr;

      if ((s->sh_flags & SHF_ALLOC) && s->sh_size)
	{
	  void *addr;

	  addr = (grub_uint8_t *) grub_efiemu_mm_obtain_request (cur->handle)
	    + cur->off;

	  switch (s->sh_type)
	    {
	    case SHT_PROGBITS:
	      grub_memcpy (addr, (char *) e + s->sh_offset, s->sh_size);
	      break;
	    case SHT_NOBITS:
	      grub_memset (addr, 0, s->sh_size);
	      break;
	    }
	}
    }

  return GRUB_ERR_NONE;
}

/* Get a string at offset OFFSET from strtab */
static char *
grub_efiemu_get_string (unsigned offset, const Elf_Ehdr *e)
{
  unsigned i;
  Elf_Shdr *s;

  for (i = 0, s = (Elf_Shdr *)((char *) e + e->e_shoff);
       i < e->e_shnum;
       i++, s = (Elf_Shdr *)((char *) s + e->e_shentsize))
    if (s->sh_type == SHT_STRTAB && offset < s->sh_size)
      return (char *) e + s->sh_offset + offset;
  return 0;
}

/* Request memory for segments and fill segments info */
static grub_err_t
grub_efiemu_init_segments (grub_efiemu_segment_t *segs, const Elf_Ehdr *e)
{
  unsigned i;
  Elf_Shdr *s;

  for (i = 0, s = (Elf_Shdr *)((char *) e + e->e_shoff);
       i < e->e_shnum;
       i++, s = (Elf_Shdr *)((char *) s + e->e_shentsize))
    {
      if (s->sh_flags & SHF_ALLOC)
	{
	  grub_efiemu_segment_t seg;
	  seg = (grub_efiemu_segment_t) grub_malloc (sizeof (*seg));
	  if (! seg)
	    return grub_errno;

	  if (s->sh_size)
	    {
	      seg->handle
		= grub_efiemu_request_memalign
		(s->sh_addralign, s->sh_size,
		 s->sh_flags & SHF_EXECINSTR ? GRUB_EFI_RUNTIME_SERVICES_CODE
		 : GRUB_EFI_RUNTIME_SERVICES_DATA);
	      if (seg->handle < 0)
		return grub_errno;
	      seg->off = 0;
	    }

	  /*
	     .text-physical doesn't need to be relocated when switching to
	     virtual mode
	   */
	  if (!grub_strcmp (grub_efiemu_get_string (s->sh_name, e),
			    ".text-physical"))
	    seg->ptv_rel_needed = 0;
	  else
	    seg->ptv_rel_needed = 1;
	  seg->size = s->sh_size;
	  seg->section = i;
	  seg->next = *segs;
	  seg->srcptr = s;
	  *segs = seg;
	}
    }

  return GRUB_ERR_NONE;
}

/* Count symbols and relocators and allocate/request memory for them */
static grub_err_t
grub_efiemu_count_symbols (const Elf_Ehdr *e)
{
  unsigned i;
  Elf_Shdr *s;
  int num = 0;

  /* Symbols */
  for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
       i < e->e_shnum;
       i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
    if (s->sh_type == SHT_SYMTAB)
      break;

  if (i == e->e_shnum)
    return grub_error (GRUB_ERR_BAD_OS, "no symbol table");

  grub_efiemu_nelfsyms = (unsigned) s->sh_size / (unsigned) s->sh_entsize;
  grub_efiemu_elfsyms = (struct grub_efiemu_elf_sym *)
    grub_malloc (sizeof (struct grub_efiemu_elf_sym) * grub_efiemu_nelfsyms);

  /* Relocators */
  for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
       i < e->e_shnum;
       i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
    if (s->sh_type == SHT_REL || s->sh_type == SHT_RELA)
      num += ((unsigned) s->sh_size) / ((unsigned) s->sh_entsize);

  grub_efiemu_request_symbols (num);

  return GRUB_ERR_NONE;
}

/* Fill grub_efiemu_elfsyms with symbol values */
static grub_err_t
grub_efiemu_resolve_symbols (grub_efiemu_segment_t segs, Elf_Ehdr *e)
{
  unsigned i;
  Elf_Shdr *s;
  Elf_Sym *sym;
  const char *str;
  Elf_Word size, entsize;

  grub_dprintf ("efiemu", "resolving symbols\n");

  for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
       i < e->e_shnum;
       i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
    if (s->sh_type == SHT_SYMTAB)
      break;

  if (i == e->e_shnum)
    return grub_error (GRUB_ERR_BAD_OS, "no symbol table");

  sym = (Elf_Sym *) ((char *) e + s->sh_offset);
  size = s->sh_size;
  entsize = s->sh_entsize;

  s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shentsize * s->sh_link);
  str = (char *) e + s->sh_offset;

  for (i = 0;
       i < size / entsize;
       i++, sym = (Elf_Sym *) ((char *) sym + entsize))
    {
      unsigned char type = ELF_ST_TYPE (sym->st_info);
      unsigned char bind = ELF_ST_BIND (sym->st_info);
      int handle;
      grub_off_t off;
      grub_err_t err;
      const char *name = str + sym->st_name;
      grub_efiemu_elfsyms[i].section = sym->st_shndx;
      switch (type)
	{
	case STT_NOTYPE:
	  /* Resolve a global symbol.  */
	  if (sym->st_name != 0 && sym->st_shndx == 0)
	    {
	      if ((err = grub_efiemu_resolve_symbol (name, &handle, &off)))
		return err;
	      grub_efiemu_elfsyms[i].handle = handle;
	      grub_efiemu_elfsyms[i].off = off;
	    }
	  else
	    sym->st_value = 0;
	  break;

	case STT_OBJECT:
	  if ((err = grub_efiemu_get_section_addr
	       (segs, sym->st_shndx, &handle, &off)))
	    return err;

	  off += sym->st_value;
	  if (bind != STB_LOCAL)
	    if ((err = grub_efiemu_register_symbol (name, handle, off)))
	      return err;
	  grub_efiemu_elfsyms[i].handle = handle;
	  grub_efiemu_elfsyms[i].off = off;
	  break;

	case STT_FUNC:
	  if ((err = grub_efiemu_get_section_addr
	       (segs, sym->st_shndx, &handle, &off)))
	    return err;

	  off += sym->st_value;
	  if (bind != STB_LOCAL)
	    if ((err = grub_efiemu_register_symbol (name, handle, off)))
	      return err;
	  grub_efiemu_elfsyms[i].handle = handle;
	  grub_efiemu_elfsyms[i].off = off;
	  break;

	case STT_SECTION:
	  if ((err = grub_efiemu_get_section_addr
	       (segs, sym->st_shndx, &handle, &off)))
	    {
	      grub_efiemu_elfsyms[i].handle = 0;
	      grub_efiemu_elfsyms[i].off = 0;
	      grub_errno = GRUB_ERR_NONE;
	      break;
	    }

	  grub_efiemu_elfsyms[i].handle = handle;
	  grub_efiemu_elfsyms[i].off = off;
	  break;

	case STT_FILE:
	  grub_efiemu_elfsyms[i].handle = 0;
	  grub_efiemu_elfsyms[i].off = 0;
	  break;

	default:
	  return grub_error (GRUB_ERR_BAD_MODULE,
			     "unknown symbol type `%d'", (int) type);
	}
    }

  return GRUB_ERR_NONE;
}

/* Load runtime to the memory and request memory for definitive location*/
grub_err_t
SUFFIX (grub_efiemu_loadcore_init) (void *core, grub_size_t core_size,
				    grub_efiemu_segment_t *segments)
{
  Elf_Ehdr *e = (Elf_Ehdr *) core;
  grub_err_t err;

  if (e->e_type != ET_REL)
    return grub_error (GRUB_ERR_BAD_MODULE, "invalid ELF file type");

  /* Make sure that every section is within the core.  */
  if ((grub_size_t) core_size < e->e_shoff + e->e_shentsize * e->e_shnum)
    return grub_error (GRUB_ERR_BAD_OS, "ELF sections outside core");

  if ((err = grub_efiemu_init_segments (segments, core)))
    return err;
  if ((err = grub_efiemu_count_symbols (core)))
    return err;

  grub_efiemu_request_symbols (1);
  return GRUB_ERR_NONE;
}

/* Load runtime definitively */
grub_err_t
SUFFIX (grub_efiemu_loadcore_load) (void *core,
				    grub_size_t core_size
				    __attribute__ ((unused)),
				    grub_efiemu_segment_t segments)
{
  grub_err_t err;
  err = grub_efiemu_load_segments (segments, core);
  if (err)
    return err;

  err = grub_efiemu_resolve_symbols (segments, core);
  if (err)
    return err;

  err = SUFFIX (grub_arch_efiemu_relocate_symbols) (segments,
						    grub_efiemu_elfsyms,
						    core);
  if (err)
    return err;

  return GRUB_ERR_NONE;
}