qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH v2 13/14] cmdline: convert -smbios and -uuid to Qemu


From: Paolo Bonzini
Subject: [Qemu-devel] [PATCH v2 13/14] cmdline: convert -smbios and -uuid to QemuOpts
Date: Wed, 11 Apr 2012 23:34:40 +0200

Signed-off-by: Paolo Bonzini <address@hidden>
---
 arch_init.c   |   37 +++-----------
 arch_init.h   |    1 -
 hw/smbios.c   |  155 +++++++++++++++++++++++++++++++++++++++++++++++---------
 hw/smbios.h   |    4 +-
 qemu-config.c |   13 +++++
 vl.c          |   39 ++++++++++++---
 6 files changed, 185 insertions(+), 64 deletions(-)

diff --git a/arch_init.c b/arch_init.c
index 9a081cf..a3bd307 100644
--- a/arch_init.c
+++ b/arch_init.c
@@ -650,44 +650,23 @@ void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
 }
 #endif
 
-int qemu_uuid_parse(const char *str, uint8_t *uuid)
-{
-    int ret;
-
-    if (strlen(str) != 36) {
-        return -1;
-    }
-
-    ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
-                 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
-                 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
-                 &uuid[15]);
-
-    if (ret != 16) {
-        return -1;
-    }
-#ifdef TARGET_I386
-    smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
-#endif
-    return 0;
-}
-
 #ifndef TARGET_I386
 int acpi_table_add(QemuOpts *opts, void *opaque)
 {
     abort();
 }
-#endif
 
