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