blob: f1097a9951d368b9e1e5ac44cb6dba275405574c [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
31#if (CONFIG_COMMANDS & CFG_CMD_NET)
32
wdenk38635852002-08-27 05:55:31 +000033extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
34
wdenke085e5b2005-04-05 23:32:21 +000035static int netboot_common (proto_t, cmd_tbl_t *, int , char *[]);
wdenk38635852002-08-27 05:55:31 +000036
37int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
38{
39 return netboot_common (BOOTP, cmdtp, argc, argv);
40}
41
wdenkf287a242003-07-01 21:06:45 +000042U_BOOT_CMD(
43 bootp, 3, 1, do_bootp,
wdenk6568eaa2004-06-09 12:42:26 +000044 "bootp\t- boot image via network using BootP/TFTP protocol\n",
wdenk57b2d802003-06-27 21:31:46 +000045 "[loadAddress] [bootfilename]\n"
46);
47
wdenk38635852002-08-27 05:55:31 +000048int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
49{
50 return netboot_common (TFTP, cmdtp, argc, argv);
51}
52
wdenkf287a242003-07-01 21:06:45 +000053U_BOOT_CMD(
54 tftpboot, 3, 1, do_tftpb,
wdenkbc160082003-06-28 23:18:28 +000055 "tftpboot- boot image via network using TFTP protocol\n",
wdenk57b2d802003-06-27 21:31:46 +000056 "[loadAddress] [bootfilename]\n"
57);
58
wdenk38635852002-08-27 05:55:31 +000059int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
60{
61 return netboot_common (RARP, cmdtp, argc, argv);
62}
63
wdenkf287a242003-07-01 21:06:45 +000064U_BOOT_CMD(
65 rarpboot, 3, 1, do_rarpb,
wdenk57b2d802003-06-27 21:31:46 +000066 "rarpboot- boot image via network using RARP/TFTP protocol\n",
67 "[loadAddress] [bootfilename]\n"
68);
69
wdenk38635852002-08-27 05:55:31 +000070#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
71int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
72{
73 return netboot_common(DHCP, cmdtp, argc, argv);
74}
wdenk57b2d802003-06-27 21:31:46 +000075
wdenkf287a242003-07-01 21:06:45 +000076U_BOOT_CMD(
77 dhcp, 3, 1, do_dhcp,
wdenk6568eaa2004-06-09 12:42:26 +000078 "dhcp\t- invoke DHCP client to obtain IP/boot params\n",
wdenk57b2d802003-06-27 21:31:46 +000079 "\n"
80);
wdenk38635852002-08-27 05:55:31 +000081#endif /* CFG_CMD_DHCP */
82
wdenkbe9c1cb2004-02-24 02:00:03 +000083#if (CONFIG_COMMANDS & CFG_CMD_NFS)
84int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
85{
86 return netboot_common(NFS, cmdtp, argc, argv);
87}
88
89U_BOOT_CMD(
90 nfs, 3, 1, do_nfs,
wdenk6568eaa2004-06-09 12:42:26 +000091 "nfs\t- boot image via network using NFS protocol\n",
wdenkbe9c1cb2004-02-24 02:00:03 +000092 "[loadAddress] [host ip addr:bootfilename]\n"
93);
94#endif /* CFG_CMD_NFS */
95
wdenk05939202004-04-18 17:39:38 +000096static void netboot_update_env (void)
wdenk38635852002-08-27 05:55:31 +000097{
wdenk05939202004-04-18 17:39:38 +000098 char tmp[22];
wdenk38635852002-08-27 05:55:31 +000099
wdenk05939202004-04-18 17:39:38 +0000100 if (NetOurGatewayIP) {
101 ip_to_string (NetOurGatewayIP, tmp);
102 setenv ("gatewayip", tmp);
103 }
wdenk38635852002-08-27 05:55:31 +0000104
wdenk05939202004-04-18 17:39:38 +0000105 if (NetOurSubnetMask) {
106 ip_to_string (NetOurSubnetMask, tmp);
107 setenv ("netmask", tmp);
108 }
wdenk38635852002-08-27 05:55:31 +0000109
wdenk05939202004-04-18 17:39:38 +0000110 if (NetOurHostName[0])
111 setenv ("hostname", NetOurHostName);
wdenk38635852002-08-27 05:55:31 +0000112
wdenk05939202004-04-18 17:39:38 +0000113 if (NetOurRootPath[0])
114 setenv ("rootpath", NetOurRootPath);
wdenk38635852002-08-27 05:55:31 +0000115
wdenk05939202004-04-18 17:39:38 +0000116 if (NetOurIP) {
117 ip_to_string (NetOurIP, tmp);
118 setenv ("ipaddr", tmp);
119 }
wdenk38635852002-08-27 05:55:31 +0000120
wdenk05939202004-04-18 17:39:38 +0000121 if (NetServerIP) {
122 ip_to_string (NetServerIP, tmp);
123 setenv ("serverip", tmp);
124 }
wdenk38635852002-08-27 05:55:31 +0000125
wdenk05939202004-04-18 17:39:38 +0000126 if (NetOurDNSIP) {
127 ip_to_string (NetOurDNSIP, tmp);
128 setenv ("dnsip", tmp);
129 }
stroesee0aadfb2003-08-28 14:17:32 +0000130#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
wdenk05939202004-04-18 17:39:38 +0000131 if (NetOurDNS2IP) {
132 ip_to_string (NetOurDNS2IP, tmp);
133 setenv ("dnsip2", tmp);
134 }
stroesee0aadfb2003-08-28 14:17:32 +0000135#endif
wdenk05939202004-04-18 17:39:38 +0000136 if (NetOurNISDomain[0])
137 setenv ("domain", NetOurNISDomain);
wdenkb4ad9622005-04-01 00:25:43 +0000138
wdenk8d5d28a2005-04-02 22:37:54 +0000139#if (CONFIG_COMMANDS & CFG_CMD_SNTP) && (CONFIG_BOOTP_MASK & CONFIG_BOOTP_TIMEOFFSET)
wdenkb4ad9622005-04-01 00:25:43 +0000140 if (NetTimeOffset) {
141 sprintf (tmp, "%d", NetTimeOffset);
142 setenv ("timeoffset", tmp);
143 }
144#endif
wdenk8d5d28a2005-04-02 22:37:54 +0000145#if (CONFIG_COMMANDS & CFG_CMD_SNTP) && (CONFIG_BOOTP_MASK & CONFIG_BOOTP_NTPSERVER)
wdenkb4ad9622005-04-01 00:25:43 +0000146 if (NetNtpServerIP) {
147 ip_to_string (NetNtpServerIP, tmp);
148 setenv ("ntpserverip", tmp);
149 }
150#endif
wdenk38635852002-08-27 05:55:31 +0000151}
wdenk05939202004-04-18 17:39:38 +0000152
wdenk38635852002-08-27 05:55:31 +0000153static int
wdenke085e5b2005-04-05 23:32:21 +0000154netboot_common (proto_t proto, cmd_tbl_t *cmdtp, int argc, char *argv[])
wdenk38635852002-08-27 05:55:31 +0000155{
156 char *s;
157 int rcode = 0;
158 int size;
159
160 /* pre-set load_addr */
161 if ((s = getenv("loadaddr")) != NULL) {
162 load_addr = simple_strtoul(s, NULL, 16);
163 }
164
165 switch (argc) {
166 case 1:
167 break;
168
169 case 2: /* only one arg - accept two forms:
170 * just load address, or just boot file name.
171 * The latter form must be written "filename" here.
172 */
173 if (argv[1][0] == '"') { /* just boot filename */
174 copy_filename (BootFile, argv[1], sizeof(BootFile));
175 } else { /* load address */
176 load_addr = simple_strtoul(argv[1], NULL, 16);
177 }
178 break;
179
180 case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
181 copy_filename (BootFile, argv[2], sizeof(BootFile));
182
183 break;
184
185 default: printf ("Usage:\n%s\n", cmdtp->usage);
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200186 show_boot_progress (-80);
wdenk38635852002-08-27 05:55:31 +0000187 return 1;
188 }
189
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200190 show_boot_progress (80);
Heiko Schocher633e03a2007-06-22 19:11:54 +0200191 if ((size = NetLoop(proto)) < 0) {
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200192 show_boot_progress (-81);
wdenk38635852002-08-27 05:55:31 +0000193 return 1;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200194 }
wdenk38635852002-08-27 05:55:31 +0000195
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200196 show_boot_progress (81);
wdenk38635852002-08-27 05:55:31 +0000197 /* NetLoop ok, update environment */
198 netboot_update_env();
199
wdenkc217f6d2002-11-11 02:11:37 +0000200 /* done if no file was loaded (no errors though) */
Heiko Schocher633e03a2007-06-22 19:11:54 +0200201 if (size == 0) {
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200202 show_boot_progress (-82);
wdenkc217f6d2002-11-11 02:11:37 +0000203 return 0;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200204 }
wdenkc217f6d2002-11-11 02:11:37 +0000205
wdenk38635852002-08-27 05:55:31 +0000206 /* flush cache */
207 flush_cache(load_addr, size);
208
209 /* Loading ok, check if we should attempt an auto-start */
210 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == 0)) {
211 char *local_args[2];
212 local_args[0] = argv[0];
213 local_args[1] = NULL;
214
215 printf ("Automatic boot of image at addr 0x%08lX ...\n",
216 load_addr);
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200217 show_boot_progress (82);
wdenk38635852002-08-27 05:55:31 +0000218 rcode = do_bootm (cmdtp, 0, 1, local_args);
219 }
220
221#ifdef CONFIG_AUTOSCRIPT
222 if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
223 printf("Running autoscript at addr 0x%08lX ...\n", load_addr);
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200224 show_boot_progress (83);
wdenk38635852002-08-27 05:55:31 +0000225 rcode = autoscript (load_addr);
226 }
227#endif
Heiko Schocher633e03a2007-06-22 19:11:54 +0200228 if (rcode < 0)
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200229 show_boot_progress (-83);
Heiko Schocher633e03a2007-06-22 19:11:54 +0200230 else
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200231 show_boot_progress (84);
wdenk38635852002-08-27 05:55:31 +0000232 return rcode;
233}
wdenke6466f62003-06-05 19:27:42 +0000234
235#if (CONFIG_COMMANDS & CFG_CMD_PING)
236int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
237{
238 if (argc < 2)
239 return -1;
240
241 NetPingIP = string_to_ip(argv[1]);
242 if (NetPingIP == 0) {
243 printf ("Usage:\n%s\n", cmdtp->usage);
244 return -1;
245 }
246
247 if (NetLoop(PING) < 0) {
248 printf("ping failed; host %s is not alive\n", argv[1]);
249 return 1;
250 }
251
252 printf("host %s is alive\n", argv[1]);
253
254 return 0;
255}
wdenkca9bc762003-07-15 07:45:49 +0000256
257U_BOOT_CMD(
258 ping, 2, 1, do_ping,
wdenk6568eaa2004-06-09 12:42:26 +0000259 "ping\t- send ICMP ECHO_REQUEST to network host\n",
wdenkca9bc762003-07-15 07:45:49 +0000260 "pingAddress\n"
261);
wdenke6466f62003-06-05 19:27:42 +0000262#endif /* CFG_CMD_PING */
wdenk38635852002-08-27 05:55:31 +0000263
wdenk145d2c12004-04-15 21:48:45 +0000264#if (CONFIG_COMMANDS & CFG_CMD_CDP)
265
266static void cdp_update_env(void)
267{
268 char tmp[16];
269
270 if (CDPApplianceVLAN != htons(-1)) {
271 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
272 VLAN_to_string(CDPApplianceVLAN, tmp);
273 setenv("vlan", tmp);
274 NetOurVLAN = CDPApplianceVLAN;
275 }
276
277 if (CDPNativeVLAN != htons(-1)) {
278 printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
279 VLAN_to_string(CDPNativeVLAN, tmp);
280 setenv("nvlan", tmp);
281 NetOurNativeVLAN = CDPNativeVLAN;
282 }
283
284}
285
286int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
287{
288 int r;
289
290 r = NetLoop(CDP);
291 if (r < 0) {
292 printf("cdp failed; perhaps not a CISCO switch?\n");
293 return 1;
294 }
295
296 cdp_update_env();
297
298 return 0;
299}
300
301U_BOOT_CMD(
302 cdp, 1, 1, do_cdp,
wdenk6568eaa2004-06-09 12:42:26 +0000303 "cdp\t- Perform CDP network configuration\n",
wdenk145d2c12004-04-15 21:48:45 +0000304);
305#endif /* CFG_CMD_CDP */
306
wdenkb4ad9622005-04-01 00:25:43 +0000307#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
308int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
309{
310 char *toff;
311
312 if (argc < 2) {
313 NetNtpServerIP = getenv_IPaddr ("ntpserverip");
314 if (NetNtpServerIP == 0) {
315 printf ("ntpserverip not set\n");
316 return (1);
317 }
318 } else {
319 NetNtpServerIP = string_to_ip(argv[1]);
320 if (NetNtpServerIP == 0) {
321 printf ("Bad NTP server IP address\n");
322 return (1);
323 }
324 }
325
326 toff = getenv ("timeoffset");
327 if (toff == NULL) NetTimeOffset = 0;
328 else NetTimeOffset = simple_strtol (toff, NULL, 10);
329
330 if (NetLoop(SNTP) < 0) {
331 printf("SNTP failed: host %s not responding\n", argv[1]);
332 return 1;
333 }
334
335 return 0;
336}
337
338U_BOOT_CMD(
339 sntp, 2, 1, do_sntp,
340 "sntp\t- synchronize RTC via network\n",
341 "[NTP server IP]\n"
342);
343#endif /* CFG_CMD_SNTP */
344
wdenk38635852002-08-27 05:55:31 +0000345#endif /* CFG_CMD_NET */