Merge branch 'testing' into working
[platform/kernel/u-boot.git] / 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 defined(CONFIG_CMD_NET)
32
33 #ifdef CONFIG_SHOW_BOOT_PROGRESS
34 # include <status_led.h>
35 extern void show_boot_progress (int val);
36 # define SHOW_BOOT_PROGRESS(arg)        show_boot_progress (arg)
37 #else
38 # define SHOW_BOOT_PROGRESS(arg)
39 #endif
40
41 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
42
43 static int netboot_common (proto_t, cmd_tbl_t *, int , char *[]);
44
45 int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
46 {
47         return netboot_common (BOOTP, cmdtp, argc, argv);
48 }
49
50 U_BOOT_CMD(
51         bootp,  3,      1,      do_bootp,
52         "bootp\t- boot image via network using BootP/TFTP protocol\n",
53         "[loadAddress] [bootfilename]\n"
54 );
55
56 int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
57 {
58         return netboot_common (TFTP, cmdtp, argc, argv);
59 }
60
61 U_BOOT_CMD(
62         tftpboot,       3,      1,      do_tftpb,
63         "tftpboot- boot image via network using TFTP protocol\n",
64         "[loadAddress] [bootfilename]\n"
65 );
66
67 int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
68 {
69         return netboot_common (RARP, cmdtp, argc, argv);
70 }
71
72 U_BOOT_CMD(
73         rarpboot,       3,      1,      do_rarpb,
74         "rarpboot- boot image via network using RARP/TFTP protocol\n",
75         "[loadAddress] [bootfilename]\n"
76 );
77
78 #if defined(CONFIG_CMD_DHCP)
79 int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
80 {
81         return netboot_common(DHCP, cmdtp, argc, argv);
82 }
83
84 U_BOOT_CMD(
85         dhcp,   3,      1,      do_dhcp,
86         "dhcp\t- invoke DHCP client to obtain IP/boot params\n",
87         "\n"
88 );
89 #endif
90
91 #if defined(CONFIG_CMD_NFS)
92 int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
93 {
94         return netboot_common(NFS, cmdtp, argc, argv);
95 }
96
97 U_BOOT_CMD(
98         nfs,    3,      1,      do_nfs,
99         "nfs\t- boot image via network using NFS protocol\n",
100         "[loadAddress] [host ip addr:bootfilename]\n"
101 );
102 #endif
103
104 static void netboot_update_env (void)
105 {
106         char tmp[22];
107
108         if (NetOurGatewayIP) {
109                 ip_to_string (NetOurGatewayIP, tmp);
110                 setenv ("gatewayip", tmp);
111         }
112
113         if (NetOurSubnetMask) {
114                 ip_to_string (NetOurSubnetMask, tmp);
115                 setenv ("netmask", tmp);
116         }
117
118         if (NetOurHostName[0])
119                 setenv ("hostname", NetOurHostName);
120
121         if (NetOurRootPath[0])
122                 setenv ("rootpath", NetOurRootPath);
123
124         if (NetOurIP) {
125                 ip_to_string (NetOurIP, tmp);
126                 setenv ("ipaddr", tmp);
127         }
128
129         if (NetServerIP) {
130                 ip_to_string (NetServerIP, tmp);
131                 setenv ("serverip", tmp);
132         }
133
134         if (NetOurDNSIP) {
135                 ip_to_string (NetOurDNSIP, tmp);
136                 setenv ("dnsip", tmp);
137         }
138 #if defined(CONFIG_BOOTP_DNS2)
139         if (NetOurDNS2IP) {
140                 ip_to_string (NetOurDNS2IP, tmp);
141                 setenv ("dnsip2", tmp);
142         }
143 #endif
144         if (NetOurNISDomain[0])
145                 setenv ("domain", NetOurNISDomain);
146
147 #if defined(CONFIG_CMD_SNTP) \
148     && defined(CONFIG_BOOTP_TIMEOFFSET)
149         if (NetTimeOffset) {
150                 sprintf (tmp, "%d", NetTimeOffset);
151                 setenv ("timeoffset", tmp);
152         }
153 #endif
154 #if defined(CONFIG_CMD_SNTP) \
155     && defined(CONFIG_BOOTP_NTPSERVER)
156         if (NetNtpServerIP) {
157                 ip_to_string (NetNtpServerIP, tmp);
158                 setenv ("ntpserverip", tmp);
159         }
160 #endif
161 }
162
163 static int
164 netboot_common (proto_t proto, cmd_tbl_t *cmdtp, int argc, char *argv[])
165 {
166         char *s;
167         int   rcode = 0;
168         int   size;
169
170         /* pre-set load_addr */
171         if ((s = getenv("loadaddr")) != NULL) {
172                 load_addr = simple_strtoul(s, NULL, 16);
173         }
174
175         switch (argc) {
176         case 1:
177                 break;
178
179         case 2: /* only one arg - accept two forms:
180                  * just load address, or just boot file name.
181                  * The latter form must be written "filename" here.
182                  */
183                 if (argv[1][0] == '"') {        /* just boot filename */
184                         copy_filename (BootFile, argv[1], sizeof(BootFile));
185                 } else {                        /* load address */
186                         load_addr = simple_strtoul(argv[1], NULL, 16);
187                 }
188                 break;
189
190         case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
191                 copy_filename (BootFile, argv[2], sizeof(BootFile));
192
193                 break;
194
195         default: printf ("Usage:\n%s\n", cmdtp->usage);
196                 SHOW_BOOT_PROGRESS(-80);
197                 return 1;
198         }
199
200         SHOW_BOOT_PROGRESS(80);
201         if ((size = NetLoop(proto)) < 0) {
202                 SHOW_BOOT_PROGRESS(-81);
203                 return 1;
204         }
205
206         SHOW_BOOT_PROGRESS(81);
207         /* NetLoop ok, update environment */
208         netboot_update_env();
209
210         /* done if no file was loaded (no errors though) */
211         if (size == 0) {
212                 SHOW_BOOT_PROGRESS(-82);
213                 return 0;
214         }
215
216         /* flush cache */
217         flush_cache(load_addr, size);
218
219         /* Loading ok, check if we should attempt an auto-start */
220         if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == 0)) {
221                 char *local_args[2];
222                 local_args[0] = argv[0];
223                 local_args[1] = NULL;
224
225                 printf ("Automatic boot of image at addr 0x%08lX ...\n",
226                         load_addr);
227                 SHOW_BOOT_PROGRESS(82);
228                 rcode = do_bootm (cmdtp, 0, 1, local_args);
229         }
230
231 #ifdef CONFIG_AUTOSCRIPT
232         if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
233                 printf("Running autoscript at addr 0x%08lX ...\n", load_addr);
234                 SHOW_BOOT_PROGRESS(83);
235                 rcode = autoscript (load_addr);
236         }
237 #endif
238 #if defined(CONFIG_SHOW_BOOT_PROGRESS)
239         if (rcode < 0)
240                 SHOW_BOOT_PROGRESS(-83);
241         else
242                 SHOW_BOOT_PROGRESS(84);
243 #endif
244         return rcode;
245 }
246
247 #if defined(CONFIG_CMD_PING)
248 int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
249 {
250         if (argc < 2)
251                 return -1;
252
253         NetPingIP = string_to_ip(argv[1]);
254         if (NetPingIP == 0) {
255                 printf ("Usage:\n%s\n", cmdtp->usage);
256                 return -1;
257         }
258
259         if (NetLoop(PING) < 0) {
260                 printf("ping failed; host %s is not alive\n", argv[1]);
261                 return 1;
262         }
263
264         printf("host %s is alive\n", argv[1]);
265
266         return 0;
267 }
268
269 U_BOOT_CMD(
270         ping,   2,      1,      do_ping,
271         "ping\t- send ICMP ECHO_REQUEST to network host\n",
272         "pingAddress\n"
273 );
274 #endif
275
276 #if defined(CONFIG_CMD_CDP)
277
278 static void cdp_update_env(void)
279 {
280         char tmp[16];
281
282         if (CDPApplianceVLAN != htons(-1)) {
283                 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
284                 VLAN_to_string(CDPApplianceVLAN, tmp);
285                 setenv("vlan", tmp);
286                 NetOurVLAN = CDPApplianceVLAN;
287         }
288
289         if (CDPNativeVLAN != htons(-1)) {
290                 printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
291                 VLAN_to_string(CDPNativeVLAN, tmp);
292                 setenv("nvlan", tmp);
293                 NetOurNativeVLAN = CDPNativeVLAN;
294         }
295
296 }
297
298 int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
299 {
300         int r;
301
302         r = NetLoop(CDP);
303         if (r < 0) {
304                 printf("cdp failed; perhaps not a CISCO switch?\n");
305                 return 1;
306         }
307
308         cdp_update_env();
309
310         return 0;
311 }
312
313 U_BOOT_CMD(
314         cdp,    1,      1,      do_cdp,
315         "cdp\t- Perform CDP network configuration\n",
316 );
317 #endif
318
319 #if defined(CONFIG_CMD_SNTP)
320 int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
321 {
322         char *toff;
323
324         if (argc < 2) {
325                 NetNtpServerIP = getenv_IPaddr ("ntpserverip");
326                 if (NetNtpServerIP == 0) {
327                         printf ("ntpserverip not set\n");
328                         return (1);
329                 }
330         } else {
331                 NetNtpServerIP = string_to_ip(argv[1]);
332                 if (NetNtpServerIP == 0) {
333                         printf ("Bad NTP server IP address\n");
334                         return (1);
335                 }
336         }
337
338         toff = getenv ("timeoffset");
339         if (toff == NULL) NetTimeOffset = 0;
340         else NetTimeOffset = simple_strtol (toff, NULL, 10);
341
342         if (NetLoop(SNTP) < 0) {
343                 printf("SNTP failed: host %s not responding\n", argv[1]);
344                 return 1;
345         }
346
347         return 0;
348 }
349
350 U_BOOT_CMD(
351         sntp,   2,      1,      do_sntp,
352         "sntp\t- synchronize RTC via network\n",
353         "[NTP server IP]\n"
354 );
355 #endif
356
357 #endif