Skip to contentSkip to conversation

smbios cleanup & nicer defaults for type 1

PATCH Superseded Markus Armbruster 2 participants 17 messages Jul 17, 2013 – Jul 30, 2013

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover smbios cleanup & nicer defaults for type 1

This gets rid of one of the last get_param_value() users, makes multiple -smbios work sanely, cleans up the gross side effect in qemu_uuid_parse(), and more. Topped off with a little feature in the last patch.

Markus Armbruster (7):
smbios: Normalize smbios_entry_add()'s error handling to exit(1)
smbios: Convert to QemuOpts
smbios: Improve diagnostics for conflicting entries
smbios: Make multiple -smbios type= accumulate sanely
smbios: Factor out smbios_maybe_add_str()
vl: Set current_machine early
smbios: Set system manufacturer, product & version by default

 arch_init.c                |   9 +-
 hw/i386/pc.c               |   6 +-
 hw/i386/pc_piix.c          |   5 +
 hw/i386/pc_q35.c           |   3 +
 hw/i386/smbios.c           | 349 ++++++++++++++++++++++++++++++++-------------
 include/hw/i386/pc.h       |   1 +
 include/hw/i386/smbios.h   |   7 +-
 include/sysemu/arch_init.h |   2 +-
 include/sysemu/sysemu.h    |   1 +
 vl.c                       |   8 +-
 10 files changed, 276 insertions(+), 115 deletions(-)
Signature · 1 line
1.7.11.7
#1 lore raw

PATCH 1/7 smbios: Normalize smbios_entry_add()'s error handling to exit(1)

It exits on all error conditions but one, where it returns -1. Normalize, and return void.

---
 arch_init.c              |  4 +---
 hw/i386/smbios.c         | 10 +++++-----
 include/hw/i386/smbios.h |  2 +-
 3 files changed, 7 insertions(+), 9 deletions(-)
arch_init.c +1 -3
index e9dd96f..e0ac143 100644
@@ -1096,9 +1096,7 @@ void do_acpitable_option(const QemuOpts *opts)
 void do_smbios_option(const char *optarg)
 {
 #ifdef TARGET_I386
-    if (smbios_entry_add(optarg) < 0) {
-        exit(1);
-    }
+    smbios_entry_add(optarg);
 #endif
 }
 
hw/i386/smbios.c +5 -5
index e708cb8..0608aee 100644
@@ -183,7 +183,7 @@ static void smbios_build_type_1_fields(const char *t)
                          buf, strlen(buf) + 1);
 }
 
-int smbios_entry_add(const char *t)
+void smbios_entry_add(const char *t)
 {
     char buf[1024];
 
@@ -222,7 +222,7 @@ int smbios_entry_add(const char *t)
         smbios_entries_len += sizeof(*table) + size;
         (*(uint16_t *)smbios_entries) =
                 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
-        return 0;
+        return;
     }
 
     if (get_param_value(buf, sizeof(buf), "type", t)) {
@@ -230,10 +230,10 @@ int smbios_entry_add(const char *t)
         switch (type) {
         case 0:
             smbios_build_type_0_fields(t);
-            return 0;
+            return;
         case 1:
             smbios_build_type_1_fields(t);
-            return 0;
+            return;
         default:
             error_report("Don't know how to build fields for SMBIOS type %ld",
                          type);
@@ -242,5 +242,5 @@ int smbios_entry_add(const char *t)
     }
 
     error_report("Must specify type= or file=");
-    return -1;
+    exit(1);
 }
include/hw/i386/smbios.h +1 -1
index 9babeaf..56c6108 100644
@@ -13,7 +13,7 @@
  *
  */
 
-int smbios_entry_add(const char *t);
+void smbios_entry_add(const char *t);
 void smbios_add_field(int type, int offset, const void *data, size_t len);
 uint8_t *smbios_get_table(size_t *length);
 
Signature · 1 line
1.7.11.7
Reviewed-by on v1 1/7
#2 lore raw

Re: [Qemu-devel] [PATCH 1/7] smbios: Normalize smbios_entry_add()'s error handling to exit(1)

Quoted 12 lines · Markus Armbruster

It exits on all error conditions but one, where it returns -1. Normalize, and return void.

---
 arch_init.c              |  4 +---
 hw/i386/smbios.c         | 10 +++++-----
 include/hw/i386/smbios.h |  2 +-
 3 files changed, 7 insertions(+), 9 deletions(-)

hw/i386/smbios.c int smbios_entry_add(const char *t) · line 245

