rng: Add Turris Mox rWTM RNG driver
PATCH v2 1/1 rng: Add Turris Mox rWTM RNG driver
A RNG driver for Armada 3720 boards running the Turris Mox rWTM firmware from CZ.NIC in the secure processor.
---
Changes in v2:
- Removed ring buffer implementation
drivers/rng/Kconfig | 8 +++ drivers/rng/Makefile | 1 + drivers/rng/turris_rwtm_rng.c | 122 ++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 drivers/rng/turris_rwtm_rng.c
drivers/rng/Kconfig +8 -0
@@ -105,4 +105,12 @@ config RNG_JH7110 help Enable True Random Number Generator in StarFive JH7110 SoCs. +config RNG_TURRIS_RWTM + bool "Turris Mox TRNG in Secure Processor" + depends on DM_RNG && ARMADA_3700 + help + Use TRNG in Turris Mox Secure Processor Firmware. Can be used + on other Armada-3700 devices (like EspressoBin) if Secure + Firmware from CZ.NIC is used. + endif
drivers/rng/Makefile +1 -0
@@ -17,3 +17,4 @@ obj-$(CONFIG_RNG_SMCCC_TRNG) += smccc_trng.o obj-$(CONFIG_RNG_ARM_RNDR) += arm_rndr.o obj-$(CONFIG_TPM_RNG) += tpm_rng.o obj-$(CONFIG_RNG_JH7110) += jh7110_rng.o +obj-$(CONFIG_RNG_TURRIS_RWTM) += turris_rwtm_rng.o
drivers/rng/turris_rwtm_rng.c new file +122 -0
122 lines not on this page · raw message
Signature · 1 line
2.43.0
Hello Max,
Out of curiousity, what is your use case for having these random numbers on this platform in U-Boot?
Below are a few more things to change and then you I'll give my Reviewed-by tag.
Quoted 85 lines · On Sun, 21 Jan 2024 21:17:16 +0100 Max Resch <[email protected]> wrote:
A RNG driver for Armada 3720 boards running the Turris Mox rWTM firmware from CZ.NIC in the secure processor.
Signed-off-by Max Resch <[email protected]>---
Changes in v2:
- Removed ring buffer implementationdrivers/rng/Kconfig | 8 +++ drivers/rng/Makefile | 1 + drivers/rng/turris_rwtm_rng.c | 122 ++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 drivers/rng/turris_rwtm_rng.cdrivers/rng/Kconfig +8
-0@@ -105,4 +105,12 @@ config RNG_JH7110 help Enable True Random Number Generator in StarFive JH7110 SoCs. +config RNG_TURRIS_RWTM + bool "Turris Mox TRNG in Secure Processor" + depends on DM_RNG && ARMADA_3700 + help + Use TRNG in Turris Mox Secure Processor Firmware. Can be used + on other Armada-3700 devices (like EspressoBin) if Secure + Firmware from CZ.NIC is used. + endifdrivers/rng/Makefile +1
-0@@ -17,3 +17,4 @@ obj-$(CONFIG_RNG_SMCCC_TRNG) += smccc_trng.o obj-$(CONFIG_RNG_ARM_RNDR) += arm_rndr.o obj-$(CONFIG_TPM_RNG) += tpm_rng.o obj-$(CONFIG_RNG_JH7110) += jh7110_rng.o +obj-$(CONFIG_RNG_TURRIS_RWTM) += turris_rwtm_rng.odrivers/rng/turris_rwtm_rng.c new file +38
-0@@ -0,0 +1,122 @@ +// SPDX-License-Identifier: GPL-2.0-or-later OR BSD-3-Clause +/* +* Copyright (c) 2024, Max Resch +*/ + +#include <dm.h> +#include <malloc.h> +#include <rng.h> +#include <asm/dma-mapping.h> +#include <asm/types.h> +#include <mach/mbox.h> + +/* size of entropy buffer */ +#define RNG_BUFFER_SIZE 128U + +struct turris_rwtm_rng_priv { +phys_addr_t buffer; +}; + +static int turris_rwtm_rng_fill_entropy (phys_addr_t entropy,size_t size) +{ +u32 args[3 ] = {1 , (u32 )entropy, size }; +int ret; + +/* flush data cache */ +flush_dcache_range (entropy, entropy + size); + +/* +* get entropy +* args[0] = 1 copies BYTES array in args[1] of length args[2] +*/ + ret =mbox_do_cmd (MBOX_CMD_GET_RANDOM , args,3 ,NULL ,0 ); +if (ret <0 ) +return ret; + +/* invalidate data cache */ +invalidate_dcache_range (entropy, entropy + size); +
drivers/rng/turris_rwtm_rng.c line 48
Quoted 10 lines
drivers/rng/turris_rwtm_rng.c new file +10
-0+return 0 ; +} + +static int turris_rwtm_rng_random_read (struct udevice *dev,void *data,size_t count) +{ +phys_addr_t p; +size_t size; +int ret; + + p = ((struct turris_rwtm_rng_priv *)dev_get_priv (dev))->buffer;
Please declare
struct turris_rwtm_rng_priv *priv = dev_get_priv(dev); phys_addr_t phys; size_t size; int ret;
and then do
phys = priv->buffer;
Quoted 6 lines
drivers/rng/turris_rwtm_rng.c new file +6
-0+while (count) { + size =min_t (size_t,RNG_BUFFER_SIZE , count); + + ret =turris_rwtm_rng_fill_entropy (p, size); + +memcpy (data, (void *)p, size);
drivers/rng/turris_rwtm_rng.c line 65
Quoted 10 lines
drivers/rng/turris_rwtm_rng.c new file +10
-0+ count -= size; + data = (u8 *)data + size; + } + +return 0 ; +} + +static int turris_rwtm_rng_probe (struct udevice *dev) +{ +struct turris_rwtm_rng_priv *priv;
= dev_get_priv(dev);
Quoted 4 lines
drivers/rng/turris_rwtm_rng.c new file +4
-0+u32 args[] = {0 }; +int ret; + +/*
drivers/rng/turris_rwtm_rng.c line 79
Quoted 10 lines
drivers/rng/turris_rwtm_rng.c new file +10
-0+* check if the random command is supported +* args[0] = 0 would copy 16 DWORDS entropy to out but we ignore them +*/ + ret =mbox_do_cmd (MBOX_CMD_GET_RANDOM , args,ARRAY_SIZE (args),NULL ,0 ); + +if (ret <0 ) +return ret; + +/* entropy buffer */ + priv = (struct turris_rwtm_rng_priv *)dev_get_priv (dev);
no need here, do it at the beginning
drivers/rng/turris_rwtm_rng.c line 92
Quoted 13 lines
drivers/rng/turris_rwtm_rng.c new file +13
-0+ priv->buffer =0 ; + +/* buffer address need to be aligned */ +dma_alloc_coherent (RNG_BUFFER_SIZE , (unsigned long *)&priv->buffer); +if (!priv->buffer) +return -ENOMEM ; + +return 0 ; +} + +static int turris_rwtm_rng_remove (struct udevice *dev) +{ +phys_addr_t p;
rename this to phys, and please do it as I described above.
drivers/rng/turris_rwtm_rng.c line 97
Quoted 5 lines
drivers/rng/turris_rwtm_rng.c new file +5
-0+ + p = ((struct turris_rwtm_rng_priv *)dev_get_priv (dev))->buffer; +dma_free_coherent ((void *)p); + +return 0 ;
Marek
Quoted 7 lines
Date: Mon, 5 Feb 2024 12:40:14 +0100
From: Marek Behún <[email protected]>Hello Max,
Out of curiousity, what is your use case for having these random numbers on this platform in U-Boot?
It means that U-Boot will implement the EFI RNG protocol. The OpenBSD bootloader will use that to fill an initial pool of random numbers for the kernel. And the Linux kernel EFI stub uses it to do kernel address space randomization.
Cheers,
Mark
Quoted 172 lines
Below are a few more things to change and then you I'll give my Reviewed-by tag.
Quoted 85 lines · On Sun, 21 Jan 2024 21:17:16 +0100 Max Resch <[email protected]> wrote:
A RNG driver for Armada 3720 boards running the Turris Mox rWTM firmware from CZ.NIC in the secure processor.
Signed-off-by Max Resch <[email protected]>---
Changes in v2:
- Removed ring buffer implementationdrivers/rng/Kconfig | 8 +++ drivers/rng/Makefile | 1 + drivers/rng/turris_rwtm_rng.c | 122 ++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 drivers/rng/turris_rwtm_rng.cdrivers/rng/Kconfig +8
-0@@ -105,4 +105,12 @@ config RNG_JH7110 help Enable True Random Number Generator in StarFive JH7110 SoCs. +config RNG_TURRIS_RWTM + bool "Turris Mox TRNG in Secure Processor" + depends on DM_RNG && ARMADA_3700 + help + Use TRNG in Turris Mox Secure Processor Firmware. Can be used + on other Armada-3700 devices (like EspressoBin) if Secure + Firmware from CZ.NIC is used. + endifdrivers/rng/Makefile +1
-0@@ -17,3 +17,4 @@ obj-$(CONFIG_RNG_SMCCC_TRNG) += smccc_trng.o obj-$(CONFIG_RNG_ARM_RNDR) += arm_rndr.o obj-$(CONFIG_TPM_RNG) += tpm_rng.o obj-$(CONFIG_RNG_JH7110) += jh7110_rng.o +obj-$(CONFIG_RNG_TURRIS_RWTM) += turris_rwtm_rng.odrivers/rng/turris_rwtm_rng.c new file +38
-0@@ -0,0 +1,122 @@ +// SPDX-License-Identifier: GPL-2.0-or-later OR BSD-3-Clause +/* +* Copyright (c) 2024, Max Resch +*/ + +#include <dm.h> +#include <malloc.h> +#include <rng.h> +#include <asm/dma-mapping.h> +#include <asm/types.h> +#include <mach/mbox.h> + +/* size of entropy buffer */ +#define RNG_BUFFER_SIZE 128U + +struct turris_rwtm_rng_priv { +phys_addr_t buffer; +}; + +static int turris_rwtm_rng_fill_entropy (phys_addr_t entropy,size_t size) +{ +u32 args[3 ] = {1 , (u32 )entropy, size }; +int ret; + +/* flush data cache */ +flush_dcache_range (entropy, entropy + size); + +/* +* get entropy +* args[0] = 1 copies BYTES array in args[1] of length args[2] +*/ + ret =mbox_do_cmd (MBOX_CMD_GET_RANDOM , args,3 ,NULL ,0 ); +if (ret <0 ) +return ret; + +/* invalidate data cache */ +invalidate_dcache_range (entropy, entropy + size); +drivers/rng/turris_rwtm_rng.c line 48
Quoted 10 lines
drivers/rng/turris_rwtm_rng.c new file +10
-0+return 0 ; +} + +static int turris_rwtm_rng_random_read (struct udevice *dev,void *data,size_t count) +{ +phys_addr_t p; +size_t size; +int ret; + + p = ((struct turris_rwtm_rng_priv *)dev_get_priv (dev))->buffer;Please declare
struct turris_rwtm_rng_priv *priv = dev_get_priv(dev); phys_addr_t phys; size_t size; int ret;and then do
phys = priv->buffer;Quoted 6 lines
drivers/rng/turris_rwtm_rng.c new file +6
-0+while (count) { + size =min_t (size_t,RNG_BUFFER_SIZE , count); + + ret =turris_rwtm_rng_fill_entropy (p, size); + +memcpy (data, (void *)p, size);drivers/rng/turris_rwtm_rng.c line 65
Quoted 10 lines
drivers/rng/turris_rwtm_rng.c new file +10
-0+ count -= size; + data = (u8 *)data + size; + } + +return 0 ; +} + +static int turris_rwtm_rng_probe (struct udevice *dev) +{ +struct turris_rwtm_rng_priv *priv;= dev_get_priv(dev);Quoted 4 lines
drivers/rng/turris_rwtm_rng.c new file +4
-0+u32 args[] = {0 }; +int ret; + +/* drivers/rng/turris_rwtm_rng.c line 79
Quoted 10 lines
drivers/rng/turris_rwtm_rng.c new file +10
-0+* check if the random command is supported +* args[0] = 0 would copy 16 DWORDS entropy to out but we ignore them +*/ + ret =mbox_do_cmd (MBOX_CMD_GET_RANDOM , args,ARRAY_SIZE (args),NULL ,0 ); + +if (ret <0 ) +return ret; + +/* entropy buffer */ + priv = (struct turris_rwtm_rng_priv *)dev_get_priv (dev);no need here, do it at the beginning
drivers/rng/turris_rwtm_rng.c line 92
Quoted 13 lines
drivers/rng/turris_rwtm_rng.c new file +13
-0+ priv->buffer =0 ; + +/* buffer address need to be aligned */ +dma_alloc_coherent (RNG_BUFFER_SIZE , (unsigned long *)&priv->buffer); +if (!priv->buffer) +return -ENOMEM ; + +return 0 ; +} + +static int turris_rwtm_rng_remove (struct udevice *dev) +{ +phys_addr_t p;rename this to phys, and please do it as I described above.
drivers/rng/turris_rwtm_rng.c line 97
Quoted 5 lines
drivers/rng/turris_rwtm_rng.c new file +5
-0+ + p = ((struct turris_rwtm_rng_priv *)dev_get_priv (dev))->buffer; +dma_free_coherent ((void *)p); + +return 0 ;Marek
So you're using A3720 with OpenBSD? Can I ask which A3720 boards do you use?
Marek
Quoted 189 lines · On Mon, 05 Feb 2024 14:46:24 +0100 Mark Kettenis <[email protected]>…
Quoted 7 lines
Date: Mon, 5 Feb 2024 12:40:14 +0100
From: Marek Behún <[email protected]>Hello Max,
Out of curiousity, what is your use case for having these random numbers on this platform in U-Boot?
It means that U-Boot will implement the EFI RNG protocol. The OpenBSD bootloader will use that to fill an initial pool of random numbers for the kernel. And the Linux kernel EFI stub uses it to do kernel address space randomization.
Cheers,
Mark
Quoted 172 lines
Below are a few more things to change and then you I'll give my Reviewed-by tag.
Quoted 85 lines · On Sun, 21 Jan 2024 21:17:16 +0100 Max Resch <[email protected]> wrote:
A RNG driver for Armada 3720 boards running the Turris Mox rWTM firmware from CZ.NIC in the secure processor.
Signed-off-by Max Resch <[email protected]>---
Changes in v2:
- Removed ring buffer implementationdrivers/rng/Kconfig | 8 +++ drivers/rng/Makefile | 1 + drivers/rng/turris_rwtm_rng.c | 122 ++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 drivers/rng/turris_rwtm_rng.cdrivers/rng/Kconfig +8
-0@@ -105,4 +105,12 @@ config RNG_JH7110 help Enable True Random Number Generator in StarFive JH7110 SoCs. +config RNG_TURRIS_RWTM + bool "Turris Mox TRNG in Secure Processor" + depends on DM_RNG && ARMADA_3700 + help + Use TRNG in Turris Mox Secure Processor Firmware. Can be used + on other Armada-3700 devices (like EspressoBin) if Secure + Firmware from CZ.NIC is used. + endifdrivers/rng/Makefile +1
-0@@ -17,3 +17,4 @@ obj-$(CONFIG_RNG_SMCCC_TRNG) += smccc_trng.o obj-$(CONFIG_RNG_ARM_RNDR) += arm_rndr.o obj-$(CONFIG_TPM_RNG) += tpm_rng.o obj-$(CONFIG_RNG_JH7110) += jh7110_rng.o +obj-$(CONFIG_RNG_TURRIS_RWTM) += turris_rwtm_rng.odrivers/rng/turris_rwtm_rng.c new file +38
-0@@ -0,0 +1,122 @@ +// SPDX-License-Identifier: GPL-2.0-or-later OR BSD-3-Clause +/* +* Copyright (c) 2024, Max Resch +*/ + +#include <dm.h> +#include <malloc.h> +#include <rng.h> +#include <asm/dma-mapping.h> +#include <asm/types.h> +#include <mach/mbox.h> + +/* size of entropy buffer */ +#define RNG_BUFFER_SIZE 128U + +struct turris_rwtm_rng_priv { +phys_addr_t buffer; +}; + +static int turris_rwtm_rng_fill_entropy (phys_addr_t entropy,size_t size) +{ +u32 args[3 ] = {1 , (u32 )entropy, size }; +int ret; + +/* flush data cache */ +flush_dcache_range (entropy, entropy + size); + +/* +* get entropy +* args[0] = 1 copies BYTES array in args[1] of length args[2] +*/ + ret =mbox_do_cmd (MBOX_CMD_GET_RANDOM , args,3 ,NULL ,0 ); +if (ret <0 ) +return ret; + +/* invalidate data cache */ +invalidate_dcache_range (entropy, entropy + size); +drivers/rng/turris_rwtm_rng.c line 48
Quoted 10 lines
drivers/rng/turris_rwtm_rng.c new file +10
-0+return 0 ; +} + +static int turris_rwtm_rng_random_read (struct udevice *dev,void *data,size_t count) +{ +phys_addr_t p; +size_t size; +int ret; + + p = ((struct turris_rwtm_rng_priv *)dev_get_priv (dev))->buffer;Please declare
struct turris_rwtm_rng_priv *priv = dev_get_priv(dev); phys_addr_t phys; size_t size; int ret;and then do
phys = priv->buffer;Quoted 6 lines
drivers/rng/turris_rwtm_rng.c new file +6
-0+while (count) { + size =min_t (size_t,RNG_BUFFER_SIZE , count); + + ret =turris_rwtm_rng_fill_entropy (p, size); + +memcpy (data, (void *)p, size);drivers/rng/turris_rwtm_rng.c line 65
Quoted 10 lines
drivers/rng/turris_rwtm_rng.c new file +10
-0+ count -= size; + data = (u8 *)data + size; + } + +return 0 ; +} + +static int turris_rwtm_rng_probe (struct udevice *dev) +{ +struct turris_rwtm_rng_priv *priv;= dev_get_priv(dev);Quoted 4 lines
drivers/rng/turris_rwtm_rng.c new file +4
-0+u32 args[] = {0 }; +int ret; + +/* drivers/rng/turris_rwtm_rng.c line 79
Quoted 10 lines
drivers/rng/turris_rwtm_rng.c new file +10
-0+* check if the random command is supported +* args[0] = 0 would copy 16 DWORDS entropy to out but we ignore them +*/ + ret =mbox_do_cmd (MBOX_CMD_GET_RANDOM , args,ARRAY_SIZE (args),NULL ,0 ); + +if (ret <0 ) +return ret; + +/* entropy buffer */ + priv = (struct turris_rwtm_rng_priv *)dev_get_priv (dev);no need here, do it at the beginning
drivers/rng/turris_rwtm_rng.c line 92
Quoted 13 lines
drivers/rng/turris_rwtm_rng.c new file +13
-0+ priv->buffer =0 ; + +/* buffer address need to be aligned */ +dma_alloc_coherent (RNG_BUFFER_SIZE , (unsigned long *)&priv->buffer); +if (!priv->buffer) +return -ENOMEM ; + +return 0 ; +} + +static int turris_rwtm_rng_remove (struct udevice *dev) +{ +phys_addr_t p;rename this to phys, and please do it as I described above.
drivers/rng/turris_rwtm_rng.c line 97
Quoted 5 lines
drivers/rng/turris_rwtm_rng.c new file +5
-0+ + p = ((struct turris_rwtm_rng_priv *)dev_get_priv (dev))->buffer; +dma_free_coherent ((void *)p); + +return 0 ;Marek
Quoted 5 lines
Date: Mon, 5 Feb 2024 15:54:12 +0100
From: Marek Behún <[email protected]>So you're using A3720 with OpenBSD? Can I ask which A3720 boards do you use?
Hi Marek,
I have a Turris Mox.
Some folks run OpenBSD on the ESPRESSObin and GL.iNet Brume as well (those are A3700 of course).
Cheers,
Mark
Quoted 194 lines
Quoted 189 lines · On Mon, 05 Feb 2024 14:46:24 +0100 Mark Kettenis <[email protected]>…
Quoted 7 lines
Date: Mon, 5 Feb 2024 12:40:14 +0100
From: Marek Behún <[email protected]>Hello Max,
Out of curiousity, what is your use case for having these random numbers on this platform in U-Boot?
It means that U-Boot will implement the EFI RNG protocol. The OpenBSD bootloader will use that to fill an initial pool of random numbers for the kernel. And the Linux kernel EFI stub uses it to do kernel address space randomization.
Cheers,
Mark
Quoted 172 lines
Below are a few more things to change and then you I'll give my Reviewed-by tag.
Quoted 85 lines · On Sun, 21 Jan 2024 21:17:16 +0100 Max Resch <[email protected]> wrote:
A RNG driver for Armada 3720 boards running the Turris Mox rWTM firmware from CZ.NIC in the secure processor.
Signed-off-by Max Resch <[email protected]>---
Changes in v2:
- Removed ring buffer implementationdrivers/rng/Kconfig | 8 +++ drivers/rng/Makefile | 1 + drivers/rng/turris_rwtm_rng.c | 122 ++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 drivers/rng/turris_rwtm_rng.cdrivers/rng/Kconfig +8
-0@@ -105,4 +105,12 @@ config RNG_JH7110 help Enable True Random Number Generator in StarFive JH7110 SoCs. +config RNG_TURRIS_RWTM + bool "Turris Mox TRNG in Secure Processor" + depends on DM_RNG && ARMADA_3700 + help + Use TRNG in Turris Mox Secure Processor Firmware. Can be used + on other Armada-3700 devices (like EspressoBin) if Secure + Firmware from CZ.NIC is used. + endifdrivers/rng/Makefile +1
-0@@ -17,3 +17,4 @@ obj-$(CONFIG_RNG_SMCCC_TRNG) += smccc_trng.o obj-$(CONFIG_RNG_ARM_RNDR) += arm_rndr.o obj-$(CONFIG_TPM_RNG) += tpm_rng.o obj-$(CONFIG_RNG_JH7110) += jh7110_rng.o +obj-$(CONFIG_RNG_TURRIS_RWTM) += turris_rwtm_rng.odrivers/rng/turris_rwtm_rng.c new file +38
-0@@ -0,0 +1,122 @@ +// SPDX-License-Identifier: GPL-2.0-or-later OR BSD-3-Clause +/* +* Copyright (c) 2024, Max Resch +*/ + +#include <dm.h> +#include <malloc.h> +#include <rng.h> +#include <asm/dma-mapping.h> +#include <asm/types.h> +#include <mach/mbox.h> + +/* size of entropy buffer */ +#define RNG_BUFFER_SIZE 128U + +struct turris_rwtm_rng_priv { +phys_addr_t buffer; +}; + +static int turris_rwtm_rng_fill_entropy (phys_addr_t entropy,size_t size) +{ +u32 args[3 ] = {1 , (u32 )entropy, size }; +int ret; + +/* flush data cache */ +flush_dcache_range (entropy, entropy + size); + +/* +* get entropy +* args[0] = 1 copies BYTES array in args[1] of length args[2] +*/ + ret =mbox_do_cmd (MBOX_CMD_GET_RANDOM , args,3 ,NULL ,0 ); +if (ret <0 ) +return ret; + +/* invalidate data cache */ +invalidate_dcache_range (entropy, entropy + size); +drivers/rng/turris_rwtm_rng.c line 48
Quoted 10 lines
drivers/rng/turris_rwtm_rng.c new file +10
-0+return 0 ; +} + +static int turris_rwtm_rng_random_read (struct udevice *dev,void *data,size_t count) +{ +phys_addr_t p; +size_t size; +int ret; + + p = ((struct turris_rwtm_rng_priv *)dev_get_priv (dev))->buffer;Please declare
struct turris_rwtm_rng_priv *priv = dev_get_priv(dev); phys_addr_t phys; size_t size; int ret;and then do
phys = priv->buffer;Quoted 6 lines
drivers/rng/turris_rwtm_rng.c new file +6
-0+while (count) { + size =min_t (size_t,RNG_BUFFER_SIZE , count); + + ret =turris_rwtm_rng_fill_entropy (p, size); + +memcpy (data, (void *)p, size);drivers/rng/turris_rwtm_rng.c line 65
Quoted 10 lines
drivers/rng/turris_rwtm_rng.c new file +10
-0+ count -= size; + data = (u8 *)data + size; + } + +return 0 ; +} + +static int turris_rwtm_rng_probe (struct udevice *dev) +{ +struct turris_rwtm_rng_priv *priv;= dev_get_priv(dev);Quoted 4 lines
drivers/rng/turris_rwtm_rng.c new file +4
-0+u32 args[] = {0 }; +int ret; + +/* drivers/rng/turris_rwtm_rng.c line 79
Quoted 10 lines
drivers/rng/turris_rwtm_rng.c new file +10
-0+* check if the random command is supported +* args[0] = 0 would copy 16 DWORDS entropy to out but we ignore them +*/ + ret =mbox_do_cmd (MBOX_CMD_GET_RANDOM , args,ARRAY_SIZE (args),NULL ,0 ); + +if (ret <0 ) +return ret; + +/* entropy buffer */ + priv = (struct turris_rwtm_rng_priv *)dev_get_priv (dev);no need here, do it at the beginning
drivers/rng/turris_rwtm_rng.c line 92
Quoted 13 lines
drivers/rng/turris_rwtm_rng.c new file +13
-0+ priv->buffer =0 ; + +/* buffer address need to be aligned */ +dma_alloc_coherent (RNG_BUFFER_SIZE , (unsigned long *)&priv->buffer); +if (!priv->buffer) +return -ENOMEM ; + +return 0 ; +} + +static int turris_rwtm_rng_remove (struct udevice *dev) +{ +phys_addr_t p;rename this to phys, and please do it as I described above.
drivers/rng/turris_rwtm_rng.c line 97
Quoted 5 lines
drivers/rng/turris_rwtm_rng.c new file +5
-0+ + p = ((struct turris_rwtm_rng_priv *)dev_get_priv (dev))->buffer; +dma_free_coherent ((void *)p); + +return 0 ;Marek
Hi there Marek,
Am 05/02/2024 um 12.40 schrieb Marek Behún:
Quoted 4 lines
Hello Max,
Out of curiousity, what is your use case for having these random numbers on this platform in U-Boot?
I use 2 EspressoBin (Armada 3720) as routers, both of them use the TurrisMox firmware. RNG enables the EFI RNG protocol which inturn allows systemd-boot to populate the Linux RNG EFI seed config table which enables ealy initialization of the kernel prng. The kernel also then allows the use of KASLR. When the kernel is booted up a module is available to interface with the Firmware to use the RNG directly.
Also, I'm not sure if this should be part of the patch, this could be enabled on some default configurations for the Armada 3700.
-- Max
Quoted 171 lines
Below are a few more things to change and then you I'll give my Reviewed-by tag.
Quoted 85 lines · On Sun, 21 Jan 2024 21:17:16 +0100 Max Resch <[email protected]> wrote:
A RNG driver for Armada 3720 boards running the Turris Mox rWTM firmware from CZ.NIC in the secure processor.
Signed-off-by Max Resch <[email protected]>---
Changes in v2:
- Removed ring buffer implementationdrivers/rng/Kconfig | 8 +++ drivers/rng/Makefile | 1 + drivers/rng/turris_rwtm_rng.c | 122 ++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+)create mode 100644
drivers/rng/turris_rwtm_rng.cdrivers/rng/Kconfig +8
-0@@ -105,4 +105,12 @@ config RNG_JH7110 help Enable True Random Number Generator in StarFive JH7110 SoCs. +config RNG_TURRIS_RWTM + bool "Turris Mox TRNG in Secure Processor" + depends on DM_RNG && ARMADA_3700 + help + Use TRNG in Turris Mox Secure Processor Firmware. Can be used + on other Armada-3700 devices (like EspressoBin) if Secure + Firmware from CZ.NIC is used. + endifdrivers/rng/Makefile +1
-0@@ -17,3 +17,4 @@ obj-$(CONFIG_RNG_SMCCC_TRNG) += smccc_trng.o obj-$(CONFIG_RNG_ARM_RNDR) += arm_rndr.o obj-$(CONFIG_TPM_RNG) += tpm_rng.o obj-$(CONFIG_RNG_JH7110) += jh7110_rng.o +obj-$(CONFIG_RNG_TURRIS_RWTM) += turris_rwtm_rng.odrivers/rng/turris_rwtm_rng.c new file +38
-0@@ -0,0 +1,122 @@ +// SPDX-License-Identifier: GPL-2.0-or-later OR BSD-3-Clause +/* +* Copyright (c) 2024, Max Resch +*/ + +#include <dm.h> +#include <malloc.h> +#include <rng.h> +#include <asm/dma-mapping.h> +#include <asm/types.h> +#include <mach/mbox.h> + +/* size of entropy buffer */ +#define RNG_BUFFER_SIZE 128U + +struct turris_rwtm_rng_priv { +phys_addr_t buffer; +}; + +static int turris_rwtm_rng_fill_entropy (phys_addr_t entropy,size_t size) +{ +u32 args[3 ] = {1 , (u32 )entropy, size }; +int ret; + +/* flush data cache */ +flush_dcache_range (entropy, entropy + size); + +/* +* get entropy +* args[0] = 1 copies BYTES array in args[1] of length args[2] +*/ + ret =mbox_do_cmd (MBOX_CMD_GET_RANDOM , args,3 ,NULL ,0 ); +if (ret <0 ) +return ret; + +/* invalidate data cache */ +invalidate_dcache_range (entropy, entropy + size); +drivers/rng/turris_rwtm_rng.c line 48
Quoted 10 lines
drivers/rng/turris_rwtm_rng.c new file +10
-0+return 0 ; +} + +static int turris_rwtm_rng_random_read (struct udevice *dev,void *data,size_t count) +{ +phys_addr_t p; +size_t size; +int ret; + + p = ((struct turris_rwtm_rng_priv *)dev_get_priv (dev))->buffer;Please declare
struct turris_rwtm_rng_priv *priv = dev_get_priv(dev); phys_addr_t phys; size_t size; int ret;and then do
phys = priv->buffer;Quoted 6 lines
drivers/rng/turris_rwtm_rng.c new file +6
-0+while (count) { + size =min_t (size_t,RNG_BUFFER_SIZE , count); + + ret =turris_rwtm_rng_fill_entropy (p, size); + +memcpy (data, (void *)p, size);drivers/rng/turris_rwtm_rng.c line 65
Quoted 10 lines
drivers/rng/turris_rwtm_rng.c new file +10
-0+ count -= size; + data = (u8 *)data + size; + } + +return 0 ; +} + +static int turris_rwtm_rng_probe (struct udevice *dev) +{ +struct turris_rwtm_rng_priv *priv;= dev_get_priv(dev);Quoted 4 lines
drivers/rng/turris_rwtm_rng.c new file +4
-0+u32 args[] = {0 }; +int ret; + +/* drivers/rng/turris_rwtm_rng.c line 79
Quoted 10 lines
drivers/rng/turris_rwtm_rng.c new file +10
-0+* check if the random command is supported +* args[0] = 0 would copy 16 DWORDS entropy to out but we ignore them +*/ + ret =mbox_do_cmd (MBOX_CMD_GET_RANDOM , args,ARRAY_SIZE (args),NULL ,0 ); + +if (ret <0 ) +return ret; + +/* entropy buffer */ + priv = (struct turris_rwtm_rng_priv *)dev_get_priv (dev);no need here, do it at the beginning
drivers/rng/turris_rwtm_rng.c line 92
Quoted 13 lines
drivers/rng/turris_rwtm_rng.c new file +13
-0+ priv->buffer =0 ; + +/* buffer address need to be aligned */ +dma_alloc_coherent (RNG_BUFFER_SIZE , (unsigned long *)&priv->buffer); +if (!priv->buffer) +return -ENOMEM ; + +return 0 ; +} + +static int turris_rwtm_rng_remove (struct udevice *dev) +{ +phys_addr_t p;rename this to phys, and please do it as I described above.
drivers/rng/turris_rwtm_rng.c line 97
Quoted 5 lines
drivers/rng/turris_rwtm_rng.c new file +5
-0+ + p = ((struct turris_rwtm_rng_priv *)dev_get_priv (dev))->buffer; +dma_free_coherent ((void *)p); + +return 0 ;Marek