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 | |
| 32 | #if (CONFIG_COMMANDS & CFG_CMD_NET) |
| 33 | |
| 34 | #define TIMEOUT 5 /* Seconds before trying BOOTP again */ |
| 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; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 51 | #ifdef DEBUG |
| 52 | printf("Got good RARP\n"); |
| 53 | #endif |
wdenk | 0e6d798 | 2004-03-14 00:07:33 +0000 | [diff] [blame^] | 54 | if ((s = getenv("autoload")) != NULL) { |
| 55 | if (*s == 'n') { |
| 56 | /* |
| 57 | * Just use RARP to configure system; |
| 58 | * Do not use TFTP/NFS to to load the bootfile. |
| 59 | */ |
| 60 | NetState = NETLOOP_SUCCESS; |
| 61 | return; |
| 62 | #if (CONFIG_COMMANDS & CFG_CMD_NFS) |
| 63 | } else if ((s != NULL) && !strcmp(s, "NFS")) { |
| 64 | NfsStart(); |
| 65 | return; |
| 66 | #endif |
| 67 | } |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 68 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 69 | TftpStart (); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /* |
| 74 | * Timeout on BOOTP request. |
| 75 | */ |
| 76 | static void |
| 77 | RarpTimeout(void) |
| 78 | { |
| 79 | if (RarpTry >= TIMEOUT_COUNT) { |
| 80 | puts ("\nRetry count exceeded; starting again\n"); |
| 81 | NetStartAgain (); |
| 82 | } else { |
| 83 | NetSetTimeout (TIMEOUT * CFG_HZ, RarpTimeout); |
| 84 | RarpRequest (); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
| 89 | void |
| 90 | RarpRequest (void) |
| 91 | { |
| 92 | int i; |
| 93 | volatile uchar *pkt; |
| 94 | ARP_t * rarp; |
| 95 | |
| 96 | printf("RARP broadcast %d\n", ++RarpTry); |
| 97 | pkt = NetTxPacket; |
| 98 | |
| 99 | NetSetEther(pkt, NetBcastAddr, PROT_RARP); |
| 100 | pkt += ETHER_HDR_SIZE; |
| 101 | |
| 102 | rarp = (ARP_t *)pkt; |
| 103 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 104 | rarp->ar_hrd = htons (ARP_ETHER); |
| 105 | rarp->ar_pro = htons (PROT_IP); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 106 | rarp->ar_hln = 6; |
| 107 | rarp->ar_pln = 4; |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 108 | rarp->ar_op = htons (RARPOP_REQUEST); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 109 | memcpy (&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */ |
| 110 | memcpy (&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ |
| 111 | memcpy (&rarp->ar_data[10], NetOurEther, 6); /* dest ET addr = source ET addr ??*/ |
| 112 | /* dest. IP addr set to broadcast */ |
| 113 | for (i = 0; i <= 3; i++) { |
| 114 | rarp->ar_data[16 + i] = 0xff; |
| 115 | } |
| 116 | |
| 117 | NetSendPacket(NetTxPacket, ETHER_HDR_SIZE + ARP_HDR_SIZE); |
| 118 | |
| 119 | NetSetTimeout(TIMEOUT * CFG_HZ, RarpTimeout); |
| 120 | NetSetHandler(RarpHandler); |
| 121 | } |
| 122 | |
| 123 | #endif /* CFG_CMD_NET */ |