blob: 4227321871c1d1d3c286620a81ba045c75451a52 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk38635852002-08-27 05:55:31 +00002/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk38635852002-08-27 05:55:31 +00005 */
6
Simon Glass31ef3942023-01-17 10:47:39 -07007#define LOG_CATEGORY UCLASS_ETH
8
wdenk38635852002-08-27 05:55:31 +00009/*
10 * Boot support
11 */
12#include <common.h>
Simon Glass1ea97892020-05-10 11:40:00 -060013#include <bootstage.h>
wdenk38635852002-08-27 05:55:31 +000014#include <command.h>
Tim Harveyc4e71e72021-06-18 15:26:21 -070015#include <dm.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060016#include <env.h>
Simon Glass85f13782019-12-28 10:45:03 -070017#include <image.h>
Simon Glass31ef3942023-01-17 10:47:39 -070018#include <log.h>
wdenk38635852002-08-27 05:55:31 +000019#include <net.h>
Viacheslav Mitrofanov12b95ae2022-12-02 12:18:07 +030020#include <net6.h>
Philippe Reynes2829d992020-09-18 14:13:02 +020021#include <net/udp.h>
22#include <net/sntp.h>
Samuel Mendoza-Jonasfeebd6c2022-08-08 21:46:04 +093023#include <net/ncsi.h>
wdenk38635852002-08-27 05:55:31 +000024
Simon Glassed38aef2020-05-10 11:40:03 -060025static int netboot_common(enum proto_t, struct cmd_tbl *, int, char * const []);
wdenk38635852002-08-27 05:55:31 +000026
Joe Hershbergerb0429462018-04-13 15:26:30 -050027#ifdef CONFIG_CMD_BOOTP
Simon Glassed38aef2020-05-10 11:40:03 -060028static int do_bootp(struct cmd_tbl *cmdtp, int flag, int argc,
29 char *const argv[])
wdenk38635852002-08-27 05:55:31 +000030{
Kim Phillipsdc00a682012-10-29 13:34:31 +000031 return netboot_common(BOOTP, cmdtp, argc, argv);
wdenk38635852002-08-27 05:55:31 +000032}
33
wdenkf287a242003-07-01 21:06:45 +000034U_BOOT_CMD(
35 bootp, 3, 1, do_bootp,
Peter Tyserdfb72b82009-01-27 18:03:12 -060036 "boot image via network using BOOTP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020037 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000038);
Joe Hershbergerb0429462018-04-13 15:26:30 -050039#endif
wdenk57b2d802003-06-27 21:31:46 +000040
Joe Hershbergerb0429462018-04-13 15:26:30 -050041#ifdef CONFIG_CMD_TFTPBOOT
Simon Glassed38aef2020-05-10 11:40:03 -060042int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk38635852002-08-27 05:55:31 +000043{
Simon Glass768cbf02011-12-10 11:08:06 +000044 int ret;
45
46 bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
47 ret = netboot_common(TFTPGET, cmdtp, argc, argv);
48 bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
49 return ret;
wdenk38635852002-08-27 05:55:31 +000050}
51
Viacheslav Mitrofanov12b95ae2022-12-02 12:18:07 +030052#if IS_ENABLED(CONFIG_IPV6)
53U_BOOT_CMD(
54 tftpboot, 4, 1, do_tftpb,
55 "boot image via network using TFTP protocol\n"
56 "To use IPv6 add -ipv6 parameter or use IPv6 hostIPaddr framed "
57 "with [] brackets",
58 "[loadAddress] [[hostIPaddr:]bootfilename] [" USE_IP6_CMD_PARAM "]"
59);
60#else
wdenkf287a242003-07-01 21:06:45 +000061U_BOOT_CMD(
62 tftpboot, 3, 1, do_tftpb,
Heinrich Schuchardt7d289f12022-09-04 09:08:11 +020063 "load file via network using TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020064 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000065);
Joe Hershbergerb0429462018-04-13 15:26:30 -050066#endif
Viacheslav Mitrofanov12b95ae2022-12-02 12:18:07 +030067#endif
wdenk57b2d802003-06-27 21:31:46 +000068
Simon Glass85d96ec2011-10-24 18:00:08 +000069#ifdef CONFIG_CMD_TFTPPUT
Simon Glassed38aef2020-05-10 11:40:03 -060070static int do_tftpput(struct cmd_tbl *cmdtp, int flag, int argc,
71 char *const argv[])
Simon Glass85d96ec2011-10-24 18:00:08 +000072{
Joe Hershbergerba867832015-03-22 17:09:09 -050073 return netboot_common(TFTPPUT, cmdtp, argc, argv);
Simon Glass85d96ec2011-10-24 18:00:08 +000074}
75
76U_BOOT_CMD(
77 tftpput, 4, 1, do_tftpput,
78 "TFTP put command, for uploading files to a server",
79 "Address Size [[hostIPaddr:]filename]"
80);
81#endif
82
Luca Ceresoli7aa81a42011-05-17 00:03:40 +000083#ifdef CONFIG_CMD_TFTPSRV
Simon Glassed38aef2020-05-10 11:40:03 -060084static int do_tftpsrv(struct cmd_tbl *cmdtp, int flag, int argc,
85 char *const argv[])
Luca Ceresoli7aa81a42011-05-17 00:03:40 +000086{
87 return netboot_common(TFTPSRV, cmdtp, argc, argv);
88}
89
90U_BOOT_CMD(
91 tftpsrv, 2, 1, do_tftpsrv,
92 "act as a TFTP server and boot the first received file",
93 "[loadAddress]\n"
94 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
95 "The transfer is aborted if a transfer has not been started after\n"
96 "about 50 seconds or if Ctrl-C is pressed."
97);
98#endif
99
100
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500101#ifdef CONFIG_CMD_RARP
Simon Glassed38aef2020-05-10 11:40:03 -0600102int do_rarpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000103{
Kim Phillipsdc00a682012-10-29 13:34:31 +0000104 return netboot_common(RARP, cmdtp, argc, argv);
wdenk38635852002-08-27 05:55:31 +0000105}
106
wdenkf287a242003-07-01 21:06:45 +0000107U_BOOT_CMD(
108 rarpboot, 3, 1, do_rarpb,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600109 "boot image via network using RARP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200110 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +0000111);
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500112#endif
wdenk57b2d802003-06-27 21:31:46 +0000113
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500114#if defined(CONFIG_CMD_DHCP)
Simon Glassed38aef2020-05-10 11:40:03 -0600115static int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc,
116 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000117{
118 return netboot_common(DHCP, cmdtp, argc, argv);
119}
wdenk57b2d802003-06-27 21:31:46 +0000120
wdenkf287a242003-07-01 21:06:45 +0000121U_BOOT_CMD(
122 dhcp, 3, 1, do_dhcp,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600123 "boot image via network using DHCP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200124 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +0000125);
Simon Glass31ef3942023-01-17 10:47:39 -0700126
127int dhcp_run(ulong addr, const char *fname, bool autoload)
128{
129 char *dhcp_argv[] = {"dhcp", NULL, (char *)fname, NULL};
130 struct cmd_tbl cmdtp = {}; /* dummy */
131 char file_addr[17];
132 int old_autoload;
133 int ret, result;
134
135 log_debug("addr=%lx, fname=%s, autoload=%d\n", addr, fname, autoload);
136 old_autoload = env_get_yesno("autoload");
137 ret = env_set("autoload", autoload ? "y" : "n");
138 if (ret)
139 return log_msg_ret("en1", -EINVAL);
140
141 if (autoload) {
142 sprintf(file_addr, "%lx", addr);
143 dhcp_argv[1] = file_addr;
144 }
145
146 result = do_dhcp(&cmdtp, 0, !autoload ? 1 : fname ? 3 : 2, dhcp_argv);
147
148 ret = env_set("autoload", old_autoload == -1 ? NULL :
149 old_autoload ? "y" : "n");
150 if (ret)
151 return log_msg_ret("en2", -EINVAL);
152
153 if (result)
154 return log_msg_ret("res", -ENOENT);
155
156 return 0;
157}
Jon Loeligerd704d912007-07-10 11:02:44 -0500158#endif
wdenk38635852002-08-27 05:55:31 +0000159
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500160#if defined(CONFIG_CMD_NFS)
Simon Glassed38aef2020-05-10 11:40:03 -0600161static int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc,
162 char *const argv[])
wdenkbe9c1cb2004-02-24 02:00:03 +0000163{
164 return netboot_common(NFS, cmdtp, argc, argv);
165}
166
167U_BOOT_CMD(
168 nfs, 3, 1, do_nfs,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600169 "boot image via network using NFS protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200170 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenkbe9c1cb2004-02-24 02:00:03 +0000171);
Jon Loeligerd704d912007-07-10 11:02:44 -0500172#endif
wdenkbe9c1cb2004-02-24 02:00:03 +0000173
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800174#if defined(CONFIG_CMD_WGET)
175static int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
176{
177 return netboot_common(WGET, cmdtp, argc, argv);
178}
179
180U_BOOT_CMD(
181 wget, 3, 1, do_wget,
182 "boot image via network using HTTP protocol",
183 "[loadAddress] [[hostIPaddr:]path and image name]"
184);
185#endif
186
Kim Phillipsdc00a682012-10-29 13:34:31 +0000187static void netboot_update_env(void)
wdenk38635852002-08-27 05:55:31 +0000188{
wdenk05939202004-04-18 17:39:38 +0000189 char tmp[22];
wdenk38635852002-08-27 05:55:31 +0000190
Joe Hershberger5874dec2015-04-08 01:41:01 -0500191 if (net_gateway.s_addr) {
192 ip_to_string(net_gateway, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600193 env_set("gatewayip", tmp);
wdenk05939202004-04-18 17:39:38 +0000194 }
wdenk38635852002-08-27 05:55:31 +0000195
Joe Hershberger5874dec2015-04-08 01:41:01 -0500196 if (net_netmask.s_addr) {
197 ip_to_string(net_netmask, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600198 env_set("netmask", tmp);
wdenk05939202004-04-18 17:39:38 +0000199 }
wdenk38635852002-08-27 05:55:31 +0000200
Krebs, Olafd9249382020-03-09 14:27:55 +0000201#ifdef CONFIG_CMD_BOOTP
Joe Hershberger6d236432015-04-08 01:41:03 -0500202 if (net_hostname[0])
Simon Glass6a38e412017-08-03 12:22:09 -0600203 env_set("hostname", net_hostname);
Krebs, Olafd9249382020-03-09 14:27:55 +0000204#endif
wdenk38635852002-08-27 05:55:31 +0000205
Krebs, Olafd9249382020-03-09 14:27:55 +0000206#ifdef CONFIG_CMD_BOOTP
Joe Hershberger6d236432015-04-08 01:41:03 -0500207 if (net_root_path[0])
Simon Glass6a38e412017-08-03 12:22:09 -0600208 env_set("rootpath", net_root_path);
Krebs, Olafd9249382020-03-09 14:27:55 +0000209#endif
wdenk38635852002-08-27 05:55:31 +0000210
Joe Hershberger5874dec2015-04-08 01:41:01 -0500211 if (net_ip.s_addr) {
212 ip_to_string(net_ip, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600213 env_set("ipaddr", tmp);
wdenk05939202004-04-18 17:39:38 +0000214 }
Joe Hershbergerd4762042012-05-23 07:59:17 +0000215 /*
Baruch Siachbc713252016-12-27 11:03:29 +0200216 * Only attempt to change serverip if net/bootp.c:store_net_params()
Joe Hershbergerd4762042012-05-23 07:59:17 +0000217 * could have set it
218 */
Simon Glass297fc202021-12-18 11:27:52 -0700219 if (!IS_ENABLED(CONFIG_BOOTP_SERVERIP) && net_server_ip.s_addr) {
Joe Hershberger5874dec2015-04-08 01:41:01 -0500220 ip_to_string(net_server_ip, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600221 env_set("serverip", tmp);
wdenk05939202004-04-18 17:39:38 +0000222 }
Joe Hershberger5874dec2015-04-08 01:41:01 -0500223 if (net_dns_server.s_addr) {
224 ip_to_string(net_dns_server, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600225 env_set("dnsip", tmp);
wdenk05939202004-04-18 17:39:38 +0000226 }
Jon Loeliger5336a762007-07-09 22:08:34 -0500227#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500228 if (net_dns_server2.s_addr) {
229 ip_to_string(net_dns_server2, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600230 env_set("dnsip2", tmp);
wdenk05939202004-04-18 17:39:38 +0000231 }
stroesee0aadfb2003-08-28 14:17:32 +0000232#endif
Krebs, Olafd9249382020-03-09 14:27:55 +0000233#ifdef CONFIG_CMD_BOOTP
Joe Hershberger6d236432015-04-08 01:41:03 -0500234 if (net_nis_domain[0])
Simon Glass6a38e412017-08-03 12:22:09 -0600235 env_set("domain", net_nis_domain);
Krebs, Olafd9249382020-03-09 14:27:55 +0000236#endif
wdenkb4ad9622005-04-01 00:25:43 +0000237
Joe Hershberger013d3872015-04-08 01:41:17 -0500238#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500239 if (net_ntp_time_offset) {
240 sprintf(tmp, "%d", net_ntp_time_offset);
Simon Glass6a38e412017-08-03 12:22:09 -0600241 env_set("timeoffset", tmp);
wdenkb4ad9622005-04-01 00:25:43 +0000242 }
243#endif
Joe Hershberger013d3872015-04-08 01:41:17 -0500244#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500245 if (net_ntp_server.s_addr) {
246 ip_to_string(net_ntp_server, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600247 env_set("ntpserverip", tmp);
wdenkb4ad9622005-04-01 00:25:43 +0000248 }
249#endif
wdenk38635852002-08-27 05:55:31 +0000250}
wdenk05939202004-04-18 17:39:38 +0000251
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000252/**
253 * parse_addr_size() - parse address and size arguments for tftpput
254 *
255 * @argv: command line arguments
256 * Return: 0 on success
257 */
258static int parse_addr_size(char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000259{
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000260 if (strict_strtoul(argv[1], 16, &image_save_addr) < 0 ||
261 strict_strtoul(argv[2], 16, &image_save_size) < 0) {
262 printf("Invalid address/size\n");
263 return CMD_RET_USAGE;
264 }
265 return 0;
266}
Alexander Graff43bf5d2018-06-15 10:29:27 +0200267
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000268/**
269 * parse_args() - parse command line arguments
270 *
271 * @proto: command prototype
272 * @argc: number of arguments
273 * @argv: command line arguments
274 * Return: 0 on success
275 */
276static int parse_args(enum proto_t proto, int argc, char *const argv[])
277{
278 ulong addr;
279 char *end;
wdenk38635852002-08-27 05:55:31 +0000280
281 switch (argc) {
282 case 1:
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000283 if (CONFIG_IS_ENABLED(CMD_TFTPPUT) && proto == TFTPPUT)
284 return 1;
285
Joe Hershberger2e135272018-07-03 19:36:42 -0500286 /* refresh bootfile name from env */
287 copy_filename(net_boot_file_name, env_get("bootfile"),
288 sizeof(net_boot_file_name));
wdenk38635852002-08-27 05:55:31 +0000289 break;
290
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000291 case 2:
292 if (CONFIG_IS_ENABLED(CMD_TFTPPUT) && proto == TFTPPUT)
293 return 1;
294 /*
Peter Tyser24c297d2008-12-02 12:59:51 -0600295 * Only one arg - accept two forms:
296 * Just load address, or just boot file name. The latter
297 * form must be written in a format which can not be
298 * mis-interpreted as a valid number.
wdenk38635852002-08-27 05:55:31 +0000299 */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600300 addr = hextoul(argv[1], &end);
Alexander Graff43bf5d2018-06-15 10:29:27 +0200301 if (end == (argv[1] + strlen(argv[1]))) {
Simon Glass892265d2019-12-28 10:45:02 -0700302 image_load_addr = addr;
Joe Hershberger2e135272018-07-03 19:36:42 -0500303 /* refresh bootfile name from env */
304 copy_filename(net_boot_file_name, env_get("bootfile"),
305 sizeof(net_boot_file_name));
Alexander Graff43bf5d2018-06-15 10:29:27 +0200306 } else {
307 net_boot_file_name_explicit = true;
Joe Hershberger290c8992015-04-08 01:41:02 -0500308 copy_filename(net_boot_file_name, argv[1],
309 sizeof(net_boot_file_name));
Alexander Graff43bf5d2018-06-15 10:29:27 +0200310 }
wdenk38635852002-08-27 05:55:31 +0000311 break;
312
Joe Hershberger013d3872015-04-08 01:41:17 -0500313 case 3:
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000314 if (CONFIG_IS_ENABLED(CMD_TFTPPUT) && proto == TFTPPUT) {
315 if (parse_addr_size(argv))
316 return 1;
317 } else {
318 image_load_addr = hextoul(argv[1], NULL);
319 net_boot_file_name_explicit = true;
320 copy_filename(net_boot_file_name, argv[2],
321 sizeof(net_boot_file_name));
322 }
wdenk38635852002-08-27 05:55:31 +0000323 break;
324
Simon Glass85d96ec2011-10-24 18:00:08 +0000325#ifdef CONFIG_CMD_TFTPPUT
326 case 4:
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000327 if (parse_addr_size(argv))
328 return 1;
Alexander Graff43bf5d2018-06-15 10:29:27 +0200329 net_boot_file_name_explicit = true;
Joe Hershberger290c8992015-04-08 01:41:02 -0500330 copy_filename(net_boot_file_name, argv[3],
331 sizeof(net_boot_file_name));
Simon Glass85d96ec2011-10-24 18:00:08 +0000332 break;
333#endif
Wolfgang Denk3b683112010-07-17 01:06:04 +0200334 default:
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000335 return 1;
336 }
337 return 0;
338}
339
340static int netboot_common(enum proto_t proto, struct cmd_tbl *cmdtp, int argc,
341 char *const argv[])
342{
343 char *s;
344 int rcode = 0;
345 int size;
346
347 net_boot_file_name_explicit = false;
348 *net_boot_file_name = '\0';
349
350 /* pre-set image_load_addr */
351 s = env_get("loadaddr");
352 if (s != NULL)
353 image_load_addr = hextoul(s, NULL);
354
Viacheslav Mitrofanov12b95ae2022-12-02 12:18:07 +0300355 if (IS_ENABLED(CONFIG_IPV6)) {
356 use_ip6 = false;
357
358 /* IPv6 parameter has to be always *last* */
359 if (!strcmp(argv[argc - 1], USE_IP6_CMD_PARAM)) {
360 use_ip6 = true;
361 /* It is a hack not to break switch/case code */
362 --argc;
363 }
364 }
365
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000366 if (parse_args(proto, argc, argv)) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000367 bootstage_error(BOOTSTAGE_ID_NET_START);
Simon Glassa06dfc72011-12-10 08:44:01 +0000368 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000369 }
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000370
Simon Glass0169e6b2012-02-13 13:51:18 +0000371 bootstage_mark(BOOTSTAGE_ID_NET_START);
wdenk38635852002-08-27 05:55:31 +0000372
Viacheslav Mitrofanov12b95ae2022-12-02 12:18:07 +0300373 if (IS_ENABLED(CONFIG_IPV6) && !use_ip6) {
374 char *s, *e;
375 size_t len;
376
377 s = strchr(net_boot_file_name, '[');
378 e = strchr(net_boot_file_name, ']');
379 if (s && e) {
380 len = e - s;
381 if (!string_to_ip6(s + 1, len - 1, &net_server_ip6))
382 use_ip6 = true;
383 }
384 }
385
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500386 size = net_loop(proto);
Joe Hershberger013d3872015-04-08 01:41:17 -0500387 if (size < 0) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000388 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
Joe Hershbergerba867832015-03-22 17:09:09 -0500389 return CMD_RET_FAILURE;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200390 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000391 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
wdenk38635852002-08-27 05:55:31 +0000392
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500393 /* net_loop ok, update environment */
wdenk38635852002-08-27 05:55:31 +0000394 netboot_update_env();
395
wdenkc217f6d2002-11-11 02:11:37 +0000396 /* done if no file was loaded (no errors though) */
Heiko Schocher633e03a2007-06-22 19:11:54 +0200397 if (size == 0) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000398 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
Joe Hershbergerba867832015-03-22 17:09:09 -0500399 return CMD_RET_SUCCESS;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200400 }
wdenkc217f6d2002-11-11 02:11:37 +0000401
Simon Glass0169e6b2012-02-13 13:51:18 +0000402 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
Simon Glass964d1d42012-01-14 15:24:52 +0000403
Mike Frysinger194c2e82011-06-05 13:43:02 +0000404 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
wdenk38635852002-08-27 05:55:31 +0000405
Joe Hershbergerba867832015-03-22 17:09:09 -0500406 if (rcode == CMD_RET_SUCCESS)
Simon Glass0169e6b2012-02-13 13:51:18 +0000407 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
Joe Hershbergerba867832015-03-22 17:09:09 -0500408 else
409 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
wdenk38635852002-08-27 05:55:31 +0000410 return rcode;
411}
wdenke6466f62003-06-05 19:27:42 +0000412
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500413#if defined(CONFIG_CMD_PING)
Simon Glassed38aef2020-05-10 11:40:03 -0600414static int do_ping(struct cmd_tbl *cmdtp, int flag, int argc,
415 char *const argv[])
wdenke6466f62003-06-05 19:27:42 +0000416{
417 if (argc < 2)
Joe Hershbergerba867832015-03-22 17:09:09 -0500418 return CMD_RET_USAGE;
wdenke6466f62003-06-05 19:27:42 +0000419
Joe Hershberger5874dec2015-04-08 01:41:01 -0500420 net_ping_ip = string_to_ip(argv[1]);
421 if (net_ping_ip.s_addr == 0)
Simon Glassa06dfc72011-12-10 08:44:01 +0000422 return CMD_RET_USAGE;
wdenke6466f62003-06-05 19:27:42 +0000423
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500424 if (net_loop(PING) < 0) {
wdenke6466f62003-06-05 19:27:42 +0000425 printf("ping failed; host %s is not alive\n", argv[1]);
Joe Hershbergerba867832015-03-22 17:09:09 -0500426 return CMD_RET_FAILURE;
wdenke6466f62003-06-05 19:27:42 +0000427 }
428
429 printf("host %s is alive\n", argv[1]);
430
Joe Hershbergerba867832015-03-22 17:09:09 -0500431 return CMD_RET_SUCCESS;
wdenke6466f62003-06-05 19:27:42 +0000432}
wdenkca9bc762003-07-15 07:45:49 +0000433
434U_BOOT_CMD(
435 ping, 2, 1, do_ping,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600436 "send ICMP ECHO_REQUEST to network host",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200437 "pingAddress"
wdenkca9bc762003-07-15 07:45:49 +0000438);
Jon Loeligerd704d912007-07-10 11:02:44 -0500439#endif
wdenk38635852002-08-27 05:55:31 +0000440
Viacheslav Mitrofanove03c8aa2022-12-02 12:18:08 +0300441#if IS_ENABLED(CONFIG_CMD_PING6)
442int do_ping6(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
443{
444 if (string_to_ip6(argv[1], strlen(argv[1]), &net_ping_ip6))
445 return CMD_RET_USAGE;
446
447 use_ip6 = true;
448 if (net_loop(PING6) < 0) {
449 use_ip6 = false;
450 printf("ping6 failed; host %pI6c is not alive\n",
451 &net_ping_ip6);
452 return 1;
453 }
454
455 use_ip6 = false;
456 printf("host %pI6c is alive\n", &net_ping_ip6);
457 return 0;
458}
459
460U_BOOT_CMD(
461 ping6, 2, 1, do_ping6,
462 "send ICMPv6 ECHO_REQUEST to network host",
463 "pingAddress"
464);
465#endif /* CONFIG_CMD_PING6 */
466
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500467#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000468
469static void cdp_update_env(void)
470{
471 char tmp[16];
472
Joe Hershberger527336f2015-04-08 01:41:14 -0500473 if (cdp_appliance_vlan != htons(-1)) {
474 printf("CDP offered appliance VLAN %d\n",
475 ntohs(cdp_appliance_vlan));
Joe Hershberger013d3872015-04-08 01:41:17 -0500476 vlan_to_string(cdp_appliance_vlan, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600477 env_set("vlan", tmp);
Joe Hershberger013d3872015-04-08 01:41:17 -0500478 net_our_vlan = cdp_appliance_vlan;
wdenk145d2c12004-04-15 21:48:45 +0000479 }
480
Joe Hershberger527336f2015-04-08 01:41:14 -0500481 if (cdp_native_vlan != htons(-1)) {
482 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
Joe Hershberger013d3872015-04-08 01:41:17 -0500483 vlan_to_string(cdp_native_vlan, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600484 env_set("nvlan", tmp);
Joe Hershberger013d3872015-04-08 01:41:17 -0500485 net_native_vlan = cdp_native_vlan;
wdenk145d2c12004-04-15 21:48:45 +0000486 }
wdenk145d2c12004-04-15 21:48:45 +0000487}
488
Simon Glassed38aef2020-05-10 11:40:03 -0600489int do_cdp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk145d2c12004-04-15 21:48:45 +0000490{
491 int r;
492
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500493 r = net_loop(CDP);
wdenk145d2c12004-04-15 21:48:45 +0000494 if (r < 0) {
495 printf("cdp failed; perhaps not a CISCO switch?\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500496 return CMD_RET_FAILURE;
wdenk145d2c12004-04-15 21:48:45 +0000497 }
498
499 cdp_update_env();
500
Joe Hershbergerba867832015-03-22 17:09:09 -0500501 return CMD_RET_SUCCESS;
wdenk145d2c12004-04-15 21:48:45 +0000502}
503
504U_BOOT_CMD(
505 cdp, 1, 1, do_cdp,
Wolfgang Denkd29afc32010-10-26 23:42:23 +0200506 "Perform CDP network configuration",
Wolfgang Denkf003d922010-12-23 17:02:18 +0100507 "\n"
wdenk145d2c12004-04-15 21:48:45 +0000508);
Jon Loeligerd704d912007-07-10 11:02:44 -0500509#endif
wdenk145d2c12004-04-15 21:48:45 +0000510
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500511#if defined(CONFIG_CMD_SNTP)
Philippe Reynes2829d992020-09-18 14:13:02 +0200512static struct udp_ops sntp_ops = {
513 .prereq = sntp_prereq,
514 .start = sntp_start,
515 .data = NULL,
516};
517
Simon Glassed38aef2020-05-10 11:40:03 -0600518int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenkb4ad9622005-04-01 00:25:43 +0000519{
520 char *toff;
521
522 if (argc < 2) {
Simon Glassda1a1342017-08-03 12:22:15 -0600523 net_ntp_server = env_get_ip("ntpserverip");
Joe Hershberger5874dec2015-04-08 01:41:01 -0500524 if (net_ntp_server.s_addr == 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000525 printf("ntpserverip not set\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500526 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000527 }
528 } else {
Joe Hershberger5874dec2015-04-08 01:41:01 -0500529 net_ntp_server = string_to_ip(argv[1]);
530 if (net_ntp_server.s_addr == 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000531 printf("Bad NTP server IP address\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500532 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000533 }
534 }
535
Simon Glass64b723f2017-08-03 12:22:12 -0600536 toff = env_get("timeoffset");
Kim Phillipsdc00a682012-10-29 13:34:31 +0000537 if (toff == NULL)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500538 net_ntp_time_offset = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000539 else
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500540 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
wdenkb4ad9622005-04-01 00:25:43 +0000541
Philippe Reynes2829d992020-09-18 14:13:02 +0200542 if (udp_loop(&sntp_ops) < 0) {
Luuk Paulussen7cb3f892011-05-16 18:27:11 +0000543 printf("SNTP failed: host %pI4 not responding\n",
Joe Hershberger013d3872015-04-08 01:41:17 -0500544 &net_ntp_server);
Joe Hershbergerba867832015-03-22 17:09:09 -0500545 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000546 }
547
Joe Hershbergerba867832015-03-22 17:09:09 -0500548 return CMD_RET_SUCCESS;
wdenkb4ad9622005-04-01 00:25:43 +0000549}
550
551U_BOOT_CMD(
552 sntp, 2, 1, do_sntp,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600553 "synchronize RTC via network",
wdenkb4ad9622005-04-01 00:25:43 +0000554 "[NTP server IP]\n"
555);
Jon Loeligerd704d912007-07-10 11:02:44 -0500556#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400557
558#if defined(CONFIG_CMD_DNS)
Simon Glassed38aef2020-05-10 11:40:03 -0600559int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Robin Getz82f0d232009-07-20 14:53:54 -0400560{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200561 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000562 return CMD_RET_USAGE;
Robin Getz82f0d232009-07-20 14:53:54 -0400563
564 /*
565 * We should check for a valid hostname:
566 * - Each label must be between 1 and 63 characters long
567 * - the entire hostname has a maximum of 255 characters
568 * - only the ASCII letters 'a' through 'z' (case-insensitive),
569 * the digits '0' through '9', and the hyphen
570 * - cannot begin or end with a hyphen
571 * - no other symbols, punctuation characters, or blank spaces are
572 * permitted
573 * but hey - this is a minimalist implmentation, so only check length
574 * and let the name server deal with things.
575 */
576 if (strlen(argv[1]) >= 255) {
577 printf("dns error: hostname too long\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500578 return CMD_RET_FAILURE;
Robin Getz82f0d232009-07-20 14:53:54 -0400579 }
580
Joe Hershbergerf725e342015-04-08 01:41:15 -0500581 net_dns_resolve = argv[1];
Robin Getz82f0d232009-07-20 14:53:54 -0400582
583 if (argc == 3)
Joe Hershbergerf725e342015-04-08 01:41:15 -0500584 net_dns_env_var = argv[2];
Robin Getz82f0d232009-07-20 14:53:54 -0400585 else
Joe Hershbergerf725e342015-04-08 01:41:15 -0500586 net_dns_env_var = NULL;
Robin Getz82f0d232009-07-20 14:53:54 -0400587
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500588 if (net_loop(DNS) < 0) {
Robin Getz82f0d232009-07-20 14:53:54 -0400589 printf("dns lookup of %s failed, check setup\n", argv[1]);
Joe Hershbergerba867832015-03-22 17:09:09 -0500590 return CMD_RET_FAILURE;
Robin Getz82f0d232009-07-20 14:53:54 -0400591 }
592
Joe Hershbergerba867832015-03-22 17:09:09 -0500593 return CMD_RET_SUCCESS;
Robin Getz82f0d232009-07-20 14:53:54 -0400594}
595
596U_BOOT_CMD(
597 dns, 3, 1, do_dns,
598 "lookup the IP of a hostname",
599 "hostname [envvar]"
600);
601
602#endif /* CONFIG_CMD_DNS */
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000603
604#if defined(CONFIG_CMD_LINK_LOCAL)
Simon Glassed38aef2020-05-10 11:40:03 -0600605static int do_link_local(struct cmd_tbl *cmdtp, int flag, int argc,
606 char *const argv[])
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000607{
608 char tmp[22];
609
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500610 if (net_loop(LINKLOCAL) < 0)
Joe Hershbergerba867832015-03-22 17:09:09 -0500611 return CMD_RET_FAILURE;
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000612
Joe Hershberger5874dec2015-04-08 01:41:01 -0500613 net_gateway.s_addr = 0;
614 ip_to_string(net_gateway, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600615 env_set("gatewayip", tmp);
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000616
Joe Hershberger5874dec2015-04-08 01:41:01 -0500617 ip_to_string(net_netmask, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600618 env_set("netmask", tmp);
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000619
Joe Hershberger5874dec2015-04-08 01:41:01 -0500620 ip_to_string(net_ip, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600621 env_set("ipaddr", tmp);
622 env_set("llipaddr", tmp); /* store this for next time */
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000623
Joe Hershbergerba867832015-03-22 17:09:09 -0500624 return CMD_RET_SUCCESS;
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000625}
626
627U_BOOT_CMD(
628 linklocal, 1, 1, do_link_local,
629 "acquire a network IP address using the link-local protocol",
630 ""
631);
632
633#endif /* CONFIG_CMD_LINK_LOCAL */
Tim Harveyc4e71e72021-06-18 15:26:21 -0700634
Tim Harveyc4e71e72021-06-18 15:26:21 -0700635static int do_net_list(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
636{
637 const struct udevice *current = eth_get_dev();
638 unsigned char env_enetaddr[ARP_HLEN];
639 const struct udevice *dev;
640 struct uclass *uc;
641
642 uclass_id_foreach_dev(UCLASS_ETH, dev, uc) {
643 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
644 printf("eth%d : %s %pM %s\n", dev_seq(dev), dev->name, env_enetaddr,
645 current == dev ? "active" : "");
646 }
647 return CMD_RET_SUCCESS;
648}
649
650static struct cmd_tbl cmd_net[] = {
651 U_BOOT_CMD_MKENT(list, 1, 0, do_net_list, "", ""),
652};
653
654static int do_net(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
655{
656 struct cmd_tbl *cp;
657
658 cp = find_cmd_tbl(argv[1], cmd_net, ARRAY_SIZE(cmd_net));
659
660 /* Drop the net command */
661 argc--;
662 argv++;
663
664 if (!cp || argc > cp->maxargs)
665 return CMD_RET_USAGE;
666 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
667 return CMD_RET_SUCCESS;
668
669 return cp->cmd(cmdtp, flag, argc, argv);
670}
671
672U_BOOT_CMD(
673 net, 2, 1, do_net,
674 "NET sub-system",
675 "list - list available devices\n"
676);
Samuel Mendoza-Jonasfeebd6c2022-08-08 21:46:04 +0930677
678#if defined(CONFIG_CMD_NCSI)
679static int do_ncsi(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
680{
681 if (!phy_interface_is_ncsi() || !ncsi_active()) {
682 printf("Device not configured for NC-SI\n");
683 return CMD_RET_FAILURE;
684 }
685
686 if (net_loop(NCSI) < 0)
687 return CMD_RET_FAILURE;
688
689 return CMD_RET_SUCCESS;
690}
691
692U_BOOT_CMD(
693 ncsi, 1, 1, do_ncsi,
694 "Configure attached NIC via NC-SI",
695 ""
696);
697#endif /* CONFIG_CMD_NCSI */