Re: getentropy() implementation on BBB

2018-03-12 Thread Gedare Bloom
Udit,

See if you can find a 2-clause BSD boilerplate to copy from in the
RTEMS already. The one you got seems to have some minor typesetting
problems with whitespace issues.

It is fine to include stdint.h in the am335x.h if needed, so you can
keep the TRNG definitions at that header, which makes the most sense
to me.

Gedare

On Sun, Mar 11, 2018 at 2:22 PM, Udit agarwal  wrote:
> Hi,
> I have updated the patch and tested getentropy() , it did seem to work
> (Logs: here).
> Moreover, i've added BSD-2-clause license please verify it once. Also, i
> need to shift
> TRNG register structure from AM335x.h to bbb_getentropy.c . since, keeping
> it in am335x.h
> requires including stdint.h (for uint32_t etc) which i don't think would be
> right, do let me know if this
> is the right way.
>
> Here's the patch:
> From f87b39708dd06482957fc7b33e78ab0ee095287b Mon Sep 17 00:00:00 2001
> From: Udit agarwal 
> Date: Sun, 11 Mar 2018 23:34:17 +0530
> Subject: [PATCH] Added getentropy support to BBB BSP
>
> ---
>  bsps/arm/include/libcpu/am335x.h   |  22 +++-
>  c/src/lib/libbsp/arm/beagle/Makefile.am|   4 +-
>  .../libbsp/arm/beagle/getentropy/bbb_getentropy.c  | 128
> +
>  3 files changed, 152 insertions(+), 2 deletions(-)
>  create mode 100644 c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
>
> diff --git a/bsps/arm/include/libcpu/am335x.h
> b/bsps/arm/include/libcpu/am335x.h
> index 367e97c..bbdc8d0 100644
> --- a/bsps/arm/include/libcpu/am335x.h
> +++ b/bsps/arm/include/libcpu/am335x.h
> @@ -14,6 +14,9 @@
>   * Modified by Ben Gras  to add lots
>   * of beagleboard/beaglebone definitions, delete lpc32xx specific
>   * ones, and merge with some other header files.
> + *
> + * Modified by Udit agarwal  to add random
> + * number generating module definitions.
>   */
>
>  #if !defined(_AM335X_H_)
> @@ -701,4 +704,21 @@
>  #define AM335X_CM_PER_OCPWP_L3_CLKSTCTRL_CLKACTIVITY_OCPWP_L4_GCLK
> (0x0020u)
>  #define AM335X_I2C_INT_STOP_CONDITION AM335X_I2C_IRQSTATUS_BF
>
> -#endif
> +/* TRNG Register */
> +
> +/* RNG base address */
> +#define RNG_BASE 0x4831
> +/* RNG clock control */
> +#define CM_PER_RNG_CLKCTRL (AM335X_CM_PER_ADDR | (9 << 4))
> +/* rng module clock status bits */
> +#define AM335X_CLK_RNG_BIT_MASK (0x3)
> +/* Offset from RNG base for output ready flag */
> +#define RNG_STATUS_RDY (1u <<  0)
> +/* Offset from RNG base for FRO related error */
> +#define RNG_STATUS_ERR (1u <<  1)
> +/* Offset from RNG base for clock status */
> +#define RNG_STATUS_CLK (1u << 31)
> +/* enable module */
> +#define AM335X_RNG_ENABLE (1 << 10)
> +
> +#endif
> \ No newline at end of file
> diff --git a/c/src/lib/libbsp/arm/beagle/Makefile.am
> b/c/src/lib/libbsp/arm/beagle/Makefile.am
> index 8251660..5d5ade3 100644
> --- a/c/src/lib/libbsp/arm/beagle/Makefile.am
> +++ b/c/src/lib/libbsp/arm/beagle/Makefile.am
> @@ -40,7 +40,6 @@ libbsp_a_LIBADD =
>
>  # Shared
>  libbsp_a_SOURCES += ../../shared/bootcard.c
> -libbsp_a_SOURCES += ../../shared/getentropy-cpucounter.c
>  libbsp_a_SOURCES += ../../shared/src/bsp-fdt.c
>  libbsp_a_SOURCES += ../../shared/bspclean.c
>  libbsp_a_SOURCES += ../../shared/bspgetworkarea.c
> @@ -88,6 +87,9 @@ libbsp_a_SOURCES += gpio/bbb-gpio.c
>  #pwm
>  libbsp_a_SOURCES += pwm/pwm.c
>
> +#getentropy
> +libbsp_a_SOURCES += getentropy/bbb_getentropy.c
> +
>  #RTC
>  libbsp_a_SOURCES += rtc.c
>  libbsp_a_SOURCES += ../../shared/tod.c
> diff --git a/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
> b/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
> new file mode 100644
> index 000..117c6b8
> --- /dev/null
> +++ b/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
> @@ -0,0 +1,128 @@
> +/**
> +* @file
> +*
> +* @ingroup arm_beagle
> +*
> +* @brief Getentropy implementation on BeagleBone Black BSP
> +*/
> +
> +/*
> +*Copyright 2018 Udit kumar agarwal 
> +*
> +*Redistribution and use in source and binary forms, with or without
> +*modification, are permitted provided that the following conditions are
> met:
> +*
> +*1. Redistributions of source code must retain the above copyright notice,
> +*   this list of conditions and the following disclaimer.
> +*
> +*2. Redistributions in binary form must reproduce the above copyright
> notice,
> +*   this list of conditions and the following disclaimer in the
> documentation
> +*   and/or other materials provided with the distribution.
> +*
> +*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
> IS"
> +*AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
> +*IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
> +*ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
> +*LIABLE FOR ANY DIRECT,INDIRECT, INCIDENTAL, SPECIAL,EXEMPLARY, OR
> +*CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 

Re: getentropy() implementation on BBB

2018-03-11 Thread Udit agarwal
Hi,
I have updated the patch and tested getentropy() , it did seem to work
(Logs: here
).
Moreover, i've added BSD-2-clause license please verify it once. Also, i
need to shift
TRNG register structure from AM335x.h to bbb_getentropy.c . since, keeping
it in am335x.h
requires including stdint.h (for uint32_t etc) which i don't think would be
right, do let me know if this
is the right way.

Here's the patch:
>From f87b39708dd06482957fc7b33e78ab0ee095287b Mon Sep 17 00:00:00 2001
From: Udit agarwal 
Date: Sun, 11 Mar 2018 23:34:17 +0530
Subject: [PATCH] Added getentropy support to BBB BSP

---
 bsps/arm/include/libcpu/am335x.h   |  22 +++-
 c/src/lib/libbsp/arm/beagle/Makefile.am|   4 +-
 .../libbsp/arm/beagle/getentropy/bbb_getentropy.c  | 128
+
 3 files changed, 152 insertions(+), 2 deletions(-)
 create mode 100644 c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c

diff --git a/bsps/arm/include/libcpu/am335x.h
b/bsps/arm/include/libcpu/am335x.h
index 367e97c..bbdc8d0 100644
--- a/bsps/arm/include/libcpu/am335x.h
+++ b/bsps/arm/include/libcpu/am335x.h
@@ -14,6 +14,9 @@
  * Modified by Ben Gras  to add lots
  * of beagleboard/beaglebone definitions, delete lpc32xx specific
  * ones, and merge with some other header files.
+ *
+ * Modified by Udit agarwal  to add random
+ * number generating module definitions.
  */

 #if !defined(_AM335X_H_)
