bootdevice.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 "system/system.h"
  27. #include "qapi/visitor.h"
  28. #include "qemu/error-report.h"
  29. #include "system/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 bootcount;
  93. switch (bootcount++) {
  94. case 0:
  95. /* First boot: use the one-time config */
  96. return;
  97. case 1:
  98. /* Second boot: restore normal boot order */
  99. if (boot_set_handler) {
  100. qemu_boot_set(normal_boot_order, &error_abort);
  101. }
  102. g_free(normal_boot_order);
  103. return;
  104. default:
  105. /* Subsequent boots: keep using normal boot order */
  106. return;
  107. }
  108. }
  109. void check_boot_index(int32_t bootindex, Error **errp)
  110. {
  111. FWBootEntry *i;
  112. if (bootindex >= 0) {
  113. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  114. if (i->bootindex == bootindex) {
  115. error_setg(errp, "The bootindex %d has already been used",
  116. bootindex);
  117. return;
  118. }
  119. }
  120. }
  121. }
  122. void del_boot_device_path(DeviceState *dev, const char *suffix)
  123. {
  124. FWBootEntry *i;
  125. if (dev == NULL) {
  126. return;
  127. }
  128. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  129. if ((!suffix || !g_strcmp0(i->suffix, suffix)) &&
  130. i->dev == dev) {
  131. QTAILQ_REMOVE(&fw_boot_order, i, link);
  132. g_free(i->suffix);
  133. g_free(i);
  134. break;
  135. }
  136. }
  137. }
  138. void add_boot_device_path(int32_t bootindex, DeviceState *dev,
  139. const char *suffix)
  140. {
  141. FWBootEntry *node, *i;
  142. if (bootindex < 0) {
  143. del_boot_device_path(dev, suffix);
  144. return;
  145. }
  146. assert(dev != NULL || suffix != NULL);
  147. del_boot_device_path(dev, suffix);
  148. node = g_new0(FWBootEntry, 1);
  149. node->bootindex = bootindex;
  150. node->suffix = g_strdup(suffix);
  151. node->dev = dev;
  152. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  153. if (i->bootindex == bootindex) {
  154. error_report("Two devices with same boot index %d", bootindex);
  155. exit(1);
  156. } else if (i->bootindex < bootindex) {
  157. continue;
  158. }
  159. QTAILQ_INSERT_BEFORE(i, node, link);
  160. return;
  161. }
  162. QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
  163. }
  164. DeviceState *get_boot_device(uint32_t position)
  165. {
  166. uint32_t counter = 0;
  167. FWBootEntry *i = NULL;
  168. DeviceState *res = NULL;
  169. if (!QTAILQ_EMPTY(&fw_boot_order)) {
  170. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  171. if (counter == position) {
  172. res = i->dev;
  173. break;
  174. }
  175. counter++;
  176. }
  177. }
  178. return res;
  179. }
  180. static char *get_boot_device_path(DeviceState *dev, bool ignore_suffixes,
  181. const char *suffix)
  182. {
  183. char *devpath = NULL, *s = NULL, *d, *bootpath;
  184. if (dev) {
  185. devpath = qdev_get_fw_dev_path(dev);
  186. assert(devpath);
  187. }
  188. if (!ignore_suffixes) {
  189. if (dev) {
  190. d = qdev_get_own_fw_dev_path_from_handler(dev->parent_bus, dev);
  191. if (d) {
  192. assert(!suffix);
  193. s = d;
  194. } else {
  195. s = g_strdup(suffix);
  196. }
  197. } else {
  198. s = g_strdup(suffix);
  199. }
  200. }
  201. bootpath = g_strdup_printf("%s%s",
  202. devpath ? devpath : "",
  203. s ? s : "");
  204. g_free(devpath);
  205. g_free(s);
  206. return bootpath;
  207. }
  208. /*
  209. * This function returns null terminated string that consist of new line
  210. * separated device paths.
  211. *
  212. * memory pointed by "size" is assigned total length of the array in bytes
  213. *
  214. */
  215. char *get_boot_devices_list(size_t *size)
  216. {
  217. FWBootEntry *i;
  218. size_t total = 0;
  219. char *list = NULL;
  220. MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
  221. bool ignore_suffixes = mc->ignore_boot_device_suffixes;
  222. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  223. char *bootpath;
  224. size_t len;
  225. bootpath = get_boot_device_path(i->dev, ignore_suffixes, i->suffix);
  226. if (total) {
  227. list[total-1] = '\n';
  228. }
  229. len = strlen(bootpath) + 1;
  230. list = g_realloc(list, total + len);
  231. memcpy(&list[total], bootpath, len);
  232. total += len;
  233. g_free(bootpath);
  234. }
  235. *size = total;
  236. if (current_machine->boot_config.has_strict &&
  237. current_machine->boot_config.strict && *size > 0) {
  238. list[total-1] = '\n';
  239. list = g_realloc(list, total + 5);
  240. memcpy(&list[total], "HALT", 5);
  241. *size = total + 5;
  242. }
  243. return list;
  244. }
  245. typedef struct {
  246. int32_t *bootindex;
  247. const char *suffix;
  248. DeviceState *dev;
  249. } BootIndexProperty;
  250. static void device_get_bootindex(Object *obj, Visitor *v, const char *name,
  251. void *opaque, Error **errp)
  252. {
  253. BootIndexProperty *prop = opaque;
  254. visit_type_int32(v, name, prop->bootindex, errp);
  255. }
  256. static void device_set_bootindex(Object *obj, Visitor *v, const char *name,
  257. void *opaque, Error **errp)
  258. {
  259. BootIndexProperty *prop = opaque;
  260. int32_t boot_index;
  261. Error *local_err = NULL;
  262. if (!visit_type_int32(v, name, &boot_index, errp)) {
  263. return;
  264. }
  265. /* check whether bootindex is present in fw_boot_order list */
  266. check_boot_index(boot_index, &local_err);
  267. if (local_err) {
  268. error_propagate(errp, local_err);
  269. return;
  270. }
  271. /* change bootindex to a new one */
  272. *prop->bootindex = boot_index;
  273. add_boot_device_path(*prop->bootindex, prop->dev, prop->suffix);
  274. }
  275. static void property_release_bootindex(Object *obj, const char *name,
  276. void *opaque)
  277. {
  278. BootIndexProperty *prop = opaque;
  279. del_boot_device_path(prop->dev, prop->suffix);
  280. g_free(prop);
  281. }
  282. void device_add_bootindex_property(Object *obj, int32_t *bootindex,
  283. const char *name, const char *suffix,
  284. DeviceState *dev)
  285. {
  286. BootIndexProperty *prop = g_malloc0(sizeof(*prop));
  287. prop->bootindex = bootindex;
  288. prop->suffix = suffix;
  289. prop->dev = dev;
  290. object_property_add(obj, name, "int32",
  291. device_get_bootindex,
  292. device_set_bootindex,
  293. property_release_bootindex,
  294. prop);
  295. /* initialize devices' bootindex property to -1 */
  296. object_property_set_int(obj, name, -1, NULL);
  297. }
  298. typedef struct FWLCHSEntry FWLCHSEntry;
  299. struct FWLCHSEntry {
  300. QTAILQ_ENTRY(FWLCHSEntry) link;
  301. DeviceState *dev;
  302. char *suffix;
  303. uint32_t lcyls;
  304. uint32_t lheads;
  305. uint32_t lsecs;
  306. };
  307. static QTAILQ_HEAD(, FWLCHSEntry) fw_lchs =
  308. QTAILQ_HEAD_INITIALIZER(fw_lchs);
  309. void add_boot_device_lchs(DeviceState *dev, const char *suffix,
  310. uint32_t lcyls, uint32_t lheads, uint32_t lsecs)
  311. {
  312. FWLCHSEntry *node;
  313. if (!lcyls && !lheads && !lsecs) {
  314. return;
  315. }
  316. assert(dev != NULL || suffix != NULL);
  317. node = g_new0(FWLCHSEntry, 1);
  318. node->suffix = g_strdup(suffix);
  319. node->dev = dev;
  320. node->lcyls = lcyls;
  321. node->lheads = lheads;
  322. node->lsecs = lsecs;
  323. QTAILQ_INSERT_TAIL(&fw_lchs, node, link);
  324. }
  325. void del_boot_device_lchs(DeviceState *dev, const char *suffix)
  326. {
  327. FWLCHSEntry *i;
  328. if (dev == NULL) {
  329. return;
  330. }
  331. QTAILQ_FOREACH(i, &fw_lchs, link) {
  332. if ((!suffix || !g_strcmp0(i->suffix, suffix)) &&
  333. i->dev == dev) {
  334. QTAILQ_REMOVE(&fw_lchs, i, link);
  335. g_free(i->suffix);
  336. g_free(i);
  337. break;
  338. }
  339. }
  340. }
  341. char *get_boot_devices_lchs_list(size_t *size)
  342. {
  343. FWLCHSEntry *i;
  344. size_t total = 0;
  345. char *list = NULL;
  346. QTAILQ_FOREACH(i, &fw_lchs, link) {
  347. char *bootpath;
  348. char *chs_string;
  349. size_t len;
  350. bootpath = get_boot_device_path(i->dev, false, i->suffix);
  351. chs_string = g_strdup_printf("%s %" PRIu32 " %" PRIu32 " %" PRIu32,
  352. bootpath, i->lcyls, i->lheads, i->lsecs);
  353. if (total) {
  354. list[total - 1] = '\n';
  355. }
  356. len = strlen(chs_string) + 1;
  357. list = g_realloc(list, total + len);
  358. memcpy(&list[total], chs_string, len);
  359. total += len;
  360. g_free(chs_string);
  361. g_free(bootpath);
  362. }
  363. *size = total;
  364. return list;
  365. }