From patchwork Tue Sep 6 08:28:33 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lin Ma X-Patchwork-Id: 9316127 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 8AE1360752 for ; Tue, 6 Sep 2016 09:04:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9770528600 for ; Tue, 6 Sep 2016 09:04:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8A39E2860C; Tue, 6 Sep 2016 09:04:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5F4F428BF4 for ; Tue, 6 Sep 2016 09:03:57 +0000 (UTC) Received: from localhost ([::1]:60136 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bhCIO-000762-Hk for patchwork-qemu-devel@patchwork.kernel.org; Tue, 06 Sep 2016 05:03:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47110) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bhBkT-0003s6-KY for qemu-devel@nongnu.org; Tue, 06 Sep 2016 04:28:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bhBkP-0001La-Dt for qemu-devel@nongnu.org; Tue, 06 Sep 2016 04:28:52 -0400 Received: from prv3-mh.provo.novell.com ([137.65.250.26]:47971) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bhBkP-0001LB-3T for qemu-devel@nongnu.org; Tue, 06 Sep 2016 04:28:49 -0400 Received: from linux-xpcv.apac.novell.com (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by prv3-mh.provo.novell.com with ESMTP (NOT encrypted); Tue, 06 Sep 2016 02:28:44 -0600 From: Lin Ma To: mst@redhat.com, imammedo@redhat.com, qemu-devel@nongnu.org Date: Tue, 6 Sep 2016 16:28:33 +0800 Message-Id: <20160906082833.25428-1-lma@suse.com> X-Mailer: git-send-email 2.9.2 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 137.65.250.26 Subject: [Qemu-devel] [PATCH] smbios: Add 1 terminator if there is any string field defined in given table. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP If user specifies binary file on command line to load smbios entries, then will get error messages while decoding them in guest. Reproducer: 1. dump a smbios table to a binary file from host or guest.(says table 1) 2. load the binary file through command line: 'qemu -smbios file=...'. 3. perform 'dmidecode' or 'dmidecode -t 1' in guest. It reports 'Invalid entry length...' because qemu doesn't add terminator(s) for the table correctly. For smbios tables which have string field provided, qemu should add 1 terminator. For smbios tables which dont have string field provided, qemu should add 2. This patch fixed the issue. Signed-off-by: Lin Ma Acked-by: Michael S. Tsirkin --- hw/smbios/smbios.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++ include/hw/smbios/smbios.h | 44 +++++++++++++++++++++++ 2 files changed, 134 insertions(+) diff --git a/hw/smbios/smbios.c b/hw/smbios/smbios.c index 74c7102..6293bc5 100644 --- a/hw/smbios/smbios.c +++ b/hw/smbios/smbios.c @@ -885,6 +885,9 @@ void smbios_entry_add(QemuOpts *opts) { const char *val; + int i, terminator_count = 2, table_str_field_count = 0; + int *tables_str_field_offset = NULL; + assert(!smbios_immutable); val = qemu_opt_get(opts, "file"); @@ -926,7 +929,94 @@ void smbios_entry_add(QemuOpts *opts) smbios_type4_count++; } + switch (header->type) { + case 0: + tables_str_field_offset = g_malloc0(sizeof(int) * \ + TYPE_0_STR_FIELD_COUNT); + tables_str_field_offset = (int []){\ + TYPE_0_STR_FIELD_OFFSET_VENDOR, \ + TYPE_0_STR_FIELD_OFFSET_BIOS_VERSION, \ + TYPE_0_STR_FIELD_OFFSET_BIOS_RELEASE_DATE}; + table_str_field_count = sizeof(tables_str_field_offset) / \ + sizeof(tables_str_field_offset[0]); + break; + case 1: + tables_str_field_offset = g_malloc0(sizeof(int) * \ + TYPE_1_STR_FIELD_COUNT); + tables_str_field_offset = (int []){ + TYPE_1_STR_FIELD_OFFSET_MANUFACTURER, \ + TYPE_1_STR_FIELD_OFFSET_PRODUCT, \ + TYPE_1_STR_FIELD_OFFSET_VERSION, \ + TYPE_1_STR_FIELD_OFFSET_SERIAL, \ + TYPE_1_STR_FIELD_OFFSET_SKU, \ + TYPE_1_STR_FIELD_OFFSET_FAMILY}; + table_str_field_count = sizeof(tables_str_field_offset) / \ + sizeof(tables_str_field_offset[0]); + break; + case 2: + tables_str_field_offset = g_malloc0(sizeof(int) * \ + TYPE_2_STR_FIELD_COUNT); + tables_str_field_offset = (int []){\ + TYPE_2_STR_FIELD_OFFSET_MANUFACTURER, \ + TYPE_2_STR_FIELD_OFFSET_PRODUCT, \ + TYPE_2_STR_FIELD_OFFSET_VERSION, \ + TYPE_2_STR_FIELD_OFFSET_SERIAL, \ + TYPE_2_STR_FIELD_OFFSET_ASSET, \ + TYPE_2_STR_FIELD_OFFSET_LOCATION}; + table_str_field_count = sizeof(tables_str_field_offset) / \ + sizeof(tables_str_field_offset[0]); + break; + case 3: + tables_str_field_offset = g_malloc0(sizeof(int) * \ + TYPE_3_STR_FIELD_COUNT); + tables_str_field_offset = (int []){\ + TYPE_3_STR_FIELD_OFFSET_MANUFACTURER, \ + TYPE_3_STR_FIELD_OFFSET_VERSION, \ + TYPE_3_STR_FIELD_OFFSET_SERIAL, \ + TYPE_3_STR_FIELD_OFFSET_ASSET, \ + TYPE_3_STR_FIELD_OFFSET_SKU}; + table_str_field_count = sizeof(tables_str_field_offset) / \ + sizeof(tables_str_field_offset[0]); + break; + case 4: + tables_str_field_offset = g_malloc0(sizeof(int) * \ + TYPE_4_STR_FIELD_COUNT); + tables_str_field_offset = (int []){\ + TYPE_4_STR_FIELD_OFFSET_SOCKET, \ + TYPE_4_STR_FIELD_OFFSET_PROCESSOR_MANUFACTURER, \ + TYPE_4_STR_FIELD_OFFSET_PROCESSOR_VERSION, \ + TYPE_4_STR_FIELD_OFFSET_SERIAL, \ + TYPE_4_STR_FIELD_OFFSET_ASSET, \ + TYPE_4_STR_FIELD_OFFSET_PART}; + table_str_field_count = sizeof(tables_str_field_offset) / \ + sizeof(tables_str_field_offset[0]); + break; + case 17: + tables_str_field_offset = g_malloc0(sizeof(int) * \ + TYPE_17_STR_FIELD_COUNT); + tables_str_field_offset = (int []){\ + TYPE_17_STR_FIELD_OFFSET_DEVICE_LOCATOR, \ + TYPE_17_STR_FIELD_OFFSET_BANK_LOCATOR, \ + TYPE_17_STR_FIELD_OFFSET_MANUFACTURER, \ + TYPE_17_STR_FIELD_OFFSET_SERIAL, \ + TYPE_17_STR_FIELD_OFFSET_ASSET, \ + TYPE_17_STR_FIELD_OFFSET_PART}; + table_str_field_count = sizeof(tables_str_field_offset) / \ + sizeof(tables_str_field_offset[0]); + break; + default: + break; + } + + for (i = 0; i < table_str_field_count; i++) { + if (*(uint8_t *)(smbios_tables + tables_str_field_offset[i]) > 0) { + terminator_count = 1; + break; + } + } + smbios_tables_len += size; + smbios_tables_len += terminator_count; if (size > smbios_table_max) { smbios_table_max = size; } diff --git a/include/hw/smbios/smbios.h b/include/hw/smbios/smbios.h index 1cd53cc..6d59c3d 100644 --- a/include/hw/smbios/smbios.h +++ b/include/hw/smbios/smbios.h @@ -267,4 +267,48 @@ void smbios_get_tables(const struct smbios_phys_mem_area *mem_array, const unsigned int mem_array_size, uint8_t **tables, size_t *tables_len, uint8_t **anchor, size_t *anchor_len); + +#define TYPE_0_STR_FIELD_OFFSET_VENDOR 0x4 +#define TYPE_0_STR_FIELD_OFFSET_BIOS_VERSION 0x5 +#define TYPE_0_STR_FIELD_OFFSET_BIOS_RELEASE_DATE 0x8 +#define TYPE_0_STR_FIELD_COUNT 3 + +#define TYPE_1_STR_FIELD_OFFSET_MANUFACTURER 0x4 +#define TYPE_1_STR_FIELD_OFFSET_PRODUCT 0x5 +#define TYPE_1_STR_FIELD_OFFSET_VERSION 0x6 +#define TYPE_1_STR_FIELD_OFFSET_SERIAL 0x7 +#define TYPE_1_STR_FIELD_OFFSET_SKU 0x19 +#define TYPE_1_STR_FIELD_OFFSET_FAMILY 0x1a +#define TYPE_1_STR_FIELD_COUNT 6 + +#define TYPE_2_STR_FIELD_OFFSET_MANUFACTURER 0x4 +#define TYPE_2_STR_FIELD_OFFSET_PRODUCT 0x5 +#define TYPE_2_STR_FIELD_OFFSET_VERSION 0x6 +#define TYPE_2_STR_FIELD_OFFSET_SERIAL 0x7 +#define TYPE_2_STR_FIELD_OFFSET_ASSET 0x8 +#define TYPE_2_STR_FIELD_OFFSET_LOCATION 0xa +#define TYPE_2_STR_FIELD_COUNT 6 + +#define TYPE_3_STR_FIELD_OFFSET_MANUFACTURER 0x4 +#define TYPE_3_STR_FIELD_OFFSET_VERSION 0x6 +#define TYPE_3_STR_FIELD_OFFSET_SERIAL 0x7 +#define TYPE_3_STR_FIELD_OFFSET_ASSET 0x8 +#define TYPE_3_STR_FIELD_OFFSET_SKU 0x14 +#define TYPE_3_STR_FIELD_COUNT 5 + +#define TYPE_4_STR_FIELD_OFFSET_SOCKET 0x4 +#define TYPE_4_STR_FIELD_OFFSET_PROCESSOR_MANUFACTURER 0x7 +#define TYPE_4_STR_FIELD_OFFSET_PROCESSOR_VERSION 0x10 +#define TYPE_4_STR_FIELD_OFFSET_SERIAL 0x20 +#define TYPE_4_STR_FIELD_OFFSET_ASSET 0x21 +#define TYPE_4_STR_FIELD_OFFSET_PART 0x22 +#define TYPE_4_STR_FIELD_COUNT 6 + +#define TYPE_17_STR_FIELD_OFFSET_DEVICE_LOCATOR 0x10 +#define TYPE_17_STR_FIELD_OFFSET_BANK_LOCATOR 0x11 +#define TYPE_17_STR_FIELD_OFFSET_MANUFACTURER 0x17 +#define TYPE_17_STR_FIELD_OFFSET_SERIAL 0x18 +#define TYPE_17_STR_FIELD_OFFSET_ASSET 0x19 +#define TYPE_17_STR_FIELD_OFFSET_PART 0x1a +#define TYPE_17_STR_FIELD_COUNT 6 #endif /* QEMU_SMBIOS_H */