root / loader.c @ cbeb0857
History | View | Annotate | Download (8 kB)
1 | 5fe141fd | bellard | /*
|
---|---|---|---|
2 | 5fe141fd | bellard | * QEMU Executable loader
|
3 | 5fe141fd | bellard | *
|
4 | 5fe141fd | bellard | * Copyright (c) 2006 Fabrice Bellard
|
5 | 5fe141fd | bellard | *
|
6 | 5fe141fd | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 5fe141fd | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 5fe141fd | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 5fe141fd | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 5fe141fd | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 5fe141fd | bellard | * furnished to do so, subject to the following conditions:
|
12 | 5fe141fd | bellard | *
|
13 | 5fe141fd | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 5fe141fd | bellard | * all copies or substantial portions of the Software.
|
15 | 5fe141fd | bellard | *
|
16 | 5fe141fd | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 5fe141fd | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 5fe141fd | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 5fe141fd | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 5fe141fd | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 5fe141fd | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 5fe141fd | bellard | * THE SOFTWARE.
|
23 | 5fe141fd | bellard | */
|
24 | 5fe141fd | bellard | #include "vl.h" |
25 | 5fe141fd | bellard | #include "disas.h" |
26 | 1c7b3754 | pbrook | #include "uboot_image.h" |
27 | 5fe141fd | bellard | |
28 | 5fe141fd | bellard | /* return the size or -1 if error */
|
29 | 5fe141fd | bellard | int get_image_size(const char *filename) |
30 | 5fe141fd | bellard | { |
31 | 5fe141fd | bellard | int fd, size;
|
32 | 5fe141fd | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
33 | 5fe141fd | bellard | if (fd < 0) |
34 | 5fe141fd | bellard | return -1; |
35 | 5fe141fd | bellard | size = lseek(fd, 0, SEEK_END);
|
36 | 5fe141fd | bellard | close(fd); |
37 | 5fe141fd | bellard | return size;
|
38 | 5fe141fd | bellard | } |
39 | 5fe141fd | bellard | |
40 | 5fe141fd | bellard | /* return the size or -1 if error */
|
41 | 5fe141fd | bellard | int load_image(const char *filename, uint8_t *addr) |
42 | 5fe141fd | bellard | { |
43 | 5fe141fd | bellard | int fd, size;
|
44 | 5fe141fd | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
45 | 5fe141fd | bellard | if (fd < 0) |
46 | 5fe141fd | bellard | return -1; |
47 | 5fe141fd | bellard | size = lseek(fd, 0, SEEK_END);
|
48 | 5fe141fd | bellard | lseek(fd, 0, SEEK_SET);
|
49 | 5fe141fd | bellard | if (read(fd, addr, size) != size) {
|
50 | 5fe141fd | bellard | close(fd); |
51 | 5fe141fd | bellard | return -1; |
52 | 5fe141fd | bellard | } |
53 | 5fe141fd | bellard | close(fd); |
54 | 5fe141fd | bellard | return size;
|
55 | 5fe141fd | bellard | } |
56 | 5fe141fd | bellard | |
57 | 5fe141fd | bellard | /* A.OUT loader */
|
58 | 5fe141fd | bellard | |
59 | 5fe141fd | bellard | struct exec
|
60 | 5fe141fd | bellard | { |
61 | 5fe141fd | bellard | uint32_t a_info; /* Use macros N_MAGIC, etc for access */
|
62 | 5fe141fd | bellard | uint32_t a_text; /* length of text, in bytes */
|
63 | 5fe141fd | bellard | uint32_t a_data; /* length of data, in bytes */
|
64 | 5fe141fd | bellard | uint32_t a_bss; /* length of uninitialized data area, in bytes */
|
65 | 5fe141fd | bellard | uint32_t a_syms; /* length of symbol table data in file, in bytes */
|
66 | 5fe141fd | bellard | uint32_t a_entry; /* start address */
|
67 | 5fe141fd | bellard | uint32_t a_trsize; /* length of relocation info for text, in bytes */
|
68 | 5fe141fd | bellard | uint32_t a_drsize; /* length of relocation info for data, in bytes */
|
69 | 5fe141fd | bellard | }; |
70 | 5fe141fd | bellard | |
71 | 5fe141fd | bellard | #ifdef BSWAP_NEEDED
|
72 | 5fe141fd | bellard | static void bswap_ahdr(struct exec *e) |
73 | 5fe141fd | bellard | { |
74 | 5fe141fd | bellard | bswap32s(&e->a_info); |
75 | 5fe141fd | bellard | bswap32s(&e->a_text); |
76 | 5fe141fd | bellard | bswap32s(&e->a_data); |
77 | 5fe141fd | bellard | bswap32s(&e->a_bss); |
78 | 5fe141fd | bellard | bswap32s(&e->a_syms); |
79 | 5fe141fd | bellard | bswap32s(&e->a_entry); |
80 | 5fe141fd | bellard | bswap32s(&e->a_trsize); |
81 | 5fe141fd | bellard | bswap32s(&e->a_drsize); |
82 | 5fe141fd | bellard | } |
83 | 5fe141fd | bellard | #else
|
84 | 5fe141fd | bellard | #define bswap_ahdr(x) do { } while (0) |
85 | 5fe141fd | bellard | #endif
|
86 | 5fe141fd | bellard | |
87 | 5fe141fd | bellard | #define N_MAGIC(exec) ((exec).a_info & 0xffff) |
88 | 5fe141fd | bellard | #define OMAGIC 0407 |
89 | 5fe141fd | bellard | #define NMAGIC 0410 |
90 | 5fe141fd | bellard | #define ZMAGIC 0413 |
91 | 5fe141fd | bellard | #define QMAGIC 0314 |
92 | 5fe141fd | bellard | #define _N_HDROFF(x) (1024 - sizeof (struct exec)) |
93 | 5fe141fd | bellard | #define N_TXTOFF(x) \
|
94 | 5fe141fd | bellard | (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \ |
95 | 5fe141fd | bellard | (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec))) |
96 | 5fe141fd | bellard | #define N_TXTADDR(x) (N_MAGIC(x) == QMAGIC ? TARGET_PAGE_SIZE : 0) |
97 | 5fe141fd | bellard | #define N_DATOFF(x) (N_TXTOFF(x) + (x).a_text)
|
98 | 5fe141fd | bellard | #define _N_SEGMENT_ROUND(x) (((x) + TARGET_PAGE_SIZE - 1) & ~(TARGET_PAGE_SIZE - 1)) |
99 | 5fe141fd | bellard | |
100 | 5fe141fd | bellard | #define _N_TXTENDADDR(x) (N_TXTADDR(x)+(x).a_text)
|
101 | 5fe141fd | bellard | |
102 | 5fe141fd | bellard | #define N_DATADDR(x) \
|
103 | 5fe141fd | bellard | (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x)) \ |
104 | 5fe141fd | bellard | : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x)))) |
105 | 5fe141fd | bellard | |
106 | 5fe141fd | bellard | |
107 | 5fe141fd | bellard | int load_aout(const char *filename, uint8_t *addr) |
108 | 5fe141fd | bellard | { |
109 | 5fe141fd | bellard | int fd, size, ret;
|
110 | 5fe141fd | bellard | struct exec e;
|
111 | 5fe141fd | bellard | uint32_t magic; |
112 | 5fe141fd | bellard | |
113 | 5fe141fd | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
114 | 5fe141fd | bellard | if (fd < 0) |
115 | 5fe141fd | bellard | return -1; |
116 | 5fe141fd | bellard | |
117 | 5fe141fd | bellard | size = read(fd, &e, sizeof(e));
|
118 | 5fe141fd | bellard | if (size < 0) |
119 | 5fe141fd | bellard | goto fail;
|
120 | 5fe141fd | bellard | |
121 | 5fe141fd | bellard | bswap_ahdr(&e); |
122 | 5fe141fd | bellard | |
123 | 5fe141fd | bellard | magic = N_MAGIC(e); |
124 | 5fe141fd | bellard | switch (magic) {
|
125 | 5fe141fd | bellard | case ZMAGIC:
|
126 | 5fe141fd | bellard | case QMAGIC:
|
127 | 5fe141fd | bellard | case OMAGIC:
|
128 | 5fe141fd | bellard | lseek(fd, N_TXTOFF(e), SEEK_SET); |
129 | 5fe141fd | bellard | size = read(fd, addr, e.a_text + e.a_data); |
130 | 5fe141fd | bellard | if (size < 0) |
131 | 5fe141fd | bellard | goto fail;
|
132 | 5fe141fd | bellard | break;
|
133 | 5fe141fd | bellard | case NMAGIC:
|
134 | 5fe141fd | bellard | lseek(fd, N_TXTOFF(e), SEEK_SET); |
135 | 5fe141fd | bellard | size = read(fd, addr, e.a_text); |
136 | 5fe141fd | bellard | if (size < 0) |
137 | 5fe141fd | bellard | goto fail;
|
138 | 5fe141fd | bellard | ret = read(fd, addr + N_DATADDR(e), e.a_data); |
139 | 5fe141fd | bellard | if (ret < 0) |
140 | 5fe141fd | bellard | goto fail;
|
141 | 5fe141fd | bellard | size += ret; |
142 | 5fe141fd | bellard | break;
|
143 | 5fe141fd | bellard | default:
|
144 | 5fe141fd | bellard | goto fail;
|
145 | 5fe141fd | bellard | } |
146 | 5fe141fd | bellard | close(fd); |
147 | 5fe141fd | bellard | return size;
|
148 | 5fe141fd | bellard | fail:
|
149 | 5fe141fd | bellard | close(fd); |
150 | 5fe141fd | bellard | return -1; |
151 | 5fe141fd | bellard | } |
152 | 5fe141fd | bellard | |
153 | 5fe141fd | bellard | /* ELF loader */
|
154 | 5fe141fd | bellard | |
155 | 5fe141fd | bellard | static void *load_at(int fd, int offset, int size) |
156 | 5fe141fd | bellard | { |
157 | 5fe141fd | bellard | void *ptr;
|
158 | 5fe141fd | bellard | if (lseek(fd, offset, SEEK_SET) < 0) |
159 | 5fe141fd | bellard | return NULL; |
160 | 5fe141fd | bellard | ptr = qemu_malloc(size); |
161 | 5fe141fd | bellard | if (!ptr)
|
162 | 5fe141fd | bellard | return NULL; |
163 | 5fe141fd | bellard | if (read(fd, ptr, size) != size) {
|
164 | 5fe141fd | bellard | qemu_free(ptr); |
165 | 5fe141fd | bellard | return NULL; |
166 | 5fe141fd | bellard | } |
167 | 5fe141fd | bellard | return ptr;
|
168 | 5fe141fd | bellard | } |
169 | 5fe141fd | bellard | |
170 | 5fe141fd | bellard | |
171 | 5fe141fd | bellard | #define ELF_CLASS ELFCLASS32
|
172 | 5fe141fd | bellard | #include "elf.h" |
173 | 5fe141fd | bellard | |
174 | 5fe141fd | bellard | #define SZ 32 |
175 | 5fe141fd | bellard | #define elf_word uint32_t
|
176 | 5fe141fd | bellard | #define bswapSZs bswap32s
|
177 | 5fe141fd | bellard | #include "elf_ops.h" |
178 | 5fe141fd | bellard | |
179 | 5fe141fd | bellard | #undef elfhdr
|
180 | 5fe141fd | bellard | #undef elf_phdr
|
181 | 5fe141fd | bellard | #undef elf_shdr
|
182 | 5fe141fd | bellard | #undef elf_sym
|
183 | 5fe141fd | bellard | #undef elf_note
|
184 | 5fe141fd | bellard | #undef elf_word
|
185 | 5fe141fd | bellard | #undef bswapSZs
|
186 | 5fe141fd | bellard | #undef SZ
|
187 | 5fe141fd | bellard | #define elfhdr elf64_hdr
|
188 | 5fe141fd | bellard | #define elf_phdr elf64_phdr
|
189 | 5fe141fd | bellard | #define elf_note elf64_note
|
190 | 5fe141fd | bellard | #define elf_shdr elf64_shdr
|
191 | 5fe141fd | bellard | #define elf_sym elf64_sym
|
192 | 5fe141fd | bellard | #define elf_word uint64_t
|
193 | 5fe141fd | bellard | #define bswapSZs bswap64s
|
194 | 5fe141fd | bellard | #define SZ 64 |
195 | 5fe141fd | bellard | #include "elf_ops.h" |
196 | 5fe141fd | bellard | |
197 | 5fe141fd | bellard | /* return < 0 if error, otherwise the number of bytes loaded in memory */
|
198 | 9ee3c029 | bellard | int load_elf(const char *filename, int64_t virt_to_phys_addend, |
199 | 74287114 | ths | uint64_t *pentry, uint64_t *lowaddr, uint64_t *highaddr) |
200 | 5fe141fd | bellard | { |
201 | 9042c0e2 | ths | int fd, data_order, host_data_order, must_swab, ret;
|
202 | 5fe141fd | bellard | uint8_t e_ident[EI_NIDENT]; |
203 | 5fe141fd | bellard | |
204 | 699e4642 | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
205 | 5fe141fd | bellard | if (fd < 0) { |
206 | 5fe141fd | bellard | perror(filename); |
207 | 5fe141fd | bellard | return -1; |
208 | 5fe141fd | bellard | } |
209 | 5fe141fd | bellard | if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident)) |
210 | 5fe141fd | bellard | goto fail;
|
211 | 5fe141fd | bellard | if (e_ident[0] != ELFMAG0 || |
212 | 5fe141fd | bellard | e_ident[1] != ELFMAG1 ||
|
213 | 5fe141fd | bellard | e_ident[2] != ELFMAG2 ||
|
214 | 5fe141fd | bellard | e_ident[3] != ELFMAG3)
|
215 | 5fe141fd | bellard | goto fail;
|
216 | 5fe141fd | bellard | #ifdef WORDS_BIGENDIAN
|
217 | 5fe141fd | bellard | data_order = ELFDATA2MSB; |
218 | 5fe141fd | bellard | #else
|
219 | 5fe141fd | bellard | data_order = ELFDATA2LSB; |
220 | 5fe141fd | bellard | #endif
|
221 | 5fe141fd | bellard | must_swab = data_order != e_ident[EI_DATA]; |
222 | 9042c0e2 | ths | |
223 | 9042c0e2 | ths | #ifdef TARGET_WORDS_BIGENDIAN
|
224 | 9042c0e2 | ths | host_data_order = ELFDATA2MSB; |
225 | 9042c0e2 | ths | #else
|
226 | 9042c0e2 | ths | host_data_order = ELFDATA2LSB; |
227 | 9042c0e2 | ths | #endif
|
228 | 9042c0e2 | ths | if (host_data_order != e_ident[EI_DATA])
|
229 | 9042c0e2 | ths | return -1; |
230 | 9042c0e2 | ths | |
231 | 5fe141fd | bellard | lseek(fd, 0, SEEK_SET);
|
232 | 5fe141fd | bellard | if (e_ident[EI_CLASS] == ELFCLASS64) {
|
233 | 74287114 | ths | ret = load_elf64(fd, virt_to_phys_addend, must_swab, pentry, |
234 | 74287114 | ths | lowaddr, highaddr); |
235 | 5fe141fd | bellard | } else {
|
236 | 74287114 | ths | ret = load_elf32(fd, virt_to_phys_addend, must_swab, pentry, |
237 | 74287114 | ths | lowaddr, highaddr); |
238 | 5fe141fd | bellard | } |
239 | 5fe141fd | bellard | |
240 | 5fe141fd | bellard | close(fd); |
241 | 5fe141fd | bellard | return ret;
|
242 | 5fe141fd | bellard | |
243 | 5fe141fd | bellard | fail:
|
244 | 5fe141fd | bellard | close(fd); |
245 | 5fe141fd | bellard | return -1; |
246 | 5fe141fd | bellard | } |
247 | 1c7b3754 | pbrook | |
248 | 1c7b3754 | pbrook | static void bswap_uboot_header(uboot_image_header_t *hdr) |
249 | 1c7b3754 | pbrook | { |
250 | 1c7b3754 | pbrook | #ifndef WORDS_BIGENDIAN
|
251 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_magic); |
252 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_hcrc); |
253 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_time); |
254 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_size); |
255 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_load); |
256 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_ep); |
257 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_dcrc); |
258 | 1c7b3754 | pbrook | #endif
|
259 | 1c7b3754 | pbrook | } |
260 | 1c7b3754 | pbrook | |
261 | 1c7b3754 | pbrook | /* Load a U-Boot image. */
|
262 | 1c7b3754 | pbrook | int load_uboot(const char *filename, target_ulong *ep, int *is_linux) |
263 | 1c7b3754 | pbrook | { |
264 | 1c7b3754 | pbrook | |
265 | 1c7b3754 | pbrook | int fd;
|
266 | 1c7b3754 | pbrook | int size;
|
267 | 1c7b3754 | pbrook | uboot_image_header_t h; |
268 | 1c7b3754 | pbrook | uboot_image_header_t *hdr = &h; |
269 | 1c7b3754 | pbrook | uint8_t *data = NULL;
|
270 | 1c7b3754 | pbrook | |
271 | 1c7b3754 | pbrook | fd = open(filename, O_RDONLY | O_BINARY); |
272 | 1c7b3754 | pbrook | if (fd < 0) |
273 | 1c7b3754 | pbrook | return -1; |
274 | 1c7b3754 | pbrook | |
275 | 1c7b3754 | pbrook | size = read(fd, hdr, sizeof(uboot_image_header_t));
|
276 | 1c7b3754 | pbrook | if (size < 0) |
277 | 1c7b3754 | pbrook | goto fail;
|
278 | 1c7b3754 | pbrook | |
279 | 1c7b3754 | pbrook | bswap_uboot_header(hdr); |
280 | 1c7b3754 | pbrook | |
281 | 1c7b3754 | pbrook | if (hdr->ih_magic != IH_MAGIC)
|
282 | 1c7b3754 | pbrook | goto fail;
|
283 | 1c7b3754 | pbrook | |
284 | 1c7b3754 | pbrook | /* TODO: Implement Multi-File images. */
|
285 | 1c7b3754 | pbrook | if (hdr->ih_type == IH_TYPE_MULTI) {
|
286 | 1c7b3754 | pbrook | fprintf(stderr, "Unable to load multi-file u-boot images\n");
|
287 | 1c7b3754 | pbrook | goto fail;
|
288 | 1c7b3754 | pbrook | } |
289 | 1c7b3754 | pbrook | |
290 | 1c7b3754 | pbrook | /* TODO: Implement compressed images. */
|
291 | 1c7b3754 | pbrook | if (hdr->ih_comp != IH_COMP_NONE) {
|
292 | 1c7b3754 | pbrook | fprintf(stderr, "Unable to load compressed u-boot images\n");
|
293 | 1c7b3754 | pbrook | goto fail;
|
294 | 1c7b3754 | pbrook | } |
295 | 1c7b3754 | pbrook | |
296 | 1c7b3754 | pbrook | /* TODO: Check CPU type. */
|
297 | 1c7b3754 | pbrook | if (is_linux) {
|
298 | 1c7b3754 | pbrook | if (hdr->ih_type == IH_TYPE_KERNEL && hdr->ih_os == IH_OS_LINUX)
|
299 | 1c7b3754 | pbrook | *is_linux = 1;
|
300 | 1c7b3754 | pbrook | else
|
301 | 1c7b3754 | pbrook | *is_linux = 0;
|
302 | 1c7b3754 | pbrook | } |
303 | 1c7b3754 | pbrook | |
304 | 1c7b3754 | pbrook | *ep = hdr->ih_ep; |
305 | 1c7b3754 | pbrook | data = qemu_malloc(hdr->ih_size); |
306 | 1c7b3754 | pbrook | if (!data)
|
307 | 1c7b3754 | pbrook | goto fail;
|
308 | 1c7b3754 | pbrook | |
309 | 1c7b3754 | pbrook | if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
|
310 | 1c7b3754 | pbrook | fprintf(stderr, "Error reading file\n");
|
311 | 1c7b3754 | pbrook | goto fail;
|
312 | 1c7b3754 | pbrook | } |
313 | 1c7b3754 | pbrook | |
314 | 1c7b3754 | pbrook | cpu_physical_memory_write_rom(hdr->ih_load, data, hdr->ih_size); |
315 | 1c7b3754 | pbrook | |
316 | 1c7b3754 | pbrook | return hdr->ih_size;
|
317 | 1c7b3754 | pbrook | |
318 | 1c7b3754 | pbrook | fail:
|
319 | 1c7b3754 | pbrook | if (data)
|
320 | 1c7b3754 | pbrook | qemu_free(data); |
321 | 1c7b3754 | pbrook | close(fd); |
322 | 1c7b3754 | pbrook | return -1; |
323 | 1c7b3754 | pbrook | } |