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 #ifdef CONFIG_CMD_BOOTP
18 static int do_bootp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
20 return netboot_common(BOOTP, cmdtp, argc, argv);
24 bootp, 3, 1, do_bootp,
25 "boot image via network using BOOTP/TFTP protocol",
26 "[loadAddress] [[hostIPaddr:]bootfilename]"
30 #ifdef CONFIG_CMD_TFTPBOOT
31 int do_tftpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
35 bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
36 ret = netboot_common(TFTPGET, cmdtp, argc, argv);
37 bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
42 tftpboot, 3, 1, do_tftpb,
43 "boot image via network using TFTP protocol",
44 "[loadAddress] [[hostIPaddr:]bootfilename]"
48 #ifdef CONFIG_CMD_TFTPPUT
49 static int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
51 return netboot_common(TFTPPUT, cmdtp, argc, argv);
55 tftpput, 4, 1, do_tftpput,
56 "TFTP put command, for uploading files to a server",
57 "Address Size [[hostIPaddr:]filename]"
61 #ifdef CONFIG_CMD_TFTPSRV
62 static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
64 return netboot_common(TFTPSRV, cmdtp, argc, argv);
68 tftpsrv, 2, 1, do_tftpsrv,
69 "act as a TFTP server and boot the first received file",
71 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
72 "The transfer is aborted if a transfer has not been started after\n"
73 "about 50 seconds or if Ctrl-C is pressed."
78 #ifdef CONFIG_CMD_RARP
79 int do_rarpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
81 return netboot_common(RARP, cmdtp, argc, argv);
85 rarpboot, 3, 1, do_rarpb,
86 "boot image via network using RARP/TFTP protocol",
87 "[loadAddress] [[hostIPaddr:]bootfilename]"
91 #if defined(CONFIG_CMD_DHCP)
92 static int do_dhcp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
94 return netboot_common(DHCP, cmdtp, argc, argv);
99 "boot image via network using DHCP/TFTP protocol",
100 "[loadAddress] [[hostIPaddr:]bootfilename]"
104 #if defined(CONFIG_CMD_NFS)
105 static int do_nfs(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
107 return netboot_common(NFS, cmdtp, argc, argv);
112 "boot image via network using NFS protocol",
113 "[loadAddress] [[hostIPaddr:]bootfilename]"
117 static void netboot_update_env(void)
121 if (net_gateway.s_addr) {
122 ip_to_string(net_gateway, tmp);
123 env_set("gatewayip", tmp);
126 if (net_netmask.s_addr) {
127 ip_to_string(net_netmask, tmp);
128 env_set("netmask", tmp);
132 env_set("hostname", net_hostname);
134 if (net_root_path[0])
135 env_set("rootpath", net_root_path);
138 ip_to_string(net_ip, tmp);
139 env_set("ipaddr", tmp);
141 #if !defined(CONFIG_BOOTP_SERVERIP)
143 * Only attempt to change serverip if net/bootp.c:store_net_params()
146 if (net_server_ip.s_addr) {
147 ip_to_string(net_server_ip, tmp);
148 env_set("serverip", tmp);
151 if (net_dns_server.s_addr) {
152 ip_to_string(net_dns_server, tmp);
153 env_set("dnsip", tmp);
155 #if defined(CONFIG_BOOTP_DNS2)
156 if (net_dns_server2.s_addr) {
157 ip_to_string(net_dns_server2, tmp);
158 env_set("dnsip2", tmp);
161 if (net_nis_domain[0])
162 env_set("domain", net_nis_domain);
164 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
165 if (net_ntp_time_offset) {
166 sprintf(tmp, "%d", net_ntp_time_offset);
167 env_set("timeoffset", tmp);
170 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
171 if (net_ntp_server.s_addr) {
172 ip_to_string(net_ntp_server, tmp);
173 env_set("ntpserverip", tmp);
178 static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
187 /* pre-set load_addr */
188 s = env_get("loadaddr");
190 load_addr = simple_strtoul(s, NULL, 16);
197 * Only one arg - accept two forms:
198 * Just load address, or just boot file name. The latter
199 * form must be written in a format which can not be
200 * mis-interpreted as a valid number.
202 addr = simple_strtoul(argv[1], &end, 16);
203 if (end == (argv[1] + strlen(argv[1])))
206 copy_filename(net_boot_file_name, argv[1],
207 sizeof(net_boot_file_name));
211 load_addr = simple_strtoul(argv[1], NULL, 16);
212 copy_filename(net_boot_file_name, argv[2],
213 sizeof(net_boot_file_name));
217 #ifdef CONFIG_CMD_TFTPPUT
219 if (strict_strtoul(argv[1], 16, &save_addr) < 0 ||
220 strict_strtoul(argv[2], 16, &save_size) < 0) {
221 printf("Invalid address/size\n");
222 return CMD_RET_USAGE;
224 copy_filename(net_boot_file_name, argv[3],
225 sizeof(net_boot_file_name));
229 bootstage_error(BOOTSTAGE_ID_NET_START);
230 return CMD_RET_USAGE;
232 bootstage_mark(BOOTSTAGE_ID_NET_START);
234 size = net_loop(proto);
236 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
237 return CMD_RET_FAILURE;
239 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
241 /* net_loop ok, update environment */
242 netboot_update_env();
244 /* done if no file was loaded (no errors though) */
246 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
247 return CMD_RET_SUCCESS;
250 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
252 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
254 if (rcode == CMD_RET_SUCCESS)
255 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
257 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
261 #if defined(CONFIG_CMD_PING)
262 static int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
265 return CMD_RET_USAGE;
267 net_ping_ip = string_to_ip(argv[1]);
268 if (net_ping_ip.s_addr == 0)
269 return CMD_RET_USAGE;
271 if (net_loop(PING) < 0) {
272 printf("ping failed; host %s is not alive\n", argv[1]);
273 return CMD_RET_FAILURE;
276 printf("host %s is alive\n", argv[1]);
278 return CMD_RET_SUCCESS;
283 "send ICMP ECHO_REQUEST to network host",
288 #if defined(CONFIG_CMD_CDP)
290 static void cdp_update_env(void)
294 if (cdp_appliance_vlan != htons(-1)) {
295 printf("CDP offered appliance VLAN %d\n",
296 ntohs(cdp_appliance_vlan));
297 vlan_to_string(cdp_appliance_vlan, tmp);
298 env_set("vlan", tmp);
299 net_our_vlan = cdp_appliance_vlan;
302 if (cdp_native_vlan != htons(-1)) {
303 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
304 vlan_to_string(cdp_native_vlan, tmp);
305 env_set("nvlan", tmp);
306 net_native_vlan = cdp_native_vlan;
310 int do_cdp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
316 printf("cdp failed; perhaps not a CISCO switch?\n");
317 return CMD_RET_FAILURE;
322 return CMD_RET_SUCCESS;
327 "Perform CDP network configuration",
332 #if defined(CONFIG_CMD_SNTP)
333 int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
338 net_ntp_server = env_get_ip("ntpserverip");
339 if (net_ntp_server.s_addr == 0) {
340 printf("ntpserverip not set\n");
341 return CMD_RET_FAILURE;
344 net_ntp_server = string_to_ip(argv[1]);
345 if (net_ntp_server.s_addr == 0) {
346 printf("Bad NTP server IP address\n");
347 return CMD_RET_FAILURE;
351 toff = env_get("timeoffset");
353 net_ntp_time_offset = 0;
355 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
357 if (net_loop(SNTP) < 0) {
358 printf("SNTP failed: host %pI4 not responding\n",
360 return CMD_RET_FAILURE;
363 return CMD_RET_SUCCESS;
368 "synchronize RTC via network",
373 #if defined(CONFIG_CMD_DNS)
374 int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
377 return CMD_RET_USAGE;
380 * We should check for a valid hostname:
381 * - Each label must be between 1 and 63 characters long
382 * - the entire hostname has a maximum of 255 characters
383 * - only the ASCII letters 'a' through 'z' (case-insensitive),
384 * the digits '0' through '9', and the hyphen
385 * - cannot begin or end with a hyphen
386 * - no other symbols, punctuation characters, or blank spaces are
388 * but hey - this is a minimalist implmentation, so only check length
389 * and let the name server deal with things.
391 if (strlen(argv[1]) >= 255) {
392 printf("dns error: hostname too long\n");
393 return CMD_RET_FAILURE;
396 net_dns_resolve = argv[1];
399 net_dns_env_var = argv[2];
401 net_dns_env_var = NULL;
403 if (net_loop(DNS) < 0) {
404 printf("dns lookup of %s failed, check setup\n", argv[1]);
405 return CMD_RET_FAILURE;
408 return CMD_RET_SUCCESS;
413 "lookup the IP of a hostname",
417 #endif /* CONFIG_CMD_DNS */
419 #if defined(CONFIG_CMD_LINK_LOCAL)
420 static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc,
425 if (net_loop(LINKLOCAL) < 0)
426 return CMD_RET_FAILURE;
428 net_gateway.s_addr = 0;
429 ip_to_string(net_gateway, tmp);
430 env_set("gatewayip", tmp);
432 ip_to_string(net_netmask, tmp);
433 env_set("netmask", tmp);
435 ip_to_string(net_ip, tmp);
436 env_set("ipaddr", tmp);
437 env_set("llipaddr", tmp); /* store this for next time */
439 return CMD_RET_SUCCESS;
443 linklocal, 1, 1, do_link_local,
444 "acquire a network IP address using the link-local protocol",
448 #endif /* CONFIG_CMD_LINK_LOCAL */