2
0

generic-loader.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. * Internally inside QEMU this is a device. It is a strange device that
  21. * provides no hardware interface but allows QEMU to monkey patch memory
  22. * specified when it is created. To be able to do this it has a reset
  23. * callback that does the memory operations.
  24. * This device allows the user to monkey patch memory. To be able to do
  25. * this it needs a backend to manage the datas, the same as other
  26. * memory-related devices. In this case as the backend is so trivial we
  27. * have merged it with the frontend instead of creating and maintaining a
  28. * separate backend.
  29. */
  30. #include "qemu/osdep.h"
  31. #include "hw/core/cpu.h"
  32. #include "sysemu/dma.h"
  33. #include "sysemu/reset.h"
  34. #include "hw/boards.h"
  35. #include "hw/loader.h"
  36. #include "hw/qdev-properties.h"
  37. #include "qapi/error.h"
  38. #include "qemu/module.h"
  39. #include "hw/core/generic-loader.h"
  40. #define CPU_NONE 0xFFFFFFFF
  41. static void generic_loader_reset(void *opaque)
  42. {
  43. GenericLoaderState *s = GENERIC_LOADER(opaque);
  44. if (s->set_pc) {
  45. CPUClass *cc = CPU_GET_CLASS(s->cpu);
  46. cpu_reset(s->cpu);
  47. if (cc) {
  48. cc->set_pc(s->cpu, s->addr);
  49. }
  50. }
  51. if (s->data_len) {
  52. assert(s->data_len <= sizeof(s->data));
  53. dma_memory_write(s->cpu->as, s->addr, &s->data, s->data_len,
  54. MEMTXATTRS_UNSPECIFIED);
  55. }
  56. }
  57. static void generic_loader_realize(DeviceState *dev, Error **errp)
  58. {
  59. GenericLoaderState *s = GENERIC_LOADER(dev);
  60. hwaddr entry;
  61. int big_endian;
  62. ssize_t size = 0;
  63. s->set_pc = false;
  64. /* Perform some error checking on the user's options */
  65. if (s->data || s->data_len || s->data_be) {
  66. /* User is loading memory values */
  67. if (s->file) {
  68. error_setg(errp, "Specifying a file is not supported when loading "
  69. "memory values");
  70. return;
  71. } else if (s->force_raw) {
  72. error_setg(errp, "Specifying force-raw is not supported when "
  73. "loading memory values");
  74. return;
  75. } else if (!s->data_len) {
  76. /* We can't check for !data here as a value of 0 is still valid. */
  77. error_setg(errp, "Both data and data-len must be specified");
  78. return;
  79. } else if (s->data_len > 8) {
  80. error_setg(errp, "data-len cannot be greater then 8 bytes");
  81. return;
  82. }
  83. } else if (s->file || s->force_raw) {
  84. /* User is loading an image */
  85. if (s->data || s->data_len || s->data_be) {
  86. error_setg(errp, "data can not be specified when loading an "
  87. "image");
  88. return;
  89. }
  90. /* The user specified a file, only set the PC if they also specified
  91. * a CPU to use.
  92. */
  93. if (s->cpu_num != CPU_NONE) {
  94. s->set_pc = true;
  95. }
  96. } else if (s->addr) {
  97. /* User is setting the PC */
  98. if (s->data || s->data_len || s->data_be) {
  99. error_setg(errp, "data can not be specified when setting a "
  100. "program counter");
  101. return;
  102. } else if (s->cpu_num == CPU_NONE) {
  103. error_setg(errp, "cpu_num must be specified when setting a "
  104. "program counter");
  105. return;
  106. }
  107. s->set_pc = true;
  108. } else {
  109. /* Did the user specify anything? */
  110. error_setg(errp, "please include valid arguments");
  111. return;
  112. }
  113. qemu_register_reset(generic_loader_reset, dev);
  114. if (s->cpu_num != CPU_NONE) {
  115. s->cpu = qemu_get_cpu(s->cpu_num);
  116. if (!s->cpu) {
  117. error_setg(errp, "Specified boot CPU#%d is nonexistent",
  118. s->cpu_num);
  119. return;
  120. }
  121. } else {
  122. s->cpu = first_cpu;
  123. }
  124. big_endian = target_words_bigendian();
  125. if (s->file) {
  126. AddressSpace *as = s->cpu ? s->cpu->as : NULL;
  127. if (!s->force_raw) {
  128. size = load_elf_as(s->file, NULL, NULL, NULL, &entry, NULL, NULL,
  129. NULL, big_endian, 0, 0, 0, as);
  130. if (size < 0) {
  131. size = load_uimage_as(s->file, &entry, NULL, NULL, NULL, NULL,
  132. as);
  133. }
  134. if (size < 0) {
  135. size = load_targphys_hex_as(s->file, &entry, as);
  136. }
  137. }
  138. if (size < 0 || s->force_raw) {
  139. /* Default to the maximum size being the machine's ram size */
  140. size = load_image_targphys_as(s->file, s->addr, current_machine->ram_size, as);
  141. } else {
  142. s->addr = entry;
  143. }
  144. if (size < 0) {
  145. error_setg(errp, "Cannot load specified image %s", s->file);
  146. return;
  147. }
  148. }
  149. /* Convert the data endiannes */
  150. if (s->data_be) {
  151. s->data = cpu_to_be64(s->data);
  152. } else {
  153. s->data = cpu_to_le64(s->data);
  154. }
  155. }
  156. static void generic_loader_unrealize(DeviceState *dev)
  157. {
  158. qemu_unregister_reset(generic_loader_reset, dev);
  159. }
  160. static Property generic_loader_props[] = {
  161. DEFINE_PROP_UINT64("addr", GenericLoaderState, addr, 0),
  162. DEFINE_PROP_UINT64("data", GenericLoaderState, data, 0),
  163. DEFINE_PROP_UINT8("data-len", GenericLoaderState, data_len, 0),
  164. DEFINE_PROP_BOOL("data-be", GenericLoaderState, data_be, false),
  165. DEFINE_PROP_UINT32("cpu-num", GenericLoaderState, cpu_num, CPU_NONE),
  166. DEFINE_PROP_BOOL("force-raw", GenericLoaderState, force_raw, false),
  167. DEFINE_PROP_STRING("file", GenericLoaderState, file),
  168. DEFINE_PROP_END_OF_LIST(),
  169. };
  170. static void generic_loader_class_init(ObjectClass *klass, void *data)
  171. {
  172. DeviceClass *dc = DEVICE_CLASS(klass);
  173. /* The reset function is not registered here and is instead registered in
  174. * the realize function to allow this device to be added via the device_add
  175. * command in the QEMU monitor.
  176. * TODO: Improve the device_add functionality to allow resets to be
  177. * connected
  178. */
  179. dc->realize = generic_loader_realize;
  180. dc->unrealize = generic_loader_unrealize;
  181. device_class_set_props(dc, generic_loader_props);
  182. dc->desc = "Generic Loader";
  183. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  184. }
  185. static const TypeInfo generic_loader_info = {
  186. .name = TYPE_GENERIC_LOADER,
  187. .parent = TYPE_DEVICE,
  188. .instance_size = sizeof(GenericLoaderState),
  189. .class_init = generic_loader_class_init,
  190. };
  191. static void generic_loader_register_type(void)
  192. {
  193. type_register_static(&generic_loader_info);
  194. }
  195. type_init(generic_loader_register_type)