Statistics
| Branch: | Revision:

root / loader.c @ 07ef34c3

History | View | Annotate | Download (13.6 kB)

1 5fe141fd bellard
/*
2 5fe141fd bellard
 * QEMU Executable loader
3 5fafdf24 ths
 *
4 5fe141fd bellard
 * Copyright (c) 2006 Fabrice Bellard
5 5fafdf24 ths
 *
6 5fe141fd bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 5fe141fd bellard
 * of this software and associated documentation files (the "Software"), to deal
8 5fe141fd bellard
 * in the Software without restriction, including without limitation the rights
9 5fe141fd bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 5fe141fd bellard
 * copies of the Software, and to permit persons to whom the Software is
11 5fe141fd bellard
 * furnished to do so, subject to the following conditions:
12 5fe141fd bellard
 *
13 5fe141fd bellard
 * The above copyright notice and this permission notice shall be included in
14 5fe141fd bellard
 * all copies or substantial portions of the Software.
15 5fe141fd bellard
 *
16 5fe141fd bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 5fe141fd bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 5fe141fd bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 5fe141fd bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 5fe141fd bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 5fe141fd bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 5fe141fd bellard
 * THE SOFTWARE.
23 5a123577 aliguori
 *
24 5a123577 aliguori
 * Gunzip functionality in this file is derived from u-boot:
25 5a123577 aliguori
 *
26 5a123577 aliguori
 * (C) Copyright 2008 Semihalf
27 5a123577 aliguori
 *
28 5a123577 aliguori
 * (C) Copyright 2000-2005
29 5a123577 aliguori
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
30 5a123577 aliguori
 *
31 5a123577 aliguori
 * This program is free software; you can redistribute it and/or
32 5a123577 aliguori
 * modify it under the terms of the GNU General Public License as
33 5a123577 aliguori
 * published by the Free Software Foundation; either version 2 of
34 5a123577 aliguori
 * the License, or (at your option) any later version.
35 5a123577 aliguori
 *
36 5a123577 aliguori
 * This program is distributed in the hope that it will be useful,
37 5a123577 aliguori
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 5a123577 aliguori
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the
39 5a123577 aliguori
 * GNU General Public License for more details.
40 5a123577 aliguori
 *
41 fad6cb1a aurel32
 * You should have received a copy of the GNU General Public License along
42 fad6cb1a aurel32
 * with this program; if not, write to the Free Software Foundation, Inc.,
43 fad6cb1a aurel32
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
44 5fe141fd bellard
 */
