* [PATCH] crypto: atmel-sha204a - Fix OTP sysfs read and error handling
@ 2026-02-15 12:41 Thorsten Blum
2026-02-15 21:09 ` Lothar Rubusch
0 siblings, 1 reply; 5+ messages in thread
From: Thorsten Blum @ 2026-02-15 12:41 UTC (permalink / raw)
To: Herbert Xu, David S. Miller, Nicolas Ferre, Alexandre Belloni,
Claudiu Beznea, Lothar Rubusch
Cc: Thorsten Blum, stable, linux-crypto, linux-arm-kernel,
linux-kernel
Fix otp_show() to read and print all 64 bytes of the OTP zone.
Previously, the loop only printed half of the OTP (32 bytes), and
partial output was returned on read errors.
Propagate the actual error from atmel_sha204a_otp_read() instead of
producing partial output.
Replace sprintf() with sysfs_emit_at(), which is preferred for
formatting sysfs output because it provides safer bounds checking.
Cc: stable@vger.kernel.org
Fixes: 13909a0c8897 ("crypto: atmel-sha204a - provide the otp content")
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
Compile-tested only.
---
drivers/crypto/atmel-sha204a.c | 19 ++++++++++---------
1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c
index 0fcf4a39de27..793c8d739a0a 100644
--- a/drivers/crypto/atmel-sha204a.c
+++ b/drivers/crypto/atmel-sha204a.c
@@ -15,6 +15,7 @@
#include <linux/module.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
+#include <linux/sysfs.h>
#include <linux/workqueue.h>
#include "atmel-i2c.h"
@@ -119,21 +120,21 @@ static ssize_t otp_show(struct device *dev,
{
u16 addr;
u8 otp[OTP_ZONE_SIZE];
- char *str = buf;
struct i2c_client *client = to_i2c_client(dev);
- int i;
+ ssize_t len = 0;
+ int i, ret;
- for (addr = 0; addr < OTP_ZONE_SIZE/4; addr++) {
- if (atmel_sha204a_otp_read(client, addr, otp + addr * 4) < 0) {
+ for (addr = 0; addr < OTP_ZONE_SIZE / 4; addr++) {
+ ret = atmel_sha204a_otp_read(client, addr, otp + addr * 4);
+ if (ret < 0) {
dev_err(dev, "failed to read otp zone\n");
- break;
+ return ret;
}
}
- for (i = 0; i < addr*2; i++)
- str += sprintf(str, "%02X", otp[i]);
- str += sprintf(str, "\n");
- return str - buf;
+ for (i = 0; i < OTP_ZONE_SIZE; i++)
+ len += sysfs_emit_at(buf, len, "%02X", otp[i]);
+ return sysfs_emit_at(buf, len, "\n");
}
static DEVICE_ATTR_RO(otp);
--
Thorsten Blum <thorsten.blum@linux.dev>
GPG: 1D60 735E 8AEF 3BE4 73B6 9D84 7336 78FD 8DFE EAD4
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH] crypto: atmel-sha204a - Fix OTP sysfs read and error handling 2026-02-15 12:41 [PATCH] crypto: atmel-sha204a - Fix OTP sysfs read and error handling Thorsten Blum @ 2026-02-15 21:09 ` Lothar Rubusch 2026-02-15 21:48 ` Thorsten Blum 0 siblings, 1 reply; 5+ messages in thread From: Lothar Rubusch @ 2026-02-15 21:09 UTC (permalink / raw) To: Thorsten Blum Cc: Herbert Xu, David S. Miller, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea, stable, linux-crypto, linux-arm-kernel, linux-kernel Hi Thorsten! I tried to verify your patch on hardware today, unfortunately it did not work for me. My setup works with current atsha204a module in the below described way. When trying to dump the OTP zone on exactly the same hardware with a patched module, it only prints '0' and nothing more, see below. Pls, let me know, if I'm doing something wrong; if my usage is wrong or if my approach is somehow flaky. Otherwise, could you please have a second look at it? Note, my patch at the time emerged from a particular use-case at work, it's likely that it's not covering all OTP. So, if incomplete, I'd really appreciate having it fixed. In the following I provide some details on what I did today. 8<-------------------------------------------------------------->8 My lab DUT hardware: - RPi 3b+ with an ATSHA204A shield via i2c, 3.3V - ATSHA204a with a locked config; initialized OTP zone with some values - Just enough to be readable i.e. verify OTP content My kernel source base, since on a RPi v6.19 branch, built w/ aarch64. Note, I'm building the module then out-of-source with a separate .dtsi (I'm not showing all the boiler-plate stuff here). $ git remote -v origin https://github.com/raspberrypi/linux (fetch) origin https://github.com/raspberrypi/linux (push) $ git log --oneline -3 e150a7a6d683 (HEAD -> rpi-6.19.y, origin/rpi-6.19.y) configs: enable Si5351 i2c common clock driver 3c957f9e74de pcie-brcmstb: move the unilateral disable of CLKREQ# before link-up 994331c6ea59 drivers: media: pispbe: Add V4L2_PIX_FMT_NV12MT_COL128 format support From the bootlog ... [ 0.000000][ T0] Kernel command line: coherent_pool=1M 8250.nr_uarts=1 snd_bcm2835.enable_headphones=0 cgroup_disable=memory bcm2708_fb.fbwidth=720 bcm2708_fb.fbheight=480 bcm2708_fb.fbswap=1 vc_mem.mem_base=0x3ec00000 vc_mem.mem_size=0x40000000 dwc_otg .lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait [ 0.000000][ T0] cgroup: Disabling memory control group subsystem [ 0.000000][ T0] Kernel parameter elevator= does not have any effect anymore. [ 0.000000][ T0] Please use sysfs to set IO scheduler for individual devices. [ 0.000000][ T0] printk: log buffer data + meta data: 131072 + 458752 = 589824 bytes ... ...current atmel-sha204a: root@dut02:~/atsha204a-orig# insmod atmel-i2c.ko root@dut02:~/atsha204a-orig# insmod atmel-sha204a.ko root@dut02:~/atsha204a-orig# cat /sys/bus/i2c/devices/1-0064/atsha204a/otp 0001ED86032D0002154C033750FFFFFF20B0F703DB0CFFFFFFFFFFFFFFFFFFFF reboot... ...atmel-sha204a with patch applied: root@dut02:~/atsha204a# modprobe i2c-dev root@dut02:~/atsha204a# insmod ./atmel-i2c.ko root@dut02:~/atsha204a# insmod ./atmel-sha204a.ko root@dut02:~/atsha204a# cat /sys/bus/i2c/devices/1-0064/atsha204a/otp 0root@dut02:~/atsha204a# root@dut02:~/atsha204a# xxd /sys/bus/i2c/devices/1-0064/atsha204a/otp 00000000: 30 0 8<-------------------------------------------------------------->8 Best, L On Sun, Feb 15, 2026 at 1:42 PM Thorsten Blum <thorsten.blum@linux.dev> wrote: > > Fix otp_show() to read and print all 64 bytes of the OTP zone. > Previously, the loop only printed half of the OTP (32 bytes), and > partial output was returned on read errors. > > Propagate the actual error from atmel_sha204a_otp_read() instead of > producing partial output. > > Replace sprintf() with sysfs_emit_at(), which is preferred for > formatting sysfs output because it provides safer bounds checking. > > Cc: stable@vger.kernel.org > Fixes: 13909a0c8897 ("crypto: atmel-sha204a - provide the otp content") > Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev> > --- > Compile-tested only. > --- > drivers/crypto/atmel-sha204a.c | 19 ++++++++++--------- > 1 file changed, 10 insertions(+), 9 deletions(-) > > diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c > index 0fcf4a39de27..793c8d739a0a 100644 > --- a/drivers/crypto/atmel-sha204a.c > +++ b/drivers/crypto/atmel-sha204a.c > @@ -15,6 +15,7 @@ > #include <linux/module.h> > #include <linux/scatterlist.h> > #include <linux/slab.h> > +#include <linux/sysfs.h> > #include <linux/workqueue.h> > #include "atmel-i2c.h" > > @@ -119,21 +120,21 @@ static ssize_t otp_show(struct device *dev, > { > u16 addr; > u8 otp[OTP_ZONE_SIZE]; > - char *str = buf; > struct i2c_client *client = to_i2c_client(dev); > - int i; > + ssize_t len = 0; > + int i, ret; > > - for (addr = 0; addr < OTP_ZONE_SIZE/4; addr++) { > - if (atmel_sha204a_otp_read(client, addr, otp + addr * 4) < 0) { > + for (addr = 0; addr < OTP_ZONE_SIZE / 4; addr++) { > + ret = atmel_sha204a_otp_read(client, addr, otp + addr * 4); > + if (ret < 0) { > dev_err(dev, "failed to read otp zone\n"); > - break; > + return ret; > } > } > > - for (i = 0; i < addr*2; i++) > - str += sprintf(str, "%02X", otp[i]); > - str += sprintf(str, "\n"); > - return str - buf; > + for (i = 0; i < OTP_ZONE_SIZE; i++) > + len += sysfs_emit_at(buf, len, "%02X", otp[i]); > + return sysfs_emit_at(buf, len, "\n"); > } > static DEVICE_ATTR_RO(otp); > > -- > Thorsten Blum <thorsten.blum@linux.dev> > GPG: 1D60 735E 8AEF 3BE4 73B6 9D84 7336 78FD 8DFE EAD4 > ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] crypto: atmel-sha204a - Fix OTP sysfs read and error handling 2026-02-15 21:09 ` Lothar Rubusch @ 2026-02-15 21:48 ` Thorsten Blum 2026-02-16 6:14 ` Lothar Rubusch 0 siblings, 1 reply; 5+ messages in thread From: Thorsten Blum @ 2026-02-15 21:48 UTC (permalink / raw) To: Lothar Rubusch Cc: Herbert Xu, David S. Miller, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea, stable, linux-crypto, linux-arm-kernel, linux-kernel On 15. Feb 2026, at 22:09, Lothar Rubusch wrote: > I tried to verify your patch on hardware today, unfortunately it did > not work for me. > > My setup works with current atsha204a module in the below described way. When > trying to dump the OTP zone on exactly the same hardware with a patched module, > it only prints '0' and nothing more, see below. > > [...] Hi Lothar, thank you for your feedback. I made a small mistake in the return value where I forgot to add the previous length 'len'. Sorry about that! Unfortunately, I don't have the hardware right now to test this - could you try if it works with the following change? Thanks, Thorsten diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c index 793c8d739a0a..431672517dba 100644 --- a/drivers/crypto/atmel-sha204a.c +++ b/drivers/crypto/atmel-sha204a.c @@ -134,7 +134,7 @@ static ssize_t otp_show(struct device *dev, for (i = 0; i < OTP_ZONE_SIZE; i++) len += sysfs_emit_at(buf, len, "%02X", otp[i]); - return sysfs_emit_at(buf, len, "\n"); + return len + sysfs_emit_at(buf, len, "\n"); } static DEVICE_ATTR_RO(otp); ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] crypto: atmel-sha204a - Fix OTP sysfs read and error handling 2026-02-15 21:48 ` Thorsten Blum @ 2026-02-16 6:14 ` Lothar Rubusch 2026-02-16 7:53 ` Thorsten Blum 0 siblings, 1 reply; 5+ messages in thread From: Lothar Rubusch @ 2026-02-16 6:14 UTC (permalink / raw) To: Thorsten Blum Cc: Herbert Xu, David S. Miller, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea, stable, linux-crypto, linux-arm-kernel, linux-kernel Hi Thorsten, On Sun, Feb 15, 2026 at 10:48 PM Thorsten Blum <thorsten.blum@linux.dev> wrote: > > On 15. Feb 2026, at 22:09, Lothar Rubusch wrote: > > I tried to verify your patch on hardware today, unfortunately it did > > not work for me. > > > > My setup works with current atsha204a module in the below described way. When > > trying to dump the OTP zone on exactly the same hardware with a patched module, > > it only prints '0' and nothing more, see below. > > > > [...] > > Hi Lothar, > > thank you for your feedback. I made a small mistake in the return value > where I forgot to add the previous length 'len'. Sorry about that! > > Unfortunately, I don't have the hardware right now to test this - could > you try if it works with the following change? > > Thanks, > Thorsten > > > diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c > index 793c8d739a0a..431672517dba 100644 > --- a/drivers/crypto/atmel-sha204a.c > +++ b/drivers/crypto/atmel-sha204a.c > @@ -134,7 +134,7 @@ static ssize_t otp_show(struct device *dev, > > for (i = 0; i < OTP_ZONE_SIZE; i++) > len += sysfs_emit_at(buf, len, "%02X", otp[i]); > - return sysfs_emit_at(buf, len, "\n"); > + return len + sysfs_emit_at(buf, len, "\n"); > } > static DEVICE_ATTR_RO(otp); > This would work. I'd squash this fixup together with the proposed patch and resubmit a fixed version. 8<-------------------------------------------------------------->8 root@dut02:~/atsha204a-modif# insmod atmel-i2c.ko root@dut02:~/atsha204a-modif# insmod atmel-sha204a.ko root@dut02:~/atsha204a-modif# cat /sys/bus/i2c/devices/1-0064/atsha204a/otp 0001ED86032D0002154C033750FFFFFF20B0F703DB0CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 8<-------------------------------------------------------------->8 Best, L ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] crypto: atmel-sha204a - Fix OTP sysfs read and error handling 2026-02-16 6:14 ` Lothar Rubusch @ 2026-02-16 7:53 ` Thorsten Blum 0 siblings, 0 replies; 5+ messages in thread From: Thorsten Blum @ 2026-02-16 7:53 UTC (permalink / raw) To: Lothar Rubusch Cc: Herbert Xu, David S. Miller, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea, stable, linux-crypto, linux-arm-kernel, linux-kernel On 16. Feb 2026, at 07:14, Lothar Rubusch wrote: > [...] > > This would work. I'd squash this fixup together with the proposed > patch and resubmit a fixed version. > > 8<-------------------------------------------------------------->8 > root@dut02:~/atsha204a-modif# insmod atmel-i2c.ko > root@dut02:~/atsha204a-modif# insmod atmel-sha204a.ko > root@dut02:~/atsha204a-modif# cat /sys/bus/i2c/devices/1-0064/atsha204a/otp > 0001ED86032D0002154C033750FFFFFF20B0F703DB0CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF > 8<-------------------------------------------------------------->8 Thanks for verifying and testing. v2 can be found here: https://lore.kernel.org/lkml/20260216074552.656814-1-thorsten.blum@linux.dev/ Best, Thorsten ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-02-16 7:54 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-02-15 12:41 [PATCH] crypto: atmel-sha204a - Fix OTP sysfs read and error handling Thorsten Blum 2026-02-15 21:09 ` Lothar Rubusch 2026-02-15 21:48 ` Thorsten Blum 2026-02-16 6:14 ` Lothar Rubusch 2026-02-16 7:53 ` Thorsten Blum
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox