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

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