]> git.proxmox.com Git - mirror_qemu.git/blame - bootdevice.c
Merge remote-tracking branch 'remotes/armbru/tags/pull-qapi-2016-03-18' into staging
[mirror_qemu.git] / bootdevice.c
CommitLineData
bc74112f
GA
1/*
2 * QEMU Boot Device Implement
3 *
fe75270c 4 * Copyright (c) 2014 HUAWEI TECHNOLOGIES CO., LTD.
bc74112f
GA
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
d38ea87a 25#include "qemu/osdep.h"
bc74112f 26#include "sysemu/sysemu.h"
12da3097 27#include "qapi/visitor.h"
54086fe5 28#include "qemu/error-report.h"
9816833d 29#include "hw/hw.h"
bc74112f
GA
30
31typedef struct FWBootEntry FWBootEntry;
32
33struct FWBootEntry {
34 QTAILQ_ENTRY(FWBootEntry) link;
35 int32_t bootindex;
36 DeviceState *dev;
37 char *suffix;
38};
39
40static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
41 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
9816833d
GA
42static QEMUBootSetHandler *boot_set_handler;
43static void *boot_set_opaque;
44
45void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
46{
47 boot_set_handler = func;
48 boot_set_opaque = opaque;
49}
50
f1839938 51void qemu_boot_set(const char *boot_order, Error **errp)
9816833d 52{
3b08098b
GA
53 Error *local_err = NULL;
54
9816833d 55 if (!boot_set_handler) {
f1839938
GA
56 error_setg(errp, "no function defined to set boot device list for"
57 " this architecture");
58 return;
59 }
60
3b08098b
GA
61 validate_bootdevices(boot_order, &local_err);
62 if (local_err) {
63 error_propagate(errp, local_err);
64 return;
65 }
66
ddcd5531 67 boot_set_handler(boot_set_opaque, boot_order, errp);
9816833d
GA
68}
69
703008e8 70void validate_bootdevices(const char *devices, Error **errp)
9816833d
GA
71{
72 /* We just do some generic consistency checks */
73 const char *p;
74 int bitmap = 0;
75
76 for (p = devices; *p != '\0'; p++) {
77 /* Allowed boot devices are:
78 * a-b: floppy disk drives
79 * c-f: IDE disk drives
80 * g-m: machine implementation dependent drives
81 * n-p: network devices
82 * It's up to each machine implementation to check if the given boot
83 * devices match the actual hardware implementation and firmware
84 * features.
85 */
86 if (*p < 'a' || *p > 'p') {
703008e8
GA
87 error_setg(errp, "Invalid boot device '%c'", *p);
88 return;
9816833d
GA
89 }
90 if (bitmap & (1 << (*p - 'a'))) {
703008e8
GA
91 error_setg(errp, "Boot device '%c' was given twice", *p);
92 return;
9816833d
GA
93 }
94 bitmap |= 1 << (*p - 'a');
95 }
96}
97
98void restore_boot_order(void *opaque)
99{
100 char *normal_boot_order = opaque;
101 static int first = 1;
102
103 /* Restore boot order and remove ourselves after the first boot */
104 if (first) {
105 first = 0;
106 return;
107 }
108
76349f5b
GA
109 if (boot_set_handler) {
110 qemu_boot_set(normal_boot_order, &error_abort);
111 }
9816833d
GA
112
113 qemu_unregister_reset(restore_boot_order, normal_boot_order);
114 g_free(normal_boot_order);
115}
bc74112f 116
694fb857
GA
117void check_boot_index(int32_t bootindex, Error **errp)
118{
119 FWBootEntry *i;
120
121 if (bootindex >= 0) {
122 QTAILQ_FOREACH(i, &fw_boot_order, link) {
123 if (i->bootindex == bootindex) {
124 error_setg(errp, "The bootindex %d has already been used",
125 bootindex);
126 return;
127 }
128 }
129 }
130}
131
9d27572d
GA
132void del_boot_device_path(DeviceState *dev, const char *suffix)
133{
134 FWBootEntry *i;
135
136 if (dev == NULL) {
137 return;
138 }
139
140 QTAILQ_FOREACH(i, &fw_boot_order, link) {
141 if ((!suffix || !g_strcmp0(i->suffix, suffix)) &&
142 i->dev == dev) {
143 QTAILQ_REMOVE(&fw_boot_order, i, link);
144 g_free(i->suffix);
145 g_free(i);
146
147 break;
148 }
149 }
150}
151
bc74112f
GA
152void add_boot_device_path(int32_t bootindex, DeviceState *dev,
153 const char *suffix)
154{
155 FWBootEntry *node, *i;
156
157 if (bootindex < 0) {
a598f2ff 158 del_boot_device_path(dev, suffix);
bc74112f
GA
159 return;
160 }
161
162 assert(dev != NULL || suffix != NULL);
163
e614b54b
GA
164 del_boot_device_path(dev, suffix);
165
bc74112f
GA
166 node = g_malloc0(sizeof(FWBootEntry));
167 node->bootindex = bootindex;
168 node->suffix = g_strdup(suffix);
169 node->dev = dev;
170
171 QTAILQ_FOREACH(i, &fw_boot_order, link) {
172 if (i->bootindex == bootindex) {
54086fe5 173 error_report("Two devices with same boot index %d", bootindex);
bc74112f
GA
174 exit(1);
175 } else if (i->bootindex < bootindex) {
176 continue;
177 }
178 QTAILQ_INSERT_BEFORE(i, node, link);
179 return;
180 }
181 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
182}
183
184DeviceState *get_boot_device(uint32_t position)
185{
186 uint32_t counter = 0;
187 FWBootEntry *i = NULL;
188 DeviceState *res = NULL;
189
190 if (!QTAILQ_EMPTY(&fw_boot_order)) {
191 QTAILQ_FOREACH(i, &fw_boot_order, link) {
192 if (counter == position) {
193 res = i->dev;
194 break;
195 }
196 counter++;
197 }
198 }
199 return res;
200}
201
202/*
203 * This function returns null terminated string that consist of new line
204 * separated device paths.
205 *
206 * memory pointed by "size" is assigned total length of the array in bytes
207 *
208 */
209char *get_boot_devices_list(size_t *size, bool ignore_suffixes)
210{
211 FWBootEntry *i;
212 size_t total = 0;
213 char *list = NULL;
214
215 QTAILQ_FOREACH(i, &fw_boot_order, link) {
0be63901
GA
216 char *devpath = NULL, *suffix = NULL;
217 char *bootpath;
218 char *d;
bc74112f
GA
219 size_t len;
220
221 if (i->dev) {
222 devpath = qdev_get_fw_dev_path(i->dev);
223 assert(devpath);
224 }
225
0be63901 226 if (!ignore_suffixes) {
57fe6a6e
GA
227 if (i->dev) {
228 d = qdev_get_own_fw_dev_path_from_handler(i->dev->parent_bus,
229 i->dev);
230 if (d) {
231 assert(!i->suffix);
232 suffix = d;
233 } else {
234 suffix = g_strdup(i->suffix);
235 }
0be63901
GA
236 } else {
237 suffix = g_strdup(i->suffix);
238 }
bc74112f
GA
239 }
240
0be63901
GA
241 bootpath = g_strdup_printf("%s%s",
242 devpath ? devpath : "",
243 suffix ? suffix : "");
244 g_free(devpath);
245 g_free(suffix);
246
bc74112f
GA
247 if (total) {
248 list[total-1] = '\n';
249 }
250 len = strlen(bootpath) + 1;
251 list = g_realloc(list, total + len);
252 memcpy(&list[total], bootpath, len);
253 total += len;
254 g_free(bootpath);
255 }
256
257 *size = total;
258
259 if (boot_strict && *size > 0) {
260 list[total-1] = '\n';
261 list = g_realloc(list, total + 5);
262 memcpy(&list[total], "HALT", 5);
263 *size = total + 5;
264 }
265 return list;
266}
12da3097
GA
267
268typedef struct {
269 int32_t *bootindex;
270 const char *suffix;
271 DeviceState *dev;
272} BootIndexProperty;
273
d7bce999
EB
274static void device_get_bootindex(Object *obj, Visitor *v, const char *name,
275 void *opaque, Error **errp)
12da3097
GA
276{
277 BootIndexProperty *prop = opaque;
51e72bc1 278 visit_type_int32(v, name, prop->bootindex, errp);
12da3097
GA
279}
280
d7bce999
EB
281static void device_set_bootindex(Object *obj, Visitor *v, const char *name,
282 void *opaque, Error **errp)
12da3097
GA
283{
284 BootIndexProperty *prop = opaque;
285 int32_t boot_index;
286 Error *local_err = NULL;
287
51e72bc1 288 visit_type_int32(v, name, &boot_index, &local_err);
12da3097
GA
289 if (local_err) {
290 goto out;
291 }
292 /* check whether bootindex is present in fw_boot_order list */
293 check_boot_index(boot_index, &local_err);
294 if (local_err) {
295 goto out;
296 }
297 /* change bootindex to a new one */
298 *prop->bootindex = boot_index;
299
d749e10c
GA
300 add_boot_device_path(*prop->bootindex, prop->dev, prop->suffix);
301
12da3097
GA
302out:
303 if (local_err) {
304 error_propagate(errp, local_err);
305 }
306}
307
308static void property_release_bootindex(Object *obj, const char *name,
309 void *opaque)
310
311{
312 BootIndexProperty *prop = opaque;
4aca8a81
GA
313
314 del_boot_device_path(prop->dev, prop->suffix);
12da3097
GA
315 g_free(prop);
316}
317
318void device_add_bootindex_property(Object *obj, int32_t *bootindex,
319 const char *name, const char *suffix,
320 DeviceState *dev, Error **errp)
321{
322 Error *local_err = NULL;
323 BootIndexProperty *prop = g_malloc0(sizeof(*prop));
324
325 prop->bootindex = bootindex;
326 prop->suffix = suffix;
327 prop->dev = dev;
328
329 object_property_add(obj, name, "int32",
330 device_get_bootindex,
331 device_set_bootindex,
332 property_release_bootindex,
333 prop, &local_err);
334
335 if (local_err) {
336 error_propagate(errp, local_err);
337 g_free(prop);
338 return;
339 }
340 /* initialize devices' bootindex property to -1 */
341 object_property_set_int(obj, -1, name, NULL);
342}