1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
11 #include <bootstage.h>
17 static int netboot_common(enum proto_t
, cmd_tbl_t
*, int, char * const []);
19 #ifdef CONFIG_CMD_BOOTP
20 static int do_bootp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
22 return netboot_common(BOOTP
, cmdtp
, argc
, argv
);
26 bootp
, 3, 1, do_bootp
,
27 "boot image via network using BOOTP/TFTP protocol",
28 "[loadAddress] [[hostIPaddr:]bootfilename]"
32 #ifdef CONFIG_CMD_TFTPBOOT
33 int do_tftpb(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
37 bootstage_mark_name(BOOTSTAGE_KERNELREAD_START
, "tftp_start");
38 ret
= netboot_common(TFTPGET
, cmdtp
, argc
, argv
);
39 bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP
, "tftp_done");
44 tftpboot
, 3, 1, do_tftpb
,
45 "boot image via network using TFTP protocol",
46 "[loadAddress] [[hostIPaddr:]bootfilename]"
50 #ifdef CONFIG_CMD_TFTPPUT
51 static int do_tftpput(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
53 return netboot_common(TFTPPUT
, cmdtp
, argc
, argv
);
57 tftpput
, 4, 1, do_tftpput
,
58 "TFTP put command, for uploading files to a server",
59 "Address Size [[hostIPaddr:]filename]"
63 #ifdef CONFIG_CMD_TFTPSRV
64 static int do_tftpsrv(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *const argv
[])
66 return netboot_common(TFTPSRV
, cmdtp
, argc
, argv
);
70 tftpsrv
, 2, 1, do_tftpsrv
,
71 "act as a TFTP server and boot the first received file",
73 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
74 "The transfer is aborted if a transfer has not been started after\n"
75 "about 50 seconds or if Ctrl-C is pressed."
80 #ifdef CONFIG_CMD_RARP
81 int do_rarpb(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
83 return netboot_common(RARP
, cmdtp
, argc
, argv
);
87 rarpboot
, 3, 1, do_rarpb
,
88 "boot image via network using RARP/TFTP protocol",
89 "[loadAddress] [[hostIPaddr:]bootfilename]"
93 #if defined(CONFIG_CMD_DHCP)
94 static int do_dhcp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
96 return netboot_common(DHCP
, cmdtp
, argc
, argv
);
101 "boot image via network using DHCP/TFTP protocol",
102 "[loadAddress] [[hostIPaddr:]bootfilename]"
106 #if defined(CONFIG_CMD_NFS)
107 static int do_nfs(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
109 return netboot_common(NFS
, cmdtp
, argc
, argv
);
114 "boot image via network using NFS protocol",
115 "[loadAddress] [[hostIPaddr:]bootfilename]"
119 static void netboot_update_env(void)
123 if (net_gateway
.s_addr
) {
124 ip_to_string(net_gateway
, tmp
);
125 env_set("gatewayip", tmp
);
128 if (net_netmask
.s_addr
) {
129 ip_to_string(net_netmask
, tmp
);
130 env_set("netmask", tmp
);
134 env_set("hostname", net_hostname
);
136 if (net_root_path
[0])
137 env_set("rootpath", net_root_path
);
140 ip_to_string(net_ip
, tmp
);
141 env_set("ipaddr", tmp
);
143 #if !defined(CONFIG_BOOTP_SERVERIP)
145 * Only attempt to change serverip if net/bootp.c:store_net_params()
148 if (net_server_ip
.s_addr
) {
149 ip_to_string(net_server_ip
, tmp
);
150 env_set("serverip", tmp
);
153 if (net_dns_server
.s_addr
) {
154 ip_to_string(net_dns_server
, tmp
);
155 env_set("dnsip", tmp
);
157 #if defined(CONFIG_BOOTP_DNS2)
158 if (net_dns_server2
.s_addr
) {
159 ip_to_string(net_dns_server2
, tmp
);
160 env_set("dnsip2", tmp
);
163 if (net_nis_domain
[0])
164 env_set("domain", net_nis_domain
);
166 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
167 if (net_ntp_time_offset
) {
168 sprintf(tmp
, "%d", net_ntp_time_offset
);
169 env_set("timeoffset", tmp
);
172 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
173 if (net_ntp_server
.s_addr
) {
174 ip_to_string(net_ntp_server
, tmp
);
175 env_set("ntpserverip", tmp
);
180 static int netboot_common(enum proto_t proto
, cmd_tbl_t
*cmdtp
, int argc
,
189 net_boot_file_name_explicit
= false;
191 /* pre-set image_load_addr */
192 s
= env_get("loadaddr");
194 image_load_addr
= simple_strtoul(s
, NULL
, 16);
198 /* refresh bootfile name from env */
199 copy_filename(net_boot_file_name
, env_get("bootfile"),
200 sizeof(net_boot_file_name
));
204 * Only one arg - accept two forms:
205 * Just load address, or just boot file name. The latter
206 * form must be written in a format which can not be
207 * mis-interpreted as a valid number.
209 addr
= simple_strtoul(argv
[1], &end
, 16);
210 if (end
== (argv
[1] + strlen(argv
[1]))) {
211 image_load_addr
= addr
;
212 /* refresh bootfile name from env */
213 copy_filename(net_boot_file_name
, env_get("bootfile"),
214 sizeof(net_boot_file_name
));
216 net_boot_file_name_explicit
= true;
217 copy_filename(net_boot_file_name
, argv
[1],
218 sizeof(net_boot_file_name
));
223 image_load_addr
= simple_strtoul(argv
[1], NULL
, 16);
224 net_boot_file_name_explicit
= true;
225 copy_filename(net_boot_file_name
, argv
[2],
226 sizeof(net_boot_file_name
));
230 #ifdef CONFIG_CMD_TFTPPUT
232 if (strict_strtoul(argv
[1], 16, &image_save_addr
) < 0 ||
233 strict_strtoul(argv
[2], 16, &image_save_size
) < 0) {
234 printf("Invalid address/size\n");
235 return CMD_RET_USAGE
;
237 net_boot_file_name_explicit
= true;
238 copy_filename(net_boot_file_name
, argv
[3],
239 sizeof(net_boot_file_name
));
243 bootstage_error(BOOTSTAGE_ID_NET_START
);
244 return CMD_RET_USAGE
;
246 bootstage_mark(BOOTSTAGE_ID_NET_START
);
248 size
= net_loop(proto
);
250 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK
);
251 return CMD_RET_FAILURE
;
253 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK
);
255 /* net_loop ok, update environment */
256 netboot_update_env();
258 /* done if no file was loaded (no errors though) */
260 bootstage_error(BOOTSTAGE_ID_NET_LOADED
);
261 return CMD_RET_SUCCESS
;
264 bootstage_mark(BOOTSTAGE_ID_NET_LOADED
);
266 rcode
= bootm_maybe_autostart(cmdtp
, argv
[0]);
268 if (rcode
== CMD_RET_SUCCESS
)
269 bootstage_mark(BOOTSTAGE_ID_NET_DONE
);
271 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR
);
275 #if defined(CONFIG_CMD_PING)
276 static int do_ping(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
279 return CMD_RET_USAGE
;
281 net_ping_ip
= string_to_ip(argv
[1]);
282 if (net_ping_ip
.s_addr
== 0)
283 return CMD_RET_USAGE
;
285 if (net_loop(PING
) < 0) {
286 printf("ping failed; host %s is not alive\n", argv
[1]);
287 return CMD_RET_FAILURE
;
290 printf("host %s is alive\n", argv
[1]);
292 return CMD_RET_SUCCESS
;
297 "send ICMP ECHO_REQUEST to network host",
302 #if defined(CONFIG_CMD_CDP)
304 static void cdp_update_env(void)
308 if (cdp_appliance_vlan
!= htons(-1)) {
309 printf("CDP offered appliance VLAN %d\n",
310 ntohs(cdp_appliance_vlan
));
311 vlan_to_string(cdp_appliance_vlan
, tmp
);
312 env_set("vlan", tmp
);
313 net_our_vlan
= cdp_appliance_vlan
;
316 if (cdp_native_vlan
!= htons(-1)) {
317 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan
));
318 vlan_to_string(cdp_native_vlan
, tmp
);
319 env_set("nvlan", tmp
);
320 net_native_vlan
= cdp_native_vlan
;
324 int do_cdp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
330 printf("cdp failed; perhaps not a CISCO switch?\n");
331 return CMD_RET_FAILURE
;
336 return CMD_RET_SUCCESS
;
341 "Perform CDP network configuration",
346 #if defined(CONFIG_CMD_SNTP)
347 int do_sntp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
352 net_ntp_server
= env_get_ip("ntpserverip");
353 if (net_ntp_server
.s_addr
== 0) {
354 printf("ntpserverip not set\n");
355 return CMD_RET_FAILURE
;
358 net_ntp_server
= string_to_ip(argv
[1]);
359 if (net_ntp_server
.s_addr
== 0) {
360 printf("Bad NTP server IP address\n");
361 return CMD_RET_FAILURE
;
365 toff
= env_get("timeoffset");
367 net_ntp_time_offset
= 0;
369 net_ntp_time_offset
= simple_strtol(toff
, NULL
, 10);
371 if (net_loop(SNTP
) < 0) {
372 printf("SNTP failed: host %pI4 not responding\n",
374 return CMD_RET_FAILURE
;
377 return CMD_RET_SUCCESS
;
382 "synchronize RTC via network",
387 #if defined(CONFIG_CMD_DNS)
388 int do_dns(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
391 return CMD_RET_USAGE
;
394 * We should check for a valid hostname:
395 * - Each label must be between 1 and 63 characters long
396 * - the entire hostname has a maximum of 255 characters
397 * - only the ASCII letters 'a' through 'z' (case-insensitive),
398 * the digits '0' through '9', and the hyphen
399 * - cannot begin or end with a hyphen
400 * - no other symbols, punctuation characters, or blank spaces are
402 * but hey - this is a minimalist implmentation, so only check length
403 * and let the name server deal with things.
405 if (strlen(argv
[1]) >= 255) {
406 printf("dns error: hostname too long\n");
407 return CMD_RET_FAILURE
;
410 net_dns_resolve
= argv
[1];
413 net_dns_env_var
= argv
[2];
415 net_dns_env_var
= NULL
;
417 if (net_loop(DNS
) < 0) {
418 printf("dns lookup of %s failed, check setup\n", argv
[1]);
419 return CMD_RET_FAILURE
;
422 return CMD_RET_SUCCESS
;
427 "lookup the IP of a hostname",
431 #endif /* CONFIG_CMD_DNS */
433 #if defined(CONFIG_CMD_LINK_LOCAL)
434 static int do_link_local(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
439 if (net_loop(LINKLOCAL
) < 0)
440 return CMD_RET_FAILURE
;
442 net_gateway
.s_addr
= 0;
443 ip_to_string(net_gateway
, tmp
);
444 env_set("gatewayip", tmp
);
446 ip_to_string(net_netmask
, tmp
);
447 env_set("netmask", tmp
);
449 ip_to_string(net_ip
, tmp
);
450 env_set("ipaddr", tmp
);
451 env_set("llipaddr", tmp
); /* store this for next time */
453 return CMD_RET_SUCCESS
;
457 linklocal
, 1, 1, do_link_local
,
458 "acquire a network IP address using the link-local protocol",
462 #endif /* CONFIG_CMD_LINK_LOCAL */