Statistics
| Branch: | Revision:

root / loader.c @ 87ecb68b

History | View | Annotate | Download (8 kB)

1
/*
2
 * QEMU Executable loader
3
 *
4
 * Copyright (c) 2006 Fabrice Bellard
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
#include "qemu-common.h"
25
#include "disas.h"
26
#include "uboot_image.h"
27

    
28
/* return the size or -1 if error */
29
int get_image_size(const char *filename)
30
{
31
    int fd, size;
32
    fd = open(filename, O_RDONLY | O_BINARY);
33
    if (fd < 0)
34
        return -1;
35
    size = lseek(fd, 0, SEEK_END);
36
    close(fd);
37
    return size;
38
}
39

    
40
/* return the size or -1 if error */
41
int load_image(const char *filename, uint8_t *addr)
42
{
43
    int fd, size;
44
    fd = open(filename, O_RDONLY | O_BINARY);
45
    if (fd < 0)
46
        return -1;
47
    size = lseek(fd, 0, SEEK_END);
48
    lseek(fd, 0, SEEK_SET);
49
    if (read(fd, addr, size) != size) {
50
        close(fd);
51
        return -1;
52
    }
53
    close(fd);
54
    return size;
55
}
56

    
57
/* A.OUT loader */
58

    
59
struct exec
60
{
61
  uint32_t a_info;   /* Use macros N_MAGIC, etc for access */
62
  uint32_t a_text;   /* length of text, in bytes */
63
  uint32_t a_data;   /* length of data, in bytes */
64
  uint32_t a_bss;    /* length of uninitialized data area, in bytes */
65
  uint32_t a_syms;   /* length of symbol table data in file, in bytes */
66
  uint32_t a_entry;  /* start address */
67
  uint32_t a_trsize; /* length of relocation info for text, in bytes */
68
  uint32_t a_drsize; /* length of relocation info for data, in bytes */
69
};
70

    
71
#ifdef BSWAP_NEEDED
72
static void bswap_ahdr(struct exec *e)
73
{
74
    bswap32s(&e->a_info);
75
    bswap32s(&e->a_text);
76
    bswap32s(&e->a_data);
77
    bswap32s(&e->a_bss);
78
    bswap32s(&e->a_syms);
79
    bswap32s(&e->a_entry);
80
    bswap32s(&e->a_trsize);
81
    bswap32s(&e->a_drsize);
82
}
83
#else
84
#define bswap_ahdr(x) do { } while (0)
85
#endif
86

    
87
#define N_MAGIC(exec) ((exec).a_info & 0xffff)
88
#define OMAGIC 0407
89
#define NMAGIC 0410
90
#define ZMAGIC 0413
91
#define QMAGIC 0314
92
#define _N_HDROFF(x) (1024 - sizeof (struct exec))
93
#define N_TXTOFF(x)                                                        \
94
    (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) :        \
95
     (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
96
#define N_TXTADDR(x) (N_MAGIC(x) == QMAGIC ? TARGET_PAGE_SIZE : 0)
97
#define N_DATOFF(x) (N_TXTOFF(x) + (x).a_text)
98
#define _N_SEGMENT_ROUND(x) (((x) + TARGET_PAGE_SIZE - 1) & ~(TARGET_PAGE_SIZE - 1))
99

    
100
#define _N_TXTENDADDR(x) (N_TXTADDR(x)+(x).a_text)
101

    
102
#define N_DATADDR(x) \
103
    (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x)) \
104
     : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x))))
105

    
106

    
107
int load_aout(const char *filename, uint8_t *addr)
108
{
109
    int fd, size, ret;
110
    struct exec e;
111
    uint32_t magic;
112

    
113
    fd = open(filename, O_RDONLY | O_BINARY);
114
    if (fd < 0)
115
        return -1;
116

    
117
    size = read(fd, &e, sizeof(e));
118
    if (size < 0)
119
        goto fail;
120

    
121
    bswap_ahdr(&e);
122

    
123
    magic = N_MAGIC(e);
124
    switch (magic) {
125
    case ZMAGIC:
126
    case QMAGIC:
127
    case OMAGIC:
128
        lseek(fd, N_TXTOFF(e), SEEK_SET);
129
        size = read(fd, addr, e.a_text + e.a_data);
130
        if (size < 0)
131
            goto fail;
132
        break;
133
    case NMAGIC:
134
        lseek(fd, N_TXTOFF(e), SEEK_SET);
135
        size = read(fd, addr, e.a_text);
136
        if (size < 0)
137
            goto fail;
138
        ret = read(fd, addr + N_DATADDR(e), e.a_data);
139
        if (ret < 0)
140
            goto fail;
141
        size += ret;
142
        break;
143
    default:
144
        goto fail;
145
    }
146
    close(fd);
147
    return size;
148
 fail:
149
    close(fd);
150
    return -1;
151
}
152

    
153
/* ELF loader */
154

    
155
static void *load_at(int fd, int offset, int size)
156
{
157
    void *ptr;
158
    if (lseek(fd, offset, SEEK_SET) < 0)
159
        return NULL;
160
    ptr = qemu_malloc(size);
161
    if (!ptr)
162
        return NULL;
163
    if (read(fd, ptr, size) != size) {
164
        qemu_free(ptr);
165
        return NULL;
166
    }
167
    return ptr;
168
}
169

    
170

    
171
#define ELF_CLASS   ELFCLASS32
172
#include "elf.h"
173

    
174
#define SZ                32
175
#define elf_word        uint32_t
176
#define elf_sword        int32_t
177
#define bswapSZs        bswap32s
178
#include "elf_ops.h"
179

    
180
#undef elfhdr
181
#undef elf_phdr
182
#undef elf_shdr
183
#undef elf_sym
184
#undef elf_note
185
#undef elf_word
186
#undef elf_sword
187
#undef bswapSZs
188
#undef SZ
189
#define elfhdr                elf64_hdr
190
#define elf_phdr        elf64_phdr
191
#define elf_note        elf64_note
192
#define elf_shdr        elf64_shdr
193
#define elf_sym                elf64_sym
194
#define elf_word        uint64_t
195
#define elf_sword        int64_t
196
#define bswapSZs        bswap64s
197
#define SZ                64
198
#include "elf_ops.h"
199

    
200
/* return < 0 if error, otherwise the number of bytes loaded in memory */
201
int load_elf(const char *filename, int64_t virt_to_phys_addend,
202
             uint64_t *pentry, uint64_t *lowaddr, uint64_t *highaddr)