@@ -701,4 +704,21 @@
 #define AM335X_CM_PER_OCPWP_L3_CLKSTCTRL_CLKACTIVITY_OCPWP_L4_GCLK
(0x0020u)
 #define AM335X_I2C_INT_STOP_CONDITION AM335X_I2C_IRQSTATUS_BF

-#endif
+/* TRNG Register */
+
+/* RNG base address */
+#define RNG_BASE 0x4831
+/* RNG clock control */
+#define CM_PER_RNG_CLKCTRL (AM335X_CM_PER_ADDR | (9 << 4))
+/* rng module clock status bits */
+#define AM335X_CLK_RNG_BIT_MASK (0x3)
+/* Offset from RNG base for output ready flag */
+#define RNG_STATUS_RDY (1u <<  0)
+/* Offset from RNG base for FRO related error */
+#define RNG_STATUS_ERR (1u <<  1)
+/* Offset from RNG base for clock status */
+#define RNG_STATUS_CLK (1u << 31)
+/* enable module */
+#define AM335X_RNG_ENABLE (1 << 10)
+
+#endif
\ No newline at end of file
diff --git a/c/src/lib/libbsp/arm/beagle/Makefile.am
b/c/src/lib/libbsp/arm/beagle/Makefile.am
index 8251660..5d5ade3 100644
--- a/c/src/lib/libbsp/arm/beagle/Makefile.am
+++ b/c/src/lib/libbsp/arm/beagle/Makefile.am
@@ -40,7 +40,6 @@ libbsp_a_LIBADD =

 # Shared
 libbsp_a_SOURCES += ../../shared/bootcard.c
-libbsp_a_SOURCES += ../../shared/getentropy-cpucounter.c
 libbsp_a_SOURCES += ../../shared/src/bsp-fdt.c
 libbsp_a_SOURCES += ../../shared/bspclean.c
 libbsp_a_SOURCES += ../../shared/bspgetworkarea.c
@@ -88,6 +87,9 @@ libbsp_a_SOURCES += gpio/bbb-gpio.c
 #pwm
 libbsp_a_SOURCES += pwm/pwm.c

+#getentropy
+libbsp_a_SOURCES += getentropy/bbb_getentropy.c
+
 #RTC
 libbsp_a_SOURCES += rtc.c
 libbsp_a_SOURCES += ../../shared/tod.c
diff --git a/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
b/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
new file mode 100644
index 000..117c6b8
--- /dev/null
+++ b/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
@@ -0,0 +1,128 @@
+/**
+* @file
+*
+* @ingroup arm_beagle
+*
+* @brief Getentropy implementation on BeagleBone Black BSP
+*/
+
+/*
+*Copyright 2018 Udit kumar agarwal 
+*
+*Redistribution and use in source and binary forms, with or without
+*modification, are permitted provided that the following conditions are
met:
+*
+*1. Redistributions of source code must retain the above copyright notice,
+*   this list of conditions and the following disclaimer.
+*
+*2. Redistributions in binary form must reproduce the above copyright
notice,
+*   this list of conditions and the following disclaimer in the
documentation
+*   and/or other materials provided with the distribution.
+*
+*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS"
+*AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+*IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+*ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+*LIABLE FOR ANY DIRECT,INDIRECT, INCIDENTAL, SPECIAL,EXEMPLARY, OR
+*CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+*SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,OR PROFITS; OR BUSINESS
+*INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,WHETHER IN
CONTRACT,
+*STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY
+*OF SUCH DAMAGE.
+*/
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* max refill 34 * 256 cycles */
+#define AM335X_RNG_MAX_REFILL (34 << 16)
+/* min refill 33 * 64 cycles */
+#define AM335X_RNG_MIN_REFILL (33 << 0)
+/* startup 33 * 256 cycles */

