Update QCA956x GPIO OUT functions list
[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 defined(CONFIG_CMD_NET)
32
33 u32 save_addr;  /* Default save address for TFTPPUT */
34 u32 save_size;  /* Default save size (in bytes) for TFTPPUT */
35
36 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
37 static int netboot_common(proto_t, cmd_tbl_t *, int, char *[]);
38
39 #if defined(CONFIG_CMD_HTTPD)
40 int do_httpd(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
41 {
42         return NetLoopHttpd();
43 }
44
45 U_BOOT_CMD(httpd, 1, 1, do_httpd,
46            "start web server for firmware recovery\n",
47            NULL);
48 #endif /* CONFIG_CMD_HTTPD */
49
50 #if defined(CONFIG_CMD_DHCP)
51 int do_dhcp(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
52 {
53         return netboot_common(DHCP, cmdtp, argc, argv);
54 }
55
56 U_BOOT_CMD(dhcp, 3, 1, do_dhcp,
57            "invoke DHCP client to obtain IP/boot params\n",
58            NULL);
59 #endif /* CONFIG_CMD_DHCP */
60
61 #if defined(CONFIG_CMD_NFS)
62 int do_nfs(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
63 {
64         return netboot_common(NFS, cmdtp, argc, argv);
65 }
66 U_BOOT_CMD(nfs, 3, 1, do_nfs,
67            "boot image via network using NFS protocol\n",
68            "[address] [host ip addr:filename]\n"
69            "\t- loads 'filename' at 'address' using NFS protocol");
70 #endif /* CONFIG_CMD_NFS */
71
72 #if defined(CONFIG_CMD_PING)
73 int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
74 {
75         if (argc < 2) {
76                 print_cmd_help(cmdtp);
77                 return -1;
78         }
79
80         NetPingIP = string_to_ip(argv[1]);
81
82         if (NetPingIP == 0) {
83                 print_cmd_help(cmdtp);
84                 return -1;
85         }
86
87         if (NetLoop(PING) < 0) {
88                 puts("\n");
89                 printf_err("ping failed, host %s is not alive!\n\n", argv[1]);
90                 return 1;
91         }
92
93         printf("\nPing OK, host %s is alive!\n\n", argv[1]);
94
95         return 0;
96 }
97
98 U_BOOT_CMD(ping, 2, 1, do_ping,
99            "send ICMP ECHO_REQUEST to network host\n",
100            "host IP\n"
101            "\t- sends ping to IP 'host IP'\n");
102 #endif /* CONFIG_CMD_PING */
103
104 #if defined(CONFIG_CMD_SNTP)
105 int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
106 {
107         char *toff;
108
109         if (argc < 2) {
110                 print_cmd_help(cmdtp);
111                 return -1;
112         } else {
113                 NetNtpServerIP = string_to_ip(argv[1]);
114
115                 if (NetNtpServerIP == 0) {
116                         printf_err("bad SNTP server IP address\n");
117                         return 1;
118                 }
119         }
120
121         toff = getenv("timeoffset");
122
123         if (toff == NULL)
124                 NetTimeOffset = 0;
125         else
126                 NetTimeOffset = simple_strtol(toff, NULL, 10);
127
128         if (NetLoop(SNTP) < 0) {
129                 printf_err("SNTP host %s not responding\n", argv[1]);
130                 return 1;
131         }
132
133         return 0 ;
134 }
135
136 U_BOOT_CMD(sntp, 2, 1, do_sntp,
137            "send NTP request to NTP server\n",
138            "ntpserverip\n"
139            "\t- sends NTP request to NTP server 'ntpserverip'\n");
140 #endif /* CONFIG_CMD_SNTP */
141
142 int do_tftpb(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
143 {
144         return netboot_common(TFTPGET, cmdtp, argc, argv);
145 }
146
147 U_BOOT_CMD(tftpboot, 3, 1, do_tftpb,
148            "boot image via network using TFTP protocol\n",
149            "[address] [filename]\n"
150            "\t- loads 'filename' at 'address' from TFTP server");
151
152 int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
153 {
154         if (argc < 4) {
155                 print_cmd_help(cmdtp);
156                 return -1;
157         }
158
159         return netboot_common(TFTPPUT, cmdtp, argc, argv);
160 }
161
162 U_BOOT_CMD(tftpput, 4, 1, do_tftpput,
163            "send file to TFTP server\n",
164            "address size filename\n"
165            "\t- sends 'size' of data from 'address' as 'filename' to TFTP server");
166
167 static void netboot_update_env(void)
168 {
169         char tmp[22];
170
171         if (NetOurGatewayIP) {
172                 ip_to_string(NetOurGatewayIP, tmp);
173                 setenv("gatewayip", tmp);
174         }
175
176         if (NetOurSubnetMask) {
177                 ip_to_string(NetOurSubnetMask, tmp);
178                 setenv("netmask", tmp);
179         }
180
181         if (NetOurHostName[0])
182                 setenv("hostname", NetOurHostName);
183
184         if (NetOurRootPath[0])
185                 setenv("rootpath", NetOurRootPath);
186
187         if (NetOurIP) {
188                 ip_to_string(NetOurIP, tmp);
189                 setenv("ipaddr", tmp);
190         }
191
192         if (NetServerIP) {
193                 ip_to_string(NetServerIP, tmp);
194                 setenv("serverip", tmp);
195         }
196
197         if (NetOurDNSIP) {
198                 ip_to_string(NetOurDNSIP, tmp);
199                 setenv("dnsip", tmp);
200         }
201
202 #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
203         if (NetOurDNS2IP) {
204                 ip_to_string(NetOurDNS2IP, tmp);
205                 setenv("dnsip2", tmp);
206         }
207 #endif
208
209         if (NetOurNISDomain[0])
210                 setenv("domain", NetOurNISDomain);
211
212 #if defined(CONFIG_CMD_SNTP)
213   #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_TIMEOFFSET)
214         if (NetTimeOffset) {
215                 sprintf(tmp, "%d", NetTimeOffset);
216                 setenv("timeoffset", tmp);
217         }
218   #endif
219
220   #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_NTPSERVER)
221         if (NetNtpServerIP) {
222                 ip_to_string(NetNtpServerIP, tmp);
223                 setenv("ntpserverip", tmp);
224         }
225   #endif
226 #endif /* CONFIG_CMD_SNTP */
227 }
228
229 static int netboot_common(proto_t proto,
230                           cmd_tbl_t *cmdtp,
231                           int argc,
232                           char *argv[])
233 {
234         char *s;
235         int size;
236         ulong tmp;
237         int rcode = 0;
238
239         /* Pre-set load_addr */
240         if ((s = getenv("loadaddr")) != NULL)
241                 load_addr = simple_strtoul(s, NULL, 16);
242
243         switch (argc) {
244         case 1:
245                 break;
246         case 2:
247                 /*
248                  * Don't allow to use loading address
249                  * lower than RAM/FLASH base
250                  */
251                 tmp = simple_strtoul(argv[1], NULL, 16);
252
253 #if defined(CFG_DIRECT_FLASH_TFTP)
254                 if ((tmp < CFG_SDRAM_BASE && tmp < CFG_FLASH_BASE) || tmp == 0)
255 #else
256                 if (tmp < CFG_SDRAM_BASE || tmp == 0)
257 #endif
258                         copy_filename(BootFile, argv[1], sizeof(BootFile));
259                 else
260                         load_addr = tmp;
261
262                 break;
263         case 3:
264                 load_addr = simple_strtoul(argv[1], NULL, 16);
265                 copy_filename(BootFile, argv[2], sizeof(BootFile));
266                 break;
267         case 4:
268                 save_addr = simple_strtoul(argv[1], NULL, 16);
269                 save_size = simple_strtoul(argv[2], NULL, 16);
270                 copy_filename(BootFile, argv[3], sizeof(BootFile));
271                 break;
272         default:
273                 print_cmd_help(cmdtp);
274                 return 1;
275         }
276
277         if ((size = NetLoop(proto)) < 0)
278                 return 1;
279
280         /* NetLoop ok, update environment */
281         netboot_update_env();
282
283         /* Done if no file was loaded (no errors though) */
284         if (size == 0)
285                 return 0;
286
287         /* Flush cache */
288         flush_cache(load_addr, size);
289
290         /* Loading ok, check if we should attempt an auto-start */
291         if (((s = getenv("autostart")) != NULL) && (strcmp(s, "yes") == 0)) {
292                 char *local_args[2];
293                 local_args[0] = argv[0];
294                 local_args[1] = NULL;
295
296                 printf("Automatic boot of image at addr 0x%08lX ...\n",
297                        load_addr);
298
299                 rcode = do_bootm(cmdtp, 0, 1, local_args);
300         }
301
302 #if defined(CONFIG_AUTOSCRIPT)
303         if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
304                 printf("Running autoscript at addr 0x%08lX ...\n",
305                        load_addr);
306
307                 rcode = autoscript(load_addr);
308         }
309 #endif
310
311         return rcode;
312 }
313 #endif /* CONFIG_CMD_NET */