| 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 data, 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 | * separate backend. |
| 31 | */ |
| 32 | |
| 33 | /* |
| 34 | * TODO: currently the "load a file" functionality provides no way |
| 35 | * for the user to specify which CPU address space to load the data |
| 36 | * into without also causing that CPU's PC to be set to the start |
| 37 | * address of the file. |
| 38 | * |
| 39 | * We could fix this by having a new suboption set-pc (default: true) |
| 40 | * so the user can say |
| 41 | * -device loader,file=<file>,cpu-num=<cpu-num> |
| 42 | * for the current "use this address space and set the PC" behaviour |
| 43 | * or |
| 44 | * -device loader,file=<file>,cpu-num=<cpu-num>,set-pc=off |
| 45 | * to just pick the address space and not set the PC. |
| 46 | * |
| 47 | * Using set-pc without file= should be handled as an error; otherwise |
| 48 | * it can feed through to what we set s->set_pc to. |
| 49 | */ |
| 50 | |
| 51 | #include "qemu/osdep.h" |
| 52 | #include "system/dma.h" |
| 53 | #include "system/reset.h" |
| 54 | #include "hw/core/boards.h" |
| 55 | #include "hw/core/loader.h" |
| 56 | #include "hw/core/qdev-properties.h" |
| 57 | #include "qapi/error.h" |
| 58 | #include "qemu/module.h" |
| 59 | #include "hw/core/generic-loader.h" |
| 60 | |
| 61 | #define CPU_NONE 0xFFFFFFFF |
| 62 | |
| 63 | static void generic_loader_reset(void *opaque) |
| 64 | { |
| 65 | GenericLoaderState *s = GENERIC_LOADER(opaque); |
| 66 | |
| 67 | if (s->set_pc) { |
| 68 | cpu_reset(s->cpu); |
| 69 | cpu_set_pc(s->cpu, s->addr); |
| 70 | } |
| 71 | |
| 72 | if (s->data_len) { |
| 73 | assert(s->data_len <= sizeof(s->data)); |
| 74 | dma_memory_write(s->cpu->as, s->addr, &s->data, s->data_len, |
| 75 | MEMTXATTRS_UNSPECIFIED); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static void generic_loader_realize(DeviceState *dev, Error **errp) |
| 80 | { |
| 81 | GenericLoaderState *s = GENERIC_LOADER(dev); |
| 82 | hwaddr entry; |
| 83 | ssize_t size = 0; |
| 84 | |
| 85 | s->set_pc = false; |
| 86 | |
| 87 | /* Perform some error checking on the user's options */ |
| 88 | if (s->data || s->data_len || s->data_be) { |
| 89 | /* User is loading memory values */ |
| 90 | if (s->file) { |
| 91 | error_setg(errp, "Specifying a file is not supported when loading " |
| 92 | "memory values"); |
| 93 | return; |
| 94 | } else if (s->force_raw) { |
| 95 | error_setg(errp, "Specifying force-raw is not supported when " |
| 96 | "loading memory values"); |
| 97 | return; |
| 98 | } else if (!s->data_len) { |
| 99 | /* We can't check for !data here as a value of 0 is still valid. */ |
| 100 | error_setg(errp, "Both data and data-len must be specified"); |
| 101 | return; |
| 102 | } else if (s->data_len > 8) { |
| 103 | error_setg(errp, "data-len cannot be greater then 8 bytes"); |
| 104 | return; |
| 105 | } |
| 106 | } else if (s->file || s->force_raw) { |
| 107 | /* User is loading an image */ |
| 108 | if (s->data || s->data_len || s->data_be) { |
| 109 | error_setg(errp, "data can not be specified when loading an " |
| 110 | "image"); |
| 111 | return; |
| 112 | } |
| 113 | /* The user specified a file, only set the PC if they also specified |
| 114 | * a CPU to use. |
| 115 | */ |
| 116 | if (s->cpu_num != CPU_NONE) { |
| 117 | s->set_pc = true; |
| 118 | } |
| 119 | } else if (s->addr) { |
| 120 | /* User is setting the PC */ |
| 121 | if (s->data || s->data_len || s->data_be) { |
| 122 | error_setg(errp, "data can not be specified when setting a " |
| 123 | "program counter"); |
| 124 | return; |
| 125 | } else if (s->cpu_num == CPU_NONE) { |
| 126 | error_setg(errp, "cpu_num must be specified when setting a " |
| 127 | "program counter"); |
| 128 | return; |
| 129 | } |
| 130 | s->set_pc = true; |
| 131 | } else { |
| 132 | /* Did the user specify anything? */ |
| 133 | error_setg(errp, "please include valid arguments"); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | qemu_register_reset(generic_loader_reset, dev); |
| 138 | |
| 139 | if (s->cpu_num != CPU_NONE) { |
| 140 | s->cpu = qemu_get_cpu(s->cpu_num); |
| 141 | if (!s->cpu) { |
| 142 | error_setg(errp, "Specified boot CPU#%d is nonexistent", |
| 143 | s->cpu_num); |
| 144 | return; |
| 145 | } |
| 146 | } else { |
| 147 | s->cpu = first_cpu; |
| 148 | } |
| 149 | |
| 150 | if (s->file) { |
| 151 | AddressSpace *as = s->cpu ? s->cpu->as : NULL; |
| 152 | |
| 153 | if (!s->force_raw) { |
| 154 | size = load_elf_as(s->file, NULL, NULL, NULL, &entry, NULL, NULL, |
| 155 | NULL, ELFDATANONE, 0, 0, 0, as); |
| 156 | |
| 157 | if (size < 0) { |
| 158 | size = load_uimage_as(s->file, &entry, NULL, NULL, NULL, NULL, |
| 159 | as); |
| 160 | } |
| 161 | |
| 162 | if (size < 0) { |
| 163 | size = load_targphys_hex_as(s->file, &entry, as); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (size < 0 || s->force_raw) { |
| 168 | /* Default to the maximum size being the machine's ram size */ |
| 169 | size = load_image_targphys_as(s->file, s->addr, |
| 170 | current_machine->ram_size, as, errp); |
| 171 | } else { |
| 172 | s->addr = entry; |
| 173 | } |
| 174 | |
| 175 | if (size < 0) { |
| 176 | error_prepend(errp, "Cannot load specified image %s: ", s->file); |
| 177 | return; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /* Convert the data endianness */ |
| 182 | if (s->data_be) { |
| 183 | s->data = cpu_to_be64(s->data); |
| 184 | } else { |
| 185 | s->data = cpu_to_le64(s->data); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | static void generic_loader_unrealize(DeviceState *dev) |
| 190 | { |
| 191 | qemu_unregister_reset(generic_loader_reset, dev); |
| 192 | } |
| 193 | |
| 194 | static const Property generic_loader_props[] = { |
| 195 | DEFINE_PROP_UINT64("addr", GenericLoaderState, addr, 0), |
| 196 | DEFINE_PROP_UINT64("data", GenericLoaderState, data, 0), |
| 197 | DEFINE_PROP_UINT8("data-len", GenericLoaderState, data_len, 0), |
| 198 | DEFINE_PROP_BOOL("data-be", GenericLoaderState, data_be, false), |
| 199 | DEFINE_PROP_UINT32("cpu-num", GenericLoaderState, cpu_num, CPU_NONE), |
| 200 | DEFINE_PROP_BOOL("force-raw", GenericLoaderState, force_raw, false), |
| 201 | DEFINE_PROP_STRING("file", GenericLoaderState, file), |
| 202 | }; |
| 203 | |
| 204 | static void generic_loader_class_init(ObjectClass *klass, const void *data) |
| 205 | { |
| 206 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 207 | |
| 208 | /* The reset function is not registered here and is instead registered in |
| 209 | * the realize function to allow this device to be added via the device_add |
| 210 | * command in the QEMU monitor. |
| 211 | * TODO: Improve the device_add functionality to allow resets to be |
| 212 | * connected |
| 213 | */ |
| 214 | dc->realize = generic_loader_realize; |
| 215 | dc->unrealize = generic_loader_unrealize; |
| 216 | device_class_set_props(dc, generic_loader_props); |
| 217 | dc->desc = "Generic Loader"; |
| 218 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
| 219 | } |
| 220 | |
| 221 | static const TypeInfo generic_loader_info = { |
| 222 | .name = TYPE_GENERIC_LOADER, |
| 223 | .parent = TYPE_DEVICE, |
| 224 | .instance_size = sizeof(GenericLoaderState), |
| 225 | .class_init = generic_loader_class_init, |
| 226 | }; |
| 227 | |
| 228 | static void generic_loader_register_type(void) |
| 229 | { |
| 230 | type_register_static(&generic_loader_info); |
| 231 | } |
| 232 | |
| 233 | type_init(generic_loader_register_type) |