45 5a123577 aliguori
46 87ecb68b pbrook
#include "qemu-common.h"
47 5fe141fd bellard
#include "disas.h"
48 9596ebb7 pbrook
#include "sysemu.h"
49 1c7b3754 pbrook
#include "uboot_image.h"
50 5fe141fd bellard
51 5a123577 aliguori
#include <zlib.h>
52 5a123577 aliguori
53 5fe141fd bellard
/* return the size or -1 if error */
54 5fe141fd bellard
int get_image_size(const char *filename)
55 5fe141fd bellard
{
56 5fe141fd bellard
    int fd, size;
57 5fe141fd bellard
    fd = open(filename, O_RDONLY | O_BINARY);
58 5fe141fd bellard
    if (fd < 0)
59 5fe141fd bellard
        return -1;
60 5fe141fd bellard
    size = lseek(fd, 0, SEEK_END);
61 5fe141fd bellard
    close(fd);
62 5fe141fd bellard
    return size;
63 5fe141fd bellard
}
64 5fe141fd bellard
65 5fe141fd bellard
/* return the size or -1 if error */
66 293f78bc blueswir1
/* deprecated, because caller does not specify buffer size! */
67 5fe141fd bellard
int load_image(const char *filename, uint8_t *addr)
68 5fe141fd bellard
{
69 5fe141fd bellard
    int fd, size;
70 5fe141fd bellard
    fd = open(filename, O_RDONLY | O_BINARY);
71 5fe141fd bellard
    if (fd < 0)
72 5fe141fd bellard
        return -1;
73 5fe141fd bellard
    size = lseek(fd, 0, SEEK_END);
74 5fe141fd bellard
    lseek(fd, 0, SEEK_SET);
75 5fe141fd bellard
    if (read(fd, addr, size) != size) {
76 5fe141fd bellard
        close(fd);
77 5fe141fd bellard
        return -1;
78 5fe141fd bellard
    }
79 5fe141fd bellard
    close(fd);
80 5fe141fd bellard
    return size;
81 5fe141fd bellard
}
82 5fe141fd bellard
83 293f78bc blueswir1
/* return the amount read, just like fread.  0 may mean error or eof */
84 293f78bc blueswir1
int fread_targphys(target_phys_addr_t dst_addr, size_t nbytes, FILE *f)
85 293f78bc blueswir1
{
86 293f78bc blueswir1
    uint8_t buf[4096];
87 293f78bc blueswir1
    target_phys_addr_t dst_begin = dst_addr;
88 293f78bc blueswir1
    size_t want, did;
89 293f78bc blueswir1
90 293f78bc blueswir1
    while (nbytes) {
91 293f78bc blueswir1
        want = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
92 293f78bc blueswir1
        did = fread(buf, 1, want, f);
93 293f78bc blueswir1
        if (did != want) break;
94 293f78bc blueswir1
95 293f78bc blueswir1
        cpu_physical_memory_write_rom(dst_addr, buf, did);
96 293f78bc blueswir1
        dst_addr += did;
97 293f78bc blueswir1
        nbytes -= did;
98 293f78bc blueswir1
    }
99 293f78bc blueswir1
    return dst_addr - dst_begin;
100 293f78bc blueswir1
}
101 293f78bc blueswir1
102 293f78bc blueswir1
/* returns 0 on error, 1 if ok */
103 293f78bc blueswir1
int fread_targphys_ok(target_phys_addr_t dst_addr, size_t nbytes, FILE *f)
104 293f78bc blueswir1
{
105 293f78bc blueswir1
    return fread_targphys(dst_addr, nbytes, f) == nbytes;
106 293f78bc blueswir1
}
107 293f78bc blueswir1
108 293f78bc blueswir1
/* read()-like version */
109 293f78bc blueswir1
int read_targphys(int fd, target_phys_addr_t dst_addr, size_t nbytes)
110 293f78bc blueswir1
{
111 293f78bc blueswir1
    uint8_t buf[4096];
112 293f78bc blueswir1
    target_phys_addr_t dst_begin = dst_addr;
113 293f78bc blueswir1
    size_t want, did;
114 293f78bc blueswir1
115 293f78bc blueswir1
    while (nbytes) {
116 293f78bc blueswir1
        want = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
117 293f78bc blueswir1
        did = read(fd, buf, want);
118 293f78bc blueswir1
        if (did != want) break;
119 293f78bc blueswir1
120 293f78bc blueswir1
        cpu_physical_memory_write_rom(dst_addr, buf, did);
121 293f78bc blueswir1
        dst_addr += did;
122 293f78bc blueswir1
        nbytes -= did;
123 293f78bc blueswir1
    }
124 293f78bc blueswir1
    return dst_addr - dst_begin;
125 293f78bc blueswir1
}
126 293f78bc blueswir1
127 293f78bc blueswir1
/* return the size or -1 if error */
128 293f78bc blueswir1
int load_image_targphys(const char *filename,
129 293f78bc blueswir1
                        target_phys_addr_t addr, int max_sz)
130 293f78bc blueswir1
{
131 293f78bc blueswir1
    FILE *f;
132 293f78bc blueswir1
    size_t got;
133 293f78bc blueswir1
134 293f78bc blueswir1
    f = fopen(filename, "rb");
135 293f78bc blueswir1
    if (!f) return -1;
136 293f78bc blueswir1
137 293f78bc blueswir1
    got = fread_targphys(addr, max_sz, f);
138 293f78bc blueswir1
    if (ferror(f)) { fclose(f); return -1; }
139 293f78bc blueswir1
    fclose(f);
140 293f78bc blueswir1
141 293f78bc blueswir1
    return got;
142 293f78bc blueswir1
}
143 293f78bc blueswir1
144 293f78bc blueswir1
void pstrcpy_targphys(target_phys_addr_t dest, int buf_size,
145 293f78bc blueswir1
                      const char *source)
