Statistics
| Branch: | Revision:

root / hw / loader.c @ 6f3a7798

History | View | Annotate | Download (13.8 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 8167ee88 Blue Swirl
 * with this program; if not, see <http://www.gnu.org/licenses/>.
43 5fe141fd bellard
 */
44 5a123577 aliguori
45 ca20cf32 Blue Swirl
#include "hw.h"
46 5fe141fd bellard
#include "disas.h"
47 9596ebb7 pbrook
#include "sysemu.h"
48 1c7b3754 pbrook
#include "uboot_image.h"
49 ca20cf32 Blue Swirl
#include "loader.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 c227f099 Anthony Liguori
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 c227f099 Anthony Liguori
    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
94 293f78bc blueswir1
        cpu_physical_memory_write_rom(dst_addr, buf, did);
95 293f78bc blueswir1
        dst_addr += did;
96 293f78bc blueswir1
        nbytes -= did;
97 7d515c1d blueswir1
        if (did != want)
98 7d515c1d blueswir1
            break;
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 c227f099 Anthony Liguori
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 c227f099 Anthony Liguori
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 c227f099 Anthony Liguori
    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 c227f099 Anthony Liguori
                        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 c227f099 Anthony Liguori
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
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
188 5fe141fd bellard
#define N_MAGIC(exec) ((exec).a_info & 0xffff)
189 5fe141fd bellard
#define OMAGIC 0407
190 5fe141fd bellard
#define NMAGIC 0410
191 5fe141fd bellard
#define ZMAGIC 0413
192 5fe141fd bellard
#define QMAGIC 0314
193 5fe141fd bellard
#define _N_HDROFF(x) (1024 - sizeof (struct exec))
194 5fe141fd bellard
#define N_TXTOFF(x)                                                        \
195 5fe141fd bellard
    (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) :        \
196 5fe141fd bellard
     (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
197 ca20cf32 Blue Swirl
#define N_TXTADDR(x, target_page_size) (N_MAGIC(x) == QMAGIC ? target_page_size : 0)
198 ca20cf32 Blue Swirl
#define _N_SEGMENT_ROUND(x, target_page_size) (((x) + target_page_size - 1) & ~(target_page_size - 1))
199 5fe141fd bellard
200 ca20cf32 Blue Swirl
#define _N_TXTENDADDR(x, target_page_size) (N_TXTADDR(x, target_page_size)+(x).a_text)
201 5fe141fd bellard
202 ca20cf32 Blue Swirl
#define N_DATADDR(x, target_page_size) \
203 ca20cf32 Blue Swirl
    (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x, target_page_size)) \
204 ca20cf32 Blue Swirl
     : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x, target_page_size), target_page_size)))
205 5fe141fd bellard
206 5fe141fd bellard
207 c227f099 Anthony Liguori
int load_aout(const char *filename, target_phys_addr_t addr, int max_sz,
208 c227f099 Anthony Liguori
              int bswap_needed, target_phys_addr_t target_page_size)
