cmd_net.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
  30. static int netboot_common (proto_t, cmd_tbl_t *, int , char *[]);
  31. int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  32. {
  33. return netboot_common (BOOTP, cmdtp, argc, argv);
  34. }
  35. U_BOOT_CMD(
  36. bootp, 3, 1, do_bootp,
  37. "bootp\t- boot image via network using BOOTP/TFTP protocol\n",
  38. "[loadAddress] [[hostIPaddr:]bootfilename]\n"
  39. );
  40. int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  41. {
  42. return netboot_common (TFTP, cmdtp, argc, argv);
  43. }
  44. U_BOOT_CMD(
  45. tftpboot, 3, 1, do_tftpb,
  46. "tftpboot- boot image via network using TFTP protocol\n",
  47. "[loadAddress] [[hostIPaddr:]bootfilename]\n"
  48. );
  49. int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  50. {
  51. return netboot_common (RARP, cmdtp, argc, argv);
  52. }
  53. U_BOOT_CMD(
  54. rarpboot, 3, 1, do_rarpb,
  55. "rarpboot- boot image via network using RARP/TFTP protocol\n",
  56. "[loadAddress] [[hostIPaddr:]bootfilename]\n"
  57. );
  58. #if defined(CONFIG_CMD_DHCP)
  59. int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  60. {
  61. return netboot_common(DHCP, cmdtp, argc, argv);
  62. }
  63. U_BOOT_CMD(
  64. dhcp, 3, 1, do_dhcp,
  65. "dhcp\t- boot image via network using DHCP/TFTP protocol\n",
  66. "[loadAddress] [[hostIPaddr:]bootfilename]\n"
  67. );
  68. #endif
  69. #if defined(CONFIG_CMD_NFS)
  70. int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  71. {
  72. return netboot_common(NFS, cmdtp, argc, argv);
  73. }
  74. U_BOOT_CMD(
  75. nfs, 3, 1, do_nfs,
  76. "nfs\t- boot image via network using NFS protocol\n",
  77. "[loadAddress] [[hostIPaddr:]bootfilename]\n"
  78. );
  79. #endif
  80. static void netboot_update_env (void)
  81. {
  82. char tmp[22];
  83. if (NetOurGatewayIP) {
  84. ip_to_string (NetOurGatewayIP, tmp);
  85. setenv ("gatewayip", tmp);
  86. }
  87. if (NetOurSubnetMask) {
  88. ip_to_string (NetOurSubnetMask, tmp);
  89. setenv ("netmask", tmp);
  90. }
  91. if (NetOurHostName[0])
  92. setenv ("hostname", NetOurHostName);
  93. if (NetOurRootPath[0])
  94. setenv ("rootpath", NetOurRootPath);
  95. if (NetOurIP) {
  96. ip_to_string (NetOurIP, tmp);
  97. setenv ("ipaddr", tmp);
  98. }
  99. if (NetServerIP) {
  100. ip_to_string (NetServerIP, tmp);
  101. setenv ("serverip", tmp);
  102. }
  103. if (NetOurDNSIP) {
  104. ip_to_string (NetOurDNSIP, tmp);
  105. setenv ("dnsip", tmp);
  106. }
  107. #if defined(CONFIG_BOOTP_DNS2)
  108. if (NetOurDNS2IP) {
  109. ip_to_string (NetOurDNS2IP, tmp);
  110. setenv ("dnsip2", tmp);
  111. }
  112. #endif
  113. if (NetOurNISDomain[0])
  114. setenv ("domain", NetOurNISDomain);
  115. #if defined(CONFIG_CMD_SNTP) \
  116. && defined(CONFIG_BOOTP_TIMEOFFSET)
  117. if (NetTimeOffset) {
  118. sprintf (tmp, "%d", NetTimeOffset);
  119. setenv ("timeoffset", tmp);
  120. }
  121. #endif
  122. #if defined(CONFIG_CMD_SNTP) \
  123. && defined(CONFIG_BOOTP_NTPSERVER)
  124. if (NetNtpServerIP) {
  125. ip_to_string (NetNtpServerIP, tmp);
  126. setenv ("ntpserverip", tmp);
  127. }
  128. #endif
  129. }
  130. static int
  131. netboot_common (proto_t proto, cmd_tbl_t *cmdtp, int argc, char *argv[])
  132. {
  133. char *s;
  134. char *end;
  135. int rcode = 0;
  136. int size;
  137. ulong addr;
  138. /* pre-set load_addr */
  139. if ((s = getenv("loadaddr")) != NULL) {
  140. load_addr = simple_strtoul(s, NULL, 16);
  141. }
  142. switch (argc) {
  143. case 1:
  144. break;
  145. case 2: /*
  146. * Only one arg - accept two forms:
  147. * Just load address, or just boot file name. The latter
  148. * form must be written in a format which can not be
  149. * mis-interpreted as a valid number.
  150. */
  151. addr = simple_strtoul(argv[1], &end, 16);
  152. if (end == (argv[1] + strlen(argv[1])))
  153. load_addr = addr;
  154. else
  155. copy_filename(BootFile, argv[1], sizeof(BootFile));
  156. break;
  157. case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
  158. copy_filename (BootFile, argv[2], sizeof(BootFile));
  159. break;
  160. default: cmd_usage(cmdtp);
  161. show_boot_progress (-80);
  162. return 1;
  163. }
  164. show_boot_progress (80);
  165. if ((size = NetLoop(proto)) < 0) {
  166. show_boot_progress (-81);
  167. return 1;
  168. }
  169. show_boot_progress (81);
  170. /* NetLoop ok, update environment */
  171. netboot_update_env();
  172. /* done if no file was loaded (no errors though) */
  173. if (size == 0) {
  174. show_boot_progress (-82);
  175. return 0;
  176. }
  177. /* flush cache */
  178. flush_cache(load_addr, size);
  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. printf ("Automatic boot of image at addr 0x%08lX ...\n",
  185. load_addr);
  186. show_boot_progress (82);
  187. rcode = do_bootm (cmdtp, 0, 1, local_args);
  188. }
  189. #ifdef CONFIG_AUTOSCRIPT
  190. if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
  191. printf ("Running autoscript at addr 0x%08lX", load_addr);
  192. s = getenv ("autoscript_uname");
  193. if (s)
  194. printf (":%s ...\n", s);
  195. else
  196. puts (" ...\n");
  197. show_boot_progress (83);
  198. rcode = autoscript (load_addr, s);
  199. }
  200. #endif
  201. if (rcode < 0)
  202. show_boot_progress (-83);
  203. else
  204. show_boot_progress (84);
  205. return rcode;
  206. }
  207. #if defined(CONFIG_CMD_PING)
  208. int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  209. {
  210. if (argc < 2)
  211. return -1;
  212. NetPingIP = string_to_ip(argv[1]);
  213. if (NetPingIP == 0) {
  214. cmd_usage(cmdtp);
  215. return -1;
  216. }
  217. if (NetLoop(PING) < 0) {
  218. printf("ping failed; host %s is not alive\n", argv[1]);
  219. return 1;
  220. }
  221. printf("host %s is alive\n", argv[1]);
  222. return 0;
  223. }
  224. U_BOOT_CMD(
  225. ping, 2, 1, do_ping,
  226. "ping\t- send ICMP ECHO_REQUEST to network host\n",
  227. "pingAddress\n"
  228. );
  229. #endif
  230. #if defined(CONFIG_CMD_CDP)
  231. static void cdp_update_env(void)
  232. {
  233. char tmp[16];
  234. if (CDPApplianceVLAN != htons(-1)) {
  235. printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
  236. VLAN_to_string(CDPApplianceVLAN, tmp);
  237. setenv("vlan", tmp);
  238. NetOurVLAN = CDPApplianceVLAN;
  239. }
  240. if (CDPNativeVLAN != htons(-1)) {
  241. printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
  242. VLAN_to_string(CDPNativeVLAN, tmp);
  243. setenv("nvlan", tmp);
  244. NetOurNativeVLAN = CDPNativeVLAN;
  245. }
  246. }
  247. int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  248. {
  249. int r;
  250. r = NetLoop(CDP);
  251. if (r < 0) {
  252. printf("cdp failed; perhaps not a CISCO switch?\n");
  253. return 1;
  254. }
  255. cdp_update_env();
  256. return 0;
  257. }
  258. U_BOOT_CMD(
  259. cdp, 1, 1, do_cdp,
  260. "cdp\t- Perform CDP network configuration\n",
  261. );
  262. #endif
  263. #if defined(CONFIG_CMD_SNTP)
  264. int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  265. {
  266. char *toff;
  267. if (argc < 2) {
  268. NetNtpServerIP = getenv_IPaddr ("ntpserverip");
  269. if (NetNtpServerIP == 0) {
  270. printf ("ntpserverip not set\n");
  271. return (1);
  272. }
  273. } else {
  274. NetNtpServerIP = string_to_ip(argv[1]);
  275. if (NetNtpServerIP == 0) {
  276. printf ("Bad NTP server IP address\n");
  277. return (1);
  278. }
  279. }
  280. toff = getenv ("timeoffset");
  281. if (toff == NULL) NetTimeOffset = 0;
  282. else NetTimeOffset = simple_strtol (toff, NULL, 10);
  283. if (NetLoop(SNTP) < 0) {
  284. printf("SNTP failed: host %s not responding\n", argv[1]);
  285. return 1;
  286. }
  287. return 0;
  288. }
  289. U_BOOT_CMD(
  290. sntp, 2, 1, do_sntp,
  291. "sntp\t- synchronize RTC via network\n",
  292. "[NTP server IP]\n"
  293. );
  294. #endif