203
{
204
    int fd, data_order, host_data_order, must_swab, ret;
205
    uint8_t e_ident[EI_NIDENT];
206

    
207
    fd = open(filename, O_RDONLY | O_BINARY);
208
    if (fd < 0) {
209
        perror(filename);
210
        return -1;
211
    }
212
    if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
213
        goto fail;
214
    if (e_ident[0] != ELFMAG0 ||
215
        e_ident[1] != ELFMAG1 ||
216
        e_ident[2] != ELFMAG2 ||
217
        e_ident[3] != ELFMAG3)
218
        goto fail;
219
#ifdef WORDS_BIGENDIAN
220
    data_order = ELFDATA2MSB;
221
#else
222
    data_order = ELFDATA2LSB;
223
#endif
224
    must_swab = data_order != e_ident[EI_DATA];
225

    
226
#ifdef TARGET_WORDS_BIGENDIAN
227
    host_data_order = ELFDATA2MSB;
228
#else
229
    host_data_order = ELFDATA2LSB;
230
#endif
231
    if (host_data_order != e_ident[EI_DATA])
232
        return -1;
233

    
234
    lseek(fd, 0, SEEK_SET);
235
    if (e_ident[EI_CLASS] == ELFCLASS64) {
236
        ret = load_elf64(fd, virt_to_phys_addend, must_swab, pentry,
237
                         lowaddr, highaddr);
238
    } else {
239
        ret = load_elf32(fd, virt_to_phys_addend, must_swab, pentry,
240
                         lowaddr, highaddr);
241
    }
242

    
243
    close(fd);
244
    return ret;
245

    
246
 fail:
247
    close(fd);
248
    return -1;
249
}
250

    
251
static void bswap_uboot_header(uboot_image_header_t *hdr)
252
{
253
#ifndef WORDS_BIGENDIAN
254
    bswap32s(&hdr->ih_magic);
255
    bswap32s(&hdr->ih_hcrc);
256
    bswap32s(&hdr->ih_time);
257
    bswap32s(&hdr->ih_size);
258
    bswap32s(&hdr->ih_load);
259
    bswap32s(&hdr->ih_ep);
260
    bswap32s(&hdr->ih_dcrc);
261
#endif
262
}
263

    
264
/* Load a U-Boot image.  */
265
int load_uboot(const char *filename, target_ulong *ep, int *is_linux)
266
{
267

    
268
    int fd;
269
    int size;
270
    uboot_image_header_t h;
271
    uboot_image_header_t *hdr = &h;
272
    uint8_t *data = NULL;
273

    
274
    fd = open(filename, O_RDONLY | O_BINARY);
275
    if (fd < 0)
276
        return -1;
277

    
278
    size = read(fd, hdr, sizeof(uboot_image_header_t));
279
    if (size < 0)
280
        goto fail;
281

    
282
    bswap_uboot_header(hdr);
283

    
284
    if (hdr->ih_magic != IH_MAGIC)
285
        goto fail;
286

    
287
    /* TODO: Implement Multi-File images.  */
288
    if (hdr->ih_type == IH_TYPE_MULTI) {
289
        fprintf(stderr, "Unable to load multi-file u-boot images\n");
290
        goto fail;
291
    }
292

    
293
    /* TODO: Implement compressed images.  */
294
    if (hdr->ih_comp != IH_COMP_NONE) {
295
        fprintf(stderr, "Unable to load compressed u-boot images\n");
296
        goto fail;
297
    }
298

    
299
    /* TODO: Check CPU type.  */
300
    if (is_linux) {
301
        if (hdr->ih_type == IH_TYPE_KERNEL && hdr->ih_os == IH_OS_LINUX)
302
            *is_linux = 1;
303
        else
304
            *is_linux = 0;
305
    }
306

    
307
    *ep = hdr->ih_ep;
308
    data = qemu_malloc(hdr->ih_size);
309
    if (!data)
310
        goto fail;
311

    
312
    if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
313
        fprintf(stderr, "Error reading file\n");
314
        goto fail;
315
    }
316

    
317
    cpu_physical_memory_write_rom(hdr->ih_load, data, hdr->ih_size);
318

    
319
    return hdr->ih_size;
320

    
321
fail:
322
    if (data)
323
        qemu_free(data);
324
    close(fd);
325
    return -1;
326
}