Statistics
| Branch: | Revision:

root / loader.c @ 8c5e95d8

History | View | Annotate | Download (10.2 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 5fe141fd bellard
 */
24 87ecb68b pbrook
#include "qemu-common.h"
25 5fe141fd bellard
#include "disas.h"
26 9596ebb7 pbrook
#include "sysemu.h"
27 1c7b3754 pbrook
#include "uboot_image.h"
28 5fe141fd bellard
29 5fe141fd bellard
/* return the size or -1 if error */
30 5fe141fd bellard
int get_image_size(const char *filename)
31 5fe141fd bellard
{
32 5fe141fd bellard
    int fd, size;
33 5fe141fd bellard
    fd = open(filename, O_RDONLY | O_BINARY);
34 5fe141fd bellard
    if (fd < 0)
35 5fe141fd bellard
        return -1;
36 5fe141fd bellard
    size = lseek(fd, 0, SEEK_END);
37 5fe141fd bellard
    close(fd);
38 5fe141fd bellard
    return size;
39 5fe141fd bellard
}
40 5fe141fd bellard
41 5fe141fd bellard
/* return the size or -1 if error */
42 293f78bc blueswir1
/* deprecated, because caller does not specify buffer size! */
43 5fe141fd bellard
int load_image(const char *filename, uint8_t *addr)
44 5fe141fd bellard
{
45 5fe141fd bellard
    int fd, size;
46 5fe141fd bellard
    fd = open(filename, O_RDONLY | O_BINARY);
47 5fe141fd bellard
    if (fd < 0)
48 5fe141fd bellard
        return -1;
49 5fe141fd bellard
    size = lseek(fd, 0, SEEK_END);
50 5fe141fd bellard
    lseek(fd, 0, SEEK_SET);
51 5fe141fd bellard
    if (read(fd, addr, size) != size) {
52 5fe141fd bellard
        close(fd);
53 5fe141fd bellard
        return -1;
54 5fe141fd bellard
    }
55 5fe141fd bellard
    close(fd);
56 5fe141fd bellard
    return size;
57 5fe141fd bellard
}
58 5fe141fd bellard
59 293f78bc blueswir1
/* return the amount read, just like fread.  0 may mean error or eof */
60 293f78bc blueswir1
int fread_targphys(target_phys_addr_t dst_addr, size_t nbytes, FILE *f)
61 293f78bc blueswir1
{
62 293f78bc blueswir1
    uint8_t buf[4096];
63 293f78bc blueswir1
    target_phys_addr_t dst_begin = dst_addr;
64 293f78bc blueswir1
    size_t want, did;
65 293f78bc blueswir1
66 293f78bc blueswir1
    while (nbytes) {
67 293f78bc blueswir1
        want = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
68 293f78bc blueswir1
        did = fread(buf, 1, want, f);
69 293f78bc blueswir1
        if (did != want) break;
70 293f78bc blueswir1
71 293f78bc blueswir1
        cpu_physical_memory_write_rom(dst_addr, buf, did);
72 293f78bc blueswir1
        dst_addr += did;
73 293f78bc blueswir1
        nbytes -= did;
74 293f78bc blueswir1
    }
75 293f78bc blueswir1
    return dst_addr - dst_begin;
76 293f78bc blueswir1
}
77 293f78bc blueswir1
78 293f78bc blueswir1
/* returns 0 on error, 1 if ok */
79 293f78bc blueswir1
int fread_targphys_ok(target_phys_addr_t dst_addr, size_t nbytes, FILE *f)
80 293f78bc blueswir1
{
81 293f78bc blueswir1
    return fread_targphys(dst_addr, nbytes, f) == nbytes;
82 293f78bc blueswir1
}
83 293f78bc blueswir1
84 293f78bc blueswir1
/* read()-like version */
85 293f78bc blueswir1
int read_targphys(int fd, target_phys_addr_t dst_addr, size_t nbytes)
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 = read(fd, buf, want);
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
/* return the size or -1 if error */
104 293f78bc blueswir1
int load_image_targphys(const char *filename,
105 293f78bc blueswir1
                        target_phys_addr_t addr, int max_sz)
106 293f78bc blueswir1
{
107 293f78bc blueswir1
    FILE *f;
108 293f78bc blueswir1
    size_t got;
109 293f78bc blueswir1
110 293f78bc blueswir1
    f = fopen(filename, "rb");
111 293f78bc blueswir1
    if (!f) return -1;
112 293f78bc blueswir1
113 293f78bc blueswir1
    got = fread_targphys(addr, max_sz, f);
114 293f78bc blueswir1
    if (ferror(f)) { fclose(f); return -1; }
115 293f78bc blueswir1
    fclose(f);
116 293f78bc blueswir1
117 293f78bc blueswir1
    return got;
118 293f78bc blueswir1
}
119 293f78bc blueswir1
120 293f78bc blueswir1
void pstrcpy_targphys(target_phys_addr_t dest, int buf_size,
121 293f78bc blueswir1
                      const char *source)
122 293f78bc blueswir1
{
123 293f78bc blueswir1
    static const uint8_t nul_byte = 0;
124 293f78bc blueswir1
    const char *nulp;
125 293f78bc blueswir1
126 293f78bc blueswir1
    if (buf_size <= 0) return;
127 293f78bc blueswir1
    nulp = memchr(source, 0, buf_size);
128 293f78bc blueswir1
    if (nulp) {
129 293f78bc blueswir1
        cpu_physical_memory_write_rom(dest, (uint8_t *)source,
130 293f78bc blueswir1
                                      (nulp - source) + 1);
131 293f78bc blueswir1
    } else {
132 293f78bc blueswir1
        cpu_physical_memory_write_rom(dest, (uint8_t *)source, buf_size - 1);
133 293f78bc blueswir1
        cpu_physical_memory_write_rom(dest, &nul_byte, 1);
134 293f78bc blueswir1
    }
135 293f78bc blueswir1
}
136 293f78bc blueswir1
137 5fe141fd bellard
/* A.OUT loader */
138 5fe141fd bellard
139 5fe141fd bellard
struct exec
140 5fe141fd bellard
{
141 5fe141fd bellard
  uint32_t a_info;   /* Use macros N_MAGIC, etc for access */
142 5fe141fd bellard
  uint32_t a_text;   /* length of text, in bytes */
143 5fe141fd bellard
  uint32_t a_data;   /* length of data, in bytes */
144 5fe141fd bellard
  uint32_t a_bss;    /* length of uninitialized data area, in bytes */
145 5fe141fd bellard
  uint32_t a_syms;   /* length of symbol table data in file, in bytes */
146 5fe141fd bellard
  uint32_t a_entry;  /* start address */
147 5fe141fd bellard
  uint32_t a_trsize; /* length of relocation info for text, in bytes */
148 5fe141fd bellard
  uint32_t a_drsize; /* length of relocation info for data, in bytes */
149 5fe141fd bellard
};
150 5fe141fd bellard
151 5fe141fd bellard
#ifdef BSWAP_NEEDED
152 5fe141fd bellard
static void bswap_ahdr(struct exec *e)
153 5fe141fd bellard
{
154 5fe141fd bellard
    bswap32s(&e->a_info);
155 5fe141fd bellard
    bswap32s(&e->a_text);
156 5fe141fd bellard
    bswap32s(&e->a_data);
157 5fe141fd bellard
    bswap32s(&e->a_bss);
158 5fe141fd bellard
    bswap32s(&e->a_syms);
159 5fe141fd bellard
    bswap32s(&e->a_entry);
160 5fe141fd bellard
    bswap32s(&e->a_trsize);
161 5fe141fd bellard
    bswap32s(&e->a_drsize);
162 5fe141fd bellard
}
163 5fe141fd bellard
#else
164 5fe141fd bellard
#define bswap_ahdr(x) do { } while (0)
165 5fe141fd bellard
#endif
166 5fe141fd bellard
167 5fe141fd bellard
#define N_MAGIC(exec) ((exec).a_info & 0xffff)
168 5fe141fd bellard
#define OMAGIC 0407
169 5fe141fd bellard
#define NMAGIC 0410
170 5fe141fd bellard
#define ZMAGIC 0413
171 5fe141fd bellard
#define QMAGIC 0314
172 5fe141fd bellard
#define _N_HDROFF(x) (1024 - sizeof (struct exec))
173 5fe141fd bellard
#define N_TXTOFF(x)                                                        \
174 5fe141fd bellard
    (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) :        \
175 5fe141fd bellard
     (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
176 5fe141fd bellard
#define N_TXTADDR(x) (N_MAGIC(x) == QMAGIC ? TARGET_PAGE_SIZE : 0)
177 5fe141fd bellard
#define N_DATOFF(x) (N_TXTOFF(x) + (x).a_text)
178 5fe141fd bellard
#define _N_SEGMENT_ROUND(x) (((x) + TARGET_PAGE_SIZE - 1) & ~(TARGET_PAGE_SIZE - 1))
179 5fe141fd bellard
180 5fe141fd bellard
#define _N_TXTENDADDR(x) (N_TXTADDR(x)+(x).a_text)
181 5fe141fd bellard
182 5fe141fd bellard
#define N_DATADDR(x) \
183 5fe141fd bellard
    (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x)) \
184 5fe141fd bellard
     : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x))))
