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