]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_net.c
net: Fix emaclite driver to support little-endian microblaze
[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
54841ab5 31extern int do_bootm (cmd_tbl_t *, int, int, char * const []);
3863585b 32
54841ab5 33static int netboot_common (proto_t, cmd_tbl_t *, int , char * const []);
3863585b 34
54841ab5 35int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b
WD
36{
37 return netboot_common (BOOTP, cmdtp, argc, argv);
38}
39
0d498393
WD
40U_BOOT_CMD(
41 bootp, 3, 1, do_bootp,
2fb2604d 42 "boot image via network using BOOTP/TFTP protocol",
a89c33db 43 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77
WD
44);
45
54841ab5 46int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b
WD
47{
48 return netboot_common (TFTP, cmdtp, argc, argv);
49}
50
0d498393
WD
51U_BOOT_CMD(
52 tftpboot, 3, 1, do_tftpb,
2fb2604d 53 "boot image via network using TFTP protocol",
a89c33db 54 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77
WD
55);
56
54841ab5 57int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b
WD
58{
59 return netboot_common (RARP, cmdtp, argc, argv);
60}
61
0d498393
WD
62U_BOOT_CMD(
63 rarpboot, 3, 1, do_rarpb,
2fb2604d 64 "boot image via network using RARP/TFTP protocol",
a89c33db 65 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77
WD
66);
67
c76fe474 68#if defined(CONFIG_CMD_DHCP)
54841ab5 69int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b
WD
70{
71 return netboot_common(DHCP, cmdtp, argc, argv);
72}
8bde7f77 73
0d498393
WD
74U_BOOT_CMD(
75 dhcp, 3, 1, do_dhcp,
2fb2604d 76 "boot image via network using DHCP/TFTP protocol",
a89c33db 77 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77 78);
90253178 79#endif
3863585b 80
c76fe474 81#if defined(CONFIG_CMD_NFS)
54841ab5 82int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
cbd8a35c
WD
83{
84 return netboot_common(NFS, cmdtp, argc, argv);
85}
86
87U_BOOT_CMD(
88 nfs, 3, 1, do_nfs,
2fb2604d 89 "boot image via network using NFS protocol",
a89c33db 90 "[loadAddress] [[hostIPaddr:]bootfilename]"
cbd8a35c 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
54841ab5 154netboot_common (proto_t proto, cmd_tbl_t *cmdtp, int argc, char * const argv[])
3863585b
WD
155{
156 char *s;
2e4970d8 157 char *end;
3863585b
WD
158 int rcode = 0;
159 int size;
2e4970d8 160 ulong addr;
3863585b
WD
161
162 /* pre-set load_addr */
163 if ((s = getenv("loadaddr")) != NULL) {
164 load_addr = simple_strtoul(s, NULL, 16);
165 }
166
167 switch (argc) {
168 case 1:
169 break;
170
2e4970d8
PT
171 case 2: /*
172 * Only one arg - accept two forms:
173 * Just load address, or just boot file name. The latter
174 * form must be written in a format which can not be
175 * mis-interpreted as a valid number.
3863585b 176 */
2e4970d8
PT
177 addr = simple_strtoul(argv[1], &end, 16);
178 if (end == (argv[1] + strlen(argv[1])))
179 load_addr = addr;
180 else
181 copy_filename(BootFile, argv[1], sizeof(BootFile));
3863585b
WD
182 break;
183
184 case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
185 copy_filename (BootFile, argv[2], sizeof(BootFile));
186
187 break;
188
47e26b1b 189 default:
fad63407 190 show_boot_progress (-80);
47e26b1b 191 return cmd_usage(cmdtp);
3863585b
WD
192 }
193
fad63407 194 show_boot_progress (80);
566a494f 195 if ((size = NetLoop(proto)) < 0) {
fad63407 196 show_boot_progress (-81);
3863585b 197 return 1;
566a494f 198 }
3863585b 199
fad63407 200 show_boot_progress (81);
3863585b
WD
201 /* NetLoop ok, update environment */
202 netboot_update_env();
203
eb9401e3 204 /* done if no file was loaded (no errors though) */
566a494f 205 if (size == 0) {
fad63407 206 show_boot_progress (-82);
eb9401e3 207 return 0;
566a494f 208 }
eb9401e3 209
3863585b
WD
210 /* flush cache */
211 flush_cache(load_addr, size);
212
213 /* Loading ok, check if we should attempt an auto-start */
214 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == 0)) {
215 char *local_args[2];
216 local_args[0] = argv[0];
217 local_args[1] = NULL;
218
219 printf ("Automatic boot of image at addr 0x%08lX ...\n",
220 load_addr);
fad63407 221 show_boot_progress (82);
3863585b
WD
222 rcode = do_bootm (cmdtp, 0, 1, local_args);
223 }
224
566a494f 225 if (rcode < 0)
fad63407 226 show_boot_progress (-83);
566a494f 227 else
fad63407 228 show_boot_progress (84);
3863585b
WD
229 return rcode;
230}
231
c76fe474 232#if defined(CONFIG_CMD_PING)
54841ab5 233int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
73a8b27c
WD
234{
235 if (argc < 2)
236 return -1;
237
238 NetPingIP = string_to_ip(argv[1]);
47e26b1b
WD
239 if (NetPingIP == 0)
240 return cmd_usage(cmdtp);
73a8b27c
WD
241
242 if (NetLoop(PING) < 0) {
243 printf("ping failed; host %s is not alive\n", argv[1]);
244 return 1;
245 }
246
247 printf("host %s is alive\n", argv[1]);
248
249 return 0;
250}
6dff5529
WD
251
252U_BOOT_CMD(
253 ping, 2, 1, do_ping,
2fb2604d 254 "send ICMP ECHO_REQUEST to network host",
a89c33db 255 "pingAddress"
6dff5529 256);
90253178 257#endif
73a8b27c 258
c76fe474 259#if defined(CONFIG_CMD_CDP)
a3d991bd
WD
260
261static void cdp_update_env(void)
262{
263 char tmp[16];
264
265 if (CDPApplianceVLAN != htons(-1)) {
266 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
267 VLAN_to_string(CDPApplianceVLAN, tmp);
268 setenv("vlan", tmp);
269 NetOurVLAN = CDPApplianceVLAN;
270 }
271
272 if (CDPNativeVLAN != htons(-1)) {
273 printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
274 VLAN_to_string(CDPNativeVLAN, tmp);
275 setenv("nvlan", tmp);
276 NetOurNativeVLAN = CDPNativeVLAN;
277 }
278
279}
280
54841ab5 281int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
a3d991bd
WD
282{
283 int r;
284
285 r = NetLoop(CDP);
286 if (r < 0) {
287 printf("cdp failed; perhaps not a CISCO switch?\n");
288 return 1;
289 }
290
291 cdp_update_env();
292
293 return 0;
294}
295
296U_BOOT_CMD(
297 cdp, 1, 1, do_cdp,
2fb2604d 298 "Perform CDP network configuration",
a3d991bd 299);
90253178 300#endif
a3d991bd 301
c76fe474 302#if defined(CONFIG_CMD_SNTP)
54841ab5 303int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
ea287deb
WD
304{
305 char *toff;
306
307 if (argc < 2) {
308 NetNtpServerIP = getenv_IPaddr ("ntpserverip");
309 if (NetNtpServerIP == 0) {
310 printf ("ntpserverip not set\n");
311 return (1);
312 }
313 } else {
314 NetNtpServerIP = string_to_ip(argv[1]);
315 if (NetNtpServerIP == 0) {
316 printf ("Bad NTP server IP address\n");
317 return (1);
318 }
319 }
320
321 toff = getenv ("timeoffset");
322 if (toff == NULL) NetTimeOffset = 0;
323 else NetTimeOffset = simple_strtol (toff, NULL, 10);
324
325 if (NetLoop(SNTP) < 0) {
326 printf("SNTP failed: host %s not responding\n", argv[1]);
327 return 1;
328 }
329
330 return 0;
331}
332
333U_BOOT_CMD(
334 sntp, 2, 1, do_sntp,
2fb2604d 335 "synchronize RTC via network",
ea287deb
WD
336 "[NTP server IP]\n"
337);
90253178 338#endif
1a32bf41
RG
339
340#if defined(CONFIG_CMD_DNS)
54841ab5 341int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1a32bf41 342{
47e26b1b
WD
343 if (argc == 1)
344 return cmd_usage(cmdtp);
1a32bf41
RG
345
346 /*
347 * We should check for a valid hostname:
348 * - Each label must be between 1 and 63 characters long
349 * - the entire hostname has a maximum of 255 characters
350 * - only the ASCII letters 'a' through 'z' (case-insensitive),
351 * the digits '0' through '9', and the hyphen
352 * - cannot begin or end with a hyphen
353 * - no other symbols, punctuation characters, or blank spaces are
354 * permitted
355 * but hey - this is a minimalist implmentation, so only check length
356 * and let the name server deal with things.
357 */
358 if (strlen(argv[1]) >= 255) {
359 printf("dns error: hostname too long\n");
360 return 1;
361 }
362
363 NetDNSResolve = argv[1];
364
365 if (argc == 3)
366 NetDNSenvvar = argv[2];
367 else
368 NetDNSenvvar = NULL;
369
370 if (NetLoop(DNS) < 0) {
371 printf("dns lookup of %s failed, check setup\n", argv[1]);
372 return 1;
373 }
374
375 return 0;
376}
377
378U_BOOT_CMD(
379 dns, 3, 1, do_dns,
380 "lookup the IP of a hostname",
381 "hostname [envvar]"
382);
383
384#endif /* CONFIG_CMD_DNS */