185 5fe141fd bellard
186 5fe141fd bellard
187 293f78bc blueswir1
int load_aout(const char *filename, target_phys_addr_t addr, int max_sz)
188 5fe141fd bellard
{
189 5fe141fd bellard
    int fd, size, ret;
190 5fe141fd bellard
    struct exec e;
191 5fe141fd bellard
    uint32_t magic;
192 5fe141fd bellard
193 5fe141fd bellard
    fd = open(filename, O_RDONLY | O_BINARY);
194 5fe141fd bellard
    if (fd < 0)
195 5fe141fd bellard
        return -1;
196 5fe141fd bellard
197 5fe141fd bellard
    size = read(fd, &e, sizeof(e));
198 5fe141fd bellard
    if (size < 0)
199 5fe141fd bellard
        goto fail;
200 5fe141fd bellard
201 5fe141fd bellard
    bswap_ahdr(&e);
202 5fe141fd bellard
203 5fe141fd bellard
    magic = N_MAGIC(e);
204 5fe141fd bellard
    switch (magic) {
205 5fe141fd bellard
    case ZMAGIC:
206 5fe141fd bellard
    case QMAGIC:
207 5fe141fd bellard
    case OMAGIC:
208 293f78bc blueswir1
        if (e.a_text + e.a_data > max_sz)
209 293f78bc blueswir1
            goto fail;
210 5fe141fd bellard
        lseek(fd, N_TXTOFF(e), SEEK_SET);
211 293f78bc blueswir1
        size = read_targphys(fd, addr, e.a_text + e.a_data);
212 5fe141fd bellard
        if (size < 0)
213 5fe141fd bellard
            goto fail;
214 5fe141fd bellard
        break;
215 5fe141fd bellard
    case NMAGIC:
216 293f78bc blueswir1
        if (N_DATADDR(e) + e.a_data > max_sz)
217 293f78bc blueswir1
            goto fail;
218 5fe141fd bellard
        lseek(fd, N_TXTOFF(e), SEEK_SET);
219 293f78bc blueswir1
        size = read_targphys(fd, addr, e.a_text);
220 5fe141fd bellard
        if (size < 0)
221 5fe141fd bellard
            goto fail;
222 293f78bc blueswir1
        ret = read_targphys(fd, addr + N_DATADDR(e), e.a_data);
223 5fe141fd bellard
        if (ret < 0)
224 5fe141fd bellard
            goto fail;
225 5fe141fd bellard
        size += ret;
226 5fe141fd bellard
        break;
227 5fe141fd bellard
    default:
228 5fe141fd bellard
        goto fail;
229 5fe141fd bellard
    }
230 5fe141fd bellard
    close(fd);
231 5fe141fd bellard
    return size;
232 5fe141fd bellard
 fail:
233 5fe141fd bellard
    close(fd);
234 5fe141fd bellard
    return -1;
235 5fe141fd bellard
}
236 5fe141fd bellard
237 5fe141fd bellard
/* ELF loader */
238 5fe141fd bellard
239 5fe141fd bellard
static void *load_at(int fd, int offset, int size)
240 5fe141fd bellard
{
241 5fe141fd bellard
    void *ptr;
242 5fe141fd bellard
    if (lseek(fd, offset, SEEK_SET) < 0)
243 5fe141fd bellard
        return NULL;
244 5fe141fd bellard
    ptr = qemu_malloc(size);
245 5fe141fd bellard
    if (!ptr)
246 5fe141fd bellard
        return NULL;
247 5fe141fd bellard
    if (read(fd, ptr, size) != size) {
248 5fe141fd bellard
        qemu_free(ptr);
249 5fe141fd bellard
        return NULL;
250 5fe141fd bellard
    }
251 5fe141fd bellard
    return ptr;
252 5fe141fd bellard
}
253 5fe141fd bellard
254 5fe141fd bellard
255 5fe141fd bellard
#define ELF_CLASS   ELFCLASS32
256 5fe141fd bellard
#include "elf.h"
257 5fe141fd bellard
258 5fe141fd bellard
#define SZ                32
259 5fe141fd bellard
#define elf_word        uint32_t
260 82790064 ths
#define elf_sword        int32_t
261 5fe141fd bellard
#define bswapSZs        bswap32s
262 5fe141fd bellard
#include "elf_ops.h"
263 5fe141fd bellard
264 5fe141fd bellard
#undef elfhdr
265 5fe141fd bellard
#undef elf_phdr
266 5fe141fd bellard
#undef elf_shdr
267 5fe141fd bellard
#undef elf_sym
268 5fe141fd bellard
#undef elf_note
269 5fe141fd bellard
#undef elf_word
270 82790064 ths
#undef elf_sword
271 5fe141fd bellard
#undef bswapSZs
272 5fe141fd bellard
#undef SZ
273 5fe141fd bellard
#define elfhdr                elf64_hdr
274 5fe141fd bellard
#define elf_phdr        elf64_phdr
275 5fe141fd bellard
#define elf_note        elf64_note
276 5fe141fd bellard
#define elf_shdr        elf64_shdr
277 5fe141fd bellard
#define elf_sym                elf64_sym
278 5fe141fd bellard
#define elf_word        uint64_t
279 82790064 ths
#define elf_sword        int64_t
280 5fe141fd bellard
#define bswapSZs        bswap64s
281 5fe141fd bellard
#define SZ                64
282 5fe141fd bellard
#include "elf_ops.h"
283 5fe141fd bellard
284 5fe141fd bellard
/* return < 0 if error, otherwise the number of bytes loaded in memory */
285 9ee3c029 bellard
int load_elf(const char *filename, int64_t virt_to_phys_addend,
286 74287114 ths
             uint64_t *pentry, uint64_t *lowaddr, uint64_t *highaddr)
