wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <command.h> |
| 26 | #include <net.h> |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 27 | #include "nfs.h" |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 28 | #include "bootp.h" |
| 29 | #include "rarp.h" |
| 30 | #include "tftp.h" |
| 31 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 32 | #if defined(CONFIG_CMD_NET) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 33 | |
Bartlomiej Sieka | 49f3bdb | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 34 | #define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 35 | #ifndef CONFIG_NET_RETRY_COUNT |
| 36 | # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ |
| 37 | #else |
| 38 | # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) |
| 39 | #endif |
| 40 | |
| 41 | |
| 42 | int RarpTry; |
| 43 | |
| 44 | /* |
| 45 | * Handle a RARP received packet. |
| 46 | */ |
| 47 | static void |
| 48 | RarpHandler(uchar * dummi0, unsigned dummi1, unsigned dummi2, unsigned dummi3) |
| 49 | { |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 50 | char *s; |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 51 | debug("Got good RARP\n"); |
wdenk | 0e6d798 | 2004-03-14 00:07:33 +0000 | [diff] [blame] | 52 | if ((s = getenv("autoload")) != NULL) { |
| 53 | if (*s == 'n') { |
| 54 | /* |
| 55 | * Just use RARP to configure system; |
| 56 | * Do not use TFTP/NFS to to load the bootfile. |
| 57 | */ |
| 58 | NetState = NETLOOP_SUCCESS; |
| 59 | return; |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 60 | #if defined(CONFIG_CMD_NFS) |
wdenk | 0e6d798 | 2004-03-14 00:07:33 +0000 | [diff] [blame] | 61 | } else if ((s != NULL) && !strcmp(s, "NFS")) { |
| 62 | NfsStart(); |
| 63 | return; |
| 64 | #endif |
| 65 | } |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 66 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 67 | TftpStart (); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /* |
| 72 | * Timeout on BOOTP request. |
| 73 | */ |
| 74 | static void |
| 75 | RarpTimeout(void) |
| 76 | { |
| 77 | if (RarpTry >= TIMEOUT_COUNT) { |
| 78 | puts ("\nRetry count exceeded; starting again\n"); |
| 79 | NetStartAgain (); |
| 80 | } else { |
Bartlomiej Sieka | 49f3bdb | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 81 | NetSetTimeout (TIMEOUT, RarpTimeout); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 82 | RarpRequest (); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
| 87 | void |
| 88 | RarpRequest (void) |
| 89 | { |
| 90 | int i; |
| 91 | volatile uchar *pkt; |
| 92 | ARP_t * rarp; |
| 93 | |
| 94 | printf("RARP broadcast %d\n", ++RarpTry); |
| 95 | pkt = NetTxPacket; |
| 96 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 97 | pkt += NetSetEther(pkt, NetBcastAddr, PROT_RARP); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 98 | |
| 99 | rarp = (ARP_t *)pkt; |
| 100 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 101 | rarp->ar_hrd = htons (ARP_ETHER); |
| 102 | rarp->ar_pro = htons (PROT_IP); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 103 | rarp->ar_hln = 6; |
| 104 | rarp->ar_pln = 4; |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 105 | rarp->ar_op = htons (RARPOP_REQUEST); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 106 | memcpy (&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */ |
| 107 | memcpy (&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ |
| 108 | memcpy (&rarp->ar_data[10], NetOurEther, 6); /* dest ET addr = source ET addr ??*/ |
| 109 | /* dest. IP addr set to broadcast */ |
| 110 | for (i = 0; i <= 3; i++) { |
| 111 | rarp->ar_data[16 + i] = 0xff; |
| 112 | } |
| 113 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 114 | NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 115 | |
Bartlomiej Sieka | 49f3bdb | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 116 | NetSetTimeout(TIMEOUT, RarpTimeout); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 117 | NetSetHandler(RarpHandler); |
| 118 | } |
| 119 | |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 120 | #endif |