209 5fe141fd bellard
{
210 5fe141fd bellard
    int fd, size, ret;
211 5fe141fd bellard
    struct exec e;
212 5fe141fd bellard
    uint32_t magic;
213 5fe141fd bellard
214 5fe141fd bellard
    fd = open(filename, O_RDONLY | O_BINARY);
215 5fe141fd bellard
    if (fd < 0)
216 5fe141fd bellard
        return -1;
217 5fe141fd bellard
218 5fe141fd bellard
    size = read(fd, &e, sizeof(e));
219 5fe141fd bellard
    if (size < 0)
220 5fe141fd bellard
        goto fail;
221 5fe141fd bellard
222 ca20cf32 Blue Swirl
    if (bswap_needed) {
223 ca20cf32 Blue Swirl
        bswap_ahdr(&e);
224 ca20cf32 Blue Swirl
    }
225 5fe141fd bellard
226 5fe141fd bellard
    magic = N_MAGIC(e);
227 5fe141fd bellard
    switch (magic) {
228 5fe141fd bellard
    case ZMAGIC:
229 5fe141fd bellard
    case QMAGIC:
230 5fe141fd bellard
    case OMAGIC:
231 293f78bc blueswir1
        if (e.a_text + e.a_data > max_sz)
232 293f78bc blueswir1
            goto fail;
233 5fe141fd bellard
        lseek(fd, N_TXTOFF(e), SEEK_SET);
234 293f78bc blueswir1
        size = read_targphys(fd, addr, e.a_text + e.a_data);
235 5fe141fd bellard
        if (size < 0)
236 5fe141fd bellard
            goto fail;
237 5fe141fd bellard
        break;
238 5fe141fd bellard
    case NMAGIC:
239 ca20cf32 Blue Swirl
        if (N_DATADDR(e, target_page_size) + e.a_data > max_sz)
240 293f78bc blueswir1
            goto fail;
241 5fe141fd bellard
        lseek(fd, N_TXTOFF(e), SEEK_SET);
242 293f78bc blueswir1
        size = read_targphys(fd, addr, e.a_text);
243 5fe141fd bellard
        if (size < 0)
244 5fe141fd bellard
            goto fail;
245 ca20cf32 Blue Swirl
        ret = read_targphys(fd, addr + N_DATADDR(e, target_page_size),
246 ca20cf32 Blue Swirl
                            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 (read(fd, ptr, size) != size) {
270 5fe141fd bellard
        qemu_free(ptr);
271 5fe141fd bellard
        return NULL;
272 5fe141fd bellard
    }
273 5fe141fd bellard
    return ptr;
274 5fe141fd bellard
}
275 5fe141fd bellard
276 3efa9a67 malc
#ifdef ELF_CLASS
277 3efa9a67 malc
#undef ELF_CLASS
278 3efa9a67 malc
#endif
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 ca20cf32 Blue Swirl
             uint64_t *pentry, uint64_t *lowaddr, uint64_t *highaddr,
312 ca20cf32 Blue Swirl
             int big_endian, int elf_machine, int clear_lsb)
313 5fe141fd bellard
{
314 ca20cf32 Blue Swirl
    int fd, data_order, target_data_order, must_swab, ret;
315 5fe141fd bellard
    uint8_t e_ident[EI_NIDENT];
316 5fe141fd bellard
317 699e4642 bellard
    fd = open(filename, O_RDONLY | O_BINARY);
318 5fe141fd bellard
    if (fd < 0) {
319 5fe141fd bellard
        perror(filename);
320 5fe141fd bellard
        return -1;
321 5fe141fd bellard
    }
322 5fe141fd bellard
    if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
323 5fe141fd bellard
        goto fail;
324 5fe141fd bellard
    if (e_ident[0] != ELFMAG0 ||
325 5fe141fd bellard
        e_ident[1] != ELFMAG1 ||
326 5fe141fd bellard
        e_ident[2] != ELFMAG2 ||
327 5fe141fd bellard
        e_ident[3] != ELFMAG3)
328 5fe141fd bellard
        goto fail;
329 e2542fe2 Juan Quintela
#ifdef HOST_WORDS_BIGENDIAN
330 5fe141fd bellard
    data_order = ELFDATA2MSB;
331 5fe141fd bellard
#else
332 5fe141fd bellard
    data_order = ELFDATA2LSB;
333 5fe141fd bellard
#endif
334 5fe141fd bellard
    must_swab = data_order != e_ident[EI_DATA];
335 ca20cf32 Blue Swirl
    if (big_endian) {
336 ca20cf32 Blue Swirl
        target_data_order = ELFDATA2MSB;
337 ca20cf32 Blue Swirl
    } else {
338 ca20cf32 Blue Swirl
        target_data_order = ELFDATA2LSB;
339 ca20cf32 Blue Swirl
    }
340 9042c0e2 ths
341 ca20cf32 Blue Swirl
    if (target_data_order != e_ident[EI_DATA])
342 9042c0e2 ths
        return -1;
343 9042c0e2 ths
344 5fe141fd bellard
    lseek(fd, 0, SEEK_SET);
345 5fe141fd bellard
    if (e_ident[EI_CLASS] == ELFCLASS64) {
346 83c1f87c pbrook
        ret = load_elf64(fd, address_offset, must_swab, pentry,
347 ca20cf32 Blue Swirl
                         lowaddr, highaddr, elf_machine, clear_lsb);
348 5fe141fd bellard
    } else {
349 83c1f87c pbrook
        ret = load_elf32(fd, address_offset, must_swab, pentry,
350 ca20cf32 Blue Swirl
                         lowaddr, highaddr, elf_machine, clear_lsb);
351 5fe141fd bellard
    }
352 5fe141fd bellard
353 5fe141fd bellard
    close(fd);
354 5fe141fd bellard
    return ret;
355 5fe141fd bellard
356 5fe141fd bellard
 fail:
357 5fe141fd bellard
    close(fd);
358 5fe141fd bellard
    return -1;
359 5fe141fd bellard
}
360 1c7b3754 pbrook
361 c227f099 Anthony Liguori
static void bswap_uboot_header(uboot_image_header_t *hdr)
362 1c7b3754 pbrook
{
363 e2542fe2 Juan Quintela
#ifndef HOST_WORDS_BIGENDIAN
364 1c7b3754 pbrook
    bswap32s(&hdr->ih_magic);
365 1c7b3754 pbrook
    bswap32s(&hdr->ih_hcrc);
366 1c7b3754 pbrook
    bswap32s(&hdr->ih_time);
367 1c7b3754 pbrook
    bswap32s(&hdr->ih_size);
368 1c7b3754 pbrook
    bswap32s(&hdr->ih_load);
369 1c7b3754 pbrook
    bswap32s(&hdr->ih_ep);
370 1c7b3754 pbrook
    bswap32s(&hdr->ih_dcrc);
371 1c7b3754 pbrook
#endif
372 1c7b3754 pbrook
}
373 1c7b3754 pbrook
374 5a123577 aliguori
375 5a123577 aliguori
#define ZALLOC_ALIGNMENT        16
376 5a123577 aliguori
377 5a123577 aliguori
static void *zalloc(void *x, unsigned items, unsigned size)
378 5a123577 aliguori
{
379 5a123577 aliguori
    void *p;
380 5a123577 aliguori
381 5a123577 aliguori
    size *= items;
382 5a123577 aliguori
    size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
383 5a123577 aliguori
384 5a123577 aliguori
    p = qemu_malloc(size);
385 5a123577 aliguori
386 5a123577 aliguori
    return (p);
387 5a123577 aliguori
}
388 5a123577 aliguori
389 d084eab6 Stefan Weil
static void zfree(void *x, void *addr)
390 5a123577 aliguori
{
391 5a123577 aliguori
    qemu_free(addr);
392 5a123577 aliguori
}
393 5a123577 aliguori
394 5a123577 aliguori
395 5a123577 aliguori
#define HEAD_CRC        2
396 5a123577 aliguori
#define EXTRA_FIELD        4
397 5a123577 aliguori
#define ORIG_NAME        8
398 5a123577 aliguori
#define COMMENT                0x10
399 5a123577 aliguori
#define RESERVED        0xe0
400 5a123577 aliguori
401 5a123577 aliguori
#define DEFLATED        8
402 5a123577 aliguori
403 5a123577 aliguori
/* This is the maximum in uboot, so if a uImage overflows this, it would
404 5a123577 aliguori
 * overflow on real hardware too. */
405 5a123577 aliguori
#define UBOOT_MAX_GUNZIP_BYTES 0x800000
406 5a123577 aliguori
407 5a123577 aliguori
static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src,
408 5a123577 aliguori
                      size_t srclen)
