wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 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> |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 29 | #include <net.h> |
| 30 | |
| 31 | #if (CONFIG_COMMANDS & CFG_CMD_NET) |
| 32 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 33 | |
| 34 | extern int do_bootm (cmd_tbl_t *, int, int, char *[]); |
| 35 | |
wdenk | e085e5b | 2005-04-05 23:32:21 +0000 | [diff] [blame] | 36 | static int netboot_common (proto_t, cmd_tbl_t *, int , char *[]); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 37 | |
| 38 | int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 39 | { |
| 40 | return netboot_common (BOOTP, cmdtp, argc, argv); |
| 41 | } |
| 42 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 43 | U_BOOT_CMD( |
| 44 | bootp, 3, 1, do_bootp, |
wdenk | 6568eaa | 2004-06-09 12:42:26 +0000 | [diff] [blame] | 45 | "bootp\t- boot image via network using BootP/TFTP protocol\n", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 46 | "[loadAddress] [bootfilename]\n" |
| 47 | ); |
| 48 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 49 | int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 50 | { |
| 51 | return netboot_common (TFTP, cmdtp, argc, argv); |
| 52 | } |
| 53 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 54 | U_BOOT_CMD( |
| 55 | tftpboot, 3, 1, do_tftpb, |
wdenk | bc16008 | 2003-06-28 23:18:28 +0000 | [diff] [blame] | 56 | "tftpboot- boot image via network using TFTP protocol\n", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 57 | "[loadAddress] [bootfilename]\n" |
| 58 | ); |
| 59 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 60 | int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 61 | { |
| 62 | return netboot_common (RARP, cmdtp, argc, argv); |
| 63 | } |
| 64 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 65 | U_BOOT_CMD( |
| 66 | rarpboot, 3, 1, do_rarpb, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 67 | "rarpboot- boot image via network using RARP/TFTP protocol\n", |
| 68 | "[loadAddress] [bootfilename]\n" |
| 69 | ); |
| 70 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 71 | #if (CONFIG_COMMANDS & CFG_CMD_DHCP) |
| 72 | int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 73 | { |
| 74 | return netboot_common(DHCP, cmdtp, argc, argv); |
| 75 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 76 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 77 | U_BOOT_CMD( |
| 78 | dhcp, 3, 1, do_dhcp, |
wdenk | 6568eaa | 2004-06-09 12:42:26 +0000 | [diff] [blame] | 79 | "dhcp\t- invoke DHCP client to obtain IP/boot params\n", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 80 | "\n" |
| 81 | ); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 82 | #endif /* CFG_CMD_DHCP */ |
| 83 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 84 | #if (CONFIG_COMMANDS & CFG_CMD_NFS) |
| 85 | int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 86 | { |
| 87 | return netboot_common(NFS, cmdtp, argc, argv); |
| 88 | } |
| 89 | |
| 90 | U_BOOT_CMD( |
| 91 | nfs, 3, 1, do_nfs, |
wdenk | 6568eaa | 2004-06-09 12:42:26 +0000 | [diff] [blame] | 92 | "nfs\t- boot image via network using NFS protocol\n", |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 93 | "[loadAddress] [host ip addr:bootfilename]\n" |
| 94 | ); |
| 95 | #endif /* CFG_CMD_NFS */ |
| 96 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 97 | static void netboot_update_env (void) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 98 | { |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 99 | char tmp[22]; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 100 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 101 | if (NetOurGatewayIP) { |
| 102 | ip_to_string (NetOurGatewayIP, tmp); |
| 103 | setenv ("gatewayip", tmp); |
| 104 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 105 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 106 | if (NetOurSubnetMask) { |
| 107 | ip_to_string (NetOurSubnetMask, tmp); |
| 108 | setenv ("netmask", tmp); |
| 109 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 110 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 111 | if (NetOurHostName[0]) |
| 112 | setenv ("hostname", NetOurHostName); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 113 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 114 | if (NetOurRootPath[0]) |
| 115 | setenv ("rootpath", NetOurRootPath); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 116 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 117 | if (NetOurIP) { |
| 118 | ip_to_string (NetOurIP, tmp); |
| 119 | setenv ("ipaddr", tmp); |
| 120 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 121 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 122 | if (NetServerIP) { |
| 123 | ip_to_string (NetServerIP, tmp); |
| 124 | setenv ("serverip", tmp); |
| 125 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 126 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 127 | if (NetOurDNSIP) { |
| 128 | ip_to_string (NetOurDNSIP, tmp); |
| 129 | setenv ("dnsip", tmp); |
| 130 | } |
stroese | e0aadfb | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 131 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2) |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 132 | if (NetOurDNS2IP) { |
| 133 | ip_to_string (NetOurDNS2IP, tmp); |
| 134 | setenv ("dnsip2", tmp); |
| 135 | } |
stroese | e0aadfb | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 136 | #endif |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 137 | if (NetOurNISDomain[0]) |
| 138 | setenv ("domain", NetOurNISDomain); |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 139 | |
wdenk | 8d5d28a | 2005-04-02 22:37:54 +0000 | [diff] [blame] | 140 | #if (CONFIG_COMMANDS & CFG_CMD_SNTP) && (CONFIG_BOOTP_MASK & CONFIG_BOOTP_TIMEOFFSET) |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 141 | if (NetTimeOffset) { |
| 142 | sprintf (tmp, "%d", NetTimeOffset); |
| 143 | setenv ("timeoffset", tmp); |
| 144 | } |
| 145 | #endif |
wdenk | 8d5d28a | 2005-04-02 22:37:54 +0000 | [diff] [blame] | 146 | #if (CONFIG_COMMANDS & CFG_CMD_SNTP) && (CONFIG_BOOTP_MASK & CONFIG_BOOTP_NTPSERVER) |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 147 | if (NetNtpServerIP) { |
| 148 | ip_to_string (NetNtpServerIP, tmp); |
| 149 | setenv ("ntpserverip", tmp); |
| 150 | } |
| 151 | #endif |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 152 | } |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 153 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 154 | static int |
wdenk | e085e5b | 2005-04-05 23:32:21 +0000 | [diff] [blame] | 155 | netboot_common (proto_t proto, cmd_tbl_t *cmdtp, int argc, char *argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 156 | { |
| 157 | char *s; |
| 158 | int rcode = 0; |
| 159 | int size; |
| 160 | |
| 161 | /* pre-set load_addr */ |
| 162 | if ((s = getenv("loadaddr")) != NULL) { |
| 163 | load_addr = simple_strtoul(s, NULL, 16); |
| 164 | } |
| 165 | |
| 166 | switch (argc) { |
| 167 | case 1: |
| 168 | break; |
| 169 | |
| 170 | case 2: /* only one arg - accept two forms: |
| 171 | * just load address, or just boot file name. |
| 172 | * The latter form must be written "filename" here. |
| 173 | */ |
| 174 | if (argv[1][0] == '"') { /* just boot filename */ |
| 175 | copy_filename (BootFile, argv[1], sizeof(BootFile)); |
| 176 | } else { /* load address */ |
| 177 | load_addr = simple_strtoul(argv[1], NULL, 16); |
| 178 | } |
| 179 | break; |
| 180 | |
| 181 | case 3: load_addr = simple_strtoul(argv[1], NULL, 16); |
| 182 | copy_filename (BootFile, argv[2], sizeof(BootFile)); |
| 183 | |
| 184 | break; |
| 185 | |
| 186 | default: printf ("Usage:\n%s\n", cmdtp->usage); |
| 187 | return 1; |
| 188 | } |
| 189 | |
wdenk | c217f6d | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 190 | if ((size = NetLoop(proto)) < 0) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 191 | return 1; |
| 192 | |
| 193 | /* NetLoop ok, update environment */ |
| 194 | netboot_update_env(); |
| 195 | |
wdenk | c217f6d | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 196 | /* done if no file was loaded (no errors though) */ |
| 197 | if (size == 0) |
| 198 | return 0; |
| 199 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 200 | /* flush cache */ |
| 201 | flush_cache(load_addr, size); |
| 202 | |
| 203 | /* Loading ok, check if we should attempt an auto-start */ |
| 204 | if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == 0)) { |
| 205 | char *local_args[2]; |
| 206 | local_args[0] = argv[0]; |
| 207 | local_args[1] = NULL; |
| 208 | |
| 209 | printf ("Automatic boot of image at addr 0x%08lX ...\n", |
| 210 | load_addr); |
| 211 | rcode = do_bootm (cmdtp, 0, 1, local_args); |
| 212 | } |
| 213 | |
| 214 | #ifdef CONFIG_AUTOSCRIPT |
| 215 | if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) { |
| 216 | printf("Running autoscript at addr 0x%08lX ...\n", load_addr); |
| 217 | rcode = autoscript (load_addr); |
| 218 | } |
| 219 | #endif |
| 220 | return rcode; |
| 221 | } |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 222 | |
| 223 | #if (CONFIG_COMMANDS & CFG_CMD_PING) |
| 224 | int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 225 | { |
| 226 | if (argc < 2) |
| 227 | return -1; |
| 228 | |
| 229 | NetPingIP = string_to_ip(argv[1]); |
| 230 | if (NetPingIP == 0) { |
| 231 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | if (NetLoop(PING) < 0) { |
| 236 | printf("ping failed; host %s is not alive\n", argv[1]); |
| 237 | return 1; |
| 238 | } |
| 239 | |
| 240 | printf("host %s is alive\n", argv[1]); |
| 241 | |
| 242 | return 0; |
| 243 | } |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 244 | |
| 245 | U_BOOT_CMD( |
| 246 | ping, 2, 1, do_ping, |
wdenk | 6568eaa | 2004-06-09 12:42:26 +0000 | [diff] [blame] | 247 | "ping\t- send ICMP ECHO_REQUEST to network host\n", |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 248 | "pingAddress\n" |
| 249 | ); |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 250 | #endif /* CFG_CMD_PING */ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 251 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 252 | #if (CONFIG_COMMANDS & CFG_CMD_CDP) |
| 253 | |
| 254 | static void cdp_update_env(void) |
| 255 | { |
| 256 | char tmp[16]; |
| 257 | |
| 258 | if (CDPApplianceVLAN != htons(-1)) { |
| 259 | printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN)); |
| 260 | VLAN_to_string(CDPApplianceVLAN, tmp); |
| 261 | setenv("vlan", tmp); |
| 262 | NetOurVLAN = CDPApplianceVLAN; |
| 263 | } |
| 264 | |
| 265 | if (CDPNativeVLAN != htons(-1)) { |
| 266 | printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN)); |
| 267 | VLAN_to_string(CDPNativeVLAN, tmp); |
| 268 | setenv("nvlan", tmp); |
| 269 | NetOurNativeVLAN = CDPNativeVLAN; |
| 270 | } |
| 271 | |
| 272 | } |
| 273 | |
| 274 | int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 275 | { |
| 276 | int r; |
| 277 | |
| 278 | r = NetLoop(CDP); |
| 279 | if (r < 0) { |
| 280 | printf("cdp failed; perhaps not a CISCO switch?\n"); |
| 281 | return 1; |
| 282 | } |
| 283 | |
| 284 | cdp_update_env(); |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | U_BOOT_CMD( |
| 290 | cdp, 1, 1, do_cdp, |
wdenk | 6568eaa | 2004-06-09 12:42:26 +0000 | [diff] [blame] | 291 | "cdp\t- Perform CDP network configuration\n", |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 292 | ); |
| 293 | #endif /* CFG_CMD_CDP */ |
| 294 | |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 295 | #if (CONFIG_COMMANDS & CFG_CMD_SNTP) |
| 296 | int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 297 | { |
| 298 | char *toff; |
| 299 | |
| 300 | if (argc < 2) { |
| 301 | NetNtpServerIP = getenv_IPaddr ("ntpserverip"); |
| 302 | if (NetNtpServerIP == 0) { |
| 303 | printf ("ntpserverip not set\n"); |
| 304 | return (1); |
| 305 | } |
| 306 | } else { |
| 307 | NetNtpServerIP = string_to_ip(argv[1]); |
| 308 | if (NetNtpServerIP == 0) { |
| 309 | printf ("Bad NTP server IP address\n"); |
| 310 | return (1); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | toff = getenv ("timeoffset"); |
| 315 | if (toff == NULL) NetTimeOffset = 0; |
| 316 | else NetTimeOffset = simple_strtol (toff, NULL, 10); |
| 317 | |
| 318 | if (NetLoop(SNTP) < 0) { |
| 319 | printf("SNTP failed: host %s not responding\n", argv[1]); |
| 320 | return 1; |
| 321 | } |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | U_BOOT_CMD( |
| 327 | sntp, 2, 1, do_sntp, |
| 328 | "sntp\t- synchronize RTC via network\n", |
| 329 | "[NTP server IP]\n" |
| 330 | ); |
| 331 | #endif /* CFG_CMD_SNTP */ |
| 332 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 333 | #endif /* CFG_CMD_NET */ |