Blame SOURCES/0010-Improve-ACPI-device-path-formatting.patch

36520b
From eb7db33c6cf4172551fe0f9f7cf4aa047dc16d88 Mon Sep 17 00:00:00 2001
36520b
From: Peter Jones <pjones@redhat.com>
36520b
Date: Wed, 20 Jun 2018 14:27:11 -0400
36520b
Subject: [PATCH 10/39] Improve ACPI device path formatting
36520b
36520b
This factors a bunch of the duplication out to another function, which
36520b
also does a better job of it.
36520b
36520b
Signed-off-by: Peter Jones <pjones@redhat.com>
36520b
---
36520b
 src/dp-acpi.c                  | 296 ++++++++++++++++++---------------
36520b
 src/include/efivar/efivar-dp.h |   2 +-
36520b
 2 files changed, 159 insertions(+), 139 deletions(-)
36520b
36520b
diff --git a/src/dp-acpi.c b/src/dp-acpi.c
36520b
index 70162f320dc..a49ef38488c 100644
36520b
--- a/src/dp-acpi.c
36520b
+++ b/src/dp-acpi.c
36520b
@@ -44,6 +44,59 @@ _format_acpi_adr(char *buf, size_t size,
36520b
 #define format_acpi_adr(buf, size, off, dp)				\
36520b
 	format_helper(_format_acpi_adr, buf, size, off, "AcpiAdr", dp)
36520b
 
36520b
+static ssize_t
36520b
+_format_acpi_hid_ex(char *buf, size_t size, const char *dp_type UNUSED,
36520b
+		    const_efidp dp,
36520b
+		    const char *hidstr, const char *cidstr, const char *uidstr)
36520b
+{
36520b
+	ssize_t off = 0;
36520b
+
36520b
+	debug(DEBUG, "hid:0x%08x hidstr:\"%s\"", dp->acpi_hid_ex.hid, hidstr);
36520b
+	debug(DEBUG, "cid:0x%08x cidstr:\"%s\"", dp->acpi_hid_ex.cid, cidstr);
36520b
+	debug(DEBUG, "uid:0x%08x uidstr:\"%s\"", dp->acpi_hid_ex.uid, uidstr);
36520b
+
36520b
+	if (!hidstr && !cidstr && (uidstr || dp->acpi_hid_ex.uid)) {
36520b
+		format(buf, size, off, "AcpiExp",
36520b
+		       "AcpiExp(0x%"PRIx32",0x%"PRIx32",",
36520b
+		       dp->acpi_hid_ex.hid, dp->acpi_hid_ex.cid);
36520b
+		if (uidstr) {
36520b
+			format(buf, size, off, "AcpiExp", "%s)", uidstr);
36520b
+		} else {
36520b
+			format(buf, size, off, "AcpiExp", "0x%"PRIx32")",
36520b
+			       dp->acpi_hid_ex.uid);
36520b
+		}
36520b
+		return off;
36520b
+	}
36520b
+
36520b
+	format(buf, size, off, "AcpiEx", "AcpiEx(");
36520b
+	if (hidstr) {
36520b
+		format(buf, size, off, "AcpiEx", "%s,", hidstr);
36520b
+	} else {
36520b
+		format(buf, size, off, "AcpiEx", "0x%"PRIx32",",
36520b
+		       dp->acpi_hid_ex.hid);
36520b
+	}
36520b
+
36520b
+	if (cidstr) {
36520b
+		format(buf, size, off, "AcpiEx", "%s,", cidstr);
36520b
+	} else {
36520b
+		format(buf, size, off, "AcpiEx", "0x%"PRIx32",",
36520b
+		       dp->acpi_hid_ex.cid);
36520b
+	}
36520b
+
36520b
+	if (uidstr) {
36520b
+		format(buf, size, off, "AcpiEx", "%s)", uidstr);
36520b
+	} else {
36520b
+		format(buf, size, off, "AcpiEx", "0x%"PRIx32")",
36520b
+		       dp->acpi_hid_ex.uid);
36520b
+	}
36520b
+
36520b
+	return off;
36520b
+}
36520b
+
36520b
+#define format_acpi_hid_ex(buf, size, off, dp, hidstr, cidstr, uidstr)  \
36520b
+	format_helper(_format_acpi_hid_ex, buf, size, off, "AcpiEx", dp,\
36520b
+		      hidstr, cidstr, uidstr)
36520b
+
36520b
 ssize_t
36520b
 _format_acpi_dn(char *buf, size_t size, const_efidp dp)
36520b
 {
36520b
@@ -53,13 +106,15 @@ _format_acpi_dn(char *buf, size_t size, const_efidp dp)
36520b
 	const char *uidstr = NULL;
36520b
 	size_t uidlen = 0;
36520b
 	const char *cidstr = NULL;
36520b
-	size_t cidlen = 0;
36520b
+	// size_t cidlen = 0;
36520b
 
36520b
 	if (dp->subtype == EFIDP_ACPI_ADR) {
36520b
+		debug(DEBUG, "formatting ACPI _ADR");
36520b
 		format_acpi_adr(buf, size, off, dp);
36520b
 		return off;
36520b
 	} else if (dp->subtype != EFIDP_ACPI_HID_EX &&
36520b
 		   dp->subtype != EFIDP_ACPI_HID) {
36520b
+		debug(DEBUG, "DP subtype %d, formatting as ACPI Path", dp->subtype);
36520b
 		format(buf, size, off, "AcpiPath", "AcpiPath(%d,", dp->subtype);
36520b
 		format_hex(buf, size, off, "AcpiPath", (uint8_t *)dp+4,
36520b
 			   (efidp_node_size(dp)-4) / 2);
36520b
@@ -69,6 +124,7 @@ _format_acpi_dn(char *buf, size_t size, const_efidp dp)
36520b
 		ssize_t limit = efidp_node_size(dp)
36520b
 				- offsetof(efidp_acpi_hid_ex, hidstr);
36520b
 
36520b
+		debug(DEBUG, "formatting ACPI HID EX");
36520b
 		hidstr = dp->acpi_hid_ex.hidstr;
36520b
 		hidlen = strnlen(hidstr, limit);
36520b
 		limit -= hidlen + 1;
36520b
@@ -81,7 +137,7 @@ _format_acpi_dn(char *buf, size_t size, const_efidp dp)
36520b
 
36520b
 		if (limit) {
36520b
 			cidstr = uidstr + uidlen + 1;
36520b
-			cidlen = strnlen(cidstr, limit);
36520b
+			// cidlen = strnlen(cidstr, limit);
36520b
 			// limit -= cidlen + 1;
36520b
 		}
36520b
 
36520b
@@ -96,143 +152,102 @@ _format_acpi_dn(char *buf, size_t size, const_efidp dp)
36520b
 				       "PcieRoot(%s)", uidstr);
36520b
 				return off;
36520b
 			default:
36520b
-				format(buf, size, off, "AcpiEx", "AcpiEx(");
36520b
-				if (hidlen)
36520b
-					format(buf, size, off, "AcpiEx", "%s",
36520b
-							hidstr);
36520b
-				else
36520b
-					format(buf, size, off, "AcpiEx", "0x%"PRIx32,
36520b
-							dp->acpi_hid_ex.hid);
36520b
-				if (cidlen)
36520b
-					format(buf, size, off, "AcpiEx", ",%s",
36520b
-							cidstr);
36520b
-				else
36520b
-					format(buf, size, off, "AcpiEx", ",0x%"PRIx32,
36520b
-							dp->acpi_hid_ex.cid);
36520b
-				if (uidlen)
36520b
-					format(buf, size, off, "AcpiEx", ",%s",
36520b
-							uidstr);
36520b
-				else
36520b
-					format(buf, size, off, "AcpiEx", ",0x%"PRIx32,
36520b
-							dp->acpi_hid_ex.uid);
36520b
-				format(buf, size, off, "AcpiEx", ")");
36520b
-				break;
36520b
+				format_acpi_hid_ex(buf, size, off, dp,
36520b
+						   hidstr, cidstr, uidstr);
36520b
+				return off;
36520b
 			}
36520b
 		}
