Statistics
| Branch: | Revision:

root / hw / loader.c @ 99a0949b

History | View | Annotate | Download (13.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
 * Gunzip functionality in this file is derived from u-boot:
25
 *
26
 * (C) Copyright 2008 Semihalf
27
 *
28
 * (C) Copyright 2000-2005
29
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
30
 *
31
 * This program is free software; you can redistribute it and/or
32
 * modify it under the terms of the GNU General Public License as
33
 * published by the Free Software Foundation; either version 2 of
34
 * the License, or (at your option) any later version.
35
 *
36
 * This program is distributed in the hope that it will be useful,
37
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the
39
 * GNU General Public License for more details.
40
 *
41
 * You should have received a copy of the GNU General Public License along
42
 * with this program; if not, see <http://www.gnu.org/licenses/>.
43
 */
44

    
45
#include "hw.h"
46
#include "disas.h"
47
#include "sysemu.h"
48
#include "uboot_image.h"
49
#include "loader.h"
50

    
51
#include <zlib.h>
52

    
53
/* return the size or -1 if error */
54
int get_image_size(const char *filename)
55
{
56
    int fd, size;
57
    fd = open(filename, O_RDONLY | O_BINARY);
58
    if (fd < 0)
59
        return -1;
60
    size = lseek(fd, 0, SEEK_END);
61
    close(fd);
62
    return size;
63
}
64

    
65
/* return the size or -1 if error */
66
/* deprecated, because caller does not specify buffer size! */
67
int load_image(const char *filename, uint8_t *addr)
68
{
69
    int fd, size;
70
    fd = open(filename, O_RDONLY | O_BINARY);
71
    if (fd < 0)
72
        return -1;
73
    size = lseek(fd, 0, SEEK_END);
74
    lseek(fd, 0, SEEK_SET);
75
    if (read(fd, addr, size) != size) {
76
        close(fd);
77
        return -1;
78
    }
79
    close(fd);
80
    return size;
81
}
82

    
83
/* return the amount read, just like fread.  0 may mean error or eof */
84
int fread_targphys(a_target_phys_addr dst_addr, size_t nbytes, FILE *f)
85
{
86
    uint8_t buf[4096];
87
    a_target_phys_addr dst_begin = dst_addr;
88
    size_t want, did;
89

    
90
    while (nbytes) {
91
        want = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
92
        did = fread(buf, 1, want, f);
93

    
94
        cpu_physical_memory_write_rom(dst_addr, buf, did);
95
        dst_addr += did;
96
        nbytes -= did;
97
        if (did != want)
98
            break;
99
    }
100
    return dst_addr - dst_begin;
101
}
102

    
103
/* returns 0 on error, 1 if ok */
104
int fread_targphys_ok(a_target_phys_addr dst_addr, size_t nbytes, FILE *f)
105
{
106
    return fread_targphys(dst_addr, nbytes, f) == nbytes;
107
}
108

    
109
/* read()-like version */
110
int read_targphys(int fd, a_target_phys_addr dst_addr, size_t nbytes)
111
{
112
    uint8_t buf[4096];
113
    a_target_phys_addr dst_begin = dst_addr;
114
    size_t want, did;
115

    
116
    while (nbytes) {
117
        want = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
118
        did = read(fd, buf, want);
119
        if (did != want) break;
120

    
121
        cpu_physical_memory_write_rom(dst_addr, buf, did);
122
        dst_addr += did;
123
        nbytes -= did;
124
    }
125
    return dst_addr - dst_begin;
126
}
127

    
128
/* return the size or -1 if error */
129
int load_image_targphys(const char *filename,
130
                        a_target_phys_addr addr, int max_sz)
131
{
132
    FILE *f;
133
    size_t got;
134

    
135
    f = fopen(filename, "rb");
136
    if (!f) return -1;
137

    
138
    got = fread_targphys(addr, max_sz, f);
139
    if (ferror(f)) { fclose(f); return -1; }
140
    fclose(f);
141

    
142
    return got;
143
}
144

    
145
void pstrcpy_targphys(a_target_phys_addr dest, int buf_size,
146
                      const char *source)
147
{
148
    static const uint8_t nul_byte = 0;
149
    const char *nulp;
150

    
151
    if (buf_size <= 0) return;
152
    nulp = memchr(source, 0, buf_size);
153
    if (nulp) {
154
        cpu_physical_memory_write_rom(dest, (uint8_t *)source,
155
                                      (nulp - source) + 1);
156
    } else {
157
        cpu_physical_memory_write_rom(dest, (uint8_t *)source, buf_size - 1);
158
        cpu_physical_memory_write_rom(dest, &nul_byte, 1);
159
    }
160
}
161

    
162
/* A.OUT loader */
163

    
164
struct exec
165
{
166
  uint32_t a_info;   /* Use macros N_MAGIC, etc for access */
167
  uint32_t a_text;   /* length of text, in bytes */
168
  uint32_t a_data;   /* length of data, in bytes */
169
  uint32_t a_bss;    /* length of uninitialized data area, in bytes */
170
  uint32_t a_syms;   /* length of symbol table data in file, in bytes */
171
  uint32_t a_entry;  /* start address */
172
  uint32_t a_trsize; /* length of relocation info for text, in bytes */
173
  uint32_t a_drsize; /* length of relocation info for data, in bytes */
174
};
175

    
176
static void bswap_ahdr(struct exec *e)
177
{
178
    bswap32s(&e->a_info);
179
    bswap32s(&e->a_text);
180
    bswap32s(&e->a_data);
181
    bswap32s(&e->a_bss);
182
    bswap32s(&e->a_syms);
183
    bswap32s(&e->a_entry);
184
    bswap32s(&e->a_trsize);
185
    bswap32s(&e->a_drsize);
186
}
187

    
188
#define N_MAGIC(exec) ((exec).a_info & 0xffff)
189
#define OMAGIC 0407
190
#define NMAGIC 0410
191
#define ZMAGIC 0413
192
#define QMAGIC 0314
193
#define _N_HDROFF(x) (1024 - sizeof (struct exec))
194
#define N_TXTOFF(x)                                                        \
195
    (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) :        \
196
     (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
197
#define N_TXTADDR(x, target_page_size) (N_MAGIC(x) == QMAGIC ? target_page_size : 0)
198
#define _N_SEGMENT_ROUND(x, target_page_size) (((x) + target_page_size - 1) & ~(target_page_size - 1))
199

    
200
#define _N_TXTENDADDR(x, target_page_size) (N_TXTADDR(x, target_page_size)+(x).a_text)
201

    
202
#define N_DATADDR(x, target_page_size) \
203
    (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x, target_page_size)) \
204
     : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x, target_page_size), target_page_size)))
