2
0

bootdevice.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * QEMU Boot Device Implement
  3. *
  4. * Copyright (c) 2014 HUAWEI TECHNOLOGIES CO., LTD.
  5. *
  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.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qapi/error.h"
  26. #include "sysemu/sysemu.h"
  27. #include "qapi/visitor.h"
  28. #include "qemu/error-report.h"
  29. #include "hw/hw.h"
  30. #include "hw/qdev-core.h"
  31. typedef struct FWBootEntry FWBootEntry;
  32. struct FWBootEntry {
  33. QTAILQ_ENTRY(FWBootEntry) link;
  34. int32_t bootindex;
  35. DeviceState *dev;
  36. char *suffix;
  37. };
  38. static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
  39. QTAILQ_HEAD_INITIALIZER(fw_boot_order);
  40. static QEMUBootSetHandler *boot_set_handler;
  41. static void *boot_set_opaque;
  42. void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
  43. {
  44. boot_set_handler = func;
  45. boot_set_opaque = opaque;
  46. }
  47. void qemu_boot_set(const char *boot_order, Error **errp)
  48. {
  49. Error *local_err = NULL;
  50. if (!boot_set_handler) {
  51. error_setg(errp, "no function defined to set boot device list for"
  52. " this architecture");
  53. return;
  54. }
  55. validate_bootdevices(boot_order, &local_err);
  56. if (local_err) {
  57. error_propagate(errp, local_err);
  58. return;
  59. }
  60. boot_set_handler(boot_set_opaque, boot_order, errp);
  61. }
  62. void validate_bootdevices(const char *devices, Error **errp)
  63. {
  64. /* We just do some generic consistency checks */
  65. const char *p;
  66. int bitmap = 0;
  67. for (p = devices; *p != '\0'; p++) {
  68. /* Allowed boot devices are:
  69. * a-b: floppy disk drives
  70. * c-f: IDE disk drives
  71. * g-m: machine implementation dependent drives
  72. * n-p: network devices
  73. * It's up to each machine implementation to check if the given boot
  74. * devices match the actual hardware implementation and firmware
  75. * features.
  76. */
  77. if (*p < 'a' || *p > 'p') {
  78. error_setg(errp, "Invalid boot device '%c'", *p);
  79. return;
  80. }
  81. if (bitmap & (1 << (*p - 'a'))) {
  82. error_setg(errp, "Boot device '%c' was given twice", *p);
  83. return;
  84. }
  85. bitmap |= 1 << (*p - 'a');
  86. }
  87. }
  88. void restore_boot_order(void *opaque)
  89. {
  90. char *normal_boot_order = opaque;
  91. static int first = 1;
  92. /* Restore boot order and remove ourselves after the first boot */
  93. if (first) {
  94. first = 0;
  95. return;
  96. }
  97. if (boot_set_handler) {
  98. qemu_boot_set(normal_boot_order, &error_abort);
  99. }
  100. qemu_unregister_reset(restore_boot_order, normal_boot_order);
  101. g_free(normal_boot_order);
  102. }
  103. void check_boot_index(int32_t bootindex, Error **errp)
  104. {
  105. FWBootEntry *i;
  106. if (bootindex >= 0) {
  107. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  108. if (i->bootindex == bootindex) {
  109. error_setg(errp, "The bootindex %d has already been used",
  110. bootindex);
  111. return;
  112. }
  113. }
  114. }
  115. }
  116. void del_boot_device_path(DeviceState *dev, const char *suffix)
  117. {
  118. FWBootEntry *i;
  119. if (dev == NULL) {
  120. return;
  121. }
  122. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  123. if ((!suffix || !g_strcmp0(i->suffix, suffix)) &&
  124. i->dev == dev) {
  125. QTAILQ_REMOVE(&fw_boot_order, i, link);
  126. g_free(i->suffix);
  127. g_free(i);
  128. break;
  129. }
  130. }
  131. }
  132. void add_boot_device_path(int32_t bootindex, DeviceState *dev,
  133. const char *suffix)
  134. {
  135. FWBootEntry *node, *i;
  136. if (bootindex < 0) {
  137. del_boot_device_path(dev, suffix);
  138. return;
  139. }
  140. assert(dev != NULL || suffix != NULL);
  141. del_boot_device_path(dev, suffix);
  142. node = g_malloc0(sizeof(FWBootEntry));
  143. node->bootindex = bootindex;
  144. node->suffix = g_strdup(suffix);
  145. node->dev = dev;
  146. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  147. if (i->bootindex == bootindex) {
  148. error_report("Two devices with same boot index %d", bootindex);
  149. exit(1);
  150. } else if (i->bootindex < bootindex) {
  151. continue;
  152. }
  153. QTAILQ_INSERT_BEFORE(i, node, link);
  154. return;
  155. }
  156. QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
  157. }
  158. DeviceState *get_boot_device(uint32_t position)
  159. {
  160. uint32_t counter = 0;
  161. FWBootEntry *i = NULL;
  162. DeviceState *res = NULL;
  163. if (!QTAILQ_EMPTY(&fw_boot_order)) {
  164. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  165. if (counter == position) {
  166. res = i->dev;
  167. break;
  168. }
  169. counter++;
  170. }
  171. }
  172. return res;
  173. }
  174. /*
  175. * This function returns null terminated string that consist of new line
  176. * separated device paths.
  177. *
  178. * memory pointed by "size" is assigned total length of the array in bytes
  179. *
  180. */
  181. char *get_boot_devices_list(size_t *size, bool ignore_suffixes)
  182. {
  183. FWBootEntry *i;
  184. size_t total = 0;
  185. char *list = NULL;
  186. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  187. char *devpath = NULL, *suffix = NULL;
  188. char *bootpath;
  189. char *d;
  190. size_t len;
  191. if (i->dev) {
  192. devpath = qdev_get_fw_dev_path(i->dev);
  193. assert(devpath);
  194. }
  195. if (!ignore_suffixes) {
  196. if (i->dev) {
  197. d = qdev_get_own_fw_dev_path_from_handler(i->dev->parent_bus,
  198. i->dev);
  199. if (d) {
  200. assert(!i->suffix);
  201. suffix = d;
  202. } else {
  203. suffix = g_strdup(i->suffix);
  204. }
  205. } else {
  206. suffix = g_strdup(i->suffix);
  207. }
  208. }
  209. bootpath = g_strdup_printf("%s%s",
  210. devpath ? devpath : "",
  211. suffix ? suffix : "");
  212. g_free(devpath);
  213. g_free(suffix);
  214. if (total) {
  215. list[total-1] = '\n';
  216. }
  217. len = strlen(bootpath) + 1;
  218. list = g_realloc(list, total + len);
  219. memcpy(&list[total], bootpath, len);
  220. total += len;
  221. g_free(bootpath);
  222. }
  223. *size = total;
  224. if (boot_strict && *size > 0) {
  225. list[total-1] = '\n';
  226. list = g_realloc(list, total + 5);
  227. memcpy(&list[total], "HALT", 5);
  228. *size = total + 5;
  229. }
  230. return list;
  231. }
  232. typedef struct {
  233. int32_t *bootindex;
  234. const char *suffix;
  235. DeviceState *dev;
  236. } BootIndexProperty;
  237. static void device_get_bootindex(Object *obj, Visitor *v, const char *name,
  238. void *opaque, Error **errp)
  239. {
  240. BootIndexProperty *prop = opaque;
  241. visit_type_int32(v, name, prop->bootindex, errp);
  242. }
  243. static void device_set_bootindex(Object *obj, Visitor *v, const char *name,
  244. void *opaque, Error **errp)
  245. {
  246. BootIndexProperty *prop = opaque;
  247. int32_t boot_index;
  248. Error *local_err = NULL;
  249. visit_type_int32(v, name, &boot_index, &local_err);
  250. if (local_err) {
  251. goto out;
  252. }
  253. /* check whether bootindex is present in fw_boot_order list */
  254. check_boot_index(boot_index, &local_err);
  255. if (local_err) {
  256. goto out;
  257. }
  258. /* change bootindex to a new one */
  259. *prop->bootindex = boot_index;
  260. add_boot_device_path(*prop->bootindex, prop->dev, prop->suffix);
  261. out:
  262. error_propagate(errp, local_err);
  263. }
  264. static void property_release_bootindex(Object *obj, const char *name,
  265. void *opaque)
  266. {
  267. BootIndexProperty *prop = opaque;
  268. del_boot_device_path(prop->dev, prop->suffix);
  269. g_free(prop);
  270. }
  271. void device_add_bootindex_property(Object *obj, int32_t *bootindex,
  272. const char *name, const char *suffix,
  273. DeviceState *dev, Error **errp)
  274. {
  275. Error *local_err = NULL;
  276. BootIndexProperty *prop = g_malloc0(sizeof(*prop));
  277. prop->bootindex = bootindex;
  278. prop->suffix = suffix;
  279. prop->dev = dev;
  280. object_property_add(obj, name, "int32",
  281. device_get_bootindex,
  282. device_set_bootindex,
  283. property_release_bootindex,
  284. prop, &local_err);
  285. if (local_err) {
  286. error_propagate(errp, local_err);
  287. g_free(prop);
  288. return;
  289. }
  290. /* initialize devices' bootindex property to -1 */
  291. object_property_set_int(obj, -1, name, NULL);
  292. }