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