146 293f78bc blueswir1
{
147 293f78bc blueswir1
    static const uint8_t nul_byte = 0;
148 293f78bc blueswir1
    const char *nulp;
149 293f78bc blueswir1
150 293f78bc blueswir1
    if (buf_size <= 0) return;
151 293f78bc blueswir1
    nulp = memchr(source, 0, buf_size);
152 293f78bc blueswir1
    if (nulp) {
153 293f78bc blueswir1
        cpu_physical_memory_write_rom(dest, (uint8_t *)source,
154 293f78bc blueswir1
                                      (nulp - source) + 1);
155 293f78bc blueswir1
    } else {
156 293f78bc blueswir1
        cpu_physical_memory_write_rom(dest, (uint8_t *)source, buf_size - 1);
157 293f78bc blueswir1
        cpu_physical_memory_write_rom(dest, &nul_byte, 1);
158 293f78bc blueswir1
    }
159 293f78bc blueswir1
}
160 293f78bc blueswir1
161 5fe141fd bellard
/* A.OUT loader */
162 5fe141fd bellard
163 5fe141fd bellard
struct exec
164 5fe141fd bellard
{
165 5fe141fd bellard
  uint32_t a_info;   /* Use macros N_MAGIC, etc for access */
166 5fe141fd bellard
  uint32_t a_text;   /* length of text, in bytes */
167 5fe141fd bellard
  uint32_t a_data;   /* length of data, in bytes */
168 5fe141fd bellard
  uint32_t a_bss;    /* length of uninitialized data area, in bytes */
169 5fe141fd bellard
  uint32_t a_syms;   /* length of symbol table data in file, in bytes */
170 5fe141fd bellard
  uint32_t a_entry;  /* start address */
171 5fe141fd bellard
  uint32_t a_trsize; /* length of relocation info for text, in bytes */
172 5fe141fd bellard
  uint32_t a_drsize; /* length of relocation info for data, in bytes */
173 5fe141fd bellard
};
174 5fe141fd bellard
175 5fe141fd bellard
#ifdef BSWAP_NEEDED
176 5fe141fd bellard
static void bswap_ahdr(struct exec *e)
177 5fe141fd bellard
{
178 5fe141fd bellard
    bswap32s(&e->a_info);
179 5fe141fd bellard
    bswap32s(&e->a_text);
180 5fe141fd bellard
    bswap32s(&e->a_data);
181 5fe141fd bellard
    bswap32s(&e->a_bss);
182 5fe141fd bellard
    bswap32s(&e->a_syms);
183 5fe141fd bellard
    bswap32s(&e->a_entry);
184 5fe141fd bellard
    bswap32s(&e->a_trsize);
185 5fe141fd bellard
    bswap32s(&e->a_drsize);
186 5fe141fd bellard
}
187 5fe141fd bellard
#else
188 5fe141fd bellard
#define bswap_ahdr(x) do { } while (0)
189 5fe141fd bellard
#endif
190 5fe141fd bellard
191 5fe141fd bellard
#define N_MAGIC(exec) ((exec).a_info & 0xffff)
192 5fe141fd bellard
#define OMAGIC 0407
193 5fe141fd bellard
#define NMAGIC 0410
194 5fe141fd bellard
#define ZMAGIC 0413
195 5fe141fd bellard
#define QMAGIC 0314
196 5fe141fd bellard
#define _N_HDROFF(x) (1024 - sizeof (struct exec))
197 5fe141fd bellard
#define N_TXTOFF(x)                                                        \
198 5fe141fd bellard
    (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) :        \
