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