DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)

{

    const char *buf;

    const char *file = NULL;

    char devname[128];

    const char *serial;

    const char *mediastr = "";

    BlockInterfaceType type;

    enum { MEDIA_DISK, MEDIA_CDROM } media;

    int bus_id, unit_id;

    int cyls, heads, secs, translation;

    BlockDriver *drv = NULL;

    int max_devs;

    int index;

    int ro = 0;

    int bdrv_flags = 0;

    int on_read_error, on_write_error;

    const char *devaddr;

    DriveInfo *dinfo;

    BlockIOLimit io_limits;

    int snapshot = 0;

    bool copy_on_read;

    int ret;



    translation = BIOS_ATA_TRANSLATION_AUTO;

    media = MEDIA_DISK;



    /* extract parameters */

    bus_id  = qemu_opt_get_number(opts, "bus", 0);

    unit_id = qemu_opt_get_number(opts, "unit", -1);

    index   = qemu_opt_get_number(opts, "index", -1);



    cyls  = qemu_opt_get_number(opts, "cyls", 0);

    heads = qemu_opt_get_number(opts, "heads", 0);

    secs  = qemu_opt_get_number(opts, "secs", 0);



    snapshot = qemu_opt_get_bool(opts, "snapshot", 0);

    ro = qemu_opt_get_bool(opts, "readonly", 0);

    copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);



    file = qemu_opt_get(opts, "file");

    serial = qemu_opt_get(opts, "serial");



    if ((buf = qemu_opt_get(opts, "if")) != NULL) {

        pstrcpy(devname, sizeof(devname), buf);

        for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)

            ;

        if (type == IF_COUNT) {

            error_report("unsupported bus type '%s'", buf);

            return NULL;

	}

    } else {

        type = default_to_scsi ? IF_SCSI : IF_IDE;

        pstrcpy(devname, sizeof(devname), if_name[type]);

    }



    max_devs = if_max_devs[type];



    if (cyls || heads || secs) {

        if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {

            error_report("invalid physical cyls number");

	    return NULL;

	}

        if (heads < 1 || (type == IF_IDE && heads > 16)) {

            error_report("invalid physical heads number");

	    return NULL;

	}

        if (secs < 1 || (type == IF_IDE && secs > 63)) {

            error_report("invalid physical secs number");

	    return NULL;

	}

    }



    if ((buf = qemu_opt_get(opts, "trans")) != NULL) {

        if (!cyls) {

            error_report("'%s' trans must be used with cyls, heads and secs",

                         buf);

            return NULL;

        }

        if (!strcmp(buf, "none"))

            translation = BIOS_ATA_TRANSLATION_NONE;

        else if (!strcmp(buf, "lba"))

            translation = BIOS_ATA_TRANSLATION_LBA;

        else if (!strcmp(buf, "auto"))

            translation = BIOS_ATA_TRANSLATION_AUTO;

	else {

            error_report("'%s' invalid translation type", buf);

	    return NULL;

	}

    }



    if ((buf = qemu_opt_get(opts, "media")) != NULL) {

        if (!strcmp(buf, "disk")) {

	    media = MEDIA_DISK;

	} else if (!strcmp(buf, "cdrom")) {

            if (cyls || secs || heads) {

                error_report("CHS can't be set with media=%s", buf);

	        return NULL;

            }

	    media = MEDIA_CDROM;

	} else {

	    error_report("'%s' invalid media", buf);

	    return NULL;

	}

    }



    if ((buf = qemu_opt_get(opts, "cache")) != NULL) {

        if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {

            error_report("invalid cache option");

            return NULL;

        }

    }



#ifdef CONFIG_LINUX_AIO

    if ((buf = qemu_opt_get(opts, "aio")) != NULL) {

        if (!strcmp(buf, "native")) {

            bdrv_flags |= BDRV_O_NATIVE_AIO;

        } else if (!strcmp(buf, "threads")) {

            /* this is the default */

        } else {

           error_report("invalid aio option");

           return NULL;

        }

    }