199 5fe141fd bellard
     (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
200 5fe141fd bellard
#define N_TXTADDR(x) (N_MAGIC(x) == QMAGIC ? TARGET_PAGE_SIZE : 0)
201 5fe141fd bellard
#define N_DATOFF(x) (N_TXTOFF(x) + (x).a_text)
202 5fe141fd bellard
#define _N_SEGMENT_ROUND(x) (((x) + TARGET_PAGE_SIZE - 1) & ~(TARGET_PAGE_SIZE - 1))
203 5fe141fd bellard
204 5fe141fd bellard
#define _N_TXTENDADDR(x) (N_TXTADDR(x)+(x).a_text)
205 5fe141fd bellard
206 5fe141fd bellard
#define N_DATADDR(x) \
207 5fe141fd bellard
    (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x)) \
208 5fe141fd bellard
     : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x))))
209 5fe141fd bellard
210 5fe141fd bellard
211 293f78bc blueswir1
int load_aout(const char *filename, target_phys_addr_t addr, int max_sz)
212 5fe141fd bellard
{
213 5fe141fd bellard
    int fd, size, ret;
214 5fe141fd bellard
    struct exec e;
215 5fe141fd bellard
    uint32_t magic;
216 5fe141fd bellard
217 5fe141fd bellard
    fd = open(filename, O_RDONLY | O_BINARY);
218 5fe141fd bellard
    if (fd < 0)
219 5fe141fd bellard
        return -1;
220 5fe141fd bellard
221 5fe141fd bellard
    size = read(fd, &e, sizeof(e));
222 5fe141fd bellard
    if (size < 0)
223 5fe141fd bellard
        goto fail;
224 5fe141fd bellard
225 5fe141fd bellard
    bswap_ahdr(&e);
226 5fe141fd bellard
227 5fe141fd bellard
    magic = N_MAGIC(e);
228 5fe141fd bellard
    switch (magic) {
229 5fe141fd bellard
    case ZMAGIC:
230 5fe141fd bellard
    case QMAGIC:
231 5fe141fd bellard
    case OMAGIC:
232 293f78bc blueswir1
        if (e.a_text + e.a_data > max_sz)
233 293f78bc blueswir1
            goto fail;
234 5fe141fd bellard
        lseek(fd, N_TXTOFF(e), SEEK_SET);
235 293f78bc blueswir1
        size = read_targphys(fd, addr, e.a_text + e.a_data);
236 5fe141fd bellard
        if (size < 0)
237 5fe141fd bellard
            goto fail;
238 5fe141fd bellard
        break;
239 5fe141fd bellard
    case NMAGIC:
240 293f78bc blueswir1
        if (N_DATADDR(e) + e.a_data > max_sz)
241 293f78bc blueswir1
            goto fail;
242 5fe141fd bellard
        lseek(fd, N_TXTOFF(e), SEEK_SET);
243 293f78bc blueswir1
        size = read_targphys(fd, addr, e.a_text);
244 5fe141fd bellard
        if (size < 0)
245 5fe141fd bellard
            goto fail;
246 293f78bc blueswir1
        ret = read_targphys(fd, addr + N_DATADDR(e), e.a_data);
247 5fe141fd bellard
        if (ret < 0)
248 5fe141fd bellard
            goto fail;
249 5fe141fd bellard
        size += ret;
250 5fe141fd bellard
        break;
251 5fe141fd bellard
    default:
252 5fe141fd bellard
        goto fail;
253 5fe141fd bellard
    }
254 5fe141fd bellard
    close(fd);
255 5fe141fd bellard
    return size;
256 5fe141fd bellard
 fail:
257 5fe141fd bellard
    close(fd);
258 5fe141fd bellard
    return -1;
259 5fe141fd bellard
}
260 5fe141fd bellard
261 5fe141fd bellard
/* ELF loader */
262 5fe141fd bellard
263 5fe141fd bellard
static void *load_at(int fd, int offset, int size)
264 5fe141fd bellard
{
265 5fe141fd bellard
    void *ptr;
266 5fe141fd bellard
    if (lseek(fd, offset, SEEK_SET) < 0)
267 5fe141fd bellard
        return NULL;
268 5fe141fd bellard
    ptr = qemu_malloc(size);
269 5fe141fd bellard
    if (!ptr)
270 5fe141fd bellard
        return NULL;
271 5fe141fd bellard
    if (read(fd, ptr, size) != size) {
272 5fe141fd bellard
        qemu_free(ptr);
273 5fe141fd bellard
        return NULL;
274 5fe141fd bellard
    }
275 5fe141fd bellard
    return ptr;
276 5fe141fd bellard
}
277 5fe141fd bellard
278 5fe141fd bellard
279 5fe141fd bellard
#define ELF_CLASS   ELFCLASS32
280 5fe141fd bellard
#include "elf.h"
281 5fe141fd bellard
282 5fe141fd bellard
#define SZ                32
283 5fe141fd bellard
#define elf_word        uint32_t
284 82790064 ths
#define elf_sword        int32_t
285 5fe141fd bellard
#define bswapSZs        bswap32s
286 5fe141fd bellard
#include "elf_ops.h"
287 5fe141fd bellard
288 5fe141fd bellard
#undef elfhdr
289 5fe141fd bellard
#undef elf_phdr
290 5fe141fd bellard
#undef elf_shdr
291 5fe141fd bellard
#undef elf_sym
292 5fe141fd bellard
#undef elf_note
293 5fe141fd bellard
#undef elf_word
294 82790064 ths
#undef elf_sword
295 5fe141fd bellard
#undef bswapSZs
296 5fe141fd bellard
#undef SZ
297 5fe141fd bellard
#define elfhdr                elf64_hdr
298 5fe141fd bellard
#define elf_phdr        elf64_phdr
299 5fe141fd bellard
#define elf_note        elf64_note
300 5fe141fd bellard
#define elf_shdr        elf64_shdr
301 5fe141fd bellard
#define elf_sym                elf64_sym
302 5fe141fd bellard
#define elf_word        uint64_t
303 82790064 ths
#define elf_sword        int64_t
304 5fe141fd bellard
#define bswapSZs        bswap64s
305 5fe141fd bellard
#define SZ                64
306 5fe141fd bellard
#include "elf_ops.h"
307 5fe141fd bellard
308 5fe141fd bellard
/* return < 0 if error, otherwise the number of bytes loaded in memory */
309 83c1f87c pbrook
int load_elf(const char *filename, int64_t address_offset,
310 74287114 ths
             uint64_t *pentry, uint64_t *lowaddr, uint64_t *highaddr)
