]> git.sur5r.net Git - u-boot/blob - common/cmd_net.c
* Code cleanup:
[u-boot] / common / cmd_net.c
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>
29 #include <cmd_autoscript.h>
30 #include <net.h>
31
32 #if (CONFIG_COMMANDS & CFG_CMD_NET)
33
34
35 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
36
37 static int netboot_common (int, cmd_tbl_t *, int , char *[]);
38
39 int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
40 {
41         return netboot_common (BOOTP, cmdtp, argc, argv);
42 }
43
44 cmd_tbl_t U_BOOT_CMD(BOOTP) = MK_CMD_ENTRY(
45         "bootp",        3,      1,      do_bootp,
46         "bootp   - boot image via network using BootP/TFTP protocol\n",
47         "[loadAddress] [bootfilename]\n"
48 );
49
50 int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
51 {
52         return netboot_common (TFTP, cmdtp, argc, argv);
53 }
54
55 cmd_tbl_t U_BOOT_CMD(TFTPB) = MK_CMD_ENTRY(
56         "tftpboot",     3,      1,      do_tftpb,
57         "tftpboot- boot image via network using TFTP protocol\n"
58         "               and env variables ipaddr and serverip\n",
59         "[loadAddress] [bootfilename]\n"
60 );
61
62 int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
63 {
64         return netboot_common (RARP, cmdtp, argc, argv);
65 }
66
67 cmd_tbl_t U_BOOT_CMD(RARPB) = MK_CMD_ENTRY(
68         "rarpboot",     3,      1,      do_rarpb,
69         "rarpboot- boot image via network using RARP/TFTP protocol\n",
70         "[loadAddress] [bootfilename]\n"
71 );
72
73 #if (CONFIG_COMMANDS & CFG_CMD_DHCP)
74 int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
75 {
76         return netboot_common(DHCP, cmdtp, argc, argv);
77 }
78
79 cmd_tbl_t U_BOOT_CMD(DHCP) = MK_CMD_ENTRY(
80         "dhcp", 3,      1,      do_dhcp,
81         "dhcp    - invoke DHCP client to obtain IP/boot params\n",
82         "\n"
83 );
84 #endif  /* CFG_CMD_DHCP */
85
86 static void netboot_update_env(void)
87 {
88     char tmp[16] ;
89
90     if (NetOurGatewayIP) {
91         ip_to_string (NetOurGatewayIP, tmp);
92         setenv("gatewayip", tmp);
93     }
94
95     if (NetOurSubnetMask) {
96         ip_to_string (NetOurSubnetMask, tmp);
97         setenv("netmask", tmp);
98     }
99
100     if (NetOurHostName[0])
101         setenv("hostname", NetOurHostName);
102
103     if (NetOurRootPath[0])
104         setenv("rootpath", NetOurRootPath);
105
106     if (NetOurIP) {
107         ip_to_string (NetOurIP, tmp);
108         setenv("ipaddr", tmp);
109     }
110
111     if (NetServerIP) {
112         ip_to_string (NetServerIP, tmp);
113         setenv("serverip", tmp);
114     }
115
116     if (NetOurDNSIP) {
117         ip_to_string (NetOurDNSIP, tmp);
118         setenv("dnsip", tmp);
119     }
120
121     if (NetOurNISDomain[0])
122         setenv("domain", NetOurNISDomain);
123
124 }
125 static int
126 netboot_common (int proto, cmd_tbl_t *cmdtp, int argc, char *argv[])
127 {
128         char *s;
129         int   rcode = 0;
130         int   size;
131
132         /* pre-set load_addr */
133         if ((s = getenv("loadaddr")) != NULL) {
134                 load_addr = simple_strtoul(s, NULL, 16);
135         }
136
137         switch (argc) {
138         case 1:
139                 break;
140
141         case 2: /* only one arg - accept two forms:
142                  * just load address, or just boot file name.
143                  * The latter form must be written "filename" here.
144                  */
145                 if (argv[1][0] == '"') {        /* just boot filename */
146                         copy_filename (BootFile, argv[1], sizeof(BootFile));
147                 } else {                        /* load address */
148                         load_addr = simple_strtoul(argv[1], NULL, 16);
149                 }
150                 break;
151
152         case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
153                 copy_filename (BootFile, argv[2], sizeof(BootFile));
154
155                 break;
156
157         default: printf ("Usage:\n%s\n", cmdtp->usage);
158                 return 1;
159         }
160
161         if ((size = NetLoop(proto)) < 0)
162                 return 1;
163
164         /* NetLoop ok, update environment */
165         netboot_update_env();
166
167         /* done if no file was loaded (no errors though) */
168         if (size == 0)
169                 return 0;
170
171         /* flush cache */
172         flush_cache(load_addr, size);
173
174         /* Loading ok, check if we should attempt an auto-start */
175         if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == 0)) {
176                 char *local_args[2];
177                 local_args[0] = argv[0];
178                 local_args[1] = NULL;
179
180                 printf ("Automatic boot of image at addr 0x%08lX ...\n",
181                         load_addr);
182                 rcode = do_bootm (cmdtp, 0, 1, local_args);
183         }
184
185 #ifdef CONFIG_AUTOSCRIPT
186         if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
187                 printf("Running autoscript at addr 0x%08lX ...\n", load_addr);
188                 rcode = autoscript (load_addr);
189         }
190 #endif
191         return rcode;
192 }
193
194 #if (CONFIG_COMMANDS & CFG_CMD_PING)
195 int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
196 {
197         if (argc < 2)
198                 return -1;
199
200         NetPingIP = string_to_ip(argv[1]);
201         if (NetPingIP == 0) {
202                 printf ("Usage:\n%s\n", cmdtp->usage);
203                 return -1;
204         }
205
206         if (NetLoop(PING) < 0) {
207                 printf("ping failed; host %s is not alive\n", argv[1]);
208                 return 1;
209         }
210
211         printf("host %s is alive\n", argv[1]);
212
213         return 0;
214 }
215 #endif  /* CFG_CMD_PING */
216
217 #endif  /* CFG_CMD_NET */