#endif



    if ((buf = qemu_opt_get(opts, "format")) != NULL) {

       if (strcmp(buf, "?") == 0) {

           error_printf("Supported formats:");

           bdrv_iterate_format(bdrv_format_print, NULL);

           error_printf("\n");

           return NULL;

        }

        drv = bdrv_find_whitelisted_format(buf);

        if (!drv) {

            error_report("'%s' invalid format", buf);

            return NULL;

        }

    }



    /* disk I/O throttling */

    io_limits.bps[BLOCK_IO_LIMIT_TOTAL]  =

                           qemu_opt_get_number(opts, "bps", 0);

    io_limits.bps[BLOCK_IO_LIMIT_READ]   =

                           qemu_opt_get_number(opts, "bps_rd", 0);

    io_limits.bps[BLOCK_IO_LIMIT_WRITE]  =

                           qemu_opt_get_number(opts, "bps_wr", 0);

    io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =

                           qemu_opt_get_number(opts, "iops", 0);

    io_limits.iops[BLOCK_IO_LIMIT_READ]  =

                           qemu_opt_get_number(opts, "iops_rd", 0);

    io_limits.iops[BLOCK_IO_LIMIT_WRITE] =

                           qemu_opt_get_number(opts, "iops_wr", 0);



    if (!do_check_io_limits(&io_limits)) {

        error_report("bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "

                     "cannot be used at the same time");

        return NULL;

    }



    on_write_error = BLOCK_ERR_STOP_ENOSPC;

    if ((buf = qemu_opt_get(opts, "werror")) != NULL) {

        if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {

            error_report("werror is not supported by this bus type");

            return NULL;

        }



        on_write_error = parse_block_error_action(buf, 0);

        if (on_write_error < 0) {

            return NULL;

        }

    }



    on_read_error = BLOCK_ERR_REPORT;

    if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {

        if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {

            error_report("rerror is not supported by this bus type");

            return NULL;

        }



        on_read_error = parse_block_error_action(buf, 1);

        if (on_read_error < 0) {

            return NULL;

        }

    }



    if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {

        if (type != IF_VIRTIO) {

            error_report("addr is not supported by this bus type");

            return NULL;

        }

    }



    /* compute bus and unit according index */



    if (index != -1) {

        if (bus_id != 0 || unit_id != -1) {

            error_report("index cannot be used with bus and unit");

            return NULL;

        }

        bus_id = drive_index_to_bus_id(type, index);

        unit_id = drive_index_to_unit_id(type, index);

    }



    /* if user doesn't specify a unit_id,

     * try to find the first free

     */



    if (unit_id == -1) {

       unit_id = 0;

       while (drive_get(type, bus_id, unit_id) != NULL) {

           unit_id++;

           if (max_devs && unit_id >= max_devs) {

               unit_id -= max_devs;

               bus_id++;

           }

       }

    }



    /* check unit id */



    if (max_devs && unit_id >= max_devs) {

        error_report("unit %d too big (max is %d)",

                     unit_id, max_devs - 1);

        return NULL;

    }



    /*

     * catch multiple definitions

     */



    if (drive_get(type, bus_id, unit_id) != NULL) {

        error_report("drive with bus=%d, unit=%d (index=%d) exists",

                     bus_id, unit_id, index);

        return NULL;

    }



    /* init */



    dinfo = g_malloc0(sizeof(*dinfo));

    if ((buf = qemu_opts_id(opts)) != NULL) {

        dinfo->id = g_strdup(buf);

    } else {

        /* no id supplied -> create one */

        dinfo->id = g_malloc0(32);

        if (type == IF_IDE || type == IF_SCSI)

            mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";

        if (max_devs)

            snprintf(dinfo->id, 32, "%s%i%s%i",

                     devname, bus_id, mediastr, unit_id);

        else

            snprintf(dinfo->id, 32, "%s%s%i",

                     devname, mediastr, unit_id);

    }

    dinfo->bdrv = bdrv_new(dinfo->id);

    dinfo->devaddr = devaddr;

    dinfo->type = type;

    dinfo->bus = bus_id;

    dinfo->unit = unit_id;

    dinfo->opts = opts;

    dinfo->refcount = 1;

    if (serial) {

        pstrcpy(dinfo->serial, sizeof(dinfo->serial), serial);

    }

    QTAILQ_INSERT_TAIL(&drives, dinfo, next);



    bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);



    /* disk I/O throttling */

    bdrv_set_io_limits(dinfo->bdrv, &io_limits);



    switch(type) {

    case IF_IDE:

    case IF_SCSI:

    case IF_XEN:

    case IF_NONE:

        switch(media) {

	case MEDIA_DISK:

            if (cyls != 0) {

                bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);

                bdrv_set_translation_hint(dinfo->bdrv, translation);

            }

	    break;

	case MEDIA_CDROM:

            dinfo->media_cd = 1;

	    break;

	}

        break;

    case IF_SD:

    case IF_FLOPPY:

    case IF_PFLASH:

    case IF_MTD:

        break;

    case IF_VIRTIO:

        /* add virtio block device */

        opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);

        if (arch_type == QEMU_ARCH_S390X) {

            qemu_opt_set(opts, "driver", "virtio-blk-s390");

        } else {

            qemu_opt_set(opts, "driver", "virtio-blk-pci");

        }

        qemu_opt_set(opts, "drive", dinfo->id);

        if (devaddr)

            qemu_opt_set(opts, "addr", devaddr);

        break;

    default:

        abort();

    }

    if (!file || !*file) {

        return dinfo;

    }

    if (snapshot) {

        /* always use cache=unsafe with snapshot */

        bdrv_flags &= ~BDRV_O_CACHE_MASK;

        bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);

    }



    if (copy_on_read) {

        bdrv_flags |= BDRV_O_COPY_ON_READ;

    }



    if (runstate_check(RUN_STATE_INMIGRATE)) {

        bdrv_flags |= BDRV_O_INCOMING;

    }



    if (media == MEDIA_CDROM) {

        /* CDROM is fine for any interface, don't check.  */

        ro = 1;

    } else if (ro == 1) {

        if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY &&

            type != IF_NONE && type != IF_PFLASH) {

            error_report("readonly not supported by this bus type");

            goto err;

        }

    }



    bdrv_flags |= ro ? 0 : BDRV_O_RDWR;



    ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);

    if (ret < 0) {

        error_report("could not open disk image %s: %s",

                     file, strerror(-ret));

        goto err;

    }



    if (bdrv_key_required(dinfo->bdrv))

        autostart = 0;

    return dinfo;



err:

    bdrv_delete(dinfo->bdrv);

    g_free(dinfo->id);

    QTAILQ_REMOVE(&drives, dinfo, next);

    g_free(dinfo);

    return NULL;

}

DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)