205

    
206

    
207
int load_aout(const char *filename, a_target_phys_addr addr, int max_sz,
208
              int bswap_needed, a_target_phys_addr target_page_size)
209
{
210
    int fd, size, ret;
211
    struct exec e;
212
    uint32_t magic;
213

    
214
    fd = open(filename, O_RDONLY | O_BINARY);
215
    if (fd < 0)
216
        return -1;
217

    
218
    size = read(fd, &e, sizeof(e));
219
    if (size < 0)
220
        goto fail;
221

    
222
    if (bswap_needed) {
223
        bswap_ahdr(&e);
224
    }
225

    
226
    magic = N_MAGIC(e);
227
    switch (magic) {
228
    case ZMAGIC:
229
    case QMAGIC:
230
    case OMAGIC:
231
        if (e.a_text + e.a_data > max_sz)
232
            goto fail;
233
        lseek(fd, N_TXTOFF(e), SEEK_SET);
234
        size = read_targphys(fd, addr, e.a_text + e.a_data);
235
        if (size < 0)
236
            goto fail;
237
        break;
238
    case NMAGIC:
239
        if (N_DATADDR(e, target_page_size) + e.a_data > max_sz)
240
            goto fail;
241
        lseek(fd, N_TXTOFF(e), SEEK_SET);
242
        size = read_targphys(fd, addr, e.a_text);
243
        if (size < 0)
244
            goto fail;
245
        ret = read_targphys(fd, addr + N_DATADDR(e, target_page_size),
246
                            e.a_data);
247
        if (ret < 0)
248
            goto fail;
249
        size += ret;
250
        break;
251
    default:
252
        goto fail;
253
    }
254
    close(fd);
255
    return size;
256
 fail:
257
    close(fd);
258
    return -1;
259
}
260

    
261
/* ELF loader */
262

    
263
static void *load_at(int fd, int offset, int size)
264
{
265
    void *ptr;
266
    if (lseek(fd, offset, SEEK_SET) < 0)
267
        return NULL;
268
    ptr = qemu_malloc(size);
269
    if (read(fd, ptr, size) != size) {
270
        qemu_free(ptr);
271
        return NULL;
272
    }
273
    return ptr;
274
}
275

    
276
#ifdef ELF_CLASS
277
#undef ELF_CLASS
278
#endif
279

    
280
#define ELF_CLASS   ELFCLASS32
281
#include "elf.h"
282

    
283
#define SZ                32
284
#define elf_word        uint32_t
285
#define elf_sword        int32_t
286
#define bswapSZs        bswap32s
287
#include "elf_ops.h"
288

    
289
#undef elfhdr
290
#undef elf_phdr
291
#undef elf_shdr
292
#undef elf_sym
293
#undef elf_note
294
#undef elf_word
295
#undef elf_sword
296
#undef bswapSZs
297
#undef SZ
298
#define elfhdr                elf64_hdr
299
#define elf_phdr        elf64_phdr
300
#define elf_note        elf64_note
301
#define elf_shdr        elf64_shdr
302
#define elf_sym                elf64_sym
303
#define elf_word        uint64_t
304
#define elf_sword        int64_t
305
#define bswapSZs        bswap64s
306
#define SZ                64
307
#include "elf_ops.h"
308

    
309
/* return < 0 if error, otherwise the number of bytes loaded in memory */
310
int load_elf(const char *filename, int64_t address_offset,
311
             uint64_t *pentry, uint64_t *lowaddr, uint64_t *highaddr,
312
             int big_endian, int elf_machine, int clear_lsb)
