2
0

bootdevice.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 "sysemu/sysemu.h"
  25. #include "qapi/visitor.h"
  26. #include "qemu/error-report.h"
  27. typedef struct FWBootEntry FWBootEntry;
  28. struct FWBootEntry {
  29. QTAILQ_ENTRY(FWBootEntry) link;
  30. int32_t bootindex;
  31. DeviceState *dev;
  32. char *suffix;
  33. };
  34. static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
  35. QTAILQ_HEAD_INITIALIZER(fw_boot_order);
  36. void check_boot_index(int32_t bootindex, Error **errp)
  37. {
  38. FWBootEntry *i;
  39. if (bootindex >= 0) {
  40. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  41. if (i->bootindex == bootindex) {
  42. error_setg(errp, "The bootindex %d has already been used",
  43. bootindex);
  44. return;
  45. }
  46. }
  47. }
  48. }
  49. void del_boot_device_path(DeviceState *dev, const char *suffix)
  50. {
  51. FWBootEntry *i;
  52. if (dev == NULL) {
  53. return;
  54. }
  55. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  56. if ((!suffix || !g_strcmp0(i->suffix, suffix)) &&
  57. i->dev == dev) {
  58. QTAILQ_REMOVE(&fw_boot_order, i, link);
  59. g_free(i->suffix);
  60. g_free(i);
  61. break;
  62. }
  63. }
  64. }
  65. void add_boot_device_path(int32_t bootindex, DeviceState *dev,
  66. const char *suffix)
  67. {
  68. FWBootEntry *node, *i;
  69. if (bootindex < 0) {
  70. del_boot_device_path(dev, suffix);
  71. return;
  72. }
  73. assert(dev != NULL || suffix != NULL);
  74. del_boot_device_path(dev, suffix);
  75. node = g_malloc0(sizeof(FWBootEntry));
  76. node->bootindex = bootindex;
  77. node->suffix = g_strdup(suffix);
  78. node->dev = dev;
  79. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  80. if (i->bootindex == bootindex) {
  81. error_report("Two devices with same boot index %d", bootindex);
  82. exit(1);
  83. } else if (i->bootindex < bootindex) {
  84. continue;
  85. }
  86. QTAILQ_INSERT_BEFORE(i, node, link);
  87. return;
  88. }
  89. QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
  90. }
  91. DeviceState *get_boot_device(uint32_t position)
  92. {
  93. uint32_t counter = 0;
  94. FWBootEntry *i = NULL;
  95. DeviceState *res = NULL;
  96. if (!QTAILQ_EMPTY(&fw_boot_order)) {
  97. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  98. if (counter == position) {
  99. res = i->dev;
  100. break;
  101. }
  102. counter++;
  103. }
  104. }
  105. return res;
  106. }
  107. /*
  108. * This function returns null terminated string that consist of new line
  109. * separated device paths.
  110. *
  111. * memory pointed by "size" is assigned total length of the array in bytes
  112. *
  113. */
  114. char *get_boot_devices_list(size_t *size, bool ignore_suffixes)
  115. {
  116. FWBootEntry *i;
  117. size_t total = 0;
  118. char *list = NULL;
  119. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  120. char *devpath = NULL, *bootpath;
  121. size_t len;
  122. if (i->dev) {
  123. devpath = qdev_get_fw_dev_path(i->dev);
  124. assert(devpath);
  125. }
  126. if (i->suffix && !ignore_suffixes && devpath) {
  127. size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
  128. bootpath = g_malloc(bootpathlen);
  129. snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
  130. g_free(devpath);
  131. } else if (devpath) {
  132. bootpath = devpath;
  133. } else if (!ignore_suffixes) {
  134. assert(i->suffix);
  135. bootpath = g_strdup(i->suffix);
  136. } else {
  137. bootpath = g_strdup("");
  138. }
  139. if (total) {
  140. list[total-1] = '\n';
  141. }
  142. len = strlen(bootpath) + 1;
  143. list = g_realloc(list, total + len);
  144. memcpy(&list[total], bootpath, len);
  145. total += len;
  146. g_free(bootpath);
  147. }
  148. *size = total;
  149. if (boot_strict && *size > 0) {
  150. list[total-1] = '\n';
  151. list = g_realloc(list, total + 5);
  152. memcpy(&list[total], "HALT", 5);
  153. *size = total + 5;
  154. }
  155. return list;
  156. }
  157. typedef struct {
  158. int32_t *bootindex;
  159. const char *suffix;
  160. DeviceState *dev;
  161. } BootIndexProperty;
  162. static void device_get_bootindex(Object *obj, Visitor *v, void *opaque,
  163. const char *name, Error **errp)
  164. {
  165. BootIndexProperty *prop = opaque;
  166. visit_type_int32(v, prop->bootindex, name, errp);
  167. }
  168. static void device_set_bootindex(Object *obj, Visitor *v, void *opaque,
  169. const char *name, Error **errp)
  170. {
  171. BootIndexProperty *prop = opaque;
  172. int32_t boot_index;
  173. Error *local_err = NULL;
  174. visit_type_int32(v, &boot_index, name, &local_err);
  175. if (local_err) {
  176. goto out;
  177. }
  178. /* check whether bootindex is present in fw_boot_order list */
  179. check_boot_index(boot_index, &local_err);
  180. if (local_err) {
  181. goto out;
  182. }
  183. /* change bootindex to a new one */
  184. *prop->bootindex = boot_index;
  185. add_boot_device_path(*prop->bootindex, prop->dev, prop->suffix);
  186. out:
  187. if (local_err) {
  188. error_propagate(errp, local_err);
  189. }
  190. }
  191. static void property_release_bootindex(Object *obj, const char *name,
  192. void *opaque)
  193. {
  194. BootIndexProperty *prop = opaque;
  195. del_boot_device_path(prop->dev, prop->suffix);
  196. g_free(prop);
  197. }
  198. void device_add_bootindex_property(Object *obj, int32_t *bootindex,
  199. const char *name, const char *suffix,
  200. DeviceState *dev, Error **errp)
  201. {
  202. Error *local_err = NULL;
  203. BootIndexProperty *prop = g_malloc0(sizeof(*prop));
  204. prop->bootindex = bootindex;
  205. prop->suffix = suffix;
  206. prop->dev = dev;
  207. object_property_add(obj, name, "int32",
  208. device_get_bootindex,
  209. device_set_bootindex,
  210. property_release_bootindex,
  211. prop, &local_err);
  212. if (local_err) {
  213. error_propagate(errp, local_err);
  214. g_free(prop);
  215. return;
  216. }
  217. /* initialize devices' bootindex property to -1 */
  218. object_property_set_int(obj, -1, name, NULL);
  219. }