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
[])
46 return netboot_common(TFTPGET
, cmdtp
, argc
, argv
);
50 tftpboot
, 3, 1, do_tftpb
,
51 "boot image via network using TFTP protocol",
52 "[loadAddress] [[hostIPaddr:]bootfilename]"
55 #ifdef CONFIG_CMD_TFTPPUT
56 int do_tftpput(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
60 ret
= netboot_common(TFTPPUT
, cmdtp
, argc
, argv
);
65 tftpput
, 4, 1, do_tftpput
,
66 "TFTP put command, for uploading files to a server",
67 "Address Size [[hostIPaddr:]filename]"
71 #ifdef CONFIG_CMD_TFTPSRV
72 static int do_tftpsrv(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *const argv
[])
74 return netboot_common(TFTPSRV
, cmdtp
, argc
, argv
);
78 tftpsrv
, 2, 1, do_tftpsrv
,
79 "act as a TFTP server and boot the first received file",
81 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
82 "The transfer is aborted if a transfer has not been started after\n"
83 "about 50 seconds or if Ctrl-C is pressed."
88 #ifdef CONFIG_CMD_RARP
89 int do_rarpb (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
91 return netboot_common (RARP
, cmdtp
, argc
, argv
);
95 rarpboot
, 3, 1, do_rarpb
,
96 "boot image via network using RARP/TFTP protocol",
97 "[loadAddress] [[hostIPaddr:]bootfilename]"
101 #if defined(CONFIG_CMD_DHCP)
102 int do_dhcp (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
104 return netboot_common(DHCP
, cmdtp
, argc
, argv
);
109 "boot image via network using DHCP/TFTP protocol",
110 "[loadAddress] [[hostIPaddr:]bootfilename]"
114 #if defined(CONFIG_CMD_NFS)
115 int do_nfs (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
117 return netboot_common(NFS
, cmdtp
, argc
, argv
);
122 "boot image via network using NFS protocol",
123 "[loadAddress] [[hostIPaddr:]bootfilename]"
127 static void netboot_update_env (void)
131 if (NetOurGatewayIP
) {
132 ip_to_string (NetOurGatewayIP
, tmp
);
133 setenv ("gatewayip", tmp
);
136 if (NetOurSubnetMask
) {
137 ip_to_string (NetOurSubnetMask
, tmp
);
138 setenv ("netmask", tmp
);
141 if (NetOurHostName
[0])
142 setenv ("hostname", NetOurHostName
);
144 if (NetOurRootPath
[0])
145 setenv ("rootpath", NetOurRootPath
);
148 ip_to_string (NetOurIP
, tmp
);
149 setenv ("ipaddr", tmp
);
153 ip_to_string (NetServerIP
, tmp
);
154 setenv ("serverip", tmp
);
158 ip_to_string (NetOurDNSIP
, tmp
);
159 setenv ("dnsip", tmp
);
161 #if defined(CONFIG_BOOTP_DNS2)
163 ip_to_string (NetOurDNS2IP
, tmp
);
164 setenv ("dnsip2", tmp
);
167 if (NetOurNISDomain
[0])
168 setenv ("domain", NetOurNISDomain
);
170 #if defined(CONFIG_CMD_SNTP) \
171 && defined(CONFIG_BOOTP_TIMEOFFSET)
173 sprintf (tmp
, "%d", NetTimeOffset
);
174 setenv ("timeoffset", tmp
);
177 #if defined(CONFIG_CMD_SNTP) \
178 && defined(CONFIG_BOOTP_NTPSERVER)
179 if (NetNtpServerIP
) {
180 ip_to_string (NetNtpServerIP
, tmp
);
181 setenv ("ntpserverip", tmp
);
186 static int netboot_common(enum proto_t proto
, cmd_tbl_t
*cmdtp
, int argc
,
195 /* pre-set load_addr */
196 if ((s
= getenv("loadaddr")) != NULL
) {
197 load_addr
= simple_strtoul(s
, NULL
, 16);
205 * Only one arg - accept two forms:
206 * Just load address, or just boot file name. The latter
207 * form must be written in a format which can not be
208 * mis-interpreted as a valid number.
210 addr
= simple_strtoul(argv
[1], &end
, 16);
211 if (end
== (argv
[1] + strlen(argv
[1])))
214 copy_filename(BootFile
, argv
[1], sizeof(BootFile
));
217 case 3: load_addr
= simple_strtoul(argv
[1], NULL
, 16);
218 copy_filename (BootFile
, argv
[2], sizeof(BootFile
));
222 #ifdef CONFIG_CMD_TFTPPUT
224 if (strict_strtoul(argv
[1], 16, &save_addr
) < 0 ||
225 strict_strtoul(argv
[2], 16, &save_size
) < 0) {
226 printf("Invalid address/size\n");
227 return cmd_usage(cmdtp
);
229 copy_filename(BootFile
, argv
[3], sizeof(BootFile
));
233 bootstage_error(BOOTSTAGE_ID_NET_START
);
234 return CMD_RET_USAGE
;
236 bootstage_mark(BOOTSTAGE_ID_NET_START
);
238 if ((size
= NetLoop(proto
)) < 0) {
239 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK
);
242 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK
);
244 /* NetLoop ok, update environment */
245 netboot_update_env();
247 /* done if no file was loaded (no errors though) */
249 bootstage_error(BOOTSTAGE_ID_NET_LOADED
);
254 flush_cache(load_addr
, size
);
256 bootstage_mark(BOOTSTAGE_ID_NET_LOADED
);
258 rcode
= bootm_maybe_autostart(cmdtp
, argv
[0]);
261 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR
);
263 bootstage_mark(BOOTSTAGE_ID_NET_DONE
);
267 #if defined(CONFIG_CMD_PING)
268 int do_ping (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
273 NetPingIP
= string_to_ip(argv
[1]);
275 return CMD_RET_USAGE
;
277 if (NetLoop(PING
) < 0) {
278 printf("ping failed; host %s is not alive\n", argv
[1]);
282 printf("host %s is alive\n", argv
[1]);
289 "send ICMP ECHO_REQUEST to network host",
294 #if defined(CONFIG_CMD_CDP)
296 static void cdp_update_env(void)
300 if (CDPApplianceVLAN
!= htons(-1)) {
301 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN
));
302 VLAN_to_string(CDPApplianceVLAN
, tmp
);
304 NetOurVLAN
= CDPApplianceVLAN
;
307 if (CDPNativeVLAN
!= htons(-1)) {
308 printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN
));
309 VLAN_to_string(CDPNativeVLAN
, tmp
);
310 setenv("nvlan", tmp
);
311 NetOurNativeVLAN
= CDPNativeVLAN
;
316 int do_cdp (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
322 printf("cdp failed; perhaps not a CISCO switch?\n");
333 "Perform CDP network configuration",
338 #if defined(CONFIG_CMD_SNTP)
339 int do_sntp (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
344 NetNtpServerIP
= getenv_IPaddr ("ntpserverip");
345 if (NetNtpServerIP
== 0) {
346 printf ("ntpserverip not set\n");
350 NetNtpServerIP
= string_to_ip(argv
[1]);
351 if (NetNtpServerIP
== 0) {
352 printf ("Bad NTP server IP address\n");
357 toff
= getenv ("timeoffset");
358 if (toff
== NULL
) NetTimeOffset
= 0;
359 else NetTimeOffset
= simple_strtol (toff
, NULL
, 10);
361 if (NetLoop(SNTP
) < 0) {
362 printf("SNTP failed: host %pI4 not responding\n",
372 "synchronize RTC via network",
377 #if defined(CONFIG_CMD_DNS)
378 int do_dns(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
381 return CMD_RET_USAGE
;
384 * We should check for a valid hostname:
385 * - Each label must be between 1 and 63 characters long
386 * - the entire hostname has a maximum of 255 characters
387 * - only the ASCII letters 'a' through 'z' (case-insensitive),
388 * the digits '0' through '9', and the hyphen
389 * - cannot begin or end with a hyphen
390 * - no other symbols, punctuation characters, or blank spaces are
392 * but hey - this is a minimalist implmentation, so only check length
393 * and let the name server deal with things.
395 if (strlen(argv
[1]) >= 255) {
396 printf("dns error: hostname too long\n");
400 NetDNSResolve
= argv
[1];
403 NetDNSenvvar
= argv
[2];
407 if (NetLoop(DNS
) < 0) {
408 printf("dns lookup of %s failed, check setup\n", argv
[1]);
417 "lookup the IP of a hostname",
421 #endif /* CONFIG_CMD_DNS */