Blame SOURCES/0052-lscpu-dmi-split-to-parse-dmi-table.patch

439b44
From 60a1a900b22e6d714fbbde03322569ad4732b4ba Mon Sep 17 00:00:00 2001
439b44
From: Masayoshi Mizuma <m.mizuma@jp.fujitsu.com>
439b44
Date: Fri, 11 Sep 2020 09:53:26 -0400
439b44
Subject: [PATCH 52/55] lscpu-dmi: split to parse dmi table
439b44
439b44
Split out a function to parse dmi table.
439b44
439b44
Signed-off-by: Masayoshi Mizuma <m.mizuma@jp.fujitsu.com>
439b44
---
439b44
 sys-utils/lscpu-dmi.c | 60 ++++++++++++++++++++++++++++---------------
439b44
 1 file changed, 40 insertions(+), 20 deletions(-)
439b44
439b44
diff --git a/sys-utils/lscpu-dmi.c b/sys-utils/lscpu-dmi.c
439b44
index 29bd2e4fc..9b57fe9e6 100644
439b44
--- a/sys-utils/lscpu-dmi.c
439b44
+++ b/sys-utils/lscpu-dmi.c
439b44
@@ -42,6 +42,12 @@ struct dmi_header
439b44
 	uint8_t *data;
439b44
 };
439b44
 
439b44
+struct dmi_info {
439b44
+	char *vendor;
439b44
+	char *product;
439b44
+	char *manufacturer;
439b44
+};
439b44
+
439b44
 static int checksum(const uint8_t *buf, size_t len)
439b44
 {
439b44
 	uint8_t sum = 0;
439b44
@@ -105,20 +111,13 @@ static char *dmi_string(const struct dmi_header *dm, uint8_t s)
439b44
 	return bp;
439b44
 }
439b44
 
439b44
-static int hypervisor_from_dmi_table(uint32_t base, uint16_t len,
439b44
-				uint16_t num, const char *devmem)
439b44
+static int parse_dmi_table(uint16_t len, uint16_t num,
439b44
+				uint8_t *data,
439b44
+				struct dmi_info *di)
439b44
 {
439b44
-	uint8_t *buf;
439b44
-	uint8_t *data;
439b44
+	uint8_t *buf = data;
439b44
+	int rc = -1;
439b44
 	int i = 0;
439b44
-	char *vendor = NULL;
439b44
-	char *product = NULL;
439b44
-	char *manufacturer = NULL;
439b44
-	int rc = HYPER_NONE;
439b44
-
439b44
-	data = buf = get_mem_chunk(base, len, devmem);
439b44
-	if (!buf)
439b44
-		goto done;
439b44
 
439b44
 	 /* 4 is the length of an SMBIOS structure header */
439b44
 	while (i < num && data + 4 <= buf + len) {
439b44
@@ -142,11 +141,11 @@ static int hypervisor_from_dmi_table(uint32_t base, uint16_t len,
439b44
 		next += 2;
439b44
 		switch (h.type) {
439b44
 			case 0:
439b44
-				vendor = dmi_string(&h, data[0x04]);
439b44
+				di->vendor = dmi_string(&h, data[0x04]);
439b44
 				break;
439b44
 			case 1:
439b44
-				manufacturer = dmi_string(&h, data[0x04]);
439b44
-				product = dmi_string(&h, data[0x05]);
439b44
+				di->manufacturer = dmi_string(&h, data[0x04]);
439b44
+				di->product = dmi_string(&h, data[0x05]);
439b44
 				break;
439b44
 			default:
439b44
 				break;
439b44
@@ -155,15 +154,36 @@ static int hypervisor_from_dmi_table(uint32_t base, uint16_t len,
439b44
 		data = next;
439b44
 		i++;
439b44
 	}
439b44
-	if (manufacturer && !strcmp(manufacturer, "innotek GmbH"))
439b44
+	rc = 0;
439b44
+done:
439b44
+	return rc;
439b44
+}
439b44
+
439b44
+static int hypervisor_from_dmi_table(uint32_t base, uint16_t len,
439b44
+				uint16_t num, const char *devmem)
439b44
+{
439b44
+	uint8_t *data;
439b44
+	int rc = HYPER_NONE;
439b44
+	struct dmi_info di;
439b44
+
439b44
+	data = get_mem_chunk(base, len, devmem);
439b44
+	if (!data)
439b44
+		return rc;
439b44
+
439b44
+	memset(&di, 0, sizeof(struct dmi_info));
439b44
+	rc = parse_dmi_table(len, num, data, &di);
439b44
+	if (rc < 0)
439b44
+		goto done;
439b44
+
439b44
+	if (di.manufacturer && !strcmp(di.manufacturer, "innotek GmbH"))
439b44
 		rc = HYPER_INNOTEK;
439b44
-	else if (manufacturer && strstr(manufacturer, "HITACHI") &&
439b44
-					product && strstr(product, "LPAR"))
439b44
+	else if (di.manufacturer && strstr(di.manufacturer, "HITACHI") &&
439b44
+					di.product && strstr(di.product, "LPAR"))
439b44
 		rc = HYPER_HITACHI;
439b44
-	else if (vendor && !strcmp(vendor, "Parallels"))
439b44
+	else if (di.vendor && !strcmp(di.vendor, "Parallels"))
439b44
 		rc = HYPER_PARALLELS;
439b44
 done:
439b44
-	free(buf);
439b44
+	free(data);
439b44
 	return rc;
439b44
 }
439b44
 
439b44
-- 
439b44
2.29.2
439b44