blob: 6139188bd43f34fb9d518cc16f565cd869aafd72 [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 Glasse4bf0c52011-10-24 18:00:02 +000046 return netboot_common(TFTPGET, cmdtp, argc, argv);
wdenk38635852002-08-27 05:55:31 +000047}
48
wdenk0d498392003-07-01 21:06:45 +000049U_BOOT_CMD(
50 tftpboot, 3, 1, do_tftpb,
Peter Tyser2fb26042009-01-27 18:03:12 -060051 "boot image via network using TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020052 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +000053);
54
Simon Glass2d46cf22011-10-24 18:00:08 +000055#ifdef CONFIG_CMD_TFTPPUT
56int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
57{
58 int ret;
59
60 ret = netboot_common(TFTPPUT, cmdtp, argc, argv);
61 return ret;
62}
63
64U_BOOT_CMD(
65 tftpput, 4, 1, do_tftpput,
66 "TFTP put command, for uploading files to a server",
67 "Address Size [[hostIPaddr:]filename]"
68);
69#endif
70
Luca Ceresoli7a83af02011-05-17 00:03:40 +000071#ifdef CONFIG_CMD_TFTPSRV
72static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
73{
74 return netboot_common(TFTPSRV, cmdtp, argc, argv);
75}
76
77U_BOOT_CMD(
78 tftpsrv, 2, 1, do_tftpsrv,
79 "act as a TFTP server and boot the first received file",
80 "[loadAddress]\n"
81 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
82 "The transfer is aborted if a transfer has not been started after\n"
83 "about 50 seconds or if Ctrl-C is pressed."
84);
85#endif
86
87
Peter Tyserbf6cb242010-09-30 11:25:48 -050088#ifdef CONFIG_CMD_RARP
Wolfgang Denk54841ab2010-06-28 22:00:46 +020089int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000090{
91 return netboot_common (RARP, cmdtp, argc, argv);
92}
93
wdenk0d498392003-07-01 21:06:45 +000094U_BOOT_CMD(
95 rarpboot, 3, 1, do_rarpb,
Peter Tyser2fb26042009-01-27 18:03:12 -060096 "boot image via network using RARP/TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020097 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +000098);
Peter Tyserbf6cb242010-09-30 11:25:48 -050099#endif
wdenk8bde7f72003-06-27 21:31:46 +0000100
Jon Loeligerc76fe472007-07-08 18:02:23 -0500101#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200102int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000103{
104 return netboot_common(DHCP, cmdtp, argc, argv);
105}
wdenk8bde7f72003-06-27 21:31:46 +0000106
wdenk0d498392003-07-01 21:06:45 +0000107U_BOOT_CMD(
108 dhcp, 3, 1, do_dhcp,
Peter Tyser2fb26042009-01-27 18:03:12 -0600109 "boot image via network using DHCP/TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200110 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +0000111);
Jon Loeliger90253172007-07-10 11:02:44 -0500112#endif
wdenk38635852002-08-27 05:55:31 +0000113
Jon Loeligerc76fe472007-07-08 18:02:23 -0500114#if defined(CONFIG_CMD_NFS)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200115int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkcbd8a352004-02-24 02:00:03 +0000116{
117 return netboot_common(NFS, cmdtp, argc, argv);
118}
119
120U_BOOT_CMD(
121 nfs, 3, 1, do_nfs,
Peter Tyser2fb26042009-01-27 18:03:12 -0600122 "boot image via network using NFS protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200123 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenkcbd8a352004-02-24 02:00:03 +0000124);
Jon Loeliger90253172007-07-10 11:02:44 -0500125#endif
wdenkcbd8a352004-02-24 02:00:03 +0000126
wdenk6e592382004-04-18 17:39:38 +0000127static void netboot_update_env (void)
wdenk38635852002-08-27 05:55:31 +0000128{
wdenk6e592382004-04-18 17:39:38 +0000129 char tmp[22];
wdenk38635852002-08-27 05:55:31 +0000130
wdenk6e592382004-04-18 17:39:38 +0000131 if (NetOurGatewayIP) {
132 ip_to_string (NetOurGatewayIP, tmp);
133 setenv ("gatewayip", tmp);
134 }
wdenk38635852002-08-27 05:55:31 +0000135
wdenk6e592382004-04-18 17:39:38 +0000136 if (NetOurSubnetMask) {
137 ip_to_string (NetOurSubnetMask, tmp);
138 setenv ("netmask", tmp);
139 }
wdenk38635852002-08-27 05:55:31 +0000140
wdenk6e592382004-04-18 17:39:38 +0000141 if (NetOurHostName[0])
142 setenv ("hostname", NetOurHostName);
wdenk38635852002-08-27 05:55:31 +0000143
wdenk6e592382004-04-18 17:39:38 +0000144 if (NetOurRootPath[0])
145 setenv ("rootpath", NetOurRootPath);
wdenk38635852002-08-27 05:55:31 +0000146
wdenk6e592382004-04-18 17:39:38 +0000147 if (NetOurIP) {
148 ip_to_string (NetOurIP, tmp);
149 setenv ("ipaddr", tmp);
150 }
wdenk38635852002-08-27 05:55:31 +0000151
wdenk6e592382004-04-18 17:39:38 +0000152 if (NetServerIP) {
153 ip_to_string (NetServerIP, tmp);
154 setenv ("serverip", tmp);
155 }
wdenk38635852002-08-27 05:55:31 +0000156
wdenk6e592382004-04-18 17:39:38 +0000157 if (NetOurDNSIP) {
158 ip_to_string (NetOurDNSIP, tmp);
159 setenv ("dnsip", tmp);
160 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500161#if defined(CONFIG_BOOTP_DNS2)
wdenk6e592382004-04-18 17:39:38 +0000162 if (NetOurDNS2IP) {
163 ip_to_string (NetOurDNS2IP, tmp);
164 setenv ("dnsip2", tmp);
165 }
stroesefe389a82003-08-28 14:17:32 +0000166#endif
wdenk6e592382004-04-18 17:39:38 +0000167 if (NetOurNISDomain[0])
168 setenv ("domain", NetOurNISDomain);
wdenkea287de2005-04-01 00:25:43 +0000169
Jon Loeligerc76fe472007-07-08 18:02:23 -0500170#if defined(CONFIG_CMD_SNTP) \
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500171 && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000172 if (NetTimeOffset) {
173 sprintf (tmp, "%d", NetTimeOffset);
174 setenv ("timeoffset", tmp);
175 }
176#endif
Jon Loeligerc76fe472007-07-08 18:02:23 -0500177#if defined(CONFIG_CMD_SNTP) \
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500178 && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000179 if (NetNtpServerIP) {
180 ip_to_string (NetNtpServerIP, tmp);
181 setenv ("ntpserverip", tmp);
182 }
183#endif
wdenk38635852002-08-27 05:55:31 +0000184}
wdenk6e592382004-04-18 17:39:38 +0000185
Simon Glasse4bf0c52011-10-24 18:00:02 +0000186static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
187 char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000188{
189 char *s;
Peter Tyser2e4970d2008-12-02 12:59:51 -0600190 char *end;
wdenk38635852002-08-27 05:55:31 +0000191 int rcode = 0;
192 int size;
Peter Tyser2e4970d2008-12-02 12:59:51 -0600193 ulong addr;
wdenk38635852002-08-27 05:55:31 +0000194
195 /* pre-set load_addr */
196 if ((s = getenv("loadaddr")) != NULL) {
197 load_addr = simple_strtoul(s, NULL, 16);
198 }
199
200 switch (argc) {
201 case 1:
202 break;
203
Peter Tyser2e4970d2008-12-02 12:59:51 -0600204 case 2: /*
205 * Only one arg - accept two forms:
206 * Just load address, or just boot file name. The latter
207 * form must be written in a format which can not be
208 * mis-interpreted as a valid number.
wdenk38635852002-08-27 05:55:31 +0000209 */
Peter Tyser2e4970d2008-12-02 12:59:51 -0600210 addr = simple_strtoul(argv[1], &end, 16);
211 if (end == (argv[1] + strlen(argv[1])))
212 load_addr = addr;
213 else
214 copy_filename(BootFile, argv[1], sizeof(BootFile));
wdenk38635852002-08-27 05:55:31 +0000215 break;
216
217 case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
218 copy_filename (BootFile, argv[2], sizeof(BootFile));
219
220 break;
221
Simon Glass2d46cf22011-10-24 18:00:08 +0000222#ifdef CONFIG_CMD_TFTPPUT
223 case 4:
Simon Glass38bd80b2011-12-19 16:10:43 +0000224 if (strict_strtoul(argv[1], 16, &save_addr) < 0 ||
225 strict_strtoul(argv[2], 16, &save_size) < 0) {
226 printf("Invalid address/size\n");
227 return cmd_usage(cmdtp);
228 }
Simon Glass2d46cf22011-10-24 18:00:08 +0000229 copy_filename(BootFile, argv[3], sizeof(BootFile));
230 break;
231#endif
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200232 default:
Simon Glass770605e2012-02-13 13:51:18 +0000233 bootstage_error(BOOTSTAGE_ID_NET_START);
Simon Glass4c12eeb2011-12-10 08:44:01 +0000234 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000235 }
Simon Glass770605e2012-02-13 13:51:18 +0000236 bootstage_mark(BOOTSTAGE_ID_NET_START);
wdenk38635852002-08-27 05:55:31 +0000237
Heiko Schocher566a4942007-06-22 19:11:54 +0200238 if ((size = NetLoop(proto)) < 0) {
Simon Glass770605e2012-02-13 13:51:18 +0000239 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
wdenk38635852002-08-27 05:55:31 +0000240 return 1;
Heiko Schocher566a4942007-06-22 19:11:54 +0200241 }
Simon Glass770605e2012-02-13 13:51:18 +0000242 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
wdenk38635852002-08-27 05:55:31 +0000243
244 /* NetLoop ok, update environment */
245 netboot_update_env();
246
wdenkeb9401e2002-11-11 02:11:37 +0000247 /* done if no file was loaded (no errors though) */
Heiko Schocher566a4942007-06-22 19:11:54 +0200248 if (size == 0) {
Simon Glass770605e2012-02-13 13:51:18 +0000249 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
wdenkeb9401e2002-11-11 02:11:37 +0000250 return 0;
Heiko Schocher566a4942007-06-22 19:11:54 +0200251 }
wdenkeb9401e2002-11-11 02:11:37 +0000252
wdenk38635852002-08-27 05:55:31 +0000253 /* flush cache */
254 flush_cache(load_addr, size);
255
Simon Glass770605e2012-02-13 13:51:18 +0000256 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
Simon Glassc8e66db2012-01-14 15:24:52 +0000257
Mike Frysinger67d668b2011-06-05 13:43:02 +0000258 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
wdenk38635852002-08-27 05:55:31 +0000259
Heiko Schocher566a4942007-06-22 19:11:54 +0200260 if (rcode < 0)
Simon Glass770605e2012-02-13 13:51:18 +0000261 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
Heiko Schocher566a4942007-06-22 19:11:54 +0200262 else
Simon Glass770605e2012-02-13 13:51:18 +0000263 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
wdenk38635852002-08-27 05:55:31 +0000264 return rcode;
265}
266
Jon Loeligerc76fe472007-07-08 18:02:23 -0500267#if defined(CONFIG_CMD_PING)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200268int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk73a8b272003-06-05 19:27:42 +0000269{
270 if (argc < 2)
271 return -1;
272
273 NetPingIP = string_to_ip(argv[1]);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200274 if (NetPingIP == 0)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000275 return CMD_RET_USAGE;
wdenk73a8b272003-06-05 19:27:42 +0000276
277 if (NetLoop(PING) < 0) {
278 printf("ping failed; host %s is not alive\n", argv[1]);
279 return 1;
280 }
281
282 printf("host %s is alive\n", argv[1]);
283
284 return 0;
285}
wdenk6dff5522003-07-15 07:45:49 +0000286
287U_BOOT_CMD(
288 ping, 2, 1, do_ping,
Peter Tyser2fb26042009-01-27 18:03:12 -0600289 "send ICMP ECHO_REQUEST to network host",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200290 "pingAddress"
wdenk6dff5522003-07-15 07:45:49 +0000291);
Jon Loeliger90253172007-07-10 11:02:44 -0500292#endif
wdenk73a8b272003-06-05 19:27:42 +0000293
Jon Loeligerc76fe472007-07-08 18:02:23 -0500294#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000295
296static void cdp_update_env(void)
297{
298 char tmp[16];
299
300 if (CDPApplianceVLAN != htons(-1)) {
301 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
302 VLAN_to_string(CDPApplianceVLAN, tmp);
303 setenv("vlan", tmp);
304 NetOurVLAN = CDPApplianceVLAN;
305 }
306
307 if (CDPNativeVLAN != htons(-1)) {
308 printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
309 VLAN_to_string(CDPNativeVLAN, tmp);
310 setenv("nvlan", tmp);
311 NetOurNativeVLAN = CDPNativeVLAN;
312 }
313
314}
315
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200316int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka3d991b2004-04-15 21:48:45 +0000317{
318 int r;
319
320 r = NetLoop(CDP);
321 if (r < 0) {
322 printf("cdp failed; perhaps not a CISCO switch?\n");
323 return 1;
324 }
325
326 cdp_update_env();
327
328 return 0;
329}
330
331U_BOOT_CMD(
332 cdp, 1, 1, do_cdp,
Wolfgang Denkec5c04c2010-10-26 23:42:23 +0200333 "Perform CDP network configuration",
Wolfgang Denk4b582662010-12-23 17:02:18 +0100334 "\n"
wdenka3d991b2004-04-15 21:48:45 +0000335);
Jon Loeliger90253172007-07-10 11:02:44 -0500336#endif
wdenka3d991b2004-04-15 21:48:45 +0000337
Jon Loeligerc76fe472007-07-08 18:02:23 -0500338#if defined(CONFIG_CMD_SNTP)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200339int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkea287de2005-04-01 00:25:43 +0000340{
341 char *toff;
342
343 if (argc < 2) {
344 NetNtpServerIP = getenv_IPaddr ("ntpserverip");
345 if (NetNtpServerIP == 0) {
346 printf ("ntpserverip not set\n");
347 return (1);
348 }
349 } else {
350 NetNtpServerIP = string_to_ip(argv[1]);
351 if (NetNtpServerIP == 0) {
352 printf ("Bad NTP server IP address\n");
353 return (1);
354 }
355 }
356
357 toff = getenv ("timeoffset");
358 if (toff == NULL) NetTimeOffset = 0;
359 else NetTimeOffset = simple_strtol (toff, NULL, 10);
360
361 if (NetLoop(SNTP) < 0) {
Luuk Paulussend6840e32011-05-16 18:27:11 +0000362 printf("SNTP failed: host %pI4 not responding\n",
363 &NetNtpServerIP);
wdenkea287de2005-04-01 00:25:43 +0000364 return 1;
365 }
366
367 return 0;
368}
369
370U_BOOT_CMD(
371 sntp, 2, 1, do_sntp,
Peter Tyser2fb26042009-01-27 18:03:12 -0600372 "synchronize RTC via network",
wdenkea287de2005-04-01 00:25:43 +0000373 "[NTP server IP]\n"
374);
Jon Loeliger90253172007-07-10 11:02:44 -0500375#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400376
377#if defined(CONFIG_CMD_DNS)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200378int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Robin Getz1a32bf42009-07-20 14:53:54 -0400379{
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200380 if (argc == 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000381 return CMD_RET_USAGE;
Robin Getz1a32bf42009-07-20 14:53:54 -0400382
383 /*
384 * We should check for a valid hostname:
385 * - Each label must be between 1 and 63 characters long
386 * - the entire hostname has a maximum of 255 characters
387 * - only the ASCII letters 'a' through 'z' (case-insensitive),
388 * the digits '0' through '9', and the hyphen
389 * - cannot begin or end with a hyphen
390 * - no other symbols, punctuation characters, or blank spaces are
391 * permitted
392 * but hey - this is a minimalist implmentation, so only check length
393 * and let the name server deal with things.
394 */
395 if (strlen(argv[1]) >= 255) {
396 printf("dns error: hostname too long\n");
397 return 1;
398 }
399
400 NetDNSResolve = argv[1];
401
402 if (argc == 3)
403 NetDNSenvvar = argv[2];
404 else
405 NetDNSenvvar = NULL;
406
407 if (NetLoop(DNS) < 0) {
408 printf("dns lookup of %s failed, check setup\n", argv[1]);
409 return 1;
410 }
411
412 return 0;
413}
414
415U_BOOT_CMD(
416 dns, 3, 1, do_dns,
417 "lookup the IP of a hostname",
418 "hostname [envvar]"
419);
420
421#endif /* CONFIG_CMD_DNS */