qemu/hw/core/generic-loader.c
<<
>>
Prefs
   1/*
   2 * Generic Loader
   3 *
   4 * Copyright (C) 2014 Li Guang
   5 * Copyright (C) 2016 Xilinx Inc.
   6 * Written by Li Guang <lig.fnst@cn.fujitsu.com>
   7 *
   8 * This program is free software; you can redistribute it and/or modify it
   9 * under the terms of the GNU General Public License as published by the
  10 * Free Software Foundation; either version 2 of the License, or
  11 * (at your option) any later version.
  12 *
  13 * This program is distributed in the hope that it will be useful, but WITHOUT
  14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16 * for more details.
  17 *
  18 */
  19
  20/*
  21 * Internally inside QEMU this is a device. It is a strange device that
  22 * provides no hardware interface but allows QEMU to monkey patch memory
  23 * specified when it is created. To be able to do this it has a reset
  24 * callback that does the memory operations.
  25
  26 * This device allows the user to monkey patch memory. To be able to do
  27 * this it needs a backend to manage the datas, the same as other
  28 * memory-related devices. In this case as the backend is so trivial we
  29 * have merged it with the frontend instead of creating and maintaining a
  30 * seperate backend.
  31 */
  32
  33#include "qemu/osdep.h"
  34#include "cpu.h"
  35#include "qom/cpu.h"
  36#include "hw/sysbus.h"
  37#include "sysemu/dma.h"
  38#include "hw/loader.h"
  39#include "qapi/error.h"
  40#include "hw/core/generic-loader.h"
  41
  42#define CPU_NONE 0xFFFFFFFF
  43
  44static void generic_loader_reset(void *opaque)
  45{
  46    GenericLoaderState *s = GENERIC_LOADER(opaque);
  47
  48    if (s->set_pc) {
  49        CPUClass *cc = CPU_GET_CLASS(s->cpu);
  50        cpu_reset(s->cpu);
  51        if (cc) {
  52            cc->set_pc(s->cpu, s->addr);
  53        }
  54    }
  55
  56    if (s->data_len) {
  57        assert(s->data_len < sizeof(s->data));
  58        dma_memory_write(s->cpu->as, s->addr, &s->data, s->data_len);
  59    }
  60}
  61
  62static void generic_loader_realize(DeviceState *dev, Error **errp)
  63{
  64    GenericLoaderState *s = GENERIC_LOADER(dev);
  65    hwaddr entry;
  66    int big_endian;
  67    int size = 0;
  68
  69    s->set_pc = false;
  70
  71    /* Xilinx: This is the old deprecated command line argument.
  72     * This will be removed in the future, print a warning to tell
  73     * users.
  74     */
  75    if (s->cpu_num_dep != CPU_NONE) {
  76        fprintf(stderr, "The 'cpu=' command line option has been deprecated " \
  77            "and replaced with 'cpu-num='. Please update your command line " \
  78            "to use 'cpu-num=' as 'cpu=' will be removed after 2017.2.\n");
  79        if (s->cpu_num == CPU_NONE) {
  80            s->cpu_num = s->cpu_num_dep;
  81        }
  82    }
  83
  84    /* Perform some error checking on the user's options */
  85    if (s->data || s->data_len  || s->data_be) {
  86        /* User is loading memory values */
  87        if (s->file) {
  88            error_setg(errp, "Specifying a file is not supported when loading "
  89                       "memory values");
  90            return;
  91        } else if (s->force_raw) {
  92            error_setg(errp, "Specifying force-raw is not supported when "
  93                       "loading memory values");
  94            return;
  95        } else if (!s->data_len) {
  96            /* We cant' check for !data here as a value of 0 is still valid. */
  97            error_setg(errp, "Both data and data-len must be specified");
  98            return;
  99        } else if (s->data_len > 8) {
 100            error_setg(errp, "data-len cannot be greater then 8 bytes");
 101            return;
 102        }
 103    } else if (s->file || s->force_raw)  {
 104        /* User is loading an image */
 105        if (s->data || s->data_len || s->data_be) {
 106            error_setg(errp, "data can not be specified when loading an "
 107                       "image");
 108            return;
 109        }
 110        /* The user specified a file, only set the PC if they also specified
 111         * a CPU to use.
 112         */
 113        if (s->cpu_num != CPU_NONE) {
 114            s->set_pc = true;
 115        }
 116    } else if (s->addr) {
 117        /* User is setting the PC */
 118        if (s->data || s->data_len || s->data_be) {
 119            error_setg(errp, "data can not be specified when setting a "
 120                       "program counter");
 121            return;
 122        } else if (!s->cpu_num) {
 123            error_setg(errp, "cpu_num must be specified when setting a "
 124                       "program counter");
 125            return;
 126        }
 127        s->set_pc = true;
 128    } else {
 129        /* Did the user specify anything? */
 130        error_setg(errp, "please include valid arguments");
 131        return;
 132    }
 133
 134    qemu_register_reset(generic_loader_reset, dev);
 135
 136    if (s->cpu_num != CPU_NONE) {
 137        s->cpu = qemu_get_cpu(s->cpu_num);
 138        if (!s->cpu) {
 139            error_setg(errp, "Specified boot CPU#%d is nonexistent",
 140                       s->cpu_num);
 141            return;
 142        }
 143    } else {
 144        s->cpu = first_cpu;
 145    }
 146
 147#ifdef TARGET_WORDS_BIGENDIAN
 148    big_endian = 1;
 149#else
 150    big_endian = 0;
 151#endif
 152
 153    if (s->file) {
 154        if (!s->force_raw) {
 155            size = load_elf_as(s->file, NULL, NULL, &entry, NULL, NULL,
 156                               big_endian, 0, 0, 0, s->cpu->as);
 157
 158            if (size < 0) {
 159                size = load_uimage_as(s->file, &entry, NULL, NULL, NULL, NULL,
 160                                      s->cpu->as);
 161            }
 162        }
 163
 164        if (size < 0 || s->force_raw) {
 165            /* Default to the maximum size being the machine's ram size */
 166            size = load_image_targphys_as(s->file, s->addr, ram_size,
 167                                          s->cpu->as);
 168        } else {
 169            s->addr = entry;
 170        }
 171
 172        if (size < 0) {
 173            error_setg(errp, "Cannot load specified image %s", s->file);
 174            return;
 175        }
 176    }
 177
 178    /* Convert the data endiannes */
 179    if (s->data_be) {
 180        s->data = cpu_to_be64(s->data);
 181    } else {
 182        s->data = cpu_to_le64(s->data);
 183    }
 184}
 185
 186static void generic_loader_unrealize(DeviceState *dev, Error **errp)
 187{
 188    qemu_unregister_reset(generic_loader_reset, dev);
 189}
 190
 191static Property generic_loader_props[] = {
 192    DEFINE_PROP_UINT64("addr", GenericLoaderState, addr, 0),
 193    DEFINE_PROP_UINT64("data", GenericLoaderState, data, 0),
 194    DEFINE_PROP_UINT8("data-len", GenericLoaderState, data_len, 0),
 195    DEFINE_PROP_BOOL("data-be", GenericLoaderState, data_be, false),
 196    /* Xilinx: This is the old deprecated command line argument. It will be
 197     * removed in the future.
 198     */
 199    DEFINE_PROP_UINT32("cpu", GenericLoaderState, cpu_num_dep, CPU_NONE),
 200    DEFINE_PROP_UINT32("cpu-num", GenericLoaderState, cpu_num, CPU_NONE),
 201    DEFINE_PROP_BOOL("force-raw", GenericLoaderState, force_raw, false),
 202    DEFINE_PROP_STRING("file", GenericLoaderState, file),
 203    DEFINE_PROP_END_OF_LIST(),
 204};
 205
 206static void generic_loader_class_init(ObjectClass *klass, void *data)
 207{
 208    DeviceClass *dc = DEVICE_CLASS(klass);
 209
 210    /* The reset function is not registered here and is instead registered in
 211     * the realize function to allow this device to be added via the device_add
 212     * command in the QEMU monitor.
 213     * TODO: Improve the device_add functionality to allow resets to be
 214     * connected
 215     */
 216    dc->realize = generic_loader_realize;
 217    dc->unrealize = generic_loader_unrealize;
 218    dc->props = generic_loader_props;
 219    dc->desc = "Generic Loader";
 220}
 221
 222static TypeInfo generic_loader_info = {
 223    .name = TYPE_GENERIC_LOADER,
 224    .parent = TYPE_DEVICE,
 225    .instance_size = sizeof(GenericLoaderState),
 226    .class_init = generic_loader_class_init,
 227};
 228
 229static void generic_loader_register_type(void)
 230{
 231    type_register_static(&generic_loader_info);
 232}
 233
 234type_init(generic_loader_register_type)
 235