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

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