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
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/>.
49 #include "uboot_image.h"
54 /* return the size or -1 if error */
55 int get_image_size(const char *filename
)
58 fd
= open(filename
, O_RDONLY
| O_BINARY
);
61 size
= lseek(fd
, 0, SEEK_END
);
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
)
71 fd
= open(filename
, O_RDONLY
| O_BINARY
);
74 size
= lseek(fd
, 0, SEEK_END
);
75 lseek(fd
, 0, SEEK_SET
);
76 if (read(fd
, addr
, size
) != size
) {
84 /* read()-like version */
85 int read_targphys(const char *name
,
86 int fd
, target_phys_addr_t dst_addr
, size_t nbytes
)
91 buf
= qemu_malloc(nbytes
);
92 did
= read(fd
, buf
, nbytes
);
94 rom_add_blob_fixed("read", buf
, did
, dst_addr
);
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
)
105 size
= get_image_size(filename
);
107 rom_add_file_fixed(filename
, addr
);
111 void pstrcpy_targphys(const char *name
, target_phys_addr_t dest
, int buf_size
,
117 if (buf_size
<= 0) return;
118 nulp
= memchr(source
, 0, buf_size
);
120 rom_add_blob_fixed(name
, source
, (nulp
- source
) + 1, dest
);
122 rom_add_blob_fixed(name
, source
, buf_size
, dest
);
123 ptr
= rom_ptr(dest
+ buf_size
- 1);
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
);
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)
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
)
180 fd
= open(filename
, O_RDONLY
| O_BINARY
);
184 size
= read(fd
, &e
, sizeof(e
));
197 if (e
.a_text
+ e
.a_data
> max_sz
)
199 lseek(fd
, N_TXTOFF(e
), SEEK_SET
);
200 size
= read_targphys(filename
, fd
, addr
, e
.a_text
+ e
.a_data
);
205 if (N_DATADDR(e
, target_page_size
) + e
.a_data
> max_sz
)
207 lseek(fd
, N_TXTOFF(e
), SEEK_SET
);
208 size
= read_targphys(filename
, fd
, addr
, e
.a_text
);
211 ret
= read_targphys(filename
, fd
, addr
+ N_DATADDR(e
, target_page_size
),
229 static void *load_at(int fd
, int offset
, int size
)
232 if (lseek(fd
, offset
, SEEK_SET
) < 0)
234 ptr
= qemu_malloc(size
);
235 if (read(fd
, ptr
, size
) != size
) {
246 #define ELF_CLASS ELFCLASS32
250 #define elf_word uint32_t
251 #define elf_sword int32_t
252 #define bswapSZs bswap32s
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
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
);
288 if (read(fd
, e_ident
, sizeof(e_ident
)) != sizeof(e_ident
))
290 if (e_ident
[0] != ELFMAG0
||
291 e_ident
[1] != ELFMAG1
||
292 e_ident
[2] != ELFMAG2
||
293 e_ident
[3] != ELFMAG3
)
295 #ifdef HOST_WORDS_BIGENDIAN
296 data_order
= ELFDATA2MSB
;
298 data_order
= ELFDATA2LSB
;
300 must_swab
= data_order
!= e_ident
[EI_DATA
];
302 target_data_order
= ELFDATA2MSB
;
304 target_data_order
= ELFDATA2LSB
;
307 if (target_data_order
!= e_ident
[EI_DATA
])
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
);
315 ret
= load_elf32(filename
, fd
, address_offset
, must_swab
, pentry
,
316 lowaddr
, highaddr
, elf_machine
, clear_lsb
);
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
);
341 #define ZALLOC_ALIGNMENT 16
343 static void *zalloc(void *x
, unsigned items
, unsigned size
)
348 size
= (size
+ ZALLOC_ALIGNMENT
- 1) & ~(ZALLOC_ALIGNMENT
- 1);
350 p
= qemu_malloc(size
);
355 static void zfree(void *x
, void *addr
)
362 #define EXTRA_FIELD 4
365 #define RESERVED 0xe0
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
,
383 if (src
[2] != DEFLATED
|| (flags
& RESERVED
) != 0) {
384 puts ("Error: Bad gzipped data\n");
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)
398 puts ("Error: gunzip out of data in header\n");
405 r
= inflateInit2(&s
, -MAX_WBITS
);
407 printf ("Error: inflateInit2() returned %d\n", r
);
411 s
.avail_in
= srclen
- i
;
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
);
419 dstbytes
= s
.next_out
- (unsigned char *) dst
;
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
)
431 uboot_image_header_t h
;
432 uboot_image_header_t
*hdr
= &h
;
433 uint8_t *data
= NULL
;
436 fd
= open(filename
, O_RDONLY
| O_BINARY
);
440 size
= read(fd
, hdr
, sizeof(uboot_image_header_t
));
444 bswap_uboot_header(hdr
);
446 if (hdr
->ih_magic
!= IH_MAGIC
)
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");
455 switch (hdr
->ih_comp
) {
461 "Unable to load u-boot images with compression type %d\n",
466 /* TODO: Check CPU type. */
468 if (hdr
->ih_os
== IH_OS_LINUX
)
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");
482 if (hdr
->ih_comp
== IH_COMP_GZIP
) {
483 uint8_t *compressed_data
;
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
);
494 fprintf(stderr
, "Unable to decompress gzipped image!\n");
497 hdr
->ih_size
= bytes
;
500 rom_add_blob_fixed(filename
, data
, hdr
->ih_size
, hdr
->ih_load
);
503 *loadaddr
= hdr
->ih_load
;
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
;
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
);
538 static void rom_insert(Rom
*rom
)
542 /* list is ordered by load address */
543 QTAILQ_FOREACH(item
, &roms
, next
) {
544 if (rom
->min
>= item
->min
)
546 QTAILQ_INSERT_BEFORE(item
, rom
, next
);
549 QTAILQ_INSERT_TAIL(&roms
, rom
, next
);
552 int rom_add_file(const char *file
,
553 target_phys_addr_t min
, target_phys_addr_t max
, int align
)
558 rom
= qemu_mallocz(sizeof(*rom
));
559 rom
->name
= qemu_strdup(file
);
560 rom
->path
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, rom
->name
);
561 if (rom
->path
== NULL
) {
562 rom
->path
= strdup(file
);
565 fd
= open(rom
->path
, O_RDONLY
| O_BINARY
);
567 fprintf(stderr
, "Could not open option rom '%s': %s\n",
568 rom
->path
, strerror(errno
));
575 rom
->romsize
= lseek(fd
, 0, SEEK_END
);
576 rom
->data
= qemu_mallocz(rom
->romsize
);
577 lseek(fd
, 0, SEEK_SET
);
578 rc
= read(fd
, rom
->data
, rom
->romsize
);
579 if (rc
!= rom
->romsize
) {
580 fprintf(stderr
, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
581 rom
->name
, rc
, rom
->romsize
);
591 qemu_free(rom
->data
);
592 qemu_free(rom
->path
);
593 qemu_free(rom
->name
);
598 int rom_add_blob(const char *name
, const void *blob
, size_t len
,
599 target_phys_addr_t min
, target_phys_addr_t max
, int align
)
603 rom
= qemu_mallocz(sizeof(*rom
));
604 rom
->name
= qemu_strdup(name
);
609 rom
->data
= qemu_mallocz(rom
->romsize
);
610 memcpy(rom
->data
, blob
, len
);
615 static void rom_reset(void *unused
)
619 QTAILQ_FOREACH(rom
, &roms
, next
) {
620 if (rom
->data
== NULL
)
622 cpu_physical_memory_write_rom(rom
->addr
, rom
->data
, rom
->romsize
);
624 /* rom needs to be written only once */
625 qemu_free(rom
->data
);
631 int rom_load_all(void)
633 target_phys_addr_t addr
= 0;
637 QTAILQ_FOREACH(rom
, &roms
, next
) {
641 /* load address range */
643 addr
+= (rom
->align
-1);
644 addr
&= ~(rom
->align
-1);
646 if (addr
+ rom
->romsize
> rom
->max
) {
647 fprintf(stderr
, "rom: out of memory (rom %s, "
648 "addr 0x" TARGET_FMT_plx
649 ", size 0x%zx, max 0x" TARGET_FMT_plx
")\n",
650 rom
->name
, addr
, rom
->romsize
, rom
->max
);
654 /* fixed address requested */
655 if (addr
!= rom
->min
) {
656 fprintf(stderr
, "rom: requested regions overlap "
657 "(rom %s. free=0x" TARGET_FMT_plx
658 ", addr=0x" TARGET_FMT_plx
")\n",
659 rom
->name
, addr
, rom
->min
);
664 addr
+= rom
->romsize
;
665 memtype
= cpu_get_physical_page_desc(rom
->addr
) & (3 << IO_MEM_SHIFT
);
666 if (memtype
== IO_MEM_ROM
)
669 qemu_register_reset(rom_reset
, NULL
);
674 static Rom
*find_rom(target_phys_addr_t addr
)
678 QTAILQ_FOREACH(rom
, &roms
, next
) {
683 if (rom
->min
+ rom
->romsize
< addr
)
690 void *rom_ptr(target_phys_addr_t addr
)
694 rom
= find_rom(addr
);
695 if (!rom
|| !rom
->data
)
697 return rom
->data
+ (addr
- rom
->min
);
700 void do_info_roms(Monitor
*mon
)
704 QTAILQ_FOREACH(rom
, &roms
, next
) {
705 monitor_printf(mon
, "addr=" TARGET_FMT_plx
706 " size=0x%06zx mem=%s name=\"%s\" \n",
707 rom
->addr
, rom
->romsize
,
708 rom
->isrom
? "rom" : "ram",