dcavalca / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

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

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