]> git.ipfire.org Git - thirdparty/u-boot.git/blob - cmd/net.c
Merge https://gitlab.denx.de/u-boot/custodians/u-boot-spi into next
[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, struct cmd_tbl *, int, char * const []);
18
19 #ifdef CONFIG_CMD_BOOTP
20 static int do_bootp(struct cmd_tbl *cmdtp, int flag, int argc,
21 char *const argv[])
22 {
23 return netboot_common(BOOTP, cmdtp, argc, argv);
24 }
25
26 U_BOOT_CMD(
27 bootp, 3, 1, do_bootp,
28 "boot image via network using BOOTP/TFTP protocol",
29 "[loadAddress] [[hostIPaddr:]bootfilename]"
30 );
31 #endif
32
33 #ifdef CONFIG_CMD_TFTPBOOT
34 int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
35 {
36 int ret;
37
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");
41 return ret;
42 }
43
44 U_BOOT_CMD(
45 tftpboot, 3, 1, do_tftpb,
46 "boot image via network using TFTP protocol",
47 "[loadAddress] [[hostIPaddr:]bootfilename]"
48 );
49 #endif
50
51 #ifdef CONFIG_CMD_TFTPPUT
52 static int do_tftpput(struct cmd_tbl *cmdtp, int flag, int argc,
53 char *const argv[])
54 {
55 return netboot_common(TFTPPUT, cmdtp, argc, argv);
56 }
57
58 U_BOOT_CMD(
59 tftpput, 4, 1, do_tftpput,
60 "TFTP put command, for uploading files to a server",
61 "Address Size [[hostIPaddr:]filename]"
62 );
63 #endif
64
65 #ifdef CONFIG_CMD_TFTPSRV
66 static int do_tftpsrv(struct cmd_tbl *cmdtp, int flag, int argc,
67 char *const argv[])
68 {
69 return netboot_common(TFTPSRV, cmdtp, argc, argv);
70 }
71
72 U_BOOT_CMD(
73 tftpsrv, 2, 1, do_tftpsrv,
74 "act as a TFTP server and boot the first received file",
75 "[loadAddress]\n"
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."
79 );
80 #endif
81
82
83 #ifdef CONFIG_CMD_RARP
84 int do_rarpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
85 {
86 return netboot_common(RARP, cmdtp, argc, argv);
87 }
88
89 U_BOOT_CMD(
90 rarpboot, 3, 1, do_rarpb,
91 "boot image via network using RARP/TFTP protocol",
92 "[loadAddress] [[hostIPaddr:]bootfilename]"
93 );
94 #endif
95
96 #if defined(CONFIG_CMD_DHCP)
97 static int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc,
98 char *const argv[])
99 {
100 return netboot_common(DHCP, cmdtp, argc, argv);
101 }
102
103 U_BOOT_CMD(
104 dhcp, 3, 1, do_dhcp,
105 "boot image via network using DHCP/TFTP protocol",
106 "[loadAddress] [[hostIPaddr:]bootfilename]"
107 );
108 #endif
109
110 #if defined(CONFIG_CMD_NFS)
111 static int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc,
112 char *const argv[])
113 {
114 return netboot_common(NFS, cmdtp, argc, argv);
115 }
116
117 U_BOOT_CMD(
118 nfs, 3, 1, do_nfs,
119 "boot image via network using NFS protocol",
120 "[loadAddress] [[hostIPaddr:]bootfilename]"
121 );
122 #endif
123
124 static void netboot_update_env(void)
125 {
126 char tmp[22];
127
128 if (net_gateway.s_addr) {
129 ip_to_string(net_gateway, tmp);
130 env_set("gatewayip", tmp);
131 }
132
133 if (net_netmask.s_addr) {
134 ip_to_string(net_netmask, tmp);
135 env_set("netmask", tmp);
136 }
137
138 #ifdef CONFIG_CMD_BOOTP
139 if (net_hostname[0])
140 env_set("hostname", net_hostname);
141 #endif
142
143 #ifdef CONFIG_CMD_BOOTP
144 if (net_root_path[0])
145 env_set("rootpath", net_root_path);
146 #endif
147
148 if (net_ip.s_addr) {
149 ip_to_string(net_ip, tmp);
150 env_set("ipaddr", tmp);
151 }
152 #if !defined(CONFIG_BOOTP_SERVERIP)
153 /*
154 * Only attempt to change serverip if net/bootp.c:store_net_params()
155 * could have set it
156 */
157 if (net_server_ip.s_addr) {
158 ip_to_string(net_server_ip, tmp);
159 env_set("serverip", tmp);
160 }
161 #endif
162 if (net_dns_server.s_addr) {
163 ip_to_string(net_dns_server, tmp);
164 env_set("dnsip", tmp);
165 }
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);
170 }
171 #endif
172 #ifdef CONFIG_CMD_BOOTP
173 if (net_nis_domain[0])
174 env_set("domain", net_nis_domain);
175 #endif
176
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);
181 }
182 #endif
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);
187 }
188 #endif
189 }
190
191 static int netboot_common(enum proto_t proto, struct cmd_tbl *cmdtp, int argc,
192 char *const argv[])
193 {
194 char *s;
195 char *end;
196 int rcode = 0;
197 int size;
198 ulong addr;
199
200 net_boot_file_name_explicit = false;
201
202 /* pre-set image_load_addr */
203 s = env_get("loadaddr");
204 if (s != NULL)
205 image_load_addr = simple_strtoul(s, NULL, 16);
206
207 switch (argc) {
208 case 1:
209 /* refresh bootfile name from env */
210 copy_filename(net_boot_file_name, env_get("bootfile"),
211 sizeof(net_boot_file_name));
212 break;
213
214 case 2: /*
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.
219 */
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));
226 } else {
227 net_boot_file_name_explicit = true;
228 copy_filename(net_boot_file_name, argv[1],
229 sizeof(net_boot_file_name));
230 }
231 break;
232
233 case 3:
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));
238
239 break;
240
241 #ifdef CONFIG_CMD_TFTPPUT
242 case 4:
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;
247 }
248 net_boot_file_name_explicit = true;
249 copy_filename(net_boot_file_name, argv[3],
250 sizeof(net_boot_file_name));
251 break;
252 #endif
253 default:
254 bootstage_error(BOOTSTAGE_ID_NET_START);
255 return CMD_RET_USAGE;
256 }
257 bootstage_mark(BOOTSTAGE_ID_NET_START);
258
259 size = net_loop(proto);
260 if (size < 0) {
261 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
262 return CMD_RET_FAILURE;
263 }
264 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
265
266 /* net_loop ok, update environment */
267 netboot_update_env();
268
269 /* done if no file was loaded (no errors though) */
270 if (size == 0) {
271 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
272 return CMD_RET_SUCCESS;
273 }
274
275 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
276
277 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
278
279 if (rcode == CMD_RET_SUCCESS)
280 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
281 else
282 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
283 return rcode;
284 }
285
286 #if defined(CONFIG_CMD_PING)
287 static int do_ping(struct cmd_tbl *cmdtp, int flag, int argc,
288 char *const argv[])
289 {
290 if (argc < 2)
291 return CMD_RET_USAGE;
292
293 net_ping_ip = string_to_ip(argv[1]);
294 if (net_ping_ip.s_addr == 0)
295 return CMD_RET_USAGE;
296
297 if (net_loop(PING) < 0) {
298 printf("ping failed; host %s is not alive\n", argv[1]);
299 return CMD_RET_FAILURE;
300 }
301
302 printf("host %s is alive\n", argv[1]);
303
304 return CMD_RET_SUCCESS;
305 }
306
307 U_BOOT_CMD(
308 ping, 2, 1, do_ping,
309 "send ICMP ECHO_REQUEST to network host",
310 "pingAddress"
311 );
312 #endif
313
314 #if defined(CONFIG_CMD_CDP)
315
316 static void cdp_update_env(void)
317 {
318 char tmp[16];
319
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;
326 }
327
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;
333 }
334 }
335
336 int do_cdp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
337 {
338 int r;
339
340 r = net_loop(CDP);
341 if (r < 0) {
342 printf("cdp failed; perhaps not a CISCO switch?\n");
343 return CMD_RET_FAILURE;
344 }
345
346 cdp_update_env();
347
348 return CMD_RET_SUCCESS;
349 }
350
351 U_BOOT_CMD(
352 cdp, 1, 1, do_cdp,
353 "Perform CDP network configuration",
354 "\n"
355 );
356 #endif
357
358 #if defined(CONFIG_CMD_SNTP)
359 int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
360 {
361 char *toff;
362
363 if (argc < 2) {
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;
368 }
369 } else {
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;
374 }
375 }
376
377 toff = env_get("timeoffset");
378 if (toff == NULL)
379 net_ntp_time_offset = 0;
380 else
381 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
382
383 if (net_loop(SNTP) < 0) {
384 printf("SNTP failed: host %pI4 not responding\n",
385 &net_ntp_server);
386 return CMD_RET_FAILURE;
387 }
388
389 return CMD_RET_SUCCESS;
390 }
391
392 U_BOOT_CMD(
393 sntp, 2, 1, do_sntp,
394 "synchronize RTC via network",
395 "[NTP server IP]\n"
396 );
397 #endif
398
399 #if defined(CONFIG_CMD_DNS)
400 int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
401 {
402 if (argc == 1)
403 return CMD_RET_USAGE;
404
405 /*
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
413 * permitted
414 * but hey - this is a minimalist implmentation, so only check length
415 * and let the name server deal with things.
416 */
417 if (strlen(argv[1]) >= 255) {
418 printf("dns error: hostname too long\n");
419 return CMD_RET_FAILURE;
420 }
421
422 net_dns_resolve = argv[1];
423
424 if (argc == 3)
425 net_dns_env_var = argv[2];
426 else
427 net_dns_env_var = NULL;
428
429 if (net_loop(DNS) < 0) {
430 printf("dns lookup of %s failed, check setup\n", argv[1]);
431 return CMD_RET_FAILURE;
432 }
433
434 return CMD_RET_SUCCESS;
435 }
436
437 U_BOOT_CMD(
438 dns, 3, 1, do_dns,
439 "lookup the IP of a hostname",
440 "hostname [envvar]"
441 );
442
443 #endif /* CONFIG_CMD_DNS */
444
445 #if defined(CONFIG_CMD_LINK_LOCAL)
446 static int do_link_local(struct cmd_tbl *cmdtp, int flag, int argc,
447 char *const argv[])
448 {
449 char tmp[22];
450
451 if (net_loop(LINKLOCAL) < 0)
452 return CMD_RET_FAILURE;
453
454 net_gateway.s_addr = 0;
455 ip_to_string(net_gateway, tmp);
456 env_set("gatewayip", tmp);
457
458 ip_to_string(net_netmask, tmp);
459 env_set("netmask", tmp);
460
461 ip_to_string(net_ip, tmp);
462 env_set("ipaddr", tmp);
463 env_set("llipaddr", tmp); /* store this for next time */
464
465 return CMD_RET_SUCCESS;
466 }
467
468 U_BOOT_CMD(
469 linklocal, 1, 1, do_link_local,
470 "acquire a network IP address using the link-local protocol",
471 ""
472 );
473
474 #endif /* CONFIG_CMD_LINK_LOCAL */