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