{

    const char *buf;

    const char *file = NULL;

    char devname[128];

    const char *serial;

    const char *mediastr = "";

    BlockInterfaceType type;

    enum { MEDIA_DISK, MEDIA_CDROM } media;

    int bus_id, unit_id;

    int cyls, heads, secs, translation;

    BlockDriver *drv = NULL;

    int max_devs;

    int index;

    int ro = 0;

    int bdrv_flags = 0;

    int on_read_error, on_write_error;

    const char *devaddr;

    DriveInfo *dinfo;

    BlockIOLimit io_limits;

    int snapshot = 0;

    bool copy_on_read;

    int ret;



    translation = BIOS_ATA_TRANSLATION_AUTO;

    media = MEDIA_DISK;



    

    bus_id  = qemu_opt_get_number(opts, "bus", 0);

    unit_id = qemu_opt_get_number(opts, "unit", -1);

    index   = qemu_opt_get_number(opts, "index", -1);



    cyls  = qemu_opt_get_number(opts, "cyls", 0);

    heads = qemu_opt_get_number(opts, "heads", 0);

    secs  = qemu_opt_get_number(opts, "secs", 0);



    snapshot = qemu_opt_get_bool(opts, "snapshot", 0);

    ro = qemu_opt_get_bool(opts, "readonly", 0);

    copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);



    file = qemu_opt_get(opts, "file");

    serial = qemu_opt_get(opts, "serial");



    if ((buf = qemu_opt_get(opts, "if")) != NULL) {

        pstrcpy(devname, sizeof(devname), buf);

        for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)

            ;

        if (type == IF_COUNT) {

            error_report("unsupported bus type '%s'", buf);

            return NULL;

	}

    } else {

        type = default_to_scsi ? IF_SCSI : IF_IDE;

        pstrcpy(devname, sizeof(devname), if_name[type]);

    }



    max_devs = if_max_devs[type];



    if (cyls || heads || secs) {

        if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {

            error_report("invalid physical cyls number");

	    return NULL;

	}

        if (heads < 1 || (type == IF_IDE && heads > 16)) {

            error_report("invalid physical heads number");

	    return NULL;

	}

        if (secs < 1 || (type == IF_IDE && secs > 63)) {

            error_report("invalid physical secs number");

	    return NULL;

	}

    }



    if ((buf = qemu_opt_get(opts, "trans")) != NULL) {

        if (!cyls) {

            error_report("'%s' trans must be used with cyls, heads and secs",

                         buf);

            return NULL;

        }

        if (!strcmp(buf, "none"))

            translation = BIOS_ATA_TRANSLATION_NONE;

        else if (!strcmp(buf, "lba"))

            translation = BIOS_ATA_TRANSLATION_LBA;

        else if (!strcmp(buf, "auto"))

            translation = BIOS_ATA_TRANSLATION_AUTO;

	else {

            error_report("'%s' invalid translation type", buf);

	    return NULL;

	}

    }



    if ((buf = qemu_opt_get(opts, "media")) != NULL) {

        if (!strcmp(buf, "disk")) {

	    media = MEDIA_DISK;

	} else if (!strcmp(buf, "cdrom")) {

            if (cyls || secs || heads) {

                error_report("CHS can't be set with media=%s", buf);

	        return NULL;

            }

	    media = MEDIA_CDROM;

	} else {

	    error_report("'%s' invalid media", buf);

	    return NULL;

	}

    }



    if ((buf = qemu_opt_get(opts, "cache")) != NULL) {

        if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {

            error_report("invalid cache option");

            return NULL;

        }

    }



#ifdef CONFIG_LINUX_AIO

    if ((buf = qemu_opt_get(opts, "aio")) != NULL) {

        if (!strcmp(buf, "native")) {

            bdrv_flags |= BDRV_O_NATIVE_AIO;

        } else if (!strcmp(buf, "threads")) {

            

        } else {

           error_report("invalid aio option");

           return NULL;

        }

    }

