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, write to the Free Software Foundation, Inc.,
43 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46 #include "qemu-common.h"
49 #include "uboot_image.h"
53 /* return the size or -1 if error */
54 int get_image_size(const char *filename
)
57 fd
= open(filename
, O_RDONLY
| O_BINARY
);
60 size
= lseek(fd
, 0, SEEK_END
);
65 /* return the size or -1 if error */
66 /* deprecated, because caller does not specify buffer size! */
67 int load_image(const char *filename
, uint8_t *addr
)
70 fd
= open(filename
, O_RDONLY
| O_BINARY
);
73 size
= lseek(fd
, 0, SEEK_END
);
74 lseek(fd
, 0, SEEK_SET
);
75 if (read(fd
, addr
, size
) != size
) {
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
)
87 target_phys_addr_t dst_begin
= dst_addr
;
91 want
= nbytes
> sizeof(buf
) ? sizeof(buf
) : nbytes
;
92 did
= fread(buf
, 1, want
, f
);
94 cpu_physical_memory_write_rom(dst_addr
, buf
, did
);
100 return dst_addr
- dst_begin
;
103 /* returns 0 on error, 1 if ok */
104 int fread_targphys_ok(target_phys_addr_t dst_addr
, size_t nbytes
, FILE *f
)
106 return fread_targphys(dst_addr
, nbytes
, f
) == nbytes
;
109 /* read()-like version */
110 int read_targphys(int fd
, target_phys_addr_t dst_addr
, size_t nbytes
)
113 target_phys_addr_t dst_begin
= dst_addr
;
117 want
= nbytes
> sizeof(buf
) ? sizeof(buf
) : nbytes
;
118 did
= read(fd
, buf
, want
);
119 if (did
!= want
) break;
121 cpu_physical_memory_write_rom(dst_addr
, buf
, did
);
125 return dst_addr
- dst_begin
;
128 /* return the size or -1 if error */
129 int load_image_targphys(const char *filename
,
130 target_phys_addr_t addr
, int max_sz
)
135 f
= fopen(filename
, "rb");
138 got
= fread_targphys(addr
, max_sz
, f
);
139 if (ferror(f
)) { fclose(f
); return -1; }
145 void pstrcpy_targphys(target_phys_addr_t dest
, int buf_size
,
148 static const uint8_t nul_byte
= 0;
151 if (buf_size
<= 0) return;
152 nulp
= memchr(source
, 0, buf_size
);
154 cpu_physical_memory_write_rom(dest
, (uint8_t *)source
,
155 (nulp
- source
) + 1);
157 cpu_physical_memory_write_rom(dest
, (uint8_t *)source
, buf_size
- 1);
158 cpu_physical_memory_write_rom(dest
, &nul_byte
, 1);
166 uint32_t a_info
; /* Use macros N_MAGIC, etc for access */
167 uint32_t a_text
; /* length of text, in bytes */
168 uint32_t a_data
; /* length of data, in bytes */
169 uint32_t a_bss
; /* length of uninitialized data area, in bytes */
170 uint32_t a_syms
; /* length of symbol table data in file, in bytes */
171 uint32_t a_entry
; /* start address */
172 uint32_t a_trsize
; /* length of relocation info for text, in bytes */
173 uint32_t a_drsize
; /* length of relocation info for data, in bytes */
177 static void bswap_ahdr(struct exec
*e
)
179 bswap32s(&e
->a_info
);
180 bswap32s(&e
->a_text
);
181 bswap32s(&e
->a_data
);
183 bswap32s(&e
->a_syms
);
184 bswap32s(&e
->a_entry
);
185 bswap32s(&e
->a_trsize
);
186 bswap32s(&e
->a_drsize
);
189 #define bswap_ahdr(x) do { } while (0)
192 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
197 #define _N_HDROFF(x) (1024 - sizeof (struct exec))
198 #define N_TXTOFF(x) \
199 (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \
200 (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
201 #define N_TXTADDR(x) (N_MAGIC(x) == QMAGIC ? TARGET_PAGE_SIZE : 0)
202 #define N_DATOFF(x) (N_TXTOFF(x) + (x).a_text)
203 #define _N_SEGMENT_ROUND(x) (((x) + TARGET_PAGE_SIZE - 1) & ~(TARGET_PAGE_SIZE - 1))
205 #define _N_TXTENDADDR(x) (N_TXTADDR(x)+(x).a_text)
207 #define N_DATADDR(x) \
208 (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x)) \
209 : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x))))
212 int load_aout(const char *filename
, target_phys_addr_t addr
, int max_sz
)
218 fd
= open(filename
, O_RDONLY
| O_BINARY
);
222 size
= read(fd
, &e
, sizeof(e
));
233 if (e
.a_text
+ e
.a_data
> max_sz
)
235 lseek(fd
, N_TXTOFF(e
), SEEK_SET
);
236 size
= read_targphys(fd
, addr
, e
.a_text
+ e
.a_data
);
241 if (N_DATADDR(e
) + e
.a_data
> max_sz
)
243 lseek(fd
, N_TXTOFF(e
), SEEK_SET
);
244 size
= read_targphys(fd
, addr
, e
.a_text
);
247 ret
= read_targphys(fd
, addr
+ N_DATADDR(e
), e
.a_data
);
264 static void *load_at(int fd
, int offset
, int size
)
267 if (lseek(fd
, offset
, SEEK_SET
) < 0)
269 ptr
= qemu_malloc(size
);
270 if (read(fd
, ptr
, size
) != size
) {
278 #define ELF_CLASS ELFCLASS32
282 #define elf_word uint32_t
283 #define elf_sword int32_t
284 #define bswapSZs bswap32s
296 #define elfhdr elf64_hdr
297 #define elf_phdr elf64_phdr
298 #define elf_note elf64_note
299 #define elf_shdr elf64_shdr
300 #define elf_sym elf64_sym
301 #define elf_word uint64_t
302 #define elf_sword int64_t
303 #define bswapSZs bswap64s
307 /* return < 0 if error, otherwise the number of bytes loaded in memory */
308 int load_elf(const char *filename
, int64_t address_offset
,
309 uint64_t *pentry
, uint64_t *lowaddr
, uint64_t *highaddr
)
311 int fd
, data_order
, host_data_order
, must_swab
, ret
;
312 uint8_t e_ident
[EI_NIDENT
];
314 fd
= open(filename
, O_RDONLY
| O_BINARY
);
319 if (read(fd
, e_ident
, sizeof(e_ident
)) != sizeof(e_ident
))
321 if (e_ident
[0] != ELFMAG0
||
322 e_ident
[1] != ELFMAG1
||
323 e_ident
[2] != ELFMAG2
||
324 e_ident
[3] != ELFMAG3
)
326 #ifdef WORDS_BIGENDIAN
327 data_order
= ELFDATA2MSB
;
329 data_order
= ELFDATA2LSB
;
331 must_swab
= data_order
!= e_ident
[EI_DATA
];
333 #ifdef TARGET_WORDS_BIGENDIAN
334 host_data_order
= ELFDATA2MSB
;
336 host_data_order
= ELFDATA2LSB
;
338 if (host_data_order
!= e_ident
[EI_DATA
])
341 lseek(fd
, 0, SEEK_SET
);
342 if (e_ident
[EI_CLASS
] == ELFCLASS64
) {
343 ret
= load_elf64(fd
, address_offset
, must_swab
, pentry
,
346 ret
= load_elf32(fd
, address_offset
, must_swab
, pentry
,
358 static void bswap_uboot_header(uboot_image_header_t
*hdr
)
360 #ifndef WORDS_BIGENDIAN
361 bswap32s(&hdr
->ih_magic
);
362 bswap32s(&hdr
->ih_hcrc
);
363 bswap32s(&hdr
->ih_time
);
364 bswap32s(&hdr
->ih_size
);
365 bswap32s(&hdr
->ih_load
);
366 bswap32s(&hdr
->ih_ep
);
367 bswap32s(&hdr
->ih_dcrc
);
372 #define ZALLOC_ALIGNMENT 16
374 static void *zalloc(void *x
, unsigned items
, unsigned size
)
379 size
= (size
+ ZALLOC_ALIGNMENT
- 1) & ~(ZALLOC_ALIGNMENT
- 1);
381 p
= qemu_malloc(size
);
386 static void zfree(void *x
, void *addr
, unsigned nb
)
393 #define EXTRA_FIELD 4
396 #define RESERVED 0xe0
400 /* This is the maximum in uboot, so if a uImage overflows this, it would
401 * overflow on real hardware too. */
402 #define UBOOT_MAX_GUNZIP_BYTES 0x800000
404 static ssize_t
gunzip(void *dst
, size_t dstlen
, uint8_t *src
,
414 if (src
[2] != DEFLATED
|| (flags
& RESERVED
) != 0) {
415 puts ("Error: Bad gzipped data\n");
418 if ((flags
& EXTRA_FIELD
) != 0)
419 i
= 12 + src
[10] + (src
[11] << 8);
420 if ((flags
& ORIG_NAME
) != 0)
421 while (src
[i
++] != 0)
423 if ((flags
& COMMENT
) != 0)
424 while (src
[i
++] != 0)
426 if ((flags
& HEAD_CRC
) != 0)
429 puts ("Error: gunzip out of data in header\n");
434 s
.zfree
= (free_func
)zfree
;
436 r
= inflateInit2(&s
, -MAX_WBITS
);
438 printf ("Error: inflateInit2() returned %d\n", r
);
442 s
.avail_in
= srclen
- i
;
444 s
.avail_out
= dstlen
;
445 r
= inflate(&s
, Z_FINISH
);
446 if (r
!= Z_OK
&& r
!= Z_STREAM_END
) {
447 printf ("Error: inflate() returned %d\n", r
);
450 dstbytes
= s
.next_out
- (unsigned char *) dst
;
456 /* Load a U-Boot image. */
457 int load_uimage(const char *filename
, target_ulong
*ep
, target_ulong
*loadaddr
,
462 uboot_image_header_t h
;
463 uboot_image_header_t
*hdr
= &h
;
464 uint8_t *data
= NULL
;
467 fd
= open(filename
, O_RDONLY
| O_BINARY
);
471 size
= read(fd
, hdr
, sizeof(uboot_image_header_t
));
475 bswap_uboot_header(hdr
);
477 if (hdr
->ih_magic
!= IH_MAGIC
)
480 /* TODO: Implement other image types. */
481 if (hdr
->ih_type
!= IH_TYPE_KERNEL
) {
482 fprintf(stderr
, "Can only load u-boot image type \"kernel\"\n");
486 switch (hdr
->ih_comp
) {
492 "Unable to load u-boot images with compression type %d\n",
497 /* TODO: Check CPU type. */
499 if (hdr
->ih_os
== IH_OS_LINUX
)
506 data
= qemu_malloc(hdr
->ih_size
);
508 if (read(fd
, data
, hdr
->ih_size
) != hdr
->ih_size
) {
509 fprintf(stderr
, "Error reading file\n");
513 if (hdr
->ih_comp
== IH_COMP_GZIP
) {
514 uint8_t *compressed_data
;
518 compressed_data
= data
;
519 max_bytes
= UBOOT_MAX_GUNZIP_BYTES
;
520 data
= qemu_malloc(max_bytes
);
522 bytes
= gunzip(data
, max_bytes
, compressed_data
, hdr
->ih_size
);
523 qemu_free(compressed_data
);
525 fprintf(stderr
, "Unable to decompress gzipped image!\n");
528 hdr
->ih_size
= bytes
;
531 cpu_physical_memory_write_rom(hdr
->ih_load
, data
, hdr
->ih_size
);
534 *loadaddr
= hdr
->ih_load
;