int drive_init(struct drive_opt *arg, int snapshot, void *opaque)

{

    char buf[128];

    char file[1024];

    char devname[128];

    char serial[21];

    const char *mediastr = "";

    BlockInterfaceType type;

    enum { MEDIA_DISK, MEDIA_CDROM } media;

    int bus_id, unit_id;

    int cyls, heads, secs, translation;

    BlockDriverState *bdrv;

    BlockDriver *drv = NULL;

    QEMUMachine *machine = opaque;

    int max_devs;

    int index;

    int cache;

    int bdrv_flags, onerror;

    int drives_table_idx;

    char *str = arg->opt;

    static const char * const params[] = { "bus", "unit", "if", "index",

                                           "cyls", "heads", "secs", "trans",

                                           "media", "snapshot", "file",

                                           "cache", "format", "serial", "werror",

                                           NULL };



    if (check_params(buf, sizeof(buf), params, str) < 0) {

         fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",

                         buf, str);

         return -1;

    }



    file[0] = 0;

    cyls = heads = secs = 0;

    bus_id = 0;

    unit_id = -1;

    translation = BIOS_ATA_TRANSLATION_AUTO;

    index = -1;

    cache = 3;



    if (machine->use_scsi) {

        type = IF_SCSI;

        max_devs = MAX_SCSI_DEVS;

        pstrcpy(devname, sizeof(devname), "scsi");

    } else {

        type = IF_IDE;

        max_devs = MAX_IDE_DEVS;

        pstrcpy(devname, sizeof(devname), "ide");

    }

    media = MEDIA_DISK;



    /* extract parameters */



    if (get_param_value(buf, sizeof(buf), "bus", str)) {

        bus_id = strtol(buf, NULL, 0);

	if (bus_id < 0) {

	    fprintf(stderr, "qemu: '%s' invalid bus id\n", str);

	    return -1;

	}

    }



    if (get_param_value(buf, sizeof(buf), "unit", str)) {

        unit_id = strtol(buf, NULL, 0);

	if (unit_id < 0) {

	    fprintf(stderr, "qemu: '%s' invalid unit id\n", str);

	    return -1;

	}

    }



    if (get_param_value(buf, sizeof(buf), "if", str)) {

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

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

	    type = IF_IDE;

            max_devs = MAX_IDE_DEVS;

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

	    type = IF_SCSI;

            max_devs = MAX_SCSI_DEVS;

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

	    type = IF_FLOPPY;

            max_devs = 0;

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

	    type = IF_PFLASH;

            max_devs = 0;

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

	    type = IF_MTD;

            max_devs = 0;

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

	    type = IF_SD;

            max_devs = 0;

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

            type = IF_VIRTIO;

            max_devs = 0;

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

	    type = IF_XEN;

            max_devs = 0;

	} else {

            fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);

            return -1;

	}

    }



    if (get_param_value(buf, sizeof(buf), "index", str)) {

        index = strtol(buf, NULL, 0);

	if (index < 0) {

	    fprintf(stderr, "qemu: '%s' invalid index\n", str);

	    return -1;

	}

    }



    if (get_param_value(buf, sizeof(buf), "cyls", str)) {

        cyls = strtol(buf, NULL, 0);

    }



    if (get_param_value(buf, sizeof(buf), "heads", str)) {

        heads = strtol(buf, NULL, 0);

    }



    if (get_param_value(buf, sizeof(buf), "secs", str)) {

        secs = strtol(buf, NULL, 0);

    }



    if (cyls || heads || secs) {

        if (cyls < 1 || cyls > 16383) {

            fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);

	    return -1;

	}

        if (heads < 1 || heads > 16) {

            fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);

	    return -1;

	}

        if (secs < 1 || secs > 63) {

            fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);

	    return -1;

	}

    }



    if (get_param_value(buf, sizeof(buf), "trans", str)) {

        if (!cyls) {

            fprintf(stderr,

                    "qemu: '%s' trans must be used with cyls,heads and secs\n",

                    str);

            return -1;

        }

        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 {

            fprintf(stderr, "qemu: '%s' invalid translation type\n", str);

	    return -1;

	}

    }



    if (get_param_value(buf, sizeof(buf), "media", str)) {

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

	    media = MEDIA_DISK;

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

            if (cyls || secs || heads) {

                fprintf(stderr,

                        "qemu: '%s' invalid physical CHS format\n", str);

	        return -1;

            }

	    media = MEDIA_CDROM;

	} else {

	    fprintf(stderr, "qemu: '%s' invalid media\n", str);

	    return -1;

	}

    }



    if (get_param_value(buf, sizeof(buf), "snapshot", str)) {

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

	    snapshot = 1;

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

	    snapshot = 0;

	else {

	    fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);

	    return -1;

	}

    }



    if (get_param_value(buf, sizeof(buf), "cache", str)) {

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

            cache = 0;

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

            cache = 1;

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

            cache = 2;

        else {

           fprintf(stderr, "qemu: invalid cache option\n");

           return -1;

        }

    }



    if (get_param_value(buf, sizeof(buf), "format", str)) {

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

            fprintf(stderr, "qemu: Supported formats:");

            bdrv_iterate_format(bdrv_format_print, NULL);

            fprintf(stderr, "\n");

	    return -1;

        }

        drv = bdrv_find_format(buf);

        if (!drv) {

            fprintf(stderr, "qemu: '%s' invalid format\n", buf);

            return -1;

        }

    }



    if (arg->file == NULL)

        get_param_value(file, sizeof(file), "file", str);

    else

        pstrcpy(file, sizeof(file), arg->file);



    if (!get_param_value(serial, sizeof(serial), "serial", str))

	    memset(serial, 0,  sizeof(serial));



    onerror = BLOCK_ERR_STOP_ENOSPC;

    if (get_param_value(buf, sizeof(serial), "werror", str)) {

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

            fprintf(stderr, "werror is no supported by this format\n");

            return -1;

        }

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

            onerror = BLOCK_ERR_IGNORE;

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

            onerror = BLOCK_ERR_STOP_ENOSPC;

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

            onerror = BLOCK_ERR_STOP_ANY;

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

            onerror = BLOCK_ERR_REPORT;

        else {

            fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);

            return -1;

        }

    }



    /* compute bus and unit according index */



    if (index != -1) {

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

            fprintf(stderr,

                    "qemu: '%s' index cannot be used with bus and unit\n", str);

            return -1;

        }

        if (max_devs == 0)

        {

            unit_id = index;

            bus_id = 0;

        } else {

            unit_id = index % max_devs;

            bus_id = index / max_devs;

        }

    }



    /* if user doesn't specify a unit_id,

     * try to find the first free

     */



    if (unit_id == -1) {

       unit_id = 0;

       while (drive_get_index(type, bus_id, unit_id) != -1) {

           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) {

        fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",

                        str, unit_id, max_devs - 1);

        return -1;

    }



    /*

     * ignore multiple definitions

     */



    if (drive_get_index(type, bus_id, unit_id) != -1)

        return -2;



    /* init */



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

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

    if (max_devs)

        snprintf(buf, sizeof(buf), "%s%i%s%i",

                 devname, bus_id, mediastr, unit_id);

    else

        snprintf(buf, sizeof(buf), "%s%s%i",

                 devname, mediastr, unit_id);

    bdrv = bdrv_new(buf);

    drives_table_idx = drive_get_free_idx();

    drives_table[drives_table_idx].bdrv = bdrv;

    drives_table[drives_table_idx].type = type;

    drives_table[drives_table_idx].bus = bus_id;

    drives_table[drives_table_idx].unit = unit_id;

    drives_table[drives_table_idx].onerror = onerror;

    drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;

    strncpy(drives_table[nb_drives].serial, serial, sizeof(serial));

    nb_drives++;



    switch(type) {

    case IF_IDE:

    case IF_SCSI:

    case IF_XEN:

        switch(media) {

	case MEDIA_DISK:

            if (cyls != 0) {

                bdrv_set_geometry_hint(bdrv, cyls, heads, secs);

                bdrv_set_translation_hint(bdrv, translation);

            }

	    break;

	case MEDIA_CDROM:

            bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);

	    break;

	}

        break;

    case IF_SD:

        /* FIXME: This isn't really a floppy, but it's a reasonable

           approximation.  */

    case IF_FLOPPY:

        bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);

        break;

    case IF_PFLASH:

    case IF_MTD:

    case IF_VIRTIO:

        break;

    }

    if (!file[0])

        return -2;

    bdrv_flags = 0;

    if (snapshot) {

        bdrv_flags |= BDRV_O_SNAPSHOT;

        cache = 2; /* always use write-back with snapshot */

    }

    if (cache == 0) /* no caching */

        bdrv_flags |= BDRV_O_NOCACHE;

    else if (cache == 2) /* write-back */

        bdrv_flags |= BDRV_O_CACHE_WB;

    else if (cache == 3) /* not specified */

        bdrv_flags |= BDRV_O_CACHE_DEF;

    if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {

        fprintf(stderr, "qemu: could not open disk image %s\n",

                        file);

        return -1;

    }

    if (bdrv_key_required(bdrv))

        autostart = 0;

    return drives_table_idx;

}

