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