blob: df9ab0e8021dc45b0a318386311aa8eec88f9ccd [file] [log] [blame]
Gongleibc741122014-10-07 16:00:05 +08001/*
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"
Gonglei12da3092014-10-07 16:00:11 +080026#include "qapi/visitor.h"
Gonglei54086fe2014-10-07 16:00:38 +080027#include "qemu/error-report.h"
Gonglei98168332014-12-03 16:49:46 +000028#include "hw/hw.h"
Gongleibc741122014-10-07 16:00:05 +080029
30typedef struct FWBootEntry FWBootEntry;
31
32struct FWBootEntry {
33 QTAILQ_ENTRY(FWBootEntry) link;
34 int32_t bootindex;
35 DeviceState *dev;
36 char *suffix;
37};
38
39static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
40 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
Gonglei98168332014-12-03 16:49:46 +000041static QEMUBootSetHandler *boot_set_handler;
42static void *boot_set_opaque;
43
44void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
45{
46 boot_set_handler = func;
47 boot_set_opaque = opaque;
48}
49
Gongleif1839932014-12-03 18:20:58 +000050void qemu_boot_set(const char *boot_order, Error **errp)
Gonglei98168332014-12-03 16:49:46 +000051{
Gonglei3b080982014-12-03 18:25:46 +000052 Error *local_err = NULL;
53
Gonglei98168332014-12-03 16:49:46 +000054 if (!boot_set_handler) {
Gongleif1839932014-12-03 18:20:58 +000055 error_setg(errp, "no function defined to set boot device list for"
56 " this architecture");
57 return;
Gonglei98168332014-12-03 16:49:46 +000058 }
Gongleif1839932014-12-03 18:20:58 +000059
Gonglei3b080982014-12-03 18:25:46 +000060 validate_bootdevices(boot_order, &local_err);
61 if (local_err) {
62 error_propagate(errp, local_err);
63 return;
64 }
65
Gongleiddcd5532014-12-03 19:04:02 +000066 boot_set_handler(boot_set_opaque, boot_order, errp);
Gonglei98168332014-12-03 16:49:46 +000067}
68
Gonglei703008e2014-12-03 17:11:39 +000069void validate_bootdevices(const char *devices, Error **errp)
Gonglei98168332014-12-03 16:49:46 +000070{
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') {
Gonglei703008e2014-12-03 17:11:39 +000086 error_setg(errp, "Invalid boot device '%c'", *p);
87 return;
Gonglei98168332014-12-03 16:49:46 +000088 }
89 if (bitmap & (1 << (*p - 'a'))) {
Gonglei703008e2014-12-03 17:11:39 +000090 error_setg(errp, "Boot device '%c' was given twice", *p);
91 return;
Gonglei98168332014-12-03 16:49:46 +000092 }
93 bitmap |= 1 << (*p - 'a');
94 }
95}
96
97void 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
Gonglei76349f52015-01-29 13:13:47 +0000108 if (boot_set_handler) {
109 qemu_boot_set(normal_boot_order, &error_abort);
110 }
Gonglei98168332014-12-03 16:49:46 +0000111
112 qemu_unregister_reset(restore_boot_order, normal_boot_order);
113 g_free(normal_boot_order);
114}
Gongleibc741122014-10-07 16:00:05 +0800115
Gonglei694fb852014-10-07 16:00:06 +0800116void check_boot_index(int32_t bootindex, Error **errp)
117{
118 FWBootEntry *i;
119
120 if (bootindex >= 0) {
121 QTAILQ_FOREACH(i, &fw_boot_order, link) {
122 if (i->bootindex == bootindex) {
123 error_setg(errp, "The bootindex %d has already been used",
124 bootindex);
125 return;
126 }
127 }
128 }
129}
130
Gonglei9d275722014-10-07 16:00:07 +0800131void del_boot_device_path(DeviceState *dev, const char *suffix)
132{
133 FWBootEntry *i;
134
135 if (dev == NULL) {
136 return;
137 }
138
139 QTAILQ_FOREACH(i, &fw_boot_order, link) {
140 if ((!suffix || !g_strcmp0(i->suffix, suffix)) &&
141 i->dev == dev) {
142 QTAILQ_REMOVE(&fw_boot_order, i, link);
143 g_free(i->suffix);
144 g_free(i);
145
146 break;
147 }
148 }
149}
150
Gongleibc741122014-10-07 16:00:05 +0800151void add_boot_device_path(int32_t bootindex, DeviceState *dev,
152 const char *suffix)
153{
154 FWBootEntry *node, *i;
155
156 if (bootindex < 0) {
Gongleia598f2f2014-10-07 16:00:10 +0800157 del_boot_device_path(dev, suffix);
Gongleibc741122014-10-07 16:00:05 +0800158 return;
159 }
160
161 assert(dev != NULL || suffix != NULL);
162
Gongleie614b542014-10-07 16:00:09 +0800163 del_boot_device_path(dev, suffix);
164
Gongleibc741122014-10-07 16:00:05 +0800165 node = g_malloc0(sizeof(FWBootEntry));
166 node->bootindex = bootindex;
167 node->suffix = g_strdup(suffix);
168 node->dev = dev;
169
170 QTAILQ_FOREACH(i, &fw_boot_order, link) {
171 if (i->bootindex == bootindex) {
Gonglei54086fe2014-10-07 16:00:38 +0800172 error_report("Two devices with same boot index %d", bootindex);
Gongleibc741122014-10-07 16:00:05 +0800173 exit(1);
174 } else if (i->bootindex < bootindex) {
175 continue;
176 }
177 QTAILQ_INSERT_BEFORE(i, node, link);
178 return;
179 }
180 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
181}
182
183DeviceState *get_boot_device(uint32_t position)
184{
185 uint32_t counter = 0;
186 FWBootEntry *i = NULL;
187 DeviceState *res = NULL;
188
189 if (!QTAILQ_EMPTY(&fw_boot_order)) {
190 QTAILQ_FOREACH(i, &fw_boot_order, link) {
191 if (counter == position) {
192 res = i->dev;
193 break;
194 }
195 counter++;
196 }
197 }
198 return res;
199}
200
201/*
202 * This function returns null terminated string that consist of new line
203 * separated device paths.
204 *
205 * memory pointed by "size" is assigned total length of the array in bytes
206 *
207 */
208char *get_boot_devices_list(size_t *size, bool ignore_suffixes)
209{
210 FWBootEntry *i;
211 size_t total = 0;
212 char *list = NULL;
213
214 QTAILQ_FOREACH(i, &fw_boot_order, link) {
Gonglei0be63902015-01-29 15:08:51 +0800215 char *devpath = NULL, *suffix = NULL;
216 char *bootpath;
217 char *d;
Gongleibc741122014-10-07 16:00:05 +0800218 size_t len;
219
220 if (i->dev) {
221 devpath = qdev_get_fw_dev_path(i->dev);
222 assert(devpath);
223 }
224
Gonglei0be63902015-01-29 15:08:51 +0800225 if (!ignore_suffixes) {
226 d = qdev_get_own_fw_dev_path_from_handler(i->dev->parent_bus, i->dev);
227 if (d) {
228 assert(!i->suffix);
229 suffix = d;
230 } else {
231 suffix = g_strdup(i->suffix);
232 }
Gongleibc741122014-10-07 16:00:05 +0800233 }
234
Gonglei0be63902015-01-29 15:08:51 +0800235 bootpath = g_strdup_printf("%s%s",
236 devpath ? devpath : "",
237 suffix ? suffix : "");
238 g_free(devpath);
239 g_free(suffix);
240
Gongleibc741122014-10-07 16:00:05 +0800241 if (total) {
242 list[total-1] = '\n';
243 }
244 len = strlen(bootpath) + 1;
245 list = g_realloc(list, total + len);
246 memcpy(&list[total], bootpath, len);
247 total += len;
248 g_free(bootpath);
249 }
250
251 *size = total;
252
253 if (boot_strict && *size > 0) {
254 list[total-1] = '\n';
255 list = g_realloc(list, total + 5);
256 memcpy(&list[total], "HALT", 5);
257 *size = total + 5;
258 }
259 return list;
260}
Gonglei12da3092014-10-07 16:00:11 +0800261
262typedef struct {
263 int32_t *bootindex;
264 const char *suffix;
265 DeviceState *dev;
266} BootIndexProperty;
267
268static void device_get_bootindex(Object *obj, Visitor *v, void *opaque,
269 const char *name, Error **errp)
270{
271 BootIndexProperty *prop = opaque;
272 visit_type_int32(v, prop->bootindex, name, errp);
273}
274
275static void device_set_bootindex(Object *obj, Visitor *v, void *opaque,
276 const char *name, Error **errp)
277{
278 BootIndexProperty *prop = opaque;
279 int32_t boot_index;
280 Error *local_err = NULL;
281
282 visit_type_int32(v, &boot_index, name, &local_err);
283 if (local_err) {
284 goto out;
285 }
286 /* check whether bootindex is present in fw_boot_order list */
287 check_boot_index(boot_index, &local_err);
288 if (local_err) {
289 goto out;
290 }
291 /* change bootindex to a new one */
292 *prop->bootindex = boot_index;
293
Gongleid749e102014-10-07 16:00:36 +0800294 add_boot_device_path(*prop->bootindex, prop->dev, prop->suffix);
295
Gonglei12da3092014-10-07 16:00:11 +0800296out:
297 if (local_err) {
298 error_propagate(errp, local_err);
299 }
300}
301
302static void property_release_bootindex(Object *obj, const char *name,
303 void *opaque)
304
305{
306 BootIndexProperty *prop = opaque;
Gonglei4aca8a82014-10-07 16:00:37 +0800307
308 del_boot_device_path(prop->dev, prop->suffix);
Gonglei12da3092014-10-07 16:00:11 +0800309 g_free(prop);
310}
311
312void device_add_bootindex_property(Object *obj, int32_t *bootindex,
313 const char *name, const char *suffix,
314 DeviceState *dev, Error **errp)
315{
316 Error *local_err = NULL;
317 BootIndexProperty *prop = g_malloc0(sizeof(*prop));
318
319 prop->bootindex = bootindex;
320 prop->suffix = suffix;
321 prop->dev = dev;
322
323 object_property_add(obj, name, "int32",
324 device_get_bootindex,
325 device_set_bootindex,
326 property_release_bootindex,
327 prop, &local_err);
328
329 if (local_err) {
330 error_propagate(errp, local_err);
331 g_free(prop);
332 return;
333 }
334 /* initialize devices' bootindex property to -1 */
335 object_property_set_int(obj, -1, name, NULL);
336}