311 5fe141fd bellard
{
312 9042c0e2 ths
    int fd, data_order, host_data_order, must_swab, ret;
313 5fe141fd bellard
    uint8_t e_ident[EI_NIDENT];
314 5fe141fd bellard
315 699e4642 bellard
    fd = open(filename, O_RDONLY | O_BINARY);
316 5fe141fd bellard
    if (fd < 0) {
317 5fe141fd bellard
        perror(filename);
318 5fe141fd bellard
        return -1;
319 5fe141fd bellard
    }
320 5fe141fd bellard
    if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
321 5fe141fd bellard
        goto fail;
322 5fe141fd bellard
    if (e_ident[0] != ELFMAG0 ||
323 5fe141fd bellard
        e_ident[1] != ELFMAG1 ||
324 5fe141fd bellard
        e_ident[2] != ELFMAG2 ||
325 5fe141fd bellard
        e_ident[3] != ELFMAG3)
326 5fe141fd bellard
        goto fail;
327 5fe141fd bellard
#ifdef WORDS_BIGENDIAN
328 5fe141fd bellard
    data_order = ELFDATA2MSB;
329 5fe141fd bellard
#else
330 5fe141fd bellard
    data_order = ELFDATA2LSB;
331 5fe141fd bellard
#endif
332 5fe141fd bellard
    must_swab = data_order != e_ident[EI_DATA];
333 9042c0e2 ths
334 9042c0e2 ths
#ifdef TARGET_WORDS_BIGENDIAN
335 9042c0e2 ths
    host_data_order = ELFDATA2MSB;
336 9042c0e2 ths
#else
337 9042c0e2 ths
    host_data_order = ELFDATA2LSB;
338 9042c0e2 ths
#endif
339 9042c0e2 ths
    if (host_data_order != e_ident[EI_DATA])
340 9042c0e2 ths
        return -1;
341 9042c0e2 ths
342 5fe141fd bellard
    lseek(fd, 0, SEEK_SET);
343 5fe141fd bellard
    if (e_ident[EI_CLASS] == ELFCLASS64) {
344 83c1f87c pbrook
        ret = load_elf64(fd, address_offset, must_swab, pentry,
345 74287114 ths
                         lowaddr, highaddr);
346 5fe141fd bellard
    } else {
347 83c1f87c pbrook
        ret = load_elf32(fd, address_offset, must_swab, pentry,
348 74287114 ths
                         lowaddr, highaddr);
349 5fe141fd bellard
    }
350 5fe141fd bellard
351 5fe141fd bellard
    close(fd);
352 5fe141fd bellard
    return ret;
353 5fe141fd bellard
354 5fe141fd bellard
 fail:
355 5fe141fd bellard
    close(fd);
356 5fe141fd bellard
    return -1;
357 5fe141fd bellard
}
358 1c7b3754 pbrook
359 1c7b3754 pbrook
static void bswap_uboot_header(uboot_image_header_t *hdr)
360 1c7b3754 pbrook
{
361 1c7b3754 pbrook
#ifndef WORDS_BIGENDIAN
362 1c7b3754 pbrook
    bswap32s(&hdr->ih_magic);
363 1c7b3754 pbrook
    bswap32s(&hdr->ih_hcrc);
364 1c7b3754 pbrook
    bswap32s(&hdr->ih_time);
365 1c7b3754 pbrook
    bswap32s(&hdr->ih_size);
366 1c7b3754 pbrook
    bswap32s(&hdr->ih_load);
367 1c7b3754 pbrook
    bswap32s(&hdr->ih_ep);
368 1c7b3754 pbrook
    bswap32s(&hdr->ih_dcrc);
369 1c7b3754 pbrook
#endif
370 1c7b3754 pbrook
}
371 1c7b3754 pbrook
372 5a123577 aliguori
373 5a123577 aliguori
#define ZALLOC_ALIGNMENT        16
374 5a123577 aliguori
375 5a123577 aliguori
static void *zalloc(void *x, unsigned items, unsigned size)
376 5a123577 aliguori
{
377 5a123577 aliguori
    void *p;
378 5a123577 aliguori
379 5a123577 aliguori
    size *= items;
380 5a123577 aliguori
    size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
381 5a123577 aliguori
382 5a123577 aliguori
    p = qemu_malloc(size);
383 5a123577 aliguori
384 5a123577 aliguori
    return (p);
385 5a123577 aliguori
}
386 5a123577 aliguori
387 5a123577 aliguori
static void zfree(void *x, void *addr, unsigned nb)
388 5a123577 aliguori
{
389 5a123577 aliguori
    qemu_free(addr);
390 5a123577 aliguori
}
391 5a123577 aliguori
392 5a123577 aliguori
393 5a123577 aliguori
#define HEAD_CRC        2
394 5a123577 aliguori
#define EXTRA_FIELD        4
395 5a123577 aliguori
#define ORIG_NAME        8
396 5a123577 aliguori
#define COMMENT                0x10
397 5a123577 aliguori
#define RESERVED        0xe0
398 5a123577 aliguori
399 5a123577 aliguori
#define DEFLATED        8
400 5a123577 aliguori
401 5a123577 aliguori
/* This is the maximum in uboot, so if a uImage overflows this, it would
402 5a123577 aliguori
 * overflow on real hardware too. */
