3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
31 static int netboot_common(enum proto_t, cmd_tbl_t *, int, char * const []);
33 int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
35 return netboot_common (BOOTP, cmdtp, argc, argv);
39 bootp, 3, 1, do_bootp,
40 "boot image via network using BOOTP/TFTP protocol",
41 "[loadAddress] [[hostIPaddr:]bootfilename]"
44 int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
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");
55 tftpboot, 3, 1, do_tftpb,
56 "boot image via network using TFTP protocol",
57 "[loadAddress] [[hostIPaddr:]bootfilename]"
60 #ifdef CONFIG_CMD_TFTPPUT
61 int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
65 ret = netboot_common(TFTPPUT, cmdtp, argc, argv);
70 tftpput, 4, 1, do_tftpput,
71 "TFTP put command, for uploading files to a server",
72 "Address Size [[hostIPaddr:]filename]"
76 #ifdef CONFIG_CMD_TFTPSRV
77 static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
79 return netboot_common(TFTPSRV, cmdtp, argc, argv);
83 tftpsrv, 2, 1, do_tftpsrv,
84 "act as a TFTP server and boot the first received file",
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."
93 #ifdef CONFIG_CMD_RARP
94 int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
96 return netboot_common (RARP, cmdtp, argc, argv);
100 rarpboot, 3, 1, do_rarpb,
101 "boot image via network using RARP/TFTP protocol",
102 "[loadAddress] [[hostIPaddr:]bootfilename]"
106 #if defined(CONFIG_CMD_DHCP)
107 int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
109 return netboot_common(DHCP, cmdtp, argc, argv);
114 "boot image via network using DHCP/TFTP protocol",
115 "[loadAddress] [[hostIPaddr:]bootfilename]"
119 #if defined(CONFIG_CMD_NFS)
120 int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
122 return netboot_common(NFS, cmdtp, argc, argv);
127 "boot image via network using NFS protocol",
128 "[loadAddress] [[hostIPaddr:]bootfilename]"
132 static void netboot_update_env (void)
136 if (NetOurGatewayIP) {
137 ip_to_string (NetOurGatewayIP, tmp);
138 setenv ("gatewayip", tmp);
141 if (NetOurSubnetMask) {
142 ip_to_string (NetOurSubnetMask, tmp);
143 setenv ("netmask", tmp);
146 if (NetOurHostName[0])
147 setenv ("hostname", NetOurHostName);
149 if (NetOurRootPath[0])
150 setenv ("rootpath", NetOurRootPath);
153 ip_to_string (NetOurIP, tmp);
154 setenv ("ipaddr", tmp);
156 #if !defined(CONFIG_BOOTP_SERVERIP)
158 * Only attempt to change serverip if net/bootp.c:BootpCopyNetParams()
162 ip_to_string (NetServerIP, tmp);
163 setenv ("serverip", tmp);
167 ip_to_string (NetOurDNSIP, tmp);
168 setenv ("dnsip", tmp);
170 #if defined(CONFIG_BOOTP_DNS2)
172 ip_to_string (NetOurDNS2IP, tmp);
173 setenv ("dnsip2", tmp);
176 if (NetOurNISDomain[0])
177 setenv ("domain", NetOurNISDomain);
179 #if defined(CONFIG_CMD_SNTP) \
180 && defined(CONFIG_BOOTP_TIMEOFFSET)
182 sprintf (tmp, "%d", NetTimeOffset);
183 setenv ("timeoffset", tmp);
186 #if defined(CONFIG_CMD_SNTP) \
187 && defined(CONFIG_BOOTP_NTPSERVER)
188 if (NetNtpServerIP) {
189 ip_to_string (NetNtpServerIP, tmp);
190 setenv ("ntpserverip", tmp);
195 static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
204 /* pre-set load_addr */
205 if ((s = getenv("loadaddr")) != NULL) {
206 load_addr = simple_strtoul(s, NULL, 16);
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.
219 addr = simple_strtoul(argv[1], &end, 16);
220 if (end == (argv[1] + strlen(argv[1])))
223 copy_filename(BootFile, argv[1], sizeof(BootFile));
226 case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
227 copy_filename (BootFile, argv[2], sizeof(BootFile));
231 #ifdef CONFIG_CMD_TFTPPUT
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);
238 copy_filename(BootFile, argv[3], sizeof(BootFile));
242 bootstage_error(BOOTSTAGE_ID_NET_START);
243 return CMD_RET_USAGE;
245 bootstage_mark(BOOTSTAGE_ID_NET_START);
247 if ((size = NetLoop(proto)) < 0) {
248 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
251 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
253 /* NetLoop ok, update environment */
254 netboot_update_env();
256 /* done if no file was loaded (no errors though) */
258 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
263 flush_cache(load_addr, size);
265 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
267 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
270 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
272 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
276 #if defined(CONFIG_CMD_PING)
277 int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
282 NetPingIP = string_to_ip(argv[1]);
284 return CMD_RET_USAGE;
286 if (NetLoop(PING) < 0) {
287 printf("ping failed; host %s is not alive\n", argv[1]);
291 printf("host %s is alive\n", argv[1]);
298 "send ICMP ECHO_REQUEST to network host",
303 #if defined(CONFIG_CMD_CDP)
305 static void cdp_update_env(void)
309 if (CDPApplianceVLAN != htons(-1)) {
310 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
311 VLAN_to_string(CDPApplianceVLAN, tmp);
313 NetOurVLAN = CDPApplianceVLAN;
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;
325 int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
331 printf("cdp failed; perhaps not a CISCO switch?\n");
342 "Perform CDP network configuration",
347 #if defined(CONFIG_CMD_SNTP)
348 int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
353 NetNtpServerIP = getenv_IPaddr ("ntpserverip");
354 if (NetNtpServerIP == 0) {
355 printf ("ntpserverip not set\n");
359 NetNtpServerIP = string_to_ip(argv[1]);
360 if (NetNtpServerIP == 0) {
361 printf ("Bad NTP server IP address\n");
366 toff = getenv ("timeoffset");
367 if (toff == NULL) NetTimeOffset = 0;
368 else NetTimeOffset = simple_strtol (toff, NULL, 10);
370 if (NetLoop(SNTP) < 0) {
371 printf("SNTP failed: host %pI4 not responding\n",
381 "synchronize RTC via network",
386 #if defined(CONFIG_CMD_DNS)
387 int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
390 return CMD_RET_USAGE;
393 * We should check for a valid hostname:
394 * - Each label must be between 1 and 63 characters long
395 * - the entire hostname has a maximum of 255 characters
396 * - only the ASCII letters 'a' through 'z' (case-insensitive),
397 * the digits '0' through '9', and the hyphen
398 * - cannot begin or end with a hyphen
399 * - no other symbols, punctuation characters, or blank spaces are
401 * but hey - this is a minimalist implmentation, so only check length
402 * and let the name server deal with things.
404 if (strlen(argv[1]) >= 255) {
405 printf("dns error: hostname too long\n");
409 NetDNSResolve = argv[1];
412 NetDNSenvvar = argv[2];
416 if (NetLoop(DNS) < 0) {
417 printf("dns lookup of %s failed, check setup\n", argv[1]);
426 "lookup the IP of a hostname",
430 #endif /* CONFIG_CMD_DNS */
432 #if defined(CONFIG_CMD_LINK_LOCAL)
433 static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc,
438 if (NetLoop(LINKLOCAL) < 0)
442 ip_to_string(NetOurGatewayIP, tmp);
443 setenv("gatewayip", tmp);
445 ip_to_string(NetOurSubnetMask, tmp);
446 setenv("netmask", tmp);
448 ip_to_string(NetOurIP, tmp);
449 setenv("ipaddr", tmp);
450 setenv("llipaddr", tmp); /* store this for next time */
456 linklocal, 1, 1, do_link_local,
457 "acquire a network IP address using the link-local protocol",
461 #endif /* CONFIG_CMD_LINK_LOCAL */