You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bootdevice.c 9.3KB

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