bootdevice.c 9.3 KB
Newer Older
1 2 3
/*
 * QEMU Boot Device Implement
 *
4
 * Copyright (c) 2014 HUAWEI TECHNOLOGIES CO., LTD.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

P
Peter Maydell 已提交
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"
33 34 35 36 37 38 39 40 41 42 43 44

typedef struct FWBootEntry FWBootEntry;

struct FWBootEntry {
    QTAILQ_ENTRY(FWBootEntry) link;
    int32_t bootindex;
    DeviceState *dev;
    char *suffix;
};

static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
    QTAILQ_HEAD_INITIALIZER(fw_boot_order);
45 46 47 48 49 50 51 52 53
static QEMUBootSetHandler *boot_set_handler;
static void *boot_set_opaque;

void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
{
    boot_set_handler = func;
    boot_set_opaque = opaque;
}

54
void qemu_boot_set(const char *boot_order, Error **errp)
55
{
56 57
    Error *local_err = NULL;

58
    if (!boot_set_handler) {
59 60 61 62 63
        error_setg(errp, "no function defined to set boot device list for"
                         " this architecture");
        return;
    }

64 65 66 67 68 69
    validate_bootdevices(boot_order, &local_err);
    if (local_err) {
        error_propagate(errp, local_err);
        return;
    }

70
    boot_set_handler(boot_set_opaque, boot_order, errp);
71 72
}

73
void validate_bootdevices(const char *devices, Error **errp)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
{
    /* We just do some generic consistency checks */
    const char *p;
    int bitmap = 0;

    for (p = devices; *p != '\0'; p++) {
        /* Allowed boot devices are:
         * a-b: floppy disk drives
         * c-f: IDE disk drives
         * g-m: machine implementation dependent drives
         * n-p: network devices
         * It's up to each machine implementation to check if the given boot
         * devices match the actual hardware implementation and firmware
         * features.
         */
        if (*p < 'a' || *p > 'p') {
90 91
            error_setg(errp, "Invalid boot device '%c'", *p);
            return;
92 93
        }
        if (bitmap & (1 << (*p - 'a'))) {
94 95
            error_setg(errp, "Boot device '%c' was given twice", *p);
            return;
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        }
        bitmap |= 1 << (*p - 'a');
    }
}

void restore_boot_order(void *opaque)
{
    char *normal_boot_order = opaque;
    static int first = 1;

    /* Restore boot order and remove ourselves after the first boot */
    if (first) {
        first = 0;
        return;
    }

112 113 114
    if (boot_set_handler) {
        qemu_boot_set(normal_boot_order, &error_abort);
    }
115 116 117 118

    qemu_unregister_reset(restore_boot_order, normal_boot_order);
    g_free(normal_boot_order);
}
119

