]>
Commit | Line | Data |
---|---|---|
5fe141fd FB |
1 | /* |
2 | * QEMU Executable loader | |
5fafdf24 | 3 | * |
5fe141fd | 4 | * Copyright (c) 2006 Fabrice Bellard |
5fafdf24 | 5 | * |
5fe141fd FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
5a123577 AL |
23 | * |
24 | * Gunzip functionality in this file is derived from u-boot: | |
25 | * | |
26 | * (C) Copyright 2008 Semihalf | |
27 | * | |
28 | * (C) Copyright 2000-2005 | |
29 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
30 | * | |
31 | * This program is free software; you can redistribute it and/or | |
32 | * modify it under the terms of the GNU General Public License as | |
33 | * published by the Free Software Foundation; either version 2 of | |
34 | * the License, or (at your option) any later version. | |
35 | * | |
36 | * This program is distributed in the hope that it will be useful, | |
37 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
38 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
39 | * GNU General Public License for more details. | |
40 | * | |
fad6cb1a AJ |
41 | * You should have received a copy of the GNU General Public License along |
42 | * with this program; if not, write to the Free Software Foundation, Inc., | |
43 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
5fe141fd | 44 | */ |
5a123577 | 45 | |
87ecb68b | 46 | #include "qemu-common.h" |
5fe141fd | 47 | #include "disas.h" |
9596ebb7 | 48 | #include "sysemu.h" |
1c7b3754 | 49 | #include "uboot_image.h" |
5fe141fd | 50 | |
5a123577 AL |
51 | #include <zlib.h> |
52 | ||
5fe141fd FB |
53 | /* return the size or -1 if error */ |
54 | int get_image_size(const char *filename) | |
55 | { | |
56 | int fd, size; | |
57 | fd = open(filename, O_RDONLY | O_BINARY); | |
58 | if (fd < 0) | |
59 | return -1; | |
60 | size = lseek(fd, 0, SEEK_END); | |
61 | close(fd); | |
62 | return size; | |
63 | } | |
64 | ||
65 | /* return the size or -1 if error */ | |
293f78bc | 66 | /* deprecated, because caller does not specify buffer size! */ |
5fe141fd FB |
67 | int load_image(const char *filename, uint8_t *addr) |
68 | { | |
69 | int fd, size; | |
70 | fd = open(filename, O_RDONLY | O_BINARY); | |
71 | if (fd < 0) | |
72 | return -1; | |
73 | size = lseek(fd, 0, SEEK_END); | |
74 | lseek(fd, 0, SEEK_SET); | |
75 | if (read(fd, addr, size) != size) { | |
76 | close(fd); | |
77 | return -1; | |
78 | } | |
79 | close(fd); | |
80 | return size; | |
81 | } | |
82 | ||
293f78bc BS |
83 | /* return the amount read, just like fread. 0 may mean error or eof */ |
84 | int fread_targphys(target_phys_addr_t dst_addr, size_t nbytes, FILE *f) | |
85 | { | |
86 | uint8_t buf[4096]; | |
87 | target_phys_addr_t dst_begin = dst_addr; | |
88 | size_t want, did; | |
89 | ||
90 | while (nbytes) { | |
91 | want = nbytes > sizeof(buf) ? sizeof(buf) : nbytes; | |
92 | did = fread(buf, 1, want, f); | |
93 | if (did != want) break; | |
94 | ||
95 | cpu_physical_memory_write_rom(dst_addr, buf, did); | |
96 | dst_addr += did; | |
97 | nbytes -= did; | |
98 | } | |
99 | return dst_addr - dst_begin; | |
100 | } | |
101 | ||
102 | /* returns 0 on error, 1 if ok */ | |
103 | int fread_targphys_ok(target_phys_addr_t dst_addr, size_t nbytes, FILE *f) | |
104 | { | |
105 | return fread_targphys(dst_addr, nbytes, f) == nbytes; | |
106 | } | |
107 | ||
108 | /* read()-like version */ | |
109 | int read_targphys(int fd, target_phys_addr_t dst_addr, size_t nbytes) | |
110 | { | |
111 | uint8_t buf[4096]; | |
112 | target_phys_addr_t dst_begin = dst_addr; | |
113 | size_t want, did; | |
114 | ||
115 | while (nbytes) { | |
116 | want = nbytes > sizeof(buf) ? sizeof(buf) : nbytes; | |
117 | did = read(fd, buf, want); | |
118 | if (did != want) break; | |
119 | ||
120 | cpu_physical_memory_write_rom(dst_addr, buf, did); | |
121 | dst_addr += did; | |
122 | nbytes -= did; | |
123 | } | |
124 | return dst_addr - dst_begin; | |
125 | } | |
126 | ||
127 | /* return the size or -1 if error */ | |
128 | int load_image_targphys(const char *filename, | |
129 | target_phys_addr_t addr, int max_sz) | |
130 | { | |
131 | FILE *f; | |
132 | size_t got; | |
133 | ||
134 | f = fopen(filename, "rb"); | |
135 | if (!f) return -1; | |
136 | ||
137 | got = fread_targphys(addr, max_sz, f); | |
138 | if (ferror(f)) { fclose(f); return -1; } | |
139 | fclose(f); | |
140 | ||
141 | return got; | |
142 | } | |
143 | ||
144 | void pstrcpy_targphys(target_phys_addr_t dest, int buf_size, | |
145 | const char *source) | |
146 | { | |
147 | static const uint8_t nul_byte = 0; | |
148 | const char *nulp; | |
149 | ||
150 | if (buf_size <= 0) return; | |
151 | nulp = memchr(source, 0, buf_size); | |
152 | if (nulp) { | |
153 | cpu_physical_memory_write_rom(dest, (uint8_t *)source, | |
154 | (nulp - source) + 1); | |
155 | } else { | |
156 | cpu_physical_memory_write_rom(dest, (uint8_t *)source, buf_size - 1); | |
157 | cpu_physical_memory_write_rom(dest, &nul_byte, 1); | |
158 | } | |
159 | } | |
160 | ||
5fe141fd FB |
161 | /* A.OUT loader */ |
162 | ||
163 | struct exec | |
164 | { | |
165 | uint32_t a_info; /* Use macros N_MAGIC, etc for access */ | |
166 | uint32_t a_text; /* length of text, in bytes */ | |
167 | uint32_t a_data; /* length of data, in bytes */ | |
168 | uint32_t a_bss; /* length of uninitialized data area, in bytes */ | |
169 | uint32_t a_syms; /* length of symbol table data in file, in bytes */ | |
170 | uint32_t a_entry; /* start address */ | |
171 | uint32_t a_trsize; /* length of relocation info for text, in bytes */ | |
172 | uint32_t a_drsize; /* length of relocation info for data, in bytes */ | |
173 | }; | |
174 | ||
175 | #ifdef BSWAP_NEEDED | |
176 | static void bswap_ahdr(struct exec *e) | |
177 | { | |
178 | bswap32s(&e->a_info); | |
179 | bswap32s(&e->a_text); | |
180 | bswap32s(&e->a_data); | |
181 | bswap32s(&e->a_bss); | |
182 | bswap32s(&e->a_syms); | |
183 | bswap32s(&e->a_entry); | |
184 | bswap32s(&e->a_trsize); | |
185 | bswap32s(&e->a_drsize); | |
186 | } | |
187 | #else | |
188 | #define bswap_ahdr(x) do { } while (0) | |
189 | #endif | |
190 | ||
191 | #define N_MAGIC(exec) ((exec).a_info & 0xffff) | |
192 | #define OMAGIC 0407 | |
193 | #define NMAGIC 0410 | |
194 | #define ZMAGIC 0413 | |
195 | #define QMAGIC 0314 | |
196 | #define _N_HDROFF(x) (1024 - sizeof (struct exec)) | |
197 | #define N_TXTOFF(x) \ | |
198 | (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \ | |
199 | (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec))) | |
200 | #define N_TXTADDR(x) (N_MAGIC(x) == QMAGIC ? TARGET_PAGE_SIZE : 0) | |
201 | #define N_DATOFF(x) (N_TXTOFF(x) + (x).a_text) | |
202 | #define _N_SEGMENT_ROUND(x) (((x) + TARGET_PAGE_SIZE - 1) & ~(TARGET_PAGE_SIZE - 1)) | |
203 | ||
204 | #define _N_TXTENDADDR(x) (N_TXTADDR(x)+(x).a_text) | |
205 | ||
206 | #define N_DATADDR(x) \ | |
207 | (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x)) \ | |
208 | : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x)))) | |
209 | ||
210 | ||
293f78bc | 211 | int load_aout(const char *filename, target_phys_addr_t addr, int max_sz) |
5fe141fd FB |
212 | { |
213 | int fd, size, ret; | |
214 | struct exec e; | |
215 | uint32_t magic; | |
216 | ||
217 | fd = open(filename, O_RDONLY | O_BINARY); | |
218 | if (fd < 0) | |
219 | return -1; | |
220 | ||
221 | size = read(fd, &e, sizeof(e)); | |
222 | if (size < 0) | |
223 | goto fail; | |
224 | ||
225 | bswap_ahdr(&e); | |
226 | ||
227 | magic = N_MAGIC(e); | |
228 | switch (magic) { | |
229 | case ZMAGIC: | |
230 | case QMAGIC: | |
231 | case OMAGIC: | |
293f78bc BS |
232 | if (e.a_text + e.a_data > max_sz) |
233 | goto fail; | |
5fe141fd | 234 | lseek(fd, N_TXTOFF(e), SEEK_SET); |
293f78bc | 235 | size = read_targphys(fd, addr, e.a_text + e.a_data); |
5fe141fd FB |
236 | if (size < 0) |
237 | goto fail; | |
238 | break; | |
239 | case NMAGIC: | |
293f78bc BS |
240 | if (N_DATADDR(e) + e.a_data > max_sz) |
241 | goto fail; | |
5fe141fd | 242 | lseek(fd, N_TXTOFF(e), SEEK_SET); |
293f78bc | 243 | size = read_targphys(fd, addr, e.a_text); |
5fe141fd FB |
244 | if (size < 0) |
245 | goto fail; | |
293f78bc | 246 | ret = read_targphys(fd, addr + N_DATADDR(e), e.a_data); |
5fe141fd FB |
247 | if (ret < 0) |
248 | goto fail; | |
249 | size += ret; | |
250 | break; | |
251 | default: | |
252 | goto fail; | |
253 | } | |
254 | close(fd); | |
255 | return size; | |
256 | fail: | |
257 | close(fd); | |
258 | return -1; | |
259 | } | |
260 | ||
261 | /* ELF loader */ | |
262 | ||
263 | static void *load_at(int fd, int offset, int size) | |
264 | { | |
265 | void *ptr; | |
266 | if (lseek(fd, offset, SEEK_SET) < 0) | |
267 | return NULL; | |
268 | ptr = qemu_malloc(size); | |
269 | if (!ptr) | |
270 | return NULL; | |
271 | if (read(fd, ptr, size) != size) { | |
272 | qemu_free(ptr); | |
273 | return NULL; | |
274 | } | |
275 | return ptr; | |
276 | } | |
277 | ||
278 | ||
279 | #define ELF_CLASS ELFCLASS32 | |
280 | #include "elf.h" | |
281 | ||
282 | #define SZ 32 | |
283 | #define elf_word uint32_t | |
82790064 | 284 | #define elf_sword int32_t |
5fe141fd FB |
285 | #define bswapSZs bswap32s |
286 | #include "elf_ops.h" | |
287 | ||
288 | #undef elfhdr | |
289 | #undef elf_phdr | |
290 | #undef elf_shdr | |
291 | #undef elf_sym | |
292 | #undef elf_note | |
293 | #undef elf_word | |
82790064 | 294 | #undef elf_sword |
5fe141fd FB |
295 | #undef bswapSZs |
296 | #undef SZ | |
297 | #define elfhdr elf64_hdr | |
298 | #define elf_phdr elf64_phdr | |
299 | #define elf_note elf64_note | |
300 | #define elf_shdr elf64_shdr | |
301 | #define elf_sym elf64_sym | |
302 | #define elf_word uint64_t | |
82790064 | 303 | #define elf_sword int64_t |
5fe141fd FB |
304 | #define bswapSZs bswap64s |
305 | #define SZ 64 | |
306 | #include "elf_ops.h" | |
307 | ||
308 | /* return < 0 if error, otherwise the number of bytes loaded in memory */ | |
83c1f87c | 309 | int load_elf(const char *filename, int64_t address_offset, |
74287114 | 310 | uint64_t *pentry, uint64_t *lowaddr, uint64_t *highaddr) |
5fe141fd | 311 | { |
9042c0e2 | 312 | int fd, data_order, host_data_order, must_swab, ret; |
5fe141fd FB |
313 | uint8_t e_ident[EI_NIDENT]; |
314 | ||
699e4642 | 315 | fd = open(filename, O_RDONLY | O_BINARY); |
5fe141fd FB |
316 | if (fd < 0) { |
317 | perror(filename); | |
318 | return -1; | |
319 | } | |
320 | if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident)) | |
321 | goto fail; | |
322 | if (e_ident[0] != ELFMAG0 || | |
323 | e_ident[1] != ELFMAG1 || | |
324 | e_ident[2] != ELFMAG2 || | |
325 | e_ident[3] != ELFMAG3) | |
326 | goto fail; | |
327 | #ifdef WORDS_BIGENDIAN | |
328 | data_order = ELFDATA2MSB; | |
329 | #else | |
330 | data_order = ELFDATA2LSB; | |
331 | #endif | |
332 | must_swab = data_order != e_ident[EI_DATA]; | |
9042c0e2 TS |
333 | |
334 | #ifdef TARGET_WORDS_BIGENDIAN | |
335 | host_data_order = ELFDATA2MSB; | |
336 | #else | |
337 | host_data_order = ELFDATA2LSB; | |
338 | #endif | |
339 | if (host_data_order != e_ident[EI_DATA]) | |
340 | return -1; | |
341 | ||
5fe141fd FB |
342 | lseek(fd, 0, SEEK_SET); |
343 | if (e_ident[EI_CLASS] == ELFCLASS64) { | |
83c1f87c | 344 | ret = load_elf64(fd, address_offset, must_swab, pentry, |
74287114 | 345 | lowaddr, highaddr); |
5fe141fd | 346 | } else { |
83c1f87c | 347 | ret = load_elf32(fd, address_offset, must_swab, pentry, |
74287114 | 348 | lowaddr, highaddr); |
5fe141fd FB |
349 | } |
350 | ||
351 | close(fd); | |
352 | return ret; | |
353 | ||
354 | fail: | |
355 | close(fd); | |
356 | return -1; | |
357 | } | |
1c7b3754 PB |
358 | |
359 | static void bswap_uboot_header(uboot_image_header_t *hdr) | |
360 | { | |
361 | #ifndef WORDS_BIGENDIAN | |
362 | bswap32s(&hdr->ih_magic); | |
363 | bswap32s(&hdr->ih_hcrc); | |
364 | bswap32s(&hdr->ih_time); | |
365 | bswap32s(&hdr->ih_size); | |
366 | bswap32s(&hdr->ih_load); | |
367 | bswap32s(&hdr->ih_ep); | |
368 | bswap32s(&hdr->ih_dcrc); | |
369 | #endif | |
370 | } | |
371 | ||
5a123577 AL |
372 | |
373 | #define ZALLOC_ALIGNMENT 16 | |
374 | ||
375 | static void *zalloc(void *x, unsigned items, unsigned size) | |
376 | { | |
377 | void *p; | |
378 | ||
379 | size *= items; | |
380 | size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1); | |
381 | ||
382 | p = qemu_malloc(size); | |
383 | ||
384 | return (p); | |
385 | } | |
386 | ||
387 | static void zfree(void *x, void *addr, unsigned nb) | |
388 | { | |
389 | qemu_free(addr); | |
390 | } | |
391 | ||
392 | ||
393 | #define HEAD_CRC 2 | |
394 | #define EXTRA_FIELD 4 | |
395 | #define ORIG_NAME 8 | |
396 | #define COMMENT 0x10 | |
397 | #define RESERVED 0xe0 | |
398 | ||
399 | #define DEFLATED 8 | |
400 | ||
401 | /* This is the maximum in uboot, so if a uImage overflows this, it would | |
402 | * overflow on real hardware too. */ | |
403 | #define UBOOT_MAX_GUNZIP_BYTES 0x800000 | |
404 | ||
405 | static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, | |
406 | size_t srclen) | |
407 | { | |
408 | z_stream s; | |
409 | ssize_t dstbytes; | |
410 | int r, i, flags; | |
411 | ||
412 | /* skip header */ | |
413 | i = 10; | |
414 | flags = src[3]; | |
415 | if (src[2] != DEFLATED || (flags & RESERVED) != 0) { | |
416 | puts ("Error: Bad gzipped data\n"); | |
417 | return -1; | |
418 | } | |
419 | if ((flags & EXTRA_FIELD) != 0) | |
420 | i = 12 + src[10] + (src[11] << 8); | |
421 | if ((flags & ORIG_NAME) != 0) | |
422 | while (src[i++] != 0) | |
423 | ; | |
424 | if ((flags & COMMENT) != 0) | |
425 | while (src[i++] != 0) | |
426 | ; | |
427 | if ((flags & HEAD_CRC) != 0) | |
428 | i += 2; | |
429 | if (i >= srclen) { | |
430 | puts ("Error: gunzip out of data in header\n"); | |
431 | return -1; | |
432 | } | |
433 | ||
434 | s.zalloc = zalloc; | |
435 | s.zfree = (free_func)zfree; | |
436 | ||
437 | r = inflateInit2(&s, -MAX_WBITS); | |
438 | if (r != Z_OK) { | |
439 | printf ("Error: inflateInit2() returned %d\n", r); | |
440 | return (-1); | |
441 | } | |
442 | s.next_in = src + i; | |
443 | s.avail_in = srclen - i; | |
444 | s.next_out = dst; | |
445 | s.avail_out = dstlen; | |
446 | r = inflate(&s, Z_FINISH); | |
447 | if (r != Z_OK && r != Z_STREAM_END) { | |
448 | printf ("Error: inflate() returned %d\n", r); | |
449 | return -1; | |
450 | } | |
451 | dstbytes = s.next_out - (unsigned char *) dst; | |
452 | inflateEnd(&s); | |
453 | ||
454 | return dstbytes; | |
455 | } | |
456 | ||
1c7b3754 | 457 | /* Load a U-Boot image. */ |
5a9154e0 AL |
458 | int load_uimage(const char *filename, target_ulong *ep, target_ulong *loadaddr, |
459 | int *is_linux) | |
1c7b3754 | 460 | { |
1c7b3754 PB |
461 | int fd; |
462 | int size; | |
463 | uboot_image_header_t h; | |
464 | uboot_image_header_t *hdr = &h; | |
465 | uint8_t *data = NULL; | |
265ca29a | 466 | int ret = -1; |
1c7b3754 PB |
467 | |
468 | fd = open(filename, O_RDONLY | O_BINARY); | |
469 | if (fd < 0) | |
470 | return -1; | |
471 | ||
472 | size = read(fd, hdr, sizeof(uboot_image_header_t)); | |
473 | if (size < 0) | |
265ca29a | 474 | goto out; |
1c7b3754 PB |
475 | |
476 | bswap_uboot_header(hdr); | |
477 | ||
478 | if (hdr->ih_magic != IH_MAGIC) | |
265ca29a | 479 | goto out; |
1c7b3754 | 480 | |
7e5f90fa AL |
481 | /* TODO: Implement other image types. */ |
482 | if (hdr->ih_type != IH_TYPE_KERNEL) { | |
483 | fprintf(stderr, "Can only load u-boot image type \"kernel\"\n"); | |
265ca29a | 484 | goto out; |
1c7b3754 PB |
485 | } |
486 | ||
5a123577 AL |
487 | switch (hdr->ih_comp) { |
488 | case IH_COMP_NONE: | |
489 | case IH_COMP_GZIP: | |
490 | break; | |
491 | default: | |
492 | fprintf(stderr, | |
493 | "Unable to load u-boot images with compression type %d\n", | |
494 | hdr->ih_comp); | |
265ca29a | 495 | goto out; |
1c7b3754 PB |
496 | } |
497 | ||
498 | /* TODO: Check CPU type. */ | |
499 | if (is_linux) { | |
7e5f90fa | 500 | if (hdr->ih_os == IH_OS_LINUX) |
1c7b3754 PB |
501 | *is_linux = 1; |
502 | else | |
503 | *is_linux = 0; | |
504 | } | |
505 | ||
506 | *ep = hdr->ih_ep; | |
507 | data = qemu_malloc(hdr->ih_size); | |
508 | if (!data) | |
265ca29a | 509 | goto out; |
1c7b3754 PB |
510 | |
511 | if (read(fd, data, hdr->ih_size) != hdr->ih_size) { | |
512 | fprintf(stderr, "Error reading file\n"); | |
265ca29a | 513 | goto out; |
1c7b3754 PB |
514 | } |
515 | ||
5a123577 AL |
516 | if (hdr->ih_comp == IH_COMP_GZIP) { |
517 | uint8_t *compressed_data; | |
518 | size_t max_bytes; | |
519 | ssize_t bytes; | |
520 | ||
521 | compressed_data = data; | |
522 | max_bytes = UBOOT_MAX_GUNZIP_BYTES; | |
523 | data = qemu_malloc(max_bytes); | |
524 | ||
525 | bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size); | |
526 | qemu_free(compressed_data); | |
527 | if (bytes < 0) { | |
528 | fprintf(stderr, "Unable to decompress gzipped image!\n"); | |
529 | goto out; | |
530 | } | |
531 | hdr->ih_size = bytes; | |
532 | } | |
533 | ||
1c7b3754 PB |
534 | cpu_physical_memory_write_rom(hdr->ih_load, data, hdr->ih_size); |
535 | ||
21cafd08 AL |
536 | if (loadaddr) |
537 | *loadaddr = hdr->ih_load; | |
538 | ||
265ca29a | 539 | ret = hdr->ih_size; |
1c7b3754 | 540 | |
265ca29a | 541 | out: |
1c7b3754 PB |
542 | if (data) |
543 | qemu_free(data); | |
544 | close(fd); | |
265ca29a | 545 | return ret; |
1c7b3754 | 546 | } |