Skip to contentSkip to conversation

drive cleanups + QemuOpts

PATCH New Gerd Hoffmann 1 participant 9 messages Jul 22, 2009

lore
Share & tools
lore
#0 lore raw

v6 cover drive cleanups + QemuOpts

Hi,

This is a respin of the drive cleanup patch series, including the QemuOpts patch. QemuOpts got a few changes, addressing to review comments.

v6 changes:

move more option parser code from vl.c to qemu-option.[ch] move QemuOpts code to qemu-option.[ch].

v5 changes:

It supports parsing of bools (on/off), simple numbers and sizes (with postfix for kilo, mega, ...) now.

Implicitly named first args work now (new -boot syntax for example). Flags are supported in various forms (flag=on,flag=off,flag,noflag).

cheers,
Gerd

also available via
http://git.et.redhat.com/?p=qemu-kraxel.git;a=shortlog;h=refs/heads/drive.v6

#1 lore raw

PATCH v6 1/8 kill drives_table

First step cleaning up the drives handling. This one does nothing but removing drives_table[], still it became seriously big.

drive_get_index() is gone and is replaced by drives_get() which hands out DriveInfo pointers instead of a table index. This needs adaption in *tons* of places all over.

The drives are now maintained as linked list.

---
 hw/device-hotplug.c           |   23 +++---
 hw/etraxfs.c                  |    5 +-
 hw/gumstix.c                  |   16 ++--
 hw/mainstone.c                |    9 +-
 hw/mips_jazz.c                |    7 +-
 hw/mips_malta.c               |   24 ++----
 hw/mips_r4k.c                 |   13 +--
 hw/musicpal.c                 |   10 +-
 hw/nand.c                     |    8 +-
 hw/omap1.c                    |    8 +-
 hw/omap2.c                    |    8 +-
 hw/omap_sx1.c                 |   10 +-
 hw/onenand.c                  |    6 +-
 hw/pc.c                       |   21 ++---
 hw/pci-hotplug.c              |   28 ++++---
 hw/petalogix_s3adsp1800_mmu.c |    5 +-
 hw/ppc405_boards.c            |   34 ++++----
 hw/ppc440_bamboo.c            |    5 +-
 hw/ppc_newworld.c             |    9 +--
 hw/ppc_oldworld.c             |   30 ++-----
 hw/ppc_prep.c                 |   16 +---
 hw/ppce500_mpc8544ds.c        |    5 +-
 hw/pxa2xx.c                   |   16 ++--
 hw/qdev.c                     |   17 ++---
 hw/r2d.c                      |    5 +-
 hw/spitz.c                    |    8 +-
 hw/sun4m.c                    |   16 ++--
 hw/sun4u.c                    |   18 ++---
 hw/tosa.c                     |    8 +-
 hw/xen_disk.c                 |   14 ++--
 hw/xen_machine_pv.c           |    9 +-
 monitor.c                     |   12 ++-
 qemu-char.c                   |    6 +-
 savevm.c                      |   32 ++++---
 sysemu.h                      |   13 ++--
 vl.c                          |  177 ++++++++++++++++++++---------------------
 36 files changed, 308 insertions(+), 343 deletions(-)
hw/device-hotplug.c +12 -11
index e178083..5257274 100644
@@ -28,22 +28,23 @@
 #include "block_int.h"
 #include "sysemu.h"
 
