blob: 75ba1c3cdcece26a8cbf918255918a49cf71f689 [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
Wolfgang Denk6262d0212010-06-28 22:00:46 +020031static int netboot_common (proto_t, cmd_tbl_t *, int , char * const []);
wdenk38635852002-08-27 05:55:31 +000032
Wolfgang Denk6262d0212010-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
wdenkf287a242003-07-01 21:06:45 +000038U_BOOT_CMD(
39 bootp, 3, 1, do_bootp,
Peter Tyserdfb72b82009-01-27 18:03:12 -060040 "boot image via network using BOOTP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020041 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000042);
43
Wolfgang Denk6262d0212010-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{
46 return netboot_common (TFTP, cmdtp, argc, argv);
47}
48
wdenkf287a242003-07-01 21:06:45 +000049U_BOOT_CMD(
50 tftpboot, 3, 1, do_tftpb,
Peter Tyserdfb72b82009-01-27 18:03:12 -060051 "boot image via network using TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020052 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000053);
54
Luca Ceresoli7aa81a42011-05-17 00:03:40 +000055#ifdef CONFIG_CMD_TFTPSRV
56static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
57{
58 return netboot_common(TFTPSRV, cmdtp, argc, argv);
59}
60
61U_BOOT_CMD(
62 tftpsrv, 2, 1, do_tftpsrv,
63 "act as a TFTP server and boot the first received file",
64 "[loadAddress]\n"
65 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
66 "The transfer is aborted if a transfer has not been started after\n"
67 "about 50 seconds or if Ctrl-C is pressed."
68);
69#endif
70
71
Peter Tyserf7a48ca2010-09-30 11:25:48 -050072#ifdef CONFIG_CMD_RARP
Wolfgang Denk6262d0212010-06-28 22:00:46 +020073int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000074{
75 return netboot_common (RARP, cmdtp, argc, argv);
76}
77
wdenkf287a242003-07-01 21:06:45 +000078U_BOOT_CMD(
79 rarpboot, 3, 1, do_rarpb,
Peter Tyserdfb72b82009-01-27 18:03:12 -060080 "boot image via network using RARP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020081 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000082);
Peter Tyserf7a48ca2010-09-30 11:25:48 -050083#endif
wdenk57b2d802003-06-27 21:31:46 +000084
Jon Loeligerd76b5c12007-07-08 18:02:23 -050085#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk6262d0212010-06-28 22:00:46 +020086int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000087{
88 return netboot_common(DHCP, cmdtp, argc, argv);
89}
wdenk57b2d802003-06-27 21:31:46 +000090
wdenkf287a242003-07-01 21:06:45 +000091U_BOOT_CMD(
92 dhcp, 3, 1, do_dhcp,
Peter Tyserdfb72b82009-01-27 18:03:12 -060093 "boot image via network using DHCP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020094 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000095);
Jon Loeligerd704d912007-07-10 11:02:44 -050096#endif
wdenk38635852002-08-27 05:55:31 +000097
Jon Loeligerd76b5c12007-07-08 18:02:23 -050098#if defined(CONFIG_CMD_NFS)
Wolfgang Denk6262d0212010-06-28 22:00:46 +020099int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkbe9c1cb2004-02-24 02:00:03 +0000100{
101 return netboot_common(NFS, cmdtp, argc, argv);
102}
103
104U_BOOT_CMD(
105 nfs, 3, 1, do_nfs,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600106 "boot image via network using NFS protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200107 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenkbe9c1cb2004-02-24 02:00:03 +0000108);
Jon Loeligerd704d912007-07-10 11:02:44 -0500109#endif
wdenkbe9c1cb2004-02-24 02:00:03 +0000110
wdenk05939202004-04-18 17:39:38 +0000111static void netboot_update_env (void)
wdenk38635852002-08-27 05:55:31 +0000112{
wdenk05939202004-04-18 17:39:38 +0000113 char tmp[22];
wdenk38635852002-08-27 05:55:31 +0000114
wdenk05939202004-04-18 17:39:38 +0000115 if (NetOurGatewayIP) {
116 ip_to_string (NetOurGatewayIP, tmp);
117 setenv ("gatewayip", tmp);
118 }
wdenk38635852002-08-27 05:55:31 +0000119
wdenk05939202004-04-18 17:39:38 +0000120 if (NetOurSubnetMask) {
121 ip_to_string (NetOurSubnetMask, tmp);
122 setenv ("netmask", tmp);
123 }
wdenk38635852002-08-27 05:55:31 +0000124
wdenk05939202004-04-18 17:39:38 +0000125 if (NetOurHostName[0])
126 setenv ("hostname", NetOurHostName);
wdenk38635852002-08-27 05:55:31 +0000127
wdenk05939202004-04-18 17:39:38 +0000128 if (NetOurRootPath[0])
129 setenv ("rootpath", NetOurRootPath);
wdenk38635852002-08-27 05:55:31 +0000130
wdenk05939202004-04-18 17:39:38 +0000131 if (NetOurIP) {
132 ip_to_string (NetOurIP, tmp);
133 setenv ("ipaddr", tmp);
134 }
wdenk38635852002-08-27 05:55:31 +0000135
wdenk05939202004-04-18 17:39:38 +0000136 if (NetServerIP) {
137 ip_to_string (NetServerIP, tmp);
138 setenv ("serverip", tmp);
139 }
wdenk38635852002-08-27 05:55:31 +0000140
wdenk05939202004-04-18 17:39:38 +0000141 if (NetOurDNSIP) {
142 ip_to_string (NetOurDNSIP, tmp);
143 setenv ("dnsip", tmp);
144 }
Jon Loeliger5336a762007-07-09 22:08:34 -0500145#if defined(CONFIG_BOOTP_DNS2)
wdenk05939202004-04-18 17:39:38 +0000146 if (NetOurDNS2IP) {
147 ip_to_string (NetOurDNS2IP, tmp);
148 setenv ("dnsip2", tmp);
149 }
stroesee0aadfb2003-08-28 14:17:32 +0000150#endif
wdenk05939202004-04-18 17:39:38 +0000151 if (NetOurNISDomain[0])
152 setenv ("domain", NetOurNISDomain);
wdenkb4ad9622005-04-01 00:25:43 +0000153
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500154#if defined(CONFIG_CMD_SNTP) \
Jon Loeliger5336a762007-07-09 22:08:34 -0500155 && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkb4ad9622005-04-01 00:25:43 +0000156 if (NetTimeOffset) {
157 sprintf (tmp, "%d", NetTimeOffset);
158 setenv ("timeoffset", tmp);
159 }
160#endif
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500161#if defined(CONFIG_CMD_SNTP) \
Jon Loeliger5336a762007-07-09 22:08:34 -0500162 && defined(CONFIG_BOOTP_NTPSERVER)
wdenkb4ad9622005-04-01 00:25:43 +0000163 if (NetNtpServerIP) {
164 ip_to_string (NetNtpServerIP, tmp);
165 setenv ("ntpserverip", tmp);
166 }
167#endif
wdenk38635852002-08-27 05:55:31 +0000168}
wdenk05939202004-04-18 17:39:38 +0000169
wdenk38635852002-08-27 05:55:31 +0000170static int
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200171netboot_common (proto_t proto, cmd_tbl_t *cmdtp, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000172{
173 char *s;
Peter Tyser24c297d2008-12-02 12:59:51 -0600174 char *end;
wdenk38635852002-08-27 05:55:31 +0000175 int rcode = 0;
176 int size;
Peter Tyser24c297d2008-12-02 12:59:51 -0600177 ulong addr;
wdenk38635852002-08-27 05:55:31 +0000178
179 /* pre-set load_addr */
180 if ((s = getenv("loadaddr")) != NULL) {
181 load_addr = simple_strtoul(s, NULL, 16);
182 }
183
184 switch (argc) {
185 case 1:
186 break;
187
Peter Tyser24c297d2008-12-02 12:59:51 -0600188 case 2: /*
189 * Only one arg - accept two forms:
190 * Just load address, or just boot file name. The latter
191 * form must be written in a format which can not be
192 * mis-interpreted as a valid number.
wdenk38635852002-08-27 05:55:31 +0000193 */
Peter Tyser24c297d2008-12-02 12:59:51 -0600194 addr = simple_strtoul(argv[1], &end, 16);
195 if (end == (argv[1] + strlen(argv[1])))
196 load_addr = addr;
197 else
198 copy_filename(BootFile, argv[1], sizeof(BootFile));
wdenk38635852002-08-27 05:55:31 +0000199 break;
200
201 case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
202 copy_filename (BootFile, argv[2], sizeof(BootFile));
203
204 break;
205
Wolfgang Denk3b683112010-07-17 01:06:04 +0200206 default:
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200207 show_boot_progress (-80);
Wolfgang Denk3b683112010-07-17 01:06:04 +0200208 return cmd_usage(cmdtp);
wdenk38635852002-08-27 05:55:31 +0000209 }
210
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200211 show_boot_progress (80);
Heiko Schocher633e03a2007-06-22 19:11:54 +0200212 if ((size = NetLoop(proto)) < 0) {
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200213 show_boot_progress (-81);
wdenk38635852002-08-27 05:55:31 +0000214 return 1;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200215 }
wdenk38635852002-08-27 05:55:31 +0000216
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200217 show_boot_progress (81);
wdenk38635852002-08-27 05:55:31 +0000218 /* NetLoop ok, update environment */
219 netboot_update_env();
220
wdenkc217f6d2002-11-11 02:11:37 +0000221 /* done if no file was loaded (no errors though) */
Heiko Schocher633e03a2007-06-22 19:11:54 +0200222 if (size == 0) {
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200223 show_boot_progress (-82);
wdenkc217f6d2002-11-11 02:11:37 +0000224 return 0;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200225 }
wdenkc217f6d2002-11-11 02:11:37 +0000226
wdenk38635852002-08-27 05:55:31 +0000227 /* flush cache */
228 flush_cache(load_addr, size);
229
230 /* Loading ok, check if we should attempt an auto-start */
Wolfgang Denkf4892502011-01-11 20:56:34 +0100231 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == 0)) {
wdenk38635852002-08-27 05:55:31 +0000232 char *local_args[2];
233 local_args[0] = argv[0];
234 local_args[1] = NULL;
235
236 printf ("Automatic boot of image at addr 0x%08lX ...\n",
237 load_addr);
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200238 show_boot_progress (82);
wdenk38635852002-08-27 05:55:31 +0000239 rcode = do_bootm (cmdtp, 0, 1, local_args);
240 }
241
Heiko Schocher633e03a2007-06-22 19:11:54 +0200242 if (rcode < 0)
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200243 show_boot_progress (-83);
Heiko Schocher633e03a2007-06-22 19:11:54 +0200244 else
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200245 show_boot_progress (84);
wdenk38635852002-08-27 05:55:31 +0000246 return rcode;
247}
wdenke6466f62003-06-05 19:27:42 +0000248
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500249#if defined(CONFIG_CMD_PING)
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200250int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenke6466f62003-06-05 19:27:42 +0000251{
252 if (argc < 2)
253 return -1;
254
255 NetPingIP = string_to_ip(argv[1]);
Wolfgang Denk3b683112010-07-17 01:06:04 +0200256 if (NetPingIP == 0)
257 return cmd_usage(cmdtp);
wdenke6466f62003-06-05 19:27:42 +0000258
259 if (NetLoop(PING) < 0) {
260 printf("ping failed; host %s is not alive\n", argv[1]);
261 return 1;
262 }
263
264 printf("host %s is alive\n", argv[1]);
265
266 return 0;
267}
wdenkca9bc762003-07-15 07:45:49 +0000268
269U_BOOT_CMD(
270 ping, 2, 1, do_ping,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600271 "send ICMP ECHO_REQUEST to network host",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200272 "pingAddress"
wdenkca9bc762003-07-15 07:45:49 +0000273);
Jon Loeligerd704d912007-07-10 11:02:44 -0500274#endif
wdenk38635852002-08-27 05:55:31 +0000275
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500276#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000277
278static void cdp_update_env(void)
279{
280 char tmp[16];
281
282 if (CDPApplianceVLAN != htons(-1)) {
283 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
284 VLAN_to_string(CDPApplianceVLAN, tmp);
285 setenv("vlan", tmp);
286 NetOurVLAN = CDPApplianceVLAN;
287 }
288
289 if (CDPNativeVLAN != htons(-1)) {
290 printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
291 VLAN_to_string(CDPNativeVLAN, tmp);
292 setenv("nvlan", tmp);
293 NetOurNativeVLAN = CDPNativeVLAN;
294 }
295
296}
297
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200298int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk145d2c12004-04-15 21:48:45 +0000299{
300 int r;
301
302 r = NetLoop(CDP);
303 if (r < 0) {
304 printf("cdp failed; perhaps not a CISCO switch?\n");
305 return 1;
306 }
307
308 cdp_update_env();
309
310 return 0;
311}
312
313U_BOOT_CMD(
314 cdp, 1, 1, do_cdp,
Wolfgang Denkd29afc32010-10-26 23:42:23 +0200315 "Perform CDP network configuration",
Wolfgang Denkf003d922010-12-23 17:02:18 +0100316 "\n"
wdenk145d2c12004-04-15 21:48:45 +0000317);
Jon Loeligerd704d912007-07-10 11:02:44 -0500318#endif
wdenk145d2c12004-04-15 21:48:45 +0000319
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500320#if defined(CONFIG_CMD_SNTP)
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200321int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkb4ad9622005-04-01 00:25:43 +0000322{
323 char *toff;
324
325 if (argc < 2) {
326 NetNtpServerIP = getenv_IPaddr ("ntpserverip");
327 if (NetNtpServerIP == 0) {
328 printf ("ntpserverip not set\n");
329 return (1);
330 }
331 } else {
332 NetNtpServerIP = string_to_ip(argv[1]);
333 if (NetNtpServerIP == 0) {
334 printf ("Bad NTP server IP address\n");
335 return (1);
336 }
337 }
338
339 toff = getenv ("timeoffset");
340 if (toff == NULL) NetTimeOffset = 0;
341 else NetTimeOffset = simple_strtol (toff, NULL, 10);
342
343 if (NetLoop(SNTP) < 0) {
Luuk Paulussen7cb3f892011-05-16 18:27:11 +0000344 printf("SNTP failed: host %pI4 not responding\n",
345 &NetNtpServerIP);
wdenkb4ad9622005-04-01 00:25:43 +0000346 return 1;
347 }
348
349 return 0;
350}
351
352U_BOOT_CMD(
353 sntp, 2, 1, do_sntp,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600354 "synchronize RTC via network",
wdenkb4ad9622005-04-01 00:25:43 +0000355 "[NTP server IP]\n"
356);
Jon Loeligerd704d912007-07-10 11:02:44 -0500357#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400358
359#if defined(CONFIG_CMD_DNS)
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200360int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Robin Getz82f0d232009-07-20 14:53:54 -0400361{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200362 if (argc == 1)
363 return cmd_usage(cmdtp);
Robin Getz82f0d232009-07-20 14:53:54 -0400364
365 /*
366 * We should check for a valid hostname:
367 * - Each label must be between 1 and 63 characters long
368 * - the entire hostname has a maximum of 255 characters
369 * - only the ASCII letters 'a' through 'z' (case-insensitive),
370 * the digits '0' through '9', and the hyphen
371 * - cannot begin or end with a hyphen
372 * - no other symbols, punctuation characters, or blank spaces are
373 * permitted
374 * but hey - this is a minimalist implmentation, so only check length
375 * and let the name server deal with things.
376 */
377 if (strlen(argv[1]) >= 255) {
378 printf("dns error: hostname too long\n");
379 return 1;
380 }
381
382 NetDNSResolve = argv[1];
383
384 if (argc == 3)
385 NetDNSenvvar = argv[2];
386 else
387 NetDNSenvvar = NULL;
388
389 if (NetLoop(DNS) < 0) {
390 printf("dns lookup of %s failed, check setup\n", argv[1]);
391 return 1;
392 }
393
394 return 0;
395}
396
397U_BOOT_CMD(
398 dns, 3, 1, do_dns,
399 "lookup the IP of a hostname",
400 "hostname [envvar]"
401);
402
403#endif /* CONFIG_CMD_DNS */