[PATCH v2] arm/atsam: protect TRNG_GetRandData with mutex

dev.madaari at gmail.com dev.madaari at gmail.com
Fri Mar 16 22:27:59 UTC 2018


From: Udit agarwal <dev.madaari at gmail.com>

---
 c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
index 11e24dc..d3f1cf5 100644
--- a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
+++ b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
@@ -16,6 +16,10 @@
 #include <unistd.h>
 #include <string.h>
 #include <rtems/sysinit.h>
+#include <rtems/thread.h>
+
+static rtems_mutex atsam_trng_mutex =
+    RTEMS_MUTEX_INITIALIZER("atsam_trng");
 
 static void atsam_trng_enable(void)
 {
@@ -25,6 +29,8 @@ static void atsam_trng_enable(void)
 
 int getentropy(void *ptr, size_t n)
 {
+	rtems_mutex_lock(&atsam_trng_mutex);
+
 	while (n > 0) {
 		uint32_t random;
 		size_t copy;
@@ -51,6 +57,7 @@ int getentropy(void *ptr, size_t n)
 		ptr += copy;
 	}
 
+	rtems_mutex_unlock(&atsam_trng_mutex);
 	return 0;
 }
 
-- 
1.9.1



More information about the devel mailing list