Joe Hershberger | eafc8db | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Based on LiMon - BOOTP. |
| 3 | * |
| 4 | * Copyright 1994, 1995, 2000 Neil Russell. |
| 5 | * (See License) |
| 6 | * Copyright 2000 Roland Borde |
| 7 | * Copyright 2000 Paolo Scaffardi |
| 8 | * Copyright 2000-2004 Wolfgang Denk, wd@denx.de |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <net.h> |
| 13 | #include "net_rand.h" |
| 14 | |
| 15 | static ulong seed1, seed2; |
| 16 | |
| 17 | void srand_mac(void) |
| 18 | { |
| 19 | ulong tst1, tst2, m_mask; |
| 20 | ulong m_value = 0; |
| 21 | int reg; |
| 22 | unsigned char bi_enetaddr[6]; |
| 23 | |
| 24 | /* get our mac */ |
| 25 | eth_getenv_enetaddr("ethaddr", bi_enetaddr); |
| 26 | |
| 27 | debug("BootpRequest => Our Mac: "); |
| 28 | for (reg = 0; reg < 6; reg++) |
| 29 | debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':'); |
| 30 | |
| 31 | /* Mac-Manipulation 2 get seed1 */ |
| 32 | tst1 = 0; |
| 33 | tst2 = 0; |
| 34 | for (reg = 2; reg < 6; reg++) { |
| 35 | tst1 = tst1 << 8; |
| 36 | tst1 = tst1 | bi_enetaddr[reg]; |
| 37 | } |
| 38 | for (reg = 0; reg < 2; reg++) { |
| 39 | tst2 = tst2 | bi_enetaddr[reg]; |
| 40 | tst2 = tst2 << 8; |
| 41 | } |
| 42 | |
| 43 | seed1 = tst1^tst2; |
| 44 | |
| 45 | /* Mirror seed1*/ |
| 46 | m_mask = 0x1; |
| 47 | for (reg = 1; reg <= 32; reg++) { |
| 48 | m_value |= (m_mask & seed1); |
| 49 | seed1 = seed1 >> 1; |
| 50 | m_value = m_value << 1; |
| 51 | } |
| 52 | seed1 = m_value; |
| 53 | seed2 = 0xb78d0945; |
| 54 | } |
| 55 | |
| 56 | unsigned long rand(void) |
| 57 | { |
| 58 | ulong sum; |
| 59 | |
| 60 | /* Random Number Generator */ |
| 61 | sum = seed1 + seed2; |
| 62 | if (sum < seed1 || sum < seed2) |
| 63 | sum++; |
| 64 | seed2 = seed1; |
| 65 | seed1 = sum; |
| 66 | |
| 67 | return sum; |
| 68 | } |