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
26 #include "uboot_image.h"
28 /* return the size or -1 if error */
29 int get_image_size(const char *filename)
32 fd = open(filename, O_RDONLY | O_BINARY);
35 size = lseek(fd, 0, SEEK_END);
40 /* return the size or -1 if error */
41 int load_image(const char *filename, uint8_t *addr)
44 fd = open(filename, O_RDONLY | O_BINARY);
47 size = lseek(fd, 0, SEEK_END);
48 lseek(fd, 0, SEEK_SET);
49 if (read(fd, addr, size) != size) {
61 uint32_t a_info; /* Use macros N_MAGIC, etc for access */
62 uint32_t a_text; /* length of text, in bytes */
63 uint32_t a_data; /* length of data, in bytes */
64 uint32_t a_bss; /* length of uninitialized data area, in bytes */
65 uint32_t a_syms; /* length of symbol table data in file, in bytes */
66 uint32_t a_entry; /* start address */
67 uint32_t a_trsize; /* length of relocation info for text, in bytes */
68 uint32_t a_drsize; /* length of relocation info for data, in bytes */
72 static void bswap_ahdr(struct exec *e)
79 bswap32s(&e->a_entry);
80 bswap32s(&e->a_trsize);
81 bswap32s(&e->a_drsize);
84 #define bswap_ahdr(x) do { } while (0)
87 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
92 #define _N_HDROFF(x) (1024 - sizeof (struct exec))
94 (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \
95 (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
96 #define N_TXTADDR(x) (N_MAGIC(x) == QMAGIC ? TARGET_PAGE_SIZE : 0)
97 #define N_DATOFF(x) (N_TXTOFF(x) + (x).a_text)
98 #define _N_SEGMENT_ROUND(x) (((x) + TARGET_PAGE_SIZE - 1) & ~(TARGET_PAGE_SIZE - 1))
100 #define _N_TXTENDADDR(x) (N_TXTADDR(x)+(x).a_text)
102 #define N_DATADDR(x) \
103 (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x)) \
104 : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x))))
107 int load_aout(const char *filename, uint8_t *addr)
113 fd = open(filename, O_RDONLY | O_BINARY);
117 size = read(fd, &e, sizeof(e));
128 lseek(fd, N_TXTOFF(e), SEEK_SET);
129 size = read(fd, addr, e.a_text + e.a_data);
134 lseek(fd, N_TXTOFF(e), SEEK_SET);
135 size = read(fd, addr, e.a_text);
138 ret = read(fd, addr + N_DATADDR(e), e.a_data);
155 static void *load_at(int fd, int offset, int size)
158 if (lseek(fd, offset, SEEK_SET) < 0)
160 ptr = qemu_malloc(size);
163 if (read(fd, ptr, size) != size) {
171 #define ELF_CLASS ELFCLASS32
175 #define elf_word uint32_t
176 #define bswapSZs bswap32s
187 #define elfhdr elf64_hdr
188 #define elf_phdr elf64_phdr
189 #define elf_note elf64_note
190 #define elf_shdr elf64_shdr
191 #define elf_sym elf64_sym
192 #define elf_word uint64_t
193 #define bswapSZs bswap64s
197 /* return < 0 if error, otherwise the number of bytes loaded in memory */
198 int load_elf(const char *filename, int64_t virt_to_phys_addend,
199 uint64_t *pentry, uint64_t *lowaddr, uint64_t *highaddr)
201 int fd, data_order, host_data_order, must_swab, ret;
202 uint8_t e_ident[EI_NIDENT];
204 fd = open(filename, O_RDONLY | O_BINARY);
209 if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
211 if (e_ident[0] != ELFMAG0 ||
212 e_ident[1] != ELFMAG1 ||
213 e_ident[2] != ELFMAG2 ||
214 e_ident[3] != ELFMAG3)
216 #ifdef WORDS_BIGENDIAN
217 data_order = ELFDATA2MSB;
219 data_order = ELFDATA2LSB;
221 must_swab = data_order != e_ident[EI_DATA];
223 #ifdef TARGET_WORDS_BIGENDIAN
224 host_data_order = ELFDATA2MSB;
226 host_data_order = ELFDATA2LSB;
228 if (host_data_order != e_ident[EI_DATA])
231 lseek(fd, 0, SEEK_SET);
232 if (e_ident[EI_CLASS] == ELFCLASS64) {
233 ret = load_elf64(fd, virt_to_phys_addend, must_swab, pentry,
236 ret = load_elf32(fd, virt_to_phys_addend, must_swab, pentry,
248 static void bswap_uboot_header(uboot_image_header_t *hdr)
250 #ifndef WORDS_BIGENDIAN
251 bswap32s(&hdr->ih_magic);
252 bswap32s(&hdr->ih_hcrc);
253 bswap32s(&hdr->ih_time);
254 bswap32s(&hdr->ih_size);
255 bswap32s(&hdr->ih_load);
256 bswap32s(&hdr->ih_ep);
257 bswap32s(&hdr->ih_dcrc);
261 /* Load a U-Boot image. */
262 int load_uboot(const char *filename, target_ulong *ep, int *is_linux)
267 uboot_image_header_t h;
268 uboot_image_header_t *hdr = &h;
269 uint8_t *data = NULL;
271 fd = open(filename, O_RDONLY | O_BINARY);
275 size = read(fd, hdr, sizeof(uboot_image_header_t));
279 bswap_uboot_header(hdr);
281 if (hdr->ih_magic != IH_MAGIC)
284 /* TODO: Implement Multi-File images. */
285 if (hdr->ih_type == IH_TYPE_MULTI) {
286 fprintf(stderr, "Unable to load multi-file u-boot images\n");
290 /* TODO: Implement compressed images. */
291 if (hdr->ih_comp != IH_COMP_NONE) {
292 fprintf(stderr, "Unable to load compressed u-boot images\n");
296 /* TODO: Check CPU type. */
298 if (hdr->ih_type == IH_TYPE_KERNEL && hdr->ih_os == IH_OS_LINUX)
305 data = qemu_malloc(hdr->ih_size);
309 if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
310 fprintf(stderr, "Error reading file\n");
314 cpu_physical_memory_write_rom(hdr->ih_load, data, hdr->ih_size);