403 5a123577 aliguori
#define UBOOT_MAX_GUNZIP_BYTES 0x800000
404 5a123577 aliguori
405 5a123577 aliguori
static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src,
406 5a123577 aliguori
                      size_t srclen)
407 5a123577 aliguori
{
408 5a123577 aliguori
    z_stream s;
409 5a123577 aliguori
    ssize_t dstbytes;
410 5a123577 aliguori
    int r, i, flags;
411 5a123577 aliguori
412 5a123577 aliguori
    /* skip header */
413 5a123577 aliguori
    i = 10;
414 5a123577 aliguori
    flags = src[3];
415 5a123577 aliguori
    if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
416 5a123577 aliguori
        puts ("Error: Bad gzipped data\n");
417 5a123577 aliguori
        return -1;
418 5a123577 aliguori
    }
419 5a123577 aliguori
    if ((flags & EXTRA_FIELD) != 0)
420 5a123577 aliguori
        i = 12 + src[10] + (src[11] << 8);
421 5a123577 aliguori
    if ((flags & ORIG_NAME) != 0)
422 5a123577 aliguori
        while (src[i++] != 0)
423 5a123577 aliguori
            ;
424 5a123577 aliguori
    if ((flags & COMMENT) != 0)
425 5a123577 aliguori
        while (src[i++] != 0)
