cmd_net.c 6.5 KB

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