int drive_init(struct drive_opt *arg, int snapshot, void *opaque)

{

    char buf[128];

    char file[1024];

    char devname[128];

    char serial[21];

    const char *mediastr = "";

    BlockInterfaceType type;

    enum { MEDIA_DISK, MEDIA_CDROM } media;

    int bus_id, unit_id;

    int cyls, heads, secs, translation;

    BlockDriverState *bdrv;

    BlockDriver *drv = NULL;

    QEMUMachine *machine = opaque;

    int max_devs;

    int index;

    int cache;

    int bdrv_flags, onerror;

    int drives_table_idx;

    char *str = arg->opt;

    static const char * const params[] = { "bus", "unit", "if", "index",

                                           "cyls", "heads", "secs", "trans",

                                           "media", "snapshot", "file",

                                           "cache", "format", "serial", "werror",

                                           NULL };



    if (check_params(buf, sizeof(buf), params, str) < 0) {

         fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",

                         buf, str);

         return -1;

    }



    file[0] = 0;

    cyls = heads = secs = 0;

    bus_id = 0;

    unit_id = -1;

    translation = BIOS_ATA_TRANSLATION_AUTO;

    index = -1;

    cache = 3;



    if (machine->use_scsi) {

        type = IF_SCSI;

        max_devs = MAX_SCSI_DEVS;

        pstrcpy(devname, sizeof(devname), "scsi");

    } else {

        type = IF_IDE;

        max_devs = MAX_IDE_DEVS;

        pstrcpy(devname, sizeof(devname), "ide");

    }

    media = MEDIA_DISK;



    



    if (get_param_value(buf, sizeof(buf), "bus", str)) {

        bus_id = strtol(buf, NULL, 0);

	if (bus_id < 0) {

	    fprintf(stderr, "qemu: '%s' invalid bus id\n", str);

	    return -1;

	}

    }



    if (get_param_value(buf, sizeof(buf), "unit", str)) {

        unit_id = strtol(buf, NULL, 0);

	if (unit_id < 0) {

	    fprintf(stderr, "qemu: '%s' invalid unit id\n", str);

	    return -1;

	}

    }



    if (get_param_value(buf, sizeof(buf), "if", str)) {

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

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

	    type = IF_IDE;

            max_devs = MAX_IDE_DEVS;

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

	    type = IF_SCSI;

            max_devs = MAX_SCSI_DEVS;

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

	    type = IF_FLOPPY;

            max_devs = 0;

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

	    type = IF_PFLASH;

            max_devs = 0;

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

	    type = IF_MTD;

            max_devs = 0;

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

	    type = IF_SD;

            max_devs = 0;

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

            type = IF_VIRTIO;

            max_devs = 0;

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

	    type = IF_XEN;

            max_devs = 0;

	} else {

            fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);

            return -1;

	}

    }



    if (get_param_value(buf, sizeof(buf), "index", str)) {

        index = strtol(buf, NULL, 0);

	if (index < 0) {

	    fprintf(stderr, "qemu: '%s' invalid index\n", str);

	    return -1;

	}

    }



    if (get_param_value(buf, sizeof(buf), "cyls", str)) {

        cyls = strtol(buf, NULL, 0);

    }



    if (get_param_value(buf, sizeof(buf), "heads", str)) {

        heads = strtol(buf, NULL, 0);

    }



    if (get_param_value(buf, sizeof(buf), "secs", str)) {

        secs = strtol(buf, NULL, 0);

    }



    if (cyls || heads || secs) {

        if (cyls < 1 || cyls > 16383) {

            fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);

	    return -1;

	}

        if (heads < 1 || heads > 16) {

            fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);

	    return -1;

	}

        if (secs < 1 || secs > 63) {

            fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);

	    return -1;

	}

    }



    if (get_param_value(buf, sizeof(buf), "trans", str)) {

        if (!cyls) {

            fprintf(stderr,

                    "qemu: '%s' trans must be used with cyls,heads and secs\n",

                    str);

            return -1;

        }

        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 {

            fprintf(stderr, "qemu: '%s' invalid translation type\n", str);

	    return -1;

	}

    }



    if (get_param_value(buf, sizeof(buf), "media", str)) {

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

	    media = MEDIA_DISK;

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

            if (cyls || secs || heads) {

                fprintf(stderr,

                        "qemu: '%s' invalid physical CHS format\n", str);

	        return -1;

            }

	    media = MEDIA_CDROM;

	} else {

	    fprintf(stderr, "qemu: '%s' invalid media\n", str);

	    return -1;

	}

    }



    if (get_param_value(buf, sizeof(buf), "snapshot", str)) {

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

	    snapshot = 1;

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

	    snapshot = 0;

	else {

	    fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);

	    return -1;

	}

    }



    if (get_param_value(buf, sizeof(buf), "cache", str)) {

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

            cache = 0;

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

            cache = 1;

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

            cache = 2;

        else {

           fprintf(stderr, "qemu: invalid cache option\n");

           return -1;

        }

    }



    if (get_param_value(buf, sizeof(buf), "format", str)) {

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

            fprintf(stderr, "qemu: Supported formats:");

            bdrv_iterate_format(bdrv_format_print, NULL);

            fprintf(stderr, "\n");

	    return -1;

        }

        drv = bdrv_find_format(buf);

        if (!drv) {

            fprintf(stderr, "qemu: '%s' invalid format\n", buf);

            return -1;

        }

    }



    if (arg->file == NULL)

        get_param_value(file, sizeof(file), "file", str);

    else

        pstrcpy(file, sizeof(file), arg->file);



    if (!get_param_value(serial, sizeof(serial), "serial", str))

	    memset(serial, 0,  sizeof(serial));



    onerror = BLOCK_ERR_STOP_ENOSPC;

    if (get_param_value(buf, sizeof(serial), "werror", str)) {

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

            fprintf(stderr, "werror is no supported by this format\n");

            return -1;

        }

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

            onerror = BLOCK_ERR_IGNORE;

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

            onerror = BLOCK_ERR_STOP_ENOSPC;

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

            onerror = BLOCK_ERR_STOP_ANY;

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

            onerror = BLOCK_ERR_REPORT;

        else {

            fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);

            return -1;

        }

    }



    



    if (index != -1) {

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

            fprintf(stderr,

                    "qemu: '%s' index cannot be used with bus and unit\n", str);

            return -1;

        }

        if (max_devs == 0)

        {

            unit_id = index;

            bus_id = 0;

        } else {

            unit_id = index % max_devs;

            bus_id = index / max_devs;

        }

    }



    







    if (unit_id == -1) {

       unit_id = 0;

       while (drive_get_index(type, bus_id, unit_id) != -1) {

           unit_id++;

           if (max_devs && unit_id >= max_devs) {

               unit_id -= max_devs;

               bus_id++;

           }

       }

    }



    



    if (max_devs && unit_id >= max_devs) {

        fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",

                        str, unit_id, max_devs - 1);

        return -1;

    }



    







    if (drive_get_index(type, bus_id, unit_id) != -1)

        return -2;



    



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

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

    if (max_devs)

        snprintf(buf, sizeof(buf), "%s%i%s%i",

                 devname, bus_id, mediastr, unit_id);

    else

        snprintf(buf, sizeof(buf), "%s%s%i",

                 devname, mediastr, unit_id);

    bdrv = bdrv_new(buf);

    drives_table_idx = drive_get_free_idx();

    drives_table[drives_table_idx].bdrv = bdrv;

    drives_table[drives_table_idx].type = type;

    drives_table[drives_table_idx].bus = bus_id;

    drives_table[drives_table_idx].unit = unit_id;

    drives_table[drives_table_idx].onerror = onerror;

    drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;

    strncpy(drives_table[nb_drives].serial, serial, sizeof(serial));

    nb_drives++;



    switch(type) {

    case IF_IDE:

    case IF_SCSI:

    case IF_XEN:

        switch(media) {

	case MEDIA_DISK:

            if (cyls != 0) {

                bdrv_set_geometry_hint(bdrv, cyls, heads, secs);

                bdrv_set_translation_hint(bdrv, translation);

            }

	    break;

	case MEDIA_CDROM:

            bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);

	    break;

	}

        break;

    case IF_SD:

        



    case IF_FLOPPY:

        bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);

        break;

    case IF_PFLASH:

    case IF_MTD:

    case IF_VIRTIO:

        break;

    }

    if (!file[0])

        return -2;

    bdrv_flags = 0;

    if (snapshot) {

        bdrv_flags |= BDRV_O_SNAPSHOT;

        cache = 2; 

    }

    if (cache == 0) 

        bdrv_flags |= BDRV_O_NOCACHE;

    else if (cache == 2) 

        bdrv_flags |= BDRV_O_CACHE_WB;

    else if (cache == 3) 

        bdrv_flags |= BDRV_O_CACHE_DEF;

    if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {

        fprintf(stderr, "qemu: could not open disk image %s\n",

                        file);

        return -1;

    }

    if (bdrv_key_required(bdrv))

        autostart = 0;

    return drives_table_idx;

}