409 5a123577 aliguori
{
410 5a123577 aliguori
    z_stream s;
411 5a123577 aliguori
    ssize_t dstbytes;
412 5a123577 aliguori
    int r, i, flags;
413 5a123577 aliguori
414 5a123577 aliguori
    /* skip header */
415 5a123577 aliguori
    i = 10;
416 5a123577 aliguori
    flags = src[3];
417 5a123577 aliguori
    if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
418 5a123577 aliguori
        puts ("Error: Bad gzipped data\n");
419 5a123577 aliguori
        return -1;
420 5a123577 aliguori
    }
421 5a123577 aliguori
    if ((flags & EXTRA_FIELD) != 0)
422 5a123577 aliguori
        i = 12 + src[10] + (src[11] << 8);
423 5a123577 aliguori
    if ((flags & ORIG_NAME) != 0)
424 5a123577 aliguori
        while (src[i++] != 0)
425 5a123577 aliguori
            ;
426 5a123577 aliguori
    if ((flags & COMMENT) != 0)
427 5a123577 aliguori
        while (src[i++] != 0)
428 5a123577 aliguori
            ;
429 5a123577 aliguori
    if ((flags & HEAD_CRC) != 0)
430 5a123577 aliguori
        i += 2;
431 5a123577 aliguori
    if (i >= srclen) {
432 5a123577 aliguori
        puts ("Error: gunzip out of data in header\n");
433 5a123577 aliguori
        return -1;
434 5a123577 aliguori
    }
435 5a123577 aliguori
436 5a123577 aliguori
    s.zalloc = zalloc;
437 d084eab6 Stefan Weil
    s.zfree = zfree;