Quoted 5 lines
hw/i386/smbios.c +1 -1
@@ -242,5 +242,5 @@ int smbios_entry_add(const char *t)
     }
 
     error_report("Must specify type= or file=");
-    return -1;
+    exit(1);

I hate magic numbers; wouldn't exit(EXIT_FAILURE) be more explicit at your intent? At any rate, you're not the first person to use exit(1), so:

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

PATCH 2/7 smbios: Convert to QemuOpts

In reply to Markus Armbruster

So that it can be set in config file for -readconfig.

This tightens parsing of -smbios, and makes it more consistent with other options: unknown parameters are rejected, numbers with trailing junk are rejected, when a parameter is given multiple times, last rather than first wins, ...

---
 arch_init.c                |   4 +-
 hw/i386/smbios.c           | 211 ++++++++++++++++++++++++++++++++++++---------
 include/hw/i386/smbios.h   |   4 +-
 include/sysemu/arch_init.h |   2 +-
 vl.c                       |   3 +-
 5 files changed, 180 insertions(+), 44 deletions(-)
arch_init.c +2 -2
index e0ac143..5930935 100644
@@ -1093,10 +1093,10 @@ void do_acpitable_option(const QemuOpts *opts)
 #endif
 }
 
-void do_smbios_option(const char *optarg)
+void do_smbios_option(QemuOpts *opts)
 {
 #ifdef TARGET_I386
-    smbios_entry_add(optarg);
+    smbios_entry_add(opts);
 #endif
 }
 
hw/i386/smbios.c +172 -39

293 lines not on this page · raw message

include/hw/i386/smbios.h +3 -1
index 56c6108..d9f43b7 100644
@@ -13,7 +13,9 @@
  *
  */
 
-void smbios_entry_add(const char *t);
+#include "qemu/option.h"
+
+void smbios_entry_add(QemuOpts *opts);
 void smbios_add_field(int type, int offset, const void *data, size_t len);
 uint8_t *smbios_get_table(size_t *length);
 
include/sysemu/arch_init.h +1 -1
index dece913..be71bca 100644
@@ -28,7 +28,7 @@ extern const uint32_t arch_type;
 
 void select_soundhw(const char *optarg);
 void do_acpitable_option(const QemuOpts *opts);
-void do_smbios_option(const char *optarg);
+void do_smbios_option(QemuOpts *opts);
 void cpudef_init(void);
 void audio_init(void);
 int tcg_available(void);
vl.c +2 -1
index 25b8f2f..0deadc4 100644
@@ -3565,7 +3565,8 @@ int main(int argc, char **argv, char **envp)
                 do_acpitable_option(opts);
                 break;
             case QEMU_OPTION_smbios:
-                do_smbios_option(optarg);
+                opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
+                do_smbios_option(opts);
                 break;
             case QEMU_OPTION_enable_kvm:
                 olist = qemu_find_opts("machine");
Signature · 1 line
1.7.11.7
Reviewed-by on v1 2/7
#4 lore raw

Re: [Qemu-devel] [PATCH 2/7] smbios: Convert to QemuOpts

Quoted 7 lines · Markus Armbruster

So that it can be set in config file for -readconfig.

This tightens parsing of -smbios, and makes it more consistent with other options: unknown parameters are rejected, numbers with trailing junk are rejected, when a parameter is given multiple times, last rather than first wins, ...

Quoted 9 lines
---
 arch_init.c                |   4 +-
 hw/i386/smbios.c           | 211 ++++++++++++++++++++++++++++++++++++---------
 include/hw/i386/smbios.h   |   4 +-
 include/sysemu/arch_init.h |   2 +-
 vl.c                       |   3 +-
 5 files changed, 180 insertions(+), 44 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#5 lore raw

PATCH 3/7 smbios: Improve diagnostics for conflicting entries

In reply to Markus Armbruster

We allow either tables or fields for the same type. Makes sense, because SeaBIOS uses fields only when no tables are present.

We do this by searching the SMBIOS blob for a previously added table or field. Error messages look like this:

qemu-system-x86_64: -smbios type=1,serial=42: SMBIOS type 1 table already defined, cannot add field

User needs to know that "table" is defined by -smbios file=..., and "field" by -smbios type=...

Instead of searching the blob, record additions of interest, and check that. Simpler, and makes better error messages possible:

qemu-system-x86_64: -smbios file=smbios_type_1.bin: Can't mix file= and type= for same type
qemu-system-x86_64: -smbios type=1,serial=42,serial=99: This is the conflicting setting

---
 hw/i386/smbios.c | 43 +++++++++++++++++--------------------------
 1 file changed, 17 insertions(+), 26 deletions(-)
