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

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