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, struct cmd_tbl *, int, char * const []);
19 #ifdef CONFIG_CMD_BOOTP
20 static int do_bootp(struct cmd_tbl *cmdtp, int flag, int argc,
23 return netboot_common(BOOTP, cmdtp, argc, argv);
27 bootp, 3, 1, do_bootp,
28 "boot image via network using BOOTP/TFTP protocol",
29 "[loadAddress] [[hostIPaddr:]bootfilename]"
33 #ifdef CONFIG_CMD_TFTPBOOT
34 int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
38 bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
39 ret = netboot_common(TFTPGET, cmdtp, argc, argv);
40 bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
45 tftpboot, 3, 1, do_tftpb,
46 "boot image via network using TFTP protocol",
47 "[loadAddress] [[hostIPaddr:]bootfilename]"
51 #ifdef CONFIG_CMD_TFTPPUT
52 static int do_tftpput(struct cmd_tbl *cmdtp, int flag, int argc,
55 return netboot_common(TFTPPUT, cmdtp, argc, argv);
59 tftpput, 4, 1, do_tftpput,
60 "TFTP put command, for uploading files to a server",
61 "Address Size [[hostIPaddr:]filename]"
65 #ifdef CONFIG_CMD_TFTPSRV
66 static int do_tftpsrv(struct cmd_tbl *cmdtp, int flag, int argc,
69 return netboot_common(TFTPSRV, cmdtp, argc, argv);
73 tftpsrv, 2, 1, do_tftpsrv,
74 "act as a TFTP server and boot the first received file",
76 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
77 "The transfer is aborted if a transfer has not been started after\n"
78 "about 50 seconds or if Ctrl-C is pressed."
83 #ifdef CONFIG_CMD_RARP
84 int do_rarpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
86 return netboot_common(RARP, cmdtp, argc, argv);
90 rarpboot, 3, 1, do_rarpb,
91 "boot image via network using RARP/TFTP protocol",
92 "[loadAddress] [[hostIPaddr:]bootfilename]"
96 #if defined(CONFIG_CMD_DHCP)
97 static int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc,
100 return netboot_common(DHCP, cmdtp, argc, argv);
105 "boot image via network using DHCP/TFTP protocol",
106 "[loadAddress] [[hostIPaddr:]bootfilename]"
110 #if defined(CONFIG_CMD_NFS)
111 static int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc,
114 return netboot_common(NFS, cmdtp, argc, argv);
119 "boot image via network using NFS protocol",
120 "[loadAddress] [[hostIPaddr:]bootfilename]"
124 static void netboot_update_env(void)
128 if (net_gateway.s_addr) {
129 ip_to_string(net_gateway, tmp);
130 env_set("gatewayip", tmp);
133 if (net_netmask.s_addr) {
134 ip_to_string(net_netmask, tmp);
135 env_set("netmask", tmp);
139 env_set("hostname", net_hostname);
141 if (net_root_path[0])
142 env_set("rootpath", net_root_path);
145 ip_to_string(net_ip, tmp);
146 env_set("ipaddr", tmp);
148 #if !defined(CONFIG_BOOTP_SERVERIP)
150 * Only attempt to change serverip if net/bootp.c:store_net_params()
153 if (net_server_ip.s_addr) {
154 ip_to_string(net_server_ip, tmp);
155 env_set("serverip", tmp);
158 if (net_dns_server.s_addr) {
159 ip_to_string(net_dns_server, tmp);
160 env_set("dnsip", tmp);
162 #if defined(CONFIG_BOOTP_DNS2)
163 if (net_dns_server2.s_addr) {
164 ip_to_string(net_dns_server2, tmp);
165 env_set("dnsip2", tmp);
168 if (net_nis_domain[0])
169 env_set("domain", net_nis_domain);
171 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
172 if (net_ntp_time_offset) {
173 sprintf(tmp, "%d", net_ntp_time_offset);
174 env_set("timeoffset", tmp);
177 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
178 if (net_ntp_server.s_addr) {
179 ip_to_string(net_ntp_server, tmp);
180 env_set("ntpserverip", tmp);
185 static int netboot_common(enum proto_t proto, struct cmd_tbl *cmdtp, int argc,
194 net_boot_file_name_explicit = false;
196 /* pre-set image_load_addr */
197 s = env_get("loadaddr");
199 image_load_addr = simple_strtoul(s, NULL, 16);
203 /* refresh bootfile name from env */
204 copy_filename(net_boot_file_name, env_get("bootfile"),
205 sizeof(net_boot_file_name));
209 * Only one arg - accept two forms:
210 * Just load address, or just boot file name. The latter
211 * form must be written in a format which can not be
212 * mis-interpreted as a valid number.
214 addr = simple_strtoul(argv[1], &end, 16);
215 if (end == (argv[1] + strlen(argv[1]))) {
216 image_load_addr = addr;
217 /* refresh bootfile name from env */
218 copy_filename(net_boot_file_name, env_get("bootfile"),
219 sizeof(net_boot_file_name));
221 net_boot_file_name_explicit = true;
222 copy_filename(net_boot_file_name, argv[1],
223 sizeof(net_boot_file_name));
228 image_load_addr = simple_strtoul(argv[1], NULL, 16);
229 net_boot_file_name_explicit = true;
230 copy_filename(net_boot_file_name, argv[2],
231 sizeof(net_boot_file_name));
235 #ifdef CONFIG_CMD_TFTPPUT
237 if (strict_strtoul(argv[1], 16, &image_save_addr) < 0 ||
238 strict_strtoul(argv[2], 16, &image_save_size) < 0) {
239 printf("Invalid address/size\n");
240 return CMD_RET_USAGE;
242 net_boot_file_name_explicit = true;
243 copy_filename(net_boot_file_name, argv[3],
244 sizeof(net_boot_file_name));
248 bootstage_error(BOOTSTAGE_ID_NET_START);
249 return CMD_RET_USAGE;
251 bootstage_mark(BOOTSTAGE_ID_NET_START);
253 size = net_loop(proto);
255 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
256 return CMD_RET_FAILURE;
258 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
260 /* net_loop ok, update environment */
261 netboot_update_env();
263 /* done if no file was loaded (no errors though) */
265 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
266 return CMD_RET_SUCCESS;
269 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
271 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
273 if (rcode == CMD_RET_SUCCESS)
274 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
276 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
280 #if defined(CONFIG_CMD_PING)
281 static int do_ping(struct cmd_tbl *cmdtp, int flag, int argc,
285 return CMD_RET_USAGE;
287 net_ping_ip = string_to_ip(argv[1]);
288 if (net_ping_ip.s_addr == 0)
289 return CMD_RET_USAGE;
291 if (net_loop(PING) < 0) {
292 printf("ping failed; host %s is not alive\n", argv[1]);
293 return CMD_RET_FAILURE;
296 printf("host %s is alive\n", argv[1]);
298 return CMD_RET_SUCCESS;
303 "send ICMP ECHO_REQUEST to network host",
308 #if defined(CONFIG_CMD_CDP)
310 static void cdp_update_env(void)
314 if (cdp_appliance_vlan != htons(-1)) {
315 printf("CDP offered appliance VLAN %d\n",
316 ntohs(cdp_appliance_vlan));
317 vlan_to_string(cdp_appliance_vlan, tmp);
318 env_set("vlan", tmp);
319 net_our_vlan = cdp_appliance_vlan;
322 if (cdp_native_vlan != htons(-1)) {
323 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
324 vlan_to_string(cdp_native_vlan, tmp);
325 env_set("nvlan", tmp);
326 net_native_vlan = cdp_native_vlan;
330 int do_cdp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
336 printf("cdp failed; perhaps not a CISCO switch?\n");
337 return CMD_RET_FAILURE;
342 return CMD_RET_SUCCESS;
347 "Perform CDP network configuration",
352 #if defined(CONFIG_CMD_SNTP)
353 int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
358 net_ntp_server = env_get_ip("ntpserverip");
359 if (net_ntp_server.s_addr == 0) {
360 printf("ntpserverip not set\n");
361 return CMD_RET_FAILURE;
364 net_ntp_server = string_to_ip(argv[1]);
365 if (net_ntp_server.s_addr == 0) {
366 printf("Bad NTP server IP address\n");
367 return CMD_RET_FAILURE;
371 toff = env_get("timeoffset");
373 net_ntp_time_offset = 0;
375 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
377 if (net_loop(SNTP) < 0) {
378 printf("SNTP failed: host %pI4 not responding\n",
380 return CMD_RET_FAILURE;
383 return CMD_RET_SUCCESS;
388 "synchronize RTC via network",
393 #if defined(CONFIG_CMD_DNS)
394 int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
397 return CMD_RET_USAGE;
400 * We should check for a valid hostname:
401 * - Each label must be between 1 and 63 characters long
402 * - the entire hostname has a maximum of 255 characters
403 * - only the ASCII letters 'a' through 'z' (case-insensitive),
404 * the digits '0' through '9', and the hyphen
405 * - cannot begin or end with a hyphen
406 * - no other symbols, punctuation characters, or blank spaces are
408 * but hey - this is a minimalist implmentation, so only check length
409 * and let the name server deal with things.
411 if (strlen(argv[1]) >= 255) {
412 printf("dns error: hostname too long\n");
413 return CMD_RET_FAILURE;
416 net_dns_resolve = argv[1];
419 net_dns_env_var = argv[2];
421 net_dns_env_var = NULL;
423 if (net_loop(DNS) < 0) {
424 printf("dns lookup of %s failed, check setup\n", argv[1]);
425 return CMD_RET_FAILURE;
428 return CMD_RET_SUCCESS;
433 "lookup the IP of a hostname",
437 #endif /* CONFIG_CMD_DNS */
439 #if defined(CONFIG_CMD_LINK_LOCAL)
440 static int do_link_local(struct cmd_tbl *cmdtp, int flag, int argc,
445 if (net_loop(LINKLOCAL) < 0)
446 return CMD_RET_FAILURE;
448 net_gateway.s_addr = 0;
449 ip_to_string(net_gateway, tmp);
450 env_set("gatewayip", tmp);
452 ip_to_string(net_netmask, tmp);
453 env_set("netmask", tmp);
455 ip_to_string(net_ip, tmp);
456 env_set("ipaddr", tmp);
457 env_set("llipaddr", tmp); /* store this for next time */
459 return CMD_RET_SUCCESS;
463 linklocal, 1, 1, do_link_local,
464 "acquire a network IP address using the link-local protocol",
468 #endif /* CONFIG_CMD_LINK_LOCAL */