Statistics
| Branch: | Revision:

root / loader.c @ 4efbe58f

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