Joe Hershberger | 797f2c5 | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copied from LiMon - BOOTP. |
| 3 | * |
| 4 | * Copyright 1994, 1995, 2000 Neil Russell. |
| 5 | * (See License) |
| 6 | * Copyright 2000 Paolo Scaffardi |
| 7 | */ |
| 8 | |
| 9 | #ifndef __NET_RAND_H__ |
| 10 | #define __NET_RAND_H__ |
| 11 | |
Michael Walle | b90b97a | 2012-06-05 11:33:15 +0000 | [diff] [blame] | 12 | #include <common.h> |
Matthias Brugger | 3b41ed6 | 2020-12-18 10:28:04 +0100 | [diff] [blame] | 13 | #include <dm/uclass.h> |
| 14 | #include <rng.h> |
Joe Hershberger | 797f2c5 | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 15 | |
| 16 | /* |
Michael Walle | b90b97a | 2012-06-05 11:33:15 +0000 | [diff] [blame] | 17 | * Return a seed for the PRNG derived from the eth0 MAC address. |
Joe Hershberger | 797f2c5 | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 18 | */ |
Michael Walle | b90b97a | 2012-06-05 11:33:15 +0000 | [diff] [blame] | 19 | static inline unsigned int seed_mac(void) |
| 20 | { |
Jimmy Du | d5017ca | 2017-06-06 11:58:54 -0500 | [diff] [blame] | 21 | unsigned char enetaddr[ARP_HLEN]; |
Michael Walle | b90b97a | 2012-06-05 11:33:15 +0000 | [diff] [blame] | 22 | unsigned int seed; |
| 23 | |
| 24 | /* get our mac */ |
Jimmy Du | d5017ca | 2017-06-06 11:58:54 -0500 | [diff] [blame] | 25 | memcpy(enetaddr, eth_get_ethaddr(), ARP_HLEN); |
Michael Walle | b90b97a | 2012-06-05 11:33:15 +0000 | [diff] [blame] | 26 | |
| 27 | seed = enetaddr[5]; |
| 28 | seed ^= enetaddr[4] << 8; |
| 29 | seed ^= enetaddr[3] << 16; |
| 30 | seed ^= enetaddr[2] << 24; |
| 31 | seed ^= enetaddr[1]; |
| 32 | seed ^= enetaddr[0] << 8; |
| 33 | |
| 34 | return seed; |
| 35 | } |
Joe Hershberger | 797f2c5 | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 36 | |
| 37 | /* |
Michael Walle | b90b97a | 2012-06-05 11:33:15 +0000 | [diff] [blame] | 38 | * Seed the random number generator using the eth0 MAC address. |
Joe Hershberger | 797f2c5 | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 39 | */ |
Michael Walle | b90b97a | 2012-06-05 11:33:15 +0000 | [diff] [blame] | 40 | static inline void srand_mac(void) |
| 41 | { |
Matthias Brugger | 3b41ed6 | 2020-12-18 10:28:04 +0100 | [diff] [blame] | 42 | int ret; |
| 43 | struct udevice *devp; |
| 44 | u32 randv = 0; |
| 45 | |
| 46 | if (IS_ENABLED(CONFIG_DM_RNG)) { |
| 47 | ret = uclass_get_device(UCLASS_RNG, 0, &devp); |
| 48 | if (ret) { |
| 49 | ret = dm_rng_read(devp, &randv, sizeof(randv)); |
| 50 | if (ret < 0) |
| 51 | randv = 0; |
| 52 | } |
| 53 | } |
| 54 | if (randv) |
| 55 | srand(randv); |
| 56 | else |
| 57 | srand(seed_mac()); |
Michael Walle | b90b97a | 2012-06-05 11:33:15 +0000 | [diff] [blame] | 58 | } |
Joe Hershberger | 797f2c5 | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 59 | |
| 60 | #endif /* __NET_RAND_H__ */ |