1 // SPDX-License-Identifier: GPL-2.0+
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);
138 #ifdef CONFIG_CMD_BOOTP
140 env_set("hostname", net_hostname);
143 #ifdef CONFIG_CMD_BOOTP
144 if (net_root_path[0])
145 env_set("rootpath", net_root_path);
149 ip_to_string(net_ip, tmp);
150 env_set("ipaddr", tmp);
152 #if !defined(CONFIG_BOOTP_SERVERIP)
154 * Only attempt to change serverip if net/bootp.c:store_net_params()
157 if (net_server_ip.s_addr) {
158 ip_to_string(net_server_ip, tmp);
159 env_set("serverip", tmp);
162 if (net_dns_server.s_addr) {
163 ip_to_string(net_dns_server, tmp);
164 env_set("dnsip", tmp);
166 #if defined(CONFIG_BOOTP_DNS2)
167 if (net_dns_server2.s_addr) {
168 ip_to_string(net_dns_server2, tmp);
169 env_set("dnsip2", tmp);
172 #ifdef CONFIG_CMD_BOOTP
173 if (net_nis_domain[0])
174 env_set("domain", net_nis_domain);
177 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
178 if (net_ntp_time_offset) {
179 sprintf(tmp, "%d", net_ntp_time_offset);
180 env_set("timeoffset", tmp);
183 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
184 if (net_ntp_server.s_addr) {
185 ip_to_string(net_ntp_server, tmp);
186 env_set("ntpserverip", tmp);
191 static int netboot_common(enum proto_t proto, struct cmd_tbl *cmdtp, int argc,
200 net_boot_file_name_explicit = false;
202 /* pre-set image_load_addr */
203 s = env_get("loadaddr");
205 image_load_addr = simple_strtoul(s, NULL, 16);
209 /* refresh bootfile name from env */
210 copy_filename(net_boot_file_name, env_get("bootfile"),
211 sizeof(net_boot_file_name));
215 * Only one arg - accept two forms:
216 * Just load address, or just boot file name. The latter
217 * form must be written in a format which can not be
218 * mis-interpreted as a valid number.
220 addr = simple_strtoul(argv[1], &end, 16);
221 if (end == (argv[1] + strlen(argv[1]))) {
222 image_load_addr = addr;
223 /* refresh bootfile name from env */
224 copy_filename(net_boot_file_name, env_get("bootfile"),
225 sizeof(net_boot_file_name));
227 net_boot_file_name_explicit = true;
228 copy_filename(net_boot_file_name, argv[1],
229 sizeof(net_boot_file_name));
234 image_load_addr = simple_strtoul(argv[1], NULL, 16);
235 net_boot_file_name_explicit = true;
236 copy_filename(net_boot_file_name, argv[2],
237 sizeof(net_boot_file_name));
241 #ifdef CONFIG_CMD_TFTPPUT
243 if (strict_strtoul(argv[1], 16, &image_save_addr) < 0 ||
244 strict_strtoul(argv[2], 16, &image_save_size) < 0) {
245 printf("Invalid address/size\n");
246 return CMD_RET_USAGE;
248 net_boot_file_name_explicit = true;
249 copy_filename(net_boot_file_name, argv[3],
250 sizeof(net_boot_file_name));
254 bootstage_error(BOOTSTAGE_ID_NET_START);
255 return CMD_RET_USAGE;
257 bootstage_mark(BOOTSTAGE_ID_NET_START);
259 size = net_loop(proto);
261 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
262 return CMD_RET_FAILURE;
264 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
266 /* net_loop ok, update environment */
267 netboot_update_env();
269 /* done if no file was loaded (no errors though) */
271 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
272 return CMD_RET_SUCCESS;
275 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
277 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
279 if (rcode == CMD_RET_SUCCESS)
280 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
282 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
286 #if defined(CONFIG_CMD_PING)
287 static int do_ping(struct cmd_tbl *cmdtp, int flag, int argc,
291 return CMD_RET_USAGE;
293 net_ping_ip = string_to_ip(argv[1]);
294 if (net_ping_ip.s_addr == 0)
295 return CMD_RET_USAGE;
297 if (net_loop(PING) < 0) {
298 printf("ping failed; host %s is not alive\n", argv[1]);
299 return CMD_RET_FAILURE;
302 printf("host %s is alive\n", argv[1]);
304 return CMD_RET_SUCCESS;
309 "send ICMP ECHO_REQUEST to network host",
314 #if defined(CONFIG_CMD_CDP)
316 static void cdp_update_env(void)
320 if (cdp_appliance_vlan != htons(-1)) {
321 printf("CDP offered appliance VLAN %d\n",
322 ntohs(cdp_appliance_vlan));
323 vlan_to_string(cdp_appliance_vlan, tmp);
324 env_set("vlan", tmp);
325 net_our_vlan = cdp_appliance_vlan;
328 if (cdp_native_vlan != htons(-1)) {
329 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
330 vlan_to_string(cdp_native_vlan, tmp);
331 env_set("nvlan", tmp);
332 net_native_vlan = cdp_native_vlan;
336 int do_cdp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
342 printf("cdp failed; perhaps not a CISCO switch?\n");
343 return CMD_RET_FAILURE;
348 return CMD_RET_SUCCESS;
353 "Perform CDP network configuration",
358 #if defined(CONFIG_CMD_SNTP)
359 int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
364 net_ntp_server = env_get_ip("ntpserverip");
365 if (net_ntp_server.s_addr == 0) {
366 printf("ntpserverip not set\n");
367 return CMD_RET_FAILURE;
370 net_ntp_server = string_to_ip(argv[1]);
371 if (net_ntp_server.s_addr == 0) {
372 printf("Bad NTP server IP address\n");
373 return CMD_RET_FAILURE;
377 toff = env_get("timeoffset");
379 net_ntp_time_offset = 0;
381 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
383 if (net_loop(SNTP) < 0) {
384 printf("SNTP failed: host %pI4 not responding\n",
386 return CMD_RET_FAILURE;
389 return CMD_RET_SUCCESS;
394 "synchronize RTC via network",
399 #if defined(CONFIG_CMD_DNS)
400 int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
403 return CMD_RET_USAGE;
406 * We should check for a valid hostname:
407 * - Each label must be between 1 and 63 characters long
408 * - the entire hostname has a maximum of 255 characters
409 * - only the ASCII letters 'a' through 'z' (case-insensitive),
410 * the digits '0' through '9', and the hyphen
411 * - cannot begin or end with a hyphen
412 * - no other symbols, punctuation characters, or blank spaces are
414 * but hey - this is a minimalist implmentation, so only check length
415 * and let the name server deal with things.
417 if (strlen(argv[1]) >= 255) {
418 printf("dns error: hostname too long\n");
419 return CMD_RET_FAILURE;
422 net_dns_resolve = argv[1];
425 net_dns_env_var = argv[2];
427 net_dns_env_var = NULL;
429 if (net_loop(DNS) < 0) {
430 printf("dns lookup of %s failed, check setup\n", argv[1]);
431 return CMD_RET_FAILURE;
434 return CMD_RET_SUCCESS;
439 "lookup the IP of a hostname",
443 #endif /* CONFIG_CMD_DNS */
445 #if defined(CONFIG_CMD_LINK_LOCAL)
446 static int do_link_local(struct cmd_tbl *cmdtp, int flag, int argc,
451 if (net_loop(LINKLOCAL) < 0)
452 return CMD_RET_FAILURE;
454 net_gateway.s_addr = 0;
455 ip_to_string(net_gateway, tmp);
456 env_set("gatewayip", tmp);
458 ip_to_string(net_netmask, tmp);
459 env_set("netmask", tmp);
461 ip_to_string(net_ip, tmp);
462 env_set("ipaddr", tmp);
463 env_set("llipaddr", tmp); /* store this for next time */
465 return CMD_RET_SUCCESS;
469 linklocal, 1, 1, do_link_local,
470 "acquire a network IP address using the link-local protocol",
474 #endif /* CONFIG_CMD_LINK_LOCAL */