#endif



    if ((buf = qemu_opt_get(opts, "format")) != NULL) {

       if (strcmp(buf, "?") == 0) {

           error_printf("Supported formats:");

           bdrv_iterate_format(bdrv_format_print, NULL);

           error_printf("\n");

           return NULL;

        }

        drv = bdrv_find_whitelisted_format(buf);

        if (!drv) {

            error_report("'%s' invalid format", buf);

            return NULL;

        }

    }



    

    io_limits.bps[BLOCK_IO_LIMIT_TOTAL]  =

                           qemu_opt_get_number(opts, "bps", 0);

    io_limits.bps[BLOCK_IO_LIMIT_READ]   =

                           qemu_opt_get_number(opts, "bps_rd", 0);

    io_limits.bps[BLOCK_IO_LIMIT_WRITE]  =

                           qemu_opt_get_number(opts, "bps_wr", 0);

    io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =

                           qemu_opt_get_number(opts, "iops", 0);

    io_limits.iops[BLOCK_IO_LIMIT_READ]  =

                           qemu_opt_get_number(opts, "iops_rd", 0);

    io_limits.iops[BLOCK_IO_LIMIT_WRITE] =

                           qemu_opt_get_number(opts, "iops_wr", 0);



    if (!do_check_io_limits(&io_limits)) {

        error_report("bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "

                     "cannot be used at the same time");

        return NULL;

    }



    on_write_error = BLOCK_ERR_STOP_ENOSPC;

    if ((buf = qemu_opt_get(opts, "werror")) != NULL) {

        if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {

            error_report("werror is not supported by this bus type");

            return NULL;

        }



        on_write_error = parse_block_error_action(buf, 0);

        if (on_write_error < 0) {

            return NULL;

        }

    }



    on_read_error = BLOCK_ERR_REPORT;

    if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {

        if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {

            error_report("rerror is not supported by this bus type");

            return NULL;

        }



        on_read_error = parse_block_error_action(buf, 1);

        if (on_read_error < 0) {

            return NULL;

        }

    }



    if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {

        if (type != IF_VIRTIO) {

            error_report("addr is not supported by this bus type");

            return NULL;

        }

    }



    



    if (index != -1) {

        if (bus_id != 0 || unit_id != -1) {

            error_report("index cannot be used with bus and unit");

            return NULL;

        }

        bus_id = drive_index_to_bus_id(type, index);

        unit_id = drive_index_to_unit_id(type, index);

    }



    







    if (unit_id == -1) {

       unit_id = 0;

       while (drive_get(type, bus_id, unit_id) != NULL) {

           unit_id++;

           if (max_devs && unit_id >= max_devs) {

               unit_id -= max_devs;

               bus_id++;

           }

       }

    }



    



    if (max_devs && unit_id >= max_devs) {

        error_report("unit %d too big (max is %d)",

                     unit_id, max_devs - 1);

        return NULL;

    }



    







    if (drive_get(type, bus_id, unit_id) != NULL) {

        error_report("drive with bus=%d, unit=%d (index=%d) exists",

                     bus_id, unit_id, index);

        return NULL;

    }



    



    dinfo = g_malloc0(sizeof(*dinfo));

    if ((buf = qemu_opts_id(opts)) != NULL) {

        dinfo->id = g_strdup(buf);

    } else {

        

        dinfo->id = g_malloc0(32);

        if (type == IF_IDE || type == IF_SCSI)

            mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";

        if (max_devs)

            snprintf(dinfo->id, 32, "%s%i%s%i",

                     devname, bus_id, mediastr, unit_id);

        else

            snprintf(dinfo->id, 32, "%s%s%i",

                     devname, mediastr, unit_id);

    }

    dinfo->bdrv = bdrv_new(dinfo->id);

    dinfo->devaddr = devaddr;

    dinfo->type = type;

    dinfo->bus = bus_id;

    dinfo->unit = unit_id;

    dinfo->opts = opts;

    dinfo->refcount = 1;

    if (serial) {

        pstrcpy(dinfo->serial, sizeof(dinfo->serial), serial);

    }

    QTAILQ_INSERT_TAIL(&drives, dinfo, next);



    bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);



    

    bdrv_set_io_limits(dinfo->bdrv, &io_limits);



    switch(type) {

    case IF_IDE:

    case IF_SCSI:

    case IF_XEN:

    case IF_NONE:

        switch(media) {

	case MEDIA_DISK:

            if (cyls != 0) {

                bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);

                bdrv_set_translation_hint(dinfo->bdrv, translation);

            }

	    break;

	case MEDIA_CDROM:

            dinfo->media_cd = 1;

	    break;

	}

        break;

    case IF_SD:

    case IF_FLOPPY:

    case IF_PFLASH:

    case IF_MTD:

        break;

    case IF_VIRTIO:

        

        opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);

        if (arch_type == QEMU_ARCH_S390X) {

            qemu_opt_set(opts, "driver", "virtio-blk-s390");

        } else {

            qemu_opt_set(opts, "driver", "virtio-blk-pci");

        }

        qemu_opt_set(opts, "drive", dinfo->id);

        if (devaddr)

            qemu_opt_set(opts, "addr", devaddr);

        break;

    default:

        abort();

    }

    if (!file || !*file) {

        return dinfo;

    }

    if (snapshot) {

        

        bdrv_flags &= ~BDRV_O_CACHE_MASK;

        bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);

    }



    if (copy_on_read) {

        bdrv_flags |= BDRV_O_COPY_ON_READ;

    }



    if (runstate_check(RUN_STATE_INMIGRATE)) {

        bdrv_flags |= BDRV_O_INCOMING;

    }



    if (media == MEDIA_CDROM) {

        

        ro = 1;

    } else if (ro == 1) {

        if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY &&

            type != IF_NONE && type != IF_PFLASH) {

            error_report("readonly not supported by this bus type");

            goto err;

        }

    }



    bdrv_flags |= ro ? 0 : BDRV_O_RDWR;



    ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);

    if (ret < 0) {

        error_report("could not open disk image %s: %s",

                     file, strerror(-ret));

        goto err;

    }



    if (bdrv_key_required(dinfo->bdrv))

        autostart = 0;

    return dinfo;



err:

    bdrv_delete(dinfo->bdrv);

    g_free(dinfo->id);

    QTAILQ_REMOVE(&drives, dinfo, next);

    g_free(dinfo);

    return NULL;

}

        opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
DriveInfo *FUNC_0(QemuOpts *opts, int default_to_scsi)

