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