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

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