crypto: atmel-sha204a - Fix error codes in OTP reads
PATCH 1/1 crypto: atmel-sha204a - Fix error codes in OTP reads
Return -EINVAL from atmel_i2c_init_read_otp_cmd() on invalid addresses instead of -1. Since the OTP zone is accessed in 4-byte blocks, valid addresses range from 0 to OTP_ZONE_SIZE / 4 - 1. Fix the bounds check accordingly.
In atmel_sha204a_otp_read(), propagate the actual error code from atmel_i2c_init_read_otp_cmd() instead of -1. Also, return -EIO instead of -EINVAL when the device is not ready.
---
Compile-tested only.
--- drivers/crypto/atmel-i2c.c | 4 ++-- drivers/crypto/atmel-sha204a.c | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-)
drivers/crypto/atmel-i2c.c +2 -2
@@ -72,8 +72,8 @@ EXPORT_SYMBOL(atmel_i2c_init_read_config_cmd);int atmel_i2c_init_read_otp_cmd (struct atmel_i2c_cmd *cmd,u16 addr) { -if (addr <0 || addr >OTP_ZONE_SIZE ) -return -1 ; +if (addr >=OTP_ZONE_SIZE /4 ) +return -EINVAL ; cmd->word_addr =COMMAND ; cmd->opcode =OPCODE_READ ;
drivers/crypto/atmel-sha204a.c +4 -3
@@ -94,9 +94,10 @@ static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max,static int atmel_sha204a_otp_read (struct i2c_client *client,u16 addr,u8 *otp) {struct atmel_i2c_cmd cmd; -int ret =-1 ; +int ret; -if (atmel_i2c_init_read_otp_cmd (&cmd, addr) <0 ) { + ret =atmel_i2c_init_read_otp_cmd (&cmd, addr); +if (ret <0 ) {dev_err (&client->dev,"failed, invalid otp address %04X\n" , addr);return ret;
@@ -106,7 +107,7 @@ static int atmel_sha204a_otp_read(struct i2c_client *client, u16 addr, u8 *otp)if (cmd.data[0 ] ==0xff ) {dev_err (&client->dev,"failed, device not ready\n" ); -return -EINVAL ; +return -EIO ; }memcpy (otp, cmd.data+1 ,4 );
Signature · 2 lines
Thorsten Blum <[email protected]> GPG: 1D60 735E 8AEF 3BE4 73B6 9D84 7336 78FD 8DFE EAD4
Hi, the change works (doesn't break behavior at least) verified on hardware, LGTM.
I remember that time we had a small discussion on what is the right approach with the return
handling, and at least me was unsure about it. If this puts it straight I'll take it for me as take
away. Thank you Thorsten, and sorry for the fuzz.
Best,
L
Quoted 65 lines · Thorsten Blum
Return
-EINVALfromatmel_i2c_init_read_otp_cmd()on invalid addresses instead of -1. Since the OTP zone is accessed in 4-byte blocks, valid addresses range from 0 toOTP_ZONE_SIZE/ 4 - 1. Fix the bounds check accordingly.In
atmel_sha204a_otp_read(), propagate the actual error code fromatmel_i2c_init_read_otp_cmd()instead of -1. Also, return-EIOinstead of-EINVALwhen the device is not ready.Fixes e05ce444e9e5 ("crypto: atmel-sha204a - add reading from otp zone")Signed-off-by Thorsten Blum <[email protected]>---
Compile-tested only.--- drivers/crypto/atmel-i2c.c | 4 ++-- drivers/crypto/atmel-sha204a.c | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-)drivers/crypto/atmel-i2c.c +2
-2@@ -72,8 +72,8 @@ EXPORT_SYMBOL(atmel_i2c_init_read_config_cmd);int atmel_i2c_init_read_otp_cmd (struct atmel_i2c_cmd *cmd,u16 addr) { -if (addr <0 || addr >OTP_ZONE_SIZE ) -return -1 ; +if (addr >=OTP_ZONE_SIZE /4 ) +return -EINVAL ; cmd->word_addr =COMMAND ; cmd->opcode =OPCODE_READ ;drivers/crypto/atmel-sha204a.c +4
-4@@ -94,9 +94,10 @@ static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max,static int atmel_sha204a_otp_read (struct i2c_client *client,u16 addr,u8 *otp) {struct atmel_i2c_cmd cmd; -int ret =-1 ; +int ret; -if (atmel_i2c_init_read_otp_cmd (&cmd, addr) <0 ) { + ret =atmel_i2c_init_read_otp_cmd (&cmd, addr); +if (ret <0 ) {dev_err (&client->dev,"failed, invalid otp address %04X\n" , addr);return ret;@@ -106,7 +107,7 @@ static int atmel_sha204a_otp_read(struct i2c_client *client, u16 addr, u8 *otp)if (cmd.data[0 ] ==0xff ) {dev_err (&client->dev,"failed, device not ready\n" ); -return -EINVAL ; +return -EIO ; }memcpy (otp, cmd.data+1 ,4 ); --Thorsten Blum <[email protected]>
GPG: 1D60 735E 8AEF 3BE4 73B6 9D84 7336 78FD 8DFE EAD4
Hi, find some comments below inlined.
Quoted 42 lines · Thorsten Blum
Return
-EINVALfromatmel_i2c_init_read_otp_cmd()on invalid addresses instead of -1. Since the OTP zone is accessed in 4-byte blocks, valid addresses range from 0 toOTP_ZONE_SIZE/ 4 - 1. Fix the bounds check accordingly.In
atmel_sha204a_otp_read(), propagate the actual error code fromatmel_i2c_init_read_otp_cmd()instead of -1. Also, return-EIOinstead of-EINVALwhen the device is not ready.Fixes e05ce444e9e5 ("crypto: atmel-sha204a - add reading from otp zone")Signed-off-by Thorsten Blum <[email protected]>---
Compile-tested only.--- drivers/crypto/atmel-i2c.c | 4 ++-- drivers/crypto/atmel-sha204a.c | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-)drivers/crypto/atmel-i2c.c +2
-2@@ -72,8 +72,8 @@ EXPORT_SYMBOL(atmel_i2c_init_read_config_cmd);int atmel_i2c_init_read_otp_cmd (struct atmel_i2c_cmd *cmd,u16 addr) { -if (addr <0 || addr >OTP_ZONE_SIZE ) -return -1 ; +if (addr >=OTP_ZONE_SIZE /4 ) +return -EINVAL ; cmd->word_addr =COMMAND ; cmd->opcode =OPCODE_READ ;drivers/crypto/atmel-sha204a.c +0
-0@@ -94,9 +94,10 @@ static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max,static int atmel_sha204a_otp_read (struct i2c_client *client,u16 addr,u8 *otp) {
drivers/crypto/atmel-sha204a.c static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max, · line 103
Quoted 10 lines
drivers/crypto/atmel-sha204a.c +3
-2struct atmel_i2c_cmd cmd; -int ret =-1 ; +int ret; -if (atmel_i2c_init_read_otp_cmd (&cmd, addr) <0 ) { + ret =atmel_i2c_init_read_otp_cmd (&cmd, addr); +if (ret <0 ) {dev_err (&client->dev,"failed, invalid otp address %04X\n" , addr);return ret;
Since I2C bus errors are caught here.
drivers/crypto/atmel-sha204a.c static int atmel_sha204a_otp_read(struct i2c_client *client, u16 addr, u8 *otp) · line 110
Quoted 6 lines
drivers/crypto/atmel-sha204a.c +1
-1@@ -106,7 +107,7 @@ static int atmel_sha204a_otp_read(struct i2c_client *client, u16 addr, u8 *otp)if (cmd.data[0 ] ==0xff ) {dev_err (&client->dev,"failed, device not ready\n" ); -return -EINVAL ; +return -EIO ;
The cmd.data holding 0xff here is not a bus error. AFAIR it can have to do with the locking state, pre-initialization, typically the atmel watchdog kicked in / timeout, etc - so the response is invalid, although hardware connection (I2C) is supposed to work. Currently the caller of this function does not distinguish anyway.
But why is EIO preferable here, over EINVAL?
Quoted 7 lines
drivers/crypto/atmel-sha204a.c +0
-1}memcpy (otp, cmd.data+1 ,4 ); --Thorsten Blum <[email protected]>
GPG: 1D60 735E 8AEF 3BE4 73B6 9D84 7336 78FD 8DFE EAD4
Quoted 29 lines · Lothar Rubusch
Quoted 16 lines · Thorsten Blum
Return
-EINVALfromatmel_i2c_init_read_otp_cmd()on invalid addresses instead of -1. Since the OTP zone is accessed in 4-byte blocks, valid addresses range from 0 toOTP_ZONE_SIZE/ 4 - 1. Fix the bounds check accordingly.In
atmel_sha204a_otp_read(), propagate the actual error code fromatmel_i2c_init_read_otp_cmd()instead of -1. Also, return-EIOinstead of-EINVALwhen the device is not ready.Fixes e05ce444e9e5 ("crypto: atmel-sha204a - add reading from otp zone")Signed-off-by Thorsten Blum <[email protected]>---
[...]drivers/crypto/atmel-sha204a.c static int atmel_sha204a_otp_read(struct i2c_client *client, u16 addr, u8 *otp) · line 110
Quoted 5 lines
drivers/crypto/atmel-sha204a.c +1
-1@@ -106,7 +107,7 @@ static int atmel_sha204a_otp_read(struct i2c_client *client, u16 addr, u8 *otp)if (cmd.data[0 ] ==0xff ) {dev_err (&client->dev,"failed, device not ready\n" ); -return -EINVAL ; +return -EIO ;The cmd.data holding
0xffhere is not a bus error. AFAIR it can have to do with the locking state, pre-initialization, typically the atmel watchdog kicked in / timeout, etc - so the response is invalid, although hardware connection (I2C) is supposed to work. Currently the caller of this function does not distinguish anyway.
Quoted 1 line
But why is EIO preferable here, over EINVAL?
AFAIK, -EINVAL is used for invalid arguments or bad input passed by the caller, which is why the address range check returns -EINVAL.
-EIO signals an I/O error or communication failure, e.g., the caller passed a valid address, but the device isn't ready yet, for whatever reason. Maybe -EAGAIN or -EBUSY instead? -EIO seemed like the most reasonable choice to me.
Since the error code will be propagated by my other patch [1], now would probably be a good time to adjust it.
Thanks,
Thorsten
Herbert Xu
maintainerQuoted 12 lines · Thorsten Blum
Return
-EINVALfromatmel_i2c_init_read_otp_cmd()on invalid addresses instead of -1. Since the OTP zone is accessed in 4-byte blocks, valid addresses range from 0 toOTP_ZONE_SIZE/ 4 - 1. Fix the bounds check accordingly.In
atmel_sha204a_otp_read(), propagate the actual error code fromatmel_i2c_init_read_otp_cmd()instead of -1. Also, return-EIOinstead of-EINVALwhen the device is not ready.Fixes e05ce444e9e5 ("crypto: atmel-sha204a - add reading from otp zone")Signed-off-by Thorsten Blum <[email protected]>
Quoted 6 lines
---
Compile-tested only.--- drivers/crypto/atmel-i2c.c | 4 ++-- drivers/crypto/atmel-sha204a.c | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-)
Patch applied. Thanks.
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Quoted 20 lines · Herbert Xu
Quoted 12 lines · Thorsten Blum
Return
-EINVALfromatmel_i2c_init_read_otp_cmd()on invalid addresses instead of -1. Since the OTP zone is accessed in 4-byte blocks, valid addresses range from 0 toOTP_ZONE_SIZE/ 4 - 1. Fix the bounds check accordingly.In
atmel_sha204a_otp_read(), propagate the actual error code fromatmel_i2c_init_read_otp_cmd()instead of -1. Also, return-EIOinstead of-EINVALwhen the device is not ready.Fixes e05ce444e9e5 ("crypto: atmel-sha204a - add reading from otp zone")Signed-off-by Thorsten Blum <[email protected]>Quoted 6 lines
---
Compile-tested only.
---drivers/crypto/atmel-i2c.c| 4 ++--drivers/crypto/atmel-sha204a.c| 7 ++++---2 files changed, 6 insertions(+), 5 deletions(-)
Quoted 1 line
Patch applied. Thanks.
Hi Herbert,
I also submitted [1], which combines this patch here and patch [2] after Lothar suggested to squash them. Feel free to apply them separately or together. Just FYI.
Thanks,
Thorsten
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/