[U-Boot] [PATCH] net: make net_random_ethaddr() more random

Michael Walle michael at walle.cc
Thu Aug 22 22:08:01 UTC 2019


The net_random_ethaddr() tries to get some entropy from different
startup times of a board. The seed is initialized with get_timer() which
has only a granularity of milliseconds. We can do better if we use
get_ticks() which returns the raw timer ticks. Using this we have a
higher chance of getting different values at startup.

Signed-off-by: Michael Walle <michael at walle.cc>
---
 include/net.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/net.h b/include/net.h
index a54d5eeac5..8215316bd3 100644
--- a/include/net.h
+++ b/include/net.h
@@ -816,7 +816,7 @@ static inline int is_valid_ethaddr(const u8 *addr)
 static inline void net_random_ethaddr(uchar *addr)
 {
 	int i;
-	unsigned int seed = get_timer(0);
+	unsigned int seed = get_ticks() & (unsigned int)-1;
 
 	for (i = 0; i < 6; i++)
 		addr[i] = rand_r(&seed);
-- 
2.20.1



More information about the U-Boot mailing list