438 5a123577 aliguori
439 5a123577 aliguori
    r = inflateInit2(&s, -MAX_WBITS);
440 5a123577 aliguori
    if (r != Z_OK) {
441 5a123577 aliguori
        printf ("Error: inflateInit2() returned %d\n", r);
442 5a123577 aliguori
        return (-1);
443 5a123577 aliguori
    }
444 5a123577 aliguori
    s.next_in = src + i;
445 5a123577 aliguori
    s.avail_in = srclen - i;
446 5a123577 aliguori
    s.next_out = dst;
447 5a123577 aliguori
    s.avail_out = dstlen;
448 5a123577 aliguori
    r = inflate(&s, Z_FINISH);
449 5a123577 aliguori
    if (r != Z_OK && r != Z_STREAM_END) {
450 5a123577 aliguori
        printf ("Error: inflate() returned %d\n", r);
451 5a123577 aliguori
        return -1;
452 5a123577 aliguori
    }
453 5a123577 aliguori
    dstbytes = s.next_out - (unsigned char *) dst;
454 5a123577 aliguori
    inflateEnd(&s);
455 5a123577 aliguori
456 5a123577 aliguori
    return dstbytes;
457 5a123577 aliguori
}
458 5a123577 aliguori
459 1c7b3754 pbrook
/* Load a U-Boot image.  */
460 c227f099 Anthony Liguori
int load_uimage(const char *filename, target_phys_addr_t *ep,
461 c227f099 Anthony Liguori
                target_phys_addr_t *loadaddr, int *is_linux)
462 1c7b3754 pbrook
{
463 1c7b3754 pbrook
    int fd;
464 1c7b3754 pbrook
    int size;
465 c227f099 Anthony Liguori
    uboot_image_header_t h;
466 c227f099 Anthony Liguori
    uboot_image_header_t *hdr = &h;
467 1c7b3754 pbrook
    uint8_t *data = NULL;
468 265ca29a aliguori
    int ret = -1;
469 1c7b3754 pbrook
470 1c7b3754 pbrook
    fd = open(filename, O_RDONLY | O_BINARY);
471 1c7b3754 pbrook
    if (fd < 0)
472 1c7b3754 pbrook
        return -1;
473 1c7b3754 pbrook
474 c227f099 Anthony Liguori
    size = read(fd, hdr, sizeof(uboot_image_header_t));
475 1c7b3754 pbrook
    if (size < 0)
476 265ca29a aliguori
        goto out;
477 1c7b3754 pbrook
478 1c7b3754 pbrook
    bswap_uboot_header(hdr);
479 1c7b3754 pbrook
480 1c7b3754 pbrook
    if (hdr->ih_magic != IH_MAGIC)
481 265ca29a aliguori
        goto out;
482 1c7b3754 pbrook
483 7e5f90fa aliguori
    /* TODO: Implement other image types.  */
484 7e5f90fa aliguori
    if (hdr->ih_type != IH_TYPE_KERNEL) {
485 7e5f90fa aliguori
        fprintf(stderr, "Can only load u-boot image type \"kernel\"\n");
486 265ca29a aliguori
        goto out;
487 1c7b3754 pbrook
    }
488 1c7b3754 pbrook
489 5a123577 aliguori
    switch (hdr->ih_comp) {
490 5a123577 aliguori
    case IH_COMP_NONE:
491 5a123577 aliguori
    case IH_COMP_GZIP:
492 5a123577 aliguori
        break;
493 5a123577 aliguori
    default:
494 5a123577 aliguori
        fprintf(stderr,
495 5a123577 aliguori
                "Unable to load u-boot images with compression type %d\n",
496 5a123577 aliguori
                hdr->ih_comp);
497 265ca29a aliguori
        goto out;
498 1c7b3754 pbrook
    }
499 1c7b3754 pbrook
500 1c7b3754 pbrook
    /* TODO: Check CPU type.  */
501 1c7b3754 pbrook
    if (is_linux) {
502 7e5f90fa aliguori
        if (hdr->ih_os == IH_OS_LINUX)
503 1c7b3754 pbrook
            *is_linux = 1;
504 1c7b3754 pbrook
        else
505 1c7b3754 pbrook
            *is_linux = 0;
506 1c7b3754 pbrook
    }
507 1c7b3754 pbrook
508 1c7b3754 pbrook
    *ep = hdr->ih_ep;
509 1c7b3754 pbrook
    data = qemu_malloc(hdr->ih_size);
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
}