2 * QEMU Boot Device Implement
4 * Copyright (c) 2014 HUAWEI TECHNOLOGIES CO., LTD.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "sysemu/sysemu.h"
28 #include "qapi/visitor.h"
29 #include "qemu/error-report.h"
31 #include "hw/qdev-core.h"
33 typedef struct FWBootEntry FWBootEntry
;
36 QTAILQ_ENTRY(FWBootEntry
) link
;
42 static QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
=
43 QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
44 static QEMUBootSetHandler
*boot_set_handler
;
45 static void *boot_set_opaque
;
47 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
49 boot_set_handler
= func
;
50 boot_set_opaque
= opaque
;
53 void qemu_boot_set(const char *boot_order
, Error
**errp
)
55 Error
*local_err
= NULL
;
57 if (!boot_set_handler
) {
58 error_setg(errp
, "no function defined to set boot device list for"
59 " this architecture");
63 validate_bootdevices(boot_order
, &local_err
);
65 error_propagate(errp
, local_err
);
69 boot_set_handler(boot_set_opaque
, boot_order
, errp
);
72 void validate_bootdevices(const char *devices
, Error
**errp
)
74 /* We just do some generic consistency checks */
78 for (p
= devices
; *p
!= '\0'; p
++) {
79 /* Allowed boot devices are:
80 * a-b: floppy disk drives
81 * c-f: IDE disk drives
82 * g-m: machine implementation dependent drives
83 * n-p: network devices
84 * It's up to each machine implementation to check if the given boot
85 * devices match the actual hardware implementation and firmware
88 if (*p
< 'a' || *p
> 'p') {
89 error_setg(errp
, "Invalid boot device '%c'", *p
);
92 if (bitmap
& (1 << (*p
- 'a'))) {
93 error_setg(errp
, "Boot device '%c' was given twice", *p
);
96 bitmap
|= 1 << (*p
- 'a');
100 void restore_boot_order(void *opaque
)
102 char *normal_boot_order
= opaque
;
103 static int first
= 1;
105 /* Restore boot order and remove ourselves after the first boot */
111 if (boot_set_handler
) {
112 qemu_boot_set(normal_boot_order
, &error_abort
);
115 qemu_unregister_reset(restore_boot_order
, normal_boot_order
);
116 g_free(normal_boot_order
);
119 void check_boot_index(int32_t bootindex
, Error
**errp
)
123 if (bootindex
>= 0) {
124 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
125 if (i
->bootindex
== bootindex
) {
126 error_setg(errp
, "The bootindex %d has already been used",
134 void del_boot_device_path(DeviceState
*dev
, const char *suffix
)
142 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
143 if ((!suffix
|| !g_strcmp0(i
->suffix
, suffix
)) &&
145 QTAILQ_REMOVE(&fw_boot_order
, i
, link
);
154 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
157 FWBootEntry
*node
, *i
;
160 del_boot_device_path(dev
, suffix
);
164 assert(dev
!= NULL
|| suffix
!= NULL
);
166 del_boot_device_path(dev
, suffix
);
168 node
= g_malloc0(sizeof(FWBootEntry
));
169 node
->bootindex
= bootindex
;
170 node
->suffix
= g_strdup(suffix
);
173 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
174 if (i
->bootindex
== bootindex
) {
175 error_report("Two devices with same boot index %d", bootindex
);
177 } else if (i
->bootindex
< bootindex
) {
180 QTAILQ_INSERT_BEFORE(i
, node
, link
);
183 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
186 DeviceState
*get_boot_device(uint32_t position
)
188 uint32_t counter
= 0;
189 FWBootEntry
*i
= NULL
;
190 DeviceState
*res
= NULL
;
192 if (!QTAILQ_EMPTY(&fw_boot_order
)) {
193 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
194 if (counter
== position
) {
205 * This function returns null terminated string that consist of new line
206 * separated device paths.
208 * memory pointed by "size" is assigned total length of the array in bytes
211 char *get_boot_devices_list(size_t *size
, bool ignore_suffixes
)
217 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
218 char *devpath
= NULL
, *suffix
= NULL
;
224 devpath
= qdev_get_fw_dev_path(i
->dev
);
228 if (!ignore_suffixes
) {
230 d
= qdev_get_own_fw_dev_path_from_handler(i
->dev
->parent_bus
,
236 suffix
= g_strdup(i
->suffix
);
239 suffix
= g_strdup(i
->suffix
);
243 bootpath
= g_strdup_printf("%s%s",
244 devpath
? devpath
: "",
245 suffix
? suffix
: "");
250 list
[total
-1] = '\n';
252 len
= strlen(bootpath
) + 1;
253 list
= g_realloc(list
, total
+ len
);
254 memcpy(&list
[total
], bootpath
, len
);
261 if (boot_strict
&& *size
> 0) {
262 list
[total
-1] = '\n';
263 list
= g_realloc(list
, total
+ 5);
264 memcpy(&list
[total
], "HALT", 5);
276 static void device_get_bootindex(Object
*obj
, Visitor
*v
, const char *name
,
277 void *opaque
, Error
**errp
)
279 BootIndexProperty
*prop
= opaque
;
280 visit_type_int32(v
, name
, prop
->bootindex
, errp
);
283 static void device_set_bootindex(Object
*obj
, Visitor
*v
, const char *name
,
284 void *opaque
, Error
**errp
)
286 BootIndexProperty
*prop
= opaque
;
288 Error
*local_err
= NULL
;
290 visit_type_int32(v
, name
, &boot_index
, &local_err
);
294 /* check whether bootindex is present in fw_boot_order list */
295 check_boot_index(boot_index
, &local_err
);
299 /* change bootindex to a new one */
300 *prop
->bootindex
= boot_index
;
302 add_boot_device_path(*prop
->bootindex
, prop
->dev
, prop
->suffix
);
306 error_propagate(errp
, local_err
);
310 static void property_release_bootindex(Object
*obj
, const char *name
,
314 BootIndexProperty
*prop
= opaque
;
316 del_boot_device_path(prop
->dev
, prop
->suffix
);
320 void device_add_bootindex_property(Object
*obj
, int32_t *bootindex
,
321 const char *name
, const char *suffix
,
322 DeviceState
*dev
, Error
**errp
)
324 Error
*local_err
= NULL
;
325 BootIndexProperty
*prop
= g_malloc0(sizeof(*prop
));
327 prop
->bootindex
= bootindex
;
328 prop
->suffix
= suffix
;
331 object_property_add(obj
, name
, "int32",
332 device_get_bootindex
,
333 device_set_bootindex
,
334 property_release_bootindex
,
338 error_propagate(errp
, local_err
);
342 /* initialize devices' bootindex property to -1 */
343 object_property_set_int(obj
, -1, name
, NULL
);