blob: 65f32bceef41de6e7b953752f2e92bf727e2d315 [file] [log] [blame]
wdenk38635852002-08-27 05:55:31 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24/*
25 * Boot support
26 */
27#include <common.h>
28#include <command.h>
wdenk38635852002-08-27 05:55:31 +000029#include <net.h>
30
Simon Glasse4bf0c52011-10-24 18:00:02 +000031static int netboot_common(enum proto_t, cmd_tbl_t *, int, char * const []);
wdenk38635852002-08-27 05:55:31 +000032
Wolfgang Denk54841ab2010-06-28 22:00:46 +020033int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000034{
35 return netboot_common (BOOTP, cmdtp, argc, argv);
36}
37
wdenk0d498392003-07-01 21:06:45 +000038U_BOOT_CMD(
39 bootp, 3, 1, do_bootp,
Peter Tyser2fb26042009-01-27 18:03:12 -060040 "boot image via network using BOOTP/TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020041 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +000042);
43
Wolfgang Denk54841ab2010-06-28 22:00:46 +020044int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000045{
Simon Glass573f14f2011-12-10 11:08:06 +000046 int ret;
47
48 bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
49 ret = netboot_common(TFTPGET, cmdtp, argc, argv);
50 bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
51 return ret;
wdenk38635852002-08-27 05:55:31 +000052}
53
wdenk0d498392003-07-01 21:06:45 +000054U_BOOT_CMD(
55 tftpboot, 3, 1, do_tftpb,
Peter Tyser2fb26042009-01-27 18:03:12 -060056 "boot image via network using TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020057 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +000058);
59
Simon Glass2d46cf22011-10-24 18:00:08 +000060#ifdef CONFIG_CMD_TFTPPUT
61int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
62{
63 int ret;
64
65 ret = netboot_common(TFTPPUT, cmdtp, argc, argv);
66 return ret;
67}
68
69U_BOOT_CMD(
70 tftpput, 4, 1, do_tftpput,
71 "TFTP put command, for uploading files to a server",
72 "Address Size [[hostIPaddr:]filename]"
73);
74#endif
75
Luca Ceresoli7a83af02011-05-17 00:03:40 +000076#ifdef CONFIG_CMD_TFTPSRV
77static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
78{
79 return netboot_common(TFTPSRV, cmdtp, argc, argv);
80}
81
82U_BOOT_CMD(
83 tftpsrv, 2, 1, do_tftpsrv,
84 "act as a TFTP server and boot the first received file",
85 "[loadAddress]\n"
86 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
87 "The transfer is aborted if a transfer has not been started after\n"
88 "about 50 seconds or if Ctrl-C is pressed."
89);
90#endif
91
92
Peter Tyserbf6cb242010-09-30 11:25:48 -050093#ifdef CONFIG_CMD_RARP
Wolfgang Denk54841ab2010-06-28 22:00:46 +020094int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000095{
96 return netboot_common (RARP, cmdtp, argc, argv);
97}
98
wdenk0d498392003-07-01 21:06:45 +000099U_BOOT_CMD(
100 rarpboot, 3, 1, do_rarpb,
Peter Tyser2fb26042009-01-27 18:03:12 -0600101 "boot image via network using RARP/TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200102 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +0000103);
Peter Tyserbf6cb242010-09-30 11:25:48 -0500104#endif
wdenk8bde7f72003-06-27 21:31:46 +0000105
Jon Loeligerc76fe472007-07-08 18:02:23 -0500106#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200107int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000108{
109 return netboot_common(DHCP, cmdtp, argc, argv);
110}
wdenk8bde7f72003-06-27 21:31:46 +0000111
wdenk0d498392003-07-01 21:06:45 +0000112U_BOOT_CMD(
113 dhcp, 3, 1, do_dhcp,
Peter Tyser2fb26042009-01-27 18:03:12 -0600114 "boot image via network using DHCP/TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200115 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +0000116);
Jon Loeliger90253172007-07-10 11:02:44 -0500117#endif
wdenk38635852002-08-27 05:55:31 +0000118
Jon Loeligerc76fe472007-07-08 18:02:23 -0500119#if defined(CONFIG_CMD_NFS)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200120int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkcbd8a352004-02-24 02:00:03 +0000121{
122 return netboot_common(NFS, cmdtp, argc, argv);
123}
124
125U_BOOT_CMD(
126 nfs, 3, 1, do_nfs,
Peter Tyser2fb26042009-01-27 18:03:12 -0600127 "boot image via network using NFS protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200128 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenkcbd8a352004-02-24 02:00:03 +0000129);
Jon Loeliger90253172007-07-10 11:02:44 -0500130#endif
wdenkcbd8a352004-02-24 02:00:03 +0000131
wdenk6e592382004-04-18 17:39:38 +0000132static void netboot_update_env (void)
wdenk38635852002-08-27 05:55:31 +0000133{
wdenk6e592382004-04-18 17:39:38 +0000134 char tmp[22];
wdenk38635852002-08-27 05:55:31 +0000135
wdenk6e592382004-04-18 17:39:38 +0000136 if (NetOurGatewayIP) {
137 ip_to_string (NetOurGatewayIP, tmp);
138 setenv ("gatewayip", tmp);
139 }
wdenk38635852002-08-27 05:55:31 +0000140
wdenk6e592382004-04-18 17:39:38 +0000141 if (NetOurSubnetMask) {
142 ip_to_string (NetOurSubnetMask, tmp);
143 setenv ("netmask", tmp);
144 }
wdenk38635852002-08-27 05:55:31 +0000145
wdenk6e592382004-04-18 17:39:38 +0000146 if (NetOurHostName[0])
147 setenv ("hostname", NetOurHostName);
wdenk38635852002-08-27 05:55:31 +0000148
wdenk6e592382004-04-18 17:39:38 +0000149 if (NetOurRootPath[0])
150 setenv ("rootpath", NetOurRootPath);
wdenk38635852002-08-27 05:55:31 +0000151
wdenk6e592382004-04-18 17:39:38 +0000152 if (NetOurIP) {
153 ip_to_string (NetOurIP, tmp);
154 setenv ("ipaddr", tmp);
155 }
wdenk38635852002-08-27 05:55:31 +0000156
wdenk6e592382004-04-18 17:39:38 +0000157 if (NetServerIP) {
158 ip_to_string (NetServerIP, tmp);
159 setenv ("serverip", tmp);
160 }
wdenk38635852002-08-27 05:55:31 +0000161
wdenk6e592382004-04-18 17:39:38 +0000162 if (NetOurDNSIP) {
163 ip_to_string (NetOurDNSIP, tmp);
164 setenv ("dnsip", tmp);
165 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500166#if defined(CONFIG_BOOTP_DNS2)
wdenk6e592382004-04-18 17:39:38 +0000167 if (NetOurDNS2IP) {
168 ip_to_string (NetOurDNS2IP, tmp);
169 setenv ("dnsip2", tmp);
170 }
stroesefe389a82003-08-28 14:17:32 +0000171#endif
wdenk6e592382004-04-18 17:39:38 +0000172 if (NetOurNISDomain[0])
173 setenv ("domain", NetOurNISDomain);
wdenkea287de2005-04-01 00:25:43 +0000174
Jon Loeligerc76fe472007-07-08 18:02:23 -0500175#if defined(CONFIG_CMD_SNTP) \
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500176 && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000177 if (NetTimeOffset) {
178 sprintf (tmp, "%d", NetTimeOffset);
179 setenv ("timeoffset", tmp);
180 }
181#endif
Jon Loeligerc76fe472007-07-08 18:02:23 -0500182#if defined(CONFIG_CMD_SNTP) \
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500183 && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000184 if (NetNtpServerIP) {
185 ip_to_string (NetNtpServerIP, tmp);
186 setenv ("ntpserverip", tmp);
187 }
188#endif
wdenk38635852002-08-27 05:55:31 +0000189}
wdenk6e592382004-04-18 17:39:38 +0000190
Simon Glasse4bf0c52011-10-24 18:00:02 +0000191static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
192 char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000193{
194 char *s;
Peter Tyser2e4970d2008-12-02 12:59:51 -0600195 char *end;
wdenk38635852002-08-27 05:55:31 +0000196 int rcode = 0;
197 int size;
Peter Tyser2e4970d2008-12-02 12:59:51 -0600198 ulong addr;
wdenk38635852002-08-27 05:55:31 +0000199
200 /* pre-set load_addr */
201 if ((s = getenv("loadaddr")) != NULL) {
202 load_addr = simple_strtoul(s, NULL, 16);
203 }
204
205 switch (argc) {
206 case 1:
207 break;
208
Peter Tyser2e4970d2008-12-02 12:59:51 -0600209 case 2: /*
210 * Only one arg - accept two forms:
211 * Just load address, or just boot file name. The latter
212 * form must be written in a format which can not be
213 * mis-interpreted as a valid number.
wdenk38635852002-08-27 05:55:31 +0000214 */
Peter Tyser2e4970d2008-12-02 12:59:51 -0600215 addr = simple_strtoul(argv[1], &end, 16);
216 if (end == (argv[1] + strlen(argv[1])))
217 load_addr = addr;
218 else
219 copy_filename(BootFile, argv[1], sizeof(BootFile));
wdenk38635852002-08-27 05:55:31 +0000220 break;
221
222 case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
223 copy_filename (BootFile, argv[2], sizeof(BootFile));
224
225 break;
226
Simon Glass2d46cf22011-10-24 18:00:08 +0000227#ifdef CONFIG_CMD_TFTPPUT
228 case 4:
Simon Glass38bd80b2011-12-19 16:10:43 +0000229 if (strict_strtoul(argv[1], 16, &save_addr) < 0 ||
230 strict_strtoul(argv[2], 16, &save_size) < 0) {
231 printf("Invalid address/size\n");
232 return cmd_usage(cmdtp);
233 }
Simon Glass2d46cf22011-10-24 18:00:08 +0000234 copy_filename(BootFile, argv[3], sizeof(BootFile));
235 break;
236#endif
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200237 default:
Simon Glass770605e2012-02-13 13:51:18 +0000238 bootstage_error(BOOTSTAGE_ID_NET_START);
Simon Glass4c12eeb2011-12-10 08:44:01 +0000239 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000240 }
Simon Glass770605e2012-02-13 13:51:18 +0000241 bootstage_mark(BOOTSTAGE_ID_NET_START);
wdenk38635852002-08-27 05:55:31 +0000242
Heiko Schocher566a4942007-06-22 19:11:54 +0200243 if ((size = NetLoop(proto)) < 0) {
Simon Glass770605e2012-02-13 13:51:18 +0000244 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
wdenk38635852002-08-27 05:55:31 +0000245 return 1;
Heiko Schocher566a4942007-06-22 19:11:54 +0200246 }
Simon Glass770605e2012-02-13 13:51:18 +0000247 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
wdenk38635852002-08-27 05:55:31 +0000248
249 /* NetLoop ok, update environment */
250 netboot_update_env();
251
wdenkeb9401e2002-11-11 02:11:37 +0000252 /* done if no file was loaded (no errors though) */
Heiko Schocher566a4942007-06-22 19:11:54 +0200253 if (size == 0) {
Simon Glass770605e2012-02-13 13:51:18 +0000254 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
wdenkeb9401e2002-11-11 02:11:37 +0000255 return 0;
Heiko Schocher566a4942007-06-22 19:11:54 +0200256 }
wdenkeb9401e2002-11-11 02:11:37 +0000257
wdenk38635852002-08-27 05:55:31 +0000258 /* flush cache */
259 flush_cache(load_addr, size);
260
Simon Glass770605e2012-02-13 13:51:18 +0000261 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
Simon Glassc8e66db2012-01-14 15:24:52 +0000262
Mike Frysinger67d668b2011-06-05 13:43:02 +0000263 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
wdenk38635852002-08-27 05:55:31 +0000264
Heiko Schocher566a4942007-06-22 19:11:54 +0200265 if (rcode < 0)
Simon Glass770605e2012-02-13 13:51:18 +0000266 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
Heiko Schocher566a4942007-06-22 19:11:54 +0200267 else
Simon Glass770605e2012-02-13 13:51:18 +0000268 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
wdenk38635852002-08-27 05:55:31 +0000269 return rcode;
270}
271
Jon Loeligerc76fe472007-07-08 18:02:23 -0500272#if defined(CONFIG_CMD_PING)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200273int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk73a8b272003-06-05 19:27:42 +0000274{
275 if (argc < 2)
276 return -1;
277
278 NetPingIP = string_to_ip(argv[1]);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200279 if (NetPingIP == 0)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000280 return CMD_RET_USAGE;
wdenk73a8b272003-06-05 19:27:42 +0000281
282 if (NetLoop(PING) < 0) {
283 printf("ping failed; host %s is not alive\n", argv[1]);
284 return 1;
285 }
286
287 printf("host %s is alive\n", argv[1]);
288
289 return 0;
290}
wdenk6dff5522003-07-15 07:45:49 +0000291
292U_BOOT_CMD(
293 ping, 2, 1, do_ping,
Peter Tyser2fb26042009-01-27 18:03:12 -0600294 "send ICMP ECHO_REQUEST to network host",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200295 "pingAddress"
wdenk6dff5522003-07-15 07:45:49 +0000296);
Jon Loeliger90253172007-07-10 11:02:44 -0500297#endif
wdenk73a8b272003-06-05 19:27:42 +0000298
Jon Loeligerc76fe472007-07-08 18:02:23 -0500299#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000300
301static void cdp_update_env(void)
302{
303 char tmp[16];
304
305 if (CDPApplianceVLAN != htons(-1)) {
306 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
307 VLAN_to_string(CDPApplianceVLAN, tmp);
308 setenv("vlan", tmp);
309 NetOurVLAN = CDPApplianceVLAN;
310 }
311
312 if (CDPNativeVLAN != htons(-1)) {
313 printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
314 VLAN_to_string(CDPNativeVLAN, tmp);
315 setenv("nvlan", tmp);
316 NetOurNativeVLAN = CDPNativeVLAN;
317 }
318
319}
320
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200321int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka3d991b2004-04-15 21:48:45 +0000322{
323 int r;
324
325 r = NetLoop(CDP);
326 if (r < 0) {
327 printf("cdp failed; perhaps not a CISCO switch?\n");
328 return 1;
329 }
330
331 cdp_update_env();
332
333 return 0;
334}
335
336U_BOOT_CMD(
337 cdp, 1, 1, do_cdp,
Wolfgang Denkec5c04c2010-10-26 23:42:23 +0200338 "Perform CDP network configuration",
Wolfgang Denk4b582662010-12-23 17:02:18 +0100339 "\n"
wdenka3d991b2004-04-15 21:48:45 +0000340);
Jon Loeliger90253172007-07-10 11:02:44 -0500341#endif
wdenka3d991b2004-04-15 21:48:45 +0000342
Jon Loeligerc76fe472007-07-08 18:02:23 -0500343#if defined(CONFIG_CMD_SNTP)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200344int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkea287de2005-04-01 00:25:43 +0000345{
346 char *toff;
347
348 if (argc < 2) {
349 NetNtpServerIP = getenv_IPaddr ("ntpserverip");
350 if (NetNtpServerIP == 0) {
351 printf ("ntpserverip not set\n");
352 return (1);
353 }
354 } else {
355 NetNtpServerIP = string_to_ip(argv[1]);
356 if (NetNtpServerIP == 0) {
357 printf ("Bad NTP server IP address\n");
358 return (1);
359 }
360 }
361
362 toff = getenv ("timeoffset");
363 if (toff == NULL) NetTimeOffset = 0;
364 else NetTimeOffset = simple_strtol (toff, NULL, 10);
365
366 if (NetLoop(SNTP) < 0) {
Luuk Paulussend6840e32011-05-16 18:27:11 +0000367 printf("SNTP failed: host %pI4 not responding\n",
368 &NetNtpServerIP);
wdenkea287de2005-04-01 00:25:43 +0000369 return 1;
370 }
371
372 return 0;
373}
374
375U_BOOT_CMD(
376 sntp, 2, 1, do_sntp,
Peter Tyser2fb26042009-01-27 18:03:12 -0600377 "synchronize RTC via network",
wdenkea287de2005-04-01 00:25:43 +0000378 "[NTP server IP]\n"
379);
Jon Loeliger90253172007-07-10 11:02:44 -0500380#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400381
382#if defined(CONFIG_CMD_DNS)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200383int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Robin Getz1a32bf42009-07-20 14:53:54 -0400384{
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200385 if (argc == 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000386 return CMD_RET_USAGE;
Robin Getz1a32bf42009-07-20 14:53:54 -0400387
388 /*
389 * We should check for a valid hostname:
390 * - Each label must be between 1 and 63 characters long
391 * - the entire hostname has a maximum of 255 characters
392 * - only the ASCII letters 'a' through 'z' (case-insensitive),
393 * the digits '0' through '9', and the hyphen
394 * - cannot begin or end with a hyphen
395 * - no other symbols, punctuation characters, or blank spaces are
396 * permitted
397 * but hey - this is a minimalist implmentation, so only check length
398 * and let the name server deal with things.
399 */
400 if (strlen(argv[1]) >= 255) {
401 printf("dns error: hostname too long\n");
402 return 1;
403 }
404
405 NetDNSResolve = argv[1];
406
407 if (argc == 3)
408 NetDNSenvvar = argv[2];
409 else
410 NetDNSenvvar = NULL;
411
412 if (NetLoop(DNS) < 0) {
413 printf("dns lookup of %s failed, check setup\n", argv[1]);
414 return 1;
415 }
416
417 return 0;
418}
419
420U_BOOT_CMD(
421 dns, 3, 1, do_dns,
422 "lookup the IP of a hostname",
423 "hostname [envvar]"
424);
425
426#endif /* CONFIG_CMD_DNS */