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