Annotation of qemu/loader.c, revision 1.1.1.7

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

unix.superglobalmegacorp.com