]> git.proxmox.com Git - grub2.git/blob - grub-core/loader/multiboot_elfxx.c
f38f37262243ca337c43e476d9275d48d1fdc1f8
[grub2.git] / grub-core / loader / multiboot_elfxx.c
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
4 *
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #if defined(MULTIBOOT_LOAD_ELF32)
20 # define XX 32
21 # define E_MACHINE MULTIBOOT_ELF32_MACHINE
22 # define ELFCLASSXX ELFCLASS32
23 # define Elf_Ehdr Elf32_Ehdr
24 # define Elf_Phdr Elf32_Phdr
25 # define Elf_Shdr Elf32_Shdr
26 #elif defined(MULTIBOOT_LOAD_ELF64)
27 # define XX 64
28 # define E_MACHINE MULTIBOOT_ELF64_MACHINE
29 # define ELFCLASSXX ELFCLASS64
30 # define Elf_Ehdr Elf64_Ehdr
31 # define Elf_Phdr Elf64_Phdr
32 # define Elf_Shdr Elf64_Shdr
33 #else
34 #error "I'm confused"
35 #endif
36
37 #include <grub/i386/relocator.h>
38
39 #define CONCAT(a,b) CONCAT_(a, b)
40 #define CONCAT_(a,b) a ## b
41
42 #pragma GCC diagnostic ignored "-Wcast-align"
43
44 /* Check if BUFFER contains ELF32 (or ELF64). */
45 static int
46 CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
47 {
48 Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
49
50 return ehdr->e_ident[EI_CLASS] == ELFCLASSXX;
51 }
52
53 static grub_err_t
54 CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, void *buffer)
55 {
56 Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
57 char *phdr_base;
58 int i;
59
60 if (ehdr->e_ident[EI_MAG0] != ELFMAG0
61 || ehdr->e_ident[EI_MAG1] != ELFMAG1
62 || ehdr->e_ident[EI_MAG2] != ELFMAG2
63 || 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 if (ehdr->e_ident[EI_CLASS] != ELFCLASSXX || ehdr->e_machine != E_MACHINE
68 || 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 + ehdr->e_phnum * ehdr->e_phentsize > MULTIBOOT_SEARCH)
76 return grub_error (GRUB_ERR_BAD_OS, "program header at a too high offset");
77
78 #ifdef MULTIBOOT_LOAD_ELF64
79 # ifdef __mips
80 /* We still in 32-bit mode. */
81 if (ehdr->e_entry < 0xffffffff80000000ULL)
82 return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64");
83 # else
84 /* We still in 32-bit mode. */
85 if (ehdr->e_entry > 0xffffffff)
86 return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64");
87 # endif
88 #endif
89
90 phdr_base = (char *) buffer + ehdr->e_phoff;
91 #define phdr(i) ((Elf_Phdr *) (phdr_base + (i) * ehdr->e_phentsize))
92
93 /* Load every loadable segment in memory. */
94 for (i = 0; i < ehdr->e_phnum; i++)
95 {
96 if (phdr(i)->p_type == PT_LOAD)
97 {
98 grub_err_t err;
99 void *source;
100
101 grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n",
102 i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
103
104 {
105 grub_relocator_chunk_t ch;
106 err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
107 &ch, phdr(i)->p_paddr,
108 phdr(i)->p_memsz);
109 if (err)
110 {
111 grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i);
112 return err;
113 }
114 source = get_virtual_current_address (ch);
115 }
116
117 if (phdr(i)->p_filesz != 0)
118 {
119 if (grub_file_seek (file, (grub_off_t) phdr(i)->p_offset)
120 == (grub_off_t) -1)
121 return grub_errno;
122
123 if (grub_file_read (file, source, phdr(i)->p_filesz)
124 != (grub_ssize_t) phdr(i)->p_filesz)
125 {
126 if (!grub_errno)
127 grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
128 filename);
129 return grub_errno;
130 }
131 }
132
133 if (phdr(i)->p_filesz < phdr(i)->p_memsz)
134 grub_memset ((grub_uint8_t *) source + phdr(i)->p_filesz, 0,
135 phdr(i)->p_memsz - phdr(i)->p_filesz);
136 }
137 }
138
139 for (i = 0; i < ehdr->e_phnum; i++)
140 if (phdr(i)->p_vaddr <= ehdr->e_entry
141 && phdr(i)->p_vaddr + phdr(i)->p_memsz > ehdr->e_entry)
142 {
143 grub_multiboot_payload_eip = (ehdr->e_entry - phdr(i)->p_vaddr)
144 + phdr(i)->p_paddr;
145 break;
146 }
147
148 if (i == ehdr->e_phnum)
149 return grub_error (GRUB_ERR_BAD_OS, "entry point isn't in a segment");
150
151 #if defined (__i386__) || defined (__x86_64__)
152
153 #elif defined (__mips)
154 grub_multiboot_payload_eip |= 0x80000000;
155 #else
156 #error Please complete this
157 #endif
158
159 if (ehdr->e_shnum)
160 {
161 grub_uint8_t *shdr, *shdrptr;
162
163 shdr = grub_malloc (ehdr->e_shnum * ehdr->e_shentsize);
164 if (!shdr)
165 return grub_errno;
166
167 if (grub_file_seek (file, ehdr->e_shoff) == (grub_off_t) -1)
168 return grub_errno;
169
170 if (grub_file_read (file, shdr, ehdr->e_shnum * ehdr->e_shentsize)
171 != (grub_ssize_t) ehdr->e_shnum * ehdr->e_shentsize)
172 {
173 if (!grub_errno)
174 grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
175 filename);
176 return grub_errno;
177 }
178
179 for (shdrptr = shdr, i = 0; i < ehdr->e_shnum;
180 shdrptr += ehdr->e_shentsize, i++)
181 {
182 Elf_Shdr *sh = (Elf_Shdr *) shdrptr;
183 void *src;
184 grub_addr_t target;
185 grub_err_t err;
186
187 /* This section is a loaded section,
188 so we don't care. */
189 if (sh->sh_addr != 0)
190 continue;
191
192 /* This section is empty, so we don't care. */
193 if (sh->sh_size == 0)
194 continue;
195
196 {
197 grub_relocator_chunk_t ch;
198 err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator,
199 &ch, 0,
200 (0xffffffff - sh->sh_size)
201 + 1, sh->sh_size,
202 sh->sh_addralign,
203 GRUB_RELOCATOR_PREFERENCE_NONE);
204 if (err)
205 {
206 grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i);
207 return err;
208 }
209 src = get_virtual_current_address (ch);
210 target = get_physical_target_address (ch);
211 }
212
213 if (grub_file_seek (file, sh->sh_offset) == (grub_off_t) -1)
214 return grub_errno;
215
216 if (grub_file_read (file, src, sh->sh_size)
217 != (grub_ssize_t) sh->sh_size)
218 {
219 if (!grub_errno)
220 grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
221 filename);
222 return grub_errno;
223 }
224 sh->sh_addr = target;
225 }
226 grub_multiboot_add_elfsyms (ehdr->e_shnum, ehdr->e_shentsize,
227 ehdr->e_shstrndx, shdr);
228 }
229
230 #undef phdr
231
232 return grub_errno;
233 }
234
235 #undef XX
236 #undef E_MACHINE
237 #undef ELFCLASSXX
238 #undef Elf_Ehdr
239 #undef Elf_Phdr
240 #undef Elf_Shdr