426 5a123577 aliguori
            ;
427 5a123577 aliguori
    if ((flags & HEAD_CRC) != 0)
428 5a123577 aliguori
        i += 2;
429 5a123577 aliguori
    if (i >= srclen) {
430 5a123577 aliguori
        puts ("Error: gunzip out of data in header\n");
431 5a123577 aliguori
        return -1;
432 5a123577 aliguori
    }
433 5a123577 aliguori
434 5a123577 aliguori
    s.zalloc = zalloc;
435 5a123577 aliguori
    s.zfree = (free_func)zfree;
436 5a123577 aliguori
437 5a123577 aliguori
    r = inflateInit2(&s, -MAX_WBITS);
438 5a123577 aliguori
    if (r != Z_OK) {
439 5a123577 aliguori
        printf ("Error: inflateInit2() returned %d\n", r);
440 5a123577 aliguori
        return (-1);
441 5a123577 aliguori
    }
442 5a123577 aliguori
    s.next_in = src + i;
443 5a123577 aliguori
    s.avail_in = srclen - i;
444 5a123577 aliguori
    s.next_out = dst;
445 5a123577 aliguori
    s.avail_out = dstlen;
446 5a123577 aliguori
    r = inflate(&s, Z_FINISH);
447 5a123577 aliguori
    if (r != Z_OK && r != Z_STREAM_END) {
448 5a123577 aliguori
        printf ("Error: inflate() returned %d\n", r);
449 5a123577 aliguori
        return -1;
450 5a123577 aliguori
    }
451 5a123577 aliguori
    dstbytes = s.next_out - (unsigned char *) dst;
452 5a123577 aliguori
    inflateEnd(&s);
453 5a123577 aliguori
454 5a123577 aliguori
    return dstbytes;
455 5a123577 aliguori
}
456 5a123577 aliguori
457 1c7b3754 pbrook
/* Load a U-Boot image.  */
458 5a9154e0 aliguori
int load_uimage(const char *filename, target_ulong *ep, target_ulong *loadaddr,
459 5a9154e0 aliguori
                int *is_linux)
