add roms/pcbios
[armpft.git] / hw / loader.c
bloba08585b040fd00147a8d83fa430b63e73bd5949b
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 * Gunzip functionality in this file is derived from u-boot:
26 * (C) Copyright 2008 Semihalf
28 * (C) Copyright 2000-2005
29 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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.
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.
41 * You should have received a copy of the GNU General Public License along
42 * with this program; if not, see <http://www.gnu.org/licenses/>.
45 #include "hw.h"
46 #include "disas.h"
47 #include "monitor.h"
48 #include "sysemu.h"
49 #include "uboot_image.h"
50 #include "loader.h"
52 #include <zlib.h>
54 /* return the size or -1 if error */
55 int get_image_size(const char *filename)
57 int fd, size;
58 fd = open(filename, O_RDONLY | O_BINARY);
59 if (fd < 0)
60 return -1;
61 size = lseek(fd, 0, SEEK_END);
62 close(fd);
63 return size;
66 /* return the size or -1 if error */
67 /* deprecated, because caller does not specify buffer size! */
68 int load_image(const char *filename, uint8_t *addr)
70 int fd, size;
71 fd = open(filename, O_RDONLY | O_BINARY);
72 if (fd < 0)
73 return -1;
74 size = lseek(fd, 0, SEEK_END);
75 lseek(fd, 0, SEEK_SET);
76 if (read(fd, addr, size) != size) {
77 close(fd);
78 return -1;
80 close(fd);
81 return size;
84 /* read()-like version */
85 int read_targphys(const char *name,
86 int fd, target_phys_addr_t dst_addr, size_t nbytes)
88 uint8_t *buf;
89 size_t did;
91 buf = qemu_malloc(nbytes);
92 did = read(fd, buf, nbytes);
93 if (did > 0)
94 rom_add_blob_fixed("read", buf, did, dst_addr);
95 qemu_free(buf);
96 return did;
99 /* return the size or -1 if error */
100 int load_image_targphys(const char *filename,
101 target_phys_addr_t addr, int max_sz)
103 int size;
105 size = get_image_size(filename);
106 if (size > 0)
107 rom_add_file_fixed(filename, addr);
108 return size;
111 void pstrcpy_targphys(const char *name, target_phys_addr_t dest, int buf_size,
112 const char *source)
114 const char *nulp;
115 char *ptr;
117 if (buf_size <= 0) return;
118 nulp = memchr(source, 0, buf_size);
119 if (nulp) {
120 rom_add_blob_fixed(name, source, (nulp - source) + 1, dest);
121 } else {
122 rom_add_blob_fixed(name, source, buf_size, dest);
123 ptr = rom_ptr(dest + buf_size - 1);
124 *ptr = 0;
128 /* A.OUT loader */
130 struct exec
132 uint32_t a_info; /* Use macros N_MAGIC, etc for access */
133 uint32_t a_text; /* length of text, in bytes */
134 uint32_t a_data; /* length of data, in bytes */
135 uint32_t a_bss; /* length of uninitialized data area, in bytes */
136 uint32_t a_syms; /* length of symbol table data in file, in bytes */
137 uint32_t a_entry; /* start address */
138 uint32_t a_trsize; /* length of relocation info for text, in bytes */
139 uint32_t a_drsize; /* length of relocation info for data, in bytes */
142 static void bswap_ahdr(struct exec *e)
144 bswap32s(&e->a_info);
145 bswap32s(&e->a_text);
146 bswap32s(&e->a_data);
147 bswap32s(&e->a_bss);
148 bswap32s(&e->a_syms);
149 bswap32s(&e->a_entry);
150 bswap32s(&e->a_trsize);
151 bswap32s(&e->a_drsize);
154 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
155 #define OMAGIC 0407
156 #define NMAGIC 0410
157 #define ZMAGIC 0413
158 #define QMAGIC 0314
159 #define _N_HDROFF(x) (1024 - sizeof (struct exec))
160 #define N_TXTOFF(x) \
161 (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \
162 (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
163 #define N_TXTADDR(x, target_page_size) (N_MAGIC(x) == QMAGIC ? target_page_size : 0)
164 #define _N_SEGMENT_ROUND(x, target_page_size) (((x) + target_page_size - 1) & ~(target_page_size - 1))
166 #define _N_TXTENDADDR(x, target_page_size) (N_TXTADDR(x, target_page_size)+(x).a_text)
168 #define N_DATADDR(x, target_page_size) \
169 (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x, target_page_size)) \
170 : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x, target_page_size), target_page_size)))
173 int load_aout(const char *filename, target_phys_addr_t addr, int max_sz,
174 int bswap_needed, target_phys_addr_t target_page_size)
176 int fd, size, ret;
177 struct exec e;
178 uint32_t magic;
180 fd = open(filename, O_RDONLY | O_BINARY);
181 if (fd < 0)
182 return -1;
184 size = read(fd, &e, sizeof(e));
185 if (size < 0)
186 goto fail;
188 if (bswap_needed) {
189 bswap_ahdr(&e);
192 magic = N_MAGIC(e);
193 switch (magic) {
194 case ZMAGIC:
195 case QMAGIC:
196 case OMAGIC:
197 if (e.a_text + e.a_data > max_sz)
198 goto fail;
199 lseek(fd, N_TXTOFF(e), SEEK_SET);
200 size = read_targphys(filename, fd, addr, e.a_text + e.a_data);
201 if (size < 0)
202 goto fail;
203 break;
204 case NMAGIC:
205 if (N_DATADDR(e, target_page_size) + e.a_data > max_sz)
206 goto fail;
207 lseek(fd, N_TXTOFF(e), SEEK_SET);
208 size = read_targphys(filename, fd, addr, e.a_text);
209 if (size < 0)
210 goto fail;
211 ret = read_targphys(filename, fd, addr + N_DATADDR(e, target_page_size),
212 e.a_data);
213 if (ret < 0)
214 goto fail;
215 size += ret;
216 break;
217 default:
218 goto fail;
220 close(fd);
221 return size;
222 fail:
223 close(fd);
224 return -1;
227 /* ELF loader */
229 static void *load_at(int fd, int offset, int size)
231 void *ptr;
232 if (lseek(fd, offset, SEEK_SET) < 0)
233 return NULL;
234 ptr = qemu_malloc(size);
235 if (read(fd, ptr, size) != size) {
236 qemu_free(ptr);
237 return NULL;
239 return ptr;
242 #ifdef ELF_CLASS
243 #undef ELF_CLASS
244 #endif
246 #define ELF_CLASS ELFCLASS32
247 #include "elf.h"
249 #define SZ 32
250 #define elf_word uint32_t
251 #define elf_sword int32_t
252 #define bswapSZs bswap32s
253 #include "elf_ops.h"
255 #undef elfhdr
256 #undef elf_phdr
257 #undef elf_shdr
258 #undef elf_sym
259 #undef elf_note
260 #undef elf_word
261 #undef elf_sword
262 #undef bswapSZs
263 #undef SZ
264 #define elfhdr elf64_hdr
265 #define elf_phdr elf64_phdr
266 #define elf_note elf64_note
267 #define elf_shdr elf64_shdr
268 #define elf_sym elf64_sym
269 #define elf_word uint64_t
270 #define elf_sword int64_t
271 #define bswapSZs bswap64s
272 #define SZ 64
273 #include "elf_ops.h"
275 /* return < 0 if error, otherwise the number of bytes loaded in memory */
276 int load_elf(const char *filename, int64_t address_offset,
277 uint64_t *pentry, uint64_t *lowaddr, uint64_t *highaddr,
278 int big_endian, int elf_machine, int clear_lsb)
280 int fd, data_order, target_data_order, must_swab, ret;
281 uint8_t e_ident[EI_NIDENT];
283 fd = open(filename, O_RDONLY | O_BINARY);
284 if (fd < 0) {
285 perror(filename);
286 return -1;
288 if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
289 goto fail;
290 if (e_ident[0] != ELFMAG0 ||
291 e_ident[1] != ELFMAG1 ||
292 e_ident[2] != ELFMAG2 ||
293 e_ident[3] != ELFMAG3)
294 goto fail;
295 #ifdef HOST_WORDS_BIGENDIAN
296 data_order = ELFDATA2MSB;
297 #else
298 data_order = ELFDATA2LSB;
299 #endif
300 must_swab = data_order != e_ident[EI_DATA];
301 if (big_endian) {
302 target_data_order = ELFDATA2MSB;
303 } else {
304 target_data_order = ELFDATA2LSB;
307 if (target_data_order != e_ident[EI_DATA])
308 return -1;
310 lseek(fd, 0, SEEK_SET);
311 if (e_ident[EI_CLASS] == ELFCLASS64) {
312 ret = load_elf64(filename, fd, address_offset, must_swab, pentry,
313 lowaddr, highaddr, elf_machine, clear_lsb);
314 } else {
315 ret = load_elf32(filename, fd, address_offset, must_swab, pentry,
316 lowaddr, highaddr, elf_machine, clear_lsb);
319 close(fd);
320 return ret;
322 fail:
323 close(fd);
324 return -1;
327 static void bswap_uboot_header(uboot_image_header_t *hdr)
329 #ifndef HOST_WORDS_BIGENDIAN
330 bswap32s(&hdr->ih_magic);
331 bswap32s(&hdr->ih_hcrc);
332 bswap32s(&hdr->ih_time);
333 bswap32s(&hdr->ih_size);
334 bswap32s(&hdr->ih_load);
335 bswap32s(&hdr->ih_ep);
336 bswap32s(&hdr->ih_dcrc);
337 #endif
341 #define ZALLOC_ALIGNMENT 16
343 static void *zalloc(void *x, unsigned items, unsigned size)
345 void *p;
347 size *= items;
348 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
350 p = qemu_malloc(size);
352 return (p);
355 static void zfree(void *x, void *addr)
357 qemu_free(addr);
361 #define HEAD_CRC 2
362 #define EXTRA_FIELD 4
363 #define ORIG_NAME 8
364 #define COMMENT 0x10
365 #define RESERVED 0xe0
367 #define DEFLATED 8
369 /* This is the maximum in uboot, so if a uImage overflows this, it would
370 * overflow on real hardware too. */
371 #define UBOOT_MAX_GUNZIP_BYTES 0x800000
373 static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src,
374 size_t srclen)
376 z_stream s;
377 ssize_t dstbytes;
378 int r, i, flags;
380 /* skip header */
381 i = 10;
382 flags = src[3];
383 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
384 puts ("Error: Bad gzipped data\n");
385 return -1;
387 if ((flags & EXTRA_FIELD) != 0)
388 i = 12 + src[10] + (src[11] << 8);
389 if ((flags & ORIG_NAME) != 0)
390 while (src[i++] != 0)
392 if ((flags & COMMENT) != 0)
393 while (src[i++] != 0)
395 if ((flags & HEAD_CRC) != 0)
396 i += 2;
397 if (i >= srclen) {
398 puts ("Error: gunzip out of data in header\n");
399 return -1;
402 s.zalloc = zalloc;
403 s.zfree = zfree;
405 r = inflateInit2(&s, -MAX_WBITS);
406 if (r != Z_OK) {
407 printf ("Error: inflateInit2() returned %d\n", r);
408 return (-1);
410 s.next_in = src + i;
411 s.avail_in = srclen - i;
412 s.next_out = dst;
413 s.avail_out = dstlen;
414 r = inflate(&s, Z_FINISH);
415 if (r != Z_OK && r != Z_STREAM_END) {
416 printf ("Error: inflate() returned %d\n", r);
417 return -1;
419 dstbytes = s.next_out - (unsigned char *) dst;
420 inflateEnd(&s);
422 return dstbytes;
425 /* Load a U-Boot image. */
426 int load_uimage(const char *filename, target_phys_addr_t *ep,
427 target_phys_addr_t *loadaddr, int *is_linux)
429 int fd;
430 int size;
431 uboot_image_header_t h;
432 uboot_image_header_t *hdr = &h;
433 uint8_t *data = NULL;
434 int ret = -1;
436 fd = open(filename, O_RDONLY | O_BINARY);
437 if (fd < 0)
438 return -1;
440 size = read(fd, hdr, sizeof(uboot_image_header_t));
441 if (size < 0)
442 goto out;
444 bswap_uboot_header(hdr);
446 if (hdr->ih_magic != IH_MAGIC)
447 goto out;
449 /* TODO: Implement other image types. */
450 if (hdr->ih_type != IH_TYPE_KERNEL) {
451 fprintf(stderr, "Can only load u-boot image type \"kernel\"\n");
452 goto out;
455 switch (hdr->ih_comp) {
456 case IH_COMP_NONE:
457 case IH_COMP_GZIP:
458 break;
459 default:
460 fprintf(stderr,
461 "Unable to load u-boot images with compression type %d\n",
462 hdr->ih_comp);
463 goto out;
466 /* TODO: Check CPU type. */
467 if (is_linux) {
468 if (hdr->ih_os == IH_OS_LINUX)
469 *is_linux = 1;
470 else
471 *is_linux = 0;
474 *ep = hdr->ih_ep;
475 data = qemu_malloc(hdr->ih_size);
477 if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
478 fprintf(stderr, "Error reading file\n");
479 goto out;
482 if (hdr->ih_comp == IH_COMP_GZIP) {
483 uint8_t *compressed_data;
484 size_t max_bytes;
485 ssize_t bytes;
487 compressed_data = data;
488 max_bytes = UBOOT_MAX_GUNZIP_BYTES;
489 data = qemu_malloc(max_bytes);
491 bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size);
492 qemu_free(compressed_data);
493 if (bytes < 0) {
494 fprintf(stderr, "Unable to decompress gzipped image!\n");
495 goto out;
497 hdr->ih_size = bytes;
500 rom_add_blob_fixed(filename, data, hdr->ih_size, hdr->ih_load);
502 if (loadaddr)
503 *loadaddr = hdr->ih_load;
505 ret = hdr->ih_size;
507 out:
508 if (data)
509 qemu_free(data);
510 close(fd);
511 return ret;
515 * Functions for reboot-persistent memory regions.
516 * - used for vga bios and option roms.
517 * - also linux kernel (-kernel / -initrd).
520 typedef struct Rom Rom;
522 struct Rom {
523 char *name;
524 char *path;
525 size_t romsize;
526 uint8_t *data;
527 int align;
528 int isrom;
530 target_phys_addr_t min;
531 target_phys_addr_t max;
532 target_phys_addr_t addr;
533 QTAILQ_ENTRY(Rom) next;
536 static QTAILQ_HEAD(, Rom) roms = QTAILQ_HEAD_INITIALIZER(roms);
537 int rom_enable_driver_roms;
539 static void rom_insert(Rom *rom)
541 Rom *item;
543 /* list is ordered by load address */
544 QTAILQ_FOREACH(item, &roms, next) {
545 if (rom->min >= item->min)
546 continue;
547 QTAILQ_INSERT_BEFORE(item, rom, next);
548 return;
550 QTAILQ_INSERT_TAIL(&roms, rom, next);
553 int rom_add_file(const char *file,
554 target_phys_addr_t min, target_phys_addr_t max, int align)
556 Rom *rom;
557 int rc, fd = -1;
559 rom = qemu_mallocz(sizeof(*rom));
560 rom->name = qemu_strdup(file);
561 rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name);
562 if (rom->path == NULL) {
563 rom->path = qemu_strdup(file);
566 fd = open(rom->path, O_RDONLY | O_BINARY);
567 if (fd == -1) {
568 fprintf(stderr, "Could not open option rom '%s': %s\n",
569 rom->path, strerror(errno));
570 goto err;
573 rom->align = align;
574 rom->min = min;
575 rom->max = max;
576 rom->romsize = lseek(fd, 0, SEEK_END);
577 rom->data = qemu_mallocz(rom->romsize);
578 lseek(fd, 0, SEEK_SET);
579 rc = read(fd, rom->data, rom->romsize);
580 if (rc != rom->romsize) {
581 fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
582 rom->name, rc, rom->romsize);
583 goto err;
585 close(fd);
586 rom_insert(rom);
587 return 0;
589 err:
590 if (fd != -1)
591 close(fd);
592 qemu_free(rom->data);
593 qemu_free(rom->path);
594 qemu_free(rom->name);
595 qemu_free(rom);
596 return -1;
599 int rom_add_blob(const char *name, const void *blob, size_t len,
600 target_phys_addr_t min, target_phys_addr_t max, int align)
602 Rom *rom;
604 rom = qemu_mallocz(sizeof(*rom));
605 rom->name = qemu_strdup(name);
606 rom->align = align;
607 rom->min = min;
608 rom->max = max;
609 rom->romsize = len;
610 rom->data = qemu_mallocz(rom->romsize);
611 memcpy(rom->data, blob, len);
612 rom_insert(rom);
613 return 0;
616 int rom_add_vga(const char *file)
618 if (!rom_enable_driver_roms)
619 return 0;
620 return rom_add_file(file, PC_ROM_MIN_VGA, PC_ROM_MAX, PC_ROM_ALIGN);
623 int rom_add_option(const char *file)
625 if (!rom_enable_driver_roms)
626 return 0;
627 return rom_add_file(file, PC_ROM_MIN_OPTION, PC_ROM_MAX, PC_ROM_ALIGN);
630 static void rom_reset(void *unused)
632 Rom *rom;
634 QTAILQ_FOREACH(rom, &roms, next) {
635 if (rom->data == NULL)
636 continue;
637 cpu_physical_memory_write_rom(rom->addr, rom->data, rom->romsize);
638 if (rom->isrom) {
639 /* rom needs to be written only once */
640 qemu_free(rom->data);
641 rom->data = NULL;
646 int rom_load_all(void)
648 target_phys_addr_t addr = 0;
649 int memtype;
650 Rom *rom;
652 QTAILQ_FOREACH(rom, &roms, next) {
653 if (addr < rom->min)
654 addr = rom->min;
655 if (rom->max) {
656 /* load address range */
657 if (rom->align) {
658 addr += (rom->align-1);
659 addr &= ~(rom->align-1);
661 if (addr + rom->romsize > rom->max) {
662 fprintf(stderr, "rom: out of memory (rom %s, "
663 "addr 0x" TARGET_FMT_plx
664 ", size 0x%zx, max 0x" TARGET_FMT_plx ")\n",
665 rom->name, addr, rom->romsize, rom->max);
666 return -1;
668 } else {
669 /* fixed address requested */
670 if (addr != rom->min) {
671 fprintf(stderr, "rom: requested regions overlap "
672 "(rom %s. free=0x" TARGET_FMT_plx
673 ", addr=0x" TARGET_FMT_plx ")\n",
674 rom->name, addr, rom->min);
675 return -1;
678 rom->addr = addr;
679 addr += rom->romsize;
680 memtype = cpu_get_physical_page_desc(rom->addr) & (3 << IO_MEM_SHIFT);
681 if (memtype == IO_MEM_ROM)
682 rom->isrom = 1;
684 qemu_register_reset(rom_reset, NULL);
685 rom_reset(NULL);
686 return 0;
689 static Rom *find_rom(target_phys_addr_t addr)
691 Rom *rom;
693 QTAILQ_FOREACH(rom, &roms, next) {
694 if (rom->max)
695 continue;
696 if (rom->min > addr)
697 continue;
698 if (rom->min + rom->romsize < addr)
699 continue;
700 return rom;
702 return NULL;
705 void *rom_ptr(target_phys_addr_t addr)
707 Rom *rom;
709 rom = find_rom(addr);
710 if (!rom || !rom->data)
711 return NULL;
712 return rom->data + (addr - rom->min);
715 void do_info_roms(Monitor *mon)
717 Rom *rom;
719 QTAILQ_FOREACH(rom, &roms, next) {
720 monitor_printf(mon, "addr=" TARGET_FMT_plx
721 " size=0x%06zx mem=%s name=\"%s\" \n",
722 rom->addr, rom->romsize,
723 rom->isrom ? "rom" : "ram",
724 rom->name);