287 5fe141fd bellard
{
288 9042c0e2 ths
    int fd, data_order, host_data_order, must_swab, ret;
289 5fe141fd bellard
    uint8_t e_ident[EI_NIDENT];
290 5fe141fd bellard
291 699e4642 bellard
    fd = open(filename, O_RDONLY | O_BINARY);
292 5fe141fd bellard
    if (fd < 0) {
293 5fe141fd bellard
        perror(filename);
294 5fe141fd bellard
        return -1;
295 5fe141fd bellard
    }
296 5fe141fd bellard
    if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
297 5fe141fd bellard
        goto fail;
298 5fe141fd bellard
    if (e_ident[0] != ELFMAG0 ||
299 5fe141fd bellard
        e_ident[1] != ELFMAG1 ||
300 5fe141fd bellard
        e_ident[2] != ELFMAG2 ||
301 5fe141fd bellard
        e_ident[3] != ELFMAG3)
302 5fe141fd bellard
        goto fail;
303 5fe141fd bellard
#ifdef WORDS_BIGENDIAN
304 5fe141fd bellard
    data_order = ELFDATA2MSB;
305 5fe141fd bellard
#else
306 5fe141fd bellard
    data_order = ELFDATA2LSB;
307 5fe141fd bellard
#endif
308 5fe141fd bellard
    must_swab = data_order != e_ident[EI_DATA];
309 9042c0e2 ths
310 9042c0e2 ths
#ifdef TARGET_WORDS_BIGENDIAN
311 9042c0e2 ths
    host_data_order = ELFDATA2MSB;
312 9042c0e2 ths
#else
313 9042c0e2 ths
    host_data_order = ELFDATA2LSB;
314 9042c0e2 ths
#endif
315 9042c0e2 ths
    if (host_data_order != e_ident[EI_DATA])
316 9042c0e2 ths
        return -1;
317 9042c0e2 ths
318 5fe141fd bellard
    lseek(fd, 0, SEEK_SET);
319 5fe141fd bellard
    if (e_ident[EI_CLASS] == ELFCLASS64) {
320 74287114 ths
        ret = load_elf64(fd, virt_to_phys_addend, must_swab, pentry,
321 74287114 ths
                         lowaddr, highaddr);
322 5fe141fd bellard
    } else {
323 74287114 ths
        ret = load_elf32(fd, virt_to_phys_addend, must_swab, pentry,
324 74287114 ths
                         lowaddr, highaddr);
325 5fe141fd bellard
    }
326 5fe141fd bellard
327 5fe141fd bellard
    close(fd);
328 5fe141fd bellard
    return ret;
329 5fe141fd bellard
330 5fe141fd bellard
 fail:
331 5fe141fd bellard
    close(fd);
332 5fe141fd bellard
    return -1;
333 5fe141fd bellard
}
334 1c7b3754 pbrook
335 1c7b3754 pbrook
static void bswap_uboot_header(uboot_image_header_t *hdr)
336 1c7b3754 pbrook
{
337 1c7b3754 pbrook
#ifndef WORDS_BIGENDIAN
338 1c7b3754 pbrook
    bswap32s(&hdr->ih_magic);
339 1c7b3754 pbrook
    bswap32s(&hdr->ih_hcrc);
340 1c7b3754 pbrook
    bswap32s(&hdr->ih_time);
341 1c7b3754 pbrook
    bswap32s(&hdr->ih_size);
342 1c7b3754 pbrook
    bswap32s(&hdr->ih_load);
343 1c7b3754 pbrook
    bswap32s(&hdr->ih_ep);
344 1c7b3754 pbrook
    bswap32s(&hdr->ih_dcrc);
345 1c7b3754 pbrook
#endif
346 1c7b3754 pbrook
}
347 1c7b3754 pbrook
348 1c7b3754 pbrook
/* Load a U-Boot image.  */
349 1c7b3754 pbrook
int load_uboot(const char *filename, target_ulong *ep, int *is_linux)
350 1c7b3754 pbrook
{
351 3b46e624 ths
352 1c7b3754 pbrook
    int fd;
353 1c7b3754 pbrook
    int size;
354 1c7b3754 pbrook
    uboot_image_header_t h;
355 1c7b3754 pbrook
    uboot_image_header_t *hdr = &h;
356 1c7b3754 pbrook
    uint8_t *data = NULL;
357 1c7b3754 pbrook
358 1c7b3754 pbrook
    fd = open(filename, O_RDONLY | O_BINARY);
359 1c7b3754 pbrook
    if (fd < 0)
360 1c7b3754 pbrook
        return -1;
361 1c7b3754 pbrook
362 1c7b3754 pbrook
    size = read(fd, hdr, sizeof(uboot_image_header_t));
363 1c7b3754 pbrook
    if (size < 0)
364 1c7b3754 pbrook
        goto fail;
365 1c7b3754 pbrook
366 1c7b3754 pbrook
    bswap_uboot_header(hdr);
367 1c7b3754 pbrook
368 1c7b3754 pbrook
    if (hdr->ih_magic != IH_MAGIC)
369 1c7b3754 pbrook
        goto fail;
370 1c7b3754 pbrook
371 1c7b3754 pbrook
    /* TODO: Implement Multi-File images.  */
372 1c7b3754 pbrook
    if (hdr->ih_type == IH_TYPE_MULTI) {
373 1c7b3754 pbrook
        fprintf(stderr, "Unable to load multi-file u-boot images\n");
374 1c7b3754 pbrook
        goto fail;
375 1c7b3754 pbrook
    }
376 1c7b3754 pbrook
377 1c7b3754 pbrook
    /* TODO: Implement compressed images.  */
378 1c7b3754 pbrook
    if (hdr->ih_comp != IH_COMP_NONE) {
379 1c7b3754 pbrook
        fprintf(stderr, "Unable to load compressed u-boot images\n");
380 1c7b3754 pbrook
        goto fail;
381 1c7b3754 pbrook
    }
382 1c7b3754 pbrook
383 1c7b3754 pbrook
    /* TODO: Check CPU type.  */
384 1c7b3754 pbrook
    if (is_linux) {
385 1c7b3754 pbrook
        if (hdr->ih_type == IH_TYPE_KERNEL && hdr->ih_os == IH_OS_LINUX)
386 1c7b3754 pbrook
            *is_linux = 1;
387 1c7b3754 pbrook
        else
388 1c7b3754 pbrook
            *is_linux = 0;
389 1c7b3754 pbrook
    }
390 1c7b3754 pbrook
391 1c7b3754 pbrook
    *ep = hdr->ih_ep;
392 1c7b3754 pbrook
    data = qemu_malloc(hdr->ih_size);
393 1c7b3754 pbrook
    if (!data)
394 1c7b3754 pbrook
        goto fail;
395 1c7b3754 pbrook
396 1c7b3754 pbrook
    if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
397 1c7b3754 pbrook
        fprintf(stderr, "Error reading file\n");
398 1c7b3754 pbrook
        goto fail;
399 1c7b3754 pbrook
    }
400 1c7b3754 pbrook
401 1c7b3754 pbrook
    cpu_physical_memory_write_rom(hdr->ih_load, data, hdr->ih_size);
402 1c7b3754 pbrook
403 1c7b3754 pbrook
    return hdr->ih_size;
404 1c7b3754 pbrook
405 1c7b3754 pbrook
fail:
406 1c7b3754 pbrook
    if (data)
407 1c7b3754 pbrook
        qemu_free(data);
408 1c7b3754 pbrook
    close(fd);
409 1c7b3754 pbrook
    return -1;
410 1c7b3754 pbrook
}