Re: getentropy() implementation on BBB

2018-03-07 Thread Christian Mauderer
Hello Udit,

let me start with a hint: It's quite normal that the first patches of a
contributor get a lot of attention and have a lot of revisions. That can
be quite annoying for the one who writes the patch. But please
understand it in the way it's thought: As suggestions for improvement.

Did you already test that patches? There is a (quite basic) test in the
RTEMS testsuite: libtests/getentropy01. This test should run without
problems. If you have a debugger, you might also want to check whether
your getentropy implementation is reached and not the generic one.

The patch looks quite good already. But like I said: A lot of revisions.
I added some further remarks in the patch below ;-)

Best regards

Christian


Am 07.03.2018 um 11:26 schrieb Udit agarwal:
> Hi,
> I have updated the code, please have a look.
> From 74b8f4f5b9dd929b71ed5fb9dd0bc721a6f27a28 Mon Sep 17 00:00:00 2001
> From: Udit agarwal >
> Date: Wed, 7 Mar 2018 15:52:13 +0530
> Subject: [PATCH] Added getentropy support to beagle BSP
> 
> ---
>  bsps/arm/include/libcpu/am335x.h   | 34 -
>  c/src/lib/libbsp/arm/beagle/Makefile.am    |  4 +
>  .../libbsp/arm/beagle/getentropy/bbb_getentropy.c  | 88
> ++
>  3 files changed, 125 insertions(+), 1 deletion(-)
>  create mode 100644 c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
> 
> diff --git a/bsps/arm/include/libcpu/am335x.h
> b/bsps/arm/include/libcpu/am335x.h
> index 367e97c..92af6dc 100644
> --- a/bsps/arm/include/libcpu/am335x.h
> +++ b/bsps/arm/include/libcpu/am335x.h
> @@ -14,6 +14,9 @@
>   * Modified by Ben Gras  > to add lots
>   * of beagleboard/beaglebone definitions, delete lpc32xx specific
>   * ones, and merge with some other header files.
> + *
> + * Modified by Udit agarwal  > to add random
> + * number generating module definitions along with register structure.
>   */
>  
>  #if !defined(_AM335X_H_)
> @@ -701,4 +704,33 @@
>  #define AM335X_CM_PER_OCPWP_L3_CLKSTCTRL_CLKACTIVITY_OCPWP_L4_GCLK
> (0x0020u)
>  #define AM335X_I2C_INT_STOP_CONDITION AM335X_I2C_IRQSTATUS_BF
>  
> -#endif
> +/* TRNG Register */
> +
> +/* RNG base address */
> +#define RNG_BASE 0x4831
> +/* RNG clock control */
> +#define CM_PER_RNG_CLKCTRL (AM335X_CM_PER_ADDR | (9 << 4))
> +/* rng module clock status bits */
> +#define AM335X_CLK_RNG_BIT_MASK (0x3)
> +/* Offset from RNG base for output ready flag */
> +#define RNG_STATUS_RDY (1u <<  0) 
> +/* Offset from RNG base for FRO related error */
> +#define RNG_STATUS_ERR (1u <<  1)
> +/* Offset from RNG base for clock status */
> +#define RNG_STATUS_CLK (1u << 31) 
> +/* enable module */
> +#define AM335X_RNG_ENABLE (1 << 10)
> +
> +/* TRNG register structure */
> +struct bbb_trng_register
> +{
> +    uint64_t output; /*00*/   
> +    uint32_t status; /*08*/   
> +    uint32_t irq_en; /*0c*/   
> +    uint32_t status_clr; /*10*/   
> +    uint32_t control; /*14*/   
> +    uint32_t config; /*18*/   
> +};
> +typedef struct bbb_trng_register bbb_trng_register;
> +
> +#endif
> \ No newline at end of file
> diff --git a/c/src/lib/libbsp/arm/beagle/Makefile.am
> b/c/src/lib/libbsp/arm/beagle/Makefile.am
> index 8251660..bf92081 100644
> --- a/c/src/lib/libbsp/arm/beagle/Makefile.am
> +++ b/c/src/lib/libbsp/arm/beagle/Makefile.am
> @@ -88,9 +88,13 @@ libbsp_a_SOURCES += gpio/bbb-gpio.c
>  #pwm
>  libbsp_a_SOURCES += pwm/pwm.c
>  
> +#getentropy
> +libbsp_a_SOURCES += getentropy/bbb_getentropy.c
> +

Adding that one is good. But please also remove the default
implementation from the Makefile.am:

libbsp_a_SOURCES += ../../shared/getentropy-cpucounter.c

Otherwise you might get problems during linking.

>  #RTC
>  libbsp_a_SOURCES += rtc.c
>  libbsp_a_SOURCES += ../../shared/tod.c
> +

You added a empty line in a section where you didn't change anything
else. Normally that's not a good idea because it slightly obfuscates the
history. Try to generate patches that only touch the area that is
related to your change. If you really think that this line should be
added, you should create a second patch with a description like "fix
spacing" or similar. But I wouldn't suggest to do that.