int FUNC_0(struct drive_opt *VAR_0, int VAR_1, void *VAR_2)

{

    char VAR_3[128];

    char VAR_4[1024];

    char VAR_5[128];

    char VAR_6[21];

    const char *VAR_7 = "";

    BlockInterfaceType type;

    enum { MEDIA_DISK, MEDIA_CDROM } VAR_8;

    int VAR_9, VAR_10;

    int VAR_11, VAR_12, VAR_13, VAR_14;

    BlockDriverState *bdrv;

    BlockDriver *drv = NULL;

    QEMUMachine *machine = VAR_2;

    int VAR_15;

    int VAR_16;

    int VAR_17;

    int VAR_18, VAR_19;

    int VAR_20;

    char *VAR_21 = VAR_0->opt;

    static const char * const VAR_22[] = { "bus", "unit", "if", "VAR_16",

                                           "VAR_11", "VAR_12", "VAR_13", "trans",

                                           "VAR_8", "VAR_1", "VAR_4",

                                           "VAR_17", "format", "VAR_6", "werror",

                                           NULL };



    if (check_params(VAR_3, sizeof(VAR_3), VAR_22, VAR_21) < 0) {

         fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",

                         VAR_3, VAR_21);

         return -1;

    }



    VAR_4[0] = 0;

    VAR_11 = VAR_12 = VAR_13 = 0;

    VAR_9 = 0;

    VAR_10 = -1;

    VAR_14 = BIOS_ATA_TRANSLATION_AUTO;

    VAR_16 = -1;

    VAR_17 = 3;



    if (machine->use_scsi) {

        type = IF_SCSI;

        VAR_15 = MAX_SCSI_DEVS;

        pstrcpy(VAR_5, sizeof(VAR_5), "scsi");

    } else {

        type = IF_IDE;

        VAR_15 = MAX_IDE_DEVS;

        pstrcpy(VAR_5, sizeof(VAR_5), "ide");

    }

    VAR_8 = MEDIA_DISK;



    



    if (get_param_value(VAR_3, sizeof(VAR_3), "bus", VAR_21)) {

        VAR_9 = strtol(VAR_3, NULL, 0);

	if (VAR_9 < 0) {

	    fprintf(stderr, "qemu: '%s' invalid bus id\n", VAR_21);

	    return -1;

	}

    }



    if (get_param_value(VAR_3, sizeof(VAR_3), "unit", VAR_21)) {

        VAR_10 = strtol(VAR_3, NULL, 0);

	if (VAR_10 < 0) {

	    fprintf(stderr, "qemu: '%s' invalid unit id\n", VAR_21);

	    return -1;

	}

    }



    if (get_param_value(VAR_3, sizeof(VAR_3), "if", VAR_21)) {

        pstrcpy(VAR_5, sizeof(VAR_5), VAR_3);

        if (!strcmp(VAR_3, "ide")) {

	    type = IF_IDE;

            VAR_15 = MAX_IDE_DEVS;

        } else if (!strcmp(VAR_3, "scsi")) {

	    type = IF_SCSI;

            VAR_15 = MAX_SCSI_DEVS;

        } else if (!strcmp(VAR_3, "floppy")) {

	    type = IF_FLOPPY;

            VAR_15 = 0;

        } else if (!strcmp(VAR_3, "pflash")) {

	    type = IF_PFLASH;

            VAR_15 = 0;

	} else if (!strcmp(VAR_3, "mtd")) {

	    type = IF_MTD;

            VAR_15 = 0;

	} else if (!strcmp(VAR_3, "sd")) {

	    type = IF_SD;

            VAR_15 = 0;

        } else if (!strcmp(VAR_3, "virtio")) {

            type = IF_VIRTIO;

            VAR_15 = 0;

	} else if (!strcmp(VAR_3, "xen")) {

	    type = IF_XEN;

            VAR_15 = 0;

	} else {

            fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", VAR_21, VAR_3);

            return -1;

	}

    }



    if (get_param_value(VAR_3, sizeof(VAR_3), "VAR_16", VAR_21)) {

        VAR_16 = strtol(VAR_3, NULL, 0);

	if (VAR_16 < 0) {

	    fprintf(stderr, "qemu: '%s' invalid VAR_16\n", VAR_21);

	    return -1;

	}

    }



    if (get_param_value(VAR_3, sizeof(VAR_3), "VAR_11", VAR_21)) {

        VAR_11 = strtol(VAR_3, NULL, 0);

    }



    if (get_param_value(VAR_3, sizeof(VAR_3), "VAR_12", VAR_21)) {

        VAR_12 = strtol(VAR_3, NULL, 0);

    }



    if (get_param_value(VAR_3, sizeof(VAR_3), "VAR_13", VAR_21)) {

        VAR_13 = strtol(VAR_3, NULL, 0);

    }



    if (VAR_11 || VAR_12 || VAR_13) {

        if (VAR_11 < 1 || VAR_11 > 16383) {

            fprintf(stderr, "qemu: '%s' invalid physical VAR_11 number\n", VAR_21);

	    return -1;

	}

        if (VAR_12 < 1 || VAR_12 > 16) {

            fprintf(stderr, "qemu: '%s' invalid physical VAR_12 number\n", VAR_21);

	    return -1;

	}

        if (VAR_13 < 1 || VAR_13 > 63) {

            fprintf(stderr, "qemu: '%s' invalid physical VAR_13 number\n", VAR_21);

	    return -1;

	}

    }



    if (get_param_value(VAR_3, sizeof(VAR_3), "trans", VAR_21)) {

        if (!VAR_11) {

            fprintf(stderr,

                    "qemu: '%s' trans must be used with VAR_11,VAR_12 and VAR_13\n",

                    VAR_21);

            return -1;

        }

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

            VAR_14 = BIOS_ATA_TRANSLATION_NONE;

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

            VAR_14 = BIOS_ATA_TRANSLATION_LBA;

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

            VAR_14 = BIOS_ATA_TRANSLATION_AUTO;

	else {

            fprintf(stderr, "qemu: '%s' invalid VAR_14 type\n", VAR_21);

	    return -1;

	}

    }



    if (get_param_value(VAR_3, sizeof(VAR_3), "VAR_8", VAR_21)) {

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

	    VAR_8 = MEDIA_DISK;

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

            if (VAR_11 || VAR_13 || VAR_12) {

                fprintf(stderr,

                        "qemu: '%s' invalid physical CHS format\n", VAR_21);

	        return -1;

            }

	    VAR_8 = MEDIA_CDROM;

	} else {

	    fprintf(stderr, "qemu: '%s' invalid VAR_8\n", VAR_21);

	    return -1;

	}

    }



    if (get_param_value(VAR_3, sizeof(VAR_3), "VAR_1", VAR_21)) {

        if (!strcmp(VAR_3, "on"))

	    VAR_1 = 1;

        else if (!strcmp(VAR_3, "off"))

	    VAR_1 = 0;

	else {

	    fprintf(stderr, "qemu: '%s' invalid VAR_1 option\n", VAR_21);

	    return -1;

	}

    }



    if (get_param_value(VAR_3, sizeof(VAR_3), "VAR_17", VAR_21)) {

        if (!strcmp(VAR_3, "off") || !strcmp(VAR_3, "none"))

            VAR_17 = 0;

        else if (!strcmp(VAR_3, "writethrough"))

            VAR_17 = 1;

        else if (!strcmp(VAR_3, "writeback"))

            VAR_17 = 2;

        else {

           fprintf(stderr, "qemu: invalid VAR_17 option\n");

           return -1;

        }

    }



    if (get_param_value(VAR_3, sizeof(VAR_3), "format", VAR_21)) {

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

            fprintf(stderr, "qemu: Supported formats:");

            bdrv_iterate_format(bdrv_format_print, NULL);

            fprintf(stderr, "\n");

	    return -1;

        }

        drv = bdrv_find_format(VAR_3);

        if (!drv) {

            fprintf(stderr, "qemu: '%s' invalid format\n", VAR_3);

            return -1;

        }

    }



    if (VAR_0->VAR_4 == NULL)

        get_param_value(VAR_4, sizeof(VAR_4), "VAR_4", VAR_21);

    else

        pstrcpy(VAR_4, sizeof(VAR_4), VAR_0->VAR_4);



    if (!get_param_value(VAR_6, sizeof(VAR_6), "VAR_6", VAR_21))

	    memset(VAR_6, 0,  sizeof(VAR_6));



    VAR_19 = BLOCK_ERR_STOP_ENOSPC;

    if (get_param_value(VAR_3, sizeof(VAR_6), "werror", VAR_21)) {

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

            fprintf(stderr, "werror is no supported by this format\n");

            return -1;

        }

        if (!strcmp(VAR_3, "ignore"))

            VAR_19 = BLOCK_ERR_IGNORE;

        else if (!strcmp(VAR_3, "enospc"))

            VAR_19 = BLOCK_ERR_STOP_ENOSPC;

        else if (!strcmp(VAR_3, "stop"))

            VAR_19 = BLOCK_ERR_STOP_ANY;

        else if (!strcmp(VAR_3, "report"))

            VAR_19 = BLOCK_ERR_REPORT;

        else {

            fprintf(stderr, "qemu: '%s' invalid write error action\n", VAR_3);

            return -1;

        }

    }



    



    if (VAR_16 != -1) {

        if (VAR_9 != 0 || VAR_10 != -1) {

            fprintf(stderr,

                    "qemu: '%s' VAR_16 cannot be used with bus and unit\n", VAR_21);

            return -1;

        }

        if (VAR_15 == 0)

        {

            VAR_10 = VAR_16;

            VAR_9 = 0;

        } else {

            VAR_10 = VAR_16 % VAR_15;

            VAR_9 = VAR_16 / VAR_15;

        }

    }



    







    if (VAR_10 == -1) {

       VAR_10 = 0;

       while (drive_get_index(type, VAR_9, VAR_10) != -1) {

           VAR_10++;

           if (VAR_15 && VAR_10 >= VAR_15) {

               VAR_10 -= VAR_15;

               VAR_9++;

           }

       }

    }



    



    if (VAR_15 && VAR_10 >= VAR_15) {

        fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",

                        VAR_21, VAR_10, VAR_15 - 1);

        return -1;

    }



    







    if (drive_get_index(type, VAR_9, VAR_10) != -1)

        return -2;



    



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

        VAR_7 = (VAR_8 == MEDIA_CDROM) ? "-cd" : "-hd";

    if (VAR_15)

        snprintf(VAR_3, sizeof(VAR_3), "%s%i%s%i",

                 VAR_5, VAR_9, VAR_7, VAR_10);

    else

        snprintf(VAR_3, sizeof(VAR_3), "%s%s%i",

                 VAR_5, VAR_7, VAR_10);

    bdrv = bdrv_new(VAR_3);

    VAR_20 = drive_get_free_idx();

    drives_table[VAR_20].bdrv = bdrv;

    drives_table[VAR_20].type = type;

    drives_table[VAR_20].bus = VAR_9;

    drives_table[VAR_20].unit = VAR_10;

    drives_table[VAR_20].VAR_19 = VAR_19;

    drives_table[VAR_20].drive_opt_idx = VAR_0 - drives_opt;

    strncpy(drives_table[nb_drives].VAR_6, VAR_6, sizeof(VAR_6));

    nb_drives++;



    switch(type) {

    case IF_IDE:

    case IF_SCSI:

    case IF_XEN:

        switch(VAR_8) {

	case MEDIA_DISK:

            if (VAR_11 != 0) {

                bdrv_set_geometry_hint(bdrv, VAR_11, VAR_12, VAR_13);

                bdrv_set_translation_hint(bdrv, VAR_14);

            }

	    break;

	case MEDIA_CDROM:

            bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);

	    break;

	}

        break;

    case IF_SD:

        



    case IF_FLOPPY:

        bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);

        break;

    case IF_PFLASH:

    case IF_MTD:

    case IF_VIRTIO:

        break;

    }

    if (!VAR_4[0])

        return -2;

    VAR_18 = 0;

    if (VAR_1) {

        VAR_18 |= BDRV_O_SNAPSHOT;

        VAR_17 = 2; 

    }

    if (VAR_17 == 0) 

        VAR_18 |= BDRV_O_NOCACHE;

    else if (VAR_17 == 2) 

        VAR_18 |= BDRV_O_CACHE_WB;

    else if (VAR_17 == 3) 

        VAR_18 |= BDRV_O_CACHE_DEF;

    if (bdrv_open2(bdrv, VAR_4, VAR_18, drv) < 0) {

        fprintf(stderr, "qemu: could not open disk image %s\n",

                        VAR_4);

        return -1;

    }

    if (bdrv_key_required(bdrv))

        autostart = 0;

    return VAR_20;

}

