Introduce pci_map_irq to get irq nr from pin number
[qemu-kvm/fedora.git] / loader.c
blobca8cdf005f21c635bbd3ffd0d8607e513127a4e8
1 /*
2 * QEMU Executable loader
4 * Copyright (c) 2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu-common.h"
25 #include "disas.h"
26 #include "sysemu.h"
27 #include "uboot_image.h"
29 #include <zlib.h>
31 /* return the size or -1 if error */
32 int get_image_size(const char *filename)
34 int fd, size;
35 fd = open(filename, O_RDONLY | O_BINARY);
36 if (fd < 0)
37 return -1;
38 size = lseek(fd, 0, SEEK_END);
39 close(fd);
40 return size;
43 /* return the size or -1 if error */
44 /* deprecated, because caller does not specify buffer size! */
45 int load_image(const char *filename, uint8_t *addr)
47 int fd, size;
48 fd = open(filename, O_RDONLY | O_BINARY);
49 if (fd < 0)
50 return -1;
51 size = lseek(fd, 0, SEEK_END);
52 lseek(fd, 0, SEEK_SET);
53 if (read(fd, addr, size) != size) {
54 close(fd);
55 return -1;
57 close(fd);
58 return size;
61 /* return the amount read, just like fread. 0 may mean error or eof */
62 int fread_targphys(target_phys_addr_t dst_addr, size_t nbytes, FILE *f)
64 uint8_t buf[4096];
65 target_phys_addr_t dst_begin = dst_addr;
66 size_t want, did;
68 while (nbytes) {
69 want = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
70 did = fread(buf, 1, want, f);
71 if (did != want) break;
73 cpu_physical_memory_write_rom(dst_addr, buf, did);
74 dst_addr += did;
75 nbytes -= did;
77 return dst_addr - dst_begin;
80 /* returns 0 on error, 1 if ok */
81 int fread_targphys_ok(target_phys_addr_t dst_addr, size_t nbytes, FILE *f)
83 return fread_targphys(dst_addr, nbytes, f) == nbytes;
86 /* read()-like version */
87 int read_targphys(int fd, target_phys_addr_t dst_addr, size_t nbytes)
89 uint8_t buf[4096];
90 target_phys_addr_t dst_begin = dst_addr;
91 size_t want, did;
93 while (nbytes) {
94 want = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
95 did = read(fd, buf, want);
96 if (did != want) break;
98 cpu_physical_memory_write_rom(dst_addr, buf, did);
99 dst_addr += did;
100 nbytes -= did;
102 return dst_addr - dst_begin;
105 /* return the size or -1 if error */
106 int load_image_targphys(const char *filename,
107 target_phys_addr_t addr, int max_sz)
109 FILE *f;
110 size_t got;
112 f = fopen(filename, "rb");
113 if (!f) return -1;
115 got = fread_targphys(addr, max_sz, f);
116 if (ferror(f)) { fclose(f); return -1; }
117 fclose(f);
119 return got;
122 void pstrcpy_targphys(target_phys_addr_t dest, int buf_size,
123 const char *source)
125 static const uint8_t nul_byte = 0;
126 const char *nulp;
128 if (buf_size <= 0) return;
129 nulp = memchr(source, 0, buf_size);
130 if (nulp) {
131 cpu_physical_memory_write_rom(dest, (uint8_t *)source,
132 (nulp - source) + 1);
133 } else {
134 cpu_physical_memory_write_rom(dest, (uint8_t *)source, buf_size - 1);
135 cpu_physical_memory_write_rom(dest, &nul_byte, 1);
139 /* A.OUT loader */
141 struct exec
143 uint32_t a_info; /* Use macros N_MAGIC, etc for access */
144 uint32_t a_text; /* length of text, in bytes */
145 uint32_t a_data; /* length of data, in bytes */
146 uint32_t a_bss; /* length of uninitialized data area, in bytes */
147 uint32_t a_syms; /* length of symbol table data in file, in bytes */
148 uint32_t a_entry; /* start address */
149 uint32_t a_trsize; /* length of relocation info for text, in bytes */
150 uint32_t a_drsize; /* length of relocation info for data, in bytes */
153 #ifdef BSWAP_NEEDED
154 static void bswap_ahdr(struct exec *e)
156 bswap32s(&e->a_info);
157 bswap32s(&e->a_text);
158 bswap32s(&e->a_data);
159 bswap32s(&e->a_bss);
160 bswap32s(&e->a_syms);
161 bswap32s(&e->a_entry);
162 bswap32s(&e->a_trsize);
163 bswap32s(&e->a_drsize);
165 #else
166 #define bswap_ahdr(x) do { } while (0)
167 #endif
169 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
170 #define OMAGIC 0407
171 #define NMAGIC 0410
172 #define ZMAGIC 0413
173 #define QMAGIC 0314
174 #define _N_HDROFF(x) (1024 - sizeof (struct exec))
175 #define N_TXTOFF(x) \
176 (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \
177 (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
178 #define N_TXTADDR(x) (N_MAGIC(x) == QMAGIC ? TARGET_PAGE_SIZE : 0)
179 #define N_DATOFF(x) (N_TXTOFF(x) + (x).a_text)
180 #define _N_SEGMENT_ROUND(x) (((x) + TARGET_PAGE_SIZE - 1) & ~(TARGET_PAGE_SIZE - 1))
182 #define _N_TXTENDADDR(x) (N_TXTADDR(x)+(x).a_text)
184 #define N_DATADDR(x) \
185 (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x)) \
186 : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x))))
189 int load_aout(const char *filename, target_phys_addr_t addr, int max_sz)
191 int fd, size, ret;
192 struct exec e;
193 uint32_t magic;
195 fd = open(filename, O_RDONLY | O_BINARY);
196 if (fd < 0)
197 return -1;
199 size = read(fd, &e, sizeof(e));
200 if (size < 0)
201 goto fail;
203 bswap_ahdr(&e);
205 magic = N_MAGIC(e);
206 switch (magic) {
207 case ZMAGIC:
208 case QMAGIC:
209 case OMAGIC:
210 if (e.a_text + e.a_data > max_sz)
211 goto fail;
212 lseek(fd, N_TXTOFF(e), SEEK_SET);
213 size = read_targphys(fd, addr, e.a_text + e.a_data);
214 if (size < 0)
215 goto fail;
216 break;
217 case NMAGIC:
218 if (N_DATADDR(e) + e.a_data > max_sz)
219 goto fail;
220 lseek(fd, N_TXTOFF(e), SEEK_SET);
221 size = read_targphys(fd, addr, e.a_text);
222 if (size < 0)
223 goto fail;
224 ret = read_targphys(fd, addr + N_DATADDR(e), e.a_data);
225 if (ret < 0)
226 goto fail;
227 size += ret;
228 break;
229 default:
230 goto fail;
232 close(fd);
233 return size;
234 fail:
235 close(fd);
236 return -1;
239 /* ELF loader */
241 static void *load_at(int fd, int offset, int size)
243 void *ptr;
244 if (lseek(fd, offset, SEEK_SET) < 0)
245 return NULL;
246 ptr = qemu_malloc(size);
247 if (!ptr)
248 return NULL;
249 if (read(fd, ptr, size) != size) {
250 qemu_free(ptr);
251 return NULL;
253 return ptr;
257 #define ELF_CLASS ELFCLASS32
258 #include "elf.h"
260 #define SZ 32
261 #define elf_word uint32_t
262 #define elf_sword int32_t
263 #define bswapSZs bswap32s
264 #include "elf_ops.h"
266 #undef elfhdr
267 #undef elf_phdr
268 #undef elf_shdr
269 #undef elf_sym
270 #undef elf_note
271 #undef elf_word
272 #undef elf_sword
273 #undef bswapSZs
274 #undef SZ
275 #define elfhdr elf64_hdr
276 #define elf_phdr elf64_phdr
277 #define elf_note elf64_note
278 #define elf_shdr elf64_shdr
279 #define elf_sym elf64_sym
280 #define elf_word uint64_t
281 #define elf_sword int64_t
282 #define bswapSZs bswap64s
283 #define SZ 64
284 #include "elf_ops.h"
286 /* return < 0 if error, otherwise the number of bytes loaded in memory */
287 int load_elf(const char *filename, int64_t address_offset,
288 uint64_t *pentry, uint64_t *lowaddr, uint64_t *highaddr)
290 int fd, data_order, host_data_order, must_swab, ret;
291 uint8_t e_ident[EI_NIDENT];
293 fd = open(filename, O_RDONLY | O_BINARY);
294 if (fd < 0) {
295 perror(filename);
296 return -1;
298 if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
299 goto fail;
300 if (e_ident[0] != ELFMAG0 ||
301 e_ident[1] != ELFMAG1 ||
302 e_ident[2] != ELFMAG2 ||
303 e_ident[3] != ELFMAG3)
304 goto fail;
305 #ifdef WORDS_BIGENDIAN
306 data_order = ELFDATA2MSB;
307 #else
308 data_order = ELFDATA2LSB;
309 #endif
310 must_swab = data_order != e_ident[EI_DATA];
312 #ifdef TARGET_WORDS_BIGENDIAN
313 host_data_order = ELFDATA2MSB;
314 #else
315 host_data_order = ELFDATA2LSB;
316 #endif
317 if (host_data_order != e_ident[EI_DATA])
318 return -1;
320 lseek(fd, 0, SEEK_SET);
321 if (e_ident[EI_CLASS] == ELFCLASS64) {
322 ret = load_elf64(fd, address_offset, must_swab, pentry,
323 lowaddr, highaddr);
324 } else {
325 ret = load_elf32(fd, address_offset, must_swab, pentry,
326 lowaddr, highaddr);
329 close(fd);
330 return ret;
332 fail:
333 close(fd);
334 return -1;
337 static void bswap_uboot_header(uboot_image_header_t *hdr)
339 #ifndef WORDS_BIGENDIAN
340 bswap32s(&hdr->ih_magic);
341 bswap32s(&hdr->ih_hcrc);
342 bswap32s(&hdr->ih_time);
343 bswap32s(&hdr->ih_size);
344 bswap32s(&hdr->ih_load);
345 bswap32s(&hdr->ih_ep);
346 bswap32s(&hdr->ih_dcrc);
347 #endif
350 /* Load a U-Boot image. */
352 /* gunzip functionality is derived from gunzip function
353 * in uboot source code
356 #define ZALLOC_ALIGNMENT 16
358 static void *zalloc(void *x, unsigned items, unsigned size)
360 void *p;
362 size *= items;
363 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
365 p = malloc (size);
367 return (p);
370 static void zfree(void *x, void *addr, unsigned nb)
372 free (addr);
376 #define HEAD_CRC 2
377 #define EXTRA_FIELD 4
378 #define ORIG_NAME 8
379 #define COMMENT 0x10
380 #define RESERVED 0xe0
382 #define DEFLATED 8
384 static int gunzip(void *dst, int dstlen, unsigned char *src,
385 unsigned long *lenp)
387 z_stream s;
388 int r, i, flags;
390 /* skip header */
391 i = 10;
392 flags = src[3];
393 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
394 puts ("Error: Bad gzipped data\n");
395 return -1;
397 if ((flags & EXTRA_FIELD) != 0)
398 i = 12 + src[10] + (src[11] << 8);
399 if ((flags & ORIG_NAME) != 0)
400 while (src[i++] != 0)
402 if ((flags & COMMENT) != 0)
403 while (src[i++] != 0)
405 if ((flags & HEAD_CRC) != 0)
406 i += 2;
407 if (i >= *lenp) {
408 puts ("Error: gunzip out of data in header\n");
409 return -1;
412 s.zalloc = zalloc;
413 s.zfree = (free_func)zfree;
415 r = inflateInit2(&s, -MAX_WBITS);
416 if (r != Z_OK) {
417 printf ("Error: inflateInit2() returned %d\n", r);
418 return (-1);
420 s.next_in = src + i;
421 s.avail_in = *lenp - i;
422 s.next_out = dst;
423 s.avail_out = dstlen;
424 r = inflate(&s, Z_FINISH);
425 if (r != Z_OK && r != Z_STREAM_END) {
426 printf ("Error: inflate() returned %d\n", r);
427 return -1;
429 *lenp = s.next_out - (unsigned char *) dst;
430 inflateEnd(&s);
432 return 0;
436 #define MAX_KERNEL_SIZE 8<<20 //8MB
437 /* This functions can load uImage & cuImage files */
438 int load_uimage(const char *filename, target_ulong *ep,
439 target_ulong *load_address,
440 target_ulong *loaded_image_size,
441 int *is_linux)
443 int fd;
444 int size;
445 int ret;
446 uboot_image_header_t h;
447 uboot_image_header_t *hdr = &h;
448 uint8_t *data = NULL;
449 uint8_t *uncompressed_data = NULL;
450 unsigned long tmp_loaded_image_size;
452 fd = open(filename, O_RDONLY | O_BINARY);
453 if (fd < 0)
454 return -1;
456 size = read(fd, hdr, sizeof(uboot_image_header_t));
457 if (size < 0)
458 goto fail;
460 bswap_uboot_header(hdr);
462 if (hdr->ih_magic != IH_MAGIC)
463 goto fail;
465 /* TODO: Implement Multi-File images. */
466 if (hdr->ih_type == IH_TYPE_MULTI) {
467 fprintf(stderr, "Unable to load multi-file u-boot images\n");
468 goto fail;
471 /* TODO bzip2 support */
472 if (hdr->ih_comp == IH_COMP_BZIP2) {
473 fprintf(stderr, "Unable to load bzip2 compressed u-boot images\n");
474 goto fail;
477 /* TODO: Check CPU type. */
479 if (is_linux) {
480 if (hdr->ih_type == IH_TYPE_KERNEL && hdr->ih_os == IH_OS_LINUX)
481 *is_linux = 1;
482 else
483 *is_linux = 0;
486 *ep = hdr->ih_ep;
488 data = qemu_malloc(hdr->ih_size);
489 if (!data)
490 goto fail;
492 if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
493 fprintf(stderr, "Error reading file\n");
494 goto fail;
497 tmp_loaded_image_size = hdr->ih_size;
499 if (hdr->ih_comp == IH_COMP_GZIP) {
500 uncompressed_data = qemu_malloc(MAX_KERNEL_SIZE);
501 ret = gunzip(uncompressed_data, MAX_KERNEL_SIZE,
502 (unsigned char *) data,
503 &tmp_loaded_image_size);
505 if (ret < 0) {
506 fprintf(stderr, "Unable to decompress gziped image!\n");
507 goto fail;
510 qemu_free(data);
511 cpu_physical_memory_write_rom(hdr->ih_load, uncompressed_data,
512 tmp_loaded_image_size);
513 } else {
514 cpu_physical_memory_write_rom(hdr->ih_load, data,
515 tmp_loaded_image_size);
518 if (loaded_image_size != NULL)
519 *loaded_image_size = tmp_loaded_image_size;
521 if (load_address != NULL)
522 *load_address = hdr->ih_load;
524 return 0;
526 fail:
527 if (data)
528 qemu_free(data);
529 close(fd);
530 return -1;
533 /* XXX this function is to keep compatibility with other
534 * qemu callers. Once those callers are modified. This function
535 * should be removed from here & sysemu.h
537 int load_uboot(const char *filename, target_ulong *ep, int *is_linux)
539 int ret;
540 target_ulong size;
542 ret = load_uimage(filename, ep, NULL, &size, is_linux);
544 if (ret < 0)
545 return ret;
546 else
547 return (int)size;