blob: 686e85f2b53704192724b0a99260bf7300c2b5d7 [file] [log] [blame]
Joe Hershberger797f2c52012-05-23 07:57:58 +00001/*
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 Brugger3b41ed62020-12-18 10:28:04 +010012#include <dm/uclass.h>
13#include <rng.h>
Joe Hershberger797f2c52012-05-23 07:57:58 +000014
15/*
Michael Walleb90b97a2012-06-05 11:33:15 +000016 * Return a seed for the PRNG derived from the eth0 MAC address.
Joe Hershberger797f2c52012-05-23 07:57:58 +000017 */
Michael Walleb90b97a2012-06-05 11:33:15 +000018static inline unsigned int seed_mac(void)
19{
Jimmy Dud5017ca2017-06-06 11:58:54 -050020 unsigned char enetaddr[ARP_HLEN];
Michael Walleb90b97a2012-06-05 11:33:15 +000021 unsigned int seed;
22
23 /* get our mac */
Jimmy Dud5017ca2017-06-06 11:58:54 -050024 memcpy(enetaddr, eth_get_ethaddr(), ARP_HLEN);
Michael Walleb90b97a2012-06-05 11:33:15 +000025
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 Hershberger797f2c52012-05-23 07:57:58 +000035
36/*
Michael Walleb90b97a2012-06-05 11:33:15 +000037 * Seed the random number generator using the eth0 MAC address.
Joe Hershberger797f2c52012-05-23 07:57:58 +000038 */
Michael Walleb90b97a2012-06-05 11:33:15 +000039static inline void srand_mac(void)
40{
Matthias Brugger3b41ed62020-12-18 10:28:04 +010041 int ret;
42 struct udevice *devp;
43 u32 randv = 0;
44
Marek Vasut0d871e72024-04-26 01:02:07 +020045 if (CONFIG_IS_ENABLED(DM_RNG)) {
Matthias Brugger3b41ed62020-12-18 10:28:04 +010046 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 Walleb90b97a2012-06-05 11:33:15 +000057}
Joe Hershberger797f2c52012-05-23 07:57:58 +000058
59#endif /* __NET_RAND_H__ */