{

    const char *VAR_0;

    const char *VAR_1 = NULL;

    char VAR_2[128];

    const char *VAR_3;

    const char *VAR_4 = "";

    BlockInterfaceType type;

    enum { MEDIA_DISK, MEDIA_CDROM } VAR_5;

    int VAR_6, VAR_7;

    int VAR_8, VAR_9, VAR_10, VAR_11;

    BlockDriver *drv = NULL;

    int VAR_12;

    int VAR_13;

    int VAR_14 = 0;

    int VAR_15 = 0;

    int VAR_16, VAR_17;

    const char *VAR_18;

    DriveInfo *dinfo;

    BlockIOLimit io_limits;

    int VAR_19 = 0;

    bool copy_on_read;

    int VAR_20;



    VAR_11 = BIOS_ATA_TRANSLATION_AUTO;

    VAR_5 = MEDIA_DISK;



    

    VAR_6  = qemu_opt_get_number(opts, "bus", 0);

    VAR_7 = qemu_opt_get_number(opts, "unit", -1);

    VAR_13   = qemu_opt_get_number(opts, "VAR_13", -1);



    VAR_8  = qemu_opt_get_number(opts, "VAR_8", 0);

    VAR_9 = qemu_opt_get_number(opts, "VAR_9", 0);

    VAR_10  = qemu_opt_get_number(opts, "VAR_10", 0);



    VAR_19 = qemu_opt_get_bool(opts, "VAR_19", 0);

    VAR_14 = qemu_opt_get_bool(opts, "readonly", 0);

    copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);



    VAR_1 = qemu_opt_get(opts, "VAR_1");

    VAR_3 = qemu_opt_get(opts, "VAR_3");



    if ((VAR_0 = qemu_opt_get(opts, "if")) != NULL) {

        pstrcpy(VAR_2, sizeof(VAR_2), VAR_0);

        for (type = 0; type < IF_COUNT && strcmp(VAR_0, if_name[type]); type++)

            ;

        if (type == IF_COUNT) {

            error_report("unsupported bus type '%s'", VAR_0);

            return NULL;

	}

    } else {

        type = default_to_scsi ? IF_SCSI : IF_IDE;

        pstrcpy(VAR_2, sizeof(VAR_2), if_name[type]);

    }



    VAR_12 = if_max_devs[type];



    if (VAR_8 || VAR_9 || VAR_10) {

        if (VAR_8 < 1 || (type == IF_IDE && VAR_8 > 16383)) {

            error_report("invalid physical VAR_8 number");

	    return NULL;

	}

        if (VAR_9 < 1 || (type == IF_IDE && VAR_9 > 16)) {

            error_report("invalid physical VAR_9 number");

	    return NULL;

	}

        if (VAR_10 < 1 || (type == IF_IDE && VAR_10 > 63)) {

            error_report("invalid physical VAR_10 number");

	    return NULL;

	}

    }



    if ((VAR_0 = qemu_opt_get(opts, "trans")) != NULL) {

        if (!VAR_8) {

            error_report("'%s' trans must be used with VAR_8, VAR_9 and VAR_10",

                         VAR_0);

            return NULL;

        }

        if (!strcmp(VAR_0, "none"))

            VAR_11 = BIOS_ATA_TRANSLATION_NONE;

        else if (!strcmp(VAR_0, "lba"))

            VAR_11 = BIOS_ATA_TRANSLATION_LBA;

        else if (!strcmp(VAR_0, "auto"))

            VAR_11 = BIOS_ATA_TRANSLATION_AUTO;

	else {

            error_report("'%s' invalid VAR_11 type", VAR_0);

	    return NULL;

	}

    }



    if ((VAR_0 = qemu_opt_get(opts, "VAR_5")) != NULL) {

        if (!strcmp(VAR_0, "disk")) {

	    VAR_5 = MEDIA_DISK;

	} else if (!strcmp(VAR_0, "cdrom")) {

            if (VAR_8 || VAR_10 || VAR_9) {

                error_report("CHS can't be set with VAR_5=%s", VAR_0);

	        return NULL;

            }

	    VAR_5 = MEDIA_CDROM;

	} else {

	    error_report("'%s' invalid VAR_5", VAR_0);

	    return NULL;

	}

    }



    if ((VAR_0 = qemu_opt_get(opts, "cache")) != NULL) {

        if (bdrv_parse_cache_flags(VAR_0, &VAR_15) != 0) {

            error_report("invalid cache option");

            return NULL;

        }

    }



#ifdef CONFIG_LINUX_AIO

    if ((VAR_0 = qemu_opt_get(opts, "aio")) != NULL) {

        if (!strcmp(VAR_0, "native")) {

            VAR_15 |= BDRV_O_NATIVE_AIO;

        } else if (!strcmp(VAR_0, "threads")) {

            

        } else {

           error_report("invalid aio option");

           return NULL;

        }

    }