hw/i386/smbios.c +17 -26
index a113f8b..7908c47 100644
@@ -48,6 +48,12 @@ static uint8_t *smbios_entries;
 static size_t smbios_entries_len;
 static int smbios_type4_count = 0;
 
+static struct {
+    bool seen;
+    int headertype;
+    Location loc;
+} first_opt[2];
+
 static QemuOptsList qemu_smbios_opts = {
     .name = "smbios",
     .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
@@ -159,35 +165,20 @@ uint8_t *smbios_get_table(size_t *length)
  */
 static void smbios_check_collision(int type, int entry)
 {
-    uint16_t *num_entries = (uint16_t *)smbios_entries;
-    struct smbios_header *header;
-    char *p;
-    int i;
-
-    if (!num_entries)
-        return;
-
-    p = (char *)(num_entries + 1);
-
-    for (i = 0; i < *num_entries; i++) {
-        header = (struct smbios_header *)p;
-        if (entry == SMBIOS_TABLE_ENTRY && header->type == SMBIOS_FIELD_ENTRY) {
-            struct smbios_field *field = (void *)header;
-            if (type == field->type) {
-                error_report("SMBIOS type %d field already defined, "
-                             "cannot add table", type);
-                exit(1);
-            }
-        } else if (entry == SMBIOS_FIELD_ENTRY &&
-                   header->type == SMBIOS_TABLE_ENTRY) {
-            struct smbios_structure_header *table = (void *)(header + 1);
-            if (type == table->type) {
-                error_report("SMBIOS type %d table already defined, "
-                             "cannot add field", type);
+    if (type < ARRAY_SIZE(first_opt)) {
+        if (first_opt[type].seen) {
+            if (first_opt[type].headertype != entry) {
+                error_report("Can't mix file= and type= for same type");
+                loc_push_restore(&first_opt[type].loc);
+                error_report("This is the conflicting setting");
+                loc_pop(&first_opt[type].loc);
                 exit(1);
             }
+        } else {
+            first_opt[type].seen = true;
+            first_opt[type].headertype = entry;
+            loc_save(&first_opt[type].loc);
         }
-        p += le16_to_cpu(header->length);
     }
 }
 
Signature · 1 line
1.7.11.7
Reviewed-by on v1 3/7
#6 lore raw

Re: [Qemu-devel] [PATCH 3/7] smbios: Improve diagnostics for conflicting entries

Quoted 14 lines · Markus Armbruster

We allow either tables or fields for the same type. Makes sense, because SeaBIOS uses fields only when no tables are present.

We do this by searching the SMBIOS blob for a previously added table or field. Error messages look like this:

qemu-system-x86_64: -smbios type=1,serial=42: SMBIOS type 1 table already defined, cannot add field

User needs to know that "table" is defined by -smbios file=..., and "field" by -smbios type=...

Instead of searching the blob, record additions of interest, and check that. Simpler, and makes better error messages possible:

Quoted 7 lines

qemu-system-x86_64: -smbios file=smbios_type_1.bin: Can't mix file= and type= for same type
qemu-system-x86_64: -smbios type=1,serial=42,serial=99: This is the conflicting setting

---
 hw/i386/smbios.c | 43 +++++++++++++++++--------------------------
 1 file changed, 17 insertions(+), 26 deletions(-)

Nicer message in fewer lines of code - what's not to like about it?

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#7 lore raw

PATCH 4/7 smbios: Make multiple -smbios type= accumulate sanely

In reply to Markus Armbruster

Currently, -smbios type=T,NAME=VAL,... adds one field (T,NAME) with value VAL to fw_cfg for each unique NAME. If NAME occurs multiple times, the last one's VAL is used (before the QemuOpts conversion, the first one was used).

Multiple -smbios can add multiple fields with the same (T, NAME). SeaBIOS reads all of them from fw_cfg, but uses only the first field (T, NAME). The others are ignored.

"First one wins, subsequent ones get ignored silently" isn't nice. We commonly let the last option win. Useful, because it lets you -readconfig first, then selectively override with command line options.

Clean up -smbios to work the common way. Accumulate the settings, with later ones overwriting earlier ones. Put the result into fw_cfg (no more useless duplicates).

Bonus cleanup: qemu_uuid_parse() no longer sets SMBIOS system uuid by side effect.

---
 arch_init.c              |   3 -
 hw/i386/smbios.c         | 152 ++++++++++++++++++++++++++++-------------------
 include/hw/i386/smbios.h |   1 -
 include/sysemu/sysemu.h  |   1 +
 vl.c                     |   2 +
 5 files changed, 94 insertions(+), 65 deletions(-)
arch_init.c +0 -3
index 5930935..8ea6fba 100644
@@ -1072,9 +1072,6 @@ int qemu_uuid_parse(const char *str, uint8_t *uuid)
     if (ret != 16) {
         return -1;
     }
-#ifdef TARGET_I386
-    smbios_add_field(1, offsetof(struct smbios_type_1, uuid), uuid, 16);
-#endif
     return 0;
 }
 
hw/i386/smbios.c +91 -61

236 lines not on this page · raw message

include/hw/i386/smbios.h +0 -1
index d9f43b7..b08ec71 100644
@@ -16,7 +16,6 @@
 #include "qemu/option.h"
 
 void smbios_entry_add(QemuOpts *opts);
-void smbios_add_field(int type, int offset, const void *data, size_t len);
 uint8_t *smbios_get_table(size_t *length);
 
 /*
include/sysemu/sysemu.h +1 -0
index 3caeb66..d490c99 100644
@@ -16,6 +16,7 @@ extern const char *bios_name;
 
 extern const char *qemu_name;
 extern uint8_t qemu_uuid[];
+extern bool qemu_uuid_set;
 int qemu_uuid_parse(const char *str, uint8_t *uuid);
 #define UUID_FMT "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
 
vl.c +2 -0
index 0deadc4..45b4c52 100644
@@ -254,6 +254,7 @@ uint64_t node_mem[MAX_NODES];
 unsigned long *node_cpumask[MAX_NODES];
 
 uint8_t qemu_uuid[16];
+bool qemu_uuid_set;
 
 static QEMUBootSetHandler *boot_set_handler;
 static void *boot_set_opaque;
@@ -3662,6 +3663,7 @@ int main(int argc, char **argv, char **envp)
                             " Wrong format.\n");
                     exit(1);
                 }
+                qemu_uuid_set = true;
                 break;
 	    case QEMU_OPTION_option_rom:
 		if (nb_option_roms >= MAX_OPTION_ROMS) {
Signature · 1 line
1.7.11.7
Reviewed-by on v1 4/7
#8 lore raw

Re: [Qemu-devel] [PATCH 4/7] smbios: Make multiple -smbios type= accumulate sanely

Quoted 33 lines · Markus Armbruster

Currently, -smbios type=T,NAME=VAL,... adds one field (T,NAME) with value VAL to fw_cfg for each unique NAME. If NAME occurs multiple times, the last one's VAL is used (before the QemuOpts conversion, the first one was used).

Multiple -smbios can add multiple fields with the same (T, NAME). SeaBIOS reads all of them from fw_cfg, but uses only the first field (T, NAME). The others are ignored.

"First one wins, subsequent ones get ignored silently" isn't nice. We commonly let the last option win. Useful, because it lets you -readconfig first, then selectively override with command line options.

Clean up -smbios to work the common way. Accumulate the settings, with later ones overwriting earlier ones. Put the result into fw_cfg (no more useless duplicates).

Bonus cleanup: qemu_uuid_parse() no longer sets SMBIOS system uuid by side effect.

---

     }
hw/i386/smbios.c +0 -5
-    val = qemu_opt_get(opts, "release");
-    if (val) {
-        if (sscanf(val, "%hhu.%hhu", &major, &minor) != 2) {
-            error_report("Invalid release");
-            exit(1);
@@ -348,7 +353,18 @@ void smbios_entry_add(QemuOpts *opts)
                 error_report("%s", error_get_pretty(local_err));

hw/i386/smbios.c void smbios_entry_add(QemuOpts *opts) · line 362

Quoted 10 lines
hw/i386/smbios.c +7 -1
                 exit(1);
             }
-            smbios_build_type_0_fields(opts);
+            save_opt(&type0.vendor, opts, "vendor");
+            save_opt(&type0.version, opts, "version");
+            save_opt(&type0.date, opts, "date");
+
+            val = qemu_opt_get(opts, "release");
+            if (val) {
+                if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {

sscanf() is lousy at detecting overflow. Since this is just code motion, you can get away without changing it now, but it would be nice if you at least considered switching this to use [a sane wrapper around] strtol() for proper overflow detection of user input at some point in your refactoring.

hw/i386/smbios.c void smbios_entry_add(QemuOpts *opts) · line 386

Quoted 5 lines
hw/i386/smbios.c +5 -0
+            val = qemu_opt_get(opts, "uuid");
+            if (val) {
+                if (qemu_uuid_parse(val, qemu_uuid) != 0) {
+                    error_report("Invalid UUID");
+                    exit(1);

My comments from earlier in the series about preferring exit(EXIT_FAILURE) apply here as well.

Neither issue is a show-stopper, so

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#9 lore raw

Re: [Qemu-devel] [PATCH 4/7] smbios: Make multiple -smbios type= accumulate sanely

Quoted 42 lines · Eric Blake
Quoted 41 lines · Markus Armbruster

Currently, -smbios type=T,NAME=VAL,... adds one field (T,NAME) with value VAL to fw_cfg for each unique NAME. If NAME occurs multiple times, the last one's VAL is used (before the QemuOpts conversion, the first one was used).

Multiple -smbios can add multiple fields with the same (T, NAME). SeaBIOS reads all of them from fw_cfg, but uses only the first field (T, NAME). The others are ignored.

"First one wins, subsequent ones get ignored silently" isn't nice. We commonly let the last option win. Useful, because it lets you -readconfig first, then selectively override with command line options.

Clean up -smbios to work the common way. Accumulate the settings, with later ones overwriting earlier ones. Put the result into fw_cfg (no more useless duplicates).

Bonus cleanup: qemu_uuid_parse() no longer sets SMBIOS system uuid by side effect.

---

     }
hw/i386/smbios.c +5 -6
-    val = qemu_opt_get(opts, "release");
-    if (val) {
-        if (sscanf(val, "%hhu.%hhu", &major, &minor) != 2) {
-            error_report("Invalid release");
-            exit(1);
@@ -348,7 +353,18 @@ void smbios_entry_add(QemuOpts *opts)
                 error_report("%s", error_get_pretty(local_err));
                 exit(1);
             }
-            smbios_build_type_0_fields(opts);
+            save_opt(&type0.vendor, opts, "vendor");
+            save_opt(&type0.version, opts, "version");
+            save_opt(&type0.date, opts, "date");
+
+            val = qemu_opt_get(opts, "release");
Quoted 8 lines

hw/i386/smbios.c void smbios_entry_add(QemuOpts *opts) · line 362

Quoted 2 lines
hw/i386/smbios.c +2 -0
+            if (val) {
+                if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {

sscanf() is lousy at detecting overflow. Since this is just code motion, you can get away without changing it now, but it would be nice if you at least considered switching this to use [a sane wrapper around] strtol() for proper overflow detection of user input at some point in your refactoring.

Correct use of strol() & friends is surprisingly involved, and a bit more than I bargained for when moving this code around :)

Quoted 8 lines

hw/i386/smbios.c void smbios_entry_add(QemuOpts *opts) · line 386

Quoted 5 lines
hw/i386/smbios.c +5 -0
+            val = qemu_opt_get(opts, "uuid");
+            if (val) {
+                if (qemu_uuid_parse(val, qemu_uuid) != 0) {
+                    error_report("Invalid UUID");
+                    exit(1);

My comments from earlier in the series about preferring exit(EXIT_FAILURE) apply here as well.

I very much prefer all unsuccessful exits to look the same. There are eight instances of exit(1) in this file before my series, I'm removing three, and adding six.

While I'm sympathetic to the general "symbols are nicer than magic numbers" sentiment, exit(1) is as harmless as it gets.

Quoted 3 lines

Neither issue is a show-stopper, so

Thanks for the review!

#10 lore raw

PATCH 5/7 smbios: Factor out smbios_maybe_add_str()

In reply to Markus Armbruster
---
 hw/i386/smbios.c | 61 +++++++++++++++++++++++---------------------------------
 1 file changed, 25 insertions(+), 36 deletions(-)
hw/i386/smbios.c +25 -36
index fb1b27b..a2eb9bf 100644
@@ -210,21 +210,22 @@ static void smbios_add_field(int type, int offset, const void *data, size_t len)
             cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
 }
 
-static void smbios_build_type_0_fields(void)
+static void smbios_maybe_add_str(int type, int offset, const char *data)
 {
-    if (type0.vendor) {
-        smbios_add_field(0, offsetof(struct smbios_type_0, vendor_str),
-                         type0.vendor, strlen(type0.vendor) + 1);
-    }
-    if (type0.version) {
-        smbios_add_field(0, offsetof(struct smbios_type_0, bios_version_str),
-                         type0.version, strlen(type0.version) + 1);
+    if (data) {
+        smbios_add_field(type, offset, data, strlen(data) + 1);
     }
-    if (type0.date) {
-        smbios_add_field(0, offsetof(struct smbios_type_0,
+}
+
+static void smbios_build_type_0_fields(void)
+{
+    smbios_maybe_add_str(0, offsetof(struct smbios_type_0, vendor_str),
+                         type0.vendor);
+    smbios_maybe_add_str(0, offsetof(struct smbios_type_0, bios_version_str),
+                         type0.version);
+    smbios_maybe_add_str(0, offsetof(struct smbios_type_0,
                                      bios_release_date_str),
-                         type0.date, strlen(type0.date) + 1);
-    }
+                         type0.date);
     if (type0.have_major_minor) {
         smbios_add_field(0, offsetof(struct smbios_type_0,
                                      system_bios_major_release),
@@ -237,30 +238,18 @@ static void smbios_build_type_0_fields(void)
 
 static void smbios_build_type_1_fields(void)
 {
-    if (type1.manufacturer) {
-        smbios_add_field(1, offsetof(struct smbios_type_1, manufacturer_str),
-                         type1.manufacturer, strlen(type1.manufacturer) + 1);
-    }
-    if (type1.product) {
-        smbios_add_field(1, offsetof(struct smbios_type_1, product_name_str),
-                         type1.product, strlen(type1.product) + 1);
-    }
-    if (type1.version) {
-        smbios_add_field(1, offsetof(struct smbios_type_1, version_str),
-                         type1.version, strlen(type1.version) + 1);
-    }
-    if (type1.serial) {
-        smbios_add_field(1, offsetof(struct smbios_type_1, serial_number_str),
-                         type1.serial, strlen(type1.serial) + 1);
-    }
-    if (type1.sku) {
-        smbios_add_field(1, offsetof(struct smbios_type_1, sku_number_str),
-                         type1.sku, strlen(type1.sku) + 1);
-    }
-    if (type1.family) {
-        smbios_add_field(1, offsetof(struct smbios_type_1, family_str),
-                         type1.family, strlen(type1.family) + 1);
-    }
+    smbios_maybe_add_str(1, offsetof(struct smbios_type_1, manufacturer_str),
+                         type1.manufacturer);
+    smbios_maybe_add_str(1, offsetof(struct smbios_type_1, product_name_str),
+                         type1.product);
+    smbios_maybe_add_str(1, offsetof(struct smbios_type_1, version_str),
+                         type1.version);
+    smbios_maybe_add_str(1, offsetof(struct smbios_type_1, serial_number_str),
+                         type1.serial);
+    smbios_maybe_add_str(1, offsetof(struct smbios_type_1, sku_number_str),
+                         type1.sku);
+    smbios_maybe_add_str(1, offsetof(struct smbios_type_1, family_str),
+                         type1.family);
     if (qemu_uuid_set) {
         smbios_add_field(1, offsetof(struct smbios_type_1, uuid),
                          qemu_uuid, 16);
Signature · 1 line
1.7.11.7
Reviewed-by on v1 5/7
#11 lore raw

Re: [Qemu-devel] [PATCH 5/7] smbios: Factor out smbios_maybe_add_str()

Quoted 4 lines · Markus Armbruster
---
 hw/i386/smbios.c | 61 +++++++++++++++++++++++---------------------------------
 1 file changed, 25 insertions(+), 36 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#12 lore raw

PATCH 6/7 vl: Set current_machine early

In reply to Markus Armbruster

I'd like to access QEMUMachine from a QEMUMachine init() method, which is currently not possible. Instead of passing it as an argument, I simply set current_machine earlier.

---
 vl.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
vl.c +1 -2
index 45b4c52..9387662 100644
@@ -3897,6 +3897,7 @@ int main(int argc, char **argv, char **envp)
         fprintf(stderr, "No machine found.\n");
         exit(1);
     }
+    current_machine = machine;
 
     if (machine->hw_version) {
         qemu_set_version(machine->hw_version);
@@ -4325,8 +4326,6 @@ int main(int argc, char **argv, char **envp)
 
     set_numa_modes();
 
-    current_machine = machine;
-
     /* init USB devices */
     if (usb_enabled(false)) {
         if (foreach_device_config(DEV_USB, usb_parse) < 0)
Signature · 1 line
1.7.11.7
Reviewed-by on v1 6/7
#13 lore raw

Re: [Qemu-devel] [PATCH 6/7] vl: Set current_machine early

Quoted 8 lines · Markus Armbruster

I'd like to access QEMUMachine from a QEMUMachine init() method, which is currently not possible. Instead of passing it as an argument, I simply set current_machine earlier.

---
 vl.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#14 lore raw

PATCH 7/7 smbios: Set system manufacturer, product & version by default

In reply to Markus Armbruster

Currently, we get SeaBIOS defaults: manufacturer Bochs, product Bochs, no version. Best SeaBIOS can do, but we can provide better defaults:
manufacturer QEMU, product & version taken from QEMUMachine desc and name.

Take care to do this only for new machine types, of course.

---
 hw/i386/pc.c             |  6 +++---
 hw/i386/pc_piix.c        |  5 +++++
 hw/i386/pc_q35.c         |  3 +++
 hw/i386/smbios.c         | 12 +++++++++++-
 include/hw/i386/pc.h     |  1 +
 include/hw/i386/smbios.h |  2 +-
 6 files changed, 24 insertions(+), 5 deletions(-)
hw/i386/pc.c +3 -3
index c5d8570..c427f9f 100644
@@ -605,7 +605,7 @@ static unsigned int pc_apic_id_limit(unsigned int max_cpus)
     return x86_cpu_apic_id_from_index(max_cpus - 1) + 1;
 }
 
-static FWCfgState *bochs_bios_init(void)
+static FWCfgState *bochs_bios_init(bool smbios_type1_defaults)
 {
     FWCfgState *fw_cfg;
     uint8_t *smbios_table;
@@ -636,7 +636,7 @@ static FWCfgState *bochs_bios_init(void)
                      acpi_tables, acpi_tables_len);
     fw_cfg_add_i32(fw_cfg, FW_CFG_IRQ0_OVERRIDE, kvm_allows_irq0_override());
 
-    smbios_table = smbios_get_table(&smbios_len);
+    smbios_table = smbios_get_table(&smbios_len, smbios_type1_defaults);
     if (smbios_table)
         fw_cfg_add_bytes(fw_cfg, FW_CFG_SMBIOS_ENTRIES,
                          smbios_table, smbios_len);
@@ -1146,7 +1146,7 @@ FWCfgState *pc_memory_init(MemoryRegion *system_memory,
                                         option_rom_mr,
                                         1);
 
-    fw_cfg = bochs_bios_init();
+    fw_cfg = bochs_bios_init(guest_info->smbios_type1_defaults);
     rom_set_fw(fw_cfg);
 
     if (linux_boot) {
hw/i386/pc_piix.c +5 -0
index b58c255..60bf752 100644
@@ -58,6 +58,7 @@ static const int ide_irq[MAX_IDE_BUS] = { 14, 15 };
 
 static bool has_pvpanic = true;
 static bool has_pci_info = true;
+static bool smbios_type1_defaults = true;
 
 /* PC hardware initialisation */
 static void pc_init1(MemoryRegion *system_memory,
@@ -128,6 +129,7 @@ static void pc_init1(MemoryRegion *system_memory,
 
     guest_info = pc_guest_info_init(below_4g_mem_size, above_4g_mem_size);
     guest_info->has_pci_info = has_pci_info;
+    guest_info->smbios_type1_defaults = smbios_type1_defaults;
 
     /* Set PCI window size the way seabios has always done it. */
     /* Power of 2 so bios can cover it with a single MTRR */
@@ -264,6 +266,7 @@ static void pc_init_pci(QEMUMachineInitArgs *args)
 static void pc_init_pci_1_5(QEMUMachineInitArgs *args)
 {
     has_pci_info = false;
+    smbios_type1_defaults = false;
     pc_init_pci(args);
 }
 
@@ -304,6 +307,7 @@ static void pc_init_pci_no_kvmclock(QEMUMachineInitArgs *args)
     const char *boot_device = args->boot_device;
     has_pvpanic = false;
     has_pci_info = false;
+    smbios_type1_defaults = false;
     disable_kvm_pv_eoi();
     enable_compat_apic_id_mode();
     pc_init1(get_system_memory(),
@@ -323,6 +327,7 @@ static void pc_init_isa(QEMUMachineInitArgs *args)
     const char *boot_device = args->boot_device;
     has_pvpanic = false;
     has_pci_info = false;
+    smbios_type1_defaults = false;
     if (cpu_model == NULL)
         cpu_model = "486";
     disable_kvm_pv_eoi();
hw/i386/pc_q35.c +3 -0
index 6f10246..c756474 100644
@@ -48,6 +48,7 @@
 
 static bool has_pvpanic = true;
 static bool has_pci_info = true;
+static bool smbios_type1_defaults = true;
 
 /* PC hardware initialisation */
 static void pc_q35_init(QEMUMachineInitArgs *args)
@@ -109,6 +110,7 @@ static void pc_q35_init(QEMUMachineInitArgs *args)
 
     guest_info = pc_guest_info_init(below_4g_mem_size, above_4g_mem_size);
     guest_info->has_pci_info = has_pci_info;
+    guest_info->smbios_type1_defaults = smbios_type1_defaults;
 
     /* allocate ram and load rom/bios */
     if (!xen_enabled()) {
@@ -217,6 +219,7 @@ static void pc_q35_init(QEMUMachineInitArgs *args)
 static void pc_q35_init_1_5(QEMUMachineInitArgs *args)
 {
     has_pci_info = false;
+    smbios_type1_defaults = false;
     pc_q35_init(args);
 }
 
hw/i386/smbios.c +11 -1
index a2eb9bf..e6413a5 100644
@@ -18,6 +18,7 @@
 #include "qemu/config-file.h"
 #include "qemu/error-report.h"
 #include "sysemu/sysemu.h"
+#include "hw/boards.h"
 #include "hw/i386/smbios.h"
 #include "hw/loader.h"
 
@@ -256,9 +257,18 @@ static void smbios_build_type_1_fields(void)
     }
 }
 
-uint8_t *smbios_get_table(size_t *length)
+uint8_t *smbios_get_table(size_t *length, bool type1_defaults)
 {
     if (!smbios_immutable) {
+        if (type1_defaults && !type1.manufacturer) {
+            type1.manufacturer = "QEMU";
+        }
+        if (type1_defaults && !type1.product) {
+            type1.product = current_machine->desc;
+        }
+        if (type1_defaults && !type1.version) {
+            type1.version = current_machine->name;
+        }
         smbios_build_type_0_fields();
         smbios_build_type_1_fields();
         smbios_validate_table();
include/hw/i386/pc.h +1 -0
index 61ff154..860f2bb 100644
@@ -20,6 +20,7 @@ typedef struct PcPciInfo {
 struct PcGuestInfo {
     PcPciInfo pci_info;
     bool has_pci_info;
+    bool smbios_type1_defaults;
     FWCfgState *fw_cfg;
 };
 
include/hw/i386/smbios.h +1 -1
index b08ec71..e258d9a 100644
@@ -16,7 +16,7 @@
 #include "qemu/option.h"
 
 void smbios_entry_add(QemuOpts *opts);
-uint8_t *smbios_get_table(size_t *length);
+uint8_t *smbios_get_table(size_t *length, bool type1_defaults);
 
 /*
  * SMBIOS spec defined tables
Signature · 1 line
1.7.11.7
Reviewed-by on v1 7/7
#15 lore raw

Re: [Qemu-devel] [PATCH 7/7] smbios: Set system manufacturer, product & version by default

Quoted 8 lines · Markus Armbruster

Currently, we get SeaBIOS defaults: manufacturer Bochs, product Bochs, no version. Best SeaBIOS can do, but we can provide better defaults:
manufacturer QEMU, product & version taken from QEMUMachine desc and name.

Take care to do this only for new machine types, of course.

Quoted 9 lines
---
 hw/i386/pc.c             |  6 +++---
 hw/i386/pc_piix.c        |  5 +++++
 hw/i386/pc_q35.c         |  3 +++
 hw/i386/smbios.c         | 12 +++++++++++-
 include/hw/i386/pc.h     |  1 +
 include/hw/i386/smbios.h |  2 +-
 6 files changed, 24 insertions(+), 5 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#16 lore raw
In reply to Markus Armbruster

Ping for 1.6

Quoted 25 lines · Markus Armbruster

This gets rid of one of the last get_param_value() users, makes multiple -smbios work sanely, cleans up the gross side effect in qemu_uuid_parse(), and more. Topped off with a little feature in the last patch.

Markus Armbruster (7):
smbios: Normalize smbios_entry_add()'s error handling to exit(1)
smbios: Convert to QemuOpts
smbios: Improve diagnostics for conflicting entries
smbios: Make multiple -smbios type= accumulate sanely
smbios: Factor out smbios_maybe_add_str()
vl: Set current_machine early
smbios: Set system manufacturer, product & version by default

 arch_init.c                |   9 +-
 hw/i386/pc.c               |   6 +-
 hw/i386/pc_piix.c          |   5 +
 hw/i386/pc_q35.c           |   3 +
 hw/i386/smbios.c           | 349 ++++++++++++++++++++++++++++++++-------------
 include/hw/i386/pc.h       |   1 +
 include/hw/i386/smbios.h   |   7 +-
 include/sysemu/arch_init.h |   2 +-
 include/sysemu/sysemu.h    |   1 +
 vl.c                       |   8 +-
 10 files changed, 276 insertions(+), 115 deletions(-)