]> git.ipfire.org Git - thirdparty/u-boot.git/blob - cmd/net.c
common: Drop bootstage.h from common header
[thirdparty/u-boot.git] / cmd / net.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 */
6
7 /*
8 * Boot support
9 */
10 #include <common.h>
11 #include <bootstage.h>
12 #include <command.h>
13 #include <env.h>
14 #include <image.h>
15 #include <net.h>
16
17 static int netboot_common(enum proto_t, cmd_tbl_t *, int, char * const []);
18
19 #ifdef CONFIG_CMD_BOOTP
20 static int do_bootp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
21 {
22 return netboot_common(BOOTP, cmdtp, argc, argv);
23 }
24
25 U_BOOT_CMD(
26 bootp, 3, 1, do_bootp,
27 "boot image via network using BOOTP/TFTP protocol",
28 "[loadAddress] [[hostIPaddr:]bootfilename]"
29 );
30 #endif
31
32 #ifdef CONFIG_CMD_TFTPBOOT
33 int do_tftpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
34 {
35 int ret;
36
37 bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
38 ret = netboot_common(TFTPGET, cmdtp, argc, argv);
39 bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
40 return ret;
41 }
42
43 U_BOOT_CMD(
44 tftpboot, 3, 1, do_tftpb,
45 "boot image via network using TFTP protocol",
46 "[loadAddress] [[hostIPaddr:]bootfilename]"
47 );
48 #endif
49
50 #ifdef CONFIG_CMD_TFTPPUT
51 static int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
52 {
53 return netboot_common(TFTPPUT, cmdtp, argc, argv);
54 }
55
56 U_BOOT_CMD(
57 tftpput, 4, 1, do_tftpput,
58 "TFTP put command, for uploading files to a server",
59 "Address Size [[hostIPaddr:]filename]"
60 );
61 #endif
62
63 #ifdef CONFIG_CMD_TFTPSRV
64 static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
65 {
66 return netboot_common(TFTPSRV, cmdtp, argc, argv);
67 }
68
69 U_BOOT_CMD(
70 tftpsrv, 2, 1, do_tftpsrv,
71 "act as a TFTP server and boot the first received file",
72 "[loadAddress]\n"
73 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
74 "The transfer is aborted if a transfer has not been started after\n"
75 "about 50 seconds or if Ctrl-C is pressed."
76 );
77 #endif
78
79
80 #ifdef CONFIG_CMD_RARP
81 int do_rarpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
82 {
83 return netboot_common(RARP, cmdtp, argc, argv);
84 }
85
86 U_BOOT_CMD(
87 rarpboot, 3, 1, do_rarpb,
88 "boot image via network using RARP/TFTP protocol",
89 "[loadAddress] [[hostIPaddr:]bootfilename]"
90 );
91 #endif
92
93 #if defined(CONFIG_CMD_DHCP)
94 static int do_dhcp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
95 {
96 return netboot_common(DHCP, cmdtp, argc, argv);
97 }
98
99 U_BOOT_CMD(
100 dhcp, 3, 1, do_dhcp,
101 "boot image via network using DHCP/TFTP protocol",
102 "[loadAddress] [[hostIPaddr:]bootfilename]"
103 );
104 #endif
105
106 #if defined(CONFIG_CMD_NFS)
107 static int do_nfs(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
108 {
109 return netboot_common(NFS, cmdtp, argc, argv);
110 }
111
112 U_BOOT_CMD(
113 nfs, 3, 1, do_nfs,
114 "boot image via network using NFS protocol",
115 "[loadAddress] [[hostIPaddr:]bootfilename]"
116 );
117 #endif
118
119 static void netboot_update_env(void)
120 {
121 char tmp[22];
122
123 if (net_gateway.s_addr) {
124 ip_to_string(net_gateway, tmp);
125 env_set("gatewayip", tmp);
126 }
127
128 if (net_netmask.s_addr) {
129 ip_to_string(net_netmask, tmp);
130 env_set("netmask", tmp);
131 }
132
133 if (net_hostname[0])
134 env_set("hostname", net_hostname);
135
136 if (net_root_path[0])
137 env_set("rootpath", net_root_path);
138
139 if (net_ip.s_addr) {
140 ip_to_string(net_ip, tmp);
141 env_set("ipaddr", tmp);
142 }
143 #if !defined(CONFIG_BOOTP_SERVERIP)
144 /*
145 * Only attempt to change serverip if net/bootp.c:store_net_params()
146 * could have set it
147 */
148 if (net_server_ip.s_addr) {
149 ip_to_string(net_server_ip, tmp);
150 env_set("serverip", tmp);
151 }
152 #endif
153 if (net_dns_server.s_addr) {
154 ip_to_string(net_dns_server, tmp);
155 env_set("dnsip", tmp);
156 }
157 #if defined(CONFIG_BOOTP_DNS2)
158 if (net_dns_server2.s_addr) {
159 ip_to_string(net_dns_server2, tmp);
160 env_set("dnsip2", tmp);
161 }
162 #endif
163 if (net_nis_domain[0])
164 env_set("domain", net_nis_domain);
165
166 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
167 if (net_ntp_time_offset) {
168 sprintf(tmp, "%d", net_ntp_time_offset);
169 env_set("timeoffset", tmp);
170 }
171 #endif
172 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
173 if (net_ntp_server.s_addr) {
174 ip_to_string(net_ntp_server, tmp);
175 env_set("ntpserverip", tmp);
176 }
177 #endif
178 }
179
180 static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
181 char * const argv[])
182 {
183 char *s;
184 char *end;
185 int rcode = 0;
186 int size;
187 ulong addr;
188
189 net_boot_file_name_explicit = false;
190
191 /* pre-set image_load_addr */
192 s = env_get("loadaddr");
193 if (s != NULL)
194 image_load_addr = simple_strtoul(s, NULL, 16);
195
196 switch (argc) {
197 case 1:
198 /* refresh bootfile name from env */
199 copy_filename(net_boot_file_name, env_get("bootfile"),
200 sizeof(net_boot_file_name));
201 break;
202
203 case 2: /*
204 * Only one arg - accept two forms:
205 * Just load address, or just boot file name. The latter
206 * form must be written in a format which can not be
207 * mis-interpreted as a valid number.
208 */
209 addr = simple_strtoul(argv[1], &end, 16);
210 if (end == (argv[1] + strlen(argv[1]))) {
211 image_load_addr = addr;
212 /* refresh bootfile name from env */
213 copy_filename(net_boot_file_name, env_get("bootfile"),
214 sizeof(net_boot_file_name));
215 } else {
216 net_boot_file_name_explicit = true;
217 copy_filename(net_boot_file_name, argv[1],
218 sizeof(net_boot_file_name));
219 }
220 break;
221
222 case 3:
223 image_load_addr = simple_strtoul(argv[1], NULL, 16);
224 net_boot_file_name_explicit = true;
225 copy_filename(net_boot_file_name, argv[2],
226 sizeof(net_boot_file_name));
227
228 break;
229
230 #ifdef CONFIG_CMD_TFTPPUT
231 case 4:
232 if (strict_strtoul(argv[1], 16, &image_save_addr) < 0 ||
233 strict_strtoul(argv[2], 16, &image_save_size) < 0) {
234 printf("Invalid address/size\n");
235 return CMD_RET_USAGE;
236 }
237 net_boot_file_name_explicit = true;
238 copy_filename(net_boot_file_name, argv[3],
239 sizeof(net_boot_file_name));
240 break;
241 #endif
242 default:
243 bootstage_error(BOOTSTAGE_ID_NET_START);
244 return CMD_RET_USAGE;
245 }
246 bootstage_mark(BOOTSTAGE_ID_NET_START);
247
248 size = net_loop(proto);
249 if (size < 0) {
250 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
251 return CMD_RET_FAILURE;
252 }
253 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
254
255 /* net_loop ok, update environment */
256 netboot_update_env();
257
258 /* done if no file was loaded (no errors though) */
259 if (size == 0) {
260 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
261 return CMD_RET_SUCCESS;
262 }
263
264 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
265
266 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
267
268 if (rcode == CMD_RET_SUCCESS)
269 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
270 else
271 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
272 return rcode;
273 }
274
275 #if defined(CONFIG_CMD_PING)
276 static int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
277 {
278 if (argc < 2)
279 return CMD_RET_USAGE;
280
281 net_ping_ip = string_to_ip(argv[1]);
282 if (net_ping_ip.s_addr == 0)
283 return CMD_RET_USAGE;
284
285 if (net_loop(PING) < 0) {
286 printf("ping failed; host %s is not alive\n", argv[1]);
287 return CMD_RET_FAILURE;
288 }
289
290 printf("host %s is alive\n", argv[1]);
291
292 return CMD_RET_SUCCESS;
293 }
294
295 U_BOOT_CMD(
296 ping, 2, 1, do_ping,
297 "send ICMP ECHO_REQUEST to network host",
298 "pingAddress"
299 );
300 #endif
301
302 #if defined(CONFIG_CMD_CDP)
303
304 static void cdp_update_env(void)
305 {
306 char tmp[16];
307
308 if (cdp_appliance_vlan != htons(-1)) {
309 printf("CDP offered appliance VLAN %d\n",
310 ntohs(cdp_appliance_vlan));
311 vlan_to_string(cdp_appliance_vlan, tmp);
312 env_set("vlan", tmp);
313 net_our_vlan = cdp_appliance_vlan;
314 }
315
316 if (cdp_native_vlan != htons(-1)) {
317 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
318 vlan_to_string(cdp_native_vlan, tmp);
319 env_set("nvlan", tmp);
320 net_native_vlan = cdp_native_vlan;
321 }
322 }
323
324 int do_cdp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
325 {
326 int r;
327
328 r = net_loop(CDP);
329 if (r < 0) {
330 printf("cdp failed; perhaps not a CISCO switch?\n");
331 return CMD_RET_FAILURE;
332 }
333
334 cdp_update_env();
335
336 return CMD_RET_SUCCESS;
337 }
338
339 U_BOOT_CMD(
340 cdp, 1, 1, do_cdp,
341 "Perform CDP network configuration",
342 "\n"
343 );
344 #endif
345
346 #if defined(CONFIG_CMD_SNTP)
347 int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
348 {
349 char *toff;
350
351 if (argc < 2) {
352 net_ntp_server = env_get_ip("ntpserverip");
353 if (net_ntp_server.s_addr == 0) {
354 printf("ntpserverip not set\n");
355 return CMD_RET_FAILURE;
356 }
357 } else {
358 net_ntp_server = string_to_ip(argv[1]);
359 if (net_ntp_server.s_addr == 0) {
360 printf("Bad NTP server IP address\n");
361 return CMD_RET_FAILURE;
362 }
363 }
364
365 toff = env_get("timeoffset");
366 if (toff == NULL)
367 net_ntp_time_offset = 0;
368 else
369 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
370
371 if (net_loop(SNTP) < 0) {
372 printf("SNTP failed: host %pI4 not responding\n",
373 &net_ntp_server);
374 return CMD_RET_FAILURE;
375 }
376
377 return CMD_RET_SUCCESS;
378 }
379
380 U_BOOT_CMD(
381 sntp, 2, 1, do_sntp,
382 "synchronize RTC via network",
383 "[NTP server IP]\n"
384 );
385 #endif
386
387 #if defined(CONFIG_CMD_DNS)
388 int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
389 {
390 if (argc == 1)
391 return CMD_RET_USAGE;
392
393 /*
394 * We should check for a valid hostname:
395 * - Each label must be between 1 and 63 characters long
396 * - the entire hostname has a maximum of 255 characters
397 * - only the ASCII letters 'a' through 'z' (case-insensitive),
398 * the digits '0' through '9', and the hyphen
399 * - cannot begin or end with a hyphen
400 * - no other symbols, punctuation characters, or blank spaces are
401 * permitted
402 * but hey - this is a minimalist implmentation, so only check length
403 * and let the name server deal with things.
404 */
405 if (strlen(argv[1]) >= 255) {
406 printf("dns error: hostname too long\n");
407 return CMD_RET_FAILURE;
408 }
409
410 net_dns_resolve = argv[1];
411
412 if (argc == 3)
413 net_dns_env_var = argv[2];
414 else
415 net_dns_env_var = NULL;
416
417 if (net_loop(DNS) < 0) {
418 printf("dns lookup of %s failed, check setup\n", argv[1]);
419 return CMD_RET_FAILURE;
420 }
421
422 return CMD_RET_SUCCESS;
423 }
424
425 U_BOOT_CMD(
426 dns, 3, 1, do_dns,
427 "lookup the IP of a hostname",
428 "hostname [envvar]"
429 );
430
431 #endif /* CONFIG_CMD_DNS */
432
433 #if defined(CONFIG_CMD_LINK_LOCAL)
434 static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc,
435 char * const argv[])
436 {
437 char tmp[22];
438
439 if (net_loop(LINKLOCAL) < 0)
440 return CMD_RET_FAILURE;
441
442 net_gateway.s_addr = 0;
443 ip_to_string(net_gateway, tmp);
444 env_set("gatewayip", tmp);
445
446 ip_to_string(net_netmask, tmp);
447 env_set("netmask", tmp);
448
449 ip_to_string(net_ip, tmp);
450 env_set("ipaddr", tmp);
451 env_set("llipaddr", tmp); /* store this for next time */
452
453 return CMD_RET_SUCCESS;
454 }
455
456 U_BOOT_CMD(
457 linklocal, 1, 1, do_link_local,
458 "acquire a network IP address using the link-local protocol",
459 ""
460 );
461
462 #endif /* CONFIG_CMD_LINK_LOCAL */