1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
11 #include <bootstage.h>
19 static int netboot_common(enum proto_t, struct cmd_tbl *, int, char * const []);
21 #ifdef CONFIG_CMD_BOOTP
22 static int do_bootp(struct cmd_tbl *cmdtp, int flag, int argc,
25 return netboot_common(BOOTP, cmdtp, argc, argv);
29 bootp, 3, 1, do_bootp,
30 "boot image via network using BOOTP/TFTP protocol",
31 "[loadAddress] [[hostIPaddr:]bootfilename]"
35 #ifdef CONFIG_CMD_TFTPBOOT
36 int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
40 bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
41 ret = netboot_common(TFTPGET, cmdtp, argc, argv);
42 bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
47 tftpboot, 3, 1, do_tftpb,
48 "boot image via network using TFTP protocol",
49 "[loadAddress] [[hostIPaddr:]bootfilename]"
53 #ifdef CONFIG_CMD_TFTPPUT
54 static int do_tftpput(struct cmd_tbl *cmdtp, int flag, int argc,
57 return netboot_common(TFTPPUT, cmdtp, argc, argv);
61 tftpput, 4, 1, do_tftpput,
62 "TFTP put command, for uploading files to a server",
63 "Address Size [[hostIPaddr:]filename]"
67 #ifdef CONFIG_CMD_TFTPSRV
68 static int do_tftpsrv(struct cmd_tbl *cmdtp, int flag, int argc,
71 return netboot_common(TFTPSRV, cmdtp, argc, argv);
75 tftpsrv, 2, 1, do_tftpsrv,
76 "act as a TFTP server and boot the first received file",
78 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
79 "The transfer is aborted if a transfer has not been started after\n"
80 "about 50 seconds or if Ctrl-C is pressed."
85 #ifdef CONFIG_CMD_RARP
86 int do_rarpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
88 return netboot_common(RARP, cmdtp, argc, argv);
92 rarpboot, 3, 1, do_rarpb,
93 "boot image via network using RARP/TFTP protocol",
94 "[loadAddress] [[hostIPaddr:]bootfilename]"
98 #if defined(CONFIG_CMD_DHCP)
99 static int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc,
102 return netboot_common(DHCP, cmdtp, argc, argv);
107 "boot image via network using DHCP/TFTP protocol",
108 "[loadAddress] [[hostIPaddr:]bootfilename]"
112 #if defined(CONFIG_CMD_NFS)
113 static int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc,
116 return netboot_common(NFS, cmdtp, argc, argv);
121 "boot image via network using NFS protocol",
122 "[loadAddress] [[hostIPaddr:]bootfilename]"
126 static void netboot_update_env(void)
130 if (net_gateway.s_addr) {
131 ip_to_string(net_gateway, tmp);
132 env_set("gatewayip", tmp);
135 if (net_netmask.s_addr) {
136 ip_to_string(net_netmask, tmp);
137 env_set("netmask", tmp);
140 #ifdef CONFIG_CMD_BOOTP
142 env_set("hostname", net_hostname);
145 #ifdef CONFIG_CMD_BOOTP
146 if (net_root_path[0])
147 env_set("rootpath", net_root_path);
151 ip_to_string(net_ip, tmp);
152 env_set("ipaddr", tmp);
154 #if !defined(CONFIG_BOOTP_SERVERIP)
156 * Only attempt to change serverip if net/bootp.c:store_net_params()
159 if (net_server_ip.s_addr) {
160 ip_to_string(net_server_ip, tmp);
161 env_set("serverip", tmp);
164 if (net_dns_server.s_addr) {
165 ip_to_string(net_dns_server, tmp);
166 env_set("dnsip", tmp);
168 #if defined(CONFIG_BOOTP_DNS2)
169 if (net_dns_server2.s_addr) {
170 ip_to_string(net_dns_server2, tmp);
171 env_set("dnsip2", tmp);
174 #ifdef CONFIG_CMD_BOOTP
175 if (net_nis_domain[0])
176 env_set("domain", net_nis_domain);
179 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
180 if (net_ntp_time_offset) {
181 sprintf(tmp, "%d", net_ntp_time_offset);
182 env_set("timeoffset", tmp);
185 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
186 if (net_ntp_server.s_addr) {
187 ip_to_string(net_ntp_server, tmp);
188 env_set("ntpserverip", tmp);
193 static int netboot_common(enum proto_t proto, struct cmd_tbl *cmdtp, int argc,
202 net_boot_file_name_explicit = false;
204 /* pre-set image_load_addr */
205 s = env_get("loadaddr");
207 image_load_addr = simple_strtoul(s, NULL, 16);
211 /* refresh bootfile name from env */
212 copy_filename(net_boot_file_name, env_get("bootfile"),
213 sizeof(net_boot_file_name));
217 * Only one arg - accept two forms:
218 * Just load address, or just boot file name. The latter
219 * form must be written in a format which can not be
220 * mis-interpreted as a valid number.
222 addr = simple_strtoul(argv[1], &end, 16);
223 if (end == (argv[1] + strlen(argv[1]))) {
224 image_load_addr = addr;
225 /* refresh bootfile name from env */
226 copy_filename(net_boot_file_name, env_get("bootfile"),
227 sizeof(net_boot_file_name));
229 net_boot_file_name_explicit = true;
230 copy_filename(net_boot_file_name, argv[1],
231 sizeof(net_boot_file_name));
236 image_load_addr = simple_strtoul(argv[1], NULL, 16);
237 net_boot_file_name_explicit = true;
238 copy_filename(net_boot_file_name, argv[2],
239 sizeof(net_boot_file_name));
243 #ifdef CONFIG_CMD_TFTPPUT
245 if (strict_strtoul(argv[1], 16, &image_save_addr) < 0 ||
246 strict_strtoul(argv[2], 16, &image_save_size) < 0) {
247 printf("Invalid address/size\n");
248 return CMD_RET_USAGE;
250 net_boot_file_name_explicit = true;
251 copy_filename(net_boot_file_name, argv[3],
252 sizeof(net_boot_file_name));
256 bootstage_error(BOOTSTAGE_ID_NET_START);
257 return CMD_RET_USAGE;
259 bootstage_mark(BOOTSTAGE_ID_NET_START);
261 size = net_loop(proto);
263 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
264 return CMD_RET_FAILURE;
266 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
268 /* net_loop ok, update environment */
269 netboot_update_env();
271 /* done if no file was loaded (no errors though) */
273 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
274 return CMD_RET_SUCCESS;
277 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
279 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
281 if (rcode == CMD_RET_SUCCESS)
282 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
284 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
288 #if defined(CONFIG_CMD_PING)
289 static int do_ping(struct cmd_tbl *cmdtp, int flag, int argc,
293 return CMD_RET_USAGE;
295 net_ping_ip = string_to_ip(argv[1]);
296 if (net_ping_ip.s_addr == 0)
297 return CMD_RET_USAGE;
299 if (net_loop(PING) < 0) {
300 printf("ping failed; host %s is not alive\n", argv[1]);
301 return CMD_RET_FAILURE;
304 printf("host %s is alive\n", argv[1]);
306 return CMD_RET_SUCCESS;
311 "send ICMP ECHO_REQUEST to network host",
316 #if defined(CONFIG_CMD_CDP)
318 static void cdp_update_env(void)
322 if (cdp_appliance_vlan != htons(-1)) {
323 printf("CDP offered appliance VLAN %d\n",
324 ntohs(cdp_appliance_vlan));
325 vlan_to_string(cdp_appliance_vlan, tmp);
326 env_set("vlan", tmp);
327 net_our_vlan = cdp_appliance_vlan;
330 if (cdp_native_vlan != htons(-1)) {
331 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
332 vlan_to_string(cdp_native_vlan, tmp);
333 env_set("nvlan", tmp);
334 net_native_vlan = cdp_native_vlan;
338 int do_cdp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
344 printf("cdp failed; perhaps not a CISCO switch?\n");
345 return CMD_RET_FAILURE;
350 return CMD_RET_SUCCESS;
355 "Perform CDP network configuration",
360 #if defined(CONFIG_CMD_SNTP)
361 static struct udp_ops sntp_ops = {
362 .prereq = sntp_prereq,
367 int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
372 net_ntp_server = env_get_ip("ntpserverip");
373 if (net_ntp_server.s_addr == 0) {
374 printf("ntpserverip not set\n");
375 return CMD_RET_FAILURE;
378 net_ntp_server = string_to_ip(argv[1]);
379 if (net_ntp_server.s_addr == 0) {
380 printf("Bad NTP server IP address\n");
381 return CMD_RET_FAILURE;
385 toff = env_get("timeoffset");
387 net_ntp_time_offset = 0;
389 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
391 if (udp_loop(&sntp_ops) < 0) {
392 printf("SNTP failed: host %pI4 not responding\n",
394 return CMD_RET_FAILURE;
397 return CMD_RET_SUCCESS;
402 "synchronize RTC via network",
407 #if defined(CONFIG_CMD_DNS)
408 int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
411 return CMD_RET_USAGE;
414 * We should check for a valid hostname:
415 * - Each label must be between 1 and 63 characters long
416 * - the entire hostname has a maximum of 255 characters
417 * - only the ASCII letters 'a' through 'z' (case-insensitive),
418 * the digits '0' through '9', and the hyphen
419 * - cannot begin or end with a hyphen
420 * - no other symbols, punctuation characters, or blank spaces are
422 * but hey - this is a minimalist implmentation, so only check length
423 * and let the name server deal with things.
425 if (strlen(argv[1]) >= 255) {
426 printf("dns error: hostname too long\n");
427 return CMD_RET_FAILURE;
430 net_dns_resolve = argv[1];
433 net_dns_env_var = argv[2];
435 net_dns_env_var = NULL;
437 if (net_loop(DNS) < 0) {
438 printf("dns lookup of %s failed, check setup\n", argv[1]);
439 return CMD_RET_FAILURE;
442 return CMD_RET_SUCCESS;
447 "lookup the IP of a hostname",
451 #endif /* CONFIG_CMD_DNS */
453 #if defined(CONFIG_CMD_LINK_LOCAL)
454 static int do_link_local(struct cmd_tbl *cmdtp, int flag, int argc,
459 if (net_loop(LINKLOCAL) < 0)
460 return CMD_RET_FAILURE;
462 net_gateway.s_addr = 0;
463 ip_to_string(net_gateway, tmp);
464 env_set("gatewayip", tmp);
466 ip_to_string(net_netmask, tmp);
467 env_set("netmask", tmp);
469 ip_to_string(net_ip, tmp);
470 env_set("ipaddr", tmp);
471 env_set("llipaddr", tmp); /* store this for next time */
473 return CMD_RET_SUCCESS;
477 linklocal, 1, 1, do_link_local,
478 "acquire a network IP address using the link-local protocol",
482 #endif /* CONFIG_CMD_LINK_LOCAL */