-void do_smbios_option(const char *optarg)
+int smbios_entry_add(QemuOpts *opts, void *opaque)
 {
-#ifdef TARGET_I386
-    if (smbios_entry_add(optarg) < 0) {
-        fprintf(stderr, "Wrong smbios provided\n");
-        exit(1);
+    const char *p;
+    p = qemu_opt_get(opts, "uuid");
+    if (p && qemu_uuid_parse(p, qemu_uuid) == -1) {
+        fprintf(stderr, "Fail to parse UUID string. Wrong format.\n");
+        return -1;
     }
-#endif
+    return 0;
 }
+#endif
 
 void cpudef_init(void)
 {
diff --git a/arch_init.h b/arch_init.h
index 82df01f..a3d675e 100644
--- a/arch_init.h
+++ b/arch_init.h
@@ -23,7 +23,6 @@ enum {
 extern const uint32_t arch_type;
 
 void select_soundhw(const char *optarg);
-void do_smbios_option(const char *optarg);
 void cpudef_init(void);
 int audio_available(void);
 void audio_init(ISABus *isa_bus, PCIBus *pci_bus);
diff --git a/hw/smbios.c b/hw/smbios.c
index c57237d..aca115a 100644
--- a/hw/smbios.c
+++ b/hw/smbios.c
@@ -14,6 +14,7 @@
  */
 
 #include "sysemu.h"
+#include "arch_init.h"
 #include "smbios.h"
 #include "loader.h"
 
@@ -99,7 +100,7 @@ static void smbios_check_collision(int type, int entry)
     }
 }
 
-void smbios_add_field(int type, int offset, int len, void *data)
+void smbios_add_field(int type, int offset, int len, const void *data)
 {
     struct smbios_field *field;
 
@@ -124,21 +125,56 @@ void smbios_add_field(int type, int offset, int len, void 
*data)
             cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
 }
 
-static void smbios_build_type_0_fields(const char *t)
+static QemuOptDesc smbios_type_0_desc[] = {
+    {
+        .name = "type",
+        .type = QEMU_OPT_NUMBER,
+    },
+    {
+        .name = "vendor",
+        .type = QEMU_OPT_STRING,
+    },
+    {
+        .name = "version",
+        .type = QEMU_OPT_STRING,
+    },
+    {
+        .name = "date",
+        .type = QEMU_OPT_STRING,
+    },
+    {
+        .name = "release",
+        .type = QEMU_OPT_STRING,
+    },
+    { /* end of list */ }
+};
+
+static int smbios_build_type_0_fields(QemuOpts *opts)
 {
-    char buf[1024];
+    const char *buf;
 
-    if (get_param_value(buf, sizeof(buf), "vendor", t))
+    if (qemu_opts_validate(opts, smbios_type_0_desc) == -1) {
+        return -1;
+    }
+
+    buf = qemu_opt_get(opts, "vendor");
+    if (buf) {
         smbios_add_field(0, offsetof(struct smbios_type_0, vendor_str),
                          strlen(buf) + 1, buf);
-    if (get_param_value(buf, sizeof(buf), "version", t))
+    }
+    buf = qemu_opt_get(opts, "version");
+    if (buf) {
         smbios_add_field(0, offsetof(struct smbios_type_0, bios_version_str),
                          strlen(buf) + 1, buf);
-    if (get_param_value(buf, sizeof(buf), "date", t))
+    }
+    buf = qemu_opt_get(opts, "date");
+    if (buf) {
         smbios_add_field(0, offsetof(struct smbios_type_0,
                                      bios_release_date_str),
                                      strlen(buf) + 1, buf);
-    if (get_param_value(buf, sizeof(buf), "release", t)) {
+    }
+    buf = qemu_opt_get(opts, "release");
+    if (buf) {
         int major, minor;
         sscanf(buf, "%d.%d", &major, &minor);
         smbios_add_field(0, offsetof(struct smbios_type_0,
@@ -146,47 +182,117 @@ static void smbios_build_type_0_fields(const char *t)
         smbios_add_field(0, offsetof(struct smbios_type_0,
                                      system_bios_minor_release), 1, &minor);
     }
+    return 0;
 }
 
-static void smbios_build_type_1_fields(const char *t)
+static QemuOptDesc smbios_type_1_desc[] = {
+    {
+        .name = "type",
+        .type = QEMU_OPT_NUMBER,
+    },
+    {
+        .name = "manufacturer",
+        .type = QEMU_OPT_STRING,
+    },
+    {
+        .name = "product",
+        .type = QEMU_OPT_STRING,
+    },
+    {
+        .name = "version",
+        .type = QEMU_OPT_STRING,
+    },
+    {
+        .name = "serial",
+        .type = QEMU_OPT_STRING,
+    },
+    {
+        .name = "uuid",
+        .type = QEMU_OPT_STRING,
+    },
+    {
+        .name = "sku",
+        .type = QEMU_OPT_STRING,
+    },
+    {
+        .name = "family",
+        .type = QEMU_OPT_STRING,
+    },
+    { /* end of list */ }
+};
+
+static int smbios_build_type_1_fields(QemuOpts *opts)
 {
-    char buf[1024];
+    const char *buf;
+
+    if (qemu_opts_validate(opts, smbios_type_1_desc) == -1) {
+        return -1;
+    }
 
-    if (get_param_value(buf, sizeof(buf), "manufacturer", t))
+    buf = qemu_opt_get(opts, "manufacturer");
+    if (buf) {
         smbios_add_field(1, offsetof(struct smbios_type_1, manufacturer_str),
                          strlen(buf) + 1, buf);
-    if (get_param_value(buf, sizeof(buf), "product", t))
+    }
+    buf = qemu_opt_get(opts, "product");
+    if (buf) {
         smbios_add_field(1, offsetof(struct smbios_type_1, product_name_str),
                          strlen(buf) + 1, buf);
-    if (get_param_value(buf, sizeof(buf), "version", t))
+    }
+    buf = qemu_opt_get(opts, "version");
+    if (buf) {
         smbios_add_field(1, offsetof(struct smbios_type_1, version_str),
                          strlen(buf) + 1, buf);
-    if (get_param_value(buf, sizeof(buf), "serial", t))
+    }
+    buf = qemu_opt_get(opts, "serial");
+    if (buf) {
         smbios_add_field(1, offsetof(struct smbios_type_1, serial_number_str),
                          strlen(buf) + 1, buf);
-    if (get_param_value(buf, sizeof(buf), "uuid", t)) {
+    }
+    buf = qemu_opt_get(opts, "uuid");
+    if (buf) {
         if (qemu_uuid_parse(buf, qemu_uuid) != 0) {
             fprintf(stderr, "Invalid SMBIOS UUID string\n");
             exit(1);
         }
+        smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, 
qemu_uuid);
     }
-    if (get_param_value(buf, sizeof(buf), "sku", t))
+    buf = qemu_opt_get(opts, "sku");
+    if (buf) {
         smbios_add_field(1, offsetof(struct smbios_type_1, sku_number_str),
                          strlen(buf) + 1, buf);
-    if (get_param_value(buf, sizeof(buf), "family", t))
+    }
+    buf = qemu_opt_get(opts, "family");
+    if (buf) {
         smbios_add_field(1, offsetof(struct smbios_type_1, family_str),
                          strlen(buf) + 1, buf);
+    }
+    return 0;
 }
 
-int smbios_entry_add(const char *t)
+static QemuOptDesc smbios_file_desc[] = {
+    {
+        .name = "file",
+        .type = QEMU_OPT_STRING,
+    },
+    { /* end of list */ }
+};
+
+int smbios_entry_add(QemuOpts *opts, void *opaque)
 {
-    char buf[1024];
+    const char *buf;
 
-    if (get_param_value(buf, sizeof(buf), "file", t)) {
+    buf = qemu_opt_get(opts, "file");
+    if (buf) {
         struct smbios_structure_header *header;
         struct smbios_table *table;
-        int size = get_image_size(buf);
+        int size;
+
+        if (qemu_opts_validate(opts, smbios_file_desc) == -1) {
+            return -1;
+        }
 
+        size = get_image_size(buf);
         if (size == -1 || size < sizeof(struct smbios_structure_header)) {
             fprintf(stderr, "Cannot read smbios file %s\n", buf);
             exit(1);
@@ -220,15 +326,14 @@ int smbios_entry_add(const char *t)
         return 0;
     }
 
-    if (get_param_value(buf, sizeof(buf), "type", t)) {
+    buf = qemu_opt_get(opts, "type");
+    if (buf != NULL) {
         unsigned long type = strtoul(buf, NULL, 0);
         switch (type) {
         case 0:
-            smbios_build_type_0_fields(t);
-            return 0;
+            return smbios_build_type_0_fields(opts);
         case 1:
-            smbios_build_type_1_fields(t);
-            return 0;
+            return smbios_build_type_1_fields(opts);
         default:
             fprintf(stderr, "Don't know how to build fields for SMBIOS type "
                     "%ld\n", type);
diff --git a/hw/smbios.h b/hw/smbios.h
index 94e3641..57c43de 100644
--- a/hw/smbios.h
+++ b/hw/smbios.h
@@ -13,8 +13,8 @@
  *
  */
 
-int smbios_entry_add(const char *t);
-void smbios_add_field(int type, int offset, int len, void *data);
+int smbios_entry_add(QemuOpts *opts, void *opaque);
+void smbios_add_field(int type, int offset, int len, const void *data);
 uint8_t *smbios_get_table(size_t *length);
 
 /*
diff --git a/qemu-config.c b/qemu-config.c
index c197317..f458a89 100644
--- a/qemu-config.c
+++ b/qemu-config.c
@@ -734,6 +734,18 @@ static QemuOptsList qemu_acpitable_opts = {
     },
 };
 
+static QemuOptsList qemu_smbios_opts = {
+    .name = "smbios",
+    .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
+    .desc = {
+        /*
+         * no elements => accept any
+         * sanity checking will happen later
+         */
+        { /* end of list */ }
+    },
+};
+
 static QemuOptsList *vm_config_groups[32] = {
     &qemu_drive_opts,
     &qemu_chardev_opts,
@@ -752,6 +764,7 @@ static QemuOptsList *vm_config_groups[32] = {
     &qemu_boot_opts,
     &qemu_iscsi_opts,
     &qemu_acpitable_opts,
+    &qemu_smbios_opts,
     NULL,
 };
 
diff --git a/vl.c b/vl.c
index 810d3c3..773d1d7 100644
--- a/vl.c
+++ b/vl.c
@@ -1749,6 +1749,25 @@ static DisplayType select_display(const char *p)
     return display;
 }
 
+int qemu_uuid_parse(const char *str, uint8_t *uuid)
+{
+    int ret;
+
+    if (strlen(str) != 36) {
+        return -1;
+    }
+
+    ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
+                 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
+                 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
+                 &uuid[15]);
+
+    if (ret != 16) {
+        return -1;
+    }
+    return 0;
+}
+
 static int balloon_parse(const char *arg)
 {
     QemuOpts *opts;
@@ -2933,7 +2952,7 @@ int main(int argc, char **argv, char **envp)
                 qemu_opts_parse(qemu_find_opts("acpitable"), optarg, 0);
                 break;
             case QEMU_OPTION_smbios:
-                do_smbios_option(optarg);
+                qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
                 break;
             case QEMU_OPTION_enable_kvm:
                 olist = qemu_find_opts("machine");
@@ -2992,13 +3011,14 @@ int main(int argc, char **argv, char **envp)
             case QEMU_OPTION_show_cursor:
                 cursor_hide = 0;
                 break;
-            case QEMU_OPTION_uuid:
-                if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
-                    fprintf(stderr, "Fail to parse UUID string."
-                            " Wrong format.\n");
-                    exit(1);
-                }
+            case QEMU_OPTION_uuid: {
+                QemuOpts *smbios;
+
+                smbios = qemu_opts_create(qemu_find_opts("smbios"), NULL, 0);
+                qemu_opt_set(smbios, "type", "1");
+                qemu_opt_set(smbios, "uuid", optarg);
                 break;
+            }
            case QEMU_OPTION_option_rom:
                if (nb_option_roms >= MAX_OPTION_ROMS) {
                    fprintf(stderr, "Too many option ROMs\n");
@@ -3206,6 +3226,11 @@ int main(int argc, char **argv, char **envp)
     }
 
 
+    if (qemu_opts_foreach(qemu_find_opts("smbios"), smbios_entry_add, NULL, 1)
+       != 0) {
+        exit(1);
+    }
+
     /*
      * Default to max_cpus = smp_cpus, in case the user doesn't
      * specify a max_cpus value.
-- 
1.7.9.1





reply via email to

[Prev in Thread] Current Thread [Next in Thread]