cmd_net.c 7.8 KB

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