3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
15 static int netboot_common(enum proto_t
, cmd_tbl_t
*, int, char * const []);
17 static int do_bootp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
19 return netboot_common(BOOTP
, cmdtp
, argc
, argv
);
23 bootp
, 3, 1, do_bootp
,
24 "boot image via network using BOOTP/TFTP protocol",
25 "[loadAddress] [[hostIPaddr:]bootfilename]"
28 int do_tftpb(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
32 bootstage_mark_name(BOOTSTAGE_KERNELREAD_START
, "tftp_start");
33 ret
= netboot_common(TFTPGET
, cmdtp
, argc
, argv
);
34 bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP
, "tftp_done");
39 tftpboot
, 3, 1, do_tftpb
,
40 "boot image via network using TFTP protocol",
41 "[loadAddress] [[hostIPaddr:]bootfilename]"
44 #ifdef CONFIG_CMD_TFTPPUT
45 int do_tftpput(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
47 return netboot_common(TFTPPUT
, cmdtp
, argc
, argv
);
51 tftpput
, 4, 1, do_tftpput
,
52 "TFTP put command, for uploading files to a server",
53 "Address Size [[hostIPaddr:]filename]"
57 #ifdef CONFIG_CMD_TFTPSRV
58 static int do_tftpsrv(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *const argv
[])
60 return netboot_common(TFTPSRV
, cmdtp
, argc
, argv
);
64 tftpsrv
, 2, 1, do_tftpsrv
,
65 "act as a TFTP server and boot the first received file",
67 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
68 "The transfer is aborted if a transfer has not been started after\n"
69 "about 50 seconds or if Ctrl-C is pressed."
74 #ifdef CONFIG_CMD_RARP
75 int do_rarpb(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
77 return netboot_common(RARP
, cmdtp
, argc
, argv
);
81 rarpboot
, 3, 1, do_rarpb
,
82 "boot image via network using RARP/TFTP protocol",
83 "[loadAddress] [[hostIPaddr:]bootfilename]"
87 #if defined(CONFIG_CMD_DHCP)
88 static int do_dhcp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
90 return netboot_common(DHCP
, cmdtp
, argc
, argv
);
95 "boot image via network using DHCP/TFTP protocol",
96 "[loadAddress] [[hostIPaddr:]bootfilename]"
100 #if defined(CONFIG_CMD_NFS)
101 static int do_nfs(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
103 return netboot_common(NFS
, cmdtp
, argc
, argv
);
108 "boot image via network using NFS protocol",
109 "[loadAddress] [[hostIPaddr:]bootfilename]"
113 static void netboot_update_env(void)
117 if (net_gateway
.s_addr
) {
118 ip_to_string(net_gateway
, tmp
);
119 setenv("gatewayip", tmp
);
122 if (net_netmask
.s_addr
) {
123 ip_to_string(net_netmask
, tmp
);
124 setenv("netmask", tmp
);
128 setenv("hostname", net_hostname
);
130 if (net_root_path
[0])
131 setenv("rootpath", net_root_path
);
134 ip_to_string(net_ip
, tmp
);
135 setenv("ipaddr", tmp
);
137 #if !defined(CONFIG_BOOTP_SERVERIP)
139 * Only attempt to change serverip if net/bootp.c:BootpCopyNetParams()
142 if (net_server_ip
.s_addr
) {
143 ip_to_string(net_server_ip
, tmp
);
144 setenv("serverip", tmp
);
147 if (net_dns_server
.s_addr
) {
148 ip_to_string(net_dns_server
, tmp
);
149 setenv("dnsip", tmp
);
151 #if defined(CONFIG_BOOTP_DNS2)
152 if (net_dns_server2
.s_addr
) {
153 ip_to_string(net_dns_server2
, tmp
);
154 setenv("dnsip2", tmp
);
157 if (net_nis_domain
[0])
158 setenv("domain", net_nis_domain
);
160 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
161 if (net_ntp_time_offset
) {
162 sprintf(tmp
, "%d", net_ntp_time_offset
);
163 setenv("timeoffset", tmp
);
166 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
167 if (net_ntp_server
.s_addr
) {
168 ip_to_string(net_ntp_server
, tmp
);
169 setenv("ntpserverip", tmp
);
174 static int netboot_common(enum proto_t proto
, cmd_tbl_t
*cmdtp
, int argc
,
183 /* pre-set load_addr */
184 s
= getenv("loadaddr");
186 load_addr
= simple_strtoul(s
, NULL
, 16);
193 * Only one arg - accept two forms:
194 * Just load address, or just boot file name. The latter
195 * form must be written in a format which can not be
196 * mis-interpreted as a valid number.
198 addr
= simple_strtoul(argv
[1], &end
, 16);
199 if (end
== (argv
[1] + strlen(argv
[1])))
202 copy_filename(net_boot_file_name
, argv
[1],
203 sizeof(net_boot_file_name
));
207 load_addr
= simple_strtoul(argv
[1], NULL
, 16);
208 copy_filename(net_boot_file_name
, argv
[2],
209 sizeof(net_boot_file_name
));
213 #ifdef CONFIG_CMD_TFTPPUT
215 if (strict_strtoul(argv
[1], 16, &save_addr
) < 0 ||
216 strict_strtoul(argv
[2], 16, &save_size
) < 0) {
217 printf("Invalid address/size\n");
218 return CMD_RET_USAGE
;
220 copy_filename(net_boot_file_name
, argv
[3],
221 sizeof(net_boot_file_name
));
225 bootstage_error(BOOTSTAGE_ID_NET_START
);
226 return CMD_RET_USAGE
;
228 bootstage_mark(BOOTSTAGE_ID_NET_START
);
230 size
= net_loop(proto
);
232 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK
);
233 return CMD_RET_FAILURE
;
235 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK
);
237 /* net_loop ok, update environment */
238 netboot_update_env();
240 /* done if no file was loaded (no errors though) */
242 bootstage_error(BOOTSTAGE_ID_NET_LOADED
);
243 return CMD_RET_SUCCESS
;
247 flush_cache(load_addr
, size
);
249 bootstage_mark(BOOTSTAGE_ID_NET_LOADED
);
251 rcode
= bootm_maybe_autostart(cmdtp
, argv
[0]);
253 if (rcode
== CMD_RET_SUCCESS
)
254 bootstage_mark(BOOTSTAGE_ID_NET_DONE
);
256 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR
);
260 #if defined(CONFIG_CMD_PING)
261 static int do_ping(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
264 return CMD_RET_USAGE
;
266 net_ping_ip
= string_to_ip(argv
[1]);
267 if (net_ping_ip
.s_addr
== 0)
268 return CMD_RET_USAGE
;
270 if (net_loop(PING
) < 0) {
271 printf("ping failed; host %s is not alive\n", argv
[1]);
272 return CMD_RET_FAILURE
;
275 printf("host %s is alive\n", argv
[1]);
277 return CMD_RET_SUCCESS
;
282 "send ICMP ECHO_REQUEST to network host",
287 #if defined(CONFIG_CMD_CDP)
289 static void cdp_update_env(void)
293 if (cdp_appliance_vlan
!= htons(-1)) {
294 printf("CDP offered appliance VLAN %d\n",
295 ntohs(cdp_appliance_vlan
));
296 vlan_to_string(cdp_appliance_vlan
, tmp
);
298 net_our_vlan
= cdp_appliance_vlan
;
301 if (cdp_native_vlan
!= htons(-1)) {
302 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan
));
303 vlan_to_string(cdp_native_vlan
, tmp
);
304 setenv("nvlan", tmp
);
305 net_native_vlan
= cdp_native_vlan
;
309 int do_cdp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
315 printf("cdp failed; perhaps not a CISCO switch?\n");
316 return CMD_RET_FAILURE
;
321 return CMD_RET_SUCCESS
;
326 "Perform CDP network configuration",
331 #if defined(CONFIG_CMD_SNTP)
332 int do_sntp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
337 net_ntp_server
= getenv_ip("ntpserverip");
338 if (net_ntp_server
.s_addr
== 0) {
339 printf("ntpserverip not set\n");
340 return CMD_RET_FAILURE
;
343 net_ntp_server
= string_to_ip(argv
[1]);
344 if (net_ntp_server
.s_addr
== 0) {
345 printf("Bad NTP server IP address\n");
346 return CMD_RET_FAILURE
;
350 toff
= getenv("timeoffset");
352 net_ntp_time_offset
= 0;
354 net_ntp_time_offset
= simple_strtol(toff
, NULL
, 10);
356 if (net_loop(SNTP
) < 0) {
357 printf("SNTP failed: host %pI4 not responding\n",
359 return CMD_RET_FAILURE
;
362 return CMD_RET_SUCCESS
;
367 "synchronize RTC via network",
372 #if defined(CONFIG_CMD_DNS)
373 int do_dns(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
376 return CMD_RET_USAGE
;
379 * We should check for a valid hostname:
380 * - Each label must be between 1 and 63 characters long
381 * - the entire hostname has a maximum of 255 characters
382 * - only the ASCII letters 'a' through 'z' (case-insensitive),
383 * the digits '0' through '9', and the hyphen
384 * - cannot begin or end with a hyphen
385 * - no other symbols, punctuation characters, or blank spaces are
387 * but hey - this is a minimalist implmentation, so only check length
388 * and let the name server deal with things.
390 if (strlen(argv
[1]) >= 255) {
391 printf("dns error: hostname too long\n");
392 return CMD_RET_FAILURE
;
395 net_dns_resolve
= argv
[1];
398 net_dns_env_var
= argv
[2];
400 net_dns_env_var
= NULL
;
402 if (net_loop(DNS
) < 0) {
403 printf("dns lookup of %s failed, check setup\n", argv
[1]);
404 return CMD_RET_FAILURE
;
407 return CMD_RET_SUCCESS
;
412 "lookup the IP of a hostname",
416 #endif /* CONFIG_CMD_DNS */
418 #if defined(CONFIG_CMD_LINK_LOCAL)
419 static int do_link_local(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
424 if (net_loop(LINKLOCAL
) < 0)
425 return CMD_RET_FAILURE
;
427 net_gateway
.s_addr
= 0;
428 ip_to_string(net_gateway
, tmp
);
429 setenv("gatewayip", tmp
);
431 ip_to_string(net_netmask
, tmp
);
432 setenv("netmask", tmp
);
434 ip_to_string(net_ip
, tmp
);
435 setenv("ipaddr", tmp
);
436 setenv("llipaddr", tmp
); /* store this for next time */
438 return CMD_RET_SUCCESS
;
442 linklocal
, 1, 1, do_link_local
,
443 "acquire a network IP address using the link-local protocol",
447 #endif /* CONFIG_CMD_LINK_LOCAL */