]> git.proxmox.com Git - grub2.git/blob - grub-core/loader/multiboot_elfxx.c
merge mainline into hints
[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 /* Check if BUFFER contains ELF32 (or ELF64). */
43 static int
44 CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
45 {
46 Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
47
48 return ehdr->e_ident[EI_CLASS] == ELFCLASSXX;
49 }
50
51 static grub_err_t
52 CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, void *buffer)
53 {
54 Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
55 char *phdr_base;
56 int i;
57
58 if (ehdr->e_ident[EI_CLASS] != ELFCLASSXX)
59 return grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF class");
60
61 if (ehdr->e_ident[EI_MAG0] != ELFMAG0
62 || ehdr->e_ident[EI_MAG1] != ELFMAG1
63 || ehdr->e_ident[EI_MAG2] != ELFMAG2
64 || ehdr->e_ident[EI_MAG3] != ELFMAG3
65 || ehdr->e_version != EV_CURRENT
66 || ehdr->e_ident[EI_DATA] != ELFDATA2LSB
67 || ehdr->e_machine != E_MACHINE)
68 return grub_error(GRUB_ERR_UNKNOWN_OS, "no valid ELF header found");
69
70 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
71 return grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF file type");
72
73 /* FIXME: Should we support program headers at strange locations? */
74 if (ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize > MULTIBOOT_SEARCH)
75 return grub_error (GRUB_ERR_BAD_OS, "program header at a too high offset");
76
77 #if defined (MULTIBOOT_LOAD_ELF64) && defined (__mips)
78 /* We still in 32-bit mode. */
79 if (ehdr->e_entry < 0xffffffff80000000ULL)
80 return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64");
81 #else
82 /* We still in 32-bit mode. */
83 if (ehdr->e_entry > 0xffffffff)
84 return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64");
85 #endif
86
87 phdr_base = (char *) buffer + ehdr->e_phoff;
88 #define phdr(i) ((Elf_Phdr *) (phdr_base + (i) * ehdr->e_phentsize))
89
90 /* Load every loadable segment in memory. */
91 for (i = 0; i < ehdr->e_phnum; i++)
92 {
93 if (phdr(i)->p_type == PT_LOAD)
94 {
95 grub_err_t err;
96 void *source;
97
98 grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n",
99 i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
100
101 {
102 grub_relocator_chunk_t ch;
103 err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
104 &ch, phdr(i)->p_paddr,
105 phdr(i)->p_memsz);
106 if (err)
107 {
108 grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i);
109 return err;
110 }
111 source = get_virtual_current_address (ch);
112 }
113
114 if (phdr(i)->p_filesz != 0)
115 {
116 if (grub_file_seek (file, (grub_off_t) phdr(i)->p_offset)
117 == (grub_off_t) -1)
118 return grub_error (GRUB_ERR_BAD_OS,
119 "invalid offset in program header");
120
121 if (grub_file_read (file, source, phdr(i)->p_filesz)
122 != (grub_ssize_t) phdr(i)->p_filesz)
123 return grub_error (GRUB_ERR_BAD_OS,
124 "couldn't read segment from file");
125 }
126
127 if (phdr(i)->p_filesz < phdr(i)->p_memsz)
128 grub_memset ((grub_uint8_t *) source + phdr(i)->p_filesz, 0,
129 phdr(i)->p_memsz - phdr(i)->p_filesz);
130 }
131 }
132
133 for (i = 0; i < ehdr->e_phnum; i++)
134 if (phdr(i)->p_vaddr <= ehdr->e_entry
135 && phdr(i)->p_vaddr + phdr(i)->p_memsz > ehdr->e_entry)
136 {
137 grub_multiboot_payload_eip = (ehdr->e_entry - phdr(i)->p_vaddr)
138 + phdr(i)->p_paddr;
139 break;
140 }
141
142 if (i == ehdr->e_phnum)
143 return grub_error (GRUB_ERR_BAD_OS, "entry point isn't in a segment");
144
145 #if defined (__i386__) || defined (__x86_64__)
146
147 #elif defined (__mips)
148 grub_multiboot_payload_eip |= 0x80000000;
149 #else
150 #error Please complete this
151 #endif
152
153 if (ehdr->e_shnum)
154 {
155 grub_uint8_t *shdr, *shdrptr;
156
157 shdr = grub_malloc (ehdr->e_shnum * ehdr->e_shentsize);
158 if (!shdr)
159 return grub_errno;
160
161 if (grub_file_seek (file, ehdr->e_shoff) == (grub_off_t) -1)
162 return grub_error (GRUB_ERR_BAD_OS,
163 "invalid offset to section headers");
164
165 if (grub_file_read (file, shdr, ehdr->e_shnum * ehdr->e_shentsize)
166 != (grub_ssize_t) ehdr->e_shnum * ehdr->e_shentsize)
167 return grub_error (GRUB_ERR_BAD_OS,
168 "couldn't read sections headers from file");
169
170 for (shdrptr = shdr, i = 0; i < ehdr->e_shnum;
171 shdrptr += ehdr->e_shentsize, i++)
172 {
173 Elf_Shdr *sh = (Elf_Shdr *) shdrptr;
174 void *src;
175 grub_addr_t target;
176 grub_err_t err;
177
178 /* This section is a loaded section,
179 so we don't care. */
180 if (sh->sh_addr != 0)
181 continue;
182
183 /* This section is empty, so we don't care. */
184 if (sh->sh_size == 0)
185 continue;
186
187 {
188 grub_relocator_chunk_t ch;
189 err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator,
190 &ch, 0,
191 (0xffffffff - sh->sh_size)
192 + 1, sh->sh_size,
193 sh->sh_addralign,
194 GRUB_RELOCATOR_PREFERENCE_NONE);
195 if (err)
196 {
197 grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i);
198 return err;
199 }
200 src = get_virtual_current_address (ch);
201 target = get_physical_target_address (ch);
202 }
203
204 if (grub_file_seek (file, sh->sh_offset) == (grub_off_t) -1)
205 return grub_error (GRUB_ERR_BAD_OS,
206 "invalid offset in section header");
207
208 if (grub_file_read (file, src, sh->sh_size)
209 != (grub_ssize_t) sh->sh_size)
210 return grub_error (GRUB_ERR_BAD_OS,
211 "couldn't read segment from file");
212 sh->sh_addr = target;
213 }
214 grub_multiboot_add_elfsyms (ehdr->e_shnum, ehdr->e_shentsize,
215 ehdr->e_shstrndx, shdr);
216 }
217
218 #undef phdr
219
220 return grub_errno;
221 }
222
223 #undef XX
224 #undef E_MACHINE
225 #undef ELFCLASSXX
226 #undef Elf_Ehdr
227 #undef Elf_Phdr
228 #undef Elf_Shdr