fix bad constant expressions in tpm.h
cover fix bad constant expressions in tpm.h
Even though the cpu_to_be32() is open coded it is not a good convention to call it in the enum definitions. This will also sparse errors about invalid constant expressions.
Ed Swierk (1):
tpm: Clean up reading of timeout and duration capabilities
Jarkko Sakkinen (1):
tpm: fix bad constant expressions
drivers/char/tpm/tpm-interface.c | 78 +++++++++++++--------------------------- drivers/char/tpm/tpm-sysfs.c | 4 +-- drivers/char/tpm/tpm.h | 23 ++++++------ 3 files changed, 38 insertions(+), 67 deletions(-)
Signature · 1 line
2.7.4
PATCH 1/2 tpm: Clean up reading of timeout and duration capabilities
From: Ed Swierk <eswierk-FilZDy9cOaHkQYj/[email protected]>
Call tpm_getcap() from tpm_get_timeouts() to eliminate redundant code. Return all errors to the caller rather than swallowing them (e.g. when tpm_transmit_cmd() returns nonzero).
--- drivers/char/tpm/tpm-interface.c | 65 +++++++++++----------------------------- 1 file changed, 17 insertions(+), 48 deletions(-)
drivers/char/tpm/tpm-interface.c +17 -48
@@ -488,10 +488,9 @@ static int tpm_startup(struct tpm_chip *chip, __be16 startup_type)int tpm_get_timeouts (struct tpm_chip *chip) { -struct tpm_cmd_t tpm_cmd; +cap_t cap;unsigned long new_timeout[4 ];unsigned long old_timeout[4 ]; -struct duration_t *duration_cap;ssize_t rc;if (chip->flags &TPM_CHIP_FLAG_TPM2 ) {
@@ -509,43 +508,25 @@ 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 =tpm_getcap (chip,TPM_CAP_PROP_TIS_TIMEOUT , &cap, +"attempting to determine the timeouts" );if (rc ==TPM_ERR_INVALID_POSTINIT ) {/* The TPM is not started, we are the first to talk to it. Execute a startup command. */ -dev_info (&chip->dev,"Issuing TPM_STARTUP" ); +dev_info (&chip->dev,"Issuing TPM_STARTUP\n" );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 =tpm_getcap (chip,TPM_CAP_PROP_TIS_TIMEOUT , &cap, +"attempting to determine the timeouts" ); } -if (rc) { -dev_err (&chip->dev, -"A TPM error (%zd) occurred attempting to determine the timeouts\n" , - rc); - goto duration; - } - -if (be32_to_cpu (tpm_cmd.header.out.return_code) !=0 || -be32_to_cpu (tpm_cmd.header.out.length) - !=sizeof (tpm_cmd.header.out) +sizeof (u32) +4 *sizeof (u32)) -return -EINVAL ; +if (rc) +return rc; - old_timeout[0 ] =be32_to_cpu (tpm_cmd.params.getcap_out.cap.timeout.a); - old_timeout[1 ] =be32_to_cpu (tpm_cmd.params.getcap_out.cap.timeout.b); - old_timeout[2 ] =be32_to_cpu (tpm_cmd.params.getcap_out.cap.timeout.c); - old_timeout[3 ] =be32_to_cpu (tpm_cmd.params.getcap_out.cap.timeout.d); + old_timeout[0 ] =be32_to_cpu (cap.timeout.a); + old_timeout[1 ] =be32_to_cpu (cap.timeout.b); + old_timeout[2 ] =be32_to_cpu (cap.timeout.c); + old_timeout[3 ] =be32_to_cpu (cap.timeout.d);memcpy (new_timeout, old_timeout,sizeof (new_timeout));/*
@@ -583,29 +564,17 @@ int tpm_get_timeouts(struct tpm_chip *chip) chip->timeout_c =usecs_to_jiffies (new_timeout[2 ]); 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 =tpm_getcap (chip,TPM_CAP_PROP_TIS_DURATION , &cap, +"attempting to determine the durations" );if (rc)return rc; -if (be32_to_cpu (tpm_cmd.header.out.return_code) !=0 || -be32_to_cpu (tpm_cmd.header.out.length) - !=sizeof (tpm_cmd.header.out) +sizeof (u32) +3 *sizeof (u32)) -return -EINVAL ; - - duration_cap = &tpm_cmd.params.getcap_out.cap.duration; chip->duration[TPM_SHORT ] = -usecs_to_jiffies (be32_to_cpu (duration_cap->tpm_short)); +usecs_to_jiffies (be32_to_cpu (cap.duration.tpm_short)); chip->duration[TPM_MEDIUM ] = -usecs_to_jiffies (be32_to_cpu (duration_cap->tpm_medium)); +usecs_to_jiffies (be32_to_cpu (cap.duration.tpm_medium)); chip->duration[TPM_LONG ] = -usecs_to_jiffies (be32_to_cpu (duration_cap->tpm_long)); +usecs_to_jiffies (be32_to_cpu (cap.duration.tpm_long));/* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above * value wrong and apparently reports msecs rather than usecs. So we
Signature · 4 lines
2.7.4 ------------------------------------------------------------------------------
PATCH 2/2 tpm: fix bad constant expressions
In reply to Jarkko SakkinenSparse reports "bad constant expression" for the use of cpu_to_be32 inside enums tpm_capabilities and tpm_sub_capabilities. Even though it is probably expanded to a constant expression, it is probably cleaner not to use it there.
--- drivers/char/tpm/tpm-interface.c | 13 ++++++++----- drivers/char/tpm/tpm-sysfs.c | 4 ++-- drivers/char/tpm/tpm.h | 23 +++++++++++------------ 3 files changed, 21 insertions(+), 19 deletions(-)
drivers/char/tpm/tpm-interface.c +8 -5
@@ -444,19 +444,22 @@ ssize_t tpm_getcap(struct tpm_chip *chip, __be32 subcap_id, cap_t *cap,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; +if (subcap_id ==TPM_CAP_VERSION_1_1 || + subcap_id ==TPM_CAP_VERSION_1_2 ) { + tpm_cmd.params.getcap_in.cap =cpu_to_be32 (subcap_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 ; + tpm_cmd.params.getcap_in.cap = +cpu_to_be32 (TPM_CAP_FLAG );else - tpm_cmd.params.getcap_in.cap =TPM_CAP_PROP ; + tpm_cmd.params.getcap_in.cap = +cpu_to_be32 (TPM_CAP_PROP ); 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);
drivers/char/tpm/tpm-sysfs.c +2 -2
@@ -193,7 +193,7 @@ static ssize_t caps_show(struct device *dev, struct device_attribute *attr, 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, + rc =tpm_getcap (chip,TPM_CAP_VERSION_1_2 , &cap,"attempting to determine the 1.2 version" );if (!rc) { str += sprintf(str,
@@ -204,7 +204,7 @@ 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, + rc =tpm_getcap (chip,TPM_CAP_VERSION_1_1 , &cap,"attempting to determine the 1.1 version" );if (rc)return 0 ;
drivers/char/tpm/tpm.h +11 -12
@@ -282,21 +282,20 @@ typedef union { } 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 ) +TPM_CAP_FLAG =4 , +TPM_CAP_PROP =5 , +TPM_CAP_VERSION_1_1 =0x06 , +TPM_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 ), - +TPM_CAP_PROP_PCR =0x101 , +TPM_CAP_PROP_MANUFACTURER =0x103 , +TPM_CAP_FLAG_PERM =0x108 , +TPM_CAP_FLAG_VOL =0x109 , +TPM_CAP_PROP_OWNER =0x111 , +TPM_CAP_PROP_TIS_TIMEOUT =0x115 , +TPM_CAP_PROP_TIS_DURATION =0x120 , };struct tpm_getcap_params_in {
Signature · 4 lines
2.7.4 ------------------------------------------------------------------------------
Re: [PATCH 1/2] tpm: Clean up reading of timeout and duration capabilities
In reply to Jarkko SakkinenQuoted 5 lines · Jarkko Sakkinen
From: Ed Swierk <[email protected]>
Call
tpm_getcap()fromtpm_get_timeouts()to eliminate redundant code. Return all errors to the caller rather than swallowing them (e.g. whentpm_transmit_cmd()returns nonzero).
I didn't look at these closely, but I agree with the idea.
Jason
RE: [tpmdd-devel] [PATCH 2/2] tpm: fix bad constant expressions
In reply to Jarkko SakkinenQuoted 9 lines
-----Original Message-----
From: Jarkko Sakkinen [mailto:jarkko.sakkinen-VuQAYsv1563Yd54FQh9/[email protected]]
Sent: Monday, September 19, 2016 23:22
To: Peter Huewe <[email protected]>
Cc: moderated list:TPM DEVICE DRIVER <tpmdd-
[email protected]>; open list <[email protected]>
Subject: [tpmdd-devel] [PATCH 2/2] tpm: fix bad constant expressions
Quoted 3 lines
Sparse reports "bad constant expression" for the use of
cpu_to_be32inside enumstpm_capabilitiesandtpm_sub_capabilities. Even though it is probably expanded to a constant expression, it is probably cleaner not to use it there.
Swap macros were rewritten due to possible compiler bug https://lkml.org/lkml/2016/5/2/597
And the warning is actually a bug in sparse, it fails to detect constant expression now.
Tomas
Quoted 110 lines
--- drivers/char/tpm/tpm-interface.c | 13 ++++++++----- drivers/char/tpm/tpm-sysfs.c | 4 ++-- drivers/char/tpm/tpm.h | 23 +++++++++++------------ 3 files changed, 21 insertions(+), 19 deletions(-)drivers/char/tpm/tpm- renamed from drivers/char/tpm/tpm-interface.c +0
-0
interface.c
index ca6162e..db7359f 100644drivers/char/tpm/tpm-interface.c +8
-5@@ -444,19 +444,22 @@ ssize_t tpm_getcap(struct tpm_chip *chip, __be32 subcap_id, cap_t *cap, 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; +if (subcap_id ==TPM_CAP_VERSION_1_1 || + subcap_id ==TPM_CAP_VERSION_1_2 ) { + tpm_cmd.params.getcap_in.cap =cpu_to_be32 (subcap_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 ; + tpm_cmd.params.getcap_in.cap = +cpu_to_be32 (TPM_CAP_FLAG );else - tpm_cmd.params.getcap_in.cap =TPM_CAP_PROP ; + tpm_cmd.params.getcap_in.cap = +cpu_to_be32 (TPM_CAP_PROP ); 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);drivers/char/tpm/tpm-interface.c +0
-0} rc = tpm_transmit_cmd(chip, &tpm_cmd,
TPM_INTERNAL_RESULT_SIZE, 0,drivers/char/tpm/tpm-interface.c +0
-0desc);drivers/char/tpm/tpm-sysfs.c +1
-1@@ -193,7 +193,7 @@ static ssize_t caps_show(struct device *dev, struct device_attribute *attr, 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, + rc =tpm_getcap (chip,TPM_CAP_VERSION_1_2 , &cap,"attempting to determine the 1.2 version" );if (!rc) {drivers/char/tpm/tpm-sysfs.c +1
-1str += sprintf(str,@@ -204,7 +204,7 @@ 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, + rc =tpm_getcap (chip,TPM_CAP_VERSION_1_1 , &cap,"attempting to determine the 1.1 version" );if (rc)drivers/char/tpm/tpm-sysfs.c +0
-0return 0 ;drivers/char/tpm/tpm.h index renamed from drivers/char/tpm/tpm.h +0
-04d183c9..edc6f75 100644
drivers/char/tpm/tpm.h +11
-13@@ -282,21 +282,20 @@ typedef union { } 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 ) +TPM_CAP_FLAG =4 , +TPM_CAP_PROP =5 , +TPM_CAP_VERSION_1_1 =0x06 , +TPM_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 ), - +TPM_CAP_PROP_PCR =0x101 , +TPM_CAP_PROP_MANUFACTURER =0x103 , +TPM_CAP_FLAG_PERM =0x108 , +TPM_CAP_FLAG_VOL =0x109 , +TPM_CAP_PROP_OWNER =0x111 , +TPM_CAP_PROP_TIS_TIMEOUT =0x115 , +TPM_CAP_PROP_TIS_DURATION =0x120 , };struct tpm_getcap_params_in { --2.7.4
Quoted 5 lines
------------------------------------------------------------------------------
_______________________________________________
tpmdd-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/tpmdd-devel
------------------------------------------------------------------------------
Re: [tpmdd-devel] [PATCH 2/2] tpm: fix bad constant expressions
Quoted 9 lines · Tomas
Quoted 9 lines
-----Original Message-----
From: Jarkko Sakkinen [mailto:[email protected]]
Sent: Monday, September 19, 2016 23:22
To: Peter Huewe <[email protected]>
Cc: moderated list:TPM DEVICE DRIVER <tpmdd- [email protected]>; open list <[email protected]>
Subject: [tpmdd-devel] [PATCH 2/2] tpm: fix bad constant expressions
Quoted 7 lines
Quoted 3 lines
Sparse reports "bad constant expression" for the use of
cpu_to_be32inside enumstpm_capabilitiesandtpm_sub_capabilities. Even though it is probably expanded to a constant expression, it is probably cleaner not to use it there.Swap macros were rewritten due to possible compiler bug https://lkml.org/lkml/2016/5/2/597
And the warning is actually a bug in sparse, it fails to detect constant expression now.
I still think that these must be change in order to have some consistency in the subsystem. Now there are two differing conventions.
/Jarkko
RE: [tpmdd-devel] [PATCH 2/2] tpm: fix bad constant expressions
Quoted 18 lines · Tomas
Quoted 10 lines
Quoted 10 lines
-----Original Message-----
From: Jarkko Sakkinen [mailto:[email protected]]
Sent: Monday, September 19, 2016 23:22
To: Peter Huewe <[email protected]>
Cc: moderated list:TPM DEVICE DRIVER <tpmdd- [email protected]>; open list <[email protected]>
Subject: [tpmdd-devel] [PATCH 2/2] tpm: fix bad constant expressionsQuoted 3 lines
Quoted 3 lines
Sparse reports "bad constant expression" for the use of
cpu_to_be32inside enumstpm_capabilitiesandtpm_sub_capabilities. Even though it is probably expanded to a constant expression, it is probably cleanernot to use it there.
Quoted 2 lines
Quoted 7 lines
Quoted 3 lines
Swap macros were rewritten due to possible compiler bug
https://lkml.org/lkml/2016/5/2/597
And the warning is actually a bug in sparse, it fails to detect constantexpression now.
I still think that these must be change in order to have some consistency in the subsystem. Now there are two differing conventions.
Nothing against the convention just the commit message is misleading in that sense. Tomas
Re: [tpmdd-devel] [PATCH 2/2] tpm: fix bad constant expressions
Quoted 21 lines · Tomas
Quoted 21 lines · Tomas
Quoted 10 lines
Quoted 10 lines
-----Original Message-----
From: Jarkko Sakkinen [mailto:[email protected]]
Sent: Monday, September 19, 2016 23:22
To: Peter Huewe <[email protected]>
Cc: moderated list:TPM DEVICE DRIVER <tpmdd- [email protected]>; open list <[email protected]>
Subject: [tpmdd-devel] [PATCH 2/2] tpm: fix bad constant expressionsQuoted 3 lines
Quoted 3 lines
Sparse reports "bad constant expression" for the use of
cpu_to_be32inside enumstpm_capabilitiesandtpm_sub_capabilities. Even though it is probably expanded to a constant expression, it is probably cleanernot to use it there.
Quoted 5 lines
Swap macros were rewritten due to possible compiler bug
https://lkml.org/lkml/2016/5/2/597
And the warning is actually a bug in sparse, it fails to detect constant
Quoted 7 lines
Quoted 4 lines
expression now.
I still think that these must be change in order to have some consistency in the subsystem. Now there are two differing conventions.
Nothing against the convention just the commit message is misleading in that sense. Tomas
Right. I can edit it to say that "sanitize the constant declarations" or something like that.
/Jarkko
Jarkko Sakkinen
Tested-byRe: [PATCH 1/2] tpm: Clean up reading of timeout and duration capabilities
In reply to Jason GunthorpeQuoted 8 lines · Jason Gunthorpe
Quoted 5 lines · Jarkko Sakkinen
From: Ed Swierk <[email protected]>
Call
tpm_getcap()fromtpm_get_timeouts()to eliminate redundant code. Return all errors to the caller rather than swallowing them (e.g. whentpm_transmit_cmd()returns nonzero).I didn't look at these closely, but I agree with the idea.
/Jarkko
Re: [PATCH 1/2] tpm: Clean up reading of timeout and duration capabilities
Quoted 11 lines · Jarkko Sakkinen
Quoted 8 lines · Jason Gunthorpe
Quoted 5 lines · Jarkko Sakkinen
From: Ed Swierk <[email protected]>
Call
tpm_getcap()fromtpm_get_timeouts()to eliminate redundant code. Return all errors to the caller rather than swallowing them (e.g. whentpm_transmit_cmd()returns nonzero).I didn't look at these closely, but I agree with the idea.
Tested-by Jarkko Sakkinen <[email protected]>
I applied this.
/Jarkko