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 static int do_bootp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
19 return netboot_common(BOOTP, cmdtp, argc, argv);
23 bootp, 3, 1, do_bootp,
24 "boot image via network using BOOTP/TFTP protocol",
25 "[loadAddress] [[hostIPaddr:]bootfilename]"
28 int do_tftpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
32 bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
33 ret = netboot_common(TFTPGET, cmdtp, argc, argv);
34 bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
39 tftpboot, 3, 1, do_tftpb,
40 "boot image via network using TFTP protocol",
41 "[loadAddress] [[hostIPaddr:]bootfilename]"
44 #ifdef CONFIG_CMD_TFTPPUT
45 int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
49 ret = netboot_common(TFTPPUT, cmdtp, argc, argv);
54 tftpput, 4, 1, do_tftpput,
55 "TFTP put command, for uploading files to a server",
56 "Address Size [[hostIPaddr:]filename]"
60 #ifdef CONFIG_CMD_TFTPSRV
61 static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
63 return netboot_common(TFTPSRV, cmdtp, argc, argv);
67 tftpsrv, 2, 1, do_tftpsrv,
68 "act as a TFTP server and boot the first received file",
70 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
71 "The transfer is aborted if a transfer has not been started after\n"
72 "about 50 seconds or if Ctrl-C is pressed."
77 #ifdef CONFIG_CMD_RARP
78 int do_rarpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
80 return netboot_common(RARP, cmdtp, argc, argv);
84 rarpboot, 3, 1, do_rarpb,
85 "boot image via network using RARP/TFTP protocol",
86 "[loadAddress] [[hostIPaddr:]bootfilename]"
90 #if defined(CONFIG_CMD_DHCP)
91 static int do_dhcp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
93 return netboot_common(DHCP, cmdtp, argc, argv);
98 "boot image via network using DHCP/TFTP protocol",
99 "[loadAddress] [[hostIPaddr:]bootfilename]"
103 #if defined(CONFIG_CMD_NFS)
104 static int do_nfs(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
106 return netboot_common(NFS, cmdtp, argc, argv);
111 "boot image via network using NFS protocol",
112 "[loadAddress] [[hostIPaddr:]bootfilename]"
116 static void netboot_update_env(void)
120 if (NetOurGatewayIP) {
121 ip_to_string(NetOurGatewayIP, tmp);
122 setenv("gatewayip", tmp);
125 if (NetOurSubnetMask) {
126 ip_to_string(NetOurSubnetMask, tmp);
127 setenv("netmask", tmp);
130 if (NetOurHostName[0])
131 setenv("hostname", NetOurHostName);
133 if (NetOurRootPath[0])
134 setenv("rootpath", NetOurRootPath);
137 ip_to_string(NetOurIP, tmp);
138 setenv("ipaddr", tmp);
140 #if !defined(CONFIG_BOOTP_SERVERIP)
142 * Only attempt to change serverip if net/bootp.c:BootpCopyNetParams()
146 ip_to_string(NetServerIP, tmp);
147 setenv("serverip", tmp);
151 ip_to_string(NetOurDNSIP, tmp);
152 setenv("dnsip", tmp);
154 #if defined(CONFIG_BOOTP_DNS2)
156 ip_to_string(NetOurDNS2IP, tmp);
157 setenv("dnsip2", tmp);
160 if (NetOurNISDomain[0])
161 setenv("domain", NetOurNISDomain);
163 #if defined(CONFIG_CMD_SNTP) \
164 && defined(CONFIG_BOOTP_TIMEOFFSET)
166 sprintf(tmp, "%d", NetTimeOffset);
167 setenv("timeoffset", tmp);
170 #if defined(CONFIG_CMD_SNTP) \
171 && defined(CONFIG_BOOTP_NTPSERVER)
172 if (NetNtpServerIP) {
173 ip_to_string(NetNtpServerIP, tmp);
174 setenv("ntpserverip", tmp);
179 static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
188 /* pre-set load_addr */
189 if ((s = getenv("loadaddr")) != NULL) {
190 load_addr = simple_strtoul(s, NULL, 16);
198 * Only one arg - accept two forms:
199 * Just load address, or just boot file name. The latter
200 * form must be written in a format which can not be
201 * mis-interpreted as a valid number.
203 addr = simple_strtoul(argv[1], &end, 16);
204 if (end == (argv[1] + strlen(argv[1])))
207 copy_filename(BootFile, argv[1], sizeof(BootFile));
210 case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
211 copy_filename(BootFile, argv[2], sizeof(BootFile));
215 #ifdef CONFIG_CMD_TFTPPUT
217 if (strict_strtoul(argv[1], 16, &save_addr) < 0 ||
218 strict_strtoul(argv[2], 16, &save_size) < 0) {
219 printf("Invalid address/size\n");
220 return cmd_usage(cmdtp);
222 copy_filename(BootFile, argv[3], sizeof(BootFile));
226 bootstage_error(BOOTSTAGE_ID_NET_START);
227 return CMD_RET_USAGE;
229 bootstage_mark(BOOTSTAGE_ID_NET_START);
231 if ((size = NetLoop(proto)) < 0) {
232 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
235 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
237 /* NetLoop ok, update environment */
238 netboot_update_env();
240 /* done if no file was loaded (no errors though) */
242 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
247 flush_cache(load_addr, size);
249 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
251 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
254 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
256 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
260 #if defined(CONFIG_CMD_PING)
261 static int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
266 NetPingIP = string_to_ip(argv[1]);
268 return CMD_RET_USAGE;
270 if (NetLoop(PING) < 0) {
271 printf("ping failed; host %s is not alive\n", argv[1]);
275 printf("host %s is alive\n", argv[1]);
282 "send ICMP ECHO_REQUEST to network host",
287 #if defined(CONFIG_CMD_CDP)
289 static void cdp_update_env(void)
293 if (CDPApplianceVLAN != htons(-1)) {
294 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
295 VLAN_to_string(CDPApplianceVLAN, tmp);
297 NetOurVLAN = CDPApplianceVLAN;
300 if (CDPNativeVLAN != htons(-1)) {
301 printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
302 VLAN_to_string(CDPNativeVLAN, tmp);
303 setenv("nvlan", tmp);
304 NetOurNativeVLAN = CDPNativeVLAN;
309 int do_cdp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
315 printf("cdp failed; perhaps not a CISCO switch?\n");
326 "Perform CDP network configuration",
331 #if defined(CONFIG_CMD_SNTP)
332 int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
337 NetNtpServerIP = getenv_IPaddr("ntpserverip");
338 if (NetNtpServerIP == 0) {
339 printf("ntpserverip not set\n");
343 NetNtpServerIP = string_to_ip(argv[1]);
344 if (NetNtpServerIP == 0) {
345 printf("Bad NTP server IP address\n");
350 toff = getenv("timeoffset");
354 NetTimeOffset = simple_strtol(toff, NULL, 10);
356 if (NetLoop(SNTP) < 0) {
357 printf("SNTP failed: host %pI4 not responding\n",
367 "synchronize RTC via network",
372 #if defined(CONFIG_CMD_DNS)
373 int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
376 return CMD_RET_USAGE;
379 * We should check for a valid hostname:
380 * - Each label must be between 1 and 63 characters long
381 * - the entire hostname has a maximum of 255 characters
382 * - only the ASCII letters 'a' through 'z' (case-insensitive),
383 * the digits '0' through '9', and the hyphen
384 * - cannot begin or end with a hyphen
385 * - no other symbols, punctuation characters, or blank spaces are
387 * but hey - this is a minimalist implmentation, so only check length
388 * and let the name server deal with things.
390 if (strlen(argv[1]) >= 255) {
391 printf("dns error: hostname too long\n");
395 NetDNSResolve = argv[1];
398 NetDNSenvvar = argv[2];
402 if (NetLoop(DNS) < 0) {
403 printf("dns lookup of %s failed, check setup\n", argv[1]);
412 "lookup the IP of a hostname",
416 #endif /* CONFIG_CMD_DNS */
418 #if defined(CONFIG_CMD_LINK_LOCAL)
419 static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc,
424 if (NetLoop(LINKLOCAL) < 0)
428 ip_to_string(NetOurGatewayIP, tmp);
429 setenv("gatewayip", tmp);
431 ip_to_string(NetOurSubnetMask, tmp);
432 setenv("netmask", tmp);
434 ip_to_string(NetOurIP, tmp);
435 setenv("ipaddr", tmp);
436 setenv("llipaddr", tmp); /* store this for next time */
442 linklocal, 1, 1, do_link_local,
443 "acquire a network IP address using the link-local protocol",
447 #endif /* CONFIG_CMD_LINK_LOCAL */