cmd_net.c 7.3 KB

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