int FUNC_0(struct drive_opt *VAR_0, int VAR_1, void *VAR_2)
{ char VAR_3[128]; char VAR_4[1024]; char VAR_5[128]; char VAR_6[21]; const char *VAR_7 = ""; BlockInterfaceType type; enum { MEDIA_DISK, MEDIA_CDROM } VAR_8; int VAR_9, VAR_10; int VAR_11, VAR_12, VAR_13, VAR_14; BlockDriverState *bdrv; BlockDriver *drv = NULL; QEMUMachine *machine = VAR_2; int VAR_15; int VAR_16; int VAR_17; int VAR_18, VAR_19; int VAR_20; char *VAR_21 = VAR_0->opt; static const char * const VAR_22[] = { "bus", "unit", "if", "VAR_16", "VAR_11", "VAR_12", "VAR_13", "trans",
"VAR_8", "VAR_1", "VAR_4",
"VAR_17", "format", "VAR_6", "werror",
NULL }; if (check_params(VAR_3, sizeof(VAR_3), VAR_22, VAR_21) < 0) { fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
VAR_3, VAR_21); return -1; } VAR_4[0] = 0; VAR_11 = VAR_12 = VAR_13 = 0; VAR_9 = 0; VAR_10 = -1; VAR_14 = BIOS_ATA_TRANSLATION_AUTO; VAR_16 = -1; VAR_17 = 3; if (machine->use_scsi) { type = IF_SCSI; VAR_15 = MAX_SCSI_DEVS; pstrcpy(VAR_5, sizeof(VAR_5), "scsi"); } else { type = IF_IDE; VAR_15 = MAX_IDE_DEVS; pstrcpy(VAR_5, sizeof(VAR_5), "ide"); } VAR_8 = MEDIA_DISK; if (get_param_value(VAR_3, sizeof(VAR_3), "bus", VAR_21)) { VAR_9 = strtol(VAR_3, NULL, 0); if (VAR_9 < 0) { fprintf(stderr, "qemu: '%s' invalid bus id\n", VAR_21); return -1; } } if (get_param_value(VAR_3, sizeof(VAR_3), "unit", VAR_21)) { VAR_10 = strtol(VAR_3, NULL, 0); if (VAR_10 < 0) { fprintf(stderr, "qemu: '%s' invalid unit id\n", VAR_21); return -1; } } if (get_param_value(VAR_3, sizeof(VAR_3), "if", VAR_21)) { pstrcpy(VAR_5, sizeof(VAR_5), VAR_3); if (!strcmp(VAR_3, "ide")) { type = IF_IDE; VAR_15 = MAX_IDE_DEVS; } else if (!strcmp(VAR_3, "scsi")) { type = IF_SCSI; VAR_15 = MAX_SCSI_DEVS; } else if (!strcmp(VAR_3, "floppy")) { type = IF_FLOPPY; VAR_15 = 0; } else if (!strcmp(VAR_3, "pflash")) { type = IF_PFLASH; VAR_15 = 0; } else if (!strcmp(VAR_3, "mtd")) { type = IF_MTD; VAR_15 = 0; } else if (!strcmp(VAR_3, "sd")) { type = IF_SD; VAR_15 = 0; } else if (!strcmp(VAR_3, "virtio")) { type = IF_VIRTIO; VAR_15 = 0; } else if (!strcmp(VAR_3, "xen")) { type = IF_XEN; VAR_15 = 0; } else { fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", VAR_21, VAR_3); return -1; } } if (get_param_value(VAR_3, sizeof(VAR_3), "VAR_16", VAR_21)) { VAR_16 = strtol(VAR_3, NULL, 0); if (VAR_16 < 0) { fprintf(stderr, "qemu: '%s' invalid VAR_16\n", VAR_21); return -1; } } if (get_param_value(VAR_3, sizeof(VAR_3), "VAR_11", VAR_21)) { VAR_11 = strtol(VAR_3, NULL, 0); } if (get_param_value(VAR_3, sizeof(VAR_3), "VAR_12", VAR_21)) { VAR_12 = strtol(VAR_3, NULL, 0); } if (get_param_value(VAR_3, sizeof(VAR_3), "VAR_13", VAR_21)) { VAR_13 = strtol(VAR_3, NULL, 0); } if (VAR_11 || VAR_12 || VAR_13) { if (VAR_11 < 1 || VAR_11 > 16383) { fprintf(stderr, "qemu: '%s' invalid physical VAR_11 number\n", VAR_21); return -1; } if (VAR_12 < 1 || VAR_12 > 16) { fprintf(stderr, "qemu: '%s' invalid physical VAR_12 number\n", VAR_21); return -1; } if (VAR_13 < 1 || VAR_13 > 63) { fprintf(stderr, "qemu: '%s' invalid physical VAR_13 number\n", VAR_21); return -1; } } if (get_param_value(VAR_3, sizeof(VAR_3), "trans", VAR_21)) { if (!VAR_11) { fprintf(stderr,
"qemu: '%s' trans must be used with VAR_11,VAR_12 and VAR_13\n",
VAR_21); return -1; } if (!strcmp(VAR_3, "none"))
VAR_14 = BIOS_ATA_TRANSLATION_NONE; else if (!strcmp(VAR_3, "lba"))
VAR_14 = BIOS_ATA_TRANSLATION_LBA; else if (!strcmp(VAR_3, "auto"))
VAR_14 = BIOS_ATA_TRANSLATION_AUTO; else { fprintf(stderr, "qemu: '%s' invalid VAR_14 type\n", VAR_21); return -1; } } if (get_param_value(VAR_3, sizeof(VAR_3), "VAR_8", VAR_21)) { if (!strcmp(VAR_3, "disk")) { VAR_8 = MEDIA_DISK; } else if (!strcmp(VAR_3, "cdrom")) { if (VAR_11 || VAR_13 || VAR_12) { fprintf(stderr,
"qemu: '%s' invalid physical CHS format\n", VAR_21); return -1; } VAR_8 = MEDIA_CDROM; } else { fprintf(stderr, "qemu: '%s' invalid VAR_8\n", VAR_21); return -1; } } if (get_param_value(VAR_3, sizeof(VAR_3), "VAR_1", VAR_21)) { if (!strcmp(VAR_3, "on"))
VAR_1 = 1; else if (!strcmp(VAR_3, "off"))
VAR_1 = 0; else { fprintf(stderr, "qemu: '%s' invalid VAR_1 option\n", VAR_21); return -1; } } if (get_param_value(VAR_3, sizeof(VAR_3), "VAR_17", VAR_21)) { if (!strcmp(VAR_3, "off") || !strcmp(VAR_3, "none"))
VAR_17 = 0; else if (!strcmp(VAR_3, "writethrough"))
VAR_17 = 1; else if (!strcmp(VAR_3, "writeback"))
VAR_17 = 2; else { fprintf(stderr, "qemu: invalid VAR_17 option\n"); return -1; } } if (get_param_value(VAR_3, sizeof(VAR_3), "format", VAR_21)) { if (strcmp(VAR_3, "?") == 0) { fprintf(stderr, "qemu: Supported formats:"); bdrv_iterate_format(bdrv_format_print, NULL); fprintf(stderr, "\n"); return -1; } drv = bdrv_find_format(VAR_3); if (!drv) { fprintf(stderr, "qemu: '%s' invalid format\n", VAR_3); return -1; } } if (VAR_0->VAR_4 == NULL)
get_param_value(VAR_4, sizeof(VAR_4), "VAR_4", VAR_21); else
pstrcpy(VAR_4, sizeof(VAR_4), VAR_0->VAR_4); if (!get_param_value(VAR_6, sizeof(VAR_6), "VAR_6", VAR_21))
memset(VAR_6, 0,  sizeof(VAR_6)); VAR_19 = BLOCK_ERR_STOP_ENOSPC; if (get_param_value(VAR_3, sizeof(VAR_6), "werror", VAR_21)) { if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) { fprintf(stderr, "werror is no supported by this format\n"); return -1; } if (!strcmp(VAR_3, "ignore"))
VAR_19 = BLOCK_ERR_IGNORE; else if (!strcmp(VAR_3, "enospc"))
VAR_19 = BLOCK_ERR_STOP_ENOSPC; else if (!strcmp(VAR_3, "stop"))
VAR_19 = BLOCK_ERR_STOP_ANY; else if (!strcmp(VAR_3, "report"))
VAR_19 = BLOCK_ERR_REPORT; else { fprintf(stderr, "qemu: '%s' invalid write error action\n", VAR_3); return -1; } } if (VAR_16 != -1) { if (VAR_9 != 0 || VAR_10 != -1) { fprintf(stderr,
"qemu: '%s' VAR_16 cannot be used with bus and unit\n", VAR_21); return -1; } if (VAR_15 == 0)
{ VAR_10 = VAR_16; VAR_9 = 0; } else { VAR_10 = VAR_16 % VAR_15; VAR_9 = VAR_16 / VAR_15; } } if (VAR_10 == -1) { VAR_10 = 0; while (drive_get_index(type, VAR_9, VAR_10) != -1) { VAR_10++; if (VAR_15 && VAR_10 >= VAR_15) { VAR_10 -= VAR_15; VAR_9++; } } } if (VAR_15 && VAR_10 >= VAR_15) { fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
VAR_21, VAR_10, VAR_15 - 1); return -1; } if (drive_get_index(type, VAR_9, VAR_10) != -1)
return -2; if (type == IF_IDE || type == IF_SCSI)
VAR_7 = (VAR_8 == MEDIA_CDROM) ? "-cd" : "-hd"; if (VAR_15)
snprintf(VAR_3, sizeof(VAR_3), "%s%i%s%i",
VAR_5, VAR_9, VAR_7, VAR_10); else
snprintf(VAR_3, sizeof(VAR_3), "%s%s%i",
VAR_5, VAR_7, VAR_10); bdrv = bdrv_new(VAR_3); VAR_20 = drive_get_free_idx(); drives_table[VAR_20].bdrv = bdrv; drives_table[VAR_20].type = type; drives_table[VAR_20].bus = VAR_9; drives_table[VAR_20].unit = VAR_10; drives_table[VAR_20].VAR_19 = VAR_19; drives_table[VAR_20].drive_opt_idx = VAR_0 - drives_opt; strncpy(drives_table[nb_drives].VAR_6, VAR_6, sizeof(VAR_6)); nb_drives++; switch(type) { case IF_IDE:
case IF_SCSI:
case IF_XEN:
switch(VAR_8) { case MEDIA_DISK:
if (VAR_11 != 0) { bdrv_set_geometry_hint(bdrv, VAR_11, VAR_12, VAR_13); bdrv_set_translation_hint(bdrv, VAR_14); } break; case MEDIA_CDROM:
bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM); break; } break; case IF_SD:
case IF_FLOPPY:
bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY); break; case IF_PFLASH:
case IF_MTD:
case IF_VIRTIO:
break; } if (!VAR_4[0])
return -2; VAR_18 = 0; if (VAR_1) { VAR_18 |= BDRV_O_SNAPSHOT; VAR_17 = 2; } if (VAR_17 == 0)
VAR_18 |= BDRV_O_NOCACHE; else if (VAR_17 == 2)
VAR_18 |= BDRV_O_CACHE_WB; else if (VAR_17 == 3)
VAR_18 |= BDRV_O_CACHE_DEF; if (bdrv_open2(bdrv, VAR_4, VAR_18, drv) < 0) { fprintf(stderr, "qemu: could not open disk image %s\n",
VAR_4); return -1; } if (bdrv_key_required(bdrv))
autostart = 0; return VAR_20; }