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