#endif



    if ((VAR_0 = qemu_opt_get(opts, "format")) != NULL) {

       if (strcmp(VAR_0, "?") == 0) {

           error_printf("Supported formats:");

           bdrv_iterate_format(bdrv_format_print, NULL);

           error_printf("\n");

           return NULL;

        }

        drv = bdrv_find_whitelisted_format(VAR_0);

        if (!drv) {

            error_report("'%s' invalid format", VAR_0);

            return NULL;

        }

    }



    

    io_limits.bps[BLOCK_IO_LIMIT_TOTAL]  =

                           qemu_opt_get_number(opts, "bps", 0);

    io_limits.bps[BLOCK_IO_LIMIT_READ]   =

                           qemu_opt_get_number(opts, "bps_rd", 0);

    io_limits.bps[BLOCK_IO_LIMIT_WRITE]  =

                           qemu_opt_get_number(opts, "bps_wr", 0);

    io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =

                           qemu_opt_get_number(opts, "iops", 0);

    io_limits.iops[BLOCK_IO_LIMIT_READ]  =

                           qemu_opt_get_number(opts, "iops_rd", 0);

    io_limits.iops[BLOCK_IO_LIMIT_WRITE] =

                           qemu_opt_get_number(opts, "iops_wr", 0);



    if (!do_check_io_limits(&io_limits)) {

        error_report("bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "

                     "cannot be used at the same time");

        return NULL;

    }



    VAR_17 = BLOCK_ERR_STOP_ENOSPC;

    if ((VAR_0 = qemu_opt_get(opts, "werror")) != NULL) {

        if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {

            error_report("werror is not supported by this bus type");

            return NULL;

        }



        VAR_17 = parse_block_error_action(VAR_0, 0);

        if (VAR_17 < 0) {

            return NULL;

        }

    }



    VAR_16 = BLOCK_ERR_REPORT;

    if ((VAR_0 = qemu_opt_get(opts, "rerror")) != NULL) {

        if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {

            error_report("rerror is not supported by this bus type");

            return NULL;

        }



        VAR_16 = parse_block_error_action(VAR_0, 1);

        if (VAR_16 < 0) {

            return NULL;

        }

    }



    if ((VAR_18 = qemu_opt_get(opts, "addr")) != NULL) {

        if (type != IF_VIRTIO) {

            error_report("addr is not supported by this bus type");

            return NULL;

        }

    }



    



    if (VAR_13 != -1) {

        if (VAR_6 != 0 || VAR_7 != -1) {

            error_report("VAR_13 cannot be used with bus and unit");

            return NULL;

        }

        VAR_6 = drive_index_to_bus_id(type, VAR_13);

        VAR_7 = drive_index_to_unit_id(type, VAR_13);

    }



    







    if (VAR_7 == -1) {

       VAR_7 = 0;

       while (drive_get(type, VAR_6, VAR_7) != NULL) {

           VAR_7++;

           if (VAR_12 && VAR_7 >= VAR_12) {

               VAR_7 -= VAR_12;

               VAR_6++;

           }

       }

    }



    



    if (VAR_12 && VAR_7 >= VAR_12) {

        error_report("unit %d too big (max is %d)",

                     VAR_7, VAR_12 - 1);

        return NULL;

    }



    







    if (drive_get(type, VAR_6, VAR_7) != NULL) {

        error_report("drive with bus=%d, unit=%d (VAR_13=%d) exists",

                     VAR_6, VAR_7, VAR_13);

        return NULL;

    }



    



    dinfo = g_malloc0(sizeof(*dinfo));

    if ((VAR_0 = qemu_opts_id(opts)) != NULL) {

        dinfo->id = g_strdup(VAR_0);

    } else {

        

        dinfo->id = g_malloc0(32);

        if (type == IF_IDE || type == IF_SCSI)

            VAR_4 = (VAR_5 == MEDIA_CDROM) ? "-cd" : "-hd";

        if (VAR_12)

            snprintf(dinfo->id, 32, "%s%i%s%i",

                     VAR_2, VAR_6, VAR_4, VAR_7);

        else

            snprintf(dinfo->id, 32, "%s%s%i",

                     VAR_2, VAR_4, VAR_7);

    }

    dinfo->bdrv = bdrv_new(dinfo->id);

    dinfo->VAR_18 = VAR_18;

    dinfo->type = type;

    dinfo->bus = VAR_6;

    dinfo->unit = VAR_7;

    dinfo->opts = opts;

    dinfo->refcount = 1;

    if (VAR_3) {

        pstrcpy(dinfo->VAR_3, sizeof(dinfo->VAR_3), VAR_3);

    }

    QTAILQ_INSERT_TAIL(&drives, dinfo, next);



    bdrv_set_on_error(dinfo->bdrv, VAR_16, VAR_17);



    

    bdrv_set_io_limits(dinfo->bdrv, &io_limits);



    switch(type) {

    case IF_IDE:

    case IF_SCSI:

    case IF_XEN:

    case IF_NONE:

        switch(VAR_5) {

	case MEDIA_DISK:

            if (VAR_8 != 0) {

                bdrv_set_geometry_hint(dinfo->bdrv, VAR_8, VAR_9, VAR_10);

                bdrv_set_translation_hint(dinfo->bdrv, VAR_11);

            }

	    break;

	case MEDIA_CDROM:

            dinfo->media_cd = 1;

	    break;

	}

        break;

    case IF_SD:

    case IF_FLOPPY:

    case IF_PFLASH:

    case IF_MTD:

        break;

    case IF_VIRTIO:

        

        opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);

        if (arch_type == QEMU_ARCH_S390X) {

            qemu_opt_set(opts, "driver", "virtio-blk-s390");

        } else {

            qemu_opt_set(opts, "driver", "virtio-blk-pci");

        }

        qemu_opt_set(opts, "drive", dinfo->id);

        if (VAR_18)

            qemu_opt_set(opts, "addr", VAR_18);

        break;

    default:

        abort();

    }

    if (!VAR_1 || !*VAR_1) {

        return dinfo;

    }

    if (VAR_19) {

        

        VAR_15 &= ~BDRV_O_CACHE_MASK;

        VAR_15 |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);

    }



    if (copy_on_read) {

        VAR_15 |= BDRV_O_COPY_ON_READ;

    }



    if (runstate_check(RUN_STATE_INMIGRATE)) {

        VAR_15 |= BDRV_O_INCOMING;

    }



    if (VAR_5 == MEDIA_CDROM) {

        

        VAR_14 = 1;

    } else if (VAR_14 == 1) {

        if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY &&

            type != IF_NONE && type != IF_PFLASH) {

            error_report("readonly not supported by this bus type");

            goto err;

        }

    }



    VAR_15 |= VAR_14 ? 0 : BDRV_O_RDWR;



    VAR_20 = bdrv_open(dinfo->bdrv, VAR_1, VAR_15, drv);

    if (VAR_20 < 0) {

        error_report("could not open disk image %s: %s",

                     VAR_1, strerror(-VAR_20));

        goto err;

    }



    if (bdrv_key_required(dinfo->bdrv))

        autostart = 0;

    return dinfo;