313
{
314
    int fd, data_order, target_data_order, must_swab, ret;
315
    uint8_t e_ident[EI_NIDENT];
316

    
317
    fd = open(filename, O_RDONLY | O_BINARY);
318
    if (fd < 0) {
319
        perror(filename);
320
        return -1;
321
    }
322
    if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
323
        goto fail;
324
    if (e_ident[0] != ELFMAG0 ||
325
        e_ident[1] != ELFMAG1 ||
326
        e_ident[2] != ELFMAG2 ||
327
        e_ident[3] != ELFMAG3)
328
        goto fail;
329
#ifdef HOST_WORDS_BIGENDIAN
330
    data_order = ELFDATA2MSB;
331
#else
332
    data_order = ELFDATA2LSB;
333
#endif
334
    must_swab = data_order != e_ident[EI_DATA];
335
    if (big_endian) {
336
        target_data_order = ELFDATA2MSB;
337
    } else {
338
        target_data_order = ELFDATA2LSB;
339
    }
340

    
341
    if (target_data_order != e_ident[EI_DATA])
342
        return -1;
343

    
344
    lseek(fd, 0, SEEK_SET);
345
    if (e_ident[EI_CLASS] == ELFCLASS64) {
346
        ret = load_elf64(fd, address_offset, must_swab, pentry,
347
                         lowaddr, highaddr, elf_machine, clear_lsb);
348
    } else {
349
        ret = load_elf32(fd, address_offset, must_swab, pentry,
350
                         lowaddr, highaddr, elf_machine, clear_lsb);
351
    }
352

    
353
    close(fd);
354
    return ret;
355

    
356
 fail:
357
    close(fd);
358
    return -1;
359
}
360

    
361
static void bswap_uboot_header(an_uboot_image_header *hdr)
362
{
363
#ifndef HOST_WORDS_BIGENDIAN
364
    bswap32s(&hdr->ih_magic);
365
    bswap32s(&hdr->ih_hcrc);
366
    bswap32s(&hdr->ih_time);
367
    bswap32s(&hdr->ih_size);
368
    bswap32s(&hdr->ih_load);
369
    bswap32s(&hdr->ih_ep);
370
    bswap32s(&hdr->ih_dcrc);
371
#endif
372
}
373

    
374

    
375
#define ZALLOC_ALIGNMENT        16
376

    
377
static void *zalloc(void *x, unsigned items, unsigned size)
378
{
379
    void *p;
380

    
381
    size *= items;
382
    size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
383

    
384
    p = qemu_malloc(size);
385

    
386
    return (p);
387
}
388

    
389
static void zfree(void *x, void *addr)
390
{
391
    qemu_free(addr);
392
}
393

    
394

    
395
#define HEAD_CRC        2
396
#define EXTRA_FIELD        4
397
#define ORIG_NAME        8
398
#define COMMENT                0x10
399
#define RESERVED        0xe0
400

    
401
#define DEFLATED        8
402

    
403
/* This is the maximum in uboot, so if a uImage overflows this, it would
404
 * overflow on real hardware too. */
405
#define UBOOT_MAX_GUNZIP_BYTES 0x800000
406

    
407
static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src,
408
                      size_t srclen)
