1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
16 static int netboot_common(enum proto_t, cmd_tbl_t *, int, char * const []);
18 #ifdef CONFIG_CMD_BOOTP
19 static int do_bootp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
21 return netboot_common(BOOTP, cmdtp, argc, argv);
25 bootp, 3, 1, do_bootp,
26 "boot image via network using BOOTP/TFTP protocol",
27 "[loadAddress] [[hostIPaddr:]bootfilename]"
31 #ifdef CONFIG_CMD_TFTPBOOT
32 int do_tftpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
36 bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
37 ret = netboot_common(TFTPGET, cmdtp, argc, argv);
38 bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
43 tftpboot, 3, 1, do_tftpb,
44 "boot image via network using TFTP protocol",
45 "[loadAddress] [[hostIPaddr:]bootfilename]"
49 #ifdef CONFIG_CMD_TFTPPUT
50 static int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
52 return netboot_common(TFTPPUT, cmdtp, argc, argv);
56 tftpput, 4, 1, do_tftpput,
57 "TFTP put command, for uploading files to a server",
58 "Address Size [[hostIPaddr:]filename]"
62 #ifdef CONFIG_CMD_TFTPSRV
63 static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
65 return netboot_common(TFTPSRV, cmdtp, argc, argv);
69 tftpsrv, 2, 1, do_tftpsrv,
70 "act as a TFTP server and boot the first received file",
72 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
73 "The transfer is aborted if a transfer has not been started after\n"
74 "about 50 seconds or if Ctrl-C is pressed."
79 #ifdef CONFIG_CMD_RARP
80 int do_rarpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
82 return netboot_common(RARP, cmdtp, argc, argv);
86 rarpboot, 3, 1, do_rarpb,
87 "boot image via network using RARP/TFTP protocol",
88 "[loadAddress] [[hostIPaddr:]bootfilename]"
92 #if defined(CONFIG_CMD_DHCP)
93 static int do_dhcp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
95 return netboot_common(DHCP, cmdtp, argc, argv);
100 "boot image via network using DHCP/TFTP protocol",
101 "[loadAddress] [[hostIPaddr:]bootfilename]"
105 #if defined(CONFIG_CMD_NFS)
106 static int do_nfs(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
108 return netboot_common(NFS, cmdtp, argc, argv);
113 "boot image via network using NFS protocol",
114 "[loadAddress] [[hostIPaddr:]bootfilename]"
118 static void netboot_update_env(void)
122 if (net_gateway.s_addr) {
123 ip_to_string(net_gateway, tmp);
124 env_set("gatewayip", tmp);
127 if (net_netmask.s_addr) {
128 ip_to_string(net_netmask, tmp);
129 env_set("netmask", tmp);
133 env_set("hostname", net_hostname);
135 if (net_root_path[0])
136 env_set("rootpath", net_root_path);
139 ip_to_string(net_ip, tmp);
140 env_set("ipaddr", tmp);
142 #if !defined(CONFIG_BOOTP_SERVERIP)
144 * Only attempt to change serverip if net/bootp.c:store_net_params()
147 if (net_server_ip.s_addr) {
148 ip_to_string(net_server_ip, tmp);
149 env_set("serverip", tmp);
152 if (net_dns_server.s_addr) {
153 ip_to_string(net_dns_server, tmp);
154 env_set("dnsip", tmp);
156 #if defined(CONFIG_BOOTP_DNS2)
157 if (net_dns_server2.s_addr) {
158 ip_to_string(net_dns_server2, tmp);
159 env_set("dnsip2", tmp);
162 if (net_nis_domain[0])
163 env_set("domain", net_nis_domain);
165 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
166 if (net_ntp_time_offset) {
167 sprintf(tmp, "%d", net_ntp_time_offset);
168 env_set("timeoffset", tmp);
171 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
172 if (net_ntp_server.s_addr) {
173 ip_to_string(net_ntp_server, tmp);
174 env_set("ntpserverip", tmp);
179 static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
188 net_boot_file_name_explicit = false;
190 /* pre-set image_load_addr */
191 s = env_get("loadaddr");
193 image_load_addr = simple_strtoul(s, NULL, 16);
197 /* refresh bootfile name from env */
198 copy_filename(net_boot_file_name, env_get("bootfile"),
199 sizeof(net_boot_file_name));
203 * Only one arg - accept two forms:
204 * Just load address, or just boot file name. The latter
205 * form must be written in a format which can not be
206 * mis-interpreted as a valid number.
208 addr = simple_strtoul(argv[1], &end, 16);
209 if (end == (argv[1] + strlen(argv[1]))) {
210 image_load_addr = addr;
211 /* refresh bootfile name from env */
212 copy_filename(net_boot_file_name, env_get("bootfile"),
213 sizeof(net_boot_file_name));
215 net_boot_file_name_explicit = true;
216 copy_filename(net_boot_file_name, argv[1],
217 sizeof(net_boot_file_name));
222 image_load_addr = simple_strtoul(argv[1], NULL, 16);
223 net_boot_file_name_explicit = true;
224 copy_filename(net_boot_file_name, argv[2],
225 sizeof(net_boot_file_name));
229 #ifdef CONFIG_CMD_TFTPPUT
231 if (strict_strtoul(argv[1], 16, &image_save_addr) < 0 ||
232 strict_strtoul(argv[2], 16, &image_save_size) < 0) {
233 printf("Invalid address/size\n");
234 return CMD_RET_USAGE;
236 net_boot_file_name_explicit = true;
237 copy_filename(net_boot_file_name, argv[3],
238 sizeof(net_boot_file_name));
242 bootstage_error(BOOTSTAGE_ID_NET_START);
243 return CMD_RET_USAGE;
245 bootstage_mark(BOOTSTAGE_ID_NET_START);
247 size = net_loop(proto);
249 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
250 return CMD_RET_FAILURE;
252 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
254 /* net_loop ok, update environment */
255 netboot_update_env();
257 /* done if no file was loaded (no errors though) */
259 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
260 return CMD_RET_SUCCESS;
263 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
265 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
267 if (rcode == CMD_RET_SUCCESS)
268 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
270 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
274 #if defined(CONFIG_CMD_PING)
275 static int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
278 return CMD_RET_USAGE;
280 net_ping_ip = string_to_ip(argv[1]);
281 if (net_ping_ip.s_addr == 0)
282 return CMD_RET_USAGE;
284 if (net_loop(PING) < 0) {
285 printf("ping failed; host %s is not alive\n", argv[1]);
286 return CMD_RET_FAILURE;
289 printf("host %s is alive\n", argv[1]);
291 return CMD_RET_SUCCESS;
296 "send ICMP ECHO_REQUEST to network host",
301 #if defined(CONFIG_CMD_CDP)
303 static void cdp_update_env(void)
307 if (cdp_appliance_vlan != htons(-1)) {
308 printf("CDP offered appliance VLAN %d\n",
309 ntohs(cdp_appliance_vlan));
310 vlan_to_string(cdp_appliance_vlan, tmp);
311 env_set("vlan", tmp);
312 net_our_vlan = cdp_appliance_vlan;
315 if (cdp_native_vlan != htons(-1)) {
316 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
317 vlan_to_string(cdp_native_vlan, tmp);
318 env_set("nvlan", tmp);
319 net_native_vlan = cdp_native_vlan;
323 int do_cdp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
329 printf("cdp failed; perhaps not a CISCO switch?\n");
330 return CMD_RET_FAILURE;
335 return CMD_RET_SUCCESS;
340 "Perform CDP network configuration",
345 #if defined(CONFIG_CMD_SNTP)
346 int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
351 net_ntp_server = env_get_ip("ntpserverip");
352 if (net_ntp_server.s_addr == 0) {
353 printf("ntpserverip not set\n");
354 return CMD_RET_FAILURE;
357 net_ntp_server = string_to_ip(argv[1]);
358 if (net_ntp_server.s_addr == 0) {
359 printf("Bad NTP server IP address\n");
360 return CMD_RET_FAILURE;
364 toff = env_get("timeoffset");
366 net_ntp_time_offset = 0;
368 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
370 if (net_loop(SNTP) < 0) {
371 printf("SNTP failed: host %pI4 not responding\n",
373 return CMD_RET_FAILURE;
376 return CMD_RET_SUCCESS;
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");
406 return CMD_RET_FAILURE;
409 net_dns_resolve = argv[1];
412 net_dns_env_var = argv[2];
414 net_dns_env_var = NULL;
416 if (net_loop(DNS) < 0) {
417 printf("dns lookup of %s failed, check setup\n", argv[1]);
418 return CMD_RET_FAILURE;
421 return CMD_RET_SUCCESS;
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 (net_loop(LINKLOCAL) < 0)
439 return CMD_RET_FAILURE;
441 net_gateway.s_addr = 0;
442 ip_to_string(net_gateway, tmp);
443 env_set("gatewayip", tmp);
445 ip_to_string(net_netmask, tmp);
446 env_set("netmask", tmp);
448 ip_to_string(net_ip, tmp);
449 env_set("ipaddr", tmp);
450 env_set("llipaddr", tmp); /* store this for next time */
452 return CMD_RET_SUCCESS;
456 linklocal, 1, 1, do_link_local,
457 "acquire a network IP address using the link-local protocol",
461 #endif /* CONFIG_CMD_LINK_LOCAL */