]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_net.c
bootstage: Plumb in bootstage calls for basic operations
[people/ms/u-boot.git] / common / cmd_net.c
CommitLineData
3863585b
WD
1/*
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>
3863585b
WD
29#include <net.h>
30
e4bf0c5c 31static int netboot_common(enum proto_t, cmd_tbl_t *, int, char * const []);
3863585b 32
54841ab5 33int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b
WD
34{
35 return netboot_common (BOOTP, cmdtp, argc, argv);
36}
37
0d498393
WD
38U_BOOT_CMD(
39 bootp, 3, 1, do_bootp,
2fb2604d 40 "boot image via network using BOOTP/TFTP protocol",
a89c33db 41 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77
WD
42);
43
54841ab5 44int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b 45{
573f14fe
SG
46 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;
3863585b
WD
52}
53
0d498393
WD
54U_BOOT_CMD(
55 tftpboot, 3, 1, do_tftpb,
2fb2604d 56 "boot image via network using TFTP protocol",
a89c33db 57 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77
WD
58);
59
2d46cf29
SG
60#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
7a83af07
LC
76#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
bf6cb247 93#ifdef CONFIG_CMD_RARP
54841ab5 94int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b
WD
95{
96 return netboot_common (RARP, cmdtp, argc, argv);
97}
98
0d498393
WD
99U_BOOT_CMD(
100 rarpboot, 3, 1, do_rarpb,
2fb2604d 101 "boot image via network using RARP/TFTP protocol",
a89c33db 102 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77 103);
bf6cb247 104#endif
8bde7f77 105
c76fe474 106#if defined(CONFIG_CMD_DHCP)
54841ab5 107int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b
WD
108{
109 return netboot_common(DHCP, cmdtp, argc, argv);
110}
8bde7f77 111
0d498393
WD
112U_BOOT_CMD(
113 dhcp, 3, 1, do_dhcp,
2fb2604d 114 "boot image via network using DHCP/TFTP protocol",
a89c33db 115 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77 116);
90253178 117#endif
3863585b 118
c76fe474 119#if defined(CONFIG_CMD_NFS)
54841ab5 120int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
cbd8a35c
WD
121{
122 return netboot_common(NFS, cmdtp, argc, argv);
123}
124
125U_BOOT_CMD(
126 nfs, 3, 1, do_nfs,
2fb2604d 127 "boot image via network using NFS protocol",
a89c33db 128 "[loadAddress] [[hostIPaddr:]bootfilename]"
cbd8a35c 129);
90253178 130#endif
cbd8a35c 131
6e592385 132static void netboot_update_env (void)
3863585b 133{
6e592385 134 char tmp[22];
3863585b 135
6e592385
WD
136 if (NetOurGatewayIP) {
137 ip_to_string (NetOurGatewayIP, tmp);
138 setenv ("gatewayip", tmp);
139 }
3863585b 140
6e592385
WD
141 if (NetOurSubnetMask) {
142 ip_to_string (NetOurSubnetMask, tmp);
143 setenv ("netmask", tmp);
144 }
3863585b 145
6e592385
WD
146 if (NetOurHostName[0])
147 setenv ("hostname", NetOurHostName);
3863585b 148
6e592385
WD
149 if (NetOurRootPath[0])
150 setenv ("rootpath", NetOurRootPath);
3863585b 151
6e592385
WD
152 if (NetOurIP) {
153 ip_to_string (NetOurIP, tmp);
154 setenv ("ipaddr", tmp);
155 }
3863585b 156
6e592385
WD
157 if (NetServerIP) {
158 ip_to_string (NetServerIP, tmp);
159 setenv ("serverip", tmp);
160 }
73a8b27c 161
6e592385
WD
162 if (NetOurDNSIP) {
163 ip_to_string (NetOurDNSIP, tmp);
164 setenv ("dnsip", tmp);
165 }
1fe80d79 166#if defined(CONFIG_BOOTP_DNS2)
6e592385
WD
167 if (NetOurDNS2IP) {
168 ip_to_string (NetOurDNS2IP, tmp);
169 setenv ("dnsip2", tmp);
170 }
fe389a82 171#endif
6e592385
WD
172 if (NetOurNISDomain[0])
173 setenv ("domain", NetOurNISDomain);
ea287deb 174
c76fe474 175#if defined(CONFIG_CMD_SNTP) \
1fe80d79 176 && defined(CONFIG_BOOTP_TIMEOFFSET)
ea287deb
WD
177 if (NetTimeOffset) {
178 sprintf (tmp, "%d", NetTimeOffset);
179 setenv ("timeoffset", tmp);
180 }
181#endif
c76fe474 182#if defined(CONFIG_CMD_SNTP) \
1fe80d79 183 && defined(CONFIG_BOOTP_NTPSERVER)
ea287deb
WD
184 if (NetNtpServerIP) {
185 ip_to_string (NetNtpServerIP, tmp);
186 setenv ("ntpserverip", tmp);
187 }
188#endif
3863585b 189}
6e592385 190
e4bf0c5c
SG
191static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
192 char * const argv[])
3863585b
WD
193{
194 char *s;
2e4970d8 195 char *end;
3863585b
WD
196 int rcode = 0;
197 int size;
2e4970d8 198 ulong addr;
3863585b
WD
199
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
2e4970d8
PT
209 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.
3863585b 214 */
2e4970d8
PT
215 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));
3863585b
WD
220 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
2d46cf29
SG
227#ifdef CONFIG_CMD_TFTPPUT
228 case 4:
38bd80b4
SG
229 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 }
2d46cf29
SG
234 copy_filename(BootFile, argv[3], sizeof(BootFile));
235 break;
236#endif
47e26b1b 237 default:
770605e4 238 bootstage_error(BOOTSTAGE_ID_NET_START);
4c12eeb8 239 return CMD_RET_USAGE;
3863585b 240 }
770605e4 241 bootstage_mark(BOOTSTAGE_ID_NET_START);
3863585b 242
566a494f 243 if ((size = NetLoop(proto)) < 0) {
770605e4 244 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
3863585b 245 return 1;
566a494f 246 }
770605e4 247 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
3863585b
WD
248
249 /* NetLoop ok, update environment */
250 netboot_update_env();
251
eb9401e3 252 /* done if no file was loaded (no errors though) */
566a494f 253 if (size == 0) {
770605e4 254 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
eb9401e3 255 return 0;
566a494f 256 }
eb9401e3 257
3863585b
WD
258 /* flush cache */
259 flush_cache(load_addr, size);
260
770605e4 261 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
c8e66db7 262
67d668bf 263 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
3863585b 264
566a494f 265 if (rcode < 0)
770605e4 266 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
566a494f 267 else
770605e4 268 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
3863585b
WD
269 return rcode;
270}
271
c76fe474 272#if defined(CONFIG_CMD_PING)
54841ab5 273int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
73a8b27c
WD
274{
275 if (argc < 2)
276 return -1;
277
278 NetPingIP = string_to_ip(argv[1]);
47e26b1b 279 if (NetPingIP == 0)
4c12eeb8 280 return CMD_RET_USAGE;
73a8b27c
WD
281
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}
6dff5529
WD
291
292U_BOOT_CMD(
293 ping, 2, 1, do_ping,
2fb2604d 294 "send ICMP ECHO_REQUEST to network host",
a89c33db 295 "pingAddress"
6dff5529 296);
90253178 297#endif
73a8b27c 298
c76fe474 299#if defined(CONFIG_CMD_CDP)
a3d991bd
WD
300
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
54841ab5 321int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
a3d991bd
WD
322{
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,
ec5c04cd 338 "Perform CDP network configuration",
4b58266e 339 "\n"
a3d991bd 340);
90253178 341#endif
a3d991bd 342
c76fe474 343#if defined(CONFIG_CMD_SNTP)
54841ab5 344int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
ea287deb
WD
345{
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) {
d6840e3d
LP
367 printf("SNTP failed: host %pI4 not responding\n",
368 &NetNtpServerIP);
ea287deb
WD
369 return 1;
370 }
371
372 return 0;
373}
374
375U_BOOT_CMD(
376 sntp, 2, 1, do_sntp,
2fb2604d 377 "synchronize RTC via network",
ea287deb
WD
378 "[NTP server IP]\n"
379);
90253178 380#endif
1a32bf41
RG
381
382#if defined(CONFIG_CMD_DNS)
54841ab5 383int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1a32bf41 384{
47e26b1b 385 if (argc == 1)
4c12eeb8 386 return CMD_RET_USAGE;
1a32bf41
RG
387
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 */