target/mips: Prefer fast cpu_env() over slower CPU QOM cast macro
[qemu/ar7.git] / system / bootdevice.c
blob2579b26dc8b4c38db43bcfd32232ff3626f8c09b
1 /*
2 * QEMU Boot Device Implement
4 * Copyright (c) 2014 HUAWEI TECHNOLOGIES CO., LTD.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "sysemu/sysemu.h"
28 #include "qapi/visitor.h"
29 #include "qemu/error-report.h"
30 #include "sysemu/reset.h"
31 #include "hw/qdev-core.h"
32 #include "hw/boards.h"
34 typedef struct FWBootEntry FWBootEntry;
36 struct FWBootEntry {
37 QTAILQ_ENTRY(FWBootEntry) link;
38 int32_t bootindex;
39 DeviceState *dev;
40 char *suffix;
43 static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
44 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
45 static QEMUBootSetHandler *boot_set_handler;
46 static void *boot_set_opaque;
48 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
50 boot_set_handler = func;
51 boot_set_opaque = opaque;
54 void qemu_boot_set(const char *boot_order, Error **errp)
56 Error *local_err = NULL;
58 if (!boot_set_handler) {
59 error_setg(errp, "no function defined to set boot device list for"
60 " this architecture");
61 return;
64 validate_bootdevices(boot_order, &local_err);
65 if (local_err) {
66 error_propagate(errp, local_err);
67 return;
70 boot_set_handler(boot_set_opaque, boot_order, errp);
73 void validate_bootdevices(const char *devices, Error **errp)
75 /* We just do some generic consistency checks */
76 const char *p;
77 int bitmap = 0;
79 for (p = devices; *p != '\0'; p++) {
80 /* Allowed boot devices are:
81 * a-b: floppy disk drives
82 * c-f: IDE disk drives
83 * g-m: machine implementation dependent drives
84 * n-p: network devices
85 * It's up to each machine implementation to check if the given boot
86 * devices match the actual hardware implementation and firmware
87 * features.
89 if (*p < 'a' || *p > 'p') {
90 error_setg(errp, "Invalid boot device '%c'", *p);
91 return;
93 if (bitmap & (1 << (*p - 'a'))) {
94 error_setg(errp, "Boot device '%c' was given twice", *p);
95 return;
97 bitmap |= 1 << (*p - 'a');
101 void restore_boot_order(void *opaque)
103 char *normal_boot_order = opaque;
104 static int bootcount;
106 switch (bootcount++) {
107 case 0:
108 /* First boot: use the one-time config */
109 return;
110 case 1:
111 /* Second boot: restore normal boot order */
112 if (boot_set_handler) {
113 qemu_boot_set(normal_boot_order, &error_abort);
115 g_free(normal_boot_order);
116 return;
117 default:
118 /* Subsequent boots: keep using normal boot order */
119 return;
123 void check_boot_index(int32_t bootindex, Error **errp)
125 FWBootEntry *i;
127 if (bootindex >= 0) {
128 QTAILQ_FOREACH(i, &fw_boot_order, link) {
129 if (i->bootindex == bootindex) {
130 error_setg(errp, "The bootindex %d has already been used",
131 bootindex);
132 return;
138 void del_boot_device_path(DeviceState *dev, const char *suffix)
140 FWBootEntry *i;
142 if (dev == NULL) {
143 return;
146 QTAILQ_FOREACH(i, &fw_boot_order, link) {
147 if ((!suffix || !g_strcmp0(i->suffix, suffix)) &&
148 i->dev == dev) {
149 QTAILQ_REMOVE(&fw_boot_order, i, link);
150 g_free(i->suffix);
151 g_free(i);
153 break;
158 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
159 const char *suffix)
161 FWBootEntry *node, *i;
163 if (bootindex < 0) {
164 del_boot_device_path(dev, suffix);
165 return;
168 assert(dev != NULL || suffix != NULL);
170 del_boot_device_path(dev, suffix);
172 node = g_new0(FWBootEntry, 1);
173 node->bootindex = bootindex;
174 node->suffix = g_strdup(suffix);
175 node->dev = dev;
177 QTAILQ_FOREACH(i, &fw_boot_order, link) {
178 if (i->bootindex == bootindex) {
179 error_report("Two devices with same boot index %d", bootindex);
180 exit(1);
181 } else if (i->bootindex < bootindex) {
182 continue;
184 QTAILQ_INSERT_BEFORE(i, node, link);
185 return;
187 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
190 DeviceState *get_boot_device(uint32_t position)
192 uint32_t counter = 0;
193 FWBootEntry *i = NULL;
194 DeviceState *res = NULL;
196 if (!QTAILQ_EMPTY(&fw_boot_order)) {
197 QTAILQ_FOREACH(i, &fw_boot_order, link) {
198 if (counter == position) {
199 res = i->dev;
200 break;
202 counter++;
205 return res;
208 static char *get_boot_device_path(DeviceState *dev, bool ignore_suffixes,
209 const char *suffix)
211 char *devpath = NULL, *s = NULL, *d, *bootpath;
213 if (dev) {
214 devpath = qdev_get_fw_dev_path(dev);
215 assert(devpath);
218 if (!ignore_suffixes) {
219 if (dev) {
220 d = qdev_get_own_fw_dev_path_from_handler(dev->parent_bus, dev);
221 if (d) {
222 assert(!suffix);
223 s = d;
224 } else {
225 s = g_strdup(suffix);
227 } else {
228 s = g_strdup(suffix);
232 bootpath = g_strdup_printf("%s%s",
233 devpath ? devpath : "",
234 s ? s : "");
235 g_free(devpath);
236 g_free(s);
238 return bootpath;
242 * This function returns null terminated string that consist of new line
243 * separated device paths.
245 * memory pointed by "size" is assigned total length of the array in bytes
248 char *get_boot_devices_list(size_t *size)
250 FWBootEntry *i;
251 size_t total = 0;
252 char *list = NULL;
253 MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
254 bool ignore_suffixes = mc->ignore_boot_device_suffixes;
256 QTAILQ_FOREACH(i, &fw_boot_order, link) {
257 char *bootpath;
258 size_t len;
260 bootpath = get_boot_device_path(i->dev, ignore_suffixes, i->suffix);
262 if (total) {
263 list[total-1] = '\n';
265 len = strlen(bootpath) + 1;
266 list = g_realloc(list, total + len);
267 memcpy(&list[total], bootpath, len);
268 total += len;
269 g_free(bootpath);
272 *size = total;
274 if (current_machine->boot_config.has_strict &&
275 current_machine->boot_config.strict && *size > 0) {
276 list[total-1] = '\n';
277 list = g_realloc(list, total + 5);
278 memcpy(&list[total], "HALT", 5);
279 *size = total + 5;
281 return list;
284 typedef struct {
285 int32_t *bootindex;
286 const char *suffix;
287 DeviceState *dev;
288 } BootIndexProperty;
290 static void device_get_bootindex(Object *obj, Visitor *v, const char *name,
291 void *opaque, Error **errp)
293 BootIndexProperty *prop = opaque;
294 visit_type_int32(v, name, prop->bootindex, errp);
297 static void device_set_bootindex(Object *obj, Visitor *v, const char *name,
298 void *opaque, Error **errp)
300 BootIndexProperty *prop = opaque;
301 int32_t boot_index;
302 Error *local_err = NULL;
304 if (!visit_type_int32(v, name, &boot_index, errp)) {
305 return;
307 /* check whether bootindex is present in fw_boot_order list */
308 check_boot_index(boot_index, &local_err);
309 if (local_err) {
310 error_propagate(errp, local_err);
311 return;
313 /* change bootindex to a new one */
314 *prop->bootindex = boot_index;
316 add_boot_device_path(*prop->bootindex, prop->dev, prop->suffix);
319 static void property_release_bootindex(Object *obj, const char *name,
320 void *opaque)
323 BootIndexProperty *prop = opaque;
325 del_boot_device_path(prop->dev, prop->suffix);
326 g_free(prop);
329 void device_add_bootindex_property(Object *obj, int32_t *bootindex,
330 const char *name, const char *suffix,
331 DeviceState *dev)
333 BootIndexProperty *prop = g_malloc0(sizeof(*prop));
335 prop->bootindex = bootindex;
336 prop->suffix = suffix;
337 prop->dev = dev;
339 object_property_add(obj, name, "int32",
340 device_get_bootindex,
341 device_set_bootindex,
342 property_release_bootindex,
343 prop);
345 /* initialize devices' bootindex property to -1 */
346 object_property_set_int(obj, name, -1, NULL);
349 typedef struct FWLCHSEntry FWLCHSEntry;
351 struct FWLCHSEntry {
352 QTAILQ_ENTRY(FWLCHSEntry) link;
353 DeviceState *dev;
354 char *suffix;
355 uint32_t lcyls;
356 uint32_t lheads;
357 uint32_t lsecs;
360 static QTAILQ_HEAD(, FWLCHSEntry) fw_lchs =
361 QTAILQ_HEAD_INITIALIZER(fw_lchs);
363 void add_boot_device_lchs(DeviceState *dev, const char *suffix,
364 uint32_t lcyls, uint32_t lheads, uint32_t lsecs)
366 FWLCHSEntry *node;
368 if (!lcyls && !lheads && !lsecs) {
369 return;
372 assert(dev != NULL || suffix != NULL);
374 node = g_new0(FWLCHSEntry, 1);
375 node->suffix = g_strdup(suffix);
376 node->dev = dev;
377 node->lcyls = lcyls;
378 node->lheads = lheads;
379 node->lsecs = lsecs;
381 QTAILQ_INSERT_TAIL(&fw_lchs, node, link);
384 void del_boot_device_lchs(DeviceState *dev, const char *suffix)
386 FWLCHSEntry *i;
388 if (dev == NULL) {
389 return;
392 QTAILQ_FOREACH(i, &fw_lchs, link) {
393 if ((!suffix || !g_strcmp0(i->suffix, suffix)) &&
394 i->dev == dev) {
395 QTAILQ_REMOVE(&fw_lchs, i, link);
396 g_free(i->suffix);
397 g_free(i);
399 break;
404 char *get_boot_devices_lchs_list(size_t *size)
406 FWLCHSEntry *i;
407 size_t total = 0;
408 char *list = NULL;
410 QTAILQ_FOREACH(i, &fw_lchs, link) {
411 char *bootpath;
412 char *chs_string;
413 size_t len;
415 bootpath = get_boot_device_path(i->dev, false, i->suffix);
416 chs_string = g_strdup_printf("%s %" PRIu32 " %" PRIu32 " %" PRIu32,
417 bootpath, i->lcyls, i->lheads, i->lsecs);
419 if (total) {
420 list[total - 1] = '\n';
422 len = strlen(chs_string) + 1;
423 list = g_realloc(list, total + len);
424 memcpy(&list[total], chs_string, len);
425 total += len;
426 g_free(chs_string);
427 g_free(bootpath);
430 *size = total;
432 return list;