]> Git Repo - u-boot.git/blame - cmd/net.c
am335x_evm: Allow booting from usb-storage device
[u-boot.git] / cmd / net.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
3863585b
WD
2/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, [email protected].
3863585b
WD
5 */
6
7/*
8 * Boot support
9 */
10#include <common.h>
52f24238 11#include <bootstage.h>
3863585b 12#include <command.h>
9fb625ce 13#include <env.h>
8e8ccfe1 14#include <image.h>
3863585b
WD
15#include <net.h>
16
09140113 17static int netboot_common(enum proto_t, struct cmd_tbl *, int, char * const []);
3863585b 18
d7a45eaf 19#ifdef CONFIG_CMD_BOOTP
09140113
SG
20static int do_bootp(struct cmd_tbl *cmdtp, int flag, int argc,
21 char *const argv[])
3863585b 22{
088f1b19 23 return netboot_common(BOOTP, cmdtp, argc, argv);
3863585b
WD
24}
25
0d498393
WD
26U_BOOT_CMD(
27 bootp, 3, 1, do_bootp,
2fb2604d 28 "boot image via network using BOOTP/TFTP protocol",
a89c33db 29 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77 30);
d7a45eaf 31#endif
8bde7f77 32
d7a45eaf 33#ifdef CONFIG_CMD_TFTPBOOT
09140113 34int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
3863585b 35{
573f14fe
SG
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;
3863585b
WD
42}
43
0d498393
WD
44U_BOOT_CMD(
45 tftpboot, 3, 1, do_tftpb,
2fb2604d 46 "boot image via network using TFTP protocol",
a89c33db 47 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77 48);
d7a45eaf 49#endif
8bde7f77 50
2d46cf29 51#ifdef CONFIG_CMD_TFTPPUT
09140113
SG
52static int do_tftpput(struct cmd_tbl *cmdtp, int flag, int argc,
53 char *const argv[])
2d46cf29 54{
85848f03 55 return netboot_common(TFTPPUT, cmdtp, argc, argv);
2d46cf29
SG
56}
57
58U_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
7a83af07 65#ifdef CONFIG_CMD_TFTPSRV
09140113
SG
66static int do_tftpsrv(struct cmd_tbl *cmdtp, int flag, int argc,
67 char *const argv[])
7a83af07
LC
68{
69 return netboot_common(TFTPSRV, cmdtp, argc, argv);
70}
71
72U_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
bf6cb247 83#ifdef CONFIG_CMD_RARP
09140113 84int do_rarpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
3863585b 85{
088f1b19 86 return netboot_common(RARP, cmdtp, argc, argv);
3863585b
WD
87}
88
0d498393
WD
89U_BOOT_CMD(
90 rarpboot, 3, 1, do_rarpb,
2fb2604d 91 "boot image via network using RARP/TFTP protocol",
a89c33db 92 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77 93);
bf6cb247 94#endif
8bde7f77 95
c76fe474 96#if defined(CONFIG_CMD_DHCP)
09140113
SG
97static int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc,
98 char *const argv[])
3863585b
WD
99{
100 return netboot_common(DHCP, cmdtp, argc, argv);
101}
8bde7f77 102
0d498393
WD
103U_BOOT_CMD(
104 dhcp, 3, 1, do_dhcp,
2fb2604d 105 "boot image via network using DHCP/TFTP protocol",
a89c33db 106 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77 107);
90253178 108#endif
3863585b 109
c76fe474 110#if defined(CONFIG_CMD_NFS)
09140113
SG
111static int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc,
112 char *const argv[])
cbd8a35c
WD
113{
114 return netboot_common(NFS, cmdtp, argc, argv);
115}
116
117U_BOOT_CMD(
118 nfs, 3, 1, do_nfs,
2fb2604d 119 "boot image via network using NFS protocol",
a89c33db 120 "[loadAddress] [[hostIPaddr:]bootfilename]"
cbd8a35c 121);
90253178 122#endif
cbd8a35c 123
088f1b19 124static void netboot_update_env(void)
3863585b 125{
6e592385 126 char tmp[22];
3863585b 127
049a95a7
JH
128 if (net_gateway.s_addr) {
129 ip_to_string(net_gateway, tmp);
382bee57 130 env_set("gatewayip", tmp);
6e592385 131 }
3863585b 132
049a95a7
JH
133 if (net_netmask.s_addr) {
134 ip_to_string(net_netmask, tmp);
382bee57 135 env_set("netmask", tmp);
6e592385 136 }
3863585b 137
808f13d8 138#ifdef CONFIG_CMD_BOOTP
586cbe51 139 if (net_hostname[0])
382bee57 140 env_set("hostname", net_hostname);
808f13d8 141#endif
3863585b 142
808f13d8 143#ifdef CONFIG_CMD_BOOTP
586cbe51 144 if (net_root_path[0])
382bee57 145 env_set("rootpath", net_root_path);
808f13d8 146#endif
3863585b 147
049a95a7
JH
148 if (net_ip.s_addr) {
149 ip_to_string(net_ip, tmp);
382bee57 150 env_set("ipaddr", tmp);
6e592385 151 }
a3e1a727
JH
152#if !defined(CONFIG_BOOTP_SERVERIP)
153 /*
ff78ad28 154 * Only attempt to change serverip if net/bootp.c:store_net_params()
a3e1a727
JH
155 * could have set it
156 */
049a95a7
JH
157 if (net_server_ip.s_addr) {
158 ip_to_string(net_server_ip, tmp);
382bee57 159 env_set("serverip", tmp);
6e592385 160 }
a3e1a727 161#endif
049a95a7
JH
162 if (net_dns_server.s_addr) {
163 ip_to_string(net_dns_server, tmp);
382bee57 164 env_set("dnsip", tmp);
6e592385 165 }
1fe80d79 166#if defined(CONFIG_BOOTP_DNS2)
049a95a7
JH
167 if (net_dns_server2.s_addr) {
168 ip_to_string(net_dns_server2, tmp);
382bee57 169 env_set("dnsip2", tmp);
6e592385 170 }
fe389a82 171#endif
808f13d8 172#ifdef CONFIG_CMD_BOOTP
586cbe51 173 if (net_nis_domain[0])
382bee57 174 env_set("domain", net_nis_domain);
808f13d8 175#endif
ea287deb 176
4fd5055f 177#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
bc0571fc
JH
178 if (net_ntp_time_offset) {
179 sprintf(tmp, "%d", net_ntp_time_offset);
382bee57 180 env_set("timeoffset", tmp);
ea287deb
WD
181 }
182#endif
4fd5055f 183#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
049a95a7
JH
184 if (net_ntp_server.s_addr) {
185 ip_to_string(net_ntp_server, tmp);
382bee57 186 env_set("ntpserverip", tmp);
ea287deb
WD
187 }
188#endif
3863585b 189}
6e592385 190
09140113
SG
191static int netboot_common(enum proto_t proto, struct cmd_tbl *cmdtp, int argc,
192 char *const argv[])
3863585b
WD
193{
194 char *s;
2e4970d8 195 char *end;
3863585b
WD
196 int rcode = 0;
197 int size;
2e4970d8 198 ulong addr;
3863585b 199
449312c1
AG
200 net_boot_file_name_explicit = false;
201
bb872dd9 202 /* pre-set image_load_addr */
00caae6d 203 s = env_get("loadaddr");
4fd5055f 204 if (s != NULL)
bb872dd9 205 image_load_addr = simple_strtoul(s, NULL, 16);
3863585b
WD
206
207 switch (argc) {
208 case 1:
f43308fa
JH
209 /* refresh bootfile name from env */
210 copy_filename(net_boot_file_name, env_get("bootfile"),
211 sizeof(net_boot_file_name));
3863585b
WD
212 break;
213
2e4970d8
PT
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.
3863585b 219 */
2e4970d8 220 addr = simple_strtoul(argv[1], &end, 16);
449312c1 221 if (end == (argv[1] + strlen(argv[1]))) {
bb872dd9 222 image_load_addr = addr;
f43308fa
JH
223 /* refresh bootfile name from env */
224 copy_filename(net_boot_file_name, env_get("bootfile"),
225 sizeof(net_boot_file_name));
449312c1
AG
226 } else {
227 net_boot_file_name_explicit = true;
1411157d
JH
228 copy_filename(net_boot_file_name, argv[1],
229 sizeof(net_boot_file_name));
449312c1 230 }
3863585b
WD
231 break;
232
4fd5055f 233 case 3:
bb872dd9 234 image_load_addr = simple_strtoul(argv[1], NULL, 16);
449312c1 235 net_boot_file_name_explicit = true;
1411157d
JH
236 copy_filename(net_boot_file_name, argv[2],
237 sizeof(net_boot_file_name));
3863585b
WD
238
239 break;
240
2d46cf29
SG
241#ifdef CONFIG_CMD_TFTPPUT
242 case 4:
bb872dd9
SG
243 if (strict_strtoul(argv[1], 16, &image_save_addr) < 0 ||
244 strict_strtoul(argv[2], 16, &image_save_size) < 0) {
38bd80b4 245 printf("Invalid address/size\n");
85848f03 246 return CMD_RET_USAGE;
38bd80b4 247 }
449312c1 248 net_boot_file_name_explicit = true;
1411157d
JH
249 copy_filename(net_boot_file_name, argv[3],
250 sizeof(net_boot_file_name));
2d46cf29
SG
251 break;
252#endif
47e26b1b 253 default:
770605e4 254 bootstage_error(BOOTSTAGE_ID_NET_START);
4c12eeb8 255 return CMD_RET_USAGE;
3863585b 256 }
770605e4 257 bootstage_mark(BOOTSTAGE_ID_NET_START);
3863585b 258
bc0571fc 259 size = net_loop(proto);
4fd5055f 260 if (size < 0) {
770605e4 261 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
85848f03 262 return CMD_RET_FAILURE;
566a494f 263 }
770605e4 264 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
3863585b 265
bc0571fc 266 /* net_loop ok, update environment */
3863585b
WD
267 netboot_update_env();
268
eb9401e3 269 /* done if no file was loaded (no errors though) */
566a494f 270 if (size == 0) {
770605e4 271 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
85848f03 272 return CMD_RET_SUCCESS;
566a494f 273 }
eb9401e3 274
770605e4 275 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
c8e66db7 276
67d668bf 277 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
3863585b 278
85848f03 279 if (rcode == CMD_RET_SUCCESS)
770605e4 280 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
85848f03
JH
281 else
282 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
3863585b
WD
283 return rcode;
284}
285
c76fe474 286#if defined(CONFIG_CMD_PING)
09140113
SG
287static int do_ping(struct cmd_tbl *cmdtp, int flag, int argc,
288 char *const argv[])
73a8b27c
WD
289{
290 if (argc < 2)
85848f03 291 return CMD_RET_USAGE;
73a8b27c 292
049a95a7
JH
293 net_ping_ip = string_to_ip(argv[1]);
294 if (net_ping_ip.s_addr == 0)
4c12eeb8 295 return CMD_RET_USAGE;
73a8b27c 296
bc0571fc 297 if (net_loop(PING) < 0) {
73a8b27c 298 printf("ping failed; host %s is not alive\n", argv[1]);
85848f03 299 return CMD_RET_FAILURE;
73a8b27c
WD
300 }
301
302 printf("host %s is alive\n", argv[1]);
303
85848f03 304 return CMD_RET_SUCCESS;
73a8b27c 305}
6dff5529
WD
306
307U_BOOT_CMD(
308 ping, 2, 1, do_ping,
2fb2604d 309 "send ICMP ECHO_REQUEST to network host",
a89c33db 310 "pingAddress"
6dff5529 311);
90253178 312#endif
73a8b27c 313
c76fe474 314#if defined(CONFIG_CMD_CDP)
a3d991bd
WD
315
316static void cdp_update_env(void)
317{
318 char tmp[16];
319
6aede5b7
JH
320 if (cdp_appliance_vlan != htons(-1)) {
321 printf("CDP offered appliance VLAN %d\n",
322 ntohs(cdp_appliance_vlan));
4fd5055f 323 vlan_to_string(cdp_appliance_vlan, tmp);
382bee57 324 env_set("vlan", tmp);
4fd5055f 325 net_our_vlan = cdp_appliance_vlan;
a3d991bd
WD
326 }
327
6aede5b7
JH
328 if (cdp_native_vlan != htons(-1)) {
329 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
4fd5055f 330 vlan_to_string(cdp_native_vlan, tmp);
382bee57 331 env_set("nvlan", tmp);
4fd5055f 332 net_native_vlan = cdp_native_vlan;
a3d991bd 333 }
a3d991bd
WD
334}
335
09140113 336int do_cdp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
a3d991bd
WD
337{
338 int r;
339
bc0571fc 340 r = net_loop(CDP);
a3d991bd
WD
341 if (r < 0) {
342 printf("cdp failed; perhaps not a CISCO switch?\n");
85848f03 343 return CMD_RET_FAILURE;
a3d991bd
WD
344 }
345
346 cdp_update_env();
347
85848f03 348 return CMD_RET_SUCCESS;
a3d991bd
WD
349}
350
351U_BOOT_CMD(
352 cdp, 1, 1, do_cdp,
ec5c04cd 353 "Perform CDP network configuration",
4b58266e 354 "\n"
a3d991bd 355);
90253178 356#endif
a3d991bd 357
c76fe474 358#if defined(CONFIG_CMD_SNTP)
09140113 359int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
ea287deb
WD
360{
361 char *toff;
362
363 if (argc < 2) {
723806cc 364 net_ntp_server = env_get_ip("ntpserverip");
049a95a7 365 if (net_ntp_server.s_addr == 0) {
088f1b19 366 printf("ntpserverip not set\n");
85848f03 367 return CMD_RET_FAILURE;
ea287deb
WD
368 }
369 } else {
049a95a7
JH
370 net_ntp_server = string_to_ip(argv[1]);
371 if (net_ntp_server.s_addr == 0) {
088f1b19 372 printf("Bad NTP server IP address\n");
85848f03 373 return CMD_RET_FAILURE;
ea287deb
WD
374 }
375 }
376
00caae6d 377 toff = env_get("timeoffset");
088f1b19 378 if (toff == NULL)
bc0571fc 379 net_ntp_time_offset = 0;
088f1b19 380 else
bc0571fc 381 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
ea287deb 382
bc0571fc 383 if (net_loop(SNTP) < 0) {
d6840e3d 384 printf("SNTP failed: host %pI4 not responding\n",
4fd5055f 385 &net_ntp_server);
85848f03 386 return CMD_RET_FAILURE;
ea287deb
WD
387 }
388
85848f03 389 return CMD_RET_SUCCESS;
ea287deb
WD
390}
391
392U_BOOT_CMD(
393 sntp, 2, 1, do_sntp,
2fb2604d 394 "synchronize RTC via network",
ea287deb
WD
395 "[NTP server IP]\n"
396);
90253178 397#endif
1a32bf41
RG
398
399#if defined(CONFIG_CMD_DNS)
09140113 400int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
1a32bf41 401{
47e26b1b 402 if (argc == 1)
4c12eeb8 403 return CMD_RET_USAGE;
1a32bf41
RG
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");
85848f03 419 return CMD_RET_FAILURE;
1a32bf41
RG
420 }
421
786eac5f 422 net_dns_resolve = argv[1];
1a32bf41
RG
423
424 if (argc == 3)
786eac5f 425 net_dns_env_var = argv[2];
1a32bf41 426 else
786eac5f 427 net_dns_env_var = NULL;
1a32bf41 428
bc0571fc 429 if (net_loop(DNS) < 0) {
1a32bf41 430 printf("dns lookup of %s failed, check setup\n", argv[1]);
85848f03 431 return CMD_RET_FAILURE;
1a32bf41
RG
432 }
433
85848f03 434 return CMD_RET_SUCCESS;
1a32bf41
RG
435}
436
437U_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 */
d22c338e
JH
444
445#if defined(CONFIG_CMD_LINK_LOCAL)
09140113
SG
446static int do_link_local(struct cmd_tbl *cmdtp, int flag, int argc,
447 char *const argv[])
d22c338e
JH
448{
449 char tmp[22];
450
bc0571fc 451 if (net_loop(LINKLOCAL) < 0)
85848f03 452 return CMD_RET_FAILURE;
d22c338e 453
049a95a7
JH
454 net_gateway.s_addr = 0;
455 ip_to_string(net_gateway, tmp);
382bee57 456 env_set("gatewayip", tmp);
d22c338e 457
049a95a7 458 ip_to_string(net_netmask, tmp);
382bee57 459 env_set("netmask", tmp);
d22c338e 460
049a95a7 461 ip_to_string(net_ip, tmp);
382bee57
SG
462 env_set("ipaddr", tmp);
463 env_set("llipaddr", tmp); /* store this for next time */
d22c338e 464
85848f03 465 return CMD_RET_SUCCESS;
d22c338e
JH
466}
467
468U_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 */
This page took 0.403848 seconds and 4 git commands to generate.