err:

    bdrv_delete(dinfo->bdrv);

    g_free(dinfo->id);

    QTAILQ_REMOVE(&drives, dinfo, next);

    g_free(dinfo);

    return NULL;

}

DriveInfo *FUNC_0(QemuOpts *opts, int default_to_scsi)
{ const char *VAR_0; const char *VAR_1 = NULL; char VAR_2[128]; const char *VAR_3; const char *VAR_4 = ""; BlockInterfaceType type; enum { MEDIA_DISK, MEDIA_CDROM } VAR_5; int VAR_6, VAR_7; int VAR_8, VAR_9, VAR_10, VAR_11; BlockDriver *drv = NULL; int VAR_12; int VAR_13; int VAR_14 = 0; int VAR_15 = 0; int VAR_16, VAR_17; const char *VAR_18; DriveInfo *dinfo; BlockIOLimit io_limits; int VAR_19 = 0; bool copy_on_read; int VAR_20; VAR_11 = BIOS_ATA_TRANSLATION_AUTO; VAR_5 = MEDIA_DISK; VAR_6  = qemu_opt_get_number(opts, "bus", 0); VAR_7 = qemu_opt_get_number(opts, "unit", -1); VAR_13   = qemu_opt_get_number(opts, "VAR_13", -1); VAR_8  = qemu_opt_get_number(opts, "VAR_8", 0); VAR_9 = qemu_opt_get_number(opts, "VAR_9", 0); VAR_10  = qemu_opt_get_number(opts, "VAR_10", 0); VAR_19 = qemu_opt_get_bool(opts, "VAR_19", 0); VAR_14 = qemu_opt_get_bool(opts, "readonly", 0); copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false); VAR_1 = qemu_opt_get(opts, "VAR_1"); VAR_3 = qemu_opt_get(opts, "VAR_3"); if ((VAR_0 = qemu_opt_get(opts, "if")) != NULL) { pstrcpy(VAR_2, sizeof(VAR_2), VAR_0); for (type = 0; type < IF_COUNT && strcmp(VAR_0, if_name[type]); type++) ; if (type == IF_COUNT) { error_report("unsupported bus type '%s'", VAR_0); return NULL; } } else { type = default_to_scsi ? IF_SCSI : IF_IDE; pstrcpy(VAR_2, sizeof(VAR_2), if_name[type]); } VAR_12 = if_max_devs[type]; if (VAR_8 || VAR_9 || VAR_10) { if (VAR_8 < 1 || (type == IF_IDE && VAR_8 > 16383)) { error_report("invalid physical VAR_8 number"); return NULL; } if (VAR_9 < 1 || (type == IF_IDE && VAR_9 > 16)) { error_report("invalid physical VAR_9 number"); return NULL; } if (VAR_10 < 1 || (type == IF_IDE && VAR_10 > 63)) { error_report("invalid physical VAR_10 number"); return NULL; } } if ((VAR_0 = qemu_opt_get(opts, "trans")) != NULL) { if (!VAR_8) { error_report("'%s' trans must be used with VAR_8, VAR_9 and VAR_10",
VAR_0); return NULL; } if (!strcmp(VAR_0, "none"))
VAR_11 = BIOS_ATA_TRANSLATION_NONE; else if (!strcmp(VAR_0, "lba"))
VAR_11 = BIOS_ATA_TRANSLATION_LBA; else if (!strcmp(VAR_0, "auto"))
VAR_11 = BIOS_ATA_TRANSLATION_AUTO; else { error_report("'%s' invalid VAR_11 type", VAR_0); return NULL; } } if ((VAR_0 = qemu_opt_get(opts, "VAR_5")) != NULL) { if (!strcmp(VAR_0, "disk")) { VAR_5 = MEDIA_DISK; } else if (!strcmp(VAR_0, "cdrom")) { if (VAR_8 || VAR_10 || VAR_9) { error_report("CHS can't be set with VAR_5=%s", VAR_0); return NULL; } VAR_5 = MEDIA_CDROM; } else { error_report("'%s' invalid VAR_5", VAR_0); return NULL; } } if ((VAR_0 = qemu_opt_get(opts, "cache")) != NULL) { if (bdrv_parse_cache_flags(VAR_0, &VAR_15) != 0) { error_report("invalid cache option"); return NULL; } } #ifdef CONFIG_LINUX_AIO
if ((VAR_0 = qemu_opt_get(opts, "aio")) != NULL) { if (!strcmp(VAR_0, "native")) { VAR_15 |= BDRV_O_NATIVE_AIO; } else if (!strcmp(VAR_0, "threads")) { } else { error_report("invalid aio option"); return NULL; } } #endif
if ((VAR_0 = qemu_opt_get(opts, "format")) != NULL) { if (strcmp(VAR_0, "?") == 0) { error_printf("Supported formats:"); bdrv_iterate_format(bdrv_format_print, NULL); error_printf("\n"); return NULL; } drv = bdrv_find_whitelisted_format(VAR_0); if (!drv) { error_report("'%s' invalid format", VAR_0); return NULL; } } io_limits.bps[BLOCK_IO_LIMIT_TOTAL]  =
qemu_opt_get_number(opts, "bps", 0); io_limits.bps[BLOCK_IO_LIMIT_READ]   =
qemu_opt_get_number(opts, "bps_rd", 0); io_limits.bps[BLOCK_IO_LIMIT_WRITE]  =
qemu_opt_get_number(opts, "bps_wr", 0); io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =
qemu_opt_get_number(opts, "iops", 0); io_limits.iops[BLOCK_IO_LIMIT_READ]  =
qemu_opt_get_number(opts, "iops_rd", 0); io_limits.iops[BLOCK_IO_LIMIT_WRITE] =
qemu_opt_get_number(opts, "iops_wr", 0); if (!do_check_io_limits(&io_limits)) { error_report("bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
"cannot be used at the same time"); return NULL; } VAR_17 = BLOCK_ERR_STOP_ENOSPC; if ((VAR_0 = qemu_opt_get(opts, "werror")) != NULL) { if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) { error_report("werror is not supported by this bus type"); return NULL; } VAR_17 = parse_block_error_action(VAR_0, 0); if (VAR_17 < 0) { return NULL; } } VAR_16 = BLOCK_ERR_REPORT; if ((VAR_0 = qemu_opt_get(opts, "rerror")) != NULL) { if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) { error_report("rerror is not supported by this bus type"); return NULL; } VAR_16 = parse_block_error_action(VAR_0, 1); if (VAR_16 < 0) { return NULL; } } if ((VAR_18 = qemu_opt_get(opts, "addr")) != NULL) { if (type != IF_VIRTIO) { error_report("addr is not supported by this bus type"); return NULL; } } if (VAR_13 != -1) { if (VAR_6 != 0 || VAR_7 != -1) { error_report("VAR_13 cannot be used with bus and unit"); return NULL; } VAR_6 = drive_index_to_bus_id(type, VAR_13); VAR_7 = drive_index_to_unit_id(type, VAR_13); } if (VAR_7 == -1) { VAR_7 = 0; while (drive_get(type, VAR_6, VAR_7) != NULL) { VAR_7++; if (VAR_12 && VAR_7 >= VAR_12) { VAR_7 -= VAR_12; VAR_6++; } } } if (VAR_12 && VAR_7 >= VAR_12) { error_report("unit %d too big (max is %d)",
VAR_7, VAR_12 - 1); return NULL; } if (drive_get(type, VAR_6, VAR_7) != NULL) { error_report("drive with bus=%d, unit=%d (VAR_13=%d) exists",
VAR_6, VAR_7, VAR_13); return NULL; } dinfo = g_malloc0(sizeof(*dinfo)); if ((VAR_0 = qemu_opts_id(opts)) != NULL) { dinfo->id = g_strdup(VAR_0); } else { dinfo->id = g_malloc0(32); if (type == IF_IDE || type == IF_SCSI)
VAR_4 = (VAR_5 == MEDIA_CDROM) ? "-cd" : "-hd"; if (VAR_12)
snprintf(dinfo->id, 32, "%s%i%s%i",
VAR_2, VAR_6, VAR_4, VAR_7); else
snprintf(dinfo->id, 32, "%s%s%i",
VAR_2, VAR_4, VAR_7); } dinfo->bdrv = bdrv_new(dinfo->id); dinfo->VAR_18 = VAR_18; dinfo->type = type; dinfo->bus = VAR_6; dinfo->unit = VAR_7; dinfo->opts = opts; dinfo->refcount = 1; if (VAR_3) { pstrcpy(dinfo->VAR_3, sizeof(dinfo->VAR_3), VAR_3); } QTAILQ_INSERT_TAIL(&drives, dinfo, next); bdrv_set_on_error(dinfo->bdrv, VAR_16, VAR_17); bdrv_set_io_limits(dinfo->bdrv, &io_limits); switch(type) { case IF_IDE:
case IF_SCSI:
case IF_XEN:
case IF_NONE:
switch(VAR_5) { case MEDIA_DISK:
if (VAR_8 != 0) { bdrv_set_geometry_hint(dinfo->bdrv, VAR_8, VAR_9, VAR_10); bdrv_set_translation_hint(dinfo->bdrv, VAR_11); } break; case MEDIA_CDROM:
dinfo->media_cd = 1; break; } break; case IF_SD:
case IF_FLOPPY:
case IF_PFLASH:
case IF_MTD:
break; case IF_VIRTIO:
opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0); if (arch_type == QEMU_ARCH_S390X) { qemu_opt_set(opts, "driver", "virtio-blk-s390"); } else { qemu_opt_set(opts, "driver", "virtio-blk-pci"); } qemu_opt_set(opts, "drive", dinfo->id); if (VAR_18)
qemu_opt_set(opts, "addr", VAR_18); break; default:
abort(); } if (!VAR_1 || !*VAR_1) { return dinfo; } if (VAR_19) { VAR_15 &= ~BDRV_O_CACHE_MASK; VAR_15 |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH); } if (copy_on_read) { VAR_15 |= BDRV_O_COPY_ON_READ; } if (runstate_check(RUN_STATE_INMIGRATE)) { VAR_15 |= BDRV_O_INCOMING; } if (VAR_5 == MEDIA_CDROM) { VAR_14 = 1; } else if (VAR_14 == 1) { if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY &&
type != IF_NONE && type != IF_PFLASH) { error_report("readonly not supported by this bus type"); goto err; } } VAR_15 |= VAR_14 ? 0 : BDRV_O_RDWR; VAR_20 = bdrv_open(dinfo->bdrv, VAR_1, VAR_15, drv); if (VAR_20 < 0) { error_report("could not open disk image %s: %s",
VAR_1, strerror(-VAR_20)); goto err; } if (bdrv_key_required(dinfo->bdrv))
autostart = 0; return dinfo; err:
bdrv_delete(dinfo->bdrv); g_free(dinfo->id); QTAILQ_REMOVE(&drives, dinfo, next); g_free(dinfo); return NULL; }