>  # Clock
>  libbsp_a_SOURCES += clock.c
>  libbsp_a_SOURCES += ../../shared/clockdrv_shell.h
> diff --git a/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
> b/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
> new file mode 100644
> index 000..5b7c5d0
> --- /dev/null
> +++ b/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
> @@ -0,0 +1,88 @@
> +/**
> +* @file
> +*
> +* @ingroup arm_beagle
> +*
> +* @brief Getentropy implementation on BeagleBone Black BSP
> +*/
> +
> +/*
> +* Copyright (c) 2018 Udit kumar agarwal  >
> +*
> +* The license and distribution terms for this file may be
> +* found in the file LICENSE 

Re: getentropy() implementation on BBB

2018-03-07 Thread Udit agarwal
Thanks, I just came across ticket #3053
 , which talks about changing RTEMS
license from GNU GPL to BSD-2-Clause (i was initially unaware of this!).
I'll make the required changes.

Regards,
Udit agarwal

On Wed, Mar 7, 2018 at 4:23 PM, Sebastian Huber <
sebastian.hu...@embedded-brains.de> wrote:

> On 07/03/18 11:26, Udit agarwal wrote:
>
>> > +/*
>> > + * Copyright (c) 2018 Udit kumar agarwal > http://gmail.com>>
>> > + *
>> > + * The license and distribution terms for this file may be
>> > + * found in the file LICENSE in this distribution or at
>> > + * http://www.rtems.org/license/LICENSE > LICENSE>.
>> > + */
>>
>
> What about new contributions from new authors? Do we want to use a
> BSD-2-Clause license for RTEMS or not?
>
> --
> Sebastian Huber, embedded brains GmbH
>
> Address : Dornierstr. 4, D-82178 Puchheim, Germany
> Phone   : +49 89 189 47 41-16
> Fax : +49 89 189 47 41-09
> E-Mail  : sebastian.hu...@embedded-brains.de
> PGP : Public key available on request.
>
> Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
>
>
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: getentropy() implementation on BBB

2018-03-07 Thread Udit agarwal
Hi,
I have updated the code, please have a look.
>From 74b8f4f5b9dd929b71ed5fb9dd0bc721a6f27a28 Mon Sep 17 00:00:00 2001
From: Udit agarwal 
Date: Wed, 7 Mar 2018 15:52:13 +0530
Subject: [PATCH] Added getentropy support to beagle BSP

---
 bsps/arm/include/libcpu/am335x.h   | 34 -
 c/src/lib/libbsp/arm/beagle/Makefile.am|  4 +
 .../libbsp/arm/beagle/getentropy/bbb_getentropy.c  | 88
++
 3 files changed, 125 insertions(+), 1 deletion(-)
 create mode 100644 c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c

diff --git a/bsps/arm/include/libcpu/am335x.h
b/bsps/arm/include/libcpu/am335x.h
index 367e97c..92af6dc 100644
--- a/bsps/arm/include/libcpu/am335x.h
+++ b/bsps/arm/include/libcpu/am335x.h
@@ -14,6 +14,9 @@
  * Modified by Ben Gras  to add lots
  * of beagleboard/beaglebone definitions, delete lpc32xx specific
  * ones, and merge with some other header files.
+ *
+ * Modified by Udit agarwal  to add random
+ * number generating module definitions along with register structure.
  */

 #if !defined(_AM335X_H_)
@@ -701,4 +704,33 @@
 #define AM335X_CM_PER_OCPWP_L3_CLKSTCTRL_CLKACTIVITY_OCPWP_L4_GCLK
(0x0020u)
 #define AM335X_I2C_INT_STOP_CONDITION AM335X_I2C_IRQSTATUS_BF

-#endif
+/* TRNG Register */
+
+/* RNG base address */
+#define RNG_BASE 0x4831
+/* RNG clock control */
+#define CM_PER_RNG_CLKCTRL (AM335X_CM_PER_ADDR | (9 << 4))
+/* rng module clock status bits */
+#define AM335X_CLK_RNG_BIT_MASK (0x3)
+/* Offset from RNG base for output ready flag */
+#define RNG_STATUS_RDY (1u <<  0)
+/* Offset from RNG base for FRO related error */
+#define RNG_STATUS_ERR (1u <<  1)
+/* Offset from RNG base for clock status */
+#define RNG_STATUS_CLK (1u << 31)
+/* enable module */
+#define AM335X_RNG_ENABLE (1 << 10)
+
+/* TRNG register structure */
+struct bbb_trng_register
+{
+uint64_t output; /*00*/
+uint32_t status; /*08*/
+uint32_t irq_en; /*0c*/
+uint32_t status_clr; /*10*/
+uint32_t control; /*14*/
+uint32_t config; /*18*/
+};
+typedef struct bbb_trng_register bbb_trng_register;
+
+#endif
\ No newline at end of file
diff --git a/c/src/lib/libbsp/arm/beagle/Makefile.am
b/c/src/lib/libbsp/arm/beagle/Makefile.am
index 8251660..bf92081 100644
--- a/c/src/lib/libbsp/arm/beagle/Makefile.am
+++ b/c/src/lib/libbsp/arm/beagle/Makefile.am
@@ -88,9 +88,13 @@ libbsp_a_SOURCES += gpio/bbb-gpio.c
 #pwm
 libbsp_a_SOURCES += pwm/pwm.c

+#getentropy
+libbsp_a_SOURCES += getentropy/bbb_getentropy.c
+
 #RTC
 libbsp_a_SOURCES += rtc.c
 libbsp_a_SOURCES += ../../shared/tod.c
+
 # Clock
 libbsp_a_SOURCES += clock.c
 libbsp_a_SOURCES += ../../shared/clockdrv_shell.h
