diff mbox

[tpmdd-devel] tpm: fix invalid constant expressions in tpm.h

Message ID 1472520498-24173-1-git-send-email-jarkko.sakkinen@linux.intel.com
State New
Headers show

Commit Message

Jarkko Sakkinen Aug. 30, 2016, 1:28 a.m. UTC
The enums tpm_capabilities and tpm_sub_capabilities do not contain legit
constant expressions. This commit makes cap_id a separate parameter in
order to provide less confusing API and cap_id and subcap_id will be u32
with the cpu byte order. The byte order conversions are handled
internally by tpm1_getcap_cmd.

$ make C=2  M=drivers/char/tpm
  CHECK   drivers/char/tpm/tpm-interface.c
drivers/char/tpm/tpm.h:285:24: error: bad constant expression
drivers/char/tpm/tpm.h:286:24: error: bad constant expression
drivers/char/tpm/tpm.h:287:27: error: bad constant expression
drivers/char/tpm/tpm.h:288:27: error: bad constant expression
drivers/char/tpm/tpm.h:292:28: error: bad constant expression
drivers/char/tpm/tpm.h:293:37: error: bad constant expression
drivers/char/tpm/tpm.h:294:29: error: bad constant expression
drivers/char/tpm/tpm.h:295:28: error: bad constant expression
drivers/char/tpm/tpm.h:296:30: error: bad constant expression
drivers/char/tpm/tpm.h:297:36: error: bad constant expression
drivers/char/tpm/tpm.h:298:37: error: bad constant expression
drivers/char/tpm/tpm-interface.c:444:23: error: incompatible types for operation (==)
drivers/char/tpm/tpm-interface.c:444:23:    left side has type unsigned int [unsigned] [usertype] subcap_id
drivers/char/tpm/tpm-interface.c:444:23:    right side has type bad type
drivers/char/tpm/tpm-interface.c:444:55: error: incompatible types for operation (==)
drivers/char/tpm/tpm-interface.c:444:55:    left side has type unsigned int [unsigned] [usertype] subcap_id
drivers/char/tpm/tpm-interface.c:444:55:    right side has type bad type
drivers/char/tpm/tpm-interface.c:450:31: error: incompatible types for operation (==)
drivers/char/tpm/tpm-interface.c:450:31:    left side has type unsigned int [unsigned] [usertype] subcap_id
drivers/char/tpm/tpm-interface.c:450:31:    right side has type bad type
drivers/char/tpm/tpm-interface.c:451:31: error: incompatible types for operation (==)
drivers/char/tpm/tpm-interface.c:451:31:    left side has type unsigned int [unsigned] [usertype] subcap_id
drivers/char/tpm/tpm-interface.c:451:31:    right side has type bad type

Fixes: 0883743825e3 ("TPM: sysfs functions consolidation")
Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
---
 drivers/char/tpm/tpm-interface.c | 58 +++++++++++++++++-----------------------
 drivers/char/tpm/tpm-sysfs.c     | 36 ++++++++++++++-----------
 drivers/char/tpm/tpm.h           | 31 +++++++++++----------
 drivers/char/tpm/tpm_tis_core.c  |  3 ++-
 4 files changed, 62 insertions(+), 66 deletions(-)

Comments

Jason Gunthorpe Aug. 30, 2016, 3:45 a.m. UTC | #1
On Tue, Aug 30, 2016 at 04:28:17AM +0300, Jarkko Sakkinen wrote:
> The enums tpm_capabilities and tpm_sub_capabilities do not contain legit
> constant expressions. This commit makes cap_id a separate parameter
> in

I wonder if this is a bug in sparse? the macro uses gcc magic to
expand to a constexpr.

You could also use __constant_cpu_to_be32 and similar instead.

But I admit I never liked the use of no-host endian in the constants..

>  #define TPM_ORD_STARTUP cpu_to_be32(153)
>  #define TPM_ST_CLEAR cpu_to_be16(1)

Would be nice to see these fixed into an enum someday too