460 1c7b3754 pbrook
{
461 1c7b3754 pbrook
    int fd;
462 1c7b3754 pbrook
    int size;
463 1c7b3754 pbrook
    uboot_image_header_t h;
464 1c7b3754 pbrook
    uboot_image_header_t *hdr = &h;
465 1c7b3754 pbrook
    uint8_t *data = NULL;
466 265ca29a aliguori
    int ret = -1;
467 1c7b3754 pbrook
468 1c7b3754 pbrook
    fd = open(filename, O_RDONLY | O_BINARY);
469 1c7b3754 pbrook
    if (fd < 0)
470 1c7b3754 pbrook
        return -1;
471 1c7b3754 pbrook
472 1c7b3754 pbrook
    size = read(fd, hdr, sizeof(uboot_image_header_t));
473 1c7b3754 pbrook
    if (size < 0)
474 265ca29a aliguori
        goto out;
475 1c7b3754 pbrook
476 1c7b3754 pbrook
    bswap_uboot_header(hdr);
477 1c7b3754 pbrook
478 1c7b3754 pbrook
    if (hdr->ih_magic != IH_MAGIC)
479 265ca29a aliguori
        goto out;
480 1c7b3754 pbrook
481 7e5f90fa aliguori
    /* TODO: Implement other image types.  */
482 7e5f90fa aliguori
    if (hdr->ih_type != IH_TYPE_KERNEL) {
483 7e5f90fa aliguori
        fprintf(stderr, "Can only load u-boot image type \"kernel\"\n");
484 265ca29a aliguori
        goto out;
485 1c7b3754 pbrook
    }
486 1c7b3754 pbrook
487 5a123577 aliguori
    switch (hdr->ih_comp) {
488 5a123577 aliguori
    case IH_COMP_NONE:
489 5a123577 aliguori
    case IH_COMP_GZIP:
490 5a123577 aliguori
        break;
491 5a123577 aliguori
    default:
492 5a123577 aliguori
        fprintf(stderr,
493 5a123577 aliguori
                "Unable to load u-boot images with compression type %d\n",
494 5a123577 aliguori
                hdr->ih_comp);
495 265ca29a aliguori
        goto out;
496 1c7b3754 pbrook
    }
497 1c7b3754 pbrook
498 1c7b3754 pbrook
    /* TODO: Check CPU type.  */
499 1c7b3754 pbrook
    if (is_linux) {
500 7e5f90fa aliguori
        if (hdr->ih_os == IH_OS_LINUX)
501 1c7b3754 pbrook
            *is_linux = 1;
502 1c7b3754 pbrook
        else
503 1c7b3754 pbrook
            *is_linux = 0;
504 1c7b3754 pbrook
    }
505 1c7b3754 pbrook
506 1c7b3754 pbrook
    *ep = hdr->ih_ep;
507 1c7b3754 pbrook
    data = qemu_malloc(hdr->ih_size);
508 1c7b3754 pbrook
    if (!data)
509 265ca29a aliguori
        goto out;
510 1c7b3754 pbrook
511 1c7b3754 pbrook
    if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
512 1c7b3754 pbrook
        fprintf(stderr, "Error reading file\n");
513 265ca29a aliguori
        goto out;
514 1c7b3754 pbrook
    }
515 1c7b3754 pbrook
516 5a123577 aliguori
    if (hdr->ih_comp == IH_COMP_GZIP) {
517 5a123577 aliguori
        uint8_t *compressed_data;
518 5a123577 aliguori
        size_t max_bytes;
519 5a123577 aliguori
        ssize_t bytes;
520 5a123577 aliguori
521 5a123577 aliguori
        compressed_data = data;
522 5a123577 aliguori
        max_bytes = UBOOT_MAX_GUNZIP_BYTES;
523 5a123577 aliguori
        data = qemu_malloc(max_bytes);
524 5a123577 aliguori
525 5a123577 aliguori
        bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size);
526 5a123577 aliguori
        qemu_free(compressed_data);
527 5a123577 aliguori
        if (bytes < 0) {
528 5a123577 aliguori
            fprintf(stderr, "Unable to decompress gzipped image!\n");
529 5a123577 aliguori
            goto out;
530 5a123577 aliguori
        }
531 5a123577 aliguori
        hdr->ih_size = bytes;
532 5a123577 aliguori
    }
533 5a123577 aliguori
534 1c7b3754 pbrook
    cpu_physical_memory_write_rom(hdr->ih_load, data, hdr->ih_size);
535 1c7b3754 pbrook
536 21cafd08 aliguori
    if (loadaddr)
537 21cafd08 aliguori
        *loadaddr = hdr->ih_load;
538 21cafd08 aliguori
539 265ca29a aliguori
    ret = hdr->ih_size;
540 1c7b3754 pbrook
541 265ca29a aliguori
out:
542 1c7b3754 pbrook
    if (data)
543 1c7b3754 pbrook
        qemu_free(data);
544 1c7b3754 pbrook
    close(fd);
545 265ca29a aliguori
    return ret;
546 1c7b3754 pbrook
}