]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_net.c
ddr cfg: DRAM_RESET needs 0x00020030
[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
e4bf0c5c 31static int netboot_common(enum proto_t, cmd_tbl_t *, int, char * const []);
3863585b 32
088f1b19 33static int do_bootp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b 34{
088f1b19 35 return netboot_common(BOOTP, cmdtp, argc, argv);
3863585b
WD
36}
37
0d498393
WD
38U_BOOT_CMD(
39 bootp, 3, 1, do_bootp,
2fb2604d 40 "boot image via network using BOOTP/TFTP protocol",
a89c33db 41 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77
WD
42);
43
088f1b19 44int do_tftpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b 45{
573f14fe
SG
46 int ret;
47
48 bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
49 ret = netboot_common(TFTPGET, cmdtp, argc, argv);
50 bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
51 return ret;
3863585b
WD
52}
53
0d498393
WD
54U_BOOT_CMD(
55 tftpboot, 3, 1, do_tftpb,
2fb2604d 56 "boot image via network using TFTP protocol",
a89c33db 57 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77
WD
58);
59
2d46cf29
SG
60#ifdef CONFIG_CMD_TFTPPUT
61int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
62{
63 int ret;
64
65 ret = netboot_common(TFTPPUT, cmdtp, argc, argv);
66 return ret;
67}
68
69U_BOOT_CMD(
70 tftpput, 4, 1, do_tftpput,
71 "TFTP put command, for uploading files to a server",
72 "Address Size [[hostIPaddr:]filename]"
73);
74#endif
75
7a83af07
LC
76#ifdef CONFIG_CMD_TFTPSRV
77static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
78{
79 return netboot_common(TFTPSRV, cmdtp, argc, argv);
80}
81
82U_BOOT_CMD(
83 tftpsrv, 2, 1, do_tftpsrv,
84 "act as a TFTP server and boot the first received file",
85 "[loadAddress]\n"
86 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
87 "The transfer is aborted if a transfer has not been started after\n"
88 "about 50 seconds or if Ctrl-C is pressed."
89);
90#endif
91
92
bf6cb247 93#ifdef CONFIG_CMD_RARP
088f1b19 94int do_rarpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b 95{
088f1b19 96 return netboot_common(RARP, cmdtp, argc, argv);
3863585b
WD
97}
98
0d498393
WD
99U_BOOT_CMD(
100 rarpboot, 3, 1, do_rarpb,
2fb2604d 101 "boot image via network using RARP/TFTP protocol",
a89c33db 102 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77 103);
bf6cb247 104#endif
8bde7f77 105
c76fe474 106#if defined(CONFIG_CMD_DHCP)
088f1b19 107static int do_dhcp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b
WD
108{
109 return netboot_common(DHCP, cmdtp, argc, argv);
110}
8bde7f77 111
0d498393
WD
112U_BOOT_CMD(
113 dhcp, 3, 1, do_dhcp,
2fb2604d 114 "boot image via network using DHCP/TFTP protocol",
a89c33db 115 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77 116);
90253178 117#endif
3863585b 118
c76fe474 119#if defined(CONFIG_CMD_NFS)
088f1b19 120static int do_nfs(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
cbd8a35c
WD
121{
122 return netboot_common(NFS, cmdtp, argc, argv);
123}
124
125U_BOOT_CMD(
126 nfs, 3, 1, do_nfs,
2fb2604d 127 "boot image via network using NFS protocol",
a89c33db 128 "[loadAddress] [[hostIPaddr:]bootfilename]"
cbd8a35c 129);
90253178 130#endif
cbd8a35c 131
088f1b19 132static void netboot_update_env(void)
3863585b 133{
6e592385 134 char tmp[22];
3863585b 135
6e592385 136 if (NetOurGatewayIP) {
088f1b19
KP
137 ip_to_string(NetOurGatewayIP, tmp);
138 setenv("gatewayip", tmp);
6e592385 139 }
3863585b 140
6e592385 141 if (NetOurSubnetMask) {
088f1b19
KP
142 ip_to_string(NetOurSubnetMask, tmp);
143 setenv("netmask", tmp);
6e592385 144 }
3863585b 145
6e592385 146 if (NetOurHostName[0])
088f1b19 147 setenv("hostname", NetOurHostName);
3863585b 148
6e592385 149 if (NetOurRootPath[0])
088f1b19 150 setenv("rootpath", NetOurRootPath);
3863585b 151
6e592385 152 if (NetOurIP) {
088f1b19
KP
153 ip_to_string(NetOurIP, tmp);
154 setenv("ipaddr", tmp);
6e592385 155 }
a3e1a727
JH
156#if !defined(CONFIG_BOOTP_SERVERIP)
157 /*
158 * Only attempt to change serverip if net/bootp.c:BootpCopyNetParams()
159 * could have set it
160 */
6e592385 161 if (NetServerIP) {
088f1b19
KP
162 ip_to_string(NetServerIP, tmp);
163 setenv("serverip", tmp);
6e592385 164 }
a3e1a727 165#endif
6e592385 166 if (NetOurDNSIP) {
088f1b19
KP
167 ip_to_string(NetOurDNSIP, tmp);
168 setenv("dnsip", tmp);
6e592385 169 }
1fe80d79 170#if defined(CONFIG_BOOTP_DNS2)
6e592385 171 if (NetOurDNS2IP) {
088f1b19
KP
172 ip_to_string(NetOurDNS2IP, tmp);
173 setenv("dnsip2", tmp);
6e592385 174 }
fe389a82 175#endif
6e592385 176 if (NetOurNISDomain[0])
088f1b19 177 setenv("domain", NetOurNISDomain);
ea287deb 178
c76fe474 179#if defined(CONFIG_CMD_SNTP) \
1fe80d79 180 && defined(CONFIG_BOOTP_TIMEOFFSET)
ea287deb 181 if (NetTimeOffset) {
088f1b19
KP
182 sprintf(tmp, "%d", NetTimeOffset);
183 setenv("timeoffset", tmp);
ea287deb
WD
184 }
185#endif
c76fe474 186#if defined(CONFIG_CMD_SNTP) \
1fe80d79 187 && defined(CONFIG_BOOTP_NTPSERVER)
ea287deb 188 if (NetNtpServerIP) {
088f1b19
KP
189 ip_to_string(NetNtpServerIP, tmp);
190 setenv("ntpserverip", tmp);
ea287deb
WD
191 }
192#endif
3863585b 193}
6e592385 194
e4bf0c5c
SG
195static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
196 char * const argv[])
3863585b
WD
197{
198 char *s;
2e4970d8 199 char *end;
3863585b
WD
200 int rcode = 0;
201 int size;
2e4970d8 202 ulong addr;
3863585b
WD
203
204 /* pre-set load_addr */
205 if ((s = getenv("loadaddr")) != NULL) {
206 load_addr = simple_strtoul(s, NULL, 16);
207 }
208
209 switch (argc) {
210 case 1:
211 break;
212
2e4970d8
PT
213 case 2: /*
214 * Only one arg - accept two forms:
215 * Just load address, or just boot file name. The latter
216 * form must be written in a format which can not be
217 * mis-interpreted as a valid number.
3863585b 218 */
2e4970d8
PT
219 addr = simple_strtoul(argv[1], &end, 16);
220 if (end == (argv[1] + strlen(argv[1])))
221 load_addr = addr;
222 else
223 copy_filename(BootFile, argv[1], sizeof(BootFile));
3863585b
WD
224 break;
225
226 case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
088f1b19 227 copy_filename(BootFile, argv[2], sizeof(BootFile));
3863585b
WD
228
229 break;
230
2d46cf29
SG
231#ifdef CONFIG_CMD_TFTPPUT
232 case 4:
38bd80b4
SG
233 if (strict_strtoul(argv[1], 16, &save_addr) < 0 ||
234 strict_strtoul(argv[2], 16, &save_size) < 0) {
235 printf("Invalid address/size\n");
236 return cmd_usage(cmdtp);
237 }
2d46cf29
SG
238 copy_filename(BootFile, argv[3], sizeof(BootFile));
239 break;
240#endif
47e26b1b 241 default:
770605e4 242 bootstage_error(BOOTSTAGE_ID_NET_START);
4c12eeb8 243 return CMD_RET_USAGE;
3863585b 244 }
770605e4 245 bootstage_mark(BOOTSTAGE_ID_NET_START);
3863585b 246
566a494f 247 if ((size = NetLoop(proto)) < 0) {
770605e4 248 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
3863585b 249 return 1;
566a494f 250 }
770605e4 251 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
3863585b
WD
252
253 /* NetLoop ok, update environment */
254 netboot_update_env();
255
eb9401e3 256 /* done if no file was loaded (no errors though) */
566a494f 257 if (size == 0) {
770605e4 258 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
eb9401e3 259 return 0;
566a494f 260 }
eb9401e3 261
3863585b
WD
262 /* flush cache */
263 flush_cache(load_addr, size);
264
770605e4 265 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
c8e66db7 266
67d668bf 267 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
3863585b 268
566a494f 269 if (rcode < 0)
770605e4 270 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
566a494f 271 else
770605e4 272 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
3863585b
WD
273 return rcode;
274}
275
c76fe474 276#if defined(CONFIG_CMD_PING)
088f1b19 277static int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
73a8b27c
WD
278{
279 if (argc < 2)
280 return -1;
281
282 NetPingIP = string_to_ip(argv[1]);
47e26b1b 283 if (NetPingIP == 0)
4c12eeb8 284 return CMD_RET_USAGE;
73a8b27c
WD
285
286 if (NetLoop(PING) < 0) {
287 printf("ping failed; host %s is not alive\n", argv[1]);
288 return 1;
289 }
290
291 printf("host %s is alive\n", argv[1]);
292
293 return 0;
294}
6dff5529
WD
295
296U_BOOT_CMD(
297 ping, 2, 1, do_ping,
2fb2604d 298 "send ICMP ECHO_REQUEST to network host",
a89c33db 299 "pingAddress"
6dff5529 300);
90253178 301#endif
73a8b27c 302
c76fe474 303#if defined(CONFIG_CMD_CDP)
a3d991bd
WD
304
305static void cdp_update_env(void)
306{
307 char tmp[16];
308
309 if (CDPApplianceVLAN != htons(-1)) {
310 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
311 VLAN_to_string(CDPApplianceVLAN, tmp);
312 setenv("vlan", tmp);
313 NetOurVLAN = CDPApplianceVLAN;
314 }
315
316 if (CDPNativeVLAN != htons(-1)) {
317 printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
318 VLAN_to_string(CDPNativeVLAN, tmp);
319 setenv("nvlan", tmp);
320 NetOurNativeVLAN = CDPNativeVLAN;
321 }
322
323}
324
088f1b19 325int do_cdp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
a3d991bd
WD
326{
327 int r;
328
329 r = NetLoop(CDP);
330 if (r < 0) {
331 printf("cdp failed; perhaps not a CISCO switch?\n");
332 return 1;
333 }
334
335 cdp_update_env();
336
337 return 0;
338}
339
340U_BOOT_CMD(
341 cdp, 1, 1, do_cdp,
ec5c04cd 342 "Perform CDP network configuration",
4b58266e 343 "\n"
a3d991bd 344);
90253178 345#endif
a3d991bd 346
c76fe474 347#if defined(CONFIG_CMD_SNTP)
088f1b19 348int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
ea287deb
WD
349{
350 char *toff;
351
352 if (argc < 2) {
088f1b19 353 NetNtpServerIP = getenv_IPaddr("ntpserverip");
ea287deb 354 if (NetNtpServerIP == 0) {
088f1b19 355 printf("ntpserverip not set\n");
ea287deb
WD
356 return (1);
357 }
358 } else {
359 NetNtpServerIP = string_to_ip(argv[1]);
360 if (NetNtpServerIP == 0) {
088f1b19 361 printf("Bad NTP server IP address\n");
ea287deb
WD
362 return (1);
363 }
364 }
365
088f1b19
KP
366 toff = getenv("timeoffset");
367 if (toff == NULL)
368 NetTimeOffset = 0;
369 else
370 NetTimeOffset = simple_strtol(toff, NULL, 10);
ea287deb
WD
371
372 if (NetLoop(SNTP) < 0) {
d6840e3d
LP
373 printf("SNTP failed: host %pI4 not responding\n",
374 &NetNtpServerIP);
ea287deb
WD
375 return 1;
376 }
377
378 return 0;
379}
380
381U_BOOT_CMD(
382 sntp, 2, 1, do_sntp,
2fb2604d 383 "synchronize RTC via network",
ea287deb
WD
384 "[NTP server IP]\n"
385);
90253178 386#endif
1a32bf41
RG
387
388#if defined(CONFIG_CMD_DNS)
54841ab5 389int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1a32bf41 390{
47e26b1b 391 if (argc == 1)
4c12eeb8 392 return CMD_RET_USAGE;
1a32bf41
RG
393
394 /*
395 * We should check for a valid hostname:
396 * - Each label must be between 1 and 63 characters long
397 * - the entire hostname has a maximum of 255 characters
398 * - only the ASCII letters 'a' through 'z' (case-insensitive),
399 * the digits '0' through '9', and the hyphen
400 * - cannot begin or end with a hyphen
401 * - no other symbols, punctuation characters, or blank spaces are
402 * permitted
403 * but hey - this is a minimalist implmentation, so only check length
404 * and let the name server deal with things.
405 */
406 if (strlen(argv[1]) >= 255) {
407 printf("dns error: hostname too long\n");
408 return 1;
409 }
410
411 NetDNSResolve = argv[1];
412
413 if (argc == 3)
414 NetDNSenvvar = argv[2];
415 else
416 NetDNSenvvar = NULL;
417
418 if (NetLoop(DNS) < 0) {
419 printf("dns lookup of %s failed, check setup\n", argv[1]);
420 return 1;
421 }
422
423 return 0;
424}
425
426U_BOOT_CMD(
427 dns, 3, 1, do_dns,
428 "lookup the IP of a hostname",
429 "hostname [envvar]"
430);
431
432#endif /* CONFIG_CMD_DNS */
d22c338e
JH
433
434#if defined(CONFIG_CMD_LINK_LOCAL)
435static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc,
436 char * const argv[])
437{
438 char tmp[22];
439
440 if (NetLoop(LINKLOCAL) < 0)
441 return 1;
442
443 NetOurGatewayIP = 0;
444 ip_to_string(NetOurGatewayIP, tmp);
445 setenv("gatewayip", tmp);
446
447 ip_to_string(NetOurSubnetMask, tmp);
448 setenv("netmask", tmp);
449
450 ip_to_string(NetOurIP, tmp);
451 setenv("ipaddr", tmp);
452 setenv("llipaddr", tmp); /* store this for next time */
453
454 return 0;
455}
456
457U_BOOT_CMD(
458 linklocal, 1, 1, do_link_local,
459 "acquire a network IP address using the link-local protocol",
460 ""
461);
462
463#endif /* CONFIG_CMD_LINK_LOCAL */