409
{
410
    z_stream s;
411
    ssize_t dstbytes;
412
    int r, i, flags;
413

    
414
    /* skip header */
415
    i = 10;
416
    flags = src[3];
417
    if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
418
        puts ("Error: Bad gzipped data\n");
419
        return -1;
420
    }
421
    if ((flags & EXTRA_FIELD) != 0)
422
        i = 12 + src[10] + (src[11] << 8);
423
    if ((flags & ORIG_NAME) != 0)
424
        while (src[i++] != 0)
425
            ;
426
    if ((flags & COMMENT) != 0)
427
        while (src[i++] != 0)
428
            ;
429
    if ((flags & HEAD_CRC) != 0)
430
        i += 2;
431
    if (i >= srclen) {
432
        puts ("Error: gunzip out of data in header\n");
433
        return -1;
434
    }
435

    
436
    s.zalloc = zalloc;
437
    s.zfree = zfree;
438

    
439
    r = inflateInit2(&s, -MAX_WBITS);
440
    if (r != Z_OK) {
441
        printf ("Error: inflateInit2() returned %d\n", r);
442
        return (-1);
443
    }
444
    s.next_in = src + i;
445
    s.avail_in = srclen - i;
446
    s.next_out = dst;
447
    s.avail_out = dstlen;
448
    r = inflate(&s, Z_FINISH);
449
    if (r != Z_OK && r != Z_STREAM_END) {
450
        printf ("Error: inflate() returned %d\n", r);
451
        return -1;
452
    }
453
    dstbytes = s.next_out - (unsigned char *) dst;
454
    inflateEnd(&s);
455

    
456
    return dstbytes;
457
}
458

    
459
/* Load a U-Boot image.  */
460
int load_uimage(const char *filename, a_target_phys_addr *ep,
461
                a_target_phys_addr *loadaddr, int *is_linux)
462
{
463
    int fd;
464
    int size;
465
    an_uboot_image_header h;
466
    an_uboot_image_header *hdr = &h;
467
    uint8_t *data = NULL;
468
    int ret = -1;
469

    
470
    fd = open(filename, O_RDONLY | O_BINARY);
471
    if (fd < 0)
472
        return -1;
473

    
474
    size = read(fd, hdr, sizeof(an_uboot_image_header));
475
    if (size < 0)
476
        goto out;
477

    
478
    bswap_uboot_header(hdr);
479

    
480
    if (hdr->ih_magic != IH_MAGIC)
481
        goto out;
482

    
483
    /* TODO: Implement other image types.  */
484
    if (hdr->ih_type != IH_TYPE_KERNEL) {
485
        fprintf(stderr, "Can only load u-boot image type \"kernel\"\n");
486
        goto out;
487
    }
488

    
489
    switch (hdr->ih_comp) {
490
    case IH_COMP_NONE:
491
    case IH_COMP_GZIP:
492
        break;
493
    default:
494
        fprintf(stderr,
495
                "Unable to load u-boot images with compression type %d\n",
496
                hdr->ih_comp);
497
        goto out;
498
    }
499

    
500
    /* TODO: Check CPU type.  */
501
    if (is_linux) {
502
        if (hdr->ih_os == IH_OS_LINUX)
503
            *is_linux = 1;
504
        else
505
            *is_linux = 0;
506
    }
507

    
508
    *ep = hdr->ih_ep;
509
    data = qemu_malloc(hdr->ih_size);
510

    
511
    if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
512
        fprintf(stderr, "Error reading file\n");
513
        goto out;
514
    }
515

    
516
    if (hdr->ih_comp == IH_COMP_GZIP) {
517
        uint8_t *compressed_data;
518
        size_t max_bytes;
519
        ssize_t bytes;
520

    
521
        compressed_data = data;
522
        max_bytes = UBOOT_MAX_GUNZIP_BYTES;
523
        data = qemu_malloc(max_bytes);
524

    
525
        bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size);
526
        qemu_free(compressed_data);
527
        if (bytes < 0) {
528
            fprintf(stderr, "Unable to decompress gzipped image!\n");
529
            goto out;
530
        }
531
        hdr->ih_size = bytes;
532
    }
533

    
534
    cpu_physical_memory_write_rom(hdr->ih_load, data, hdr->ih_size);
535

    
536
    if (loadaddr)
537
        *loadaddr = hdr->ih_load;
538

    
539
    ret = hdr->ih_size;
540

    
541
out:
542
    if (data)
543
        qemu_free(data);
544
    close(fd);
545
    return ret;
546
}