diff --git a/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
b/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
new file mode 100644
index 000..5b7c5d0
--- /dev/null
+++ b/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
@@ -0,0 +1,88 @@
+/**
+* @file
+*
+* @ingroup arm_beagle
+*
+* @brief Getentropy implementation on BeagleBone Black BSP
+*/
+
+/*
+* Copyright (c) 2018 Udit kumar agarwal 
+*
+* The license and distribution terms for this file may be
+* found in the file LICENSE in this distribution or at
+* http://www.rtems.org/license/LICENSE.
+*/
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* max refill 34 * 256 cycles */
+#define AM335X_RNG_MAX_REFILL (34 << 16)
+/* min refill 33 * 64 cycles */
+#define AM335X_RNG_MIN_REFILL (33 << 0)
+/* startup 33 * 256 cycles */
+#define AM335X_RNG_STARTUP_CYCLES (33 << 16)
+
+/* maximun and minimum refill cycle sets the number of samples to be taken
+   from FRO to generate random number */
+static void am335x_rng_enable(bbb_trng_register *rng)
+{
+rng->control = rng->config = 0;
+rng->config |= AM335X_RNG_MIN_REFILL | AM335X_RNG_MAX_REFILL ;
+rng->control |= AM335X_RNG_STARTUP_CYCLES | AM335X_RNG_ENABLE ;
+}
+
+static void am335x_rng_clock_enable(void)
+{
+*(volatile uint8_t *) CM_PER_RNG_CLKCTRL = 2;
+while( *(volatile uint32_t *) CM_PER_RNG_CLKCTRL &
AM335X_CLK_RNG_BIT_MASK ) {}
+}
+
+static uint64_t trng_getranddata(bbb_trng_register *rng)
+{
+uint64_t output = rng->output;
+return output;
+}
+
+int getentropy(void *ptr, size_t n)
+{
+volatile const bbb_trng_register  *rng = (bbb_trng_register*) RNG_BASE;
+am335x_rng_enable(rng);
+while (n > 0)
+{
+uint64_t random;
+size_t copy;
+
+/* wait untill RNG becomes ready with next set of random data */
+while( ( rng->status & RNG_STATUS_RDY ) == 0 );
+
+random = trng_getranddata(rng);
+
+/* Checking for error by masking all bits other then error bit in
status register */
+if( ((rng->status & RNG_STATUS_ERR)>>1) == 0)
+{
+/* clear the status flag after reading to generate new random
value*/
+rng->status_clr = RNG_STATUS_RDY;
+copy = 

Re: getentropy() implementation on BBB

2018-03-06 Thread Gedare Bloom
On Tue, Mar 6, 2018 at 9:43 AM, Udit agarwal  wrote:
> Hi,
> Here's the updated code(I have also attached the patch PFA):
>
> From 96e6e1bfd8cffeef5d309eb0a07fe2bfd086ef0a Mon Sep 17 00:00:00 2001
> From: Udit agarwal 
> Date: Tue, 6 Mar 2018 20:07:44 +0530
> Subject: [PATCH] Added getentropy support to BBB BSP
>
> ---
>  bsps/arm/beagle/headers.am |  1 +
>  bsps/arm/beagle/include/bsp/bbb_getentropy.h   | 57 +++
>  bsps/arm/include/libcpu/am335x.h   | 16 +
>  c/src/lib/libbsp/arm/beagle/Makefile.am|  4 ++
>  .../libbsp/arm/beagle/getentropy/bbb_getentropy.c  | 80
> ++
>  5 files changed, 158 insertions(+)
>  create mode 100644 bsps/arm/beagle/include/bsp/bbb_getentropy.h
>  create mode 100644 c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c
>
> diff --git a/bsps/arm/beagle/headers.am b/bsps/arm/beagle/headers.am
> index 6692d0b..ac4ff7c 100644
> --- a/bsps/arm/beagle/headers.am
> +++ b/bsps/arm/beagle/headers.am
> @@ -12,3 +12,4 @@ include_bsp_HEADERS +=
> ../../../../../../bsps/arm/beagle/include/bsp/bbb-pwm.h
>  include_bsp_HEADERS +=
> ../../../../../../bsps/arm/beagle/include/bsp/beagleboneblack.h
>  include_bsp_HEADERS += ../../../../../../bsps/arm/beagle/include/bsp/i2c.h
>  include_bsp_HEADERS += ../../../../../../bsps/arm/beagle/include/bsp/irq.h
> +include_bsp_HEADERS +=
> ../../../../../../bsps/arm/beagle/include/bsp/bbb_getentropy.h
> diff --git a/bsps/arm/beagle/include/bsp/bbb_getentropy.h
> b/bsps/arm/beagle/include/bsp/bbb_getentropy.h
> new file mode 100644
> index 000..5e46f89
> --- /dev/null
> +++ b/bsps/arm/beagle/include/bsp/bbb_getentropy.h
> @@ -0,0 +1,57 @@
> +/**
> + * @file
> + *
> + * @ingroup arm_beagle
> + *
> + * @brief TRNG Definations
> + */
> +
> +/*
> + * Copyright (c) 2018 Udit kumar agarwal 
> + *
> + * The license and distribution terms for this file may be
> + * found in the file LICENSE in this distribution or at
> + * http://www.rtems.org/license/LICENSE.
> + */
> +
> +
> +#ifndef _TRNG_
> +#define _TRNG_
> +
> +/*--
Please look at similar files in RTEMS for how to add comments.
We do not use this style /* 
And we don't put this kind of comment about the file sections.

> + * Headers
> +
> **/
> +
> +#include 
> +
> +
> /*--
> + * Register structure
> +
> **/
> +
> +struct rng {
> +uint64_t output; /*00*/
> +uint32_t status; /*08*/
> +uint32_t irq_en; /*0c*/
> +uint32_t status_clr; /*10*/
> +uint32_t control; /*14*/
> +uint32_t config; /*18*/
> +};
for an exported header, the struct namespace needs to be user
friendly. See similar files at this layer for how to name things.
Probably, this can be defined in the am335x.h header?

> +
> +typedef volatile struct rng *rng_ref_t;
I'm not such a big fan of using the typedef to hide things like
volatile or pointer notation. I don't see a need for this usually.

> +
> +/**/
> +/* Exported functions
> */
> +/**/
> +
> +/* configure and enable RNG module  */
> +static void am335x_rng_enable(rng_ref_t);
> +/* enable module clock for random number generator */
> +static void am335x_rng_clock_enable(void);
> +/* outputs random data */
> +static uint64_t trng_getranddata(rng_ref_t);

static keyword implies these are not exported.

> +/* Copy random data of a specified size to the pointer supplied */
> +int getentropy(void *ptr, size_t);
I guess getentropy() is already exported by a different header file
(unistd.h) in libc? This doesn't belong here.

I suspect this header file is not actually necessary.

> +
> +
> +
Don't use multiple blank lines in a row. Only one empty line is ever
needed by RTEMS style.

> +#endif /* #ifndef _TRNG_ */
> \ No newline at end of file
> diff --git a/bsps/arm/include/libcpu/am335x.h
> b/bsps/arm/include/libcpu/am335x.h
> index 367e97c..8b58f1a 100644
> --- a/bsps/arm/include/libcpu/am335x.h
> +++ b/bsps/arm/include/libcpu/am335x.h
> @@ -14,6 +14,9 @@
>   * Modified by Ben Gras  to add lots
>   * of beagleboard/beaglebone definitions, delete lpc32xx specific
>   * ones, and merge with some other header files.
> + *
> + * Modified by Udit agarwal  to add random
> + * number generating module definations
typo: definitions
add a period (full stop).

>   */
>
>  #if !defined(_AM335X_H_)
> @@ -701,4 +704,17 @@
>  #define AM335X_CM_PER_OCPWP_L3_CLKSTCTRL_CLKACTIVITY_OCPWP_L4_GCLK
> (0x0020u)
>  

Re: getentropy() implementation on BBB

2018-03-06 Thread Udit agarwal
Hi,
Here's the updated code(I have also attached the patch PFA):

>From 96e6e1bfd8cffeef5d309eb0a07fe2bfd086ef0a Mon Sep 17 00:00:00 2001
From: Udit agarwal 
Date: Tue, 6 Mar 2018 20:07:44 +0530
Subject: [PATCH] Added getentropy support to BBB BSP

---
 bsps/arm/beagle/headers.am |  1 +
 bsps/arm/beagle/include/bsp/bbb_getentropy.h   | 57 +++
 bsps/arm/include/libcpu/am335x.h   | 16 +
 c/src/lib/libbsp/arm/beagle/Makefile.am|  4 ++
 .../libbsp/arm/beagle/getentropy/bbb_getentropy.c  | 80
++
 5 files changed, 158 insertions(+)
 create mode 100644 bsps/arm/beagle/include/bsp/bbb_getentropy.h
 create mode 100644 c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c

diff --git a/bsps/arm/beagle/headers.am b/bsps/arm/beagle/headers.am
index 6692d0b..ac4ff7c 100644
--- a/bsps/arm/beagle/headers.am
+++ b/bsps/arm/beagle/headers.am
@@ -12,3 +12,4 @@ include_bsp_HEADERS +=
../../../../../../bsps/arm/beagle/include/bsp/bbb-pwm.h
 include_bsp_HEADERS +=
../../../../../../bsps/arm/beagle/include/bsp/beagleboneblack.h
 include_bsp_HEADERS += ../../../../../../bsps/arm/beagle/include/bsp/i2c.h
 include_bsp_HEADERS += ../../../../../../bsps/arm/beagle/include/bsp/irq.h
+include_bsp_HEADERS +=
../../../../../../bsps/arm/beagle/include/bsp/bbb_getentropy.h
diff --git a/bsps/arm/beagle/include/bsp/bbb_getentropy.h
b/bsps/arm/beagle/include/bsp/bbb_getentropy.h
new file mode 100644
index 000..5e46f89
--- /dev/null
+++ b/bsps/arm/beagle/include/bsp/bbb_getentropy.h
@@ -0,0 +1,57 @@
+/**
+ * @file
+ *
+ * @ingroup arm_beagle
+ *
+ * @brief TRNG Definations
+ */
+
+/*
+ * Copyright (c) 2018 Udit kumar agarwal 
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.org/license/LICENSE.
+ */
+
+
+#ifndef _TRNG_
+#define _TRNG_
+
+/*--
+ * Headers
+
**/
+
+#include 
+
+
/*--
+ * Register structure
+
**/
+
+struct rng {
+uint64_t output; /*00*/
+uint32_t status; /*08*/
+uint32_t irq_en; /*0c*/
+uint32_t status_clr; /*10*/
+uint32_t control; /*14*/
+uint32_t config; /*18*/
+};
+
+typedef volatile struct rng *rng_ref_t;
+
+/**/
+/* Exported
functions */
+/**/
+
+/* configure and enable RNG module  */
+static void am335x_rng_enable(rng_ref_t);
+/* enable module clock for random number generator */
+static void am335x_rng_clock_enable(void);
+/* outputs random data */
+static uint64_t trng_getranddata(rng_ref_t);
+/* Copy random data of a specified size to the pointer supplied */
+int getentropy(void *ptr, size_t);
+
+
+
+#endif /* #ifndef _TRNG_ */
\ No newline at end of file
diff --git a/bsps/arm/include/libcpu/am335x.h
b/bsps/arm/include/libcpu/am335x.h
index 367e97c..8b58f1a 100644
--- a/bsps/arm/include/libcpu/am335x.h
+++ b/bsps/arm/include/libcpu/am335x.h
@@ -14,6 +14,9 @@
  * Modified by Ben Gras  to add lots
  * of beagleboard/beaglebone definitions, delete lpc32xx specific
  * ones, and merge with some other header files.
+ *
+ * Modified by Udit agarwal  to add random
+ * number generating module definations
  */

 #if !defined(_AM335X_H_)
@@ -701,4 +704,17 @@
 #define AM335X_CM_PER_OCPWP_L3_CLKSTCTRL_CLKACTIVITY_OCPWP_L4_GCLK
(0x0020u)
 #define AM335X_I2C_INT_STOP_CONDITION AM335X_I2C_IRQSTATUS_BF

+/* TRNG Register */
+
+/* RNG base address */
+#define RNG_BASE0x4831
+/* RNG clock control */
+#define CM_PER_RNG_CLKCTRLAM335X_CM_PER_ADDR | 9<<4
+/* Offset from RNG base for output ready flag */
+#define RNG_STATUS_RDY(1u <<  0)
+/* Offset from RNG base for FRO related error*/
+#define RNG_STATUS_ERR(1u <<  1)
+/* Offset from RNG base for clock status */
+#define RNG_STATUS_CLK(1u << 31)
+
 #endif
diff --git a/c/src/lib/libbsp/arm/beagle/Makefile.am
b/c/src/lib/libbsp/arm/beagle/Makefile.am
index 8251660..bf92081 100644
--- a/c/src/lib/libbsp/arm/beagle/Makefile.am
+++ b/c/src/lib/libbsp/arm/beagle/Makefile.am
@@ -88,9 +88,13 @@ libbsp_a_SOURCES += gpio/bbb-gpio.c
 #pwm
 libbsp_a_SOURCES += pwm/pwm.c

+#getentropy
+libbsp_a_SOURCES += getentropy/bbb_getentropy.c
+
 #RTC
 libbsp_a_SOURCES += rtc.c
 libbsp_a_SOURCES += ../../shared/tod.c
+
 # Clock
 libbsp_a_SOURCES += clock.c
 libbsp_a_SOURCES += ../../shared/clockdrv_shell.h
diff --git a/c/src/lib/libbsp/arm/beagle/getentropy/bbb_getentropy.c

Re: getentropy() implementation on BBB

2018-03-05 Thread Christian Mauderer
Am 05.03.2018 um 14:51 schrieb Udit agarwal:
> Hi,
> I tried implementing getentropy on BBB, below is the patch. Please have
> a look.
> I followed these(1
>  & 2
> )
> links for code reference. and this
> 
> for register reference. Moreover, what further configuration in RTEMS is
> needed to execute and test this code along with getentropy() sample
> testcode?

Hello Udit,

regarding the patch: Please use 'git format-patch' for generating your
patches and send them unchanged to the mailing list (for example with
'git send-email'. If you follow this workflow, it's easier to apply and
test the patches. The preferred workflow for RTEMS is described here:
https://devel.rtems.org/wiki/Developer/Git/Users#CreatingaPatch

Regarding documentation: It seems that there is really only few
documentation regarding the crypto part available. The FreeBSD driver is
most likely your best source.

I added further comments down in your code.

Best regards

Christian

> 
> 
> + /*
> +  * Copyright (c) 2018 embedded brains GmbH.  All rights reserved.
> +  *
> +  *  embedded brains GmbH
> +  *  Dornierstr. 4
> +  *  82178 Puchheim
> +  *  Germany
> +  *  >
> +  *

Also it's nice if you add an embedded brains header, I don't think that
I've seen you when I have been at work today. So please add your own
name to the copyright line instead. With that you can even point to that
file and tell everyone that it's from you.

An address isn't really necessary except if you want it for some reason
(like PR in case of a company).

> +  * The license and distribution terms for this file may be
> +  * found in the file LICENSE in this distribution or at
> +  * http://www.rtems.org/license/LICENSE.
> +  */
> +
> + #include 
> + #include 
> + #include 
> + #include 
> +
> + #define RNG_BASE    0x4831
> + #define CM_PER_RNG_CLKCTRL    0x44e00090
> + #define RNG_STATUS_RDY    (1u <<  0)  // output ready for reading
> + #define RNG_STATUS_ERR    (1u <<  1)  // FRO shutdown alarm
> + #define RNG_STATUS_CLK    (1u << 31)  // module functional clock
> active (no irq)

Please add address definitions and the bit masks to the
bsps/arm/include/libcpu/am335x.h instead of defining them here locally.
Note that for the CM_PER_RNG_CLKCTRL, there is already a
AM335X_CM_PER_ADDR that should be used as a base.

> +
> + typedef volatile struct rng *rng_ref_t;
> +
> + struct rng {
> + /*00*/    uint64_t output;    //r-
> + /*08*/    uint32_t status;    //r-
> + /*0c*/    uint32_t irq_en;    //rw
> + /*10*/    uint32_t status_clr;    //-c
> + /*14*/    uint32_t control;    //rw
> + /*18*/    uint32_t config;    //rw
> + };

Same is true here: It's a structure describing the registers. So it
should be in the header instead.

It seems that you copied that from the forum post. Please always check
the license when you copy code. We have to avoid that we get any code
into RTEMS that isn't compatible with RTEMS license.

In this case the code should be obvious enough that - if you just
rewrite it in RTEMS style, there shouldn't be a problem. But just
remember that licensing is always a difficult topic.

> +
> + static void TRNG_Enable(rng_ref_t rng){
> +     rng->config = 0
> +     | 34 << 16  // max refill 34 * 256 cycles
> +     | 33 <<  0;  // min refill 33 * 64 cycles
> +     rng->control = 0
> +     | 33 << 16  // startup 33 * 256 cycles
> +     |  1 << 10;  // enable module
> + }

Please use C-Style comments /* */ in all pure C code (most of RTEMS).

> +
> + static void beagle_trng_clock_enable(void)
> + {
> +     // enable module clock
> +     *(volatile uint8_t *) CM_PER_RNG_CLKCTRL = 2;
> +     while( *(volatile uint32_t *) CM_PER_RNG_CLKCTRL & 0x3 ) {}
> + }
> +
> + static uint64_t TRNG_GetRandData(){
> +     uint64_t output = rng->output;

'rng' isn't defined here.

> +     return output;
> + }
> +
> + int getentropy(void *ptr, size_t n)
> + {
> +     rng_ref_t const rng = (rng_ref_t) RNG_BASE;
> +     TRNG_Enable(rng);
> +     while (n > 0) {
> +         uint64_t random;
> +         size_t copy;
> +
> +         while( ( rng->status & RNG_STATUS_RDY ) == 0 ); //wait

You should check specifically for the TRNG_STATUS_READY flag here like
done in FreeBSD. I'm not sure what the other flags are but it's quite
possible that there is also some error flag or similar. In that case it
would not be a good idea to use the random number.

> +
> +         random = TRNG_GetRandData(rng);

Note that you don't have to copy the function names from the atsam
exactly. In the atsam BSP, the library has provided these names.
Otherwise it's quite unusual to start functions with upper case in RTEMS.

> +
> +         /*
> +          * Read TRNG status one more time to avoid race 

getentropy() implementation on BBB

2018-03-05 Thread Udit agarwal
Hi,
I tried implementing getentropy on BBB, below is the patch. Please have a
look.
I followed these(1
 & 2
)
links for code reference. and this

for register reference. Moreover, what further configuration in RTEMS is
needed to execute and test this code along with getentropy() sample
testcode?


+ /*
+  * Copyright (c) 2018 embedded brains GmbH.  All rights reserved.
+  *
+  *  embedded brains GmbH
+  *  Dornierstr. 4
+  *  82178 Puchheim
+  *  Germany
+  *  
+  *
+  * The license and distribution terms for this file may be
+  * found in the file LICENSE in this distribution or at
+  * http://www.rtems.org/license/LICENSE.
+  */
+
+ #include 
+ #include 
+ #include 
+ #include 
+
+ #define RNG_BASE0x4831
+ #define CM_PER_RNG_CLKCTRL0x44e00090
+ #define RNG_STATUS_RDY(1u <<  0)  // output ready for reading
+ #define RNG_STATUS_ERR(1u <<  1)  // FRO shutdown alarm
+ #define RNG_STATUS_CLK(1u << 31)  // module functional clock active
(no irq)
+
+ typedef volatile struct rng *rng_ref_t;
+
+ struct rng {
+ /*00*/uint64_t output;//r-
+ /*08*/uint32_t status;//r-
+ /*0c*/uint32_t irq_en;//rw
+ /*10*/uint32_t status_clr;//-c
+ /*14*/uint32_t control;//rw
+ /*18*/uint32_t config;//rw
+ };
+
+ static void TRNG_Enable(rng_ref_t rng){
+ rng->config = 0
+ | 34 << 16  // max refill 34 * 256 cycles
+ | 33 <<  0;  // min refill 33 * 64 cycles
+ rng->control = 0
+ | 33 << 16  // startup 33 * 256 cycles
+ |  1 << 10;  // enable module
+ }
+
+ static void beagle_trng_clock_enable(void)
+ {
+ // enable module clock
+ *(volatile uint8_t *) CM_PER_RNG_CLKCTRL = 2;
+ while( *(volatile uint32_t *) CM_PER_RNG_CLKCTRL & 0x3 ) {}
+ }
+
+ static uint64_t TRNG_GetRandData(){
+ uint64_t output = rng->output;
+ return output;
+ }
+
+ int getentropy(void *ptr, size_t n)
+ {
+ rng_ref_t const rng = (rng_ref_t) RNG_BASE;
+ TRNG_Enable(rng);
+ while (n > 0) {
+ uint64_t random;
+ size_t copy;
+
+ while( ( rng->status & RNG_STATUS_RDY ) == 0 ); //wait
+
+ random = TRNG_GetRandData(rng);
+
+ /*
+  * Read TRNG status one more time to avoid race condition.
+  * Otherwise we can read (and clear) an old ready status but get
+  * a new value. The ready status for this value wouldn't be
+  * reset.
+  */
+ rng->status_clr = RNG_STATUS_RDY;
+
+ copy = sizeof(random);
+ if (n < copy ) {
+ copy = n;
+ }
+ memcpy(ptr, , copy);
+ n -= copy;
+ ptr += copy;
+ }
+
+ return 0;
+ }
+
+ RTEMS_SYSINIT_ITEM(
+   beagle_trng_clock_enable,
+   RTEMS_SYSINIT_DEVICE_DRIVERS,
+   RTEMS_SYSINIT_ORDER_LAST
+ );

Regards,
Udit agarwal
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel