2
0

generic-loader.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 "hw/sysbus.h"
  33. #include "sysemu/dma.h"
  34. #include "sysemu/reset.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. }
  55. }
  56. static void generic_loader_realize(DeviceState *dev, Error **errp)
  57. {
  58. GenericLoaderState *s = GENERIC_LOADER(dev);
  59. hwaddr entry;
  60. int big_endian;
  61. int size = 0;
  62. s->set_pc = false;
  63. /* Perform some error checking on the user's options */
  64. if (s->data || s->data_len || s->data_be) {
  65. /* User is loading memory values */
  66. if (s->file) {
  67. error_setg(errp, "Specifying a file is not supported when loading "
  68. "memory values");
  69. return;
  70. } else if (s->force_raw) {
  71. error_setg(errp, "Specifying force-raw is not supported when "
  72. "loading memory values");
  73. return;
  74. } else if (!s->data_len) {
  75. /* We can't check for !data here as a value of 0 is still valid. */
  76. error_setg(errp, "Both data and data-len must be specified");
  77. return;
  78. } else if (s->data_len > 8) {
  79. error_setg(errp, "data-len cannot be greater then 8 bytes");
  80. return;
  81. }
  82. } else if (s->file || s->force_raw) {
  83. /* User is loading an image */
  84. if (s->data || s->data_len || s->data_be) {
  85. error_setg(errp, "data can not be specified when loading an "
  86. "image");
  87. return;
  88. }
  89. /* The user specified a file, only set the PC if they also specified
  90. * a CPU to use.
  91. */
  92. if (s->cpu_num != CPU_NONE) {
  93. s->set_pc = true;
  94. }
  95. } else if (s->addr) {
  96. /* User is setting the PC */
  97. if (s->data || s->data_len || s->data_be) {
  98. error_setg(errp, "data can not be specified when setting a "
  99. "program counter");
  100. return;
  101. } else if (s->cpu_num == CPU_NONE) {
  102. error_setg(errp, "cpu_num must be specified when setting a "
  103. "program counter");
  104. return;
  105. }
  106. s->set_pc = true;
  107. } else {
  108. /* Did the user specify anything? */
  109. error_setg(errp, "please include valid arguments");
  110. return;
  111. }
  112. qemu_register_reset(generic_loader_reset, dev);
  113. if (s->cpu_num != CPU_NONE) {
  114. s->cpu = qemu_get_cpu(s->cpu_num);
  115. if (!s->cpu) {
  116. error_setg(errp, "Specified boot CPU#%d is nonexistent",
  117. s->cpu_num);
  118. return;
  119. }
  120. } else {
  121. s->cpu = first_cpu;
  122. }
  123. big_endian = target_words_bigendian();
  124. if (s->file) {
  125. AddressSpace *as = s->cpu ? s->cpu->as : NULL;
  126. if (!s->force_raw) {
  127. size = load_elf_as(s->file, NULL, NULL, NULL, &entry, NULL, NULL,
  128. NULL, big_endian, 0, 0, 0, as);
  129. if (size < 0) {
  130. size = load_uimage_as(s->file, &entry, NULL, NULL, NULL, NULL,
  131. as);
  132. }
  133. if (size < 0) {
  134. size = load_targphys_hex_as(s->file, &entry, as);
  135. }
  136. }
  137. if (size < 0 || s->force_raw) {
  138. /* Default to the maximum size being the machine's ram size */
  139. size = load_image_targphys_as(s->file, s->addr, ram_size, as);
  140. } else {
  141. s->addr = entry;
  142. }
  143. if (size < 0) {
  144. error_setg(errp, "Cannot load specified image %s", s->file);
  145. return;
  146. }
  147. }
  148. /* Convert the data endiannes */
  149. if (s->data_be) {
  150. s->data = cpu_to_be64(s->data);
  151. } else {
  152. s->data = cpu_to_le64(s->data);
  153. }
  154. }
  155. static void generic_loader_unrealize(DeviceState *dev)
  156. {
  157. qemu_unregister_reset(generic_loader_reset, dev);
  158. }
  159. static Property generic_loader_props[] = {
  160. DEFINE_PROP_UINT64("addr", GenericLoaderState, addr, 0),
  161. DEFINE_PROP_UINT64("data", GenericLoaderState, data, 0),
  162. DEFINE_PROP_UINT8("data-len", GenericLoaderState, data_len, 0),
  163. DEFINE_PROP_BOOL("data-be", GenericLoaderState, data_be, false),
  164. DEFINE_PROP_UINT32("cpu-num", GenericLoaderState, cpu_num, CPU_NONE),
  165. DEFINE_PROP_BOOL("force-raw", GenericLoaderState, force_raw, false),
  166. DEFINE_PROP_STRING("file", GenericLoaderState, file),
  167. DEFINE_PROP_END_OF_LIST(),
  168. };
  169. static void generic_loader_class_init(ObjectClass *klass, void *data)
  170. {
  171. DeviceClass *dc = DEVICE_CLASS(klass);
  172. /* The reset function is not registered here and is instead registered in
  173. * the realize function to allow this device to be added via the device_add
  174. * command in the QEMU monitor.
  175. * TODO: Improve the device_add functionality to allow resets to be
  176. * connected
  177. */
  178. dc->realize = generic_loader_realize;
  179. dc->unrealize = generic_loader_unrealize;
  180. device_class_set_props(dc, generic_loader_props);
  181. dc->desc = "Generic Loader";
  182. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  183. }
  184. static TypeInfo generic_loader_info = {
  185. .name = TYPE_GENERIC_LOADER,
  186. .parent = TYPE_DEVICE,
  187. .instance_size = sizeof(GenericLoaderState),
  188. .class_init = generic_loader_class_init,
  189. };
  190. static void generic_loader_register_type(void)
  191. {
  192. type_register_static(&generic_loader_info);
  193. }
  194. type_init(generic_loader_register_type)