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 | |
| 36 | static int netboot_common (int, cmd_tbl_t *, int , char *[]); |
| 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 | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 43 | U_BOOT_CMD( |
| 44 | bootp, 3, 1, do_bootp, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 45 | "bootp - boot image via network using BootP/TFTP protocol\n", |
| 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 | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 54 | U_BOOT_CMD( |
| 55 | tftpboot, 3, 1, do_tftpb, |
wdenk | eeacb89 | 2003-06-28 23:18:28 +0000 | [diff] [blame] | 56 | "tftpboot- boot image via network using TFTP protocol\n", |
wdenk | 8bde7f7 | 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 | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 65 | U_BOOT_CMD( |
| 66 | rarpboot, 3, 1, do_rarpb, |
wdenk | 8bde7f7 | 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 | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 76 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 77 | U_BOOT_CMD( |
| 78 | dhcp, 3, 1, do_dhcp, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 79 | "dhcp - invoke DHCP client to obtain IP/boot params\n", |
| 80 | "\n" |
| 81 | ); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 82 | #endif /* CFG_CMD_DHCP */ |
| 83 | |
| 84 | static void netboot_update_env(void) |
| 85 | { |
| 86 | char tmp[16] ; |
| 87 | |
| 88 | if (NetOurGatewayIP) { |
| 89 | ip_to_string (NetOurGatewayIP, tmp); |
| 90 | setenv("gatewayip", tmp); |
| 91 | } |
| 92 | |
| 93 | if (NetOurSubnetMask) { |
| 94 | ip_to_string (NetOurSubnetMask, tmp); |
| 95 | setenv("netmask", tmp); |
| 96 | } |
| 97 | |
| 98 | if (NetOurHostName[0]) |
| 99 | setenv("hostname", NetOurHostName); |
| 100 | |
| 101 | if (NetOurRootPath[0]) |
| 102 | setenv("rootpath", NetOurRootPath); |
| 103 | |
| 104 | if (NetOurIP) { |
| 105 | ip_to_string (NetOurIP, tmp); |
| 106 | setenv("ipaddr", tmp); |
| 107 | } |
| 108 | |
| 109 | if (NetServerIP) { |
| 110 | ip_to_string (NetServerIP, tmp); |
| 111 | setenv("serverip", tmp); |
| 112 | } |
| 113 | |
| 114 | if (NetOurDNSIP) { |
| 115 | ip_to_string (NetOurDNSIP, tmp); |
| 116 | setenv("dnsip", tmp); |
| 117 | } |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 118 | |
stroese | fe389a8 | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 119 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2) |
| 120 | if (NetOurDNS2IP) { |
| 121 | ip_to_string (NetOurDNS2IP, tmp); |
| 122 | setenv("dnsip2", tmp); |
| 123 | } |
| 124 | #endif |
| 125 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 126 | if (NetOurNISDomain[0]) |
| 127 | setenv("domain", NetOurNISDomain); |
| 128 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 129 | } |
| 130 | static int |
| 131 | netboot_common (int proto, cmd_tbl_t *cmdtp, int argc, char *argv[]) |
| 132 | { |
| 133 | char *s; |
| 134 | int rcode = 0; |
| 135 | int size; |
| 136 | |
| 137 | /* pre-set load_addr */ |
| 138 | if ((s = getenv("loadaddr")) != NULL) { |
| 139 | load_addr = simple_strtoul(s, NULL, 16); |
| 140 | } |
| 141 | |
| 142 | switch (argc) { |
| 143 | case 1: |
| 144 | break; |
| 145 | |
| 146 | case 2: /* only one arg - accept two forms: |
| 147 | * just load address, or just boot file name. |
| 148 | * The latter form must be written "filename" here. |
| 149 | */ |
| 150 | if (argv[1][0] == '"') { /* just boot filename */ |
| 151 | copy_filename (BootFile, argv[1], sizeof(BootFile)); |
| 152 | } else { /* load address */ |
| 153 | load_addr = simple_strtoul(argv[1], NULL, 16); |
| 154 | } |
| 155 | break; |
| 156 | |
| 157 | case 3: load_addr = simple_strtoul(argv[1], NULL, 16); |
| 158 | copy_filename (BootFile, argv[2], sizeof(BootFile)); |
| 159 | |
| 160 | break; |
| 161 | |
| 162 | default: printf ("Usage:\n%s\n", cmdtp->usage); |
| 163 | return 1; |
| 164 | } |
| 165 | |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 166 | if ((size = NetLoop(proto)) < 0) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 167 | return 1; |
| 168 | |
| 169 | /* NetLoop ok, update environment */ |
| 170 | netboot_update_env(); |
| 171 | |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 172 | /* done if no file was loaded (no errors though) */ |
| 173 | if (size == 0) |
| 174 | return 0; |
| 175 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 176 | /* flush cache */ |
| 177 | flush_cache(load_addr, size); |
| 178 | |
| 179 | /* Loading ok, check if we should attempt an auto-start */ |
| 180 | if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == 0)) { |
| 181 | char *local_args[2]; |
| 182 | local_args[0] = argv[0]; |
| 183 | local_args[1] = NULL; |
| 184 | |
| 185 | printf ("Automatic boot of image at addr 0x%08lX ...\n", |
| 186 | load_addr); |
| 187 | rcode = do_bootm (cmdtp, 0, 1, local_args); |
| 188 | } |
| 189 | |
| 190 | #ifdef CONFIG_AUTOSCRIPT |
| 191 | if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) { |
| 192 | printf("Running autoscript at addr 0x%08lX ...\n", load_addr); |
| 193 | rcode = autoscript (load_addr); |
| 194 | } |
| 195 | #endif |
| 196 | return rcode; |
| 197 | } |
| 198 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 199 | #if (CONFIG_COMMANDS & CFG_CMD_PING) |
| 200 | int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 201 | { |
| 202 | if (argc < 2) |
| 203 | return -1; |
| 204 | |
| 205 | NetPingIP = string_to_ip(argv[1]); |
| 206 | if (NetPingIP == 0) { |
| 207 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | if (NetLoop(PING) < 0) { |
| 212 | printf("ping failed; host %s is not alive\n", argv[1]); |
| 213 | return 1; |
| 214 | } |
| 215 | |
| 216 | printf("host %s is alive\n", argv[1]); |
| 217 | |
| 218 | return 0; |
| 219 | } |
wdenk | 6dff552 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 220 | |
| 221 | U_BOOT_CMD( |
| 222 | ping, 2, 1, do_ping, |
| 223 | "ping - send ICMP ECHO_REQUEST to network host\n", |
| 224 | "pingAddress\n" |
| 225 | ); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 226 | #endif /* CFG_CMD_PING */ |
| 227 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 228 | #endif /* CFG_CMD_NET */ |