Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / efiemu / i386 / loadcore32.c
blobe746df8df447f80aac488456be93c82ac6165b2a
1 /* i386 CPU-specific part of loadcore.c for 32-bit mode */
2 /*
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/>.
20 #include <grub/err.h>
21 #include <grub/mm.h>
22 #include <grub/misc.h>
23 #include <grub/efiemu/efiemu.h>
24 #include <grub/cpu/efiemu.h>
25 #include <grub/elf.h>
26 #include <grub/i18n.h>
28 /* Check if EHDR is a valid ELF header. */
29 int
30 grub_arch_efiemu_check_header32 (void *ehdr)
32 Elf32_Ehdr *e = ehdr;
34 /* Check the magic numbers. */
35 return (e->e_ident[EI_CLASS] == ELFCLASS32
36 && e->e_ident[EI_DATA] == ELFDATA2LSB
37 && e->e_machine == EM_386);
40 /* Relocate symbols. */
41 grub_err_t
42 grub_arch_efiemu_relocate_symbols32 (grub_efiemu_segment_t segs,
43 struct grub_efiemu_elf_sym *elfsyms,
44 void *ehdr)
46 unsigned i;
47 Elf32_Ehdr *e = ehdr;
48 Elf32_Shdr *s;
49 grub_err_t err;
51 grub_dprintf ("efiemu", "relocating symbols %d %d\n",
52 e->e_shoff, e->e_shnum);
54 for (i = 0, s = (Elf32_Shdr *) ((char *) e + e->e_shoff);
55 i < e->e_shnum;
56 i++, s = (Elf32_Shdr *) ((char *) s + e->e_shentsize))
57 if (s->sh_type == SHT_REL)
59 grub_efiemu_segment_t seg;
60 grub_dprintf ("efiemu", "shtrel\n");
62 /* Find the target segment. */
63 for (seg = segs; seg; seg = seg->next)
64 if (seg->section == s->sh_info)
65 break;
67 if (seg)
69 Elf32_Rel *rel, *max;
71 for (rel = (Elf32_Rel *) ((char *) e + s->sh_offset),
72 max = rel + s->sh_size / s->sh_entsize;
73 rel < max;
74 rel++)
76 Elf32_Word *addr;
77 struct grub_efiemu_elf_sym sym;
78 if (seg->size < rel->r_offset)
79 return grub_error (GRUB_ERR_BAD_MODULE,
80 "reloc offset is out of the segment");
82 addr = (Elf32_Word *)
83 ((char *) grub_efiemu_mm_obtain_request (seg->handle)
84 + seg->off + rel->r_offset);
85 sym = elfsyms[ELF32_R_SYM (rel->r_info)];
87 switch (ELF32_R_TYPE (rel->r_info))
89 case R_386_32:
90 err = grub_efiemu_write_value (addr, sym.off + *addr,
91 sym.handle, 0,
92 seg->ptv_rel_needed,
93 sizeof (grub_uint32_t));
94 if (err)
95 return err;
97 break;
99 case R_386_PC32:
100 err = grub_efiemu_write_value (addr, sym.off + *addr
101 - rel->r_offset
102 - seg->off, sym.handle,
103 seg->handle,
104 seg->ptv_rel_needed,
105 sizeof (grub_uint32_t));
106 if (err)
107 return err;
108 break;
109 default:
110 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
111 N_("relocation 0x%x is not implemented yet"),
112 ELF_R_TYPE (rel->r_info));
118 return GRUB_ERR_NONE;