-int add_init_drive(const char *opts)
+DriveInfo *add_init_drive(const char *opts)
 {
-    int drive_opt_idx, drive_idx;
-    int ret = -1;
+    int drive_opt_idx;
+    int fatal_error;
+    DriveInfo *dinfo;
 
     drive_opt_idx = drive_add(NULL, "%s", opts);
     if (!drive_opt_idx)
-        return ret;
+        return NULL;
 
-    drive_idx = drive_init(&drives_opt[drive_opt_idx], 0, current_machine);
-    if (drive_idx == -1) {
+    dinfo = drive_init(&drives_opt[drive_opt_idx], 0, current_machine, &fatal_error);
+    if (!dinfo) {
         drive_remove(drive_opt_idx);
-        return ret;
+        return NULL;
     }
 
-    return drive_idx;
+    return dinfo;
 }
 
 void destroy_nic(dev_match_fn *match_fn, void *arg)
@@ -64,11 +65,11 @@ void destroy_nic(dev_match_fn *match_fn, void *arg)
 
 void destroy_bdrvs(dev_match_fn *match_fn, void *arg)
 {
-    int i;
+    DriveInfo *dinfo;
     struct BlockDriverState *bs;
 
-    for (i = 0; i <= MAX_DRIVES; i++) {
-        bs = drives_table[i].bdrv;
+    TAILQ_FOREACH(dinfo, &drives, next) {
+        bs = dinfo->bdrv;
         if (bs) {
             if (bs->private && match_fn(bs->private, arg)) {
                 drive_uninit(bs);
hw/etraxfs.c +3 -2
index c2eca52..ab6a3a3 100644
@@ -55,6 +55,7 @@ void bareetraxfs_init (ram_addr_t ram_size,
     void *etraxfs_dmac;
     struct etraxfs_dma_client *eth[2] = {NULL, NULL};
     int kernel_size;
+    DriveInfo *dinfo;
     int i;
     ram_addr_t phys_ram;
     ram_addr_t phys_flash;
@@ -79,9 +80,9 @@ void bareetraxfs_init (ram_addr_t ram_size,
 
 
     phys_flash = qemu_ram_alloc(FLASH_SIZE);
-    i = drive_get_index(IF_PFLASH, 0, 0);
+    dinfo = drive_get(IF_PFLASH, 0, 0);
     pflash_cfi02_register(0x0, phys_flash,
-                          i != -1 ? drives_table[i].bdrv : NULL, (64 * 1024),
+                          dinfo ? dinfo->bdrv : NULL, (64 * 1024),
                           FLASH_SIZE >> 16,
                           1, 2, 0x0000, 0x0000, 0x0000, 0x0000,
                           0x555, 0x2aa);
hw/gumstix.c +8 -8
index 85b95ee..8fbf64c 100644
@@ -47,22 +47,22 @@ static void connex_init(ram_addr_t ram_size,
                 const char *initrd_filename, const char *cpu_model)
 {
     PXA2xxState *cpu;
-    int index;
+    DriveInfo *dinfo;
 
     uint32_t connex_rom = 0x01000000;
     uint32_t connex_ram = 0x04000000;
 
     cpu = pxa255_init(connex_ram);
 
-    index = drive_get_index(IF_PFLASH, 0, 0);
-    if (index == -1) {
+    dinfo = drive_get(IF_PFLASH, 0, 0);
+    if (!dinfo) {
         fprintf(stderr, "A flash image must be given with the "
                 "'pflash' parameter\n");
         exit(1);
     }
 
     if (!pflash_cfi01_register(0x00000000, qemu_ram_alloc(connex_rom),
-            drives_table[index].bdrv, sector_len, connex_rom / sector_len,
+            dinfo->bdrv, sector_len, connex_rom / sector_len,
             2, 0, 0, 0, 0)) {
         fprintf(stderr, "qemu: Error registering flash memory.\n");
         exit(1);
@@ -81,22 +81,22 @@ static void verdex_init(ram_addr_t ram_size,
                 const char *initrd_filename, const char *cpu_model)
 {
     PXA2xxState *cpu;
-    int index;
+    DriveInfo *dinfo;
 
     uint32_t verdex_rom = 0x02000000;
     uint32_t verdex_ram = 0x10000000;
 
     cpu = pxa270_init(verdex_ram, cpu_model ?: "pxa270-c0");
 
-    index = drive_get_index(IF_PFLASH, 0, 0);
-    if (index == -1) {
+    dinfo = drive_get(IF_PFLASH, 0, 0);
+    if (!dinfo) {
         fprintf(stderr, "A flash image must be given with the "
                 "'pflash' parameter\n");
         exit(1);
     }
 
     if (!pflash_cfi01_register(0x00000000, qemu_ram_alloc(verdex_rom),
-            drives_table[index].bdrv, sector_len, verdex_rom / sector_len,
+            dinfo->bdrv, sector_len, verdex_rom / sector_len,
             2, 0, 0, 0, 0)) {
         fprintf(stderr, "qemu: Error registering flash memory.\n");
         exit(1);
hw/mainstone.c +5 -4
index 151ea0e..3e517f0 100644
@@ -77,7 +77,8 @@ static void mainstone_common_init(ram_addr_t ram_size,
     target_phys_addr_t mainstone_flash_base[] = { MST_FLASH_0, MST_FLASH_1 };
     PXA2xxState *cpu;
     qemu_irq *mst_irq;
-    int i, index;
+    DriveInfo *dinfo;
+    int i;
 
     if (!cpu_model)
         cpu_model = "pxa270-c5";
@@ -92,8 +93,8 @@ static void mainstone_common_init(ram_addr_t ram_size,
 
     /* There are two 32MiB flash devices on the board */
     for (i = 0; i < 2; i ++) {
-        index = drive_get_index(IF_PFLASH, 0, i);
-        if (index == -1) {
+        dinfo = drive_get(IF_PFLASH, 0, i);
+        if (!dinfo) {
             fprintf(stderr, "Two flash images must be given with the "
                     "'pflash' parameter\n");
             exit(1);
@@ -101,7 +102,7 @@ static void mainstone_common_init(ram_addr_t ram_size,
 
         if (!pflash_cfi01_register(mainstone_flash_base[i],
                                 qemu_ram_alloc(MAINSTONE_FLASH),
-                                drives_table[index].bdrv, sector_len,
+                                dinfo->bdrv, sector_len,
                                 MAINSTONE_FLASH / sector_len, 4, 0, 0, 0, 0)) {
             fprintf(stderr, "qemu: Error registering flash memory.\n");
             exit(1);
hw/mips_jazz.c +2 -5
index 82cd385..e09a6b2 100644
@@ -235,11 +235,8 @@ void mips_jazz_init (ram_addr_t ram_size,
         exit(1);
     }
     for (n = 0; n < MAX_FD; n++) {
-        int fd = drive_get_index(IF_FLOPPY, 0, n);
-        if (fd != -1)
-            fds[n] = drives_table[fd].bdrv;
-        else
-            fds[n] = NULL;
+        DriveInfo *dinfo = drive_get(IF_FLOPPY, 0, n);
+        fds[n] = dinfo ? dinfo->bdrv : NULL;
     }
     fdctrl_init(rc4030[1], 0, 1, 0x80003000, fds);
 
hw/mips_malta.c +9 -15
index 7728e58..4e51c8d 100644
@@ -773,7 +773,7 @@ void mips_malta_init (ram_addr_t ram_size,
     uint8_t *eeprom_buf;
     i2c_bus *smbus;
     int i;
-    int index;
+    DriveInfo *dinfo;
     BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
     BlockDriverState *fd[MAX_FD];
     int fl_idx = 0;
@@ -827,8 +827,8 @@ void mips_malta_init (ram_addr_t ram_size,
         env->CP0_Status &= ~((1 << CP0St_BEV) | (1 << CP0St_ERL));
         write_bootloader(env, qemu_get_ram_ptr(bios_offset), kernel_entry);
     } else {
-        index = drive_get_index(IF_PFLASH, 0, fl_idx);
-        if (index != -1) {
+        dinfo = drive_get(IF_PFLASH, 0, fl_idx);
+        if (dinfo) {
             /* Load firmware from flash. */
             bios_size = 0x400000;
             fl_sectors = bios_size >> 16;
@@ -836,10 +836,10 @@ void mips_malta_init (ram_addr_t ram_size,
             printf("Register parallel flash %d size " TARGET_FMT_lx " at "
                    "offset %08lx addr %08llx '%s' %x\n",
                    fl_idx, bios_size, bios_offset, 0x1e000000LL,
-                   bdrv_get_device_name(drives_table[index].bdrv), fl_sectors);
+                   bdrv_get_device_name(dinfo->bdrv), fl_sectors);
 #endif
             pflash_cfi01_register(0x1e000000LL, bios_offset,
-                                  drives_table[index].bdrv, 65536, fl_sectors,
+                                  dinfo->bdrv, 65536, fl_sectors,
                                   4, 0x0000, 0x0000, 0x0000, 0x0000);
             fl_idx++;
         } else {
@@ -898,11 +898,8 @@ void mips_malta_init (ram_addr_t ram_size,
     }
 
     for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
-        index = drive_get_index(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
-        if (index != -1)
-            hd[i] = drives_table[index].bdrv;
-        else
-            hd[i] = NULL;
+        dinfo = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
+        hd[i] = dinfo ? dinfo->bdrv : NULL;
     }
 
     piix4_devfn = piix4_init(pci_bus, 80);
@@ -929,11 +926,8 @@ void mips_malta_init (ram_addr_t ram_size,
     if (parallel_hds[0])
         parallel_init(0x378, i8259[7], parallel_hds[0]);
     for(i = 0; i < MAX_FD; i++) {
-        index = drive_get_index(IF_FLOPPY, 0, i);
-       if (index != -1)
-           fd[i] = drives_table[index].bdrv;
-       else
-           fd[i] = NULL;
+        dinfo = drive_get(IF_FLOPPY, 0, i);
+        fd[i] = dinfo ? dinfo->bdrv : NULL;
     }
     floppy_controller = fdctrl_init(i8259[6], 2, 0, 0x3f0, fd);
 
hw/mips_r4k.c +5 -8
index 7f8af74..476612c 100644
@@ -155,8 +155,8 @@ void mips_r4k_init (ram_addr_t ram_size,
     RTCState *rtc_state;
     int i;
     qemu_irq *i8259;
-    int index;
     BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
+    DriveInfo *dinfo;
 
     /* init CPUs */
     if (cpu_model == NULL) {
@@ -208,11 +208,11 @@ void mips_r4k_init (ram_addr_t ram_size,
                                      bios_offset | IO_MEM_ROM);
 
         load_image_targphys(filename, 0x1fc00000, BIOS_SIZE);
-    } else if ((index = drive_get_index(IF_PFLASH, 0, 0)) > -1) {
+    } else if ((dinfo = drive_get(IF_PFLASH, 0, 0)) != NULL) {
         uint32_t mips_rom = 0x00400000;
         bios_offset = qemu_ram_alloc(mips_rom);
         if (!pflash_cfi01_register(0x1fc00000, bios_offset,
-            drives_table[index].bdrv, sector_len, mips_rom / sector_len,
+            dinfo->bdrv, sector_len, mips_rom / sector_len,
             4, 0, 0, 0, 0)) {
             fprintf(stderr, "qemu: Error registering flash memory.\n");
 	}
@@ -267,11 +267,8 @@ void mips_r4k_init (ram_addr_t ram_size,
     }
 
     for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
-        index = drive_get_index(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
-        if (index != -1)
-            hd[i] = drives_table[index].bdrv;
-        else
-            hd[i] = NULL;
+        dinfo = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
+        hd[i] = dinfo ? dinfo->bdrv : NULL;
     }
 
     for(i = 0; i < MAX_IDE_BUS; i++)
hw/musicpal.c +5 -5
index e636791..2d26b33 100644
@@ -1512,8 +1512,8 @@ static void musicpal_init(ram_addr_t ram_size,
     qemu_irq pic[32];
     DeviceState *dev;
     int i;
-    int index;
     unsigned long flash_size;
+    DriveInfo *dinfo;
 
     if (!cpu_model)
         cpu_model = "arm926";
@@ -1549,9 +1549,9 @@ static void musicpal_init(ram_addr_t ram_size,
                    serial_hds[1], 1);
 
     /* Register flash */
-    index = drive_get_index(IF_PFLASH, 0, 0);
-    if (index != -1) {
-        flash_size = bdrv_getlength(drives_table[index].bdrv);
+    dinfo = drive_get(IF_PFLASH, 0, 0);
+    if (dinfo) {
+        flash_size = bdrv_getlength(dinfo->bdrv);
         if (flash_size != 8*1024*1024 && flash_size != 16*1024*1024 &&
             flash_size != 32*1024*1024) {
             fprintf(stderr, "Invalid flash image size\n");
@@ -1564,7 +1564,7 @@ static void musicpal_init(ram_addr_t ram_size,
          * image is smaller than 32 MB.
          */
         pflash_cfi02_register(0-MP_FLASH_SIZE_MAX, qemu_ram_alloc(flash_size),
-                              drives_table[index].bdrv, 0x10000,
+                              dinfo->bdrv, 0x10000,
                               (flash_size + 0xffff) >> 16,
                               MP_FLASH_SIZE_MAX / flash_size,
                               2, 0x00BF, 0x236D, 0x0000, 0x0000,
hw/nand.c +4 -4
index 4ad77ec..37fd524 100644
@@ -442,16 +442,16 @@ NANDFlashState *nand_init(int manf_id, int chip_id)
 {
     int pagesize;
     NANDFlashState *s;
-    int index;
+    DriveInfo *dinfo;
 
     if (nand_flash_ids[chip_id].size == 0) {
         hw_error("%s: Unsupported NAND chip ID.\n", __FUNCTION__);
     }
 
     s = (NANDFlashState *) qemu_mallocz(sizeof(NANDFlashState));
-    index = drive_get_index(IF_MTD, 0, 0);
-    if (index != -1)
-        s->bdrv = drives_table[index].bdrv;
+    dinfo = drive_get(IF_MTD, 0, 0);
+    if (dinfo)
+        s->bdrv = dinfo->bdrv;
     s->manf_id = manf_id;
     s->chip_id = chip_id;
     s->size = nand_flash_ids[s->chip_id].size << 20;
hw/omap1.c +4 -4
index 14d7a82..93515ee 100644
@@ -4628,7 +4628,7 @@ struct omap_mpu_state_s *omap310_mpu_init(unsigned long sdram_size,
     ram_addr_t imif_base, emiff_base;
     qemu_irq *cpu_irq;
     qemu_irq dma_irqs[6];
-    int sdindex;
+    DriveInfo *dinfo;
 
     if (!core)
         core = "ti925t";
@@ -4740,12 +4740,12 @@ struct omap_mpu_state_s *omap310_mpu_init(unsigned long sdram_size,
     omap_dpll_init(&s->dpll[1], 0xfffed000, omap_findclk(s, "dpll2"));
     omap_dpll_init(&s->dpll[2], 0xfffed100, omap_findclk(s, "dpll3"));
 
-    sdindex = drive_get_index(IF_SD, 0, 0);
-    if (sdindex == -1) {
+    dinfo = drive_get(IF_SD, 0, 0);
+    if (!dinfo) {
         fprintf(stderr, "qemu: missing SecureDigital device\n");
         exit(1);
     }
-    s->mmc = omap_mmc_init(0xfffb7800, drives_table[sdindex].bdrv,
+    s->mmc = omap_mmc_init(0xfffb7800, dinfo->bdrv,
                     s->irq[1][OMAP_INT_OQN], &s->drq[OMAP_DMA_MMC_TX],
                     omap_findclk(s, "mmc_ck"));
 
hw/omap2.c +4 -4
index 8cbc4b5..e43b4a8 100644
@@ -4496,7 +4496,7 @@ struct omap_mpu_state_s *omap2420_mpu_init(unsigned long sdram_size,
     qemu_irq *cpu_irq;
     qemu_irq dma_irqs[4];
     omap_clk gpio_clks[4];
-    int sdindex;
+    DriveInfo *dinfo;
     int i;
 
     /* Core */
@@ -4645,12 +4645,12 @@ struct omap_mpu_state_s *omap2420_mpu_init(unsigned long sdram_size,
     s->sdrc = omap_sdrc_init(0x68009000);
     s->gpmc = omap_gpmc_init(0x6800a000, s->irq[0][OMAP_INT_24XX_GPMC_IRQ]);
 
-    sdindex = drive_get_index(IF_SD, 0, 0);
-    if (sdindex == -1) {
+    dinfo = drive_get(IF_SD, 0, 0);
+    if (!dinfo) {
         fprintf(stderr, "qemu: missing SecureDigital device\n");
         exit(1);
     }
-    s->mmc = omap2_mmc_init(omap_l4tao(s->l4, 9), drives_table[sdindex].bdrv,
+    s->mmc = omap2_mmc_init(omap_l4tao(s->l4, 9), dinfo->bdrv,
                     s->irq[0][OMAP_INT_24XX_MMC_IRQ],
                     &s->drq[OMAP24XX_DMA_MMC1_TX],
                     omap_findclk(s, "mmc_fclk"), omap_findclk(s, "mmc_iclk"));
hw/omap_sx1.c +5 -5
index 1ba42f6..22f801b 100644
@@ -127,7 +127,7 @@ static void sx1_init(ram_addr_t ram_size,
     static uint32_t cs2val = 0x00001139;
     static uint32_t cs3val = 0x00001139;
     ram_addr_t phys_flash;
-    int index;
+    DriveInfo *dinfo;
     int fl_idx;
     uint32_t flash_size = flash0_size;
 
@@ -151,9 +151,9 @@ static void sx1_init(ram_addr_t ram_size,
 
     fl_idx = 0;
 
-    if ((index = drive_get_index(IF_PFLASH, 0, fl_idx)) > -1) {
+    if ((dinfo = drive_get(IF_PFLASH, 0, fl_idx)) != NULL) {
         if (!pflash_cfi01_register(OMAP_CS0_BASE, qemu_ram_alloc(flash_size),
-            drives_table[index].bdrv, sector_size, flash_size / sector_size,
+            dinfo->bdrv, sector_size, flash_size / sector_size,
             4, 0, 0, 0, 0)) {
             fprintf(stderr, "qemu: Error registering flash memory %d.\n",
                            fl_idx);
@@ -162,7 +162,7 @@ static void sx1_init(ram_addr_t ram_size,
     }
 
     if ((version == 1) &&
-            (index = drive_get_index(IF_PFLASH, 0, fl_idx)) > -1) {
+            (dinfo = drive_get(IF_PFLASH, 0, fl_idx)) != NULL) {
         cpu_register_physical_memory(OMAP_CS1_BASE, flash1_size,
                         (phys_flash = qemu_ram_alloc(flash1_size)) |
                         IO_MEM_ROM);
@@ -171,7 +171,7 @@ static void sx1_init(ram_addr_t ram_size,
                         OMAP_CS1_SIZE - flash1_size, io);
 
         if (!pflash_cfi01_register(OMAP_CS1_BASE, qemu_ram_alloc(flash1_size),
-            drives_table[index].bdrv, sector_size, flash1_size / sector_size,
+            dinfo->bdrv, sector_size, flash1_size / sector_size,
             4, 0, 0, 0, 0)) {
             fprintf(stderr, "qemu: Error registering flash memory %d.\n",
                            fl_idx);
hw/onenand.c +3 -3
index 870cfd9..84fdb21 100644
@@ -618,7 +618,7 @@ static CPUWriteMemoryFunc *onenand_writefn[] = {
 void *onenand_init(uint32_t id, int regshift, qemu_irq irq)
 {
     OneNANDState *s = (OneNANDState *) qemu_mallocz(sizeof(*s));
-    int bdrv_index = drive_get_index(IF_MTD, 0, 0);
+    DriveInfo *dinfo = drive_get(IF_MTD, 0, 0);
     uint32_t size = 1 << (24 + ((id >> 12) & 7));
     void *ram;
 
@@ -632,11 +632,11 @@ void *onenand_init(uint32_t id, int regshift, qemu_irq irq)
     s->density_mask = (id & (1 << 11)) ? (1 << (6 + ((id >> 12) & 7))) : 0;
     s->iomemtype = cpu_register_io_memory(onenand_readfn,
                     onenand_writefn, s);
-    if (bdrv_index == -1)
+    if (!dinfo)
         s->image = memset(qemu_malloc(size + (size >> 5)),
                         0xff, size + (size >> 5));
     else
-        s->bdrv = drives_table[bdrv_index].bdrv;
+        s->bdrv = dinfo->bdrv;
     s->otp = memset(qemu_malloc((64 + 2) << PAGE_SHIFT),
                     0xff, (64 + 2) << PAGE_SHIFT);
     s->ram = qemu_ram_alloc(0xc000 << s->shift);
hw/pc.c +7 -14
index a50f23d..da0ef18 100644
@@ -1126,7 +1126,7 @@ static void pc_init1(ram_addr_t ram_size,
     CPUState *env;
     qemu_irq *cpu_irq;
     qemu_irq *i8259;
-    int index;
+    DriveInfo *dinfo;
     BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
     BlockDriverState *fd[MAX_FD];
     int using_vga = cirrus_vga_enabled || std_vga_enabled || vmsvga_enabled;
@@ -1356,11 +1356,8 @@ static void pc_init1(ram_addr_t ram_size,
     }
 
     for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
-        index = drive_get_index(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
-	if (index != -1)
-	    hd[i] = drives_table[index].bdrv;
-	else
-	    hd[i] = NULL;
+        dinfo = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
+        hd[i] = dinfo ? dinfo->bdrv : NULL;
     }
 
     if (pci_enabled) {
@@ -1379,11 +1376,8 @@ static void pc_init1(ram_addr_t ram_size,
 #endif
 
     for(i = 0; i < MAX_FD; i++) {
-        index = drive_get_index(IF_FLOPPY, 0, i);
-	if (index != -1)
-	    fd[i] = drives_table[index].bdrv;
-	else
-	    fd[i] = NULL;
+        dinfo = drive_get(IF_FLOPPY, 0, i);
+        fd[i] = dinfo ? dinfo->bdrv : NULL;
     }
     floppy_controller = fdctrl_init(i8259[6], 2, 0, 0x3f0, fd);
 
@@ -1437,12 +1431,11 @@ static void pc_init1(ram_addr_t ram_size,
 
     /* Add virtio block devices */
     if (pci_enabled) {
-        int index;
         int unit_id = 0;
 
-        while ((index = drive_get_index(IF_VIRTIO, 0, unit_id)) != -1) {
+        while ((dinfo = drive_get(IF_VIRTIO, 0, unit_id)) != NULL) {
             pci_dev = pci_create(virtio_blk_name,
-                                 drives_table[index].devaddr);
+                                 dinfo->devaddr);
             qdev_init(&pci_dev->qdev);
             unit_id++;
         }
hw/pci-hotplug.c +15 -13
index d0f2911..43675e2 100644
@@ -52,9 +52,10 @@ void drive_hot_add(Monitor *mon, const char *pci_addr, const char *opts)
 {
     int dom, pci_bus;
     unsigned slot;
-    int drive_idx, type, bus;
+    int type, bus;
     int success = 0;
     PCIDevice *dev;
+    DriveInfo *dinfo;
 
     if (pci_read_devaddr(mon, pci_addr, &dom, &pci_bus, &slot)) {
         return;
@@ -66,21 +67,21 @@ void drive_hot_add(Monitor *mon, const char *pci_addr, const char *opts)
         return;
     }
 
-    drive_idx = add_init_drive(opts);
-    if (drive_idx < 0)
+    dinfo = add_init_drive(opts);
+    if (!dinfo)
         return;
-    if (drives_table[drive_idx].devaddr) {
+    if (dinfo->devaddr) {
         monitor_printf(mon, "Parameter addr not supported\n");
         return;
     }
-    type = drives_table[drive_idx].type;
+    type = dinfo->type;
     bus = drive_get_max_bus (type);
 
     switch (type) {
     case IF_SCSI:
         success = 1;
-        lsi_scsi_attach(&dev->qdev, drives_table[drive_idx].bdrv,
-                        drives_table[drive_idx].unit);
+        lsi_scsi_attach(&dev->qdev, dinfo->bdrv,
+                        dinfo->unit);
         break;
     default:
         monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
@@ -88,8 +89,8 @@ void drive_hot_add(Monitor *mon, const char *pci_addr, const char *opts)
 
     if (success)
         monitor_printf(mon, "OK bus %d, unit %d\n",
-                       drives_table[drive_idx].bus,
-                       drives_table[drive_idx].unit);
+                       dinfo->bus,
+                       dinfo->unit);
     return;
 }
 
@@ -98,7 +99,8 @@ static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
                                            const char *opts)
 {
     PCIDevice *dev;
-    int type = -1, drive_idx = -1;
+    DriveInfo *dinfo;
+    int type = -1;
     char buf[128];
 
     if (get_param_value(buf, sizeof(buf), "if", opts)) {
@@ -116,10 +118,10 @@ static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
     }
 
     if (get_param_value(buf, sizeof(buf), "file", opts)) {
-        drive_idx = add_init_drive(opts);
-        if (drive_idx < 0)
+        dinfo = add_init_drive(opts);
+        if (!dinfo)
             return NULL;
-        if (drives_table[drive_idx].devaddr) {
+        if (dinfo->devaddr) {
             monitor_printf(mon, "Parameter addr not supported\n");
             return NULL;
         }
hw/petalogix_s3adsp1800_mmu.c +3 -2
index c2a196f..1a1cc0e 100644
@@ -106,6 +106,7 @@ petalogix_s3adsp1800_init(ram_addr_t ram_size,
     DeviceState *dev;
     CPUState *env;
     int kernel_size;
+    DriveInfo *dinfo;
     int i;
     target_phys_addr_t ddr_base = 0x90000000;
     ram_addr_t phys_lmb_bram;
@@ -131,9 +132,9 @@ petalogix_s3adsp1800_init(ram_addr_t ram_size,
     cpu_register_physical_memory(ddr_base, ram_size, phys_ram | IO_MEM_RAM);
 
     phys_flash = qemu_ram_alloc(FLASH_SIZE);
-    i = drive_get_index(IF_PFLASH, 0, 0);
+    dinfo = drive_get(IF_PFLASH, 0, 0);
     pflash_cfi02_register(0xa0000000, phys_flash,
-                          i != -1 ? drives_table[i].bdrv : NULL, (64 * 1024),
+                          dinfo ? dinfo->bdrv : NULL, (64 * 1024),
                           FLASH_SIZE >> 16,
                           1, 1, 0x0000, 0x0000, 0x0000, 0x0000,
                           0x555, 0x2aa);
hw/ppc405_boards.c +17 -17
index c9a1986..f2b4a84 100644
@@ -188,7 +188,7 @@ static void ref405ep_init (ram_addr_t ram_size,
     int linux_boot;
     int fl_idx, fl_sectors, len;
     int ppc_boot_device = boot_device[0];
-    int index;
+    DriveInfo *dinfo;
 
     /* XXX: fix this */
     ram_bases[0] = qemu_ram_alloc(0x08000000);
@@ -215,19 +215,19 @@ static void ref405ep_init (ram_addr_t ram_size,
 #endif
     fl_idx = 0;
 #ifdef USE_FLASH_BIOS
-    index = drive_get_index(IF_PFLASH, 0, fl_idx);
-    if (index != -1) {
-        bios_size = bdrv_getlength(drives_table[index].bdrv);
+    dinfo = drive_get(IF_PFLASH, 0, fl_idx);
+    if (dinfo) {
+        bios_size = bdrv_getlength(dinfo->bdrv);
         bios_offset = qemu_ram_alloc(bios_size);
         fl_sectors = (bios_size + 65535) >> 16;
 #ifdef DEBUG_BOARD_INIT
         printf("Register parallel flash %d size " ADDRX " at offset %08lx "
                " addr " ADDRX " '%s' %d\n",
                fl_idx, bios_size, bios_offset, -bios_size,
-               bdrv_get_device_name(drives_table[index].bdrv), fl_sectors);
+               bdrv_get_device_name(dinfo->bdrv), fl_sectors);
 #endif
         pflash_cfi02_register((uint32_t)(-bios_size), bios_offset,
-                              drives_table[index].bdrv, 65536, fl_sectors, 1,
+                              dinfo->bdrv, 65536, fl_sectors, 1,
                               2, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA);
         fl_idx++;
     } else
@@ -509,7 +509,7 @@ static void taihu_405ep_init(ram_addr_t ram_size,
     int linux_boot;
     int fl_idx, fl_sectors;
     int ppc_boot_device = boot_device[0];
-    int index;
+    DriveInfo *dinfo;
 
     /* RAM is soldered to the board so the size cannot be changed */
     ram_bases[0] = qemu_ram_alloc(0x04000000);
@@ -528,9 +528,9 @@ static void taihu_405ep_init(ram_addr_t ram_size,
 #endif
     fl_idx = 0;
 #if defined(USE_FLASH_BIOS)
-    index = drive_get_index(IF_PFLASH, 0, fl_idx);
-    if (index != -1) {
-        bios_size = bdrv_getlength(drives_table[index].bdrv);
+    dinfo = drive_get(IF_PFLASH, 0, fl_idx);
+    if (dinfo) {
+        bios_size = bdrv_getlength(dinfo->bdrv);
         /* XXX: should check that size is 2MB */
         //        bios_size = 2 * 1024 * 1024;
         fl_sectors = (bios_size + 65535) >> 16;
@@ -539,10 +539,10 @@ static void taihu_405ep_init(ram_addr_t ram_size,
         printf("Register parallel flash %d size " ADDRX " at offset %08lx "
                " addr " ADDRX " '%s' %d\n",
                fl_idx, bios_size, bios_offset, -bios_size,
-               bdrv_get_device_name(drives_table[index].bdrv), fl_sectors);
+               bdrv_get_device_name(dinfo->bdrv), fl_sectors);
 #endif
         pflash_cfi02_register((uint32_t)(-bios_size), bios_offset,
-                              drives_table[index].bdrv, 65536, fl_sectors, 1,
+                              dinfo->bdrv, 65536, fl_sectors, 1,
                               4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA);
         fl_idx++;
     } else
@@ -570,9 +570,9 @@ static void taihu_405ep_init(ram_addr_t ram_size,
                                      bios_size, bios_offset | IO_MEM_ROM);
     }
     /* Register Linux flash */
-    index = drive_get_index(IF_PFLASH, 0, fl_idx);
-    if (index != -1) {
-        bios_size = bdrv_getlength(drives_table[index].bdrv);
+    dinfo = drive_get(IF_PFLASH, 0, fl_idx);
+    if (dinfo) {
+        bios_size = bdrv_getlength(dinfo->bdrv);
         /* XXX: should check that size is 32MB */
         bios_size = 32 * 1024 * 1024;
         fl_sectors = (bios_size + 65535) >> 16;
@@ -580,11 +580,11 @@ static void taihu_405ep_init(ram_addr_t ram_size,
         printf("Register parallel flash %d size " ADDRX " at offset %08lx "
                " addr " ADDRX " '%s'\n",
                fl_idx, bios_size, bios_offset, (target_ulong)0xfc000000,
-               bdrv_get_device_name(drives_table[index].bdrv));
+               bdrv_get_device_name(dinfo->bdrv));
 #endif
         bios_offset = qemu_ram_alloc(bios_size);
         pflash_cfi02_register(0xfc000000, bios_offset,
-                              drives_table[index].bdrv, 65536, fl_sectors, 1,
+                              dinfo->bdrv, 65536, fl_sectors, 1,
                               4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA);
         fl_idx++;
     }
hw/ppc440_bamboo.c +3 -2
index d9ef3ec..624e15b 100644
@@ -102,6 +102,7 @@ static void bamboo_init(ram_addr_t ram_size,
     target_ulong dt_base = 0;
     void *fdt;
     int i;
+    DriveInfo *dinfo;
 
     /* Setup CPU. */
     env = ppc440ep_init(&ram_size, &pcibus, pci_irq_nrs, 1, cpu_model);
@@ -110,8 +111,8 @@ static void bamboo_init(ram_addr_t ram_size,
         int unit_id = 0;
 
         /* Add virtio block devices. */
-        while ((i = drive_get_index(IF_VIRTIO, 0, unit_id)) != -1) {
-            pci_dev = pci_create("virtio-blk-pci", drives_table[i].devaddr);
+        while ((dinfo = drive_get(IF_VIRTIO, 0, unit_id)) != NULL) {
+            pci_dev = pci_create("virtio-blk-pci", dinfo->devaddr);
             qdev_init(&pci_dev->qdev);
             unit_id++;
         }
hw/ppc_newworld.c +3 -6
index 4e5043c..caab74e 100644
@@ -106,7 +106,7 @@ static void ppc_core99_init (ram_addr_t ram_size,
     qemu_irq *dummy_irq;
     int pic_mem_index, dbdma_mem_index, cuda_mem_index, escc_mem_index;
     int ppc_boot_device;
-    int index;
+    DriveInfo *dinfo;
     BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
     void *fw_cfg;
     void *dbdma;
@@ -311,11 +311,8 @@ static void ppc_core99_init (ram_addr_t ram_size,
         exit(1);
     }
     for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
-        index = drive_get_index(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
-        if (index != -1)
-            hd[i] = drives_table[index].bdrv;
-        else
-            hd[i] = NULL;
+        dinfo = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
+        hd[i] = dinfo ? dinfo->bdrv : NULL;
     }
     dbdma = DBDMA_init(&dbdma_mem_index);
     pci_cmd646_ide_init(pci_bus, hd, 0);
hw/ppc_oldworld.c +9 -21
index b26e407..388db31 100644
@@ -135,7 +135,7 @@ static void ppc_heathrow_init (ram_addr_t ram_size,
     int escc_mem_index, ide_mem_index[2];
     uint16_t ppc_boot_device;
     BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
-    int index;
+    DriveInfo *dinfo;
     void *fw_cfg;
     void *dbdma;
     uint8_t *vga_bios_ptr;
@@ -324,31 +324,19 @@ static void ppc_heathrow_init (ram_addr_t ram_size,
     }
 
     /* First IDE channel is a MAC IDE on the MacIO bus */
-    index = drive_get_index(IF_IDE, 0, 0);
-    if (index == -1)
-        hd[0] = NULL;
-    else
-        hd[0] =  drives_table[index].bdrv;
-    index = drive_get_index(IF_IDE, 0, 1);
-    if (index == -1)
-        hd[1] = NULL;
-    else
-        hd[1] =  drives_table[index].bdrv;
+    dinfo = drive_get(IF_IDE, 0, 0);
+    hd[0] = dinfo ? dinfo->bdrv : NULL;
+    dinfo = drive_get(IF_IDE, 0, 1);
+    hd[1] = dinfo ? dinfo->bdrv : NULL;
     dbdma = DBDMA_init(&dbdma_mem_index);
     ide_mem_index[0] = -1;
     ide_mem_index[1] = pmac_ide_init(hd, pic[0x0D], dbdma, 0x16, pic[0x02]);
 
     /* Second IDE channel is a CMD646 on the PCI bus */
-    index = drive_get_index(IF_IDE, 1, 0);
-    if (index == -1)
-        hd[0] = NULL;
-    else
-        hd[0] =  drives_table[index].bdrv;
-    index = drive_get_index(IF_IDE, 1, 1);
-    if (index == -1)
-        hd[1] = NULL;
-    else
-        hd[1] =  drives_table[index].bdrv;
+    dinfo = drive_get(IF_IDE, 1, 0);
+    hd[0] = dinfo ? dinfo->bdrv : NULL;
+    dinfo = drive_get(IF_IDE, 1, 1);
+    hd[1] = dinfo ? dinfo->bdrv : NULL;
     hd[3] = hd[2] = NULL;
     pci_cmd646_ide_init(pci_bus, hd, 0);
 
hw/ppc_prep.c +5 -11
index 7a21977..97190a2 100644
@@ -550,7 +550,7 @@ static void ppc_prep_init (ram_addr_t ram_size,
     PCIBus *pci_bus;
     qemu_irq *i8259;
     int ppc_boot_device;
-    int index;
+    DriveInfo *dinfo;
     BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
     BlockDriverState *fd[MAX_FD];
 
@@ -691,11 +691,8 @@ static void ppc_prep_init (ram_addr_t ram_size,
     }
 
     for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
-        index = drive_get_index(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
-        if (index != -1)
-            hd[i] = drives_table[index].bdrv;
-        else
-            hd[i] = NULL;
+        dinfo = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
+        hd[i] = dinfo ? dinfo->bdrv : NULL;
     }
 
     for(i = 0; i < MAX_IDE_BUS; i++) {
@@ -708,11 +705,8 @@ static void ppc_prep_init (ram_addr_t ram_size,
     //    SB16_init();
 
     for(i = 0; i < MAX_FD; i++) {
-        index = drive_get_index(IF_FLOPPY, 0, i);
-        if (index != -1)
-            fd[i] = drives_table[index].bdrv;
-        else
-            fd[i] = NULL;
+        dinfo = drive_get(IF_FLOPPY, 0, i);
+        fd[i] = dinfo ? dinfo->bdrv : NULL;
     }
     fdctrl_init(i8259[6], 2, 0, 0x3f0, fd);
 
hw/ppce500_mpc8544ds.c +3 -2
index c0e367d..04c6acd 100644
@@ -172,6 +172,7 @@ static void mpc8544ds_init(ram_addr_t ram_size,
     unsigned int pci_irq_nrs[4] = {1, 2, 3, 4};
     qemu_irq *irqs, *mpic, *pci_irqs;
     SerialState * serial[2];
+    DriveInfo *dinfo;
 
     /* Setup CPU */
     env = cpu_ppc_init("e500v2_v30");
@@ -219,8 +220,8 @@ static void mpc8544ds_init(ram_addr_t ram_size,
         int unit_id = 0;
 
         /* Add virtio block devices. */
-        while ((i = drive_get_index(IF_VIRTIO, 0, unit_id)) != -1) {
-            pci_dev = pci_create("virtio-blk-pci", drives_table[i].devaddr);
+        while ((dinfo = drive_get(IF_VIRTIO, 0, unit_id)) != NULL) {
+            pci_dev = pci_create("virtio-blk-pci", dinfo->devaddr);
             qdev_init(&pci_dev->qdev);
             unit_id++;
         }
hw/pxa2xx.c +8 -8
index f2e98ff..28e9610 100644
@@ -2034,7 +2034,7 @@ PXA2xxState *pxa270_init(unsigned int sdram_size, const char *revision)
 {
     PXA2xxState *s;
     int iomemtype, i;
-    int index;
+    DriveInfo *dinfo;
     s = (PXA2xxState *) qemu_mallocz(sizeof(PXA2xxState));
 
     if (revision && strncmp(revision, "pxa27", 5)) {
@@ -2066,12 +2066,12 @@ PXA2xxState *pxa270_init(unsigned int sdram_size, const char *revision)
 
     s->gpio = pxa2xx_gpio_init(0x40e00000, s->env, s->pic, 121);
 
-    index = drive_get_index(IF_SD, 0, 0);
-    if (index == -1) {
+    dinfo = drive_get(IF_SD, 0, 0);
+    if (!dinfo) {
         fprintf(stderr, "qemu: missing SecureDigital device\n");
         exit(1);
     }
-    s->mmc = pxa2xx_mmci_init(0x41100000, drives_table[index].bdrv,
+    s->mmc = pxa2xx_mmci_init(0x41100000, dinfo->bdrv,
                               s->pic[PXA2XX_PIC_MMC], s->dma);
 
     for (i = 0; pxa270_serial[i].io_base; i ++)
@@ -2153,7 +2153,7 @@ PXA2xxState *pxa255_init(unsigned int sdram_size)
 {
     PXA2xxState *s;
     int iomemtype, i;
-    int index;
+    DriveInfo *dinfo;
 
     s = (PXA2xxState *) qemu_mallocz(sizeof(PXA2xxState));
 
@@ -2178,12 +2178,12 @@ PXA2xxState *pxa255_init(unsigned int sdram_size)
 
     s->gpio = pxa2xx_gpio_init(0x40e00000, s->env, s->pic, 85);
 
-    index = drive_get_index(IF_SD, 0, 0);
-    if (index == -1) {
+    dinfo = drive_get(IF_SD, 0, 0);
+    if (!dinfo) {
         fprintf(stderr, "qemu: missing SecureDigital device\n");
         exit(1);
     }
-    s->mmc = pxa2xx_mmci_init(0x41100000, drives_table[index].bdrv,
+    s->mmc = pxa2xx_mmci_init(0x41100000, dinfo->bdrv,
                               s->pic[PXA2XX_PIC_MMC], s->dma);
 
     for (i = 0; pxa255_serial[i].io_base; i ++)
hw/qdev.c +7 -10
index faecc76..c3ddd66 100644
@@ -189,13 +189,10 @@ static int next_block_unit[IF_COUNT];
 BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type)
 {
     int unit = next_block_unit[type]++;
-    int index;
+    DriveInfo *dinfo;
 
-    index = drive_get_index(type, 0, unit);
-    if (index == -1) {
-        return NULL;
-    }
-    return drives_table[index].bdrv;
+    dinfo = drive_get(type, 0, unit);
+    return dinfo ? dinfo->bdrv : NULL;
 }
 
 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
@@ -218,14 +215,14 @@ void scsi_bus_new(DeviceState *host, SCSIAttachFn attach)
 {
    int bus = next_scsi_bus++;
    int unit;
-   int index;
+   DriveInfo *dinfo;
 
    for (unit = 0; unit < MAX_SCSI_DEVS; unit++) {
-       index = drive_get_index(IF_SCSI, bus, unit);
-       if (index == -1) {
+       dinfo = drive_get(IF_SCSI, bus, unit);
+       if (!dinfo) {
            continue;
        }
-       attach(host, drives_table[index].bdrv, unit);
+       attach(host, dinfo->bdrv, unit);
    }
 }
 
hw/r2d.c +3 -2
index 8ce6832..697bcb6 100644
@@ -203,6 +203,7 @@ static void r2d_init(ram_addr_t ram_size,
     ram_addr_t sdram_addr;
     qemu_irq *irq;
     PCIBus *pci;
+    DriveInfo *dinfo;
     int i;
 
     if (!cpu_model)
@@ -225,9 +226,9 @@ static void r2d_init(ram_addr_t ram_size,
     sm501_init(0x10000000, SM501_VRAM_SIZE, irq[SM501], serial_hds[2]);
 
     /* onboard CF (True IDE mode, Master only). */
-    if ((i = drive_get_index(IF_IDE, 0, 0)) != -1)
+    if ((dinfo = drive_get(IF_IDE, 0, 0)) != NULL)
 	mmio_ide_init(0x14001000, 0x1400080c, irq[CF_IDE], 1,
-		      drives_table[i].bdrv, NULL);
+		      dinfo->bdrv, NULL);
 
     /* NIC: rtl8139 on-board, and 2 slots. */
     for (i = 0; i < nb_nics; i++)
hw/spitz.c +4 -4
index d9ff85e..36bf534 100644
@@ -744,13 +744,13 @@ static void spitz_ssp_attach(PXA2xxState *cpu)
 static void spitz_microdrive_attach(PXA2xxState *cpu, int slot)
 {
     PCMCIACardState *md;
-    int index;
     BlockDriverState *bs;
+    DriveInfo *dinfo;
 
-    index = drive_get_index(IF_IDE, 0, 0);
-    if (index == -1)
+    dinfo = drive_get(IF_IDE, 0, 0);
+    if (!dinfo)
         return;
-    bs = drives_table[index].bdrv;
+    bs = dinfo->bdrv;
     if (bdrv_is_inserted(bs) && !bdrv_is_removable(bs)) {
         md = dscm1xxxx_init(bs);
         pxa2xx_pcmcia_attach(cpu->pcmcia[slot], md);
hw/sun4m.c +8 -8
index c810b35..6d42caf 100644
@@ -580,9 +580,9 @@ static void sun4m_hw_init(const struct sun4m_hwdef *hwdef, ram_addr_t RAM_size,
     qemu_irq *cpu_halt;
     unsigned long kernel_size;
     BlockDriverState *fd[MAX_FD];
-    int drive_index;
     void *fw_cfg;
     DeviceState *dev;
+    DriveInfo *dinfo;
 
     /* init CPUs */
     if (!cpu_model)
@@ -661,9 +661,9 @@ static void sun4m_hw_init(const struct sun4m_hwdef *hwdef, ram_addr_t RAM_size,
     if (hwdef->fd_base) {
         /* there is zero or one floppy drive */
         memset(fd, 0, sizeof(fd));
-        drive_index = drive_get_index(IF_FLOPPY, 0, 0);
-        if (drive_index != -1)
-            fd[0] = drives_table[drive_index].bdrv;
+        dinfo = drive_get(IF_FLOPPY, 0, 0);
+        if (dinfo)
+            fd[0] = dinfo->bdrv;
 
         sun4m_fdctrl_init(slavio_irq[hwdef->fd_irq], hwdef->fd_base, fd,
                           &fdc_tc);
@@ -1498,8 +1498,8 @@ static void sun4c_hw_init(const struct sun4c_hwdef *hwdef, ram_addr_t RAM_size,
     qemu_irq fdc_tc;
     unsigned long kernel_size;
     BlockDriverState *fd[MAX_FD];
-    int drive_index;
     void *fw_cfg;
+    DriveInfo *dinfo;
 
     /* init CPU */
     if (!cpu_model)
@@ -1551,9 +1551,9 @@ static void sun4c_hw_init(const struct sun4c_hwdef *hwdef, ram_addr_t RAM_size,
     if (hwdef->fd_base != (target_phys_addr_t)-1) {
         /* there is zero or one floppy drive */
         memset(fd, 0, sizeof(fd));
-        drive_index = drive_get_index(IF_FLOPPY, 0, 0);
-        if (drive_index != -1)
-            fd[0] = drives_table[drive_index].bdrv;
+        dinfo = drive_get(IF_FLOPPY, 0, 0);
+        if (dinfo)
+            fd[0] = dinfo->bdrv;
 
         sun4m_fdctrl_init(slavio_irq[hwdef->fd_irq], hwdef->fd_base, fd,
                           &fdc_tc);
hw/sun4u.c +6 -12
index 9d2a7f5..1fba1a4 100644
@@ -358,11 +358,11 @@ static void sun4uv_init(ram_addr_t RAM_size,
     PCIBus *pci_bus, *pci_bus2, *pci_bus3;
     QEMUBH *bh;
     qemu_irq *irq;
-    int drive_index;
     BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
     BlockDriverState *fd[MAX_FD];
     void *fw_cfg;
     ResetData *reset_info;
+    DriveInfo *dinfo;
 
     linux_boot = (kernel_filename != NULL);
 
@@ -504,12 +504,9 @@ static void sun4uv_init(ram_addr_t RAM_size,
         exit(1);
     }
     for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
-        drive_index = drive_get_index(IF_IDE, i / MAX_IDE_DEVS,
-                                      i % MAX_IDE_DEVS);
-       if (drive_index != -1)
-           hd[i] = drives_table[drive_index].bdrv;
-       else
-           hd[i] = NULL;
+        dinfo = drive_get(IF_IDE, i / MAX_IDE_DEVS,
+                          i % MAX_IDE_DEVS);
+        hd[i] = dinfo ? dinfo->bdrv : NULL;
     }
 
     pci_cmd646_ide_init(pci_bus, hd, 1);
@@ -517,11 +514,8 @@ static void sun4uv_init(ram_addr_t RAM_size,
     /* FIXME: wire up interrupts.  */
     i8042_init(NULL/*1*/, NULL/*12*/, 0x60);
     for(i = 0; i < MAX_FD; i++) {
-        drive_index = drive_get_index(IF_FLOPPY, 0, i);
-       if (drive_index != -1)
-           fd[i] = drives_table[drive_index].bdrv;
-       else
-           fd[i] = NULL;
+        dinfo = drive_get(IF_FLOPPY, 0, i);
+        fd[i] = dinfo ? dinfo->bdrv : NULL;
     }
     floppy_controller = fdctrl_init(NULL/*6*/, 2, 0, 0x3f0, fd);
     nvram = m48t59_init(NULL/*8*/, 0, 0x0074, NVRAM_SIZE, 59);
hw/tosa.c +4 -4
index b2d66f3..b932efc 100644
@@ -49,13 +49,13 @@
 static void tosa_microdrive_attach(PXA2xxState *cpu)
 {
     PCMCIACardState *md;
-    int index;
     BlockDriverState *bs;
+    DriveInfo *dinfo;
 
-    index = drive_get_index(IF_IDE, 0, 0);
-    if (index == -1)
+    dinfo = drive_get(IF_IDE, 0, 0);
+    if (!dinfo)
         return;
-    bs = drives_table[index].bdrv;
+    bs = dinfo->bdrv;
     if (bdrv_is_inserted(bs) && !bdrv_is_removable(bs)) {
         md = dscm1xxxx_init(bs);
         pxa2xx_pcmcia_attach(cpu->pcmcia[0], md);
hw/xen_disk.c +7 -7
index 7ed6c33..100ef8e 100644
@@ -107,7 +107,7 @@ struct XenBlkDev {
     int                 requests_finished;
 
     /* qemu block driver */
-    int                 index;
+    DriveInfo           *dinfo;
     BlockDriverState    *bs;
     QEMUBH              *bh;
 };
@@ -575,7 +575,7 @@ static void blk_alloc(struct XenDevice *xendev)
 static int blk_init(struct XenDevice *xendev)
 {
     struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
-    int mode, qflags, have_barriers, info = 0;
+    int index, mode, qflags, have_barriers, info = 0;
     char *h;
 
     /* read xenstore entries */
@@ -622,9 +622,9 @@ static int blk_init(struct XenDevice *xendev)
 	info  |= VDISK_CDROM;
 
     /* init qemu block driver */
-    blkdev->index = (blkdev->xendev.dev - 202 * 256) / 16;
-    blkdev->index = drive_get_index(IF_XEN, 0, blkdev->index);
-    if (blkdev->index == -1) {
+    index = (blkdev->xendev.dev - 202 * 256) / 16;
+    blkdev->dinfo = drive_get(IF_XEN, 0, index);
+    if (!blkdev->dinfo) {
         /* setup via xenbus -> create new block driver instance */
         xen_be_printf(&blkdev->xendev, 2, "create new bdrv (xenbus setup)\n");
 	blkdev->bs = bdrv_new(blkdev->dev);
@@ -640,7 +640,7 @@ static int blk_init(struct XenDevice *xendev)
     } else {
         /* setup via qemu cmdline -> already setup for us */
         xen_be_printf(&blkdev->xendev, 2, "get configured bdrv (cmdline setup)\n");
-	blkdev->bs = drives_table[blkdev->index].bdrv;
+	blkdev->bs = blkdev->dinfo->bdrv;
     }
     blkdev->file_blk  = BLOCK_SIZE;
     blkdev->file_size = bdrv_getlength(blkdev->bs);
@@ -729,7 +729,7 @@ static void blk_disconnect(struct XenDevice *xendev)
     struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
 
     if (blkdev->bs) {
-        if (blkdev->index == -1) {
+        if (!blkdev->dinfo) {
             /* close/delete only if we created it ourself */
             bdrv_close(blkdev->bs);
             bdrv_delete(blkdev->bs);
hw/xen_machine_pv.c +5 -4
index 6ae5000..162f88d 100644
@@ -40,7 +40,8 @@ static void xen_init_pv(ram_addr_t ram_size,
 			const char *cpu_model)
 {
     CPUState *env;
-    int i, index;
+    DriveInfo *dinfo;
+    int i;
 
     /* Initialize a dummy CPU */
     if (cpu_model == NULL) {
@@ -90,10 +91,10 @@ static void xen_init_pv(ram_addr_t ram_size,
 
     /* configure disks */
     for (i = 0; i < 16; i++) {
-        index = drive_get_index(IF_XEN, 0, i);
-        if (index == -1)
+        dinfo = drive_get(IF_XEN, 0, i);
+        if (!dinfo)
             continue;
-        xen_config_dev_blk(drives_table + index);
+        xen_config_dev_blk(dinfo);
     }
 
     /* configure nics */
monitor.c +7 -5
index e38c86e..9a426be 100644
@@ -244,13 +244,15 @@ static void help_cmd(Monitor *mon, const char *name)
 
 static void do_commit(Monitor *mon, const char *device)
 {
-    int i, all_devices;
+    int all_devices;
+    DriveInfo *dinfo;
 
     all_devices = !strcmp(device, "all");
-    for (i = 0; i < nb_drives; i++) {
-            if (all_devices ||
-                !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
-                bdrv_commit(drives_table[i].bdrv);
+    TAILQ_FOREACH(dinfo, &drives, next) {
+        if (!all_devices)
+            if (!strcmp(bdrv_get_device_name(dinfo->bdrv), device))
+                continue;
+        bdrv_commit(dinfo->bdrv);
     }
 }
 
qemu-char.c +3 -3
index 287e0cd..4a1c082 100644
@@ -345,9 +345,9 @@ static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
             }
         case 's':
             {
-                int i;
-                for (i = 0; i < nb_drives; i++) {
-                        bdrv_commit(drives_table[i].bdrv);
+                DriveInfo *dinfo;
+                TAILQ_FOREACH(dinfo, &drives, next) {
+                    bdrv_commit(dinfo->bdrv);
                 }
             }
             break;
savevm.c +18 -14
index 181c088..0c0b49f 100644
@@ -1011,12 +1011,12 @@ static int bdrv_has_snapshot(BlockDriverState *bs)
 static BlockDriverState *get_bs_snapshots(void)
 {
     BlockDriverState *bs;
-    int i;
+    DriveInfo *dinfo;
 
     if (bs_snapshots)
         return bs_snapshots;
-    for(i = 0; i <= nb_drives; i++) {
-        bs = drives_table[i].bdrv;
+    TAILQ_FOREACH(dinfo, &drives, next) {
+        bs = dinfo->bdrv;
         if (bdrv_can_snapshot(bs))
             goto ok;
     }
@@ -1050,9 +1050,10 @@ static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
 
 void do_savevm(Monitor *mon, const char *name)
 {
+    DriveInfo *dinfo;
     BlockDriverState *bs, *bs1;
     QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
-    int must_delete, ret, i;
+    int must_delete, ret;
     QEMUFile *f;
     int saved_vm_running;
     uint32_t vm_state_size;
@@ -1118,8 +1119,8 @@ void do_savevm(Monitor *mon, const char *name)
 
     /* create the snapshots */
 
-    for(i = 0; i < nb_drives; i++) {
-        bs1 = drives_table[i].bdrv;
+    TAILQ_FOREACH(dinfo, &drives, next) {
+        bs1 = dinfo->bdrv;
         if (bdrv_has_snapshot(bs1)) {
             if (must_delete) {
                 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
@@ -1146,10 +1147,11 @@ void do_savevm(Monitor *mon, const char *name)
 
 void do_loadvm(Monitor *mon, const char *name)
 {
+    DriveInfo *dinfo;
     BlockDriverState *bs, *bs1;
     QEMUSnapshotInfo sn;
     QEMUFile *f;
-    int i, ret;
+    int ret;
     int saved_vm_running;
 
     bs = get_bs_snapshots();
@@ -1164,8 +1166,8 @@ void do_loadvm(Monitor *mon, const char *name)
     saved_vm_running = vm_running;
     vm_stop(0);
 
-    for(i = 0; i <= nb_drives; i++) {
-        bs1 = drives_table[i].bdrv;
+    TAILQ_FOREACH(dinfo, &drives, next) {
+        bs1 = dinfo->bdrv;
         if (bdrv_has_snapshot(bs1)) {
             ret = bdrv_snapshot_goto(bs1, name);
             if (ret < 0) {
@@ -1217,8 +1219,9 @@ void do_loadvm(Monitor *mon, const char *name)
 
 void do_delvm(Monitor *mon, const char *name)
 {
+    DriveInfo *dinfo;
     BlockDriverState *bs, *bs1;
-    int i, ret;
+    int ret;
 
     bs = get_bs_snapshots();
     if (!bs) {
@@ -1226,8 +1229,8 @@ void do_delvm(Monitor *mon, const char *name)
         return;
     }
 
-    for(i = 0; i <= nb_drives; i++) {
-        bs1 = drives_table[i].bdrv;
+    TAILQ_FOREACH(dinfo, &drives, next) {
+        bs1 = dinfo->bdrv;
         if (bdrv_has_snapshot(bs1)) {
             ret = bdrv_snapshot_delete(bs1, name);
             if (ret < 0) {
@@ -1245,6 +1248,7 @@ void do_delvm(Monitor *mon, const char *name)
 
 void do_info_snapshots(Monitor *mon)
 {
+    DriveInfo *dinfo;
     BlockDriverState *bs, *bs1;
     QEMUSnapshotInfo *sn_tab, *sn;
     int nb_sns, i;
@@ -1256,8 +1260,8 @@ void do_info_snapshots(Monitor *mon)
         return;
     }
     monitor_printf(mon, "Snapshot devices:");
-    for(i = 0; i <= nb_drives; i++) {
-        bs1 = drives_table[i].bdrv;
+    TAILQ_FOREACH(dinfo, &drives, next) {
+        bs1 = dinfo->bdrv;
         if (bdrv_has_snapshot(bs1)) {
             if (bs == bs1)
                 monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
sysemu.h +7 -6
index df19f02..e2a8cf5 100644
@@ -3,6 +3,7 @@
 /* Misc. things related to the system emulator.  */
 
 #include "qemu-common.h"
+#include "sys-queue.h"
 
 #ifdef _WIN32
 #include <windows.h>
@@ -164,20 +165,19 @@ typedef struct DriveInfo {
     BlockInterfaceType type;
     int bus;
     int unit;
-    int used;
     int drive_opt_idx;
     BlockInterfaceErrorAction onerror;
     char serial[BLOCK_SERIAL_STRLEN + 1];
+    TAILQ_ENTRY(DriveInfo) next;
 } DriveInfo;
 
 #define MAX_IDE_DEVS	2
 #define MAX_SCSI_DEVS	7
 #define MAX_DRIVES 32
 
-extern int nb_drives;
-extern DriveInfo drives_table[MAX_DRIVES+1];
+extern TAILQ_HEAD(drivelist, DriveInfo) drives;
 
-extern int drive_get_index(BlockInterfaceType type, int bus, int unit);
+extern DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit);
 extern int drive_get_max_bus(BlockInterfaceType type);
 extern void drive_uninit(BlockDriverState *bdrv);
 extern void drive_remove(int index);
@@ -196,7 +196,8 @@ extern struct drive_opt drives_opt[MAX_DRIVES];
 extern int nb_drives_opt;
 
 extern int drive_add(const char *file, const char *fmt, ...);
-extern int drive_init(struct drive_opt *arg, int snapshot, void *machine);
+extern DriveInfo *drive_init(struct drive_opt *arg, int snapshot, void *machine,
+                             int *fatal_error);
 
 /* acpi */
 typedef void (*qemu_system_device_hot_add_t)(int pcibus, int slot, int state);
@@ -207,7 +208,7 @@ void qemu_system_device_hot_add(int pcibus, int slot, int state);
 
 typedef int (dev_match_fn)(void *dev_private, void *arg);
 
-int add_init_drive(const char *opts);
+DriveInfo *add_init_drive(const char *opts);
 void destroy_nic(dev_match_fn *match_fn, void *arg);
 void destroy_bdrvs(dev_match_fn *match_fn, void *arg);
 
vl.c +87 -90

401 lines not on this page · raw message

Signature · 1 line
1.6.2.5
#2 lore raw

PATCH v6 2/8 add support for drive ids.

In reply to Gerd Hoffmann

-drive accepts the new id= now, allowing to explicitely name your drives. They will show up with that name in "info block" if specified, otherwise the existing namimg scheme is used to autogenerate one.

There is also a new function to lookup drives by name. Not used yet. The plan is to link disk drivers and drives using the drive id instead of passing around pointers to BlockDriveState.

---
 sysemu.h |    2 ++
 vl.c     |   50 ++++++++++++++++++++++++++++++++------------------
 2 files changed, 34 insertions(+), 18 deletions(-)
sysemu.h +2 -0
index e2a8cf5..5bd4214 100644
@@ -161,6 +161,7 @@ typedef enum {
 
 typedef struct DriveInfo {
     BlockDriverState *bdrv;
+    char *id;
     const char *devaddr;
     BlockInterfaceType type;
     int bus;
@@ -178,6 +179,7 @@ typedef struct DriveInfo {
 extern TAILQ_HEAD(drivelist, DriveInfo) drives;
 
 extern DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit);
+extern DriveInfo *drive_get_by_id(char *id);
 extern int drive_get_max_bus(BlockInterfaceType type);
 extern void drive_uninit(BlockDriverState *bdrv);
 extern void drive_remove(int index);
vl.c +32 -18

100 lines not on this page · raw message

Signature · 1 line
1.6.2.5
#3 lore raw

PATCH v6 3/8 kill drives_opt

In reply to Gerd Hoffmann

cleanup pretty simliar to the drives_table removal patch:
- drop the table and make a linked list out of it.
- pass around struct pointers instead of table indices.

---
 hw/device-hotplug.c |   10 +++---
 sysemu.h            |   24 +++++++--------
 vl.c                |   79 +++++++++++++++++---------------------------------
 3 files changed, 43 insertions(+), 70 deletions(-)
hw/device-hotplug.c +5 -5
index 5257274..8f0dec8 100644
@@ -30,17 +30,17 @@
 
 DriveInfo *add_init_drive(const char *opts)
 {
-    int drive_opt_idx;
     int fatal_error;
     DriveInfo *dinfo;
+    DriveOpt *dopt;
 
-    drive_opt_idx = drive_add(NULL, "%s", opts);
-    if (!drive_opt_idx)
+    dopt = drive_add(NULL, "%s", opts);
+    if (!dopt)
         return NULL;
 
-    dinfo = drive_init(&drives_opt[drive_opt_idx], 0, current_machine, &fatal_error);
+    dinfo = drive_init(dopt, 0, current_machine, &fatal_error);
     if (!dinfo) {
-        drive_remove(drive_opt_idx);
+        drive_remove(dopt);
         return NULL;
     }
 
sysemu.h +11 -13
index 5bd4214..ec55877 100644
@@ -159,6 +159,12 @@ typedef enum {
 
 #define BLOCK_SERIAL_STRLEN 20
 
+typedef struct DriveOpt {
+    const char *file;
+    char opt[1024];
+    TAILQ_ENTRY(DriveOpt) next;
+} DriveOpt;
+
 typedef struct DriveInfo {
     BlockDriverState *bdrv;
     char *id;
@@ -166,7 +172,7 @@ typedef struct DriveInfo {
     BlockInterfaceType type;
     int bus;
     int unit;
-    int drive_opt_idx;
+    DriveOpt *opt;
     BlockInterfaceErrorAction onerror;
     char serial[BLOCK_SERIAL_STRLEN + 1];
     TAILQ_ENTRY(DriveInfo) next;
@@ -177,28 +183,20 @@ typedef struct DriveInfo {
 #define MAX_DRIVES 32
 
 extern TAILQ_HEAD(drivelist, DriveInfo) drives;
+extern TAILQ_HEAD(driveoptlist, DriveOpt) driveopts;
 
 extern DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit);
 extern DriveInfo *drive_get_by_id(char *id);
 extern int drive_get_max_bus(BlockInterfaceType type);
 extern void drive_uninit(BlockDriverState *bdrv);
-extern void drive_remove(int index);
+extern void drive_remove(DriveOpt *opt);
 extern const char *drive_get_serial(BlockDriverState *bdrv);
 extern BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv);
 
 BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type);
 
-struct drive_opt {
-    const char *file;
-    char opt[1024];
-    int used;
-};
-
-extern struct drive_opt drives_opt[MAX_DRIVES];
-extern int nb_drives_opt;
-
-extern int drive_add(const char *file, const char *fmt, ...);
-extern DriveInfo *drive_init(struct drive_opt *arg, int snapshot, void *machine,
+extern DriveOpt *drive_add(const char *file, const char *fmt, ...);
+extern DriveInfo *drive_init(DriveOpt *arg, int snapshot, void *machine,
                              int *fatal_error);
 
 /* acpi */
vl.c +27 -52

171 lines not on this page · raw message

Signature · 1 line
1.6.2.5
#4 lore raw

PATCH v6 4/8 move parser functions from vl.c to qemu-option.c

In reply to Gerd Hoffmann
---
 qemu-option.c |   64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-option.h |    6 +++++
 sysemu.h      |    8 +------
 vl.c          |   64 ---------------------------------------------------------
 4 files changed, 71 insertions(+), 71 deletions(-)
qemu-option.c +64 -0
index 646bbad..bb2a09e 100644
@@ -85,6 +85,70 @@ const char *get_opt_value(char *buf, int buf_size, const char *p)
     return p;
 }
 
+int get_next_param_value(char *buf, int buf_size,
+                         const char *tag, const char **pstr)
+{
+    const char *p;
+    char option[128];
+
+    p = *pstr;
+    for(;;) {
+        p = get_opt_name(option, sizeof(option), p, '=');
+        if (*p != '=')
+            break;
+        p++;
+        if (!strcmp(tag, option)) {
+            *pstr = get_opt_value(buf, buf_size, p);
+            if (**pstr == ',') {
+                (*pstr)++;
+            }
+            return strlen(buf);
+        } else {
+            p = get_opt_value(NULL, 0, p);
+        }
+        if (*p != ',')
+            break;
+        p++;
+    }
+    return 0;
+}
+
+int get_param_value(char *buf, int buf_size,
+                    const char *tag, const char *str)
+{
+    return get_next_param_value(buf, buf_size, tag, &str);
+}
+
+int check_params(char *buf, int buf_size,
+                 const char * const *params, const char *str)
+{
+    const char *p;
+    int i;
+
+    p = str;
+    while (*p != '\0') {
+        p = get_opt_name(buf, buf_size, p, '=');
+        if (*p != '=') {
+            return -1;
+        }
+        p++;
+        for (i = 0; params[i] != NULL; i++) {
+            if (!strcmp(params[i], buf)) {
+                break;
+            }
+        }
+        if (params[i] == NULL) {
+            return -1;
+        }
+        p = get_opt_value(NULL, 0, p);
+        if (*p != ',') {
+            break;
+        }
+        p++;
+    }
+    return 0;
+}
+
 /*
  * Searches an option list for an option with the given name
  */
qemu-option.h +6 -0
index 059c0a4..267abae 100644
@@ -46,6 +46,12 @@ typedef struct QEMUOptionParameter {
 
 const char *get_opt_name(char *buf, int buf_size, const char *p, char delim);
 const char *get_opt_value(char *buf, int buf_size, const char *p);
+int get_next_param_value(char *buf, int buf_size,
+                         const char *tag, const char **pstr);
+int get_param_value(char *buf, int buf_size,
+                    const char *tag, const char *str);
+int check_params(char *buf, int buf_size,
+                 const char * const *params, const char *str);
 
 
 /*
sysemu.h +1 -7
index ec55877..3748af1 100644
@@ -3,6 +3,7 @@
 /* Misc. things related to the system emulator.  */
 
 #include "qemu-common.h"
+#include "qemu-option.h"
 #include "sys-queue.h"
 
 #ifdef _WIN32
@@ -276,13 +277,6 @@ void do_usb_add(Monitor *mon, const char *devname);
 void do_usb_del(Monitor *mon, const char *devname);
 void usb_info(Monitor *mon);
 
-int get_param_value(char *buf, int buf_size,
-                    const char *tag, const char *str);
-int get_next_param_value(char *buf, int buf_size,
-                         const char *tag, const char **pstr);
-int check_params(char *buf, int buf_size,
-                 const char * const *params, const char *str);
-
 void register_devices(void);
 
 #endif
vl.c +0 -64

70 lines not on this page · raw message

Signature · 1 line
1.6.2.5
#5 lore raw

PATCH v6 5/8 qemu-option: factor out parse_option_bool

In reply to Gerd Hoffmann
---
 qemu-option.c |   34 ++++++++++++++++++++++------------
 1 files changed, 22 insertions(+), 12 deletions(-)
qemu-option.c +22 -12
index bb2a09e..8aac6ae 100644
@@ -165,6 +165,23 @@ QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list,
     return NULL;
 }
 
+static int parse_option_bool(const char *name, const char *value, int *ret)
+{
+    if (value != NULL) {
+        if (!strcmp(value, "on")) {
+            *ret = 1;
+        } else if (!strcmp(value, "off")) {
+            *ret = 0;
+        } else {
+            fprintf(stderr, "Option '%s': Use 'on' or 'off'\n", name);
+            return -1;
+        }
+    } else {
+        *ret = 1;
+    }
+    return 0;
+}
+
 /*
  * Sets the value of a parameter in a given option list. The parsing of the
  * value depends on the type of option:
@@ -185,6 +202,8 @@ QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list,
 int set_option_parameter(QEMUOptionParameter *list, const char *name,
     const char *value)
 {
+    int flag;
+
     // Find a matching parameter
     list = get_option_parameter(list, name);
     if (list == NULL) {
@@ -195,18 +214,9 @@ int set_option_parameter(QEMUOptionParameter *list, const char *name,
     // Process parameter
     switch (list->type) {
     case OPT_FLAG:
-        if (value != NULL) {
-            if (!strcmp(value, "on")) {
-                list->value.n = 1;
-            } else if (!strcmp(value, "off")) {
-                list->value.n = 0;
-            } else {
-                fprintf(stderr, "Option '%s': Use 'on' or 'off'\n", name);
-                return -1;
-            }
-        } else {
-            list->value.n = 1;
-        }
+        if (-1 == parse_option_bool(name, value, &flag))
+            return -1;
+        list->value.n = flag;
         break;
 
     case OPT_STRING:
Signature · 1 line
1.6.2.5
#6 lore raw

PATCH v6 6/8 qemu-option: factor out parse_option_size

In reply to Gerd Hoffmann
---
 qemu-option.c |   62 +++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 36 insertions(+), 26 deletions(-)
qemu-option.c +36 -26
index 8aac6ae..819b8e5 100644
@@ -182,6 +182,40 @@ static int parse_option_bool(const char *name, const char *value, int *ret)
     return 0;
 }
 
+static int parse_option_size(const char *name, const char *value, uint64_t *ret)
+{
+    char *postfix;
+    double sizef;
+
+    if (value != NULL) {
+        sizef = strtod(value, &postfix);
+        switch (*postfix) {
+        case 'T':
+            sizef *= 1024;
+        case 'G':
+            sizef *= 1024;
+        case 'M':
+            sizef *= 1024;
+        case 'K':
+        case 'k':
+            sizef *= 1024;
+        case 'b':
+        case '\0':
+            *ret = (uint64_t) sizef;
+            break;
+        default:
+            fprintf(stderr, "Option '%s' needs size as parameter\n", name);
+            fprintf(stderr, "You may use k, M, G or T suffixes for "
+                    "kilobytes, megabytes, gigabytes and terabytes.\n");
+            return -1;
+        }
+    } else {
+        fprintf(stderr, "Option '%s' needs a parameter\n", name);
+        return -1;
+    }
+    return 0;
+}
+
 /*
  * Sets the value of a parameter in a given option list. The parsing of the
  * value depends on the type of option:
@@ -229,34 +263,10 @@ int set_option_parameter(QEMUOptionParameter *list, const char *name,
         break;
 
     case OPT_SIZE:
-        if (value != NULL) {
-            double sizef = strtod(value, (char**) &value);
-
-            switch (*value) {
-            case 'T':
-                sizef *= 1024;
-            case 'G':
-                sizef *= 1024;
-            case 'M':
-                sizef *= 1024;
-            case 'K':
-            case 'k':
-                sizef *= 1024;
-            case 'b':
-            case '\0':
-                list->value.n = (uint64_t) sizef;
-                break;
-            default:
-                fprintf(stderr, "Option '%s' needs size as parameter\n", name);
-                fprintf(stderr, "You may use k, M, G or T suffixes for "
-                    "kilobytes, megabytes, gigabytes and terabytes.\n");
-                return -1;
-            }
-        } else {
-            fprintf(stderr, "Option '%s' needs a parameter\n", name);
+        if (-1 == parse_option_size(name, value, &list->value.n))
             return -1;
-        }
         break;
+
     default:
         fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
         return -1;
Signature · 1 line
1.6.2.5
#7 lore raw

PATCH v6 7/8 QemuOpts: framework for storing and parsing options.

In reply to Gerd Hoffmann

This stores device parameters in a better way than unparsed strings.

New types:

  QemuOpt       -  one key-value pair.
  QemuOpts      -  group of key-value pairs, belonging to one

device, i.e. one drive.
QemuOptsList - list of some kind of devices, i.e. all drives.

Functions are provided to work with these types. The plan is that some day we will pass around QemuOpts pointers instead of strings filled with "key1=value1,key2=value2".

Check out the next patch to see all this in action ;)

---
 qemu-option.c |  314 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-option.h |   45 ++++++++
 2 files changed, 359 insertions(+), 0 deletions(-)
qemu-option.c +314 -0
index 819b8e5..73c2175 100644
@@ -182,6 +182,24 @@ static int parse_option_bool(const char *name, const char *value, int *ret)
     return 0;
 }
 
+static int parse_option_number(const char *name, const char *value, uint64_t *ret)
+{
+    char *postfix;
+    uint64_t number;
+
+    if (value != NULL) {
+        number = strtoull(value, &postfix, 0);
+        if (*postfix != '\0') {
+            fprintf(stderr, "Option '%s' needs a number as parameter\n", name);
+            return -1;
+        }
+    } else {
+        fprintf(stderr, "Option '%s' needs a parameter\n", name);
+        return -1;
+    }
+    return 0;
+}
+
 static int parse_option_size(const char *name, const char *value, uint64_t *ret)
 {
     char *postfix;
@@ -444,3 +462,299 @@ void print_option_help(QEMUOptionParameter *list)
         list++;
     }
 }
+
+/* ------------------------------------------------------------------ */
+
+struct QemuOpt {
+    const char   *name;
+    const char   *str;
+
+    QemuOptDesc  *desc;
+    union {
+        int      bool;
+        uint64_t uint;
+    } value;
+
+    QemuOpts     *opts;
+    TAILQ_ENTRY(QemuOpt) next;
+};
+
+struct QemuOpts {
+    const char *id;
+    QemuOptsList *list;
+    TAILQ_HEAD(, QemuOpt) head;
+    TAILQ_ENTRY(QemuOpts) next;
+};
+
+static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
+{
+    QemuOpt *opt;
+
+    TAILQ_FOREACH(opt, &opts->head, next) {
+        if (strcmp(opt->name, name) != 0)
+            continue;
+        return opt;
+    }
+    return NULL;
+}
+
+const char *qemu_opt_get(QemuOpts *opts, const char *name)
+{
+    QemuOpt *opt = qemu_opt_find(opts, name);
+    return opt ? opt->str : NULL;
+}
+
+int qemu_opt_get_bool(QemuOpts *opts, const char *name, int defval)
+{
+    QemuOpt *opt = qemu_opt_find(opts, name);
+
+    if (opt == NULL)
+        return defval;
+    assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
+    return opt->value.bool;
+}
+
+uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
+{
+    QemuOpt *opt = qemu_opt_find(opts, name);
+
+    if (opt == NULL)
+        return defval;
+    assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
+    return opt->value.uint;
+}
+
+uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
+{
+    QemuOpt *opt = qemu_opt_find(opts, name);
+
+    if (opt == NULL)
+        return defval;
+    assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
+    return opt->value.uint;
+}
+
+static int qemu_opt_parse(QemuOpt *opt)
+{
+    if (opt->desc == NULL)
+        return 0;
+    switch (opt->desc->type) {
+    case QEMU_OPT_STRING:
+        /* nothing */
+        return 0;
+    case QEMU_OPT_BOOL:
+        return parse_option_bool(opt->name, opt->str, &opt->value.bool);
+    case QEMU_OPT_NUMBER:
+        return parse_option_number(opt->name, opt->str, &opt->value.uint);
+    case QEMU_OPT_SIZE:
+        return parse_option_size(opt->name, opt->str, &opt->value.uint);
+    default:
+        abort();
+    }
+}
+
+static void qemu_opt_del(QemuOpt *opt)
+{
+    TAILQ_REMOVE(&opt->opts->head, opt, next);
+    qemu_free((/* !const */ char*)opt->name);
+    qemu_free((/* !const */ char*)opt->str);
+    qemu_free(opt);
+}
+
+int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
+{
+    QemuOpt *opt;
+
+    opt = qemu_opt_find(opts, name);
+    if (!opt) {
+        QemuOptDesc *desc = opts->list->desc;
+        int i;
+
+        for (i = 0; desc[i].name != NULL; i++) {
+            if (strcmp(desc[i].name, name) == 0) {
+                break;
+            }
+        }
+        if (desc[i].name == NULL) {
+            if (i == 0) {
+                /* empty list -> allow any */;
+            } else {
+                fprintf(stderr, "option \"%s\" is not valid for %s\n",
+                        name, opts->list->name);
+                return -1;
+            }
+        }
+        opt = qemu_mallocz(sizeof(*opt));
+        opt->name = qemu_strdup(name);
+        opt->opts = opts;
+        TAILQ_INSERT_TAIL(&opts->head, opt, next);
+        if (desc[i].name != NULL) {
+            opt->desc = desc+i;
+        }
+    }
+    qemu_free((/* !const */ char*)opt->str);
+    opt->str = NULL;
+    if (value) {
+        opt->str = qemu_strdup(value);
+    }
+    if (qemu_opt_parse(opt) < 0) {
+        fprintf(stderr, "Failed to parse \"%s\" for \"%s.%s\"\n", opt->str,
+                opts->list->name, opt->name);
+        qemu_opt_del(opt);
+        return -1;
+    }
+    return 0;
+}
+
+QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
+{
+    QemuOpts *opts;
+
+    TAILQ_FOREACH(opts, &list->head, next) {
+        if (!opts->id) {
+            continue;
+        }
+        if (strcmp(opts->id, id) != 0) {
+            continue;
+        }
+        return opts;
+    }
+    return NULL;
+}
+
+QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists)
+{
+    QemuOpts *opts = NULL;
+
+    if (id) {
+        opts = qemu_opts_find(list, id);
+        if (opts != NULL) {
+            if (fail_if_exists) {
+                fprintf(stderr, "tried to create id \"%s\" twice for \"%s\"\n",
+                        id, list->name);
+                return NULL;
+            } else {
+                return opts;
+            }
+        }
+    }
+    opts = qemu_mallocz(sizeof(*opts));
+    if (id) {
+        opts->id = qemu_strdup(id);
+    }
+    opts->list = list;
+    TAILQ_INIT(&opts->head);
+    TAILQ_INSERT_TAIL(&list->head, opts, next);
+    return opts;
+}
+
+int qemu_opts_set(QemuOptsList *list, const char *id,
+                  const char *name, const char *value)
+{
+    QemuOpts *opts;
+
+    opts = qemu_opts_create(list, id, 1);
+    if (opts == NULL) {
+        fprintf(stderr, "id \"%s\" not found for \"%s\"\n",
+                id, list->name);
+        return -1;
+    }
+    return qemu_opt_set(opts, name, value);
+}
+
+void qemu_opts_del(QemuOpts *opts)
+{
+    QemuOpt *opt;
+
+    for (;;) {
+        opt = TAILQ_FIRST(&opts->head);
+        if (opt == NULL)
+            break;
+        qemu_opt_del(opt);
+    }
+    TAILQ_REMOVE(&opts->list->head, opts, next);
+    qemu_free(opts);
+}
+
+int qemu_opts_print(QemuOpts *opts, void *dummy)
+{
+    QemuOpt *opt;
+
+    fprintf(stderr, "%s: %s:", opts->list->name,
+            opts->id ? opts->id : "<noid>");
+    TAILQ_FOREACH(opt, &opts->head, next) {
+        fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
+    }
+    fprintf(stderr, "\n");
+    return 0;
+}
+
+QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *firstname)
+{
+    char option[128], value[128], *id = NULL;
+    QemuOpts *opts;
+    const char *p,*pe,*pc;
+
+    if (get_param_value(value, sizeof(value), "id", params))
+        id = qemu_strdup(value);
+    opts = qemu_opts_create(list, id, 1);
+    if (opts == NULL)
+        return NULL;
+
+    p = params;
+    for(;;) {
+        pe = strchr(p, '=');
+        pc = strchr(p, ',');
+        if (!pe || (pc && pc < pe)) {
+            /* found "foo,more" */
+            if (p == params && firstname) {
+                /* implicitly named first option */
+                pstrcpy(option, sizeof(option), firstname);
+                p = get_opt_value(value, sizeof(value), p);
+            } else {
+                /* option without value, probably a flag */
+                p = get_opt_name(option, sizeof(option), p, ',');
+                if (strncmp(p, "no", 2) == 0) {
+                    memmove(option, option+2, strlen(option+2)+1);
+                    pstrcpy(value, sizeof(value), "off");
+                } else {
+                    pstrcpy(value, sizeof(value), "on");
+                }
+            }
+        } else {
+            /* found "foo=bar,more" */
+            p = get_opt_name(option, sizeof(option), p, '=');
+            if (*p != '=') {
+                break;
+            }
+            p++;
+            p = get_opt_value(value, sizeof(value), p);
+        }
+        if (strcmp(option, "id") != 0) {
+            /* store and parse */
+            if (-1 == qemu_opt_set(opts, option, value)) {
+                qemu_opts_del(opts);
+                return NULL;
+            }
+        }
+        if (*p != ',') {
+            break;
+        }
+        p++;
+    }
+    return opts;
+}
+
+int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
+                      int abort_on_failure)
+{
+    QemuOpts *opts;
+    int rc = 0;
+
+    TAILQ_FOREACH(opts, &list->head, next) {
+        rc = func(opts, opaque);
+        if (abort_on_failure  &&  rc != 0)
+            break;
+    }
+    return rc;
+}
qemu-option.h +45 -0

55 lines not on this page · raw message

Signature · 1 line
1.6.2.5
#8 lore raw

PATCH v6 8/8 switch -drive to QemuOpts.

In reply to Gerd Hoffmann

Demo QemuOpts in action ;)

Implementing a alternative way to specify the filename should be just a few lines of code now once we decided how the cmd line syntax should look like.

---
 hw/device-hotplug.c |   12 +-
 sysemu.h            |   14 +--
 vl.c                |  308 ++++++++++++++++++++++++++++-----------------------
 3 files changed, 178 insertions(+), 156 deletions(-)
hw/device-hotplug.c +6 -6
index 8f0dec8..fd4cc3f 100644
@@ -28,19 +28,19 @@
 #include "block_int.h"
 #include "sysemu.h"
 
-DriveInfo *add_init_drive(const char *opts)
+DriveInfo *add_init_drive(const char *optstr)
 {
     int fatal_error;
     DriveInfo *dinfo;
-    DriveOpt *dopt;
+    QemuOpts *opts;
 
-    dopt = drive_add(NULL, "%s", opts);
-    if (!dopt)
+    opts = drive_add(NULL, "%s", optstr);
+    if (!opts)
         return NULL;
 
-    dinfo = drive_init(dopt, 0, current_machine, &fatal_error);
+    dinfo = drive_init(opts, current_machine, &fatal_error);
     if (!dinfo) {
-        drive_remove(dopt);
+        qemu_opts_del(opts);
         return NULL;
     }
 
sysemu.h +3 -11
index 3748af1..97d1581 100644
@@ -160,12 +160,6 @@ typedef enum {
 
 #define BLOCK_SERIAL_STRLEN 20
 
-typedef struct DriveOpt {
-    const char *file;
-    char opt[1024];
-    TAILQ_ENTRY(DriveOpt) next;
-} DriveOpt;
-
 typedef struct DriveInfo {
     BlockDriverState *bdrv;
     char *id;
@@ -173,7 +167,7 @@ typedef struct DriveInfo {
     BlockInterfaceType type;
     int bus;
     int unit;
-    DriveOpt *opt;
+    QemuOpts *opts;
     BlockInterfaceErrorAction onerror;
     char serial[BLOCK_SERIAL_STRLEN + 1];
     TAILQ_ENTRY(DriveInfo) next;
@@ -190,15 +184,13 @@ extern DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit);
 extern DriveInfo *drive_get_by_id(char *id);
 extern int drive_get_max_bus(BlockInterfaceType type);
 extern void drive_uninit(BlockDriverState *bdrv);
-extern void drive_remove(DriveOpt *opt);
 extern const char *drive_get_serial(BlockDriverState *bdrv);
 extern BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv);
 
 BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type);
 
-extern DriveOpt *drive_add(const char *file, const char *fmt, ...);
-extern DriveInfo *drive_init(DriveOpt *arg, int snapshot, void *machine,
-                             int *fatal_error);
+extern QemuOpts *drive_add(const char *file, const char *fmt, ...);
+extern DriveInfo *drive_init(QemuOpts *arg, void *machine, int *fatal_error);
 
 /* acpi */
 typedef void (*qemu_system_device_hot_add_t)(int pcibus, int slot, int state);
vl.c +169 -139

498 lines not on this page · raw message

Signature · 1 line
1.6.2.5