Joe Hershberger | eafc8db | 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 | |
Matthias Brugger | ea707dc | 2020-12-18 10:28:04 +0100 | [diff] [blame] | 12 | #include <dm/uclass.h> |
| 13 | #include <rng.h> |
Joe Hershberger | eafc8db | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 14 | |
| 15 | /* |
Michael Walle | 99e139d | 2012-06-05 11:33:15 +0000 | [diff] [blame] | 16 | * Return a seed for the PRNG derived from the eth0 MAC address. |
Joe Hershberger | eafc8db | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 17 | */ |
Michael Walle | 99e139d | 2012-06-05 11:33:15 +0000 | [diff] [blame] | 18 | static inline unsigned int seed_mac(void) |
| 19 | { |
Jimmy Du | b044cc1 | 2017-06-06 11:58:54 -0500 | [diff] [blame] | 20 | unsigned char enetaddr[ARP_HLEN]; |
Michael Walle | 99e139d | 2012-06-05 11:33:15 +0000 | [diff] [blame] | 21 | unsigned int seed; |
| 22 | |
| 23 | /* get our mac */ |
Jimmy Du | b044cc1 | 2017-06-06 11:58:54 -0500 | [diff] [blame] | 24 | memcpy(enetaddr, eth_get_ethaddr(), ARP_HLEN); |
Michael Walle | 99e139d | 2012-06-05 11:33:15 +0000 | [diff] [blame] | 25 | |
| 26 | seed = enetaddr[5]; |
| 27 | seed ^= enetaddr[4] << 8; |
| 28 | seed ^= enetaddr[3] << 16; |
| 29 | seed ^= enetaddr[2] << 24; |
| 30 | seed ^= enetaddr[1]; |
| 31 | seed ^= enetaddr[0] << 8; |
| 32 | |
| 33 | return seed; |
| 34 | } |
Joe Hershberger | eafc8db | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
Michael Walle | 99e139d | 2012-06-05 11:33:15 +0000 | [diff] [blame] | 37 | * Seed the random number generator using the eth0 MAC address. |
Joe Hershberger | eafc8db | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 38 | */ |
Michael Walle | 99e139d | 2012-06-05 11:33:15 +0000 | [diff] [blame] | 39 | static inline void srand_mac(void) |
| 40 | { |
Matthias Brugger | ea707dc | 2020-12-18 10:28:04 +0100 | [diff] [blame] | 41 | int ret; |
| 42 | struct udevice *devp; |
| 43 | u32 randv = 0; |
| 44 | |
Marek Vasut | 591257b | 2024-04-26 01:02:07 +0200 | [diff] [blame] | 45 | if (CONFIG_IS_ENABLED(DM_RNG)) { |
Matthias Brugger | ea707dc | 2020-12-18 10:28:04 +0100 | [diff] [blame] | 46 | ret = uclass_get_device(UCLASS_RNG, 0, &devp); |
| 47 | if (ret) { |
| 48 | ret = dm_rng_read(devp, &randv, sizeof(randv)); |
| 49 | if (ret < 0) |
| 50 | randv = 0; |
| 51 | } |
| 52 | } |
| 53 | if (randv) |
| 54 | srand(randv); |
| 55 | else |
| 56 | srand(seed_mac()); |
Michael Walle | 99e139d | 2012-06-05 11:33:15 +0000 | [diff] [blame] | 57 | } |
Joe Hershberger | eafc8db | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 58 | |
| 59 | #endif /* __NET_RAND_H__ */ |