]> git.sur5r.net Git - u-boot/blob - common/cmd_net.c
47f9622fad5e36254ddf31b1c49173b4cc0ecf69
[u-boot] / common / cmd_net.c
1 /*
2  * (C) Copyright 2000
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 /*
25  * Boot support
26  */
27 #include <common.h>
28 #include <command.h>
29 #include <net.h>
30
31 #if (CONFIG_COMMANDS & CFG_CMD_NET)
32
33
34 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
35
36 static int netboot_common (int, cmd_tbl_t *, int , char *[]);
37
38 int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
39 {
40         return netboot_common (BOOTP, cmdtp, argc, argv);
41 }
42
43 U_BOOT_CMD(
44         bootp,  3,      1,      do_bootp,
45         "bootp\t- boot image via network using BootP/TFTP protocol\n",
46         "[loadAddress] [bootfilename]\n"
47 );
48
49 int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
50 {
51         return netboot_common (TFTP, cmdtp, argc, argv);
52 }
53
54 U_BOOT_CMD(
55         tftpboot,       3,      1,      do_tftpb,
56         "tftpboot- boot image via network using TFTP protocol\n",
57         "[loadAddress] [bootfilename]\n"
58 );
59
60 int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
61 {
62         return netboot_common (RARP, cmdtp, argc, argv);
63 }
64
65 U_BOOT_CMD(
66         rarpboot,       3,      1,      do_rarpb,
67         "rarpboot- boot image via network using RARP/TFTP protocol\n",
68         "[loadAddress] [bootfilename]\n"
69 );
70
71 #if (CONFIG_COMMANDS & CFG_CMD_DHCP)
72 int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
73 {
74         return netboot_common(DHCP, cmdtp, argc, argv);
75 }
76
77 U_BOOT_CMD(
78         dhcp,   3,      1,      do_dhcp,
79         "dhcp\t- invoke DHCP client to obtain IP/boot params\n",
80         "\n"
81 );
82 #endif  /* CFG_CMD_DHCP */
83
84 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
85 int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
86 {
87         return netboot_common(NFS, cmdtp, argc, argv);
88 }
89
90 U_BOOT_CMD(
91         nfs,    3,      1,      do_nfs,
92         "nfs\t- boot image via network using NFS protocol\n",
93         "[loadAddress] [host ip addr:bootfilename]\n"
94 );
95 #endif  /* CFG_CMD_NFS */
96
97 static void netboot_update_env (void)
98 {
99         char tmp[22];
100
101         if (NetOurGatewayIP) {
102                 ip_to_string (NetOurGatewayIP, tmp);
103                 setenv ("gatewayip", tmp);
104         }
105
106         if (NetOurSubnetMask) {
107                 ip_to_string (NetOurSubnetMask, tmp);
108                 setenv ("netmask", tmp);
109         }
110
111         if (NetOurHostName[0])
112                 setenv ("hostname", NetOurHostName);
113
114         if (NetOurRootPath[0])
115                 setenv ("rootpath", NetOurRootPath);
116
117         if (NetOurIP) {
118                 ip_to_string (NetOurIP, tmp);
119                 setenv ("ipaddr", tmp);
120         }
121
122         if (NetServerIP) {
123                 ip_to_string (NetServerIP, tmp);
124                 setenv ("serverip", tmp);
125         }
126
127         if (NetOurDNSIP) {
128                 ip_to_string (NetOurDNSIP, tmp);
129                 setenv ("dnsip", tmp);
130         }
131 #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
132         if (NetOurDNS2IP) {
133                 ip_to_string (NetOurDNS2IP, tmp);
134                 setenv ("dnsip2", tmp);
135         }
136 #endif
137         if (NetOurNISDomain[0])
138                 setenv ("domain", NetOurNISDomain);
139 }
140
141 static int
142 netboot_common (int proto, cmd_tbl_t *cmdtp, int argc, char *argv[])
143 {
144         char *s;
145         int   rcode = 0;
146         int   size;
147
148         /* pre-set load_addr */
149         if ((s = getenv("loadaddr")) != NULL) {
150                 load_addr = simple_strtoul(s, NULL, 16);
151         }
152
153         switch (argc) {
154         case 1:
155                 break;
156
157         case 2: /* only one arg - accept two forms:
158                  * just load address, or just boot file name.
159                  * The latter form must be written "filename" here.
160                  */
161                 if (argv[1][0] == '"') {        /* just boot filename */
162                         copy_filename (BootFile, argv[1], sizeof(BootFile));
163                 } else {                        /* load address */
164                         load_addr = simple_strtoul(argv[1], NULL, 16);
165                 }
166                 break;
167
168         case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
169                 copy_filename (BootFile, argv[2], sizeof(BootFile));
170
171                 break;
172
173         default: printf ("Usage:\n%s\n", cmdtp->usage);
174                 return 1;
175         }
176
177         if ((size = NetLoop(proto)) < 0)
178                 return 1;
179
180         /* NetLoop ok, update environment */
181         netboot_update_env();
182
183         /* done if no file was loaded (no errors though) */
184         if (size == 0)
185                 return 0;
186
187         /* flush cache */
188         flush_cache(load_addr, size);
189
190         /* Loading ok, check if we should attempt an auto-start */
191         if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == 0)) {
192                 char *local_args[2];
193                 local_args[0] = argv[0];
194                 local_args[1] = NULL;
195
196                 printf ("Automatic boot of image at addr 0x%08lX ...\n",
197                         load_addr);
198                 rcode = do_bootm (cmdtp, 0, 1, local_args);
199         }
200
201 #ifdef CONFIG_AUTOSCRIPT
202         if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
203                 printf("Running autoscript at addr 0x%08lX ...\n", load_addr);
204                 rcode = autoscript (load_addr);
205         }
206 #endif
207         return rcode;
208 }
209
210 #if (CONFIG_COMMANDS & CFG_CMD_PING)
211 int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
212 {
213         if (argc < 2)
214                 return -1;
215
216         NetPingIP = string_to_ip(argv[1]);
217         if (NetPingIP == 0) {
218                 printf ("Usage:\n%s\n", cmdtp->usage);
219                 return -1;
220         }
221
222         if (NetLoop(PING) < 0) {
223                 printf("ping failed; host %s is not alive\n", argv[1]);
224                 return 1;
225         }
226
227         printf("host %s is alive\n", argv[1]);
228
229         return 0;
230 }
231
232 U_BOOT_CMD(
233         ping,   2,      1,      do_ping,
234         "ping\t- send ICMP ECHO_REQUEST to network host\n",
235         "pingAddress\n"
236 );
237 #endif  /* CFG_CMD_PING */
238
239 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
240
241 static void cdp_update_env(void)
242 {
243         char tmp[16];
244
245         if (CDPApplianceVLAN != htons(-1)) {
246                 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
247                 VLAN_to_string(CDPApplianceVLAN, tmp);
248                 setenv("vlan", tmp);
249                 NetOurVLAN = CDPApplianceVLAN;
250         }
251
252         if (CDPNativeVLAN != htons(-1)) {
253                 printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
254                 VLAN_to_string(CDPNativeVLAN, tmp);
255                 setenv("nvlan", tmp);
256                 NetOurNativeVLAN = CDPNativeVLAN;
257         }
258
259 }
260
261 int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
262 {
263         int r;
264
265         r = NetLoop(CDP);
266         if (r < 0) {
267                 printf("cdp failed; perhaps not a CISCO switch?\n");
268                 return 1;
269         }
270
271         cdp_update_env();
272
273         return 0;
274 }
275
276 U_BOOT_CMD(
277         cdp,    1,      1,      do_cdp,
278         "cdp\t- Perform CDP network configuration\n",
279 );
280 #endif  /* CFG_CMD_CDP */
281
282 #endif  /* CFG_CMD_NET */