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"
30 #include "sysemu/reset.h"
31 #include "hw/qdev-core.h"
32 #include "hw/boards.h"
34 typedef struct FWBootEntry FWBootEntry
;
37 QTAILQ_ENTRY(FWBootEntry
) link
;
43 static QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
=
44 QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
45 static QEMUBootSetHandler
*boot_set_handler
;
46 static void *boot_set_opaque
;
48 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
50 boot_set_handler
= func
;
51 boot_set_opaque
= opaque
;
54 void qemu_boot_set(const char *boot_order
, Error
**errp
)
56 Error
*local_err
= NULL
;
58 if (!boot_set_handler
) {
59 error_setg(errp
, "no function defined to set boot device list for"
60 " this architecture");
64 validate_bootdevices(boot_order
, &local_err
);
66 error_propagate(errp
, local_err
);
70 boot_set_handler(boot_set_opaque
, boot_order
, errp
);
73 void validate_bootdevices(const char *devices
, Error
**errp
)
75 /* We just do some generic consistency checks */
79 for (p
= devices
; *p
!= '\0'; p
++) {
80 /* Allowed boot devices are:
81 * a-b: floppy disk drives
82 * c-f: IDE disk drives
83 * g-m: machine implementation dependent drives
84 * n-p: network devices
85 * It's up to each machine implementation to check if the given boot
86 * devices match the actual hardware implementation and firmware
89 if (*p
< 'a' || *p
> 'p') {
90 error_setg(errp
, "Invalid boot device '%c'", *p
);
93 if (bitmap
& (1 << (*p
- 'a'))) {
94 error_setg(errp
, "Boot device '%c' was given twice", *p
);
97 bitmap
|= 1 << (*p
- 'a');
101 void restore_boot_order(void *opaque
)
103 char *normal_boot_order
= opaque
;
104 static int first
= 1;
106 /* Restore boot order and remove ourselves after the first boot */
112 if (boot_set_handler
) {
113 qemu_boot_set(normal_boot_order
, &error_abort
);
116 qemu_unregister_reset(restore_boot_order
, normal_boot_order
);
117 g_free(normal_boot_order
);
120 void check_boot_index(int32_t bootindex
, Error
**errp
)
124 if (bootindex
>= 0) {
125 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
126 if (i
->bootindex
== bootindex
) {
127 error_setg(errp
, "The bootindex %d has already been used",
135 void del_boot_device_path(DeviceState
*dev
, const char *suffix
)
143 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
144 if ((!suffix
|| !g_strcmp0(i
->suffix
, suffix
)) &&
146 QTAILQ_REMOVE(&fw_boot_order
, i
, link
);
155 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
158 FWBootEntry
*node
, *i
;
161 del_boot_device_path(dev
, suffix
);
165 assert(dev
!= NULL
|| suffix
!= NULL
);
167 del_boot_device_path(dev
, suffix
);
169 node
= g_new0(FWBootEntry
, 1);
170 node
->bootindex
= bootindex
;
171 node
->suffix
= g_strdup(suffix
);
174 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
175 if (i
->bootindex
== bootindex
) {
176 error_report("Two devices with same boot index %d", bootindex
);
178 } else if (i
->bootindex
< bootindex
) {
181 QTAILQ_INSERT_BEFORE(i
, node
, link
);
184 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
187 DeviceState
*get_boot_device(uint32_t position
)
189 uint32_t counter
= 0;
190 FWBootEntry
*i
= NULL
;
191 DeviceState
*res
= NULL
;
193 if (!QTAILQ_EMPTY(&fw_boot_order
)) {
194 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
195 if (counter
== position
) {
205 static char *get_boot_device_path(DeviceState
*dev
, bool ignore_suffixes
,
208 char *devpath
= NULL
, *s
= NULL
, *d
, *bootpath
;
211 devpath
= qdev_get_fw_dev_path(dev
);
215 if (!ignore_suffixes
) {
217 d
= qdev_get_own_fw_dev_path_from_handler(dev
->parent_bus
, dev
);
222 s
= g_strdup(suffix
);
225 s
= g_strdup(suffix
);
229 bootpath
= g_strdup_printf("%s%s",
230 devpath
? devpath
: "",
239 * This function returns null terminated string that consist of new line
240 * separated device paths.
242 * memory pointed by "size" is assigned total length of the array in bytes
245 char *get_boot_devices_list(size_t *size
)
250 MachineClass
*mc
= MACHINE_GET_CLASS(qdev_get_machine());
251 bool ignore_suffixes
= mc
->ignore_boot_device_suffixes
;
253 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
257 bootpath
= get_boot_device_path(i
->dev
, ignore_suffixes
, i
->suffix
);
260 list
[total
-1] = '\n';
262 len
= strlen(bootpath
) + 1;
263 list
= g_realloc(list
, total
+ len
);
264 memcpy(&list
[total
], bootpath
, len
);
271 if (current_machine
->boot_config
.has_strict
&&
272 current_machine
->boot_config
.strict
&& *size
> 0) {
273 list
[total
-1] = '\n';
274 list
= g_realloc(list
, total
+ 5);
275 memcpy(&list
[total
], "HALT", 5);
287 static void device_get_bootindex(Object
*obj
, Visitor
*v
, const char *name
,
288 void *opaque
, Error
**errp
)
290 BootIndexProperty
*prop
= opaque
;
291 visit_type_int32(v
, name
, prop
->bootindex
, errp
);
294 static void device_set_bootindex(Object
*obj
, Visitor
*v
, const char *name
,
295 void *opaque
, Error
**errp
)
297 BootIndexProperty
*prop
= opaque
;
299 Error
*local_err
= NULL
;
301 if (!visit_type_int32(v
, name
, &boot_index
, errp
)) {
304 /* check whether bootindex is present in fw_boot_order list */
305 check_boot_index(boot_index
, &local_err
);
307 error_propagate(errp
, local_err
);
310 /* change bootindex to a new one */
311 *prop
->bootindex
= boot_index
;
313 add_boot_device_path(*prop
->bootindex
, prop
->dev
, prop
->suffix
);
316 static void property_release_bootindex(Object
*obj
, const char *name
,
320 BootIndexProperty
*prop
= opaque
;
322 del_boot_device_path(prop
->dev
, prop
->suffix
);
326 void device_add_bootindex_property(Object
*obj
, int32_t *bootindex
,
327 const char *name
, const char *suffix
,
330 BootIndexProperty
*prop
= g_malloc0(sizeof(*prop
));
332 prop
->bootindex
= bootindex
;
333 prop
->suffix
= suffix
;
336 object_property_add(obj
, name
, "int32",
337 device_get_bootindex
,
338 device_set_bootindex
,
339 property_release_bootindex
,
342 /* initialize devices' bootindex property to -1 */
343 object_property_set_int(obj
, name
, -1, NULL
);
346 typedef struct FWLCHSEntry FWLCHSEntry
;
349 QTAILQ_ENTRY(FWLCHSEntry
) link
;
357 static QTAILQ_HEAD(, FWLCHSEntry
) fw_lchs
=
358 QTAILQ_HEAD_INITIALIZER(fw_lchs
);
360 void add_boot_device_lchs(DeviceState
*dev
, const char *suffix
,
361 uint32_t lcyls
, uint32_t lheads
, uint32_t lsecs
)
365 if (!lcyls
&& !lheads
&& !lsecs
) {
369 assert(dev
!= NULL
|| suffix
!= NULL
);
371 node
= g_new0(FWLCHSEntry
, 1);
372 node
->suffix
= g_strdup(suffix
);
375 node
->lheads
= lheads
;
378 QTAILQ_INSERT_TAIL(&fw_lchs
, node
, link
);
381 void del_boot_device_lchs(DeviceState
*dev
, const char *suffix
)
389 QTAILQ_FOREACH(i
, &fw_lchs
, link
) {
390 if ((!suffix
|| !g_strcmp0(i
->suffix
, suffix
)) &&
392 QTAILQ_REMOVE(&fw_lchs
, i
, link
);
401 char *get_boot_devices_lchs_list(size_t *size
)
407 QTAILQ_FOREACH(i
, &fw_lchs
, link
) {
412 bootpath
= get_boot_device_path(i
->dev
, false, i
->suffix
);
413 chs_string
= g_strdup_printf("%s %" PRIu32
" %" PRIu32
" %" PRIu32
,
414 bootpath
, i
->lcyls
, i
->lheads
, i
->lsecs
);
417 list
[total
- 1] = '\n';
419 len
= strlen(chs_string
) + 1;
420 list
= g_realloc(list
, total
+ len
);
421 memcpy(&list
[total
], chs_string
, len
);