G
Gonglei 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
void check_boot_index(int32_t bootindex, Error **errp)
{
    FWBootEntry *i;

    if (bootindex >= 0) {
        QTAILQ_FOREACH(i, &fw_boot_order, link) {
            if (i->bootindex == bootindex) {
                error_setg(errp, "The bootindex %d has already been used",
                           bootindex);
                return;
            }
        }
    }
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
void del_boot_device_path(DeviceState *dev, const char *suffix)
{
    FWBootEntry *i;

    if (dev == NULL) {
        return;
    }

    QTAILQ_FOREACH(i, &fw_boot_order, link) {
        if ((!suffix || !g_strcmp0(i->suffix, suffix)) &&
             i->dev == dev) {
            QTAILQ_REMOVE(&fw_boot_order, i, link);
            g_free(i->suffix);
            g_free(i);

            break;
        }
    }
}

155 156 157 158 159 160
void add_boot_device_path(int32_t bootindex, DeviceState *dev,
                          const char *suffix)
{
    FWBootEntry *node, *i;

    if (bootindex < 0) {
161
        del_boot_device_path(dev, suffix);
162 163 164 165 166
        return;
    }

    assert(dev != NULL || suffix != NULL);

167 168
    del_boot_device_path(dev, suffix);

169 170 171 172 173 174 175
    node = g_malloc0(sizeof(FWBootEntry));
    node->bootindex = bootindex;
    node->suffix = g_strdup(suffix);
    node->dev = dev;

    QTAILQ_FOREACH(i, &fw_boot_order, link) {
        if (i->bootindex == bootindex) {
176
            error_report("Two devices with same boot index %d", bootindex);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
            exit(1);
        } else if (i->bootindex < bootindex) {
            continue;
        }
        QTAILQ_INSERT_BEFORE(i, node, link);
        return;
    }
    QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
}

DeviceState *get_boot_device(uint32_t position)
{
    uint32_t counter = 0;
    FWBootEntry *i = NULL;
    DeviceState *res = NULL;

    if (!QTAILQ_EMPTY(&fw_boot_order)) {
        QTAILQ_FOREACH(i, &fw_boot_order, link) {
            if (counter == position) {
                res = i->dev;
                break;
            }
            counter++;
        }
    }
    return res;
}

/*
 * This function returns null terminated string that consist of new line
 * separated device paths.
 *
 * memory pointed by "size" is assigned total length of the array in bytes
 *
 */
212
char *get_boot_devices_list(size_t *size)
213 214 215 216
{
    FWBootEntry *i;
    size_t total = 0;
    char *list = NULL;
217 218
    MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
    bool ignore_suffixes = mc->ignore_boot_device_suffixes;
219 220

    QTAILQ_FOREACH(i, &fw_boot_order, link) {
221 222 223
        char *devpath = NULL,  *suffix = NULL;
        char *bootpath;
        char *d;
224 225 226 227 228 229 230
        size_t len;

        if (i->dev) {
            devpath = qdev_get_fw_dev_path(i->dev);
            assert(devpath);
        }

231
        if (!ignore_suffixes) {
232 233 234 235 236 237 238 239 240
            if (i->dev) {
                d = qdev_get_own_fw_dev_path_from_handler(i->dev->parent_bus,
                                                          i->dev);
                if (d) {
                    assert(!i->suffix);
                    suffix = d;
                } else {
                    suffix = g_strdup(i->suffix);
                }
241 242 243
            } else {
                suffix = g_strdup(i->suffix);
            }
244 245
        }

246 247 248 249 250 251
        bootpath = g_strdup_printf("%s%s",
                                   devpath ? devpath : "",
                                   suffix ? suffix : "");
        g_free(devpath);
        g_free(suffix);

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        if (total) {
            list[total-1] = '\n';
        }
        len = strlen(bootpath) + 1;
        list = g_realloc(list, total + len);
        memcpy(&list[total], bootpath, len);
        total += len;
        g_free(bootpath);
    }

    *size = total;

    if (boot_strict && *size > 0) {
        list[total-1] = '\n';
        list = g_realloc(list, total + 5);
        memcpy(&list[total], "HALT", 5);
        *size = total + 5;
    }
    return list;
}
272 273 274 275 276 277 278

typedef struct {
    int32_t *bootindex;
    const char *suffix;
    DeviceState *dev;
} BootIndexProperty;

279 280
static void device_get_bootindex(Object *obj, Visitor *v, const char *name,
                                 void *opaque, Error **errp)
281 282
{
    BootIndexProperty *prop = opaque;
283
    visit_type_int32(v, name, prop->bootindex, errp);
284 285
}

286 287
static void device_set_bootindex(Object *obj, Visitor *v, const char *name,
                                 void *opaque, Error **errp)
288 289 290 291 292
{
    BootIndexProperty *prop = opaque;
    int32_t boot_index;
    Error *local_err = NULL;

293
    visit_type_int32(v, name, &boot_index, &local_err);
294 295 296 297 298 299 300 301 302 303 304
    if (local_err) {
        goto out;
    }
    /* check whether bootindex is present in fw_boot_order list  */
    check_boot_index(boot_index, &local_err);
    if (local_err) {
        goto out;
    }
    /* change bootindex to a new one */
    *prop->bootindex = boot_index;

305 306
    add_boot_device_path(*prop->bootindex, prop->dev, prop->suffix);

307
out:
308
    error_propagate(errp, local_err);
309 310 311 312 313 314 315
}

static void property_release_bootindex(Object *obj, const char *name,
                                       void *opaque)

{
    BootIndexProperty *prop = opaque;
316 317

    del_boot_device_path(prop->dev, prop->suffix);
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    g_free(prop);
}

void device_add_bootindex_property(Object *obj, int32_t *bootindex,
                                   const char *name, const char *suffix,
                                   DeviceState *dev, Error **errp)
{
    Error *local_err = NULL;
    BootIndexProperty *prop = g_malloc0(sizeof(*prop));

    prop->bootindex = bootindex;
    prop->suffix = suffix;
    prop->dev = dev;

    object_property_add(obj, name, "int32",
                        device_get_bootindex,
                        device_set_bootindex,
                        property_release_bootindex,
                        prop, &local_err);

    if (local_err) {
        error_propagate(errp, local_err);
        g_free(prop);
        return;
    }
    /* initialize devices' bootindex property to -1 */
    object_property_set_int(obj, -1, name, NULL);
}