> +enum tpm1_capabilities {
> +	TPM1_CAP_FLAG		= 0x04,
> +	TPM1_CAP_PROP		= 0x05,
> +	TPM1_CAP_VERSION_1_1	= 0x06,
> +	TPM1_CAP_VERSION_1_2	= 0x1A,

I usually discourage the extra horizontal whitespace, just causes patch churn to
keep it up (and clang-format won't do it automatically). Not sure if
there is a consensus on that though.

But looks fine to me.

Reviewed-by: Jason Gunthorpe <jgunthorpe@obsidianresearch.com>

Jason

------------------------------------------------------------------------------
Jarkko Sakkinen Aug. 30, 2016, 6:31 a.m. UTC | #2
On Mon, Aug 29, 2016 at 09:45:36PM -0600, Jason Gunthorpe wrote:
> On Tue, Aug 30, 2016 at 04:28:17AM +0300, Jarkko Sakkinen wrote:
> > The enums tpm_capabilities and tpm_sub_capabilities do not contain legit
> > constant expressions. This commit makes cap_id a separate parameter
> > in
> 
> I wonder if this is a bug in sparse? the macro uses gcc magic to
> expand to a constexpr.
> 
> You could also use __constant_cpu_to_be32 and similar instead.
> 
> But I admit I never liked the use of no-host endian in the constants..

It's too much magic for me, be it way or another :)


> >  #define TPM_ORD_STARTUP cpu_to_be32(153)
> >  #define TPM_ST_CLEAR cpu_to_be16(1)
> 
> Would be nice to see these fixed into an enum someday too

(putting note into my backlog text file)

> > +enum tpm1_capabilities {
> > +	TPM1_CAP_FLAG		= 0x04,
> > +	TPM1_CAP_PROP		= 0x05,
> > +	TPM1_CAP_VERSION_1_1	= 0x06,
> > +	TPM1_CAP_VERSION_1_2	= 0x1A,
> 
> I usually discourage the extra horizontal whitespace, just causes patch churn to
> keep it up (and clang-format won't do it automatically). Not sure if
> there is a consensus on that though.
> 
> But looks fine to me.
> 
> Reviewed-by: Jason Gunthorpe <jgunthorpe@obsidianresearch.com>

Thanks!

> Jason

/Jarkko

------------------------------------------------------------------------------
diff mbox

Patch

diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index 3edf2e8..fd863ff 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -434,34 +434,40 @@  static const struct tpm_input_header tpm_getcap_header = {
 	.ordinal = TPM_ORD_GET_CAP
 };
 
-ssize_t tpm_getcap(struct tpm_chip *chip, __be32 subcap_id, cap_t *cap,
-		   const char *desc)
+/**
+ * tpm1_getcap_cmd() - execute a TPM 1.x TPM_GetCapability command
+ * @chip: TPM chip to use
+ * @cap_id: capability
+ * @subcap_id: subcapability
+ * @desc: same as with tpm_transmit_cmd
+ *
+ * Return: same as with tpm_transmit_cmd
+ */
+ssize_t tpm1_getcap_cmd(struct tpm_chip *chip, u32 cap_id, u32 subcap_id,
+			cap_t *cap, const char *desc)
 {
 	struct tpm_cmd_t tpm_cmd;
 	int rc;
 
 	tpm_cmd.header.in = tpm_getcap_header;
-	if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
-		tpm_cmd.params.getcap_in.cap = subcap_id;
-		/*subcap field not necessary */
+	if (cap_id == TPM1_CAP_VERSION_1_1 ||
+	    cap_id == TPM1_CAP_VERSION_1_2) {
+		tpm_cmd.params.getcap_in.cap = cpu_to_be32(cap_id);
+		/* subcap field not necessary */
 		tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
 		tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
 	} else {
-		if (subcap_id == TPM_CAP_FLAG_PERM ||
-		    subcap_id == TPM_CAP_FLAG_VOL)
-			tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
-		else
-			tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
+		tpm_cmd.params.getcap_in.cap = cpu_to_be32(cap_id);
 		tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
-		tpm_cmd.params.getcap_in.subcap = subcap_id;
+		tpm_cmd.params.getcap_in.subcap = cpu_to_be32(subcap_id);
 	}
 	rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 0,
 			      desc);
-	if (!rc)
+	if (!rc && cap)
 		*cap = tpm_cmd.params.getcap_out.cap;
 	return rc;
 }
-EXPORT_SYMBOL_GPL(tpm_getcap);
+EXPORT_SYMBOL_GPL(tpm1_getcap_cmd);
 
 #define TPM_ORD_STARTUP cpu_to_be32(153)
 #define TPM_ST_CLEAR cpu_to_be16(1)
@@ -506,13 +512,8 @@  int tpm_get_timeouts(struct tpm_chip *chip)
 		return 0;
 	}
 
-	tpm_cmd.header.in = tpm_getcap_header;
-	tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
-	tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
-	tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
-	rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 0,
-			      NULL);
-
+	rc = tpm1_getcap_cmd(chip, TPM1_CAP_PROP, TPM1_CAP_PROP_TIS_TIMEOUT,
+			     NULL, NULL);
 	if (rc == TPM_ERR_INVALID_POSTINIT) {
 		/* The TPM is not started, we are the first to talk to it.
 		   Execute a startup command. */
@@ -520,12 +521,8 @@  int tpm_get_timeouts(struct tpm_chip *chip)
 		if (tpm_startup(chip, TPM_ST_CLEAR))
 			return rc;
 
-		tpm_cmd.header.in = tpm_getcap_header;
-		tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
-		tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
-		tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
-		rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
-				      0, NULL);
+		rc = tpm1_getcap_cmd(chip, TPM1_CAP_PROP,
+				     TPM1_CAP_PROP_TIS_TIMEOUT, NULL, NULL);
 	}
 	if (rc) {
 		dev_err(&chip->dev,
@@ -581,13 +578,8 @@  int tpm_get_timeouts(struct tpm_chip *chip)
 	chip->timeout_d = usecs_to_jiffies(new_timeout[3]);
 
 duration:
-	tpm_cmd.header.in = tpm_getcap_header;
-	tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
-	tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
-	tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
-
-	rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 0,
-			      "attempting to determine the durations");
+	rc = tpm1_getcap_cmd(chip, TPM1_CAP_PROP, TPM1_CAP_PROP_TIS_TIMEOUT,
+			     NULL, "attempting to determine the durations");
 	if (rc)
 		return rc;
 
diff --git a/drivers/char/tpm/tpm-sysfs.c b/drivers/char/tpm/tpm-sysfs.c
index e1f7236..b19f0c4 100644
--- a/drivers/char/tpm/tpm-sysfs.c
+++ b/drivers/char/tpm/tpm-sysfs.c
@@ -94,8 +94,8 @@  static ssize_t pcrs_show(struct device *dev, struct device_attribute *attr,
 	char *str = buf;
 	struct tpm_chip *chip = to_tpm_chip(dev);
 
-	rc = tpm_getcap(chip, TPM_CAP_PROP_PCR, &cap,
-			"attempting to determine the number of PCRS");
+	rc = tpm1_getcap_cmd(chip, TPM1_CAP_PROP, TPM1_CAP_PROP_PCR, &cap,
+			     "attempting to determine the number of PCRS");
 	if (rc)
 		return 0;
 
@@ -119,8 +119,9 @@  static ssize_t enabled_show(struct device *dev, struct device_attribute *attr,
 	cap_t cap;
 	ssize_t rc;
 
-	rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_PERM, &cap,
-			"attempting to determine the permanent enabled state");
+	rc = tpm1_getcap_cmd(to_tpm_chip(dev), TPM1_CAP_FLAG,
+			     TPM1_CAP_FLAG_PERM, &cap,
+			     "attempting to determine the permanent enabled state");
 	if (rc)
 		return 0;
 
@@ -135,8 +136,9 @@  static ssize_t active_show(struct device *dev, struct device_attribute *attr,
 	cap_t cap;
 	ssize_t rc;
 
-	rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_PERM, &cap,
-			"attempting to determine the permanent active state");
+	rc = tpm1_getcap_cmd(to_tpm_chip(dev), TPM1_CAP_FLAG,
+			     TPM1_CAP_FLAG_PERM, &cap,
+			     "attempting to determine the permanent active state");
 	if (rc)
 		return 0;
 
@@ -151,8 +153,9 @@  static ssize_t owned_show(struct device *dev, struct device_attribute *attr,
 	cap_t cap;
 	ssize_t rc;
 
-	rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap,
-			"attempting to determine the owner state");
+	rc = tpm1_getcap_cmd(to_tpm_chip(dev), TPM1_CAP_PROP,
+			     TPM1_CAP_PROP_OWNER, &cap,
+			     "attempting to determine the owner state");
 	if (rc)
 		return 0;
 
@@ -167,8 +170,9 @@  static ssize_t temp_deactivated_show(struct device *dev,
 	cap_t cap;
 	ssize_t rc;
 
-	rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_VOL, &cap,
-			"attempting to determine the temporary state");
+	rc = tpm1_getcap_cmd(to_tpm_chip(dev), TPM1_CAP_FLAG, TPM1_CAP_FLAG_VOL,
+			     &cap,
+			     "attempting to determine the temporary state");
 	if (rc)
 		return 0;
 
@@ -185,16 +189,16 @@  static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
 	ssize_t rc;
 	char *str = buf;
 
-	rc = tpm_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap,
-			"attempting to determine the manufacturer");
+	rc = tpm1_getcap_cmd(chip, TPM1_CAP_PROP, TPM1_CAP_PROP_MANUFACTURER,
+			     &cap, "attempting to determine the manufacturer");
 	if (rc)
 		return 0;
 	str += sprintf(str, "Manufacturer: 0x%x\n",
 		       be32_to_cpu(cap.manufacturer_id));
 
 	/* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */
-	rc = tpm_getcap(chip, CAP_VERSION_1_2, &cap,
-			"attempting to determine the 1.2 version");
+	rc = tpm1_getcap_cmd(chip, TPM1_CAP_VERSION_1_2, 0, &cap,
+			     "attempting to determine the 1.2 version");
 	if (!rc) {
 		str += sprintf(str,
 			       "TCG version: %d.%d\nFirmware version: %d.%d\n",
@@ -204,8 +208,8 @@  static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
 			       cap.tpm_version_1_2.revMinor);
 	} else {
 		/* Otherwise just use TPM_STRUCT_VER */
-		rc = tpm_getcap(chip, CAP_VERSION_1_1, &cap,
-				"attempting to determine the 1.1 version");
+		rc = tpm1_getcap_cmd(chip, TPM1_CAP_VERSION_1_1, 0, &cap,
+				     "attempting to determine the 1.1 version");
 		if (rc)
 			return 0;
 		str += sprintf(str,
diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
index 1d1361a..3e952fb 100644
--- a/drivers/char/tpm/tpm.h
+++ b/drivers/char/tpm/tpm.h
@@ -281,22 +281,21 @@  typedef union {
 	struct duration_t duration;
 } cap_t;
 
-enum tpm_capabilities {
-	TPM_CAP_FLAG = cpu_to_be32(4),
-	TPM_CAP_PROP = cpu_to_be32(5),
-	CAP_VERSION_1_1 = cpu_to_be32(0x06),
-	CAP_VERSION_1_2 = cpu_to_be32(0x1A)
+enum tpm1_capabilities {
+	TPM1_CAP_FLAG		= 0x04,
+	TPM1_CAP_PROP		= 0x05,
+	TPM1_CAP_VERSION_1_1	= 0x06,
+	TPM1_CAP_VERSION_1_2	= 0x1A,
 };
 
-enum tpm_sub_capabilities {
-	TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
-	TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
-	TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
-	TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
-	TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
-	TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
-	TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
-
+enum tpm1_sub_capabilities {
+	TPM1_CAP_PROP_PCR		= 0x101,
+	TPM1_CAP_PROP_MANUFACTURER	= 0x103,
+	TPM1_CAP_FLAG_PERM		= 0x108,
+	TPM1_CAP_FLAG_VOL		= 0x109,
+	TPM1_CAP_PROP_OWNER		= 0x111,
+	TPM1_CAP_PROP_TIS_TIMEOUT	= 0x115,
+	TPM1_CAP_PROP_TIS_DURATION	= 0x120,
 };
 
 struct	tpm_getcap_params_in {
@@ -484,8 +483,8 @@  ssize_t tpm_transmit(struct tpm_chip *chip, const u8 *buf, size_t bufsiz,
 		     unsigned int flags);
 ssize_t tpm_transmit_cmd(struct tpm_chip *chip, const void *cmd, int len,
 			 unsigned int flags, const char *desc);
-ssize_t tpm_getcap(struct tpm_chip *chip, __be32 subcap_id, cap_t *cap,
-		   const char *desc);
+ssize_t tpm1_getcap_cmd(struct tpm_chip *chip, u32 cap_id, u32 subcap_id,
+			cap_t *cap, const char *desc);
 int tpm_get_timeouts(struct tpm_chip *chip);
 int tpm1_auto_startup(struct tpm_chip *chip);
 int tpm_do_selftest(struct tpm_chip *chip);
diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c
index f22caf8..88c8d8a 100644
--- a/drivers/char/tpm/tpm_tis_core.c
+++ b/drivers/char/tpm/tpm_tis_core.c
@@ -535,7 +535,8 @@  static int tpm_tis_gen_interrupt(struct tpm_chip *chip)
 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
 		return tpm2_get_tpm_pt(chip, 0x100, &cap2, desc);
 	else
-		return tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc);
+		return tpm1_getcap_cmd(chip, TPM1_CAP_PROP,
+				       TPM1_CAP_PROP_TIS_TIMEOUT, &cap, desc);
 }
 
 /* Register the IRQ and issue a command that will cause an interrupt. If an