1
/* Load runtime image of EFIemu. Functions specific to 32/64-bit mode */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2009 Free Software Foundation, Inc.
6
* GRUB is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* GRUB is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
22
#include <grub/misc.h>
23
#include <grub/efiemu/efiemu.h>
24
#include <grub/cpu/efiemu.h>
27
/* ELF symbols and their values */
28
static struct grub_efiemu_elf_sym *grub_efiemu_elfsyms = 0;
29
static int grub_efiemu_nelfsyms = 0;
31
/* Return the address of a section whose index is N. */
33
grub_efiemu_get_section_addr (grub_efiemu_segment_t segs, unsigned n,
34
int *handle, grub_off_t *off)
36
grub_efiemu_segment_t seg;
38
for (seg = segs; seg; seg = seg->next)
39
if (seg->section == n)
41
*handle = seg->handle;
46
return grub_error (GRUB_ERR_BAD_OS, "section %d not found", n);
50
SUFFIX (grub_efiemu_loadcore_unload) (void)
52
grub_free (grub_efiemu_elfsyms);
53
grub_efiemu_elfsyms = 0;
57
/* Check if EHDR is a valid ELF header. */
59
SUFFIX (grub_efiemu_check_header) (void *ehdr, grub_size_t size)
63
/* Check the header size. */
64
if (size < sizeof (Elf_Ehdr))
67
/* Check the magic numbers. */
68
if (!SUFFIX (grub_arch_efiemu_check_header) (ehdr)
69
|| e->e_ident[EI_MAG0] != ELFMAG0
70
|| e->e_ident[EI_MAG1] != ELFMAG1
71
|| e->e_ident[EI_MAG2] != ELFMAG2
72
|| e->e_ident[EI_MAG3] != ELFMAG3
73
|| e->e_ident[EI_VERSION] != EV_CURRENT
74
|| e->e_version != EV_CURRENT)
80
/* Load all segments from memory specified by E. */
82
grub_efiemu_load_segments (grub_efiemu_segment_t segs, const Elf_Ehdr *e)
85
grub_efiemu_segment_t cur;
87
grub_dprintf ("efiemu", "loading segments\n");
89
for (cur=segs; cur; cur = cur->next)
91
s = (Elf_Shdr *)cur->srcptr;
93
if ((s->sh_flags & SHF_ALLOC) && s->sh_size)
97
addr = (grub_uint8_t *) grub_efiemu_mm_obtain_request (cur->handle)
103
grub_memcpy (addr, (char *) e + s->sh_offset, s->sh_size);
106
grub_memset (addr, 0, s->sh_size);
112
return GRUB_ERR_NONE;
115
/* Get a string at offset OFFSET from strtab */
117
grub_efiemu_get_string (unsigned offset, const Elf_Ehdr *e)
122
for (i = 0, s = (Elf_Shdr *)((char *) e + e->e_shoff);
124
i++, s = (Elf_Shdr *)((char *) s + e->e_shentsize))
125
if (s->sh_type == SHT_STRTAB && offset < s->sh_size)
126
return (char *) e + s->sh_offset + offset;
130
/* Request memory for segments and fill segments info */
132
grub_efiemu_init_segments (grub_efiemu_segment_t *segs, const Elf_Ehdr *e)
137
for (i = 0, s = (Elf_Shdr *)((char *) e + e->e_shoff);
139
i++, s = (Elf_Shdr *)((char *) s + e->e_shentsize))
141
if (s->sh_flags & SHF_ALLOC)
143
grub_efiemu_segment_t seg;
144
seg = (grub_efiemu_segment_t) grub_malloc (sizeof (*seg));
151
= grub_efiemu_request_memalign
152
(s->sh_addralign, s->sh_size,
153
s->sh_flags & SHF_EXECINSTR ? GRUB_EFI_RUNTIME_SERVICES_CODE
154
: GRUB_EFI_RUNTIME_SERVICES_DATA);
161
.text-physical doesn't need to be relocated when switching to
164
if (!grub_strcmp (grub_efiemu_get_string (s->sh_name, e),
166
seg->ptv_rel_needed = 0;
168
seg->ptv_rel_needed = 1;
169
seg->size = s->sh_size;
177
return GRUB_ERR_NONE;
180
/* Count symbols and relocators and allocate/request memory for them */
182
grub_efiemu_count_symbols (const Elf_Ehdr *e)
189
for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
191
i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
192
if (s->sh_type == SHT_SYMTAB)
196
return grub_error (GRUB_ERR_BAD_OS, "no symbol table");
198
grub_efiemu_nelfsyms = (unsigned) s->sh_size / (unsigned) s->sh_entsize;
199
grub_efiemu_elfsyms = (struct grub_efiemu_elf_sym *)
200
grub_malloc (sizeof (struct grub_efiemu_elf_sym) * grub_efiemu_nelfsyms);
203
for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
205
i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
206
if (s->sh_type == SHT_REL || s->sh_type == SHT_RELA)
207
num += ((unsigned) s->sh_size) / ((unsigned) s->sh_entsize);
209
grub_efiemu_request_symbols (num);
211
return GRUB_ERR_NONE;
214
/* Fill grub_efiemu_elfsyms with symbol values */
216
grub_efiemu_resolve_symbols (grub_efiemu_segment_t segs, Elf_Ehdr *e)
222
Elf_Word size, entsize;
224
grub_dprintf ("efiemu", "resolving symbols\n");
226
for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
228
i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
229
if (s->sh_type == SHT_SYMTAB)
233
return grub_error (GRUB_ERR_BAD_OS, "no symbol table");
235
sym = (Elf_Sym *) ((char *) e + s->sh_offset);
237
entsize = s->sh_entsize;
239
s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shentsize * s->sh_link);
240
str = (char *) e + s->sh_offset;
244
i++, sym = (Elf_Sym *) ((char *) sym + entsize))
246
unsigned char type = ELF_ST_TYPE (sym->st_info);
247
unsigned char bind = ELF_ST_BIND (sym->st_info);
251
const char *name = str + sym->st_name;
252
grub_efiemu_elfsyms[i].section = sym->st_shndx;
256
/* Resolve a global symbol. */
257
if (sym->st_name != 0 && sym->st_shndx == 0)
259
if ((err = grub_efiemu_resolve_symbol (name, &handle, &off)))
261
grub_efiemu_elfsyms[i].handle = handle;
262
grub_efiemu_elfsyms[i].off = off;
269
if ((err = grub_efiemu_get_section_addr
270
(segs, sym->st_shndx, &handle, &off)))
273
off += sym->st_value;
274
if (bind != STB_LOCAL)
275
if ((err = grub_efiemu_register_symbol (name, handle, off)))
277
grub_efiemu_elfsyms[i].handle = handle;
278
grub_efiemu_elfsyms[i].off = off;
282
if ((err = grub_efiemu_get_section_addr
283
(segs, sym->st_shndx, &handle, &off)))
286
off += sym->st_value;
287
if (bind != STB_LOCAL)
288
if ((err = grub_efiemu_register_symbol (name, handle, off)))
290
grub_efiemu_elfsyms[i].handle = handle;
291
grub_efiemu_elfsyms[i].off = off;
295
if ((err = grub_efiemu_get_section_addr
296
(segs, sym->st_shndx, &handle, &off)))
298
grub_efiemu_elfsyms[i].handle = 0;
299
grub_efiemu_elfsyms[i].off = 0;
300
grub_errno = GRUB_ERR_NONE;
304
grub_efiemu_elfsyms[i].handle = handle;
305
grub_efiemu_elfsyms[i].off = off;
309
grub_efiemu_elfsyms[i].handle = 0;
310
grub_efiemu_elfsyms[i].off = 0;
314
return grub_error (GRUB_ERR_BAD_MODULE,
315
"unknown symbol type `%d'", (int) type);
319
return GRUB_ERR_NONE;
322
/* Load runtime to the memory and request memory for definitive location*/
324
SUFFIX (grub_efiemu_loadcore_init) (void *core, grub_size_t core_size,
325
grub_efiemu_segment_t *segments)
327
Elf_Ehdr *e = (Elf_Ehdr *) core;
330
if (e->e_type != ET_REL)
331
return grub_error (GRUB_ERR_BAD_MODULE, "invalid ELF file type");
333
/* Make sure that every section is within the core. */
334
if ((grub_size_t) core_size < e->e_shoff + e->e_shentsize * e->e_shnum)
335
return grub_error (GRUB_ERR_BAD_OS, "ELF sections outside core");
337
if ((err = grub_efiemu_init_segments (segments, core)))
339
if ((err = grub_efiemu_count_symbols (core)))
342
grub_efiemu_request_symbols (1);
343
return GRUB_ERR_NONE;
346
/* Load runtime definitively */
348
SUFFIX (grub_efiemu_loadcore_load) (void *core,
349
grub_size_t core_size
350
__attribute__ ((unused)),
351
grub_efiemu_segment_t segments)
354
err = grub_efiemu_load_segments (segments, core);
358
err = grub_efiemu_resolve_symbols (segments, core);
362
err = SUFFIX (grub_arch_efiemu_relocate_symbols) (segments,
368
return GRUB_ERR_NONE;