cmd_net.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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\t- 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\t- 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\t- 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. }
  117. static int
  118. netboot_common (int proto, cmd_tbl_t *cmdtp, int argc, char *argv[])
  119. {
  120. char *s;
  121. int rcode = 0;
  122. int size;
  123. /* pre-set load_addr */
  124. if ((s = getenv("loadaddr")) != NULL) {
  125. load_addr = simple_strtoul(s, NULL, 16);
  126. }
  127. switch (argc) {
  128. case 1:
  129. break;
  130. case 2: /* only one arg - accept two forms:
  131. * just load address, or just boot file name.
  132. * The latter form must be written "filename" here.
  133. */
  134. if (argv[1][0] == '"') { /* just boot filename */
  135. copy_filename (BootFile, argv[1], sizeof(BootFile));
  136. } else { /* load address */
  137. load_addr = simple_strtoul(argv[1], NULL, 16);
  138. }
  139. break;
  140. case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
  141. copy_filename (BootFile, argv[2], sizeof(BootFile));
  142. break;
  143. default: printf ("Usage:\n%s\n", cmdtp->usage);
  144. return 1;
  145. }
  146. if ((size = NetLoop(proto)) < 0)
  147. return 1;
  148. /* NetLoop ok, update environment */
  149. netboot_update_env();
  150. /* done if no file was loaded (no errors though) */
  151. if (size == 0)
  152. return 0;
  153. /* flush cache */
  154. flush_cache(load_addr, size);
  155. /* Loading ok, check if we should attempt an auto-start */
  156. if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == 0)) {
  157. char *local_args[2];
  158. local_args[0] = argv[0];
  159. local_args[1] = NULL;
  160. printf ("Automatic boot of image at addr 0x%08lX ...\n",
  161. load_addr);
  162. rcode = do_bootm (cmdtp, 0, 1, local_args);
  163. }
  164. #ifdef CONFIG_AUTOSCRIPT
  165. if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
  166. printf("Running autoscript at addr 0x%08lX ...\n", load_addr);
  167. rcode = autoscript (load_addr);
  168. }
  169. #endif
  170. return rcode;
  171. }
  172. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  173. int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  174. {
  175. if (argc < 2)
  176. return -1;
  177. NetPingIP = string_to_ip(argv[1]);
  178. if (NetPingIP == 0) {
  179. printf ("Usage:\n%s\n", cmdtp->usage);
  180. return -1;
  181. }
  182. if (NetLoop(PING) < 0) {
  183. printf("ping failed; host %s is not alive\n", argv[1]);
  184. return 1;
  185. }
  186. printf("host %s is alive\n", argv[1]);
  187. return 0;
  188. }
  189. U_BOOT_CMD(
  190. ping, 2, 1, do_ping,
  191. "ping\t- send ICMP ECHO_REQUEST to network host\n",
  192. "pingAddress\n"
  193. );
  194. #endif /* CFG_CMD_PING */
  195. #if (CONFIG_COMMANDS & CFG_CMD_CDP)
  196. static void cdp_update_env(void)
  197. {
  198. char tmp[16];
  199. if (CDPApplianceVLAN != htons(-1)) {
  200. printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
  201. VLAN_to_string(CDPApplianceVLAN, tmp);
  202. setenv("vlan", tmp);
  203. NetOurVLAN = CDPApplianceVLAN;
  204. }
  205. if (CDPNativeVLAN != htons(-1)) {
  206. printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
  207. VLAN_to_string(CDPNativeVLAN, tmp);
  208. setenv("nvlan", tmp);
  209. NetOurNativeVLAN = CDPNativeVLAN;
  210. }
  211. }
  212. int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  213. {
  214. int r;
  215. r = NetLoop(CDP);
  216. if (r < 0) {
  217. printf("cdp failed; perhaps not a CISCO switch?\n");
  218. return 1;
  219. }
  220. cdp_update_env();
  221. return 0;
  222. }
  223. U_BOOT_CMD(
  224. cdp, 1, 1, do_cdp,
  225. "cdp\t- Perform CDP network configuration\n",
  226. );
  227. #endif /* CFG_CMD_CDP */
  228. #endif /* CFG_CMD_NET */