36520b
-	}
36520b
-
36520b
-	switch (dp->acpi_hid.hid) {
36520b
-	case EFIDP_ACPI_PCI_ROOT_HID:
36520b
-		format(buf, size, off, "PciRoot", "PciRoot(0x%"PRIx32")",
36520b
-		       dp->acpi_hid.uid);
36520b
-		break;
36520b
-	case EFIDP_ACPI_PCIE_ROOT_HID:
36520b
-		format(buf, size, off, "PcieRoot", "PcieRoot(0x%"PRIx32")",
36520b
-		       dp->acpi_hid.uid);
36520b
-		break;
36520b
-	case EFIDP_ACPI_FLOPPY_HID:
36520b
-		format(buf, size, off, "Floppy", "Floppy(0x%"PRIx32")",
36520b
-		       dp->acpi_hid.uid);
36520b
-		break;
36520b
-	case EFIDP_ACPI_KEYBOARD_HID:
36520b
-		format(buf, size, off, "Keyboard", "Keyboard(0x%"PRIx32")",
36520b
-		       dp->acpi_hid.uid);
36520b
-		break;
36520b
-	case EFIDP_ACPI_SERIAL_HID:
36520b
-		format(buf, size, off, "Keyboard", "Serial(0x%"PRIx32")",
36520b
-		       dp->acpi_hid.uid);
36520b
-		break;
36520b
-	case EFIDP_ACPI_NVDIMM_HID: {
36520b
-		int rc;
36520b
-		const_efidp next = NULL;
36520b
-		efidp_acpi_adr *adrdp;
36520b
-		int end;
36520b
-
36520b
-		format(buf, size, off, "NvRoot()", "NvRoot()");
36520b
-
36520b
-		rc = efidp_next_node(dp, &next;;
36520b
-		if (rc < 0 || !next) {
36520b
-			efi_error("could not format DP");
36520b
-			return rc;
36520b
-		}
36520b
+	} else if (dp->subtype == EFIDP_ACPI_HID_EX) {
36520b
+		switch (dp->acpi_hid.hid) {
36520b
+		case EFIDP_ACPI_PCI_ROOT_HID:
36520b
+			format(buf, size, off, "PciRoot",
36520b
+			       "PciRoot(0x%"PRIx32")",
36520b
+			       dp->acpi_hid.uid);
36520b
+			break;
36520b
+		case EFIDP_ACPI_PCIE_ROOT_HID:
36520b
+			format(buf, size, off, "PcieRoot",
36520b
+			       "PcieRoot(0x%"PRIx32")",
36520b
+			       dp->acpi_hid.uid);
36520b
+			break;
36520b
+		case EFIDP_ACPI_FLOPPY_HID:
36520b
+			format(buf, size, off, "Floppy",
36520b
+			       "Floppy(0x%"PRIx32")",
36520b
+			       dp->acpi_hid.uid);
36520b
+			break;
36520b
+		case EFIDP_ACPI_KEYBOARD_HID:
36520b
+			format(buf, size, off, "Keyboard",
36520b
+			       "Keyboard(0x%"PRIx32")",
36520b
+			       dp->acpi_hid.uid);
36520b
+			break;
36520b
+		case EFIDP_ACPI_SERIAL_HID:
36520b
+			format(buf, size, off, "Serial",
36520b
+			       "Serial(0x%"PRIx32")",
36520b
+			       dp->acpi_hid.uid);
36520b
+			break;
36520b
+		case EFIDP_ACPI_NVDIMM_HID: {
36520b
+			int rc;
36520b
+			const_efidp next = NULL;
36520b
+			efidp_acpi_adr *adrdp;
36520b
+			int end;
36520b
 
36520b
-		if (efidp_type(next) != EFIDP_ACPI_TYPE ||
36520b
-		    efidp_subtype(next) != EFIDP_ACPI_ADR) {
36520b
-			efi_error("Invalid child node type (0x%02x,0x%02x)",
36520b
-				  efidp_type(next), efidp_subtype(next));
36520b
-			return -EINVAL;
36520b
-		}
36520b
-		adrdp = (efidp_acpi_adr *)next;
36520b
+			format(buf, size, off, "NvRoot()", "NvRoot()");
36520b
 
36520b
-		end = efidp_size_after(adrdp, header)
36520b
-			/ sizeof(adrdp->adr[0]);
36520b
+			rc = efidp_next_node(dp, &next;;
36520b
+			if (rc < 0 || !next) {
36520b
+				efi_error("could not format DP");
36520b
+				return rc;
36520b
+			}
36520b
 
36520b
-		for (int i = 0; i < end; i++) {
36520b
-			uint32_t node_controller, socket, memory_controller;
36520b
-			uint32_t memory_channel, dimm;
36520b
-			uint32_t adr = adrdp->adr[i];
36520b
+			if (efidp_type(next) != EFIDP_ACPI_TYPE ||
36520b
+			    efidp_subtype(next) != EFIDP_ACPI_ADR) {
36520b
+				efi_error("Invalid child node type (0x%02x,0x%02x)",
36520b
+					  efidp_type(next), efidp_subtype(next));
36520b
+				return -EINVAL;
36520b
+			}
36520b
+			adrdp = (efidp_acpi_adr *)next;
36520b
 
36520b
-			efidp_decode_acpi_nvdimm_adr(adr, &node_controller,
36520b
-						     &socket,
36520b
-						     &memory_controller,
36520b
-						     &memory_channel, &dimm);
36520b
+			end = efidp_size_after(adrdp, header)
36520b
+				/ sizeof(adrdp->adr[0]);
36520b
 
36520b
-			if (i != 0)
36520b
-				format(buf, size, off, "NvDimm", ",");
36520b
+			for (int i = 0; i < end; i++) {
36520b
+				uint32_t node_controller, socket, memory_controller;
36520b
+				uint32_t memory_channel, dimm;
36520b
+				uint32_t adr = adrdp->adr[i];
36520b
 
36520b
-			format(buf, size, off, "NvDimm",
36520b
-			       "NvDimm(0x%03x,0x%01x,0x%01x,0x%01x,0x%01x)",
36520b
-			       node_controller, socket, memory_controller,
36520b
-			       memory_channel, dimm);
36520b
-		}
36520b
-		break;
36520b
-				    }
36520b
-	default:
36520b
-		switch (dp->subtype) {
36520b
-		case EFIDP_ACPI_HID_EX:
36520b
-			if (!hidstr && !cidstr &&
36520b
-					(uidstr || dp->acpi_hid_ex.uid)){
36520b
-				format(buf, size, off, "AcpiExp",
36520b
-				       "AcpiExp(0x%"PRIx32",0x%"PRIx32",",
36520b
-				       dp->acpi_hid_ex.hid,
36520b
-				       dp->acpi_hid_ex.cid);
36520b
-				if (uidstr) {
36520b
-					format(buf, size, off, "AcpiExp",
36520b
-					       "%s)", uidstr);
36520b
-				} else {
36520b
-					format(buf, size, off, "AcpiExp",
36520b
-					       "0x%"PRIx32")",
36520b
-					       dp->acpi_hid.uid);
36520b
-				}
36520b
-				break;
36520b
-			}
36520b
-			format(buf, size, off, "AcpiEx", "AcpiEx(");
36520b
-			if (hidstr) {
36520b
-				format(buf, size, off, "AcpiEx", "%s,", hidstr);
36520b
-			} else {
36520b
-				format(buf, size, off, "AcpiEx", "0x%"PRIx32",",
36520b
-					      dp->acpi_hid.hid);
36520b
-			}
36520b
+				efidp_decode_acpi_nvdimm_adr(adr,
36520b
+					&node_controller, &socket,
36520b
+					&memory_controller, &memory_channel,
36520b
+					&dimm);
36520b
 
36520b
-			if (cidstr) {
36520b
-				format(buf, size, off, "AcpiEx", "%s,", cidstr);
36520b
-			} else {
36520b
-				format(buf, size, off, "AcpiEx", "0x%"PRIx32",",
36520b
-				       dp->acpi_hid_ex.cid);
36520b
-			}
36520b
+				if (i != 0)
36520b
+					format(buf, size, off, "NvDimm", ",");
36520b
 
36520b
-			if (uidstr) {
36520b
-				format(buf, size, off, "AcpiEx", "%s)", uidstr);
36520b
-			} else {
36520b
-				format(buf, size, off, "AcpiEx", "0x%"PRIx32")",
36520b
-				       dp->acpi_hid.uid);
36520b
+				format(buf, size, off, "NvDimm",
36520b
+				       "NvDimm(0x%03x,0x%01x,0x%01x,0x%01x,0x%01x)",
36520b
+				       node_controller, socket, memory_controller,
36520b
+				       memory_channel, dimm);
36520b
 			}
36520b
 			break;
36520b
-		case EFIDP_ACPI_HID:
36520b
-			format(buf, size, off, "Acpi",
36520b
-			       "Acpi(0x%"PRIx32",0x%"PRIx32")",
36520b
-			       dp->acpi_hid.hid, dp->acpi_hid.uid);
36520b
-			break;
36520b
+					    }
36520b
+		default:
36520b
+			debug(DEBUG, "Decoding non-well-known HID");
36520b
+			switch (dp->subtype) {
36520b
+			case EFIDP_ACPI_HID_EX:
36520b
+				format_acpi_hid_ex(buf, size, off, dp,
36520b
+						   hidstr, cidstr, uidstr);
36520b
+				break;
36520b
+			case EFIDP_ACPI_HID:
36520b
+				debug(DEBUG, "Decoding ACPI HID");
36520b
+				format(buf, size, off, "Acpi",
36520b
+				       "Acpi(0x%08x,0x%"PRIx32")",
36520b
+				       dp->acpi_hid.hid, dp->acpi_hid.uid);
36520b
+				break;
36520b
+			default:
36520b
+				debug(DEBUG, "ACPI subtype %d???",
36520b
+				      dp->subtype);
36520b
+				errno = EINVAL;
36520b
+				return -1;
36520b
+			}
36520b
 		}
36520b
 	}
36520b
 
36520b
@@ -259,7 +274,7 @@ efidp_make_acpi_hid(uint8_t *buf, ssize_t size, uint32_t hid, uint32_t uid)
36520b
 	return sz;
36520b
 }
36520b
 
36520b
-ssize_t PUBLIC NONNULL(6, 7, 8)
36520b
+ssize_t PUBLIC
36520b
 efidp_make_acpi_hid_ex(uint8_t *buf, ssize_t size,
36520b
 		       uint32_t hid, uint32_t uid, uint32_t cid,
36520b
 		       const char *hidstr, const char *uidstr,
36520b
@@ -268,21 +283,26 @@ efidp_make_acpi_hid_ex(uint8_t *buf, ssize_t size,
36520b
 	efidp_acpi_hid_ex *acpi_hid = (efidp_acpi_hid_ex *)buf;
36520b
 	ssize_t req;
36520b
 	ssize_t sz;
36520b
+	size_t hidlen = hidstr ? strlen(hidstr) : 0;
36520b
+	size_t uidlen = uidstr ? strlen(uidstr) : 0;
36520b
+	size_t cidlen = cidstr ? strlen(cidstr) : 0;
36520b
 
36520b
-	req = sizeof (*acpi_hid) + 3 +
36520b
-		strlen(hidstr) + strlen(uidstr) + strlen(cidstr);
36520b
+	req = sizeof (*acpi_hid) + 3 + hidlen + uidlen + cidlen;
36520b
 	sz = efidp_make_generic(buf, size, EFIDP_ACPI_TYPE, EFIDP_ACPI_HID_EX,
36520b
 				req);
36520b
 	if (size && sz == req) {
36520b
-		acpi_hid->uid = uid;
36520b
-		acpi_hid->hid = hid;
36520b
-		acpi_hid->cid = cid;
36520b
+		acpi_hid->hid = hidlen ? 0 : hid;
36520b
+		acpi_hid->uid = uidlen ? 0 : uid;
36520b
+		acpi_hid->cid = cidlen ? 0 : cid;
36520b
 		char *next = (char *)buf+offsetof(efidp_acpi_hid_ex, hidstr);
36520b
-		strcpy(next, hidstr);
36520b
-		next += strlen(hidstr) + 1;
36520b
-		strcpy(next, uidstr);
36520b
-		next += strlen(uidstr) + 1;
36520b
-		strcpy(next, cidstr);
36520b
+		if (hidlen)
36520b
+			strcpy(next, hidstr);
36520b
+		next += hidlen + 1;
36520b
+		if (uidlen)
36520b
+			strcpy(next, uidstr);
36520b
+		next += uidlen + 1;
36520b
+		if (cidlen)
36520b
+			strcpy(next, cidstr);
36520b
 	}
36520b
 
36520b
 	if (sz < 0)
36520b
diff --git a/src/include/efivar/efivar-dp.h b/src/include/efivar/efivar-dp.h
36520b
index 106d3645e21..1b05775ae7e 100644
36520b
--- a/src/include/efivar/efivar-dp.h
36520b
+++ b/src/include/efivar/efivar-dp.h
36520b
@@ -133,7 +133,7 @@ typedef struct {
36520b
 	/* three ascii string fields follow */
36520b
 	char		hidstr[];
36520b
 } EFIVAR_PACKED efidp_acpi_hid_ex;
36520b
-extern ssize_t __attribute__((__nonnull__ (6,7,8)))
36520b
+extern ssize_t
36520b
 efidp_make_acpi_hid_ex(uint8_t *buf, ssize_t size,
36520b
                        uint32_t hid, uint32_t uid, uint32_t cid,
36520b
                        const char *hidstr, const char *uidstr,
36520b
-- 
36520b
2.17.1
36520b