e8bf0ae9f2b4b34d740a2e153eae16c1c6a3a726
[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 <cmd_net.h>
30 #include <net.h>
31
32 #if (CONFIG_COMMANDS & CFG_CMD_NET)
33
34 # if (CONFIG_COMMANDS & CFG_CMD_AUTOSCRIPT)
35 # include <cmd_autoscript.h>
36 # endif
37
38 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
39
40 static int netboot_common (int, cmd_tbl_t *, int , char *[]);
41
42 int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
43 {
44         return netboot_common (BOOTP, cmdtp, argc, argv);
45 }
46
47 int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
48 {
49         return netboot_common (TFTP, cmdtp, argc, argv);
50 }
51
52 int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
53 {
54         return netboot_common (RARP, cmdtp, argc, argv);
55 }
56
57 #if (CONFIG_COMMANDS & CFG_CMD_DHCP)
58 int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
59 {
60         return netboot_common(DHCP, cmdtp, argc, argv);
61 }
62 #endif  /* CFG_CMD_DHCP */
63
64 static void netboot_update_env(void)
65 {
66     char tmp[16] ;
67
68     if (NetOurGatewayIP) {
69         ip_to_string (NetOurGatewayIP, tmp);
70         setenv("gatewayip", tmp);
71     }
72
73     if (NetOurSubnetMask) {
74         ip_to_string (NetOurSubnetMask, tmp);
75         setenv("netmask", tmp);
76     }
77
78     if (NetOurHostName[0])
79         setenv("hostname", NetOurHostName);
80
81     if (NetOurRootPath[0])
82         setenv("rootpath", NetOurRootPath);
83
84     if (NetOurIP) {
85         ip_to_string (NetOurIP, tmp);
86         setenv("ipaddr", tmp);
87     }
88
89     if (NetServerIP) {
90         ip_to_string (NetServerIP, tmp);
91         setenv("serverip", tmp);
92     }
93
94     if (NetOurDNSIP) {
95         ip_to_string (NetOurDNSIP, tmp);
96         setenv("dnsip", tmp);
97     }
98
99     if (NetOurNISDomain[0])
100         setenv("domain", NetOurNISDomain);
101
102 }
103 static int
104 netboot_common (int proto, cmd_tbl_t *cmdtp, int argc, char *argv[])
105 {
106         char *s;
107         int   rcode = 0;
108         int   size;
109
110         /* pre-set load_addr */
111         if ((s = getenv("loadaddr")) != NULL) {
112                 load_addr = simple_strtoul(s, NULL, 16);
113         }
114
115         switch (argc) {
116         case 1:
117                 break;
118
119         case 2: /* only one arg - accept two forms:
120                  * just load address, or just boot file name.
121                  * The latter form must be written "filename" here.
122                  */
123                 if (argv[1][0] == '"') {        /* just boot filename */
124                         copy_filename (BootFile, argv[1], sizeof(BootFile));
125                 } else {                        /* load address */
126                         load_addr = simple_strtoul(argv[1], NULL, 16);
127                 }
128                 break;
129
130         case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
131                 copy_filename (BootFile, argv[2], sizeof(BootFile));
132
133                 break;
134
135         default: printf ("Usage:\n%s\n", cmdtp->usage);
136                 return 1;
137         }
138
139         if ((size = NetLoop(proto)) < 0)
140                 return 1;
141
142         /* NetLoop ok, update environment */
143         netboot_update_env();
144
145         /* done if no file was loaded (no errors though) */
146         if (size == 0)
147                 return 0;
148
149         /* flush cache */
150         flush_cache(load_addr, size);
151
152         /* Loading ok, check if we should attempt an auto-start */
153         if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == 0)) {
154                 char *local_args[2];
155                 local_args[0] = argv[0];
156                 local_args[1] = NULL;
157
158                 printf ("Automatic boot of image at addr 0x%08lX ...\n",
159                         load_addr);
160                 rcode = do_bootm (cmdtp, 0, 1, local_args);
161         }
162
163 #ifdef CONFIG_AUTOSCRIPT
164         if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
165                 printf("Running autoscript at addr 0x%08lX ...\n", load_addr);
166                 rcode = autoscript (load_addr);
167         }
168 #endif
169         return rcode;
170 }
171
172 #if (CONFIG_COMMANDS & CFG_CMD_PING)
173 int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
174 {
175         if (argc < 2)
176                 return -1;
177
178         NetPingIP = string_to_ip(argv[1]);
179         if (NetPingIP == 0) {
180                 printf ("Usage:\n%s\n", cmdtp->usage);
181                 return -1;
182         }
183
184         if (NetLoop(PING) < 0) {
185                 printf("ping failed; host %s is not alive\n", argv[1]);
186                 return 1;
187         }
188
189         printf("host %s is alive\n", argv[1]);
190
191         return 0;
192 }
193 #endif  /* CFG_CMD_PING */
194
195 #endif  /* CFG_CMD_NET */