bootdevice.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. static char *get_boot_device_path(DeviceState *dev, bool ignore_suffixes,
  176. const char *suffix)
  177. {
  178. char *devpath = NULL, *s = NULL, *d, *bootpath;
  179. if (dev) {
  180. devpath = qdev_get_fw_dev_path(dev);
  181. assert(devpath);
  182. }
  183. if (!ignore_suffixes) {
  184. if (dev) {
  185. d = qdev_get_own_fw_dev_path_from_handler(dev->parent_bus, dev);
  186. if (d) {
  187. assert(!suffix);
  188. s = d;
  189. } else {
  190. s = g_strdup(suffix);
  191. }
  192. } else {
  193. s = g_strdup(suffix);
  194. }
  195. }
  196. bootpath = g_strdup_printf("%s%s",
  197. devpath ? devpath : "",
  198. s ? s : "");
  199. g_free(devpath);
  200. g_free(s);
  201. return bootpath;
  202. }
  203. /*
  204. * This function returns null terminated string that consist of new line
  205. * separated device paths.
  206. *
  207. * memory pointed by "size" is assigned total length of the array in bytes
  208. *
  209. */
  210. char *get_boot_devices_list(size_t *size)
  211. {
  212. FWBootEntry *i;
  213. size_t total = 0;
  214. char *list = NULL;
  215. MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
  216. bool ignore_suffixes = mc->ignore_boot_device_suffixes;
  217. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  218. char *bootpath;
  219. size_t len;
  220. bootpath = get_boot_device_path(i->dev, ignore_suffixes, i->suffix);
  221. if (total) {
  222. list[total-1] = '\n';
  223. }
  224. len = strlen(bootpath) + 1;
  225. list = g_realloc(list, total + len);
  226. memcpy(&list[total], bootpath, len);
  227. total += len;
  228. g_free(bootpath);
  229. }
  230. *size = total;
  231. if (boot_strict && *size > 0) {
  232. list[total-1] = '\n';
  233. list = g_realloc(list, total + 5);
  234. memcpy(&list[total], "HALT", 5);
  235. *size = total + 5;
  236. }
  237. return list;
  238. }
  239. typedef struct {
  240. int32_t *bootindex;
  241. const char *suffix;
  242. DeviceState *dev;
  243. } BootIndexProperty;
  244. static void device_get_bootindex(Object *obj, Visitor *v, const char *name,
  245. void *opaque, Error **errp)
  246. {
  247. BootIndexProperty *prop = opaque;
  248. visit_type_int32(v, name, prop->bootindex, errp);
  249. }
  250. static void device_set_bootindex(Object *obj, Visitor *v, const char *name,
  251. void *opaque, Error **errp)
  252. {
  253. BootIndexProperty *prop = opaque;
  254. int32_t boot_index;
  255. Error *local_err = NULL;
  256. if (!visit_type_int32(v, name, &boot_index, errp)) {
  257. return;
  258. }
  259. /* check whether bootindex is present in fw_boot_order list */
  260. check_boot_index(boot_index, &local_err);
  261. if (local_err) {
  262. error_propagate(errp, local_err);
  263. return;
  264. }
  265. /* change bootindex to a new one */
  266. *prop->bootindex = boot_index;
  267. add_boot_device_path(*prop->bootindex, prop->dev, prop->suffix);
  268. }
  269. static void property_release_bootindex(Object *obj, const char *name,
  270. void *opaque)
  271. {
  272. BootIndexProperty *prop = opaque;
  273. del_boot_device_path(prop->dev, prop->suffix);
  274. g_free(prop);
  275. }
  276. void device_add_bootindex_property(Object *obj, int32_t *bootindex,
  277. const char *name, const char *suffix,
  278. DeviceState *dev)
  279. {
  280. BootIndexProperty *prop = g_malloc0(sizeof(*prop));
  281. prop->bootindex = bootindex;
  282. prop->suffix = suffix;
  283. prop->dev = dev;
  284. object_property_add(obj, name, "int32",
  285. device_get_bootindex,
  286. device_set_bootindex,
  287. property_release_bootindex,
  288. prop);
  289. /* initialize devices' bootindex property to -1 */
  290. object_property_set_int(obj, name, -1, NULL);
  291. }
  292. typedef struct FWLCHSEntry FWLCHSEntry;
  293. struct FWLCHSEntry {
  294. QTAILQ_ENTRY(FWLCHSEntry) link;
  295. DeviceState *dev;
  296. char *suffix;
  297. uint32_t lcyls;
  298. uint32_t lheads;
  299. uint32_t lsecs;
  300. };
  301. static QTAILQ_HEAD(, FWLCHSEntry) fw_lchs =
  302. QTAILQ_HEAD_INITIALIZER(fw_lchs);
  303. void add_boot_device_lchs(DeviceState *dev, const char *suffix,
  304. uint32_t lcyls, uint32_t lheads, uint32_t lsecs)
  305. {
  306. FWLCHSEntry *node;
  307. if (!lcyls && !lheads && !lsecs) {
  308. return;
  309. }
  310. assert(dev != NULL || suffix != NULL);
  311. node = g_malloc0(sizeof(FWLCHSEntry));
  312. node->suffix = g_strdup(suffix);
  313. node->dev = dev;
  314. node->lcyls = lcyls;
  315. node->lheads = lheads;
  316. node->lsecs = lsecs;
  317. QTAILQ_INSERT_TAIL(&fw_lchs, node, link);
  318. }
  319. void del_boot_device_lchs(DeviceState *dev, const char *suffix)
  320. {
  321. FWLCHSEntry *i;
  322. if (dev == NULL) {
  323. return;
  324. }
  325. QTAILQ_FOREACH(i, &fw_lchs, link) {
  326. if ((!suffix || !g_strcmp0(i->suffix, suffix)) &&
  327. i->dev == dev) {
  328. QTAILQ_REMOVE(&fw_lchs, i, link);
  329. g_free(i->suffix);
  330. g_free(i);
  331. break;
  332. }
  333. }
  334. }
  335. char *get_boot_devices_lchs_list(size_t *size)
  336. {
  337. FWLCHSEntry *i;
  338. size_t total = 0;
  339. char *list = NULL;
  340. QTAILQ_FOREACH(i, &fw_lchs, link) {
  341. char *bootpath;
  342. char *chs_string;
  343. size_t len;
  344. bootpath = get_boot_device_path(i->dev, false, i->suffix);
  345. chs_string = g_strdup_printf("%s %" PRIu32 " %" PRIu32 " %" PRIu32,
  346. bootpath, i->lcyls, i->lheads, i->lsecs);
  347. if (total) {
  348. list[total - 1] = '\n';
  349. }
  350. len = strlen(chs_string) + 1;
  351. list = g_realloc(list, total + len);
  352. memcpy(&list[total], chs_string, len);
  353. total += len;
  354. g_free(chs_string);
  355. g_free(bootpath);
  356. }
  357. *size = total;
  358. return list;
  359. }